Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / nfc / pn533 / usb.c
blobe153e8b64bb8bdd527cd4681c7cb4605d06d3f8e
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 pn533 *priv;
68 static void pn533_recv_response(struct urb *urb)
70 struct pn533_usb_phy *phy = urb->context;
71 struct sk_buff *skb = NULL;
73 if (!urb->status) {
74 skb = alloc_skb(urb->actual_length, GFP_KERNEL);
75 if (!skb) {
76 nfc_err(&phy->udev->dev, "failed to alloc memory\n");
77 } else {
78 skb_put_data(skb, urb->transfer_buffer,
79 urb->actual_length);
83 pn533_recv_frame(phy->priv, skb, urb->status);
86 static int pn533_submit_urb_for_response(struct pn533_usb_phy *phy, gfp_t flags)
88 phy->in_urb->complete = pn533_recv_response;
90 return usb_submit_urb(phy->in_urb, flags);
93 static void pn533_recv_ack(struct urb *urb)
95 struct pn533_usb_phy *phy = urb->context;
96 struct pn533 *priv = phy->priv;
97 struct pn533_cmd *cmd = priv->cmd;
98 struct pn533_std_frame *in_frame;
99 int rc;
101 cmd->status = urb->status;
103 switch (urb->status) {
104 case 0:
105 break; /* success */
106 case -ECONNRESET:
107 case -ENOENT:
108 dev_dbg(&phy->udev->dev,
109 "The urb has been stopped (status %d)\n",
110 urb->status);
111 goto sched_wq;
112 case -ESHUTDOWN:
113 default:
114 nfc_err(&phy->udev->dev,
115 "Urb failure (status %d)\n", urb->status);
116 goto sched_wq;
119 in_frame = phy->in_urb->transfer_buffer;
121 if (!pn533_rx_frame_is_ack(in_frame)) {
122 nfc_err(&phy->udev->dev, "Received an invalid ack\n");
123 cmd->status = -EIO;
124 goto sched_wq;
127 rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
128 if (rc) {
129 nfc_err(&phy->udev->dev,
130 "usb_submit_urb failed with result %d\n", rc);
131 cmd->status = rc;
132 goto sched_wq;
135 return;
137 sched_wq:
138 queue_work(priv->wq, &priv->cmd_complete_work);
141 static int pn533_submit_urb_for_ack(struct pn533_usb_phy *phy, gfp_t flags)
143 phy->in_urb->complete = pn533_recv_ack;
145 return usb_submit_urb(phy->in_urb, flags);
148 static int pn533_usb_send_ack(struct pn533 *dev, gfp_t flags)
150 struct pn533_usb_phy *phy = dev->phy;
151 static const u8 ack[6] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
152 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
153 int rc;
155 phy->out_urb->transfer_buffer = (u8 *)ack;
156 phy->out_urb->transfer_buffer_length = sizeof(ack);
157 rc = usb_submit_urb(phy->out_urb, flags);
159 return rc;
162 static int pn533_usb_send_frame(struct pn533 *dev,
163 struct sk_buff *out)
165 struct pn533_usb_phy *phy = dev->phy;
166 int rc;
168 if (phy->priv == NULL)
169 phy->priv = dev;
171 phy->out_urb->transfer_buffer = out->data;
172 phy->out_urb->transfer_buffer_length = out->len;
174 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
175 out->data, out->len, false);
177 rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
178 if (rc)
179 return rc;
181 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
182 /* request for response for sent packet directly */
183 rc = pn533_submit_urb_for_response(phy, GFP_ATOMIC);
184 if (rc)
185 goto error;
186 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
187 /* request for ACK if that's the case */
188 rc = pn533_submit_urb_for_ack(phy, GFP_KERNEL);
189 if (rc)
190 goto error;
193 return 0;
195 error:
196 usb_unlink_urb(phy->out_urb);
197 return rc;
200 static void pn533_usb_abort_cmd(struct pn533 *dev, gfp_t flags)
202 struct pn533_usb_phy *phy = dev->phy;
204 /* ACR122U does not support any command which aborts last
205 * issued command i.e. as ACK for standard PN533. Additionally,
206 * it behaves stange, sending broken or incorrect responses,
207 * when we cancel urb before the chip will send response.
209 if (dev->device_type == PN533_DEVICE_ACR122U)
210 return;
212 /* An ack will cancel the last issued command */
213 pn533_usb_send_ack(dev, flags);
215 /* cancel the urb request */
216 usb_kill_urb(phy->in_urb);
219 /* ACR122 specific structs and fucntions */
221 /* ACS ACR122 pn533 frame definitions */
222 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
223 + 2)
224 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
225 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
226 + 2)
227 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
228 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
230 /* CCID messages types */
231 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
232 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
234 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
237 struct pn533_acr122_ccid_hdr {
238 u8 type;
239 u32 datalen;
240 u8 slot;
241 u8 seq;
244 * 3 msg specific bytes or status, error and 1 specific
245 * byte for reposnse msg
247 u8 params[3];
248 u8 data[]; /* payload */
249 } __packed;
251 struct pn533_acr122_apdu_hdr {
252 u8 class;
253 u8 ins;
254 u8 p1;
255 u8 p2;
256 } __packed;
258 struct pn533_acr122_tx_frame {
259 struct pn533_acr122_ccid_hdr ccid;
260 struct pn533_acr122_apdu_hdr apdu;
261 u8 datalen;
262 u8 data[]; /* pn533 frame: TFI ... */
263 } __packed;
265 struct pn533_acr122_rx_frame {
266 struct pn533_acr122_ccid_hdr ccid;
267 u8 data[]; /* pn533 frame : TFI ... */
268 } __packed;
270 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
272 struct pn533_acr122_tx_frame *frame = _frame;
274 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
275 /* sizeof(apdu_hdr) + sizeof(datalen) */
276 frame->ccid.datalen = sizeof(frame->apdu) + 1;
277 frame->ccid.slot = 0;
278 frame->ccid.seq = 0;
279 frame->ccid.params[0] = 0;
280 frame->ccid.params[1] = 0;
281 frame->ccid.params[2] = 0;
283 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
284 frame->data[1] = cmd_code;
285 frame->datalen = 2; /* data[0] + data[1] */
287 frame->apdu.class = 0xFF;
288 frame->apdu.ins = 0;
289 frame->apdu.p1 = 0;
290 frame->apdu.p2 = 0;
293 static void pn533_acr122_tx_frame_finish(void *_frame)
295 struct pn533_acr122_tx_frame *frame = _frame;
297 frame->ccid.datalen += frame->datalen;
300 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
302 struct pn533_acr122_tx_frame *frame = _frame;
304 frame->datalen += len;
307 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
309 struct pn533_acr122_rx_frame *frame = _frame;
311 if (frame->ccid.type != 0x83)
312 return false;
314 if (!frame->ccid.datalen)
315 return false;
317 if (frame->data[frame->ccid.datalen - 2] == 0x63)
318 return false;
320 return true;
323 static int pn533_acr122_rx_frame_size(void *frame)
325 struct pn533_acr122_rx_frame *f = frame;
327 /* f->ccid.datalen already includes tail length */
328 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
331 static u8 pn533_acr122_get_cmd_code(void *frame)
333 struct pn533_acr122_rx_frame *f = frame;
335 return PN533_FRAME_CMD(f);
338 static struct pn533_frame_ops pn533_acr122_frame_ops = {
339 .tx_frame_init = pn533_acr122_tx_frame_init,
340 .tx_frame_finish = pn533_acr122_tx_frame_finish,
341 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
342 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
343 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
345 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
346 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
347 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
348 .rx_frame_size = pn533_acr122_rx_frame_size,
350 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
351 .get_cmd_code = pn533_acr122_get_cmd_code,
354 struct pn533_acr122_poweron_rdr_arg {
355 int rc;
356 struct completion done;
359 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
361 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
363 dev_dbg(&urb->dev->dev, "%s\n", __func__);
365 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
366 urb->transfer_buffer, urb->transfer_buffer_length,
367 false);
369 arg->rc = urb->status;
370 complete(&arg->done);
373 static int pn533_acr122_poweron_rdr(struct pn533_usb_phy *phy)
375 /* Power on th reader (CCID cmd) */
376 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
377 0, 0, 0, 0, 0, 0, 3, 0, 0};
378 int rc;
379 void *cntx;
380 struct pn533_acr122_poweron_rdr_arg arg;
382 dev_dbg(&phy->udev->dev, "%s\n", __func__);
384 init_completion(&arg.done);
385 cntx = phy->in_urb->context; /* backup context */
387 phy->in_urb->complete = pn533_acr122_poweron_rdr_resp;
388 phy->in_urb->context = &arg;
390 phy->out_urb->transfer_buffer = cmd;
391 phy->out_urb->transfer_buffer_length = sizeof(cmd);
393 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
394 cmd, sizeof(cmd), false);
396 rc = usb_submit_urb(phy->out_urb, GFP_KERNEL);
397 if (rc) {
398 nfc_err(&phy->udev->dev,
399 "Reader power on cmd error %d\n", rc);
400 return rc;
403 rc = usb_submit_urb(phy->in_urb, GFP_KERNEL);
404 if (rc) {
405 nfc_err(&phy->udev->dev,
406 "Can't submit reader poweron cmd response %d\n", rc);
407 return rc;
410 wait_for_completion(&arg.done);
411 phy->in_urb->context = cntx; /* restore context */
413 return arg.rc;
416 static void pn533_send_complete(struct urb *urb)
418 struct pn533_usb_phy *phy = urb->context;
420 switch (urb->status) {
421 case 0:
422 break; /* success */
423 case -ECONNRESET:
424 case -ENOENT:
425 dev_dbg(&phy->udev->dev,
426 "The urb has been stopped (status %d)\n",
427 urb->status);
428 break;
429 case -ESHUTDOWN:
430 default:
431 nfc_err(&phy->udev->dev,
432 "Urb failure (status %d)\n",
433 urb->status);
437 static struct pn533_phy_ops usb_phy_ops = {
438 .send_frame = pn533_usb_send_frame,
439 .send_ack = pn533_usb_send_ack,
440 .abort_cmd = pn533_usb_abort_cmd,
443 static int pn533_usb_probe(struct usb_interface *interface,
444 const struct usb_device_id *id)
446 struct pn533 *priv;
447 struct pn533_usb_phy *phy;
448 struct usb_host_interface *iface_desc;
449 struct usb_endpoint_descriptor *endpoint;
450 int in_endpoint = 0;
451 int out_endpoint = 0;
452 int rc = -ENOMEM;
453 int i;
454 u32 protocols;
455 enum pn533_protocol_type protocol_type = PN533_PROTO_REQ_ACK_RESP;
456 struct pn533_frame_ops *fops = NULL;
457 unsigned char *in_buf;
458 int in_buf_len = PN533_EXT_FRAME_HEADER_LEN +
459 PN533_STD_FRAME_MAX_PAYLOAD_LEN +
460 PN533_STD_FRAME_TAIL_LEN;
462 phy = devm_kzalloc(&interface->dev, sizeof(*phy), GFP_KERNEL);
463 if (!phy)
464 return -ENOMEM;
466 in_buf = kzalloc(in_buf_len, GFP_KERNEL);
467 if (!in_buf)
468 return -ENOMEM;
470 phy->udev = usb_get_dev(interface_to_usbdev(interface));
471 phy->interface = interface;
473 iface_desc = interface->cur_altsetting;
474 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
475 endpoint = &iface_desc->endpoint[i].desc;
477 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
478 in_endpoint = endpoint->bEndpointAddress;
480 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
481 out_endpoint = endpoint->bEndpointAddress;
484 if (!in_endpoint || !out_endpoint) {
485 nfc_err(&interface->dev,
486 "Could not find bulk-in or bulk-out endpoint\n");
487 rc = -ENODEV;
488 goto error;
491 phy->in_urb = usb_alloc_urb(0, GFP_KERNEL);
492 phy->out_urb = usb_alloc_urb(0, GFP_KERNEL);
494 if (!phy->in_urb || !phy->out_urb)
495 goto error;
497 usb_fill_bulk_urb(phy->in_urb, phy->udev,
498 usb_rcvbulkpipe(phy->udev, in_endpoint),
499 in_buf, in_buf_len, NULL, phy);
501 usb_fill_bulk_urb(phy->out_urb, phy->udev,
502 usb_sndbulkpipe(phy->udev, out_endpoint),
503 NULL, 0, pn533_send_complete, phy);
506 switch (id->driver_info) {
507 case PN533_DEVICE_STD:
508 protocols = PN533_ALL_PROTOCOLS;
509 break;
511 case PN533_DEVICE_PASORI:
512 protocols = PN533_NO_TYPE_B_PROTOCOLS;
513 break;
515 case PN533_DEVICE_ACR122U:
516 protocols = PN533_NO_TYPE_B_PROTOCOLS;
517 fops = &pn533_acr122_frame_ops;
518 protocol_type = PN533_PROTO_REQ_RESP,
520 rc = pn533_acr122_poweron_rdr(phy);
521 if (rc < 0) {
522 nfc_err(&interface->dev,
523 "Couldn't poweron the reader (error %d)\n", rc);
524 goto error;
526 break;
528 default:
529 nfc_err(&interface->dev, "Unknown device type %lu\n",
530 id->driver_info);
531 rc = -EINVAL;
532 goto error;
535 priv = pn533_register_device(id->driver_info, protocols, protocol_type,
536 phy, &usb_phy_ops, fops,
537 &phy->udev->dev, &interface->dev);
539 if (IS_ERR(priv)) {
540 rc = PTR_ERR(priv);
541 goto error;
544 phy->priv = priv;
546 rc = pn533_finalize_setup(priv);
547 if (rc)
548 goto error;
550 usb_set_intfdata(interface, phy);
552 return 0;
554 error:
555 usb_free_urb(phy->in_urb);
556 usb_free_urb(phy->out_urb);
557 usb_put_dev(phy->udev);
558 kfree(in_buf);
560 return rc;
563 static void pn533_usb_disconnect(struct usb_interface *interface)
565 struct pn533_usb_phy *phy = usb_get_intfdata(interface);
567 if (!phy)
568 return;
570 pn533_unregister_device(phy->priv);
572 usb_set_intfdata(interface, NULL);
574 usb_kill_urb(phy->in_urb);
575 usb_kill_urb(phy->out_urb);
577 kfree(phy->in_urb->transfer_buffer);
578 usb_free_urb(phy->in_urb);
579 usb_free_urb(phy->out_urb);
581 nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
584 static struct usb_driver pn533_usb_driver = {
585 .name = "pn533_usb",
586 .probe = pn533_usb_probe,
587 .disconnect = pn533_usb_disconnect,
588 .id_table = pn533_usb_table,
591 module_usb_driver(pn533_usb_driver);
593 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
594 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
595 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
596 MODULE_DESCRIPTION("PN533 USB driver ver " VERSION);
597 MODULE_VERSION(VERSION);
598 MODULE_LICENSE("GPL");