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