se.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Secure Element driver for STMicroelectronics NFC NCI chip
  3. *
  4. * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/nfc.h>
  20. #include <linux/delay.h>
  21. #include <net/nfc/nci.h>
  22. #include <net/nfc/nci_core.h>
  23. #include "st-nci.h"
  24. struct st_nci_pipe_info {
  25. u8 pipe_state;
  26. u8 src_host_id;
  27. u8 src_gate_id;
  28. u8 dst_host_id;
  29. u8 dst_gate_id;
  30. } __packed;
  31. /* Hosts */
  32. #define ST_NCI_HOST_CONTROLLER_ID 0x00
  33. #define ST_NCI_TERMINAL_HOST_ID 0x01
  34. #define ST_NCI_UICC_HOST_ID 0x02
  35. #define ST_NCI_ESE_HOST_ID 0xc0
  36. /* Gates */
  37. #define ST_NCI_APDU_READER_GATE 0xf0
  38. #define ST_NCI_CONNECTIVITY_GATE 0x41
  39. /* Pipes */
  40. #define ST_NCI_DEVICE_MGNT_PIPE 0x02
  41. /* Connectivity pipe only */
  42. #define ST_NCI_SE_COUNT_PIPE_UICC 0x01
  43. /* Connectivity + APDU Reader pipe */
  44. #define ST_NCI_SE_COUNT_PIPE_EMBEDDED 0x02
  45. #define ST_NCI_SE_TO_HOT_PLUG 1000 /* msecs */
  46. #define ST_NCI_SE_TO_PIPES 2000
  47. #define ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
  48. #define NCI_HCI_APDU_PARAM_ATR 0x01
  49. #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
  50. #define NCI_HCI_ADMIN_PARAM_WHITELIST 0x03
  51. #define NCI_HCI_ADMIN_PARAM_HOST_LIST 0x04
  52. #define ST_NCI_EVT_SE_HARD_RESET 0x20
  53. #define ST_NCI_EVT_TRANSMIT_DATA 0x10
  54. #define ST_NCI_EVT_WTX_REQUEST 0x11
  55. #define ST_NCI_EVT_SE_SOFT_RESET 0x11
  56. #define ST_NCI_EVT_SE_END_OF_APDU_TRANSFER 0x21
  57. #define ST_NCI_EVT_HOT_PLUG 0x03
  58. #define ST_NCI_SE_MODE_OFF 0x00
  59. #define ST_NCI_SE_MODE_ON 0x01
  60. #define ST_NCI_EVT_CONNECTIVITY 0x10
  61. #define ST_NCI_EVT_TRANSACTION 0x12
  62. #define ST_NCI_DM_GETINFO 0x13
  63. #define ST_NCI_DM_GETINFO_PIPE_LIST 0x02
  64. #define ST_NCI_DM_GETINFO_PIPE_INFO 0x01
  65. #define ST_NCI_DM_PIPE_CREATED 0x02
  66. #define ST_NCI_DM_PIPE_OPEN 0x04
  67. #define ST_NCI_DM_RF_ACTIVE 0x80
  68. #define ST_NCI_DM_DISCONNECT 0x30
  69. #define ST_NCI_DM_IS_PIPE_OPEN(p) \
  70. ((p & 0x0f) == (ST_NCI_DM_PIPE_CREATED | ST_NCI_DM_PIPE_OPEN))
  71. #define ST_NCI_ATR_DEFAULT_BWI 0x04
  72. /*
  73. * WT = 2^BWI/10[s], convert into msecs and add a secure
  74. * room by increasing by 2 this timeout
  75. */
  76. #define ST_NCI_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
  77. #define ST_NCI_ATR_GET_Y_FROM_TD(x) (x >> 4)
  78. /* If TA is present bit 0 is set */
  79. #define ST_NCI_ATR_TA_PRESENT(x) (x & 0x01)
  80. /* If TB is present bit 1 is set */
  81. #define ST_NCI_ATR_TB_PRESENT(x) (x & 0x02)
  82. #define ST_NCI_NUM_DEVICES 256
  83. static DECLARE_BITMAP(dev_mask, ST_NCI_NUM_DEVICES);
  84. /* Here are the mandatory pipe for st_nci */
  85. static struct nci_hci_gate st_nci_gates[] = {
  86. {NCI_HCI_ADMIN_GATE, NCI_HCI_ADMIN_PIPE,
  87. ST_NCI_HOST_CONTROLLER_ID},
  88. {NCI_HCI_LINK_MGMT_GATE, NCI_HCI_LINK_MGMT_PIPE,
  89. ST_NCI_HOST_CONTROLLER_ID},
  90. {ST_NCI_DEVICE_MGNT_GATE, ST_NCI_DEVICE_MGNT_PIPE,
  91. ST_NCI_HOST_CONTROLLER_ID},
  92. {NCI_HCI_IDENTITY_MGMT_GATE, NCI_HCI_INVALID_PIPE,
  93. ST_NCI_HOST_CONTROLLER_ID},
  94. /* Secure element pipes are created by secure element host */
  95. {ST_NCI_CONNECTIVITY_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
  96. ST_NCI_HOST_CONTROLLER_ID},
  97. {ST_NCI_APDU_READER_GATE, NCI_HCI_DO_NOT_OPEN_PIPE,
  98. ST_NCI_HOST_CONTROLLER_ID},
  99. };
  100. static u8 st_nci_se_get_bwi(struct nci_dev *ndev)
  101. {
  102. int i;
  103. u8 td;
  104. struct st_nci_info *info = nci_get_drvdata(ndev);
  105. /* Bits 8 to 5 of the first TB for T=1 encode BWI from zero to nine */
  106. for (i = 1; i < ST_NCI_ESE_MAX_LENGTH; i++) {
  107. td = ST_NCI_ATR_GET_Y_FROM_TD(info->se_info.atr[i]);
  108. if (ST_NCI_ATR_TA_PRESENT(td))
  109. i++;
  110. if (ST_NCI_ATR_TB_PRESENT(td)) {
  111. i++;
  112. return info->se_info.atr[i] >> 4;
  113. }
  114. }
  115. return ST_NCI_ATR_DEFAULT_BWI;
  116. }
  117. static void st_nci_se_get_atr(struct nci_dev *ndev)
  118. {
  119. struct st_nci_info *info = nci_get_drvdata(ndev);
  120. int r;
  121. struct sk_buff *skb;
  122. r = nci_hci_get_param(ndev, ST_NCI_APDU_READER_GATE,
  123. NCI_HCI_APDU_PARAM_ATR, &skb);
  124. if (r < 0)
  125. return;
  126. if (skb->len <= ST_NCI_ESE_MAX_LENGTH) {
  127. memcpy(info->se_info.atr, skb->data, skb->len);
  128. info->se_info.wt_timeout =
  129. ST_NCI_BWI_TO_TIMEOUT(st_nci_se_get_bwi(ndev));
  130. }
  131. kfree_skb(skb);
  132. }
  133. int st_nci_hci_load_session(struct nci_dev *ndev)
  134. {
  135. int i, j, r;
  136. struct sk_buff *skb_pipe_list, *skb_pipe_info;
  137. struct st_nci_pipe_info *dm_pipe_info;
  138. u8 pipe_list[] = { ST_NCI_DM_GETINFO_PIPE_LIST,
  139. ST_NCI_TERMINAL_HOST_ID};
  140. u8 pipe_info[] = { ST_NCI_DM_GETINFO_PIPE_INFO,
  141. ST_NCI_TERMINAL_HOST_ID, 0};
  142. /* On ST_NCI device pipes number are dynamics
  143. * If pipes are already created, hci_dev_up will fail.
  144. * Doing a clear all pipe is a bad idea because:
  145. * - It does useless EEPROM cycling
  146. * - It might cause issue for secure elements support
  147. * (such as removing connectivity or APDU reader pipe)
  148. * A better approach on ST_NCI is to:
  149. * - get a pipe list for each host.
  150. * (eg: ST_NCI_HOST_CONTROLLER_ID for now).
  151. * (TODO Later on UICC HOST and eSE HOST)
  152. * - get pipe information
  153. * - match retrieved pipe list in st_nci_gates
  154. * ST_NCI_DEVICE_MGNT_GATE is a proprietary gate
  155. * with ST_NCI_DEVICE_MGNT_PIPE.
  156. * Pipe can be closed and need to be open.
  157. */
  158. r = nci_hci_connect_gate(ndev, ST_NCI_HOST_CONTROLLER_ID,
  159. ST_NCI_DEVICE_MGNT_GATE,
  160. ST_NCI_DEVICE_MGNT_PIPE);
  161. if (r < 0)
  162. return r;
  163. /* Get pipe list */
  164. r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE,
  165. ST_NCI_DM_GETINFO, pipe_list, sizeof(pipe_list),
  166. &skb_pipe_list);
  167. if (r < 0)
  168. return r;
  169. /* Complete the existing gate_pipe table */
  170. for (i = 0; i < skb_pipe_list->len; i++) {
  171. pipe_info[2] = skb_pipe_list->data[i];
  172. r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE,
  173. ST_NCI_DM_GETINFO, pipe_info,
  174. sizeof(pipe_info), &skb_pipe_info);
  175. if (r)
  176. continue;
  177. /*
  178. * Match pipe ID and gate ID
  179. * Output format from ST21NFC_DM_GETINFO is:
  180. * - pipe state (1byte)
  181. * - source hid (1byte)
  182. * - source gid (1byte)
  183. * - destination hid (1byte)
  184. * - destination gid (1byte)
  185. */
  186. dm_pipe_info = (struct st_nci_pipe_info *)skb_pipe_info->data;
  187. if (dm_pipe_info->dst_gate_id == ST_NCI_APDU_READER_GATE &&
  188. dm_pipe_info->src_host_id == ST_NCI_UICC_HOST_ID) {
  189. pr_err("Unexpected apdu_reader pipe on host %x\n",
  190. dm_pipe_info->src_host_id);
  191. kfree_skb(skb_pipe_info);
  192. continue;
  193. }
  194. for (j = 3; (j < ARRAY_SIZE(st_nci_gates)) &&
  195. (st_nci_gates[j].gate != dm_pipe_info->dst_gate_id); j++)
  196. ;
  197. if (j < ARRAY_SIZE(st_nci_gates) &&
  198. st_nci_gates[j].gate == dm_pipe_info->dst_gate_id &&
  199. ST_NCI_DM_IS_PIPE_OPEN(dm_pipe_info->pipe_state)) {
  200. ndev->hci_dev->init_data.gates[j].pipe = pipe_info[2];
  201. ndev->hci_dev->gate2pipe[st_nci_gates[j].gate] =
  202. pipe_info[2];
  203. ndev->hci_dev->pipes[pipe_info[2]].gate =
  204. st_nci_gates[j].gate;
  205. ndev->hci_dev->pipes[pipe_info[2]].host =
  206. dm_pipe_info->src_host_id;
  207. }
  208. kfree_skb(skb_pipe_info);
  209. }
  210. /*
  211. * 3 gates have a well known pipe ID. Only NCI_HCI_LINK_MGMT_GATE
  212. * is not yet open at this stage.
  213. */
  214. r = nci_hci_connect_gate(ndev, ST_NCI_HOST_CONTROLLER_ID,
  215. NCI_HCI_LINK_MGMT_GATE,
  216. NCI_HCI_LINK_MGMT_PIPE);
  217. kfree_skb(skb_pipe_list);
  218. return r;
  219. }
  220. EXPORT_SYMBOL_GPL(st_nci_hci_load_session);
  221. static void st_nci_hci_admin_event_received(struct nci_dev *ndev,
  222. u8 event, struct sk_buff *skb)
  223. {
  224. struct st_nci_info *info = nci_get_drvdata(ndev);
  225. switch (event) {
  226. case ST_NCI_EVT_HOT_PLUG:
  227. if (info->se_info.se_active) {
  228. if (!ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
  229. del_timer_sync(&info->se_info.se_active_timer);
  230. info->se_info.se_active = false;
  231. complete(&info->se_info.req_completion);
  232. } else {
  233. mod_timer(&info->se_info.se_active_timer,
  234. jiffies +
  235. msecs_to_jiffies(ST_NCI_SE_TO_PIPES));
  236. }
  237. }
  238. break;
  239. default:
  240. nfc_err(&ndev->nfc_dev->dev, "Unexpected event on admin gate\n");
  241. }
  242. }
  243. static int st_nci_hci_apdu_reader_event_received(struct nci_dev *ndev,
  244. u8 event,
  245. struct sk_buff *skb)
  246. {
  247. int r = 0;
  248. struct st_nci_info *info = nci_get_drvdata(ndev);
  249. pr_debug("apdu reader gate event: %x\n", event);
  250. switch (event) {
  251. case ST_NCI_EVT_TRANSMIT_DATA:
  252. del_timer_sync(&info->se_info.bwi_timer);
  253. info->se_info.bwi_active = false;
  254. info->se_info.cb(info->se_info.cb_context,
  255. skb->data, skb->len, 0);
  256. break;
  257. case ST_NCI_EVT_WTX_REQUEST:
  258. mod_timer(&info->se_info.bwi_timer, jiffies +
  259. msecs_to_jiffies(info->se_info.wt_timeout));
  260. break;
  261. default:
  262. nfc_err(&ndev->nfc_dev->dev, "Unexpected event on apdu reader gate\n");
  263. return 1;
  264. }
  265. kfree_skb(skb);
  266. return r;
  267. }
  268. /*
  269. * Returns:
  270. * <= 0: driver handled the event, skb consumed
  271. * 1: driver does not handle the event, please do standard processing
  272. */
  273. static int st_nci_hci_connectivity_event_received(struct nci_dev *ndev,
  274. u8 host, u8 event,
  275. struct sk_buff *skb)
  276. {
  277. int r = 0;
  278. struct device *dev = &ndev->nfc_dev->dev;
  279. struct nfc_evt_transaction *transaction;
  280. pr_debug("connectivity gate event: %x\n", event);
  281. switch (event) {
  282. case ST_NCI_EVT_CONNECTIVITY:
  283. r = nfc_se_connectivity(ndev->nfc_dev, host);
  284. break;
  285. case ST_NCI_EVT_TRANSACTION:
  286. /* According to specification etsi 102 622
  287. * 11.2.2.4 EVT_TRANSACTION Table 52
  288. * Description Tag Length
  289. * AID 81 5 to 16
  290. * PARAMETERS 82 0 to 255
  291. */
  292. if (skb->len < NFC_MIN_AID_LENGTH + 2 &&
  293. skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
  294. return -EPROTO;
  295. transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
  296. skb->len - 2, GFP_KERNEL);
  297. if (!transaction)
  298. return -ENOMEM;
  299. transaction->aid_len = skb->data[1];
  300. memcpy(transaction->aid, &skb->data[2], transaction->aid_len);
  301. /* Check next byte is PARAMETERS tag (82) */
  302. if (skb->data[transaction->aid_len + 2] !=
  303. NFC_EVT_TRANSACTION_PARAMS_TAG)
  304. return -EPROTO;
  305. transaction->params_len = skb->data[transaction->aid_len + 3];
  306. memcpy(transaction->params, skb->data +
  307. transaction->aid_len + 4, transaction->params_len);
  308. r = nfc_se_transaction(ndev->nfc_dev, host, transaction);
  309. break;
  310. default:
  311. nfc_err(&ndev->nfc_dev->dev, "Unexpected event on connectivity gate\n");
  312. return 1;
  313. }
  314. kfree_skb(skb);
  315. return r;
  316. }
  317. void st_nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
  318. u8 event, struct sk_buff *skb)
  319. {
  320. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  321. u8 host = ndev->hci_dev->pipes[pipe].host;
  322. switch (gate) {
  323. case NCI_HCI_ADMIN_GATE:
  324. st_nci_hci_admin_event_received(ndev, event, skb);
  325. break;
  326. case ST_NCI_APDU_READER_GATE:
  327. st_nci_hci_apdu_reader_event_received(ndev, event, skb);
  328. break;
  329. case ST_NCI_CONNECTIVITY_GATE:
  330. st_nci_hci_connectivity_event_received(ndev, host, event, skb);
  331. break;
  332. }
  333. }
  334. EXPORT_SYMBOL_GPL(st_nci_hci_event_received);
  335. void st_nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
  336. struct sk_buff *skb)
  337. {
  338. struct st_nci_info *info = nci_get_drvdata(ndev);
  339. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  340. pr_debug("cmd: %x\n", cmd);
  341. switch (cmd) {
  342. case NCI_HCI_ANY_OPEN_PIPE:
  343. if (gate != ST_NCI_APDU_READER_GATE &&
  344. ndev->hci_dev->pipes[pipe].host != ST_NCI_UICC_HOST_ID)
  345. ndev->hci_dev->count_pipes++;
  346. if (ndev->hci_dev->count_pipes ==
  347. ndev->hci_dev->expected_pipes) {
  348. del_timer_sync(&info->se_info.se_active_timer);
  349. info->se_info.se_active = false;
  350. ndev->hci_dev->count_pipes = 0;
  351. complete(&info->se_info.req_completion);
  352. }
  353. break;
  354. }
  355. }
  356. EXPORT_SYMBOL_GPL(st_nci_hci_cmd_received);
  357. static int st_nci_control_se(struct nci_dev *ndev, u8 se_idx,
  358. u8 state)
  359. {
  360. struct st_nci_info *info = nci_get_drvdata(ndev);
  361. int r, i;
  362. struct sk_buff *sk_host_list;
  363. u8 host_id;
  364. switch (se_idx) {
  365. case ST_NCI_UICC_HOST_ID:
  366. ndev->hci_dev->count_pipes = 0;
  367. ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_UICC;
  368. break;
  369. case ST_NCI_ESE_HOST_ID:
  370. ndev->hci_dev->count_pipes = 0;
  371. ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_EMBEDDED;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. /*
  377. * Wait for an EVT_HOT_PLUG in order to
  378. * retrieve a relevant host list.
  379. */
  380. reinit_completion(&info->se_info.req_completion);
  381. r = nci_nfcee_mode_set(ndev, se_idx, state);
  382. if (r != NCI_STATUS_OK)
  383. return r;
  384. mod_timer(&info->se_info.se_active_timer, jiffies +
  385. msecs_to_jiffies(ST_NCI_SE_TO_HOT_PLUG));
  386. info->se_info.se_active = true;
  387. /* Ignore return value and check in any case the host_list */
  388. wait_for_completion_interruptible(&info->se_info.req_completion);
  389. /* There might be some "collision" after receiving a HOT_PLUG event
  390. * This may cause the CLF to not answer to the next hci command.
  391. * There is no possible synchronization to prevent this.
  392. * Adding a small delay is the only way to solve the issue.
  393. */
  394. if (info->se_info.se_status->is_ese_present &&
  395. info->se_info.se_status->is_uicc_present)
  396. usleep_range(15000, 20000);
  397. r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
  398. NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list);
  399. if (r != NCI_HCI_ANY_OK)
  400. return r;
  401. for (i = 0; i < sk_host_list->len &&
  402. sk_host_list->data[i] != se_idx; i++)
  403. ;
  404. host_id = sk_host_list->data[i];
  405. kfree_skb(sk_host_list);
  406. if (state == ST_NCI_SE_MODE_ON && host_id == se_idx)
  407. return se_idx;
  408. else if (state == ST_NCI_SE_MODE_OFF && host_id != se_idx)
  409. return se_idx;
  410. return -1;
  411. }
  412. int st_nci_disable_se(struct nci_dev *ndev, u32 se_idx)
  413. {
  414. int r;
  415. pr_debug("st_nci_disable_se\n");
  416. /*
  417. * According to upper layer, se_idx == NFC_SE_UICC when
  418. * info->se_info.se_status->is_uicc_enable is true should never happen
  419. * Same for eSE.
  420. */
  421. r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_OFF);
  422. if (r < 0) {
  423. /* Do best effort to release SWP */
  424. if (se_idx == NFC_SE_EMBEDDED) {
  425. r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  426. ST_NCI_EVT_SE_END_OF_APDU_TRANSFER,
  427. NULL, 0);
  428. }
  429. return r;
  430. }
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(st_nci_disable_se);
  434. int st_nci_enable_se(struct nci_dev *ndev, u32 se_idx)
  435. {
  436. int r;
  437. pr_debug("st_nci_enable_se\n");
  438. /*
  439. * According to upper layer, se_idx == NFC_SE_UICC when
  440. * info->se_info.se_status->is_uicc_enable is true should never happen.
  441. * Same for eSE.
  442. */
  443. r = st_nci_control_se(ndev, se_idx, ST_NCI_SE_MODE_ON);
  444. if (r == ST_NCI_ESE_HOST_ID) {
  445. st_nci_se_get_atr(ndev);
  446. r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  447. ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
  448. }
  449. if (r < 0) {
  450. /*
  451. * The activation procedure failed, the secure element
  452. * is not connected. Remove from the list.
  453. */
  454. nfc_remove_se(ndev->nfc_dev, se_idx);
  455. return r;
  456. }
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(st_nci_enable_se);
  460. static int st_nci_hci_network_init(struct nci_dev *ndev)
  461. {
  462. struct st_nci_info *info = nci_get_drvdata(ndev);
  463. struct core_conn_create_dest_spec_params *dest_params;
  464. struct dest_spec_params spec_params;
  465. struct nci_conn_info *conn_info;
  466. int r, dev_num;
  467. dest_params =
  468. kzalloc(sizeof(struct core_conn_create_dest_spec_params) +
  469. sizeof(struct dest_spec_params), GFP_KERNEL);
  470. if (dest_params == NULL) {
  471. r = -ENOMEM;
  472. goto exit;
  473. }
  474. dest_params->type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE;
  475. dest_params->length = sizeof(struct dest_spec_params);
  476. spec_params.id = ndev->hci_dev->nfcee_id;
  477. spec_params.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS;
  478. memcpy(dest_params->value, &spec_params,
  479. sizeof(struct dest_spec_params));
  480. r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCEE, 1,
  481. sizeof(struct core_conn_create_dest_spec_params) +
  482. sizeof(struct dest_spec_params),
  483. dest_params);
  484. if (r != NCI_STATUS_OK)
  485. goto free_dest_params;
  486. conn_info = ndev->hci_dev->conn_info;
  487. if (!conn_info)
  488. goto free_dest_params;
  489. ndev->hci_dev->init_data.gate_count = ARRAY_SIZE(st_nci_gates);
  490. memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
  491. sizeof(st_nci_gates));
  492. /*
  493. * Session id must include the driver name + i2c bus addr
  494. * persistent info to discriminate 2 identical chips
  495. */
  496. dev_num = find_first_zero_bit(dev_mask, ST_NCI_NUM_DEVICES);
  497. if (dev_num >= ST_NCI_NUM_DEVICES) {
  498. r = -ENODEV;
  499. goto free_dest_params;
  500. }
  501. scnprintf(ndev->hci_dev->init_data.session_id,
  502. sizeof(ndev->hci_dev->init_data.session_id),
  503. "%s%2x", "ST21BH", dev_num);
  504. r = nci_hci_dev_session_init(ndev);
  505. if (r != NCI_HCI_ANY_OK)
  506. goto free_dest_params;
  507. /*
  508. * In factory mode, we prevent secure elements activation
  509. * by disabling nfcee on the current HCI connection id.
  510. * HCI will be used here only for proprietary commands.
  511. */
  512. if (test_bit(ST_NCI_FACTORY_MODE, &info->flags))
  513. r = nci_nfcee_mode_set(ndev,
  514. ndev->hci_dev->conn_info->dest_params->id,
  515. NCI_NFCEE_DISABLE);
  516. else
  517. r = nci_nfcee_mode_set(ndev,
  518. ndev->hci_dev->conn_info->dest_params->id,
  519. NCI_NFCEE_ENABLE);
  520. free_dest_params:
  521. kfree(dest_params);
  522. exit:
  523. return r;
  524. }
  525. int st_nci_discover_se(struct nci_dev *ndev)
  526. {
  527. u8 white_list[2];
  528. int r, wl_size = 0;
  529. int se_count = 0;
  530. struct st_nci_info *info = nci_get_drvdata(ndev);
  531. pr_debug("st_nci_discover_se\n");
  532. r = st_nci_hci_network_init(ndev);
  533. if (r != 0)
  534. return r;
  535. if (test_bit(ST_NCI_FACTORY_MODE, &info->flags))
  536. return 0;
  537. if (info->se_info.se_status->is_uicc_present)
  538. white_list[wl_size++] = ST_NCI_UICC_HOST_ID;
  539. if (info->se_info.se_status->is_ese_present)
  540. white_list[wl_size++] = ST_NCI_ESE_HOST_ID;
  541. if (wl_size) {
  542. r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
  543. NCI_HCI_ADMIN_PARAM_WHITELIST,
  544. white_list, wl_size);
  545. if (r != NCI_HCI_ANY_OK)
  546. return r;
  547. }
  548. if (info->se_info.se_status->is_uicc_present) {
  549. nfc_add_se(ndev->nfc_dev, ST_NCI_UICC_HOST_ID, NFC_SE_UICC);
  550. se_count++;
  551. }
  552. if (info->se_info.se_status->is_ese_present) {
  553. nfc_add_se(ndev->nfc_dev, ST_NCI_ESE_HOST_ID, NFC_SE_EMBEDDED);
  554. se_count++;
  555. }
  556. return !se_count;
  557. }
  558. EXPORT_SYMBOL_GPL(st_nci_discover_se);
  559. int st_nci_se_io(struct nci_dev *ndev, u32 se_idx,
  560. u8 *apdu, size_t apdu_length,
  561. se_io_cb_t cb, void *cb_context)
  562. {
  563. struct st_nci_info *info = nci_get_drvdata(ndev);
  564. pr_debug("\n");
  565. switch (se_idx) {
  566. case ST_NCI_ESE_HOST_ID:
  567. info->se_info.cb = cb;
  568. info->se_info.cb_context = cb_context;
  569. mod_timer(&info->se_info.bwi_timer, jiffies +
  570. msecs_to_jiffies(info->se_info.wt_timeout));
  571. info->se_info.bwi_active = true;
  572. return nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  573. ST_NCI_EVT_TRANSMIT_DATA, apdu,
  574. apdu_length);
  575. default:
  576. return -ENODEV;
  577. }
  578. }
  579. EXPORT_SYMBOL(st_nci_se_io);
  580. static void st_nci_se_wt_timeout(struct timer_list *t)
  581. {
  582. /*
  583. * No answer from the secure element
  584. * within the defined timeout.
  585. * Let's send a reset request as recovery procedure.
  586. * According to the situation, we first try to send a software reset
  587. * to the secure element. If the next command is still not
  588. * answering in time, we send to the CLF a secure element hardware
  589. * reset request.
  590. */
  591. /* hardware reset managed through VCC_UICC_OUT power supply */
  592. u8 param = 0x01;
  593. struct st_nci_info *info = from_timer(info, t, se_info.bwi_timer);
  594. pr_debug("\n");
  595. info->se_info.bwi_active = false;
  596. if (!info->se_info.xch_error) {
  597. info->se_info.xch_error = true;
  598. nci_hci_send_event(info->ndlc->ndev, ST_NCI_APDU_READER_GATE,
  599. ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
  600. } else {
  601. info->se_info.xch_error = false;
  602. nci_hci_send_event(info->ndlc->ndev, ST_NCI_DEVICE_MGNT_GATE,
  603. ST_NCI_EVT_SE_HARD_RESET, &param, 1);
  604. }
  605. info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
  606. }
  607. static void st_nci_se_activation_timeout(struct timer_list *t)
  608. {
  609. struct st_nci_info *info = from_timer(info, t,
  610. se_info.se_active_timer);
  611. pr_debug("\n");
  612. info->se_info.se_active = false;
  613. complete(&info->se_info.req_completion);
  614. }
  615. int st_nci_se_init(struct nci_dev *ndev, struct st_nci_se_status *se_status)
  616. {
  617. struct st_nci_info *info = nci_get_drvdata(ndev);
  618. init_completion(&info->se_info.req_completion);
  619. /* initialize timers */
  620. timer_setup(&info->se_info.bwi_timer, st_nci_se_wt_timeout, 0);
  621. info->se_info.bwi_active = false;
  622. timer_setup(&info->se_info.se_active_timer,
  623. st_nci_se_activation_timeout, 0);
  624. info->se_info.se_active = false;
  625. info->se_info.xch_error = false;
  626. info->se_info.wt_timeout =
  627. ST_NCI_BWI_TO_TIMEOUT(ST_NCI_ATR_DEFAULT_BWI);
  628. info->se_info.se_status = se_status;
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(st_nci_se_init);
  632. void st_nci_se_deinit(struct nci_dev *ndev)
  633. {
  634. struct st_nci_info *info = nci_get_drvdata(ndev);
  635. if (info->se_info.bwi_active)
  636. del_timer_sync(&info->se_info.bwi_timer);
  637. if (info->se_info.se_active)
  638. del_timer_sync(&info->se_info.se_active_timer);
  639. info->se_info.se_active = false;
  640. info->se_info.bwi_active = false;
  641. }
  642. EXPORT_SYMBOL(st_nci_se_deinit);