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_ESE_HOST_ID 0xc0
  30. #define ST21NFCA_SE_TO_HOT_PLUG 1000
  31. /* Connectivity pipe only */
  32. #define ST21NFCA_SE_COUNT_PIPE_UICC 0x01
  33. /* Connectivity + APDU Reader pipe */
  34. #define ST21NFCA_SE_COUNT_PIPE_EMBEDDED 0x02
  35. #define ST21NFCA_SE_MODE_OFF 0x00
  36. #define ST21NFCA_SE_MODE_ON 0x01
  37. #define ST21NFCA_PARAM_ATR 0x01
  38. #define ST21NFCA_ATR_DEFAULT_BWI 0x04
  39. /*
  40. * WT = 2^BWI/10[s], convert into msecs and add a secure
  41. * room by increasing by 2 this timeout
  42. */
  43. #define ST21NFCA_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
  44. #define ST21NFCA_ATR_GET_Y_FROM_TD(x) (x >> 4)
  45. /* If TA is present bit 0 is set */
  46. #define ST21NFCA_ATR_TA_PRESENT(x) (x & 0x01)
  47. /* If TB is present bit 1 is set */
  48. #define ST21NFCA_ATR_TB_PRESENT(x) (x & 0x02)
  49. static u8 st21nfca_se_get_bwi(struct nfc_hci_dev *hdev)
  50. {
  51. int i;
  52. u8 td;
  53. 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))
  58. i++;
  59. if (ST21NFCA_ATR_TB_PRESENT(td)) {
  60. i++;
  61. return info->se_info.atr[i] >> 4;
  62. }
  63. }
  64. return ST21NFCA_ATR_DEFAULT_BWI;
  65. }
  66. static void st21nfca_se_get_atr(struct nfc_hci_dev *hdev)
  67. {
  68. int r;
  69. struct sk_buff *skb;
  70. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  71. r = nfc_hci_get_param(hdev, ST21NFCA_APDU_READER_GATE,
  72. ST21NFCA_PARAM_ATR, &skb);
  73. if (r < 0)
  74. return;
  75. if (skb->len <= ST21NFCA_ESE_MAX_LENGTH) {
  76. memcpy(info->se_info.atr, skb->data, skb->len);
  77. info->se_info.wt_timeout =
  78. ST21NFCA_BWI_TO_TIMEOUT(st21nfca_se_get_bwi(hdev));
  79. }
  80. kfree_skb(skb);
  81. }
  82. static int st21nfca_hci_control_se(struct nfc_hci_dev *hdev, u32 se_idx,
  83. u8 state)
  84. {
  85. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  86. int r, i;
  87. struct sk_buff *sk_host_list;
  88. u8 se_event, host_id;
  89. switch (se_idx) {
  90. case NFC_HCI_UICC_HOST_ID:
  91. se_event = (state == ST21NFCA_SE_MODE_ON ?
  92. ST21NFCA_EVT_UICC_ACTIVATE :
  93. ST21NFCA_EVT_UICC_DEACTIVATE);
  94. info->se_info.count_pipes = 0;
  95. info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_UICC;
  96. break;
  97. case ST21NFCA_ESE_HOST_ID:
  98. se_event = (state == ST21NFCA_SE_MODE_ON ?
  99. ST21NFCA_EVT_SE_ACTIVATE :
  100. ST21NFCA_EVT_SE_DEACTIVATE);
  101. info->se_info.count_pipes = 0;
  102. info->se_info.expected_pipes = ST21NFCA_SE_COUNT_PIPE_EMBEDDED;
  103. break;
  104. default:
  105. return -EINVAL;
  106. }
  107. /*
  108. * Wait for an EVT_HOT_PLUG in order to
  109. * retrieve a relevant host list.
  110. */
  111. reinit_completion(&info->se_info.req_completion);
  112. r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE, se_event,
  113. NULL, 0);
  114. if (r < 0)
  115. return r;
  116. mod_timer(&info->se_info.se_active_timer, jiffies +
  117. msecs_to_jiffies(ST21NFCA_SE_TO_HOT_PLUG));
  118. info->se_info.se_active = true;
  119. /* Ignore return value and check in any case the host_list */
  120. wait_for_completion_interruptible(&info->se_info.req_completion);
  121. r = nfc_hci_get_param(hdev, NFC_HCI_ADMIN_GATE,
  122. NFC_HCI_ADMIN_HOST_LIST,
  123. &sk_host_list);
  124. if (r < 0)
  125. return r;
  126. for (i = 0; i < sk_host_list->len &&
  127. sk_host_list->data[i] != se_idx; i++)
  128. ;
  129. host_id = sk_host_list->data[i];
  130. kfree_skb(sk_host_list);
  131. if (state == ST21NFCA_SE_MODE_ON && host_id == se_idx)
  132. return se_idx;
  133. else if (state == ST21NFCA_SE_MODE_OFF && host_id != se_idx)
  134. return se_idx;
  135. return -1;
  136. }
  137. int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev)
  138. {
  139. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  140. int se_count = 0;
  141. if (test_bit(ST21NFCA_FACTORY_MODE, &hdev->quirks))
  142. return 0;
  143. if (info->se_status->is_uicc_present) {
  144. nfc_add_se(hdev->ndev, NFC_HCI_UICC_HOST_ID, NFC_SE_UICC);
  145. se_count++;
  146. }
  147. if (info->se_status->is_ese_present) {
  148. nfc_add_se(hdev->ndev, ST21NFCA_ESE_HOST_ID, NFC_SE_EMBEDDED);
  149. se_count++;
  150. }
  151. return !se_count;
  152. }
  153. EXPORT_SYMBOL(st21nfca_hci_discover_se);
  154. int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
  155. {
  156. int r;
  157. /*
  158. * According to upper layer, se_idx == NFC_SE_UICC when
  159. * info->se_status->is_uicc_enable is true should never happen.
  160. * Same for eSE.
  161. */
  162. r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_ON);
  163. if (r == ST21NFCA_ESE_HOST_ID) {
  164. st21nfca_se_get_atr(hdev);
  165. r = nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
  166. ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
  167. if (r < 0)
  168. return r;
  169. } else if (r < 0) {
  170. /*
  171. * The activation tentative failed, the secure element
  172. * is not connected. Remove from the list.
  173. */
  174. nfc_remove_se(hdev->ndev, se_idx);
  175. return r;
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(st21nfca_hci_enable_se);
  180. int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
  181. {
  182. int r;
  183. /*
  184. * According to upper layer, se_idx == NFC_SE_UICC when
  185. * info->se_status->is_uicc_enable is true should never happen
  186. * Same for eSE.
  187. */
  188. r = st21nfca_hci_control_se(hdev, se_idx, ST21NFCA_SE_MODE_OFF);
  189. if (r < 0)
  190. return r;
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(st21nfca_hci_disable_se);
  194. int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
  195. u8 *apdu, size_t apdu_length,
  196. se_io_cb_t cb, void *cb_context)
  197. {
  198. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  199. pr_debug("se_io %x\n", se_idx);
  200. switch (se_idx) {
  201. case ST21NFCA_ESE_HOST_ID:
  202. info->se_info.cb = cb;
  203. info->se_info.cb_context = cb_context;
  204. mod_timer(&info->se_info.bwi_timer, jiffies +
  205. msecs_to_jiffies(info->se_info.wt_timeout));
  206. info->se_info.bwi_active = true;
  207. return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
  208. ST21NFCA_EVT_TRANSMIT_DATA,
  209. apdu, apdu_length);
  210. default:
  211. return -ENODEV;
  212. }
  213. }
  214. EXPORT_SYMBOL(st21nfca_hci_se_io);
  215. static void st21nfca_se_wt_timeout(unsigned long data)
  216. {
  217. /*
  218. * No answer from the secure element
  219. * within the defined timeout.
  220. * Let's send a reset request as recovery procedure.
  221. * According to the situation, we first try to send a software reset
  222. * to the secure element. If the next command is still not
  223. * answering in time, we send to the CLF a secure element hardware
  224. * reset request.
  225. */
  226. /* hardware reset managed through VCC_UICC_OUT power supply */
  227. u8 param = 0x01;
  228. struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data;
  229. pr_debug("\n");
  230. info->se_info.bwi_active = false;
  231. if (!info->se_info.xch_error) {
  232. info->se_info.xch_error = true;
  233. nfc_hci_send_event(info->hdev, ST21NFCA_APDU_READER_GATE,
  234. ST21NFCA_EVT_SE_SOFT_RESET, NULL, 0);
  235. } else {
  236. info->se_info.xch_error = false;
  237. nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
  238. ST21NFCA_EVT_SE_HARD_RESET, &param, 1);
  239. }
  240. info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
  241. }
  242. static void st21nfca_se_activation_timeout(unsigned long data)
  243. {
  244. struct st21nfca_hci_info *info = (struct st21nfca_hci_info *) data;
  245. pr_debug("\n");
  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. pr_debug("connectivity gate event: %x\n", event);
  261. switch (event) {
  262. case ST21NFCA_EVT_CONNECTIVITY:
  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);