blob: 147e2d904c636956af89ba3bfb392540dbb96751 [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"
Christophe Ricarded06aeef2015-06-09 22:26:05 +020026
27struct 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 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +020042#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
103static DECLARE_BITMAP(dev_mask, ST_NCI_NUM_DEVICES);
104
105/* Here are the mandatory pipe for st_nci */
106static 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
Christophe Ricard7e357402015-10-25 22:54:33 +0100114 {NCI_HCI_IDENTITY_MGMT_GATE, NCI_HCI_INVALID_PIPE,
115 ST_NCI_HOST_CONTROLLER_ID},
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100116 {NCI_HCI_LOOPBACK_GATE, NCI_HCI_INVALID_PIPE,
117 ST_NCI_HOST_CONTROLLER_ID},
Christophe Ricard7e357402015-10-25 22:54:33 +0100118
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200119 /* 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
126static 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
145static 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
165int 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)
Christophe Ricarddaaf1e12015-08-14 22:33:34 +0200195 return r;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200196
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)
Christophe Ricarddaaf1e12015-08-14 22:33:34 +0200202 return r;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200203
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);
Christophe Ricarddaaf1e12015-08-14 22:33:34 +0200228 kfree_skb(skb_pipe_info);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200229 continue;
230 }
231
Christophe Ricardd3f13c52015-10-25 22:54:34 +0100232 for (j = 3; (j < ARRAY_SIZE(st_nci_gates)) &&
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200233 (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)) {
Christophe Ricard22c84c52015-10-25 22:54:30 +0100239 ndev->hci_dev->init_data.gates[j].pipe = pipe_info[2];
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200240
241 ndev->hci_dev->gate2pipe[st_nci_gates[j].gate] =
Christophe Ricard22c84c52015-10-25 22:54:30 +0100242 pipe_info[2];
243 ndev->hci_dev->pipes[pipe_info[2]].gate =
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200244 st_nci_gates[j].gate;
Christophe Ricard22c84c52015-10-25 22:54:30 +0100245 ndev->hci_dev->pipes[pipe_info[2]].host =
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200246 dm_pipe_info->src_host_id;
247 }
Christophe Ricarddaaf1e12015-08-14 22:33:34 +0200248 kfree_skb(skb_pipe_info);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200249 }
250
Christophe Ricard9dfe29f2015-10-25 22:54:28 +0100251 /*
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
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200259 kfree_skb(skb_pipe_list);
260 return r;
261}
262EXPORT_SYMBOL_GPL(st_nci_hci_load_session);
263
264static 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;
Christophe Ricard2b5dbe02015-10-25 22:54:37 +0100283 default:
284 nfc_err(&ndev->nfc_dev->dev, "Unexpected event on admin gate\n");
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200285 }
286}
287
288static int st_nci_hci_apdu_reader_event_received(struct nci_dev *ndev,
289 u8 event,
290 struct sk_buff *skb)
291{
292 int r = 0;
293 struct st_nci_info *info = nci_get_drvdata(ndev);
294
295 pr_debug("apdu reader gate event: %x\n", event);
296
297 switch (event) {
298 case ST_NCI_EVT_TRANSMIT_DATA:
299 del_timer_sync(&info->se_info.bwi_timer);
300 info->se_info.bwi_active = false;
301 info->se_info.cb(info->se_info.cb_context,
302 skb->data, skb->len, 0);
303 break;
304 case ST_NCI_EVT_WTX_REQUEST:
305 mod_timer(&info->se_info.bwi_timer, jiffies +
306 msecs_to_jiffies(info->se_info.wt_timeout));
307 break;
Christophe Ricard2b5dbe02015-10-25 22:54:37 +0100308 default:
309 nfc_err(&ndev->nfc_dev->dev, "Unexpected event on apdu reader gate\n");
310 return 1;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200311 }
312
313 kfree_skb(skb);
314 return r;
315}
316
317/*
318 * Returns:
319 * <= 0: driver handled the event, skb consumed
320 * 1: driver does not handle the event, please do standard processing
321 */
322static int st_nci_hci_connectivity_event_received(struct nci_dev *ndev,
323 u8 host, u8 event,
324 struct sk_buff *skb)
325{
326 int r = 0;
327 struct device *dev = &ndev->nfc_dev->dev;
328 struct nfc_evt_transaction *transaction;
329
330 pr_debug("connectivity gate event: %x\n", event);
331
332 switch (event) {
333 case ST_NCI_EVT_CONNECTIVITY:
334
335 break;
336 case ST_NCI_EVT_TRANSACTION:
337 /* According to specification etsi 102 622
338 * 11.2.2.4 EVT_TRANSACTION Table 52
339 * Description Tag Length
340 * AID 81 5 to 16
341 * PARAMETERS 82 0 to 255
342 */
343 if (skb->len < NFC_MIN_AID_LENGTH + 2 &&
344 skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
345 return -EPROTO;
346
347 transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
348 skb->len - 2, GFP_KERNEL);
349
350 transaction->aid_len = skb->data[1];
351 memcpy(transaction->aid, &skb->data[2], transaction->aid_len);
352
353 /* Check next byte is PARAMETERS tag (82) */
354 if (skb->data[transaction->aid_len + 2] !=
355 NFC_EVT_TRANSACTION_PARAMS_TAG)
356 return -EPROTO;
357
358 transaction->params_len = skb->data[transaction->aid_len + 3];
359 memcpy(transaction->params, skb->data +
360 transaction->aid_len + 4, transaction->params_len);
361
362 r = nfc_se_transaction(ndev->nfc_dev, host, transaction);
363 break;
364 default:
Christophe Ricard2b5dbe02015-10-25 22:54:37 +0100365 nfc_err(&ndev->nfc_dev->dev, "Unexpected event on connectivity gate\n");
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200366 return 1;
367 }
368 kfree_skb(skb);
369 return r;
370}
371
372void st_nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
373 u8 event, struct sk_buff *skb)
374{
375 u8 gate = ndev->hci_dev->pipes[pipe].gate;
376 u8 host = ndev->hci_dev->pipes[pipe].host;
377
378 switch (gate) {
379 case NCI_HCI_ADMIN_GATE:
380 st_nci_hci_admin_event_received(ndev, event, skb);
381 break;
382 case ST_NCI_APDU_READER_GATE:
383 st_nci_hci_apdu_reader_event_received(ndev, event, skb);
384 break;
385 case ST_NCI_CONNECTIVITY_GATE:
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100386 st_nci_hci_connectivity_event_received(ndev, host, event, skb);
387 break;
388 case NCI_HCI_LOOPBACK_GATE:
389 st_nci_hci_loopback_event_received(ndev, event, skb);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200390 break;
391 }
392}
393EXPORT_SYMBOL_GPL(st_nci_hci_event_received);
394
395
396void st_nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
397 struct sk_buff *skb)
398{
399 struct st_nci_info *info = nci_get_drvdata(ndev);
400 u8 gate = ndev->hci_dev->pipes[pipe].gate;
401
402 pr_debug("cmd: %x\n", cmd);
403
404 switch (cmd) {
405 case NCI_HCI_ANY_OPEN_PIPE:
406 if (gate != ST_NCI_APDU_READER_GATE &&
407 ndev->hci_dev->pipes[pipe].host != ST_NCI_UICC_HOST_ID)
408 ndev->hci_dev->count_pipes++;
409
410 if (ndev->hci_dev->count_pipes ==
411 ndev->hci_dev->expected_pipes) {
412 del_timer_sync(&info->se_info.se_active_timer);
413 info->se_info.se_active = false;
414 ndev->hci_dev->count_pipes = 0;
415 complete(&info->se_info.req_completion);
416 }
417 break;
418 }
419}
420EXPORT_SYMBOL_GPL(st_nci_hci_cmd_received);
421
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200422static int st_nci_control_se(struct nci_dev *ndev, u8 se_idx,
Christophe Ricard3648dc62015-10-25 22:54:39 +0100423 u8 state)
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200424{
425 struct st_nci_info *info = nci_get_drvdata(ndev);
426 int r;
427 struct sk_buff *sk_host_list;
428 u8 host_id;
429
430 switch (se_idx) {
431 case ST_NCI_UICC_HOST_ID:
432 ndev->hci_dev->count_pipes = 0;
433 ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_UICC;
434 break;
435 case ST_NCI_ESE_HOST_ID:
436 ndev->hci_dev->count_pipes = 0;
437 ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_EMBEDDED;
438 break;
439 default:
440 return -EINVAL;
441 }
442
443 /*
444 * Wait for an EVT_HOT_PLUG in order to
445 * retrieve a relevant host list.
446 */
447 reinit_completion(&info->se_info.req_completion);
Christophe Ricard3648dc62015-10-25 22:54:39 +0100448 r = nci_nfcee_mode_set(ndev, se_idx, state);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200449 if (r != NCI_STATUS_OK)
450 return r;
451
452 mod_timer(&info->se_info.se_active_timer, jiffies +
453 msecs_to_jiffies(ST_NCI_SE_TO_HOT_PLUG));
454 info->se_info.se_active = true;
455
456 /* Ignore return value and check in any case the host_list */
457 wait_for_completion_interruptible(&info->se_info.req_completion);
458
459 /* There might be some "collision" after receiving a HOT_PLUG event
460 * This may cause the CLF to not answer to the next hci command.
461 * There is no possible synchronization to prevent this.
462 * Adding a small delay is the only way to solve the issue.
463 */
Christophe Ricard3648dc62015-10-25 22:54:39 +0100464 if (info->se_info.se_status->is_ese_present &&
465 info->se_info.se_status->is_uicc_present)
466 usleep_range(3000, 5000);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200467
468 r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
469 NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list);
470 if (r != NCI_HCI_ANY_OK)
471 return r;
472
473 host_id = sk_host_list->data[sk_host_list->len - 1];
474 kfree_skb(sk_host_list);
475 if (state == ST_NCI_SE_MODE_ON && host_id == se_idx)
476 return se_idx;
477 else if (state == ST_NCI_SE_MODE_OFF && host_id != se_idx)
478 return se_idx;
479
480 return -1;
481}
482
483int st_nci_disable_se(struct nci_dev *ndev, u32 se_idx)
484{
485 int r;
486
487 pr_debug("st_nci_disable_se\n");
488
Christophe Ricard3648dc62015-10-25 22:54:39 +0100489 /*
490 * According to upper layer, se_idx == NFC_SE_UICC when
491 * info->se_info.se_status->is_uicc_enable is true should never happen
492 * Same for eSE.
493 */
494 r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_OFF);
495 if (r < 0) {
496 /* Do best effort to release SWP */
497 if (se_idx == NFC_SE_EMBEDDED) {
498 r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
499 ST_NCI_EVT_SE_END_OF_APDU_TRANSFER,
500 NULL, 0);
501 }
502 return r;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200503 }
504
505 return 0;
506}
507EXPORT_SYMBOL_GPL(st_nci_disable_se);
508
509int st_nci_enable_se(struct nci_dev *ndev, u32 se_idx)
510{
511 int r;
512
513 pr_debug("st_nci_enable_se\n");
514
Christophe Ricard3648dc62015-10-25 22:54:39 +0100515 /*
516 * According to upper layer, se_idx == NFC_SE_UICC when
517 * info->se_info.se_status->is_uicc_enable is true should never happen.
518 * Same for eSE.
519 */
520 r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_ON);
521 if (r == ST_NCI_HCI_HOST_ID_ESE) {
522 st_nci_se_get_atr(ndev);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200523 r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
524 ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
Christophe Ricard3648dc62015-10-25 22:54:39 +0100525 }
526
527 if (r < 0) {
528 /*
529 * The activation procedure failed, the secure element
530 * is not connected. Remove from the list.
531 */
532 nfc_remove_se(ndev->nfc_dev, se_idx);
533 return r;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200534 }
535
536 return 0;
537}
538EXPORT_SYMBOL_GPL(st_nci_enable_se);
539
540static int st_nci_hci_network_init(struct nci_dev *ndev)
541{
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100542 struct st_nci_info *info = nci_get_drvdata(ndev);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200543 struct core_conn_create_dest_spec_params *dest_params;
544 struct dest_spec_params spec_params;
545 struct nci_conn_info *conn_info;
546 int r, dev_num;
547
548 dest_params =
549 kzalloc(sizeof(struct core_conn_create_dest_spec_params) +
550 sizeof(struct dest_spec_params), GFP_KERNEL);
551 if (dest_params == NULL) {
552 r = -ENOMEM;
553 goto exit;
554 }
555
556 dest_params->type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE;
557 dest_params->length = sizeof(struct dest_spec_params);
558 spec_params.id = ndev->hci_dev->nfcee_id;
559 spec_params.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS;
560 memcpy(dest_params->value, &spec_params,
561 sizeof(struct dest_spec_params));
562 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCEE, 1,
563 sizeof(struct core_conn_create_dest_spec_params) +
564 sizeof(struct dest_spec_params),
565 dest_params);
566 if (r != NCI_STATUS_OK)
567 goto free_dest_params;
568
569 conn_info = ndev->hci_dev->conn_info;
570 if (!conn_info)
571 goto free_dest_params;
572
Christophe Ricard404b3e52015-10-25 22:54:32 +0100573 ndev->hci_dev->init_data.gate_count = ARRAY_SIZE(st_nci_gates);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200574 memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
575 sizeof(st_nci_gates));
576
577 /*
578 * Session id must include the driver name + i2c bus addr
579 * persistent info to discriminate 2 identical chips
580 */
581 dev_num = find_first_zero_bit(dev_mask, ST_NCI_NUM_DEVICES);
582 if (dev_num >= ST_NCI_NUM_DEVICES) {
583 r = -ENODEV;
584 goto free_dest_params;
585 }
586
587 scnprintf(ndev->hci_dev->init_data.session_id,
588 sizeof(ndev->hci_dev->init_data.session_id),
589 "%s%2x", "ST21BH", dev_num);
590
591 r = nci_hci_dev_session_init(ndev);
592 if (r != NCI_HCI_ANY_OK)
593 goto free_dest_params;
594
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100595 /*
596 * In factory mode, we prevent secure elements activation
597 * by disabling nfcee on the current HCI connection id.
598 * HCI will be used here only for proprietary commands.
599 */
600 if (test_bit(ST_NCI_FACTORY_MODE, &info->flags))
601 r = nci_nfcee_mode_set(ndev, ndev->hci_dev->conn_info->id,
602 NCI_NFCEE_DISABLE);
603 else
604 r = nci_nfcee_mode_set(ndev, ndev->hci_dev->conn_info->id,
605 NCI_NFCEE_ENABLE);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200606
607free_dest_params:
608 kfree(dest_params);
609
610exit:
611 return r;
612}
613
614int st_nci_discover_se(struct nci_dev *ndev)
615{
Christophe Ricard3648dc62015-10-25 22:54:39 +0100616 u8 white_list[2];
617 int r, wl_size = 0;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200618 int se_count = 0;
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100619 struct st_nci_info *info = nci_get_drvdata(ndev);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200620
621 pr_debug("st_nci_discover_se\n");
622
623 r = st_nci_hci_network_init(ndev);
624 if (r != 0)
625 return r;
626
Christophe Ricardb1fa4dc2015-10-25 22:54:36 +0100627 if (test_bit(ST_NCI_FACTORY_MODE, &info->flags))
628 return 0;
629
Christophe Ricard3648dc62015-10-25 22:54:39 +0100630 if (info->se_info.se_status->is_ese_present &&
631 info->se_info.se_status->is_uicc_present) {
632 white_list[wl_size++] = ST_NCI_UICC_HOST_ID;
633 white_list[wl_size++] = ST_NCI_ESE_HOST_ID;
634 } else if (!info->se_info.se_status->is_ese_present &&
635 info->se_info.se_status->is_uicc_present) {
636 white_list[wl_size++] = ST_NCI_UICC_HOST_ID;
637 } else if (info->se_info.se_status->is_ese_present &&
638 !info->se_info.se_status->is_uicc_present) {
639 white_list[wl_size++] = ST_NCI_ESE_HOST_ID;
640 }
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200641
Christophe Ricard3648dc62015-10-25 22:54:39 +0100642 if (wl_size) {
643 r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
644 NCI_HCI_ADMIN_PARAM_WHITELIST,
645 white_list, wl_size);
646 if (r != NCI_HCI_ANY_OK)
647 return r;
648 }
649
650 if (info->se_info.se_status->is_uicc_present) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200651 nfc_add_se(ndev->nfc_dev, ST_NCI_UICC_HOST_ID, NFC_SE_UICC);
652 se_count++;
653 }
654
Christophe Ricard3648dc62015-10-25 22:54:39 +0100655 if (info->se_info.se_status->is_ese_present) {
656 nfc_add_se(ndev->nfc_dev, ST_NCI_ESE_HOST_ID, NFC_SE_EMBEDDED);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200657 se_count++;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200658 }
659
660 return !se_count;
661}
662EXPORT_SYMBOL_GPL(st_nci_discover_se);
663
664int st_nci_se_io(struct nci_dev *ndev, u32 se_idx,
665 u8 *apdu, size_t apdu_length,
666 se_io_cb_t cb, void *cb_context)
667{
668 struct st_nci_info *info = nci_get_drvdata(ndev);
669
670 pr_debug("\n");
671
672 switch (se_idx) {
673 case ST_NCI_HCI_HOST_ID_ESE:
674 info->se_info.cb = cb;
675 info->se_info.cb_context = cb_context;
676 mod_timer(&info->se_info.bwi_timer, jiffies +
677 msecs_to_jiffies(info->se_info.wt_timeout));
678 info->se_info.bwi_active = true;
679 return nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
680 ST_NCI_EVT_TRANSMIT_DATA, apdu,
681 apdu_length);
682 default:
683 return -ENODEV;
684 }
685}
686EXPORT_SYMBOL(st_nci_se_io);
687
688static void st_nci_se_wt_timeout(unsigned long data)
689{
690 /*
691 * No answer from the secure element
692 * within the defined timeout.
693 * Let's send a reset request as recovery procedure.
694 * According to the situation, we first try to send a software reset
695 * to the secure element. If the next command is still not
696 * answering in time, we send to the CLF a secure element hardware
697 * reset request.
698 */
699 /* hardware reset managed through VCC_UICC_OUT power supply */
700 u8 param = 0x01;
701 struct st_nci_info *info = (struct st_nci_info *) data;
702
703 pr_debug("\n");
704
705 info->se_info.bwi_active = false;
706
707 if (!info->se_info.xch_error) {
708 info->se_info.xch_error = true;
709 nci_hci_send_event(info->ndlc->ndev, ST_NCI_APDU_READER_GATE,
710 ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
711 } else {
712 info->se_info.xch_error = false;
713 nci_hci_send_event(info->ndlc->ndev, ST_NCI_DEVICE_MGNT_GATE,
714 ST_NCI_EVT_SE_HARD_RESET, &param, 1);
715 }
716 info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
717}
718
719static void st_nci_se_activation_timeout(unsigned long data)
720{
721 struct st_nci_info *info = (struct st_nci_info *) data;
722
723 pr_debug("\n");
724
725 info->se_info.se_active = false;
726
727 complete(&info->se_info.req_completion);
728}
729
Christophe Ricard3648dc62015-10-25 22:54:39 +0100730int st_nci_se_init(struct nci_dev *ndev, struct st_nci_se_status *se_status)
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200731{
732 struct st_nci_info *info = nci_get_drvdata(ndev);
733
734 init_completion(&info->se_info.req_completion);
735 /* initialize timers */
736 init_timer(&info->se_info.bwi_timer);
737 info->se_info.bwi_timer.data = (unsigned long)info;
738 info->se_info.bwi_timer.function = st_nci_se_wt_timeout;
739 info->se_info.bwi_active = false;
740
741 init_timer(&info->se_info.se_active_timer);
742 info->se_info.se_active_timer.data = (unsigned long)info;
743 info->se_info.se_active_timer.function =
744 st_nci_se_activation_timeout;
745 info->se_info.se_active = false;
746
747 info->se_info.xch_error = false;
748
749 info->se_info.wt_timeout =
750 ST_NCI_BWI_TO_TIMEOUT(ST_NCI_ATR_DEFAULT_BWI);
751
Christophe Ricard3648dc62015-10-25 22:54:39 +0100752 info->se_info.se_status = se_status;
753
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200754 return 0;
755}
756EXPORT_SYMBOL(st_nci_se_init);
757
758void st_nci_se_deinit(struct nci_dev *ndev)
759{
760 struct st_nci_info *info = nci_get_drvdata(ndev);
761
762 if (info->se_info.bwi_active)
763 del_timer_sync(&info->se_info.bwi_timer);
764 if (info->se_info.se_active)
765 del_timer_sync(&info->se_info.se_active_timer);
766
767 info->se_info.se_active = false;
768 info->se_info.bwi_active = false;
769}
770EXPORT_SYMBOL(st_nci_se_deinit);
771