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