blob: b445f0339e03f05eefe4e2b5fef2387eff14fbaa [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;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100363 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200364 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300365 u8 cmd;
366
367 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
368 u8 poll_mod_count;
369 u8 poll_mod_curr;
370 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200371 u32 listen_protocols;
372
373 u8 *gb;
374 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300375
376 u8 tgt_available_prots;
377 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200378 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200379
380 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200381
382 struct list_head cmd_queue;
383 u8 cmd_pending;
384};
385
386struct pn533_cmd {
387 struct list_head queue;
388 struct pn533_frame *out_frame;
389 struct pn533_frame *in_frame;
390 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100391 u8 cmd_code;
392 struct sk_buff *req;
393 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200394 pn533_cmd_complete_t cmd_complete;
395 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396};
397
398struct pn533_frame {
399 u8 preamble;
400 __be16 start_frame;
401 u8 datalen;
402 u8 datalen_checksum;
403 u8 data[];
404} __packed;
405
406/* The rule: value + checksum = 0 */
407static inline u8 pn533_checksum(u8 value)
408{
409 return ~value + 1;
410}
411
412/* The rule: sum(data elements) + checksum = 0 */
413static u8 pn533_data_checksum(u8 *data, int datalen)
414{
415 u8 sum = 0;
416 int i;
417
418 for (i = 0; i < datalen; i++)
419 sum += data[i];
420
421 return pn533_checksum(sum);
422}
423
424/**
425 * pn533_tx_frame_ack - create a ack frame
426 * @frame: The frame to be set as ack
427 *
428 * Ack is different type of standard frame. As a standard frame, it has
429 * preamble and start_frame. However the checksum of this frame must fail,
430 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
431 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
432 * After datalen_checksum field, the postamble is placed.
433 */
434static void pn533_tx_frame_ack(struct pn533_frame *frame)
435{
436 frame->preamble = 0;
437 frame->start_frame = cpu_to_be16(PN533_SOF);
438 frame->datalen = 0;
439 frame->datalen_checksum = 0xFF;
440 /* data[0] is used as postamble */
441 frame->data[0] = 0;
442}
443
444static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
445{
446 frame->preamble = 0;
447 frame->start_frame = cpu_to_be16(PN533_SOF);
448 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
449 PN533_FRAME_CMD(frame) = cmd;
450 frame->datalen = 2;
451}
452
453static void pn533_tx_frame_finish(struct pn533_frame *frame)
454{
455 frame->datalen_checksum = pn533_checksum(frame->datalen);
456
457 PN533_FRAME_CHECKSUM(frame) =
458 pn533_data_checksum(frame->data, frame->datalen);
459
460 PN533_FRAME_POSTAMBLE(frame) = 0;
461}
462
463static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
464{
465 u8 checksum;
466
467 if (frame->start_frame != cpu_to_be16(PN533_SOF))
468 return false;
469
470 checksum = pn533_checksum(frame->datalen);
471 if (checksum != frame->datalen_checksum)
472 return false;
473
474 checksum = pn533_data_checksum(frame->data, frame->datalen);
475 if (checksum != PN533_FRAME_CHECKSUM(frame))
476 return false;
477
478 return true;
479}
480
481static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
482{
483 if (frame->start_frame != cpu_to_be16(PN533_SOF))
484 return false;
485
486 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
487 return false;
488
489 return true;
490}
491
492static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
493{
494 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
495}
496
Samuel Ortiz4849f852012-04-10 19:43:17 +0200497
498static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300499{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200500 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200501 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300502 int rc;
503
Samuel Ortiz4849f852012-04-10 19:43:17 +0200504 in_frame = dev->wq_in_frame;
505
506 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300507 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200508 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300509 else
510 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
511 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
512 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
513
514 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200515 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516}
517
518static void pn533_recv_response(struct urb *urb)
519{
520 struct pn533 *dev = urb->context;
521 struct pn533_frame *in_frame;
522
Samuel Ortiz4849f852012-04-10 19:43:17 +0200523 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524
525 switch (urb->status) {
526 case 0:
527 /* success */
528 break;
529 case -ECONNRESET:
530 case -ENOENT:
531 case -ESHUTDOWN:
532 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
533 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200534 dev->wq_in_error = urb->status;
535 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300536 default:
537 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
538 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200539 dev->wq_in_error = urb->status;
540 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300541 }
542
543 in_frame = dev->in_urb->transfer_buffer;
544
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100545 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
546 in_frame, PN533_FRAME_SIZE(in_frame), false);
547
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 if (!pn533_rx_frame_is_valid(in_frame)) {
549 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200550 dev->wq_in_error = -EIO;
551 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300552 }
553
554 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
555 nfc_dev_err(&dev->interface->dev, "The received frame is not "
556 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200557 dev->wq_in_error = -EIO;
558 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300559 }
560
561 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200562 dev->wq_in_error = 0;
563 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300564
Samuel Ortiz4849f852012-04-10 19:43:17 +0200565sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200566 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567}
568
569static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
570{
571 dev->in_urb->complete = pn533_recv_response;
572
573 return usb_submit_urb(dev->in_urb, flags);
574}
575
576static void pn533_recv_ack(struct urb *urb)
577{
578 struct pn533 *dev = urb->context;
579 struct pn533_frame *in_frame;
580 int rc;
581
582 switch (urb->status) {
583 case 0:
584 /* success */
585 break;
586 case -ECONNRESET:
587 case -ENOENT:
588 case -ESHUTDOWN:
589 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
590 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200591 dev->wq_in_error = urb->status;
592 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300593 default:
594 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
595 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200596 dev->wq_in_error = urb->status;
597 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300598 }
599
600 in_frame = dev->in_urb->transfer_buffer;
601
602 if (!pn533_rx_frame_is_ack(in_frame)) {
603 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200604 dev->wq_in_error = -EIO;
605 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606 }
607
608 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
609
610 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
611 if (rc) {
612 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
613 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200614 dev->wq_in_error = rc;
615 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300616 }
617
618 return;
619
Samuel Ortiz4849f852012-04-10 19:43:17 +0200620sched_wq:
621 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200622 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300623}
624
625static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
626{
627 dev->in_urb->complete = pn533_recv_ack;
628
629 return usb_submit_urb(dev->in_urb, flags);
630}
631
632static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
633{
634 int rc;
635
636 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
637
638 pn533_tx_frame_ack(dev->out_frame);
639
640 dev->out_urb->transfer_buffer = dev->out_frame;
641 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
642 rc = usb_submit_urb(dev->out_urb, flags);
643
644 return rc;
645}
646
647static int __pn533_send_cmd_frame_async(struct pn533 *dev,
648 struct pn533_frame *out_frame,
649 struct pn533_frame *in_frame,
650 int in_frame_len,
651 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100652 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300653{
654 int rc;
655
656 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
657 PN533_FRAME_CMD(out_frame));
658
659 dev->cmd = PN533_FRAME_CMD(out_frame);
660 dev->cmd_complete = cmd_complete;
661 dev->cmd_complete_arg = arg;
662
663 dev->out_urb->transfer_buffer = out_frame;
664 dev->out_urb->transfer_buffer_length =
665 PN533_FRAME_SIZE(out_frame);
666
667 dev->in_urb->transfer_buffer = in_frame;
668 dev->in_urb->transfer_buffer_length = in_frame_len;
669
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100670 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
671 out_frame, PN533_FRAME_SIZE(out_frame), false);
672
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100673 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300674 if (rc)
675 return rc;
676
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100677 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300678 if (rc)
679 goto error;
680
681 return 0;
682
683error:
684 usb_unlink_urb(dev->out_urb);
685 return rc;
686}
687
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100688static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
689{
690 struct pn533_frame *frame;
691 /* payload is already there, just update datalen */
692 int payload_len = skb->len;
693
694 skb_push(skb, PN533_FRAME_HEADER_LEN);
695 skb_put(skb, PN533_FRAME_TAIL_LEN);
696
697 frame = (struct pn533_frame *)skb->data;
698
699 pn533_tx_frame_init(frame, cmd_code);
700 frame->datalen += payload_len;
701 pn533_tx_frame_finish(frame);
702}
703
704struct pn533_send_async_complete_arg {
705 pn533_send_async_complete_t complete_cb;
706 void *complete_cb_context;
707 struct sk_buff *resp;
708 struct sk_buff *req;
709};
710
711static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
712 int params_len)
713{
714 struct pn533_send_async_complete_arg *arg = _arg;
715
716 struct sk_buff *req = arg->req;
717 struct sk_buff *resp = arg->resp;
718
719 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
720 int rc;
721
722 dev_kfree_skb(req);
723
724 if (params_len < 0) {
725 nfc_dev_err(&dev->interface->dev,
726 "Error %d when starting as a target",
727 params_len);
728
729 arg->complete_cb(dev, arg->complete_cb_context,
730 ERR_PTR(params_len));
731 rc = params_len;
732 dev_kfree_skb(resp);
733 goto out;
734 }
735
736 skb_put(resp, PN533_FRAME_SIZE(frame));
737 skb_pull(resp, PN533_FRAME_HEADER_LEN);
738 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
739
740 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
741
742out:
743 kfree(arg);
744 return rc;
745}
746
747static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
748 struct sk_buff *req, struct sk_buff *resp,
749 int resp_len,
750 pn533_send_async_complete_t complete_cb,
751 void *complete_cb_context)
752{
753 struct pn533_cmd *cmd;
754 struct pn533_send_async_complete_arg *arg;
755 int rc = 0;
756
757 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
758
759 arg = kzalloc(sizeof(arg), GFP_KERNEL);
760 if (!arg)
761 return -ENOMEM;
762
763 arg->complete_cb = complete_cb;
764 arg->complete_cb_context = complete_cb_context;
765 arg->resp = resp;
766 arg->req = req;
767
768 pn533_build_cmd_frame(cmd_code, req);
769
770 mutex_lock(&dev->cmd_lock);
771
772 if (!dev->cmd_pending) {
773 rc = __pn533_send_cmd_frame_async(dev,
774 (struct pn533_frame *)req->data,
775 (struct pn533_frame *)resp->data,
776 resp_len, pn533_send_async_complete,
777 arg);
778 if (rc)
779 goto error;
780
781 dev->cmd_pending = 1;
782 goto unlock;
783 }
784
785 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
786
787 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
788 if (!cmd) {
789 rc = -ENOMEM;
790 goto error;
791 }
792
793 INIT_LIST_HEAD(&cmd->queue);
794 cmd->cmd_code = cmd_code;
795 cmd->req = req;
796 cmd->resp = resp;
797 cmd->arg = arg;
798
799 list_add_tail(&cmd->queue, &dev->cmd_queue);
800
801 goto unlock;
802
803error:
804 kfree(arg);
805unlock:
806 mutex_unlock(&dev->cmd_lock);
807 return rc;
808}
809
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100810static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
811 struct sk_buff *req,
812 pn533_send_async_complete_t complete_cb,
813 void *complete_cb_context)
814{
815 struct sk_buff *resp;
816 int rc;
817 int resp_len = PN533_FRAME_HEADER_LEN +
818 PN533_FRAME_MAX_PAYLOAD_LEN +
819 PN533_FRAME_TAIL_LEN;
820
821 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
822
823 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
824 if (!resp)
825 return -ENOMEM;
826
827 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
828 complete_cb_context);
829 if (rc)
830 dev_kfree_skb(resp);
831
832 return rc;
833}
834
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100835static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
836 struct sk_buff *req,
837 pn533_send_async_complete_t complete_cb,
838 void *complete_cb_context)
839{
840 struct sk_buff *resp;
841 int rc;
842
843 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
844
845 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
846 if (!resp)
847 return -ENOMEM;
848
849 rc = __pn533_send_async(dev, cmd_code, req, resp,
850 PN533_NORMAL_FRAME_MAX_LEN,
851 complete_cb, complete_cb_context);
852 if (rc)
853 dev_kfree_skb(resp);
854
855 return rc;
856}
857
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100858/*
859 * pn533_send_cmd_direct_async
860 *
861 * The function sends a piority cmd directly to the chip omiting the cmd
862 * queue. It's intended to be used by chaining mechanism of received responses
863 * where the host has to request every single chunk of data before scheduling
864 * next cmd from the queue.
865 */
866static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
867 struct sk_buff *req,
868 pn533_send_async_complete_t complete_cb,
869 void *complete_cb_context)
870{
871 struct pn533_send_async_complete_arg *arg;
872 struct sk_buff *resp;
873 int rc;
874 int resp_len = PN533_FRAME_HEADER_LEN +
875 PN533_FRAME_MAX_PAYLOAD_LEN +
876 PN533_FRAME_TAIL_LEN;
877
878 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
879
880 resp = alloc_skb(resp_len, GFP_KERNEL);
881 if (!resp)
882 return -ENOMEM;
883
884 arg = kzalloc(sizeof(arg), GFP_KERNEL);
885 if (!arg) {
886 dev_kfree_skb(resp);
887 return -ENOMEM;
888 }
889
890 arg->complete_cb = complete_cb;
891 arg->complete_cb_context = complete_cb_context;
892 arg->resp = resp;
893 arg->req = req;
894
895 pn533_build_cmd_frame(cmd_code, req);
896
897 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
898 (struct pn533_frame *)resp->data,
899 resp_len, pn533_send_async_complete,
900 arg);
901 if (rc < 0) {
902 dev_kfree_skb(resp);
903 kfree(arg);
904 }
905
906 return rc;
907}
908
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200909static void pn533_wq_cmd(struct work_struct *work)
910{
911 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
912 struct pn533_cmd *cmd;
913
914 mutex_lock(&dev->cmd_lock);
915
916 if (list_empty(&dev->cmd_queue)) {
917 dev->cmd_pending = 0;
918 mutex_unlock(&dev->cmd_lock);
919 return;
920 }
921
922 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
923
Szymon Janc60ad07a2012-10-25 17:29:45 +0200924 list_del(&cmd->queue);
925
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200926 mutex_unlock(&dev->cmd_lock);
927
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100928 if (cmd->cmd_code != PN533_CMD_UNDEF)
929 __pn533_send_cmd_frame_async(dev,
930 (struct pn533_frame *)cmd->req->data,
931 (struct pn533_frame *)cmd->resp->data,
932 PN533_NORMAL_FRAME_MAX_LEN,
933 pn533_send_async_complete,
934 cmd->arg);
935 else
936 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
937 cmd->in_frame_len,
938 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200939
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200940 kfree(cmd);
941}
942
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300943static int pn533_send_cmd_frame_async(struct pn533 *dev,
944 struct pn533_frame *out_frame,
945 struct pn533_frame *in_frame,
946 int in_frame_len,
947 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100948 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300949{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200950 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200951 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300952
953 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
954
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200955 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300956
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200957 if (!dev->cmd_pending) {
958 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
959 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100960 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200961 if (!rc)
962 dev->cmd_pending = 1;
963
Szymon Jancee5e8d82012-09-27 09:16:54 +0200964 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200965 }
966
967 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
968
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100969 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200970 if (!cmd) {
971 rc = -ENOMEM;
972 goto unlock;
973 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200974
975 INIT_LIST_HEAD(&cmd->queue);
976 cmd->out_frame = out_frame;
977 cmd->in_frame = in_frame;
978 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100979 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200980 cmd->cmd_complete = cmd_complete;
981 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200982
983 list_add_tail(&cmd->queue, &dev->cmd_queue);
984
Szymon Jancee5e8d82012-09-27 09:16:54 +0200985unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200986 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300987
Szymon Jancee5e8d82012-09-27 09:16:54 +0200988 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300989}
990
991struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100992 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300993 struct completion done;
994};
995
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100996static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
997 struct sk_buff *resp)
998{
999 struct pn533_sync_cmd_response *arg = _arg;
1000
1001 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1002
1003 arg->resp = resp;
1004 complete(&arg->done);
1005
1006 return 0;
1007}
1008
1009/* pn533_send_cmd_sync
1010 *
1011 * Please note the req parameter is freed inside the function to
1012 * limit a number of return value interpretations by the caller.
1013 *
1014 * 1. negative in case of error during TX path -> req should be freed
1015 *
1016 * 2. negative in case of error during RX path -> req should not be freed
1017 * as it's been already freed at the begining of RX path by
1018 * async_complete_cb.
1019 *
1020 * 3. valid pointer in case of succesfult RX path
1021 *
1022 * A caller has to check a return value with IS_ERR macro. If the test pass,
1023 * the returned pointer is valid.
1024 *
1025 * */
1026static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1027 struct sk_buff *req)
1028{
1029 int rc;
1030 struct pn533_sync_cmd_response arg;
1031
1032 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1033
1034 init_completion(&arg.done);
1035
1036 rc = pn533_send_cmd_async(dev, cmd_code, req,
1037 pn533_send_sync_complete, &arg);
1038 if (rc) {
1039 dev_kfree_skb(req);
1040 return ERR_PTR(rc);
1041 }
1042
1043 wait_for_completion(&arg.done);
1044
1045 return arg.resp;
1046}
1047
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001048static void pn533_send_complete(struct urb *urb)
1049{
1050 struct pn533 *dev = urb->context;
1051
1052 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1053
1054 switch (urb->status) {
1055 case 0:
1056 /* success */
1057 break;
1058 case -ECONNRESET:
1059 case -ENOENT:
1060 case -ESHUTDOWN:
1061 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1062 " status: %d", urb->status);
1063 break;
1064 default:
1065 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1066 " %d", urb->status);
1067 }
1068}
1069
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001070static struct sk_buff *pn533_alloc_skb(unsigned int size)
1071{
1072 struct sk_buff *skb;
1073
1074 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1075 size +
1076 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1077
1078 if (skb)
1079 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1080
1081 return skb;
1082}
1083
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001084struct pn533_target_type_a {
1085 __be16 sens_res;
1086 u8 sel_res;
1087 u8 nfcid_len;
1088 u8 nfcid_data[];
1089} __packed;
1090
1091
1092#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1093#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1094#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1095
1096#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1097#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1098
1099#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1100#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1101
1102#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1103#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1104#define PN533_TYPE_A_SEL_PROT_DEP 2
1105#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1106
1107static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1108 int target_data_len)
1109{
1110 u8 ssd;
1111 u8 platconf;
1112
1113 if (target_data_len < sizeof(struct pn533_target_type_a))
1114 return false;
1115
1116 /* The lenght check of nfcid[] and ats[] are not being performed because
1117 the values are not being used */
1118
1119 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1120 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1121 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1122
1123 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1124 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1125 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1126 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1127 return false;
1128
1129 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1130 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1131 return false;
1132
1133 return true;
1134}
1135
1136static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1137 int tgt_data_len)
1138{
1139 struct pn533_target_type_a *tgt_type_a;
1140
1141 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1142
1143 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1144 return -EPROTO;
1145
1146 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1147 case PN533_TYPE_A_SEL_PROT_MIFARE:
1148 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1149 break;
1150 case PN533_TYPE_A_SEL_PROT_ISO14443:
1151 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1152 break;
1153 case PN533_TYPE_A_SEL_PROT_DEP:
1154 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1155 break;
1156 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1157 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1158 NFC_PROTO_NFC_DEP_MASK;
1159 break;
1160 }
1161
1162 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1163 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001164 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1165 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001166
1167 return 0;
1168}
1169
1170struct pn533_target_felica {
1171 u8 pol_res;
1172 u8 opcode;
1173 u8 nfcid2[8];
1174 u8 pad[8];
1175 /* optional */
1176 u8 syst_code[];
1177} __packed;
1178
1179#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1180#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1181
1182static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1183 int target_data_len)
1184{
1185 if (target_data_len < sizeof(struct pn533_target_felica))
1186 return false;
1187
1188 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1189 return false;
1190
1191 return true;
1192}
1193
1194static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1195 int tgt_data_len)
1196{
1197 struct pn533_target_felica *tgt_felica;
1198
1199 tgt_felica = (struct pn533_target_felica *) tgt_data;
1200
1201 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1202 return -EPROTO;
1203
1204 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1205 tgt_felica->nfcid2[1] ==
1206 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1207 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1208 else
1209 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1210
Samuel Ortiz79757542012-03-05 01:03:45 +01001211 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1212 nfc_tgt->sensf_res_len = 9;
1213
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001214 return 0;
1215}
1216
1217struct pn533_target_jewel {
1218 __be16 sens_res;
1219 u8 jewelid[4];
1220} __packed;
1221
1222static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1223 int target_data_len)
1224{
1225 u8 ssd;
1226 u8 platconf;
1227
1228 if (target_data_len < sizeof(struct pn533_target_jewel))
1229 return false;
1230
1231 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1232 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1233 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1234
1235 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1236 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1237 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1238 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1239 return false;
1240
1241 return true;
1242}
1243
1244static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1245 int tgt_data_len)
1246{
1247 struct pn533_target_jewel *tgt_jewel;
1248
1249 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1250
1251 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1252 return -EPROTO;
1253
1254 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1255 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001256 nfc_tgt->nfcid1_len = 4;
1257 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001258
1259 return 0;
1260}
1261
1262struct pn533_type_b_prot_info {
1263 u8 bitrate;
1264 u8 fsci_type;
1265 u8 fwi_adc_fo;
1266} __packed;
1267
1268#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1269#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1270#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1271
1272struct pn533_type_b_sens_res {
1273 u8 opcode;
1274 u8 nfcid[4];
1275 u8 appdata[4];
1276 struct pn533_type_b_prot_info prot_info;
1277} __packed;
1278
1279#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1280
1281struct pn533_target_type_b {
1282 struct pn533_type_b_sens_res sensb_res;
1283 u8 attrib_res_len;
1284 u8 attrib_res[];
1285} __packed;
1286
1287static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1288 int target_data_len)
1289{
1290 if (target_data_len < sizeof(struct pn533_target_type_b))
1291 return false;
1292
1293 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1294 return false;
1295
1296 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1297 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1298 return false;
1299
1300 return true;
1301}
1302
1303static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1304 int tgt_data_len)
1305{
1306 struct pn533_target_type_b *tgt_type_b;
1307
1308 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1309
1310 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1311 return -EPROTO;
1312
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001313 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001314
1315 return 0;
1316}
1317
1318struct pn533_poll_response {
1319 u8 nbtg;
1320 u8 tg;
1321 u8 target_data[];
1322} __packed;
1323
1324static int pn533_target_found(struct pn533 *dev,
1325 struct pn533_poll_response *resp, int resp_len)
1326{
1327 int target_data_len;
1328 struct nfc_target nfc_tgt;
1329 int rc;
1330
1331 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1332 dev->poll_mod_curr);
1333
1334 if (resp->tg != 1)
1335 return -EPROTO;
1336
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001337 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1338
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001339 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1340
1341 switch (dev->poll_mod_curr) {
1342 case PN533_POLL_MOD_106KBPS_A:
1343 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1344 target_data_len);
1345 break;
1346 case PN533_POLL_MOD_212KBPS_FELICA:
1347 case PN533_POLL_MOD_424KBPS_FELICA:
1348 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1349 target_data_len);
1350 break;
1351 case PN533_POLL_MOD_106KBPS_JEWEL:
1352 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1353 target_data_len);
1354 break;
1355 case PN533_POLL_MOD_847KBPS_B:
1356 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1357 target_data_len);
1358 break;
1359 default:
1360 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1361 " modulation");
1362 return -EPROTO;
1363 }
1364
1365 if (rc)
1366 return rc;
1367
1368 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1369 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1370 " have the desired protocol");
1371 return -EAGAIN;
1372 }
1373
1374 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1375 "0x%x", nfc_tgt.supported_protocols);
1376
1377 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1378
1379 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1380
1381 return 0;
1382}
1383
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001384static inline void pn533_poll_next_mod(struct pn533 *dev)
1385{
1386 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1387}
1388
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001389static void pn533_poll_reset_mod_list(struct pn533 *dev)
1390{
1391 dev->poll_mod_count = 0;
1392}
1393
1394static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1395{
1396 dev->poll_mod_active[dev->poll_mod_count] =
1397 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1398 dev->poll_mod_count++;
1399}
1400
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001401static void pn533_poll_create_mod_list(struct pn533 *dev,
1402 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001403{
1404 pn533_poll_reset_mod_list(dev);
1405
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001406 if (im_protocols & NFC_PROTO_MIFARE_MASK
1407 || im_protocols & NFC_PROTO_ISO14443_MASK
1408 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001409 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1410
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001411 if (im_protocols & NFC_PROTO_FELICA_MASK
1412 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001413 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1414 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1415 }
1416
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001417 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001418 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1419
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001420 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001421 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001422
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001423 if (tm_protocols)
1424 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001425}
1426
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001427static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001428{
1429 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001430 int rc;
1431
1432 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1433
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001434 resp = (struct pn533_poll_response *) params;
1435 if (resp->nbtg) {
1436 rc = pn533_target_found(dev, resp, params_len);
1437
1438 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001439 if (rc == 0) {
1440 pn533_poll_reset_mod_list(dev);
1441 return 0;
1442 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001443 }
1444
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001445 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001446}
1447
Samuel Ortizad3823c2012-05-30 23:54:55 +02001448static int pn533_init_target_frame(struct pn533_frame *frame,
1449 u8 *gb, size_t gb_len)
1450{
1451 struct pn533_cmd_init_target *cmd;
1452 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001453 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1454 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1455 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1456 0xff, 0xff}; /* System code */
1457 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1458 0x0, 0x0, 0x0,
1459 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001460
1461 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1462 cmd = kzalloc(cmd_len, GFP_KERNEL);
1463 if (cmd == NULL)
1464 return -ENOMEM;
1465
1466 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1467
1468 /* DEP support only */
1469 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001470
1471 /* Felica params */
1472 memcpy(cmd->felica, felica_params, 18);
1473 get_random_bytes(cmd->felica + 2, 6);
1474
1475 /* NFCID3 */
1476 memset(cmd->nfcid3, 0, 10);
1477 memcpy(cmd->nfcid3, cmd->felica, 8);
1478
1479 /* MIFARE params */
1480 memcpy(cmd->mifare, mifare_params, 6);
1481
1482 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001483 cmd->gb_len = gb_len;
1484 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001485
Samuel Ortizad3823c2012-05-30 23:54:55 +02001486 /* Len Tk */
1487 cmd->gb[gb_len] = 0;
1488
1489 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001490
Samuel Ortizad3823c2012-05-30 23:54:55 +02001491 frame->datalen += cmd_len;
1492
1493 pn533_tx_frame_finish(frame);
1494
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001495 kfree(cmd);
1496
Samuel Ortizad3823c2012-05-30 23:54:55 +02001497 return 0;
1498}
1499
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001500#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001501#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1502static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001503 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001504{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001505 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001506
1507 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1508
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001509 if (IS_ERR(resp))
1510 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001511
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001512 status = resp->data[0];
1513 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001514
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001515 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001516 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001517 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001518 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001519 return 0;
1520 }
1521
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001522 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001523}
1524
1525static void pn533_wq_tg_get_data(struct work_struct *work)
1526{
1527 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001528
1529 struct sk_buff *skb;
1530 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001531
1532 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1533
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001534 skb = pn533_alloc_skb(0);
1535 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001536 return;
1537
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001538 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1539 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001540
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001541 if (rc < 0)
1542 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001543
1544 return;
1545}
1546
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001547#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001548static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001549{
1550 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001551 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1552 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001553 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001554
1555 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1556
1557 if (params_len < 0) {
1558 nfc_dev_err(&dev->interface->dev,
1559 "Error %d when starting as a target",
1560 params_len);
1561
1562 return params_len;
1563 }
1564
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001565 if (params_len < ATR_REQ_GB_OFFSET + 1)
1566 return -EINVAL;
1567
Samuel Ortizad3823c2012-05-30 23:54:55 +02001568 resp = (struct pn533_cmd_init_target_response *) params;
1569
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001570 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1571 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001572
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001573 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1574 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1575 comm_mode = NFC_COMM_ACTIVE;
1576
1577 /* Again, only DEP */
1578 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1579 return -EOPNOTSUPP;
1580
1581 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1582 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1583
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001584 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1585 comm_mode, gb, gb_len);
1586 if (rc < 0) {
1587 nfc_dev_err(&dev->interface->dev,
1588 "Error when signaling target activation");
1589 return rc;
1590 }
1591
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001592 dev->tgt_mode = 1;
1593
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001594 queue_work(dev->wq, &dev->tg_work);
1595
1596 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001597}
1598
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001599static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001600{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001601 struct pn533 *dev = (struct pn533 *) data;
1602
1603 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1604
1605 /* An ack will cancel the last issued command (poll) */
1606 pn533_send_ack(dev, GFP_ATOMIC);
1607
1608 dev->cancel_listen = 1;
1609
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 pn533_poll_next_mod(dev);
1611
1612 queue_work(dev->wq, &dev->poll_work);
1613}
1614
1615static int pn533_poll_complete(struct pn533 *dev, void *arg,
1616 u8 *params, int params_len)
1617{
1618 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001619 int rc;
1620
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1622
1623 if (params_len == -ENOENT) {
1624 if (dev->poll_mod_count != 0)
1625 return 0;
1626
1627 nfc_dev_err(&dev->interface->dev,
1628 "Polling operation has been stopped");
1629
1630 goto stop_poll;
1631 }
1632
1633 if (params_len < 0) {
1634 nfc_dev_err(&dev->interface->dev,
1635 "Error %d when running poll", params_len);
1636
1637 goto stop_poll;
1638 }
1639
1640 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1641
1642 if (cur_mod->len == 0) {
1643 del_timer(&dev->listen_timer);
1644
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001645 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001647 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001648 if (!rc)
1649 return rc;
1650 }
1651
1652 pn533_poll_next_mod(dev);
1653
1654 queue_work(dev->wq, &dev->poll_work);
1655
1656 return 0;
1657
1658stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001659 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001660 dev->poll_protocols = 0;
1661 return 0;
1662}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001663
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001664static void pn533_build_poll_frame(struct pn533 *dev,
1665 struct pn533_frame *frame,
1666 struct pn533_poll_modulations *mod)
1667{
1668 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001669
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001670 if (mod->len == 0) {
1671 /* Listen mode */
1672 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1673 } else {
1674 /* Polling mode */
1675 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1676
1677 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1678 frame->datalen += mod->len;
1679
1680 pn533_tx_frame_finish(frame);
1681 }
1682}
1683
1684static int pn533_send_poll_frame(struct pn533 *dev)
1685{
1686 struct pn533_poll_modulations *cur_mod;
1687 int rc;
1688
1689 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1690
1691 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001692
1693 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001694 PN533_NORMAL_FRAME_MAX_LEN,
1695 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001696 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001697 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001698 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001699
1700 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001701}
1702
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001703static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001705 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1706 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707 int rc;
1708
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001709 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1710
1711 nfc_dev_dbg(&dev->interface->dev,
1712 "%s cancel_listen %d modulation len %d",
1713 __func__, dev->cancel_listen, cur_mod->len);
1714
1715 if (dev->cancel_listen == 1) {
1716 dev->cancel_listen = 0;
1717 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718 }
1719
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001720 rc = pn533_send_poll_frame(dev);
1721 if (rc)
1722 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001723
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001724 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1725 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001726
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001727 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728}
1729
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001730static int pn533_start_poll(struct nfc_dev *nfc_dev,
1731 u32 im_protocols, u32 tm_protocols)
1732{
1733 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1734
1735 nfc_dev_dbg(&dev->interface->dev,
1736 "%s: im protocols 0x%x tm protocols 0x%x",
1737 __func__, im_protocols, tm_protocols);
1738
1739 if (dev->tgt_active_prot) {
1740 nfc_dev_err(&dev->interface->dev,
1741 "Cannot poll with a target already activated");
1742 return -EBUSY;
1743 }
1744
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001745 if (dev->tgt_mode) {
1746 nfc_dev_err(&dev->interface->dev,
1747 "Cannot poll while already being activated");
1748 return -EBUSY;
1749 }
1750
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001751 if (tm_protocols) {
1752 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1753 if (dev->gb == NULL)
1754 tm_protocols = 0;
1755 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001756
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001757 dev->poll_mod_curr = 0;
1758 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1759 dev->poll_protocols = im_protocols;
1760 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001761
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001762 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001763}
1764
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001765static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1766{
1767 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1768
1769 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1770
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001771 del_timer(&dev->listen_timer);
1772
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 if (!dev->poll_mod_count) {
1774 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1775 " running");
1776 return;
1777 }
1778
1779 /* An ack will cancel the last issued command (poll) */
1780 pn533_send_ack(dev, GFP_KERNEL);
1781
1782 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1783 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001784
1785 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001786}
1787
1788static int pn533_activate_target_nfcdep(struct pn533 *dev)
1789{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001790 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001791 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001792 int rc;
1793
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001794 struct sk_buff *skb;
1795 struct sk_buff *resp;
1796
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001797 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1798
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001799 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1800 if (!skb)
1801 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001802
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001803 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1804 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001805
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001806 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1807 if (IS_ERR(resp))
1808 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001809
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001810 rsp = (struct pn533_cmd_activate_response *) resp->data;
1811 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001812 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001813 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814 return -EIO;
1815
Samuel Ortiz541d9202011-12-14 16:43:10 +01001816 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001817 gt_len = resp->len - 16;
1818 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001819
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001820 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001821 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001822}
1823
Eric Lapuyade90099432012-05-07 12:31:13 +02001824static int pn533_activate_target(struct nfc_dev *nfc_dev,
1825 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001826{
1827 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1828 int rc;
1829
1830 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1831 protocol);
1832
1833 if (dev->poll_mod_count) {
1834 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1835 " polling");
1836 return -EBUSY;
1837 }
1838
1839 if (dev->tgt_active_prot) {
1840 nfc_dev_err(&dev->interface->dev, "There is already an active"
1841 " target");
1842 return -EBUSY;
1843 }
1844
1845 if (!dev->tgt_available_prots) {
1846 nfc_dev_err(&dev->interface->dev, "There is no available target"
1847 " to activate");
1848 return -EINVAL;
1849 }
1850
1851 if (!(dev->tgt_available_prots & (1 << protocol))) {
1852 nfc_dev_err(&dev->interface->dev, "The target does not support"
1853 " the requested protocol %u", protocol);
1854 return -EINVAL;
1855 }
1856
1857 if (protocol == NFC_PROTO_NFC_DEP) {
1858 rc = pn533_activate_target_nfcdep(dev);
1859 if (rc) {
1860 nfc_dev_err(&dev->interface->dev, "Error %d when"
1861 " activating target with"
1862 " NFC_DEP protocol", rc);
1863 return rc;
1864 }
1865 }
1866
1867 dev->tgt_active_prot = protocol;
1868 dev->tgt_available_prots = 0;
1869
1870 return 0;
1871}
1872
Eric Lapuyade90099432012-05-07 12:31:13 +02001873static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1874 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001875{
1876 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001877
1878 struct sk_buff *skb;
1879 struct sk_buff *resp;
1880
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001881 int rc;
1882
1883 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1884
1885 if (!dev->tgt_active_prot) {
1886 nfc_dev_err(&dev->interface->dev, "There is no active target");
1887 return;
1888 }
1889
1890 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001891 skb_queue_purge(&dev->resp_q);
1892
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001893 skb = pn533_alloc_skb(sizeof(u8));
1894 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001895 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001896
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001897 *skb_put(skb, 1) = 1; /* TG*/
1898
1899 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1900 if (IS_ERR(resp))
1901 return;
1902
1903 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001904 if (rc != PN533_CMD_RET_SUCCESS)
1905 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1906 " the target", rc);
1907
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001908 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001909 return;
1910}
1911
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001912
1913static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001914 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001915{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001916 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001917 u8 target_gt_len;
1918 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001919 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001920
1921 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001922
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001923 if (IS_ERR(resp))
1924 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001925
1926 if (dev->tgt_available_prots &&
1927 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1928 nfc_dev_err(&dev->interface->dev,
1929 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001930 rc = -EINVAL;
1931 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001932 }
1933
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001934 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1935
1936 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001937 if (rc != PN533_CMD_RET_SUCCESS) {
1938 nfc_dev_err(&dev->interface->dev,
1939 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001940 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001941 }
1942
1943 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001944 struct nfc_target nfc_target;
1945
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001946 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1947
1948 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001949 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001950 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001951 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1952 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001953 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001954
1955 dev->tgt_available_prots = 0;
1956 }
1957
1958 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1959
1960 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001961 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001962 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001963 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001964 if (rc == 0)
1965 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 dev->nfc_dev->targets[0].idx,
1967 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001968
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001969error:
1970 dev_kfree_skb(resp);
1971 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001972}
1973
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001974static int pn533_mod_to_baud(struct pn533 *dev)
1975{
1976 switch (dev->poll_mod_curr) {
1977 case PN533_POLL_MOD_106KBPS_A:
1978 return 0;
1979 case PN533_POLL_MOD_212KBPS_FELICA:
1980 return 1;
1981 case PN533_POLL_MOD_424KBPS_FELICA:
1982 return 2;
1983 default:
1984 return -EINVAL;
1985 }
1986}
1987
Samuel Ortizd7f33452012-05-29 21:45:21 +02001988#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001989static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001990 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001991{
1992 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001993 struct sk_buff *skb;
1994 int rc, baud, skb_len;
1995 u8 *next, *arg;
1996
Samuel Ortizd7f33452012-05-29 21:45:21 +02001997 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001998
1999 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2000
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002001 if (dev->poll_mod_count) {
2002 nfc_dev_err(&dev->interface->dev,
2003 "Cannot bring the DEP link up while polling");
2004 return -EBUSY;
2005 }
2006
2007 if (dev->tgt_active_prot) {
2008 nfc_dev_err(&dev->interface->dev,
2009 "There is already an active target");
2010 return -EBUSY;
2011 }
2012
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002013 baud = pn533_mod_to_baud(dev);
2014 if (baud < 0) {
2015 nfc_dev_err(&dev->interface->dev,
2016 "Invalid curr modulation %d", dev->poll_mod_curr);
2017 return baud;
2018 }
2019
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002020 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02002021 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002022 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002023
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002024 skb = pn533_alloc_skb(skb_len);
2025 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002026 return -ENOMEM;
2027
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002028 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2029 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002030
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002031 next = skb_put(skb, 1); /* Next */
2032 *next = 0;
2033
2034 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
2035 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
2036 PASSIVE_DATA_LEN);
2037 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002038 }
2039
Samuel Ortiz47807d32012-03-05 01:03:50 +01002040 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002041 memcpy(skb_put(skb, gb_len), gb, gb_len);
2042 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002043 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002044 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002045 }
2046
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002047 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2048 if (!arg) {
2049 dev_kfree_skb(skb);
2050 return -ENOMEM;
2051 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002052
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002053 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002054
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002055 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2056 pn533_in_dep_link_up_complete, arg);
2057
2058 if (rc < 0) {
2059 dev_kfree_skb(skb);
2060 kfree(arg);
2061 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002062
2063 return rc;
2064}
2065
2066static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2067{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002068 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2069
2070 pn533_poll_reset_mod_list(dev);
2071
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002072 if (dev->tgt_mode || dev->tgt_active_prot) {
2073 pn533_send_ack(dev, GFP_KERNEL);
2074 usb_kill_urb(dev->in_urb);
2075 }
2076
2077 dev->tgt_active_prot = 0;
2078 dev->tgt_mode = 0;
2079
2080 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002081
2082 return 0;
2083}
2084
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002086 data_exchange_cb_t cb;
2087 void *cb_context;
2088};
2089
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002090static struct sk_buff *pn533_build_response(struct pn533 *dev)
2091{
2092 struct sk_buff *skb, *tmp, *t;
2093 unsigned int skb_len = 0, tmp_len = 0;
2094
2095 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2096
2097 if (skb_queue_empty(&dev->resp_q))
2098 return NULL;
2099
2100 if (skb_queue_len(&dev->resp_q) == 1) {
2101 skb = skb_dequeue(&dev->resp_q);
2102 goto out;
2103 }
2104
2105 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2106 skb_len += tmp->len;
2107
2108 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2109 __func__, skb_len);
2110
2111 skb = alloc_skb(skb_len, GFP_KERNEL);
2112 if (skb == NULL)
2113 goto out;
2114
2115 skb_put(skb, skb_len);
2116
2117 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2118 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2119 tmp_len += tmp->len;
2120 }
2121
2122out:
2123 skb_queue_purge(&dev->resp_q);
2124
2125 return skb;
2126}
2127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002128static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002129 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130{
2131 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002132 struct sk_buff *skb;
2133 int rc = 0;
2134 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002135
2136 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2137
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002138 if (IS_ERR(resp)) {
2139 rc = PTR_ERR(resp);
2140 goto _error;
2141 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002142
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002143 status = resp->data[0];
2144 ret = status & PN533_CMD_RET_MASK;
2145 mi = status & PN533_CMD_MI_MASK;
2146
2147 skb_pull(resp, sizeof(status));
2148
2149 if (ret != PN533_CMD_RET_SUCCESS) {
2150 nfc_dev_err(&dev->interface->dev,
2151 "PN533 reported error %d when exchanging data",
2152 ret);
2153 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002154 goto error;
2155 }
2156
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002157 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002159 if (mi) {
2160 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002161 queue_work(dev->wq, &dev->mi_work);
2162 return -EINPROGRESS;
2163 }
2164
2165 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002166 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002167 goto error;
2168
2169 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002170 kfree(arg);
2171 return 0;
2172
2173error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002174 dev_kfree_skb(resp);
2175_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002176 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002177 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002178 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002179 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002180}
2181
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002182static int pn533_transceive(struct nfc_dev *nfc_dev,
2183 struct nfc_target *target, struct sk_buff *skb,
2184 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002185{
2186 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002187 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002188 int rc;
2189
2190 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2191
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002192 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2193 /* TODO: Implement support to multi-part data exchange */
2194 nfc_dev_err(&dev->interface->dev,
2195 "Data length greater than the max allowed: %d",
2196 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2197 rc = -ENOSYS;
2198 goto error;
2199 }
2200
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002201 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
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002208 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002209 if (!arg) {
2210 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002211 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002212 }
2213
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002214 arg->cb = cb;
2215 arg->cb_context = cb_context;
2216
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002217 switch (dev->device_type) {
2218 case PN533_DEVICE_PASORI:
2219 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2220 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2221 skb,
2222 pn533_data_exchange_complete,
2223 arg);
2224
2225 break;
2226 }
2227 default:
2228 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2229
2230 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2231 skb, pn533_data_exchange_complete,
2232 arg);
2233
2234 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002235 }
2236
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002237 if (rc < 0) /* rc from send_async */
2238 goto error;
2239
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002240 return 0;
2241
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002242error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002243 kfree(arg);
2244 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002245 return rc;
2246}
2247
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002248static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002249 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002250{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002251 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002252
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002253 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2254
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002255 if (IS_ERR(resp))
2256 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002257
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002258 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002259
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002260 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002261
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002262 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002263 nfc_tm_deactivated(dev->nfc_dev);
2264
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002265 dev->tgt_mode = 0;
2266
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002267 return 0;
2268 }
2269
2270 queue_work(dev->wq, &dev->tg_work);
2271
2272 return 0;
2273}
2274
2275static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2276{
2277 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002278 int rc;
2279
2280 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2281
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002282 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002283 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002284 "Data length greater than the max allowed: %d",
2285 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2286 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002287 }
2288
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002289 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2290 pn533_tm_send_complete, NULL);
2291 if (rc < 0)
2292 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002293
2294 return rc;
2295}
2296
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002297static void pn533_wq_mi_recv(struct work_struct *work)
2298{
2299 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002300
2301 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002302 int rc;
2303
2304 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2305
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002306 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2307 if (!skb)
2308 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002309
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002310 switch (dev->device_type) {
2311 case PN533_DEVICE_PASORI:
2312 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2313 rc = pn533_send_cmd_direct_async(dev,
2314 PN533_CMD_IN_COMM_THRU,
2315 skb,
2316 pn533_data_exchange_complete,
2317 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002318
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002319 break;
2320 }
2321 default:
2322 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002323
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002324 rc = pn533_send_cmd_direct_async(dev,
2325 PN533_CMD_IN_DATA_EXCHANGE,
2326 skb,
2327 pn533_data_exchange_complete,
2328 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002329
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002330 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002331 }
2332
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002333 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002334 return;
2335
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002336 nfc_dev_err(&dev->interface->dev,
2337 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002338
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002339 dev_kfree_skb(skb);
2340 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002341
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002342error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002343 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002344 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002345}
2346
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002347static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2348 u8 cfgdata_len)
2349{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002350 struct sk_buff *skb;
2351 struct sk_buff *resp;
2352
2353 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002354
2355 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2356
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002357 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002358
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002359 skb = pn533_alloc_skb(skb_len);
2360 if (!skb)
2361 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002362
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002363 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2364 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002365
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002366 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2367 if (IS_ERR(resp))
2368 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002369
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002370 dev_kfree_skb(resp);
2371 return 0;
2372}
2373
2374static int pn533_get_firmware_version(struct pn533 *dev,
2375 struct pn533_fw_version *fv)
2376{
2377 struct sk_buff *skb;
2378 struct sk_buff *resp;
2379
2380 skb = pn533_alloc_skb(0);
2381 if (!skb)
2382 return -ENOMEM;
2383
2384 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2385 if (IS_ERR(resp))
2386 return PTR_ERR(resp);
2387
2388 fv->ic = resp->data[0];
2389 fv->ver = resp->data[1];
2390 fv->rev = resp->data[2];
2391 fv->support = resp->data[3];
2392
2393 dev_kfree_skb(resp);
2394 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002395}
2396
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002397static int pn533_fw_reset(struct pn533 *dev)
2398{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002399 struct sk_buff *skb;
2400 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002401
2402 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2403
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002404 skb = pn533_alloc_skb(sizeof(u8));
2405 if (!skb)
2406 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002407
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002408 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002409
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002410 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2411 if (IS_ERR(resp))
2412 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002413
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002414 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002415
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002416 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002417}
2418
2419static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002420 .dev_up = NULL,
2421 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002422 .dep_link_up = pn533_dep_link_up,
2423 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002424 .start_poll = pn533_start_poll,
2425 .stop_poll = pn533_stop_poll,
2426 .activate_target = pn533_activate_target,
2427 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002428 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002429 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002430};
2431
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002432static int pn533_setup(struct pn533 *dev)
2433{
2434 struct pn533_config_max_retries max_retries;
2435 struct pn533_config_timing timing;
2436 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2437 int rc;
2438
2439 switch (dev->device_type) {
2440 case PN533_DEVICE_STD:
2441 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2442 max_retries.mx_rty_psl = 2;
2443 max_retries.mx_rty_passive_act =
2444 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2445
2446 timing.rfu = PN533_CONFIG_TIMING_102;
2447 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2448 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2449
2450 break;
2451
2452 case PN533_DEVICE_PASORI:
2453 max_retries.mx_rty_atr = 0x2;
2454 max_retries.mx_rty_psl = 0x1;
2455 max_retries.mx_rty_passive_act =
2456 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2457
2458 timing.rfu = PN533_CONFIG_TIMING_102;
2459 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2460 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2461
2462 break;
2463
2464 default:
2465 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2466 dev->device_type);
2467 return -EINVAL;
2468 }
2469
2470 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2471 (u8 *)&max_retries, sizeof(max_retries));
2472 if (rc) {
2473 nfc_dev_err(&dev->interface->dev,
2474 "Error on setting MAX_RETRIES config");
2475 return rc;
2476 }
2477
2478
2479 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2480 (u8 *)&timing, sizeof(timing));
2481 if (rc) {
2482 nfc_dev_err(&dev->interface->dev,
2483 "Error on setting RF timings");
2484 return rc;
2485 }
2486
2487 switch (dev->device_type) {
2488 case PN533_DEVICE_STD:
2489 break;
2490
2491 case PN533_DEVICE_PASORI:
2492 pn533_fw_reset(dev);
2493
2494 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2495 pasori_cfg, 3);
2496 if (rc) {
2497 nfc_dev_err(&dev->interface->dev,
2498 "Error while settings PASORI config");
2499 return rc;
2500 }
2501
2502 pn533_fw_reset(dev);
2503
2504 break;
2505 }
2506
2507 return 0;
2508}
2509
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002510static int pn533_probe(struct usb_interface *interface,
2511 const struct usb_device_id *id)
2512{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002513 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002514 struct pn533 *dev;
2515 struct usb_host_interface *iface_desc;
2516 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002517 int in_endpoint = 0;
2518 int out_endpoint = 0;
2519 int rc = -ENOMEM;
2520 int i;
2521 u32 protocols;
2522
2523 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2524 if (!dev)
2525 return -ENOMEM;
2526
2527 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2528 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002529 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002530
2531 iface_desc = interface->cur_altsetting;
2532 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2533 endpoint = &iface_desc->endpoint[i].desc;
2534
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002535 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002536 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002538 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002539 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002540 }
2541
2542 if (!in_endpoint || !out_endpoint) {
2543 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2544 " bulk-out endpoint");
2545 rc = -ENODEV;
2546 goto error;
2547 }
2548
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002549 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002550 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002551 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002552 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2553
2554 if (!dev->in_frame || !dev->out_frame ||
2555 !dev->in_urb || !dev->out_urb)
2556 goto error;
2557
2558 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2559 usb_rcvbulkpipe(dev->udev, in_endpoint),
2560 NULL, 0, NULL, dev);
2561 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2562 usb_sndbulkpipe(dev->udev, out_endpoint),
2563 NULL, 0,
2564 pn533_send_complete, dev);
2565
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002566 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2567 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002568 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002569 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002570 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002571 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002572 if (dev->wq == NULL)
2573 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002574
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002575 init_timer(&dev->listen_timer);
2576 dev->listen_timer.data = (unsigned long) dev;
2577 dev->listen_timer.function = pn533_listen_mode_timer;
2578
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002579 skb_queue_head_init(&dev->resp_q);
2580
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002581 INIT_LIST_HEAD(&dev->cmd_queue);
2582
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002583 usb_set_intfdata(interface, dev);
2584
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002585 memset(&fw_ver, 0, sizeof(fw_ver));
2586 rc = pn533_get_firmware_version(dev, &fw_ver);
2587 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002588 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002589
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002590 nfc_dev_info(&dev->interface->dev,
2591 "NXP PN533 firmware ver %d.%d now attached",
2592 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002593
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002594 dev->device_type = id->driver_info;
2595 switch (dev->device_type) {
2596 case PN533_DEVICE_STD:
2597 protocols = PN533_ALL_PROTOCOLS;
2598 break;
2599
2600 case PN533_DEVICE_PASORI:
2601 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2602 break;
2603
2604 default:
2605 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2606 dev->device_type);
2607 rc = -EINVAL;
2608 goto destroy_wq;
2609 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002610
Samuel Ortize8753042011-08-19 15:47:11 +02002611 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002612 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002613 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002614 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002615 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002616 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002617
2618 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2619 nfc_set_drvdata(dev->nfc_dev, dev);
2620
2621 rc = nfc_register_device(dev->nfc_dev);
2622 if (rc)
2623 goto free_nfc_dev;
2624
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002625 rc = pn533_setup(dev);
2626 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002627 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002628
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002629 return 0;
2630
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002631unregister_nfc_dev:
2632 nfc_unregister_device(dev->nfc_dev);
2633
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002634free_nfc_dev:
2635 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002636
Samuel Ortiz4849f852012-04-10 19:43:17 +02002637destroy_wq:
2638 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002639error:
2640 kfree(dev->in_frame);
2641 usb_free_urb(dev->in_urb);
2642 kfree(dev->out_frame);
2643 usb_free_urb(dev->out_urb);
2644 kfree(dev);
2645 return rc;
2646}
2647
2648static void pn533_disconnect(struct usb_interface *interface)
2649{
2650 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002651 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002652
2653 dev = usb_get_intfdata(interface);
2654 usb_set_intfdata(interface, NULL);
2655
2656 nfc_unregister_device(dev->nfc_dev);
2657 nfc_free_device(dev->nfc_dev);
2658
2659 usb_kill_urb(dev->in_urb);
2660 usb_kill_urb(dev->out_urb);
2661
Samuel Ortiz4849f852012-04-10 19:43:17 +02002662 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002663
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002664 skb_queue_purge(&dev->resp_q);
2665
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002666 del_timer(&dev->listen_timer);
2667
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002668 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2669 list_del(&cmd->queue);
2670 kfree(cmd);
2671 }
2672
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002673 kfree(dev->in_frame);
2674 usb_free_urb(dev->in_urb);
2675 kfree(dev->out_frame);
2676 usb_free_urb(dev->out_urb);
2677 kfree(dev);
2678
Dan Carpenter276556d2011-07-08 10:21:15 +03002679 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002680}
2681
2682static struct usb_driver pn533_driver = {
2683 .name = "pn533",
2684 .probe = pn533_probe,
2685 .disconnect = pn533_disconnect,
2686 .id_table = pn533_table,
2687};
2688
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002689module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002690
2691MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2692 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2693MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2694MODULE_VERSION(VERSION);
2695MODULE_LICENSE("GPL");