blob: 0e9bf9dfbbb543ee3d1099dc8cd43dd73c7498f8 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030031#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030032
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081/* frame definitions */
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
83 254 (DATA)
84 2 (DCS, postamble) */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010085#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020088
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010089/*
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
92 */
93#define PN533_FRAME_MAX_PAYLOAD_LEN 263
94
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030095#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010096 PN533_FRAME_TAIL_LEN)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030097#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
100
101/* start of frame */
102#define PN533_SOF 0x00FF
103
104/* frame identifier: in/out/error */
105#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106#define PN533_DIR_OUT 0xD4
107#define PN533_DIR_IN 0xD5
108
109/* PN533 Commands */
110#define PN533_FRAME_CMD(f) (f->data[1])
111#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
113
114#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115#define PN533_CMD_RF_CONFIGURATION 0x32
116#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200117#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300118#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119#define PN533_CMD_IN_ATR 0x50
120#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100121#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122
Samuel Ortizad3823c2012-05-30 23:54:55 +0200123#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200124#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200125#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100126#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300128#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
129
130/* PN533 Return codes */
131#define PN533_CMD_RET_MASK 0x3F
132#define PN533_CMD_MI_MASK 0x40
133#define PN533_CMD_RET_SUCCESS 0x00
134
135struct pn533;
136
137typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
139
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100140typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143/* structs for pn533 commands */
144
145/* PN533_CMD_GET_FIRMWARE_VERSION */
146struct pn533_fw_version {
147 u8 ic;
148 u8 ver;
149 u8 rev;
150 u8 support;
151};
152
153/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200154#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200156#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200158#define PN533_CONFIG_TIMING_102 0xb
159#define PN533_CONFIG_TIMING_204 0xc
160#define PN533_CONFIG_TIMING_409 0xd
161#define PN533_CONFIG_TIMING_819 0xe
162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300163#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
165
166struct pn533_config_max_retries {
167 u8 mx_rty_atr;
168 u8 mx_rty_psl;
169 u8 mx_rty_passive_act;
170} __packed;
171
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200172struct pn533_config_timing {
173 u8 rfu;
174 u8 atr_res_timeout;
175 u8 dep_timeout;
176} __packed;
177
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300178/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
179
180/* felica commands opcode */
181#define PN533_FELICA_OPC_SENSF_REQ 0
182#define PN533_FELICA_OPC_SENSF_RES 1
183/* felica SENSF_REQ parameters */
184#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
188
189/* type B initiator_data values */
190#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
193
194union pn533_cmd_poll_initdata {
195 struct {
196 u8 afi;
197 u8 polling_method;
198 } __packed type_b;
199 struct {
200 u8 opcode;
201 __be16 sc;
202 u8 rc;
203 u8 tsn;
204 } __packed felica;
205};
206
207/* Poll modulations */
208enum {
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200214 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300215
216 __PN533_POLL_MOD_AFTER_LAST,
217};
218#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
219
220struct pn533_poll_modulations {
221 struct {
222 u8 maxtg;
223 u8 brty;
224 union pn533_cmd_poll_initdata initiator_data;
225 } __packed data;
226 u8 len;
227};
228
229const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 0,
234 },
235 .len = 2,
236 },
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
238 .data = {
239 .maxtg = 1,
240 .brty = 1,
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
245 .tsn = 0,
246 },
247 },
248 .len = 7,
249 },
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
251 .data = {
252 .maxtg = 1,
253 .brty = 2,
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
258 .tsn = 0,
259 },
260 },
261 .len = 7,
262 },
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 4,
267 },
268 .len = 2,
269 },
270 [PN533_POLL_MOD_847KBPS_B] = {
271 .data = {
272 .maxtg = 1,
273 .brty = 8,
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
276 .polling_method =
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
278 },
279 },
280 .len = 3,
281 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200282 [PN533_LISTEN_MOD] = {
283 .len = 0,
284 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300285};
286
287/* PN533_CMD_IN_ATR */
288
289struct pn533_cmd_activate_param {
290 u8 tg;
291 u8 next;
292} __packed;
293
294struct pn533_cmd_activate_response {
295 u8 status;
296 u8 nfcid3t[10];
297 u8 didt;
298 u8 bst;
299 u8 brt;
300 u8 to;
301 u8 ppt;
302 /* optional */
303 u8 gt[];
304} __packed;
305
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100306/* PN533_CMD_IN_JUMP_FOR_DEP */
307struct pn533_cmd_jump_dep {
308 u8 active;
309 u8 baud;
310 u8 next;
Samuel Ortizd7f33452012-05-29 21:45:21 +0200311 u8 data[];
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100312} __packed;
313
314struct pn533_cmd_jump_dep_response {
315 u8 status;
316 u8 tg;
317 u8 nfcid3t[10];
318 u8 didt;
319 u8 bst;
320 u8 brt;
321 u8 to;
322 u8 ppt;
323 /* optional */
324 u8 gt[];
325} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300326
Samuel Ortizad3823c2012-05-30 23:54:55 +0200327
328/* PN533_TG_INIT_AS_TARGET */
329#define PN533_INIT_TARGET_PASSIVE 0x1
330#define PN533_INIT_TARGET_DEP 0x2
331
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200332#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
333#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
334#define PN533_INIT_TARGET_RESP_DEP 0x4
335
Samuel Ortizad3823c2012-05-30 23:54:55 +0200336struct pn533_cmd_init_target {
337 u8 mode;
338 u8 mifare[6];
339 u8 felica[18];
340 u8 nfcid3[10];
341 u8 gb_len;
342 u8 gb[];
343} __packed;
344
345struct pn533_cmd_init_target_response {
346 u8 mode;
347 u8 cmd[];
348} __packed;
349
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350struct pn533 {
351 struct usb_device *udev;
352 struct usb_interface *interface;
353 struct nfc_dev *nfc_dev;
354
355 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300356 struct pn533_frame *out_frame;
357
358 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300359 struct pn533_frame *in_frame;
360
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200361 struct sk_buff_head resp_q;
362
Samuel Ortiz4849f852012-04-10 19:43:17 +0200363 struct workqueue_struct *wq;
364 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200365 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200366 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200367 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200368 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200369 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200370 struct pn533_frame *wq_in_frame;
371 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200372 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300373
374 pn533_cmd_complete_t cmd_complete;
375 void *cmd_complete_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200376 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300377 u8 cmd;
378
379 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
380 u8 poll_mod_count;
381 u8 poll_mod_curr;
382 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200383 u32 listen_protocols;
384
385 u8 *gb;
386 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300387
388 u8 tgt_available_prots;
389 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200390 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200391
392 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200393
394 struct list_head cmd_queue;
395 u8 cmd_pending;
396};
397
398struct pn533_cmd {
399 struct list_head queue;
400 struct pn533_frame *out_frame;
401 struct pn533_frame *in_frame;
402 int in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100403 u8 cmd_code;
404 struct sk_buff *req;
405 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200406 pn533_cmd_complete_t cmd_complete;
407 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300408};
409
410struct pn533_frame {
411 u8 preamble;
412 __be16 start_frame;
413 u8 datalen;
414 u8 datalen_checksum;
415 u8 data[];
416} __packed;
417
418/* The rule: value + checksum = 0 */
419static inline u8 pn533_checksum(u8 value)
420{
421 return ~value + 1;
422}
423
424/* The rule: sum(data elements) + checksum = 0 */
425static u8 pn533_data_checksum(u8 *data, int datalen)
426{
427 u8 sum = 0;
428 int i;
429
430 for (i = 0; i < datalen; i++)
431 sum += data[i];
432
433 return pn533_checksum(sum);
434}
435
436/**
437 * pn533_tx_frame_ack - create a ack frame
438 * @frame: The frame to be set as ack
439 *
440 * Ack is different type of standard frame. As a standard frame, it has
441 * preamble and start_frame. However the checksum of this frame must fail,
442 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
443 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
444 * After datalen_checksum field, the postamble is placed.
445 */
446static void pn533_tx_frame_ack(struct pn533_frame *frame)
447{
448 frame->preamble = 0;
449 frame->start_frame = cpu_to_be16(PN533_SOF);
450 frame->datalen = 0;
451 frame->datalen_checksum = 0xFF;
452 /* data[0] is used as postamble */
453 frame->data[0] = 0;
454}
455
456static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
457{
458 frame->preamble = 0;
459 frame->start_frame = cpu_to_be16(PN533_SOF);
460 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
461 PN533_FRAME_CMD(frame) = cmd;
462 frame->datalen = 2;
463}
464
465static void pn533_tx_frame_finish(struct pn533_frame *frame)
466{
467 frame->datalen_checksum = pn533_checksum(frame->datalen);
468
469 PN533_FRAME_CHECKSUM(frame) =
470 pn533_data_checksum(frame->data, frame->datalen);
471
472 PN533_FRAME_POSTAMBLE(frame) = 0;
473}
474
475static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
476{
477 u8 checksum;
478
479 if (frame->start_frame != cpu_to_be16(PN533_SOF))
480 return false;
481
482 checksum = pn533_checksum(frame->datalen);
483 if (checksum != frame->datalen_checksum)
484 return false;
485
486 checksum = pn533_data_checksum(frame->data, frame->datalen);
487 if (checksum != PN533_FRAME_CHECKSUM(frame))
488 return false;
489
490 return true;
491}
492
493static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
494{
495 if (frame->start_frame != cpu_to_be16(PN533_SOF))
496 return false;
497
498 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
499 return false;
500
501 return true;
502}
503
504static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
505{
506 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
507}
508
Samuel Ortiz4849f852012-04-10 19:43:17 +0200509
510static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200512 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200513 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 int rc;
515
Samuel Ortiz4849f852012-04-10 19:43:17 +0200516 in_frame = dev->wq_in_frame;
517
518 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300519 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200520 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300521 else
522 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
523 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
524 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
525
526 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200527 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528}
529
530static void pn533_recv_response(struct urb *urb)
531{
532 struct pn533 *dev = urb->context;
533 struct pn533_frame *in_frame;
534
Samuel Ortiz4849f852012-04-10 19:43:17 +0200535 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300536
537 switch (urb->status) {
538 case 0:
539 /* success */
540 break;
541 case -ECONNRESET:
542 case -ENOENT:
543 case -ESHUTDOWN:
544 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
545 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200546 dev->wq_in_error = urb->status;
547 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 default:
549 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
550 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200551 dev->wq_in_error = urb->status;
552 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300553 }
554
555 in_frame = dev->in_urb->transfer_buffer;
556
557 if (!pn533_rx_frame_is_valid(in_frame)) {
558 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200559 dev->wq_in_error = -EIO;
560 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300561 }
562
563 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
564 nfc_dev_err(&dev->interface->dev, "The received frame is not "
565 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200566 dev->wq_in_error = -EIO;
567 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568 }
569
570 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200571 dev->wq_in_error = 0;
572 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300573
Samuel Ortiz4849f852012-04-10 19:43:17 +0200574sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200575 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300576}
577
578static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
579{
580 dev->in_urb->complete = pn533_recv_response;
581
582 return usb_submit_urb(dev->in_urb, flags);
583}
584
585static void pn533_recv_ack(struct urb *urb)
586{
587 struct pn533 *dev = urb->context;
588 struct pn533_frame *in_frame;
589 int rc;
590
591 switch (urb->status) {
592 case 0:
593 /* success */
594 break;
595 case -ECONNRESET:
596 case -ENOENT:
597 case -ESHUTDOWN:
598 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
599 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200600 dev->wq_in_error = urb->status;
601 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 default:
603 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
604 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200605 dev->wq_in_error = urb->status;
606 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300607 }
608
609 in_frame = dev->in_urb->transfer_buffer;
610
611 if (!pn533_rx_frame_is_ack(in_frame)) {
612 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200613 dev->wq_in_error = -EIO;
614 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300615 }
616
617 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
618
619 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
620 if (rc) {
621 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
622 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200623 dev->wq_in_error = rc;
624 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300625 }
626
627 return;
628
Samuel Ortiz4849f852012-04-10 19:43:17 +0200629sched_wq:
630 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200631 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300632}
633
634static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
635{
636 dev->in_urb->complete = pn533_recv_ack;
637
638 return usb_submit_urb(dev->in_urb, flags);
639}
640
641static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
642{
643 int rc;
644
645 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
646
647 pn533_tx_frame_ack(dev->out_frame);
648
649 dev->out_urb->transfer_buffer = dev->out_frame;
650 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
651 rc = usb_submit_urb(dev->out_urb, flags);
652
653 return rc;
654}
655
656static int __pn533_send_cmd_frame_async(struct pn533 *dev,
657 struct pn533_frame *out_frame,
658 struct pn533_frame *in_frame,
659 int in_frame_len,
660 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100661 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300662{
663 int rc;
664
665 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
666 PN533_FRAME_CMD(out_frame));
667
668 dev->cmd = PN533_FRAME_CMD(out_frame);
669 dev->cmd_complete = cmd_complete;
670 dev->cmd_complete_arg = arg;
671
672 dev->out_urb->transfer_buffer = out_frame;
673 dev->out_urb->transfer_buffer_length =
674 PN533_FRAME_SIZE(out_frame);
675
676 dev->in_urb->transfer_buffer = in_frame;
677 dev->in_urb->transfer_buffer_length = in_frame_len;
678
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100679 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300680 if (rc)
681 return rc;
682
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100683 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300684 if (rc)
685 goto error;
686
687 return 0;
688
689error:
690 usb_unlink_urb(dev->out_urb);
691 return rc;
692}
693
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100694static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
695{
696 struct pn533_frame *frame;
697 /* payload is already there, just update datalen */
698 int payload_len = skb->len;
699
700 skb_push(skb, PN533_FRAME_HEADER_LEN);
701 skb_put(skb, PN533_FRAME_TAIL_LEN);
702
703 frame = (struct pn533_frame *)skb->data;
704
705 pn533_tx_frame_init(frame, cmd_code);
706 frame->datalen += payload_len;
707 pn533_tx_frame_finish(frame);
708}
709
710struct pn533_send_async_complete_arg {
711 pn533_send_async_complete_t complete_cb;
712 void *complete_cb_context;
713 struct sk_buff *resp;
714 struct sk_buff *req;
715};
716
717static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
718 int params_len)
719{
720 struct pn533_send_async_complete_arg *arg = _arg;
721
722 struct sk_buff *req = arg->req;
723 struct sk_buff *resp = arg->resp;
724
725 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
726 int rc;
727
728 dev_kfree_skb(req);
729
730 if (params_len < 0) {
731 nfc_dev_err(&dev->interface->dev,
732 "Error %d when starting as a target",
733 params_len);
734
735 arg->complete_cb(dev, arg->complete_cb_context,
736 ERR_PTR(params_len));
737 rc = params_len;
738 dev_kfree_skb(resp);
739 goto out;
740 }
741
742 skb_put(resp, PN533_FRAME_SIZE(frame));
743 skb_pull(resp, PN533_FRAME_HEADER_LEN);
744 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
745
746 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
747
748out:
749 kfree(arg);
750 return rc;
751}
752
753static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
754 struct sk_buff *req, struct sk_buff *resp,
755 int resp_len,
756 pn533_send_async_complete_t complete_cb,
757 void *complete_cb_context)
758{
759 struct pn533_cmd *cmd;
760 struct pn533_send_async_complete_arg *arg;
761 int rc = 0;
762
763 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
764
765 arg = kzalloc(sizeof(arg), GFP_KERNEL);
766 if (!arg)
767 return -ENOMEM;
768
769 arg->complete_cb = complete_cb;
770 arg->complete_cb_context = complete_cb_context;
771 arg->resp = resp;
772 arg->req = req;
773
774 pn533_build_cmd_frame(cmd_code, req);
775
776 mutex_lock(&dev->cmd_lock);
777
778 if (!dev->cmd_pending) {
779 rc = __pn533_send_cmd_frame_async(dev,
780 (struct pn533_frame *)req->data,
781 (struct pn533_frame *)resp->data,
782 resp_len, pn533_send_async_complete,
783 arg);
784 if (rc)
785 goto error;
786
787 dev->cmd_pending = 1;
788 goto unlock;
789 }
790
791 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
792
793 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
794 if (!cmd) {
795 rc = -ENOMEM;
796 goto error;
797 }
798
799 INIT_LIST_HEAD(&cmd->queue);
800 cmd->cmd_code = cmd_code;
801 cmd->req = req;
802 cmd->resp = resp;
803 cmd->arg = arg;
804
805 list_add_tail(&cmd->queue, &dev->cmd_queue);
806
807 goto unlock;
808
809error:
810 kfree(arg);
811unlock:
812 mutex_unlock(&dev->cmd_lock);
813 return rc;
814}
815
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100816static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
817 struct sk_buff *req,
818 pn533_send_async_complete_t complete_cb,
819 void *complete_cb_context)
820{
821 struct sk_buff *resp;
822 int rc;
823 int resp_len = PN533_FRAME_HEADER_LEN +
824 PN533_FRAME_MAX_PAYLOAD_LEN +
825 PN533_FRAME_TAIL_LEN;
826
827 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
828
829 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
830 if (!resp)
831 return -ENOMEM;
832
833 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
834 complete_cb_context);
835 if (rc)
836 dev_kfree_skb(resp);
837
838 return rc;
839}
840
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100841static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
842 struct sk_buff *req,
843 pn533_send_async_complete_t complete_cb,
844 void *complete_cb_context)
845{
846 struct sk_buff *resp;
847 int rc;
848
849 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
850
851 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
852 if (!resp)
853 return -ENOMEM;
854
855 rc = __pn533_send_async(dev, cmd_code, req, resp,
856 PN533_NORMAL_FRAME_MAX_LEN,
857 complete_cb, complete_cb_context);
858 if (rc)
859 dev_kfree_skb(resp);
860
861 return rc;
862}
863
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200864static void pn533_wq_cmd(struct work_struct *work)
865{
866 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
867 struct pn533_cmd *cmd;
868
869 mutex_lock(&dev->cmd_lock);
870
871 if (list_empty(&dev->cmd_queue)) {
872 dev->cmd_pending = 0;
873 mutex_unlock(&dev->cmd_lock);
874 return;
875 }
876
877 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
878
Szymon Janc60ad07a2012-10-25 17:29:45 +0200879 list_del(&cmd->queue);
880
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200881 mutex_unlock(&dev->cmd_lock);
882
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100883 if (cmd->cmd_code != PN533_CMD_UNDEF)
884 __pn533_send_cmd_frame_async(dev,
885 (struct pn533_frame *)cmd->req->data,
886 (struct pn533_frame *)cmd->resp->data,
887 PN533_NORMAL_FRAME_MAX_LEN,
888 pn533_send_async_complete,
889 cmd->arg);
890 else
891 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
892 cmd->in_frame_len,
893 cmd->cmd_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200894
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200895 kfree(cmd);
896}
897
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300898static int pn533_send_cmd_frame_async(struct pn533 *dev,
899 struct pn533_frame *out_frame,
900 struct pn533_frame *in_frame,
901 int in_frame_len,
902 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100903 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300904{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200905 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200906 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300907
908 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
909
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200910 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300911
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200912 if (!dev->cmd_pending) {
913 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
914 in_frame_len, cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100915 arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200916 if (!rc)
917 dev->cmd_pending = 1;
918
Szymon Jancee5e8d82012-09-27 09:16:54 +0200919 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200920 }
921
922 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
923
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100924 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200925 if (!cmd) {
926 rc = -ENOMEM;
927 goto unlock;
928 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200929
930 INIT_LIST_HEAD(&cmd->queue);
931 cmd->out_frame = out_frame;
932 cmd->in_frame = in_frame;
933 cmd->in_frame_len = in_frame_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100934 cmd->cmd_code = PN533_CMD_UNDEF;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200935 cmd->cmd_complete = cmd_complete;
936 cmd->arg = arg;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200937
938 list_add_tail(&cmd->queue, &dev->cmd_queue);
939
Szymon Jancee5e8d82012-09-27 09:16:54 +0200940unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200941 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300942
Szymon Jancee5e8d82012-09-27 09:16:54 +0200943 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300944}
945
946struct pn533_sync_cmd_response {
947 int rc;
948 struct completion done;
949};
950
951static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
952 u8 *params, int params_len)
953{
954 struct pn533_sync_cmd_response *arg = _arg;
955
956 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
957
958 arg->rc = 0;
959
960 if (params_len < 0) /* error */
961 arg->rc = params_len;
962
963 complete(&arg->done);
964
965 return 0;
966}
967
968static int pn533_send_cmd_frame_sync(struct pn533 *dev,
969 struct pn533_frame *out_frame,
970 struct pn533_frame *in_frame,
971 int in_frame_len)
972{
973 int rc;
974 struct pn533_sync_cmd_response arg;
975
976 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
977
978 init_completion(&arg.done);
979
980 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100981 pn533_sync_cmd_complete, &arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300982 if (rc)
983 return rc;
984
985 wait_for_completion(&arg.done);
986
987 return arg.rc;
988}
989
990static void pn533_send_complete(struct urb *urb)
991{
992 struct pn533 *dev = urb->context;
993
994 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
995
996 switch (urb->status) {
997 case 0:
998 /* success */
999 break;
1000 case -ECONNRESET:
1001 case -ENOENT:
1002 case -ESHUTDOWN:
1003 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1004 " status: %d", urb->status);
1005 break;
1006 default:
1007 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1008 " %d", urb->status);
1009 }
1010}
1011
1012struct pn533_target_type_a {
1013 __be16 sens_res;
1014 u8 sel_res;
1015 u8 nfcid_len;
1016 u8 nfcid_data[];
1017} __packed;
1018
1019
1020#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1021#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1022#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1023
1024#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1025#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1026
1027#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1028#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1029
1030#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1031#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1032#define PN533_TYPE_A_SEL_PROT_DEP 2
1033#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1034
1035static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1036 int target_data_len)
1037{
1038 u8 ssd;
1039 u8 platconf;
1040
1041 if (target_data_len < sizeof(struct pn533_target_type_a))
1042 return false;
1043
1044 /* The lenght check of nfcid[] and ats[] are not being performed because
1045 the values are not being used */
1046
1047 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1048 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1049 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1050
1051 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1052 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1053 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1054 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1055 return false;
1056
1057 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1058 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1059 return false;
1060
1061 return true;
1062}
1063
1064static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1065 int tgt_data_len)
1066{
1067 struct pn533_target_type_a *tgt_type_a;
1068
1069 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1070
1071 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1072 return -EPROTO;
1073
1074 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1075 case PN533_TYPE_A_SEL_PROT_MIFARE:
1076 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1077 break;
1078 case PN533_TYPE_A_SEL_PROT_ISO14443:
1079 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1080 break;
1081 case PN533_TYPE_A_SEL_PROT_DEP:
1082 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1083 break;
1084 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1085 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1086 NFC_PROTO_NFC_DEP_MASK;
1087 break;
1088 }
1089
1090 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1091 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001092 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1093 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001094
1095 return 0;
1096}
1097
1098struct pn533_target_felica {
1099 u8 pol_res;
1100 u8 opcode;
1101 u8 nfcid2[8];
1102 u8 pad[8];
1103 /* optional */
1104 u8 syst_code[];
1105} __packed;
1106
1107#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1108#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1109
1110static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1111 int target_data_len)
1112{
1113 if (target_data_len < sizeof(struct pn533_target_felica))
1114 return false;
1115
1116 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1117 return false;
1118
1119 return true;
1120}
1121
1122static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1123 int tgt_data_len)
1124{
1125 struct pn533_target_felica *tgt_felica;
1126
1127 tgt_felica = (struct pn533_target_felica *) tgt_data;
1128
1129 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1130 return -EPROTO;
1131
1132 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1133 tgt_felica->nfcid2[1] ==
1134 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1135 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1136 else
1137 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1138
Samuel Ortiz79757542012-03-05 01:03:45 +01001139 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1140 nfc_tgt->sensf_res_len = 9;
1141
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001142 return 0;
1143}
1144
1145struct pn533_target_jewel {
1146 __be16 sens_res;
1147 u8 jewelid[4];
1148} __packed;
1149
1150static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1151 int target_data_len)
1152{
1153 u8 ssd;
1154 u8 platconf;
1155
1156 if (target_data_len < sizeof(struct pn533_target_jewel))
1157 return false;
1158
1159 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1160 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1161 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1162
1163 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1164 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1165 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1166 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1167 return false;
1168
1169 return true;
1170}
1171
1172static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1173 int tgt_data_len)
1174{
1175 struct pn533_target_jewel *tgt_jewel;
1176
1177 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1178
1179 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1180 return -EPROTO;
1181
1182 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1183 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001184 nfc_tgt->nfcid1_len = 4;
1185 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001186
1187 return 0;
1188}
1189
1190struct pn533_type_b_prot_info {
1191 u8 bitrate;
1192 u8 fsci_type;
1193 u8 fwi_adc_fo;
1194} __packed;
1195
1196#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1197#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1198#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1199
1200struct pn533_type_b_sens_res {
1201 u8 opcode;
1202 u8 nfcid[4];
1203 u8 appdata[4];
1204 struct pn533_type_b_prot_info prot_info;
1205} __packed;
1206
1207#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1208
1209struct pn533_target_type_b {
1210 struct pn533_type_b_sens_res sensb_res;
1211 u8 attrib_res_len;
1212 u8 attrib_res[];
1213} __packed;
1214
1215static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1216 int target_data_len)
1217{
1218 if (target_data_len < sizeof(struct pn533_target_type_b))
1219 return false;
1220
1221 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1222 return false;
1223
1224 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1225 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1226 return false;
1227
1228 return true;
1229}
1230
1231static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1232 int tgt_data_len)
1233{
1234 struct pn533_target_type_b *tgt_type_b;
1235
1236 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1237
1238 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1239 return -EPROTO;
1240
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001241 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001242
1243 return 0;
1244}
1245
1246struct pn533_poll_response {
1247 u8 nbtg;
1248 u8 tg;
1249 u8 target_data[];
1250} __packed;
1251
1252static int pn533_target_found(struct pn533 *dev,
1253 struct pn533_poll_response *resp, int resp_len)
1254{
1255 int target_data_len;
1256 struct nfc_target nfc_tgt;
1257 int rc;
1258
1259 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1260 dev->poll_mod_curr);
1261
1262 if (resp->tg != 1)
1263 return -EPROTO;
1264
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001265 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1266
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001267 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1268
1269 switch (dev->poll_mod_curr) {
1270 case PN533_POLL_MOD_106KBPS_A:
1271 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1272 target_data_len);
1273 break;
1274 case PN533_POLL_MOD_212KBPS_FELICA:
1275 case PN533_POLL_MOD_424KBPS_FELICA:
1276 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1277 target_data_len);
1278 break;
1279 case PN533_POLL_MOD_106KBPS_JEWEL:
1280 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1281 target_data_len);
1282 break;
1283 case PN533_POLL_MOD_847KBPS_B:
1284 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1285 target_data_len);
1286 break;
1287 default:
1288 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1289 " modulation");
1290 return -EPROTO;
1291 }
1292
1293 if (rc)
1294 return rc;
1295
1296 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1297 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1298 " have the desired protocol");
1299 return -EAGAIN;
1300 }
1301
1302 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1303 "0x%x", nfc_tgt.supported_protocols);
1304
1305 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1306
1307 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1308
1309 return 0;
1310}
1311
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001312static inline void pn533_poll_next_mod(struct pn533 *dev)
1313{
1314 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1315}
1316
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317static void pn533_poll_reset_mod_list(struct pn533 *dev)
1318{
1319 dev->poll_mod_count = 0;
1320}
1321
1322static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1323{
1324 dev->poll_mod_active[dev->poll_mod_count] =
1325 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1326 dev->poll_mod_count++;
1327}
1328
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001329static void pn533_poll_create_mod_list(struct pn533 *dev,
1330 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331{
1332 pn533_poll_reset_mod_list(dev);
1333
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001334 if (im_protocols & NFC_PROTO_MIFARE_MASK
1335 || im_protocols & NFC_PROTO_ISO14443_MASK
1336 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001337 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1338
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001339 if (im_protocols & NFC_PROTO_FELICA_MASK
1340 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001341 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1342 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1343 }
1344
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001345 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001346 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1347
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001348 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001349 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001350
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001351 if (tm_protocols)
1352 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001353}
1354
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001355static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001356{
1357 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001358 int rc;
1359
1360 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1361
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001362 resp = (struct pn533_poll_response *) params;
1363 if (resp->nbtg) {
1364 rc = pn533_target_found(dev, resp, params_len);
1365
1366 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001367 if (rc == 0) {
1368 pn533_poll_reset_mod_list(dev);
1369 return 0;
1370 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001371 }
1372
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001373 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001374}
1375
Samuel Ortizad3823c2012-05-30 23:54:55 +02001376static int pn533_init_target_frame(struct pn533_frame *frame,
1377 u8 *gb, size_t gb_len)
1378{
1379 struct pn533_cmd_init_target *cmd;
1380 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001381 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1382 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1383 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1384 0xff, 0xff}; /* System code */
1385 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1386 0x0, 0x0, 0x0,
1387 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001388
1389 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1390 cmd = kzalloc(cmd_len, GFP_KERNEL);
1391 if (cmd == NULL)
1392 return -ENOMEM;
1393
1394 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1395
1396 /* DEP support only */
1397 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001398
1399 /* Felica params */
1400 memcpy(cmd->felica, felica_params, 18);
1401 get_random_bytes(cmd->felica + 2, 6);
1402
1403 /* NFCID3 */
1404 memset(cmd->nfcid3, 0, 10);
1405 memcpy(cmd->nfcid3, cmd->felica, 8);
1406
1407 /* MIFARE params */
1408 memcpy(cmd->mifare, mifare_params, 6);
1409
1410 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001411 cmd->gb_len = gb_len;
1412 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001413
Samuel Ortizad3823c2012-05-30 23:54:55 +02001414 /* Len Tk */
1415 cmd->gb[gb_len] = 0;
1416
1417 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001418
Samuel Ortizad3823c2012-05-30 23:54:55 +02001419 frame->datalen += cmd_len;
1420
1421 pn533_tx_frame_finish(frame);
1422
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001423 kfree(cmd);
1424
Samuel Ortizad3823c2012-05-30 23:54:55 +02001425 return 0;
1426}
1427
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001428#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001429#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1430static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1431 u8 *params, int params_len)
1432{
1433 struct sk_buff *skb_resp = arg;
1434 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1435
1436 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1437
1438 if (params_len < 0) {
1439 nfc_dev_err(&dev->interface->dev,
1440 "Error %d when starting as a target",
1441 params_len);
1442
1443 return params_len;
1444 }
1445
1446 if (params_len > 0 && params[0] != 0) {
1447 nfc_tm_deactivated(dev->nfc_dev);
1448
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001449 dev->tgt_mode = 0;
1450
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001451 kfree_skb(skb_resp);
1452 return 0;
1453 }
1454
1455 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001456 skb_pull(skb_resp,
1457 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1458 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001459
1460 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1461}
1462
1463static void pn533_wq_tg_get_data(struct work_struct *work)
1464{
1465 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1466 struct pn533_frame *in_frame;
1467 struct sk_buff *skb_resp;
1468 size_t skb_resp_len;
1469
1470 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1471
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001472 skb_resp_len = PN533_FRAME_HEADER_LEN +
1473 PN533_CMD_DATAEXCH_HEAD_LEN +
1474 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1475 PN533_FRAME_TAIL_LEN;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001476
1477 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1478 if (!skb_resp)
1479 return;
1480
1481 in_frame = (struct pn533_frame *)skb_resp->data;
1482
1483 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1484 pn533_tx_frame_finish(dev->out_frame);
1485
1486 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1487 skb_resp_len,
1488 pn533_tm_get_data_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001489 skb_resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001490
1491 return;
1492}
1493
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001494#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001495static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001496{
1497 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001498 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1499 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001500 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001501
1502 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1503
1504 if (params_len < 0) {
1505 nfc_dev_err(&dev->interface->dev,
1506 "Error %d when starting as a target",
1507 params_len);
1508
1509 return params_len;
1510 }
1511
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001512 if (params_len < ATR_REQ_GB_OFFSET + 1)
1513 return -EINVAL;
1514
Samuel Ortizad3823c2012-05-30 23:54:55 +02001515 resp = (struct pn533_cmd_init_target_response *) params;
1516
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001517 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1518 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001519
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001520 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1521 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1522 comm_mode = NFC_COMM_ACTIVE;
1523
1524 /* Again, only DEP */
1525 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1526 return -EOPNOTSUPP;
1527
1528 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1529 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1530
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001531 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1532 comm_mode, gb, gb_len);
1533 if (rc < 0) {
1534 nfc_dev_err(&dev->interface->dev,
1535 "Error when signaling target activation");
1536 return rc;
1537 }
1538
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001539 dev->tgt_mode = 1;
1540
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001541 queue_work(dev->wq, &dev->tg_work);
1542
1543 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001544}
1545
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001546static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001547{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001548 struct pn533 *dev = (struct pn533 *) data;
1549
1550 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1551
1552 /* An ack will cancel the last issued command (poll) */
1553 pn533_send_ack(dev, GFP_ATOMIC);
1554
1555 dev->cancel_listen = 1;
1556
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557 pn533_poll_next_mod(dev);
1558
1559 queue_work(dev->wq, &dev->poll_work);
1560}
1561
1562static int pn533_poll_complete(struct pn533 *dev, void *arg,
1563 u8 *params, int params_len)
1564{
1565 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001566 int rc;
1567
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1569
1570 if (params_len == -ENOENT) {
1571 if (dev->poll_mod_count != 0)
1572 return 0;
1573
1574 nfc_dev_err(&dev->interface->dev,
1575 "Polling operation has been stopped");
1576
1577 goto stop_poll;
1578 }
1579
1580 if (params_len < 0) {
1581 nfc_dev_err(&dev->interface->dev,
1582 "Error %d when running poll", params_len);
1583
1584 goto stop_poll;
1585 }
1586
1587 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1588
1589 if (cur_mod->len == 0) {
1590 del_timer(&dev->listen_timer);
1591
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001592 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001593 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001594 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001595 if (!rc)
1596 return rc;
1597 }
1598
1599 pn533_poll_next_mod(dev);
1600
1601 queue_work(dev->wq, &dev->poll_work);
1602
1603 return 0;
1604
1605stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001606 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001607 dev->poll_protocols = 0;
1608 return 0;
1609}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001610
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001611static void pn533_build_poll_frame(struct pn533 *dev,
1612 struct pn533_frame *frame,
1613 struct pn533_poll_modulations *mod)
1614{
1615 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001616
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001617 if (mod->len == 0) {
1618 /* Listen mode */
1619 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1620 } else {
1621 /* Polling mode */
1622 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1623
1624 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1625 frame->datalen += mod->len;
1626
1627 pn533_tx_frame_finish(frame);
1628 }
1629}
1630
1631static int pn533_send_poll_frame(struct pn533 *dev)
1632{
1633 struct pn533_poll_modulations *cur_mod;
1634 int rc;
1635
1636 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1637
1638 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001639
1640 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001641 PN533_NORMAL_FRAME_MAX_LEN,
1642 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001643 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001644 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001645 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001646
1647 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001648}
1649
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001650static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001651{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001652 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1653 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001654 int rc;
1655
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001656 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1657
1658 nfc_dev_dbg(&dev->interface->dev,
1659 "%s cancel_listen %d modulation len %d",
1660 __func__, dev->cancel_listen, cur_mod->len);
1661
1662 if (dev->cancel_listen == 1) {
1663 dev->cancel_listen = 0;
1664 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001665 }
1666
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 rc = pn533_send_poll_frame(dev);
1668 if (rc)
1669 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001670
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001671 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1672 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001673
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001674 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001675}
1676
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001677static int pn533_start_poll(struct nfc_dev *nfc_dev,
1678 u32 im_protocols, u32 tm_protocols)
1679{
1680 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1681
1682 nfc_dev_dbg(&dev->interface->dev,
1683 "%s: im protocols 0x%x tm protocols 0x%x",
1684 __func__, im_protocols, tm_protocols);
1685
1686 if (dev->tgt_active_prot) {
1687 nfc_dev_err(&dev->interface->dev,
1688 "Cannot poll with a target already activated");
1689 return -EBUSY;
1690 }
1691
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001692 if (dev->tgt_mode) {
1693 nfc_dev_err(&dev->interface->dev,
1694 "Cannot poll while already being activated");
1695 return -EBUSY;
1696 }
1697
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001698 if (tm_protocols) {
1699 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1700 if (dev->gb == NULL)
1701 tm_protocols = 0;
1702 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001703
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001704 dev->poll_mod_curr = 0;
1705 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1706 dev->poll_protocols = im_protocols;
1707 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001708
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001709 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001710}
1711
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001712static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1713{
1714 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1715
1716 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1717
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001718 del_timer(&dev->listen_timer);
1719
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720 if (!dev->poll_mod_count) {
1721 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1722 " running");
1723 return;
1724 }
1725
1726 /* An ack will cancel the last issued command (poll) */
1727 pn533_send_ack(dev, GFP_KERNEL);
1728
1729 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1730 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001731
1732 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001733}
1734
1735static int pn533_activate_target_nfcdep(struct pn533 *dev)
1736{
1737 struct pn533_cmd_activate_param param;
1738 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001739 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001740 int rc;
1741
1742 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1743
1744 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1745
1746 param.tg = 1;
1747 param.next = 0;
1748 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1749 sizeof(struct pn533_cmd_activate_param));
1750 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1751
1752 pn533_tx_frame_finish(dev->out_frame);
1753
1754 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001755 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001756 if (rc)
1757 return rc;
1758
1759 resp = (struct pn533_cmd_activate_response *)
1760 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1761 rc = resp->status & PN533_CMD_RET_MASK;
1762 if (rc != PN533_CMD_RET_SUCCESS)
1763 return -EIO;
1764
Samuel Ortiz541d9202011-12-14 16:43:10 +01001765 /* ATR_RES general bytes are located at offset 16 */
1766 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1767 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1768
1769 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001770}
1771
Eric Lapuyade90099432012-05-07 12:31:13 +02001772static int pn533_activate_target(struct nfc_dev *nfc_dev,
1773 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001774{
1775 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1776 int rc;
1777
1778 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1779 protocol);
1780
1781 if (dev->poll_mod_count) {
1782 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1783 " polling");
1784 return -EBUSY;
1785 }
1786
1787 if (dev->tgt_active_prot) {
1788 nfc_dev_err(&dev->interface->dev, "There is already an active"
1789 " target");
1790 return -EBUSY;
1791 }
1792
1793 if (!dev->tgt_available_prots) {
1794 nfc_dev_err(&dev->interface->dev, "There is no available target"
1795 " to activate");
1796 return -EINVAL;
1797 }
1798
1799 if (!(dev->tgt_available_prots & (1 << protocol))) {
1800 nfc_dev_err(&dev->interface->dev, "The target does not support"
1801 " the requested protocol %u", protocol);
1802 return -EINVAL;
1803 }
1804
1805 if (protocol == NFC_PROTO_NFC_DEP) {
1806 rc = pn533_activate_target_nfcdep(dev);
1807 if (rc) {
1808 nfc_dev_err(&dev->interface->dev, "Error %d when"
1809 " activating target with"
1810 " NFC_DEP protocol", rc);
1811 return rc;
1812 }
1813 }
1814
1815 dev->tgt_active_prot = protocol;
1816 dev->tgt_available_prots = 0;
1817
1818 return 0;
1819}
1820
Eric Lapuyade90099432012-05-07 12:31:13 +02001821static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1822 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823{
1824 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1825 u8 tg;
1826 u8 status;
1827 int rc;
1828
1829 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1830
1831 if (!dev->tgt_active_prot) {
1832 nfc_dev_err(&dev->interface->dev, "There is no active target");
1833 return;
1834 }
1835
1836 dev->tgt_active_prot = 0;
1837
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001838 skb_queue_purge(&dev->resp_q);
1839
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001840 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1841
1842 tg = 1;
1843 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1844 dev->out_frame->datalen += sizeof(u8);
1845
1846 pn533_tx_frame_finish(dev->out_frame);
1847
1848 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001849 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001850 if (rc) {
1851 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1852 " command to the controller");
1853 return;
1854 }
1855
1856 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1857 rc = status & PN533_CMD_RET_MASK;
1858 if (rc != PN533_CMD_RET_SUCCESS)
1859 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1860 " the target", rc);
1861
1862 return;
1863}
1864
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001865
1866static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1867 u8 *params, int params_len)
1868{
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869 struct pn533_cmd_jump_dep_response *resp;
1870 struct nfc_target nfc_target;
1871 u8 target_gt_len;
1872 int rc;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001873 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1874 u8 active = cmd->active;
1875
1876 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877
1878 if (params_len == -ENOENT) {
1879 nfc_dev_dbg(&dev->interface->dev, "");
1880 return 0;
1881 }
1882
1883 if (params_len < 0) {
1884 nfc_dev_err(&dev->interface->dev,
1885 "Error %d when bringing DEP link up",
1886 params_len);
1887 return 0;
1888 }
1889
1890 if (dev->tgt_available_prots &&
1891 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1892 nfc_dev_err(&dev->interface->dev,
1893 "The target does not support DEP");
1894 return -EINVAL;
1895 }
1896
1897 resp = (struct pn533_cmd_jump_dep_response *) params;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001898 rc = resp->status & PN533_CMD_RET_MASK;
1899 if (rc != PN533_CMD_RET_SUCCESS) {
1900 nfc_dev_err(&dev->interface->dev,
1901 "Bringing DEP link up failed %d", rc);
1902 return 0;
1903 }
1904
1905 if (!dev->tgt_available_prots) {
1906 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1907
1908 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001909 nfc_target.nfcid1_len = 10;
1910 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001911 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1912 if (rc)
1913 return 0;
1914
1915 dev->tgt_available_prots = 0;
1916 }
1917
1918 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1919
1920 /* ATR_RES general bytes are located at offset 17 */
1921 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1922 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1923 resp->gt, target_gt_len);
1924 if (rc == 0)
1925 rc = nfc_dep_link_is_up(dev->nfc_dev,
1926 dev->nfc_dev->targets[0].idx,
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001927 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001928
1929 return 0;
1930}
1931
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001932static int pn533_mod_to_baud(struct pn533 *dev)
1933{
1934 switch (dev->poll_mod_curr) {
1935 case PN533_POLL_MOD_106KBPS_A:
1936 return 0;
1937 case PN533_POLL_MOD_212KBPS_FELICA:
1938 return 1;
1939 case PN533_POLL_MOD_424KBPS_FELICA:
1940 return 2;
1941 default:
1942 return -EINVAL;
1943 }
1944}
1945
Samuel Ortizd7f33452012-05-29 21:45:21 +02001946#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001947static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001948 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001949{
1950 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1951 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001952 u8 cmd_len, *data_ptr;
1953 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001954 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001955
1956 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1957
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001958 if (dev->poll_mod_count) {
1959 nfc_dev_err(&dev->interface->dev,
1960 "Cannot bring the DEP link up while polling");
1961 return -EBUSY;
1962 }
1963
1964 if (dev->tgt_active_prot) {
1965 nfc_dev_err(&dev->interface->dev,
1966 "There is already an active target");
1967 return -EBUSY;
1968 }
1969
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001970 baud = pn533_mod_to_baud(dev);
1971 if (baud < 0) {
1972 nfc_dev_err(&dev->interface->dev,
1973 "Invalid curr modulation %d", dev->poll_mod_curr);
1974 return baud;
1975 }
1976
Samuel Ortiz47807d32012-03-05 01:03:50 +01001977 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001978 if (comm_mode == NFC_COMM_PASSIVE)
1979 cmd_len += PASSIVE_DATA_LEN;
1980
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001981 cmd = kzalloc(cmd_len, GFP_KERNEL);
1982 if (cmd == NULL)
1983 return -ENOMEM;
1984
1985 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1986
1987 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001988 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001989 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001990 data_ptr = cmd->data;
1991 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
1992 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
1993 cmd->next |= 1;
1994 data_ptr += PASSIVE_DATA_LEN;
1995 }
1996
Samuel Ortiz47807d32012-03-05 01:03:50 +01001997 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02001998 cmd->next |= 4; /* We have some Gi */
1999 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002000 } else {
2001 cmd->next = 0;
2002 }
2003
2004 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2005 dev->out_frame->datalen += cmd_len;
2006
2007 pn533_tx_frame_finish(dev->out_frame);
2008
2009 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002010 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002011 pn533_in_dep_link_up_complete, cmd);
Szymon Janc770f7502012-10-29 14:04:43 +01002012 if (rc < 0)
2013 kfree(cmd);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002014
2015 return rc;
2016}
2017
2018static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2019{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002020 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2021
2022 pn533_poll_reset_mod_list(dev);
2023
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002024 if (dev->tgt_mode || dev->tgt_active_prot) {
2025 pn533_send_ack(dev, GFP_KERNEL);
2026 usb_kill_urb(dev->in_urb);
2027 }
2028
2029 dev->tgt_active_prot = 0;
2030 dev->tgt_mode = 0;
2031
2032 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002033
2034 return 0;
2035}
2036
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002037static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2038 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002039{
2040 int payload_len = skb->len;
2041 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002042 u8 tg;
2043
2044 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2045 payload_len);
2046
2047 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2048 /* TODO: Implement support to multi-part data exchange */
2049 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2050 " max allowed: %d",
2051 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2052 return -ENOSYS;
2053 }
2054
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002055 skb_push(skb, PN533_FRAME_HEADER_LEN);
2056
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002057 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002058 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002059 case PN533_DEVICE_PASORI:
2060 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002061 out_frame = (struct pn533_frame *) skb->data;
2062 pn533_tx_frame_init(out_frame,
2063 PN533_CMD_IN_COMM_THRU);
2064
2065 break;
2066 }
2067
2068 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002069 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2070 out_frame = (struct pn533_frame *) skb->data;
2071 pn533_tx_frame_init(out_frame,
2072 PN533_CMD_IN_DATA_EXCHANGE);
2073 tg = 1;
2074 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2075 &tg, sizeof(u8));
2076 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002077
2078 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002079 }
2080
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002081 } else {
2082 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2083 out_frame = (struct pn533_frame *) skb->data;
2084 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2085 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002086
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002087
2088 /* The data is already in the out_frame, just update the datalen */
2089 out_frame->datalen += payload_len;
2090
2091 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002092 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002093
2094 return 0;
2095}
2096
2097struct pn533_data_exchange_arg {
2098 struct sk_buff *skb_resp;
2099 struct sk_buff *skb_out;
2100 data_exchange_cb_t cb;
2101 void *cb_context;
2102};
2103
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002104static struct sk_buff *pn533_build_response(struct pn533 *dev)
2105{
2106 struct sk_buff *skb, *tmp, *t;
2107 unsigned int skb_len = 0, tmp_len = 0;
2108
2109 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2110
2111 if (skb_queue_empty(&dev->resp_q))
2112 return NULL;
2113
2114 if (skb_queue_len(&dev->resp_q) == 1) {
2115 skb = skb_dequeue(&dev->resp_q);
2116 goto out;
2117 }
2118
2119 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2120 skb_len += tmp->len;
2121
2122 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2123 __func__, skb_len);
2124
2125 skb = alloc_skb(skb_len, GFP_KERNEL);
2126 if (skb == NULL)
2127 goto out;
2128
2129 skb_put(skb, skb_len);
2130
2131 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2132 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2133 tmp_len += tmp->len;
2134 }
2135
2136out:
2137 skb_queue_purge(&dev->resp_q);
2138
2139 return skb;
2140}
2141
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002142static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2143 u8 *params, int params_len)
2144{
2145 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002146 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002147 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2148 int err = 0;
2149 u8 status;
2150 u8 cmd_ret;
2151
2152 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2153
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002154 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002155
2156 if (params_len < 0) { /* error */
2157 err = params_len;
2158 goto error;
2159 }
2160
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161 status = params[0];
2162
2163 cmd_ret = status & PN533_CMD_RET_MASK;
2164 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2165 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2166 " exchanging data", cmd_ret);
2167 err = -EIO;
2168 goto error;
2169 }
2170
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002171 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002172 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002173 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002174 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002175 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002176
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002177 if (status & PN533_CMD_MI_MASK) {
2178 queue_work(dev->wq, &dev->mi_work);
2179 return -EINPROGRESS;
2180 }
2181
2182 skb = pn533_build_response(dev);
2183 if (skb == NULL)
2184 goto error;
2185
2186 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002187 kfree(arg);
2188 return 0;
2189
2190error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002191 skb_queue_purge(&dev->resp_q);
2192 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002193 arg->cb(arg->cb_context, NULL, err);
2194 kfree(arg);
2195 return 0;
2196}
2197
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002198static int pn533_transceive(struct nfc_dev *nfc_dev,
2199 struct nfc_target *target, struct sk_buff *skb,
2200 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002201{
2202 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2203 struct pn533_frame *out_frame, *in_frame;
2204 struct pn533_data_exchange_arg *arg;
2205 struct sk_buff *skb_resp;
2206 int skb_resp_len;
2207 int rc;
2208
2209 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2210
2211 if (!dev->tgt_active_prot) {
2212 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2213 " there is no active target");
2214 rc = -EINVAL;
2215 goto error;
2216 }
2217
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002218 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002219 if (rc)
2220 goto error;
2221
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002222 skb_resp_len = PN533_FRAME_HEADER_LEN +
2223 PN533_CMD_DATAEXCH_HEAD_LEN +
2224 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2225 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002226
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002227 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002228 if (!skb_resp) {
2229 rc = -ENOMEM;
2230 goto error;
2231 }
2232
2233 in_frame = (struct pn533_frame *) skb_resp->data;
2234 out_frame = (struct pn533_frame *) skb->data;
2235
2236 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2237 if (!arg) {
2238 rc = -ENOMEM;
2239 goto free_skb_resp;
2240 }
2241
2242 arg->skb_resp = skb_resp;
2243 arg->skb_out = skb;
2244 arg->cb = cb;
2245 arg->cb_context = cb_context;
2246
2247 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002248 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002249 if (rc) {
2250 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2251 " perform data_exchange", rc);
2252 goto free_arg;
2253 }
2254
2255 return 0;
2256
2257free_arg:
2258 kfree(arg);
2259free_skb_resp:
2260 kfree_skb(skb_resp);
2261error:
2262 kfree_skb(skb);
2263 return rc;
2264}
2265
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002266static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2267 u8 *params, int params_len)
2268{
Thierry Escande5b412fd2012-11-15 18:24:28 +01002269 struct sk_buff *skb_out = arg;
2270
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002271 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2272
Thierry Escande5b412fd2012-11-15 18:24:28 +01002273 dev_kfree_skb(skb_out);
2274
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002275 if (params_len < 0) {
2276 nfc_dev_err(&dev->interface->dev,
2277 "Error %d when sending data",
2278 params_len);
2279
2280 return params_len;
2281 }
2282
2283 if (params_len > 0 && params[0] != 0) {
2284 nfc_tm_deactivated(dev->nfc_dev);
2285
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002286 dev->tgt_mode = 0;
2287
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002288 return 0;
2289 }
2290
2291 queue_work(dev->wq, &dev->tg_work);
2292
2293 return 0;
2294}
2295
2296static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2297{
2298 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2299 struct pn533_frame *out_frame;
2300 int rc;
2301
2302 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2303
2304 rc = pn533_build_tx_frame(dev, skb, false);
2305 if (rc)
2306 goto error;
2307
2308 out_frame = (struct pn533_frame *) skb->data;
2309
2310 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002311 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002312 pn533_tm_send_complete, skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002313 if (rc) {
2314 nfc_dev_err(&dev->interface->dev,
2315 "Error %d when trying to send data", rc);
2316 goto error;
2317 }
2318
2319 return 0;
2320
2321error:
2322 kfree_skb(skb);
2323
2324 return rc;
2325}
2326
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002327static void pn533_wq_mi_recv(struct work_struct *work)
2328{
2329 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2330 struct sk_buff *skb_cmd;
2331 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2332 struct pn533_frame *out_frame, *in_frame;
2333 struct sk_buff *skb_resp;
2334 int skb_resp_len;
2335 int rc;
2336
2337 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2338
2339 /* This is a zero payload size skb */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002340 skb_cmd = alloc_skb(PN533_FRAME_HEADER_LEN +
2341 PN533_CMD_DATAEXCH_HEAD_LEN +
2342 PN533_FRAME_TAIL_LEN,
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002343 GFP_KERNEL);
2344 if (skb_cmd == NULL)
2345 goto error_cmd;
2346
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002347 skb_reserve(skb_cmd,
2348 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002349
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002350 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002351 if (rc)
2352 goto error_frame;
2353
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002354 skb_resp_len = PN533_FRAME_HEADER_LEN +
2355 PN533_CMD_DATAEXCH_HEAD_LEN +
2356 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2357 PN533_FRAME_TAIL_LEN;
2358
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002359 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2360 if (!skb_resp) {
2361 rc = -ENOMEM;
2362 goto error_frame;
2363 }
2364
2365 in_frame = (struct pn533_frame *) skb_resp->data;
2366 out_frame = (struct pn533_frame *) skb_cmd->data;
2367
2368 arg->skb_resp = skb_resp;
2369 arg->skb_out = skb_cmd;
2370
2371 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2372 skb_resp_len,
2373 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002374 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002375 if (!rc)
2376 return;
2377
2378 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2379 " perform data_exchange", rc);
2380
2381 kfree_skb(skb_resp);
2382
2383error_frame:
2384 kfree_skb(skb_cmd);
2385
2386error_cmd:
2387 pn533_send_ack(dev, GFP_KERNEL);
2388
2389 kfree(arg);
2390
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002391 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002392}
2393
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002394static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2395 u8 cfgdata_len)
2396{
2397 int rc;
2398 u8 *params;
2399
2400 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2401
2402 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2403
2404 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2405 params[0] = cfgitem;
2406 memcpy(&params[1], cfgdata, cfgdata_len);
2407 dev->out_frame->datalen += (1 + cfgdata_len);
2408
2409 pn533_tx_frame_finish(dev->out_frame);
2410
2411 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002412 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002413
2414 return rc;
2415}
2416
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002417static int pn533_fw_reset(struct pn533 *dev)
2418{
2419 int rc;
2420 u8 *params;
2421
2422 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2423
2424 pn533_tx_frame_init(dev->out_frame, 0x18);
2425
2426 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2427 params[0] = 0x1;
2428 dev->out_frame->datalen += 1;
2429
2430 pn533_tx_frame_finish(dev->out_frame);
2431
2432 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002433 PN533_NORMAL_FRAME_MAX_LEN);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002434
2435 return rc;
2436}
2437
2438static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002439 .dev_up = NULL,
2440 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002441 .dep_link_up = pn533_dep_link_up,
2442 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002443 .start_poll = pn533_start_poll,
2444 .stop_poll = pn533_stop_poll,
2445 .activate_target = pn533_activate_target,
2446 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002447 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002448 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002449};
2450
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002451static int pn533_setup(struct pn533 *dev)
2452{
2453 struct pn533_config_max_retries max_retries;
2454 struct pn533_config_timing timing;
2455 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2456 int rc;
2457
2458 switch (dev->device_type) {
2459 case PN533_DEVICE_STD:
2460 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2461 max_retries.mx_rty_psl = 2;
2462 max_retries.mx_rty_passive_act =
2463 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2464
2465 timing.rfu = PN533_CONFIG_TIMING_102;
2466 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2467 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2468
2469 break;
2470
2471 case PN533_DEVICE_PASORI:
2472 max_retries.mx_rty_atr = 0x2;
2473 max_retries.mx_rty_psl = 0x1;
2474 max_retries.mx_rty_passive_act =
2475 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2476
2477 timing.rfu = PN533_CONFIG_TIMING_102;
2478 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2479 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2480
2481 break;
2482
2483 default:
2484 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2485 dev->device_type);
2486 return -EINVAL;
2487 }
2488
2489 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2490 (u8 *)&max_retries, sizeof(max_retries));
2491 if (rc) {
2492 nfc_dev_err(&dev->interface->dev,
2493 "Error on setting MAX_RETRIES config");
2494 return rc;
2495 }
2496
2497
2498 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2499 (u8 *)&timing, sizeof(timing));
2500 if (rc) {
2501 nfc_dev_err(&dev->interface->dev,
2502 "Error on setting RF timings");
2503 return rc;
2504 }
2505
2506 switch (dev->device_type) {
2507 case PN533_DEVICE_STD:
2508 break;
2509
2510 case PN533_DEVICE_PASORI:
2511 pn533_fw_reset(dev);
2512
2513 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2514 pasori_cfg, 3);
2515 if (rc) {
2516 nfc_dev_err(&dev->interface->dev,
2517 "Error while settings PASORI config");
2518 return rc;
2519 }
2520
2521 pn533_fw_reset(dev);
2522
2523 break;
2524 }
2525
2526 return 0;
2527}
2528
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002529static int pn533_probe(struct usb_interface *interface,
2530 const struct usb_device_id *id)
2531{
2532 struct pn533_fw_version *fw_ver;
2533 struct pn533 *dev;
2534 struct usb_host_interface *iface_desc;
2535 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002536 int in_endpoint = 0;
2537 int out_endpoint = 0;
2538 int rc = -ENOMEM;
2539 int i;
2540 u32 protocols;
2541
2542 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2543 if (!dev)
2544 return -ENOMEM;
2545
2546 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2547 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002548 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549
2550 iface_desc = interface->cur_altsetting;
2551 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2552 endpoint = &iface_desc->endpoint[i].desc;
2553
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002554 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002556
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002557 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002558 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002559 }
2560
2561 if (!in_endpoint || !out_endpoint) {
2562 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2563 " bulk-out endpoint");
2564 rc = -ENODEV;
2565 goto error;
2566 }
2567
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002568 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002569 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002570 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002571 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2572
2573 if (!dev->in_frame || !dev->out_frame ||
2574 !dev->in_urb || !dev->out_urb)
2575 goto error;
2576
2577 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2578 usb_rcvbulkpipe(dev->udev, in_endpoint),
2579 NULL, 0, NULL, dev);
2580 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2581 usb_sndbulkpipe(dev->udev, out_endpoint),
2582 NULL, 0,
2583 pn533_send_complete, dev);
2584
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002585 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2586 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002587 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002588 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002589 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002590 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002591 if (dev->wq == NULL)
2592 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002593
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002594 init_timer(&dev->listen_timer);
2595 dev->listen_timer.data = (unsigned long) dev;
2596 dev->listen_timer.function = pn533_listen_mode_timer;
2597
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002598 skb_queue_head_init(&dev->resp_q);
2599
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002600 INIT_LIST_HEAD(&dev->cmd_queue);
2601
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602 usb_set_intfdata(interface, dev);
2603
2604 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2605 pn533_tx_frame_finish(dev->out_frame);
2606
2607 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002608 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002610 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002611
2612 fw_ver = (struct pn533_fw_version *)
2613 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2614 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2615 " attached", fw_ver->ver, fw_ver->rev);
2616
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002617 dev->device_type = id->driver_info;
2618 switch (dev->device_type) {
2619 case PN533_DEVICE_STD:
2620 protocols = PN533_ALL_PROTOCOLS;
2621 break;
2622
2623 case PN533_DEVICE_PASORI:
2624 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2625 break;
2626
2627 default:
2628 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2629 dev->device_type);
2630 rc = -EINVAL;
2631 goto destroy_wq;
2632 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002633
Samuel Ortize8753042011-08-19 15:47:11 +02002634 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002635 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002636 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002637 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002638 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002639 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002640
2641 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2642 nfc_set_drvdata(dev->nfc_dev, dev);
2643
2644 rc = nfc_register_device(dev->nfc_dev);
2645 if (rc)
2646 goto free_nfc_dev;
2647
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002648 rc = pn533_setup(dev);
2649 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002650 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002651
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002652 return 0;
2653
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002654unregister_nfc_dev:
2655 nfc_unregister_device(dev->nfc_dev);
2656
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002657free_nfc_dev:
2658 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002659
Samuel Ortiz4849f852012-04-10 19:43:17 +02002660destroy_wq:
2661 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662error:
2663 kfree(dev->in_frame);
2664 usb_free_urb(dev->in_urb);
2665 kfree(dev->out_frame);
2666 usb_free_urb(dev->out_urb);
2667 kfree(dev);
2668 return rc;
2669}
2670
2671static void pn533_disconnect(struct usb_interface *interface)
2672{
2673 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002674 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002675
2676 dev = usb_get_intfdata(interface);
2677 usb_set_intfdata(interface, NULL);
2678
2679 nfc_unregister_device(dev->nfc_dev);
2680 nfc_free_device(dev->nfc_dev);
2681
2682 usb_kill_urb(dev->in_urb);
2683 usb_kill_urb(dev->out_urb);
2684
Samuel Ortiz4849f852012-04-10 19:43:17 +02002685 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002686
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002687 skb_queue_purge(&dev->resp_q);
2688
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002689 del_timer(&dev->listen_timer);
2690
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002691 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2692 list_del(&cmd->queue);
2693 kfree(cmd);
2694 }
2695
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002696 kfree(dev->in_frame);
2697 usb_free_urb(dev->in_urb);
2698 kfree(dev->out_frame);
2699 usb_free_urb(dev->out_urb);
2700 kfree(dev);
2701
Dan Carpenter276556d2011-07-08 10:21:15 +03002702 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002703}
2704
2705static struct usb_driver pn533_driver = {
2706 .name = "pn533",
2707 .probe = pn533_probe,
2708 .disconnect = pn533_disconnect,
2709 .id_table = pn533_table,
2710};
2711
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002712module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002713
2714MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2715 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2716MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2717MODULE_VERSION(VERSION);
2718MODULE_LICENSE("GPL");