1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/*
* Copyright (C) 2015 NXP Semiconductors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* DAL I2C port implementation for linux
*
* Project: Trusted NFC Linux
*
*/
#include <hardware/nfc.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <errno.h>
#include <phNxpLog.h>
#include <phTmlNfc_i2c.h>
#include <phNfcStatus.h>
#include <string.h>
#include <phNxpNciHal_utils.h>
#define CRC_LEN 2
#define NORMAL_MODE_HEADER_LEN 3
#define FW_DNLD_HEADER_LEN 2
#define FW_DNLD_LEN_OFFSET 1
#define NORMAL_MODE_LEN_OFFSET 2
#define FRAGMENTSIZE_MAX PHNFC_I2C_FRAGMENT_SIZE
static bool_t bFwDnldFlag = false;
bool_t notifyFwrequest;
/*******************************************************************************
**
** Function phTmlNfc_i2c_close
**
** Description Closes PN54X device
**
** Parameters pDevHandle - device handle
**
** Returns None
**
*******************************************************************************/
void phTmlNfc_i2c_close(void* pDevHandle) {
if (NULL != pDevHandle) {
close((intptr_t)pDevHandle);
}
return;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_open_and_configure
**
** Description Open and configure PN54X device
**
** Parameters pConfig - hardware information
** pLinkHandle - device handle
**
** Returns NFC status:
** NFCSTATUS_SUCCESS - open_and_configure operation success
** NFCSTATUS_INVALID_DEVICE - device open operation failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_i2c_open_and_configure(pphTmlNfc_Config_t pConfig,
void** pLinkHandle) {
int nHandle;
NXPLOG_TML_D("Opening port=%s\n", pConfig->pDevName);
/* open port */
nHandle = open((const char*)pConfig->pDevName, O_RDWR);
if (nHandle < 0) {
NXPLOG_TML_E("_i2c_open() Failed: retval %x", nHandle);
*pLinkHandle = NULL;
return NFCSTATUS_INVALID_DEVICE;
}
*pLinkHandle = (void*)((intptr_t)nHandle);
/*Reset PN54X*/
phTmlNfc_i2c_reset((void*)((intptr_t)nHandle), 0);
usleep(10 * 1000);
phTmlNfc_i2c_reset((void*)((intptr_t)nHandle), 1);
return NFCSTATUS_SUCCESS;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_read
**
** Description Reads requested number of bytes from PN54X device into given
** buffer
**
** Parameters pDevHandle - valid device handle
** pBuffer - buffer for read data
** nNbBytesToRead - number of bytes requested to be read
**
** Returns numRead - number of successfully read bytes
** -1 - read operation failure
**
*******************************************************************************/
int phTmlNfc_i2c_read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead) {
int ret_Read;
int ret_Select;
int numRead = 0;
struct timeval tv;
fd_set rfds;
uint16_t totalBtyesToRead = 0;
UNUSED(nNbBytesToRead);
if (NULL == pDevHandle) {
return -1;
}
if (false == bFwDnldFlag) {
totalBtyesToRead = NORMAL_MODE_HEADER_LEN;
} else {
totalBtyesToRead = FW_DNLD_HEADER_LEN;
}
/* Read with 2 second timeout, so that the read thread can be aborted
when the PN54X does not respond and we need to switch to FW download
mode. This should be done via a control socket instead. */
FD_ZERO(&rfds);
FD_SET((intptr_t)pDevHandle, &rfds);
tv.tv_sec = 2;
tv.tv_usec = 1;
ret_Select =
select((int)((intptr_t)pDevHandle + (int)1), &rfds, NULL, NULL, &tv);
if (ret_Select < 0) {
NXPLOG_TML_D("i2c select() errno : %x", errno);
return -1;
} else if (ret_Select == 0) {
NXPLOG_TML_D("i2c select() Timeout");
return -1;
} else {
ret_Read = read((intptr_t)pDevHandle, pBuffer, totalBtyesToRead - numRead);
if (ret_Read > 0) {
numRead += ret_Read;
} else if (ret_Read == 0) {
NXPLOG_TML_E("_i2c_read() [hdr]EOF");
return -1;
} else {
NXPLOG_TML_E("_i2c_read() [hdr] errno : %x", errno);
return -1;
}
if (false == bFwDnldFlag) {
totalBtyesToRead = NORMAL_MODE_HEADER_LEN;
} else {
totalBtyesToRead = FW_DNLD_HEADER_LEN;
}
if (numRead < totalBtyesToRead) {
ret_Read =
read((intptr_t)pDevHandle, pBuffer, totalBtyesToRead - numRead);
if (ret_Read != totalBtyesToRead - numRead) {
NXPLOG_TML_E("_i2c_read() [hdr] errno : %x", errno);
return -1;
} else {
numRead += ret_Read;
}
}
if (true == bFwDnldFlag) {
totalBtyesToRead =
pBuffer[FW_DNLD_LEN_OFFSET] + FW_DNLD_HEADER_LEN + CRC_LEN;
} else {
totalBtyesToRead =
pBuffer[NORMAL_MODE_LEN_OFFSET] + NORMAL_MODE_HEADER_LEN;
}
if ((totalBtyesToRead - numRead) != 0) {
ret_Read = read((intptr_t)pDevHandle, (pBuffer + numRead),
totalBtyesToRead - numRead);
if (ret_Read > 0) {
numRead += ret_Read;
} else if (ret_Read == 0) {
NXPLOG_TML_E("_i2c_read() [pyld] EOF");
return -1;
} else {
if (false == bFwDnldFlag) {
NXPLOG_TML_D("_i2c_read() [hdr] received");
phNxpNciHal_print_packet("RECV", pBuffer, NORMAL_MODE_HEADER_LEN);
}
NXPLOG_TML_E("_i2c_read() [pyld] errno : %x", errno);
return -1;
}
} else {
NXPLOG_TML_E("_>>>>> Empty packet recieved !!");
}
}
return numRead;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_write
**
** Description Writes requested number of bytes from given buffer into
** PN54X device
**
** Parameters pDevHandle - valid device handle
** pBuffer - buffer for read data
** nNbBytesToWrite - number of bytes requested to be written
**
** Returns numWrote - number of successfully written bytes
** -1 - write operation failure
**
*******************************************************************************/
int phTmlNfc_i2c_write(void* pDevHandle, uint8_t* pBuffer,
int nNbBytesToWrite) {
int ret;
int numWrote = 0;
int numBytes = nNbBytesToWrite;
if (NULL == pDevHandle) {
return -1;
}
if (fragmentation_enabled == I2C_FRAGMENATATION_DISABLED &&
nNbBytesToWrite > FRAGMENTSIZE_MAX) {
NXPLOG_TML_D(
"i2c_write() data larger than maximum I2C size,enable I2C "
"fragmentation");
return -1;
}
while (numWrote < nNbBytesToWrite) {
if (fragmentation_enabled == I2C_FRAGMENTATION_ENABLED &&
nNbBytesToWrite > FRAGMENTSIZE_MAX) {
if (nNbBytesToWrite - numWrote > FRAGMENTSIZE_MAX) {
numBytes = numWrote + FRAGMENTSIZE_MAX;
} else {
numBytes = nNbBytesToWrite;
}
}
ret = write((intptr_t)pDevHandle, pBuffer + numWrote, numBytes - numWrote);
if (ret > 0) {
numWrote += ret;
if (fragmentation_enabled == I2C_FRAGMENTATION_ENABLED &&
numWrote < nNbBytesToWrite) {
usleep(500);
}
} else if (ret == 0) {
NXPLOG_TML_D("_i2c_write() EOF");
return -1;
} else {
NXPLOG_TML_D("_i2c_write() errno : %x", errno);
if (errno == EINTR || errno == EAGAIN) {
continue;
}
return -1;
}
}
return numWrote;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_reset
**
** Description Reset PN54X device, using VEN pin
**
** Parameters pDevHandle - valid device handle
** level - reset level
**
** Returns 0 - reset operation success
** -1 - reset operation failure
**
*******************************************************************************/
int phTmlNfc_i2c_reset(void* pDevHandle, long level) {
int ret = -1;
NXPLOG_TML_D("phTmlNfc_i2c_reset(), VEN level %ld", level);
if (NULL == pDevHandle) {
return -1;
}
ret = ioctl((intptr_t)pDevHandle, PN544_SET_PWR, level);
if (ret < 0) {
NXPLOG_TML_E("%s :failed errno = 0x%x", __func__, errno);
if (level == 2 && errno == EBUSY) {
notifyFwrequest = true;
} else {
notifyFwrequest = false;
}
}
if (level == 2 && ret == 0) {
bFwDnldFlag = true;
} else {
bFwDnldFlag = false;
}
return ret;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_set_Jcop_dwnld_state
**
** Description This function set jcop download state.
**
** Parameters pDevHandle - valid device handle
**
** Returns NFCSTATUS
**
**
*******************************************************************************/
NFCSTATUS phTmlNfc_i2c_set_Jcop_dwnld_state(void* pDevHandle, long level) {
NXPLOG_TML_D("phTmlNfc_i2c_set_Jcop_dwnld_state()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
int32_t ret = -1;
NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
NXPLOG_TML_D("phTmlNfc_i2c_set_Jcop_dwnld_state(), level %ld", level);
if (NULL == pDevHandle) return NFCSTATUS_FAILED;
ret =
ioctl((intptr_t)pDevHandle, PN544_SET_DWNLD_STATUS, (unsigned long)level);
if (ret < 0) {
NXPLOG_TML_E("%s : failed errno = 0x%x", __func__, errno);
if (errno == -EBUSY) {
wStatus = NFCSTATUS_BUSY;
} else if (errno == -EBADRQC) {
wStatus = NFCSTATUS_INVALID_FORMAT;
} else if (errno == -EINVAL) {
wStatus = NFCSTATUS_INVALID_PARAMETER;
} else {
wStatus = NFCSTATUS_FAILED;
}
}
return wStatus;
}
/*******************************************************************************
**
** Function phTmlNfc_set_pid
**
** Description
**
** Parameters pDevHandle - valid device handle
** pid - nfc service pid
**
** Returns p61_access_state_t - get_p61_power operation success
** P61_STATE_INVALID - get_p61_power operation failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_set_pid(void* pDevHandle, long pid) {
NXPLOG_TML_D("phTmlNfc_set_pid()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
NFCSTATUS wStatus = NFCSTATUS_FAILED;
uint32_t ret = -1;
NXPLOG_TML_D("phTmlNfc_set_pid(), pid %ld", pid);
if (NULL == pDevHandle) {
return NFCSTATUS_FAILED;
}
ret = (uint32_t)ioctl((intptr_t)pDevHandle, P544_SET_NFC_SERVICE_PID, pid);
if (!ret) wStatus = NFCSTATUS_SUCCESS;
return wStatus;
}
/*******************************************************************************
**
** Function phTmlNfc_set_power_scheme
**
** Description This function sets the eSE power scheme in pn544 driver
**
** Parameters pDevHandle - valid device handle
** pid - nfc service pid
**
** Returns p61_access_state_t - get_p61_power operation success
** P61_STATE_INVALID - get_p61_power operation failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_set_power_scheme(void* pDevHandle, long id) {
NXPLOG_TML_D("phTmlNfc_set_power_scheme()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
int ret;
NXPLOG_TML_D("phTmlNfc_set_power_scheme(), id %ld", id);
if (NULL == pDevHandle) {
return NFCSTATUS_FAILED;
}
ret = ioctl((intptr_t)pDevHandle, P544_SET_POWER_SCHEME, id);
return ret;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_set_p61_power_state
**
** Description
**
** Parameters pDevHandle - valid device handle
**
** Returns p61_access_state_t - get_p61_power operation success
** P61_STATE_INVALID - get_p61_power operation failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_i2c_set_p61_power_state(void* pDevHandle, long level) {
NXPLOG_TML_D("phTmlNfc_i2c_set_p61_power_state()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
int ret = -1;
NFCSTATUS wStatus = NFCSTATUS_FAILED;
NXPLOG_TML_D("phTmlNfc_i2c_set_p61_power_state(), level %ld", level);
if (NULL == pDevHandle) {
return -1;
}
ret = ioctl((intptr_t)pDevHandle, P61_SET_WIRED_ACCESS, (unsigned long)level);
if (ret < 0) {
NXPLOG_TML_E("%s : failed errno = 0x%x", __func__, errno);
if (errno == -EBUSY) {
wStatus = NFCSTATUS_BUSY;
} else if (errno == -EPERM) {
wStatus = NFCSTATUS_NOT_ALLOWED;
} else if (errno == -EBADRQC) {
wStatus = NFCSTATUS_INVALID_PARAMETER;
} else {
wStatus = NFCSTATUS_FAILED;
}
} else {
wStatus = NFCSTATUS_SUCCESS;
}
return wStatus;
}
/*******************************************************************************
**
** Function phTmlNfc_i2c_get_p61_power_state
**
** Description
**
** Parameters pDevHandle - valid device handle
**
** Returns get_p61_power operation success
** NFCSTATUS_FAILED - get_p61_power operation failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_i2c_get_p61_power_state(void* pDevHandle) {
NXPLOG_TML_D("phTmlNfc_i2c_get_p61_power_state()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
int ret;
NFCSTATUS wStatus = NFCSTATUS_FAILED;
p61_access_state_t p61_current_state = P61_STATE_INVALID;
NXPLOG_TML_D("phTmlNfc_i2c_get_p61_power_mode()");
if (NULL == pDevHandle) {
return -1;
}
ret = ioctl((intptr_t)pDevHandle, P61_GET_PWR_STATUS,
(unsigned long)&p61_current_state);
if (ret < 0) {
NXPLOG_TML_E("%s : failed errno = 0x%x", __func__, errno);
p61_current_state = P61_STATE_INVALID;
}
wStatus = p61_current_state;
return wStatus;
}
/*******************************************************************************
**
** Function phTmlNfc_get_ese_access
**
** Description
**
** Parameters pDevHandle - valid device handle
** timeout - timeout to wait for ese access
**
** Returns success or failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_get_ese_access(void* pDevHandle, long timeout) {
NXPLOG_TML_D("phTmlNfc_get_ese_access()");
if(!nfcFL.nfcNxpEse) {
NXPLOG_TML_D("NxpEse not available. Returning");
return NFCSTATUS_FAILED;
}
int ret = -1;
NFCSTATUS status = NFCSTATUS_SUCCESS;
NXPLOG_TML_D("phTmlNfc_get_ese_access(), enter timeout %ld", timeout);
if (NULL == pDevHandle) {
return NFCSTATUS_FAILED;
}
ret = ioctl((intptr_t)pDevHandle, P544_GET_ESE_ACCESS, timeout);
if (ret < 0) {
if (ret == -EBUSY)
status = NFCSTATUS_BUSY;
else
status = NFCSTATUS_FAILED;
}
NXPLOG_TML_D("phTmlNfc_get_ese_access(), exit ret %d, status %d", ret,
status);
return status;
}
/*******************************************************************************
**
** Function phTmlNfc_rel_svdd_wait
**
** Description
**
** Parameters pDevHandle - valid device handle
**
** Returns success or failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_rel_svdd_wait(void *pDevHandle, long svddWaitStatus) {
NXPLOG_TML_D("phTmlNfc_rel_svdd_wait()");
if (!nfcFL.nfcNxpEse && !nfcFL.eseFL._ESE_SVDD_SYNC) {
NXPLOG_TML_D("NxpEse and ESE_SVDD_SYNC not available. Returning");
return NFCSTATUS_FAILED;
}
int ret = -1;
NFCSTATUS status = NFCSTATUS_SUCCESS;
NXPLOG_TML_D("phTmlNfc_rel_svdd_wait(), enter ");
if (NULL == pDevHandle) {
return NFCSTATUS_FAILED;
}
ret = ioctl((intptr_t)pDevHandle, P544_REL_SVDD_WAIT, svddWaitStatus);
if (ret < 0) {
if (ret == -EBUSY)
status = NFCSTATUS_BUSY;
else
status = NFCSTATUS_FAILED;
}
NXPLOG_TML_D("phTmlNfc_rel_svdd_wait(), exit ret %d, status %d", ret,
status);
return status;
}
/*******************************************************************************
**
** Function getDownloadFlag
**
** Description Returns the current mode
**
** Parameters none
**
** Returns Current mode download/NCI
*******************************************************************************/
bool_t getDownloadFlag(void) { return bFwDnldFlag; }
/*******************************************************************************
**
** Function phTmlNfc_rel_dwpOnOff_wait
**
** Description
**
** Parameters pDevHandle - valid device handle
**
** Returns success or failure
**
*******************************************************************************/
NFCSTATUS phTmlNfc_rel_dwpOnOff_wait(void *pDevHandle, long dwplinkActvStatus) {
int ret = -1;
NFCSTATUS status = NFCSTATUS_SUCCESS;
NXPLOG_TML_D("phTmlNfc_rel_dwpOnOff_wait(), enter ");
if (NULL == pDevHandle) {
return NFCSTATUS_FAILED;
}
ret = ioctl((intptr_t)pDevHandle, P544_REL_DWPONOFF_WAIT, dwplinkActvStatus);
if (ret < 0) {
if (ret == -EBUSY)
status = NFCSTATUS_BUSY;
else
status = NFCSTATUS_FAILED;
}
NXPLOG_TML_D("phTmlNfc_rel_dwpOnOff_wait(), exit ret %d, status %d", ret,
status);
return status;
}
|