core.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 != ST21NFCA_ESE_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_ese_present &&
  219. info->se_status->is_uicc_present) {
  220. white_list[wl_size++] = NFC_HCI_UICC_HOST_ID;
  221. white_list[wl_size++] = ST21NFCA_ESE_HOST_ID;
  222. } else if (!info->se_status->is_ese_present &&
  223. info->se_status->is_uicc_present) {
  224. white_list[wl_size++] = NFC_HCI_UICC_HOST_ID;
  225. } else if (info->se_status->is_ese_present &&
  226. !info->se_status->is_uicc_present) {
  227. white_list[wl_size++] = ST21NFCA_ESE_HOST_ID;
  228. }
  229. if (wl_size) {
  230. r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
  231. NFC_HCI_ADMIN_WHITELIST,
  232. (u8 *) &white_list, wl_size);
  233. if (r < 0)
  234. return r;
  235. }
  236. /* Set NFC_MODE in device management gate to enable */
  237. r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  238. ST21NFCA_NFC_MODE, &skb);
  239. if (r < 0)
  240. return r;
  241. param = skb->data[0];
  242. kfree_skb(skb);
  243. if (param == 0) {
  244. param = 1;
  245. r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  246. ST21NFCA_NFC_MODE, &param, 1);
  247. if (r < 0)
  248. return r;
  249. }
  250. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  251. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  252. if (r < 0)
  253. return r;
  254. r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
  255. NFC_HCI_ID_MGMT_VERSION_SW, &skb);
  256. if (r < 0)
  257. return r;
  258. if (skb->len != FULL_VERSION_LEN) {
  259. kfree_skb(skb);
  260. return -EINVAL;
  261. }
  262. print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
  263. DUMP_PREFIX_NONE, 16, 1,
  264. skb->data, FULL_VERSION_LEN, false);
  265. kfree_skb(skb);
  266. return 0;
  267. }
  268. static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  269. {
  270. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  271. return info->phy_ops->write(info->phy_id, skb);
  272. }
  273. static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
  274. u32 im_protocols, u32 tm_protocols)
  275. {
  276. int r;
  277. u32 pol_req;
  278. u8 param[19];
  279. struct sk_buff *datarate_skb;
  280. pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
  281. __func__, im_protocols, tm_protocols);
  282. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  283. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  284. if (r < 0)
  285. return r;
  286. if (im_protocols) {
  287. /*
  288. * enable polling according to im_protocols & tm_protocols
  289. * - CLOSE pipe according to im_protocols & tm_protocols
  290. */
  291. if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
  292. r = nfc_hci_disconnect_gate(hdev,
  293. NFC_HCI_RF_READER_B_GATE);
  294. if (r < 0)
  295. return r;
  296. }
  297. if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
  298. r = nfc_hci_disconnect_gate(hdev,
  299. NFC_HCI_RF_READER_A_GATE);
  300. if (r < 0)
  301. return r;
  302. }
  303. if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
  304. r = nfc_hci_disconnect_gate(hdev,
  305. ST21NFCA_RF_READER_F_GATE);
  306. if (r < 0)
  307. return r;
  308. } else {
  309. hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
  310. &hdev->gb_len);
  311. if (hdev->gb == NULL || hdev->gb_len == 0) {
  312. im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  313. tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  314. }
  315. param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
  316. ST21NFCA_RF_READER_F_DATARATE_212 |
  317. ST21NFCA_RF_READER_F_DATARATE_424;
  318. r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
  319. ST21NFCA_RF_READER_F_DATARATE,
  320. param, 1);
  321. if (r < 0)
  322. return r;
  323. pol_req = be32_to_cpu((__force __be32)
  324. ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
  325. r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
  326. ST21NFCA_RF_READER_F_POL_REQ,
  327. (u8 *) &pol_req, 4);
  328. if (r < 0)
  329. return r;
  330. }
  331. if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
  332. r = nfc_hci_disconnect_gate(hdev,
  333. ST21NFCA_RF_READER_14443_3_A_GATE);
  334. if (r < 0)
  335. return r;
  336. }
  337. if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
  338. r = nfc_hci_disconnect_gate(hdev,
  339. ST21NFCA_RF_READER_ISO15693_GATE);
  340. if (r < 0)
  341. return r;
  342. }
  343. r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  344. NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
  345. if (r < 0)
  346. nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
  347. NFC_HCI_EVT_END_OPERATION, NULL, 0);
  348. }
  349. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  350. r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  351. ST21NFCA_RF_CARD_F_DATARATE,
  352. &datarate_skb);
  353. if (r < 0)
  354. return r;
  355. /* Configure the maximum supported datarate to 424Kbps */
  356. if (datarate_skb->len > 0 &&
  357. datarate_skb->data[0] !=
  358. ST21NFCA_RF_CARD_F_DATARATE_212_424) {
  359. param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
  360. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  361. ST21NFCA_RF_CARD_F_DATARATE,
  362. param, 1);
  363. if (r < 0) {
  364. kfree_skb(datarate_skb);
  365. return r;
  366. }
  367. }
  368. kfree_skb(datarate_skb);
  369. /*
  370. * Configure sens_res
  371. *
  372. * NFC Forum Digital Spec Table 7:
  373. * NFCID1 size: triple (10 bytes)
  374. */
  375. param[0] = 0x00;
  376. param[1] = 0x08;
  377. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  378. ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
  379. if (r < 0)
  380. return r;
  381. /*
  382. * Configure sel_res
  383. *
  384. * NFC Forum Digistal Spec Table 17:
  385. * b3 set to 0b (value b7-b6):
  386. * - 10b: Configured for NFC-DEP Protocol
  387. */
  388. param[0] = 0x40;
  389. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  390. ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
  391. if (r < 0)
  392. return r;
  393. /* Configure NFCID1 Random uid */
  394. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  395. ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
  396. if (r < 0)
  397. return r;
  398. /* Configure NFCID2_LIST */
  399. /* System Code */
  400. param[0] = 0x00;
  401. param[1] = 0x00;
  402. /* NFCID2 */
  403. param[2] = 0x01;
  404. param[3] = 0xfe;
  405. param[4] = 'S';
  406. param[5] = 'T';
  407. param[6] = 'M';
  408. param[7] = 'i';
  409. param[8] = 'c';
  410. param[9] = 'r';
  411. /* 8 byte Pad bytes used for polling respone frame */
  412. /*
  413. * Configuration byte:
  414. * - bit 0: define the default NFCID2 entry used when the
  415. * system code is equal to 'FFFF'
  416. * - bit 1: use a random value for lowest 6 bytes of
  417. * NFCID2 value
  418. * - bit 2: ignore polling request frame if request code
  419. * is equal to '01'
  420. * - Other bits are RFU
  421. */
  422. param[18] = 0x01;
  423. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  424. ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
  425. 19);
  426. if (r < 0)
  427. return r;
  428. param[0] = 0x02;
  429. r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
  430. ST21NFCA_RF_CARD_F_MODE, param, 1);
  431. }
  432. return r;
  433. }
  434. static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
  435. {
  436. nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  437. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  438. }
  439. static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
  440. {
  441. int r;
  442. struct sk_buff *atqa_skb = NULL;
  443. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  444. ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
  445. if (r < 0)
  446. goto exit;
  447. if (atqa_skb->len != 2) {
  448. r = -EPROTO;
  449. goto exit;
  450. }
  451. *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
  452. exit:
  453. kfree_skb(atqa_skb);
  454. return r;
  455. }
  456. static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
  457. {
  458. int r;
  459. struct sk_buff *sak_skb = NULL;
  460. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  461. ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
  462. if (r < 0)
  463. goto exit;
  464. if (sak_skb->len != 1) {
  465. r = -EPROTO;
  466. goto exit;
  467. }
  468. *sak = sak_skb->data[0];
  469. exit:
  470. kfree_skb(sak_skb);
  471. return r;
  472. }
  473. static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *uid,
  474. int *len)
  475. {
  476. int r;
  477. struct sk_buff *uid_skb = NULL;
  478. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
  479. ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
  480. if (r < 0)
  481. goto exit;
  482. if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
  483. r = -EPROTO;
  484. goto exit;
  485. }
  486. memcpy(uid, uid_skb->data, uid_skb->len);
  487. *len = uid_skb->len;
  488. exit:
  489. kfree_skb(uid_skb);
  490. return r;
  491. }
  492. static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
  493. struct nfc_target *target)
  494. {
  495. int r;
  496. struct sk_buff *inventory_skb = NULL;
  497. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
  498. ST21NFCA_RF_READER_ISO15693_INVENTORY,
  499. &inventory_skb);
  500. if (r < 0)
  501. goto exit;
  502. skb_pull(inventory_skb, 2);
  503. if (inventory_skb->len == 0 ||
  504. inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
  505. r = -EPROTO;
  506. goto exit;
  507. }
  508. memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
  509. target->iso15693_dsfid = inventory_skb->data[1];
  510. target->is_iso15693 = 1;
  511. exit:
  512. kfree_skb(inventory_skb);
  513. return r;
  514. }
  515. static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
  516. struct nfc_target *target, u8 comm_mode,
  517. u8 *gb, size_t gb_len)
  518. {
  519. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  520. info->dep_info.idx = target->idx;
  521. return st21nfca_im_send_atr_req(hdev, gb, gb_len);
  522. }
  523. static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
  524. {
  525. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  526. info->state = ST21NFCA_ST_READY;
  527. return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
  528. ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
  529. }
  530. static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  531. struct nfc_target *target)
  532. {
  533. int r, len;
  534. u16 atqa;
  535. u8 sak;
  536. u8 uid[NFC_NFCID1_MAXSIZE];
  537. switch (gate) {
  538. case ST21NFCA_RF_READER_F_GATE:
  539. target->supported_protocols = NFC_PROTO_FELICA_MASK;
  540. break;
  541. case ST21NFCA_RF_READER_14443_3_A_GATE:
  542. /* ISO14443-3 type 1 or 2 tags */
  543. r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
  544. if (r < 0)
  545. return r;
  546. if (atqa == 0x000c) {
  547. target->supported_protocols = NFC_PROTO_JEWEL_MASK;
  548. target->sens_res = 0x0c00;
  549. } else {
  550. r = st21nfca_get_iso14443_3_sak(hdev, &sak);
  551. if (r < 0)
  552. return r;
  553. r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
  554. if (r < 0)
  555. return r;
  556. target->supported_protocols =
  557. nfc_hci_sak_to_protocol(sak);
  558. if (target->supported_protocols == 0xffffffff)
  559. return -EPROTO;
  560. target->sens_res = atqa;
  561. target->sel_res = sak;
  562. memcpy(target->nfcid1, uid, len);
  563. target->nfcid1_len = len;
  564. }
  565. break;
  566. case ST21NFCA_RF_READER_ISO15693_GATE:
  567. target->supported_protocols = NFC_PROTO_ISO15693_MASK;
  568. r = st21nfca_get_iso15693_inventory(hdev, target);
  569. if (r < 0)
  570. return r;
  571. break;
  572. default:
  573. return -EPROTO;
  574. }
  575. return 0;
  576. }
  577. static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
  578. u8 gate,
  579. struct nfc_target *target)
  580. {
  581. int r;
  582. struct sk_buff *nfcid_skb = NULL;
  583. if (gate == ST21NFCA_RF_READER_F_GATE) {
  584. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  585. ST21NFCA_RF_READER_F_NFCID2, &nfcid_skb);
  586. if (r < 0)
  587. goto exit;
  588. if (nfcid_skb->len > NFC_SENSF_RES_MAXSIZE) {
  589. r = -EPROTO;
  590. goto exit;
  591. }
  592. /*
  593. * - After the recepton of polling response for type F frame
  594. * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
  595. * updated.
  596. * - After the reception of SEL_RES with NFCIP-1 compliant bit
  597. * set for type A frame NFCID1 will be updated
  598. */
  599. if (nfcid_skb->len > 0) {
  600. /* P2P in type F */
  601. memcpy(target->sensf_res, nfcid_skb->data,
  602. nfcid_skb->len);
  603. target->sensf_res_len = nfcid_skb->len;
  604. /* NFC Forum Digital Protocol Table 44 */
  605. if (target->sensf_res[0] == 0x01 &&
  606. target->sensf_res[1] == 0xfe)
  607. target->supported_protocols =
  608. NFC_PROTO_NFC_DEP_MASK;
  609. else
  610. target->supported_protocols =
  611. NFC_PROTO_FELICA_MASK;
  612. } else {
  613. kfree_skb(nfcid_skb);
  614. /* P2P in type A */
  615. r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
  616. ST21NFCA_RF_READER_F_NFCID1,
  617. &nfcid_skb);
  618. if (r < 0)
  619. goto exit;
  620. if (nfcid_skb->len > NFC_NFCID1_MAXSIZE) {
  621. r = -EPROTO;
  622. goto exit;
  623. }
  624. memcpy(target->sensf_res, nfcid_skb->data,
  625. nfcid_skb->len);
  626. target->sensf_res_len = nfcid_skb->len;
  627. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  628. }
  629. target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
  630. }
  631. r = 1;
  632. exit:
  633. kfree_skb(nfcid_skb);
  634. return r;
  635. }
  636. #define ST21NFCA_CB_TYPE_READER_ISO15693 1
  637. static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
  638. int err)
  639. {
  640. struct st21nfca_hci_info *info = context;
  641. switch (info->async_cb_type) {
  642. case ST21NFCA_CB_TYPE_READER_ISO15693:
  643. if (err == 0)
  644. skb_trim(skb, skb->len - 1);
  645. info->async_cb(info->async_cb_context, skb, err);
  646. break;
  647. default:
  648. if (err == 0)
  649. kfree_skb(skb);
  650. break;
  651. }
  652. }
  653. /*
  654. * Returns:
  655. * <= 0: driver handled the data exchange
  656. * 1: driver doesn't especially handle, please do standard processing
  657. */
  658. static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
  659. struct nfc_target *target,
  660. struct sk_buff *skb,
  661. data_exchange_cb_t cb, void *cb_context)
  662. {
  663. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  664. pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
  665. target->hci_reader_gate, skb->len);
  666. switch (target->hci_reader_gate) {
  667. case ST21NFCA_RF_READER_F_GATE:
  668. if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
  669. return st21nfca_im_send_dep_req(hdev, skb);
  670. *skb_push(skb, 1) = 0x1a;
  671. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  672. ST21NFCA_WR_XCHG_DATA, skb->data,
  673. skb->len, cb, cb_context);
  674. case ST21NFCA_RF_READER_14443_3_A_GATE:
  675. *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
  676. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  677. ST21NFCA_WR_XCHG_DATA, skb->data,
  678. skb->len, cb, cb_context);
  679. case ST21NFCA_RF_READER_ISO15693_GATE:
  680. info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
  681. info->async_cb = cb;
  682. info->async_cb_context = cb_context;
  683. *skb_push(skb, 1) = 0x17;
  684. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  685. ST21NFCA_WR_XCHG_DATA, skb->data,
  686. skb->len,
  687. st21nfca_hci_data_exchange_cb,
  688. info);
  689. break;
  690. default:
  691. return 1;
  692. }
  693. }
  694. static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  695. {
  696. return st21nfca_tm_send_dep_res(hdev, skb);
  697. }
  698. static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
  699. struct nfc_target *target)
  700. {
  701. u8 fwi = 0x11;
  702. switch (target->hci_reader_gate) {
  703. case NFC_HCI_RF_READER_A_GATE:
  704. case NFC_HCI_RF_READER_B_GATE:
  705. /*
  706. * PRESENCE_CHECK on those gates is available
  707. * However, the answer to this command is taking 3 * fwi
  708. * if the card is no present.
  709. * Instead, we send an empty I-Frame with a very short
  710. * configurable fwi ~604µs.
  711. */
  712. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  713. ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
  714. case ST21NFCA_RF_READER_14443_3_A_GATE:
  715. return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
  716. ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
  717. NULL, 0, NULL);
  718. default:
  719. return -EOPNOTSUPP;
  720. }
  721. }
  722. static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
  723. struct sk_buff *skb)
  724. {
  725. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  726. u8 gate = hdev->pipes[pipe].gate;
  727. pr_debug("cmd: %x\n", cmd);
  728. switch (cmd) {
  729. case NFC_HCI_ANY_OPEN_PIPE:
  730. if (gate != ST21NFCA_APDU_READER_GATE &&
  731. hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID)
  732. info->se_info.count_pipes++;
  733. if (info->se_info.count_pipes == info->se_info.expected_pipes) {
  734. del_timer_sync(&info->se_info.se_active_timer);
  735. info->se_info.se_active = false;
  736. info->se_info.count_pipes = 0;
  737. complete(&info->se_info.req_completion);
  738. }
  739. break;
  740. }
  741. }
  742. static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event,
  743. struct sk_buff *skb)
  744. {
  745. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  746. pr_debug("admin event: %x\n", event);
  747. switch (event) {
  748. case ST21NFCA_EVT_HOT_PLUG:
  749. if (info->se_info.se_active) {
  750. if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
  751. del_timer_sync(&info->se_info.se_active_timer);
  752. info->se_info.se_active = false;
  753. complete(&info->se_info.req_completion);
  754. } else {
  755. mod_timer(&info->se_info.se_active_timer,
  756. jiffies +
  757. msecs_to_jiffies(ST21NFCA_SE_TO_PIPES));
  758. }
  759. }
  760. break;
  761. default:
  762. nfc_err(&hdev->ndev->dev, "Unexpected event on admin gate\n");
  763. }
  764. kfree_skb(skb);
  765. return 0;
  766. }
  767. /*
  768. * Returns:
  769. * <= 0: driver handled the event, skb consumed
  770. * 1: driver does not handle the event, please do standard processing
  771. */
  772. static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe,
  773. u8 event, struct sk_buff *skb)
  774. {
  775. u8 gate = hdev->pipes[pipe].gate;
  776. u8 host = hdev->pipes[pipe].dest_host;
  777. pr_debug("hci event: %d gate: %x\n", event, gate);
  778. switch (gate) {
  779. case NFC_HCI_ADMIN_GATE:
  780. return st21nfca_admin_event_received(hdev, event, skb);
  781. case ST21NFCA_RF_CARD_F_GATE:
  782. return st21nfca_dep_event_received(hdev, event, skb);
  783. case ST21NFCA_CONNECTIVITY_GATE:
  784. return st21nfca_connectivity_event_received(hdev, host,
  785. event, skb);
  786. case ST21NFCA_APDU_READER_GATE:
  787. return st21nfca_apdu_reader_event_received(hdev, event, skb);
  788. case NFC_HCI_LOOPBACK_GATE:
  789. return st21nfca_hci_loopback_event_received(hdev, event, skb);
  790. default:
  791. return 1;
  792. }
  793. }
  794. static struct nfc_hci_ops st21nfca_hci_ops = {
  795. .open = st21nfca_hci_open,
  796. .close = st21nfca_hci_close,
  797. .load_session = st21nfca_hci_load_session,
  798. .hci_ready = st21nfca_hci_ready,
  799. .xmit = st21nfca_hci_xmit,
  800. .start_poll = st21nfca_hci_start_poll,
  801. .stop_poll = st21nfca_hci_stop_poll,
  802. .dep_link_up = st21nfca_hci_dep_link_up,
  803. .dep_link_down = st21nfca_hci_dep_link_down,
  804. .target_from_gate = st21nfca_hci_target_from_gate,
  805. .complete_target_discovered = st21nfca_hci_complete_target_discovered,
  806. .im_transceive = st21nfca_hci_im_transceive,
  807. .tm_send = st21nfca_hci_tm_send,
  808. .check_presence = st21nfca_hci_check_presence,
  809. .event_received = st21nfca_hci_event_received,
  810. .cmd_received = st21nfca_hci_cmd_received,
  811. .discover_se = st21nfca_hci_discover_se,
  812. .enable_se = st21nfca_hci_enable_se,
  813. .disable_se = st21nfca_hci_disable_se,
  814. .se_io = st21nfca_hci_se_io,
  815. };
  816. int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
  817. char *llc_name, int phy_headroom, int phy_tailroom,
  818. int phy_payload, struct nfc_hci_dev **hdev,
  819. struct st21nfca_se_status *se_status)
  820. {
  821. struct st21nfca_hci_info *info;
  822. int r = 0;
  823. int dev_num;
  824. u32 protocols;
  825. struct nfc_hci_init_data init_data;
  826. unsigned long quirks = 0;
  827. info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
  828. if (!info) {
  829. r = -ENOMEM;
  830. goto err_alloc_hdev;
  831. }
  832. info->phy_ops = phy_ops;
  833. info->phy_id = phy_id;
  834. info->state = ST21NFCA_ST_COLD;
  835. mutex_init(&info->info_lock);
  836. init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
  837. memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
  838. /*
  839. * Session id must include the driver name + i2c bus addr
  840. * persistent info to discriminate 2 identical chips
  841. */
  842. dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
  843. if (dev_num >= ST21NFCA_NUM_DEVICES)
  844. return -ENODEV;
  845. set_bit(dev_num, dev_mask);
  846. scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
  847. "ST21AH", dev_num);
  848. protocols = NFC_PROTO_JEWEL_MASK |
  849. NFC_PROTO_MIFARE_MASK |
  850. NFC_PROTO_FELICA_MASK |
  851. NFC_PROTO_ISO14443_MASK |
  852. NFC_PROTO_ISO14443_B_MASK |
  853. NFC_PROTO_ISO15693_MASK |
  854. NFC_PROTO_NFC_DEP_MASK;
  855. set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
  856. info->hdev =
  857. nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
  858. protocols, llc_name,
  859. phy_headroom + ST21NFCA_CMDS_HEADROOM,
  860. phy_tailroom, phy_payload);
  861. if (!info->hdev) {
  862. pr_err("Cannot allocate nfc hdev.\n");
  863. r = -ENOMEM;
  864. goto err_alloc_hdev;
  865. }
  866. info->se_status = se_status;
  867. nfc_hci_set_clientdata(info->hdev, info);
  868. r = nfc_hci_register_device(info->hdev);
  869. if (r)
  870. goto err_regdev;
  871. *hdev = info->hdev;
  872. st21nfca_dep_init(info->hdev);
  873. st21nfca_se_init(info->hdev);
  874. st21nfca_vendor_cmds_init(info->hdev);
  875. return 0;
  876. err_regdev:
  877. nfc_hci_free_device(info->hdev);
  878. err_alloc_hdev:
  879. kfree(info);
  880. return r;
  881. }
  882. EXPORT_SYMBOL(st21nfca_hci_probe);
  883. void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
  884. {
  885. struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
  886. st21nfca_dep_deinit(hdev);
  887. st21nfca_se_deinit(hdev);
  888. nfc_hci_unregister_device(hdev);
  889. nfc_hci_free_device(hdev);
  890. kfree(info);
  891. }
  892. EXPORT_SYMBOL(st21nfca_hci_remove);
  893. MODULE_LICENSE("GPL");
  894. MODULE_DESCRIPTION(DRIVER_DESC);