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