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