1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
6 #include <net/nfc/hci.h>
10 #define ST21NFCA_EVT_UICC_ACTIVATE 0x10
11 #define ST21NFCA_EVT_UICC_DEACTIVATE 0x13
12 #define ST21NFCA_EVT_SE_HARD_RESET 0x20
13 #define ST21NFCA_EVT_SE_SOFT_RESET 0x11
14 #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER 0x21
15 #define ST21NFCA_EVT_SE_ACTIVATE 0x22
16 #define ST21NFCA_EVT_SE_DEACTIVATE 0x23
18 #define ST21NFCA_EVT_TRANSMIT_DATA 0x10
19 #define ST21NFCA_EVT_WTX_REQUEST 0x11
21 #define ST21NFCA_EVT_CONNECTIVITY 0x10
22 #define ST21NFCA_EVT_TRANSACTION 0x12
24 #define ST21NFCA_SE_TO_HOT_PLUG 1000
25 /* Connectivity pipe only */
26 #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01
27 /* Connectivity + APDU Reader pipe */
28 #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02
30 #define ST21NFCA_SE_MODE_OFF 0x00
31 #define ST21NFCA_SE_MODE_ON 0x01
33 #define ST21NFCA_PARAM_ATR 0x01
34 #define ST21NFCA_ATR_DEFAULT_BWI 0x04
37 * WT = 2^BWI/10[s], convert into msecs and add a secure
38 * room by increasing by 2 this timeout
40 #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
41 #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4)
43 /* If TA is present bit 0 is set */
44 #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01)
45 /* If TB is present bit 1 is set */
46 #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02)
48 static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev)
52 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
54 /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
55 for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) {
56 td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
57 if (ST21NFCA_ATR_TA_PRESENT(td))
59 if (ST21NFCA_ATR_TB_PRESENT(td)) {
61 return info->se_info.atr[i] >> 4;
64 return ST21NFCA_ATR_DEFAULT_BWI;
67 static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev)
71 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
73 r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE,
74 ST21NFCA_PARAM_ATR, &skb);
78 if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) {
79 memcpy(info->se_info.atr, skb->data, skb->len);
80 info->se_info.wt_timeout =
81 ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev));
86 static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx,
89 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
91 struct sk_buff *sk_host_list;
95 case NFC_HCI_UICC_HOST_ID:
96 se_event = (state == ST21NFCA_SE_MODE_ON ?
97 ST21NFCA_EVT_UICC_ACTIVATE :
98 ST21NFCA_EVT_UICC_DEACTIVATE);
100 info->se_info.count_pipes = 0;
101 info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC;
103 case ST21NFCA_ESE_HOST_ID:
104 se_event = (state == ST21NFCA_SE_MODE_ON ?
105 ST21NFCA_EVT_SE_ACTIVATE :
106 ST21NFCA_EVT_SE_DEACTIVATE);
108 info->se_info.count_pipes = 0;
109 info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED;
116 * Wait for an EVT_HOT_PLUG in order to
117 * retrieve a relevant host list.
119 reinit_completion(&info->se_info.req_completion);
120 r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event,
125 mod_timer(&info->se_info.se_active_timer, jiffies +
126 msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG));
127 info->se_info.se_active = true;
129 /* Ignore return value and check in any case the host_list */
130 wait_for_completion_interruptible(&info->se_info.req_completion);
132 r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
133 NFC_HCI_ADMIN_HOST_LIST,
138 for (i = 0; i < sk_host_list->len &&
139 sk_host_list->data[i] != se_idx; i++)
141 host_id = sk_host_list->data[i];
142 kfree_skb(sk_host_list);
144 if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx)
146 else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx)
152 int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev)
154 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
157 if (test_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks))
160 if (info->se_status->is_uicc_present) {
161 nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC);
165 if (info->se_status->is_ese_present) {
166 nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED);
172 EXPORT_SYMBOL(st21nfca_hci_discover_se);
174 int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
179 * According to upper layer, se_idx == NFC_SE_UICC when
180 * info->se_status->is_uicc_enable is true should never happen.
183 r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON);
184 if (r == ST21NFCA_ESE_HOST_ID) {
185 st21nfca_se_get_atr(hdev);
186 r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
187 ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
192 * The activation tentative failed, the secure element
193 * is not connected. Remove from the list.
195 nfc_remove_se(hdev->ndev, se_idx);
201 EXPORT_SYMBOL(st21nfca_hci_enable_se);
203 int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
208 * According to upper layer, se_idx == NFC_SE_UICC when
209 * info->se_status->is_uicc_enable is true should never happen
212 r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF);
218 EXPORT_SYMBOL(st21nfca_hci_disable_se);
220 int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
221 u8 *apdu, size_t apdu_length,
222 se_io_cb_t cb, void *cb_context)
224 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
226 pr_debug("se_io %x\n", se_idx);
229 case ST21NFCA_ESE_HOST_ID:
230 info->se_info.cb = cb;
231 info->se_info.cb_context = cb_context;
232 mod_timer(&info->se_info.bwi_timer, jiffies +
233 msecs_to_jiffies(info->se_info.wt_timeout));
234 info->se_info.bwi_active = true;
235 return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
236 ST21NFCA_EVT_TRANSMIT_DATA,
239 /* Need to free cb_context here as at the moment we can't
240 * clearly indicate to the caller if the callback function
241 * would be called (and free it) or not. In both cases a
242 * negative value may be returned to the caller.
248 EXPORT_SYMBOL(st21nfca_hci_se_io);
250 static void st21nfca_se_wt_work(struct work_struct *work)
253 * No answer from the secure element
254 * within the defined timeout.
255 * Let's send a reset request as recovery procedure.
256 * According to the situation, we first try to send a software reset
257 * to the secure element. If the next command is still not
258 * answering in time, we send to the CLF a secure element hardware
261 /* hardware reset managed through VCC_UICC_OUT power supply */
263 struct st21nfca_hci_info *info = container_of(work,
264 struct st21nfca_hci_info,
265 se_info.timeout_work);
269 info->se_info.bwi_active = false;
271 if (!info->se_info.xch_error) {
272 info->se_info.xch_error = true;
273 nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE,
274 ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
276 info->se_info.xch_error = false;
277 nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
278 ST21NFCA_EVT_SE_HARD_RESET, ¶m, 1);
280 info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
283 static void st21nfca_se_wt_timeout(struct timer_list *t)
285 struct st21nfca_hci_info *info = from_timer(info, t, se_info.bwi_timer);
287 schedule_work(&info->se_info.timeout_work);
290 static void st21nfca_se_activation_timeout(struct timer_list *t)
292 struct st21nfca_hci_info *info = from_timer(info, t,
293 se_info.se_active_timer);
297 info->se_info.se_active = false;
299 complete(&info->se_info.req_completion);
304 * <= 0: driver handled the event, skb consumed
305 * 1: driver does not handle the event, please do standard processing
307 int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
308 u8 event, struct sk_buff *skb)
311 struct device *dev = &hdev->ndev->dev;
312 struct nfc_evt_transaction *transaction;
316 pr_debug("connectivity gate event: %x\n", event);
319 case ST21NFCA_EVT_CONNECTIVITY:
320 r = nfc_se_connectivity(hdev->ndev, host);
322 case ST21NFCA_EVT_TRANSACTION:
323 /* According to specification etsi 102 622
324 * 11.2.2.4 EVT_TRANSACTION Table 52
325 * Description Tag Length
327 * PARAMETERS 82 0 to 255
329 * The key differences are aid storage length is variably sized
330 * in the packet, but fixed in nfc_evt_transaction, and that the aid_len
331 * is u8 in the packet, but u32 in the structure, and the tags in
332 * the packet are not included in nfc_evt_transaction.
334 * size in bytes: 1 1 5-16 1 1 0-255
335 * offset: 0 1 2 aid_len + 2 aid_len + 3 aid_len + 4
336 * member name: aid_tag(M) aid_len aid params_tag(M) params_len params
337 * example: 0x81 5-16 X 0x82 0-255 X
339 if (skb->len < 2 || skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
342 aid_len = skb->data[1];
344 if (skb->len < aid_len + 4 || aid_len > sizeof(transaction->aid))
347 params_len = skb->data[aid_len + 3];
349 /* Verify PARAMETERS tag is (82), and final check that there is enough
350 * space in the packet to read everything.
352 if ((skb->data[aid_len + 2] != NFC_EVT_TRANSACTION_PARAMS_TAG) ||
353 (skb->len < aid_len + 4 + params_len))
356 transaction = devm_kzalloc(dev, sizeof(*transaction) + params_len, GFP_KERNEL);
360 transaction->aid_len = aid_len;
361 transaction->params_len = params_len;
363 memcpy(transaction->aid, &skb->data[2], aid_len);
364 memcpy(transaction->params, &skb->data[aid_len + 4], params_len);
366 r = nfc_se_transaction(hdev->ndev, host, transaction);
369 nfc_err(&hdev->ndev->dev, "Unexpected event on connectivity gate\n");
375 EXPORT_SYMBOL(st21nfca_connectivity_event_received);
377 int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
378 u8 event, struct sk_buff *skb)
381 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
383 pr_debug("apdu reader gate event: %x\n", event);
386 case ST21NFCA_EVT_TRANSMIT_DATA:
387 del_timer_sync(&info->se_info.bwi_timer);
388 cancel_work_sync(&info->se_info.timeout_work);
389 info->se_info.bwi_active = false;
390 r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE,
391 ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
395 info->se_info.cb(info->se_info.cb_context,
396 skb->data, skb->len, 0);
398 case ST21NFCA_EVT_WTX_REQUEST:
399 mod_timer(&info->se_info.bwi_timer, jiffies +
400 msecs_to_jiffies(info->se_info.wt_timeout));
403 nfc_err(&hdev->ndev->dev, "Unexpected event on apdu reader gate\n");
411 EXPORT_SYMBOL(st21nfca_apdu_reader_event_received);
413 void st21nfca_se_init(struct nfc_hci_dev *hdev)
415 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
417 init_completion(&info->se_info.req_completion);
418 INIT_WORK(&info->se_info.timeout_work, st21nfca_se_wt_work);
419 /* initialize timers */
420 timer_setup(&info->se_info.bwi_timer, st21nfca_se_wt_timeout, 0);
421 info->se_info.bwi_active = false;
423 timer_setup(&info->se_info.se_active_timer,
424 st21nfca_se_activation_timeout, 0);
425 info->se_info.se_active = false;
427 info->se_info.count_pipes = 0;
428 info->se_info.expected_pipes = 0;
430 info->se_info.xch_error = false;
432 info->se_info.wt_timeout =
433 ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI);
435 EXPORT_SYMBOL(st21nfca_se_init);
437 void st21nfca_se_deinit(struct nfc_hci_dev *hdev)
439 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
441 if (info->se_info.bwi_active)
442 del_timer_sync(&info->se_info.bwi_timer);
443 if (info->se_info.se_active)
444 del_timer_sync(&info->se_info.se_active_timer);
446 cancel_work_sync(&info->se_info.timeout_work);
447 info->se_info.bwi_active = false;
448 info->se_info.se_active = false;
450 EXPORT_SYMBOL(st21nfca_se_deinit);