]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/nfc/pn533.c
NFC: pn533: Remove pn533_send_cmd_frame_sync
[karo-tx-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 #define SONY_VENDOR_ID         0x054c
42 #define PASORI_PRODUCT_ID      0x02e1
43
44 #define PN533_DEVICE_STD    0x1
45 #define PN533_DEVICE_PASORI 0x2
46
47 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48                              NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49                              NFC_PROTO_NFC_DEP_MASK |\
50                              NFC_PROTO_ISO14443_B_MASK)
51
52 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53                                    NFC_PROTO_MIFARE_MASK | \
54                                    NFC_PROTO_FELICA_MASK | \
55                                    NFC_PROTO_ISO14443_MASK | \
56                                    NFC_PROTO_NFC_DEP_MASK)
57
58 static const struct usb_device_id pn533_table[] = {
59         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE,
60           .idVendor             = PN533_VENDOR_ID,
61           .idProduct            = PN533_PRODUCT_ID,
62           .driver_info          = PN533_DEVICE_STD,
63         },
64         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE,
65           .idVendor             = SCM_VENDOR_ID,
66           .idProduct            = SCL3711_PRODUCT_ID,
67           .driver_info          = PN533_DEVICE_STD,
68         },
69         { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE,
70           .idVendor             = SONY_VENDOR_ID,
71           .idProduct            = PASORI_PRODUCT_ID,
72           .driver_info          = PN533_DEVICE_PASORI,
73         },
74         { }
75 };
76 MODULE_DEVICE_TABLE(usb, pn533_table);
77
78 /* How much time we spend listening for initiators */
79 #define PN533_LISTEN_TIME 2
80
81 /* frame definitions */
82 #define PN533_NORMAL_FRAME_MAX_LEN 262  /* 6   (PREAMBLE, SOF, LEN, LCS, TFI)
83                                            254 (DATA)
84                                            2   (DCS, postamble) */
85 #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86                                         + 2) /* data[0] TFI, data[1] CC */
87 #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
88
89 /*
90  * Max extended frame payload len, excluding TFI and CC
91  * which are already in PN533_FRAME_HEADER_LEN.
92  */
93 #define PN533_FRAME_MAX_PAYLOAD_LEN 263
94
95 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
96                                 PN533_FRAME_TAIL_LEN)
97 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
100
101 /* start of frame */
102 #define PN533_SOF 0x00FF
103
104 /* frame identifier: in/out/error */
105 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106 #define PN533_DIR_OUT 0xD4
107 #define PN533_DIR_IN 0xD5
108
109 /* PN533 Commands */
110 #define PN533_FRAME_CMD(f) (f->data[1])
111 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
113
114 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115 #define PN533_CMD_RF_CONFIGURATION 0x32
116 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
117 #define PN533_CMD_IN_COMM_THRU     0x42
118 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119 #define PN533_CMD_IN_ATR 0x50
120 #define PN533_CMD_IN_RELEASE 0x52
121 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
122
123 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
124 #define PN533_CMD_TG_GET_DATA 0x86
125 #define PN533_CMD_TG_SET_DATA 0x8e
126 #define PN533_CMD_UNDEF 0xff
127
128 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
129
130 /* PN533 Return codes */
131 #define PN533_CMD_RET_MASK 0x3F
132 #define PN533_CMD_MI_MASK 0x40
133 #define PN533_CMD_RET_SUCCESS 0x00
134
135 struct pn533;
136
137 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138                                         u8 *params, int params_len);
139
140 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141                                         struct sk_buff *resp);
142
143 /* structs for pn533 commands */
144
145 /* PN533_CMD_GET_FIRMWARE_VERSION */
146 struct pn533_fw_version {
147         u8 ic;
148         u8 ver;
149         u8 rev;
150         u8 support;
151 };
152
153 /* PN533_CMD_RF_CONFIGURATION */
154 #define PN533_CFGITEM_TIMING 0x02
155 #define PN533_CFGITEM_MAX_RETRIES 0x05
156 #define PN533_CFGITEM_PASORI 0x82
157
158 #define PN533_CONFIG_TIMING_102 0xb
159 #define PN533_CONFIG_TIMING_204 0xc
160 #define PN533_CONFIG_TIMING_409 0xd
161 #define PN533_CONFIG_TIMING_819 0xe
162
163 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
165
166 struct pn533_config_max_retries {
167         u8 mx_rty_atr;
168         u8 mx_rty_psl;
169         u8 mx_rty_passive_act;
170 } __packed;
171
172 struct pn533_config_timing {
173         u8 rfu;
174         u8 atr_res_timeout;
175         u8 dep_timeout;
176 } __packed;
177
178 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
179
180 /* felica commands opcode */
181 #define PN533_FELICA_OPC_SENSF_REQ 0
182 #define PN533_FELICA_OPC_SENSF_RES 1
183 /* felica SENSF_REQ parameters */
184 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
188
189 /* type B initiator_data values */
190 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
193
194 union pn533_cmd_poll_initdata {
195         struct {
196                 u8 afi;
197                 u8 polling_method;
198         } __packed type_b;
199         struct {
200                 u8 opcode;
201                 __be16 sc;
202                 u8 rc;
203                 u8 tsn;
204         } __packed felica;
205 };
206
207 /* Poll modulations */
208 enum {
209         PN533_POLL_MOD_106KBPS_A,
210         PN533_POLL_MOD_212KBPS_FELICA,
211         PN533_POLL_MOD_424KBPS_FELICA,
212         PN533_POLL_MOD_106KBPS_JEWEL,
213         PN533_POLL_MOD_847KBPS_B,
214         PN533_LISTEN_MOD,
215
216         __PN533_POLL_MOD_AFTER_LAST,
217 };
218 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
219
220 struct pn533_poll_modulations {
221         struct {
222                 u8 maxtg;
223                 u8 brty;
224                 union pn533_cmd_poll_initdata initiator_data;
225         } __packed data;
226         u8 len;
227 };
228
229 const struct pn533_poll_modulations poll_mod[] = {
230         [PN533_POLL_MOD_106KBPS_A] = {
231                 .data = {
232                         .maxtg = 1,
233                         .brty = 0,
234                 },
235                 .len = 2,
236         },
237         [PN533_POLL_MOD_212KBPS_FELICA] = {
238                 .data = {
239                         .maxtg = 1,
240                         .brty = 1,
241                         .initiator_data.felica = {
242                                 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243                                 .sc = PN533_FELICA_SENSF_SC_ALL,
244                                 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
245                                 .tsn = 0,
246                         },
247                 },
248                 .len = 7,
249         },
250         [PN533_POLL_MOD_424KBPS_FELICA] = {
251                 .data = {
252                         .maxtg = 1,
253                         .brty = 2,
254                         .initiator_data.felica = {
255                                 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256                                 .sc = PN533_FELICA_SENSF_SC_ALL,
257                                 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
258                                 .tsn = 0,
259                         },
260                  },
261                 .len = 7,
262         },
263         [PN533_POLL_MOD_106KBPS_JEWEL] = {
264                 .data = {
265                         .maxtg = 1,
266                         .brty = 4,
267                 },
268                 .len = 2,
269         },
270         [PN533_POLL_MOD_847KBPS_B] = {
271                 .data = {
272                         .maxtg = 1,
273                         .brty = 8,
274                         .initiator_data.type_b = {
275                                 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
276                                 .polling_method =
277                                         PN533_TYPE_B_POLL_METHOD_TIMESLOT,
278                         },
279                 },
280                 .len = 3,
281         },
282         [PN533_LISTEN_MOD] = {
283                 .len = 0,
284         },
285 };
286
287 /* PN533_CMD_IN_ATR */
288
289 struct pn533_cmd_activate_response {
290         u8 status;
291         u8 nfcid3t[10];
292         u8 didt;
293         u8 bst;
294         u8 brt;
295         u8 to;
296         u8 ppt;
297         /* optional */
298         u8 gt[];
299 } __packed;
300
301 /* PN533_CMD_IN_JUMP_FOR_DEP */
302 struct pn533_cmd_jump_dep {
303         u8 active;
304         u8 baud;
305         u8 next;
306         u8 data[];
307 } __packed;
308
309 struct pn533_cmd_jump_dep_response {
310         u8 status;
311         u8 tg;
312         u8 nfcid3t[10];
313         u8 didt;
314         u8 bst;
315         u8 brt;
316         u8 to;
317         u8 ppt;
318         /* optional */
319         u8 gt[];
320 } __packed;
321
322
323 /* PN533_TG_INIT_AS_TARGET */
324 #define PN533_INIT_TARGET_PASSIVE 0x1
325 #define PN533_INIT_TARGET_DEP 0x2
326
327 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
328 #define PN533_INIT_TARGET_RESP_ACTIVE     0x1
329 #define PN533_INIT_TARGET_RESP_DEP        0x4
330
331 struct pn533_cmd_init_target {
332         u8 mode;
333         u8 mifare[6];
334         u8 felica[18];
335         u8 nfcid3[10];
336         u8 gb_len;
337         u8 gb[];
338 } __packed;
339
340 struct pn533_cmd_init_target_response {
341         u8 mode;
342         u8 cmd[];
343 } __packed;
344
345 struct pn533 {
346         struct usb_device *udev;
347         struct usb_interface *interface;
348         struct nfc_dev *nfc_dev;
349
350         struct urb *out_urb;
351         struct pn533_frame *out_frame;
352
353         struct urb *in_urb;
354         struct pn533_frame *in_frame;
355
356         struct sk_buff_head resp_q;
357
358         struct workqueue_struct *wq;
359         struct work_struct cmd_work;
360         struct work_struct cmd_complete_work;
361         struct work_struct poll_work;
362         struct work_struct mi_work;
363         struct work_struct tg_work;
364         struct timer_list listen_timer;
365         struct pn533_frame *wq_in_frame;
366         int wq_in_error;
367         int cancel_listen;
368
369         pn533_cmd_complete_t cmd_complete;
370         void *cmd_complete_arg;
371         struct mutex cmd_lock;
372         u8 cmd;
373
374         struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
375         u8 poll_mod_count;
376         u8 poll_mod_curr;
377         u32 poll_protocols;
378         u32 listen_protocols;
379
380         u8 *gb;
381         size_t gb_len;
382
383         u8 tgt_available_prots;
384         u8 tgt_active_prot;
385         u8 tgt_mode;
386
387         u32 device_type;
388
389         struct list_head cmd_queue;
390         u8 cmd_pending;
391 };
392
393 struct pn533_cmd {
394         struct list_head queue;
395         struct pn533_frame *out_frame;
396         struct pn533_frame *in_frame;
397         int in_frame_len;
398         u8 cmd_code;
399         struct sk_buff *req;
400         struct sk_buff *resp;
401         pn533_cmd_complete_t cmd_complete;
402         void *arg;
403 };
404
405 struct pn533_frame {
406         u8 preamble;
407         __be16 start_frame;
408         u8 datalen;
409         u8 datalen_checksum;
410         u8 data[];
411 } __packed;
412
413 /* The rule: value + checksum = 0 */
414 static inline u8 pn533_checksum(u8 value)
415 {
416         return ~value + 1;
417 }
418
419 /* The rule: sum(data elements) + checksum = 0 */
420 static u8 pn533_data_checksum(u8 *data, int datalen)
421 {
422         u8 sum = 0;
423         int i;
424
425         for (i = 0; i < datalen; i++)
426                 sum += data[i];
427
428         return pn533_checksum(sum);
429 }
430
431 /**
432  * pn533_tx_frame_ack - create a ack frame
433  * @frame:      The frame to be set as ack
434  *
435  * Ack is different type of standard frame. As a standard frame, it has
436  * preamble and start_frame. However the checksum of this frame must fail,
437  * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
438  * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
439  * After datalen_checksum field, the postamble is placed.
440  */
441 static void pn533_tx_frame_ack(struct pn533_frame *frame)
442 {
443         frame->preamble = 0;
444         frame->start_frame = cpu_to_be16(PN533_SOF);
445         frame->datalen = 0;
446         frame->datalen_checksum = 0xFF;
447         /* data[0] is used as postamble */
448         frame->data[0] = 0;
449 }
450
451 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
452 {
453         frame->preamble = 0;
454         frame->start_frame = cpu_to_be16(PN533_SOF);
455         PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
456         PN533_FRAME_CMD(frame) = cmd;
457         frame->datalen = 2;
458 }
459
460 static void pn533_tx_frame_finish(struct pn533_frame *frame)
461 {
462         frame->datalen_checksum = pn533_checksum(frame->datalen);
463
464         PN533_FRAME_CHECKSUM(frame) =
465                 pn533_data_checksum(frame->data, frame->datalen);
466
467         PN533_FRAME_POSTAMBLE(frame) = 0;
468 }
469
470 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
471 {
472         u8 checksum;
473
474         if (frame->start_frame != cpu_to_be16(PN533_SOF))
475                 return false;
476
477         checksum = pn533_checksum(frame->datalen);
478         if (checksum != frame->datalen_checksum)
479                 return false;
480
481         checksum = pn533_data_checksum(frame->data, frame->datalen);
482         if (checksum != PN533_FRAME_CHECKSUM(frame))
483                 return false;
484
485         return true;
486 }
487
488 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
489 {
490         if (frame->start_frame != cpu_to_be16(PN533_SOF))
491                 return false;
492
493         if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
494                 return false;
495
496         return true;
497 }
498
499 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
500 {
501         return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
502 }
503
504
505 static void pn533_wq_cmd_complete(struct work_struct *work)
506 {
507         struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
508         struct pn533_frame *in_frame;
509         int rc;
510
511         in_frame = dev->wq_in_frame;
512
513         if (dev->wq_in_error)
514                 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
515                                                         dev->wq_in_error);
516         else
517                 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
518                                         PN533_FRAME_CMD_PARAMS_PTR(in_frame),
519                                         PN533_FRAME_CMD_PARAMS_LEN(in_frame));
520
521         if (rc != -EINPROGRESS)
522                 queue_work(dev->wq, &dev->cmd_work);
523 }
524
525 static void pn533_recv_response(struct urb *urb)
526 {
527         struct pn533 *dev = urb->context;
528         struct pn533_frame *in_frame;
529
530         dev->wq_in_frame = NULL;
531
532         switch (urb->status) {
533         case 0:
534                 /* success */
535                 break;
536         case -ECONNRESET:
537         case -ENOENT:
538         case -ESHUTDOWN:
539                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
540                                                 " status: %d", urb->status);
541                 dev->wq_in_error = urb->status;
542                 goto sched_wq;
543         default:
544                 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
545                                                         " %d", urb->status);
546                 dev->wq_in_error = urb->status;
547                 goto sched_wq;
548         }
549
550         in_frame = dev->in_urb->transfer_buffer;
551
552         if (!pn533_rx_frame_is_valid(in_frame)) {
553                 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
554                 dev->wq_in_error = -EIO;
555                 goto sched_wq;
556         }
557
558         if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
559                 nfc_dev_err(&dev->interface->dev, "The received frame is not "
560                                                 "response to the last command");
561                 dev->wq_in_error = -EIO;
562                 goto sched_wq;
563         }
564
565         nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
566         dev->wq_in_error = 0;
567         dev->wq_in_frame = in_frame;
568
569 sched_wq:
570         queue_work(dev->wq, &dev->cmd_complete_work);
571 }
572
573 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
574 {
575         dev->in_urb->complete = pn533_recv_response;
576
577         return usb_submit_urb(dev->in_urb, flags);
578 }
579
580 static void pn533_recv_ack(struct urb *urb)
581 {
582         struct pn533 *dev = urb->context;
583         struct pn533_frame *in_frame;
584         int rc;
585
586         switch (urb->status) {
587         case 0:
588                 /* success */
589                 break;
590         case -ECONNRESET:
591         case -ENOENT:
592         case -ESHUTDOWN:
593                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
594                                                 " status: %d", urb->status);
595                 dev->wq_in_error = urb->status;
596                 goto sched_wq;
597         default:
598                 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
599                                                         " %d", urb->status);
600                 dev->wq_in_error = urb->status;
601                 goto sched_wq;
602         }
603
604         in_frame = dev->in_urb->transfer_buffer;
605
606         if (!pn533_rx_frame_is_ack(in_frame)) {
607                 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
608                 dev->wq_in_error = -EIO;
609                 goto sched_wq;
610         }
611
612         nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
613
614         rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
615         if (rc) {
616                 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
617                                                         " result %d", rc);
618                 dev->wq_in_error = rc;
619                 goto sched_wq;
620         }
621
622         return;
623
624 sched_wq:
625         dev->wq_in_frame = NULL;
626         queue_work(dev->wq, &dev->cmd_complete_work);
627 }
628
629 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
630 {
631         dev->in_urb->complete = pn533_recv_ack;
632
633         return usb_submit_urb(dev->in_urb, flags);
634 }
635
636 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
637 {
638         int rc;
639
640         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
641
642         pn533_tx_frame_ack(dev->out_frame);
643
644         dev->out_urb->transfer_buffer = dev->out_frame;
645         dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
646         rc = usb_submit_urb(dev->out_urb, flags);
647
648         return rc;
649 }
650
651 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
652                                         struct pn533_frame *out_frame,
653                                         struct pn533_frame *in_frame,
654                                         int in_frame_len,
655                                         pn533_cmd_complete_t cmd_complete,
656                                         void *arg)
657 {
658         int rc;
659
660         nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
661                                                 PN533_FRAME_CMD(out_frame));
662
663         dev->cmd = PN533_FRAME_CMD(out_frame);
664         dev->cmd_complete = cmd_complete;
665         dev->cmd_complete_arg = arg;
666
667         dev->out_urb->transfer_buffer = out_frame;
668         dev->out_urb->transfer_buffer_length =
669                                 PN533_FRAME_SIZE(out_frame);
670
671         dev->in_urb->transfer_buffer = in_frame;
672         dev->in_urb->transfer_buffer_length = in_frame_len;
673
674         rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
675         if (rc)
676                 return rc;
677
678         rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
679         if (rc)
680                 goto error;
681
682         return 0;
683
684 error:
685         usb_unlink_urb(dev->out_urb);
686         return rc;
687 }
688
689 static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
690 {
691         struct pn533_frame *frame;
692         /* payload is already there, just update datalen */
693         int payload_len = skb->len;
694
695         skb_push(skb, PN533_FRAME_HEADER_LEN);
696         skb_put(skb, PN533_FRAME_TAIL_LEN);
697
698         frame = (struct pn533_frame *)skb->data;
699
700         pn533_tx_frame_init(frame, cmd_code);
701         frame->datalen += payload_len;
702         pn533_tx_frame_finish(frame);
703 }
704
705 struct pn533_send_async_complete_arg {
706         pn533_send_async_complete_t  complete_cb;
707         void *complete_cb_context;
708         struct sk_buff *resp;
709         struct sk_buff *req;
710 };
711
712 static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
713                                      int params_len)
714 {
715         struct pn533_send_async_complete_arg *arg = _arg;
716
717         struct sk_buff *req = arg->req;
718         struct sk_buff *resp = arg->resp;
719
720         struct pn533_frame *frame = (struct pn533_frame *)resp->data;
721         int rc;
722
723         dev_kfree_skb(req);
724
725         if (params_len < 0) {
726                 nfc_dev_err(&dev->interface->dev,
727                             "Error %d when starting as a target",
728                             params_len);
729
730                 arg->complete_cb(dev, arg->complete_cb_context,
731                                  ERR_PTR(params_len));
732                 rc = params_len;
733                 dev_kfree_skb(resp);
734                 goto out;
735         }
736
737         skb_put(resp, PN533_FRAME_SIZE(frame));
738         skb_pull(resp, PN533_FRAME_HEADER_LEN);
739         skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
740
741         rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
742
743 out:
744         kfree(arg);
745         return rc;
746 }
747
748 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
749                               struct sk_buff *req, struct sk_buff *resp,
750                               int resp_len,
751                               pn533_send_async_complete_t complete_cb,
752                               void *complete_cb_context)
753 {
754         struct pn533_cmd *cmd;
755         struct pn533_send_async_complete_arg *arg;
756         int rc = 0;
757
758         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
759
760         arg = kzalloc(sizeof(arg), GFP_KERNEL);
761         if (!arg)
762                 return -ENOMEM;
763
764         arg->complete_cb = complete_cb;
765         arg->complete_cb_context = complete_cb_context;
766         arg->resp = resp;
767         arg->req = req;
768
769         pn533_build_cmd_frame(cmd_code, req);
770
771         mutex_lock(&dev->cmd_lock);
772
773         if (!dev->cmd_pending) {
774                 rc = __pn533_send_cmd_frame_async(dev,
775                                                   (struct pn533_frame *)req->data,
776                                                   (struct pn533_frame *)resp->data,
777                                                   resp_len, pn533_send_async_complete,
778                                                   arg);
779                 if (rc)
780                         goto error;
781
782                 dev->cmd_pending = 1;
783                 goto unlock;
784         }
785
786         nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
787
788         cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
789         if (!cmd) {
790                 rc = -ENOMEM;
791                 goto error;
792         }
793
794         INIT_LIST_HEAD(&cmd->queue);
795         cmd->cmd_code = cmd_code;
796         cmd->req = req;
797         cmd->resp = resp;
798         cmd->arg = arg;
799
800         list_add_tail(&cmd->queue, &dev->cmd_queue);
801
802         goto unlock;
803
804 error:
805         kfree(arg);
806 unlock:
807         mutex_unlock(&dev->cmd_lock);
808         return rc;
809 }
810
811 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
812                                  struct sk_buff *req,
813                                  pn533_send_async_complete_t complete_cb,
814                                  void *complete_cb_context)
815 {
816         struct sk_buff *resp;
817         int rc;
818         int  resp_len = PN533_FRAME_HEADER_LEN +
819                         PN533_FRAME_MAX_PAYLOAD_LEN +
820                         PN533_FRAME_TAIL_LEN;
821
822         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
823
824         resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
825         if (!resp)
826                 return -ENOMEM;
827
828         rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
829                                 complete_cb_context);
830         if (rc)
831                 dev_kfree_skb(resp);
832
833         return rc;
834 }
835
836 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
837                                 struct sk_buff *req,
838                                 pn533_send_async_complete_t complete_cb,
839                                 void *complete_cb_context)
840 {
841         struct sk_buff *resp;
842         int rc;
843
844         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
845
846         resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
847         if (!resp)
848                 return -ENOMEM;
849
850         rc = __pn533_send_async(dev, cmd_code, req, resp,
851                                 PN533_NORMAL_FRAME_MAX_LEN,
852                                 complete_cb, complete_cb_context);
853         if (rc)
854                 dev_kfree_skb(resp);
855
856         return rc;
857 }
858
859 static void pn533_wq_cmd(struct work_struct *work)
860 {
861         struct pn533 *dev = container_of(work, struct pn533, cmd_work);
862         struct pn533_cmd *cmd;
863
864         mutex_lock(&dev->cmd_lock);
865
866         if (list_empty(&dev->cmd_queue)) {
867                 dev->cmd_pending = 0;
868                 mutex_unlock(&dev->cmd_lock);
869                 return;
870         }
871
872         cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
873
874         list_del(&cmd->queue);
875
876         mutex_unlock(&dev->cmd_lock);
877
878         if (cmd->cmd_code != PN533_CMD_UNDEF)
879                 __pn533_send_cmd_frame_async(dev,
880                                              (struct pn533_frame *)cmd->req->data,
881                                              (struct pn533_frame *)cmd->resp->data,
882                                              PN533_NORMAL_FRAME_MAX_LEN,
883                                              pn533_send_async_complete,
884                                              cmd->arg);
885         else
886                 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
887                                              cmd->in_frame_len,
888                                              cmd->cmd_complete, cmd->arg);
889
890         kfree(cmd);
891 }
892
893 static int pn533_send_cmd_frame_async(struct pn533 *dev,
894                                         struct pn533_frame *out_frame,
895                                         struct pn533_frame *in_frame,
896                                         int in_frame_len,
897                                         pn533_cmd_complete_t cmd_complete,
898                                         void *arg)
899 {
900         struct pn533_cmd *cmd;
901         int rc = 0;
902
903         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
904
905         mutex_lock(&dev->cmd_lock);
906
907         if (!dev->cmd_pending) {
908                 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
909                                                   in_frame_len, cmd_complete,
910                                                   arg);
911                 if (!rc)
912                         dev->cmd_pending = 1;
913
914                 goto unlock;
915         }
916
917         nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
918
919         cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
920         if (!cmd) {
921                 rc = -ENOMEM;
922                 goto unlock;
923         }
924
925         INIT_LIST_HEAD(&cmd->queue);
926         cmd->out_frame = out_frame;
927         cmd->in_frame = in_frame;
928         cmd->in_frame_len = in_frame_len;
929         cmd->cmd_code = PN533_CMD_UNDEF;
930         cmd->cmd_complete = cmd_complete;
931         cmd->arg = arg;
932
933         list_add_tail(&cmd->queue, &dev->cmd_queue);
934
935 unlock:
936         mutex_unlock(&dev->cmd_lock);
937
938         return rc;
939 }
940
941 struct pn533_sync_cmd_response {
942         struct sk_buff *resp;
943         struct completion done;
944 };
945
946 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
947                                     struct sk_buff *resp)
948 {
949         struct pn533_sync_cmd_response *arg = _arg;
950
951         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
952
953         arg->resp = resp;
954         complete(&arg->done);
955
956         return 0;
957 }
958
959 /*  pn533_send_cmd_sync
960  *
961  *  Please note the req parameter is freed inside the function to
962  *  limit a number of return value interpretations by the caller.
963  *
964  *  1. negative in case of error during TX path -> req should be freed
965  *
966  *  2. negative in case of error during RX path -> req should not be freed
967  *     as it's been already freed at the begining of RX path by
968  *     async_complete_cb.
969  *
970  *  3. valid pointer in case of succesfult RX path
971  *
972  *  A caller has to check a return value with IS_ERR macro. If the test pass,
973  *  the returned pointer is valid.
974  *
975  * */
976 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
977                                                struct sk_buff *req)
978 {
979         int rc;
980         struct pn533_sync_cmd_response arg;
981
982         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
983
984         init_completion(&arg.done);
985
986         rc = pn533_send_cmd_async(dev, cmd_code, req,
987                                   pn533_send_sync_complete, &arg);
988         if (rc) {
989                 dev_kfree_skb(req);
990                 return ERR_PTR(rc);
991         }
992
993         wait_for_completion(&arg.done);
994
995         return arg.resp;
996 }
997
998 static void pn533_send_complete(struct urb *urb)
999 {
1000         struct pn533 *dev = urb->context;
1001
1002         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1003
1004         switch (urb->status) {
1005         case 0:
1006                 /* success */
1007                 break;
1008         case -ECONNRESET:
1009         case -ENOENT:
1010         case -ESHUTDOWN:
1011                 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1012                                                 " status: %d", urb->status);
1013                 break;
1014         default:
1015                 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1016                                                         " %d", urb->status);
1017         }
1018 }
1019
1020 static struct sk_buff *pn533_alloc_skb(unsigned int size)
1021 {
1022         struct sk_buff *skb;
1023
1024         skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1025                         size +
1026                         PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1027
1028         if (skb)
1029                 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1030
1031         return skb;
1032 }
1033
1034 struct pn533_target_type_a {
1035         __be16 sens_res;
1036         u8 sel_res;
1037         u8 nfcid_len;
1038         u8 nfcid_data[];
1039 } __packed;
1040
1041
1042 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1043 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1044 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1045
1046 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1047 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1048
1049 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1050 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1051
1052 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1053 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1054 #define PN533_TYPE_A_SEL_PROT_DEP 2
1055 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1056
1057 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1058                                                         int target_data_len)
1059 {
1060         u8 ssd;
1061         u8 platconf;
1062
1063         if (target_data_len < sizeof(struct pn533_target_type_a))
1064                 return false;
1065
1066         /* The lenght check of nfcid[] and ats[] are not being performed because
1067            the values are not being used */
1068
1069         /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1070         ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1071         platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1072
1073         if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1074                         platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1075                         (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1076                         platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1077                 return false;
1078
1079         /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1080         if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1081                 return false;
1082
1083         return true;
1084 }
1085
1086 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1087                                                         int tgt_data_len)
1088 {
1089         struct pn533_target_type_a *tgt_type_a;
1090
1091         tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1092
1093         if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1094                 return -EPROTO;
1095
1096         switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1097         case PN533_TYPE_A_SEL_PROT_MIFARE:
1098                 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1099                 break;
1100         case PN533_TYPE_A_SEL_PROT_ISO14443:
1101                 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1102                 break;
1103         case PN533_TYPE_A_SEL_PROT_DEP:
1104                 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1105                 break;
1106         case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1107                 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1108                                                         NFC_PROTO_NFC_DEP_MASK;
1109                 break;
1110         }
1111
1112         nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1113         nfc_tgt->sel_res = tgt_type_a->sel_res;
1114         nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1115         memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1116
1117         return 0;
1118 }
1119
1120 struct pn533_target_felica {
1121         u8 pol_res;
1122         u8 opcode;
1123         u8 nfcid2[8];
1124         u8 pad[8];
1125         /* optional */
1126         u8 syst_code[];
1127 } __packed;
1128
1129 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1130 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1131
1132 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1133                                                         int target_data_len)
1134 {
1135         if (target_data_len < sizeof(struct pn533_target_felica))
1136                 return false;
1137
1138         if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1139                 return false;
1140
1141         return true;
1142 }
1143
1144 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1145                                                         int tgt_data_len)
1146 {
1147         struct pn533_target_felica *tgt_felica;
1148
1149         tgt_felica = (struct pn533_target_felica *) tgt_data;
1150
1151         if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1152                 return -EPROTO;
1153
1154         if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1155                                         tgt_felica->nfcid2[1] ==
1156                                         PN533_FELICA_SENSF_NFCID2_DEP_B2)
1157                 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1158         else
1159                 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1160
1161         memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1162         nfc_tgt->sensf_res_len = 9;
1163
1164         return 0;
1165 }
1166
1167 struct pn533_target_jewel {
1168         __be16 sens_res;
1169         u8 jewelid[4];
1170 } __packed;
1171
1172 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1173                                                         int target_data_len)
1174 {
1175         u8 ssd;
1176         u8 platconf;
1177
1178         if (target_data_len < sizeof(struct pn533_target_jewel))
1179                 return false;
1180
1181         /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1182         ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1183         platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1184
1185         if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1186                         platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1187                         (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1188                         platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1189                 return false;
1190
1191         return true;
1192 }
1193
1194 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1195                                                         int tgt_data_len)
1196 {
1197         struct pn533_target_jewel *tgt_jewel;
1198
1199         tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1200
1201         if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1202                 return -EPROTO;
1203
1204         nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1205         nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1206         nfc_tgt->nfcid1_len = 4;
1207         memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1208
1209         return 0;
1210 }
1211
1212 struct pn533_type_b_prot_info {
1213         u8 bitrate;
1214         u8 fsci_type;
1215         u8 fwi_adc_fo;
1216 } __packed;
1217
1218 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1219 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1220 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1221
1222 struct pn533_type_b_sens_res {
1223         u8 opcode;
1224         u8 nfcid[4];
1225         u8 appdata[4];
1226         struct pn533_type_b_prot_info prot_info;
1227 } __packed;
1228
1229 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1230
1231 struct pn533_target_type_b {
1232         struct pn533_type_b_sens_res sensb_res;
1233         u8 attrib_res_len;
1234         u8 attrib_res[];
1235 } __packed;
1236
1237 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1238                                                         int target_data_len)
1239 {
1240         if (target_data_len < sizeof(struct pn533_target_type_b))
1241                 return false;
1242
1243         if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1244                 return false;
1245
1246         if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1247                                                 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1248                 return false;
1249
1250         return true;
1251 }
1252
1253 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1254                                                         int tgt_data_len)
1255 {
1256         struct pn533_target_type_b *tgt_type_b;
1257
1258         tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1259
1260         if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1261                 return -EPROTO;
1262
1263         nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1264
1265         return 0;
1266 }
1267
1268 struct pn533_poll_response {
1269         u8 nbtg;
1270         u8 tg;
1271         u8 target_data[];
1272 } __packed;
1273
1274 static int pn533_target_found(struct pn533 *dev,
1275                         struct pn533_poll_response *resp, int resp_len)
1276 {
1277         int target_data_len;
1278         struct nfc_target nfc_tgt;
1279         int rc;
1280
1281         nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1282                                                         dev->poll_mod_curr);
1283
1284         if (resp->tg != 1)
1285                 return -EPROTO;
1286
1287         memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1288
1289         target_data_len = resp_len - sizeof(struct pn533_poll_response);
1290
1291         switch (dev->poll_mod_curr) {
1292         case PN533_POLL_MOD_106KBPS_A:
1293                 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1294                                                         target_data_len);
1295                 break;
1296         case PN533_POLL_MOD_212KBPS_FELICA:
1297         case PN533_POLL_MOD_424KBPS_FELICA:
1298                 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1299                                                         target_data_len);
1300                 break;
1301         case PN533_POLL_MOD_106KBPS_JEWEL:
1302                 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1303                                                         target_data_len);
1304                 break;
1305         case PN533_POLL_MOD_847KBPS_B:
1306                 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1307                                                         target_data_len);
1308                 break;
1309         default:
1310                 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1311                                                                 " modulation");
1312                 return -EPROTO;
1313         }
1314
1315         if (rc)
1316                 return rc;
1317
1318         if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1319                 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1320                                                 " have the desired protocol");
1321                 return -EAGAIN;
1322         }
1323
1324         nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1325                                         "0x%x", nfc_tgt.supported_protocols);
1326
1327         dev->tgt_available_prots = nfc_tgt.supported_protocols;
1328
1329         nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1330
1331         return 0;
1332 }
1333
1334 static inline void pn533_poll_next_mod(struct pn533 *dev)
1335 {
1336         dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1337 }
1338
1339 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1340 {
1341         dev->poll_mod_count = 0;
1342 }
1343
1344 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1345 {
1346         dev->poll_mod_active[dev->poll_mod_count] =
1347                 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1348         dev->poll_mod_count++;
1349 }
1350
1351 static void pn533_poll_create_mod_list(struct pn533 *dev,
1352                                        u32 im_protocols, u32 tm_protocols)
1353 {
1354         pn533_poll_reset_mod_list(dev);
1355
1356         if (im_protocols & NFC_PROTO_MIFARE_MASK
1357             || im_protocols & NFC_PROTO_ISO14443_MASK
1358             || im_protocols & NFC_PROTO_NFC_DEP_MASK)
1359                 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1360
1361         if (im_protocols & NFC_PROTO_FELICA_MASK
1362             || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1363                 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1364                 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1365         }
1366
1367         if (im_protocols & NFC_PROTO_JEWEL_MASK)
1368                 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1369
1370         if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1371                 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1372
1373         if (tm_protocols)
1374                 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1375 }
1376
1377 static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
1378 {
1379         struct pn533_poll_response *resp;
1380         int rc;
1381
1382         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1383
1384         resp = (struct pn533_poll_response *) params;
1385         if (resp->nbtg) {
1386                 rc = pn533_target_found(dev, resp, params_len);
1387
1388                 /* We must stop the poll after a valid target found */
1389                 if (rc == 0) {
1390                         pn533_poll_reset_mod_list(dev);
1391                         return 0;
1392                 }
1393         }
1394
1395         return -EAGAIN;
1396 }
1397
1398 static int pn533_init_target_frame(struct pn533_frame *frame,
1399                                    u8 *gb, size_t gb_len)
1400 {
1401         struct pn533_cmd_init_target *cmd;
1402         size_t cmd_len;
1403         u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1404                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1405                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1406                                 0xff, 0xff}; /* System code */
1407         u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1408                                0x0, 0x0, 0x0,
1409                                0x40}; /* SEL_RES for DEP */
1410
1411         cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1412         cmd = kzalloc(cmd_len, GFP_KERNEL);
1413         if (cmd == NULL)
1414                 return -ENOMEM;
1415
1416         pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1417
1418         /* DEP support only */
1419         cmd->mode |= PN533_INIT_TARGET_DEP;
1420
1421         /* Felica params */
1422         memcpy(cmd->felica, felica_params, 18);
1423         get_random_bytes(cmd->felica + 2, 6);
1424
1425         /* NFCID3 */
1426         memset(cmd->nfcid3, 0, 10);
1427         memcpy(cmd->nfcid3, cmd->felica, 8);
1428
1429         /* MIFARE params */
1430         memcpy(cmd->mifare, mifare_params, 6);
1431
1432         /* General bytes */
1433         cmd->gb_len = gb_len;
1434         memcpy(cmd->gb, gb, gb_len);
1435
1436         /* Len Tk */
1437         cmd->gb[gb_len] = 0;
1438
1439         memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1440
1441         frame->datalen += cmd_len;
1442
1443         pn533_tx_frame_finish(frame);
1444
1445         kfree(cmd);
1446
1447         return 0;
1448 }
1449
1450 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1451 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1452 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1453                                       u8 *params, int params_len)
1454 {
1455         struct sk_buff *skb_resp = arg;
1456         struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1457
1458         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1459
1460         if (params_len < 0) {
1461                 nfc_dev_err(&dev->interface->dev,
1462                             "Error %d when starting as a target",
1463                             params_len);
1464
1465                 return params_len;
1466         }
1467
1468         if (params_len > 0 && params[0] != 0) {
1469                 nfc_tm_deactivated(dev->nfc_dev);
1470
1471                 dev->tgt_mode = 0;
1472
1473                 kfree_skb(skb_resp);
1474                 return 0;
1475         }
1476
1477         skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1478         skb_pull(skb_resp,
1479                  PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1480         skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
1481
1482         return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1483 }
1484
1485 static void pn533_wq_tg_get_data(struct work_struct *work)
1486 {
1487         struct pn533 *dev = container_of(work, struct pn533, tg_work);
1488         struct pn533_frame *in_frame;
1489         struct sk_buff *skb_resp;
1490         size_t skb_resp_len;
1491
1492         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1493
1494         skb_resp_len = PN533_FRAME_HEADER_LEN +
1495                        PN533_CMD_DATAEXCH_HEAD_LEN +
1496                        PN533_CMD_DATAEXCH_DATA_MAXLEN +
1497                        PN533_FRAME_TAIL_LEN;
1498
1499         skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1500         if (!skb_resp)
1501                 return;
1502
1503         in_frame = (struct pn533_frame *)skb_resp->data;
1504
1505         pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1506         pn533_tx_frame_finish(dev->out_frame);
1507
1508         pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1509                                    skb_resp_len,
1510                                    pn533_tm_get_data_complete,
1511                                    skb_resp);
1512
1513         return;
1514 }
1515
1516 #define ATR_REQ_GB_OFFSET 17
1517 static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
1518 {
1519         struct pn533_cmd_init_target_response *resp;
1520         u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1521         size_t gb_len;
1522         int rc;
1523
1524         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1525
1526         if (params_len < 0) {
1527                 nfc_dev_err(&dev->interface->dev,
1528                             "Error %d when starting as a target",
1529                             params_len);
1530
1531                 return params_len;
1532         }
1533
1534         if (params_len < ATR_REQ_GB_OFFSET + 1)
1535                 return -EINVAL;
1536
1537         resp = (struct pn533_cmd_init_target_response *) params;
1538
1539         nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1540                     resp->mode, params_len);
1541
1542         frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1543         if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1544                 comm_mode = NFC_COMM_ACTIVE;
1545
1546         /* Again, only DEP */
1547         if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1548                 return -EOPNOTSUPP;
1549
1550         gb = resp->cmd + ATR_REQ_GB_OFFSET;
1551         gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1552
1553         rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1554                               comm_mode, gb, gb_len);
1555         if (rc < 0) {
1556                 nfc_dev_err(&dev->interface->dev,
1557                             "Error when signaling target activation");
1558                 return rc;
1559         }
1560
1561         dev->tgt_mode = 1;
1562
1563         queue_work(dev->wq, &dev->tg_work);
1564
1565         return 0;
1566 }
1567
1568 static void pn533_listen_mode_timer(unsigned long data)
1569 {
1570         struct pn533 *dev = (struct pn533 *) data;
1571
1572         nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1573
1574         /* An ack will cancel the last issued command (poll) */
1575         pn533_send_ack(dev, GFP_ATOMIC);
1576
1577         dev->cancel_listen = 1;
1578
1579         pn533_poll_next_mod(dev);
1580
1581         queue_work(dev->wq, &dev->poll_work);
1582 }
1583
1584 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1585                                u8 *params, int params_len)
1586 {
1587         struct pn533_poll_modulations *cur_mod;
1588         int rc;
1589
1590         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1591
1592         if (params_len == -ENOENT) {
1593                 if (dev->poll_mod_count != 0)
1594                         return 0;
1595
1596                 nfc_dev_err(&dev->interface->dev,
1597                             "Polling operation has been stopped");
1598
1599                 goto stop_poll;
1600         }
1601
1602         if (params_len < 0) {
1603                 nfc_dev_err(&dev->interface->dev,
1604                             "Error %d when running poll", params_len);
1605
1606                 goto stop_poll;
1607         }
1608
1609         cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1610
1611         if (cur_mod->len == 0) {
1612                 del_timer(&dev->listen_timer);
1613
1614                 return pn533_init_target_complete(dev, params, params_len);
1615         } else {
1616                 rc = pn533_start_poll_complete(dev, params, params_len);
1617                 if (!rc)
1618                         return rc;
1619         }
1620
1621         pn533_poll_next_mod(dev);
1622
1623         queue_work(dev->wq, &dev->poll_work);
1624
1625         return 0;
1626
1627 stop_poll:
1628         pn533_poll_reset_mod_list(dev);
1629         dev->poll_protocols = 0;
1630         return 0;
1631 }
1632
1633 static void pn533_build_poll_frame(struct pn533 *dev,
1634                                    struct pn533_frame *frame,
1635                                    struct pn533_poll_modulations *mod)
1636 {
1637         nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
1638
1639         if (mod->len == 0) {
1640                 /* Listen mode */
1641                 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1642         } else {
1643                 /* Polling mode */
1644                 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1645
1646                 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1647                 frame->datalen += mod->len;
1648
1649                 pn533_tx_frame_finish(frame);
1650         }
1651 }
1652
1653 static int pn533_send_poll_frame(struct pn533 *dev)
1654 {
1655         struct pn533_poll_modulations *cur_mod;
1656         int rc;
1657
1658         cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1659
1660         pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
1661
1662         rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1663                                         PN533_NORMAL_FRAME_MAX_LEN,
1664                                         pn533_poll_complete,
1665                                         NULL);
1666         if (rc)
1667                 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1668
1669         return rc;
1670 }
1671
1672 static void pn533_wq_poll(struct work_struct *work)
1673 {
1674         struct pn533 *dev = container_of(work, struct pn533, poll_work);
1675         struct pn533_poll_modulations *cur_mod;
1676         int rc;
1677
1678         cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1679
1680         nfc_dev_dbg(&dev->interface->dev,
1681                     "%s cancel_listen %d modulation len %d",
1682                     __func__, dev->cancel_listen, cur_mod->len);
1683
1684         if (dev->cancel_listen == 1) {
1685                 dev->cancel_listen = 0;
1686                 usb_kill_urb(dev->in_urb);
1687         }
1688
1689         rc = pn533_send_poll_frame(dev);
1690         if (rc)
1691                 return;
1692
1693         if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1694                 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1695
1696         return;
1697 }
1698
1699 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1700                             u32 im_protocols, u32 tm_protocols)
1701 {
1702         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1703
1704         nfc_dev_dbg(&dev->interface->dev,
1705                     "%s: im protocols 0x%x tm protocols 0x%x",
1706                     __func__, im_protocols, tm_protocols);
1707
1708         if (dev->tgt_active_prot) {
1709                 nfc_dev_err(&dev->interface->dev,
1710                             "Cannot poll with a target already activated");
1711                 return -EBUSY;
1712         }
1713
1714         if (dev->tgt_mode) {
1715                 nfc_dev_err(&dev->interface->dev,
1716                             "Cannot poll while already being activated");
1717                 return -EBUSY;
1718         }
1719
1720         if (tm_protocols) {
1721                 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1722                 if (dev->gb == NULL)
1723                         tm_protocols = 0;
1724         }
1725
1726         dev->poll_mod_curr = 0;
1727         pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1728         dev->poll_protocols = im_protocols;
1729         dev->listen_protocols = tm_protocols;
1730
1731         return pn533_send_poll_frame(dev);
1732 }
1733
1734 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1735 {
1736         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1737
1738         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1739
1740         del_timer(&dev->listen_timer);
1741
1742         if (!dev->poll_mod_count) {
1743                 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1744                                                                 " running");
1745                 return;
1746         }
1747
1748         /* An ack will cancel the last issued command (poll) */
1749         pn533_send_ack(dev, GFP_KERNEL);
1750
1751         /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1752         usb_kill_urb(dev->in_urb);
1753
1754         pn533_poll_reset_mod_list(dev);
1755 }
1756
1757 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1758 {
1759         struct pn533_cmd_activate_response *rsp;
1760         u16 gt_len;
1761         int rc;
1762
1763         struct sk_buff *skb;
1764         struct sk_buff *resp;
1765
1766         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1767
1768         skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1769         if (!skb)
1770                 return -ENOMEM;
1771
1772         *skb_put(skb, sizeof(u8)) = 1; /* TG */
1773         *skb_put(skb, sizeof(u8)) = 0; /* Next */
1774
1775         resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1776         if (IS_ERR(resp))
1777                 return PTR_ERR(resp);
1778
1779         rsp = (struct pn533_cmd_activate_response *) resp->data;
1780         rc = rsp->status & PN533_CMD_RET_MASK;
1781         if (rc != PN533_CMD_RET_SUCCESS)
1782                 dev_kfree_skb(resp);
1783                 return -EIO;
1784
1785         /* ATR_RES general bytes are located at offset 16 */
1786         gt_len = resp->len - 16;
1787         rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1788
1789         dev_kfree_skb(resp);
1790         return rc;
1791 }
1792
1793 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1794                                  struct nfc_target *target, u32 protocol)
1795 {
1796         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1797         int rc;
1798
1799         nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1800                                                                 protocol);
1801
1802         if (dev->poll_mod_count) {
1803                 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1804                                                                 " polling");
1805                 return -EBUSY;
1806         }
1807
1808         if (dev->tgt_active_prot) {
1809                 nfc_dev_err(&dev->interface->dev, "There is already an active"
1810                                                                 " target");
1811                 return -EBUSY;
1812         }
1813
1814         if (!dev->tgt_available_prots) {
1815                 nfc_dev_err(&dev->interface->dev, "There is no available target"
1816                                                                 " to activate");
1817                 return -EINVAL;
1818         }
1819
1820         if (!(dev->tgt_available_prots & (1 << protocol))) {
1821                 nfc_dev_err(&dev->interface->dev, "The target does not support"
1822                                         " the requested protocol %u", protocol);
1823                 return -EINVAL;
1824         }
1825
1826         if (protocol == NFC_PROTO_NFC_DEP) {
1827                 rc = pn533_activate_target_nfcdep(dev);
1828                 if (rc) {
1829                         nfc_dev_err(&dev->interface->dev, "Error %d when"
1830                                                 " activating target with"
1831                                                 " NFC_DEP protocol", rc);
1832                         return rc;
1833                 }
1834         }
1835
1836         dev->tgt_active_prot = protocol;
1837         dev->tgt_available_prots = 0;
1838
1839         return 0;
1840 }
1841
1842 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1843                                     struct nfc_target *target)
1844 {
1845         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1846
1847         struct sk_buff *skb;
1848         struct sk_buff *resp;
1849
1850         int rc;
1851
1852         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1853
1854         if (!dev->tgt_active_prot) {
1855                 nfc_dev_err(&dev->interface->dev, "There is no active target");
1856                 return;
1857         }
1858
1859         dev->tgt_active_prot = 0;
1860         skb_queue_purge(&dev->resp_q);
1861
1862         skb = pn533_alloc_skb(sizeof(u8));
1863         if (!skb)
1864                 return;
1865
1866         *skb_put(skb, 1) = 1; /* TG*/
1867
1868         resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1869         if (IS_ERR(resp))
1870                 return;
1871
1872         rc = resp->data[0] & PN533_CMD_RET_MASK;
1873         if (rc != PN533_CMD_RET_SUCCESS)
1874                 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1875                                                         " the target", rc);
1876
1877         dev_kfree_skb(resp);
1878         return;
1879 }
1880
1881
1882 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1883                                                 u8 *params, int params_len)
1884 {
1885         struct pn533_cmd_jump_dep_response *resp;
1886         struct nfc_target nfc_target;
1887         u8 target_gt_len;
1888         int rc;
1889         struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1890         u8 active = cmd->active;
1891
1892         kfree(arg);
1893
1894         if (params_len == -ENOENT) {
1895                 nfc_dev_dbg(&dev->interface->dev, "");
1896                 return 0;
1897         }
1898
1899         if (params_len < 0) {
1900                 nfc_dev_err(&dev->interface->dev,
1901                                 "Error %d when bringing DEP link up",
1902                                                                 params_len);
1903                 return 0;
1904         }
1905
1906         if (dev->tgt_available_prots &&
1907             !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1908                 nfc_dev_err(&dev->interface->dev,
1909                         "The target does not support DEP");
1910                 return -EINVAL;
1911         }
1912
1913         resp = (struct pn533_cmd_jump_dep_response *) params;
1914         rc = resp->status & PN533_CMD_RET_MASK;
1915         if (rc != PN533_CMD_RET_SUCCESS) {
1916                 nfc_dev_err(&dev->interface->dev,
1917                                 "Bringing DEP link up failed %d", rc);
1918                 return 0;
1919         }
1920
1921         if (!dev->tgt_available_prots) {
1922                 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1923
1924                 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1925                 nfc_target.nfcid1_len = 10;
1926                 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
1927                 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1928                 if (rc)
1929                         return 0;
1930
1931                 dev->tgt_available_prots = 0;
1932         }
1933
1934         dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1935
1936         /* ATR_RES general bytes are located at offset 17 */
1937         target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1938         rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1939                                                 resp->gt, target_gt_len);
1940         if (rc == 0)
1941                 rc = nfc_dep_link_is_up(dev->nfc_dev,
1942                                                 dev->nfc_dev->targets[0].idx,
1943                                                 !active, NFC_RF_INITIATOR);
1944
1945         return 0;
1946 }
1947
1948 static int pn533_mod_to_baud(struct pn533 *dev)
1949 {
1950         switch (dev->poll_mod_curr) {
1951         case PN533_POLL_MOD_106KBPS_A:
1952                 return 0;
1953         case PN533_POLL_MOD_212KBPS_FELICA:
1954                 return 1;
1955         case PN533_POLL_MOD_424KBPS_FELICA:
1956                 return 2;
1957         default:
1958                 return -EINVAL;
1959         }
1960 }
1961
1962 #define PASSIVE_DATA_LEN 5
1963 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1964                              u8 comm_mode, u8* gb, size_t gb_len)
1965 {
1966         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1967         struct pn533_cmd_jump_dep *cmd;
1968         u8 cmd_len, *data_ptr;
1969         u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1970         int rc, baud;
1971
1972         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1973
1974         if (dev->poll_mod_count) {
1975                 nfc_dev_err(&dev->interface->dev,
1976                                 "Cannot bring the DEP link up while polling");
1977                 return -EBUSY;
1978         }
1979
1980         if (dev->tgt_active_prot) {
1981                 nfc_dev_err(&dev->interface->dev,
1982                                 "There is already an active target");
1983                 return -EBUSY;
1984         }
1985
1986         baud = pn533_mod_to_baud(dev);
1987         if (baud < 0) {
1988                 nfc_dev_err(&dev->interface->dev,
1989                             "Invalid curr modulation %d", dev->poll_mod_curr);
1990                 return baud;
1991         }
1992
1993         cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
1994         if (comm_mode == NFC_COMM_PASSIVE)
1995                 cmd_len += PASSIVE_DATA_LEN;
1996
1997         cmd = kzalloc(cmd_len, GFP_KERNEL);
1998         if (cmd == NULL)
1999                 return -ENOMEM;
2000
2001         pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
2002
2003         cmd->active = !comm_mode;
2004         cmd->next = 0;
2005         cmd->baud = baud;
2006         data_ptr = cmd->data;
2007         if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
2008                 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
2009                 cmd->next |= 1;
2010                 data_ptr += PASSIVE_DATA_LEN;
2011         }
2012
2013         if (gb != NULL && gb_len > 0) {
2014                 cmd->next |= 4; /* We have some Gi */
2015                 memcpy(data_ptr, gb, gb_len);
2016         } else {
2017                 cmd->next = 0;
2018         }
2019
2020         memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2021         dev->out_frame->datalen += cmd_len;
2022
2023         pn533_tx_frame_finish(dev->out_frame);
2024
2025         rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
2026                                         PN533_NORMAL_FRAME_MAX_LEN,
2027                                         pn533_in_dep_link_up_complete, cmd);
2028         if (rc < 0)
2029                 kfree(cmd);
2030
2031         return rc;
2032 }
2033
2034 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2035 {
2036         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2037
2038         pn533_poll_reset_mod_list(dev);
2039
2040         if (dev->tgt_mode || dev->tgt_active_prot) {
2041                 pn533_send_ack(dev, GFP_KERNEL);
2042                 usb_kill_urb(dev->in_urb);
2043         }
2044
2045         dev->tgt_active_prot = 0;
2046         dev->tgt_mode = 0;
2047
2048         skb_queue_purge(&dev->resp_q);
2049
2050         return 0;
2051 }
2052
2053 static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2054                                 bool target)
2055 {
2056         int payload_len = skb->len;
2057         struct pn533_frame *out_frame;
2058         u8 tg;
2059
2060         nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2061                                                                 payload_len);
2062
2063         if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2064                 /* TODO: Implement support to multi-part data exchange */
2065                 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2066                                                 " max allowed: %d",
2067                                                 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2068                 return -ENOSYS;
2069         }
2070
2071         skb_push(skb, PN533_FRAME_HEADER_LEN);
2072
2073         if (target == true) {
2074                 switch (dev->device_type) {
2075                 case PN533_DEVICE_PASORI:
2076                         if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2077                                 out_frame = (struct pn533_frame *) skb->data;
2078                                 pn533_tx_frame_init(out_frame,
2079                                                     PN533_CMD_IN_COMM_THRU);
2080
2081                                 break;
2082                         }
2083
2084                 default:
2085                         skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2086                         out_frame = (struct pn533_frame *) skb->data;
2087                         pn533_tx_frame_init(out_frame,
2088                                             PN533_CMD_IN_DATA_EXCHANGE);
2089                         tg = 1;
2090                         memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2091                                &tg, sizeof(u8));
2092                         out_frame->datalen += sizeof(u8);
2093
2094                         break;
2095                 }
2096
2097         } else {
2098                 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2099                 out_frame = (struct pn533_frame *) skb->data;
2100                 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2101         }
2102
2103
2104         /* The data is already in the out_frame, just update the datalen */
2105         out_frame->datalen += payload_len;
2106
2107         pn533_tx_frame_finish(out_frame);
2108         skb_put(skb, PN533_FRAME_TAIL_LEN);
2109
2110         return 0;
2111 }
2112
2113 struct pn533_data_exchange_arg {
2114         struct sk_buff *skb_resp;
2115         struct sk_buff *skb_out;
2116         data_exchange_cb_t cb;
2117         void *cb_context;
2118 };
2119
2120 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2121 {
2122         struct sk_buff *skb, *tmp, *t;
2123         unsigned int skb_len = 0, tmp_len = 0;
2124
2125         nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2126
2127         if (skb_queue_empty(&dev->resp_q))
2128                 return NULL;
2129
2130         if (skb_queue_len(&dev->resp_q) == 1) {
2131                 skb = skb_dequeue(&dev->resp_q);
2132                 goto out;
2133         }
2134
2135         skb_queue_walk_safe(&dev->resp_q, tmp, t)
2136                 skb_len += tmp->len;
2137
2138         nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2139                     __func__, skb_len);
2140
2141         skb = alloc_skb(skb_len, GFP_KERNEL);
2142         if (skb == NULL)
2143                 goto out;
2144
2145         skb_put(skb, skb_len);
2146
2147         skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2148                 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2149                 tmp_len += tmp->len;
2150         }
2151
2152 out:
2153         skb_queue_purge(&dev->resp_q);
2154
2155         return skb;
2156 }
2157
2158 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2159                                                 u8 *params, int params_len)
2160 {
2161         struct pn533_data_exchange_arg *arg = _arg;
2162         struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
2163         struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2164         int err = 0;
2165         u8 status;
2166         u8 cmd_ret;
2167
2168         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2169
2170         dev_kfree_skb(arg->skb_out);
2171
2172         if (params_len < 0) { /* error */
2173                 err = params_len;
2174                 goto error;
2175         }
2176
2177         status = params[0];
2178
2179         cmd_ret = status & PN533_CMD_RET_MASK;
2180         if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2181                 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2182                                                 " exchanging data", cmd_ret);
2183                 err = -EIO;
2184                 goto error;
2185         }
2186
2187         skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
2188         skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
2189         skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
2190         skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
2191         skb_queue_tail(&dev->resp_q, skb_resp);
2192
2193         if (status & PN533_CMD_MI_MASK) {
2194                 queue_work(dev->wq, &dev->mi_work);
2195                 return -EINPROGRESS;
2196         }
2197
2198         skb = pn533_build_response(dev);
2199         if (skb == NULL)
2200                 goto error;
2201
2202         arg->cb(arg->cb_context, skb, 0);
2203         kfree(arg);
2204         return 0;
2205
2206 error:
2207         skb_queue_purge(&dev->resp_q);
2208         dev_kfree_skb(skb_resp);
2209         arg->cb(arg->cb_context, NULL, err);
2210         kfree(arg);
2211         return 0;
2212 }
2213
2214 static int pn533_transceive(struct nfc_dev *nfc_dev,
2215                             struct nfc_target *target, struct sk_buff *skb,
2216                             data_exchange_cb_t cb, void *cb_context)
2217 {
2218         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2219         struct pn533_frame *out_frame, *in_frame;
2220         struct pn533_data_exchange_arg *arg;
2221         struct sk_buff *skb_resp;
2222         int skb_resp_len;
2223         int rc;
2224
2225         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2226
2227         if (!dev->tgt_active_prot) {
2228                 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2229                                                 " there is no active target");
2230                 rc = -EINVAL;
2231                 goto error;
2232         }
2233
2234         rc = pn533_build_tx_frame(dev, skb, true);
2235         if (rc)
2236                 goto error;
2237
2238         skb_resp_len = PN533_FRAME_HEADER_LEN +
2239                        PN533_CMD_DATAEXCH_HEAD_LEN +
2240                        PN533_CMD_DATAEXCH_DATA_MAXLEN +
2241                        PN533_FRAME_TAIL_LEN;
2242
2243         skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
2244         if (!skb_resp) {
2245                 rc = -ENOMEM;
2246                 goto error;
2247         }
2248
2249         in_frame = (struct pn533_frame *) skb_resp->data;
2250         out_frame = (struct pn533_frame *) skb->data;
2251
2252         arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2253         if (!arg) {
2254                 rc = -ENOMEM;
2255                 goto free_skb_resp;
2256         }
2257
2258         arg->skb_resp = skb_resp;
2259         arg->skb_out = skb;
2260         arg->cb = cb;
2261         arg->cb_context = cb_context;
2262
2263         rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2264                                         pn533_data_exchange_complete, arg);
2265         if (rc) {
2266                 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2267                                                 " perform data_exchange", rc);
2268                 goto free_arg;
2269         }
2270
2271         return 0;
2272
2273 free_arg:
2274         kfree(arg);
2275 free_skb_resp:
2276         kfree_skb(skb_resp);
2277 error:
2278         kfree_skb(skb);
2279         return rc;
2280 }
2281
2282 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2283                                   u8 *params, int params_len)
2284 {
2285         struct sk_buff *skb_out = arg;
2286
2287         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2288
2289         dev_kfree_skb(skb_out);
2290
2291         if (params_len < 0) {
2292                 nfc_dev_err(&dev->interface->dev,
2293                             "Error %d when sending data",
2294                             params_len);
2295
2296                 return params_len;
2297         }
2298
2299         if (params_len > 0 && params[0] != 0) {
2300                 nfc_tm_deactivated(dev->nfc_dev);
2301
2302                 dev->tgt_mode = 0;
2303
2304                 return 0;
2305         }
2306
2307         queue_work(dev->wq, &dev->tg_work);
2308
2309         return 0;
2310 }
2311
2312 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2313 {
2314         struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2315         struct pn533_frame *out_frame;
2316         int rc;
2317
2318         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2319
2320         rc = pn533_build_tx_frame(dev, skb, false);
2321         if (rc)
2322                 goto error;
2323
2324         out_frame = (struct pn533_frame *) skb->data;
2325
2326         rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2327                                         PN533_NORMAL_FRAME_MAX_LEN,
2328                                         pn533_tm_send_complete, skb);
2329         if (rc) {
2330                 nfc_dev_err(&dev->interface->dev,
2331                             "Error %d when trying to send data", rc);
2332                 goto error;
2333         }
2334
2335         return 0;
2336
2337 error:
2338         kfree_skb(skb);
2339
2340         return rc;
2341 }
2342
2343 static void pn533_wq_mi_recv(struct work_struct *work)
2344 {
2345         struct pn533 *dev = container_of(work, struct pn533, mi_work);
2346         struct sk_buff *skb_cmd;
2347         struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2348         struct pn533_frame *out_frame, *in_frame;
2349         struct sk_buff *skb_resp;
2350         int skb_resp_len;
2351         int rc;
2352
2353         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2354
2355         /* This is a zero payload size skb */
2356         skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2357         if (skb_cmd == NULL)
2358                 goto error_cmd;
2359
2360         skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2361
2362         rc = pn533_build_tx_frame(dev, skb_cmd, true);
2363         if (rc)
2364                 goto error_frame;
2365
2366         skb_resp_len = PN533_FRAME_HEADER_LEN +
2367                        PN533_CMD_DATAEXCH_HEAD_LEN +
2368                        PN533_CMD_DATAEXCH_DATA_MAXLEN +
2369                        PN533_FRAME_TAIL_LEN;
2370
2371         skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2372         if (!skb_resp) {
2373                 rc = -ENOMEM;
2374                 goto error_frame;
2375         }
2376
2377         in_frame = (struct pn533_frame *) skb_resp->data;
2378         out_frame = (struct pn533_frame *) skb_cmd->data;
2379
2380         arg->skb_resp = skb_resp;
2381         arg->skb_out = skb_cmd;
2382
2383         rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2384                                           skb_resp_len,
2385                                           pn533_data_exchange_complete,
2386                                           dev->cmd_complete_arg);
2387         if (!rc)
2388                 return;
2389
2390         nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2391                                                 " perform data_exchange", rc);
2392
2393         kfree_skb(skb_resp);
2394
2395 error_frame:
2396         kfree_skb(skb_cmd);
2397
2398 error_cmd:
2399         pn533_send_ack(dev, GFP_KERNEL);
2400
2401         kfree(arg);
2402
2403         queue_work(dev->wq, &dev->cmd_work);
2404 }
2405
2406 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2407                                                                 u8 cfgdata_len)
2408 {
2409         struct sk_buff *skb;
2410         struct sk_buff *resp;
2411
2412         int skb_len;
2413
2414         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2415
2416         skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2417
2418         skb = pn533_alloc_skb(skb_len);
2419         if (!skb)
2420                 return -ENOMEM;
2421
2422         *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2423         memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2424
2425         resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2426         if (IS_ERR(resp))
2427                 return PTR_ERR(resp);
2428
2429         dev_kfree_skb(resp);
2430         return 0;
2431 }
2432
2433 static int pn533_get_firmware_version(struct pn533 *dev,
2434                                       struct pn533_fw_version *fv)
2435 {
2436         struct sk_buff *skb;
2437         struct sk_buff *resp;
2438
2439         skb = pn533_alloc_skb(0);
2440         if (!skb)
2441                 return -ENOMEM;
2442
2443         resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2444         if (IS_ERR(resp))
2445                 return PTR_ERR(resp);
2446
2447         fv->ic = resp->data[0];
2448         fv->ver = resp->data[1];
2449         fv->rev = resp->data[2];
2450         fv->support = resp->data[3];
2451
2452         dev_kfree_skb(resp);
2453         return 0;
2454 }
2455
2456 static int pn533_fw_reset(struct pn533 *dev)
2457 {
2458         struct sk_buff *skb;
2459         struct sk_buff *resp;
2460
2461         nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2462
2463         skb = pn533_alloc_skb(sizeof(u8));
2464         if (!skb)
2465                 return -ENOMEM;
2466
2467         *skb_put(skb, sizeof(u8)) = 0x1;
2468
2469         resp = pn533_send_cmd_sync(dev, 0x18, skb);
2470         if (IS_ERR(resp))
2471                 return PTR_ERR(resp);
2472
2473         dev_kfree_skb(resp);
2474
2475         return 0;
2476 }
2477
2478 static struct nfc_ops pn533_nfc_ops = {
2479         .dev_up = NULL,
2480         .dev_down = NULL,
2481         .dep_link_up = pn533_dep_link_up,
2482         .dep_link_down = pn533_dep_link_down,
2483         .start_poll = pn533_start_poll,
2484         .stop_poll = pn533_stop_poll,
2485         .activate_target = pn533_activate_target,
2486         .deactivate_target = pn533_deactivate_target,
2487         .im_transceive = pn533_transceive,
2488         .tm_send = pn533_tm_send,
2489 };
2490
2491 static int pn533_setup(struct pn533 *dev)
2492 {
2493         struct pn533_config_max_retries max_retries;
2494         struct pn533_config_timing timing;
2495         u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2496         int rc;
2497
2498         switch (dev->device_type) {
2499         case PN533_DEVICE_STD:
2500                 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2501                 max_retries.mx_rty_psl = 2;
2502                 max_retries.mx_rty_passive_act =
2503                         PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2504
2505                 timing.rfu = PN533_CONFIG_TIMING_102;
2506                 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2507                 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2508
2509                 break;
2510
2511         case PN533_DEVICE_PASORI:
2512                 max_retries.mx_rty_atr = 0x2;
2513                 max_retries.mx_rty_psl = 0x1;
2514                 max_retries.mx_rty_passive_act =
2515                         PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2516
2517                 timing.rfu = PN533_CONFIG_TIMING_102;
2518                 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2519                 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2520
2521                 break;
2522
2523         default:
2524                 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2525                             dev->device_type);
2526                 return -EINVAL;
2527         }
2528
2529         rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2530                                      (u8 *)&max_retries, sizeof(max_retries));
2531         if (rc) {
2532                 nfc_dev_err(&dev->interface->dev,
2533                             "Error on setting MAX_RETRIES config");
2534                 return rc;
2535         }
2536
2537
2538         rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2539                                      (u8 *)&timing, sizeof(timing));
2540         if (rc) {
2541                 nfc_dev_err(&dev->interface->dev,
2542                             "Error on setting RF timings");
2543                 return rc;
2544         }
2545
2546         switch (dev->device_type) {
2547         case PN533_DEVICE_STD:
2548                 break;
2549
2550         case PN533_DEVICE_PASORI:
2551                 pn533_fw_reset(dev);
2552
2553                 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2554                                              pasori_cfg, 3);
2555                 if (rc) {
2556                         nfc_dev_err(&dev->interface->dev,
2557                                     "Error while settings PASORI config");
2558                         return rc;
2559                 }
2560
2561                 pn533_fw_reset(dev);
2562
2563                 break;
2564         }
2565
2566         return 0;
2567 }
2568
2569 static int pn533_probe(struct usb_interface *interface,
2570                         const struct usb_device_id *id)
2571 {
2572         struct pn533_fw_version fw_ver;
2573         struct pn533 *dev;
2574         struct usb_host_interface *iface_desc;
2575         struct usb_endpoint_descriptor *endpoint;
2576         int in_endpoint = 0;
2577         int out_endpoint = 0;
2578         int rc = -ENOMEM;
2579         int i;
2580         u32 protocols;
2581
2582         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2583         if (!dev)
2584                 return -ENOMEM;
2585
2586         dev->udev = usb_get_dev(interface_to_usbdev(interface));
2587         dev->interface = interface;
2588         mutex_init(&dev->cmd_lock);
2589
2590         iface_desc = interface->cur_altsetting;
2591         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2592                 endpoint = &iface_desc->endpoint[i].desc;
2593
2594                 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2595                         in_endpoint = endpoint->bEndpointAddress;
2596
2597                 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2598                         out_endpoint = endpoint->bEndpointAddress;
2599         }
2600
2601         if (!in_endpoint || !out_endpoint) {
2602                 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2603                                                         " bulk-out endpoint");
2604                 rc = -ENODEV;
2605                 goto error;
2606         }
2607
2608         dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2609         dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2610         dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2611         dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2612
2613         if (!dev->in_frame || !dev->out_frame ||
2614                 !dev->in_urb || !dev->out_urb)
2615                 goto error;
2616
2617         usb_fill_bulk_urb(dev->in_urb, dev->udev,
2618                         usb_rcvbulkpipe(dev->udev, in_endpoint),
2619                         NULL, 0, NULL, dev);
2620         usb_fill_bulk_urb(dev->out_urb, dev->udev,
2621                         usb_sndbulkpipe(dev->udev, out_endpoint),
2622                         NULL, 0,
2623                         pn533_send_complete, dev);
2624
2625         INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2626         INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2627         INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2628         INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2629         INIT_WORK(&dev->poll_work, pn533_wq_poll);
2630         dev->wq = alloc_ordered_workqueue("pn533", 0);
2631         if (dev->wq == NULL)
2632                 goto error;
2633
2634         init_timer(&dev->listen_timer);
2635         dev->listen_timer.data = (unsigned long) dev;
2636         dev->listen_timer.function = pn533_listen_mode_timer;
2637
2638         skb_queue_head_init(&dev->resp_q);
2639
2640         INIT_LIST_HEAD(&dev->cmd_queue);
2641
2642         usb_set_intfdata(interface, dev);
2643
2644         memset(&fw_ver, 0, sizeof(fw_ver));
2645         rc = pn533_get_firmware_version(dev, &fw_ver);
2646         if (rc < 0)
2647                 goto destroy_wq;
2648
2649         nfc_dev_info(&dev->interface->dev,
2650                      "NXP PN533 firmware ver %d.%d now attached",
2651                      fw_ver.ver, fw_ver.rev);
2652
2653         dev->device_type = id->driver_info;
2654         switch (dev->device_type) {
2655         case PN533_DEVICE_STD:
2656                 protocols = PN533_ALL_PROTOCOLS;
2657                 break;
2658
2659         case PN533_DEVICE_PASORI:
2660                 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2661                 break;
2662
2663         default:
2664                 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2665                             dev->device_type);
2666                 rc = -EINVAL;
2667                 goto destroy_wq;
2668         }
2669
2670         dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2671                                            PN533_FRAME_HEADER_LEN +
2672                                            PN533_CMD_DATAEXCH_HEAD_LEN,
2673                                            PN533_FRAME_TAIL_LEN);
2674         if (!dev->nfc_dev)
2675                 goto destroy_wq;
2676
2677         nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2678         nfc_set_drvdata(dev->nfc_dev, dev);
2679
2680         rc = nfc_register_device(dev->nfc_dev);
2681         if (rc)
2682                 goto free_nfc_dev;
2683
2684         rc = pn533_setup(dev);
2685         if (rc)
2686                 goto unregister_nfc_dev;
2687
2688         return 0;
2689
2690 unregister_nfc_dev:
2691         nfc_unregister_device(dev->nfc_dev);
2692
2693 free_nfc_dev:
2694         nfc_free_device(dev->nfc_dev);
2695
2696 destroy_wq:
2697         destroy_workqueue(dev->wq);
2698 error:
2699         kfree(dev->in_frame);
2700         usb_free_urb(dev->in_urb);
2701         kfree(dev->out_frame);
2702         usb_free_urb(dev->out_urb);
2703         kfree(dev);
2704         return rc;
2705 }
2706
2707 static void pn533_disconnect(struct usb_interface *interface)
2708 {
2709         struct pn533 *dev;
2710         struct pn533_cmd *cmd, *n;
2711
2712         dev = usb_get_intfdata(interface);
2713         usb_set_intfdata(interface, NULL);
2714
2715         nfc_unregister_device(dev->nfc_dev);
2716         nfc_free_device(dev->nfc_dev);
2717
2718         usb_kill_urb(dev->in_urb);
2719         usb_kill_urb(dev->out_urb);
2720
2721         destroy_workqueue(dev->wq);
2722
2723         skb_queue_purge(&dev->resp_q);
2724
2725         del_timer(&dev->listen_timer);
2726
2727         list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2728                 list_del(&cmd->queue);
2729                 kfree(cmd);
2730         }
2731
2732         kfree(dev->in_frame);
2733         usb_free_urb(dev->in_urb);
2734         kfree(dev->out_frame);
2735         usb_free_urb(dev->out_urb);
2736         kfree(dev);
2737
2738         nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2739 }
2740
2741 static struct usb_driver pn533_driver = {
2742         .name =         "pn533",
2743         .probe =        pn533_probe,
2744         .disconnect =   pn533_disconnect,
2745         .id_table =     pn533_table,
2746 };
2747
2748 module_usb_driver(pn533_driver);
2749
2750 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2751                         " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2752 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2753 MODULE_VERSION(VERSION);
2754 MODULE_LICENSE("GPL");