Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 NXP Semiconductors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 /*!
     17  * \file  phHal4Nfc.c
     18  * \brief Hal4Nfc source.
     19  *
     20  * Project: NFC-FRI 1.1
     21  *
     22  * $Date: Fri Jun 11 09:32:23 2010 $
     23  * $Author: ing07385 $
     24  * $Revision: 1.192 $
     25  * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
     26  *
     27  */
     28 
     29 /* ---------------------------Include files ---------------------------------*/
     30 #include <phHal4Nfc.h>
     31 #include <phHal4Nfc_Internal.h>
     32 #include <phOsalNfc.h>
     33 #include <phHciNfc.h>
     34 #include <phLlcNfc.h>
     35 #include <phDal4Nfc.h>
     36 //#include <phDnldNfc.h>
     37 #include <phOsalNfc_Timer.h>
     38 
     39 
     40 
     41 /* ------------------------------- Macros -----------------------------------*/
     42 #ifndef HAL_UNIT_TEST
     43 #define STATIC static
     44 #else
     45 #define STATIC
     46 #endif/*#ifndef UNIT_TEST*/
     47 #define HAL4_LAYERS                 3
     48 #define LAYER_HCI                   2
     49 #define LAYER_LLC                   1
     50 #define LAYER_DAL                   0
     51 
     52 /* --------------------Structures and enumerations --------------------------*/
     53 
     54 phHal_sHwReference_t *gpphHal4Nfc_Hwref;
     55 
     56 static void phHal4Nfc_IoctlComplete(
     57                                     phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     58                                     void     *pInfo
     59                                     );
     60 
     61 static void phHal4Nfc_LowerNotificationHandler(
     62                                         void    *pContext,
     63                                         void    *pHwRef,
     64                                         uint8_t  type,
     65                                         void     *pInfo
     66                                         );
     67 static void phHal4Nfc_HandleEvent(
     68                               phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     69                               void     *pInfo
     70                               );
     71 
     72 static void phHal4Nfc_OpenComplete(
     73                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     74                                    void *pInfo
     75                                    );
     76 
     77 static void phHal4Nfc_CloseComplete(
     78                                     phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     79                                     void *pInfo
     80                                     );
     81 
     82 static NFCSTATUS phHal4Nfc_Configure_Layers(
     83                                 phNfcLayer_sCfg_t       **pphLayer
     84                                 );
     85 
     86 
     87 /*Callback for Self tests*/
     88 static void phHal4Nfc_SelfTestComplete(
     89                                        phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     90                                        void *pInfo
     91                                        );
     92 
     93 /**
     94  *  The open callback function to be called by the HCI when open (initializaion)
     95  *  sequence is completed  or if there is an error in initialization.
     96  *  It is passed as a parameter to HCI when calling HCI Init.
     97  */
     98 
     99 static void phHal4Nfc_OpenComplete(
    100                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
    101                                    void *pInfo
    102                                    )
    103 {
    104     NFCSTATUS status = ((phNfc_sCompletionInfo_t *)pInfo)->status;
    105     pphHal4Nfc_GenCallback_t pUpper_OpenCb
    106                                     = Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb;
    107     void                   *pUpper_Context
    108                                 = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
    109     if(status == NFCSTATUS_SUCCESS)
    110     {
    111         PHDBG_INFO("Hal4:Open Successful");
    112 #ifdef MERGE_SAK_SW1 /*Software Workaround*/
    113         if(eHal4StateOpenAndReady == Hal4Ctxt->Hal4NextState)
    114         {
    115             status = phHciNfc_System_Configure (
    116                                     Hal4Ctxt->psHciHandle,
    117                                     (void *)gpphHal4Nfc_Hwref,
    118                                     PH_HAL4NFC_TGT_MERGE_ADDRESS,
    119                                     PH_HAL4NFC_TGT_MERGE_SAK /*config value*/
    120                                     );
    121         }
    122         if(NFCSTATUS_PENDING != status)
    123 #endif/*#ifdef MERGE_SAK_SW1*/
    124         {
    125             /*Update State*/
    126             Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
    127             Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
    128             Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb = NULL;
    129             if(NULL != pUpper_OpenCb)
    130             {
    131                 /*Upper layer's Open Cb*/
    132                 (*pUpper_OpenCb)(Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
    133                     NFCSTATUS_SUCCESS
    134                     );
    135             }
    136         }
    137     }
    138     else/*Open did not succeed.Go back to reset state*/
    139     {
    140         Hal4Ctxt->Hal4CurrentState = eHal4StateClosed;
    141         Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
    142         Hal4Ctxt->psHciHandle = NULL;
    143         phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
    144         Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
    145         phOsalNfc_FreeMemory((void *)Hal4Ctxt);
    146         gpphHal4Nfc_Hwref->hal_context = NULL;
    147         gpphHal4Nfc_Hwref = NULL;
    148         PHDBG_INFO("Hal4:Open Failed");
    149         /*Call upper layer's Open Cb with error status*/
    150         (*pUpper_OpenCb)(pUpper_Context,status);
    151     }
    152     return;
    153 }
    154 
    155 /**
    156  *  The close callback function called by the HCI when close  sequence is
    157  *  completed or if there is an error in closing.
    158  *  It is passed as a parameter to HCI when calling HCI Release.
    159  */
    160 static void phHal4Nfc_CloseComplete(
    161                                     phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
    162                                     void *pInfo
    163                                     )
    164 {
    165     NFCSTATUS   status= ((phNfc_sCompletionInfo_t *)pInfo)->status;
    166     pphHal4Nfc_GenCallback_t pUpper_CloseCb;
    167     void                    *pUpper_Context;
    168     uint8_t                 RemoteDevNumber = 0;
    169     pUpper_CloseCb = Hal4Ctxt->sUpperLayerInfo.pUpperCloseCb;
    170     pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
    171     /*Update state*/
    172     Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
    173     Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
    174     /*If Closed successfully*/
    175     if(NFCSTATUS_SUCCESS == status)
    176     {
    177         Hal4Ctxt->psHciHandle = NULL;
    178         /*Free all heap allocations*/
    179         phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
    180         Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
    181         /*Free ADD context info*/
    182         if(NULL != Hal4Ctxt->psADDCtxtInfo)
    183         {
    184             while(RemoteDevNumber < MAX_REMOTE_DEVICES)
    185             {
    186                 if(NULL != Hal4Ctxt->rem_dev_list[RemoteDevNumber])
    187                 {
    188                     phOsalNfc_FreeMemory((void *)
    189                                     (Hal4Ctxt->rem_dev_list[RemoteDevNumber]));
    190                     Hal4Ctxt->rem_dev_list[RemoteDevNumber] = NULL;
    191                 }
    192                 RemoteDevNumber++;
    193             }
    194             Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    195             phOsalNfc_FreeMemory(Hal4Ctxt->psADDCtxtInfo);
    196         }/*if(NULL != Hal4Ctxt->psADDCtxtInfo)*/
    197         /*Free Trcv context info*/
    198         if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    199         {
    200             if(NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
    201             {
    202                     phOsalNfc_FreeMemory(
    203                         Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer
    204                         );
    205             }
    206             if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
    207                 && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
    208             {
    209                 phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
    210             }
    211             phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
    212         }/*if(NULL != Hal4Ctxt->psTrcvCtxtInfo)*/
    213         /*Free Hal context and Hardware reference*/
    214         gpphHal4Nfc_Hwref->hal_context = NULL;
    215         gpphHal4Nfc_Hwref = NULL;
    216         phOsalNfc_FreeMemory((void *)Hal4Ctxt);
    217     }/* if(NFCSTATUS_SUCCESS == status)*/
    218     /*Call Upper layer's Close Cb with status*/
    219     (*pUpper_CloseCb)(pUpper_Context,status);
    220     return;
    221 }
    222 
    223 
    224 /*
    225 * For configuring the various layers during the Initialization call
    226 *
    227 *
    228 */
    229 static
    230 NFCSTATUS
    231 phHal4Nfc_Configure_Layers(
    232                         phNfcLayer_sCfg_t       **pphLayer
    233                         )
    234 {
    235     uint8_t index = HAL4_LAYERS - 1;
    236     uint8_t i = 0;
    237     NFCSTATUS status = NFCSTATUS_SUCCESS ;
    238     PHDBG_INFO("Hal4:Configuring layers");
    239     *pphLayer = (phNfcLayer_sCfg_t *) phOsalNfc_GetMemory(
    240                             sizeof(phNfcLayer_sCfg_t) * HAL4_LAYERS);
    241 
    242     if( NULL == *pphLayer)
    243     {
    244         status = PHNFCSTVAL(CID_NFC_HAL,
    245                     NFCSTATUS_INSUFFICIENT_RESOURCES);/*Memory allocation error*/
    246     }
    247     else
    248     {
    249 
    250         (void)memset((void *)*pphLayer,0,(
    251                                 sizeof(phNfcLayer_sCfg_t) * HAL4_LAYERS));
    252 
    253         for(i=0 ; i < HAL4_LAYERS ;i++, index-- )
    254         {
    255             (*pphLayer + i)->layer_index = index;
    256             switch(index)
    257             {
    258                 case LAYER_HCI: /*Configure Hci*/
    259                 {
    260                     (*pphLayer+i)->layer_name  =(uint8_t *) "Hci";
    261                     (*pphLayer+i)->layer_registry  = NULL;
    262                     (*pphLayer+i)->layer_next  =
    263                                     (((phNfcLayer_sCfg_t *)*pphLayer) + i + 1);
    264                     break;
    265                 }
    266                 case LAYER_LLC:/*Configure LLC*/
    267                 {
    268                     (*pphLayer+i)->layer_registry  = phLlcNfc_Register;
    269                     (*pphLayer+i)->layer_name  = (uint8_t *)"Llc";
    270                     (*pphLayer+i)->layer_next  =
    271                                     (((phNfcLayer_sCfg_t *)*pphLayer) + i + 1);
    272                     break;
    273                 }
    274                 case LAYER_DAL: /*Configure the DAL*/
    275                 {
    276                     (*pphLayer+i)->layer_registry  = phDal4Nfc_Register;
    277                     (*pphLayer+i)->layer_name  = (uint8_t *)"Dal";
    278                     (*pphLayer+i)->layer_next  = NULL ;
    279                     break;
    280                 }
    281                 default:
    282                     break;
    283             } /* End of Switch */
    284         }   /* End of For Loop */
    285     }   /* End of NULL Check */
    286 
    287     return status ;
    288 }
    289 
    290 
    291 /**
    292  *  The open function called by the upper HAL when HAL4 is to be opened
    293  *  (initialized).
    294  *
    295  */
    296 NFCSTATUS phHal4Nfc_Open(
    297                          phHal_sHwReference_t       *psHwReference,
    298                          phHal4Nfc_InitType_t        InitType,
    299                          pphHal4Nfc_GenCallback_t    pOpenCallback,
    300                          void                       *pContext
    301                          )
    302 {
    303     NFCSTATUS openRetVal = NFCSTATUS_SUCCESS;
    304     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    305     phHciNfc_Init_t   eHciInitType = (phHciNfc_Init_t)InitType;
    306     /*Set Default Clock settings once*/
    307     static phHal_sHwConfig_t sHwConfig = {
    308         {0},
    309         NXP_DEFAULT_CLK_REQUEST,
    310         NXP_DEFAULT_INPUT_CLK
    311         };
    312     /*NULL checks*/
    313     if(NULL == psHwReference || NULL == pOpenCallback)
    314     {
    315         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    316         openRetVal = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    317     }
    318     else if(NULL != gpphHal4Nfc_Hwref)
    319     {
    320         /*Hal4 context is open or open in progress ,return Ctxt already open*/
    321         openRetVal =  PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_ALREADY_INITIALISED);
    322     }
    323     else/*Do an initialization*/
    324     {
    325         /*If hal4 ctxt in Hwreference is NULL create a new context*/
    326         if(NULL == ((phHal_sHwReference_t *)psHwReference)->hal_context)
    327         {
    328             Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)
    329                                 phOsalNfc_GetMemory((uint32_t)sizeof(
    330                                                         phHal4Nfc_Hal4Ctxt_t)
    331                                                         );
    332             ((phHal_sHwReference_t *)psHwReference)->hal_context = Hal4Ctxt;
    333         }
    334         else/*Take context from Hw reference*/
    335         {
    336             Hal4Ctxt = ((phHal_sHwReference_t *)psHwReference)->hal_context;
    337         }
    338         if(NULL == Hal4Ctxt)
    339         {
    340             openRetVal = PHNFCSTVAL(CID_NFC_HAL,
    341                         NFCSTATUS_INSUFFICIENT_RESOURCES);
    342         }
    343         else
    344         {
    345             (void)memset((void *)Hal4Ctxt,
    346                         0,
    347                         ((uint32_t)sizeof(phHal4Nfc_Hal4Ctxt_t)));
    348             /* Configure layers if not configured */
    349             if( NULL == Hal4Ctxt->pHal4Nfc_LayerCfg )
    350             {
    351                 openRetVal = phHal4Nfc_Configure_Layers(
    352                                                   &(Hal4Ctxt->pHal4Nfc_LayerCfg)
    353                                                   );
    354             }
    355 
    356             if( openRetVal == NFCSTATUS_SUCCESS )
    357             {
    358                 /*update Next state*/
    359                 Hal4Ctxt->Hal4NextState = (HCI_SELF_TEST == eHciInitType?
    360                                 eHal4StateSelfTestMode:eHal4StateOpenAndReady);
    361                 /*Store callback and context ,and set Default settings in Context*/
    362                 Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb = pOpenCallback;
    363                 Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    364                 Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_DEACTIVATED;
    365                 gpphHal4Nfc_Hwref = psHwReference;
    366                 PHDBG_INFO("Hal4:Calling Hci-Init");
    367                 openRetVal = phHciNfc_Initialise (
    368                                         (void *)&Hal4Ctxt->psHciHandle,
    369                                         psHwReference,
    370                                         eHciInitType,
    371                                         &sHwConfig,
    372                                         (pphNfcIF_Notification_CB_t)
    373                                             phHal4Nfc_LowerNotificationHandler,
    374                                         (void *)Hal4Ctxt,
    375                                         Hal4Ctxt->pHal4Nfc_LayerCfg
    376                                         );
    377                 /*Hci Init did not succeed.free Resources and return*/
    378                 if( (openRetVal != NFCSTATUS_SUCCESS)
    379                             && (PHNFCSTATUS (openRetVal) != NFCSTATUS_PENDING) )
    380                 {
    381                     phOsalNfc_FreeMemory(Hal4Ctxt->pHal4Nfc_LayerCfg);
    382                     phOsalNfc_FreeMemory(Hal4Ctxt);
    383                     Hal4Ctxt = NULL;
    384                 }
    385             }/*if( openRetVal == NFCSTATUS_SUCCESS )*/
    386             else/*Free the context*/
    387             {
    388                 phOsalNfc_FreeMemory(Hal4Ctxt);
    389             }/*else*/
    390         }
    391     }
    392     return openRetVal;
    393 }
    394 
    395 /**  The I/O Control function allows the caller to use (vendor-) specific
    396 *  functionality provided by the lower layer or by the hardware. */
    397 NFCSTATUS phHal4Nfc_Ioctl(
    398                           phHal_sHwReference_t       *psHwReference,
    399                           uint32_t                    IoctlCode,
    400                           phNfc_sData_t              *pInParam,
    401                           phNfc_sData_t              *pOutParam,
    402                           pphHal4Nfc_IoctlCallback_t  pIoctlCallback,
    403                           void                       *pContext
    404                           )
    405 {
    406     NFCSTATUS RetStatus = NFCSTATUS_FAILED;
    407     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    408     uint32_t config_type = 0;
    409     uint8_t ind = 0;
    410     /*NULL checks*/
    411     if((NULL == psHwReference)
    412         || (NULL == pIoctlCallback)
    413         )
    414     {
    415         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    416         RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    417     }
    418     /*Only the Ioctls NFC_FW_DOWNLOAD_CHECK and NFC_FW_DOWNLOAD are allowed in
    419       the uninitialized state of HAL*/
    420     else if(NULL == psHwReference->hal_context)
    421     {
    422 #ifdef FW_DOWNLOAD
    423         if(NFC_FW_DOWNLOAD_CHECK == IoctlCode)
    424         {
    425             RetStatus = phDnldNfc_Run_Check(
    426                 psHwReference
    427                 );
    428         }
    429         else if((NFC_FW_DOWNLOAD == IoctlCode)
    430             &&(NULL == gpphHal4Nfc_Hwref))/*Indicates current state is shutdown*/
    431         {
    432             Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)
    433                 phOsalNfc_GetMemory((uint32_t)sizeof(
    434                                                 phHal4Nfc_Hal4Ctxt_t)
    435                                                 );
    436             if(NULL == Hal4Ctxt)
    437             {
    438                 RetStatus = PHNFCSTVAL(CID_NFC_HAL,
    439                     NFCSTATUS_INSUFFICIENT_RESOURCES);
    440             }
    441             else
    442             {
    443                 ((phHal_sHwReference_t *)psHwReference)->hal_context
    444                     = Hal4Ctxt;
    445                 (void)memset((void *)Hal4Ctxt,
    446                                  0,
    447                                    ((uint32_t)sizeof(phHal4Nfc_Hal4Ctxt_t)));
    448                 Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    449                 Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb
    450                     = pIoctlCallback;/*Register upper layer callback*/
    451                 Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam = pOutParam;
    452                 /*Upgrade the firmware*/
    453                 RetStatus = phDnldNfc_Upgrade (
    454                         psHwReference,
    455                         phHal4Nfc_DownloadComplete,
    456                         Hal4Ctxt
    457                         );
    458                 if(NFCSTATUS_SUCCESS == RetStatus)
    459                 {
    460                     phOsalNfc_FreeMemory(Hal4Ctxt);
    461                     ((phHal_sHwReference_t *)psHwReference)->hal_context = NULL;
    462                 }
    463             }
    464         }
    465         else
    466 #endif/*NFC_FW_DOWNLOAD*/
    467         {
    468             RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);
    469         }
    470     }
    471     else/*Status is Initialised*/
    472     {
    473         /*Register upper layer context*/
    474         Hal4Ctxt = psHwReference->hal_context;
    475         Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam = pOutParam;
    476         switch(IoctlCode)
    477         {
    478         /*Self test Ioctls*/
    479         case DEVMGMT_ANTENNA_TEST:
    480         case DEVMGMT_SWP_TEST:
    481         case DEVMGMT_NFCWI_TEST:
    482             if(eHal4StateSelfTestMode ==Hal4Ctxt->Hal4CurrentState)
    483             {
    484                 RetStatus = phHciNfc_System_Test(
    485                     Hal4Ctxt->psHciHandle,
    486                     (void *)psHwReference,
    487                     IoctlCode ,
    488                     pInParam
    489                     );
    490             }
    491             break;
    492         /*PRBS Test*/
    493         case DEVMGMT_PRBS_TEST:
    494             RetStatus = phHciNfc_PRBS_Test(
    495                 Hal4Ctxt->psHciHandle,
    496                 (void *)psHwReference,
    497                 IoctlCode ,
    498                 pInParam
    499                 );
    500             break;
    501         /*To Set Antenna Power Level*/
    502         case NFC_ANTENNA_CWG:
    503             if(eHal4StateSelfTestMode ==Hal4Ctxt->Hal4CurrentState)
    504             {
    505                 RetStatus = phHciNfc_System_Configure (
    506                     Hal4Ctxt->psHciHandle,
    507                     (void *)psHwReference,
    508                     NFC_ANTENNA_CWG,
    509                     pInParam->buffer[0] /**Set Power Level*/
    510                     );
    511 
    512             }
    513             break;
    514         /*Not allowed when Init is complete*/
    515         case NFC_FW_DOWNLOAD_CHECK:
    516         case NFC_FW_DOWNLOAD:
    517             RetStatus = PHNFCSTVAL(CID_NFC_HAL,
    518                 NFCSTATUS_BUSY);
    519             break;
    520         /*Gpio read*/
    521         case NFC_GPIO_READ:
    522             /* if(eHal4StateSelfTestMode == Hal4Ctxt->Hal4CurrentState) */
    523             {
    524                 RetStatus = phHciNfc_System_Get_Info(
    525                     Hal4Ctxt->psHciHandle,
    526                     (void *)psHwReference,
    527                     IoctlCode ,
    528                     pOutParam->buffer
    529                     );
    530             }
    531             break;
    532         /*Used to Read Memory/Registers .3 bytes of Array passed form the
    533           address to read from in MSB first format.*/
    534         case NFC_MEM_READ:
    535             {
    536                 if((NULL != pInParam)
    537                     && (pInParam->length == 3))
    538                 {
    539                     for( ind = 0; ind < 3; ind++ )
    540                     {
    541                         config_type = ((config_type << BYTE_SIZE )
    542                                         | (pInParam->buffer[ind] ));
    543                     }
    544                     RetStatus = phHciNfc_System_Get_Info(
    545                         Hal4Ctxt->psHciHandle,
    546                         (void *)psHwReference,
    547                         config_type ,
    548                         pOutParam->buffer
    549                         );
    550                 }
    551                 else
    552                 {
    553                     RetStatus = PHNFCSTVAL(CID_NFC_HAL,
    554                         NFCSTATUS_INVALID_PARAMETER);
    555                 }
    556             }
    557             break;
    558         /*Used to Write Memory/Registers .First 3 bytes of Array passed in MSB
    559           first format form the address to write to.The 4th Byte is the 8 bit
    560           value to be written to the address*/
    561         case NFC_MEM_WRITE:
    562             {
    563                 if((NULL != pInParam)
    564                     && (pInParam->length == 4))
    565                 {
    566                     for( ind = 0; ind < 3; ind++ )
    567                     {
    568                         config_type = ((config_type << BYTE_SIZE )
    569                                         | (pInParam->buffer[ind] ));
    570                     }
    571                     RetStatus = phHciNfc_System_Configure (
    572                         Hal4Ctxt->psHciHandle,
    573                         (void *)psHwReference,
    574                         config_type,
    575                         pInParam->buffer[3] /*config value*/
    576                         );
    577                 }
    578                 else
    579                 {
    580                     RetStatus = PHNFCSTVAL(CID_NFC_HAL ,
    581                         NFCSTATUS_INVALID_PARAMETER);
    582                 }
    583             }
    584             break;
    585         default:
    586             break;
    587         }
    588         if(NFCSTATUS_PENDING == RetStatus)/*Callback Pending*/
    589         {
    590             /*Register upper layer callback and context*/
    591             Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    592             Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb= pIoctlCallback;
    593             /*Store the Ioctl code*/
    594             Hal4Ctxt->Ioctl_Type = IoctlCode;
    595         }
    596     }
    597     return RetStatus;
    598 }
    599 
    600 
    601 /**
    602  *  The close function called by the upper layer when HAL4 is to be closed
    603  *  (shutdown).
    604  */
    605 NFCSTATUS phHal4Nfc_Close(
    606                           phHal_sHwReference_t *psHwReference,
    607                           pphHal4Nfc_GenCallback_t pCloseCallback,
    608                           void *pContext
    609                           )
    610 {
    611     NFCSTATUS closeRetVal = NFCSTATUS_SUCCESS;
    612     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    613     /*NULL checks*/
    614     if(NULL == psHwReference || NULL == pCloseCallback)
    615     {
    616         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    617         closeRetVal = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
    618     }
    619     else if((NULL == psHwReference->hal_context)
    620                         || (((phHal4Nfc_Hal4Ctxt_t *)
    621                                 psHwReference->hal_context)->Hal4CurrentState
    622                                                < eHal4StateSelfTestMode)
    623                         || (((phHal4Nfc_Hal4Ctxt_t *)
    624                                 psHwReference->hal_context)->Hal4NextState
    625                                                == eHal4StateClosed))
    626     {
    627         /*return already closed*/
    628         closeRetVal= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    629     }
    630     else  /*Close the HAL*/
    631     {
    632         /*Get Hal4 context from Hw reference*/
    633         Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)((phHal_sHwReference_t *)
    634                                                psHwReference)->hal_context;
    635         /*Unregister Tag Listener*/
    636         if(NULL != Hal4Ctxt->psADDCtxtInfo)
    637         {
    638             Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
    639         }
    640         /*store Callback and Context*/
    641         Hal4Ctxt->sUpperLayerInfo.pUpperCloseCb = pCloseCallback;
    642         Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    643         /*Call Hci Release*/
    644         PHDBG_INFO("Hal4:Calling Hci Release");
    645         closeRetVal =(NFCSTATUS)phHciNfc_Release(
    646                                     (void *)Hal4Ctxt->psHciHandle,
    647                                     psHwReference,
    648                                     (pphNfcIF_Notification_CB_t)
    649                                     phHal4Nfc_LowerNotificationHandler,
    650                                     (void *)Hal4Ctxt
    651                                     );
    652         /*Update Next state and exit*/
    653         if( PHNFCSTATUS (closeRetVal) == NFCSTATUS_PENDING )
    654         {
    655             Hal4Ctxt->Hal4NextState = eHal4StateClosed;
    656             Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
    657         }
    658         else
    659         {
    660 
    661         }
    662     }
    663     return closeRetVal;
    664 }
    665 
    666 /*Forcibly shutdown the HAl4.Frees all Resources in use by Hal4 before shutting
    667   down*/
    668 void phHal4Nfc_Hal4Reset(
    669                          phHal_sHwReference_t *pHwRef,
    670                          void                 *pContext
    671                          )
    672 {
    673     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    674     NFCSTATUS             closeRetVal = NFCSTATUS_SUCCESS;
    675     uint8_t               RemoteDevNumber = 0;
    676     if(pHwRef ==NULL)
    677     {
    678         closeRetVal = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
    679     }
    680     else if(pHwRef->hal_context != NULL)
    681     {
    682         /*Get the Hal context*/
    683         Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pHwRef->hal_context;
    684         /*store the upper layer context*/
    685         Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    686         Hal4Ctxt->Hal4NextState = eHal4StateClosed;
    687         Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
    688         /*Call Hci Release*/
    689         PHDBG_INFO("Hal4:Calling Hci Release");
    690         closeRetVal =(NFCSTATUS)phHciNfc_Release(
    691                                             (void *)Hal4Ctxt->psHciHandle,
    692                                             pHwRef,
    693                                             (pphNfcIF_Notification_CB_t)NULL,
    694                                             (void *)Hal4Ctxt
    695                                             );/*Clean up Hci*/
    696         Hal4Ctxt->Hal4CurrentState = eHal4StateClosed;
    697         phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
    698         Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
    699         /*Free ADD context*/
    700         if(NULL != Hal4Ctxt->psADDCtxtInfo)
    701         {
    702             Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
    703             while(RemoteDevNumber < MAX_REMOTE_DEVICES)
    704             {
    705                 if(NULL != Hal4Ctxt->rem_dev_list[RemoteDevNumber])
    706                 {
    707                     phOsalNfc_FreeMemory((void *)
    708                             (Hal4Ctxt->rem_dev_list[RemoteDevNumber]));
    709                     Hal4Ctxt->rem_dev_list[RemoteDevNumber] = NULL;
    710                 }
    711                 RemoteDevNumber++;
    712             }
    713             Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    714             phOsalNfc_FreeMemory(Hal4Ctxt->psADDCtxtInfo);
    715         }
    716         /*Free Trcv context*/
    717         if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    718         {
    719             if(NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
    720             {
    721                 phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo
    722                                                     ->sLowerRecvData.buffer);
    723             }
    724             if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
    725                 && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
    726             {
    727                 phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
    728             }
    729             phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
    730         }
    731         phOsalNfc_FreeMemory(Hal4Ctxt);/*Free the context*/
    732         pHwRef->hal_context = NULL;
    733         gpphHal4Nfc_Hwref = NULL;
    734     }
    735     else
    736     {
    737         /*Hal4 Context is already closed.Return Success*/
    738     }
    739     /*Reset Should always return Success*/
    740     if(closeRetVal != NFCSTATUS_SUCCESS)
    741     {
    742         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    743     }
    744     return;
    745 }
    746 
    747 /**
    748  *  \if hal
    749  *   \ingroup grp_hal_common
    750  *  \else
    751  *   \ingroup grp_mw_external_hal_funcs
    752  *  \endif
    753  *
    754  *  Retrieves the capabilities of the device represented by the Hardware
    755  *  Reference parameter.
    756  *  The HW, SW versions, the MTU and other mandatory information are located
    757  *  inside the pDevCapabilities parameter.
    758  */
    759 NFCSTATUS phHal4Nfc_GetDeviceCapabilities(
    760                             phHal_sHwReference_t          *psHwReference,
    761                             phHal_sDeviceCapabilities_t   *psDevCapabilities,
    762                             void                          *pContext
    763                             )
    764 {
    765     NFCSTATUS retstatus = NFCSTATUS_SUCCESS;
    766     /*NULL checks*/
    767     if(psDevCapabilities == NULL || psHwReference == NULL || pContext == NULL)
    768     {
    769         retstatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    770     }
    771     /*Check for Initialized state*/
    772     else if((NULL == psHwReference->hal_context)
    773                         || (((phHal4Nfc_Hal4Ctxt_t *)
    774                                 psHwReference->hal_context)->Hal4CurrentState
    775                                                < eHal4StateOpenAndReady)
    776                         || (((phHal4Nfc_Hal4Ctxt_t *)
    777                                 psHwReference->hal_context)->Hal4NextState
    778                                                == eHal4StateClosed))
    779     {
    780         retstatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);
    781     }
    782     else/*Provide Device capabilities and Version Info to the caller*/
    783     {
    784         (void)memcpy((void *)psDevCapabilities,
    785             (void *)&(psHwReference->device_info),
    786             sizeof(phHal_sDeviceCapabilities_t));
    787         psDevCapabilities->ReaderSupProtocol.Felica         = TRUE;
    788         psDevCapabilities->ReaderSupProtocol.ISO14443_4A    = TRUE;
    789         psDevCapabilities->ReaderSupProtocol.ISO14443_4B    = TRUE;
    790         psDevCapabilities->ReaderSupProtocol.ISO15693       = TRUE;
    791         psDevCapabilities->ReaderSupProtocol.Jewel          = TRUE;
    792         psDevCapabilities->ReaderSupProtocol.MifareStd      = TRUE;
    793         psDevCapabilities->ReaderSupProtocol.MifareUL       = TRUE;
    794         psDevCapabilities->ReaderSupProtocol.NFC            = TRUE;
    795         psDevCapabilities->EmulationSupProtocol.Felica      = FALSE;
    796         psDevCapabilities->EmulationSupProtocol.ISO14443_4A = FALSE;
    797         psDevCapabilities->EmulationSupProtocol.ISO14443_4B = FALSE;
    798         psDevCapabilities->EmulationSupProtocol.ISO15693    = FALSE;
    799         psDevCapabilities->EmulationSupProtocol.Jewel       = FALSE;
    800         psDevCapabilities->EmulationSupProtocol.MifareStd   = FALSE;
    801         psDevCapabilities->EmulationSupProtocol.MifareUL    = FALSE;
    802         psDevCapabilities->EmulationSupProtocol.NFC         = TRUE;
    803         psDevCapabilities->hal_version = (
    804                     (((PH_HAL4NFC_INTERFACE_VERSION << BYTE_SIZE)
    805                       |(PH_HAL4NFC_INTERFACE_REVISION)<<BYTE_SIZE)
    806                       |(PH_HAL4NFC_INTERFACE_PATCH)<<BYTE_SIZE)
    807                       |PH_HAL4NFC_INTERAFECE_BUILD
    808                       );
    809     }
    810     return retstatus;
    811 }
    812 
    813 /*
    814  * Handles all notifications received from HCI layer.
    815  *
    816  */
    817 static void phHal4Nfc_LowerNotificationHandler(
    818                                     void    *pContext,
    819                                     void    *pHwRef,
    820                                     uint8_t  type,
    821                                     void     *pInfo
    822                                     )
    823 {
    824     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    825     if((NULL == pInfo) || (NULL == pHwRef)
    826         || (NULL == pContext))
    827     {
    828         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    829     }
    830     else
    831     {
    832         /*A copy of hardware reference is maintained in HAL for comparing passed
    833           and returned context.Set to NULL after a Shutdown*/
    834         if(NULL != gpphHal4Nfc_Hwref)/*Get context from Hw ref*/
    835         {
    836             Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)gpphHal4Nfc_Hwref->hal_context;
    837             if(NFC_INVALID_RELEASE_TYPE == Hal4Ctxt->sTgtConnectInfo.ReleaseType)
    838             {
    839                 Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
    840                 gpphHal4Nfc_Hwref = (phHal_sHwReference_t *)pHwRef;
    841             }
    842         }
    843         else/*No Copy of Hw ref in HAL.Copy both Hwref and Hal context passed
    844              by Hci*/
    845         {
    846             Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
    847             gpphHal4Nfc_Hwref = (phHal_sHwReference_t *)pHwRef;
    848         }
    849         /*Check the type of notification received from Hci and handle it
    850           accordingly*/
    851         switch(type)
    852         {
    853             case NFC_NOTIFY_INIT_COMPLETED:
    854             case NFC_NOTIFY_INIT_FAILED:
    855                 phHal4Nfc_OpenComplete(Hal4Ctxt,pInfo);
    856                 break;
    857             case NFC_IO_SUCCESS:
    858             case NFC_IO_ERROR:
    859                 phHal4Nfc_IoctlComplete(Hal4Ctxt,pInfo);
    860                 break;
    861             case NFC_NOTIFY_RESULT:
    862                 phHal4Nfc_SelfTestComplete(Hal4Ctxt,pInfo);
    863                 break;
    864             case NFC_NOTIFY_DEINIT_COMPLETED:
    865             case NFC_NOTIFY_DEINIT_FAILED:
    866                 phHal4Nfc_CloseComplete(Hal4Ctxt,pInfo);
    867                 break;
    868             case NFC_NOTIFY_POLL_ENABLED:
    869             case NFC_NOTIFY_POLL_DISABLED:
    870             case NFC_NOTIFY_POLL_RESTARTED:
    871             case NFC_NOTIFY_CONFIG_ERROR:
    872             case NFC_NOTIFY_CONFIG_SUCCESS:
    873                 phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
    874                 break;
    875             case NFC_NOTIFY_TARGET_DISCOVERED:
    876             case NFC_NOTIFY_DISCOVERY_ERROR:
    877                 phHal4Nfc_TargetDiscoveryComplete(Hal4Ctxt,pInfo);
    878                 break;
    879             case NFC_NOTIFY_TARGET_REACTIVATED:
    880                 phHal4Nfc_ReactivationComplete(Hal4Ctxt,pInfo);
    881                 break;
    882             case NFC_NOTIFY_EVENT:
    883                 PHDBG_INFO("Hal4:Calling Event callback");
    884                 phHal4Nfc_HandleEvent(Hal4Ctxt,pInfo);
    885                 break;
    886             case NFC_NOTIFY_TARGET_CONNECTED:
    887                 PHDBG_INFO("Hal4:Calling Hal4 Connect complete");
    888                 phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
    889                 break;
    890 
    891             case NFC_NOTIFY_TARGET_DISCONNECTED:
    892             {
    893                 PHDBG_INFO("Hal4:Target Disconnected");
    894                 if(Hal4Ctxt->Hal4NextState == eHal4StatePresenceCheck)
    895                 {
    896                     phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
    897                 }
    898                 else
    899                 {
    900                     phHal4Nfc_DisconnectComplete(Hal4Ctxt,pInfo);
    901                 }
    902                 break;
    903             }
    904             case NFC_NOTIFY_TRANSCEIVE_COMPLETED:
    905             case NFC_NOTIFY_TRANSCEIVE_ERROR    :
    906                 PHDBG_INFO("Hal4:Transceive Callback");
    907                 if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    908                 {
    909 #ifdef TRANSACTION_TIMER
    910                     if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    911                         != PH_OSALNFC_INVALID_TIMER_ID)
    912                     {
    913                         phOsalNfc_Timer_Stop(
    914                             Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    915                             );
    916                         phOsalNfc_Timer_Delete(
    917                             Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    918                             );
    919                         Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    920                                     = PH_OSALNFC_INVALID_TIMER_ID;
    921                     }
    922 #endif /*TRANSACTION_TIMER*/
    923                     phHal4Nfc_TransceiveComplete(Hal4Ctxt,pInfo);
    924                 }
    925                 break;
    926             case NFC_NOTIFY_SEND_COMPLETED   :
    927                 PHDBG_INFO("Hal4:NfcIp1 Send Callback");
    928                 if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    929                 {
    930                     phHal4Nfc_SendCompleteHandler(Hal4Ctxt,pInfo);
    931                 }
    932                 break;
    933             case NFC_NOTIFY_TRANSACTION  :
    934                 phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
    935                 break;
    936             case NFC_NOTIFY_RECV_ERROR    :
    937             case NFC_NOTIFY_RECV_EVENT    :
    938                 PHDBG_INFO("Hal4:Receive Event");
    939                 if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    940                 {
    941                     if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    942                         != PH_OSALNFC_INVALID_TIMER_ID)
    943                     {
    944                         phOsalNfc_Timer_Stop(
    945                             Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    946                             );
    947                         phOsalNfc_Timer_Delete(
    948                             Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    949                             );
    950                         Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
    951                             = PH_OSALNFC_INVALID_TIMER_ID;
    952                     }
    953                 }
    954                 phHal4Nfc_RecvCompleteHandler(Hal4Ctxt,pInfo);
    955                 break;
    956             case NFC_NOTIFY_TARGET_PRESENT:
    957                 phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
    958                 break;
    959             case NFC_NOTIFY_DEVICE_ERROR:
    960             {
    961                 static phHal4Nfc_NotificationInfo_t uNotificationInfo;
    962                 Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
    963                 if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
    964                 {
    965                     Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
    966                         Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
    967                         NFC_EVENT_NOTIFICATION,
    968                         uNotificationInfo,
    969                         NFCSTATUS_BOARD_COMMUNICATION_ERROR
    970                         );
    971                 }
    972                 else
    973                 {
    974                     phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
    975                 }
    976                 break;
    977             }
    978             case NFC_NOTIFY_CONNECT_FAILED:
    979             case NFC_NOTIFY_DISCONNECT_FAILED:
    980             /*Generic Error type received from Hci.Handle the error based on
    981               Hal4 next state and which past callback was Pending*/
    982             case NFC_NOTIFY_ERROR:
    983             {
    984                 PHDBG_WARNING("Hal4:Error Notification from HCI");
    985                 switch(Hal4Ctxt->Hal4NextState)
    986                 {
    987                     case eHal4StateClosed:
    988                         phHal4Nfc_CloseComplete(Hal4Ctxt,pInfo);
    989                         break;
    990                     case eHal4StateSelfTestMode:
    991                         phHal4Nfc_SelfTestComplete(Hal4Ctxt,pInfo);
    992                         break;
    993                     case eHal4StateConfiguring:
    994                         phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
    995                         break;
    996                     case eHal4StateTargetDiscovered:
    997                     case eHal4StateTargetActivate:
    998                     {
    999                         if(NULL != Hal4Ctxt->sTgtConnectInfo.pUpperConnectCb)
   1000                         {
   1001                             if(NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
   1002                             {
   1003                                  phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
   1004                             }
   1005                             else
   1006                             {
   1007                                 phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
   1008                             }
   1009                         }
   1010                         else
   1011                         {
   1012                             phHal4Nfc_TargetDiscoveryComplete(Hal4Ctxt,pInfo);
   1013                         }
   1014                         break;
   1015                     }
   1016                     case eHal4StateTargetConnected:
   1017                         phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
   1018                         break;
   1019                     case eHal4StateOpenAndReady:
   1020                         phHal4Nfc_DisconnectComplete(Hal4Ctxt,pInfo);
   1021                         break;
   1022                     case eHal4StatePresenceCheck:
   1023                         phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
   1024                         break;
   1025                     default:
   1026                         PHDBG_WARNING("Unknown Error notification");
   1027                         break;
   1028                 }
   1029                 break;
   1030             }/*End of switch(Hal4Ctxt->Hal4State)*/
   1031             default:
   1032                 phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
   1033                 break;
   1034         }/*End of switch(type)*/
   1035     }
   1036     return;
   1037 }
   1038 
   1039 
   1040 /*Event handler for HAL-HCI interface*/
   1041 static void phHal4Nfc_HandleEvent(
   1042                        phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
   1043                        void *pInfo
   1044                        )
   1045 {
   1046     phHal_sEventInfo_t *psEventInfo = (phHal_sEventInfo_t *)pInfo;
   1047     static phNfc_sNotificationInfo_t sNotificationInfo;
   1048     phHal4Nfc_NotificationInfo_t uNotificationInfo = {NULL};
   1049     NFCSTATUS RetStatus = NFCSTATUS_FAILED;
   1050     /*Check if Hal4 Close has already been called*/
   1051     if(eHal4StateClosed != Hal4Ctxt->Hal4NextState)
   1052     {
   1053         switch(psEventInfo->eventType)
   1054         {
   1055         case NFC_EVT_ACTIVATED:/*Target Activated*/
   1056         {
   1057             if(psEventInfo->eventHost == phHal_eHostController)
   1058             {
   1059                 switch(psEventInfo->eventSource)
   1060                 {
   1061                     case phHal_eNfcIP1_Target:
   1062                         phHal4Nfc_P2PActivateComplete(Hal4Ctxt,pInfo);
   1063                         break;
   1064                     case phHal_eISO14443_A_PICC:
   1065                     case phHal_eISO14443_B_PICC:
   1066                         sNotificationInfo.info = psEventInfo;
   1067                         sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1068                         sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1069                         pInfo = &sNotificationInfo;
   1070                         phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1071                         break;
   1072                     default:
   1073                         break;
   1074                 }
   1075             }
   1076         }
   1077             break;
   1078         case NFC_EVT_DEACTIVATED:/*Target Deactivated*/
   1079         {
   1080             if(psEventInfo->eventHost == phHal_eHostController)
   1081             {
   1082                 switch(psEventInfo->eventSource)
   1083                 {
   1084                 case phHal_eNfcIP1_Target:
   1085                     phHal4Nfc_HandleP2PDeActivate(Hal4Ctxt,pInfo);
   1086                     break;
   1087                 case phHal_eISO14443_A_PICC:
   1088                 case phHal_eISO14443_B_PICC:
   1089                     sNotificationInfo.info = psEventInfo;
   1090                     sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1091                     sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1092                     pInfo = &sNotificationInfo;
   1093                     phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1094                     break;
   1095                 default:
   1096                     break;
   1097                 }
   1098             }
   1099         }
   1100             break;
   1101         /*Set Protection Event*/
   1102         case NFC_EVT_PROTECTED:
   1103         {
   1104 #ifdef IGNORE_EVT_PROTECTED
   1105             /*Ignore_Event_Protected is set to false during Field Off event and
   1106               Set protection Configuration.After a NFC_EVT_PROTECTED is received
   1107               once all subsequent NFC_EVT_PROTECTED events are ignored*/
   1108             if(FALSE == Hal4Ctxt->Ignore_Event_Protected)
   1109             {
   1110                 Hal4Ctxt->Ignore_Event_Protected = TRUE;
   1111 #endif/*#ifdef IGNORE_EVT_PROTECTED*/
   1112                 sNotificationInfo.info = psEventInfo;
   1113                 sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1114                 sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1115                 pInfo = &sNotificationInfo;
   1116                 phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1117 #ifdef IGNORE_EVT_PROTECTED
   1118             }
   1119 #endif/*#ifdef IGNORE_EVT_PROTECTED*/
   1120             break;
   1121         }
   1122         /*NFC_UICC_RDPHASES_DEACTIVATE_REQ*/
   1123         case NFC_UICC_RDPHASES_DEACTIVATE_REQ:
   1124         {
   1125             if(NULL != gpphHal4Nfc_Hwref)
   1126             {
   1127                 gpphHal4Nfc_Hwref->uicc_rdr_active = FALSE;
   1128             }
   1129             break;
   1130         }
   1131         case NFC_UICC_RDPHASES_ACTIVATE_REQ:
   1132         {
   1133             if(NULL != gpphHal4Nfc_Hwref)
   1134             {
   1135                 gpphHal4Nfc_Hwref->uicc_rdr_active = TRUE;
   1136             }
   1137             /*If a NFC_UICC_RDPHASES_ACTIVATE_REQ is received before a configure
   1138              discovery,then create a ADD context info*/
   1139             if (NULL == Hal4Ctxt->psADDCtxtInfo)
   1140             {
   1141                 Hal4Ctxt->psADDCtxtInfo= (pphHal4Nfc_ADDCtxtInfo_t)
   1142                     phOsalNfc_GetMemory((uint32_t)
   1143                     (sizeof(phHal4Nfc_ADDCtxtInfo_t)));
   1144                 if(NULL != Hal4Ctxt->psADDCtxtInfo)
   1145                 {
   1146                     (void)memset(Hal4Ctxt->psADDCtxtInfo,0,
   1147                         sizeof(phHal4Nfc_ADDCtxtInfo_t)
   1148                         );
   1149                 }
   1150             }
   1151             if(NULL != Hal4Ctxt->psADDCtxtInfo)
   1152             {
   1153                 Hal4Ctxt->psADDCtxtInfo->sADDCfg.PollDevInfo.PollEnabled
   1154                                 |= psEventInfo->eventInfo.rd_phases;
   1155                 /*Configure HCI Discovery*/
   1156                 RetStatus = phHciNfc_Config_Discovery(
   1157                     (void *)Hal4Ctxt->psHciHandle,
   1158                     gpphHal4Nfc_Hwref,
   1159                     &(Hal4Ctxt->psADDCtxtInfo->sADDCfg)
   1160                     );
   1161                 Hal4Ctxt->Hal4NextState = (NFCSTATUS_PENDING == RetStatus?
   1162                                                 eHal4StateConfiguring:
   1163                                                 Hal4Ctxt->Hal4NextState);
   1164             }
   1165             break;
   1166         }
   1167         /*Call Default Event handler for these Events*/
   1168         case NFC_INFO_TXLDO_OVERCUR:
   1169         case NFC_INFO_MEM_VIOLATION:
   1170         case NFC_INFO_TEMP_OVERHEAT:
   1171         {
   1172             sNotificationInfo.info = psEventInfo;
   1173             sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1174             sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1175             pInfo = &sNotificationInfo;
   1176             PHDBG_INFO("Hal4:Exception events");
   1177             if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
   1178             {
   1179                 /*Pass on Event notification info from Hci to Upper layer*/
   1180                 uNotificationInfo.psEventInfo = psEventInfo;
   1181                 Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
   1182                     Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
   1183                     sNotificationInfo.type,
   1184                     uNotificationInfo,
   1185                     NFCSTATUS_SUCCESS
   1186                     );
   1187             }
   1188             break;
   1189         }
   1190         /*Call emulation Event handler fto handle these Events*/
   1191         case NFC_EVT_TRANSACTION:
   1192         case NFC_EVT_START_OF_TRANSACTION:
   1193         case NFC_EVT_END_OF_TRANSACTION:
   1194         case NFC_EVT_CONNECTIVITY:
   1195         case NFC_EVT_OPERATION_ENDED:
   1196             sNotificationInfo.info = psEventInfo;
   1197             sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1198             sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1199             pInfo = &sNotificationInfo;
   1200             PHDBG_INFO("Hal4:Event transaction\n");
   1201             phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1202             break;
   1203         case NFC_EVT_FIELD_ON:
   1204             Hal4Ctxt->psEventInfo = sNotificationInfo.info = psEventInfo;
   1205             sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1206             sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1207             pInfo = &sNotificationInfo;
   1208             PHDBG_INFO("Hal4:Event Field ON\n");
   1209             phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1210             break;
   1211         case NFC_EVT_FIELD_OFF:
   1212     #ifdef IGNORE_EVT_PROTECTED
   1213             Hal4Ctxt->Ignore_Event_Protected = FALSE;
   1214     #endif/*#ifdef IGNORE_EVT_PROTECTED*/
   1215             Hal4Ctxt->psEventInfo = sNotificationInfo.info = psEventInfo;
   1216             sNotificationInfo.status = NFCSTATUS_SUCCESS;
   1217             sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
   1218             pInfo = &sNotificationInfo;
   1219             PHDBG_INFO("Hal4:Event Field OFF\n");
   1220             phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
   1221             break;
   1222         default:
   1223             PHDBG_WARNING("Hal4:Unhandled Event type received");
   1224             break;
   1225         }/*End of switch*/
   1226     }/*if(eHal4StateClosed != Hal4Ctxt->Hal4NextState)*/
   1227     return;
   1228 }
   1229 
   1230 
   1231 /*Callback handler for Self Test Ioctl completion*/
   1232 static void phHal4Nfc_SelfTestComplete(
   1233                                        phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
   1234                                        void *pInfo
   1235                                        )
   1236 {
   1237     NFCSTATUS status = NFCSTATUS_FAILED;
   1238     phNfc_sData_t *SelfTestResults
   1239         = (phNfc_sData_t *)(((phNfc_sCompletionInfo_t *)pInfo)->info);
   1240     pphHal4Nfc_IoctlCallback_t pUpper_IoctlCb
   1241         = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
   1242     void  *pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
   1243     /*check for Success*/
   1244     if((SelfTestResults->length > 0) && (0 == SelfTestResults->buffer[0]))
   1245     {
   1246         status = NFCSTATUS_SUCCESS;
   1247     }
   1248     /*Copy response buffer and length*/
   1249     (void)memcpy(Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->buffer,
   1250                  SelfTestResults->buffer,
   1251                  SelfTestResults->length);
   1252     Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->length
   1253                                         = SelfTestResults->length;
   1254     /*Call registered Ioctl callback*/
   1255     (*pUpper_IoctlCb)(
   1256                 pUpper_Context,
   1257                 Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam,
   1258                 status
   1259                 );
   1260     return;
   1261 }
   1262 
   1263 
   1264 static void phHal4Nfc_IoctlComplete(
   1265                                     phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
   1266                                     void *pInfo
   1267                                     )
   1268 {
   1269     /*Copy status*/
   1270     NFCSTATUS status = (((phNfc_sCompletionInfo_t *)pInfo)->status);
   1271     pphHal4Nfc_IoctlCallback_t pUpper_IoctlCb
   1272                                     = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
   1273 #ifdef MERGE_SAK_SW2
   1274     pphHal4Nfc_GenCallback_t pConfigCallback =
   1275         Hal4Ctxt->sUpperLayerInfo.pConfigCallback;
   1276 #endif/*#ifdef MERGE_SAK_SW2*/
   1277     void  *pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
   1278     Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb = NULL;
   1279 #ifdef MERGE_SAK_SW1 /*Software workaround 1*/
   1280     if(eHal4StateOpenAndReady == Hal4Ctxt->Hal4NextState)
   1281     {
   1282         Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
   1283         Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
   1284         /*Upper layer's Open Cb*/
   1285         (*Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb)(
   1286             Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
   1287             NFCSTATUS_SUCCESS
   1288             );
   1289     }
   1290 #endif/*#ifdef MERGE_SAK_SW1*/
   1291 #ifdef MERGE_SAK_SW2 /*Software workaround 2*/
   1292     else if((eHal4StateConfiguring == Hal4Ctxt->Hal4NextState)
   1293             &&(NULL != pConfigCallback))
   1294     {
   1295         Hal4Ctxt->sUpperLayerInfo.pConfigCallback = NULL;
   1296         Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
   1297         (*pConfigCallback)(
   1298             Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,status
   1299             );
   1300     }
   1301     else
   1302 #endif/*#ifdef MERGE_SAK_SW2*/
   1303     {
   1304         /*for NFC_MEM_READ and NFC_GPIO_READ ,provide one Byte Response*/
   1305         if ((NFC_MEM_READ == Hal4Ctxt->Ioctl_Type)
   1306             || (NFC_GPIO_READ == Hal4Ctxt->Ioctl_Type))
   1307         {
   1308             Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->length
   1309                 = sizeof (uint8_t);
   1310         }
   1311          /*Call registered Ioctl callback*/
   1312         if(NULL != pUpper_IoctlCb)
   1313         {
   1314             (*pUpper_IoctlCb)(
   1315                 pUpper_Context,
   1316                 Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam,
   1317                 status
   1318                 );
   1319         }
   1320     }
   1321     return;
   1322 }
   1323 
   1324 #ifdef FW_DOWNLOAD
   1325 /**Callback handler for Download completion*/
   1326 STATIC void phHal4Nfc_DownloadComplete(
   1327                                 void *pContext,
   1328                                 void *pHwRef,
   1329                                 uint8_t type,
   1330                                 void *pInfo
   1331                                 )
   1332 {
   1333     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
   1334     NFCSTATUS status = NFCSTATUS_FAILED;
   1335     pphHal4Nfc_IoctlCallback_t pUpper_DnldCb = NULL;
   1336     phNfc_sData_t *pIoctlOutParam = NULL;
   1337     phHal_sHwReference_t *psHwRef = NULL;
   1338     void  *pUpper_Context = NULL;
   1339     /*NULL checks*/
   1340     if((NULL == pInfo) || (NULL == pHwRef) || (NULL == pContext))
   1341     {
   1342         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
   1343     }
   1344     else
   1345     {
   1346         type = type;
   1347         Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
   1348         /*Copy back stored context/callback for the upper layer*/
   1349         pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
   1350         pIoctlOutParam = Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam;
   1351         pUpper_DnldCb = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
   1352         Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb = NULL;
   1353         /*Copy download status*/
   1354         status = (((phNfc_sCompletionInfo_t *)pInfo)->status);
   1355         /*copy hw reference*/
   1356         psHwRef = (phHal_sHwReference_t *)pHwRef;
   1357         /*Free the temporary hal context used only for the sake of download*/
   1358         phOsalNfc_FreeMemory(psHwRef->hal_context);
   1359         psHwRef->hal_context = NULL;
   1360         /*Call upper layer callback*/
   1361         if(NULL != pUpper_DnldCb)
   1362         {
   1363             (*pUpper_DnldCb)(
   1364                 pUpper_Context,
   1365                 pIoctlOutParam,
   1366                 status
   1367                 );
   1368         }
   1369     }
   1370     return;
   1371 }
   1372 #endif /*FW_DOWNLOAD*/
   1373 
   1374