Home | History | Annotate | Download | only in hal
      1 /*
      2  * Copyright (C) 2012-2014 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 #include <sys/stat.h>
     18 #include <phNxpNciHal.h>
     19 #include <phNxpNciHal_ext.h>
     20 #include <phNxpNciHal_Dnld.h>
     21 #include <phNxpNciHal_Adaptation.h>
     22 #include <phTmlNfc.h>
     23 #include <phDnldNfc.h>
     24 #include <phDal4Nfc_messageQueueLib.h>
     25 #include <phNxpLog.h>
     26 #include <phNxpConfig.h>
     27 #include <phNxpNciHal_NfcDepSWPrio.h>
     28 #include <phNxpNciHal_Kovio.h>
     29 /*********************** Global Variables *************************************/
     30 #define PN547C2_CLOCK_SETTING
     31 #undef  PN547C2_FACTORY_RESET_DEBUG
     32 #define CORE_RES_STATUS_BYTE 3
     33 
     34 /* Processing of ISO 15693 EOF */
     35 extern uint8_t icode_send_eof;
     36 extern uint8_t icode_detected;
     37 static uint8_t cmd_icode_eof[] = { 0x00, 0x00, 0x00 };
     38 
     39 /* FW download success flag */
     40 static uint8_t fw_download_success = 0;
     41 
     42 static uint8_t config_access = FALSE;
     43 /* NCI HAL Control structure */
     44 phNxpNciHal_Control_t nxpncihal_ctrl;
     45 
     46 /* NXP Poll Profile structure */
     47 phNxpNciProfile_Control_t nxpprofile_ctrl;
     48 
     49 /* TML Context */
     50 extern phTmlNfc_Context_t *gpphTmlNfc_Context;
     51 extern void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t result);
     52 /* global variable to get FW version from NCI response*/
     53 uint32_t wFwVerRsp;
     54 /* External global variable to get FW version */
     55 extern uint16_t wFwVer;
     56 
     57 extern uint16_t fw_maj_ver;
     58 extern uint16_t rom_version;
     59 extern int send_to_upper_kovio;
     60 extern int kovio_detected;
     61 extern int disable_kovio;
     62 #if(NFC_NXP_CHIP_TYPE != PN547C2)
     63 extern uint8_t gRecFWDwnld;
     64 static uint8_t gRecFwRetryCount; //variable to hold dummy FW recovery count
     65 #endif
     66 static uint8_t Rx_data[NCI_MAX_DATA_LEN];
     67 
     68 #if(NFC_NXP_CHIP_TYPE == PN548C2)
     69 uint8_t discovery_cmd[50] = { 0 };
     70 uint8_t discovery_cmd_len = 0;
     71 #endif
     72 extern bool_t rf_deactive_cmd;
     73 uint32_t timeoutTimerId = 0;
     74 phNxpNciHal_Sem_t config_data;
     75 
     76 phNxpNciClock_t phNxpNciClock={0, {0}, FALSE};
     77 
     78 phNxpNciRfSetting_t phNxpNciRfSet={FALSE, {0} };
     79 
     80 phNxpNciMwEepromArea_t phNxpNciMwEepromArea = {FALSE, {0} };
     81 
     82 /**************** local methods used in this file only ************************/
     83 static NFCSTATUS phNxpNciHal_fw_download(void);
     84 static void phNxpNciHal_open_complete(NFCSTATUS status);
     85 static void phNxpNciHal_write_complete(void *pContext, phTmlNfc_TransactInfo_t *pInfo);
     86 static void phNxpNciHal_read_complete(void *pContext, phTmlNfc_TransactInfo_t *pInfo);
     87 static void phNxpNciHal_close_complete(NFCSTATUS status);
     88 static void phNxpNciHal_core_initialized_complete(NFCSTATUS status);
     89 static void phNxpNciHal_pre_discover_complete(NFCSTATUS status);
     90 static void phNxpNciHal_power_cycle_complete(NFCSTATUS status);
     91 static void phNxpNciHal_kill_client_thread(phNxpNciHal_Control_t *p_nxpncihal_ctrl);
     92 static void *phNxpNciHal_client_thread(void *arg);
     93 static void phNxpNciHal_get_clk_freq(void);
     94 static void phNxpNciHal_set_clock(void);
     95 static void phNxpNciHal_check_factory_reset(void);
     96 static void phNxpNciHal_print_res_status( uint8_t *p_rx_data, uint16_t *p_len);
     97 static NFCSTATUS phNxpNciHal_CheckValidFwVersion(void);
     98 static void phNxpNciHal_enable_i2c_fragmentation();
     99 static NFCSTATUS phNxpNciHal_get_mw_eeprom (void);
    100 static NFCSTATUS phNxpNciHal_set_mw_eeprom (void);
    101 static int phNxpNciHal_fw_mw_ver_check ();
    102 NFCSTATUS phNxpNciHal_check_clock_config(void);
    103 NFCSTATUS phNxpNciHal_china_tianjin_rf_setting(void);
    104 #if(NFC_NXP_CHIP_TYPE != PN547C2)
    105 static NFCSTATUS phNxpNciHalRFConfigCmdRecSequence ();
    106 static NFCSTATUS phNxpNciHal_CheckRFCmdRespStatus ();
    107 #endif
    108 int  check_config_parameter();
    109 
    110 /******************************************************************************
    111  * Function         phNxpNciHal_client_thread
    112  *
    113  * Description      This function is a thread handler which handles all TML and
    114  *                  NCI messages.
    115  *
    116  * Returns          void
    117  *
    118  ******************************************************************************/
    119 static void *phNxpNciHal_client_thread(void *arg)
    120 {
    121     phNxpNciHal_Control_t *p_nxpncihal_ctrl = (phNxpNciHal_Control_t *) arg;
    122     phLibNfc_Message_t msg;
    123 
    124     NXPLOG_NCIHAL_D("thread started");
    125 
    126     p_nxpncihal_ctrl->thread_running = 1;
    127 
    128     while (p_nxpncihal_ctrl->thread_running == 1)
    129     {
    130         /* Fetch next message from the NFC stack message queue */
    131         if (phDal4Nfc_msgrcv(p_nxpncihal_ctrl->gDrvCfg.nClientId,
    132                 &msg, 0, 0) == -1)
    133         {
    134             NXPLOG_NCIHAL_E("NFC client received bad message");
    135             continue;
    136         }
    137 
    138         if(p_nxpncihal_ctrl->thread_running == 0){
    139             break;
    140         }
    141 
    142         switch (msg.eMsgType)
    143         {
    144             case PH_LIBNFC_DEFERREDCALL_MSG:
    145             {
    146                 phLibNfc_DeferredCall_t *deferCall =
    147                         (phLibNfc_DeferredCall_t *) (msg.pMsgData);
    148 
    149                 REENTRANCE_LOCK();
    150                 deferCall->pCallback(deferCall->pParameter);
    151                 REENTRANCE_UNLOCK();
    152 
    153             break;
    154         }
    155 
    156         case NCI_HAL_OPEN_CPLT_MSG:
    157         {
    158             REENTRANCE_LOCK();
    159             if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
    160             {
    161                 /* Send the event */
    162                 (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_OPEN_CPLT_EVT,
    163                         HAL_NFC_STATUS_OK);
    164             }
    165             REENTRANCE_UNLOCK();
    166             break;
    167         }
    168 
    169         case NCI_HAL_CLOSE_CPLT_MSG:
    170         {
    171             REENTRANCE_LOCK();
    172             if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
    173             {
    174                 /* Send the event */
    175                 (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_CLOSE_CPLT_EVT,
    176                         HAL_NFC_STATUS_OK);
    177                 phNxpNciHal_kill_client_thread(&nxpncihal_ctrl);
    178             }
    179             REENTRANCE_UNLOCK();
    180             break;
    181         }
    182 
    183         case NCI_HAL_POST_INIT_CPLT_MSG:
    184         {
    185             REENTRANCE_LOCK();
    186             if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
    187             {
    188                 /* Send the event */
    189                 (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_POST_INIT_CPLT_EVT,
    190                         HAL_NFC_STATUS_OK);
    191             }
    192             REENTRANCE_UNLOCK();
    193             break;
    194         }
    195 
    196         case NCI_HAL_PRE_DISCOVER_CPLT_MSG:
    197         {
    198             REENTRANCE_LOCK();
    199             if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
    200             {
    201                 /* Send the event */
    202                 (*nxpncihal_ctrl.p_nfc_stack_cback)(
    203                         HAL_NFC_PRE_DISCOVER_CPLT_EVT, HAL_NFC_STATUS_OK);
    204             }
    205             REENTRANCE_UNLOCK();
    206             break;
    207         }
    208 
    209         case NCI_HAL_ERROR_MSG:
    210         {
    211             REENTRANCE_LOCK();
    212             if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
    213             {
    214                 /* Send the event */
    215                 (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_ERROR_EVT,
    216                         HAL_NFC_STATUS_FAILED);
    217             }
    218             REENTRANCE_UNLOCK();
    219             break;
    220         }
    221 
    222         case NCI_HAL_RX_MSG:
    223         {
    224             REENTRANCE_LOCK();
    225             if (nxpncihal_ctrl.p_nfc_stack_data_cback != NULL)
    226             {
    227                 (*nxpncihal_ctrl.p_nfc_stack_data_cback)(
    228                         nxpncihal_ctrl.rsp_len, nxpncihal_ctrl.p_rsp_data);
    229             }
    230             REENTRANCE_UNLOCK();
    231             break;
    232         }
    233         }
    234     }
    235 
    236     NXPLOG_NCIHAL_D("NxpNciHal thread stopped");
    237 
    238     return NULL;
    239 }
    240 
    241 /******************************************************************************
    242  * Function         phNxpNciHal_kill_client_thread
    243  *
    244  * Description      This function safely kill the client thread and clean all
    245  *                  resources.
    246  *
    247  * Returns          void.
    248  *
    249  ******************************************************************************/
    250 static void phNxpNciHal_kill_client_thread(phNxpNciHal_Control_t *p_nxpncihal_ctrl)
    251 {
    252     NXPLOG_NCIHAL_D("Terminating phNxpNciHal client thread...");
    253 
    254     p_nxpncihal_ctrl->p_nfc_stack_cback = NULL;
    255     p_nxpncihal_ctrl->p_nfc_stack_data_cback = NULL;
    256     p_nxpncihal_ctrl->thread_running = 0;
    257 
    258     return;
    259 }
    260 
    261 /******************************************************************************
    262  * Function         phNxpNciHal_fw_download
    263  *
    264  * Description      This function download the PN54X secure firmware to IC. If
    265  *                  firmware version in Android filesystem and firmware in the
    266  *                  IC is same then firmware download will return with success
    267  *                  without downloading the firmware.
    268  *
    269  * Returns          NFCSTATUS_SUCCESS if firmware download successful
    270  *                  NFCSTATUS_FAILED in case of failure
    271  *
    272  ******************************************************************************/
    273 static NFCSTATUS phNxpNciHal_fw_download(void)
    274 {
    275     NFCSTATUS status = NFCSTATUS_FAILED;
    276 
    277     phNxpNciHal_get_clk_freq();
    278     status = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
    279     if (NFCSTATUS_SUCCESS == status)
    280     {
    281         /* Set the obtained device handle to download module */
    282         phDnldNfc_SetHwDevHandle();
    283         NXPLOG_NCIHAL_D("Calling Seq handler for FW Download \n");
    284         status = phNxpNciHal_fw_download_seq(nxpprofile_ctrl.bClkSrcVal, nxpprofile_ctrl.bClkFreqVal);
    285         phDnldNfc_ReSetHwDevHandle();
    286     }
    287     else
    288     {
    289         status = NFCSTATUS_FAILED;
    290     }
    291 
    292     return status;
    293 }
    294 
    295 /******************************************************************************
    296  * Function         phNxpNciHal_CheckValidFwVersion
    297  *
    298  * Description      This function checks the valid FW for Mobile device.
    299  *                  If the FW doesn't belong the Mobile device it further
    300  *                  checks nxp config file to override.
    301  *
    302  * Returns          NFCSTATUS_SUCCESS if valid fw version found
    303  *                  NFCSTATUS_NOT_ALLOWED in case of FW not valid for mobile
    304  *                  device
    305  *
    306  ******************************************************************************/
    307 static NFCSTATUS phNxpNciHal_CheckValidFwVersion(void)
    308 {
    309     NFCSTATUS status = NFCSTATUS_NOT_ALLOWED;
    310 #if(NFC_NXP_CHIP_TYPE == PN551)
    311     const unsigned char sfw_mobile_major_no = 0x05;
    312 #else
    313     const unsigned char sfw_mobile_major_no = 0x01;
    314 #endif
    315     const unsigned char sfw_infra_major_no = 0x02;
    316     unsigned char ufw_current_major_no = 0x00;
    317     unsigned long num = 0;
    318     int isfound = 0;
    319 
    320     /* extract the firmware's major no */
    321     ufw_current_major_no = ((0x00FF) & (wFwVer >> 8U));
    322 
    323     NXPLOG_NCIHAL_D("%s current_major_no = 0x%x", __FUNCTION__,ufw_current_major_no );
    324     if ( ufw_current_major_no == sfw_mobile_major_no)
    325     {
    326         status = NFCSTATUS_SUCCESS;
    327     }
    328     else if (ufw_current_major_no == sfw_infra_major_no)
    329     {
    330         /* Check the nxp config file if still want to go for download */
    331         /* By default NAME_NXP_FW_PROTECION_OVERRIDE will not be defined in config file.
    332            If user really want to override the Infra firmware over mobile firmware, please
    333            put "NXP_FW_PROTECION_OVERRIDE=0x01" in libnfc-nxp.conf file.
    334            Please note once Infra firmware downloaded to Mobile device, The device
    335            can never be updated to Mobile firmware*/
    336         isfound = GetNxpNumValue(NAME_NXP_FW_PROTECION_OVERRIDE, &num, sizeof(num));
    337         if (isfound > 0)
    338         {
    339             if (num == 0x01)
    340             {
    341                 NXPLOG_NCIHAL_D("Override Infra FW over Mobile");
    342                 status = NFCSTATUS_SUCCESS;
    343             }
    344             else
    345             {
    346                 NXPLOG_NCIHAL_D("Firmware download not allowed (NXP_FW_PROTECION_OVERRIDE invalid value)");
    347             }
    348         }
    349         else
    350         {
    351             NXPLOG_NCIHAL_D("Firmware download not allowed (NXP_FW_PROTECION_OVERRIDE not defiend)");
    352         }
    353     }
    354 #if(NFC_NXP_CHIP_TYPE != PN547C2)
    355     else if (gRecFWDwnld == TRUE)
    356     {
    357         status = NFCSTATUS_SUCCESS;
    358     }
    359 #endif
    360     else if (wFwVerRsp == 0)
    361     {
    362         NXPLOG_NCIHAL_E("FW Version not received by NCI command >>> Force Firmware download");
    363         status = NFCSTATUS_SUCCESS;
    364     }
    365     else
    366     {
    367         NXPLOG_NCIHAL_E("Wrong FW Version >>> Firmware download not allowed");
    368     }
    369 
    370     return status;
    371 }
    372 
    373 static void phNxpNciHal_get_clk_freq(void)
    374 {
    375     unsigned long num = 0;
    376     int isfound = 0;
    377 
    378     nxpprofile_ctrl.bClkSrcVal = 0;
    379     nxpprofile_ctrl.bClkFreqVal = 0;
    380     nxpprofile_ctrl.bTimeout = 0;
    381 
    382     isfound = GetNxpNumValue(NAME_NXP_SYS_CLK_SRC_SEL, &num, sizeof(num));
    383     if (isfound > 0)
    384     {
    385         nxpprofile_ctrl.bClkSrcVal = num;
    386     }
    387 
    388     num = 0;
    389     isfound = 0;
    390     isfound = GetNxpNumValue(NAME_NXP_SYS_CLK_FREQ_SEL, &num, sizeof(num));
    391     if (isfound > 0)
    392     {
    393         nxpprofile_ctrl.bClkFreqVal = num;
    394     }
    395 
    396     num = 0;
    397     isfound = 0;
    398     isfound = GetNxpNumValue(NAME_NXP_SYS_CLOCK_TO_CFG, &num, sizeof(num));
    399     if (isfound > 0)
    400     {
    401         nxpprofile_ctrl.bTimeout = num;
    402     }
    403 
    404     NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkSrcVal = 0x%x", nxpprofile_ctrl.bClkSrcVal);
    405     NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkFreqVal = 0x%x", nxpprofile_ctrl.bClkFreqVal);
    406     NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkFreqVal = 0x%x", nxpprofile_ctrl.bTimeout);
    407 
    408     if ((nxpprofile_ctrl.bClkSrcVal < CLK_SRC_XTAL) ||
    409             (nxpprofile_ctrl.bClkSrcVal > CLK_SRC_PLL))
    410     {
    411         NXPLOG_FWDNLD_E("Clock source value is wrong in config file, setting it as default");
    412         nxpprofile_ctrl.bClkSrcVal = NXP_SYS_CLK_SRC_SEL;
    413     }
    414     if ((nxpprofile_ctrl.bClkFreqVal < CLK_FREQ_13MHZ) ||
    415             (nxpprofile_ctrl.bClkFreqVal > CLK_FREQ_52MHZ))
    416     {
    417         NXPLOG_FWDNLD_E("Clock frequency value is wrong in config file, setting it as default");
    418         nxpprofile_ctrl.bClkFreqVal = NXP_SYS_CLK_FREQ_SEL;
    419     }
    420     if ((nxpprofile_ctrl.bTimeout < CLK_TO_CFG_DEF) || (nxpprofile_ctrl.bTimeout > CLK_TO_CFG_MAX))
    421     {
    422         NXPLOG_FWDNLD_E("Clock timeout value is wrong in config file, setting it as default");
    423         nxpprofile_ctrl.bTimeout = CLK_TO_CFG_DEF;
    424     }
    425 
    426 }
    427 
    428 /******************************************************************************
    429  * Function         phNxpNciHal_open
    430  *
    431  * Description      This function is called by libnfc-nci during the
    432  *                  initialization of the NFCC. It opens the physical connection
    433  *                  with NFCC (PN54X) and creates required client thread for
    434  *                  operation.
    435  *                  After open is complete, status is informed to libnfc-nci
    436  *                  through callback function.
    437  *
    438  * Returns          This function return NFCSTATUS_SUCCES (0) in case of success
    439  *                  In case of failure returns other failure value.
    440  *
    441  ******************************************************************************/
    442 int phNxpNciHal_open(nfc_stack_callback_t *p_cback, nfc_stack_data_callback_t *p_data_cback)
    443 {
    444     phOsalNfc_Config_t tOsalConfig;
    445     phTmlNfc_Config_t tTmlConfig;
    446     char *nfc_dev_node = NULL;
    447     const uint16_t max_len = 260;
    448     NFCSTATUS wConfigStatus = NFCSTATUS_SUCCESS;
    449     NFCSTATUS status = NFCSTATUS_SUCCESS;
    450     /*NCI_INIT_CMD*/
    451     static uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
    452     /*NCI_RESET_CMD*/
    453     static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
    454     /* reset config cache */
    455     resetNxpConfig();
    456 
    457     int init_retry_cnt= 0;
    458     int8_t ret_val = 0x00;
    459 
    460     /* initialize trace level */
    461     phNxpLog_InitializeLogLevel();
    462 
    463     /*Create the timer for extns write response*/
    464     timeoutTimerId = phOsalNfc_Timer_Create();
    465 
    466     if (phNxpNciHal_init_monitor() == NULL)
    467     {
    468         NXPLOG_NCIHAL_E("Init monitor failed");
    469         return NFCSTATUS_FAILED;
    470     }
    471 
    472     CONCURRENCY_LOCK();
    473 
    474     memset(&nxpncihal_ctrl, 0x00, sizeof(nxpncihal_ctrl));
    475     memset(&tOsalConfig, 0x00, sizeof(tOsalConfig));
    476     memset(&tTmlConfig, 0x00, sizeof(tTmlConfig));
    477     memset (&nxpprofile_ctrl, 0, sizeof(phNxpNciProfile_Control_t));
    478 
    479     /* By default HAL status is HAL_STATUS_OPEN */
    480     nxpncihal_ctrl.halStatus = HAL_STATUS_OPEN;
    481 
    482     nxpncihal_ctrl.p_nfc_stack_cback = p_cback;
    483     nxpncihal_ctrl.p_nfc_stack_data_cback = p_data_cback;
    484 
    485     /* Read the nfc device node name */
    486     nfc_dev_node = (char*) malloc (max_len * sizeof (char *));
    487     if (nfc_dev_node == NULL)
    488     {
    489         NXPLOG_NCIHAL_E ("malloc of nfc_dev_node failed ");
    490         goto clean_and_return;
    491     }
    492     else if (!GetNxpStrValue (NAME_NXP_NFC_DEV_NODE, nfc_dev_node, sizeof (nfc_dev_node)))
    493     {
    494         NXPLOG_NCIHAL_E ("Invalid nfc device node name keeping the default device node /dev/pn54x");
    495         strcpy (nfc_dev_node, "/dev/pn54x");
    496     }
    497 
    498     /* Configure hardware link */
    499     nxpncihal_ctrl.gDrvCfg.nClientId = phDal4Nfc_msgget(0, 0600);
    500     nxpncihal_ctrl.gDrvCfg.nLinkType = ENUM_LINK_TYPE_I2C;/* For PN54X */
    501     tTmlConfig.pDevName = (int8_t *) nfc_dev_node;
    502     tOsalConfig.dwCallbackThreadId
    503     = (uintptr_t) nxpncihal_ctrl.gDrvCfg.nClientId;
    504     tOsalConfig.pLogFile = NULL;
    505     tTmlConfig.dwGetMsgThreadId = (uintptr_t) nxpncihal_ctrl.gDrvCfg.nClientId;
    506 
    507 #if(NFC_NXP_CHIP_TYPE == PN548C2)
    508     memset (discovery_cmd, 0, sizeof(discovery_cmd));
    509     discovery_cmd_len = 0;
    510 #endif
    511 
    512     /* Initialize TML layer */
    513     wConfigStatus = phTmlNfc_Init(&tTmlConfig);
    514     if (wConfigStatus != NFCSTATUS_SUCCESS)
    515     {
    516         NXPLOG_NCIHAL_E("phTmlNfc_Init Failed");
    517         goto clean_and_return;
    518     }
    519     else
    520     {
    521         if (nfc_dev_node != NULL)
    522         {
    523             free (nfc_dev_node);
    524             nfc_dev_node = NULL;
    525         }
    526     }
    527 
    528     /* Create the client thread */
    529     pthread_attr_t attr;
    530     pthread_attr_init(&attr);
    531     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    532     ret_val = pthread_create (&nxpncihal_ctrl.client_thread, &attr,
    533             phNxpNciHal_client_thread, &nxpncihal_ctrl);
    534     pthread_attr_destroy (&attr);
    535     if (ret_val != 0)
    536     {
    537         NXPLOG_NCIHAL_E ("pthread_create failed");
    538         wConfigStatus = phTmlNfc_Shutdown ();
    539         goto clean_and_return;
    540     }
    541 
    542     CONCURRENCY_UNLOCK();
    543 
    544     /* call read pending */
    545     status = phTmlNfc_Read(
    546             nxpncihal_ctrl.p_cmd_data,
    547             NCI_MAX_DATA_LEN,
    548             (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_read_complete,
    549             NULL);
    550     if (status != NFCSTATUS_PENDING)
    551     {
    552         NXPLOG_NCIHAL_E("TML Read status error status = %x", status);
    553         wConfigStatus = phTmlNfc_Shutdown();
    554         wConfigStatus = NFCSTATUS_FAILED;
    555         goto clean_and_return;
    556     }
    557 
    558 init_retry:
    559 
    560     phNxpNciHal_ext_init();
    561 
    562     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci),cmd_reset_nci);
    563     if((status != NFCSTATUS_SUCCESS) && (nxpncihal_ctrl.retry_cnt >= MAX_RETRY_COUNT))
    564     {
    565         NXPLOG_NCIHAL_E("Force FW Download, NFCC not coming out from Standby");
    566         wConfigStatus = NFCSTATUS_FAILED;
    567         goto force_download;
    568     }
    569     else if(status != NFCSTATUS_SUCCESS)
    570     {
    571         NXPLOG_NCIHAL_E ("NCI_CORE_RESET: Failed");
    572         if(init_retry_cnt < 3) {
    573             init_retry_cnt++;
    574             (void)phNxpNciHal_power_cycle();
    575             goto init_retry;
    576         } else
    577             init_retry_cnt = 0;
    578         wConfigStatus = phTmlNfc_Shutdown();
    579         wConfigStatus = NFCSTATUS_FAILED;
    580         goto clean_and_return;
    581     }
    582 
    583     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci),cmd_init_nci);
    584     if(status != NFCSTATUS_SUCCESS)
    585     {
    586         NXPLOG_NCIHAL_E ("NCI_CORE_INIT : Failed");
    587         if(init_retry_cnt < 3) {
    588             init_retry_cnt++;
    589             (void)phNxpNciHal_power_cycle();
    590             goto init_retry;
    591         } else
    592             init_retry_cnt = 0;
    593         wConfigStatus = phTmlNfc_Shutdown();
    594         wConfigStatus = NFCSTATUS_FAILED;
    595         goto clean_and_return;
    596     }
    597     phNxpNciHal_enable_i2c_fragmentation();
    598     /*Get FW version from device*/
    599     status = phDnldNfc_InitImgInfo();
    600     NXPLOG_NCIHAL_D ("FW version for FW file = 0x%x", wFwVer);
    601     NXPLOG_NCIHAL_D ("FW version from device = 0x%x", wFwVerRsp);
    602     if ((wFwVerRsp & 0x0000FFFF) == wFwVer)
    603     {
    604         NXPLOG_NCIHAL_D ("FW uptodate not required");
    605         phDnldNfc_ReSetHwDevHandle();
    606     }
    607     else
    608     {
    609 force_download:
    610         if (wFwVerRsp == 0)
    611         {
    612             phDnldNfc_InitImgInfo();
    613         }
    614         if (NFCSTATUS_SUCCESS == phNxpNciHal_CheckValidFwVersion())
    615         {
    616             NXPLOG_NCIHAL_D ("FW update required");
    617             fw_download_success = 0;
    618             status = phNxpNciHal_fw_download();
    619             if (status != NFCSTATUS_SUCCESS)
    620             {
    621                 if (NFCSTATUS_SUCCESS != phNxpNciHal_fw_mw_ver_check ())
    622                 {
    623                     NXPLOG_NCIHAL_D ("Chip Version Middleware Version mismatch!!!!");
    624                     /* Abort any pending read and write */
    625                     phNxpNciHal_send_ext_cmd (sizeof (cmd_reset_nci), cmd_reset_nci);
    626                     phTmlNfc_ReadAbort ();
    627                     phTmlNfc_WriteAbort ();
    628                     phTmlNfc_Shutdown ();
    629                     wConfigStatus = NFCSTATUS_FAILED;
    630                     goto clean_and_return;
    631                 }
    632                 NXPLOG_NCIHAL_E ("FW Download failed - NFCC init will continue");
    633             }
    634             else
    635             {
    636                 wConfigStatus = NFCSTATUS_SUCCESS;
    637                 fw_download_success = 1;
    638                 /* call read pending */
    639                 status = phTmlNfc_Read(
    640                 nxpncihal_ctrl.p_cmd_data,
    641                 NCI_MAX_DATA_LEN,
    642                    (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_read_complete,
    643                         NULL);
    644                 if (status != NFCSTATUS_PENDING)
    645                 {
    646                     NXPLOG_NCIHAL_E("TML Read status error status = %x", status);
    647                     wConfigStatus = phTmlNfc_Shutdown();
    648                     wConfigStatus = NFCSTATUS_FAILED;
    649                     goto clean_and_return;
    650                 }
    651             }
    652         }
    653         else
    654         {
    655             if (wFwVerRsp == 0)
    656                phDnldNfc_ReSetHwDevHandle();
    657         }
    658     }
    659     /* Call open complete */
    660     phNxpNciHal_open_complete(wConfigStatus);
    661 
    662     return wConfigStatus;
    663 
    664     clean_and_return:
    665     CONCURRENCY_UNLOCK();
    666     if (nfc_dev_node != NULL)
    667     {
    668         free (nfc_dev_node);
    669         nfc_dev_node = NULL;
    670     }
    671     /* Report error status */
    672     (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_OPEN_CPLT_EVT,
    673             HAL_NFC_STATUS_FAILED);
    674 
    675     nxpncihal_ctrl.p_nfc_stack_cback = NULL;
    676     nxpncihal_ctrl.p_nfc_stack_data_cback = NULL;
    677     phNxpNciHal_cleanup_monitor();
    678     nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
    679     return NFCSTATUS_FAILED;
    680 }
    681 
    682 /******************************************************************************
    683  * Function         phNxpNciHal_fw_mw_check
    684  *
    685  * Description      This function inform the status of phNxpNciHal_fw_mw_check
    686  *                  function to libnfc-nci.
    687  *
    688  * Returns          int.
    689  *
    690  ******************************************************************************/
    691 int phNxpNciHal_fw_mw_ver_check()
    692 {
    693     NFCSTATUS status = NFCSTATUS_FAILED;
    694     if(!strcmp (COMPILATION_MW, "PN551") && (rom_version == 0x10) && (fw_maj_ver == 0x05))
    695     {
    696         status = NFCSTATUS_SUCCESS;
    697     }
    698     else if (!strcmp (COMPILATION_MW, "PN548C2") && (rom_version == 0x10) && (fw_maj_ver == 0x01))
    699     {
    700         status = NFCSTATUS_SUCCESS;
    701     }
    702     else if (!strcmp (COMPILATION_MW, "PN547C2") && (rom_version == 0x08) && (fw_maj_ver == 0x01))
    703     {
    704         status = NFCSTATUS_SUCCESS;
    705     }
    706     return status;
    707 }
    708 /******************************************************************************
    709  * Function         phNxpNciHal_open_complete
    710  *
    711  * Description      This function inform the status of phNxpNciHal_open
    712  *                  function to libnfc-nci.
    713  *
    714  * Returns          void.
    715  *
    716  ******************************************************************************/
    717 static void phNxpNciHal_open_complete(NFCSTATUS status)
    718 {
    719     static phLibNfc_Message_t msg;
    720 
    721     if (status == NFCSTATUS_SUCCESS)
    722     {
    723         msg.eMsgType = NCI_HAL_OPEN_CPLT_MSG;
    724         nxpncihal_ctrl.hal_open_status = TRUE;
    725     }
    726     else
    727     {
    728         msg.eMsgType = NCI_HAL_ERROR_MSG;
    729     }
    730 
    731     msg.pMsgData = NULL;
    732     msg.Size = 0;
    733 
    734     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
    735             (phLibNfc_Message_t *) &msg);
    736 
    737     return;
    738 }
    739 
    740 /******************************************************************************
    741  * Function         phNxpNciHal_write
    742  *
    743  * Description      This function write the data to NFCC through physical
    744  *                  interface (e.g. I2C) using the PN54X driver interface.
    745  *                  Before sending the data to NFCC, phNxpNciHal_write_ext
    746  *                  is called to check if there is any extension processing
    747  *                  is required for the NCI packet being sent out.
    748  *
    749  * Returns          It returns number of bytes successfully written to NFCC.
    750  *
    751  ******************************************************************************/
    752 int phNxpNciHal_write(uint16_t data_len, const uint8_t *p_data)
    753 {
    754     NFCSTATUS status = NFCSTATUS_FAILED;
    755     static phLibNfc_Message_t msg;
    756 
    757     /* Create local copy of cmd_data */
    758     memcpy(nxpncihal_ctrl.p_cmd_data, p_data, data_len);
    759     nxpncihal_ctrl.cmd_len = data_len;
    760     if (nxpncihal_ctrl.cmd_len > NCI_MAX_DATA_LEN)
    761     {
    762         NXPLOG_NCIHAL_D ("cmd_len exceeds limit NCI_MAX_DATA_LEN");
    763         goto clean_and_return;
    764     }
    765 #ifdef P2P_PRIO_LOGIC_HAL_IMP
    766     /* Specific logic to block RF disable when P2P priority logic is busy */
    767     if (p_data[0] == 0x21&&
    768         p_data[1] == 0x06 &&
    769         p_data[2] == 0x01 &&
    770         EnableP2P_PrioLogic == TRUE)
    771     {
    772         NXPLOG_NCIHAL_D ("P2P priority logic busy: Disable it.");
    773         phNxpNciHal_clean_P2P_Prio();
    774     }
    775 #endif
    776     /* Specific logic to block RF disable when Kovio detection logic is active */
    777     if (p_data[0] == 0x21&&
    778         p_data[1] == 0x06 &&
    779         p_data[2] == 0x01)
    780     {
    781         rf_deactive_cmd = TRUE;
    782         if (kovio_detected == TRUE)
    783         {
    784             NXPLOG_NCIHAL_D ("Kovio detection logic is active: Set Flag to disable it.");
    785             disable_kovio = 0x01;
    786         }
    787     }
    788 
    789     /* Check for NXP ext before sending write */
    790     status = phNxpNciHal_write_ext(&nxpncihal_ctrl.cmd_len,
    791             nxpncihal_ctrl.p_cmd_data, &nxpncihal_ctrl.rsp_len,
    792             nxpncihal_ctrl.p_rsp_data);
    793     if (status != NFCSTATUS_SUCCESS)
    794     {
    795         /* Do not send packet to PN54X, send response directly */
    796         msg.eMsgType = NCI_HAL_RX_MSG;
    797         msg.pMsgData = NULL;
    798         msg.Size = 0;
    799 
    800         phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
    801                 (phLibNfc_Message_t *) &msg);
    802         goto clean_and_return;
    803     }
    804 
    805     CONCURRENCY_LOCK();
    806     data_len = phNxpNciHal_write_unlocked(nxpncihal_ctrl.cmd_len,
    807             nxpncihal_ctrl.p_cmd_data);
    808     CONCURRENCY_UNLOCK();
    809 
    810     if (icode_send_eof == 1)
    811     {
    812         usleep (10000);
    813         icode_send_eof = 2;
    814         phNxpNciHal_send_ext_cmd (3, cmd_icode_eof);
    815     }
    816 
    817     clean_and_return:
    818     /* No data written */
    819     return data_len;
    820 }
    821 
    822 /******************************************************************************
    823  * Function         phNxpNciHal_write_unlocked
    824  *
    825  * Description      This is the actual function which is being called by
    826  *                  phNxpNciHal_write. This function writes the data to NFCC.
    827  *                  It waits till write callback provide the result of write
    828  *                  process.
    829  *
    830  * Returns          It returns number of bytes successfully written to NFCC.
    831  *
    832  ******************************************************************************/
    833 int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t *p_data)
    834 {
    835     NFCSTATUS status = NFCSTATUS_INVALID_PARAMETER;
    836     phNxpNciHal_Sem_t cb_data;
    837     nxpncihal_ctrl.retry_cnt = 0;
    838     static uint8_t reset_ntf[] = {0x60, 0x00, 0x06, 0xA0, 0x00, 0xC7, 0xD4, 0x00, 0x00};
    839 
    840     /* Create the local semaphore */
    841     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    842     {
    843         NXPLOG_NCIHAL_D("phNxpNciHal_write_unlocked Create cb data failed");
    844         data_len = 0;
    845         goto clean_and_return;
    846     }
    847 
    848     /* Create local copy of cmd_data */
    849     memcpy(nxpncihal_ctrl.p_cmd_data, p_data, data_len);
    850     nxpncihal_ctrl.cmd_len = data_len;
    851 
    852     retry:
    853 
    854     data_len = nxpncihal_ctrl.cmd_len;
    855 
    856     status = phTmlNfc_Write( (uint8_t *) nxpncihal_ctrl.p_cmd_data,
    857             (uint16_t) nxpncihal_ctrl.cmd_len,
    858             (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_write_complete,
    859             (void *) &cb_data);
    860     if (status != NFCSTATUS_PENDING)
    861     {
    862         NXPLOG_NCIHAL_E("write_unlocked status error");
    863         data_len = 0;
    864         goto clean_and_return;
    865     }
    866 
    867     /* Wait for callback response */
    868     if (SEM_WAIT(cb_data))
    869     {
    870         NXPLOG_NCIHAL_E("write_unlocked semaphore error");
    871         data_len = 0;
    872         goto clean_and_return;
    873     }
    874 
    875     if (cb_data.status != NFCSTATUS_SUCCESS)
    876     {
    877         data_len = 0;
    878         if(nxpncihal_ctrl.retry_cnt++ < MAX_RETRY_COUNT)
    879         {
    880             NXPLOG_NCIHAL_E("write_unlocked failed - PN54X Maybe in Standby Mode - Retry");
    881             /* 1ms delay to give NFCC wake up delay */
    882             usleep(1000);
    883             goto retry;
    884         }
    885         else
    886         {
    887 
    888             NXPLOG_NCIHAL_E("write_unlocked failed - PN54X Maybe in Standby Mode (max count = 0x%x)", nxpncihal_ctrl.retry_cnt);
    889 
    890             status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
    891 
    892             if(NFCSTATUS_SUCCESS == status)
    893             {
    894                 NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n");
    895             }
    896             else
    897             {
    898                 NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
    899             }
    900             if (nxpncihal_ctrl.p_nfc_stack_data_cback!= NULL &&
    901                 nxpncihal_ctrl.p_rx_data!= NULL &&
    902                 nxpncihal_ctrl.hal_open_status == TRUE)
    903             {
    904                 NXPLOG_NCIHAL_D("Send the Core Reset NTF to upper layer, which will trigger the recovery\n");
    905                 //Send the Core Reset NTF to upper layer, which will trigger the recovery.
    906                 nxpncihal_ctrl.rx_data_len = sizeof(reset_ntf);
    907                 memcpy(nxpncihal_ctrl.p_rx_data, reset_ntf, sizeof(reset_ntf));
    908                 (*nxpncihal_ctrl.p_nfc_stack_data_cback)(nxpncihal_ctrl.rx_data_len, nxpncihal_ctrl.p_rx_data);
    909             }
    910         }
    911     }
    912 
    913     clean_and_return:
    914     phNxpNciHal_cleanup_cb_data(&cb_data);
    915     return data_len;
    916 }
    917 
    918 /******************************************************************************
    919  * Function         phNxpNciHal_write_complete
    920  *
    921  * Description      This function handles write callback.
    922  *
    923  * Returns          void.
    924  *
    925  ******************************************************************************/
    926 static void phNxpNciHal_write_complete(void *pContext, phTmlNfc_TransactInfo_t *pInfo)
    927 {
    928     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    929 
    930     if (pInfo->wStatus == NFCSTATUS_SUCCESS)
    931     {
    932         NXPLOG_NCIHAL_D("write successful status = 0x%x", pInfo->wStatus);
    933     }
    934     else
    935     {
    936         NXPLOG_NCIHAL_E("write error status = 0x%x", pInfo->wStatus);
    937     }
    938 
    939     p_cb_data->status = pInfo->wStatus;
    940 
    941     SEM_POST(p_cb_data);
    942 
    943     return;
    944 }
    945 
    946 /******************************************************************************
    947  * Function         phNxpNciHal_read_complete
    948  *
    949  * Description      This function is called whenever there is an NCI packet
    950  *                  received from NFCC. It could be RSP or NTF packet. This
    951  *                  function provide the received NCI packet to libnfc-nci
    952  *                  using data callback of libnfc-nci.
    953  *                  There is a pending read called from each
    954  *                  phNxpNciHal_read_complete so each a packet received from
    955  *                  NFCC can be provide to libnfc-nci.
    956  *
    957  * Returns          void.
    958  *
    959  ******************************************************************************/
    960 static void phNxpNciHal_read_complete(void *pContext, phTmlNfc_TransactInfo_t *pInfo)
    961 {
    962     NFCSTATUS status = NFCSTATUS_FAILED;
    963     UNUSED(pContext);
    964     if(nxpncihal_ctrl.read_retry_cnt == 1)
    965     {
    966         nxpncihal_ctrl.read_retry_cnt = 0;
    967     }
    968 
    969     if (pInfo->wStatus == NFCSTATUS_SUCCESS)
    970     {
    971         NXPLOG_NCIHAL_D("read successful status = 0x%x", pInfo->wStatus);
    972 
    973         nxpncihal_ctrl.p_rx_data = pInfo->pBuff;
    974         nxpncihal_ctrl.rx_data_len = pInfo->wLength;
    975 
    976         status = phNxpNciHal_process_ext_rsp (nxpncihal_ctrl.p_rx_data, &nxpncihal_ctrl.rx_data_len);
    977 
    978         phNxpNciHal_print_res_status(nxpncihal_ctrl.p_rx_data,  &nxpncihal_ctrl.rx_data_len);
    979         /* Check if response should go to hal module only */
    980         if (nxpncihal_ctrl.hal_ext_enabled == 1
    981                 && (nxpncihal_ctrl.p_rx_data[0x00] & 0xF0) == 0x40)
    982         {
    983             if(status == NFCSTATUS_FAILED)
    984             {
    985                 NXPLOG_NCIHAL_D("enter into NFCC init recovery");
    986                 nxpncihal_ctrl.ext_cb_data.status = status;
    987             }
    988             /* Unlock semaphore only for responses*/
    989             if ((nxpncihal_ctrl.p_rx_data[0x00] & 0xF0) == 0x40 ||
    990                ((icode_detected == TRUE) &&(icode_send_eof == 3)))
    991             {
    992                 /* Unlock semaphore */
    993                 SEM_POST (&(nxpncihal_ctrl.ext_cb_data));
    994             }
    995         }
    996         /* Read successful send the event to higher layer */
    997         else if ((nxpncihal_ctrl.p_nfc_stack_data_cback != NULL) &&
    998                 (status == NFCSTATUS_SUCCESS)&&(send_to_upper_kovio==1))
    999         {
   1000             (*nxpncihal_ctrl.p_nfc_stack_data_cback)(
   1001                     nxpncihal_ctrl.rx_data_len, nxpncihal_ctrl.p_rx_data);
   1002         }
   1003     }
   1004     else
   1005     {
   1006         NXPLOG_NCIHAL_E("read error status = 0x%x", pInfo->wStatus);
   1007     }
   1008 
   1009     if(nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE)
   1010     {
   1011         return;
   1012     }
   1013     /* Read again because read must be pending always.*/
   1014     status = phTmlNfc_Read(
   1015             Rx_data,
   1016             NCI_MAX_DATA_LEN,
   1017             (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_read_complete,
   1018             NULL);
   1019     if (status != NFCSTATUS_PENDING)
   1020     {
   1021         NXPLOG_NCIHAL_E("read status error status = %x", status);
   1022         /* TODO: Not sure how to handle this ? */
   1023     }
   1024 
   1025     return;
   1026 }
   1027 
   1028 void read_retry()
   1029 {
   1030     /* Read again because read must be pending always.*/
   1031     NFCSTATUS status = phTmlNfc_Read(
   1032             Rx_data,
   1033             NCI_MAX_DATA_LEN,
   1034             (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_read_complete,
   1035             NULL);
   1036     if (status != NFCSTATUS_PENDING)
   1037     {
   1038         NXPLOG_NCIHAL_E("read status error status = %x", status);
   1039         /* TODO: Not sure how to handle this ? */
   1040     }
   1041 }
   1042 /******************************************************************************
   1043  * Function         phNxpNciHal_core_initialized
   1044  *
   1045  * Description      This function is called by libnfc-nci after successful open
   1046  *                  of NFCC. All proprietary setting for PN54X are done here.
   1047  *                  After completion of proprietary settings notification is
   1048  *                  provided to libnfc-nci through callback function.
   1049  *
   1050  * Returns          Always returns NFCSTATUS_SUCCESS (0).
   1051  *
   1052  ******************************************************************************/
   1053 int phNxpNciHal_core_initialized(uint8_t* p_core_init_rsp_params)
   1054 {
   1055     NFCSTATUS status = NFCSTATUS_SUCCESS;
   1056     static uint8_t p2p_listen_mode_routing_cmd[] = { 0x21, 0x01, 0x07, 0x00, 0x01,
   1057                                                 0x01, 0x03, 0x00, 0x01, 0x05 };
   1058 
   1059     uint8_t swp_full_pwr_mode_on_cmd[] = { 0x20, 0x02, 0x05, 0x01, 0xA0,
   1060                                            0xF1,0x01,0x01 };
   1061 
   1062     static uint8_t android_l_aid_matching_mode_on_cmd[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x91, 0x01, 0x01};
   1063     static uint8_t swp_switch_timeout_cmd[] = {0x20, 0x02, 0x06, 0x01, 0xA0, 0xF3, 0x02, 0x00, 0x00};
   1064 
   1065     uint8_t *buffer = NULL;
   1066     long bufflen = 260;
   1067     long retlen = 0;
   1068     int isfound;
   1069     /* Temp fix to re-apply the proper clock setting */
   1070     int temp_fix = 1;
   1071     unsigned long num = 0;
   1072 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1073     //initialize dummy FW recovery variables
   1074     gRecFwRetryCount = 0;
   1075     gRecFWDwnld = 0;
   1076 #endif
   1077     // recovery --start
   1078     /*NCI_INIT_CMD*/
   1079     static uint8_t cmd_init_nci[] = {0x20,0x01,0x00};
   1080     /*NCI_RESET_CMD*/
   1081     static uint8_t cmd_reset_nci[] = {0x20,0x00,0x01,0x00}; //keep configuration
   1082     /* reset config cache */
   1083     static uint8_t retry_core_init_cnt;
   1084 
   1085     if((*p_core_init_rsp_params > 0) && (*p_core_init_rsp_params < 4)) //initializing for recovery.
   1086     {
   1087 retry_core_init:
   1088         config_access = FALSE;
   1089         if(buffer != NULL)
   1090         {
   1091             free(buffer);
   1092             buffer = NULL;
   1093         }
   1094         if(retry_core_init_cnt > 3)
   1095         {
   1096             return NFCSTATUS_FAILED;
   1097         }
   1098 
   1099         status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
   1100         if(NFCSTATUS_SUCCESS == status) { NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n"); }
   1101         else { NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n"); }
   1102 
   1103         status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci),cmd_reset_nci);
   1104         if((status != NFCSTATUS_SUCCESS) && (nxpncihal_ctrl.retry_cnt >= MAX_RETRY_COUNT))
   1105         {
   1106             NXPLOG_NCIHAL_E("Force FW Download, NFCC not coming out from Standby");
   1107             retry_core_init_cnt++;
   1108             goto retry_core_init;
   1109         }
   1110         else if(status != NFCSTATUS_SUCCESS)
   1111         {
   1112             NXPLOG_NCIHAL_E ("NCI_CORE_RESET: Failed");
   1113             retry_core_init_cnt++;
   1114             goto retry_core_init;
   1115 
   1116         }
   1117 
   1118         if(*p_core_init_rsp_params == 2) {
   1119             NXPLOG_NCIHAL_E(" Last command is CORE_RESET!!");
   1120             goto invoke_callback;
   1121         }
   1122 
   1123         status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci),cmd_init_nci);
   1124         if(status != NFCSTATUS_SUCCESS)
   1125         {
   1126             NXPLOG_NCIHAL_E ("NCI_CORE_INIT : Failed");
   1127             retry_core_init_cnt++;
   1128             goto retry_core_init;
   1129         }
   1130 
   1131         if(*p_core_init_rsp_params == 3) {
   1132             NXPLOG_NCIHAL_E(" Last command is CORE_INIT!!");
   1133             goto invoke_callback;
   1134         }
   1135     }
   1136 // recovery --end
   1137 
   1138 
   1139     buffer = (uint8_t*) malloc(bufflen*sizeof(uint8_t));
   1140     if(NULL == buffer)
   1141     {
   1142         return NFCSTATUS_FAILED;
   1143     }
   1144     config_access = TRUE;
   1145     retlen = 0;
   1146     isfound = GetNxpByteArrayValue(NAME_NXP_ACT_PROP_EXTN, (char *) buffer,
   1147             bufflen, &retlen);
   1148     if (retlen > 0) {
   1149         /* NXP ACT Proprietary Ext */
   1150         status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1151         if (status != NFCSTATUS_SUCCESS) {
   1152             NXPLOG_NCIHAL_E("NXP ACT Proprietary Ext failed");
   1153             retry_core_init_cnt++;
   1154             goto retry_core_init;
   1155         }
   1156     }
   1157 
   1158     // Check if firmware download success
   1159     status = phNxpNciHal_get_mw_eeprom ();
   1160     if (status != NFCSTATUS_SUCCESS)
   1161     {
   1162         NXPLOG_NCIHAL_E ("NXP GET MW EEPROM AREA Proprietary Ext failed");
   1163         retry_core_init_cnt++;
   1164         goto retry_core_init;
   1165     }
   1166 
   1167     //
   1168     status = phNxpNciHal_check_clock_config();
   1169     if (status != NFCSTATUS_SUCCESS) {
   1170         NXPLOG_NCIHAL_E("phNxpNciHal_check_clock_config failed");
   1171         retry_core_init_cnt++;
   1172         goto retry_core_init;
   1173     }
   1174 
   1175 #ifdef PN547C2_CLOCK_SETTING
   1176     if (isNxpConfigModified() || (fw_download_success == 1) || (phNxpNciClock.issetConfig)
   1177 #if(NFC_NXP_HFO_SETTINGS == TRUE)
   1178         || temp_fix == 1
   1179 #endif
   1180         )
   1181     {
   1182         //phNxpNciHal_get_clk_freq();
   1183         phNxpNciHal_set_clock();
   1184         phNxpNciClock.issetConfig = FALSE;
   1185 #if(NFC_NXP_HFO_SETTINGS == TRUE)
   1186         if (temp_fix == 1 )
   1187         {
   1188             NXPLOG_NCIHAL_D("Applying Default Clock setting and DPLL register at power on");
   1189             /*
   1190             # A0, 0D, 06, 06, 83, 55, 2A, 04, 00 RF_CLIF_CFG_TARGET CLIF_DPLL_GEAR_REG
   1191             # A0, 0D, 06, 06, 82, 33, 14, 17, 00 RF_CLIF_CFG_TARGET CLIF_DPLL_INIT_REG
   1192             # A0, 0D, 06, 06, 84, AA, 85, 00, 80 RF_CLIF_CFG_TARGET CLIF_DPLL_INIT_FREQ_REG
   1193             # A0, 0D, 06, 06, 81, 63, 00, 00, 00 RF_CLIF_CFG_TARGET CLIF_DPLL_CONTROL_REG
   1194             */
   1195             static uint8_t cmd_dpll_set_reg_nci[] = {0x20, 0x02, 0x25, 0x04,
   1196                                                                             0xA0, 0x0D, 0x06, 0x06, 0x83, 0x55, 0x2A, 0x04, 0x00,
   1197                                                                             0xA0, 0x0D, 0x06, 0x06, 0x82, 0x33, 0x14, 0x17, 0x00,
   1198                                                                             0xA0, 0x0D, 0x06, 0x06, 0x84, 0xAA, 0x85, 0x00, 0x80,
   1199                                                                             0xA0, 0x0D, 0x06, 0x06, 0x81, 0x63, 0x00, 0x00, 0x00};
   1200 
   1201             status = phNxpNciHal_send_ext_cmd(sizeof(cmd_dpll_set_reg_nci), cmd_dpll_set_reg_nci);
   1202             if (status != NFCSTATUS_SUCCESS) {
   1203                 NXPLOG_NCIHAL_E("NXP DPLL REG ACT Proprietary Ext failed");
   1204                 retry_core_init_cnt++;
   1205                 goto retry_core_init;
   1206             }
   1207             /* reset the NFCC after applying the clock setting and DPLL setting */
   1208             //phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
   1209             temp_fix = 0;
   1210             goto retry_core_init;
   1211         }
   1212 #endif
   1213     }
   1214 #endif
   1215 
   1216     phNxpNciHal_check_factory_reset();
   1217     retlen = 0;
   1218     config_access = TRUE;
   1219     isfound = GetNxpByteArrayValue(NAME_NXP_NFC_PROFILE_EXTN, (char *) buffer,
   1220             bufflen, &retlen);
   1221     if (retlen > 0) {
   1222         /* NXP ACT Proprietary Ext */
   1223         status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1224         if (status != NFCSTATUS_SUCCESS) {
   1225             NXPLOG_NCIHAL_E("NXP ACT Proprietary Ext failed");
   1226             retry_core_init_cnt++;
   1227             goto retry_core_init;
   1228         }
   1229     }
   1230 
   1231     if(isNxpConfigModified() || (fw_download_success == 1))
   1232     {
   1233 
   1234         retlen = 0;
   1235         fw_download_success = 0;
   1236 
   1237 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1238         NXPLOG_NCIHAL_D ("Performing TVDD Settings");
   1239         isfound = GetNxpNumValue(NAME_NXP_EXT_TVDD_CFG, &num, sizeof(num));
   1240         if (isfound > 0) {
   1241             if(num == 1) {
   1242                 isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_1, (char *) buffer,
   1243                         bufflen, &retlen);
   1244                 if (retlen > 0) {
   1245                     status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1246                     if (status != NFCSTATUS_SUCCESS) {
   1247                         NXPLOG_NCIHAL_E("EXT TVDD CFG 1 Settings failed");
   1248                         retry_core_init_cnt++;
   1249                         goto retry_core_init;
   1250                     }
   1251                 }
   1252             }
   1253             else if(num == 2) {
   1254                 isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_2, (char *) buffer,
   1255                         bufflen, &retlen);
   1256                     if (retlen > 0) {
   1257                     status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1258                     if (status != NFCSTATUS_SUCCESS) {
   1259                         NXPLOG_NCIHAL_E("EXT TVDD CFG 2 Settings failed");
   1260                         retry_core_init_cnt++;
   1261                         goto retry_core_init;
   1262                     }
   1263                 }
   1264             }
   1265             else if(num == 3) {
   1266                 isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_3, (char *) buffer,
   1267                         bufflen, &retlen);
   1268                     if (retlen > 0) {
   1269                     status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1270                     if (status != NFCSTATUS_SUCCESS) {
   1271                         NXPLOG_NCIHAL_E("EXT TVDD CFG 3 Settings failed");
   1272                         retry_core_init_cnt++;
   1273                         goto retry_core_init;
   1274                     }
   1275                 }
   1276             }
   1277             else {
   1278                 NXPLOG_NCIHAL_E("Wrong Configuration Value %ld", num);
   1279             }
   1280 
   1281         }
   1282 #endif
   1283         retlen = 0;
   1284 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1285         config_access = FALSE;
   1286 #endif
   1287         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 1");
   1288         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_1, (char *) buffer,
   1289                 bufflen, &retlen);
   1290         if (retlen > 0) {
   1291             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1292 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1293             if (status == NFCSTATUS_SUCCESS)
   1294             {
   1295                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1296                 /*STATUS INVALID PARAM 0x09*/
   1297                 if (status == 0x09)
   1298                 {
   1299                     phNxpNciHalRFConfigCmdRecSequence ();
   1300                     retry_core_init_cnt++;
   1301                     goto retry_core_init;
   1302                 }
   1303             }
   1304             else
   1305 #endif
   1306             if (status != NFCSTATUS_SUCCESS) {
   1307                 NXPLOG_NCIHAL_E("RF Settings BLK 1 failed");
   1308                 retry_core_init_cnt++;
   1309                 goto retry_core_init;
   1310             }
   1311         }
   1312         retlen = 0;
   1313 
   1314         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 2");
   1315         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_2, (char *) buffer,
   1316                 bufflen, &retlen);
   1317         if (retlen > 0) {
   1318             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1319 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1320             if (status == NFCSTATUS_SUCCESS)
   1321             {
   1322                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1323                 /*STATUS INVALID PARAM 0x09*/
   1324                 if (status == 0x09)
   1325                 {
   1326                     phNxpNciHalRFConfigCmdRecSequence ();
   1327                     retry_core_init_cnt++;
   1328                     goto retry_core_init;
   1329                 }
   1330             }
   1331             else
   1332 #endif
   1333             if (status != NFCSTATUS_SUCCESS) {
   1334                 NXPLOG_NCIHAL_E("RF Settings BLK 2 failed");
   1335                 retry_core_init_cnt++;
   1336                 goto retry_core_init;
   1337             }
   1338         }
   1339         retlen = 0;
   1340 
   1341         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 3");
   1342         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_3, (char *) buffer,
   1343                 bufflen, &retlen);
   1344         if (retlen > 0) {
   1345             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1346 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1347             if (status == NFCSTATUS_SUCCESS)
   1348             {
   1349                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1350                 /*STATUS INVALID PARAM 0x09*/
   1351                 if (status == 0x09)
   1352                 {
   1353                     phNxpNciHalRFConfigCmdRecSequence ();
   1354                     retry_core_init_cnt++;
   1355                     goto retry_core_init;
   1356                 }
   1357             }
   1358             else
   1359 #endif
   1360             if (status != NFCSTATUS_SUCCESS) {
   1361                 NXPLOG_NCIHAL_E("RF Settings BLK 3 failed");
   1362                 retry_core_init_cnt++;
   1363                 goto retry_core_init;
   1364             }
   1365         }
   1366         retlen = 0;
   1367 
   1368         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 4");
   1369         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_4, (char *) buffer,
   1370                 bufflen, &retlen);
   1371         if (retlen > 0) {
   1372             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1373 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1374             if (status == NFCSTATUS_SUCCESS)
   1375             {
   1376                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1377                 /*STATUS INVALID PARAM 0x09*/
   1378                 if (status == 0x09)
   1379                 {
   1380                     phNxpNciHalRFConfigCmdRecSequence ();
   1381                     retry_core_init_cnt++;
   1382                     goto retry_core_init;
   1383                 }
   1384             }
   1385             else
   1386 #endif
   1387             if (status != NFCSTATUS_SUCCESS) {
   1388                 NXPLOG_NCIHAL_E("RF Settings BLK 4 failed");
   1389                 retry_core_init_cnt++;
   1390                 goto retry_core_init;
   1391             }
   1392         }
   1393         retlen = 0;
   1394 
   1395         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 5");
   1396         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_5, (char *) buffer,
   1397                 bufflen, &retlen);
   1398         if (retlen > 0) {
   1399             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1400 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1401             if (status == NFCSTATUS_SUCCESS)
   1402             {
   1403                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1404                 /*STATUS INVALID PARAM 0x09*/
   1405                 if (status == 0x09)
   1406                 {
   1407                     phNxpNciHalRFConfigCmdRecSequence ();
   1408                     retry_core_init_cnt++;
   1409                     goto retry_core_init;
   1410                 }
   1411             }
   1412             else
   1413 #endif
   1414             if (status != NFCSTATUS_SUCCESS) {
   1415                 NXPLOG_NCIHAL_E("RF Settings BLK 5 failed");
   1416                 retry_core_init_cnt++;
   1417                 goto retry_core_init;
   1418             }
   1419         }
   1420         retlen = 0;
   1421 
   1422         NXPLOG_NCIHAL_D ("Performing RF Settings BLK 6");
   1423         isfound = GetNxpByteArrayValue(NAME_NXP_RF_CONF_BLK_6, (char *) buffer,
   1424                 bufflen, &retlen);
   1425         if (retlen > 0) {
   1426             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1427 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1428             if (status == NFCSTATUS_SUCCESS)
   1429             {
   1430                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1431                 /*STATUS INVALID PARAM 0x09*/
   1432                 if (status == 0x09)
   1433                 {
   1434                     phNxpNciHalRFConfigCmdRecSequence ();
   1435                     retry_core_init_cnt++;
   1436                     goto retry_core_init;
   1437                 }
   1438             }
   1439             else
   1440 #endif
   1441             if (status != NFCSTATUS_SUCCESS) {
   1442                 NXPLOG_NCIHAL_E("RF Settings BLK 6 failed");
   1443                 retry_core_init_cnt++;
   1444                 goto retry_core_init;
   1445             }
   1446         }
   1447         retlen = 0;
   1448 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1449         config_access = TRUE;
   1450 #endif
   1451         NXPLOG_NCIHAL_D ("Performing NAME_NXP_CORE_CONF_EXTN Settings");
   1452         isfound = GetNxpByteArrayValue(NAME_NXP_CORE_CONF_EXTN,
   1453                 (char *) buffer, bufflen, &retlen);
   1454         if (retlen > 0) {
   1455             /* NXP ACT Proprietary Ext */
   1456             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1457             if (status != NFCSTATUS_SUCCESS) {
   1458                 NXPLOG_NCIHAL_E("NXP Core configuration failed");
   1459                 retry_core_init_cnt++;
   1460                 goto retry_core_init;
   1461             }
   1462         }
   1463 
   1464         retlen = 0;
   1465 
   1466         isfound = GetNxpByteArrayValue(NAME_NXP_CORE_MFCKEY_SETTING,
   1467                 (char *) buffer, bufflen, &retlen);
   1468         if (retlen > 0) {
   1469             /* NXP ACT Proprietary Ext */
   1470             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1471             if (status != NFCSTATUS_SUCCESS) {
   1472                 NXPLOG_NCIHAL_E("Setting mifare keys failed");
   1473                 retry_core_init_cnt++;
   1474                 goto retry_core_init;
   1475             }
   1476         }
   1477 
   1478         retlen = 0;
   1479 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1480         config_access = FALSE;
   1481 #endif
   1482         isfound = GetNxpByteArrayValue(NAME_NXP_CORE_RF_FIELD,
   1483                 (char *) buffer, bufflen, &retlen);
   1484         if (retlen > 0) {
   1485             /* NXP ACT Proprietary Ext */
   1486             status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1487 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1488             if (status == NFCSTATUS_SUCCESS)
   1489             {
   1490                 status = phNxpNciHal_CheckRFCmdRespStatus ();
   1491                 /*STATUS INVALID PARAM 0x09*/
   1492                 if (status == 0x09)
   1493                 {
   1494                     phNxpNciHalRFConfigCmdRecSequence ();
   1495                     retry_core_init_cnt++;
   1496                     goto retry_core_init;
   1497                 }
   1498             }
   1499             else
   1500 #endif
   1501             if (status != NFCSTATUS_SUCCESS) {
   1502                 NXPLOG_NCIHAL_E("Setting NXP_CORE_RF_FIELD status failed");
   1503                 retry_core_init_cnt++;
   1504                 goto retry_core_init;
   1505             }
   1506         }
   1507 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1508         config_access = TRUE;
   1509 #endif
   1510 
   1511         retlen = 0;
   1512 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1513         /* NXP SWP switch timeout Setting*/
   1514         if(GetNxpNumValue(NAME_NXP_SWP_SWITCH_TIMEOUT, (void *)&retlen, sizeof(retlen)))
   1515         {
   1516             //Check the permissible range [0 - 60]
   1517             if(0 <= retlen && retlen <= 60)
   1518             {
   1519                 if( 0 < retlen)
   1520                 {
   1521                     unsigned int timeout = retlen * 1000;
   1522                     unsigned int timeoutHx = 0x0000;
   1523 
   1524                     char tmpbuffer[10] = {0};
   1525                     snprintf ((char*)tmpbuffer, 10, "%04x", timeout);
   1526                     sscanf ((char*)tmpbuffer,"%x", &timeoutHx);
   1527 
   1528                     swp_switch_timeout_cmd[7]= (timeoutHx & 0xFF);
   1529                     swp_switch_timeout_cmd[8]= ((timeoutHx & 0xFF00) >> 8);
   1530                 }
   1531 
   1532                 status = phNxpNciHal_send_ext_cmd (sizeof(swp_switch_timeout_cmd),
   1533                                                           swp_switch_timeout_cmd);
   1534                 if (status != NFCSTATUS_SUCCESS)
   1535                 {
   1536                     NXPLOG_NCIHAL_E ("SWP switch timeout Setting Failed");
   1537                     retry_core_init_cnt++;
   1538                     goto retry_core_init;
   1539                 }
   1540             }
   1541             else
   1542             {
   1543                 NXPLOG_NCIHAL_E("SWP switch timeout Setting Failed - out of range!");
   1544             }
   1545 
   1546         }
   1547 
   1548         status = phNxpNciHal_china_tianjin_rf_setting();
   1549         if (status != NFCSTATUS_SUCCESS)
   1550         {
   1551             NXPLOG_NCIHAL_E("phNxpNciHal_china_tianjin_rf_setting failed");
   1552             return NFCSTATUS_FAILED;
   1553         }
   1554 #endif
   1555         // Update eeprom value
   1556         status = phNxpNciHal_set_mw_eeprom ();
   1557         if (status != NFCSTATUS_SUCCESS)
   1558         {
   1559             NXPLOG_NCIHAL_E ("NXP Update MW EEPROM Proprietary Ext failed");
   1560         }
   1561     }
   1562 
   1563     retlen = 0;
   1564 
   1565     isfound = GetNxpByteArrayValue(NAME_NXP_CORE_STANDBY, (char *) buffer,bufflen, &retlen);
   1566     if (retlen > 0) {
   1567         /* NXP ACT Proprietary Ext */
   1568         status = phNxpNciHal_send_ext_cmd(retlen, buffer);
   1569         if (status != NFCSTATUS_SUCCESS) {
   1570             NXPLOG_NCIHAL_E("Stand by mode enable failed");
   1571             retry_core_init_cnt++;
   1572             goto retry_core_init;
   1573         }
   1574     }
   1575     retlen = 0;
   1576 
   1577     isfound =  GetNxpByteArrayValue(NAME_NXP_CORE_CONF,(char *)buffer,bufflen,&retlen);
   1578     if(retlen > 0)
   1579     {
   1580         /* NXP ACT Proprietary Ext */
   1581         status = phNxpNciHal_send_ext_cmd(retlen,buffer);
   1582         if (status != NFCSTATUS_SUCCESS)
   1583         {
   1584             NXPLOG_NCIHAL_E("Core Set Config failed");
   1585             retry_core_init_cnt++;
   1586             goto retry_core_init;
   1587         }
   1588     }
   1589 
   1590     config_access = FALSE;
   1591     //if length of last command is 0 then only reset the P2P listen mode routing.
   1592     if(p_core_init_rsp_params[35] == 0)
   1593     {
   1594         /* P2P listen mode routing */
   1595         status = phNxpNciHal_send_ext_cmd (sizeof (p2p_listen_mode_routing_cmd), p2p_listen_mode_routing_cmd);
   1596         if (status != NFCSTATUS_SUCCESS)
   1597         {
   1598             NXPLOG_NCIHAL_E("P2P listen mode routing failed");
   1599             retry_core_init_cnt++;
   1600             goto retry_core_init;
   1601         }
   1602     }
   1603 
   1604     retlen = 0;
   1605 
   1606     /* SWP FULL PWR MODE SETTING ON */
   1607     if(GetNxpNumValue(NAME_NXP_SWP_FULL_PWR_ON, (void *)&retlen, sizeof(retlen)))
   1608     {
   1609         if(1 == retlen)
   1610         {
   1611             status = phNxpNciHal_send_ext_cmd (sizeof(swp_full_pwr_mode_on_cmd),
   1612                                                       swp_full_pwr_mode_on_cmd);
   1613             if (status != NFCSTATUS_SUCCESS)
   1614             {
   1615                NXPLOG_NCIHAL_E("SWP FULL PWR MODE SETTING ON CMD FAILED");
   1616                 retry_core_init_cnt++;
   1617                 goto retry_core_init;
   1618             }
   1619         }
   1620         else
   1621         {
   1622             swp_full_pwr_mode_on_cmd[7]=0x00;
   1623             status = phNxpNciHal_send_ext_cmd (sizeof(swp_full_pwr_mode_on_cmd),
   1624                                                       swp_full_pwr_mode_on_cmd);
   1625             if (status != NFCSTATUS_SUCCESS)
   1626             {
   1627                 NXPLOG_NCIHAL_E("SWP FULL PWR MODE SETTING OFF CMD FAILED");
   1628                 retry_core_init_cnt++;
   1629                 goto retry_core_init;
   1630             }
   1631         }
   1632     }
   1633 
   1634     /* Android L AID Matching Platform Setting*/
   1635     if(GetNxpNumValue(NAME_AID_MATCHING_PLATFORM, (void *)&retlen, sizeof(retlen)))
   1636     {
   1637         if(1 == retlen)
   1638         {
   1639             status = phNxpNciHal_send_ext_cmd (sizeof(android_l_aid_matching_mode_on_cmd),
   1640                     android_l_aid_matching_mode_on_cmd);
   1641             if (status != NFCSTATUS_SUCCESS)
   1642             {
   1643                NXPLOG_NCIHAL_E("Android L AID Matching Platform Setting Failed");
   1644                 retry_core_init_cnt++;
   1645                 goto retry_core_init;
   1646             }
   1647         }
   1648         else if (2 == retlen)
   1649         {
   1650             android_l_aid_matching_mode_on_cmd[7]=0x00;
   1651             status = phNxpNciHal_send_ext_cmd (sizeof(android_l_aid_matching_mode_on_cmd),
   1652                     android_l_aid_matching_mode_on_cmd);
   1653             if (status != NFCSTATUS_SUCCESS)
   1654             {
   1655                 NXPLOG_NCIHAL_E("Android L AID Matching Platform Setting Failed");
   1656                 retry_core_init_cnt++;
   1657                 goto retry_core_init;
   1658             }
   1659         }
   1660     }
   1661 
   1662     if((*p_core_init_rsp_params > 0) && (*p_core_init_rsp_params < 4))
   1663     {
   1664         static phLibNfc_Message_t msg;
   1665         uint16_t tmp_len = 0;
   1666         uint8_t uicc_set_mode[] = {0x22, 0x01, 0x02, 0x02, 0x01};
   1667         uint8_t set_screen_state[] = {0x2F, 0x15, 01, 00};      //SCREEN ON
   1668         uint8_t nfcc_core_conn_create[] = {0x20, 0x04, 0x06, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01};
   1669         uint8_t nfcc_mode_set_on[] = {0x22, 0x01, 0x02, 0x01, 0x01};
   1670 
   1671         NXPLOG_NCIHAL_E("Sending DH and NFCC core connection command as raw packet!!");
   1672         status = phNxpNciHal_send_ext_cmd (sizeof(nfcc_core_conn_create), nfcc_core_conn_create);
   1673 
   1674         if (status != NFCSTATUS_SUCCESS)
   1675         {
   1676             NXPLOG_NCIHAL_E("Sending DH and NFCC core connection command as raw packet!! Failed");
   1677             retry_core_init_cnt++;
   1678             goto retry_core_init;
   1679         }
   1680 
   1681         NXPLOG_NCIHAL_E("Sending DH and NFCC mode set as raw packet!!");
   1682         status = phNxpNciHal_send_ext_cmd (sizeof(nfcc_mode_set_on), nfcc_mode_set_on);
   1683 
   1684         if (status != NFCSTATUS_SUCCESS)
   1685         {
   1686             NXPLOG_NCIHAL_E("Sending DH and NFCC mode set as raw packet!! Failed");
   1687             retry_core_init_cnt++;
   1688             goto retry_core_init;
   1689         }
   1690 
   1691         NXPLOG_NCIHAL_E("Sending UICC Select Command as raw packet!!");
   1692         status = phNxpNciHal_send_ext_cmd (sizeof(uicc_set_mode),
   1693                                       uicc_set_mode);
   1694         if (status != NFCSTATUS_SUCCESS)
   1695         {
   1696             NXPLOG_NCIHAL_E("Sending UICC Select Command as raw packet!! Failed");
   1697             retry_core_init_cnt++;
   1698             goto retry_core_init;
   1699         }
   1700 
   1701         if(*(p_core_init_rsp_params + 1) == 1) // RF state is Discovery!!
   1702         {
   1703             NXPLOG_NCIHAL_E("Sending Set Screen ON State Command as raw packet!!");
   1704             status = phNxpNciHal_send_ext_cmd (sizeof(set_screen_state),
   1705                                           set_screen_state);
   1706             if (status != NFCSTATUS_SUCCESS)
   1707             {
   1708                 NXPLOG_NCIHAL_E("Sending Set Screen ON State Command as raw packet!! Failed");
   1709                 retry_core_init_cnt++;
   1710                 goto retry_core_init;
   1711             }
   1712 
   1713             NXPLOG_NCIHAL_E("Sending discovery as raw packet!!");
   1714             status = phNxpNciHal_send_ext_cmd (p_core_init_rsp_params[2],
   1715                                                       (uint8_t *)&p_core_init_rsp_params[3]);
   1716             if (status != NFCSTATUS_SUCCESS)
   1717             {
   1718                 NXPLOG_NCIHAL_E("Sending discovery as raw packet Failed");
   1719                 retry_core_init_cnt++;
   1720                 goto retry_core_init;
   1721             }
   1722 
   1723         }
   1724         else
   1725         {
   1726             NXPLOG_NCIHAL_E("Sending Set Screen OFF State Command as raw packet!!");
   1727             set_screen_state[3] = 0x01; //Screen OFF
   1728             status = phNxpNciHal_send_ext_cmd (sizeof(set_screen_state),
   1729                                           set_screen_state);
   1730             if (status != NFCSTATUS_SUCCESS)
   1731             {
   1732                 NXPLOG_NCIHAL_E("Sending Set Screen OFF State Command as raw packet!! Failed");
   1733                 retry_core_init_cnt++;
   1734                 goto retry_core_init;
   1735             }
   1736 
   1737         }
   1738         NXPLOG_NCIHAL_E("Sending last command for Recovery ");
   1739 
   1740         if(p_core_init_rsp_params[35] > 0)
   1741         {  //if length of last command is 0 then it doesn't need to send last command.
   1742             if( !(((p_core_init_rsp_params[36] == 0x21) && (p_core_init_rsp_params[37] == 0x03))
   1743                 && (*(p_core_init_rsp_params + 1) == 1)) &&
   1744                     !((p_core_init_rsp_params[36] == 0x21) && (p_core_init_rsp_params[37] == 0x06) && (p_core_init_rsp_params[39] == 0x00) &&(*(p_core_init_rsp_params + 1) == 0x00)))
   1745                 //if last command is discovery and RF status is also discovery state, then it doesn't need to execute or similarly
   1746                 // if the last command is deactivate to idle and RF status is also idle , no need to execute the command .
   1747             {
   1748                 tmp_len = p_core_init_rsp_params[35];
   1749 
   1750                 /* Check for NXP ext before sending write */
   1751                 status = phNxpNciHal_write_ext(&tmp_len,
   1752                         (uint8_t *)&p_core_init_rsp_params[36], &nxpncihal_ctrl.rsp_len,
   1753                         nxpncihal_ctrl.p_rsp_data);
   1754                 if (status != NFCSTATUS_SUCCESS)
   1755                 {
   1756                     if (buffer)
   1757                     {
   1758                         free (buffer);
   1759                         buffer = NULL;
   1760                     }
   1761                     /* Do not send packet to PN54X, send response directly */
   1762                     msg.eMsgType = NCI_HAL_RX_MSG;
   1763                     msg.pMsgData = NULL;
   1764                     msg.Size = 0;
   1765 
   1766                     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
   1767                             (phLibNfc_Message_t *) &msg);
   1768                     return NFCSTATUS_SUCCESS;
   1769                 }
   1770 
   1771                 p_core_init_rsp_params[35] = (uint8_t)tmp_len;
   1772 
   1773                 status = phNxpNciHal_send_ext_cmd (p_core_init_rsp_params[35],
   1774                                                           (uint8_t *)&p_core_init_rsp_params[36]);
   1775                 if (status != NFCSTATUS_SUCCESS)
   1776                 {
   1777                     NXPLOG_NCIHAL_E("Sending last command for Recovery Failed");
   1778                     retry_core_init_cnt++;
   1779                     goto retry_core_init;
   1780                 }
   1781             }
   1782         }
   1783     }
   1784 
   1785     retry_core_init_cnt = 0;
   1786 
   1787     if(buffer)
   1788     {
   1789         free(buffer);
   1790         buffer = NULL;
   1791     }
   1792 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1793     //initialize dummy FW recovery variables
   1794     gRecFWDwnld = 0;
   1795     gRecFwRetryCount = 0;
   1796 #endif
   1797     if(!((*p_core_init_rsp_params > 0) && (*p_core_init_rsp_params < 4)))
   1798         phNxpNciHal_core_initialized_complete(status);
   1799     else
   1800     {
   1801 invoke_callback:
   1802         config_access = FALSE;
   1803         if (nxpncihal_ctrl.p_nfc_stack_data_cback != NULL)
   1804         {
   1805             *p_core_init_rsp_params = 0;
   1806             NXPLOG_NCIHAL_E("Invoking data callback!!");
   1807             (*nxpncihal_ctrl.p_nfc_stack_data_cback)(
   1808                     nxpncihal_ctrl.rx_data_len, nxpncihal_ctrl.p_rx_data);
   1809         }
   1810     }
   1811 
   1812 #ifdef PN547C2_CLOCK_SETTING
   1813     if (isNxpConfigModified())
   1814     {
   1815         updateNxpConfigTimestamp();
   1816     }
   1817 #endif
   1818     return NFCSTATUS_SUCCESS;
   1819 }
   1820 #if(NFC_NXP_CHIP_TYPE != PN547C2)
   1821 /******************************************************************************
   1822  * Function         phNxpNciHal_CheckRFCmdRespStatus
   1823  *
   1824  * Description      This function is called to check the resp status of
   1825  *                  RF update commands.
   1826  *
   1827  * Returns          NFCSTATUS_SUCCESS           if successful,
   1828  *                  NFCSTATUS_INVALID_PARAMETER if parameter is inavlid
   1829  *                  NFCSTATUS_FAILED            if failed response
   1830  *
   1831  ******************************************************************************/
   1832 NFCSTATUS phNxpNciHal_CheckRFCmdRespStatus ()
   1833 {
   1834     NFCSTATUS status = NFCSTATUS_SUCCESS;
   1835     static uint16_t INVALID_PARAM = 0x09;
   1836     if ((nxpncihal_ctrl.rx_data_len > 0) && (nxpncihal_ctrl.p_rx_data[2] > 0))
   1837     {
   1838         if (nxpncihal_ctrl.p_rx_data[3] == 0x09)
   1839         {
   1840             status = INVALID_PARAM;
   1841         }
   1842         else if (nxpncihal_ctrl.p_rx_data[3] != NFCSTATUS_SUCCESS)
   1843         {
   1844             status = NFCSTATUS_FAILED;
   1845         }
   1846     }
   1847     return status;
   1848 }
   1849 /******************************************************************************
   1850  * Function         phNxpNciHalRFConfigCmdRecSequence
   1851  *
   1852  * Description      This function is called to handle dummy FW recovery sequence
   1853  *                  Whenever RF settings are failed to apply with invalid param
   1854  *                  response, recovery mechanism includes dummy firmware download
   1855  *                  followed by firmware download and then config settings. The dummy
   1856  *                  firmware changes the major number of the firmware inside NFCC.
   1857  *                  Then actual firmware dowenload will be successful. This can be
   1858  *                  retried maximum three times.
   1859  *
   1860  * Returns          Always returns NFCSTATUS_SUCCESS
   1861  *
   1862  ******************************************************************************/
   1863 NFCSTATUS phNxpNciHalRFConfigCmdRecSequence ()
   1864 {
   1865     NFCSTATUS status = NFCSTATUS_SUCCESS;
   1866     uint16_t recFWState = 1;
   1867     gRecFWDwnld = TRUE;
   1868     gRecFwRetryCount++;
   1869     if (gRecFwRetryCount > 0x03)
   1870     {
   1871         NXPLOG_NCIHAL_D ("Max retry count for RF config FW recovery exceeded ");
   1872         gRecFWDwnld = FALSE;
   1873         return NFCSTATUS_FAILED;
   1874     }
   1875     do {
   1876         status = phTmlNfc_IoCtl (phTmlNfc_e_ResetDevice);
   1877         phDnldNfc_InitImgInfo ();
   1878         if (NFCSTATUS_SUCCESS == phNxpNciHal_CheckValidFwVersion ())
   1879         {
   1880             fw_download_success = 0;
   1881             status = phNxpNciHal_fw_download ();
   1882             if (status == NFCSTATUS_SUCCESS)
   1883             {
   1884                 fw_download_success = 1;
   1885                 status = phTmlNfc_Read(
   1886                     nxpncihal_ctrl.p_cmd_data,
   1887                     NCI_MAX_DATA_LEN,
   1888                     (pphTmlNfc_TransactCompletionCb_t) &phNxpNciHal_read_complete,
   1889                     NULL);
   1890                 if (status != NFCSTATUS_PENDING)
   1891                 {
   1892                     NXPLOG_NCIHAL_E ("TML Read status error status = %x", status);
   1893                     phTmlNfc_Shutdown ();
   1894                     status = NFCSTATUS_FAILED;
   1895                     break;
   1896                 }
   1897             }
   1898             else
   1899             {
   1900                 status = NFCSTATUS_FAILED;
   1901                 break;
   1902             }
   1903         }
   1904         gRecFWDwnld = FALSE;
   1905     }while (recFWState--);
   1906     gRecFWDwnld = FALSE;
   1907     return status;
   1908 }
   1909 #endif
   1910 /******************************************************************************
   1911  * Function         phNxpNciHal_core_initialized_complete
   1912  *
   1913  * Description      This function is called when phNxpNciHal_core_initialized
   1914  *                  complete all proprietary command exchanges. This function
   1915  *                  informs libnfc-nci about completion of core initialize
   1916  *                  and result of that through callback.
   1917  *
   1918  * Returns          void.
   1919  *
   1920  ******************************************************************************/
   1921 static void phNxpNciHal_core_initialized_complete(NFCSTATUS status)
   1922 {
   1923     static phLibNfc_Message_t msg;
   1924 
   1925     if (status == NFCSTATUS_SUCCESS)
   1926     {
   1927         msg.eMsgType = NCI_HAL_POST_INIT_CPLT_MSG;
   1928     }
   1929     else
   1930     {
   1931         msg.eMsgType = NCI_HAL_ERROR_MSG;
   1932     }
   1933     msg.pMsgData = NULL;
   1934     msg.Size = 0;
   1935 
   1936     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
   1937             (phLibNfc_Message_t *) &msg);
   1938 
   1939     return;
   1940 }
   1941 
   1942 /******************************************************************************
   1943  * Function         phNxpNciHal_pre_discover
   1944  *
   1945  * Description      This function is called by libnfc-nci to perform any
   1946  *                  proprietary exchange before RF discovery. When proprietary
   1947  *                  exchange is over completion is informed to libnfc-nci
   1948  *                  through phNxpNciHal_pre_discover_complete function.
   1949  *
   1950  * Returns          It always returns NFCSTATUS_SUCCESS (0).
   1951  *
   1952  ******************************************************************************/
   1953 int phNxpNciHal_pre_discover(void)
   1954 {
   1955     /* Nothing to do here for initial version */
   1956     return NFCSTATUS_SUCCESS;
   1957 }
   1958 
   1959 /******************************************************************************
   1960  * Function         phNxpNciHal_pre_discover_complete
   1961  *
   1962  * Description      This function informs libnfc-nci about completion and
   1963  *                  status of phNxpNciHal_pre_discover through callback.
   1964  *
   1965  * Returns          void.
   1966  *
   1967  ******************************************************************************/
   1968 static void phNxpNciHal_pre_discover_complete(NFCSTATUS status)
   1969 {
   1970     static phLibNfc_Message_t msg;
   1971 
   1972     if (status == NFCSTATUS_SUCCESS)
   1973     {
   1974         msg.eMsgType = NCI_HAL_PRE_DISCOVER_CPLT_MSG;
   1975     }
   1976     else
   1977     {
   1978         msg.eMsgType = NCI_HAL_ERROR_MSG;
   1979     }
   1980     msg.pMsgData = NULL;
   1981     msg.Size = 0;
   1982 
   1983     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
   1984             &msg);
   1985 
   1986     return;
   1987 }
   1988 
   1989 /******************************************************************************
   1990  * Function         phNxpNciHal_close
   1991  *
   1992  * Description      This function close the NFCC interface and free all
   1993  *                  resources.This is called by libnfc-nci on NFC service stop.
   1994  *
   1995  * Returns          Always return NFCSTATUS_SUCCESS (0).
   1996  *
   1997  ******************************************************************************/
   1998 int phNxpNciHal_close(void)
   1999 {
   2000     NFCSTATUS status;
   2001     /*NCI_RESET_CMD*/
   2002     static uint8_t cmd_reset_nci[] = {0x20,0x00,0x01,0x00};
   2003 
   2004     static uint8_t cmd_ce_disc_nci[] = {0x21,0x03,0x07,0x03,0x80,0x01,0x81,0x01,0x82,0x01};
   2005 
   2006     CONCURRENCY_LOCK();
   2007 
   2008     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_ce_disc_nci),cmd_ce_disc_nci);
   2009     if(status != NFCSTATUS_SUCCESS)
   2010     {
   2011         NXPLOG_NCIHAL_E ("CMD_CE_DISC_NCI: Failed");
   2012     }
   2013 
   2014     nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
   2015 
   2016     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci),cmd_reset_nci);
   2017     if(status != NFCSTATUS_SUCCESS)
   2018     {
   2019         NXPLOG_NCIHAL_E ("NCI_CORE_RESET: Failed");
   2020     }
   2021 
   2022     if (NULL != gpphTmlNfc_Context->pDevHandle)
   2023     {
   2024         phNxpNciHal_close_complete(NFCSTATUS_SUCCESS);
   2025         /* Abort any pending read and write */
   2026         status = phTmlNfc_ReadAbort();
   2027         status = phTmlNfc_WriteAbort();
   2028 
   2029         phOsalNfc_Timer_Cleanup();
   2030 
   2031         status = phTmlNfc_Shutdown();
   2032 
   2033         phDal4Nfc_msgrelease(nxpncihal_ctrl.gDrvCfg.nClientId);
   2034 
   2035 
   2036         memset (&nxpncihal_ctrl, 0x00, sizeof (nxpncihal_ctrl));
   2037 
   2038         NXPLOG_NCIHAL_D("phNxpNciHal_close - phOsalNfc_DeInit completed");
   2039     }
   2040 
   2041     CONCURRENCY_UNLOCK();
   2042 
   2043     phNxpNciHal_cleanup_monitor();
   2044 
   2045     /* Return success always */
   2046     return NFCSTATUS_SUCCESS;
   2047 }
   2048 
   2049 /******************************************************************************
   2050  * Function         phNxpNciHal_close_complete
   2051  *
   2052  * Description      This function inform libnfc-nci about result of
   2053  *                  phNxpNciHal_close.
   2054  *
   2055  * Returns          void.
   2056  *
   2057  ******************************************************************************/
   2058 void phNxpNciHal_close_complete(NFCSTATUS status)
   2059 {
   2060     static phLibNfc_Message_t msg;
   2061 
   2062     if (status == NFCSTATUS_SUCCESS)
   2063     {
   2064         msg.eMsgType = NCI_HAL_CLOSE_CPLT_MSG;
   2065     }
   2066     else
   2067     {
   2068         msg.eMsgType = NCI_HAL_ERROR_MSG;
   2069     }
   2070     msg.pMsgData = NULL;
   2071     msg.Size = 0;
   2072 
   2073     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
   2074             &msg);
   2075 
   2076     return;
   2077 }
   2078 /******************************************************************************
   2079  * Function         phNxpNciHal_notify_i2c_fragmentation
   2080  *
   2081  * Description      This function can be used by HAL to inform
   2082  *                 libnfc-nci that i2c fragmentation is enabled/disabled
   2083  *
   2084  * Returns          void.
   2085  *
   2086  ******************************************************************************/
   2087 void phNxpNciHal_notify_i2c_fragmentation(void)
   2088 {
   2089     if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
   2090     {
   2091         /*inform libnfc-nci that i2c fragmentation is enabled/disabled */
   2092         (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT,
   2093                 HAL_NFC_STATUS_OK);
   2094     }
   2095 }
   2096 /******************************************************************************
   2097  * Function         phNxpNciHal_control_granted
   2098  *
   2099  * Description      Called by libnfc-nci when NFCC control is granted to HAL.
   2100  *
   2101  * Returns          Always returns NFCSTATUS_SUCCESS (0).
   2102  *
   2103  ******************************************************************************/
   2104 int phNxpNciHal_control_granted(void)
   2105 {
   2106     /* Take the concurrency lock so no other calls from upper layer
   2107      * will be allowed
   2108      */
   2109     CONCURRENCY_LOCK();
   2110 
   2111     if(NULL != nxpncihal_ctrl.p_control_granted_cback)
   2112     {
   2113         (*nxpncihal_ctrl.p_control_granted_cback)();
   2114     }
   2115     /* At the end concurrency unlock so calls from upper layer will
   2116      * be allowed
   2117      */
   2118     CONCURRENCY_UNLOCK();
   2119     return NFCSTATUS_SUCCESS;
   2120 }
   2121 
   2122 /******************************************************************************
   2123  * Function         phNxpNciHal_request_control
   2124  *
   2125  * Description      This function can be used by HAL to request control of
   2126  *                  NFCC to libnfc-nci. When control is provided to HAL it is
   2127  *                  notified through phNxpNciHal_control_granted.
   2128  *
   2129  * Returns          void.
   2130  *
   2131  ******************************************************************************/
   2132 void phNxpNciHal_request_control(void)
   2133 {
   2134     if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
   2135     {
   2136         /* Request Control of NCI Controller from NCI NFC Stack */
   2137         (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_REQUEST_CONTROL_EVT,
   2138                 HAL_NFC_STATUS_OK);
   2139     }
   2140 
   2141     return;
   2142 }
   2143 
   2144 /******************************************************************************
   2145  * Function         phNxpNciHal_release_control
   2146  *
   2147  * Description      This function can be used by HAL to release the control of
   2148  *                  NFCC back to libnfc-nci.
   2149  *
   2150  * Returns          void.
   2151  *
   2152  ******************************************************************************/
   2153 void phNxpNciHal_release_control(void)
   2154 {
   2155     if (nxpncihal_ctrl.p_nfc_stack_cback != NULL)
   2156     {
   2157         /* Release Control of NCI Controller to NCI NFC Stack */
   2158         (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_RELEASE_CONTROL_EVT,
   2159                 HAL_NFC_STATUS_OK);
   2160     }
   2161 
   2162     return;
   2163 }
   2164 
   2165 /******************************************************************************
   2166  * Function         phNxpNciHal_power_cycle
   2167  *
   2168  * Description      This function is called by libnfc-nci when power cycling is
   2169  *                  performed. When processing is complete it is notified to
   2170  *                  libnfc-nci through phNxpNciHal_power_cycle_complete.
   2171  *
   2172  * Returns          Always return NFCSTATUS_SUCCESS (0).
   2173  *
   2174  ******************************************************************************/
   2175 int phNxpNciHal_power_cycle(void)
   2176 {
   2177     NXPLOG_NCIHAL_D("Power Cycle");
   2178 
   2179     NFCSTATUS status = NFCSTATUS_FAILED;
   2180 
   2181     status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
   2182 
   2183     if(NFCSTATUS_SUCCESS == status)
   2184     {
   2185         NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n");
   2186     }
   2187     else
   2188     {
   2189         NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
   2190     }
   2191 
   2192     phNxpNciHal_power_cycle_complete(NFCSTATUS_SUCCESS);
   2193 
   2194     return NFCSTATUS_SUCCESS;
   2195 }
   2196 
   2197 /******************************************************************************
   2198  * Function         phNxpNciHal_power_cycle_complete
   2199  *
   2200  * Description      This function is called to provide the status of
   2201  *                  phNxpNciHal_power_cycle to libnfc-nci through callback.
   2202  *
   2203  * Returns          void.
   2204  *
   2205  ******************************************************************************/
   2206 static void phNxpNciHal_power_cycle_complete(NFCSTATUS status)
   2207 {
   2208     static phLibNfc_Message_t msg;
   2209 
   2210     if (status == NFCSTATUS_SUCCESS)
   2211     {
   2212         msg.eMsgType = NCI_HAL_OPEN_CPLT_MSG;
   2213     }
   2214     else
   2215     {
   2216         msg.eMsgType = NCI_HAL_ERROR_MSG;
   2217     }
   2218     msg.pMsgData = NULL;
   2219     msg.Size = 0;
   2220 
   2221     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
   2222             &msg);
   2223 
   2224     return;
   2225 }
   2226 
   2227 /******************************************************************************
   2228  * Function         phNxpNciHal_get_mw_eeprom
   2229  *
   2230  * Description      This function is called to retreive data in mw eeprom area
   2231  *
   2232  * Returns          NFCSTATUS.
   2233  *
   2234  ******************************************************************************/
   2235 static NFCSTATUS phNxpNciHal_get_mw_eeprom (void)
   2236 {
   2237     NFCSTATUS status = NFCSTATUS_SUCCESS;
   2238     uint8_t retry_cnt = 0;
   2239     static uint8_t get_mw_eeprom_cmd[] = { 0x20, 0x03,0x03, 0x01, 0xA0, 0x0F };
   2240     uint8_t bConfig;
   2241 
   2242 retry_send_ext:
   2243     if (retry_cnt > 3)
   2244     {
   2245         return NFCSTATUS_FAILED;
   2246     }
   2247 
   2248     phNxpNciMwEepromArea.isGetEepromArea = TRUE;
   2249     status = phNxpNciHal_send_ext_cmd (sizeof(get_mw_eeprom_cmd), get_mw_eeprom_cmd);
   2250     if (status != NFCSTATUS_SUCCESS)
   2251     {
   2252         NXPLOG_NCIHAL_E ("unable to get the mw eeprom data");
   2253         phNxpNciMwEepromArea.isGetEepromArea = FALSE;
   2254         retry_cnt++;
   2255         goto retry_send_ext;
   2256     }
   2257     phNxpNciMwEepromArea.isGetEepromArea = FALSE;
   2258 
   2259     if (phNxpNciMwEepromArea.p_rx_data[12])
   2260     {
   2261         fw_download_success = 1;
   2262     }
   2263     return status;
   2264 }
   2265 
   2266 /******************************************************************************
   2267  * Function         phNxpNciHal_set_mw_eeprom
   2268  *
   2269  * Description      This function is called to update data in mw eeprom area
   2270  *
   2271  * Returns          void.
   2272  *
   2273  ******************************************************************************/
   2274 static NFCSTATUS phNxpNciHal_set_mw_eeprom (void)
   2275 {
   2276     NFCSTATUS status = NFCSTATUS_SUCCESS;
   2277     uint8_t retry_cnt = 0;
   2278     uint8_t set_mw_eeprom_cmd[39] = {0};
   2279     uint8_t cmd_header[] = { 0x20, 0x02,0x24, 0x01, 0xA0, 0x0F, 0x20 };
   2280 
   2281     memcpy (set_mw_eeprom_cmd, cmd_header, sizeof(cmd_header));
   2282     phNxpNciMwEepromArea.p_rx_data[12] = 0;
   2283     memcpy (set_mw_eeprom_cmd + sizeof(cmd_header), phNxpNciMwEepromArea.p_rx_data, sizeof(phNxpNciMwEepromArea.p_rx_data));
   2284 
   2285 retry_send_ext:
   2286     if (retry_cnt > 3)
   2287     {
   2288         return NFCSTATUS_FAILED;
   2289     }
   2290 
   2291     status = phNxpNciHal_send_ext_cmd (sizeof(set_mw_eeprom_cmd), set_mw_eeprom_cmd);
   2292     if (status != NFCSTATUS_SUCCESS)
   2293     {
   2294         NXPLOG_NCIHAL_E ("unable to update the mw eeprom data");
   2295         retry_cnt++;
   2296         goto retry_send_ext;
   2297     }
   2298     return status;
   2299 }
   2300 
   2301 /******************************************************************************
   2302  * Function         phNxpNciHal_set_clock
   2303  *
   2304  * Description      This function is called after successfull download
   2305  *                  to apply the clock setting provided in config file
   2306  *
   2307  * Returns          void.
   2308  *
   2309  ******************************************************************************/
   2310 static void phNxpNciHal_set_clock(void)
   2311 {
   2312     NFCSTATUS status = NFCSTATUS_FAILED;
   2313     int retryCount = 0;
   2314 
   2315 retrySetclock:
   2316     phNxpNciClock.isClockSet = TRUE;
   2317     if (nxpprofile_ctrl.bClkSrcVal == CLK_SRC_PLL)
   2318     {
   2319         static uint8_t set_clock_cmd[] = {0x20, 0x02,0x09, 0x02, 0xA0, 0x03, 0x01, 0x11,
   2320                                                                0xA0, 0x04, 0x01, 0x01};
   2321         uint8_t param_clock_src = CLK_SRC_PLL;
   2322         param_clock_src = param_clock_src << 3;
   2323 
   2324         if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_13MHZ)
   2325         {
   2326             param_clock_src |= 0x00;
   2327         }
   2328         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_19_2MHZ)
   2329         {
   2330             param_clock_src |= 0x01;
   2331         }
   2332         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_24MHZ)
   2333         {
   2334             param_clock_src |= 0x02;
   2335         }
   2336         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_26MHZ)
   2337         {
   2338             param_clock_src |= 0x03;
   2339         }
   2340         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_38_4MHZ)
   2341         {
   2342             param_clock_src |= 0x04;
   2343         }
   2344         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_52MHZ)
   2345         {
   2346             param_clock_src |= 0x05;
   2347         }
   2348         else
   2349         {
   2350             NXPLOG_NCIHAL_E("Wrong clock freq, send default PLL (at) 19.2MHz");
   2351             param_clock_src = 0x11;
   2352         }
   2353 
   2354         set_clock_cmd[7] = param_clock_src;
   2355         set_clock_cmd[11] = nxpprofile_ctrl.bTimeout;
   2356         status = phNxpNciHal_send_ext_cmd(sizeof(set_clock_cmd), set_clock_cmd);
   2357         if (status != NFCSTATUS_SUCCESS)
   2358         {
   2359             NXPLOG_NCIHAL_E("PLL colck setting failed !!");
   2360         }
   2361     }
   2362     else if(nxpprofile_ctrl.bClkSrcVal == CLK_SRC_XTAL)
   2363     {
   2364         static uint8_t set_clock_cmd[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x03, 0x01, 0x08};
   2365         status = phNxpNciHal_send_ext_cmd(sizeof(set_clock_cmd), set_clock_cmd);
   2366         if (status != NFCSTATUS_SUCCESS)
   2367         {
   2368             NXPLOG_NCIHAL_E("XTAL colck setting failed !!");
   2369         }
   2370     }
   2371     else
   2372     {
   2373         NXPLOG_NCIHAL_E("Wrong clock source. Dont apply any modification")
   2374     }
   2375 
   2376    // Checking for SET CONFG SUCCESS, re-send the command  if not.
   2377     phNxpNciClock.isClockSet = FALSE;
   2378     if(phNxpNciClock.p_rx_data[3]   != NFCSTATUS_SUCCESS )
   2379     {
   2380         if(retryCount++  < 3)
   2381         {
   2382             NXPLOG_NCIHAL_E("Set-clk failed retry again ");
   2383             goto retrySetclock;
   2384         }
   2385         else
   2386         {
   2387             NXPLOG_NCIHAL_D("Set clk  failed -  max count = 0x%x exceeded ", retryCount);
   2388 //            NXPLOG_NCIHAL_E("Set Config is failed for Clock Due to elctrical disturbances, aborting the NFC process");
   2389 //            abort ();
   2390        }
   2391     }
   2392 }
   2393 
   2394 /******************************************************************************
   2395  * Function         phNxpNciHal_check_clock_config
   2396  *
   2397  * Description      This function is called after successfull download
   2398  *                  to check if clock settings in config file and chip
   2399  *                  is same
   2400  *
   2401  * Returns          void.
   2402  *
   2403  ******************************************************************************/
   2404 NFCSTATUS phNxpNciHal_check_clock_config(void)
   2405 {
   2406     NFCSTATUS status = NFCSTATUS_SUCCESS;
   2407     uint8_t param_clock_src;
   2408     static uint8_t get_clock_cmd[] = {0x20, 0x03,0x07, 0x03, 0xA0, 0x02,
   2409             0xA0, 0x03, 0xA0, 0x04};
   2410     phNxpNciClock.isClockSet = TRUE;
   2411     phNxpNciHal_get_clk_freq();
   2412     status = phNxpNciHal_send_ext_cmd(sizeof(get_clock_cmd),get_clock_cmd);
   2413 
   2414     if(status != NFCSTATUS_SUCCESS)
   2415     {
   2416         NXPLOG_NCIHAL_E("unable to retrieve get_clk_src_sel");
   2417         return status;
   2418     }
   2419     param_clock_src = check_config_parameter();
   2420     if( phNxpNciClock.p_rx_data[12] == param_clock_src &&  phNxpNciClock.p_rx_data[16] == nxpprofile_ctrl.bTimeout)
   2421     {
   2422         phNxpNciClock.issetConfig = FALSE;
   2423     }else {
   2424         phNxpNciClock.issetConfig = TRUE;
   2425     }
   2426     phNxpNciClock.isClockSet = FALSE;
   2427 
   2428     return status;
   2429 }
   2430 
   2431 /******************************************************************************
   2432  * Function         phNxpNciHal_china_tianjin_rf_setting
   2433  *
   2434  * Description      This function is called to check RF Setting
   2435  *
   2436  * Returns          Status.
   2437  *
   2438  ******************************************************************************/
   2439 NFCSTATUS phNxpNciHal_china_tianjin_rf_setting(void)
   2440 {
   2441     NFCSTATUS status = NFCSTATUS_SUCCESS;
   2442     int isfound = 0;
   2443     int rf_enable = FALSE;
   2444     int rf_val = 0;
   2445     int send_flag;
   2446     uint8_t retry_cnt =0;
   2447     int enable_bit =0;
   2448     static uint8_t get_rf_cmd[] = {0x20, 0x03,0x03, 0x01, 0xA0, 0x85};
   2449 
   2450 retry_send_ext:
   2451     if(retry_cnt > 3)
   2452     {
   2453         return NFCSTATUS_FAILED;
   2454     }
   2455     send_flag = TRUE;
   2456     phNxpNciRfSet.isGetRfSetting = TRUE;
   2457     status = phNxpNciHal_send_ext_cmd(sizeof(get_rf_cmd),get_rf_cmd);
   2458     if(status != NFCSTATUS_SUCCESS)
   2459     {
   2460         NXPLOG_NCIHAL_E("unable to get the RF setting");
   2461         phNxpNciRfSet.isGetRfSetting = FALSE;
   2462         retry_cnt++;
   2463         goto retry_send_ext;
   2464     }
   2465     phNxpNciRfSet.isGetRfSetting = FALSE;
   2466     if(phNxpNciRfSet.p_rx_data[3] != 0x00)
   2467     {
   2468         NXPLOG_NCIHAL_E("GET_CONFIG_RSP is FAILED for CHINA TIANJIN");
   2469         return status;
   2470     }
   2471     rf_val = phNxpNciRfSet.p_rx_data[10];
   2472     isfound = (GetNxpNumValue(NAME_NXP_CHINA_TIANJIN_RF_ENABLED, (void *)&rf_enable, sizeof(rf_enable)));
   2473     if(isfound >0)
   2474     {
   2475         enable_bit = rf_val & 0x40;
   2476         if((enable_bit != 0x40) && (rf_enable == 1))
   2477         {
   2478             phNxpNciRfSet.p_rx_data[10] |= 0x40;   // Enable if it is disabled
   2479         }
   2480         else if((enable_bit == 0x40) && (rf_enable == 0))
   2481         {
   2482             phNxpNciRfSet.p_rx_data[10] &= 0xBF;  // Disable if it is Enabled
   2483         }
   2484         else
   2485         {
   2486             send_flag = FALSE;  // No need to change in RF setting
   2487         }
   2488 
   2489         if(send_flag == TRUE)
   2490         {
   2491             static uint8_t set_rf_cmd[] = {0x20, 0x02, 0x08, 0x01, 0xA0, 0x85, 0x04, 0x50, 0x08, 0x68, 0x00};
   2492             memcpy(&set_rf_cmd[4],&phNxpNciRfSet.p_rx_data[5],7);
   2493             status = phNxpNciHal_send_ext_cmd(sizeof(set_rf_cmd),set_rf_cmd);
   2494             if(status != NFCSTATUS_SUCCESS)
   2495             {
   2496                 NXPLOG_NCIHAL_E("unable to set the RF setting");
   2497                 retry_cnt++;
   2498                 goto retry_send_ext;
   2499             }
   2500         }
   2501     }
   2502 
   2503     return status;
   2504 }
   2505 
   2506 int  check_config_parameter()
   2507 {
   2508     NFCSTATUS status = NFCSTATUS_FAILED;
   2509     uint8_t param_clock_src = CLK_SRC_PLL;
   2510     if (nxpprofile_ctrl.bClkSrcVal == CLK_SRC_PLL)
   2511     {
   2512         param_clock_src = param_clock_src << 3;
   2513 
   2514         if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_13MHZ)
   2515         {
   2516             param_clock_src |= 0x00;
   2517         }
   2518         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_19_2MHZ)
   2519         {
   2520             param_clock_src |= 0x01;
   2521         }
   2522         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_24MHZ)
   2523         {
   2524             param_clock_src |= 0x02;
   2525         }
   2526         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_26MHZ)
   2527         {
   2528             param_clock_src |= 0x03;
   2529         }
   2530         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_38_4MHZ)
   2531         {
   2532             param_clock_src |= 0x04;
   2533         }
   2534         else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_52MHZ)
   2535         {
   2536             param_clock_src |= 0x05;
   2537         }
   2538         else
   2539         {
   2540             NXPLOG_NCIHAL_E("Wrong clock freq, send default PLL (at) 19.2MHz");
   2541             param_clock_src = 0x11;
   2542         }
   2543     }
   2544     else if(nxpprofile_ctrl.bClkSrcVal == CLK_SRC_XTAL)
   2545     {
   2546         param_clock_src = 0x08;
   2547 
   2548     }
   2549     else
   2550     {
   2551         NXPLOG_NCIHAL_E("Wrong clock source. Dont apply any modification")
   2552     }
   2553     return param_clock_src;
   2554 }
   2555 /******************************************************************************
   2556  * Function         phNxpNciHal_enable_i2c_fragmentation
   2557  *
   2558  * Description      This function is called to process the response status
   2559  *                  and print the status byte.
   2560  *
   2561  * Returns          void.
   2562  *
   2563  ******************************************************************************/
   2564 void phNxpNciHal_enable_i2c_fragmentation()
   2565 {
   2566     NFCSTATUS status = NFCSTATUS_FAILED;
   2567     static uint8_t fragmentation_enable_config_cmd[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x05, 0x01, 0x10};
   2568     int isfound = 0;
   2569     long i2c_status = 0x00;
   2570     long config_i2c_vlaue = 0xff;
   2571     /*NCI_RESET_CMD*/
   2572     static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
   2573     /*NCI_INIT_CMD*/
   2574     static uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
   2575     static uint8_t get_i2c_fragmentation_cmd[] = {0x20, 0x03, 0x03, 0x01 ,0xA0 ,0x05};
   2576     isfound = (GetNxpNumValue(NAME_NXP_I2C_FRAGMENTATION_ENABLED, (void *)&i2c_status, sizeof(i2c_status)));
   2577     status = phNxpNciHal_send_ext_cmd(sizeof(get_i2c_fragmentation_cmd),get_i2c_fragmentation_cmd);
   2578     if(status != NFCSTATUS_SUCCESS)
   2579     {
   2580         NXPLOG_NCIHAL_E("unable to retrieve  get_i2c_fragmentation_cmd");
   2581     }
   2582     else
   2583     {
   2584         if(nxpncihal_ctrl.p_rx_data[8] == 0x10)
   2585         {
   2586             config_i2c_vlaue = 0x01;
   2587             phNxpNciHal_notify_i2c_fragmentation();
   2588             phTmlNfc_set_fragmentation_enabled(I2C_FRAGMENTATION_ENABLED);
   2589         }
   2590         else if(nxpncihal_ctrl.p_rx_data[8] == 0x00)
   2591         {
   2592             config_i2c_vlaue = 0x00;
   2593         }
   2594         if( config_i2c_vlaue == i2c_status)
   2595         {
   2596             NXPLOG_NCIHAL_E("i2c_fragmentation_status existing");
   2597         }
   2598         else
   2599         {
   2600             if (i2c_status == 0x01)
   2601             {
   2602                 /* NXP I2C fragmenation enabled*/
   2603                 status = phNxpNciHal_send_ext_cmd(sizeof(fragmentation_enable_config_cmd), fragmentation_enable_config_cmd);
   2604                 if (status != NFCSTATUS_SUCCESS)
   2605                 {
   2606                     NXPLOG_NCIHAL_E("NXP fragmentation enable failed");
   2607                 }
   2608             }
   2609             else if (i2c_status == 0x00 || config_i2c_vlaue == 0xff)
   2610             {
   2611                 fragmentation_enable_config_cmd[7] = 0x00;
   2612                 /* NXP I2C fragmentation disabled*/
   2613                 status = phNxpNciHal_send_ext_cmd(sizeof(fragmentation_enable_config_cmd), fragmentation_enable_config_cmd);
   2614                 if (status != NFCSTATUS_SUCCESS)
   2615                 {
   2616                     NXPLOG_NCIHAL_E("NXP fragmentation disable failed");
   2617                 }
   2618             }
   2619             status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci),cmd_reset_nci);
   2620             if(status != NFCSTATUS_SUCCESS)
   2621             {
   2622                 NXPLOG_NCIHAL_E ("NCI_CORE_RESET: Failed");
   2623             }
   2624             status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci),cmd_init_nci);
   2625             if(status != NFCSTATUS_SUCCESS)
   2626             {
   2627                 NXPLOG_NCIHAL_E ("NCI_CORE_INIT : Failed");
   2628             }
   2629             else if(i2c_status == 0x01)
   2630             {
   2631                 phNxpNciHal_notify_i2c_fragmentation();
   2632                 phTmlNfc_set_fragmentation_enabled(I2C_FRAGMENTATION_ENABLED);
   2633             }
   2634         }
   2635     }
   2636 }
   2637 /******************************************************************************
   2638  * Function         phNxpNciHal_check_factory_reset
   2639  *
   2640  * Description      This function is called at init time to check
   2641  *                  the presence of ese related info. If file are not
   2642  *                  present set the SWP_INT_SESSION_ID_CFG to FF to
   2643  *                  force the NFCEE to re-run its initialization sequence.
   2644  *
   2645  * Returns          void.
   2646  *
   2647  ******************************************************************************/
   2648 static void phNxpNciHal_check_factory_reset(void)
   2649 {
   2650     struct stat st;
   2651     int ret = 0;
   2652     NFCSTATUS status = NFCSTATUS_FAILED;
   2653     const char config_eseinfo_path[] = "/data/nfc/nfaStorage.bin1";
   2654     static uint8_t reset_ese_session_identity_set[] = { 0x20, 0x02, 0x17, 0x02,
   2655                                       0xA0, 0xEA, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   2656                                       0xA0, 0xEB, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
   2657 #ifdef PN547C2_FACTORY_RESET_DEBUG
   2658     static uint8_t reset_ese_session_identity[] = { 0x20, 0x03, 0x05, 0x02,
   2659                                           0xA0, 0xEA, 0xA0, 0xEB};
   2660 #endif
   2661     if (stat(config_eseinfo_path, &st) == -1)
   2662     {
   2663         NXPLOG_NCIHAL_D("%s file not present = %s", __FUNCTION__, config_eseinfo_path);
   2664         ret = -1;
   2665     }
   2666     else
   2667     {
   2668         ret = 0;
   2669     }
   2670 
   2671     if(ret == -1)
   2672     {
   2673 #ifdef PN547C2_FACTORY_RESET_DEBUG
   2674         /* NXP ACT Proprietary Ext */
   2675         status = phNxpNciHal_send_ext_cmd(sizeof(reset_ese_session_identity),
   2676                                            reset_ese_session_identity);
   2677         if (status != NFCSTATUS_SUCCESS) {
   2678             NXPLOG_NCIHAL_E("NXP reset_ese_session_identity command failed");
   2679         }
   2680 #endif
   2681         status = phNxpNciHal_send_ext_cmd(sizeof(reset_ese_session_identity_set),
   2682                                            reset_ese_session_identity_set);
   2683         if (status != NFCSTATUS_SUCCESS) {
   2684             NXPLOG_NCIHAL_E("NXP reset_ese_session_identity_set command failed");
   2685         }
   2686 #ifdef PN547C2_FACTORY_RESET_DEBUG
   2687         /* NXP ACT Proprietary Ext */
   2688         status = phNxpNciHal_send_ext_cmd(sizeof(reset_ese_session_identity),
   2689                                            reset_ese_session_identity);
   2690         if (status != NFCSTATUS_SUCCESS) {
   2691             NXPLOG_NCIHAL_E("NXP reset_ese_session_identity command failed");
   2692         }
   2693 #endif
   2694 
   2695     }
   2696 }
   2697 
   2698 /******************************************************************************
   2699  * Function         phNxpNciHal_print_res_status
   2700  *
   2701  * Description      This function is called to process the response status
   2702  *                  and print the status byte.
   2703  *
   2704  * Returns          void.
   2705  *
   2706  ******************************************************************************/
   2707 static void phNxpNciHal_print_res_status( uint8_t *p_rx_data, uint16_t *p_len)
   2708 {
   2709     static uint8_t response_buf[][30] = {"STATUS_OK",
   2710                                      "STATUS_REJECTED",
   2711                                      "STATUS_RF_FRAME_CORRUPTED" ,
   2712                                      "STATUS_FAILED" ,
   2713                                      "STATUS_NOT_INITIALIZED" ,
   2714                                      "STATUS_SYNTAX_ERROR",
   2715                                      "STATUS_SEMANTIC_ERROR",
   2716                                      "RFU",
   2717                                      "RFU",
   2718                                      "STATUS_INVALID_PARAM",
   2719                                      "STATUS_MESSAGE_SIZE_EXCEEDED",
   2720                                      "STATUS_UNDEFINED"};
   2721     int status_byte;
   2722     if(p_rx_data[0] == 0x40 && (p_rx_data[1] == 0x02 || p_rx_data[1] == 0x03))
   2723     {
   2724         if(p_rx_data[2] &&  p_rx_data[3]<=10)
   2725         {
   2726             status_byte = p_rx_data[CORE_RES_STATUS_BYTE];
   2727             NXPLOG_NCIHAL_D("%s: response status =%s",__FUNCTION__,response_buf[status_byte]);
   2728         }
   2729         else
   2730         {
   2731             NXPLOG_NCIHAL_D("%s: response status =%s",__FUNCTION__,response_buf[11]);
   2732         }
   2733         if(phNxpNciClock.isClockSet)
   2734         {
   2735             int i;
   2736             for(i=0; i<* p_len; i++)
   2737             {
   2738                 phNxpNciClock.p_rx_data[i] = p_rx_data[i];
   2739             }
   2740         }
   2741 
   2742         else if(phNxpNciRfSet.isGetRfSetting)
   2743         {
   2744             int i;
   2745             for(i=0; i<* p_len; i++)
   2746             {
   2747                 phNxpNciRfSet.p_rx_data[i] = p_rx_data[i];
   2748                 //NXPLOG_NCIHAL_D("%s: response status =0x%x",__FUNCTION__,p_rx_data[i]);
   2749             }
   2750         }
   2751         else if (phNxpNciMwEepromArea.isGetEepromArea)
   2752         {
   2753             int i;
   2754             for (i = 8; i < *p_len; i++)
   2755             {
   2756                 phNxpNciMwEepromArea.p_rx_data[i - 8] = p_rx_data[i];
   2757             }
   2758         }
   2759     }
   2760 
   2761     if (p_rx_data[2] && (config_access == TRUE))
   2762     {
   2763         if (p_rx_data[3] != NFCSTATUS_SUCCESS)
   2764         {
   2765             NXPLOG_NCIHAL_W ("Invalid Data from config file . Aborting..");
   2766             phNxpNciHal_close ();
   2767         }
   2768     }
   2769 }
   2770 
   2771 #if(NFC_NXP_CHIP_TYPE == PN548C2)
   2772 NFCSTATUS phNxpNciHal_core_reset_recovery ()
   2773 {
   2774     NFCSTATUS status = NFCSTATUS_FAILED;
   2775 
   2776     uint8_t buffer[260];
   2777     long bufflen = 260;
   2778 
   2779     /*NCI_INIT_CMD*/
   2780     static uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
   2781     /*NCI_RESET_CMD*/
   2782     static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00}; //keep configuration
   2783 
   2784     /* reset config cache */
   2785     uint8_t retry_core_init_cnt = 0;
   2786 
   2787     if (discovery_cmd_len == 0)
   2788     {
   2789         goto FAILURE;
   2790     }
   2791     NXPLOG_NCIHAL_D ("%s: recovery", __FUNCTION__);
   2792 
   2793 retry_core_init:
   2794     if (retry_core_init_cnt > 3)
   2795     {
   2796         goto FAILURE;
   2797     }
   2798 
   2799     status = phTmlNfc_IoCtl (phTmlNfc_e_ResetDevice);
   2800     if (status != NFCSTATUS_SUCCESS)
   2801     {
   2802         NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
   2803         goto FAILURE;
   2804     }
   2805     status = phNxpNciHal_send_ext_cmd (sizeof(cmd_reset_nci), cmd_reset_nci);
   2806     if ((status != NFCSTATUS_SUCCESS) && (nxpncihal_ctrl.retry_cnt >= MAX_RETRY_COUNT))
   2807     {
   2808         retry_core_init_cnt++;
   2809         goto retry_core_init;
   2810     }
   2811     else if (status != NFCSTATUS_SUCCESS)
   2812     {
   2813         NXPLOG_NCIHAL_D ("NCI_CORE_RESET: Failed");
   2814         retry_core_init_cnt++;
   2815         goto retry_core_init;
   2816     }
   2817     status = phNxpNciHal_send_ext_cmd (sizeof(cmd_init_nci), cmd_init_nci);
   2818     if (status != NFCSTATUS_SUCCESS)
   2819     {
   2820         NXPLOG_NCIHAL_D ("NCI_CORE_INIT : Failed");
   2821         retry_core_init_cnt++;
   2822         goto retry_core_init;
   2823     }
   2824 
   2825     status = phNxpNciHal_send_ext_cmd (discovery_cmd_len, discovery_cmd);
   2826     if (status != NFCSTATUS_SUCCESS)
   2827     {
   2828         NXPLOG_NCIHAL_D ("RF_DISCOVERY : Failed");
   2829         retry_core_init_cnt++;
   2830         goto retry_core_init;
   2831     }
   2832 
   2833     return NFCSTATUS_SUCCESS;
   2834 FAILURE:
   2835     abort ();
   2836 }
   2837 
   2838 void phNxpNciHal_discovery_cmd_ext (uint8_t *p_cmd_data, uint16_t cmd_len)
   2839 {
   2840     NXPLOG_NCIHAL_D ("phNxpNciHal_discovery_cmd_ext");
   2841     if (cmd_len > 0 && cmd_len <= sizeof(discovery_cmd))
   2842     {
   2843         memcpy (discovery_cmd, p_cmd_data, cmd_len);
   2844         discovery_cmd_len = cmd_len;
   2845     }
   2846 }
   2847 #endif
   2848