blob: 9912824030f61e78b386c621221eacbbee3539a2 [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,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001453 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001454{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001455 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001456
1457 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1458
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001459 if (IS_ERR(resp))
1460 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001461
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001462 status = resp->data[0];
1463 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001464
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001465 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001466 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001467 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001468 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001469 return 0;
1470 }
1471
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001472 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001473}
1474
1475static void pn533_wq_tg_get_data(struct work_struct *work)
1476{
1477 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001478
1479 struct sk_buff *skb;
1480 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001481
1482 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1483
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001484 skb = pn533_alloc_skb(0);
1485 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001486 return;
1487
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001488 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1489 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001490
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001491 if (rc < 0)
1492 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001493
1494 return;
1495}
1496
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001497#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001498static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001499{
1500 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001501 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1502 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001503 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001504
1505 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1506
1507 if (params_len < 0) {
1508 nfc_dev_err(&dev->interface->dev,
1509 "Error %d when starting as a target",
1510 params_len);
1511
1512 return params_len;
1513 }
1514
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001515 if (params_len < ATR_REQ_GB_OFFSET + 1)
1516 return -EINVAL;
1517
Samuel Ortizad3823c2012-05-30 23:54:55 +02001518 resp = (struct pn533_cmd_init_target_response *) params;
1519
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001520 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1521 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001522
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001523 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1524 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1525 comm_mode = NFC_COMM_ACTIVE;
1526
1527 /* Again, only DEP */
1528 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1529 return -EOPNOTSUPP;
1530
1531 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1532 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1533
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001534 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1535 comm_mode, gb, gb_len);
1536 if (rc < 0) {
1537 nfc_dev_err(&dev->interface->dev,
1538 "Error when signaling target activation");
1539 return rc;
1540 }
1541
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001542 dev->tgt_mode = 1;
1543
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001544 queue_work(dev->wq, &dev->tg_work);
1545
1546 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001547}
1548
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001549static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001550{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001551 struct pn533 *dev = (struct pn533 *) data;
1552
1553 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1554
1555 /* An ack will cancel the last issued command (poll) */
1556 pn533_send_ack(dev, GFP_ATOMIC);
1557
1558 dev->cancel_listen = 1;
1559
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560 pn533_poll_next_mod(dev);
1561
1562 queue_work(dev->wq, &dev->poll_work);
1563}
1564
1565static int pn533_poll_complete(struct pn533 *dev, void *arg,
1566 u8 *params, int params_len)
1567{
1568 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001569 int rc;
1570
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1572
1573 if (params_len == -ENOENT) {
1574 if (dev->poll_mod_count != 0)
1575 return 0;
1576
1577 nfc_dev_err(&dev->interface->dev,
1578 "Polling operation has been stopped");
1579
1580 goto stop_poll;
1581 }
1582
1583 if (params_len < 0) {
1584 nfc_dev_err(&dev->interface->dev,
1585 "Error %d when running poll", params_len);
1586
1587 goto stop_poll;
1588 }
1589
1590 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1591
1592 if (cur_mod->len == 0) {
1593 del_timer(&dev->listen_timer);
1594
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001595 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001596 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001597 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001598 if (!rc)
1599 return rc;
1600 }
1601
1602 pn533_poll_next_mod(dev);
1603
1604 queue_work(dev->wq, &dev->poll_work);
1605
1606 return 0;
1607
1608stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001609 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 dev->poll_protocols = 0;
1611 return 0;
1612}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001613
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001614static void pn533_build_poll_frame(struct pn533 *dev,
1615 struct pn533_frame *frame,
1616 struct pn533_poll_modulations *mod)
1617{
1618 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001619
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001620 if (mod->len == 0) {
1621 /* Listen mode */
1622 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1623 } else {
1624 /* Polling mode */
1625 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1626
1627 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1628 frame->datalen += mod->len;
1629
1630 pn533_tx_frame_finish(frame);
1631 }
1632}
1633
1634static int pn533_send_poll_frame(struct pn533 *dev)
1635{
1636 struct pn533_poll_modulations *cur_mod;
1637 int rc;
1638
1639 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1640
1641 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001642
1643 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001644 PN533_NORMAL_FRAME_MAX_LEN,
1645 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001646 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001647 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001648 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001649
1650 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001651}
1652
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001653static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001654{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001655 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1656 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001657 int rc;
1658
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001659 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1660
1661 nfc_dev_dbg(&dev->interface->dev,
1662 "%s cancel_listen %d modulation len %d",
1663 __func__, dev->cancel_listen, cur_mod->len);
1664
1665 if (dev->cancel_listen == 1) {
1666 dev->cancel_listen = 0;
1667 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001668 }
1669
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001670 rc = pn533_send_poll_frame(dev);
1671 if (rc)
1672 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001673
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001674 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1675 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001676
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001677 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678}
1679
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001680static int pn533_start_poll(struct nfc_dev *nfc_dev,
1681 u32 im_protocols, u32 tm_protocols)
1682{
1683 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1684
1685 nfc_dev_dbg(&dev->interface->dev,
1686 "%s: im protocols 0x%x tm protocols 0x%x",
1687 __func__, im_protocols, tm_protocols);
1688
1689 if (dev->tgt_active_prot) {
1690 nfc_dev_err(&dev->interface->dev,
1691 "Cannot poll with a target already activated");
1692 return -EBUSY;
1693 }
1694
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001695 if (dev->tgt_mode) {
1696 nfc_dev_err(&dev->interface->dev,
1697 "Cannot poll while already being activated");
1698 return -EBUSY;
1699 }
1700
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001701 if (tm_protocols) {
1702 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1703 if (dev->gb == NULL)
1704 tm_protocols = 0;
1705 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001706
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001707 dev->poll_mod_curr = 0;
1708 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1709 dev->poll_protocols = im_protocols;
1710 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001711
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001712 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001713}
1714
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001715static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1716{
1717 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1718
1719 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1720
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001721 del_timer(&dev->listen_timer);
1722
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001723 if (!dev->poll_mod_count) {
1724 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1725 " running");
1726 return;
1727 }
1728
1729 /* An ack will cancel the last issued command (poll) */
1730 pn533_send_ack(dev, GFP_KERNEL);
1731
1732 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1733 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001734
1735 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001736}
1737
1738static int pn533_activate_target_nfcdep(struct pn533 *dev)
1739{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001740 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001741 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001742 int rc;
1743
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001744 struct sk_buff *skb;
1745 struct sk_buff *resp;
1746
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1748
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001749 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1750 if (!skb)
1751 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001752
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001753 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1754 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001755
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001756 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1757 if (IS_ERR(resp))
1758 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001759
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001760 rsp = (struct pn533_cmd_activate_response *) resp->data;
1761 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001762 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001763 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001764 return -EIO;
1765
Samuel Ortiz541d9202011-12-14 16:43:10 +01001766 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001767 gt_len = resp->len - 16;
1768 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001769
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001770 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001771 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001772}
1773
Eric Lapuyade90099432012-05-07 12:31:13 +02001774static int pn533_activate_target(struct nfc_dev *nfc_dev,
1775 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001776{
1777 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1778 int rc;
1779
1780 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1781 protocol);
1782
1783 if (dev->poll_mod_count) {
1784 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1785 " polling");
1786 return -EBUSY;
1787 }
1788
1789 if (dev->tgt_active_prot) {
1790 nfc_dev_err(&dev->interface->dev, "There is already an active"
1791 " target");
1792 return -EBUSY;
1793 }
1794
1795 if (!dev->tgt_available_prots) {
1796 nfc_dev_err(&dev->interface->dev, "There is no available target"
1797 " to activate");
1798 return -EINVAL;
1799 }
1800
1801 if (!(dev->tgt_available_prots & (1 << protocol))) {
1802 nfc_dev_err(&dev->interface->dev, "The target does not support"
1803 " the requested protocol %u", protocol);
1804 return -EINVAL;
1805 }
1806
1807 if (protocol == NFC_PROTO_NFC_DEP) {
1808 rc = pn533_activate_target_nfcdep(dev);
1809 if (rc) {
1810 nfc_dev_err(&dev->interface->dev, "Error %d when"
1811 " activating target with"
1812 " NFC_DEP protocol", rc);
1813 return rc;
1814 }
1815 }
1816
1817 dev->tgt_active_prot = protocol;
1818 dev->tgt_available_prots = 0;
1819
1820 return 0;
1821}
1822
Eric Lapuyade90099432012-05-07 12:31:13 +02001823static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1824 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001825{
1826 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001827
1828 struct sk_buff *skb;
1829 struct sk_buff *resp;
1830
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001831 int rc;
1832
1833 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1834
1835 if (!dev->tgt_active_prot) {
1836 nfc_dev_err(&dev->interface->dev, "There is no active target");
1837 return;
1838 }
1839
1840 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001841 skb_queue_purge(&dev->resp_q);
1842
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001843 skb = pn533_alloc_skb(sizeof(u8));
1844 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001845 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001846
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001847 *skb_put(skb, 1) = 1; /* TG*/
1848
1849 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1850 if (IS_ERR(resp))
1851 return;
1852
1853 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001854 if (rc != PN533_CMD_RET_SUCCESS)
1855 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1856 " the target", rc);
1857
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001858 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001859 return;
1860}
1861
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862
1863static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1864 u8 *params, int params_len)
1865{
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001866 struct pn533_cmd_jump_dep_response *resp;
1867 struct nfc_target nfc_target;
1868 u8 target_gt_len;
1869 int rc;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001870 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1871 u8 active = cmd->active;
1872
1873 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001874
1875 if (params_len == -ENOENT) {
1876 nfc_dev_dbg(&dev->interface->dev, "");
1877 return 0;
1878 }
1879
1880 if (params_len < 0) {
1881 nfc_dev_err(&dev->interface->dev,
1882 "Error %d when bringing DEP link up",
1883 params_len);
1884 return 0;
1885 }
1886
1887 if (dev->tgt_available_prots &&
1888 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1889 nfc_dev_err(&dev->interface->dev,
1890 "The target does not support DEP");
1891 return -EINVAL;
1892 }
1893
1894 resp = (struct pn533_cmd_jump_dep_response *) params;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001895 rc = resp->status & PN533_CMD_RET_MASK;
1896 if (rc != PN533_CMD_RET_SUCCESS) {
1897 nfc_dev_err(&dev->interface->dev,
1898 "Bringing DEP link up failed %d", rc);
1899 return 0;
1900 }
1901
1902 if (!dev->tgt_available_prots) {
1903 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1904
1905 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001906 nfc_target.nfcid1_len = 10;
1907 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001908 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1909 if (rc)
1910 return 0;
1911
1912 dev->tgt_available_prots = 0;
1913 }
1914
1915 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1916
1917 /* ATR_RES general bytes are located at offset 17 */
1918 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1919 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1920 resp->gt, target_gt_len);
1921 if (rc == 0)
1922 rc = nfc_dep_link_is_up(dev->nfc_dev,
1923 dev->nfc_dev->targets[0].idx,
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001924 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001925
1926 return 0;
1927}
1928
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001929static int pn533_mod_to_baud(struct pn533 *dev)
1930{
1931 switch (dev->poll_mod_curr) {
1932 case PN533_POLL_MOD_106KBPS_A:
1933 return 0;
1934 case PN533_POLL_MOD_212KBPS_FELICA:
1935 return 1;
1936 case PN533_POLL_MOD_424KBPS_FELICA:
1937 return 2;
1938 default:
1939 return -EINVAL;
1940 }
1941}
1942
Samuel Ortizd7f33452012-05-29 21:45:21 +02001943#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001944static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001945 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001946{
1947 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1948 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001949 u8 cmd_len, *data_ptr;
1950 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001951 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001952
1953 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1954
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001955 if (dev->poll_mod_count) {
1956 nfc_dev_err(&dev->interface->dev,
1957 "Cannot bring the DEP link up while polling");
1958 return -EBUSY;
1959 }
1960
1961 if (dev->tgt_active_prot) {
1962 nfc_dev_err(&dev->interface->dev,
1963 "There is already an active target");
1964 return -EBUSY;
1965 }
1966
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001967 baud = pn533_mod_to_baud(dev);
1968 if (baud < 0) {
1969 nfc_dev_err(&dev->interface->dev,
1970 "Invalid curr modulation %d", dev->poll_mod_curr);
1971 return baud;
1972 }
1973
Samuel Ortiz47807d32012-03-05 01:03:50 +01001974 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001975 if (comm_mode == NFC_COMM_PASSIVE)
1976 cmd_len += PASSIVE_DATA_LEN;
1977
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001978 cmd = kzalloc(cmd_len, GFP_KERNEL);
1979 if (cmd == NULL)
1980 return -ENOMEM;
1981
1982 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1983
1984 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001985 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001986 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001987 data_ptr = cmd->data;
1988 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
1989 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
1990 cmd->next |= 1;
1991 data_ptr += PASSIVE_DATA_LEN;
1992 }
1993
Samuel Ortiz47807d32012-03-05 01:03:50 +01001994 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02001995 cmd->next |= 4; /* We have some Gi */
1996 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001997 } else {
1998 cmd->next = 0;
1999 }
2000
2001 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2002 dev->out_frame->datalen += cmd_len;
2003
2004 pn533_tx_frame_finish(dev->out_frame);
2005
2006 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002007 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002008 pn533_in_dep_link_up_complete, cmd);
Szymon Janc770f7502012-10-29 14:04:43 +01002009 if (rc < 0)
2010 kfree(cmd);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002011
2012 return rc;
2013}
2014
2015static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2016{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002017 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2018
2019 pn533_poll_reset_mod_list(dev);
2020
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002021 if (dev->tgt_mode || dev->tgt_active_prot) {
2022 pn533_send_ack(dev, GFP_KERNEL);
2023 usb_kill_urb(dev->in_urb);
2024 }
2025
2026 dev->tgt_active_prot = 0;
2027 dev->tgt_mode = 0;
2028
2029 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002030
2031 return 0;
2032}
2033
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002034static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2035 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002036{
2037 int payload_len = skb->len;
2038 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002039 u8 tg;
2040
2041 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2042 payload_len);
2043
2044 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2045 /* TODO: Implement support to multi-part data exchange */
2046 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2047 " max allowed: %d",
2048 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2049 return -ENOSYS;
2050 }
2051
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002052 skb_push(skb, PN533_FRAME_HEADER_LEN);
2053
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002054 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002055 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002056 case PN533_DEVICE_PASORI:
2057 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002058 out_frame = (struct pn533_frame *) skb->data;
2059 pn533_tx_frame_init(out_frame,
2060 PN533_CMD_IN_COMM_THRU);
2061
2062 break;
2063 }
2064
2065 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002066 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2067 out_frame = (struct pn533_frame *) skb->data;
2068 pn533_tx_frame_init(out_frame,
2069 PN533_CMD_IN_DATA_EXCHANGE);
2070 tg = 1;
2071 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2072 &tg, sizeof(u8));
2073 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002074
2075 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002076 }
2077
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002078 } else {
2079 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2080 out_frame = (struct pn533_frame *) skb->data;
2081 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2082 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002083
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002084
2085 /* The data is already in the out_frame, just update the datalen */
2086 out_frame->datalen += payload_len;
2087
2088 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002089 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002090
2091 return 0;
2092}
2093
2094struct pn533_data_exchange_arg {
2095 struct sk_buff *skb_resp;
2096 struct sk_buff *skb_out;
2097 data_exchange_cb_t cb;
2098 void *cb_context;
2099};
2100
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002101static struct sk_buff *pn533_build_response(struct pn533 *dev)
2102{
2103 struct sk_buff *skb, *tmp, *t;
2104 unsigned int skb_len = 0, tmp_len = 0;
2105
2106 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2107
2108 if (skb_queue_empty(&dev->resp_q))
2109 return NULL;
2110
2111 if (skb_queue_len(&dev->resp_q) == 1) {
2112 skb = skb_dequeue(&dev->resp_q);
2113 goto out;
2114 }
2115
2116 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2117 skb_len += tmp->len;
2118
2119 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2120 __func__, skb_len);
2121
2122 skb = alloc_skb(skb_len, GFP_KERNEL);
2123 if (skb == NULL)
2124 goto out;
2125
2126 skb_put(skb, skb_len);
2127
2128 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2129 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2130 tmp_len += tmp->len;
2131 }
2132
2133out:
2134 skb_queue_purge(&dev->resp_q);
2135
2136 return skb;
2137}
2138
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002139static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2140 u8 *params, int params_len)
2141{
2142 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002143 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002144 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2145 int err = 0;
2146 u8 status;
2147 u8 cmd_ret;
2148
2149 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2150
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002151 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002152
2153 if (params_len < 0) { /* error */
2154 err = params_len;
2155 goto error;
2156 }
2157
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158 status = params[0];
2159
2160 cmd_ret = status & PN533_CMD_RET_MASK;
2161 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2162 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2163 " exchanging data", cmd_ret);
2164 err = -EIO;
2165 goto error;
2166 }
2167
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002168 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002169 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002170 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002171 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002172 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002173
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002174 if (status & PN533_CMD_MI_MASK) {
2175 queue_work(dev->wq, &dev->mi_work);
2176 return -EINPROGRESS;
2177 }
2178
2179 skb = pn533_build_response(dev);
2180 if (skb == NULL)
2181 goto error;
2182
2183 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002184 kfree(arg);
2185 return 0;
2186
2187error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002188 skb_queue_purge(&dev->resp_q);
2189 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002190 arg->cb(arg->cb_context, NULL, err);
2191 kfree(arg);
2192 return 0;
2193}
2194
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002195static int pn533_transceive(struct nfc_dev *nfc_dev,
2196 struct nfc_target *target, struct sk_buff *skb,
2197 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002198{
2199 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2200 struct pn533_frame *out_frame, *in_frame;
2201 struct pn533_data_exchange_arg *arg;
2202 struct sk_buff *skb_resp;
2203 int skb_resp_len;
2204 int rc;
2205
2206 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2207
2208 if (!dev->tgt_active_prot) {
2209 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2210 " there is no active target");
2211 rc = -EINVAL;
2212 goto error;
2213 }
2214
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002215 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002216 if (rc)
2217 goto error;
2218
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002219 skb_resp_len = PN533_FRAME_HEADER_LEN +
2220 PN533_CMD_DATAEXCH_HEAD_LEN +
2221 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2222 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002223
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002224 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002225 if (!skb_resp) {
2226 rc = -ENOMEM;
2227 goto error;
2228 }
2229
2230 in_frame = (struct pn533_frame *) skb_resp->data;
2231 out_frame = (struct pn533_frame *) skb->data;
2232
2233 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2234 if (!arg) {
2235 rc = -ENOMEM;
2236 goto free_skb_resp;
2237 }
2238
2239 arg->skb_resp = skb_resp;
2240 arg->skb_out = skb;
2241 arg->cb = cb;
2242 arg->cb_context = cb_context;
2243
2244 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002245 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002246 if (rc) {
2247 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2248 " perform data_exchange", rc);
2249 goto free_arg;
2250 }
2251
2252 return 0;
2253
2254free_arg:
2255 kfree(arg);
2256free_skb_resp:
2257 kfree_skb(skb_resp);
2258error:
2259 kfree_skb(skb);
2260 return rc;
2261}
2262
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002263static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002264 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002265{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002266 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002267
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002268 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2269
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002270 if (IS_ERR(resp))
2271 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002272
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002273 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002274
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002275 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002276
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002277 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002278 nfc_tm_deactivated(dev->nfc_dev);
2279
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002280 dev->tgt_mode = 0;
2281
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002282 return 0;
2283 }
2284
2285 queue_work(dev->wq, &dev->tg_work);
2286
2287 return 0;
2288}
2289
2290static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2291{
2292 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002293 int rc;
2294
2295 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2296
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002297 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002298 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002299 "Data length greater than the max allowed: %d",
2300 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2301 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002302 }
2303
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002304 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2305 pn533_tm_send_complete, NULL);
2306 if (rc < 0)
2307 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002308
2309 return rc;
2310}
2311
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002312static void pn533_wq_mi_recv(struct work_struct *work)
2313{
2314 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2315 struct sk_buff *skb_cmd;
2316 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2317 struct pn533_frame *out_frame, *in_frame;
2318 struct sk_buff *skb_resp;
2319 int skb_resp_len;
2320 int rc;
2321
2322 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2323
2324 /* This is a zero payload size skb */
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002325 skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002326 if (skb_cmd == NULL)
2327 goto error_cmd;
2328
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002329 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002330
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002331 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002332 if (rc)
2333 goto error_frame;
2334
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002335 skb_resp_len = PN533_FRAME_HEADER_LEN +
2336 PN533_CMD_DATAEXCH_HEAD_LEN +
2337 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2338 PN533_FRAME_TAIL_LEN;
2339
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002340 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2341 if (!skb_resp) {
2342 rc = -ENOMEM;
2343 goto error_frame;
2344 }
2345
2346 in_frame = (struct pn533_frame *) skb_resp->data;
2347 out_frame = (struct pn533_frame *) skb_cmd->data;
2348
2349 arg->skb_resp = skb_resp;
2350 arg->skb_out = skb_cmd;
2351
2352 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2353 skb_resp_len,
2354 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002355 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002356 if (!rc)
2357 return;
2358
2359 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2360 " perform data_exchange", rc);
2361
2362 kfree_skb(skb_resp);
2363
2364error_frame:
2365 kfree_skb(skb_cmd);
2366
2367error_cmd:
2368 pn533_send_ack(dev, GFP_KERNEL);
2369
2370 kfree(arg);
2371
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002372 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002373}
2374
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002375static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2376 u8 cfgdata_len)
2377{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002378 struct sk_buff *skb;
2379 struct sk_buff *resp;
2380
2381 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002382
2383 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2384
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002385 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002386
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002387 skb = pn533_alloc_skb(skb_len);
2388 if (!skb)
2389 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002390
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002391 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2392 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002393
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002394 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2395 if (IS_ERR(resp))
2396 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002397
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002398 dev_kfree_skb(resp);
2399 return 0;
2400}
2401
2402static int pn533_get_firmware_version(struct pn533 *dev,
2403 struct pn533_fw_version *fv)
2404{
2405 struct sk_buff *skb;
2406 struct sk_buff *resp;
2407
2408 skb = pn533_alloc_skb(0);
2409 if (!skb)
2410 return -ENOMEM;
2411
2412 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2413 if (IS_ERR(resp))
2414 return PTR_ERR(resp);
2415
2416 fv->ic = resp->data[0];
2417 fv->ver = resp->data[1];
2418 fv->rev = resp->data[2];
2419 fv->support = resp->data[3];
2420
2421 dev_kfree_skb(resp);
2422 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423}
2424
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002425static int pn533_fw_reset(struct pn533 *dev)
2426{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002427 struct sk_buff *skb;
2428 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002429
2430 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2431
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002432 skb = pn533_alloc_skb(sizeof(u8));
2433 if (!skb)
2434 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002435
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002436 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002437
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002438 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2439 if (IS_ERR(resp))
2440 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002441
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002442 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002443
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002444 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002445}
2446
2447static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002448 .dev_up = NULL,
2449 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002450 .dep_link_up = pn533_dep_link_up,
2451 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002452 .start_poll = pn533_start_poll,
2453 .stop_poll = pn533_stop_poll,
2454 .activate_target = pn533_activate_target,
2455 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002456 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002457 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458};
2459
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002460static int pn533_setup(struct pn533 *dev)
2461{
2462 struct pn533_config_max_retries max_retries;
2463 struct pn533_config_timing timing;
2464 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2465 int rc;
2466
2467 switch (dev->device_type) {
2468 case PN533_DEVICE_STD:
2469 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2470 max_retries.mx_rty_psl = 2;
2471 max_retries.mx_rty_passive_act =
2472 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2473
2474 timing.rfu = PN533_CONFIG_TIMING_102;
2475 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2476 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2477
2478 break;
2479
2480 case PN533_DEVICE_PASORI:
2481 max_retries.mx_rty_atr = 0x2;
2482 max_retries.mx_rty_psl = 0x1;
2483 max_retries.mx_rty_passive_act =
2484 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2485
2486 timing.rfu = PN533_CONFIG_TIMING_102;
2487 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2488 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2489
2490 break;
2491
2492 default:
2493 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2494 dev->device_type);
2495 return -EINVAL;
2496 }
2497
2498 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2499 (u8 *)&max_retries, sizeof(max_retries));
2500 if (rc) {
2501 nfc_dev_err(&dev->interface->dev,
2502 "Error on setting MAX_RETRIES config");
2503 return rc;
2504 }
2505
2506
2507 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2508 (u8 *)&timing, sizeof(timing));
2509 if (rc) {
2510 nfc_dev_err(&dev->interface->dev,
2511 "Error on setting RF timings");
2512 return rc;
2513 }
2514
2515 switch (dev->device_type) {
2516 case PN533_DEVICE_STD:
2517 break;
2518
2519 case PN533_DEVICE_PASORI:
2520 pn533_fw_reset(dev);
2521
2522 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2523 pasori_cfg, 3);
2524 if (rc) {
2525 nfc_dev_err(&dev->interface->dev,
2526 "Error while settings PASORI config");
2527 return rc;
2528 }
2529
2530 pn533_fw_reset(dev);
2531
2532 break;
2533 }
2534
2535 return 0;
2536}
2537
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538static int pn533_probe(struct usb_interface *interface,
2539 const struct usb_device_id *id)
2540{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002541 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002542 struct pn533 *dev;
2543 struct usb_host_interface *iface_desc;
2544 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002545 int in_endpoint = 0;
2546 int out_endpoint = 0;
2547 int rc = -ENOMEM;
2548 int i;
2549 u32 protocols;
2550
2551 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2552 if (!dev)
2553 return -ENOMEM;
2554
2555 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2556 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002557 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002558
2559 iface_desc = interface->cur_altsetting;
2560 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2561 endpoint = &iface_desc->endpoint[i].desc;
2562
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002563 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002564 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002565
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002566 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002567 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002568 }
2569
2570 if (!in_endpoint || !out_endpoint) {
2571 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2572 " bulk-out endpoint");
2573 rc = -ENODEV;
2574 goto error;
2575 }
2576
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002577 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002578 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002579 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002580 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2581
2582 if (!dev->in_frame || !dev->out_frame ||
2583 !dev->in_urb || !dev->out_urb)
2584 goto error;
2585
2586 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2587 usb_rcvbulkpipe(dev->udev, in_endpoint),
2588 NULL, 0, NULL, dev);
2589 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2590 usb_sndbulkpipe(dev->udev, out_endpoint),
2591 NULL, 0,
2592 pn533_send_complete, dev);
2593
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002594 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2595 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002596 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002597 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002598 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002599 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002600 if (dev->wq == NULL)
2601 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002603 init_timer(&dev->listen_timer);
2604 dev->listen_timer.data = (unsigned long) dev;
2605 dev->listen_timer.function = pn533_listen_mode_timer;
2606
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002607 skb_queue_head_init(&dev->resp_q);
2608
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002609 INIT_LIST_HEAD(&dev->cmd_queue);
2610
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002611 usb_set_intfdata(interface, dev);
2612
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002613 memset(&fw_ver, 0, sizeof(fw_ver));
2614 rc = pn533_get_firmware_version(dev, &fw_ver);
2615 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002616 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002617
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002618 nfc_dev_info(&dev->interface->dev,
2619 "NXP PN533 firmware ver %d.%d now attached",
2620 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002621
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002622 dev->device_type = id->driver_info;
2623 switch (dev->device_type) {
2624 case PN533_DEVICE_STD:
2625 protocols = PN533_ALL_PROTOCOLS;
2626 break;
2627
2628 case PN533_DEVICE_PASORI:
2629 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2630 break;
2631
2632 default:
2633 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2634 dev->device_type);
2635 rc = -EINVAL;
2636 goto destroy_wq;
2637 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002638
Samuel Ortize8753042011-08-19 15:47:11 +02002639 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002640 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002641 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002642 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002643 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002644 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002645
2646 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2647 nfc_set_drvdata(dev->nfc_dev, dev);
2648
2649 rc = nfc_register_device(dev->nfc_dev);
2650 if (rc)
2651 goto free_nfc_dev;
2652
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002653 rc = pn533_setup(dev);
2654 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002655 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002656
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002657 return 0;
2658
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002659unregister_nfc_dev:
2660 nfc_unregister_device(dev->nfc_dev);
2661
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662free_nfc_dev:
2663 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002664
Samuel Ortiz4849f852012-04-10 19:43:17 +02002665destroy_wq:
2666 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002667error:
2668 kfree(dev->in_frame);
2669 usb_free_urb(dev->in_urb);
2670 kfree(dev->out_frame);
2671 usb_free_urb(dev->out_urb);
2672 kfree(dev);
2673 return rc;
2674}
2675
2676static void pn533_disconnect(struct usb_interface *interface)
2677{
2678 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002679 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002680
2681 dev = usb_get_intfdata(interface);
2682 usb_set_intfdata(interface, NULL);
2683
2684 nfc_unregister_device(dev->nfc_dev);
2685 nfc_free_device(dev->nfc_dev);
2686
2687 usb_kill_urb(dev->in_urb);
2688 usb_kill_urb(dev->out_urb);
2689
Samuel Ortiz4849f852012-04-10 19:43:17 +02002690 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002691
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002692 skb_queue_purge(&dev->resp_q);
2693
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002694 del_timer(&dev->listen_timer);
2695
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002696 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2697 list_del(&cmd->queue);
2698 kfree(cmd);
2699 }
2700
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002701 kfree(dev->in_frame);
2702 usb_free_urb(dev->in_urb);
2703 kfree(dev->out_frame);
2704 usb_free_urb(dev->out_urb);
2705 kfree(dev);
2706
Dan Carpenter276556d2011-07-08 10:21:15 +03002707 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002708}
2709
2710static struct usb_driver pn533_driver = {
2711 .name = "pn533",
2712 .probe = pn533_probe,
2713 .disconnect = pn533_disconnect,
2714 .id_table = pn533_table,
2715};
2716
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002717module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002718
2719MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2720 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2721MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2722MODULE_VERSION(VERSION);
2723MODULE_LICENSE("GPL");