2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/nfc.h>
30 #include <linux/netdevice.h>
31 #include <net/nfc/nfc.h>
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
41 #define SONY_VENDOR_ID 0x054c
42 #define PASORI_PRODUCT_ID 0x02e1
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
47 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
52 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
55 NFC_PROTO_ISO14443_MASK | \
56 NFC_PROTO_NFC_DEP_MASK)
58 static const struct usb_device_id pn533_table
[] = {
59 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
60 .idVendor
= PN533_VENDOR_ID
,
61 .idProduct
= PN533_PRODUCT_ID
,
62 .driver_info
= PN533_DEVICE_STD
,
64 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
65 .idVendor
= SCM_VENDOR_ID
,
66 .idProduct
= SCL3711_PRODUCT_ID
,
67 .driver_info
= PN533_DEVICE_STD
,
69 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
70 .idVendor
= SONY_VENDOR_ID
,
71 .idProduct
= PASORI_PRODUCT_ID
,
72 .driver_info
= PN533_DEVICE_PASORI
,
76 MODULE_DEVICE_TABLE(usb
, pn533_table
);
78 /* How much time we spend listening for initiators */
79 #define PN533_LISTEN_TIME 2
81 /* frame definitions */
82 #define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
85 #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87 #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
93 #define PN533_FRAME_MAX_PAYLOAD_LEN 263
95 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
97 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
102 #define PN533_SOF 0x00FF
104 /* frame identifier: in/out/error */
105 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106 #define PN533_DIR_OUT 0xD4
107 #define PN533_DIR_IN 0xD5
110 #define PN533_FRAME_CMD(f) (f->data[1])
111 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
114 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115 #define PN533_CMD_RF_CONFIGURATION 0x32
116 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
117 #define PN533_CMD_IN_COMM_THRU 0x42
118 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119 #define PN533_CMD_IN_ATR 0x50
120 #define PN533_CMD_IN_RELEASE 0x52
121 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
123 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
124 #define PN533_CMD_TG_GET_DATA 0x86
125 #define PN533_CMD_TG_SET_DATA 0x8e
126 #define PN533_CMD_UNDEF 0xff
128 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
130 /* PN533 Return codes */
131 #define PN533_CMD_RET_MASK 0x3F
132 #define PN533_CMD_MI_MASK 0x40
133 #define PN533_CMD_RET_SUCCESS 0x00
137 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
138 u8
*params
, int params_len
);
140 typedef int (*pn533_send_async_complete_t
) (struct pn533
*dev
, void *arg
,
141 struct sk_buff
*resp
);
143 /* structs for pn533 commands */
145 /* PN533_CMD_GET_FIRMWARE_VERSION */
146 struct pn533_fw_version
{
153 /* PN533_CMD_RF_CONFIGURATION */
154 #define PN533_CFGITEM_TIMING 0x02
155 #define PN533_CFGITEM_MAX_RETRIES 0x05
156 #define PN533_CFGITEM_PASORI 0x82
158 #define PN533_CONFIG_TIMING_102 0xb
159 #define PN533_CONFIG_TIMING_204 0xc
160 #define PN533_CONFIG_TIMING_409 0xd
161 #define PN533_CONFIG_TIMING_819 0xe
163 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
166 struct pn533_config_max_retries
{
169 u8 mx_rty_passive_act
;
172 struct pn533_config_timing
{
178 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
180 /* felica commands opcode */
181 #define PN533_FELICA_OPC_SENSF_REQ 0
182 #define PN533_FELICA_OPC_SENSF_RES 1
183 /* felica SENSF_REQ parameters */
184 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
189 /* type B initiator_data values */
190 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
194 union pn533_cmd_poll_initdata
{
207 /* Poll modulations */
209 PN533_POLL_MOD_106KBPS_A
,
210 PN533_POLL_MOD_212KBPS_FELICA
,
211 PN533_POLL_MOD_424KBPS_FELICA
,
212 PN533_POLL_MOD_106KBPS_JEWEL
,
213 PN533_POLL_MOD_847KBPS_B
,
216 __PN533_POLL_MOD_AFTER_LAST
,
218 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
220 struct pn533_poll_modulations
{
224 union pn533_cmd_poll_initdata initiator_data
;
229 const struct pn533_poll_modulations poll_mod
[] = {
230 [PN533_POLL_MOD_106KBPS_A
] = {
237 [PN533_POLL_MOD_212KBPS_FELICA
] = {
241 .initiator_data
.felica
= {
242 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
243 .sc
= PN533_FELICA_SENSF_SC_ALL
,
244 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
250 [PN533_POLL_MOD_424KBPS_FELICA
] = {
254 .initiator_data
.felica
= {
255 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
256 .sc
= PN533_FELICA_SENSF_SC_ALL
,
257 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
263 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
270 [PN533_POLL_MOD_847KBPS_B
] = {
274 .initiator_data
.type_b
= {
275 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
282 [PN533_LISTEN_MOD
] = {
287 /* PN533_CMD_IN_ATR */
289 struct pn533_cmd_activate_response
{
301 struct pn533_cmd_jump_dep_response
{
315 /* PN533_TG_INIT_AS_TARGET */
316 #define PN533_INIT_TARGET_PASSIVE 0x1
317 #define PN533_INIT_TARGET_DEP 0x2
319 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321 #define PN533_INIT_TARGET_RESP_DEP 0x4
324 struct usb_device
*udev
;
325 struct usb_interface
*interface
;
326 struct nfc_dev
*nfc_dev
;
329 struct pn533_frame
*out_frame
;
332 struct pn533_frame
*in_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
;
374 struct pn533_frame
*out_frame
;
375 struct pn533_frame
*in_frame
;
379 struct sk_buff
*resp
;
380 pn533_cmd_complete_t cmd_complete
;
392 /* The rule: value + checksum = 0 */
393 static inline u8
pn533_checksum(u8 value
)
398 /* The rule: sum(data elements) + checksum = 0 */
399 static u8
pn533_data_checksum(u8
*data
, int datalen
)
404 for (i
= 0; i
< datalen
; i
++)
407 return pn533_checksum(sum
);
411 * pn533_tx_frame_ack - create a ack frame
412 * @frame: The frame to be set as ack
414 * Ack is different type of standard frame. As a standard frame, it has
415 * preamble and start_frame. However the checksum of this frame must fail,
416 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
417 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
418 * After datalen_checksum field, the postamble is placed.
420 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
423 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
425 frame
->datalen_checksum
= 0xFF;
426 /* data[0] is used as postamble */
430 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
433 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
434 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
435 PN533_FRAME_CMD(frame
) = cmd
;
439 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
441 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
443 PN533_FRAME_CHECKSUM(frame
) =
444 pn533_data_checksum(frame
->data
, frame
->datalen
);
446 PN533_FRAME_POSTAMBLE(frame
) = 0;
449 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
453 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
456 checksum
= pn533_checksum(frame
->datalen
);
457 if (checksum
!= frame
->datalen_checksum
)
460 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
461 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
467 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
469 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
472 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
478 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
480 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
484 static void pn533_wq_cmd_complete(struct work_struct
*work
)
486 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
487 struct pn533_frame
*in_frame
;
490 in_frame
= dev
->wq_in_frame
;
492 if (dev
->wq_in_error
)
493 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
496 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
497 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
498 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
500 if (rc
!= -EINPROGRESS
)
501 queue_work(dev
->wq
, &dev
->cmd_work
);
504 static void pn533_recv_response(struct urb
*urb
)
506 struct pn533
*dev
= urb
->context
;
507 struct pn533_frame
*in_frame
;
509 dev
->wq_in_frame
= NULL
;
511 switch (urb
->status
) {
518 nfc_dev_dbg(&dev
->interface
->dev
,
519 "Urb shutting down with status: %d", urb
->status
);
520 dev
->wq_in_error
= urb
->status
;
523 nfc_dev_err(&dev
->interface
->dev
,
524 "Nonzero urb status received: %d", urb
->status
);
525 dev
->wq_in_error
= urb
->status
;
529 in_frame
= dev
->in_urb
->transfer_buffer
;
531 print_hex_dump(KERN_DEBUG
, "PN533 RX: ", DUMP_PREFIX_NONE
, 16, 1,
532 in_frame
, PN533_FRAME_SIZE(in_frame
), false);
534 if (!pn533_rx_frame_is_valid(in_frame
)) {
535 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
536 dev
->wq_in_error
= -EIO
;
540 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
541 nfc_dev_err(&dev
->interface
->dev
,
542 "It it not the response to the last command");
543 dev
->wq_in_error
= -EIO
;
547 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
548 dev
->wq_in_error
= 0;
549 dev
->wq_in_frame
= in_frame
;
552 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
555 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
557 dev
->in_urb
->complete
= pn533_recv_response
;
559 return usb_submit_urb(dev
->in_urb
, flags
);
562 static void pn533_recv_ack(struct urb
*urb
)
564 struct pn533
*dev
= urb
->context
;
565 struct pn533_frame
*in_frame
;
568 switch (urb
->status
) {
575 nfc_dev_dbg(&dev
->interface
->dev
,
576 "Urb shutting down with status: %d", urb
->status
);
577 dev
->wq_in_error
= urb
->status
;
580 nfc_dev_err(&dev
->interface
->dev
,
581 "Nonzero urb status received: %d", urb
->status
);
582 dev
->wq_in_error
= urb
->status
;
586 in_frame
= dev
->in_urb
->transfer_buffer
;
588 if (!pn533_rx_frame_is_ack(in_frame
)) {
589 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
590 dev
->wq_in_error
= -EIO
;
594 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
596 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
598 nfc_dev_err(&dev
->interface
->dev
,
599 "usb_submit_urb failed with result %d", rc
);
600 dev
->wq_in_error
= rc
;
607 dev
->wq_in_frame
= NULL
;
608 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
611 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
613 dev
->in_urb
->complete
= pn533_recv_ack
;
615 return usb_submit_urb(dev
->in_urb
, flags
);
618 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
622 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
624 pn533_tx_frame_ack(dev
->out_frame
);
626 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
627 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
628 rc
= usb_submit_urb(dev
->out_urb
, flags
);
633 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
634 struct pn533_frame
*out_frame
,
635 struct pn533_frame
*in_frame
,
637 pn533_cmd_complete_t cmd_complete
,
642 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
643 PN533_FRAME_CMD(out_frame
));
645 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
646 dev
->cmd_complete
= cmd_complete
;
647 dev
->cmd_complete_arg
= arg
;
649 dev
->out_urb
->transfer_buffer
= out_frame
;
650 dev
->out_urb
->transfer_buffer_length
=
651 PN533_FRAME_SIZE(out_frame
);
653 dev
->in_urb
->transfer_buffer
= in_frame
;
654 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
656 print_hex_dump(KERN_DEBUG
, "PN533 TX: ", DUMP_PREFIX_NONE
, 16, 1,
657 out_frame
, PN533_FRAME_SIZE(out_frame
), false);
659 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
663 rc
= pn533_submit_urb_for_ack(dev
, GFP_KERNEL
);
670 usb_unlink_urb(dev
->out_urb
);
674 static void pn533_build_cmd_frame(u8 cmd_code
, struct sk_buff
*skb
)
676 struct pn533_frame
*frame
;
677 /* payload is already there, just update datalen */
678 int payload_len
= skb
->len
;
680 skb_push(skb
, PN533_FRAME_HEADER_LEN
);
681 skb_put(skb
, PN533_FRAME_TAIL_LEN
);
683 frame
= (struct pn533_frame
*)skb
->data
;
685 pn533_tx_frame_init(frame
, cmd_code
);
686 frame
->datalen
+= payload_len
;
687 pn533_tx_frame_finish(frame
);
690 struct pn533_send_async_complete_arg
{
691 pn533_send_async_complete_t complete_cb
;
692 void *complete_cb_context
;
693 struct sk_buff
*resp
;
697 static int pn533_send_async_complete(struct pn533
*dev
, void *_arg
, u8
*params
,
700 struct pn533_send_async_complete_arg
*arg
= _arg
;
702 struct sk_buff
*req
= arg
->req
;
703 struct sk_buff
*resp
= arg
->resp
;
705 struct pn533_frame
*frame
= (struct pn533_frame
*)resp
->data
;
710 if (params_len
< 0) {
711 nfc_dev_err(&dev
->interface
->dev
,
712 "Error %d when starting as a target",
715 arg
->complete_cb(dev
, arg
->complete_cb_context
,
716 ERR_PTR(params_len
));
722 skb_put(resp
, PN533_FRAME_SIZE(frame
));
723 skb_pull(resp
, PN533_FRAME_HEADER_LEN
);
724 skb_trim(resp
, resp
->len
- PN533_FRAME_TAIL_LEN
);
726 rc
= arg
->complete_cb(dev
, arg
->complete_cb_context
, resp
);
733 static int __pn533_send_async(struct pn533
*dev
, u8 cmd_code
,
734 struct sk_buff
*req
, struct sk_buff
*resp
,
736 pn533_send_async_complete_t complete_cb
,
737 void *complete_cb_context
)
739 struct pn533_cmd
*cmd
;
740 struct pn533_send_async_complete_arg
*arg
;
743 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
745 arg
= kzalloc(sizeof(arg
), GFP_KERNEL
);
749 arg
->complete_cb
= complete_cb
;
750 arg
->complete_cb_context
= complete_cb_context
;
754 pn533_build_cmd_frame(cmd_code
, req
);
756 mutex_lock(&dev
->cmd_lock
);
758 if (!dev
->cmd_pending
) {
759 rc
= __pn533_send_cmd_frame_async(dev
,
760 (struct pn533_frame
*)req
->data
,
761 (struct pn533_frame
*)resp
->data
,
762 resp_len
, pn533_send_async_complete
,
767 dev
->cmd_pending
= 1;
771 nfc_dev_dbg(&dev
->interface
->dev
, "%s Queueing command", __func__
);
773 cmd
= kzalloc(sizeof(struct pn533_cmd
), GFP_KERNEL
);
779 INIT_LIST_HEAD(&cmd
->queue
);
780 cmd
->cmd_code
= cmd_code
;
785 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
792 mutex_unlock(&dev
->cmd_lock
);
796 static int pn533_send_data_async(struct pn533
*dev
, u8 cmd_code
,
798 pn533_send_async_complete_t complete_cb
,
799 void *complete_cb_context
)
801 struct sk_buff
*resp
;
803 int resp_len
= PN533_FRAME_HEADER_LEN
+
804 PN533_FRAME_MAX_PAYLOAD_LEN
+
805 PN533_FRAME_TAIL_LEN
;
807 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
809 resp
= nfc_alloc_recv_skb(resp_len
, GFP_KERNEL
);
813 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
, resp_len
, complete_cb
,
814 complete_cb_context
);
821 static int pn533_send_cmd_async(struct pn533
*dev
, u8 cmd_code
,
823 pn533_send_async_complete_t complete_cb
,
824 void *complete_cb_context
)
826 struct sk_buff
*resp
;
829 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
831 resp
= alloc_skb(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
835 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
,
836 PN533_NORMAL_FRAME_MAX_LEN
,
837 complete_cb
, complete_cb_context
);
845 * pn533_send_cmd_direct_async
847 * The function sends a piority cmd directly to the chip omiting the cmd
848 * queue. It's intended to be used by chaining mechanism of received responses
849 * where the host has to request every single chunk of data before scheduling
850 * next cmd from the queue.
852 static int pn533_send_cmd_direct_async(struct pn533
*dev
, u8 cmd_code
,
854 pn533_send_async_complete_t complete_cb
,
855 void *complete_cb_context
)
857 struct pn533_send_async_complete_arg
*arg
;
858 struct sk_buff
*resp
;
860 int resp_len
= PN533_FRAME_HEADER_LEN
+
861 PN533_FRAME_MAX_PAYLOAD_LEN
+
862 PN533_FRAME_TAIL_LEN
;
864 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
866 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
870 arg
= kzalloc(sizeof(arg
), GFP_KERNEL
);
876 arg
->complete_cb
= complete_cb
;
877 arg
->complete_cb_context
= complete_cb_context
;
881 pn533_build_cmd_frame(cmd_code
, req
);
883 rc
= __pn533_send_cmd_frame_async(dev
, (struct pn533_frame
*)req
->data
,
884 (struct pn533_frame
*)resp
->data
,
885 resp_len
, pn533_send_async_complete
,
895 static void pn533_wq_cmd(struct work_struct
*work
)
897 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
898 struct pn533_cmd
*cmd
;
900 mutex_lock(&dev
->cmd_lock
);
902 if (list_empty(&dev
->cmd_queue
)) {
903 dev
->cmd_pending
= 0;
904 mutex_unlock(&dev
->cmd_lock
);
908 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
910 list_del(&cmd
->queue
);
912 mutex_unlock(&dev
->cmd_lock
);
914 if (cmd
->cmd_code
!= PN533_CMD_UNDEF
)
915 __pn533_send_cmd_frame_async(dev
,
916 (struct pn533_frame
*)cmd
->req
->data
,
917 (struct pn533_frame
*)cmd
->resp
->data
,
918 PN533_NORMAL_FRAME_MAX_LEN
,
919 pn533_send_async_complete
,
922 __pn533_send_cmd_frame_async(dev
, cmd
->out_frame
, cmd
->in_frame
,
924 cmd
->cmd_complete
, cmd
->arg
);
929 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
930 struct pn533_frame
*out_frame
,
931 struct pn533_frame
*in_frame
,
933 pn533_cmd_complete_t cmd_complete
,
936 struct pn533_cmd
*cmd
;
939 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
941 mutex_lock(&dev
->cmd_lock
);
943 if (!dev
->cmd_pending
) {
944 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
945 in_frame_len
, cmd_complete
,
948 dev
->cmd_pending
= 1;
953 nfc_dev_dbg(&dev
->interface
->dev
, "%s Queueing command", __func__
);
955 cmd
= kzalloc(sizeof(struct pn533_cmd
), GFP_KERNEL
);
961 INIT_LIST_HEAD(&cmd
->queue
);
962 cmd
->out_frame
= out_frame
;
963 cmd
->in_frame
= in_frame
;
964 cmd
->in_frame_len
= in_frame_len
;
965 cmd
->cmd_code
= PN533_CMD_UNDEF
;
966 cmd
->cmd_complete
= cmd_complete
;
969 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
972 mutex_unlock(&dev
->cmd_lock
);
977 struct pn533_sync_cmd_response
{
978 struct sk_buff
*resp
;
979 struct completion done
;
982 static int pn533_send_sync_complete(struct pn533
*dev
, void *_arg
,
983 struct sk_buff
*resp
)
985 struct pn533_sync_cmd_response
*arg
= _arg
;
987 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
990 complete(&arg
->done
);
995 /* pn533_send_cmd_sync
997 * Please note the req parameter is freed inside the function to
998 * limit a number of return value interpretations by the caller.
1000 * 1. negative in case of error during TX path -> req should be freed
1002 * 2. negative in case of error during RX path -> req should not be freed
1003 * as it's been already freed at the begining of RX path by
1004 * async_complete_cb.
1006 * 3. valid pointer in case of succesfult RX path
1008 * A caller has to check a return value with IS_ERR macro. If the test pass,
1009 * the returned pointer is valid.
1012 static struct sk_buff
*pn533_send_cmd_sync(struct pn533
*dev
, u8 cmd_code
,
1013 struct sk_buff
*req
)
1016 struct pn533_sync_cmd_response arg
;
1018 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1020 init_completion(&arg
.done
);
1022 rc
= pn533_send_cmd_async(dev
, cmd_code
, req
,
1023 pn533_send_sync_complete
, &arg
);
1029 wait_for_completion(&arg
.done
);
1034 static void pn533_send_complete(struct urb
*urb
)
1036 struct pn533
*dev
= urb
->context
;
1038 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1040 switch (urb
->status
) {
1047 nfc_dev_dbg(&dev
->interface
->dev
,
1048 "Urb shutting down with status: %d", urb
->status
);
1051 nfc_dev_dbg(&dev
->interface
->dev
,
1052 "Nonzero urb status received: %d", urb
->status
);
1056 static struct sk_buff
*pn533_alloc_skb(unsigned int size
)
1058 struct sk_buff
*skb
;
1060 skb
= alloc_skb(PN533_FRAME_HEADER_LEN
+
1062 PN533_FRAME_TAIL_LEN
, GFP_KERNEL
);
1065 skb_reserve(skb
, PN533_FRAME_HEADER_LEN
);
1070 struct pn533_target_type_a
{
1078 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1079 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1080 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1082 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1083 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1085 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1086 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1088 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1089 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1090 #define PN533_TYPE_A_SEL_PROT_DEP 2
1091 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1093 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
1094 int target_data_len
)
1099 if (target_data_len
< sizeof(struct pn533_target_type_a
))
1102 /* The lenght check of nfcid[] and ats[] are not being performed because
1103 the values are not being used */
1105 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1106 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
1107 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
1109 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1110 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1111 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1112 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1115 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1116 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
1122 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1125 struct pn533_target_type_a
*tgt_type_a
;
1127 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
1129 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
1132 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
1133 case PN533_TYPE_A_SEL_PROT_MIFARE
:
1134 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
1136 case PN533_TYPE_A_SEL_PROT_ISO14443
:
1137 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
1139 case PN533_TYPE_A_SEL_PROT_DEP
:
1140 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1142 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
1143 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
1144 NFC_PROTO_NFC_DEP_MASK
;
1148 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
1149 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
1150 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
1151 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
1156 struct pn533_target_felica
{
1165 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1166 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1168 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
1169 int target_data_len
)
1171 if (target_data_len
< sizeof(struct pn533_target_felica
))
1174 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
1180 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1183 struct pn533_target_felica
*tgt_felica
;
1185 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
1187 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
1190 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
1191 tgt_felica
->nfcid2
[1] ==
1192 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
1193 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1195 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
1197 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
1198 nfc_tgt
->sensf_res_len
= 9;
1203 struct pn533_target_jewel
{
1208 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
1209 int target_data_len
)
1214 if (target_data_len
< sizeof(struct pn533_target_jewel
))
1217 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1218 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
1219 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
1221 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1222 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1223 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1224 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1230 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1233 struct pn533_target_jewel
*tgt_jewel
;
1235 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
1237 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
1240 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
1241 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
1242 nfc_tgt
->nfcid1_len
= 4;
1243 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
1248 struct pn533_type_b_prot_info
{
1254 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1255 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1256 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1258 struct pn533_type_b_sens_res
{
1262 struct pn533_type_b_prot_info prot_info
;
1265 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1267 struct pn533_target_type_b
{
1268 struct pn533_type_b_sens_res sensb_res
;
1273 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
1274 int target_data_len
)
1276 if (target_data_len
< sizeof(struct pn533_target_type_b
))
1279 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
1282 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
1283 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
1289 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1292 struct pn533_target_type_b
*tgt_type_b
;
1294 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
1296 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
1299 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
1304 static int pn533_target_found(struct pn533
*dev
, u8 tg
, u8
*tgdata
,
1307 struct nfc_target nfc_tgt
;
1310 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
1311 dev
->poll_mod_curr
);
1316 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1318 switch (dev
->poll_mod_curr
) {
1319 case PN533_POLL_MOD_106KBPS_A
:
1320 rc
= pn533_target_found_type_a(&nfc_tgt
, tgdata
, tgdata_len
);
1322 case PN533_POLL_MOD_212KBPS_FELICA
:
1323 case PN533_POLL_MOD_424KBPS_FELICA
:
1324 rc
= pn533_target_found_felica(&nfc_tgt
, tgdata
, tgdata_len
);
1326 case PN533_POLL_MOD_106KBPS_JEWEL
:
1327 rc
= pn533_target_found_jewel(&nfc_tgt
, tgdata
, tgdata_len
);
1329 case PN533_POLL_MOD_847KBPS_B
:
1330 rc
= pn533_target_found_type_b(&nfc_tgt
, tgdata
, tgdata_len
);
1333 nfc_dev_err(&dev
->interface
->dev
,
1334 "Unknown current poll modulation");
1341 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1342 nfc_dev_dbg(&dev
->interface
->dev
,
1343 "The Tg found doesn't have the desired protocol");
1347 nfc_dev_dbg(&dev
->interface
->dev
,
1348 "Target found - supported protocols: 0x%x",
1349 nfc_tgt
.supported_protocols
);
1351 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1353 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1358 static inline void pn533_poll_next_mod(struct pn533
*dev
)
1360 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1363 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
1365 dev
->poll_mod_count
= 0;
1368 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
1370 dev
->poll_mod_active
[dev
->poll_mod_count
] =
1371 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
1372 dev
->poll_mod_count
++;
1375 static void pn533_poll_create_mod_list(struct pn533
*dev
,
1376 u32 im_protocols
, u32 tm_protocols
)
1378 pn533_poll_reset_mod_list(dev
);
1380 if (im_protocols
& NFC_PROTO_MIFARE_MASK
1381 || im_protocols
& NFC_PROTO_ISO14443_MASK
1382 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
)
1383 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1385 if (im_protocols
& NFC_PROTO_FELICA_MASK
1386 || im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1387 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1388 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1391 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1392 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1394 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1395 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1398 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
1401 static int pn533_start_poll_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1403 u8 nbtg
, tg
, *tgdata
;
1406 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1408 nbtg
= resp
->data
[0];
1410 tgdata
= &resp
->data
[2];
1411 tgdata_len
= resp
->len
- 2; /* nbtg + tg */
1414 rc
= pn533_target_found(dev
, tg
, tgdata
, tgdata_len
);
1416 /* We must stop the poll after a valid target found */
1418 pn533_poll_reset_mod_list(dev
);
1426 static struct sk_buff
*pn533_alloc_poll_tg_frame(u8
*gbytes
, size_t gbytes_len
)
1428 struct sk_buff
*skb
;
1429 u8
*felica
, *nfcid3
, *gb
;
1431 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1432 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1433 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1434 0xff, 0xff}; /* System code */
1436 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1438 0x40}; /* SEL_RES for DEP */
1440 unsigned int skb_len
= 36 + /* mode (1), mifare (6),
1441 felica (18), nfcid3 (10), gb_len (1) */
1445 skb
= pn533_alloc_skb(skb_len
);
1449 /* DEP support only */
1450 *skb_put(skb
, 1) |= PN533_INIT_TARGET_DEP
;
1453 memcpy(skb_put(skb
, 6), mifare_params
, 6);
1456 felica
= skb_put(skb
, 18);
1457 memcpy(felica
, felica_params
, 18);
1458 get_random_bytes(felica
+ 2, 6);
1461 nfcid3
= skb_put(skb
, 10);
1462 memset(nfcid3
, 0, 10);
1463 memcpy(nfcid3
, felica
, 8);
1466 *skb_put(skb
, 1) = gbytes_len
;
1468 gb
= skb_put(skb
, gbytes_len
);
1469 memcpy(gb
, gbytes
, gbytes_len
);
1472 *skb_put(skb
, 1) = 0;
1477 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1478 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1479 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1480 struct sk_buff
*resp
)
1484 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1487 return PTR_ERR(resp
);
1489 status
= resp
->data
[0];
1490 skb_pull(resp
, sizeof(status
));
1493 nfc_tm_deactivated(dev
->nfc_dev
);
1495 dev_kfree_skb(resp
);
1499 return nfc_tm_data_received(dev
->nfc_dev
, resp
);
1502 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1504 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1506 struct sk_buff
*skb
;
1509 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1511 skb
= pn533_alloc_skb(0);
1515 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_GET_DATA
, skb
,
1516 pn533_tm_get_data_complete
, NULL
);
1524 #define ATR_REQ_GB_OFFSET 17
1525 static int pn533_init_target_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1527 u8 mode
, *cmd
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1531 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1533 if (resp
->len
< ATR_REQ_GB_OFFSET
+ 1)
1536 mode
= resp
->data
[0];
1537 cmd
= &resp
->data
[1];
1539 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x len %d\n",
1542 if ((mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
) ==
1543 PN533_INIT_TARGET_RESP_ACTIVE
)
1544 comm_mode
= NFC_COMM_ACTIVE
;
1546 if ((mode
& PN533_INIT_TARGET_RESP_DEP
) == 0) /* Only DEP supported */
1549 gb
= cmd
+ ATR_REQ_GB_OFFSET
;
1550 gb_len
= resp
->len
- (ATR_REQ_GB_OFFSET
+ 1);
1552 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1553 comm_mode
, gb
, gb_len
);
1555 nfc_dev_err(&dev
->interface
->dev
,
1556 "Error when signaling target activation");
1561 queue_work(dev
->wq
, &dev
->tg_work
);
1566 static void pn533_listen_mode_timer(unsigned long data
)
1568 struct pn533
*dev
= (struct pn533
*) data
;
1570 nfc_dev_dbg(&dev
->interface
->dev
, "Listen mode timeout");
1572 /* An ack will cancel the last issued command (poll) */
1573 pn533_send_ack(dev
, GFP_ATOMIC
);
1575 dev
->cancel_listen
= 1;
1577 pn533_poll_next_mod(dev
);
1579 queue_work(dev
->wq
, &dev
->poll_work
);
1582 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1583 struct sk_buff
*resp
)
1585 struct pn533_poll_modulations
*cur_mod
;
1588 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1593 nfc_dev_err(&dev
->interface
->dev
, "%s Poll complete error %d",
1596 if (rc
== -ENOENT
) {
1597 if (dev
->poll_mod_count
!= 0)
1601 } else if (rc
< 0) {
1602 nfc_dev_err(&dev
->interface
->dev
,
1603 "Error %d when running poll", rc
);
1608 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1610 if (cur_mod
->len
== 0) { /* Target mode */
1611 del_timer(&dev
->listen_timer
);
1612 rc
= pn533_init_target_complete(dev
, resp
);
1616 /* Initiator mode */
1617 rc
= pn533_start_poll_complete(dev
, resp
);
1621 pn533_poll_next_mod(dev
);
1622 queue_work(dev
->wq
, &dev
->poll_work
);
1625 dev_kfree_skb(resp
);
1629 nfc_dev_err(&dev
->interface
->dev
, "Polling operation has been stopped");
1631 pn533_poll_reset_mod_list(dev
);
1632 dev
->poll_protocols
= 0;
1636 static struct sk_buff
*pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1639 struct sk_buff
*skb
;
1641 skb
= pn533_alloc_skb(mod
->len
);
1645 memcpy(skb_put(skb
, mod
->len
), &mod
->data
, mod
->len
);
1650 static int pn533_send_poll_frame(struct pn533
*dev
)
1652 struct pn533_poll_modulations
*mod
;
1653 struct sk_buff
*skb
;
1657 mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1659 nfc_dev_dbg(&dev
->interface
->dev
, "%s mod len %d\n",
1660 __func__
, mod
->len
);
1662 if (mod
->len
== 0) { /* Listen mode */
1663 cmd_code
= PN533_CMD_TG_INIT_AS_TARGET
;
1664 skb
= pn533_alloc_poll_tg_frame(dev
->gb
, dev
->gb_len
);
1665 } else { /* Polling mode */
1666 cmd_code
= PN533_CMD_IN_LIST_PASSIVE_TARGET
;
1667 skb
= pn533_alloc_poll_in_frame(mod
);
1671 nfc_dev_err(&dev
->interface
->dev
, "Failed to allocate skb.");
1675 rc
= pn533_send_cmd_async(dev
, cmd_code
, skb
, pn533_poll_complete
,
1679 nfc_dev_err(&dev
->interface
->dev
, "Polling loop error %d", rc
);
1685 static void pn533_wq_poll(struct work_struct
*work
)
1687 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
);
1688 struct pn533_poll_modulations
*cur_mod
;
1691 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1693 nfc_dev_dbg(&dev
->interface
->dev
,
1694 "%s cancel_listen %d modulation len %d",
1695 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1697 if (dev
->cancel_listen
== 1) {
1698 dev
->cancel_listen
= 0;
1699 usb_kill_urb(dev
->in_urb
);
1702 rc
= pn533_send_poll_frame(dev
);
1706 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1707 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1712 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1713 u32 im_protocols
, u32 tm_protocols
)
1715 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1717 nfc_dev_dbg(&dev
->interface
->dev
,
1718 "%s: im protocols 0x%x tm protocols 0x%x",
1719 __func__
, im_protocols
, tm_protocols
);
1721 if (dev
->tgt_active_prot
) {
1722 nfc_dev_err(&dev
->interface
->dev
,
1723 "Cannot poll with a target already activated");
1727 if (dev
->tgt_mode
) {
1728 nfc_dev_err(&dev
->interface
->dev
,
1729 "Cannot poll while already being activated");
1734 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1735 if (dev
->gb
== NULL
)
1739 dev
->poll_mod_curr
= 0;
1740 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1741 dev
->poll_protocols
= im_protocols
;
1742 dev
->listen_protocols
= tm_protocols
;
1744 return pn533_send_poll_frame(dev
);
1747 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1749 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1751 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1753 del_timer(&dev
->listen_timer
);
1755 if (!dev
->poll_mod_count
) {
1756 nfc_dev_dbg(&dev
->interface
->dev
,
1757 "Polling operation was not running");
1761 /* An ack will cancel the last issued command (poll) */
1762 pn533_send_ack(dev
, GFP_KERNEL
);
1764 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1765 usb_kill_urb(dev
->in_urb
);
1767 pn533_poll_reset_mod_list(dev
);
1770 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1772 struct pn533_cmd_activate_response
*rsp
;
1776 struct sk_buff
*skb
;
1777 struct sk_buff
*resp
;
1779 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1781 skb
= pn533_alloc_skb(sizeof(u8
) * 2); /*TG + Next*/
1785 *skb_put(skb
, sizeof(u8
)) = 1; /* TG */
1786 *skb_put(skb
, sizeof(u8
)) = 0; /* Next */
1788 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_ATR
, skb
);
1790 return PTR_ERR(resp
);
1792 rsp
= (struct pn533_cmd_activate_response
*) resp
->data
;
1793 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1794 if (rc
!= PN533_CMD_RET_SUCCESS
)
1795 dev_kfree_skb(resp
);
1798 /* ATR_RES general bytes are located at offset 16 */
1799 gt_len
= resp
->len
- 16;
1800 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, rsp
->gt
, gt_len
);
1802 dev_kfree_skb(resp
);
1806 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1807 struct nfc_target
*target
, u32 protocol
)
1809 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1812 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1815 if (dev
->poll_mod_count
) {
1816 nfc_dev_err(&dev
->interface
->dev
,
1817 "Cannot activate while polling");
1821 if (dev
->tgt_active_prot
) {
1822 nfc_dev_err(&dev
->interface
->dev
,
1823 "There is already an active target");
1827 if (!dev
->tgt_available_prots
) {
1828 nfc_dev_err(&dev
->interface
->dev
,
1829 "There is no available target to activate");
1833 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1834 nfc_dev_err(&dev
->interface
->dev
,
1835 "Target doesn't support requested proto %u",
1840 if (protocol
== NFC_PROTO_NFC_DEP
) {
1841 rc
= pn533_activate_target_nfcdep(dev
);
1843 nfc_dev_err(&dev
->interface
->dev
,
1844 "Activating target with DEP failed %d", rc
);
1849 dev
->tgt_active_prot
= protocol
;
1850 dev
->tgt_available_prots
= 0;
1855 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1856 struct nfc_target
*target
)
1858 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1860 struct sk_buff
*skb
;
1861 struct sk_buff
*resp
;
1865 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1867 if (!dev
->tgt_active_prot
) {
1868 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1872 dev
->tgt_active_prot
= 0;
1873 skb_queue_purge(&dev
->resp_q
);
1875 skb
= pn533_alloc_skb(sizeof(u8
));
1879 *skb_put(skb
, 1) = 1; /* TG*/
1881 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_RELEASE
, skb
);
1885 rc
= resp
->data
[0] & PN533_CMD_RET_MASK
;
1886 if (rc
!= PN533_CMD_RET_SUCCESS
)
1887 nfc_dev_err(&dev
->interface
->dev
,
1888 "Error 0x%x when releasing the target", rc
);
1890 dev_kfree_skb(resp
);
1895 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1896 struct sk_buff
*resp
)
1898 struct pn533_cmd_jump_dep_response
*rsp
;
1901 u8 active
= *(u8
*)arg
;
1906 return PTR_ERR(resp
);
1908 if (dev
->tgt_available_prots
&&
1909 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1910 nfc_dev_err(&dev
->interface
->dev
,
1911 "The target does not support DEP");
1916 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1918 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1919 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1920 nfc_dev_err(&dev
->interface
->dev
,
1921 "Bringing DEP link up failed %d", rc
);
1925 if (!dev
->tgt_available_prots
) {
1926 struct nfc_target nfc_target
;
1928 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1930 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1931 nfc_target
.nfcid1_len
= 10;
1932 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1933 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1937 dev
->tgt_available_prots
= 0;
1940 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1942 /* ATR_RES general bytes are located at offset 17 */
1943 target_gt_len
= resp
->len
- 17;
1944 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1945 rsp
->gt
, target_gt_len
);
1947 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1948 dev
->nfc_dev
->targets
[0].idx
,
1949 !active
, NFC_RF_INITIATOR
);
1952 dev_kfree_skb(resp
);
1956 static int pn533_mod_to_baud(struct pn533
*dev
)
1958 switch (dev
->poll_mod_curr
) {
1959 case PN533_POLL_MOD_106KBPS_A
:
1961 case PN533_POLL_MOD_212KBPS_FELICA
:
1963 case PN533_POLL_MOD_424KBPS_FELICA
:
1970 #define PASSIVE_DATA_LEN 5
1971 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1972 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1974 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1975 struct sk_buff
*skb
;
1976 int rc
, baud
, skb_len
;
1979 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1981 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1983 if (dev
->poll_mod_count
) {
1984 nfc_dev_err(&dev
->interface
->dev
,
1985 "Cannot bring the DEP link up while polling");
1989 if (dev
->tgt_active_prot
) {
1990 nfc_dev_err(&dev
->interface
->dev
,
1991 "There is already an active target");
1995 baud
= pn533_mod_to_baud(dev
);
1997 nfc_dev_err(&dev
->interface
->dev
,
1998 "Invalid curr modulation %d", dev
->poll_mod_curr
);
2002 skb_len
= 3 + gb_len
; /* ActPass + BR + Next */
2003 if (comm_mode
== NFC_COMM_PASSIVE
)
2004 skb_len
+= PASSIVE_DATA_LEN
;
2006 skb
= pn533_alloc_skb(skb_len
);
2010 *skb_put(skb
, 1) = !comm_mode
; /* ActPass */
2011 *skb_put(skb
, 1) = baud
; /* Baud rate */
2013 next
= skb_put(skb
, 1); /* Next */
2016 if (comm_mode
== NFC_COMM_PASSIVE
&& baud
> 0) {
2017 memcpy(skb_put(skb
, PASSIVE_DATA_LEN
), passive_data
,
2022 if (gb
!= NULL
&& gb_len
> 0) {
2023 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
2024 *next
|= 4; /* We have some Gi */
2029 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2037 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
2038 pn533_in_dep_link_up_complete
, arg
);
2048 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
2050 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2052 pn533_poll_reset_mod_list(dev
);
2054 if (dev
->tgt_mode
|| dev
->tgt_active_prot
) {
2055 pn533_send_ack(dev
, GFP_KERNEL
);
2056 usb_kill_urb(dev
->in_urb
);
2059 dev
->tgt_active_prot
= 0;
2062 skb_queue_purge(&dev
->resp_q
);
2067 struct pn533_data_exchange_arg
{
2068 data_exchange_cb_t cb
;
2072 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
2074 struct sk_buff
*skb
, *tmp
, *t
;
2075 unsigned int skb_len
= 0, tmp_len
= 0;
2077 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
2079 if (skb_queue_empty(&dev
->resp_q
))
2082 if (skb_queue_len(&dev
->resp_q
) == 1) {
2083 skb
= skb_dequeue(&dev
->resp_q
);
2087 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
2088 skb_len
+= tmp
->len
;
2090 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
2093 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
2097 skb_put(skb
, skb_len
);
2099 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
2100 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
2101 tmp_len
+= tmp
->len
;
2105 skb_queue_purge(&dev
->resp_q
);
2110 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
2111 struct sk_buff
*resp
)
2113 struct pn533_data_exchange_arg
*arg
= _arg
;
2114 struct sk_buff
*skb
;
2118 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2125 status
= resp
->data
[0];
2126 ret
= status
& PN533_CMD_RET_MASK
;
2127 mi
= status
& PN533_CMD_MI_MASK
;
2129 skb_pull(resp
, sizeof(status
));
2131 if (ret
!= PN533_CMD_RET_SUCCESS
) {
2132 nfc_dev_err(&dev
->interface
->dev
,
2133 "PN533 reported error %d when exchanging data",
2139 skb_queue_tail(&dev
->resp_q
, resp
);
2142 dev
->cmd_complete_mi_arg
= arg
;
2143 queue_work(dev
->wq
, &dev
->mi_work
);
2144 return -EINPROGRESS
;
2147 skb
= pn533_build_response(dev
);
2151 arg
->cb(arg
->cb_context
, skb
, 0);
2156 dev_kfree_skb(resp
);
2158 skb_queue_purge(&dev
->resp_q
);
2159 arg
->cb(arg
->cb_context
, NULL
, rc
);
2164 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2165 struct nfc_target
*target
, struct sk_buff
*skb
,
2166 data_exchange_cb_t cb
, void *cb_context
)
2168 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2169 struct pn533_data_exchange_arg
*arg
= NULL
;
2172 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2174 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2175 /* TODO: Implement support to multi-part data exchange */
2176 nfc_dev_err(&dev
->interface
->dev
,
2177 "Data length greater than the max allowed: %d",
2178 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2183 if (!dev
->tgt_active_prot
) {
2184 nfc_dev_err(&dev
->interface
->dev
,
2185 "Can't exchange data if there is no active target");
2190 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2197 arg
->cb_context
= cb_context
;
2199 switch (dev
->device_type
) {
2200 case PN533_DEVICE_PASORI
:
2201 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2202 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_COMM_THRU
,
2204 pn533_data_exchange_complete
,
2210 *skb_push(skb
, sizeof(u8
)) = 1; /*TG*/
2212 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_DATA_EXCHANGE
,
2213 skb
, pn533_data_exchange_complete
,
2219 if (rc
< 0) /* rc from send_async */
2230 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2231 struct sk_buff
*resp
)
2235 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2238 return PTR_ERR(resp
);
2240 status
= resp
->data
[0];
2242 dev_kfree_skb(resp
);
2245 nfc_tm_deactivated(dev
->nfc_dev
);
2252 queue_work(dev
->wq
, &dev
->tg_work
);
2257 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2259 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2262 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2264 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2265 nfc_dev_err(&dev
->interface
->dev
,
2266 "Data length greater than the max allowed: %d",
2267 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2271 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_DATA
, skb
,
2272 pn533_tm_send_complete
, NULL
);
2279 static void pn533_wq_mi_recv(struct work_struct
*work
)
2281 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
2283 struct sk_buff
*skb
;
2286 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2288 skb
= pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
);
2292 switch (dev
->device_type
) {
2293 case PN533_DEVICE_PASORI
:
2294 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2295 rc
= pn533_send_cmd_direct_async(dev
,
2296 PN533_CMD_IN_COMM_THRU
,
2298 pn533_data_exchange_complete
,
2299 dev
->cmd_complete_mi_arg
);
2304 *skb_put(skb
, sizeof(u8
)) = 1; /*TG*/
2306 rc
= pn533_send_cmd_direct_async(dev
,
2307 PN533_CMD_IN_DATA_EXCHANGE
,
2309 pn533_data_exchange_complete
,
2310 dev
->cmd_complete_mi_arg
);
2315 if (rc
== 0) /* success */
2318 nfc_dev_err(&dev
->interface
->dev
,
2319 "Error %d when trying to perform data_exchange", rc
);
2322 kfree(dev
->cmd_complete_arg
);
2325 pn533_send_ack(dev
, GFP_KERNEL
);
2326 queue_work(dev
->wq
, &dev
->cmd_work
);
2329 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2332 struct sk_buff
*skb
;
2333 struct sk_buff
*resp
;
2337 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2339 skb_len
= sizeof(cfgitem
) + cfgdata_len
; /* cfgitem + cfgdata */
2341 skb
= pn533_alloc_skb(skb_len
);
2345 *skb_put(skb
, sizeof(cfgitem
)) = cfgitem
;
2346 memcpy(skb_put(skb
, cfgdata_len
), cfgdata
, cfgdata_len
);
2348 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_RF_CONFIGURATION
, skb
);
2350 return PTR_ERR(resp
);
2352 dev_kfree_skb(resp
);
2356 static int pn533_get_firmware_version(struct pn533
*dev
,
2357 struct pn533_fw_version
*fv
)
2359 struct sk_buff
*skb
;
2360 struct sk_buff
*resp
;
2362 skb
= pn533_alloc_skb(0);
2366 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_GET_FIRMWARE_VERSION
, skb
);
2368 return PTR_ERR(resp
);
2370 fv
->ic
= resp
->data
[0];
2371 fv
->ver
= resp
->data
[1];
2372 fv
->rev
= resp
->data
[2];
2373 fv
->support
= resp
->data
[3];
2375 dev_kfree_skb(resp
);
2379 static int pn533_fw_reset(struct pn533
*dev
)
2381 struct sk_buff
*skb
;
2382 struct sk_buff
*resp
;
2384 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2386 skb
= pn533_alloc_skb(sizeof(u8
));
2390 *skb_put(skb
, sizeof(u8
)) = 0x1;
2392 resp
= pn533_send_cmd_sync(dev
, 0x18, skb
);
2394 return PTR_ERR(resp
);
2396 dev_kfree_skb(resp
);
2401 static struct nfc_ops pn533_nfc_ops
= {
2404 .dep_link_up
= pn533_dep_link_up
,
2405 .dep_link_down
= pn533_dep_link_down
,
2406 .start_poll
= pn533_start_poll
,
2407 .stop_poll
= pn533_stop_poll
,
2408 .activate_target
= pn533_activate_target
,
2409 .deactivate_target
= pn533_deactivate_target
,
2410 .im_transceive
= pn533_transceive
,
2411 .tm_send
= pn533_tm_send
,
2414 static int pn533_setup(struct pn533
*dev
)
2416 struct pn533_config_max_retries max_retries
;
2417 struct pn533_config_timing timing
;
2418 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2421 switch (dev
->device_type
) {
2422 case PN533_DEVICE_STD
:
2423 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
2424 max_retries
.mx_rty_psl
= 2;
2425 max_retries
.mx_rty_passive_act
=
2426 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2428 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2429 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_204
;
2430 timing
.dep_timeout
= PN533_CONFIG_TIMING_409
;
2434 case PN533_DEVICE_PASORI
:
2435 max_retries
.mx_rty_atr
= 0x2;
2436 max_retries
.mx_rty_psl
= 0x1;
2437 max_retries
.mx_rty_passive_act
=
2438 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2440 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2441 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2442 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2447 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2452 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2453 (u8
*)&max_retries
, sizeof(max_retries
));
2455 nfc_dev_err(&dev
->interface
->dev
,
2456 "Error on setting MAX_RETRIES config");
2461 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2462 (u8
*)&timing
, sizeof(timing
));
2464 nfc_dev_err(&dev
->interface
->dev
,
2465 "Error on setting RF timings");
2469 switch (dev
->device_type
) {
2470 case PN533_DEVICE_STD
:
2473 case PN533_DEVICE_PASORI
:
2474 pn533_fw_reset(dev
);
2476 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2479 nfc_dev_err(&dev
->interface
->dev
,
2480 "Error while settings PASORI config");
2484 pn533_fw_reset(dev
);
2492 static int pn533_probe(struct usb_interface
*interface
,
2493 const struct usb_device_id
*id
)
2495 struct pn533_fw_version fw_ver
;
2497 struct usb_host_interface
*iface_desc
;
2498 struct usb_endpoint_descriptor
*endpoint
;
2499 int in_endpoint
= 0;
2500 int out_endpoint
= 0;
2505 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2509 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2510 dev
->interface
= interface
;
2511 mutex_init(&dev
->cmd_lock
);
2513 iface_desc
= interface
->cur_altsetting
;
2514 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2515 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2517 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
2518 in_endpoint
= endpoint
->bEndpointAddress
;
2520 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
2521 out_endpoint
= endpoint
->bEndpointAddress
;
2524 if (!in_endpoint
|| !out_endpoint
) {
2525 nfc_dev_err(&interface
->dev
,
2526 "Could not find bulk-in or bulk-out endpoint");
2531 dev
->in_frame
= kmalloc(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
2532 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2533 dev
->out_frame
= kmalloc(PN533_NORMAL_FRAME_MAX_LEN
, GFP_KERNEL
);
2534 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2536 if (!dev
->in_frame
|| !dev
->out_frame
||
2537 !dev
->in_urb
|| !dev
->out_urb
)
2540 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
2541 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
2542 NULL
, 0, NULL
, dev
);
2543 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
2544 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
2546 pn533_send_complete
, dev
);
2548 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd
);
2549 INIT_WORK(&dev
->cmd_complete_work
, pn533_wq_cmd_complete
);
2550 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
2551 INIT_WORK(&dev
->tg_work
, pn533_wq_tg_get_data
);
2552 INIT_WORK(&dev
->poll_work
, pn533_wq_poll
);
2553 dev
->wq
= alloc_ordered_workqueue("pn533", 0);
2554 if (dev
->wq
== NULL
)
2557 init_timer(&dev
->listen_timer
);
2558 dev
->listen_timer
.data
= (unsigned long) dev
;
2559 dev
->listen_timer
.function
= pn533_listen_mode_timer
;
2561 skb_queue_head_init(&dev
->resp_q
);
2563 INIT_LIST_HEAD(&dev
->cmd_queue
);
2565 usb_set_intfdata(interface
, dev
);
2567 memset(&fw_ver
, 0, sizeof(fw_ver
));
2568 rc
= pn533_get_firmware_version(dev
, &fw_ver
);
2572 nfc_dev_info(&dev
->interface
->dev
,
2573 "NXP PN533 firmware ver %d.%d now attached",
2574 fw_ver
.ver
, fw_ver
.rev
);
2576 dev
->device_type
= id
->driver_info
;
2577 switch (dev
->device_type
) {
2578 case PN533_DEVICE_STD
:
2579 protocols
= PN533_ALL_PROTOCOLS
;
2582 case PN533_DEVICE_PASORI
:
2583 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2587 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2593 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2594 PN533_FRAME_HEADER_LEN
+
2595 PN533_CMD_DATAEXCH_HEAD_LEN
,
2596 PN533_FRAME_TAIL_LEN
);
2600 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
2601 nfc_set_drvdata(dev
->nfc_dev
, dev
);
2603 rc
= nfc_register_device(dev
->nfc_dev
);
2607 rc
= pn533_setup(dev
);
2609 goto unregister_nfc_dev
;
2614 nfc_unregister_device(dev
->nfc_dev
);
2617 nfc_free_device(dev
->nfc_dev
);
2620 destroy_workqueue(dev
->wq
);
2622 kfree(dev
->in_frame
);
2623 usb_free_urb(dev
->in_urb
);
2624 kfree(dev
->out_frame
);
2625 usb_free_urb(dev
->out_urb
);
2630 static void pn533_disconnect(struct usb_interface
*interface
)
2633 struct pn533_cmd
*cmd
, *n
;
2635 dev
= usb_get_intfdata(interface
);
2636 usb_set_intfdata(interface
, NULL
);
2638 nfc_unregister_device(dev
->nfc_dev
);
2639 nfc_free_device(dev
->nfc_dev
);
2641 usb_kill_urb(dev
->in_urb
);
2642 usb_kill_urb(dev
->out_urb
);
2644 destroy_workqueue(dev
->wq
);
2646 skb_queue_purge(&dev
->resp_q
);
2648 del_timer(&dev
->listen_timer
);
2650 list_for_each_entry_safe(cmd
, n
, &dev
->cmd_queue
, queue
) {
2651 list_del(&cmd
->queue
);
2655 kfree(dev
->in_frame
);
2656 usb_free_urb(dev
->in_urb
);
2657 kfree(dev
->out_frame
);
2658 usb_free_urb(dev
->out_urb
);
2661 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2664 static struct usb_driver pn533_driver
= {
2666 .probe
= pn533_probe
,
2667 .disconnect
= pn533_disconnect
,
2668 .id_table
= pn533_table
,
2671 module_usb_driver(pn533_driver
);
2673 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2674 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2675 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2676 MODULE_VERSION(VERSION
);
2677 MODULE_LICENSE("GPL");