aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
authorChristophe Ricard <christophe.ricard@gmail.com>2015-02-01 16:26:15 -0500
committerSamuel Ortiz <sameo@linux.intel.com>2015-02-02 15:50:40 -0500
commit8ae01f7967718ac9ed800a99964f88c0c8b07e11 (patch)
treeb3eecaca807410f2a5f547ea9fcb551375b89163 /drivers/nfc
parent11f54f228643d0248ec00ce8c9fb8d872f87e7b8 (diff)
NFC: st21nfcb: Add support for secure element
The st21nfcb chipset has 3 SWP (Single Wire Protocol) lines and supports up to 3 secure elements (UICC/eSE and µSD in the future). Some st21nfcb firmware does not support the nci command nci_nfcee_mode_set(NCI_NFCEE_DISABLE). For this reason, we assume 2 secures elements are always present (UICC and eSE). They will be added to the SE list once successfully activated and they will be available only after running through enable_se handler or when the poll in listen mode is started. During initialization, the white_list will be always set assuming both UICC & eSE are present. On eSE activation, the ATR bytes are fetched to build the command exchange timeout. The se_io hook will allow to transfer data over SWP. 2 kind of events may appear data is sent over: - ST21NFCB_EVT_TRANSMIT_DATA when receiving an apdu answer - ST21NFCB_EVT_WTX_REQUEST when the secure element needs more time than expected to process a command. If this timeout expires, we send a software reset, and then a hardware one if it still fails. Signed-off-by: Christophe Ricard <christophe-h.ricard@st.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/st21nfcb/Makefile2
-rw-r--r--drivers/nfc/st21nfcb/st21nfcb.c11
-rw-r--r--drivers/nfc/st21nfcb/st21nfcb.h2
-rw-r--r--drivers/nfc/st21nfcb/st21nfcb_se.c672
-rw-r--r--drivers/nfc/st21nfcb/st21nfcb_se.h61
5 files changed, 746 insertions, 2 deletions
diff --git a/drivers/nfc/st21nfcb/Makefile b/drivers/nfc/st21nfcb/Makefile
index f4d835dd15f2..ce659a9e5a1a 100644
--- a/drivers/nfc/st21nfcb/Makefile
+++ b/drivers/nfc/st21nfcb/Makefile
@@ -2,7 +2,7 @@
2# Makefile for ST21NFCB NCI based NFC driver 2# Makefile for ST21NFCB NCI based NFC driver
3# 3#
4 4
5st21nfcb_nci-objs = ndlc.o st21nfcb.o 5st21nfcb_nci-objs = ndlc.o st21nfcb.o st21nfcb_se.o
6obj-$(CONFIG_NFC_ST21NFCB) += st21nfcb_nci.o 6obj-$(CONFIG_NFC_ST21NFCB) += st21nfcb_nci.o
7 7
8st21nfcb_i2c-objs = i2c.o 8st21nfcb_i2c-objs = i2c.o
diff --git a/drivers/nfc/st21nfcb/st21nfcb.c b/drivers/nfc/st21nfcb/st21nfcb.c
index ea63d5877831..ca9871ab3fb3 100644
--- a/drivers/nfc/st21nfcb/st21nfcb.c
+++ b/drivers/nfc/st21nfcb/st21nfcb.c
@@ -22,6 +22,7 @@
22#include <net/nfc/nci_core.h> 22#include <net/nfc/nci_core.h>
23 23
24#include "st21nfcb.h" 24#include "st21nfcb.h"
25#include "st21nfcb_se.h"
25 26
26#define DRIVER_DESC "NCI NFC driver for ST21NFCB" 27#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
27 28
@@ -78,6 +79,13 @@ static struct nci_ops st21nfcb_nci_ops = {
78 .close = st21nfcb_nci_close, 79 .close = st21nfcb_nci_close,
79 .send = st21nfcb_nci_send, 80 .send = st21nfcb_nci_send,
80 .get_rfprotocol = st21nfcb_nci_get_rfprotocol, 81 .get_rfprotocol = st21nfcb_nci_get_rfprotocol,
82 .discover_se = st21nfcb_nci_discover_se,
83 .enable_se = st21nfcb_nci_enable_se,
84 .disable_se = st21nfcb_nci_disable_se,
85 .se_io = st21nfcb_nci_se_io,
86 .hci_load_session = st21nfcb_hci_load_session,
87 .hci_event_received = st21nfcb_hci_event_received,
88 .hci_cmd_received = st21nfcb_hci_cmd_received,
81}; 89};
82 90
83int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom, 91int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
@@ -114,9 +122,10 @@ int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
114 if (r) { 122 if (r) {
115 pr_err("Cannot register nfc device to nci core\n"); 123 pr_err("Cannot register nfc device to nci core\n");
116 nci_free_device(ndlc->ndev); 124 nci_free_device(ndlc->ndev);
125 return r;
117 } 126 }
118 127
119 return r; 128 return st21nfcb_se_init(ndlc->ndev);
120} 129}
121EXPORT_SYMBOL_GPL(st21nfcb_nci_probe); 130EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
122 131
diff --git a/drivers/nfc/st21nfcb/st21nfcb.h b/drivers/nfc/st21nfcb/st21nfcb.h
index ea58a56ad794..5ef8a58c9839 100644
--- a/drivers/nfc/st21nfcb/st21nfcb.h
+++ b/drivers/nfc/st21nfcb/st21nfcb.h
@@ -19,6 +19,7 @@
19#ifndef __LOCAL_ST21NFCB_H_ 19#ifndef __LOCAL_ST21NFCB_H_
20#define __LOCAL_ST21NFCB_H_ 20#define __LOCAL_ST21NFCB_H_
21 21
22#include "st21nfcb_se.h"
22#include "ndlc.h" 23#include "ndlc.h"
23 24
24/* Define private flags: */ 25/* Define private flags: */
@@ -27,6 +28,7 @@
27struct st21nfcb_nci_info { 28struct st21nfcb_nci_info {
28 struct llt_ndlc *ndlc; 29 struct llt_ndlc *ndlc;
29 unsigned long flags; 30 unsigned long flags;
31 struct st21nfcb_se_info se_info;
30}; 32};
31 33
32void st21nfcb_nci_remove(struct nci_dev *ndev); 34void st21nfcb_nci_remove(struct nci_dev *ndev);
diff --git a/drivers/nfc/st21nfcb/st21nfcb_se.c b/drivers/nfc/st21nfcb/st21nfcb_se.c
new file mode 100644
index 000000000000..f7977a47c047
--- /dev/null
+++ b/drivers/nfc/st21nfcb/st21nfcb_se.c
@@ -0,0 +1,672 @@
1/*
2 * NCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 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 "st21nfcb.h"
26#include "st21nfcb_se.h"
27
28struct st21nfcb_pipe_info {
29 u8 pipe_state;
30 u8 src_host_id;
31 u8 src_gate_id;
32 u8 dst_host_id;
33 u8 dst_gate_id;
34} __packed;
35
36/* Hosts */
37#define ST21NFCB_HOST_CONTROLLER_ID 0x00
38#define ST21NFCB_TERMINAL_HOST_ID 0x01
39#define ST21NFCB_UICC_HOST_ID 0x02
40#define ST21NFCB_ESE_HOST_ID 0xc0
41
42/* Gates */
43#define ST21NFCB_DEVICE_MGNT_GATE 0x01
44#define ST21NFCB_APDU_READER_GATE 0xf0
45#define ST21NFCB_CONNECTIVITY_GATE 0x41
46
47/* Pipes */
48#define ST21NFCB_DEVICE_MGNT_PIPE 0x02
49
50/* Connectivity pipe only */
51#define ST21NFCB_SE_COUNT_PIPE_UICC 0x01
52/* Connectivity + APDU Reader pipe */
53#define ST21NFCB_SE_COUNT_PIPE_EMBEDDED 0x02
54
55#define ST21NFCB_SE_TO_HOT_PLUG 1000 /* msecs */
56#define ST21NFCB_SE_TO_PIPES 2000
57
58#define ST21NFCB_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
59
60#define NCI_HCI_APDU_PARAM_ATR 0x01
61#define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
62#define NCI_HCI_ADMIN_PARAM_WHITELIST 0x03
63#define NCI_HCI_ADMIN_PARAM_HOST_LIST 0x04
64
65#define ST21NFCB_EVT_SE_HARD_RESET 0x20
66#define ST21NFCB_EVT_TRANSMIT_DATA 0x10
67#define ST21NFCB_EVT_WTX_REQUEST 0x11
68#define ST21NFCB_EVT_SE_SOFT_RESET 0x11
69#define ST21NFCB_EVT_SE_END_OF_APDU_TRANSFER 0x21
70#define ST21NFCB_EVT_HOT_PLUG 0x03
71
72#define ST21NFCB_SE_MODE_OFF 0x00
73#define ST21NFCB_SE_MODE_ON 0x01
74
75#define ST21NFCB_EVT_CONNECTIVITY 0x10
76#define ST21NFCB_EVT_TRANSACTION 0x12
77
78#define ST21NFCB_DM_GETINFO 0x13
79#define ST21NFCB_DM_GETINFO_PIPE_LIST 0x02
80#define ST21NFCB_DM_GETINFO_PIPE_INFO 0x01
81#define ST21NFCB_DM_PIPE_CREATED 0x02
82#define ST21NFCB_DM_PIPE_OPEN 0x04
83#define ST21NFCB_DM_RF_ACTIVE 0x80
84#define ST21NFCB_DM_DISCONNECT 0x30
85
86#define ST21NFCB_DM_IS_PIPE_OPEN(p) \
87 ((p & 0x0f) == (ST21NFCB_DM_PIPE_CREATED | ST21NFCB_DM_PIPE_OPEN))
88
89#define ST21NFCB_ATR_DEFAULT_BWI 0x04
90
91/*
92 * WT = 2^BWI/10[s], convert into msecs and add a secure
93 * room by increasing by 2 this timeout
94 */
95#define ST21NFCB_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
96#define ST21NFCB_ATR_GET_Y_FROM_TD(x) (x >> 4)
97
98/* If TA is present bit 0 is set */
99#define ST21NFCB_ATR_TA_PRESENT(x) (x & 0x01)
100/* If TB is present bit 1 is set */
101#define ST21NFCB_ATR_TB_PRESENT(x) (x & 0x02)
102
103#define ST21NFCB_NUM_DEVICES 256
104
105static DECLARE_BITMAP(dev_mask, ST21NFCB_NUM_DEVICES);
106
107/* Here are the mandatory pipe for st21nfcb */
108static struct nci_hci_gate st21nfcb_gates[] = {
109 {NCI_HCI_ADMIN_GATE, NCI_HCI_ADMIN_PIPE,
110 ST21NFCB_HOST_CONTROLLER_ID},
111 {NCI_HCI_LINK_MGMT_GATE, NCI_HCI_LINK_MGMT_PIPE,
112 ST21NFCB_HOST_CONTROLLER_ID},
113 {ST21NFCB_DEVICE_MGNT_GATE, ST21NFCB_DEVICE_MGNT_PIPE,
114 ST21NFCB_HOST_CONTROLLER_ID},
115
116 /* Secure element pipes are created by secure element host */
117 {ST21NFCB_CONNECTIVITY_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
118 ST21NFCB_HOST_CONTROLLER_ID},
119 {ST21NFCB_APDU_READER_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
120 ST21NFCB_HOST_CONTROLLER_ID},
121};
122
123static u8 st21nfcb_se_get_bwi(struct nci_dev *ndev)
124{
125 int i;
126 u8 td;
127 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
128
129 /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
130 for (i = 1; i < ST21NFCB_ESE_MAX_LENGTH; i++) {
131 td = ST21NFCB_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
132 if (ST21NFCB_ATR_TA_PRESENT(td))
133 i++;
134 if (ST21NFCB_ATR_TB_PRESENT(td)) {
135 i++;
136 return info->se_info.atr[i] >> 4;
137 }
138 }
139 return ST21NFCB_ATR_DEFAULT_BWI;
140}
141
142static void st21nfcb_se_get_atr(struct nci_dev *ndev)
143{
144 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
145 int r;
146 struct sk_buff *skb;
147
148 r = nci_hci_get_param(ndev, ST21NFCB_APDU_READER_GATE,
149 NCI_HCI_APDU_PARAM_ATR, &skb);
150 if (r < 0)
151 return;
152
153 if (skb->len <= ST21NFCB_ESE_MAX_LENGTH) {
154 memcpy(info->se_info.atr, skb->data, skb->len);
155
156 info->se_info.wt_timeout =
157 ST21NFCB_BWI_TO_TIMEOUT(st21nfcb_se_get_bwi(ndev));
158 }
159 kfree_skb(skb);
160}
161
162int st21nfcb_hci_load_session(struct nci_dev *ndev)
163{
164 int i, j, r;
165 struct sk_buff *skb_pipe_list, *skb_pipe_info;
166 struct st21nfcb_pipe_info *dm_pipe_info;
167 u8 pipe_list[] = { ST21NFCB_DM_GETINFO_PIPE_LIST,
168 ST21NFCB_TERMINAL_HOST_ID};
169 u8 pipe_info[] = { ST21NFCB_DM_GETINFO_PIPE_INFO,
170 ST21NFCB_TERMINAL_HOST_ID, 0};
171
172 /* On ST21NFCB device pipes number are dynamics
173 * If pipes are already created, hci_dev_up will fail.
174 * Doing a clear all pipe is a bad idea because:
175 * - It does useless EEPROM cycling
176 * - It might cause issue for secure elements support
177 * (such as removing connectivity or APDU reader pipe)
178 * A better approach on ST21NFCB is to:
179 * - get a pipe list for each host.
180 * (eg: ST21NFCB_HOST_CONTROLLER_ID for now).
181 * (TODO Later on UICC HOST and eSE HOST)
182 * - get pipe information
183 * - match retrieved pipe list in st21nfcb_gates
184 * ST21NFCB_DEVICE_MGNT_GATE is a proprietary gate
185 * with ST21NFCB_DEVICE_MGNT_PIPE.
186 * Pipe can be closed and need to be open.
187 */
188 r = nci_hci_connect_gate(ndev, ST21NFCB_HOST_CONTROLLER_ID,
189 ST21NFCB_DEVICE_MGNT_GATE,
190 ST21NFCB_DEVICE_MGNT_PIPE);
191 if (r < 0)
192 goto free_info;
193
194 /* Get pipe list */
195 r = nci_hci_send_cmd(ndev, ST21NFCB_DEVICE_MGNT_GATE,
196 ST21NFCB_DM_GETINFO, pipe_list, sizeof(pipe_list),
197 &skb_pipe_list);
198 if (r < 0)
199 goto free_info;
200
201 /* Complete the existing gate_pipe table */
202 for (i = 0; i < skb_pipe_list->len; i++) {
203 pipe_info[2] = skb_pipe_list->data[i];
204 r = nci_hci_send_cmd(ndev, ST21NFCB_DEVICE_MGNT_GATE,
205 ST21NFCB_DM_GETINFO, pipe_info,
206 sizeof(pipe_info), &skb_pipe_info);
207
208 if (r)
209 continue;
210
211 /*
212 * Match pipe ID and gate ID
213 * Output format from ST21NFC_DM_GETINFO is:
214 * - pipe state (1byte)
215 * - source hid (1byte)
216 * - source gid (1byte)
217 * - destination hid (1byte)
218 * - destination gid (1byte)
219 */
220 dm_pipe_info = (struct st21nfcb_pipe_info *)skb_pipe_info->data;
221 if (dm_pipe_info->dst_gate_id == ST21NFCB_APDU_READER_GATE &&
222 dm_pipe_info->src_host_id != ST21NFCB_ESE_HOST_ID) {
223 pr_err("Unexpected apdu_reader pipe on host %x\n",
224 dm_pipe_info->src_host_id);
225 continue;
226 }
227
228 for (j = 0; (j < ARRAY_SIZE(st21nfcb_gates)) &&
229 (st21nfcb_gates[j].gate != dm_pipe_info->dst_gate_id); j++)
230 ;
231
232 if (j < ARRAY_SIZE(st21nfcb_gates) &&
233 st21nfcb_gates[j].gate == dm_pipe_info->dst_gate_id &&
234 ST21NFCB_DM_IS_PIPE_OPEN(dm_pipe_info->pipe_state)) {
235 st21nfcb_gates[j].pipe = pipe_info[2];
236
237 ndev->hci_dev->gate2pipe[st21nfcb_gates[j].gate] =
238 st21nfcb_gates[j].pipe;
239 ndev->hci_dev->pipes[st21nfcb_gates[j].pipe].gate =
240 st21nfcb_gates[j].gate;
241 ndev->hci_dev->pipes[st21nfcb_gates[j].pipe].host =
242 dm_pipe_info->src_host_id;
243 }
244 }
245
246 memcpy(ndev->hci_dev->init_data.gates, st21nfcb_gates,
247 sizeof(st21nfcb_gates));
248
249free_info:
250 kfree_skb(skb_pipe_info);
251 kfree_skb(skb_pipe_list);
252 return r;
253}
254EXPORT_SYMBOL_GPL(st21nfcb_hci_load_session);
255
256static void st21nfcb_hci_admin_event_received(struct nci_dev *ndev,
257 u8 event, struct sk_buff *skb)
258{
259 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
260
261 switch (event) {
262 case ST21NFCB_EVT_HOT_PLUG:
263 if (info->se_info.se_active) {
264 if (!ST21NFCB_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
265 del_timer_sync(&info->se_info.se_active_timer);
266 info->se_info.se_active = false;
267 complete(&info->se_info.req_completion);
268 } else {
269 mod_timer(&info->se_info.se_active_timer,
270 jiffies +
271 msecs_to_jiffies(ST21NFCB_SE_TO_PIPES));
272 }
273 }
274 break;
275 }
276}
277
278static int st21nfcb_hci_apdu_reader_event_received(struct nci_dev *ndev,
279 u8 event,
280 struct sk_buff *skb)
281{
282 int r = 0;
283 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
284
285 pr_debug("apdu reader gate event: %x\n", event);
286
287 switch (event) {
288 case ST21NFCB_EVT_TRANSMIT_DATA:
289 del_timer_sync(&info->se_info.bwi_timer);
290 info->se_info.bwi_active = false;
291 info->se_info.cb(info->se_info.cb_context,
292 skb->data, skb->len, 0);
293 break;
294 case ST21NFCB_EVT_WTX_REQUEST:
295 mod_timer(&info->se_info.bwi_timer, jiffies +
296 msecs_to_jiffies(info->se_info.wt_timeout));
297 break;
298 }
299
300 kfree_skb(skb);
301 return r;
302}
303
304/*
305 * Returns:
306 * <= 0: driver handled the event, skb consumed
307 * 1: driver does not handle the event, please do standard processing
308 */
309static int st21nfcb_hci_connectivity_event_received(struct nci_dev *ndev,
310 u8 host, u8 event,
311 struct sk_buff *skb)
312{
313 int r = 0;
314
315 pr_debug("connectivity gate event: %x\n", event);
316
317 switch (event) {
318 case ST21NFCB_EVT_CONNECTIVITY:
319 break;
320 case ST21NFCB_EVT_TRANSACTION:
321 break;
322 default:
323 return 1;
324 }
325 kfree_skb(skb);
326 return r;
327}
328
329void st21nfcb_hci_event_received(struct nci_dev *ndev, u8 pipe,
330 u8 event, struct sk_buff *skb)
331{
332 u8 gate = ndev->hci_dev->pipes[pipe].gate;
333 u8 host = ndev->hci_dev->pipes[pipe].host;
334
335 switch (gate) {
336 case NCI_HCI_ADMIN_GATE:
337 st21nfcb_hci_admin_event_received(ndev, event, skb);
338 break;
339 case ST21NFCB_APDU_READER_GATE:
340 st21nfcb_hci_apdu_reader_event_received(ndev, event, skb);
341 break;
342 case ST21NFCB_CONNECTIVITY_GATE:
343 st21nfcb_hci_connectivity_event_received(ndev, host, event,
344 skb);
345 break;
346 }
347}
348EXPORT_SYMBOL_GPL(st21nfcb_hci_event_received);
349
350
351void st21nfcb_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
352 struct sk_buff *skb)
353{
354 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
355 u8 gate = ndev->hci_dev->pipes[pipe].gate;
356
357 pr_debug("cmd: %x\n", cmd);
358
359 switch (cmd) {
360 case NCI_HCI_ANY_OPEN_PIPE:
361 if (gate != ST21NFCB_APDU_READER_GATE &&
362 ndev->hci_dev->pipes[pipe].host != ST21NFCB_UICC_HOST_ID)
363 ndev->hci_dev->count_pipes++;
364
365 if (ndev->hci_dev->count_pipes ==
366 ndev->hci_dev->expected_pipes) {
367 del_timer_sync(&info->se_info.se_active_timer);
368 info->se_info.se_active = false;
369 ndev->hci_dev->count_pipes = 0;
370 complete(&info->se_info.req_completion);
371 }
372 break;
373 }
374}
375EXPORT_SYMBOL_GPL(st21nfcb_hci_cmd_received);
376
377/*
378 * Remarks: On some early st21nfcb firmware, nci_nfcee_mode_set(0)
379 * is rejected
380 */
381static int st21nfcb_nci_control_se(struct nci_dev *ndev, u8 se_idx,
382 u8 state)
383{
384 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
385 int r;
386 struct sk_buff *sk_host_list;
387 u8 host_id;
388
389 switch (se_idx) {
390 case ST21NFCB_UICC_HOST_ID:
391 ndev->hci_dev->count_pipes = 0;
392 ndev->hci_dev->expected_pipes = ST21NFCB_SE_COUNT_PIPE_UICC;
393 break;
394 case ST21NFCB_ESE_HOST_ID:
395 ndev->hci_dev->count_pipes = 0;
396 ndev->hci_dev->expected_pipes = ST21NFCB_SE_COUNT_PIPE_EMBEDDED;
397 break;
398 default:
399 return -EINVAL;
400 }
401
402 /*
403 * Wait for an EVT_HOT_PLUG in order to
404 * retrieve a relevant host list.
405 */
406 reinit_completion(&info->se_info.req_completion);
407 r = nci_nfcee_mode_set(ndev, se_idx, NCI_NFCEE_ENABLE);
408 if (r != NCI_STATUS_OK)
409 return r;
410
411 mod_timer(&info->se_info.se_active_timer, jiffies +
412 msecs_to_jiffies(ST21NFCB_SE_TO_HOT_PLUG));
413 info->se_info.se_active = true;
414
415 /* Ignore return value and check in any case the host_list */
416 wait_for_completion_interruptible(&info->se_info.req_completion);
417
418 /* There might be some "collision" after receiving a HOT_PLUG event
419 * This may cause the CLF to not answer to the next hci command.
420 * There is no possible synchronization to prevent this.
421 * Adding a small delay is the only way to solve the issue.
422 */
423 usleep_range(3000, 5000);
424
425 r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
426 NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list);
427 if (r != NCI_HCI_ANY_OK)
428 return r;
429
430 host_id = sk_host_list->data[sk_host_list->len - 1];
431 kfree_skb(sk_host_list);
432 if (state == ST21NFCB_SE_MODE_ON && host_id == se_idx)
433 return se_idx;
434 else if (state == ST21NFCB_SE_MODE_OFF && host_id != se_idx)
435 return se_idx;
436
437 return -1;
438}
439
440int st21nfcb_nci_disable_se(struct nci_dev *ndev, u32 se_idx)
441{
442 int r;
443
444 pr_debug("st21nfcb_nci_disable_se\n");
445
446 if (se_idx == NFC_SE_EMBEDDED) {
447 r = nci_hci_send_event(ndev, ST21NFCB_APDU_READER_GATE,
448 ST21NFCB_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
449 if (r < 0)
450 return r;
451 }
452
453 return 0;
454}
455EXPORT_SYMBOL_GPL(st21nfcb_nci_disable_se);
456
457int st21nfcb_nci_enable_se(struct nci_dev *ndev, u32 se_idx)
458{
459 int r;
460
461 pr_debug("st21nfcb_nci_enable_se\n");
462
463 if (se_idx == ST21NFCB_HCI_HOST_ID_ESE) {
464 r = nci_hci_send_event(ndev, ST21NFCB_APDU_READER_GATE,
465 ST21NFCB_EVT_SE_SOFT_RESET, NULL, 0);
466 if (r < 0)
467 return r;
468 }
469
470 return 0;
471}
472EXPORT_SYMBOL_GPL(st21nfcb_nci_enable_se);
473
474static int st21nfcb_hci_network_init(struct nci_dev *ndev)
475{
476 struct core_conn_create_dest_spec_params dest_params;
477 struct nci_conn_info *conn_info;
478 int r, dev_num;
479
480 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
481 if (r != NCI_STATUS_OK)
482 goto exit;
483
484 dest_params.type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE;
485 dest_params.length = sizeof(struct dest_spec_params);
486 dest_params.value.id = ndev->hci_dev->conn_info->id;
487 dest_params.value.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS;
488 r = nci_core_conn_create(ndev, &dest_params);
489 if (r != NCI_STATUS_OK)
490 goto exit;
491
492 conn_info = ndev->hci_dev->conn_info;
493 if (!conn_info)
494 goto exit;
495
496 memcpy(ndev->hci_dev->init_data.gates, st21nfcb_gates,
497 sizeof(st21nfcb_gates));
498
499 /*
500 * Session id must include the driver name + i2c bus addr
501 * persistent info to discriminate 2 identical chips
502 */
503 dev_num = find_first_zero_bit(dev_mask, ST21NFCB_NUM_DEVICES);
504 if (dev_num >= ST21NFCB_NUM_DEVICES)
505 return -ENODEV;
506
507 scnprintf(ndev->hci_dev->init_data.session_id,
508 sizeof(ndev->hci_dev->init_data.session_id),
509 "%s%2x", "ST21BH", dev_num);
510
511 r = nci_hci_dev_session_init(ndev);
512 if (r != NCI_HCI_ANY_OK)
513 goto exit;
514
515 r = nci_nfcee_mode_set(ndev, ndev->hci_dev->conn_info->id,
516 NCI_NFCEE_ENABLE);
517 if (r != NCI_STATUS_OK)
518 goto exit;
519
520 return 0;
521
522exit:
523 return r;
524}
525
526int st21nfcb_nci_discover_se(struct nci_dev *ndev)
527{
528 u8 param[2];
529 int r;
530 int se_count = 0;
531
532 pr_debug("st21nfcb_nci_discover_se\n");
533
534 r = st21nfcb_hci_network_init(ndev);
535 if (r != 0)
536 return r;
537
538 param[0] = ST21NFCB_UICC_HOST_ID;
539 param[1] = ST21NFCB_HCI_HOST_ID_ESE;
540 r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
541 NCI_HCI_ADMIN_PARAM_WHITELIST,
542 param, sizeof(param));
543 if (r != NCI_HCI_ANY_OK)
544 return r;
545
546 r = st21nfcb_nci_control_se(ndev, ST21NFCB_UICC_HOST_ID,
547 ST21NFCB_SE_MODE_ON);
548 if (r == ST21NFCB_UICC_HOST_ID) {
549 nfc_add_se(ndev->nfc_dev, ST21NFCB_UICC_HOST_ID, NFC_SE_UICC);
550 se_count++;
551 }
552
553 /* Try to enable eSE in order to check availability */
554 r = st21nfcb_nci_control_se(ndev, ST21NFCB_HCI_HOST_ID_ESE,
555 ST21NFCB_SE_MODE_ON);
556 if (r == ST21NFCB_HCI_HOST_ID_ESE) {
557 nfc_add_se(ndev->nfc_dev, ST21NFCB_HCI_HOST_ID_ESE,
558 NFC_SE_EMBEDDED);
559 se_count++;
560 st21nfcb_se_get_atr(ndev);
561 }
562
563 return !se_count;
564}
565EXPORT_SYMBOL_GPL(st21nfcb_nci_discover_se);
566
567int st21nfcb_nci_se_io(struct nci_dev *ndev, u32 se_idx,
568 u8 *apdu, size_t apdu_length,
569 se_io_cb_t cb, void *cb_context)
570{
571 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
572
573 pr_debug("\n");
574
575 switch (se_idx) {
576 case ST21NFCB_HCI_HOST_ID_ESE:
577 info->se_info.cb = cb;
578 info->se_info.cb_context = cb_context;
579 mod_timer(&info->se_info.bwi_timer, jiffies +
580 msecs_to_jiffies(info->se_info.wt_timeout));
581 info->se_info.bwi_active = true;
582 return nci_hci_send_event(ndev, ST21NFCB_APDU_READER_GATE,
583 ST21NFCB_EVT_TRANSMIT_DATA, apdu,
584 apdu_length);
585 default:
586 return -ENODEV;
587 }
588}
589EXPORT_SYMBOL(st21nfcb_nci_se_io);
590
591static void st21nfcb_se_wt_timeout(unsigned long data)
592{
593 /*
594 * No answer from the secure element
595 * within the defined timeout.
596 * Let's send a reset request as recovery procedure.
597 * According to the situation, we first try to send a software reset
598 * to the secure element. If the next command is still not
599 * answering in time, we send to the CLF a secure element hardware
600 * reset request.
601 */
602 /* hardware reset managed through VCC_UICC_OUT power supply */
603 u8 param = 0x01;
604 struct st21nfcb_nci_info *info = (struct st21nfcb_nci_info *) data;
605
606 pr_debug("\n");
607
608 info->se_info.bwi_active = false;
609
610 if (!info->se_info.xch_error) {
611 info->se_info.xch_error = true;
612 nci_hci_send_event(info->ndlc->ndev, ST21NFCB_APDU_READER_GATE,
613 ST21NFCB_EVT_SE_SOFT_RESET, NULL, 0);
614 } else {
615 info->se_info.xch_error = false;
616 nci_hci_send_event(info->ndlc->ndev, ST21NFCB_DEVICE_MGNT_GATE,
617 ST21NFCB_EVT_SE_HARD_RESET, &param, 1);
618 }
619 info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
620}
621
622static void st21nfcb_se_activation_timeout(unsigned long data)
623{
624 struct st21nfcb_nci_info *info = (struct st21nfcb_nci_info *) data;
625
626 pr_debug("\n");
627
628 info->se_info.se_active = false;
629
630 complete(&info->se_info.req_completion);
631}
632
633int st21nfcb_se_init(struct nci_dev *ndev)
634{
635 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
636
637 init_completion(&info->se_info.req_completion);
638 /* initialize timers */
639 init_timer(&info->se_info.bwi_timer);
640 info->se_info.bwi_timer.data = (unsigned long)info;
641 info->se_info.bwi_timer.function = st21nfcb_se_wt_timeout;
642 info->se_info.bwi_active = false;
643
644 init_timer(&info->se_info.se_active_timer);
645 info->se_info.se_active_timer.data = (unsigned long)info;
646 info->se_info.se_active_timer.function =
647 st21nfcb_se_activation_timeout;
648 info->se_info.se_active = false;
649
650 info->se_info.xch_error = false;
651
652 info->se_info.wt_timeout =
653 ST21NFCB_BWI_TO_TIMEOUT(ST21NFCB_ATR_DEFAULT_BWI);
654
655 return 0;
656}
657EXPORT_SYMBOL(st21nfcb_se_init);
658
659void st21nfcb_se_deinit(struct nci_dev *ndev)
660{
661 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
662
663 if (info->se_info.bwi_active)
664 del_timer_sync(&info->se_info.bwi_timer);
665 if (info->se_info.se_active)
666 del_timer_sync(&info->se_info.se_active_timer);
667
668 info->se_info.se_active = false;
669 info->se_info.bwi_active = false;
670}
671EXPORT_SYMBOL(st21nfcb_se_deinit);
672
diff --git a/drivers/nfc/st21nfcb/st21nfcb_se.h b/drivers/nfc/st21nfcb/st21nfcb_se.h
new file mode 100644
index 000000000000..52a323872bea
--- /dev/null
+++ b/drivers/nfc/st21nfcb/st21nfcb_se.h
@@ -0,0 +1,61 @@
1/*
2 * NCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 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#ifndef __LOCAL_ST21NFCB_SE_H_
19#define __LOCAL_ST21NFCB_SE_H_
20
21/*
22 * ref ISO7816-3 chap 8.1. the initial character TS is followed by a
23 * sequence of at most 32 characters.
24 */
25#define ST21NFCB_ESE_MAX_LENGTH 33
26#define ST21NFCB_HCI_HOST_ID_ESE 0xc0
27
28struct st21nfcb_se_info {
29 u8 atr[ST21NFCB_ESE_MAX_LENGTH];
30 struct completion req_completion;
31
32 struct timer_list bwi_timer;
33 int wt_timeout; /* in msecs */
34 bool bwi_active;
35
36 struct timer_list se_active_timer;
37 bool se_active;
38
39 bool xch_error;
40
41 se_io_cb_t cb;
42 void *cb_context;
43};
44
45int st21nfcb_se_init(struct nci_dev *ndev);
46void st21nfcb_se_deinit(struct nci_dev *ndev);
47
48int st21nfcb_nci_discover_se(struct nci_dev *ndev);
49int st21nfcb_nci_enable_se(struct nci_dev *ndev, u32 se_idx);
50int st21nfcb_nci_disable_se(struct nci_dev *ndev, u32 se_idx);
51int st21nfcb_nci_se_io(struct nci_dev *ndev, u32 se_idx,
52 u8 *apdu, size_t apdu_length,
53 se_io_cb_t cb, void *cb_context);
54int st21nfcb_hci_load_session(struct nci_dev *ndev);
55void st21nfcb_hci_event_received(struct nci_dev *ndev, u8 pipe,
56 u8 event, struct sk_buff *skb);
57void st21nfcb_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
58 struct sk_buff *skb);
59
60
61#endif /* __LOCAL_ST21NFCB_NCI_H_ */