2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/nfc.h>
30 #include <linux/netdevice.h>
31 #include <net/nfc/nfc.h>
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
41 #define SONY_VENDOR_ID 0x054c
42 #define PASORI_PRODUCT_ID 0x02e1
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
47 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
52 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
55 NFC_PROTO_ISO14443_MASK | \
56 NFC_PROTO_NFC_DEP_MASK)
58 static const struct usb_device_id pn533_table
[] = {
59 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
60 .idVendor
= PN533_VENDOR_ID
,
61 .idProduct
= PN533_PRODUCT_ID
,
62 .driver_info
= PN533_DEVICE_STD
,
64 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
65 .idVendor
= SCM_VENDOR_ID
,
66 .idProduct
= SCL3711_PRODUCT_ID
,
67 .driver_info
= PN533_DEVICE_STD
,
69 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
70 .idVendor
= SONY_VENDOR_ID
,
71 .idProduct
= PASORI_PRODUCT_ID
,
72 .driver_info
= PN533_DEVICE_PASORI
,
76 MODULE_DEVICE_TABLE(usb
, pn533_table
);
78 /* How much time we spend listening for initiators */
79 #define PN533_LISTEN_TIME 2
81 /* frame definitions */
82 #define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
85 #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87 #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
93 #define PN533_FRAME_MAX_PAYLOAD_LEN 263
95 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
97 #define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
99 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
100 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
103 #define PN533_SOF 0x00FF
105 /* frame identifier: in/out/error */
106 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
107 #define PN533_DIR_OUT 0xD4
108 #define PN533_DIR_IN 0xD5
111 #define PN533_FRAME_CMD(f) (f->data[1])
112 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
113 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
115 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
116 #define PN533_CMD_RF_CONFIGURATION 0x32
117 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
118 #define PN533_CMD_IN_COMM_THRU 0x42
119 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
120 #define PN533_CMD_IN_ATR 0x50
121 #define PN533_CMD_IN_RELEASE 0x52
122 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
124 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
125 #define PN533_CMD_TG_GET_DATA 0x86
126 #define PN533_CMD_TG_SET_DATA 0x8e
127 #define PN533_CMD_UNDEF 0xff
129 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
131 /* PN533 Return codes */
132 #define PN533_CMD_RET_MASK 0x3F
133 #define PN533_CMD_MI_MASK 0x40
134 #define PN533_CMD_RET_SUCCESS 0x00
138 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
139 u8
*params
, int params_len
);
141 typedef int (*pn533_send_async_complete_t
) (struct pn533
*dev
, void *arg
,
142 struct sk_buff
*resp
);
144 /* structs for pn533 commands */
146 /* PN533_CMD_GET_FIRMWARE_VERSION */
147 struct pn533_fw_version
{
154 /* PN533_CMD_RF_CONFIGURATION */
155 #define PN533_CFGITEM_TIMING 0x02
156 #define PN533_CFGITEM_MAX_RETRIES 0x05
157 #define PN533_CFGITEM_PASORI 0x82
159 #define PN533_CONFIG_TIMING_102 0xb
160 #define PN533_CONFIG_TIMING_204 0xc
161 #define PN533_CONFIG_TIMING_409 0xd
162 #define PN533_CONFIG_TIMING_819 0xe
164 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
165 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
167 struct pn533_config_max_retries
{
170 u8 mx_rty_passive_act
;
173 struct pn533_config_timing
{
179 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
181 /* felica commands opcode */
182 #define PN533_FELICA_OPC_SENSF_REQ 0
183 #define PN533_FELICA_OPC_SENSF_RES 1
184 /* felica SENSF_REQ parameters */
185 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
186 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
187 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
188 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
190 /* type B initiator_data values */
191 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
192 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
193 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
195 union pn533_cmd_poll_initdata
{
208 /* Poll modulations */
210 PN533_POLL_MOD_106KBPS_A
,
211 PN533_POLL_MOD_212KBPS_FELICA
,
212 PN533_POLL_MOD_424KBPS_FELICA
,
213 PN533_POLL_MOD_106KBPS_JEWEL
,
214 PN533_POLL_MOD_847KBPS_B
,
217 __PN533_POLL_MOD_AFTER_LAST
,
219 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
221 struct pn533_poll_modulations
{
225 union pn533_cmd_poll_initdata initiator_data
;
230 const struct pn533_poll_modulations poll_mod
[] = {
231 [PN533_POLL_MOD_106KBPS_A
] = {
238 [PN533_POLL_MOD_212KBPS_FELICA
] = {
242 .initiator_data
.felica
= {
243 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
244 .sc
= PN533_FELICA_SENSF_SC_ALL
,
245 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
251 [PN533_POLL_MOD_424KBPS_FELICA
] = {
255 .initiator_data
.felica
= {
256 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
257 .sc
= PN533_FELICA_SENSF_SC_ALL
,
258 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
264 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
271 [PN533_POLL_MOD_847KBPS_B
] = {
275 .initiator_data
.type_b
= {
276 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
278 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
283 [PN533_LISTEN_MOD
] = {
288 /* PN533_CMD_IN_ATR */
290 struct pn533_cmd_activate_response
{
302 struct pn533_cmd_jump_dep_response
{
316 /* PN533_TG_INIT_AS_TARGET */
317 #define PN533_INIT_TARGET_PASSIVE 0x1
318 #define PN533_INIT_TARGET_DEP 0x2
320 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
321 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
322 #define PN533_INIT_TARGET_RESP_DEP 0x4
325 struct usb_device
*udev
;
326 struct usb_interface
*interface
;
327 struct nfc_dev
*nfc_dev
;
330 struct pn533_frame
*out_frame
;
334 struct sk_buff_head resp_q
;
336 struct workqueue_struct
*wq
;
337 struct work_struct cmd_work
;
338 struct work_struct cmd_complete_work
;
339 struct work_struct poll_work
;
340 struct work_struct mi_work
;
341 struct work_struct tg_work
;
342 struct timer_list listen_timer
;
343 struct pn533_frame
*wq_in_frame
;
347 pn533_cmd_complete_t cmd_complete
;
348 void *cmd_complete_arg
;
349 void *cmd_complete_mi_arg
;
350 struct mutex cmd_lock
;
353 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
357 u32 listen_protocols
;
362 u8 tgt_available_prots
;
368 struct list_head cmd_queue
;
373 struct list_head queue
;
376 struct sk_buff
*resp
;
388 /* The rule: value + checksum = 0 */
389 static inline u8
pn533_checksum(u8 value
)
394 /* The rule: sum(data elements) + checksum = 0 */
395 static u8
pn533_data_checksum(u8
*data
, int datalen
)
400 for (i
= 0; i
< datalen
; i
++)
403 return pn533_checksum(sum
);
406 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
409 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
410 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
411 PN533_FRAME_CMD(frame
) = cmd
;
415 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
417 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
419 PN533_FRAME_CHECKSUM(frame
) =
420 pn533_data_checksum(frame
->data
, frame
->datalen
);
422 PN533_FRAME_POSTAMBLE(frame
) = 0;
425 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
429 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
432 checksum
= pn533_checksum(frame
->datalen
);
433 if (checksum
!= frame
->datalen_checksum
)
436 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
437 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
443 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
445 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
448 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
454 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
456 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
460 static void pn533_wq_cmd_complete(struct work_struct
*work
)
462 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
463 struct pn533_frame
*in_frame
;
466 in_frame
= dev
->wq_in_frame
;
468 if (dev
->wq_in_error
)
469 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
472 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
473 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
474 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
476 if (rc
!= -EINPROGRESS
)
477 queue_work(dev
->wq
, &dev
->cmd_work
);
480 static void pn533_recv_response(struct urb
*urb
)
482 struct pn533
*dev
= urb
->context
;
483 struct pn533_frame
*in_frame
;
485 dev
->wq_in_frame
= NULL
;
487 switch (urb
->status
) {
492 nfc_dev_dbg(&dev
->interface
->dev
,
493 "The urb has been canceled (status %d)",
495 dev
->wq_in_error
= urb
->status
;
500 nfc_dev_err(&dev
->interface
->dev
,
501 "Urb failure (status %d)", urb
->status
);
502 dev
->wq_in_error
= urb
->status
;
506 in_frame
= dev
->in_urb
->transfer_buffer
;
508 nfc_dev_dbg(&dev
->interface
->dev
, "Received a frame.");
509 print_hex_dump(KERN_DEBUG
, "PN533 RX: ", DUMP_PREFIX_NONE
, 16, 1,
510 in_frame
, PN533_FRAME_SIZE(in_frame
), false);
512 if (!pn533_rx_frame_is_valid(in_frame
)) {
513 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
514 dev
->wq_in_error
= -EIO
;
518 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
519 nfc_dev_err(&dev
->interface
->dev
,
520 "It it not the response to the last command");
521 dev
->wq_in_error
= -EIO
;
525 dev
->wq_in_error
= 0;
526 dev
->wq_in_frame
= in_frame
;
529 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
532 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
534 dev
->in_urb
->complete
= pn533_recv_response
;
536 return usb_submit_urb(dev
->in_urb
, flags
);
539 static void pn533_recv_ack(struct urb
*urb
)
541 struct pn533
*dev
= urb
->context
;
542 struct pn533_frame
*in_frame
;
545 switch (urb
->status
) {
550 nfc_dev_dbg(&dev
->interface
->dev
,
551 "The urb has been stopped (status %d)",
553 dev
->wq_in_error
= urb
->status
;
558 nfc_dev_err(&dev
->interface
->dev
,
559 "Urb failure (status %d)", urb
->status
);
560 dev
->wq_in_error
= urb
->status
;
564 in_frame
= dev
->in_urb
->transfer_buffer
;
566 if (!pn533_rx_frame_is_ack(in_frame
)) {
567 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
568 dev
->wq_in_error
= -EIO
;
572 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
574 nfc_dev_err(&dev
->interface
->dev
,
575 "usb_submit_urb failed with result %d", rc
);
576 dev
->wq_in_error
= rc
;
583 dev
->wq_in_frame
= NULL
;
584 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
587 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
589 dev
->in_urb
->complete
= pn533_recv_ack
;
591 return usb_submit_urb(dev
->in_urb
, flags
);
594 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
596 u8 ack
[PN533_FRAME_ACK_SIZE
] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
597 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
600 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
602 dev
->out_urb
->transfer_buffer
= ack
;
603 dev
->out_urb
->transfer_buffer_length
= sizeof(ack
);
604 rc
= usb_submit_urb(dev
->out_urb
, flags
);
609 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
610 struct pn533_frame
*out_frame
,
611 struct pn533_frame
*in_frame
,
613 pn533_cmd_complete_t cmd_complete
,
618 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
619 dev
->cmd_complete
= cmd_complete
;
620 dev
->cmd_complete_arg
= arg
;
622 dev
->out_urb
->transfer_buffer
= out_frame
;
623 dev
->out_urb
->transfer_buffer_length
=
624 PN533_FRAME_SIZE(out_frame
);
626 dev
->in_urb
->transfer_buffer
= in_frame
;
627 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
629 print_hex_dump(KERN_DEBUG
, "PN533 TX: ", DUMP_PREFIX_NONE
, 16, 1,
630 out_frame
, PN533_FRAME_SIZE(out_frame
), false);
632 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
636 rc
= pn533_submit_urb_for_ack(dev
, GFP_KERNEL
);
643 usb_unlink_urb(dev
->out_urb
);
647 static void pn533_build_cmd_frame(u8 cmd_code
, struct sk_buff
*skb
)
649 struct pn533_frame
*frame
;
650 /* payload is already there, just update datalen */
651 int payload_len
= skb
->len
;
653 skb_push(skb
, PN533_FRAME_HEADER_LEN
);
654 skb_put(skb
, PN533_FRAME_TAIL_LEN
);
656 frame
= (struct pn533_frame
*)skb
->data
;
658 pn533_tx_frame_init(frame
, cmd_code
);
659 frame
->datalen
+= payload_len
;
660 pn533_tx_frame_finish(frame
);
663 struct pn533_send_async_complete_arg
{
664 pn533_send_async_complete_t complete_cb
;
665 void *complete_cb_context
;
666 struct sk_buff
*resp
;
670 static int pn533_send_async_complete(struct pn533
*dev
, void *_arg
, u8
*params
,
673 struct pn533_send_async_complete_arg
*arg
= _arg
;
675 struct sk_buff
*req
= arg
->req
;
676 struct sk_buff
*resp
= arg
->resp
;
678 struct pn533_frame
*frame
= (struct pn533_frame
*)resp
->data
;
683 if (params_len
< 0) {
684 arg
->complete_cb(dev
, arg
->complete_cb_context
,
685 ERR_PTR(params_len
));
691 skb_put(resp
, PN533_FRAME_SIZE(frame
));
692 skb_pull(resp
, PN533_FRAME_HEADER_LEN
);
693 skb_trim(resp
, resp
->len
- PN533_FRAME_TAIL_LEN
);
695 rc
= arg
->complete_cb(dev
, arg
->complete_cb_context
, resp
);
702 static int __pn533_send_async(struct pn533
*dev
, u8 cmd_code
,
703 struct sk_buff
*req
, struct sk_buff
*resp
,
705 pn533_send_async_complete_t complete_cb
,
706 void *complete_cb_context
)
708 struct pn533_cmd
*cmd
;
709 struct pn533_send_async_complete_arg
*arg
;
712 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x", cmd_code
);
714 arg
= kzalloc(sizeof(arg
), GFP_KERNEL
);
718 arg
->complete_cb
= complete_cb
;
719 arg
->complete_cb_context
= complete_cb_context
;
723 pn533_build_cmd_frame(cmd_code
, req
);
725 mutex_lock(&dev
->cmd_lock
);
727 if (!dev
->cmd_pending
) {
728 rc
= __pn533_send_cmd_frame_async(dev
,
729 (struct pn533_frame
*)req
->data
,
730 (struct pn533_frame
*)resp
->data
,
731 resp_len
, pn533_send_async_complete
,
736 dev
->cmd_pending
= 1;
740 nfc_dev_dbg(&dev
->interface
->dev
, "%s Queueing command 0x%x", __func__
,
743 cmd
= kzalloc(sizeof(struct pn533_cmd
), GFP_KERNEL
);
749 INIT_LIST_HEAD(&cmd
->queue
);
750 cmd
->cmd_code
= cmd_code
;
755 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
762 mutex_unlock(&dev
->cmd_lock
);
766 static int pn533_send_data_async(struct pn533
*dev
, u8 cmd_code
,
768 pn533_send_async_complete_t complete_cb
,
769 void *complete_cb_context
)
771 struct sk_buff
*resp
;
773 int resp_len
= PN533_FRAME_HEADER_LEN
+
774 PN533_FRAME_MAX_PAYLOAD_LEN
+
775 PN533_FRAME_TAIL_LEN
;
777 resp
= nfc_alloc_recv_skb(resp_len
, GFP_KERNEL
);
781 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
, resp_len
, complete_cb
,
782 complete_cb_context
);
789 static int pn533_send_cmd_async(struct pn533
*dev
, u8 cmd_code
,
791 pn533_send_async_complete_t complete_cb
,
792 void *complete_cb_context
)
794 struct sk_buff
*resp
;
797 resp
= alloc_skb(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
801 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
,
802 PN533_NORMAL_FRAME_MAX_LEN
,
803 complete_cb
, complete_cb_context
);
811 * pn533_send_cmd_direct_async
813 * The function sends a piority cmd directly to the chip omiting the cmd
814 * queue. It's intended to be used by chaining mechanism of received responses
815 * where the host has to request every single chunk of data before scheduling
816 * next cmd from the queue.
818 static int pn533_send_cmd_direct_async(struct pn533
*dev
, u8 cmd_code
,
820 pn533_send_async_complete_t complete_cb
,
821 void *complete_cb_context
)
823 struct pn533_send_async_complete_arg
*arg
;
824 struct sk_buff
*resp
;
826 int resp_len
= PN533_FRAME_HEADER_LEN
+
827 PN533_FRAME_MAX_PAYLOAD_LEN
+
828 PN533_FRAME_TAIL_LEN
;
830 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
834 arg
= kzalloc(sizeof(arg
), GFP_KERNEL
);
840 arg
->complete_cb
= complete_cb
;
841 arg
->complete_cb_context
= complete_cb_context
;
845 pn533_build_cmd_frame(cmd_code
, req
);
847 rc
= __pn533_send_cmd_frame_async(dev
, (struct pn533_frame
*)req
->data
,
848 (struct pn533_frame
*)resp
->data
,
849 resp_len
, pn533_send_async_complete
,
859 static void pn533_wq_cmd(struct work_struct
*work
)
861 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
862 struct pn533_cmd
*cmd
;
864 mutex_lock(&dev
->cmd_lock
);
866 if (list_empty(&dev
->cmd_queue
)) {
867 dev
->cmd_pending
= 0;
868 mutex_unlock(&dev
->cmd_lock
);
872 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
874 list_del(&cmd
->queue
);
876 mutex_unlock(&dev
->cmd_lock
);
878 __pn533_send_cmd_frame_async(dev
,
879 (struct pn533_frame
*)cmd
->req
->data
,
880 (struct pn533_frame
*)cmd
->resp
->data
,
881 PN533_NORMAL_FRAME_MAX_LEN
,
882 pn533_send_async_complete
,
888 struct pn533_sync_cmd_response
{
889 struct sk_buff
*resp
;
890 struct completion done
;
893 static int pn533_send_sync_complete(struct pn533
*dev
, void *_arg
,
894 struct sk_buff
*resp
)
896 struct pn533_sync_cmd_response
*arg
= _arg
;
899 complete(&arg
->done
);
904 /* pn533_send_cmd_sync
906 * Please note the req parameter is freed inside the function to
907 * limit a number of return value interpretations by the caller.
909 * 1. negative in case of error during TX path -> req should be freed
911 * 2. negative in case of error during RX path -> req should not be freed
912 * as it's been already freed at the begining of RX path by
915 * 3. valid pointer in case of succesfult RX path
917 * A caller has to check a return value with IS_ERR macro. If the test pass,
918 * the returned pointer is valid.
921 static struct sk_buff
*pn533_send_cmd_sync(struct pn533
*dev
, u8 cmd_code
,
925 struct pn533_sync_cmd_response arg
;
927 init_completion(&arg
.done
);
929 rc
= pn533_send_cmd_async(dev
, cmd_code
, req
,
930 pn533_send_sync_complete
, &arg
);
936 wait_for_completion(&arg
.done
);
941 static void pn533_send_complete(struct urb
*urb
)
943 struct pn533
*dev
= urb
->context
;
945 switch (urb
->status
) {
950 nfc_dev_dbg(&dev
->interface
->dev
,
951 "The urb has been stopped (status %d)",
956 nfc_dev_err(&dev
->interface
->dev
,
957 "Urb failure (status %d)", urb
->status
);
961 static struct sk_buff
*pn533_alloc_skb(unsigned int size
)
965 skb
= alloc_skb(PN533_FRAME_HEADER_LEN
+
967 PN533_FRAME_TAIL_LEN
, GFP_KERNEL
);
970 skb_reserve(skb
, PN533_FRAME_HEADER_LEN
);
975 struct pn533_target_type_a
{
983 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
984 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
985 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
987 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
988 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
990 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
991 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
993 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
994 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
995 #define PN533_TYPE_A_SEL_PROT_DEP 2
996 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
998 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
1004 if (target_data_len
< sizeof(struct pn533_target_type_a
))
1007 /* The lenght check of nfcid[] and ats[] are not being performed because
1008 the values are not being used */
1010 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1011 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
1012 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
1014 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1015 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1016 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1017 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1020 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1021 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
1027 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1030 struct pn533_target_type_a
*tgt_type_a
;
1032 tgt_type_a
= (struct pn533_target_type_a
*)tgt_data
;
1034 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
1037 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
1038 case PN533_TYPE_A_SEL_PROT_MIFARE
:
1039 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
1041 case PN533_TYPE_A_SEL_PROT_ISO14443
:
1042 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
1044 case PN533_TYPE_A_SEL_PROT_DEP
:
1045 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1047 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
1048 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
1049 NFC_PROTO_NFC_DEP_MASK
;
1053 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
1054 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
1055 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
1056 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
1061 struct pn533_target_felica
{
1070 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1071 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1073 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
1074 int target_data_len
)
1076 if (target_data_len
< sizeof(struct pn533_target_felica
))
1079 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
1085 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1088 struct pn533_target_felica
*tgt_felica
;
1090 tgt_felica
= (struct pn533_target_felica
*)tgt_data
;
1092 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
1095 if ((tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
) &&
1096 (tgt_felica
->nfcid2
[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2
))
1097 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1099 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
1101 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
1102 nfc_tgt
->sensf_res_len
= 9;
1107 struct pn533_target_jewel
{
1112 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
1113 int target_data_len
)
1118 if (target_data_len
< sizeof(struct pn533_target_jewel
))
1121 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1122 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
1123 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
1125 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1126 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1127 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1128 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1134 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1137 struct pn533_target_jewel
*tgt_jewel
;
1139 tgt_jewel
= (struct pn533_target_jewel
*)tgt_data
;
1141 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
1144 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
1145 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
1146 nfc_tgt
->nfcid1_len
= 4;
1147 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
1152 struct pn533_type_b_prot_info
{
1158 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1159 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1160 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1162 struct pn533_type_b_sens_res
{
1166 struct pn533_type_b_prot_info prot_info
;
1169 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1171 struct pn533_target_type_b
{
1172 struct pn533_type_b_sens_res sensb_res
;
1177 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
1178 int target_data_len
)
1180 if (target_data_len
< sizeof(struct pn533_target_type_b
))
1183 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
1186 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
1187 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
1193 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1196 struct pn533_target_type_b
*tgt_type_b
;
1198 tgt_type_b
= (struct pn533_target_type_b
*)tgt_data
;
1200 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
1203 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
1208 static int pn533_target_found(struct pn533
*dev
, u8 tg
, u8
*tgdata
,
1211 struct nfc_target nfc_tgt
;
1214 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
1215 dev
->poll_mod_curr
);
1220 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1222 switch (dev
->poll_mod_curr
) {
1223 case PN533_POLL_MOD_106KBPS_A
:
1224 rc
= pn533_target_found_type_a(&nfc_tgt
, tgdata
, tgdata_len
);
1226 case PN533_POLL_MOD_212KBPS_FELICA
:
1227 case PN533_POLL_MOD_424KBPS_FELICA
:
1228 rc
= pn533_target_found_felica(&nfc_tgt
, tgdata
, tgdata_len
);
1230 case PN533_POLL_MOD_106KBPS_JEWEL
:
1231 rc
= pn533_target_found_jewel(&nfc_tgt
, tgdata
, tgdata_len
);
1233 case PN533_POLL_MOD_847KBPS_B
:
1234 rc
= pn533_target_found_type_b(&nfc_tgt
, tgdata
, tgdata_len
);
1237 nfc_dev_err(&dev
->interface
->dev
,
1238 "Unknown current poll modulation");
1245 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1246 nfc_dev_dbg(&dev
->interface
->dev
,
1247 "The Tg found doesn't have the desired protocol");
1251 nfc_dev_dbg(&dev
->interface
->dev
,
1252 "Target found - supported protocols: 0x%x",
1253 nfc_tgt
.supported_protocols
);
1255 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1257 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1262 static inline void pn533_poll_next_mod(struct pn533
*dev
)
1264 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1267 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
1269 dev
->poll_mod_count
= 0;
1272 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
1274 dev
->poll_mod_active
[dev
->poll_mod_count
] =
1275 (struct pn533_poll_modulations
*)&poll_mod
[mod_index
];
1276 dev
->poll_mod_count
++;
1279 static void pn533_poll_create_mod_list(struct pn533
*dev
,
1280 u32 im_protocols
, u32 tm_protocols
)
1282 pn533_poll_reset_mod_list(dev
);
1284 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) ||
1285 (im_protocols
& NFC_PROTO_ISO14443_MASK
) ||
1286 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
1287 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1289 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
1290 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1291 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1292 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1295 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1296 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1298 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1299 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1302 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
1305 static int pn533_start_poll_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1307 u8 nbtg
, tg
, *tgdata
;
1310 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1312 nbtg
= resp
->data
[0];
1314 tgdata
= &resp
->data
[2];
1315 tgdata_len
= resp
->len
- 2; /* nbtg + tg */
1318 rc
= pn533_target_found(dev
, tg
, tgdata
, tgdata_len
);
1320 /* We must stop the poll after a valid target found */
1322 pn533_poll_reset_mod_list(dev
);
1330 static struct sk_buff
*pn533_alloc_poll_tg_frame(u8
*gbytes
, size_t gbytes_len
)
1332 struct sk_buff
*skb
;
1333 u8
*felica
, *nfcid3
, *gb
;
1335 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1336 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1337 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1338 0xff, 0xff}; /* System code */
1340 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1342 0x40}; /* SEL_RES for DEP */
1344 unsigned int skb_len
= 36 + /* mode (1), mifare (6),
1345 felica (18), nfcid3 (10), gb_len (1) */
1349 skb
= pn533_alloc_skb(skb_len
);
1353 /* DEP support only */
1354 *skb_put(skb
, 1) |= PN533_INIT_TARGET_DEP
;
1357 memcpy(skb_put(skb
, 6), mifare_params
, 6);
1360 felica
= skb_put(skb
, 18);
1361 memcpy(felica
, felica_params
, 18);
1362 get_random_bytes(felica
+ 2, 6);
1365 nfcid3
= skb_put(skb
, 10);
1366 memset(nfcid3
, 0, 10);
1367 memcpy(nfcid3
, felica
, 8);
1370 *skb_put(skb
, 1) = gbytes_len
;
1372 gb
= skb_put(skb
, gbytes_len
);
1373 memcpy(gb
, gbytes
, gbytes_len
);
1376 *skb_put(skb
, 1) = 0;
1381 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1382 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1383 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1384 struct sk_buff
*resp
)
1388 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1391 return PTR_ERR(resp
);
1393 status
= resp
->data
[0];
1394 skb_pull(resp
, sizeof(status
));
1397 nfc_tm_deactivated(dev
->nfc_dev
);
1399 dev_kfree_skb(resp
);
1403 return nfc_tm_data_received(dev
->nfc_dev
, resp
);
1406 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1408 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1410 struct sk_buff
*skb
;
1413 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1415 skb
= pn533_alloc_skb(0);
1419 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_GET_DATA
, skb
,
1420 pn533_tm_get_data_complete
, NULL
);
1428 #define ATR_REQ_GB_OFFSET 17
1429 static int pn533_init_target_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1431 u8 mode
, *cmd
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1435 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1437 if (resp
->len
< ATR_REQ_GB_OFFSET
+ 1)
1440 mode
= resp
->data
[0];
1441 cmd
= &resp
->data
[1];
1443 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x len %d\n",
1446 if ((mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
) ==
1447 PN533_INIT_TARGET_RESP_ACTIVE
)
1448 comm_mode
= NFC_COMM_ACTIVE
;
1450 if ((mode
& PN533_INIT_TARGET_RESP_DEP
) == 0) /* Only DEP supported */
1453 gb
= cmd
+ ATR_REQ_GB_OFFSET
;
1454 gb_len
= resp
->len
- (ATR_REQ_GB_OFFSET
+ 1);
1456 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1457 comm_mode
, gb
, gb_len
);
1459 nfc_dev_err(&dev
->interface
->dev
,
1460 "Error when signaling target activation");
1465 queue_work(dev
->wq
, &dev
->tg_work
);
1470 static void pn533_listen_mode_timer(unsigned long data
)
1472 struct pn533
*dev
= (struct pn533
*)data
;
1474 nfc_dev_dbg(&dev
->interface
->dev
, "Listen mode timeout");
1476 /* An ack will cancel the last issued command (poll) */
1477 pn533_send_ack(dev
, GFP_ATOMIC
);
1479 dev
->cancel_listen
= 1;
1481 pn533_poll_next_mod(dev
);
1483 queue_work(dev
->wq
, &dev
->poll_work
);
1486 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1487 struct sk_buff
*resp
)
1489 struct pn533_poll_modulations
*cur_mod
;
1492 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1497 nfc_dev_err(&dev
->interface
->dev
, "%s Poll complete error %d",
1500 if (rc
== -ENOENT
) {
1501 if (dev
->poll_mod_count
!= 0)
1505 } else if (rc
< 0) {
1506 nfc_dev_err(&dev
->interface
->dev
,
1507 "Error %d when running poll", rc
);
1512 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1514 if (cur_mod
->len
== 0) { /* Target mode */
1515 del_timer(&dev
->listen_timer
);
1516 rc
= pn533_init_target_complete(dev
, resp
);
1520 /* Initiator mode */
1521 rc
= pn533_start_poll_complete(dev
, resp
);
1525 pn533_poll_next_mod(dev
);
1526 queue_work(dev
->wq
, &dev
->poll_work
);
1529 dev_kfree_skb(resp
);
1533 nfc_dev_err(&dev
->interface
->dev
, "Polling operation has been stopped");
1535 pn533_poll_reset_mod_list(dev
);
1536 dev
->poll_protocols
= 0;
1540 static struct sk_buff
*pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1543 struct sk_buff
*skb
;
1545 skb
= pn533_alloc_skb(mod
->len
);
1549 memcpy(skb_put(skb
, mod
->len
), &mod
->data
, mod
->len
);
1554 static int pn533_send_poll_frame(struct pn533
*dev
)
1556 struct pn533_poll_modulations
*mod
;
1557 struct sk_buff
*skb
;
1561 mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1563 nfc_dev_dbg(&dev
->interface
->dev
, "%s mod len %d\n",
1564 __func__
, mod
->len
);
1566 if (mod
->len
== 0) { /* Listen mode */
1567 cmd_code
= PN533_CMD_TG_INIT_AS_TARGET
;
1568 skb
= pn533_alloc_poll_tg_frame(dev
->gb
, dev
->gb_len
);
1569 } else { /* Polling mode */
1570 cmd_code
= PN533_CMD_IN_LIST_PASSIVE_TARGET
;
1571 skb
= pn533_alloc_poll_in_frame(mod
);
1575 nfc_dev_err(&dev
->interface
->dev
, "Failed to allocate skb.");
1579 rc
= pn533_send_cmd_async(dev
, cmd_code
, skb
, pn533_poll_complete
,
1583 nfc_dev_err(&dev
->interface
->dev
, "Polling loop error %d", rc
);
1589 static void pn533_wq_poll(struct work_struct
*work
)
1591 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
);
1592 struct pn533_poll_modulations
*cur_mod
;
1595 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1597 nfc_dev_dbg(&dev
->interface
->dev
,
1598 "%s cancel_listen %d modulation len %d",
1599 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1601 if (dev
->cancel_listen
== 1) {
1602 dev
->cancel_listen
= 0;
1603 usb_kill_urb(dev
->in_urb
);
1606 rc
= pn533_send_poll_frame(dev
);
1610 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1611 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1616 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1617 u32 im_protocols
, u32 tm_protocols
)
1619 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1621 nfc_dev_dbg(&dev
->interface
->dev
,
1622 "%s: im protocols 0x%x tm protocols 0x%x",
1623 __func__
, im_protocols
, tm_protocols
);
1625 if (dev
->tgt_active_prot
) {
1626 nfc_dev_err(&dev
->interface
->dev
,
1627 "Cannot poll with a target already activated");
1631 if (dev
->tgt_mode
) {
1632 nfc_dev_err(&dev
->interface
->dev
,
1633 "Cannot poll while already being activated");
1638 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1639 if (dev
->gb
== NULL
)
1643 dev
->poll_mod_curr
= 0;
1644 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1645 dev
->poll_protocols
= im_protocols
;
1646 dev
->listen_protocols
= tm_protocols
;
1648 return pn533_send_poll_frame(dev
);
1651 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1653 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1655 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1657 del_timer(&dev
->listen_timer
);
1659 if (!dev
->poll_mod_count
) {
1660 nfc_dev_dbg(&dev
->interface
->dev
,
1661 "Polling operation was not running");
1665 /* An ack will cancel the last issued command (poll) */
1666 pn533_send_ack(dev
, GFP_KERNEL
);
1668 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1669 usb_kill_urb(dev
->in_urb
);
1671 pn533_poll_reset_mod_list(dev
);
1674 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1676 struct pn533_cmd_activate_response
*rsp
;
1680 struct sk_buff
*skb
;
1681 struct sk_buff
*resp
;
1683 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1685 skb
= pn533_alloc_skb(sizeof(u8
) * 2); /*TG + Next*/
1689 *skb_put(skb
, sizeof(u8
)) = 1; /* TG */
1690 *skb_put(skb
, sizeof(u8
)) = 0; /* Next */
1692 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_ATR
, skb
);
1694 return PTR_ERR(resp
);
1696 rsp
= (struct pn533_cmd_activate_response
*)resp
->data
;
1697 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1698 if (rc
!= PN533_CMD_RET_SUCCESS
)
1699 dev_kfree_skb(resp
);
1702 /* ATR_RES general bytes are located at offset 16 */
1703 gt_len
= resp
->len
- 16;
1704 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, rsp
->gt
, gt_len
);
1706 dev_kfree_skb(resp
);
1710 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1711 struct nfc_target
*target
, u32 protocol
)
1713 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1716 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1719 if (dev
->poll_mod_count
) {
1720 nfc_dev_err(&dev
->interface
->dev
,
1721 "Cannot activate while polling");
1725 if (dev
->tgt_active_prot
) {
1726 nfc_dev_err(&dev
->interface
->dev
,
1727 "There is already an active target");
1731 if (!dev
->tgt_available_prots
) {
1732 nfc_dev_err(&dev
->interface
->dev
,
1733 "There is no available target to activate");
1737 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1738 nfc_dev_err(&dev
->interface
->dev
,
1739 "Target doesn't support requested proto %u",
1744 if (protocol
== NFC_PROTO_NFC_DEP
) {
1745 rc
= pn533_activate_target_nfcdep(dev
);
1747 nfc_dev_err(&dev
->interface
->dev
,
1748 "Activating target with DEP failed %d", rc
);
1753 dev
->tgt_active_prot
= protocol
;
1754 dev
->tgt_available_prots
= 0;
1759 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1760 struct nfc_target
*target
)
1762 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1764 struct sk_buff
*skb
;
1765 struct sk_buff
*resp
;
1769 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1771 if (!dev
->tgt_active_prot
) {
1772 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1776 dev
->tgt_active_prot
= 0;
1777 skb_queue_purge(&dev
->resp_q
);
1779 skb
= pn533_alloc_skb(sizeof(u8
));
1783 *skb_put(skb
, 1) = 1; /* TG*/
1785 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_RELEASE
, skb
);
1789 rc
= resp
->data
[0] & PN533_CMD_RET_MASK
;
1790 if (rc
!= PN533_CMD_RET_SUCCESS
)
1791 nfc_dev_err(&dev
->interface
->dev
,
1792 "Error 0x%x when releasing the target", rc
);
1794 dev_kfree_skb(resp
);
1799 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1800 struct sk_buff
*resp
)
1802 struct pn533_cmd_jump_dep_response
*rsp
;
1805 u8 active
= *(u8
*)arg
;
1810 return PTR_ERR(resp
);
1812 if (dev
->tgt_available_prots
&&
1813 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1814 nfc_dev_err(&dev
->interface
->dev
,
1815 "The target does not support DEP");
1820 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1822 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1823 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1824 nfc_dev_err(&dev
->interface
->dev
,
1825 "Bringing DEP link up failed %d", rc
);
1829 if (!dev
->tgt_available_prots
) {
1830 struct nfc_target nfc_target
;
1832 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1834 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1835 nfc_target
.nfcid1_len
= 10;
1836 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1837 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1841 dev
->tgt_available_prots
= 0;
1844 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1846 /* ATR_RES general bytes are located at offset 17 */
1847 target_gt_len
= resp
->len
- 17;
1848 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1849 rsp
->gt
, target_gt_len
);
1851 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1852 dev
->nfc_dev
->targets
[0].idx
,
1853 !active
, NFC_RF_INITIATOR
);
1856 dev_kfree_skb(resp
);
1860 static int pn533_mod_to_baud(struct pn533
*dev
)
1862 switch (dev
->poll_mod_curr
) {
1863 case PN533_POLL_MOD_106KBPS_A
:
1865 case PN533_POLL_MOD_212KBPS_FELICA
:
1867 case PN533_POLL_MOD_424KBPS_FELICA
:
1874 #define PASSIVE_DATA_LEN 5
1875 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1876 u8 comm_mode
, u8
*gb
, size_t gb_len
)
1878 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1879 struct sk_buff
*skb
;
1880 int rc
, baud
, skb_len
;
1883 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1885 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1887 if (dev
->poll_mod_count
) {
1888 nfc_dev_err(&dev
->interface
->dev
,
1889 "Cannot bring the DEP link up while polling");
1893 if (dev
->tgt_active_prot
) {
1894 nfc_dev_err(&dev
->interface
->dev
,
1895 "There is already an active target");
1899 baud
= pn533_mod_to_baud(dev
);
1901 nfc_dev_err(&dev
->interface
->dev
,
1902 "Invalid curr modulation %d", dev
->poll_mod_curr
);
1906 skb_len
= 3 + gb_len
; /* ActPass + BR + Next */
1907 if (comm_mode
== NFC_COMM_PASSIVE
)
1908 skb_len
+= PASSIVE_DATA_LEN
;
1910 skb
= pn533_alloc_skb(skb_len
);
1914 *skb_put(skb
, 1) = !comm_mode
; /* ActPass */
1915 *skb_put(skb
, 1) = baud
; /* Baud rate */
1917 next
= skb_put(skb
, 1); /* Next */
1920 if (comm_mode
== NFC_COMM_PASSIVE
&& baud
> 0) {
1921 memcpy(skb_put(skb
, PASSIVE_DATA_LEN
), passive_data
,
1926 if (gb
!= NULL
&& gb_len
> 0) {
1927 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
1928 *next
|= 4; /* We have some Gi */
1933 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
1941 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
1942 pn533_in_dep_link_up_complete
, arg
);
1952 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1954 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1956 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1958 pn533_poll_reset_mod_list(dev
);
1960 if (dev
->tgt_mode
|| dev
->tgt_active_prot
) {
1961 pn533_send_ack(dev
, GFP_KERNEL
);
1962 usb_kill_urb(dev
->in_urb
);
1965 dev
->tgt_active_prot
= 0;
1968 skb_queue_purge(&dev
->resp_q
);
1973 struct pn533_data_exchange_arg
{
1974 data_exchange_cb_t cb
;
1978 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1980 struct sk_buff
*skb
, *tmp
, *t
;
1981 unsigned int skb_len
= 0, tmp_len
= 0;
1983 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1985 if (skb_queue_empty(&dev
->resp_q
))
1988 if (skb_queue_len(&dev
->resp_q
) == 1) {
1989 skb
= skb_dequeue(&dev
->resp_q
);
1993 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1994 skb_len
+= tmp
->len
;
1996 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1999 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
2003 skb_put(skb
, skb_len
);
2005 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
2006 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
2007 tmp_len
+= tmp
->len
;
2011 skb_queue_purge(&dev
->resp_q
);
2016 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
2017 struct sk_buff
*resp
)
2019 struct pn533_data_exchange_arg
*arg
= _arg
;
2020 struct sk_buff
*skb
;
2024 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2031 status
= resp
->data
[0];
2032 ret
= status
& PN533_CMD_RET_MASK
;
2033 mi
= status
& PN533_CMD_MI_MASK
;
2035 skb_pull(resp
, sizeof(status
));
2037 if (ret
!= PN533_CMD_RET_SUCCESS
) {
2038 nfc_dev_err(&dev
->interface
->dev
,
2039 "PN533 reported error %d when exchanging data",
2045 skb_queue_tail(&dev
->resp_q
, resp
);
2048 dev
->cmd_complete_mi_arg
= arg
;
2049 queue_work(dev
->wq
, &dev
->mi_work
);
2050 return -EINPROGRESS
;
2053 skb
= pn533_build_response(dev
);
2057 arg
->cb(arg
->cb_context
, skb
, 0);
2062 dev_kfree_skb(resp
);
2064 skb_queue_purge(&dev
->resp_q
);
2065 arg
->cb(arg
->cb_context
, NULL
, rc
);
2070 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2071 struct nfc_target
*target
, struct sk_buff
*skb
,
2072 data_exchange_cb_t cb
, void *cb_context
)
2074 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2075 struct pn533_data_exchange_arg
*arg
= NULL
;
2078 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2080 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2081 /* TODO: Implement support to multi-part data exchange */
2082 nfc_dev_err(&dev
->interface
->dev
,
2083 "Data length greater than the max allowed: %d",
2084 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2089 if (!dev
->tgt_active_prot
) {
2090 nfc_dev_err(&dev
->interface
->dev
,
2091 "Can't exchange data if there is no active target");
2096 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2103 arg
->cb_context
= cb_context
;
2105 switch (dev
->device_type
) {
2106 case PN533_DEVICE_PASORI
:
2107 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2108 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_COMM_THRU
,
2110 pn533_data_exchange_complete
,
2116 *skb_push(skb
, sizeof(u8
)) = 1; /*TG*/
2118 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_DATA_EXCHANGE
,
2119 skb
, pn533_data_exchange_complete
,
2125 if (rc
< 0) /* rc from send_async */
2136 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2137 struct sk_buff
*resp
)
2141 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2144 return PTR_ERR(resp
);
2146 status
= resp
->data
[0];
2148 dev_kfree_skb(resp
);
2151 nfc_tm_deactivated(dev
->nfc_dev
);
2158 queue_work(dev
->wq
, &dev
->tg_work
);
2163 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2165 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2168 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2170 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2171 nfc_dev_err(&dev
->interface
->dev
,
2172 "Data length greater than the max allowed: %d",
2173 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2177 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_DATA
, skb
,
2178 pn533_tm_send_complete
, NULL
);
2185 static void pn533_wq_mi_recv(struct work_struct
*work
)
2187 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
2189 struct sk_buff
*skb
;
2192 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2194 skb
= pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
);
2198 switch (dev
->device_type
) {
2199 case PN533_DEVICE_PASORI
:
2200 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2201 rc
= pn533_send_cmd_direct_async(dev
,
2202 PN533_CMD_IN_COMM_THRU
,
2204 pn533_data_exchange_complete
,
2205 dev
->cmd_complete_mi_arg
);
2210 *skb_put(skb
, sizeof(u8
)) = 1; /*TG*/
2212 rc
= pn533_send_cmd_direct_async(dev
,
2213 PN533_CMD_IN_DATA_EXCHANGE
,
2215 pn533_data_exchange_complete
,
2216 dev
->cmd_complete_mi_arg
);
2221 if (rc
== 0) /* success */
2224 nfc_dev_err(&dev
->interface
->dev
,
2225 "Error %d when trying to perform data_exchange", rc
);
2228 kfree(dev
->cmd_complete_arg
);
2231 pn533_send_ack(dev
, GFP_KERNEL
);
2232 queue_work(dev
->wq
, &dev
->cmd_work
);
2235 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2238 struct sk_buff
*skb
;
2239 struct sk_buff
*resp
;
2243 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2245 skb_len
= sizeof(cfgitem
) + cfgdata_len
; /* cfgitem + cfgdata */
2247 skb
= pn533_alloc_skb(skb_len
);
2251 *skb_put(skb
, sizeof(cfgitem
)) = cfgitem
;
2252 memcpy(skb_put(skb
, cfgdata_len
), cfgdata
, cfgdata_len
);
2254 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_RF_CONFIGURATION
, skb
);
2256 return PTR_ERR(resp
);
2258 dev_kfree_skb(resp
);
2262 static int pn533_get_firmware_version(struct pn533
*dev
,
2263 struct pn533_fw_version
*fv
)
2265 struct sk_buff
*skb
;
2266 struct sk_buff
*resp
;
2268 skb
= pn533_alloc_skb(0);
2272 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_GET_FIRMWARE_VERSION
, skb
);
2274 return PTR_ERR(resp
);
2276 fv
->ic
= resp
->data
[0];
2277 fv
->ver
= resp
->data
[1];
2278 fv
->rev
= resp
->data
[2];
2279 fv
->support
= resp
->data
[3];
2281 dev_kfree_skb(resp
);
2285 static int pn533_fw_reset(struct pn533
*dev
)
2287 struct sk_buff
*skb
;
2288 struct sk_buff
*resp
;
2290 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2292 skb
= pn533_alloc_skb(sizeof(u8
));
2296 *skb_put(skb
, sizeof(u8
)) = 0x1;
2298 resp
= pn533_send_cmd_sync(dev
, 0x18, skb
);
2300 return PTR_ERR(resp
);
2302 dev_kfree_skb(resp
);
2307 static struct nfc_ops pn533_nfc_ops
= {
2310 .dep_link_up
= pn533_dep_link_up
,
2311 .dep_link_down
= pn533_dep_link_down
,
2312 .start_poll
= pn533_start_poll
,
2313 .stop_poll
= pn533_stop_poll
,
2314 .activate_target
= pn533_activate_target
,
2315 .deactivate_target
= pn533_deactivate_target
,
2316 .im_transceive
= pn533_transceive
,
2317 .tm_send
= pn533_tm_send
,
2320 static int pn533_setup(struct pn533
*dev
)
2322 struct pn533_config_max_retries max_retries
;
2323 struct pn533_config_timing timing
;
2324 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2327 switch (dev
->device_type
) {
2328 case PN533_DEVICE_STD
:
2329 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
2330 max_retries
.mx_rty_psl
= 2;
2331 max_retries
.mx_rty_passive_act
=
2332 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2334 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2335 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_204
;
2336 timing
.dep_timeout
= PN533_CONFIG_TIMING_409
;
2340 case PN533_DEVICE_PASORI
:
2341 max_retries
.mx_rty_atr
= 0x2;
2342 max_retries
.mx_rty_psl
= 0x1;
2343 max_retries
.mx_rty_passive_act
=
2344 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2346 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2347 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2348 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2353 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2358 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2359 (u8
*)&max_retries
, sizeof(max_retries
));
2361 nfc_dev_err(&dev
->interface
->dev
,
2362 "Error on setting MAX_RETRIES config");
2367 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2368 (u8
*)&timing
, sizeof(timing
));
2370 nfc_dev_err(&dev
->interface
->dev
,
2371 "Error on setting RF timings");
2375 switch (dev
->device_type
) {
2376 case PN533_DEVICE_STD
:
2379 case PN533_DEVICE_PASORI
:
2380 pn533_fw_reset(dev
);
2382 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2385 nfc_dev_err(&dev
->interface
->dev
,
2386 "Error while settings PASORI config");
2390 pn533_fw_reset(dev
);
2398 static int pn533_probe(struct usb_interface
*interface
,
2399 const struct usb_device_id
*id
)
2401 struct pn533_fw_version fw_ver
;
2403 struct usb_host_interface
*iface_desc
;
2404 struct usb_endpoint_descriptor
*endpoint
;
2405 int in_endpoint
= 0;
2406 int out_endpoint
= 0;
2411 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2415 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2416 dev
->interface
= interface
;
2417 mutex_init(&dev
->cmd_lock
);
2419 iface_desc
= interface
->cur_altsetting
;
2420 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2421 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2423 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
2424 in_endpoint
= endpoint
->bEndpointAddress
;
2426 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
2427 out_endpoint
= endpoint
->bEndpointAddress
;
2430 if (!in_endpoint
|| !out_endpoint
) {
2431 nfc_dev_err(&interface
->dev
,
2432 "Could not find bulk-in or bulk-out endpoint");
2437 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2438 dev
->out_frame
= kmalloc(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
2439 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2441 if (!dev
->out_frame
|| !dev
->in_urb
|| !dev
->out_urb
)
2444 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
2445 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
2446 NULL
, 0, NULL
, dev
);
2447 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
2448 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
2449 NULL
, 0, pn533_send_complete
, dev
);
2451 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd
);
2452 INIT_WORK(&dev
->cmd_complete_work
, pn533_wq_cmd_complete
);
2453 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
2454 INIT_WORK(&dev
->tg_work
, pn533_wq_tg_get_data
);
2455 INIT_WORK(&dev
->poll_work
, pn533_wq_poll
);
2456 dev
->wq
= alloc_ordered_workqueue("pn533", 0);
2457 if (dev
->wq
== NULL
)
2460 init_timer(&dev
->listen_timer
);
2461 dev
->listen_timer
.data
= (unsigned long) dev
;
2462 dev
->listen_timer
.function
= pn533_listen_mode_timer
;
2464 skb_queue_head_init(&dev
->resp_q
);
2466 INIT_LIST_HEAD(&dev
->cmd_queue
);
2468 usb_set_intfdata(interface
, dev
);
2470 memset(&fw_ver
, 0, sizeof(fw_ver
));
2471 rc
= pn533_get_firmware_version(dev
, &fw_ver
);
2475 nfc_dev_info(&dev
->interface
->dev
,
2476 "NXP PN533 firmware ver %d.%d now attached",
2477 fw_ver
.ver
, fw_ver
.rev
);
2479 dev
->device_type
= id
->driver_info
;
2480 switch (dev
->device_type
) {
2481 case PN533_DEVICE_STD
:
2482 protocols
= PN533_ALL_PROTOCOLS
;
2485 case PN533_DEVICE_PASORI
:
2486 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2490 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2496 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2497 PN533_FRAME_HEADER_LEN
+
2498 PN533_CMD_DATAEXCH_HEAD_LEN
,
2499 PN533_FRAME_TAIL_LEN
);
2503 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
2504 nfc_set_drvdata(dev
->nfc_dev
, dev
);
2506 rc
= nfc_register_device(dev
->nfc_dev
);
2510 rc
= pn533_setup(dev
);
2512 goto unregister_nfc_dev
;
2517 nfc_unregister_device(dev
->nfc_dev
);
2520 nfc_free_device(dev
->nfc_dev
);
2523 destroy_workqueue(dev
->wq
);
2525 usb_free_urb(dev
->in_urb
);
2526 kfree(dev
->out_frame
);
2527 usb_free_urb(dev
->out_urb
);
2532 static void pn533_disconnect(struct usb_interface
*interface
)
2535 struct pn533_cmd
*cmd
, *n
;
2537 dev
= usb_get_intfdata(interface
);
2538 usb_set_intfdata(interface
, NULL
);
2540 nfc_unregister_device(dev
->nfc_dev
);
2541 nfc_free_device(dev
->nfc_dev
);
2543 usb_kill_urb(dev
->in_urb
);
2544 usb_kill_urb(dev
->out_urb
);
2546 destroy_workqueue(dev
->wq
);
2548 skb_queue_purge(&dev
->resp_q
);
2550 del_timer(&dev
->listen_timer
);
2552 list_for_each_entry_safe(cmd
, n
, &dev
->cmd_queue
, queue
) {
2553 list_del(&cmd
->queue
);
2557 usb_free_urb(dev
->in_urb
);
2558 kfree(dev
->out_frame
);
2559 usb_free_urb(dev
->out_urb
);
2562 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2565 static struct usb_driver pn533_driver
= {
2567 .probe
= pn533_probe
,
2568 .disconnect
= pn533_disconnect
,
2569 .id_table
= pn533_table
,
2572 module_usb_driver(pn533_driver
);
2574 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2575 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2576 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2577 MODULE_VERSION(VERSION
);
2578 MODULE_LICENSE("GPL");