1 /**************************************************************************** 2 **+-----------------------------------------------------------------------+** 3 **| |** 4 **| Copyright(c) 1998 - 2008 Texas Instruments. All rights reserved. |** 5 **| All rights reserved. |** 6 **| |** 7 **| Redistribution and use in source and binary forms, with or without |** 8 **| modification, are permitted provided that the following conditions |** 9 **| are met: |** 10 **| |** 11 **| * Redistributions of source code must retain the above copyright |** 12 **| notice, this list of conditions and the following disclaimer. |** 13 **| * Redistributions in binary form must reproduce the above copyright |** 14 **| notice, this list of conditions and the following disclaimer in |** 15 **| the documentation and/or other materials provided with the |** 16 **| distribution. |** 17 **| * Neither the name Texas Instruments nor the names of its |** 18 **| contributors may be used to endorse or promote products derived |** 19 **| from this software without specific prior written permission. |** 20 **| |** 21 **| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |** 22 **| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |** 23 **| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |** 24 **| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |** 25 **| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |** 26 **| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |** 27 **| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |** 28 **| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |** 29 **| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |** 30 **| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |** 31 **| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |** 32 **| |** 33 **+-----------------------------------------------------------------------+** 34 ****************************************************************************/ 35 36 #include "IPCKernelApi.h" 37 #include "EvHandler.h" 38 #include "osAdapter.h" 39 /*#include "configMgr.h"*/ 40 41 42 #ifdef EV_HANDLER_DEBUG 43 TI_HANDLE ghEvHandler; /* for debug, remove later*/ 44 #endif 45 46 /* ************************** Upper Interface **********************************/ 47 48 TI_HANDLE EvHandlerInit(TI_HANDLE hOs) 49 { 50 51 EV_HANDLER_STRUCT_T* pEvHandler; 52 PTIWLN_ADAPTER_T pAdapter = (PTIWLN_ADAPTER_T) hOs; 53 54 PRINT(DBG_INIT_LOUD, (" EvHandlerInit\n")); 55 pEvHandler = os_memoryAlloc(hOs,sizeof(EV_HANDLER_STRUCT_T)); 56 os_memoryZero(hOs,pEvHandler,sizeof(EV_HANDLER_STRUCT_T)); 57 58 #ifdef EV_HANDLER_DEBUG 59 ghEvHandler= pEvHandler; 60 PRINTF(DBG_INIT_VERY_LOUD, ("EvHandlerInit: ghEvHandler set to %08X\n", ghEvHandler)); 61 #endif 62 63 pEvHandler->hOs = hOs; 64 65 pAdapter->hEvHandler = pEvHandler; 66 67 pEvHandler->LastUMEventType = 0xFFFFFFFF; 68 69 70 return (TI_HANDLE) pEvHandler; 71 } 72 73 UINT32 EvHandlerUnload (TI_HANDLE hEvHandler){ 74 75 EV_HANDLER_STRUCT_T* pEvHandler; 76 77 PRINT(DBG_INIT_LOUD, (" ev_handler_unLoad\n")); 78 pEvHandler = (EV_HANDLER_STRUCT_T *)hEvHandler; 79 80 os_memoryFree(pEvHandler->hOs,pEvHandler,sizeof(EV_HANDLER_STRUCT_T)); 81 82 return OK; 83 } 84 85 86 UINT32 EvHandlerRegisterEvent(TI_HANDLE hEvHandler, PUCHAR pData, ULONG Length) 87 { 88 EV_HANDLER_STRUCT_T* pEvHandler; 89 IPC_EVENT_PARAMS* pEvParams; 90 UINT32 ModuleIndex; 91 92 if( (hEvHandler==NULL) || (pData == NULL)){ 93 PRINT(DBG_INIT_ERROR, "EvHandler:EvHandlerRegisterEvent Bad Handle passed \n"); 94 return NOK; 95 } 96 97 #ifdef EV_HANDLER_DEBUG 98 if (ghEvHandler != hEvHandler) 99 { 100 return NOK; 101 } 102 #endif 103 104 pEvHandler = (EV_HANDLER_STRUCT_T*)hEvHandler; 105 106 pEvParams = (IPC_EVENT_PARAMS*)pData; 107 108 109 PRINTF(DBG_INIT_LOUD, (" EvHandlerRegisterEvent EventType = %d \n",pEvParams->uEventType)); 110 111 /* used to be: if ( sizeof(IPC_EVENT_PARAMS) != Length) 112 relaxed size checking (okay if output buffer is larger) */ 113 if ( sizeof(IPC_EVENT_PARAMS) > Length) 114 { 115 PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error sizeof(IPC_EVENT_PARAMS) != Length," 116 "%d != %d \n",sizeof(IPC_EVENT_PARAMS), (int)Length)); 117 return (UINT32)STATUS_INVALID_PARAMETER; 118 } 119 120 if( pEvParams->uEventType >= IPC_EVENT_MAX){ 121 PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error - Invalid Event Type = %d \n", 122 pEvParams->uEventType)); 123 return (UINT32)STATUS_INVALID_PARAMETER; 124 } 125 126 ModuleIndex = 0; 127 128 129 while ( (pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID != NULL ) 130 && ( ModuleIndex < MAX_REGISTERED_MODULES) ) 131 { 132 ModuleIndex++; 133 } 134 135 if(ModuleIndex == MAX_REGISTERED_MODULES) 136 { 137 PRINTF(DBG_INIT_WARNING, (" EvHandlerRegisterEvent %d" 138 "Registration queue full or event already registered! %x address \n", 139 pEvParams->uEventType, 140 (int)(pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID))); 141 return (UINT32)STATUS_INVALID_PARAMETER; 142 } 143 144 os_memoryCopy(pEvHandler->hOs,(UINT8*)&pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex], 145 (UINT8*)pEvParams,Length); 146 147 pEvParams->uEventID = (TI_HANDLE)&pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex]; 148 149 pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID = pEvParams->uEventID; 150 151 PRINT(DBG_INIT_LOUD, " EvHandlerRegisterEvent Out \n"); 152 153 return STATUS_SUCCESS; 154 155 } 156 157 158 UINT32 EvHandlerUnRegisterEvent(TI_HANDLE hEvHandler, TI_HANDLE uEventID) 159 { 160 EV_HANDLER_STRUCT_T* pEvHandler; 161 IPC_EVENT_PARAMS* pEvParams; 162 UINT32 ModuleIndex; 163 164 #ifdef EV_HANDLER_DEBUG 165 if (ghEvHandler != hEvHandler ) 166 { 167 return NOK; 168 } 169 #endif 170 171 if (uEventID == NULL) 172 { 173 return NOK; 174 } 175 176 pEvHandler = (EV_HANDLER_STRUCT_T*)hEvHandler; 177 pEvParams = (IPC_EVENT_PARAMS*)uEventID; 178 179 180 PRINTF(DBG_INIT_LOUD, (" EvHandlerUnRegisterEvent EventType = %d \n",pEvParams->uEventType)); 181 182 if( pEvParams->uEventType >= IPC_EVENT_MAX){ 183 PRINTF(DBG_INIT_ERROR, (" EvHandlerRegisterEvent Error Event Type = %d \n", 184 pEvParams->uEventType)); 185 return (UINT32)STATUS_INVALID_PARAMETER; 186 } 187 188 ModuleIndex = 0; 189 190 while ( (pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID != pEvParams->uEventID ) 191 && ( ModuleIndex < MAX_REGISTERED_MODULES) ) 192 { 193 ModuleIndex++; 194 } 195 196 if(ModuleIndex == MAX_REGISTERED_MODULES) 197 { 198 PRINTF(DBG_INIT_ERROR, (" EvHandlerUnRegisterEvent %d" 199 "Registration queue doesn't hold this event! %x address \n", 200 pEvParams->uEventType, 201 (int)(pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID))); 202 return (UINT32)STATUS_INVALID_PARAMETER; 203 } 204 205 pEvHandler->RegistrationArray[pEvParams->uEventType][ModuleIndex].uEventID = NULL; 206 207 return STATUS_SUCCESS; 208 } 209 210 211 /* Doesn't support pLength less than expected needed size*/ 212 UINT32 EvHandlerGetEventData(TI_HANDLE hEvHandler, PUCHAR pData, ULONG* pLength) 213 { 214 EV_HANDLER_STRUCT_T* pEvHandler; 215 UINT32 HeadIndex; 216 IPC_EV_DATA* pNewEvent; 217 218 if(pData == NULL){ 219 /*PRINT(DBG_INIT_ERROR, "EvHandler:EvHandlerGetEventData Bad Handle passed \n");*/ 220 return NOK; 221 } 222 223 #ifdef EV_HANDLER_DEBUG 224 if (ghEvHandler != hEvHandler) 225 { 226 return NOK; 227 } 228 #endif 229 230 pEvHandler = (EV_HANDLER_STRUCT_T*)hEvHandler; 231 HeadIndex = pEvHandler->SendEventArray.HeadIndex; 232 pNewEvent = &pEvHandler->SendEventArray.Array[HeadIndex]; 233 234 PRINTF(DBG_INIT_LOUD, (" IPCGetEventData Getting Event = %d", 235 pEvHandler->SendEventArray.Array[HeadIndex].EvParams.uEventType)); 236 237 if (pEvHandler->SendEventArray.Counter == 0) 238 { 239 PRINT(DBG_INIT_ERROR, "Attempting to get from empty queue"); 240 return NOK; 241 } 242 *pLength = sizeof(IPC_EV_DATA); 243 244 os_memoryCopy(pEvHandler->hOs, pData,pNewEvent, sizeof(IPC_EV_DATA)); 245 246 pEvHandler->SendEventArray.HeadIndex = (HeadIndex+1) % MAX_SEND_EVENTS; 247 pEvHandler->SendEventArray.Counter--; 248 HeadIndex = pEvHandler->SendEventArray.HeadIndex; 249 250 /* SEND SIGNAL FOR THE NEXT EVENT*/ 251 if (pEvHandler->SendEventArray.Counter != 0) 252 { 253 IPC_EventSend (pEvHandler->hOs,NULL,0); 254 } 255 256 PRINTF(DBG_INIT_LOUD, ("EvHandlerGetEventData: Normal Exit\n")); 257 258 return STATUS_SUCCESS; 259 } 260 261 /* ************************** Upper Interface End*********************************/ 262 263 /* ************************** Bottom Interface **********************************/ 264 265 UINT32 EvHandlerSendEvent(TI_HANDLE hEvHandler, UINT32 EvType, UINT8* pData, UINT32 Length) 266 { 267 EV_HANDLER_STRUCT_T* pEvHandler; 268 IPC_EV_DATA* pNewEvent; 269 UINT32 TailIndex; 270 UINT32 ModuleIndex=0; 271 272 PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d \n", EvType)); 273 274 if(hEvHandler == NULL){ 275 PRINT(DBG_INIT_ERROR, "EvHandlerSendEvent Bad Handle passed \n"); 276 return NOK; 277 } 278 279 #ifdef EV_HANDLER_DEBUG 280 if (ghEvHandler != hEvHandler) 281 { 282 return NOK; 283 } 284 #endif 285 286 pEvHandler = (EV_HANDLER_STRUCT_T*)hEvHandler; 287 288 TailIndex = pEvHandler->SendEventArray.TailIndex; 289 290 while ((ModuleIndex < MAX_REGISTERED_MODULES) && (EvType <= IPC_EVENT_MAX)) 291 { 292 293 if (pEvHandler->RegistrationArray[EvType][ModuleIndex].uEventID != NULL ) 294 { 295 if(pEvHandler->SendEventArray.Counter == MAX_SEND_EVENTS){ 296 PRINT(DBG_INIT_ERROR, " EvHandlerSendEvent Array Full u Fool! \n"); 297 return NOK; 298 } 299 300 pNewEvent = &pEvHandler->SendEventArray.Array[TailIndex]; 301 302 /* copy the event parameters and data to the events queue*/ 303 os_memoryCopy(pEvHandler->hOs,(UINT8*)&pNewEvent->EvParams, 304 (UINT8*)&pEvHandler->RegistrationArray[EvType][ModuleIndex], 305 sizeof(IPC_EVENT_PARAMS)); 306 307 if( Length > MAX_EVENT_DATA_SIZE ) { /* Dm: Security fix */ 308 PRINTF(DBG_INIT_ERROR, ("TI: %s Security Error\n", __FUNCTION__)); 309 return NOK; 310 } 311 os_memoryCopy(pEvHandler->hOs,(UINT8*)pNewEvent->uBuffer, 312 (UINT8*)pData,Length); 313 314 pNewEvent->uBufferSize = Length; 315 316 if(pNewEvent->EvParams.uDeliveryType == DELIVERY_PUSH) 317 { 318 PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d to OS \n", EvType)); 319 PRINTF(DBG_INIT_LOUD, ("EvHandlerSendEvent Matching OS Registered event found at EvType = %d," 320 "ModuleIndex = %d \n", EvType, ModuleIndex)); 321 IPC_EventSend (pEvHandler->hOs,(UINT8*)pNewEvent,sizeof(IPC_EV_DATA)); 322 } 323 else { 324 325 pEvHandler->LastUMEventType = EvType; 326 pEvHandler->SendEventArray.TailIndex = (TailIndex+1) % MAX_SEND_EVENTS; 327 pEvHandler->SendEventArray.Counter++; 328 TailIndex = pEvHandler->SendEventArray.TailIndex; 329 PRINTF(DBG_INIT_LOUD, (" EvHandlerSendEvent %d to User Mode \n", EvType)); 330 PRINTF(DBG_INIT_LOUD, ("EvHandlerSendEvent Matching User Mode Registered event found at EvType = %d," 331 "ModuleIndex = %d \n", EvType, ModuleIndex)); 332 if (pEvHandler->SendEventArray.Counter == 1) 333 { 334 IPC_EventSend (pEvHandler->hOs,NULL,0); 335 } 336 } 337 338 } /* end if*/ 339 340 ModuleIndex++; 341 342 } /* end of while*/ 343 344 return OK; 345 } 346 347 /* ************************** Bottom Interface End **********************************/ 348 349