core.c 27 KB

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