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_TG_INIT_AS_TARGET 0x8c
79 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
81 /* PN533 Return codes */
82 #define PN533_CMD_RET_MASK 0x3F
83 #define PN533_CMD_MI_MASK 0x40
84 #define PN533_CMD_RET_SUCCESS 0x00
88 typedef int (*pn533_cmd_complete_t
) (struct pn533
*dev
, void *arg
,
89 u8
*params
, int params_len
);
91 /* structs for pn533 commands */
93 /* PN533_CMD_GET_FIRMWARE_VERSION */
94 struct pn533_fw_version
{
101 /* PN533_CMD_RF_CONFIGURATION */
102 #define PN533_CFGITEM_MAX_RETRIES 0x05
104 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
105 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
107 struct pn533_config_max_retries
{
110 u8 mx_rty_passive_act
;
113 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
115 /* felica commands opcode */
116 #define PN533_FELICA_OPC_SENSF_REQ 0
117 #define PN533_FELICA_OPC_SENSF_RES 1
118 /* felica SENSF_REQ parameters */
119 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
120 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
121 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
122 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
124 /* type B initiator_data values */
125 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
126 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
127 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
129 union pn533_cmd_poll_initdata
{
142 /* Poll modulations */
144 PN533_POLL_MOD_106KBPS_A
,
145 PN533_POLL_MOD_212KBPS_FELICA
,
146 PN533_POLL_MOD_424KBPS_FELICA
,
147 PN533_POLL_MOD_106KBPS_JEWEL
,
148 PN533_POLL_MOD_847KBPS_B
,
150 __PN533_POLL_MOD_AFTER_LAST
,
152 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
154 struct pn533_poll_modulations
{
158 union pn533_cmd_poll_initdata initiator_data
;
163 const struct pn533_poll_modulations poll_mod
[] = {
164 [PN533_POLL_MOD_106KBPS_A
] = {
171 [PN533_POLL_MOD_212KBPS_FELICA
] = {
175 .initiator_data
.felica
= {
176 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
177 .sc
= PN533_FELICA_SENSF_SC_ALL
,
178 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
184 [PN533_POLL_MOD_424KBPS_FELICA
] = {
188 .initiator_data
.felica
= {
189 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
190 .sc
= PN533_FELICA_SENSF_SC_ALL
,
191 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
197 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
204 [PN533_POLL_MOD_847KBPS_B
] = {
208 .initiator_data
.type_b
= {
209 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
211 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
218 /* PN533_CMD_IN_ATR */
220 struct pn533_cmd_activate_param
{
225 struct pn533_cmd_activate_response
{
237 /* PN533_CMD_IN_JUMP_FOR_DEP */
238 struct pn533_cmd_jump_dep
{
245 struct pn533_cmd_jump_dep_response
{
259 /* PN533_TG_INIT_AS_TARGET */
260 #define PN533_INIT_TARGET_PASSIVE 0x1
261 #define PN533_INIT_TARGET_DEP 0x2
263 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
264 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
265 #define PN533_INIT_TARGET_RESP_DEP 0x4
267 struct pn533_cmd_init_target
{
276 struct pn533_cmd_init_target_response
{
282 struct usb_device
*udev
;
283 struct usb_interface
*interface
;
284 struct nfc_dev
*nfc_dev
;
288 struct pn533_frame
*out_frame
;
292 struct pn533_frame
*in_frame
;
294 struct sk_buff_head resp_q
;
296 struct workqueue_struct
*wq
;
297 struct work_struct cmd_work
;
298 struct work_struct mi_work
;
299 struct pn533_frame
*wq_in_frame
;
302 pn533_cmd_complete_t cmd_complete
;
303 void *cmd_complete_arg
;
304 struct semaphore cmd_lock
;
307 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
312 u8 tgt_available_prots
;
324 /* The rule: value + checksum = 0 */
325 static inline u8
pn533_checksum(u8 value
)
330 /* The rule: sum(data elements) + checksum = 0 */
331 static u8
pn533_data_checksum(u8
*data
, int datalen
)
336 for (i
= 0; i
< datalen
; i
++)
339 return pn533_checksum(sum
);
343 * pn533_tx_frame_ack - create a ack frame
344 * @frame: The frame to be set as ack
346 * Ack is different type of standard frame. As a standard frame, it has
347 * preamble and start_frame. However the checksum of this frame must fail,
348 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
349 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
350 * After datalen_checksum field, the postamble is placed.
352 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
355 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
357 frame
->datalen_checksum
= 0xFF;
358 /* data[0] is used as postamble */
362 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
365 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
366 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
367 PN533_FRAME_CMD(frame
) = cmd
;
371 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
373 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
375 PN533_FRAME_CHECKSUM(frame
) =
376 pn533_data_checksum(frame
->data
, frame
->datalen
);
378 PN533_FRAME_POSTAMBLE(frame
) = 0;
381 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
385 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
388 checksum
= pn533_checksum(frame
->datalen
);
389 if (checksum
!= frame
->datalen_checksum
)
392 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
393 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
399 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
401 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
404 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
410 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
412 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
416 static void pn533_wq_cmd_complete(struct work_struct
*work
)
418 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
419 struct pn533_frame
*in_frame
;
422 in_frame
= dev
->wq_in_frame
;
424 if (dev
->wq_in_error
)
425 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
428 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
429 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
430 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
432 if (rc
!= -EINPROGRESS
)
436 static void pn533_recv_response(struct urb
*urb
)
438 struct pn533
*dev
= urb
->context
;
439 struct pn533_frame
*in_frame
;
441 dev
->wq_in_frame
= NULL
;
443 switch (urb
->status
) {
450 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
451 " status: %d", urb
->status
);
452 dev
->wq_in_error
= urb
->status
;
455 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
457 dev
->wq_in_error
= urb
->status
;
461 in_frame
= dev
->in_urb
->transfer_buffer
;
463 if (!pn533_rx_frame_is_valid(in_frame
)) {
464 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
465 dev
->wq_in_error
= -EIO
;
469 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
470 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
471 "response to the last command");
472 dev
->wq_in_error
= -EIO
;
476 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
477 dev
->wq_in_error
= 0;
478 dev
->wq_in_frame
= in_frame
;
481 queue_work(dev
->wq
, &dev
->cmd_work
);
484 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
486 dev
->in_urb
->complete
= pn533_recv_response
;
488 return usb_submit_urb(dev
->in_urb
, flags
);
491 static void pn533_recv_ack(struct urb
*urb
)
493 struct pn533
*dev
= urb
->context
;
494 struct pn533_frame
*in_frame
;
497 switch (urb
->status
) {
504 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
505 " status: %d", urb
->status
);
506 dev
->wq_in_error
= urb
->status
;
509 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
511 dev
->wq_in_error
= urb
->status
;
515 in_frame
= dev
->in_urb
->transfer_buffer
;
517 if (!pn533_rx_frame_is_ack(in_frame
)) {
518 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
519 dev
->wq_in_error
= -EIO
;
523 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
525 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
527 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
529 dev
->wq_in_error
= rc
;
536 dev
->wq_in_frame
= NULL
;
537 queue_work(dev
->wq
, &dev
->cmd_work
);
540 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
542 dev
->in_urb
->complete
= pn533_recv_ack
;
544 return usb_submit_urb(dev
->in_urb
, flags
);
547 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
551 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
553 pn533_tx_frame_ack(dev
->out_frame
);
555 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
556 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
557 rc
= usb_submit_urb(dev
->out_urb
, flags
);
562 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
563 struct pn533_frame
*out_frame
,
564 struct pn533_frame
*in_frame
,
566 pn533_cmd_complete_t cmd_complete
,
567 void *arg
, gfp_t flags
)
571 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
572 PN533_FRAME_CMD(out_frame
));
574 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
575 dev
->cmd_complete
= cmd_complete
;
576 dev
->cmd_complete_arg
= arg
;
578 dev
->out_urb
->transfer_buffer
= out_frame
;
579 dev
->out_urb
->transfer_buffer_length
=
580 PN533_FRAME_SIZE(out_frame
);
582 dev
->in_urb
->transfer_buffer
= in_frame
;
583 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
585 rc
= usb_submit_urb(dev
->out_urb
, flags
);
589 rc
= pn533_submit_urb_for_ack(dev
, flags
);
596 usb_unlink_urb(dev
->out_urb
);
600 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
601 struct pn533_frame
*out_frame
,
602 struct pn533_frame
*in_frame
,
604 pn533_cmd_complete_t cmd_complete
,
605 void *arg
, gfp_t flags
)
609 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
611 if (down_trylock(&dev
->cmd_lock
))
614 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
615 in_frame_len
, cmd_complete
, arg
, flags
);
625 struct pn533_sync_cmd_response
{
627 struct completion done
;
630 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
631 u8
*params
, int params_len
)
633 struct pn533_sync_cmd_response
*arg
= _arg
;
635 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
639 if (params_len
< 0) /* error */
640 arg
->rc
= params_len
;
642 complete(&arg
->done
);
647 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
648 struct pn533_frame
*out_frame
,
649 struct pn533_frame
*in_frame
,
653 struct pn533_sync_cmd_response arg
;
655 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
657 init_completion(&arg
.done
);
659 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
660 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
664 wait_for_completion(&arg
.done
);
669 static void pn533_send_complete(struct urb
*urb
)
671 struct pn533
*dev
= urb
->context
;
673 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
675 switch (urb
->status
) {
682 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
683 " status: %d", urb
->status
);
686 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
691 struct pn533_target_type_a
{
699 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
700 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
701 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
703 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
704 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
706 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
707 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
709 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
710 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
711 #define PN533_TYPE_A_SEL_PROT_DEP 2
712 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
714 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
720 if (target_data_len
< sizeof(struct pn533_target_type_a
))
723 /* The lenght check of nfcid[] and ats[] are not being performed because
724 the values are not being used */
726 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
727 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
728 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
730 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
731 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
732 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
733 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
736 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
737 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
743 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
746 struct pn533_target_type_a
*tgt_type_a
;
748 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
750 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
753 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
754 case PN533_TYPE_A_SEL_PROT_MIFARE
:
755 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
757 case PN533_TYPE_A_SEL_PROT_ISO14443
:
758 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
760 case PN533_TYPE_A_SEL_PROT_DEP
:
761 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
763 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
764 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
765 NFC_PROTO_NFC_DEP_MASK
;
769 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
770 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
771 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
772 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
777 struct pn533_target_felica
{
786 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
787 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
789 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
792 if (target_data_len
< sizeof(struct pn533_target_felica
))
795 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
801 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
804 struct pn533_target_felica
*tgt_felica
;
806 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
808 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
811 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
812 tgt_felica
->nfcid2
[1] ==
813 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
814 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
816 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
818 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
819 nfc_tgt
->sensf_res_len
= 9;
824 struct pn533_target_jewel
{
829 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
835 if (target_data_len
< sizeof(struct pn533_target_jewel
))
838 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
839 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
840 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
842 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
843 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
844 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
845 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
851 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
854 struct pn533_target_jewel
*tgt_jewel
;
856 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
858 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
861 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
862 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
863 nfc_tgt
->nfcid1_len
= 4;
864 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
869 struct pn533_type_b_prot_info
{
875 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
876 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
877 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
879 struct pn533_type_b_sens_res
{
883 struct pn533_type_b_prot_info prot_info
;
886 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
888 struct pn533_target_type_b
{
889 struct pn533_type_b_sens_res sensb_res
;
894 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
897 if (target_data_len
< sizeof(struct pn533_target_type_b
))
900 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
903 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
904 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
910 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
913 struct pn533_target_type_b
*tgt_type_b
;
915 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
917 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
920 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
925 struct pn533_poll_response
{
931 static int pn533_target_found(struct pn533
*dev
,
932 struct pn533_poll_response
*resp
, int resp_len
)
935 struct nfc_target nfc_tgt
;
938 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
944 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
946 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
948 switch (dev
->poll_mod_curr
) {
949 case PN533_POLL_MOD_106KBPS_A
:
950 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
953 case PN533_POLL_MOD_212KBPS_FELICA
:
954 case PN533_POLL_MOD_424KBPS_FELICA
:
955 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
958 case PN533_POLL_MOD_106KBPS_JEWEL
:
959 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
962 case PN533_POLL_MOD_847KBPS_B
:
963 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
967 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
975 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
976 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
977 " have the desired protocol");
981 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
982 "0x%x", nfc_tgt
.supported_protocols
);
984 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
986 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
991 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
993 dev
->poll_mod_count
= 0;
996 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
998 dev
->poll_mod_active
[dev
->poll_mod_count
] =
999 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
1000 dev
->poll_mod_count
++;
1003 static void pn533_poll_create_mod_list(struct pn533
*dev
, u32 protocols
)
1005 pn533_poll_reset_mod_list(dev
);
1007 if (protocols
& NFC_PROTO_MIFARE_MASK
1008 || protocols
& NFC_PROTO_ISO14443_MASK
1009 || protocols
& NFC_PROTO_NFC_DEP_MASK
)
1010 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1012 if (protocols
& NFC_PROTO_FELICA_MASK
1013 || protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1014 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1015 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1018 if (protocols
& NFC_PROTO_JEWEL_MASK
)
1019 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1021 if (protocols
& NFC_PROTO_ISO14443_MASK
)
1022 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1025 static void pn533_start_poll_frame(struct pn533_frame
*frame
,
1026 struct pn533_poll_modulations
*mod
)
1029 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
1031 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1032 frame
->datalen
+= mod
->len
;
1034 pn533_tx_frame_finish(frame
);
1037 static int pn533_start_poll_complete(struct pn533
*dev
, void *arg
,
1038 u8
*params
, int params_len
)
1040 struct pn533_poll_response
*resp
;
1041 struct pn533_poll_modulations
*next_mod
;
1044 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1046 if (params_len
== -ENOENT
) {
1047 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation has been"
1052 if (params_len
< 0) {
1053 nfc_dev_err(&dev
->interface
->dev
, "Error %d when running poll",
1058 resp
= (struct pn533_poll_response
*) params
;
1060 rc
= pn533_target_found(dev
, resp
, params_len
);
1062 /* We must stop the poll after a valid target found */
1067 nfc_dev_err(&dev
->interface
->dev
, "The target found is"
1068 " not valid - continuing to poll");
1071 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1073 next_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1075 nfc_dev_dbg(&dev
->interface
->dev
, "Polling next modulation (0x%x)",
1076 dev
->poll_mod_curr
);
1078 pn533_start_poll_frame(dev
->out_frame
, next_mod
);
1080 /* Don't need to down the semaphore again */
1081 rc
= __pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1082 dev
->in_maxlen
, pn533_start_poll_complete
,
1086 nfc_dev_dbg(&dev
->interface
->dev
, "Cannot poll next modulation"
1087 " because poll has been stopped");
1092 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to poll"
1093 " next modulation", rc
);
1097 /* Inform caller function to do not up the semaphore */
1098 return -EINPROGRESS
;
1101 pn533_poll_reset_mod_list(dev
);
1102 dev
->poll_protocols
= 0;
1106 static int pn533_init_target_frame(struct pn533_frame
*frame
,
1107 u8
*gb
, size_t gb_len
)
1109 struct pn533_cmd_init_target
*cmd
;
1112 cmd_len
= sizeof(struct pn533_cmd_init_target
) + gb_len
+ 1;
1113 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1117 pn533_tx_frame_init(frame
, PN533_CMD_TG_INIT_AS_TARGET
);
1119 /* DEP support only */
1120 cmd
->mode
|= PN533_INIT_TARGET_DEP
;
1121 get_random_bytes(cmd
->nfcid3
, 10);
1122 cmd
->gb_len
= gb_len
;
1123 memcpy(cmd
->gb
, gb
, gb_len
);
1125 cmd
->gb
[gb_len
] = 0;
1127 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), cmd
, cmd_len
);
1128 frame
->datalen
+= cmd_len
;
1130 pn533_tx_frame_finish(frame
);
1135 #define ATR_REQ_GB_OFFSET 17
1136 static int pn533_init_target_complete(struct pn533
*dev
, void *arg
,
1137 u8
*params
, int params_len
)
1139 struct pn533_cmd_init_target_response
*resp
;
1140 u8 frame
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1143 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1145 if (params_len
< 0) {
1146 nfc_dev_err(&dev
->interface
->dev
,
1147 "Error %d when starting as a target",
1153 if (params_len
< ATR_REQ_GB_OFFSET
+ 1)
1156 resp
= (struct pn533_cmd_init_target_response
*) params
;
1158 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x param len %d\n",
1159 resp
->mode
, params_len
);
1161 frame
= resp
->mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
;
1162 if (frame
== PN533_INIT_TARGET_RESP_ACTIVE
)
1163 comm_mode
= NFC_COMM_ACTIVE
;
1165 /* Again, only DEP */
1166 if ((resp
->mode
& PN533_INIT_TARGET_RESP_DEP
) == 0)
1169 gb
= resp
->cmd
+ ATR_REQ_GB_OFFSET
;
1170 gb_len
= params_len
- (ATR_REQ_GB_OFFSET
+ 1);
1172 return nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1173 comm_mode
, gb
, gb_len
);
1176 static int pn533_init_target(struct nfc_dev
*nfc_dev
, u32 protocols
)
1178 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1183 pn533_poll_reset_mod_list(dev
);
1185 gb
= nfc_get_local_general_bytes(nfc_dev
, &gb_len
);
1189 rc
= pn533_init_target_frame(dev
->out_frame
, gb
, gb_len
);
1193 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1195 pn533_init_target_complete
,
1199 nfc_dev_err(&dev
->interface
->dev
,
1200 "Error %d when trying to initiate as a target", rc
);
1202 dev
->poll_mod_count
++;
1207 static int pn533_start_im_poll(struct nfc_dev
*nfc_dev
, u32 protocols
)
1209 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1210 struct pn533_poll_modulations
*start_mod
;
1213 if (dev
->poll_mod_count
) {
1214 nfc_dev_err(&dev
->interface
->dev
, "Polling operation already"
1219 pn533_poll_create_mod_list(dev
, protocols
);
1221 if (!dev
->poll_mod_count
) {
1222 nfc_dev_err(&dev
->interface
->dev
, "No valid protocols"
1228 nfc_dev_dbg(&dev
->interface
->dev
, "It will poll %d modulations types",
1229 dev
->poll_mod_count
);
1231 dev
->poll_mod_curr
= 0;
1232 start_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1234 pn533_start_poll_frame(dev
->out_frame
, start_mod
);
1236 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1237 dev
->in_maxlen
, pn533_start_poll_complete
,
1241 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1246 dev
->poll_protocols
= protocols
;
1251 pn533_poll_reset_mod_list(dev
);
1255 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1256 u32 im_protocols
, u32 tm_protocols
)
1258 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1260 nfc_dev_dbg(&dev
->interface
->dev
,
1261 "%s: im protocols 0x%x tm protocols 0x%x",
1262 __func__
, im_protocols
, tm_protocols
);
1264 if (dev
->tgt_active_prot
) {
1265 nfc_dev_err(&dev
->interface
->dev
,
1266 "Cannot poll with a target already activated");
1271 return pn533_start_im_poll(nfc_dev
, im_protocols
);
1274 return pn533_init_target(nfc_dev
, tm_protocols
);
1279 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1281 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1283 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1285 if (!dev
->poll_mod_count
) {
1286 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1291 /* An ack will cancel the last issued command (poll) */
1292 pn533_send_ack(dev
, GFP_KERNEL
);
1294 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1295 usb_kill_urb(dev
->in_urb
);
1298 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1300 struct pn533_cmd_activate_param param
;
1301 struct pn533_cmd_activate_response
*resp
;
1305 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1307 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1311 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1312 sizeof(struct pn533_cmd_activate_param
));
1313 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1315 pn533_tx_frame_finish(dev
->out_frame
);
1317 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1322 resp
= (struct pn533_cmd_activate_response
*)
1323 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1324 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1325 if (rc
!= PN533_CMD_RET_SUCCESS
)
1328 /* ATR_RES general bytes are located at offset 16 */
1329 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1330 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1335 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1336 struct nfc_target
*target
, u32 protocol
)
1338 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1341 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1344 if (dev
->poll_mod_count
) {
1345 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1350 if (dev
->tgt_active_prot
) {
1351 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1356 if (!dev
->tgt_available_prots
) {
1357 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1362 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1363 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1364 " the requested protocol %u", protocol
);
1368 if (protocol
== NFC_PROTO_NFC_DEP
) {
1369 rc
= pn533_activate_target_nfcdep(dev
);
1371 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1372 " activating target with"
1373 " NFC_DEP protocol", rc
);
1378 dev
->tgt_active_prot
= protocol
;
1379 dev
->tgt_available_prots
= 0;
1384 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1385 struct nfc_target
*target
)
1387 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1392 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1394 if (!dev
->tgt_active_prot
) {
1395 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1399 dev
->tgt_active_prot
= 0;
1401 skb_queue_purge(&dev
->resp_q
);
1403 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1406 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1407 dev
->out_frame
->datalen
+= sizeof(u8
);
1409 pn533_tx_frame_finish(dev
->out_frame
);
1411 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1414 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1415 " command to the controller");
1419 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1420 rc
= status
& PN533_CMD_RET_MASK
;
1421 if (rc
!= PN533_CMD_RET_SUCCESS
)
1422 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1429 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1430 u8
*params
, int params_len
)
1432 struct pn533_cmd_jump_dep
*cmd
;
1433 struct pn533_cmd_jump_dep_response
*resp
;
1434 struct nfc_target nfc_target
;
1438 if (params_len
== -ENOENT
) {
1439 nfc_dev_dbg(&dev
->interface
->dev
, "");
1443 if (params_len
< 0) {
1444 nfc_dev_err(&dev
->interface
->dev
,
1445 "Error %d when bringing DEP link up",
1450 if (dev
->tgt_available_prots
&&
1451 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1452 nfc_dev_err(&dev
->interface
->dev
,
1453 "The target does not support DEP");
1457 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1458 cmd
= (struct pn533_cmd_jump_dep
*) arg
;
1459 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1460 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1461 nfc_dev_err(&dev
->interface
->dev
,
1462 "Bringing DEP link up failed %d", rc
);
1466 if (!dev
->tgt_available_prots
) {
1467 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1469 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1470 nfc_target
.nfcid1_len
= 10;
1471 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1472 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1476 dev
->tgt_available_prots
= 0;
1479 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1481 /* ATR_RES general bytes are located at offset 17 */
1482 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1483 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1484 resp
->gt
, target_gt_len
);
1486 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1487 dev
->nfc_dev
->targets
[0].idx
,
1488 !cmd
->active
, NFC_RF_INITIATOR
);
1493 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1494 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1496 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1497 struct pn533_cmd_jump_dep
*cmd
;
1501 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1503 if (dev
->poll_mod_count
) {
1504 nfc_dev_err(&dev
->interface
->dev
,
1505 "Cannot bring the DEP link up while polling");
1509 if (dev
->tgt_active_prot
) {
1510 nfc_dev_err(&dev
->interface
->dev
,
1511 "There is already an active target");
1515 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1516 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1520 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1522 cmd
->active
= !comm_mode
;
1524 if (gb
!= NULL
&& gb_len
> 0) {
1525 cmd
->next
= 4; /* We have some Gi */
1526 memcpy(cmd
->gt
, gb
, gb_len
);
1531 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1532 dev
->out_frame
->datalen
+= cmd_len
;
1534 pn533_tx_frame_finish(dev
->out_frame
);
1536 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1537 dev
->in_maxlen
, pn533_in_dep_link_up_complete
,
1549 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1551 pn533_deactivate_target(nfc_dev
, 0);
1556 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1557 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1559 static int pn533_data_exchange_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
)
1561 int payload_len
= skb
->len
;
1562 struct pn533_frame
*out_frame
;
1565 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1568 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1569 /* TODO: Implement support to multi-part data exchange */
1570 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1572 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1576 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1577 out_frame
= (struct pn533_frame
*) skb
->data
;
1579 pn533_tx_frame_init(out_frame
, PN533_CMD_IN_DATA_EXCHANGE
);
1582 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
), &tg
, sizeof(u8
));
1583 out_frame
->datalen
+= sizeof(u8
);
1585 /* The data is already in the out_frame, just update the datalen */
1586 out_frame
->datalen
+= payload_len
;
1588 pn533_tx_frame_finish(out_frame
);
1589 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1594 struct pn533_data_exchange_arg
{
1595 struct sk_buff
*skb_resp
;
1596 struct sk_buff
*skb_out
;
1597 data_exchange_cb_t cb
;
1601 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1603 struct sk_buff
*skb
, *tmp
, *t
;
1604 unsigned int skb_len
= 0, tmp_len
= 0;
1606 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
1608 if (skb_queue_empty(&dev
->resp_q
))
1611 if (skb_queue_len(&dev
->resp_q
) == 1) {
1612 skb
= skb_dequeue(&dev
->resp_q
);
1616 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1617 skb_len
+= tmp
->len
;
1619 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1622 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
1626 skb_put(skb
, skb_len
);
1628 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
1629 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
1630 tmp_len
+= tmp
->len
;
1634 skb_queue_purge(&dev
->resp_q
);
1639 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1640 u8
*params
, int params_len
)
1642 struct pn533_data_exchange_arg
*arg
= _arg
;
1643 struct sk_buff
*skb
= NULL
, *skb_resp
= arg
->skb_resp
;
1644 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1649 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1651 dev_kfree_skb(arg
->skb_out
);
1653 if (params_len
< 0) { /* error */
1660 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1661 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1662 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1663 " exchanging data", cmd_ret
);
1668 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1669 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1670 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1671 skb_queue_tail(&dev
->resp_q
, skb_resp
);
1673 if (status
& PN533_CMD_MI_MASK
) {
1674 queue_work(dev
->wq
, &dev
->mi_work
);
1675 return -EINPROGRESS
;
1678 skb
= pn533_build_response(dev
);
1682 arg
->cb(arg
->cb_context
, skb
, 0);
1687 skb_queue_purge(&dev
->resp_q
);
1688 dev_kfree_skb(skb_resp
);
1689 arg
->cb(arg
->cb_context
, NULL
, err
);
1694 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
1695 struct nfc_target
*target
, struct sk_buff
*skb
,
1696 data_exchange_cb_t cb
, void *cb_context
)
1698 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1699 struct pn533_frame
*out_frame
, *in_frame
;
1700 struct pn533_data_exchange_arg
*arg
;
1701 struct sk_buff
*skb_resp
;
1705 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1707 if (!dev
->tgt_active_prot
) {
1708 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
1709 " there is no active target");
1714 rc
= pn533_data_exchange_tx_frame(dev
, skb
);
1718 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1719 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1720 PN533_FRAME_TAIL_SIZE
;
1722 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1728 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1729 out_frame
= (struct pn533_frame
*) skb
->data
;
1731 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
1737 arg
->skb_resp
= skb_resp
;
1740 arg
->cb_context
= cb_context
;
1742 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
1743 pn533_data_exchange_complete
, arg
,
1746 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1747 " perform data_exchange", rc
);
1756 kfree_skb(skb_resp
);
1762 static void pn533_wq_mi_recv(struct work_struct
*work
)
1764 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
1765 struct sk_buff
*skb_cmd
;
1766 struct pn533_data_exchange_arg
*arg
= dev
->cmd_complete_arg
;
1767 struct pn533_frame
*out_frame
, *in_frame
;
1768 struct sk_buff
*skb_resp
;
1772 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1774 /* This is a zero payload size skb */
1775 skb_cmd
= alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
+ PN533_FRAME_TAIL_SIZE
,
1777 if (skb_cmd
== NULL
)
1780 skb_reserve(skb_cmd
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1782 rc
= pn533_data_exchange_tx_frame(dev
, skb_cmd
);
1786 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1787 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1788 PN533_FRAME_TAIL_SIZE
;
1789 skb_resp
= alloc_skb(skb_resp_len
, GFP_KERNEL
);
1795 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1796 out_frame
= (struct pn533_frame
*) skb_cmd
->data
;
1798 arg
->skb_resp
= skb_resp
;
1799 arg
->skb_out
= skb_cmd
;
1801 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
1803 pn533_data_exchange_complete
,
1804 dev
->cmd_complete_arg
, GFP_KERNEL
);
1808 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1809 " perform data_exchange", rc
);
1811 kfree_skb(skb_resp
);
1817 pn533_send_ack(dev
, GFP_KERNEL
);
1824 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
1830 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1832 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
1834 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
1835 params
[0] = cfgitem
;
1836 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
1837 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
1839 pn533_tx_frame_finish(dev
->out_frame
);
1841 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1847 struct nfc_ops pn533_nfc_ops
= {
1850 .dep_link_up
= pn533_dep_link_up
,
1851 .dep_link_down
= pn533_dep_link_down
,
1852 .start_poll
= pn533_start_poll
,
1853 .stop_poll
= pn533_stop_poll
,
1854 .activate_target
= pn533_activate_target
,
1855 .deactivate_target
= pn533_deactivate_target
,
1856 .im_transceive
= pn533_transceive
,
1859 static int pn533_probe(struct usb_interface
*interface
,
1860 const struct usb_device_id
*id
)
1862 struct pn533_fw_version
*fw_ver
;
1864 struct usb_host_interface
*iface_desc
;
1865 struct usb_endpoint_descriptor
*endpoint
;
1866 struct pn533_config_max_retries max_retries
;
1867 int in_endpoint
= 0;
1868 int out_endpoint
= 0;
1873 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1877 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1878 dev
->interface
= interface
;
1879 sema_init(&dev
->cmd_lock
, 1);
1881 iface_desc
= interface
->cur_altsetting
;
1882 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1883 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1885 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
1886 dev
->in_maxlen
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1887 in_endpoint
= endpoint
->bEndpointAddress
;
1890 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
)) {
1892 le16_to_cpu(endpoint
->wMaxPacketSize
);
1893 out_endpoint
= endpoint
->bEndpointAddress
;
1897 if (!in_endpoint
|| !out_endpoint
) {
1898 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
1899 " bulk-out endpoint");
1904 dev
->in_frame
= kmalloc(dev
->in_maxlen
, GFP_KERNEL
);
1905 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1906 dev
->out_frame
= kmalloc(dev
->out_maxlen
, GFP_KERNEL
);
1907 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1909 if (!dev
->in_frame
|| !dev
->out_frame
||
1910 !dev
->in_urb
|| !dev
->out_urb
)
1913 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1914 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1915 NULL
, 0, NULL
, dev
);
1916 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1917 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1919 pn533_send_complete
, dev
);
1921 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd_complete
);
1922 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
1923 dev
->wq
= alloc_workqueue("pn533",
1924 WQ_NON_REENTRANT
| WQ_UNBOUND
| WQ_MEM_RECLAIM
,
1926 if (dev
->wq
== NULL
)
1929 skb_queue_head_init(&dev
->resp_q
);
1931 usb_set_intfdata(interface
, dev
);
1933 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
1934 pn533_tx_frame_finish(dev
->out_frame
);
1936 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1941 fw_ver
= (struct pn533_fw_version
*)
1942 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1943 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
1944 " attached", fw_ver
->ver
, fw_ver
->rev
);
1946 protocols
= NFC_PROTO_JEWEL_MASK
1947 | NFC_PROTO_MIFARE_MASK
| NFC_PROTO_FELICA_MASK
1948 | NFC_PROTO_ISO14443_MASK
1949 | NFC_PROTO_NFC_DEP_MASK
;
1951 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
1952 PN533_CMD_DATAEXCH_HEAD_LEN
,
1953 PN533_FRAME_TAIL_SIZE
);
1957 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
1958 nfc_set_drvdata(dev
->nfc_dev
, dev
);
1960 rc
= nfc_register_device(dev
->nfc_dev
);
1964 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
1965 max_retries
.mx_rty_psl
= 2;
1966 max_retries
.mx_rty_passive_act
= PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
1968 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
1969 (u8
*) &max_retries
, sizeof(max_retries
));
1972 nfc_dev_err(&dev
->interface
->dev
, "Error on setting MAX_RETRIES"
1980 nfc_free_device(dev
->nfc_dev
);
1982 destroy_workqueue(dev
->wq
);
1984 kfree(dev
->in_frame
);
1985 usb_free_urb(dev
->in_urb
);
1986 kfree(dev
->out_frame
);
1987 usb_free_urb(dev
->out_urb
);
1992 static void pn533_disconnect(struct usb_interface
*interface
)
1996 dev
= usb_get_intfdata(interface
);
1997 usb_set_intfdata(interface
, NULL
);
1999 nfc_unregister_device(dev
->nfc_dev
);
2000 nfc_free_device(dev
->nfc_dev
);
2002 usb_kill_urb(dev
->in_urb
);
2003 usb_kill_urb(dev
->out_urb
);
2005 destroy_workqueue(dev
->wq
);
2007 skb_queue_purge(&dev
->resp_q
);
2009 kfree(dev
->in_frame
);
2010 usb_free_urb(dev
->in_urb
);
2011 kfree(dev
->out_frame
);
2012 usb_free_urb(dev
->out_urb
);
2015 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2018 static struct usb_driver pn533_driver
= {
2020 .probe
= pn533_probe
,
2021 .disconnect
= pn533_disconnect
,
2022 .id_table
= pn533_table
,
2025 module_usb_driver(pn533_driver
);
2027 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2028 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2029 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2030 MODULE_VERSION(VERSION
);
2031 MODULE_LICENSE("GPL");