core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. /* P2P in type A */
  608. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  609. ST21NFCA_RF_READER_F_NFCID1,
  610. &nfcid_skb);
  611. if (r < 0)
  612. goto exit;
  613. if (nfcid_skb->len > NFC_NFCID1_MAXSIZE) {
  614. r = -EPROTO;
  615. goto exit;
  616. }
  617. memcpy(target->sensf_res, nfcid_skb->data,
  618. nfcid_skb->len);
  619. target->sensf_res_len = nfcid_skb->len;
  620. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  621. }
  622. target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
  623. }
  624. r = 1;
  625. exit:
  626. kfree_skb(nfcid_skb);
  627. return r;
  628. }
  629. #define ST21NFCA_CB_TYPE_READER_ISO15693 1
  630. static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  631. int err)
  632. {
  633. struct st21nfca_hci_info *info = context;
  634. switch (info->async_cb_type) {
  635. case ST21NFCA_CB_TYPE_READER_ISO15693:
  636. if (err == 0)
  637. skb_trim(skb, skb->len - 1);
  638. info->async_cb(info->async_cb_context, skb, err);
  639. break;
  640. default:
  641. if (err == 0)
  642. kfree_skb(skb);
  643. break;
  644. }
  645. }
  646. /*
  647. * Returns:
  648. * <= 0: driver handled the data exchange
  649. * 1: driver doesn't especially handle, please do standard processing
  650. */
  651. static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
  652. struct nfc_target *target,
  653. struct sk_buff *skb,
  654. data_exchange_cb_t cb, void *cb_context)
  655. {
  656. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  657. pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
  658. target->hci_reader_gate, skb->len);
  659. switch (target->hci_reader_gate) {
  660. case ST21NFCA_RF_READER_F_GATE:
  661. if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
  662. return st21nfca_im_send_dep_req(hdev, skb);
  663. *skb_push(skb, 1) = 0x1a;
  664. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  665. ST21NFCA_WR_XCHG_DATA, skb->data,
  666. skb->len, cb, cb_context);
  667. case ST21NFCA_RF_READER_14443_3_A_GATE:
  668. *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
  669. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  670. ST21NFCA_WR_XCHG_DATA, skb->data,
  671. skb->len, cb, cb_context);
  672. case ST21NFCA_RF_READER_ISO15693_GATE:
  673. info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
  674. info->async_cb = cb;
  675. info->async_cb_context = cb_context;
  676. *skb_push(skb, 1) = 0x17;
  677. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  678. ST21NFCA_WR_XCHG_DATA, skb->data,
  679. skb->len,
  680. st21nfca_hci_data_exchange_cb,
  681. info);
  682. break;
  683. default:
  684. return 1;
  685. }
  686. }
  687. static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  688. {
  689. return st21nfca_tm_send_dep_res(hdev, skb);
  690. }
  691. static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
  692. struct nfc_target *target)
  693. {
  694. u8 fwi = 0x11;
  695. switch (target->hci_reader_gate) {
  696. case NFC_HCI_RF_READER_A_GATE:
  697. case NFC_HCI_RF_READER_B_GATE:
  698. /*
  699. * PRESENCE_CHECK on those gates is available
  700. * However, the answer to this command is taking 3 * fwi
  701. * if the card is no present.
  702. * Instead, we send an empty I-Frame with a very short
  703. * configurable fwi ~604µs.
  704. */
  705. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  706. ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
  707. case ST21NFCA_RF_READER_14443_3_A_GATE:
  708. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  709. ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
  710. NULL, 0, NULL);
  711. default:
  712. return -EOPNOTSUPP;
  713. }
  714. }
  715. static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  716. struct sk_buff *skb)
  717. {
  718. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  719. u8 gate = hdev->pipes[pipe].gate;
  720. pr_debug("cmd: %x\n", cmd);
  721. switch (cmd) {
  722. case NFC_HCI_ANY_OPEN_PIPE:
  723. if (gate != ST21NFCA_APDU_READER_GATE &&
  724. hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID)
  725. info->se_info.count_pipes++;
  726. if (info->se_info.count_pipes == info->se_info.expected_pipes) {
  727. del_timer_sync(&info->se_info.se_active_timer);
  728. info->se_info.se_active = false;
  729. info->se_info.count_pipes = 0;
  730. complete(&info->se_info.req_completion);
  731. }
  732. break;
  733. }
  734. }
  735. static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event,
  736. struct sk_buff *skb)
  737. {
  738. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  739. pr_debug("admin event: %x\n", event);
  740. switch (event) {
  741. case ST21NFCA_EVT_HOT_PLUG:
  742. if (info->se_info.se_active) {
  743. if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
  744. del_timer_sync(&info->se_info.se_active_timer);
  745. info->se_info.se_active = false;
  746. complete(&info->se_info.req_completion);
  747. } else {
  748. mod_timer(&info->se_info.se_active_timer,
  749. jiffies +
  750. msecs_to_jiffies(ST21NFCA_SE_TO_PIPES));
  751. }
  752. }
  753. break;
  754. default:
  755. nfc_err(&hdev->ndev->dev, "Unexpected event on admin gate\n");
  756. }
  757. kfree_skb(skb);
  758. return 0;
  759. }
  760. /*
  761. * Returns:
  762. * <= 0: driver handled the event, skb consumed
  763. * 1: driver does not handle the event, please do standard processing
  764. */
  765. static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe,
  766. u8 event, struct sk_buff *skb)
  767. {
  768. u8 gate = hdev->pipes[pipe].gate;
  769. u8 host = hdev->pipes[pipe].dest_host;
  770. pr_debug("hci event: %d gate: %x\n", event, gate);
  771. switch (gate) {
  772. case NFC_HCI_ADMIN_GATE:
  773. return st21nfca_admin_event_received(hdev, event, skb);
  774. case ST21NFCA_RF_CARD_F_GATE:
  775. return st21nfca_dep_event_received(hdev, event, skb);
  776. case ST21NFCA_CONNECTIVITY_GATE:
  777. return st21nfca_connectivity_event_received(hdev, host,
  778. event, skb);
  779. case ST21NFCA_APDU_READER_GATE:
  780. return st21nfca_apdu_reader_event_received(hdev, event, skb);
  781. case NFC_HCI_LOOPBACK_GATE:
  782. return st21nfca_hci_loopback_event_received(hdev, event, skb);
  783. default:
  784. return 1;
  785. }
  786. }
  787. static struct nfc_hci_ops st21nfca_hci_ops = {
  788. .open = st21nfca_hci_open,
  789. .close = st21nfca_hci_close,
  790. .load_session = st21nfca_hci_load_session,
  791. .hci_ready = st21nfca_hci_ready,
  792. .xmit = st21nfca_hci_xmit,
  793. .start_poll = st21nfca_hci_start_poll,
  794. .stop_poll = st21nfca_hci_stop_poll,
  795. .dep_link_up = st21nfca_hci_dep_link_up,
  796. .dep_link_down = st21nfca_hci_dep_link_down,
  797. .target_from_gate = st21nfca_hci_target_from_gate,
  798. .complete_target_discovered = st21nfca_hci_complete_target_discovered,
  799. .im_transceive = st21nfca_hci_im_transceive,
  800. .tm_send = st21nfca_hci_tm_send,
  801. .check_presence = st21nfca_hci_check_presence,
  802. .event_received = st21nfca_hci_event_received,
  803. .cmd_received = st21nfca_hci_cmd_received,
  804. .discover_se = st21nfca_hci_discover_se,
  805. .enable_se = st21nfca_hci_enable_se,
  806. .disable_se = st21nfca_hci_disable_se,
  807. .se_io = st21nfca_hci_se_io,
  808. };
  809. int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
  810. char *llc_name, int phy_headroom, int phy_tailroom,
  811. int phy_payload, struct nfc_hci_dev **hdev,
  812. struct st21nfca_se_status *se_status)
  813. {
  814. struct st21nfca_hci_info *info;
  815. int r = 0;
  816. int dev_num;
  817. u32 protocols;
  818. struct nfc_hci_init_data init_data;
  819. unsigned long quirks = 0;
  820. info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
  821. if (!info) {
  822. r = -ENOMEM;
  823. goto err_alloc_hdev;
  824. }
  825. info->phy_ops = phy_ops;
  826. info->phy_id = phy_id;
  827. info->state = ST21NFCA_ST_COLD;
  828. mutex_init(&info->info_lock);
  829. init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
  830. memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
  831. /*
  832. * Session id must include the driver name + i2c bus addr
  833. * persistent info to discriminate 2 identical chips
  834. */
  835. dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
  836. if (dev_num >= ST21NFCA_NUM_DEVICES)
  837. return -ENODEV;
  838. set_bit(dev_num, dev_mask);
  839. scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
  840. "ST21AH", dev_num);
  841. protocols = NFC_PROTO_JEWEL_MASK |
  842. NFC_PROTO_MIFARE_MASK |
  843. NFC_PROTO_FELICA_MASK |
  844. NFC_PROTO_ISO14443_MASK |
  845. NFC_PROTO_ISO14443_B_MASK |
  846. NFC_PROTO_ISO15693_MASK |
  847. NFC_PROTO_NFC_DEP_MASK;
  848. set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
  849. info->hdev =
  850. nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
  851. protocols, llc_name,
  852. phy_headroom + ST21NFCA_CMDS_HEADROOM,
  853. phy_tailroom, phy_payload);
  854. if (!info->hdev) {
  855. pr_err("Cannot allocate nfc hdev.\n");
  856. r = -ENOMEM;
  857. goto err_alloc_hdev;
  858. }
  859. info->se_status = se_status;
  860. nfc_hci_set_clientdata(info->hdev, info);
  861. r = nfc_hci_register_device(info->hdev);
  862. if (r)
  863. goto err_regdev;
  864. *hdev = info->hdev;
  865. st21nfca_dep_init(info->hdev);
  866. st21nfca_se_init(info->hdev);
  867. st21nfca_vendor_cmds_init(info->hdev);
  868. return 0;
  869. err_regdev:
  870. nfc_hci_free_device(info->hdev);
  871. err_alloc_hdev:
  872. kfree(info);
  873. return r;
  874. }
  875. EXPORT_SYMBOL(st21nfca_hci_probe);
  876. void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
  877. {
  878. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  879. st21nfca_dep_deinit(hdev);
  880. st21nfca_se_deinit(hdev);
  881. nfc_hci_unregister_device(hdev);
  882. nfc_hci_free_device(hdev);
  883. kfree(info);
  884. }
  885. EXPORT_SYMBOL(st21nfca_hci_remove);
  886. MODULE_LICENSE("GPL");
  887. MODULE_DESCRIPTION(DRIVER_DESC);