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   /* Parsing CORE_RESET_RSP and CORE_RESET_NTF to update NCI version.*/
439   if (p_ntf == NULL || *p_len < 2) {
440     return NFCSTATUS_FAILED;
441   }
442   if (p_ntf[0] == NCI_MT_RSP &&
443       ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
444     if (*p_len < 4) {
445       android_errorWriteLog(0x534e4554, "169258455");
446       return NFCSTATUS_FAILED;
447     }
448     if (p_ntf[2] == 0x01 && p_ntf[3] == 0x00) {
449       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI2.0");
450       if (nxpncihal_ctrl.hal_ext_enabled == TRUE &&
451           nxpncihal_ctrl.isCoreRstForFwDnld != TRUE) {
452         nxpncihal_ctrl.nci_info.wait_for_ntf = TRUE;
453       }
454     } else if (p_ntf[2] == 0x03 && p_ntf[3] == 0x00) {
455       if (*p_len < 5) {
456         android_errorWriteLog(0x534e4554, "169258455");
457         return NFCSTATUS_FAILED;
458       }
459       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI1.0");
460       nxpncihal_ctrl.nci_info.nci_version = p_ntf[4];
461     } else
462       status = NFCSTATUS_FAILED;
463   } else if (p_ntf[0] == NCI_MT_NTF &&
464              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
465     if (*p_len < 4) {
466       android_errorWriteLog(0x534e4554, "169258455");
467       return NFCSTATUS_FAILED;
468     }
469     if (p_ntf[3] == CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED) {
470       if (*p_len < 6) {
471         android_errorWriteLog(0x534e4554, "169258455");
472         return NFCSTATUS_FAILED;
473       }
474       NXPLOG_NCIHAL_D("CORE_RESET_NTF NCI2.0 reason CORE_RESET_CMD received !");
475       nxpncihal_ctrl.nci_info.nci_version = p_ntf[5];
476       if (!nxpncihal_ctrl.hal_open_status)
477         phNxpNciHal_configFeatureList(p_ntf, *p_len);
478       int len = p_ntf[2] + 2; /*include 2 byte header*/
479       if (len != *p_len - 1) {
480         NXPLOG_NCIHAL_E(
481             "phNxpNciHal_ext_process_nfc_init_rsp invalid NTF length");
482         android_errorWriteLog(0x534e4554, "121263487");
483         return NFCSTATUS_FAILED;
484       }
485       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
486                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
487       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
488                       p_ntf[len - 1], p_ntf[len]);
489     } else {
490       bool is_abort_req = true;
491       if ((p_ntf[3] == CORE_RESET_TRIGGER_TYPE_WATCHDOG_RESET ||
492            p_ntf[3] == CORE_RESET_TRIGGER_TYPE_FW_ASSERT) ||
493           ((p_ntf[3] == CORE_RESET_TRIGGER_TYPE_UNRECOVERABLE_ERROR) &&
494            (p_ntf[4] == CORE_RESET_TRIGGER_TYPE_WATCHDOG_RESET ||
495             p_ntf[4] == CORE_RESET_TRIGGER_TYPE_FW_ASSERT))) {
496         /* WA : In some cases for Watchdog reset FW sends reset reason code as
497          * unrecoverable error and config status as WATCHDOG_RESET */
498         is_abort_req = phNxpNciHal_update_core_reset_ntf_prop();
499       }
500       if (is_abort_req) phNxpNciHal_emergency_recovery(p_ntf[3]);
501       status = NFCSTATUS_FAILED;
502     } /* Parsing CORE_INIT_RSP*/
503   } else if (p_ntf[0] == NCI_MT_RSP &&
504              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_INIT)) {
505     if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
506       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI2.0 received !");
507       /* Remove NFC-DEP interface support from INIT RESP */
508       RemoveNfcDepIntfFromInitResp(p_ntf, p_len);
509     } else {
510       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI1.0 received !");
511       if (!nxpncihal_ctrl.hal_open_status &&
512           nxpncihal_ctrl.nci_info.nci_version != NCI_VERSION_2_0) {
513         phNxpNciHal_configFeatureList(p_ntf, *p_len);
514       }
515       if (*p_len < 3) {
516         android_errorWriteLog(0x534e4554, "169258455");
517         return NFCSTATUS_FAILED;
518       }
519       int len = p_ntf[2] + 2; /*include 2 byte header*/
520       if (len != *p_len - 1) {
521         NXPLOG_NCIHAL_E(
522             "phNxpNciHal_ext_process_nfc_init_rsp invalid NTF length");
523         android_errorWriteLog(0x534e4554, "121263487");
524         return NFCSTATUS_FAILED;
525       }
526       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
527                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
528       if (wFwVerRsp == 0) status = NFCSTATUS_FAILED;
529       iCoreInitRspLen = *p_len;
530       memcpy(bCoreInitRsp, p_ntf, *p_len);
531       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
532                       p_ntf[len - 1], p_ntf[len]);
533     }
534   }
535   return status;
536 }
537 
538 /******************************************************************************
539  * Function         phNxpNciHal_process_ext_cmd_rsp
540  *
541  * Description      This function process the extension command response. It
542  *                  also checks the received response to expected response.
543  *
544  * Returns          returns NFCSTATUS_SUCCESS if response is as expected else
545  *                  returns failure.
546  *
547  ******************************************************************************/
phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,uint8_t * p_cmd)548 static NFCSTATUS phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,
549                                                  uint8_t* p_cmd) {
550   NFCSTATUS status = NFCSTATUS_FAILED;
551   uint16_t data_written = 0;
552 
553   /* Check NCI Command is well formed */
554   if (!phTmlNfc_IsFwDnldModeEnabled() &&
555       (cmd_len != (p_cmd[2] + NCI_MODE_HEADER_LEN))) {
556     NXPLOG_NCIHAL_E("NCI command not well formed");
557     return NFCSTATUS_FAILED;
558   }
559 
560   /* Create the local semaphore */
561   if (phNxpNciHal_init_cb_data(&nxpncihal_ctrl.ext_cb_data, NULL) !=
562       NFCSTATUS_SUCCESS) {
563     NXPLOG_NCIHAL_D("Create ext_cb_data failed");
564     return NFCSTATUS_FAILED;
565   }
566 
567   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_SUCCESS;
568 
569   /* Send ext command */
570   data_written = phNxpNciHal_write_unlocked(cmd_len, p_cmd, ORIG_NXPHAL);
571   if (data_written != cmd_len) {
572     NXPLOG_NCIHAL_D("phNxpNciHal_write failed for hal ext");
573     goto clean_and_return;
574   }
575 
576   /* Start timer */
577   status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
578                                  &hal_extns_write_rsp_timeout_cb, NULL);
579   if (NFCSTATUS_SUCCESS == status) {
580     NXPLOG_NCIHAL_D("Response timer started");
581   } else {
582     NXPLOG_NCIHAL_E("Response timer not started!!!");
583     status = NFCSTATUS_FAILED;
584     goto clean_and_return;
585   }
586 
587   /* Wait for rsp */
588   NXPLOG_NCIHAL_D("Waiting after ext cmd sent");
589   if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
590     NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
591     goto clean_and_return;
592   }
593 
594   /* Stop Timer */
595   status = phOsalNfc_Timer_Stop(timeoutTimerId);
596   if (NFCSTATUS_SUCCESS == status) {
597     NXPLOG_NCIHAL_D("Response timer stopped");
598   } else {
599     NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
600     status = NFCSTATUS_FAILED;
601     goto clean_and_return;
602   }
603 
604   if (cmd_len < 3) {
605     android_errorWriteLog(0x534e4554, "153880630");
606     status = NFCSTATUS_FAILED;
607     goto clean_and_return;
608   }
609 
610   /* No NTF expected for OMAPI command */
611   if (p_cmd[0] == 0x2F && p_cmd[1] == 0x1 && p_cmd[2] == 0x01) {
612     nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
613   }
614   /* Start timer to wait for NTF*/
615   if (nxpncihal_ctrl.nci_info.wait_for_ntf == TRUE) {
616     status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
617                                    &hal_extns_write_rsp_timeout_cb, NULL);
618     if (NFCSTATUS_SUCCESS == status) {
619       NXPLOG_NCIHAL_D("Response timer started");
620     } else {
621       NXPLOG_NCIHAL_E("Response timer not started!!!");
622       status = NFCSTATUS_FAILED;
623       goto clean_and_return;
624     }
625     if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
626       NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
627       /* Stop Timer */
628       status = phOsalNfc_Timer_Stop(timeoutTimerId);
629       goto clean_and_return;
630     }
631     status = phOsalNfc_Timer_Stop(timeoutTimerId);
632     if (NFCSTATUS_SUCCESS == status) {
633       NXPLOG_NCIHAL_D("Response timer stopped");
634     } else {
635       NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
636       status = NFCSTATUS_FAILED;
637       goto clean_and_return;
638     }
639   }
640 
641   if (nxpncihal_ctrl.ext_cb_data.status != NFCSTATUS_SUCCESS) {
642     NXPLOG_NCIHAL_E(
643         "Callback Status is failed!! Timer Expired!! Couldn't read it! 0x%x",
644         nxpncihal_ctrl.ext_cb_data.status);
645     status = NFCSTATUS_FAILED;
646     goto clean_and_return;
647   }
648 
649   NXPLOG_NCIHAL_D("Checking response");
650   status = NFCSTATUS_SUCCESS;
651 
652   /*Response check for Set config, Core Reset & Core init command sent part of
653    * HAL_EXT*/
654   if (nxpncihal_ctrl.hal_open_status == HAL_OPEN_CORE_INITIALIZING &&
655       nxpncihal_ctrl.p_rx_data[0] == 0x40 &&
656       nxpncihal_ctrl.p_rx_data[1] <= 0x02 &&
657       nxpncihal_ctrl.p_rx_data[2] != 0x00) {
658     status = nxpncihal_ctrl.p_rx_data[3];
659     if (status != NCI_STATUS_OK) {
660       /*Add 500ms delay for FW to flush circular buffer */
661       usleep(500 * 1000);
662       NXPLOG_NCIHAL_D("Status Failed. Status = 0x%02x", status);
663     }
664   }
665 
666   /*Response check for SYSTEM SET SERVICE STATUS command sent as part of
667    * HAL_EXT*/
668   if (IS_CHIP_TYPE_GE(sn220u) && nxpncihal_ctrl.p_rx_data[0] == 0x4F &&
669       nxpncihal_ctrl.p_rx_data[1] == 0x01 &&
670       nxpncihal_ctrl.p_rx_data[2] != 0x00) {
671     status = nxpncihal_ctrl.p_rx_data[3];
672   }
673 
674 clean_and_return:
675   phNxpNciHal_cleanup_cb_data(&nxpncihal_ctrl.ext_cb_data);
676   nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
677   HAL_DISABLE_EXT();
678   return status;
679 }
680 
681 /******************************************************************************
682  * Function         phNxpNciHal_write_ext
683  *
684  * Description      This function inform the status of phNxpNciHal_open
685  *                  function to libnfc-nci.
686  *
687  * Returns          It return NFCSTATUS_SUCCESS then continue with send else
688  *                  sends NFCSTATUS_FAILED direct response is prepared and
689  *                  do not send anything to NFCC.
690  *
691  ******************************************************************************/
phNxpNciHal_write_ext(uint16_t * cmd_len,uint8_t * p_cmd_data,uint16_t * rsp_len,uint8_t * p_rsp_data)692 NFCSTATUS phNxpNciHal_write_ext(uint16_t* cmd_len, uint8_t* p_cmd_data,
693                                 uint16_t* rsp_len, uint8_t* p_rsp_data) {
694   NFCSTATUS status = NFCSTATUS_SUCCESS;
695 
696   if (p_cmd_data[0] == PROPRIETARY_CMD_FELICA_READER_MODE &&
697       p_cmd_data[1] == PROPRIETARY_CMD_FELICA_READER_MODE &&
698       p_cmd_data[2] == PROPRIETARY_CMD_FELICA_READER_MODE) {
699     NXPLOG_NCIHAL_D("Received proprietary command to set Felica Reader mode:%d",
700                     p_cmd_data[3]);
701     gFelicaReaderMode = p_cmd_data[3];
702     /* frame the fake response */
703     *rsp_len = 4;
704     p_rsp_data[0] = 0x00;
705     p_rsp_data[1] = 0x00;
706     p_rsp_data[2] = 0x00;
707     p_rsp_data[3] = 0x00;
708     status = NFCSTATUS_FAILED;
709   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
710              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
711              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
712              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
713              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x01) {
714     nxpprofile_ctrl.profile_type = EMV_CO_PROFILE;
715     NXPLOG_NCIHAL_D("EMV_CO_PROFILE mode - Enabled");
716     status = NFCSTATUS_SUCCESS;
717   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
718              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
719              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
720              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
721              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x00) {
722     NXPLOG_NCIHAL_D("NFC_FORUM_PROFILE mode - Enabled");
723     nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
724     status = NFCSTATUS_SUCCESS;
725   }
726 
727   if (nxpprofile_ctrl.profile_type == EMV_CO_PROFILE) {
728     if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x06 &&
729         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x03) {
730 #if 0
731             //Needs clarification whether to keep it or not
732             NXPLOG_NCIHAL_D ("EmvCo Poll mode - RF Deactivate discard");
733             phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
734             *rsp_len = 4;
735             p_rsp_data[0] = 0x41;
736             p_rsp_data[1] = 0x06;
737             p_rsp_data[2] = 0x01;
738             p_rsp_data[3] = 0x00;
739             phNxpNciHal_print_packet("RECV", p_rsp_data, 4);
740             status = NFCSTATUS_FAILED;
741 #endif
742     } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
743       NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A and B");
744       p_cmd_data[2] = 0x05;
745       p_cmd_data[3] = 0x02;
746       p_cmd_data[4] = 0x00;
747       p_cmd_data[5] = 0x01;
748       p_cmd_data[6] = 0x01;
749       p_cmd_data[7] = 0x01;
750       *cmd_len = 8;
751     }
752   }
753 
754   if (mfc_mode == true && p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
755     NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A");
756     p_cmd_data[2] = 0x03;
757     p_cmd_data[3] = 0x01;
758     p_cmd_data[4] = 0x00;
759     p_cmd_data[5] = 0x01;
760     *cmd_len = 6;
761     mfc_mode = false;
762   }
763 
764   if (*cmd_len <= (NCI_MAX_DATA_LEN - 3) && bEnableMfcReader &&
765       (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x00) &&
766       (nxpprofile_ctrl.profile_type == NFC_FORUM_PROFILE)) {
767     if (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
768         p_cmd_data[4] == 0x80 && p_cmd_data[5] == 0x01 &&
769         p_cmd_data[6] == 0x83) {
770       mfc_mode = true;
771     } else {
772       if (bEnableMfcReader) {
773         NXPLOG_NCIHAL_D("Going through extns - Adding Mifare in RF Discovery");
774         p_cmd_data[2] += 3;
775         p_cmd_data[3] += 1;
776         p_cmd_data[*cmd_len] = 0x80;
777         p_cmd_data[*cmd_len + 1] = 0x01;
778         p_cmd_data[*cmd_len + 2] = 0x80;
779         *cmd_len += 3;
780         status = NFCSTATUS_SUCCESS;
781         NXPLOG_NCIHAL_D(
782             "Going through extns - Adding Mifare in RF Discovery - END");
783       }
784     }
785   } else if (icode_detected) {
786     if (IS_CHIP_TYPE_L(sn100u) && IS_CHIP_TYPE_NE(pn557) &&
787         (p_cmd_data[3] & 0x40) == 0x40 &&
788         (p_cmd_data[4] == 0x21 || p_cmd_data[4] == 0x22 ||
789          p_cmd_data[4] == 0x24 || p_cmd_data[4] == 0x27 ||
790          p_cmd_data[4] == 0x28 || p_cmd_data[4] == 0x29 ||
791          p_cmd_data[4] == 0x2a)) {
792       NXPLOG_NCIHAL_D("> Send EOF set");
793       icode_send_eof = 1;
794     }
795 
796     if (p_cmd_data[3] == 0x20 || p_cmd_data[3] == 0x24 ||
797         p_cmd_data[3] == 0x60) {
798       NXPLOG_NCIHAL_D("> NFC ISO_15693 Proprietary CMD ");
799       p_cmd_data[3] += 0x02;
800     }
801   } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
802     NXPLOG_NCIHAL_D("> Polling Loop Started");
803     icode_detected = 0;
804     if (IS_CHIP_TYPE_L(sn100u)) {
805       icode_send_eof = 0;
806     }
807   }
808   // 22000100
809   else if (p_cmd_data[0] == 0x22 && p_cmd_data[1] == 0x00 &&
810            p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x00) {
811     // ee_disc_done = 0x01;//Reader Over SWP event getting
812     *rsp_len = 0x05;
813     p_rsp_data[0] = 0x42;
814     p_rsp_data[1] = 0x00;
815     p_rsp_data[2] = 0x02;
816     p_rsp_data[3] = 0x00;
817     p_rsp_data[4] = 0x00;
818     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
819     status = NFCSTATUS_FAILED;
820   }
821   // 2002 0904 3000 3100 3200 5000
822   else if (*cmd_len <= (NCI_MAX_DATA_LEN - 1) &&
823            (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
824            ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) /*||
825             (p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
826             )) {
827     *cmd_len += 0x01;
828     p_cmd_data[2] += 0x01;
829     p_cmd_data[9] = 0x01;
830     p_cmd_data[10] = 0x40;
831     p_cmd_data[11] = 0x50;
832     p_cmd_data[12] = 0x00;
833 
834     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
835     //        phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
836     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
837   }
838   //    20020703300031003200
839   //    2002 0301 3200
840   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
841            ((p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x03) ||
842             (p_cmd_data[2] == 0x03 && p_cmd_data[3] == 0x01 &&
843              p_cmd_data[4] == 0x32))) {
844     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
845     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
846     *rsp_len = 5;
847     p_rsp_data[0] = 0x40;
848     p_rsp_data[1] = 0x02;
849     p_rsp_data[2] = 0x02;
850     p_rsp_data[3] = 0x00;
851     p_rsp_data[4] = 0x00;
852 
853     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
854     status = NFCSTATUS_FAILED;
855     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
856   }
857 
858   // 2002 0D04 300104 310100 320100 500100
859   // 2002 0401 320100
860   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
861            (
862                /*(p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
863                (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
864                 p_cmd_data[4] == 0x32 && p_cmd_data[5] == 0x00))) {
865     //        p_cmd_data[12] = 0x40;
866 
867     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
868     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
869     p_cmd_data[6] = 0x60;
870 
871     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
872     //        status = NFCSTATUS_FAILED;
873     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
874   }
875 #if 0
876     else if ( (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 ) &&
877                  ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) ||
878                      (p_cmd_data[2] == 0x0B && p_cmd_data[3] == 0x05) ||
879                      (p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x02) ||
880                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x03) ||
881                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x04) ||
882                      (p_cmd_data[2] == 0x05 && p_cmd_data[3] == 0x02))
883              )
884     {
885         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
886         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
887         *rsp_len = 5;
888         p_rsp_data[0] = 0x40;
889         p_rsp_data[1] = 0x02;
890         p_rsp_data[2] = 0x02;
891         p_rsp_data[3] = 0x00;
892         p_rsp_data[4] = 0x00;
893 
894         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
895         status = NFCSTATUS_FAILED;
896         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
897     }
898 
899     else if((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
900            ((p_cmd_data[3] == 0x00) ||
901            ((*cmd_len >= 0x06) && (p_cmd_data[5] == 0x00)))) /*If the length of the first param id is zero don't allow*/
902     {
903         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
904         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
905         *rsp_len = 5;
906         p_rsp_data[0] = 0x40;
907         p_rsp_data[1] = 0x02;
908         p_rsp_data[2] = 0x02;
909         p_rsp_data[3] = 0x00;
910         p_rsp_data[4] = 0x00;
911 
912         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
913         status = NFCSTATUS_FAILED;
914         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
915     }
916 #endif
917   else if ((wFwVerRsp & 0x0000FFFF) == wFwVer) {
918     /* skip CORE_RESET and CORE_INIT from Brcm */
919     if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x00 &&
920         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x01) {
921       //            *rsp_len = 6;
922       //
923       //            NXPLOG_NCIHAL_D("> Going - core reset optimization");
924       //
925       //            p_rsp_data[0] = 0x40;
926       //            p_rsp_data[1] = 0x00;
927       //            p_rsp_data[2] = 0x03;
928       //            p_rsp_data[3] = 0x00;
929       //            p_rsp_data[4] = 0x10;
930       //            p_rsp_data[5] = 0x01;
931       //
932       //            status = NFCSTATUS_FAILED;
933       //            NXPLOG_NCIHAL_D("> Going - core reset optimization - END");
934     }
935     /* CORE_INIT */
936     else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x01 &&
937              p_cmd_data[2] == 0x00) {
938       //            NXPLOG_NCIHAL_D("> Going - core init optimization");
939       //            *rsp_len = iCoreInitRspLen;
940       //            memcpy(p_rsp_data, bCoreInitRsp, iCoreInitRspLen);
941       //            status = NFCSTATUS_FAILED;
942       //            NXPLOG_NCIHAL_D("> Going - core init optimization - END");
943     }
944   }
945   if (!phNxpTempMgr::GetInstance().IsICTempOk()) {
946     NXPLOG_NCIHAL_E("> IC Temp is NOK");
947     status = phNxpNciHal_process_screen_state_cmd(cmd_len, p_cmd_data, rsp_len,
948                                                   p_rsp_data);
949   }
950   /* CORE_SET_POWER_SUB_STATE */
951   if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x09 && p_cmd_data[2] == 0x01 &&
952       (p_cmd_data[3] == 0x00 || p_cmd_data[3] == 0x02)) {
953     // Sync power tracker data for screen on transition.
954     if (gPowerTrackerHandle.stateChange != NULL) {
955       gPowerTrackerHandle.stateChange(SCREEN_ON);
956     }
957   }
958 
959   return status;
960 }
961 
962 /******************************************************************************
963  * Function         phNxpNciHal_send_ext_cmd
964  *
965  * Description      This function send the extension command to NFCC. No
966  *                  response is checked by this function but it waits for
967  *                  the response to come.
968  *
969  * Returns          Returns NFCSTATUS_SUCCESS if sending cmd is successful and
970  *                  response is received.
971  *
972  ******************************************************************************/
phNxpNciHal_send_ext_cmd(uint16_t cmd_len,uint8_t * p_cmd)973 NFCSTATUS phNxpNciHal_send_ext_cmd(uint16_t cmd_len, uint8_t* p_cmd) {
974   NFCSTATUS status = NFCSTATUS_FAILED;
975   nxpncihal_ctrl.cmd_len = cmd_len;
976   memcpy(nxpncihal_ctrl.p_cmd_data, p_cmd, cmd_len);
977   status = phNxpNciHal_process_ext_cmd_rsp(nxpncihal_ctrl.cmd_len,
978                                            nxpncihal_ctrl.p_cmd_data);
979 
980   return status;
981 }
982 
983 /******************************************************************************
984  * Function         hal_extns_write_rsp_timeout_cb
985  *
986  * Description      Timer call back function
987  *
988  * Returns          None
989  *
990  ******************************************************************************/
hal_extns_write_rsp_timeout_cb(uint32_t timerId,void * pContext)991 static void hal_extns_write_rsp_timeout_cb(uint32_t timerId, void* pContext) {
992   UNUSED_PROP(timerId);
993   UNUSED_PROP(pContext);
994   NXPLOG_NCIHAL_D("hal_extns_write_rsp_timeout_cb - write timeout!!!");
995   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_FAILED;
996   usleep(1);
997   sem_post(&(nxpncihal_ctrl.syncSpiNfc));
998   SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
999 
1000   return;
1001 }
1002 
1003 /*******************************************************************************
1004  **
1005  ** Function:        request_EEPROM()
1006  **
1007  ** Description:     get and set EEPROM data
1008  **                  In case of request_modes GET_EEPROM_DATA or
1009  *SET_EEPROM_DATA,
1010  **                   1.caller has to pass the buffer and the length of data
1011  *required
1012  **                     to be read/written.
1013  **                   2.Type of information required to be read/written
1014  **                     (Example - EEPROM_RF_CFG)
1015  **
1016  ** Returns:         Returns NFCSTATUS_SUCCESS if sending cmd is successful and
1017  **                  status failed if not successful
1018  **
1019  *******************************************************************************/
request_EEPROM(phNxpNci_EEPROM_info_t * mEEPROM_info)1020 NFCSTATUS request_EEPROM(phNxpNci_EEPROM_info_t* mEEPROM_info) {
1021   NXPLOG_NCIHAL_D(
1022       "%s Enter  request_type : 0x%02x,  request_mode : 0x%02x,  bufflen : "
1023       "0x%02x",
1024       __func__, mEEPROM_info->request_type, mEEPROM_info->request_mode,
1025       mEEPROM_info->bufflen);
1026   NFCSTATUS status = NFCSTATUS_FAILED;
1027   uint8_t retry_cnt = 0;
1028   uint8_t getCfgStartIndex = 0x08;
1029   uint8_t setCfgStartIndex = 0x07;
1030   uint8_t memIndex = 0x00;
1031   uint8_t fieldLen = 0x01;  // Memory field len 1bytes
1032   char addr[2] = {0};
1033   uint8_t cur_value = 0, len = 5;
1034   uint8_t b_position = 0;
1035   bool_t update_req = false;
1036   uint16_t set_cfg_cmd_len = 0;
1037   uint8_t *set_cfg_eeprom, *base_addr;
1038 
1039   mEEPROM_info->update_mode = BITWISE;
1040 
1041   switch (mEEPROM_info->request_type) {
1042     case EEPROM_RF_CFG:
1043       memIndex = 0x00;
1044       fieldLen = 0x20;
1045       len = fieldLen + 4;  // 4 - numParam+2add+val
1046       addr[0] = 0xA0;
1047       addr[1] = 0x14;
1048       mEEPROM_info->update_mode = BYTEWISE;
1049       break;
1050 
1051     case EEPROM_FW_DWNLD:
1052       fieldLen = 0x20;
1053       memIndex = 0x0C;
1054       len = fieldLen + 4;
1055       addr[0] = 0xA0;
1056       addr[1] = 0x0F;
1057       mEEPROM_info->update_mode = BYTEWISE;
1058       break;
1059 
1060     case EEPROM_WIREDMODE_RESUME_TIMEOUT:
1061       mEEPROM_info->update_mode = BYTEWISE;
1062       memIndex = 0x00;
1063       fieldLen = 0x04;
1064       len = fieldLen + 4;
1065       addr[0] = 0xA0;
1066       addr[1] = 0xFC;
1067       break;
1068 
1069     case EEPROM_ESE_SVDD_POWER:
1070       b_position = 0;
1071       memIndex = 0x00;
1072       addr[0] = 0xA0;
1073       addr[1] = 0xF2;
1074       break;
1075     case EEPROM_ESE_POWER_EXT_PMU:
1076       mEEPROM_info->update_mode = BYTEWISE;
1077       memIndex = 0x00;
1078       addr[0] = 0xA0;
1079       addr[1] = 0xD7;
1080       break;
1081 
1082     case EEPROM_PROP_ROUTING:
1083       b_position = 7;
1084       memIndex = 0x00;
1085       addr[0] = 0xA0;
1086       addr[1] = 0x98;
1087       break;
1088 
1089     case EEPROM_ESE_SESSION_ID:
1090       b_position = 0;
1091       memIndex = 0x00;
1092       addr[0] = 0xA0;
1093       addr[1] = 0xEB;
1094       break;
1095 
1096     case EEPROM_SWP1_INTF:
1097       b_position = 0;
1098       memIndex = 0x00;
1099       addr[0] = 0xA0;
1100       addr[1] = 0xEC;
1101       break;
1102 
1103     case EEPROM_SWP1A_INTF:
1104       b_position = 0;
1105       memIndex = 0x00;
1106       addr[0] = 0xA0;
1107       addr[1] = 0xD4;
1108       break;
1109     case EEPROM_SWP2_INTF:
1110       b_position = 0;
1111       memIndex = 0x00;
1112       addr[0] = 0xA0;
1113       addr[1] = 0xED;
1114       break;
1115     case EEPROM_FLASH_UPDATE:
1116       /* This flag is no more used in MW */
1117       fieldLen = 0x20;
1118       memIndex = 0x00;
1119       len = fieldLen + 4;
1120       addr[0] = 0xA0;
1121       addr[1] = 0x0F;
1122       break;
1123     case EEPROM_AUTH_CMD_TIMEOUT:
1124       mEEPROM_info->update_mode = BYTEWISE;
1125       memIndex = 0x00;
1126       fieldLen = mEEPROM_info->bufflen;
1127       len = fieldLen + 4;
1128       addr[0] = 0xA0;
1129       addr[1] = 0xF7;
1130       break;
1131     case EEPROM_GUARD_TIMER:
1132       mEEPROM_info->update_mode = BYTEWISE;
1133       memIndex = 0x00;
1134       addr[0] = 0xA1;
1135       addr[1] = 0x0B;
1136       break;
1137     case EEPROM_AUTONOMOUS_MODE:
1138       mEEPROM_info->update_mode = BYTEWISE;
1139       memIndex = 0x00;
1140       addr[0] = 0xA0;
1141       addr[1] = 0x15;
1142       break;
1143     case EEPROM_T4T_NFCEE_ENABLE:
1144       mEEPROM_info->update_mode = BYTEWISE;
1145       b_position = 0;
1146       memIndex = 0x00;
1147       addr[0] = 0xA0;
1148       addr[1] = 0x95;
1149       break;
1150     case EEPROM_CE_PHONE_OFF_CFG:
1151       mEEPROM_info->update_mode = BYTEWISE;
1152       b_position = 0;
1153       memIndex = 0x00;
1154       addr[0] = 0xA0;
1155       addr[1] = 0x8E;
1156       break;
1157     case EEPROM_ENABLE_VEN_CFG:
1158       mEEPROM_info->update_mode = BYTEWISE;
1159       b_position = 0;
1160       memIndex = 0x00;
1161       addr[0] = 0xA0;
1162       addr[1] = 0x07;
1163       break;
1164     case EEPROM_ISODEP_MERGE_SAK:
1165       mEEPROM_info->update_mode = BYTEWISE;
1166       b_position = 0;
1167       memIndex = 0x00;
1168       addr[0] = 0xA1;
1169       addr[1] = 0x1B;
1170       break;
1171     case EEPROM_SRD_TIMEOUT:
1172       mEEPROM_info->update_mode = BYTEWISE;
1173       memIndex = 0x00;
1174       fieldLen = 0x02;
1175       len = fieldLen + 4;
1176       addr[0] = 0xA1;
1177       addr[1] = 0x17;
1178       break;
1179     case EEPROM_UICC1_SESSION_ID:
1180       fieldLen = mEEPROM_info->bufflen;
1181       len = fieldLen + 4;
1182       memIndex = 0x00;
1183       addr[0] = 0xA0;
1184       addr[1] = 0xE4;
1185       mEEPROM_info->update_mode = BYTEWISE;
1186       break;
1187     case EEPROM_UICC2_SESSION_ID:
1188       fieldLen = mEEPROM_info->bufflen;
1189       len = fieldLen + 4;
1190       memIndex = 0x00;
1191       addr[0] = 0xA0;
1192       addr[1] = 0xE5;
1193       mEEPROM_info->update_mode = BYTEWISE;
1194       break;
1195     case EEPROM_CE_ACT_NTF:
1196       mEEPROM_info->update_mode = BYTEWISE;
1197       b_position = 0;
1198       memIndex = 0x00;
1199       addr[0] = 0xA0;
1200       addr[1] = 0x96;
1201       break;
1202     case EEPROM_UICC_HCI_CE_STATE:
1203       fieldLen = mEEPROM_info->bufflen;
1204       len = fieldLen + 4;
1205       memIndex = 0x00;
1206       addr[0] = 0xA0;
1207       addr[1] = 0xE6;
1208       mEEPROM_info->update_mode = BYTEWISE;
1209       break;
1210     case EEPROM_EXT_FIELD_DETECT_MODE:
1211       mEEPROM_info->update_mode = BYTEWISE;
1212       b_position = 0;
1213       memIndex = 0x00;
1214       addr[0] = 0xA1;
1215       addr[1] = 0x36;
1216       break;
1217     case EEPROM_CONF_GPIO_CTRL:
1218       mEEPROM_info->update_mode = BYTEWISE;
1219       memIndex = 0x00;
1220       fieldLen = mEEPROM_info->bufflen;
1221       len = fieldLen + 4;
1222       addr[0] = 0xA1;
1223       addr[1] = 0x0F;
1224       break;
1225     case EEPROM_SET_GPIO_VALUE:
1226       mEEPROM_info->update_mode = BYTEWISE;
1227       memIndex = 0x00;
1228       fieldLen = mEEPROM_info->bufflen;
1229       len = fieldLen + 4;
1230       addr[0] = 0xA1;
1231       addr[1] = 0x65;
1232       break;
1233     case EEPROM_POWER_TRACKER_ENABLE:
1234       mEEPROM_info->update_mode = BYTEWISE;
1235       memIndex = 0x00;
1236       fieldLen = mEEPROM_info->bufflen;
1237       len = fieldLen + 4;
1238       addr[0] = 0xA0;
1239       addr[1] = 0x6D;
1240       break;
1241     case EEPROM_VDDPA:
1242       mEEPROM_info->update_mode = BYTEWISE;
1243       memIndex = 0x14;
1244       fieldLen = 0x30;
1245       len = fieldLen + 4;
1246       addr[0] = 0xA0;
1247       addr[1] = 0x0E;
1248       break;
1249     default:
1250       ALOGE("No valid request information found");
1251       break;
1252   }
1253 
1254   uint8_t get_cfg_eeprom[6] = {
1255       0x20,              // get_cfg header
1256       0x03,              // get_cfg header
1257       0x03,              // len of following value
1258       0x01,              // Num Parameters
1259       (uint8_t)addr[0],  // First byte of Address
1260       (uint8_t)addr[1]   // Second byte of Address
1261   };
1262   uint8_t set_cfg_cmd_hdr[7] = {
1263       0x20,              // set_cfg header
1264       0x02,              // set_cfg header
1265       len,               // len of following value
1266       0x01,              // Num Param
1267       (uint8_t)addr[0],  // First byte of Address
1268       (uint8_t)addr[1],  // Second byte of Address
1269       fieldLen           // Data len
1270   };
1271 
1272   set_cfg_cmd_len = sizeof(set_cfg_cmd_hdr) + fieldLen;
1273   set_cfg_eeprom = (uint8_t*)malloc(set_cfg_cmd_len);
1274   if (set_cfg_eeprom == NULL) {
1275     ALOGE("memory allocation failed");
1276     return status;
1277   }
1278   base_addr = set_cfg_eeprom;
1279   memset(set_cfg_eeprom, 0, set_cfg_cmd_len);
1280   memcpy(set_cfg_eeprom, set_cfg_cmd_hdr, sizeof(set_cfg_cmd_hdr));
1281 
1282 retryget:
1283   status = phNxpNciHal_send_ext_cmd(sizeof(get_cfg_eeprom), get_cfg_eeprom);
1284   if (status == NFCSTATUS_SUCCESS) {
1285     status = nxpncihal_ctrl.p_rx_data[3];
1286     if (status != NFCSTATUS_SUCCESS) {
1287       ALOGE("failed to get requested memory address");
1288     } else if (mEEPROM_info->request_mode == GET_EEPROM_DATA) {
1289       if (mEEPROM_info->bufflen == 0xFF) {
1290         /* Max bufferlenth for single Get Config Command is 0xFF.
1291          * If buffer length set to max value, reassign buffer value
1292          * depends on response from Get Config command */
1293         mEEPROM_info->bufflen =
1294             *(nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex - 1);
1295       }
1296       memcpy(mEEPROM_info->buffer,
1297              nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex,
1298              mEEPROM_info->bufflen);
1299     } else if (mEEPROM_info->request_mode == SET_EEPROM_DATA) {
1300       // Clear the buffer first
1301       memset(set_cfg_eeprom + setCfgStartIndex, 0x00,
1302              (set_cfg_cmd_len - setCfgStartIndex));
1303 
1304       // copy get config data into set_cfg_eeprom
1305       memcpy(set_cfg_eeprom + setCfgStartIndex,
1306              nxpncihal_ctrl.p_rx_data + getCfgStartIndex, fieldLen);
1307       if (mEEPROM_info->update_mode == BITWISE) {
1308         cur_value =
1309             (set_cfg_eeprom[setCfgStartIndex + memIndex] >> b_position) & 0x01;
1310         if (cur_value != mEEPROM_info->buffer[0]) {
1311           update_req = true;
1312           if (mEEPROM_info->buffer[0] == 1) {
1313             set_cfg_eeprom[setCfgStartIndex + memIndex] |= (1 << b_position);
1314           } else if (mEEPROM_info->buffer[0] == 0) {
1315             set_cfg_eeprom[setCfgStartIndex + memIndex] &= (~(1 << b_position));
1316           }
1317         }
1318       } else if (mEEPROM_info->update_mode == BYTEWISE) {
1319         if (memcmp(set_cfg_eeprom + setCfgStartIndex + memIndex,
1320                    mEEPROM_info->buffer, mEEPROM_info->bufflen)) {
1321           update_req = true;
1322           memcpy(set_cfg_eeprom + setCfgStartIndex + memIndex,
1323                  mEEPROM_info->buffer, mEEPROM_info->bufflen);
1324         }
1325       } else {
1326         ALOGE("%s, invalid update mode", __func__);
1327       }
1328 
1329       if (update_req) {
1330       // do set config
1331       retryset:
1332         status = phNxpNciHal_send_ext_cmd(set_cfg_cmd_len, set_cfg_eeprom);
1333         if (status != NFCSTATUS_SUCCESS && retry_cnt < 3) {
1334           retry_cnt++;
1335           ALOGE("Set Cfg Retry cnt=%x", retry_cnt);
1336           goto retryset;
1337         }
1338       } else {
1339         ALOGD("%s: values are same no update required", __func__);
1340       }
1341     }
1342   } else if (retry_cnt < 3) {
1343     retry_cnt++;
1344     ALOGE("Get Cfg Retry cnt=%x", retry_cnt);
1345     goto retryget;
1346   }
1347 
1348   if (base_addr != NULL) {
1349     free(base_addr);
1350     base_addr = NULL;
1351   }
1352   retry_cnt = 0;
1353   return status;
1354 }
1355 
1356 /*******************************************************************************
1357  **
1358  ** Function:        phNxpNciHal_enableDefaultUICC2SWPline()
1359  **
1360  ** Description:     Select UICC2 or UICC3
1361  **
1362  ** Returns:         status
1363  **
1364  ********************************************************************************/
phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel)1365 NFCSTATUS phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel) {
1366   NFCSTATUS status = NFCSTATUS_FAILED;
1367   uint8_t p_data[255] = {NCI_MT_CMD, NXP_CORE_SET_CONFIG_CMD};
1368   uint8_t LEN_INDEX = 2, PARAM_INDEX = 3;
1369   uint8_t* p = p_data;
1370   NXPLOG_NCIHAL_D("phNxpNciHal_enableDefaultUICC2SWPline %d", uicc2_sel);
1371   p_data[LEN_INDEX] = 1;
1372   p += 4;
1373   if (uicc2_sel == 0x03) {
1374     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1375     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWP2);
1376     UINT8_TO_STREAM(p, 0x01);
1377     UINT8_TO_STREAM(p, 0x01);
1378     p_data[LEN_INDEX] += 4;
1379     p_data[PARAM_INDEX] += 1;
1380   }
1381   if (uicc2_sel == 0x04) {
1382     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1383     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWPUICC3);
1384     UINT8_TO_STREAM(p, 0x01);
1385     UINT8_TO_STREAM(p, 0x01);
1386     p_data[LEN_INDEX] += 4;
1387     p_data[PARAM_INDEX] += 1;
1388   }
1389   if (p_data[PARAM_INDEX] > 0x00)
1390     status = phNxpNciHal_send_ext_cmd(p - p_data, p_data);
1391   return status;
1392 }
1393 
1394 /******************************************************************************
1395  * Function         phNxpNciHal_prop_conf_lpcd
1396  *
1397  * Description      If NFCC is not in Nfc Forum mode, then this function will
1398  *                  configure it back to the Nfc Forum mode.
1399  *
1400  * Returns          none
1401  *
1402  ******************************************************************************/
phNxpNciHal_prop_conf_lpcd(bool enableLPCD)1403 void phNxpNciHal_prop_conf_lpcd(bool enableLPCD) {
1404   uint8_t cmd_get_lpcdval[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x68};
1405   vector<uint8_t> cmd_set_lpcdval{0x20, 0x02, 0x2E};
1406 
1407   if (NFCSTATUS_SUCCESS ==
1408       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_lpcdval), cmd_get_lpcdval)) {
1409     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1410       if (!(nxpncihal_ctrl.p_rx_data[17] & (1 << 7)) && enableLPCD) {
1411         nxpncihal_ctrl.p_rx_data[17] |= (1 << 7);
1412         cmd_set_lpcdval.insert(
1413             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1414             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1415         if (NFCSTATUS_SUCCESS ==
1416             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1417                                      &cmd_set_lpcdval[0])) {
1418           return;
1419         }
1420       } else if (!enableLPCD && (nxpncihal_ctrl.p_rx_data[17] & (1 << 7))) {
1421         nxpncihal_ctrl.p_rx_data[17] &= ~(1 << 7);
1422         cmd_set_lpcdval.insert(
1423             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1424             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1425         if (NFCSTATUS_SUCCESS ==
1426             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1427                                      &cmd_set_lpcdval[0])) {
1428           return;
1429         }
1430       } else {
1431         return;
1432       }
1433     }
1434   }
1435   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1436   return;
1437 }
1438 
1439 /******************************************************************************
1440  * Function         phNxpNciHal_prop_conf_rssi
1441  *
1442  * Description      It resets RSSI param to default value.
1443  *
1444  * Returns          none
1445  *
1446  ******************************************************************************/
phNxpNciHal_prop_conf_rssi()1447 void phNxpNciHal_prop_conf_rssi() {
1448   if (IS_CHIP_TYPE_L(sn220u)) {
1449     NXPLOG_NCIHAL_D("%s: feature is not supported", __func__);
1450     return;
1451   }
1452   vector<uint8_t> cmd_get_rssival = {0x20, 0x03, 0x03, 0x01, 0xA1, 0x55};
1453   vector<uint8_t> cmd_set_rssival = {0x20, 0x02, 0x06, 0x01, 0xA1,
1454                                      0x55, 0x02, 0x00, 0x00};
1455 
1456   if (NFCSTATUS_SUCCESS !=
1457       phNxpNciHal_send_ext_cmd(cmd_get_rssival.size(), &cmd_get_rssival[0])) {
1458     NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1459     return;
1460   }
1461   if ((nxpncihal_ctrl.rx_data_len <= 9) ||
1462       (NFCSTATUS_SUCCESS != nxpncihal_ctrl.p_rx_data[3])) {
1463     NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1464     return;
1465   }
1466   if ((nxpncihal_ctrl.p_rx_data[8] != 0x00) ||
1467       (nxpncihal_ctrl.p_rx_data[9] != 0x00)) {
1468     if (NFCSTATUS_SUCCESS !=
1469         phNxpNciHal_send_ext_cmd(cmd_set_rssival.size(), &cmd_set_rssival[0])) {
1470       NXPLOG_NCIHAL_E("%s: failed!! Line:%d", __func__, __LINE__);
1471       return;
1472     }
1473   }
1474 
1475   return;
1476 }
1477 
1478 /******************************************************************************
1479  * Function         phNxpNciHal_conf_nfc_forum_mode
1480  *
1481  * Description      If NFCC is not in Nfc Forum mode, then this function will
1482  *                  configure it back to the Nfc Forum mode.
1483  *
1484  * Returns          none
1485  *
1486  ******************************************************************************/
phNxpNciHal_conf_nfc_forum_mode()1487 void phNxpNciHal_conf_nfc_forum_mode() {
1488   uint8_t cmd_get_emvcocfg[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x44};
1489   uint8_t cmd_reset_emvcocfg[8];
1490   long cmdlen = 8;
1491   long retlen = 0;
1492 
1493   if (GetNxpByteArrayValue(NAME_NXP_PROP_RESET_EMVCO_CMD,
1494                            (char*)cmd_reset_emvcocfg, cmdlen, &retlen)) {
1495   }
1496   if (retlen != 0x08) {
1497     NXPLOG_NCIHAL_E("%s: command is not provided", __func__);
1498     return;
1499   }
1500   /* Update the flag address from the Nxp config file */
1501   cmd_get_emvcocfg[4] = cmd_reset_emvcocfg[4];
1502   cmd_get_emvcocfg[5] = cmd_reset_emvcocfg[5];
1503 
1504   if (NFCSTATUS_SUCCESS ==
1505       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_emvcocfg), cmd_get_emvcocfg)) {
1506     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1507       if (0x01 & nxpncihal_ctrl.p_rx_data[8]) {
1508         if (NFCSTATUS_SUCCESS ==
1509             phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_emvcocfg),
1510                                      cmd_reset_emvcocfg)) {
1511           return;
1512         }
1513       } else {
1514         return;
1515       }
1516     }
1517   }
1518   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1519   return;
1520 }
1521 
1522 /******************************************************************************
1523  * Function         RemoveNfcDepIntfFromInitResp
1524  *
1525  * Description      This function process the NCI_CORE_INIT_RESP & removes
1526  *                  remove the NFC_DEP interface support and modify the
1527  *                  CORE_INIT_RESP accordingly.
1528  *
1529  * Returns          None
1530  *
1531  ******************************************************************************/
RemoveNfcDepIntfFromInitResp(uint8_t * coreInitResp,uint16_t * coreInitRespLen)1532 void RemoveNfcDepIntfFromInitResp(uint8_t* coreInitResp,
1533                                   uint16_t* coreInitRespLen) {
1534   /* As per NCI 2.0 index Number of Supported RF interfaces is 13 */
1535   uint8_t indexOfSupportedRfIntf = 13;
1536   /* as per NCI 2.0 Number of Supported RF Interfaces Payload field index is 13
1537    * & 3 bytes for NCI_MSG_HEADER */
1538   uint8_t noOfSupportedInterface =
1539       *(coreInitResp + indexOfSupportedRfIntf + NCI_HEADER_SIZE);
1540   uint8_t rfInterfacesLength =
1541       *coreInitRespLen - (indexOfSupportedRfIntf + 1 + NCI_HEADER_SIZE);
1542   uint8_t* supportedRfInterfaces = NULL;
1543   bool removeNfcDepRequired = false;
1544   if (noOfSupportedInterface) {
1545     supportedRfInterfaces =
1546         coreInitResp + indexOfSupportedRfIntf + 1 + NCI_HEADER_SIZE;
1547   }
1548   uint8_t* supportedRfInterfacesDetails = supportedRfInterfaces;
1549   /* Get the index of Supported RF Interface for NFC-DEP interface in CORE_INIT
1550    * Response*/
1551   for (int i = 0; i < noOfSupportedInterface; i++) {
1552     if (*supportedRfInterfaces == NCI_NFC_DEP_RF_INTF) {
1553       removeNfcDepRequired = true;
1554       break;
1555     }
1556     uint8_t noOfExtensions = *(supportedRfInterfaces + 1);
1557     /* 2 bytes for RF interface type & length of Extensions */
1558     supportedRfInterfaces += (2 + noOfExtensions);
1559   }
1560   /* If NFC-DEP is found in response then remove NFC-DEP from init response and
1561    * frame new CORE_INIT_RESP and send to upper layer*/
1562   if (!removeNfcDepRequired) {
1563     NXPLOG_NCIHAL_E("%s: NFC-DEP Removal is not requored !!", __func__);
1564     return;
1565   } else {
1566     coreInitResp[16] = noOfSupportedInterface - 1;
1567     uint8_t noBytesToSkipForNfcDep = 2 + *(supportedRfInterfaces + 1);
1568     memcpy(supportedRfInterfaces,
1569            supportedRfInterfaces + noBytesToSkipForNfcDep,
1570            (rfInterfacesLength -
1571             ((supportedRfInterfaces - supportedRfInterfacesDetails) +
1572              noBytesToSkipForNfcDep)));
1573     *coreInitRespLen -= noBytesToSkipForNfcDep;
1574     coreInitResp[2] -= noBytesToSkipForNfcDep;
1575 
1576     /* Print updated CORE_INIT_RESP for debug purpose*/
1577     phNxpNciHal_print_packet("DEBUG", coreInitResp, *coreInitRespLen);
1578   }
1579 }
1580 
1581 /******************************************************************************
1582  * Function         phNxpNciHal_process_screen_state_cmd
1583  *
1584  * Description      Forms a dummy response for cmds sent during screen
1585  *                  state change,when IC temp is not normal.
1586  *                  These Cmds are not forwarded to NFCC.
1587  *
1588  * Returns          Returns NFCSTATUS_FAILED for screen state change cmd
1589  *                  or returns NFCSTATUS_SUCCESS.
1590  *
1591  ******************************************************************************/
phNxpNciHal_process_screen_state_cmd(uint16_t * cmd_len,uint8_t * p_cmd_data,uint16_t * rsp_len,uint8_t * p_rsp_data)1592 static NFCSTATUS phNxpNciHal_process_screen_state_cmd(uint16_t* cmd_len,
1593                                                       uint8_t* p_cmd_data,
1594                                                       uint16_t* rsp_len,
1595                                                       uint8_t* p_rsp_data) {
1596   NFCSTATUS status = NFCSTATUS_SUCCESS;
1597   if (*cmd_len == 7 && p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
1598       p_cmd_data[2] == 0x04 && p_cmd_data[4] == 0x02) {
1599     // CON_DISCOVERY_PARAM
1600     phNxpNciHal_print_packet("SEND", p_cmd_data, 7);
1601     *rsp_len = 5;
1602     p_rsp_data[0] = 0x40;
1603     p_rsp_data[1] = 0x02;
1604     p_rsp_data[2] = 0x02;
1605     p_rsp_data[3] = 0x00;
1606     p_rsp_data[4] = 0x00;
1607     NXPLOG_NCIHAL_E(
1608         "> Sending fake response for CON_DISCOVERY_PARAM set config");
1609     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
1610     status = NFCSTATUS_FAILED;
1611   } else if (*cmd_len == 4 && p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x09) {
1612     phNxpNciHal_print_packet("SEND", p_cmd_data, 4);
1613     *rsp_len = 4;
1614     p_rsp_data[0] = 0x40;
1615     p_rsp_data[1] = 0x09;
1616     p_rsp_data[2] = 0x01;
1617     p_rsp_data[3] = 0x00;
1618     NXPLOG_NCIHAL_E("> Sending fake response for POWER_SUB_STATE cmd");
1619     phNxpNciHal_print_packet("RECV", p_rsp_data, 4);
1620     status = NFCSTATUS_FAILED;
1621   }
1622   return status;
1623 }
1624 
1625 /******************************************************************************
1626  * Function         phNxpNciHal_update_core_reset_ntf_prop
1627  *
1628  * Description      This function updates the vendor property which keep track
1629  *                  core reset ntf count for fw recovery.
1630  *
1631  * Returns          void
1632  *
1633  *****************************************************************************/
1634 
phNxpNciHal_update_core_reset_ntf_prop()1635 static bool phNxpNciHal_update_core_reset_ntf_prop() {
1636   NXPLOG_NCIHAL_D("%s: Entry", __func__);
1637   bool is_abort_req = true;
1638   int32_t core_reset_count =
1639       phNxpNciHal_getVendorProp_int32(core_reset_ntf_count_prop_name, 0);
1640   if (core_reset_count == CORE_RESET_NTF_RECOVERY_REQ_COUNT) {
1641     NXPLOG_NCIHAL_D("%s: Notify main thread of fresh ntf received", __func__);
1642     sem_post(&sem_reset_ntf_received);
1643     is_abort_req = false;
1644   }
1645   ++core_reset_count;
1646   std::string ntf_count_str = std::to_string(core_reset_count);
1647   NXPLOG_NCIHAL_D("Core reset counter prop value  %d", core_reset_count);
1648   if (NFCSTATUS_SUCCESS !=
1649       phNxpNciHal_setVendorProp(core_reset_ntf_count_prop_name,
1650                                 ntf_count_str.c_str())) {
1651     NXPLOG_NCIHAL_D("setting core_reset_ntf_count_prop failed");
1652   }
1653   NXPLOG_NCIHAL_D("%s: Exit", __func__);
1654   return is_abort_req;
1655 }
1656