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_QUIRKS_TYPE_A BIT(0)
45 #define PN533_QUIRKS_TYPE_F BIT(1)
46 #define PN533_QUIRKS_DEP BIT(2)
47 #define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
49 #define PN533_DEVICE_STD 0x1
50 #define PN533_DEVICE_PASORI 0x2
52 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
53 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
54 NFC_PROTO_NFC_DEP_MASK |\
55 NFC_PROTO_ISO14443_B_MASK)
57 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
58 NFC_PROTO_MIFARE_MASK | \
59 NFC_PROTO_FELICA_MASK | \
60 NFC_PROTO_ISO14443_MASK | \
61 NFC_PROTO_NFC_DEP_MASK)
63 static const struct usb_device_id pn533_table
[] = {
64 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
65 .idVendor
= PN533_VENDOR_ID
,
66 .idProduct
= PN533_PRODUCT_ID
,
67 .driver_info
= PN533_DEVICE_STD
,
69 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
70 .idVendor
= SCM_VENDOR_ID
,
71 .idProduct
= SCL3711_PRODUCT_ID
,
72 .driver_info
= PN533_DEVICE_STD
,
74 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
75 .idVendor
= SONY_VENDOR_ID
,
76 .idProduct
= PASORI_PRODUCT_ID
,
77 .driver_info
= PN533_DEVICE_PASORI
,
81 MODULE_DEVICE_TABLE(usb
, pn533_table
);
83 /* How much time we spend listening for initiators */
84 #define PN533_LISTEN_TIME 2
86 /* frame definitions */
87 #define PN533_FRAME_TAIL_SIZE 2
88 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
89 PN533_FRAME_TAIL_SIZE)
90 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
91 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
92 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
95 #define PN533_SOF 0x00FF
97 /* frame identifier: in/out/error */
98 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
99 #define PN533_DIR_OUT 0xD4
100 #define PN533_DIR_IN 0xD5
103 #define PN533_FRAME_CMD(f) (f->data[1])
104 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
105 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
107 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
108 #define PN533_CMD_RF_CONFIGURATION 0x32
109 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
110 #define PN533_CMD_IN_COMM_THRU 0x42
111 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
112 #define PN533_CMD_IN_ATR 0x50
113 #define PN533_CMD_IN_RELEASE 0x52
114 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
116 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
117 #define PN533_CMD_TG_GET_DATA 0x86
118 #define PN533_CMD_TG_SET_DATA 0x8e
120 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
122 /* PN533 Return codes */
123 #define PN533_CMD_RET_MASK 0x3F
124 #define PN533_CMD_MI_MASK 0x40
125 #define PN533_CMD_RET_SUCCESS 0x00
127 /* PN533 status codes */
128 #define PN533_STATUS_TARGET_RELEASED 0x29
132 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
133 u8
*params
, int params_len
);
135 /* structs for pn533 commands */
137 /* PN533_CMD_GET_FIRMWARE_VERSION */
138 struct pn533_fw_version
{
145 /* PN533_CMD_RF_CONFIGURATION */
146 #define PN533_CFGITEM_TIMING 0x02
147 #define PN533_CFGITEM_MAX_RETRIES 0x05
148 #define PN533_CFGITEM_PASORI 0x82
150 #define PN533_CONFIG_TIMING_102 0xb
151 #define PN533_CONFIG_TIMING_204 0xc
152 #define PN533_CONFIG_TIMING_409 0xd
153 #define PN533_CONFIG_TIMING_819 0xe
155 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
156 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
158 struct pn533_config_max_retries
{
161 u8 mx_rty_passive_act
;
164 struct pn533_config_timing
{
170 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
172 /* felica commands opcode */
173 #define PN533_FELICA_OPC_SENSF_REQ 0
174 #define PN533_FELICA_OPC_SENSF_RES 1
175 /* felica SENSF_REQ parameters */
176 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
177 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
178 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
179 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
181 /* type B initiator_data values */
182 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
183 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
184 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
186 union pn533_cmd_poll_initdata
{
199 /* Poll modulations */
201 PN533_POLL_MOD_106KBPS_A
,
202 PN533_POLL_MOD_212KBPS_FELICA
,
203 PN533_POLL_MOD_424KBPS_FELICA
,
204 PN533_POLL_MOD_106KBPS_JEWEL
,
205 PN533_POLL_MOD_847KBPS_B
,
208 __PN533_POLL_MOD_AFTER_LAST
,
210 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
212 struct pn533_poll_modulations
{
216 union pn533_cmd_poll_initdata initiator_data
;
221 const struct pn533_poll_modulations poll_mod
[] = {
222 [PN533_POLL_MOD_106KBPS_A
] = {
229 [PN533_POLL_MOD_212KBPS_FELICA
] = {
233 .initiator_data
.felica
= {
234 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
235 .sc
= PN533_FELICA_SENSF_SC_ALL
,
236 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
242 [PN533_POLL_MOD_424KBPS_FELICA
] = {
246 .initiator_data
.felica
= {
247 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
248 .sc
= PN533_FELICA_SENSF_SC_ALL
,
249 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
255 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
262 [PN533_POLL_MOD_847KBPS_B
] = {
266 .initiator_data
.type_b
= {
267 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
269 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
274 [PN533_LISTEN_MOD
] = {
279 /* PN533_CMD_IN_ATR */
281 struct pn533_cmd_activate_param
{
286 struct pn533_cmd_activate_response
{
298 /* PN533_CMD_IN_JUMP_FOR_DEP */
299 struct pn533_cmd_jump_dep
{
306 struct pn533_cmd_jump_dep_response
{
320 /* PN533_TG_INIT_AS_TARGET */
321 #define PN533_INIT_TARGET_PASSIVE 0x1
322 #define PN533_INIT_TARGET_DEP 0x2
324 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
325 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
326 #define PN533_INIT_TARGET_RESP_DEP 0x4
328 struct pn533_cmd_init_target
{
337 struct pn533_cmd_init_target_response
{
343 struct usb_device
*udev
;
344 struct usb_interface
*interface
;
345 struct nfc_dev
*nfc_dev
;
349 struct pn533_frame
*out_frame
;
353 struct pn533_frame
*in_frame
;
355 struct sk_buff_head resp_q
;
357 struct workqueue_struct
*wq
;
358 struct work_struct cmd_work
;
359 struct work_struct poll_work
;
360 struct work_struct mi_work
;
361 struct work_struct tg_work
;
362 struct timer_list listen_timer
;
363 struct pn533_frame
*wq_in_frame
;
367 pn533_cmd_complete_t cmd_complete
;
368 void *cmd_complete_arg
;
369 struct mutex cmd_lock
;
372 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
376 u32 listen_protocols
;
381 u8 tgt_available_prots
;
396 /* The rule: value + checksum = 0 */
397 static inline u8
pn533_checksum(u8 value
)
402 /* The rule: sum(data elements) + checksum = 0 */
403 static u8
pn533_data_checksum(u8
*data
, int datalen
)
408 for (i
= 0; i
< datalen
; i
++)
411 return pn533_checksum(sum
);
415 * pn533_tx_frame_ack - create a ack frame
416 * @frame: The frame to be set as ack
418 * Ack is different type of standard frame. As a standard frame, it has
419 * preamble and start_frame. However the checksum of this frame must fail,
420 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
421 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
422 * After datalen_checksum field, the postamble is placed.
424 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
427 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
429 frame
->datalen_checksum
= 0xFF;
430 /* data[0] is used as postamble */
434 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
437 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
438 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
439 PN533_FRAME_CMD(frame
) = cmd
;
443 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
445 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
447 PN533_FRAME_CHECKSUM(frame
) =
448 pn533_data_checksum(frame
->data
, frame
->datalen
);
450 PN533_FRAME_POSTAMBLE(frame
) = 0;
453 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
457 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
460 checksum
= pn533_checksum(frame
->datalen
);
461 if (checksum
!= frame
->datalen_checksum
)
464 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
465 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
471 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
473 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
476 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
482 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
484 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
488 static void pn533_wq_cmd_complete(struct work_struct
*work
)
490 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
491 struct pn533_frame
*in_frame
;
494 in_frame
= dev
->wq_in_frame
;
496 if (dev
->wq_in_error
)
497 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
500 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
501 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
502 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
504 if (rc
!= -EINPROGRESS
)
505 mutex_unlock(&dev
->cmd_lock
);
508 static void pn533_recv_response(struct urb
*urb
)
510 struct pn533
*dev
= urb
->context
;
511 struct pn533_frame
*in_frame
;
513 dev
->wq_in_frame
= NULL
;
515 switch (urb
->status
) {
522 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
523 " status: %d", urb
->status
);
524 dev
->wq_in_error
= urb
->status
;
527 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
529 dev
->wq_in_error
= urb
->status
;
533 in_frame
= dev
->in_urb
->transfer_buffer
;
535 if (!pn533_rx_frame_is_valid(in_frame
)) {
536 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
537 dev
->wq_in_error
= -EIO
;
541 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
542 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
543 "response to the last command");
544 dev
->wq_in_error
= -EIO
;
548 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
549 dev
->wq_in_error
= 0;
550 dev
->wq_in_frame
= in_frame
;
553 queue_work(dev
->wq
, &dev
->cmd_work
);
556 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
558 dev
->in_urb
->complete
= pn533_recv_response
;
560 return usb_submit_urb(dev
->in_urb
, flags
);
563 static void pn533_recv_ack(struct urb
*urb
)
565 struct pn533
*dev
= urb
->context
;
566 struct pn533_frame
*in_frame
;
569 switch (urb
->status
) {
576 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
577 " status: %d", urb
->status
);
578 dev
->wq_in_error
= urb
->status
;
581 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
583 dev
->wq_in_error
= urb
->status
;
587 in_frame
= dev
->in_urb
->transfer_buffer
;
589 if (!pn533_rx_frame_is_ack(in_frame
)) {
590 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
591 dev
->wq_in_error
= -EIO
;
595 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
597 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
599 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
601 dev
->wq_in_error
= rc
;
608 dev
->wq_in_frame
= NULL
;
609 queue_work(dev
->wq
, &dev
->cmd_work
);
612 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
614 dev
->in_urb
->complete
= pn533_recv_ack
;
616 return usb_submit_urb(dev
->in_urb
, flags
);
619 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
623 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
625 pn533_tx_frame_ack(dev
->out_frame
);
627 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
628 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
629 rc
= usb_submit_urb(dev
->out_urb
, flags
);
634 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
635 struct pn533_frame
*out_frame
,
636 struct pn533_frame
*in_frame
,
638 pn533_cmd_complete_t cmd_complete
,
639 void *arg
, gfp_t flags
)
643 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
644 PN533_FRAME_CMD(out_frame
));
646 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
647 dev
->cmd_complete
= cmd_complete
;
648 dev
->cmd_complete_arg
= arg
;
650 dev
->out_urb
->transfer_buffer
= out_frame
;
651 dev
->out_urb
->transfer_buffer_length
=
652 PN533_FRAME_SIZE(out_frame
);
654 dev
->in_urb
->transfer_buffer
= in_frame
;
655 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
657 rc
= usb_submit_urb(dev
->out_urb
, flags
);
661 rc
= pn533_submit_urb_for_ack(dev
, flags
);
668 usb_unlink_urb(dev
->out_urb
);
672 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
673 struct pn533_frame
*out_frame
,
674 struct pn533_frame
*in_frame
,
676 pn533_cmd_complete_t cmd_complete
,
677 void *arg
, gfp_t flags
)
681 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
683 if (!mutex_trylock(&dev
->cmd_lock
))
686 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
687 in_frame_len
, cmd_complete
, arg
, flags
);
693 mutex_unlock(&dev
->cmd_lock
);
697 struct pn533_sync_cmd_response
{
699 struct completion done
;
702 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
703 u8
*params
, int params_len
)
705 struct pn533_sync_cmd_response
*arg
= _arg
;
707 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
711 if (params_len
< 0) /* error */
712 arg
->rc
= params_len
;
714 complete(&arg
->done
);
719 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
720 struct pn533_frame
*out_frame
,
721 struct pn533_frame
*in_frame
,
725 struct pn533_sync_cmd_response arg
;
727 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
729 init_completion(&arg
.done
);
731 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
732 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
736 wait_for_completion(&arg
.done
);
741 static void pn533_send_complete(struct urb
*urb
)
743 struct pn533
*dev
= urb
->context
;
745 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
747 switch (urb
->status
) {
754 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
755 " status: %d", urb
->status
);
758 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
763 struct pn533_target_type_a
{
771 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
772 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
773 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
775 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
776 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
778 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
779 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
781 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
782 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
783 #define PN533_TYPE_A_SEL_PROT_DEP 2
784 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
786 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
792 if (target_data_len
< sizeof(struct pn533_target_type_a
))
795 /* The lenght check of nfcid[] and ats[] are not being performed because
796 the values are not being used */
798 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
799 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
800 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
802 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
803 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
804 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
805 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
808 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
809 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
815 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
818 struct pn533_target_type_a
*tgt_type_a
;
820 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
822 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
825 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
826 case PN533_TYPE_A_SEL_PROT_MIFARE
:
827 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
829 case PN533_TYPE_A_SEL_PROT_ISO14443
:
830 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
832 case PN533_TYPE_A_SEL_PROT_DEP
:
833 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
835 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
836 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
837 NFC_PROTO_NFC_DEP_MASK
;
841 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
842 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
843 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
844 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
849 struct pn533_target_felica
{
858 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
859 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
861 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
864 if (target_data_len
< sizeof(struct pn533_target_felica
))
867 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
873 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
876 struct pn533_target_felica
*tgt_felica
;
878 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
880 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
883 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
884 tgt_felica
->nfcid2
[1] ==
885 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
886 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
888 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
890 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
891 nfc_tgt
->sensf_res_len
= 9;
896 struct pn533_target_jewel
{
901 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
907 if (target_data_len
< sizeof(struct pn533_target_jewel
))
910 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
911 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
912 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
914 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
915 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
916 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
917 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
923 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
926 struct pn533_target_jewel
*tgt_jewel
;
928 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
930 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
933 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
934 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
935 nfc_tgt
->nfcid1_len
= 4;
936 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
941 struct pn533_type_b_prot_info
{
947 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
948 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
949 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
951 struct pn533_type_b_sens_res
{
955 struct pn533_type_b_prot_info prot_info
;
958 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
960 struct pn533_target_type_b
{
961 struct pn533_type_b_sens_res sensb_res
;
966 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
969 if (target_data_len
< sizeof(struct pn533_target_type_b
))
972 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
975 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
976 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
982 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
985 struct pn533_target_type_b
*tgt_type_b
;
987 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
989 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
992 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
997 struct pn533_poll_response
{
1003 static int pn533_target_found(struct pn533
*dev
,
1004 struct pn533_poll_response
*resp
, int resp_len
)
1006 int target_data_len
;
1007 struct nfc_target nfc_tgt
;
1010 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
1011 dev
->poll_mod_curr
);
1016 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1018 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
1020 switch (dev
->poll_mod_curr
) {
1021 case PN533_POLL_MOD_106KBPS_A
:
1022 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
1025 case PN533_POLL_MOD_212KBPS_FELICA
:
1026 case PN533_POLL_MOD_424KBPS_FELICA
:
1027 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
1030 case PN533_POLL_MOD_106KBPS_JEWEL
:
1031 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
1034 case PN533_POLL_MOD_847KBPS_B
:
1035 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
1039 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
1047 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1048 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
1049 " have the desired protocol");
1053 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
1054 "0x%x", nfc_tgt
.supported_protocols
);
1056 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1058 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1063 static inline void pn533_poll_next_mod(struct pn533
*dev
)
1065 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1068 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
1070 dev
->poll_mod_count
= 0;
1073 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
1075 dev
->poll_mod_active
[dev
->poll_mod_count
] =
1076 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
1077 dev
->poll_mod_count
++;
1080 static void pn533_poll_create_mod_list(struct pn533
*dev
,
1081 u32 im_protocols
, u32 tm_protocols
)
1083 pn533_poll_reset_mod_list(dev
);
1085 if (im_protocols
& NFC_PROTO_MIFARE_MASK
1086 || im_protocols
& NFC_PROTO_ISO14443_MASK
1087 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
)
1088 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1090 if (im_protocols
& NFC_PROTO_FELICA_MASK
1091 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1092 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1093 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1096 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1097 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1099 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1100 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1103 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
1106 static int pn533_start_poll_complete(struct pn533
*dev
, void *arg
,
1107 u8
*params
, int params_len
)
1109 struct pn533_poll_response
*resp
;
1112 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1114 resp
= (struct pn533_poll_response
*) params
;
1116 rc
= pn533_target_found(dev
, resp
, params_len
);
1118 /* We must stop the poll after a valid target found */
1120 pn533_poll_reset_mod_list(dev
);
1128 static int pn533_init_target_frame(struct pn533_frame
*frame
,
1129 u8
*gb
, size_t gb_len
)
1131 struct pn533_cmd_init_target
*cmd
;
1133 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1134 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1135 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1136 0xff, 0xff}; /* System code */
1137 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1139 0x40}; /* SEL_RES for DEP */
1141 cmd_len
= sizeof(struct pn533_cmd_init_target
) + gb_len
+ 1;
1142 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1146 pn533_tx_frame_init(frame
, PN533_CMD_TG_INIT_AS_TARGET
);
1148 /* DEP support only */
1149 cmd
->mode
|= PN533_INIT_TARGET_DEP
;
1152 memcpy(cmd
->felica
, felica_params
, 18);
1153 get_random_bytes(cmd
->felica
+ 2, 6);
1156 memset(cmd
->nfcid3
, 0, 10);
1157 memcpy(cmd
->nfcid3
, cmd
->felica
, 8);
1160 memcpy(cmd
->mifare
, mifare_params
, 6);
1163 cmd
->gb_len
= gb_len
;
1164 memcpy(cmd
->gb
, gb
, gb_len
);
1167 cmd
->gb
[gb_len
] = 0;
1169 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), cmd
, cmd_len
);
1171 frame
->datalen
+= cmd_len
;
1173 pn533_tx_frame_finish(frame
);
1180 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1181 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1182 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1183 u8
*params
, int params_len
)
1185 struct sk_buff
*skb_resp
= arg
;
1186 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1188 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1190 if (params_len
< 0) {
1191 nfc_dev_err(&dev
->interface
->dev
,
1192 "Error %d when starting as a target",
1198 if (params_len
> 0 && params
[0] != 0) {
1199 nfc_tm_deactivated(dev
->nfc_dev
);
1203 kfree_skb(skb_resp
);
1207 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1208 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1209 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1211 return nfc_tm_data_received(dev
->nfc_dev
, skb_resp
);
1214 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1216 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1217 struct pn533_frame
*in_frame
;
1218 struct sk_buff
*skb_resp
;
1219 size_t skb_resp_len
;
1221 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1223 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1224 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1225 PN533_FRAME_TAIL_SIZE
;
1227 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1231 in_frame
= (struct pn533_frame
*)skb_resp
->data
;
1233 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_TG_GET_DATA
);
1234 pn533_tx_frame_finish(dev
->out_frame
);
1236 pn533_send_cmd_frame_async(dev
, dev
->out_frame
, in_frame
,
1238 pn533_tm_get_data_complete
,
1239 skb_resp
, GFP_KERNEL
);
1244 #define ATR_REQ_GB_OFFSET 17
1245 static int pn533_init_target_complete(struct pn533
*dev
, void *arg
,
1246 u8
*params
, int params_len
)
1248 struct pn533_cmd_init_target_response
*resp
;
1249 u8 frame
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1253 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1255 if (params_len
< 0) {
1256 nfc_dev_err(&dev
->interface
->dev
,
1257 "Error %d when starting as a target",
1263 if (params_len
< ATR_REQ_GB_OFFSET
+ 1)
1266 resp
= (struct pn533_cmd_init_target_response
*) params
;
1268 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x param len %d\n",
1269 resp
->mode
, params_len
);
1271 frame
= resp
->mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
;
1272 if (frame
== PN533_INIT_TARGET_RESP_ACTIVE
)
1273 comm_mode
= NFC_COMM_ACTIVE
;
1275 /* Again, only DEP */
1276 if ((resp
->mode
& PN533_INIT_TARGET_RESP_DEP
) == 0)
1279 gb
= resp
->cmd
+ ATR_REQ_GB_OFFSET
;
1280 gb_len
= params_len
- (ATR_REQ_GB_OFFSET
+ 1);
1282 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1283 comm_mode
, gb
, gb_len
);
1285 nfc_dev_err(&dev
->interface
->dev
,
1286 "Error when signaling target activation");
1292 queue_work(dev
->wq
, &dev
->tg_work
);
1297 static void pn533_listen_mode_timer(unsigned long data
)
1299 struct pn533
*dev
= (struct pn533
*) data
;
1301 nfc_dev_dbg(&dev
->interface
->dev
, "Listen mode timeout");
1303 /* An ack will cancel the last issued command (poll) */
1304 pn533_send_ack(dev
, GFP_ATOMIC
);
1306 dev
->cancel_listen
= 1;
1308 mutex_unlock(&dev
->cmd_lock
);
1310 pn533_poll_next_mod(dev
);
1312 queue_work(dev
->wq
, &dev
->poll_work
);
1315 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1316 u8
*params
, int params_len
)
1318 struct pn533_poll_modulations
*cur_mod
;
1321 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1323 if (params_len
== -ENOENT
) {
1324 if (dev
->poll_mod_count
!= 0)
1327 nfc_dev_err(&dev
->interface
->dev
,
1328 "Polling operation has been stopped");
1333 if (params_len
< 0) {
1334 nfc_dev_err(&dev
->interface
->dev
,
1335 "Error %d when running poll", params_len
);
1340 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1342 if (cur_mod
->len
== 0) {
1343 del_timer(&dev
->listen_timer
);
1345 return pn533_init_target_complete(dev
, arg
, params
, params_len
);
1347 rc
= pn533_start_poll_complete(dev
, arg
, params
, params_len
);
1352 pn533_poll_next_mod(dev
);
1354 queue_work(dev
->wq
, &dev
->poll_work
);
1359 pn533_poll_reset_mod_list(dev
);
1360 dev
->poll_protocols
= 0;
1364 static void pn533_build_poll_frame(struct pn533
*dev
,
1365 struct pn533_frame
*frame
,
1366 struct pn533_poll_modulations
*mod
)
1368 nfc_dev_dbg(&dev
->interface
->dev
, "mod len %d\n", mod
->len
);
1370 if (mod
->len
== 0) {
1372 pn533_init_target_frame(frame
, dev
->gb
, dev
->gb_len
);
1375 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
1377 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1378 frame
->datalen
+= mod
->len
;
1380 pn533_tx_frame_finish(frame
);
1384 static int pn533_send_poll_frame(struct pn533
*dev
)
1386 struct pn533_poll_modulations
*cur_mod
;
1389 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1391 pn533_build_poll_frame(dev
, dev
->out_frame
, cur_mod
);
1393 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1394 dev
->in_maxlen
, pn533_poll_complete
,
1397 nfc_dev_err(&dev
->interface
->dev
, "Polling loop error %d", rc
);
1402 static void pn533_wq_poll(struct work_struct
*work
)
1404 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
);
1405 struct pn533_poll_modulations
*cur_mod
;
1408 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1410 nfc_dev_dbg(&dev
->interface
->dev
,
1411 "%s cancel_listen %d modulation len %d",
1412 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1414 if (dev
->cancel_listen
== 1) {
1415 dev
->cancel_listen
= 0;
1416 usb_kill_urb(dev
->in_urb
);
1419 rc
= pn533_send_poll_frame(dev
);
1423 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1424 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1429 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1430 u32 im_protocols
, u32 tm_protocols
)
1432 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1434 nfc_dev_dbg(&dev
->interface
->dev
,
1435 "%s: im protocols 0x%x tm protocols 0x%x",
1436 __func__
, im_protocols
, tm_protocols
);
1438 if (dev
->tgt_active_prot
) {
1439 nfc_dev_err(&dev
->interface
->dev
,
1440 "Cannot poll with a target already activated");
1444 if (dev
->tgt_mode
) {
1445 nfc_dev_err(&dev
->interface
->dev
,
1446 "Cannot poll while already being activated");
1451 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1452 if (dev
->gb
== NULL
)
1456 dev
->poll_mod_curr
= 0;
1457 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1458 dev
->poll_protocols
= im_protocols
;
1459 dev
->listen_protocols
= tm_protocols
;
1461 return pn533_send_poll_frame(dev
);
1464 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1466 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1468 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1470 del_timer(&dev
->listen_timer
);
1472 if (!dev
->poll_mod_count
) {
1473 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1478 /* An ack will cancel the last issued command (poll) */
1479 pn533_send_ack(dev
, GFP_KERNEL
);
1481 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1482 usb_kill_urb(dev
->in_urb
);
1484 pn533_poll_reset_mod_list(dev
);
1487 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1489 struct pn533_cmd_activate_param param
;
1490 struct pn533_cmd_activate_response
*resp
;
1494 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1496 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1500 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1501 sizeof(struct pn533_cmd_activate_param
));
1502 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1504 pn533_tx_frame_finish(dev
->out_frame
);
1506 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1511 resp
= (struct pn533_cmd_activate_response
*)
1512 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1513 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1514 if (rc
!= PN533_CMD_RET_SUCCESS
)
1517 /* ATR_RES general bytes are located at offset 16 */
1518 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1519 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1524 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1525 struct nfc_target
*target
, u32 protocol
)
1527 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1530 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1533 if (dev
->poll_mod_count
) {
1534 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1539 if (dev
->tgt_active_prot
) {
1540 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1545 if (!dev
->tgt_available_prots
) {
1546 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1551 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1552 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1553 " the requested protocol %u", protocol
);
1557 if (protocol
== NFC_PROTO_NFC_DEP
) {
1558 rc
= pn533_activate_target_nfcdep(dev
);
1560 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1561 " activating target with"
1562 " NFC_DEP protocol", rc
);
1567 dev
->tgt_active_prot
= protocol
;
1568 dev
->tgt_available_prots
= 0;
1573 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1574 struct nfc_target
*target
)
1576 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1581 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1583 if (!dev
->tgt_active_prot
) {
1584 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1588 dev
->tgt_active_prot
= 0;
1590 skb_queue_purge(&dev
->resp_q
);
1592 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1595 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1596 dev
->out_frame
->datalen
+= sizeof(u8
);
1598 pn533_tx_frame_finish(dev
->out_frame
);
1600 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1603 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1604 " command to the controller");
1608 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1609 rc
= status
& PN533_CMD_RET_MASK
;
1610 if (rc
!= PN533_CMD_RET_SUCCESS
)
1611 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1618 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1619 u8
*params
, int params_len
)
1621 struct pn533_cmd_jump_dep
*cmd
;
1622 struct pn533_cmd_jump_dep_response
*resp
;
1623 struct nfc_target nfc_target
;
1627 if (params_len
== -ENOENT
) {
1628 nfc_dev_dbg(&dev
->interface
->dev
, "");
1632 if (params_len
< 0) {
1633 nfc_dev_err(&dev
->interface
->dev
,
1634 "Error %d when bringing DEP link up",
1639 if (dev
->tgt_available_prots
&&
1640 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1641 nfc_dev_err(&dev
->interface
->dev
,
1642 "The target does not support DEP");
1646 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1647 cmd
= (struct pn533_cmd_jump_dep
*) arg
;
1648 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1649 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1650 nfc_dev_err(&dev
->interface
->dev
,
1651 "Bringing DEP link up failed %d", rc
);
1655 if (!dev
->tgt_available_prots
) {
1656 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1658 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1659 nfc_target
.nfcid1_len
= 10;
1660 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1661 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1665 dev
->tgt_available_prots
= 0;
1668 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1670 /* ATR_RES general bytes are located at offset 17 */
1671 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1672 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1673 resp
->gt
, target_gt_len
);
1675 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1676 dev
->nfc_dev
->targets
[0].idx
,
1677 !cmd
->active
, NFC_RF_INITIATOR
);
1682 static int pn533_mod_to_baud(struct pn533
*dev
)
1684 switch (dev
->poll_mod_curr
) {
1685 case PN533_POLL_MOD_106KBPS_A
:
1687 case PN533_POLL_MOD_212KBPS_FELICA
:
1689 case PN533_POLL_MOD_424KBPS_FELICA
:
1696 #define PASSIVE_DATA_LEN 5
1697 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1698 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1700 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1701 struct pn533_cmd_jump_dep
*cmd
;
1702 u8 cmd_len
, *data_ptr
;
1703 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1706 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1708 if (dev
->poll_mod_count
) {
1709 nfc_dev_err(&dev
->interface
->dev
,
1710 "Cannot bring the DEP link up while polling");
1714 if (dev
->tgt_active_prot
) {
1715 nfc_dev_err(&dev
->interface
->dev
,
1716 "There is already an active target");
1720 baud
= pn533_mod_to_baud(dev
);
1722 nfc_dev_err(&dev
->interface
->dev
,
1723 "Invalid curr modulation %d", dev
->poll_mod_curr
);
1727 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1728 if (comm_mode
== NFC_COMM_PASSIVE
)
1729 cmd_len
+= PASSIVE_DATA_LEN
;
1731 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1735 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1737 cmd
->active
= !comm_mode
;
1740 data_ptr
= cmd
->data
;
1741 if (comm_mode
== NFC_COMM_PASSIVE
&& cmd
->baud
> 0) {
1742 memcpy(data_ptr
, passive_data
, PASSIVE_DATA_LEN
);
1744 data_ptr
+= PASSIVE_DATA_LEN
;
1747 if (gb
!= NULL
&& gb_len
> 0) {
1748 cmd
->next
|= 4; /* We have some Gi */
1749 memcpy(data_ptr
, gb
, gb_len
);
1754 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1755 dev
->out_frame
->datalen
+= cmd_len
;
1757 pn533_tx_frame_finish(dev
->out_frame
);
1759 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1760 dev
->in_maxlen
, pn533_in_dep_link_up_complete
,
1772 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1774 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1776 pn533_poll_reset_mod_list(dev
);
1778 if (dev
->tgt_mode
|| dev
->tgt_active_prot
) {
1779 pn533_send_ack(dev
, GFP_KERNEL
);
1780 usb_kill_urb(dev
->in_urb
);
1783 dev
->tgt_active_prot
= 0;
1786 skb_queue_purge(&dev
->resp_q
);
1791 static int pn533_build_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
,
1794 int payload_len
= skb
->len
;
1795 struct pn533_frame
*out_frame
;
1798 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1801 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1802 /* TODO: Implement support to multi-part data exchange */
1803 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1805 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1809 if (target
== true) {
1810 switch (dev
->device_type
) {
1811 case PN533_DEVICE_PASORI
:
1812 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
1813 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
- 1);
1814 out_frame
= (struct pn533_frame
*) skb
->data
;
1815 pn533_tx_frame_init(out_frame
,
1816 PN533_CMD_IN_COMM_THRU
);
1822 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1823 out_frame
= (struct pn533_frame
*) skb
->data
;
1824 pn533_tx_frame_init(out_frame
,
1825 PN533_CMD_IN_DATA_EXCHANGE
);
1827 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
),
1829 out_frame
->datalen
+= sizeof(u8
);
1835 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
- 1);
1836 out_frame
= (struct pn533_frame
*) skb
->data
;
1837 pn533_tx_frame_init(out_frame
, PN533_CMD_TG_SET_DATA
);
1841 /* The data is already in the out_frame, just update the datalen */
1842 out_frame
->datalen
+= payload_len
;
1844 pn533_tx_frame_finish(out_frame
);
1845 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1850 struct pn533_data_exchange_arg
{
1851 struct sk_buff
*skb_resp
;
1852 struct sk_buff
*skb_out
;
1853 data_exchange_cb_t cb
;
1857 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1859 struct sk_buff
*skb
, *tmp
, *t
;
1860 unsigned int skb_len
= 0, tmp_len
= 0;
1862 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
1864 if (skb_queue_empty(&dev
->resp_q
))
1867 if (skb_queue_len(&dev
->resp_q
) == 1) {
1868 skb
= skb_dequeue(&dev
->resp_q
);
1872 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1873 skb_len
+= tmp
->len
;
1875 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1878 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
1882 skb_put(skb
, skb_len
);
1884 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
1885 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
1886 tmp_len
+= tmp
->len
;
1890 skb_queue_purge(&dev
->resp_q
);
1895 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1896 u8
*params
, int params_len
)
1898 struct pn533_data_exchange_arg
*arg
= _arg
;
1899 struct sk_buff
*skb
= NULL
, *skb_resp
= arg
->skb_resp
;
1900 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1905 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1907 dev_kfree_skb(arg
->skb_out
);
1909 if (params_len
< 0) { /* error */
1916 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1917 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1918 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1919 " exchanging data", cmd_ret
);
1924 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1925 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1926 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1927 skb_queue_tail(&dev
->resp_q
, skb_resp
);
1929 if (status
& PN533_CMD_MI_MASK
) {
1930 queue_work(dev
->wq
, &dev
->mi_work
);
1931 return -EINPROGRESS
;
1934 skb
= pn533_build_response(dev
);
1938 arg
->cb(arg
->cb_context
, skb
, 0);
1943 skb_queue_purge(&dev
->resp_q
);
1944 dev_kfree_skb(skb_resp
);
1945 arg
->cb(arg
->cb_context
, NULL
, err
);
1950 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
1951 struct nfc_target
*target
, struct sk_buff
*skb
,
1952 data_exchange_cb_t cb
, void *cb_context
)
1954 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1955 struct pn533_frame
*out_frame
, *in_frame
;
1956 struct pn533_data_exchange_arg
*arg
;
1957 struct sk_buff
*skb_resp
;
1961 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1963 if (!dev
->tgt_active_prot
) {
1964 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
1965 " there is no active target");
1970 rc
= pn533_build_tx_frame(dev
, skb
, true);
1974 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1975 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1976 PN533_FRAME_TAIL_SIZE
;
1978 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1984 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1985 out_frame
= (struct pn533_frame
*) skb
->data
;
1987 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
1993 arg
->skb_resp
= skb_resp
;
1996 arg
->cb_context
= cb_context
;
1998 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
1999 pn533_data_exchange_complete
, arg
,
2002 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
2003 " perform data_exchange", rc
);
2012 kfree_skb(skb_resp
);
2018 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2019 u8
*params
, int params_len
)
2021 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2023 if (params_len
< 0) {
2024 nfc_dev_err(&dev
->interface
->dev
,
2025 "Error %d when sending data",
2031 if (params_len
> 0 && params
[0] != 0) {
2032 nfc_tm_deactivated(dev
->nfc_dev
);
2039 queue_work(dev
->wq
, &dev
->tg_work
);
2044 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2046 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2047 struct pn533_frame
*out_frame
;
2050 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2052 rc
= pn533_build_tx_frame(dev
, skb
, false);
2056 out_frame
= (struct pn533_frame
*) skb
->data
;
2058 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, dev
->in_frame
,
2059 dev
->in_maxlen
, pn533_tm_send_complete
,
2062 nfc_dev_err(&dev
->interface
->dev
,
2063 "Error %d when trying to send data", rc
);
2075 static void pn533_wq_mi_recv(struct work_struct
*work
)
2077 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
2078 struct sk_buff
*skb_cmd
;
2079 struct pn533_data_exchange_arg
*arg
= dev
->cmd_complete_arg
;
2080 struct pn533_frame
*out_frame
, *in_frame
;
2081 struct sk_buff
*skb_resp
;
2085 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2087 /* This is a zero payload size skb */
2088 skb_cmd
= alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
+ PN533_FRAME_TAIL_SIZE
,
2090 if (skb_cmd
== NULL
)
2093 skb_reserve(skb_cmd
, PN533_CMD_DATAEXCH_HEAD_LEN
);
2095 rc
= pn533_build_tx_frame(dev
, skb_cmd
, true);
2099 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
2100 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
2101 PN533_FRAME_TAIL_SIZE
;
2102 skb_resp
= alloc_skb(skb_resp_len
, GFP_KERNEL
);
2108 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
2109 out_frame
= (struct pn533_frame
*) skb_cmd
->data
;
2111 arg
->skb_resp
= skb_resp
;
2112 arg
->skb_out
= skb_cmd
;
2114 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
2116 pn533_data_exchange_complete
,
2117 dev
->cmd_complete_arg
, GFP_KERNEL
);
2121 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
2122 " perform data_exchange", rc
);
2124 kfree_skb(skb_resp
);
2130 pn533_send_ack(dev
, GFP_KERNEL
);
2134 mutex_unlock(&dev
->cmd_lock
);
2137 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2143 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2145 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
2147 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
2148 params
[0] = cfgitem
;
2149 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
2150 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
2152 pn533_tx_frame_finish(dev
->out_frame
);
2154 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2160 static int pn533_fw_reset(struct pn533
*dev
)
2165 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2167 pn533_tx_frame_init(dev
->out_frame
, 0x18);
2169 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
2171 dev
->out_frame
->datalen
+= 1;
2173 pn533_tx_frame_finish(dev
->out_frame
);
2175 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2181 static struct nfc_ops pn533_nfc_ops
= {
2184 .dep_link_up
= pn533_dep_link_up
,
2185 .dep_link_down
= pn533_dep_link_down
,
2186 .start_poll
= pn533_start_poll
,
2187 .stop_poll
= pn533_stop_poll
,
2188 .activate_target
= pn533_activate_target
,
2189 .deactivate_target
= pn533_deactivate_target
,
2190 .im_transceive
= pn533_transceive
,
2191 .tm_send
= pn533_tm_send
,
2194 static int pn533_setup(struct pn533
*dev
)
2196 struct pn533_config_max_retries max_retries
;
2197 struct pn533_config_timing timing
;
2198 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2201 switch (dev
->device_type
) {
2202 case PN533_DEVICE_STD
:
2203 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
2204 max_retries
.mx_rty_psl
= 2;
2205 max_retries
.mx_rty_passive_act
=
2206 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2208 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2209 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_204
;
2210 timing
.dep_timeout
= PN533_CONFIG_TIMING_409
;
2214 case PN533_DEVICE_PASORI
:
2215 max_retries
.mx_rty_atr
= 0x2;
2216 max_retries
.mx_rty_psl
= 0x1;
2217 max_retries
.mx_rty_passive_act
=
2218 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2220 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2221 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2222 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2227 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2232 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2233 (u8
*)&max_retries
, sizeof(max_retries
));
2235 nfc_dev_err(&dev
->interface
->dev
,
2236 "Error on setting MAX_RETRIES config");
2241 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2242 (u8
*)&timing
, sizeof(timing
));
2244 nfc_dev_err(&dev
->interface
->dev
,
2245 "Error on setting RF timings");
2249 switch (dev
->device_type
) {
2250 case PN533_DEVICE_STD
:
2253 case PN533_DEVICE_PASORI
:
2254 pn533_fw_reset(dev
);
2256 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2259 nfc_dev_err(&dev
->interface
->dev
,
2260 "Error while settings PASORI config");
2264 pn533_fw_reset(dev
);
2272 static int pn533_probe(struct usb_interface
*interface
,
2273 const struct usb_device_id
*id
)
2275 struct pn533_fw_version
*fw_ver
;
2277 struct usb_host_interface
*iface_desc
;
2278 struct usb_endpoint_descriptor
*endpoint
;
2279 int in_endpoint
= 0;
2280 int out_endpoint
= 0;
2285 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2289 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2290 dev
->interface
= interface
;
2291 mutex_init(&dev
->cmd_lock
);
2293 iface_desc
= interface
->cur_altsetting
;
2294 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2295 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2297 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
2298 dev
->in_maxlen
= le16_to_cpu(endpoint
->wMaxPacketSize
);
2299 in_endpoint
= endpoint
->bEndpointAddress
;
2302 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
)) {
2304 le16_to_cpu(endpoint
->wMaxPacketSize
);
2305 out_endpoint
= endpoint
->bEndpointAddress
;
2309 if (!in_endpoint
|| !out_endpoint
) {
2310 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
2311 " bulk-out endpoint");
2316 dev
->in_frame
= kmalloc(dev
->in_maxlen
, GFP_KERNEL
);
2317 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2318 dev
->out_frame
= kmalloc(dev
->out_maxlen
, GFP_KERNEL
);
2319 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2321 if (!dev
->in_frame
|| !dev
->out_frame
||
2322 !dev
->in_urb
|| !dev
->out_urb
)
2325 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
2326 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
2327 NULL
, 0, NULL
, dev
);
2328 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
2329 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
2331 pn533_send_complete
, dev
);
2333 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd_complete
);
2334 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
2335 INIT_WORK(&dev
->tg_work
, pn533_wq_tg_get_data
);
2336 INIT_WORK(&dev
->poll_work
, pn533_wq_poll
);
2337 dev
->wq
= alloc_workqueue("pn533",
2338 WQ_NON_REENTRANT
| WQ_UNBOUND
| WQ_MEM_RECLAIM
,
2340 if (dev
->wq
== NULL
)
2343 init_timer(&dev
->listen_timer
);
2344 dev
->listen_timer
.data
= (unsigned long) dev
;
2345 dev
->listen_timer
.function
= pn533_listen_mode_timer
;
2347 skb_queue_head_init(&dev
->resp_q
);
2349 usb_set_intfdata(interface
, dev
);
2351 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
2352 pn533_tx_frame_finish(dev
->out_frame
);
2354 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2359 fw_ver
= (struct pn533_fw_version
*)
2360 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
2361 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
2362 " attached", fw_ver
->ver
, fw_ver
->rev
);
2364 dev
->device_type
= id
->driver_info
;
2365 switch (dev
->device_type
) {
2366 case PN533_DEVICE_STD
:
2367 protocols
= PN533_ALL_PROTOCOLS
;
2370 case PN533_DEVICE_PASORI
:
2371 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2375 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2381 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2382 PN533_CMD_DATAEXCH_HEAD_LEN
,
2383 PN533_FRAME_TAIL_SIZE
);
2387 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
2388 nfc_set_drvdata(dev
->nfc_dev
, dev
);
2390 rc
= nfc_register_device(dev
->nfc_dev
);
2394 rc
= pn533_setup(dev
);
2396 goto unregister_nfc_dev
;
2401 nfc_unregister_device(dev
->nfc_dev
);
2404 nfc_free_device(dev
->nfc_dev
);
2407 destroy_workqueue(dev
->wq
);
2409 kfree(dev
->in_frame
);
2410 usb_free_urb(dev
->in_urb
);
2411 kfree(dev
->out_frame
);
2412 usb_free_urb(dev
->out_urb
);
2417 static void pn533_disconnect(struct usb_interface
*interface
)
2421 dev
= usb_get_intfdata(interface
);
2422 usb_set_intfdata(interface
, NULL
);
2424 nfc_unregister_device(dev
->nfc_dev
);
2425 nfc_free_device(dev
->nfc_dev
);
2427 usb_kill_urb(dev
->in_urb
);
2428 usb_kill_urb(dev
->out_urb
);
2430 destroy_workqueue(dev
->wq
);
2432 skb_queue_purge(&dev
->resp_q
);
2434 del_timer(&dev
->listen_timer
);
2436 kfree(dev
->in_frame
);
2437 usb_free_urb(dev
->in_urb
);
2438 kfree(dev
->out_frame
);
2439 usb_free_urb(dev
->out_urb
);
2442 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2445 static struct usb_driver pn533_driver
= {
2447 .probe
= pn533_probe
,
2448 .disconnect
= pn533_disconnect
,
2449 .id_table
= pn533_table
,
2452 module_usb_driver(pn533_driver
);
2454 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2455 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2456 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2457 MODULE_VERSION(VERSION
);
2458 MODULE_LICENSE("GPL");