blob: d9763440b8e968498aeb6eeb523b531e7b7edb60 [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
Samuel Ortiz103b34c2012-05-31 00:07:51 +020078#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +020079#define PN533_CMD_TG_SET_DATA 0x8e
Samuel Ortizad3823c2012-05-30 23:54:55 +020080
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081#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
Samuel Ortiz103b34c2012-05-31 00:07:51 +020088/* PN533 status codes */
89#define PN533_STATUS_TARGET_RELEASED 0x29
90
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030091struct 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 */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200107#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300108#define PN533_CFGITEM_MAX_RETRIES 0x05
109
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200110#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
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300115#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
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200124struct pn533_config_timing {
125 u8 rfu;
126 u8 atr_res_timeout;
127 u8 dep_timeout;
128} __packed;
129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300130/* 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
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100254/* PN533_CMD_IN_JUMP_FOR_DEP */
255struct pn533_cmd_jump_dep {
256 u8 active;
257 u8 baud;
258 u8 next;
259 u8 gt[];
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;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300274
Samuel Ortizad3823c2012-05-30 23:54:55 +0200275
276/* PN533_TG_INIT_AS_TARGET */
277#define PN533_INIT_TARGET_PASSIVE 0x1
278#define PN533_INIT_TARGET_DEP 0x2
279
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200280#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
Samuel Ortizad3823c2012-05-30 23:54:55 +0200284struct 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
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300298struct 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
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200311 struct sk_buff_head resp_q;
312
Samuel Ortiz4849f852012-04-10 19:43:17 +0200313 struct workqueue_struct *wq;
314 struct work_struct cmd_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200315 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200316 struct work_struct tg_work;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200317 struct pn533_frame *wq_in_frame;
318 int wq_in_error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319
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
Samuel Ortiz4849f852012-04-10 19:43:17 +0200433
434static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300435{
Samuel Ortiz4849f852012-04-10 19:43:17 +0200436 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
437 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300438 int rc;
439
Samuel Ortiz4849f852012-04-10 19:43:17 +0200440 in_frame = dev->wq_in_frame;
441
442 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300443 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200444 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300445 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
Samuel Ortiz4849f852012-04-10 19:43:17 +0200459 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300460
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);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200470 dev->wq_in_error = urb->status;
471 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300472 default:
473 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
474 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200475 dev->wq_in_error = urb->status;
476 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300477 }
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");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200483 dev->wq_in_error = -EIO;
484 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300485 }
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");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200490 dev->wq_in_error = -EIO;
491 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300492 }
493
494 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200495 dev->wq_in_error = 0;
496 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300497
Samuel Ortiz4849f852012-04-10 19:43:17 +0200498sched_wq:
499 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300500}
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);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200524 dev->wq_in_error = urb->status;
525 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300526 default:
527 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
528 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200529 dev->wq_in_error = urb->status;
530 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300531 }
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");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200537 dev->wq_in_error = -EIO;
538 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300539 }
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);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200547 dev->wq_in_error = rc;
548 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300549 }
550
551 return;
552
Samuel Ortiz4849f852012-04-10 19:43:17 +0200553sched_wq:
554 dev->wq_in_frame = NULL;
555 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300556}
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;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +0100789 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
790 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300791
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
Samuel Ortiz79757542012-03-05 01:03:45 +0100836 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
837 nfc_tgt->sensf_res_len = 9;
838
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300839 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);
Samuel Ortizd8dc1072012-03-05 01:03:46 +0100881 nfc_tgt->nfcid1_len = 4;
882 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300883
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
Samuel Ortiz98b3ac12012-03-05 01:03:39 +0100962 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
963
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300964 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
Samuel Ortizad3823c2012-05-30 23:54:55 +02001124static 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
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001153#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
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001215#define ATR_REQ_GB_OFFSET 17
Samuel Ortizad3823c2012-05-30 23:54:55 +02001216static int pn533_init_target_complete(struct pn533 *dev, void *arg,
1217 u8 *params, int params_len)
1218{
1219 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001220 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1221 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001222 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001223
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
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001234 if (params_len < ATR_REQ_GB_OFFSET + 1)
1235 return -EINVAL;
1236
Samuel Ortizad3823c2012-05-30 23:54:55 +02001237 resp = (struct pn533_cmd_init_target_response *) params;
1238
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001239 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1240 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001241
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001242 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
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001253 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;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001264}
1265
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001266static int pn533_init_target(struct nfc_dev *nfc_dev, u32 protocols)
1267{
Samuel Ortizad3823c2012-05-30 23:54:55 +02001268 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;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001295}
1296
1297static int pn533_start_im_poll(struct nfc_dev *nfc_dev, u32 protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001298{
1299 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1300 struct pn533_poll_modulations *start_mod;
1301 int rc;
1302
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001303 if (dev->poll_mod_count) {
1304 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1305 " active");
1306 return -EBUSY;
1307 }
1308
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309 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
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001345static 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
Samuel Ortizad3823c2012-05-30 23:54:55 +02001360 if (im_protocols)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001361 return pn533_start_im_poll(nfc_dev, im_protocols);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001362
1363 if (tm_protocols)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001364 return pn533_init_target(nfc_dev, tm_protocols);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001365
1366 return -EINVAL;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001367}
1368
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001369static 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);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001386
1387 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001388}
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;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001394 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001395 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
Samuel Ortiz541d9202011-12-14 16:43:10 +01001420 /* 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;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001425}
1426
Eric Lapuyade90099432012-05-07 12:31:13 +02001427static int pn533_activate_target(struct nfc_dev *nfc_dev,
1428 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001429{
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
Eric Lapuyade90099432012-05-07 12:31:13 +02001476static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1477 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001478{
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
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001493 skb_queue_purge(&dev->resp_q);
1494
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001495 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
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001520
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;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001562 nfc_target.nfcid1_len = 10;
1563 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001564 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
Eric Lapuyade90099432012-05-07 12:31:13 +02001585static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001586 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001587{
1588 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1589 struct pn533_cmd_jump_dep *cmd;
Samuel Ortiz47807d32012-03-05 01:03:50 +01001590 u8 cmd_len;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001591 int rc;
1592
1593 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1594
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001595 if (dev->poll_mod_count) {
1596 nfc_dev_err(&dev->interface->dev,
1597 "Cannot bring the DEP link up while polling");
1598 return -EBUSY;
1599 }
1600
1601 if (dev->tgt_active_prot) {
1602 nfc_dev_err(&dev->interface->dev,
1603 "There is already an active target");
1604 return -EBUSY;
1605 }
1606
Samuel Ortiz47807d32012-03-05 01:03:50 +01001607 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001608 cmd = kzalloc(cmd_len, GFP_KERNEL);
1609 if (cmd == NULL)
1610 return -ENOMEM;
1611
1612 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1613
1614 cmd->active = !comm_mode;
1615 cmd->baud = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +01001616 if (gb != NULL && gb_len > 0) {
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001617 cmd->next = 4; /* We have some Gi */
Samuel Ortiz47807d32012-03-05 01:03:50 +01001618 memcpy(cmd->gt, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001619 } else {
1620 cmd->next = 0;
1621 }
1622
1623 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1624 dev->out_frame->datalen += cmd_len;
1625
1626 pn533_tx_frame_finish(dev->out_frame);
1627
1628 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1629 dev->in_maxlen, pn533_in_dep_link_up_complete,
1630 cmd, GFP_KERNEL);
1631 if (rc)
1632 goto out;
1633
1634
1635out:
1636 kfree(cmd);
1637
1638 return rc;
1639}
1640
1641static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1642{
1643 pn533_deactivate_target(nfc_dev, 0);
1644
1645 return 0;
1646}
1647
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001648static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
1649 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001650{
1651 int payload_len = skb->len;
1652 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001653 u8 tg;
1654
1655 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1656 payload_len);
1657
1658 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1659 /* TODO: Implement support to multi-part data exchange */
1660 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1661 " max allowed: %d",
1662 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1663 return -ENOSYS;
1664 }
1665
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001666 if (target == true) {
1667 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1668 out_frame = (struct pn533_frame *) skb->data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001669
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001670 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1671 tg = 1;
1672 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1673 out_frame->datalen += sizeof(u8);
1674 } else {
1675 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1676 out_frame = (struct pn533_frame *) skb->data;
1677 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
1678 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001679
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001680
1681 /* The data is already in the out_frame, just update the datalen */
1682 out_frame->datalen += payload_len;
1683
1684 pn533_tx_frame_finish(out_frame);
1685 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1686
1687 return 0;
1688}
1689
1690struct pn533_data_exchange_arg {
1691 struct sk_buff *skb_resp;
1692 struct sk_buff *skb_out;
1693 data_exchange_cb_t cb;
1694 void *cb_context;
1695};
1696
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001697static struct sk_buff *pn533_build_response(struct pn533 *dev)
1698{
1699 struct sk_buff *skb, *tmp, *t;
1700 unsigned int skb_len = 0, tmp_len = 0;
1701
1702 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1703
1704 if (skb_queue_empty(&dev->resp_q))
1705 return NULL;
1706
1707 if (skb_queue_len(&dev->resp_q) == 1) {
1708 skb = skb_dequeue(&dev->resp_q);
1709 goto out;
1710 }
1711
1712 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1713 skb_len += tmp->len;
1714
1715 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1716 __func__, skb_len);
1717
1718 skb = alloc_skb(skb_len, GFP_KERNEL);
1719 if (skb == NULL)
1720 goto out;
1721
1722 skb_put(skb, skb_len);
1723
1724 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1725 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1726 tmp_len += tmp->len;
1727 }
1728
1729out:
1730 skb_queue_purge(&dev->resp_q);
1731
1732 return skb;
1733}
1734
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001735static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1736 u8 *params, int params_len)
1737{
1738 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001739 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001740 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1741 int err = 0;
1742 u8 status;
1743 u8 cmd_ret;
1744
1745 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1746
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001747 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001748
1749 if (params_len < 0) { /* error */
1750 err = params_len;
1751 goto error;
1752 }
1753
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001754 status = params[0];
1755
1756 cmd_ret = status & PN533_CMD_RET_MASK;
1757 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1758 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1759 " exchanging data", cmd_ret);
1760 err = -EIO;
1761 goto error;
1762 }
1763
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001764 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001765 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1766 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001767 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001768
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001769 if (status & PN533_CMD_MI_MASK) {
1770 queue_work(dev->wq, &dev->mi_work);
1771 return -EINPROGRESS;
1772 }
1773
1774 skb = pn533_build_response(dev);
1775 if (skb == NULL)
1776 goto error;
1777
1778 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001779 kfree(arg);
1780 return 0;
1781
1782error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001783 skb_queue_purge(&dev->resp_q);
1784 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001785 arg->cb(arg->cb_context, NULL, err);
1786 kfree(arg);
1787 return 0;
1788}
1789
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001790static int pn533_transceive(struct nfc_dev *nfc_dev,
1791 struct nfc_target *target, struct sk_buff *skb,
1792 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001793{
1794 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1795 struct pn533_frame *out_frame, *in_frame;
1796 struct pn533_data_exchange_arg *arg;
1797 struct sk_buff *skb_resp;
1798 int skb_resp_len;
1799 int rc;
1800
1801 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1802
1803 if (!dev->tgt_active_prot) {
1804 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1805 " there is no active target");
1806 rc = -EINVAL;
1807 goto error;
1808 }
1809
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001810 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001811 if (rc)
1812 goto error;
1813
1814 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1815 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1816 PN533_FRAME_TAIL_SIZE;
1817
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01001818 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001819 if (!skb_resp) {
1820 rc = -ENOMEM;
1821 goto error;
1822 }
1823
1824 in_frame = (struct pn533_frame *) skb_resp->data;
1825 out_frame = (struct pn533_frame *) skb->data;
1826
1827 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1828 if (!arg) {
1829 rc = -ENOMEM;
1830 goto free_skb_resp;
1831 }
1832
1833 arg->skb_resp = skb_resp;
1834 arg->skb_out = skb;
1835 arg->cb = cb;
1836 arg->cb_context = cb_context;
1837
1838 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1839 pn533_data_exchange_complete, arg,
1840 GFP_KERNEL);
1841 if (rc) {
1842 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1843 " perform data_exchange", rc);
1844 goto free_arg;
1845 }
1846
1847 return 0;
1848
1849free_arg:
1850 kfree(arg);
1851free_skb_resp:
1852 kfree_skb(skb_resp);
1853error:
1854 kfree_skb(skb);
1855 return rc;
1856}
1857
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001858static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1859 u8 *params, int params_len)
1860{
1861 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1862
1863 if (params_len < 0) {
1864 nfc_dev_err(&dev->interface->dev,
1865 "Error %d when sending data",
1866 params_len);
1867
1868 return params_len;
1869 }
1870
1871 if (params_len > 0 && params[0] != 0) {
1872 nfc_tm_deactivated(dev->nfc_dev);
1873
1874 return 0;
1875 }
1876
1877 queue_work(dev->wq, &dev->tg_work);
1878
1879 return 0;
1880}
1881
1882static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1883{
1884 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1885 struct pn533_frame *out_frame;
1886 int rc;
1887
1888 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1889
1890 rc = pn533_build_tx_frame(dev, skb, false);
1891 if (rc)
1892 goto error;
1893
1894 out_frame = (struct pn533_frame *) skb->data;
1895
1896 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
1897 dev->in_maxlen, pn533_tm_send_complete,
1898 NULL, GFP_KERNEL);
1899 if (rc) {
1900 nfc_dev_err(&dev->interface->dev,
1901 "Error %d when trying to send data", rc);
1902 goto error;
1903 }
1904
1905 return 0;
1906
1907error:
1908 kfree_skb(skb);
1909
1910 return rc;
1911}
1912
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001913static void pn533_wq_mi_recv(struct work_struct *work)
1914{
1915 struct pn533 *dev = container_of(work, struct pn533, mi_work);
1916 struct sk_buff *skb_cmd;
1917 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
1918 struct pn533_frame *out_frame, *in_frame;
1919 struct sk_buff *skb_resp;
1920 int skb_resp_len;
1921 int rc;
1922
1923 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1924
1925 /* This is a zero payload size skb */
1926 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
1927 GFP_KERNEL);
1928 if (skb_cmd == NULL)
1929 goto error_cmd;
1930
1931 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
1932
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001933 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001934 if (rc)
1935 goto error_frame;
1936
1937 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1938 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1939 PN533_FRAME_TAIL_SIZE;
1940 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
1941 if (!skb_resp) {
1942 rc = -ENOMEM;
1943 goto error_frame;
1944 }
1945
1946 in_frame = (struct pn533_frame *) skb_resp->data;
1947 out_frame = (struct pn533_frame *) skb_cmd->data;
1948
1949 arg->skb_resp = skb_resp;
1950 arg->skb_out = skb_cmd;
1951
1952 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
1953 skb_resp_len,
1954 pn533_data_exchange_complete,
1955 dev->cmd_complete_arg, GFP_KERNEL);
1956 if (!rc)
1957 return;
1958
1959 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1960 " perform data_exchange", rc);
1961
1962 kfree_skb(skb_resp);
1963
1964error_frame:
1965 kfree_skb(skb_cmd);
1966
1967error_cmd:
1968 pn533_send_ack(dev, GFP_KERNEL);
1969
1970 kfree(arg);
1971
1972 up(&dev->cmd_lock);
1973}
1974
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001975static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1976 u8 cfgdata_len)
1977{
1978 int rc;
1979 u8 *params;
1980
1981 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1982
1983 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1984
1985 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1986 params[0] = cfgitem;
1987 memcpy(&params[1], cfgdata, cfgdata_len);
1988 dev->out_frame->datalen += (1 + cfgdata_len);
1989
1990 pn533_tx_frame_finish(dev->out_frame);
1991
1992 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1993 dev->in_maxlen);
1994
1995 return rc;
1996}
1997
1998struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001999 .dev_up = NULL,
2000 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002001 .dep_link_up = pn533_dep_link_up,
2002 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002003 .start_poll = pn533_start_poll,
2004 .stop_poll = pn533_stop_poll,
2005 .activate_target = pn533_activate_target,
2006 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002007 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002008 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002009};
2010
2011static int pn533_probe(struct usb_interface *interface,
2012 const struct usb_device_id *id)
2013{
2014 struct pn533_fw_version *fw_ver;
2015 struct pn533 *dev;
2016 struct usb_host_interface *iface_desc;
2017 struct usb_endpoint_descriptor *endpoint;
2018 struct pn533_config_max_retries max_retries;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002019 struct pn533_config_timing timing;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002020 int in_endpoint = 0;
2021 int out_endpoint = 0;
2022 int rc = -ENOMEM;
2023 int i;
2024 u32 protocols;
2025
2026 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2027 if (!dev)
2028 return -ENOMEM;
2029
2030 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2031 dev->interface = interface;
2032 sema_init(&dev->cmd_lock, 1);
2033
2034 iface_desc = interface->cur_altsetting;
2035 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2036 endpoint = &iface_desc->endpoint[i].desc;
2037
2038 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2039 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
2040 in_endpoint = endpoint->bEndpointAddress;
2041 }
2042
2043 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
2044 dev->out_maxlen =
2045 le16_to_cpu(endpoint->wMaxPacketSize);
2046 out_endpoint = endpoint->bEndpointAddress;
2047 }
2048 }
2049
2050 if (!in_endpoint || !out_endpoint) {
2051 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2052 " bulk-out endpoint");
2053 rc = -ENODEV;
2054 goto error;
2055 }
2056
2057 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
2058 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2059 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
2060 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2061
2062 if (!dev->in_frame || !dev->out_frame ||
2063 !dev->in_urb || !dev->out_urb)
2064 goto error;
2065
2066 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2067 usb_rcvbulkpipe(dev->udev, in_endpoint),
2068 NULL, 0, NULL, dev);
2069 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2070 usb_sndbulkpipe(dev->udev, out_endpoint),
2071 NULL, 0,
2072 pn533_send_complete, dev);
2073
Samuel Ortiz4849f852012-04-10 19:43:17 +02002074 INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002075 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002076 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002077 dev->wq = alloc_workqueue("pn533",
2078 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
2079 1);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002080 if (dev->wq == NULL)
2081 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002082
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002083 skb_queue_head_init(&dev->resp_q);
2084
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085 usb_set_intfdata(interface, dev);
2086
2087 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2088 pn533_tx_frame_finish(dev->out_frame);
2089
2090 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2091 dev->in_maxlen);
2092 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002093 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002094
2095 fw_ver = (struct pn533_fw_version *)
2096 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2097 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2098 " attached", fw_ver->ver, fw_ver->rev);
2099
2100 protocols = NFC_PROTO_JEWEL_MASK
2101 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
2102 | NFC_PROTO_ISO14443_MASK
2103 | NFC_PROTO_NFC_DEP_MASK;
2104
Samuel Ortize8753042011-08-19 15:47:11 +02002105 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2106 PN533_CMD_DATAEXCH_HEAD_LEN,
2107 PN533_FRAME_TAIL_SIZE);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002109 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002110
2111 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2112 nfc_set_drvdata(dev->nfc_dev, dev);
2113
2114 rc = nfc_register_device(dev->nfc_dev);
2115 if (rc)
2116 goto free_nfc_dev;
2117
2118 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2119 max_retries.mx_rty_psl = 2;
2120 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2121
2122 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2123 (u8 *) &max_retries, sizeof(max_retries));
2124
2125 if (rc) {
2126 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
2127 " config");
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002128 goto unregister_nfc_dev;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002129 }
2130
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002131 timing.rfu = PN533_CONFIG_TIMING_102;
2132 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2133 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2134
2135 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2136 (u8 *) &timing, sizeof(timing));
2137 if (rc) {
2138 nfc_dev_err(&dev->interface->dev,
2139 "Error on setting RF timings");
2140 goto unregister_nfc_dev;
2141 }
2142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002143 return 0;
2144
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002145unregister_nfc_dev:
2146 nfc_unregister_device(dev->nfc_dev);
2147
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002148free_nfc_dev:
2149 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002150
Samuel Ortiz4849f852012-04-10 19:43:17 +02002151destroy_wq:
2152 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002153error:
2154 kfree(dev->in_frame);
2155 usb_free_urb(dev->in_urb);
2156 kfree(dev->out_frame);
2157 usb_free_urb(dev->out_urb);
2158 kfree(dev);
2159 return rc;
2160}
2161
2162static void pn533_disconnect(struct usb_interface *interface)
2163{
2164 struct pn533 *dev;
2165
2166 dev = usb_get_intfdata(interface);
2167 usb_set_intfdata(interface, NULL);
2168
2169 nfc_unregister_device(dev->nfc_dev);
2170 nfc_free_device(dev->nfc_dev);
2171
2172 usb_kill_urb(dev->in_urb);
2173 usb_kill_urb(dev->out_urb);
2174
Samuel Ortiz4849f852012-04-10 19:43:17 +02002175 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002176
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002177 skb_queue_purge(&dev->resp_q);
2178
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002179 kfree(dev->in_frame);
2180 usb_free_urb(dev->in_urb);
2181 kfree(dev->out_frame);
2182 usb_free_urb(dev->out_urb);
2183 kfree(dev);
2184
Dan Carpenter276556d2011-07-08 10:21:15 +03002185 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002186}
2187
2188static struct usb_driver pn533_driver = {
2189 .name = "pn533",
2190 .probe = pn533_probe,
2191 .disconnect = pn533_disconnect,
2192 .id_table = pn533_table,
2193};
2194
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002195module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002196
2197MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2198 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2199MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2200MODULE_VERSION(VERSION);
2201MODULE_LICENSE("GPL");