se.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  4. */
  5. #include <net/nfc/hci.h>
  6. #include "st21nfca.h"
  7. #define ST21NFCA_EVT_UICC_ACTIVATE 0x10
  8. #define ST21NFCA_EVT_UICC_DEACTIVATE 0x13
  9. #define ST21NFCA_EVT_SE_HARD_RESET 0x20
  10. #define ST21NFCA_EVT_SE_SOFT_RESET 0x11
  11. #define ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER 0x21
  12. #define ST21NFCA_EVT_SE_ACTIVATE 0x22
  13. #define ST21NFCA_EVT_SE_DEACTIVATE 0x23
  14. #define ST21NFCA_EVT_TRANSMIT_DATA 0x10
  15. #define ST21NFCA_EVT_WTX_REQUEST 0x11
  16. #define ST21NFCA_EVT_CONNECTIVITY 0x10
  17. #define ST21NFCA_EVT_TRANSACTION 0x12
  18. #define ST21NFCA_SE_TO_HOT_PLUG 1000
  19. /* Connectivity pipe only */
  20. #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01
  21. /* Connectivity + APDU Reader pipe */
  22. #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02
  23. #define ST21NFCA_SE_MODE_OFF 0x00
  24. #define ST21NFCA_SE_MODE_ON 0x01
  25. #define ST21NFCA_PARAM_ATR 0x01
  26. #define ST21NFCA_ATR_DEFAULT_BWI 0x04
  27. /*
  28. * WT = 2^BWI/10[s], convert into msecs and add a secure
  29. * room by increasing by 2 this timeout
  30. */
  31. #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
  32. #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4)
  33. /* If TA is present bit 0 is set */
  34. #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01)
  35. /* If TB is present bit 1 is set */
  36. #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02)
  37. static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev)
  38. {
  39. int i;
  40. u8 td;
  41. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  42. /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
  43. for (i = 1; i < ST21NFCA_ESE_MAX_LENGTH; i++) {
  44. td = ST21NFCA_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
  45. if (ST21NFCA_ATR_TA_PRESENT(td))
  46. i++;
  47. if (ST21NFCA_ATR_TB_PRESENT(td)) {
  48. i++;
  49. return info->se_info.atr[i] >> 4;
  50. }
  51. }
  52. return ST21NFCA_ATR_DEFAULT_BWI;
  53. }
  54. static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev)
  55. {
  56. int r;
  57. struct sk_buff *skb;
  58. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  59. r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE,
  60. ST21NFCA_PARAM_ATR, &skb);
  61. if (r < 0)
  62. return;
  63. if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) {
  64. memcpy(info->se_info.atr, skb->data, skb->len);
  65. info->se_info.wt_timeout =
  66. ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev));
  67. }
  68. kfree_skb(skb);
  69. }
  70. static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx,
  71. u8 state)
  72. {
  73. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  74. int r, i;
  75. struct sk_buff *sk_host_list;
  76. u8 se_event, host_id;
  77. switch (se_idx) {
  78. case NFC_HCI_UICC_HOST_ID:
  79. se_event = (state == ST21NFCA_SE_MODE_ON ?
  80. ST21NFCA_EVT_UICC_ACTIVATE :
  81. ST21NFCA_EVT_UICC_DEACTIVATE);
  82. info->se_info.count_pipes = 0;
  83. info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC;
  84. break;
  85. case ST21NFCA_ESE_HOST_ID:
  86. se_event = (state == ST21NFCA_SE_MODE_ON ?
  87. ST21NFCA_EVT_SE_ACTIVATE :
  88. ST21NFCA_EVT_SE_DEACTIVATE);
  89. info->se_info.count_pipes = 0;
  90. info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED;
  91. break;
  92. default:
  93. return -EINVAL;
  94. }
  95. /*
  96. * Wait for an EVT_HOT_PLUG in order to
  97. * retrieve a relevant host list.
  98. */
  99. reinit_completion(&info->se_info.req_completion);
  100. r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event,
  101. NULL, 0);
  102. if (r < 0)
  103. return r;
  104. mod_timer(&info->se_info.se_active_timer, jiffies +
  105. msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG));
  106. info->se_info.se_active = true;
  107. /* Ignore return value and check in any case the host_list */
  108. wait_for_completion_interruptible(&info->se_info.req_completion);
  109. r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
  110. NFC_HCI_ADMIN_HOST_LIST,
  111. &sk_host_list);
  112. if (r < 0)
  113. return r;
  114. for (i = 0; i < sk_host_list->len &&
  115. sk_host_list->data[i] != se_idx; i++)
  116. ;
  117. host_id = sk_host_list->data[i];
  118. kfree_skb(sk_host_list);
  119. if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx)
  120. return se_idx;
  121. else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx)
  122. return se_idx;
  123. return -1;
  124. }
  125. int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev)
  126. {
  127. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  128. int se_count = 0;
  129. if (test_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks))
  130. return 0;
  131. if (info->se_status->is_uicc_present) {
  132. nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC);
  133. se_count++;
  134. }
  135. if (info->se_status->is_ese_present) {
  136. nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED);
  137. se_count++;
  138. }
  139. return !se_count;
  140. }
  141. EXPORT_SYMBOL(st21nfca_hci_discover_se);
  142. int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
  143. {
  144. int r;
  145. /*
  146. * According to upper layer, se_idx == NFC_SE_UICC when
  147. * info->se_status->is_uicc_enable is true should never happen.
  148. * Same for eSE.
  149. */
  150. r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON);
  151. if (r == ST21NFCA_ESE_HOST_ID) {
  152. st21nfca_se_get_atr(hdev);
  153. r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
  154. ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
  155. if (r < 0)
  156. return r;
  157. } else if (r < 0) {
  158. /*
  159. * The activation tentative failed, the secure element
  160. * is not connected. Remove from the list.
  161. */
  162. nfc_remove_se(hdev->ndev, se_idx);
  163. return r;
  164. }
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(st21nfca_hci_enable_se);
  168. int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
  169. {
  170. int r;
  171. /*
  172. * According to upper layer, se_idx == NFC_SE_UICC when
  173. * info->se_status->is_uicc_enable is true should never happen
  174. * Same for eSE.
  175. */
  176. r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF);
  177. if (r < 0)
  178. return r;
  179. return 0;
  180. }
  181. EXPORT_SYMBOL(st21nfca_hci_disable_se);
  182. int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
  183. u8 *apdu, size_t apdu_length,
  184. se_io_cb_t cb, void *cb_context)
  185. {
  186. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  187. pr_debug("se_io %x\n", se_idx);
  188. switch (se_idx) {
  189. case ST21NFCA_ESE_HOST_ID:
  190. info->se_info.cb = cb;
  191. info->se_info.cb_context = cb_context;
  192. mod_timer(&info->se_info.bwi_timer, jiffies +
  193. msecs_to_jiffies(info->se_info.wt_timeout));
  194. info->se_info.bwi_active = true;
  195. return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
  196. ST21NFCA_EVT_TRANSMIT_DATA,
  197. apdu, apdu_length);
  198. default:
  199. /* Need to free cb_context here as at the moment we can't
  200. * clearly indicate to the caller if the callback function
  201. * would be called (and free it) or not. In both cases a
  202. * negative value may be returned to the caller.
  203. */
  204. kfree(cb_context);
  205. return -ENODEV;
  206. }
  207. }
  208. EXPORT_SYMBOL(st21nfca_hci_se_io);
  209. static void st21nfca_se_wt_work(struct work_struct *work)
  210. {
  211. /*
  212. * No answer from the secure element
  213. * within the defined timeout.
  214. * Let's send a reset request as recovery procedure.
  215. * According to the situation, we first try to send a software reset
  216. * to the secure element. If the next command is still not
  217. * answering in time, we send to the CLF a secure element hardware
  218. * reset request.
  219. */
  220. /* hardware reset managed through VCC_UICC_OUT power supply */
  221. u8 param = 0x01;
  222. struct st21nfca_hci_info *info = container_of(work,
  223. struct st21nfca_hci_info,
  224. se_info.timeout_work);
  225. info->se_info.bwi_active = false;
  226. if (!info->se_info.xch_error) {
  227. info->se_info.xch_error = true;
  228. nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE,
  229. ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
  230. } else {
  231. info->se_info.xch_error = false;
  232. nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
  233. ST21NFCA_EVT_SE_HARD_RESET, &param, 1);
  234. }
  235. info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
  236. }
  237. static void st21nfca_se_wt_timeout(struct timer_list *t)
  238. {
  239. struct st21nfca_hci_info *info = from_timer(info, t, se_info.bwi_timer);
  240. schedule_work(&info->se_info.timeout_work);
  241. }
  242. static void st21nfca_se_activation_timeout(struct timer_list *t)
  243. {
  244. struct st21nfca_hci_info *info = from_timer(info, t,
  245. se_info.se_active_timer);
  246. info->se_info.se_active = false;
  247. complete(&info->se_info.req_completion);
  248. }
  249. /*
  250. * Returns:
  251. * <= 0: driver handled the event, skb consumed
  252. * 1: driver does not handle the event, please do standard processing
  253. */
  254. int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
  255. u8 event, struct sk_buff *skb)
  256. {
  257. int r = 0;
  258. struct device *dev = &hdev->ndev->dev;
  259. struct nfc_evt_transaction *transaction;
  260. u32 aid_len;
  261. u8 params_len;
  262. pr_debug("connectivity gate event: %x\n", event);
  263. switch (event) {
  264. case ST21NFCA_EVT_CONNECTIVITY:
  265. r = nfc_se_connectivity(hdev->ndev, host);
  266. break;
  267. case ST21NFCA_EVT_TRANSACTION:
  268. /* According to specification etsi 102 622
  269. * 11.2.2.4 EVT_TRANSACTION Table 52
  270. * Description Tag Length
  271. * AID 81 5 to 16
  272. * PARAMETERS 82 0 to 255
  273. *
  274. * The key differences are aid storage length is variably sized
  275. * in the packet, but fixed in nfc_evt_transaction, and that the aid_len
  276. * is u8 in the packet, but u32 in the structure, and the tags in
  277. * the packet are not included in nfc_evt_transaction.
  278. *
  279. * size in bytes: 1 1 5-16 1 1 0-255
  280. * offset: 0 1 2 aid_len + 2 aid_len + 3 aid_len + 4
  281. * member name: aid_tag(M) aid_len aid params_tag(M) params_len params
  282. * example: 0x81 5-16 X 0x82 0-255 X
  283. */
  284. if (skb->len < 2 || skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
  285. return -EPROTO;
  286. aid_len = skb->data[1];
  287. if (skb->len < aid_len + 4 || aid_len > sizeof(transaction->aid))
  288. return -EPROTO;
  289. params_len = skb->data[aid_len + 3];
  290. /* Verify PARAMETERS tag is (82), and final check that there is enough
  291. * space in the packet to read everything.
  292. */
  293. if ((skb->data[aid_len + 2] != NFC_EVT_TRANSACTION_PARAMS_TAG) ||
  294. (skb->len < aid_len + 4 + params_len))
  295. return -EPROTO;
  296. transaction = devm_kzalloc(dev, sizeof(*transaction) + params_len, GFP_KERNEL);
  297. if (!transaction)
  298. return -ENOMEM;
  299. transaction->aid_len = aid_len;
  300. transaction->params_len = params_len;
  301. memcpy(transaction->aid, &skb->data[2], aid_len);
  302. memcpy(transaction->params, &skb->data[aid_len + 4], params_len);
  303. r = nfc_se_transaction(hdev->ndev, host, transaction);
  304. break;
  305. default:
  306. nfc_err(&hdev->ndev->dev, "Unexpected event on connectivity gate\n");
  307. return 1;
  308. }
  309. kfree_skb(skb);
  310. return r;
  311. }
  312. EXPORT_SYMBOL(st21nfca_connectivity_event_received);
  313. int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
  314. u8 event, struct sk_buff *skb)
  315. {
  316. int r = 0;
  317. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  318. pr_debug("apdu reader gate event: %x\n", event);
  319. switch (event) {
  320. case ST21NFCA_EVT_TRANSMIT_DATA:
  321. del_timer_sync(&info->se_info.bwi_timer);
  322. cancel_work_sync(&info->se_info.timeout_work);
  323. info->se_info.bwi_active = false;
  324. r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  325. ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
  326. if (r < 0)
  327. goto exit;
  328. info->se_info.cb(info->se_info.cb_context,
  329. skb->data, skb->len, 0);
  330. break;
  331. case ST21NFCA_EVT_WTX_REQUEST:
  332. mod_timer(&info->se_info.bwi_timer, jiffies +
  333. msecs_to_jiffies(info->se_info.wt_timeout));
  334. break;
  335. default:
  336. nfc_err(&hdev->ndev->dev, "Unexpected event on apdu reader gate\n");
  337. return 1;
  338. }
  339. exit:
  340. kfree_skb(skb);
  341. return r;
  342. }
  343. EXPORT_SYMBOL(st21nfca_apdu_reader_event_received);
  344. void st21nfca_se_init(struct nfc_hci_dev *hdev)
  345. {
  346. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  347. init_completion(&info->se_info.req_completion);
  348. INIT_WORK(&info->se_info.timeout_work, st21nfca_se_wt_work);
  349. /* initialize timers */
  350. timer_setup(&info->se_info.bwi_timer, st21nfca_se_wt_timeout, 0);
  351. info->se_info.bwi_active = false;
  352. timer_setup(&info->se_info.se_active_timer,
  353. st21nfca_se_activation_timeout, 0);
  354. info->se_info.se_active = false;
  355. info->se_info.count_pipes = 0;
  356. info->se_info.expected_pipes = 0;
  357. info->se_info.xch_error = false;
  358. info->se_info.wt_timeout =
  359. ST21NFCA_BWI_TO_TIMEOUT(ST21NFCA_ATR_DEFAULT_BWI);
  360. }
  361. EXPORT_SYMBOL(st21nfca_se_init);
  362. void st21nfca_se_deinit(struct nfc_hci_dev *hdev)
  363. {
  364. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  365. if (info->se_info.bwi_active)
  366. del_timer_sync(&info->se_info.bwi_timer);
  367. if (info->se_info.se_active)
  368. del_timer_sync(&info->se_info.se_active_timer);
  369. cancel_work_sync(&info->se_info.timeout_work);
  370. info->se_info.bwi_active = false;
  371. info->se_info.se_active = false;
  372. }
  373. EXPORT_SYMBOL(st21nfca_se_deinit);