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