1 /*
2 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <net/nfc/hci.h>
18
19 #include "st21nfca.h"
20
21 #define ST21NFCA_EVT_UICC_ACTIVATE 0x10
22 #define ST21NFCA_EVT_UICC_DEACTIVATE 0x13
23 #define ST21NFCA_EVT_SE_HARD_RESET 0x20
24 #define ST21NFCA_EVT_SE_SOFT_RESET 0x11
25 #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER 0x21
26 #define ST21NFCA_EVT_SE_ACTIVATE 0x22
27 #define ST21NFCA_EVT_SE_DEACTIVATE 0x23
28
29 #define ST21NFCA_EVT_TRANSMIT_DATA 0x10
30 #define ST21NFCA_EVT_WTX_REQUEST 0x11
31
32 #define ST21NFCA_EVT_CONNECTIVITY 0x10
33 #define ST21NFCA_EVT_TRANSACTION 0x12
34
35 #define ST21NFCA_ESE_HOST_ID 0xc0
36
37 #define ST21NFCA_SE_TO_HOT_PLUG 1000
38 /* Connectivity pipe only */
39 #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01
40 /* Connectivity + APDU Reader pipe */
41 #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02
42
43 #define ST21NFCA_SE_MODE_OFF 0x00
44 #define ST21NFCA_SE_MODE_ON 0x01
45
46 #define ST21NFCA_PARAM_ATR 0x01
47 #define ST21NFCA_ATR_DEFAULT_BWI 0x04
48
49 /*
50 * WT = 2^BWI/10[s], convert into msecs and add a secure
51 * room by increasing by 2 this timeout
52 */
53 #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
54 #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4)
55
56 /* If TA is present bit 0 is set */
57 #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01)
58 /* If TB is present bit 1 is set */
59 #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02)
60
st21nfca_se_get_bwi(struct nfc_hci_dev * hdev)61 static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev)
62 {
63 int i;
64 u8 td;
65 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
66
67 /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
68 for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) {
69 td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
70 if (ST21NFCA_ATR_TA_PRESENT(td))
71 i++;
72 if (ST21NFCA_ATR_TB_PRESENT(td)) {
73 i++;
74 return info->se_info.atr[i] >> 4;
75 }
76 }
77 return ST21NFCA_ATR_DEFAULT_BWI;
78 }
79
st21nfca_se_get_atr(struct nfc_hci_dev * hdev)80 static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev)
81 {
82 int r;
83 struct sk_buff *skb;
84 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
85
86 r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE,
87 ST21NFCA_PARAM_ATR, &skb);
88 if (r < 0)
89 return;
90
91 if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) {
92 memcpy(info->se_info.atr, skb->data, skb->len);
93 info->se_info.wt_timeout =
94 ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev));
95 }
96 kfree_skb(skb);
97 }
98
st21nfca_hci_control_se(struct nfc_hci_dev * hdev,u32 se_idx,u8 state)99 static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx,
100 u8 state)
101 {
102 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
103 int r, i;
104 struct sk_buff *sk_host_list;
105 u8 se_event, host_id;
106
107 switch (se_idx) {
108 case NFC_HCI_UICC_HOST_ID:
109 se_event = (state == ST21NFCA_SE_MODE_ON ?
110 ST21NFCA_EVT_UICC_ACTIVATE :
111 ST21NFCA_EVT_UICC_DEACTIVATE);
112
113 info->se_info.count_pipes = 0;
114 info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC;
115 break;
116 case ST21NFCA_ESE_HOST_ID:
117 se_event = (state == ST21NFCA_SE_MODE_ON ?
118 ST21NFCA_EVT_SE_ACTIVATE :
119 ST21NFCA_EVT_SE_DEACTIVATE);
120
121 info->se_info.count_pipes = 0;
122 info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED;
123 break;
124 default:
125 return -EINVAL;
126 }
127
128 /*
129 * Wait for an EVT_HOT_PLUG in order to
130 * retrieve a relevant host list.
131 */
132 reinit_completion(&info->se_info.req_completion);
133 r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event,
134 NULL, 0);
135 if (r < 0)
136 return r;
137
138 mod_timer(&info->se_info.se_active_timer, jiffies +
139 msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG));
140 info->se_info.se_active = true;
141
142 /* Ignore return value and check in any case the host_list */
143 wait_for_completion_interruptible(&info->se_info.req_completion);
144
145 r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
146 NFC_HCI_ADMIN_HOST_LIST,
147 &sk_host_list);
148 if (r < 0)
149 return r;
150
151 for (i = 0; i < sk_host_list->len &&
152 sk_host_list->data[i] != se_idx; i++)
153 ;
154 host_id = sk_host_list->data[i];
155 kfree_skb(sk_host_list);
156
157 if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx)
158 return se_idx;
159 else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx)
160 return se_idx;
161
162 return -1;
163 }
164
st21nfca_hci_discover_se(struct nfc_hci_dev * hdev)165 int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev)
166 {
167 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
168 int se_count = 0;
169
170 if (test_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks))
171 return 0;
172
173 if (info->se_status->is_uicc_present) {
174 nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC);
175 se_count++;
176 }
177
178 if (info->se_status->is_ese_present) {
179 nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED);
180 se_count++;
181 }
182
183 return !se_count;
184 }
185 EXPORT_SYMBOL(st21nfca_hci_discover_se);
186
st21nfca_hci_enable_se(struct nfc_hci_dev * hdev,u32 se_idx)187 int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
188 {
189 int r;
190
191 /*
192 * According to upper layer, se_idx == NFC_SE_UICC when
193 * info->se_status->is_uicc_enable is true should never happen.
194 * Same for eSE.
195 */
196 r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON);
197 if (r == ST21NFCA_ESE_HOST_ID) {
198 st21nfca_se_get_atr(hdev);
199 r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
200 ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
201 if (r < 0)
202 return r;
203 } else if (r < 0) {
204 /*
205 * The activation tentative failed, the secure element
206 * is not connected. Remove from the list.
207 */
208 nfc_remove_se(hdev->ndev, se_idx);
209 return r;
210 }
211
212 return 0;
213 }
214 EXPORT_SYMBOL(st21nfca_hci_enable_se);
215
st21nfca_hci_disable_se(struct nfc_hci_dev * hdev,u32 se_idx)216 int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
217 {
218 int r;
219
220 /*
221 * According to upper layer, se_idx == NFC_SE_UICC when
222 * info->se_status->is_uicc_enable is true should never happen
223 * Same for eSE.
224 */
225 r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF);
226 if (r < 0)
227 return r;
228
229 return 0;
230 }
231 EXPORT_SYMBOL(st21nfca_hci_disable_se);
232
st21nfca_hci_se_io(struct nfc_hci_dev * hdev,u32 se_idx,u8 * apdu,size_t apdu_length,se_io_cb_t cb,void * cb_context)233 int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
234 u8 *apdu, size_t apdu_length,
235 se_io_cb_t cb, void *cb_context)
236 {
237 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
238
239 pr_debug("se_io %x\n", se_idx);
240
241 switch (se_idx) {
242 case ST21NFCA_ESE_HOST_ID:
243 info->se_info.cb = cb;
244 info->se_info.cb_context = cb_context;
245 mod_timer(&info->se_info.bwi_timer, jiffies +
246 msecs_to_jiffies(info->se_info.wt_timeout));
247 info->se_info.bwi_active = true;
248 return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
249 ST21NFCA_EVT_TRANSMIT_DATA,
250 apdu, apdu_length);
251 default:
252 return -ENODEV;
253 }
254 }
255 EXPORT_SYMBOL(st21nfca_hci_se_io);
256
st21nfca_se_wt_timeout(unsigned long data)257 static void st21nfca_se_wt_timeout(unsigned long data)
258 {
259 /*
260 * No answer from the secure element
261 * within the defined timeout.
262 * Let's send a reset request as recovery procedure.
263 * According to the situation, we first try to send a software reset
264 * to the secure element. If the next command is still not
265 * answering in time, we send to the CLF a secure element hardware
266 * reset request.
267 */
268 /* hardware reset managed through VCC_UICC_OUT power supply */
269 u8 param = 0x01;
270 struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data;
271
272 pr_debug("\n");
273
274 info->se_info.bwi_active = false;
275
276 if (!info->se_info.xch_error) {
277 info->se_info.xch_error = true;
278 nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE,
279 ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
280 } else {
281 info->se_info.xch_error = false;
282 nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
283 ST21NFCA_EVT_SE_HARD_RESET, ¶m, 1);
284 }
285 info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
286 }
287
st21nfca_se_activation_timeout(unsigned long data)288 static void st21nfca_se_activation_timeout(unsigned long data)
289 {
290 struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data;
291
292 pr_debug("\n");
293
294 info->se_info.se_active = false;
295
296 complete(&info->se_info.req_completion);
297 }
298
299 /*
300 * Returns:
301 * <= 0: driver handled the event, skb consumed
302 * 1: driver does not handle the event, please do standard processing
303 */
st21nfca_connectivity_event_received(struct nfc_hci_dev * hdev,u8 host,u8 event,struct sk_buff * skb)304 int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
305 u8 event, struct sk_buff *skb)
306 {
307 int r = 0;
308 struct device *dev = &hdev->ndev->dev;
309 struct nfc_evt_transaction *transaction;
310
311 pr_debug("connectivity gate event: %x\n", event);
312
313 switch (event) {
314 case ST21NFCA_EVT_CONNECTIVITY:
315 break;
316 case ST21NFCA_EVT_TRANSACTION:
317 /*
318 * According to specification etsi 102 622
319 * 11.2.2.4 EVT_TRANSACTION Table 52
320 * Description Tag Length
321 * AID 81 5 to 16
322 * PARAMETERS 82 0 to 255
323 */
324 if (skb->len < NFC_MIN_AID_LENGTH + 2 ||
325 skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
326 return -EPROTO;
327
328 /*
329 * Buffer should have enough space for at least
330 * two tag fields + two length fields + aid_len (skb->data[1])
331 */
332 if (skb->len < skb->data[1] + 4)
333 return -EPROTO;
334
335 transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
336 skb->len - 2, GFP_KERNEL);
337 if (!transaction)
338 return -ENOMEM;
339
340 transaction->aid_len = skb->data[1];
341 memcpy(transaction->aid, &skb->data[2],
342 transaction->aid_len);
343 transaction->params_len = skb->data[transaction->aid_len + 3];
344
345 /* Check next byte is PARAMETERS tag (82) and the length field */
346 if (skb->data[transaction->aid_len + 2] !=
347 NFC_EVT_TRANSACTION_PARAMS_TAG ||
348 skb->len < transaction->aid_len + transaction->params_len + 4) {
349 devm_kfree(dev, transaction);
350 return -EPROTO;
351 }
352
353 memcpy(transaction->params, skb->data +
354 transaction->aid_len + 4, transaction->params_len);
355
356 r = nfc_se_transaction(hdev->ndev, host, transaction);
357 break;
358 default:
359 nfc_err(&hdev->ndev->dev, "Unexpected event on connectivity gate\n");
360 return 1;
361 }
362 kfree_skb(skb);
363 return r;
364 }
365 EXPORT_SYMBOL(st21nfca_connectivity_event_received);
366
st21nfca_apdu_reader_event_received(struct nfc_hci_dev * hdev,u8 event,struct sk_buff * skb)367 int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
368 u8 event, struct sk_buff *skb)
369 {
370 int r = 0;
371 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
372
373 pr_debug("apdu reader gate event: %x\n", event);
374
375 switch (event) {
376 case ST21NFCA_EVT_TRANSMIT_DATA:
377 del_timer_sync(&info->se_info.bwi_timer);
378 info->se_info.bwi_active = false;
379 r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE,
380 ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
381 if (r < 0)
382 goto exit;
383
384 info->se_info.cb(info->se_info.cb_context,
385 skb->data, skb->len, 0);
386 break;
387 case ST21NFCA_EVT_WTX_REQUEST:
388 mod_timer(&info->se_info.bwi_timer, jiffies +
389 msecs_to_jiffies(info->se_info.wt_timeout));
390 break;
391 default:
392 nfc_err(&hdev->ndev->dev, "Unexpected event on apdu reader gate\n");
393 return 1;
394 }
395
396 exit:
397 kfree_skb(skb);
398 return r;
399 }
400 EXPORT_SYMBOL(st21nfca_apdu_reader_event_received);
401
st21nfca_se_init(struct nfc_hci_dev * hdev)402 void st21nfca_se_init(struct nfc_hci_dev *hdev)
403 {
404 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
405
406 init_completion(&info->se_info.req_completion);
407 /* initialize timers */
408 init_timer(&info->se_info.bwi_timer);
409 info->se_info.bwi_timer.data = (unsigned long)info;
410 info->se_info.bwi_timer.function = st21nfca_se_wt_timeout;
411 info->se_info.bwi_active = false;
412
413 init_timer(&info->se_info.se_active_timer);
414 info->se_info.se_active_timer.data = (unsigned long)info;
415 info->se_info.se_active_timer.function = st21nfca_se_activation_timeout;
416 info->se_info.se_active = false;
417
418 info->se_info.count_pipes = 0;
419 info->se_info.expected_pipes = 0;
420
421 info->se_info.xch_error = false;
422
423 info->se_info.wt_timeout =
424 ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI);
425 }
426 EXPORT_SYMBOL(st21nfca_se_init);
427
st21nfca_se_deinit(struct nfc_hci_dev * hdev)428 void st21nfca_se_deinit(struct nfc_hci_dev *hdev)
429 {
430 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
431
432 if (info->se_info.bwi_active)
433 del_timer_sync(&info->se_info.bwi_timer);
434 if (info->se_info.se_active)
435 del_timer_sync(&info->se_info.se_active_timer);
436
437 info->se_info.bwi_active = false;
438 info->se_info.se_active = false;
439 }
440 EXPORT_SYMBOL(st21nfca_se_deinit);
441