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