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