core.c 27 KB

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