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