• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2018 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #define LOG_TAG "NxpEseHal"
19 #include <log/log.h>
20 
21 #include <cutils/properties.h>
22 #include <ese_config.h>
23 #include <phNxpEseFeatures.h>
24 #include <phNxpEsePal.h>
25 #include <phNxpEsePal_spi.h>
26 #include <phNxpEseProto7816_3.h>
27 #include <phNxpEse_Internal.h>
28 
29 #define RECIEVE_PACKET_SOF 0xA5
30 #define PH_PAL_ESE_PRINT_PACKET_TX(data, len) \
31   ({ phPalEse_print_packet("SEND", data, len); })
32 #define PH_PAL_ESE_PRINT_PACKET_RX(data, len) \
33   ({ phPalEse_print_packet("RECV", data, len); })
34 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
35                                int nNbBytesToRead);
36 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
37 static ESESTATUS phNxpEse_checkJcopDwnldState(void);
38 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state);
39 #endif
40 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
41 static ESESTATUS phNxpEse_checkFWDwnldStatus(void);
42 #endif
43 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer);
44 #ifdef NXP_SECURE_TIMER_SESSION
45 static unsigned char* phNxpEse_GgetTimerTlvBuffer(unsigned char* timer_buffer,
46                                                   unsigned int value);
47 #endif
48 /*********************** Global Variables *************************************/
49 
50 /* ESE Context structure */
51 phNxpEse_Context_t nxpese_ctxt;
52 bool ese_debug_enabled = true;
53 
54 /******************************************************************************
55  * Function         phNxpLog_InitializeLogLevel
56  *
57  * Description      This function is called during phNxpEse_init to initialize
58  *                  debug log level.
59  *
60  * Returns          None
61  *
62  ******************************************************************************/
63 
phNxpLog_InitializeLogLevel()64 void phNxpLog_InitializeLogLevel() {
65   ese_debug_enabled =
66       (EseConfig::getUnsigned(NAME_SE_DEBUG_ENABLED, 0) != 0) ? true : false;
67 
68   char valueStr[PROPERTY_VALUE_MAX] = {0};
69   int len = property_get("vendor.ese.debug_enabled", valueStr, "");
70   if (len > 0) {
71     // let Android property override .conf variable
72     unsigned debug_enabled = 0;
73     sscanf(valueStr, "%u", &debug_enabled);
74     ese_debug_enabled = (debug_enabled == 0) ? false : true;
75   }
76 
77   ALOGD_IF(ese_debug_enabled, "%s: level=%u", __func__, ese_debug_enabled);
78 }
79 
80 /******************************************************************************
81  * Function         phNxpEse_init
82  *
83  * Description      This function is called by Jni/phNxpEse_open during the
84  *                  initialization of the ESE. It initializes protocol stack
85  *instance variable
86  *
87  * Returns          This function return ESESTATUS_SUCCES (0) in case of success
88  *                  In case of failure returns other failure value.
89  *
90  ******************************************************************************/
phNxpEse_init(phNxpEse_initParams initParams)91 ESESTATUS phNxpEse_init(phNxpEse_initParams initParams) {
92   ESESTATUS wConfigStatus = ESESTATUS_FAILED;
93   unsigned long int num;
94   unsigned long maxTimer = 0;
95   phNxpEseProto7816InitParam_t protoInitParam;
96   phNxpEse_memset(&protoInitParam, 0x00, sizeof(phNxpEseProto7816InitParam_t));
97   /* STATUS_OPEN */
98   nxpese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
99 
100   if (EseConfig::hasKey(NAME_NXP_WTX_COUNT_VALUE)) {
101     num = EseConfig::getUnsigned(NAME_NXP_WTX_COUNT_VALUE);
102     protoInitParam.wtx_counter_limit = num;
103     ALOGD_IF(ese_debug_enabled, "Wtx_counter read from config file - %lu",
104              protoInitParam.wtx_counter_limit);
105   } else {
106     protoInitParam.wtx_counter_limit = PH_PROTO_WTX_DEFAULT_COUNT;
107   }
108   if (EseConfig::hasKey(NAME_NXP_MAX_RNACK_RETRY)) {
109     protoInitParam.rnack_retry_limit =
110         EseConfig::getUnsigned(NAME_NXP_MAX_RNACK_RETRY);
111   } else {
112     protoInitParam.rnack_retry_limit = MAX_RNACK_RETRY_LIMIT;
113   }
114   if (ESE_MODE_NORMAL ==
115       initParams.initMode) /* TZ/Normal wired mode should come here*/
116   {
117     if (EseConfig::hasKey(NAME_NXP_SPI_INTF_RST_ENABLE)) {
118       protoInitParam.interfaceReset =
119           (EseConfig::getUnsigned(NAME_NXP_SPI_INTF_RST_ENABLE) == 1) ? true
120                                                                       : false;
121     } else {
122       protoInitParam.interfaceReset = true;
123     }
124   } else /* OSU mode, no interface reset is required */
125   {
126     protoInitParam.interfaceReset = false;
127   }
128   /* Sharing lib context for fetching secure timer values */
129   protoInitParam.pSecureTimerParams =
130       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams;
131 
132   ALOGD_IF(ese_debug_enabled,
133            "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
134            __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
135            nxpese_ctxt.secureTimerParams.secureTimer2,
136            nxpese_ctxt.secureTimerParams.secureTimer3);
137 
138   phNxpEse_GetMaxTimer(&maxTimer);
139 
140   /* T=1 Protocol layer open */
141   wConfigStatus = phNxpEseProto7816_Open(protoInitParam);
142   if (ESESTATUS_FAILED == wConfigStatus) {
143     wConfigStatus = ESESTATUS_FAILED;
144     ALOGE("phNxpEseProto7816_Open failed");
145   }
146   return wConfigStatus;
147 }
148 
149 /******************************************************************************
150  * Function         phNxpEse_open
151  *
152  * Description      This function is called by Jni during the
153  *                  initialization of the ESE. It opens the physical connection
154  *                  with ESE and creates required client thread for
155  *                  operation.
156  * Returns          This function return ESESTATUS_SUCCES (0) in case of success
157  *                  In case of failure returns other failure value.
158  *
159  ******************************************************************************/
phNxpEse_open(phNxpEse_initParams initParams)160 ESESTATUS phNxpEse_open(phNxpEse_initParams initParams) {
161   phPalEse_Config_t tPalConfig;
162   ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
163   unsigned long int tpm_enable = 0;
164   char ese_dev_node[64];
165   std::string ese_node;
166 #ifdef SPM_INTEGRATED
167   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
168   spm_state_t current_spm_state = SPM_STATE_INVALID;
169 #endif
170 
171   ALOGE("phNxpEse_open Enter");
172   /*When spi channel is already opened return status as FAILED*/
173   if (nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
174     ALOGD_IF(ese_debug_enabled, "already opened\n");
175     return ESESTATUS_BUSY;
176   }
177 
178   phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
179   phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
180 
181   ALOGE("MW SEAccessKit Version");
182   ALOGE("Android Version:0x%x", NXP_ANDROID_VER);
183   ALOGE("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
184   ALOGE("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
185 
186   if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
187     tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
188     ALOGE(
189         "SPI Throughput measurement enable/disable read from config file - %lu",
190         tpm_enable);
191   } else {
192     ALOGE("SPI Throughput not defined in config file - %lu", tpm_enable);
193   }
194 #ifdef NXP_POWER_SCHEME_SUPPORT
195   unsigned long int num = 0;
196   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
197     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
198     nxpese_ctxt.pwr_scheme = num;
199     ALOGE("Power scheme read from config file - %lu", num);
200   } else {
201     nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
202     ALOGE("Power scheme not defined in config file - %lu", num);
203   }
204 #else
205   nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
206   tpm_enable = 0x00;
207 #endif
208   /* initialize trace level */
209   phNxpLog_InitializeLogLevel();
210 
211   /*Read device node path*/
212   ese_node = EseConfig::getString(NAME_NXP_ESE_DEV_NODE, "/dev/pn81a");
213   strcpy(ese_dev_node, ese_node.c_str());
214   tPalConfig.pDevName = (int8_t*)ese_dev_node;
215 
216   /* Initialize PAL layer */
217   wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
218   if (wConfigStatus != ESESTATUS_SUCCESS) {
219     ALOGE("phPalEse_Init Failed");
220     goto clean_and_return;
221   }
222   /* Copying device handle to ESE Lib context*/
223   nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
224 
225 #ifdef SPM_INTEGRATED
226   /* Get the Access of ESE*/
227   wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
228   if (wSpmStatus != ESESTATUS_SUCCESS) {
229     ALOGE("phNxpEse_SPM_Init Failed");
230     wConfigStatus = ESESTATUS_FAILED;
231     goto clean_and_return_2;
232   }
233   wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
234   if (wSpmStatus != ESESTATUS_SUCCESS) {
235     ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
236     wConfigStatus = ESESTATUS_FAILED;
237     goto clean_and_return_1;
238   }
239 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
240   wConfigStatus = phNxpEse_checkFWDwnldStatus();
241   if (wConfigStatus != ESESTATUS_SUCCESS) {
242     ALOGD_IF(ese_debug_enabled,
243              "Failed to open SPI due to VEN pin used by FW download \n");
244     wConfigStatus = ESESTATUS_FAILED;
245     goto clean_and_return_1;
246   }
247 #endif
248   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
249   if (wSpmStatus != ESESTATUS_SUCCESS) {
250     ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
251     wConfigStatus = ESESTATUS_FAILED;
252     goto clean_and_return_1;
253   } else {
254     if ((current_spm_state & SPM_STATE_SPI) |
255         (current_spm_state & SPM_STATE_SPI_PRIO)) {
256       ALOGE(" %s : SPI is already opened...second instance not allowed",
257             __FUNCTION__);
258       wConfigStatus = ESESTATUS_FAILED;
259       goto clean_and_return_1;
260     }
261   }
262 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
263   if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
264     ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
265     wConfigStatus = ESESTATUS_FAILED;
266     goto clean_and_return_1;
267   }
268 #endif
269   phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams,
270                   sizeof(phNxpEse_initParams));
271 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
272   /* Updating ESE power state based on the init mode */
273   if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
274     ALOGE("%s Init mode ---->OSU", __FUNCTION__);
275     wConfigStatus = phNxpEse_checkJcopDwnldState();
276     if (wConfigStatus != ESESTATUS_SUCCESS) {
277       ALOGE("phNxpEse_checkJcopDwnldState failed");
278       goto clean_and_return_1;
279     }
280   }
281 #endif
282   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_ENABLE);
283   if (wSpmStatus != ESESTATUS_SUCCESS) {
284     ALOGE("phNxpEse_SPM_ConfigPwr: enabling power Failed");
285     if (wSpmStatus == ESESTATUS_BUSY) {
286       wConfigStatus = ESESTATUS_BUSY;
287     } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
288       wConfigStatus = ESESTATUS_DWNLD_BUSY;
289     } else {
290       wConfigStatus = ESESTATUS_FAILED;
291     }
292     goto clean_and_return;
293   } else {
294     ALOGD_IF(ese_debug_enabled, "nxpese_ctxt.spm_power_state true");
295     nxpese_ctxt.spm_power_state = true;
296   }
297 #endif
298 
299   ALOGD_IF(ese_debug_enabled, "wConfigStatus %x", wConfigStatus);
300   return wConfigStatus;
301 
302 clean_and_return:
303 #ifdef SPM_INTEGRATED
304   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
305   if (wSpmStatus != ESESTATUS_SUCCESS) {
306     ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
307   }
308 clean_and_return_1:
309   phNxpEse_SPM_DeInit();
310 clean_and_return_2:
311 #endif
312   if (NULL != nxpese_ctxt.pDevHandle) {
313     phPalEse_close(nxpese_ctxt.pDevHandle);
314     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
315   }
316   nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
317   nxpese_ctxt.spm_power_state = false;
318   return ESESTATUS_FAILED;
319 }
320 
321 /******************************************************************************
322  * \ingroup spi_libese
323  *
324  * \brief  Check if libese has opened
325  *
326  * \retval return false if it is close, otherwise true.
327  *
328  ******************************************************************************/
phNxpEse_isOpen()329 bool phNxpEse_isOpen() { return nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE; }
330 
331 /******************************************************************************
332  * Function         phNxpEse_openPrioSession
333  *
334  * Description      This function is called by Jni during the
335  *                  initialization of the ESE. It opens the physical connection
336  *                  with ESE () and creates required client thread for
337  *                  operation.  This will get priority access to ESE for timeout
338  duration.
339 
340  * Returns          This function return ESESTATUS_SUCCES (0) in case of success
341  *                  In case of failure returns other failure value.
342  *
343  ******************************************************************************/
phNxpEse_openPrioSession(phNxpEse_initParams initParams)344 ESESTATUS phNxpEse_openPrioSession(phNxpEse_initParams initParams) {
345   phPalEse_Config_t tPalConfig;
346   ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
347   unsigned long int num = 0, tpm_enable = 0;
348 
349   ALOGE("phNxpEse_openPrioSession Enter");
350 #ifdef SPM_INTEGRATED
351   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
352   spm_state_t current_spm_state = SPM_STATE_INVALID;
353 #endif
354   phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
355   phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
356 
357   ALOGE("MW SEAccessKit Version");
358   ALOGE("Android Version:0x%x", NXP_ANDROID_VER);
359   ALOGE("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
360   ALOGE("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
361 
362 #ifdef NXP_POWER_SCHEME_SUPPORT
363   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
364     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
365     nxpese_ctxt.pwr_scheme = num;
366     ALOGE("Power scheme read from config file - %lu", num);
367   } else
368 #endif
369   {
370     nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
371     ALOGE("Power scheme not defined in config file - %lu", num);
372   }
373   if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
374     num = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
375     ALOGE(
376         "SPI Throughput measurement enable/disable read from config file - %lu",
377         num);
378   } else {
379     ALOGE("SPI Throughput not defined in config file - %lu", num);
380   }
381   /* initialize trace level */
382   phNxpLog_InitializeLogLevel();
383 
384   tPalConfig.pDevName = (int8_t*)"/dev/p73";
385 
386   /* Initialize PAL layer */
387   wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
388   if (wConfigStatus != ESESTATUS_SUCCESS) {
389     ALOGE("phPalEse_Init Failed");
390     goto clean_and_return;
391   }
392   /* Copying device handle to hal context*/
393   nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
394 
395 #ifdef SPM_INTEGRATED
396   /* Get the Access of ESE*/
397   wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
398   if (wSpmStatus != ESESTATUS_SUCCESS) {
399     ALOGE("phNxpEse_SPM_Init Failed");
400     wConfigStatus = ESESTATUS_FAILED;
401     goto clean_and_return_2;
402   }
403   wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
404   if (wSpmStatus != ESESTATUS_SUCCESS) {
405     ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
406     wConfigStatus = ESESTATUS_FAILED;
407     goto clean_and_return_1;
408   }
409   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
410   if (wSpmStatus != ESESTATUS_SUCCESS) {
411     ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
412     wConfigStatus = ESESTATUS_FAILED;
413     goto clean_and_return_1;
414   } else {
415     if ((current_spm_state & SPM_STATE_SPI) |
416         (current_spm_state & SPM_STATE_SPI_PRIO)) {
417       ALOGE(" %s : SPI is already opened...second instance not allowed",
418             __FUNCTION__);
419       wConfigStatus = ESESTATUS_FAILED;
420       goto clean_and_return_1;
421     }
422 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
423     if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
424       ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
425       wConfigStatus = ESESTATUS_FAILED;
426       goto clean_and_return_1;
427     }
428 #endif
429 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
430     wConfigStatus = phNxpEse_checkFWDwnldStatus();
431     if (wConfigStatus != ESESTATUS_SUCCESS) {
432       ALOGD_IF(ese_debug_enabled,
433                "Failed to open SPI due to VEN pin used by FW download \n");
434       wConfigStatus = ESESTATUS_FAILED;
435       goto clean_and_return_1;
436     }
437 #endif
438   }
439   phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams.initMode,
440                   sizeof(phNxpEse_initParams));
441 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
442   /* Updating ESE power state based on the init mode */
443   if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
444     wConfigStatus = phNxpEse_checkJcopDwnldState();
445     if (wConfigStatus != ESESTATUS_SUCCESS) {
446       ALOGE("phNxpEse_checkJcopDwnldState failed");
447       goto clean_and_return_1;
448     }
449   }
450 #endif
451   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_PRIO_ENABLE);
452   if (wSpmStatus != ESESTATUS_SUCCESS) {
453     ALOGE("phNxpEse_SPM_ConfigPwr: enabling power for spi prio Failed");
454     if (wSpmStatus == ESESTATUS_BUSY) {
455       wConfigStatus = ESESTATUS_BUSY;
456     } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
457       wConfigStatus = ESESTATUS_DWNLD_BUSY;
458     } else {
459       wConfigStatus = ESESTATUS_FAILED;
460     }
461     goto clean_and_return;
462   } else {
463     ALOGE("nxpese_ctxt.spm_power_state true");
464     nxpese_ctxt.spm_power_state = true;
465   }
466 #endif
467 
468 #ifndef SPM_INTEGRATED
469   wConfigStatus =
470       phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
471   if (wConfigStatus != ESESTATUS_SUCCESS) {
472     ALOGE("phPalEse_IoCtl Failed");
473     goto clean_and_return;
474   }
475 #endif
476   wConfigStatus =
477       phPalEse_ioctl(phPalEse_e_EnableLog, nxpese_ctxt.pDevHandle, 0);
478   if (wConfigStatus != ESESTATUS_SUCCESS) {
479     ALOGE("phPalEse_IoCtl Failed");
480     goto clean_and_return;
481   }
482   wConfigStatus =
483       phPalEse_ioctl(phPalEse_e_EnablePollMode, nxpese_ctxt.pDevHandle, 1);
484   if (tpm_enable) {
485     wConfigStatus = phPalEse_ioctl(phPalEse_e_EnableThroughputMeasurement,
486                                    nxpese_ctxt.pDevHandle, 0);
487     if (wConfigStatus != ESESTATUS_SUCCESS) {
488       ALOGE("phPalEse_IoCtl Failed");
489       goto clean_and_return;
490     }
491   }
492   if (wConfigStatus != ESESTATUS_SUCCESS) {
493     ALOGE("phPalEse_IoCtl Failed");
494     goto clean_and_return;
495   }
496 
497   ALOGE("wConfigStatus %x", wConfigStatus);
498 
499   return wConfigStatus;
500 
501 clean_and_return:
502 #ifdef SPM_INTEGRATED
503   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
504   if (wSpmStatus != ESESTATUS_SUCCESS) {
505     ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
506   }
507 clean_and_return_1:
508   phNxpEse_SPM_DeInit();
509 clean_and_return_2:
510 #endif
511   if (NULL != nxpese_ctxt.pDevHandle) {
512     phPalEse_close(nxpese_ctxt.pDevHandle);
513     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
514   }
515   nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
516   nxpese_ctxt.spm_power_state = false;
517   return ESESTATUS_FAILED;
518 }
519 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
520 /******************************************************************************
521  * Function         phNxpEse_setJcopDwnldState
522  *
523  * Description      This function is  used to check whether JCOP OS
524  *                  download can be started or not.
525  *
526  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_FAILED
527  *
528  ******************************************************************************/
phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state)529 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state) {
530   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
531   ESESTATUS wConfigStatus = ESESTATUS_FAILED;
532   ALOGE("phNxpEse_setJcopDwnldState Enter");
533 
534   wSpmStatus = phNxpEse_SPM_SetJcopDwnldState(state);
535   if (wSpmStatus == ESESTATUS_SUCCESS) {
536     wConfigStatus = ESESTATUS_SUCCESS;
537   }
538 
539   return wConfigStatus;
540 }
541 
542 /******************************************************************************
543  * Function         phNxpEse_checkJcopDwnldState
544  *
545  * Description      This function is  used to check whether JCOP OS
546  *                  download can be started or not.
547  *
548  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_BUSY
549  *
550  ******************************************************************************/
phNxpEse_checkJcopDwnldState(void)551 static ESESTATUS phNxpEse_checkJcopDwnldState(void) {
552   ALOGE("phNxpEse_checkJcopDwnld Enter");
553   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
554   spm_state_t current_spm_state = SPM_STATE_INVALID;
555   uint8_t ese_dwnld_retry = 0x00;
556   ESESTATUS status = ESESTATUS_FAILED;
557 
558   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
559   if (wSpmStatus == ESESTATUS_SUCCESS) {
560     /* Check current_spm_state and update config/Spm status*/
561     if ((current_spm_state & SPM_STATE_JCOP_DWNLD) ||
562         (current_spm_state & SPM_STATE_WIRED))
563       return ESESTATUS_BUSY;
564 
565     status = phNxpEse_setJcopDwnldState(JCP_DWNLD_INIT);
566     if (status == ESESTATUS_SUCCESS) {
567       while (ese_dwnld_retry < ESE_JCOP_OS_DWNLD_RETRY_CNT) {
568         ALOGE("ESE_JCOP_OS_DWNLD_RETRY_CNT retry count");
569         wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
570         if (wSpmStatus == ESESTATUS_SUCCESS) {
571           if ((current_spm_state & SPM_STATE_JCOP_DWNLD)) {
572             status = ESESTATUS_SUCCESS;
573             break;
574           }
575         } else {
576           status = ESESTATUS_FAILED;
577           break;
578         }
579         phNxpEse_Sleep(
580             200000); /*sleep for 200 ms checking for jcop dwnld status*/
581         ese_dwnld_retry++;
582       }
583     }
584   }
585 
586   ALOGE("phNxpEse_checkJcopDwnldState status %x", status);
587   return status;
588 }
589 #endif
590 /******************************************************************************
591  * Function         phNxpEse_Transceive
592  *
593  * Description      This function update the len and provided buffer
594  *
595  * Returns          On Success ESESTATUS_SUCCESS else proper error code
596  *
597  ******************************************************************************/
phNxpEse_Transceive(phNxpEse_data * pCmd,phNxpEse_data * pRsp)598 ESESTATUS phNxpEse_Transceive(phNxpEse_data* pCmd, phNxpEse_data* pRsp) {
599   ESESTATUS status = ESESTATUS_FAILED;
600 
601   if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
602 
603   if ((pCmd->len == 0) || pCmd->p_data == NULL) {
604     ALOGE(" phNxpEse_Transceive - Invalid Parameter no data\n");
605     return ESESTATUS_INVALID_PARAMETER;
606   } else if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
607     ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
608     return ESESTATUS_NOT_INITIALISED;
609   } else if ((ESE_STATUS_BUSY == nxpese_ctxt.EseLibStatus)) {
610     ALOGE(" %s ESE - BUSY \n", __FUNCTION__);
611     return ESESTATUS_BUSY;
612   } else {
613     nxpese_ctxt.EseLibStatus = ESE_STATUS_BUSY;
614     status = phNxpEseProto7816_Transceive((phNxpEse_data*)pCmd,
615                                           (phNxpEse_data*)pRsp);
616     if (ESESTATUS_SUCCESS != status) {
617       ALOGE(" %s phNxpEseProto7816_Transceive- Failed \n", __FUNCTION__);
618     }
619     nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
620 
621     ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
622              status);
623     return status;
624   }
625 }
626 
627 /******************************************************************************
628  * Function         phNxpEse_reset
629  *
630  * Description      This function reset the ESE interface and free all
631  *
632  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
633  *successful else
634  *                  ESESTATUS_FAILED(1)
635  ******************************************************************************/
phNxpEse_reset(void)636 ESESTATUS phNxpEse_reset(void) {
637   ESESTATUS status = ESESTATUS_SUCCESS;
638   unsigned long maxTimer = 0;
639 #ifdef SPM_INTEGRATED
640   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
641 #endif
642 
643   /* TBD : Call the ioctl to reset the ESE */
644   ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
645   /* Do an interface reset, don't wait to see if JCOP went through a full power
646    * cycle or not */
647   ESESTATUS bStatus = phNxpEseProto7816_IntfReset(
648       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
649   if (!bStatus) status = ESESTATUS_FAILED;
650   ALOGD_IF(ese_debug_enabled,
651            "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
652            __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
653            nxpese_ctxt.secureTimerParams.secureTimer2,
654            nxpese_ctxt.secureTimerParams.secureTimer3);
655   phNxpEse_GetMaxTimer(&maxTimer);
656 #ifdef SPM_INTEGRATED
657 #ifdef NXP_SECURE_TIMER_SESSION
658   status = phNxpEse_SPM_DisablePwrControl(maxTimer);
659   if (status != ESESTATUS_SUCCESS) {
660     ALOGE("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
661   }
662 #endif
663   if ((nxpese_ctxt.pwr_scheme == PN67T_POWER_SCHEME) ||
664       (nxpese_ctxt.pwr_scheme == PN80T_LEGACY_SCHEME)) {
665     wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
666     if (wSpmStatus != ESESTATUS_SUCCESS) {
667       ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
668     }
669   }
670 #else
671   /* if arg ==2 (hard reset)
672    * if arg ==1 (soft reset)
673    */
674   status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
675   if (status != ESESTATUS_SUCCESS) {
676     ALOGE("phNxpEse_reset Failed");
677   }
678 #endif
679   ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
680   return status;
681 }
682 
683 /******************************************************************************
684  * Function         phNxpEse_resetJcopUpdate
685  *
686  * Description      This function reset the ESE interface during JCOP Update
687  *
688  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
689  *successful else
690  *                  ESESTATUS_FAILED(1)
691  ******************************************************************************/
phNxpEse_resetJcopUpdate(void)692 ESESTATUS phNxpEse_resetJcopUpdate(void) {
693   ESESTATUS status = ESESTATUS_SUCCESS;
694 
695 #ifdef SPM_INTEGRATED
696 #ifdef NXP_POWER_SCHEME_SUPPORT
697   unsigned long int num = 0;
698 #endif
699 #endif
700 
701   /* TBD : Call the ioctl to reset the  */
702   ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
703 
704   /* Reset interface after every reset irrespective of
705   whether JCOP did a full power cycle or not. */
706   status = phNxpEseProto7816_Reset();
707 
708 #ifdef SPM_INTEGRATED
709 #ifdef NXP_POWER_SCHEME_SUPPORT
710   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
711     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
712     if ((num == 1) || (num == 2)) {
713       ALOGD_IF(ese_debug_enabled, " %s Call Config Pwr Reset \n", __FUNCTION__);
714       status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
715       if (status != ESESTATUS_SUCCESS) {
716         ALOGE("phNxpEse_resetJcopUpdate: reset Failed");
717         status = ESESTATUS_FAILED;
718       }
719     } else if (num == 3) {
720       ALOGD_IF(ese_debug_enabled, " %s Call eSE Chip Reset \n", __FUNCTION__);
721       status = phNxpEse_chipReset();
722       if (status != ESESTATUS_SUCCESS) {
723         ALOGE("phNxpEse_resetJcopUpdate: chip reset Failed");
724         status = ESESTATUS_FAILED;
725       }
726     } else {
727       ALOGD_IF(ese_debug_enabled, " %s Invalid Power scheme \n", __FUNCTION__);
728     }
729   }
730 #else
731   {
732     status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
733     if (status != ESESTATUS_SUCCESS) {
734       ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
735       status = ESESTATUS_FAILED;
736     }
737   }
738 #endif
739 #else
740   /* if arg ==2 (hard reset)
741    * if arg ==1 (soft reset)
742    */
743   status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
744   if (status != ESESTATUS_SUCCESS) {
745     ALOGE("phNxpEse_resetJcopUpdate Failed");
746   }
747 #endif
748 
749   ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
750   return status;
751 }
752 /******************************************************************************
753  * Function         phNxpEse_EndOfApdu
754  *
755  * Description      This function is used to send S-frame to indicate
756  *END_OF_APDU
757  *
758  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
759  *successful else
760  *                  ESESTATUS_FAILED(1)
761  *
762  ******************************************************************************/
phNxpEse_EndOfApdu(void)763 ESESTATUS phNxpEse_EndOfApdu(void) {
764   ESESTATUS status = ESESTATUS_SUCCESS;
765 #ifdef NXP_ESE_END_OF_SESSION
766   status = phNxpEseProto7816_Close(
767       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
768 #endif
769   return status;
770 }
771 
772 /******************************************************************************
773  * Function         phNxpEse_chipReset
774  *
775  * Description      This function is used to reset the ESE.
776  *
777  * Returns          Always return ESESTATUS_SUCCESS (0).
778  *
779  ******************************************************************************/
phNxpEse_chipReset(void)780 ESESTATUS phNxpEse_chipReset(void) {
781   ESESTATUS status = ESESTATUS_SUCCESS;
782   ESESTATUS bStatus = ESESTATUS_FAILED;
783   if (nxpese_ctxt.pwr_scheme == PN80T_EXT_PMU_SCHEME) {
784     bStatus = phNxpEseProto7816_Reset();
785     if (!bStatus) {
786       status = ESESTATUS_FAILED;
787       ALOGE("Inside phNxpEse_chipReset, phNxpEseProto7816_Reset Failed");
788     }
789     status = phPalEse_ioctl(phPalEse_e_ChipRst, nxpese_ctxt.pDevHandle, 6);
790     if (status != ESESTATUS_SUCCESS) {
791       ALOGE("phNxpEse_chipReset  Failed");
792     }
793   } else {
794     ALOGE("phNxpEse_chipReset is not supported in legacy power scheme");
795     status = ESESTATUS_FAILED;
796   }
797   return status;
798 }
799 
800 /******************************************************************************
801  * Function         phNxpEse_deInit
802  *
803  * Description      This function de-initializes all the ESE protocol params
804  *
805  * Returns          Always return ESESTATUS_SUCCESS (0).
806  *
807  ******************************************************************************/
phNxpEse_deInit(void)808 ESESTATUS phNxpEse_deInit(void) {
809   ESESTATUS status = ESESTATUS_SUCCESS;
810   unsigned long maxTimer = 0;
811   status = phNxpEseProto7816_Close(
812       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
813   if (status == ESESTATUS_FAILED) {
814     status = ESESTATUS_FAILED;
815   } else {
816     ALOGD_IF(ese_debug_enabled,
817              "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
818              __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
819              nxpese_ctxt.secureTimerParams.secureTimer2,
820              nxpese_ctxt.secureTimerParams.secureTimer3);
821     phNxpEse_GetMaxTimer(&maxTimer);
822 #ifdef SPM_INTEGRATED
823 #ifdef NXP_SECURE_TIMER_SESSION
824     status = phNxpEse_SPM_DisablePwrControl(maxTimer);
825     if (status != ESESTATUS_SUCCESS) {
826       ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
827     }
828 #endif
829 #endif
830   }
831   return status;
832 }
833 
834 /******************************************************************************
835  * Function         phNxpEse_close
836  *
837  * Description      This function close the ESE interface and free all
838  *                  resources.
839  *
840  * Returns          Always return ESESTATUS_SUCCESS (0).
841  *
842  ******************************************************************************/
phNxpEse_close(void)843 ESESTATUS phNxpEse_close(void) {
844   ESESTATUS status = ESESTATUS_SUCCESS;
845 
846   if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
847     ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
848     return ESESTATUS_NOT_INITIALISED;
849   }
850 
851 #ifdef SPM_INTEGRATED
852   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
853 #endif
854 
855 #ifdef SPM_INTEGRATED
856   /* Release the Access of  */
857   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
858   if (wSpmStatus != ESESTATUS_SUCCESS) {
859     ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
860   } else {
861     nxpese_ctxt.spm_power_state = false;
862   }
863 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
864   if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
865     status = phNxpEse_setJcopDwnldState(JCP_SPI_DWNLD_COMPLETE);
866     if (status != ESESTATUS_SUCCESS) {
867       ALOGE("%s: phNxpEse_setJcopDwnldState failed", __FUNCTION__);
868     }
869   }
870 #endif
871   wSpmStatus = phNxpEse_SPM_DeInit();
872   if (wSpmStatus != ESESTATUS_SUCCESS) {
873     ALOGE("phNxpEse_SPM_DeInit Failed");
874   }
875 
876 #endif
877   if (NULL != nxpese_ctxt.pDevHandle) {
878     phPalEse_close(nxpese_ctxt.pDevHandle);
879     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
880     ALOGD_IF(ese_debug_enabled,
881              "phNxpEse_close - ESE Context deinit completed");
882   }
883   /* Return success always */
884   return status;
885 }
886 
887 /******************************************************************************
888  * Function         phNxpEse_read
889  *
890  * Description      This function write the data to ESE through physical
891  *                  interface (e.g. I2C) using the  driver interface.
892  *                  Before sending the data to ESE, phNxpEse_write_ext
893  *                  is called to check if there is any extension processing
894  *                  is required for the SPI packet being sent out.
895  *
896  * Returns          It returns ESESTATUS_SUCCESS (0) if read successful else
897  *                  ESESTATUS_FAILED(1)
898  *
899  ******************************************************************************/
phNxpEse_read(uint32_t * data_len,uint8_t ** pp_data)900 ESESTATUS phNxpEse_read(uint32_t* data_len, uint8_t** pp_data) {
901   ESESTATUS status = ESESTATUS_SUCCESS;
902   int ret = -1;
903 
904   ALOGD_IF(ese_debug_enabled, "%s Enter ..", __FUNCTION__);
905 
906   ret = phNxpEse_readPacket(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_read_buff,
907                             MAX_DATA_LEN);
908   if (ret < 0) {
909     ALOGE("PAL Read status error status = %x", status);
910     *data_len = 2;
911     *pp_data = nxpese_ctxt.p_read_buff;
912     status = ESESTATUS_FAILED;
913   } else {
914     PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff, ret);
915     *data_len = ret;
916     *pp_data = nxpese_ctxt.p_read_buff;
917     status = ESESTATUS_SUCCESS;
918   }
919 
920   ALOGD_IF(ese_debug_enabled, "%s Exit", __FUNCTION__);
921   return status;
922 }
923 
924 /******************************************************************************
925  * Function         phNxpEse_readPacket
926  *
927  * Description      This function Reads requested number of bytes from
928  *                  pn547 device into given buffer.
929  *
930  * Returns          nNbBytesToRead- number of successfully read bytes
931  *                  -1        - read operation failure
932  *
933  ******************************************************************************/
phNxpEse_readPacket(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)934 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
935                                int nNbBytesToRead) {
936   int ret = -1;
937   int sof_counter = 0; /* one read may take 1 ms*/
938   int total_count = 0, numBytesToRead = 0, headerIndex = 0;
939 
940   ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
941   do {
942     sof_counter++;
943     ret = -1;
944     ret = phPalEse_read(pDevHandle, pBuffer, 2);
945     if (ret < 0) {
946       /*Polling for read on spi, hence Debug log*/
947       ALOGD_IF(ese_debug_enabled, "_spi_read() [HDR]errno : %x ret : %X", errno,
948                ret);
949     }
950     if (pBuffer[0] == RECIEVE_PACKET_SOF) {
951       /* Read the HEADR of one byte*/
952       ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
953       numBytesToRead = 1;
954       headerIndex = 1;
955       break;
956     } else if (pBuffer[1] == RECIEVE_PACKET_SOF) {
957       /* Read the HEADR of Two bytes*/
958       ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
959       pBuffer[0] = RECIEVE_PACKET_SOF;
960       numBytesToRead = 2;
961       headerIndex = 0;
962       break;
963     }
964     ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus", __FUNCTION__,
965              READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
966     phPalEse_sleep(READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
967   } while (sof_counter < ESE_NAD_POLLING_MAX);
968   if (pBuffer[0] == RECIEVE_PACKET_SOF) {
969     ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
970     /* Read the HEADR of one/Two bytes based on how two bytes read A5 PCB or 00
971      * A5*/
972     ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
973     if (ret < 0) {
974       ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
975     }
976     total_count = 3;
977     nNbBytesToRead = pBuffer[2];
978     /* Read the Complete data + one byte CRC*/
979     ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
980     if (ret < 0) {
981       ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
982       ret = -1;
983     } else {
984       ret = (total_count + (nNbBytesToRead + 1));
985     }
986   } else if (ret < 0) {
987     /*In case of IO Error*/
988     ret = -2;
989     pBuffer[0] = 0x64;
990     pBuffer[1] = 0xFF;
991   } else {
992     ret = -1;
993   }
994   ALOGD_IF(ese_debug_enabled, "%s Exit ret = %d", __FUNCTION__, ret);
995   return ret;
996 }
997 /******************************************************************************
998  * Function         phNxpEse_WriteFrame
999  *
1000  * Description      This is the actual function which is being called by
1001  *                  phNxpEse_write. This function writes the data to ESE.
1002  *                  It waits till write callback provide the result of write
1003  *                  process.
1004  *
1005  * Returns          It returns ESESTATUS_SUCCESS (0) if write successful else
1006  *                  ESESTATUS_FAILED(1)
1007  *
1008  ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,const uint8_t * p_data)1009 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, const uint8_t* p_data) {
1010   ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1011   int32_t dwNoBytesWrRd = 0;
1012   ALOGD_IF(ese_debug_enabled, "Enter %s ", __FUNCTION__);
1013 
1014   /* Create local copy of cmd_data */
1015   phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1016   nxpese_ctxt.cmd_len = data_len;
1017 
1018   dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1019                                  nxpese_ctxt.cmd_len);
1020   if (-1 == dwNoBytesWrRd) {
1021     ALOGE(" - Error in SPI Write.....\n");
1022     status = ESESTATUS_FAILED;
1023   } else {
1024     status = ESESTATUS_SUCCESS;
1025     PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1026   }
1027 
1028   ALOGD_IF(ese_debug_enabled, "Exit %s status %x\n", __FUNCTION__, status);
1029   return status;
1030 }
1031 
1032 /******************************************************************************
1033  * Function         phNxpEse_setIfsc
1034  *
1035  * Description      This function sets the IFSC size to 240/254 support JCOP OS
1036  *Update.
1037  *
1038  * Returns          Always return ESESTATUS_SUCCESS (0).
1039  *
1040  ******************************************************************************/
phNxpEse_setIfsc(uint16_t IFSC_Size)1041 ESESTATUS phNxpEse_setIfsc(uint16_t IFSC_Size) {
1042   /*SET the IFSC size to 240 bytes*/
1043   phNxpEseProto7816_SetIfscSize(IFSC_Size);
1044   return ESESTATUS_SUCCESS;
1045 }
1046 
1047 /******************************************************************************
1048  * Function         phNxpEse_Sleep
1049  *
1050  * Description      This function  suspends execution of the calling thread for
1051  *           (at least) usec microseconds
1052  *
1053  * Returns          Always return ESESTATUS_SUCCESS (0).
1054  *
1055  ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1056 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1057   phPalEse_sleep(usec);
1058   return ESESTATUS_SUCCESS;
1059 }
1060 
1061 /******************************************************************************
1062  * Function         phNxpEse_memset
1063  *
1064  * Description      This function updates destination buffer with val
1065  *                  data in len size
1066  *
1067  * Returns          Always return ESESTATUS_SUCCESS (0).
1068  *
1069  ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1070 void* phNxpEse_memset(void* buff, int val, size_t len) {
1071   return phPalEse_memset(buff, val, len);
1072 }
1073 
1074 /******************************************************************************
1075  * Function         phNxpEse_memcpy
1076  *
1077  * Description      This function copies source buffer to  destination buffer
1078  *                  data in len size
1079  *
1080  * Returns          Return pointer to allocated memory location.
1081  *
1082  ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1083 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1084   return phPalEse_memcpy(dest, src, len);
1085 }
1086 
1087 /******************************************************************************
1088  * Function         phNxpEse_Memalloc
1089  *
1090  * Description      This function allocation memory
1091  *
1092  * Returns          Return pointer to allocated memory or NULL.
1093  *
1094  ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1095 void* phNxpEse_memalloc(uint32_t size) {
1096   return phPalEse_memalloc(size);
1097   ;
1098 }
1099 
1100 /******************************************************************************
1101  * Function         phNxpEse_calloc
1102  *
1103  * Description      This is utility function for runtime heap memory allocation
1104  *
1105  * Returns          Return pointer to allocated memory or NULL.
1106  *
1107  ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1108 void* phNxpEse_calloc(size_t datatype, size_t size) {
1109   return phPalEse_calloc(datatype, size);
1110 }
1111 
1112 /******************************************************************************
1113  * Function         phNxpEse_free
1114  *
1115  * Description      This function de-allocation memory
1116  *
1117  * Returns         void.
1118  *
1119  ******************************************************************************/
phNxpEse_free(void * ptr)1120 void phNxpEse_free(void* ptr) {
1121   if (ptr != NULL) {
1122     free(ptr);
1123     ptr = NULL;
1124   }
1125   return;
1126 }
1127 
1128 /******************************************************************************
1129  * Function         phNxpEse_GetMaxTimer
1130  *
1131  * Description      This function finds out the max. timer value returned from
1132  *JCOP
1133  *
1134  * Returns          void.
1135  *
1136  ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1137 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1138   /* Finding the max. of the timer value */
1139   *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1140   if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1141     *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1142   *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1143                    ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1144                    : *pMaxTimer;
1145 
1146   /* Converting timer to millisecond from sec */
1147   *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1148   /* Add extra 5% to the timer */
1149   *pMaxTimer +=
1150       CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1151   ALOGE("%s Max timer value = %lu", __FUNCTION__, *pMaxTimer);
1152   return;
1153 }
1154 
1155 /******************************************************************************
1156  * Function         phNxpEseP61_DisablePwrCntrl
1157  *
1158  * Description      This function disables eSE GPIO power off/on control
1159  *                  when enabled
1160  *
1161  * Returns         SUCCESS/FAIL.
1162  *
1163  ******************************************************************************/
phNxpEse_DisablePwrCntrl(void)1164 ESESTATUS phNxpEse_DisablePwrCntrl(void) {
1165   ESESTATUS status = ESESTATUS_SUCCESS;
1166   unsigned long maxTimer = 0;
1167   ALOGE("%s Enter", __FUNCTION__);
1168   phNxpEse_GetMaxTimer(&maxTimer);
1169 #ifdef NXP_SECURE_TIMER_SESSION
1170   status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1171   if (status != ESESTATUS_SUCCESS) {
1172     ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1173   }
1174 #else
1175   ALOGE("%s phNxpEseP61_DisablePwrCntrl: not supported", __FUNCTION__);
1176   status = ESESTATUS_FAILED;
1177 #endif
1178   return status;
1179 }
1180 
1181 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
1182 /******************************************************************************
1183  * Function         phNxpEse_checkFWDwnldStatus
1184  *
1185  * Description      This function is  used to  check whether FW download
1186  *                  is completed or not.
1187  *
1188  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_BUSY
1189  *
1190  ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1191 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1192   ALOGE("phNxpEse_checkFWDwnldStatus Enter");
1193   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1194   spm_state_t current_spm_state = SPM_STATE_INVALID;
1195   uint8_t ese_dwnld_retry = 0x00;
1196   ESESTATUS status = ESESTATUS_FAILED;
1197 
1198   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
1199   if (wSpmStatus == ESESTATUS_SUCCESS) {
1200     /* Check current_spm_state and update config/Spm status*/
1201     while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1202       ALOGE("ESE_FW_DWNLD_RETRY_CNT retry count");
1203       wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
1204       if (wSpmStatus == ESESTATUS_SUCCESS) {
1205         if ((current_spm_state & SPM_STATE_DWNLD)) {
1206           status = ESESTATUS_FAILED;
1207         } else {
1208           ALOGE("Exit polling no FW Download ..");
1209           status = ESESTATUS_SUCCESS;
1210           break;
1211         }
1212       } else {
1213         status = ESESTATUS_FAILED;
1214         break;
1215       }
1216       phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1217       ese_dwnld_retry++;
1218     }
1219   }
1220 
1221   ALOGE("phNxpEse_checkFWDwnldStatus status %x", status);
1222   return status;
1223 }
1224 #endif
1225 /******************************************************************************
1226  * Function         phNxpEse_GetEseStatus(unsigned char *timer_buffer)
1227  *
1228  * Description      This function returns the all three timer
1229  * Timeout buffer length should be minimum 18 bytes. Response will be in below
1230  format:
1231  * <0xF1><Len><Timer Value><0xF2><Len><Timer Value><0xF3><Len><Timer Value>
1232  *
1233  * Returns         SUCCESS/FAIL.
1234  * ESESTATUS_SUCCESS if 0xF1 or 0xF2 tag timeout >= 0 & 0xF3 == 0
1235  * ESESTATUS_BUSY if 0xF3 tag timeout > 0
1236  * ESESTATUS_FAILED if any other error
1237 
1238  ******************************************************************************/
phNxpEse_GetEseStatus(phNxpEse_data * timer_buffer)1239 ESESTATUS phNxpEse_GetEseStatus(phNxpEse_data* timer_buffer) {
1240   ESESTATUS status = ESESTATUS_FAILED;
1241 
1242   phNxpEse_SecureTimer_t secureTimerParams;
1243   uint8_t* temp_timer_buffer = NULL;
1244   ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
1245 
1246   if (timer_buffer != NULL) {
1247     timer_buffer->len =
1248         (sizeof(secureTimerParams.secureTimer1) +
1249          sizeof(secureTimerParams.secureTimer2) +
1250          sizeof(secureTimerParams.secureTimer3)) +
1251         PH_PROPTO_7816_FRAME_LENGTH_OFFSET * PH_PROPTO_7816_FRAME_LENGTH_OFFSET;
1252     temp_timer_buffer = (uint8_t*)phNxpEse_memalloc(timer_buffer->len);
1253     timer_buffer->p_data = temp_timer_buffer;
1254 
1255 #ifdef NXP_SECURE_TIMER_SESSION
1256     phNxpEse_memcpy(&secureTimerParams, &nxpese_ctxt.secureTimerParams,
1257                     sizeof(phNxpEse_SecureTimer_t));
1258 
1259     ALOGD_IF(
1260         ese_debug_enabled,
1261         "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x len = %d",
1262         __FUNCTION__, secureTimerParams.secureTimer1,
1263         secureTimerParams.secureTimer2, secureTimerParams.secureTimer3,
1264         timer_buffer->len);
1265 
1266     *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER1;
1267     *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer1);
1268     temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1269         temp_timer_buffer, secureTimerParams.secureTimer1);
1270     if (temp_timer_buffer != NULL) {
1271       *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER2;
1272       *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer2);
1273       temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1274           temp_timer_buffer, secureTimerParams.secureTimer2);
1275       if (temp_timer_buffer != NULL) {
1276         *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER3;
1277         *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer3);
1278         temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1279             temp_timer_buffer, secureTimerParams.secureTimer3);
1280         if (temp_timer_buffer != NULL) {
1281           if (secureTimerParams.secureTimer3 > 0) {
1282             status = ESESTATUS_BUSY;
1283           } else {
1284             status = ESESTATUS_SUCCESS;
1285           }
1286         }
1287       }
1288     }
1289 #endif
1290   } else {
1291     ALOGE("%s Invalid timer buffer ", __FUNCTION__);
1292   }
1293 
1294   ALOGD_IF(ese_debug_enabled, "%s Exit status = 0x%x", __FUNCTION__, status);
1295   return status;
1296 }
1297 #ifdef NXP_SECURE_TIMER_SESSION
phNxpEse_GgetTimerTlvBuffer(uint8_t * timer_buffer,unsigned int value)1298 static unsigned char* phNxpEse_GgetTimerTlvBuffer(uint8_t* timer_buffer,
1299                                                   unsigned int value) {
1300   short int count = 0, shift = 3;
1301   unsigned int mask = 0x000000FF;
1302   ALOGD_IF(ese_debug_enabled, "value = %x \n", value);
1303   for (count = 0; count < 4; count++) {
1304     if (timer_buffer != NULL) {
1305       *timer_buffer = (value >> (shift * 8) & mask);
1306       ALOGD_IF(ese_debug_enabled, "*timer_buffer=0x%x shift=0x%x",
1307                *timer_buffer, shift);
1308       timer_buffer++;
1309       shift--;
1310     } else {
1311       break;
1312     }
1313   }
1314   return timer_buffer;
1315 }
1316 #endif
1317