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 struct pn533_cmd_init_target
{
272 struct pn533_cmd_init_target_response
{
278 struct usb_device
*udev
;
279 struct usb_interface
*interface
;
280 struct nfc_dev
*nfc_dev
;
284 struct pn533_frame
*out_frame
;
288 struct pn533_frame
*in_frame
;
290 struct sk_buff_head resp_q
;
292 struct workqueue_struct
*wq
;
293 struct work_struct cmd_work
;
294 struct work_struct mi_work
;
295 struct pn533_frame
*wq_in_frame
;
298 pn533_cmd_complete_t cmd_complete
;
299 void *cmd_complete_arg
;
300 struct semaphore cmd_lock
;
303 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
308 u8 tgt_available_prots
;
320 /* The rule: value + checksum = 0 */
321 static inline u8
pn533_checksum(u8 value
)
326 /* The rule: sum(data elements) + checksum = 0 */
327 static u8
pn533_data_checksum(u8
*data
, int datalen
)
332 for (i
= 0; i
< datalen
; i
++)
335 return pn533_checksum(sum
);
339 * pn533_tx_frame_ack - create a ack frame
340 * @frame: The frame to be set as ack
342 * Ack is different type of standard frame. As a standard frame, it has
343 * preamble and start_frame. However the checksum of this frame must fail,
344 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
345 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
346 * After datalen_checksum field, the postamble is placed.
348 static void pn533_tx_frame_ack(struct pn533_frame
*frame
)
351 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
353 frame
->datalen_checksum
= 0xFF;
354 /* data[0] is used as postamble */
358 static void pn533_tx_frame_init(struct pn533_frame
*frame
, u8 cmd
)
361 frame
->start_frame
= cpu_to_be16(PN533_SOF
);
362 PN533_FRAME_IDENTIFIER(frame
) = PN533_DIR_OUT
;
363 PN533_FRAME_CMD(frame
) = cmd
;
367 static void pn533_tx_frame_finish(struct pn533_frame
*frame
)
369 frame
->datalen_checksum
= pn533_checksum(frame
->datalen
);
371 PN533_FRAME_CHECKSUM(frame
) =
372 pn533_data_checksum(frame
->data
, frame
->datalen
);
374 PN533_FRAME_POSTAMBLE(frame
) = 0;
377 static bool pn533_rx_frame_is_valid(struct pn533_frame
*frame
)
381 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
384 checksum
= pn533_checksum(frame
->datalen
);
385 if (checksum
!= frame
->datalen_checksum
)
388 checksum
= pn533_data_checksum(frame
->data
, frame
->datalen
);
389 if (checksum
!= PN533_FRAME_CHECKSUM(frame
))
395 static bool pn533_rx_frame_is_ack(struct pn533_frame
*frame
)
397 if (frame
->start_frame
!= cpu_to_be16(PN533_SOF
))
400 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
406 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame
*frame
, u8 cmd
)
408 return (PN533_FRAME_CMD(frame
) == PN533_CMD_RESPONSE(cmd
));
412 static void pn533_wq_cmd_complete(struct work_struct
*work
)
414 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
415 struct pn533_frame
*in_frame
;
418 in_frame
= dev
->wq_in_frame
;
420 if (dev
->wq_in_error
)
421 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
, NULL
,
424 rc
= dev
->cmd_complete(dev
, dev
->cmd_complete_arg
,
425 PN533_FRAME_CMD_PARAMS_PTR(in_frame
),
426 PN533_FRAME_CMD_PARAMS_LEN(in_frame
));
428 if (rc
!= -EINPROGRESS
)
432 static void pn533_recv_response(struct urb
*urb
)
434 struct pn533
*dev
= urb
->context
;
435 struct pn533_frame
*in_frame
;
437 dev
->wq_in_frame
= NULL
;
439 switch (urb
->status
) {
446 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
447 " status: %d", urb
->status
);
448 dev
->wq_in_error
= urb
->status
;
451 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
453 dev
->wq_in_error
= urb
->status
;
457 in_frame
= dev
->in_urb
->transfer_buffer
;
459 if (!pn533_rx_frame_is_valid(in_frame
)) {
460 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
461 dev
->wq_in_error
= -EIO
;
465 if (!pn533_rx_frame_is_cmd_response(in_frame
, dev
->cmd
)) {
466 nfc_dev_err(&dev
->interface
->dev
, "The received frame is not "
467 "response to the last command");
468 dev
->wq_in_error
= -EIO
;
472 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid frame");
473 dev
->wq_in_error
= 0;
474 dev
->wq_in_frame
= in_frame
;
477 queue_work(dev
->wq
, &dev
->cmd_work
);
480 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
482 dev
->in_urb
->complete
= pn533_recv_response
;
484 return usb_submit_urb(dev
->in_urb
, flags
);
487 static void pn533_recv_ack(struct urb
*urb
)
489 struct pn533
*dev
= urb
->context
;
490 struct pn533_frame
*in_frame
;
493 switch (urb
->status
) {
500 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
501 " status: %d", urb
->status
);
502 dev
->wq_in_error
= urb
->status
;
505 nfc_dev_err(&dev
->interface
->dev
, "Nonzero urb status received:"
507 dev
->wq_in_error
= urb
->status
;
511 in_frame
= dev
->in_urb
->transfer_buffer
;
513 if (!pn533_rx_frame_is_ack(in_frame
)) {
514 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
515 dev
->wq_in_error
= -EIO
;
519 nfc_dev_dbg(&dev
->interface
->dev
, "Received a valid ack");
521 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
523 nfc_dev_err(&dev
->interface
->dev
, "usb_submit_urb failed with"
525 dev
->wq_in_error
= rc
;
532 dev
->wq_in_frame
= NULL
;
533 queue_work(dev
->wq
, &dev
->cmd_work
);
536 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
538 dev
->in_urb
->complete
= pn533_recv_ack
;
540 return usb_submit_urb(dev
->in_urb
, flags
);
543 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
547 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
549 pn533_tx_frame_ack(dev
->out_frame
);
551 dev
->out_urb
->transfer_buffer
= dev
->out_frame
;
552 dev
->out_urb
->transfer_buffer_length
= PN533_FRAME_ACK_SIZE
;
553 rc
= usb_submit_urb(dev
->out_urb
, flags
);
558 static int __pn533_send_cmd_frame_async(struct pn533
*dev
,
559 struct pn533_frame
*out_frame
,
560 struct pn533_frame
*in_frame
,
562 pn533_cmd_complete_t cmd_complete
,
563 void *arg
, gfp_t flags
)
567 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x",
568 PN533_FRAME_CMD(out_frame
));
570 dev
->cmd
= PN533_FRAME_CMD(out_frame
);
571 dev
->cmd_complete
= cmd_complete
;
572 dev
->cmd_complete_arg
= arg
;
574 dev
->out_urb
->transfer_buffer
= out_frame
;
575 dev
->out_urb
->transfer_buffer_length
=
576 PN533_FRAME_SIZE(out_frame
);
578 dev
->in_urb
->transfer_buffer
= in_frame
;
579 dev
->in_urb
->transfer_buffer_length
= in_frame_len
;
581 rc
= usb_submit_urb(dev
->out_urb
, flags
);
585 rc
= pn533_submit_urb_for_ack(dev
, flags
);
592 usb_unlink_urb(dev
->out_urb
);
596 static int pn533_send_cmd_frame_async(struct pn533
*dev
,
597 struct pn533_frame
*out_frame
,
598 struct pn533_frame
*in_frame
,
600 pn533_cmd_complete_t cmd_complete
,
601 void *arg
, gfp_t flags
)
605 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
607 if (down_trylock(&dev
->cmd_lock
))
610 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
611 in_frame_len
, cmd_complete
, arg
, flags
);
621 struct pn533_sync_cmd_response
{
623 struct completion done
;
626 static int pn533_sync_cmd_complete(struct pn533
*dev
, void *_arg
,
627 u8
*params
, int params_len
)
629 struct pn533_sync_cmd_response
*arg
= _arg
;
631 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
635 if (params_len
< 0) /* error */
636 arg
->rc
= params_len
;
638 complete(&arg
->done
);
643 static int pn533_send_cmd_frame_sync(struct pn533
*dev
,
644 struct pn533_frame
*out_frame
,
645 struct pn533_frame
*in_frame
,
649 struct pn533_sync_cmd_response arg
;
651 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
653 init_completion(&arg
.done
);
655 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, in_frame_len
,
656 pn533_sync_cmd_complete
, &arg
, GFP_KERNEL
);
660 wait_for_completion(&arg
.done
);
665 static void pn533_send_complete(struct urb
*urb
)
667 struct pn533
*dev
= urb
->context
;
669 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
671 switch (urb
->status
) {
678 nfc_dev_dbg(&dev
->interface
->dev
, "Urb shutting down with"
679 " status: %d", urb
->status
);
682 nfc_dev_dbg(&dev
->interface
->dev
, "Nonzero urb status received:"
687 struct pn533_target_type_a
{
695 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
696 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
697 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
699 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
700 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
702 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
703 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
705 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
706 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
707 #define PN533_TYPE_A_SEL_PROT_DEP 2
708 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
710 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
716 if (target_data_len
< sizeof(struct pn533_target_type_a
))
719 /* The lenght check of nfcid[] and ats[] are not being performed because
720 the values are not being used */
722 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
723 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
724 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
726 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
727 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
728 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
729 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
732 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
733 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
739 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
742 struct pn533_target_type_a
*tgt_type_a
;
744 tgt_type_a
= (struct pn533_target_type_a
*) tgt_data
;
746 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
749 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
750 case PN533_TYPE_A_SEL_PROT_MIFARE
:
751 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
753 case PN533_TYPE_A_SEL_PROT_ISO14443
:
754 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
756 case PN533_TYPE_A_SEL_PROT_DEP
:
757 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
759 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
760 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
761 NFC_PROTO_NFC_DEP_MASK
;
765 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
766 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
767 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
768 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
773 struct pn533_target_felica
{
782 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
783 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
785 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
788 if (target_data_len
< sizeof(struct pn533_target_felica
))
791 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
797 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
800 struct pn533_target_felica
*tgt_felica
;
802 tgt_felica
= (struct pn533_target_felica
*) tgt_data
;
804 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
807 if (tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
&&
808 tgt_felica
->nfcid2
[1] ==
809 PN533_FELICA_SENSF_NFCID2_DEP_B2
)
810 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
812 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
814 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
815 nfc_tgt
->sensf_res_len
= 9;
820 struct pn533_target_jewel
{
825 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
831 if (target_data_len
< sizeof(struct pn533_target_jewel
))
834 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
835 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
836 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
838 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
839 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
840 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
841 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
847 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
850 struct pn533_target_jewel
*tgt_jewel
;
852 tgt_jewel
= (struct pn533_target_jewel
*) tgt_data
;
854 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
857 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
858 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
859 nfc_tgt
->nfcid1_len
= 4;
860 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
865 struct pn533_type_b_prot_info
{
871 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
872 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
873 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
875 struct pn533_type_b_sens_res
{
879 struct pn533_type_b_prot_info prot_info
;
882 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
884 struct pn533_target_type_b
{
885 struct pn533_type_b_sens_res sensb_res
;
890 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
893 if (target_data_len
< sizeof(struct pn533_target_type_b
))
896 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
899 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
900 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
906 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
909 struct pn533_target_type_b
*tgt_type_b
;
911 tgt_type_b
= (struct pn533_target_type_b
*) tgt_data
;
913 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
916 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
921 struct pn533_poll_response
{
927 static int pn533_target_found(struct pn533
*dev
,
928 struct pn533_poll_response
*resp
, int resp_len
)
931 struct nfc_target nfc_tgt
;
934 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
940 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
942 target_data_len
= resp_len
- sizeof(struct pn533_poll_response
);
944 switch (dev
->poll_mod_curr
) {
945 case PN533_POLL_MOD_106KBPS_A
:
946 rc
= pn533_target_found_type_a(&nfc_tgt
, resp
->target_data
,
949 case PN533_POLL_MOD_212KBPS_FELICA
:
950 case PN533_POLL_MOD_424KBPS_FELICA
:
951 rc
= pn533_target_found_felica(&nfc_tgt
, resp
->target_data
,
954 case PN533_POLL_MOD_106KBPS_JEWEL
:
955 rc
= pn533_target_found_jewel(&nfc_tgt
, resp
->target_data
,
958 case PN533_POLL_MOD_847KBPS_B
:
959 rc
= pn533_target_found_type_b(&nfc_tgt
, resp
->target_data
,
963 nfc_dev_err(&dev
->interface
->dev
, "Unknown current poll"
971 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
972 nfc_dev_dbg(&dev
->interface
->dev
, "The target found does not"
973 " have the desired protocol");
977 nfc_dev_dbg(&dev
->interface
->dev
, "Target found - supported protocols: "
978 "0x%x", nfc_tgt
.supported_protocols
);
980 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
982 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
987 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
989 dev
->poll_mod_count
= 0;
992 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
994 dev
->poll_mod_active
[dev
->poll_mod_count
] =
995 (struct pn533_poll_modulations
*) &poll_mod
[mod_index
];
996 dev
->poll_mod_count
++;
999 static void pn533_poll_create_mod_list(struct pn533
*dev
, u32 protocols
)
1001 pn533_poll_reset_mod_list(dev
);
1003 if (protocols
& NFC_PROTO_MIFARE_MASK
1004 || protocols
& NFC_PROTO_ISO14443_MASK
1005 || protocols
& NFC_PROTO_NFC_DEP_MASK
)
1006 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1008 if (protocols
& NFC_PROTO_FELICA_MASK
1009 || protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1010 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1011 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1014 if (protocols
& NFC_PROTO_JEWEL_MASK
)
1015 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1017 if (protocols
& NFC_PROTO_ISO14443_MASK
)
1018 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1021 static void pn533_start_poll_frame(struct pn533_frame
*frame
,
1022 struct pn533_poll_modulations
*mod
)
1025 pn533_tx_frame_init(frame
, PN533_CMD_IN_LIST_PASSIVE_TARGET
);
1027 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), &mod
->data
, mod
->len
);
1028 frame
->datalen
+= mod
->len
;
1030 pn533_tx_frame_finish(frame
);
1033 static int pn533_start_poll_complete(struct pn533
*dev
, void *arg
,
1034 u8
*params
, int params_len
)
1036 struct pn533_poll_response
*resp
;
1037 struct pn533_poll_modulations
*next_mod
;
1040 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1042 if (params_len
== -ENOENT
) {
1043 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation has been"
1048 if (params_len
< 0) {
1049 nfc_dev_err(&dev
->interface
->dev
, "Error %d when running poll",
1054 resp
= (struct pn533_poll_response
*) params
;
1056 rc
= pn533_target_found(dev
, resp
, params_len
);
1058 /* We must stop the poll after a valid target found */
1063 nfc_dev_err(&dev
->interface
->dev
, "The target found is"
1064 " not valid - continuing to poll");
1067 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1069 next_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1071 nfc_dev_dbg(&dev
->interface
->dev
, "Polling next modulation (0x%x)",
1072 dev
->poll_mod_curr
);
1074 pn533_start_poll_frame(dev
->out_frame
, next_mod
);
1076 /* Don't need to down the semaphore again */
1077 rc
= __pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1078 dev
->in_maxlen
, pn533_start_poll_complete
,
1082 nfc_dev_dbg(&dev
->interface
->dev
, "Cannot poll next modulation"
1083 " because poll has been stopped");
1088 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to poll"
1089 " next modulation", rc
);
1093 /* Inform caller function to do not up the semaphore */
1094 return -EINPROGRESS
;
1097 pn533_poll_reset_mod_list(dev
);
1098 dev
->poll_protocols
= 0;
1102 static int pn533_init_target_frame(struct pn533_frame
*frame
,
1103 u8
*gb
, size_t gb_len
)
1105 struct pn533_cmd_init_target
*cmd
;
1108 cmd_len
= sizeof(struct pn533_cmd_init_target
) + gb_len
+ 1;
1109 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1113 pn533_tx_frame_init(frame
, PN533_CMD_TG_INIT_AS_TARGET
);
1115 /* DEP support only */
1116 cmd
->mode
|= PN533_INIT_TARGET_DEP
;
1117 get_random_bytes(cmd
->nfcid3
, 10);
1118 cmd
->gb_len
= gb_len
;
1119 memcpy(cmd
->gb
, gb
, gb_len
);
1121 cmd
->gb
[gb_len
] = 0;
1123 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame
), cmd
, cmd_len
);
1124 frame
->datalen
+= cmd_len
;
1126 pn533_tx_frame_finish(frame
);
1131 static int pn533_init_target_complete(struct pn533
*dev
, void *arg
,
1132 u8
*params
, int params_len
)
1134 struct pn533_cmd_init_target_response
*resp
;
1136 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1138 if (params_len
< 0) {
1139 nfc_dev_err(&dev
->interface
->dev
,
1140 "Error %d when starting as a target",
1146 resp
= (struct pn533_cmd_init_target_response
*) params
;
1148 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x\n", resp
->mode
);
1153 static int pn533_init_target(struct nfc_dev
*nfc_dev
, u32 protocols
)
1155 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1160 pn533_poll_reset_mod_list(dev
);
1162 gb
= nfc_get_local_general_bytes(nfc_dev
, &gb_len
);
1166 rc
= pn533_init_target_frame(dev
->out_frame
, gb
, gb_len
);
1170 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1172 pn533_init_target_complete
,
1176 nfc_dev_err(&dev
->interface
->dev
,
1177 "Error %d when trying to initiate as a target", rc
);
1179 dev
->poll_mod_count
++;
1184 static int pn533_start_im_poll(struct nfc_dev
*nfc_dev
, u32 protocols
)
1186 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1187 struct pn533_poll_modulations
*start_mod
;
1190 if (dev
->poll_mod_count
) {
1191 nfc_dev_err(&dev
->interface
->dev
, "Polling operation already"
1196 pn533_poll_create_mod_list(dev
, protocols
);
1198 if (!dev
->poll_mod_count
) {
1199 nfc_dev_err(&dev
->interface
->dev
, "No valid protocols"
1205 nfc_dev_dbg(&dev
->interface
->dev
, "It will poll %d modulations types",
1206 dev
->poll_mod_count
);
1208 dev
->poll_mod_curr
= 0;
1209 start_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1211 pn533_start_poll_frame(dev
->out_frame
, start_mod
);
1213 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1214 dev
->in_maxlen
, pn533_start_poll_complete
,
1218 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1223 dev
->poll_protocols
= protocols
;
1228 pn533_poll_reset_mod_list(dev
);
1232 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1233 u32 im_protocols
, u32 tm_protocols
)
1235 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1237 nfc_dev_dbg(&dev
->interface
->dev
,
1238 "%s: im protocols 0x%x tm protocols 0x%x",
1239 __func__
, im_protocols
, tm_protocols
);
1241 if (dev
->tgt_active_prot
) {
1242 nfc_dev_err(&dev
->interface
->dev
,
1243 "Cannot poll with a target already activated");
1248 return pn533_start_im_poll(nfc_dev
, im_protocols
);
1251 return pn533_init_target(nfc_dev
, tm_protocols
);
1256 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1258 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1260 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1262 if (!dev
->poll_mod_count
) {
1263 nfc_dev_dbg(&dev
->interface
->dev
, "Polling operation was not"
1268 /* An ack will cancel the last issued command (poll) */
1269 pn533_send_ack(dev
, GFP_KERNEL
);
1271 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1272 usb_kill_urb(dev
->in_urb
);
1275 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1277 struct pn533_cmd_activate_param param
;
1278 struct pn533_cmd_activate_response
*resp
;
1282 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1284 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_ATR
);
1288 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), ¶m
,
1289 sizeof(struct pn533_cmd_activate_param
));
1290 dev
->out_frame
->datalen
+= sizeof(struct pn533_cmd_activate_param
);
1292 pn533_tx_frame_finish(dev
->out_frame
);
1294 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1299 resp
= (struct pn533_cmd_activate_response
*)
1300 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1301 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1302 if (rc
!= PN533_CMD_RET_SUCCESS
)
1305 /* ATR_RES general bytes are located at offset 16 */
1306 gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 16;
1307 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, resp
->gt
, gt_len
);
1312 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1313 struct nfc_target
*target
, u32 protocol
)
1315 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1318 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1321 if (dev
->poll_mod_count
) {
1322 nfc_dev_err(&dev
->interface
->dev
, "Cannot activate while"
1327 if (dev
->tgt_active_prot
) {
1328 nfc_dev_err(&dev
->interface
->dev
, "There is already an active"
1333 if (!dev
->tgt_available_prots
) {
1334 nfc_dev_err(&dev
->interface
->dev
, "There is no available target"
1339 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1340 nfc_dev_err(&dev
->interface
->dev
, "The target does not support"
1341 " the requested protocol %u", protocol
);
1345 if (protocol
== NFC_PROTO_NFC_DEP
) {
1346 rc
= pn533_activate_target_nfcdep(dev
);
1348 nfc_dev_err(&dev
->interface
->dev
, "Error %d when"
1349 " activating target with"
1350 " NFC_DEP protocol", rc
);
1355 dev
->tgt_active_prot
= protocol
;
1356 dev
->tgt_available_prots
= 0;
1361 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1362 struct nfc_target
*target
)
1364 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1369 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1371 if (!dev
->tgt_active_prot
) {
1372 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1376 dev
->tgt_active_prot
= 0;
1378 skb_queue_purge(&dev
->resp_q
);
1380 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_RELEASE
);
1383 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), &tg
, sizeof(u8
));
1384 dev
->out_frame
->datalen
+= sizeof(u8
);
1386 pn533_tx_frame_finish(dev
->out_frame
);
1388 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1391 nfc_dev_err(&dev
->interface
->dev
, "Error when sending release"
1392 " command to the controller");
1396 status
= PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
)[0];
1397 rc
= status
& PN533_CMD_RET_MASK
;
1398 if (rc
!= PN533_CMD_RET_SUCCESS
)
1399 nfc_dev_err(&dev
->interface
->dev
, "Error 0x%x when releasing"
1406 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1407 u8
*params
, int params_len
)
1409 struct pn533_cmd_jump_dep
*cmd
;
1410 struct pn533_cmd_jump_dep_response
*resp
;
1411 struct nfc_target nfc_target
;
1415 if (params_len
== -ENOENT
) {
1416 nfc_dev_dbg(&dev
->interface
->dev
, "");
1420 if (params_len
< 0) {
1421 nfc_dev_err(&dev
->interface
->dev
,
1422 "Error %d when bringing DEP link up",
1427 if (dev
->tgt_available_prots
&&
1428 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1429 nfc_dev_err(&dev
->interface
->dev
,
1430 "The target does not support DEP");
1434 resp
= (struct pn533_cmd_jump_dep_response
*) params
;
1435 cmd
= (struct pn533_cmd_jump_dep
*) arg
;
1436 rc
= resp
->status
& PN533_CMD_RET_MASK
;
1437 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1438 nfc_dev_err(&dev
->interface
->dev
,
1439 "Bringing DEP link up failed %d", rc
);
1443 if (!dev
->tgt_available_prots
) {
1444 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
1446 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1447 nfc_target
.nfcid1_len
= 10;
1448 memcpy(nfc_target
.nfcid1
, resp
->nfcid3t
, nfc_target
.nfcid1_len
);
1449 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1453 dev
->tgt_available_prots
= 0;
1456 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1458 /* ATR_RES general bytes are located at offset 17 */
1459 target_gt_len
= PN533_FRAME_CMD_PARAMS_LEN(dev
->in_frame
) - 17;
1460 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1461 resp
->gt
, target_gt_len
);
1463 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1464 dev
->nfc_dev
->targets
[0].idx
,
1465 !cmd
->active
, NFC_RF_INITIATOR
);
1470 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1471 u8 comm_mode
, u8
* gb
, size_t gb_len
)
1473 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1474 struct pn533_cmd_jump_dep
*cmd
;
1478 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1480 if (dev
->poll_mod_count
) {
1481 nfc_dev_err(&dev
->interface
->dev
,
1482 "Cannot bring the DEP link up while polling");
1486 if (dev
->tgt_active_prot
) {
1487 nfc_dev_err(&dev
->interface
->dev
,
1488 "There is already an active target");
1492 cmd_len
= sizeof(struct pn533_cmd_jump_dep
) + gb_len
;
1493 cmd
= kzalloc(cmd_len
, GFP_KERNEL
);
1497 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_IN_JUMP_FOR_DEP
);
1499 cmd
->active
= !comm_mode
;
1501 if (gb
!= NULL
&& gb_len
> 0) {
1502 cmd
->next
= 4; /* We have some Gi */
1503 memcpy(cmd
->gt
, gb
, gb_len
);
1508 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
), cmd
, cmd_len
);
1509 dev
->out_frame
->datalen
+= cmd_len
;
1511 pn533_tx_frame_finish(dev
->out_frame
);
1513 rc
= pn533_send_cmd_frame_async(dev
, dev
->out_frame
, dev
->in_frame
,
1514 dev
->in_maxlen
, pn533_in_dep_link_up_complete
,
1526 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
1528 pn533_deactivate_target(nfc_dev
, 0);
1533 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1534 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1536 static int pn533_data_exchange_tx_frame(struct pn533
*dev
, struct sk_buff
*skb
)
1538 int payload_len
= skb
->len
;
1539 struct pn533_frame
*out_frame
;
1542 nfc_dev_dbg(&dev
->interface
->dev
, "%s - Sending %d bytes", __func__
,
1545 if (payload_len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
1546 /* TODO: Implement support to multi-part data exchange */
1547 nfc_dev_err(&dev
->interface
->dev
, "Data length greater than the"
1549 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
1553 skb_push(skb
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1554 out_frame
= (struct pn533_frame
*) skb
->data
;
1556 pn533_tx_frame_init(out_frame
, PN533_CMD_IN_DATA_EXCHANGE
);
1559 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame
), &tg
, sizeof(u8
));
1560 out_frame
->datalen
+= sizeof(u8
);
1562 /* The data is already in the out_frame, just update the datalen */
1563 out_frame
->datalen
+= payload_len
;
1565 pn533_tx_frame_finish(out_frame
);
1566 skb_put(skb
, PN533_FRAME_TAIL_SIZE
);
1571 struct pn533_data_exchange_arg
{
1572 struct sk_buff
*skb_resp
;
1573 struct sk_buff
*skb_out
;
1574 data_exchange_cb_t cb
;
1578 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
1580 struct sk_buff
*skb
, *tmp
, *t
;
1581 unsigned int skb_len
= 0, tmp_len
= 0;
1583 nfc_dev_dbg(&dev
->interface
->dev
, "%s\n", __func__
);
1585 if (skb_queue_empty(&dev
->resp_q
))
1588 if (skb_queue_len(&dev
->resp_q
) == 1) {
1589 skb
= skb_dequeue(&dev
->resp_q
);
1593 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
1594 skb_len
+= tmp
->len
;
1596 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
1599 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
1603 skb_put(skb
, skb_len
);
1605 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
1606 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
1607 tmp_len
+= tmp
->len
;
1611 skb_queue_purge(&dev
->resp_q
);
1616 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
1617 u8
*params
, int params_len
)
1619 struct pn533_data_exchange_arg
*arg
= _arg
;
1620 struct sk_buff
*skb
= NULL
, *skb_resp
= arg
->skb_resp
;
1621 struct pn533_frame
*in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1626 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1628 dev_kfree_skb(arg
->skb_out
);
1630 if (params_len
< 0) { /* error */
1637 cmd_ret
= status
& PN533_CMD_RET_MASK
;
1638 if (cmd_ret
!= PN533_CMD_RET_SUCCESS
) {
1639 nfc_dev_err(&dev
->interface
->dev
, "PN533 reported error %d when"
1640 " exchanging data", cmd_ret
);
1645 skb_put(skb_resp
, PN533_FRAME_SIZE(in_frame
));
1646 skb_pull(skb_resp
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1647 skb_trim(skb_resp
, skb_resp
->len
- PN533_FRAME_TAIL_SIZE
);
1648 skb_queue_tail(&dev
->resp_q
, skb_resp
);
1650 if (status
& PN533_CMD_MI_MASK
) {
1651 queue_work(dev
->wq
, &dev
->mi_work
);
1652 return -EINPROGRESS
;
1655 skb
= pn533_build_response(dev
);
1659 arg
->cb(arg
->cb_context
, skb
, 0);
1664 skb_queue_purge(&dev
->resp_q
);
1665 dev_kfree_skb(skb_resp
);
1666 arg
->cb(arg
->cb_context
, NULL
, err
);
1671 static int pn533_data_exchange(struct nfc_dev
*nfc_dev
,
1672 struct nfc_target
*target
, struct sk_buff
*skb
,
1673 data_exchange_cb_t cb
, void *cb_context
)
1675 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1676 struct pn533_frame
*out_frame
, *in_frame
;
1677 struct pn533_data_exchange_arg
*arg
;
1678 struct sk_buff
*skb_resp
;
1682 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1684 if (!dev
->tgt_active_prot
) {
1685 nfc_dev_err(&dev
->interface
->dev
, "Cannot exchange data if"
1686 " there is no active target");
1691 rc
= pn533_data_exchange_tx_frame(dev
, skb
);
1695 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1696 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1697 PN533_FRAME_TAIL_SIZE
;
1699 skb_resp
= nfc_alloc_recv_skb(skb_resp_len
, GFP_KERNEL
);
1705 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1706 out_frame
= (struct pn533_frame
*) skb
->data
;
1708 arg
= kmalloc(sizeof(struct pn533_data_exchange_arg
), GFP_KERNEL
);
1714 arg
->skb_resp
= skb_resp
;
1717 arg
->cb_context
= cb_context
;
1719 rc
= pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
, skb_resp_len
,
1720 pn533_data_exchange_complete
, arg
,
1723 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1724 " perform data_exchange", rc
);
1733 kfree_skb(skb_resp
);
1739 static void pn533_wq_mi_recv(struct work_struct
*work
)
1741 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
1742 struct sk_buff
*skb_cmd
;
1743 struct pn533_data_exchange_arg
*arg
= dev
->cmd_complete_arg
;
1744 struct pn533_frame
*out_frame
, *in_frame
;
1745 struct sk_buff
*skb_resp
;
1749 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1751 /* This is a zero payload size skb */
1752 skb_cmd
= alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN
+ PN533_FRAME_TAIL_SIZE
,
1754 if (skb_cmd
== NULL
)
1757 skb_reserve(skb_cmd
, PN533_CMD_DATAEXCH_HEAD_LEN
);
1759 rc
= pn533_data_exchange_tx_frame(dev
, skb_cmd
);
1763 skb_resp_len
= PN533_CMD_DATAEXCH_HEAD_LEN
+
1764 PN533_CMD_DATAEXCH_DATA_MAXLEN
+
1765 PN533_FRAME_TAIL_SIZE
;
1766 skb_resp
= alloc_skb(skb_resp_len
, GFP_KERNEL
);
1772 in_frame
= (struct pn533_frame
*) skb_resp
->data
;
1773 out_frame
= (struct pn533_frame
*) skb_cmd
->data
;
1775 arg
->skb_resp
= skb_resp
;
1776 arg
->skb_out
= skb_cmd
;
1778 rc
= __pn533_send_cmd_frame_async(dev
, out_frame
, in_frame
,
1780 pn533_data_exchange_complete
,
1781 dev
->cmd_complete_arg
, GFP_KERNEL
);
1785 nfc_dev_err(&dev
->interface
->dev
, "Error %d when trying to"
1786 " perform data_exchange", rc
);
1788 kfree_skb(skb_resp
);
1794 pn533_send_ack(dev
, GFP_KERNEL
);
1801 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
1807 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1809 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_RF_CONFIGURATION
);
1811 params
= PN533_FRAME_CMD_PARAMS_PTR(dev
->out_frame
);
1812 params
[0] = cfgitem
;
1813 memcpy(¶ms
[1], cfgdata
, cfgdata_len
);
1814 dev
->out_frame
->datalen
+= (1 + cfgdata_len
);
1816 pn533_tx_frame_finish(dev
->out_frame
);
1818 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1824 struct nfc_ops pn533_nfc_ops
= {
1827 .dep_link_up
= pn533_dep_link_up
,
1828 .dep_link_down
= pn533_dep_link_down
,
1829 .start_poll
= pn533_start_poll
,
1830 .stop_poll
= pn533_stop_poll
,
1831 .activate_target
= pn533_activate_target
,
1832 .deactivate_target
= pn533_deactivate_target
,
1833 .data_exchange
= pn533_data_exchange
,
1836 static int pn533_probe(struct usb_interface
*interface
,
1837 const struct usb_device_id
*id
)
1839 struct pn533_fw_version
*fw_ver
;
1841 struct usb_host_interface
*iface_desc
;
1842 struct usb_endpoint_descriptor
*endpoint
;
1843 struct pn533_config_max_retries max_retries
;
1844 int in_endpoint
= 0;
1845 int out_endpoint
= 0;
1850 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1854 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
1855 dev
->interface
= interface
;
1856 sema_init(&dev
->cmd_lock
, 1);
1858 iface_desc
= interface
->cur_altsetting
;
1859 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1860 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1862 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
)) {
1863 dev
->in_maxlen
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1864 in_endpoint
= endpoint
->bEndpointAddress
;
1867 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
)) {
1869 le16_to_cpu(endpoint
->wMaxPacketSize
);
1870 out_endpoint
= endpoint
->bEndpointAddress
;
1874 if (!in_endpoint
|| !out_endpoint
) {
1875 nfc_dev_err(&interface
->dev
, "Could not find bulk-in or"
1876 " bulk-out endpoint");
1881 dev
->in_frame
= kmalloc(dev
->in_maxlen
, GFP_KERNEL
);
1882 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1883 dev
->out_frame
= kmalloc(dev
->out_maxlen
, GFP_KERNEL
);
1884 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1886 if (!dev
->in_frame
|| !dev
->out_frame
||
1887 !dev
->in_urb
|| !dev
->out_urb
)
1890 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
1891 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
1892 NULL
, 0, NULL
, dev
);
1893 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
1894 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
1896 pn533_send_complete
, dev
);
1898 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd_complete
);
1899 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
1900 dev
->wq
= alloc_workqueue("pn533",
1901 WQ_NON_REENTRANT
| WQ_UNBOUND
| WQ_MEM_RECLAIM
,
1903 if (dev
->wq
== NULL
)
1906 skb_queue_head_init(&dev
->resp_q
);
1908 usb_set_intfdata(interface
, dev
);
1910 pn533_tx_frame_init(dev
->out_frame
, PN533_CMD_GET_FIRMWARE_VERSION
);
1911 pn533_tx_frame_finish(dev
->out_frame
);
1913 rc
= pn533_send_cmd_frame_sync(dev
, dev
->out_frame
, dev
->in_frame
,
1918 fw_ver
= (struct pn533_fw_version
*)
1919 PN533_FRAME_CMD_PARAMS_PTR(dev
->in_frame
);
1920 nfc_dev_info(&dev
->interface
->dev
, "NXP PN533 firmware ver %d.%d now"
1921 " attached", fw_ver
->ver
, fw_ver
->rev
);
1923 protocols
= NFC_PROTO_JEWEL_MASK
1924 | NFC_PROTO_MIFARE_MASK
| NFC_PROTO_FELICA_MASK
1925 | NFC_PROTO_ISO14443_MASK
1926 | NFC_PROTO_NFC_DEP_MASK
;
1928 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
1929 PN533_CMD_DATAEXCH_HEAD_LEN
,
1930 PN533_FRAME_TAIL_SIZE
);
1934 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
1935 nfc_set_drvdata(dev
->nfc_dev
, dev
);
1937 rc
= nfc_register_device(dev
->nfc_dev
);
1941 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
1942 max_retries
.mx_rty_psl
= 2;
1943 max_retries
.mx_rty_passive_act
= PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
1945 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
1946 (u8
*) &max_retries
, sizeof(max_retries
));
1949 nfc_dev_err(&dev
->interface
->dev
, "Error on setting MAX_RETRIES"
1957 nfc_free_device(dev
->nfc_dev
);
1959 destroy_workqueue(dev
->wq
);
1961 kfree(dev
->in_frame
);
1962 usb_free_urb(dev
->in_urb
);
1963 kfree(dev
->out_frame
);
1964 usb_free_urb(dev
->out_urb
);
1969 static void pn533_disconnect(struct usb_interface
*interface
)
1973 dev
= usb_get_intfdata(interface
);
1974 usb_set_intfdata(interface
, NULL
);
1976 nfc_unregister_device(dev
->nfc_dev
);
1977 nfc_free_device(dev
->nfc_dev
);
1979 usb_kill_urb(dev
->in_urb
);
1980 usb_kill_urb(dev
->out_urb
);
1982 destroy_workqueue(dev
->wq
);
1984 skb_queue_purge(&dev
->resp_q
);
1986 kfree(dev
->in_frame
);
1987 usb_free_urb(dev
->in_urb
);
1988 kfree(dev
->out_frame
);
1989 usb_free_urb(dev
->out_urb
);
1992 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
1995 static struct usb_driver pn533_driver
= {
1997 .probe
= pn533_probe
,
1998 .disconnect
= pn533_disconnect
,
1999 .id_table
= pn533_table
,
2002 module_usb_driver(pn533_driver
);
2004 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2005 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2006 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2007 MODULE_VERSION(VERSION
);
2008 MODULE_LICENSE("GPL");