NFC: Implement the pn533 target mode data fetching routine
[cascardo/linux.git] / drivers / nfc / pn533.c
1 /*
2  * Copyright (C) 2011 Instituto Nokia de Tecnologia
3  *
4  * Authors:
5  *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6  *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7  *
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.
12  *
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.
17  *
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.
22  */
23
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>
32
33 #define VERSION "0.1"
34
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
37
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
40
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         { }
45 };
46 MODULE_DEVICE_TABLE(usb, pn533_table);
47
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])
55
56 /* start of frame */
57 #define PN533_SOF 0x00FF
58
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
63
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)
68
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
76
77 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
78 #define PN533_CMD_TG_GET_DATA 0x86
79
80 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
81
82 /* PN533 Return codes */
83 #define PN533_CMD_RET_MASK 0x3F
84 #define PN533_CMD_MI_MASK 0x40
85 #define PN533_CMD_RET_SUCCESS 0x00
86
87 /* PN533 status codes */
88 #define PN533_STATUS_TARGET_RELEASED 0x29
89
90 struct pn533;
91
92 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
93                                         u8 *params, int params_len);
94
95 /* structs for pn533 commands */
96
97 /* PN533_CMD_GET_FIRMWARE_VERSION */
98 struct pn533_fw_version {
99         u8 ic;
100         u8 ver;
101         u8 rev;
102         u8 support;
103 };
104
105 /* PN533_CMD_RF_CONFIGURATION */
106 #define PN533_CFGITEM_MAX_RETRIES 0x05
107
108 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
109 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
110
111 struct pn533_config_max_retries {
112         u8 mx_rty_atr;
113         u8 mx_rty_psl;
114         u8 mx_rty_passive_act;
115 } __packed;
116
117 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
118
119 /* felica commands opcode */
120 #define PN533_FELICA_OPC_SENSF_REQ 0
121 #define PN533_FELICA_OPC_SENSF_RES 1
122 /* felica SENSF_REQ parameters */
123 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
124 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
125 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
126 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
127
128 /* type B initiator_data values */
129 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
130 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
131 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
132
133 union pn533_cmd_poll_initdata {
134         struct {
135                 u8 afi;
136                 u8 polling_method;
137         } __packed type_b;
138         struct {
139                 u8 opcode;
140                 __be16 sc;
141                 u8 rc;
142                 u8 tsn;
143         } __packed felica;
144 };
145
146 /* Poll modulations */
147 enum {
148         PN533_POLL_MOD_106KBPS_A,
149         PN533_POLL_MOD_212KBPS_FELICA,
150         PN533_POLL_MOD_424KBPS_FELICA,
151         PN533_POLL_MOD_106KBPS_JEWEL,
152         PN533_POLL_MOD_847KBPS_B,
153
154         __PN533_POLL_MOD_AFTER_LAST,
155 };
156 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
157
158 struct pn533_poll_modulations {
159         struct {
160                 u8 maxtg;
161                 u8 brty;
162                 union pn533_cmd_poll_initdata initiator_data;
163         } __packed data;
164         u8 len;
165 };
166
167 const struct pn533_poll_modulations poll_mod[] = {
168         [PN533_POLL_MOD_106KBPS_A] = {
169                 .data = {
170                         .maxtg = 1,
171                         .brty = 0,
172                 },
173                 .len = 2,
174         },
175         [PN533_POLL_MOD_212KBPS_FELICA] = {
176                 .data = {
177                         .maxtg = 1,
178                         .brty = 1,
179                         .initiator_data.felica = {
180                                 .opcode = PN533_FELICA_OPC_SENSF_REQ,
181                                 .sc = PN533_FELICA_SENSF_SC_ALL,
182                                 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
183                                 .tsn = 0,
184                         },
185                 },
186                 .len = 7,
187         },
188         [PN533_POLL_MOD_424KBPS_FELICA] = {
189                 .data = {
190                         .maxtg = 1,
191                         .brty = 2,
192                         .initiator_data.felica = {
193                                 .opcode = PN533_FELICA_OPC_SENSF_REQ,
194                                 .sc = PN533_FELICA_SENSF_SC_ALL,
195                                 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
196                                 .tsn = 0,
197                         },
198                  },
199                 .len = 7,
200         },
201         [PN533_POLL_MOD_106KBPS_JEWEL] = {
202                 .data = {
203                         .maxtg = 1,
204                         .brty = 4,
205                 },
206                 .len = 2,
207         },
208         [PN533_POLL_MOD_847KBPS_B] = {
209                 .data = {
210                         .maxtg = 1,
211                         .brty = 8,
212                         .initiator_data.type_b = {
213                                 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
214                                 .polling_method =
215                                         PN533_TYPE_B_POLL_METHOD_TIMESLOT,
216                         },
217                 },
218                 .len = 3,
219         },
220 };
221
222 /* PN533_CMD_IN_ATR */
223
224 struct pn533_cmd_activate_param {
225         u8 tg;
226         u8 next;
227 } __packed;
228
229 struct pn533_cmd_activate_response {
230         u8 status;
231         u8 nfcid3t[10];
232         u8 didt;
233         u8 bst;
234         u8 brt;
235         u8 to;
236         u8 ppt;
237         /* optional */
238         u8 gt[];
239 } __packed;
240
241 /* PN533_CMD_IN_JUMP_FOR_DEP */
242 struct pn533_cmd_jump_dep {
243         u8 active;
244         u8 baud;
245         u8 next;
246         u8 gt[];
247 } __packed;
248
249 struct pn533_cmd_jump_dep_response {
250         u8 status;
251         u8 tg;
252         u8 nfcid3t[10];
253         u8 didt;
254         u8 bst;
255         u8 brt;
256         u8 to;
257         u8 ppt;
258         /* optional */
259         u8 gt[];
260 } __packed;
261
262
263 /* PN533_TG_INIT_AS_TARGET */
264 #define PN533_INIT_TARGET_PASSIVE 0x1
265 #define PN533_INIT_TARGET_DEP 0x2
266
267 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
268 #define PN533_INIT_TARGET_RESP_ACTIVE     0x1
269 #define PN533_INIT_TARGET_RESP_DEP        0x4
270
271 struct pn533_cmd_init_target {
272         u8 mode;
273         u8 mifare[6];
274         u8 felica[18];
275         u8 nfcid3[10];
276         u8 gb_len;
277         u8 gb[];
278 } __packed;
279
280 struct pn533_cmd_init_target_response {
281         u8 mode;
282         u8 cmd[];
283 } __packed;
284
285 struct pn533 {
286         struct usb_device *udev;
287         struct usb_interface *interface;
288         struct nfc_dev *nfc_dev;
289
290         struct urb *out_urb;
291         int out_maxlen;
292         struct pn533_frame *out_frame;
293
294         struct urb *in_urb;
295         int in_maxlen;
296         struct pn533_frame *in_frame;
297
298         struct sk_buff_head resp_q;
299
300         struct workqueue_struct *wq;
301         struct work_struct cmd_work;
302         struct work_struct mi_work;
303         struct work_struct tg_work;
304         struct pn533_frame *wq_in_frame;
305         int wq_in_error;
306
307         pn533_cmd_complete_t cmd_complete;
308         void *cmd_complete_arg;
309         struct semaphore cmd_lock;
310         u8 cmd;
311
312         struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
313         u8 poll_mod_count;
314         u8 poll_mod_curr;
315         u32 poll_protocols;
316
317         u8 tgt_available_prots;
318         u8 tgt_active_prot;
319 };
320
321 struct pn533_frame {
322         u8 preamble;
323         __be16 start_frame;
324         u8 datalen;
325         u8 datalen_checksum;
326         u8 data[];
327 } __packed;
328
329 /* The rule: value + checksum = 0 */
330 static inline u8 pn533_checksum(u8 value)
331 {
332         return ~value + 1;
333 }
334
335 /* The rule: sum(data elements) + checksum = 0 */
336 static u8 pn533_data_checksum(u8 *data, int datalen)
337 {
338         u8 sum = 0;
339         int i;
340
341         for (i = 0; i < datalen; i++)
342                 sum += data[i];
343
344         return pn533_checksum(sum);
345 }
346
347 /**
348  * pn533_tx_frame_ack - create a ack frame
349  * @frame:      The frame to be set as ack
350  *
351  * Ack is different type of standard frame. As a standard frame, it has
352  * preamble and start_frame. However the checksum of this frame must fail,
353  * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
354  * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
355  * After datalen_checksum field, the postamble is placed.
356  */
357 static void pn533_tx_frame_ack(struct pn533_frame *frame)
358 {
359         frame->preamble = 0;
360         frame->start_frame = cpu_to_be16(PN533_SOF);
361         frame->datalen = 0;
362         frame->datalen_checksum = 0xFF;
363         /* data[0] is used as postamble */
364         frame->data[0] = 0;
365 }
366
367 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
368 {
369         frame->preamble = 0;
370         frame->start_frame = cpu_to_be16(PN533_SOF);
371         PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
372         PN533_FRAME_CMD(frame) = cmd;
373         frame->datalen = 2;
374 }
375
376 static void pn533_tx_frame_finish(struct pn533_frame *frame)
377 {
378         frame->datalen_checksum = pn533_checksum(frame->datalen);
379
380         PN533_FRAME_CHECKSUM(frame) =
381                 pn533_data_checksum(frame->data, frame->datalen);
382
383         PN533_FRAME_POSTAMBLE(frame) = 0;
384 }
385
386 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
387 {
388         u8 checksum;
389
390         if (frame->start_frame != cpu_to_be16(PN533_SOF))
391                 return false;
392
393         checksum = pn533_checksum(frame->datalen);
394         if (checksum != frame->datalen_checksum)
395                 return false;
396
397         checksum = pn533_data_checksum(frame->data, frame->datalen);
398         if (checksum != PN533_FRAME_CHECKSUM(frame))
399                 return false;
400
401         return true;
402 }
403
404 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
405 {
406         if (frame->start_frame != cpu_to_be16(PN533_SOF))
407                 return false;
408
409         if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
410                 return false;
411
412         return true;
413 }
414
415 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
416 {
417         return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
418 }
419
420
421 static void pn533_wq_cmd_complete(struct work_struct *work)
422 {
423         struct pn533 *dev = container_of(work, struct pn533, cmd_work);
424         struct pn533_frame *in_frame;
425         int rc;
426
427         in_frame = dev->wq_in_frame;
428
429         if (dev->wq_in_error)
430                 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
431                                                         dev->wq_in_error);
432         else
433                 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
434                                         PN533_FRAME_CMD_PARAMS_PTR(in_frame),
435                                         PN533_FRAME_CMD_PARAMS_LEN(in_frame));
436
437         if (rc != -EINPROGRESS)
438                 up(&dev->cmd_lock);
439 }
440
441 static void pn533_recv_response(struct urb *urb)
442 {
443         struct pn533 *dev = urb->context;
444         struct pn533_frame *in_frame;
445
446         dev->wq_in_frame = NULL;
447
448         switch (urb->status) {
449         case 0:
450                 /* success */
451                 break;
452         case -ECONNRESET:
453         case -ENOENT:
454         case -ESHUTDOWN:
455                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
456                                                 " status: %d", urb->status);
457                 dev->wq_in_error = urb->status;
458                 goto sched_wq;
459         default:
460                 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
461                                                         " %d", urb->status);
462                 dev->wq_in_error = urb->status;
463                 goto sched_wq;
464         }
465
466         in_frame = dev->in_urb->transfer_buffer;
467
468         if (!pn533_rx_frame_is_valid(in_frame)) {
469                 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
470                 dev->wq_in_error = -EIO;
471                 goto sched_wq;
472         }
473
474         if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
475                 nfc_dev_err(&dev->interface->dev, "The received frame is not "
476                                                 "response to the last command");
477                 dev->wq_in_error = -EIO;
478                 goto sched_wq;
479         }
480
481         nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
482         dev->wq_in_error = 0;
483         dev->wq_in_frame = in_frame;
484
485 sched_wq:
486         queue_work(dev->wq, &dev->cmd_work);
487 }
488
489 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
490 {
491         dev->in_urb->complete = pn533_recv_response;
492
493         return usb_submit_urb(dev->in_urb, flags);
494 }
495
496 static void pn533_recv_ack(struct urb *urb)
497 {
498         struct pn533 *dev = urb->context;
499         struct pn533_frame *in_frame;
500         int rc;
501
502         switch (urb->status) {
503         case 0:
504                 /* success */
505                 break;
506         case -ECONNRESET:
507         case -ENOENT:
508         case -ESHUTDOWN:
509                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
510                                                 " status: %d", urb->status);
511                 dev->wq_in_error = urb->status;
512                 goto sched_wq;
513         default:
514                 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
515                                                         " %d", urb->status);
516                 dev->wq_in_error = urb->status;
517                 goto sched_wq;
518         }
519
520         in_frame = dev->in_urb->transfer_buffer;
521
522         if (!pn533_rx_frame_is_ack(in_frame)) {
523                 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
524                 dev->wq_in_error = -EIO;
525                 goto sched_wq;
526         }
527
528         nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
529
530         rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
531         if (rc) {
532                 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
533                                                         " result %d", rc);
534                 dev->wq_in_error = rc;
535                 goto sched_wq;
536         }
537
538         return;
539
540 sched_wq:
541         dev->wq_in_frame = NULL;
542         queue_work(dev->wq, &dev->cmd_work);
543 }
544
545 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
546 {
547         dev->in_urb->complete = pn533_recv_ack;
548
549         return usb_submit_urb(dev->in_urb, flags);
550 }
551
552 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
553 {
554         int rc;
555
556         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
557
558         pn533_tx_frame_ack(dev->out_frame);
559
560         dev->out_urb->transfer_buffer = dev->out_frame;
561         dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
562         rc = usb_submit_urb(dev->out_urb, flags);
563
564         return rc;
565 }
566
567 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
568                                         struct pn533_frame *out_frame,
569                                         struct pn533_frame *in_frame,
570                                         int in_frame_len,
571                                         pn533_cmd_complete_t cmd_complete,
572                                         void *arg, gfp_t flags)
573 {
574         int rc;
575
576         nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
577                                                 PN533_FRAME_CMD(out_frame));
578
579         dev->cmd = PN533_FRAME_CMD(out_frame);
580         dev->cmd_complete = cmd_complete;
581         dev->cmd_complete_arg = arg;
582
583         dev->out_urb->transfer_buffer = out_frame;
584         dev->out_urb->transfer_buffer_length =
585                                 PN533_FRAME_SIZE(out_frame);
586
587         dev->in_urb->transfer_buffer = in_frame;
588         dev->in_urb->transfer_buffer_length = in_frame_len;
589
590         rc = usb_submit_urb(dev->out_urb, flags);
591         if (rc)
592                 return rc;
593
594         rc = pn533_submit_urb_for_ack(dev, flags);
595         if (rc)
596                 goto error;
597
598         return 0;
599
600 error:
601         usb_unlink_urb(dev->out_urb);
602         return rc;
603 }
604
605 static int pn533_send_cmd_frame_async(struct pn533 *dev,
606                                         struct pn533_frame *out_frame,
607                                         struct pn533_frame *in_frame,
608                                         int in_frame_len,
609                                         pn533_cmd_complete_t cmd_complete,
610                                         void *arg, gfp_t flags)
611 {
612         int rc;
613
614         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
615
616         if (down_trylock(&dev->cmd_lock))
617                 return -EBUSY;
618
619         rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
620                                         in_frame_len, cmd_complete, arg, flags);
621         if (rc)
622                 goto error;
623
624         return 0;
625 error:
626         up(&dev->cmd_lock);
627         return rc;
628 }
629
630 struct pn533_sync_cmd_response {
631         int rc;
632         struct completion done;
633 };
634
635 static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
636                                         u8 *params, int params_len)
637 {
638         struct pn533_sync_cmd_response *arg = _arg;
639
640         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
641
642         arg->rc = 0;
643
644         if (params_len < 0) /* error */
645                 arg->rc = params_len;
646
647         complete(&arg->done);
648
649         return 0;
650 }
651
652 static int pn533_send_cmd_frame_sync(struct pn533 *dev,
653                                                 struct pn533_frame *out_frame,
654                                                 struct pn533_frame *in_frame,
655                                                 int in_frame_len)
656 {
657         int rc;
658         struct pn533_sync_cmd_response arg;
659
660         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
661
662         init_completion(&arg.done);
663
664         rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
665                                 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
666         if (rc)
667                 return rc;
668
669         wait_for_completion(&arg.done);
670
671         return arg.rc;
672 }
673
674 static void pn533_send_complete(struct urb *urb)
675 {
676         struct pn533 *dev = urb->context;
677
678         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
679
680         switch (urb->status) {
681         case 0:
682                 /* success */
683                 break;
684         case -ECONNRESET:
685         case -ENOENT:
686         case -ESHUTDOWN:
687                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
688                                                 " status: %d", urb->status);
689                 break;
690         default:
691                 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
692                                                         " %d", urb->status);
693         }
694 }
695
696 struct pn533_target_type_a {
697         __be16 sens_res;
698         u8 sel_res;
699         u8 nfcid_len;
700         u8 nfcid_data[];
701 } __packed;
702
703
704 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
705 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
706 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
707
708 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
709 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
710
711 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
712 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
713
714 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
715 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
716 #define PN533_TYPE_A_SEL_PROT_DEP 2
717 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
718
719 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
720                                                         int target_data_len)
721 {
722         u8 ssd;
723         u8 platconf;
724
725         if (target_data_len < sizeof(struct pn533_target_type_a))
726                 return false;
727
728         /* The lenght check of nfcid[] and ats[] are not being performed because
729            the values are not being used */
730
731         /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
732         ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
733         platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
734
735         if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
736                         platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
737                         (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
738                         platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
739                 return false;
740
741         /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
742         if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
743                 return false;
744
745         return true;
746 }
747
748 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
749                                                         int tgt_data_len)
750 {
751         struct pn533_target_type_a *tgt_type_a;
752
753         tgt_type_a = (struct pn533_target_type_a *) tgt_data;
754
755         if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
756                 return -EPROTO;
757
758         switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
759         case PN533_TYPE_A_SEL_PROT_MIFARE:
760                 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
761                 break;
762         case PN533_TYPE_A_SEL_PROT_ISO14443:
763                 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
764                 break;
765         case PN533_TYPE_A_SEL_PROT_DEP:
766                 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
767                 break;
768         case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
769                 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
770                                                         NFC_PROTO_NFC_DEP_MASK;
771                 break;
772         }
773
774         nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
775         nfc_tgt->sel_res = tgt_type_a->sel_res;
776         nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
777         memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
778
779         return 0;
780 }
781
782 struct pn533_target_felica {
783         u8 pol_res;
784         u8 opcode;
785         u8 nfcid2[8];
786         u8 pad[8];
787         /* optional */
788         u8 syst_code[];
789 } __packed;
790
791 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
792 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
793
794 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
795                                                         int target_data_len)
796 {
797         if (target_data_len < sizeof(struct pn533_target_felica))
798                 return false;
799
800         if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
801                 return false;
802
803         return true;
804 }
805
806 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
807                                                         int tgt_data_len)
808 {
809         struct pn533_target_felica *tgt_felica;
810
811         tgt_felica = (struct pn533_target_felica *) tgt_data;
812
813         if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
814                 return -EPROTO;
815
816         if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
817                                         tgt_felica->nfcid2[1] ==
818                                         PN533_FELICA_SENSF_NFCID2_DEP_B2)
819                 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
820         else
821                 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
822
823         memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
824         nfc_tgt->sensf_res_len = 9;
825
826         return 0;
827 }
828
829 struct pn533_target_jewel {
830         __be16 sens_res;
831         u8 jewelid[4];
832 } __packed;
833
834 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
835                                                         int target_data_len)
836 {
837         u8 ssd;
838         u8 platconf;
839
840         if (target_data_len < sizeof(struct pn533_target_jewel))
841                 return false;
842
843         /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
844         ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
845         platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
846
847         if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
848                         platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
849                         (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
850                         platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
851                 return false;
852
853         return true;
854 }
855
856 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
857                                                         int tgt_data_len)
858 {
859         struct pn533_target_jewel *tgt_jewel;
860
861         tgt_jewel = (struct pn533_target_jewel *) tgt_data;
862
863         if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
864                 return -EPROTO;
865
866         nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
867         nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
868         nfc_tgt->nfcid1_len = 4;
869         memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
870
871         return 0;
872 }
873
874 struct pn533_type_b_prot_info {
875         u8 bitrate;
876         u8 fsci_type;
877         u8 fwi_adc_fo;
878 } __packed;
879
880 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
881 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
882 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
883
884 struct pn533_type_b_sens_res {
885         u8 opcode;
886         u8 nfcid[4];
887         u8 appdata[4];
888         struct pn533_type_b_prot_info prot_info;
889 } __packed;
890
891 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
892
893 struct pn533_target_type_b {
894         struct pn533_type_b_sens_res sensb_res;
895         u8 attrib_res_len;
896         u8 attrib_res[];
897 } __packed;
898
899 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
900                                                         int target_data_len)
901 {
902         if (target_data_len < sizeof(struct pn533_target_type_b))
903                 return false;
904
905         if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
906                 return false;
907
908         if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
909                                                 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
910                 return false;
911
912         return true;
913 }
914
915 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
916                                                         int tgt_data_len)
917 {
918         struct pn533_target_type_b *tgt_type_b;
919
920         tgt_type_b = (struct pn533_target_type_b *) tgt_data;
921
922         if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
923                 return -EPROTO;
924
925         nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
926
927         return 0;
928 }
929
930 struct pn533_poll_response {
931         u8 nbtg;
932         u8 tg;
933         u8 target_data[];
934 } __packed;
935
936 static int pn533_target_found(struct pn533 *dev,
937                         struct pn533_poll_response *resp, int resp_len)
938 {
939         int target_data_len;
940         struct nfc_target nfc_tgt;
941         int rc;
942
943         nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
944                                                         dev->poll_mod_curr);
945
946         if (resp->tg != 1)
947                 return -EPROTO;
948
949         memset(&nfc_tgt, 0, sizeof(struct nfc_target));
950
951         target_data_len = resp_len - sizeof(struct pn533_poll_response);
952
953         switch (dev->poll_mod_curr) {
954         case PN533_POLL_MOD_106KBPS_A:
955                 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
956                                                         target_data_len);
957                 break;
958         case PN533_POLL_MOD_212KBPS_FELICA:
959         case PN533_POLL_MOD_424KBPS_FELICA:
960                 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
961                                                         target_data_len);
962                 break;
963         case PN533_POLL_MOD_106KBPS_JEWEL:
964                 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
965                                                         target_data_len);
966                 break;
967         case PN533_POLL_MOD_847KBPS_B:
968                 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
969                                                         target_data_len);
970                 break;
971         default:
972                 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
973                                                                 " modulation");
974                 return -EPROTO;
975         }
976
977         if (rc)
978                 return rc;
979
980         if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
981                 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
982                                                 " have the desired protocol");
983                 return -EAGAIN;
984         }
985
986         nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
987                                         "0x%x", nfc_tgt.supported_protocols);
988
989         dev->tgt_available_prots = nfc_tgt.supported_protocols;
990
991         nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
992
993         return 0;
994 }
995
996 static void pn533_poll_reset_mod_list(struct pn533 *dev)
997 {
998         dev->poll_mod_count = 0;
999 }
1000
1001 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1002 {
1003         dev->poll_mod_active[dev->poll_mod_count] =
1004                 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1005         dev->poll_mod_count++;
1006 }
1007
1008 static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
1009 {
1010         pn533_poll_reset_mod_list(dev);
1011
1012         if (protocols & NFC_PROTO_MIFARE_MASK
1013                                         || protocols & NFC_PROTO_ISO14443_MASK
1014                                         || protocols & NFC_PROTO_NFC_DEP_MASK)
1015                 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1016
1017         if (protocols & NFC_PROTO_FELICA_MASK
1018                                         || protocols & NFC_PROTO_NFC_DEP_MASK) {
1019                 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1020                 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1021         }
1022
1023         if (protocols & NFC_PROTO_JEWEL_MASK)
1024                 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1025
1026         if (protocols & NFC_PROTO_ISO14443_MASK)
1027                 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1028 }
1029
1030 static void pn533_start_poll_frame(struct pn533_frame *frame,
1031                                         struct pn533_poll_modulations *mod)
1032 {
1033
1034         pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1035
1036         memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1037         frame->datalen += mod->len;
1038
1039         pn533_tx_frame_finish(frame);
1040 }
1041
1042 static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
1043                                                 u8 *params, int params_len)
1044 {
1045         struct pn533_poll_response *resp;
1046         struct pn533_poll_modulations *next_mod;
1047         int rc;
1048
1049         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1050
1051         if (params_len == -ENOENT) {
1052                 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
1053                                                                 " stopped");
1054                 goto stop_poll;
1055         }
1056
1057         if (params_len < 0) {
1058                 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
1059                                                                 params_len);
1060                 goto stop_poll;
1061         }
1062
1063         resp = (struct pn533_poll_response *) params;
1064         if (resp->nbtg) {
1065                 rc = pn533_target_found(dev, resp, params_len);
1066
1067                 /* We must stop the poll after a valid target found */
1068                 if (rc == 0)
1069                         goto stop_poll;
1070
1071                 if (rc != -EAGAIN)
1072                         nfc_dev_err(&dev->interface->dev, "The target found is"
1073                                         " not valid - continuing to poll");
1074         }
1075
1076         dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1077
1078         next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1079
1080         nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1081                                                         dev->poll_mod_curr);
1082
1083         pn533_start_poll_frame(dev->out_frame, next_mod);
1084
1085         /* Don't need to down the semaphore again */
1086         rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1087                                 dev->in_maxlen, pn533_start_poll_complete,
1088                                 NULL, GFP_ATOMIC);
1089
1090         if (rc == -EPERM) {
1091                 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1092                                         " because poll has been stopped");
1093                 goto stop_poll;
1094         }
1095
1096         if (rc) {
1097                 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1098                                                         " next modulation", rc);
1099                 goto stop_poll;
1100         }
1101
1102         /* Inform caller function to do not up the semaphore */
1103         return -EINPROGRESS;
1104
1105 stop_poll:
1106         pn533_poll_reset_mod_list(dev);
1107         dev->poll_protocols = 0;
1108         return 0;
1109 }
1110
1111 static int pn533_init_target_frame(struct pn533_frame *frame,
1112                                    u8 *gb, size_t gb_len)
1113 {
1114         struct pn533_cmd_init_target *cmd;
1115         size_t cmd_len;
1116
1117         cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1118         cmd = kzalloc(cmd_len, GFP_KERNEL);
1119         if (cmd == NULL)
1120                 return -ENOMEM;
1121
1122         pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1123
1124         /* DEP support only */
1125         cmd->mode |= PN533_INIT_TARGET_DEP;
1126         get_random_bytes(cmd->nfcid3, 10);
1127         cmd->gb_len = gb_len;
1128         memcpy(cmd->gb, gb, gb_len);
1129         /* Len Tk */
1130         cmd->gb[gb_len] = 0;
1131
1132         memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1133         frame->datalen += cmd_len;
1134
1135         pn533_tx_frame_finish(frame);
1136
1137         return 0;
1138 }
1139
1140 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1141 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1142 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1143                                       u8 *params, int params_len)
1144 {
1145         struct sk_buff *skb_resp = arg;
1146         struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1147
1148         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1149
1150         if (params_len < 0) {
1151                 nfc_dev_err(&dev->interface->dev,
1152                             "Error %d when starting as a target",
1153                             params_len);
1154
1155                 return params_len;
1156         }
1157
1158         if (params_len > 0 && params[0] != 0) {
1159                 nfc_tm_deactivated(dev->nfc_dev);
1160
1161                 kfree_skb(skb_resp);
1162                 return 0;
1163         }
1164
1165         skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1166         skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1167         skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1168
1169         return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1170 }
1171
1172 static void pn533_wq_tg_get_data(struct work_struct *work)
1173 {
1174         struct pn533 *dev = container_of(work, struct pn533, tg_work);
1175         struct pn533_frame *in_frame;
1176         struct sk_buff *skb_resp;
1177         size_t skb_resp_len;
1178
1179         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1180
1181         skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1182                 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1183                 PN533_FRAME_TAIL_SIZE;
1184
1185         skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1186         if (!skb_resp)
1187                 return;
1188
1189         in_frame = (struct pn533_frame *)skb_resp->data;
1190
1191         pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1192         pn533_tx_frame_finish(dev->out_frame);
1193
1194         pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1195                                    skb_resp_len,
1196                                    pn533_tm_get_data_complete,
1197                                    skb_resp, GFP_KERNEL);
1198
1199         return;
1200 }
1201
1202 #define ATR_REQ_GB_OFFSET 17
1203 static int pn533_init_target_complete(struct pn533 *dev, void *arg,
1204                                       u8 *params, int params_len)
1205 {
1206         struct pn533_cmd_init_target_response *resp;
1207         u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1208         size_t gb_len;
1209         int rc;
1210
1211         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1212
1213         if (params_len < 0) {
1214                 nfc_dev_err(&dev->interface->dev,
1215                             "Error %d when starting as a target",
1216                             params_len);
1217
1218                 return params_len;
1219         }
1220
1221         if (params_len < ATR_REQ_GB_OFFSET + 1)
1222                 return -EINVAL;
1223
1224         resp = (struct pn533_cmd_init_target_response *) params;
1225
1226         nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1227                     resp->mode, params_len);
1228
1229         frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1230         if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1231                 comm_mode = NFC_COMM_ACTIVE;
1232
1233         /* Again, only DEP */
1234         if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1235                 return -EOPNOTSUPP;
1236
1237         gb = resp->cmd + ATR_REQ_GB_OFFSET;
1238         gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1239
1240         rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1241                               comm_mode, gb, gb_len);
1242         if (rc < 0) {
1243                 nfc_dev_err(&dev->interface->dev,
1244                             "Error when signaling target activation");
1245                 return rc;
1246         }
1247
1248         queue_work(dev->wq, &dev->tg_work);
1249
1250         return 0;
1251 }
1252
1253 static int pn533_init_target(struct nfc_dev *nfc_dev, u32 protocols)
1254 {
1255         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1256         u8 *gb;
1257         size_t gb_len;
1258         int rc;
1259
1260         pn533_poll_reset_mod_list(dev);
1261
1262         gb = nfc_get_local_general_bytes(nfc_dev, &gb_len);
1263         if (gb == NULL)
1264                 return -ENOMEM;
1265
1266         rc = pn533_init_target_frame(dev->out_frame, gb, gb_len);
1267         if (rc < 0)
1268                 return rc;
1269
1270         rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1271                                         dev->in_maxlen,
1272                                         pn533_init_target_complete,
1273                                         NULL, GFP_KERNEL);
1274
1275         if (rc)
1276                 nfc_dev_err(&dev->interface->dev,
1277                             "Error %d when trying to initiate as a target", rc);
1278
1279         dev->poll_mod_count++;
1280
1281         return rc;
1282 }
1283
1284 static int pn533_start_im_poll(struct nfc_dev *nfc_dev, u32 protocols)
1285 {
1286         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1287         struct pn533_poll_modulations *start_mod;
1288         int rc;
1289
1290         if (dev->poll_mod_count) {
1291                 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1292                                                                 " active");
1293                 return -EBUSY;
1294         }
1295
1296         pn533_poll_create_mod_list(dev, protocols);
1297
1298         if (!dev->poll_mod_count) {
1299                 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1300                                                                 " specified");
1301                 rc = -EINVAL;
1302                 goto error;
1303         }
1304
1305         nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1306                                                         dev->poll_mod_count);
1307
1308         dev->poll_mod_curr = 0;
1309         start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1310
1311         pn533_start_poll_frame(dev->out_frame, start_mod);
1312
1313         rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1314                                 dev->in_maxlen, pn533_start_poll_complete,
1315                                 NULL, GFP_KERNEL);
1316
1317         if (rc) {
1318                 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1319                                                         " start poll", rc);
1320                 goto error;
1321         }
1322
1323         dev->poll_protocols = protocols;
1324
1325         return 0;
1326
1327 error:
1328         pn533_poll_reset_mod_list(dev);
1329         return rc;
1330 }
1331
1332 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1333                             u32 im_protocols, u32 tm_protocols)
1334 {
1335         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1336
1337         nfc_dev_dbg(&dev->interface->dev,
1338                     "%s: im protocols 0x%x tm protocols 0x%x",
1339                     __func__, im_protocols, tm_protocols);
1340
1341         if (dev->tgt_active_prot) {
1342                 nfc_dev_err(&dev->interface->dev,
1343                             "Cannot poll with a target already activated");
1344                 return -EBUSY;
1345         }
1346
1347         if (im_protocols)
1348                 return pn533_start_im_poll(nfc_dev, im_protocols);
1349
1350         if (tm_protocols)
1351                 return pn533_init_target(nfc_dev, tm_protocols);
1352
1353         return -EINVAL;
1354 }
1355
1356 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1357 {
1358         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1359
1360         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1361
1362         if (!dev->poll_mod_count) {
1363                 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1364                                                                 " running");
1365                 return;
1366         }
1367
1368         /* An ack will cancel the last issued command (poll) */
1369         pn533_send_ack(dev, GFP_KERNEL);
1370
1371         /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1372         usb_kill_urb(dev->in_urb);
1373 }
1374
1375 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1376 {
1377         struct pn533_cmd_activate_param param;
1378         struct pn533_cmd_activate_response *resp;
1379         u16 gt_len;
1380         int rc;
1381
1382         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1383
1384         pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1385
1386         param.tg = 1;
1387         param.next = 0;
1388         memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1389                                 sizeof(struct pn533_cmd_activate_param));
1390         dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1391
1392         pn533_tx_frame_finish(dev->out_frame);
1393
1394         rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1395                                                                 dev->in_maxlen);
1396         if (rc)
1397                 return rc;
1398
1399         resp = (struct pn533_cmd_activate_response *)
1400                                 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1401         rc = resp->status & PN533_CMD_RET_MASK;
1402         if (rc != PN533_CMD_RET_SUCCESS)
1403                 return -EIO;
1404
1405         /* ATR_RES general bytes are located at offset 16 */
1406         gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1407         rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1408
1409         return rc;
1410 }
1411
1412 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1413                                  struct nfc_target *target, u32 protocol)
1414 {
1415         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1416         int rc;
1417
1418         nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1419                                                                 protocol);
1420
1421         if (dev->poll_mod_count) {
1422                 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1423                                                                 " polling");
1424                 return -EBUSY;
1425         }
1426
1427         if (dev->tgt_active_prot) {
1428                 nfc_dev_err(&dev->interface->dev, "There is already an active"
1429                                                                 " target");
1430                 return -EBUSY;
1431         }
1432
1433         if (!dev->tgt_available_prots) {
1434                 nfc_dev_err(&dev->interface->dev, "There is no available target"
1435                                                                 " to activate");
1436                 return -EINVAL;
1437         }
1438
1439         if (!(dev->tgt_available_prots & (1 << protocol))) {
1440                 nfc_dev_err(&dev->interface->dev, "The target does not support"
1441                                         " the requested protocol %u", protocol);
1442                 return -EINVAL;
1443         }
1444
1445         if (protocol == NFC_PROTO_NFC_DEP) {
1446                 rc = pn533_activate_target_nfcdep(dev);
1447                 if (rc) {
1448                         nfc_dev_err(&dev->interface->dev, "Error %d when"
1449                                                 " activating target with"
1450                                                 " NFC_DEP protocol", rc);
1451                         return rc;
1452                 }
1453         }
1454
1455         dev->tgt_active_prot = protocol;
1456         dev->tgt_available_prots = 0;
1457
1458         return 0;
1459 }
1460
1461 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1462                                     struct nfc_target *target)
1463 {
1464         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1465         u8 tg;
1466         u8 status;
1467         int rc;
1468
1469         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1470
1471         if (!dev->tgt_active_prot) {
1472                 nfc_dev_err(&dev->interface->dev, "There is no active target");
1473                 return;
1474         }
1475
1476         dev->tgt_active_prot = 0;
1477
1478         skb_queue_purge(&dev->resp_q);
1479
1480         pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1481
1482         tg = 1;
1483         memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1484         dev->out_frame->datalen += sizeof(u8);
1485
1486         pn533_tx_frame_finish(dev->out_frame);
1487
1488         rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1489                                                                 dev->in_maxlen);
1490         if (rc) {
1491                 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1492                                                 " command to the controller");
1493                 return;
1494         }
1495
1496         status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1497         rc = status & PN533_CMD_RET_MASK;
1498         if (rc != PN533_CMD_RET_SUCCESS)
1499                 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1500                                                         " the target", rc);
1501
1502         return;
1503 }
1504
1505
1506 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1507                                                 u8 *params, int params_len)
1508 {
1509         struct pn533_cmd_jump_dep *cmd;
1510         struct pn533_cmd_jump_dep_response *resp;
1511         struct nfc_target nfc_target;
1512         u8 target_gt_len;
1513         int rc;
1514
1515         if (params_len == -ENOENT) {
1516                 nfc_dev_dbg(&dev->interface->dev, "");
1517                 return 0;
1518         }
1519
1520         if (params_len < 0) {
1521                 nfc_dev_err(&dev->interface->dev,
1522                                 "Error %d when bringing DEP link up",
1523                                                                 params_len);
1524                 return 0;
1525         }
1526
1527         if (dev->tgt_available_prots &&
1528             !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1529                 nfc_dev_err(&dev->interface->dev,
1530                         "The target does not support DEP");
1531                 return -EINVAL;
1532         }
1533
1534         resp = (struct pn533_cmd_jump_dep_response *) params;
1535         cmd = (struct pn533_cmd_jump_dep *) arg;
1536         rc = resp->status & PN533_CMD_RET_MASK;
1537         if (rc != PN533_CMD_RET_SUCCESS) {
1538                 nfc_dev_err(&dev->interface->dev,
1539                                 "Bringing DEP link up failed %d", rc);
1540                 return 0;
1541         }
1542
1543         if (!dev->tgt_available_prots) {
1544                 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1545
1546                 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1547                 nfc_target.nfcid1_len = 10;
1548                 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
1549                 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1550                 if (rc)
1551                         return 0;
1552
1553                 dev->tgt_available_prots = 0;
1554         }
1555
1556         dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1557
1558         /* ATR_RES general bytes are located at offset 17 */
1559         target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1560         rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1561                                                 resp->gt, target_gt_len);
1562         if (rc == 0)
1563                 rc = nfc_dep_link_is_up(dev->nfc_dev,
1564                                                 dev->nfc_dev->targets[0].idx,
1565                                                 !cmd->active, NFC_RF_INITIATOR);
1566
1567         return 0;
1568 }
1569
1570 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1571                              u8 comm_mode, u8* gb, size_t gb_len)
1572 {
1573         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1574         struct pn533_cmd_jump_dep *cmd;
1575         u8 cmd_len;
1576         int rc;
1577
1578         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1579
1580         if (dev->poll_mod_count) {
1581                 nfc_dev_err(&dev->interface->dev,
1582                                 "Cannot bring the DEP link up while polling");
1583                 return -EBUSY;
1584         }
1585
1586         if (dev->tgt_active_prot) {
1587                 nfc_dev_err(&dev->interface->dev,
1588                                 "There is already an active target");
1589                 return -EBUSY;
1590         }
1591
1592         cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
1593         cmd = kzalloc(cmd_len, GFP_KERNEL);
1594         if (cmd == NULL)
1595                 return -ENOMEM;
1596
1597         pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1598
1599         cmd->active = !comm_mode;
1600         cmd->baud = 0;
1601         if (gb != NULL && gb_len > 0) {
1602                 cmd->next = 4; /* We have some Gi */
1603                 memcpy(cmd->gt, gb, gb_len);
1604         } else {
1605                 cmd->next = 0;
1606         }
1607
1608         memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1609         dev->out_frame->datalen += cmd_len;
1610
1611         pn533_tx_frame_finish(dev->out_frame);
1612
1613         rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1614                                 dev->in_maxlen, pn533_in_dep_link_up_complete,
1615                                 cmd, GFP_KERNEL);
1616         if (rc)
1617                 goto out;
1618
1619
1620 out:
1621         kfree(cmd);
1622
1623         return rc;
1624 }
1625
1626 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1627 {
1628         pn533_deactivate_target(nfc_dev, 0);
1629
1630         return 0;
1631 }
1632
1633 static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1634 {
1635         int payload_len = skb->len;
1636         struct pn533_frame *out_frame;
1637         u8 tg;
1638
1639         nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1640                                                                 payload_len);
1641
1642         if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1643                 /* TODO: Implement support to multi-part data exchange */
1644                 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1645                                                 " max allowed: %d",
1646                                                 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1647                 return -ENOSYS;
1648         }
1649
1650         skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1651         out_frame = (struct pn533_frame *) skb->data;
1652
1653         pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1654
1655         tg = 1;
1656         memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1657         out_frame->datalen += sizeof(u8);
1658
1659         /* The data is already in the out_frame, just update the datalen */
1660         out_frame->datalen += payload_len;
1661
1662         pn533_tx_frame_finish(out_frame);
1663         skb_put(skb, PN533_FRAME_TAIL_SIZE);
1664
1665         return 0;
1666 }
1667
1668 struct pn533_data_exchange_arg {
1669         struct sk_buff *skb_resp;
1670         struct sk_buff *skb_out;
1671         data_exchange_cb_t cb;
1672         void *cb_context;
1673 };
1674
1675 static struct sk_buff *pn533_build_response(struct pn533 *dev)
1676 {
1677         struct sk_buff *skb, *tmp, *t;
1678         unsigned int skb_len = 0, tmp_len = 0;
1679
1680         nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1681
1682         if (skb_queue_empty(&dev->resp_q))
1683                 return NULL;
1684
1685         if (skb_queue_len(&dev->resp_q) == 1) {
1686                 skb = skb_dequeue(&dev->resp_q);
1687                 goto out;
1688         }
1689
1690         skb_queue_walk_safe(&dev->resp_q, tmp, t)
1691                 skb_len += tmp->len;
1692
1693         nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1694                     __func__, skb_len);
1695
1696         skb = alloc_skb(skb_len, GFP_KERNEL);
1697         if (skb == NULL)
1698                 goto out;
1699
1700         skb_put(skb, skb_len);
1701
1702         skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1703                 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1704                 tmp_len += tmp->len;
1705         }
1706
1707 out:
1708         skb_queue_purge(&dev->resp_q);
1709
1710         return skb;
1711 }
1712
1713 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1714                                                 u8 *params, int params_len)
1715 {
1716         struct pn533_data_exchange_arg *arg = _arg;
1717         struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
1718         struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1719         int err = 0;
1720         u8 status;
1721         u8 cmd_ret;
1722
1723         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1724
1725         dev_kfree_skb(arg->skb_out);
1726
1727         if (params_len < 0) { /* error */
1728                 err = params_len;
1729                 goto error;
1730         }
1731
1732         status = params[0];
1733
1734         cmd_ret = status & PN533_CMD_RET_MASK;
1735         if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1736                 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1737                                                 " exchanging data", cmd_ret);
1738                 err = -EIO;
1739                 goto error;
1740         }
1741
1742         skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1743         skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1744         skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1745         skb_queue_tail(&dev->resp_q, skb_resp);
1746
1747         if (status & PN533_CMD_MI_MASK) {
1748                 queue_work(dev->wq, &dev->mi_work);
1749                 return -EINPROGRESS;
1750         }
1751
1752         skb = pn533_build_response(dev);
1753         if (skb == NULL)
1754                 goto error;
1755
1756         arg->cb(arg->cb_context, skb, 0);
1757         kfree(arg);
1758         return 0;
1759
1760 error:
1761         skb_queue_purge(&dev->resp_q);
1762         dev_kfree_skb(skb_resp);
1763         arg->cb(arg->cb_context, NULL, err);
1764         kfree(arg);
1765         return 0;
1766 }
1767
1768 static int pn533_transceive(struct nfc_dev *nfc_dev,
1769                             struct nfc_target *target, struct sk_buff *skb,
1770                             data_exchange_cb_t cb, void *cb_context)
1771 {
1772         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1773         struct pn533_frame *out_frame, *in_frame;
1774         struct pn533_data_exchange_arg *arg;
1775         struct sk_buff *skb_resp;
1776         int skb_resp_len;
1777         int rc;
1778
1779         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1780
1781         if (!dev->tgt_active_prot) {
1782                 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1783                                                 " there is no active target");
1784                 rc = -EINVAL;
1785                 goto error;
1786         }
1787
1788         rc = pn533_data_exchange_tx_frame(dev, skb);
1789         if (rc)
1790                 goto error;
1791
1792         skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1793                         PN533_CMD_DATAEXCH_DATA_MAXLEN +
1794                         PN533_FRAME_TAIL_SIZE;
1795
1796         skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1797         if (!skb_resp) {
1798                 rc = -ENOMEM;
1799                 goto error;
1800         }
1801
1802         in_frame = (struct pn533_frame *) skb_resp->data;
1803         out_frame = (struct pn533_frame *) skb->data;
1804
1805         arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1806         if (!arg) {
1807                 rc = -ENOMEM;
1808                 goto free_skb_resp;
1809         }
1810
1811         arg->skb_resp = skb_resp;
1812         arg->skb_out = skb;
1813         arg->cb = cb;
1814         arg->cb_context = cb_context;
1815
1816         rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1817                                         pn533_data_exchange_complete, arg,
1818                                         GFP_KERNEL);
1819         if (rc) {
1820                 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1821                                                 " perform data_exchange", rc);
1822                 goto free_arg;
1823         }
1824
1825         return 0;
1826
1827 free_arg:
1828         kfree(arg);
1829 free_skb_resp:
1830         kfree_skb(skb_resp);
1831 error:
1832         kfree_skb(skb);
1833         return rc;
1834 }
1835
1836 static void pn533_wq_mi_recv(struct work_struct *work)
1837 {
1838         struct pn533 *dev = container_of(work, struct pn533, mi_work);
1839         struct sk_buff *skb_cmd;
1840         struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
1841         struct pn533_frame *out_frame, *in_frame;
1842         struct sk_buff *skb_resp;
1843         int skb_resp_len;
1844         int rc;
1845
1846         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1847
1848         /* This is a zero payload size skb */
1849         skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
1850                             GFP_KERNEL);
1851         if (skb_cmd == NULL)
1852                 goto error_cmd;
1853
1854         skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
1855
1856         rc = pn533_data_exchange_tx_frame(dev, skb_cmd);
1857         if (rc)
1858                 goto error_frame;
1859
1860         skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1861                         PN533_CMD_DATAEXCH_DATA_MAXLEN +
1862                         PN533_FRAME_TAIL_SIZE;
1863         skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
1864         if (!skb_resp) {
1865                 rc = -ENOMEM;
1866                 goto error_frame;
1867         }
1868
1869         in_frame = (struct pn533_frame *) skb_resp->data;
1870         out_frame = (struct pn533_frame *) skb_cmd->data;
1871
1872         arg->skb_resp = skb_resp;
1873         arg->skb_out = skb_cmd;
1874
1875         rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
1876                                           skb_resp_len,
1877                                           pn533_data_exchange_complete,
1878                                           dev->cmd_complete_arg, GFP_KERNEL);
1879         if (!rc)
1880                 return;
1881
1882         nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1883                                                 " perform data_exchange", rc);
1884
1885         kfree_skb(skb_resp);
1886
1887 error_frame:
1888         kfree_skb(skb_cmd);
1889
1890 error_cmd:
1891         pn533_send_ack(dev, GFP_KERNEL);
1892
1893         kfree(arg);
1894
1895         up(&dev->cmd_lock);
1896 }
1897
1898 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1899                                                                 u8 cfgdata_len)
1900 {
1901         int rc;
1902         u8 *params;
1903
1904         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1905
1906         pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1907
1908         params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1909         params[0] = cfgitem;
1910         memcpy(&params[1], cfgdata, cfgdata_len);
1911         dev->out_frame->datalen += (1 + cfgdata_len);
1912
1913         pn533_tx_frame_finish(dev->out_frame);
1914
1915         rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1916                                                                 dev->in_maxlen);
1917
1918         return rc;
1919 }
1920
1921 struct nfc_ops pn533_nfc_ops = {
1922         .dev_up = NULL,
1923         .dev_down = NULL,
1924         .dep_link_up = pn533_dep_link_up,
1925         .dep_link_down = pn533_dep_link_down,
1926         .start_poll = pn533_start_poll,
1927         .stop_poll = pn533_stop_poll,
1928         .activate_target = pn533_activate_target,
1929         .deactivate_target = pn533_deactivate_target,
1930         .im_transceive = pn533_transceive,
1931 };
1932
1933 static int pn533_probe(struct usb_interface *interface,
1934                         const struct usb_device_id *id)
1935 {
1936         struct pn533_fw_version *fw_ver;
1937         struct pn533 *dev;
1938         struct usb_host_interface *iface_desc;
1939         struct usb_endpoint_descriptor *endpoint;
1940         struct pn533_config_max_retries max_retries;
1941         int in_endpoint = 0;
1942         int out_endpoint = 0;
1943         int rc = -ENOMEM;
1944         int i;
1945         u32 protocols;
1946
1947         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1948         if (!dev)
1949                 return -ENOMEM;
1950
1951         dev->udev = usb_get_dev(interface_to_usbdev(interface));
1952         dev->interface = interface;
1953         sema_init(&dev->cmd_lock, 1);
1954
1955         iface_desc = interface->cur_altsetting;
1956         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1957                 endpoint = &iface_desc->endpoint[i].desc;
1958
1959                 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1960                         dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1961                         in_endpoint = endpoint->bEndpointAddress;
1962                 }
1963
1964                 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1965                         dev->out_maxlen =
1966                                 le16_to_cpu(endpoint->wMaxPacketSize);
1967                         out_endpoint = endpoint->bEndpointAddress;
1968                 }
1969         }
1970
1971         if (!in_endpoint || !out_endpoint) {
1972                 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1973                                                         " bulk-out endpoint");
1974                 rc = -ENODEV;
1975                 goto error;
1976         }
1977
1978         dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1979         dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1980         dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1981         dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1982
1983         if (!dev->in_frame || !dev->out_frame ||
1984                 !dev->in_urb || !dev->out_urb)
1985                 goto error;
1986
1987         usb_fill_bulk_urb(dev->in_urb, dev->udev,
1988                         usb_rcvbulkpipe(dev->udev, in_endpoint),
1989                         NULL, 0, NULL, dev);
1990         usb_fill_bulk_urb(dev->out_urb, dev->udev,
1991                         usb_sndbulkpipe(dev->udev, out_endpoint),
1992                         NULL, 0,
1993                         pn533_send_complete, dev);
1994
1995         INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
1996         INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
1997         INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
1998         dev->wq = alloc_workqueue("pn533",
1999                                   WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
2000                                   1);
2001         if (dev->wq == NULL)
2002                 goto error;
2003
2004         skb_queue_head_init(&dev->resp_q);
2005
2006         usb_set_intfdata(interface, dev);
2007
2008         pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2009         pn533_tx_frame_finish(dev->out_frame);
2010
2011         rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2012                                                                 dev->in_maxlen);
2013         if (rc)
2014                 goto destroy_wq;
2015
2016         fw_ver = (struct pn533_fw_version *)
2017                                 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2018         nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2019                                         " attached", fw_ver->ver, fw_ver->rev);
2020
2021         protocols = NFC_PROTO_JEWEL_MASK
2022                         | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
2023                         | NFC_PROTO_ISO14443_MASK
2024                         | NFC_PROTO_NFC_DEP_MASK;
2025
2026         dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2027                                            PN533_CMD_DATAEXCH_HEAD_LEN,
2028                                            PN533_FRAME_TAIL_SIZE);
2029         if (!dev->nfc_dev)
2030                 goto destroy_wq;
2031
2032         nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2033         nfc_set_drvdata(dev->nfc_dev, dev);
2034
2035         rc = nfc_register_device(dev->nfc_dev);
2036         if (rc)
2037                 goto free_nfc_dev;
2038
2039         max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2040         max_retries.mx_rty_psl = 2;
2041         max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2042
2043         rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2044                                 (u8 *) &max_retries, sizeof(max_retries));
2045
2046         if (rc) {
2047                 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
2048                                                                 " config");
2049                 goto free_nfc_dev;
2050         }
2051
2052         return 0;
2053
2054 free_nfc_dev:
2055         nfc_free_device(dev->nfc_dev);
2056 destroy_wq:
2057         destroy_workqueue(dev->wq);
2058 error:
2059         kfree(dev->in_frame);
2060         usb_free_urb(dev->in_urb);
2061         kfree(dev->out_frame);
2062         usb_free_urb(dev->out_urb);
2063         kfree(dev);
2064         return rc;
2065 }
2066
2067 static void pn533_disconnect(struct usb_interface *interface)
2068 {
2069         struct pn533 *dev;
2070
2071         dev = usb_get_intfdata(interface);
2072         usb_set_intfdata(interface, NULL);
2073
2074         nfc_unregister_device(dev->nfc_dev);
2075         nfc_free_device(dev->nfc_dev);
2076
2077         usb_kill_urb(dev->in_urb);
2078         usb_kill_urb(dev->out_urb);
2079
2080         destroy_workqueue(dev->wq);
2081
2082         skb_queue_purge(&dev->resp_q);
2083
2084         kfree(dev->in_frame);
2085         usb_free_urb(dev->in_urb);
2086         kfree(dev->out_frame);
2087         usb_free_urb(dev->out_urb);
2088         kfree(dev);
2089
2090         nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2091 }
2092
2093 static struct usb_driver pn533_driver = {
2094         .name =         "pn533",
2095         .probe =        pn533_probe,
2096         .disconnect =   pn533_disconnect,
2097         .id_table =     pn533_table,
2098 };
2099
2100 module_usb_driver(pn533_driver);
2101
2102 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2103                         " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2104 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2105 MODULE_VERSION(VERSION);
2106 MODULE_LICENSE("GPL");