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