microread.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * HCI based Driver for Inside Secure microread NFC Chip
  3. *
  4. * Copyright (C) 2013 Intel Corporation. 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/crc-ccitt.h>
  23. #include <linux/nfc.h>
  24. #include <net/nfc/nfc.h>
  25. #include <net/nfc/hci.h>
  26. #include <net/nfc/llc.h>
  27. #include "microread.h"
  28. /* Proprietary gates, events, commands and registers */
  29. /* Admin */
  30. #define MICROREAD_GATE_ID_ADM NFC_HCI_ADMIN_GATE
  31. #define MICROREAD_GATE_ID_MGT 0x01
  32. #define MICROREAD_GATE_ID_OS 0x02
  33. #define MICROREAD_GATE_ID_TESTRF 0x03
  34. #define MICROREAD_GATE_ID_LOOPBACK NFC_HCI_LOOPBACK_GATE
  35. #define MICROREAD_GATE_ID_IDT NFC_HCI_ID_MGMT_GATE
  36. #define MICROREAD_GATE_ID_LMS NFC_HCI_LINK_MGMT_GATE
  37. /* Reader */
  38. #define MICROREAD_GATE_ID_MREAD_GEN 0x10
  39. #define MICROREAD_GATE_ID_MREAD_ISO_B NFC_HCI_RF_READER_B_GATE
  40. #define MICROREAD_GATE_ID_MREAD_NFC_T1 0x12
  41. #define MICROREAD_GATE_ID_MREAD_ISO_A NFC_HCI_RF_READER_A_GATE
  42. #define MICROREAD_GATE_ID_MREAD_NFC_T3 0x14
  43. #define MICROREAD_GATE_ID_MREAD_ISO_15_3 0x15
  44. #define MICROREAD_GATE_ID_MREAD_ISO_15_2 0x16
  45. #define MICROREAD_GATE_ID_MREAD_ISO_B_3 0x17
  46. #define MICROREAD_GATE_ID_MREAD_BPRIME 0x18
  47. #define MICROREAD_GATE_ID_MREAD_ISO_A_3 0x19
  48. /* Card */
  49. #define MICROREAD_GATE_ID_MCARD_GEN 0x20
  50. #define MICROREAD_GATE_ID_MCARD_ISO_B 0x21
  51. #define MICROREAD_GATE_ID_MCARD_BPRIME 0x22
  52. #define MICROREAD_GATE_ID_MCARD_ISO_A 0x23
  53. #define MICROREAD_GATE_ID_MCARD_NFC_T3 0x24
  54. #define MICROREAD_GATE_ID_MCARD_ISO_15_3 0x25
  55. #define MICROREAD_GATE_ID_MCARD_ISO_15_2 0x26
  56. #define MICROREAD_GATE_ID_MCARD_ISO_B_2 0x27
  57. #define MICROREAD_GATE_ID_MCARD_ISO_CUSTOM 0x28
  58. #define MICROREAD_GATE_ID_SECURE_ELEMENT 0x2F
  59. /* P2P */
  60. #define MICROREAD_GATE_ID_P2P_GEN 0x30
  61. #define MICROREAD_GATE_ID_P2P_TARGET 0x31
  62. #define MICROREAD_PAR_P2P_TARGET_MODE 0x01
  63. #define MICROREAD_PAR_P2P_TARGET_GT 0x04
  64. #define MICROREAD_GATE_ID_P2P_INITIATOR 0x32
  65. #define MICROREAD_PAR_P2P_INITIATOR_GI 0x01
  66. #define MICROREAD_PAR_P2P_INITIATOR_GT 0x03
  67. /* Those pipes are created/opened by default in the chip */
  68. #define MICROREAD_PIPE_ID_LMS 0x00
  69. #define MICROREAD_PIPE_ID_ADMIN 0x01
  70. #define MICROREAD_PIPE_ID_MGT 0x02
  71. #define MICROREAD_PIPE_ID_OS 0x03
  72. #define MICROREAD_PIPE_ID_HDS_LOOPBACK 0x04
  73. #define MICROREAD_PIPE_ID_HDS_IDT 0x05
  74. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B 0x08
  75. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_BPRIME 0x09
  76. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_A 0x0A
  77. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_3 0x0B
  78. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_2 0x0C
  79. #define MICROREAD_PIPE_ID_HDS_MCARD_NFC_T3 0x0D
  80. #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B_2 0x0E
  81. #define MICROREAD_PIPE_ID_HDS_MCARD_CUSTOM 0x0F
  82. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B 0x10
  83. #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1 0x11
  84. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A 0x12
  85. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_3 0x13
  86. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_2 0x14
  87. #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3 0x15
  88. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B_3 0x16
  89. #define MICROREAD_PIPE_ID_HDS_MREAD_BPRIME 0x17
  90. #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3 0x18
  91. #define MICROREAD_PIPE_ID_HDS_MREAD_GEN 0x1B
  92. #define MICROREAD_PIPE_ID_HDS_STACKED_ELEMENT 0x1C
  93. #define MICROREAD_PIPE_ID_HDS_INSTANCES 0x1D
  94. #define MICROREAD_PIPE_ID_HDS_TESTRF 0x1E
  95. #define MICROREAD_PIPE_ID_HDS_P2P_TARGET 0x1F
  96. #define MICROREAD_PIPE_ID_HDS_P2P_INITIATOR 0x20
  97. /* Events */
  98. #define MICROREAD_EVT_MREAD_DISCOVERY_OCCURED NFC_HCI_EVT_TARGET_DISCOVERED
  99. #define MICROREAD_EVT_MREAD_CARD_FOUND 0x3D
  100. #define MICROREAD_EMCF_A_ATQA 0
  101. #define MICROREAD_EMCF_A_SAK 2
  102. #define MICROREAD_EMCF_A_LEN 3
  103. #define MICROREAD_EMCF_A_UID 4
  104. #define MICROREAD_EMCF_A3_ATQA 0
  105. #define MICROREAD_EMCF_A3_SAK 2
  106. #define MICROREAD_EMCF_A3_LEN 3
  107. #define MICROREAD_EMCF_A3_UID 4
  108. #define MICROREAD_EMCF_B_UID 0
  109. #define MICROREAD_EMCF_T1_ATQA 0
  110. #define MICROREAD_EMCF_T1_UID 4
  111. #define MICROREAD_EMCF_T3_UID 0
  112. #define MICROREAD_EVT_MREAD_DISCOVERY_START NFC_HCI_EVT_READER_REQUESTED
  113. #define MICROREAD_EVT_MREAD_DISCOVERY_START_SOME 0x3E
  114. #define MICROREAD_EVT_MREAD_DISCOVERY_STOP NFC_HCI_EVT_END_OPERATION
  115. #define MICROREAD_EVT_MREAD_SIM_REQUESTS 0x3F
  116. #define MICROREAD_EVT_MCARD_EXCHANGE NFC_HCI_EVT_TARGET_DISCOVERED
  117. #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF 0x20
  118. #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF 0x21
  119. #define MICROREAD_EVT_MCARD_FIELD_ON 0x11
  120. #define MICROREAD_EVT_P2P_TARGET_ACTIVATED 0x13
  121. #define MICROREAD_EVT_P2P_TARGET_DEACTIVATED 0x12
  122. #define MICROREAD_EVT_MCARD_FIELD_OFF 0x14
  123. /* Commands */
  124. #define MICROREAD_CMD_MREAD_EXCHANGE 0x10
  125. #define MICROREAD_CMD_MREAD_SUBSCRIBE 0x3F
  126. /* Hosts IDs */
  127. #define MICROREAD_ELT_ID_HDS NFC_HCI_TERMINAL_HOST_ID
  128. #define MICROREAD_ELT_ID_SIM NFC_HCI_UICC_HOST_ID
  129. #define MICROREAD_ELT_ID_SE1 0x03
  130. #define MICROREAD_ELT_ID_SE2 0x04
  131. #define MICROREAD_ELT_ID_SE3 0x05
  132. static struct nfc_hci_gate microread_gates[] = {
  133. {MICROREAD_GATE_ID_ADM, MICROREAD_PIPE_ID_ADMIN},
  134. {MICROREAD_GATE_ID_LOOPBACK, MICROREAD_PIPE_ID_HDS_LOOPBACK},
  135. {MICROREAD_GATE_ID_IDT, MICROREAD_PIPE_ID_HDS_IDT},
  136. {MICROREAD_GATE_ID_LMS, MICROREAD_PIPE_ID_LMS},
  137. {MICROREAD_GATE_ID_MREAD_ISO_B, MICROREAD_PIPE_ID_HDS_MREAD_ISO_B},
  138. {MICROREAD_GATE_ID_MREAD_ISO_A, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A},
  139. {MICROREAD_GATE_ID_MREAD_ISO_A_3, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3},
  140. {MICROREAD_GATE_ID_MGT, MICROREAD_PIPE_ID_MGT},
  141. {MICROREAD_GATE_ID_OS, MICROREAD_PIPE_ID_OS},
  142. {MICROREAD_GATE_ID_MREAD_NFC_T1, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1},
  143. {MICROREAD_GATE_ID_MREAD_NFC_T3, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3},
  144. {MICROREAD_GATE_ID_P2P_TARGET, MICROREAD_PIPE_ID_HDS_P2P_TARGET},
  145. {MICROREAD_GATE_ID_P2P_INITIATOR, MICROREAD_PIPE_ID_HDS_P2P_INITIATOR}
  146. };
  147. /* Largest headroom needed for outgoing custom commands */
  148. #define MICROREAD_CMDS_HEADROOM 2
  149. #define MICROREAD_CMD_TAILROOM 2
  150. struct microread_info {
  151. struct nfc_phy_ops *phy_ops;
  152. void *phy_id;
  153. struct nfc_hci_dev *hdev;
  154. int async_cb_type;
  155. data_exchange_cb_t async_cb;
  156. void *async_cb_context;
  157. };
  158. static int microread_open(struct nfc_hci_dev *hdev)
  159. {
  160. struct microread_info *info = nfc_hci_get_clientdata(hdev);
  161. return info->phy_ops->enable(info->phy_id);
  162. }
  163. static void microread_close(struct nfc_hci_dev *hdev)
  164. {
  165. struct microread_info *info = nfc_hci_get_clientdata(hdev);
  166. info->phy_ops->disable(info->phy_id);
  167. }
  168. static int microread_hci_ready(struct nfc_hci_dev *hdev)
  169. {
  170. int r;
  171. u8 param[4];
  172. param[0] = 0x03;
  173. r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
  174. MICROREAD_CMD_MREAD_SUBSCRIBE, param, 1, NULL);
  175. if (r)
  176. return r;
  177. r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A_3,
  178. MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
  179. if (r)
  180. return r;
  181. param[0] = 0x00;
  182. param[1] = 0x03;
  183. param[2] = 0x00;
  184. r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_B,
  185. MICROREAD_CMD_MREAD_SUBSCRIBE, param, 3, NULL);
  186. if (r)
  187. return r;
  188. r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T1,
  189. MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
  190. if (r)
  191. return r;
  192. param[0] = 0xFF;
  193. param[1] = 0xFF;
  194. param[2] = 0x00;
  195. param[3] = 0x00;
  196. r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T3,
  197. MICROREAD_CMD_MREAD_SUBSCRIBE, param, 4, NULL);
  198. return r;
  199. }
  200. static int microread_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  201. {
  202. struct microread_info *info = nfc_hci_get_clientdata(hdev);
  203. return info->phy_ops->write(info->phy_id, skb);
  204. }
  205. static int microread_start_poll(struct nfc_hci_dev *hdev,
  206. u32 im_protocols, u32 tm_protocols)
  207. {
  208. int r;
  209. u8 param[2];
  210. u8 mode;
  211. param[0] = 0x00;
  212. param[1] = 0x00;
  213. if (im_protocols & NFC_PROTO_ISO14443_MASK)
  214. param[0] |= (1 << 2);
  215. if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
  216. param[0] |= 1;
  217. if (im_protocols & NFC_PROTO_MIFARE_MASK)
  218. param[1] |= 1;
  219. if (im_protocols & NFC_PROTO_JEWEL_MASK)
  220. param[0] |= (1 << 1);
  221. if (im_protocols & NFC_PROTO_FELICA_MASK)
  222. param[0] |= (1 << 5);
  223. if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
  224. param[1] |= (1 << 1);
  225. if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
  226. hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
  227. &hdev->gb_len);
  228. if (hdev->gb == NULL || hdev->gb_len == 0) {
  229. im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  230. tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
  231. }
  232. }
  233. r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
  234. MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
  235. if (r)
  236. return r;
  237. mode = 0xff;
  238. r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
  239. MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
  240. if (r)
  241. return r;
  242. if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  243. r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
  244. MICROREAD_PAR_P2P_INITIATOR_GI,
  245. hdev->gb, hdev->gb_len);
  246. if (r)
  247. return r;
  248. }
  249. if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
  250. r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
  251. MICROREAD_PAR_P2P_TARGET_GT,
  252. hdev->gb, hdev->gb_len);
  253. if (r)
  254. return r;
  255. mode = 0x02;
  256. r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
  257. MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
  258. if (r)
  259. return r;
  260. }
  261. return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
  262. MICROREAD_EVT_MREAD_DISCOVERY_START_SOME,
  263. param, 2);
  264. }
  265. static int microread_dep_link_up(struct nfc_hci_dev *hdev,
  266. struct nfc_target *target, u8 comm_mode,
  267. u8 *gb, size_t gb_len)
  268. {
  269. struct sk_buff *rgb_skb = NULL;
  270. int r;
  271. r = nfc_hci_get_param(hdev, target->hci_reader_gate,
  272. MICROREAD_PAR_P2P_INITIATOR_GT, &rgb_skb);
  273. if (r < 0)
  274. return r;
  275. if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
  276. r = -EPROTO;
  277. goto exit;
  278. }
  279. r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
  280. rgb_skb->len);
  281. if (r == 0)
  282. r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
  283. NFC_RF_INITIATOR);
  284. exit:
  285. kfree_skb(rgb_skb);
  286. return r;
  287. }
  288. static int microread_dep_link_down(struct nfc_hci_dev *hdev)
  289. {
  290. return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
  291. MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
  292. }
  293. static int microread_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
  294. struct nfc_target *target)
  295. {
  296. switch (gate) {
  297. case MICROREAD_GATE_ID_P2P_INITIATOR:
  298. target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  299. break;
  300. default:
  301. return -EPROTO;
  302. }
  303. return 0;
  304. }
  305. static int microread_complete_target_discovered(struct nfc_hci_dev *hdev,
  306. u8 gate,
  307. struct nfc_target *target)
  308. {
  309. return 0;
  310. }
  311. #define MICROREAD_CB_TYPE_READER_ALL 1
  312. static void microread_im_transceive_cb(void *context, struct sk_buff *skb,
  313. int err)
  314. {
  315. struct microread_info *info = context;
  316. switch (info->async_cb_type) {
  317. case MICROREAD_CB_TYPE_READER_ALL:
  318. if (err == 0) {
  319. if (skb->len == 0) {
  320. err = -EPROTO;
  321. kfree_skb(skb);
  322. info->async_cb(info->async_cb_context, NULL,
  323. -EPROTO);
  324. return;
  325. }
  326. if (skb->data[skb->len - 1] != 0) {
  327. err = nfc_hci_result_to_errno(
  328. skb->data[skb->len - 1]);
  329. kfree_skb(skb);
  330. info->async_cb(info->async_cb_context, NULL,
  331. err);
  332. return;
  333. }
  334. skb_trim(skb, skb->len - 1); /* RF Error ind. */
  335. }
  336. info->async_cb(info->async_cb_context, skb, err);
  337. break;
  338. default:
  339. if (err == 0)
  340. kfree_skb(skb);
  341. break;
  342. }
  343. }
  344. /*
  345. * Returns:
  346. * <= 0: driver handled the data exchange
  347. * 1: driver doesn't especially handle, please do standard processing
  348. */
  349. static int microread_im_transceive(struct nfc_hci_dev *hdev,
  350. struct nfc_target *target,
  351. struct sk_buff *skb, data_exchange_cb_t cb,
  352. void *cb_context)
  353. {
  354. struct microread_info *info = nfc_hci_get_clientdata(hdev);
  355. u8 control_bits;
  356. u16 crc;
  357. pr_info("data exchange to gate 0x%x\n", target->hci_reader_gate);
  358. if (target->hci_reader_gate == MICROREAD_GATE_ID_P2P_INITIATOR) {
  359. *skb_push(skb, 1) = 0;
  360. return nfc_hci_send_event(hdev, target->hci_reader_gate,
  361. MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF,
  362. skb->data, skb->len);
  363. }
  364. switch (target->hci_reader_gate) {
  365. case MICROREAD_GATE_ID_MREAD_ISO_A:
  366. control_bits = 0xCB;
  367. break;
  368. case MICROREAD_GATE_ID_MREAD_ISO_A_3:
  369. control_bits = 0xCB;
  370. break;
  371. case MICROREAD_GATE_ID_MREAD_ISO_B:
  372. control_bits = 0xCB;
  373. break;
  374. case MICROREAD_GATE_ID_MREAD_NFC_T1:
  375. control_bits = 0x1B;
  376. crc = crc_ccitt(0xffff, skb->data, skb->len);
  377. crc = ~crc;
  378. *skb_put(skb, 1) = crc & 0xff;
  379. *skb_put(skb, 1) = crc >> 8;
  380. break;
  381. case MICROREAD_GATE_ID_MREAD_NFC_T3:
  382. control_bits = 0xDB;
  383. break;
  384. default:
  385. pr_info("Abort im_transceive to invalid gate 0x%x\n",
  386. target->hci_reader_gate);
  387. return 1;
  388. }
  389. *skb_push(skb, 1) = control_bits;
  390. info->async_cb_type = MICROREAD_CB_TYPE_READER_ALL;
  391. info->async_cb = cb;
  392. info->async_cb_context = cb_context;
  393. return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
  394. MICROREAD_CMD_MREAD_EXCHANGE,
  395. skb->data, skb->len,
  396. microread_im_transceive_cb, info);
  397. }
  398. static int microread_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
  399. {
  400. int r;
  401. r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_TARGET,
  402. MICROREAD_EVT_MCARD_EXCHANGE,
  403. skb->data, skb->len);
  404. kfree_skb(skb);
  405. return r;
  406. }
  407. static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
  408. struct sk_buff *skb)
  409. {
  410. struct nfc_target *targets;
  411. int r = 0;
  412. pr_info("target discovered to gate 0x%x\n", gate);
  413. targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
  414. if (targets == NULL) {
  415. r = -ENOMEM;
  416. goto exit;
  417. }
  418. targets->hci_reader_gate = gate;
  419. switch (gate) {
  420. case MICROREAD_GATE_ID_MREAD_ISO_A:
  421. targets->supported_protocols =
  422. nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A_SAK]);
  423. targets->sens_res =
  424. be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
  425. targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
  426. targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
  427. if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
  428. r = -EINVAL;
  429. goto exit_free;
  430. }
  431. memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
  432. targets->nfcid1_len);
  433. break;
  434. case MICROREAD_GATE_ID_MREAD_ISO_A_3:
  435. targets->supported_protocols =
  436. nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A3_SAK]);
  437. targets->sens_res =
  438. be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);
  439. targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];
  440. targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
  441. if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
  442. r = -EINVAL;
  443. goto exit_free;
  444. }
  445. memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
  446. targets->nfcid1_len);
  447. break;
  448. case MICROREAD_GATE_ID_MREAD_ISO_B:
  449. targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  450. memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_B_UID], 4);
  451. targets->nfcid1_len = 4;
  452. break;
  453. case MICROREAD_GATE_ID_MREAD_NFC_T1:
  454. targets->supported_protocols = NFC_PROTO_JEWEL_MASK;
  455. targets->sens_res =
  456. le16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_T1_ATQA]);
  457. memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T1_UID], 4);
  458. targets->nfcid1_len = 4;
  459. break;
  460. case MICROREAD_GATE_ID_MREAD_NFC_T3:
  461. targets->supported_protocols = NFC_PROTO_FELICA_MASK;
  462. memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T3_UID], 8);
  463. targets->nfcid1_len = 8;
  464. break;
  465. default:
  466. pr_info("discard target discovered to gate 0x%x\n", gate);
  467. goto exit_free;
  468. }
  469. r = nfc_targets_found(hdev->ndev, targets, 1);
  470. exit_free:
  471. kfree(targets);
  472. exit:
  473. kfree_skb(skb);
  474. if (r)
  475. pr_err("Failed to handle discovered target err=%d\n", r);
  476. }
  477. static int microread_event_received(struct nfc_hci_dev *hdev, u8 pipe,
  478. u8 event, struct sk_buff *skb)
  479. {
  480. int r;
  481. u8 gate = hdev->pipes[pipe].gate;
  482. u8 mode;
  483. pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate);
  484. switch (event) {
  485. case MICROREAD_EVT_MREAD_CARD_FOUND:
  486. microread_target_discovered(hdev, gate, skb);
  487. return 0;
  488. case MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF:
  489. if (skb->len < 1) {
  490. kfree_skb(skb);
  491. return -EPROTO;
  492. }
  493. if (skb->data[skb->len - 1]) {
  494. kfree_skb(skb);
  495. return -EIO;
  496. }
  497. skb_trim(skb, skb->len - 1);
  498. r = nfc_tm_data_received(hdev->ndev, skb);
  499. break;
  500. case MICROREAD_EVT_MCARD_FIELD_ON:
  501. case MICROREAD_EVT_MCARD_FIELD_OFF:
  502. kfree_skb(skb);
  503. return 0;
  504. case MICROREAD_EVT_P2P_TARGET_ACTIVATED:
  505. r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
  506. NFC_COMM_PASSIVE, skb->data,
  507. skb->len);
  508. kfree_skb(skb);
  509. break;
  510. case MICROREAD_EVT_MCARD_EXCHANGE:
  511. if (skb->len < 1) {
  512. kfree_skb(skb);
  513. return -EPROTO;
  514. }
  515. if (skb->data[skb->len-1]) {
  516. kfree_skb(skb);
  517. return -EIO;
  518. }
  519. skb_trim(skb, skb->len - 1);
  520. r = nfc_tm_data_received(hdev->ndev, skb);
  521. break;
  522. case MICROREAD_EVT_P2P_TARGET_DEACTIVATED:
  523. kfree_skb(skb);
  524. mode = 0xff;
  525. r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
  526. MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
  527. if (r)
  528. break;
  529. r = nfc_hci_send_event(hdev, gate,
  530. MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL,
  531. 0);
  532. break;
  533. default:
  534. return 1;
  535. }
  536. return r;
  537. }
  538. static struct nfc_hci_ops microread_hci_ops = {
  539. .open = microread_open,
  540. .close = microread_close,
  541. .hci_ready = microread_hci_ready,
  542. .xmit = microread_xmit,
  543. .start_poll = microread_start_poll,
  544. .dep_link_up = microread_dep_link_up,
  545. .dep_link_down = microread_dep_link_down,
  546. .target_from_gate = microread_target_from_gate,
  547. .complete_target_discovered = microread_complete_target_discovered,
  548. .im_transceive = microread_im_transceive,
  549. .tm_send = microread_tm_send,
  550. .check_presence = NULL,
  551. .event_received = microread_event_received,
  552. };
  553. int microread_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
  554. int phy_headroom, int phy_tailroom, int phy_payload,
  555. struct nfc_hci_dev **hdev)
  556. {
  557. struct microread_info *info;
  558. unsigned long quirks = 0;
  559. u32 protocols;
  560. struct nfc_hci_init_data init_data;
  561. int r;
  562. info = kzalloc(sizeof(struct microread_info), GFP_KERNEL);
  563. if (!info) {
  564. r = -ENOMEM;
  565. goto err_info_alloc;
  566. }
  567. info->phy_ops = phy_ops;
  568. info->phy_id = phy_id;
  569. init_data.gate_count = ARRAY_SIZE(microread_gates);
  570. memcpy(init_data.gates, microread_gates, sizeof(microread_gates));
  571. strcpy(init_data.session_id, "MICROREA");
  572. set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
  573. protocols = NFC_PROTO_JEWEL_MASK |
  574. NFC_PROTO_MIFARE_MASK |
  575. NFC_PROTO_FELICA_MASK |
  576. NFC_PROTO_ISO14443_MASK |
  577. NFC_PROTO_ISO14443_B_MASK |
  578. NFC_PROTO_NFC_DEP_MASK;
  579. info->hdev = nfc_hci_allocate_device(&microread_hci_ops, &init_data,
  580. quirks, protocols, llc_name,
  581. phy_headroom +
  582. MICROREAD_CMDS_HEADROOM,
  583. phy_tailroom +
  584. MICROREAD_CMD_TAILROOM,
  585. phy_payload);
  586. if (!info->hdev) {
  587. pr_err("Cannot allocate nfc hdev\n");
  588. r = -ENOMEM;
  589. goto err_alloc_hdev;
  590. }
  591. nfc_hci_set_clientdata(info->hdev, info);
  592. r = nfc_hci_register_device(info->hdev);
  593. if (r)
  594. goto err_regdev;
  595. *hdev = info->hdev;
  596. return 0;
  597. err_regdev:
  598. nfc_hci_free_device(info->hdev);
  599. err_alloc_hdev:
  600. kfree(info);
  601. err_info_alloc:
  602. return r;
  603. }
  604. EXPORT_SYMBOL(microread_probe);
  605. void microread_remove(struct nfc_hci_dev *hdev)
  606. {
  607. struct microread_info *info = nfc_hci_get_clientdata(hdev);
  608. nfc_hci_unregister_device(hdev);
  609. nfc_hci_free_device(hdev);
  610. kfree(info);
  611. }
  612. EXPORT_SYMBOL(microread_remove);
  613. MODULE_LICENSE("GPL");
  614. MODULE_DESCRIPTION(DRIVER_DESC);