st-nci_se.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #include "st-nci_se.h"
  25. struct st_nci_pipe_info {
  26. u8 pipe_state;
  27. u8 src_host_id;
  28. u8 src_gate_id;
  29. u8 dst_host_id;
  30. u8 dst_gate_id;
  31. } __packed;
  32. /* Hosts */
  33. #define ST_NCI_HOST_CONTROLLER_ID 0x00
  34. #define ST_NCI_TERMINAL_HOST_ID 0x01
  35. #define ST_NCI_UICC_HOST_ID 0x02
  36. #define ST_NCI_ESE_HOST_ID 0xc0
  37. /* Gates */
  38. #define ST_NCI_DEVICE_MGNT_GATE 0x01
  39. #define ST_NCI_APDU_READER_GATE 0xf0
  40. #define ST_NCI_CONNECTIVITY_GATE 0x41
  41. /* Pipes */
  42. #define ST_NCI_DEVICE_MGNT_PIPE 0x02
  43. /* Connectivity pipe only */
  44. #define ST_NCI_SE_COUNT_PIPE_UICC 0x01
  45. /* Connectivity + APDU Reader pipe */
  46. #define ST_NCI_SE_COUNT_PIPE_EMBEDDED 0x02
  47. #define ST_NCI_SE_TO_HOT_PLUG 1000 /* msecs */
  48. #define ST_NCI_SE_TO_PIPES 2000
  49. #define ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
  50. #define NCI_HCI_APDU_PARAM_ATR 0x01
  51. #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
  52. #define NCI_HCI_ADMIN_PARAM_WHITELIST 0x03
  53. #define NCI_HCI_ADMIN_PARAM_HOST_LIST 0x04
  54. #define ST_NCI_EVT_SE_HARD_RESET 0x20
  55. #define ST_NCI_EVT_TRANSMIT_DATA 0x10
  56. #define ST_NCI_EVT_WTX_REQUEST 0x11
  57. #define ST_NCI_EVT_SE_SOFT_RESET 0x11
  58. #define ST_NCI_EVT_SE_END_OF_APDU_TRANSFER 0x21
  59. #define ST_NCI_EVT_HOT_PLUG 0x03
  60. #define ST_NCI_SE_MODE_OFF 0x00
  61. #define ST_NCI_SE_MODE_ON 0x01
  62. #define ST_NCI_EVT_CONNECTIVITY 0x10
  63. #define ST_NCI_EVT_TRANSACTION 0x12
  64. #define ST_NCI_DM_GETINFO 0x13
  65. #define ST_NCI_DM_GETINFO_PIPE_LIST 0x02
  66. #define ST_NCI_DM_GETINFO_PIPE_INFO 0x01
  67. #define ST_NCI_DM_PIPE_CREATED 0x02
  68. #define ST_NCI_DM_PIPE_OPEN 0x04
  69. #define ST_NCI_DM_RF_ACTIVE 0x80
  70. #define ST_NCI_DM_DISCONNECT 0x30
  71. #define ST_NCI_DM_IS_PIPE_OPEN(p) \
  72. ((p & 0x0f) == (ST_NCI_DM_PIPE_CREATED | ST_NCI_DM_PIPE_OPEN))
  73. #define ST_NCI_ATR_DEFAULT_BWI 0x04
  74. /*
  75. * WT = 2^BWI/10[s], convert into msecs and add a secure
  76. * room by increasing by 2 this timeout
  77. */
  78. #define ST_NCI_BWI_TO_TIMEOUT(x) ((1 << x) * 200)
  79. #define ST_NCI_ATR_GET_Y_FROM_TD(x) (x >> 4)
  80. /* If TA is present bit 0 is set */
  81. #define ST_NCI_ATR_TA_PRESENT(x) (x & 0x01)
  82. /* If TB is present bit 1 is set */
  83. #define ST_NCI_ATR_TB_PRESENT(x) (x & 0x02)
  84. #define ST_NCI_NUM_DEVICES 256
  85. static DECLARE_BITMAP(dev_mask, ST_NCI_NUM_DEVICES);
  86. /* Here are the mandatory pipe for st_nci */
  87. static struct nci_hci_gate st_nci_gates[] = {
  88. {NCI_HCI_ADMIN_GATE, NCI_HCI_ADMIN_PIPE,
  89. ST_NCI_HOST_CONTROLLER_ID},
  90. {NCI_HCI_LINK_MGMT_GATE, NCI_HCI_LINK_MGMT_PIPE,
  91. ST_NCI_HOST_CONTROLLER_ID},
  92. {ST_NCI_DEVICE_MGNT_GATE, ST_NCI_DEVICE_MGNT_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_ESE_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 = 0; (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. st_nci_gates[j].pipe = pipe_info[2];
  201. ndev->hci_dev->gate2pipe[st_nci_gates[j].gate] =
  202. st_nci_gates[j].pipe;
  203. ndev->hci_dev->pipes[st_nci_gates[j].pipe].gate =
  204. st_nci_gates[j].gate;
  205. ndev->hci_dev->pipes[st_nci_gates[j].pipe].host =
  206. dm_pipe_info->src_host_id;
  207. }
  208. kfree_skb(skb_pipe_info);
  209. }
  210. memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
  211. sizeof(st_nci_gates));
  212. kfree_skb(skb_pipe_list);
  213. return r;
  214. }
  215. EXPORT_SYMBOL_GPL(st_nci_hci_load_session);
  216. static void st_nci_hci_admin_event_received(struct nci_dev *ndev,
  217. u8 event, struct sk_buff *skb)
  218. {
  219. struct st_nci_info *info = nci_get_drvdata(ndev);
  220. switch (event) {
  221. case ST_NCI_EVT_HOT_PLUG:
  222. if (info->se_info.se_active) {
  223. if (!ST_NCI_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
  224. del_timer_sync(&info->se_info.se_active_timer);
  225. info->se_info.se_active = false;
  226. complete(&info->se_info.req_completion);
  227. } else {
  228. mod_timer(&info->se_info.se_active_timer,
  229. jiffies +
  230. msecs_to_jiffies(ST_NCI_SE_TO_PIPES));
  231. }
  232. }
  233. break;
  234. }
  235. }
  236. static int st_nci_hci_apdu_reader_event_received(struct nci_dev *ndev,
  237. u8 event,
  238. struct sk_buff *skb)
  239. {
  240. int r = 0;
  241. struct st_nci_info *info = nci_get_drvdata(ndev);
  242. pr_debug("apdu reader gate event: %x\n", event);
  243. switch (event) {
  244. case ST_NCI_EVT_TRANSMIT_DATA:
  245. del_timer_sync(&info->se_info.bwi_timer);
  246. info->se_info.bwi_active = false;
  247. info->se_info.cb(info->se_info.cb_context,
  248. skb->data, skb->len, 0);
  249. break;
  250. case ST_NCI_EVT_WTX_REQUEST:
  251. mod_timer(&info->se_info.bwi_timer, jiffies +
  252. msecs_to_jiffies(info->se_info.wt_timeout));
  253. break;
  254. }
  255. kfree_skb(skb);
  256. return r;
  257. }
  258. /*
  259. * Returns:
  260. * <= 0: driver handled the event, skb consumed
  261. * 1: driver does not handle the event, please do standard processing
  262. */
  263. static int st_nci_hci_connectivity_event_received(struct nci_dev *ndev,
  264. u8 host, u8 event,
  265. struct sk_buff *skb)
  266. {
  267. int r = 0;
  268. struct device *dev = &ndev->nfc_dev->dev;
  269. struct nfc_evt_transaction *transaction;
  270. pr_debug("connectivity gate event: %x\n", event);
  271. switch (event) {
  272. case ST_NCI_EVT_CONNECTIVITY:
  273. break;
  274. case ST_NCI_EVT_TRANSACTION:
  275. /* According to specification etsi 102 622
  276. * 11.2.2.4 EVT_TRANSACTION Table 52
  277. * Description Tag Length
  278. * AID 81 5 to 16
  279. * PARAMETERS 82 0 to 255
  280. */
  281. if (skb->len < NFC_MIN_AID_LENGTH + 2 &&
  282. skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
  283. return -EPROTO;
  284. transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
  285. skb->len - 2, GFP_KERNEL);
  286. transaction->aid_len = skb->data[1];
  287. memcpy(transaction->aid, &skb->data[2], transaction->aid_len);
  288. /* Check next byte is PARAMETERS tag (82) */
  289. if (skb->data[transaction->aid_len + 2] !=
  290. NFC_EVT_TRANSACTION_PARAMS_TAG)
  291. return -EPROTO;
  292. transaction->params_len = skb->data[transaction->aid_len + 3];
  293. memcpy(transaction->params, skb->data +
  294. transaction->aid_len + 4, transaction->params_len);
  295. r = nfc_se_transaction(ndev->nfc_dev, host, transaction);
  296. break;
  297. default:
  298. return 1;
  299. }
  300. kfree_skb(skb);
  301. return r;
  302. }
  303. void st_nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
  304. u8 event, struct sk_buff *skb)
  305. {
  306. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  307. u8 host = ndev->hci_dev->pipes[pipe].host;
  308. switch (gate) {
  309. case NCI_HCI_ADMIN_GATE:
  310. st_nci_hci_admin_event_received(ndev, event, skb);
  311. break;
  312. case ST_NCI_APDU_READER_GATE:
  313. st_nci_hci_apdu_reader_event_received(ndev, event, skb);
  314. break;
  315. case ST_NCI_CONNECTIVITY_GATE:
  316. st_nci_hci_connectivity_event_received(ndev, host, event,
  317. skb);
  318. break;
  319. }
  320. }
  321. EXPORT_SYMBOL_GPL(st_nci_hci_event_received);
  322. void st_nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, u8 cmd,
  323. struct sk_buff *skb)
  324. {
  325. struct st_nci_info *info = nci_get_drvdata(ndev);
  326. u8 gate = ndev->hci_dev->pipes[pipe].gate;
  327. pr_debug("cmd: %x\n", cmd);
  328. switch (cmd) {
  329. case NCI_HCI_ANY_OPEN_PIPE:
  330. if (gate != ST_NCI_APDU_READER_GATE &&
  331. ndev->hci_dev->pipes[pipe].host != ST_NCI_UICC_HOST_ID)
  332. ndev->hci_dev->count_pipes++;
  333. if (ndev->hci_dev->count_pipes ==
  334. ndev->hci_dev->expected_pipes) {
  335. del_timer_sync(&info->se_info.se_active_timer);
  336. info->se_info.se_active = false;
  337. ndev->hci_dev->count_pipes = 0;
  338. complete(&info->se_info.req_completion);
  339. }
  340. break;
  341. }
  342. }
  343. EXPORT_SYMBOL_GPL(st_nci_hci_cmd_received);
  344. /*
  345. * Remarks: On some early st_nci firmware, nci_nfcee_mode_set(0)
  346. * is rejected
  347. */
  348. static int st_nci_control_se(struct nci_dev *ndev, u8 se_idx,
  349. u8 state)
  350. {
  351. struct st_nci_info *info = nci_get_drvdata(ndev);
  352. int r;
  353. struct sk_buff *sk_host_list;
  354. u8 host_id;
  355. switch (se_idx) {
  356. case ST_NCI_UICC_HOST_ID:
  357. ndev->hci_dev->count_pipes = 0;
  358. ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_UICC;
  359. break;
  360. case ST_NCI_ESE_HOST_ID:
  361. ndev->hci_dev->count_pipes = 0;
  362. ndev->hci_dev->expected_pipes = ST_NCI_SE_COUNT_PIPE_EMBEDDED;
  363. break;
  364. default:
  365. return -EINVAL;
  366. }
  367. /*
  368. * Wait for an EVT_HOT_PLUG in order to
  369. * retrieve a relevant host list.
  370. */
  371. reinit_completion(&info->se_info.req_completion);
  372. r = nci_nfcee_mode_set(ndev, se_idx, NCI_NFCEE_ENABLE);
  373. if (r != NCI_STATUS_OK)
  374. return r;
  375. mod_timer(&info->se_info.se_active_timer, jiffies +
  376. msecs_to_jiffies(ST_NCI_SE_TO_HOT_PLUG));
  377. info->se_info.se_active = true;
  378. /* Ignore return value and check in any case the host_list */
  379. wait_for_completion_interruptible(&info->se_info.req_completion);
  380. /* There might be some "collision" after receiving a HOT_PLUG event
  381. * This may cause the CLF to not answer to the next hci command.
  382. * There is no possible synchronization to prevent this.
  383. * Adding a small delay is the only way to solve the issue.
  384. */
  385. usleep_range(3000, 5000);
  386. r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
  387. NCI_HCI_ADMIN_PARAM_HOST_LIST, &sk_host_list);
  388. if (r != NCI_HCI_ANY_OK)
  389. return r;
  390. host_id = sk_host_list->data[sk_host_list->len - 1];
  391. kfree_skb(sk_host_list);
  392. if (state == ST_NCI_SE_MODE_ON && host_id == se_idx)
  393. return se_idx;
  394. else if (state == ST_NCI_SE_MODE_OFF && host_id != se_idx)
  395. return se_idx;
  396. return -1;
  397. }
  398. int st_nci_disable_se(struct nci_dev *ndev, u32 se_idx)
  399. {
  400. int r;
  401. pr_debug("st_nci_disable_se\n");
  402. if (se_idx == NFC_SE_EMBEDDED) {
  403. r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  404. ST_NCI_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
  405. if (r < 0)
  406. return r;
  407. }
  408. return 0;
  409. }
  410. EXPORT_SYMBOL_GPL(st_nci_disable_se);
  411. int st_nci_enable_se(struct nci_dev *ndev, u32 se_idx)
  412. {
  413. int r;
  414. pr_debug("st_nci_enable_se\n");
  415. if (se_idx == ST_NCI_HCI_HOST_ID_ESE) {
  416. r = nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  417. ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
  418. if (r < 0)
  419. return r;
  420. }
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(st_nci_enable_se);
  424. static int st_nci_hci_network_init(struct nci_dev *ndev)
  425. {
  426. struct core_conn_create_dest_spec_params *dest_params;
  427. struct dest_spec_params spec_params;
  428. struct nci_conn_info *conn_info;
  429. int r, dev_num;
  430. dest_params =
  431. kzalloc(sizeof(struct core_conn_create_dest_spec_params) +
  432. sizeof(struct dest_spec_params), GFP_KERNEL);
  433. if (dest_params == NULL) {
  434. r = -ENOMEM;
  435. goto exit;
  436. }
  437. dest_params->type = NCI_DESTINATION_SPECIFIC_PARAM_NFCEE_TYPE;
  438. dest_params->length = sizeof(struct dest_spec_params);
  439. spec_params.id = ndev->hci_dev->nfcee_id;
  440. spec_params.protocol = NCI_NFCEE_INTERFACE_HCI_ACCESS;
  441. memcpy(dest_params->value, &spec_params,
  442. sizeof(struct dest_spec_params));
  443. r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCEE, 1,
  444. sizeof(struct core_conn_create_dest_spec_params) +
  445. sizeof(struct dest_spec_params),
  446. dest_params);
  447. if (r != NCI_STATUS_OK)
  448. goto free_dest_params;
  449. conn_info = ndev->hci_dev->conn_info;
  450. if (!conn_info)
  451. goto free_dest_params;
  452. memcpy(ndev->hci_dev->init_data.gates, st_nci_gates,
  453. sizeof(st_nci_gates));
  454. /*
  455. * Session id must include the driver name + i2c bus addr
  456. * persistent info to discriminate 2 identical chips
  457. */
  458. dev_num = find_first_zero_bit(dev_mask, ST_NCI_NUM_DEVICES);
  459. if (dev_num >= ST_NCI_NUM_DEVICES) {
  460. r = -ENODEV;
  461. goto free_dest_params;
  462. }
  463. scnprintf(ndev->hci_dev->init_data.session_id,
  464. sizeof(ndev->hci_dev->init_data.session_id),
  465. "%s%2x", "ST21BH", dev_num);
  466. r = nci_hci_dev_session_init(ndev);
  467. if (r != NCI_HCI_ANY_OK)
  468. goto free_dest_params;
  469. r = nci_nfcee_mode_set(ndev, ndev->hci_dev->conn_info->id,
  470. NCI_NFCEE_ENABLE);
  471. if (r != NCI_STATUS_OK)
  472. goto free_dest_params;
  473. free_dest_params:
  474. kfree(dest_params);
  475. exit:
  476. return r;
  477. }
  478. int st_nci_discover_se(struct nci_dev *ndev)
  479. {
  480. u8 param[2];
  481. int r;
  482. int se_count = 0;
  483. pr_debug("st_nci_discover_se\n");
  484. r = st_nci_hci_network_init(ndev);
  485. if (r != 0)
  486. return r;
  487. param[0] = ST_NCI_UICC_HOST_ID;
  488. param[1] = ST_NCI_HCI_HOST_ID_ESE;
  489. r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
  490. NCI_HCI_ADMIN_PARAM_WHITELIST,
  491. param, sizeof(param));
  492. if (r != NCI_HCI_ANY_OK)
  493. return r;
  494. r = st_nci_control_se(ndev, ST_NCI_UICC_HOST_ID,
  495. ST_NCI_SE_MODE_ON);
  496. if (r == ST_NCI_UICC_HOST_ID) {
  497. nfc_add_se(ndev->nfc_dev, ST_NCI_UICC_HOST_ID, NFC_SE_UICC);
  498. se_count++;
  499. }
  500. /* Try to enable eSE in order to check availability */
  501. r = st_nci_control_se(ndev, ST_NCI_HCI_HOST_ID_ESE,
  502. ST_NCI_SE_MODE_ON);
  503. if (r == ST_NCI_HCI_HOST_ID_ESE) {
  504. nfc_add_se(ndev->nfc_dev, ST_NCI_HCI_HOST_ID_ESE,
  505. NFC_SE_EMBEDDED);
  506. se_count++;
  507. st_nci_se_get_atr(ndev);
  508. }
  509. return !se_count;
  510. }
  511. EXPORT_SYMBOL_GPL(st_nci_discover_se);
  512. int st_nci_se_io(struct nci_dev *ndev, u32 se_idx,
  513. u8 *apdu, size_t apdu_length,
  514. se_io_cb_t cb, void *cb_context)
  515. {
  516. struct st_nci_info *info = nci_get_drvdata(ndev);
  517. pr_debug("\n");
  518. switch (se_idx) {
  519. case ST_NCI_HCI_HOST_ID_ESE:
  520. info->se_info.cb = cb;
  521. info->se_info.cb_context = cb_context;
  522. mod_timer(&info->se_info.bwi_timer, jiffies +
  523. msecs_to_jiffies(info->se_info.wt_timeout));
  524. info->se_info.bwi_active = true;
  525. return nci_hci_send_event(ndev, ST_NCI_APDU_READER_GATE,
  526. ST_NCI_EVT_TRANSMIT_DATA, apdu,
  527. apdu_length);
  528. default:
  529. return -ENODEV;
  530. }
  531. }
  532. EXPORT_SYMBOL(st_nci_se_io);
  533. static void st_nci_se_wt_timeout(unsigned long data)
  534. {
  535. /*
  536. * No answer from the secure element
  537. * within the defined timeout.
  538. * Let's send a reset request as recovery procedure.
  539. * According to the situation, we first try to send a software reset
  540. * to the secure element. If the next command is still not
  541. * answering in time, we send to the CLF a secure element hardware
  542. * reset request.
  543. */
  544. /* hardware reset managed through VCC_UICC_OUT power supply */
  545. u8 param = 0x01;
  546. struct st_nci_info *info = (struct st_nci_info *) data;
  547. pr_debug("\n");
  548. info->se_info.bwi_active = false;
  549. if (!info->se_info.xch_error) {
  550. info->se_info.xch_error = true;
  551. nci_hci_send_event(info->ndlc->ndev, ST_NCI_APDU_READER_GATE,
  552. ST_NCI_EVT_SE_SOFT_RESET, NULL, 0);
  553. } else {
  554. info->se_info.xch_error = false;
  555. nci_hci_send_event(info->ndlc->ndev, ST_NCI_DEVICE_MGNT_GATE,
  556. ST_NCI_EVT_SE_HARD_RESET, &param, 1);
  557. }
  558. info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
  559. }
  560. static void st_nci_se_activation_timeout(unsigned long data)
  561. {
  562. struct st_nci_info *info = (struct st_nci_info *) data;
  563. pr_debug("\n");
  564. info->se_info.se_active = false;
  565. complete(&info->se_info.req_completion);
  566. }
  567. int st_nci_se_init(struct nci_dev *ndev)
  568. {
  569. struct st_nci_info *info = nci_get_drvdata(ndev);
  570. init_completion(&info->se_info.req_completion);
  571. /* initialize timers */
  572. init_timer(&info->se_info.bwi_timer);
  573. info->se_info.bwi_timer.data = (unsigned long)info;
  574. info->se_info.bwi_timer.function = st_nci_se_wt_timeout;
  575. info->se_info.bwi_active = false;
  576. init_timer(&info->se_info.se_active_timer);
  577. info->se_info.se_active_timer.data = (unsigned long)info;
  578. info->se_info.se_active_timer.function =
  579. st_nci_se_activation_timeout;
  580. info->se_info.se_active = false;
  581. info->se_info.xch_error = false;
  582. info->se_info.wt_timeout =
  583. ST_NCI_BWI_TO_TIMEOUT(ST_NCI_ATR_DEFAULT_BWI);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(st_nci_se_init);
  587. void st_nci_se_deinit(struct nci_dev *ndev)
  588. {
  589. struct st_nci_info *info = nci_get_drvdata(ndev);
  590. if (info->se_info.bwi_active)
  591. del_timer_sync(&info->se_info.bwi_timer);
  592. if (info->se_info.se_active)
  593. del_timer_sync(&info->se_info.se_active_timer);
  594. info->se_info.se_active = false;
  595. info->se_info.bwi_active = false;
  596. }
  597. EXPORT_SYMBOL(st_nci_se_deinit);