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