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