NFC: Convert pn533 from tasklet to workqueues
[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
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
48/* frame definitions */
49#define PN533_FRAME_TAIL_SIZE 2
50#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
51 PN533_FRAME_TAIL_SIZE)
52#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
53#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
54#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
55
56/* start of frame */
57#define PN533_SOF 0x00FF
58
59/* frame identifier: in/out/error */
60#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
61#define PN533_DIR_OUT 0xD4
62#define PN533_DIR_IN 0xD5
63
64/* PN533 Commands */
65#define PN533_FRAME_CMD(f) (f->data[1])
66#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
67#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
68
69#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
70#define PN533_CMD_RF_CONFIGURATION 0x32
71#define PN533_CMD_IN_DATA_EXCHANGE 0x40
72#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
73#define PN533_CMD_IN_ATR 0x50
74#define PN533_CMD_IN_RELEASE 0x52
361f3cb7 75#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
c46ee386
AAJ
76
77#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
78
79/* PN533 Return codes */
80#define PN533_CMD_RET_MASK 0x3F
81#define PN533_CMD_MI_MASK 0x40
82#define PN533_CMD_RET_SUCCESS 0x00
83
84struct pn533;
85
86typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
87 u8 *params, int params_len);
88
89/* structs for pn533 commands */
90
91/* PN533_CMD_GET_FIRMWARE_VERSION */
92struct pn533_fw_version {
93 u8 ic;
94 u8 ver;
95 u8 rev;
96 u8 support;
97};
98
99/* PN533_CMD_RF_CONFIGURATION */
100#define PN533_CFGITEM_MAX_RETRIES 0x05
101
102#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
103#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
104
105struct pn533_config_max_retries {
106 u8 mx_rty_atr;
107 u8 mx_rty_psl;
108 u8 mx_rty_passive_act;
109} __packed;
110
111/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
112
113/* felica commands opcode */
114#define PN533_FELICA_OPC_SENSF_REQ 0
115#define PN533_FELICA_OPC_SENSF_RES 1
116/* felica SENSF_REQ parameters */
117#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
118#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
119#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
120#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
121
122/* type B initiator_data values */
123#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
124#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
125#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
126
127union pn533_cmd_poll_initdata {
128 struct {
129 u8 afi;
130 u8 polling_method;
131 } __packed type_b;
132 struct {
133 u8 opcode;
134 __be16 sc;
135 u8 rc;
136 u8 tsn;
137 } __packed felica;
138};
139
140/* Poll modulations */
141enum {
142 PN533_POLL_MOD_106KBPS_A,
143 PN533_POLL_MOD_212KBPS_FELICA,
144 PN533_POLL_MOD_424KBPS_FELICA,
145 PN533_POLL_MOD_106KBPS_JEWEL,
146 PN533_POLL_MOD_847KBPS_B,
147
148 __PN533_POLL_MOD_AFTER_LAST,
149};
150#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
151
152struct pn533_poll_modulations {
153 struct {
154 u8 maxtg;
155 u8 brty;
156 union pn533_cmd_poll_initdata initiator_data;
157 } __packed data;
158 u8 len;
159};
160
161const struct pn533_poll_modulations poll_mod[] = {
162 [PN533_POLL_MOD_106KBPS_A] = {
163 .data = {
164 .maxtg = 1,
165 .brty = 0,
166 },
167 .len = 2,
168 },
169 [PN533_POLL_MOD_212KBPS_FELICA] = {
170 .data = {
171 .maxtg = 1,
172 .brty = 1,
173 .initiator_data.felica = {
174 .opcode = PN533_FELICA_OPC_SENSF_REQ,
175 .sc = PN533_FELICA_SENSF_SC_ALL,
176 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
177 .tsn = 0,
178 },
179 },
180 .len = 7,
181 },
182 [PN533_POLL_MOD_424KBPS_FELICA] = {
183 .data = {
184 .maxtg = 1,
185 .brty = 2,
186 .initiator_data.felica = {
187 .opcode = PN533_FELICA_OPC_SENSF_REQ,
188 .sc = PN533_FELICA_SENSF_SC_ALL,
189 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
190 .tsn = 0,
191 },
192 },
193 .len = 7,
194 },
195 [PN533_POLL_MOD_106KBPS_JEWEL] = {
196 .data = {
197 .maxtg = 1,
198 .brty = 4,
199 },
200 .len = 2,
201 },
202 [PN533_POLL_MOD_847KBPS_B] = {
203 .data = {
204 .maxtg = 1,
205 .brty = 8,
206 .initiator_data.type_b = {
207 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
208 .polling_method =
209 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
210 },
211 },
212 .len = 3,
213 },
214};
215
216/* PN533_CMD_IN_ATR */
217
218struct pn533_cmd_activate_param {
219 u8 tg;
220 u8 next;
221} __packed;
222
223struct pn533_cmd_activate_response {
224 u8 status;
225 u8 nfcid3t[10];
226 u8 didt;
227 u8 bst;
228 u8 brt;
229 u8 to;
230 u8 ppt;
231 /* optional */
232 u8 gt[];
233} __packed;
234
361f3cb7
SO
235/* PN533_CMD_IN_JUMP_FOR_DEP */
236struct pn533_cmd_jump_dep {
237 u8 active;
238 u8 baud;
239 u8 next;
240 u8 gt[];
241} __packed;
242
243struct pn533_cmd_jump_dep_response {
244 u8 status;
245 u8 tg;
246 u8 nfcid3t[10];
247 u8 didt;
248 u8 bst;
249 u8 brt;
250 u8 to;
251 u8 ppt;
252 /* optional */
253 u8 gt[];
254} __packed;
c46ee386
AAJ
255
256struct pn533 {
257 struct usb_device *udev;
258 struct usb_interface *interface;
259 struct nfc_dev *nfc_dev;
260
261 struct urb *out_urb;
262 int out_maxlen;
263 struct pn533_frame *out_frame;
264
265 struct urb *in_urb;
266 int in_maxlen;
267 struct pn533_frame *in_frame;
268
4849f85e
SO
269 struct workqueue_struct *wq;
270 struct work_struct cmd_work;
271 struct pn533_frame *wq_in_frame;
272 int wq_in_error;
c46ee386
AAJ
273
274 pn533_cmd_complete_t cmd_complete;
275 void *cmd_complete_arg;
276 struct semaphore cmd_lock;
277 u8 cmd;
278
279 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
280 u8 poll_mod_count;
281 u8 poll_mod_curr;
282 u32 poll_protocols;
283
284 u8 tgt_available_prots;
285 u8 tgt_active_prot;
286};
287
288struct pn533_frame {
289 u8 preamble;
290 __be16 start_frame;
291 u8 datalen;
292 u8 datalen_checksum;
293 u8 data[];
294} __packed;
295
296/* The rule: value + checksum = 0 */
297static inline u8 pn533_checksum(u8 value)
298{
299 return ~value + 1;
300}
301
302/* The rule: sum(data elements) + checksum = 0 */
303static u8 pn533_data_checksum(u8 *data, int datalen)
304{
305 u8 sum = 0;
306 int i;
307
308 for (i = 0; i < datalen; i++)
309 sum += data[i];
310
311 return pn533_checksum(sum);
312}
313
314/**
315 * pn533_tx_frame_ack - create a ack frame
316 * @frame: The frame to be set as ack
317 *
318 * Ack is different type of standard frame. As a standard frame, it has
319 * preamble and start_frame. However the checksum of this frame must fail,
320 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
321 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
322 * After datalen_checksum field, the postamble is placed.
323 */
324static void pn533_tx_frame_ack(struct pn533_frame *frame)
325{
326 frame->preamble = 0;
327 frame->start_frame = cpu_to_be16(PN533_SOF);
328 frame->datalen = 0;
329 frame->datalen_checksum = 0xFF;
330 /* data[0] is used as postamble */
331 frame->data[0] = 0;
332}
333
334static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
335{
336 frame->preamble = 0;
337 frame->start_frame = cpu_to_be16(PN533_SOF);
338 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
339 PN533_FRAME_CMD(frame) = cmd;
340 frame->datalen = 2;
341}
342
343static void pn533_tx_frame_finish(struct pn533_frame *frame)
344{
345 frame->datalen_checksum = pn533_checksum(frame->datalen);
346
347 PN533_FRAME_CHECKSUM(frame) =
348 pn533_data_checksum(frame->data, frame->datalen);
349
350 PN533_FRAME_POSTAMBLE(frame) = 0;
351}
352
353static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
354{
355 u8 checksum;
356
357 if (frame->start_frame != cpu_to_be16(PN533_SOF))
358 return false;
359
360 checksum = pn533_checksum(frame->datalen);
361 if (checksum != frame->datalen_checksum)
362 return false;
363
364 checksum = pn533_data_checksum(frame->data, frame->datalen);
365 if (checksum != PN533_FRAME_CHECKSUM(frame))
366 return false;
367
368 return true;
369}
370
371static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
372{
373 if (frame->start_frame != cpu_to_be16(PN533_SOF))
374 return false;
375
376 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
377 return false;
378
379 return true;
380}
381
382static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
383{
384 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
385}
386
4849f85e
SO
387
388static void pn533_wq_cmd_complete(struct work_struct *work)
c46ee386 389{
4849f85e
SO
390 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
391 struct pn533_frame *in_frame;
c46ee386
AAJ
392 int rc;
393
4849f85e
SO
394 if (dev == NULL)
395 return;
396
397 in_frame = dev->wq_in_frame;
398
399 if (dev->wq_in_error)
c46ee386 400 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
4849f85e 401 dev->wq_in_error);
c46ee386
AAJ
402 else
403 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
404 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
405 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
406
407 if (rc != -EINPROGRESS)
408 up(&dev->cmd_lock);
409}
410
411static void pn533_recv_response(struct urb *urb)
412{
413 struct pn533 *dev = urb->context;
414 struct pn533_frame *in_frame;
415
4849f85e 416 dev->wq_in_frame = NULL;
c46ee386
AAJ
417
418 switch (urb->status) {
419 case 0:
420 /* success */
421 break;
422 case -ECONNRESET:
423 case -ENOENT:
424 case -ESHUTDOWN:
425 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
426 " status: %d", urb->status);
4849f85e
SO
427 dev->wq_in_error = urb->status;
428 goto sched_wq;
c46ee386
AAJ
429 default:
430 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
431 " %d", urb->status);
4849f85e
SO
432 dev->wq_in_error = urb->status;
433 goto sched_wq;
c46ee386
AAJ
434 }
435
436 in_frame = dev->in_urb->transfer_buffer;
437
438 if (!pn533_rx_frame_is_valid(in_frame)) {
439 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
4849f85e
SO
440 dev->wq_in_error = -EIO;
441 goto sched_wq;
c46ee386
AAJ
442 }
443
444 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
445 nfc_dev_err(&dev->interface->dev, "The received frame is not "
446 "response to the last command");
4849f85e
SO
447 dev->wq_in_error = -EIO;
448 goto sched_wq;
c46ee386
AAJ
449 }
450
451 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
4849f85e
SO
452 dev->wq_in_error = 0;
453 dev->wq_in_frame = in_frame;
c46ee386 454
4849f85e
SO
455sched_wq:
456 queue_work(dev->wq, &dev->cmd_work);
c46ee386
AAJ
457}
458
459static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
460{
461 dev->in_urb->complete = pn533_recv_response;
462
463 return usb_submit_urb(dev->in_urb, flags);
464}
465
466static void pn533_recv_ack(struct urb *urb)
467{
468 struct pn533 *dev = urb->context;
469 struct pn533_frame *in_frame;
470 int rc;
471
472 switch (urb->status) {
473 case 0:
474 /* success */
475 break;
476 case -ECONNRESET:
477 case -ENOENT:
478 case -ESHUTDOWN:
479 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
480 " status: %d", urb->status);
4849f85e
SO
481 dev->wq_in_error = urb->status;
482 goto sched_wq;
c46ee386
AAJ
483 default:
484 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
485 " %d", urb->status);
4849f85e
SO
486 dev->wq_in_error = urb->status;
487 goto sched_wq;
c46ee386
AAJ
488 }
489
490 in_frame = dev->in_urb->transfer_buffer;
491
492 if (!pn533_rx_frame_is_ack(in_frame)) {
493 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
4849f85e
SO
494 dev->wq_in_error = -EIO;
495 goto sched_wq;
c46ee386
AAJ
496 }
497
498 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
499
500 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
501 if (rc) {
502 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
503 " result %d", rc);
4849f85e
SO
504 dev->wq_in_error = rc;
505 goto sched_wq;
c46ee386
AAJ
506 }
507
508 return;
509
4849f85e
SO
510sched_wq:
511 dev->wq_in_frame = NULL;
512 queue_work(dev->wq, &dev->cmd_work);
c46ee386
AAJ
513}
514
515static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
516{
517 dev->in_urb->complete = pn533_recv_ack;
518
519 return usb_submit_urb(dev->in_urb, flags);
520}
521
522static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
523{
524 int rc;
525
526 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
527
528 pn533_tx_frame_ack(dev->out_frame);
529
530 dev->out_urb->transfer_buffer = dev->out_frame;
531 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
532 rc = usb_submit_urb(dev->out_urb, flags);
533
534 return rc;
535}
536
537static int __pn533_send_cmd_frame_async(struct pn533 *dev,
538 struct pn533_frame *out_frame,
539 struct pn533_frame *in_frame,
540 int in_frame_len,
541 pn533_cmd_complete_t cmd_complete,
542 void *arg, gfp_t flags)
543{
544 int rc;
545
546 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
547 PN533_FRAME_CMD(out_frame));
548
549 dev->cmd = PN533_FRAME_CMD(out_frame);
550 dev->cmd_complete = cmd_complete;
551 dev->cmd_complete_arg = arg;
552
553 dev->out_urb->transfer_buffer = out_frame;
554 dev->out_urb->transfer_buffer_length =
555 PN533_FRAME_SIZE(out_frame);
556
557 dev->in_urb->transfer_buffer = in_frame;
558 dev->in_urb->transfer_buffer_length = in_frame_len;
559
560 rc = usb_submit_urb(dev->out_urb, flags);
561 if (rc)
562 return rc;
563
564 rc = pn533_submit_urb_for_ack(dev, flags);
565 if (rc)
566 goto error;
567
568 return 0;
569
570error:
571 usb_unlink_urb(dev->out_urb);
572 return rc;
573}
574
575static int pn533_send_cmd_frame_async(struct pn533 *dev,
576 struct pn533_frame *out_frame,
577 struct pn533_frame *in_frame,
578 int in_frame_len,
579 pn533_cmd_complete_t cmd_complete,
580 void *arg, gfp_t flags)
581{
582 int rc;
583
584 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
585
586 if (down_trylock(&dev->cmd_lock))
587 return -EBUSY;
588
589 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
590 in_frame_len, cmd_complete, arg, flags);
591 if (rc)
592 goto error;
593
594 return 0;
595error:
596 up(&dev->cmd_lock);
597 return rc;
598}
599
600struct pn533_sync_cmd_response {
601 int rc;
602 struct completion done;
603};
604
605static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
606 u8 *params, int params_len)
607{
608 struct pn533_sync_cmd_response *arg = _arg;
609
610 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
611
612 arg->rc = 0;
613
614 if (params_len < 0) /* error */
615 arg->rc = params_len;
616
617 complete(&arg->done);
618
619 return 0;
620}
621
622static int pn533_send_cmd_frame_sync(struct pn533 *dev,
623 struct pn533_frame *out_frame,
624 struct pn533_frame *in_frame,
625 int in_frame_len)
626{
627 int rc;
628 struct pn533_sync_cmd_response arg;
629
630 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
631
632 init_completion(&arg.done);
633
634 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
635 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
636 if (rc)
637 return rc;
638
639 wait_for_completion(&arg.done);
640
641 return arg.rc;
642}
643
644static void pn533_send_complete(struct urb *urb)
645{
646 struct pn533 *dev = urb->context;
647
648 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
649
650 switch (urb->status) {
651 case 0:
652 /* success */
653 break;
654 case -ECONNRESET:
655 case -ENOENT:
656 case -ESHUTDOWN:
657 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
658 " status: %d", urb->status);
659 break;
660 default:
661 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
662 " %d", urb->status);
663 }
664}
665
666struct pn533_target_type_a {
667 __be16 sens_res;
668 u8 sel_res;
669 u8 nfcid_len;
670 u8 nfcid_data[];
671} __packed;
672
673
674#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
675#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
676#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
677
678#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
679#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
680
681#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
682#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
683
684#define PN533_TYPE_A_SEL_PROT_MIFARE 0
685#define PN533_TYPE_A_SEL_PROT_ISO14443 1
686#define PN533_TYPE_A_SEL_PROT_DEP 2
687#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
688
689static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
690 int target_data_len)
691{
692 u8 ssd;
693 u8 platconf;
694
695 if (target_data_len < sizeof(struct pn533_target_type_a))
696 return false;
697
698 /* The lenght check of nfcid[] and ats[] are not being performed because
699 the values are not being used */
700
701 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
702 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
703 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
704
705 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
706 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
707 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
708 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
709 return false;
710
711 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
712 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
713 return false;
714
715 return true;
716}
717
718static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
719 int tgt_data_len)
720{
721 struct pn533_target_type_a *tgt_type_a;
722
723 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
724
725 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
726 return -EPROTO;
727
728 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
729 case PN533_TYPE_A_SEL_PROT_MIFARE:
730 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
731 break;
732 case PN533_TYPE_A_SEL_PROT_ISO14443:
733 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
734 break;
735 case PN533_TYPE_A_SEL_PROT_DEP:
736 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
737 break;
738 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
739 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
740 NFC_PROTO_NFC_DEP_MASK;
741 break;
742 }
743
744 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
745 nfc_tgt->sel_res = tgt_type_a->sel_res;
c3b1e1e8
SO
746 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
747 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
c46ee386
AAJ
748
749 return 0;
750}
751
752struct pn533_target_felica {
753 u8 pol_res;
754 u8 opcode;
755 u8 nfcid2[8];
756 u8 pad[8];
757 /* optional */
758 u8 syst_code[];
759} __packed;
760
761#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
762#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
763
764static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
765 int target_data_len)
766{
767 if (target_data_len < sizeof(struct pn533_target_felica))
768 return false;
769
770 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
771 return false;
772
773 return true;
774}
775
776static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
777 int tgt_data_len)
778{
779 struct pn533_target_felica *tgt_felica;
780
781 tgt_felica = (struct pn533_target_felica *) tgt_data;
782
783 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
784 return -EPROTO;
785
786 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
787 tgt_felica->nfcid2[1] ==
788 PN533_FELICA_SENSF_NFCID2_DEP_B2)
789 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
790 else
791 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
792
7975754f
SO
793 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
794 nfc_tgt->sensf_res_len = 9;
795
c46ee386
AAJ
796 return 0;
797}
798
799struct pn533_target_jewel {
800 __be16 sens_res;
801 u8 jewelid[4];
802} __packed;
803
804static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
805 int target_data_len)
806{
807 u8 ssd;
808 u8 platconf;
809
810 if (target_data_len < sizeof(struct pn533_target_jewel))
811 return false;
812
813 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
814 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
815 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
816
817 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
818 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
819 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
820 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
821 return false;
822
823 return true;
824}
825
826static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
827 int tgt_data_len)
828{
829 struct pn533_target_jewel *tgt_jewel;
830
831 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
832
833 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
834 return -EPROTO;
835
836 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
837 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
d8dc1072
SO
838 nfc_tgt->nfcid1_len = 4;
839 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
c46ee386
AAJ
840
841 return 0;
842}
843
844struct pn533_type_b_prot_info {
845 u8 bitrate;
846 u8 fsci_type;
847 u8 fwi_adc_fo;
848} __packed;
849
850#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
851#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
852#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
853
854struct pn533_type_b_sens_res {
855 u8 opcode;
856 u8 nfcid[4];
857 u8 appdata[4];
858 struct pn533_type_b_prot_info prot_info;
859} __packed;
860
861#define PN533_TYPE_B_OPC_SENSB_RES 0x50
862
863struct pn533_target_type_b {
864 struct pn533_type_b_sens_res sensb_res;
865 u8 attrib_res_len;
866 u8 attrib_res[];
867} __packed;
868
869static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
870 int target_data_len)
871{
872 if (target_data_len < sizeof(struct pn533_target_type_b))
873 return false;
874
875 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
876 return false;
877
878 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
879 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
880 return false;
881
882 return true;
883}
884
885static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
886 int tgt_data_len)
887{
888 struct pn533_target_type_b *tgt_type_b;
889
890 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
891
892 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
893 return -EPROTO;
894
895 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
896
897 return 0;
898}
899
900struct pn533_poll_response {
901 u8 nbtg;
902 u8 tg;
903 u8 target_data[];
904} __packed;
905
906static int pn533_target_found(struct pn533 *dev,
907 struct pn533_poll_response *resp, int resp_len)
908{
909 int target_data_len;
910 struct nfc_target nfc_tgt;
911 int rc;
912
913 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
914 dev->poll_mod_curr);
915
916 if (resp->tg != 1)
917 return -EPROTO;
918
98b3ac1b
SO
919 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
920
c46ee386
AAJ
921 target_data_len = resp_len - sizeof(struct pn533_poll_response);
922
923 switch (dev->poll_mod_curr) {
924 case PN533_POLL_MOD_106KBPS_A:
925 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
926 target_data_len);
927 break;
928 case PN533_POLL_MOD_212KBPS_FELICA:
929 case PN533_POLL_MOD_424KBPS_FELICA:
930 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
931 target_data_len);
932 break;
933 case PN533_POLL_MOD_106KBPS_JEWEL:
934 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
935 target_data_len);
936 break;
937 case PN533_POLL_MOD_847KBPS_B:
938 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
939 target_data_len);
940 break;
941 default:
942 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
943 " modulation");
944 return -EPROTO;
945 }
946
947 if (rc)
948 return rc;
949
950 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
951 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
952 " have the desired protocol");
953 return -EAGAIN;
954 }
955
956 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
957 "0x%x", nfc_tgt.supported_protocols);
958
959 dev->tgt_available_prots = nfc_tgt.supported_protocols;
960
961 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
962
963 return 0;
964}
965
966static void pn533_poll_reset_mod_list(struct pn533 *dev)
967{
968 dev->poll_mod_count = 0;
969}
970
971static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
972{
973 dev->poll_mod_active[dev->poll_mod_count] =
974 (struct pn533_poll_modulations *) &poll_mod[mod_index];
975 dev->poll_mod_count++;
976}
977
978static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
979{
980 pn533_poll_reset_mod_list(dev);
981
982 if (protocols & NFC_PROTO_MIFARE_MASK
983 || protocols & NFC_PROTO_ISO14443_MASK
984 || protocols & NFC_PROTO_NFC_DEP_MASK)
985 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
986
987 if (protocols & NFC_PROTO_FELICA_MASK
988 || protocols & NFC_PROTO_NFC_DEP_MASK) {
989 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
990 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
991 }
992
993 if (protocols & NFC_PROTO_JEWEL_MASK)
994 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
995
996 if (protocols & NFC_PROTO_ISO14443_MASK)
997 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
998}
999
1000static void pn533_start_poll_frame(struct pn533_frame *frame,
1001 struct pn533_poll_modulations *mod)
1002{
1003
1004 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1005
1006 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1007 frame->datalen += mod->len;
1008
1009 pn533_tx_frame_finish(frame);
1010}
1011
1012static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
1013 u8 *params, int params_len)
1014{
1015 struct pn533_poll_response *resp;
1016 struct pn533_poll_modulations *next_mod;
1017 int rc;
1018
1019 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1020
1021 if (params_len == -ENOENT) {
1022 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
1023 " stopped");
1024 goto stop_poll;
1025 }
1026
1027 if (params_len < 0) {
1028 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
1029 params_len);
1030 goto stop_poll;
1031 }
1032
1033 resp = (struct pn533_poll_response *) params;
1034 if (resp->nbtg) {
1035 rc = pn533_target_found(dev, resp, params_len);
1036
1037 /* We must stop the poll after a valid target found */
1038 if (rc == 0)
1039 goto stop_poll;
1040
1041 if (rc != -EAGAIN)
1042 nfc_dev_err(&dev->interface->dev, "The target found is"
1043 " not valid - continuing to poll");
1044 }
1045
1046 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1047
1048 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1049
1050 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1051 dev->poll_mod_curr);
1052
1053 pn533_start_poll_frame(dev->out_frame, next_mod);
1054
1055 /* Don't need to down the semaphore again */
1056 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1057 dev->in_maxlen, pn533_start_poll_complete,
1058 NULL, GFP_ATOMIC);
1059
1060 if (rc == -EPERM) {
1061 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1062 " because poll has been stopped");
1063 goto stop_poll;
1064 }
1065
1066 if (rc) {
1067 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1068 " next modulation", rc);
1069 goto stop_poll;
1070 }
1071
1072 /* Inform caller function to do not up the semaphore */
1073 return -EINPROGRESS;
1074
1075stop_poll:
1076 pn533_poll_reset_mod_list(dev);
1077 dev->poll_protocols = 0;
1078 return 0;
1079}
1080
1081static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
1082{
1083 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1084 struct pn533_poll_modulations *start_mod;
1085 int rc;
1086
1087 nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
1088 protocols);
1089
1090 if (dev->poll_mod_count) {
1091 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1092 " active");
1093 return -EBUSY;
1094 }
1095
1096 if (dev->tgt_active_prot) {
1097 nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
1098 " already activated");
1099 return -EBUSY;
1100 }
1101
1102 pn533_poll_create_mod_list(dev, protocols);
1103
1104 if (!dev->poll_mod_count) {
1105 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1106 " specified");
1107 rc = -EINVAL;
1108 goto error;
1109 }
1110
1111 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1112 dev->poll_mod_count);
1113
1114 dev->poll_mod_curr = 0;
1115 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1116
1117 pn533_start_poll_frame(dev->out_frame, start_mod);
1118
1119 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1120 dev->in_maxlen, pn533_start_poll_complete,
1121 NULL, GFP_KERNEL);
1122
1123 if (rc) {
1124 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1125 " start poll", rc);
1126 goto error;
1127 }
1128
1129 dev->poll_protocols = protocols;
1130
1131 return 0;
1132
1133error:
1134 pn533_poll_reset_mod_list(dev);
1135 return rc;
1136}
1137
1138static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1139{
1140 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1141
1142 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1143
1144 if (!dev->poll_mod_count) {
1145 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1146 " running");
1147 return;
1148 }
1149
1150 /* An ack will cancel the last issued command (poll) */
1151 pn533_send_ack(dev, GFP_KERNEL);
1152
1153 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1154 usb_kill_urb(dev->in_urb);
1155}
1156
1157static int pn533_activate_target_nfcdep(struct pn533 *dev)
1158{
1159 struct pn533_cmd_activate_param param;
1160 struct pn533_cmd_activate_response *resp;
541d920b 1161 u16 gt_len;
c46ee386
AAJ
1162 int rc;
1163
1164 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1165
1166 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1167
1168 param.tg = 1;
1169 param.next = 0;
1170 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1171 sizeof(struct pn533_cmd_activate_param));
1172 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1173
1174 pn533_tx_frame_finish(dev->out_frame);
1175
1176 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1177 dev->in_maxlen);
1178 if (rc)
1179 return rc;
1180
1181 resp = (struct pn533_cmd_activate_response *)
1182 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1183 rc = resp->status & PN533_CMD_RET_MASK;
1184 if (rc != PN533_CMD_RET_SUCCESS)
1185 return -EIO;
1186
541d920b
SO
1187 /* ATR_RES general bytes are located at offset 16 */
1188 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1189 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1190
1191 return rc;
c46ee386
AAJ
1192}
1193
1194static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
1195 u32 protocol)
1196{
1197 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1198 int rc;
1199
1200 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1201 protocol);
1202
1203 if (dev->poll_mod_count) {
1204 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1205 " polling");
1206 return -EBUSY;
1207 }
1208
1209 if (dev->tgt_active_prot) {
1210 nfc_dev_err(&dev->interface->dev, "There is already an active"
1211 " target");
1212 return -EBUSY;
1213 }
1214
1215 if (!dev->tgt_available_prots) {
1216 nfc_dev_err(&dev->interface->dev, "There is no available target"
1217 " to activate");
1218 return -EINVAL;
1219 }
1220
1221 if (!(dev->tgt_available_prots & (1 << protocol))) {
1222 nfc_dev_err(&dev->interface->dev, "The target does not support"
1223 " the requested protocol %u", protocol);
1224 return -EINVAL;
1225 }
1226
1227 if (protocol == NFC_PROTO_NFC_DEP) {
1228 rc = pn533_activate_target_nfcdep(dev);
1229 if (rc) {
1230 nfc_dev_err(&dev->interface->dev, "Error %d when"
1231 " activating target with"
1232 " NFC_DEP protocol", rc);
1233 return rc;
1234 }
1235 }
1236
1237 dev->tgt_active_prot = protocol;
1238 dev->tgt_available_prots = 0;
1239
1240 return 0;
1241}
1242
1243static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
1244{
1245 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1246 u8 tg;
1247 u8 status;
1248 int rc;
1249
1250 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1251
1252 if (!dev->tgt_active_prot) {
1253 nfc_dev_err(&dev->interface->dev, "There is no active target");
1254 return;
1255 }
1256
1257 dev->tgt_active_prot = 0;
1258
1259 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1260
1261 tg = 1;
1262 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1263 dev->out_frame->datalen += sizeof(u8);
1264
1265 pn533_tx_frame_finish(dev->out_frame);
1266
1267 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1268 dev->in_maxlen);
1269 if (rc) {
1270 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1271 " command to the controller");
1272 return;
1273 }
1274
1275 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1276 rc = status & PN533_CMD_RET_MASK;
1277 if (rc != PN533_CMD_RET_SUCCESS)
1278 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1279 " the target", rc);
1280
1281 return;
1282}
1283
361f3cb7
SO
1284
1285static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1286 u8 *params, int params_len)
1287{
1288 struct pn533_cmd_jump_dep *cmd;
1289 struct pn533_cmd_jump_dep_response *resp;
1290 struct nfc_target nfc_target;
1291 u8 target_gt_len;
1292 int rc;
1293
1294 if (params_len == -ENOENT) {
1295 nfc_dev_dbg(&dev->interface->dev, "");
1296 return 0;
1297 }
1298
1299 if (params_len < 0) {
1300 nfc_dev_err(&dev->interface->dev,
1301 "Error %d when bringing DEP link up",
1302 params_len);
1303 return 0;
1304 }
1305
1306 if (dev->tgt_available_prots &&
1307 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1308 nfc_dev_err(&dev->interface->dev,
1309 "The target does not support DEP");
1310 return -EINVAL;
1311 }
1312
1313 resp = (struct pn533_cmd_jump_dep_response *) params;
1314 cmd = (struct pn533_cmd_jump_dep *) arg;
1315 rc = resp->status & PN533_CMD_RET_MASK;
1316 if (rc != PN533_CMD_RET_SUCCESS) {
1317 nfc_dev_err(&dev->interface->dev,
1318 "Bringing DEP link up failed %d", rc);
1319 return 0;
1320 }
1321
1322 if (!dev->tgt_available_prots) {
1323 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1324
1325 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2fbabfa4
SO
1326 nfc_target.nfcid1_len = 10;
1327 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
361f3cb7
SO
1328 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1329 if (rc)
1330 return 0;
1331
1332 dev->tgt_available_prots = 0;
1333 }
1334
1335 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1336
1337 /* ATR_RES general bytes are located at offset 17 */
1338 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1339 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1340 resp->gt, target_gt_len);
1341 if (rc == 0)
1342 rc = nfc_dep_link_is_up(dev->nfc_dev,
1343 dev->nfc_dev->targets[0].idx,
1344 !cmd->active, NFC_RF_INITIATOR);
1345
1346 return 0;
1347}
1348
1349static int pn533_dep_link_up(struct nfc_dev *nfc_dev, int target_idx,
47807d3d 1350 u8 comm_mode, u8* gb, size_t gb_len)
361f3cb7
SO
1351{
1352 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1353 struct pn533_cmd_jump_dep *cmd;
47807d3d 1354 u8 cmd_len;
361f3cb7
SO
1355 int rc;
1356
1357 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1358
361f3cb7
SO
1359 if (dev->poll_mod_count) {
1360 nfc_dev_err(&dev->interface->dev,
1361 "Cannot bring the DEP link up while polling");
1362 return -EBUSY;
1363 }
1364
1365 if (dev->tgt_active_prot) {
1366 nfc_dev_err(&dev->interface->dev,
1367 "There is already an active target");
1368 return -EBUSY;
1369 }
1370
47807d3d 1371 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
361f3cb7
SO
1372 cmd = kzalloc(cmd_len, GFP_KERNEL);
1373 if (cmd == NULL)
1374 return -ENOMEM;
1375
1376 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1377
1378 cmd->active = !comm_mode;
1379 cmd->baud = 0;
47807d3d 1380 if (gb != NULL && gb_len > 0) {
361f3cb7 1381 cmd->next = 4; /* We have some Gi */
47807d3d 1382 memcpy(cmd->gt, gb, gb_len);
361f3cb7
SO
1383 } else {
1384 cmd->next = 0;
1385 }
1386
1387 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1388 dev->out_frame->datalen += cmd_len;
1389
1390 pn533_tx_frame_finish(dev->out_frame);
1391
1392 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1393 dev->in_maxlen, pn533_in_dep_link_up_complete,
1394 cmd, GFP_KERNEL);
1395 if (rc)
1396 goto out;
1397
1398
1399out:
1400 kfree(cmd);
1401
1402 return rc;
1403}
1404
1405static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1406{
1407 pn533_deactivate_target(nfc_dev, 0);
1408
1409 return 0;
1410}
1411
c46ee386
AAJ
1412#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1413#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1414
1415static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1416{
1417 int payload_len = skb->len;
1418 struct pn533_frame *out_frame;
c46ee386
AAJ
1419 u8 tg;
1420
1421 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1422 payload_len);
1423
1424 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1425 /* TODO: Implement support to multi-part data exchange */
1426 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1427 " max allowed: %d",
1428 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1429 return -ENOSYS;
1430 }
1431
c46ee386
AAJ
1432 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1433 out_frame = (struct pn533_frame *) skb->data;
1434
1435 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1436
1437 tg = 1;
1438 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1439 out_frame->datalen += sizeof(u8);
1440
1441 /* The data is already in the out_frame, just update the datalen */
1442 out_frame->datalen += payload_len;
1443
1444 pn533_tx_frame_finish(out_frame);
1445 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1446
1447 return 0;
1448}
1449
1450struct pn533_data_exchange_arg {
1451 struct sk_buff *skb_resp;
1452 struct sk_buff *skb_out;
1453 data_exchange_cb_t cb;
1454 void *cb_context;
1455};
1456
1457static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1458 u8 *params, int params_len)
1459{
1460 struct pn533_data_exchange_arg *arg = _arg;
1461 struct sk_buff *skb_resp = arg->skb_resp;
1462 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1463 int err = 0;
1464 u8 status;
1465 u8 cmd_ret;
1466
1467 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1468
1469 dev_kfree_skb_irq(arg->skb_out);
1470
1471 if (params_len < 0) { /* error */
1472 err = params_len;
1473 goto error;
1474 }
1475
1476 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1477
1478 status = params[0];
1479
1480 cmd_ret = status & PN533_CMD_RET_MASK;
1481 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1482 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1483 " exchanging data", cmd_ret);
1484 err = -EIO;
1485 goto error;
1486 }
1487
1488 if (status & PN533_CMD_MI_MASK) {
1489 /* TODO: Implement support to multi-part data exchange */
1490 nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
1491 " supported");
1492 /* Prevent the other messages from controller */
1493 pn533_send_ack(dev, GFP_ATOMIC);
1494 err = -ENOSYS;
1495 goto error;
1496 }
1497
1498 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1499 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1500
1501 arg->cb(arg->cb_context, skb_resp, 0);
1502 kfree(arg);
1503 return 0;
1504
1505error:
1506 dev_kfree_skb_irq(skb_resp);
1507 arg->cb(arg->cb_context, NULL, err);
1508 kfree(arg);
1509 return 0;
1510}
1511
85eb28a3 1512static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
c46ee386
AAJ
1513 struct sk_buff *skb,
1514 data_exchange_cb_t cb,
1515 void *cb_context)
1516{
1517 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1518 struct pn533_frame *out_frame, *in_frame;
1519 struct pn533_data_exchange_arg *arg;
1520 struct sk_buff *skb_resp;
1521 int skb_resp_len;
1522 int rc;
1523
1524 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1525
1526 if (!dev->tgt_active_prot) {
1527 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1528 " there is no active target");
1529 rc = -EINVAL;
1530 goto error;
1531 }
1532
1533 rc = pn533_data_exchange_tx_frame(dev, skb);
1534 if (rc)
1535 goto error;
1536
1537 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1538 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1539 PN533_FRAME_TAIL_SIZE;
1540
7c7cd3bf 1541 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
c46ee386
AAJ
1542 if (!skb_resp) {
1543 rc = -ENOMEM;
1544 goto error;
1545 }
1546
1547 in_frame = (struct pn533_frame *) skb_resp->data;
1548 out_frame = (struct pn533_frame *) skb->data;
1549
1550 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1551 if (!arg) {
1552 rc = -ENOMEM;
1553 goto free_skb_resp;
1554 }
1555
1556 arg->skb_resp = skb_resp;
1557 arg->skb_out = skb;
1558 arg->cb = cb;
1559 arg->cb_context = cb_context;
1560
1561 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1562 pn533_data_exchange_complete, arg,
1563 GFP_KERNEL);
1564 if (rc) {
1565 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1566 " perform data_exchange", rc);
1567 goto free_arg;
1568 }
1569
1570 return 0;
1571
1572free_arg:
1573 kfree(arg);
1574free_skb_resp:
1575 kfree_skb(skb_resp);
1576error:
1577 kfree_skb(skb);
1578 return rc;
1579}
1580
1581static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1582 u8 cfgdata_len)
1583{
1584 int rc;
1585 u8 *params;
1586
1587 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1588
1589 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1590
1591 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1592 params[0] = cfgitem;
1593 memcpy(&params[1], cfgdata, cfgdata_len);
1594 dev->out_frame->datalen += (1 + cfgdata_len);
1595
1596 pn533_tx_frame_finish(dev->out_frame);
1597
1598 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1599 dev->in_maxlen);
1600
1601 return rc;
1602}
1603
1604struct nfc_ops pn533_nfc_ops = {
8b3fe7b5
IE
1605 .dev_up = NULL,
1606 .dev_down = NULL,
361f3cb7
SO
1607 .dep_link_up = pn533_dep_link_up,
1608 .dep_link_down = pn533_dep_link_down,
c46ee386
AAJ
1609 .start_poll = pn533_start_poll,
1610 .stop_poll = pn533_stop_poll,
1611 .activate_target = pn533_activate_target,
1612 .deactivate_target = pn533_deactivate_target,
1613 .data_exchange = pn533_data_exchange,
1614};
1615
1616static int pn533_probe(struct usb_interface *interface,
1617 const struct usb_device_id *id)
1618{
1619 struct pn533_fw_version *fw_ver;
1620 struct pn533 *dev;
1621 struct usb_host_interface *iface_desc;
1622 struct usb_endpoint_descriptor *endpoint;
1623 struct pn533_config_max_retries max_retries;
1624 int in_endpoint = 0;
1625 int out_endpoint = 0;
1626 int rc = -ENOMEM;
1627 int i;
1628 u32 protocols;
1629
1630 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1631 if (!dev)
1632 return -ENOMEM;
1633
1634 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1635 dev->interface = interface;
1636 sema_init(&dev->cmd_lock, 1);
1637
1638 iface_desc = interface->cur_altsetting;
1639 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1640 endpoint = &iface_desc->endpoint[i].desc;
1641
1642 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1643 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1644 in_endpoint = endpoint->bEndpointAddress;
1645 }
1646
1647 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1648 dev->out_maxlen =
1649 le16_to_cpu(endpoint->wMaxPacketSize);
1650 out_endpoint = endpoint->bEndpointAddress;
1651 }
1652 }
1653
1654 if (!in_endpoint || !out_endpoint) {
1655 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1656 " bulk-out endpoint");
1657 rc = -ENODEV;
1658 goto error;
1659 }
1660
1661 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1662 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1663 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1664 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1665
1666 if (!dev->in_frame || !dev->out_frame ||
1667 !dev->in_urb || !dev->out_urb)
1668 goto error;
1669
1670 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1671 usb_rcvbulkpipe(dev->udev, in_endpoint),
1672 NULL, 0, NULL, dev);
1673 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1674 usb_sndbulkpipe(dev->udev, out_endpoint),
1675 NULL, 0,
1676 pn533_send_complete, dev);
1677
4849f85e
SO
1678 INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
1679 dev->wq = create_singlethread_workqueue("pn533");
1680 if (dev->wq == NULL)
1681 goto error;
c46ee386
AAJ
1682
1683 usb_set_intfdata(interface, dev);
1684
1685 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
1686 pn533_tx_frame_finish(dev->out_frame);
1687
1688 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1689 dev->in_maxlen);
1690 if (rc)
4849f85e 1691 goto destroy_wq;
c46ee386
AAJ
1692
1693 fw_ver = (struct pn533_fw_version *)
1694 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1695 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
1696 " attached", fw_ver->ver, fw_ver->rev);
1697
1698 protocols = NFC_PROTO_JEWEL_MASK
1699 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
1700 | NFC_PROTO_ISO14443_MASK
1701 | NFC_PROTO_NFC_DEP_MASK;
1702
e8753043
SO
1703 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
1704 PN533_CMD_DATAEXCH_HEAD_LEN,
1705 PN533_FRAME_TAIL_SIZE);
c46ee386 1706 if (!dev->nfc_dev)
4849f85e 1707 goto destroy_wq;
c46ee386
AAJ
1708
1709 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
1710 nfc_set_drvdata(dev->nfc_dev, dev);
1711
1712 rc = nfc_register_device(dev->nfc_dev);
1713 if (rc)
1714 goto free_nfc_dev;
1715
1716 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
1717 max_retries.mx_rty_psl = 2;
1718 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
1719
1720 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
1721 (u8 *) &max_retries, sizeof(max_retries));
1722
1723 if (rc) {
1724 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
1725 " config");
1726 goto free_nfc_dev;
1727 }
1728
1729 return 0;
1730
1731free_nfc_dev:
1732 nfc_free_device(dev->nfc_dev);
4849f85e
SO
1733destroy_wq:
1734 destroy_workqueue(dev->wq);
c46ee386
AAJ
1735error:
1736 kfree(dev->in_frame);
1737 usb_free_urb(dev->in_urb);
1738 kfree(dev->out_frame);
1739 usb_free_urb(dev->out_urb);
1740 kfree(dev);
1741 return rc;
1742}
1743
1744static void pn533_disconnect(struct usb_interface *interface)
1745{
1746 struct pn533 *dev;
1747
1748 dev = usb_get_intfdata(interface);
1749 usb_set_intfdata(interface, NULL);
1750
1751 nfc_unregister_device(dev->nfc_dev);
1752 nfc_free_device(dev->nfc_dev);
1753
1754 usb_kill_urb(dev->in_urb);
1755 usb_kill_urb(dev->out_urb);
1756
4849f85e 1757 destroy_workqueue(dev->wq);
c46ee386
AAJ
1758
1759 kfree(dev->in_frame);
1760 usb_free_urb(dev->in_urb);
1761 kfree(dev->out_frame);
1762 usb_free_urb(dev->out_urb);
1763 kfree(dev);
1764
276556db 1765 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
c46ee386
AAJ
1766}
1767
1768static struct usb_driver pn533_driver = {
1769 .name = "pn533",
1770 .probe = pn533_probe,
1771 .disconnect = pn533_disconnect,
1772 .id_table = pn533_table,
1773};
1774
fe748483 1775module_usb_driver(pn533_driver);
c46ee386
AAJ
1776
1777MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1778 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1779MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
1780MODULE_VERSION(VERSION);
1781MODULE_LICENSE("GPL");
This page took 0.149009 seconds and 5 git commands to generate.