st21nfca.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. #include "st21nfca_dep.h"
  24. #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
  25. #define FULL_VERSION_LEN 3
  26. /* Proprietary gates, events, commands and registers */
  27. /* Commands that apply to all RF readers */
  28. #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
  29. #define ST21NFCA_RF_READER_ISO15693_GATE 0x12
  30. #define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
  31. /*
  32. * Reader gate for communication with contact-less cards using Type A
  33. * protocol ISO14443-3 but not compliant with ISO14443-4
  34. */
  35. #define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
  36. #define ST21NFCA_RF_READER_14443_3_A_UID 0x02
  37. #define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
  38. #define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
  39. #define ST21NFCA_RF_READER_F_DATARATE 0x01
  40. #define ST21NFCA_RF_READER_F_DATARATE_106 0x01
  41. #define ST21NFCA_RF_READER_F_DATARATE_212 0x02
  42. #define ST21NFCA_RF_READER_F_DATARATE_424 0x04
  43. #define ST21NFCA_RF_READER_F_POL_REQ 0x02
  44. #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
  45. #define ST21NFCA_RF_READER_F_NFCID2 0x03
  46. #define ST21NFCA_RF_READER_F_NFCID1 0x04
  47. #define ST21NFCA_RF_CARD_F_MODE 0x01
  48. #define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
  49. #define ST21NFCA_RF_CARD_F_NFCID1 0x05
  50. #define ST21NFCA_RF_CARD_F_SENS_RES 0x06
  51. #define ST21NFCA_RF_CARD_F_SEL_RES 0x07
  52. #define ST21NFCA_RF_CARD_F_DATARATE 0x08
  53. #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
  54. #define ST21NFCA_DEVICE_MGNT_GATE 0x01
  55. #define ST21NFCA_DEVICE_MGNT_PIPE 0x02
  56. #define ST21NFCA_DM_GETINFO 0x13
  57. #define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
  58. #define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
  59. #define ST21NFCA_DM_PIPE_CREATED 0x02
  60. #define ST21NFCA_DM_PIPE_OPEN 0x04
  61. #define ST21NFCA_DM_RF_ACTIVE 0x80
  62. #define ST21NFCA_DM_DISCONNECT 0x30
  63. #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
  64. ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
  65. #define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
  66. #define ST21NFCA_EVT_FIELD_ON 0x11
  67. #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
  68. #define ST21NFCA_EVT_CARD_ACTIVATED 0x13
  69. #define ST21NFCA_EVT_FIELD_OFF 0x14
  70. static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
  71. static struct nfc_hci_gate st21nfca_gates[] = {
  72. {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
  73. {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
  74. {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
  75. {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
  76. {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
  77. {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
  78. {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
  79. {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
  80. {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
  81. {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
  82. {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_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. skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
  105. if (!skb_pipe_list) {
  106. r = -ENOMEM;
  107. goto free_list;
  108. }
  109. skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
  110. if (!skb_pipe_info) {
  111. r = -ENOMEM;
  112. goto free_info;
  113. }
  114. /* On ST21NFCA device pipes number are dynamics
  115. * A maximum of 16 pipes can be created at the same time
  116. * If pipes are already created, hci_dev_up will fail.
  117. * Doing a clear all pipe is a bad idea because:
  118. * - It does useless EEPROM cycling
  119. * - It might cause issue for secure elements support
  120. * (such as removing connectivity or APDU reader pipe)
  121. * A better approach on ST21NFCA is to:
  122. * - get a pipe list for each host.
  123. * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
  124. * (TODO Later on UICC HOST and eSE HOST)
  125. * - get pipe information
  126. * - match retrieved pipe list in st21nfca_gates
  127. * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
  128. * with ST21NFCA_DEVICE_MGNT_PIPE.
  129. * Pipe can be closed and need to be open.
  130. */
  131. r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
  132. ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
  133. if (r < 0)
  134. goto free_info;
  135. /* Get pipe list */
  136. r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  137. ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
  138. &skb_pipe_list);
  139. if (r < 0)
  140. goto free_info;
  141. /* Complete the existing gate_pipe table */
  142. for (i = 0; i < skb_pipe_list->len; i++) {
  143. pipe_info[2] = skb_pipe_list->data[i];
  144. r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  145. ST21NFCA_DM_GETINFO, pipe_info,
  146. sizeof(pipe_info), &skb_pipe_info);
  147. if (r)
  148. continue;
  149. /*
  150. * Match pipe ID and gate ID
  151. * Output format from ST21NFC_DM_GETINFO is:
  152. * - pipe state (1byte)
  153. * - source hid (1byte)
  154. * - source gid (1byte)
  155. * - destination hid (1byte)
  156. * - destination gid (1byte)
  157. */
  158. info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
  159. for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
  160. (st21nfca_gates[j].gate != info->dst_gate_id);
  161. j++)
  162. ;
  163. if (j < ARRAY_SIZE(st21nfca_gates) &&
  164. st21nfca_gates[j].gate == info->dst_gate_id &&
  165. ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
  166. st21nfca_gates[j].pipe = pipe_info[2];
  167. hdev->gate2pipe[st21nfca_gates[j].gate] =
  168. st21nfca_gates[j].pipe;
  169. }
  170. }
  171. /*
  172. * 3 gates have a well known pipe ID.
  173. * They will never appear in the pipe list
  174. */
  175. if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
  176. for (i = skb_pipe_list->len + 3;
  177. i < ARRAY_SIZE(st21nfca_gates); i++) {
  178. r = nfc_hci_connect_gate(hdev,
  179. NFC_HCI_HOST_CONTROLLER_ID,
  180. st21nfca_gates[i].gate,
  181. st21nfca_gates[i].pipe);
  182. if (r < 0)
  183. goto free_info;
  184. }
  185. }
  186. memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
  187. free_info:
  188. kfree_skb(skb_pipe_info);
  189. free_list:
  190. kfree_skb(skb_pipe_list);
  191. return r;
  192. }
  193. static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
  194. {
  195. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  196. int r;
  197. mutex_lock(&info->info_lock);
  198. if (info->state != ST21NFCA_ST_COLD) {
  199. r = -EBUSY;
  200. goto out;
  201. }
  202. r = info->phy_ops->enable(info->phy_id);
  203. if (r == 0)
  204. info->state = ST21NFCA_ST_READY;
  205. out:
  206. mutex_unlock(&info->info_lock);
  207. return r;
  208. }
  209. static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
  210. {
  211. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  212. mutex_lock(&info->info_lock);
  213. if (info->state == ST21NFCA_ST_COLD)
  214. goto out;
  215. info->phy_ops->disable(info->phy_id);
  216. info->state = ST21NFCA_ST_COLD;
  217. out:
  218. mutex_unlock(&info->info_lock);
  219. }
  220. static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
  221. {
  222. struct sk_buff *skb;
  223. u8 param;
  224. int r;
  225. param = NFC_HCI_UICC_HOST_ID;
  226. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  227. NFC_HCI_ADMIN_WHITELIST, &param, 1);
  228. if (r < 0)
  229. return r;
  230. /* Set NFC_MODE in device management gate to enable */
  231. r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  232. ST21NFCA_NFC_MODE, &skb);
  233. if (r < 0)
  234. return r;
  235. if (skb->data[0] == 0) {
  236. kfree_skb(skb);
  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. return r;
  358. }
  359. /*
  360. * Configure sens_res
  361. *
  362. * NFC Forum Digital Spec Table 7:
  363. * NFCID1 size: triple (10 bytes)
  364. */
  365. param[0] = 0x00;
  366. param[1] = 0x08;
  367. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  368. ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
  369. if (r < 0)
  370. return r;
  371. /*
  372. * Configure sel_res
  373. *
  374. * NFC Forum Digistal Spec Table 17:
  375. * b3 set to 0b (value b7-b6):
  376. * - 10b: Configured for NFC-DEP Protocol
  377. */
  378. param[0] = 0x40;
  379. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  380. ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
  381. if (r < 0)
  382. return r;
  383. /* Configure NFCID1 Random uid */
  384. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  385. ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
  386. if (r < 0)
  387. return r;
  388. /* Configure NFCID2_LIST */
  389. /* System Code */
  390. param[0] = 0x00;
  391. param[1] = 0x00;
  392. /* NFCID2 */
  393. param[2] = 0x01;
  394. param[3] = 0xfe;
  395. param[4] = 'S';
  396. param[5] = 'T';
  397. param[6] = 'M';
  398. param[7] = 'i';
  399. param[8] = 'c';
  400. param[9] = 'r';
  401. /* 8 byte Pad bytes used for polling respone frame */
  402. /*
  403. * Configuration byte:
  404. * - bit 0: define the default NFCID2 entry used when the
  405. * system code is equal to 'FFFF'
  406. * - bit 1: use a random value for lowest 6 bytes of
  407. * NFCID2 value
  408. * - bit 2: ignore polling request frame if request code
  409. * is equal to '01'
  410. * - Other bits are RFU
  411. */
  412. param[18] = 0x01;
  413. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  414. ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
  415. 19);
  416. if (r < 0)
  417. return r;
  418. param[0] = 0x02;
  419. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  420. ST21NFCA_RF_CARD_F_MODE, param, 1);
  421. }
  422. return r;
  423. }
  424. static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
  425. {
  426. nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  427. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  428. }
  429. static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
  430. {
  431. int r;
  432. struct sk_buff *atqa_skb = NULL;
  433. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  434. ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
  435. if (r < 0)
  436. goto exit;
  437. if (atqa_skb->len != 2) {
  438. r = -EPROTO;
  439. goto exit;
  440. }
  441. *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
  442. exit:
  443. kfree_skb(atqa_skb);
  444. return r;
  445. }
  446. static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
  447. {
  448. int r;
  449. struct sk_buff *sak_skb = NULL;
  450. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  451. ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
  452. if (r < 0)
  453. goto exit;
  454. if (sak_skb->len != 1) {
  455. r = -EPROTO;
  456. goto exit;
  457. }
  458. *sak = sak_skb->data[0];
  459. exit:
  460. kfree_skb(sak_skb);
  461. return r;
  462. }
  463. static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
  464. int *len)
  465. {
  466. int r;
  467. struct sk_buff *uid_skb = NULL;
  468. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  469. ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
  470. if (r < 0)
  471. goto exit;
  472. if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
  473. r = -EPROTO;
  474. goto exit;
  475. }
  476. gate = uid_skb->data;
  477. *len = uid_skb->len;
  478. exit:
  479. kfree_skb(uid_skb);
  480. return r;
  481. }
  482. static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
  483. struct nfc_target *target)
  484. {
  485. int r;
  486. struct sk_buff *inventory_skb = NULL;
  487. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
  488. ST21NFCA_RF_READER_ISO15693_INVENTORY,
  489. &inventory_skb);
  490. if (r < 0)
  491. goto exit;
  492. skb_pull(inventory_skb, 2);
  493. if (inventory_skb->len == 0 ||
  494. inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
  495. r = -EPROTO;
  496. goto exit;
  497. }
  498. memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
  499. target->iso15693_dsfid = inventory_skb->data[1];
  500. target->is_iso15693 = 1;
  501. exit:
  502. kfree_skb(inventory_skb);
  503. return r;
  504. }
  505. static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
  506. struct nfc_target *target, u8 comm_mode,
  507. u8 *gb, size_t gb_len)
  508. {
  509. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  510. info->dep_info.idx = target->idx;
  511. return st21nfca_im_send_atr_req(hdev, gb, gb_len);
  512. }
  513. static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
  514. {
  515. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  516. info->state = ST21NFCA_ST_READY;
  517. return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  518. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  519. }
  520. static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  521. struct nfc_target *target)
  522. {
  523. int r, len;
  524. u16 atqa;
  525. u8 sak;
  526. u8 uid[NFC_NFCID1_MAXSIZE];
  527. switch (gate) {
  528. case ST21NFCA_RF_READER_F_GATE:
  529. target->supported_protocols = NFC_PROTO_FELICA_MASK;
  530. break;
  531. case ST21NFCA_RF_READER_14443_3_A_GATE:
  532. /* ISO14443-3 type 1 or 2 tags */
  533. r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
  534. if (r < 0)
  535. return r;
  536. if (atqa == 0x000c) {
  537. target->supported_protocols = NFC_PROTO_JEWEL_MASK;
  538. target->sens_res = 0x0c00;
  539. } else {
  540. r = st21nfca_get_iso14443_3_sak(hdev, &sak);
  541. if (r < 0)
  542. return r;
  543. r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
  544. if (r < 0)
  545. return r;
  546. target->supported_protocols =
  547. nfc_hci_sak_to_protocol(sak);
  548. if (target->supported_protocols == 0xffffffff)
  549. return -EPROTO;
  550. target->sens_res = atqa;
  551. target->sel_res = sak;
  552. memcpy(target->nfcid1, uid, len);
  553. target->nfcid1_len = len;
  554. }
  555. break;
  556. case ST21NFCA_RF_READER_ISO15693_GATE:
  557. target->supported_protocols = NFC_PROTO_ISO15693_MASK;
  558. r = st21nfca_get_iso15693_inventory(hdev, target);
  559. if (r < 0)
  560. return r;
  561. break;
  562. default:
  563. return -EPROTO;
  564. }
  565. return 0;
  566. }
  567. static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
  568. u8 gate,
  569. struct nfc_target *target)
  570. {
  571. int r;
  572. struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
  573. if (gate == ST21NFCA_RF_READER_F_GATE) {
  574. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  575. ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
  576. if (r < 0)
  577. goto exit;
  578. if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
  579. r = -EPROTO;
  580. goto exit;
  581. }
  582. /*
  583. * - After the recepton of polling response for type F frame
  584. * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
  585. * updated.
  586. * - After the reception of SEL_RES with NFCIP-1 compliant bit
  587. * set for type A frame NFCID1 will be updated
  588. */
  589. if (nfcid2_skb->len > 0) {
  590. /* P2P in type F */
  591. memcpy(target->sensf_res, nfcid2_skb->data,
  592. nfcid2_skb->len);
  593. target->sensf_res_len = nfcid2_skb->len;
  594. /* NFC Forum Digital Protocol Table 44 */
  595. if (target->sensf_res[0] == 0x01 &&
  596. target->sensf_res[1] == 0xfe)
  597. target->supported_protocols =
  598. NFC_PROTO_NFC_DEP_MASK;
  599. else
  600. target->supported_protocols =
  601. NFC_PROTO_FELICA_MASK;
  602. } else {
  603. /* P2P in type A */
  604. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  605. ST21NFCA_RF_READER_F_NFCID1,
  606. &nfcid1_skb);
  607. if (r < 0)
  608. goto exit;
  609. if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
  610. r = -EPROTO;
  611. goto exit;
  612. }
  613. memcpy(target->sensf_res, nfcid1_skb->data,
  614. nfcid1_skb->len);
  615. target->sensf_res_len = nfcid1_skb->len;
  616. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  617. }
  618. target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
  619. }
  620. r = 1;
  621. exit:
  622. kfree_skb(nfcid2_skb);
  623. return r;
  624. }
  625. #define ST21NFCA_CB_TYPE_READER_ISO15693 1
  626. static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  627. int err)
  628. {
  629. struct st21nfca_hci_info *info = context;
  630. switch (info->async_cb_type) {
  631. case ST21NFCA_CB_TYPE_READER_ISO15693:
  632. if (err == 0)
  633. skb_trim(skb, skb->len - 1);
  634. info->async_cb(info->async_cb_context, skb, err);
  635. break;
  636. default:
  637. if (err == 0)
  638. kfree_skb(skb);
  639. break;
  640. }
  641. }
  642. /*
  643. * Returns:
  644. * <= 0: driver handled the data exchange
  645. * 1: driver doesn't especially handle, please do standard processing
  646. */
  647. static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
  648. struct nfc_target *target,
  649. struct sk_buff *skb,
  650. data_exchange_cb_t cb, void *cb_context)
  651. {
  652. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  653. pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
  654. target->hci_reader_gate, skb->len);
  655. switch (target->hci_reader_gate) {
  656. case ST21NFCA_RF_READER_F_GATE:
  657. if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
  658. return st21nfca_im_send_dep_req(hdev, skb);
  659. *skb_push(skb, 1) = 0x1a;
  660. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  661. ST21NFCA_WR_XCHG_DATA, skb->data,
  662. skb->len, cb, cb_context);
  663. case ST21NFCA_RF_READER_14443_3_A_GATE:
  664. *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
  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_ISO15693_GATE:
  669. info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
  670. info->async_cb = cb;
  671. info->async_cb_context = cb_context;
  672. *skb_push(skb, 1) = 0x17;
  673. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  674. ST21NFCA_WR_XCHG_DATA, skb->data,
  675. skb->len,
  676. st21nfca_hci_data_exchange_cb,
  677. info);
  678. break;
  679. default:
  680. return 1;
  681. }
  682. }
  683. static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  684. {
  685. return st21nfca_tm_send_dep_res(hdev, skb);
  686. }
  687. static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
  688. struct nfc_target *target)
  689. {
  690. u8 fwi = 0x11;
  691. switch (target->hci_reader_gate) {
  692. case NFC_HCI_RF_READER_A_GATE:
  693. case NFC_HCI_RF_READER_B_GATE:
  694. /*
  695. * PRESENCE_CHECK on those gates is available
  696. * However, the answer to this command is taking 3 * fwi
  697. * if the card is no present.
  698. * Instead, we send an empty I-Frame with a very short
  699. * configurable fwi ~604µs.
  700. */
  701. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  702. ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
  703. case ST21NFCA_RF_READER_14443_3_A_GATE:
  704. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  705. ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
  706. NULL, 0, NULL);
  707. default:
  708. return -EOPNOTSUPP;
  709. }
  710. }
  711. /*
  712. * Returns:
  713. * <= 0: driver handled the event, skb consumed
  714. * 1: driver does not handle the event, please do standard processing
  715. */
  716. static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
  717. u8 event, struct sk_buff *skb)
  718. {
  719. int r;
  720. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  721. pr_debug("hci event: %d\n", event);
  722. switch (event) {
  723. case ST21NFCA_EVT_CARD_ACTIVATED:
  724. if (gate == ST21NFCA_RF_CARD_F_GATE)
  725. info->dep_info.curr_nfc_dep_pni = 0;
  726. break;
  727. case ST21NFCA_EVT_CARD_DEACTIVATED:
  728. break;
  729. case ST21NFCA_EVT_FIELD_ON:
  730. break;
  731. case ST21NFCA_EVT_FIELD_OFF:
  732. break;
  733. case ST21NFCA_EVT_SEND_DATA:
  734. if (gate == ST21NFCA_RF_CARD_F_GATE) {
  735. r = st21nfca_tm_event_send_data(hdev, skb, gate);
  736. if (r < 0)
  737. return r;
  738. return 0;
  739. }
  740. info->dep_info.curr_nfc_dep_pni = 0;
  741. return 1;
  742. default:
  743. return 1;
  744. }
  745. kfree_skb(skb);
  746. return 0;
  747. }
  748. static struct nfc_hci_ops st21nfca_hci_ops = {
  749. .open = st21nfca_hci_open,
  750. .close = st21nfca_hci_close,
  751. .load_session = st21nfca_hci_load_session,
  752. .hci_ready = st21nfca_hci_ready,
  753. .xmit = st21nfca_hci_xmit,
  754. .start_poll = st21nfca_hci_start_poll,
  755. .stop_poll = st21nfca_hci_stop_poll,
  756. .dep_link_up = st21nfca_hci_dep_link_up,
  757. .dep_link_down = st21nfca_hci_dep_link_down,
  758. .target_from_gate = st21nfca_hci_target_from_gate,
  759. .complete_target_discovered = st21nfca_hci_complete_target_discovered,
  760. .im_transceive = st21nfca_hci_im_transceive,
  761. .tm_send = st21nfca_hci_tm_send,
  762. .check_presence = st21nfca_hci_check_presence,
  763. .event_received = st21nfca_hci_event_received,
  764. };
  765. int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
  766. char *llc_name, int phy_headroom, int phy_tailroom,
  767. int phy_payload, struct nfc_hci_dev **hdev)
  768. {
  769. struct st21nfca_hci_info *info;
  770. int r = 0;
  771. int dev_num;
  772. u32 protocols;
  773. struct nfc_hci_init_data init_data;
  774. unsigned long quirks = 0;
  775. info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
  776. if (!info) {
  777. r = -ENOMEM;
  778. goto err_alloc_hdev;
  779. }
  780. info->phy_ops = phy_ops;
  781. info->phy_id = phy_id;
  782. info->state = ST21NFCA_ST_COLD;
  783. mutex_init(&info->info_lock);
  784. init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
  785. memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
  786. /*
  787. * Session id must include the driver name + i2c bus addr
  788. * persistent info to discriminate 2 identical chips
  789. */
  790. dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
  791. if (dev_num >= ST21NFCA_NUM_DEVICES)
  792. return -ENODEV;
  793. set_bit(dev_num, dev_mask);
  794. scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
  795. "ST21AH", dev_num);
  796. protocols = NFC_PROTO_JEWEL_MASK |
  797. NFC_PROTO_MIFARE_MASK |
  798. NFC_PROTO_FELICA_MASK |
  799. NFC_PROTO_ISO14443_MASK |
  800. NFC_PROTO_ISO14443_B_MASK |
  801. NFC_PROTO_ISO15693_MASK |
  802. NFC_PROTO_NFC_DEP_MASK;
  803. set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
  804. info->hdev =
  805. nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
  806. protocols, llc_name,
  807. phy_headroom + ST21NFCA_CMDS_HEADROOM,
  808. phy_tailroom, phy_payload);
  809. if (!info->hdev) {
  810. pr_err("Cannot allocate nfc hdev.\n");
  811. r = -ENOMEM;
  812. goto err_alloc_hdev;
  813. }
  814. nfc_hci_set_clientdata(info->hdev, info);
  815. r = nfc_hci_register_device(info->hdev);
  816. if (r)
  817. goto err_regdev;
  818. *hdev = info->hdev;
  819. st21nfca_dep_init(info->hdev);
  820. return 0;
  821. err_regdev:
  822. nfc_hci_free_device(info->hdev);
  823. err_alloc_hdev:
  824. kfree(info);
  825. return r;
  826. }
  827. EXPORT_SYMBOL(st21nfca_hci_probe);
  828. void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
  829. {
  830. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  831. st21nfca_dep_deinit(hdev);
  832. nfc_hci_unregister_device(hdev);
  833. nfc_hci_free_device(hdev);
  834. kfree(info);
  835. }
  836. EXPORT_SYMBOL(st21nfca_hci_remove);
  837. MODULE_LICENSE("GPL");
  838. MODULE_DESCRIPTION(DRIVER_DESC);