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 tasklet_struct tasklet
;
270 struct pn533_frame
*tklt_in_frame
;
273 pn533_cmd_complete_t cmd_complete
;
274 void *cmd_complete_arg
;
275 struct semaphore cmd_lock
;
278 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
283 u8 tgt_available_prots
;
295 /* The rule: value + checksum = 0 */
296 static inline u8
pn533_checksum(u8 value
)
301 /* The rule: sum(data elements) + checksum = 0 */
302 static u8
pn533_data_checksum(u8
*data
, int datalen
)
307 for (i
= 0; i
< datalen
; i
++)
310 return pn533_checksum(sum
);
314 * pn533_tx_frame_ack - create a ack frame
315 * @frame: The frame to be set as ack
317 * Ack is different type of standard frame. As a standard frame, it has
318 * preamble and start_frame. However the checksum of this frame must fail,
319 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
320 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
321 * After datalen_checksum field, the postamble is placed.
323 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
326 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
328 frame
->datalen_checksum
= 0xFF;
329 /* data[0] is used as postamble */
333 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
336 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
337 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
338 PN533_FRAME_CMD(frame
) = cmd
;
342 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
344 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
346 PN533_FRAME_CHECKSUM(frame
) =
347 pn533_data_checksum(frame
->data
, frame
->datalen
);
349 PN533_FRAME_POSTAMBLE(frame
) = 0;
352 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
356 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
359 checksum
= pn533_checksum(frame
->datalen
);
360 if (checksum
!= frame
->datalen_checksum
)
363 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
364 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
370 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
372 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
375 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
381 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
383 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
386 static void pn533_tasklet_cmd_complete(unsigned long arg
)
388 struct pn533
*dev
= (struct pn533
*) arg
;
389 struct pn533_frame
*in_frame
= dev
->tklt_in_frame
;
392 if (dev
->tklt_in_error
)
393 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
396 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
397 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
398 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
400 if (rc
!= -EINPROGRESS
)
404 static void pn533_recv_response(struct urb
*urb
)
406 struct pn533
*dev
= urb
->context
;
407 struct pn533_frame
*in_frame
;
409 dev
->tklt_in_frame
= NULL
;
411 switch (urb
->status
) {
418 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
419 " status: %d", urb
->status
);
420 dev
->tklt_in_error
= urb
->status
;
423 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
425 dev
->tklt_in_error
= urb
->status
;
429 in_frame
= dev
->in_urb
->transfer_buffer
;
431 if (!pn533_rx_frame_is_valid(in_frame
)) {
432 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
433 dev
->tklt_in_error
= -EIO
;
437 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
438 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
439 "response to the last command");
440 dev
->tklt_in_error
= -EIO
;
444 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
445 dev
->tklt_in_error
= 0;
446 dev
->tklt_in_frame
= in_frame
;
449 tasklet_schedule(&dev
->tasklet
);
452 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
454 dev
->in_urb
->complete
= pn533_recv_response
;
456 return usb_submit_urb(dev
->in_urb
, flags
);
459 static void pn533_recv_ack(struct urb
*urb
)
461 struct pn533
*dev
= urb
->context
;
462 struct pn533_frame
*in_frame
;
465 switch (urb
->status
) {
472 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
473 " status: %d", urb
->status
);
474 dev
->tklt_in_error
= urb
->status
;
477 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
479 dev
->tklt_in_error
= urb
->status
;
483 in_frame
= dev
->in_urb
->transfer_buffer
;
485 if (!pn533_rx_frame_is_ack(in_frame
)) {
486 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
487 dev
->tklt_in_error
= -EIO
;
491 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
493 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
495 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
497 dev
->tklt_in_error
= rc
;
504 dev
->tklt_in_frame
= NULL
;
505 tasklet_schedule(&dev
->tasklet
);
508 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
510 dev
->in_urb
->complete
= pn533_recv_ack
;
512 return usb_submit_urb(dev
->in_urb
, flags
);
515 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
519 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
521 pn533_tx_frame_ack(dev
->out_frame
);
523 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
524 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
525 rc
= usb_submit_urb(dev
->out_urb
, flags
);
530 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
531 struct pn533_frame
*out_frame
,
532 struct pn533_frame
*in_frame
,
534 pn533_cmd_complete_t cmd_complete
,
535 void *arg
, gfp_t flags
)
539 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
540 PN533_FRAME_CMD(out_frame
));
542 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
543 dev
->cmd_complete
= cmd_complete
;
544 dev
->cmd_complete_arg
= arg
;
546 dev
->out_urb
->transfer_buffer
= out_frame
;
547 dev
->out_urb
->transfer_buffer_length
=
548 PN533_FRAME_SIZE(out_frame
);
550 dev
->in_urb
->transfer_buffer
= in_frame
;
551 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
553 rc
= usb_submit_urb(dev
->out_urb
, flags
);
557 rc
= pn533_submit_urb_for_ack(dev
, flags
);
564 usb_unlink_urb(dev
->out_urb
);
568 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
569 struct pn533_frame
*out_frame
,
570 struct pn533_frame
*in_frame
,
572 pn533_cmd_complete_t cmd_complete
,
573 void *arg
, gfp_t flags
)
577 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
579 if (down_trylock(&dev
->cmd_lock
))
582 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
583 in_frame_len
, cmd_complete
, arg
, flags
);
593 struct pn533_sync_cmd_response
{
595 struct completion done
;
598 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
599 u8
*params
, int params_len
)
601 struct pn533_sync_cmd_response
*arg
= _arg
;
603 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
607 if (params_len
< 0) /* error */
608 arg
->rc
= params_len
;
610 complete(&arg
->done
);
615 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
616 struct pn533_frame
*out_frame
,
617 struct pn533_frame
*in_frame
,
621 struct pn533_sync_cmd_response arg
;
623 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
625 init_completion(&arg
.done
);
627 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
628 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
632 wait_for_completion(&arg
.done
);
637 static void pn533_send_complete(struct urb
*urb
)
639 struct pn533
*dev
= urb
->context
;
641 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
643 switch (urb
->status
) {
650 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
651 " status: %d", urb
->status
);
654 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
659 struct pn533_target_type_a
{
667 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
668 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
669 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
671 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
672 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
674 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
675 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
677 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
678 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
679 #define PN533_TYPE_A_SEL_PROT_DEP 2
680 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
682 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
688 if (target_data_len
< sizeof(struct pn533_target_type_a
))
691 /* The lenght check of nfcid[] and ats[] are not being performed because
692 the values are not being used */
694 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
695 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
696 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
698 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
699 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
700 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
701 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
704 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
705 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
711 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
714 struct pn533_target_type_a
*tgt_type_a
;
716 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
718 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
721 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
722 case PN533_TYPE_A_SEL_PROT_MIFARE
:
723 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
725 case PN533_TYPE_A_SEL_PROT_ISO14443
:
726 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
728 case PN533_TYPE_A_SEL_PROT_DEP
:
729 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
731 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
732 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
733 NFC_PROTO_NFC_DEP_MASK
;
737 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
738 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
739 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
740 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
745 struct pn533_target_felica
{
754 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
755 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
757 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
760 if (target_data_len
< sizeof(struct pn533_target_felica
))
763 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
769 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
772 struct pn533_target_felica
*tgt_felica
;
774 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
776 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
779 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
780 tgt_felica
->nfcid2
[1] ==
781 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
782 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
784 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
786 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
787 nfc_tgt
->sensf_res_len
= 9;
792 struct pn533_target_jewel
{
797 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
803 if (target_data_len
< sizeof(struct pn533_target_jewel
))
806 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
807 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
808 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
810 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
811 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
812 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
813 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
819 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
822 struct pn533_target_jewel
*tgt_jewel
;
824 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
826 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
829 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
830 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
831 nfc_tgt
->nfcid1_len
= 4;
832 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
837 struct pn533_type_b_prot_info
{
843 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
844 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
845 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
847 struct pn533_type_b_sens_res
{
851 struct pn533_type_b_prot_info prot_info
;
854 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
856 struct pn533_target_type_b
{
857 struct pn533_type_b_sens_res sensb_res
;
862 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
865 if (target_data_len
< sizeof(struct pn533_target_type_b
))
868 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
871 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
872 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
878 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
881 struct pn533_target_type_b
*tgt_type_b
;
883 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
885 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
888 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
893 struct pn533_poll_response
{
899 static int pn533_target_found(struct pn533
*dev
,
900 struct pn533_poll_response
*resp
, int resp_len
)
903 struct nfc_target nfc_tgt
;
906 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
912 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
914 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
916 switch (dev
->poll_mod_curr
) {
917 case PN533_POLL_MOD_106KBPS_A
:
918 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
921 case PN533_POLL_MOD_212KBPS_FELICA
:
922 case PN533_POLL_MOD_424KBPS_FELICA
:
923 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
926 case PN533_POLL_MOD_106KBPS_JEWEL
:
927 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
930 case PN533_POLL_MOD_847KBPS_B
:
931 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
935 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
943 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
944 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
945 " have the desired protocol");
949 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
950 "0x%x", nfc_tgt
.supported_protocols
);
952 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
954 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
959 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
961 dev
->poll_mod_count
= 0;
964 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
966 dev
->poll_mod_active
[dev
->poll_mod_count
] =
967 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
968 dev
->poll_mod_count
++;
971 static void pn533_poll_create_mod_list(struct pn533
*dev
, u32 protocols
)
973 pn533_poll_reset_mod_list(dev
);
975 if (protocols
& NFC_PROTO_MIFARE_MASK
976 || protocols
& NFC_PROTO_ISO14443_MASK
977 || protocols
& NFC_PROTO_NFC_DEP_MASK
)
978 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
980 if (protocols
& NFC_PROTO_FELICA_MASK
981 || protocols
& NFC_PROTO_NFC_DEP_MASK
) {
982 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
983 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
986 if (protocols
& NFC_PROTO_JEWEL_MASK
)
987 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
989 if (protocols
& NFC_PROTO_ISO14443_MASK
)
990 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
993 static void pn533_start_poll_frame(struct pn533_frame
*frame
,
994 struct pn533_poll_modulations
*mod
)
997 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
999 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1000 frame
->datalen
+= mod
->len
;
1002 pn533_tx_frame_finish(frame
);
1005 static int pn533_start_poll_complete(struct pn533
*dev
, void *arg
,
1006 u8
*params
, int params_len
)
1008 struct pn533_poll_response
*resp
;
1009 struct pn533_poll_modulations
*next_mod
;
1012 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1014 if (params_len
== -ENOENT
) {
1015 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation has been"
1020 if (params_len
< 0) {
1021 nfc_dev_err(&dev
->interface
->dev
, "Error %d when running poll",
1026 resp
= (struct pn533_poll_response
*) params
;
1028 rc
= pn533_target_found(dev
, resp
, params_len
);
1030 /* We must stop the poll after a valid target found */
1035 nfc_dev_err(&dev
->interface
->dev
, "The target found is"
1036 " not valid - continuing to poll");
1039 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1041 next_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1043 nfc_dev_dbg(&dev
->interface
->dev
, "Polling next modulation (0x%x)",
1044 dev
->poll_mod_curr
);
1046 pn533_start_poll_frame(dev
->out_frame
, next_mod
);
1048 /* Don't need to down the semaphore again */
1049 rc
= __pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1050 dev
->in_maxlen
, pn533_start_poll_complete
,
1054 nfc_dev_dbg(&dev
->interface
->dev
, "Cannot poll next modulation"
1055 " because poll has been stopped");
1060 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to poll"
1061 " next modulation", rc
);
1065 /* Inform caller function to do not up the semaphore */
1066 return -EINPROGRESS
;
1069 pn533_poll_reset_mod_list(dev
);
1070 dev
->poll_protocols
= 0;
1074 static int pn533_start_poll(struct nfc_dev
*nfc_dev
, u32 protocols
)
1076 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1077 struct pn533_poll_modulations
*start_mod
;
1080 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocols=0x%x", __func__
,
1083 if (dev
->poll_mod_count
) {
1084 nfc_dev_err(&dev
->interface
->dev
, "Polling operation already"
1089 if (dev
->tgt_active_prot
) {
1090 nfc_dev_err(&dev
->interface
->dev
, "Cannot poll with a target"
1091 " already activated");
1095 pn533_poll_create_mod_list(dev
, protocols
);
1097 if (!dev
->poll_mod_count
) {
1098 nfc_dev_err(&dev
->interface
->dev
, "No valid protocols"
1104 nfc_dev_dbg(&dev
->interface
->dev
, "It will poll %d modulations types",
1105 dev
->poll_mod_count
);
1107 dev
->poll_mod_curr
= 0;
1108 start_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1110 pn533_start_poll_frame(dev
->out_frame
, start_mod
);
1112 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1113 dev
->in_maxlen
, pn533_start_poll_complete
,
1117 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1122 dev
->poll_protocols
= protocols
;
1127 pn533_poll_reset_mod_list(dev
);
1131 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1133 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1135 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1137 if (!dev
->poll_mod_count
) {
1138 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1143 /* An ack will cancel the last issued command (poll) */
1144 pn533_send_ack(dev
, GFP_KERNEL
);
1146 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1147 usb_kill_urb(dev
->in_urb
);
1150 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1152 struct pn533_cmd_activate_param param
;
1153 struct pn533_cmd_activate_response
*resp
;
1157 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1159 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1163 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1164 sizeof(struct pn533_cmd_activate_param
));
1165 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1167 pn533_tx_frame_finish(dev
->out_frame
);
1169 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1174 resp
= (struct pn533_cmd_activate_response
*)
1175 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1176 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1177 if (rc
!= PN533_CMD_RET_SUCCESS
)
1180 /* ATR_RES general bytes are located at offset 16 */
1181 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1182 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1187 static int pn533_activate_target(struct nfc_dev
*nfc_dev
, u32 target_idx
,
1190 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1193 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1196 if (dev
->poll_mod_count
) {
1197 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1202 if (dev
->tgt_active_prot
) {
1203 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1208 if (!dev
->tgt_available_prots
) {
1209 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1214 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1215 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1216 " the requested protocol %u", protocol
);
1220 if (protocol
== NFC_PROTO_NFC_DEP
) {
1221 rc
= pn533_activate_target_nfcdep(dev
);
1223 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1224 " activating target with"
1225 " NFC_DEP protocol", rc
);
1230 dev
->tgt_active_prot
= protocol
;
1231 dev
->tgt_available_prots
= 0;
1236 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
, u32 target_idx
)
1238 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1243 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1245 if (!dev
->tgt_active_prot
) {
1246 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1250 dev
->tgt_active_prot
= 0;
1252 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1255 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1256 dev
->out_frame
->datalen
+= sizeof(u8
);
1258 pn533_tx_frame_finish(dev
->out_frame
);
1260 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1263 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1264 " command to the controller");
1268 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1269 rc
= status
& PN533_CMD_RET_MASK
;
1270 if (rc
!= PN533_CMD_RET_SUCCESS
)
1271 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1278 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1279 u8
*params
, int params_len
)
1281 struct pn533_cmd_jump_dep_response
*resp
;
1282 struct nfc_target nfc_target
;
1285 struct pn533_cmd_jump_dep
*cmd
= (struct pn533_cmd_jump_dep
*)arg
;
1286 u8 active
= cmd
->active
;
1290 if (params_len
== -ENOENT
) {
1291 nfc_dev_dbg(&dev
->interface
->dev
, "");
1295 if (params_len
< 0) {
1296 nfc_dev_err(&dev
->interface
->dev
,
1297 "Error %d when bringing DEP link up",
1302 if (dev
->tgt_available_prots
&&
1303 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1304 nfc_dev_err(&dev
->interface
->dev
,
1305 "The target does not support DEP");
1309 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1310 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1311 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1312 nfc_dev_err(&dev
->interface
->dev
,
1313 "Bringing DEP link up failed %d", rc
);
1317 if (!dev
->tgt_available_prots
) {
1318 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1320 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1321 nfc_target
.nfcid1_len
= 10;
1322 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1323 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1327 dev
->tgt_available_prots
= 0;
1330 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1332 /* ATR_RES general bytes are located at offset 17 */
1333 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1334 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1335 resp
->gt
, target_gt_len
);
1337 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1338 dev
->nfc_dev
->targets
[0].idx
,
1339 !active
, NFC_RF_INITIATOR
);
1344 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, int target_idx
,
1345 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1347 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1348 struct pn533_cmd_jump_dep
*cmd
;
1352 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1354 if (dev
->poll_mod_count
) {
1355 nfc_dev_err(&dev
->interface
->dev
,
1356 "Cannot bring the DEP link up while polling");
1360 if (dev
->tgt_active_prot
) {
1361 nfc_dev_err(&dev
->interface
->dev
,
1362 "There is already an active target");
1366 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1367 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1371 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1373 cmd
->active
= !comm_mode
;
1375 if (gb
!= NULL
&& gb_len
> 0) {
1376 cmd
->next
= 4; /* We have some Gi */
1377 memcpy(cmd
->gt
, gb
, gb_len
);
1382 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1383 dev
->out_frame
->datalen
+= cmd_len
;
1385 pn533_tx_frame_finish(dev
->out_frame
);
1387 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1388 dev
->in_maxlen
, pn533_in_dep_link_up_complete
,
1396 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1398 pn533_deactivate_target(nfc_dev
, 0);
1403 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1404 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1406 static int pn533_data_exchange_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
)
1408 int payload_len
= skb
->len
;
1409 struct pn533_frame
*out_frame
;
1412 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1415 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1416 /* TODO: Implement support to multi-part data exchange */
1417 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1419 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1423 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1424 out_frame
= (struct pn533_frame
*) skb
->data
;
1426 pn533_tx_frame_init(out_frame
, PN533_CMD_IN_DATA_EXCHANGE
);
1429 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
), &tg
, sizeof(u8
));
1430 out_frame
->datalen
+= sizeof(u8
);
1432 /* The data is already in the out_frame, just update the datalen */
1433 out_frame
->datalen
+= payload_len
;
1435 pn533_tx_frame_finish(out_frame
);
1436 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1441 struct pn533_data_exchange_arg
{
1442 struct sk_buff
*skb_resp
;
1443 struct sk_buff
*skb_out
;
1444 data_exchange_cb_t cb
;
1448 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1449 u8
*params
, int params_len
)
1451 struct pn533_data_exchange_arg
*arg
= _arg
;
1452 struct sk_buff
*skb_resp
= arg
->skb_resp
;
1453 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1458 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1460 dev_kfree_skb_irq(arg
->skb_out
);
1462 if (params_len
< 0) { /* error */
1467 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1471 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1472 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1473 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1474 " exchanging data", cmd_ret
);
1479 if (status
& PN533_CMD_MI_MASK
) {
1480 /* TODO: Implement support to multi-part data exchange */
1481 nfc_dev_err(&dev
->interface
->dev
, "Multi-part message not yet"
1483 /* Prevent the other messages from controller */
1484 pn533_send_ack(dev
, GFP_ATOMIC
);
1489 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1490 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1492 arg
->cb(arg
->cb_context
, skb_resp
, 0);
1497 dev_kfree_skb_irq(skb_resp
);
1498 arg
->cb(arg
->cb_context
, NULL
, err
);
1503 static int pn533_data_exchange(struct nfc_dev
*nfc_dev
, u32 target_idx
,
1504 struct sk_buff
*skb
,
1505 data_exchange_cb_t cb
,
1508 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1509 struct pn533_frame
*out_frame
, *in_frame
;
1510 struct pn533_data_exchange_arg
*arg
;
1511 struct sk_buff
*skb_resp
;
1515 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1517 if (!dev
->tgt_active_prot
) {
1518 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
1519 " there is no active target");
1524 rc
= pn533_data_exchange_tx_frame(dev
, skb
);
1528 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1529 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1530 PN533_FRAME_TAIL_SIZE
;
1532 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1538 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1539 out_frame
= (struct pn533_frame
*) skb
->data
;
1541 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
1547 arg
->skb_resp
= skb_resp
;
1550 arg
->cb_context
= cb_context
;
1552 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
1553 pn533_data_exchange_complete
, arg
,
1556 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1557 " perform data_exchange", rc
);
1566 kfree_skb(skb_resp
);
1572 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
1578 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1580 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
1582 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
1583 params
[0] = cfgitem
;
1584 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
1585 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
1587 pn533_tx_frame_finish(dev
->out_frame
);
1589 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1595 struct nfc_ops pn533_nfc_ops
= {
1598 .dep_link_up
= pn533_dep_link_up
,
1599 .dep_link_down
= pn533_dep_link_down
,
1600 .start_poll
= pn533_start_poll
,
1601 .stop_poll
= pn533_stop_poll
,
1602 .activate_target
= pn533_activate_target
,
1603 .deactivate_target
= pn533_deactivate_target
,
1604 .data_exchange
= pn533_data_exchange
,
1607 static int pn533_probe(struct usb_interface
*interface
,
1608 const struct usb_device_id
*id
)
1610 struct pn533_fw_version
*fw_ver
;
1612 struct usb_host_interface
*iface_desc
;
1613 struct usb_endpoint_descriptor
*endpoint
;
1614 struct pn533_config_max_retries max_retries
;
1615 int in_endpoint
= 0;
1616 int out_endpoint
= 0;
1621 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1625 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1626 dev
->interface
= interface
;
1627 sema_init(&dev
->cmd_lock
, 1);
1629 iface_desc
= interface
->cur_altsetting
;
1630 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1631 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1633 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
1634 dev
->in_maxlen
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1635 in_endpoint
= endpoint
->bEndpointAddress
;
1638 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
)) {
1640 le16_to_cpu(endpoint
->wMaxPacketSize
);
1641 out_endpoint
= endpoint
->bEndpointAddress
;
1645 if (!in_endpoint
|| !out_endpoint
) {
1646 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
1647 " bulk-out endpoint");
1652 dev
->in_frame
= kmalloc(dev
->in_maxlen
, GFP_KERNEL
);
1653 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1654 dev
->out_frame
= kmalloc(dev
->out_maxlen
, GFP_KERNEL
);
1655 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1657 if (!dev
->in_frame
|| !dev
->out_frame
||
1658 !dev
->in_urb
|| !dev
->out_urb
)
1661 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1662 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1663 NULL
, 0, NULL
, dev
);
1664 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1665 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1667 pn533_send_complete
, dev
);
1669 tasklet_init(&dev
->tasklet
, pn533_tasklet_cmd_complete
, (ulong
)dev
);
1671 usb_set_intfdata(interface
, dev
);
1673 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
1674 pn533_tx_frame_finish(dev
->out_frame
);
1676 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1681 fw_ver
= (struct pn533_fw_version
*)
1682 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1683 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
1684 " attached", fw_ver
->ver
, fw_ver
->rev
);
1686 protocols
= NFC_PROTO_JEWEL_MASK
1687 | NFC_PROTO_MIFARE_MASK
| NFC_PROTO_FELICA_MASK
1688 | NFC_PROTO_ISO14443_MASK
1689 | NFC_PROTO_NFC_DEP_MASK
;
1691 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
1692 PN533_CMD_DATAEXCH_HEAD_LEN
,
1693 PN533_FRAME_TAIL_SIZE
);
1697 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
1698 nfc_set_drvdata(dev
->nfc_dev
, dev
);
1700 rc
= nfc_register_device(dev
->nfc_dev
);
1704 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
1705 max_retries
.mx_rty_psl
= 2;
1706 max_retries
.mx_rty_passive_act
= PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
1708 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
1709 (u8
*) &max_retries
, sizeof(max_retries
));
1712 nfc_dev_err(&dev
->interface
->dev
, "Error on setting MAX_RETRIES"
1720 nfc_free_device(dev
->nfc_dev
);
1722 tasklet_kill(&dev
->tasklet
);
1724 kfree(dev
->in_frame
);
1725 usb_free_urb(dev
->in_urb
);
1726 kfree(dev
->out_frame
);
1727 usb_free_urb(dev
->out_urb
);
1732 static void pn533_disconnect(struct usb_interface
*interface
)
1736 dev
= usb_get_intfdata(interface
);
1737 usb_set_intfdata(interface
, NULL
);
1739 nfc_unregister_device(dev
->nfc_dev
);
1740 nfc_free_device(dev
->nfc_dev
);
1742 usb_kill_urb(dev
->in_urb
);
1743 usb_kill_urb(dev
->out_urb
);
1745 tasklet_kill(&dev
->tasklet
);
1747 kfree(dev
->in_frame
);
1748 usb_free_urb(dev
->in_urb
);
1749 kfree(dev
->out_frame
);
1750 usb_free_urb(dev
->out_urb
);
1753 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
1756 static struct usb_driver pn533_driver
= {
1758 .probe
= pn533_probe
,
1759 .disconnect
= pn533_disconnect
,
1760 .id_table
= pn533_table
,
1763 module_usb_driver(pn533_driver
);
1765 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1766 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1767 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
1768 MODULE_VERSION(VERSION
);
1769 MODULE_LICENSE("GPL");