blob: aa7803f16773f91796d95aa13bfa3cfed69fc345 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030031#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030032
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081/* frame definitions */
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
83 254 (DATA)
84 2 (DCS, postamble) */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010085#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020088
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010089/*
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
92 */
93#define PN533_FRAME_MAX_PAYLOAD_LEN 263
94
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030095#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010096 PN533_FRAME_TAIL_LEN)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030097#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
100
101/* start of frame */
102#define PN533_SOF 0x00FF
103
104/* frame identifier: in/out/error */
105#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106#define PN533_DIR_OUT 0xD4
107#define PN533_DIR_IN 0xD5
108
109/* PN533 Commands */
110#define PN533_FRAME_CMD(f) (f->data[1])
111#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
113
114#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115#define PN533_CMD_RF_CONFIGURATION 0x32
116#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200117#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300118#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119#define PN533_CMD_IN_ATR 0x50
120#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100121#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122
Samuel Ortizad3823c2012-05-30 23:54:55 +0200123#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200124#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200125#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100126#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300128#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
129
130/* PN533 Return codes */
131#define PN533_CMD_RET_MASK 0x3F
132#define PN533_CMD_MI_MASK 0x40
133#define PN533_CMD_RET_SUCCESS 0x00
134
135struct pn533;
136
137typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
139
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100140typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143/* structs for pn533 commands */
144
145/* PN533_CMD_GET_FIRMWARE_VERSION */
146struct pn533_fw_version {
147 u8 ic;
148 u8 ver;
149 u8 rev;
150 u8 support;
151};
152
153/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200154#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200156#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200158#define PN533_CONFIG_TIMING_102 0xb
159#define PN533_CONFIG_TIMING_204 0xc
160#define PN533_CONFIG_TIMING_409 0xd
161#define PN533_CONFIG_TIMING_819 0xe
162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300163#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
165
166struct pn533_config_max_retries {
167 u8 mx_rty_atr;
168 u8 mx_rty_psl;
169 u8 mx_rty_passive_act;
170} __packed;
171
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200172struct pn533_config_timing {
173 u8 rfu;
174 u8 atr_res_timeout;
175 u8 dep_timeout;
176} __packed;
177
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300178/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
179
180/* felica commands opcode */
181#define PN533_FELICA_OPC_SENSF_REQ 0
182#define PN533_FELICA_OPC_SENSF_RES 1
183/* felica SENSF_REQ parameters */
184#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
188
189/* type B initiator_data values */
190#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
193
194union pn533_cmd_poll_initdata {
195 struct {
196 u8 afi;
197 u8 polling_method;
198 } __packed type_b;
199 struct {
200 u8 opcode;
201 __be16 sc;
202 u8 rc;
203 u8 tsn;
204 } __packed felica;
205};
206
207/* Poll modulations */
208enum {
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200214 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300215
216 __PN533_POLL_MOD_AFTER_LAST,
217};
218#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
219
220struct pn533_poll_modulations {
221 struct {
222 u8 maxtg;
223 u8 brty;
224 union pn533_cmd_poll_initdata initiator_data;
225 } __packed data;
226 u8 len;
227};
228
229const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 0,
234 },
235 .len = 2,
236 },
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
238 .data = {
239 .maxtg = 1,
240 .brty = 1,
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
245 .tsn = 0,
246 },
247 },
248 .len = 7,
249 },
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
251 .data = {
252 .maxtg = 1,
253 .brty = 2,
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
258 .tsn = 0,
259 },
260 },
261 .len = 7,
262 },
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 4,
267 },
268 .len = 2,
269 },
270 [PN533_POLL_MOD_847KBPS_B] = {
271 .data = {
272 .maxtg = 1,
273 .brty = 8,
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
276 .polling_method =
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
278 },
279 },
280 .len = 3,
281 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200282 [PN533_LISTEN_MOD] = {
283 .len = 0,
284 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300285};
286
287/* PN533_CMD_IN_ATR */
288
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300289struct pn533_cmd_activate_response {
290 u8 status;
291 u8 nfcid3t[10];
292 u8 didt;
293 u8 bst;
294 u8 brt;
295 u8 to;
296 u8 ppt;
297 /* optional */
298 u8 gt[];
299} __packed;
300
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100301struct pn533_cmd_jump_dep_response {
302 u8 status;
303 u8 tg;
304 u8 nfcid3t[10];
305 u8 didt;
306 u8 bst;
307 u8 brt;
308 u8 to;
309 u8 ppt;
310 /* optional */
311 u8 gt[];
312} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313
Samuel Ortizad3823c2012-05-30 23:54:55 +0200314
315/* PN533_TG_INIT_AS_TARGET */
316#define PN533_INIT_TARGET_PASSIVE 0x1
317#define PN533_INIT_TARGET_DEP 0x2
318
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200319#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321#define PN533_INIT_TARGET_RESP_DEP 0x4
322
Samuel Ortizad3823c2012-05-30 23:54:55 +0200323struct pn533_cmd_init_target {
324 u8 mode;
325 u8 mifare[6];
326 u8 felica[18];
327 u8 nfcid3[10];
328 u8 gb_len;
329 u8 gb[];
330} __packed;
331
332struct pn533_cmd_init_target_response {
333 u8 mode;
334 u8 cmd[];
335} __packed;
336
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300337struct pn533 {
338 struct usb_device *udev;
339 struct usb_interface *interface;
340 struct nfc_dev *nfc_dev;
341
342 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300343 struct pn533_frame *out_frame;
344
345 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300346 struct pn533_frame *in_frame;
347
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200348 struct sk_buff_head resp_q;
349
Samuel Ortiz4849f852012-04-10 19:43:17 +0200350 struct workqueue_struct *wq;
351 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200352 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200353 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200354 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200355 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200356 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200357 struct pn533_frame *wq_in_frame;
358 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200359 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300360
361 pn533_cmd_complete_t cmd_complete;
362 void *cmd_complete_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200363 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300364 u8 cmd;
365
366 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
367 u8 poll_mod_count;
368 u8 poll_mod_curr;
369 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200370 u32 listen_protocols;
371
372 u8 *gb;
373 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300374
375 u8 tgt_available_prots;
376 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200377 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200378
379 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200380
381 struct list_head cmd_queue;
382 u8 cmd_pending;
383};
384
385struct pn533_cmd {
386 struct list_head queue;
387 struct pn533_frame *out_frame;
388 struct pn533_frame *in_frame;
389 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100390 u8 cmd_code;
391 struct sk_buff *req;
392 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200393 pn533_cmd_complete_t cmd_complete;
394 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300395};
396
397struct pn533_frame {
398 u8 preamble;
399 __be16 start_frame;
400 u8 datalen;
401 u8 datalen_checksum;
402 u8 data[];
403} __packed;
404
405/* The rule: value + checksum = 0 */
406static inline u8 pn533_checksum(u8 value)
407{
408 return ~value + 1;
409}
410
411/* The rule: sum(data elements) + checksum = 0 */
412static u8 pn533_data_checksum(u8 *data, int datalen)
413{
414 u8 sum = 0;
415 int i;
416
417 for (i = 0; i < datalen; i++)
418 sum += data[i];
419
420 return pn533_checksum(sum);
421}
422
423/**
424 * pn533_tx_frame_ack - create a ack frame
425 * @frame: The frame to be set as ack
426 *
427 * Ack is different type of standard frame. As a standard frame, it has
428 * preamble and start_frame. However the checksum of this frame must fail,
429 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
430 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
431 * After datalen_checksum field, the postamble is placed.
432 */
433static void pn533_tx_frame_ack(struct pn533_frame *frame)
434{
435 frame->preamble = 0;
436 frame->start_frame = cpu_to_be16(PN533_SOF);
437 frame->datalen = 0;
438 frame->datalen_checksum = 0xFF;
439 /* data[0] is used as postamble */
440 frame->data[0] = 0;
441}
442
443static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
444{
445 frame->preamble = 0;
446 frame->start_frame = cpu_to_be16(PN533_SOF);
447 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
448 PN533_FRAME_CMD(frame) = cmd;
449 frame->datalen = 2;
450}
451
452static void pn533_tx_frame_finish(struct pn533_frame *frame)
453{
454 frame->datalen_checksum = pn533_checksum(frame->datalen);
455
456 PN533_FRAME_CHECKSUM(frame) =
457 pn533_data_checksum(frame->data, frame->datalen);
458
459 PN533_FRAME_POSTAMBLE(frame) = 0;
460}
461
462static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
463{
464 u8 checksum;
465
466 if (frame->start_frame != cpu_to_be16(PN533_SOF))
467 return false;
468
469 checksum = pn533_checksum(frame->datalen);
470 if (checksum != frame->datalen_checksum)
471 return false;
472
473 checksum = pn533_data_checksum(frame->data, frame->datalen);
474 if (checksum != PN533_FRAME_CHECKSUM(frame))
475 return false;
476
477 return true;
478}
479
480static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
481{
482 if (frame->start_frame != cpu_to_be16(PN533_SOF))
483 return false;
484
485 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
486 return false;
487
488 return true;
489}
490
491static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
492{
493 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
494}
495
Samuel Ortiz4849f852012-04-10 19:43:17 +0200496
497static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300498{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200499 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200500 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300501 int rc;
502
Samuel Ortiz4849f852012-04-10 19:43:17 +0200503 in_frame = dev->wq_in_frame;
504
505 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300506 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200507 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300508 else
509 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
510 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
511 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
512
513 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200514 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300515}
516
517static void pn533_recv_response(struct urb *urb)
518{
519 struct pn533 *dev = urb->context;
520 struct pn533_frame *in_frame;
521
Samuel Ortiz4849f852012-04-10 19:43:17 +0200522 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523
524 switch (urb->status) {
525 case 0:
526 /* success */
527 break;
528 case -ECONNRESET:
529 case -ENOENT:
530 case -ESHUTDOWN:
531 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
532 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200533 dev->wq_in_error = urb->status;
534 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300535 default:
536 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
537 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200538 dev->wq_in_error = urb->status;
539 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300540 }
541
542 in_frame = dev->in_urb->transfer_buffer;
543
544 if (!pn533_rx_frame_is_valid(in_frame)) {
545 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200546 dev->wq_in_error = -EIO;
547 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 }
549
550 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
551 nfc_dev_err(&dev->interface->dev, "The received frame is not "
552 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200553 dev->wq_in_error = -EIO;
554 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300555 }
556
557 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200558 dev->wq_in_error = 0;
559 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300560
Samuel Ortiz4849f852012-04-10 19:43:17 +0200561sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200562 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563}
564
565static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
566{
567 dev->in_urb->complete = pn533_recv_response;
568
569 return usb_submit_urb(dev->in_urb, flags);
570}
571
572static void pn533_recv_ack(struct urb *urb)
573{
574 struct pn533 *dev = urb->context;
575 struct pn533_frame *in_frame;
576 int rc;
577
578 switch (urb->status) {
579 case 0:
580 /* success */
581 break;
582 case -ECONNRESET:
583 case -ENOENT:
584 case -ESHUTDOWN:
585 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
586 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200587 dev->wq_in_error = urb->status;
588 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300589 default:
590 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
591 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200592 dev->wq_in_error = urb->status;
593 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300594 }
595
596 in_frame = dev->in_urb->transfer_buffer;
597
598 if (!pn533_rx_frame_is_ack(in_frame)) {
599 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200600 dev->wq_in_error = -EIO;
601 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 }
603
604 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
605
606 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
607 if (rc) {
608 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
609 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200610 dev->wq_in_error = rc;
611 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300612 }
613
614 return;
615
Samuel Ortiz4849f852012-04-10 19:43:17 +0200616sched_wq:
617 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200618 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300619}
620
621static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
622{
623 dev->in_urb->complete = pn533_recv_ack;
624
625 return usb_submit_urb(dev->in_urb, flags);
626}
627
628static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
629{
630 int rc;
631
632 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
633
634 pn533_tx_frame_ack(dev->out_frame);
635
636 dev->out_urb->transfer_buffer = dev->out_frame;
637 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
638 rc = usb_submit_urb(dev->out_urb, flags);
639
640 return rc;
641}
642
643static int __pn533_send_cmd_frame_async(struct pn533 *dev,
644 struct pn533_frame *out_frame,
645 struct pn533_frame *in_frame,
646 int in_frame_len,
647 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100648 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300649{
650 int rc;
651
652 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
653 PN533_FRAME_CMD(out_frame));
654
655 dev->cmd = PN533_FRAME_CMD(out_frame);
656 dev->cmd_complete = cmd_complete;
657 dev->cmd_complete_arg = arg;
658
659 dev->out_urb->transfer_buffer = out_frame;
660 dev->out_urb->transfer_buffer_length =
661 PN533_FRAME_SIZE(out_frame);
662
663 dev->in_urb->transfer_buffer = in_frame;
664 dev->in_urb->transfer_buffer_length = in_frame_len;
665
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100666 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300667 if (rc)
668 return rc;
669
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100670 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300671 if (rc)
672 goto error;
673
674 return 0;
675
676error:
677 usb_unlink_urb(dev->out_urb);
678 return rc;
679}
680
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
682{
683 struct pn533_frame *frame;
684 /* payload is already there, just update datalen */
685 int payload_len = skb->len;
686
687 skb_push(skb, PN533_FRAME_HEADER_LEN);
688 skb_put(skb, PN533_FRAME_TAIL_LEN);
689
690 frame = (struct pn533_frame *)skb->data;
691
692 pn533_tx_frame_init(frame, cmd_code);
693 frame->datalen += payload_len;
694 pn533_tx_frame_finish(frame);
695}
696
697struct pn533_send_async_complete_arg {
698 pn533_send_async_complete_t complete_cb;
699 void *complete_cb_context;
700 struct sk_buff *resp;
701 struct sk_buff *req;
702};
703
704static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
705 int params_len)
706{
707 struct pn533_send_async_complete_arg *arg = _arg;
708
709 struct sk_buff *req = arg->req;
710 struct sk_buff *resp = arg->resp;
711
712 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
713 int rc;
714
715 dev_kfree_skb(req);
716
717 if (params_len < 0) {
718 nfc_dev_err(&dev->interface->dev,
719 "Error %d when starting as a target",
720 params_len);
721
722 arg->complete_cb(dev, arg->complete_cb_context,
723 ERR_PTR(params_len));
724 rc = params_len;
725 dev_kfree_skb(resp);
726 goto out;
727 }
728
729 skb_put(resp, PN533_FRAME_SIZE(frame));
730 skb_pull(resp, PN533_FRAME_HEADER_LEN);
731 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
732
733 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
734
735out:
736 kfree(arg);
737 return rc;
738}
739
740static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
741 struct sk_buff *req, struct sk_buff *resp,
742 int resp_len,
743 pn533_send_async_complete_t complete_cb,
744 void *complete_cb_context)
745{
746 struct pn533_cmd *cmd;
747 struct pn533_send_async_complete_arg *arg;
748 int rc = 0;
749
750 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
751
752 arg = kzalloc(sizeof(arg), GFP_KERNEL);
753 if (!arg)
754 return -ENOMEM;
755
756 arg->complete_cb = complete_cb;
757 arg->complete_cb_context = complete_cb_context;
758 arg->resp = resp;
759 arg->req = req;
760
761 pn533_build_cmd_frame(cmd_code, req);
762
763 mutex_lock(&dev->cmd_lock);
764
765 if (!dev->cmd_pending) {
766 rc = __pn533_send_cmd_frame_async(dev,
767 (struct pn533_frame *)req->data,
768 (struct pn533_frame *)resp->data,
769 resp_len, pn533_send_async_complete,
770 arg);
771 if (rc)
772 goto error;
773
774 dev->cmd_pending = 1;
775 goto unlock;
776 }
777
778 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
779
780 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
781 if (!cmd) {
782 rc = -ENOMEM;
783 goto error;
784 }
785
786 INIT_LIST_HEAD(&cmd->queue);
787 cmd->cmd_code = cmd_code;
788 cmd->req = req;
789 cmd->resp = resp;
790 cmd->arg = arg;
791
792 list_add_tail(&cmd->queue, &dev->cmd_queue);
793
794 goto unlock;
795
796error:
797 kfree(arg);
798unlock:
799 mutex_unlock(&dev->cmd_lock);
800 return rc;
801}
802
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100803static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
804 struct sk_buff *req,
805 pn533_send_async_complete_t complete_cb,
806 void *complete_cb_context)
807{
808 struct sk_buff *resp;
809 int rc;
810 int resp_len = PN533_FRAME_HEADER_LEN +
811 PN533_FRAME_MAX_PAYLOAD_LEN +
812 PN533_FRAME_TAIL_LEN;
813
814 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
815
816 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
817 if (!resp)
818 return -ENOMEM;
819
820 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
821 complete_cb_context);
822 if (rc)
823 dev_kfree_skb(resp);
824
825 return rc;
826}
827
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100828static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
829 struct sk_buff *req,
830 pn533_send_async_complete_t complete_cb,
831 void *complete_cb_context)
832{
833 struct sk_buff *resp;
834 int rc;
835
836 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
837
838 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
839 if (!resp)
840 return -ENOMEM;
841
842 rc = __pn533_send_async(dev, cmd_code, req, resp,
843 PN533_NORMAL_FRAME_MAX_LEN,
844 complete_cb, complete_cb_context);
845 if (rc)
846 dev_kfree_skb(resp);
847
848 return rc;
849}
850
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200851static void pn533_wq_cmd(struct work_struct *work)
852{
853 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
854 struct pn533_cmd *cmd;
855
856 mutex_lock(&dev->cmd_lock);
857
858 if (list_empty(&dev->cmd_queue)) {
859 dev->cmd_pending = 0;
860 mutex_unlock(&dev->cmd_lock);
861 return;
862 }
863
864 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
865
Szymon Janc60ad07a2012-10-25 17:29:45 +0200866 list_del(&cmd->queue);
867
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200868 mutex_unlock(&dev->cmd_lock);
869
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100870 if (cmd->cmd_code != PN533_CMD_UNDEF)
871 __pn533_send_cmd_frame_async(dev,
872 (struct pn533_frame *)cmd->req->data,
873 (struct pn533_frame *)cmd->resp->data,
874 PN533_NORMAL_FRAME_MAX_LEN,
875 pn533_send_async_complete,
876 cmd->arg);
877 else
878 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
879 cmd->in_frame_len,
880 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200881
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200882 kfree(cmd);
883}
884
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300885static int pn533_send_cmd_frame_async(struct pn533 *dev,
886 struct pn533_frame *out_frame,
887 struct pn533_frame *in_frame,
888 int in_frame_len,
889 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100890 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300891{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200892 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200893 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300894
895 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
896
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200897 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300898
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200899 if (!dev->cmd_pending) {
900 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
901 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100902 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200903 if (!rc)
904 dev->cmd_pending = 1;
905
Szymon Jancee5e8d82012-09-27 09:16:54 +0200906 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200907 }
908
909 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
910
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100911 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200912 if (!cmd) {
913 rc = -ENOMEM;
914 goto unlock;
915 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200916
917 INIT_LIST_HEAD(&cmd->queue);
918 cmd->out_frame = out_frame;
919 cmd->in_frame = in_frame;
920 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100921 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200922 cmd->cmd_complete = cmd_complete;
923 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200924
925 list_add_tail(&cmd->queue, &dev->cmd_queue);
926
Szymon Jancee5e8d82012-09-27 09:16:54 +0200927unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200928 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300929
Szymon Jancee5e8d82012-09-27 09:16:54 +0200930 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300931}
932
933struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100934 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300935 struct completion done;
936};
937
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100938static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
939 struct sk_buff *resp)
940{
941 struct pn533_sync_cmd_response *arg = _arg;
942
943 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
944
945 arg->resp = resp;
946 complete(&arg->done);
947
948 return 0;
949}
950
951/* pn533_send_cmd_sync
952 *
953 * Please note the req parameter is freed inside the function to
954 * limit a number of return value interpretations by the caller.
955 *
956 * 1. negative in case of error during TX path -> req should be freed
957 *
958 * 2. negative in case of error during RX path -> req should not be freed
959 * as it's been already freed at the begining of RX path by
960 * async_complete_cb.
961 *
962 * 3. valid pointer in case of succesfult RX path
963 *
964 * A caller has to check a return value with IS_ERR macro. If the test pass,
965 * the returned pointer is valid.
966 *
967 * */
968static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
969 struct sk_buff *req)
970{
971 int rc;
972 struct pn533_sync_cmd_response arg;
973
974 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
975
976 init_completion(&arg.done);
977
978 rc = pn533_send_cmd_async(dev, cmd_code, req,
979 pn533_send_sync_complete, &arg);
980 if (rc) {
981 dev_kfree_skb(req);
982 return ERR_PTR(rc);
983 }
984
985 wait_for_completion(&arg.done);
986
987 return arg.resp;
988}
989
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300990static void pn533_send_complete(struct urb *urb)
991{
992 struct pn533 *dev = urb->context;
993
994 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
995
996 switch (urb->status) {
997 case 0:
998 /* success */
999 break;
1000 case -ECONNRESET:
1001 case -ENOENT:
1002 case -ESHUTDOWN:
1003 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1004 " status: %d", urb->status);
1005 break;
1006 default:
1007 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1008 " %d", urb->status);
1009 }
1010}
1011
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001012static struct sk_buff *pn533_alloc_skb(unsigned int size)
1013{
1014 struct sk_buff *skb;
1015
1016 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1017 size +
1018 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1019
1020 if (skb)
1021 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1022
1023 return skb;
1024}
1025
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001026struct pn533_target_type_a {
1027 __be16 sens_res;
1028 u8 sel_res;
1029 u8 nfcid_len;
1030 u8 nfcid_data[];
1031} __packed;
1032
1033
1034#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1035#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1036#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1037
1038#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1039#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1040
1041#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1042#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1043
1044#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1045#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1046#define PN533_TYPE_A_SEL_PROT_DEP 2
1047#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1048
1049static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1050 int target_data_len)
1051{
1052 u8 ssd;
1053 u8 platconf;
1054
1055 if (target_data_len < sizeof(struct pn533_target_type_a))
1056 return false;
1057
1058 /* The lenght check of nfcid[] and ats[] are not being performed because
1059 the values are not being used */
1060
1061 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1062 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1063 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1064
1065 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1066 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1067 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1068 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1069 return false;
1070
1071 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1072 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1073 return false;
1074
1075 return true;
1076}
1077
1078static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1079 int tgt_data_len)
1080{
1081 struct pn533_target_type_a *tgt_type_a;
1082
1083 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1084
1085 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1086 return -EPROTO;
1087
1088 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1089 case PN533_TYPE_A_SEL_PROT_MIFARE:
1090 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1091 break;
1092 case PN533_TYPE_A_SEL_PROT_ISO14443:
1093 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1094 break;
1095 case PN533_TYPE_A_SEL_PROT_DEP:
1096 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1097 break;
1098 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1099 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1100 NFC_PROTO_NFC_DEP_MASK;
1101 break;
1102 }
1103
1104 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1105 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001106 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1107 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001108
1109 return 0;
1110}
1111
1112struct pn533_target_felica {
1113 u8 pol_res;
1114 u8 opcode;
1115 u8 nfcid2[8];
1116 u8 pad[8];
1117 /* optional */
1118 u8 syst_code[];
1119} __packed;
1120
1121#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1122#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1123
1124static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1125 int target_data_len)
1126{
1127 if (target_data_len < sizeof(struct pn533_target_felica))
1128 return false;
1129
1130 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1131 return false;
1132
1133 return true;
1134}
1135
1136static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1137 int tgt_data_len)
1138{
1139 struct pn533_target_felica *tgt_felica;
1140
1141 tgt_felica = (struct pn533_target_felica *) tgt_data;
1142
1143 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1144 return -EPROTO;
1145
1146 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1147 tgt_felica->nfcid2[1] ==
1148 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1149 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1150 else
1151 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1152
Samuel Ortiz79757542012-03-05 01:03:45 +01001153 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1154 nfc_tgt->sensf_res_len = 9;
1155
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001156 return 0;
1157}
1158
1159struct pn533_target_jewel {
1160 __be16 sens_res;
1161 u8 jewelid[4];
1162} __packed;
1163
1164static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1165 int target_data_len)
1166{
1167 u8 ssd;
1168 u8 platconf;
1169
1170 if (target_data_len < sizeof(struct pn533_target_jewel))
1171 return false;
1172
1173 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1174 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1175 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1176
1177 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1178 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1179 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1180 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1181 return false;
1182
1183 return true;
1184}
1185
1186static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1187 int tgt_data_len)
1188{
1189 struct pn533_target_jewel *tgt_jewel;
1190
1191 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1192
1193 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1194 return -EPROTO;
1195
1196 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1197 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001198 nfc_tgt->nfcid1_len = 4;
1199 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001200
1201 return 0;
1202}
1203
1204struct pn533_type_b_prot_info {
1205 u8 bitrate;
1206 u8 fsci_type;
1207 u8 fwi_adc_fo;
1208} __packed;
1209
1210#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1211#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1212#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1213
1214struct pn533_type_b_sens_res {
1215 u8 opcode;
1216 u8 nfcid[4];
1217 u8 appdata[4];
1218 struct pn533_type_b_prot_info prot_info;
1219} __packed;
1220
1221#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1222
1223struct pn533_target_type_b {
1224 struct pn533_type_b_sens_res sensb_res;
1225 u8 attrib_res_len;
1226 u8 attrib_res[];
1227} __packed;
1228
1229static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1230 int target_data_len)
1231{
1232 if (target_data_len < sizeof(struct pn533_target_type_b))
1233 return false;
1234
1235 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1236 return false;
1237
1238 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1239 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1240 return false;
1241
1242 return true;
1243}
1244
1245static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1246 int tgt_data_len)
1247{
1248 struct pn533_target_type_b *tgt_type_b;
1249
1250 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1251
1252 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1253 return -EPROTO;
1254
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001255 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001256
1257 return 0;
1258}
1259
1260struct pn533_poll_response {
1261 u8 nbtg;
1262 u8 tg;
1263 u8 target_data[];
1264} __packed;
1265
1266static int pn533_target_found(struct pn533 *dev,
1267 struct pn533_poll_response *resp, int resp_len)
1268{
1269 int target_data_len;
1270 struct nfc_target nfc_tgt;
1271 int rc;
1272
1273 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1274 dev->poll_mod_curr);
1275
1276 if (resp->tg != 1)
1277 return -EPROTO;
1278
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001279 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1280
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001281 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1282
1283 switch (dev->poll_mod_curr) {
1284 case PN533_POLL_MOD_106KBPS_A:
1285 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1286 target_data_len);
1287 break;
1288 case PN533_POLL_MOD_212KBPS_FELICA:
1289 case PN533_POLL_MOD_424KBPS_FELICA:
1290 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1291 target_data_len);
1292 break;
1293 case PN533_POLL_MOD_106KBPS_JEWEL:
1294 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1295 target_data_len);
1296 break;
1297 case PN533_POLL_MOD_847KBPS_B:
1298 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1299 target_data_len);
1300 break;
1301 default:
1302 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1303 " modulation");
1304 return -EPROTO;
1305 }
1306
1307 if (rc)
1308 return rc;
1309
1310 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1311 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1312 " have the desired protocol");
1313 return -EAGAIN;
1314 }
1315
1316 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1317 "0x%x", nfc_tgt.supported_protocols);
1318
1319 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1320
1321 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1322
1323 return 0;
1324}
1325
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001326static inline void pn533_poll_next_mod(struct pn533 *dev)
1327{
1328 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1329}
1330
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331static void pn533_poll_reset_mod_list(struct pn533 *dev)
1332{
1333 dev->poll_mod_count = 0;
1334}
1335
1336static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1337{
1338 dev->poll_mod_active[dev->poll_mod_count] =
1339 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1340 dev->poll_mod_count++;
1341}
1342
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001343static void pn533_poll_create_mod_list(struct pn533 *dev,
1344 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001345{
1346 pn533_poll_reset_mod_list(dev);
1347
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001348 if (im_protocols & NFC_PROTO_MIFARE_MASK
1349 || im_protocols & NFC_PROTO_ISO14443_MASK
1350 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001351 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1352
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001353 if (im_protocols & NFC_PROTO_FELICA_MASK
1354 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001355 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1356 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1357 }
1358
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001359 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001360 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1361
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001362 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001363 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001364
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001365 if (tm_protocols)
1366 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001367}
1368
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001369static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001370{
1371 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001372 int rc;
1373
1374 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1375
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001376 resp = (struct pn533_poll_response *) params;
1377 if (resp->nbtg) {
1378 rc = pn533_target_found(dev, resp, params_len);
1379
1380 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001381 if (rc == 0) {
1382 pn533_poll_reset_mod_list(dev);
1383 return 0;
1384 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001385 }
1386
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001387 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001388}
1389
Samuel Ortizad3823c2012-05-30 23:54:55 +02001390static int pn533_init_target_frame(struct pn533_frame *frame,
1391 u8 *gb, size_t gb_len)
1392{
1393 struct pn533_cmd_init_target *cmd;
1394 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001395 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1396 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1397 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1398 0xff, 0xff}; /* System code */
1399 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1400 0x0, 0x0, 0x0,
1401 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001402
1403 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1404 cmd = kzalloc(cmd_len, GFP_KERNEL);
1405 if (cmd == NULL)
1406 return -ENOMEM;
1407
1408 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1409
1410 /* DEP support only */
1411 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001412
1413 /* Felica params */
1414 memcpy(cmd->felica, felica_params, 18);
1415 get_random_bytes(cmd->felica + 2, 6);
1416
1417 /* NFCID3 */
1418 memset(cmd->nfcid3, 0, 10);
1419 memcpy(cmd->nfcid3, cmd->felica, 8);
1420
1421 /* MIFARE params */
1422 memcpy(cmd->mifare, mifare_params, 6);
1423
1424 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001425 cmd->gb_len = gb_len;
1426 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001427
Samuel Ortizad3823c2012-05-30 23:54:55 +02001428 /* Len Tk */
1429 cmd->gb[gb_len] = 0;
1430
1431 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001432
Samuel Ortizad3823c2012-05-30 23:54:55 +02001433 frame->datalen += cmd_len;
1434
1435 pn533_tx_frame_finish(frame);
1436
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001437 kfree(cmd);
1438
Samuel Ortizad3823c2012-05-30 23:54:55 +02001439 return 0;
1440}
1441
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001442#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001443#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1444static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001445 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001446{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001447 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001448
1449 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1450
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001451 if (IS_ERR(resp))
1452 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001453
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001454 status = resp->data[0];
1455 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001456
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001457 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001458 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001459 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001460 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001461 return 0;
1462 }
1463
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001464 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001465}
1466
1467static void pn533_wq_tg_get_data(struct work_struct *work)
1468{
1469 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001470
1471 struct sk_buff *skb;
1472 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001473
1474 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1475
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001476 skb = pn533_alloc_skb(0);
1477 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001478 return;
1479
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001480 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1481 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001482
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001483 if (rc < 0)
1484 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001485
1486 return;
1487}
1488
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001489#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001490static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001491{
1492 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001493 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1494 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001495 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001496
1497 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1498
1499 if (params_len < 0) {
1500 nfc_dev_err(&dev->interface->dev,
1501 "Error %d when starting as a target",
1502 params_len);
1503
1504 return params_len;
1505 }
1506
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001507 if (params_len < ATR_REQ_GB_OFFSET + 1)
1508 return -EINVAL;
1509
Samuel Ortizad3823c2012-05-30 23:54:55 +02001510 resp = (struct pn533_cmd_init_target_response *) params;
1511
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001512 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1513 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001514
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001515 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1516 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1517 comm_mode = NFC_COMM_ACTIVE;
1518
1519 /* Again, only DEP */
1520 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1521 return -EOPNOTSUPP;
1522
1523 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1524 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1525
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001526 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1527 comm_mode, gb, gb_len);
1528 if (rc < 0) {
1529 nfc_dev_err(&dev->interface->dev,
1530 "Error when signaling target activation");
1531 return rc;
1532 }
1533
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001534 dev->tgt_mode = 1;
1535
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001536 queue_work(dev->wq, &dev->tg_work);
1537
1538 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001539}
1540
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001541static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001542{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001543 struct pn533 *dev = (struct pn533 *) data;
1544
1545 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1546
1547 /* An ack will cancel the last issued command (poll) */
1548 pn533_send_ack(dev, GFP_ATOMIC);
1549
1550 dev->cancel_listen = 1;
1551
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552 pn533_poll_next_mod(dev);
1553
1554 queue_work(dev->wq, &dev->poll_work);
1555}
1556
1557static int pn533_poll_complete(struct pn533 *dev, void *arg,
1558 u8 *params, int params_len)
1559{
1560 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001561 int rc;
1562
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001563 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1564
1565 if (params_len == -ENOENT) {
1566 if (dev->poll_mod_count != 0)
1567 return 0;
1568
1569 nfc_dev_err(&dev->interface->dev,
1570 "Polling operation has been stopped");
1571
1572 goto stop_poll;
1573 }
1574
1575 if (params_len < 0) {
1576 nfc_dev_err(&dev->interface->dev,
1577 "Error %d when running poll", params_len);
1578
1579 goto stop_poll;
1580 }
1581
1582 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1583
1584 if (cur_mod->len == 0) {
1585 del_timer(&dev->listen_timer);
1586
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001587 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001588 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001589 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001590 if (!rc)
1591 return rc;
1592 }
1593
1594 pn533_poll_next_mod(dev);
1595
1596 queue_work(dev->wq, &dev->poll_work);
1597
1598 return 0;
1599
1600stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001601 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001602 dev->poll_protocols = 0;
1603 return 0;
1604}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001605
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001606static void pn533_build_poll_frame(struct pn533 *dev,
1607 struct pn533_frame *frame,
1608 struct pn533_poll_modulations *mod)
1609{
1610 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001611
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001612 if (mod->len == 0) {
1613 /* Listen mode */
1614 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1615 } else {
1616 /* Polling mode */
1617 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1618
1619 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1620 frame->datalen += mod->len;
1621
1622 pn533_tx_frame_finish(frame);
1623 }
1624}
1625
1626static int pn533_send_poll_frame(struct pn533 *dev)
1627{
1628 struct pn533_poll_modulations *cur_mod;
1629 int rc;
1630
1631 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1632
1633 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001634
1635 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001636 PN533_NORMAL_FRAME_MAX_LEN,
1637 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001638 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001639 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001640 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001641
1642 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001643}
1644
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001645static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001646{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001647 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1648 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001649 int rc;
1650
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001651 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1652
1653 nfc_dev_dbg(&dev->interface->dev,
1654 "%s cancel_listen %d modulation len %d",
1655 __func__, dev->cancel_listen, cur_mod->len);
1656
1657 if (dev->cancel_listen == 1) {
1658 dev->cancel_listen = 0;
1659 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001660 }
1661
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001662 rc = pn533_send_poll_frame(dev);
1663 if (rc)
1664 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001665
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001666 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1667 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001668
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001669 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001670}
1671
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001672static int pn533_start_poll(struct nfc_dev *nfc_dev,
1673 u32 im_protocols, u32 tm_protocols)
1674{
1675 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1676
1677 nfc_dev_dbg(&dev->interface->dev,
1678 "%s: im protocols 0x%x tm protocols 0x%x",
1679 __func__, im_protocols, tm_protocols);
1680
1681 if (dev->tgt_active_prot) {
1682 nfc_dev_err(&dev->interface->dev,
1683 "Cannot poll with a target already activated");
1684 return -EBUSY;
1685 }
1686
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001687 if (dev->tgt_mode) {
1688 nfc_dev_err(&dev->interface->dev,
1689 "Cannot poll while already being activated");
1690 return -EBUSY;
1691 }
1692
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001693 if (tm_protocols) {
1694 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1695 if (dev->gb == NULL)
1696 tm_protocols = 0;
1697 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001698
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001699 dev->poll_mod_curr = 0;
1700 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1701 dev->poll_protocols = im_protocols;
1702 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001703
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001704 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001705}
1706
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1708{
1709 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1710
1711 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1712
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001713 del_timer(&dev->listen_timer);
1714
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001715 if (!dev->poll_mod_count) {
1716 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1717 " running");
1718 return;
1719 }
1720
1721 /* An ack will cancel the last issued command (poll) */
1722 pn533_send_ack(dev, GFP_KERNEL);
1723
1724 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1725 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001726
1727 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728}
1729
1730static int pn533_activate_target_nfcdep(struct pn533 *dev)
1731{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001732 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001733 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734 int rc;
1735
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001736 struct sk_buff *skb;
1737 struct sk_buff *resp;
1738
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001739 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1740
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001741 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1742 if (!skb)
1743 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001744
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001745 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1746 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001748 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1749 if (IS_ERR(resp))
1750 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001752 rsp = (struct pn533_cmd_activate_response *) resp->data;
1753 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001754 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001755 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001756 return -EIO;
1757
Samuel Ortiz541d9202011-12-14 16:43:10 +01001758 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001759 gt_len = resp->len - 16;
1760 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001761
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001762 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001763 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001764}
1765
Eric Lapuyade90099432012-05-07 12:31:13 +02001766static int pn533_activate_target(struct nfc_dev *nfc_dev,
1767 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001768{
1769 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1770 int rc;
1771
1772 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1773 protocol);
1774
1775 if (dev->poll_mod_count) {
1776 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1777 " polling");
1778 return -EBUSY;
1779 }
1780
1781 if (dev->tgt_active_prot) {
1782 nfc_dev_err(&dev->interface->dev, "There is already an active"
1783 " target");
1784 return -EBUSY;
1785 }
1786
1787 if (!dev->tgt_available_prots) {
1788 nfc_dev_err(&dev->interface->dev, "There is no available target"
1789 " to activate");
1790 return -EINVAL;
1791 }
1792
1793 if (!(dev->tgt_available_prots & (1 << protocol))) {
1794 nfc_dev_err(&dev->interface->dev, "The target does not support"
1795 " the requested protocol %u", protocol);
1796 return -EINVAL;
1797 }
1798
1799 if (protocol == NFC_PROTO_NFC_DEP) {
1800 rc = pn533_activate_target_nfcdep(dev);
1801 if (rc) {
1802 nfc_dev_err(&dev->interface->dev, "Error %d when"
1803 " activating target with"
1804 " NFC_DEP protocol", rc);
1805 return rc;
1806 }
1807 }
1808
1809 dev->tgt_active_prot = protocol;
1810 dev->tgt_available_prots = 0;
1811
1812 return 0;
1813}
1814
Eric Lapuyade90099432012-05-07 12:31:13 +02001815static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1816 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001817{
1818 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001819
1820 struct sk_buff *skb;
1821 struct sk_buff *resp;
1822
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823 int rc;
1824
1825 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1826
1827 if (!dev->tgt_active_prot) {
1828 nfc_dev_err(&dev->interface->dev, "There is no active target");
1829 return;
1830 }
1831
1832 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001833 skb_queue_purge(&dev->resp_q);
1834
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001835 skb = pn533_alloc_skb(sizeof(u8));
1836 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001837 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001838
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001839 *skb_put(skb, 1) = 1; /* TG*/
1840
1841 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1842 if (IS_ERR(resp))
1843 return;
1844
1845 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001846 if (rc != PN533_CMD_RET_SUCCESS)
1847 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1848 " the target", rc);
1849
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001850 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001851 return;
1852}
1853
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001854
1855static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001856 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001857{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001858 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001859 u8 target_gt_len;
1860 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001861 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001862
1863 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001864
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001865 if (IS_ERR(resp))
1866 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867
1868 if (dev->tgt_available_prots &&
1869 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1870 nfc_dev_err(&dev->interface->dev,
1871 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001872 rc = -EINVAL;
1873 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001874 }
1875
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001876 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1877
1878 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879 if (rc != PN533_CMD_RET_SUCCESS) {
1880 nfc_dev_err(&dev->interface->dev,
1881 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001882 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001883 }
1884
1885 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001886 struct nfc_target nfc_target;
1887
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001888 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1889
1890 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001891 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001892 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001893 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1894 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001895 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001896
1897 dev->tgt_available_prots = 0;
1898 }
1899
1900 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1901
1902 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001903 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001904 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001905 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906 if (rc == 0)
1907 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001908 dev->nfc_dev->targets[0].idx,
1909 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001910
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001911error:
1912 dev_kfree_skb(resp);
1913 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001914}
1915
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001916static int pn533_mod_to_baud(struct pn533 *dev)
1917{
1918 switch (dev->poll_mod_curr) {
1919 case PN533_POLL_MOD_106KBPS_A:
1920 return 0;
1921 case PN533_POLL_MOD_212KBPS_FELICA:
1922 return 1;
1923 case PN533_POLL_MOD_424KBPS_FELICA:
1924 return 2;
1925 default:
1926 return -EINVAL;
1927 }
1928}
1929
Samuel Ortizd7f33452012-05-29 21:45:21 +02001930#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001931static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001932 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001933{
1934 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001935 struct sk_buff *skb;
1936 int rc, baud, skb_len;
1937 u8 *next, *arg;
1938
Samuel Ortizd7f33452012-05-29 21:45:21 +02001939 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001940
1941 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1942
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001943 if (dev->poll_mod_count) {
1944 nfc_dev_err(&dev->interface->dev,
1945 "Cannot bring the DEP link up while polling");
1946 return -EBUSY;
1947 }
1948
1949 if (dev->tgt_active_prot) {
1950 nfc_dev_err(&dev->interface->dev,
1951 "There is already an active target");
1952 return -EBUSY;
1953 }
1954
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001955 baud = pn533_mod_to_baud(dev);
1956 if (baud < 0) {
1957 nfc_dev_err(&dev->interface->dev,
1958 "Invalid curr modulation %d", dev->poll_mod_curr);
1959 return baud;
1960 }
1961
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001962 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001963 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001964 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001965
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 skb = pn533_alloc_skb(skb_len);
1967 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001968 return -ENOMEM;
1969
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001970 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1971 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001972
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001973 next = skb_put(skb, 1); /* Next */
1974 *next = 0;
1975
1976 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1977 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1978 PASSIVE_DATA_LEN);
1979 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001980 }
1981
Samuel Ortiz47807d32012-03-05 01:03:50 +01001982 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001983 memcpy(skb_put(skb, gb_len), gb, gb_len);
1984 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001985 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001986 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001987 }
1988
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001989 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1990 if (!arg) {
1991 dev_kfree_skb(skb);
1992 return -ENOMEM;
1993 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001994
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001995 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001996
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001997 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1998 pn533_in_dep_link_up_complete, arg);
1999
2000 if (rc < 0) {
2001 dev_kfree_skb(skb);
2002 kfree(arg);
2003 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002004
2005 return rc;
2006}
2007
2008static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2009{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002010 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2011
2012 pn533_poll_reset_mod_list(dev);
2013
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002014 if (dev->tgt_mode || dev->tgt_active_prot) {
2015 pn533_send_ack(dev, GFP_KERNEL);
2016 usb_kill_urb(dev->in_urb);
2017 }
2018
2019 dev->tgt_active_prot = 0;
2020 dev->tgt_mode = 0;
2021
2022 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002023
2024 return 0;
2025}
2026
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002027static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2028 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002029{
2030 int payload_len = skb->len;
2031 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002032 u8 tg;
2033
2034 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2035 payload_len);
2036
2037 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2038 /* TODO: Implement support to multi-part data exchange */
2039 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2040 " max allowed: %d",
2041 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2042 return -ENOSYS;
2043 }
2044
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002045 skb_push(skb, PN533_FRAME_HEADER_LEN);
2046
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002047 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002048 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002049 case PN533_DEVICE_PASORI:
2050 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002051 out_frame = (struct pn533_frame *) skb->data;
2052 pn533_tx_frame_init(out_frame,
2053 PN533_CMD_IN_COMM_THRU);
2054
2055 break;
2056 }
2057
2058 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002059 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2060 out_frame = (struct pn533_frame *) skb->data;
2061 pn533_tx_frame_init(out_frame,
2062 PN533_CMD_IN_DATA_EXCHANGE);
2063 tg = 1;
2064 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2065 &tg, sizeof(u8));
2066 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002067
2068 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002069 }
2070
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002071 } else {
2072 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2073 out_frame = (struct pn533_frame *) skb->data;
2074 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2075 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002076
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002077
2078 /* The data is already in the out_frame, just update the datalen */
2079 out_frame->datalen += payload_len;
2080
2081 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002082 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002083
2084 return 0;
2085}
2086
2087struct pn533_data_exchange_arg {
2088 struct sk_buff *skb_resp;
2089 struct sk_buff *skb_out;
2090 data_exchange_cb_t cb;
2091 void *cb_context;
2092};
2093
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002094static struct sk_buff *pn533_build_response(struct pn533 *dev)
2095{
2096 struct sk_buff *skb, *tmp, *t;
2097 unsigned int skb_len = 0, tmp_len = 0;
2098
2099 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2100
2101 if (skb_queue_empty(&dev->resp_q))
2102 return NULL;
2103
2104 if (skb_queue_len(&dev->resp_q) == 1) {
2105 skb = skb_dequeue(&dev->resp_q);
2106 goto out;
2107 }
2108
2109 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2110 skb_len += tmp->len;
2111
2112 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2113 __func__, skb_len);
2114
2115 skb = alloc_skb(skb_len, GFP_KERNEL);
2116 if (skb == NULL)
2117 goto out;
2118
2119 skb_put(skb, skb_len);
2120
2121 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2122 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2123 tmp_len += tmp->len;
2124 }
2125
2126out:
2127 skb_queue_purge(&dev->resp_q);
2128
2129 return skb;
2130}
2131
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002132static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2133 u8 *params, int params_len)
2134{
2135 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002136 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002137 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2138 int err = 0;
2139 u8 status;
2140 u8 cmd_ret;
2141
2142 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2143
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002144 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002145
2146 if (params_len < 0) { /* error */
2147 err = params_len;
2148 goto error;
2149 }
2150
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151 status = params[0];
2152
2153 cmd_ret = status & PN533_CMD_RET_MASK;
2154 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2155 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2156 " exchanging data", cmd_ret);
2157 err = -EIO;
2158 goto error;
2159 }
2160
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002161 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002162 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002163 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002164 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002165 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002166
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002167 if (status & PN533_CMD_MI_MASK) {
2168 queue_work(dev->wq, &dev->mi_work);
2169 return -EINPROGRESS;
2170 }
2171
2172 skb = pn533_build_response(dev);
2173 if (skb == NULL)
2174 goto error;
2175
2176 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002177 kfree(arg);
2178 return 0;
2179
2180error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002181 skb_queue_purge(&dev->resp_q);
2182 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002183 arg->cb(arg->cb_context, NULL, err);
2184 kfree(arg);
2185 return 0;
2186}
2187
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002188static int pn533_transceive(struct nfc_dev *nfc_dev,
2189 struct nfc_target *target, struct sk_buff *skb,
2190 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002191{
2192 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2193 struct pn533_frame *out_frame, *in_frame;
2194 struct pn533_data_exchange_arg *arg;
2195 struct sk_buff *skb_resp;
2196 int skb_resp_len;
2197 int rc;
2198
2199 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2200
2201 if (!dev->tgt_active_prot) {
2202 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2203 " there is no active target");
2204 rc = -EINVAL;
2205 goto error;
2206 }
2207
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002208 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002209 if (rc)
2210 goto error;
2211
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002212 skb_resp_len = PN533_FRAME_HEADER_LEN +
2213 PN533_CMD_DATAEXCH_HEAD_LEN +
2214 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2215 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002216
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002217 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002218 if (!skb_resp) {
2219 rc = -ENOMEM;
2220 goto error;
2221 }
2222
2223 in_frame = (struct pn533_frame *) skb_resp->data;
2224 out_frame = (struct pn533_frame *) skb->data;
2225
2226 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2227 if (!arg) {
2228 rc = -ENOMEM;
2229 goto free_skb_resp;
2230 }
2231
2232 arg->skb_resp = skb_resp;
2233 arg->skb_out = skb;
2234 arg->cb = cb;
2235 arg->cb_context = cb_context;
2236
2237 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002238 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002239 if (rc) {
2240 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2241 " perform data_exchange", rc);
2242 goto free_arg;
2243 }
2244
2245 return 0;
2246
2247free_arg:
2248 kfree(arg);
2249free_skb_resp:
2250 kfree_skb(skb_resp);
2251error:
2252 kfree_skb(skb);
2253 return rc;
2254}
2255
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002256static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002257 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002258{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002259 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002260
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002261 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2262
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002263 if (IS_ERR(resp))
2264 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002265
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002266 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002267
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002268 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002269
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002270 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002271 nfc_tm_deactivated(dev->nfc_dev);
2272
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002273 dev->tgt_mode = 0;
2274
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002275 return 0;
2276 }
2277
2278 queue_work(dev->wq, &dev->tg_work);
2279
2280 return 0;
2281}
2282
2283static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2284{
2285 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002286 int rc;
2287
2288 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2289
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002290 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002291 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002292 "Data length greater than the max allowed: %d",
2293 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2294 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002295 }
2296
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002297 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2298 pn533_tm_send_complete, NULL);
2299 if (rc < 0)
2300 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002301
2302 return rc;
2303}
2304
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002305static void pn533_wq_mi_recv(struct work_struct *work)
2306{
2307 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2308 struct sk_buff *skb_cmd;
2309 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2310 struct pn533_frame *out_frame, *in_frame;
2311 struct sk_buff *skb_resp;
2312 int skb_resp_len;
2313 int rc;
2314
2315 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2316
2317 /* This is a zero payload size skb */
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002318 skb_cmd = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002319 if (skb_cmd == NULL)
2320 goto error_cmd;
2321
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01002322 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002323
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002324 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002325 if (rc)
2326 goto error_frame;
2327
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002328 skb_resp_len = PN533_FRAME_HEADER_LEN +
2329 PN533_CMD_DATAEXCH_HEAD_LEN +
2330 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2331 PN533_FRAME_TAIL_LEN;
2332
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002333 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2334 if (!skb_resp) {
2335 rc = -ENOMEM;
2336 goto error_frame;
2337 }
2338
2339 in_frame = (struct pn533_frame *) skb_resp->data;
2340 out_frame = (struct pn533_frame *) skb_cmd->data;
2341
2342 arg->skb_resp = skb_resp;
2343 arg->skb_out = skb_cmd;
2344
2345 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2346 skb_resp_len,
2347 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002348 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002349 if (!rc)
2350 return;
2351
2352 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2353 " perform data_exchange", rc);
2354
2355 kfree_skb(skb_resp);
2356
2357error_frame:
2358 kfree_skb(skb_cmd);
2359
2360error_cmd:
2361 pn533_send_ack(dev, GFP_KERNEL);
2362
2363 kfree(arg);
2364
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002365 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002366}
2367
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002368static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2369 u8 cfgdata_len)
2370{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002371 struct sk_buff *skb;
2372 struct sk_buff *resp;
2373
2374 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002375
2376 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2377
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002378 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002379
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002380 skb = pn533_alloc_skb(skb_len);
2381 if (!skb)
2382 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002383
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002384 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2385 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002386
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002387 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2388 if (IS_ERR(resp))
2389 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002390
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002391 dev_kfree_skb(resp);
2392 return 0;
2393}
2394
2395static int pn533_get_firmware_version(struct pn533 *dev,
2396 struct pn533_fw_version *fv)
2397{
2398 struct sk_buff *skb;
2399 struct sk_buff *resp;
2400
2401 skb = pn533_alloc_skb(0);
2402 if (!skb)
2403 return -ENOMEM;
2404
2405 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2406 if (IS_ERR(resp))
2407 return PTR_ERR(resp);
2408
2409 fv->ic = resp->data[0];
2410 fv->ver = resp->data[1];
2411 fv->rev = resp->data[2];
2412 fv->support = resp->data[3];
2413
2414 dev_kfree_skb(resp);
2415 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002416}
2417
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002418static int pn533_fw_reset(struct pn533 *dev)
2419{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002420 struct sk_buff *skb;
2421 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002422
2423 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2424
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002425 skb = pn533_alloc_skb(sizeof(u8));
2426 if (!skb)
2427 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002428
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002429 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002430
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002431 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2432 if (IS_ERR(resp))
2433 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002434
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002435 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002436
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002437 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002438}
2439
2440static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002441 .dev_up = NULL,
2442 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002443 .dep_link_up = pn533_dep_link_up,
2444 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445 .start_poll = pn533_start_poll,
2446 .stop_poll = pn533_stop_poll,
2447 .activate_target = pn533_activate_target,
2448 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002449 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002450 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002451};
2452
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002453static int pn533_setup(struct pn533 *dev)
2454{
2455 struct pn533_config_max_retries max_retries;
2456 struct pn533_config_timing timing;
2457 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2458 int rc;
2459
2460 switch (dev->device_type) {
2461 case PN533_DEVICE_STD:
2462 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2463 max_retries.mx_rty_psl = 2;
2464 max_retries.mx_rty_passive_act =
2465 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2466
2467 timing.rfu = PN533_CONFIG_TIMING_102;
2468 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2469 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2470
2471 break;
2472
2473 case PN533_DEVICE_PASORI:
2474 max_retries.mx_rty_atr = 0x2;
2475 max_retries.mx_rty_psl = 0x1;
2476 max_retries.mx_rty_passive_act =
2477 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2478
2479 timing.rfu = PN533_CONFIG_TIMING_102;
2480 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2481 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2482
2483 break;
2484
2485 default:
2486 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2487 dev->device_type);
2488 return -EINVAL;
2489 }
2490
2491 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2492 (u8 *)&max_retries, sizeof(max_retries));
2493 if (rc) {
2494 nfc_dev_err(&dev->interface->dev,
2495 "Error on setting MAX_RETRIES config");
2496 return rc;
2497 }
2498
2499
2500 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2501 (u8 *)&timing, sizeof(timing));
2502 if (rc) {
2503 nfc_dev_err(&dev->interface->dev,
2504 "Error on setting RF timings");
2505 return rc;
2506 }
2507
2508 switch (dev->device_type) {
2509 case PN533_DEVICE_STD:
2510 break;
2511
2512 case PN533_DEVICE_PASORI:
2513 pn533_fw_reset(dev);
2514
2515 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2516 pasori_cfg, 3);
2517 if (rc) {
2518 nfc_dev_err(&dev->interface->dev,
2519 "Error while settings PASORI config");
2520 return rc;
2521 }
2522
2523 pn533_fw_reset(dev);
2524
2525 break;
2526 }
2527
2528 return 0;
2529}
2530
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002531static int pn533_probe(struct usb_interface *interface,
2532 const struct usb_device_id *id)
2533{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002534 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002535 struct pn533 *dev;
2536 struct usb_host_interface *iface_desc;
2537 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538 int in_endpoint = 0;
2539 int out_endpoint = 0;
2540 int rc = -ENOMEM;
2541 int i;
2542 u32 protocols;
2543
2544 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2545 if (!dev)
2546 return -ENOMEM;
2547
2548 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2549 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002550 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002551
2552 iface_desc = interface->cur_altsetting;
2553 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2554 endpoint = &iface_desc->endpoint[i].desc;
2555
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002556 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002557 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002558
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002559 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002560 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002561 }
2562
2563 if (!in_endpoint || !out_endpoint) {
2564 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2565 " bulk-out endpoint");
2566 rc = -ENODEV;
2567 goto error;
2568 }
2569
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002570 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002571 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002572 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002573 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2574
2575 if (!dev->in_frame || !dev->out_frame ||
2576 !dev->in_urb || !dev->out_urb)
2577 goto error;
2578
2579 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2580 usb_rcvbulkpipe(dev->udev, in_endpoint),
2581 NULL, 0, NULL, dev);
2582 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2583 usb_sndbulkpipe(dev->udev, out_endpoint),
2584 NULL, 0,
2585 pn533_send_complete, dev);
2586
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002587 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2588 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002589 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002590 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002591 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002592 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002593 if (dev->wq == NULL)
2594 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002596 init_timer(&dev->listen_timer);
2597 dev->listen_timer.data = (unsigned long) dev;
2598 dev->listen_timer.function = pn533_listen_mode_timer;
2599
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002600 skb_queue_head_init(&dev->resp_q);
2601
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002602 INIT_LIST_HEAD(&dev->cmd_queue);
2603
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002604 usb_set_intfdata(interface, dev);
2605
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002606 memset(&fw_ver, 0, sizeof(fw_ver));
2607 rc = pn533_get_firmware_version(dev, &fw_ver);
2608 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002609 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002610
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002611 nfc_dev_info(&dev->interface->dev,
2612 "NXP PN533 firmware ver %d.%d now attached",
2613 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002614
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002615 dev->device_type = id->driver_info;
2616 switch (dev->device_type) {
2617 case PN533_DEVICE_STD:
2618 protocols = PN533_ALL_PROTOCOLS;
2619 break;
2620
2621 case PN533_DEVICE_PASORI:
2622 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2623 break;
2624
2625 default:
2626 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2627 dev->device_type);
2628 rc = -EINVAL;
2629 goto destroy_wq;
2630 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002631
Samuel Ortize8753042011-08-19 15:47:11 +02002632 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002633 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002634 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002635 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002636 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002637 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002638
2639 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2640 nfc_set_drvdata(dev->nfc_dev, dev);
2641
2642 rc = nfc_register_device(dev->nfc_dev);
2643 if (rc)
2644 goto free_nfc_dev;
2645
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002646 rc = pn533_setup(dev);
2647 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002648 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002649
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002650 return 0;
2651
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002652unregister_nfc_dev:
2653 nfc_unregister_device(dev->nfc_dev);
2654
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655free_nfc_dev:
2656 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002657
Samuel Ortiz4849f852012-04-10 19:43:17 +02002658destroy_wq:
2659 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002660error:
2661 kfree(dev->in_frame);
2662 usb_free_urb(dev->in_urb);
2663 kfree(dev->out_frame);
2664 usb_free_urb(dev->out_urb);
2665 kfree(dev);
2666 return rc;
2667}
2668
2669static void pn533_disconnect(struct usb_interface *interface)
2670{
2671 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002672 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002673
2674 dev = usb_get_intfdata(interface);
2675 usb_set_intfdata(interface, NULL);
2676
2677 nfc_unregister_device(dev->nfc_dev);
2678 nfc_free_device(dev->nfc_dev);
2679
2680 usb_kill_urb(dev->in_urb);
2681 usb_kill_urb(dev->out_urb);
2682
Samuel Ortiz4849f852012-04-10 19:43:17 +02002683 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002684
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002685 skb_queue_purge(&dev->resp_q);
2686
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002687 del_timer(&dev->listen_timer);
2688
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002689 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2690 list_del(&cmd->queue);
2691 kfree(cmd);
2692 }
2693
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002694 kfree(dev->in_frame);
2695 usb_free_urb(dev->in_urb);
2696 kfree(dev->out_frame);
2697 usb_free_urb(dev->out_urb);
2698 kfree(dev);
2699
Dan Carpenter276556d2011-07-08 10:21:15 +03002700 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002701}
2702
2703static struct usb_driver pn533_driver = {
2704 .name = "pn533",
2705 .probe = pn533_probe,
2706 .disconnect = pn533_disconnect,
2707 .id_table = pn533_table,
2708};
2709
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002710module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002711
2712MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2713 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2714MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2715MODULE_VERSION(VERSION);
2716MODULE_LICENSE("GPL");