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 /* PN533_CMD_IN_JUMP_FOR_DEP */
302 struct pn533_cmd_jump_dep {
309 struct pn533_cmd_jump_dep_response {
323 /* PN533_TG_INIT_AS_TARGET */
324 #define PN533_INIT_TARGET_PASSIVE 0x1
325 #define PN533_INIT_TARGET_DEP 0x2
327 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
328 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
329 #define PN533_INIT_TARGET_RESP_DEP 0x4
331 struct pn533_cmd_init_target {
340 struct pn533_cmd_init_target_response {
346 struct usb_device *udev;
347 struct usb_interface *interface;
348 struct nfc_dev *nfc_dev;
351 struct pn533_frame *out_frame;
354 struct pn533_frame *in_frame;
356 struct sk_buff_head resp_q;
358 struct workqueue_struct *wq;
359 struct work_struct cmd_work;
360 struct work_struct cmd_complete_work;
361 struct work_struct poll_work;
362 struct work_struct mi_work;
363 struct work_struct tg_work;
364 struct timer_list listen_timer;
365 struct pn533_frame *wq_in_frame;
369 pn533_cmd_complete_t cmd_complete;
370 void *cmd_complete_arg;
371 struct mutex cmd_lock;
374 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
378 u32 listen_protocols;
383 u8 tgt_available_prots;
389 struct list_head cmd_queue;
394 struct list_head queue;
395 struct pn533_frame *out_frame;
396 struct pn533_frame *in_frame;
400 struct sk_buff *resp;
401 pn533_cmd_complete_t cmd_complete;
413 /* The rule: value + checksum = 0 */
414 static inline u8 pn533_checksum(u8 value)
419 /* The rule: sum(data elements) + checksum = 0 */
420 static u8 pn533_data_checksum(u8 *data, int datalen)
425 for (i = 0; i < datalen; i++)
428 return pn533_checksum(sum);
432 * pn533_tx_frame_ack - create a ack frame
433 * @frame: The frame to be set as ack
435 * Ack is different type of standard frame. As a standard frame, it has
436 * preamble and start_frame. However the checksum of this frame must fail,
437 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
438 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
439 * After datalen_checksum field, the postamble is placed.
441 static void pn533_tx_frame_ack(struct pn533_frame *frame)
444 frame->start_frame = cpu_to_be16(PN533_SOF);
446 frame->datalen_checksum = 0xFF;
447 /* data[0] is used as postamble */
451 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
454 frame->start_frame = cpu_to_be16(PN533_SOF);
455 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
456 PN533_FRAME_CMD(frame) = cmd;
460 static void pn533_tx_frame_finish(struct pn533_frame *frame)
462 frame->datalen_checksum = pn533_checksum(frame->datalen);
464 PN533_FRAME_CHECKSUM(frame) =
465 pn533_data_checksum(frame->data, frame->datalen);
467 PN533_FRAME_POSTAMBLE(frame) = 0;
470 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
474 if (frame->start_frame != cpu_to_be16(PN533_SOF))
477 checksum = pn533_checksum(frame->datalen);
478 if (checksum != frame->datalen_checksum)
481 checksum = pn533_data_checksum(frame->data, frame->datalen);
482 if (checksum != PN533_FRAME_CHECKSUM(frame))
488 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
490 if (frame->start_frame != cpu_to_be16(PN533_SOF))
493 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
499 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
501 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
505 static void pn533_wq_cmd_complete(struct work_struct *work)
507 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
508 struct pn533_frame *in_frame;
511 in_frame = dev->wq_in_frame;
513 if (dev->wq_in_error)
514 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
517 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
518 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
519 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
521 if (rc != -EINPROGRESS)
522 queue_work(dev->wq, &dev->cmd_work);
525 static void pn533_recv_response(struct urb *urb)
527 struct pn533 *dev = urb->context;
528 struct pn533_frame *in_frame;
530 dev->wq_in_frame = NULL;
532 switch (urb->status) {
539 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
540 " status: %d", urb->status);
541 dev->wq_in_error = urb->status;
544 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
546 dev->wq_in_error = urb->status;
550 in_frame = dev->in_urb->transfer_buffer;
552 if (!pn533_rx_frame_is_valid(in_frame)) {
553 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
554 dev->wq_in_error = -EIO;
558 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
559 nfc_dev_err(&dev->interface->dev, "The received frame is not "
560 "response to the last command");
561 dev->wq_in_error = -EIO;
565 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
566 dev->wq_in_error = 0;
567 dev->wq_in_frame = in_frame;
570 queue_work(dev->wq, &dev->cmd_complete_work);
573 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
575 dev->in_urb->complete = pn533_recv_response;
577 return usb_submit_urb(dev->in_urb, flags);
580 static void pn533_recv_ack(struct urb *urb)
582 struct pn533 *dev = urb->context;
583 struct pn533_frame *in_frame;
586 switch (urb->status) {
593 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
594 " status: %d", urb->status);
595 dev->wq_in_error = urb->status;
598 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
600 dev->wq_in_error = urb->status;
604 in_frame = dev->in_urb->transfer_buffer;
606 if (!pn533_rx_frame_is_ack(in_frame)) {
607 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
608 dev->wq_in_error = -EIO;
612 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
614 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
616 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
618 dev->wq_in_error = rc;
625 dev->wq_in_frame = NULL;
626 queue_work(dev->wq, &dev->cmd_complete_work);
629 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
631 dev->in_urb->complete = pn533_recv_ack;
633 return usb_submit_urb(dev->in_urb, flags);
636 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
640 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
642 pn533_tx_frame_ack(dev->out_frame);
644 dev->out_urb->transfer_buffer = dev->out_frame;
645 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
646 rc = usb_submit_urb(dev->out_urb, flags);
651 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
652 struct pn533_frame *out_frame,
653 struct pn533_frame *in_frame,
655 pn533_cmd_complete_t cmd_complete,
660 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
661 PN533_FRAME_CMD(out_frame));
663 dev->cmd = PN533_FRAME_CMD(out_frame);
664 dev->cmd_complete = cmd_complete;
665 dev->cmd_complete_arg = arg;
667 dev->out_urb->transfer_buffer = out_frame;
668 dev->out_urb->transfer_buffer_length =
669 PN533_FRAME_SIZE(out_frame);
671 dev->in_urb->transfer_buffer = in_frame;
672 dev->in_urb->transfer_buffer_length = in_frame_len;
674 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
678 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
685 usb_unlink_urb(dev->out_urb);
689 static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
691 struct pn533_frame *frame;
692 /* payload is already there, just update datalen */
693 int payload_len = skb->len;
695 skb_push(skb, PN533_FRAME_HEADER_LEN);
696 skb_put(skb, PN533_FRAME_TAIL_LEN);
698 frame = (struct pn533_frame *)skb->data;
700 pn533_tx_frame_init(frame, cmd_code);
701 frame->datalen += payload_len;
702 pn533_tx_frame_finish(frame);
705 struct pn533_send_async_complete_arg {
706 pn533_send_async_complete_t complete_cb;
707 void *complete_cb_context;
708 struct sk_buff *resp;
712 static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
715 struct pn533_send_async_complete_arg *arg = _arg;
717 struct sk_buff *req = arg->req;
718 struct sk_buff *resp = arg->resp;
720 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
725 if (params_len < 0) {
726 nfc_dev_err(&dev->interface->dev,
727 "Error %d when starting as a target",
730 arg->complete_cb(dev, arg->complete_cb_context,
731 ERR_PTR(params_len));
737 skb_put(resp, PN533_FRAME_SIZE(frame));
738 skb_pull(resp, PN533_FRAME_HEADER_LEN);
739 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
741 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
748 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
749 struct sk_buff *req, struct sk_buff *resp,
751 pn533_send_async_complete_t complete_cb,
752 void *complete_cb_context)
754 struct pn533_cmd *cmd;
755 struct pn533_send_async_complete_arg *arg;
758 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
760 arg = kzalloc(sizeof(arg), GFP_KERNEL);
764 arg->complete_cb = complete_cb;
765 arg->complete_cb_context = complete_cb_context;
769 pn533_build_cmd_frame(cmd_code, req);
771 mutex_lock(&dev->cmd_lock);
773 if (!dev->cmd_pending) {
774 rc = __pn533_send_cmd_frame_async(dev,
775 (struct pn533_frame *)req->data,
776 (struct pn533_frame *)resp->data,
777 resp_len, pn533_send_async_complete,
782 dev->cmd_pending = 1;
786 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
788 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
794 INIT_LIST_HEAD(&cmd->queue);
795 cmd->cmd_code = cmd_code;
800 list_add_tail(&cmd->queue, &dev->cmd_queue);
807 mutex_unlock(&dev->cmd_lock);
811 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
813 pn533_send_async_complete_t complete_cb,
814 void *complete_cb_context)
816 struct sk_buff *resp;
818 int resp_len = PN533_FRAME_HEADER_LEN +
819 PN533_FRAME_MAX_PAYLOAD_LEN +
820 PN533_FRAME_TAIL_LEN;
822 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
824 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
828 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
829 complete_cb_context);
836 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
838 pn533_send_async_complete_t complete_cb,
839 void *complete_cb_context)
841 struct sk_buff *resp;
844 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
846 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
850 rc = __pn533_send_async(dev, cmd_code, req, resp,
851 PN533_NORMAL_FRAME_MAX_LEN,
852 complete_cb, complete_cb_context);
859 static void pn533_wq_cmd(struct work_struct *work)
861 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
862 struct pn533_cmd *cmd;
864 mutex_lock(&dev->cmd_lock);
866 if (list_empty(&dev->cmd_queue)) {
867 dev->cmd_pending = 0;
868 mutex_unlock(&dev->cmd_lock);
872 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
874 list_del(&cmd->queue);
876 mutex_unlock(&dev->cmd_lock);
878 if (cmd->cmd_code != PN533_CMD_UNDEF)
879 __pn533_send_cmd_frame_async(dev,
880 (struct pn533_frame *)cmd->req->data,
881 (struct pn533_frame *)cmd->resp->data,
882 PN533_NORMAL_FRAME_MAX_LEN,
883 pn533_send_async_complete,
886 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
888 cmd->cmd_complete, cmd->arg);
893 static int pn533_send_cmd_frame_async(struct pn533 *dev,
894 struct pn533_frame *out_frame,
895 struct pn533_frame *in_frame,
897 pn533_cmd_complete_t cmd_complete,
900 struct pn533_cmd *cmd;
903 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
905 mutex_lock(&dev->cmd_lock);
907 if (!dev->cmd_pending) {
908 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
909 in_frame_len, cmd_complete,
912 dev->cmd_pending = 1;
917 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
919 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
925 INIT_LIST_HEAD(&cmd->queue);
926 cmd->out_frame = out_frame;
927 cmd->in_frame = in_frame;
928 cmd->in_frame_len = in_frame_len;
929 cmd->cmd_code = PN533_CMD_UNDEF;
930 cmd->cmd_complete = cmd_complete;
933 list_add_tail(&cmd->queue, &dev->cmd_queue);
936 mutex_unlock(&dev->cmd_lock);
941 struct pn533_sync_cmd_response {
942 struct sk_buff *resp;
943 struct completion done;
946 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
947 struct sk_buff *resp)
949 struct pn533_sync_cmd_response *arg = _arg;
951 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
954 complete(&arg->done);
959 /* pn533_send_cmd_sync
961 * Please note the req parameter is freed inside the function to
962 * limit a number of return value interpretations by the caller.
964 * 1. negative in case of error during TX path -> req should be freed
966 * 2. negative in case of error during RX path -> req should not be freed
967 * as it's been already freed at the begining of RX path by
970 * 3. valid pointer in case of succesfult RX path
972 * A caller has to check a return value with IS_ERR macro. If the test pass,
973 * the returned pointer is valid.
976 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
980 struct pn533_sync_cmd_response arg;
982 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
984 init_completion(&arg.done);
986 rc = pn533_send_cmd_async(dev, cmd_code, req,
987 pn533_send_sync_complete, &arg);
993 wait_for_completion(&arg.done);
998 static void pn533_send_complete(struct urb *urb)
1000 struct pn533 *dev = urb->context;
1002 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1004 switch (urb->status) {
1011 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1012 " status: %d", urb->status);
1015 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1016 " %d", urb->status);
1020 static struct sk_buff *pn533_alloc_skb(unsigned int size)
1022 struct sk_buff *skb;
1024 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1026 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1029 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1034 struct pn533_target_type_a {
1042 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1043 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1044 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1046 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1047 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1049 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1050 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1052 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1053 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1054 #define PN533_TYPE_A_SEL_PROT_DEP 2
1055 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1057 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1058 int target_data_len)
1063 if (target_data_len < sizeof(struct pn533_target_type_a))
1066 /* The lenght check of nfcid[] and ats[] are not being performed because
1067 the values are not being used */
1069 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1070 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1071 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1073 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1074 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1075 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1076 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1079 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1080 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1086 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1089 struct pn533_target_type_a *tgt_type_a;
1091 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1093 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1096 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1097 case PN533_TYPE_A_SEL_PROT_MIFARE:
1098 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1100 case PN533_TYPE_A_SEL_PROT_ISO14443:
1101 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1103 case PN533_TYPE_A_SEL_PROT_DEP:
1104 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1106 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1107 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1108 NFC_PROTO_NFC_DEP_MASK;
1112 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1113 nfc_tgt->sel_res = tgt_type_a->sel_res;
1114 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1115 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1120 struct pn533_target_felica {
1129 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1130 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1132 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1133 int target_data_len)
1135 if (target_data_len < sizeof(struct pn533_target_felica))
1138 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1144 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1147 struct pn533_target_felica *tgt_felica;
1149 tgt_felica = (struct pn533_target_felica *) tgt_data;
1151 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1154 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1155 tgt_felica->nfcid2[1] ==
1156 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1157 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1159 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1161 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1162 nfc_tgt->sensf_res_len = 9;
1167 struct pn533_target_jewel {
1172 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1173 int target_data_len)
1178 if (target_data_len < sizeof(struct pn533_target_jewel))
1181 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1182 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1183 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1185 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1186 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1187 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1188 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1194 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1197 struct pn533_target_jewel *tgt_jewel;
1199 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1201 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1204 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1205 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1206 nfc_tgt->nfcid1_len = 4;
1207 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1212 struct pn533_type_b_prot_info {
1218 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1219 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1220 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1222 struct pn533_type_b_sens_res {
1226 struct pn533_type_b_prot_info prot_info;
1229 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1231 struct pn533_target_type_b {
1232 struct pn533_type_b_sens_res sensb_res;
1237 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1238 int target_data_len)
1240 if (target_data_len < sizeof(struct pn533_target_type_b))
1243 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1246 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1247 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1253 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1256 struct pn533_target_type_b *tgt_type_b;
1258 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1260 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1263 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1268 struct pn533_poll_response {
1274 static int pn533_target_found(struct pn533 *dev,
1275 struct pn533_poll_response *resp, int resp_len)
1277 int target_data_len;
1278 struct nfc_target nfc_tgt;
1281 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1282 dev->poll_mod_curr);
1287 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1289 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1291 switch (dev->poll_mod_curr) {
1292 case PN533_POLL_MOD_106KBPS_A:
1293 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1296 case PN533_POLL_MOD_212KBPS_FELICA:
1297 case PN533_POLL_MOD_424KBPS_FELICA:
1298 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1301 case PN533_POLL_MOD_106KBPS_JEWEL:
1302 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1305 case PN533_POLL_MOD_847KBPS_B:
1306 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1310 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1318 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1319 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1320 " have the desired protocol");
1324 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1325 "0x%x", nfc_tgt.supported_protocols);
1327 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1329 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1334 static inline void pn533_poll_next_mod(struct pn533 *dev)
1336 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1339 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1341 dev->poll_mod_count = 0;
1344 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1346 dev->poll_mod_active[dev->poll_mod_count] =
1347 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1348 dev->poll_mod_count++;
1351 static void pn533_poll_create_mod_list(struct pn533 *dev,
1352 u32 im_protocols, u32 tm_protocols)
1354 pn533_poll_reset_mod_list(dev);
1356 if (im_protocols & NFC_PROTO_MIFARE_MASK
1357 || im_protocols & NFC_PROTO_ISO14443_MASK
1358 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
1359 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1361 if (im_protocols & NFC_PROTO_FELICA_MASK
1362 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1363 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1364 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1367 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1368 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1370 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1371 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1374 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1377 static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
1379 struct pn533_poll_response *resp;
1382 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1384 resp = (struct pn533_poll_response *) params;
1386 rc = pn533_target_found(dev, resp, params_len);
1388 /* We must stop the poll after a valid target found */
1390 pn533_poll_reset_mod_list(dev);
1398 static int pn533_init_target_frame(struct pn533_frame *frame,
1399 u8 *gb, size_t gb_len)
1401 struct pn533_cmd_init_target *cmd;
1403 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1404 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1405 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1406 0xff, 0xff}; /* System code */
1407 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1409 0x40}; /* SEL_RES for DEP */
1411 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1412 cmd = kzalloc(cmd_len, GFP_KERNEL);
1416 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1418 /* DEP support only */
1419 cmd->mode |= PN533_INIT_TARGET_DEP;
1422 memcpy(cmd->felica, felica_params, 18);
1423 get_random_bytes(cmd->felica + 2, 6);
1426 memset(cmd->nfcid3, 0, 10);
1427 memcpy(cmd->nfcid3, cmd->felica, 8);
1430 memcpy(cmd->mifare, mifare_params, 6);
1433 cmd->gb_len = gb_len;
1434 memcpy(cmd->gb, gb, gb_len);
1437 cmd->gb[gb_len] = 0;
1439 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1441 frame->datalen += cmd_len;
1443 pn533_tx_frame_finish(frame);
1450 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1451 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1452 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1453 u8 *params, int params_len)
1455 struct sk_buff *skb_resp = arg;
1456 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1458 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1460 if (params_len < 0) {
1461 nfc_dev_err(&dev->interface->dev,
1462 "Error %d when starting as a target",
1468 if (params_len > 0 && params[0] != 0) {
1469 nfc_tm_deactivated(dev->nfc_dev);
1473 kfree_skb(skb_resp);
1477 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1479 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1480 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
1482 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1485 static void pn533_wq_tg_get_data(struct work_struct *work)
1487 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1488 struct pn533_frame *in_frame;
1489 struct sk_buff *skb_resp;
1490 size_t skb_resp_len;
1492 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1494 skb_resp_len = PN533_FRAME_HEADER_LEN +
1495 PN533_CMD_DATAEXCH_HEAD_LEN +
1496 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1497 PN533_FRAME_TAIL_LEN;
1499 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1503 in_frame = (struct pn533_frame *)skb_resp->data;
1505 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1506 pn533_tx_frame_finish(dev->out_frame);
1508 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1510 pn533_tm_get_data_complete,
1516 #define ATR_REQ_GB_OFFSET 17
1517 static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
1519 struct pn533_cmd_init_target_response *resp;
1520 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1524 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1526 if (params_len < 0) {
1527 nfc_dev_err(&dev->interface->dev,
1528 "Error %d when starting as a target",
1534 if (params_len < ATR_REQ_GB_OFFSET + 1)
1537 resp = (struct pn533_cmd_init_target_response *) params;
1539 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1540 resp->mode, params_len);
1542 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1543 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1544 comm_mode = NFC_COMM_ACTIVE;
1546 /* Again, only DEP */
1547 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1550 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1551 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1553 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1554 comm_mode, gb, gb_len);
1556 nfc_dev_err(&dev->interface->dev,
1557 "Error when signaling target activation");
1563 queue_work(dev->wq, &dev->tg_work);
1568 static void pn533_listen_mode_timer(unsigned long data)
1570 struct pn533 *dev = (struct pn533 *) data;
1572 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1574 /* An ack will cancel the last issued command (poll) */
1575 pn533_send_ack(dev, GFP_ATOMIC);
1577 dev->cancel_listen = 1;
1579 pn533_poll_next_mod(dev);
1581 queue_work(dev->wq, &dev->poll_work);
1584 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1585 u8 *params, int params_len)
1587 struct pn533_poll_modulations *cur_mod;
1590 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1592 if (params_len == -ENOENT) {
1593 if (dev->poll_mod_count != 0)
1596 nfc_dev_err(&dev->interface->dev,
1597 "Polling operation has been stopped");
1602 if (params_len < 0) {
1603 nfc_dev_err(&dev->interface->dev,
1604 "Error %d when running poll", params_len);
1609 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1611 if (cur_mod->len == 0) {
1612 del_timer(&dev->listen_timer);
1614 return pn533_init_target_complete(dev, params, params_len);
1616 rc = pn533_start_poll_complete(dev, params, params_len);
1621 pn533_poll_next_mod(dev);
1623 queue_work(dev->wq, &dev->poll_work);
1628 pn533_poll_reset_mod_list(dev);
1629 dev->poll_protocols = 0;
1633 static void pn533_build_poll_frame(struct pn533 *dev,
1634 struct pn533_frame *frame,
1635 struct pn533_poll_modulations *mod)
1637 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
1639 if (mod->len == 0) {
1641 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1644 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1646 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1647 frame->datalen += mod->len;
1649 pn533_tx_frame_finish(frame);
1653 static int pn533_send_poll_frame(struct pn533 *dev)
1655 struct pn533_poll_modulations *cur_mod;
1658 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1660 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
1662 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1663 PN533_NORMAL_FRAME_MAX_LEN,
1664 pn533_poll_complete,
1667 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1672 static void pn533_wq_poll(struct work_struct *work)
1674 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1675 struct pn533_poll_modulations *cur_mod;
1678 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1680 nfc_dev_dbg(&dev->interface->dev,
1681 "%s cancel_listen %d modulation len %d",
1682 __func__, dev->cancel_listen, cur_mod->len);
1684 if (dev->cancel_listen == 1) {
1685 dev->cancel_listen = 0;
1686 usb_kill_urb(dev->in_urb);
1689 rc = pn533_send_poll_frame(dev);
1693 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1694 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1699 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1700 u32 im_protocols, u32 tm_protocols)
1702 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1704 nfc_dev_dbg(&dev->interface->dev,
1705 "%s: im protocols 0x%x tm protocols 0x%x",
1706 __func__, im_protocols, tm_protocols);
1708 if (dev->tgt_active_prot) {
1709 nfc_dev_err(&dev->interface->dev,
1710 "Cannot poll with a target already activated");
1714 if (dev->tgt_mode) {
1715 nfc_dev_err(&dev->interface->dev,
1716 "Cannot poll while already being activated");
1721 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1722 if (dev->gb == NULL)
1726 dev->poll_mod_curr = 0;
1727 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1728 dev->poll_protocols = im_protocols;
1729 dev->listen_protocols = tm_protocols;
1731 return pn533_send_poll_frame(dev);
1734 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1736 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1738 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1740 del_timer(&dev->listen_timer);
1742 if (!dev->poll_mod_count) {
1743 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1748 /* An ack will cancel the last issued command (poll) */
1749 pn533_send_ack(dev, GFP_KERNEL);
1751 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1752 usb_kill_urb(dev->in_urb);
1754 pn533_poll_reset_mod_list(dev);
1757 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1759 struct pn533_cmd_activate_response *rsp;
1763 struct sk_buff *skb;
1764 struct sk_buff *resp;
1766 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1768 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1772 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1773 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1775 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1777 return PTR_ERR(resp);
1779 rsp = (struct pn533_cmd_activate_response *) resp->data;
1780 rc = rsp->status & PN533_CMD_RET_MASK;
1781 if (rc != PN533_CMD_RET_SUCCESS)
1782 dev_kfree_skb(resp);
1785 /* ATR_RES general bytes are located at offset 16 */
1786 gt_len = resp->len - 16;
1787 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1789 dev_kfree_skb(resp);
1793 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1794 struct nfc_target *target, u32 protocol)
1796 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1799 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1802 if (dev->poll_mod_count) {
1803 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1808 if (dev->tgt_active_prot) {
1809 nfc_dev_err(&dev->interface->dev, "There is already an active"
1814 if (!dev->tgt_available_prots) {
1815 nfc_dev_err(&dev->interface->dev, "There is no available target"
1820 if (!(dev->tgt_available_prots & (1 << protocol))) {
1821 nfc_dev_err(&dev->interface->dev, "The target does not support"
1822 " the requested protocol %u", protocol);
1826 if (protocol == NFC_PROTO_NFC_DEP) {
1827 rc = pn533_activate_target_nfcdep(dev);
1829 nfc_dev_err(&dev->interface->dev, "Error %d when"
1830 " activating target with"
1831 " NFC_DEP protocol", rc);
1836 dev->tgt_active_prot = protocol;
1837 dev->tgt_available_prots = 0;
1842 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1843 struct nfc_target *target)
1845 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1847 struct sk_buff *skb;
1848 struct sk_buff *resp;
1852 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1854 if (!dev->tgt_active_prot) {
1855 nfc_dev_err(&dev->interface->dev, "There is no active target");
1859 dev->tgt_active_prot = 0;
1860 skb_queue_purge(&dev->resp_q);
1862 skb = pn533_alloc_skb(sizeof(u8));
1866 *skb_put(skb, 1) = 1; /* TG*/
1868 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1872 rc = resp->data[0] & PN533_CMD_RET_MASK;
1873 if (rc != PN533_CMD_RET_SUCCESS)
1874 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1877 dev_kfree_skb(resp);
1882 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1883 u8 *params, int params_len)
1885 struct pn533_cmd_jump_dep_response *resp;
1886 struct nfc_target nfc_target;
1889 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1890 u8 active = cmd->active;
1894 if (params_len == -ENOENT) {
1895 nfc_dev_dbg(&dev->interface->dev, "");
1899 if (params_len < 0) {
1900 nfc_dev_err(&dev->interface->dev,
1901 "Error %d when bringing DEP link up",
1906 if (dev->tgt_available_prots &&
1907 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1908 nfc_dev_err(&dev->interface->dev,
1909 "The target does not support DEP");
1913 resp = (struct pn533_cmd_jump_dep_response *) params;
1914 rc = resp->status & PN533_CMD_RET_MASK;
1915 if (rc != PN533_CMD_RET_SUCCESS) {
1916 nfc_dev_err(&dev->interface->dev,
1917 "Bringing DEP link up failed %d", rc);
1921 if (!dev->tgt_available_prots) {
1922 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1924 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1925 nfc_target.nfcid1_len = 10;
1926 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
1927 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1931 dev->tgt_available_prots = 0;
1934 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1936 /* ATR_RES general bytes are located at offset 17 */
1937 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1938 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1939 resp->gt, target_gt_len);
1941 rc = nfc_dep_link_is_up(dev->nfc_dev,
1942 dev->nfc_dev->targets[0].idx,
1943 !active, NFC_RF_INITIATOR);
1948 static int pn533_mod_to_baud(struct pn533 *dev)
1950 switch (dev->poll_mod_curr) {
1951 case PN533_POLL_MOD_106KBPS_A:
1953 case PN533_POLL_MOD_212KBPS_FELICA:
1955 case PN533_POLL_MOD_424KBPS_FELICA:
1962 #define PASSIVE_DATA_LEN 5
1963 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1964 u8 comm_mode, u8* gb, size_t gb_len)
1966 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1967 struct pn533_cmd_jump_dep *cmd;
1968 u8 cmd_len, *data_ptr;
1969 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1972 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1974 if (dev->poll_mod_count) {
1975 nfc_dev_err(&dev->interface->dev,
1976 "Cannot bring the DEP link up while polling");
1980 if (dev->tgt_active_prot) {
1981 nfc_dev_err(&dev->interface->dev,
1982 "There is already an active target");
1986 baud = pn533_mod_to_baud(dev);
1988 nfc_dev_err(&dev->interface->dev,
1989 "Invalid curr modulation %d", dev->poll_mod_curr);
1993 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
1994 if (comm_mode == NFC_COMM_PASSIVE)
1995 cmd_len += PASSIVE_DATA_LEN;
1997 cmd = kzalloc(cmd_len, GFP_KERNEL);
2001 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
2003 cmd->active = !comm_mode;
2006 data_ptr = cmd->data;
2007 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
2008 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
2010 data_ptr += PASSIVE_DATA_LEN;
2013 if (gb != NULL && gb_len > 0) {
2014 cmd->next |= 4; /* We have some Gi */
2015 memcpy(data_ptr, gb, gb_len);
2020 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2021 dev->out_frame->datalen += cmd_len;
2023 pn533_tx_frame_finish(dev->out_frame);
2025 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
2026 PN533_NORMAL_FRAME_MAX_LEN,
2027 pn533_in_dep_link_up_complete, cmd);
2034 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2036 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2038 pn533_poll_reset_mod_list(dev);
2040 if (dev->tgt_mode || dev->tgt_active_prot) {
2041 pn533_send_ack(dev, GFP_KERNEL);
2042 usb_kill_urb(dev->in_urb);
2045 dev->tgt_active_prot = 0;
2048 skb_queue_purge(&dev->resp_q);
2053 static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2056 int payload_len = skb->len;
2057 struct pn533_frame *out_frame;
2060 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2063 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2064 /* TODO: Implement support to multi-part data exchange */
2065 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2067 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2071 skb_push(skb, PN533_FRAME_HEADER_LEN);
2073 if (target == true) {
2074 switch (dev->device_type) {
2075 case PN533_DEVICE_PASORI:
2076 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2077 out_frame = (struct pn533_frame *) skb->data;
2078 pn533_tx_frame_init(out_frame,
2079 PN533_CMD_IN_COMM_THRU);
2085 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2086 out_frame = (struct pn533_frame *) skb->data;
2087 pn533_tx_frame_init(out_frame,
2088 PN533_CMD_IN_DATA_EXCHANGE);
2090 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2092 out_frame->datalen += sizeof(u8);
2098 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2099 out_frame = (struct pn533_frame *) skb->data;
2100 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2104 /* The data is already in the out_frame, just update the datalen */
2105 out_frame->datalen += payload_len;
2107 pn533_tx_frame_finish(out_frame);
2108 skb_put(skb, PN533_FRAME_TAIL_LEN);
2113 struct pn533_data_exchange_arg {
2114 struct sk_buff *skb_resp;
2115 struct sk_buff *skb_out;
2116 data_exchange_cb_t cb;
2120 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2122 struct sk_buff *skb, *tmp, *t;
2123 unsigned int skb_len = 0, tmp_len = 0;
2125 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2127 if (skb_queue_empty(&dev->resp_q))
2130 if (skb_queue_len(&dev->resp_q) == 1) {
2131 skb = skb_dequeue(&dev->resp_q);
2135 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2136 skb_len += tmp->len;
2138 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2141 skb = alloc_skb(skb_len, GFP_KERNEL);
2145 skb_put(skb, skb_len);
2147 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2148 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2149 tmp_len += tmp->len;
2153 skb_queue_purge(&dev->resp_q);
2158 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2159 u8 *params, int params_len)
2161 struct pn533_data_exchange_arg *arg = _arg;
2162 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
2163 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2168 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2170 dev_kfree_skb(arg->skb_out);
2172 if (params_len < 0) { /* error */
2179 cmd_ret = status & PN533_CMD_RET_MASK;
2180 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2181 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2182 " exchanging data", cmd_ret);
2187 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
2188 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
2189 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
2190 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
2191 skb_queue_tail(&dev->resp_q, skb_resp);
2193 if (status & PN533_CMD_MI_MASK) {
2194 queue_work(dev->wq, &dev->mi_work);
2195 return -EINPROGRESS;
2198 skb = pn533_build_response(dev);
2202 arg->cb(arg->cb_context, skb, 0);
2207 skb_queue_purge(&dev->resp_q);
2208 dev_kfree_skb(skb_resp);
2209 arg->cb(arg->cb_context, NULL, err);
2214 static int pn533_transceive(struct nfc_dev *nfc_dev,
2215 struct nfc_target *target, struct sk_buff *skb,
2216 data_exchange_cb_t cb, void *cb_context)
2218 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2219 struct pn533_frame *out_frame, *in_frame;
2220 struct pn533_data_exchange_arg *arg;
2221 struct sk_buff *skb_resp;
2225 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2227 if (!dev->tgt_active_prot) {
2228 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2229 " there is no active target");
2234 rc = pn533_build_tx_frame(dev, skb, true);
2238 skb_resp_len = PN533_FRAME_HEADER_LEN +
2239 PN533_CMD_DATAEXCH_HEAD_LEN +
2240 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2241 PN533_FRAME_TAIL_LEN;
2243 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
2249 in_frame = (struct pn533_frame *) skb_resp->data;
2250 out_frame = (struct pn533_frame *) skb->data;
2252 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2258 arg->skb_resp = skb_resp;
2261 arg->cb_context = cb_context;
2263 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2264 pn533_data_exchange_complete, arg);
2266 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2267 " perform data_exchange", rc);
2276 kfree_skb(skb_resp);
2282 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2283 u8 *params, int params_len)
2285 struct sk_buff *skb_out = arg;
2287 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2289 dev_kfree_skb(skb_out);
2291 if (params_len < 0) {
2292 nfc_dev_err(&dev->interface->dev,
2293 "Error %d when sending data",
2299 if (params_len > 0 && params[0] != 0) {
2300 nfc_tm_deactivated(dev->nfc_dev);
2307 queue_work(dev->wq, &dev->tg_work);
2312 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2314 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2315 struct pn533_frame *out_frame;
2318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2320 rc = pn533_build_tx_frame(dev, skb, false);
2324 out_frame = (struct pn533_frame *) skb->data;
2326 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2327 PN533_NORMAL_FRAME_MAX_LEN,
2328 pn533_tm_send_complete, skb);
2330 nfc_dev_err(&dev->interface->dev,
2331 "Error %d when trying to send data", rc);
2343 static void pn533_wq_mi_recv(struct work_struct *work)
2345 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2346 struct sk_buff *skb_cmd;
2347 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2348 struct pn533_frame *out_frame, *in_frame;
2349 struct sk_buff *skb_resp;
2353 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2355 /* This is a zero payload size skb */
2356 skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2357 if (skb_cmd == NULL)
2360 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2362 rc = pn533_build_tx_frame(dev, skb_cmd, true);
2366 skb_resp_len = PN533_FRAME_HEADER_LEN +
2367 PN533_CMD_DATAEXCH_HEAD_LEN +
2368 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2369 PN533_FRAME_TAIL_LEN;
2371 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2377 in_frame = (struct pn533_frame *) skb_resp->data;
2378 out_frame = (struct pn533_frame *) skb_cmd->data;
2380 arg->skb_resp = skb_resp;
2381 arg->skb_out = skb_cmd;
2383 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2385 pn533_data_exchange_complete,
2386 dev->cmd_complete_arg);
2390 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2391 " perform data_exchange", rc);
2393 kfree_skb(skb_resp);
2399 pn533_send_ack(dev, GFP_KERNEL);
2403 queue_work(dev->wq, &dev->cmd_work);
2406 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2409 struct sk_buff *skb;
2410 struct sk_buff *resp;
2414 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2416 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2418 skb = pn533_alloc_skb(skb_len);
2422 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2423 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2425 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2427 return PTR_ERR(resp);
2429 dev_kfree_skb(resp);
2433 static int pn533_get_firmware_version(struct pn533 *dev,
2434 struct pn533_fw_version *fv)
2436 struct sk_buff *skb;
2437 struct sk_buff *resp;
2439 skb = pn533_alloc_skb(0);
2443 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2445 return PTR_ERR(resp);
2447 fv->ic = resp->data[0];
2448 fv->ver = resp->data[1];
2449 fv->rev = resp->data[2];
2450 fv->support = resp->data[3];
2452 dev_kfree_skb(resp);
2456 static int pn533_fw_reset(struct pn533 *dev)
2458 struct sk_buff *skb;
2459 struct sk_buff *resp;
2461 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2463 skb = pn533_alloc_skb(sizeof(u8));
2467 *skb_put(skb, sizeof(u8)) = 0x1;
2469 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2471 return PTR_ERR(resp);
2473 dev_kfree_skb(resp);
2478 static struct nfc_ops pn533_nfc_ops = {
2481 .dep_link_up = pn533_dep_link_up,
2482 .dep_link_down = pn533_dep_link_down,
2483 .start_poll = pn533_start_poll,
2484 .stop_poll = pn533_stop_poll,
2485 .activate_target = pn533_activate_target,
2486 .deactivate_target = pn533_deactivate_target,
2487 .im_transceive = pn533_transceive,
2488 .tm_send = pn533_tm_send,
2491 static int pn533_setup(struct pn533 *dev)
2493 struct pn533_config_max_retries max_retries;
2494 struct pn533_config_timing timing;
2495 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2498 switch (dev->device_type) {
2499 case PN533_DEVICE_STD:
2500 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2501 max_retries.mx_rty_psl = 2;
2502 max_retries.mx_rty_passive_act =
2503 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2505 timing.rfu = PN533_CONFIG_TIMING_102;
2506 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2507 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2511 case PN533_DEVICE_PASORI:
2512 max_retries.mx_rty_atr = 0x2;
2513 max_retries.mx_rty_psl = 0x1;
2514 max_retries.mx_rty_passive_act =
2515 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2517 timing.rfu = PN533_CONFIG_TIMING_102;
2518 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2519 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2524 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2529 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2530 (u8 *)&max_retries, sizeof(max_retries));
2532 nfc_dev_err(&dev->interface->dev,
2533 "Error on setting MAX_RETRIES config");
2538 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2539 (u8 *)&timing, sizeof(timing));
2541 nfc_dev_err(&dev->interface->dev,
2542 "Error on setting RF timings");
2546 switch (dev->device_type) {
2547 case PN533_DEVICE_STD:
2550 case PN533_DEVICE_PASORI:
2551 pn533_fw_reset(dev);
2553 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2556 nfc_dev_err(&dev->interface->dev,
2557 "Error while settings PASORI config");
2561 pn533_fw_reset(dev);
2569 static int pn533_probe(struct usb_interface *interface,
2570 const struct usb_device_id *id)
2572 struct pn533_fw_version fw_ver;
2574 struct usb_host_interface *iface_desc;
2575 struct usb_endpoint_descriptor *endpoint;
2576 int in_endpoint = 0;
2577 int out_endpoint = 0;
2582 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2586 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2587 dev->interface = interface;
2588 mutex_init(&dev->cmd_lock);
2590 iface_desc = interface->cur_altsetting;
2591 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2592 endpoint = &iface_desc->endpoint[i].desc;
2594 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2595 in_endpoint = endpoint->bEndpointAddress;
2597 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2598 out_endpoint = endpoint->bEndpointAddress;
2601 if (!in_endpoint || !out_endpoint) {
2602 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2603 " bulk-out endpoint");
2608 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2609 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2610 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2611 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2613 if (!dev->in_frame || !dev->out_frame ||
2614 !dev->in_urb || !dev->out_urb)
2617 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2618 usb_rcvbulkpipe(dev->udev, in_endpoint),
2619 NULL, 0, NULL, dev);
2620 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2621 usb_sndbulkpipe(dev->udev, out_endpoint),
2623 pn533_send_complete, dev);
2625 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2626 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2627 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2628 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2629 INIT_WORK(&dev->poll_work, pn533_wq_poll);
2630 dev->wq = alloc_ordered_workqueue("pn533", 0);
2631 if (dev->wq == NULL)
2634 init_timer(&dev->listen_timer);
2635 dev->listen_timer.data = (unsigned long) dev;
2636 dev->listen_timer.function = pn533_listen_mode_timer;
2638 skb_queue_head_init(&dev->resp_q);
2640 INIT_LIST_HEAD(&dev->cmd_queue);
2642 usb_set_intfdata(interface, dev);
2644 memset(&fw_ver, 0, sizeof(fw_ver));
2645 rc = pn533_get_firmware_version(dev, &fw_ver);
2649 nfc_dev_info(&dev->interface->dev,
2650 "NXP PN533 firmware ver %d.%d now attached",
2651 fw_ver.ver, fw_ver.rev);
2653 dev->device_type = id->driver_info;
2654 switch (dev->device_type) {
2655 case PN533_DEVICE_STD:
2656 protocols = PN533_ALL_PROTOCOLS;
2659 case PN533_DEVICE_PASORI:
2660 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2664 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2670 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2671 PN533_FRAME_HEADER_LEN +
2672 PN533_CMD_DATAEXCH_HEAD_LEN,
2673 PN533_FRAME_TAIL_LEN);
2677 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2678 nfc_set_drvdata(dev->nfc_dev, dev);
2680 rc = nfc_register_device(dev->nfc_dev);
2684 rc = pn533_setup(dev);
2686 goto unregister_nfc_dev;
2691 nfc_unregister_device(dev->nfc_dev);
2694 nfc_free_device(dev->nfc_dev);
2697 destroy_workqueue(dev->wq);
2699 kfree(dev->in_frame);
2700 usb_free_urb(dev->in_urb);
2701 kfree(dev->out_frame);
2702 usb_free_urb(dev->out_urb);
2707 static void pn533_disconnect(struct usb_interface *interface)
2710 struct pn533_cmd *cmd, *n;
2712 dev = usb_get_intfdata(interface);
2713 usb_set_intfdata(interface, NULL);
2715 nfc_unregister_device(dev->nfc_dev);
2716 nfc_free_device(dev->nfc_dev);
2718 usb_kill_urb(dev->in_urb);
2719 usb_kill_urb(dev->out_urb);
2721 destroy_workqueue(dev->wq);
2723 skb_queue_purge(&dev->resp_q);
2725 del_timer(&dev->listen_timer);
2727 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2728 list_del(&cmd->queue);
2732 kfree(dev->in_frame);
2733 usb_free_urb(dev->in_urb);
2734 kfree(dev->out_frame);
2735 usb_free_urb(dev->out_urb);
2738 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2741 static struct usb_driver pn533_driver = {
2743 .probe = pn533_probe,
2744 .disconnect = pn533_disconnect,
2745 .id_table = pn533_table,
2748 module_usb_driver(pn533_driver);
2750 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2751 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2752 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2753 MODULE_VERSION(VERSION);
2754 MODULE_LICENSE("GPL");