blob: e5615f538aaada68d0fa67958bf74fd90fa2de36 [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 +0100301/* PN533_CMD_IN_JUMP_FOR_DEP */
302struct pn533_cmd_jump_dep {
303 u8 active;
304 u8 baud;
305 u8 next;
Samuel Ortizd7f33452012-05-29 21:45:21 +0200306 u8 data[];
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100307} __packed;
308
309struct pn533_cmd_jump_dep_response {
310 u8 status;
311 u8 tg;
312 u8 nfcid3t[10];
313 u8 didt;
314 u8 bst;
315 u8 brt;
316 u8 to;
317 u8 ppt;
318 /* optional */
319 u8 gt[];
320} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300321
Samuel Ortizad3823c2012-05-30 23:54:55 +0200322
323/* PN533_TG_INIT_AS_TARGET */
324#define PN533_INIT_TARGET_PASSIVE 0x1
325#define PN533_INIT_TARGET_DEP 0x2
326
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200327#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
328#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
329#define PN533_INIT_TARGET_RESP_DEP 0x4
330
Samuel Ortizad3823c2012-05-30 23:54:55 +0200331struct pn533_cmd_init_target {
332 u8 mode;
333 u8 mifare[6];
334 u8 felica[18];
335 u8 nfcid3[10];
336 u8 gb_len;
337 u8 gb[];
338} __packed;
339
340struct pn533_cmd_init_target_response {
341 u8 mode;
342 u8 cmd[];
343} __packed;
344
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300345struct pn533 {
346 struct usb_device *udev;
347 struct usb_interface *interface;
348 struct nfc_dev *nfc_dev;
349
350 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300351 struct pn533_frame *out_frame;
352
353 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300354 struct pn533_frame *in_frame;
355
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200356 struct sk_buff_head resp_q;
357
Samuel Ortiz4849f852012-04-10 19:43:17 +0200358 struct workqueue_struct *wq;
359 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200360 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200361 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200362 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200363 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200364 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200365 struct pn533_frame *wq_in_frame;
366 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200367 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300368
369 pn533_cmd_complete_t cmd_complete;
370 void *cmd_complete_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200371 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300372 u8 cmd;
373
374 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
375 u8 poll_mod_count;
376 u8 poll_mod_curr;
377 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200378 u32 listen_protocols;
379
380 u8 *gb;
381 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300382
383 u8 tgt_available_prots;
384 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200385 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200386
387 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200388
389 struct list_head cmd_queue;
390 u8 cmd_pending;
391};
392
393struct pn533_cmd {
394 struct list_head queue;
395 struct pn533_frame *out_frame;
396 struct pn533_frame *in_frame;
397 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100398 u8 cmd_code;
399 struct sk_buff *req;
400 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200401 pn533_cmd_complete_t cmd_complete;
402 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300403};
404
405struct pn533_frame {
406 u8 preamble;
407 __be16 start_frame;
408 u8 datalen;
409 u8 datalen_checksum;
410 u8 data[];
411} __packed;
412
413/* The rule: value + checksum = 0 */
414static inline u8 pn533_checksum(u8 value)
415{
416 return ~value + 1;
417}
418
419/* The rule: sum(data elements) + checksum = 0 */
420static u8 pn533_data_checksum(u8 *data, int datalen)
421{
422 u8 sum = 0;
423 int i;
424
425 for (i = 0; i < datalen; i++)
426 sum += data[i];
427
428 return pn533_checksum(sum);
429}
430
431/**
432 * pn533_tx_frame_ack - create a ack frame
433 * @frame: The frame to be set as ack
434 *
435 * Ack is different type of standard frame. As a standard frame, it has
436 * preamble and start_frame. However the checksum of this frame must fail,
437 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
438 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
439 * After datalen_checksum field, the postamble is placed.
440 */
441static void pn533_tx_frame_ack(struct pn533_frame *frame)
442{
443 frame->preamble = 0;
444 frame->start_frame = cpu_to_be16(PN533_SOF);
445 frame->datalen = 0;
446 frame->datalen_checksum = 0xFF;
447 /* data[0] is used as postamble */
448 frame->data[0] = 0;
449}
450
451static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
452{
453 frame->preamble = 0;
454 frame->start_frame = cpu_to_be16(PN533_SOF);
455 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
456 PN533_FRAME_CMD(frame) = cmd;
457 frame->datalen = 2;
458}
459
460static void pn533_tx_frame_finish(struct pn533_frame *frame)
461{
462 frame->datalen_checksum = pn533_checksum(frame->datalen);
463
464 PN533_FRAME_CHECKSUM(frame) =
465 pn533_data_checksum(frame->data, frame->datalen);
466
467 PN533_FRAME_POSTAMBLE(frame) = 0;
468}
469
470static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
471{
472 u8 checksum;
473
474 if (frame->start_frame != cpu_to_be16(PN533_SOF))
475 return false;
476
477 checksum = pn533_checksum(frame->datalen);
478 if (checksum != frame->datalen_checksum)
479 return false;
480
481 checksum = pn533_data_checksum(frame->data, frame->datalen);
482 if (checksum != PN533_FRAME_CHECKSUM(frame))
483 return false;
484
485 return true;
486}
487
488static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
489{
490 if (frame->start_frame != cpu_to_be16(PN533_SOF))
491 return false;
492
493 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
494 return false;
495
496 return true;
497}
498
499static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
500{
501 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
502}
503
Samuel Ortiz4849f852012-04-10 19:43:17 +0200504
505static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300506{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200507 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200508 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300509 int rc;
510
Samuel Ortiz4849f852012-04-10 19:43:17 +0200511 in_frame = dev->wq_in_frame;
512
513 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200515 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516 else
517 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
518 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
519 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
520
521 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200522 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523}
524
525static void pn533_recv_response(struct urb *urb)
526{
527 struct pn533 *dev = urb->context;
528 struct pn533_frame *in_frame;
529
Samuel Ortiz4849f852012-04-10 19:43:17 +0200530 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300531
532 switch (urb->status) {
533 case 0:
534 /* success */
535 break;
536 case -ECONNRESET:
537 case -ENOENT:
538 case -ESHUTDOWN:
539 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
540 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200541 dev->wq_in_error = urb->status;
542 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300543 default:
544 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
545 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200546 dev->wq_in_error = urb->status;
547 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 }
549
550 in_frame = dev->in_urb->transfer_buffer;
551
552 if (!pn533_rx_frame_is_valid(in_frame)) {
553 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200554 dev->wq_in_error = -EIO;
555 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300556 }
557
558 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
559 nfc_dev_err(&dev->interface->dev, "The received frame is not "
560 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200561 dev->wq_in_error = -EIO;
562 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563 }
564
565 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200566 dev->wq_in_error = 0;
567 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568
Samuel Ortiz4849f852012-04-10 19:43:17 +0200569sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200570 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300571}
572
573static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
574{
575 dev->in_urb->complete = pn533_recv_response;
576
577 return usb_submit_urb(dev->in_urb, flags);
578}
579
580static void pn533_recv_ack(struct urb *urb)
581{
582 struct pn533 *dev = urb->context;
583 struct pn533_frame *in_frame;
584 int rc;
585
586 switch (urb->status) {
587 case 0:
588 /* success */
589 break;
590 case -ECONNRESET:
591 case -ENOENT:
592 case -ESHUTDOWN:
593 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
594 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200595 dev->wq_in_error = urb->status;
596 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300597 default:
598 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
599 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200600 dev->wq_in_error = urb->status;
601 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 }
603
604 in_frame = dev->in_urb->transfer_buffer;
605
606 if (!pn533_rx_frame_is_ack(in_frame)) {
607 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200608 dev->wq_in_error = -EIO;
609 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300610 }
611
612 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
613
614 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
615 if (rc) {
616 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
617 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200618 dev->wq_in_error = rc;
619 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300620 }
621
622 return;
623
Samuel Ortiz4849f852012-04-10 19:43:17 +0200624sched_wq:
625 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200626 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300627}
628
629static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
630{
631 dev->in_urb->complete = pn533_recv_ack;
632
633 return usb_submit_urb(dev->in_urb, flags);
634}
635
636static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
637{
638 int rc;
639
640 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
641
642 pn533_tx_frame_ack(dev->out_frame);
643
644 dev->out_urb->transfer_buffer = dev->out_frame;
645 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
646 rc = usb_submit_urb(dev->out_urb, flags);
647
648 return rc;
649}
650
651static int __pn533_send_cmd_frame_async(struct pn533 *dev,
652 struct pn533_frame *out_frame,
653 struct pn533_frame *in_frame,
654 int in_frame_len,
655 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100656 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657{
658 int rc;
659
660 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
661 PN533_FRAME_CMD(out_frame));
662
663 dev->cmd = PN533_FRAME_CMD(out_frame);
664 dev->cmd_complete = cmd_complete;
665 dev->cmd_complete_arg = arg;
666
667 dev->out_urb->transfer_buffer = out_frame;
668 dev->out_urb->transfer_buffer_length =
669 PN533_FRAME_SIZE(out_frame);
670
671 dev->in_urb->transfer_buffer = in_frame;
672 dev->in_urb->transfer_buffer_length = in_frame_len;
673
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100674 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300675 if (rc)
676 return rc;
677
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100678 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300679 if (rc)
680 goto error;
681
682 return 0;
683
684error:
685 usb_unlink_urb(dev->out_urb);
686 return rc;
687}
688
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100689static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
690{
691 struct pn533_frame *frame;
692 /* payload is already there, just update datalen */
693 int payload_len = skb->len;
694
695 skb_push(skb, PN533_FRAME_HEADER_LEN);
696 skb_put(skb, PN533_FRAME_TAIL_LEN);
697
698 frame = (struct pn533_frame *)skb->data;
699
700 pn533_tx_frame_init(frame, cmd_code);
701 frame->datalen += payload_len;
702 pn533_tx_frame_finish(frame);
703}
704
705struct pn533_send_async_complete_arg {
706 pn533_send_async_complete_t complete_cb;
707 void *complete_cb_context;
708 struct sk_buff *resp;
709 struct sk_buff *req;
710};
711
712static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
713 int params_len)
714{
715 struct pn533_send_async_complete_arg *arg = _arg;
716
717 struct sk_buff *req = arg->req;
718 struct sk_buff *resp = arg->resp;
719
720 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
721 int rc;
722
723 dev_kfree_skb(req);
724
725 if (params_len < 0) {
726 nfc_dev_err(&dev->interface->dev,
727 "Error %d when starting as a target",
728 params_len);
729
730 arg->complete_cb(dev, arg->complete_cb_context,
731 ERR_PTR(params_len));
732 rc = params_len;
733 dev_kfree_skb(resp);
734 goto out;
735 }
736
737 skb_put(resp, PN533_FRAME_SIZE(frame));
738 skb_pull(resp, PN533_FRAME_HEADER_LEN);
739 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
740
741 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
742
743out:
744 kfree(arg);
745 return rc;
746}
747
748static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
749 struct sk_buff *req, struct sk_buff *resp,
750 int resp_len,
751 pn533_send_async_complete_t complete_cb,
752 void *complete_cb_context)
753{
754 struct pn533_cmd *cmd;
755 struct pn533_send_async_complete_arg *arg;
756 int rc = 0;
757
758 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
759
760 arg = kzalloc(sizeof(arg), GFP_KERNEL);
761 if (!arg)
762 return -ENOMEM;
763
764 arg->complete_cb = complete_cb;
765 arg->complete_cb_context = complete_cb_context;
766 arg->resp = resp;
767 arg->req = req;
768
769 pn533_build_cmd_frame(cmd_code, req);
770
771 mutex_lock(&dev->cmd_lock);
772
773 if (!dev->cmd_pending) {
774 rc = __pn533_send_cmd_frame_async(dev,
775 (struct pn533_frame *)req->data,
776 (struct pn533_frame *)resp->data,
777 resp_len, pn533_send_async_complete,
778 arg);
779 if (rc)
780 goto error;
781
782 dev->cmd_pending = 1;
783 goto unlock;
784 }
785
786 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
787
788 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
789 if (!cmd) {
790 rc = -ENOMEM;
791 goto error;
792 }
793
794 INIT_LIST_HEAD(&cmd->queue);
795 cmd->cmd_code = cmd_code;
796 cmd->req = req;
797 cmd->resp = resp;
798 cmd->arg = arg;
799
800 list_add_tail(&cmd->queue, &dev->cmd_queue);
801
802 goto unlock;
803
804error:
805 kfree(arg);
806unlock:
807 mutex_unlock(&dev->cmd_lock);
808 return rc;
809}
810
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100811static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
812 struct sk_buff *req,
813 pn533_send_async_complete_t complete_cb,
814 void *complete_cb_context)
815{
816 struct sk_buff *resp;
817 int rc;
818 int resp_len = PN533_FRAME_HEADER_LEN +
819 PN533_FRAME_MAX_PAYLOAD_LEN +
820 PN533_FRAME_TAIL_LEN;
821
822 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
823
824 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
825 if (!resp)
826 return -ENOMEM;
827
828 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
829 complete_cb_context);
830 if (rc)
831 dev_kfree_skb(resp);
832
833 return rc;
834}
835
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100836static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
837 struct sk_buff *req,
838 pn533_send_async_complete_t complete_cb,
839 void *complete_cb_context)
840{
841 struct sk_buff *resp;
842 int rc;
843
844 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
845
846 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
847 if (!resp)
848 return -ENOMEM;
849
850 rc = __pn533_send_async(dev, cmd_code, req, resp,
851 PN533_NORMAL_FRAME_MAX_LEN,
852 complete_cb, complete_cb_context);
853 if (rc)
854 dev_kfree_skb(resp);
855
856 return rc;
857}
858
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200859static void pn533_wq_cmd(struct work_struct *work)
860{
861 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
862 struct pn533_cmd *cmd;
863
864 mutex_lock(&dev->cmd_lock);
865
866 if (list_empty(&dev->cmd_queue)) {
867 dev->cmd_pending = 0;
868 mutex_unlock(&dev->cmd_lock);
869 return;
870 }
871
872 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
873
Szymon Janc60ad07a2012-10-25 17:29:45 +0200874 list_del(&cmd->queue);
875
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200876 mutex_unlock(&dev->cmd_lock);
877
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100878 if (cmd->cmd_code != PN533_CMD_UNDEF)
879 __pn533_send_cmd_frame_async(dev,
880 (struct pn533_frame *)cmd->req->data,
881 (struct pn533_frame *)cmd->resp->data,
882 PN533_NORMAL_FRAME_MAX_LEN,
883 pn533_send_async_complete,
884 cmd->arg);
885 else
886 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
887 cmd->in_frame_len,
888 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200889
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200890 kfree(cmd);
891}
892
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300893static int pn533_send_cmd_frame_async(struct pn533 *dev,
894 struct pn533_frame *out_frame,
895 struct pn533_frame *in_frame,
896 int in_frame_len,
897 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100898 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300899{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200900 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200901 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300902
903 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
904
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200905 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300906
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200907 if (!dev->cmd_pending) {
908 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
909 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100910 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200911 if (!rc)
912 dev->cmd_pending = 1;
913
Szymon Jancee5e8d82012-09-27 09:16:54 +0200914 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200915 }
916
917 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
918
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100919 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200920 if (!cmd) {
921 rc = -ENOMEM;
922 goto unlock;
923 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200924
925 INIT_LIST_HEAD(&cmd->queue);
926 cmd->out_frame = out_frame;
927 cmd->in_frame = in_frame;
928 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100929 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200930 cmd->cmd_complete = cmd_complete;
931 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200932
933 list_add_tail(&cmd->queue, &dev->cmd_queue);
934
Szymon Jancee5e8d82012-09-27 09:16:54 +0200935unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200936 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300937
Szymon Jancee5e8d82012-09-27 09:16:54 +0200938 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300939}
940
941struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100942 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300943 struct completion done;
944};
945
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100946static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
947 struct sk_buff *resp)
948{
949 struct pn533_sync_cmd_response *arg = _arg;
950
951 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
952
953 arg->resp = resp;
954 complete(&arg->done);
955
956 return 0;
957}
958
959/* pn533_send_cmd_sync
960 *
961 * Please note the req parameter is freed inside the function to
962 * limit a number of return value interpretations by the caller.
963 *
964 * 1. negative in case of error during TX path -> req should be freed
965 *
966 * 2. negative in case of error during RX path -> req should not be freed
967 * as it's been already freed at the begining of RX path by
968 * async_complete_cb.
969 *
970 * 3. valid pointer in case of succesfult RX path
971 *
972 * A caller has to check a return value with IS_ERR macro. If the test pass,
973 * the returned pointer is valid.
974 *
975 * */
976static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
977 struct sk_buff *req)
978{
979 int rc;
980 struct pn533_sync_cmd_response arg;
981
982 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
983
984 init_completion(&arg.done);
985
986 rc = pn533_send_cmd_async(dev, cmd_code, req,
987 pn533_send_sync_complete, &arg);
988 if (rc) {
989 dev_kfree_skb(req);
990 return ERR_PTR(rc);
991 }
992
993 wait_for_completion(&arg.done);
994
995 return arg.resp;
996}
997
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300998static void pn533_send_complete(struct urb *urb)
999{
1000 struct pn533 *dev = urb->context;
1001
1002 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1003
1004 switch (urb->status) {
1005 case 0:
1006 /* success */
1007 break;
1008 case -ECONNRESET:
1009 case -ENOENT:
1010 case -ESHUTDOWN:
1011 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1012 " status: %d", urb->status);
1013 break;
1014 default:
1015 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1016 " %d", urb->status);
1017 }
1018}
1019
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001020static struct sk_buff *pn533_alloc_skb(unsigned int size)
1021{
1022 struct sk_buff *skb;
1023
1024 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1025 size +
1026 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1027
1028 if (skb)
1029 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1030
1031 return skb;
1032}
1033
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001034struct pn533_target_type_a {
1035 __be16 sens_res;
1036 u8 sel_res;
1037 u8 nfcid_len;
1038 u8 nfcid_data[];
1039} __packed;
1040
1041
1042#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1043#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1044#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1045
1046#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1047#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1048
1049#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1050#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1051
1052#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1053#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1054#define PN533_TYPE_A_SEL_PROT_DEP 2
1055#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1056
1057static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1058 int target_data_len)
1059{
1060 u8 ssd;
1061 u8 platconf;
1062
1063 if (target_data_len < sizeof(struct pn533_target_type_a))
1064 return false;
1065
1066 /* The lenght check of nfcid[] and ats[] are not being performed because
1067 the values are not being used */
1068
1069 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1070 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1071 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1072
1073 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1074 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1075 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1076 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1077 return false;
1078
1079 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1080 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1081 return false;
1082
1083 return true;
1084}
1085
1086static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1087 int tgt_data_len)
1088{
1089 struct pn533_target_type_a *tgt_type_a;
1090
1091 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1092
1093 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1094 return -EPROTO;
1095
1096 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1097 case PN533_TYPE_A_SEL_PROT_MIFARE:
1098 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1099 break;
1100 case PN533_TYPE_A_SEL_PROT_ISO14443:
1101 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1102 break;
1103 case PN533_TYPE_A_SEL_PROT_DEP:
1104 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1105 break;
1106 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1107 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1108 NFC_PROTO_NFC_DEP_MASK;
1109 break;
1110 }
1111
1112 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1113 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001114 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1115 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001116
1117 return 0;
1118}
1119
1120struct pn533_target_felica {
1121 u8 pol_res;
1122 u8 opcode;
1123 u8 nfcid2[8];
1124 u8 pad[8];
1125 /* optional */
1126 u8 syst_code[];
1127} __packed;
1128
1129#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1130#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1131
1132static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1133 int target_data_len)
1134{
1135 if (target_data_len < sizeof(struct pn533_target_felica))
1136 return false;
1137
1138 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1139 return false;
1140
1141 return true;
1142}
1143
1144static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1145 int tgt_data_len)
1146{
1147 struct pn533_target_felica *tgt_felica;
1148
1149 tgt_felica = (struct pn533_target_felica *) tgt_data;
1150
1151 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1152 return -EPROTO;
1153
1154 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1155 tgt_felica->nfcid2[1] ==
1156 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1157 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1158 else
1159 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1160
Samuel Ortiz79757542012-03-05 01:03:45 +01001161 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1162 nfc_tgt->sensf_res_len = 9;
1163
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001164 return 0;
1165}
1166
1167struct pn533_target_jewel {
1168 __be16 sens_res;
1169 u8 jewelid[4];
1170} __packed;
1171
1172static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1173 int target_data_len)
1174{
1175 u8 ssd;
1176 u8 platconf;
1177
1178 if (target_data_len < sizeof(struct pn533_target_jewel))
1179 return false;
1180
1181 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1182 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1183 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1184
1185 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1186 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1187 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1188 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1189 return false;
1190
1191 return true;
1192}
1193
1194static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1195 int tgt_data_len)
1196{
1197 struct pn533_target_jewel *tgt_jewel;
1198
1199 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1200
1201 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1202 return -EPROTO;
1203
1204 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1205 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001206 nfc_tgt->nfcid1_len = 4;
1207 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001208
1209 return 0;
1210}
1211
1212struct pn533_type_b_prot_info {
1213 u8 bitrate;
1214 u8 fsci_type;
1215 u8 fwi_adc_fo;
1216} __packed;
1217
1218#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1219#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1220#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1221
1222struct pn533_type_b_sens_res {
1223 u8 opcode;
1224 u8 nfcid[4];
1225 u8 appdata[4];
1226 struct pn533_type_b_prot_info prot_info;
1227} __packed;
1228
1229#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1230
1231struct pn533_target_type_b {
1232 struct pn533_type_b_sens_res sensb_res;
1233 u8 attrib_res_len;
1234 u8 attrib_res[];
1235} __packed;
1236
1237static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1238 int target_data_len)
1239{
1240 if (target_data_len < sizeof(struct pn533_target_type_b))
1241 return false;
1242
1243 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1244 return false;
1245
1246 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1247 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1248 return false;
1249
1250 return true;
1251}
1252
1253static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1254 int tgt_data_len)
1255{
1256 struct pn533_target_type_b *tgt_type_b;
1257
1258 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1259
1260 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1261 return -EPROTO;
1262
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001263 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001264
1265 return 0;
1266}
1267
1268struct pn533_poll_response {
1269 u8 nbtg;
1270 u8 tg;
1271 u8 target_data[];
1272} __packed;
1273
1274static int pn533_target_found(struct pn533 *dev,
1275 struct pn533_poll_response *resp, int resp_len)
1276{
1277 int target_data_len;
1278 struct nfc_target nfc_tgt;
1279 int rc;
1280
1281 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1282 dev->poll_mod_curr);
1283
1284 if (resp->tg != 1)
1285 return -EPROTO;
1286
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001287 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1288
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001289 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1290
1291 switch (dev->poll_mod_curr) {
1292 case PN533_POLL_MOD_106KBPS_A:
1293 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1294 target_data_len);
1295 break;
1296 case PN533_POLL_MOD_212KBPS_FELICA:
1297 case PN533_POLL_MOD_424KBPS_FELICA:
1298 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1299 target_data_len);
1300 break;
1301 case PN533_POLL_MOD_106KBPS_JEWEL:
1302 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1303 target_data_len);
1304 break;
1305 case PN533_POLL_MOD_847KBPS_B:
1306 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1307 target_data_len);
1308 break;
1309 default:
1310 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1311 " modulation");
1312 return -EPROTO;
1313 }
1314
1315 if (rc)
1316 return rc;
1317
1318 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1319 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1320 " have the desired protocol");
1321 return -EAGAIN;
1322 }
1323
1324 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1325 "0x%x", nfc_tgt.supported_protocols);
1326
1327 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1328
1329 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1330
1331 return 0;
1332}
1333
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001334static inline void pn533_poll_next_mod(struct pn533 *dev)
1335{
1336 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1337}
1338
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001339static void pn533_poll_reset_mod_list(struct pn533 *dev)
1340{
1341 dev->poll_mod_count = 0;
1342}
1343
1344static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1345{
1346 dev->poll_mod_active[dev->poll_mod_count] =
1347 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1348 dev->poll_mod_count++;
1349}
1350
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001351static void pn533_poll_create_mod_list(struct pn533 *dev,
1352 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001353{
1354 pn533_poll_reset_mod_list(dev);
1355
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001356 if (im_protocols & NFC_PROTO_MIFARE_MASK
1357 || im_protocols & NFC_PROTO_ISO14443_MASK
1358 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001359 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1360
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001361 if (im_protocols & NFC_PROTO_FELICA_MASK
1362 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001363 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1364 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1365 }
1366
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001367 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001368 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1369
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001370 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001371 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001372
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001373 if (tm_protocols)
1374 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001375}
1376
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001377static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001378{
1379 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001380 int rc;
1381
1382 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1383
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001384 resp = (struct pn533_poll_response *) params;
1385 if (resp->nbtg) {
1386 rc = pn533_target_found(dev, resp, params_len);
1387
1388 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001389 if (rc == 0) {
1390 pn533_poll_reset_mod_list(dev);
1391 return 0;
1392 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001393 }
1394
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001395 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001396}
1397
Samuel Ortizad3823c2012-05-30 23:54:55 +02001398static int pn533_init_target_frame(struct pn533_frame *frame,
1399 u8 *gb, size_t gb_len)
1400{
1401 struct pn533_cmd_init_target *cmd;
1402 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001403 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1404 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1405 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1406 0xff, 0xff}; /* System code */
1407 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1408 0x0, 0x0, 0x0,
1409 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001410
1411 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1412 cmd = kzalloc(cmd_len, GFP_KERNEL);
1413 if (cmd == NULL)
1414 return -ENOMEM;
1415
1416 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1417
1418 /* DEP support only */
1419 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001420
1421 /* Felica params */
1422 memcpy(cmd->felica, felica_params, 18);
1423 get_random_bytes(cmd->felica + 2, 6);
1424
1425 /* NFCID3 */
1426 memset(cmd->nfcid3, 0, 10);
1427 memcpy(cmd->nfcid3, cmd->felica, 8);
1428
1429 /* MIFARE params */
1430 memcpy(cmd->mifare, mifare_params, 6);
1431
1432 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001433 cmd->gb_len = gb_len;
1434 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001435
Samuel Ortizad3823c2012-05-30 23:54:55 +02001436 /* Len Tk */
1437 cmd->gb[gb_len] = 0;
1438
1439 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001440
Samuel Ortizad3823c2012-05-30 23:54:55 +02001441 frame->datalen += cmd_len;
1442
1443 pn533_tx_frame_finish(frame);
1444
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001445 kfree(cmd);
1446
Samuel Ortizad3823c2012-05-30 23:54:55 +02001447 return 0;
1448}
1449
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001450#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001451#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1452static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1453 u8 *params, int params_len)
1454{
1455 struct sk_buff *skb_resp = arg;
1456 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1457
1458 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1459
1460 if (params_len < 0) {
1461 nfc_dev_err(&dev->interface->dev,
1462 "Error %d when starting as a target",
1463 params_len);
1464
1465 return params_len;
1466 }
1467
1468 if (params_len > 0 && params[0] != 0) {
1469 nfc_tm_deactivated(dev->nfc_dev);
1470
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001471 dev->tgt_mode = 0;
1472
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001473 kfree_skb(skb_resp);
1474 return 0;
1475 }
1476
1477 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001478 skb_pull(skb_resp,
1479 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1480 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001481
1482 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1483}
1484
1485static void pn533_wq_tg_get_data(struct work_struct *work)
1486{
1487 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1488 struct pn533_frame *in_frame;
1489 struct sk_buff *skb_resp;
1490 size_t skb_resp_len;
1491
1492 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1493
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001494 skb_resp_len = PN533_FRAME_HEADER_LEN +
1495 PN533_CMD_DATAEXCH_HEAD_LEN +
1496 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1497 PN533_FRAME_TAIL_LEN;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001498
1499 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1500 if (!skb_resp)
1501 return;
1502
1503 in_frame = (struct pn533_frame *)skb_resp->data;
1504
1505 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1506 pn533_tx_frame_finish(dev->out_frame);
1507
1508 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1509 skb_resp_len,
1510 pn533_tm_get_data_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001511 skb_resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001512
1513 return;
1514}
1515
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001516#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001517static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001518{
1519 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001520 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1521 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001522 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001523
1524 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1525
1526 if (params_len < 0) {
1527 nfc_dev_err(&dev->interface->dev,
1528 "Error %d when starting as a target",
1529 params_len);
1530
1531 return params_len;
1532 }
1533
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001534 if (params_len < ATR_REQ_GB_OFFSET + 1)
1535 return -EINVAL;
1536
Samuel Ortizad3823c2012-05-30 23:54:55 +02001537 resp = (struct pn533_cmd_init_target_response *) params;
1538
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001539 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1540 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001541
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001542 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1543 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1544 comm_mode = NFC_COMM_ACTIVE;
1545
1546 /* Again, only DEP */
1547 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1548 return -EOPNOTSUPP;
1549
1550 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1551 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1552
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001553 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1554 comm_mode, gb, gb_len);
1555 if (rc < 0) {
1556 nfc_dev_err(&dev->interface->dev,
1557 "Error when signaling target activation");
1558 return rc;
1559 }
1560
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001561 dev->tgt_mode = 1;
1562
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001563 queue_work(dev->wq, &dev->tg_work);
1564
1565 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001566}
1567
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001569{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001570 struct pn533 *dev = (struct pn533 *) data;
1571
1572 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1573
1574 /* An ack will cancel the last issued command (poll) */
1575 pn533_send_ack(dev, GFP_ATOMIC);
1576
1577 dev->cancel_listen = 1;
1578
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001579 pn533_poll_next_mod(dev);
1580
1581 queue_work(dev->wq, &dev->poll_work);
1582}
1583
1584static int pn533_poll_complete(struct pn533 *dev, void *arg,
1585 u8 *params, int params_len)
1586{
1587 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001588 int rc;
1589
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001590 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1591
1592 if (params_len == -ENOENT) {
1593 if (dev->poll_mod_count != 0)
1594 return 0;
1595
1596 nfc_dev_err(&dev->interface->dev,
1597 "Polling operation has been stopped");
1598
1599 goto stop_poll;
1600 }
1601
1602 if (params_len < 0) {
1603 nfc_dev_err(&dev->interface->dev,
1604 "Error %d when running poll", params_len);
1605
1606 goto stop_poll;
1607 }
1608
1609 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1610
1611 if (cur_mod->len == 0) {
1612 del_timer(&dev->listen_timer);
1613
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001614 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001615 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001616 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001617 if (!rc)
1618 return rc;
1619 }
1620
1621 pn533_poll_next_mod(dev);
1622
1623 queue_work(dev->wq, &dev->poll_work);
1624
1625 return 0;
1626
1627stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001628 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001629 dev->poll_protocols = 0;
1630 return 0;
1631}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001632
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001633static void pn533_build_poll_frame(struct pn533 *dev,
1634 struct pn533_frame *frame,
1635 struct pn533_poll_modulations *mod)
1636{
1637 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001638
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001639 if (mod->len == 0) {
1640 /* Listen mode */
1641 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1642 } else {
1643 /* Polling mode */
1644 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1645
1646 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1647 frame->datalen += mod->len;
1648
1649 pn533_tx_frame_finish(frame);
1650 }
1651}
1652
1653static int pn533_send_poll_frame(struct pn533 *dev)
1654{
1655 struct pn533_poll_modulations *cur_mod;
1656 int rc;
1657
1658 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1659
1660 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001661
1662 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001663 PN533_NORMAL_FRAME_MAX_LEN,
1664 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001665 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001666 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001668
1669 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001670}
1671
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001672static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001673{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001674 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1675 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001676 int rc;
1677
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001678 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1679
1680 nfc_dev_dbg(&dev->interface->dev,
1681 "%s cancel_listen %d modulation len %d",
1682 __func__, dev->cancel_listen, cur_mod->len);
1683
1684 if (dev->cancel_listen == 1) {
1685 dev->cancel_listen = 0;
1686 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001687 }
1688
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001689 rc = pn533_send_poll_frame(dev);
1690 if (rc)
1691 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001692
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001693 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1694 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001695
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001696 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001697}
1698
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001699static int pn533_start_poll(struct nfc_dev *nfc_dev,
1700 u32 im_protocols, u32 tm_protocols)
1701{
1702 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1703
1704 nfc_dev_dbg(&dev->interface->dev,
1705 "%s: im protocols 0x%x tm protocols 0x%x",
1706 __func__, im_protocols, tm_protocols);
1707
1708 if (dev->tgt_active_prot) {
1709 nfc_dev_err(&dev->interface->dev,
1710 "Cannot poll with a target already activated");
1711 return -EBUSY;
1712 }
1713
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001714 if (dev->tgt_mode) {
1715 nfc_dev_err(&dev->interface->dev,
1716 "Cannot poll while already being activated");
1717 return -EBUSY;
1718 }
1719
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001720 if (tm_protocols) {
1721 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1722 if (dev->gb == NULL)
1723 tm_protocols = 0;
1724 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001725
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001726 dev->poll_mod_curr = 0;
1727 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1728 dev->poll_protocols = im_protocols;
1729 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001730
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001731 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001732}
1733
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1735{
1736 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1737
1738 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1739
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001740 del_timer(&dev->listen_timer);
1741
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001742 if (!dev->poll_mod_count) {
1743 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1744 " running");
1745 return;
1746 }
1747
1748 /* An ack will cancel the last issued command (poll) */
1749 pn533_send_ack(dev, GFP_KERNEL);
1750
1751 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1752 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001753
1754 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001755}
1756
1757static int pn533_activate_target_nfcdep(struct pn533 *dev)
1758{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001759 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001760 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001761 int rc;
1762
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001763 struct sk_buff *skb;
1764 struct sk_buff *resp;
1765
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001766 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1767
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001768 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1769 if (!skb)
1770 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001771
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001772 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1773 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001774
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001775 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1776 if (IS_ERR(resp))
1777 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001778
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001779 rsp = (struct pn533_cmd_activate_response *) resp->data;
1780 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001781 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001782 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001783 return -EIO;
1784
Samuel Ortiz541d9202011-12-14 16:43:10 +01001785 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001786 gt_len = resp->len - 16;
1787 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001788
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001789 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001790 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001791}
1792
Eric Lapuyade90099432012-05-07 12:31:13 +02001793static int pn533_activate_target(struct nfc_dev *nfc_dev,
1794 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001795{
1796 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1797 int rc;
1798
1799 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1800 protocol);
1801
1802 if (dev->poll_mod_count) {
1803 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1804 " polling");
1805 return -EBUSY;
1806 }
1807
1808 if (dev->tgt_active_prot) {
1809 nfc_dev_err(&dev->interface->dev, "There is already an active"
1810 " target");
1811 return -EBUSY;
1812 }
1813
1814 if (!dev->tgt_available_prots) {
1815 nfc_dev_err(&dev->interface->dev, "There is no available target"
1816 " to activate");
1817 return -EINVAL;
1818 }
1819
1820 if (!(dev->tgt_available_prots & (1 << protocol))) {
1821 nfc_dev_err(&dev->interface->dev, "The target does not support"
1822 " the requested protocol %u", protocol);
1823 return -EINVAL;
1824 }
1825
1826 if (protocol == NFC_PROTO_NFC_DEP) {
1827 rc = pn533_activate_target_nfcdep(dev);
1828 if (rc) {
1829 nfc_dev_err(&dev->interface->dev, "Error %d when"
1830 " activating target with"
1831 " NFC_DEP protocol", rc);
1832 return rc;
1833 }
1834 }
1835
1836 dev->tgt_active_prot = protocol;
1837 dev->tgt_available_prots = 0;
1838
1839 return 0;
1840}
1841
Eric Lapuyade90099432012-05-07 12:31:13 +02001842static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1843 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001844{
1845 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001846
1847 struct sk_buff *skb;
1848 struct sk_buff *resp;
1849
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001850 int rc;
1851
1852 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1853
1854 if (!dev->tgt_active_prot) {
1855 nfc_dev_err(&dev->interface->dev, "There is no active target");
1856 return;
1857 }
1858
1859 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001860 skb_queue_purge(&dev->resp_q);
1861
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001862 skb = pn533_alloc_skb(sizeof(u8));
1863 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001864 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001865
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001866 *skb_put(skb, 1) = 1; /* TG*/
1867
1868 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1869 if (IS_ERR(resp))
1870 return;
1871
1872 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001873 if (rc != PN533_CMD_RET_SUCCESS)
1874 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1875 " the target", rc);
1876
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001877 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001878 return;
1879}
1880
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001881
1882static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1883 u8 *params, int params_len)
1884{
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001885 struct pn533_cmd_jump_dep_response *resp;
1886 struct nfc_target nfc_target;
1887 u8 target_gt_len;
1888 int rc;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001889 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1890 u8 active = cmd->active;
1891
1892 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001893
1894 if (params_len == -ENOENT) {
1895 nfc_dev_dbg(&dev->interface->dev, "");
1896 return 0;
1897 }
1898
1899 if (params_len < 0) {
1900 nfc_dev_err(&dev->interface->dev,
1901 "Error %d when bringing DEP link up",
1902 params_len);
1903 return 0;
1904 }
1905
1906 if (dev->tgt_available_prots &&
1907 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1908 nfc_dev_err(&dev->interface->dev,
1909 "The target does not support DEP");
1910 return -EINVAL;
1911 }
1912
1913 resp = (struct pn533_cmd_jump_dep_response *) params;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001914 rc = resp->status & PN533_CMD_RET_MASK;
1915 if (rc != PN533_CMD_RET_SUCCESS) {
1916 nfc_dev_err(&dev->interface->dev,
1917 "Bringing DEP link up failed %d", rc);
1918 return 0;
1919 }
1920
1921 if (!dev->tgt_available_prots) {
1922 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1923
1924 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001925 nfc_target.nfcid1_len = 10;
1926 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001927 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1928 if (rc)
1929 return 0;
1930
1931 dev->tgt_available_prots = 0;
1932 }
1933
1934 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1935
1936 /* ATR_RES general bytes are located at offset 17 */
1937 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1938 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1939 resp->gt, target_gt_len);
1940 if (rc == 0)
1941 rc = nfc_dep_link_is_up(dev->nfc_dev,
1942 dev->nfc_dev->targets[0].idx,
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001943 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001944
1945 return 0;
1946}
1947
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001948static int pn533_mod_to_baud(struct pn533 *dev)
1949{
1950 switch (dev->poll_mod_curr) {
1951 case PN533_POLL_MOD_106KBPS_A:
1952 return 0;
1953 case PN533_POLL_MOD_212KBPS_FELICA:
1954 return 1;
1955 case PN533_POLL_MOD_424KBPS_FELICA:
1956 return 2;
1957 default:
1958 return -EINVAL;
1959 }
1960}
1961
Samuel Ortizd7f33452012-05-29 21:45:21 +02001962#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001963static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001964 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001965{
1966 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1967 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001968 u8 cmd_len, *data_ptr;
1969 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001970 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001971
1972 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1973
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001974 if (dev->poll_mod_count) {
1975 nfc_dev_err(&dev->interface->dev,
1976 "Cannot bring the DEP link up while polling");
1977 return -EBUSY;
1978 }
1979
1980 if (dev->tgt_active_prot) {
1981 nfc_dev_err(&dev->interface->dev,
1982 "There is already an active target");
1983 return -EBUSY;
1984 }
1985
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001986 baud = pn533_mod_to_baud(dev);
1987 if (baud < 0) {
1988 nfc_dev_err(&dev->interface->dev,
1989 "Invalid curr modulation %d", dev->poll_mod_curr);
1990 return baud;
1991 }
1992
Samuel Ortiz47807d32012-03-05 01:03:50 +01001993 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001994 if (comm_mode == NFC_COMM_PASSIVE)
1995 cmd_len += PASSIVE_DATA_LEN;
1996
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001997 cmd = kzalloc(cmd_len, GFP_KERNEL);
1998 if (cmd == NULL)
1999 return -ENOMEM;
2000
2001 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
2002
2003 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002004 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002005 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002006 data_ptr = cmd->data;
2007 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
2008 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
2009 cmd->next |= 1;
2010 data_ptr += PASSIVE_DATA_LEN;
2011 }
2012
Samuel Ortiz47807d32012-03-05 01:03:50 +01002013 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02002014 cmd->next |= 4; /* We have some Gi */
2015 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002016 } else {
2017 cmd->next = 0;
2018 }
2019
2020 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2021 dev->out_frame->datalen += cmd_len;
2022
2023 pn533_tx_frame_finish(dev->out_frame);
2024
2025 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002026 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002027 pn533_in_dep_link_up_complete, cmd);
Szymon Janc770f7502012-10-29 14:04:43 +01002028 if (rc < 0)
2029 kfree(cmd);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002030
2031 return rc;
2032}
2033
2034static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2035{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002036 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2037
2038 pn533_poll_reset_mod_list(dev);
2039
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002040 if (dev->tgt_mode || dev->tgt_active_prot) {
2041 pn533_send_ack(dev, GFP_KERNEL);
2042 usb_kill_urb(dev->in_urb);
2043 }
2044
2045 dev->tgt_active_prot = 0;
2046 dev->tgt_mode = 0;
2047
2048 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002049
2050 return 0;
2051}
2052
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002053static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2054 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002055{
2056 int payload_len = skb->len;
2057 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002058 u8 tg;
2059
2060 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2061 payload_len);
2062
2063 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2064 /* TODO: Implement support to multi-part data exchange */
2065 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2066 " max allowed: %d",
2067 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2068 return -ENOSYS;
2069 }
2070
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002071 skb_push(skb, PN533_FRAME_HEADER_LEN);
2072
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002073 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002074 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002075 case PN533_DEVICE_PASORI:
2076 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002077 out_frame = (struct pn533_frame *) skb->data;
2078 pn533_tx_frame_init(out_frame,
2079 PN533_CMD_IN_COMM_THRU);
2080
2081 break;
2082 }
2083
2084 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002085 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2086 out_frame = (struct pn533_frame *) skb->data;
2087 pn533_tx_frame_init(out_frame,
2088 PN533_CMD_IN_DATA_EXCHANGE);
2089 tg = 1;
2090 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2091 &tg, sizeof(u8));
2092 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002093
2094 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002095 }
2096
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002097 } else {
2098 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2099 out_frame = (struct pn533_frame *) skb->data;
2100 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2101 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002102
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002103
2104 /* The data is already in the out_frame, just update the datalen */
2105 out_frame->datalen += payload_len;
2106
2107 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002108 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002109
2110 return 0;
2111}
2112
2113struct pn533_data_exchange_arg {
2114 struct sk_buff *skb_resp;
2115 struct sk_buff *skb_out;
2116 data_exchange_cb_t cb;
2117 void *cb_context;
2118};
2119
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002120static struct sk_buff *pn533_build_response(struct pn533 *dev)
2121{
2122 struct sk_buff *skb, *tmp, *t;
2123 unsigned int skb_len = 0, tmp_len = 0;
2124
2125 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2126
2127 if (skb_queue_empty(&dev->resp_q))
2128 return NULL;
2129
2130 if (skb_queue_len(&dev->resp_q) == 1) {
2131 skb = skb_dequeue(&dev->resp_q);
2132 goto out;
2133 }
2134
2135 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2136 skb_len += tmp->len;
2137
2138 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2139 __func__, skb_len);
2140
2141 skb = alloc_skb(skb_len, GFP_KERNEL);
2142 if (skb == NULL)
2143 goto out;
2144
2145 skb_put(skb, skb_len);
2146
2147 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2148 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2149 tmp_len += tmp->len;
2150 }
2151
2152out:
2153 skb_queue_purge(&dev->resp_q);
2154
2155 return skb;
2156}
2157
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2159 u8 *params, int params_len)
2160{
2161 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002162 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002163 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2164 int err = 0;
2165 u8 status;
2166 u8 cmd_ret;
2167
2168 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2169
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002170 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002171
2172 if (params_len < 0) { /* error */
2173 err = params_len;
2174 goto error;
2175 }
2176
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002177 status = params[0];
2178
2179 cmd_ret = status & PN533_CMD_RET_MASK;
2180 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2181 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2182 " exchanging data", cmd_ret);
2183 err = -EIO;
2184 goto error;
2185 }
2186
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002187 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002188 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002189 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002190 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002191 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002192
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002193 if (status & PN533_CMD_MI_MASK) {
2194 queue_work(dev->wq, &dev->mi_work);
2195 return -EINPROGRESS;
2196 }
2197
2198 skb = pn533_build_response(dev);
2199 if (skb == NULL)
2200 goto error;
2201
2202 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002203 kfree(arg);
2204 return 0;
2205
2206error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002207 skb_queue_purge(&dev->resp_q);
2208 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002209 arg->cb(arg->cb_context, NULL, err);
2210 kfree(arg);
2211 return 0;
2212}
2213
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002214static int pn533_transceive(struct nfc_dev *nfc_dev,
2215 struct nfc_target *target, struct sk_buff *skb,
2216 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002217{
2218 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2219 struct pn533_frame *out_frame, *in_frame;
2220 struct pn533_data_exchange_arg *arg;
2221 struct sk_buff *skb_resp;
2222 int skb_resp_len;
2223 int rc;
2224
2225 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2226
2227 if (!dev->tgt_active_prot) {
2228 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2229 " there is no active target");
2230 rc = -EINVAL;
2231 goto error;
2232 }
2233
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002234 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002235 if (rc)
2236 goto error;
2237
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002238 skb_resp_len = PN533_FRAME_HEADER_LEN +
2239 PN533_CMD_DATAEXCH_HEAD_LEN +
2240 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2241 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002242
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002243 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002244 if (!skb_resp) {
2245 rc = -ENOMEM;
2246 goto error;
2247 }
2248
2249 in_frame = (struct pn533_frame *) skb_resp->data;
2250 out_frame = (struct pn533_frame *) skb->data;
2251
2252 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2253 if (!arg) {
2254 rc = -ENOMEM;
2255 goto free_skb_resp;
2256 }
2257
2258 arg->skb_resp = skb_resp;
2259 arg->skb_out = skb;
2260 arg->cb = cb;
2261 arg->cb_context = cb_context;
2262
2263 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002264 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002265 if (rc) {
2266 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2267 " perform data_exchange", rc);
2268 goto free_arg;
2269 }
2270
2271 return 0;
2272
2273free_arg:
2274 kfree(arg);
2275free_skb_resp:
2276 kfree_skb(skb_resp);
2277error:
2278 kfree_skb(skb);
2279 return rc;
2280}
2281
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002282static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2283 u8 *params, int params_len)
2284{
Thierry Escande5b412fd2012-11-15 18:24:28 +01002285 struct sk_buff *skb_out = arg;
2286
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002287 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2288
Thierry Escande5b412fd2012-11-15 18:24:28 +01002289 dev_kfree_skb(skb_out);
2290
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002291 if (params_len < 0) {
2292 nfc_dev_err(&dev->interface->dev,
2293 "Error %d when sending data",
2294 params_len);
2295
2296 return params_len;
2297 }
2298
2299 if (params_len > 0 && params[0] != 0) {
2300 nfc_tm_deactivated(dev->nfc_dev);
2301
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002302 dev->tgt_mode = 0;
2303
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002304 return 0;
2305 }
2306
2307 queue_work(dev->wq, &dev->tg_work);
2308
2309 return 0;
2310}
2311
2312static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2313{
2314 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2315 struct pn533_frame *out_frame;
2316 int rc;
2317
2318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2319
2320 rc = pn533_build_tx_frame(dev, skb, false);
2321 if (rc)
2322 goto error;
2323
2324 out_frame = (struct pn533_frame *) skb->data;
2325
2326 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002327 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002328 pn533_tm_send_complete, skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002329 if (rc) {
2330 nfc_dev_err(&dev->interface->dev,
2331 "Error %d when trying to send data", rc);
2332 goto error;
2333 }
2334
2335 return 0;
2336
2337error:
2338 kfree_skb(skb);
2339
2340 return rc;
2341}
2342
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002343static void pn533_wq_mi_recv(struct work_struct *work)
2344{
2345 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2346 struct sk_buff *skb_cmd;
2347 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2348 struct pn533_frame *out_frame, *in_frame;
2349 struct sk_buff *skb_resp;
2350 int skb_resp_len;
2351 int rc;
2352
2353 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2354
2355 /* This is a zero payload size skb */
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002356 skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002357 if (skb_cmd == NULL)
2358 goto error_cmd;
2359
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002360 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002361
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002362 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002363 if (rc)
2364 goto error_frame;
2365
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002366 skb_resp_len = PN533_FRAME_HEADER_LEN +
2367 PN533_CMD_DATAEXCH_HEAD_LEN +
2368 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2369 PN533_FRAME_TAIL_LEN;
2370
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002371 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2372 if (!skb_resp) {
2373 rc = -ENOMEM;
2374 goto error_frame;
2375 }
2376
2377 in_frame = (struct pn533_frame *) skb_resp->data;
2378 out_frame = (struct pn533_frame *) skb_cmd->data;
2379
2380 arg->skb_resp = skb_resp;
2381 arg->skb_out = skb_cmd;
2382
2383 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2384 skb_resp_len,
2385 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002386 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002387 if (!rc)
2388 return;
2389
2390 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2391 " perform data_exchange", rc);
2392
2393 kfree_skb(skb_resp);
2394
2395error_frame:
2396 kfree_skb(skb_cmd);
2397
2398error_cmd:
2399 pn533_send_ack(dev, GFP_KERNEL);
2400
2401 kfree(arg);
2402
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002403 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002404}
2405
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002406static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2407 u8 cfgdata_len)
2408{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002409 struct sk_buff *skb;
2410 struct sk_buff *resp;
2411
2412 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002413
2414 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2415
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002416 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002417
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002418 skb = pn533_alloc_skb(skb_len);
2419 if (!skb)
2420 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002421
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002422 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2423 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002424
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002425 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2426 if (IS_ERR(resp))
2427 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002429 dev_kfree_skb(resp);
2430 return 0;
2431}
2432
2433static int pn533_get_firmware_version(struct pn533 *dev,
2434 struct pn533_fw_version *fv)
2435{
2436 struct sk_buff *skb;
2437 struct sk_buff *resp;
2438
2439 skb = pn533_alloc_skb(0);
2440 if (!skb)
2441 return -ENOMEM;
2442
2443 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2444 if (IS_ERR(resp))
2445 return PTR_ERR(resp);
2446
2447 fv->ic = resp->data[0];
2448 fv->ver = resp->data[1];
2449 fv->rev = resp->data[2];
2450 fv->support = resp->data[3];
2451
2452 dev_kfree_skb(resp);
2453 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002454}
2455
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002456static int pn533_fw_reset(struct pn533 *dev)
2457{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002458 struct sk_buff *skb;
2459 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002460
2461 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2462
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002463 skb = pn533_alloc_skb(sizeof(u8));
2464 if (!skb)
2465 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002466
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002467 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002468
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002469 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2470 if (IS_ERR(resp))
2471 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002472
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002473 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002474
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002475 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002476}
2477
2478static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002479 .dev_up = NULL,
2480 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002481 .dep_link_up = pn533_dep_link_up,
2482 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002483 .start_poll = pn533_start_poll,
2484 .stop_poll = pn533_stop_poll,
2485 .activate_target = pn533_activate_target,
2486 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002487 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002488 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002489};
2490
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002491static int pn533_setup(struct pn533 *dev)
2492{
2493 struct pn533_config_max_retries max_retries;
2494 struct pn533_config_timing timing;
2495 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2496 int rc;
2497
2498 switch (dev->device_type) {
2499 case PN533_DEVICE_STD:
2500 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2501 max_retries.mx_rty_psl = 2;
2502 max_retries.mx_rty_passive_act =
2503 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2504
2505 timing.rfu = PN533_CONFIG_TIMING_102;
2506 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2507 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2508
2509 break;
2510
2511 case PN533_DEVICE_PASORI:
2512 max_retries.mx_rty_atr = 0x2;
2513 max_retries.mx_rty_psl = 0x1;
2514 max_retries.mx_rty_passive_act =
2515 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2516
2517 timing.rfu = PN533_CONFIG_TIMING_102;
2518 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2519 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2520
2521 break;
2522
2523 default:
2524 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2525 dev->device_type);
2526 return -EINVAL;
2527 }
2528
2529 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2530 (u8 *)&max_retries, sizeof(max_retries));
2531 if (rc) {
2532 nfc_dev_err(&dev->interface->dev,
2533 "Error on setting MAX_RETRIES config");
2534 return rc;
2535 }
2536
2537
2538 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2539 (u8 *)&timing, sizeof(timing));
2540 if (rc) {
2541 nfc_dev_err(&dev->interface->dev,
2542 "Error on setting RF timings");
2543 return rc;
2544 }
2545
2546 switch (dev->device_type) {
2547 case PN533_DEVICE_STD:
2548 break;
2549
2550 case PN533_DEVICE_PASORI:
2551 pn533_fw_reset(dev);
2552
2553 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2554 pasori_cfg, 3);
2555 if (rc) {
2556 nfc_dev_err(&dev->interface->dev,
2557 "Error while settings PASORI config");
2558 return rc;
2559 }
2560
2561 pn533_fw_reset(dev);
2562
2563 break;
2564 }
2565
2566 return 0;
2567}
2568
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002569static int pn533_probe(struct usb_interface *interface,
2570 const struct usb_device_id *id)
2571{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002572 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002573 struct pn533 *dev;
2574 struct usb_host_interface *iface_desc;
2575 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002576 int in_endpoint = 0;
2577 int out_endpoint = 0;
2578 int rc = -ENOMEM;
2579 int i;
2580 u32 protocols;
2581
2582 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2583 if (!dev)
2584 return -ENOMEM;
2585
2586 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2587 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002588 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002589
2590 iface_desc = interface->cur_altsetting;
2591 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2592 endpoint = &iface_desc->endpoint[i].desc;
2593
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002594 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002596
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002597 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002598 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002599 }
2600
2601 if (!in_endpoint || !out_endpoint) {
2602 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2603 " bulk-out endpoint");
2604 rc = -ENODEV;
2605 goto error;
2606 }
2607
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002608 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002610 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002611 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2612
2613 if (!dev->in_frame || !dev->out_frame ||
2614 !dev->in_urb || !dev->out_urb)
2615 goto error;
2616
2617 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2618 usb_rcvbulkpipe(dev->udev, in_endpoint),
2619 NULL, 0, NULL, dev);
2620 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2621 usb_sndbulkpipe(dev->udev, out_endpoint),
2622 NULL, 0,
2623 pn533_send_complete, dev);
2624
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002625 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2626 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002627 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002628 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002629 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002630 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002631 if (dev->wq == NULL)
2632 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002633
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002634 init_timer(&dev->listen_timer);
2635 dev->listen_timer.data = (unsigned long) dev;
2636 dev->listen_timer.function = pn533_listen_mode_timer;
2637
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002638 skb_queue_head_init(&dev->resp_q);
2639
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002640 INIT_LIST_HEAD(&dev->cmd_queue);
2641
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002642 usb_set_intfdata(interface, dev);
2643
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002644 memset(&fw_ver, 0, sizeof(fw_ver));
2645 rc = pn533_get_firmware_version(dev, &fw_ver);
2646 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002647 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002648
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002649 nfc_dev_info(&dev->interface->dev,
2650 "NXP PN533 firmware ver %d.%d now attached",
2651 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002652
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002653 dev->device_type = id->driver_info;
2654 switch (dev->device_type) {
2655 case PN533_DEVICE_STD:
2656 protocols = PN533_ALL_PROTOCOLS;
2657 break;
2658
2659 case PN533_DEVICE_PASORI:
2660 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2661 break;
2662
2663 default:
2664 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2665 dev->device_type);
2666 rc = -EINVAL;
2667 goto destroy_wq;
2668 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002669
Samuel Ortize8753042011-08-19 15:47:11 +02002670 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002671 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002672 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002673 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002674 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002675 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002676
2677 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2678 nfc_set_drvdata(dev->nfc_dev, dev);
2679
2680 rc = nfc_register_device(dev->nfc_dev);
2681 if (rc)
2682 goto free_nfc_dev;
2683
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002684 rc = pn533_setup(dev);
2685 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002686 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002687
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002688 return 0;
2689
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002690unregister_nfc_dev:
2691 nfc_unregister_device(dev->nfc_dev);
2692
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002693free_nfc_dev:
2694 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002695
Samuel Ortiz4849f852012-04-10 19:43:17 +02002696destroy_wq:
2697 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002698error:
2699 kfree(dev->in_frame);
2700 usb_free_urb(dev->in_urb);
2701 kfree(dev->out_frame);
2702 usb_free_urb(dev->out_urb);
2703 kfree(dev);
2704 return rc;
2705}
2706
2707static void pn533_disconnect(struct usb_interface *interface)
2708{
2709 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002710 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002711
2712 dev = usb_get_intfdata(interface);
2713 usb_set_intfdata(interface, NULL);
2714
2715 nfc_unregister_device(dev->nfc_dev);
2716 nfc_free_device(dev->nfc_dev);
2717
2718 usb_kill_urb(dev->in_urb);
2719 usb_kill_urb(dev->out_urb);
2720
Samuel Ortiz4849f852012-04-10 19:43:17 +02002721 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002722
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002723 skb_queue_purge(&dev->resp_q);
2724
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002725 del_timer(&dev->listen_timer);
2726
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002727 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2728 list_del(&cmd->queue);
2729 kfree(cmd);
2730 }
2731
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002732 kfree(dev->in_frame);
2733 usb_free_urb(dev->in_urb);
2734 kfree(dev->out_frame);
2735 usb_free_urb(dev->out_urb);
2736 kfree(dev);
2737
Dan Carpenter276556d2011-07-08 10:21:15 +03002738 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002739}
2740
2741static struct usb_driver pn533_driver = {
2742 .name = "pn533",
2743 .probe = pn533_probe,
2744 .disconnect = pn533_disconnect,
2745 .id_table = pn533_table,
2746};
2747
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002748module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002749
2750MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2751 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2752MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2753MODULE_VERSION(VERSION);
2754MODULE_LICENSE("GPL");