blob: 89f747479a4f6d98f4b023e13546649483bc49b7 [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
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030089#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010090 PN533_FRAME_TAIL_LEN)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030091#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
92#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
93#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
94
95/* start of frame */
96#define PN533_SOF 0x00FF
97
98/* frame identifier: in/out/error */
99#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
100#define PN533_DIR_OUT 0xD4
101#define PN533_DIR_IN 0xD5
102
103/* PN533 Commands */
104#define PN533_FRAME_CMD(f) (f->data[1])
105#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
106#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
107
108#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
109#define PN533_CMD_RF_CONFIGURATION 0x32
110#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200111#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300112#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
113#define PN533_CMD_IN_ATR 0x50
114#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100115#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300116
Samuel Ortizad3823c2012-05-30 23:54:55 +0200117#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200118#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200119#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100120#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
123
124/* PN533 Return codes */
125#define PN533_CMD_RET_MASK 0x3F
126#define PN533_CMD_MI_MASK 0x40
127#define PN533_CMD_RET_SUCCESS 0x00
128
129struct pn533;
130
131typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
132 u8 *params, int params_len);
133
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100134typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
135 struct sk_buff *resp);
136
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300137/* structs for pn533 commands */
138
139/* PN533_CMD_GET_FIRMWARE_VERSION */
140struct pn533_fw_version {
141 u8 ic;
142 u8 ver;
143 u8 rev;
144 u8 support;
145};
146
147/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200148#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300149#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200150#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300151
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200152#define PN533_CONFIG_TIMING_102 0xb
153#define PN533_CONFIG_TIMING_204 0xc
154#define PN533_CONFIG_TIMING_409 0xd
155#define PN533_CONFIG_TIMING_819 0xe
156
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
158#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
159
160struct pn533_config_max_retries {
161 u8 mx_rty_atr;
162 u8 mx_rty_psl;
163 u8 mx_rty_passive_act;
164} __packed;
165
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200166struct pn533_config_timing {
167 u8 rfu;
168 u8 atr_res_timeout;
169 u8 dep_timeout;
170} __packed;
171
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300172/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
173
174/* felica commands opcode */
175#define PN533_FELICA_OPC_SENSF_REQ 0
176#define PN533_FELICA_OPC_SENSF_RES 1
177/* felica SENSF_REQ parameters */
178#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
179#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
180#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
181#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
182
183/* type B initiator_data values */
184#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
185#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
186#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
187
188union pn533_cmd_poll_initdata {
189 struct {
190 u8 afi;
191 u8 polling_method;
192 } __packed type_b;
193 struct {
194 u8 opcode;
195 __be16 sc;
196 u8 rc;
197 u8 tsn;
198 } __packed felica;
199};
200
201/* Poll modulations */
202enum {
203 PN533_POLL_MOD_106KBPS_A,
204 PN533_POLL_MOD_212KBPS_FELICA,
205 PN533_POLL_MOD_424KBPS_FELICA,
206 PN533_POLL_MOD_106KBPS_JEWEL,
207 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200208 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300209
210 __PN533_POLL_MOD_AFTER_LAST,
211};
212#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
213
214struct pn533_poll_modulations {
215 struct {
216 u8 maxtg;
217 u8 brty;
218 union pn533_cmd_poll_initdata initiator_data;
219 } __packed data;
220 u8 len;
221};
222
223const struct pn533_poll_modulations poll_mod[] = {
224 [PN533_POLL_MOD_106KBPS_A] = {
225 .data = {
226 .maxtg = 1,
227 .brty = 0,
228 },
229 .len = 2,
230 },
231 [PN533_POLL_MOD_212KBPS_FELICA] = {
232 .data = {
233 .maxtg = 1,
234 .brty = 1,
235 .initiator_data.felica = {
236 .opcode = PN533_FELICA_OPC_SENSF_REQ,
237 .sc = PN533_FELICA_SENSF_SC_ALL,
238 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
239 .tsn = 0,
240 },
241 },
242 .len = 7,
243 },
244 [PN533_POLL_MOD_424KBPS_FELICA] = {
245 .data = {
246 .maxtg = 1,
247 .brty = 2,
248 .initiator_data.felica = {
249 .opcode = PN533_FELICA_OPC_SENSF_REQ,
250 .sc = PN533_FELICA_SENSF_SC_ALL,
251 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
252 .tsn = 0,
253 },
254 },
255 .len = 7,
256 },
257 [PN533_POLL_MOD_106KBPS_JEWEL] = {
258 .data = {
259 .maxtg = 1,
260 .brty = 4,
261 },
262 .len = 2,
263 },
264 [PN533_POLL_MOD_847KBPS_B] = {
265 .data = {
266 .maxtg = 1,
267 .brty = 8,
268 .initiator_data.type_b = {
269 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
270 .polling_method =
271 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
272 },
273 },
274 .len = 3,
275 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200276 [PN533_LISTEN_MOD] = {
277 .len = 0,
278 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300279};
280
281/* PN533_CMD_IN_ATR */
282
283struct pn533_cmd_activate_param {
284 u8 tg;
285 u8 next;
286} __packed;
287
288struct pn533_cmd_activate_response {
289 u8 status;
290 u8 nfcid3t[10];
291 u8 didt;
292 u8 bst;
293 u8 brt;
294 u8 to;
295 u8 ppt;
296 /* optional */
297 u8 gt[];
298} __packed;
299
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100300/* PN533_CMD_IN_JUMP_FOR_DEP */
301struct pn533_cmd_jump_dep {
302 u8 active;
303 u8 baud;
304 u8 next;
Samuel Ortizd7f33452012-05-29 21:45:21 +0200305 u8 data[];
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100306} __packed;
307
308struct pn533_cmd_jump_dep_response {
309 u8 status;
310 u8 tg;
311 u8 nfcid3t[10];
312 u8 didt;
313 u8 bst;
314 u8 brt;
315 u8 to;
316 u8 ppt;
317 /* optional */
318 u8 gt[];
319} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320
Samuel Ortizad3823c2012-05-30 23:54:55 +0200321
322/* PN533_TG_INIT_AS_TARGET */
323#define PN533_INIT_TARGET_PASSIVE 0x1
324#define PN533_INIT_TARGET_DEP 0x2
325
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200326#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
327#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
328#define PN533_INIT_TARGET_RESP_DEP 0x4
329
Samuel Ortizad3823c2012-05-30 23:54:55 +0200330struct pn533_cmd_init_target {
331 u8 mode;
332 u8 mifare[6];
333 u8 felica[18];
334 u8 nfcid3[10];
335 u8 gb_len;
336 u8 gb[];
337} __packed;
338
339struct pn533_cmd_init_target_response {
340 u8 mode;
341 u8 cmd[];
342} __packed;
343
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300344struct pn533 {
345 struct usb_device *udev;
346 struct usb_interface *interface;
347 struct nfc_dev *nfc_dev;
348
349 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350 struct pn533_frame *out_frame;
351
352 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300353 struct pn533_frame *in_frame;
354
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200355 struct sk_buff_head resp_q;
356
Samuel Ortiz4849f852012-04-10 19:43:17 +0200357 struct workqueue_struct *wq;
358 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200359 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200360 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200361 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200362 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200363 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200364 struct pn533_frame *wq_in_frame;
365 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200366 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300367
368 pn533_cmd_complete_t cmd_complete;
369 void *cmd_complete_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200370 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300371 u8 cmd;
372
373 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
374 u8 poll_mod_count;
375 u8 poll_mod_curr;
376 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200377 u32 listen_protocols;
378
379 u8 *gb;
380 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300381
382 u8 tgt_available_prots;
383 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200384 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200385
386 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200387
388 struct list_head cmd_queue;
389 u8 cmd_pending;
390};
391
392struct pn533_cmd {
393 struct list_head queue;
394 struct pn533_frame *out_frame;
395 struct pn533_frame *in_frame;
396 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100397 u8 cmd_code;
398 struct sk_buff *req;
399 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200400 pn533_cmd_complete_t cmd_complete;
401 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300402};
403
404struct pn533_frame {
405 u8 preamble;
406 __be16 start_frame;
407 u8 datalen;
408 u8 datalen_checksum;
409 u8 data[];
410} __packed;
411
412/* The rule: value + checksum = 0 */
413static inline u8 pn533_checksum(u8 value)
414{
415 return ~value + 1;
416}
417
418/* The rule: sum(data elements) + checksum = 0 */
419static u8 pn533_data_checksum(u8 *data, int datalen)
420{
421 u8 sum = 0;
422 int i;
423
424 for (i = 0; i < datalen; i++)
425 sum += data[i];
426
427 return pn533_checksum(sum);
428}
429
430/**
431 * pn533_tx_frame_ack - create a ack frame
432 * @frame: The frame to be set as ack
433 *
434 * Ack is different type of standard frame. As a standard frame, it has
435 * preamble and start_frame. However the checksum of this frame must fail,
436 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
437 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
438 * After datalen_checksum field, the postamble is placed.
439 */
440static void pn533_tx_frame_ack(struct pn533_frame *frame)
441{
442 frame->preamble = 0;
443 frame->start_frame = cpu_to_be16(PN533_SOF);
444 frame->datalen = 0;
445 frame->datalen_checksum = 0xFF;
446 /* data[0] is used as postamble */
447 frame->data[0] = 0;
448}
449
450static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
451{
452 frame->preamble = 0;
453 frame->start_frame = cpu_to_be16(PN533_SOF);
454 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
455 PN533_FRAME_CMD(frame) = cmd;
456 frame->datalen = 2;
457}
458
459static void pn533_tx_frame_finish(struct pn533_frame *frame)
460{
461 frame->datalen_checksum = pn533_checksum(frame->datalen);
462
463 PN533_FRAME_CHECKSUM(frame) =
464 pn533_data_checksum(frame->data, frame->datalen);
465
466 PN533_FRAME_POSTAMBLE(frame) = 0;
467}
468
469static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
470{
471 u8 checksum;
472
473 if (frame->start_frame != cpu_to_be16(PN533_SOF))
474 return false;
475
476 checksum = pn533_checksum(frame->datalen);
477 if (checksum != frame->datalen_checksum)
478 return false;
479
480 checksum = pn533_data_checksum(frame->data, frame->datalen);
481 if (checksum != PN533_FRAME_CHECKSUM(frame))
482 return false;
483
484 return true;
485}
486
487static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
488{
489 if (frame->start_frame != cpu_to_be16(PN533_SOF))
490 return false;
491
492 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
493 return false;
494
495 return true;
496}
497
498static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
499{
500 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
501}
502
Samuel Ortiz4849f852012-04-10 19:43:17 +0200503
504static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300505{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200506 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200507 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300508 int rc;
509
Samuel Ortiz4849f852012-04-10 19:43:17 +0200510 in_frame = dev->wq_in_frame;
511
512 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300513 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200514 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300515 else
516 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
517 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
518 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
519
520 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200521 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300522}
523
524static void pn533_recv_response(struct urb *urb)
525{
526 struct pn533 *dev = urb->context;
527 struct pn533_frame *in_frame;
528
Samuel Ortiz4849f852012-04-10 19:43:17 +0200529 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300530
531 switch (urb->status) {
532 case 0:
533 /* success */
534 break;
535 case -ECONNRESET:
536 case -ENOENT:
537 case -ESHUTDOWN:
538 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
539 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200540 dev->wq_in_error = urb->status;
541 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300542 default:
543 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
544 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200545 dev->wq_in_error = urb->status;
546 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300547 }
548
549 in_frame = dev->in_urb->transfer_buffer;
550
551 if (!pn533_rx_frame_is_valid(in_frame)) {
552 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200553 dev->wq_in_error = -EIO;
554 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300555 }
556
557 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
558 nfc_dev_err(&dev->interface->dev, "The received frame is not "
559 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200560 dev->wq_in_error = -EIO;
561 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300562 }
563
564 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200565 dev->wq_in_error = 0;
566 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567
Samuel Ortiz4849f852012-04-10 19:43:17 +0200568sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200569 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300570}
571
572static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
573{
574 dev->in_urb->complete = pn533_recv_response;
575
576 return usb_submit_urb(dev->in_urb, flags);
577}
578
579static void pn533_recv_ack(struct urb *urb)
580{
581 struct pn533 *dev = urb->context;
582 struct pn533_frame *in_frame;
583 int rc;
584
585 switch (urb->status) {
586 case 0:
587 /* success */
588 break;
589 case -ECONNRESET:
590 case -ENOENT:
591 case -ESHUTDOWN:
592 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
593 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200594 dev->wq_in_error = urb->status;
595 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300596 default:
597 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
598 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200599 dev->wq_in_error = urb->status;
600 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300601 }
602
603 in_frame = dev->in_urb->transfer_buffer;
604
605 if (!pn533_rx_frame_is_ack(in_frame)) {
606 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200607 dev->wq_in_error = -EIO;
608 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300609 }
610
611 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
612
613 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
614 if (rc) {
615 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
616 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200617 dev->wq_in_error = rc;
618 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300619 }
620
621 return;
622
Samuel Ortiz4849f852012-04-10 19:43:17 +0200623sched_wq:
624 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200625 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300626}
627
628static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
629{
630 dev->in_urb->complete = pn533_recv_ack;
631
632 return usb_submit_urb(dev->in_urb, flags);
633}
634
635static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
636{
637 int rc;
638
639 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
640
641 pn533_tx_frame_ack(dev->out_frame);
642
643 dev->out_urb->transfer_buffer = dev->out_frame;
644 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
645 rc = usb_submit_urb(dev->out_urb, flags);
646
647 return rc;
648}
649
650static int __pn533_send_cmd_frame_async(struct pn533 *dev,
651 struct pn533_frame *out_frame,
652 struct pn533_frame *in_frame,
653 int in_frame_len,
654 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100655 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300656{
657 int rc;
658
659 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
660 PN533_FRAME_CMD(out_frame));
661
662 dev->cmd = PN533_FRAME_CMD(out_frame);
663 dev->cmd_complete = cmd_complete;
664 dev->cmd_complete_arg = arg;
665
666 dev->out_urb->transfer_buffer = out_frame;
667 dev->out_urb->transfer_buffer_length =
668 PN533_FRAME_SIZE(out_frame);
669
670 dev->in_urb->transfer_buffer = in_frame;
671 dev->in_urb->transfer_buffer_length = in_frame_len;
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
810static int pn533_send_cmd_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
818 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
819
820 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
821 if (!resp)
822 return -ENOMEM;
823
824 rc = __pn533_send_async(dev, cmd_code, req, resp,
825 PN533_NORMAL_FRAME_MAX_LEN,
826 complete_cb, complete_cb_context);
827 if (rc)
828 dev_kfree_skb(resp);
829
830 return rc;
831}
832
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200833static void pn533_wq_cmd(struct work_struct *work)
834{
835 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
836 struct pn533_cmd *cmd;
837
838 mutex_lock(&dev->cmd_lock);
839
840 if (list_empty(&dev->cmd_queue)) {
841 dev->cmd_pending = 0;
842 mutex_unlock(&dev->cmd_lock);
843 return;
844 }
845
846 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
847
Szymon Janc60ad07a2012-10-25 17:29:45 +0200848 list_del(&cmd->queue);
849
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200850 mutex_unlock(&dev->cmd_lock);
851
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100852 if (cmd->cmd_code != PN533_CMD_UNDEF)
853 __pn533_send_cmd_frame_async(dev,
854 (struct pn533_frame *)cmd->req->data,
855 (struct pn533_frame *)cmd->resp->data,
856 PN533_NORMAL_FRAME_MAX_LEN,
857 pn533_send_async_complete,
858 cmd->arg);
859 else
860 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
861 cmd->in_frame_len,
862 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200863
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200864 kfree(cmd);
865}
866
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300867static int pn533_send_cmd_frame_async(struct pn533 *dev,
868 struct pn533_frame *out_frame,
869 struct pn533_frame *in_frame,
870 int in_frame_len,
871 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100872 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300873{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200874 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200875 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300876
877 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
878
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200879 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300880
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200881 if (!dev->cmd_pending) {
882 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
883 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100884 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200885 if (!rc)
886 dev->cmd_pending = 1;
887
Szymon Jancee5e8d82012-09-27 09:16:54 +0200888 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200889 }
890
891 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
892
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100893 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200894 if (!cmd) {
895 rc = -ENOMEM;
896 goto unlock;
897 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200898
899 INIT_LIST_HEAD(&cmd->queue);
900 cmd->out_frame = out_frame;
901 cmd->in_frame = in_frame;
902 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100903 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200904 cmd->cmd_complete = cmd_complete;
905 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200906
907 list_add_tail(&cmd->queue, &dev->cmd_queue);
908
Szymon Jancee5e8d82012-09-27 09:16:54 +0200909unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200910 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300911
Szymon Jancee5e8d82012-09-27 09:16:54 +0200912 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300913}
914
915struct pn533_sync_cmd_response {
916 int rc;
917 struct completion done;
918};
919
920static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
921 u8 *params, int params_len)
922{
923 struct pn533_sync_cmd_response *arg = _arg;
924
925 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
926
927 arg->rc = 0;
928
929 if (params_len < 0) /* error */
930 arg->rc = params_len;
931
932 complete(&arg->done);
933
934 return 0;
935}
936
937static int pn533_send_cmd_frame_sync(struct pn533 *dev,
938 struct pn533_frame *out_frame,
939 struct pn533_frame *in_frame,
940 int in_frame_len)
941{
942 int rc;
943 struct pn533_sync_cmd_response arg;
944
945 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
946
947 init_completion(&arg.done);
948
949 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100950 pn533_sync_cmd_complete, &arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300951 if (rc)
952 return rc;
953
954 wait_for_completion(&arg.done);
955
956 return arg.rc;
957}
958
959static void pn533_send_complete(struct urb *urb)
960{
961 struct pn533 *dev = urb->context;
962
963 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
964
965 switch (urb->status) {
966 case 0:
967 /* success */
968 break;
969 case -ECONNRESET:
970 case -ENOENT:
971 case -ESHUTDOWN:
972 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
973 " status: %d", urb->status);
974 break;
975 default:
976 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
977 " %d", urb->status);
978 }
979}
980
981struct pn533_target_type_a {
982 __be16 sens_res;
983 u8 sel_res;
984 u8 nfcid_len;
985 u8 nfcid_data[];
986} __packed;
987
988
989#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
990#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
991#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
992
993#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
994#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
995
996#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
997#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
998
999#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1000#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1001#define PN533_TYPE_A_SEL_PROT_DEP 2
1002#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1003
1004static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1005 int target_data_len)
1006{
1007 u8 ssd;
1008 u8 platconf;
1009
1010 if (target_data_len < sizeof(struct pn533_target_type_a))
1011 return false;
1012
1013 /* The lenght check of nfcid[] and ats[] are not being performed because
1014 the values are not being used */
1015
1016 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1017 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1018 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1019
1020 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1021 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1022 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1023 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1024 return false;
1025
1026 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1027 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1028 return false;
1029
1030 return true;
1031}
1032
1033static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1034 int tgt_data_len)
1035{
1036 struct pn533_target_type_a *tgt_type_a;
1037
1038 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1039
1040 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1041 return -EPROTO;
1042
1043 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1044 case PN533_TYPE_A_SEL_PROT_MIFARE:
1045 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1046 break;
1047 case PN533_TYPE_A_SEL_PROT_ISO14443:
1048 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1049 break;
1050 case PN533_TYPE_A_SEL_PROT_DEP:
1051 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1052 break;
1053 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1054 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1055 NFC_PROTO_NFC_DEP_MASK;
1056 break;
1057 }
1058
1059 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1060 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001061 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1062 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001063
1064 return 0;
1065}
1066
1067struct pn533_target_felica {
1068 u8 pol_res;
1069 u8 opcode;
1070 u8 nfcid2[8];
1071 u8 pad[8];
1072 /* optional */
1073 u8 syst_code[];
1074} __packed;
1075
1076#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1077#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1078
1079static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1080 int target_data_len)
1081{
1082 if (target_data_len < sizeof(struct pn533_target_felica))
1083 return false;
1084
1085 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1086 return false;
1087
1088 return true;
1089}
1090
1091static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1092 int tgt_data_len)
1093{
1094 struct pn533_target_felica *tgt_felica;
1095
1096 tgt_felica = (struct pn533_target_felica *) tgt_data;
1097
1098 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1099 return -EPROTO;
1100
1101 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1102 tgt_felica->nfcid2[1] ==
1103 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1104 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1105 else
1106 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1107
Samuel Ortiz79757542012-03-05 01:03:45 +01001108 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1109 nfc_tgt->sensf_res_len = 9;
1110
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001111 return 0;
1112}
1113
1114struct pn533_target_jewel {
1115 __be16 sens_res;
1116 u8 jewelid[4];
1117} __packed;
1118
1119static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1120 int target_data_len)
1121{
1122 u8 ssd;
1123 u8 platconf;
1124
1125 if (target_data_len < sizeof(struct pn533_target_jewel))
1126 return false;
1127
1128 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1129 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1130 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1131
1132 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1133 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1134 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1135 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1136 return false;
1137
1138 return true;
1139}
1140
1141static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1142 int tgt_data_len)
1143{
1144 struct pn533_target_jewel *tgt_jewel;
1145
1146 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1147
1148 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1149 return -EPROTO;
1150
1151 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1152 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001153 nfc_tgt->nfcid1_len = 4;
1154 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001155
1156 return 0;
1157}
1158
1159struct pn533_type_b_prot_info {
1160 u8 bitrate;
1161 u8 fsci_type;
1162 u8 fwi_adc_fo;
1163} __packed;
1164
1165#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1166#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1167#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1168
1169struct pn533_type_b_sens_res {
1170 u8 opcode;
1171 u8 nfcid[4];
1172 u8 appdata[4];
1173 struct pn533_type_b_prot_info prot_info;
1174} __packed;
1175
1176#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1177
1178struct pn533_target_type_b {
1179 struct pn533_type_b_sens_res sensb_res;
1180 u8 attrib_res_len;
1181 u8 attrib_res[];
1182} __packed;
1183
1184static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1185 int target_data_len)
1186{
1187 if (target_data_len < sizeof(struct pn533_target_type_b))
1188 return false;
1189
1190 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1191 return false;
1192
1193 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1194 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1195 return false;
1196
1197 return true;
1198}
1199
1200static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1201 int tgt_data_len)
1202{
1203 struct pn533_target_type_b *tgt_type_b;
1204
1205 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1206
1207 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1208 return -EPROTO;
1209
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001210 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001211
1212 return 0;
1213}
1214
1215struct pn533_poll_response {
1216 u8 nbtg;
1217 u8 tg;
1218 u8 target_data[];
1219} __packed;
1220
1221static int pn533_target_found(struct pn533 *dev,
1222 struct pn533_poll_response *resp, int resp_len)
1223{
1224 int target_data_len;
1225 struct nfc_target nfc_tgt;
1226 int rc;
1227
1228 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1229 dev->poll_mod_curr);
1230
1231 if (resp->tg != 1)
1232 return -EPROTO;
1233
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001234 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1235
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1237
1238 switch (dev->poll_mod_curr) {
1239 case PN533_POLL_MOD_106KBPS_A:
1240 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1241 target_data_len);
1242 break;
1243 case PN533_POLL_MOD_212KBPS_FELICA:
1244 case PN533_POLL_MOD_424KBPS_FELICA:
1245 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1246 target_data_len);
1247 break;
1248 case PN533_POLL_MOD_106KBPS_JEWEL:
1249 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1250 target_data_len);
1251 break;
1252 case PN533_POLL_MOD_847KBPS_B:
1253 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1254 target_data_len);
1255 break;
1256 default:
1257 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1258 " modulation");
1259 return -EPROTO;
1260 }
1261
1262 if (rc)
1263 return rc;
1264
1265 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1266 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1267 " have the desired protocol");
1268 return -EAGAIN;
1269 }
1270
1271 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1272 "0x%x", nfc_tgt.supported_protocols);
1273
1274 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1275
1276 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1277
1278 return 0;
1279}
1280
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001281static inline void pn533_poll_next_mod(struct pn533 *dev)
1282{
1283 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1284}
1285
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001286static void pn533_poll_reset_mod_list(struct pn533 *dev)
1287{
1288 dev->poll_mod_count = 0;
1289}
1290
1291static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1292{
1293 dev->poll_mod_active[dev->poll_mod_count] =
1294 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1295 dev->poll_mod_count++;
1296}
1297
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001298static void pn533_poll_create_mod_list(struct pn533 *dev,
1299 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001300{
1301 pn533_poll_reset_mod_list(dev);
1302
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001303 if (im_protocols & NFC_PROTO_MIFARE_MASK
1304 || im_protocols & NFC_PROTO_ISO14443_MASK
1305 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1307
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001308 if (im_protocols & NFC_PROTO_FELICA_MASK
1309 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1311 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1312 }
1313
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001314 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001315 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1316
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001317 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001319
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001320 if (tm_protocols)
1321 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001322}
1323
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001324static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325{
1326 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327 int rc;
1328
1329 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1330
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331 resp = (struct pn533_poll_response *) params;
1332 if (resp->nbtg) {
1333 rc = pn533_target_found(dev, resp, params_len);
1334
1335 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001336 if (rc == 0) {
1337 pn533_poll_reset_mod_list(dev);
1338 return 0;
1339 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001340 }
1341
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001342 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001343}
1344
Samuel Ortizad3823c2012-05-30 23:54:55 +02001345static int pn533_init_target_frame(struct pn533_frame *frame,
1346 u8 *gb, size_t gb_len)
1347{
1348 struct pn533_cmd_init_target *cmd;
1349 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001350 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1351 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1352 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1353 0xff, 0xff}; /* System code */
1354 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1355 0x0, 0x0, 0x0,
1356 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001357
1358 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1359 cmd = kzalloc(cmd_len, GFP_KERNEL);
1360 if (cmd == NULL)
1361 return -ENOMEM;
1362
1363 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1364
1365 /* DEP support only */
1366 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001367
1368 /* Felica params */
1369 memcpy(cmd->felica, felica_params, 18);
1370 get_random_bytes(cmd->felica + 2, 6);
1371
1372 /* NFCID3 */
1373 memset(cmd->nfcid3, 0, 10);
1374 memcpy(cmd->nfcid3, cmd->felica, 8);
1375
1376 /* MIFARE params */
1377 memcpy(cmd->mifare, mifare_params, 6);
1378
1379 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001380 cmd->gb_len = gb_len;
1381 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001382
Samuel Ortizad3823c2012-05-30 23:54:55 +02001383 /* Len Tk */
1384 cmd->gb[gb_len] = 0;
1385
1386 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001387
Samuel Ortizad3823c2012-05-30 23:54:55 +02001388 frame->datalen += cmd_len;
1389
1390 pn533_tx_frame_finish(frame);
1391
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001392 kfree(cmd);
1393
Samuel Ortizad3823c2012-05-30 23:54:55 +02001394 return 0;
1395}
1396
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001397#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001398#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1399static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1400 u8 *params, int params_len)
1401{
1402 struct sk_buff *skb_resp = arg;
1403 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1404
1405 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1406
1407 if (params_len < 0) {
1408 nfc_dev_err(&dev->interface->dev,
1409 "Error %d when starting as a target",
1410 params_len);
1411
1412 return params_len;
1413 }
1414
1415 if (params_len > 0 && params[0] != 0) {
1416 nfc_tm_deactivated(dev->nfc_dev);
1417
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001418 dev->tgt_mode = 0;
1419
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001420 kfree_skb(skb_resp);
1421 return 0;
1422 }
1423
1424 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001425 skb_pull(skb_resp,
1426 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1427 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001428
1429 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1430}
1431
1432static void pn533_wq_tg_get_data(struct work_struct *work)
1433{
1434 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1435 struct pn533_frame *in_frame;
1436 struct sk_buff *skb_resp;
1437 size_t skb_resp_len;
1438
1439 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1440
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001441 skb_resp_len = PN533_FRAME_HEADER_LEN +
1442 PN533_CMD_DATAEXCH_HEAD_LEN +
1443 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1444 PN533_FRAME_TAIL_LEN;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001445
1446 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1447 if (!skb_resp)
1448 return;
1449
1450 in_frame = (struct pn533_frame *)skb_resp->data;
1451
1452 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1453 pn533_tx_frame_finish(dev->out_frame);
1454
1455 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1456 skb_resp_len,
1457 pn533_tm_get_data_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001458 skb_resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001459
1460 return;
1461}
1462
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001463#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001464static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001465{
1466 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001467 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1468 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001469 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001470
1471 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1472
1473 if (params_len < 0) {
1474 nfc_dev_err(&dev->interface->dev,
1475 "Error %d when starting as a target",
1476 params_len);
1477
1478 return params_len;
1479 }
1480
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001481 if (params_len < ATR_REQ_GB_OFFSET + 1)
1482 return -EINVAL;
1483
Samuel Ortizad3823c2012-05-30 23:54:55 +02001484 resp = (struct pn533_cmd_init_target_response *) params;
1485
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001486 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1487 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001488
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001489 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1490 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1491 comm_mode = NFC_COMM_ACTIVE;
1492
1493 /* Again, only DEP */
1494 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1495 return -EOPNOTSUPP;
1496
1497 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1498 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1499
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001500 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1501 comm_mode, gb, gb_len);
1502 if (rc < 0) {
1503 nfc_dev_err(&dev->interface->dev,
1504 "Error when signaling target activation");
1505 return rc;
1506 }
1507
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001508 dev->tgt_mode = 1;
1509
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001510 queue_work(dev->wq, &dev->tg_work);
1511
1512 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001513}
1514
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001515static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001516{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001517 struct pn533 *dev = (struct pn533 *) data;
1518
1519 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1520
1521 /* An ack will cancel the last issued command (poll) */
1522 pn533_send_ack(dev, GFP_ATOMIC);
1523
1524 dev->cancel_listen = 1;
1525
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001526 pn533_poll_next_mod(dev);
1527
1528 queue_work(dev->wq, &dev->poll_work);
1529}
1530
1531static int pn533_poll_complete(struct pn533 *dev, void *arg,
1532 u8 *params, int params_len)
1533{
1534 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001535 int rc;
1536
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001537 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1538
1539 if (params_len == -ENOENT) {
1540 if (dev->poll_mod_count != 0)
1541 return 0;
1542
1543 nfc_dev_err(&dev->interface->dev,
1544 "Polling operation has been stopped");
1545
1546 goto stop_poll;
1547 }
1548
1549 if (params_len < 0) {
1550 nfc_dev_err(&dev->interface->dev,
1551 "Error %d when running poll", params_len);
1552
1553 goto stop_poll;
1554 }
1555
1556 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1557
1558 if (cur_mod->len == 0) {
1559 del_timer(&dev->listen_timer);
1560
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001561 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001563 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001564 if (!rc)
1565 return rc;
1566 }
1567
1568 pn533_poll_next_mod(dev);
1569
1570 queue_work(dev->wq, &dev->poll_work);
1571
1572 return 0;
1573
1574stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001575 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001576 dev->poll_protocols = 0;
1577 return 0;
1578}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001579
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001580static void pn533_build_poll_frame(struct pn533 *dev,
1581 struct pn533_frame *frame,
1582 struct pn533_poll_modulations *mod)
1583{
1584 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001585
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001586 if (mod->len == 0) {
1587 /* Listen mode */
1588 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1589 } else {
1590 /* Polling mode */
1591 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1592
1593 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1594 frame->datalen += mod->len;
1595
1596 pn533_tx_frame_finish(frame);
1597 }
1598}
1599
1600static int pn533_send_poll_frame(struct pn533 *dev)
1601{
1602 struct pn533_poll_modulations *cur_mod;
1603 int rc;
1604
1605 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1606
1607 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001608
1609 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001610 PN533_NORMAL_FRAME_MAX_LEN,
1611 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001612 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001613 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001614 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001615
1616 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001617}
1618
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001619static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001620{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1622 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001623 int rc;
1624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1626
1627 nfc_dev_dbg(&dev->interface->dev,
1628 "%s cancel_listen %d modulation len %d",
1629 __func__, dev->cancel_listen, cur_mod->len);
1630
1631 if (dev->cancel_listen == 1) {
1632 dev->cancel_listen = 0;
1633 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001634 }
1635
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001636 rc = pn533_send_poll_frame(dev);
1637 if (rc)
1638 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001639
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001640 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1641 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001642
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001644}
1645
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001646static int pn533_start_poll(struct nfc_dev *nfc_dev,
1647 u32 im_protocols, u32 tm_protocols)
1648{
1649 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1650
1651 nfc_dev_dbg(&dev->interface->dev,
1652 "%s: im protocols 0x%x tm protocols 0x%x",
1653 __func__, im_protocols, tm_protocols);
1654
1655 if (dev->tgt_active_prot) {
1656 nfc_dev_err(&dev->interface->dev,
1657 "Cannot poll with a target already activated");
1658 return -EBUSY;
1659 }
1660
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001661 if (dev->tgt_mode) {
1662 nfc_dev_err(&dev->interface->dev,
1663 "Cannot poll while already being activated");
1664 return -EBUSY;
1665 }
1666
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 if (tm_protocols) {
1668 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1669 if (dev->gb == NULL)
1670 tm_protocols = 0;
1671 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001672
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001673 dev->poll_mod_curr = 0;
1674 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1675 dev->poll_protocols = im_protocols;
1676 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001677
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001678 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001679}
1680
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001681static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1682{
1683 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1684
1685 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1686
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001687 del_timer(&dev->listen_timer);
1688
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001689 if (!dev->poll_mod_count) {
1690 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1691 " running");
1692 return;
1693 }
1694
1695 /* An ack will cancel the last issued command (poll) */
1696 pn533_send_ack(dev, GFP_KERNEL);
1697
1698 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1699 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001700
1701 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001702}
1703
1704static int pn533_activate_target_nfcdep(struct pn533 *dev)
1705{
1706 struct pn533_cmd_activate_param param;
1707 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001708 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001709 int rc;
1710
1711 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1712
1713 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1714
1715 param.tg = 1;
1716 param.next = 0;
1717 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1718 sizeof(struct pn533_cmd_activate_param));
1719 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1720
1721 pn533_tx_frame_finish(dev->out_frame);
1722
1723 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001724 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001725 if (rc)
1726 return rc;
1727
1728 resp = (struct pn533_cmd_activate_response *)
1729 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1730 rc = resp->status & PN533_CMD_RET_MASK;
1731 if (rc != PN533_CMD_RET_SUCCESS)
1732 return -EIO;
1733
Samuel Ortiz541d9202011-12-14 16:43:10 +01001734 /* ATR_RES general bytes are located at offset 16 */
1735 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1736 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1737
1738 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001739}
1740
Eric Lapuyade90099432012-05-07 12:31:13 +02001741static int pn533_activate_target(struct nfc_dev *nfc_dev,
1742 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001743{
1744 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1745 int rc;
1746
1747 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1748 protocol);
1749
1750 if (dev->poll_mod_count) {
1751 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1752 " polling");
1753 return -EBUSY;
1754 }
1755
1756 if (dev->tgt_active_prot) {
1757 nfc_dev_err(&dev->interface->dev, "There is already an active"
1758 " target");
1759 return -EBUSY;
1760 }
1761
1762 if (!dev->tgt_available_prots) {
1763 nfc_dev_err(&dev->interface->dev, "There is no available target"
1764 " to activate");
1765 return -EINVAL;
1766 }
1767
1768 if (!(dev->tgt_available_prots & (1 << protocol))) {
1769 nfc_dev_err(&dev->interface->dev, "The target does not support"
1770 " the requested protocol %u", protocol);
1771 return -EINVAL;
1772 }
1773
1774 if (protocol == NFC_PROTO_NFC_DEP) {
1775 rc = pn533_activate_target_nfcdep(dev);
1776 if (rc) {
1777 nfc_dev_err(&dev->interface->dev, "Error %d when"
1778 " activating target with"
1779 " NFC_DEP protocol", rc);
1780 return rc;
1781 }
1782 }
1783
1784 dev->tgt_active_prot = protocol;
1785 dev->tgt_available_prots = 0;
1786
1787 return 0;
1788}
1789
Eric Lapuyade90099432012-05-07 12:31:13 +02001790static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1791 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001792{
1793 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1794 u8 tg;
1795 u8 status;
1796 int rc;
1797
1798 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1799
1800 if (!dev->tgt_active_prot) {
1801 nfc_dev_err(&dev->interface->dev, "There is no active target");
1802 return;
1803 }
1804
1805 dev->tgt_active_prot = 0;
1806
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001807 skb_queue_purge(&dev->resp_q);
1808
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001809 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1810
1811 tg = 1;
1812 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1813 dev->out_frame->datalen += sizeof(u8);
1814
1815 pn533_tx_frame_finish(dev->out_frame);
1816
1817 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001818 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001819 if (rc) {
1820 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1821 " command to the controller");
1822 return;
1823 }
1824
1825 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1826 rc = status & PN533_CMD_RET_MASK;
1827 if (rc != PN533_CMD_RET_SUCCESS)
1828 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1829 " the target", rc);
1830
1831 return;
1832}
1833
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001834
1835static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1836 u8 *params, int params_len)
1837{
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001838 struct pn533_cmd_jump_dep_response *resp;
1839 struct nfc_target nfc_target;
1840 u8 target_gt_len;
1841 int rc;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001842 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1843 u8 active = cmd->active;
1844
1845 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001846
1847 if (params_len == -ENOENT) {
1848 nfc_dev_dbg(&dev->interface->dev, "");
1849 return 0;
1850 }
1851
1852 if (params_len < 0) {
1853 nfc_dev_err(&dev->interface->dev,
1854 "Error %d when bringing DEP link up",
1855 params_len);
1856 return 0;
1857 }
1858
1859 if (dev->tgt_available_prots &&
1860 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1861 nfc_dev_err(&dev->interface->dev,
1862 "The target does not support DEP");
1863 return -EINVAL;
1864 }
1865
1866 resp = (struct pn533_cmd_jump_dep_response *) params;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867 rc = resp->status & PN533_CMD_RET_MASK;
1868 if (rc != PN533_CMD_RET_SUCCESS) {
1869 nfc_dev_err(&dev->interface->dev,
1870 "Bringing DEP link up failed %d", rc);
1871 return 0;
1872 }
1873
1874 if (!dev->tgt_available_prots) {
1875 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1876
1877 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001878 nfc_target.nfcid1_len = 10;
1879 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001880 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1881 if (rc)
1882 return 0;
1883
1884 dev->tgt_available_prots = 0;
1885 }
1886
1887 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1888
1889 /* ATR_RES general bytes are located at offset 17 */
1890 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1891 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1892 resp->gt, target_gt_len);
1893 if (rc == 0)
1894 rc = nfc_dep_link_is_up(dev->nfc_dev,
1895 dev->nfc_dev->targets[0].idx,
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001896 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001897
1898 return 0;
1899}
1900
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001901static int pn533_mod_to_baud(struct pn533 *dev)
1902{
1903 switch (dev->poll_mod_curr) {
1904 case PN533_POLL_MOD_106KBPS_A:
1905 return 0;
1906 case PN533_POLL_MOD_212KBPS_FELICA:
1907 return 1;
1908 case PN533_POLL_MOD_424KBPS_FELICA:
1909 return 2;
1910 default:
1911 return -EINVAL;
1912 }
1913}
1914
Samuel Ortizd7f33452012-05-29 21:45:21 +02001915#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001916static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001917 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001918{
1919 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1920 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001921 u8 cmd_len, *data_ptr;
1922 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001923 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001924
1925 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1926
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001927 if (dev->poll_mod_count) {
1928 nfc_dev_err(&dev->interface->dev,
1929 "Cannot bring the DEP link up while polling");
1930 return -EBUSY;
1931 }
1932
1933 if (dev->tgt_active_prot) {
1934 nfc_dev_err(&dev->interface->dev,
1935 "There is already an active target");
1936 return -EBUSY;
1937 }
1938
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001939 baud = pn533_mod_to_baud(dev);
1940 if (baud < 0) {
1941 nfc_dev_err(&dev->interface->dev,
1942 "Invalid curr modulation %d", dev->poll_mod_curr);
1943 return baud;
1944 }
1945
Samuel Ortiz47807d32012-03-05 01:03:50 +01001946 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001947 if (comm_mode == NFC_COMM_PASSIVE)
1948 cmd_len += PASSIVE_DATA_LEN;
1949
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001950 cmd = kzalloc(cmd_len, GFP_KERNEL);
1951 if (cmd == NULL)
1952 return -ENOMEM;
1953
1954 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1955
1956 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001957 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001958 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001959 data_ptr = cmd->data;
1960 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
1961 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
1962 cmd->next |= 1;
1963 data_ptr += PASSIVE_DATA_LEN;
1964 }
1965
Samuel Ortiz47807d32012-03-05 01:03:50 +01001966 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02001967 cmd->next |= 4; /* We have some Gi */
1968 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001969 } else {
1970 cmd->next = 0;
1971 }
1972
1973 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1974 dev->out_frame->datalen += cmd_len;
1975
1976 pn533_tx_frame_finish(dev->out_frame);
1977
1978 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001979 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001980 pn533_in_dep_link_up_complete, cmd);
Szymon Janc770f7502012-10-29 14:04:43 +01001981 if (rc < 0)
1982 kfree(cmd);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001983
1984 return rc;
1985}
1986
1987static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1988{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001989 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1990
1991 pn533_poll_reset_mod_list(dev);
1992
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001993 if (dev->tgt_mode || dev->tgt_active_prot) {
1994 pn533_send_ack(dev, GFP_KERNEL);
1995 usb_kill_urb(dev->in_urb);
1996 }
1997
1998 dev->tgt_active_prot = 0;
1999 dev->tgt_mode = 0;
2000
2001 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002002
2003 return 0;
2004}
2005
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002006static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2007 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002008{
2009 int payload_len = skb->len;
2010 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002011 u8 tg;
2012
2013 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2014 payload_len);
2015
2016 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2017 /* TODO: Implement support to multi-part data exchange */
2018 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2019 " max allowed: %d",
2020 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2021 return -ENOSYS;
2022 }
2023
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002024 skb_push(skb, PN533_FRAME_HEADER_LEN);
2025
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002026 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002027 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002028 case PN533_DEVICE_PASORI:
2029 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002030 out_frame = (struct pn533_frame *) skb->data;
2031 pn533_tx_frame_init(out_frame,
2032 PN533_CMD_IN_COMM_THRU);
2033
2034 break;
2035 }
2036
2037 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002038 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2039 out_frame = (struct pn533_frame *) skb->data;
2040 pn533_tx_frame_init(out_frame,
2041 PN533_CMD_IN_DATA_EXCHANGE);
2042 tg = 1;
2043 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2044 &tg, sizeof(u8));
2045 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002046
2047 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002048 }
2049
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002050 } else {
2051 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2052 out_frame = (struct pn533_frame *) skb->data;
2053 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2054 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002055
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002056
2057 /* The data is already in the out_frame, just update the datalen */
2058 out_frame->datalen += payload_len;
2059
2060 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002061 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002062
2063 return 0;
2064}
2065
2066struct pn533_data_exchange_arg {
2067 struct sk_buff *skb_resp;
2068 struct sk_buff *skb_out;
2069 data_exchange_cb_t cb;
2070 void *cb_context;
2071};
2072
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002073static struct sk_buff *pn533_build_response(struct pn533 *dev)
2074{
2075 struct sk_buff *skb, *tmp, *t;
2076 unsigned int skb_len = 0, tmp_len = 0;
2077
2078 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2079
2080 if (skb_queue_empty(&dev->resp_q))
2081 return NULL;
2082
2083 if (skb_queue_len(&dev->resp_q) == 1) {
2084 skb = skb_dequeue(&dev->resp_q);
2085 goto out;
2086 }
2087
2088 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2089 skb_len += tmp->len;
2090
2091 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2092 __func__, skb_len);
2093
2094 skb = alloc_skb(skb_len, GFP_KERNEL);
2095 if (skb == NULL)
2096 goto out;
2097
2098 skb_put(skb, skb_len);
2099
2100 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2101 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2102 tmp_len += tmp->len;
2103 }
2104
2105out:
2106 skb_queue_purge(&dev->resp_q);
2107
2108 return skb;
2109}
2110
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2112 u8 *params, int params_len)
2113{
2114 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002115 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002116 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2117 int err = 0;
2118 u8 status;
2119 u8 cmd_ret;
2120
2121 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2122
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002123 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002124
2125 if (params_len < 0) { /* error */
2126 err = params_len;
2127 goto error;
2128 }
2129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130 status = params[0];
2131
2132 cmd_ret = status & PN533_CMD_RET_MASK;
2133 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2134 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2135 " exchanging data", cmd_ret);
2136 err = -EIO;
2137 goto error;
2138 }
2139
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002140 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002141 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002142 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002143 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002144 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002145
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002146 if (status & PN533_CMD_MI_MASK) {
2147 queue_work(dev->wq, &dev->mi_work);
2148 return -EINPROGRESS;
2149 }
2150
2151 skb = pn533_build_response(dev);
2152 if (skb == NULL)
2153 goto error;
2154
2155 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002156 kfree(arg);
2157 return 0;
2158
2159error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002160 skb_queue_purge(&dev->resp_q);
2161 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002162 arg->cb(arg->cb_context, NULL, err);
2163 kfree(arg);
2164 return 0;
2165}
2166
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002167static int pn533_transceive(struct nfc_dev *nfc_dev,
2168 struct nfc_target *target, struct sk_buff *skb,
2169 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002170{
2171 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2172 struct pn533_frame *out_frame, *in_frame;
2173 struct pn533_data_exchange_arg *arg;
2174 struct sk_buff *skb_resp;
2175 int skb_resp_len;
2176 int rc;
2177
2178 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2179
2180 if (!dev->tgt_active_prot) {
2181 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2182 " there is no active target");
2183 rc = -EINVAL;
2184 goto error;
2185 }
2186
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002187 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002188 if (rc)
2189 goto error;
2190
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002191 skb_resp_len = PN533_FRAME_HEADER_LEN +
2192 PN533_CMD_DATAEXCH_HEAD_LEN +
2193 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2194 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002195
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002196 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002197 if (!skb_resp) {
2198 rc = -ENOMEM;
2199 goto error;
2200 }
2201
2202 in_frame = (struct pn533_frame *) skb_resp->data;
2203 out_frame = (struct pn533_frame *) skb->data;
2204
2205 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2206 if (!arg) {
2207 rc = -ENOMEM;
2208 goto free_skb_resp;
2209 }
2210
2211 arg->skb_resp = skb_resp;
2212 arg->skb_out = skb;
2213 arg->cb = cb;
2214 arg->cb_context = cb_context;
2215
2216 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002217 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002218 if (rc) {
2219 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2220 " perform data_exchange", rc);
2221 goto free_arg;
2222 }
2223
2224 return 0;
2225
2226free_arg:
2227 kfree(arg);
2228free_skb_resp:
2229 kfree_skb(skb_resp);
2230error:
2231 kfree_skb(skb);
2232 return rc;
2233}
2234
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002235static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2236 u8 *params, int params_len)
2237{
Thierry Escande5b412fd2012-11-15 18:24:28 +01002238 struct sk_buff *skb_out = arg;
2239
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002240 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2241
Thierry Escande5b412fd2012-11-15 18:24:28 +01002242 dev_kfree_skb(skb_out);
2243
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002244 if (params_len < 0) {
2245 nfc_dev_err(&dev->interface->dev,
2246 "Error %d when sending data",
2247 params_len);
2248
2249 return params_len;
2250 }
2251
2252 if (params_len > 0 && params[0] != 0) {
2253 nfc_tm_deactivated(dev->nfc_dev);
2254
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002255 dev->tgt_mode = 0;
2256
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002257 return 0;
2258 }
2259
2260 queue_work(dev->wq, &dev->tg_work);
2261
2262 return 0;
2263}
2264
2265static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2266{
2267 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2268 struct pn533_frame *out_frame;
2269 int rc;
2270
2271 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2272
2273 rc = pn533_build_tx_frame(dev, skb, false);
2274 if (rc)
2275 goto error;
2276
2277 out_frame = (struct pn533_frame *) skb->data;
2278
2279 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002280 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002281 pn533_tm_send_complete, skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002282 if (rc) {
2283 nfc_dev_err(&dev->interface->dev,
2284 "Error %d when trying to send data", rc);
2285 goto error;
2286 }
2287
2288 return 0;
2289
2290error:
2291 kfree_skb(skb);
2292
2293 return rc;
2294}
2295
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002296static void pn533_wq_mi_recv(struct work_struct *work)
2297{
2298 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2299 struct sk_buff *skb_cmd;
2300 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2301 struct pn533_frame *out_frame, *in_frame;
2302 struct sk_buff *skb_resp;
2303 int skb_resp_len;
2304 int rc;
2305
2306 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2307
2308 /* This is a zero payload size skb */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002309 skb_cmd = alloc_skb(PN533_FRAME_HEADER_LEN +
2310 PN533_CMD_DATAEXCH_HEAD_LEN +
2311 PN533_FRAME_TAIL_LEN,
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002312 GFP_KERNEL);
2313 if (skb_cmd == NULL)
2314 goto error_cmd;
2315
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002316 skb_reserve(skb_cmd,
2317 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002318
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002319 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002320 if (rc)
2321 goto error_frame;
2322
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002323 skb_resp_len = PN533_FRAME_HEADER_LEN +
2324 PN533_CMD_DATAEXCH_HEAD_LEN +
2325 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2326 PN533_FRAME_TAIL_LEN;
2327
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002328 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2329 if (!skb_resp) {
2330 rc = -ENOMEM;
2331 goto error_frame;
2332 }
2333
2334 in_frame = (struct pn533_frame *) skb_resp->data;
2335 out_frame = (struct pn533_frame *) skb_cmd->data;
2336
2337 arg->skb_resp = skb_resp;
2338 arg->skb_out = skb_cmd;
2339
2340 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2341 skb_resp_len,
2342 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002343 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002344 if (!rc)
2345 return;
2346
2347 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2348 " perform data_exchange", rc);
2349
2350 kfree_skb(skb_resp);
2351
2352error_frame:
2353 kfree_skb(skb_cmd);
2354
2355error_cmd:
2356 pn533_send_ack(dev, GFP_KERNEL);
2357
2358 kfree(arg);
2359
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002360 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002361}
2362
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002363static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2364 u8 cfgdata_len)
2365{
2366 int rc;
2367 u8 *params;
2368
2369 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2370
2371 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2372
2373 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2374 params[0] = cfgitem;
2375 memcpy(&params[1], cfgdata, cfgdata_len);
2376 dev->out_frame->datalen += (1 + cfgdata_len);
2377
2378 pn533_tx_frame_finish(dev->out_frame);
2379
2380 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002381 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002382
2383 return rc;
2384}
2385
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002386static int pn533_fw_reset(struct pn533 *dev)
2387{
2388 int rc;
2389 u8 *params;
2390
2391 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2392
2393 pn533_tx_frame_init(dev->out_frame, 0x18);
2394
2395 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2396 params[0] = 0x1;
2397 dev->out_frame->datalen += 1;
2398
2399 pn533_tx_frame_finish(dev->out_frame);
2400
2401 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002402 PN533_NORMAL_FRAME_MAX_LEN);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002403
2404 return rc;
2405}
2406
2407static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002408 .dev_up = NULL,
2409 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002410 .dep_link_up = pn533_dep_link_up,
2411 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002412 .start_poll = pn533_start_poll,
2413 .stop_poll = pn533_stop_poll,
2414 .activate_target = pn533_activate_target,
2415 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002416 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002417 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418};
2419
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002420static int pn533_setup(struct pn533 *dev)
2421{
2422 struct pn533_config_max_retries max_retries;
2423 struct pn533_config_timing timing;
2424 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2425 int rc;
2426
2427 switch (dev->device_type) {
2428 case PN533_DEVICE_STD:
2429 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2430 max_retries.mx_rty_psl = 2;
2431 max_retries.mx_rty_passive_act =
2432 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2433
2434 timing.rfu = PN533_CONFIG_TIMING_102;
2435 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2436 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2437
2438 break;
2439
2440 case PN533_DEVICE_PASORI:
2441 max_retries.mx_rty_atr = 0x2;
2442 max_retries.mx_rty_psl = 0x1;
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_102;
2448 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2449
2450 break;
2451
2452 default:
2453 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2454 dev->device_type);
2455 return -EINVAL;
2456 }
2457
2458 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2459 (u8 *)&max_retries, sizeof(max_retries));
2460 if (rc) {
2461 nfc_dev_err(&dev->interface->dev,
2462 "Error on setting MAX_RETRIES config");
2463 return rc;
2464 }
2465
2466
2467 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2468 (u8 *)&timing, sizeof(timing));
2469 if (rc) {
2470 nfc_dev_err(&dev->interface->dev,
2471 "Error on setting RF timings");
2472 return rc;
2473 }
2474
2475 switch (dev->device_type) {
2476 case PN533_DEVICE_STD:
2477 break;
2478
2479 case PN533_DEVICE_PASORI:
2480 pn533_fw_reset(dev);
2481
2482 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2483 pasori_cfg, 3);
2484 if (rc) {
2485 nfc_dev_err(&dev->interface->dev,
2486 "Error while settings PASORI config");
2487 return rc;
2488 }
2489
2490 pn533_fw_reset(dev);
2491
2492 break;
2493 }
2494
2495 return 0;
2496}
2497
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002498static int pn533_probe(struct usb_interface *interface,
2499 const struct usb_device_id *id)
2500{
2501 struct pn533_fw_version *fw_ver;
2502 struct pn533 *dev;
2503 struct usb_host_interface *iface_desc;
2504 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505 int in_endpoint = 0;
2506 int out_endpoint = 0;
2507 int rc = -ENOMEM;
2508 int i;
2509 u32 protocols;
2510
2511 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2512 if (!dev)
2513 return -ENOMEM;
2514
2515 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2516 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002517 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002518
2519 iface_desc = interface->cur_altsetting;
2520 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2521 endpoint = &iface_desc->endpoint[i].desc;
2522
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002523 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002524 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002525
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002526 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002527 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002528 }
2529
2530 if (!in_endpoint || !out_endpoint) {
2531 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2532 " bulk-out endpoint");
2533 rc = -ENODEV;
2534 goto error;
2535 }
2536
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002537 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002539 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002540 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2541
2542 if (!dev->in_frame || !dev->out_frame ||
2543 !dev->in_urb || !dev->out_urb)
2544 goto error;
2545
2546 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2547 usb_rcvbulkpipe(dev->udev, in_endpoint),
2548 NULL, 0, NULL, dev);
2549 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2550 usb_sndbulkpipe(dev->udev, out_endpoint),
2551 NULL, 0,
2552 pn533_send_complete, dev);
2553
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002554 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2555 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002556 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002557 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002558 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002559 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002560 if (dev->wq == NULL)
2561 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002562
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002563 init_timer(&dev->listen_timer);
2564 dev->listen_timer.data = (unsigned long) dev;
2565 dev->listen_timer.function = pn533_listen_mode_timer;
2566
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002567 skb_queue_head_init(&dev->resp_q);
2568
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002569 INIT_LIST_HEAD(&dev->cmd_queue);
2570
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002571 usb_set_intfdata(interface, dev);
2572
2573 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2574 pn533_tx_frame_finish(dev->out_frame);
2575
2576 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002577 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002578 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002579 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002580
2581 fw_ver = (struct pn533_fw_version *)
2582 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2583 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2584 " attached", fw_ver->ver, fw_ver->rev);
2585
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002586 dev->device_type = id->driver_info;
2587 switch (dev->device_type) {
2588 case PN533_DEVICE_STD:
2589 protocols = PN533_ALL_PROTOCOLS;
2590 break;
2591
2592 case PN533_DEVICE_PASORI:
2593 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2594 break;
2595
2596 default:
2597 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2598 dev->device_type);
2599 rc = -EINVAL;
2600 goto destroy_wq;
2601 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602
Samuel Ortize8753042011-08-19 15:47:11 +02002603 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002604 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002605 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002606 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002607 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002608 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609
2610 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2611 nfc_set_drvdata(dev->nfc_dev, dev);
2612
2613 rc = nfc_register_device(dev->nfc_dev);
2614 if (rc)
2615 goto free_nfc_dev;
2616
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002617 rc = pn533_setup(dev);
2618 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002619 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002620
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002621 return 0;
2622
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002623unregister_nfc_dev:
2624 nfc_unregister_device(dev->nfc_dev);
2625
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002626free_nfc_dev:
2627 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002628
Samuel Ortiz4849f852012-04-10 19:43:17 +02002629destroy_wq:
2630 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002631error:
2632 kfree(dev->in_frame);
2633 usb_free_urb(dev->in_urb);
2634 kfree(dev->out_frame);
2635 usb_free_urb(dev->out_urb);
2636 kfree(dev);
2637 return rc;
2638}
2639
2640static void pn533_disconnect(struct usb_interface *interface)
2641{
2642 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002643 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002644
2645 dev = usb_get_intfdata(interface);
2646 usb_set_intfdata(interface, NULL);
2647
2648 nfc_unregister_device(dev->nfc_dev);
2649 nfc_free_device(dev->nfc_dev);
2650
2651 usb_kill_urb(dev->in_urb);
2652 usb_kill_urb(dev->out_urb);
2653
Samuel Ortiz4849f852012-04-10 19:43:17 +02002654 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002656 skb_queue_purge(&dev->resp_q);
2657
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002658 del_timer(&dev->listen_timer);
2659
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002660 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2661 list_del(&cmd->queue);
2662 kfree(cmd);
2663 }
2664
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002665 kfree(dev->in_frame);
2666 usb_free_urb(dev->in_urb);
2667 kfree(dev->out_frame);
2668 usb_free_urb(dev->out_urb);
2669 kfree(dev);
2670
Dan Carpenter276556d2011-07-08 10:21:15 +03002671 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002672}
2673
2674static struct usb_driver pn533_driver = {
2675 .name = "pn533",
2676 .probe = pn533_probe,
2677 .disconnect = pn533_disconnect,
2678 .id_table = pn533_table,
2679};
2680
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002681module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002682
2683MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2684 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2685MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2686MODULE_VERSION(VERSION);
2687MODULE_LICENSE("GPL");