blob: 2a7e4e8e4f11fb9ffc0f5ca9537e080601f5ac11 [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 */
107#define PN533_CFGITEM_MAX_RETRIES 0x05
108
109#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
110#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
111
112struct pn533_config_max_retries {
113 u8 mx_rty_atr;
114 u8 mx_rty_psl;
115 u8 mx_rty_passive_act;
116} __packed;
117
118/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
119
120/* felica commands opcode */
121#define PN533_FELICA_OPC_SENSF_REQ 0
122#define PN533_FELICA_OPC_SENSF_RES 1
123/* felica SENSF_REQ parameters */
124#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
125#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
126#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
127#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
128
129/* type B initiator_data values */
130#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
131#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
132#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
133
134union pn533_cmd_poll_initdata {
135 struct {
136 u8 afi;
137 u8 polling_method;
138 } __packed type_b;
139 struct {
140 u8 opcode;
141 __be16 sc;
142 u8 rc;
143 u8 tsn;
144 } __packed felica;
145};
146
147/* Poll modulations */
148enum {
149 PN533_POLL_MOD_106KBPS_A,
150 PN533_POLL_MOD_212KBPS_FELICA,
151 PN533_POLL_MOD_424KBPS_FELICA,
152 PN533_POLL_MOD_106KBPS_JEWEL,
153 PN533_POLL_MOD_847KBPS_B,
154
155 __PN533_POLL_MOD_AFTER_LAST,
156};
157#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
158
159struct pn533_poll_modulations {
160 struct {
161 u8 maxtg;
162 u8 brty;
163 union pn533_cmd_poll_initdata initiator_data;
164 } __packed data;
165 u8 len;
166};
167
168const struct pn533_poll_modulations poll_mod[] = {
169 [PN533_POLL_MOD_106KBPS_A] = {
170 .data = {
171 .maxtg = 1,
172 .brty = 0,
173 },
174 .len = 2,
175 },
176 [PN533_POLL_MOD_212KBPS_FELICA] = {
177 .data = {
178 .maxtg = 1,
179 .brty = 1,
180 .initiator_data.felica = {
181 .opcode = PN533_FELICA_OPC_SENSF_REQ,
182 .sc = PN533_FELICA_SENSF_SC_ALL,
183 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
184 .tsn = 0,
185 },
186 },
187 .len = 7,
188 },
189 [PN533_POLL_MOD_424KBPS_FELICA] = {
190 .data = {
191 .maxtg = 1,
192 .brty = 2,
193 .initiator_data.felica = {
194 .opcode = PN533_FELICA_OPC_SENSF_REQ,
195 .sc = PN533_FELICA_SENSF_SC_ALL,
196 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
197 .tsn = 0,
198 },
199 },
200 .len = 7,
201 },
202 [PN533_POLL_MOD_106KBPS_JEWEL] = {
203 .data = {
204 .maxtg = 1,
205 .brty = 4,
206 },
207 .len = 2,
208 },
209 [PN533_POLL_MOD_847KBPS_B] = {
210 .data = {
211 .maxtg = 1,
212 .brty = 8,
213 .initiator_data.type_b = {
214 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
215 .polling_method =
216 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
217 },
218 },
219 .len = 3,
220 },
221};
222
223/* PN533_CMD_IN_ATR */
224
225struct pn533_cmd_activate_param {
226 u8 tg;
227 u8 next;
228} __packed;
229
230struct pn533_cmd_activate_response {
231 u8 status;
232 u8 nfcid3t[10];
233 u8 didt;
234 u8 bst;
235 u8 brt;
236 u8 to;
237 u8 ppt;
238 /* optional */
239 u8 gt[];
240} __packed;
241
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100242/* PN533_CMD_IN_JUMP_FOR_DEP */
243struct pn533_cmd_jump_dep {
244 u8 active;
245 u8 baud;
246 u8 next;
247 u8 gt[];
248} __packed;
249
250struct pn533_cmd_jump_dep_response {
251 u8 status;
252 u8 tg;
253 u8 nfcid3t[10];
254 u8 didt;
255 u8 bst;
256 u8 brt;
257 u8 to;
258 u8 ppt;
259 /* optional */
260 u8 gt[];
261} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300262
Samuel Ortizad3823c2012-05-30 23:54:55 +0200263
264/* PN533_TG_INIT_AS_TARGET */
265#define PN533_INIT_TARGET_PASSIVE 0x1
266#define PN533_INIT_TARGET_DEP 0x2
267
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200268#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
269#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
270#define PN533_INIT_TARGET_RESP_DEP 0x4
271
Samuel Ortizad3823c2012-05-30 23:54:55 +0200272struct pn533_cmd_init_target {
273 u8 mode;
274 u8 mifare[6];
275 u8 felica[18];
276 u8 nfcid3[10];
277 u8 gb_len;
278 u8 gb[];
279} __packed;
280
281struct pn533_cmd_init_target_response {
282 u8 mode;
283 u8 cmd[];
284} __packed;
285
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300286struct pn533 {
287 struct usb_device *udev;
288 struct usb_interface *interface;
289 struct nfc_dev *nfc_dev;
290
291 struct urb *out_urb;
292 int out_maxlen;
293 struct pn533_frame *out_frame;
294
295 struct urb *in_urb;
296 int in_maxlen;
297 struct pn533_frame *in_frame;
298
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200299 struct sk_buff_head resp_q;
300
Samuel Ortiz4849f852012-04-10 19:43:17 +0200301 struct workqueue_struct *wq;
302 struct work_struct cmd_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200303 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200304 struct work_struct tg_work;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200305 struct pn533_frame *wq_in_frame;
306 int wq_in_error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300307
308 pn533_cmd_complete_t cmd_complete;
309 void *cmd_complete_arg;
310 struct semaphore cmd_lock;
311 u8 cmd;
312
313 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
314 u8 poll_mod_count;
315 u8 poll_mod_curr;
316 u32 poll_protocols;
317
318 u8 tgt_available_prots;
319 u8 tgt_active_prot;
320};
321
322struct pn533_frame {
323 u8 preamble;
324 __be16 start_frame;
325 u8 datalen;
326 u8 datalen_checksum;
327 u8 data[];
328} __packed;
329
330/* The rule: value + checksum = 0 */
331static inline u8 pn533_checksum(u8 value)
332{
333 return ~value + 1;
334}
335
336/* The rule: sum(data elements) + checksum = 0 */
337static u8 pn533_data_checksum(u8 *data, int datalen)
338{
339 u8 sum = 0;
340 int i;
341
342 for (i = 0; i < datalen; i++)
343 sum += data[i];
344
345 return pn533_checksum(sum);
346}
347
348/**
349 * pn533_tx_frame_ack - create a ack frame
350 * @frame: The frame to be set as ack
351 *
352 * Ack is different type of standard frame. As a standard frame, it has
353 * preamble and start_frame. However the checksum of this frame must fail,
354 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
355 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
356 * After datalen_checksum field, the postamble is placed.
357 */
358static void pn533_tx_frame_ack(struct pn533_frame *frame)
359{
360 frame->preamble = 0;
361 frame->start_frame = cpu_to_be16(PN533_SOF);
362 frame->datalen = 0;
363 frame->datalen_checksum = 0xFF;
364 /* data[0] is used as postamble */
365 frame->data[0] = 0;
366}
367
368static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
369{
370 frame->preamble = 0;
371 frame->start_frame = cpu_to_be16(PN533_SOF);
372 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
373 PN533_FRAME_CMD(frame) = cmd;
374 frame->datalen = 2;
375}
376
377static void pn533_tx_frame_finish(struct pn533_frame *frame)
378{
379 frame->datalen_checksum = pn533_checksum(frame->datalen);
380
381 PN533_FRAME_CHECKSUM(frame) =
382 pn533_data_checksum(frame->data, frame->datalen);
383
384 PN533_FRAME_POSTAMBLE(frame) = 0;
385}
386
387static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
388{
389 u8 checksum;
390
391 if (frame->start_frame != cpu_to_be16(PN533_SOF))
392 return false;
393
394 checksum = pn533_checksum(frame->datalen);
395 if (checksum != frame->datalen_checksum)
396 return false;
397
398 checksum = pn533_data_checksum(frame->data, frame->datalen);
399 if (checksum != PN533_FRAME_CHECKSUM(frame))
400 return false;
401
402 return true;
403}
404
405static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
406{
407 if (frame->start_frame != cpu_to_be16(PN533_SOF))
408 return false;
409
410 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
411 return false;
412
413 return true;
414}
415
416static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
417{
418 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
419}
420
Samuel Ortiz4849f852012-04-10 19:43:17 +0200421
422static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300423{
Samuel Ortiz4849f852012-04-10 19:43:17 +0200424 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
425 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300426 int rc;
427
Samuel Ortiz4849f852012-04-10 19:43:17 +0200428 in_frame = dev->wq_in_frame;
429
430 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300431 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200432 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300433 else
434 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
435 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
436 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
437
438 if (rc != -EINPROGRESS)
439 up(&dev->cmd_lock);
440}
441
442static void pn533_recv_response(struct urb *urb)
443{
444 struct pn533 *dev = urb->context;
445 struct pn533_frame *in_frame;
446
Samuel Ortiz4849f852012-04-10 19:43:17 +0200447 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448
449 switch (urb->status) {
450 case 0:
451 /* success */
452 break;
453 case -ECONNRESET:
454 case -ENOENT:
455 case -ESHUTDOWN:
456 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
457 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200458 dev->wq_in_error = urb->status;
459 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300460 default:
461 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
462 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200463 dev->wq_in_error = urb->status;
464 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300465 }
466
467 in_frame = dev->in_urb->transfer_buffer;
468
469 if (!pn533_rx_frame_is_valid(in_frame)) {
470 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200471 dev->wq_in_error = -EIO;
472 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300473 }
474
475 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
476 nfc_dev_err(&dev->interface->dev, "The received frame is not "
477 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200478 dev->wq_in_error = -EIO;
479 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300480 }
481
482 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200483 dev->wq_in_error = 0;
484 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300485
Samuel Ortiz4849f852012-04-10 19:43:17 +0200486sched_wq:
487 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300488}
489
490static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
491{
492 dev->in_urb->complete = pn533_recv_response;
493
494 return usb_submit_urb(dev->in_urb, flags);
495}
496
497static void pn533_recv_ack(struct urb *urb)
498{
499 struct pn533 *dev = urb->context;
500 struct pn533_frame *in_frame;
501 int rc;
502
503 switch (urb->status) {
504 case 0:
505 /* success */
506 break;
507 case -ECONNRESET:
508 case -ENOENT:
509 case -ESHUTDOWN:
510 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
511 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200512 dev->wq_in_error = urb->status;
513 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 default:
515 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
516 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200517 dev->wq_in_error = urb->status;
518 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300519 }
520
521 in_frame = dev->in_urb->transfer_buffer;
522
523 if (!pn533_rx_frame_is_ack(in_frame)) {
524 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200525 dev->wq_in_error = -EIO;
526 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300527 }
528
529 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
530
531 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
532 if (rc) {
533 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
534 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200535 dev->wq_in_error = rc;
536 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537 }
538
539 return;
540
Samuel Ortiz4849f852012-04-10 19:43:17 +0200541sched_wq:
542 dev->wq_in_frame = NULL;
543 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300544}
545
546static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
547{
548 dev->in_urb->complete = pn533_recv_ack;
549
550 return usb_submit_urb(dev->in_urb, flags);
551}
552
553static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
554{
555 int rc;
556
557 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
558
559 pn533_tx_frame_ack(dev->out_frame);
560
561 dev->out_urb->transfer_buffer = dev->out_frame;
562 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
563 rc = usb_submit_urb(dev->out_urb, flags);
564
565 return rc;
566}
567
568static int __pn533_send_cmd_frame_async(struct pn533 *dev,
569 struct pn533_frame *out_frame,
570 struct pn533_frame *in_frame,
571 int in_frame_len,
572 pn533_cmd_complete_t cmd_complete,
573 void *arg, gfp_t flags)
574{
575 int rc;
576
577 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
578 PN533_FRAME_CMD(out_frame));
579
580 dev->cmd = PN533_FRAME_CMD(out_frame);
581 dev->cmd_complete = cmd_complete;
582 dev->cmd_complete_arg = arg;
583
584 dev->out_urb->transfer_buffer = out_frame;
585 dev->out_urb->transfer_buffer_length =
586 PN533_FRAME_SIZE(out_frame);
587
588 dev->in_urb->transfer_buffer = in_frame;
589 dev->in_urb->transfer_buffer_length = in_frame_len;
590
591 rc = usb_submit_urb(dev->out_urb, flags);
592 if (rc)
593 return rc;
594
595 rc = pn533_submit_urb_for_ack(dev, flags);
596 if (rc)
597 goto error;
598
599 return 0;
600
601error:
602 usb_unlink_urb(dev->out_urb);
603 return rc;
604}
605
606static int pn533_send_cmd_frame_async(struct pn533 *dev,
607 struct pn533_frame *out_frame,
608 struct pn533_frame *in_frame,
609 int in_frame_len,
610 pn533_cmd_complete_t cmd_complete,
611 void *arg, gfp_t flags)
612{
613 int rc;
614
615 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
616
617 if (down_trylock(&dev->cmd_lock))
618 return -EBUSY;
619
620 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
621 in_frame_len, cmd_complete, arg, flags);
622 if (rc)
623 goto error;
624
625 return 0;
626error:
627 up(&dev->cmd_lock);
628 return rc;
629}
630
631struct pn533_sync_cmd_response {
632 int rc;
633 struct completion done;
634};
635
636static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
637 u8 *params, int params_len)
638{
639 struct pn533_sync_cmd_response *arg = _arg;
640
641 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
642
643 arg->rc = 0;
644
645 if (params_len < 0) /* error */
646 arg->rc = params_len;
647
648 complete(&arg->done);
649
650 return 0;
651}
652
653static int pn533_send_cmd_frame_sync(struct pn533 *dev,
654 struct pn533_frame *out_frame,
655 struct pn533_frame *in_frame,
656 int in_frame_len)
657{
658 int rc;
659 struct pn533_sync_cmd_response arg;
660
661 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
662
663 init_completion(&arg.done);
664
665 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
666 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
667 if (rc)
668 return rc;
669
670 wait_for_completion(&arg.done);
671
672 return arg.rc;
673}
674
675static void pn533_send_complete(struct urb *urb)
676{
677 struct pn533 *dev = urb->context;
678
679 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
680
681 switch (urb->status) {
682 case 0:
683 /* success */
684 break;
685 case -ECONNRESET:
686 case -ENOENT:
687 case -ESHUTDOWN:
688 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
689 " status: %d", urb->status);
690 break;
691 default:
692 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
693 " %d", urb->status);
694 }
695}
696
697struct pn533_target_type_a {
698 __be16 sens_res;
699 u8 sel_res;
700 u8 nfcid_len;
701 u8 nfcid_data[];
702} __packed;
703
704
705#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
706#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
707#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
708
709#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
710#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
711
712#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
713#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
714
715#define PN533_TYPE_A_SEL_PROT_MIFARE 0
716#define PN533_TYPE_A_SEL_PROT_ISO14443 1
717#define PN533_TYPE_A_SEL_PROT_DEP 2
718#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
719
720static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
721 int target_data_len)
722{
723 u8 ssd;
724 u8 platconf;
725
726 if (target_data_len < sizeof(struct pn533_target_type_a))
727 return false;
728
729 /* The lenght check of nfcid[] and ats[] are not being performed because
730 the values are not being used */
731
732 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
733 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
734 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
735
736 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
737 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
738 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
739 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
740 return false;
741
742 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
743 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
744 return false;
745
746 return true;
747}
748
749static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
750 int tgt_data_len)
751{
752 struct pn533_target_type_a *tgt_type_a;
753
754 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
755
756 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
757 return -EPROTO;
758
759 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
760 case PN533_TYPE_A_SEL_PROT_MIFARE:
761 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
762 break;
763 case PN533_TYPE_A_SEL_PROT_ISO14443:
764 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
765 break;
766 case PN533_TYPE_A_SEL_PROT_DEP:
767 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
768 break;
769 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
770 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
771 NFC_PROTO_NFC_DEP_MASK;
772 break;
773 }
774
775 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
776 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +0100777 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
778 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300779
780 return 0;
781}
782
783struct pn533_target_felica {
784 u8 pol_res;
785 u8 opcode;
786 u8 nfcid2[8];
787 u8 pad[8];
788 /* optional */
789 u8 syst_code[];
790} __packed;
791
792#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
793#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
794
795static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
796 int target_data_len)
797{
798 if (target_data_len < sizeof(struct pn533_target_felica))
799 return false;
800
801 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
802 return false;
803
804 return true;
805}
806
807static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
808 int tgt_data_len)
809{
810 struct pn533_target_felica *tgt_felica;
811
812 tgt_felica = (struct pn533_target_felica *) tgt_data;
813
814 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
815 return -EPROTO;
816
817 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
818 tgt_felica->nfcid2[1] ==
819 PN533_FELICA_SENSF_NFCID2_DEP_B2)
820 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
821 else
822 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
823
Samuel Ortiz79757542012-03-05 01:03:45 +0100824 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
825 nfc_tgt->sensf_res_len = 9;
826
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300827 return 0;
828}
829
830struct pn533_target_jewel {
831 __be16 sens_res;
832 u8 jewelid[4];
833} __packed;
834
835static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
836 int target_data_len)
837{
838 u8 ssd;
839 u8 platconf;
840
841 if (target_data_len < sizeof(struct pn533_target_jewel))
842 return false;
843
844 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
845 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
846 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
847
848 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
849 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
850 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
851 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
852 return false;
853
854 return true;
855}
856
857static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
858 int tgt_data_len)
859{
860 struct pn533_target_jewel *tgt_jewel;
861
862 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
863
864 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
865 return -EPROTO;
866
867 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
868 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +0100869 nfc_tgt->nfcid1_len = 4;
870 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300871
872 return 0;
873}
874
875struct pn533_type_b_prot_info {
876 u8 bitrate;
877 u8 fsci_type;
878 u8 fwi_adc_fo;
879} __packed;
880
881#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
882#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
883#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
884
885struct pn533_type_b_sens_res {
886 u8 opcode;
887 u8 nfcid[4];
888 u8 appdata[4];
889 struct pn533_type_b_prot_info prot_info;
890} __packed;
891
892#define PN533_TYPE_B_OPC_SENSB_RES 0x50
893
894struct pn533_target_type_b {
895 struct pn533_type_b_sens_res sensb_res;
896 u8 attrib_res_len;
897 u8 attrib_res[];
898} __packed;
899
900static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
901 int target_data_len)
902{
903 if (target_data_len < sizeof(struct pn533_target_type_b))
904 return false;
905
906 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
907 return false;
908
909 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
910 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
911 return false;
912
913 return true;
914}
915
916static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
917 int tgt_data_len)
918{
919 struct pn533_target_type_b *tgt_type_b;
920
921 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
922
923 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
924 return -EPROTO;
925
926 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
927
928 return 0;
929}
930
931struct pn533_poll_response {
932 u8 nbtg;
933 u8 tg;
934 u8 target_data[];
935} __packed;
936
937static int pn533_target_found(struct pn533 *dev,
938 struct pn533_poll_response *resp, int resp_len)
939{
940 int target_data_len;
941 struct nfc_target nfc_tgt;
942 int rc;
943
944 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
945 dev->poll_mod_curr);
946
947 if (resp->tg != 1)
948 return -EPROTO;
949
Samuel Ortiz98b3ac12012-03-05 01:03:39 +0100950 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
951
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300952 target_data_len = resp_len - sizeof(struct pn533_poll_response);
953
954 switch (dev->poll_mod_curr) {
955 case PN533_POLL_MOD_106KBPS_A:
956 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
957 target_data_len);
958 break;
959 case PN533_POLL_MOD_212KBPS_FELICA:
960 case PN533_POLL_MOD_424KBPS_FELICA:
961 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
962 target_data_len);
963 break;
964 case PN533_POLL_MOD_106KBPS_JEWEL:
965 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
966 target_data_len);
967 break;
968 case PN533_POLL_MOD_847KBPS_B:
969 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
970 target_data_len);
971 break;
972 default:
973 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
974 " modulation");
975 return -EPROTO;
976 }
977
978 if (rc)
979 return rc;
980
981 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
982 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
983 " have the desired protocol");
984 return -EAGAIN;
985 }
986
987 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
988 "0x%x", nfc_tgt.supported_protocols);
989
990 dev->tgt_available_prots = nfc_tgt.supported_protocols;
991
992 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
993
994 return 0;
995}
996
997static void pn533_poll_reset_mod_list(struct pn533 *dev)
998{
999 dev->poll_mod_count = 0;
1000}
1001
1002static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1003{
1004 dev->poll_mod_active[dev->poll_mod_count] =
1005 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1006 dev->poll_mod_count++;
1007}
1008
1009static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
1010{
1011 pn533_poll_reset_mod_list(dev);
1012
1013 if (protocols & NFC_PROTO_MIFARE_MASK
1014 || protocols & NFC_PROTO_ISO14443_MASK
1015 || protocols & NFC_PROTO_NFC_DEP_MASK)
1016 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1017
1018 if (protocols & NFC_PROTO_FELICA_MASK
1019 || protocols & NFC_PROTO_NFC_DEP_MASK) {
1020 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1021 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1022 }
1023
1024 if (protocols & NFC_PROTO_JEWEL_MASK)
1025 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1026
1027 if (protocols & NFC_PROTO_ISO14443_MASK)
1028 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1029}
1030
1031static void pn533_start_poll_frame(struct pn533_frame *frame,
1032 struct pn533_poll_modulations *mod)
1033{
1034
1035 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1036
1037 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1038 frame->datalen += mod->len;
1039
1040 pn533_tx_frame_finish(frame);
1041}
1042
1043static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
1044 u8 *params, int params_len)
1045{
1046 struct pn533_poll_response *resp;
1047 struct pn533_poll_modulations *next_mod;
1048 int rc;
1049
1050 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1051
1052 if (params_len == -ENOENT) {
1053 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
1054 " stopped");
1055 goto stop_poll;
1056 }
1057
1058 if (params_len < 0) {
1059 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
1060 params_len);
1061 goto stop_poll;
1062 }
1063
1064 resp = (struct pn533_poll_response *) params;
1065 if (resp->nbtg) {
1066 rc = pn533_target_found(dev, resp, params_len);
1067
1068 /* We must stop the poll after a valid target found */
1069 if (rc == 0)
1070 goto stop_poll;
1071
1072 if (rc != -EAGAIN)
1073 nfc_dev_err(&dev->interface->dev, "The target found is"
1074 " not valid - continuing to poll");
1075 }
1076
1077 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1078
1079 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1080
1081 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1082 dev->poll_mod_curr);
1083
1084 pn533_start_poll_frame(dev->out_frame, next_mod);
1085
1086 /* Don't need to down the semaphore again */
1087 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1088 dev->in_maxlen, pn533_start_poll_complete,
1089 NULL, GFP_ATOMIC);
1090
1091 if (rc == -EPERM) {
1092 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1093 " because poll has been stopped");
1094 goto stop_poll;
1095 }
1096
1097 if (rc) {
1098 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1099 " next modulation", rc);
1100 goto stop_poll;
1101 }
1102
1103 /* Inform caller function to do not up the semaphore */
1104 return -EINPROGRESS;
1105
1106stop_poll:
1107 pn533_poll_reset_mod_list(dev);
1108 dev->poll_protocols = 0;
1109 return 0;
1110}
1111
Samuel Ortizad3823c2012-05-30 23:54:55 +02001112static int pn533_init_target_frame(struct pn533_frame *frame,
1113 u8 *gb, size_t gb_len)
1114{
1115 struct pn533_cmd_init_target *cmd;
1116 size_t cmd_len;
1117
1118 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1119 cmd = kzalloc(cmd_len, GFP_KERNEL);
1120 if (cmd == NULL)
1121 return -ENOMEM;
1122
1123 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1124
1125 /* DEP support only */
1126 cmd->mode |= PN533_INIT_TARGET_DEP;
1127 get_random_bytes(cmd->nfcid3, 10);
1128 cmd->gb_len = gb_len;
1129 memcpy(cmd->gb, gb, gb_len);
1130 /* Len Tk */
1131 cmd->gb[gb_len] = 0;
1132
1133 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1134 frame->datalen += cmd_len;
1135
1136 pn533_tx_frame_finish(frame);
1137
1138 return 0;
1139}
1140
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001141#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1142#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1143static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1144 u8 *params, int params_len)
1145{
1146 struct sk_buff *skb_resp = arg;
1147 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1148
1149 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1150
1151 if (params_len < 0) {
1152 nfc_dev_err(&dev->interface->dev,
1153 "Error %d when starting as a target",
1154 params_len);
1155
1156 return params_len;
1157 }
1158
1159 if (params_len > 0 && params[0] != 0) {
1160 nfc_tm_deactivated(dev->nfc_dev);
1161
1162 kfree_skb(skb_resp);
1163 return 0;
1164 }
1165
1166 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1167 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1168 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1169
1170 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1171}
1172
1173static void pn533_wq_tg_get_data(struct work_struct *work)
1174{
1175 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1176 struct pn533_frame *in_frame;
1177 struct sk_buff *skb_resp;
1178 size_t skb_resp_len;
1179
1180 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1181
1182 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1183 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1184 PN533_FRAME_TAIL_SIZE;
1185
1186 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1187 if (!skb_resp)
1188 return;
1189
1190 in_frame = (struct pn533_frame *)skb_resp->data;
1191
1192 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1193 pn533_tx_frame_finish(dev->out_frame);
1194
1195 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1196 skb_resp_len,
1197 pn533_tm_get_data_complete,
1198 skb_resp, GFP_KERNEL);
1199
1200 return;
1201}
1202
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001203#define ATR_REQ_GB_OFFSET 17
Samuel Ortizad3823c2012-05-30 23:54:55 +02001204static int pn533_init_target_complete(struct pn533 *dev, void *arg,
1205 u8 *params, int params_len)
1206{
1207 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001208 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1209 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001210 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001211
1212 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1213
1214 if (params_len < 0) {
1215 nfc_dev_err(&dev->interface->dev,
1216 "Error %d when starting as a target",
1217 params_len);
1218
1219 return params_len;
1220 }
1221
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001222 if (params_len < ATR_REQ_GB_OFFSET + 1)
1223 return -EINVAL;
1224
Samuel Ortizad3823c2012-05-30 23:54:55 +02001225 resp = (struct pn533_cmd_init_target_response *) params;
1226
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001227 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1228 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001229
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001230 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1231 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1232 comm_mode = NFC_COMM_ACTIVE;
1233
1234 /* Again, only DEP */
1235 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1236 return -EOPNOTSUPP;
1237
1238 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1239 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1240
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001241 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1242 comm_mode, gb, gb_len);
1243 if (rc < 0) {
1244 nfc_dev_err(&dev->interface->dev,
1245 "Error when signaling target activation");
1246 return rc;
1247 }
1248
1249 queue_work(dev->wq, &dev->tg_work);
1250
1251 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001252}
1253
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001254static int pn533_init_target(struct nfc_dev *nfc_dev, u32 protocols)
1255{
Samuel Ortizad3823c2012-05-30 23:54:55 +02001256 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1257 u8 *gb;
1258 size_t gb_len;
1259 int rc;
1260
1261 pn533_poll_reset_mod_list(dev);
1262
1263 gb = nfc_get_local_general_bytes(nfc_dev, &gb_len);
1264 if (gb == NULL)
1265 return -ENOMEM;
1266
1267 rc = pn533_init_target_frame(dev->out_frame, gb, gb_len);
1268 if (rc < 0)
1269 return rc;
1270
1271 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1272 dev->in_maxlen,
1273 pn533_init_target_complete,
1274 NULL, GFP_KERNEL);
1275
1276 if (rc)
1277 nfc_dev_err(&dev->interface->dev,
1278 "Error %d when trying to initiate as a target", rc);
1279
1280 dev->poll_mod_count++;
1281
1282 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001283}
1284
1285static int pn533_start_im_poll(struct nfc_dev *nfc_dev, u32 protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001286{
1287 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1288 struct pn533_poll_modulations *start_mod;
1289 int rc;
1290
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001291 if (dev->poll_mod_count) {
1292 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1293 " active");
1294 return -EBUSY;
1295 }
1296
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001297 pn533_poll_create_mod_list(dev, protocols);
1298
1299 if (!dev->poll_mod_count) {
1300 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1301 " specified");
1302 rc = -EINVAL;
1303 goto error;
1304 }
1305
1306 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1307 dev->poll_mod_count);
1308
1309 dev->poll_mod_curr = 0;
1310 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1311
1312 pn533_start_poll_frame(dev->out_frame, start_mod);
1313
1314 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1315 dev->in_maxlen, pn533_start_poll_complete,
1316 NULL, GFP_KERNEL);
1317
1318 if (rc) {
1319 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1320 " start poll", rc);
1321 goto error;
1322 }
1323
1324 dev->poll_protocols = protocols;
1325
1326 return 0;
1327
1328error:
1329 pn533_poll_reset_mod_list(dev);
1330 return rc;
1331}
1332
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001333static int pn533_start_poll(struct nfc_dev *nfc_dev,
1334 u32 im_protocols, u32 tm_protocols)
1335{
1336 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1337
1338 nfc_dev_dbg(&dev->interface->dev,
1339 "%s: im protocols 0x%x tm protocols 0x%x",
1340 __func__, im_protocols, tm_protocols);
1341
1342 if (dev->tgt_active_prot) {
1343 nfc_dev_err(&dev->interface->dev,
1344 "Cannot poll with a target already activated");
1345 return -EBUSY;
1346 }
1347
Samuel Ortizad3823c2012-05-30 23:54:55 +02001348 if (im_protocols)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001349 return pn533_start_im_poll(nfc_dev, im_protocols);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001350
1351 if (tm_protocols)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001352 return pn533_init_target(nfc_dev, tm_protocols);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001353
1354 return -EINVAL;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001355}
1356
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001357static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1358{
1359 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1360
1361 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1362
1363 if (!dev->poll_mod_count) {
1364 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1365 " running");
1366 return;
1367 }
1368
1369 /* An ack will cancel the last issued command (poll) */
1370 pn533_send_ack(dev, GFP_KERNEL);
1371
1372 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1373 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001374
1375 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001376}
1377
1378static int pn533_activate_target_nfcdep(struct pn533 *dev)
1379{
1380 struct pn533_cmd_activate_param param;
1381 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001382 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001383 int rc;
1384
1385 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1386
1387 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1388
1389 param.tg = 1;
1390 param.next = 0;
1391 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1392 sizeof(struct pn533_cmd_activate_param));
1393 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1394
1395 pn533_tx_frame_finish(dev->out_frame);
1396
1397 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1398 dev->in_maxlen);
1399 if (rc)
1400 return rc;
1401
1402 resp = (struct pn533_cmd_activate_response *)
1403 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1404 rc = resp->status & PN533_CMD_RET_MASK;
1405 if (rc != PN533_CMD_RET_SUCCESS)
1406 return -EIO;
1407
Samuel Ortiz541d9202011-12-14 16:43:10 +01001408 /* ATR_RES general bytes are located at offset 16 */
1409 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1410 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1411
1412 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001413}
1414
Eric Lapuyade90099432012-05-07 12:31:13 +02001415static int pn533_activate_target(struct nfc_dev *nfc_dev,
1416 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001417{
1418 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1419 int rc;
1420
1421 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1422 protocol);
1423
1424 if (dev->poll_mod_count) {
1425 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1426 " polling");
1427 return -EBUSY;
1428 }
1429
1430 if (dev->tgt_active_prot) {
1431 nfc_dev_err(&dev->interface->dev, "There is already an active"
1432 " target");
1433 return -EBUSY;
1434 }
1435
1436 if (!dev->tgt_available_prots) {
1437 nfc_dev_err(&dev->interface->dev, "There is no available target"
1438 " to activate");
1439 return -EINVAL;
1440 }
1441
1442 if (!(dev->tgt_available_prots & (1 << protocol))) {
1443 nfc_dev_err(&dev->interface->dev, "The target does not support"
1444 " the requested protocol %u", protocol);
1445 return -EINVAL;
1446 }
1447
1448 if (protocol == NFC_PROTO_NFC_DEP) {
1449 rc = pn533_activate_target_nfcdep(dev);
1450 if (rc) {
1451 nfc_dev_err(&dev->interface->dev, "Error %d when"
1452 " activating target with"
1453 " NFC_DEP protocol", rc);
1454 return rc;
1455 }
1456 }
1457
1458 dev->tgt_active_prot = protocol;
1459 dev->tgt_available_prots = 0;
1460
1461 return 0;
1462}
1463
Eric Lapuyade90099432012-05-07 12:31:13 +02001464static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1465 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001466{
1467 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1468 u8 tg;
1469 u8 status;
1470 int rc;
1471
1472 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1473
1474 if (!dev->tgt_active_prot) {
1475 nfc_dev_err(&dev->interface->dev, "There is no active target");
1476 return;
1477 }
1478
1479 dev->tgt_active_prot = 0;
1480
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001481 skb_queue_purge(&dev->resp_q);
1482
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001483 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1484
1485 tg = 1;
1486 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1487 dev->out_frame->datalen += sizeof(u8);
1488
1489 pn533_tx_frame_finish(dev->out_frame);
1490
1491 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1492 dev->in_maxlen);
1493 if (rc) {
1494 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1495 " command to the controller");
1496 return;
1497 }
1498
1499 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1500 rc = status & PN533_CMD_RET_MASK;
1501 if (rc != PN533_CMD_RET_SUCCESS)
1502 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1503 " the target", rc);
1504
1505 return;
1506}
1507
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001508
1509static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1510 u8 *params, int params_len)
1511{
1512 struct pn533_cmd_jump_dep *cmd;
1513 struct pn533_cmd_jump_dep_response *resp;
1514 struct nfc_target nfc_target;
1515 u8 target_gt_len;
1516 int rc;
1517
1518 if (params_len == -ENOENT) {
1519 nfc_dev_dbg(&dev->interface->dev, "");
1520 return 0;
1521 }
1522
1523 if (params_len < 0) {
1524 nfc_dev_err(&dev->interface->dev,
1525 "Error %d when bringing DEP link up",
1526 params_len);
1527 return 0;
1528 }
1529
1530 if (dev->tgt_available_prots &&
1531 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1532 nfc_dev_err(&dev->interface->dev,
1533 "The target does not support DEP");
1534 return -EINVAL;
1535 }
1536
1537 resp = (struct pn533_cmd_jump_dep_response *) params;
1538 cmd = (struct pn533_cmd_jump_dep *) arg;
1539 rc = resp->status & PN533_CMD_RET_MASK;
1540 if (rc != PN533_CMD_RET_SUCCESS) {
1541 nfc_dev_err(&dev->interface->dev,
1542 "Bringing DEP link up failed %d", rc);
1543 return 0;
1544 }
1545
1546 if (!dev->tgt_available_prots) {
1547 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1548
1549 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001550 nfc_target.nfcid1_len = 10;
1551 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001552 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1553 if (rc)
1554 return 0;
1555
1556 dev->tgt_available_prots = 0;
1557 }
1558
1559 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1560
1561 /* ATR_RES general bytes are located at offset 17 */
1562 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1563 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1564 resp->gt, target_gt_len);
1565 if (rc == 0)
1566 rc = nfc_dep_link_is_up(dev->nfc_dev,
1567 dev->nfc_dev->targets[0].idx,
1568 !cmd->active, NFC_RF_INITIATOR);
1569
1570 return 0;
1571}
1572
Eric Lapuyade90099432012-05-07 12:31:13 +02001573static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001574 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001575{
1576 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1577 struct pn533_cmd_jump_dep *cmd;
Samuel Ortiz47807d32012-03-05 01:03:50 +01001578 u8 cmd_len;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001579 int rc;
1580
1581 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1582
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001583 if (dev->poll_mod_count) {
1584 nfc_dev_err(&dev->interface->dev,
1585 "Cannot bring the DEP link up while polling");
1586 return -EBUSY;
1587 }
1588
1589 if (dev->tgt_active_prot) {
1590 nfc_dev_err(&dev->interface->dev,
1591 "There is already an active target");
1592 return -EBUSY;
1593 }
1594
Samuel Ortiz47807d32012-03-05 01:03:50 +01001595 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001596 cmd = kzalloc(cmd_len, GFP_KERNEL);
1597 if (cmd == NULL)
1598 return -ENOMEM;
1599
1600 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1601
1602 cmd->active = !comm_mode;
1603 cmd->baud = 0;
Samuel Ortiz47807d32012-03-05 01:03:50 +01001604 if (gb != NULL && gb_len > 0) {
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001605 cmd->next = 4; /* We have some Gi */
Samuel Ortiz47807d32012-03-05 01:03:50 +01001606 memcpy(cmd->gt, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001607 } else {
1608 cmd->next = 0;
1609 }
1610
1611 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1612 dev->out_frame->datalen += cmd_len;
1613
1614 pn533_tx_frame_finish(dev->out_frame);
1615
1616 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1617 dev->in_maxlen, pn533_in_dep_link_up_complete,
1618 cmd, GFP_KERNEL);
1619 if (rc)
1620 goto out;
1621
1622
1623out:
1624 kfree(cmd);
1625
1626 return rc;
1627}
1628
1629static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1630{
1631 pn533_deactivate_target(nfc_dev, 0);
1632
1633 return 0;
1634}
1635
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001636static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
1637 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001638{
1639 int payload_len = skb->len;
1640 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001641 u8 tg;
1642
1643 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1644 payload_len);
1645
1646 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1647 /* TODO: Implement support to multi-part data exchange */
1648 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1649 " max allowed: %d",
1650 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1651 return -ENOSYS;
1652 }
1653
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001654 if (target == true) {
1655 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1656 out_frame = (struct pn533_frame *) skb->data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001657
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001658 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1659 tg = 1;
1660 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1661 out_frame->datalen += sizeof(u8);
1662 } else {
1663 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1664 out_frame = (struct pn533_frame *) skb->data;
1665 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
1666 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001667
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001668
1669 /* The data is already in the out_frame, just update the datalen */
1670 out_frame->datalen += payload_len;
1671
1672 pn533_tx_frame_finish(out_frame);
1673 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1674
1675 return 0;
1676}
1677
1678struct pn533_data_exchange_arg {
1679 struct sk_buff *skb_resp;
1680 struct sk_buff *skb_out;
1681 data_exchange_cb_t cb;
1682 void *cb_context;
1683};
1684
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001685static struct sk_buff *pn533_build_response(struct pn533 *dev)
1686{
1687 struct sk_buff *skb, *tmp, *t;
1688 unsigned int skb_len = 0, tmp_len = 0;
1689
1690 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1691
1692 if (skb_queue_empty(&dev->resp_q))
1693 return NULL;
1694
1695 if (skb_queue_len(&dev->resp_q) == 1) {
1696 skb = skb_dequeue(&dev->resp_q);
1697 goto out;
1698 }
1699
1700 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1701 skb_len += tmp->len;
1702
1703 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1704 __func__, skb_len);
1705
1706 skb = alloc_skb(skb_len, GFP_KERNEL);
1707 if (skb == NULL)
1708 goto out;
1709
1710 skb_put(skb, skb_len);
1711
1712 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1713 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1714 tmp_len += tmp->len;
1715 }
1716
1717out:
1718 skb_queue_purge(&dev->resp_q);
1719
1720 return skb;
1721}
1722
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001723static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1724 u8 *params, int params_len)
1725{
1726 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001727 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1729 int err = 0;
1730 u8 status;
1731 u8 cmd_ret;
1732
1733 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1734
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001735 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001736
1737 if (params_len < 0) { /* error */
1738 err = params_len;
1739 goto error;
1740 }
1741
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001742 status = params[0];
1743
1744 cmd_ret = status & PN533_CMD_RET_MASK;
1745 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1746 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1747 " exchanging data", cmd_ret);
1748 err = -EIO;
1749 goto error;
1750 }
1751
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001752 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001753 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1754 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001755 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001756
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001757 if (status & PN533_CMD_MI_MASK) {
1758 queue_work(dev->wq, &dev->mi_work);
1759 return -EINPROGRESS;
1760 }
1761
1762 skb = pn533_build_response(dev);
1763 if (skb == NULL)
1764 goto error;
1765
1766 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 kfree(arg);
1768 return 0;
1769
1770error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001771 skb_queue_purge(&dev->resp_q);
1772 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 arg->cb(arg->cb_context, NULL, err);
1774 kfree(arg);
1775 return 0;
1776}
1777
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001778static int pn533_transceive(struct nfc_dev *nfc_dev,
1779 struct nfc_target *target, struct sk_buff *skb,
1780 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001781{
1782 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1783 struct pn533_frame *out_frame, *in_frame;
1784 struct pn533_data_exchange_arg *arg;
1785 struct sk_buff *skb_resp;
1786 int skb_resp_len;
1787 int rc;
1788
1789 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1790
1791 if (!dev->tgt_active_prot) {
1792 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1793 " there is no active target");
1794 rc = -EINVAL;
1795 goto error;
1796 }
1797
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001798 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001799 if (rc)
1800 goto error;
1801
1802 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1803 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1804 PN533_FRAME_TAIL_SIZE;
1805
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01001806 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001807 if (!skb_resp) {
1808 rc = -ENOMEM;
1809 goto error;
1810 }
1811
1812 in_frame = (struct pn533_frame *) skb_resp->data;
1813 out_frame = (struct pn533_frame *) skb->data;
1814
1815 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1816 if (!arg) {
1817 rc = -ENOMEM;
1818 goto free_skb_resp;
1819 }
1820
1821 arg->skb_resp = skb_resp;
1822 arg->skb_out = skb;
1823 arg->cb = cb;
1824 arg->cb_context = cb_context;
1825
1826 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1827 pn533_data_exchange_complete, arg,
1828 GFP_KERNEL);
1829 if (rc) {
1830 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1831 " perform data_exchange", rc);
1832 goto free_arg;
1833 }
1834
1835 return 0;
1836
1837free_arg:
1838 kfree(arg);
1839free_skb_resp:
1840 kfree_skb(skb_resp);
1841error:
1842 kfree_skb(skb);
1843 return rc;
1844}
1845
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001846static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1847 u8 *params, int params_len)
1848{
1849 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1850
1851 if (params_len < 0) {
1852 nfc_dev_err(&dev->interface->dev,
1853 "Error %d when sending data",
1854 params_len);
1855
1856 return params_len;
1857 }
1858
1859 if (params_len > 0 && params[0] != 0) {
1860 nfc_tm_deactivated(dev->nfc_dev);
1861
1862 return 0;
1863 }
1864
1865 queue_work(dev->wq, &dev->tg_work);
1866
1867 return 0;
1868}
1869
1870static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1871{
1872 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1873 struct pn533_frame *out_frame;
1874 int rc;
1875
1876 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1877
1878 rc = pn533_build_tx_frame(dev, skb, false);
1879 if (rc)
1880 goto error;
1881
1882 out_frame = (struct pn533_frame *) skb->data;
1883
1884 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
1885 dev->in_maxlen, pn533_tm_send_complete,
1886 NULL, GFP_KERNEL);
1887 if (rc) {
1888 nfc_dev_err(&dev->interface->dev,
1889 "Error %d when trying to send data", rc);
1890 goto error;
1891 }
1892
1893 return 0;
1894
1895error:
1896 kfree_skb(skb);
1897
1898 return rc;
1899}
1900
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001901static void pn533_wq_mi_recv(struct work_struct *work)
1902{
1903 struct pn533 *dev = container_of(work, struct pn533, mi_work);
1904 struct sk_buff *skb_cmd;
1905 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
1906 struct pn533_frame *out_frame, *in_frame;
1907 struct sk_buff *skb_resp;
1908 int skb_resp_len;
1909 int rc;
1910
1911 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1912
1913 /* This is a zero payload size skb */
1914 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
1915 GFP_KERNEL);
1916 if (skb_cmd == NULL)
1917 goto error_cmd;
1918
1919 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
1920
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001921 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001922 if (rc)
1923 goto error_frame;
1924
1925 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1926 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1927 PN533_FRAME_TAIL_SIZE;
1928 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
1929 if (!skb_resp) {
1930 rc = -ENOMEM;
1931 goto error_frame;
1932 }
1933
1934 in_frame = (struct pn533_frame *) skb_resp->data;
1935 out_frame = (struct pn533_frame *) skb_cmd->data;
1936
1937 arg->skb_resp = skb_resp;
1938 arg->skb_out = skb_cmd;
1939
1940 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
1941 skb_resp_len,
1942 pn533_data_exchange_complete,
1943 dev->cmd_complete_arg, GFP_KERNEL);
1944 if (!rc)
1945 return;
1946
1947 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1948 " perform data_exchange", rc);
1949
1950 kfree_skb(skb_resp);
1951
1952error_frame:
1953 kfree_skb(skb_cmd);
1954
1955error_cmd:
1956 pn533_send_ack(dev, GFP_KERNEL);
1957
1958 kfree(arg);
1959
1960 up(&dev->cmd_lock);
1961}
1962
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001963static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1964 u8 cfgdata_len)
1965{
1966 int rc;
1967 u8 *params;
1968
1969 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1970
1971 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1972
1973 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1974 params[0] = cfgitem;
1975 memcpy(&params[1], cfgdata, cfgdata_len);
1976 dev->out_frame->datalen += (1 + cfgdata_len);
1977
1978 pn533_tx_frame_finish(dev->out_frame);
1979
1980 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1981 dev->in_maxlen);
1982
1983 return rc;
1984}
1985
1986struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03001987 .dev_up = NULL,
1988 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001989 .dep_link_up = pn533_dep_link_up,
1990 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001991 .start_poll = pn533_start_poll,
1992 .stop_poll = pn533_stop_poll,
1993 .activate_target = pn533_activate_target,
1994 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001995 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001996 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001997};
1998
1999static int pn533_probe(struct usb_interface *interface,
2000 const struct usb_device_id *id)
2001{
2002 struct pn533_fw_version *fw_ver;
2003 struct pn533 *dev;
2004 struct usb_host_interface *iface_desc;
2005 struct usb_endpoint_descriptor *endpoint;
2006 struct pn533_config_max_retries max_retries;
2007 int in_endpoint = 0;
2008 int out_endpoint = 0;
2009 int rc = -ENOMEM;
2010 int i;
2011 u32 protocols;
2012
2013 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2014 if (!dev)
2015 return -ENOMEM;
2016
2017 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2018 dev->interface = interface;
2019 sema_init(&dev->cmd_lock, 1);
2020
2021 iface_desc = interface->cur_altsetting;
2022 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2023 endpoint = &iface_desc->endpoint[i].desc;
2024
2025 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2026 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
2027 in_endpoint = endpoint->bEndpointAddress;
2028 }
2029
2030 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
2031 dev->out_maxlen =
2032 le16_to_cpu(endpoint->wMaxPacketSize);
2033 out_endpoint = endpoint->bEndpointAddress;
2034 }
2035 }
2036
2037 if (!in_endpoint || !out_endpoint) {
2038 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2039 " bulk-out endpoint");
2040 rc = -ENODEV;
2041 goto error;
2042 }
2043
2044 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
2045 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2046 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
2047 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2048
2049 if (!dev->in_frame || !dev->out_frame ||
2050 !dev->in_urb || !dev->out_urb)
2051 goto error;
2052
2053 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2054 usb_rcvbulkpipe(dev->udev, in_endpoint),
2055 NULL, 0, NULL, dev);
2056 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2057 usb_sndbulkpipe(dev->udev, out_endpoint),
2058 NULL, 0,
2059 pn533_send_complete, dev);
2060
Samuel Ortiz4849f852012-04-10 19:43:17 +02002061 INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002062 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002063 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002064 dev->wq = alloc_workqueue("pn533",
2065 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
2066 1);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002067 if (dev->wq == NULL)
2068 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002069
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002070 skb_queue_head_init(&dev->resp_q);
2071
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002072 usb_set_intfdata(interface, dev);
2073
2074 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2075 pn533_tx_frame_finish(dev->out_frame);
2076
2077 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2078 dev->in_maxlen);
2079 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002080 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002081
2082 fw_ver = (struct pn533_fw_version *)
2083 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2084 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2085 " attached", fw_ver->ver, fw_ver->rev);
2086
2087 protocols = NFC_PROTO_JEWEL_MASK
2088 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
2089 | NFC_PROTO_ISO14443_MASK
2090 | NFC_PROTO_NFC_DEP_MASK;
2091
Samuel Ortize8753042011-08-19 15:47:11 +02002092 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2093 PN533_CMD_DATAEXCH_HEAD_LEN,
2094 PN533_FRAME_TAIL_SIZE);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002095 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002096 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002097
2098 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2099 nfc_set_drvdata(dev->nfc_dev, dev);
2100
2101 rc = nfc_register_device(dev->nfc_dev);
2102 if (rc)
2103 goto free_nfc_dev;
2104
2105 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2106 max_retries.mx_rty_psl = 2;
2107 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2108
2109 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2110 (u8 *) &max_retries, sizeof(max_retries));
2111
2112 if (rc) {
2113 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
2114 " config");
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002115 goto unregister_nfc_dev;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002116 }
2117
2118 return 0;
2119
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002120unregister_nfc_dev:
2121 nfc_unregister_device(dev->nfc_dev);
2122
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123free_nfc_dev:
2124 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002125
Samuel Ortiz4849f852012-04-10 19:43:17 +02002126destroy_wq:
2127 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002128error:
2129 kfree(dev->in_frame);
2130 usb_free_urb(dev->in_urb);
2131 kfree(dev->out_frame);
2132 usb_free_urb(dev->out_urb);
2133 kfree(dev);
2134 return rc;
2135}
2136
2137static void pn533_disconnect(struct usb_interface *interface)
2138{
2139 struct pn533 *dev;
2140
2141 dev = usb_get_intfdata(interface);
2142 usb_set_intfdata(interface, NULL);
2143
2144 nfc_unregister_device(dev->nfc_dev);
2145 nfc_free_device(dev->nfc_dev);
2146
2147 usb_kill_urb(dev->in_urb);
2148 usb_kill_urb(dev->out_urb);
2149
Samuel Ortiz4849f852012-04-10 19:43:17 +02002150 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002152 skb_queue_purge(&dev->resp_q);
2153
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002154 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
Dan Carpenter276556d2011-07-08 10:21:15 +03002160 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161}
2162
2163static struct usb_driver pn533_driver = {
2164 .name = "pn533",
2165 .probe = pn533_probe,
2166 .disconnect = pn533_disconnect,
2167 .id_table = pn533_table,
2168};
2169
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002170module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002171
2172MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2173 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2174MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2175MODULE_VERSION(VERSION);
2176MODULE_LICENSE("GPL");