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 static const struct usb_device_id pn533_table
[] = {
42 { USB_DEVICE(PN533_VENDOR_ID
, PN533_PRODUCT_ID
) },
43 { USB_DEVICE(SCM_VENDOR_ID
, SCL3711_PRODUCT_ID
) },
46 MODULE_DEVICE_TABLE(usb
, pn533_table
);
48 /* frame definitions */
49 #define PN533_FRAME_TAIL_SIZE 2
50 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
51 PN533_FRAME_TAIL_SIZE)
52 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
53 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
54 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
57 #define PN533_SOF 0x00FF
59 /* frame identifier: in/out/error */
60 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
61 #define PN533_DIR_OUT 0xD4
62 #define PN533_DIR_IN 0xD5
65 #define PN533_FRAME_CMD(f) (f->data[1])
66 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
67 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
69 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
70 #define PN533_CMD_RF_CONFIGURATION 0x32
71 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
72 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
73 #define PN533_CMD_IN_ATR 0x50
74 #define PN533_CMD_IN_RELEASE 0x52
75 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
77 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
79 /* PN533 Return codes */
80 #define PN533_CMD_RET_MASK 0x3F
81 #define PN533_CMD_MI_MASK 0x40
82 #define PN533_CMD_RET_SUCCESS 0x00
86 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
87 u8
*params
, int params_len
);
89 /* structs for pn533 commands */
91 /* PN533_CMD_GET_FIRMWARE_VERSION */
92 struct pn533_fw_version
{
99 /* PN533_CMD_RF_CONFIGURATION */
100 #define PN533_CFGITEM_MAX_RETRIES 0x05
102 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
103 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
105 struct pn533_config_max_retries
{
108 u8 mx_rty_passive_act
;
111 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
113 /* felica commands opcode */
114 #define PN533_FELICA_OPC_SENSF_REQ 0
115 #define PN533_FELICA_OPC_SENSF_RES 1
116 /* felica SENSF_REQ parameters */
117 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
118 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
119 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
120 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
122 /* type B initiator_data values */
123 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
124 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
125 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
127 union pn533_cmd_poll_initdata
{
140 /* Poll modulations */
142 PN533_POLL_MOD_106KBPS_A
,
143 PN533_POLL_MOD_212KBPS_FELICA
,
144 PN533_POLL_MOD_424KBPS_FELICA
,
145 PN533_POLL_MOD_106KBPS_JEWEL
,
146 PN533_POLL_MOD_847KBPS_B
,
148 __PN533_POLL_MOD_AFTER_LAST
,
150 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
152 struct pn533_poll_modulations
{
156 union pn533_cmd_poll_initdata initiator_data
;
161 const struct pn533_poll_modulations poll_mod
[] = {
162 [PN533_POLL_MOD_106KBPS_A
] = {
169 [PN533_POLL_MOD_212KBPS_FELICA
] = {
173 .initiator_data
.felica
= {
174 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
175 .sc
= PN533_FELICA_SENSF_SC_ALL
,
176 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
182 [PN533_POLL_MOD_424KBPS_FELICA
] = {
186 .initiator_data
.felica
= {
187 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
188 .sc
= PN533_FELICA_SENSF_SC_ALL
,
189 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
195 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
202 [PN533_POLL_MOD_847KBPS_B
] = {
206 .initiator_data
.type_b
= {
207 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
209 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
216 /* PN533_CMD_IN_ATR */
218 struct pn533_cmd_activate_param
{
223 struct pn533_cmd_activate_response
{
235 /* PN533_CMD_IN_JUMP_FOR_DEP */
236 struct pn533_cmd_jump_dep
{
243 struct pn533_cmd_jump_dep_response
{
257 struct usb_device
*udev
;
258 struct usb_interface
*interface
;
259 struct nfc_dev
*nfc_dev
;
263 struct pn533_frame
*out_frame
;
267 struct pn533_frame
*in_frame
;
269 struct sk_buff_head resp_q
;
271 struct workqueue_struct
*wq
;
272 struct work_struct cmd_work
;
273 struct work_struct mi_work
;
274 struct pn533_frame
*wq_in_frame
;
277 pn533_cmd_complete_t cmd_complete
;
278 void *cmd_complete_arg
;
279 struct semaphore cmd_lock
;
282 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
287 u8 tgt_available_prots
;
299 /* The rule: value + checksum = 0 */
300 static inline u8
pn533_checksum(u8 value
)
305 /* The rule: sum(data elements) + checksum = 0 */
306 static u8
pn533_data_checksum(u8
*data
, int datalen
)
311 for (i
= 0; i
< datalen
; i
++)
314 return pn533_checksum(sum
);
318 * pn533_tx_frame_ack - create a ack frame
319 * @frame: The frame to be set as ack
321 * Ack is different type of standard frame. As a standard frame, it has
322 * preamble and start_frame. However the checksum of this frame must fail,
323 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
324 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
325 * After datalen_checksum field, the postamble is placed.
327 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
330 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
332 frame
->datalen_checksum
= 0xFF;
333 /* data[0] is used as postamble */
337 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
340 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
341 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
342 PN533_FRAME_CMD(frame
) = cmd
;
346 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
348 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
350 PN533_FRAME_CHECKSUM(frame
) =
351 pn533_data_checksum(frame
->data
, frame
->datalen
);
353 PN533_FRAME_POSTAMBLE(frame
) = 0;
356 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
360 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
363 checksum
= pn533_checksum(frame
->datalen
);
364 if (checksum
!= frame
->datalen_checksum
)
367 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
368 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
374 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
376 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
379 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
385 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
387 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
391 static void pn533_wq_cmd_complete(struct work_struct
*work
)
393 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
394 struct pn533_frame
*in_frame
;
400 in_frame
= dev
->wq_in_frame
;
402 if (dev
->wq_in_error
)
403 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
406 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
407 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
408 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
410 if (rc
!= -EINPROGRESS
)
414 static void pn533_recv_response(struct urb
*urb
)
416 struct pn533
*dev
= urb
->context
;
417 struct pn533_frame
*in_frame
;
419 dev
->wq_in_frame
= NULL
;
421 switch (urb
->status
) {
428 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
429 " status: %d", urb
->status
);
430 dev
->wq_in_error
= urb
->status
;
433 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
435 dev
->wq_in_error
= urb
->status
;
439 in_frame
= dev
->in_urb
->transfer_buffer
;
441 if (!pn533_rx_frame_is_valid(in_frame
)) {
442 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
443 dev
->wq_in_error
= -EIO
;
447 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
448 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
449 "response to the last command");
450 dev
->wq_in_error
= -EIO
;
454 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
455 dev
->wq_in_error
= 0;
456 dev
->wq_in_frame
= in_frame
;
459 queue_work(dev
->wq
, &dev
->cmd_work
);
462 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
464 dev
->in_urb
->complete
= pn533_recv_response
;
466 return usb_submit_urb(dev
->in_urb
, flags
);
469 static void pn533_recv_ack(struct urb
*urb
)
471 struct pn533
*dev
= urb
->context
;
472 struct pn533_frame
*in_frame
;
475 switch (urb
->status
) {
482 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
483 " status: %d", urb
->status
);
484 dev
->wq_in_error
= urb
->status
;
487 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
489 dev
->wq_in_error
= urb
->status
;
493 in_frame
= dev
->in_urb
->transfer_buffer
;
495 if (!pn533_rx_frame_is_ack(in_frame
)) {
496 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
497 dev
->wq_in_error
= -EIO
;
501 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
503 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
505 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
507 dev
->wq_in_error
= rc
;
514 dev
->wq_in_frame
= NULL
;
515 queue_work(dev
->wq
, &dev
->cmd_work
);
518 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
520 dev
->in_urb
->complete
= pn533_recv_ack
;
522 return usb_submit_urb(dev
->in_urb
, flags
);
525 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
529 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
531 pn533_tx_frame_ack(dev
->out_frame
);
533 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
534 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
535 rc
= usb_submit_urb(dev
->out_urb
, flags
);
540 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
541 struct pn533_frame
*out_frame
,
542 struct pn533_frame
*in_frame
,
544 pn533_cmd_complete_t cmd_complete
,
545 void *arg
, gfp_t flags
)
549 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
550 PN533_FRAME_CMD(out_frame
));
552 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
553 dev
->cmd_complete
= cmd_complete
;
554 dev
->cmd_complete_arg
= arg
;
556 dev
->out_urb
->transfer_buffer
= out_frame
;
557 dev
->out_urb
->transfer_buffer_length
=
558 PN533_FRAME_SIZE(out_frame
);
560 dev
->in_urb
->transfer_buffer
= in_frame
;
561 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
563 rc
= usb_submit_urb(dev
->out_urb
, flags
);
567 rc
= pn533_submit_urb_for_ack(dev
, flags
);
574 usb_unlink_urb(dev
->out_urb
);
578 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
579 struct pn533_frame
*out_frame
,
580 struct pn533_frame
*in_frame
,
582 pn533_cmd_complete_t cmd_complete
,
583 void *arg
, gfp_t flags
)
587 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
589 if (down_trylock(&dev
->cmd_lock
))
592 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
593 in_frame_len
, cmd_complete
, arg
, flags
);
603 struct pn533_sync_cmd_response
{
605 struct completion done
;
608 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
609 u8
*params
, int params_len
)
611 struct pn533_sync_cmd_response
*arg
= _arg
;
613 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
617 if (params_len
< 0) /* error */
618 arg
->rc
= params_len
;
620 complete(&arg
->done
);
625 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
626 struct pn533_frame
*out_frame
,
627 struct pn533_frame
*in_frame
,
631 struct pn533_sync_cmd_response arg
;
633 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
635 init_completion(&arg
.done
);
637 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
638 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
642 wait_for_completion(&arg
.done
);
647 static void pn533_send_complete(struct urb
*urb
)
649 struct pn533
*dev
= urb
->context
;
651 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
653 switch (urb
->status
) {
660 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
661 " status: %d", urb
->status
);
664 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
669 struct pn533_target_type_a
{
677 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
678 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
679 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
681 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
682 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
684 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
685 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
687 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
688 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
689 #define PN533_TYPE_A_SEL_PROT_DEP 2
690 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
692 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
698 if (target_data_len
< sizeof(struct pn533_target_type_a
))
701 /* The lenght check of nfcid[] and ats[] are not being performed because
702 the values are not being used */
704 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
705 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
706 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
708 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
709 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
710 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
711 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
714 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
715 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
721 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
724 struct pn533_target_type_a
*tgt_type_a
;
726 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
728 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
731 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
732 case PN533_TYPE_A_SEL_PROT_MIFARE
:
733 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
735 case PN533_TYPE_A_SEL_PROT_ISO14443
:
736 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
738 case PN533_TYPE_A_SEL_PROT_DEP
:
739 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
741 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
742 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
743 NFC_PROTO_NFC_DEP_MASK
;
747 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
748 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
749 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
750 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
755 struct pn533_target_felica
{
764 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
765 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
767 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
770 if (target_data_len
< sizeof(struct pn533_target_felica
))
773 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
779 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
782 struct pn533_target_felica
*tgt_felica
;
784 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
786 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
789 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
790 tgt_felica
->nfcid2
[1] ==
791 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
792 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
794 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
796 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
797 nfc_tgt
->sensf_res_len
= 9;
802 struct pn533_target_jewel
{
807 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
813 if (target_data_len
< sizeof(struct pn533_target_jewel
))
816 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
817 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
818 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
820 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
821 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
822 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
823 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
829 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
832 struct pn533_target_jewel
*tgt_jewel
;
834 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
836 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
839 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
840 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
841 nfc_tgt
->nfcid1_len
= 4;
842 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
847 struct pn533_type_b_prot_info
{
853 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
854 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
855 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
857 struct pn533_type_b_sens_res
{
861 struct pn533_type_b_prot_info prot_info
;
864 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
866 struct pn533_target_type_b
{
867 struct pn533_type_b_sens_res sensb_res
;
872 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
875 if (target_data_len
< sizeof(struct pn533_target_type_b
))
878 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
881 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
882 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
888 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
891 struct pn533_target_type_b
*tgt_type_b
;
893 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
895 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
898 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
903 struct pn533_poll_response
{
909 static int pn533_target_found(struct pn533
*dev
,
910 struct pn533_poll_response
*resp
, int resp_len
)
913 struct nfc_target nfc_tgt
;
916 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
922 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
924 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
926 switch (dev
->poll_mod_curr
) {
927 case PN533_POLL_MOD_106KBPS_A
:
928 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
931 case PN533_POLL_MOD_212KBPS_FELICA
:
932 case PN533_POLL_MOD_424KBPS_FELICA
:
933 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
936 case PN533_POLL_MOD_106KBPS_JEWEL
:
937 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
940 case PN533_POLL_MOD_847KBPS_B
:
941 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
945 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
953 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
954 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
955 " have the desired protocol");
959 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
960 "0x%x", nfc_tgt
.supported_protocols
);
962 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
964 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
969 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
971 dev
->poll_mod_count
= 0;
974 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
976 dev
->poll_mod_active
[dev
->poll_mod_count
] =
977 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
978 dev
->poll_mod_count
++;
981 static void pn533_poll_create_mod_list(struct pn533
*dev
, u32 protocols
)
983 pn533_poll_reset_mod_list(dev
);
985 if (protocols
& NFC_PROTO_MIFARE_MASK
986 || protocols
& NFC_PROTO_ISO14443_MASK
987 || protocols
& NFC_PROTO_NFC_DEP_MASK
)
988 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
990 if (protocols
& NFC_PROTO_FELICA_MASK
991 || protocols
& NFC_PROTO_NFC_DEP_MASK
) {
992 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
993 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
996 if (protocols
& NFC_PROTO_JEWEL_MASK
)
997 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
999 if (protocols
& NFC_PROTO_ISO14443_MASK
)
1000 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1003 static void pn533_start_poll_frame(struct pn533_frame
*frame
,
1004 struct pn533_poll_modulations
*mod
)
1007 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
1009 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1010 frame
->datalen
+= mod
->len
;
1012 pn533_tx_frame_finish(frame
);
1015 static int pn533_start_poll_complete(struct pn533
*dev
, void *arg
,
1016 u8
*params
, int params_len
)
1018 struct pn533_poll_response
*resp
;
1019 struct pn533_poll_modulations
*next_mod
;
1022 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1024 if (params_len
== -ENOENT
) {
1025 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation has been"
1030 if (params_len
< 0) {
1031 nfc_dev_err(&dev
->interface
->dev
, "Error %d when running poll",
1036 resp
= (struct pn533_poll_response
*) params
;
1038 rc
= pn533_target_found(dev
, resp
, params_len
);
1040 /* We must stop the poll after a valid target found */
1045 nfc_dev_err(&dev
->interface
->dev
, "The target found is"
1046 " not valid - continuing to poll");
1049 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1051 next_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1053 nfc_dev_dbg(&dev
->interface
->dev
, "Polling next modulation (0x%x)",
1054 dev
->poll_mod_curr
);
1056 pn533_start_poll_frame(dev
->out_frame
, next_mod
);
1058 /* Don't need to down the semaphore again */
1059 rc
= __pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1060 dev
->in_maxlen
, pn533_start_poll_complete
,
1064 nfc_dev_dbg(&dev
->interface
->dev
, "Cannot poll next modulation"
1065 " because poll has been stopped");
1070 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to poll"
1071 " next modulation", rc
);
1075 /* Inform caller function to do not up the semaphore */
1076 return -EINPROGRESS
;
1079 pn533_poll_reset_mod_list(dev
);
1080 dev
->poll_protocols
= 0;
1084 static int pn533_start_poll(struct nfc_dev
*nfc_dev
, u32 protocols
)
1086 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1087 struct pn533_poll_modulations
*start_mod
;
1090 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocols=0x%x", __func__
,
1093 if (dev
->poll_mod_count
) {
1094 nfc_dev_err(&dev
->interface
->dev
, "Polling operation already"
1099 if (dev
->tgt_active_prot
) {
1100 nfc_dev_err(&dev
->interface
->dev
, "Cannot poll with a target"
1101 " already activated");
1105 pn533_poll_create_mod_list(dev
, protocols
);
1107 if (!dev
->poll_mod_count
) {
1108 nfc_dev_err(&dev
->interface
->dev
, "No valid protocols"
1114 nfc_dev_dbg(&dev
->interface
->dev
, "It will poll %d modulations types",
1115 dev
->poll_mod_count
);
1117 dev
->poll_mod_curr
= 0;
1118 start_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1120 pn533_start_poll_frame(dev
->out_frame
, start_mod
);
1122 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1123 dev
->in_maxlen
, pn533_start_poll_complete
,
1127 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1132 dev
->poll_protocols
= protocols
;
1137 pn533_poll_reset_mod_list(dev
);
1141 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1143 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1145 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1147 if (!dev
->poll_mod_count
) {
1148 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1153 /* An ack will cancel the last issued command (poll) */
1154 pn533_send_ack(dev
, GFP_KERNEL
);
1156 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1157 usb_kill_urb(dev
->in_urb
);
1160 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1162 struct pn533_cmd_activate_param param
;
1163 struct pn533_cmd_activate_response
*resp
;
1167 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1169 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1173 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1174 sizeof(struct pn533_cmd_activate_param
));
1175 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1177 pn533_tx_frame_finish(dev
->out_frame
);
1179 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1184 resp
= (struct pn533_cmd_activate_response
*)
1185 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1186 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1187 if (rc
!= PN533_CMD_RET_SUCCESS
)
1190 /* ATR_RES general bytes are located at offset 16 */
1191 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1192 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1197 static int pn533_activate_target(struct nfc_dev
*nfc_dev
, u32 target_idx
,
1200 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1203 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1206 if (dev
->poll_mod_count
) {
1207 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1212 if (dev
->tgt_active_prot
) {
1213 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1218 if (!dev
->tgt_available_prots
) {
1219 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1224 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1225 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1226 " the requested protocol %u", protocol
);
1230 if (protocol
== NFC_PROTO_NFC_DEP
) {
1231 rc
= pn533_activate_target_nfcdep(dev
);
1233 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1234 " activating target with"
1235 " NFC_DEP protocol", rc
);
1240 dev
->tgt_active_prot
= protocol
;
1241 dev
->tgt_available_prots
= 0;
1246 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
, u32 target_idx
)
1248 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1253 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1255 if (!dev
->tgt_active_prot
) {
1256 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1260 dev
->tgt_active_prot
= 0;
1262 skb_queue_purge(&dev
->resp_q
);
1264 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1267 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1268 dev
->out_frame
->datalen
+= sizeof(u8
);
1270 pn533_tx_frame_finish(dev
->out_frame
);
1272 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1275 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1276 " command to the controller");
1280 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1281 rc
= status
& PN533_CMD_RET_MASK
;
1282 if (rc
!= PN533_CMD_RET_SUCCESS
)
1283 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1290 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1291 u8
*params
, int params_len
)
1293 struct pn533_cmd_jump_dep
*cmd
;
1294 struct pn533_cmd_jump_dep_response
*resp
;
1295 struct nfc_target nfc_target
;
1299 if (params_len
== -ENOENT
) {
1300 nfc_dev_dbg(&dev
->interface
->dev
, "");
1304 if (params_len
< 0) {
1305 nfc_dev_err(&dev
->interface
->dev
,
1306 "Error %d when bringing DEP link up",
1311 if (dev
->tgt_available_prots
&&
1312 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1313 nfc_dev_err(&dev
->interface
->dev
,
1314 "The target does not support DEP");
1318 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1319 cmd
= (struct pn533_cmd_jump_dep
*) arg
;
1320 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1321 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1322 nfc_dev_err(&dev
->interface
->dev
,
1323 "Bringing DEP link up failed %d", rc
);
1327 if (!dev
->tgt_available_prots
) {
1328 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1330 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1331 nfc_target
.nfcid1_len
= 10;
1332 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1333 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1337 dev
->tgt_available_prots
= 0;
1340 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1342 /* ATR_RES general bytes are located at offset 17 */
1343 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1344 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1345 resp
->gt
, target_gt_len
);
1347 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1348 dev
->nfc_dev
->targets
[0].idx
,
1349 !cmd
->active
, NFC_RF_INITIATOR
);
1354 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, int target_idx
,
1355 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1357 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1358 struct pn533_cmd_jump_dep
*cmd
;
1362 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1364 if (dev
->poll_mod_count
) {
1365 nfc_dev_err(&dev
->interface
->dev
,
1366 "Cannot bring the DEP link up while polling");
1370 if (dev
->tgt_active_prot
) {
1371 nfc_dev_err(&dev
->interface
->dev
,
1372 "There is already an active target");
1376 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1377 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1381 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1383 cmd
->active
= !comm_mode
;
1385 if (gb
!= NULL
&& gb_len
> 0) {
1386 cmd
->next
= 4; /* We have some Gi */
1387 memcpy(cmd
->gt
, gb
, gb_len
);
1392 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1393 dev
->out_frame
->datalen
+= cmd_len
;
1395 pn533_tx_frame_finish(dev
->out_frame
);
1397 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1398 dev
->in_maxlen
, pn533_in_dep_link_up_complete
,
1410 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1412 pn533_deactivate_target(nfc_dev
, 0);
1417 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1418 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1420 static int pn533_data_exchange_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
)
1422 int payload_len
= skb
->len
;
1423 struct pn533_frame
*out_frame
;
1426 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1429 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1430 /* TODO: Implement support to multi-part data exchange */
1431 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1433 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1437 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1438 out_frame
= (struct pn533_frame
*) skb
->data
;
1440 pn533_tx_frame_init(out_frame
, PN533_CMD_IN_DATA_EXCHANGE
);
1443 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
), &tg
, sizeof(u8
));
1444 out_frame
->datalen
+= sizeof(u8
);
1446 /* The data is already in the out_frame, just update the datalen */
1447 out_frame
->datalen
+= payload_len
;
1449 pn533_tx_frame_finish(out_frame
);
1450 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1455 struct pn533_data_exchange_arg
{
1456 struct sk_buff
*skb_resp
;
1457 struct sk_buff
*skb_out
;
1458 data_exchange_cb_t cb
;
1462 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1464 struct sk_buff
*skb
, *tmp
, *t
;
1465 unsigned int skb_len
= 0, tmp_len
= 0;
1467 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
1469 if (skb_queue_empty(&dev
->resp_q
))
1472 if (skb_queue_len(&dev
->resp_q
) == 1) {
1473 skb
= skb_dequeue(&dev
->resp_q
);
1477 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1478 skb_len
+= tmp
->len
;
1480 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1483 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
1487 skb_put(skb
, skb_len
);
1489 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
1490 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
1491 tmp_len
+= tmp
->len
;
1495 skb_queue_purge(&dev
->resp_q
);
1500 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1501 u8
*params
, int params_len
)
1503 struct pn533_data_exchange_arg
*arg
= _arg
;
1504 struct sk_buff
*skb
= NULL
, *skb_resp
= arg
->skb_resp
;
1505 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1510 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1512 dev_kfree_skb(arg
->skb_out
);
1514 if (params_len
< 0) { /* error */
1521 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1522 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1523 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1524 " exchanging data", cmd_ret
);
1529 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1530 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1531 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1532 skb_queue_tail(&dev
->resp_q
, skb_resp
);
1534 if (status
& PN533_CMD_MI_MASK
) {
1535 queue_work(dev
->wq
, &dev
->mi_work
);
1536 return -EINPROGRESS
;
1539 skb
= pn533_build_response(dev
);
1543 arg
->cb(arg
->cb_context
, skb
, 0);
1548 skb_queue_purge(&dev
->resp_q
);
1549 dev_kfree_skb(skb_resp
);
1550 arg
->cb(arg
->cb_context
, NULL
, err
);
1555 static int pn533_data_exchange(struct nfc_dev
*nfc_dev
, u32 target_idx
,
1556 struct sk_buff
*skb
,
1557 data_exchange_cb_t cb
,
1560 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1561 struct pn533_frame
*out_frame
, *in_frame
;
1562 struct pn533_data_exchange_arg
*arg
;
1563 struct sk_buff
*skb_resp
;
1567 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1569 if (!dev
->tgt_active_prot
) {
1570 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
1571 " there is no active target");
1576 rc
= pn533_data_exchange_tx_frame(dev
, skb
);
1580 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1581 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1582 PN533_FRAME_TAIL_SIZE
;
1584 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1590 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1591 out_frame
= (struct pn533_frame
*) skb
->data
;
1593 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
1599 arg
->skb_resp
= skb_resp
;
1602 arg
->cb_context
= cb_context
;
1604 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
1605 pn533_data_exchange_complete
, arg
,
1608 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1609 " perform data_exchange", rc
);
1618 kfree_skb(skb_resp
);
1624 static void pn533_wq_mi_recv(struct work_struct
*work
)
1626 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
1627 struct sk_buff
*skb_cmd
;
1628 struct pn533_data_exchange_arg
*arg
= dev
->cmd_complete_arg
;
1629 struct pn533_frame
*out_frame
, *in_frame
;
1630 struct sk_buff
*skb_resp
;
1634 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1636 /* This is a zero payload size skb */
1637 skb_cmd
= alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
+ PN533_FRAME_TAIL_SIZE
,
1639 if (skb_cmd
== NULL
)
1642 skb_reserve(skb_cmd
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1644 rc
= pn533_data_exchange_tx_frame(dev
, skb_cmd
);
1648 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1649 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1650 PN533_FRAME_TAIL_SIZE
;
1651 skb_resp
= alloc_skb(skb_resp_len
, GFP_KERNEL
);
1657 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1658 out_frame
= (struct pn533_frame
*) skb_cmd
->data
;
1660 arg
->skb_resp
= skb_resp
;
1661 arg
->skb_out
= skb_cmd
;
1663 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
1665 pn533_data_exchange_complete
,
1666 dev
->cmd_complete_arg
, GFP_KERNEL
);
1670 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1671 " perform data_exchange", rc
);
1673 kfree_skb(skb_resp
);
1679 pn533_send_ack(dev
, GFP_KERNEL
);
1686 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
1692 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1694 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
1696 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
1697 params
[0] = cfgitem
;
1698 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
1699 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
1701 pn533_tx_frame_finish(dev
->out_frame
);
1703 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1709 struct nfc_ops pn533_nfc_ops
= {
1712 .dep_link_up
= pn533_dep_link_up
,
1713 .dep_link_down
= pn533_dep_link_down
,
1714 .start_poll
= pn533_start_poll
,
1715 .stop_poll
= pn533_stop_poll
,
1716 .activate_target
= pn533_activate_target
,
1717 .deactivate_target
= pn533_deactivate_target
,
1718 .data_exchange
= pn533_data_exchange
,
1721 static int pn533_probe(struct usb_interface
*interface
,
1722 const struct usb_device_id
*id
)
1724 struct pn533_fw_version
*fw_ver
;
1726 struct usb_host_interface
*iface_desc
;
1727 struct usb_endpoint_descriptor
*endpoint
;
1728 struct pn533_config_max_retries max_retries
;
1729 int in_endpoint
= 0;
1730 int out_endpoint
= 0;
1735 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1739 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1740 dev
->interface
= interface
;
1741 sema_init(&dev
->cmd_lock
, 1);
1743 iface_desc
= interface
->cur_altsetting
;
1744 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1745 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1747 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
1748 dev
->in_maxlen
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1749 in_endpoint
= endpoint
->bEndpointAddress
;
1752 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
)) {
1754 le16_to_cpu(endpoint
->wMaxPacketSize
);
1755 out_endpoint
= endpoint
->bEndpointAddress
;
1759 if (!in_endpoint
|| !out_endpoint
) {
1760 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
1761 " bulk-out endpoint");
1766 dev
->in_frame
= kmalloc(dev
->in_maxlen
, GFP_KERNEL
);
1767 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1768 dev
->out_frame
= kmalloc(dev
->out_maxlen
, GFP_KERNEL
);
1769 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1771 if (!dev
->in_frame
|| !dev
->out_frame
||
1772 !dev
->in_urb
|| !dev
->out_urb
)
1775 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1776 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1777 NULL
, 0, NULL
, dev
);
1778 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1779 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1781 pn533_send_complete
, dev
);
1783 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd_complete
);
1784 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
1785 dev
->wq
= alloc_workqueue("pn533",
1786 WQ_NON_REENTRANT
| WQ_UNBOUND
| WQ_MEM_RECLAIM
,
1788 if (dev
->wq
== NULL
)
1791 skb_queue_head_init(&dev
->resp_q
);
1793 usb_set_intfdata(interface
, dev
);
1795 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
1796 pn533_tx_frame_finish(dev
->out_frame
);
1798 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1803 fw_ver
= (struct pn533_fw_version
*)
1804 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1805 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
1806 " attached", fw_ver
->ver
, fw_ver
->rev
);
1808 protocols
= NFC_PROTO_JEWEL_MASK
1809 | NFC_PROTO_MIFARE_MASK
| NFC_PROTO_FELICA_MASK
1810 | NFC_PROTO_ISO14443_MASK
1811 | NFC_PROTO_NFC_DEP_MASK
;
1813 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
1814 PN533_CMD_DATAEXCH_HEAD_LEN
,
1815 PN533_FRAME_TAIL_SIZE
);
1819 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
1820 nfc_set_drvdata(dev
->nfc_dev
, dev
);
1822 rc
= nfc_register_device(dev
->nfc_dev
);
1826 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
1827 max_retries
.mx_rty_psl
= 2;
1828 max_retries
.mx_rty_passive_act
= PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
1830 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
1831 (u8
*) &max_retries
, sizeof(max_retries
));
1834 nfc_dev_err(&dev
->interface
->dev
, "Error on setting MAX_RETRIES"
1842 nfc_free_device(dev
->nfc_dev
);
1844 destroy_workqueue(dev
->wq
);
1846 kfree(dev
->in_frame
);
1847 usb_free_urb(dev
->in_urb
);
1848 kfree(dev
->out_frame
);
1849 usb_free_urb(dev
->out_urb
);
1854 static void pn533_disconnect(struct usb_interface
*interface
)
1858 dev
= usb_get_intfdata(interface
);
1859 usb_set_intfdata(interface
, NULL
);
1861 nfc_unregister_device(dev
->nfc_dev
);
1862 nfc_free_device(dev
->nfc_dev
);
1864 usb_kill_urb(dev
->in_urb
);
1865 usb_kill_urb(dev
->out_urb
);
1867 destroy_workqueue(dev
->wq
);
1869 skb_queue_purge(&dev
->resp_q
);
1871 kfree(dev
->in_frame
);
1872 usb_free_urb(dev
->in_urb
);
1873 kfree(dev
->out_frame
);
1874 usb_free_urb(dev
->out_urb
);
1877 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
1880 static struct usb_driver pn533_driver
= {
1882 .probe
= pn533_probe
,
1883 .disconnect
= pn533_disconnect
,
1884 .id_table
= pn533_table
,
1887 module_usb_driver(pn533_driver
);
1889 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1890 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1891 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
1892 MODULE_VERSION(VERSION
);
1893 MODULE_LICENSE("GPL");