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