i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / nfc / pn533.c
blob1a1500bc845233dc1537ea2b05c35379c33907e5
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
4 * Authors:
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>
33 #define VERSION "0.1"
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) },
44 { }
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])
56 /* start of frame */
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
64 /* PN533 Commands */
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
84 struct pn533;
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 {
93 u8 ic;
94 u8 ver;
95 u8 rev;
96 u8 support;
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 {
106 u8 mx_rty_atr;
107 u8 mx_rty_psl;
108 u8 mx_rty_passive_act;
109 } __packed;
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 {
128 struct {
129 u8 afi;
130 u8 polling_method;
131 } __packed type_b;
132 struct {
133 u8 opcode;
134 __be16 sc;
135 u8 rc;
136 u8 tsn;
137 } __packed felica;
140 /* Poll modulations */
141 enum {
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 {
153 struct {
154 u8 maxtg;
155 u8 brty;
156 union pn533_cmd_poll_initdata initiator_data;
157 } __packed data;
158 u8 len;
161 const struct pn533_poll_modulations poll_mod[] = {
162 [PN533_POLL_MOD_106KBPS_A] = {
163 .data = {
164 .maxtg = 1,
165 .brty = 0,
167 .len = 2,
169 [PN533_POLL_MOD_212KBPS_FELICA] = {
170 .data = {
171 .maxtg = 1,
172 .brty = 1,
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,
177 .tsn = 0,
180 .len = 7,
182 [PN533_POLL_MOD_424KBPS_FELICA] = {
183 .data = {
184 .maxtg = 1,
185 .brty = 2,
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,
190 .tsn = 0,
193 .len = 7,
195 [PN533_POLL_MOD_106KBPS_JEWEL] = {
196 .data = {
197 .maxtg = 1,
198 .brty = 4,
200 .len = 2,
202 [PN533_POLL_MOD_847KBPS_B] = {
203 .data = {
204 .maxtg = 1,
205 .brty = 8,
206 .initiator_data.type_b = {
207 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
208 .polling_method =
209 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
212 .len = 3,
216 /* PN533_CMD_IN_ATR */
218 struct pn533_cmd_activate_param {
219 u8 tg;
220 u8 next;
221 } __packed;
223 struct pn533_cmd_activate_response {
224 u8 status;
225 u8 nfcid3t[10];
226 u8 didt;
227 u8 bst;
228 u8 brt;
229 u8 to;
230 u8 ppt;
231 /* optional */
232 u8 gt[];
233 } __packed;
235 /* PN533_CMD_IN_JUMP_FOR_DEP */
236 struct pn533_cmd_jump_dep {
237 u8 active;
238 u8 baud;
239 u8 next;
240 u8 gt[];
241 } __packed;
243 struct pn533_cmd_jump_dep_response {
244 u8 status;
245 u8 tg;
246 u8 nfcid3t[10];
247 u8 didt;
248 u8 bst;
249 u8 brt;
250 u8 to;
251 u8 ppt;
252 /* optional */
253 u8 gt[];
254 } __packed;
256 struct pn533 {
257 struct usb_device *udev;
258 struct usb_interface *interface;
259 struct nfc_dev *nfc_dev;
261 struct urb *out_urb;
262 int out_maxlen;
263 struct pn533_frame *out_frame;
265 struct urb *in_urb;
266 int in_maxlen;
267 struct pn533_frame *in_frame;
269 struct tasklet_struct tasklet;
270 struct pn533_frame *tklt_in_frame;
271 int tklt_in_error;
273 pn533_cmd_complete_t cmd_complete;
274 void *cmd_complete_arg;
275 struct semaphore cmd_lock;
276 u8 cmd;
278 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
279 u8 poll_mod_count;
280 u8 poll_mod_curr;
281 u32 poll_protocols;
283 u8 tgt_available_prots;
284 u8 tgt_active_prot;
287 struct pn533_frame {
288 u8 preamble;
289 __be16 start_frame;
290 u8 datalen;
291 u8 datalen_checksum;
292 u8 data[];
293 } __packed;
295 /* The rule: value + checksum = 0 */
296 static inline u8 pn533_checksum(u8 value)
298 return ~value + 1;
301 /* The rule: sum(data elements) + checksum = 0 */
302 static u8 pn533_data_checksum(u8 *data, int datalen)
304 u8 sum = 0;
305 int i;
307 for (i = 0; i < datalen; i++)
308 sum += data[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)
325 frame->preamble = 0;
326 frame->start_frame = cpu_to_be16(PN533_SOF);
327 frame->datalen = 0;
328 frame->datalen_checksum = 0xFF;
329 /* data[0] is used as postamble */
330 frame->data[0] = 0;
333 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
335 frame->preamble = 0;
336 frame->start_frame = cpu_to_be16(PN533_SOF);
337 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
338 PN533_FRAME_CMD(frame) = cmd;
339 frame->datalen = 2;
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)
354 u8 checksum;
356 if (frame->start_frame != cpu_to_be16(PN533_SOF))
357 return false;
359 checksum = pn533_checksum(frame->datalen);
360 if (checksum != frame->datalen_checksum)
361 return false;
363 checksum = pn533_data_checksum(frame->data, frame->datalen);
364 if (checksum != PN533_FRAME_CHECKSUM(frame))
365 return false;
367 return true;
370 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
372 if (frame->start_frame != cpu_to_be16(PN533_SOF))
373 return false;
375 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
376 return false;
378 return true;
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;
390 int rc;
392 if (dev->tklt_in_error)
393 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
394 dev->tklt_in_error);
395 else
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)
401 up(&dev->cmd_lock);
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) {
412 case 0:
413 /* success */
414 break;
415 case -ECONNRESET:
416 case -ENOENT:
417 case -ESHUTDOWN:
418 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
419 " status: %d", urb->status);
420 dev->tklt_in_error = urb->status;
421 goto sched_tasklet;
422 default:
423 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
424 " %d", urb->status);
425 dev->tklt_in_error = urb->status;
426 goto sched_tasklet;
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;
434 goto sched_tasklet;
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;
441 goto sched_tasklet;
444 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
445 dev->tklt_in_error = 0;
446 dev->tklt_in_frame = in_frame;
448 sched_tasklet:
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;
463 int rc;
465 switch (urb->status) {
466 case 0:
467 /* success */
468 break;
469 case -ECONNRESET:
470 case -ENOENT:
471 case -ESHUTDOWN:
472 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
473 " status: %d", urb->status);
474 dev->tklt_in_error = urb->status;
475 goto sched_tasklet;
476 default:
477 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
478 " %d", urb->status);
479 dev->tklt_in_error = urb->status;
480 goto sched_tasklet;
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;
488 goto sched_tasklet;
491 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
493 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
494 if (rc) {
495 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
496 " result %d", rc);
497 dev->tklt_in_error = rc;
498 goto sched_tasklet;
501 return;
503 sched_tasklet:
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)
517 int rc;
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);
527 return rc;
530 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
531 struct pn533_frame *out_frame,
532 struct pn533_frame *in_frame,
533 int in_frame_len,
534 pn533_cmd_complete_t cmd_complete,
535 void *arg, gfp_t flags)
537 int rc;
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);
554 if (rc)
555 return rc;
557 rc = pn533_submit_urb_for_ack(dev, flags);
558 if (rc)
559 goto error;
561 return 0;
563 error:
564 usb_unlink_urb(dev->out_urb);
565 return rc;
568 static int pn533_send_cmd_frame_async(struct pn533 *dev,
569 struct pn533_frame *out_frame,
570 struct pn533_frame *in_frame,
571 int in_frame_len,
572 pn533_cmd_complete_t cmd_complete,
573 void *arg, gfp_t flags)
575 int rc;
577 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
579 if (down_trylock(&dev->cmd_lock))
580 return -EBUSY;
582 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
583 in_frame_len, cmd_complete, arg, flags);
584 if (rc)
585 goto error;
587 return 0;
588 error:
589 up(&dev->cmd_lock);
590 return rc;
593 struct pn533_sync_cmd_response {
594 int rc;
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__);
605 arg->rc = 0;
607 if (params_len < 0) /* error */
608 arg->rc = params_len;
610 complete(&arg->done);
612 return 0;
615 static int pn533_send_cmd_frame_sync(struct pn533 *dev,
616 struct pn533_frame *out_frame,
617 struct pn533_frame *in_frame,
618 int in_frame_len)
620 int rc;
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);
629 if (rc)
630 return rc;
632 wait_for_completion(&arg.done);
634 return arg.rc;
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) {
644 case 0:
645 /* success */
646 break;
647 case -ECONNRESET:
648 case -ENOENT:
649 case -ESHUTDOWN:
650 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
651 " status: %d", urb->status);
652 break;
653 default:
654 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
655 " %d", urb->status);
659 struct pn533_target_type_a {
660 __be16 sens_res;
661 u8 sel_res;
662 u8 nfcid_len;
663 u8 nfcid_data[];
664 } __packed;
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,
683 int target_data_len)
685 u8 ssd;
686 u8 platconf;
688 if (target_data_len < sizeof(struct pn533_target_type_a))
689 return false;
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))
702 return false;
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)
706 return false;
708 return true;
711 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
712 int tgt_data_len)
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))
719 return -EPROTO;
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;
724 break;
725 case PN533_TYPE_A_SEL_PROT_ISO14443:
726 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
727 break;
728 case PN533_TYPE_A_SEL_PROT_DEP:
729 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
730 break;
731 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
732 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
733 NFC_PROTO_NFC_DEP_MASK;
734 break;
737 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
738 nfc_tgt->sel_res = tgt_type_a->sel_res;
740 return 0;
743 struct pn533_target_felica {
744 u8 pol_res;
745 u8 opcode;
746 u8 nfcid2[8];
747 u8 pad[8];
748 /* optional */
749 u8 syst_code[];
750 } __packed;
752 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
753 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
755 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
756 int target_data_len)
758 if (target_data_len < sizeof(struct pn533_target_felica))
759 return false;
761 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
762 return false;
764 return true;
767 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
768 int tgt_data_len)
770 struct pn533_target_felica *tgt_felica;
772 tgt_felica = (struct pn533_target_felica *) tgt_data;
774 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
775 return -EPROTO;
777 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
778 tgt_felica->nfcid2[1] ==
779 PN533_FELICA_SENSF_NFCID2_DEP_B2)
780 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
781 else
782 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
784 return 0;
787 struct pn533_target_jewel {
788 __be16 sens_res;
789 u8 jewelid[4];
790 } __packed;
792 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
793 int target_data_len)
795 u8 ssd;
796 u8 platconf;
798 if (target_data_len < sizeof(struct pn533_target_jewel))
799 return false;
801 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
802 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
803 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
805 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
806 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
807 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
808 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
809 return false;
811 return true;
814 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
815 int tgt_data_len)
817 struct pn533_target_jewel *tgt_jewel;
819 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
821 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
822 return -EPROTO;
824 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
825 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
827 return 0;
830 struct pn533_type_b_prot_info {
831 u8 bitrate;
832 u8 fsci_type;
833 u8 fwi_adc_fo;
834 } __packed;
836 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
837 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
838 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
840 struct pn533_type_b_sens_res {
841 u8 opcode;
842 u8 nfcid[4];
843 u8 appdata[4];
844 struct pn533_type_b_prot_info prot_info;
845 } __packed;
847 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
849 struct pn533_target_type_b {
850 struct pn533_type_b_sens_res sensb_res;
851 u8 attrib_res_len;
852 u8 attrib_res[];
853 } __packed;
855 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
856 int target_data_len)
858 if (target_data_len < sizeof(struct pn533_target_type_b))
859 return false;
861 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
862 return false;
864 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
865 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
866 return false;
868 return true;
871 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
872 int tgt_data_len)
874 struct pn533_target_type_b *tgt_type_b;
876 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
878 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
879 return -EPROTO;
881 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
883 return 0;
886 struct pn533_poll_response {
887 u8 nbtg;
888 u8 tg;
889 u8 target_data[];
890 } __packed;
892 static int pn533_target_found(struct pn533 *dev,
893 struct pn533_poll_response *resp, int resp_len)
895 int target_data_len;
896 struct nfc_target nfc_tgt;
897 int rc;
899 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
900 dev->poll_mod_curr);
902 if (resp->tg != 1)
903 return -EPROTO;
905 target_data_len = resp_len - sizeof(struct pn533_poll_response);
907 switch (dev->poll_mod_curr) {
908 case PN533_POLL_MOD_106KBPS_A:
909 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
910 target_data_len);
911 break;
912 case PN533_POLL_MOD_212KBPS_FELICA:
913 case PN533_POLL_MOD_424KBPS_FELICA:
914 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
915 target_data_len);
916 break;
917 case PN533_POLL_MOD_106KBPS_JEWEL:
918 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
919 target_data_len);
920 break;
921 case PN533_POLL_MOD_847KBPS_B:
922 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
923 target_data_len);
924 break;
925 default:
926 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
927 " modulation");
928 return -EPROTO;
931 if (rc)
932 return rc;
934 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
935 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
936 " have the desired protocol");
937 return -EAGAIN;
940 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
941 "0x%x", nfc_tgt.supported_protocols);
943 dev->tgt_available_prots = nfc_tgt.supported_protocols;
945 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
947 return 0;
950 static void pn533_poll_reset_mod_list(struct pn533 *dev)
952 dev->poll_mod_count = 0;
955 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
957 dev->poll_mod_active[dev->poll_mod_count] =
958 (struct pn533_poll_modulations *) &poll_mod[mod_index];
959 dev->poll_mod_count++;
962 static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
964 pn533_poll_reset_mod_list(dev);
966 if (protocols & NFC_PROTO_MIFARE_MASK
967 || protocols & NFC_PROTO_ISO14443_MASK
968 || protocols & NFC_PROTO_NFC_DEP_MASK)
969 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
971 if (protocols & NFC_PROTO_FELICA_MASK
972 || protocols & NFC_PROTO_NFC_DEP_MASK) {
973 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
974 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
977 if (protocols & NFC_PROTO_JEWEL_MASK)
978 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
980 if (protocols & NFC_PROTO_ISO14443_MASK)
981 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
984 static void pn533_start_poll_frame(struct pn533_frame *frame,
985 struct pn533_poll_modulations *mod)
988 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
990 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
991 frame->datalen += mod->len;
993 pn533_tx_frame_finish(frame);
996 static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
997 u8 *params, int params_len)
999 struct pn533_poll_response *resp;
1000 struct pn533_poll_modulations *next_mod;
1001 int rc;
1003 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1005 if (params_len == -ENOENT) {
1006 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
1007 " stopped");
1008 goto stop_poll;
1011 if (params_len < 0) {
1012 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
1013 params_len);
1014 goto stop_poll;
1017 resp = (struct pn533_poll_response *) params;
1018 if (resp->nbtg) {
1019 rc = pn533_target_found(dev, resp, params_len);
1021 /* We must stop the poll after a valid target found */
1022 if (rc == 0)
1023 goto stop_poll;
1025 if (rc != -EAGAIN)
1026 nfc_dev_err(&dev->interface->dev, "The target found is"
1027 " not valid - continuing to poll");
1030 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1032 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1034 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1035 dev->poll_mod_curr);
1037 pn533_start_poll_frame(dev->out_frame, next_mod);
1039 /* Don't need to down the semaphore again */
1040 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1041 dev->in_maxlen, pn533_start_poll_complete,
1042 NULL, GFP_ATOMIC);
1044 if (rc == -EPERM) {
1045 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1046 " because poll has been stopped");
1047 goto stop_poll;
1050 if (rc) {
1051 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1052 " next modulation", rc);
1053 goto stop_poll;
1056 /* Inform caller function to do not up the semaphore */
1057 return -EINPROGRESS;
1059 stop_poll:
1060 pn533_poll_reset_mod_list(dev);
1061 dev->poll_protocols = 0;
1062 return 0;
1065 static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
1067 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1068 struct pn533_poll_modulations *start_mod;
1069 int rc;
1071 nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
1072 protocols);
1074 if (dev->poll_mod_count) {
1075 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1076 " active");
1077 return -EBUSY;
1080 if (dev->tgt_active_prot) {
1081 nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
1082 " already activated");
1083 return -EBUSY;
1086 pn533_poll_create_mod_list(dev, protocols);
1088 if (!dev->poll_mod_count) {
1089 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1090 " specified");
1091 rc = -EINVAL;
1092 goto error;
1095 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1096 dev->poll_mod_count);
1098 dev->poll_mod_curr = 0;
1099 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1101 pn533_start_poll_frame(dev->out_frame, start_mod);
1103 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1104 dev->in_maxlen, pn533_start_poll_complete,
1105 NULL, GFP_KERNEL);
1107 if (rc) {
1108 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1109 " start poll", rc);
1110 goto error;
1113 dev->poll_protocols = protocols;
1115 return 0;
1117 error:
1118 pn533_poll_reset_mod_list(dev);
1119 return rc;
1122 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1124 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1126 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1128 if (!dev->poll_mod_count) {
1129 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1130 " running");
1131 return;
1134 /* An ack will cancel the last issued command (poll) */
1135 pn533_send_ack(dev, GFP_KERNEL);
1137 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1138 usb_kill_urb(dev->in_urb);
1141 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1143 struct pn533_cmd_activate_param param;
1144 struct pn533_cmd_activate_response *resp;
1145 u16 gt_len;
1146 int rc;
1148 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1150 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1152 param.tg = 1;
1153 param.next = 0;
1154 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1155 sizeof(struct pn533_cmd_activate_param));
1156 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1158 pn533_tx_frame_finish(dev->out_frame);
1160 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1161 dev->in_maxlen);
1162 if (rc)
1163 return rc;
1165 resp = (struct pn533_cmd_activate_response *)
1166 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1167 rc = resp->status & PN533_CMD_RET_MASK;
1168 if (rc != PN533_CMD_RET_SUCCESS)
1169 return -EIO;
1171 /* ATR_RES general bytes are located at offset 16 */
1172 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1173 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1175 return rc;
1178 static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
1179 u32 protocol)
1181 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1182 int rc;
1184 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1185 protocol);
1187 if (dev->poll_mod_count) {
1188 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1189 " polling");
1190 return -EBUSY;
1193 if (dev->tgt_active_prot) {
1194 nfc_dev_err(&dev->interface->dev, "There is already an active"
1195 " target");
1196 return -EBUSY;
1199 if (!dev->tgt_available_prots) {
1200 nfc_dev_err(&dev->interface->dev, "There is no available target"
1201 " to activate");
1202 return -EINVAL;
1205 if (!(dev->tgt_available_prots & (1 << protocol))) {
1206 nfc_dev_err(&dev->interface->dev, "The target does not support"
1207 " the requested protocol %u", protocol);
1208 return -EINVAL;
1211 if (protocol == NFC_PROTO_NFC_DEP) {
1212 rc = pn533_activate_target_nfcdep(dev);
1213 if (rc) {
1214 nfc_dev_err(&dev->interface->dev, "Error %d when"
1215 " activating target with"
1216 " NFC_DEP protocol", rc);
1217 return rc;
1221 dev->tgt_active_prot = protocol;
1222 dev->tgt_available_prots = 0;
1224 return 0;
1227 static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
1229 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1230 u8 tg;
1231 u8 status;
1232 int rc;
1234 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1236 if (!dev->tgt_active_prot) {
1237 nfc_dev_err(&dev->interface->dev, "There is no active target");
1238 return;
1241 dev->tgt_active_prot = 0;
1243 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1245 tg = 1;
1246 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1247 dev->out_frame->datalen += sizeof(u8);
1249 pn533_tx_frame_finish(dev->out_frame);
1251 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1252 dev->in_maxlen);
1253 if (rc) {
1254 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1255 " command to the controller");
1256 return;
1259 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1260 rc = status & PN533_CMD_RET_MASK;
1261 if (rc != PN533_CMD_RET_SUCCESS)
1262 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1263 " the target", rc);
1265 return;
1269 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1270 u8 *params, int params_len)
1272 struct pn533_cmd_jump_dep *cmd;
1273 struct pn533_cmd_jump_dep_response *resp;
1274 struct nfc_target nfc_target;
1275 u8 target_gt_len;
1276 int rc;
1278 if (params_len == -ENOENT) {
1279 nfc_dev_dbg(&dev->interface->dev, "");
1280 return 0;
1283 if (params_len < 0) {
1284 nfc_dev_err(&dev->interface->dev,
1285 "Error %d when bringing DEP link up",
1286 params_len);
1287 return 0;
1290 if (dev->tgt_available_prots &&
1291 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1292 nfc_dev_err(&dev->interface->dev,
1293 "The target does not support DEP");
1294 return -EINVAL;
1297 resp = (struct pn533_cmd_jump_dep_response *) params;
1298 cmd = (struct pn533_cmd_jump_dep *) arg;
1299 rc = resp->status & PN533_CMD_RET_MASK;
1300 if (rc != PN533_CMD_RET_SUCCESS) {
1301 nfc_dev_err(&dev->interface->dev,
1302 "Bringing DEP link up failed %d", rc);
1303 return 0;
1306 if (!dev->tgt_available_prots) {
1307 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1309 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1310 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1311 if (rc)
1312 return 0;
1314 dev->tgt_available_prots = 0;
1317 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1319 /* ATR_RES general bytes are located at offset 17 */
1320 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1321 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1322 resp->gt, target_gt_len);
1323 if (rc == 0)
1324 rc = nfc_dep_link_is_up(dev->nfc_dev,
1325 dev->nfc_dev->targets[0].idx,
1326 !cmd->active, NFC_RF_INITIATOR);
1328 return 0;
1331 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, int target_idx,
1332 u8 comm_mode, u8 rf_mode)
1334 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1335 struct pn533_cmd_jump_dep *cmd;
1336 u8 cmd_len, local_gt_len, *local_gt;
1337 int rc;
1339 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1341 if (rf_mode == NFC_RF_TARGET) {
1342 nfc_dev_err(&dev->interface->dev, "Target mode not supported");
1343 return -EOPNOTSUPP;
1347 if (dev->poll_mod_count) {
1348 nfc_dev_err(&dev->interface->dev,
1349 "Cannot bring the DEP link up while polling");
1350 return -EBUSY;
1353 if (dev->tgt_active_prot) {
1354 nfc_dev_err(&dev->interface->dev,
1355 "There is already an active target");
1356 return -EBUSY;
1359 local_gt = nfc_get_local_general_bytes(dev->nfc_dev, &local_gt_len);
1360 if (local_gt_len > NFC_MAX_GT_LEN)
1361 return -EINVAL;
1363 cmd_len = sizeof(struct pn533_cmd_jump_dep) + local_gt_len;
1364 cmd = kzalloc(cmd_len, GFP_KERNEL);
1365 if (cmd == NULL)
1366 return -ENOMEM;
1368 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1370 cmd->active = !comm_mode;
1371 cmd->baud = 0;
1372 if (local_gt != NULL) {
1373 cmd->next = 4; /* We have some Gi */
1374 memcpy(cmd->gt, local_gt, local_gt_len);
1375 } else {
1376 cmd->next = 0;
1379 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1380 dev->out_frame->datalen += cmd_len;
1382 pn533_tx_frame_finish(dev->out_frame);
1384 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1385 dev->in_maxlen, pn533_in_dep_link_up_complete,
1386 cmd, GFP_KERNEL);
1387 if (rc)
1388 goto out;
1391 out:
1392 kfree(cmd);
1394 return rc;
1397 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1399 pn533_deactivate_target(nfc_dev, 0);
1401 return 0;
1404 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1405 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1407 static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1409 int payload_len = skb->len;
1410 struct pn533_frame *out_frame;
1411 u8 tg;
1413 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1414 payload_len);
1416 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1417 /* TODO: Implement support to multi-part data exchange */
1418 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1419 " max allowed: %d",
1420 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1421 return -ENOSYS;
1424 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1425 out_frame = (struct pn533_frame *) skb->data;
1427 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1429 tg = 1;
1430 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1431 out_frame->datalen += sizeof(u8);
1433 /* The data is already in the out_frame, just update the datalen */
1434 out_frame->datalen += payload_len;
1436 pn533_tx_frame_finish(out_frame);
1437 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1439 return 0;
1442 struct pn533_data_exchange_arg {
1443 struct sk_buff *skb_resp;
1444 struct sk_buff *skb_out;
1445 data_exchange_cb_t cb;
1446 void *cb_context;
1449 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1450 u8 *params, int params_len)
1452 struct pn533_data_exchange_arg *arg = _arg;
1453 struct sk_buff *skb_resp = arg->skb_resp;
1454 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1455 int err = 0;
1456 u8 status;
1457 u8 cmd_ret;
1459 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1461 dev_kfree_skb_irq(arg->skb_out);
1463 if (params_len < 0) { /* error */
1464 err = params_len;
1465 goto error;
1468 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1470 status = params[0];
1472 cmd_ret = status & PN533_CMD_RET_MASK;
1473 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1474 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1475 " exchanging data", cmd_ret);
1476 err = -EIO;
1477 goto error;
1480 if (status & PN533_CMD_MI_MASK) {
1481 /* TODO: Implement support to multi-part data exchange */
1482 nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
1483 " supported");
1484 /* Prevent the other messages from controller */
1485 pn533_send_ack(dev, GFP_ATOMIC);
1486 err = -ENOSYS;
1487 goto error;
1490 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1491 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1493 arg->cb(arg->cb_context, skb_resp, 0);
1494 kfree(arg);
1495 return 0;
1497 error:
1498 dev_kfree_skb_irq(skb_resp);
1499 arg->cb(arg->cb_context, NULL, err);
1500 kfree(arg);
1501 return 0;
1504 static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
1505 struct sk_buff *skb,
1506 data_exchange_cb_t cb,
1507 void *cb_context)
1509 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1510 struct pn533_frame *out_frame, *in_frame;
1511 struct pn533_data_exchange_arg *arg;
1512 struct sk_buff *skb_resp;
1513 int skb_resp_len;
1514 int rc;
1516 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1518 if (!dev->tgt_active_prot) {
1519 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1520 " there is no active target");
1521 rc = -EINVAL;
1522 goto error;
1525 rc = pn533_data_exchange_tx_frame(dev, skb);
1526 if (rc)
1527 goto error;
1529 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1530 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1531 PN533_FRAME_TAIL_SIZE;
1533 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1534 if (!skb_resp) {
1535 rc = -ENOMEM;
1536 goto error;
1539 in_frame = (struct pn533_frame *) skb_resp->data;
1540 out_frame = (struct pn533_frame *) skb->data;
1542 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1543 if (!arg) {
1544 rc = -ENOMEM;
1545 goto free_skb_resp;
1548 arg->skb_resp = skb_resp;
1549 arg->skb_out = skb;
1550 arg->cb = cb;
1551 arg->cb_context = cb_context;
1553 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1554 pn533_data_exchange_complete, arg,
1555 GFP_KERNEL);
1556 if (rc) {
1557 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1558 " perform data_exchange", rc);
1559 goto free_arg;
1562 return 0;
1564 free_arg:
1565 kfree(arg);
1566 free_skb_resp:
1567 kfree_skb(skb_resp);
1568 error:
1569 kfree_skb(skb);
1570 return rc;
1573 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1574 u8 cfgdata_len)
1576 int rc;
1577 u8 *params;
1579 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1581 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1583 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1584 params[0] = cfgitem;
1585 memcpy(&params[1], cfgdata, cfgdata_len);
1586 dev->out_frame->datalen += (1 + cfgdata_len);
1588 pn533_tx_frame_finish(dev->out_frame);
1590 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1591 dev->in_maxlen);
1593 return rc;
1596 struct nfc_ops pn533_nfc_ops = {
1597 .dev_up = NULL,
1598 .dev_down = NULL,
1599 .dep_link_up = pn533_dep_link_up,
1600 .dep_link_down = pn533_dep_link_down,
1601 .start_poll = pn533_start_poll,
1602 .stop_poll = pn533_stop_poll,
1603 .activate_target = pn533_activate_target,
1604 .deactivate_target = pn533_deactivate_target,
1605 .data_exchange = pn533_data_exchange,
1608 static int pn533_probe(struct usb_interface *interface,
1609 const struct usb_device_id *id)
1611 struct pn533_fw_version *fw_ver;
1612 struct pn533 *dev;
1613 struct usb_host_interface *iface_desc;
1614 struct usb_endpoint_descriptor *endpoint;
1615 struct pn533_config_max_retries max_retries;
1616 int in_endpoint = 0;
1617 int out_endpoint = 0;
1618 int rc = -ENOMEM;
1619 int i;
1620 u32 protocols;
1622 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1623 if (!dev)
1624 return -ENOMEM;
1626 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1627 dev->interface = interface;
1628 sema_init(&dev->cmd_lock, 1);
1630 iface_desc = interface->cur_altsetting;
1631 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1632 endpoint = &iface_desc->endpoint[i].desc;
1634 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1635 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1636 in_endpoint = endpoint->bEndpointAddress;
1639 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1640 dev->out_maxlen =
1641 le16_to_cpu(endpoint->wMaxPacketSize);
1642 out_endpoint = endpoint->bEndpointAddress;
1646 if (!in_endpoint || !out_endpoint) {
1647 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1648 " bulk-out endpoint");
1649 rc = -ENODEV;
1650 goto error;
1653 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1654 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1655 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1656 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1658 if (!dev->in_frame || !dev->out_frame ||
1659 !dev->in_urb || !dev->out_urb)
1660 goto error;
1662 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1663 usb_rcvbulkpipe(dev->udev, in_endpoint),
1664 NULL, 0, NULL, dev);
1665 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1666 usb_sndbulkpipe(dev->udev, out_endpoint),
1667 NULL, 0,
1668 pn533_send_complete, dev);
1670 tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
1672 usb_set_intfdata(interface, dev);
1674 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
1675 pn533_tx_frame_finish(dev->out_frame);
1677 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1678 dev->in_maxlen);
1679 if (rc)
1680 goto kill_tasklet;
1682 fw_ver = (struct pn533_fw_version *)
1683 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1684 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
1685 " attached", fw_ver->ver, fw_ver->rev);
1687 protocols = NFC_PROTO_JEWEL_MASK
1688 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
1689 | NFC_PROTO_ISO14443_MASK
1690 | NFC_PROTO_NFC_DEP_MASK;
1692 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
1693 PN533_CMD_DATAEXCH_HEAD_LEN,
1694 PN533_FRAME_TAIL_SIZE);
1695 if (!dev->nfc_dev)
1696 goto kill_tasklet;
1698 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
1699 nfc_set_drvdata(dev->nfc_dev, dev);
1701 rc = nfc_register_device(dev->nfc_dev);
1702 if (rc)
1703 goto free_nfc_dev;
1705 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
1706 max_retries.mx_rty_psl = 2;
1707 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
1709 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
1710 (u8 *) &max_retries, sizeof(max_retries));
1712 if (rc) {
1713 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
1714 " config");
1715 goto free_nfc_dev;
1718 return 0;
1720 free_nfc_dev:
1721 nfc_free_device(dev->nfc_dev);
1722 kill_tasklet:
1723 tasklet_kill(&dev->tasklet);
1724 error:
1725 kfree(dev->in_frame);
1726 usb_free_urb(dev->in_urb);
1727 kfree(dev->out_frame);
1728 usb_free_urb(dev->out_urb);
1729 kfree(dev);
1730 return rc;
1733 static void pn533_disconnect(struct usb_interface *interface)
1735 struct pn533 *dev;
1737 dev = usb_get_intfdata(interface);
1738 usb_set_intfdata(interface, NULL);
1740 nfc_unregister_device(dev->nfc_dev);
1741 nfc_free_device(dev->nfc_dev);
1743 usb_kill_urb(dev->in_urb);
1744 usb_kill_urb(dev->out_urb);
1746 tasklet_kill(&dev->tasklet);
1748 kfree(dev->in_frame);
1749 usb_free_urb(dev->in_urb);
1750 kfree(dev->out_frame);
1751 usb_free_urb(dev->out_urb);
1752 kfree(dev);
1754 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
1757 static struct usb_driver pn533_driver = {
1758 .name = "pn533",
1759 .probe = pn533_probe,
1760 .disconnect = pn533_disconnect,
1761 .id_table = pn533_table,
1764 module_usb_driver(pn533_driver);
1766 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1767 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1768 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
1769 MODULE_VERSION(VERSION);
1770 MODULE_LICENSE("GPL");