aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/Kconfig24
-rw-r--r--drivers/nfc/Makefile3
-rw-r--r--drivers/nfc/pn533.c1632
-rw-r--r--drivers/nfc/pn544.c1055
4 files changed, 1928 insertions, 786 deletions
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index ea158008534..2acff4307ca 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -2,17 +2,8 @@
2# Near Field Communication (NFC) devices 2# Near Field Communication (NFC) devices
3# 3#
4 4
5menuconfig NFC_DEVICES 5menu "Near Field Communication (NFC) devices"
6 bool "Near Field Communication (NFC) devices" 6 depends on NFC
7 default n
8 ---help---
9 You'll have to say Y if your computer contains an NFC device that
10 you want to use under Linux.
11
12 You can say N here if you don't have any Near Field Communication
13 devices connected to your computer.
14
15if NFC_DEVICES
16 7
17config PN544_NFC 8config PN544_NFC
18 tristate "PN544 NFC driver" 9 tristate "PN544 NFC driver"
@@ -26,5 +17,14 @@ config PN544_NFC
26 To compile this driver as a module, choose m here. The module will 17 To compile this driver as a module, choose m here. The module will
27 be called pn544. 18 be called pn544.
28 19
20config NFC_PN533
21 tristate "NXP PN533 USB driver"
22 depends on USB
23 help
24 NXP PN533 USB driver.
25 This driver provides support for NFC NXP PN533 devices.
26
27 Say Y here to compile support for PN533 devices into the
28 kernel or say M to compile it as module (pn533).
29 29
30endif # NFC_DEVICES 30endmenu
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index a4efb164ec4..8ef446d2c1b 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -3,3 +3,6 @@
3# 3#
4 4
5obj-$(CONFIG_PN544_NFC) += pn544.o 5obj-$(CONFIG_PN544_NFC) += pn544.o
6obj-$(CONFIG_NFC_PN533) += pn533.o
7
8ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
diff --git a/drivers/nfc/pn533.c b/drivers/nfc/pn533.c
new file mode 100644
index 00000000000..c77e0543e50
--- /dev/null
+++ b/drivers/nfc/pn533.c
@@ -0,0 +1,1632 @@
1/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
31#include <net/nfc.h>
32
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
41static const struct usb_device_id pn533_table[] = {
42 { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID) },
43 { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID) },
44 { }
45};
46MODULE_DEVICE_TABLE(usb, pn533_table);
47
48/* frame definitions */
49#define PN533_FRAME_TAIL_SIZE 2
50#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
51 PN533_FRAME_TAIL_SIZE)
52#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
53#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
54#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
55
56/* start of frame */
57#define PN533_SOF 0x00FF
58
59/* frame identifier: in/out/error */
60#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
61#define PN533_DIR_OUT 0xD4
62#define PN533_DIR_IN 0xD5
63
64/* PN533 Commands */
65#define PN533_FRAME_CMD(f) (f->data[1])
66#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
67#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
68
69#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
70#define PN533_CMD_RF_CONFIGURATION 0x32
71#define PN533_CMD_IN_DATA_EXCHANGE 0x40
72#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
73#define PN533_CMD_IN_ATR 0x50
74#define PN533_CMD_IN_RELEASE 0x52
75
76#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
77
78/* PN533 Return codes */
79#define PN533_CMD_RET_MASK 0x3F
80#define PN533_CMD_MI_MASK 0x40
81#define PN533_CMD_RET_SUCCESS 0x00
82
83struct pn533;
84
85typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
86 u8 *params, int params_len);
87
88/* structs for pn533 commands */
89
90/* PN533_CMD_GET_FIRMWARE_VERSION */
91struct pn533_fw_version {
92 u8 ic;
93 u8 ver;
94 u8 rev;
95 u8 support;
96};
97
98/* PN533_CMD_RF_CONFIGURATION */
99#define PN533_CFGITEM_MAX_RETRIES 0x05
100
101#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
102#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
103
104struct pn533_config_max_retries {
105 u8 mx_rty_atr;
106 u8 mx_rty_psl;
107 u8 mx_rty_passive_act;
108} __packed;
109
110/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
111
112/* felica commands opcode */
113#define PN533_FELICA_OPC_SENSF_REQ 0
114#define PN533_FELICA_OPC_SENSF_RES 1
115/* felica SENSF_REQ parameters */
116#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
117#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
118#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
119#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
120
121/* type B initiator_data values */
122#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
123#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
124#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
125
126union pn533_cmd_poll_initdata {
127 struct {
128 u8 afi;
129 u8 polling_method;
130 } __packed type_b;
131 struct {
132 u8 opcode;
133 __be16 sc;
134 u8 rc;
135 u8 tsn;
136 } __packed felica;
137};
138
139/* Poll modulations */
140enum {
141 PN533_POLL_MOD_106KBPS_A,
142 PN533_POLL_MOD_212KBPS_FELICA,
143 PN533_POLL_MOD_424KBPS_FELICA,
144 PN533_POLL_MOD_106KBPS_JEWEL,
145 PN533_POLL_MOD_847KBPS_B,
146
147 __PN533_POLL_MOD_AFTER_LAST,
148};
149#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
150
151struct pn533_poll_modulations {
152 struct {
153 u8 maxtg;
154 u8 brty;
155 union pn533_cmd_poll_initdata initiator_data;
156 } __packed data;
157 u8 len;
158};
159
160const struct pn533_poll_modulations poll_mod[] = {
161 [PN533_POLL_MOD_106KBPS_A] = {
162 .data = {
163 .maxtg = 1,
164 .brty = 0,
165 },
166 .len = 2,
167 },
168 [PN533_POLL_MOD_212KBPS_FELICA] = {
169 .data = {
170 .maxtg = 1,
171 .brty = 1,
172 .initiator_data.felica = {
173 .opcode = PN533_FELICA_OPC_SENSF_REQ,
174 .sc = PN533_FELICA_SENSF_SC_ALL,
175 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
176 .tsn = 0,
177 },
178 },
179 .len = 7,
180 },
181 [PN533_POLL_MOD_424KBPS_FELICA] = {
182 .data = {
183 .maxtg = 1,
184 .brty = 2,
185 .initiator_data.felica = {
186 .opcode = PN533_FELICA_OPC_SENSF_REQ,
187 .sc = PN533_FELICA_SENSF_SC_ALL,
188 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
189 .tsn = 0,
190 },
191 },
192 .len = 7,
193 },
194 [PN533_POLL_MOD_106KBPS_JEWEL] = {
195 .data = {
196 .maxtg = 1,
197 .brty = 4,
198 },
199 .len = 2,
200 },
201 [PN533_POLL_MOD_847KBPS_B] = {
202 .data = {
203 .maxtg = 1,
204 .brty = 8,
205 .initiator_data.type_b = {
206 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
207 .polling_method =
208 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
209 },
210 },
211 .len = 3,
212 },
213};
214
215/* PN533_CMD_IN_ATR */
216
217struct pn533_cmd_activate_param {
218 u8 tg;
219 u8 next;
220} __packed;
221
222struct pn533_cmd_activate_response {
223 u8 status;
224 u8 nfcid3t[10];
225 u8 didt;
226 u8 bst;
227 u8 brt;
228 u8 to;
229 u8 ppt;
230 /* optional */
231 u8 gt[];
232} __packed;
233
234
235struct pn533 {
236 struct usb_device *udev;
237 struct usb_interface *interface;
238 struct nfc_dev *nfc_dev;
239
240 struct urb *out_urb;
241 int out_maxlen;
242 struct pn533_frame *out_frame;
243
244 struct urb *in_urb;
245 int in_maxlen;
246 struct pn533_frame *in_frame;
247
248 struct tasklet_struct tasklet;
249 struct pn533_frame *tklt_in_frame;
250 int tklt_in_error;
251
252 pn533_cmd_complete_t cmd_complete;
253 void *cmd_complete_arg;
254 struct semaphore cmd_lock;
255 u8 cmd;
256
257 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
258 u8 poll_mod_count;
259 u8 poll_mod_curr;
260 u32 poll_protocols;
261
262 u8 tgt_available_prots;
263 u8 tgt_active_prot;
264};
265
266struct pn533_frame {
267 u8 preamble;
268 __be16 start_frame;
269 u8 datalen;
270 u8 datalen_checksum;
271 u8 data[];
272} __packed;
273
274/* The rule: value + checksum = 0 */
275static inline u8 pn533_checksum(u8 value)
276{
277 return ~value + 1;
278}
279
280/* The rule: sum(data elements) + checksum = 0 */
281static u8 pn533_data_checksum(u8 *data, int datalen)
282{
283 u8 sum = 0;
284 int i;
285
286 for (i = 0; i < datalen; i++)
287 sum += data[i];
288
289 return pn533_checksum(sum);
290}
291
292/**
293 * pn533_tx_frame_ack - create a ack frame
294 * @frame: The frame to be set as ack
295 *
296 * Ack is different type of standard frame. As a standard frame, it has
297 * preamble and start_frame. However the checksum of this frame must fail,
298 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
299 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
300 * After datalen_checksum field, the postamble is placed.
301 */
302static void pn533_tx_frame_ack(struct pn533_frame *frame)
303{
304 frame->preamble = 0;
305 frame->start_frame = cpu_to_be16(PN533_SOF);
306 frame->datalen = 0;
307 frame->datalen_checksum = 0xFF;
308 /* data[0] is used as postamble */
309 frame->data[0] = 0;
310}
311
312static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
313{
314 frame->preamble = 0;
315 frame->start_frame = cpu_to_be16(PN533_SOF);
316 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
317 PN533_FRAME_CMD(frame) = cmd;
318 frame->datalen = 2;
319}
320
321static void pn533_tx_frame_finish(struct pn533_frame *frame)
322{
323 frame->datalen_checksum = pn533_checksum(frame->datalen);
324
325 PN533_FRAME_CHECKSUM(frame) =
326 pn533_data_checksum(frame->data, frame->datalen);
327
328 PN533_FRAME_POSTAMBLE(frame) = 0;
329}
330
331static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
332{
333 u8 checksum;
334
335 if (frame->start_frame != cpu_to_be16(PN533_SOF))
336 return false;
337
338 checksum = pn533_checksum(frame->datalen);
339 if (checksum != frame->datalen_checksum)
340 return false;
341
342 checksum = pn533_data_checksum(frame->data, frame->datalen);
343 if (checksum != PN533_FRAME_CHECKSUM(frame))
344 return false;
345
346 return true;
347}
348
349static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
350{
351 if (frame->start_frame != cpu_to_be16(PN533_SOF))
352 return false;
353
354 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
355 return false;
356
357 return true;
358}
359
360static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
361{
362 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
363}
364
365static void pn533_tasklet_cmd_complete(unsigned long arg)
366{
367 struct pn533 *dev = (struct pn533 *) arg;
368 struct pn533_frame *in_frame = dev->tklt_in_frame;
369 int rc;
370
371 if (dev->tklt_in_error)
372 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
373 dev->tklt_in_error);
374 else
375 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
376 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
377 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
378
379 if (rc != -EINPROGRESS)
380 up(&dev->cmd_lock);
381}
382
383static void pn533_recv_response(struct urb *urb)
384{
385 struct pn533 *dev = urb->context;
386 struct pn533_frame *in_frame;
387
388 dev->tklt_in_frame = NULL;
389
390 switch (urb->status) {
391 case 0:
392 /* success */
393 break;
394 case -ECONNRESET:
395 case -ENOENT:
396 case -ESHUTDOWN:
397 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
398 " status: %d", urb->status);
399 dev->tklt_in_error = urb->status;
400 goto sched_tasklet;
401 default:
402 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
403 " %d", urb->status);
404 dev->tklt_in_error = urb->status;
405 goto sched_tasklet;
406 }
407
408 in_frame = dev->in_urb->transfer_buffer;
409
410 if (!pn533_rx_frame_is_valid(in_frame)) {
411 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
412 dev->tklt_in_error = -EIO;
413 goto sched_tasklet;
414 }
415
416 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
417 nfc_dev_err(&dev->interface->dev, "The received frame is not "
418 "response to the last command");
419 dev->tklt_in_error = -EIO;
420 goto sched_tasklet;
421 }
422
423 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
424 dev->tklt_in_error = 0;
425 dev->tklt_in_frame = in_frame;
426
427sched_tasklet:
428 tasklet_schedule(&dev->tasklet);
429}
430
431static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
432{
433 dev->in_urb->complete = pn533_recv_response;
434
435 return usb_submit_urb(dev->in_urb, flags);
436}
437
438static void pn533_recv_ack(struct urb *urb)
439{
440 struct pn533 *dev = urb->context;
441 struct pn533_frame *in_frame;
442 int rc;
443
444 switch (urb->status) {
445 case 0:
446 /* success */
447 break;
448 case -ECONNRESET:
449 case -ENOENT:
450 case -ESHUTDOWN:
451 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
452 " status: %d", urb->status);
453 dev->tklt_in_error = urb->status;
454 goto sched_tasklet;
455 default:
456 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
457 " %d", urb->status);
458 dev->tklt_in_error = urb->status;
459 goto sched_tasklet;
460 }
461
462 in_frame = dev->in_urb->transfer_buffer;
463
464 if (!pn533_rx_frame_is_ack(in_frame)) {
465 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
466 dev->tklt_in_error = -EIO;
467 goto sched_tasklet;
468 }
469
470 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
471
472 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
473 if (rc) {
474 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
475 " result %d", rc);
476 dev->tklt_in_error = rc;
477 goto sched_tasklet;
478 }
479
480 return;
481
482sched_tasklet:
483 dev->tklt_in_frame = NULL;
484 tasklet_schedule(&dev->tasklet);
485}
486
487static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
488{
489 dev->in_urb->complete = pn533_recv_ack;
490
491 return usb_submit_urb(dev->in_urb, flags);
492}
493
494static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
495{
496 int rc;
497
498 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
499
500 pn533_tx_frame_ack(dev->out_frame);
501
502 dev->out_urb->transfer_buffer = dev->out_frame;
503 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
504 rc = usb_submit_urb(dev->out_urb, flags);
505
506 return rc;
507}
508
509static int __pn533_send_cmd_frame_async(struct pn533 *dev,
510 struct pn533_frame *out_frame,
511 struct pn533_frame *in_frame,
512 int in_frame_len,
513 pn533_cmd_complete_t cmd_complete,
514 void *arg, gfp_t flags)
515{
516 int rc;
517
518 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
519 PN533_FRAME_CMD(out_frame));
520
521 dev->cmd = PN533_FRAME_CMD(out_frame);
522 dev->cmd_complete = cmd_complete;
523 dev->cmd_complete_arg = arg;
524
525 dev->out_urb->transfer_buffer = out_frame;
526 dev->out_urb->transfer_buffer_length =
527 PN533_FRAME_SIZE(out_frame);
528
529 dev->in_urb->transfer_buffer = in_frame;
530 dev->in_urb->transfer_buffer_length = in_frame_len;
531
532 rc = usb_submit_urb(dev->out_urb, flags);
533 if (rc)
534 return rc;
535
536 rc = pn533_submit_urb_for_ack(dev, flags);
537 if (rc)
538 goto error;
539
540 return 0;
541
542error:
543 usb_unlink_urb(dev->out_urb);
544 return rc;
545}
546
547static int pn533_send_cmd_frame_async(struct pn533 *dev,
548 struct pn533_frame *out_frame,
549 struct pn533_frame *in_frame,
550 int in_frame_len,
551 pn533_cmd_complete_t cmd_complete,
552 void *arg, gfp_t flags)
553{
554 int rc;
555
556 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
557
558 if (down_trylock(&dev->cmd_lock))
559 return -EBUSY;
560
561 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
562 in_frame_len, cmd_complete, arg, flags);
563 if (rc)
564 goto error;
565
566 return 0;
567error:
568 up(&dev->cmd_lock);
569 return rc;
570}
571
572struct pn533_sync_cmd_response {
573 int rc;
574 struct completion done;
575};
576
577static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
578 u8 *params, int params_len)
579{
580 struct pn533_sync_cmd_response *arg = _arg;
581
582 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
583
584 arg->rc = 0;
585
586 if (params_len < 0) /* error */
587 arg->rc = params_len;
588
589 complete(&arg->done);
590
591 return 0;
592}
593
594static int pn533_send_cmd_frame_sync(struct pn533 *dev,
595 struct pn533_frame *out_frame,
596 struct pn533_frame *in_frame,
597 int in_frame_len)
598{
599 int rc;
600 struct pn533_sync_cmd_response arg;
601
602 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
603
604 init_completion(&arg.done);
605
606 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
607 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
608 if (rc)
609 return rc;
610
611 wait_for_completion(&arg.done);
612
613 return arg.rc;
614}
615
616static void pn533_send_complete(struct urb *urb)
617{
618 struct pn533 *dev = urb->context;
619
620 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
621
622 switch (urb->status) {
623 case 0:
624 /* success */
625 break;
626 case -ECONNRESET:
627 case -ENOENT:
628 case -ESHUTDOWN:
629 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
630 " status: %d", urb->status);
631 break;
632 default:
633 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
634 " %d", urb->status);
635 }
636}
637
638struct pn533_target_type_a {
639 __be16 sens_res;
640 u8 sel_res;
641 u8 nfcid_len;
642 u8 nfcid_data[];
643} __packed;
644
645
646#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
647#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
648#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
649
650#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
651#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
652
653#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
654#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
655
656#define PN533_TYPE_A_SEL_PROT_MIFARE 0
657#define PN533_TYPE_A_SEL_PROT_ISO14443 1
658#define PN533_TYPE_A_SEL_PROT_DEP 2
659#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
660
661static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
662 int target_data_len)
663{
664 u8 ssd;
665 u8 platconf;
666
667 if (target_data_len < sizeof(struct pn533_target_type_a))
668 return false;
669
670 /* The lenght check of nfcid[] and ats[] are not being performed because
671 the values are not being used */
672
673 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
674 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
675 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
676
677 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
678 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
679 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
680 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
681 return false;
682
683 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
684 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
685 return false;
686
687 return true;
688}
689
690static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
691 int tgt_data_len)
692{
693 struct pn533_target_type_a *tgt_type_a;
694
695 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
696
697 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
698 return -EPROTO;
699
700 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
701 case PN533_TYPE_A_SEL_PROT_MIFARE:
702 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
703 break;
704 case PN533_TYPE_A_SEL_PROT_ISO14443:
705 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
706 break;
707 case PN533_TYPE_A_SEL_PROT_DEP:
708 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
709 break;
710 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
711 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
712 NFC_PROTO_NFC_DEP_MASK;
713 break;
714 }
715
716 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
717 nfc_tgt->sel_res = tgt_type_a->sel_res;
718
719 return 0;
720}
721
722struct pn533_target_felica {
723 u8 pol_res;
724 u8 opcode;
725 u8 nfcid2[8];
726 u8 pad[8];
727 /* optional */
728 u8 syst_code[];
729} __packed;
730
731#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
732#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
733
734static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
735 int target_data_len)
736{
737 if (target_data_len < sizeof(struct pn533_target_felica))
738 return false;
739
740 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
741 return false;
742
743 return true;
744}
745
746static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
747 int tgt_data_len)
748{
749 struct pn533_target_felica *tgt_felica;
750
751 tgt_felica = (struct pn533_target_felica *) tgt_data;
752
753 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
754 return -EPROTO;
755
756 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
757 tgt_felica->nfcid2[1] ==
758 PN533_FELICA_SENSF_NFCID2_DEP_B2)
759 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
760 else
761 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
762
763 return 0;
764}
765
766struct pn533_target_jewel {
767 __be16 sens_res;
768 u8 jewelid[4];
769} __packed;
770
771static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
772 int target_data_len)
773{
774 u8 ssd;
775 u8 platconf;
776
777 if (target_data_len < sizeof(struct pn533_target_jewel))
778 return false;
779
780 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
781 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
782 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
783
784 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
785 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
786 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
787 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
788 return false;
789
790 return true;
791}
792
793static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
794 int tgt_data_len)
795{
796 struct pn533_target_jewel *tgt_jewel;
797
798 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
799
800 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
801 return -EPROTO;
802
803 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
804 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
805
806 return 0;
807}
808
809struct pn533_type_b_prot_info {
810 u8 bitrate;
811 u8 fsci_type;
812 u8 fwi_adc_fo;
813} __packed;
814
815#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
816#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
817#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
818
819struct pn533_type_b_sens_res {
820 u8 opcode;
821 u8 nfcid[4];
822 u8 appdata[4];
823 struct pn533_type_b_prot_info prot_info;
824} __packed;
825
826#define PN533_TYPE_B_OPC_SENSB_RES 0x50
827
828struct pn533_target_type_b {
829 struct pn533_type_b_sens_res sensb_res;
830 u8 attrib_res_len;
831 u8 attrib_res[];
832} __packed;
833
834static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
835 int target_data_len)
836{
837 if (target_data_len < sizeof(struct pn533_target_type_b))
838 return false;
839
840 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
841 return false;
842
843 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
844 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
845 return false;
846
847 return true;
848}
849
850static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
851 int tgt_data_len)
852{
853 struct pn533_target_type_b *tgt_type_b;
854
855 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
856
857 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
858 return -EPROTO;
859
860 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
861
862 return 0;
863}
864
865struct pn533_poll_response {
866 u8 nbtg;
867 u8 tg;
868 u8 target_data[];
869} __packed;
870
871static int pn533_target_found(struct pn533 *dev,
872 struct pn533_poll_response *resp, int resp_len)
873{
874 int target_data_len;
875 struct nfc_target nfc_tgt;
876 int rc;
877
878 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
879 dev->poll_mod_curr);
880
881 if (resp->tg != 1)
882 return -EPROTO;
883
884 target_data_len = resp_len - sizeof(struct pn533_poll_response);
885
886 switch (dev->poll_mod_curr) {
887 case PN533_POLL_MOD_106KBPS_A:
888 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
889 target_data_len);
890 break;
891 case PN533_POLL_MOD_212KBPS_FELICA:
892 case PN533_POLL_MOD_424KBPS_FELICA:
893 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
894 target_data_len);
895 break;
896 case PN533_POLL_MOD_106KBPS_JEWEL:
897 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
898 target_data_len);
899 break;
900 case PN533_POLL_MOD_847KBPS_B:
901 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
902 target_data_len);
903 break;
904 default:
905 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
906 " modulation");
907 return -EPROTO;
908 }
909
910 if (rc)
911 return rc;
912
913 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
914 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
915 " have the desired protocol");
916 return -EAGAIN;
917 }
918
919 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
920 "0x%x", nfc_tgt.supported_protocols);
921
922 dev->tgt_available_prots = nfc_tgt.supported_protocols;
923
924 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
925
926 return 0;
927}
928
929static void pn533_poll_reset_mod_list(struct pn533 *dev)
930{
931 dev->poll_mod_count = 0;
932}
933
934static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
935{
936 dev->poll_mod_active[dev->poll_mod_count] =
937 (struct pn533_poll_modulations *) &poll_mod[mod_index];
938 dev->poll_mod_count++;
939}
940
941static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
942{
943 pn533_poll_reset_mod_list(dev);
944
945 if (protocols & NFC_PROTO_MIFARE_MASK
946 || protocols & NFC_PROTO_ISO14443_MASK
947 || protocols & NFC_PROTO_NFC_DEP_MASK)
948 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
949
950 if (protocols & NFC_PROTO_FELICA_MASK
951 || protocols & NFC_PROTO_NFC_DEP_MASK) {
952 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
953 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
954 }
955
956 if (protocols & NFC_PROTO_JEWEL_MASK)
957 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
958
959 if (protocols & NFC_PROTO_ISO14443_MASK)
960 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
961}
962
963static void pn533_start_poll_frame(struct pn533_frame *frame,
964 struct pn533_poll_modulations *mod)
965{
966
967 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
968
969 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
970 frame->datalen += mod->len;
971
972 pn533_tx_frame_finish(frame);
973}
974
975static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
976 u8 *params, int params_len)
977{
978 struct pn533_poll_response *resp;
979 struct pn533_poll_modulations *next_mod;
980 int rc;
981
982 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
983
984 if (params_len == -ENOENT) {
985 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
986 " stopped");
987 goto stop_poll;
988 }
989
990 if (params_len < 0) {
991 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
992 params_len);
993 goto stop_poll;
994 }
995
996 resp = (struct pn533_poll_response *) params;
997 if (resp->nbtg) {
998 rc = pn533_target_found(dev, resp, params_len);
999
1000 /* We must stop the poll after a valid target found */
1001 if (rc == 0)
1002 goto stop_poll;
1003
1004 if (rc != -EAGAIN)
1005 nfc_dev_err(&dev->interface->dev, "The target found is"
1006 " not valid - continuing to poll");
1007 }
1008
1009 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1010
1011 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1012
1013 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1014 dev->poll_mod_curr);
1015
1016 pn533_start_poll_frame(dev->out_frame, next_mod);
1017
1018 /* Don't need to down the semaphore again */
1019 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1020 dev->in_maxlen, pn533_start_poll_complete,
1021 NULL, GFP_ATOMIC);
1022
1023 if (rc == -EPERM) {
1024 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1025 " because poll has been stopped");
1026 goto stop_poll;
1027 }
1028
1029 if (rc) {
1030 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1031 " next modulation", rc);
1032 goto stop_poll;
1033 }
1034
1035 /* Inform caller function to do not up the semaphore */
1036 return -EINPROGRESS;
1037
1038stop_poll:
1039 pn533_poll_reset_mod_list(dev);
1040 dev->poll_protocols = 0;
1041 return 0;
1042}
1043
1044static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
1045{
1046 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1047 struct pn533_poll_modulations *start_mod;
1048 int rc;
1049
1050 nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
1051 protocols);
1052
1053 if (dev->poll_mod_count) {
1054 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1055 " active");
1056 return -EBUSY;
1057 }
1058
1059 if (dev->tgt_active_prot) {
1060 nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
1061 " already activated");
1062 return -EBUSY;
1063 }
1064
1065 pn533_poll_create_mod_list(dev, protocols);
1066
1067 if (!dev->poll_mod_count) {
1068 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1069 " specified");
1070 rc = -EINVAL;
1071 goto error;
1072 }
1073
1074 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1075 dev->poll_mod_count);
1076
1077 dev->poll_mod_curr = 0;
1078 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1079
1080 pn533_start_poll_frame(dev->out_frame, start_mod);
1081
1082 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1083 dev->in_maxlen, pn533_start_poll_complete,
1084 NULL, GFP_KERNEL);
1085
1086 if (rc) {
1087 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1088 " start poll", rc);
1089 goto error;
1090 }
1091
1092 dev->poll_protocols = protocols;
1093
1094 return 0;
1095
1096error:
1097 pn533_poll_reset_mod_list(dev);
1098 return rc;
1099}
1100
1101static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1102{
1103 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1104
1105 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1106
1107 if (!dev->poll_mod_count) {
1108 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1109 " running");
1110 return;
1111 }
1112
1113 /* An ack will cancel the last issued command (poll) */
1114 pn533_send_ack(dev, GFP_KERNEL);
1115
1116 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1117 usb_kill_urb(dev->in_urb);
1118}
1119
1120static int pn533_activate_target_nfcdep(struct pn533 *dev)
1121{
1122 struct pn533_cmd_activate_param param;
1123 struct pn533_cmd_activate_response *resp;
1124 int rc;
1125
1126 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1127
1128 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1129
1130 param.tg = 1;
1131 param.next = 0;
1132 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1133 sizeof(struct pn533_cmd_activate_param));
1134 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1135
1136 pn533_tx_frame_finish(dev->out_frame);
1137
1138 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1139 dev->in_maxlen);
1140 if (rc)
1141 return rc;
1142
1143 resp = (struct pn533_cmd_activate_response *)
1144 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1145 rc = resp->status & PN533_CMD_RET_MASK;
1146 if (rc != PN533_CMD_RET_SUCCESS)
1147 return -EIO;
1148
1149 return 0;
1150}
1151
1152static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
1153 u32 protocol)
1154{
1155 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1156 int rc;
1157
1158 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1159 protocol);
1160
1161 if (dev->poll_mod_count) {
1162 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1163 " polling");
1164 return -EBUSY;
1165 }
1166
1167 if (dev->tgt_active_prot) {
1168 nfc_dev_err(&dev->interface->dev, "There is already an active"
1169 " target");
1170 return -EBUSY;
1171 }
1172
1173 if (!dev->tgt_available_prots) {
1174 nfc_dev_err(&dev->interface->dev, "There is no available target"
1175 " to activate");
1176 return -EINVAL;
1177 }
1178
1179 if (!(dev->tgt_available_prots & (1 << protocol))) {
1180 nfc_dev_err(&dev->interface->dev, "The target does not support"
1181 " the requested protocol %u", protocol);
1182 return -EINVAL;
1183 }
1184
1185 if (protocol == NFC_PROTO_NFC_DEP) {
1186 rc = pn533_activate_target_nfcdep(dev);
1187 if (rc) {
1188 nfc_dev_err(&dev->interface->dev, "Error %d when"
1189 " activating target with"
1190 " NFC_DEP protocol", rc);
1191 return rc;
1192 }
1193 }
1194
1195 dev->tgt_active_prot = protocol;
1196 dev->tgt_available_prots = 0;
1197
1198 return 0;
1199}
1200
1201static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
1202{
1203 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1204 u8 tg;
1205 u8 status;
1206 int rc;
1207
1208 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1209
1210 if (!dev->tgt_active_prot) {
1211 nfc_dev_err(&dev->interface->dev, "There is no active target");
1212 return;
1213 }
1214
1215 dev->tgt_active_prot = 0;
1216
1217 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1218
1219 tg = 1;
1220 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1221 dev->out_frame->datalen += sizeof(u8);
1222
1223 pn533_tx_frame_finish(dev->out_frame);
1224
1225 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1226 dev->in_maxlen);
1227 if (rc) {
1228 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1229 " command to the controller");
1230 return;
1231 }
1232
1233 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1234 rc = status & PN533_CMD_RET_MASK;
1235 if (rc != PN533_CMD_RET_SUCCESS)
1236 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1237 " the target", rc);
1238
1239 return;
1240}
1241
1242#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1243#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1244
1245static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1246{
1247 int payload_len = skb->len;
1248 struct pn533_frame *out_frame;
1249 struct sk_buff *discarded;
1250 u8 tg;
1251
1252 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1253 payload_len);
1254
1255 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1256 /* TODO: Implement support to multi-part data exchange */
1257 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1258 " max allowed: %d",
1259 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1260 return -ENOSYS;
1261 }
1262
1263 /* Reserving header space */
1264 if (skb_cow_head(skb, PN533_CMD_DATAEXCH_HEAD_LEN)) {
1265 nfc_dev_err(&dev->interface->dev, "Error to add header data");
1266 return -ENOMEM;
1267 }
1268
1269 /* Reserving tail space, see pn533_tx_frame_finish */
1270 if (skb_cow_data(skb, PN533_FRAME_TAIL_SIZE, &discarded) < 0) {
1271 nfc_dev_err(&dev->interface->dev, "Error to add tail data");
1272 return -ENOMEM;
1273 }
1274
1275 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1276 out_frame = (struct pn533_frame *) skb->data;
1277
1278 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1279
1280 tg = 1;
1281 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1282 out_frame->datalen += sizeof(u8);
1283
1284 /* The data is already in the out_frame, just update the datalen */
1285 out_frame->datalen += payload_len;
1286
1287 pn533_tx_frame_finish(out_frame);
1288 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1289
1290 return 0;
1291}
1292
1293struct pn533_data_exchange_arg {
1294 struct sk_buff *skb_resp;
1295 struct sk_buff *skb_out;
1296 data_exchange_cb_t cb;
1297 void *cb_context;
1298};
1299
1300static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1301 u8 *params, int params_len)
1302{
1303 struct pn533_data_exchange_arg *arg = _arg;
1304 struct sk_buff *skb_resp = arg->skb_resp;
1305 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1306 int err = 0;
1307 u8 status;
1308 u8 cmd_ret;
1309
1310 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1311
1312 dev_kfree_skb_irq(arg->skb_out);
1313
1314 if (params_len < 0) { /* error */
1315 err = params_len;
1316 goto error;
1317 }
1318
1319 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1320
1321 status = params[0];
1322
1323 cmd_ret = status & PN533_CMD_RET_MASK;
1324 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1325 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1326 " exchanging data", cmd_ret);
1327 err = -EIO;
1328 goto error;
1329 }
1330
1331 if (status & PN533_CMD_MI_MASK) {
1332 /* TODO: Implement support to multi-part data exchange */
1333 nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
1334 " supported");
1335 /* Prevent the other messages from controller */
1336 pn533_send_ack(dev, GFP_ATOMIC);
1337 err = -ENOSYS;
1338 goto error;
1339 }
1340
1341 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1342 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1343
1344 arg->cb(arg->cb_context, skb_resp, 0);
1345 kfree(arg);
1346 return 0;
1347
1348error:
1349 dev_kfree_skb_irq(skb_resp);
1350 arg->cb(arg->cb_context, NULL, err);
1351 kfree(arg);
1352 return 0;
1353}
1354
1355int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
1356 struct sk_buff *skb,
1357 data_exchange_cb_t cb,
1358 void *cb_context)
1359{
1360 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1361 struct pn533_frame *out_frame, *in_frame;
1362 struct pn533_data_exchange_arg *arg;
1363 struct sk_buff *skb_resp;
1364 int skb_resp_len;
1365 int rc;
1366
1367 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1368
1369 if (!dev->tgt_active_prot) {
1370 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1371 " there is no active target");
1372 rc = -EINVAL;
1373 goto error;
1374 }
1375
1376 rc = pn533_data_exchange_tx_frame(dev, skb);
1377 if (rc)
1378 goto error;
1379
1380 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1381 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1382 PN533_FRAME_TAIL_SIZE;
1383
1384 skb_resp = nfc_alloc_skb(skb_resp_len, GFP_KERNEL);
1385 if (!skb_resp) {
1386 rc = -ENOMEM;
1387 goto error;
1388 }
1389
1390 in_frame = (struct pn533_frame *) skb_resp->data;
1391 out_frame = (struct pn533_frame *) skb->data;
1392
1393 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1394 if (!arg) {
1395 rc = -ENOMEM;
1396 goto free_skb_resp;
1397 }
1398
1399 arg->skb_resp = skb_resp;
1400 arg->skb_out = skb;
1401 arg->cb = cb;
1402 arg->cb_context = cb_context;
1403
1404 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1405 pn533_data_exchange_complete, arg,
1406 GFP_KERNEL);
1407 if (rc) {
1408 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1409 " perform data_exchange", rc);
1410 goto free_arg;
1411 }
1412
1413 return 0;
1414
1415free_arg:
1416 kfree(arg);
1417free_skb_resp:
1418 kfree_skb(skb_resp);
1419error:
1420 kfree_skb(skb);
1421 return rc;
1422}
1423
1424static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1425 u8 cfgdata_len)
1426{
1427 int rc;
1428 u8 *params;
1429
1430 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1431
1432 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1433
1434 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1435 params[0] = cfgitem;
1436 memcpy(&params[1], cfgdata, cfgdata_len);
1437 dev->out_frame->datalen += (1 + cfgdata_len);
1438
1439 pn533_tx_frame_finish(dev->out_frame);
1440
1441 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1442 dev->in_maxlen);
1443
1444 return rc;
1445}
1446
1447struct nfc_ops pn533_nfc_ops = {
1448 .start_poll = pn533_start_poll,
1449 .stop_poll = pn533_stop_poll,
1450 .activate_target = pn533_activate_target,
1451 .deactivate_target = pn533_deactivate_target,
1452 .data_exchange = pn533_data_exchange,
1453};
1454
1455static int pn533_probe(struct usb_interface *interface,
1456 const struct usb_device_id *id)
1457{
1458 struct pn533_fw_version *fw_ver;
1459 struct pn533 *dev;
1460 struct usb_host_interface *iface_desc;
1461 struct usb_endpoint_descriptor *endpoint;
1462 struct pn533_config_max_retries max_retries;
1463 int in_endpoint = 0;
1464 int out_endpoint = 0;
1465 int rc = -ENOMEM;
1466 int i;
1467 u32 protocols;
1468
1469 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1470 if (!dev)
1471 return -ENOMEM;
1472
1473 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1474 dev->interface = interface;
1475 sema_init(&dev->cmd_lock, 1);
1476
1477 iface_desc = interface->cur_altsetting;
1478 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1479 endpoint = &iface_desc->endpoint[i].desc;
1480
1481 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1482 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1483 in_endpoint = endpoint->bEndpointAddress;
1484 }
1485
1486 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1487 dev->out_maxlen =
1488 le16_to_cpu(endpoint->wMaxPacketSize);
1489 out_endpoint = endpoint->bEndpointAddress;
1490 }
1491 }
1492
1493 if (!in_endpoint || !out_endpoint) {
1494 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1495 " bulk-out endpoint");
1496 rc = -ENODEV;
1497 goto error;
1498 }
1499
1500 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1501 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1502 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1503 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1504
1505 if (!dev->in_frame || !dev->out_frame ||
1506 !dev->in_urb || !dev->out_urb)
1507 goto error;
1508
1509 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1510 usb_rcvbulkpipe(dev->udev, in_endpoint),
1511 NULL, 0, NULL, dev);
1512 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1513 usb_sndbulkpipe(dev->udev, out_endpoint),
1514 NULL, 0,
1515 pn533_send_complete, dev);
1516
1517 tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
1518
1519 usb_set_intfdata(interface, dev);
1520
1521 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
1522 pn533_tx_frame_finish(dev->out_frame);
1523
1524 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1525 dev->in_maxlen);
1526 if (rc)
1527 goto kill_tasklet;
1528
1529 fw_ver = (struct pn533_fw_version *)
1530 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1531 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
1532 " attached", fw_ver->ver, fw_ver->rev);
1533
1534 protocols = NFC_PROTO_JEWEL_MASK
1535 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
1536 | NFC_PROTO_ISO14443_MASK
1537 | NFC_PROTO_NFC_DEP_MASK;
1538
1539 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols);
1540 if (!dev->nfc_dev)
1541 goto kill_tasklet;
1542
1543 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
1544 nfc_set_drvdata(dev->nfc_dev, dev);
1545
1546 rc = nfc_register_device(dev->nfc_dev);
1547 if (rc)
1548 goto free_nfc_dev;
1549
1550 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
1551 max_retries.mx_rty_psl = 2;
1552 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
1553
1554 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
1555 (u8 *) &max_retries, sizeof(max_retries));
1556
1557 if (rc) {
1558 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
1559 " config");
1560 goto free_nfc_dev;
1561 }
1562
1563 return 0;
1564
1565free_nfc_dev:
1566 nfc_free_device(dev->nfc_dev);
1567kill_tasklet:
1568 tasklet_kill(&dev->tasklet);
1569error:
1570 kfree(dev->in_frame);
1571 usb_free_urb(dev->in_urb);
1572 kfree(dev->out_frame);
1573 usb_free_urb(dev->out_urb);
1574 kfree(dev);
1575 return rc;
1576}
1577
1578static void pn533_disconnect(struct usb_interface *interface)
1579{
1580 struct pn533 *dev;
1581
1582 dev = usb_get_intfdata(interface);
1583 usb_set_intfdata(interface, NULL);
1584
1585 nfc_unregister_device(dev->nfc_dev);
1586 nfc_free_device(dev->nfc_dev);
1587
1588 usb_kill_urb(dev->in_urb);
1589 usb_kill_urb(dev->out_urb);
1590
1591 tasklet_kill(&dev->tasklet);
1592
1593 kfree(dev->in_frame);
1594 usb_free_urb(dev->in_urb);
1595 kfree(dev->out_frame);
1596 usb_free_urb(dev->out_urb);
1597 kfree(dev);
1598
1599 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
1600}
1601
1602static struct usb_driver pn533_driver = {
1603 .name = "pn533",
1604 .probe = pn533_probe,
1605 .disconnect = pn533_disconnect,
1606 .id_table = pn533_table,
1607};
1608
1609static int __init pn533_init(void)
1610{
1611 int rc;
1612
1613 rc = usb_register(&pn533_driver);
1614 if (rc)
1615 err("usb_register failed. Error number %d", rc);
1616
1617 return rc;
1618}
1619
1620static void __exit pn533_exit(void)
1621{
1622 usb_deregister(&pn533_driver);
1623}
1624
1625module_init(pn533_init);
1626module_exit(pn533_exit);
1627
1628MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1629 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1630MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
1631MODULE_VERSION(VERSION);
1632MODULE_LICENSE("GPL");
diff --git a/drivers/nfc/pn544.c b/drivers/nfc/pn544.c
index 724f65d8f9e..91216e465e1 100644
--- a/drivers/nfc/pn544.c
+++ b/drivers/nfc/pn544.c
@@ -1,893 +1,400 @@
1/* 1/*
2 * Driver for the PN544 NFC chip. 2 * Copyright (C) 2010 Trusted Logic S.A.
3 * 3 *
4 * Copyright (C) Nokia Corporation 4 * This program is free software; you can redistribute it and/or modify
5 * 5 * it under the terms of the GNU General Public License as published by
6 * Author: Jari Vanhala <ext-jari.vanhala@nokia.com> 6 * the Free Software Foundation; either version 2 of the License, or
7 * Contact: Matti Aaltonen <matti.j.aaltonen@nokia.com> 7 * (at your option) any later version.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 * 8 *
13 * This program is distributed in the hope that it will be useful, 9 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 12 * GNU General Public License for more details.
17 * 13 *
18 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
21 */ 18 */
22 19
23#include <linux/completion.h> 20#include <linux/kernel.h>
24#include <linux/crc-ccitt.h> 21#include <linux/module.h>
22#include <linux/fs.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/i2c.h>
27#include <linux/irq.h>
28#include <linux/jiffies.h>
29#include <linux/uaccess.h>
25#include <linux/delay.h> 30#include <linux/delay.h>
26#include <linux/interrupt.h> 31#include <linux/interrupt.h>
27#include <linux/kernel.h> 32#include <linux/io.h>
33#include <linux/platform_device.h>
34#include <linux/gpio.h>
28#include <linux/miscdevice.h> 35#include <linux/miscdevice.h>
29#include <linux/module.h> 36#include <linux/spinlock.h>
30#include <linux/mutex.h>
31#include <linux/nfc/pn544.h> 37#include <linux/nfc/pn544.h>
32#include <linux/poll.h>
33#include <linux/regulator/consumer.h>
34#include <linux/serial_core.h> /* for TCGETS */
35#include <linux/slab.h>
36
37#define DRIVER_CARD "PN544 NFC"
38#define DRIVER_DESC "NFC driver for PN544"
39
40static struct i2c_device_id pn544_id_table[] = {
41 { PN544_DRIVER_NAME, 0 },
42 { }
43};
44MODULE_DEVICE_TABLE(i2c, pn544_id_table);
45
46#define HCI_MODE 0
47#define FW_MODE 1
48 38
49enum pn544_state { 39#define MAX_BUFFER_SIZE 512
50 PN544_ST_COLD, 40
51 PN544_ST_FW_READY, 41struct pn544_dev {
52 PN544_ST_READY, 42 wait_queue_head_t read_wq;
43 struct mutex read_mutex;
44 struct i2c_client *client;
45 struct miscdevice pn544_device;
46 unsigned int ven_gpio;
47 unsigned int firm_gpio;
48 unsigned int irq_gpio;
49 bool irq_enabled;
50 spinlock_t irq_enabled_lock;
53}; 51};
54 52
55enum pn544_irq { 53static void pn544_disable_irq(struct pn544_dev *pn544_dev)
56 PN544_NONE,
57 PN544_INT,
58};
59
60struct pn544_info {
61 struct miscdevice miscdev;
62 struct i2c_client *i2c_dev;
63 struct regulator_bulk_data regs[3];
64
65 enum pn544_state state;
66 wait_queue_head_t read_wait;
67 loff_t read_offset;
68 enum pn544_irq read_irq;
69 struct mutex read_mutex; /* Serialize read_irq access */
70 struct mutex mutex; /* Serialize info struct access */
71 u8 *buf;
72 size_t buflen;
73};
74
75static const char reg_vdd_io[] = "Vdd_IO";
76static const char reg_vbat[] = "VBat";
77static const char reg_vsim[] = "VSim";
78
79/* sysfs interface */
80static ssize_t pn544_test(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 struct pn544_info *info = dev_get_drvdata(dev);
84 struct i2c_client *client = info->i2c_dev;
85 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
86
87 return snprintf(buf, PAGE_SIZE, "%d\n", pdata->test());
88}
89
90static int pn544_enable(struct pn544_info *info, int mode)
91{
92 struct pn544_nfc_platform_data *pdata;
93 struct i2c_client *client = info->i2c_dev;
94
95 int r;
96
97 r = regulator_bulk_enable(ARRAY_SIZE(info->regs), info->regs);
98 if (r < 0)
99 return r;
100
101 pdata = client->dev.platform_data;
102 info->read_irq = PN544_NONE;
103 if (pdata->enable)
104 pdata->enable(mode);
105
106 if (mode) {
107 info->state = PN544_ST_FW_READY;
108 dev_dbg(&client->dev, "now in FW-mode\n");
109 } else {
110 info->state = PN544_ST_READY;
111 dev_dbg(&client->dev, "now in HCI-mode\n");
112 }
113
114 usleep_range(10000, 15000);
115
116 return 0;
117}
118
119static void pn544_disable(struct pn544_info *info)
120{
121 struct pn544_nfc_platform_data *pdata;
122 struct i2c_client *client = info->i2c_dev;
123
124 pdata = client->dev.platform_data;
125 if (pdata->disable)
126 pdata->disable();
127
128 info->state = PN544_ST_COLD;
129
130 dev_dbg(&client->dev, "Now in OFF-mode\n");
131
132 msleep(PN544_RESETVEN_TIME);
133
134 info->read_irq = PN544_NONE;
135 regulator_bulk_disable(ARRAY_SIZE(info->regs), info->regs);
136}
137
138static int check_crc(u8 *buf, int buflen)
139{ 54{
140 u8 len; 55 unsigned long flags;
141 u16 crc;
142
143 len = buf[0] + 1;
144 if (len < 4 || len != buflen || len > PN544_MSG_MAX_SIZE) {
145 pr_err(PN544_DRIVER_NAME
146 ": CRC; corrupt packet len %u (%d)\n", len, buflen);
147 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
148 16, 2, buf, buflen, false);
149 return -EPERM;
150 }
151 crc = crc_ccitt(0xffff, buf, len - 2);
152 crc = ~crc;
153 56
154 if (buf[len-2] != (crc & 0xff) || buf[len-1] != (crc >> 8)) { 57 spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags);
155 pr_err(PN544_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n", 58 if (pn544_dev->irq_enabled) {
156 crc, buf[len-1], buf[len-2]); 59 disable_irq_nosync(pn544_dev->client->irq);
157 60 pn544_dev->irq_enabled = false;
158 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
159 16, 2, buf, buflen, false);
160 return -EPERM;
161 } 61 }
162 return 0; 62 spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags);
163} 63}
164 64
165static int pn544_i2c_write(struct i2c_client *client, u8 *buf, int len) 65static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
166{ 66{
167 int r; 67 struct pn544_dev *pn544_dev = dev_id;
168
169 if (len < 4 || len != (buf[0] + 1)) {
170 dev_err(&client->dev, "%s: Illegal message length: %d\n",
171 __func__, len);
172 return -EINVAL;
173 }
174
175 if (check_crc(buf, len))
176 return -EINVAL;
177 68
178 usleep_range(3000, 6000); 69 pn544_disable_irq(pn544_dev);
179
180 r = i2c_master_send(client, buf, len);
181 dev_dbg(&client->dev, "send: %d\n", r);
182
183 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
184 usleep_range(6000, 10000);
185 r = i2c_master_send(client, buf, len);
186 dev_dbg(&client->dev, "send2: %d\n", r);
187 }
188 70
189 if (r != len) 71 /* Wake up waiting readers */
190 return -EREMOTEIO; 72 wake_up(&pn544_dev->read_wq);
191 73
192 return r; 74 return IRQ_HANDLED;
193} 75}
194 76
195static int pn544_i2c_read(struct i2c_client *client, u8 *buf, int buflen) 77static ssize_t pn544_dev_read(struct file *filp, char __user *buf,
78 size_t count, loff_t *offset)
196{ 79{
197 int r; 80 struct pn544_dev *pn544_dev = filp->private_data;
198 u8 len; 81 char tmp[MAX_BUFFER_SIZE];
199 82 int ret;
200 /*
201 * You could read a packet in one go, but then you'd need to read
202 * max size and rest would be 0xff fill, so we do split reads.
203 */
204 r = i2c_master_recv(client, &len, 1);
205 dev_dbg(&client->dev, "recv1: %d\n", r);
206
207 if (r != 1)
208 return -EREMOTEIO;
209
210 if (len < PN544_LLC_HCI_OVERHEAD)
211 len = PN544_LLC_HCI_OVERHEAD;
212 else if (len > (PN544_MSG_MAX_SIZE - 1))
213 len = PN544_MSG_MAX_SIZE - 1;
214
215 if (1 + len > buflen) /* len+(data+crc16) */
216 return -EMSGSIZE;
217
218 buf[0] = len;
219 83
220 r = i2c_master_recv(client, buf + 1, len); 84 if (count > MAX_BUFFER_SIZE)
221 dev_dbg(&client->dev, "recv2: %d\n", r); 85 count = MAX_BUFFER_SIZE;
222 86
223 if (r != len) 87 pr_debug("%s : reading %zu bytes.\n", __func__, count);
224 return -EREMOTEIO;
225 88
226 usleep_range(3000, 6000); 89 mutex_lock(&pn544_dev->read_mutex);
227 90
228 return r + 1; 91 if (!gpio_get_value(pn544_dev->irq_gpio)) {
229} 92 if (filp->f_flags & O_NONBLOCK) {
93 ret = -EAGAIN;
94 goto fail;
95 }
230 96
231static int pn544_fw_write(struct i2c_client *client, u8 *buf, int len) 97 pn544_dev->irq_enabled = true;
232{ 98 enable_irq(pn544_dev->client->irq);
233 int r; 99 ret = wait_event_interruptible(pn544_dev->read_wq,
100 gpio_get_value(pn544_dev->irq_gpio));
234 101
235 dev_dbg(&client->dev, "%s\n", __func__); 102 pn544_disable_irq(pn544_dev);
236 103
237 if (len < PN544_FW_HEADER_SIZE || 104 if (ret)
238 (PN544_FW_HEADER_SIZE + (buf[1] << 8) + buf[2]) != len) 105 goto fail;
239 return -EINVAL;
240 106
241 r = i2c_master_send(client, buf, len);
242 dev_dbg(&client->dev, "fw send: %d\n", r);
243
244 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
245 usleep_range(6000, 10000);
246 r = i2c_master_send(client, buf, len);
247 dev_dbg(&client->dev, "fw send2: %d\n", r);
248 } 107 }
249 108
250 if (r != len) 109 /* Read data */
251 return -EREMOTEIO; 110 ret = i2c_master_recv(pn544_dev->client, tmp, count);
252
253 return r;
254}
255
256static int pn544_fw_read(struct i2c_client *client, u8 *buf, int buflen)
257{
258 int r, len;
259
260 if (buflen < PN544_FW_HEADER_SIZE)
261 return -EINVAL;
262
263 r = i2c_master_recv(client, buf, PN544_FW_HEADER_SIZE);
264 dev_dbg(&client->dev, "FW recv1: %d\n", r);
265
266 if (r < 0)
267 return r;
268
269 if (r < PN544_FW_HEADER_SIZE)
270 return -EINVAL;
271
272 len = (buf[1] << 8) + buf[2];
273 if (len == 0) /* just header, no additional data */
274 return r;
275
276 if (len > buflen - PN544_FW_HEADER_SIZE)
277 return -EMSGSIZE;
278
279 r = i2c_master_recv(client, buf + PN544_FW_HEADER_SIZE, len);
280 dev_dbg(&client->dev, "fw recv2: %d\n", r);
281
282 if (r != len)
283 return -EINVAL;
284
285 return r + PN544_FW_HEADER_SIZE;
286}
287
288static irqreturn_t pn544_irq_thread_fn(int irq, void *dev_id)
289{
290 struct pn544_info *info = dev_id;
291 struct i2c_client *client = info->i2c_dev;
292
293 BUG_ON(!info);
294 BUG_ON(irq != info->i2c_dev->irq);
295
296 dev_dbg(&client->dev, "IRQ\n");
297
298 mutex_lock(&info->read_mutex);
299 info->read_irq = PN544_INT;
300 mutex_unlock(&info->read_mutex);
301
302 wake_up_interruptible(&info->read_wait);
303 111
304 return IRQ_HANDLED; 112 mutex_unlock(&pn544_dev->read_mutex);
305}
306
307static enum pn544_irq pn544_irq_state(struct pn544_info *info)
308{
309 enum pn544_irq irq;
310
311 mutex_lock(&info->read_mutex);
312 irq = info->read_irq;
313 mutex_unlock(&info->read_mutex);
314 /*
315 * XXX: should we check GPIO-line status directly?
316 * return pdata->irq_status() ? PN544_INT : PN544_NONE;
317 */
318
319 return irq;
320}
321 113
322static ssize_t pn544_read(struct file *file, char __user *buf, 114 /* pn544 seems to be slow in handling I2C read requests
323 size_t count, loff_t *offset) 115 * so add 1ms delay after recv operation */
324{ 116 udelay(1000);
325 struct pn544_info *info = container_of(file->private_data,
326 struct pn544_info, miscdev);
327 struct i2c_client *client = info->i2c_dev;
328 enum pn544_irq irq;
329 size_t len;
330 int r = 0;
331
332 dev_dbg(&client->dev, "%s: info: %p, count: %zu\n", __func__,
333 info, count);
334 117
335 mutex_lock(&info->mutex); 118 if (ret < 0) {
336 119 pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
337 if (info->state == PN544_ST_COLD) { 120 return ret;
338 r = -ENODEV;
339 goto out;
340 } 121 }
341 122 if (ret > count) {
342 irq = pn544_irq_state(info); 123 pr_err("%s: received too many bytes from i2c (%d)\n",
343 if (irq == PN544_NONE) { 124 __func__, ret);
344 if (file->f_flags & O_NONBLOCK) { 125 return -EIO;
345 r = -EAGAIN;
346 goto out;
347 }
348
349 if (wait_event_interruptible(info->read_wait,
350 (info->read_irq == PN544_INT))) {
351 r = -ERESTARTSYS;
352 goto out;
353 }
354 } 126 }
355 127 if (copy_to_user(buf, tmp, ret)) {
356 if (info->state == PN544_ST_FW_READY) { 128 pr_warning("%s : failed to copy to user space\n", __func__);
357 len = min(count, info->buflen); 129 return -EFAULT;
358
359 mutex_lock(&info->read_mutex);
360 r = pn544_fw_read(info->i2c_dev, info->buf, len);
361 info->read_irq = PN544_NONE;
362 mutex_unlock(&info->read_mutex);
363
364 if (r < 0) {
365 dev_err(&info->i2c_dev->dev, "FW read failed: %d\n", r);
366 goto out;
367 }
368
369 print_hex_dump(KERN_DEBUG, "FW read: ", DUMP_PREFIX_NONE,
370 16, 2, info->buf, r, false);
371
372 *offset += r;
373 if (copy_to_user(buf, info->buf, r)) {
374 r = -EFAULT;
375 goto out;
376 }
377 } else {
378 len = min(count, info->buflen);
379
380 mutex_lock(&info->read_mutex);
381 r = pn544_i2c_read(info->i2c_dev, info->buf, len);
382 info->read_irq = PN544_NONE;
383 mutex_unlock(&info->read_mutex);
384
385 if (r < 0) {
386 dev_err(&info->i2c_dev->dev, "read failed (%d)\n", r);
387 goto out;
388 }
389 print_hex_dump(KERN_DEBUG, "read: ", DUMP_PREFIX_NONE,
390 16, 2, info->buf, r, false);
391
392 *offset += r;
393 if (copy_to_user(buf, info->buf, r)) {
394 r = -EFAULT;
395 goto out;
396 }
397 } 130 }
131 return ret;
398 132
399out: 133fail:
400 mutex_unlock(&info->mutex); 134 mutex_unlock(&pn544_dev->read_mutex);
401 135 return ret;
402 return r;
403} 136}
404 137
405static unsigned int pn544_poll(struct file *file, poll_table *wait) 138static ssize_t pn544_dev_write(struct file *filp, const char __user *buf,
139 size_t count, loff_t *offset)
406{ 140{
407 struct pn544_info *info = container_of(file->private_data, 141 struct pn544_dev *pn544_dev;
408 struct pn544_info, miscdev); 142 char tmp[MAX_BUFFER_SIZE];
409 struct i2c_client *client = info->i2c_dev; 143 int ret;
410 int r = 0;
411 144
412 dev_dbg(&client->dev, "%s: info: %p\n", __func__, info); 145 pn544_dev = filp->private_data;
413 146
414 mutex_lock(&info->mutex); 147 if (count > MAX_BUFFER_SIZE)
148 count = MAX_BUFFER_SIZE;
415 149
416 if (info->state == PN544_ST_COLD) { 150 if (copy_from_user(tmp, buf, count)) {
417 r = -ENODEV; 151 pr_err("%s : failed to copy from user space\n", __func__);
418 goto out; 152 return -EFAULT;
419 } 153 }
420 154
421 poll_wait(file, &info->read_wait, wait); 155 pr_debug("%s : writing %zu bytes.\n", __func__, count);
422 156 /* Write data */
423 if (pn544_irq_state(info) == PN544_INT) { 157 ret = i2c_master_send(pn544_dev->client, tmp, count);
424 r = POLLIN | POLLRDNORM; 158 if (ret != count) {
425 goto out; 159 pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
160 ret = -EIO;
426 } 161 }
427out:
428 mutex_unlock(&info->mutex);
429 162
430 return r; 163 /* pn544 seems to be slow in handling I2C write requests
164 * so add 1ms delay after I2C send oparation */
165 udelay(1000);
166
167 return ret;
431} 168}
432 169
433static ssize_t pn544_write(struct file *file, const char __user *buf, 170static int pn544_dev_open(struct inode *inode, struct file *filp)
434 size_t count, loff_t *ppos)
435{ 171{
436 struct pn544_info *info = container_of(file->private_data, 172 struct pn544_dev *pn544_dev = container_of(filp->private_data,
437 struct pn544_info, miscdev); 173 struct pn544_dev,
438 struct i2c_client *client = info->i2c_dev; 174 pn544_device);
439 ssize_t len;
440 int r;
441
442 dev_dbg(&client->dev, "%s: info: %p, count %zu\n", __func__,
443 info, count);
444
445 mutex_lock(&info->mutex);
446
447 if (info->state == PN544_ST_COLD) {
448 r = -ENODEV;
449 goto out;
450 }
451
452 /*
453 * XXX: should we detect rset-writes and clean possible
454 * read_irq state
455 */
456 if (info->state == PN544_ST_FW_READY) {
457 size_t fw_len;
458
459 if (count < PN544_FW_HEADER_SIZE) {
460 r = -EINVAL;
461 goto out;
462 }
463
464 len = min(count, info->buflen);
465 if (copy_from_user(info->buf, buf, len)) {
466 r = -EFAULT;
467 goto out;
468 }
469
470 print_hex_dump(KERN_DEBUG, "FW write: ", DUMP_PREFIX_NONE,
471 16, 2, info->buf, len, false);
472
473 fw_len = PN544_FW_HEADER_SIZE + (info->buf[1] << 8) +
474 info->buf[2];
475
476 if (len > fw_len) /* 1 msg at a time */
477 len = fw_len;
478 175
479 r = pn544_fw_write(info->i2c_dev, info->buf, len); 176 filp->private_data = pn544_dev;
480 } else {
481 if (count < PN544_LLC_MIN_SIZE) {
482 r = -EINVAL;
483 goto out;
484 }
485
486 len = min(count, info->buflen);
487 if (copy_from_user(info->buf, buf, len)) {
488 r = -EFAULT;
489 goto out;
490 }
491
492 print_hex_dump(KERN_DEBUG, "write: ", DUMP_PREFIX_NONE,
493 16, 2, info->buf, len, false);
494
495 if (len > (info->buf[0] + 1)) /* 1 msg at a time */
496 len = info->buf[0] + 1;
497
498 r = pn544_i2c_write(info->i2c_dev, info->buf, len);
499 }
500out:
501 mutex_unlock(&info->mutex);
502 177
503 return r; 178 pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));
504 179
180 return 0;
505} 181}
506 182
507static long pn544_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 183static long pn544_dev_ioctl(struct file *filp, unsigned int cmd,
184 unsigned long arg)
508{ 185{
509 struct pn544_info *info = container_of(file->private_data, 186 struct pn544_dev *pn544_dev = filp->private_data;
510 struct pn544_info, miscdev);
511 struct i2c_client *client = info->i2c_dev;
512 struct pn544_nfc_platform_data *pdata;
513 unsigned int val;
514 int r = 0;
515
516 dev_dbg(&client->dev, "%s: info: %p, cmd: 0x%x\n", __func__, info, cmd);
517 187
518 mutex_lock(&info->mutex);
519
520 if (info->state == PN544_ST_COLD) {
521 r = -ENODEV;
522 goto out;
523 }
524
525 pdata = info->i2c_dev->dev.platform_data;
526 switch (cmd) { 188 switch (cmd) {
527 case PN544_GET_FW_MODE: 189 case PN544_SET_PWR:
528 dev_dbg(&client->dev, "%s: PN544_GET_FW_MODE\n", __func__); 190 if (arg == 2) {
529 191 /* power on with firmware download (requires hw reset)
530 val = (info->state == PN544_ST_FW_READY); 192 */
531 if (copy_to_user((void __user *)arg, &val, sizeof(val))) { 193 pr_info("%s power on with firmware\n", __func__);
532 r = -EFAULT; 194 gpio_set_value(pn544_dev->ven_gpio, 1);
533 goto out; 195 msleep(20);
534 } 196 if (pn544_dev->firm_gpio)
535 197 gpio_set_value(pn544_dev->firm_gpio, 1);
536 break; 198 msleep(20);
537 199 gpio_set_value(pn544_dev->ven_gpio, 0);
538 case PN544_SET_FW_MODE: 200 msleep(100);
539 dev_dbg(&client->dev, "%s: PN544_SET_FW_MODE\n", __func__); 201 gpio_set_value(pn544_dev->ven_gpio, 1);
540 202 msleep(20);
541 if (copy_from_user(&val, (void __user *)arg, sizeof(val))) { 203 } else if (arg == 1) {
542 r = -EFAULT; 204 /* power on */
543 goto out; 205 pr_info("%s power on\n", __func__);
544 } 206 if (pn544_dev->firm_gpio)
545 207 gpio_set_value(pn544_dev->firm_gpio, 0);
546 if (val) { 208 gpio_set_value(pn544_dev->ven_gpio, 1);
547 if (info->state == PN544_ST_FW_READY) 209 msleep(100);
548 break; 210 } else if (arg == 0) {
549 211 /* power off */
550 pn544_disable(info); 212 pr_info("%s power off\n", __func__);
551 r = pn544_enable(info, FW_MODE); 213 if (pn544_dev->firm_gpio)
552 if (r < 0) 214 gpio_set_value(pn544_dev->firm_gpio, 0);
553 goto out; 215 gpio_set_value(pn544_dev->ven_gpio, 0);
216 msleep(100);
554 } else { 217 } else {
555 if (info->state == PN544_ST_READY) 218 pr_err("%s bad arg %lu\n", __func__, arg);
556 break; 219 return -EINVAL;
557 pn544_disable(info);
558 r = pn544_enable(info, HCI_MODE);
559 if (r < 0)
560 goto out;
561 } 220 }
562 file->f_pos = info->read_offset;
563 break; 221 break;
564
565 case TCGETS:
566 dev_dbg(&client->dev, "%s: TCGETS\n", __func__);
567
568 r = -ENOIOCTLCMD;
569 break;
570
571 default: 222 default:
572 dev_err(&client->dev, "Unknown ioctl 0x%x\n", cmd); 223 pr_err("%s bad ioctl %u\n", __func__, cmd);
573 r = -ENOIOCTLCMD; 224 return -EINVAL;
574 break;
575 }
576
577out:
578 mutex_unlock(&info->mutex);
579
580 return r;
581}
582
583static int pn544_open(struct inode *inode, struct file *file)
584{
585 struct pn544_info *info = container_of(file->private_data,
586 struct pn544_info, miscdev);
587 struct i2c_client *client = info->i2c_dev;
588 int r = 0;
589
590 dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__,
591 info, info->i2c_dev);
592
593 mutex_lock(&info->mutex);
594
595 /*
596 * Only 1 at a time.
597 * XXX: maybe user (counter) would work better
598 */
599 if (info->state != PN544_ST_COLD) {
600 r = -EBUSY;
601 goto out;
602 } 225 }
603 226
604 file->f_pos = info->read_offset;
605 r = pn544_enable(info, HCI_MODE);
606
607out:
608 mutex_unlock(&info->mutex);
609 return r;
610}
611
612static int pn544_close(struct inode *inode, struct file *file)
613{
614 struct pn544_info *info = container_of(file->private_data,
615 struct pn544_info, miscdev);
616 struct i2c_client *client = info->i2c_dev;
617
618 dev_dbg(&client->dev, "%s: info: %p, client %p\n",
619 __func__, info, info->i2c_dev);
620
621 mutex_lock(&info->mutex);
622 pn544_disable(info);
623 mutex_unlock(&info->mutex);
624
625 return 0; 227 return 0;
626} 228}
627 229
628static const struct file_operations pn544_fops = { 230static const struct file_operations pn544_dev_fops = {
629 .owner = THIS_MODULE, 231 .owner = THIS_MODULE,
630 .llseek = no_llseek, 232 .llseek = no_llseek,
631 .read = pn544_read, 233 .read = pn544_dev_read,
632 .write = pn544_write, 234 .write = pn544_dev_write,
633 .poll = pn544_poll, 235 .open = pn544_dev_open,
634 .open = pn544_open, 236 .unlocked_ioctl = pn544_dev_ioctl,
635 .release = pn544_close,
636 .unlocked_ioctl = pn544_ioctl,
637}; 237};
638 238
639#ifdef CONFIG_PM 239static int pn544_probe(struct i2c_client *client,
640static int pn544_suspend(struct device *dev) 240 const struct i2c_device_id *id)
641{
642 struct i2c_client *client = to_i2c_client(dev);
643 struct pn544_info *info;
644 int r = 0;
645
646 dev_info(&client->dev, "***\n%s: client %p\n***\n", __func__, client);
647
648 info = i2c_get_clientdata(client);
649 dev_info(&client->dev, "%s: info: %p, client %p\n", __func__,
650 info, client);
651
652 mutex_lock(&info->mutex);
653
654 switch (info->state) {
655 case PN544_ST_FW_READY:
656 /* Do not suspend while upgrading FW, please! */
657 r = -EPERM;
658 break;
659
660 case PN544_ST_READY:
661 /*
662 * CHECK: Device should be in standby-mode. No way to check?
663 * Allowing low power mode for the regulator is potentially
664 * dangerous if pn544 does not go to suspension.
665 */
666 break;
667
668 case PN544_ST_COLD:
669 break;
670 };
671
672 mutex_unlock(&info->mutex);
673 return r;
674}
675
676static int pn544_resume(struct device *dev)
677{
678 struct i2c_client *client = to_i2c_client(dev);
679 struct pn544_info *info = i2c_get_clientdata(client);
680 int r = 0;
681
682 dev_dbg(&client->dev, "%s: info: %p, client %p\n", __func__,
683 info, client);
684
685 mutex_lock(&info->mutex);
686
687 switch (info->state) {
688 case PN544_ST_READY:
689 /*
690 * CHECK: If regulator low power mode is allowed in
691 * pn544_suspend, we should go back to normal mode
692 * here.
693 */
694 break;
695
696 case PN544_ST_COLD:
697 break;
698
699 case PN544_ST_FW_READY:
700 break;
701 };
702
703 mutex_unlock(&info->mutex);
704
705 return r;
706}
707
708static SIMPLE_DEV_PM_OPS(pn544_pm_ops, pn544_suspend, pn544_resume);
709#endif
710
711static struct device_attribute pn544_attr =
712 __ATTR(nfc_test, S_IRUGO, pn544_test, NULL);
713
714static int __devinit pn544_probe(struct i2c_client *client,
715 const struct i2c_device_id *id)
716{ 241{
717 struct pn544_info *info; 242 int ret;
718 struct pn544_nfc_platform_data *pdata; 243 struct pn544_i2c_platform_data *platform_data;
719 int r = 0; 244 struct pn544_dev *pn544_dev;
720 245
721 dev_dbg(&client->dev, "%s\n", __func__); 246 platform_data = client->dev.platform_data;
722 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
723 247
724 /* private data allocation */ 248 if (platform_data == NULL) {
725 info = kzalloc(sizeof(struct pn544_info), GFP_KERNEL); 249 pr_err("%s : nfc probe fail\n", __func__);
726 if (!info) { 250 return -ENODEV;
727 dev_err(&client->dev,
728 "Cannot allocate memory for pn544_info.\n");
729 r = -ENOMEM;
730 goto err_info_alloc;
731 } 251 }
732 252
733 info->buflen = max(PN544_MSG_MAX_SIZE, PN544_MAX_I2C_TRANSFER); 253 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
734 info->buf = kzalloc(info->buflen, GFP_KERNEL); 254 pr_err("%s : need I2C_FUNC_I2C\n", __func__);
735 if (!info->buf) { 255 return -ENODEV;
736 dev_err(&client->dev,
737 "Cannot allocate memory for pn544_info->buf.\n");
738 r = -ENOMEM;
739 goto err_buf_alloc;
740 } 256 }
741 257
742 info->regs[0].supply = reg_vdd_io; 258 ret = gpio_request(platform_data->irq_gpio, "nfc_int");
743 info->regs[1].supply = reg_vbat; 259 if (ret)
744 info->regs[2].supply = reg_vsim; 260 return -ENODEV;
745 r = regulator_bulk_get(&client->dev, ARRAY_SIZE(info->regs), 261 ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
746 info->regs); 262 if (ret)
747 if (r < 0) 263 goto err_ven;
748 goto err_kmalloc; 264 if (platform_data->firm_gpio) {
749 265 ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
750 info->i2c_dev = client; 266 if (ret)
751 info->state = PN544_ST_COLD; 267 goto err_firm;
752 info->read_irq = PN544_NONE;
753 mutex_init(&info->read_mutex);
754 mutex_init(&info->mutex);
755 init_waitqueue_head(&info->read_wait);
756 i2c_set_clientdata(client, info);
757 pdata = client->dev.platform_data;
758 if (!pdata) {
759 dev_err(&client->dev, "No platform data\n");
760 r = -EINVAL;
761 goto err_reg;
762 } 268 }
763 269
764 if (!pdata->request_resources) { 270 pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL);
765 dev_err(&client->dev, "request_resources() missing\n"); 271 if (pn544_dev == NULL) {
766 r = -EINVAL; 272 dev_err(&client->dev,
767 goto err_reg; 273 "failed to allocate memory for module data\n");
274 ret = -ENOMEM;
275 goto err_exit;
768 } 276 }
769 277
770 r = pdata->request_resources(client); 278 pn544_dev->irq_gpio = platform_data->irq_gpio;
771 if (r) { 279 pn544_dev->ven_gpio = platform_data->ven_gpio;
772 dev_err(&client->dev, "Cannot get platform resources\n"); 280 pn544_dev->firm_gpio = platform_data->firm_gpio;
773 goto err_reg; 281 pn544_dev->client = client;
774 }
775 282
776 r = request_threaded_irq(client->irq, NULL, pn544_irq_thread_fn, 283 ret = gpio_direction_input(pn544_dev->irq_gpio);
777 IRQF_TRIGGER_RISING, PN544_DRIVER_NAME, 284 if (ret < 0) {
778 info); 285 pr_err("%s :not able to set irq_gpio as input\n", __func__);
779 if (r < 0) { 286 goto err_ven;
780 dev_err(&client->dev, "Unable to register IRQ handler\n");
781 goto err_res;
782 } 287 }
783 288 ret = gpio_direction_output(pn544_dev->ven_gpio, 0);
784 /* If we don't have the test we don't need the sysfs file */ 289 if (ret < 0) {
785 if (pdata->test) { 290 pr_err("%s : not able to set ven_gpio as output\n", __func__);
786 r = device_create_file(&client->dev, &pn544_attr); 291 goto err_firm;
787 if (r) { 292 }
788 dev_err(&client->dev, 293 if (platform_data->firm_gpio) {
789 "sysfs registration failed, error %d\n", r); 294 ret = gpio_direction_output(pn544_dev->firm_gpio, 0);
790 goto err_irq; 295 if (ret < 0) {
296 pr_err("%s : not able to set firm_gpio as output\n",
297 __func__);
298 goto err_exit;
791 } 299 }
792 } 300 }
793 301
794 info->miscdev.minor = MISC_DYNAMIC_MINOR; 302 /* init mutex and queues */
795 info->miscdev.name = PN544_DRIVER_NAME; 303 init_waitqueue_head(&pn544_dev->read_wq);
796 info->miscdev.fops = &pn544_fops; 304 mutex_init(&pn544_dev->read_mutex);
797 info->miscdev.parent = &client->dev; 305 spin_lock_init(&pn544_dev->irq_enabled_lock);
798 r = misc_register(&info->miscdev); 306
799 if (r < 0) { 307 pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
800 dev_err(&client->dev, "Device registration failed\n"); 308 pn544_dev->pn544_device.name = "pn544";
801 goto err_sysfs; 309 pn544_dev->pn544_device.fops = &pn544_dev_fops;
310
311 ret = misc_register(&pn544_dev->pn544_device);
312 if (ret) {
313 pr_err("%s : misc_register failed\n", __FILE__);
314 goto err_misc_register;
802 } 315 }
803 316
804 dev_dbg(&client->dev, "%s: info: %p, pdata %p, client %p\n", 317 /* request irq. the irq is set whenever the chip has data available
805 __func__, info, pdata, client); 318 * for reading. it is cleared when all data has been read.
319 */
320 pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
321 pn544_dev->irq_enabled = true;
322 ret = request_irq(client->irq, pn544_dev_irq_handler,
323 IRQF_TRIGGER_HIGH, client->name, pn544_dev);
324 if (ret) {
325 dev_err(&client->dev, "request_irq failed\n");
326 goto err_request_irq_failed;
327 }
328 pn544_disable_irq(pn544_dev);
329 i2c_set_clientdata(client, pn544_dev);
806 330
807 return 0; 331 return 0;
808 332
809err_sysfs: 333err_request_irq_failed:
810 if (pdata->test) 334 misc_deregister(&pn544_dev->pn544_device);
811 device_remove_file(&client->dev, &pn544_attr); 335err_misc_register:
812err_irq: 336 mutex_destroy(&pn544_dev->read_mutex);
813 free_irq(client->irq, info); 337 kfree(pn544_dev);
814err_res: 338err_exit:
815 if (pdata->free_resources) 339 if (pn544_dev->firm_gpio)
816 pdata->free_resources(); 340 gpio_free(platform_data->firm_gpio);
817err_reg: 341err_firm:
818 regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs); 342 gpio_free(platform_data->ven_gpio);
819err_kmalloc: 343err_ven:
820 kfree(info->buf); 344 gpio_free(platform_data->irq_gpio);
821err_buf_alloc: 345 return ret;
822 kfree(info);
823err_info_alloc:
824 return r;
825} 346}
826 347
827static __devexit int pn544_remove(struct i2c_client *client) 348static int pn544_remove(struct i2c_client *client)
828{ 349{
829 struct pn544_info *info = i2c_get_clientdata(client); 350 struct pn544_dev *pn544_dev;
830 struct pn544_nfc_platform_data *pdata = client->dev.platform_data; 351
831 352 pn544_dev = i2c_get_clientdata(client);
832 dev_dbg(&client->dev, "%s\n", __func__); 353 free_irq(client->irq, pn544_dev);
833 354 misc_deregister(&pn544_dev->pn544_device);
834 misc_deregister(&info->miscdev); 355 mutex_destroy(&pn544_dev->read_mutex);
835 if (pdata->test) 356 gpio_free(pn544_dev->irq_gpio);
836 device_remove_file(&client->dev, &pn544_attr); 357 gpio_free(pn544_dev->ven_gpio);
837 358 if (pn544_dev->firm_gpio)
838 if (info->state != PN544_ST_COLD) { 359 gpio_free(pn544_dev->firm_gpio);
839 if (pdata->disable) 360 kfree(pn544_dev);
840 pdata->disable();
841
842 info->read_irq = PN544_NONE;
843 }
844
845 free_irq(client->irq, info);
846 if (pdata->free_resources)
847 pdata->free_resources();
848
849 regulator_bulk_free(ARRAY_SIZE(info->regs), info->regs);
850 kfree(info->buf);
851 kfree(info);
852 361
853 return 0; 362 return 0;
854} 363}
855 364
365static const struct i2c_device_id pn544_id[] = {
366 { "pn544", 0 },
367 { }
368};
369
856static struct i2c_driver pn544_driver = { 370static struct i2c_driver pn544_driver = {
857 .driver = { 371 .id_table = pn544_id,
858 .name = PN544_DRIVER_NAME, 372 .probe = pn544_probe,
859#ifdef CONFIG_PM 373 .remove = pn544_remove,
860 .pm = &pn544_pm_ops, 374 .driver = {
861#endif 375 .owner = THIS_MODULE,
376 .name = "pn544",
862 }, 377 },
863 .probe = pn544_probe,
864 .id_table = pn544_id_table,
865 .remove = __devexit_p(pn544_remove),
866}; 378};
867 379
868static int __init pn544_init(void) 380/*
869{ 381 * module load/unload record keeping
870 int r; 382 */
871
872 pr_debug(DRIVER_DESC ": %s\n", __func__);
873
874 r = i2c_add_driver(&pn544_driver);
875 if (r) {
876 pr_err(PN544_DRIVER_NAME ": driver registration failed\n");
877 return r;
878 }
879 383
880 return 0; 384static int __init pn544_dev_init(void)
385{
386 pr_info("Loading pn544 driver\n");
387 return i2c_add_driver(&pn544_driver);
881} 388}
389module_init(pn544_dev_init);
882 390
883static void __exit pn544_exit(void) 391static void __exit pn544_dev_exit(void)
884{ 392{
393 pr_info("Unloading pn544 driver\n");
885 i2c_del_driver(&pn544_driver); 394 i2c_del_driver(&pn544_driver);
886 pr_info(DRIVER_DESC ", Exiting.\n");
887} 395}
396module_exit(pn544_dev_exit);
888 397
889module_init(pn544_init); 398MODULE_AUTHOR("Sylvain Fonteneau");
890module_exit(pn544_exit); 399MODULE_DESCRIPTION("NFC PN544 driver");
891
892MODULE_LICENSE("GPL"); 400MODULE_LICENSE("GPL");
893MODULE_DESCRIPTION(DRIVER_DESC);