Revert "USB: core: only clean up what we allocated"
[linux-stable.git] / drivers / nfc / pn533 / usb.c
blob5d823e965883b0f5f23db5ab39afc9f96a128267
1 /*
2 * Driver for NXP PN533 NFC Chip - USB transport layer
4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Copyright (C) 2012-2013 Tieto Poland
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.
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.
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/>.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb.h>
26 #include <linux/nfc.h>
27 #include <linux/netdevice.h>
28 #include <net/nfc/nfc.h>
29 #include "pn533.h"
31 #define VERSION "0.1"
33 #define PN533_VENDOR_ID 0x4CC
34 #define PN533_PRODUCT_ID 0x2533
36 #define SCM_VENDOR_ID 0x4E6
37 #define SCL3711_PRODUCT_ID 0x5591
39 #define SONY_VENDOR_ID 0x054c
40 #define PASORI_PRODUCT_ID 0x02e1
42 #define ACS_VENDOR_ID 0x072f
43 #define ACR122U_PRODUCT_ID 0x2200
45 static const struct usb_device_id pn533_usb_table[] = {
46 { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID),
47 .driver_info = PN533_DEVICE_STD },
48 { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID),
49 .driver_info = PN533_DEVICE_STD },
50 { USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID),
51 .driver_info = PN533_DEVICE_PASORI },
52 { USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID),
53 .driver_info = PN533_DEVICE_ACR122U },
54 { }
56 MODULE_DEVICE_TABLE(usb, pn533_usb_table);
58 struct pn533_usb_phy {
59 struct usb_device *udev;
60 struct usb_interface *interface;
62 struct urb *out_urb;
63 struct urb *in_urb;
65 struct urb *ack_urb;
66 u8 *ack_buffer;
68 struct pn533 *priv;
71 static void pn533_recv_response(struct urb *urb)
73 struct pn533_usb_phy *phy = urb->context;
74 struct sk_buff *skb = NULL;
76 if (!urb->status) {
77 skb = alloc_skb(urb->actual_length, GFP_ATOMIC);
78 if (!skb) {
79 nfc_err(&phy->udev->dev, "failed to alloc memory\n");
80 } else {
81 skb_put_data(skb, urb->transfer_buffer,
82 urb->actual_length);
86 pn533_recv_frame(phy->priv, skb, urb->status);
89 static int pn533_submit_urb_for_response(struct pn533_usb_phy *phy, gfp_t flags)
91 phy->in_urb->complete = pn533_recv_response;
93 return usb_submit_urb(phy->in_urb, flags);
96 static void pn533_recv_ack(struct urb *urb)
98 struct pn533_usb_phy *phy = urb->context;
99 struct pn533 *priv = phy->priv;
100 struct pn533_cmd *cmd = priv->cmd;
101 struct pn533_std_frame *in_frame;
102 int rc;
104 cmd->status = urb->status;
106 switch (urb->status) {
107 case 0:
108 break; /* success */
109 case -ECONNRESET:
110 case -ENOENT:
111 dev_dbg(&phy->udev->dev,
112 "The urb has been stopped (status %d)\n",
113 urb->status);
114 goto sched_wq;
115 case -ESHUTDOWN:
116 default:
117 nfc_err(&phy->udev->dev,
118 "Urb failure (status %d)\n", urb->status);
119 goto sched_wq;
122 in_frame = phy->in_urb->transfer_buffer;
124 if (!pn533_rx_frame_is_ack(in_frame)) {
125 nfc_err(&phy->udev->dev, "Received an invalid ack\n");
126 cmd->status = -EIO;
127 goto sched_wq;
130 rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
131 if (rc) {
132 nfc_err(&phy->udev->dev,
133 "usb_submit_urb failed with result %d\n", rc);
134 cmd->status = rc;
135 goto sched_wq;
138 return;
140 sched_wq:
141 queue_work(priv->wq, &priv->cmd_complete_work);
144 static int pn533_submit_urb_for_ack(struct pn533_usb_phy *phy, gfp_t flags)
146 phy->in_urb->complete = pn533_recv_ack;
148 return usb_submit_urb(phy->in_urb, flags);
151 static int pn533_usb_send_ack(struct pn533 *dev, gfp_t flags)
153 struct pn533_usb_phy *phy = dev->phy;
154 static const u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
155 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
157 if (!phy->ack_buffer) {
158 phy->ack_buffer = kmemdup(ack, sizeof(ack), flags);
159 if (!phy->ack_buffer)
160 return -ENOMEM;
163 phy->ack_urb->transfer_buffer = phy->ack_buffer;
164 phy->ack_urb->transfer_buffer_length = sizeof(ack);
165 return usb_submit_urb(phy->ack_urb, flags);
168 static int pn533_usb_send_frame(struct pn533 *dev,
169 struct sk_buff *out)
171 struct pn533_usb_phy *phy = dev->phy;
172 int rc;
174 if (phy->priv == NULL)
175 phy->priv = dev;
177 phy->out_urb->transfer_buffer = out->data;
178 phy->out_urb->transfer_buffer_length = out->len;
180 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
181 out->data, out->len, false);
183 rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
184 if (rc)
185 return rc;
187 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
188 /* request for response for sent packet directly */
189 rc = pn533_submit_urb_for_response(phy, GFP_KERNEL);
190 if (rc)
191 goto error;
192 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
193 /* request for ACK if that's the case */
194 rc = pn533_submit_urb_for_ack(phy, GFP_KERNEL);
195 if (rc)
196 goto error;
199 return 0;
201 error:
202 usb_unlink_urb(phy->out_urb);
203 return rc;
206 static void pn533_usb_abort_cmd(struct pn533 *dev, gfp_t flags)
208 struct pn533_usb_phy *phy = dev->phy;
210 /* ACR122U does not support any command which aborts last
211 * issued command i.e. as ACK for standard PN533. Additionally,
212 * it behaves stange, sending broken or incorrect responses,
213 * when we cancel urb before the chip will send response.
215 if (dev->device_type == PN533_DEVICE_ACR122U)
216 return;
218 /* An ack will cancel the last issued command */
219 pn533_usb_send_ack(dev, flags);
221 /* cancel the urb request */
222 usb_kill_urb(phy->in_urb);
225 /* ACR122 specific structs and fucntions */
227 /* ACS ACR122 pn533 frame definitions */
228 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
229 + 2)
230 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
231 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
232 + 2)
233 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
234 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
236 /* CCID messages types */
237 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
238 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
240 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
243 struct pn533_acr122_ccid_hdr {
244 u8 type;
245 u32 datalen;
246 u8 slot;
247 u8 seq;
250 * 3 msg specific bytes or status, error and 1 specific
251 * byte for reposnse msg
253 u8 params[3];
254 u8 data[]; /* payload */
255 } __packed;
257 struct pn533_acr122_apdu_hdr {
258 u8 class;
259 u8 ins;
260 u8 p1;
261 u8 p2;
262 } __packed;
264 struct pn533_acr122_tx_frame {
265 struct pn533_acr122_ccid_hdr ccid;
266 struct pn533_acr122_apdu_hdr apdu;
267 u8 datalen;
268 u8 data[]; /* pn533 frame: TFI ... */
269 } __packed;
271 struct pn533_acr122_rx_frame {
272 struct pn533_acr122_ccid_hdr ccid;
273 u8 data[]; /* pn533 frame : TFI ... */
274 } __packed;
276 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
278 struct pn533_acr122_tx_frame *frame = _frame;
280 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
281 /* sizeof(apdu_hdr) + sizeof(datalen) */
282 frame->ccid.datalen = sizeof(frame->apdu) + 1;
283 frame->ccid.slot = 0;
284 frame->ccid.seq = 0;
285 frame->ccid.params[0] = 0;
286 frame->ccid.params[1] = 0;
287 frame->ccid.params[2] = 0;
289 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
290 frame->data[1] = cmd_code;
291 frame->datalen = 2; /* data[0] + data[1] */
293 frame->apdu.class = 0xFF;
294 frame->apdu.ins = 0;
295 frame->apdu.p1 = 0;
296 frame->apdu.p2 = 0;
299 static void pn533_acr122_tx_frame_finish(void *_frame)
301 struct pn533_acr122_tx_frame *frame = _frame;
303 frame->ccid.datalen += frame->datalen;
306 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
308 struct pn533_acr122_tx_frame *frame = _frame;
310 frame->datalen += len;
313 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
315 struct pn533_acr122_rx_frame *frame = _frame;
317 if (frame->ccid.type != 0x83)
318 return false;
320 if (!frame->ccid.datalen)
321 return false;
323 if (frame->data[frame->ccid.datalen - 2] == 0x63)
324 return false;
326 return true;
329 static int pn533_acr122_rx_frame_size(void *frame)
331 struct pn533_acr122_rx_frame *f = frame;
333 /* f->ccid.datalen already includes tail length */
334 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
337 static u8 pn533_acr122_get_cmd_code(void *frame)
339 struct pn533_acr122_rx_frame *f = frame;
341 return PN533_FRAME_CMD(f);
344 static struct pn533_frame_ops pn533_acr122_frame_ops = {
345 .tx_frame_init = pn533_acr122_tx_frame_init,
346 .tx_frame_finish = pn533_acr122_tx_frame_finish,
347 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
348 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
349 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
351 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
352 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
353 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
354 .rx_frame_size = pn533_acr122_rx_frame_size,
356 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
357 .get_cmd_code = pn533_acr122_get_cmd_code,
360 struct pn533_acr122_poweron_rdr_arg {
361 int rc;
362 struct completion done;
365 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
367 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
369 dev_dbg(&urb->dev->dev, "%s\n", __func__);
371 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
372 urb->transfer_buffer, urb->transfer_buffer_length,
373 false);
375 arg->rc = urb->status;
376 complete(&arg->done);
379 static int pn533_acr122_poweron_rdr(struct pn533_usb_phy *phy)
381 /* Power on th reader (CCID cmd) */
382 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
383 0, 0, 0, 0, 0, 0, 3, 0, 0};
384 char *buffer;
385 int transferred;
386 int rc;
387 void *cntx;
388 struct pn533_acr122_poweron_rdr_arg arg;
390 dev_dbg(&phy->udev->dev, "%s\n", __func__);
392 buffer = kmemdup(cmd, sizeof(cmd), GFP_KERNEL);
393 if (!buffer)
394 return -ENOMEM;
396 init_completion(&arg.done);
397 cntx = phy->in_urb->context; /* backup context */
399 phy->in_urb->complete = pn533_acr122_poweron_rdr_resp;
400 phy->in_urb->context = &arg;
402 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
403 cmd, sizeof(cmd), false);
405 rc = usb_bulk_msg(phy->udev, phy->out_urb->pipe, buffer, sizeof(cmd),
406 &transferred, 0);
407 kfree(buffer);
408 if (rc || (transferred != sizeof(cmd))) {
409 nfc_err(&phy->udev->dev,
410 "Reader power on cmd error %d\n", rc);
411 return rc;
414 rc = usb_submit_urb(phy->in_urb, GFP_KERNEL);
415 if (rc) {
416 nfc_err(&phy->udev->dev,
417 "Can't submit reader poweron cmd response %d\n", rc);
418 return rc;
421 wait_for_completion(&arg.done);
422 phy->in_urb->context = cntx; /* restore context */
424 return arg.rc;
427 static void pn533_send_complete(struct urb *urb)
429 struct pn533_usb_phy *phy = urb->context;
431 switch (urb->status) {
432 case 0:
433 break; /* success */
434 case -ECONNRESET:
435 case -ENOENT:
436 dev_dbg(&phy->udev->dev,
437 "The urb has been stopped (status %d)\n",
438 urb->status);
439 break;
440 case -ESHUTDOWN:
441 default:
442 nfc_err(&phy->udev->dev,
443 "Urb failure (status %d)\n",
444 urb->status);
448 static struct pn533_phy_ops usb_phy_ops = {
449 .send_frame = pn533_usb_send_frame,
450 .send_ack = pn533_usb_send_ack,
451 .abort_cmd = pn533_usb_abort_cmd,
454 static int pn533_usb_probe(struct usb_interface *interface,
455 const struct usb_device_id *id)
457 struct pn533 *priv;
458 struct pn533_usb_phy *phy;
459 struct usb_host_interface *iface_desc;
460 struct usb_endpoint_descriptor *endpoint;
461 int in_endpoint = 0;
462 int out_endpoint = 0;
463 int rc = -ENOMEM;
464 int i;
465 u32 protocols;
466 enum pn533_protocol_type protocol_type = PN533_PROTO_REQ_ACK_RESP;
467 struct pn533_frame_ops *fops = NULL;
468 unsigned char *in_buf;
469 int in_buf_len = PN533_EXT_FRAME_HEADER_LEN +
470 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
471 PN533_STD_FRAME_TAIL_LEN;
473 phy = devm_kzalloc(&interface->dev, sizeof(*phy), GFP_KERNEL);
474 if (!phy)
475 return -ENOMEM;
477 in_buf = kzalloc(in_buf_len, GFP_KERNEL);
478 if (!in_buf)
479 return -ENOMEM;
481 phy->udev = usb_get_dev(interface_to_usbdev(interface));
482 phy->interface = interface;
484 iface_desc = interface->cur_altsetting;
485 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
486 endpoint = &iface_desc->endpoint[i].desc;
488 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
489 in_endpoint = endpoint->bEndpointAddress;
491 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
492 out_endpoint = endpoint->bEndpointAddress;
495 if (!in_endpoint || !out_endpoint) {
496 nfc_err(&interface->dev,
497 "Could not find bulk-in or bulk-out endpoint\n");
498 rc = -ENODEV;
499 goto error;
502 phy->in_urb = usb_alloc_urb(0, GFP_KERNEL);
503 phy->out_urb = usb_alloc_urb(0, GFP_KERNEL);
504 phy->ack_urb = usb_alloc_urb(0, GFP_KERNEL);
506 if (!phy->in_urb || !phy->out_urb || !phy->ack_urb)
507 goto error;
509 usb_fill_bulk_urb(phy->in_urb, phy->udev,
510 usb_rcvbulkpipe(phy->udev, in_endpoint),
511 in_buf, in_buf_len, NULL, phy);
513 usb_fill_bulk_urb(phy->out_urb, phy->udev,
514 usb_sndbulkpipe(phy->udev, out_endpoint),
515 NULL, 0, pn533_send_complete, phy);
516 usb_fill_bulk_urb(phy->ack_urb, phy->udev,
517 usb_sndbulkpipe(phy->udev, out_endpoint),
518 NULL, 0, pn533_send_complete, phy);
520 switch (id->driver_info) {
521 case PN533_DEVICE_STD:
522 protocols = PN533_ALL_PROTOCOLS;
523 break;
525 case PN533_DEVICE_PASORI:
526 protocols = PN533_NO_TYPE_B_PROTOCOLS;
527 break;
529 case PN533_DEVICE_ACR122U:
530 protocols = PN533_NO_TYPE_B_PROTOCOLS;
531 fops = &pn533_acr122_frame_ops;
532 protocol_type = PN533_PROTO_REQ_RESP,
534 rc = pn533_acr122_poweron_rdr(phy);
535 if (rc < 0) {
536 nfc_err(&interface->dev,
537 "Couldn't poweron the reader (error %d)\n", rc);
538 goto error;
540 break;
542 default:
543 nfc_err(&interface->dev, "Unknown device type %lu\n",
544 id->driver_info);
545 rc = -EINVAL;
546 goto error;
549 priv = pn533_register_device(id->driver_info, protocols, protocol_type,
550 phy, &usb_phy_ops, fops,
551 &phy->udev->dev, &interface->dev);
553 if (IS_ERR(priv)) {
554 rc = PTR_ERR(priv);
555 goto error;
558 phy->priv = priv;
560 rc = pn533_finalize_setup(priv);
561 if (rc)
562 goto error;
564 usb_set_intfdata(interface, phy);
566 return 0;
568 error:
569 usb_free_urb(phy->in_urb);
570 usb_free_urb(phy->out_urb);
571 usb_free_urb(phy->ack_urb);
572 usb_put_dev(phy->udev);
573 kfree(in_buf);
575 return rc;
578 static void pn533_usb_disconnect(struct usb_interface *interface)
580 struct pn533_usb_phy *phy = usb_get_intfdata(interface);
582 if (!phy)
583 return;
585 pn533_unregister_device(phy->priv);
587 usb_set_intfdata(interface, NULL);
589 usb_kill_urb(phy->in_urb);
590 usb_kill_urb(phy->out_urb);
591 usb_kill_urb(phy->ack_urb);
593 kfree(phy->in_urb->transfer_buffer);
594 usb_free_urb(phy->in_urb);
595 usb_free_urb(phy->out_urb);
596 usb_free_urb(phy->ack_urb);
597 kfree(phy->ack_buffer);
599 nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
602 static struct usb_driver pn533_usb_driver = {
603 .name = "pn533_usb",
604 .probe = pn533_usb_probe,
605 .disconnect = pn533_usb_disconnect,
606 .id_table = pn533_usb_table,
609 module_usb_driver(pn533_usb_driver);
611 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
612 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
613 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
614 MODULE_DESCRIPTION("PN533 USB driver ver " VERSION);
615 MODULE_VERSION(VERSION);
616 MODULE_LICENSE("GPL");