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