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 strlcpy(ese_dev_node, ese_node.c_str(), sizeof(ese_dev_node));
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(¤t_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;
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(¤t_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 (wConfigStatus != ESESTATUS_SUCCESS) {
485 ALOGE("phPalEse_IoCtl Failed");
486 goto clean_and_return;
487 }
488
489 ALOGE("wConfigStatus %x", wConfigStatus);
490
491 return wConfigStatus;
492
493 clean_and_return:
494 #ifdef SPM_INTEGRATED
495 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
496 if (wSpmStatus != ESESTATUS_SUCCESS) {
497 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
498 }
499 clean_and_return_1:
500 phNxpEse_SPM_DeInit();
501 clean_and_return_2:
502 #endif
503 if (NULL != nxpese_ctxt.pDevHandle) {
504 phPalEse_close(nxpese_ctxt.pDevHandle);
505 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
506 }
507 nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
508 nxpese_ctxt.spm_power_state = false;
509 return ESESTATUS_FAILED;
510 }
511 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
512 /******************************************************************************
513 * Function phNxpEse_setJcopDwnldState
514 *
515 * Description This function is used to check whether JCOP OS
516 * download can be started or not.
517 *
518 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_FAILED
519 *
520 ******************************************************************************/
phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state)521 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state) {
522 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
523 ESESTATUS wConfigStatus = ESESTATUS_FAILED;
524 ALOGE("phNxpEse_setJcopDwnldState Enter");
525
526 wSpmStatus = phNxpEse_SPM_SetJcopDwnldState(state);
527 if (wSpmStatus == ESESTATUS_SUCCESS) {
528 wConfigStatus = ESESTATUS_SUCCESS;
529 }
530
531 return wConfigStatus;
532 }
533
534 /******************************************************************************
535 * Function phNxpEse_checkJcopDwnldState
536 *
537 * Description This function is used to check whether JCOP OS
538 * download can be started or not.
539 *
540 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
541 *
542 ******************************************************************************/
phNxpEse_checkJcopDwnldState(void)543 static ESESTATUS phNxpEse_checkJcopDwnldState(void) {
544 ALOGE("phNxpEse_checkJcopDwnld Enter");
545 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
546 spm_state_t current_spm_state = SPM_STATE_INVALID;
547 uint8_t ese_dwnld_retry = 0x00;
548 ESESTATUS status = ESESTATUS_FAILED;
549
550 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
551 if (wSpmStatus == ESESTATUS_SUCCESS) {
552 /* Check current_spm_state and update config/Spm status*/
553 if ((current_spm_state & SPM_STATE_JCOP_DWNLD) ||
554 (current_spm_state & SPM_STATE_WIRED))
555 return ESESTATUS_BUSY;
556
557 status = phNxpEse_setJcopDwnldState(JCP_DWNLD_INIT);
558 if (status == ESESTATUS_SUCCESS) {
559 while (ese_dwnld_retry < ESE_JCOP_OS_DWNLD_RETRY_CNT) {
560 ALOGE("ESE_JCOP_OS_DWNLD_RETRY_CNT retry count");
561 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
562 if (wSpmStatus == ESESTATUS_SUCCESS) {
563 if ((current_spm_state & SPM_STATE_JCOP_DWNLD)) {
564 status = ESESTATUS_SUCCESS;
565 break;
566 }
567 } else {
568 status = ESESTATUS_FAILED;
569 break;
570 }
571 phNxpEse_Sleep(
572 200000); /*sleep for 200 ms checking for jcop dwnld status*/
573 ese_dwnld_retry++;
574 }
575 }
576 }
577
578 ALOGE("phNxpEse_checkJcopDwnldState status %x", status);
579 return status;
580 }
581 #endif
582 /******************************************************************************
583 * Function phNxpEse_Transceive
584 *
585 * Description This function update the len and provided buffer
586 *
587 * Returns On Success ESESTATUS_SUCCESS else proper error code
588 *
589 ******************************************************************************/
phNxpEse_Transceive(phNxpEse_data * pCmd,phNxpEse_data * pRsp)590 ESESTATUS phNxpEse_Transceive(phNxpEse_data* pCmd, phNxpEse_data* pRsp) {
591 ESESTATUS status = ESESTATUS_FAILED;
592
593 if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
594
595 if ((pCmd->len == 0) || pCmd->p_data == NULL) {
596 ALOGE(" phNxpEse_Transceive - Invalid Parameter no data\n");
597 return ESESTATUS_INVALID_PARAMETER;
598 } else if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
599 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
600 return ESESTATUS_NOT_INITIALISED;
601 } else if ((ESE_STATUS_BUSY == nxpese_ctxt.EseLibStatus)) {
602 ALOGE(" %s ESE - BUSY \n", __FUNCTION__);
603 return ESESTATUS_BUSY;
604 } else {
605 nxpese_ctxt.EseLibStatus = ESE_STATUS_BUSY;
606 status = phNxpEseProto7816_Transceive((phNxpEse_data*)pCmd,
607 (phNxpEse_data*)pRsp);
608 if (ESESTATUS_SUCCESS != status) {
609 ALOGE(" %s phNxpEseProto7816_Transceive- Failed \n", __FUNCTION__);
610 }
611 nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
612
613 ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
614 status);
615 return status;
616 }
617 }
618
619 /******************************************************************************
620 * Function phNxpEse_reset
621 *
622 * Description This function reset the ESE interface and free all
623 *
624 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
625 *successful else
626 * ESESTATUS_FAILED(1)
627 ******************************************************************************/
phNxpEse_reset(void)628 ESESTATUS phNxpEse_reset(void) {
629 ESESTATUS status = ESESTATUS_SUCCESS;
630 unsigned long maxTimer = 0;
631 #ifdef SPM_INTEGRATED
632 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
633 #endif
634
635 /* TBD : Call the ioctl to reset the ESE */
636 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
637 /* Do an interface reset, don't wait to see if JCOP went through a full power
638 * cycle or not */
639 ESESTATUS bStatus = phNxpEseProto7816_IntfReset(
640 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
641 if (!bStatus) status = ESESTATUS_FAILED;
642 ALOGD_IF(ese_debug_enabled,
643 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
644 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
645 nxpese_ctxt.secureTimerParams.secureTimer2,
646 nxpese_ctxt.secureTimerParams.secureTimer3);
647 phNxpEse_GetMaxTimer(&maxTimer);
648 #ifdef SPM_INTEGRATED
649 #ifdef NXP_SECURE_TIMER_SESSION
650 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
651 if (status != ESESTATUS_SUCCESS) {
652 ALOGE("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
653 }
654 #endif
655 if ((nxpese_ctxt.pwr_scheme == PN67T_POWER_SCHEME) ||
656 (nxpese_ctxt.pwr_scheme == PN80T_LEGACY_SCHEME)) {
657 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
658 if (wSpmStatus != ESESTATUS_SUCCESS) {
659 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
660 }
661 }
662 #else
663 /* if arg ==2 (hard reset)
664 * if arg ==1 (soft reset)
665 */
666 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
667 if (status != ESESTATUS_SUCCESS) {
668 ALOGE("phNxpEse_reset Failed");
669 }
670 #endif
671 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
672 return status;
673 }
674
675 /******************************************************************************
676 * Function phNxpEse_resetJcopUpdate
677 *
678 * Description This function reset the ESE interface during JCOP Update
679 *
680 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
681 *successful else
682 * ESESTATUS_FAILED(1)
683 ******************************************************************************/
phNxpEse_resetJcopUpdate(void)684 ESESTATUS phNxpEse_resetJcopUpdate(void) {
685 ESESTATUS status = ESESTATUS_SUCCESS;
686
687 #ifdef SPM_INTEGRATED
688 #ifdef NXP_POWER_SCHEME_SUPPORT
689 unsigned long int num = 0;
690 #endif
691 #endif
692
693 /* TBD : Call the ioctl to reset the */
694 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
695
696 /* Reset interface after every reset irrespective of
697 whether JCOP did a full power cycle or not. */
698 status = phNxpEseProto7816_Reset();
699
700 #ifdef SPM_INTEGRATED
701 #ifdef NXP_POWER_SCHEME_SUPPORT
702 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
703 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
704 if ((num == 1) || (num == 2)) {
705 ALOGD_IF(ese_debug_enabled, " %s Call Config Pwr Reset \n", __FUNCTION__);
706 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
707 if (status != ESESTATUS_SUCCESS) {
708 ALOGE("phNxpEse_resetJcopUpdate: reset Failed");
709 status = ESESTATUS_FAILED;
710 }
711 } else if (num == 3) {
712 ALOGD_IF(ese_debug_enabled, " %s Call eSE Chip Reset \n", __FUNCTION__);
713 status = phNxpEse_chipReset();
714 if (status != ESESTATUS_SUCCESS) {
715 ALOGE("phNxpEse_resetJcopUpdate: chip reset Failed");
716 status = ESESTATUS_FAILED;
717 }
718 } else {
719 ALOGD_IF(ese_debug_enabled, " %s Invalid Power scheme \n", __FUNCTION__);
720 }
721 }
722 #else
723 {
724 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
725 if (status != ESESTATUS_SUCCESS) {
726 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
727 status = ESESTATUS_FAILED;
728 }
729 }
730 #endif
731 #else
732 /* if arg ==2 (hard reset)
733 * if arg ==1 (soft reset)
734 */
735 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
736 if (status != ESESTATUS_SUCCESS) {
737 ALOGE("phNxpEse_resetJcopUpdate Failed");
738 }
739 #endif
740
741 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
742 return status;
743 }
744 /******************************************************************************
745 * Function phNxpEse_EndOfApdu
746 *
747 * Description This function is used to send S-frame to indicate
748 *END_OF_APDU
749 *
750 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
751 *successful else
752 * ESESTATUS_FAILED(1)
753 *
754 ******************************************************************************/
phNxpEse_EndOfApdu(void)755 ESESTATUS phNxpEse_EndOfApdu(void) {
756 ESESTATUS status = ESESTATUS_SUCCESS;
757 #ifdef NXP_ESE_END_OF_SESSION
758 status = phNxpEseProto7816_Close(
759 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
760 #endif
761 return status;
762 }
763
764 /******************************************************************************
765 * Function phNxpEse_chipReset
766 *
767 * Description This function is used to reset the ESE.
768 *
769 * Returns Always return ESESTATUS_SUCCESS (0).
770 *
771 ******************************************************************************/
phNxpEse_chipReset(void)772 ESESTATUS phNxpEse_chipReset(void) {
773 ESESTATUS status = ESESTATUS_SUCCESS;
774 ESESTATUS bStatus = ESESTATUS_FAILED;
775 if (nxpese_ctxt.pwr_scheme == PN80T_EXT_PMU_SCHEME) {
776 bStatus = phNxpEseProto7816_Reset();
777 if (!bStatus) {
778 status = ESESTATUS_FAILED;
779 ALOGE("Inside phNxpEse_chipReset, phNxpEseProto7816_Reset Failed");
780 }
781 status = phPalEse_ioctl(phPalEse_e_ChipRst, nxpese_ctxt.pDevHandle, 6);
782 if (status != ESESTATUS_SUCCESS) {
783 ALOGE("phNxpEse_chipReset Failed");
784 }
785 } else {
786 ALOGE("phNxpEse_chipReset is not supported in legacy power scheme");
787 status = ESESTATUS_FAILED;
788 }
789 return status;
790 }
791
792 /******************************************************************************
793 * Function phNxpEse_deInit
794 *
795 * Description This function de-initializes all the ESE protocol params
796 *
797 * Returns Always return ESESTATUS_SUCCESS (0).
798 *
799 ******************************************************************************/
phNxpEse_deInit(void)800 ESESTATUS phNxpEse_deInit(void) {
801 ESESTATUS status = ESESTATUS_SUCCESS;
802 unsigned long maxTimer = 0;
803 status = phNxpEseProto7816_Close(
804 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
805 if (status == ESESTATUS_FAILED) {
806 status = ESESTATUS_FAILED;
807 } else {
808 ALOGD_IF(ese_debug_enabled,
809 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
810 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
811 nxpese_ctxt.secureTimerParams.secureTimer2,
812 nxpese_ctxt.secureTimerParams.secureTimer3);
813 phNxpEse_GetMaxTimer(&maxTimer);
814 #ifdef SPM_INTEGRATED
815 #ifdef NXP_SECURE_TIMER_SESSION
816 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
817 if (status != ESESTATUS_SUCCESS) {
818 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
819 }
820 #endif
821 #endif
822 }
823 return status;
824 }
825
826 /******************************************************************************
827 * Function phNxpEse_close
828 *
829 * Description This function close the ESE interface and free all
830 * resources.
831 *
832 * Returns Always return ESESTATUS_SUCCESS (0).
833 *
834 ******************************************************************************/
phNxpEse_close(void)835 ESESTATUS phNxpEse_close(void) {
836 ESESTATUS status = ESESTATUS_SUCCESS;
837
838 if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
839 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
840 return ESESTATUS_NOT_INITIALISED;
841 }
842
843 #ifdef SPM_INTEGRATED
844 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
845 #endif
846
847 #ifdef SPM_INTEGRATED
848 /* Release the Access of */
849 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
850 if (wSpmStatus != ESESTATUS_SUCCESS) {
851 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
852 } else {
853 nxpese_ctxt.spm_power_state = false;
854 }
855 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
856 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
857 status = phNxpEse_setJcopDwnldState(JCP_SPI_DWNLD_COMPLETE);
858 if (status != ESESTATUS_SUCCESS) {
859 ALOGE("%s: phNxpEse_setJcopDwnldState failed", __FUNCTION__);
860 }
861 }
862 #endif
863 wSpmStatus = phNxpEse_SPM_DeInit();
864 if (wSpmStatus != ESESTATUS_SUCCESS) {
865 ALOGE("phNxpEse_SPM_DeInit Failed");
866 }
867
868 #endif
869 if (NULL != nxpese_ctxt.pDevHandle) {
870 phPalEse_close(nxpese_ctxt.pDevHandle);
871 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
872 ALOGD_IF(ese_debug_enabled,
873 "phNxpEse_close - ESE Context deinit completed");
874 }
875 /* Return success always */
876 return status;
877 }
878
879 /******************************************************************************
880 * Function phNxpEse_read
881 *
882 * Description This function write the data to ESE through physical
883 * interface (e.g. I2C) using the driver interface.
884 * Before sending the data to ESE, phNxpEse_write_ext
885 * is called to check if there is any extension processing
886 * is required for the SPI packet being sent out.
887 *
888 * Returns It returns ESESTATUS_SUCCESS (0) if read successful else
889 * ESESTATUS_FAILED(1)
890 *
891 ******************************************************************************/
phNxpEse_read(uint32_t * data_len,uint8_t ** pp_data)892 ESESTATUS phNxpEse_read(uint32_t* data_len, uint8_t** pp_data) {
893 ESESTATUS status = ESESTATUS_SUCCESS;
894 int ret = -1;
895
896 ALOGD_IF(ese_debug_enabled, "%s Enter ..", __FUNCTION__);
897
898 ret = phNxpEse_readPacket(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_read_buff,
899 MAX_DATA_LEN);
900 if (ret < 0) {
901 ALOGE("PAL Read status error status = %x", status);
902 *data_len = 2;
903 *pp_data = nxpese_ctxt.p_read_buff;
904 status = ESESTATUS_FAILED;
905 } else {
906 PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff, ret);
907 *data_len = ret;
908 *pp_data = nxpese_ctxt.p_read_buff;
909 status = ESESTATUS_SUCCESS;
910 }
911
912 ALOGD_IF(ese_debug_enabled, "%s Exit", __FUNCTION__);
913 return status;
914 }
915
916 /******************************************************************************
917 * Function phNxpEse_readPacket
918 *
919 * Description This function Reads requested number of bytes from
920 * pn547 device into given buffer.
921 *
922 * Returns nNbBytesToRead- number of successfully read bytes
923 * -1 - read operation failure
924 *
925 ******************************************************************************/
phNxpEse_readPacket(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)926 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
927 int nNbBytesToRead) {
928 int ret = -1;
929 int sof_counter = 0; /* one read may take 1 ms*/
930 int total_count = 0, numBytesToRead = 0, headerIndex = 0;
931
932 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
933 do {
934 sof_counter++;
935 ret = -1;
936 ret = phPalEse_read(pDevHandle, pBuffer, 2);
937 if (ret < 0) {
938 /*Polling for read on spi, hence Debug log*/
939 ALOGD_IF(ese_debug_enabled, "_spi_read() [HDR]errno : %x ret : %X", errno,
940 ret);
941 }
942 if (pBuffer[0] == RECIEVE_PACKET_SOF) {
943 /* Read the HEADR of one byte*/
944 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
945 numBytesToRead = 1;
946 headerIndex = 1;
947 break;
948 } else if (pBuffer[1] == RECIEVE_PACKET_SOF) {
949 /* Read the HEADR of Two bytes*/
950 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
951 pBuffer[0] = RECIEVE_PACKET_SOF;
952 numBytesToRead = 2;
953 headerIndex = 0;
954 break;
955 }
956 ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus", __FUNCTION__,
957 READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
958 phPalEse_sleep(READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
959 } while (sof_counter < ESE_NAD_POLLING_MAX);
960 if (pBuffer[0] == RECIEVE_PACKET_SOF) {
961 ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
962 /* Read the HEADR of one/Two bytes based on how two bytes read A5 PCB or 00
963 * A5*/
964 ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
965 if (ret < 0) {
966 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
967 }
968 total_count = 3;
969 nNbBytesToRead = pBuffer[2];
970 /* Read the Complete data + one byte CRC*/
971 ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
972 if (ret < 0) {
973 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
974 ret = -1;
975 } else {
976 ret = (total_count + (nNbBytesToRead + 1));
977 }
978 } else if (ret < 0) {
979 /*In case of IO Error*/
980 ret = -2;
981 pBuffer[0] = 0x64;
982 pBuffer[1] = 0xFF;
983 } else {
984 ret = -1;
985 }
986 ALOGD_IF(ese_debug_enabled, "%s Exit ret = %d", __FUNCTION__, ret);
987 return ret;
988 }
989 /******************************************************************************
990 * Function phNxpEse_WriteFrame
991 *
992 * Description This is the actual function which is being called by
993 * phNxpEse_write. This function writes the data to ESE.
994 * It waits till write callback provide the result of write
995 * process.
996 *
997 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
998 * ESESTATUS_FAILED(1)
999 *
1000 ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,const uint8_t * p_data)1001 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, const uint8_t* p_data) {
1002 ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1003 int32_t dwNoBytesWrRd = 0;
1004 ALOGD_IF(ese_debug_enabled, "Enter %s ", __FUNCTION__);
1005
1006 /* Create local copy of cmd_data */
1007 phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1008 nxpese_ctxt.cmd_len = data_len;
1009
1010 dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1011 nxpese_ctxt.cmd_len);
1012 if (-1 == dwNoBytesWrRd) {
1013 ALOGE(" - Error in SPI Write.....\n");
1014 status = ESESTATUS_FAILED;
1015 } else {
1016 status = ESESTATUS_SUCCESS;
1017 PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1018 }
1019
1020 ALOGD_IF(ese_debug_enabled, "Exit %s status %x\n", __FUNCTION__, status);
1021 return status;
1022 }
1023
1024 /******************************************************************************
1025 * Function phNxpEse_setIfsc
1026 *
1027 * Description This function sets the IFSC size to 240/254 support JCOP OS
1028 *Update.
1029 *
1030 * Returns Always return ESESTATUS_SUCCESS (0).
1031 *
1032 ******************************************************************************/
phNxpEse_setIfsc(uint16_t IFSC_Size)1033 ESESTATUS phNxpEse_setIfsc(uint16_t IFSC_Size) {
1034 /*SET the IFSC size to 240 bytes*/
1035 phNxpEseProto7816_SetIfscSize(IFSC_Size);
1036 return ESESTATUS_SUCCESS;
1037 }
1038
1039 /******************************************************************************
1040 * Function phNxpEse_Sleep
1041 *
1042 * Description This function suspends execution of the calling thread for
1043 * (at least) usec microseconds
1044 *
1045 * Returns Always return ESESTATUS_SUCCESS (0).
1046 *
1047 ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1048 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1049 phPalEse_sleep(usec);
1050 return ESESTATUS_SUCCESS;
1051 }
1052
1053 /******************************************************************************
1054 * Function phNxpEse_memset
1055 *
1056 * Description This function updates destination buffer with val
1057 * data in len size
1058 *
1059 * Returns Always return ESESTATUS_SUCCESS (0).
1060 *
1061 ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1062 void* phNxpEse_memset(void* buff, int val, size_t len) {
1063 return phPalEse_memset(buff, val, len);
1064 }
1065
1066 /******************************************************************************
1067 * Function phNxpEse_memcpy
1068 *
1069 * Description This function copies source buffer to destination buffer
1070 * data in len size
1071 *
1072 * Returns Return pointer to allocated memory location.
1073 *
1074 ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1075 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1076 return phPalEse_memcpy(dest, src, len);
1077 }
1078
1079 /******************************************************************************
1080 * Function phNxpEse_Memalloc
1081 *
1082 * Description This function allocation memory
1083 *
1084 * Returns Return pointer to allocated memory or NULL.
1085 *
1086 ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1087 void* phNxpEse_memalloc(uint32_t size) {
1088 return phPalEse_memalloc(size);
1089 ;
1090 }
1091
1092 /******************************************************************************
1093 * Function phNxpEse_calloc
1094 *
1095 * Description This is utility function for runtime heap memory allocation
1096 *
1097 * Returns Return pointer to allocated memory or NULL.
1098 *
1099 ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1100 void* phNxpEse_calloc(size_t datatype, size_t size) {
1101 return phPalEse_calloc(datatype, size);
1102 }
1103
1104 /******************************************************************************
1105 * Function phNxpEse_free
1106 *
1107 * Description This function de-allocation memory
1108 *
1109 * Returns void.
1110 *
1111 ******************************************************************************/
phNxpEse_free(void * ptr)1112 void phNxpEse_free(void* ptr) {
1113 if (ptr != NULL) {
1114 free(ptr);
1115 ptr = NULL;
1116 }
1117 return;
1118 }
1119
1120 /******************************************************************************
1121 * Function phNxpEse_GetMaxTimer
1122 *
1123 * Description This function finds out the max. timer value returned from
1124 *JCOP
1125 *
1126 * Returns void.
1127 *
1128 ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1129 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1130 /* Finding the max. of the timer value */
1131 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1132 if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1133 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1134 *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1135 ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1136 : *pMaxTimer;
1137
1138 /* Converting timer to millisecond from sec */
1139 *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1140 /* Add extra 5% to the timer */
1141 *pMaxTimer +=
1142 CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1143 ALOGE("%s Max timer value = %lu", __FUNCTION__, *pMaxTimer);
1144 return;
1145 }
1146
1147 /******************************************************************************
1148 * Function phNxpEseP61_DisablePwrCntrl
1149 *
1150 * Description This function disables eSE GPIO power off/on control
1151 * when enabled
1152 *
1153 * Returns SUCCESS/FAIL.
1154 *
1155 ******************************************************************************/
phNxpEse_DisablePwrCntrl(void)1156 ESESTATUS phNxpEse_DisablePwrCntrl(void) {
1157 ESESTATUS status = ESESTATUS_SUCCESS;
1158 unsigned long maxTimer = 0;
1159 ALOGE("%s Enter", __FUNCTION__);
1160 phNxpEse_GetMaxTimer(&maxTimer);
1161 #ifdef NXP_SECURE_TIMER_SESSION
1162 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1163 if (status != ESESTATUS_SUCCESS) {
1164 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1165 }
1166 #else
1167 ALOGE("%s phNxpEseP61_DisablePwrCntrl: not supported", __FUNCTION__);
1168 status = ESESTATUS_FAILED;
1169 #endif
1170 return status;
1171 }
1172
1173 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
1174 /******************************************************************************
1175 * Function phNxpEse_checkFWDwnldStatus
1176 *
1177 * Description This function is used to check whether FW download
1178 * is completed or not.
1179 *
1180 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
1181 *
1182 ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1183 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1184 ALOGE("phNxpEse_checkFWDwnldStatus Enter");
1185 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1186 spm_state_t current_spm_state = SPM_STATE_INVALID;
1187 uint8_t ese_dwnld_retry = 0x00;
1188 ESESTATUS status = ESESTATUS_FAILED;
1189
1190 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1191 if (wSpmStatus == ESESTATUS_SUCCESS) {
1192 /* Check current_spm_state and update config/Spm status*/
1193 while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1194 ALOGE("ESE_FW_DWNLD_RETRY_CNT retry count");
1195 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1196 if (wSpmStatus == ESESTATUS_SUCCESS) {
1197 if ((current_spm_state & SPM_STATE_DWNLD)) {
1198 status = ESESTATUS_FAILED;
1199 } else {
1200 ALOGE("Exit polling no FW Download ..");
1201 status = ESESTATUS_SUCCESS;
1202 break;
1203 }
1204 } else {
1205 status = ESESTATUS_FAILED;
1206 break;
1207 }
1208 phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1209 ese_dwnld_retry++;
1210 }
1211 }
1212
1213 ALOGE("phNxpEse_checkFWDwnldStatus status %x", status);
1214 return status;
1215 }
1216 #endif
1217 /******************************************************************************
1218 * Function phNxpEse_GetEseStatus(unsigned char *timer_buffer)
1219 *
1220 * Description This function returns the all three timer
1221 * Timeout buffer length should be minimum 18 bytes. Response will be in below
1222 format:
1223 * <0xF1><Len><Timer Value><0xF2><Len><Timer Value><0xF3><Len><Timer Value>
1224 *
1225 * Returns SUCCESS/FAIL.
1226 * ESESTATUS_SUCCESS if 0xF1 or 0xF2 tag timeout >= 0 & 0xF3 == 0
1227 * ESESTATUS_BUSY if 0xF3 tag timeout > 0
1228 * ESESTATUS_FAILED if any other error
1229
1230 ******************************************************************************/
phNxpEse_GetEseStatus(phNxpEse_data * timer_buffer)1231 ESESTATUS phNxpEse_GetEseStatus(phNxpEse_data* timer_buffer) {
1232 ESESTATUS status = ESESTATUS_FAILED;
1233
1234 phNxpEse_SecureTimer_t secureTimerParams;
1235 uint8_t* temp_timer_buffer = NULL;
1236 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
1237
1238 if (timer_buffer != NULL) {
1239 timer_buffer->len =
1240 (sizeof(secureTimerParams.secureTimer1) +
1241 sizeof(secureTimerParams.secureTimer2) +
1242 sizeof(secureTimerParams.secureTimer3)) +
1243 PH_PROPTO_7816_FRAME_LENGTH_OFFSET * PH_PROPTO_7816_FRAME_LENGTH_OFFSET;
1244 temp_timer_buffer = (uint8_t*)phNxpEse_memalloc(timer_buffer->len);
1245 timer_buffer->p_data = temp_timer_buffer;
1246
1247 #ifdef NXP_SECURE_TIMER_SESSION
1248 phNxpEse_memcpy(&secureTimerParams, &nxpese_ctxt.secureTimerParams,
1249 sizeof(phNxpEse_SecureTimer_t));
1250
1251 ALOGD_IF(
1252 ese_debug_enabled,
1253 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x len = %d",
1254 __FUNCTION__, secureTimerParams.secureTimer1,
1255 secureTimerParams.secureTimer2, secureTimerParams.secureTimer3,
1256 timer_buffer->len);
1257
1258 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER1;
1259 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer1);
1260 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1261 temp_timer_buffer, secureTimerParams.secureTimer1);
1262 if (temp_timer_buffer != NULL) {
1263 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER2;
1264 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer2);
1265 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1266 temp_timer_buffer, secureTimerParams.secureTimer2);
1267 if (temp_timer_buffer != NULL) {
1268 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER3;
1269 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer3);
1270 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1271 temp_timer_buffer, secureTimerParams.secureTimer3);
1272 if (temp_timer_buffer != NULL) {
1273 if (secureTimerParams.secureTimer3 > 0) {
1274 status = ESESTATUS_BUSY;
1275 } else {
1276 status = ESESTATUS_SUCCESS;
1277 }
1278 }
1279 }
1280 }
1281 #endif
1282 } else {
1283 ALOGE("%s Invalid timer buffer ", __FUNCTION__);
1284 }
1285
1286 ALOGD_IF(ese_debug_enabled, "%s Exit status = 0x%x", __FUNCTION__, status);
1287 return status;
1288 }
1289 #ifdef NXP_SECURE_TIMER_SESSION
phNxpEse_GgetTimerTlvBuffer(uint8_t * timer_buffer,unsigned int value)1290 static unsigned char* phNxpEse_GgetTimerTlvBuffer(uint8_t* timer_buffer,
1291 unsigned int value) {
1292 short int count = 0, shift = 3;
1293 unsigned int mask = 0x000000FF;
1294 ALOGD_IF(ese_debug_enabled, "value = %x \n", value);
1295 for (count = 0; count < 4; count++) {
1296 if (timer_buffer != NULL) {
1297 *timer_buffer = (value >> (shift * 8) & mask);
1298 ALOGD_IF(ese_debug_enabled, "*timer_buffer=0x%x shift=0x%x",
1299 *timer_buffer, shift);
1300 timer_buffer++;
1301 shift--;
1302 } else {
1303 break;
1304 }
1305 }
1306 return timer_buffer;
1307 }
1308 #endif
1309