blob: 02043dc430b959936e2c0f39de5c423c1533e245 [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
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081/* frame definitions */
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
83 254 (DATA)
84 2 (DCS, postamble) */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010085#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020088
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010089/*
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
92 */
93#define PN533_FRAME_MAX_PAYLOAD_LEN 263
94
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030095#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010096 PN533_FRAME_TAIL_LEN)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030097#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
100
101/* start of frame */
102#define PN533_SOF 0x00FF
103
104/* frame identifier: in/out/error */
105#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106#define PN533_DIR_OUT 0xD4
107#define PN533_DIR_IN 0xD5
108
109/* PN533 Commands */
110#define PN533_FRAME_CMD(f) (f->data[1])
111#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
113
114#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115#define PN533_CMD_RF_CONFIGURATION 0x32
116#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200117#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300118#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119#define PN533_CMD_IN_ATR 0x50
120#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100121#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122
Samuel Ortizad3823c2012-05-30 23:54:55 +0200123#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200124#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200125#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100126#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300128#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
129
130/* PN533 Return codes */
131#define PN533_CMD_RET_MASK 0x3F
132#define PN533_CMD_MI_MASK 0x40
133#define PN533_CMD_RET_SUCCESS 0x00
134
135struct pn533;
136
137typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
139
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100140typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143/* structs for pn533 commands */
144
145/* PN533_CMD_GET_FIRMWARE_VERSION */
146struct pn533_fw_version {
147 u8 ic;
148 u8 ver;
149 u8 rev;
150 u8 support;
151};
152
153/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200154#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200156#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200158#define PN533_CONFIG_TIMING_102 0xb
159#define PN533_CONFIG_TIMING_204 0xc
160#define PN533_CONFIG_TIMING_409 0xd
161#define PN533_CONFIG_TIMING_819 0xe
162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300163#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
165
166struct pn533_config_max_retries {
167 u8 mx_rty_atr;
168 u8 mx_rty_psl;
169 u8 mx_rty_passive_act;
170} __packed;
171
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200172struct pn533_config_timing {
173 u8 rfu;
174 u8 atr_res_timeout;
175 u8 dep_timeout;
176} __packed;
177
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300178/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
179
180/* felica commands opcode */
181#define PN533_FELICA_OPC_SENSF_REQ 0
182#define PN533_FELICA_OPC_SENSF_RES 1
183/* felica SENSF_REQ parameters */
184#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
188
189/* type B initiator_data values */
190#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
193
194union pn533_cmd_poll_initdata {
195 struct {
196 u8 afi;
197 u8 polling_method;
198 } __packed type_b;
199 struct {
200 u8 opcode;
201 __be16 sc;
202 u8 rc;
203 u8 tsn;
204 } __packed felica;
205};
206
207/* Poll modulations */
208enum {
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200214 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300215
216 __PN533_POLL_MOD_AFTER_LAST,
217};
218#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
219
220struct pn533_poll_modulations {
221 struct {
222 u8 maxtg;
223 u8 brty;
224 union pn533_cmd_poll_initdata initiator_data;
225 } __packed data;
226 u8 len;
227};
228
229const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 0,
234 },
235 .len = 2,
236 },
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
238 .data = {
239 .maxtg = 1,
240 .brty = 1,
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
245 .tsn = 0,
246 },
247 },
248 .len = 7,
249 },
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
251 .data = {
252 .maxtg = 1,
253 .brty = 2,
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
258 .tsn = 0,
259 },
260 },
261 .len = 7,
262 },
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 4,
267 },
268 .len = 2,
269 },
270 [PN533_POLL_MOD_847KBPS_B] = {
271 .data = {
272 .maxtg = 1,
273 .brty = 8,
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
276 .polling_method =
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
278 },
279 },
280 .len = 3,
281 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200282 [PN533_LISTEN_MOD] = {
283 .len = 0,
284 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300285};
286
287/* PN533_CMD_IN_ATR */
288
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300289struct pn533_cmd_activate_response {
290 u8 status;
291 u8 nfcid3t[10];
292 u8 didt;
293 u8 bst;
294 u8 brt;
295 u8 to;
296 u8 ppt;
297 /* optional */
298 u8 gt[];
299} __packed;
300
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100301struct pn533_cmd_jump_dep_response {
302 u8 status;
303 u8 tg;
304 u8 nfcid3t[10];
305 u8 didt;
306 u8 bst;
307 u8 brt;
308 u8 to;
309 u8 ppt;
310 /* optional */
311 u8 gt[];
312} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313
Samuel Ortizad3823c2012-05-30 23:54:55 +0200314
315/* PN533_TG_INIT_AS_TARGET */
316#define PN533_INIT_TARGET_PASSIVE 0x1
317#define PN533_INIT_TARGET_DEP 0x2
318
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200319#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321#define PN533_INIT_TARGET_RESP_DEP 0x4
322
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300323struct pn533 {
324 struct usb_device *udev;
325 struct usb_interface *interface;
326 struct nfc_dev *nfc_dev;
327
328 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300329 struct pn533_frame *out_frame;
330
331 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332 struct pn533_frame *in_frame;
333
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200334 struct sk_buff_head resp_q;
335
Samuel Ortiz4849f852012-04-10 19:43:17 +0200336 struct workqueue_struct *wq;
337 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200338 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200339 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200340 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200341 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200342 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200343 struct pn533_frame *wq_in_frame;
344 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200345 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300346
347 pn533_cmd_complete_t cmd_complete;
348 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100349 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200350 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300351 u8 cmd;
352
353 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
354 u8 poll_mod_count;
355 u8 poll_mod_curr;
356 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200357 u32 listen_protocols;
358
359 u8 *gb;
360 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300361
362 u8 tgt_available_prots;
363 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200364 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200365
366 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200367
368 struct list_head cmd_queue;
369 u8 cmd_pending;
370};
371
372struct pn533_cmd {
373 struct list_head queue;
374 struct pn533_frame *out_frame;
375 struct pn533_frame *in_frame;
376 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100377 u8 cmd_code;
378 struct sk_buff *req;
379 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200380 pn533_cmd_complete_t cmd_complete;
381 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300382};
383
384struct pn533_frame {
385 u8 preamble;
386 __be16 start_frame;
387 u8 datalen;
388 u8 datalen_checksum;
389 u8 data[];
390} __packed;
391
392/* The rule: value + checksum = 0 */
393static inline u8 pn533_checksum(u8 value)
394{
395 return ~value + 1;
396}
397
398/* The rule: sum(data elements) + checksum = 0 */
399static u8 pn533_data_checksum(u8 *data, int datalen)
400{
401 u8 sum = 0;
402 int i;
403
404 for (i = 0; i < datalen; i++)
405 sum += data[i];
406
407 return pn533_checksum(sum);
408}
409
410/**
411 * pn533_tx_frame_ack - create a ack frame
412 * @frame: The frame to be set as ack
413 *
414 * Ack is different type of standard frame. As a standard frame, it has
415 * preamble and start_frame. However the checksum of this frame must fail,
416 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
417 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
418 * After datalen_checksum field, the postamble is placed.
419 */
420static void pn533_tx_frame_ack(struct pn533_frame *frame)
421{
422 frame->preamble = 0;
423 frame->start_frame = cpu_to_be16(PN533_SOF);
424 frame->datalen = 0;
425 frame->datalen_checksum = 0xFF;
426 /* data[0] is used as postamble */
427 frame->data[0] = 0;
428}
429
430static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
431{
432 frame->preamble = 0;
433 frame->start_frame = cpu_to_be16(PN533_SOF);
434 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
435 PN533_FRAME_CMD(frame) = cmd;
436 frame->datalen = 2;
437}
438
439static void pn533_tx_frame_finish(struct pn533_frame *frame)
440{
441 frame->datalen_checksum = pn533_checksum(frame->datalen);
442
443 PN533_FRAME_CHECKSUM(frame) =
444 pn533_data_checksum(frame->data, frame->datalen);
445
446 PN533_FRAME_POSTAMBLE(frame) = 0;
447}
448
449static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
450{
451 u8 checksum;
452
453 if (frame->start_frame != cpu_to_be16(PN533_SOF))
454 return false;
455
456 checksum = pn533_checksum(frame->datalen);
457 if (checksum != frame->datalen_checksum)
458 return false;
459
460 checksum = pn533_data_checksum(frame->data, frame->datalen);
461 if (checksum != PN533_FRAME_CHECKSUM(frame))
462 return false;
463
464 return true;
465}
466
467static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
468{
469 if (frame->start_frame != cpu_to_be16(PN533_SOF))
470 return false;
471
472 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
473 return false;
474
475 return true;
476}
477
478static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
479{
480 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
481}
482
Samuel Ortiz4849f852012-04-10 19:43:17 +0200483
484static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300485{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200486 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200487 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300488 int rc;
489
Samuel Ortiz4849f852012-04-10 19:43:17 +0200490 in_frame = dev->wq_in_frame;
491
492 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300493 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200494 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300495 else
496 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
497 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
498 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
499
500 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200501 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300502}
503
504static void pn533_recv_response(struct urb *urb)
505{
506 struct pn533 *dev = urb->context;
507 struct pn533_frame *in_frame;
508
Samuel Ortiz4849f852012-04-10 19:43:17 +0200509 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300510
511 switch (urb->status) {
512 case 0:
513 /* success */
514 break;
515 case -ECONNRESET:
516 case -ENOENT:
517 case -ESHUTDOWN:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100518 nfc_dev_dbg(&dev->interface->dev,
519 "Urb shutting down with status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200520 dev->wq_in_error = urb->status;
521 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300522 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100523 nfc_dev_err(&dev->interface->dev,
524 "Nonzero urb status received: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200525 dev->wq_in_error = urb->status;
526 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300527 }
528
529 in_frame = dev->in_urb->transfer_buffer;
530
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100531 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
532 in_frame, PN533_FRAME_SIZE(in_frame), false);
533
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300534 if (!pn533_rx_frame_is_valid(in_frame)) {
535 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200536 dev->wq_in_error = -EIO;
537 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300538 }
539
540 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100541 nfc_dev_err(&dev->interface->dev,
542 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200543 dev->wq_in_error = -EIO;
544 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300545 }
546
547 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200548 dev->wq_in_error = 0;
549 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300550
Samuel Ortiz4849f852012-04-10 19:43:17 +0200551sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200552 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300553}
554
555static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
556{
557 dev->in_urb->complete = pn533_recv_response;
558
559 return usb_submit_urb(dev->in_urb, flags);
560}
561
562static void pn533_recv_ack(struct urb *urb)
563{
564 struct pn533 *dev = urb->context;
565 struct pn533_frame *in_frame;
566 int rc;
567
568 switch (urb->status) {
569 case 0:
570 /* success */
571 break;
572 case -ECONNRESET:
573 case -ENOENT:
574 case -ESHUTDOWN:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100575 nfc_dev_dbg(&dev->interface->dev,
576 "Urb shutting down with status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200577 dev->wq_in_error = urb->status;
578 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300579 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100580 nfc_dev_err(&dev->interface->dev,
581 "Nonzero urb status received: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200582 dev->wq_in_error = urb->status;
583 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300584 }
585
586 in_frame = dev->in_urb->transfer_buffer;
587
588 if (!pn533_rx_frame_is_ack(in_frame)) {
589 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200590 dev->wq_in_error = -EIO;
591 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300592 }
593
594 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
595
596 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
597 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100598 nfc_dev_err(&dev->interface->dev,
599 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200600 dev->wq_in_error = rc;
601 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 }
603
604 return;
605
Samuel Ortiz4849f852012-04-10 19:43:17 +0200606sched_wq:
607 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200608 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300609}
610
611static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
612{
613 dev->in_urb->complete = pn533_recv_ack;
614
615 return usb_submit_urb(dev->in_urb, flags);
616}
617
618static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
619{
620 int rc;
621
622 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
623
624 pn533_tx_frame_ack(dev->out_frame);
625
626 dev->out_urb->transfer_buffer = dev->out_frame;
627 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
628 rc = usb_submit_urb(dev->out_urb, flags);
629
630 return rc;
631}
632
633static int __pn533_send_cmd_frame_async(struct pn533 *dev,
634 struct pn533_frame *out_frame,
635 struct pn533_frame *in_frame,
636 int in_frame_len,
637 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100638 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300639{
640 int rc;
641
642 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100643 PN533_FRAME_CMD(out_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300644
645 dev->cmd = PN533_FRAME_CMD(out_frame);
646 dev->cmd_complete = cmd_complete;
647 dev->cmd_complete_arg = arg;
648
649 dev->out_urb->transfer_buffer = out_frame;
650 dev->out_urb->transfer_buffer_length =
651 PN533_FRAME_SIZE(out_frame);
652
653 dev->in_urb->transfer_buffer = in_frame;
654 dev->in_urb->transfer_buffer_length = in_frame_len;
655
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100656 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
657 out_frame, PN533_FRAME_SIZE(out_frame), false);
658
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100659 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300660 if (rc)
661 return rc;
662
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100663 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300664 if (rc)
665 goto error;
666
667 return 0;
668
669error:
670 usb_unlink_urb(dev->out_urb);
671 return rc;
672}
673
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100674static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
675{
676 struct pn533_frame *frame;
677 /* payload is already there, just update datalen */
678 int payload_len = skb->len;
679
680 skb_push(skb, PN533_FRAME_HEADER_LEN);
681 skb_put(skb, PN533_FRAME_TAIL_LEN);
682
683 frame = (struct pn533_frame *)skb->data;
684
685 pn533_tx_frame_init(frame, cmd_code);
686 frame->datalen += payload_len;
687 pn533_tx_frame_finish(frame);
688}
689
690struct pn533_send_async_complete_arg {
691 pn533_send_async_complete_t complete_cb;
692 void *complete_cb_context;
693 struct sk_buff *resp;
694 struct sk_buff *req;
695};
696
697static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
698 int params_len)
699{
700 struct pn533_send_async_complete_arg *arg = _arg;
701
702 struct sk_buff *req = arg->req;
703 struct sk_buff *resp = arg->resp;
704
705 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
706 int rc;
707
708 dev_kfree_skb(req);
709
710 if (params_len < 0) {
711 nfc_dev_err(&dev->interface->dev,
712 "Error %d when starting as a target",
713 params_len);
714
715 arg->complete_cb(dev, arg->complete_cb_context,
716 ERR_PTR(params_len));
717 rc = params_len;
718 dev_kfree_skb(resp);
719 goto out;
720 }
721
722 skb_put(resp, PN533_FRAME_SIZE(frame));
723 skb_pull(resp, PN533_FRAME_HEADER_LEN);
724 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
725
726 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
727
728out:
729 kfree(arg);
730 return rc;
731}
732
733static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
734 struct sk_buff *req, struct sk_buff *resp,
735 int resp_len,
736 pn533_send_async_complete_t complete_cb,
737 void *complete_cb_context)
738{
739 struct pn533_cmd *cmd;
740 struct pn533_send_async_complete_arg *arg;
741 int rc = 0;
742
743 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
744
745 arg = kzalloc(sizeof(arg), GFP_KERNEL);
746 if (!arg)
747 return -ENOMEM;
748
749 arg->complete_cb = complete_cb;
750 arg->complete_cb_context = complete_cb_context;
751 arg->resp = resp;
752 arg->req = req;
753
754 pn533_build_cmd_frame(cmd_code, req);
755
756 mutex_lock(&dev->cmd_lock);
757
758 if (!dev->cmd_pending) {
759 rc = __pn533_send_cmd_frame_async(dev,
760 (struct pn533_frame *)req->data,
761 (struct pn533_frame *)resp->data,
762 resp_len, pn533_send_async_complete,
763 arg);
764 if (rc)
765 goto error;
766
767 dev->cmd_pending = 1;
768 goto unlock;
769 }
770
771 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
772
773 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
774 if (!cmd) {
775 rc = -ENOMEM;
776 goto error;
777 }
778
779 INIT_LIST_HEAD(&cmd->queue);
780 cmd->cmd_code = cmd_code;
781 cmd->req = req;
782 cmd->resp = resp;
783 cmd->arg = arg;
784
785 list_add_tail(&cmd->queue, &dev->cmd_queue);
786
787 goto unlock;
788
789error:
790 kfree(arg);
791unlock:
792 mutex_unlock(&dev->cmd_lock);
793 return rc;
794}
795
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100796static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
797 struct sk_buff *req,
798 pn533_send_async_complete_t complete_cb,
799 void *complete_cb_context)
800{
801 struct sk_buff *resp;
802 int rc;
803 int resp_len = PN533_FRAME_HEADER_LEN +
804 PN533_FRAME_MAX_PAYLOAD_LEN +
805 PN533_FRAME_TAIL_LEN;
806
807 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
808
809 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
810 if (!resp)
811 return -ENOMEM;
812
813 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
814 complete_cb_context);
815 if (rc)
816 dev_kfree_skb(resp);
817
818 return rc;
819}
820
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100821static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
822 struct sk_buff *req,
823 pn533_send_async_complete_t complete_cb,
824 void *complete_cb_context)
825{
826 struct sk_buff *resp;
827 int rc;
828
829 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
830
831 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
832 if (!resp)
833 return -ENOMEM;
834
835 rc = __pn533_send_async(dev, cmd_code, req, resp,
836 PN533_NORMAL_FRAME_MAX_LEN,
837 complete_cb, complete_cb_context);
838 if (rc)
839 dev_kfree_skb(resp);
840
841 return rc;
842}
843
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100844/*
845 * pn533_send_cmd_direct_async
846 *
847 * The function sends a piority cmd directly to the chip omiting the cmd
848 * queue. It's intended to be used by chaining mechanism of received responses
849 * where the host has to request every single chunk of data before scheduling
850 * next cmd from the queue.
851 */
852static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
853 struct sk_buff *req,
854 pn533_send_async_complete_t complete_cb,
855 void *complete_cb_context)
856{
857 struct pn533_send_async_complete_arg *arg;
858 struct sk_buff *resp;
859 int rc;
860 int resp_len = PN533_FRAME_HEADER_LEN +
861 PN533_FRAME_MAX_PAYLOAD_LEN +
862 PN533_FRAME_TAIL_LEN;
863
864 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
865
866 resp = alloc_skb(resp_len, GFP_KERNEL);
867 if (!resp)
868 return -ENOMEM;
869
870 arg = kzalloc(sizeof(arg), GFP_KERNEL);
871 if (!arg) {
872 dev_kfree_skb(resp);
873 return -ENOMEM;
874 }
875
876 arg->complete_cb = complete_cb;
877 arg->complete_cb_context = complete_cb_context;
878 arg->resp = resp;
879 arg->req = req;
880
881 pn533_build_cmd_frame(cmd_code, req);
882
883 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
884 (struct pn533_frame *)resp->data,
885 resp_len, pn533_send_async_complete,
886 arg);
887 if (rc < 0) {
888 dev_kfree_skb(resp);
889 kfree(arg);
890 }
891
892 return rc;
893}
894
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200895static void pn533_wq_cmd(struct work_struct *work)
896{
897 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
898 struct pn533_cmd *cmd;
899
900 mutex_lock(&dev->cmd_lock);
901
902 if (list_empty(&dev->cmd_queue)) {
903 dev->cmd_pending = 0;
904 mutex_unlock(&dev->cmd_lock);
905 return;
906 }
907
908 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
909
Szymon Janc60ad07a2012-10-25 17:29:45 +0200910 list_del(&cmd->queue);
911
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200912 mutex_unlock(&dev->cmd_lock);
913
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100914 if (cmd->cmd_code != PN533_CMD_UNDEF)
915 __pn533_send_cmd_frame_async(dev,
916 (struct pn533_frame *)cmd->req->data,
917 (struct pn533_frame *)cmd->resp->data,
918 PN533_NORMAL_FRAME_MAX_LEN,
919 pn533_send_async_complete,
920 cmd->arg);
921 else
922 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
923 cmd->in_frame_len,
924 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200925
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200926 kfree(cmd);
927}
928
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300929static int pn533_send_cmd_frame_async(struct pn533 *dev,
930 struct pn533_frame *out_frame,
931 struct pn533_frame *in_frame,
932 int in_frame_len,
933 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100934 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300935{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200936 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200937 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300938
939 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
940
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200941 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300942
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200943 if (!dev->cmd_pending) {
944 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
945 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100946 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200947 if (!rc)
948 dev->cmd_pending = 1;
949
Szymon Jancee5e8d82012-09-27 09:16:54 +0200950 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200951 }
952
953 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
954
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100955 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200956 if (!cmd) {
957 rc = -ENOMEM;
958 goto unlock;
959 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200960
961 INIT_LIST_HEAD(&cmd->queue);
962 cmd->out_frame = out_frame;
963 cmd->in_frame = in_frame;
964 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100965 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200966 cmd->cmd_complete = cmd_complete;
967 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200968
969 list_add_tail(&cmd->queue, &dev->cmd_queue);
970
Szymon Jancee5e8d82012-09-27 09:16:54 +0200971unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200972 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300973
Szymon Jancee5e8d82012-09-27 09:16:54 +0200974 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300975}
976
977struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100978 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300979 struct completion done;
980};
981
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100982static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
983 struct sk_buff *resp)
984{
985 struct pn533_sync_cmd_response *arg = _arg;
986
987 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
988
989 arg->resp = resp;
990 complete(&arg->done);
991
992 return 0;
993}
994
995/* pn533_send_cmd_sync
996 *
997 * Please note the req parameter is freed inside the function to
998 * limit a number of return value interpretations by the caller.
999 *
1000 * 1. negative in case of error during TX path -> req should be freed
1001 *
1002 * 2. negative in case of error during RX path -> req should not be freed
1003 * as it's been already freed at the begining of RX path by
1004 * async_complete_cb.
1005 *
1006 * 3. valid pointer in case of succesfult RX path
1007 *
1008 * A caller has to check a return value with IS_ERR macro. If the test pass,
1009 * the returned pointer is valid.
1010 *
1011 * */
1012static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1013 struct sk_buff *req)
1014{
1015 int rc;
1016 struct pn533_sync_cmd_response arg;
1017
1018 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1019
1020 init_completion(&arg.done);
1021
1022 rc = pn533_send_cmd_async(dev, cmd_code, req,
1023 pn533_send_sync_complete, &arg);
1024 if (rc) {
1025 dev_kfree_skb(req);
1026 return ERR_PTR(rc);
1027 }
1028
1029 wait_for_completion(&arg.done);
1030
1031 return arg.resp;
1032}
1033
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001034static void pn533_send_complete(struct urb *urb)
1035{
1036 struct pn533 *dev = urb->context;
1037
1038 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1039
1040 switch (urb->status) {
1041 case 0:
1042 /* success */
1043 break;
1044 case -ECONNRESET:
1045 case -ENOENT:
1046 case -ESHUTDOWN:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001047 nfc_dev_dbg(&dev->interface->dev,
1048 "Urb shutting down with status: %d", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001049 break;
1050 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001051 nfc_dev_dbg(&dev->interface->dev,
1052 "Nonzero urb status received: %d", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001053 }
1054}
1055
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001056static struct sk_buff *pn533_alloc_skb(unsigned int size)
1057{
1058 struct sk_buff *skb;
1059
1060 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1061 size +
1062 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1063
1064 if (skb)
1065 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1066
1067 return skb;
1068}
1069
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001070struct pn533_target_type_a {
1071 __be16 sens_res;
1072 u8 sel_res;
1073 u8 nfcid_len;
1074 u8 nfcid_data[];
1075} __packed;
1076
1077
1078#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1079#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1080#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1081
1082#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1083#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1084
1085#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1086#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1087
1088#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1089#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1090#define PN533_TYPE_A_SEL_PROT_DEP 2
1091#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1092
1093static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1094 int target_data_len)
1095{
1096 u8 ssd;
1097 u8 platconf;
1098
1099 if (target_data_len < sizeof(struct pn533_target_type_a))
1100 return false;
1101
1102 /* The lenght check of nfcid[] and ats[] are not being performed because
1103 the values are not being used */
1104
1105 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1106 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1107 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1108
1109 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001110 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1111 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1112 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001113 return false;
1114
1115 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1116 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1117 return false;
1118
1119 return true;
1120}
1121
1122static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1123 int tgt_data_len)
1124{
1125 struct pn533_target_type_a *tgt_type_a;
1126
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001127 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001128
1129 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1130 return -EPROTO;
1131
1132 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1133 case PN533_TYPE_A_SEL_PROT_MIFARE:
1134 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1135 break;
1136 case PN533_TYPE_A_SEL_PROT_ISO14443:
1137 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1138 break;
1139 case PN533_TYPE_A_SEL_PROT_DEP:
1140 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1141 break;
1142 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1143 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1144 NFC_PROTO_NFC_DEP_MASK;
1145 break;
1146 }
1147
1148 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1149 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001150 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1151 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001152
1153 return 0;
1154}
1155
1156struct pn533_target_felica {
1157 u8 pol_res;
1158 u8 opcode;
1159 u8 nfcid2[8];
1160 u8 pad[8];
1161 /* optional */
1162 u8 syst_code[];
1163} __packed;
1164
1165#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1166#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1167
1168static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1169 int target_data_len)
1170{
1171 if (target_data_len < sizeof(struct pn533_target_felica))
1172 return false;
1173
1174 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1175 return false;
1176
1177 return true;
1178}
1179
1180static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1181 int tgt_data_len)
1182{
1183 struct pn533_target_felica *tgt_felica;
1184
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001185 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001186
1187 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1188 return -EPROTO;
1189
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001190 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1191 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001192 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1193 else
1194 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1195
Samuel Ortiz79757542012-03-05 01:03:45 +01001196 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1197 nfc_tgt->sensf_res_len = 9;
1198
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001199 return 0;
1200}
1201
1202struct pn533_target_jewel {
1203 __be16 sens_res;
1204 u8 jewelid[4];
1205} __packed;
1206
1207static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1208 int target_data_len)
1209{
1210 u8 ssd;
1211 u8 platconf;
1212
1213 if (target_data_len < sizeof(struct pn533_target_jewel))
1214 return false;
1215
1216 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1217 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1218 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1219
1220 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001221 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1222 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1223 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001224 return false;
1225
1226 return true;
1227}
1228
1229static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1230 int tgt_data_len)
1231{
1232 struct pn533_target_jewel *tgt_jewel;
1233
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001234 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001235
1236 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1237 return -EPROTO;
1238
1239 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1240 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001241 nfc_tgt->nfcid1_len = 4;
1242 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243
1244 return 0;
1245}
1246
1247struct pn533_type_b_prot_info {
1248 u8 bitrate;
1249 u8 fsci_type;
1250 u8 fwi_adc_fo;
1251} __packed;
1252
1253#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1254#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1255#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1256
1257struct pn533_type_b_sens_res {
1258 u8 opcode;
1259 u8 nfcid[4];
1260 u8 appdata[4];
1261 struct pn533_type_b_prot_info prot_info;
1262} __packed;
1263
1264#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1265
1266struct pn533_target_type_b {
1267 struct pn533_type_b_sens_res sensb_res;
1268 u8 attrib_res_len;
1269 u8 attrib_res[];
1270} __packed;
1271
1272static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1273 int target_data_len)
1274{
1275 if (target_data_len < sizeof(struct pn533_target_type_b))
1276 return false;
1277
1278 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1279 return false;
1280
1281 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1282 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1283 return false;
1284
1285 return true;
1286}
1287
1288static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1289 int tgt_data_len)
1290{
1291 struct pn533_target_type_b *tgt_type_b;
1292
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001293 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001294
1295 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1296 return -EPROTO;
1297
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001298 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299
1300 return 0;
1301}
1302
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001303static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1304 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001305{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306 struct nfc_target nfc_tgt;
1307 int rc;
1308
1309 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001310 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001311
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001312 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001313 return -EPROTO;
1314
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001315 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1316
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317 switch (dev->poll_mod_curr) {
1318 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001319 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001320 break;
1321 case PN533_POLL_MOD_212KBPS_FELICA:
1322 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001323 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001324 break;
1325 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001326 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327 break;
1328 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001329 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001330 break;
1331 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001332 nfc_dev_err(&dev->interface->dev,
1333 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001334 return -EPROTO;
1335 }
1336
1337 if (rc)
1338 return rc;
1339
1340 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001341 nfc_dev_dbg(&dev->interface->dev,
1342 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001343 return -EAGAIN;
1344 }
1345
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001346 nfc_dev_dbg(&dev->interface->dev,
1347 "Target found - supported protocols: 0x%x",
1348 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001349
1350 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1351
1352 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1353
1354 return 0;
1355}
1356
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001357static inline void pn533_poll_next_mod(struct pn533 *dev)
1358{
1359 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1360}
1361
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001362static void pn533_poll_reset_mod_list(struct pn533 *dev)
1363{
1364 dev->poll_mod_count = 0;
1365}
1366
1367static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1368{
1369 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001370 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001371 dev->poll_mod_count++;
1372}
1373
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001374static void pn533_poll_create_mod_list(struct pn533 *dev,
1375 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001376{
1377 pn533_poll_reset_mod_list(dev);
1378
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001379 if (im_protocols & NFC_PROTO_MIFARE_MASK
1380 || im_protocols & NFC_PROTO_ISO14443_MASK
1381 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001382 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1383
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001384 if (im_protocols & NFC_PROTO_FELICA_MASK
1385 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001386 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1387 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1388 }
1389
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001390 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001391 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1392
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001393 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001394 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001395
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001396 if (tm_protocols)
1397 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001398}
1399
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001400static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001401{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001402 u8 nbtg, tg, *tgdata;
1403 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001404
1405 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1406
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001407 nbtg = resp->data[0];
1408 tg = resp->data[1];
1409 tgdata = &resp->data[2];
1410 tgdata_len = resp->len - 2; /* nbtg + tg */
1411
1412 if (nbtg) {
1413 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001414
1415 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001416 if (rc == 0) {
1417 pn533_poll_reset_mod_list(dev);
1418 return 0;
1419 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001420 }
1421
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001422 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001423}
1424
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001425static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001426{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001427 struct sk_buff *skb;
1428 u8 *felica, *nfcid3, *gb;
1429
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001430 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1431 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1432 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1433 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001434
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001435 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1436 0x0, 0x0, 0x0,
1437 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001438
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001439 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1440 felica (18), nfcid3 (10), gb_len (1) */
1441 gbytes_len +
1442 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001443
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001444 skb = pn533_alloc_skb(skb_len);
1445 if (!skb)
1446 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001447
1448 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001449 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001450
1451 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001452 memcpy(skb_put(skb, 6), mifare_params, 6);
1453
1454 /* Felica params */
1455 felica = skb_put(skb, 18);
1456 memcpy(felica, felica_params, 18);
1457 get_random_bytes(felica + 2, 6);
1458
1459 /* NFCID3 */
1460 nfcid3 = skb_put(skb, 10);
1461 memset(nfcid3, 0, 10);
1462 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001463
1464 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001465 *skb_put(skb, 1) = gbytes_len;
1466
1467 gb = skb_put(skb, gbytes_len);
1468 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001469
Samuel Ortizad3823c2012-05-30 23:54:55 +02001470 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001472
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001473 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001474}
1475
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001476#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001477#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1478static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001479 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001480{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001481 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001482
1483 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1484
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001485 if (IS_ERR(resp))
1486 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001487
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001488 status = resp->data[0];
1489 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001490
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001491 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001492 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001493 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001494 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001495 return 0;
1496 }
1497
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001498 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001499}
1500
1501static void pn533_wq_tg_get_data(struct work_struct *work)
1502{
1503 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001504
1505 struct sk_buff *skb;
1506 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001507
1508 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1509
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001510 skb = pn533_alloc_skb(0);
1511 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001512 return;
1513
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001514 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1515 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001516
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001517 if (rc < 0)
1518 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001519
1520 return;
1521}
1522
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001523#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001524static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001525{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001526 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001527 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001528 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001529
1530 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1531
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001532 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001533 return -EINVAL;
1534
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001535 mode = resp->data[0];
1536 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001537
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001538 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1539 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001540
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001541 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1542 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001543 comm_mode = NFC_COMM_ACTIVE;
1544
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001546 return -EOPNOTSUPP;
1547
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001548 gb = cmd + ATR_REQ_GB_OFFSET;
1549 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001550
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001551 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1552 comm_mode, gb, gb_len);
1553 if (rc < 0) {
1554 nfc_dev_err(&dev->interface->dev,
1555 "Error when signaling target activation");
1556 return rc;
1557 }
1558
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001559 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001560 queue_work(dev->wq, &dev->tg_work);
1561
1562 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001563}
1564
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001565static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001566{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001567 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568
1569 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1570
1571 /* An ack will cancel the last issued command (poll) */
1572 pn533_send_ack(dev, GFP_ATOMIC);
1573
1574 dev->cancel_listen = 1;
1575
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001576 pn533_poll_next_mod(dev);
1577
1578 queue_work(dev->wq, &dev->poll_work);
1579}
1580
1581static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001583{
1584 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001585 int rc;
1586
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001587 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1588
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001589 if (IS_ERR(resp)) {
1590 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001591
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1593 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001594
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 if (rc == -ENOENT) {
1596 if (dev->poll_mod_count != 0)
1597 return rc;
1598 else
1599 goto stop_poll;
1600 } else if (rc < 0) {
1601 nfc_dev_err(&dev->interface->dev,
1602 "Error %d when running poll", rc);
1603 goto stop_poll;
1604 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001605 }
1606
1607 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1608
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001609 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001611 rc = pn533_init_target_complete(dev, resp);
1612 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001613 }
1614
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001615 /* Initiator mode */
1616 rc = pn533_start_poll_complete(dev, resp);
1617 if (!rc)
1618 goto done;
1619
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001620 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 queue_work(dev->wq, &dev->poll_work);
1622
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001623done:
1624 dev_kfree_skb(resp);
1625 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001626
1627stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001628 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1629
Samuel Ortizad3823c2012-05-30 23:54:55 +02001630 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001631 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001632 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001633}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001634
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001635static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1636 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001637{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001638 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001639
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001640 skb = pn533_alloc_skb(mod->len);
1641 if (!skb)
1642 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001644 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001645
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001646 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001647}
1648
1649static int pn533_send_poll_frame(struct pn533 *dev)
1650{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001651 struct pn533_poll_modulations *mod;
1652 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001653 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001654 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001655
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001656 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001657
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001658 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1659 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001660
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001661 if (mod->len == 0) { /* Listen mode */
1662 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1663 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1664 } else { /* Polling mode */
1665 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1666 skb = pn533_alloc_poll_in_frame(mod);
1667 }
1668
1669 if (!skb) {
1670 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1671 return -ENOMEM;
1672 }
1673
1674 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1675 NULL);
1676 if (rc < 0) {
1677 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001678 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001679 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001680
1681 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001682}
1683
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001684static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001685{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001686 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1687 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688 int rc;
1689
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001690 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1691
1692 nfc_dev_dbg(&dev->interface->dev,
1693 "%s cancel_listen %d modulation len %d",
1694 __func__, dev->cancel_listen, cur_mod->len);
1695
1696 if (dev->cancel_listen == 1) {
1697 dev->cancel_listen = 0;
1698 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001699 }
1700
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001701 rc = pn533_send_poll_frame(dev);
1702 if (rc)
1703 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001705 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1706 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001708 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001709}
1710
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001711static int pn533_start_poll(struct nfc_dev *nfc_dev,
1712 u32 im_protocols, u32 tm_protocols)
1713{
1714 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1715
1716 nfc_dev_dbg(&dev->interface->dev,
1717 "%s: im protocols 0x%x tm protocols 0x%x",
1718 __func__, im_protocols, tm_protocols);
1719
1720 if (dev->tgt_active_prot) {
1721 nfc_dev_err(&dev->interface->dev,
1722 "Cannot poll with a target already activated");
1723 return -EBUSY;
1724 }
1725
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001726 if (dev->tgt_mode) {
1727 nfc_dev_err(&dev->interface->dev,
1728 "Cannot poll while already being activated");
1729 return -EBUSY;
1730 }
1731
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001732 if (tm_protocols) {
1733 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1734 if (dev->gb == NULL)
1735 tm_protocols = 0;
1736 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001737
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001738 dev->poll_mod_curr = 0;
1739 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1740 dev->poll_protocols = im_protocols;
1741 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001742
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001743 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001744}
1745
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001746static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1747{
1748 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1749
1750 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1751
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001752 del_timer(&dev->listen_timer);
1753
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001754 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001755 nfc_dev_dbg(&dev->interface->dev,
1756 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001757 return;
1758 }
1759
1760 /* An ack will cancel the last issued command (poll) */
1761 pn533_send_ack(dev, GFP_KERNEL);
1762
1763 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1764 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001765
1766 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767}
1768
1769static int pn533_activate_target_nfcdep(struct pn533 *dev)
1770{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001771 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001772 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 int rc;
1774
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001775 struct sk_buff *skb;
1776 struct sk_buff *resp;
1777
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001778 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1779
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001780 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1781 if (!skb)
1782 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001783
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001784 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1785 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001786
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001787 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1788 if (IS_ERR(resp))
1789 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001790
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001791 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001792 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001793 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001794 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001795 return -EIO;
1796
Samuel Ortiz541d9202011-12-14 16:43:10 +01001797 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001798 gt_len = resp->len - 16;
1799 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001800
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001801 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001802 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001803}
1804
Eric Lapuyade90099432012-05-07 12:31:13 +02001805static int pn533_activate_target(struct nfc_dev *nfc_dev,
1806 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001807{
1808 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1809 int rc;
1810
1811 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001812 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001813
1814 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001815 nfc_dev_err(&dev->interface->dev,
1816 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001817 return -EBUSY;
1818 }
1819
1820 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001821 nfc_dev_err(&dev->interface->dev,
1822 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823 return -EBUSY;
1824 }
1825
1826 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001827 nfc_dev_err(&dev->interface->dev,
1828 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001829 return -EINVAL;
1830 }
1831
1832 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001833 nfc_dev_err(&dev->interface->dev,
1834 "Target doesn't support requested proto %u",
1835 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001836 return -EINVAL;
1837 }
1838
1839 if (protocol == NFC_PROTO_NFC_DEP) {
1840 rc = pn533_activate_target_nfcdep(dev);
1841 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001842 nfc_dev_err(&dev->interface->dev,
1843 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001844 return rc;
1845 }
1846 }
1847
1848 dev->tgt_active_prot = protocol;
1849 dev->tgt_available_prots = 0;
1850
1851 return 0;
1852}
1853
Eric Lapuyade90099432012-05-07 12:31:13 +02001854static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1855 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001856{
1857 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001858
1859 struct sk_buff *skb;
1860 struct sk_buff *resp;
1861
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001862 int rc;
1863
1864 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1865
1866 if (!dev->tgt_active_prot) {
1867 nfc_dev_err(&dev->interface->dev, "There is no active target");
1868 return;
1869 }
1870
1871 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001872 skb_queue_purge(&dev->resp_q);
1873
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001874 skb = pn533_alloc_skb(sizeof(u8));
1875 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001876 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001877
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001878 *skb_put(skb, 1) = 1; /* TG*/
1879
1880 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1881 if (IS_ERR(resp))
1882 return;
1883
1884 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001885 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001886 nfc_dev_err(&dev->interface->dev,
1887 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001888
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001889 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001890 return;
1891}
1892
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001893
1894static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001895 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001896{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001897 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001898 u8 target_gt_len;
1899 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001900 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001901
1902 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001903
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001904 if (IS_ERR(resp))
1905 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906
1907 if (dev->tgt_available_prots &&
1908 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1909 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001910 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001911 rc = -EINVAL;
1912 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001913 }
1914
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001915 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1916
1917 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001918 if (rc != PN533_CMD_RET_SUCCESS) {
1919 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001920 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001921 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001922 }
1923
1924 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001925 struct nfc_target nfc_target;
1926
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001927 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1928
1929 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001930 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001931 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001932 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1933 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001934 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001935
1936 dev->tgt_available_prots = 0;
1937 }
1938
1939 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1940
1941 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001942 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001943 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001944 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001945 if (rc == 0)
1946 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 dev->nfc_dev->targets[0].idx,
1948 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001949
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001950error:
1951 dev_kfree_skb(resp);
1952 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001953}
1954
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001955static int pn533_mod_to_baud(struct pn533 *dev)
1956{
1957 switch (dev->poll_mod_curr) {
1958 case PN533_POLL_MOD_106KBPS_A:
1959 return 0;
1960 case PN533_POLL_MOD_212KBPS_FELICA:
1961 return 1;
1962 case PN533_POLL_MOD_424KBPS_FELICA:
1963 return 2;
1964 default:
1965 return -EINVAL;
1966 }
1967}
1968
Samuel Ortizd7f33452012-05-29 21:45:21 +02001969#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001970static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001971 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001972{
1973 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001974 struct sk_buff *skb;
1975 int rc, baud, skb_len;
1976 u8 *next, *arg;
1977
Samuel Ortizd7f33452012-05-29 21:45:21 +02001978 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001979
1980 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1981
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001982 if (dev->poll_mod_count) {
1983 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001984 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001985 return -EBUSY;
1986 }
1987
1988 if (dev->tgt_active_prot) {
1989 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001990 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001991 return -EBUSY;
1992 }
1993
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001994 baud = pn533_mod_to_baud(dev);
1995 if (baud < 0) {
1996 nfc_dev_err(&dev->interface->dev,
1997 "Invalid curr modulation %d", dev->poll_mod_curr);
1998 return baud;
1999 }
2000
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002001 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02002002 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002003 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002004
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002005 skb = pn533_alloc_skb(skb_len);
2006 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002007 return -ENOMEM;
2008
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002009 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2010 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002011
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002012 next = skb_put(skb, 1); /* Next */
2013 *next = 0;
2014
2015 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
2016 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
2017 PASSIVE_DATA_LEN);
2018 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002019 }
2020
Samuel Ortiz47807d32012-03-05 01:03:50 +01002021 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002022 memcpy(skb_put(skb, gb_len), gb, gb_len);
2023 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002024 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002025 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002026 }
2027
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002028 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2029 if (!arg) {
2030 dev_kfree_skb(skb);
2031 return -ENOMEM;
2032 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002033
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002034 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002035
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002036 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2037 pn533_in_dep_link_up_complete, arg);
2038
2039 if (rc < 0) {
2040 dev_kfree_skb(skb);
2041 kfree(arg);
2042 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002043
2044 return rc;
2045}
2046
2047static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2048{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002049 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2050
2051 pn533_poll_reset_mod_list(dev);
2052
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002053 if (dev->tgt_mode || dev->tgt_active_prot) {
2054 pn533_send_ack(dev, GFP_KERNEL);
2055 usb_kill_urb(dev->in_urb);
2056 }
2057
2058 dev->tgt_active_prot = 0;
2059 dev->tgt_mode = 0;
2060
2061 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002062
2063 return 0;
2064}
2065
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002066struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002067 data_exchange_cb_t cb;
2068 void *cb_context;
2069};
2070
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002071static struct sk_buff *pn533_build_response(struct pn533 *dev)
2072{
2073 struct sk_buff *skb, *tmp, *t;
2074 unsigned int skb_len = 0, tmp_len = 0;
2075
2076 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2077
2078 if (skb_queue_empty(&dev->resp_q))
2079 return NULL;
2080
2081 if (skb_queue_len(&dev->resp_q) == 1) {
2082 skb = skb_dequeue(&dev->resp_q);
2083 goto out;
2084 }
2085
2086 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2087 skb_len += tmp->len;
2088
2089 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2090 __func__, skb_len);
2091
2092 skb = alloc_skb(skb_len, GFP_KERNEL);
2093 if (skb == NULL)
2094 goto out;
2095
2096 skb_put(skb, skb_len);
2097
2098 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2099 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2100 tmp_len += tmp->len;
2101 }
2102
2103out:
2104 skb_queue_purge(&dev->resp_q);
2105
2106 return skb;
2107}
2108
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002109static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002110 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111{
2112 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002113 struct sk_buff *skb;
2114 int rc = 0;
2115 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002116
2117 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2118
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002119 if (IS_ERR(resp)) {
2120 rc = PTR_ERR(resp);
2121 goto _error;
2122 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002124 status = resp->data[0];
2125 ret = status & PN533_CMD_RET_MASK;
2126 mi = status & PN533_CMD_MI_MASK;
2127
2128 skb_pull(resp, sizeof(status));
2129
2130 if (ret != PN533_CMD_RET_SUCCESS) {
2131 nfc_dev_err(&dev->interface->dev,
2132 "PN533 reported error %d when exchanging data",
2133 ret);
2134 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002135 goto error;
2136 }
2137
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002138 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002139
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002140 if (mi) {
2141 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002142 queue_work(dev->wq, &dev->mi_work);
2143 return -EINPROGRESS;
2144 }
2145
2146 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002147 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002148 goto error;
2149
2150 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151 kfree(arg);
2152 return 0;
2153
2154error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002155 dev_kfree_skb(resp);
2156_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002157 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002158 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002159 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002160 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161}
2162
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002163static int pn533_transceive(struct nfc_dev *nfc_dev,
2164 struct nfc_target *target, struct sk_buff *skb,
2165 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002166{
2167 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002168 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002169 int rc;
2170
2171 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2172
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002173 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2174 /* TODO: Implement support to multi-part data exchange */
2175 nfc_dev_err(&dev->interface->dev,
2176 "Data length greater than the max allowed: %d",
2177 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2178 rc = -ENOSYS;
2179 goto error;
2180 }
2181
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002182 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002183 nfc_dev_err(&dev->interface->dev,
2184 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002185 rc = -EINVAL;
2186 goto error;
2187 }
2188
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002189 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002190 if (!arg) {
2191 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002192 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002193 }
2194
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002195 arg->cb = cb;
2196 arg->cb_context = cb_context;
2197
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002198 switch (dev->device_type) {
2199 case PN533_DEVICE_PASORI:
2200 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2201 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2202 skb,
2203 pn533_data_exchange_complete,
2204 arg);
2205
2206 break;
2207 }
2208 default:
2209 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2210
2211 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2212 skb, pn533_data_exchange_complete,
2213 arg);
2214
2215 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002216 }
2217
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002218 if (rc < 0) /* rc from send_async */
2219 goto error;
2220
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002221 return 0;
2222
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002223error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002224 kfree(arg);
2225 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002226 return rc;
2227}
2228
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002229static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002230 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002231{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002232 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002233
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002234 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2235
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002236 if (IS_ERR(resp))
2237 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002238
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002239 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002240
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002241 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002242
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002243 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002244 nfc_tm_deactivated(dev->nfc_dev);
2245
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002246 dev->tgt_mode = 0;
2247
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002248 return 0;
2249 }
2250
2251 queue_work(dev->wq, &dev->tg_work);
2252
2253 return 0;
2254}
2255
2256static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2257{
2258 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002259 int rc;
2260
2261 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2262
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002263 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002264 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002265 "Data length greater than the max allowed: %d",
2266 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2267 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002268 }
2269
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002270 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2271 pn533_tm_send_complete, NULL);
2272 if (rc < 0)
2273 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002274
2275 return rc;
2276}
2277
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002278static void pn533_wq_mi_recv(struct work_struct *work)
2279{
2280 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002281
2282 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002283 int rc;
2284
2285 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2286
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002287 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2288 if (!skb)
2289 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002290
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002291 switch (dev->device_type) {
2292 case PN533_DEVICE_PASORI:
2293 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2294 rc = pn533_send_cmd_direct_async(dev,
2295 PN533_CMD_IN_COMM_THRU,
2296 skb,
2297 pn533_data_exchange_complete,
2298 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002299
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002300 break;
2301 }
2302 default:
2303 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002304
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002305 rc = pn533_send_cmd_direct_async(dev,
2306 PN533_CMD_IN_DATA_EXCHANGE,
2307 skb,
2308 pn533_data_exchange_complete,
2309 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002310
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002311 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002312 }
2313
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002314 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002315 return;
2316
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002317 nfc_dev_err(&dev->interface->dev,
2318 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002319
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002320 dev_kfree_skb(skb);
2321 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002322
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002323error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002324 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002325 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002326}
2327
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002328static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2329 u8 cfgdata_len)
2330{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002331 struct sk_buff *skb;
2332 struct sk_buff *resp;
2333
2334 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002335
2336 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2337
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002338 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002339
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002340 skb = pn533_alloc_skb(skb_len);
2341 if (!skb)
2342 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002343
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002344 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2345 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002346
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002347 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2348 if (IS_ERR(resp))
2349 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002350
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002351 dev_kfree_skb(resp);
2352 return 0;
2353}
2354
2355static int pn533_get_firmware_version(struct pn533 *dev,
2356 struct pn533_fw_version *fv)
2357{
2358 struct sk_buff *skb;
2359 struct sk_buff *resp;
2360
2361 skb = pn533_alloc_skb(0);
2362 if (!skb)
2363 return -ENOMEM;
2364
2365 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2366 if (IS_ERR(resp))
2367 return PTR_ERR(resp);
2368
2369 fv->ic = resp->data[0];
2370 fv->ver = resp->data[1];
2371 fv->rev = resp->data[2];
2372 fv->support = resp->data[3];
2373
2374 dev_kfree_skb(resp);
2375 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002376}
2377
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002378static int pn533_fw_reset(struct pn533 *dev)
2379{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002380 struct sk_buff *skb;
2381 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002382
2383 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2384
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002385 skb = pn533_alloc_skb(sizeof(u8));
2386 if (!skb)
2387 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002388
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002389 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002390
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002391 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2392 if (IS_ERR(resp))
2393 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002394
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002395 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002396
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002397 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002398}
2399
2400static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002401 .dev_up = NULL,
2402 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002403 .dep_link_up = pn533_dep_link_up,
2404 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002405 .start_poll = pn533_start_poll,
2406 .stop_poll = pn533_stop_poll,
2407 .activate_target = pn533_activate_target,
2408 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002409 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002410 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002411};
2412
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002413static int pn533_setup(struct pn533 *dev)
2414{
2415 struct pn533_config_max_retries max_retries;
2416 struct pn533_config_timing timing;
2417 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2418 int rc;
2419
2420 switch (dev->device_type) {
2421 case PN533_DEVICE_STD:
2422 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2423 max_retries.mx_rty_psl = 2;
2424 max_retries.mx_rty_passive_act =
2425 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2426
2427 timing.rfu = PN533_CONFIG_TIMING_102;
2428 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2429 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2430
2431 break;
2432
2433 case PN533_DEVICE_PASORI:
2434 max_retries.mx_rty_atr = 0x2;
2435 max_retries.mx_rty_psl = 0x1;
2436 max_retries.mx_rty_passive_act =
2437 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2438
2439 timing.rfu = PN533_CONFIG_TIMING_102;
2440 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2441 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2442
2443 break;
2444
2445 default:
2446 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2447 dev->device_type);
2448 return -EINVAL;
2449 }
2450
2451 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2452 (u8 *)&max_retries, sizeof(max_retries));
2453 if (rc) {
2454 nfc_dev_err(&dev->interface->dev,
2455 "Error on setting MAX_RETRIES config");
2456 return rc;
2457 }
2458
2459
2460 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2461 (u8 *)&timing, sizeof(timing));
2462 if (rc) {
2463 nfc_dev_err(&dev->interface->dev,
2464 "Error on setting RF timings");
2465 return rc;
2466 }
2467
2468 switch (dev->device_type) {
2469 case PN533_DEVICE_STD:
2470 break;
2471
2472 case PN533_DEVICE_PASORI:
2473 pn533_fw_reset(dev);
2474
2475 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2476 pasori_cfg, 3);
2477 if (rc) {
2478 nfc_dev_err(&dev->interface->dev,
2479 "Error while settings PASORI config");
2480 return rc;
2481 }
2482
2483 pn533_fw_reset(dev);
2484
2485 break;
2486 }
2487
2488 return 0;
2489}
2490
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002491static int pn533_probe(struct usb_interface *interface,
2492 const struct usb_device_id *id)
2493{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002494 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002495 struct pn533 *dev;
2496 struct usb_host_interface *iface_desc;
2497 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002498 int in_endpoint = 0;
2499 int out_endpoint = 0;
2500 int rc = -ENOMEM;
2501 int i;
2502 u32 protocols;
2503
2504 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2505 if (!dev)
2506 return -ENOMEM;
2507
2508 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2509 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002510 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002511
2512 iface_desc = interface->cur_altsetting;
2513 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2514 endpoint = &iface_desc->endpoint[i].desc;
2515
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002516 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002517 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002518
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002519 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002520 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002521 }
2522
2523 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002524 nfc_dev_err(&interface->dev,
2525 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002526 rc = -ENODEV;
2527 goto error;
2528 }
2529
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002530 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002531 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002532 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002533 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2534
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002535 if (!dev->in_frame || !dev->out_frame || !dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002536 goto error;
2537
2538 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002539 usb_rcvbulkpipe(dev->udev, in_endpoint),
2540 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002541 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002542 usb_sndbulkpipe(dev->udev, out_endpoint),
2543 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002544
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002545 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2546 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002547 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002548 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002549 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002550 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002551 if (dev->wq == NULL)
2552 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002553
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002554 init_timer(&dev->listen_timer);
2555 dev->listen_timer.data = (unsigned long) dev;
2556 dev->listen_timer.function = pn533_listen_mode_timer;
2557
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002558 skb_queue_head_init(&dev->resp_q);
2559
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002560 INIT_LIST_HEAD(&dev->cmd_queue);
2561
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002562 usb_set_intfdata(interface, dev);
2563
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002564 memset(&fw_ver, 0, sizeof(fw_ver));
2565 rc = pn533_get_firmware_version(dev, &fw_ver);
2566 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002567 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002568
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002569 nfc_dev_info(&dev->interface->dev,
2570 "NXP PN533 firmware ver %d.%d now attached",
2571 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002572
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002573 dev->device_type = id->driver_info;
2574 switch (dev->device_type) {
2575 case PN533_DEVICE_STD:
2576 protocols = PN533_ALL_PROTOCOLS;
2577 break;
2578
2579 case PN533_DEVICE_PASORI:
2580 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2581 break;
2582
2583 default:
2584 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2585 dev->device_type);
2586 rc = -EINVAL;
2587 goto destroy_wq;
2588 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002589
Samuel Ortize8753042011-08-19 15:47:11 +02002590 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002591 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002592 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002593 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002595 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002596
2597 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2598 nfc_set_drvdata(dev->nfc_dev, dev);
2599
2600 rc = nfc_register_device(dev->nfc_dev);
2601 if (rc)
2602 goto free_nfc_dev;
2603
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002604 rc = pn533_setup(dev);
2605 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002606 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002607
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002608 return 0;
2609
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002610unregister_nfc_dev:
2611 nfc_unregister_device(dev->nfc_dev);
2612
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002613free_nfc_dev:
2614 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002615
Samuel Ortiz4849f852012-04-10 19:43:17 +02002616destroy_wq:
2617 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002618error:
2619 kfree(dev->in_frame);
2620 usb_free_urb(dev->in_urb);
2621 kfree(dev->out_frame);
2622 usb_free_urb(dev->out_urb);
2623 kfree(dev);
2624 return rc;
2625}
2626
2627static void pn533_disconnect(struct usb_interface *interface)
2628{
2629 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002630 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002631
2632 dev = usb_get_intfdata(interface);
2633 usb_set_intfdata(interface, NULL);
2634
2635 nfc_unregister_device(dev->nfc_dev);
2636 nfc_free_device(dev->nfc_dev);
2637
2638 usb_kill_urb(dev->in_urb);
2639 usb_kill_urb(dev->out_urb);
2640
Samuel Ortiz4849f852012-04-10 19:43:17 +02002641 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002642
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002643 skb_queue_purge(&dev->resp_q);
2644
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002645 del_timer(&dev->listen_timer);
2646
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002647 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2648 list_del(&cmd->queue);
2649 kfree(cmd);
2650 }
2651
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002652 kfree(dev->in_frame);
2653 usb_free_urb(dev->in_urb);
2654 kfree(dev->out_frame);
2655 usb_free_urb(dev->out_urb);
2656 kfree(dev);
2657
Dan Carpenter276556d2011-07-08 10:21:15 +03002658 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002659}
2660
2661static struct usb_driver pn533_driver = {
2662 .name = "pn533",
2663 .probe = pn533_probe,
2664 .disconnect = pn533_disconnect,
2665 .id_table = pn533_table,
2666};
2667
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002668module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002669
2670MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2671 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2672MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2673MODULE_VERSION(VERSION);
2674MODULE_LICENSE("GPL");