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