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