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