• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012-2024 NXP
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <log/log.h>
17 #include <phDal4Nfc_messageQueueLib.h>
18 #include <phDnldNfc.h>
19 #include <phNxpConfig.h>
20 #include <phNxpLog.h>
21 #include <phNxpNciHal.h>
22 #include <phNxpNciHal_Adaptation.h>
23 #include <phNxpNciHal_ext.h>
24 #include <phNxpTempMgr.h>
25 #include <phTmlNfc.h>
26 
27 #include <vector>
28 
29 #include "phNxpEventLogger.h"
30 #include "phNxpNciHal.h"
31 #include "phNxpNciHal_IoctlOperations.h"
32 #include "phNxpNciHal_LxDebug.h"
33 #include "phNxpNciHal_PowerTrackerIface.h"
34 #include "phNxpNciHal_VendorProp.h"
35 
36 #define NXP_EN_SN110U 1
37 #define NXP_EN_SN100U 1
38 #define NXP_EN_SN220U 1
39 #define NXP_EN_PN557 1
40 #define NXP_EN_PN560 1
41 #define NXP_EN_SN300U 1
42 #define NXP_EN_SN330U 1
43 #define NFC_NXP_MW_ANDROID_VER (15U)  /* Android version used by NFC MW */
44 #define NFC_NXP_MW_VERSION_MAJ (0x0C) /* MW Major Version */
45 #define NFC_NXP_MW_VERSION_MIN (0x00) /* MW Minor Version */
46 #define NFC_NXP_MW_CUSTOMER_ID (0x00) /* MW Customer Id */
47 #define NFC_NXP_MW_RC_VERSION (0x00)  /* MW RC Version */
48 
49 /* Timeout value to wait for response from PN548AD */
50 #define HAL_EXTNS_WRITE_RSP_TIMEOUT (1000)
51 #define NCI_NFC_DEP_RF_INTF 0x03
52 #define NCI_STATUS_OK 0x00
53 #define NCI_MODE_HEADER_LEN 3
54 
55 /******************* Global variables *****************************************/
56 extern phNxpNciHal_Control_t nxpncihal_ctrl;
57 extern phNxpNciProfile_Control_t nxpprofile_ctrl;
58 extern phNxpNci_getCfg_info_t* mGetCfg_info;
59 extern PowerTrackerHandle gPowerTrackerHandle;
60 
61 extern bool_t gsIsFwRecoveryRequired;
62 
63 extern bool nfc_debug_enabled;
64 extern const char* core_reset_ntf_count_prop_name;
65 uint8_t icode_detected = 0x00;
66 uint8_t icode_send_eof = 0x00;
67 static uint8_t ee_disc_done = 0x00;
68 extern bool bEnableMfcExtns;
69 extern bool bEnableMfcReader;
70 /* NFCEE Set mode */
71 static uint8_t setEEModeDone = 0x00;
72 /* External global variable to get FW version from NCI response*/
73 extern uint32_t wFwVerRsp;
74 /* External global variable to get FW version from FW file*/
75 extern uint16_t wFwVer;
76 /* local buffer to store CORE_INIT response */
77 static uint32_t bCoreInitRsp[40];
78 static uint32_t iCoreInitRspLen;
79 
80 extern uint32_t timeoutTimerId;
81 extern sem_t sem_reset_ntf_received;
82 
83 /************** HAL extension functions ***************************************/
84 static void hal_extns_write_rsp_timeout_cb(uint32_t TimerId, void* pContext);
85 
86 /*Proprietary cmd sent to HAL to send reader mode flag
87  * Last byte of 4 byte proprietary cmd data contains ReaderMode flag
88  * If this flag is enabled, NFC-DEP protocol is modified to T3T protocol
89  * if FrameRF interface is selected. This needs to be done as the FW
90  * always sends Ntf for FrameRF with NFC-DEP even though FrameRF with T3T is
91  * previously selected with DISCOVER_SELECT_CMD
92  */
93 #define PROPRIETARY_CMD_FELICA_READER_MODE 0xFE
94 static uint8_t gFelicaReaderMode;
95 static bool mfc_mode = false;
96 
97 static NFCSTATUS phNxpNciHal_ext_process_nfc_init_rsp(uint8_t* p_ntf,
98                                                       uint16_t* p_len);
99 static void RemoveNfcDepIntfFromInitResp(uint8_t* coreInitResp,
100                                          uint16_t* coreInitRespLen);
101 
102 static NFCSTATUS phNxpNciHal_process_screen_state_cmd(uint16_t* cmd_len,
103                                                       uint8_t* p_cmd_data,
104                                                       uint16_t* rsp_len,
105                                                       uint8_t* p_rsp_data);
106 static bool phNxpNciHal_update_core_reset_ntf_prop();
107 
printNfcMwVersion()108 void printNfcMwVersion() {
109   uint32_t validation = (NXP_EN_SN100U << 13);
110   validation |= (NXP_EN_SN110U << 14);
111   validation |= (NXP_EN_SN220U << 15);
112   validation |= (NXP_EN_PN560 << 16);
113   validation |= (NXP_EN_SN300U << 17);
114   validation |= (NXP_EN_SN330U << 18);
115   validation |= (NXP_EN_PN557 << 11);
116 
117   ALOGE("MW-HAL Version: NFC_AR_%02X_%05X_%02d.%02x.%02x",
118         NFC_NXP_MW_CUSTOMER_ID, validation, NFC_NXP_MW_ANDROID_VER,
119         NFC_NXP_MW_VERSION_MAJ, NFC_NXP_MW_VERSION_MIN);
120 }
121 /*******************************************************************************
122 **
123 ** Function         phNxpNciHal_ext_init
124 **
125 ** Description      initialize extension function
126 **
127 *******************************************************************************/
phNxpNciHal_ext_init(void)128 void phNxpNciHal_ext_init(void) {
129   icode_detected = 0x00;
130   if (IS_CHIP_TYPE_L(sn100u)) {
131     icode_send_eof = 0x00;
132   }
133   setEEModeDone = 0x00;
134 }
135 
136 /*******************************************************************************
137 **
138 ** Function         phNxpNciHal_ext_send_sram_config_to_flash
139 **
140 ** Description      This function is called to update the SRAM contents such as
141 **                  set config to FLASH for permanent storage.
142 **                  Note: This function has to be called after set config and
143 **                  before sending  core_reset command again.
144 **
145 *******************************************************************************/
phNxpNciHal_ext_send_sram_config_to_flash()146 NFCSTATUS phNxpNciHal_ext_send_sram_config_to_flash() {
147   NXPLOG_NCIHAL_D("phNxpNciHal_ext_send_sram_config_to_flash  send");
148   NFCSTATUS status = NFCSTATUS_SUCCESS;
149   uint8_t send_sram_flash[] = {NXP_PROPCMD_GID, NXP_FLUSH_SRAM_AO_TO_FLASH,
150                                0x00};
151   status = phNxpNciHal_send_ext_cmd(sizeof(send_sram_flash), send_sram_flash);
152   return status;
153 }
154 /*******************************************************************************
155 **
156 ** Function         phNxpNciHal_process_ext_rsp
157 **
158 ** Description      Process extension function response
159 **
160 ** Returns          NFCSTATUS_SUCCESS if success
161 **
162 *******************************************************************************/
phNxpNciHal_process_ext_rsp(uint8_t * p_ntf,uint16_t * p_len)163 NFCSTATUS phNxpNciHal_process_ext_rsp(uint8_t* p_ntf, uint16_t* p_len) {
164   NFCSTATUS status = NFCSTATUS_SUCCESS;
165 
166 #if (NXP_SRD == TRUE)
167   if (*p_len > 29 && p_ntf[0] == 0x01 && p_ntf[1] == 0x00 && p_ntf[5] == 0x81 &&
168       p_ntf[23] == 0x82 && p_ntf[26] == 0xA0 && p_ntf[27] == 0xFE) {
169     if (p_ntf[29] == 0x01) {
170       nxpprofile_ctrl.profile_type = SRD_PROFILE;
171     } else if (p_ntf[29] == 0x00) {
172       nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
173     }
174   } else if (*p_len > 3 && p_ntf[0] == 0x60 && p_ntf[1] == 0x07 &&
175              p_ntf[2] == 0x01 && p_ntf[3] == 0xE2) {
176     nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
177   }
178 #endif
179 
180   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && *p_len < 14) {
181     if (*p_len <= 6) {
182       android_errorWriteLog(0x534e4554, "118152591");
183     }
184     NXPLOG_NCIHAL_E("RF_INTF_ACTIVATED_NTF length error!");
185     status = NFCSTATUS_FAILED;
186     return status;
187   }
188 
189   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[4] == 0x03 &&
190       p_ntf[5] == 0x05 && nxpprofile_ctrl.profile_type == EMV_CO_PROFILE) {
191     p_ntf[4] = 0xFF;
192     p_ntf[5] = 0xFF;
193     p_ntf[6] = 0xFF;
194     NXPLOG_NCIHAL_D("Nfc-Dep Detect in EmvCo profile - Restart polling");
195   }
196 
197   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[4] == 0x01 &&
198       p_ntf[5] == 0x05 && p_ntf[6] == 0x02 && gFelicaReaderMode) {
199     /*If FelicaReaderMode is enabled,Change Protocol to T3T from NFC-DEP
200      * when FrameRF interface is selected*/
201     p_ntf[5] = 0x03;
202     NXPLOG_NCIHAL_D("FelicaReaderMode:Activity 1.1");
203   }
204 
205   if (bEnableMfcExtns && p_ntf[0] == 0) {
206     if (*p_len < NCI_HEADER_SIZE) {
207       android_errorWriteLog(0x534e4554, "169258743");
208       return NFCSTATUS_FAILED;
209     }
210     uint16_t extlen;
211     extlen = *p_len - NCI_HEADER_SIZE;
212     NxpMfcReaderInstance.AnalyzeMfcResp(&p_ntf[3], &extlen);
213     p_ntf[2] = extlen;
214     *p_len = extlen + NCI_HEADER_SIZE;
215   }
216 
217   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05) {
218     bEnableMfcExtns = false;
219     if (p_ntf[4] == 0x80 && p_ntf[5] == 0x80) {
220       bEnableMfcExtns = true;
221       NXPLOG_NCIHAL_D("NxpNci: RF Interface = Mifare Enable MifareExtns");
222     }
223     switch (p_ntf[4]) {
224       case 0x00:
225         NXPLOG_NCIHAL_D("NxpNci: RF Interface = NFCEE Direct RF");
226         break;
227       case 0x01:
228         NXPLOG_NCIHAL_D("NxpNci: RF Interface = Frame RF");
229         break;
230       case 0x02:
231         NXPLOG_NCIHAL_D("NxpNci: RF Interface = ISO-DEP");
232         break;
233       case 0x03:
234         NXPLOG_NCIHAL_D("NxpNci: RF Interface = NFC-DEP");
235         break;
236       case 0x80:
237         NXPLOG_NCIHAL_D("NxpNci: RF Interface = MIFARE");
238         break;
239       default:
240         NXPLOG_NCIHAL_D("NxpNci: RF Interface = Unknown");
241         break;
242     }
243 
244     switch (p_ntf[5]) {
245       case 0x01:
246         NXPLOG_NCIHAL_D("NxpNci: Protocol = T1T");
247         break;
248       case 0x02:
249         NXPLOG_NCIHAL_D("NxpNci: Protocol = T2T");
250         break;
251       case 0x03:
252         NXPLOG_NCIHAL_D("NxpNci: Protocol = T3T");
253         break;
254       case 0x04:
255         NXPLOG_NCIHAL_D("NxpNci: Protocol = ISO-DEP");
256         break;
257       case 0x05:
258         NXPLOG_NCIHAL_D("NxpNci: Protocol = NFC-DEP");
259         break;
260       case 0x06:
261         NXPLOG_NCIHAL_D("NxpNci: Protocol = 15693");
262         break;
263       case 0x80:
264         NXPLOG_NCIHAL_D("NxpNci: Protocol = MIFARE");
265         break;
266       case 0x81:
267         NXPLOG_NCIHAL_D("NxpNci: Protocol = Kovio");
268         break;
269       default:
270         NXPLOG_NCIHAL_D("NxpNci: Protocol = Unknown");
271         break;
272     }
273 
274     switch (p_ntf[6]) {
275       case 0x00:
276         NXPLOG_NCIHAL_D("NxpNci: Mode = A Passive Poll");
277         break;
278       case 0x01:
279         NXPLOG_NCIHAL_D("NxpNci: Mode = B Passive Poll");
280         break;
281       case 0x02:
282         NXPLOG_NCIHAL_D("NxpNci: Mode = F Passive Poll");
283         break;
284       case 0x03:
285         NXPLOG_NCIHAL_D("NxpNci: Mode = A Active Poll");
286         break;
287       case 0x05:
288         NXPLOG_NCIHAL_D("NxpNci: Mode = F Active Poll");
289         break;
290       case 0x06:
291         NXPLOG_NCIHAL_D("NxpNci: Mode = 15693 Passive Poll");
292         break;
293       case 0x70:
294         NXPLOG_NCIHAL_D("NxpNci: Mode = Kovio");
295         break;
296 #if (NXP_QTAG == TRUE)
297       case 0x71:
298         NXPLOG_NCIHAL_D("NxpNci: Mode = Q Passive Poll");
299         break;
300 #endif
301       case 0x80:
302         NXPLOG_NCIHAL_D("NxpNci: Mode = A Passive Listen");
303         break;
304       case 0x81:
305         NXPLOG_NCIHAL_D("NxpNci: Mode = B Passive Listen");
306         break;
307       case 0x82:
308         NXPLOG_NCIHAL_D("NxpNci: Mode = F Passive Listen");
309         break;
310       case 0x83:
311         NXPLOG_NCIHAL_D("NxpNci: Mode = A Active Listen");
312         break;
313       case 0x85:
314         NXPLOG_NCIHAL_D("NxpNci: Mode = F Active Listen");
315         break;
316       case 0x86:
317         NXPLOG_NCIHAL_D("NxpNci: Mode = 15693 Passive Listen");
318         break;
319       default:
320         NXPLOG_NCIHAL_D("NxpNci: Mode = Unknown");
321         break;
322     }
323   }
324   phNxpNciHal_ext_process_nfc_init_rsp(p_ntf, p_len);
325   if (p_ntf[0] == NCI_MT_NTF &&
326       ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET) &&
327       p_ntf[3] == CORE_RESET_TRIGGER_TYPE_POWERED_ON) {
328     status = NFCSTATUS_FAILED;
329     NXPLOG_NCIHAL_D("Skipping power on reset notification!!:");
330     return status;
331   }
332   if (p_ntf[0] == 0x42 && p_ntf[1] == 0x01 && p_ntf[2] == 0x01 &&
333       p_ntf[3] == 0x00) {
334     if (nxpncihal_ctrl.hal_ext_enabled == TRUE && IS_CHIP_TYPE_GE(sn100u)) {
335       nxpncihal_ctrl.nci_info.wait_for_ntf = TRUE;
336       NXPLOG_NCIHAL_D(" Mode set received");
337     }
338   } else if (*p_len > 22 && p_ntf[0] == 0x61 && p_ntf[1] == 0x05 &&
339              p_ntf[2] == 0x15 && p_ntf[4] == 0x01 && p_ntf[5] == 0x06 &&
340              p_ntf[6] == 0x06) {
341     NXPLOG_NCIHAL_D("> Going through workaround - notification of ISO 15693");
342     icode_detected = 0x01;
343     p_ntf[21] = 0x01;
344     p_ntf[22] = 0x01;
345   } else if (IS_CHIP_TYPE_L(sn100u) && icode_detected == 1 &&
346              icode_send_eof == 2) {
347     icode_send_eof = 3;
348   } else if (IS_CHIP_TYPE_L(sn100u) && p_ntf[0] == 0x00 && p_ntf[1] == 0x00 &&
349              icode_detected == 1) {
350     if (icode_send_eof == 3) {
351       icode_send_eof = 0;
352     }
353     if (nxpncihal_ctrl.nci_info.nci_version != NCI_VERSION_2_0) {
354       if (*p_len <= (p_ntf[2] + 2)) {
355         android_errorWriteLog(0x534e4554, "181660091");
356         NXPLOG_NCIHAL_E("length error!");
357         return NFCSTATUS_FAILED;
358       }
359       if (p_ntf[p_ntf[2] + 2] == 0x00) {
360         NXPLOG_NCIHAL_D("> Going through workaround - data of ISO 15693");
361         p_ntf[2]--;
362         (*p_len)--;
363       } else {
364         p_ntf[p_ntf[2] + 2] |= 0x01;
365       }
366     }
367   } else if (IS_CHIP_TYPE_L(sn100u) && p_ntf[2] == 0x02 && p_ntf[1] == 0x00 &&
368              icode_detected == 1) {
369     NXPLOG_NCIHAL_D("> ICODE EOF response do not send to upper layer");
370   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x06 && icode_detected == 1) {
371     NXPLOG_NCIHAL_D("> Polling Loop Re-Started");
372     icode_detected = 0;
373     if (IS_CHIP_TYPE_L(sn100u)) icode_send_eof = 0;
374   }
375 
376   if (p_ntf[0] == 0x60 && p_ntf[1] == 0x07 && p_ntf[2] == 0x01) {
377     if (p_ntf[3] == CORE_GENERIC_ERR_CURRENT_NTF ||
378         p_ntf[3] == CORE_GENERIC_ERR_UA_CRC_NTF ||
379         p_ntf[3] == CORE_GENERIC_ERR_UA_MIR_CRC_NTF) {
380       gsIsFwRecoveryRequired = true;
381       NXPLOG_NCIHAL_D("FW update required");
382       status = NFCSTATUS_FAILED;
383     } else if ((p_ntf[3] == 0xE5) || (p_ntf[3] == 0x60)) {
384       NXPLOG_NCIHAL_D("ignore core generic error");
385       status = NFCSTATUS_FAILED;
386     }
387     return status;
388   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x21 && p_ntf[2] == 0x00) {
389     status = NFCSTATUS_FAILED;
390     NXPLOG_NCIHAL_D("ignore core generic error");
391     return status;
392   }
393   // 4200 02 00 01
394   else if (p_ntf[0] == 0x42 && p_ntf[1] == 0x00 && ee_disc_done == 0x01) {
395     NXPLOG_NCIHAL_D("Going through workaround - NFCEE_DISCOVER_RSP");
396     if (p_ntf[4] == 0x01) {
397       p_ntf[4] = 0x00;
398 
399       ee_disc_done = 0x00;
400     }
401     NXPLOG_NCIHAL_D("Going through workaround - NFCEE_DISCOVER_RSP - END");
402   } else if (*p_len >= 2 && p_ntf[0] == 0x6F && p_ntf[1] == 0x04) {
403     NXPLOG_NCIHAL_D(">  SMB Debug notification received");
404     PhNxpEventLogger::GetInstance().Log(p_ntf, *p_len,
405                                         LogEventType::kLogSMBEvent);
406   } else if (*p_len >= 5 && p_ntf[0] == 0x01 &&
407              (p_ntf[3] == ESE_CONNECTIVITY_PACKET ||
408               p_ntf[3] == EUICC_CONNECTIVITY_PACKET) &&
409              p_ntf[4] == ESE_DPD_EVENT) {
410     NXPLOG_NCIHAL_D(">  DPD monitor event received");
411     PhNxpEventLogger::GetInstance().Log(p_ntf, *p_len,
412                                         LogEventType::kLogDPDEvent);
413   } else if (p_ntf[0] == 0x6F &&
414              p_ntf[1] == NCI_OID_SYSTEM_TERMPERATURE_INFO_NTF &&
415              p_ntf[2] == 0x06) {
416     NXPLOG_NCIHAL_D(">  Temperature status ntf received");
417     phNxpTempMgr::GetInstance().UpdateICTempStatus(p_ntf, *p_len);
418   }
419   return status;
420 }
421 
422 /******************************************************************************
423  * Function         phNxpNciHal_ext_process_nfc_init_rsp
424  *
425  * Description      This function is used to process the HAL NFC core reset rsp
426  *                  and ntf and core init rsp of NCI 1.0 or NCI2.0 and update
427  *                  NCI version.
428  *                  It also handles error response such as core_reset_ntf with
429  *                  error status in both NCI2.0 and NCI1.0.
430  *
431  * Returns          Returns NFCSTATUS_SUCCESS if parsing response is successful
432  *                  or returns failure.
433  *
434  ******************************************************************************/
phNxpNciHal_ext_process_nfc_init_rsp(uint8_t * p_ntf,uint16_t * p_len)435 static NFCSTATUS phNxpNciHal_ext_process_nfc_init_rsp(uint8_t* p_ntf,
436                                                       uint16_t* p_len) {
437   NFCSTATUS status = NFCSTATUS_SUCCESS;
438   bool is_abort_req = false;
439   /* Parsing CORE_RESET_RSP and CORE_RESET_NTF to update NCI version.*/
440   if (p_ntf == NULL || *p_len < 2) {
441     return NFCSTATUS_FAILED;
442   }
443   if (p_ntf[0] == NCI_MT_RSP &&
444       ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
445     if (*p_len < 4) {
446       android_errorWriteLog(0x534e4554, "169258455");
447       NXPLOG_NCIHAL_E("%s invalid CORE_RESET_RSP len", __func__);
448       goto core_reset_err;
449     }
450     if (p_ntf[2] == 0x01 && p_ntf[3] == 0x00) {
451       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI2.0");
452       if (nxpncihal_ctrl.hal_ext_enabled == TRUE &&
453           nxpncihal_ctrl.isCoreRstForFwDnld != TRUE) {
454         nxpncihal_ctrl.nci_info.wait_for_ntf = TRUE;
455       }
456     } else if (p_ntf[2] == 0x03 && p_ntf[3] == 0x00) {
457       if (*p_len < 5) {
458         android_errorWriteLog(0x534e4554, "169258455");
459         NXPLOG_NCIHAL_E("%s invalid CORE_RESET_RSP len", __func__);
460         goto core_reset_err;
461       }
462       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI1.0");
463       nxpncihal_ctrl.nci_info.nci_version = p_ntf[4];
464     } else {
465       NXPLOG_NCIHAL_E("%s invalid CORE_RESET_RSP", __func__);
466       goto core_reset_err;
467     }
468   } else if (p_ntf[0] == NCI_MT_NTF &&
469              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
470     if (*p_len < 4) {
471       android_errorWriteLog(0x534e4554, "169258455");
472       NXPLOG_NCIHAL_E("%s invalid CORE_RESET_NTF len", __func__);
473       goto core_reset_err;
474     }
475     if (p_ntf[3] == CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED) {
476       if (*p_len < 6) {
477         android_errorWriteLog(0x534e4554, "169258455");
478         NXPLOG_NCIHAL_E("%s invalid CORE_RESET_NTF len", __func__);
479         goto core_reset_err;
480       }
481       NXPLOG_NCIHAL_D("CORE_RESET_NTF NCI2.0 reason CORE_RESET_CMD received !");
482       nxpncihal_ctrl.nci_info.nci_version = p_ntf[5];
483       if (!nxpncihal_ctrl.hal_open_status)
484         phNxpNciHal_configFeatureList(p_ntf, *p_len);
485       int len = p_ntf[2] + 2; /*include 2 byte header*/
486       if (len != *p_len - 1) {
487         android_errorWriteLog(0x534e4554, "121263487");
488         NXPLOG_NCIHAL_E("%s invalid CORE_RESET_NTF len", __func__);
489         goto core_reset_err;
490       }
491       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
492                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
493       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
494                       p_ntf[len - 1], p_ntf[len]);
495     } else {
496       if ((p_ntf[3] == CORE_RESET_TRIGGER_TYPE_WATCHDOG_RESET ||
497            p_ntf[3] == CORE_RESET_TRIGGER_TYPE_FW_ASSERT) ||
498           ((p_ntf[3] == CORE_RESET_TRIGGER_TYPE_UNRECOVERABLE_ERROR) &&
499            (p_ntf[4] == CORE_RESET_TRIGGER_TYPE_WATCHDOG_RESET ||
500             p_ntf[4] == CORE_RESET_TRIGGER_TYPE_FW_ASSERT))) {
501         /* WA : In some cases for Watchdog reset FW sends reset reason code as
502          * unrecoverable error and config status as WATCHDOG_RESET */
503         is_abort_req = phNxpNciHal_update_core_reset_ntf_prop();
504       } else {
505         is_abort_req = true;
506       }
507       NXPLOG_NCIHAL_E("%s NFC FW reset triggered", __func__);
508       goto core_reset_err;
509     } /* Parsing CORE_INIT_RSP*/
510   } else if (p_ntf[0] == NCI_MT_RSP &&
511              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_INIT)) {
512     if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
513       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI2.0 received !");
514       /* Remove NFC-DEP interface support from INIT RESP */
515       RemoveNfcDepIntfFromInitResp(p_ntf, p_len);
516     } else {
517       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI1.0 received !");
518       if (!nxpncihal_ctrl.hal_open_status &&
519           nxpncihal_ctrl.nci_info.nci_version != NCI_VERSION_2_0) {
520         phNxpNciHal_configFeatureList(p_ntf, *p_len);
521       }
522       if (*p_len < 3) {
523         android_errorWriteLog(0x534e4554, "169258455");
524         NXPLOG_NCIHAL_E("%s invalid CORE_INIT_RSP len", __func__);
525         goto core_reset_err;
526       }
527       int len = p_ntf[2] + 2; /*include 2 byte header*/
528       if (len != *p_len - 1) {
529         android_errorWriteLog(0x534e4554, "121263487");
530         NXPLOG_NCIHAL_E("%s invalid CORE_INIT_RSP len", __func__);
531         goto core_reset_err;
532       }
533       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
534                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
535       if (wFwVerRsp == 0) {
536         NXPLOG_NCIHAL_E("%s invalid FW Version: %x.%x.%x", p_ntf[len - 2],
537                         p_ntf[len - 1], p_ntf[len]);
538         status = NFCSTATUS_FAILED;
539       }
540       iCoreInitRspLen = *p_len;
541       memcpy(bCoreInitRsp, p_ntf, *p_len);
542       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
543                       p_ntf[len - 1], p_ntf[len]);
544     }
545   }
546   return status;
547 
548 core_reset_err:
549   uint32_t i;
550   char print_buffer[*p_len * 3 + 1];
551 
552   memset(print_buffer, 0, sizeof(print_buffer));
553   for (i = 0; i < *p_len; i++) {
554     snprintf(&print_buffer[i * 2], 3, "%02X", p_ntf[i]);
555   }
556   NXPLOG_NCIR_E("%s len = %3d > %s", __func__, *p_len, print_buffer);
557 
558   if (is_abort_req) phNxpNciHal_emergency_recovery(p_ntf[3]);
559   return NFCSTATUS_FAILED;
560 }
561 
562 /******************************************************************************
563  * Function         phNxpNciHal_process_ext_cmd_rsp
564  *
565  * Description      This function process the extension command response. It
566  *                  also checks the received response to expected response.
567  *
568  * Returns          returns NFCSTATUS_SUCCESS if response is as expected else
569  *                  returns failure.
570  *
571  ******************************************************************************/
phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,uint8_t * p_cmd)572 static NFCSTATUS phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,
573                                                  uint8_t* p_cmd) {
574   NFCSTATUS status = NFCSTATUS_FAILED;
575   uint16_t data_written = 0;
576 
577   /* Check NCI Command is well formed */
578   if (!phTmlNfc_IsFwDnldModeEnabled() &&
579       (cmd_len != (p_cmd[2] + NCI_MODE_HEADER_LEN))) {
580     NXPLOG_NCIHAL_E("NCI command not well formed");
581     return NFCSTATUS_FAILED;
582   }
583 
584   /* Create the local semaphore */
585   if (phNxpNciHal_init_cb_data(&nxpncihal_ctrl.ext_cb_data, NULL) !=
586       NFCSTATUS_SUCCESS) {
587     NXPLOG_NCIHAL_D("Create ext_cb_data failed");
588     return NFCSTATUS_FAILED;
589   }
590 
591   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_SUCCESS;
592 
593   /* Send ext command */
594   data_written = phNxpNciHal_write_unlocked(cmd_len, p_cmd, ORIG_NXPHAL);
595   if (data_written != cmd_len) {
596     NXPLOG_NCIHAL_D("phNxpNciHal_write failed for hal ext");
597     goto clean_and_return;
598   }
599 
600   /* Start timer */
601   status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
602                                  &hal_extns_write_rsp_timeout_cb, NULL);
603   if (NFCSTATUS_SUCCESS == status) {
604     NXPLOG_NCIHAL_D("Response timer started");
605   } else {
606     NXPLOG_NCIHAL_E("Response timer not started!!!");
607     status = NFCSTATUS_FAILED;
608     goto clean_and_return;
609   }
610 
611   /* Wait for rsp */
612   NXPLOG_NCIHAL_D("Waiting after ext cmd sent");
613   if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
614     NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
615     goto clean_and_return;
616   }
617 
618   /* Stop Timer */
619   status = phOsalNfc_Timer_Stop(timeoutTimerId);
620   if (NFCSTATUS_SUCCESS == status) {
621     NXPLOG_NCIHAL_D("Response timer stopped");
622   } else {
623     NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
624     status = NFCSTATUS_FAILED;
625     goto clean_and_return;
626   }
627 
628   if (cmd_len < 3) {
629     android_errorWriteLog(0x534e4554, "153880630");
630     status = NFCSTATUS_FAILED;
631     goto clean_and_return;
632   }
633 
634   /* No NTF expected for OMAPI command */
635   if (p_cmd[0] == 0x2F && p_cmd[1] == 0x1 && p_cmd[2] == 0x01) {
636     nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
637   }
638   /* Start timer to wait for NTF*/
639   if (nxpncihal_ctrl.nci_info.wait_for_ntf == TRUE) {
640     status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
641                                    &hal_extns_write_rsp_timeout_cb, NULL);
642     if (NFCSTATUS_SUCCESS == status) {
643       NXPLOG_NCIHAL_D("Response timer started");
644     } else {
645       NXPLOG_NCIHAL_E("Response timer not started!!!");
646       status = NFCSTATUS_FAILED;
647       goto clean_and_return;
648     }
649     if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
650       NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
651       /* Stop Timer */
652       status = phOsalNfc_Timer_Stop(timeoutTimerId);
653       goto clean_and_return;
654     }
655     status = phOsalNfc_Timer_Stop(timeoutTimerId);
656     if (NFCSTATUS_SUCCESS == status) {
657       NXPLOG_NCIHAL_D("Response timer stopped");
658     } else {
659       NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
660       status = NFCSTATUS_FAILED;
661       goto clean_and_return;
662     }
663   }
664 
665   if (nxpncihal_ctrl.ext_cb_data.status != NFCSTATUS_SUCCESS) {
666     NXPLOG_NCIHAL_E(
667         "Callback Status is failed!! Timer Expired!! Couldn't read it! 0x%x",
668         nxpncihal_ctrl.ext_cb_data.status);
669     status = NFCSTATUS_FAILED;
670     goto clean_and_return;
671   }
672 
673   NXPLOG_NCIHAL_D("Checking response");
674   status = NFCSTATUS_SUCCESS;
675 
676   /*Response check for Set config, Core Reset & Core init command sent part of
677    * HAL_EXT*/
678   if (nxpncihal_ctrl.hal_open_status == HAL_OPEN_CORE_INITIALIZING &&
679       nxpncihal_ctrl.p_rx_data[0] == 0x40 &&
680       nxpncihal_ctrl.p_rx_data[1] <= 0x02 &&
681       nxpncihal_ctrl.p_rx_data[2] != 0x00) {
682     status = nxpncihal_ctrl.p_rx_data[3];
683     if (status != NCI_STATUS_OK) {
684       /*Add 500ms delay for FW to flush circular buffer */
685       usleep(500 * 1000);
686       NXPLOG_NCIHAL_D("Status Failed. Status = 0x%02x", status);
687     }
688   }
689 
690   /*Response check for SYSTEM SET SERVICE STATUS command sent as part of
691    * HAL_EXT*/
692   if (IS_CHIP_TYPE_GE(sn220u) && nxpncihal_ctrl.p_rx_data[0] == 0x4F &&
693       nxpncihal_ctrl.p_rx_data[1] == 0x01 &&
694       nxpncihal_ctrl.p_rx_data[2] != 0x00) {
695     status = nxpncihal_ctrl.p_rx_data[3];
696   }
697 
698 clean_and_return:
699   phNxpNciHal_cleanup_cb_data(&nxpncihal_ctrl.ext_cb_data);
700   nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
701   HAL_DISABLE_EXT();
702   return status;
703 }
704 
705 /******************************************************************************
706  * Function         phNxpNciHal_write_ext
707  *
708  * Description      This function inform the status of phNxpNciHal_open
709  *                  function to libnfc-nci.
710  *
711  * Returns          It return NFCSTATUS_SUCCESS then continue with send else
712  *                  sends NFCSTATUS_FAILED direct response is prepared and
713  *                  do not send anything to NFCC.
714  *
715  ******************************************************************************/
phNxpNciHal_write_ext(uint16_t * cmd_len,uint8_t * p_cmd_data,uint16_t * rsp_len,uint8_t * p_rsp_data)716 NFCSTATUS phNxpNciHal_write_ext(uint16_t* cmd_len, uint8_t* p_cmd_data,
717                                 uint16_t* rsp_len, uint8_t* p_rsp_data) {
718   NFCSTATUS status = NFCSTATUS_SUCCESS;
719 
720   if (p_cmd_data[0] == PROPRIETARY_CMD_FELICA_READER_MODE &&
721       p_cmd_data[1] == PROPRIETARY_CMD_FELICA_READER_MODE &&
722       p_cmd_data[2] == PROPRIETARY_CMD_FELICA_READER_MODE) {
723     NXPLOG_NCIHAL_D("Received proprietary command to set Felica Reader mode:%d",
724                     p_cmd_data[3]);
725     gFelicaReaderMode = p_cmd_data[3];
726     /* frame the fake response */
727     *rsp_len = 4;
728     p_rsp_data[0] = 0x00;
729     p_rsp_data[1] = 0x00;
730     p_rsp_data[2] = 0x00;
731     p_rsp_data[3] = 0x00;
732     status = NFCSTATUS_FAILED;
733   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
734              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
735              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
736              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
737              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x01) {
738     nxpprofile_ctrl.profile_type = EMV_CO_PROFILE;
739     NXPLOG_NCIHAL_D("EMV_CO_PROFILE mode - Enabled");
740     status = NFCSTATUS_SUCCESS;
741   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
742              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
743              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
744              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
745              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x00) {
746     NXPLOG_NCIHAL_D("NFC_FORUM_PROFILE mode - Enabled");
747     nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
748     status = NFCSTATUS_SUCCESS;
749   }
750 
751   if (nxpprofile_ctrl.profile_type == EMV_CO_PROFILE) {
752     if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x06 &&
753         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x03) {
754 #if 0
755             //Needs clarification whether to keep it or not
756             NXPLOG_NCIHAL_D ("EmvCo Poll mode - RF Deactivate discard");
757             phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
758             *rsp_len = 4;
759             p_rsp_data[0] = 0x41;
760             p_rsp_data[1] = 0x06;
761             p_rsp_data[2] = 0x01;
762             p_rsp_data[3] = 0x00;
763             phNxpNciHal_print_packet("RECV", p_rsp_data, 4);
764             status = NFCSTATUS_FAILED;
765 #endif
766     } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
767       NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A and B");
768       p_cmd_data[2] = 0x05;
769       p_cmd_data[3] = 0x02;
770       p_cmd_data[4] = 0x00;
771       p_cmd_data[5] = 0x01;
772       p_cmd_data[6] = 0x01;
773       p_cmd_data[7] = 0x01;
774       *cmd_len = 8;
775     }
776   }
777 
778   if (mfc_mode == true && p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
779     NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A");
780     p_cmd_data[2] = 0x03;
781     p_cmd_data[3] = 0x01;
782     p_cmd_data[4] = 0x00;
783     p_cmd_data[5] = 0x01;
784     *cmd_len = 6;
785     mfc_mode = false;
786   }
787 
788   if (*cmd_len <= (NCI_MAX_DATA_LEN - 3) && bEnableMfcReader &&
789       (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x00) &&
790       (nxpprofile_ctrl.profile_type == NFC_FORUM_PROFILE)) {
791     if (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
792         p_cmd_data[4] == 0x80 && p_cmd_data[5] == 0x01 &&
793         p_cmd_data[6] == 0x83) {
794       mfc_mode = true;
795     } else {
796       if (bEnableMfcReader) {
797         NXPLOG_NCIHAL_D("Going through extns - Adding Mifare in RF Discovery");
798         p_cmd_data[2] += 3;
799         p_cmd_data[3] += 1;
800         p_cmd_data[*cmd_len] = 0x80;
801         p_cmd_data[*cmd_len + 1] = 0x01;
802         p_cmd_data[*cmd_len + 2] = 0x80;
803         *cmd_len += 3;
804         status = NFCSTATUS_SUCCESS;
805         NXPLOG_NCIHAL_D(
806             "Going through extns - Adding Mifare in RF Discovery - END");
807       }
808     }
809   } else if (icode_detected) {
810     if (IS_CHIP_TYPE_L(sn100u) && IS_CHIP_TYPE_NE(pn557) &&
811         (p_cmd_data[3] & 0x40) == 0x40 &&
812         (p_cmd_data[4] == 0x21 || p_cmd_data[4] == 0x22 ||
813          p_cmd_data[4] == 0x24 || p_cmd_data[4] == 0x27 ||
814          p_cmd_data[4] == 0x28 || p_cmd_data[4] == 0x29 ||
815          p_cmd_data[4] == 0x2a)) {
816       NXPLOG_NCIHAL_D("> Send EOF set");
817       icode_send_eof = 1;
818     }
819 
820     if (p_cmd_data[3] == 0x20 || p_cmd_data[3] == 0x24 ||
821         p_cmd_data[3] == 0x60) {
822       NXPLOG_NCIHAL_D("> NFC ISO_15693 Proprietary CMD ");
823       p_cmd_data[3] += 0x02;
824     }
825   } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
826     NXPLOG_NCIHAL_D("> Polling Loop Started");
827     icode_detected = 0;
828     if (IS_CHIP_TYPE_L(sn100u)) {
829       icode_send_eof = 0;
830     }
831   }
832   // 22000100
833   else if (p_cmd_data[0] == 0x22 && p_cmd_data[1] == 0x00 &&
834            p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x00) {
835     // ee_disc_done = 0x01;//Reader Over SWP event getting
836     *rsp_len = 0x05;
837     p_rsp_data[0] = 0x42;
838     p_rsp_data[1] = 0x00;
839     p_rsp_data[2] = 0x02;
840     p_rsp_data[3] = 0x00;
841     p_rsp_data[4] = 0x00;
842     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
843     status = NFCSTATUS_FAILED;
844   }
845   // 2002 0904 3000 3100 3200 5000
846   else if (*cmd_len <= (NCI_MAX_DATA_LEN - 1) &&
847            (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
848            ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) /*||
849             (p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
850             )) {
851     *cmd_len += 0x01;
852     p_cmd_data[2] += 0x01;
853     p_cmd_data[9] = 0x01;
854     p_cmd_data[10] = 0x40;
855     p_cmd_data[11] = 0x50;
856     p_cmd_data[12] = 0x00;
857 
858     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
859     //        phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
860     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
861   }
862   //    20020703300031003200
863   //    2002 0301 3200
864   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
865            ((p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x03) ||
866             (p_cmd_data[2] == 0x03 && p_cmd_data[3] == 0x01 &&
867              p_cmd_data[4] == 0x32))) {
868     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
869     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
870     *rsp_len = 5;
871     p_rsp_data[0] = 0x40;
872     p_rsp_data[1] = 0x02;
873     p_rsp_data[2] = 0x02;
874     p_rsp_data[3] = 0x00;
875     p_rsp_data[4] = 0x00;
876 
877     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
878     status = NFCSTATUS_FAILED;
879     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
880   }
881 
882   // 2002 0D04 300104 310100 320100 500100
883   // 2002 0401 320100
884   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
885            (
886                /*(p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
887                (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
888                 p_cmd_data[4] == 0x32 && p_cmd_data[5] == 0x00))) {
889     //        p_cmd_data[12] = 0x40;
890 
891     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
892     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
893     p_cmd_data[6] = 0x60;
894 
895     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
896     //        status = NFCSTATUS_FAILED;
897     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
898   }
899 #if 0
900     else if ( (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 ) &&
901                  ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) ||
902                      (p_cmd_data[2] == 0x0B && p_cmd_data[3] == 0x05) ||
903                      (p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x02) ||
904                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x03) ||
905                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x04) ||
906                      (p_cmd_data[2] == 0x05 && p_cmd_data[3] == 0x02))
907              )
908     {
909         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
910         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
911         *rsp_len = 5;
912         p_rsp_data[0] = 0x40;
913         p_rsp_data[1] = 0x02;
914         p_rsp_data[2] = 0x02;
915         p_rsp_data[3] = 0x00;
916         p_rsp_data[4] = 0x00;
917 
918         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
919         status = NFCSTATUS_FAILED;
920         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
921     }
922 
923     else if((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
924            ((p_cmd_data[3] == 0x00) ||
925            ((*cmd_len >= 0x06) && (p_cmd_data[5] == 0x00)))) /*If the length of the first param id is zero don't allow*/
926     {
927         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
928         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
929         *rsp_len = 5;
930         p_rsp_data[0] = 0x40;
931         p_rsp_data[1] = 0x02;
932         p_rsp_data[2] = 0x02;
933         p_rsp_data[3] = 0x00;
934         p_rsp_data[4] = 0x00;
935 
936         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
937         status = NFCSTATUS_FAILED;
938         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
939     }
940 #endif
941   else if ((wFwVerRsp & 0x0000FFFF) == wFwVer) {
942     /* skip CORE_RESET and CORE_INIT from Brcm */
943     if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x00 &&
944         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x01) {
945       //            *rsp_len = 6;
946       //
947       //            NXPLOG_NCIHAL_D("> Going - core reset optimization");
948       //
949       //            p_rsp_data[0] = 0x40;
950       //            p_rsp_data[1] = 0x00;
951       //            p_rsp_data[2] = 0x03;
952       //            p_rsp_data[3] = 0x00;
953       //            p_rsp_data[4] = 0x10;
954       //            p_rsp_data[5] = 0x01;
955       //
956       //            status = NFCSTATUS_FAILED;
957       //            NXPLOG_NCIHAL_D("> Going - core reset optimization - END");
958     }
959     /* CORE_INIT */
960     else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x01 &&
961              p_cmd_data[2] == 0x00) {
962       //            NXPLOG_NCIHAL_D("> Going - core init optimization");
963       //            *rsp_len = iCoreInitRspLen;
964       //            memcpy(p_rsp_data, bCoreInitRsp, iCoreInitRspLen);
965       //            status = NFCSTATUS_FAILED;
966       //            NXPLOG_NCIHAL_D("> Going - core init optimization - END");
967     }
968   }
969   if (!phNxpTempMgr::GetInstance().IsICTempOk()) {
970     NXPLOG_NCIHAL_E("> IC Temp is NOK");
971     status = phNxpNciHal_process_screen_state_cmd(cmd_len, p_cmd_data, rsp_len,
972                                                   p_rsp_data);
973   }
974   /* CORE_SET_POWER_SUB_STATE */
975   if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x09 && p_cmd_data[2] == 0x01 &&
976       (p_cmd_data[3] == 0x00 || p_cmd_data[3] == 0x02)) {
977     // Sync power tracker data for screen on transition.
978     if (gPowerTrackerHandle.stateChange != NULL) {
979       gPowerTrackerHandle.stateChange(SCREEN_ON);
980     }
981   }
982 
983   return status;
984 }
985 
986 /******************************************************************************
987  * Function         phNxpNciHal_send_ext_cmd
988  *
989  * Description      This function send the extension command to NFCC. No
990  *                  response is checked by this function but it waits for
991  *                  the response to come.
992  *
993  * Returns          Returns NFCSTATUS_SUCCESS if sending cmd is successful and
994  *                  response is received.
995  *
996  ******************************************************************************/
phNxpNciHal_send_ext_cmd(uint16_t cmd_len,uint8_t * p_cmd)997 NFCSTATUS phNxpNciHal_send_ext_cmd(uint16_t cmd_len, uint8_t* p_cmd) {
998   NFCSTATUS status = NFCSTATUS_FAILED;
999   nxpncihal_ctrl.cmd_len = cmd_len;
1000   memcpy(nxpncihal_ctrl.p_cmd_data, p_cmd, cmd_len);
1001   status = phNxpNciHal_process_ext_cmd_rsp(nxpncihal_ctrl.cmd_len,
1002                                            nxpncihal_ctrl.p_cmd_data);
1003 
1004   return status;
1005 }
1006 
1007 /******************************************************************************
1008  * Function         hal_extns_write_rsp_timeout_cb
1009  *
1010  * Description      Timer call back function
1011  *
1012  * Returns          None
1013  *
1014  ******************************************************************************/
hal_extns_write_rsp_timeout_cb(uint32_t timerId,void * pContext)1015 static void hal_extns_write_rsp_timeout_cb(uint32_t timerId, void* pContext) {
1016   UNUSED_PROP(timerId);
1017   UNUSED_PROP(pContext);
1018   NXPLOG_NCIHAL_D("hal_extns_write_rsp_timeout_cb - write timeout!!!");
1019   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_FAILED;
1020   usleep(1);
1021   sem_post(&(nxpncihal_ctrl.syncSpiNfc));
1022   SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1023 
1024   return;
1025 }
1026 
1027 /*******************************************************************************
1028  **
1029  ** Function:        request_EEPROM()
1030  **
1031  ** Description:     get and set EEPROM data
1032  **                  In case of request_modes GET_EEPROM_DATA or
1033  *SET_EEPROM_DATA,
1034  **                   1.caller has to pass the buffer and the length of data
1035  *required
1036  **                     to be read/written.
1037  **                   2.Type of information required to be read/written
1038  **                     (Example - EEPROM_RF_CFG)
1039  **
1040  ** Returns:         Returns NFCSTATUS_SUCCESS if sending cmd is successful and
1041  **                  status failed if not successful
1042  **
1043  *******************************************************************************/
request_EEPROM(phNxpNci_EEPROM_info_t * mEEPROM_info)1044 NFCSTATUS request_EEPROM(phNxpNci_EEPROM_info_t* mEEPROM_info) {
1045   NXPLOG_NCIHAL_D(
1046       "%s Enter  request_type : 0x%02x,  request_mode : 0x%02x,  bufflen : "
1047       "0x%02x",
1048       __func__, mEEPROM_info->request_type, mEEPROM_info->request_mode,
1049       mEEPROM_info->bufflen);
1050   NFCSTATUS status = NFCSTATUS_FAILED;
1051   uint8_t retry_cnt = 0;
1052   uint8_t getCfgStartIndex = 0x08;
1053   uint8_t setCfgStartIndex = 0x07;
1054   uint8_t memIndex = 0x00;
1055   uint8_t fieldLen = 0x01;  // Memory field len 1bytes
1056   char addr[2] = {0};
1057   uint8_t cur_value = 0, len = 5;
1058   uint8_t b_position = 0;
1059   bool_t update_req = false;
1060   uint16_t set_cfg_cmd_len = 0;
1061   uint8_t *set_cfg_eeprom, *base_addr;
1062 
1063   mEEPROM_info->update_mode = BITWISE;
1064 
1065   switch (mEEPROM_info->request_type) {
1066     case EEPROM_RF_CFG:
1067       memIndex = 0x00;
1068       fieldLen = 0x20;
1069       len = fieldLen + 4;  // 4 - numParam+2add+val
1070       addr[0] = 0xA0;
1071       addr[1] = 0x14;
1072       mEEPROM_info->update_mode = BYTEWISE;
1073       break;
1074 
1075     case EEPROM_FW_DWNLD:
1076       fieldLen = 0x20;
1077       memIndex = 0x0C;
1078       len = fieldLen + 4;
1079       addr[0] = 0xA0;
1080       addr[1] = 0x0F;
1081       mEEPROM_info->update_mode = BYTEWISE;
1082       break;
1083 
1084     case EEPROM_WIREDMODE_RESUME_TIMEOUT:
1085       mEEPROM_info->update_mode = BYTEWISE;
1086       memIndex = 0x00;
1087       fieldLen = 0x04;
1088       len = fieldLen + 4;
1089       addr[0] = 0xA0;
1090       addr[1] = 0xFC;
1091       break;
1092 
1093     case EEPROM_ESE_SVDD_POWER:
1094       b_position = 0;
1095       memIndex = 0x00;
1096       addr[0] = 0xA0;
1097       addr[1] = 0xF2;
1098       break;
1099     case EEPROM_ESE_POWER_EXT_PMU:
1100       mEEPROM_info->update_mode = BYTEWISE;
1101       memIndex = 0x00;
1102       addr[0] = 0xA0;
1103       addr[1] = 0xD7;
1104       break;
1105 
1106     case EEPROM_PROP_ROUTING:
1107       b_position = 7;
1108       memIndex = 0x00;
1109       addr[0] = 0xA0;
1110       addr[1] = 0x98;
1111       break;
1112 
1113     case EEPROM_ESE_SESSION_ID:
1114       b_position = 0;
1115       memIndex = 0x00;
1116       addr[0] = 0xA0;
1117       addr[1] = 0xEB;
1118       break;
1119 
1120     case EEPROM_SWP1_INTF:
1121       b_position = 0;
1122       memIndex = 0x00;
1123       addr[0] = 0xA0;
1124       addr[1] = 0xEC;
1125       break;
1126 
1127     case EEPROM_SWP1A_INTF:
1128       b_position = 0;
1129       memIndex = 0x00;
1130       addr[0] = 0xA0;
1131       addr[1] = 0xD4;
1132       break;
1133     case EEPROM_SWP2_INTF:
1134       b_position = 0;
1135       memIndex = 0x00;
1136       addr[0] = 0xA0;
1137       addr[1] = 0xED;
1138       break;
1139     case EEPROM_FLASH_UPDATE:
1140       /* This flag is no more used in MW */
1141       fieldLen = 0x20;
1142       memIndex = 0x00;
1143       len = fieldLen + 4;
1144       addr[0] = 0xA0;
1145       addr[1] = 0x0F;
1146       break;
1147     case EEPROM_AUTH_CMD_TIMEOUT:
1148       mEEPROM_info->update_mode = BYTEWISE;
1149       memIndex = 0x00;
1150       fieldLen = mEEPROM_info->bufflen;
1151       len = fieldLen + 4;
1152       addr[0] = 0xA0;
1153       addr[1] = 0xF7;
1154       break;
1155     case EEPROM_GUARD_TIMER:
1156       mEEPROM_info->update_mode = BYTEWISE;
1157       memIndex = 0x00;
1158       addr[0] = 0xA1;
1159       addr[1] = 0x0B;
1160       break;
1161     case EEPROM_AUTONOMOUS_MODE:
1162       mEEPROM_info->update_mode = BYTEWISE;
1163       memIndex = 0x00;
1164       addr[0] = 0xA0;
1165       addr[1] = 0x15;
1166       break;
1167     case EEPROM_T4T_NFCEE_ENABLE:
1168       mEEPROM_info->update_mode = BYTEWISE;
1169       b_position = 0;
1170       memIndex = 0x00;
1171       addr[0] = 0xA0;
1172       addr[1] = 0x95;
1173       break;
1174     case EEPROM_CE_PHONE_OFF_CFG:
1175       mEEPROM_info->update_mode = BYTEWISE;
1176       b_position = 0;
1177       memIndex = 0x00;
1178       addr[0] = 0xA0;
1179       addr[1] = 0x8E;
1180       break;
1181     case EEPROM_ENABLE_VEN_CFG:
1182       mEEPROM_info->update_mode = BYTEWISE;
1183       b_position = 0;
1184       memIndex = 0x00;
1185       addr[0] = 0xA0;
1186       addr[1] = 0x07;
1187       break;
1188     case EEPROM_ISODEP_MERGE_SAK:
1189       mEEPROM_info->update_mode = BYTEWISE;
1190       b_position = 0;
1191       memIndex = 0x00;
1192       addr[0] = 0xA1;
1193       addr[1] = 0x1B;
1194       break;
1195     case EEPROM_SRD_TIMEOUT:
1196       mEEPROM_info->update_mode = BYTEWISE;
1197       memIndex = 0x00;
1198       fieldLen = 0x02;
1199       len = fieldLen + 4;
1200       addr[0] = 0xA1;
1201       addr[1] = 0x17;
1202       break;
1203     case EEPROM_UICC1_SESSION_ID:
1204       fieldLen = mEEPROM_info->bufflen;
1205       len = fieldLen + 4;
1206       memIndex = 0x00;
1207       addr[0] = 0xA0;
1208       addr[1] = 0xE4;
1209       mEEPROM_info->update_mode = BYTEWISE;
1210       break;
1211     case EEPROM_UICC2_SESSION_ID:
1212       fieldLen = mEEPROM_info->bufflen;
1213       len = fieldLen + 4;
1214       memIndex = 0x00;
1215       addr[0] = 0xA0;
1216       addr[1] = 0xE5;
1217       mEEPROM_info->update_mode = BYTEWISE;
1218       break;
1219     case EEPROM_CE_ACT_NTF:
1220       mEEPROM_info->update_mode = BYTEWISE;
1221       b_position = 0;
1222       memIndex = 0x00;
1223       addr[0] = 0xA0;
1224       addr[1] = 0x96;
1225       break;
1226     case EEPROM_UICC_HCI_CE_STATE:
1227       fieldLen = mEEPROM_info->bufflen;
1228       len = fieldLen + 4;
1229       memIndex = 0x00;
1230       addr[0] = 0xA0;
1231       addr[1] = 0xE6;
1232       mEEPROM_info->update_mode = BYTEWISE;
1233       break;
1234     case EEPROM_EXT_FIELD_DETECT_MODE:
1235       mEEPROM_info->update_mode = BYTEWISE;
1236       b_position = 0;
1237       memIndex = 0x00;
1238       addr[0] = 0xA1;
1239       addr[1] = 0x36;
1240       break;
1241     case EEPROM_CONF_GPIO_CTRL:
1242       mEEPROM_info->update_mode = BYTEWISE;
1243       memIndex = 0x00;
1244       fieldLen = mEEPROM_info->bufflen;
1245       len = fieldLen + 4;
1246       addr[0] = 0xA1;
1247       addr[1] = 0x0F;
1248       break;
1249     case EEPROM_SET_GPIO_VALUE:
1250       mEEPROM_info->update_mode = BYTEWISE;
1251       memIndex = 0x00;
1252       fieldLen = mEEPROM_info->bufflen;
1253       len = fieldLen + 4;
1254       addr[0] = 0xA1;
1255       addr[1] = 0x65;
1256       break;
1257     case EEPROM_POWER_TRACKER_ENABLE:
1258       mEEPROM_info->update_mode = BYTEWISE;
1259       memIndex = 0x00;
1260       fieldLen = mEEPROM_info->bufflen;
1261       len = fieldLen + 4;
1262       addr[0] = 0xA0;
1263       addr[1] = 0x6D;
1264       break;
1265     case EEPROM_VDDPA:
1266       mEEPROM_info->update_mode = BYTEWISE;
1267       memIndex = 0x14;
1268       fieldLen = 0x30;
1269       len = fieldLen + 4;
1270       addr[0] = 0xA0;
1271       addr[1] = 0x0E;
1272       break;
1273     default:
1274       ALOGE("No valid request information found");
1275       break;
1276   }
1277 
1278   uint8_t get_cfg_eeprom[6] = {
1279       0x20,              // get_cfg header
1280       0x03,              // get_cfg header
1281       0x03,              // len of following value
1282       0x01,              // Num Parameters
1283       (uint8_t)addr[0],  // First byte of Address
1284       (uint8_t)addr[1]   // Second byte of Address
1285   };
1286   uint8_t set_cfg_cmd_hdr[7] = {
1287       0x20,              // set_cfg header
1288       0x02,              // set_cfg header
1289       len,               // len of following value
1290       0x01,              // Num Param
1291       (uint8_t)addr[0],  // First byte of Address
1292       (uint8_t)addr[1],  // Second byte of Address
1293       fieldLen           // Data len
1294   };
1295 
1296   set_cfg_cmd_len = sizeof(set_cfg_cmd_hdr) + fieldLen;
1297   set_cfg_eeprom = (uint8_t*)malloc(set_cfg_cmd_len);
1298   if (set_cfg_eeprom == NULL) {
1299     ALOGE("memory allocation failed");
1300     return status;
1301   }
1302   base_addr = set_cfg_eeprom;
1303   memset(set_cfg_eeprom, 0, set_cfg_cmd_len);
1304   memcpy(set_cfg_eeprom, set_cfg_cmd_hdr, sizeof(set_cfg_cmd_hdr));
1305 
1306 retryget:
1307   status = phNxpNciHal_send_ext_cmd(sizeof(get_cfg_eeprom), get_cfg_eeprom);
1308   if (status == NFCSTATUS_SUCCESS) {
1309     status = nxpncihal_ctrl.p_rx_data[3];
1310     if (status != NFCSTATUS_SUCCESS) {
1311       ALOGE("failed to get requested memory address");
1312     } else if (mEEPROM_info->request_mode == GET_EEPROM_DATA) {
1313       if (mEEPROM_info->bufflen == 0xFF) {
1314         /* Max bufferlenth for single Get Config Command is 0xFF.
1315          * If buffer length set to max value, reassign buffer value
1316          * depends on response from Get Config command */
1317         mEEPROM_info->bufflen =
1318             *(nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex - 1);
1319       }
1320       memcpy(mEEPROM_info->buffer,
1321              nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex,
1322              mEEPROM_info->bufflen);
1323     } else if (mEEPROM_info->request_mode == SET_EEPROM_DATA) {
1324       // Clear the buffer first
1325       memset(set_cfg_eeprom + setCfgStartIndex, 0x00,
1326              (set_cfg_cmd_len - setCfgStartIndex));
1327 
1328       // copy get config data into set_cfg_eeprom
1329       memcpy(set_cfg_eeprom + setCfgStartIndex,
1330              nxpncihal_ctrl.p_rx_data + getCfgStartIndex, fieldLen);
1331       if (mEEPROM_info->update_mode == BITWISE) {
1332         cur_value =
1333             (set_cfg_eeprom[setCfgStartIndex + memIndex] >> b_position) & 0x01;
1334         if (cur_value != mEEPROM_info->buffer[0]) {
1335           update_req = true;
1336           if (mEEPROM_info->buffer[0] == 1) {
1337             set_cfg_eeprom[setCfgStartIndex + memIndex] |= (1 << b_position);
1338           } else if (mEEPROM_info->buffer[0] == 0) {
1339             set_cfg_eeprom[setCfgStartIndex + memIndex] &= (~(1 << b_position));
1340           }
1341         }
1342       } else if (mEEPROM_info->update_mode == BYTEWISE) {
1343         if (memcmp(set_cfg_eeprom + setCfgStartIndex + memIndex,
1344                    mEEPROM_info->buffer, mEEPROM_info->bufflen)) {
1345           update_req = true;
1346           memcpy(set_cfg_eeprom + setCfgStartIndex + memIndex,
1347                  mEEPROM_info->buffer, mEEPROM_info->bufflen);
1348         }
1349       } else {
1350         ALOGE("%s, invalid update mode", __func__);
1351       }
1352 
1353       if (update_req) {
1354       // do set config
1355       retryset:
1356         status = phNxpNciHal_send_ext_cmd(set_cfg_cmd_len, set_cfg_eeprom);
1357         if (status != NFCSTATUS_SUCCESS && retry_cnt < 3) {
1358           retry_cnt++;
1359           ALOGE("Set Cfg Retry cnt=%x", retry_cnt);
1360           goto retryset;
1361         }
1362       } else {
1363         ALOGD("%s: values are same no update required", __func__);
1364       }
1365     }
1366   } else if (retry_cnt < 3) {
1367     retry_cnt++;
1368     ALOGE("Get Cfg Retry cnt=%x", retry_cnt);
1369     goto retryget;
1370   }
1371 
1372   if (base_addr != NULL) {
1373     free(base_addr);
1374     base_addr = NULL;
1375   }
1376   retry_cnt = 0;
1377   return status;
1378 }
1379 
1380 /*******************************************************************************
1381  **
1382  ** Function:        phNxpNciHal_enableDefaultUICC2SWPline()
1383  **
1384  ** Description:     Select UICC2 or UICC3
1385  **
1386  ** Returns:         status
1387  **
1388  ********************************************************************************/
phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel)1389 NFCSTATUS phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel) {
1390   NFCSTATUS status = NFCSTATUS_FAILED;
1391   uint8_t p_data[255] = {NCI_MT_CMD, NXP_CORE_SET_CONFIG_CMD};
1392   uint8_t LEN_INDEX = 2, PARAM_INDEX = 3;
1393   uint8_t* p = p_data;
1394   NXPLOG_NCIHAL_D("phNxpNciHal_enableDefaultUICC2SWPline %d", uicc2_sel);
1395   p_data[LEN_INDEX] = 1;
1396   p += 4;
1397   if (uicc2_sel == 0x03) {
1398     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1399     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWP2);
1400     UINT8_TO_STREAM(p, 0x01);
1401     UINT8_TO_STREAM(p, 0x01);
1402     p_data[LEN_INDEX] += 4;
1403     p_data[PARAM_INDEX] += 1;
1404   }
1405   if (uicc2_sel == 0x04) {
1406     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1407     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWPUICC3);
1408     UINT8_TO_STREAM(p, 0x01);
1409     UINT8_TO_STREAM(p, 0x01);
1410     p_data[LEN_INDEX] += 4;
1411     p_data[PARAM_INDEX] += 1;
1412   }
1413   if (p_data[PARAM_INDEX] > 0x00)
1414     status = phNxpNciHal_send_ext_cmd(p - p_data, p_data);
1415   return status;
1416 }
1417 
1418 /******************************************************************************
1419  * Function         phNxpNciHal_prop_conf_lpcd
1420  *
1421  * Description      If NFCC is not in Nfc Forum mode, then this function will
1422  *                  configure it back to the Nfc Forum mode.
1423  *
1424  * Returns          none
1425  *
1426  ******************************************************************************/
phNxpNciHal_prop_conf_lpcd(bool enableLPCD)1427 void phNxpNciHal_prop_conf_lpcd(bool enableLPCD) {
1428   uint8_t cmd_get_lpcdval[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x68};
1429   vector<uint8_t> cmd_set_lpcdval{0x20, 0x02, 0x2E};
1430 
1431   if (NFCSTATUS_SUCCESS ==
1432       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_lpcdval), cmd_get_lpcdval)) {
1433     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1434       if (!(nxpncihal_ctrl.p_rx_data[17] & (1 << 7)) && enableLPCD) {
1435         nxpncihal_ctrl.p_rx_data[17] |= (1 << 7);
1436         cmd_set_lpcdval.insert(
1437             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1438             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1439         if (NFCSTATUS_SUCCESS ==
1440             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1441                                      &cmd_set_lpcdval[0])) {
1442           return;
1443         }
1444       } else if (!enableLPCD && (nxpncihal_ctrl.p_rx_data[17] & (1 << 7))) {
1445         nxpncihal_ctrl.p_rx_data[17] &= ~(1 << 7);
1446         cmd_set_lpcdval.insert(
1447             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1448             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1449         if (NFCSTATUS_SUCCESS ==
1450             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1451                                      &cmd_set_lpcdval[0])) {
1452           return;
1453         }
1454       } else {
1455         return;
1456       }
1457     }
1458   }
1459   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1460   return;
1461 }
1462 
1463 /******************************************************************************
1464  * Function         phNxpNciHal_prop_conf_rssi
1465  *
1466  * Description      It resets RSSI param to default value.
1467  *
1468  * Returns          none
1469  *
1470  ******************************************************************************/
phNxpNciHal_prop_conf_rssi()1471 void phNxpNciHal_prop_conf_rssi() {
1472   if (IS_CHIP_TYPE_L(sn220u)) {
1473     NXPLOG_NCIHAL_D("%s: feature is not supported", __func__);
1474     return;
1475   }
1476   vector<uint8_t> cmd_get_rssival = {0x20, 0x03, 0x03, 0x01, 0xA1, 0x55};
1477   vector<uint8_t> cmd_set_rssival = {0x20, 0x02, 0x06, 0x01, 0xA1,
1478                                      0x55, 0x02, 0x00, 0x00};
1479 
1480   if (NFCSTATUS_SUCCESS !=
1481       phNxpNciHal_send_ext_cmd(cmd_get_rssival.size(), &cmd_get_rssival[0])) {
1482     NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1483     return;
1484   }
1485   if ((nxpncihal_ctrl.rx_data_len <= 9) ||
1486       (NFCSTATUS_SUCCESS != nxpncihal_ctrl.p_rx_data[3])) {
1487     NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1488     return;
1489   }
1490   if ((nxpncihal_ctrl.p_rx_data[8] != 0x00) ||
1491       (nxpncihal_ctrl.p_rx_data[9] != 0x00)) {
1492     if (NFCSTATUS_SUCCESS !=
1493         phNxpNciHal_send_ext_cmd(cmd_set_rssival.size(), &cmd_set_rssival[0])) {
1494       NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1495       return;
1496     }
1497   }
1498 
1499   return;
1500 }
1501 
1502 /******************************************************************************
1503  * Function         phNxpNciHal_conf_nfc_forum_mode
1504  *
1505  * Description      If NFCC is not in Nfc Forum mode, then this function will
1506  *                  configure it back to the Nfc Forum mode.
1507  *
1508  * Returns          none
1509  *
1510  ******************************************************************************/
phNxpNciHal_conf_nfc_forum_mode()1511 void phNxpNciHal_conf_nfc_forum_mode() {
1512   uint8_t cmd_get_emvcocfg[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x44};
1513   uint8_t cmd_reset_emvcocfg[8];
1514   long cmdlen = 8;
1515   long retlen = 0;
1516 
1517   if (GetNxpByteArrayValue(NAME_NXP_PROP_RESET_EMVCO_CMD,
1518                            (char*)cmd_reset_emvcocfg, cmdlen, &retlen)) {
1519   }
1520   if (retlen != 0x08) {
1521     NXPLOG_NCIHAL_E("%s: command is not provided", __func__);
1522     return;
1523   }
1524   /* Update the flag address from the Nxp config file */
1525   cmd_get_emvcocfg[4] = cmd_reset_emvcocfg[4];
1526   cmd_get_emvcocfg[5] = cmd_reset_emvcocfg[5];
1527 
1528   if (NFCSTATUS_SUCCESS ==
1529       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_emvcocfg), cmd_get_emvcocfg)) {
1530     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1531       if (0x01 & nxpncihal_ctrl.p_rx_data[8]) {
1532         if (NFCSTATUS_SUCCESS ==
1533             phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_emvcocfg),
1534                                      cmd_reset_emvcocfg)) {
1535           return;
1536         }
1537       } else {
1538         return;
1539       }
1540     }
1541   }
1542   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1543   return;
1544 }
1545 
1546 /******************************************************************************
1547  * Function         RemoveNfcDepIntfFromInitResp
1548  *
1549  * Description      This function process the NCI_CORE_INIT_RESP & removes
1550  *                  remove the NFC_DEP interface support and modify the
1551  *                  CORE_INIT_RESP accordingly.
1552  *
1553  * Returns          None
1554  *
1555  ******************************************************************************/
RemoveNfcDepIntfFromInitResp(uint8_t * coreInitResp,uint16_t * coreInitRespLen)1556 void RemoveNfcDepIntfFromInitResp(uint8_t* coreInitResp,
1557                                   uint16_t* coreInitRespLen) {
1558   /* As per NCI 2.0 index Number of Supported RF interfaces is 13 */
1559   uint8_t indexOfSupportedRfIntf = 13;
1560   /* as per NCI 2.0 Number of Supported RF Interfaces Payload field index is 13
1561    * & 3 bytes for NCI_MSG_HEADER */
1562   uint8_t noOfSupportedInterface =
1563       *(coreInitResp + indexOfSupportedRfIntf + NCI_HEADER_SIZE);
1564   uint8_t rfInterfacesLength =
1565       *coreInitRespLen - (indexOfSupportedRfIntf + 1 + NCI_HEADER_SIZE);
1566   uint8_t* supportedRfInterfaces = NULL;
1567   bool removeNfcDepRequired = false;
1568   if (noOfSupportedInterface) {
1569     supportedRfInterfaces =
1570         coreInitResp + indexOfSupportedRfIntf + 1 + NCI_HEADER_SIZE;
1571   }
1572   uint8_t* supportedRfInterfacesDetails = supportedRfInterfaces;
1573   /* Get the index of Supported RF Interface for NFC-DEP interface in CORE_INIT
1574    * Response*/
1575   for (int i = 0; i < noOfSupportedInterface; i++) {
1576     if (*supportedRfInterfaces == NCI_NFC_DEP_RF_INTF) {
1577       removeNfcDepRequired = true;
1578       break;
1579     }
1580     uint8_t noOfExtensions = *(supportedRfInterfaces + 1);
1581     /* 2 bytes for RF interface type & length of Extensions */
1582     supportedRfInterfaces += (2 + noOfExtensions);
1583   }
1584   /* If NFC-DEP is found in response then remove NFC-DEP from init response and
1585    * frame new CORE_INIT_RESP and send to upper layer*/
1586   if (!removeNfcDepRequired) {
1587     NXPLOG_NCIHAL_E("%s: NFC-DEP Removal is not requored !!", __func__);
1588     return;
1589   } else {
1590     coreInitResp[16] = noOfSupportedInterface - 1;
1591     uint8_t noBytesToSkipForNfcDep = 2 + *(supportedRfInterfaces + 1);
1592     memcpy(supportedRfInterfaces,
1593            supportedRfInterfaces + noBytesToSkipForNfcDep,
1594            (rfInterfacesLength -
1595             ((supportedRfInterfaces - supportedRfInterfacesDetails) +
1596              noBytesToSkipForNfcDep)));
1597     *coreInitRespLen -= noBytesToSkipForNfcDep;
1598     coreInitResp[2] -= noBytesToSkipForNfcDep;
1599 
1600     /* Print updated CORE_INIT_RESP for debug purpose*/
1601     phNxpNciHal_print_packet("DEBUG", coreInitResp, *coreInitRespLen);
1602   }
1603 }
1604 
1605 /******************************************************************************
1606  * Function         phNxpNciHal_process_screen_state_cmd
1607  *
1608  * Description      Forms a dummy response for cmds sent during screen
1609  *                  state change,when IC temp is not normal.
1610  *                  These Cmds are not forwarded to NFCC.
1611  *
1612  * Returns          Returns NFCSTATUS_FAILED for screen state change cmd
1613  *                  or returns NFCSTATUS_SUCCESS.
1614  *
1615  ******************************************************************************/
phNxpNciHal_process_screen_state_cmd(uint16_t * cmd_len,uint8_t * p_cmd_data,uint16_t * rsp_len,uint8_t * p_rsp_data)1616 static NFCSTATUS phNxpNciHal_process_screen_state_cmd(uint16_t* cmd_len,
1617                                                       uint8_t* p_cmd_data,
1618                                                       uint16_t* rsp_len,
1619                                                       uint8_t* p_rsp_data) {
1620   NFCSTATUS status = NFCSTATUS_SUCCESS;
1621   if (*cmd_len == 7 && p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
1622       p_cmd_data[2] == 0x04 && p_cmd_data[4] == 0x02) {
1623     // CON_DISCOVERY_PARAM
1624     phNxpNciHal_print_packet("SEND", p_cmd_data, 7);
1625     *rsp_len = 5;
1626     p_rsp_data[0] = 0x40;
1627     p_rsp_data[1] = 0x02;
1628     p_rsp_data[2] = 0x02;
1629     p_rsp_data[3] = 0x00;
1630     p_rsp_data[4] = 0x00;
1631     NXPLOG_NCIHAL_E(
1632         "> Sending fake response for CON_DISCOVERY_PARAM set config");
1633     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
1634     status = NFCSTATUS_FAILED;
1635   } else if (*cmd_len == 4 && p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x09) {
1636     phNxpNciHal_print_packet("SEND", p_cmd_data, 4);
1637     *rsp_len = 4;
1638     p_rsp_data[0] = 0x40;
1639     p_rsp_data[1] = 0x09;
1640     p_rsp_data[2] = 0x01;
1641     p_rsp_data[3] = 0x00;
1642     NXPLOG_NCIHAL_E("> Sending fake response for POWER_SUB_STATE cmd");
1643     phNxpNciHal_print_packet("RECV", p_rsp_data, 4);
1644     status = NFCSTATUS_FAILED;
1645   }
1646   return status;
1647 }
1648 
1649 /******************************************************************************
1650  * Function         phNxpNciHal_update_core_reset_ntf_prop
1651  *
1652  * Description      This function updates the vendor property which keep track
1653  *                  core reset ntf count for fw recovery.
1654  *
1655  * Returns          void
1656  *
1657  *****************************************************************************/
1658 
phNxpNciHal_update_core_reset_ntf_prop()1659 static bool phNxpNciHal_update_core_reset_ntf_prop() {
1660   NXPLOG_NCIHAL_D("%s: Entry", __func__);
1661   bool is_abort_req = true;
1662   int32_t core_reset_count =
1663       phNxpNciHal_getVendorProp_int32(core_reset_ntf_count_prop_name, 0);
1664   if (core_reset_count == CORE_RESET_NTF_RECOVERY_REQ_COUNT) {
1665     NXPLOG_NCIHAL_D("%s: Notify main thread of fresh ntf received", __func__);
1666     sem_post(&sem_reset_ntf_received);
1667     is_abort_req = false;
1668   }
1669   ++core_reset_count;
1670   std::string ntf_count_str = std::to_string(core_reset_count);
1671   NXPLOG_NCIHAL_D("Core reset counter prop value  %d", core_reset_count);
1672   if (NFCSTATUS_SUCCESS !=
1673       phNxpNciHal_setVendorProp(core_reset_ntf_count_prop_name,
1674                                 ntf_count_str.c_str())) {
1675     NXPLOG_NCIHAL_D("setting core_reset_ntf_count_prop failed");
1676   }
1677   NXPLOG_NCIHAL_D("%s: Exit", __func__);
1678   return is_abort_req;
1679 }
1680