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_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
91 #define PN533_FRAME_TAIL_SIZE 2
92 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
93 PN533_FRAME_TAIL_SIZE)
94 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
95 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
96 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
99 #define PN533_SOF 0x00FF
101 /* frame identifier: in/out/error */
102 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
103 #define PN533_DIR_OUT 0xD4
104 #define PN533_DIR_IN 0xD5
107 #define PN533_FRAME_CMD(f) (f->data[1])
108 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
109 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
111 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
112 #define PN533_CMD_RF_CONFIGURATION 0x32
113 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
114 #define PN533_CMD_IN_COMM_THRU 0x42
115 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
116 #define PN533_CMD_IN_ATR 0x50
117 #define PN533_CMD_IN_RELEASE 0x52
118 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
120 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
121 #define PN533_CMD_TG_GET_DATA 0x86
122 #define PN533_CMD_TG_SET_DATA 0x8e
124 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
126 /* PN533 Return codes */
127 #define PN533_CMD_RET_MASK 0x3F
128 #define PN533_CMD_MI_MASK 0x40
129 #define PN533_CMD_RET_SUCCESS 0x00
131 /* PN533 status codes */
132 #define PN533_STATUS_TARGET_RELEASED 0x29
136 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
137 u8
*params
, int params_len
);
139 /* structs for pn533 commands */
141 /* PN533_CMD_GET_FIRMWARE_VERSION */
142 struct pn533_fw_version
{
149 /* PN533_CMD_RF_CONFIGURATION */
150 #define PN533_CFGITEM_TIMING 0x02
151 #define PN533_CFGITEM_MAX_RETRIES 0x05
152 #define PN533_CFGITEM_PASORI 0x82
154 #define PN533_CONFIG_TIMING_102 0xb
155 #define PN533_CONFIG_TIMING_204 0xc
156 #define PN533_CONFIG_TIMING_409 0xd
157 #define PN533_CONFIG_TIMING_819 0xe
159 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
160 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
162 struct pn533_config_max_retries
{
165 u8 mx_rty_passive_act
;
168 struct pn533_config_timing
{
174 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
176 /* felica commands opcode */
177 #define PN533_FELICA_OPC_SENSF_REQ 0
178 #define PN533_FELICA_OPC_SENSF_RES 1
179 /* felica SENSF_REQ parameters */
180 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
181 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
182 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
183 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
185 /* type B initiator_data values */
186 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
187 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
188 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
190 union pn533_cmd_poll_initdata
{
203 /* Poll modulations */
205 PN533_POLL_MOD_106KBPS_A
,
206 PN533_POLL_MOD_212KBPS_FELICA
,
207 PN533_POLL_MOD_424KBPS_FELICA
,
208 PN533_POLL_MOD_106KBPS_JEWEL
,
209 PN533_POLL_MOD_847KBPS_B
,
212 __PN533_POLL_MOD_AFTER_LAST
,
214 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
216 struct pn533_poll_modulations
{
220 union pn533_cmd_poll_initdata initiator_data
;
225 const struct pn533_poll_modulations poll_mod
[] = {
226 [PN533_POLL_MOD_106KBPS_A
] = {
233 [PN533_POLL_MOD_212KBPS_FELICA
] = {
237 .initiator_data
.felica
= {
238 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
239 .sc
= PN533_FELICA_SENSF_SC_ALL
,
240 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
246 [PN533_POLL_MOD_424KBPS_FELICA
] = {
250 .initiator_data
.felica
= {
251 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
252 .sc
= PN533_FELICA_SENSF_SC_ALL
,
253 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
259 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
266 [PN533_POLL_MOD_847KBPS_B
] = {
270 .initiator_data
.type_b
= {
271 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
273 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
278 [PN533_LISTEN_MOD
] = {
283 /* PN533_CMD_IN_ATR */
285 struct pn533_cmd_activate_param
{
290 struct pn533_cmd_activate_response
{
302 /* PN533_CMD_IN_JUMP_FOR_DEP */
303 struct pn533_cmd_jump_dep
{
310 struct pn533_cmd_jump_dep_response
{
324 /* PN533_TG_INIT_AS_TARGET */
325 #define PN533_INIT_TARGET_PASSIVE 0x1
326 #define PN533_INIT_TARGET_DEP 0x2
328 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
329 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
330 #define PN533_INIT_TARGET_RESP_DEP 0x4
332 struct pn533_cmd_init_target
{
341 struct pn533_cmd_init_target_response
{
347 struct usb_device
*udev
;
348 struct usb_interface
*interface
;
349 struct nfc_dev
*nfc_dev
;
352 struct pn533_frame
*out_frame
;
355 struct pn533_frame
*in_frame
;
357 struct sk_buff_head resp_q
;
359 struct workqueue_struct
*wq
;
360 struct work_struct cmd_work
;
361 struct work_struct cmd_complete_work
;
362 struct work_struct poll_work
;
363 struct work_struct mi_work
;
364 struct work_struct tg_work
;
365 struct timer_list listen_timer
;
366 struct pn533_frame
*wq_in_frame
;
370 pn533_cmd_complete_t cmd_complete
;
371 void *cmd_complete_arg
;
372 struct mutex cmd_lock
;
375 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
379 u32 listen_protocols
;
384 u8 tgt_available_prots
;
390 struct list_head cmd_queue
;
395 struct list_head queue
;
396 struct pn533_frame
*out_frame
;
397 struct pn533_frame
*in_frame
;
399 pn533_cmd_complete_t cmd_complete
;
412 /* The rule: value + checksum = 0 */
413 static inline u8
pn533_checksum(u8 value
)
418 /* The rule: sum(data elements) + checksum = 0 */
419 static u8
pn533_data_checksum(u8
*data
, int datalen
)
424 for (i
= 0; i
< datalen
; i
++)
427 return pn533_checksum(sum
);
431 * pn533_tx_frame_ack - create a ack frame
432 * @frame: The frame to be set as ack
434 * Ack is different type of standard frame. As a standard frame, it has
435 * preamble and start_frame. However the checksum of this frame must fail,
436 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
437 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
438 * After datalen_checksum field, the postamble is placed.
440 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
443 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
445 frame
->datalen_checksum
= 0xFF;
446 /* data[0] is used as postamble */
450 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
453 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
454 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
455 PN533_FRAME_CMD(frame
) = cmd
;
459 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
461 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
463 PN533_FRAME_CHECKSUM(frame
) =
464 pn533_data_checksum(frame
->data
, frame
->datalen
);
466 PN533_FRAME_POSTAMBLE(frame
) = 0;
469 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
473 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
476 checksum
= pn533_checksum(frame
->datalen
);
477 if (checksum
!= frame
->datalen_checksum
)
480 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
481 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
487 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
489 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
492 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
498 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
500 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
504 static void pn533_wq_cmd_complete(struct work_struct
*work
)
506 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
507 struct pn533_frame
*in_frame
;
510 in_frame
= dev
->wq_in_frame
;
512 if (dev
->wq_in_error
)
513 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
516 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
517 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
518 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
520 if (rc
!= -EINPROGRESS
)
521 queue_work(dev
->wq
, &dev
->cmd_work
);
524 static void pn533_recv_response(struct urb
*urb
)
526 struct pn533
*dev
= urb
->context
;
527 struct pn533_frame
*in_frame
;
529 dev
->wq_in_frame
= NULL
;
531 switch (urb
->status
) {
538 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
539 " status: %d", urb
->status
);
540 dev
->wq_in_error
= urb
->status
;
543 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
545 dev
->wq_in_error
= urb
->status
;
549 in_frame
= dev
->in_urb
->transfer_buffer
;
551 if (!pn533_rx_frame_is_valid(in_frame
)) {
552 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
553 dev
->wq_in_error
= -EIO
;
557 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
558 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
559 "response to the last command");
560 dev
->wq_in_error
= -EIO
;
564 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
565 dev
->wq_in_error
= 0;
566 dev
->wq_in_frame
= in_frame
;
569 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
572 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
574 dev
->in_urb
->complete
= pn533_recv_response
;
576 return usb_submit_urb(dev
->in_urb
, flags
);
579 static void pn533_recv_ack(struct urb
*urb
)
581 struct pn533
*dev
= urb
->context
;
582 struct pn533_frame
*in_frame
;
585 switch (urb
->status
) {
592 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
593 " status: %d", urb
->status
);
594 dev
->wq_in_error
= urb
->status
;
597 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
599 dev
->wq_in_error
= urb
->status
;
603 in_frame
= dev
->in_urb
->transfer_buffer
;
605 if (!pn533_rx_frame_is_ack(in_frame
)) {
606 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
607 dev
->wq_in_error
= -EIO
;
611 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
613 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
615 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
617 dev
->wq_in_error
= rc
;
624 dev
->wq_in_frame
= NULL
;
625 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
628 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
630 dev
->in_urb
->complete
= pn533_recv_ack
;
632 return usb_submit_urb(dev
->in_urb
, flags
);
635 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
639 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
641 pn533_tx_frame_ack(dev
->out_frame
);
643 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
644 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
645 rc
= usb_submit_urb(dev
->out_urb
, flags
);
650 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
651 struct pn533_frame
*out_frame
,
652 struct pn533_frame
*in_frame
,
654 pn533_cmd_complete_t cmd_complete
,
655 void *arg
, gfp_t flags
)
659 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
660 PN533_FRAME_CMD(out_frame
));
662 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
663 dev
->cmd_complete
= cmd_complete
;
664 dev
->cmd_complete_arg
= arg
;
666 dev
->out_urb
->transfer_buffer
= out_frame
;
667 dev
->out_urb
->transfer_buffer_length
=
668 PN533_FRAME_SIZE(out_frame
);
670 dev
->in_urb
->transfer_buffer
= in_frame
;
671 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
673 rc
= usb_submit_urb(dev
->out_urb
, flags
);
677 rc
= pn533_submit_urb_for_ack(dev
, flags
);
684 usb_unlink_urb(dev
->out_urb
);
688 static void pn533_wq_cmd(struct work_struct
*work
)
690 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
691 struct pn533_cmd
*cmd
;
693 mutex_lock(&dev
->cmd_lock
);
695 if (list_empty(&dev
->cmd_queue
)) {
696 dev
->cmd_pending
= 0;
697 mutex_unlock(&dev
->cmd_lock
);
701 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
703 list_del(&cmd
->queue
);
705 mutex_unlock(&dev
->cmd_lock
);
707 __pn533_send_cmd_frame_async(dev
, cmd
->out_frame
, cmd
->in_frame
,
708 cmd
->in_frame_len
, cmd
->cmd_complete
,
709 cmd
->arg
, cmd
->flags
);
714 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
715 struct pn533_frame
*out_frame
,
716 struct pn533_frame
*in_frame
,
718 pn533_cmd_complete_t cmd_complete
,
719 void *arg
, gfp_t flags
)
721 struct pn533_cmd
*cmd
;
724 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
726 mutex_lock(&dev
->cmd_lock
);
728 if (!dev
->cmd_pending
) {
729 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
730 in_frame_len
, cmd_complete
,
733 dev
->cmd_pending
= 1;
738 nfc_dev_dbg(&dev
->interface
->dev
, "%s Queueing command", __func__
);
740 cmd
= kzalloc(sizeof(struct pn533_cmd
), flags
);
746 INIT_LIST_HEAD(&cmd
->queue
);
747 cmd
->out_frame
= out_frame
;
748 cmd
->in_frame
= in_frame
;
749 cmd
->in_frame_len
= in_frame_len
;
750 cmd
->cmd_complete
= cmd_complete
;
754 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
757 mutex_unlock(&dev
->cmd_lock
);
762 struct pn533_sync_cmd_response
{
764 struct completion done
;
767 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
768 u8
*params
, int params_len
)
770 struct pn533_sync_cmd_response
*arg
= _arg
;
772 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
776 if (params_len
< 0) /* error */
777 arg
->rc
= params_len
;
779 complete(&arg
->done
);
784 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
785 struct pn533_frame
*out_frame
,
786 struct pn533_frame
*in_frame
,
790 struct pn533_sync_cmd_response arg
;
792 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
794 init_completion(&arg
.done
);
796 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
797 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
801 wait_for_completion(&arg
.done
);
806 static void pn533_send_complete(struct urb
*urb
)
808 struct pn533
*dev
= urb
->context
;
810 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
812 switch (urb
->status
) {
819 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
820 " status: %d", urb
->status
);
823 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
828 struct pn533_target_type_a
{
836 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
837 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
838 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
840 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
841 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
843 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
844 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
846 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
847 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
848 #define PN533_TYPE_A_SEL_PROT_DEP 2
849 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
851 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
857 if (target_data_len
< sizeof(struct pn533_target_type_a
))
860 /* The lenght check of nfcid[] and ats[] are not being performed because
861 the values are not being used */
863 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
864 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
865 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
867 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
868 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
869 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
870 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
873 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
874 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
880 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
883 struct pn533_target_type_a
*tgt_type_a
;
885 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
887 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
890 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
891 case PN533_TYPE_A_SEL_PROT_MIFARE
:
892 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
894 case PN533_TYPE_A_SEL_PROT_ISO14443
:
895 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
897 case PN533_TYPE_A_SEL_PROT_DEP
:
898 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
900 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
901 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
902 NFC_PROTO_NFC_DEP_MASK
;
906 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
907 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
908 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
909 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
914 struct pn533_target_felica
{
923 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
924 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
926 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
929 if (target_data_len
< sizeof(struct pn533_target_felica
))
932 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
938 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
941 struct pn533_target_felica
*tgt_felica
;
943 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
945 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
948 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
949 tgt_felica
->nfcid2
[1] ==
950 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
951 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
953 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
955 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
956 nfc_tgt
->sensf_res_len
= 9;
961 struct pn533_target_jewel
{
966 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
972 if (target_data_len
< sizeof(struct pn533_target_jewel
))
975 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
976 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
977 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
979 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
980 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
981 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
982 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
988 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
991 struct pn533_target_jewel
*tgt_jewel
;
993 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
995 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
998 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
999 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
1000 nfc_tgt
->nfcid1_len
= 4;
1001 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
1006 struct pn533_type_b_prot_info
{
1012 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1013 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1014 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1016 struct pn533_type_b_sens_res
{
1020 struct pn533_type_b_prot_info prot_info
;
1023 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1025 struct pn533_target_type_b
{
1026 struct pn533_type_b_sens_res sensb_res
;
1031 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
1032 int target_data_len
)
1034 if (target_data_len
< sizeof(struct pn533_target_type_b
))
1037 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
1040 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
1041 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
1047 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1050 struct pn533_target_type_b
*tgt_type_b
;
1052 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
1054 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
1057 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
1062 struct pn533_poll_response
{
1068 static int pn533_target_found(struct pn533
*dev
,
1069 struct pn533_poll_response
*resp
, int resp_len
)
1071 int target_data_len
;
1072 struct nfc_target nfc_tgt
;
1075 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
1076 dev
->poll_mod_curr
);
1081 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1083 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
1085 switch (dev
->poll_mod_curr
) {
1086 case PN533_POLL_MOD_106KBPS_A
:
1087 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
1090 case PN533_POLL_MOD_212KBPS_FELICA
:
1091 case PN533_POLL_MOD_424KBPS_FELICA
:
1092 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
1095 case PN533_POLL_MOD_106KBPS_JEWEL
:
1096 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
1099 case PN533_POLL_MOD_847KBPS_B
:
1100 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
1104 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
1112 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1113 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
1114 " have the desired protocol");
1118 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
1119 "0x%x", nfc_tgt
.supported_protocols
);
1121 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1123 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1128 static inline void pn533_poll_next_mod(struct pn533
*dev
)
1130 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1133 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
1135 dev
->poll_mod_count
= 0;
1138 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
1140 dev
->poll_mod_active
[dev
->poll_mod_count
] =
1141 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
1142 dev
->poll_mod_count
++;
1145 static void pn533_poll_create_mod_list(struct pn533
*dev
,
1146 u32 im_protocols
, u32 tm_protocols
)
1148 pn533_poll_reset_mod_list(dev
);
1150 if (im_protocols
& NFC_PROTO_MIFARE_MASK
1151 || im_protocols
& NFC_PROTO_ISO14443_MASK
1152 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
)
1153 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1155 if (im_protocols
& NFC_PROTO_FELICA_MASK
1156 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1157 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1158 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1161 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1162 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1164 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1165 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1168 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
1171 static int pn533_start_poll_complete(struct pn533
*dev
, u8
*params
, int params_len
)
1173 struct pn533_poll_response
*resp
;
1176 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1178 resp
= (struct pn533_poll_response
*) params
;
1180 rc
= pn533_target_found(dev
, resp
, params_len
);
1182 /* We must stop the poll after a valid target found */
1184 pn533_poll_reset_mod_list(dev
);
1192 static int pn533_init_target_frame(struct pn533_frame
*frame
,
1193 u8
*gb
, size_t gb_len
)
1195 struct pn533_cmd_init_target
*cmd
;
1197 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1198 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1199 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1200 0xff, 0xff}; /* System code */
1201 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1203 0x40}; /* SEL_RES for DEP */
1205 cmd_len
= sizeof(struct pn533_cmd_init_target
) + gb_len
+ 1;
1206 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1210 pn533_tx_frame_init(frame
, PN533_CMD_TG_INIT_AS_TARGET
);
1212 /* DEP support only */
1213 cmd
->mode
|= PN533_INIT_TARGET_DEP
;
1216 memcpy(cmd
->felica
, felica_params
, 18);
1217 get_random_bytes(cmd
->felica
+ 2, 6);
1220 memset(cmd
->nfcid3
, 0, 10);
1221 memcpy(cmd
->nfcid3
, cmd
->felica
, 8);
1224 memcpy(cmd
->mifare
, mifare_params
, 6);
1227 cmd
->gb_len
= gb_len
;
1228 memcpy(cmd
->gb
, gb
, gb_len
);
1231 cmd
->gb
[gb_len
] = 0;
1233 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), cmd
, cmd_len
);
1235 frame
->datalen
+= cmd_len
;
1237 pn533_tx_frame_finish(frame
);
1244 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1245 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1246 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1247 u8
*params
, int params_len
)
1249 struct sk_buff
*skb_resp
= arg
;
1250 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1252 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1254 if (params_len
< 0) {
1255 nfc_dev_err(&dev
->interface
->dev
,
1256 "Error %d when starting as a target",
1262 if (params_len
> 0 && params
[0] != 0) {
1263 nfc_tm_deactivated(dev
->nfc_dev
);
1267 kfree_skb(skb_resp
);
1271 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1272 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1273 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1275 return nfc_tm_data_received(dev
->nfc_dev
, skb_resp
);
1278 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1280 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1281 struct pn533_frame
*in_frame
;
1282 struct sk_buff
*skb_resp
;
1283 size_t skb_resp_len
;
1285 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1287 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1288 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1289 PN533_FRAME_TAIL_SIZE
;
1291 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1295 in_frame
= (struct pn533_frame
*)skb_resp
->data
;
1297 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_TG_GET_DATA
);
1298 pn533_tx_frame_finish(dev
->out_frame
);
1300 pn533_send_cmd_frame_async(dev
, dev
->out_frame
, in_frame
,
1302 pn533_tm_get_data_complete
,
1303 skb_resp
, GFP_KERNEL
);
1308 #define ATR_REQ_GB_OFFSET 17
1309 static int pn533_init_target_complete(struct pn533
*dev
, u8
*params
, int params_len
)
1311 struct pn533_cmd_init_target_response
*resp
;
1312 u8 frame
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1316 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1318 if (params_len
< 0) {
1319 nfc_dev_err(&dev
->interface
->dev
,
1320 "Error %d when starting as a target",
1326 if (params_len
< ATR_REQ_GB_OFFSET
+ 1)
1329 resp
= (struct pn533_cmd_init_target_response
*) params
;
1331 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x param len %d\n",
1332 resp
->mode
, params_len
);
1334 frame
= resp
->mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
;
1335 if (frame
== PN533_INIT_TARGET_RESP_ACTIVE
)
1336 comm_mode
= NFC_COMM_ACTIVE
;
1338 /* Again, only DEP */
1339 if ((resp
->mode
& PN533_INIT_TARGET_RESP_DEP
) == 0)
1342 gb
= resp
->cmd
+ ATR_REQ_GB_OFFSET
;
1343 gb_len
= params_len
- (ATR_REQ_GB_OFFSET
+ 1);
1345 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1346 comm_mode
, gb
, gb_len
);
1348 nfc_dev_err(&dev
->interface
->dev
,
1349 "Error when signaling target activation");
1355 queue_work(dev
->wq
, &dev
->tg_work
);
1360 static void pn533_listen_mode_timer(unsigned long data
)
1362 struct pn533
*dev
= (struct pn533
*) data
;
1364 nfc_dev_dbg(&dev
->interface
->dev
, "Listen mode timeout");
1366 /* An ack will cancel the last issued command (poll) */
1367 pn533_send_ack(dev
, GFP_ATOMIC
);
1369 dev
->cancel_listen
= 1;
1371 pn533_poll_next_mod(dev
);
1373 queue_work(dev
->wq
, &dev
->poll_work
);
1376 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1377 u8
*params
, int params_len
)
1379 struct pn533_poll_modulations
*cur_mod
;
1382 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1384 if (params_len
== -ENOENT
) {
1385 if (dev
->poll_mod_count
!= 0)
1388 nfc_dev_err(&dev
->interface
->dev
,
1389 "Polling operation has been stopped");
1394 if (params_len
< 0) {
1395 nfc_dev_err(&dev
->interface
->dev
,
1396 "Error %d when running poll", params_len
);
1401 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1403 if (cur_mod
->len
== 0) {
1404 del_timer(&dev
->listen_timer
);
1406 return pn533_init_target_complete(dev
, params
, params_len
);
1408 rc
= pn533_start_poll_complete(dev
, params
, params_len
);
1413 pn533_poll_next_mod(dev
);
1415 queue_work(dev
->wq
, &dev
->poll_work
);
1420 pn533_poll_reset_mod_list(dev
);
1421 dev
->poll_protocols
= 0;
1425 static void pn533_build_poll_frame(struct pn533
*dev
,
1426 struct pn533_frame
*frame
,
1427 struct pn533_poll_modulations
*mod
)
1429 nfc_dev_dbg(&dev
->interface
->dev
, "mod len %d\n", mod
->len
);
1431 if (mod
->len
== 0) {
1433 pn533_init_target_frame(frame
, dev
->gb
, dev
->gb_len
);
1436 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
1438 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1439 frame
->datalen
+= mod
->len
;
1441 pn533_tx_frame_finish(frame
);
1445 static int pn533_send_poll_frame(struct pn533
*dev
)
1447 struct pn533_poll_modulations
*cur_mod
;
1450 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1452 pn533_build_poll_frame(dev
, dev
->out_frame
, cur_mod
);
1454 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1455 PN533_NORMAL_FRAME_MAX_LEN
,
1456 pn533_poll_complete
,
1459 nfc_dev_err(&dev
->interface
->dev
, "Polling loop error %d", rc
);
1464 static void pn533_wq_poll(struct work_struct
*work
)
1466 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
);
1467 struct pn533_poll_modulations
*cur_mod
;
1470 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1472 nfc_dev_dbg(&dev
->interface
->dev
,
1473 "%s cancel_listen %d modulation len %d",
1474 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1476 if (dev
->cancel_listen
== 1) {
1477 dev
->cancel_listen
= 0;
1478 usb_kill_urb(dev
->in_urb
);
1481 rc
= pn533_send_poll_frame(dev
);
1485 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1486 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1491 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1492 u32 im_protocols
, u32 tm_protocols
)
1494 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1496 nfc_dev_dbg(&dev
->interface
->dev
,
1497 "%s: im protocols 0x%x tm protocols 0x%x",
1498 __func__
, im_protocols
, tm_protocols
);
1500 if (dev
->tgt_active_prot
) {
1501 nfc_dev_err(&dev
->interface
->dev
,
1502 "Cannot poll with a target already activated");
1506 if (dev
->tgt_mode
) {
1507 nfc_dev_err(&dev
->interface
->dev
,
1508 "Cannot poll while already being activated");
1513 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1514 if (dev
->gb
== NULL
)
1518 dev
->poll_mod_curr
= 0;
1519 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1520 dev
->poll_protocols
= im_protocols
;
1521 dev
->listen_protocols
= tm_protocols
;
1523 return pn533_send_poll_frame(dev
);
1526 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1528 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1530 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1532 del_timer(&dev
->listen_timer
);
1534 if (!dev
->poll_mod_count
) {
1535 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1540 /* An ack will cancel the last issued command (poll) */
1541 pn533_send_ack(dev
, GFP_KERNEL
);
1543 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1544 usb_kill_urb(dev
->in_urb
);
1546 pn533_poll_reset_mod_list(dev
);
1549 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1551 struct pn533_cmd_activate_param param
;
1552 struct pn533_cmd_activate_response
*resp
;
1556 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1558 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1562 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1563 sizeof(struct pn533_cmd_activate_param
));
1564 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1566 pn533_tx_frame_finish(dev
->out_frame
);
1568 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1569 PN533_NORMAL_FRAME_MAX_LEN
);
1573 resp
= (struct pn533_cmd_activate_response
*)
1574 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1575 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1576 if (rc
!= PN533_CMD_RET_SUCCESS
)
1579 /* ATR_RES general bytes are located at offset 16 */
1580 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1581 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1586 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1587 struct nfc_target
*target
, u32 protocol
)
1589 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1592 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1595 if (dev
->poll_mod_count
) {
1596 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1601 if (dev
->tgt_active_prot
) {
1602 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1607 if (!dev
->tgt_available_prots
) {
1608 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1613 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1614 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1615 " the requested protocol %u", protocol
);
1619 if (protocol
== NFC_PROTO_NFC_DEP
) {
1620 rc
= pn533_activate_target_nfcdep(dev
);
1622 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1623 " activating target with"
1624 " NFC_DEP protocol", rc
);
1629 dev
->tgt_active_prot
= protocol
;
1630 dev
->tgt_available_prots
= 0;
1635 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1636 struct nfc_target
*target
)
1638 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1643 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1645 if (!dev
->tgt_active_prot
) {
1646 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1650 dev
->tgt_active_prot
= 0;
1652 skb_queue_purge(&dev
->resp_q
);
1654 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1657 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1658 dev
->out_frame
->datalen
+= sizeof(u8
);
1660 pn533_tx_frame_finish(dev
->out_frame
);
1662 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1663 PN533_NORMAL_FRAME_MAX_LEN
);
1665 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1666 " command to the controller");
1670 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1671 rc
= status
& PN533_CMD_RET_MASK
;
1672 if (rc
!= PN533_CMD_RET_SUCCESS
)
1673 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1680 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1681 u8
*params
, int params_len
)
1683 struct pn533_cmd_jump_dep_response
*resp
;
1684 struct nfc_target nfc_target
;
1687 struct pn533_cmd_jump_dep
*cmd
= (struct pn533_cmd_jump_dep
*)arg
;
1688 u8 active
= cmd
->active
;
1692 if (params_len
== -ENOENT
) {
1693 nfc_dev_dbg(&dev
->interface
->dev
, "");
1697 if (params_len
< 0) {
1698 nfc_dev_err(&dev
->interface
->dev
,
1699 "Error %d when bringing DEP link up",
1704 if (dev
->tgt_available_prots
&&
1705 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1706 nfc_dev_err(&dev
->interface
->dev
,
1707 "The target does not support DEP");
1711 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1712 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1713 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1714 nfc_dev_err(&dev
->interface
->dev
,
1715 "Bringing DEP link up failed %d", rc
);
1719 if (!dev
->tgt_available_prots
) {
1720 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1722 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1723 nfc_target
.nfcid1_len
= 10;
1724 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1725 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1729 dev
->tgt_available_prots
= 0;
1732 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1734 /* ATR_RES general bytes are located at offset 17 */
1735 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1736 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1737 resp
->gt
, target_gt_len
);
1739 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1740 dev
->nfc_dev
->targets
[0].idx
,
1741 !active
, NFC_RF_INITIATOR
);
1746 static int pn533_mod_to_baud(struct pn533
*dev
)
1748 switch (dev
->poll_mod_curr
) {
1749 case PN533_POLL_MOD_106KBPS_A
:
1751 case PN533_POLL_MOD_212KBPS_FELICA
:
1753 case PN533_POLL_MOD_424KBPS_FELICA
:
1760 #define PASSIVE_DATA_LEN 5
1761 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1762 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1764 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1765 struct pn533_cmd_jump_dep
*cmd
;
1766 u8 cmd_len
, *data_ptr
;
1767 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1770 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1772 if (dev
->poll_mod_count
) {
1773 nfc_dev_err(&dev
->interface
->dev
,
1774 "Cannot bring the DEP link up while polling");
1778 if (dev
->tgt_active_prot
) {
1779 nfc_dev_err(&dev
->interface
->dev
,
1780 "There is already an active target");
1784 baud
= pn533_mod_to_baud(dev
);
1786 nfc_dev_err(&dev
->interface
->dev
,
1787 "Invalid curr modulation %d", dev
->poll_mod_curr
);
1791 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1792 if (comm_mode
== NFC_COMM_PASSIVE
)
1793 cmd_len
+= PASSIVE_DATA_LEN
;
1795 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1799 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1801 cmd
->active
= !comm_mode
;
1804 data_ptr
= cmd
->data
;
1805 if (comm_mode
== NFC_COMM_PASSIVE
&& cmd
->baud
> 0) {
1806 memcpy(data_ptr
, passive_data
, PASSIVE_DATA_LEN
);
1808 data_ptr
+= PASSIVE_DATA_LEN
;
1811 if (gb
!= NULL
&& gb_len
> 0) {
1812 cmd
->next
|= 4; /* We have some Gi */
1813 memcpy(data_ptr
, gb
, gb_len
);
1818 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1819 dev
->out_frame
->datalen
+= cmd_len
;
1821 pn533_tx_frame_finish(dev
->out_frame
);
1823 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1824 PN533_NORMAL_FRAME_MAX_LEN
,
1825 pn533_in_dep_link_up_complete
, cmd
,
1833 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1835 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1837 pn533_poll_reset_mod_list(dev
);
1839 if (dev
->tgt_mode
|| dev
->tgt_active_prot
) {
1840 pn533_send_ack(dev
, GFP_KERNEL
);
1841 usb_kill_urb(dev
->in_urb
);
1844 dev
->tgt_active_prot
= 0;
1847 skb_queue_purge(&dev
->resp_q
);
1852 static int pn533_build_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
,
1855 int payload_len
= skb
->len
;
1856 struct pn533_frame
*out_frame
;
1859 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1862 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1863 /* TODO: Implement support to multi-part data exchange */
1864 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1866 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1870 if (target
== true) {
1871 switch (dev
->device_type
) {
1872 case PN533_DEVICE_PASORI
:
1873 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
1874 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
- 1);
1875 out_frame
= (struct pn533_frame
*) skb
->data
;
1876 pn533_tx_frame_init(out_frame
,
1877 PN533_CMD_IN_COMM_THRU
);
1883 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1884 out_frame
= (struct pn533_frame
*) skb
->data
;
1885 pn533_tx_frame_init(out_frame
,
1886 PN533_CMD_IN_DATA_EXCHANGE
);
1888 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
),
1890 out_frame
->datalen
+= sizeof(u8
);
1896 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
- 1);
1897 out_frame
= (struct pn533_frame
*) skb
->data
;
1898 pn533_tx_frame_init(out_frame
, PN533_CMD_TG_SET_DATA
);
1902 /* The data is already in the out_frame, just update the datalen */
1903 out_frame
->datalen
+= payload_len
;
1905 pn533_tx_frame_finish(out_frame
);
1906 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1911 struct pn533_data_exchange_arg
{
1912 struct sk_buff
*skb_resp
;
1913 struct sk_buff
*skb_out
;
1914 data_exchange_cb_t cb
;
1918 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1920 struct sk_buff
*skb
, *tmp
, *t
;
1921 unsigned int skb_len
= 0, tmp_len
= 0;
1923 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
1925 if (skb_queue_empty(&dev
->resp_q
))
1928 if (skb_queue_len(&dev
->resp_q
) == 1) {
1929 skb
= skb_dequeue(&dev
->resp_q
);
1933 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1934 skb_len
+= tmp
->len
;
1936 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1939 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
1943 skb_put(skb
, skb_len
);
1945 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
1946 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
1947 tmp_len
+= tmp
->len
;
1951 skb_queue_purge(&dev
->resp_q
);
1956 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1957 u8
*params
, int params_len
)
1959 struct pn533_data_exchange_arg
*arg
= _arg
;
1960 struct sk_buff
*skb
= NULL
, *skb_resp
= arg
->skb_resp
;
1961 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1966 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1968 dev_kfree_skb(arg
->skb_out
);
1970 if (params_len
< 0) { /* error */
1977 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1978 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1979 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1980 " exchanging data", cmd_ret
);
1985 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1986 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1987 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1988 skb_queue_tail(&dev
->resp_q
, skb_resp
);
1990 if (status
& PN533_CMD_MI_MASK
) {
1991 queue_work(dev
->wq
, &dev
->mi_work
);
1992 return -EINPROGRESS
;
1995 skb
= pn533_build_response(dev
);
1999 arg
->cb(arg
->cb_context
, skb
, 0);
2004 skb_queue_purge(&dev
->resp_q
);
2005 dev_kfree_skb(skb_resp
);
2006 arg
->cb(arg
->cb_context
, NULL
, err
);
2011 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2012 struct nfc_target
*target
, struct sk_buff
*skb
,
2013 data_exchange_cb_t cb
, void *cb_context
)
2015 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2016 struct pn533_frame
*out_frame
, *in_frame
;
2017 struct pn533_data_exchange_arg
*arg
;
2018 struct sk_buff
*skb_resp
;
2022 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2024 if (!dev
->tgt_active_prot
) {
2025 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
2026 " there is no active target");
2031 rc
= pn533_build_tx_frame(dev
, skb
, true);
2035 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
2036 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
2037 PN533_FRAME_TAIL_SIZE
;
2039 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
2045 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
2046 out_frame
= (struct pn533_frame
*) skb
->data
;
2048 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
2054 arg
->skb_resp
= skb_resp
;
2057 arg
->cb_context
= cb_context
;
2059 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
2060 pn533_data_exchange_complete
, arg
,
2063 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
2064 " perform data_exchange", rc
);
2073 kfree_skb(skb_resp
);
2079 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2080 u8
*params
, int params_len
)
2082 struct sk_buff
*skb_out
= arg
;
2084 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2086 dev_kfree_skb(skb_out
);
2088 if (params_len
< 0) {
2089 nfc_dev_err(&dev
->interface
->dev
,
2090 "Error %d when sending data",
2096 if (params_len
> 0 && params
[0] != 0) {
2097 nfc_tm_deactivated(dev
->nfc_dev
);
2104 queue_work(dev
->wq
, &dev
->tg_work
);
2109 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2111 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2112 struct pn533_frame
*out_frame
;
2115 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2117 rc
= pn533_build_tx_frame(dev
, skb
, false);
2121 out_frame
= (struct pn533_frame
*) skb
->data
;
2123 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, dev
->in_frame
,
2124 PN533_NORMAL_FRAME_MAX_LEN
,
2125 pn533_tm_send_complete
, skb
,
2128 nfc_dev_err(&dev
->interface
->dev
,
2129 "Error %d when trying to send data", rc
);
2141 static void pn533_wq_mi_recv(struct work_struct
*work
)
2143 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
2144 struct sk_buff
*skb_cmd
;
2145 struct pn533_data_exchange_arg
*arg
= dev
->cmd_complete_arg
;
2146 struct pn533_frame
*out_frame
, *in_frame
;
2147 struct sk_buff
*skb_resp
;
2151 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2153 /* This is a zero payload size skb */
2154 skb_cmd
= alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
+ PN533_FRAME_TAIL_SIZE
,
2156 if (skb_cmd
== NULL
)
2159 skb_reserve(skb_cmd
, PN533_CMD_DATAEXCH_HEAD_LEN
);
2161 rc
= pn533_build_tx_frame(dev
, skb_cmd
, true);
2165 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
2166 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
2167 PN533_FRAME_TAIL_SIZE
;
2168 skb_resp
= alloc_skb(skb_resp_len
, GFP_KERNEL
);
2174 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
2175 out_frame
= (struct pn533_frame
*) skb_cmd
->data
;
2177 arg
->skb_resp
= skb_resp
;
2178 arg
->skb_out
= skb_cmd
;
2180 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
2182 pn533_data_exchange_complete
,
2183 dev
->cmd_complete_arg
, GFP_KERNEL
);
2187 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
2188 " perform data_exchange", rc
);
2190 kfree_skb(skb_resp
);
2196 pn533_send_ack(dev
, GFP_KERNEL
);
2200 queue_work(dev
->wq
, &dev
->cmd_work
);
2203 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2209 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2211 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
2213 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
2214 params
[0] = cfgitem
;
2215 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
2216 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
2218 pn533_tx_frame_finish(dev
->out_frame
);
2220 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2221 PN533_NORMAL_FRAME_MAX_LEN
);
2226 static int pn533_fw_reset(struct pn533
*dev
)
2231 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2233 pn533_tx_frame_init(dev
->out_frame
, 0x18);
2235 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
2237 dev
->out_frame
->datalen
+= 1;
2239 pn533_tx_frame_finish(dev
->out_frame
);
2241 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2242 PN533_NORMAL_FRAME_MAX_LEN
);
2247 static struct nfc_ops pn533_nfc_ops
= {
2250 .dep_link_up
= pn533_dep_link_up
,
2251 .dep_link_down
= pn533_dep_link_down
,
2252 .start_poll
= pn533_start_poll
,
2253 .stop_poll
= pn533_stop_poll
,
2254 .activate_target
= pn533_activate_target
,
2255 .deactivate_target
= pn533_deactivate_target
,
2256 .im_transceive
= pn533_transceive
,
2257 .tm_send
= pn533_tm_send
,
2260 static int pn533_setup(struct pn533
*dev
)
2262 struct pn533_config_max_retries max_retries
;
2263 struct pn533_config_timing timing
;
2264 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2267 switch (dev
->device_type
) {
2268 case PN533_DEVICE_STD
:
2269 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
2270 max_retries
.mx_rty_psl
= 2;
2271 max_retries
.mx_rty_passive_act
=
2272 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2274 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2275 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_204
;
2276 timing
.dep_timeout
= PN533_CONFIG_TIMING_409
;
2280 case PN533_DEVICE_PASORI
:
2281 max_retries
.mx_rty_atr
= 0x2;
2282 max_retries
.mx_rty_psl
= 0x1;
2283 max_retries
.mx_rty_passive_act
=
2284 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2286 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2287 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2288 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2293 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2298 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2299 (u8
*)&max_retries
, sizeof(max_retries
));
2301 nfc_dev_err(&dev
->interface
->dev
,
2302 "Error on setting MAX_RETRIES config");
2307 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2308 (u8
*)&timing
, sizeof(timing
));
2310 nfc_dev_err(&dev
->interface
->dev
,
2311 "Error on setting RF timings");
2315 switch (dev
->device_type
) {
2316 case PN533_DEVICE_STD
:
2319 case PN533_DEVICE_PASORI
:
2320 pn533_fw_reset(dev
);
2322 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2325 nfc_dev_err(&dev
->interface
->dev
,
2326 "Error while settings PASORI config");
2330 pn533_fw_reset(dev
);
2338 static int pn533_probe(struct usb_interface
*interface
,
2339 const struct usb_device_id
*id
)
2341 struct pn533_fw_version
*fw_ver
;
2343 struct usb_host_interface
*iface_desc
;
2344 struct usb_endpoint_descriptor
*endpoint
;
2345 int in_endpoint
= 0;
2346 int out_endpoint
= 0;
2351 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2355 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2356 dev
->interface
= interface
;
2357 mutex_init(&dev
->cmd_lock
);
2359 iface_desc
= interface
->cur_altsetting
;
2360 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2361 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2363 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
2364 in_endpoint
= endpoint
->bEndpointAddress
;
2366 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
2367 out_endpoint
= endpoint
->bEndpointAddress
;
2370 if (!in_endpoint
|| !out_endpoint
) {
2371 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
2372 " bulk-out endpoint");
2377 dev
->in_frame
= kmalloc(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
2378 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2379 dev
->out_frame
= kmalloc(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
2380 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2382 if (!dev
->in_frame
|| !dev
->out_frame
||
2383 !dev
->in_urb
|| !dev
->out_urb
)
2386 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
2387 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
2388 NULL
, 0, NULL
, dev
);
2389 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
2390 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
2392 pn533_send_complete
, dev
);
2394 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd
);
2395 INIT_WORK(&dev
->cmd_complete_work
, pn533_wq_cmd_complete
);
2396 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
2397 INIT_WORK(&dev
->tg_work
, pn533_wq_tg_get_data
);
2398 INIT_WORK(&dev
->poll_work
, pn533_wq_poll
);
2399 dev
->wq
= alloc_ordered_workqueue("pn533", 0);
2400 if (dev
->wq
== NULL
)
2403 init_timer(&dev
->listen_timer
);
2404 dev
->listen_timer
.data
= (unsigned long) dev
;
2405 dev
->listen_timer
.function
= pn533_listen_mode_timer
;
2407 skb_queue_head_init(&dev
->resp_q
);
2409 INIT_LIST_HEAD(&dev
->cmd_queue
);
2411 usb_set_intfdata(interface
, dev
);
2413 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
2414 pn533_tx_frame_finish(dev
->out_frame
);
2416 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
2417 PN533_NORMAL_FRAME_MAX_LEN
);
2421 fw_ver
= (struct pn533_fw_version
*)
2422 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
2423 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
2424 " attached", fw_ver
->ver
, fw_ver
->rev
);
2426 dev
->device_type
= id
->driver_info
;
2427 switch (dev
->device_type
) {
2428 case PN533_DEVICE_STD
:
2429 protocols
= PN533_ALL_PROTOCOLS
;
2432 case PN533_DEVICE_PASORI
:
2433 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2437 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2443 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2444 PN533_CMD_DATAEXCH_HEAD_LEN
,
2445 PN533_FRAME_TAIL_SIZE
);
2449 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
2450 nfc_set_drvdata(dev
->nfc_dev
, dev
);
2452 rc
= nfc_register_device(dev
->nfc_dev
);
2456 rc
= pn533_setup(dev
);
2458 goto unregister_nfc_dev
;
2463 nfc_unregister_device(dev
->nfc_dev
);
2466 nfc_free_device(dev
->nfc_dev
);
2469 destroy_workqueue(dev
->wq
);
2471 kfree(dev
->in_frame
);
2472 usb_free_urb(dev
->in_urb
);
2473 kfree(dev
->out_frame
);
2474 usb_free_urb(dev
->out_urb
);
2479 static void pn533_disconnect(struct usb_interface
*interface
)
2482 struct pn533_cmd
*cmd
, *n
;
2484 dev
= usb_get_intfdata(interface
);
2485 usb_set_intfdata(interface
, NULL
);
2487 nfc_unregister_device(dev
->nfc_dev
);
2488 nfc_free_device(dev
->nfc_dev
);
2490 usb_kill_urb(dev
->in_urb
);
2491 usb_kill_urb(dev
->out_urb
);
2493 destroy_workqueue(dev
->wq
);
2495 skb_queue_purge(&dev
->resp_q
);
2497 del_timer(&dev
->listen_timer
);
2499 list_for_each_entry_safe(cmd
, n
, &dev
->cmd_queue
, queue
) {
2500 list_del(&cmd
->queue
);
2504 kfree(dev
->in_frame
);
2505 usb_free_urb(dev
->in_urb
);
2506 kfree(dev
->out_frame
);
2507 usb_free_urb(dev
->out_urb
);
2510 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2513 static struct usb_driver pn533_driver
= {
2515 .probe
= pn533_probe
,
2516 .disconnect
= pn533_disconnect
,
2517 .id_table
= pn533_table
,
2520 module_usb_driver(pn533_driver
);
2522 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2523 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2524 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2525 MODULE_VERSION(VERSION
);
2526 MODULE_LICENSE("GPL");