1 /*
2  * Copyright 2012-2023 NXP
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 <EseAdaptation.h>
18 #include <android-base/file.h>
19 #include <android-base/stringprintf.h>
20 #include <dlfcn.h>
21 #include <log/log.h>
22 #include <phDal4Nfc_messageQueueLib.h>
23 #include <phDnldNfc.h>
24 #include <phNfcNciConstants.h>
25 #include <phNxpConfig.h>
26 #include <phNxpEventLogger.h>
27 #include <phNxpLog.h>
28 #include <phNxpNciHal.h>
29 #include <phNxpNciHal_Adaptation.h>
30 #include <phNxpNciHal_Dnld.h>
31 #include <phNxpNciHal_ext.h>
32 #include <phNxpTempMgr.h>
33 #include <phTmlNfc.h>
34 #include <sys/stat.h>
35 
36 #include "NciDiscoveryCommandBuilder.h"
37 #include "NfccTransportFactory.h"
38 #include "NxpNfcThreadMutex.h"
39 #include "ObserveMode.h"
40 #include "ReaderPollConfigParser.h"
41 #include "phNxpNciHal_IoctlOperations.h"
42 #include "phNxpNciHal_LxDebug.h"
43 #include "phNxpNciHal_PowerTrackerIface.h"
44 #include "phNxpNciHal_ULPDet.h"
45 #include "phNxpNciHal_extOperations.h"
46 
47 using android::base::StringPrintf;
48 using android::base::WriteStringToFile;
49 
50 /*********************** Global Variables *************************************/
51 #define PN547C2_CLOCK_SETTING
52 #define CORE_RES_STATUS_BYTE 3
53 #define MAX_NXP_HAL_EXTN_BYTES 10
54 #define DEFAULT_MINIMAL_FW_VERSION 0x0110DE
55 #define EOS_FW_SESSION_STATE_LOCKED 0x02
56 
57 bool bEnableMfcExtns = false;
58 bool bEnableMfcReader = false;
59 
60 /* Processing of ISO 15693 EOF */
61 extern uint8_t icode_send_eof;
62 extern uint8_t icode_detected;
63 static uint8_t cmd_icode_eof[] = {0x00, 0x00, 0x00};
64 static const char* rf_block_num[] = {
65     "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",  "10", "11",
66     "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
67     "23", "24", "25", "26", "27", "28", "29", "30", NULL};
68 const char* rf_block_name = "NXP_RF_CONF_BLK_";
69 static uint8_t read_failed_disable_nfc = false;
70 /* FW download success flag */
71 static uint8_t fw_download_success = 0;
72 static uint8_t config_access = false;
73 static uint8_t config_success = true;
74 NfcHalThreadMutex sHalFnLock;
75 
76 /* NCI HAL Control structure */
77 phNxpNciHal_Control_t nxpncihal_ctrl;
78 
79 /* NXP Poll Profile structure */
80 phNxpNciProfile_Control_t nxpprofile_ctrl;
81 
82 /* TML Context */
83 extern phTmlNfc_Context_t* gpphTmlNfc_Context;
84 extern spTransport gpTransportObj;
85 
86 extern void phTmlNfc_set_fragmentation_enabled(
87     phTmlNfc_i2cfragmentation_t result);
88 
89 extern NFCSTATUS phNxpNciHal_ext_send_sram_config_to_flash();
90 extern NFCSTATUS phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel);
91 extern void phNxpNciHal_conf_nfc_forum_mode();
92 extern void phNxpNciHal_prop_conf_lpcd(bool enableLPCD);
93 extern void phNxpNciHal_prop_conf_rssi();
94 
95 nfc_stack_callback_t* p_nfc_stack_cback_backup;
96 phNxpNci_getCfg_info_t* mGetCfg_info = NULL;
97 /* global variable to get FW version from NCI response or dl get version
98  * response*/
99 uint32_t wFwVerRsp;
100 EseAdaptation* gpEseAdapt = NULL;
101 #ifdef NXP_BOOTTIME_UPDATE
102 ese_update_state_t ese_update = ESE_UPDATE_COMPLETED;
103 #endif
104 /* External global variable to get FW version */
105 extern uint16_t wFwVer;
106 extern uint8_t gRecFWDwnld;
107 static uint8_t gRecFwRetryCount;  // variable to hold recovery FW retry count
108 static uint8_t write_unlocked_status = NFCSTATUS_SUCCESS;
109 uint8_t wFwUpdateReq = false;
110 uint8_t wRfUpdateReq = false;
111 uint32_t timeoutTimerId = 0;
112 #ifndef FW_DWNLD_FLAG
113 uint8_t fw_dwnld_flag = false;
114 #endif
115 bool nfc_debug_enabled = true;
116 PowerTrackerHandle gPowerTrackerHandle;
117 
118 /*  Used to send Callback Transceive data during Mifare Write.
119  *  If this flag is enabled, no need to send response to Upper layer */
120 bool sendRspToUpperLayer = true;
121 
122 phNxpNciHal_Sem_t config_data;
123 
124 phNxpNciClock_t phNxpNciClock = {0, {0}, false};
125 
126 phNxpNciRfSetting_t phNxpNciRfSet = {false, {0}};
127 
128 phNxpNciMwEepromArea_t phNxpNciMwEepromArea = {false, {0}};
129 
130 volatile bool_t gsIsFirstHalMinOpen = true;
131 volatile bool_t gsIsFwRecoveryRequired = false;
132 
133 void* RfFwRegionDnld_handle = NULL;
134 fpVerInfoStoreInEeprom_t fpVerInfoStoreInEeprom = NULL;
135 fpRegRfFwDndl_t fpRegRfFwDndl = NULL;
136 fpPropConfCover_t fpPropConfCover = NULL;
137 fpDoAntennaActivity_t fpDoAntennaActivity = NULL;
138 void* phNxpNciHal_client_thread(void* arg);
139 /**************** local methods used in this file only ************************/
140 static void phNxpNciHal_open_complete(NFCSTATUS status);
141 static void phNxpNciHal_MinOpen_complete(NFCSTATUS status);
142 static void phNxpNciHal_write_complete(void* pContext,
143                                        phTmlNfc_TransactInfo_t* pInfo);
144 static void phNxpNciHal_read_complete(void* pContext,
145                                       phTmlNfc_TransactInfo_t* pInfo);
146 static void phNxpNciHal_close_complete(NFCSTATUS status);
147 static void phNxpNciHal_core_initialized_complete(NFCSTATUS status);
148 static void phNxpNciHal_power_cycle_complete(NFCSTATUS status);
149 static void phNxpNciHal_kill_client_thread(
150     phNxpNciHal_Control_t* p_nxpncihal_ctrl);
151 static void phNxpNciHal_nfccClockCfgRead(void);
152 static void phNxpNciHal_hci_network_reset(void);
153 static NFCSTATUS phNxpNciHal_do_swp_session_reset(void);
154 static void phNxpNciHal_print_res_status(uint8_t* p_rx_data, uint16_t* p_len);
155 static void phNxpNciHal_enable_i2c_fragmentation();
156 static NFCSTATUS phNxpNciHal_get_mw_eeprom(void);
157 static NFCSTATUS phNxpNciHal_set_mw_eeprom(void);
158 static void phNxpNciHal_configureLxDebugMode();
159 static void phNxpNciHal_gpio_restore(phNxpNciHal_GpioInfoState state);
160 static void phNxpNciHal_initialize_debug_enabled_flag();
161 static void phNxpNciHal_initialize_mifare_flag();
162 static NFCSTATUS phNxpNciHalRFConfigCmdRecSequence();
163 static NFCSTATUS phNxpNciHal_CheckRFCmdRespStatus();
164 static void phNxpNciHal_UpdateFwStatus(HalNfcFwUpdateStatus fwStatus);
165 static NFCSTATUS phNxpNciHal_resetDefaultSettings(uint8_t fw_update_req,
166                                                   bool keep_config);
167 static NFCSTATUS phNxpNciHal_force_fw_download(uint8_t seq_handler_offset = 0,
168                                                bool bIsNfccDlState = false);
169 static int phNxpNciHal_MinOpen_Clean(char* nfc_dev_node);
170 static void phNxpNciHal_DownloadFw(bool isMinFwVer,
171                                    bool degradedFwDnld = false);
172 static void phNxpNciHal_CheckAndHandleFwTearDown(void);
173 static NFCSTATUS phNxpNciHal_getChipInfoInFwDnldMode(
174     bool bIsVenResetReqd = false);
175 static uint8_t phNxpNciHal_getSessionInfoInFwDnldMode();
176 static NFCSTATUS phNxpNciHal_dlResetInFwDnldMode();
177 static NFCSTATUS phNxpNciHal_enableTmlRead();
178 
179 /******************************************************************************
180  * Function         onLoadLibrary
181  *
182  * Description      This function as marked with attribute constructor causes
183  *                  the function to be called automatically before execution
184  *                  enters main (). It is useful for initializing execution
185  *                  context  that will be used implicitly during the execution
186  *                  of the program like loading another dynamic library.
187  * PARAM            None
188  * Returns          void
189  *
190  ******************************************************************************/
onLoadLibrary(void)191 static __attribute__((constructor)) void onLoadLibrary(void) {
192   NXPLOG_NCIHAL_D("Initializing power tracker");
193   phNxpNciHal_PowerTrackerInit(&gPowerTrackerHandle);
194 }
195 
196 /******************************************************************************
197  * Function         onUnloadLibrary
198  *
199  * Description      This function as marked with attribute destructor causes
200  *                  the function to be called automatically after execution
201  *                  main () has completed. It is useful for de-initializing
202  *                  execution context  that were be used implicitly during the
203  *                  execution of the program like unloading another dynamic
204  *                  library.
205  * PARAM            None
206  * Returns          void
207  *
208  ******************************************************************************/
onUnloadLibrary(void)209 static __attribute__((destructor)) void onUnloadLibrary(void) {
210   NXPLOG_NCIHAL_D("De-initializing power tracker");
211   phNxpNciHal_PowerTrackerDeinit(&gPowerTrackerHandle);
212 }
213 
214 /******************************************************************************
215  * Function         phNxpNciHal_initialize_debug_enabled_flag
216  *
217  * Description      This function gets the value for nfc_debug_enabled
218  *
219  * Returns          void
220  *
221  ******************************************************************************/
phNxpNciHal_initialize_debug_enabled_flag()222 static void phNxpNciHal_initialize_debug_enabled_flag() {
223   unsigned long num = 0;
224   char valueStr[PROPERTY_VALUE_MAX] = {0};
225   if (GetNxpNumValue(NAME_NFC_DEBUG_ENABLED, &num, sizeof(num))) {
226     nfc_debug_enabled = (num == 0) ? false : true;
227   }
228 
229   int len = property_get("nfc.debug_enabled", valueStr, "");
230   if (len > 0) {
231     // let Android property override .conf variable
232     unsigned debug_enabled = 0;
233     int ret = sscanf(valueStr, "%u", &debug_enabled);
234     if (ret) nfc_debug_enabled = (debug_enabled == 0) ? false : true;
235   }
236   NXPLOG_NCIHAL_D("nfc_debug_enabled : %d", nfc_debug_enabled);
237 }
238 
239 /******************************************************************************
240  * Function         phNxpNciHal_client_thread
241  *
242  * Description      This function is a thread handler which handles all TML and
243  *                  NCI messages.
244  *
245  * Returns          void
246  *
247  ******************************************************************************/
phNxpNciHal_client_thread(void * arg)248 void* phNxpNciHal_client_thread(void* arg) {
249   phNxpNciHal_Control_t* p_nxpncihal_ctrl = (phNxpNciHal_Control_t*)arg;
250   phLibNfc_Message_t msg;
251 
252   NXPLOG_NCIHAL_D("thread started");
253 
254   p_nxpncihal_ctrl->thread_running = 1;
255 
256   while (p_nxpncihal_ctrl->thread_running == 1) {
257     /* Fetch next message from the NFC stack message queue */
258     if (phDal4Nfc_msgrcv(p_nxpncihal_ctrl->gDrvCfg.nClientId, &msg, 0, 0) ==
259         -1) {
260       NXPLOG_NCIHAL_E("NFC client received bad message");
261       continue;
262     }
263 
264     if (p_nxpncihal_ctrl->thread_running == 0) {
265       break;
266     }
267 
268     switch (msg.eMsgType) {
269       case PH_LIBNFC_DEFERREDCALL_MSG: {
270         phLibNfc_DeferredCall_t* deferCall =
271             (phLibNfc_DeferredCall_t*)(msg.pMsgData);
272 
273         REENTRANCE_LOCK();
274         deferCall->pCallback(deferCall->pParameter);
275         REENTRANCE_UNLOCK();
276 
277         break;
278       }
279 
280       case NCI_HAL_OPEN_CPLT_MSG: {
281         REENTRANCE_LOCK();
282         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
283           /* Send the event */
284           (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_OPEN_CPLT_EVT,
285                                               HAL_NFC_STATUS_OK);
286         }
287         REENTRANCE_UNLOCK();
288         break;
289       }
290 
291       case NCI_HAL_CLOSE_CPLT_MSG: {
292         REENTRANCE_LOCK();
293         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
294           /* Send the event */
295           (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_CLOSE_CPLT_EVT,
296                                               HAL_NFC_STATUS_OK);
297         }
298         phNxpNciHal_kill_client_thread(&nxpncihal_ctrl);
299         REENTRANCE_UNLOCK();
300         break;
301       }
302 
303       case NCI_HAL_POST_INIT_CPLT_MSG: {
304         REENTRANCE_LOCK();
305         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
306           /* Send the event */
307           (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_POST_INIT_CPLT_EVT,
308                                               HAL_NFC_STATUS_OK);
309         }
310         REENTRANCE_UNLOCK();
311         break;
312       }
313 
314       case NCI_HAL_PRE_DISCOVER_CPLT_MSG: {
315         REENTRANCE_LOCK();
316         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
317           /* Send the event */
318           (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_PRE_DISCOVER_CPLT_EVT,
319                                               HAL_NFC_STATUS_OK);
320         }
321         REENTRANCE_UNLOCK();
322         break;
323       }
324 
325       case NCI_HAL_HCI_NETWORK_RESET_MSG: {
326         REENTRANCE_LOCK();
327         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
328           /* Send the event */
329           (*nxpncihal_ctrl.p_nfc_stack_cback)(
330               (uint32_t)HAL_HCI_NETWORK_RESET_EVT, HAL_NFC_STATUS_OK);
331         }
332         REENTRANCE_UNLOCK();
333         break;
334       }
335 
336       case NCI_HAL_ERROR_MSG: {
337         REENTRANCE_LOCK();
338         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
339           /* Send the event */
340           (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_ERROR_EVT,
341                                               HAL_NFC_STATUS_FAILED);
342         }
343         REENTRANCE_UNLOCK();
344         break;
345       }
346 
347       case NCI_HAL_RX_MSG: {
348         REENTRANCE_LOCK();
349         if (nxpncihal_ctrl.p_nfc_stack_data_cback != NULL) {
350           (*nxpncihal_ctrl.p_nfc_stack_data_cback)(nxpncihal_ctrl.rsp_len,
351                                                    nxpncihal_ctrl.p_rsp_data);
352         }
353         REENTRANCE_UNLOCK();
354         break;
355       }
356       case HAL_NFC_FW_UPDATE_STATUS_EVT: {
357         REENTRANCE_LOCK();
358         if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
359           /* Send the event */
360           (*nxpncihal_ctrl.p_nfc_stack_cback)(msg.eMsgType,
361                                               *((uint8_t*)msg.pMsgData));
362         }
363         REENTRANCE_UNLOCK();
364         break;
365       }
366     }
367   }
368 
369   NXPLOG_NCIHAL_D("NxpNciHal thread stopped");
370 
371   return NULL;
372 }
373 
374 /******************************************************************************
375  * Function         phNxpNciHal_kill_client_thread
376  *
377  * Description      This function safely kill the client thread and clean all
378  *                  resources.
379  *
380  * Returns          void.
381  *
382  ******************************************************************************/
phNxpNciHal_kill_client_thread(phNxpNciHal_Control_t * p_nxpncihal_ctrl)383 static void phNxpNciHal_kill_client_thread(
384     phNxpNciHal_Control_t* p_nxpncihal_ctrl) {
385   NXPLOG_NCIHAL_D("Terminating phNxpNciHal client thread...");
386 
387   p_nxpncihal_ctrl->p_nfc_stack_cback = NULL;
388   p_nxpncihal_ctrl->p_nfc_stack_data_cback = NULL;
389   p_nxpncihal_ctrl->thread_running = 0;
390 
391   return;
392 }
393 /******************************************************************************
394  * Function         phNxpNciHal_CheckIntegrityRecovery
395  *
396  * Description     This function to enter in recovery if FW download fails with
397  *                 check integrity.
398  *
399  * Returns         NFCSTATUS
400  *
401  ******************************************************************************/
phNxpNciHal_CheckIntegrityRecovery()402 static NFCSTATUS phNxpNciHal_CheckIntegrityRecovery() {
403   NFCSTATUS status = NFCSTATUS_FAILED;
404   if (phNxpNciHal_nfcc_core_reset_init(false) == NFCSTATUS_SUCCESS) {
405     status = phNxpNciHal_fw_download();
406   } else {
407     status = NFCSTATUS_FW_CHECK_INTEGRITY_FAILED;
408   }
409   return status;
410 }
411 /******************************************************************************
412  * Function         phNxpNciHal_force_fw_download
413  *
414  * Description     This function, based on the offset provided, will trigger
415  *                 Secure FW download sequence.
416  *                 It will retry the FW download in case the Check Integrity
417  *                 has been failed.
418  *
419  * Parameters      Offset by which the FW dnld Seq handler shall be triggered.
420  *                 e.g. if we want to send only the Check Integrity command,
421  *                 then the offset shall be 7.
422  *                 bIsNfccDlState : Indicates if current FW State is FW
423  *                 Download/NCI.
424  *
425  * Returns         SUCCESS if FW download is successful else FAIL.
426  *
427  ******************************************************************************/
phNxpNciHal_force_fw_download(uint8_t seq_handler_offset,bool bIsNfccDlState)428 static NFCSTATUS phNxpNciHal_force_fw_download(uint8_t seq_handler_offset,
429                                                bool bIsNfccDlState) {
430   NFCSTATUS wConfigStatus = NFCSTATUS_SUCCESS;
431   NFCSTATUS status = NFCSTATUS_SUCCESS;
432   /*Get FW version from device*/
433   for (int retry = 1; retry >= 0; retry--) {
434     if (phDnldNfc_InitImgInfo() == NFCSTATUS_SUCCESS) {
435       break;
436     } else {
437       phDnldNfc_ReSetHwDevHandle();
438       NXPLOG_NCIHAL_E("Image information extraction Failed!!");
439       if (!retry) return NFCSTATUS_FAILED;
440     }
441   }
442 
443   NXPLOG_NCIHAL_D("FW version for FW file = 0x%x", wFwVer);
444   NXPLOG_NCIHAL_D("FW version from device = 0x%x", wFwVerRsp);
445   if (wFwVerRsp == 0) {
446     status = phNxpNciHal_getChipInfoInFwDnldMode(true);
447     if (status != NFCSTATUS_SUCCESS) {
448       NXPLOG_NCIHAL_E("phNxpNciHal_getChipInfoInFwDnldMode Failed");
449     }
450     bIsNfccDlState = true;
451   }
452   if (NFCSTATUS_SUCCESS == phNxpNciHal_CheckValidFwVersion()) {
453     NXPLOG_NCIHAL_D("FW update required");
454     nxpncihal_ctrl.phNxpNciGpioInfo.state = GPIO_UNKNOWN;
455     if (IS_CHIP_TYPE_L(sn100u)) phNxpNciHal_gpio_restore(GPIO_STORE);
456     fw_download_success = 0;
457     /*We are expecting NFC to be either in NFC or in the FW Download state*/
458     status = phNxpNciHal_fw_download(seq_handler_offset, bIsNfccDlState);
459     if (status == NFCSTATUS_FW_CHECK_INTEGRITY_FAILED) {
460       status = phNxpNciHal_CheckIntegrityRecovery();
461     }
462     property_set("nfc.fw.downloadmode_force", "0");
463     if (status == NFCSTATUS_SUCCESS) {
464       wConfigStatus = NFCSTATUS_SUCCESS;
465       fw_download_success = TRUE;
466     } else if (status == NFCSTATUS_FW_CHECK_INTEGRITY_FAILED ||
467                (phNxpNciHal_fw_mw_ver_check() != NFCSTATUS_SUCCESS)) {
468       phOsalNfc_Timer_Cleanup();
469       phNxpTempMgr::GetInstance().Reset();
470       phTmlNfc_Shutdown_CleanUp();
471       return NFCSTATUS_CMD_ABORTED;
472     }
473 
474     status = phNxpNciHal_nfcc_core_reset_init();
475     if (status == NFCSTATUS_SUCCESS && IS_CHIP_TYPE_L(sn100u)) {
476       if (status == NFCSTATUS_SUCCESS) {
477         phNxpNciHal_gpio_restore(GPIO_RESTORE);
478       } else {
479         NXPLOG_NCIHAL_E("Failed to restore GPIO values!!!\n");
480       }
481     }
482   }
483   return wConfigStatus;
484 }
485 
486 /******************************************************************************
487  * Function         phNxpNciHal_fw_download
488  *
489  * Description      This function download the PN54X secure firmware to IC. If
490  *                  firmware version in Android filesystem and firmware in the
491  *                  IC is same then firmware download will return with success
492  *                  without downloading the firmware.
493  *
494  * Returns          NFCSTATUS_SUCCESS if firmware download successful
495  *                  NFCSTATUS_FAILED in case of failure
496  *
497  ******************************************************************************/
phNxpNciHal_fw_download(uint8_t seq_handler_offset,bool bIsNfccDlState)498 NFCSTATUS phNxpNciHal_fw_download(uint8_t seq_handler_offset,
499                                   bool bIsNfccDlState) {
500   NFCSTATUS status = NFCSTATUS_SUCCESS;
501   phNxpNciHal_UpdateFwStatus(HAL_NFC_FW_UPDATE_START);
502   phNxpNciHal_nfccClockCfgRead();
503 
504   if (!bIsNfccDlState) {
505     status = phNxpNciHal_write_fw_dw_status(TRUE);
506     if (status != NFCSTATUS_SUCCESS) {
507       NXPLOG_NCIHAL_E("%s: NXP Set FW DW Flag failed", __FUNCTION__);
508     }
509 
510     NXPLOG_NCIHAL_D("nfcFL.nfccFL._NFCC_DWNLD_MODE %x\n",
511                     nfcFL.nfccFL._NFCC_DWNLD_MODE);
512 
513     if (IS_CHIP_TYPE_GE(sn100u)) {
514       uint8_t ven_cfg_low_cmd[] = {0x20, 0x02, 0x05, 0x01,
515                                    0xA0, 0x07, 0x01, 0x00};
516       status =
517           phNxpNciHal_send_ext_cmd(sizeof(ven_cfg_low_cmd), ven_cfg_low_cmd);
518       if (status != NFCSTATUS_SUCCESS) {
519         NXPLOG_NCIHAL_E("Failed to set VEN_CFG to low \n");
520       }
521     }
522     /*Save UICC params */
523     status = phNxpNciHal_save_uicc_params();
524     if (status != NFCSTATUS_SUCCESS) {
525       NXPLOG_NCIHAL_E("Failed to save UICC params \n");
526     }
527 
528     status = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
529     if (NFCSTATUS_SUCCESS != status) {
530       phTmlNfc_EnableFwDnldMode(false);
531       phNxpNciHal_UpdateFwStatus(HAL_NFC_FW_UPDATE_FAILED);
532       return NFCSTATUS_FAILED;
533     }
534   }
535 
536   /* Make sure read thread is pending before updating phTmlNfc_EnableFwDnldMode
537    * to true*/
538   NFCSTATUS readStatus = phNxpNciHal_enableTmlRead();
539   if (readStatus != PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_BUSY)) {
540     NXPLOG_NCIHAL_E("Read Thread is not pending already. status = 0x%x \n",
541                     readStatus);
542   }
543 
544   if (nfcFL.nfccFL._NFCC_DWNLD_MODE == NFCC_DWNLD_WITH_NCI_CMD &&
545       (!bIsNfccDlState)) {
546     nxpncihal_ctrl.isCoreRstForFwDnld = TRUE;
547     /*NCI_RESET_CMD*/
548     static uint8_t cmd_reset_nci_dwnld[] = {0x20, 0x00, 0x01, 0x80};
549     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci_dwnld),
550                                       cmd_reset_nci_dwnld);
551     if (status != NFCSTATUS_SUCCESS) {
552       NXPLOG_NCIHAL_E("Core reset FW download command failed \n");
553     }
554     nxpncihal_ctrl.isCoreRstForFwDnld = FALSE;
555   }
556   if (NFCSTATUS_SUCCESS == status) {
557     phTmlNfc_EnableFwDnldMode(true);
558     /* Set the obtained device handle to download module */
559 
560     phDnldNfc_SetHwDevHandle();
561 
562     NXPLOG_NCIHAL_D("Calling Seq handler for FW Download \n");
563     status = phNxpNciHal_fw_download_seq(nxpprofile_ctrl.bClkSrcVal,
564                                          nxpprofile_ctrl.bClkFreqVal,
565                                          seq_handler_offset);
566 
567     if (phNxpNciHal_dlResetInFwDnldMode() != NFCSTATUS_SUCCESS) {
568       NXPLOG_NCIHAL_E("DL Reset failed in FW DN mode");
569     }
570 
571     /* FW download done.Therefore if previous I2C write failed then we can
572      * change the state to NFCSTATUS_SUCCESS*/
573     write_unlocked_status = NFCSTATUS_SUCCESS;
574   } else {
575     phTmlNfc_EnableFwDnldMode(false);
576     status = NFCSTATUS_FAILED;
577   }
578   if (NFCSTATUS_SUCCESS == status) {
579     phNxpNciHal_UpdateFwStatus(HAL_NFC_FW_UPDATE_SCUCCESS);
580   } else {
581     phNxpNciHal_UpdateFwStatus(HAL_NFC_FW_UPDATE_FAILED);
582   }
583   return status;
584 }
585 
586 /******************************************************************************
587  * Function         phNxpNciHal_CheckValidFwVersion
588  *
589  * Description      This function checks the valid FW for Mobile device.
590  *                  If the FW doesn't belong the Mobile device it further
591  *                  checks nxp config file to override.
592  *
593  * Returns          NFCSTATUS_SUCCESS if valid fw version found
594  *                  NFCSTATUS_NOT_ALLOWED in case of FW not valid for mobile
595  *                  device
596  *
597  ******************************************************************************/
phNxpNciHal_CheckValidFwVersion(void)598 NFCSTATUS phNxpNciHal_CheckValidFwVersion(void) {
599   NFCSTATUS status = NFCSTATUS_NOT_ALLOWED;
600   const unsigned char sfw_infra_major_no = 0x02;
601   unsigned char ufw_current_major_no = 0x00;
602   uint8_t rom_version = 0xFF & (wFwVerRsp >> 16);
603   uint8_t fw_maj_ver = 0xFF & (wFwVerRsp >> 8);
604 
605   /* extract the firmware's major no */
606   ufw_current_major_no = ((0x00FF) & (wFwVer >> 8U));
607   NXPLOG_NCIHAL_D("%s current_major_no = 0x%x", __func__, ufw_current_major_no);
608   NXPLOG_NCIHAL_D("%s fw_maj_ver = 0x%x", __func__, fw_maj_ver);
609   if (IS_CHIP_TYPE_EQ(pn557)) {
610     if (ufw_current_major_no >= fw_maj_ver) {
611       /* if file major version is grater than the one from the
612          Nfc init command allow FW download
613       */
614       status = NFCSTATUS_SUCCESS;
615     }
616     return status;
617   }
618 
619   if (wFwVerRsp == 0) {
620     NXPLOG_NCIHAL_E(
621         "FW Version not received by NCI command >>> Force Firmware download");
622     status = NFCSTATUS_SUCCESS;
623   } else if ((ufw_current_major_no == nfcFL._FW_MOBILE_MAJOR_NUMBER) ||
624              ((ufw_current_major_no == FW_MOBILE_MAJOR_NUMBER_PN81A) &&
625               (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0))) {
626     NXPLOG_NCIHAL_E("FW Version 2");
627     status = NFCSTATUS_SUCCESS;
628   } else if (ufw_current_major_no == sfw_infra_major_no) {
629     if ((rom_version == FW_MOBILE_ROM_VERSION_PN553 ||
630          rom_version == FW_MOBILE_ROM_VERSION_PN557)) {
631       NXPLOG_NCIHAL_D(" PN557  allow Fw download with major number =  0x%x",
632                       ufw_current_major_no);
633       status = NFCSTATUS_SUCCESS;
634     } else {
635       status = NFCSTATUS_NOT_ALLOWED;
636     }
637   } else {
638     NXPLOG_NCIHAL_E("Wrong FW Version >>> Firmware download not allowed");
639   }
640 
641   return status;
642 }
643 
644 /******************************************************************************
645  * Function         phNxpNciHal_MinOpen_Clean
646  *
647  * Description      This function shall be called from phNxpNciHal_MinOpen when
648  *                  any unrecoverable error has encountered which needs to mark
649  *                  min open as failed, HAL status as closed & deallocate any
650  *                  memory if allocated.
651  *
652  * Returns          This function always returns Failure
653  *
654  ******************************************************************************/
phNxpNciHal_MinOpen_Clean(char * nfc_dev_node)655 static int phNxpNciHal_MinOpen_Clean(char* nfc_dev_node) {
656   if (nfc_dev_node != NULL) {
657     free(nfc_dev_node);
658     nfc_dev_node = NULL;
659   }
660   if (mGetCfg_info != NULL) {
661     free(mGetCfg_info);
662     mGetCfg_info = NULL;
663   }
664   /* Report error status */
665   phNxpNciHal_cleanup_monitor();
666   nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
667   return NFCSTATUS_FAILED;
668 }
669 
670 /******************************************************************************
671  * Function         phNxpNciHal_MinOpen
672  *
673  * Description      This function initializes the least required resources to
674  *                  communicate to NFCC.This is mainly used to communicate to
675  *                  NFCC when NFC service is not available.
676  *
677  *
678  * Returns          This function return NFCSTATUS_SUCCESS (0) in case of
679  *                  success In case of failure returns other failure value.
680  *
681  ******************************************************************************/
phNxpNciHal_MinOpen()682 int phNxpNciHal_MinOpen() {
683   phOsalNfc_Config_t tOsalConfig;
684   phTmlNfc_Config_t tTmlConfig;
685   char* nfc_dev_node = NULL;
686   const uint16_t max_len = 260;
687   NFCSTATUS wConfigStatus = NFCSTATUS_SUCCESS;
688   NFCSTATUS status = NFCSTATUS_SUCCESS;
689   int dnld_retry_cnt = 0;
690   NXPLOG_NCIHAL_D("phNxpNci_MinOpen(): enter");
691 
692   if (nxpncihal_ctrl.halStatus == HAL_STATUS_MIN_OPEN) {
693     NXPLOG_NCIHAL_D("phNxpNciHal_MinOpen(): already open");
694     return NFCSTATUS_SUCCESS;
695   }
696   phNxpNciHal_initializeRegRfFwDnld();
697 
698   int8_t ret_val = 0x00;
699 
700   phNxpNciHal_initialize_debug_enabled_flag();
701   /* initialize trace level */
702   phNxpLog_InitializeLogLevel();
703 
704   /* initialize Mifare flags*/
705   phNxpNciHal_initialize_mifare_flag();
706 
707   /*Create the timer for extns write response*/
708   timeoutTimerId = phOsalNfc_Timer_Create();
709 
710   if (phNxpNciHal_init_monitor() == NULL) {
711     NXPLOG_NCIHAL_E("Init monitor failed");
712     return NFCSTATUS_FAILED;
713   }
714 
715   CONCURRENCY_LOCK();
716   memset(&tOsalConfig, 0x00, sizeof(tOsalConfig));
717   memset(&tTmlConfig, 0x00, sizeof(tTmlConfig));
718   memset(&nxpprofile_ctrl, 0, sizeof(phNxpNciProfile_Control_t));
719 
720   /*Init binary semaphore for Spi Nfc synchronization*/
721   if (0 != sem_init(&nxpncihal_ctrl.syncSpiNfc, 0, 1)) {
722     NXPLOG_NCIHAL_E("sem_init() FAiled, errno = 0x%02X", errno);
723     CONCURRENCY_UNLOCK();
724     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
725   }
726 
727   /* By default HAL status is HAL_STATUS_OPEN */
728   nxpncihal_ctrl.halStatus = HAL_STATUS_OPEN;
729 
730   /*nci version NCI_VERSION_2_0 version by default for SN100 chip type*/
731   nxpncihal_ctrl.nci_info.nci_version = NCI_VERSION_2_0;
732   /* Read the nfc device node name */
733   nfc_dev_node = (char*)malloc(max_len * sizeof(char));
734   if (nfc_dev_node == NULL) {
735     NXPLOG_NCIHAL_D("malloc of nfc_dev_node failed ");
736     CONCURRENCY_UNLOCK();
737     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
738   } else if (!GetNxpStrValue(NAME_NXP_NFC_DEV_NODE, nfc_dev_node, max_len)) {
739     NXPLOG_NCIHAL_D(
740         "Invalid nfc device node name keeping the default device node "
741         "/dev/pn54x");
742     strlcpy(nfc_dev_node, "/dev/pn54x", (max_len * sizeof(char)));
743   }
744   /* Configure hardware link */
745   nxpncihal_ctrl.gDrvCfg.nClientId = phDal4Nfc_msgget(0, 0600);
746   nxpncihal_ctrl.gDrvCfg.nLinkType = ENUM_LINK_TYPE_I2C; /* For PN54X */
747   tTmlConfig.pDevName = (int8_t*)nfc_dev_node;
748   tOsalConfig.dwCallbackThreadId = (uintptr_t)nxpncihal_ctrl.gDrvCfg.nClientId;
749   tOsalConfig.pLogFile = NULL;
750   tTmlConfig.dwGetMsgThreadId = (uintptr_t)nxpncihal_ctrl.gDrvCfg.nClientId;
751   mGetCfg_info = NULL;
752   mGetCfg_info =
753       (phNxpNci_getCfg_info_t*)nxp_malloc(sizeof(phNxpNci_getCfg_info_t));
754   if (mGetCfg_info == NULL) {
755     CONCURRENCY_UNLOCK();
756     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
757   }
758   memset(mGetCfg_info, 0x00, sizeof(phNxpNci_getCfg_info_t));
759 
760   /* Initialize TML layer */
761   wConfigStatus = phTmlNfc_Init(&tTmlConfig);
762   if (wConfigStatus != NFCSTATUS_SUCCESS) {
763     NXPLOG_NCIHAL_E("phTmlNfc_Init Failed");
764     CONCURRENCY_UNLOCK();
765     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
766   } else {
767     if (nfc_dev_node != NULL) {
768       free(nfc_dev_node);
769       nfc_dev_node = NULL;
770     }
771   }
772 
773   /* Create the client thread */
774   ret_val = pthread_create(&nxpncihal_ctrl.client_thread, NULL,
775                            phNxpNciHal_client_thread, &nxpncihal_ctrl);
776   if (ret_val != 0) {
777     NXPLOG_NCIHAL_E("pthread_create failed");
778     wConfigStatus = phTmlNfc_Shutdown_CleanUp();
779     CONCURRENCY_UNLOCK();
780     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
781   }
782 
783   CONCURRENCY_UNLOCK();
784   /* call read pending */
785   status = phTmlNfc_Read(
786       nxpncihal_ctrl.p_rsp_data, NCI_MAX_DATA_LEN,
787       (pphTmlNfc_TransactCompletionCb_t)&phNxpNciHal_read_complete, NULL);
788   if (status != NFCSTATUS_PENDING) {
789     NXPLOG_NCIHAL_E("TML Read status error status = %x", status);
790     wConfigStatus = phTmlNfc_Shutdown_CleanUp();
791     wConfigStatus = NFCSTATUS_FAILED;
792     return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
793   }
794 
795   /* Get the chip-type to know if it is PN557
796    Then don't send the Get version command */
797   unsigned long chipInfo = 0;
798   if (GetNxpNumValue(NAME_NXP_NFC_CHIP, &chipInfo, sizeof(chipInfo))) {
799     NXPLOG_NCIHAL_D("The chip type is %lx", chipInfo);
800   }
801 
802   if (gsIsFirstHalMinOpen) {
803     /*Skip get version command for pn557*/
804     if (chipInfo != pn557) phNxpNciHal_CheckAndHandleFwTearDown();
805   }
806 
807   uint8_t seq_handler_offset = 0x00;
808   uint8_t fw_update_req = 1;
809   uint8_t rf_update_req;
810   bool bVenResetRequired = false;
811   bool bIsNfccDlState = false;
812   phNxpNciHal_ext_init();
813 
814   phTmlNfc_IoCtl(phTmlNfc_e_EnableVen);
815 
816   if (phNxpNciHal_isULPDetSupported()) {
817     status = phTmlNfc_IoCtl(phTmlNfc_e_PullVenHigh);
818     if (NFCSTATUS_SUCCESS == status) {
819       NXPLOG_NCIHAL_D("ULPDET phTmlNfc_e_PullVenHigh - SUCCESS\n");
820     } else {
821       NXPLOG_NCIHAL_D("ULPDET phTmlNfc_e_PullVenHigh - FAILED\n");
822     }
823   }
824 
825   if (wFwVerRsp == 0) {
826     bVenResetRequired = true;
827   }
828   /* reset version info new version info will be fetch */
829   wFwVerRsp = 0x00;
830   wFwVer = 0x00;
831   if (NFCSTATUS_SUCCESS == phNxpNciHal_nfcc_core_reset_init(true)) {
832     setNxpFwConfigPath();
833     if (IS_CHIP_TYPE_L(sn100u)) phNxpNciHal_enable_i2c_fragmentation();
834 
835     status = phNxpNciHal_CheckFwRegFlashRequired(&fw_update_req, &rf_update_req,
836                                                  false);
837     if (status != NFCSTATUS_OK) {
838       NXPLOG_NCIHAL_D(
839           "phNxpNciHal_CheckFwRegFlashRequired() failed:exit status = %x",
840           status);
841       fw_update_req = FALSE;
842       rf_update_req = FALSE;
843     }
844 
845     if (!wFwUpdateReq) {
846       uint8_t is_teared_down = 0x00;
847       status = phNxpNciHal_read_fw_dw_status(is_teared_down);
848       if (status != NFCSTATUS_SUCCESS) {
849         NXPLOG_NCIHAL_E("%s: NXP get FW DW Flag failed", __FUNCTION__);
850       }
851       if (is_teared_down) {
852         seq_handler_offset = PHLIBNFC_DNLD_CHECKINTEGRITY_OFFSET;
853         fw_update_req = TRUE;
854       } else {
855         NXPLOG_NCIHAL_D("FW update not required");
856         property_set("nfc.fw.downloadmode_force", "0");
857         phDnldNfc_ReSetHwDevHandle();
858       }
859     }
860   } else if (bVenResetRequired) {
861     if (NFCSTATUS_SUCCESS == phNxpNciHal_getChipInfoInFwDnldMode(true))
862       bIsNfccDlState = true;
863   }
864 
865   if (gsIsFirstHalMinOpen && gsIsFwRecoveryRequired) {
866     NXPLOG_NCIHAL_E("FW Recovery is required");
867     fw_update_req = true;
868   }
869 
870   do {
871     if (fw_update_req && !fw_download_success) {
872       gsIsFwRecoveryRequired = false;
873       status =
874           phNxpNciHal_force_fw_download(seq_handler_offset, bIsNfccDlState);
875       if (status == NFCSTATUS_CMD_ABORTED) {
876         return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
877       } else if (fw_download_success) {
878         wConfigStatus = NFCSTATUS_SUCCESS;
879       }
880     }
881     status = phNxpNciHal_resetDefaultSettings(
882         fw_update_req, fw_download_success ? false : true);
883 
884     if ((status != NFCSTATUS_SUCCESS && fw_download_success) ||
885         (gsIsFwRecoveryRequired && (fw_update_req || gsIsFirstHalMinOpen))) {
886       NXPLOG_NCIHAL_E(
887           "FW Recovery required, Perform Force FW Download "
888           "gsIsFwRecoveryRequired %d",
889           gsIsFwRecoveryRequired);
890       fw_update_req = 1;
891       dnld_retry_cnt++;
892     } else if (status != NFCSTATUS_SUCCESS) {
893       return phNxpNciHal_MinOpen_Clean(nfc_dev_node);
894     } else {
895       break;
896     }
897 
898     if (dnld_retry_cnt > 1) {
899       wConfigStatus = NFCSTATUS_FAILED;
900       break;
901     }
902 
903   } while (status != NFCSTATUS_SUCCESS || gsIsFwRecoveryRequired);
904 
905   if (fpDoAntennaActivity != NULL &&
906       (gsIsFirstHalMinOpen || fw_download_success)) {
907     fpDoAntennaActivity(ANTENNA_CHECK_STATUS);
908   }
909   /* Call open complete */
910   phNxpNciHal_MinOpen_complete(wConfigStatus);
911   NXPLOG_NCIHAL_D("phNxpNciHal_MinOpen(): exit");
912   return wConfigStatus;
913 }
914 
915 /******************************************************************************
916  * Function         phNxpNciHal_open
917  *
918  * Description      This function is called by libnfc-nci during the
919  *                  initialization of the NFCC. It opens the physical connection
920  *                  with NFCC (PN54X) and creates required client thread for
921  *                  operation.
922  *                  After open is complete, status is informed to libnfc-nci
923  *                  through callback function.
924  *
925  * Returns          This function return NFCSTATUS_SUCCESS (0) in case of
926  *                  success In case of failure returns other failure value.
927  *
928  ******************************************************************************/
phNxpNciHal_open(nfc_stack_callback_t * p_cback,nfc_stack_data_callback_t * p_data_cback)929 int phNxpNciHal_open(nfc_stack_callback_t* p_cback,
930                      nfc_stack_data_callback_t* p_data_cback) {
931   NFCSTATUS wConfigStatus = NFCSTATUS_SUCCESS;
932   NFCSTATUS status = NFCSTATUS_SUCCESS;
933   NXPLOG_NCIHAL_E("phNxpNciHal_open NFC HAL OPEN");
934 #ifdef NXP_BOOTTIME_UPDATE
935   if (ese_update != ESE_UPDATE_COMPLETED) {
936     ALOGD("BLOCK NFC HAL OPEN");
937     if (p_cback != NULL) {
938       p_nfc_stack_cback_backup = p_cback;
939       (*p_cback)(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
940     }
941     return NFCSTATUS_FAILED;
942   }
943 #endif
944   NfcHalAutoThreadMutex a(sHalFnLock);
945   if (nxpncihal_ctrl.halStatus == HAL_STATUS_OPEN) {
946     NXPLOG_NCIHAL_D("phNxpNciHal_open already open");
947     phNxpNciHal_open_complete(wConfigStatus);
948     return wConfigStatus;
949   } else if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
950     PhNxpEventLogger::GetInstance().Initialize();
951     memset(&nxpncihal_ctrl, 0x00, sizeof(nxpncihal_ctrl));
952     nxpncihal_ctrl.p_nfc_stack_cback = p_cback;
953     nxpncihal_ctrl.p_nfc_stack_data_cback = p_data_cback;
954     status = phNxpNciHal_MinOpen();
955     if (status != NFCSTATUS_SUCCESS) {
956       NXPLOG_NCIHAL_E("phNxpNciHal_MinOpen failed");
957       goto clean_and_return;
958     } /*else its already in MIN_OPEN state. continue with rest of
959          functionality*/
960   } else {
961     nxpncihal_ctrl.p_nfc_stack_cback = p_cback;
962     nxpncihal_ctrl.p_nfc_stack_data_cback = p_data_cback;
963   }
964   /* Call open complete */
965   phNxpNciHal_open_complete(wConfigStatus);
966 
967   return wConfigStatus;
968 
969 clean_and_return:
970   CONCURRENCY_UNLOCK();
971   /* Report error status */
972   if (p_cback != NULL) {
973     (*p_cback)(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
974   }
975 
976   nxpncihal_ctrl.p_nfc_stack_cback = NULL;
977   nxpncihal_ctrl.p_nfc_stack_data_cback = NULL;
978   phNxpNciHal_cleanup_monitor();
979   nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
980   return NFCSTATUS_FAILED;
981 }
982 
983 /******************************************************************************
984  * Function         phNxpNciHal_fw_mw_check
985  *
986  * Description      This function inform the status of phNxpNciHal_fw_mw_check
987  *                  function to libnfc-nci.
988  *
989  * Returns          int.
990  *
991  ******************************************************************************/
phNxpNciHal_fw_mw_ver_check()992 int phNxpNciHal_fw_mw_ver_check() {
993   NFCSTATUS status = NFCSTATUS_FAILED;
994   uint8_t rom_version = 0xFF & (wFwVerRsp >> 16);
995   uint8_t fw_maj_ver = 0xFF & (wFwVerRsp >> 8);
996 
997   switch (nfcFL.chipType) {
998     case pn557:
999       if ((rom_version == FW_MOBILE_ROM_VERSION_PN557) &&
1000           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_PN557))
1001         status = NFCSTATUS_SUCCESS;
1002       break;
1003     case pn80T:
1004       /* PN553 & PN80T have same rom & fw major version */
1005       [[fallthrough]];
1006     case pn553:
1007       if ((rom_version == FW_MOBILE_ROM_VERSION_PN553) &&
1008           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_PN553))
1009         status = NFCSTATUS_SUCCESS;
1010       break;
1011     case pn67T:
1012       /* PN551 & PN67T have same rom & fw major version */
1013       [[fallthrough]];
1014     case pn551:
1015       if ((rom_version == FW_MOBILE_ROM_VERSION_PN551) &&
1016           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_PN551))
1017         status = NFCSTATUS_SUCCESS;
1018       break;
1019     case sn100u:
1020       if ((rom_version == FW_MOBILE_ROM_VERSION_SN100U) &&
1021           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_SN100U))
1022         status = NFCSTATUS_SUCCESS;
1023       break;
1024     case sn220u:
1025       if ((rom_version == FW_MOBILE_ROM_VERSION_SN220U) &&
1026           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_SN220U))
1027         status = NFCSTATUS_SUCCESS;
1028       break;
1029     case sn300u:
1030       if ((rom_version == FW_MOBILE_ROM_VERSION_SN300U) &&
1031           (fw_maj_ver == FW_MOBILE_MAJOR_NUMBER_SN300U))
1032         status = NFCSTATUS_SUCCESS;
1033       break;
1034     default:
1035       status = NFCSTATUS_FAILED;
1036   }
1037   if (NFCSTATUS_SUCCESS != status) {
1038     NXPLOG_NCIHAL_D("Chip Version Middleware Version mismatch!!!!");
1039   }
1040   return status;
1041 }
1042 /******************************************************************************
1043  * Function         phNxpNciHal_MinOpen_complete
1044  *
1045  * Description      This function updates the status of
1046  *phNxpNciHal_MinOpen_complete to halstatus.
1047  *
1048  * Returns          void.
1049  *
1050  ******************************************************************************/
phNxpNciHal_MinOpen_complete(NFCSTATUS status)1051 static void phNxpNciHal_MinOpen_complete(NFCSTATUS status) {
1052   gsIsFirstHalMinOpen = false;
1053   if (status == NFCSTATUS_SUCCESS) {
1054     nxpncihal_ctrl.halStatus = HAL_STATUS_MIN_OPEN;
1055   }
1056 
1057   return;
1058 }
1059 
1060 /******************************************************************************
1061  * Function         phNxpNciHal_open_complete
1062  *
1063  * Description      This function inform the status of phNxpNciHal_open
1064  *                  function to libnfc-nci.
1065  *
1066  * Returns          void.
1067  *
1068  ******************************************************************************/
phNxpNciHal_open_complete(NFCSTATUS status)1069 static void phNxpNciHal_open_complete(NFCSTATUS status) {
1070   static phLibNfc_Message_t msg;
1071 
1072   if (status == NFCSTATUS_SUCCESS) {
1073     msg.eMsgType = NCI_HAL_OPEN_CPLT_MSG;
1074     nxpncihal_ctrl.hal_open_status = true;
1075     nxpncihal_ctrl.halStatus = HAL_STATUS_OPEN;
1076   } else {
1077     msg.eMsgType = NCI_HAL_ERROR_MSG;
1078   }
1079 
1080   msg.pMsgData = NULL;
1081   msg.Size = 0;
1082 
1083   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
1084                         (phLibNfc_Message_t*)&msg);
1085 
1086   return;
1087 }
1088 
1089 /******************************************************************************
1090  * Function         phNxpNciHal_write
1091  *
1092  * Description      This function write the data to NFCC through physical
1093  *                  interface (e.g. I2C) using the PN54X driver interface.
1094  *                  Before sending the data to NFCC, phNxpNciHal_write_ext
1095  *                  is called to check if there is any extension processing
1096  *                  is required for the NCI packet being sent out.
1097  *
1098  * Returns          It returns number of bytes successfully written to NFCC.
1099  *
1100  ******************************************************************************/
phNxpNciHal_write(uint16_t data_len,const uint8_t * p_data)1101 int phNxpNciHal_write(uint16_t data_len, const uint8_t* p_data) {
1102   if (bEnableMfcExtns && p_data[NCI_GID_INDEX] == 0x00) {
1103     return NxpMfcReaderInstance.Write(data_len, p_data);
1104   } else if (phNxpNciHal_isVendorSpecificCommand(data_len, p_data)) {
1105     return phNxpNciHal_handleVendorSpecificCommand(data_len, p_data);
1106   } else if (isObserveModeEnabled() &&
1107              p_data[NCI_GID_INDEX] == NCI_RF_DISC_COMMD_GID &&
1108              p_data[NCI_OID_INDEX] == NCI_RF_DISC_COMMAND_OID) {
1109     NciDiscoveryCommandBuilder builder;
1110     vector<uint8_t> v_data = builder.reConfigRFDiscCmd(data_len, p_data);
1111     return phNxpNciHal_write_internal(v_data.size(), v_data.data());
1112   }
1113   return phNxpNciHal_write_internal(data_len, p_data);
1114 }
1115 
1116 /******************************************************************************
1117  * Function         phNxpNciHal_write_internal
1118  *
1119  * Description      This function write the data to NFCC through physical
1120  *                  interface (e.g. I2C) using the PN54X driver interface.
1121  *                  Before sending the data to NFCC, phNxpNciHal_write_ext
1122  *                  is called to check if there is any extension processing
1123  *                  is required for the NCI packet being sent out.
1124  *
1125  * Returns          It returns number of bytes successfully written to NFCC.
1126  *
1127  ******************************************************************************/
phNxpNciHal_write_internal(uint16_t data_len,const uint8_t * p_data)1128 int phNxpNciHal_write_internal(uint16_t data_len, const uint8_t* p_data) {
1129   NFCSTATUS status = NFCSTATUS_FAILED;
1130   static phLibNfc_Message_t msg;
1131   if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
1132     return NFCSTATUS_FAILED;
1133   }
1134   if ((data_len + MAX_NXP_HAL_EXTN_BYTES) > NCI_MAX_DATA_LEN) {
1135     NXPLOG_NCIHAL_D("cmd_len exceeds limit NCI_MAX_DATA_LEN");
1136     android_errorWriteLog(0x534e4554, "121267042");
1137     goto clean_and_return;
1138   }
1139 
1140   CONCURRENCY_LOCK();
1141   /* Create local copy of cmd_data */
1142   memcpy(nxpncihal_ctrl.p_cmd_data, p_data, data_len);
1143   nxpncihal_ctrl.cmd_len = data_len;
1144 
1145   /* Check for NXP ext before sending write */
1146   status =
1147       phNxpNciHal_write_ext(&nxpncihal_ctrl.cmd_len, nxpncihal_ctrl.p_cmd_data,
1148                             &nxpncihal_ctrl.rsp_len, nxpncihal_ctrl.p_rsp_data);
1149   if (status != NFCSTATUS_SUCCESS) {
1150     /* Do not send packet to PN54X, send response directly */
1151     msg.eMsgType = NCI_HAL_RX_MSG;
1152     msg.pMsgData = NULL;
1153     msg.Size = 0;
1154 
1155     phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
1156                           (phLibNfc_Message_t*)&msg);
1157     goto clean_and_return;
1158   }
1159 
1160   data_len = phNxpNciHal_write_unlocked(nxpncihal_ctrl.cmd_len,
1161                                         nxpncihal_ctrl.p_cmd_data, ORIG_LIBNFC);
1162 
1163   if (IS_CHIP_TYPE_L(sn100u) && IS_CHIP_TYPE_NE(pn557) && icode_send_eof == 1) {
1164     usleep(10000);
1165     icode_send_eof = 2;
1166     status = phNxpNciHal_send_ext_cmd(3, cmd_icode_eof);
1167     if (status != NFCSTATUS_SUCCESS) {
1168       NXPLOG_NCIHAL_E("ICODE end of frame command failed");
1169     }
1170   }
1171 
1172 clean_and_return:
1173   /* No data written */
1174   CONCURRENCY_UNLOCK();
1175   return data_len;
1176 }
1177 
1178 /******************************************************************************
1179  * Function         phNxpNciHal_write_unlocked
1180  *
1181  * Description      This is the actual function which is being called by
1182  *                  phNxpNciHal_write. This function writes the data to NFCC.
1183  *                  It waits till write callback provide the result of write
1184  *                  process.
1185  *
1186  * Returns          It returns number of bytes successfully written to NFCC.
1187  *
1188  ******************************************************************************/
phNxpNciHal_write_unlocked(uint16_t data_len,const uint8_t * p_data,int origin)1189 int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t* p_data,
1190                                int origin) {
1191   NFCSTATUS status = NFCSTATUS_INVALID_PARAMETER;
1192   phNxpNciHal_Sem_t cb_data;
1193   nxpncihal_ctrl.retry_cnt = 0;
1194   int sem_val = 0;
1195   static uint8_t reset_ntf[] = {0x60, 0x00, 0x06, 0xA0, 0x00,
1196                                 0xC7, 0xD4, 0x00, 0x00};
1197   /* Create the local semaphore */
1198   if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS) {
1199     NXPLOG_NCIHAL_D("phNxpNciHal_write_unlocked Create cb data failed");
1200     data_len = 0;
1201     goto clean_and_return;
1202   }
1203 
1204   /* Create local copy of cmd_data */
1205   memcpy(nxpncihal_ctrl.p_cmd_data, p_data, data_len);
1206   nxpncihal_ctrl.cmd_len = data_len;
1207   write_unlocked_status = NFCSTATUS_FAILED;
1208   /* check for write synchronyztion */
1209   if (phNxpNciHal_check_ncicmd_write_window(nxpncihal_ctrl.cmd_len,
1210                                             nxpncihal_ctrl.p_cmd_data) !=
1211       NFCSTATUS_SUCCESS) {
1212     NXPLOG_NCIHAL_D("phNxpNciHal_write_unlocked  CMD window  check failed");
1213     data_len = 0;
1214     goto clean_and_return;
1215   }
1216 
1217   if (origin == ORIG_NXPHAL) HAL_ENABLE_EXT();
1218 
1219 retry:
1220 
1221   data_len = nxpncihal_ctrl.cmd_len;
1222   if (!phNxpTempMgr::GetInstance().IsICTempOk())
1223     phNxpTempMgr::GetInstance().Wait();
1224 
1225   status = phTmlNfc_Write(
1226       (uint8_t*)nxpncihal_ctrl.p_cmd_data, (uint16_t)nxpncihal_ctrl.cmd_len,
1227       (pphTmlNfc_TransactCompletionCb_t)&phNxpNciHal_write_complete,
1228       (void*)&cb_data);
1229   if (status != NFCSTATUS_PENDING) {
1230     NXPLOG_NCIHAL_E("write_unlocked status error");
1231     data_len = 0;
1232     goto clean_and_return;
1233   }
1234 
1235   /* Wait for callback response */
1236   if (SEM_WAIT(cb_data)) {
1237     NXPLOG_NCIHAL_E("write_unlocked semaphore error");
1238     data_len = 0;
1239     goto clean_and_return;
1240   }
1241 
1242   if (cb_data.status != NFCSTATUS_SUCCESS) {
1243     data_len = 0;
1244     if (nxpncihal_ctrl.retry_cnt++ < MAX_RETRY_COUNT) {
1245       NXPLOG_NCIHAL_D(
1246           "write_unlocked failed - PN54X Maybe in Standby Mode - Retry");
1247       /* 10ms delay to give NFCC wake up delay */
1248       usleep(1000 * 10);
1249       goto retry;
1250     } else {
1251       NXPLOG_NCIHAL_E(
1252           "write_unlocked failed - PN54X Maybe in Standby Mode (max count = "
1253           "0x%x)",
1254           nxpncihal_ctrl.retry_cnt);
1255 
1256       status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
1257 
1258       if (NFCSTATUS_SUCCESS == status) {
1259         NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n");
1260       } else {
1261         NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
1262       }
1263       if (nxpncihal_ctrl.p_nfc_stack_data_cback != NULL &&
1264           nxpncihal_ctrl.hal_open_status == true) {
1265         if (nxpncihal_ctrl.p_rx_data != NULL) {
1266           NXPLOG_NCIHAL_D(
1267               "Send the Core Reset NTF to upper layer, which will trigger the "
1268               "recovery\n");
1269           // Send the Core Reset NTF to upper layer, which will trigger the
1270           // recovery.
1271           abort();
1272           nxpncihal_ctrl.rx_data_len = sizeof(reset_ntf);
1273           memcpy(nxpncihal_ctrl.p_rx_data, reset_ntf, sizeof(reset_ntf));
1274           (*nxpncihal_ctrl.p_nfc_stack_data_cback)(nxpncihal_ctrl.rx_data_len,
1275                                                    nxpncihal_ctrl.p_rx_data);
1276         } else {
1277           (*nxpncihal_ctrl.p_nfc_stack_data_cback)(0x00, NULL);
1278         }
1279         write_unlocked_status = NFCSTATUS_FAILED;
1280       }
1281     }
1282   } else {
1283     write_unlocked_status = NFCSTATUS_SUCCESS;
1284   }
1285 
1286 clean_and_return:
1287   if (write_unlocked_status == NFCSTATUS_FAILED) {
1288     sem_getvalue(&(nxpncihal_ctrl.syncSpiNfc), &sem_val);
1289     if (((nxpncihal_ctrl.p_cmd_data[0] & NCI_MT_MASK) == NCI_MT_CMD) &&
1290         sem_val == 0) {
1291       sem_post(&(nxpncihal_ctrl.syncSpiNfc));
1292       NXPLOG_NCIHAL_D("HAL write  failed CMD window check releasing \n");
1293     }
1294   }
1295   phNxpNciHal_cleanup_cb_data(&cb_data);
1296   return data_len;
1297 }
1298 
1299 /******************************************************************************
1300  * Function         phNxpNciHal_write_complete
1301  *
1302  * Description      This function handles write callback.
1303  *
1304  * Returns          void.
1305  *
1306  ******************************************************************************/
phNxpNciHal_write_complete(void * pContext,phTmlNfc_TransactInfo_t * pInfo)1307 static void phNxpNciHal_write_complete(void* pContext,
1308                                        phTmlNfc_TransactInfo_t* pInfo) {
1309   phNxpNciHal_Sem_t* p_cb_data = (phNxpNciHal_Sem_t*)pContext;
1310   if (pInfo->wStatus == NFCSTATUS_SUCCESS) {
1311     NXPLOG_NCIHAL_D("write successful status = 0x%x", pInfo->wStatus);
1312   } else {
1313     NXPLOG_NCIHAL_D("write error status = 0x%x", pInfo->wStatus);
1314   }
1315 
1316   p_cb_data->status = pInfo->wStatus;
1317 
1318   SEM_POST(p_cb_data);
1319 
1320   return;
1321 }
1322 
1323 /******************************************************************************
1324  * Function         phNxpNciHal_read_complete
1325  *
1326  * Description      This function is called whenever there is an NCI packet
1327  *                  received from NFCC. It could be RSP or NTF packet. This
1328  *                  function provide the received NCI packet to libnfc-nci
1329  *                  using data callback of libnfc-nci.
1330  *                  There is a pending read called from each
1331  *                  phNxpNciHal_read_complete so each a packet received from
1332  *                  NFCC can be provide to libnfc-nci.
1333  *
1334  * Returns          void.
1335  *
1336  ******************************************************************************/
phNxpNciHal_read_complete(void * pContext,phTmlNfc_TransactInfo_t * pInfo)1337 static void phNxpNciHal_read_complete(void* pContext,
1338                                       phTmlNfc_TransactInfo_t* pInfo) {
1339   NFCSTATUS status = NFCSTATUS_FAILED;
1340   int sem_val;
1341   UNUSED_PROP(pContext);
1342   if (nxpncihal_ctrl.read_retry_cnt == 1) {
1343     nxpncihal_ctrl.read_retry_cnt = 0;
1344   }
1345   if (pInfo->wStatus == NFCSTATUS_SUCCESS) {
1346     NXPLOG_NCIHAL_D("read successful status = 0x%x", pInfo->wStatus);
1347 
1348     /*Check the Omapi command response and store in dedicated buffer to solve
1349      * sync issue*/
1350     if (IS_CHIP_TYPE_LE(sn100u) && pInfo->wLength > 2 &&
1351         pInfo->pBuff[0] == 0x4F && pInfo->pBuff[1] == 0x01 &&
1352         pInfo->pBuff[2] == 0x01) {
1353       nxpncihal_ctrl.p_rx_ese_data = pInfo->pBuff;
1354       nxpncihal_ctrl.rx_ese_data_len = pInfo->wLength;
1355       SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1356     } else {
1357       nxpncihal_ctrl.p_rx_data = pInfo->pBuff;
1358       nxpncihal_ctrl.rx_data_len = pInfo->wLength;
1359       status = phNxpNciHal_process_ext_rsp(nxpncihal_ctrl.p_rx_data,
1360                                            &nxpncihal_ctrl.rx_data_len);
1361       if (nxpncihal_ctrl.hal_ext_enabled && phTmlNfc_IsFwDnldModeEnabled()) {
1362         SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1363       }
1364     }
1365     phNxpNciHal_print_res_status(pInfo->pBuff, &pInfo->wLength);
1366     if (nxpncihal_ctrl.power_reset_triggered == true) {
1367       nxpncihal_ctrl.power_reset_triggered = false;
1368     }
1369 
1370     /* Check if response should go to hal module only */
1371     if (nxpncihal_ctrl.hal_ext_enabled == TRUE &&
1372         (nxpncihal_ctrl.p_rx_data[0x00] & NCI_MT_MASK) == NCI_MT_RSP) {
1373       if (status == NFCSTATUS_FAILED) {
1374         NXPLOG_NCIHAL_D("enter into NFCC init recovery");
1375         nxpncihal_ctrl.ext_cb_data.status = status;
1376       }
1377       /* Unlock semaphore only for responses*/
1378       if ((nxpncihal_ctrl.p_rx_data[0x00] & NCI_MT_MASK) == NCI_MT_RSP ||
1379           (IS_CHIP_TYPE_L(sn100u) && (icode_detected == true) &&
1380            (icode_send_eof == 3))) {
1381         /* Unlock semaphore */
1382         SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1383       }
1384     }  // Notification Checking
1385     else if ((nxpncihal_ctrl.hal_ext_enabled == TRUE) &&
1386              ((nxpncihal_ctrl.p_rx_data[0x00] & NCI_MT_MASK) == NCI_MT_NTF) &&
1387              ((nxpncihal_ctrl.p_cmd_data[0x00] & NCI_GID_MASK) ==
1388               (nxpncihal_ctrl.p_rx_data[0x00] & NCI_GID_MASK)) &&
1389              ((nxpncihal_ctrl.p_cmd_data[0x01] & NCI_OID_MASK) ==
1390               (nxpncihal_ctrl.p_rx_data[0x01] & NCI_OID_MASK)) &&
1391              (nxpncihal_ctrl.nci_info.wait_for_ntf == TRUE)) {
1392       /* Unlock semaphore waiting for only  ntf*/
1393       nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
1394       SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1395     } else if (!sendRspToUpperLayer &&
1396                (nxpncihal_ctrl.p_rx_data[0x00] == 0x00)) {
1397       sendRspToUpperLayer = true;
1398       NFCSTATUS mfcRspStatus = NxpMfcReaderInstance.CheckMfcResponse(
1399           nxpncihal_ctrl.p_rx_data, nxpncihal_ctrl.rx_data_len);
1400       NXPLOG_NCIHAL_D("Mfc Response Status = 0x%x", mfcRspStatus);
1401       SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1402     }
1403     /* Read successful send the event to higher layer */
1404     else if (status == NFCSTATUS_SUCCESS) {
1405       phNxpNciHal_client_data_callback();
1406     }
1407     /* Unblock next Write Command Window */
1408     sem_getvalue(&(nxpncihal_ctrl.syncSpiNfc), &sem_val);
1409     if (((pInfo->pBuff[0] & NCI_MT_MASK) == NCI_MT_RSP) && sem_val == 0) {
1410       sem_post(&(nxpncihal_ctrl.syncSpiNfc));
1411     }
1412   } else {
1413     NXPLOG_NCIHAL_E("read error status = 0x%x", pInfo->wStatus);
1414   }
1415 
1416   if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE &&
1417       (nxpncihal_ctrl.p_cmd_data[0x00] & NCI_GID_MASK) ==
1418           (nxpncihal_ctrl.p_rx_data[0x00] & NCI_GID_MASK) &&
1419       (nxpncihal_ctrl.p_cmd_data[0x01] & NCI_OID_MASK) ==
1420           (nxpncihal_ctrl.p_rx_data[0x01] & NCI_OID_MASK) &&
1421       nxpncihal_ctrl.nci_info.wait_for_ntf == FALSE) {
1422     NXPLOG_NCIHAL_D(" Ignoring read , HAL close triggered");
1423     return;
1424   }
1425   /* Read again because read must be pending always except FWDNLD.*/
1426   if (!phTmlNfc_IsFwDnldModeEnabled()) {
1427     status = phTmlNfc_Read(
1428         nxpncihal_ctrl.p_rsp_data, NCI_MAX_DATA_LEN,
1429         (pphTmlNfc_TransactCompletionCb_t)&phNxpNciHal_read_complete, NULL);
1430     if (status != NFCSTATUS_PENDING) {
1431       NXPLOG_NCIHAL_E("read status error status = %x", status);
1432       /* TODO: Not sure how to handle this ? */
1433     }
1434   }
1435   return;
1436 }
1437 
1438 /******************************************************************************
1439  * Function         phNxpNciHal_client_data_callback
1440  *
1441  * Description      This will process the data and sends message to lib-nfc
1442  *                  client via callback
1443  *
1444  * Returns          void
1445  *
1446  ******************************************************************************/
phNxpNciHal_client_data_callback()1447 void phNxpNciHal_client_data_callback() {
1448   if (nxpncihal_ctrl.p_nfc_stack_data_cback == NULL) {
1449     NXPLOG_NCIHAL_E("callback is NULL");
1450     return;
1451   }
1452   NxpMfcReaderInstance.MfcNotifyOnAckReceived(nxpncihal_ctrl.p_rx_data);
1453 
1454   if (isObserveModeEnabled() &&
1455       nxpncihal_ctrl.p_rx_data[NCI_GID_INDEX] == NCI_PROP_NTF_GID &&
1456       nxpncihal_ctrl.p_rx_data[NCI_OID_INDEX] == NCI_PROP_LX_NTF_OID) {
1457     ReaderPollConfigParser readerPollConfigParser;
1458     readerPollConfigParser.setReaderPollCallBack(
1459         nxpncihal_ctrl.p_nfc_stack_data_cback);
1460     readerPollConfigParser.parseAndSendReaderPollInfo(
1461         nxpncihal_ctrl.p_rx_data, nxpncihal_ctrl.rx_data_len);
1462   } else {
1463     (*nxpncihal_ctrl.p_nfc_stack_data_cback)(nxpncihal_ctrl.rx_data_len,
1464                                              nxpncihal_ctrl.p_rx_data);
1465   }
1466   // workaround for sync issue between SPI and NFC
1467   if (IS_CHIP_TYPE_EQ(pn557) && nxpncihal_ctrl.p_rx_data[0] == 0x62 &&
1468       nxpncihal_ctrl.p_rx_data[1] == 0x00 &&
1469       nxpncihal_ctrl.p_rx_data[3] == 0xC0 &&
1470       nxpncihal_ctrl.p_rx_data[4] == 0x00) {
1471     uint8_t nfcee_notifiations[3][9] = {
1472         {0x61, 0x0A, 0x06, 0x01, 0x00, 0x03, 0xC0, 0x80, 0x04},
1473         {0x61, 0x0A, 0x06, 0x01, 0x00, 0x03, 0xC0, 0x81, 0x04},
1474         {0x61, 0x0A, 0x06, 0x01, 0x00, 0x03, 0xC0, 0x82, 0x03},
1475     };
1476 
1477     for (int i = 0; i < 3; i++) {
1478       (*nxpncihal_ctrl.p_nfc_stack_data_cback)(sizeof(nfcee_notifiations[i]),
1479                                                nfcee_notifiations[i]);
1480     }
1481   }
1482 }
1483 
1484 /******************************************************************************
1485  * Function         phNxpNciHal_enableTmlRead
1486  *
1487  * Description      Invokes TmlNfc Read to make sure always read thread is
1488  *                  pending
1489  *
1490  * Returns          Returns read status
1491  *
1492  ******************************************************************************/
phNxpNciHal_enableTmlRead()1493 NFCSTATUS phNxpNciHal_enableTmlRead() {
1494   /* Read again because read must be pending always.*/
1495   NFCSTATUS status = phTmlNfc_Read(
1496       nxpncihal_ctrl.p_rsp_data, NCI_MAX_DATA_LEN,
1497       (pphTmlNfc_TransactCompletionCb_t)&phNxpNciHal_read_complete, NULL);
1498   if (status != NFCSTATUS_PENDING) {
1499     NXPLOG_NCIHAL_E("read status error status = %x", status);
1500   }
1501   return status;
1502 }
1503 /******************************************************************************
1504  * Function         phNxpNciHal_core_initialized
1505  *
1506  * Description      This function is called by libnfc-nci after successful open
1507  *                  of NFCC. All proprietary setting for PN54X are done here.
1508  *                  After completion of proprietary settings notification is
1509  *                  provided to libnfc-nci through callback function.
1510  *
1511  * Returns          Always returns NFCSTATUS_SUCCESS (0).
1512  *
1513  ******************************************************************************/
phNxpNciHal_core_initialized(uint16_t core_init_rsp_params_len,uint8_t * p_core_init_rsp_params)1514 int phNxpNciHal_core_initialized(uint16_t core_init_rsp_params_len,
1515                                  uint8_t* p_core_init_rsp_params) {
1516   NFCSTATUS status = NFCSTATUS_SUCCESS;
1517   uint8_t* buffer = NULL;
1518   uint8_t isfound = 0;
1519   uint8_t fw_dwnld_flag = false;
1520   uint8_t setConfigAlways = false;
1521 
1522   uint8_t swp_full_pwr_mode_on_cmd[] = {0x20, 0x02, 0x05, 0x01,
1523                                         0xA0, 0xF1, 0x01, 0x01};
1524   uint8_t enable_ce_in_phone_off = 0x01;
1525   uint8_t enable_ven_cfg = 0x01;
1526 
1527   uint8_t swp_switch_timeout_cmd[] = {0x20, 0x02, 0x06, 0x01, 0xA0,
1528                                       0xF3, 0x02, 0x00, 0x00};
1529 
1530   config_success = true;
1531   long bufflen = 260;
1532   long retlen = 0;
1533   phNxpNci_EEPROM_info_t mEEPROM_info = {.request_mode = 0};
1534 #if (NFC_NXP_HFO_SETTINGS == TRUE)
1535   /* Temp fix to re-apply the proper clock setting */
1536   int temp_fix = 1;
1537 #endif
1538   unsigned long num = 0;
1539   /*initialize recovery FW variables*/
1540   gRecFwRetryCount = 0;
1541   gRecFWDwnld = 0;
1542   // recovery --start
1543   /*NCI_INIT_CMD*/
1544   static uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
1545   /*NCI_RESET_CMD*/
1546   static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01,
1547                                     0x00};  // keep configuration
1548   static uint8_t cmd_init_nci2_0[] = {0x20, 0x01, 0x02, 0x00, 0x00};
1549   /* reset config cache */
1550   uint8_t retry_core_init_cnt = 0;
1551   if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
1552     return NFCSTATUS_FAILED;
1553   }
1554   if (core_init_rsp_params_len >= 1 && (*p_core_init_rsp_params > 0) &&
1555       (*p_core_init_rsp_params < 4))  // initializing for recovery.
1556   {
1557   retry_core_init:
1558     config_access = false;
1559     if (mGetCfg_info != NULL) {
1560       mGetCfg_info->isGetcfg = false;
1561     }
1562     if (buffer != NULL) {
1563       free(buffer);
1564       buffer = NULL;
1565     }
1566     if (retry_core_init_cnt > 3) {
1567       return NFCSTATUS_FAILED;
1568     }
1569     if (IS_CHIP_TYPE_L(sn100u)) {
1570       status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
1571       if (NFCSTATUS_SUCCESS == status) {
1572         NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n");
1573       } else {
1574         NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
1575       }
1576     }
1577 
1578     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);
1579     if ((status != NFCSTATUS_SUCCESS) &&
1580         (nxpncihal_ctrl.retry_cnt >= MAX_RETRY_COUNT)) {
1581       NXPLOG_NCIHAL_E("Force FW Download, NFCC not coming out from Standby");
1582       retry_core_init_cnt++;
1583       goto retry_core_init;
1584     } else if (status != NFCSTATUS_SUCCESS) {
1585       NXPLOG_NCIHAL_E("NCI_CORE_RESET: Failed");
1586       retry_core_init_cnt++;
1587       goto retry_core_init;
1588     }
1589 
1590     if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
1591       status =
1592           phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci2_0), cmd_init_nci2_0);
1593     } else {
1594       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci), cmd_init_nci);
1595     }
1596     if (status != NFCSTATUS_SUCCESS) {
1597       NXPLOG_NCIHAL_E("NCI_CORE_INIT : Failed");
1598       retry_core_init_cnt++;
1599       goto retry_core_init;
1600     }
1601   }
1602   // recovery --end
1603 
1604   buffer = (uint8_t*)malloc(bufflen * sizeof(uint8_t));
1605   if (NULL == buffer) {
1606     return NFCSTATUS_FAILED;
1607   }
1608   config_access = true;
1609   retlen = 0;
1610   isfound = GetNxpByteArrayValue(NAME_NXP_ACT_PROP_EXTN, (char*)buffer, bufflen,
1611                                  &retlen);
1612   if (isfound > 0 && retlen > 0) {
1613     /* NXP ACT Proprietary Ext */
1614     status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1615     if (status != NFCSTATUS_SUCCESS) {
1616       NXPLOG_NCIHAL_E("NXP ACT Proprietary Ext failed");
1617       retry_core_init_cnt++;
1618       goto retry_core_init;
1619     }
1620   }
1621   if (IS_CHIP_TYPE_EQ(sn100u)) {
1622     uint8_t cmd_get_cfg_dbg_info[] = {0x20, 0x03, 0x0D, 0x06, 0xA0, 0x39,
1623                                       0xA0, 0x1A, 0xA0, 0x1B, 0xA0, 0x1C,
1624                                       0xA0, 0x27, 0xA1, 0x1F};
1625     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_get_cfg_dbg_info),
1626                                       cmd_get_cfg_dbg_info);
1627   } else if (IS_CHIP_TYPE_GE(sn220u) || IS_CHIP_TYPE_EQ(pn557)) {
1628     uint8_t cmd_get_cfg_dbg_info[] = {0x20, 0x03, 0x0B, 0x05, 0xA0, 0x39, 0xA0,
1629                                       0x1A, 0xA0, 0x1B, 0xA0, 0x1C, 0xA0, 0x27};
1630     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_get_cfg_dbg_info),
1631                                       cmd_get_cfg_dbg_info);
1632   }
1633   if (status != NFCSTATUS_SUCCESS) {
1634     NXPLOG_NCIHAL_E("Failed to retrieve NFCC debug info");
1635   }
1636 
1637   if (IS_CHIP_TYPE_GE(sn220u)) {
1638     uint8_t cmd_get_hard_fault_ctr_info[] = {0x20, 0x03, 0x03,
1639                                              0x01, 0xA1, 0x5A};
1640     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_get_hard_fault_ctr_info),
1641                                       cmd_get_hard_fault_ctr_info);
1642     if (status != NFCSTATUS_SUCCESS) {
1643       NXPLOG_NCIHAL_E("Failed to retrieve NFCC hard fault counter debug info");
1644     }
1645   }
1646 
1647   num = 0;
1648   if (GetNxpNumValue("NXP_I3C_MODE", &num, sizeof(num))) {
1649     if (num == 1) {
1650       uint8_t coreStandBy[] = {0x2F, 0x00, 0x01, 0x00};
1651       NXPLOG_NCIHAL_E("Disable NFCC standby");
1652       status = phNxpNciHal_send_ext_cmd(sizeof(coreStandBy), coreStandBy);
1653       if (status != NFCSTATUS_SUCCESS) {
1654         NXPLOG_NCIHAL_E("Failed to set NFCC Standby Disabled");
1655       }
1656     }
1657   }
1658 
1659   status = phNxpNciHal_setAutonomousMode();
1660   if (status != NFCSTATUS_SUCCESS) {
1661     NXPLOG_NCIHAL_E("Set Autonomous enable: Failed");
1662     retry_core_init_cnt++;
1663     goto retry_core_init;
1664   }
1665 
1666   if (IS_CHIP_TYPE_EQ(pn557)) enable_ven_cfg = PN557_VEN_CFG_DEFAULT;
1667 
1668   mEEPROM_info.buffer = &enable_ven_cfg;
1669   mEEPROM_info.bufflen = sizeof(uint8_t);
1670   mEEPROM_info.request_type = EEPROM_ENABLE_VEN_CFG;
1671   mEEPROM_info.request_mode = SET_EEPROM_DATA;
1672   request_EEPROM(&mEEPROM_info);
1673 
1674   if (IS_CHIP_TYPE_GE(sn100u)) {
1675     mEEPROM_info.buffer = &enable_ce_in_phone_off;
1676     mEEPROM_info.bufflen = sizeof(enable_ce_in_phone_off);
1677     mEEPROM_info.request_type = EEPROM_CE_PHONE_OFF_CFG;
1678     mEEPROM_info.request_mode = SET_EEPROM_DATA;
1679     request_EEPROM(&mEEPROM_info);
1680   }
1681 
1682   phNxpNciHal_propConfULPDetMode(false);
1683 
1684   if (gPowerTrackerHandle.start != NULL) {
1685     gPowerTrackerHandle.start(gPowerTrackerHandle.pollDuration);
1686   }
1687   config_access = false;
1688   status = phNxpNciHal_read_fw_dw_status(fw_dwnld_flag);
1689   if (status != NFCSTATUS_SUCCESS) {
1690     NXPLOG_NCIHAL_E("%s: NXP get FW DW Flag failed", __FUNCTION__);
1691   }
1692   fw_dwnld_flag |= (bool)fw_download_success;
1693   if (fw_dwnld_flag == true) {
1694     phNxpNciHal_hci_network_reset();
1695   }
1696   if (IS_CHIP_TYPE_L(sn100u)) {
1697     // Check if firmware download success
1698     status = phNxpNciHal_get_mw_eeprom();
1699     if (status != NFCSTATUS_SUCCESS) {
1700       NXPLOG_NCIHAL_E("NXP GET MW EEPROM AREA Proprietary Ext failed");
1701       retry_core_init_cnt++;
1702       goto retry_core_init;
1703     }
1704   }
1705 
1706   config_access = true;
1707   setConfigAlways = false;
1708   isfound = GetNxpNumValue(NAME_NXP_SET_CONFIG_ALWAYS, &num, sizeof(num));
1709   if (isfound > 0) {
1710     setConfigAlways = num;
1711   }
1712   NXPLOG_NCIHAL_D("EEPROM_fw_dwnld_flag : 0x%02x SetConfigAlways flag : 0x%02x",
1713                   fw_dwnld_flag, setConfigAlways);
1714 
1715   if (isNxpConfigModified() || (fw_dwnld_flag == true)) {
1716     retlen = 0;
1717     fw_download_success = 0;
1718 
1719     /* EEPROM access variables */
1720     mEEPROM_info.request_mode = GET_EEPROM_DATA;
1721     retlen = 0;
1722     memset(buffer, 0x00, bufflen);
1723     isfound = GetNxpByteArrayValue(NAME_NXP_AUTH_TIMEOUT_CFG, (char*)buffer,
1724                                    bufflen, &retlen);
1725 
1726     if ((isfound > 0) && (retlen > 0)) {
1727       uint64_t auth_timeout_buffer_length;
1728       if (IS_CHIP_TYPE_GE(sn100u)) {
1729         auth_timeout_buffer_length = SNXXX_NXP_AUTH_TIMEOUT_BUF_LEN;
1730       } else {
1731         auth_timeout_buffer_length = PN557_NXP_AUTH_TIMEOUT_BUF_LEN;
1732       }
1733       uint8_t auth_timeout_buffer[auth_timeout_buffer_length];
1734       memcpy(&auth_timeout_buffer, buffer, auth_timeout_buffer_length);
1735       mEEPROM_info.request_mode = SET_EEPROM_DATA;
1736       mEEPROM_info.buffer = auth_timeout_buffer;
1737       mEEPROM_info.bufflen = auth_timeout_buffer_length;
1738       mEEPROM_info.request_type = EEPROM_AUTH_CMD_TIMEOUT;
1739       status = request_EEPROM(&mEEPROM_info);
1740       if (NFCSTATUS_SUCCESS == status) {
1741         memcpy(&mGetCfg_info->auth_cmd_timeout, mEEPROM_info.buffer,
1742                mEEPROM_info.bufflen);
1743         mGetCfg_info->auth_cmd_timeoutlen = mEEPROM_info.bufflen;
1744       }
1745     }
1746     NXPLOG_NCIHAL_D("Performing TVDD Settings");
1747     isfound = GetNxpNumValue(NAME_NXP_EXT_TVDD_CFG, &num, sizeof(num));
1748     if (isfound > 0) {
1749       if (num == 1) {
1750         isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_1, (char*)buffer,
1751                                        bufflen, &retlen);
1752         if (isfound && retlen > 0) {
1753           status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1754           if (status != NFCSTATUS_SUCCESS) {
1755             NXPLOG_NCIHAL_E("EXT TVDD CFG 1 Settings failed");
1756             retry_core_init_cnt++;
1757             goto retry_core_init;
1758           }
1759         }
1760       } else if (num == 2) {
1761         isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_2, (char*)buffer,
1762                                        bufflen, &retlen);
1763         if (isfound && retlen > 0) {
1764           status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1765           if (status != NFCSTATUS_SUCCESS) {
1766             NXPLOG_NCIHAL_E("EXT TVDD CFG 2 Settings failed");
1767             retry_core_init_cnt++;
1768             goto retry_core_init;
1769           }
1770         }
1771       } else if (num == 3) {
1772         isfound = GetNxpByteArrayValue(NAME_NXP_EXT_TVDD_CFG_3, (char*)buffer,
1773                                        bufflen, &retlen);
1774         if (isfound && retlen > 0) {
1775           status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1776           if (status != NFCSTATUS_SUCCESS) {
1777             NXPLOG_NCIHAL_E("EXT TVDD CFG 3 Settings failed");
1778             retry_core_init_cnt++;
1779             goto retry_core_init;
1780           }
1781         }
1782       } else {
1783         NXPLOG_NCIHAL_E("Wrong Configuration Value %ld", num);
1784       }
1785     }
1786   }
1787   if ((true == fw_dwnld_flag) || (true == setConfigAlways) ||
1788       isNxpConfigModified()) {
1789     config_access = true;
1790 
1791     if (IS_CHIP_TYPE_NE(pn547C2)) {
1792       config_access = true;
1793     }
1794 
1795     retlen = 0;
1796     /*Select UICC2/UICC3 SWP line from config param*/
1797     if (GetNxpNumValue(NAME_NXP_DEFAULT_UICC2_SELECT, (void*)&retlen,
1798                        sizeof(retlen))) {
1799       if (retlen > 0) phNxpNciHal_enableDefaultUICC2SWPline((uint8_t)retlen);
1800     }
1801     status = phNxpNciHal_setExtendedFieldMode();
1802     if (status != NFCSTATUS_SUCCESS &&
1803         status != NFCSTATUS_FEATURE_NOT_SUPPORTED) {
1804       NXPLOG_NCIHAL_E("phNxpNciHal_setExtendedFieldMode failed");
1805       retry_core_init_cnt++;
1806       goto retry_core_init;
1807     }
1808     status = phNxpNciHal_setGuardTimer();
1809     if (status != NFCSTATUS_SUCCESS &&
1810         status != NFCSTATUS_FEATURE_NOT_SUPPORTED) {
1811       NXPLOG_NCIHAL_E("phNxpNciHal_setGuardTimer failed");
1812       retry_core_init_cnt++;
1813       goto retry_core_init;
1814     }
1815 #if (NXP_SRD == TRUE)
1816     status = phNxpNciHal_setSrdtimeout();
1817     if (status != NFCSTATUS_SUCCESS &&
1818         status != NFCSTATUS_FEATURE_NOT_SUPPORTED) {
1819       NXPLOG_NCIHAL_E("phNxpNciHal_setSrdtimeout failed");
1820       retry_core_init_cnt++;
1821       goto retry_core_init;
1822     }
1823 #endif
1824     config_access = true;
1825     retlen = 0;
1826     NXPLOG_NCIHAL_D("Performing ndef nfcee config settings");
1827     uint8_t cmd_t4t_nfcee_cfg;
1828 
1829     if (!GetNxpNumValue(NAME_NXP_T4T_NFCEE_ENABLE, (void*)&retlen,
1830                         sizeof(retlen))) {
1831       retlen = 0x00;
1832       NXPLOG_NCIHAL_D(
1833           "T4T_NFCEE_ENABLE not found. Taking default value : 0x%02lx", retlen);
1834     }
1835     cmd_t4t_nfcee_cfg = (uint8_t)retlen;
1836     mEEPROM_info.buffer = &cmd_t4t_nfcee_cfg;
1837     mEEPROM_info.bufflen = sizeof(cmd_t4t_nfcee_cfg);
1838     mEEPROM_info.request_type = EEPROM_T4T_NFCEE_ENABLE;
1839     mEEPROM_info.request_mode = SET_EEPROM_DATA;
1840     request_EEPROM(&mEEPROM_info);
1841     if (IS_CHIP_TYPE_GE(sn100u)) {
1842       if (phNxpNciHal_configure_merge_sak() != NFCSTATUS_SUCCESS) {
1843         NXPLOG_NCIHAL_E("Applying iso_dep sak merge settings failed");
1844       }
1845     }
1846   }
1847   if ((true == fw_dwnld_flag) || (true == setConfigAlways) ||
1848       isNxpConfigModified() || (wRfUpdateReq == true)) {
1849     retlen = 0;
1850     NXPLOG_NCIHAL_D("Performing NAME_NXP_CORE_CONF_EXTN Settings");
1851     isfound = GetNxpByteArrayValue(NAME_NXP_CORE_CONF_EXTN, (char*)buffer,
1852                                    bufflen, &retlen);
1853     if (isfound > 0 && retlen > 0) {
1854       /* NXP ACT Proprietary Ext */
1855       status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1856       if (status != NFCSTATUS_SUCCESS) {
1857         NXPLOG_NCIHAL_E("NXP Core configuration failed");
1858         retry_core_init_cnt++;
1859         goto retry_core_init;
1860       }
1861     }
1862 
1863     NXPLOG_NCIHAL_D("Performing SE Settings");
1864     phNxpNciHal_read_and_update_se_state();
1865 
1866     NXPLOG_NCIHAL_D("Performing NAME_NXP_CORE_CONF Settings");
1867     retlen = 0;
1868     isfound = GetNxpByteArrayValue(NAME_NXP_CORE_CONF, (char*)buffer, bufflen,
1869                                    &retlen);
1870     if (isfound > 0 && retlen > 0) {
1871       /* NXP ACT Proprietary Ext */
1872       status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1873       if (status != NFCSTATUS_SUCCESS) {
1874         NXPLOG_NCIHAL_E("Core Set Config failed");
1875         retry_core_init_cnt++;
1876         goto retry_core_init;
1877       }
1878     }
1879 
1880     phNxpNciHal_setDCDCConfig();
1881 
1882     if (fpVerInfoStoreInEeprom != NULL) {
1883       fpVerInfoStoreInEeprom();
1884     }
1885   }
1886   config_access = false;
1887   if ((true == fw_dwnld_flag) || (true == setConfigAlways) ||
1888       isNxpRFConfigModified()) {
1889     unsigned long loopcnt = 0;
1890 
1891     do {
1892       char rf_conf_block[22] = {'\0'};
1893       strlcpy(rf_conf_block, rf_block_name, sizeof(rf_conf_block));
1894       retlen = 0;
1895       strlcat(rf_conf_block, rf_block_num[loopcnt++], sizeof(rf_conf_block));
1896       isfound =
1897           GetNxpByteArrayValue(rf_conf_block, (char*)buffer, bufflen, &retlen);
1898       if (isfound > 0 && retlen > 0) {
1899         NXPLOG_NCIHAL_D(" Performing RF Settings BLK %ld", loopcnt);
1900         status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1901 
1902         if (status == NFCSTATUS_SUCCESS) {
1903           status = phNxpNciHal_CheckRFCmdRespStatus();
1904           /*STATUS INVALID PARAM 0x09*/
1905           if (status == 0x09) {
1906             phNxpNciHalRFConfigCmdRecSequence();
1907             retry_core_init_cnt++;
1908             goto retry_core_init;
1909           }
1910         } else if (status != NFCSTATUS_SUCCESS) {
1911           NXPLOG_NCIHAL_E("RF Settings BLK %ld failed", loopcnt);
1912           retry_core_init_cnt++;
1913           goto retry_core_init;
1914         }
1915       }
1916     } while (rf_block_num[loopcnt] != NULL);
1917     loopcnt = 0;
1918     if (phNxpNciHal_nfccClockCfgApply() != NFCSTATUS_SUCCESS) {
1919       NXPLOG_NCIHAL_E("phNxpNciHal_nfccClockCfgApply failed");
1920       retry_core_init_cnt++;
1921       goto retry_core_init;
1922     }
1923   }
1924   if (fpDoAntennaActivity != NULL) {
1925     fpDoAntennaActivity(ANTENNA_SET_VDDPA);
1926   }
1927   config_access = true;
1928 
1929   retlen = 0;
1930   if (IS_CHIP_TYPE_NE(pn547C2)) {
1931     config_access = false;
1932   }
1933   isfound = GetNxpByteArrayValue(NAME_NXP_CORE_RF_FIELD, (char*)buffer, bufflen,
1934                                  &retlen);
1935   if (isfound > 0 && retlen > 0) {
1936     /* NXP ACT Proprietary Ext */
1937     status = phNxpNciHal_send_ext_cmd(retlen, buffer);
1938     if (status == NFCSTATUS_SUCCESS) {
1939       status = phNxpNciHal_CheckRFCmdRespStatus();
1940       /*STATUS INVALID PARAM 0x09*/
1941       if (status == 0x09) {
1942         phNxpNciHalRFConfigCmdRecSequence();
1943         retry_core_init_cnt++;
1944         goto retry_core_init;
1945       }
1946     } else if (status != NFCSTATUS_SUCCESS) {
1947       NXPLOG_NCIHAL_E("Setting NXP_CORE_RF_FIELD status failed");
1948       retry_core_init_cnt++;
1949       goto retry_core_init;
1950     }
1951   }
1952   config_access = true;
1953 
1954   retlen = 0;
1955   /* NXP SWP switch timeout Setting*/
1956   if (GetNxpNumValue(NAME_NXP_SWP_SWITCH_TIMEOUT, (void*)&retlen,
1957                      sizeof(retlen))) {
1958     // Check the permissible range [0 - 60]
1959     if (0 <= retlen && retlen <= 60) {
1960       if (0 < retlen) {
1961         unsigned int timeout = (uint32_t)retlen * 1000;
1962         unsigned int timeoutHx = 0x0000;
1963 
1964         char tmpbuffer[10] = {0};
1965         snprintf((char*)tmpbuffer, 10, "%04x", timeout);
1966         int ret = sscanf((char*)tmpbuffer, "%x", &timeoutHx);
1967         if (!ret) timeoutHx = 0x0000;
1968 
1969         swp_switch_timeout_cmd[7] = (timeoutHx & 0xFF);
1970         swp_switch_timeout_cmd[8] = ((timeoutHx & 0xFF00) >> 8);
1971       }
1972 
1973       status = phNxpNciHal_send_ext_cmd(sizeof(swp_switch_timeout_cmd),
1974                                         swp_switch_timeout_cmd);
1975       if (status != NFCSTATUS_SUCCESS) {
1976         NXPLOG_NCIHAL_E("SWP switch timeout Setting Failed");
1977         retry_core_init_cnt++;
1978         goto retry_core_init;
1979       }
1980     } else {
1981       NXPLOG_NCIHAL_E("SWP switch timeout Setting Failed - out of range!");
1982     }
1983   }
1984 
1985   status = phNxpNciHal_china_tianjin_rf_setting();
1986   if (status != NFCSTATUS_SUCCESS) {
1987     NXPLOG_NCIHAL_E("phNxpNciHal_china_tianjin_rf_setting failed");
1988     retry_core_init_cnt++;
1989     goto retry_core_init;
1990   }
1991   if (IS_CHIP_TYPE_L(sn100u)) {
1992     // Update eeprom value
1993     status = phNxpNciHal_set_mw_eeprom();
1994     if (status != NFCSTATUS_SUCCESS) {
1995       NXPLOG_NCIHAL_E("NXP Update MW EEPROM Proprietary Ext failed");
1996     }
1997   }
1998 
1999   retlen = 0;
2000   config_access = false;
2001 
2002   retlen = 0;
2003 
2004   /* SWP FULL PWR MODE SETTING ON */
2005   if (GetNxpNumValue(NAME_NXP_SWP_FULL_PWR_ON, (void*)&retlen,
2006                      sizeof(retlen))) {
2007     if (1 == retlen) {
2008       status = phNxpNciHal_send_ext_cmd(sizeof(swp_full_pwr_mode_on_cmd),
2009                                         swp_full_pwr_mode_on_cmd);
2010       if (status != NFCSTATUS_SUCCESS) {
2011         NXPLOG_NCIHAL_E("SWP FULL PWR MODE SETTING ON CMD FAILED");
2012         retry_core_init_cnt++;
2013         goto retry_core_init;
2014       }
2015     } else {
2016       swp_full_pwr_mode_on_cmd[7] = 0x00;
2017       status = phNxpNciHal_send_ext_cmd(sizeof(swp_full_pwr_mode_on_cmd),
2018                                         swp_full_pwr_mode_on_cmd);
2019       if (status != NFCSTATUS_SUCCESS) {
2020         NXPLOG_NCIHAL_E("SWP FULL PWR MODE SETTING OFF CMD FAILED");
2021         retry_core_init_cnt++;
2022         goto retry_core_init;
2023       }
2024     }
2025   }
2026 
2027   uint8_t gpioCtrl[3] = {0x00, 0x00, 0x00};
2028   long gpioCtrlLen = 0;
2029   isfound = GetNxpByteArrayValue(NAME_CONF_GPIO_CONTROL, (char*)gpioCtrl,
2030                                  sizeof(gpioCtrl), &gpioCtrlLen);
2031   if (isfound > 0 && gpioCtrlLen != 0) {
2032     phNxpNciHal_configGPIOControl(gpioCtrl, gpioCtrlLen);
2033   }
2034   phNxpNciHal_configureLxDebugMode();
2035 
2036   if (IS_CHIP_TYPE_EQ(pn557)) {
2037     if (GetNxpNumValue(NAME_NXP_PROP_CE_ACTION_NTF, (void*)&retlen,
2038                        sizeof(retlen))) {
2039       uint8_t value = (uint8_t)retlen;
2040       NXPLOG_NCIHAL_D("Prop CE ACT NTF %x", value);
2041       mEEPROM_info.buffer = &value;
2042       mEEPROM_info.bufflen = sizeof(value);
2043       mEEPROM_info.request_type = EEPROM_CE_ACT_NTF;
2044       mEEPROM_info.request_mode = SET_EEPROM_DATA;
2045       request_EEPROM(&mEEPROM_info);
2046     }
2047   }
2048 
2049   config_access = false;
2050   {
2051     if (isNxpRFConfigModified() || isNxpConfigModified() || fw_dwnld_flag ||
2052         setConfigAlways) {
2053       if (IS_CHIP_TYPE_GE(sn100u)) {
2054         status = phNxpNciHal_ext_send_sram_config_to_flash();
2055         if (status != NFCSTATUS_SUCCESS) {
2056           NXPLOG_NCIHAL_E("Updation of the SRAM contents failed");
2057         }
2058       }
2059       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);
2060       if (status == NFCSTATUS_SUCCESS) {
2061         if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
2062           status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci2_0),
2063                                             cmd_init_nci2_0);
2064         } else {
2065           status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci), cmd_init_nci);
2066         }
2067       }
2068     }
2069     if (status == NFCSTATUS_SUCCESS) {
2070       status = phNxpNciHal_restore_uicc_params();
2071       if (status != NFCSTATUS_SUCCESS) {
2072         NXPLOG_NCIHAL_E("%s: Restore UICC params failed", __FUNCTION__);
2073       }
2074 
2075       phNxpNciHal_prop_conf_rssi();
2076 
2077       fw_dwnld_flag = false;
2078       status = phNxpNciHal_write_fw_dw_status(fw_dwnld_flag);
2079       if (status != NFCSTATUS_SUCCESS) {
2080         NXPLOG_NCIHAL_E("%s: NXP Set FW Download Flag failed", __FUNCTION__);
2081       }
2082       status = phNxpNciHal_send_get_cfgs();
2083       if (status == NFCSTATUS_SUCCESS) {
2084         NXPLOG_NCIHAL_E("Send get Configs SUCCESS");
2085       } else {
2086         NXPLOG_NCIHAL_E("Send get Configs FAILED");
2087       }
2088     }
2089   }
2090   retry_core_init_cnt = 0;
2091 
2092   if (buffer) {
2093     free(buffer);
2094     buffer = NULL;
2095   }
2096   // initialize recovery FW variables
2097   gRecFWDwnld = 0;
2098   gRecFwRetryCount = 0;
2099 
2100   phNxpNciHal_core_initialized_complete(status);
2101   if (isNxpConfigModified()) {
2102     updateNxpConfigTimestamp();
2103   }
2104   if (isNxpRFConfigModified()) {
2105     updateNxpRfConfigTimestamp();
2106   }
2107   return NFCSTATUS_SUCCESS;
2108 }
2109 /******************************************************************************
2110  * Function         phNxpNciHal_CheckRFCmdRespStatus
2111  *
2112  * Description      This function is called to check the resp status of
2113  *                  RF update commands.
2114  *
2115  * Returns          NFCSTATUS_SUCCESS           if successful,
2116  *                  NFCSTATUS_INVALID_PARAMETER if parameter is inavlid
2117  *                  NFCSTATUS_FAILED            if failed response
2118  *
2119  ******************************************************************************/
phNxpNciHal_CheckRFCmdRespStatus()2120 NFCSTATUS phNxpNciHal_CheckRFCmdRespStatus() {
2121   NFCSTATUS status = NFCSTATUS_SUCCESS;
2122   static uint16_t INVALID_PARAM = 0x09;
2123   if ((nxpncihal_ctrl.rx_data_len > 0) && (nxpncihal_ctrl.p_rx_data[2] > 0)) {
2124     if (nxpncihal_ctrl.p_rx_data[3] == 0x09) {
2125       status = INVALID_PARAM;
2126     } else if (nxpncihal_ctrl.p_rx_data[3] != NFCSTATUS_SUCCESS) {
2127       status = NFCSTATUS_FAILED;
2128     }
2129   }
2130   return status;
2131 }
2132 /******************************************************************************
2133  * Function         phNxpNciHalRFConfigCmdRecSequence
2134  *
2135  * Description      This function is called to handle recovery FW sequence
2136  *                  Whenever RF settings are failed to apply with invalid param
2137  *                  response, recovery mechanism includes recovery firmware
2138  *                  download followed by firmware download and then config
2139  *                  settings. The recovery firmware changes the major number of
2140  *                  the firmware inside NFCC.Then actual firmware dowenload will
2141  *                  be successful. This can be retried maximum three times.
2142  *
2143  * Returns          Always returns NFCSTATUS_SUCCESS
2144  *
2145  ******************************************************************************/
phNxpNciHalRFConfigCmdRecSequence()2146 NFCSTATUS phNxpNciHalRFConfigCmdRecSequence() {
2147   NFCSTATUS status = NFCSTATUS_SUCCESS;
2148   uint16_t recFWState = 1;
2149   gRecFWDwnld = true;
2150   gRecFwRetryCount++;
2151   if (gRecFwRetryCount > 0x03) {
2152     NXPLOG_NCIHAL_D("Max retry count for RF config FW recovery exceeded ");
2153     gRecFWDwnld = false;
2154     return NFCSTATUS_FAILED;
2155   }
2156   do {
2157     status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
2158     phDnldNfc_InitImgInfo();
2159     if (NFCSTATUS_SUCCESS == phNxpNciHal_CheckValidFwVersion()) {
2160       status = phNxpNciHal_fw_download();
2161       if (status != NFCSTATUS_SUCCESS) {
2162         NXPLOG_NCIHAL_E("error in download = %x", status);
2163       }
2164       break;
2165     }
2166     gRecFWDwnld = false;
2167   } while (recFWState--);
2168   gRecFWDwnld = false;
2169   return status;
2170 }
2171 
2172 /******************************************************************************
2173  * Function         phNxpNciHal_core_initialized_complete
2174  *
2175  * Description      This function is called when phNxpNciHal_core_initialized
2176  *                  complete all proprietary command exchanges. This function
2177  *                  informs libnfc-nci about completion of core initialize
2178  *                  and result of that through callback.
2179  *
2180  * Returns          void.
2181  *
2182  ******************************************************************************/
phNxpNciHal_core_initialized_complete(NFCSTATUS status)2183 static void phNxpNciHal_core_initialized_complete(NFCSTATUS status) {
2184   static phLibNfc_Message_t msg;
2185 
2186   if (status == NFCSTATUS_SUCCESS) {
2187     msg.eMsgType = NCI_HAL_POST_INIT_CPLT_MSG;
2188   } else {
2189     msg.eMsgType = NCI_HAL_ERROR_MSG;
2190   }
2191   msg.pMsgData = NULL;
2192   msg.Size = 0;
2193 
2194   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
2195                         (phLibNfc_Message_t*)&msg);
2196   return;
2197 }
2198 
2199 /******************************************************************************
2200  * Function         phNxpNciHal_pre_discover
2201  *
2202  * Description      This function is called by libnfc-nci to perform any
2203  *                  proprietary exchange before RF discovery.
2204  *
2205  * Returns          It always returns NFCSTATUS_SUCCESS (0).
2206  *
2207  ******************************************************************************/
phNxpNciHal_pre_discover(void)2208 int phNxpNciHal_pre_discover(void) {
2209   /* Nothing to do here for initial version */
2210   // This is set to return Failed as no vendor specific pre-discovery action is
2211   // needed in case of HalPrediscover
2212   return NFCSTATUS_FAILED;
2213 }
2214 
2215 /******************************************************************************
2216  * Function         phNxpNciHal_release_info
2217  *
2218  * Description      This function frees allocated memory for mGetCfg_info
2219  *
2220  * Returns          void.
2221  *
2222  ******************************************************************************/
phNxpNciHal_release_info(void)2223 static void phNxpNciHal_release_info(void) {
2224   NXPLOG_NCIHAL_D("phNxpNciHal_release_info mGetCfg_info");
2225   if (mGetCfg_info != NULL) {
2226     free(mGetCfg_info);
2227     mGetCfg_info = NULL;
2228   }
2229 }
2230 /******************************************************************************
2231  * Function         phNxpNciHal_close
2232  *
2233  * Description      This function close the NFCC interface and free all
2234  *                  resources.This is called by libnfc-nci on NFC service stop.
2235  *
2236  * Returns          Always return NFCSTATUS_SUCCESS (0).
2237  *
2238  ******************************************************************************/
phNxpNciHal_close(bool bShutdown)2239 int phNxpNciHal_close(bool bShutdown) {
2240   NFCSTATUS status = NFCSTATUS_FAILED;
2241   uint8_t cmd_ce_discovery_nci[10] = {
2242       0x21,
2243       0x03,
2244   };
2245   uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
2246   uint8_t cmd_system_ese_power_cycle[] = {0x2F, 0x1E, 0x00};
2247   uint8_t cmd_ce_in_phone_off[] = {0x20, 0x02, 0x05, 0x01,
2248                                    0xA0, 0x8E, 0x01, 0x00};
2249   uint8_t cmd_ce_in_phone_off_pn557[] = {0x20, 0x02, 0x05, 0x01,
2250                                          0xA0, 0x07, 0x01, 0x02};
2251   uint8_t cmd_system_set_service_status[] = {0x2F, 0x01, 0x01, 0x00};
2252   uint8_t length = 0;
2253   uint8_t numPrms = 0;
2254   uint8_t ptr = 4;
2255   unsigned long uiccListenMask = 0x00;
2256   unsigned long eseListenMask = 0x00;
2257   uint8_t retry = 0;
2258 
2259   phNxpNciHal_deinitializeRegRfFwDnld();
2260   NfcHalAutoThreadMutex a(sHalFnLock);
2261   if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
2262     NXPLOG_NCIHAL_D("phNxpNciHal_close is already closed, ignoring close");
2263     return NFCSTATUS_FAILED;
2264   }
2265   if (gPowerTrackerHandle.stop != NULL) {
2266     gPowerTrackerHandle.stop();
2267   }
2268   if (IS_CHIP_TYPE_L(sn100u)) {
2269     if (!(GetNxpNumValue(NAME_NXP_UICC_LISTEN_TECH_MASK, &uiccListenMask,
2270                          sizeof(uiccListenMask)))) {
2271       uiccListenMask = 0x07;
2272       NXPLOG_NCIHAL_D("UICC_LISTEN_TECH_MASK = 0x%0lX", uiccListenMask);
2273     }
2274 
2275     if (!(GetNxpNumValue(NAME_NXP_ESE_LISTEN_TECH_MASK, &eseListenMask,
2276                          sizeof(eseListenMask)))) {
2277       eseListenMask = 0x07;
2278       NXPLOG_NCIHAL_D("NXP_ESE_LISTEN_TECH_MASK = 0x%0lX", eseListenMask);
2279     }
2280   }
2281 
2282   CONCURRENCY_LOCK();
2283   int sem_val;
2284   sem_getvalue(&(nxpncihal_ctrl.syncSpiNfc), &sem_val);
2285   if (sem_val == 0) {
2286     sem_post(&(nxpncihal_ctrl.syncSpiNfc));
2287   }
2288   if (!bShutdown && phNxpNciHal_getULPDetFlag() == false) {
2289     if (IS_CHIP_TYPE_GE(sn100u)) {
2290       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_ce_in_phone_off),
2291                                         cmd_ce_in_phone_off);
2292       if (status != NFCSTATUS_SUCCESS) {
2293         NXPLOG_NCIHAL_E("CMD_CE_IN_PHONE_OFF: Failed");
2294       }
2295       config_ext.autonomous_mode = 0x00;
2296       status = phNxpNciHal_setAutonomousMode();
2297       if (status != NFCSTATUS_SUCCESS) {
2298         NXPLOG_NCIHAL_E("Autonomous mode Disable: Failed");
2299       }
2300     } else {
2301       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_ce_in_phone_off_pn557),
2302                                         cmd_ce_in_phone_off_pn557);
2303       if (status != NFCSTATUS_SUCCESS) {
2304         NXPLOG_NCIHAL_E("CMD_CE_IN_PHONE_OFF: Failed");
2305       }
2306     }
2307   }
2308   if (nfcFL.nfccFL._NFCC_I2C_READ_WRITE_IMPROVEMENT &&
2309       read_failed_disable_nfc) {
2310     read_failed_disable_nfc = false;
2311     goto close_and_return;
2312   }
2313 
2314   if (write_unlocked_status == NFCSTATUS_FAILED) {
2315     NXPLOG_NCIHAL_D("phNxpNciHal_close i2c write failed .Clean and Return");
2316     goto close_and_return;
2317   }
2318 
2319   if ((!bShutdown) && IS_CHIP_TYPE_L(sn100u)) {
2320     if ((uiccListenMask & 0x1) == 0x01 || (eseListenMask & 0x1) == 0x01) {
2321       NXPLOG_NCIHAL_D("phNxpNciHal_close (): Adding A passive listen");
2322       numPrms++;
2323       cmd_ce_discovery_nci[ptr++] = 0x80;
2324       cmd_ce_discovery_nci[ptr++] = 0x01;
2325       length += 2;
2326     }
2327     if ((uiccListenMask & 0x2) == 0x02 || (eseListenMask & 0x4) == 0x04) {
2328       NXPLOG_NCIHAL_D("phNxpNciHal_close (): Adding B passive listen");
2329       numPrms++;
2330       cmd_ce_discovery_nci[ptr++] = 0x81;
2331       cmd_ce_discovery_nci[ptr++] = 0x01;
2332       length += 2;
2333     }
2334     if ((uiccListenMask & 0x4) == 0x04 || (eseListenMask & 0x4) == 0x04) {
2335       NXPLOG_NCIHAL_D("phNxpNciHal_close (): Adding F passive listen");
2336       numPrms++;
2337       cmd_ce_discovery_nci[ptr++] = 0x82;
2338       cmd_ce_discovery_nci[ptr++] = 0x01;
2339       length += 2;
2340     }
2341 
2342     if (length != 0) {
2343       cmd_ce_discovery_nci[2] = length + 1;
2344       cmd_ce_discovery_nci[3] = numPrms;
2345       status = phNxpNciHal_send_ext_cmd(length + 4, cmd_ce_discovery_nci);
2346       if (status != NFCSTATUS_SUCCESS) {
2347         NXPLOG_NCIHAL_E("CMD_CE_DISC_NCI: Failed");
2348       }
2349     } else {
2350       NXPLOG_NCIHAL_E(
2351           "No changes in the discovery command, sticking to last discovery "
2352           "command sent");
2353     }
2354   } else if ((!bShutdown) && IS_CHIP_TYPE_GE(sn220u)) {
2355     if (phNxpNciHal_getULPDetFlag() == true) {
2356       phNxpNciHal_propConfULPDetMode(true);
2357     }
2358   }
2359 close_and_return:
2360   if (IS_CHIP_TYPE_EQ(sn100u) && bShutdown) {
2361     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_system_ese_power_cycle),
2362                                       cmd_system_ese_power_cycle);
2363     if (status != NFCSTATUS_SUCCESS) {
2364       NXPLOG_NCIHAL_E("ese power cycle failed");
2365     }
2366   }
2367   if (IS_CHIP_TYPE_L(sn220u) || bShutdown) {
2368     nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
2369   }
2370   if (phNxpNciHal_getULPDetFlag() == false) {
2371     do {
2372       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);
2373 
2374       if (status == NFCSTATUS_SUCCESS) {
2375         break;
2376       } else {
2377         NXPLOG_NCIHAL_E("NCI_CORE_RESET: Failed, perform retry after delay");
2378         usleep(1000 * 1000);
2379         if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
2380           // make sure read is pending
2381           NFCSTATUS readStatus = phNxpNciHal_enableTmlRead();
2382           NXPLOG_NCIHAL_D("read status = %x", readStatus);
2383         }
2384         retry++;
2385         if (retry > 3) {
2386           NXPLOG_NCIHAL_E(
2387               "Maximum retries performed, shall restart HAL to recover");
2388           abort();
2389         }
2390       }
2391     } while (1);
2392 
2393     if (IS_CHIP_TYPE_GE(sn220u) && !bShutdown) {
2394       nxpncihal_ctrl.halStatus = HAL_STATUS_CLOSE;
2395       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_system_set_service_status),
2396                                         cmd_system_set_service_status);
2397       if (status != NFCSTATUS_SUCCESS) {
2398         NXPLOG_NCIHAL_E("NCI SYSTEM SET SERVICE STATUS to OFF Failed");
2399       }
2400     }
2401   }
2402 
2403   sem_destroy(&nxpncihal_ctrl.syncSpiNfc);
2404 
2405   if (NULL != gpphTmlNfc_Context->pDevHandle) {
2406     phNxpNciHal_close_complete(NFCSTATUS_SUCCESS);
2407     /* Abort any pending read and write */
2408     status = phTmlNfc_ReadAbort();
2409     status = phTmlNfc_WriteAbort();
2410 
2411     phOsalNfc_Timer_Cleanup();
2412 
2413     status = phTmlNfc_Shutdown();
2414 
2415     if (0 != pthread_join(nxpncihal_ctrl.client_thread, (void**)NULL)) {
2416       NXPLOG_TML_E("Fail to kill client thread!");
2417     }
2418     PhNxpEventLogger::GetInstance().Finalize();
2419     phNxpTempMgr::GetInstance().Reset();
2420     phTmlNfc_CleanUp();
2421 
2422     phDal4Nfc_msgrelease(nxpncihal_ctrl.gDrvCfg.nClientId);
2423 
2424     memset(&nxpncihal_ctrl, 0x00, sizeof(nxpncihal_ctrl));
2425 
2426     NXPLOG_NCIHAL_D("phNxpNciHal_close - phOsalNfc_DeInit completed");
2427   }
2428 
2429   CONCURRENCY_UNLOCK();
2430 
2431   phNxpNciHal_cleanup_monitor();
2432   write_unlocked_status = NFCSTATUS_SUCCESS;
2433   phNxpNciHal_release_info();
2434   /* reset config cache */
2435   resetNxpConfig();
2436   /* Return success always */
2437   return NFCSTATUS_SUCCESS;
2438 }
2439 
2440 /******************************************************************************
2441  * Function         phNxpNciHal_close_complete
2442  *
2443  * Description      This function inform libnfc-nci about result of
2444  *                  phNxpNciHal_close.
2445  *
2446  * Returns          void.
2447  *
2448  ******************************************************************************/
phNxpNciHal_close_complete(NFCSTATUS status)2449 void phNxpNciHal_close_complete(NFCSTATUS status) {
2450   static phLibNfc_Message_t msg;
2451 
2452   if (status == NFCSTATUS_SUCCESS) {
2453     msg.eMsgType = NCI_HAL_CLOSE_CPLT_MSG;
2454   } else {
2455     msg.eMsgType = NCI_HAL_ERROR_MSG;
2456   }
2457   msg.pMsgData = NULL;
2458   msg.Size = 0;
2459   nxpncihal_ctrl.hal_open_status = false;
2460   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, &msg);
2461 
2462   return;
2463 }
2464 
2465 /******************************************************************************
2466  * Function         phNxpNciHal_configDiscShutdown
2467  *
2468  * Description      Enable the CE and VEN config during shutdown.
2469  *
2470  * Returns          Always return NFCSTATUS_SUCCESS (0).
2471  *
2472  ******************************************************************************/
phNxpNciHal_configDiscShutdown(void)2473 int phNxpNciHal_configDiscShutdown(void) {
2474   NFCSTATUS status;
2475   /*NCI_RESET_CMD*/
2476 
2477   uint8_t cmd_disable_disc[] = {0x21, 0x06, 0x01, 0x00};
2478 
2479   uint8_t cmd_ce_disc_nci[] = {0x21, 0x03, 0x07, 0x03, 0x80,
2480                                0x01, 0x81, 0x01, 0x82, 0x01};
2481 
2482   uint8_t cmd_ven_pulld_enable_nci[] = {0x20, 0x02, 0x05, 0x01,
2483                                         0xA0, 0x07, 0x01, 0x03};
2484 
2485   /* Discover map - PROTOCOL_ISO_DEP, PROTOCOL_T3T and MIFARE Classic*/
2486   uint8_t cmd_disc_map[] = {0x21, 0x00, 0x0A, 0x03, 0x04, 0x03, 0x02,
2487                             0x03, 0x02, 0x01, 0x80, 0x01, 0x80};
2488   CONCURRENCY_LOCK();
2489 
2490   status = phNxpNciHal_send_ext_cmd(sizeof(cmd_disable_disc), cmd_disable_disc);
2491   if (status != NFCSTATUS_SUCCESS) {
2492     NXPLOG_NCIHAL_E("CMD_DISABLE_DISCOVERY: Failed");
2493   }
2494   if (IS_CHIP_TYPE_L(sn100u)) {
2495     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_ven_pulld_enable_nci),
2496                                       cmd_ven_pulld_enable_nci);
2497     if (status != NFCSTATUS_SUCCESS) {
2498       NXPLOG_NCIHAL_E("CMD_VEN_PULLD_ENABLE_NCI: Failed");
2499     }
2500   }
2501 
2502   if (IS_CHIP_TYPE_GE(sn100u)) {
2503     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_disc_map), cmd_disc_map);
2504     if (status != NFCSTATUS_SUCCESS) {
2505       NXPLOG_NCIHAL_E("Discovery Map command: Failed");
2506     }
2507     status = phNxpNciHal_ext_send_sram_config_to_flash();
2508     if (status != NFCSTATUS_SUCCESS) {
2509       NXPLOG_NCIHAL_E("Updation of the SRAM contents failed");
2510     }
2511   }
2512   status = phNxpNciHal_send_ext_cmd(sizeof(cmd_ce_disc_nci), cmd_ce_disc_nci);
2513   if (status != NFCSTATUS_SUCCESS) {
2514     NXPLOG_NCIHAL_E("CMD_CE_DISC_NCI: Failed");
2515   }
2516 
2517   CONCURRENCY_UNLOCK();
2518 
2519   status = phNxpNciHal_close(true);
2520   if (status != NFCSTATUS_SUCCESS) {
2521     NXPLOG_NCIHAL_E("NCI_HAL_CLOSE: Failed");
2522   }
2523 
2524   /* Return success always */
2525   return NFCSTATUS_SUCCESS;
2526 }
2527 
2528 /******************************************************************************
2529  * Function         phNxpNciHal_notify_i2c_fragmentation
2530  *
2531  * Description      This function can be used by HAL to inform
2532  *                 libnfc-nci that i2c fragmentation is enabled/disabled
2533  *
2534  * Returns          void.
2535  *
2536  ******************************************************************************/
phNxpNciHal_notify_i2c_fragmentation(void)2537 void phNxpNciHal_notify_i2c_fragmentation(void) {
2538   if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
2539     /*inform libnfc-nci that i2c fragmentation is enabled/disabled */
2540     (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT,
2541                                         HAL_NFC_STATUS_OK);
2542   }
2543 }
2544 /******************************************************************************
2545  * Function         phNxpNciHal_control_granted
2546  *
2547  * Description      Called by libnfc-nci when NFCC control is granted to HAL.
2548  *
2549  * Returns          Always returns NFCSTATUS_SUCCESS (0).
2550  *
2551  ******************************************************************************/
phNxpNciHal_control_granted(void)2552 int phNxpNciHal_control_granted(void) {
2553   /* Take the concurrency lock so no other calls from upper layer
2554    * will be allowed
2555    */
2556   CONCURRENCY_LOCK();
2557 
2558   if (NULL != nxpncihal_ctrl.p_control_granted_cback) {
2559     (*nxpncihal_ctrl.p_control_granted_cback)();
2560   }
2561   /* At the end concurrency unlock so calls from upper layer will
2562    * be allowed
2563    */
2564   CONCURRENCY_UNLOCK();
2565   return NFCSTATUS_SUCCESS;
2566 }
2567 
2568 /******************************************************************************
2569  * Function         phNxpNciHal_request_control
2570  *
2571  * Description      This function can be used by HAL to request control of
2572  *                  NFCC to libnfc-nci. When control is provided to HAL it is
2573  *                  notified through phNxpNciHal_control_granted.
2574  *
2575  * Returns          void.
2576  *
2577  ******************************************************************************/
phNxpNciHal_request_control(void)2578 void phNxpNciHal_request_control(void) {
2579   if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
2580     /* Request Control of NCI Controller from NCI NFC Stack */
2581     (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_REQUEST_CONTROL_EVT,
2582                                         HAL_NFC_STATUS_OK);
2583   }
2584 
2585   return;
2586 }
2587 
2588 /******************************************************************************
2589  * Function         phNxpNciHal_release_control
2590  *
2591  * Description      This function can be used by HAL to release the control of
2592  *                  NFCC back to libnfc-nci.
2593  *
2594  * Returns          void.
2595  *
2596  ******************************************************************************/
phNxpNciHal_release_control(void)2597 void phNxpNciHal_release_control(void) {
2598   if (nxpncihal_ctrl.p_nfc_stack_cback != NULL) {
2599     /* Release Control of NCI Controller to NCI NFC Stack */
2600     (*nxpncihal_ctrl.p_nfc_stack_cback)(HAL_NFC_RELEASE_CONTROL_EVT,
2601                                         HAL_NFC_STATUS_OK);
2602   }
2603 
2604   return;
2605 }
2606 
2607 /******************************************************************************
2608  * Function         phNxpNciHal_power_cycle
2609  *
2610  * Description      This function is called by libnfc-nci when power cycling is
2611  *                  performed. When processing is complete it is notified to
2612  *                  libnfc-nci through phNxpNciHal_power_cycle_complete.
2613  *
2614  * Returns          Always return NFCSTATUS_SUCCESS (0).
2615  *
2616  ******************************************************************************/
phNxpNciHal_power_cycle(void)2617 int phNxpNciHal_power_cycle(void) {
2618   NXPLOG_NCIHAL_D("Power Cycle");
2619   NFCSTATUS status = NFCSTATUS_FAILED;
2620   if (nxpncihal_ctrl.halStatus != HAL_STATUS_OPEN) {
2621     NXPLOG_NCIHAL_D("Power Cycle failed due to hal status not open");
2622     return NFCSTATUS_FAILED;
2623   }
2624   nxpncihal_ctrl.power_reset_triggered = true;
2625   status = phTmlNfc_IoCtl(phTmlNfc_e_PowerReset);
2626 
2627   if (NFCSTATUS_SUCCESS == status) {
2628     NXPLOG_NCIHAL_D("PN54X Reset - SUCCESS\n");
2629   } else {
2630     NXPLOG_NCIHAL_D("PN54X Reset - FAILED\n");
2631   }
2632 
2633   phNxpNciHal_power_cycle_complete(NFCSTATUS_SUCCESS);
2634   return NFCSTATUS_SUCCESS;
2635 }
2636 
2637 /******************************************************************************
2638  * Function         phNxpNciHal_power_cycle_complete
2639  *
2640  * Description      This function is called to provide the status of
2641  *                  phNxpNciHal_power_cycle to libnfc-nci through callback.
2642  *
2643  * Returns          void.
2644  *
2645  ******************************************************************************/
phNxpNciHal_power_cycle_complete(NFCSTATUS status)2646 static void phNxpNciHal_power_cycle_complete(NFCSTATUS status) {
2647   static phLibNfc_Message_t msg;
2648 
2649   if (status == NFCSTATUS_SUCCESS) {
2650     msg.eMsgType = NCI_HAL_OPEN_CPLT_MSG;
2651   } else {
2652     msg.eMsgType = NCI_HAL_ERROR_MSG;
2653   }
2654   msg.pMsgData = NULL;
2655   msg.Size = 0;
2656 
2657   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, &msg);
2658 
2659   return;
2660 }
2661 /******************************************************************************
2662  * Function         phNxpNciHal_check_ncicmd_write_window
2663  *
2664  * Description      This function is called to check the write synchroniztion
2665  *                  status if write already acquired then wait for corresponding
2666                     read to complete.
2667  *
2668  * Returns          return 0 on success and -1 on fail.
2669  *
2670  ******************************************************************************/
2671 
phNxpNciHal_check_ncicmd_write_window(uint16_t cmd_len,uint8_t * p_cmd)2672 int phNxpNciHal_check_ncicmd_write_window(uint16_t cmd_len, uint8_t* p_cmd) {
2673   NFCSTATUS status = NFCSTATUS_FAILED;
2674   int sem_timedout = 2, s;
2675   struct timespec ts;
2676 
2677   if (cmd_len < 1) {
2678     android_errorWriteLog(0x534e4554, "153880357");
2679     return NFCSTATUS_FAILED;
2680   }
2681 
2682   if ((p_cmd[0] & 0xF0) == 0x20) {
2683     clock_gettime(CLOCK_MONOTONIC, &ts);
2684     ts.tv_sec += sem_timedout;
2685     while ((s = sem_timedwait_monotonic_np(&nxpncihal_ctrl.syncSpiNfc, &ts)) == -1 &&
2686            errno == EINTR) {
2687       continue; /* Restart if interrupted by handler */
2688     }
2689     if (s != -1) {
2690       status = NFCSTATUS_SUCCESS;
2691     }
2692   } else {
2693     /* cmd window check not required for writing data packet */
2694     status = NFCSTATUS_SUCCESS;
2695   }
2696   return status;
2697 }
2698 
2699 /******************************************************************************
2700  * Function         phNxpNciHal_ioctl
2701  *
2702  * Description      This function is called by jni when wired mode is
2703  *                  performed.First Pn54x driver will give the access
2704  *                  permission whether wired mode is allowed or not
2705  *                  arg (0):
2706  * Returns          return 0 on success and -1 on fail, On success
2707  *                  update the acutual state of operation in arg pointer
2708  *
2709  ******************************************************************************/
phNxpNciHal_ioctl(long arg,void * p_data)2710 int phNxpNciHal_ioctl(long arg, void* p_data) {
2711   return phNxpNciHal_ioctlIf(arg, p_data);
2712 }
2713 
2714 /******************************************************************************
2715  * Function         phNxpNciHal_nfccClockCfgRead
2716  *
2717  * Description      This function is called for loading a data strcuture from
2718  *                  the config file with clock source and clock frequency values
2719  *
2720  * Returns          void.
2721  *
2722  ******************************************************************************/
phNxpNciHal_nfccClockCfgRead(void)2723 static void phNxpNciHal_nfccClockCfgRead(void) {
2724   unsigned long num = 0;
2725   int isfound = 0;
2726 
2727   nxpprofile_ctrl.bClkSrcVal = 0;
2728   nxpprofile_ctrl.bClkFreqVal = 0;
2729   nxpprofile_ctrl.bTimeout = 0;
2730 
2731   isfound = GetNxpNumValue(NAME_NXP_SYS_CLK_SRC_SEL, &num, sizeof(num));
2732   if (isfound > 0) {
2733     nxpprofile_ctrl.bClkSrcVal = num;
2734   }
2735 
2736   num = 0;
2737   isfound = 0;
2738   isfound = GetNxpNumValue(NAME_NXP_SYS_CLK_FREQ_SEL, &num, sizeof(num));
2739   if (isfound > 0) {
2740     nxpprofile_ctrl.bClkFreqVal = num;
2741   }
2742 
2743   num = 0;
2744   isfound = 0;
2745   isfound = GetNxpNumValue(NAME_NXP_SYS_CLOCK_TO_CFG, &num, sizeof(num));
2746   if (isfound > 0) {
2747     nxpprofile_ctrl.bTimeout = num;
2748   }
2749 
2750   NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkSrcVal = 0x%x",
2751                   nxpprofile_ctrl.bClkSrcVal);
2752   NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkFreqVal = 0x%x",
2753                   nxpprofile_ctrl.bClkFreqVal);
2754   NXPLOG_FWDNLD_D("gphNxpNciHal_fw_IoctlCtx.bClkFreqVal = 0x%x",
2755                   nxpprofile_ctrl.bTimeout);
2756 
2757   if ((nxpprofile_ctrl.bClkSrcVal < CLK_SRC_XTAL) ||
2758       (nxpprofile_ctrl.bClkSrcVal > CLK_SRC_PLL)) {
2759     NXPLOG_FWDNLD_E(
2760         "Clock source value is wrong in config file, setting it as default");
2761     nxpprofile_ctrl.bClkSrcVal = NXP_SYS_CLK_SRC_SEL;
2762   }
2763   if ((nxpprofile_ctrl.bClkFreqVal < CLK_FREQ_13MHZ) ||
2764       (nxpprofile_ctrl.bClkFreqVal > CLK_FREQ_48MHZ)) {
2765     NXPLOG_FWDNLD_E(
2766         "Clock frequency value is wrong in config file, setting it as default");
2767     nxpprofile_ctrl.bClkFreqVal = NXP_SYS_CLK_FREQ_SEL;
2768   }
2769   if ((nxpprofile_ctrl.bTimeout < CLK_TO_CFG_DEF) ||
2770       (nxpprofile_ctrl.bTimeout > CLK_TO_CFG_MAX)) {
2771     NXPLOG_FWDNLD_E(
2772         "Clock timeout value is wrong in config file, setting it as default");
2773     nxpprofile_ctrl.bTimeout = CLK_TO_CFG_DEF;
2774   }
2775 }
2776 
2777 /******************************************************************************
2778  * Function         phNxpNciHal_determineConfiguredClockSrc
2779  *
2780  * Description      This function determines and encodes clock source based on
2781  *                  clock frequency
2782  *
2783  * Returns          encoded form of clock source
2784  *
2785  *****************************************************************************/
phNxpNciHal_determineConfiguredClockSrc()2786 int phNxpNciHal_determineConfiguredClockSrc() {
2787   uint8_t param_clock_src = CLK_SRC_PLL;
2788   if (nxpprofile_ctrl.bClkSrcVal == CLK_SRC_PLL) {
2789     if (IS_CHIP_TYPE_EQ(pn553)) {
2790       param_clock_src = param_clock_src << 3;
2791     } else if (IS_CHIP_TYPE_GE(sn100u)) {
2792       param_clock_src = 0;
2793     }
2794 
2795     if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_13MHZ) {
2796       param_clock_src |= 0x00;
2797     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_19_2MHZ) {
2798       param_clock_src |= 0x01;
2799     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_24MHZ) {
2800       param_clock_src |= 0x02;
2801     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_26MHZ) {
2802       param_clock_src |= 0x03;
2803     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_38_4MHZ) {
2804       param_clock_src |= 0x04;
2805     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_52MHZ) {
2806       param_clock_src |= 0x05;
2807     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_32MHZ) {
2808       param_clock_src |= 0x06;
2809     } else if (nxpprofile_ctrl.bClkFreqVal == CLK_FREQ_48MHZ) {
2810       param_clock_src |= 0x0A;
2811     } else {
2812       NXPLOG_NCIHAL_E("Wrong clock freq, send default PLL@19.2MHz");
2813       if (IS_CHIP_TYPE_L(sn100u))
2814         param_clock_src = 0x11;
2815       else
2816         param_clock_src = 0x01;
2817     }
2818   } else if (nxpprofile_ctrl.bClkSrcVal == CLK_SRC_XTAL) {
2819     param_clock_src = 0x08;
2820 
2821   } else {
2822     NXPLOG_NCIHAL_E("Wrong clock source. Don't apply any modification");
2823   }
2824   return param_clock_src;
2825 }
2826 
2827 /******************************************************************************
2828  * Function         phNxpNciHal_determineConfiguredClockSrc
2829  *
2830  * Description      This function determines and encodes clock source based on
2831  *                  clock frequency
2832  *
2833  * Returns          encoded form of clock source
2834  *
2835  *****************************************************************************/
phNxpNciHal_determineClockDelayRequest(uint8_t nfcc_cfg_clock_src)2836 int phNxpNciHal_determineClockDelayRequest(uint8_t nfcc_cfg_clock_src) {
2837   unsigned long num = 0;
2838   int isfound = 0;
2839   uint8_t nfcc_clock_delay_req = 0;
2840   uint8_t nfcc_clock_set_needed = false;
2841 
2842   isfound = GetNxpNumValue(NAME_NXP_CLOCK_REQ_DELAY, &num, sizeof(num));
2843   if (isfound > 0) {
2844     nxpprofile_ctrl.clkReqDelay = num;
2845   }
2846   if ((nxpprofile_ctrl.clkReqDelay < CLK_REQ_DELAY_MIN) ||
2847       (nxpprofile_ctrl.clkReqDelay > CLK_REQ_DELAY_MAX)) {
2848     NXPLOG_FWDNLD_E(
2849         "default delay to start clock value is wrong in config "
2850         "file, setting it as default");
2851     nxpprofile_ctrl.clkReqDelay = CLK_REQ_DELAY_DEF;
2852     return nfcc_clock_set_needed;
2853   }
2854   nfcc_clock_delay_req = nxpprofile_ctrl.clkReqDelay;
2855 
2856   /*Check if the clock source is XTAL as per config*/
2857   if (nfcc_cfg_clock_src == CLK_CFG_XTAL) {
2858     if (nfcc_clock_delay_req !=
2859         (phNxpNciClock.p_rx_data[CLK_REQ_DELAY_XTAL_OFFSET] &
2860          CLK_REQ_DELAY_MASK)) {
2861       nfcc_clock_set_needed = true;
2862       phNxpNciClock.p_rx_data[CLK_REQ_DELAY_XTAL_OFFSET] &=
2863           ~(CLK_REQ_DELAY_MASK);
2864       phNxpNciClock.p_rx_data[CLK_REQ_DELAY_XTAL_OFFSET] |=
2865           (nfcc_clock_delay_req & CLK_REQ_DELAY_MASK);
2866     }
2867   }
2868   /*Check if the clock source is PLL as per config*/
2869   else if (nfcc_cfg_clock_src < 6) {
2870     if (nfcc_clock_delay_req !=
2871         (phNxpNciClock.p_rx_data[CLK_REQ_DELAY_PLL_OFFSET] &
2872          CLK_REQ_DELAY_MASK)) {
2873       nfcc_clock_set_needed = true;
2874       phNxpNciClock.p_rx_data[CLK_REQ_DELAY_PLL_OFFSET] &=
2875           ~(CLK_REQ_DELAY_MASK);
2876       phNxpNciClock.p_rx_data[CLK_REQ_DELAY_PLL_OFFSET] |=
2877           (nfcc_clock_delay_req & CLK_REQ_DELAY_MASK);
2878     }
2879   }
2880   return nfcc_clock_set_needed;
2881 }
2882 
2883 /******************************************************************************
2884  * Function         phNxpNciHal_nfccClockCfgApply
2885  *
2886  * Description      This function is called after successful download
2887  *                  to check if clock settings in config file and chip
2888  *                  is same
2889  *
2890  * Returns          void.
2891  *
2892  ******************************************************************************/
phNxpNciHal_nfccClockCfgApply(void)2893 NFCSTATUS phNxpNciHal_nfccClockCfgApply(void) {
2894   NFCSTATUS status = NFCSTATUS_SUCCESS;
2895   uint8_t nfcc_cfg_clock_src, nfcc_cur_clock_src;
2896   uint8_t nfcc_clock_set_needed;
2897   uint8_t nfcc_clock_delay_req;
2898   static uint8_t* get_clock_cmd;
2899   uint8_t get_clck_cmd[] = {0x20, 0x03, 0x07, 0x03, 0xA0,
2900                             0x02, 0xA0, 0x03, 0xA0, 0x04};
2901   uint8_t get_clck_cmd_sn100[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x11};
2902   uint8_t set_clck_cmd[] = {0x20, 0x02, 0x0B, 0x01, 0xA0, 0x11, 0x07,
2903                             0x01, 0x0A, 0x32, 0x02, 0x01, 0xF6, 0xF6};
2904   uint8_t get_clk_size = 0;
2905 
2906   if (IS_CHIP_TYPE_L(sn100u)) {
2907     get_clock_cmd = get_clck_cmd;
2908     get_clk_size = sizeof(get_clck_cmd);
2909   } else {
2910     get_clock_cmd = get_clck_cmd_sn100;
2911     get_clk_size = sizeof(get_clck_cmd_sn100);
2912   }
2913   phNxpNciHal_nfccClockCfgRead();
2914   phNxpNciClock.isClockSet = true;
2915   status = phNxpNciHal_send_ext_cmd(get_clk_size, get_clock_cmd);
2916   phNxpNciClock.isClockSet = false;
2917 
2918   if (status != NFCSTATUS_SUCCESS) {
2919     NXPLOG_NCIHAL_E("unable to retrieve get_clk_src_sel");
2920     return status;
2921   }
2922 
2923   nfcc_cfg_clock_src = phNxpNciHal_determineConfiguredClockSrc();
2924   if (IS_CHIP_TYPE_L(sn100u)) {
2925     nfcc_cur_clock_src = phNxpNciClock.p_rx_data[12];
2926   } else {
2927     nfcc_cur_clock_src = phNxpNciClock.p_rx_data[8];
2928   }
2929 
2930   if (IS_CHIP_TYPE_L(sn100u)) {
2931     nfcc_clock_set_needed =
2932         (nfcc_cfg_clock_src != nfcc_cur_clock_src ||
2933          phNxpNciClock.p_rx_data[16] == nxpprofile_ctrl.bTimeout)
2934             ? true
2935             : false;
2936   } else {
2937     nfcc_clock_delay_req =
2938         phNxpNciHal_determineClockDelayRequest(nfcc_cfg_clock_src);
2939     /**Determine clock src is as expected*/
2940     nfcc_clock_set_needed =
2941         ((nfcc_cfg_clock_src != nfcc_cur_clock_src || nfcc_clock_delay_req)
2942              ? true
2943              : false);
2944   }
2945 
2946   if (nfcc_clock_set_needed) {
2947     NXPLOG_NCIHAL_D("Setting Clock Source and Frequency");
2948     if (IS_CHIP_TYPE_L(sn100u)) {
2949       phNxpNciHal_txNfccClockSetCmd();
2950     } else {
2951       /*Read the preset value from FW*/
2952       memcpy(&set_clck_cmd[7], &phNxpNciClock.p_rx_data[8],
2953              phNxpNciClock.p_rx_data[7]);
2954       /*Update clock source and frequency as per DH configuration*/
2955       set_clck_cmd[7] = nfcc_cfg_clock_src;
2956       status = phNxpNciHal_send_ext_cmd(sizeof(set_clck_cmd), set_clck_cmd);
2957     }
2958   }
2959 
2960   return status;
2961 }
2962 
2963 /******************************************************************************
2964  * Function         phNxpNciHal_get_mw_eeprom
2965  *
2966  * Description      This function is called to retrieve data in mw eeprom area
2967  *
2968  * Returns          NFCSTATUS.
2969  *
2970  ******************************************************************************/
phNxpNciHal_get_mw_eeprom(void)2971 static NFCSTATUS phNxpNciHal_get_mw_eeprom(void) {
2972   NFCSTATUS status = NFCSTATUS_SUCCESS;
2973   uint8_t retry_cnt = 0;
2974   static uint8_t get_mw_eeprom_cmd[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x0F};
2975 
2976 retry_send_ext:
2977   if (retry_cnt > 3) {
2978     return NFCSTATUS_FAILED;
2979   }
2980 
2981   phNxpNciMwEepromArea.isGetEepromArea = true;
2982   status =
2983       phNxpNciHal_send_ext_cmd(sizeof(get_mw_eeprom_cmd), get_mw_eeprom_cmd);
2984   if (status != NFCSTATUS_SUCCESS) {
2985     NXPLOG_NCIHAL_D("unable to get the mw eeprom data");
2986     phNxpNciMwEepromArea.isGetEepromArea = false;
2987     retry_cnt++;
2988     goto retry_send_ext;
2989   }
2990   phNxpNciMwEepromArea.isGetEepromArea = false;
2991 
2992   if (phNxpNciMwEepromArea.p_rx_data[12]) {
2993     fw_download_success = 1;
2994   }
2995   return status;
2996 }
2997 
2998 /******************************************************************************
2999  * Function         phNxpNciHal_set_mw_eeprom
3000  *
3001  * Description      This function is called to update data in mw eeprom area
3002  *
3003  * Returns          void.
3004  *
3005  ******************************************************************************/
phNxpNciHal_set_mw_eeprom(void)3006 static NFCSTATUS phNxpNciHal_set_mw_eeprom(void) {
3007   NFCSTATUS status = NFCSTATUS_SUCCESS;
3008   uint8_t retry_cnt = 0;
3009   uint8_t set_mw_eeprom_cmd[39] = {0};
3010   uint8_t cmd_header[] = {0x20, 0x02, 0x24, 0x01, 0xA0, 0x0F, 0x20};
3011 
3012   memcpy(set_mw_eeprom_cmd, cmd_header, sizeof(cmd_header));
3013   phNxpNciMwEepromArea.p_rx_data[12] = 0;
3014   memcpy(set_mw_eeprom_cmd + sizeof(cmd_header), phNxpNciMwEepromArea.p_rx_data,
3015          sizeof(phNxpNciMwEepromArea.p_rx_data));
3016 
3017 retry_send_ext:
3018   if (retry_cnt > 3) {
3019     return NFCSTATUS_FAILED;
3020   }
3021 
3022   status =
3023       phNxpNciHal_send_ext_cmd(sizeof(set_mw_eeprom_cmd), set_mw_eeprom_cmd);
3024   if (status != NFCSTATUS_SUCCESS) {
3025     NXPLOG_NCIHAL_D("unable to update the mw eeprom data");
3026     retry_cnt++;
3027     goto retry_send_ext;
3028   }
3029   return status;
3030 }
3031 
3032 /******************************************************************************
3033  * Function         phNxpNciHal_china_tianjin_rf_setting
3034  *
3035  * Description      This function is called to check RF Setting
3036  *
3037  * Returns          Status.
3038  *
3039  ******************************************************************************/
phNxpNciHal_china_tianjin_rf_setting(void)3040 NFCSTATUS phNxpNciHal_china_tianjin_rf_setting(void) {
3041   NFCSTATUS status = NFCSTATUS_SUCCESS;
3042   int isfound = 0;
3043   unsigned long config_value = 0;
3044   int rf_val = 0;
3045   int flag_send_tianjin_config = true;
3046   int flag_send_transit_config = true;
3047   int flag_send_cmabypass_config = true;
3048   int flag_send_mfc_rf_setting_config = true;
3049   uint8_t retry_cnt = 0;
3050   int enable_bit = 0;
3051 
3052   static uint8_t get_rf_cmd[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x85};
3053   NXPLOG_NCIHAL_D("phNxpNciHal_china_tianjin_rf_setting - Enter");
3054 
3055 retry_send_ext:
3056   if (retry_cnt > 3) {
3057     return NFCSTATUS_FAILED;
3058   }
3059 
3060   phNxpNciRfSet.isGetRfSetting = true;
3061   status = phNxpNciHal_send_ext_cmd(sizeof(get_rf_cmd), get_rf_cmd);
3062   if (status != NFCSTATUS_SUCCESS) {
3063     NXPLOG_NCIHAL_E("unable to get the RF setting");
3064     phNxpNciRfSet.isGetRfSetting = false;
3065     retry_cnt++;
3066     goto retry_send_ext;
3067   }
3068   phNxpNciRfSet.isGetRfSetting = false;
3069   if (phNxpNciRfSet.p_rx_data[3] != 0x00) {
3070     NXPLOG_NCIHAL_E("GET_CONFIG_RSP is FAILED for CHINA TIANJIN");
3071     return status;
3072   }
3073 
3074   /* check if tianjin_rf_setting is required */
3075   rf_val = phNxpNciRfSet.p_rx_data[10];
3076   isfound = (GetNxpNumValue(NAME_NXP_CHINA_TIANJIN_RF_ENABLED,
3077                             (void*)&config_value, sizeof(config_value)));
3078   if (isfound > 0) {
3079     enable_bit = rf_val & 0x40;
3080     if (nfcFL.nfccFL._NFCC_MIFARE_TIANJIN) {
3081       if ((enable_bit != 0x40) && (config_value == 1)) {
3082         phNxpNciRfSet.p_rx_data[10] |= 0x40;  // Enable if it is disabled
3083       } else if ((enable_bit == 0x40) && (config_value == 0)) {
3084         phNxpNciRfSet.p_rx_data[10] &= 0xBF;  // Disable if it is Enabled
3085       } else {
3086         flag_send_tianjin_config = false;  // No need to change in RF setting
3087       }
3088     } else {
3089       enable_bit = phNxpNciRfSet.p_rx_data[11] & 0x10;
3090       if ((config_value == 1) && (enable_bit != 0x10)) {
3091         NXPLOG_NCIHAL_E("Setting Non-Mifare reader for china tianjin");
3092         phNxpNciRfSet.p_rx_data[11] |= 0x10;
3093       } else if ((config_value == 0) && (enable_bit == 0x10)) {
3094         NXPLOG_NCIHAL_E("Setting Non-Mifare reader for china tianjin");
3095         phNxpNciRfSet.p_rx_data[11] &= 0xEF;
3096       } else {
3097         flag_send_tianjin_config = false;
3098       }
3099     }
3100   } else {
3101     flag_send_tianjin_config = false;
3102   }
3103 
3104   config_value = 0;
3105   /*check MFC NACK settings*/
3106   rf_val = phNxpNciRfSet.p_rx_data[9];
3107   isfound = (GetNxpNumValue(NAME_NXP_MIFARE_NACK_TO_RATS_ENABLE,
3108                             (void*)&config_value, sizeof(config_value)));
3109   if (isfound > 0) {
3110     enable_bit = rf_val & 0x20;
3111     if ((enable_bit != 0x20) && (config_value == 1)) {
3112       phNxpNciRfSet.p_rx_data[9] |= 0x20;  // Enable if it is disabled
3113     } else if ((enable_bit == 0x20) && (config_value == 0)) {
3114       phNxpNciRfSet.p_rx_data[9] &= ~0x20;  // Disable if it is Enabled
3115     } else {
3116       flag_send_mfc_rf_setting_config =
3117           false;  // No need to change in RF setting
3118     }
3119   } else {
3120     flag_send_mfc_rf_setting_config = FALSE;  // No need to change in RF setting
3121   }
3122 
3123   config_value = 0;
3124   /*check if china block number check is required*/
3125   rf_val = phNxpNciRfSet.p_rx_data[8];
3126   isfound = (GetNxpNumValue(NAME_NXP_CHINA_BLK_NUM_CHK_ENABLE,
3127                             (void*)&config_value, sizeof(config_value)));
3128   if (isfound > 0) {
3129     enable_bit = rf_val & 0x40;
3130     if ((enable_bit != 0x40) && (config_value == 1)) {
3131       phNxpNciRfSet.p_rx_data[8] |= 0x40;  // Enable if it is disabled
3132     } else if ((enable_bit == 0x40) && (config_value == 0)) {
3133       phNxpNciRfSet.p_rx_data[8] &= ~0x40;  // Disable if it is Enabled
3134     } else {
3135       flag_send_transit_config = false;  // No need to change in RF setting
3136     }
3137   } else {
3138     flag_send_transit_config = FALSE;  // No need to change in RF setting
3139   }
3140 
3141   config_value = 0;
3142   isfound = (GetNxpNumValue(NAME_NXP_CN_TRANSIT_CMA_BYPASSMODE_ENABLE,
3143                             (void*)&config_value, sizeof(config_value)));
3144   if (isfound > 0) {
3145     if (config_value == 0 && ((phNxpNciRfSet.p_rx_data[10] & 0x80) == 0x80)) {
3146       NXPLOG_NCIHAL_D("Disable CMA_BYPASSMODE Supports EMVCo PICC Complaincy");
3147       phNxpNciRfSet.p_rx_data[10] &=
3148           ~0x80;  // set 24th bit of RF MISC SETTING to 0 for EMVCo PICC
3149                   // Complaincy support
3150     } else if (config_value == 1 &&
3151                ((phNxpNciRfSet.p_rx_data[10] & 0x80) == 0)) {
3152       NXPLOG_NCIHAL_D(
3153           "Enable CMA_BYPASSMODE bypass the ISO14443-3A state machine from "
3154           "READY to ACTIVE and backward compatibility with MIfrae Reader ");
3155       phNxpNciRfSet.p_rx_data[10] |=
3156           0x80;  // set 24th bit of RF MISC SETTING to 1 for backward
3157                  // compatibility with MIfrae Reader
3158     } else {
3159       flag_send_cmabypass_config = FALSE;  // No need to change in RF setting
3160     }
3161   } else {
3162     flag_send_cmabypass_config = FALSE;
3163   }
3164 
3165   if (flag_send_tianjin_config || flag_send_transit_config ||
3166       flag_send_cmabypass_config || flag_send_mfc_rf_setting_config) {
3167     static uint8_t set_rf_cmd[] = {0x20, 0x02, 0x08, 0x01, 0xA0, 0x85,
3168                                    0x04, 0x50, 0x08, 0x68, 0x00};
3169     memcpy(&set_rf_cmd[4], &phNxpNciRfSet.p_rx_data[5], 7);
3170     status = phNxpNciHal_send_ext_cmd(sizeof(set_rf_cmd), set_rf_cmd);
3171     if (status != NFCSTATUS_SUCCESS) {
3172       NXPLOG_NCIHAL_E("unable to set the RF setting");
3173       retry_cnt++;
3174       goto retry_send_ext;
3175     }
3176   }
3177 
3178   return status;
3179 }
3180 
3181 /******************************************************************************
3182  * Function         phNxpNciHal_DownloadFw
3183  *
3184  * Description      It is used to trigger the FW download as part of FW tearing
3185  *                  scenario handling. It downloads either degraded or Normal
3186  *                  FW, based on the session state of the NFCC.
3187  *
3188  * Returns          void
3189  *
3190  ******************************************************************************/
phNxpNciHal_DownloadFw(bool isMinFwVer,bool degradedFwDnld)3191 static void phNxpNciHal_DownloadFw(bool isMinFwVer, bool degradedFwDnld) {
3192   NFCSTATUS status = NFCSTATUS_FAILED;
3193   phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
3194   if (isMinFwVer) {
3195     /* since minimal fw required dlreset to boot in Download mode */
3196     status = phNxpNciHal_dlResetInFwDnldMode();
3197     if (status != NFCSTATUS_SUCCESS) {
3198       NXPLOG_NCIHAL_E("DL Reset failed for minimal fw");
3199     }
3200   }
3201   phTmlNfc_EnableFwDnldMode(true);
3202 
3203   /* Set the obtained device handle to download module */
3204   phDnldNfc_SetHwDevHandle();
3205   NXPLOG_NCIHAL_D("Calling Seq handler for FW Download \n");
3206   status = phNxpNciHal_fw_download_seq(nxpprofile_ctrl.bClkSrcVal,
3207                                        nxpprofile_ctrl.bClkFreqVal, 0, false,
3208                                        degradedFwDnld);
3209   if (status != NFCSTATUS_SUCCESS) {
3210     NXPLOG_NCIHAL_E("FW Download Sequence Handler Failed.");
3211   } else {
3212     property_set("nfc.fw.force_download", "0");
3213     fw_download_success = 1;
3214   }
3215 
3216   status = phNxpNciHal_dlResetInFwDnldMode();
3217   if (status != NFCSTATUS_SUCCESS) {
3218     NXPLOG_NCIHAL_E("DL Reset failed in FW DN mode");
3219   }
3220 }
3221 
3222 /******************************************************************************
3223  * Function         phNxpNciHal_CheckAndHandleFwTearDown
3224  *
3225  * Description      Check Whether chip is in FW download mode, If chip is in
3226  *                  Download mode and previous session is not complete, then
3227  *                  Do force FW update.
3228  *
3229  * Returns          void
3230  *
3231  ******************************************************************************/
phNxpNciHal_CheckAndHandleFwTearDown()3232 void phNxpNciHal_CheckAndHandleFwTearDown() {
3233   NFCSTATUS status = NFCSTATUS_FAILED;
3234   uint8_t session_state = -1;
3235   unsigned long minimal_fw_version = DEFAULT_MINIMAL_FW_VERSION;
3236   bool isMinFwVer = false;
3237   status = phNxpNciHal_getChipInfoInFwDnldMode();
3238   if (status != NFCSTATUS_SUCCESS) {
3239     NXPLOG_NCIHAL_E("Get Chip Info Failed");
3240     usleep(150 * 1000);
3241     return;
3242   }
3243   if (!GetNxpNumValue(NAME_NXP_MINIMAL_FW_VERSION, &minimal_fw_version,
3244                       sizeof(minimal_fw_version))) {
3245     /* If config file doesn't contain the info use default */
3246     minimal_fw_version = DEFAULT_MINIMAL_FW_VERSION;
3247   }
3248   if (wFwVerRsp != minimal_fw_version) {
3249     session_state = phNxpNciHal_getSessionInfoInFwDnldMode();
3250     if (session_state == 0) {
3251       NXPLOG_NCIHAL_E("NFC not in the teared state, boot NFCC in NCI mode");
3252       return;
3253     }
3254   } else {
3255     isMinFwVer = true;
3256   }
3257   if (session_state == EOS_FW_SESSION_STATE_LOCKED) {
3258     phNxpNciHal_DownloadFw(isMinFwVer, true);
3259   }
3260   phNxpNciHal_DownloadFw(isMinFwVer);
3261 }
3262 
3263 /******************************************************************************
3264  * Function         phNxpNciHal_getChipInfoInFwDnldMode
3265  *
3266  * Description      Helper function to get the chip info in download mode
3267  *
3268  * Returns          Status
3269  *
3270  ******************************************************************************/
phNxpNciHal_getChipInfoInFwDnldMode(bool bIsVenResetReqd)3271 NFCSTATUS phNxpNciHal_getChipInfoInFwDnldMode(bool bIsVenResetReqd) {
3272   uint8_t get_chip_info_cmd[] = {0x00, 0x04, 0xF1, 0x00,
3273                                  0x00, 0x00, 0x6E, 0xEF};
3274   NFCSTATUS status = NFCSTATUS_FAILED;
3275   int retry_cnt = 0;
3276   if (bIsVenResetReqd) {
3277     status = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadModeWithVenRst);
3278     if (status != NFCSTATUS_SUCCESS) {
3279       NXPLOG_NCIHAL_E("Enable Download mode failed");
3280       return status;
3281     }
3282   }
3283   phTmlNfc_EnableFwDnldMode(true);
3284   do {
3285     status =
3286         phNxpNciHal_send_ext_cmd(sizeof(get_chip_info_cmd), get_chip_info_cmd);
3287     if (status != NFCSTATUS_SUCCESS) {
3288       /* break the loop if HAL write failed or response Timeout */
3289       break;
3290     } else {
3291       /* Check FW getResponse command response status byte */
3292       if (nxpncihal_ctrl.p_rx_data[0] == 0x00) {
3293         if (nxpncihal_ctrl.p_rx_data[2] != 0x00) {
3294           status = NFCSTATUS_FAILED;
3295           /* Resend DL_GET_VERSION_CMD to recover from error
3296            * such as DL_PROTOCOL_ERROR.
3297            */
3298           if (retry_cnt < MAX_RETRY_COUNT) {
3299             retry_cnt++;
3300             /* No default read pending in FW dowbload mode.
3301              * Thus, keep read pending before every cmd retry
3302              */
3303             if (phNxpNciHal_enableTmlRead() != NFCSTATUS_PENDING) {
3304               NXPLOG_NCIHAL_E("%s read error", __func__);
3305             }
3306           }
3307         }
3308       } else {
3309         status = NFCSTATUS_FAILED;
3310         break;
3311       }
3312     }
3313   } while ((status != NFCSTATUS_SUCCESS) && (retry_cnt < MAX_RETRY_COUNT));
3314 
3315   phTmlNfc_EnableFwDnldMode(false);
3316   if (phNxpNciHal_enableTmlRead() != NFCSTATUS_PENDING) {
3317     NXPLOG_NCIHAL_E("%s read status error status", __FUNCTION__);
3318   }
3319   if (status == NFCSTATUS_SUCCESS) {
3320     phNxpNciHal_configFeatureList(nxpncihal_ctrl.p_rx_data,
3321                                   nxpncihal_ctrl.rx_data_len);
3322     wFwVerRsp = pConfigFL->getFWVersionInfo(nxpncihal_ctrl.p_rx_data,
3323                                             nxpncihal_ctrl.rx_data_len);
3324     setNxpFwConfigPath();
3325   }
3326   return status;
3327 }
3328 
3329 /******************************************************************************
3330  * Function         phNxpNciHal_getSessionInfoInFwDnldMode
3331  *
3332  * Description      Helper function to get the session info in download mode
3333  *
3334  * Returns          0 means session closed
3335  *
3336  ******************************************************************************/
phNxpNciHal_getSessionInfoInFwDnldMode()3337 uint8_t phNxpNciHal_getSessionInfoInFwDnldMode() {
3338   uint8_t session_status = -1;
3339   uint8_t get_session_info_cmd[] = {0x00, 0x04, 0xF2, 0x00,
3340                                     0x00, 0x00, 0xF5, 0x33};
3341   phTmlNfc_EnableFwDnldMode(true);
3342   NFCSTATUS status = phNxpNciHal_send_ext_cmd(sizeof(get_session_info_cmd),
3343                                               get_session_info_cmd);
3344   if (status == NFCSTATUS_SUCCESS) {
3345     /* Check FW getResponse command response status byte */
3346     if (nxpncihal_ctrl.p_rx_data[2] == 0x00 &&
3347         nxpncihal_ctrl.p_rx_data[0] == 0x00) {
3348       session_status = nxpncihal_ctrl.p_rx_data[3];
3349     } else {
3350       NXPLOG_NCIHAL_D("get session info Failed !!!");
3351       usleep(150 * 1000);
3352     }
3353   }
3354   status = phNxpNciHal_dlResetInFwDnldMode();
3355   if (status != NFCSTATUS_SUCCESS) {
3356     NXPLOG_NCIHAL_E("DL Reset failed in FW DN mode");
3357   }
3358   return session_status;
3359 }
3360 
3361 /******************************************************************************
3362  * Function         phNxpNciHal_dlResetInFwDnldMode
3363  *
3364  * Description      Helper function to change the mode from FW to NCI
3365  *
3366  * Returns          Status
3367  *
3368  ******************************************************************************/
phNxpNciHal_dlResetInFwDnldMode()3369 NFCSTATUS phNxpNciHal_dlResetInFwDnldMode() {
3370   NFCSTATUS status = NFCSTATUS_FAILED;
3371   phTmlNfc_EnableFwDnldMode(true);
3372   NXPLOG_NCIHAL_D("Sending DL Reset for NFCC soft reboot");
3373   phDnldNfc_SetHwDevHandle();
3374 
3375   status = phNxpNciHal_fw_dnld_switch_normal_mode();
3376 
3377   phTmlNfc_EnableFwDnldMode(false);
3378   phTmlNfc_ReadAbort();
3379   phDnldNfc_ReSetHwDevHandle();
3380   if (phNxpNciHal_enableTmlRead() != NFCSTATUS_PENDING) {
3381     NXPLOG_NCIHAL_E("%s read status error status", __FUNCTION__);
3382     status = NFCSTATUS_FAILED;
3383   }
3384   return status;
3385 }
3386 
3387 /******************************************************************************
3388  * Function         phNxpNciHal_gpio_restore
3389  *
3390  * Description      This function restores the gpio values into eeprom
3391  *
3392  * Returns          void
3393  *
3394  ******************************************************************************/
phNxpNciHal_gpio_restore(phNxpNciHal_GpioInfoState state)3395 static void phNxpNciHal_gpio_restore(phNxpNciHal_GpioInfoState state) {
3396   NFCSTATUS status = NFCSTATUS_SUCCESS;
3397   uint8_t get_gpio_values_cmd[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x00};
3398   uint8_t set_gpio_values_cmd[] = {
3399       0x20, 0x02, 0x00, 0x01, 0xA0, 0x00, 0x20, 0x00, 0x00, 0x00,
3400       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3401       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3402       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3403 
3404   if (state == GPIO_STORE) {
3405     nxpncihal_ctrl.phNxpNciGpioInfo.state = GPIO_STORE;
3406     get_gpio_values_cmd[5] = 0x08;
3407     status = phNxpNciHal_send_ext_cmd(sizeof(get_gpio_values_cmd),
3408                                       get_gpio_values_cmd);
3409     if (status != NFCSTATUS_SUCCESS) {
3410       NXPLOG_NCIHAL_E("Failed to get GPIO values!!!\n");
3411       return;
3412     }
3413 
3414     nxpncihal_ctrl.phNxpNciGpioInfo.state = GPIO_STORE_DONE;
3415     set_gpio_values_cmd[2] = 0x24;
3416     set_gpio_values_cmd[5] = 0x14;
3417     set_gpio_values_cmd[7] = nxpncihal_ctrl.phNxpNciGpioInfo.values[0];
3418     set_gpio_values_cmd[8] = nxpncihal_ctrl.phNxpNciGpioInfo.values[1];
3419     status = phNxpNciHal_send_ext_cmd(sizeof(set_gpio_values_cmd),
3420                                       set_gpio_values_cmd);
3421     if (status != NFCSTATUS_SUCCESS) {
3422       NXPLOG_NCIHAL_E("Failed to set GPIO values!!!\n");
3423       return;
3424     }
3425   } else if (state == GPIO_RESTORE) {
3426     nxpncihal_ctrl.phNxpNciGpioInfo.state = GPIO_RESTORE;
3427     get_gpio_values_cmd[5] = 0x14;
3428     status = phNxpNciHal_send_ext_cmd(sizeof(get_gpio_values_cmd),
3429                                       get_gpio_values_cmd);
3430     if (status != NFCSTATUS_SUCCESS) {
3431       NXPLOG_NCIHAL_E("Failed to get GPIO values!!!\n");
3432       return;
3433     }
3434 
3435     nxpncihal_ctrl.phNxpNciGpioInfo.state = GPIO_RESTORE_DONE;
3436     set_gpio_values_cmd[2] = 0x06;
3437     set_gpio_values_cmd[5] = 0x08;  // update TAG
3438     set_gpio_values_cmd[6] = 0x02;  // update length
3439     set_gpio_values_cmd[7] = nxpncihal_ctrl.phNxpNciGpioInfo.values[0];
3440     set_gpio_values_cmd[8] = nxpncihal_ctrl.phNxpNciGpioInfo.values[1];
3441     status = phNxpNciHal_send_ext_cmd(9, set_gpio_values_cmd);
3442     if (status != NFCSTATUS_SUCCESS) {
3443       NXPLOG_NCIHAL_E("Failed to set GPIO values!!!\n");
3444       return;
3445     }
3446   } else {
3447     NXPLOG_NCIHAL_E("GPIO Restore Invalid Option!!!\n");
3448   }
3449 }
3450 
3451 /******************************************************************************
3452  * Function         phNxpNciHal_nfcc_core_reset_init
3453  *
3454  * Description      Helper function to do nfcc core reset & core init
3455  *
3456  * Returns          Status
3457  *
3458  ******************************************************************************/
phNxpNciHal_nfcc_core_reset_init(bool keep_config)3459 NFCSTATUS phNxpNciHal_nfcc_core_reset_init(bool keep_config) {
3460   NFCSTATUS status = NFCSTATUS_FAILED;
3461   uint8_t retry_cnt = 0;
3462   uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x01};
3463 
3464   if (keep_config) {
3465     cmd_reset_nci[3] = 0x00;
3466   }
3467 retry_core_reset:
3468   status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);
3469   if ((status != NFCSTATUS_SUCCESS) && (retry_cnt < 3)) {
3470     NXPLOG_NCIHAL_D("Retry: NCI_CORE_RESET");
3471     retry_cnt++;
3472     goto retry_core_reset;
3473   } else if (status != NFCSTATUS_SUCCESS) {
3474     NXPLOG_NCIHAL_E("NCI_CORE_RESET failed!!!\n");
3475     return status;
3476   }
3477 
3478   retry_cnt = 0;
3479   uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
3480   uint8_t cmd_init_nci2_0[] = {0x20, 0x01, 0x02, 0x00, 0x00};
3481 retry_core_init:
3482   if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
3483     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci2_0), cmd_init_nci2_0);
3484   } else {
3485     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci), cmd_init_nci);
3486   }
3487 
3488   if ((status != NFCSTATUS_SUCCESS) && (retry_cnt < 3)) {
3489     NXPLOG_NCIHAL_D("Retry: NCI_CORE_INIT\n");
3490     retry_cnt++;
3491     goto retry_core_init;
3492   } else if (status != NFCSTATUS_SUCCESS) {
3493     NXPLOG_NCIHAL_E("NCI_CORE_INIT failed!!!\n");
3494     return status;
3495   }
3496 
3497   return status;
3498 }
3499 
3500 /******************************************************************************
3501  * Function         phNxpNciHal_resetDefaultSettings
3502  *
3503  * Description      Helper function to do nfcc core reset, core init
3504  *                  (if previously firmware update was triggered) and
3505  *                  apply default NFC settings
3506  *
3507  * Returns          Status
3508  *
3509  ******************************************************************************/
phNxpNciHal_resetDefaultSettings(uint8_t fw_update_req,bool keep_config)3510 NFCSTATUS phNxpNciHal_resetDefaultSettings(uint8_t fw_update_req,
3511                                            bool keep_config) {
3512   NFCSTATUS status = NFCSTATUS_SUCCESS;
3513   if (fw_update_req) {
3514     status = phNxpNciHal_nfcc_core_reset_init(keep_config);
3515   }
3516   if (status == NFCSTATUS_SUCCESS) {
3517     unsigned long num = 0;
3518     int ret = 0;
3519     phNxpNciHal_conf_nfc_forum_mode();
3520     if (IS_CHIP_TYPE_GE(sn100u)) {
3521       ret = GetNxpNumValue(NAME_NXP_RDR_DISABLE_ENABLE_LPCD, &num, sizeof(num));
3522       if (!ret || num == 1 || num == 2) {
3523         phNxpNciHal_prop_conf_lpcd(true);
3524       } else if (ret && num == 0) {
3525         phNxpNciHal_prop_conf_lpcd(false);
3526       }
3527     }
3528   }
3529   return status;
3530 }
3531 
3532 /******************************************************************************
3533  * Function         phNxpNciHal_enable_i2c_fragmentation
3534  *
3535  * Description      This function is called to process the response status
3536  *                  and print the status byte.
3537  *
3538  * Returns          void.
3539  *
3540  ******************************************************************************/
phNxpNciHal_enable_i2c_fragmentation()3541 void phNxpNciHal_enable_i2c_fragmentation() {
3542   NFCSTATUS status = NFCSTATUS_FAILED;
3543   static uint8_t fragmentation_enable_config_cmd[] = {0x20, 0x02, 0x05, 0x01,
3544                                                       0xA0, 0x05, 0x01, 0x10};
3545   long i2c_status = 0x00;
3546   long config_i2c_vlaue = 0xff;
3547   /*NCI_RESET_CMD*/
3548   static uint8_t cmd_reset_nci[] = {0x20, 0x00, 0x01, 0x00};
3549   /*NCI_INIT_CMD*/
3550   static uint8_t cmd_init_nci[] = {0x20, 0x01, 0x00};
3551   static uint8_t cmd_init_nci2_0[] = {0x20, 0x01, 0x02, 0x00, 0x00};
3552   static uint8_t get_i2c_fragmentation_cmd[] = {0x20, 0x03, 0x03,
3553                                                 0x01, 0xA0, 0x05};
3554   if (GetNxpNumValue(NAME_NXP_I2C_FRAGMENTATION_ENABLED, (void*)&i2c_status,
3555                      sizeof(i2c_status)) == true) {
3556     NXPLOG_FWDNLD_D("I2C status : %ld", i2c_status);
3557   } else {
3558     NXPLOG_FWDNLD_E("I2C status read not succeeded. Default value : %ld",
3559                     i2c_status);
3560   }
3561   status = phNxpNciHal_send_ext_cmd(sizeof(get_i2c_fragmentation_cmd),
3562                                     get_i2c_fragmentation_cmd);
3563   if (status != NFCSTATUS_SUCCESS) {
3564     NXPLOG_NCIHAL_E("unable to retrieve  get_i2c_fragmentation_cmd");
3565   } else {
3566     if (nxpncihal_ctrl.p_rx_data[8] == 0x10) {
3567       config_i2c_vlaue = 0x01;
3568       phNxpNciHal_notify_i2c_fragmentation();
3569       phTmlNfc_set_fragmentation_enabled(I2C_FRAGMENTATION_ENABLED);
3570     } else if (nxpncihal_ctrl.p_rx_data[8] == 0x00) {
3571       config_i2c_vlaue = 0x00;
3572     }
3573     // if the value already matches, nothing to be done
3574     if (config_i2c_vlaue != i2c_status) {
3575       if (i2c_status == 0x01) {
3576         /* NXP I2C fragmenation enabled*/
3577         status =
3578             phNxpNciHal_send_ext_cmd(sizeof(fragmentation_enable_config_cmd),
3579                                      fragmentation_enable_config_cmd);
3580         if (status != NFCSTATUS_SUCCESS) {
3581           NXPLOG_NCIHAL_E("NXP fragmentation enable failed");
3582         }
3583       } else if (i2c_status == 0x00 || config_i2c_vlaue == 0xff) {
3584         fragmentation_enable_config_cmd[7] = 0x00;
3585         /* NXP I2C fragmentation disabled*/
3586         status =
3587             phNxpNciHal_send_ext_cmd(sizeof(fragmentation_enable_config_cmd),
3588                                      fragmentation_enable_config_cmd);
3589         if (status != NFCSTATUS_SUCCESS) {
3590           NXPLOG_NCIHAL_E("NXP fragmentation disable failed");
3591         }
3592       }
3593       status = phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_nci), cmd_reset_nci);
3594       if (status != NFCSTATUS_SUCCESS) {
3595         NXPLOG_NCIHAL_E("NCI_CORE_RESET: Failed");
3596       }
3597       if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
3598         status =
3599             phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci2_0), cmd_init_nci2_0);
3600       } else {
3601         status = phNxpNciHal_send_ext_cmd(sizeof(cmd_init_nci), cmd_init_nci);
3602       }
3603       if (status != NFCSTATUS_SUCCESS) {
3604         NXPLOG_NCIHAL_E("NCI_CORE_INIT : Failed");
3605       } else if (i2c_status == 0x01) {
3606         phNxpNciHal_notify_i2c_fragmentation();
3607         phTmlNfc_set_fragmentation_enabled(I2C_FRAGMENTATION_ENABLED);
3608       }
3609     }
3610   }
3611 }
3612 /******************************************************************************
3613  * Function         phNxpNciHal_do_se_session_reset
3614  *
3615  * Description      This function is called to set the session id to default
3616  *                  value.
3617  *
3618  * Returns          NFCSTATUS.
3619  *
3620  ******************************************************************************/
phNxpNciHal_do_swp_session_reset(void)3621 static NFCSTATUS phNxpNciHal_do_swp_session_reset(void) {
3622   NFCSTATUS status = NFCSTATUS_FAILED;
3623   static uint8_t reset_swp_session_identity_set[] = {
3624       0x20, 0x02, 0x17, 0x02, 0xA0, 0xEA, 0x08, 0xFF, 0xFF,
3625       0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xA0, 0x1E, 0x08,
3626       0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
3627   status = phNxpNciHal_send_ext_cmd(sizeof(reset_swp_session_identity_set),
3628                                     reset_swp_session_identity_set);
3629   if (status != NFCSTATUS_SUCCESS) {
3630     NXPLOG_NCIHAL_E("NXP reset_ese_session_identity_set command failed");
3631   }
3632   return status;
3633 }
3634 /******************************************************************************
3635  * Function         phNxpNciHal_do_factory_reset
3636  *
3637  * Description      This function is called during factory reset to clear/reset
3638  *                  nfc sub-system persistent data.
3639  *
3640  * Returns          void.
3641  *
3642  ******************************************************************************/
phNxpNciHal_do_factory_reset(void)3643 void phNxpNciHal_do_factory_reset(void) {
3644   NFCSTATUS status = NFCSTATUS_FAILED;
3645   // After factory reset phone will turnoff so mutex not required here.
3646   if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
3647     status = phNxpNciHal_MinOpen();
3648     if (status != NFCSTATUS_SUCCESS) {
3649       NXPLOG_NCIHAL_E("%s: NXP Nfc Open failed", __func__);
3650       return;
3651     }
3652     phNxpNciHal_deinitializeRegRfFwDnld();
3653   }
3654   status = phNxpNciHal_do_swp_session_reset();
3655   if (status != NFCSTATUS_SUCCESS) {
3656     NXPLOG_NCIHAL_E("%s failed. status = %x ", __func__, status);
3657   }
3658 }
3659 /******************************************************************************
3660  * Function         phNxpNciHal_hci_network_reset
3661  *
3662  * Description      This function resets the session id's of all the se's
3663  *                  in the HCI network and notify to HCI_NETWORK_RESET event to
3664  *                  NFC HAL Client.
3665  *
3666  * Returns          void.
3667  *
3668  ******************************************************************************/
phNxpNciHal_hci_network_reset(void)3669 static void phNxpNciHal_hci_network_reset(void) {
3670   static phLibNfc_Message_t msg;
3671   msg.pMsgData = NULL;
3672   msg.Size = 0;
3673 
3674   NFCSTATUS status = phNxpNciHal_do_swp_session_reset();
3675 
3676   if (status != NFCSTATUS_SUCCESS) {
3677     msg.eMsgType = NCI_HAL_ERROR_MSG;
3678   } else {
3679     msg.eMsgType = NCI_HAL_HCI_NETWORK_RESET_MSG;
3680   }
3681   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, &msg);
3682 }
3683 /******************************************************************************
3684  * Function         phNxpNciHal_print_res_status
3685  *
3686  * Description      This function is called to process the response status
3687  *                  and print the status byte.
3688  *
3689  * Returns          void.
3690  *
3691  ******************************************************************************/
phNxpNciHal_print_res_status(uint8_t * p_rx_data,uint16_t * p_len)3692 static void phNxpNciHal_print_res_status(uint8_t* p_rx_data, uint16_t* p_len) {
3693   static uint8_t response_buf[][30] = {"STATUS_OK",
3694                                        "STATUS_REJECTED",
3695                                        "STATUS_RF_FRAME_CORRUPTED",
3696                                        "STATUS_FAILED",
3697                                        "STATUS_NOT_INITIALIZED",
3698                                        "STATUS_SYNTAX_ERROR",
3699                                        "STATUS_SEMANTIC_ERROR",
3700                                        "RFU",
3701                                        "RFU",
3702                                        "STATUS_INVALID_PARAM",
3703                                        "STATUS_MESSAGE_SIZE_EXCEEDED",
3704                                        "STATUS_UNDEFINED"};
3705   int status_byte;
3706   if (p_rx_data[0] == 0x40 && (p_rx_data[1] == 0x02 || p_rx_data[1] == 0x03)) {
3707     if (p_rx_data[2] && p_rx_data[3] <= 10) {
3708       status_byte = p_rx_data[CORE_RES_STATUS_BYTE];
3709       NXPLOG_NCIHAL_D("%s: response status =%s", __func__,
3710                       response_buf[status_byte]);
3711     } else {
3712       NXPLOG_NCIHAL_D("%s: response status =%s", __func__, response_buf[11]);
3713     }
3714     if (phNxpNciClock.isClockSet) {
3715       int i, len = sizeof(phNxpNciClock.p_rx_data);
3716       if (*p_len > len) {
3717         android_errorWriteLog(0x534e4554, "169257710");
3718       } else {
3719         len = *p_len;
3720       }
3721       for (i = 0; i < len; i++) {
3722         phNxpNciClock.p_rx_data[i] = p_rx_data[i];
3723       }
3724     }
3725 
3726     else if (phNxpNciRfSet.isGetRfSetting) {
3727       int i, len = sizeof(phNxpNciRfSet.p_rx_data);
3728       if (*p_len > len) {
3729         android_errorWriteLog(0x534e4554, "169258733");
3730       } else {
3731         len = *p_len;
3732       }
3733       for (i = 0; i < len; i++) {
3734         phNxpNciRfSet.p_rx_data[i] = p_rx_data[i];
3735         // NXPLOG_NCIHAL_D("%s: response status =0x%x",__func__,p_rx_data[i]);
3736       }
3737     } else if (phNxpNciMwEepromArea.isGetEepromArea) {
3738       int i, len = sizeof(phNxpNciMwEepromArea.p_rx_data) + 8;
3739       if (*p_len > len) {
3740         android_errorWriteLog(0x534e4554, "169258884");
3741       } else {
3742         len = *p_len;
3743       }
3744       for (i = 8; i < len; i++) {
3745         phNxpNciMwEepromArea.p_rx_data[i - 8] = p_rx_data[i];
3746       }
3747     } else if (nxpncihal_ctrl.phNxpNciGpioInfo.state == GPIO_STORE) {
3748       NXPLOG_NCIHAL_D("%s: Storing GPIO Values...", __func__);
3749       nxpncihal_ctrl.phNxpNciGpioInfo.values[0] = p_rx_data[9];
3750       nxpncihal_ctrl.phNxpNciGpioInfo.values[1] = p_rx_data[8];
3751     } else if (nxpncihal_ctrl.phNxpNciGpioInfo.state == GPIO_RESTORE) {
3752       NXPLOG_NCIHAL_D("%s: Restoring GPIO Values...", __func__);
3753       nxpncihal_ctrl.phNxpNciGpioInfo.values[0] = p_rx_data[9];
3754       nxpncihal_ctrl.phNxpNciGpioInfo.values[1] = p_rx_data[8];
3755     }
3756   }
3757 
3758   if (p_rx_data[2] && (config_access == true)) {
3759     if (p_rx_data[3] != NFCSTATUS_SUCCESS) {
3760       NXPLOG_NCIHAL_W("Invalid Data from config file.");
3761       config_success = false;
3762     }
3763   }
3764 }
3765 /******************************************************************************
3766  * Function         phNxpNciHal_initialize_mifare_flag
3767  *
3768  * Description      This function gets the value for Mfc flags.
3769  *
3770  * Returns          void
3771  *
3772  ******************************************************************************/
phNxpNciHal_initialize_mifare_flag()3773 static void phNxpNciHal_initialize_mifare_flag() {
3774   unsigned long num = 0;
3775   bEnableMfcReader = false;
3776   // 1: Enable Mifare Classic protocol in RF Discovery.
3777   // 0: Remove Mifare Classic protocol in RF Discovery.
3778   if (GetNxpNumValue(NAME_MIFARE_READER_ENABLE, &num, sizeof(num))) {
3779     bEnableMfcReader = (num == 0) ? false : true;
3780   }
3781 }
3782 
3783 /*****************************************************************************
3784  * Function         phNxpNciHal_send_get_cfgs
3785  *
3786  * Description      This function is called to  send get configs
3787  *                  for all the types in get_cfg_arr.
3788  *                  Response of getConfigs(EEPROM stored) will be
3789  *                  compared with request coming from MW during discovery.
3790  *                  If same, then current setConfigs will be dropped
3791  *
3792  * Returns          Returns NFCSTATUS_SUCCESS if sending cmd is successful and
3793  *                  response is received.
3794  *
3795  *****************************************************************************/
phNxpNciHal_send_get_cfgs()3796 NFCSTATUS phNxpNciHal_send_get_cfgs() {
3797   NXPLOG_NCIHAL_D("%s Enter", __func__);
3798   NFCSTATUS status = NFCSTATUS_FAILED;
3799   uint8_t num_cfgs = sizeof(get_cfg_arr) / sizeof(uint8_t);
3800   uint8_t cfg_count = 0, retry_cnt = 0;
3801   if (mGetCfg_info != NULL) {
3802     mGetCfg_info->isGetcfg = true;
3803   }
3804   uint8_t cmd_get_cfg[] = {0x20, 0x03, 0x02, 0x01, 0x00};
3805 
3806   while (cfg_count < num_cfgs) {
3807     cmd_get_cfg[sizeof(cmd_get_cfg) - 1] = get_cfg_arr[cfg_count];
3808 
3809   retry_get_cfg:
3810     status = phNxpNciHal_send_ext_cmd(sizeof(cmd_get_cfg), cmd_get_cfg);
3811     if (status != NFCSTATUS_SUCCESS && retry_cnt < 3) {
3812       NXPLOG_NCIHAL_E("cmd_get_cfg failed");
3813       retry_cnt++;
3814       goto retry_get_cfg;
3815     }
3816     if (retry_cnt == 3) {
3817       break;
3818     }
3819     cfg_count++;
3820     retry_cnt = 0;
3821   }
3822   mGetCfg_info->isGetcfg = false;
3823   return status;
3824 }
3825 
3826 /*******************************************************************************
3827 **
3828 ** Function         phNxpNciHal_configFeatureList
3829 **
3830 ** Description      Configures the featureList based on chip type &
3831 **                  Configure fragmentation length based on chip type.
3832 **                  HW Version information number will provide chipType.
3833 **                  HW Version can be obtained from CORE_INIT_RESPONSE(NCI 1.0)
3834 **                  or CORE_RST_NTF(NCI 2.0)
3835 **
3836 ** Parameters       CORE_INIT_RESPONSE/CORE_RST_NTF, len
3837 **
3838 ** Returns          none
3839 *******************************************************************************/
phNxpNciHal_configFeatureList(uint8_t * init_rsp,uint16_t rsp_len)3840 void phNxpNciHal_configFeatureList(uint8_t* init_rsp, uint16_t rsp_len) {
3841   nxpncihal_ctrl.chipType = pConfigFL->processChipType(init_rsp, rsp_len);
3842   tNFC_chipType chipType = nxpncihal_ctrl.chipType;
3843   NXPLOG_NCIHAL_D("%s chipType = %s", __func__, pConfigFL->product[chipType]);
3844   CONFIGURE_FEATURELIST(chipType);
3845   /* update fragment len based on the chip type.*/
3846   phTmlNfc_IoCtl(phTmlNfc_e_setFragmentSize);
3847 }
3848 
3849 /*******************************************************************************
3850 **
3851 ** Function         phNxpNciHal_UpdateFwStatus
3852 **
3853 ** Description      It shall be called to update the FW download status to the
3854 **                  libnfc-nci.
3855 **
3856 ** Parameters       fwStatus: FW update status
3857 **
3858 ** Returns          void
3859 *******************************************************************************/
phNxpNciHal_UpdateFwStatus(HalNfcFwUpdateStatus fwStatus)3860 static void phNxpNciHal_UpdateFwStatus(HalNfcFwUpdateStatus fwStatus) {
3861   static phLibNfc_Message_t msg;
3862   static uint8_t status;
3863   if (RfFwRegionDnld_handle == NULL) {
3864     /* If proprietary feature not supported */
3865     return;
3866   }
3867   NXPLOG_NCIHAL_D("phNxpNciHal_UpdateFwStatus Enter");
3868 
3869   status = (uint8_t)fwStatus;
3870   msg.eMsgType = HAL_NFC_FW_UPDATE_STATUS_EVT;
3871   msg.pMsgData = &status;
3872   msg.Size = sizeof(status);
3873   phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
3874                         (phLibNfc_Message_t*)&msg);
3875   return;
3876 }
3877 
3878 /*******************************************************************************
3879 **
3880 ** Function         phNxpNciHal_configureLxDebugMode
3881 **
3882 ** Description      Helper function to configure LxDebug modes
3883 **
3884 ** Parameters       none
3885 **
3886 ** Returns          void
3887 *******************************************************************************/
phNxpNciHal_configureLxDebugMode()3888 void phNxpNciHal_configureLxDebugMode() {
3889   NFCSTATUS status = NFCSTATUS_SUCCESS;
3890   unsigned long lx_debug_cfg = 0;
3891   uint8_t isfound = 0;
3892   static uint8_t cmd_lxdebug[] = {0x20, 0x02, 0x06, 0x01, 0xA0,
3893                                   0x1D, 0x02, 0x00, 0x00};
3894 
3895   isfound = GetNxpNumValue(NAME_NXP_CORE_PROP_SYSTEM_DEBUG, &lx_debug_cfg,
3896                            sizeof(lx_debug_cfg));
3897 
3898   if (isfound) {
3899     if (lx_debug_cfg & LX_DEBUG_CFG_MASK_RFU) {
3900       NXPLOG_NCIHAL_E(
3901           "One or more RFU bits are enabled.\nMasking the RFU bits");
3902       lx_debug_cfg = lx_debug_cfg & ~LX_DEBUG_CFG_MASK_RFU;
3903     }
3904     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_L1_EVENT) {
3905       NXPLOG_NCIHAL_D("Enable L1 RF NTF debugs");
3906     }
3907     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_L2_EVENT) {
3908       NXPLOG_NCIHAL_D("Enable L2 RF NTF debugs (CE)");
3909     }
3910     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_FELICA_RF) {
3911       NXPLOG_NCIHAL_D("Enable all Felica CM events");
3912     }
3913     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_FELICA_SYSCODE) {
3914       NXPLOG_NCIHAL_D("Enable Felica System Code");
3915     }
3916     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_L2_EVENT_READER) {
3917       NXPLOG_NCIHAL_D("Enable L2 RF NTF debugs (Reader)");
3918     }
3919     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_MOD_DETECTED_EVENT) {
3920       NXPLOG_NCIHAL_D("Enable Modulation detected event");
3921     }
3922     if (lx_debug_cfg & LX_DEBUG_CFG_ENABLE_CMA_EVENTS) {
3923       NXPLOG_NCIHAL_D("Enable CMA events");
3924     }
3925 
3926     cmd_lxdebug[7] = (uint8_t)(lx_debug_cfg & LX_DEBUG_CFG_MASK);
3927     cmd_lxdebug[8] = (uint8_t)((lx_debug_cfg & LX_DEBUG_CFG_MASK) >> 8);
3928   }
3929   if (lx_debug_cfg == LX_DEBUG_CFG_DISABLE) {
3930     NXPLOG_NCIHAL_D("Disable LxDebug");
3931   }
3932   status = phNxpNciHal_send_ext_cmd(
3933       sizeof(cmd_lxdebug) / sizeof(cmd_lxdebug[0]), cmd_lxdebug);
3934   if (status != NFCSTATUS_SUCCESS) {
3935     NXPLOG_NCIHAL_E("Set lxDebug config failed");
3936   }
3937 }
3938 
3939 /*******************************************************************************
3940 **
3941 ** Function         phNxpNciHal_initializeRegRfFwDnld(void)
3942 **
3943 ** Description      Loads the module & initializes function pointers for Region
3944 **                  based RF & FW update module
3945 **
3946 ** Parameters       none
3947 **
3948 ** Returns          void
3949 *******************************************************************************/
phNxpNciHal_initializeRegRfFwDnld()3950 void phNxpNciHal_initializeRegRfFwDnld() {
3951   // Getting pointer to RF & RF Region Code Download module
3952   RfFwRegionDnld_handle =
3953       dlopen("/system/vendor/lib64/libonebinary.so", RTLD_NOW);
3954   if (RfFwRegionDnld_handle == NULL) {
3955     NXPLOG_NCIHAL_D(
3956         "Error : opening (/system/vendor/lib64/libonebinary.so) !!");
3957     return;
3958   }
3959   if ((fpVerInfoStoreInEeprom = (fpVerInfoStoreInEeprom_t)dlsym(
3960            RfFwRegionDnld_handle, "read_version_info_and_store_in_eeprom")) ==
3961       NULL) {
3962     NXPLOG_NCIHAL_D(
3963         "Error while linking (read_version_info_and_store_in_eeprom) !!");
3964     return;
3965   }
3966   if ((fpRegRfFwDndl = (fpRegRfFwDndl_t)dlsym(RfFwRegionDnld_handle,
3967                                               "RegRfFwDndl")) == NULL) {
3968     NXPLOG_NCIHAL_D("Error while linking (RegRfFwDndl) !!");
3969     return;
3970   }
3971   if ((fpPropConfCover = (fpPropConfCover_t)dlsym(RfFwRegionDnld_handle,
3972                                                   "prop_conf_cover")) == NULL) {
3973     NXPLOG_NCIHAL_D("Error while linking (prop_conf_cover) !!");
3974     return;
3975   }
3976   if ((fpDoAntennaActivity = (fpDoAntennaActivity_t)dlsym(
3977            RfFwRegionDnld_handle, "DoAntennaActivity")) == NULL) {
3978     NXPLOG_NCIHAL_E("Error while linking (DoAntennaActivity) !!");
3979     return;
3980   }
3981 }
3982 
3983 /*******************************************************************************
3984 **
3985 ** Function         phNxpNciHal_deinitializeRegRfFwDnld(void)
3986 **
3987 ** Description      Resets the module handle & all the function pointers for
3988 **                  Region based RF & FW update module
3989 **
3990 ** Parameters       none
3991 **
3992 ** Returns          void
3993 *******************************************************************************/
phNxpNciHal_deinitializeRegRfFwDnld()3994 void phNxpNciHal_deinitializeRegRfFwDnld() {
3995   if (RfFwRegionDnld_handle != NULL) {
3996     NXPLOG_NCIHAL_D("closing libonebinary.so");
3997     fpVerInfoStoreInEeprom = NULL;
3998     fpRegRfFwDndl = NULL;
3999     fpPropConfCover = NULL;
4000     dlclose(RfFwRegionDnld_handle);
4001     RfFwRegionDnld_handle = NULL;
4002   }
4003 }
4004 
4005 /******************************************************************************
4006  * Function         phNxpNciHal_setVerboseLogging
4007  *
4008  * Description      This function enables the nfc_debug_enabled
4009  *
4010  * Returns          void
4011  *
4012  *****************************************************************************/
4013 
phNxpNciHal_setVerboseLogging(bool enable)4014 void phNxpNciHal_setVerboseLogging(bool enable) { nfc_debug_enabled = enable; }
4015 
4016 /******************************************************************************
4017  * Function         phNxpNciHal_getVerboseLogging
4018  *
4019  * Description      This function returns the value of nfc_debug_enabled
4020  *
4021  * Returns          void
4022  *
4023  *****************************************************************************/
4024 
phNxpNciHal_getVerboseLogging()4025 bool phNxpNciHal_getVerboseLogging() { return nfc_debug_enabled; }
4026