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