core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HCI based Driver for STMicroelectronics NFC Chip
  4. *
  5. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/nfc.h>
  9. #include <net/nfc/hci.h>
  10. #include <net/nfc/llc.h>
  11. #include "st21nfca.h"
  12. #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
  13. #define FULL_VERSION_LEN 3
  14. /* Proprietary gates, events, commands and registers */
  15. /* Commands that apply to all RF readers */
  16. #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
  17. #define ST21NFCA_RF_READER_ISO15693_GATE 0x12
  18. #define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
  19. /*
  20. * Reader gate for communication with contact-less cards using Type A
  21. * protocol ISO14443-3 but not compliant with ISO14443-4
  22. */
  23. #define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
  24. #define ST21NFCA_RF_READER_14443_3_A_UID 0x02
  25. #define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
  26. #define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
  27. #define ST21NFCA_RF_READER_F_DATARATE 0x01
  28. #define ST21NFCA_RF_READER_F_DATARATE_106 0x01
  29. #define ST21NFCA_RF_READER_F_DATARATE_212 0x02
  30. #define ST21NFCA_RF_READER_F_DATARATE_424 0x04
  31. #define ST21NFCA_RF_READER_F_POL_REQ 0x02
  32. #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
  33. #define ST21NFCA_RF_READER_F_NFCID2 0x03
  34. #define ST21NFCA_RF_READER_F_NFCID1 0x04
  35. #define ST21NFCA_RF_CARD_F_MODE 0x01
  36. #define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
  37. #define ST21NFCA_RF_CARD_F_NFCID1 0x05
  38. #define ST21NFCA_RF_CARD_F_SENS_RES 0x06
  39. #define ST21NFCA_RF_CARD_F_SEL_RES 0x07
  40. #define ST21NFCA_RF_CARD_F_DATARATE 0x08
  41. #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
  42. #define ST21NFCA_DEVICE_MGNT_PIPE 0x02
  43. #define ST21NFCA_DM_GETINFO 0x13
  44. #define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
  45. #define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
  46. #define ST21NFCA_DM_PIPE_CREATED 0x02
  47. #define ST21NFCA_DM_PIPE_OPEN 0x04
  48. #define ST21NFCA_DM_RF_ACTIVE 0x80
  49. #define ST21NFCA_DM_DISCONNECT 0x30
  50. #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
  51. ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
  52. #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
  53. #define ST21NFCA_EVT_HOT_PLUG 0x03
  54. #define ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
  55. #define ST21NFCA_SE_TO_PIPES 2000
  56. static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
  57. static struct nfc_hci_gate st21nfca_gates[] = {
  58. {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
  59. {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
  60. {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
  61. {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
  62. {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  63. {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
  64. {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
  65. {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
  66. {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
  67. {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
  68. {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
  69. /* Secure element pipes are created by secure element host */
  70. {ST21NFCA_CONNECTIVITY_GATE, NFC_HCI_DO_NOT_CREATE_PIPE},
  71. {ST21NFCA_APDU_READER_GATE, NFC_HCI_DO_NOT_CREATE_PIPE},
  72. };
  73. struct st21nfca_pipe_info {
  74. u8 pipe_state;
  75. u8 src_host_id;
  76. u8 src_gate_id;
  77. u8 dst_host_id;
  78. u8 dst_gate_id;
  79. } __packed;
  80. /* Largest headroom needed for outgoing custom commands */
  81. #define ST21NFCA_CMDS_HEADROOM 7
  82. static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
  83. {
  84. int i, j, r;
  85. struct sk_buff *skb_pipe_list, *skb_pipe_info;
  86. struct st21nfca_pipe_info *info;
  87. u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
  88. NFC_HCI_TERMINAL_HOST_ID
  89. };
  90. u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
  91. NFC_HCI_TERMINAL_HOST_ID, 0
  92. };
  93. /* On ST21NFCA device pipes number are dynamics
  94. * A maximum of 16 pipes can be created at the same time
  95. * If pipes are already created, hci_dev_up will fail.
  96. * Doing a clear all pipe is a bad idea because:
  97. * - It does useless EEPROM cycling
  98. * - It might cause issue for secure elements support
  99. * (such as removing connectivity or APDU reader pipe)
  100. * A better approach on ST21NFCA is to:
  101. * - get a pipe list for each host.
  102. * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
  103. * (TODO Later on UICC HOST and eSE HOST)
  104. * - get pipe information
  105. * - match retrieved pipe list in st21nfca_gates
  106. * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
  107. * with ST21NFCA_DEVICE_MGNT_PIPE.
  108. * Pipe can be closed and need to be open.
  109. */
  110. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  111. ST21NFCA_DEVICE_MGNT_GATE,
  112. ST21NFCA_DEVICE_MGNT_PIPE);
  113. if (r < 0)
  114. return r;
  115. /* Get pipe list */
  116. r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  117. ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
  118. &skb_pipe_list);
  119. if (r < 0)
  120. return r;
  121. /* Complete the existing gate_pipe table */
  122. for (i = 0; i < skb_pipe_list->len; i++) {
  123. pipe_info[2] = skb_pipe_list->data[i];
  124. r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  125. ST21NFCA_DM_GETINFO, pipe_info,
  126. sizeof(pipe_info), &skb_pipe_info);
  127. if (r)
  128. continue;
  129. /*
  130. * Match pipe ID and gate ID
  131. * Output format from ST21NFC_DM_GETINFO is:
  132. * - pipe state (1byte)
  133. * - source hid (1byte)
  134. * - source gid (1byte)
  135. * - destination hid (1byte)
  136. * - destination gid (1byte)
  137. */
  138. info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
  139. if (info->dst_gate_id == ST21NFCA_APDU_READER_GATE &&
  140. info->src_host_id == NFC_HCI_UICC_HOST_ID) {
  141. pr_err("Unexpected apdu_reader pipe on host %x\n",
  142. info->src_host_id);
  143. kfree_skb(skb_pipe_info);
  144. continue;
  145. }
  146. for (j = 3; (j < ARRAY_SIZE(st21nfca_gates)) &&
  147. (st21nfca_gates[j].gate != info->dst_gate_id) ; j++)
  148. ;
  149. if (j < ARRAY_SIZE(st21nfca_gates) &&
  150. st21nfca_gates[j].gate == info->dst_gate_id &&
  151. ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
  152. hdev->init_data.gates[j].pipe = pipe_info[2];
  153. hdev->gate2pipe[st21nfca_gates[j].gate] =
  154. pipe_info[2];
  155. hdev->pipes[pipe_info[2]].gate =
  156. st21nfca_gates[j].gate;
  157. hdev->pipes[pipe_info[2]].dest_host =
  158. info->src_host_id;
  159. }
  160. kfree_skb(skb_pipe_info);
  161. }
  162. /*
  163. * 3 gates have a well known pipe ID. Only NFC_HCI_LINK_MGMT_GATE
  164. * is not yet open at this stage.
  165. */
  166. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  167. NFC_HCI_LINK_MGMT_GATE,
  168. NFC_HCI_LINK_MGMT_PIPE);
  169. kfree_skb(skb_pipe_list);
  170. return r;
  171. }
  172. static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
  173. {
  174. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  175. int r;
  176. mutex_lock(&info->info_lock);
  177. if (info->state != ST21NFCA_ST_COLD) {
  178. r = -EBUSY;
  179. goto out;
  180. }
  181. r = info->phy_ops->enable(info->phy_id);
  182. if (r == 0)
  183. info->state = ST21NFCA_ST_READY;
  184. out:
  185. mutex_unlock(&info->info_lock);
  186. return r;
  187. }
  188. static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
  189. {
  190. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  191. mutex_lock(&info->info_lock);
  192. if (info->state == ST21NFCA_ST_COLD)
  193. goto out;
  194. info->phy_ops->disable(info->phy_id);
  195. info->state = ST21NFCA_ST_COLD;
  196. out:
  197. mutex_unlock(&info->info_lock);
  198. }
  199. static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
  200. {
  201. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  202. struct sk_buff *skb;
  203. u8 param;
  204. u8 white_list[2];
  205. int wl_size = 0;
  206. int r;
  207. if (info->se_status->is_uicc_present)
  208. white_list[wl_size++] = NFC_HCI_UICC_HOST_ID;
  209. if (info->se_status->is_ese_present)
  210. white_list[wl_size++] = ST21NFCA_ESE_HOST_ID;
  211. if (wl_size) {
  212. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  213. NFC_HCI_ADMIN_WHITELIST,
  214. (u8 *) &white_list, wl_size);
  215. if (r < 0)
  216. return r;
  217. }
  218. /* Set NFC_MODE in device management gate to enable */
  219. r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  220. ST21NFCA_NFC_MODE, &skb);
  221. if (r < 0)
  222. return r;
  223. param = skb->data[0];
  224. kfree_skb(skb);
  225. if (param == 0) {
  226. param = 1;
  227. r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  228. ST21NFCA_NFC_MODE, &param, 1);
  229. if (r < 0)
  230. return r;
  231. }
  232. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  233. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  234. if (r < 0)
  235. return r;
  236. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  237. NFC_HCI_ID_MGMT_VERSION_SW, &skb);
  238. if (r < 0)
  239. return r;
  240. if (skb->len != FULL_VERSION_LEN) {
  241. kfree_skb(skb);
  242. return -EINVAL;
  243. }
  244. print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
  245. DUMP_PREFIX_NONE, 16, 1,
  246. skb->data, FULL_VERSION_LEN, false);
  247. kfree_skb(skb);
  248. return 0;
  249. }
  250. static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  251. {
  252. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  253. return info->phy_ops->write(info->phy_id, skb);
  254. }
  255. static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
  256. u32 im_protocols, u32 tm_protocols)
  257. {
  258. int r;
  259. u32 pol_req;
  260. u8 param[19];
  261. struct sk_buff *datarate_skb;
  262. pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
  263. __func__, im_protocols, tm_protocols);
  264. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  265. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  266. if (r < 0)
  267. return r;
  268. if (im_protocols) {
  269. /*
  270. * enable polling according to im_protocols & tm_protocols
  271. * - CLOSE pipe according to im_protocols & tm_protocols
  272. */
  273. if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
  274. r = nfc_hci_disconnect_gate(hdev,
  275. NFC_HCI_RF_READER_B_GATE);
  276. if (r < 0)
  277. return r;
  278. }
  279. if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
  280. r = nfc_hci_disconnect_gate(hdev,
  281. NFC_HCI_RF_READER_A_GATE);
  282. if (r < 0)
  283. return r;
  284. }
  285. if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
  286. r = nfc_hci_disconnect_gate(hdev,
  287. ST21NFCA_RF_READER_F_GATE);
  288. if (r < 0)
  289. return r;
  290. } else {
  291. hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
  292. &hdev->gb_len);
  293. if (hdev->gb == NULL || hdev->gb_len == 0) {
  294. im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  295. tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  296. }
  297. param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
  298. ST21NFCA_RF_READER_F_DATARATE_212 |
  299. ST21NFCA_RF_READER_F_DATARATE_424;
  300. r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
  301. ST21NFCA_RF_READER_F_DATARATE,
  302. param, 1);
  303. if (r < 0)
  304. return r;
  305. pol_req = be32_to_cpu((__force __be32)
  306. ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
  307. r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
  308. ST21NFCA_RF_READER_F_POL_REQ,
  309. (u8 *) &pol_req, 4);
  310. if (r < 0)
  311. return r;
  312. }
  313. if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
  314. r = nfc_hci_disconnect_gate(hdev,
  315. ST21NFCA_RF_READER_14443_3_A_GATE);
  316. if (r < 0)
  317. return r;
  318. }
  319. if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
  320. r = nfc_hci_disconnect_gate(hdev,
  321. ST21NFCA_RF_READER_ISO15693_GATE);
  322. if (r < 0)
  323. return r;
  324. }
  325. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  326. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  327. if (r < 0)
  328. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  329. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  330. }
  331. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  332. r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  333. ST21NFCA_RF_CARD_F_DATARATE,
  334. &datarate_skb);
  335. if (r < 0)
  336. return r;
  337. /* Configure the maximum supported datarate to 424Kbps */
  338. if (datarate_skb->len > 0 &&
  339. datarate_skb->data[0] !=
  340. ST21NFCA_RF_CARD_F_DATARATE_212_424) {
  341. param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
  342. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  343. ST21NFCA_RF_CARD_F_DATARATE,
  344. param, 1);
  345. if (r < 0) {
  346. kfree_skb(datarate_skb);
  347. return r;
  348. }
  349. }
  350. kfree_skb(datarate_skb);
  351. /*
  352. * Configure sens_res
  353. *
  354. * NFC Forum Digital Spec Table 7:
  355. * NFCID1 size: triple (10 bytes)
  356. */
  357. param[0] = 0x00;
  358. param[1] = 0x08;
  359. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  360. ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
  361. if (r < 0)
  362. return r;
  363. /*
  364. * Configure sel_res
  365. *
  366. * NFC Forum Digistal Spec Table 17:
  367. * b3 set to 0b (value b7-b6):
  368. * - 10b: Configured for NFC-DEP Protocol
  369. */
  370. param[0] = 0x40;
  371. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  372. ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
  373. if (r < 0)
  374. return r;
  375. /* Configure NFCID1 Random uid */
  376. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  377. ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
  378. if (r < 0)
  379. return r;
  380. /* Configure NFCID2_LIST */
  381. /* System Code */
  382. param[0] = 0x00;
  383. param[1] = 0x00;
  384. /* NFCID2 */
  385. param[2] = 0x01;
  386. param[3] = 0xfe;
  387. param[4] = 'S';
  388. param[5] = 'T';
  389. param[6] = 'M';
  390. param[7] = 'i';
  391. param[8] = 'c';
  392. param[9] = 'r';
  393. /* 8 byte Pad bytes used for polling respone frame */
  394. /*
  395. * Configuration byte:
  396. * - bit 0: define the default NFCID2 entry used when the
  397. * system code is equal to 'FFFF'
  398. * - bit 1: use a random value for lowest 6 bytes of
  399. * NFCID2 value
  400. * - bit 2: ignore polling request frame if request code
  401. * is equal to '01'
  402. * - Other bits are RFU
  403. */
  404. param[18] = 0x01;
  405. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  406. ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
  407. 19);
  408. if (r < 0)
  409. return r;
  410. param[0] = 0x02;
  411. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  412. ST21NFCA_RF_CARD_F_MODE, param, 1);
  413. }
  414. return r;
  415. }
  416. static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
  417. {
  418. nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  419. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  420. }
  421. static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
  422. {
  423. int r;
  424. struct sk_buff *atqa_skb = NULL;
  425. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  426. ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
  427. if (r < 0)
  428. goto exit;
  429. if (atqa_skb->len != 2) {
  430. r = -EPROTO;
  431. goto exit;
  432. }
  433. *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
  434. exit:
  435. kfree_skb(atqa_skb);
  436. return r;
  437. }
  438. static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
  439. {
  440. int r;
  441. struct sk_buff *sak_skb = NULL;
  442. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  443. ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
  444. if (r < 0)
  445. goto exit;
  446. if (sak_skb->len != 1) {
  447. r = -EPROTO;
  448. goto exit;
  449. }
  450. *sak = sak_skb->data[0];
  451. exit:
  452. kfree_skb(sak_skb);
  453. return r;
  454. }
  455. static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *uid,
  456. int *len)
  457. {
  458. int r;
  459. struct sk_buff *uid_skb = NULL;
  460. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  461. ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
  462. if (r < 0)
  463. goto exit;
  464. if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
  465. r = -EPROTO;
  466. goto exit;
  467. }
  468. memcpy(uid, uid_skb->data, uid_skb->len);
  469. *len = uid_skb->len;
  470. exit:
  471. kfree_skb(uid_skb);
  472. return r;
  473. }
  474. static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
  475. struct nfc_target *target)
  476. {
  477. int r;
  478. struct sk_buff *inventory_skb = NULL;
  479. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
  480. ST21NFCA_RF_READER_ISO15693_INVENTORY,
  481. &inventory_skb);
  482. if (r < 0)
  483. goto exit;
  484. skb_pull(inventory_skb, 2);
  485. if (inventory_skb->len == 0 ||
  486. inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
  487. r = -EPROTO;
  488. goto exit;
  489. }
  490. memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
  491. target->iso15693_dsfid = inventory_skb->data[1];
  492. target->is_iso15693 = 1;
  493. exit:
  494. kfree_skb(inventory_skb);
  495. return r;
  496. }
  497. static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
  498. struct nfc_target *target, u8 comm_mode,
  499. u8 *gb, size_t gb_len)
  500. {
  501. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  502. info->dep_info.idx = target->idx;
  503. return st21nfca_im_send_atr_req(hdev, gb, gb_len);
  504. }
  505. static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
  506. {
  507. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  508. info->state = ST21NFCA_ST_READY;
  509. return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  510. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  511. }
  512. static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  513. struct nfc_target *target)
  514. {
  515. int r, len;
  516. u16 atqa;
  517. u8 sak;
  518. u8 uid[NFC_NFCID1_MAXSIZE];
  519. switch (gate) {
  520. case ST21NFCA_RF_READER_F_GATE:
  521. target->supported_protocols = NFC_PROTO_FELICA_MASK;
  522. break;
  523. case ST21NFCA_RF_READER_14443_3_A_GATE:
  524. /* ISO14443-3 type 1 or 2 tags */
  525. r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
  526. if (r < 0)
  527. return r;
  528. if (atqa == 0x000c) {
  529. target->supported_protocols = NFC_PROTO_JEWEL_MASK;
  530. target->sens_res = 0x0c00;
  531. } else {
  532. r = st21nfca_get_iso14443_3_sak(hdev, &sak);
  533. if (r < 0)
  534. return r;
  535. r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
  536. if (r < 0)
  537. return r;
  538. target->supported_protocols =
  539. nfc_hci_sak_to_protocol(sak);
  540. if (target->supported_protocols == 0xffffffff)
  541. return -EPROTO;
  542. target->sens_res = atqa;
  543. target->sel_res = sak;
  544. memcpy(target->nfcid1, uid, len);
  545. target->nfcid1_len = len;
  546. }
  547. break;
  548. case ST21NFCA_RF_READER_ISO15693_GATE:
  549. target->supported_protocols = NFC_PROTO_ISO15693_MASK;
  550. r = st21nfca_get_iso15693_inventory(hdev, target);
  551. if (r < 0)
  552. return r;
  553. break;
  554. default:
  555. return -EPROTO;
  556. }
  557. return 0;
  558. }
  559. static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
  560. u8 gate,
  561. struct nfc_target *target)
  562. {
  563. int r;
  564. struct sk_buff *nfcid_skb = NULL;
  565. if (gate == ST21NFCA_RF_READER_F_GATE) {
  566. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  567. ST21NFCA_RF_READER_F_NFCID2, &nfcid_skb);
  568. if (r < 0)
  569. goto exit;
  570. if (nfcid_skb->len > NFC_SENSF_RES_MAXSIZE) {
  571. r = -EPROTO;
  572. goto exit;
  573. }
  574. /*
  575. * - After the recepton of polling response for type F frame
  576. * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
  577. * updated.
  578. * - After the reception of SEL_RES with NFCIP-1 compliant bit
  579. * set for type A frame NFCID1 will be updated
  580. */
  581. if (nfcid_skb->len > 0) {
  582. /* P2P in type F */
  583. memcpy(target->sensf_res, nfcid_skb->data,
  584. nfcid_skb->len);
  585. target->sensf_res_len = nfcid_skb->len;
  586. /* NFC Forum Digital Protocol Table 44 */
  587. if (target->sensf_res[0] == 0x01 &&
  588. target->sensf_res[1] == 0xfe)
  589. target->supported_protocols =
  590. NFC_PROTO_NFC_DEP_MASK;
  591. else
  592. target->supported_protocols =
  593. NFC_PROTO_FELICA_MASK;
  594. } else {
  595. kfree_skb(nfcid_skb);
  596. nfcid_skb = NULL;
  597. /* P2P in type A */
  598. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  599. ST21NFCA_RF_READER_F_NFCID1,
  600. &nfcid_skb);
  601. if (r < 0)
  602. goto exit;
  603. if (nfcid_skb->len > NFC_NFCID1_MAXSIZE) {
  604. r = -EPROTO;
  605. goto exit;
  606. }
  607. memcpy(target->sensf_res, nfcid_skb->data,
  608. nfcid_skb->len);
  609. target->sensf_res_len = nfcid_skb->len;
  610. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  611. }
  612. target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
  613. }
  614. r = 1;
  615. exit:
  616. kfree_skb(nfcid_skb);
  617. return r;
  618. }
  619. #define ST21NFCA_CB_TYPE_READER_ISO15693 1
  620. static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  621. int err)
  622. {
  623. struct st21nfca_hci_info *info = context;
  624. switch (info->async_cb_type) {
  625. case ST21NFCA_CB_TYPE_READER_ISO15693:
  626. if (err == 0)
  627. skb_trim(skb, skb->len - 1);
  628. info->async_cb(info->async_cb_context, skb, err);
  629. break;
  630. default:
  631. if (err == 0)
  632. kfree_skb(skb);
  633. break;
  634. }
  635. }
  636. /*
  637. * Returns:
  638. * <= 0: driver handled the data exchange
  639. * 1: driver doesn't especially handle, please do standard processing
  640. */
  641. static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
  642. struct nfc_target *target,
  643. struct sk_buff *skb,
  644. data_exchange_cb_t cb, void *cb_context)
  645. {
  646. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  647. pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
  648. target->hci_reader_gate, skb->len);
  649. switch (target->hci_reader_gate) {
  650. case ST21NFCA_RF_READER_F_GATE:
  651. if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
  652. return st21nfca_im_send_dep_req(hdev, skb);
  653. *(u8 *)skb_push(skb, 1) = 0x1a;
  654. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  655. ST21NFCA_WR_XCHG_DATA, skb->data,
  656. skb->len, cb, cb_context);
  657. case ST21NFCA_RF_READER_14443_3_A_GATE:
  658. *(u8 *)skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
  659. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  660. ST21NFCA_WR_XCHG_DATA, skb->data,
  661. skb->len, cb, cb_context);
  662. case ST21NFCA_RF_READER_ISO15693_GATE:
  663. info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
  664. info->async_cb = cb;
  665. info->async_cb_context = cb_context;
  666. *(u8 *)skb_push(skb, 1) = 0x17;
  667. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  668. ST21NFCA_WR_XCHG_DATA, skb->data,
  669. skb->len,
  670. st21nfca_hci_data_exchange_cb,
  671. info);
  672. default:
  673. return 1;
  674. }
  675. }
  676. static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  677. {
  678. return st21nfca_tm_send_dep_res(hdev, skb);
  679. }
  680. static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
  681. struct nfc_target *target)
  682. {
  683. u8 fwi = 0x11;
  684. switch (target->hci_reader_gate) {
  685. case NFC_HCI_RF_READER_A_GATE:
  686. case NFC_HCI_RF_READER_B_GATE:
  687. /*
  688. * PRESENCE_CHECK on those gates is available
  689. * However, the answer to this command is taking 3 * fwi
  690. * if the card is no present.
  691. * Instead, we send an empty I-Frame with a very short
  692. * configurable fwi ~604µs.
  693. */
  694. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  695. ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
  696. case ST21NFCA_RF_READER_14443_3_A_GATE:
  697. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  698. ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
  699. NULL, 0, NULL);
  700. default:
  701. return -EOPNOTSUPP;
  702. }
  703. }
  704. static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  705. struct sk_buff *skb)
  706. {
  707. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  708. u8 gate = hdev->pipes[pipe].gate;
  709. pr_debug("cmd: %x\n", cmd);
  710. switch (cmd) {
  711. case NFC_HCI_ANY_OPEN_PIPE:
  712. if (gate != ST21NFCA_APDU_READER_GATE &&
  713. hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID)
  714. info->se_info.count_pipes++;
  715. if (info->se_info.count_pipes == info->se_info.expected_pipes) {
  716. del_timer_sync(&info->se_info.se_active_timer);
  717. info->se_info.se_active = false;
  718. info->se_info.count_pipes = 0;
  719. complete(&info->se_info.req_completion);
  720. }
  721. break;
  722. }
  723. }
  724. static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event,
  725. struct sk_buff *skb)
  726. {
  727. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  728. pr_debug("admin event: %x\n", event);
  729. switch (event) {
  730. case ST21NFCA_EVT_HOT_PLUG:
  731. if (info->se_info.se_active) {
  732. if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
  733. del_timer_sync(&info->se_info.se_active_timer);
  734. info->se_info.se_active = false;
  735. complete(&info->se_info.req_completion);
  736. } else {
  737. mod_timer(&info->se_info.se_active_timer,
  738. jiffies +
  739. msecs_to_jiffies(ST21NFCA_SE_TO_PIPES));
  740. }
  741. }
  742. break;
  743. default:
  744. nfc_err(&hdev->ndev->dev, "Unexpected event on admin gate\n");
  745. }
  746. kfree_skb(skb);
  747. return 0;
  748. }
  749. /*
  750. * Returns:
  751. * <= 0: driver handled the event, skb consumed
  752. * 1: driver does not handle the event, please do standard processing
  753. */
  754. static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe,
  755. u8 event, struct sk_buff *skb)
  756. {
  757. u8 gate = hdev->pipes[pipe].gate;
  758. u8 host = hdev->pipes[pipe].dest_host;
  759. pr_debug("hci event: %d gate: %x\n", event, gate);
  760. switch (gate) {
  761. case NFC_HCI_ADMIN_GATE:
  762. return st21nfca_admin_event_received(hdev, event, skb);
  763. case ST21NFCA_RF_CARD_F_GATE:
  764. return st21nfca_dep_event_received(hdev, event, skb);
  765. case ST21NFCA_CONNECTIVITY_GATE:
  766. return st21nfca_connectivity_event_received(hdev, host,
  767. event, skb);
  768. case ST21NFCA_APDU_READER_GATE:
  769. return st21nfca_apdu_reader_event_received(hdev, event, skb);
  770. case NFC_HCI_LOOPBACK_GATE:
  771. return st21nfca_hci_loopback_event_received(hdev, event, skb);
  772. default:
  773. return 1;
  774. }
  775. }
  776. static struct nfc_hci_ops st21nfca_hci_ops = {
  777. .open = st21nfca_hci_open,
  778. .close = st21nfca_hci_close,
  779. .load_session = st21nfca_hci_load_session,
  780. .hci_ready = st21nfca_hci_ready,
  781. .xmit = st21nfca_hci_xmit,
  782. .start_poll = st21nfca_hci_start_poll,
  783. .stop_poll = st21nfca_hci_stop_poll,
  784. .dep_link_up = st21nfca_hci_dep_link_up,
  785. .dep_link_down = st21nfca_hci_dep_link_down,
  786. .target_from_gate = st21nfca_hci_target_from_gate,
  787. .complete_target_discovered = st21nfca_hci_complete_target_discovered,
  788. .im_transceive = st21nfca_hci_im_transceive,
  789. .tm_send = st21nfca_hci_tm_send,
  790. .check_presence = st21nfca_hci_check_presence,
  791. .event_received = st21nfca_hci_event_received,
  792. .cmd_received = st21nfca_hci_cmd_received,
  793. .discover_se = st21nfca_hci_discover_se,
  794. .enable_se = st21nfca_hci_enable_se,
  795. .disable_se = st21nfca_hci_disable_se,
  796. .se_io = st21nfca_hci_se_io,
  797. };
  798. int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
  799. char *llc_name, int phy_headroom, int phy_tailroom,
  800. int phy_payload, struct nfc_hci_dev **hdev,
  801. struct st21nfca_se_status *se_status)
  802. {
  803. struct st21nfca_hci_info *info;
  804. int r = 0;
  805. int dev_num;
  806. u32 protocols;
  807. struct nfc_hci_init_data init_data;
  808. unsigned long quirks = 0;
  809. info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
  810. if (!info)
  811. return -ENOMEM;
  812. info->phy_ops = phy_ops;
  813. info->phy_id = phy_id;
  814. info->state = ST21NFCA_ST_COLD;
  815. mutex_init(&info->info_lock);
  816. init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
  817. memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
  818. /*
  819. * Session id must include the driver name + i2c bus addr
  820. * persistent info to discriminate 2 identical chips
  821. */
  822. dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
  823. if (dev_num >= ST21NFCA_NUM_DEVICES) {
  824. r = -ENODEV;
  825. goto err_alloc_hdev;
  826. }
  827. set_bit(dev_num, dev_mask);
  828. scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
  829. "ST21AH", dev_num);
  830. protocols = NFC_PROTO_JEWEL_MASK |
  831. NFC_PROTO_MIFARE_MASK |
  832. NFC_PROTO_FELICA_MASK |
  833. NFC_PROTO_ISO14443_MASK |
  834. NFC_PROTO_ISO14443_B_MASK |
  835. NFC_PROTO_ISO15693_MASK |
  836. NFC_PROTO_NFC_DEP_MASK;
  837. set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
  838. info->hdev =
  839. nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
  840. protocols, llc_name,
  841. phy_headroom + ST21NFCA_CMDS_HEADROOM,
  842. phy_tailroom, phy_payload);
  843. if (!info->hdev) {
  844. pr_err("Cannot allocate nfc hdev.\n");
  845. r = -ENOMEM;
  846. goto err_alloc_hdev;
  847. }
  848. info->se_status = se_status;
  849. nfc_hci_set_clientdata(info->hdev, info);
  850. r = nfc_hci_register_device(info->hdev);
  851. if (r)
  852. goto err_regdev;
  853. *hdev = info->hdev;
  854. st21nfca_dep_init(info->hdev);
  855. st21nfca_se_init(info->hdev);
  856. st21nfca_vendor_cmds_init(info->hdev);
  857. return 0;
  858. err_regdev:
  859. nfc_hci_free_device(info->hdev);
  860. err_alloc_hdev:
  861. kfree(info);
  862. return r;
  863. }
  864. EXPORT_SYMBOL(st21nfca_hci_probe);
  865. void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
  866. {
  867. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  868. st21nfca_dep_deinit(hdev);
  869. st21nfca_se_deinit(hdev);
  870. nfc_hci_unregister_device(hdev);
  871. nfc_hci_free_device(hdev);
  872. kfree(info);
  873. }
  874. EXPORT_SYMBOL(st21nfca_hci_remove);
  875. MODULE_LICENSE("GPL");
  876. MODULE_DESCRIPTION(DRIVER_DESC);