blob: 97addfa96c6ff38c088a66e6d303113bc7ca086a [file] [log] [blame]
Christophe Ricarded06aeef2015-06-09 22:26:05 +02001/*
2 * Secure Element driver for STMicroelectronics NFC NCI chip
3 *
4 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/module.h>
20#include <linux/nfc.h>
21#include <linux/delay.h>
22#include <net/nfc/nci.h>
23#include <net/nfc/nci_core.h>
24
25#include "st-nci.h"
26#include "st-nci_se.h"
27
28struct st_nci_pipe_info {
29 u8 pipe_state;
30 u8 src_host_id;
31 u8 src_gate_id;
32 u8 dst_host_id;
33 u8 dst_gate_id;
34} __packed;
35
36/* Hosts */
37#define ST_NCI_HOST_CONTROLLER_ID 0x00
38#define ST_NCI_TERMINAL_HOST_ID 0x01
39#define ST_NCI_UICC_HOST_ID 0x02
40#define ST_NCI_ESE_HOST_ID 0xc0
41
42/* Gates */
43#define ST_NCI_DEVICE_MGNT_GATE 0x01
44#define ST_NCI_APDU_READER_GATE 0xf0
45#define ST_NCI_CONNECTIVITY_GATE 0x41
46
47/* Pipes */
48#define ST_NCI_DEVICE_MGNT_PIPE 0x02
49
50/* Connectivity pipe only */
51#define ST_NCI_SE_COUNT_PIPE_UICC 0x01
52/* Connectivity + APDU Reader pipe */
53#define ST_NCI_SE_COUNT_PIPE_EMBEDDED 0x02
54
55#define ST_NCI_SE_TO_HOT_PLUG 1000 /* msecs */
56#define ST_NCI_SE_TO_PIPES 2000
57
58#define ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
59
60#define NCI_HCI_APDU_PARAM_ATR 0x01
61#define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
62#define NCI_HCI_ADMIN_PARAM_WHITELIST 0x03
63#define NCI_HCI_ADMIN_PARAM_HOST_LIST 0x04
64
65#define ST_NCI_EVT_SE_HARD_RESET 0x20
66#define ST_NCI_EVT_TRANSMIT_DATA 0x10
67#define ST_NCI_EVT_WTX_REQUEST 0x11
68#define ST_NCI_EVT_SE_SOFT_RESET 0x11
69#define ST_NCI_EVT_SE_END_OF_APDU_TRANSFER 0x21
70#define ST_NCI_EVT_HOT_PLUG 0x03
71
72#define ST_NCI_SE_MODE_OFF 0x00
73#define ST_NCI_SE_MODE_ON 0x01
74
75#define ST_NCI_EVT_CONNECTIVITY 0x10
76#define ST_NCI_EVT_TRANSACTION 0x12
77
78#define ST_NCI_DM_GETINFO 0x13
79#define ST_NCI_DM_GETINFO_PIPE_LIST 0x02
80#define ST_NCI_DM_GETINFO_PIPE_INFO 0x01
81#define ST_NCI_DM_PIPE_CREATED 0x02
82#define ST_NCI_DM_PIPE_OPEN 0x04
83#define ST_NCI_DM_RF_ACTIVE 0x80
84#define ST_NCI_DM_DISCONNECT 0x30
85
86#define ST_NCI_DM_IS_PIPE_OPEN(p) \
87 ((p & 0x0f) == (ST_NCI_DM_PIPE_CREATED | ST_NCI_DM_PIPE_OPEN))
88
89#define ST_NCI_ATR_DEFAULT_BWI 0x04
90
91/*
92 * WT = 2^BWI/10[s], convert into msecs and add a secure
93 * room by increasing by 2 this timeout
94 */
95#define ST_NCI_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
96#define ST_NCI_ATR_GET_Y_FROM_TD(x) (x >> 4)
97
98/* If TA is present bit 0 is set */
99#define ST_NCI_ATR_TA_PRESENT(x) (x & 0x01)
100/* If TB is present bit 1 is set */
101#define ST_NCI_ATR_TB_PRESENT(x) (x & 0x02)
102
103#define ST_NCI_NUM_DEVICES 256
104
105static DECLARE_BITMAP(dev_mask, ST_NCI_NUM_DEVICES);
106
107/* Here are the mandatory pipe for st_nci */
108static struct nci_hci_gate st_nci_gates[] = {
109 {NCI_HCI_ADMIN_GATE, NCI_HCI_ADMIN_PIPE,
110 ST_NCI_HOST_CONTROLLER_ID},
111 {NCI_HCI_LINK_MGMT_GATE, NCI_HCI_LINK_MGMT_PIPE,
112 ST_NCI_HOST_CONTROLLER_ID},
113 {ST_NCI_DEVICE_MGNT_GATE, ST_NCI_DEVICE_MGNT_PIPE,
114 ST_NCI_HOST_CONTROLLER_ID},
115
116 /* Secure element pipes are created by secure element host */
117 {ST_NCI_CONNECTIVITY_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
118 ST_NCI_HOST_CONTROLLER_ID},
119 {ST_NCI_APDU_READER_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
120 ST_NCI_HOST_CONTROLLER_ID},
121};
122
123static u8 st_nci_se_get_bwi(struct nci_dev *ndev)
124{
125 int i;
126 u8 td;
127 struct st_nci_info *info = nci_get_drvdata(ndev);
128
129 /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
130 for (i = 1; i < ST_NCI_ESE_MAX_LENGTH; i++) {
131 td = ST_NCI_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
132 if (ST_NCI_ATR_TA_PRESENT(td))
133 i++;
134 if (ST_NCI_ATR_TB_PRESENT(td)) {
135 i++;
136 return info->se_info.atr[i] >> 4;
137 }
138 }
139 return ST_NCI_ATR_DEFAULT_BWI;
140}
141
142static void st_nci_se_get_atr(struct nci_dev *ndev)
143{
144 struct st_nci_info *info = nci_get_drvdata(ndev);
145 int r;
146 struct sk_buff *skb;
147
148 r = nci_hci_get_param(ndev, ST_NCI_APDU_READER_GATE,
149 NCI_HCI_APDU_PARAM_ATR, &skb);
150 if (r < 0)
151 return;
152
153 if (skb->len <= ST_NCI_ESE_MAX_LENGTH) {
154 memcpy(info->se_info.atr, skb->data, skb->len);
155
156 info->se_info.wt_timeout =
157 ST_NCI_BWI_TO_TIMEOUT(st_nci_se_get_bwi(ndev));
158 }
159 kfree_skb(skb);
160}
161
162int st_nci_hci_load_session(struct nci_dev *ndev)
163{
164 int i, j, r;
165 struct sk_buff *skb_pipe_list, *skb_pipe_info;
166 struct st_nci_pipe_info *dm_pipe_info;
167 u8 pipe_list[] = { ST_NCI_DM_GETINFO_PIPE_LIST,
168 ST_NCI_TERMINAL_HOST_ID};
169 u8 pipe_info[] = { ST_NCI_DM_GETINFO_PIPE_INFO,
170 ST_NCI_TERMINAL_HOST_ID, 0};
171
172 /* On ST_NCI device pipes number are dynamics
173 * If pipes are already created, hci_dev_up will fail.
174 * Doing a clear all pipe is a bad idea because:
175 * - It does useless EEPROM cycling
176 * - It might cause issue for secure elements support
177 * (such as removing connectivity or APDU reader pipe)
178 * A better approach on ST_NCI is to:
179 * - get a pipe list for each host.
180 * (eg: ST_NCI_HOST_CONTROLLER_ID for now).
181 * (TODO Later on UICC HOST and eSE HOST)
182 * - get pipe information
183 * - match retrieved pipe list in st_nci_gates
184 * ST_NCI_DEVICE_MGNT_GATE is a proprietary gate
185 * with ST_NCI_DEVICE_MGNT_PIPE.
186 * Pipe can be closed and need to be open.
187 */
188 r = nci_hci_connect_gate(ndev, ST_NCI_HOST_CONTROLLER_ID,
189 ST_NCI_DEVICE_MGNT_GATE,
190 ST_NCI_DEVICE_MGNT_PIPE);
191 if (r < 0)
192 goto free_info;
193
194 /* Get pipe list */
195 r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE,
196 ST_NCI_DM_GETINFO, pipe_list, sizeof(pipe_list),
197 &skb_pipe_list);
198 if (r < 0)
199 goto free_info;
200
201 /* Complete the existing gate_pipe table */
202 for (i = 0; i < skb_pipe_list->len; i++) {
203 pipe_info[2] = skb_pipe_list->data[i];
204 r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE,
205 ST_NCI_DM_GETINFO, pipe_info,
206 sizeof(pipe_info), &skb_pipe_info);
207
208 if (r)
209 continue;
210
211 /*
212 * Match pipe ID and gate ID
213 * Output format from ST21NFC_DM_GETINFO is:
214 * - pipe state (1byte)
215 * - source hid (1byte)
216 * - source gid (1byte)
217 * - destination hid (1byte)
218 * - destination gid (1byte)
219 */
220 dm_pipe_info = (struct st_nci_pipe_info *)skb_pipe_info->data;
221 if (dm_pipe_info->dst_gate_id == ST_NCI_APDU_READER_GATE &&
222 dm_pipe_info->src_host_id != ST_NCI_ESE_HOST_ID) {
223 pr_err("Unexpected apdu_reader pipe on host %x\n",
224 dm_pipe_info->src_host_id);
225 continue;
226 }
227
228 for (j = 0; (j < ARRAY_SIZE(st_nci_gates)) &&
229 (st_nci_gates[j].gate != dm_pipe_info->dst_gate_id); j++)
230 ;
231
232 if (j < ARRAY_SIZE(st_nci_gates) &&
233 st_nci_gates[j].gate == dm_pipe_info->dst_gate_id &&
234 ST_NCI_DM_IS_PIPE_OPEN(dm_pipe_info->pipe_state)) {
235 st_nci_gates[j].pipe = pipe_info[2];
236
237 ndev->hci_dev->gate2pipe[st_nci_gates[j].gate] =
238 st_nci_gates[j].pipe;
239 ndev->hci_dev->pipes[st_nci_gates[j].pipe].gate =
240 st_nci_gates[j].gate;
241 ndev->hci_dev->pipes[st_nci_gates[j].pipe].host =
242 dm_pipe_info->src_host_id;
243 }
244 }
245
246 memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
247 sizeof(st_nci_gates));
248
249free_info:
250 kfree_skb(skb_pipe_info);
251 kfree_skb(skb_pipe_list);
252 return r;
253}
254EXPORT_SYMBOL_GPL(st_nci_hci_load_session);
255
256static void st_nci_hci_admin_event_received(struct nci_dev *ndev,
257 u8 event, struct sk_buff *skb)
258{
259 struct st_nci_info *info = nci_get_drvdata(ndev);
260
261 switch (event) {
262 case ST_NCI_EVT_HOT_PLUG:
263 if (info->se_info.se_active) {
264 if (!ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
265 del_timer_sync(&info->se_info.se_active_timer);
266 info->se_info.se_active = false;
267 complete(&info->se_info.req_completion);
268 } else {
269 mod_timer(&info->se_info.se_active_timer,
270 jiffies +
271 msecs_to_jiffies(ST_NCI_SE_TO_PIPES));
272 }
273 }
274 break;
275 }
276}
277
278static int st_nci_hci_apdu_reader_event_received(struct nci_dev *ndev,
279 u8 event,
280 struct sk_buff *skb)
281{
282 int r = 0;
283 struct st_nci_info *info = nci_get_drvdata(ndev);
284
285 pr_debug("apdu reader gate event: %x\n", event);
286
287 switch (event) {
288 case ST_NCI_EVT_TRANSMIT_DATA:
289 del_timer_sync(&info->se_info.bwi_timer);
290 info->se_info.bwi_active = false;
291 info->se_info.cb(info->se_info.cb_context,
292 skb->data, skb->len, 0);
293 break;
294 case ST_NCI_EVT_WTX_REQUEST:
295 mod_timer(&info->se_info.bwi_timer, jiffies +
296 msecs_to_jiffies(info->se_info.wt_timeout));
297 break;
298 }
299
300 kfree_skb(skb);
301 return r;
302}
303
304/*
305 * Returns:
306 * <= 0: driver handled the event, skb consumed
307 * 1: driver does not handle the event, please do standard processing
308 */
309static int st_nci_hci_connectivity_event_received(struct nci_dev *ndev,
310 u8 host, u8 event,
311 struct sk_buff *skb)
312{
313 int r = 0;
314 struct device *dev = &ndev->nfc_dev->dev;
315 struct nfc_evt_transaction *transaction;
316
317 pr_debug("connectivity gate event: %x\n", event);
318
319 switch (event) {
320 case ST_NCI_EVT_CONNECTIVITY:
321
322 break;
323 case ST_NCI_EVT_TRANSACTION:
324 /* According to specification etsi 102 622
325 * 11.2.2.4 EVT_TRANSACTION Table 52
326 * Description Tag Length
327 * AID 81 5 to 16
328 * PARAMETERS 82 0 to 255
329 */
330 if (skb->len < NFC_MIN_AID_LENGTH + 2 &&
331 skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
332 return -EPROTO;
333
334 transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
335 skb->len - 2, GFP_KERNEL);
336
337 transaction->aid_len = skb->data[1];
338 memcpy(transaction->aid, &skb->data[2], transaction->aid_len);
339
340 /* Check next byte is PARAMETERS tag (82) */
341 if (skb->data[transaction->aid_len + 2] !=
342 NFC_EVT_TRANSACTION_PARAMS_TAG)
343 return -EPROTO;
344
345 transaction->params_len = skb->data[transaction->aid_len + 3];
346 memcpy(transaction->params, skb->data +
347 transaction->aid_len + 4, transaction->params_len);
348
349 r = nfc_se_transaction(ndev->nfc_dev, host, transaction);
350 break;
351 default:
352 return 1;
353 }
354 kfree_skb(skb);
355 return r;
356}
357
358void st_nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
359 u8 event, struct sk_buff *skb)
360{
361 u8 gate = ndev->hci_dev->pipes[pipe].gate;
362 u8 host = ndev->hci_dev->pipes[pipe].host;
363
364 switch (gate) {
365 case NCI_HCI_ADMIN_GATE:
366 st_nci_hci_admin_event_received(ndev, event, skb);
367 break;
368 case ST_NCI_APDU_READER_GATE:
369 st_nci_hci_apdu_reader_event_received(ndev, event, skb);
370 break;
371 case ST_NCI_CONNECTIVITY_GATE:
372 st_nci_hci_connectivity_event_received(ndev, host, event,
373 skb);
374 break;
375 }
376}
377EXPORT_SYMBOL_GPL(st_nci_hci_event_received);
378
379
380void st_nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
381 struct sk_buff *skb)
382{
383 struct st_nci_info *info = nci_get_drvdata(ndev);
384 u8 gate = ndev->hci_dev->pipes[pipe].gate;
385
386 pr_debug("cmd: %x\n", cmd);
387
388 switch (cmd) {
389 case NCI_HCI_ANY_OPEN_PIPE:
390 if (gate != ST_NCI_APDU_READER_GATE &&
391 ndev->hci_dev->pipes[pipe].host != ST_NCI_UICC_HOST_ID)
392 ndev->hci_dev->count_pipes++;
393
394 if (ndev->hci_dev->count_pipes ==
395 ndev->hci_dev->expected_pipes) {
396 del_timer_sync(&info->se_info.se_active_timer);
397 info->se_info.se_active = false;
398 ndev->hci_dev->count_pipes = 0;
399 complete(&info->se_info.req_completion);
400 }
401 break;
402 }
403}
404EXPORT_SYMBOL_GPL(st_nci_hci_cmd_received);
405
406/*
407 * Remarks: On some early st_nci firmware, nci_nfcee_mode_set(0)
408 * is rejected
409 */
410static int st_nci_control_se(struct nci_dev *ndev, u8 se_idx,
411 u8 state)
412{
413 struct st_nci_info *info = nci_get_drvdata(ndev);
414 int r;
415 struct sk_buff *sk_host_list;
416 u8 host_id;
417
418 switch (se_idx) {
419 case ST_NCI_UICC_HOST_ID:
420 ndev->hci_dev->count_pipes = 0;
421 ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_UICC;
422 break;
423 case ST_NCI_ESE_HOST_ID:
424 ndev->hci_dev->count_pipes = 0;
425 ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_EMBEDDED;
426 break;
427 default:
428 return -EINVAL;
429 }
430
431 /*
432 * Wait for an EVT_HOT_PLUG in order to
433 * retrieve a relevant host list.
434 */
435 reinit_completion(&info->se_info.req_completion);
436 r = nci_nfcee_mode_set(ndev, se_idx, NCI_NFCEE_ENABLE);
437 if (r != NCI_STATUS_OK)
438 return r;
439
440 mod_timer(&info->se_info.se_active_timer, jiffies +
441 msecs_to_jiffies(ST_NCI_SE_TO_HOT_PLUG));
442 info->se_info.se_active = true;
443
444 /* Ignore return value and check in any case the host_list */
445 wait_for_completion_interruptible(&info->se_info.req_completion);
446
447 /* There might be some "collision" after receiving a HOT_PLUG event
448 * This may cause the CLF to not answer to the next hci command.
449 * There is no possible synchronization to prevent this.
450 * Adding a small delay is the only way to solve the issue.
451 */
452 usleep_range(3000, 5000);
453
454 r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
455 NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list);
456 if (r != NCI_HCI_ANY_OK)
457 return r;
458
459 host_id = sk_host_list->data[sk_host_list->len - 1];
460 kfree_skb(sk_host_list);
461 if (state == ST_NCI_SE_MODE_ON && host_id == se_idx)
462 return se_idx;
463 else if (state == ST_NCI_SE_MODE_OFF && host_id != se_idx)
464 return se_idx;
465
466 return -1;
467}
468
469int st_nci_disable_se(struct nci_dev *ndev, u32 se_idx)
470{
471 int r;
472
473 pr_debug("st_nci_disable_se\n");
474
475 if (se_idx == NFC_SE_EMBEDDED) {
476 r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
477 ST_NCI_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
478 if (r < 0)
479 return r;
480 }
481
482 return 0;
483}
484EXPORT_SYMBOL_GPL(st_nci_disable_se);
485
486int st_nci_enable_se(struct nci_dev *ndev, u32 se_idx)
487{
488 int r;
489
490 pr_debug("st_nci_enable_se\n");
491
492 if (se_idx == ST_NCI_HCI_HOST_ID_ESE) {
493 r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
494 ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
495 if (r < 0)
496 return r;
497 }
498
499 return 0;
500}
501EXPORT_SYMBOL_GPL(st_nci_enable_se);
502
503static int st_nci_hci_network_init(struct nci_dev *ndev)
504{
505 struct core_conn_create_dest_spec_params *dest_params;
506 struct dest_spec_params spec_params;
507 struct nci_conn_info *conn_info;
508 int r, dev_num;
509
510 dest_params =
511 kzalloc(sizeof(struct core_conn_create_dest_spec_params) +
512 sizeof(struct dest_spec_params), GFP_KERNEL);
513 if (dest_params == NULL) {
514 r = -ENOMEM;
515 goto exit;
516 }
517
518 dest_params->type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE;
519 dest_params->length = sizeof(struct dest_spec_params);
520 spec_params.id = ndev->hci_dev->nfcee_id;
521 spec_params.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS;
522 memcpy(dest_params->value, &spec_params,
523 sizeof(struct dest_spec_params));
524 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCEE, 1,
525 sizeof(struct core_conn_create_dest_spec_params) +
526 sizeof(struct dest_spec_params),
527 dest_params);
528 if (r != NCI_STATUS_OK)
529 goto free_dest_params;
530
531 conn_info = ndev->hci_dev->conn_info;
532 if (!conn_info)
533 goto free_dest_params;
534
535 memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
536 sizeof(st_nci_gates));
537
538 /*
539 * Session id must include the driver name + i2c bus addr
540 * persistent info to discriminate 2 identical chips
541 */
542 dev_num = find_first_zero_bit(dev_mask, ST_NCI_NUM_DEVICES);
543 if (dev_num >= ST_NCI_NUM_DEVICES) {
544 r = -ENODEV;
545 goto free_dest_params;
546 }
547
548 scnprintf(ndev->hci_dev->init_data.session_id,
549 sizeof(ndev->hci_dev->init_data.session_id),
550 "%s%2x", "ST21BH", dev_num);
551
552 r = nci_hci_dev_session_init(ndev);
553 if (r != NCI_HCI_ANY_OK)
554 goto free_dest_params;
555
556 r = nci_nfcee_mode_set(ndev, ndev->hci_dev->conn_info->id,
557 NCI_NFCEE_ENABLE);
558 if (r != NCI_STATUS_OK)
559 goto free_dest_params;
560
561free_dest_params:
562 kfree(dest_params);
563
564exit:
565 return r;
566}
567
568int st_nci_discover_se(struct nci_dev *ndev)
569{
570 u8 param[2];
571 int r;
572 int se_count = 0;
573
574 pr_debug("st_nci_discover_se\n");
575
576 r = st_nci_hci_network_init(ndev);
577 if (r != 0)
578 return r;
579
580 param[0] = ST_NCI_UICC_HOST_ID;
581 param[1] = ST_NCI_HCI_HOST_ID_ESE;
582 r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
583 NCI_HCI_ADMIN_PARAM_WHITELIST,
584 param, sizeof(param));
585 if (r != NCI_HCI_ANY_OK)
586 return r;
587
588 r = st_nci_control_se(ndev, ST_NCI_UICC_HOST_ID,
589 ST_NCI_SE_MODE_ON);
590 if (r == ST_NCI_UICC_HOST_ID) {
591 nfc_add_se(ndev->nfc_dev, ST_NCI_UICC_HOST_ID, NFC_SE_UICC);
592 se_count++;
593 }
594
595 /* Try to enable eSE in order to check availability */
596 r = st_nci_control_se(ndev, ST_NCI_HCI_HOST_ID_ESE,
597 ST_NCI_SE_MODE_ON);
598 if (r == ST_NCI_HCI_HOST_ID_ESE) {
599 nfc_add_se(ndev->nfc_dev, ST_NCI_HCI_HOST_ID_ESE,
600 NFC_SE_EMBEDDED);
601 se_count++;
602 st_nci_se_get_atr(ndev);
603 }
604
605 return !se_count;
606}
607EXPORT_SYMBOL_GPL(st_nci_discover_se);
608
609int st_nci_se_io(struct nci_dev *ndev, u32 se_idx,
610 u8 *apdu, size_t apdu_length,
611 se_io_cb_t cb, void *cb_context)
612{
613 struct st_nci_info *info = nci_get_drvdata(ndev);
614
615 pr_debug("\n");
616
617 switch (se_idx) {
618 case ST_NCI_HCI_HOST_ID_ESE:
619 info->se_info.cb = cb;
620 info->se_info.cb_context = cb_context;
621 mod_timer(&info->se_info.bwi_timer, jiffies +
622 msecs_to_jiffies(info->se_info.wt_timeout));
623 info->se_info.bwi_active = true;
624 return nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
625 ST_NCI_EVT_TRANSMIT_DATA, apdu,
626 apdu_length);
627 default:
628 return -ENODEV;
629 }
630}
631EXPORT_SYMBOL(st_nci_se_io);
632
633static void st_nci_se_wt_timeout(unsigned long data)
634{
635 /*
636 * No answer from the secure element
637 * within the defined timeout.
638 * Let's send a reset request as recovery procedure.
639 * According to the situation, we first try to send a software reset
640 * to the secure element. If the next command is still not
641 * answering in time, we send to the CLF a secure element hardware
642 * reset request.
643 */
644 /* hardware reset managed through VCC_UICC_OUT power supply */
645 u8 param = 0x01;
646 struct st_nci_info *info = (struct st_nci_info *) data;
647
648 pr_debug("\n");
649
650 info->se_info.bwi_active = false;
651
652 if (!info->se_info.xch_error) {
653 info->se_info.xch_error = true;
654 nci_hci_send_event(info->ndlc->ndev, ST_NCI_APDU_READER_GATE,
655 ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
656 } else {
657 info->se_info.xch_error = false;
658 nci_hci_send_event(info->ndlc->ndev, ST_NCI_DEVICE_MGNT_GATE,
659 ST_NCI_EVT_SE_HARD_RESET, &param, 1);
660 }
661 info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
662}
663
664static void st_nci_se_activation_timeout(unsigned long data)
665{
666 struct st_nci_info *info = (struct st_nci_info *) data;
667
668 pr_debug("\n");
669
670 info->se_info.se_active = false;
671
672 complete(&info->se_info.req_completion);
673}
674
675int st_nci_se_init(struct nci_dev *ndev)
676{
677 struct st_nci_info *info = nci_get_drvdata(ndev);
678
679 init_completion(&info->se_info.req_completion);
680 /* initialize timers */
681 init_timer(&info->se_info.bwi_timer);
682 info->se_info.bwi_timer.data = (unsigned long)info;
683 info->se_info.bwi_timer.function = st_nci_se_wt_timeout;
684 info->se_info.bwi_active = false;
685
686 init_timer(&info->se_info.se_active_timer);
687 info->se_info.se_active_timer.data = (unsigned long)info;
688 info->se_info.se_active_timer.function =
689 st_nci_se_activation_timeout;
690 info->se_info.se_active = false;
691
692 info->se_info.xch_error = false;
693
694 info->se_info.wt_timeout =
695 ST_NCI_BWI_TO_TIMEOUT(ST_NCI_ATR_DEFAULT_BWI);
696
697 return 0;
698}
699EXPORT_SYMBOL(st_nci_se_init);
700
701void st_nci_se_deinit(struct nci_dev *ndev)
702{
703 struct st_nci_info *info = nci_get_drvdata(ndev);
704
705 if (info->se_info.bwi_active)
706 del_timer_sync(&info->se_info.bwi_timer);
707 if (info->se_info.se_active)
708 del_timer_sync(&info->se_info.se_active_timer);
709
710 info->se_info.se_active = false;
711 info->se_info.bwi_active = false;
712}
713EXPORT_SYMBOL(st_nci_se_deinit);
714