usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Driver for NXP PN533 NFC Chip - USB transport layer
  3. *
  4. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  5. * Copyright (C) 2012-2013 Tieto Poland
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/usb.h>
  25. #include <linux/nfc.h>
  26. #include <linux/netdevice.h>
  27. #include <net/nfc/nfc.h>
  28. #include "pn533.h"
  29. #define VERSION "0.1"
  30. #define PN533_VENDOR_ID 0x4CC
  31. #define PN533_PRODUCT_ID 0x2533
  32. #define SCM_VENDOR_ID 0x4E6
  33. #define SCL3711_PRODUCT_ID 0x5591
  34. #define SONY_VENDOR_ID 0x054c
  35. #define PASORI_PRODUCT_ID 0x02e1
  36. #define ACS_VENDOR_ID 0x072f
  37. #define ACR122U_PRODUCT_ID 0x2200
  38. static const struct usb_device_id pn533_usb_table[] = {
  39. { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID),
  40. .driver_info = PN533_DEVICE_STD },
  41. { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID),
  42. .driver_info = PN533_DEVICE_STD },
  43. { USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID),
  44. .driver_info = PN533_DEVICE_PASORI },
  45. { USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID),
  46. .driver_info = PN533_DEVICE_ACR122U },
  47. { }
  48. };
  49. MODULE_DEVICE_TABLE(usb, pn533_usb_table);
  50. struct pn533_usb_phy {
  51. struct usb_device *udev;
  52. struct usb_interface *interface;
  53. struct urb *out_urb;
  54. struct urb *in_urb;
  55. struct pn533 *priv;
  56. };
  57. static void pn533_recv_response(struct urb *urb)
  58. {
  59. struct pn533_usb_phy *phy = urb->context;
  60. struct sk_buff *skb = NULL;
  61. if (!urb->status) {
  62. skb = alloc_skb(urb->actual_length, GFP_KERNEL);
  63. if (!skb) {
  64. nfc_err(&phy->udev->dev, "failed to alloc memory\n");
  65. } else {
  66. memcpy(skb_put(skb, urb->actual_length),
  67. urb->transfer_buffer, urb->actual_length);
  68. }
  69. }
  70. pn533_recv_frame(phy->priv, skb, urb->status);
  71. }
  72. static int pn533_submit_urb_for_response(struct pn533_usb_phy *phy, gfp_t flags)
  73. {
  74. phy->in_urb->complete = pn533_recv_response;
  75. return usb_submit_urb(phy->in_urb, flags);
  76. }
  77. static void pn533_recv_ack(struct urb *urb)
  78. {
  79. struct pn533_usb_phy *phy = urb->context;
  80. struct pn533 *priv = phy->priv;
  81. struct pn533_cmd *cmd = priv->cmd;
  82. struct pn533_std_frame *in_frame;
  83. int rc;
  84. cmd->status = urb->status;
  85. switch (urb->status) {
  86. case 0:
  87. break; /* success */
  88. case -ECONNRESET:
  89. case -ENOENT:
  90. dev_dbg(&phy->udev->dev,
  91. "The urb has been stopped (status %d)\n",
  92. urb->status);
  93. goto sched_wq;
  94. case -ESHUTDOWN:
  95. default:
  96. nfc_err(&phy->udev->dev,
  97. "Urb failure (status %d)\n", urb->status);
  98. goto sched_wq;
  99. }
  100. in_frame = phy->in_urb->transfer_buffer;
  101. if (!pn533_rx_frame_is_ack(in_frame)) {
  102. nfc_err(&phy->udev->dev, "Received an invalid ack\n");
  103. cmd->status = -EIO;
  104. goto sched_wq;
  105. }
  106. rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
  107. if (rc) {
  108. nfc_err(&phy->udev->dev,
  109. "usb_submit_urb failed with result %d\n", rc);
  110. cmd->status = rc;
  111. goto sched_wq;
  112. }
  113. return;
  114. sched_wq:
  115. queue_work(priv->wq, &priv->cmd_complete_work);
  116. }
  117. static int pn533_submit_urb_for_ack(struct pn533_usb_phy *phy, gfp_t flags)
  118. {
  119. phy->in_urb->complete = pn533_recv_ack;
  120. return usb_submit_urb(phy->in_urb, flags);
  121. }
  122. static int pn533_usb_send_ack(struct pn533 *dev, gfp_t flags)
  123. {
  124. struct pn533_usb_phy *phy = dev->phy;
  125. u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
  126. /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
  127. int rc;
  128. phy->out_urb->transfer_buffer = ack;
  129. phy->out_urb->transfer_buffer_length = sizeof(ack);
  130. rc = usb_submit_urb(phy->out_urb, flags);
  131. return rc;
  132. }
  133. static int pn533_usb_send_frame(struct pn533 *dev,
  134. struct sk_buff *out)
  135. {
  136. struct pn533_usb_phy *phy = dev->phy;
  137. int rc;
  138. if (phy->priv == NULL)
  139. phy->priv = dev;
  140. phy->out_urb->transfer_buffer = out->data;
  141. phy->out_urb->transfer_buffer_length = out->len;
  142. print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
  143. out->data, out->len, false);
  144. rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
  145. if (rc)
  146. return rc;
  147. if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
  148. /* request for response for sent packet directly */
  149. rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
  150. if (rc)
  151. goto error;
  152. } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
  153. /* request for ACK if that's the case */
  154. rc = pn533_submit_urb_for_ack(phy, GFP_KERNEL);
  155. if (rc)
  156. goto error;
  157. }
  158. return 0;
  159. error:
  160. usb_unlink_urb(phy->out_urb);
  161. return rc;
  162. }
  163. static void pn533_usb_abort_cmd(struct pn533 *dev, gfp_t flags)
  164. {
  165. struct pn533_usb_phy *phy = dev->phy;
  166. /* ACR122U does not support any command which aborts last
  167. * issued command i.e. as ACK for standard PN533. Additionally,
  168. * it behaves stange, sending broken or incorrect responses,
  169. * when we cancel urb before the chip will send response.
  170. */
  171. if (dev->device_type == PN533_DEVICE_ACR122U)
  172. return;
  173. /* An ack will cancel the last issued command */
  174. pn533_usb_send_ack(dev, flags);
  175. /* cancel the urb request */
  176. usb_kill_urb(phy->in_urb);
  177. }
  178. /* ACR122 specific structs and fucntions */
  179. /* ACS ACR122 pn533 frame definitions */
  180. #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
  181. + 2)
  182. #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
  183. #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
  184. + 2)
  185. #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
  186. #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
  187. /* CCID messages types */
  188. #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
  189. #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
  190. #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
  191. struct pn533_acr122_ccid_hdr {
  192. u8 type;
  193. u32 datalen;
  194. u8 slot;
  195. u8 seq;
  196. /*
  197. * 3 msg specific bytes or status, error and 1 specific
  198. * byte for reposnse msg
  199. */
  200. u8 params[3];
  201. u8 data[]; /* payload */
  202. } __packed;
  203. struct pn533_acr122_apdu_hdr {
  204. u8 class;
  205. u8 ins;
  206. u8 p1;
  207. u8 p2;
  208. } __packed;
  209. struct pn533_acr122_tx_frame {
  210. struct pn533_acr122_ccid_hdr ccid;
  211. struct pn533_acr122_apdu_hdr apdu;
  212. u8 datalen;
  213. u8 data[]; /* pn533 frame: TFI ... */
  214. } __packed;
  215. struct pn533_acr122_rx_frame {
  216. struct pn533_acr122_ccid_hdr ccid;
  217. u8 data[]; /* pn533 frame : TFI ... */
  218. } __packed;
  219. static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
  220. {
  221. struct pn533_acr122_tx_frame *frame = _frame;
  222. frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
  223. /* sizeof(apdu_hdr) + sizeof(datalen) */
  224. frame->ccid.datalen = sizeof(frame->apdu) + 1;
  225. frame->ccid.slot = 0;
  226. frame->ccid.seq = 0;
  227. frame->ccid.params[0] = 0;
  228. frame->ccid.params[1] = 0;
  229. frame->ccid.params[2] = 0;
  230. frame->data[0] = PN533_STD_FRAME_DIR_OUT;
  231. frame->data[1] = cmd_code;
  232. frame->datalen = 2; /* data[0] + data[1] */
  233. frame->apdu.class = 0xFF;
  234. frame->apdu.ins = 0;
  235. frame->apdu.p1 = 0;
  236. frame->apdu.p2 = 0;
  237. }
  238. static void pn533_acr122_tx_frame_finish(void *_frame)
  239. {
  240. struct pn533_acr122_tx_frame *frame = _frame;
  241. frame->ccid.datalen += frame->datalen;
  242. }
  243. static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
  244. {
  245. struct pn533_acr122_tx_frame *frame = _frame;
  246. frame->datalen += len;
  247. }
  248. static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
  249. {
  250. struct pn533_acr122_rx_frame *frame = _frame;
  251. if (frame->ccid.type != 0x83)
  252. return false;
  253. if (!frame->ccid.datalen)
  254. return false;
  255. if (frame->data[frame->ccid.datalen - 2] == 0x63)
  256. return false;
  257. return true;
  258. }
  259. static int pn533_acr122_rx_frame_size(void *frame)
  260. {
  261. struct pn533_acr122_rx_frame *f = frame;
  262. /* f->ccid.datalen already includes tail length */
  263. return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
  264. }
  265. static u8 pn533_acr122_get_cmd_code(void *frame)
  266. {
  267. struct pn533_acr122_rx_frame *f = frame;
  268. return PN533_FRAME_CMD(f);
  269. }
  270. static struct pn533_frame_ops pn533_acr122_frame_ops = {
  271. .tx_frame_init = pn533_acr122_tx_frame_init,
  272. .tx_frame_finish = pn533_acr122_tx_frame_finish,
  273. .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
  274. .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
  275. .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
  276. .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
  277. .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
  278. .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
  279. .rx_frame_size = pn533_acr122_rx_frame_size,
  280. .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
  281. .get_cmd_code = pn533_acr122_get_cmd_code,
  282. };
  283. struct pn533_acr122_poweron_rdr_arg {
  284. int rc;
  285. struct completion done;
  286. };
  287. static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
  288. {
  289. struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
  290. dev_dbg(&urb->dev->dev, "%s\n", __func__);
  291. print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
  292. urb->transfer_buffer, urb->transfer_buffer_length,
  293. false);
  294. arg->rc = urb->status;
  295. complete(&arg->done);
  296. }
  297. static int pn533_acr122_poweron_rdr(struct pn533_usb_phy *phy)
  298. {
  299. /* Power on th reader (CCID cmd) */
  300. u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
  301. 0, 0, 0, 0, 0, 0, 3, 0, 0};
  302. int rc;
  303. void *cntx;
  304. struct pn533_acr122_poweron_rdr_arg arg;
  305. dev_dbg(&phy->udev->dev, "%s\n", __func__);
  306. init_completion(&arg.done);
  307. cntx = phy->in_urb->context; /* backup context */
  308. phy->in_urb->complete = pn533_acr122_poweron_rdr_resp;
  309. phy->in_urb->context = &arg;
  310. phy->out_urb->transfer_buffer = cmd;
  311. phy->out_urb->transfer_buffer_length = sizeof(cmd);
  312. print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
  313. cmd, sizeof(cmd), false);
  314. rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
  315. if (rc) {
  316. nfc_err(&phy->udev->dev,
  317. "Reader power on cmd error %d\n", rc);
  318. return rc;
  319. }
  320. rc = usb_submit_urb(phy->in_urb, GFP_KERNEL);
  321. if (rc) {
  322. nfc_err(&phy->udev->dev,
  323. "Can't submit reader poweron cmd response %d\n", rc);
  324. return rc;
  325. }
  326. wait_for_completion(&arg.done);
  327. phy->in_urb->context = cntx; /* restore context */
  328. return arg.rc;
  329. }
  330. static void pn533_send_complete(struct urb *urb)
  331. {
  332. struct pn533_usb_phy *phy = urb->context;
  333. switch (urb->status) {
  334. case 0:
  335. break; /* success */
  336. case -ECONNRESET:
  337. case -ENOENT:
  338. dev_dbg(&phy->udev->dev,
  339. "The urb has been stopped (status %d)\n",
  340. urb->status);
  341. break;
  342. case -ESHUTDOWN:
  343. default:
  344. nfc_err(&phy->udev->dev,
  345. "Urb failure (status %d)\n",
  346. urb->status);
  347. }
  348. }
  349. static struct pn533_phy_ops usb_phy_ops = {
  350. .send_frame = pn533_usb_send_frame,
  351. .send_ack = pn533_usb_send_ack,
  352. .abort_cmd = pn533_usb_abort_cmd,
  353. };
  354. static int pn533_usb_probe(struct usb_interface *interface,
  355. const struct usb_device_id *id)
  356. {
  357. struct pn533 *priv;
  358. struct pn533_usb_phy *phy;
  359. struct usb_host_interface *iface_desc;
  360. struct usb_endpoint_descriptor *endpoint;
  361. int in_endpoint = 0;
  362. int out_endpoint = 0;
  363. int rc = -ENOMEM;
  364. int i;
  365. u32 protocols;
  366. enum pn533_protocol_type protocol_type = PN533_PROTO_REQ_ACK_RESP;
  367. struct pn533_frame_ops *fops = NULL;
  368. unsigned char *in_buf;
  369. int in_buf_len = PN533_EXT_FRAME_HEADER_LEN +
  370. PN533_STD_FRAME_MAX_PAYLOAD_LEN +
  371. PN533_STD_FRAME_TAIL_LEN;
  372. phy = devm_kzalloc(&interface->dev, sizeof(*phy), GFP_KERNEL);
  373. if (!phy)
  374. return -ENOMEM;
  375. in_buf = kzalloc(in_buf_len, GFP_KERNEL);
  376. if (!in_buf)
  377. return -ENOMEM;
  378. phy->udev = usb_get_dev(interface_to_usbdev(interface));
  379. phy->interface = interface;
  380. iface_desc = interface->cur_altsetting;
  381. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  382. endpoint = &iface_desc->endpoint[i].desc;
  383. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
  384. in_endpoint = endpoint->bEndpointAddress;
  385. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
  386. out_endpoint = endpoint->bEndpointAddress;
  387. }
  388. if (!in_endpoint || !out_endpoint) {
  389. nfc_err(&interface->dev,
  390. "Could not find bulk-in or bulk-out endpoint\n");
  391. rc = -ENODEV;
  392. goto error;
  393. }
  394. phy->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  395. phy->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  396. if (!phy->in_urb || !phy->out_urb)
  397. goto error;
  398. usb_fill_bulk_urb(phy->in_urb, phy->udev,
  399. usb_rcvbulkpipe(phy->udev, in_endpoint),
  400. in_buf, in_buf_len, NULL, phy);
  401. usb_fill_bulk_urb(phy->out_urb, phy->udev,
  402. usb_sndbulkpipe(phy->udev, out_endpoint),
  403. NULL, 0, pn533_send_complete, phy);
  404. switch (id->driver_info) {
  405. case PN533_DEVICE_STD:
  406. protocols = PN533_ALL_PROTOCOLS;
  407. break;
  408. case PN533_DEVICE_PASORI:
  409. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  410. break;
  411. case PN533_DEVICE_ACR122U:
  412. protocols = PN533_NO_TYPE_B_PROTOCOLS;
  413. fops = &pn533_acr122_frame_ops;
  414. protocol_type = PN533_PROTO_REQ_RESP,
  415. rc = pn533_acr122_poweron_rdr(phy);
  416. if (rc < 0) {
  417. nfc_err(&interface->dev,
  418. "Couldn't poweron the reader (error %d)\n", rc);
  419. goto error;
  420. }
  421. break;
  422. default:
  423. nfc_err(&interface->dev, "Unknown device type %lu\n",
  424. id->driver_info);
  425. rc = -EINVAL;
  426. goto error;
  427. }
  428. priv = pn533_register_device(id->driver_info, protocols, protocol_type,
  429. phy, &usb_phy_ops, fops,
  430. &phy->udev->dev, &interface->dev);
  431. if (IS_ERR(priv)) {
  432. rc = PTR_ERR(priv);
  433. goto error;
  434. }
  435. phy->priv = priv;
  436. usb_set_intfdata(interface, phy);
  437. return 0;
  438. error:
  439. usb_free_urb(phy->in_urb);
  440. usb_free_urb(phy->out_urb);
  441. usb_put_dev(phy->udev);
  442. kfree(in_buf);
  443. return rc;
  444. }
  445. static void pn533_usb_disconnect(struct usb_interface *interface)
  446. {
  447. struct pn533_usb_phy *phy = usb_get_intfdata(interface);
  448. if (!phy)
  449. return;
  450. pn533_unregister_device(phy->priv);
  451. usb_set_intfdata(interface, NULL);
  452. usb_kill_urb(phy->in_urb);
  453. usb_kill_urb(phy->out_urb);
  454. kfree(phy->in_urb->transfer_buffer);
  455. usb_free_urb(phy->in_urb);
  456. usb_free_urb(phy->out_urb);
  457. nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
  458. }
  459. static struct usb_driver pn533_usb_driver = {
  460. .name = "pn533_usb",
  461. .probe = pn533_usb_probe,
  462. .disconnect = pn533_usb_disconnect,
  463. .id_table = pn533_usb_table,
  464. };
  465. module_usb_driver(pn533_usb_driver);
  466. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  467. MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  468. MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
  469. MODULE_DESCRIPTION("PN533 USB driver ver " VERSION);
  470. MODULE_VERSION(VERSION);
  471. MODULE_LICENSE("GPL");