2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/nfc.h>
30 #include <linux/netdevice.h>
31 #include <net/nfc/nfc.h>
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
41 #define SONY_VENDOR_ID 0x054c
42 #define PASORI_PRODUCT_ID 0x02e1
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
47 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
52 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
55 NFC_PROTO_ISO14443_MASK | \
56 NFC_PROTO_NFC_DEP_MASK)
58 static const struct usb_device_id pn533_table[] = {
59 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
76 MODULE_DEVICE_TABLE(usb, pn533_table);
78 /* How much time we spend listening for initiators */
79 #define PN533_LISTEN_TIME 2
81 /* frame definitions */
82 #define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
85 #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87 #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
93 #define PN533_FRAME_MAX_PAYLOAD_LEN 263
95 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
97 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
102 #define PN533_SOF 0x00FF
104 /* frame identifier: in/out/error */
105 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106 #define PN533_DIR_OUT 0xD4
107 #define PN533_DIR_IN 0xD5
110 #define PN533_FRAME_CMD(f) (f->data[1])
111 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
114 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115 #define PN533_CMD_RF_CONFIGURATION 0x32
116 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
117 #define PN533_CMD_IN_COMM_THRU 0x42
118 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119 #define PN533_CMD_IN_ATR 0x50
120 #define PN533_CMD_IN_RELEASE 0x52
121 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
123 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
124 #define PN533_CMD_TG_GET_DATA 0x86
125 #define PN533_CMD_TG_SET_DATA 0x8e
126 #define PN533_CMD_UNDEF 0xff
128 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
130 /* PN533 Return codes */
131 #define PN533_CMD_RET_MASK 0x3F
132 #define PN533_CMD_MI_MASK 0x40
133 #define PN533_CMD_RET_SUCCESS 0x00
137 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
140 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
143 /* structs for pn533 commands */
145 /* PN533_CMD_GET_FIRMWARE_VERSION */
146 struct pn533_fw_version {
153 /* PN533_CMD_RF_CONFIGURATION */
154 #define PN533_CFGITEM_TIMING 0x02
155 #define PN533_CFGITEM_MAX_RETRIES 0x05
156 #define PN533_CFGITEM_PASORI 0x82
158 #define PN533_CONFIG_TIMING_102 0xb
159 #define PN533_CONFIG_TIMING_204 0xc
160 #define PN533_CONFIG_TIMING_409 0xd
161 #define PN533_CONFIG_TIMING_819 0xe
163 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
166 struct pn533_config_max_retries {
169 u8 mx_rty_passive_act;
172 struct pn533_config_timing {
178 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
180 /* felica commands opcode */
181 #define PN533_FELICA_OPC_SENSF_REQ 0
182 #define PN533_FELICA_OPC_SENSF_RES 1
183 /* felica SENSF_REQ parameters */
184 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
189 /* type B initiator_data values */
190 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
194 union pn533_cmd_poll_initdata {
207 /* Poll modulations */
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
216 __PN533_POLL_MOD_AFTER_LAST,
218 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
220 struct pn533_poll_modulations {
224 union pn533_cmd_poll_initdata initiator_data;
229 const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
270 [PN533_POLL_MOD_847KBPS_B] = {
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
282 [PN533_LISTEN_MOD] = {
287 /* PN533_CMD_IN_ATR */
289 struct pn533_cmd_activate_response {
301 struct pn533_cmd_jump_dep_response {
315 /* PN533_TG_INIT_AS_TARGET */
316 #define PN533_INIT_TARGET_PASSIVE 0x1
317 #define PN533_INIT_TARGET_DEP 0x2
319 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321 #define PN533_INIT_TARGET_RESP_DEP 0x4
323 struct pn533_cmd_init_target {
332 struct pn533_cmd_init_target_response {
338 struct usb_device *udev;
339 struct usb_interface *interface;
340 struct nfc_dev *nfc_dev;
343 struct pn533_frame *out_frame;
346 struct pn533_frame *in_frame;
348 struct sk_buff_head resp_q;
350 struct workqueue_struct *wq;
351 struct work_struct cmd_work;
352 struct work_struct cmd_complete_work;
353 struct work_struct poll_work;
354 struct work_struct mi_work;
355 struct work_struct tg_work;
356 struct timer_list listen_timer;
357 struct pn533_frame *wq_in_frame;
361 pn533_cmd_complete_t cmd_complete;
362 void *cmd_complete_arg;
363 struct mutex cmd_lock;
366 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
370 u32 listen_protocols;
375 u8 tgt_available_prots;
381 struct list_head cmd_queue;
386 struct list_head queue;
387 struct pn533_frame *out_frame;
388 struct pn533_frame *in_frame;
392 struct sk_buff *resp;
393 pn533_cmd_complete_t cmd_complete;
405 /* The rule: value + checksum = 0 */
406 static inline u8 pn533_checksum(u8 value)
411 /* The rule: sum(data elements) + checksum = 0 */
412 static u8 pn533_data_checksum(u8 *data, int datalen)
417 for (i = 0; i < datalen; i++)
420 return pn533_checksum(sum);
424 * pn533_tx_frame_ack - create a ack frame
425 * @frame: The frame to be set as ack
427 * Ack is different type of standard frame. As a standard frame, it has
428 * preamble and start_frame. However the checksum of this frame must fail,
429 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
430 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
431 * After datalen_checksum field, the postamble is placed.
433 static void pn533_tx_frame_ack(struct pn533_frame *frame)
436 frame->start_frame = cpu_to_be16(PN533_SOF);
438 frame->datalen_checksum = 0xFF;
439 /* data[0] is used as postamble */
443 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
446 frame->start_frame = cpu_to_be16(PN533_SOF);
447 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
448 PN533_FRAME_CMD(frame) = cmd;
452 static void pn533_tx_frame_finish(struct pn533_frame *frame)
454 frame->datalen_checksum = pn533_checksum(frame->datalen);
456 PN533_FRAME_CHECKSUM(frame) =
457 pn533_data_checksum(frame->data, frame->datalen);
459 PN533_FRAME_POSTAMBLE(frame) = 0;
462 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
466 if (frame->start_frame != cpu_to_be16(PN533_SOF))
469 checksum = pn533_checksum(frame->datalen);
470 if (checksum != frame->datalen_checksum)
473 checksum = pn533_data_checksum(frame->data, frame->datalen);
474 if (checksum != PN533_FRAME_CHECKSUM(frame))
480 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
482 if (frame->start_frame != cpu_to_be16(PN533_SOF))
485 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
491 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
493 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
497 static void pn533_wq_cmd_complete(struct work_struct *work)
499 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
500 struct pn533_frame *in_frame;
503 in_frame = dev->wq_in_frame;
505 if (dev->wq_in_error)
506 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
509 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
510 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
511 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
513 if (rc != -EINPROGRESS)
514 queue_work(dev->wq, &dev->cmd_work);
517 static void pn533_recv_response(struct urb *urb)
519 struct pn533 *dev = urb->context;
520 struct pn533_frame *in_frame;
522 dev->wq_in_frame = NULL;
524 switch (urb->status) {
531 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
532 " status: %d", urb->status);
533 dev->wq_in_error = urb->status;
536 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
538 dev->wq_in_error = urb->status;
542 in_frame = dev->in_urb->transfer_buffer;
544 if (!pn533_rx_frame_is_valid(in_frame)) {
545 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
546 dev->wq_in_error = -EIO;
550 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
551 nfc_dev_err(&dev->interface->dev, "The received frame is not "
552 "response to the last command");
553 dev->wq_in_error = -EIO;
557 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
558 dev->wq_in_error = 0;
559 dev->wq_in_frame = in_frame;
562 queue_work(dev->wq, &dev->cmd_complete_work);
565 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
567 dev->in_urb->complete = pn533_recv_response;
569 return usb_submit_urb(dev->in_urb, flags);
572 static void pn533_recv_ack(struct urb *urb)
574 struct pn533 *dev = urb->context;
575 struct pn533_frame *in_frame;
578 switch (urb->status) {
585 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
586 " status: %d", urb->status);
587 dev->wq_in_error = urb->status;
590 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
592 dev->wq_in_error = urb->status;
596 in_frame = dev->in_urb->transfer_buffer;
598 if (!pn533_rx_frame_is_ack(in_frame)) {
599 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
600 dev->wq_in_error = -EIO;
604 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
606 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
608 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
610 dev->wq_in_error = rc;
617 dev->wq_in_frame = NULL;
618 queue_work(dev->wq, &dev->cmd_complete_work);
621 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
623 dev->in_urb->complete = pn533_recv_ack;
625 return usb_submit_urb(dev->in_urb, flags);
628 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
632 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
634 pn533_tx_frame_ack(dev->out_frame);
636 dev->out_urb->transfer_buffer = dev->out_frame;
637 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
638 rc = usb_submit_urb(dev->out_urb, flags);
643 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
644 struct pn533_frame *out_frame,
645 struct pn533_frame *in_frame,
647 pn533_cmd_complete_t cmd_complete,
652 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
653 PN533_FRAME_CMD(out_frame));
655 dev->cmd = PN533_FRAME_CMD(out_frame);
656 dev->cmd_complete = cmd_complete;
657 dev->cmd_complete_arg = arg;
659 dev->out_urb->transfer_buffer = out_frame;
660 dev->out_urb->transfer_buffer_length =
661 PN533_FRAME_SIZE(out_frame);
663 dev->in_urb->transfer_buffer = in_frame;
664 dev->in_urb->transfer_buffer_length = in_frame_len;
666 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
670 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
677 usb_unlink_urb(dev->out_urb);
681 static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
683 struct pn533_frame *frame;
684 /* payload is already there, just update datalen */
685 int payload_len = skb->len;
687 skb_push(skb, PN533_FRAME_HEADER_LEN);
688 skb_put(skb, PN533_FRAME_TAIL_LEN);
690 frame = (struct pn533_frame *)skb->data;
692 pn533_tx_frame_init(frame, cmd_code);
693 frame->datalen += payload_len;
694 pn533_tx_frame_finish(frame);
697 struct pn533_send_async_complete_arg {
698 pn533_send_async_complete_t complete_cb;
699 void *complete_cb_context;
700 struct sk_buff *resp;
704 static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
707 struct pn533_send_async_complete_arg *arg = _arg;
709 struct sk_buff *req = arg->req;
710 struct sk_buff *resp = arg->resp;
712 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
717 if (params_len < 0) {
718 nfc_dev_err(&dev->interface->dev,
719 "Error %d when starting as a target",
722 arg->complete_cb(dev, arg->complete_cb_context,
723 ERR_PTR(params_len));
729 skb_put(resp, PN533_FRAME_SIZE(frame));
730 skb_pull(resp, PN533_FRAME_HEADER_LEN);
731 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
733 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
740 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
741 struct sk_buff *req, struct sk_buff *resp,
743 pn533_send_async_complete_t complete_cb,
744 void *complete_cb_context)
746 struct pn533_cmd *cmd;
747 struct pn533_send_async_complete_arg *arg;
750 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
752 arg = kzalloc(sizeof(arg), GFP_KERNEL);
756 arg->complete_cb = complete_cb;
757 arg->complete_cb_context = complete_cb_context;
761 pn533_build_cmd_frame(cmd_code, req);
763 mutex_lock(&dev->cmd_lock);
765 if (!dev->cmd_pending) {
766 rc = __pn533_send_cmd_frame_async(dev,
767 (struct pn533_frame *)req->data,
768 (struct pn533_frame *)resp->data,
769 resp_len, pn533_send_async_complete,
774 dev->cmd_pending = 1;
778 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
780 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
786 INIT_LIST_HEAD(&cmd->queue);
787 cmd->cmd_code = cmd_code;
792 list_add_tail(&cmd->queue, &dev->cmd_queue);
799 mutex_unlock(&dev->cmd_lock);
803 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
805 pn533_send_async_complete_t complete_cb,
806 void *complete_cb_context)
808 struct sk_buff *resp;
810 int resp_len = PN533_FRAME_HEADER_LEN +
811 PN533_FRAME_MAX_PAYLOAD_LEN +
812 PN533_FRAME_TAIL_LEN;
814 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
816 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
820 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
821 complete_cb_context);
828 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
830 pn533_send_async_complete_t complete_cb,
831 void *complete_cb_context)
833 struct sk_buff *resp;
836 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
838 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
842 rc = __pn533_send_async(dev, cmd_code, req, resp,
843 PN533_NORMAL_FRAME_MAX_LEN,
844 complete_cb, complete_cb_context);
851 static void pn533_wq_cmd(struct work_struct *work)
853 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
854 struct pn533_cmd *cmd;
856 mutex_lock(&dev->cmd_lock);
858 if (list_empty(&dev->cmd_queue)) {
859 dev->cmd_pending = 0;
860 mutex_unlock(&dev->cmd_lock);
864 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
866 list_del(&cmd->queue);
868 mutex_unlock(&dev->cmd_lock);
870 if (cmd->cmd_code != PN533_CMD_UNDEF)
871 __pn533_send_cmd_frame_async(dev,
872 (struct pn533_frame *)cmd->req->data,
873 (struct pn533_frame *)cmd->resp->data,
874 PN533_NORMAL_FRAME_MAX_LEN,
875 pn533_send_async_complete,
878 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
880 cmd->cmd_complete, cmd->arg);
885 static int pn533_send_cmd_frame_async(struct pn533 *dev,
886 struct pn533_frame *out_frame,
887 struct pn533_frame *in_frame,
889 pn533_cmd_complete_t cmd_complete,
892 struct pn533_cmd *cmd;
895 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
897 mutex_lock(&dev->cmd_lock);
899 if (!dev->cmd_pending) {
900 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
901 in_frame_len, cmd_complete,
904 dev->cmd_pending = 1;
909 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
911 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
917 INIT_LIST_HEAD(&cmd->queue);
918 cmd->out_frame = out_frame;
919 cmd->in_frame = in_frame;
920 cmd->in_frame_len = in_frame_len;
921 cmd->cmd_code = PN533_CMD_UNDEF;
922 cmd->cmd_complete = cmd_complete;
925 list_add_tail(&cmd->queue, &dev->cmd_queue);
928 mutex_unlock(&dev->cmd_lock);
933 struct pn533_sync_cmd_response {
934 struct sk_buff *resp;
935 struct completion done;
938 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
939 struct sk_buff *resp)
941 struct pn533_sync_cmd_response *arg = _arg;
943 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
946 complete(&arg->done);
951 /* pn533_send_cmd_sync
953 * Please note the req parameter is freed inside the function to
954 * limit a number of return value interpretations by the caller.
956 * 1. negative in case of error during TX path -> req should be freed
958 * 2. negative in case of error during RX path -> req should not be freed
959 * as it's been already freed at the begining of RX path by
962 * 3. valid pointer in case of succesfult RX path
964 * A caller has to check a return value with IS_ERR macro. If the test pass,
965 * the returned pointer is valid.
968 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
972 struct pn533_sync_cmd_response arg;
974 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
976 init_completion(&arg.done);
978 rc = pn533_send_cmd_async(dev, cmd_code, req,
979 pn533_send_sync_complete, &arg);
985 wait_for_completion(&arg.done);
990 static void pn533_send_complete(struct urb *urb)
992 struct pn533 *dev = urb->context;
994 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
996 switch (urb->status) {
1003 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1004 " status: %d", urb->status);
1007 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1008 " %d", urb->status);
1012 static struct sk_buff *pn533_alloc_skb(unsigned int size)
1014 struct sk_buff *skb;
1016 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1018 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1021 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1026 struct pn533_target_type_a {
1034 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1035 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1036 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1038 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1039 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1041 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1042 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1044 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1045 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1046 #define PN533_TYPE_A_SEL_PROT_DEP 2
1047 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1049 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1050 int target_data_len)
1055 if (target_data_len < sizeof(struct pn533_target_type_a))
1058 /* The lenght check of nfcid[] and ats[] are not being performed because
1059 the values are not being used */
1061 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1062 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1063 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1065 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1066 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1067 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1068 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1071 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1072 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1078 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1081 struct pn533_target_type_a *tgt_type_a;
1083 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1085 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1088 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1089 case PN533_TYPE_A_SEL_PROT_MIFARE:
1090 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1092 case PN533_TYPE_A_SEL_PROT_ISO14443:
1093 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1095 case PN533_TYPE_A_SEL_PROT_DEP:
1096 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1098 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1099 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1100 NFC_PROTO_NFC_DEP_MASK;
1104 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1105 nfc_tgt->sel_res = tgt_type_a->sel_res;
1106 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1107 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1112 struct pn533_target_felica {
1121 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1122 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1124 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1125 int target_data_len)
1127 if (target_data_len < sizeof(struct pn533_target_felica))
1130 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1136 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1139 struct pn533_target_felica *tgt_felica;
1141 tgt_felica = (struct pn533_target_felica *) tgt_data;
1143 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1146 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1147 tgt_felica->nfcid2[1] ==
1148 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1149 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1151 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1153 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1154 nfc_tgt->sensf_res_len = 9;
1159 struct pn533_target_jewel {
1164 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1165 int target_data_len)
1170 if (target_data_len < sizeof(struct pn533_target_jewel))
1173 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1174 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1175 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1177 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1178 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1179 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1180 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1186 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1189 struct pn533_target_jewel *tgt_jewel;
1191 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1193 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1196 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1197 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1198 nfc_tgt->nfcid1_len = 4;
1199 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1204 struct pn533_type_b_prot_info {
1210 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1211 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1212 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1214 struct pn533_type_b_sens_res {
1218 struct pn533_type_b_prot_info prot_info;
1221 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1223 struct pn533_target_type_b {
1224 struct pn533_type_b_sens_res sensb_res;
1229 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1230 int target_data_len)
1232 if (target_data_len < sizeof(struct pn533_target_type_b))
1235 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1238 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1239 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1245 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1248 struct pn533_target_type_b *tgt_type_b;
1250 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1252 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1255 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1260 struct pn533_poll_response {
1266 static int pn533_target_found(struct pn533 *dev,
1267 struct pn533_poll_response *resp, int resp_len)
1269 int target_data_len;
1270 struct nfc_target nfc_tgt;
1273 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1274 dev->poll_mod_curr);
1279 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1281 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1283 switch (dev->poll_mod_curr) {
1284 case PN533_POLL_MOD_106KBPS_A:
1285 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1288 case PN533_POLL_MOD_212KBPS_FELICA:
1289 case PN533_POLL_MOD_424KBPS_FELICA:
1290 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1293 case PN533_POLL_MOD_106KBPS_JEWEL:
1294 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1297 case PN533_POLL_MOD_847KBPS_B:
1298 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1302 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1310 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1311 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1312 " have the desired protocol");
1316 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1317 "0x%x", nfc_tgt.supported_protocols);
1319 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1321 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1326 static inline void pn533_poll_next_mod(struct pn533 *dev)
1328 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1331 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1333 dev->poll_mod_count = 0;
1336 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1338 dev->poll_mod_active[dev->poll_mod_count] =
1339 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1340 dev->poll_mod_count++;
1343 static void pn533_poll_create_mod_list(struct pn533 *dev,
1344 u32 im_protocols, u32 tm_protocols)
1346 pn533_poll_reset_mod_list(dev);
1348 if (im_protocols & NFC_PROTO_MIFARE_MASK
1349 || im_protocols & NFC_PROTO_ISO14443_MASK
1350 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
1351 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1353 if (im_protocols & NFC_PROTO_FELICA_MASK
1354 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1355 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1356 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1359 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1360 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1362 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1363 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1366 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1369 static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
1371 struct pn533_poll_response *resp;
1374 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1376 resp = (struct pn533_poll_response *) params;
1378 rc = pn533_target_found(dev, resp, params_len);
1380 /* We must stop the poll after a valid target found */
1382 pn533_poll_reset_mod_list(dev);
1390 static int pn533_init_target_frame(struct pn533_frame *frame,
1391 u8 *gb, size_t gb_len)
1393 struct pn533_cmd_init_target *cmd;
1395 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1396 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1397 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1398 0xff, 0xff}; /* System code */
1399 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1401 0x40}; /* SEL_RES for DEP */
1403 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1404 cmd = kzalloc(cmd_len, GFP_KERNEL);
1408 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1410 /* DEP support only */
1411 cmd->mode |= PN533_INIT_TARGET_DEP;
1414 memcpy(cmd->felica, felica_params, 18);
1415 get_random_bytes(cmd->felica + 2, 6);
1418 memset(cmd->nfcid3, 0, 10);
1419 memcpy(cmd->nfcid3, cmd->felica, 8);
1422 memcpy(cmd->mifare, mifare_params, 6);
1425 cmd->gb_len = gb_len;
1426 memcpy(cmd->gb, gb, gb_len);
1429 cmd->gb[gb_len] = 0;
1431 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1433 frame->datalen += cmd_len;
1435 pn533_tx_frame_finish(frame);
1442 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1443 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1444 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1445 struct sk_buff *resp)
1449 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1452 return PTR_ERR(resp);
1454 status = resp->data[0];
1455 skb_pull(resp, sizeof(status));
1458 nfc_tm_deactivated(dev->nfc_dev);
1460 dev_kfree_skb(resp);
1464 return nfc_tm_data_received(dev->nfc_dev, resp);
1467 static void pn533_wq_tg_get_data(struct work_struct *work)
1469 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1471 struct sk_buff *skb;
1474 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1476 skb = pn533_alloc_skb(0);
1480 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1481 pn533_tm_get_data_complete, NULL);
1489 #define ATR_REQ_GB_OFFSET 17
1490 static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
1492 struct pn533_cmd_init_target_response *resp;
1493 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1497 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1499 if (params_len < 0) {
1500 nfc_dev_err(&dev->interface->dev,
1501 "Error %d when starting as a target",
1507 if (params_len < ATR_REQ_GB_OFFSET + 1)
1510 resp = (struct pn533_cmd_init_target_response *) params;
1512 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1513 resp->mode, params_len);
1515 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1516 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1517 comm_mode = NFC_COMM_ACTIVE;
1519 /* Again, only DEP */
1520 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1523 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1524 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1526 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1527 comm_mode, gb, gb_len);
1529 nfc_dev_err(&dev->interface->dev,
1530 "Error when signaling target activation");
1536 queue_work(dev->wq, &dev->tg_work);
1541 static void pn533_listen_mode_timer(unsigned long data)
1543 struct pn533 *dev = (struct pn533 *) data;
1545 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1547 /* An ack will cancel the last issued command (poll) */
1548 pn533_send_ack(dev, GFP_ATOMIC);
1550 dev->cancel_listen = 1;
1552 pn533_poll_next_mod(dev);
1554 queue_work(dev->wq, &dev->poll_work);
1557 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1558 u8 *params, int params_len)
1560 struct pn533_poll_modulations *cur_mod;
1563 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1565 if (params_len == -ENOENT) {
1566 if (dev->poll_mod_count != 0)
1569 nfc_dev_err(&dev->interface->dev,
1570 "Polling operation has been stopped");
1575 if (params_len < 0) {
1576 nfc_dev_err(&dev->interface->dev,
1577 "Error %d when running poll", params_len);
1582 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1584 if (cur_mod->len == 0) {
1585 del_timer(&dev->listen_timer);
1587 return pn533_init_target_complete(dev, params, params_len);
1589 rc = pn533_start_poll_complete(dev, params, params_len);
1594 pn533_poll_next_mod(dev);
1596 queue_work(dev->wq, &dev->poll_work);
1601 pn533_poll_reset_mod_list(dev);
1602 dev->poll_protocols = 0;
1606 static void pn533_build_poll_frame(struct pn533 *dev,
1607 struct pn533_frame *frame,
1608 struct pn533_poll_modulations *mod)
1610 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
1612 if (mod->len == 0) {
1614 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1617 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1619 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1620 frame->datalen += mod->len;
1622 pn533_tx_frame_finish(frame);
1626 static int pn533_send_poll_frame(struct pn533 *dev)
1628 struct pn533_poll_modulations *cur_mod;
1631 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1633 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
1635 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1636 PN533_NORMAL_FRAME_MAX_LEN,
1637 pn533_poll_complete,
1640 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1645 static void pn533_wq_poll(struct work_struct *work)
1647 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1648 struct pn533_poll_modulations *cur_mod;
1651 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1653 nfc_dev_dbg(&dev->interface->dev,
1654 "%s cancel_listen %d modulation len %d",
1655 __func__, dev->cancel_listen, cur_mod->len);
1657 if (dev->cancel_listen == 1) {
1658 dev->cancel_listen = 0;
1659 usb_kill_urb(dev->in_urb);
1662 rc = pn533_send_poll_frame(dev);
1666 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1667 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1672 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1673 u32 im_protocols, u32 tm_protocols)
1675 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1677 nfc_dev_dbg(&dev->interface->dev,
1678 "%s: im protocols 0x%x tm protocols 0x%x",
1679 __func__, im_protocols, tm_protocols);
1681 if (dev->tgt_active_prot) {
1682 nfc_dev_err(&dev->interface->dev,
1683 "Cannot poll with a target already activated");
1687 if (dev->tgt_mode) {
1688 nfc_dev_err(&dev->interface->dev,
1689 "Cannot poll while already being activated");
1694 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1695 if (dev->gb == NULL)
1699 dev->poll_mod_curr = 0;
1700 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1701 dev->poll_protocols = im_protocols;
1702 dev->listen_protocols = tm_protocols;
1704 return pn533_send_poll_frame(dev);
1707 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1709 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1711 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1713 del_timer(&dev->listen_timer);
1715 if (!dev->poll_mod_count) {
1716 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1721 /* An ack will cancel the last issued command (poll) */
1722 pn533_send_ack(dev, GFP_KERNEL);
1724 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1725 usb_kill_urb(dev->in_urb);
1727 pn533_poll_reset_mod_list(dev);
1730 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1732 struct pn533_cmd_activate_response *rsp;
1736 struct sk_buff *skb;
1737 struct sk_buff *resp;
1739 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1741 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1745 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1746 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1748 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1750 return PTR_ERR(resp);
1752 rsp = (struct pn533_cmd_activate_response *) resp->data;
1753 rc = rsp->status & PN533_CMD_RET_MASK;
1754 if (rc != PN533_CMD_RET_SUCCESS)
1755 dev_kfree_skb(resp);
1758 /* ATR_RES general bytes are located at offset 16 */
1759 gt_len = resp->len - 16;
1760 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1762 dev_kfree_skb(resp);
1766 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1767 struct nfc_target *target, u32 protocol)
1769 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1772 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1775 if (dev->poll_mod_count) {
1776 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1781 if (dev->tgt_active_prot) {
1782 nfc_dev_err(&dev->interface->dev, "There is already an active"
1787 if (!dev->tgt_available_prots) {
1788 nfc_dev_err(&dev->interface->dev, "There is no available target"
1793 if (!(dev->tgt_available_prots & (1 << protocol))) {
1794 nfc_dev_err(&dev->interface->dev, "The target does not support"
1795 " the requested protocol %u", protocol);
1799 if (protocol == NFC_PROTO_NFC_DEP) {
1800 rc = pn533_activate_target_nfcdep(dev);
1802 nfc_dev_err(&dev->interface->dev, "Error %d when"
1803 " activating target with"
1804 " NFC_DEP protocol", rc);
1809 dev->tgt_active_prot = protocol;
1810 dev->tgt_available_prots = 0;
1815 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1816 struct nfc_target *target)
1818 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1820 struct sk_buff *skb;
1821 struct sk_buff *resp;
1825 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1827 if (!dev->tgt_active_prot) {
1828 nfc_dev_err(&dev->interface->dev, "There is no active target");
1832 dev->tgt_active_prot = 0;
1833 skb_queue_purge(&dev->resp_q);
1835 skb = pn533_alloc_skb(sizeof(u8));
1839 *skb_put(skb, 1) = 1; /* TG*/
1841 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1845 rc = resp->data[0] & PN533_CMD_RET_MASK;
1846 if (rc != PN533_CMD_RET_SUCCESS)
1847 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1850 dev_kfree_skb(resp);
1855 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1856 struct sk_buff *resp)
1858 struct pn533_cmd_jump_dep_response *rsp;
1861 u8 active = *(u8 *)arg;
1866 return PTR_ERR(resp);
1868 if (dev->tgt_available_prots &&
1869 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1870 nfc_dev_err(&dev->interface->dev,
1871 "The target does not support DEP");
1876 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1878 rc = rsp->status & PN533_CMD_RET_MASK;
1879 if (rc != PN533_CMD_RET_SUCCESS) {
1880 nfc_dev_err(&dev->interface->dev,
1881 "Bringing DEP link up failed %d", rc);
1885 if (!dev->tgt_available_prots) {
1886 struct nfc_target nfc_target;
1888 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1890 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1891 nfc_target.nfcid1_len = 10;
1892 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1893 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1897 dev->tgt_available_prots = 0;
1900 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1902 /* ATR_RES general bytes are located at offset 17 */
1903 target_gt_len = resp->len - 17;
1904 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1905 rsp->gt, target_gt_len);
1907 rc = nfc_dep_link_is_up(dev->nfc_dev,
1908 dev->nfc_dev->targets[0].idx,
1909 !active, NFC_RF_INITIATOR);
1912 dev_kfree_skb(resp);
1916 static int pn533_mod_to_baud(struct pn533 *dev)
1918 switch (dev->poll_mod_curr) {
1919 case PN533_POLL_MOD_106KBPS_A:
1921 case PN533_POLL_MOD_212KBPS_FELICA:
1923 case PN533_POLL_MOD_424KBPS_FELICA:
1930 #define PASSIVE_DATA_LEN 5
1931 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1932 u8 comm_mode, u8* gb, size_t gb_len)
1934 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1935 struct sk_buff *skb;
1936 int rc, baud, skb_len;
1939 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1941 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1943 if (dev->poll_mod_count) {
1944 nfc_dev_err(&dev->interface->dev,
1945 "Cannot bring the DEP link up while polling");
1949 if (dev->tgt_active_prot) {
1950 nfc_dev_err(&dev->interface->dev,
1951 "There is already an active target");
1955 baud = pn533_mod_to_baud(dev);
1957 nfc_dev_err(&dev->interface->dev,
1958 "Invalid curr modulation %d", dev->poll_mod_curr);
1962 skb_len = 3 + gb_len; /* ActPass + BR + Next */
1963 if (comm_mode == NFC_COMM_PASSIVE)
1964 skb_len += PASSIVE_DATA_LEN;
1966 skb = pn533_alloc_skb(skb_len);
1970 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1971 *skb_put(skb, 1) = baud; /* Baud rate */
1973 next = skb_put(skb, 1); /* Next */
1976 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1977 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1982 if (gb != NULL && gb_len > 0) {
1983 memcpy(skb_put(skb, gb_len), gb, gb_len);
1984 *next |= 4; /* We have some Gi */
1989 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1997 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1998 pn533_in_dep_link_up_complete, arg);
2008 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2010 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2012 pn533_poll_reset_mod_list(dev);
2014 if (dev->tgt_mode || dev->tgt_active_prot) {
2015 pn533_send_ack(dev, GFP_KERNEL);
2016 usb_kill_urb(dev->in_urb);
2019 dev->tgt_active_prot = 0;
2022 skb_queue_purge(&dev->resp_q);
2027 static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2030 int payload_len = skb->len;
2031 struct pn533_frame *out_frame;
2034 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2037 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2038 /* TODO: Implement support to multi-part data exchange */
2039 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2041 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2045 skb_push(skb, PN533_FRAME_HEADER_LEN);
2047 if (target == true) {
2048 switch (dev->device_type) {
2049 case PN533_DEVICE_PASORI:
2050 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2051 out_frame = (struct pn533_frame *) skb->data;
2052 pn533_tx_frame_init(out_frame,
2053 PN533_CMD_IN_COMM_THRU);
2059 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2060 out_frame = (struct pn533_frame *) skb->data;
2061 pn533_tx_frame_init(out_frame,
2062 PN533_CMD_IN_DATA_EXCHANGE);
2064 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2066 out_frame->datalen += sizeof(u8);
2072 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2073 out_frame = (struct pn533_frame *) skb->data;
2074 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2078 /* The data is already in the out_frame, just update the datalen */
2079 out_frame->datalen += payload_len;
2081 pn533_tx_frame_finish(out_frame);
2082 skb_put(skb, PN533_FRAME_TAIL_LEN);
2087 struct pn533_data_exchange_arg {
2088 struct sk_buff *skb_resp;
2089 struct sk_buff *skb_out;
2090 data_exchange_cb_t cb;
2094 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2096 struct sk_buff *skb, *tmp, *t;
2097 unsigned int skb_len = 0, tmp_len = 0;
2099 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2101 if (skb_queue_empty(&dev->resp_q))
2104 if (skb_queue_len(&dev->resp_q) == 1) {
2105 skb = skb_dequeue(&dev->resp_q);
2109 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2110 skb_len += tmp->len;
2112 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2115 skb = alloc_skb(skb_len, GFP_KERNEL);
2119 skb_put(skb, skb_len);
2121 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2122 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2123 tmp_len += tmp->len;
2127 skb_queue_purge(&dev->resp_q);
2132 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2133 u8 *params, int params_len)
2135 struct pn533_data_exchange_arg *arg = _arg;
2136 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
2137 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2142 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2144 dev_kfree_skb(arg->skb_out);
2146 if (params_len < 0) { /* error */
2153 cmd_ret = status & PN533_CMD_RET_MASK;
2154 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2155 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2156 " exchanging data", cmd_ret);
2161 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
2162 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
2163 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
2164 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
2165 skb_queue_tail(&dev->resp_q, skb_resp);
2167 if (status & PN533_CMD_MI_MASK) {
2168 queue_work(dev->wq, &dev->mi_work);
2169 return -EINPROGRESS;
2172 skb = pn533_build_response(dev);
2176 arg->cb(arg->cb_context, skb, 0);
2181 skb_queue_purge(&dev->resp_q);
2182 dev_kfree_skb(skb_resp);
2183 arg->cb(arg->cb_context, NULL, err);
2188 static int pn533_transceive(struct nfc_dev *nfc_dev,
2189 struct nfc_target *target, struct sk_buff *skb,
2190 data_exchange_cb_t cb, void *cb_context)
2192 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2193 struct pn533_frame *out_frame, *in_frame;
2194 struct pn533_data_exchange_arg *arg;
2195 struct sk_buff *skb_resp;
2199 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2201 if (!dev->tgt_active_prot) {
2202 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2203 " there is no active target");
2208 rc = pn533_build_tx_frame(dev, skb, true);
2212 skb_resp_len = PN533_FRAME_HEADER_LEN +
2213 PN533_CMD_DATAEXCH_HEAD_LEN +
2214 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2215 PN533_FRAME_TAIL_LEN;
2217 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
2223 in_frame = (struct pn533_frame *) skb_resp->data;
2224 out_frame = (struct pn533_frame *) skb->data;
2226 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2232 arg->skb_resp = skb_resp;
2235 arg->cb_context = cb_context;
2237 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2238 pn533_data_exchange_complete, arg);
2240 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2241 " perform data_exchange", rc);
2250 kfree_skb(skb_resp);
2256 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2257 struct sk_buff *resp)
2261 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2264 return PTR_ERR(resp);
2266 status = resp->data[0];
2268 dev_kfree_skb(resp);
2271 nfc_tm_deactivated(dev->nfc_dev);
2278 queue_work(dev->wq, &dev->tg_work);
2283 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2285 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2288 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2290 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2291 nfc_dev_err(&dev->interface->dev,
2292 "Data length greater than the max allowed: %d",
2293 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2297 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2298 pn533_tm_send_complete, NULL);
2305 static void pn533_wq_mi_recv(struct work_struct *work)
2307 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2308 struct sk_buff *skb_cmd;
2309 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2310 struct pn533_frame *out_frame, *in_frame;
2311 struct sk_buff *skb_resp;
2315 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2317 /* This is a zero payload size skb */
2318 skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2319 if (skb_cmd == NULL)
2322 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2324 rc = pn533_build_tx_frame(dev, skb_cmd, true);
2328 skb_resp_len = PN533_FRAME_HEADER_LEN +
2329 PN533_CMD_DATAEXCH_HEAD_LEN +
2330 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2331 PN533_FRAME_TAIL_LEN;
2333 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2339 in_frame = (struct pn533_frame *) skb_resp->data;
2340 out_frame = (struct pn533_frame *) skb_cmd->data;
2342 arg->skb_resp = skb_resp;
2343 arg->skb_out = skb_cmd;
2345 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2347 pn533_data_exchange_complete,
2348 dev->cmd_complete_arg);
2352 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2353 " perform data_exchange", rc);
2355 kfree_skb(skb_resp);
2361 pn533_send_ack(dev, GFP_KERNEL);
2365 queue_work(dev->wq, &dev->cmd_work);
2368 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2371 struct sk_buff *skb;
2372 struct sk_buff *resp;
2376 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2378 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2380 skb = pn533_alloc_skb(skb_len);
2384 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2385 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2387 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2389 return PTR_ERR(resp);
2391 dev_kfree_skb(resp);
2395 static int pn533_get_firmware_version(struct pn533 *dev,
2396 struct pn533_fw_version *fv)
2398 struct sk_buff *skb;
2399 struct sk_buff *resp;
2401 skb = pn533_alloc_skb(0);
2405 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2407 return PTR_ERR(resp);
2409 fv->ic = resp->data[0];
2410 fv->ver = resp->data[1];
2411 fv->rev = resp->data[2];
2412 fv->support = resp->data[3];
2414 dev_kfree_skb(resp);
2418 static int pn533_fw_reset(struct pn533 *dev)
2420 struct sk_buff *skb;
2421 struct sk_buff *resp;
2423 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2425 skb = pn533_alloc_skb(sizeof(u8));
2429 *skb_put(skb, sizeof(u8)) = 0x1;
2431 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2433 return PTR_ERR(resp);
2435 dev_kfree_skb(resp);
2440 static struct nfc_ops pn533_nfc_ops = {
2443 .dep_link_up = pn533_dep_link_up,
2444 .dep_link_down = pn533_dep_link_down,
2445 .start_poll = pn533_start_poll,
2446 .stop_poll = pn533_stop_poll,
2447 .activate_target = pn533_activate_target,
2448 .deactivate_target = pn533_deactivate_target,
2449 .im_transceive = pn533_transceive,
2450 .tm_send = pn533_tm_send,
2453 static int pn533_setup(struct pn533 *dev)
2455 struct pn533_config_max_retries max_retries;
2456 struct pn533_config_timing timing;
2457 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2460 switch (dev->device_type) {
2461 case PN533_DEVICE_STD:
2462 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2463 max_retries.mx_rty_psl = 2;
2464 max_retries.mx_rty_passive_act =
2465 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2467 timing.rfu = PN533_CONFIG_TIMING_102;
2468 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2469 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2473 case PN533_DEVICE_PASORI:
2474 max_retries.mx_rty_atr = 0x2;
2475 max_retries.mx_rty_psl = 0x1;
2476 max_retries.mx_rty_passive_act =
2477 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2479 timing.rfu = PN533_CONFIG_TIMING_102;
2480 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2481 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2486 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2491 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2492 (u8 *)&max_retries, sizeof(max_retries));
2494 nfc_dev_err(&dev->interface->dev,
2495 "Error on setting MAX_RETRIES config");
2500 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2501 (u8 *)&timing, sizeof(timing));
2503 nfc_dev_err(&dev->interface->dev,
2504 "Error on setting RF timings");
2508 switch (dev->device_type) {
2509 case PN533_DEVICE_STD:
2512 case PN533_DEVICE_PASORI:
2513 pn533_fw_reset(dev);
2515 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2518 nfc_dev_err(&dev->interface->dev,
2519 "Error while settings PASORI config");
2523 pn533_fw_reset(dev);
2531 static int pn533_probe(struct usb_interface *interface,
2532 const struct usb_device_id *id)
2534 struct pn533_fw_version fw_ver;
2536 struct usb_host_interface *iface_desc;
2537 struct usb_endpoint_descriptor *endpoint;
2538 int in_endpoint = 0;
2539 int out_endpoint = 0;
2544 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2548 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2549 dev->interface = interface;
2550 mutex_init(&dev->cmd_lock);
2552 iface_desc = interface->cur_altsetting;
2553 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2554 endpoint = &iface_desc->endpoint[i].desc;
2556 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2557 in_endpoint = endpoint->bEndpointAddress;
2559 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2560 out_endpoint = endpoint->bEndpointAddress;
2563 if (!in_endpoint || !out_endpoint) {
2564 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2565 " bulk-out endpoint");
2570 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2571 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2572 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2573 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2575 if (!dev->in_frame || !dev->out_frame ||
2576 !dev->in_urb || !dev->out_urb)
2579 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2580 usb_rcvbulkpipe(dev->udev, in_endpoint),
2581 NULL, 0, NULL, dev);
2582 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2583 usb_sndbulkpipe(dev->udev, out_endpoint),
2585 pn533_send_complete, dev);
2587 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2588 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2589 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2590 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2591 INIT_WORK(&dev->poll_work, pn533_wq_poll);
2592 dev->wq = alloc_ordered_workqueue("pn533", 0);
2593 if (dev->wq == NULL)
2596 init_timer(&dev->listen_timer);
2597 dev->listen_timer.data = (unsigned long) dev;
2598 dev->listen_timer.function = pn533_listen_mode_timer;
2600 skb_queue_head_init(&dev->resp_q);
2602 INIT_LIST_HEAD(&dev->cmd_queue);
2604 usb_set_intfdata(interface, dev);
2606 memset(&fw_ver, 0, sizeof(fw_ver));
2607 rc = pn533_get_firmware_version(dev, &fw_ver);
2611 nfc_dev_info(&dev->interface->dev,
2612 "NXP PN533 firmware ver %d.%d now attached",
2613 fw_ver.ver, fw_ver.rev);
2615 dev->device_type = id->driver_info;
2616 switch (dev->device_type) {
2617 case PN533_DEVICE_STD:
2618 protocols = PN533_ALL_PROTOCOLS;
2621 case PN533_DEVICE_PASORI:
2622 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2626 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2632 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2633 PN533_FRAME_HEADER_LEN +
2634 PN533_CMD_DATAEXCH_HEAD_LEN,
2635 PN533_FRAME_TAIL_LEN);
2639 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2640 nfc_set_drvdata(dev->nfc_dev, dev);
2642 rc = nfc_register_device(dev->nfc_dev);
2646 rc = pn533_setup(dev);
2648 goto unregister_nfc_dev;
2653 nfc_unregister_device(dev->nfc_dev);
2656 nfc_free_device(dev->nfc_dev);
2659 destroy_workqueue(dev->wq);
2661 kfree(dev->in_frame);
2662 usb_free_urb(dev->in_urb);
2663 kfree(dev->out_frame);
2664 usb_free_urb(dev->out_urb);
2669 static void pn533_disconnect(struct usb_interface *interface)
2672 struct pn533_cmd *cmd, *n;
2674 dev = usb_get_intfdata(interface);
2675 usb_set_intfdata(interface, NULL);
2677 nfc_unregister_device(dev->nfc_dev);
2678 nfc_free_device(dev->nfc_dev);
2680 usb_kill_urb(dev->in_urb);
2681 usb_kill_urb(dev->out_urb);
2683 destroy_workqueue(dev->wq);
2685 skb_queue_purge(&dev->resp_q);
2687 del_timer(&dev->listen_timer);
2689 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2690 list_del(&cmd->queue);
2694 kfree(dev->in_frame);
2695 usb_free_urb(dev->in_urb);
2696 kfree(dev->out_frame);
2697 usb_free_urb(dev->out_urb);
2700 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2703 static struct usb_driver pn533_driver = {
2705 .probe = pn533_probe,
2706 .disconnect = pn533_disconnect,
2707 .id_table = pn533_table,
2710 module_usb_driver(pn533_driver);
2712 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2713 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2714 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2715 MODULE_VERSION(VERSION);
2716 MODULE_LICENSE("GPL");