se.c 11 KB

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