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