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>
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
},
56 MODULE_DEVICE_TABLE(usb
, pn533_usb_table
);
58 struct pn533_usb_phy
{
59 struct usb_device
*udev
;
60 struct usb_interface
*interface
;
71 static void pn533_recv_response(struct urb
*urb
)
73 struct pn533_usb_phy
*phy
= urb
->context
;
74 struct sk_buff
*skb
= NULL
;
77 skb
= alloc_skb(urb
->actual_length
, GFP_ATOMIC
);
79 nfc_err(&phy
->udev
->dev
, "failed to alloc memory\n");
81 skb_put_data(skb
, urb
->transfer_buffer
,
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
;
104 cmd
->status
= urb
->status
;
106 switch (urb
->status
) {
111 dev_dbg(&phy
->udev
->dev
,
112 "The urb has been stopped (status %d)\n",
117 nfc_err(&phy
->udev
->dev
,
118 "Urb failure (status %d)\n", urb
->status
);
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");
130 rc
= pn533_submit_urb_for_response(phy
, GFP_ATOMIC
);
132 nfc_err(&phy
->udev
->dev
,
133 "usb_submit_urb failed with result %d\n", rc
);
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
)
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
,
171 struct pn533_usb_phy
*phy
= dev
->phy
;
174 if (phy
->priv
== NULL
)
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
);
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
);
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
);
202 usb_unlink_urb(phy
->out_urb
);
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
)
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) \
230 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
231 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
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
{
250 * 3 msg specific bytes or status, error and 1 specific
251 * byte for reposnse msg
254 u8 data
[]; /* payload */
257 struct pn533_acr122_apdu_hdr
{
264 struct pn533_acr122_tx_frame
{
265 struct pn533_acr122_ccid_hdr ccid
;
266 struct pn533_acr122_apdu_hdr apdu
;
268 u8 data
[]; /* pn533 frame: TFI ... */
271 struct pn533_acr122_rx_frame
{
272 struct pn533_acr122_ccid_hdr ccid
;
273 u8 data
[]; /* pn533 frame : TFI ... */
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;
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;
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)
320 if (!frame
->ccid
.datalen
)
323 if (frame
->data
[frame
->ccid
.datalen
- 2] == 0x63)
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
{
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
,
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};
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
);
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
),
408 if (rc
|| (transferred
!= sizeof(cmd
))) {
409 nfc_err(&phy
->udev
->dev
,
410 "Reader power on cmd error %d\n", rc
);
414 rc
= usb_submit_urb(phy
->in_urb
, GFP_KERNEL
);
416 nfc_err(&phy
->udev
->dev
,
417 "Can't submit reader poweron cmd response %d\n", rc
);
421 wait_for_completion(&arg
.done
);
422 phy
->in_urb
->context
= cntx
; /* restore context */
427 static void pn533_send_complete(struct urb
*urb
)
429 struct pn533_usb_phy
*phy
= urb
->context
;
431 switch (urb
->status
) {
436 dev_dbg(&phy
->udev
->dev
,
437 "The urb has been stopped (status %d)\n",
442 nfc_err(&phy
->udev
->dev
,
443 "Urb failure (status %d)\n",
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
)
458 struct pn533_usb_phy
*phy
;
459 struct usb_host_interface
*iface_desc
;
460 struct usb_endpoint_descriptor
*endpoint
;
462 int out_endpoint
= 0;
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
);
477 in_buf
= kzalloc(in_buf_len
, GFP_KERNEL
);
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");
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
)
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
;
525 case PN533_DEVICE_PASORI
:
526 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
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
);
536 nfc_err(&interface
->dev
,
537 "Couldn't poweron the reader (error %d)\n", rc
);
543 nfc_err(&interface
->dev
, "Unknown device type %lu\n",
549 priv
= pn533_register_device(id
->driver_info
, protocols
, protocol_type
,
550 phy
, &usb_phy_ops
, fops
,
551 &phy
->udev
->dev
, &interface
->dev
);
560 rc
= pn533_finalize_setup(priv
);
564 usb_set_intfdata(interface
, phy
);
569 pn533_unregister_device(phy
->priv
);
571 usb_kill_urb(phy
->in_urb
);
572 usb_kill_urb(phy
->out_urb
);
573 usb_kill_urb(phy
->ack_urb
);
575 usb_free_urb(phy
->in_urb
);
576 usb_free_urb(phy
->out_urb
);
577 usb_free_urb(phy
->ack_urb
);
578 usb_put_dev(phy
->udev
);
580 kfree(phy
->ack_buffer
);
585 static void pn533_usb_disconnect(struct usb_interface
*interface
)
587 struct pn533_usb_phy
*phy
= usb_get_intfdata(interface
);
592 pn533_unregister_device(phy
->priv
);
594 usb_set_intfdata(interface
, NULL
);
596 usb_kill_urb(phy
->in_urb
);
597 usb_kill_urb(phy
->out_urb
);
598 usb_kill_urb(phy
->ack_urb
);
600 kfree(phy
->in_urb
->transfer_buffer
);
601 usb_free_urb(phy
->in_urb
);
602 usb_free_urb(phy
->out_urb
);
603 usb_free_urb(phy
->ack_urb
);
604 kfree(phy
->ack_buffer
);
606 nfc_info(&interface
->dev
, "NXP PN533 NFC device disconnected\n");
609 static struct usb_driver pn533_usb_driver
= {
611 .probe
= pn533_usb_probe
,
612 .disconnect
= pn533_usb_disconnect
,
613 .id_table
= pn533_usb_table
,
616 module_usb_driver(pn533_usb_driver
);
618 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
619 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
620 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
621 MODULE_DESCRIPTION("PN533 USB driver ver " VERSION
);
622 MODULE_VERSION(VERSION
);
623 MODULE_LICENSE("GPL");