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