blob: 94c88bb601cdde2caf7edfb0abcccb591f10b52b [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;
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100948 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300949 struct completion done;
950};
951
952static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
953 u8 *params, int params_len)
954{
955 struct pn533_sync_cmd_response *arg = _arg;
956
957 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
958
959 arg->rc = 0;
960
961 if (params_len < 0) /* error */
962 arg->rc = params_len;
963
964 complete(&arg->done);
965
966 return 0;
967}
968
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100969static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
970 struct sk_buff *resp)
971{
972 struct pn533_sync_cmd_response *arg = _arg;
973
974 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
975
976 arg->resp = resp;
977 complete(&arg->done);
978
979 return 0;
980}
981
982/* pn533_send_cmd_sync
983 *
984 * Please note the req parameter is freed inside the function to
985 * limit a number of return value interpretations by the caller.
986 *
987 * 1. negative in case of error during TX path -> req should be freed
988 *
989 * 2. negative in case of error during RX path -> req should not be freed
990 * as it's been already freed at the begining of RX path by
991 * async_complete_cb.
992 *
993 * 3. valid pointer in case of succesfult RX path
994 *
995 * A caller has to check a return value with IS_ERR macro. If the test pass,
996 * the returned pointer is valid.
997 *
998 * */
999static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1000 struct sk_buff *req)
1001{
1002 int rc;
1003 struct pn533_sync_cmd_response arg;
1004
1005 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1006
1007 init_completion(&arg.done);
1008
1009 rc = pn533_send_cmd_async(dev, cmd_code, req,
1010 pn533_send_sync_complete, &arg);
1011 if (rc) {
1012 dev_kfree_skb(req);
1013 return ERR_PTR(rc);
1014 }
1015
1016 wait_for_completion(&arg.done);
1017
1018 return arg.resp;
1019}
1020
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001021static int pn533_send_cmd_frame_sync(struct pn533 *dev,
1022 struct pn533_frame *out_frame,
1023 struct pn533_frame *in_frame,
1024 int in_frame_len)
1025{
1026 int rc;
1027 struct pn533_sync_cmd_response arg;
1028
1029 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1030
1031 init_completion(&arg.done);
1032
1033 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001034 pn533_sync_cmd_complete, &arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001035 if (rc)
1036 return rc;
1037
1038 wait_for_completion(&arg.done);
1039
1040 return arg.rc;
1041}
1042
1043static void pn533_send_complete(struct urb *urb)
1044{
1045 struct pn533 *dev = urb->context;
1046
1047 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1048
1049 switch (urb->status) {
1050 case 0:
1051 /* success */
1052 break;
1053 case -ECONNRESET:
1054 case -ENOENT:
1055 case -ESHUTDOWN:
1056 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1057 " status: %d", urb->status);
1058 break;
1059 default:
1060 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1061 " %d", urb->status);
1062 }
1063}
1064
1065struct pn533_target_type_a {
1066 __be16 sens_res;
1067 u8 sel_res;
1068 u8 nfcid_len;
1069 u8 nfcid_data[];
1070} __packed;
1071
1072
1073#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1074#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1075#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1076
1077#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1078#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1079
1080#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1081#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1082
1083#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1084#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1085#define PN533_TYPE_A_SEL_PROT_DEP 2
1086#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1087
1088static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1089 int target_data_len)
1090{
1091 u8 ssd;
1092 u8 platconf;
1093
1094 if (target_data_len < sizeof(struct pn533_target_type_a))
1095 return false;
1096
1097 /* The lenght check of nfcid[] and ats[] are not being performed because
1098 the values are not being used */
1099
1100 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1101 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1102 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1103
1104 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1105 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1106 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1107 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1108 return false;
1109
1110 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1111 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1112 return false;
1113
1114 return true;
1115}
1116
1117static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1118 int tgt_data_len)
1119{
1120 struct pn533_target_type_a *tgt_type_a;
1121
1122 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1123
1124 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1125 return -EPROTO;
1126
1127 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1128 case PN533_TYPE_A_SEL_PROT_MIFARE:
1129 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1130 break;
1131 case PN533_TYPE_A_SEL_PROT_ISO14443:
1132 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1133 break;
1134 case PN533_TYPE_A_SEL_PROT_DEP:
1135 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1136 break;
1137 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1138 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1139 NFC_PROTO_NFC_DEP_MASK;
1140 break;
1141 }
1142
1143 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1144 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001145 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1146 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001147
1148 return 0;
1149}
1150
1151struct pn533_target_felica {
1152 u8 pol_res;
1153 u8 opcode;
1154 u8 nfcid2[8];
1155 u8 pad[8];
1156 /* optional */
1157 u8 syst_code[];
1158} __packed;
1159
1160#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1161#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1162
1163static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1164 int target_data_len)
1165{
1166 if (target_data_len < sizeof(struct pn533_target_felica))
1167 return false;
1168
1169 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1170 return false;
1171
1172 return true;
1173}
1174
1175static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1176 int tgt_data_len)
1177{
1178 struct pn533_target_felica *tgt_felica;
1179
1180 tgt_felica = (struct pn533_target_felica *) tgt_data;
1181
1182 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1183 return -EPROTO;
1184
1185 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1186 tgt_felica->nfcid2[1] ==
1187 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1188 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1189 else
1190 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1191
Samuel Ortiz79757542012-03-05 01:03:45 +01001192 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1193 nfc_tgt->sensf_res_len = 9;
1194
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001195 return 0;
1196}
1197
1198struct pn533_target_jewel {
1199 __be16 sens_res;
1200 u8 jewelid[4];
1201} __packed;
1202
1203static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1204 int target_data_len)
1205{
1206 u8 ssd;
1207 u8 platconf;
1208
1209 if (target_data_len < sizeof(struct pn533_target_jewel))
1210 return false;
1211
1212 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1213 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1214 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1215
1216 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1217 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1218 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1219 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1220 return false;
1221
1222 return true;
1223}
1224
1225static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1226 int tgt_data_len)
1227{
1228 struct pn533_target_jewel *tgt_jewel;
1229
1230 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1231
1232 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1233 return -EPROTO;
1234
1235 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1236 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001237 nfc_tgt->nfcid1_len = 4;
1238 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001239
1240 return 0;
1241}
1242
1243struct pn533_type_b_prot_info {
1244 u8 bitrate;
1245 u8 fsci_type;
1246 u8 fwi_adc_fo;
1247} __packed;
1248
1249#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1250#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1251#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1252
1253struct pn533_type_b_sens_res {
1254 u8 opcode;
1255 u8 nfcid[4];
1256 u8 appdata[4];
1257 struct pn533_type_b_prot_info prot_info;
1258} __packed;
1259
1260#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1261
1262struct pn533_target_type_b {
1263 struct pn533_type_b_sens_res sensb_res;
1264 u8 attrib_res_len;
1265 u8 attrib_res[];
1266} __packed;
1267
1268static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1269 int target_data_len)
1270{
1271 if (target_data_len < sizeof(struct pn533_target_type_b))
1272 return false;
1273
1274 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1275 return false;
1276
1277 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1278 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1279 return false;
1280
1281 return true;
1282}
1283
1284static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1285 int tgt_data_len)
1286{
1287 struct pn533_target_type_b *tgt_type_b;
1288
1289 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1290
1291 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1292 return -EPROTO;
1293
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001294 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295
1296 return 0;
1297}
1298
1299struct pn533_poll_response {
1300 u8 nbtg;
1301 u8 tg;
1302 u8 target_data[];
1303} __packed;
1304
1305static int pn533_target_found(struct pn533 *dev,
1306 struct pn533_poll_response *resp, int resp_len)
1307{
1308 int target_data_len;
1309 struct nfc_target nfc_tgt;
1310 int rc;
1311
1312 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1313 dev->poll_mod_curr);
1314
1315 if (resp->tg != 1)
1316 return -EPROTO;
1317
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001318 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1319
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001320 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1321
1322 switch (dev->poll_mod_curr) {
1323 case PN533_POLL_MOD_106KBPS_A:
1324 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1325 target_data_len);
1326 break;
1327 case PN533_POLL_MOD_212KBPS_FELICA:
1328 case PN533_POLL_MOD_424KBPS_FELICA:
1329 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1330 target_data_len);
1331 break;
1332 case PN533_POLL_MOD_106KBPS_JEWEL:
1333 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1334 target_data_len);
1335 break;
1336 case PN533_POLL_MOD_847KBPS_B:
1337 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1338 target_data_len);
1339 break;
1340 default:
1341 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1342 " modulation");
1343 return -EPROTO;
1344 }
1345
1346 if (rc)
1347 return rc;
1348
1349 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1350 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1351 " have the desired protocol");
1352 return -EAGAIN;
1353 }
1354
1355 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1356 "0x%x", nfc_tgt.supported_protocols);
1357
1358 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1359
1360 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1361
1362 return 0;
1363}
1364
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001365static inline void pn533_poll_next_mod(struct pn533 *dev)
1366{
1367 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1368}
1369
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001370static void pn533_poll_reset_mod_list(struct pn533 *dev)
1371{
1372 dev->poll_mod_count = 0;
1373}
1374
1375static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1376{
1377 dev->poll_mod_active[dev->poll_mod_count] =
1378 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1379 dev->poll_mod_count++;
1380}
1381
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001382static void pn533_poll_create_mod_list(struct pn533 *dev,
1383 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001384{
1385 pn533_poll_reset_mod_list(dev);
1386
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001387 if (im_protocols & NFC_PROTO_MIFARE_MASK
1388 || im_protocols & NFC_PROTO_ISO14443_MASK
1389 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001390 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1391
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001392 if (im_protocols & NFC_PROTO_FELICA_MASK
1393 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001394 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1395 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1396 }
1397
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001398 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001399 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1400
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001401 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001402 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001403
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001404 if (tm_protocols)
1405 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001406}
1407
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001408static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001409{
1410 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001411 int rc;
1412
1413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1414
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001415 resp = (struct pn533_poll_response *) params;
1416 if (resp->nbtg) {
1417 rc = pn533_target_found(dev, resp, params_len);
1418
1419 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001420 if (rc == 0) {
1421 pn533_poll_reset_mod_list(dev);
1422 return 0;
1423 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001424 }
1425
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001426 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001427}
1428
Samuel Ortizad3823c2012-05-30 23:54:55 +02001429static int pn533_init_target_frame(struct pn533_frame *frame,
1430 u8 *gb, size_t gb_len)
1431{
1432 struct pn533_cmd_init_target *cmd;
1433 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001434 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1435 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1436 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1437 0xff, 0xff}; /* System code */
1438 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1439 0x0, 0x0, 0x0,
1440 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001441
1442 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1443 cmd = kzalloc(cmd_len, GFP_KERNEL);
1444 if (cmd == NULL)
1445 return -ENOMEM;
1446
1447 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1448
1449 /* DEP support only */
1450 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001451
1452 /* Felica params */
1453 memcpy(cmd->felica, felica_params, 18);
1454 get_random_bytes(cmd->felica + 2, 6);
1455
1456 /* NFCID3 */
1457 memset(cmd->nfcid3, 0, 10);
1458 memcpy(cmd->nfcid3, cmd->felica, 8);
1459
1460 /* MIFARE params */
1461 memcpy(cmd->mifare, mifare_params, 6);
1462
1463 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001464 cmd->gb_len = gb_len;
1465 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001466
Samuel Ortizad3823c2012-05-30 23:54:55 +02001467 /* Len Tk */
1468 cmd->gb[gb_len] = 0;
1469
1470 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001471
Samuel Ortizad3823c2012-05-30 23:54:55 +02001472 frame->datalen += cmd_len;
1473
1474 pn533_tx_frame_finish(frame);
1475
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001476 kfree(cmd);
1477
Samuel Ortizad3823c2012-05-30 23:54:55 +02001478 return 0;
1479}
1480
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001481#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001482#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1483static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1484 u8 *params, int params_len)
1485{
1486 struct sk_buff *skb_resp = arg;
1487 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1488
1489 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1490
1491 if (params_len < 0) {
1492 nfc_dev_err(&dev->interface->dev,
1493 "Error %d when starting as a target",
1494 params_len);
1495
1496 return params_len;
1497 }
1498
1499 if (params_len > 0 && params[0] != 0) {
1500 nfc_tm_deactivated(dev->nfc_dev);
1501
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001502 dev->tgt_mode = 0;
1503
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001504 kfree_skb(skb_resp);
1505 return 0;
1506 }
1507
1508 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001509 skb_pull(skb_resp,
1510 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1511 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001512
1513 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1514}
1515
1516static void pn533_wq_tg_get_data(struct work_struct *work)
1517{
1518 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1519 struct pn533_frame *in_frame;
1520 struct sk_buff *skb_resp;
1521 size_t skb_resp_len;
1522
1523 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1524
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001525 skb_resp_len = PN533_FRAME_HEADER_LEN +
1526 PN533_CMD_DATAEXCH_HEAD_LEN +
1527 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1528 PN533_FRAME_TAIL_LEN;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001529
1530 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1531 if (!skb_resp)
1532 return;
1533
1534 in_frame = (struct pn533_frame *)skb_resp->data;
1535
1536 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1537 pn533_tx_frame_finish(dev->out_frame);
1538
1539 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1540 skb_resp_len,
1541 pn533_tm_get_data_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001542 skb_resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001543
1544 return;
1545}
1546
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001547#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001548static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001549{
1550 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001551 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1552 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001553 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001554
1555 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1556
1557 if (params_len < 0) {
1558 nfc_dev_err(&dev->interface->dev,
1559 "Error %d when starting as a target",
1560 params_len);
1561
1562 return params_len;
1563 }
1564
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001565 if (params_len < ATR_REQ_GB_OFFSET + 1)
1566 return -EINVAL;
1567
Samuel Ortizad3823c2012-05-30 23:54:55 +02001568 resp = (struct pn533_cmd_init_target_response *) params;
1569
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001570 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1571 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001572
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001573 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1574 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1575 comm_mode = NFC_COMM_ACTIVE;
1576
1577 /* Again, only DEP */
1578 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1579 return -EOPNOTSUPP;
1580
1581 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1582 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1583
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001584 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1585 comm_mode, gb, gb_len);
1586 if (rc < 0) {
1587 nfc_dev_err(&dev->interface->dev,
1588 "Error when signaling target activation");
1589 return rc;
1590 }
1591
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001592 dev->tgt_mode = 1;
1593
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001594 queue_work(dev->wq, &dev->tg_work);
1595
1596 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001597}
1598
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001599static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001600{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001601 struct pn533 *dev = (struct pn533 *) data;
1602
1603 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1604
1605 /* An ack will cancel the last issued command (poll) */
1606 pn533_send_ack(dev, GFP_ATOMIC);
1607
1608 dev->cancel_listen = 1;
1609
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 pn533_poll_next_mod(dev);
1611
1612 queue_work(dev->wq, &dev->poll_work);
1613}
1614
1615static int pn533_poll_complete(struct pn533 *dev, void *arg,
1616 u8 *params, int params_len)
1617{
1618 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001619 int rc;
1620
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1622
1623 if (params_len == -ENOENT) {
1624 if (dev->poll_mod_count != 0)
1625 return 0;
1626
1627 nfc_dev_err(&dev->interface->dev,
1628 "Polling operation has been stopped");
1629
1630 goto stop_poll;
1631 }
1632
1633 if (params_len < 0) {
1634 nfc_dev_err(&dev->interface->dev,
1635 "Error %d when running poll", params_len);
1636
1637 goto stop_poll;
1638 }
1639
1640 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1641
1642 if (cur_mod->len == 0) {
1643 del_timer(&dev->listen_timer);
1644
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001645 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001647 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001648 if (!rc)
1649 return rc;
1650 }
1651
1652 pn533_poll_next_mod(dev);
1653
1654 queue_work(dev->wq, &dev->poll_work);
1655
1656 return 0;
1657
1658stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001659 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001660 dev->poll_protocols = 0;
1661 return 0;
1662}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001663
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001664static void pn533_build_poll_frame(struct pn533 *dev,
1665 struct pn533_frame *frame,
1666 struct pn533_poll_modulations *mod)
1667{
1668 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001669
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001670 if (mod->len == 0) {
1671 /* Listen mode */
1672 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1673 } else {
1674 /* Polling mode */
1675 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1676
1677 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1678 frame->datalen += mod->len;
1679
1680 pn533_tx_frame_finish(frame);
1681 }
1682}
1683
1684static int pn533_send_poll_frame(struct pn533 *dev)
1685{
1686 struct pn533_poll_modulations *cur_mod;
1687 int rc;
1688
1689 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1690
1691 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001692
1693 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001694 PN533_NORMAL_FRAME_MAX_LEN,
1695 pn533_poll_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01001696 NULL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001697 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001698 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001699
1700 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001701}
1702
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001703static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001705 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1706 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707 int rc;
1708
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001709 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1710
1711 nfc_dev_dbg(&dev->interface->dev,
1712 "%s cancel_listen %d modulation len %d",
1713 __func__, dev->cancel_listen, cur_mod->len);
1714
1715 if (dev->cancel_listen == 1) {
1716 dev->cancel_listen = 0;
1717 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718 }
1719
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001720 rc = pn533_send_poll_frame(dev);
1721 if (rc)
1722 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001723
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001724 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1725 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001726
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001727 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728}
1729
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001730static int pn533_start_poll(struct nfc_dev *nfc_dev,
1731 u32 im_protocols, u32 tm_protocols)
1732{
1733 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1734
1735 nfc_dev_dbg(&dev->interface->dev,
1736 "%s: im protocols 0x%x tm protocols 0x%x",
1737 __func__, im_protocols, tm_protocols);
1738
1739 if (dev->tgt_active_prot) {
1740 nfc_dev_err(&dev->interface->dev,
1741 "Cannot poll with a target already activated");
1742 return -EBUSY;
1743 }
1744
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001745 if (dev->tgt_mode) {
1746 nfc_dev_err(&dev->interface->dev,
1747 "Cannot poll while already being activated");
1748 return -EBUSY;
1749 }
1750
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001751 if (tm_protocols) {
1752 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1753 if (dev->gb == NULL)
1754 tm_protocols = 0;
1755 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001756
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001757 dev->poll_mod_curr = 0;
1758 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1759 dev->poll_protocols = im_protocols;
1760 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001761
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001762 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001763}
1764
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001765static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1766{
1767 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1768
1769 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1770
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001771 del_timer(&dev->listen_timer);
1772
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 if (!dev->poll_mod_count) {
1774 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1775 " running");
1776 return;
1777 }
1778
1779 /* An ack will cancel the last issued command (poll) */
1780 pn533_send_ack(dev, GFP_KERNEL);
1781
1782 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1783 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001784
1785 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001786}
1787
1788static int pn533_activate_target_nfcdep(struct pn533 *dev)
1789{
1790 struct pn533_cmd_activate_param param;
1791 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001792 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001793 int rc;
1794
1795 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1796
1797 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1798
1799 param.tg = 1;
1800 param.next = 0;
1801 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1802 sizeof(struct pn533_cmd_activate_param));
1803 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1804
1805 pn533_tx_frame_finish(dev->out_frame);
1806
1807 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001808 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001809 if (rc)
1810 return rc;
1811
1812 resp = (struct pn533_cmd_activate_response *)
1813 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1814 rc = resp->status & PN533_CMD_RET_MASK;
1815 if (rc != PN533_CMD_RET_SUCCESS)
1816 return -EIO;
1817
Samuel Ortiz541d9202011-12-14 16:43:10 +01001818 /* ATR_RES general bytes are located at offset 16 */
1819 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1820 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1821
1822 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823}
1824
Eric Lapuyade90099432012-05-07 12:31:13 +02001825static int pn533_activate_target(struct nfc_dev *nfc_dev,
1826 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001827{
1828 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1829 int rc;
1830
1831 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1832 protocol);
1833
1834 if (dev->poll_mod_count) {
1835 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1836 " polling");
1837 return -EBUSY;
1838 }
1839
1840 if (dev->tgt_active_prot) {
1841 nfc_dev_err(&dev->interface->dev, "There is already an active"
1842 " target");
1843 return -EBUSY;
1844 }
1845
1846 if (!dev->tgt_available_prots) {
1847 nfc_dev_err(&dev->interface->dev, "There is no available target"
1848 " to activate");
1849 return -EINVAL;
1850 }
1851
1852 if (!(dev->tgt_available_prots & (1 << protocol))) {
1853 nfc_dev_err(&dev->interface->dev, "The target does not support"
1854 " the requested protocol %u", protocol);
1855 return -EINVAL;
1856 }
1857
1858 if (protocol == NFC_PROTO_NFC_DEP) {
1859 rc = pn533_activate_target_nfcdep(dev);
1860 if (rc) {
1861 nfc_dev_err(&dev->interface->dev, "Error %d when"
1862 " activating target with"
1863 " NFC_DEP protocol", rc);
1864 return rc;
1865 }
1866 }
1867
1868 dev->tgt_active_prot = protocol;
1869 dev->tgt_available_prots = 0;
1870
1871 return 0;
1872}
1873
Eric Lapuyade90099432012-05-07 12:31:13 +02001874static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1875 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001876{
1877 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1878 u8 tg;
1879 u8 status;
1880 int rc;
1881
1882 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1883
1884 if (!dev->tgt_active_prot) {
1885 nfc_dev_err(&dev->interface->dev, "There is no active target");
1886 return;
1887 }
1888
1889 dev->tgt_active_prot = 0;
1890
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001891 skb_queue_purge(&dev->resp_q);
1892
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001893 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1894
1895 tg = 1;
1896 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1897 dev->out_frame->datalen += sizeof(u8);
1898
1899 pn533_tx_frame_finish(dev->out_frame);
1900
1901 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01001902 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001903 if (rc) {
1904 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1905 " command to the controller");
1906 return;
1907 }
1908
1909 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1910 rc = status & PN533_CMD_RET_MASK;
1911 if (rc != PN533_CMD_RET_SUCCESS)
1912 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1913 " the target", rc);
1914
1915 return;
1916}
1917
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001918
1919static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1920 u8 *params, int params_len)
1921{
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001922 struct pn533_cmd_jump_dep_response *resp;
1923 struct nfc_target nfc_target;
1924 u8 target_gt_len;
1925 int rc;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001926 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1927 u8 active = cmd->active;
1928
1929 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001930
1931 if (params_len == -ENOENT) {
1932 nfc_dev_dbg(&dev->interface->dev, "");
1933 return 0;
1934 }
1935
1936 if (params_len < 0) {
1937 nfc_dev_err(&dev->interface->dev,
1938 "Error %d when bringing DEP link up",
1939 params_len);
1940 return 0;
1941 }
1942
1943 if (dev->tgt_available_prots &&
1944 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1945 nfc_dev_err(&dev->interface->dev,
1946 "The target does not support DEP");
1947 return -EINVAL;
1948 }
1949
1950 resp = (struct pn533_cmd_jump_dep_response *) params;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001951 rc = resp->status & PN533_CMD_RET_MASK;
1952 if (rc != PN533_CMD_RET_SUCCESS) {
1953 nfc_dev_err(&dev->interface->dev,
1954 "Bringing DEP link up failed %d", rc);
1955 return 0;
1956 }
1957
1958 if (!dev->tgt_available_prots) {
1959 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1960
1961 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001962 nfc_target.nfcid1_len = 10;
1963 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001964 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1965 if (rc)
1966 return 0;
1967
1968 dev->tgt_available_prots = 0;
1969 }
1970
1971 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1972
1973 /* ATR_RES general bytes are located at offset 17 */
1974 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1975 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1976 resp->gt, target_gt_len);
1977 if (rc == 0)
1978 rc = nfc_dep_link_is_up(dev->nfc_dev,
1979 dev->nfc_dev->targets[0].idx,
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001980 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001981
1982 return 0;
1983}
1984
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001985static int pn533_mod_to_baud(struct pn533 *dev)
1986{
1987 switch (dev->poll_mod_curr) {
1988 case PN533_POLL_MOD_106KBPS_A:
1989 return 0;
1990 case PN533_POLL_MOD_212KBPS_FELICA:
1991 return 1;
1992 case PN533_POLL_MOD_424KBPS_FELICA:
1993 return 2;
1994 default:
1995 return -EINVAL;
1996 }
1997}
1998
Samuel Ortizd7f33452012-05-29 21:45:21 +02001999#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02002000static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01002001 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002002{
2003 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2004 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002005 u8 cmd_len, *data_ptr;
2006 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002007 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002008
2009 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2010
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002011 if (dev->poll_mod_count) {
2012 nfc_dev_err(&dev->interface->dev,
2013 "Cannot bring the DEP link up while polling");
2014 return -EBUSY;
2015 }
2016
2017 if (dev->tgt_active_prot) {
2018 nfc_dev_err(&dev->interface->dev,
2019 "There is already an active target");
2020 return -EBUSY;
2021 }
2022
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002023 baud = pn533_mod_to_baud(dev);
2024 if (baud < 0) {
2025 nfc_dev_err(&dev->interface->dev,
2026 "Invalid curr modulation %d", dev->poll_mod_curr);
2027 return baud;
2028 }
2029
Samuel Ortiz47807d32012-03-05 01:03:50 +01002030 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002031 if (comm_mode == NFC_COMM_PASSIVE)
2032 cmd_len += PASSIVE_DATA_LEN;
2033
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002034 cmd = kzalloc(cmd_len, GFP_KERNEL);
2035 if (cmd == NULL)
2036 return -ENOMEM;
2037
2038 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
2039
2040 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002041 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002042 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002043 data_ptr = cmd->data;
2044 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
2045 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
2046 cmd->next |= 1;
2047 data_ptr += PASSIVE_DATA_LEN;
2048 }
2049
Samuel Ortiz47807d32012-03-05 01:03:50 +01002050 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02002051 cmd->next |= 4; /* We have some Gi */
2052 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002053 } else {
2054 cmd->next = 0;
2055 }
2056
2057 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2058 dev->out_frame->datalen += cmd_len;
2059
2060 pn533_tx_frame_finish(dev->out_frame);
2061
2062 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002063 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002064 pn533_in_dep_link_up_complete, cmd);
Szymon Janc770f7502012-10-29 14:04:43 +01002065 if (rc < 0)
2066 kfree(cmd);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002067
2068 return rc;
2069}
2070
2071static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2072{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002073 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2074
2075 pn533_poll_reset_mod_list(dev);
2076
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002077 if (dev->tgt_mode || dev->tgt_active_prot) {
2078 pn533_send_ack(dev, GFP_KERNEL);
2079 usb_kill_urb(dev->in_urb);
2080 }
2081
2082 dev->tgt_active_prot = 0;
2083 dev->tgt_mode = 0;
2084
2085 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002086
2087 return 0;
2088}
2089
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002090static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2091 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002092{
2093 int payload_len = skb->len;
2094 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002095 u8 tg;
2096
2097 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2098 payload_len);
2099
2100 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2101 /* TODO: Implement support to multi-part data exchange */
2102 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2103 " max allowed: %d",
2104 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2105 return -ENOSYS;
2106 }
2107
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002108 skb_push(skb, PN533_FRAME_HEADER_LEN);
2109
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002110 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002111 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002112 case PN533_DEVICE_PASORI:
2113 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02002114 out_frame = (struct pn533_frame *) skb->data;
2115 pn533_tx_frame_init(out_frame,
2116 PN533_CMD_IN_COMM_THRU);
2117
2118 break;
2119 }
2120
2121 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002122 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2123 out_frame = (struct pn533_frame *) skb->data;
2124 pn533_tx_frame_init(out_frame,
2125 PN533_CMD_IN_DATA_EXCHANGE);
2126 tg = 1;
2127 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2128 &tg, sizeof(u8));
2129 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002130
2131 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002132 }
2133
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002134 } else {
2135 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2136 out_frame = (struct pn533_frame *) skb->data;
2137 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2138 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002139
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002140
2141 /* The data is already in the out_frame, just update the datalen */
2142 out_frame->datalen += payload_len;
2143
2144 pn533_tx_frame_finish(out_frame);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002145 skb_put(skb, PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002146
2147 return 0;
2148}
2149
2150struct pn533_data_exchange_arg {
2151 struct sk_buff *skb_resp;
2152 struct sk_buff *skb_out;
2153 data_exchange_cb_t cb;
2154 void *cb_context;
2155};
2156
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002157static struct sk_buff *pn533_build_response(struct pn533 *dev)
2158{
2159 struct sk_buff *skb, *tmp, *t;
2160 unsigned int skb_len = 0, tmp_len = 0;
2161
2162 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2163
2164 if (skb_queue_empty(&dev->resp_q))
2165 return NULL;
2166
2167 if (skb_queue_len(&dev->resp_q) == 1) {
2168 skb = skb_dequeue(&dev->resp_q);
2169 goto out;
2170 }
2171
2172 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2173 skb_len += tmp->len;
2174
2175 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2176 __func__, skb_len);
2177
2178 skb = alloc_skb(skb_len, GFP_KERNEL);
2179 if (skb == NULL)
2180 goto out;
2181
2182 skb_put(skb, skb_len);
2183
2184 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2185 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2186 tmp_len += tmp->len;
2187 }
2188
2189out:
2190 skb_queue_purge(&dev->resp_q);
2191
2192 return skb;
2193}
2194
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002195static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2196 u8 *params, int params_len)
2197{
2198 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002199 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002200 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2201 int err = 0;
2202 u8 status;
2203 u8 cmd_ret;
2204
2205 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2206
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002207 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002208
2209 if (params_len < 0) { /* error */
2210 err = params_len;
2211 goto error;
2212 }
2213
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002214 status = params[0];
2215
2216 cmd_ret = status & PN533_CMD_RET_MASK;
2217 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2218 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2219 " exchanging data", cmd_ret);
2220 err = -EIO;
2221 goto error;
2222 }
2223
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002224 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002225 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002226 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002227 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002228 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002229
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002230 if (status & PN533_CMD_MI_MASK) {
2231 queue_work(dev->wq, &dev->mi_work);
2232 return -EINPROGRESS;
2233 }
2234
2235 skb = pn533_build_response(dev);
2236 if (skb == NULL)
2237 goto error;
2238
2239 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002240 kfree(arg);
2241 return 0;
2242
2243error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002244 skb_queue_purge(&dev->resp_q);
2245 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002246 arg->cb(arg->cb_context, NULL, err);
2247 kfree(arg);
2248 return 0;
2249}
2250
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002251static int pn533_transceive(struct nfc_dev *nfc_dev,
2252 struct nfc_target *target, struct sk_buff *skb,
2253 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002254{
2255 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2256 struct pn533_frame *out_frame, *in_frame;
2257 struct pn533_data_exchange_arg *arg;
2258 struct sk_buff *skb_resp;
2259 int skb_resp_len;
2260 int rc;
2261
2262 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2263
2264 if (!dev->tgt_active_prot) {
2265 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2266 " there is no active target");
2267 rc = -EINVAL;
2268 goto error;
2269 }
2270
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002271 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002272 if (rc)
2273 goto error;
2274
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002275 skb_resp_len = PN533_FRAME_HEADER_LEN +
2276 PN533_CMD_DATAEXCH_HEAD_LEN +
2277 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2278 PN533_FRAME_TAIL_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002279
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002280 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002281 if (!skb_resp) {
2282 rc = -ENOMEM;
2283 goto error;
2284 }
2285
2286 in_frame = (struct pn533_frame *) skb_resp->data;
2287 out_frame = (struct pn533_frame *) skb->data;
2288
2289 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2290 if (!arg) {
2291 rc = -ENOMEM;
2292 goto free_skb_resp;
2293 }
2294
2295 arg->skb_resp = skb_resp;
2296 arg->skb_out = skb;
2297 arg->cb = cb;
2298 arg->cb_context = cb_context;
2299
2300 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002301 pn533_data_exchange_complete, arg);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002302 if (rc) {
2303 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2304 " perform data_exchange", rc);
2305 goto free_arg;
2306 }
2307
2308 return 0;
2309
2310free_arg:
2311 kfree(arg);
2312free_skb_resp:
2313 kfree_skb(skb_resp);
2314error:
2315 kfree_skb(skb);
2316 return rc;
2317}
2318
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002319static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2320 u8 *params, int params_len)
2321{
Thierry Escande5b412fd2012-11-15 18:24:28 +01002322 struct sk_buff *skb_out = arg;
2323
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002324 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2325
Thierry Escande5b412fd2012-11-15 18:24:28 +01002326 dev_kfree_skb(skb_out);
2327
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002328 if (params_len < 0) {
2329 nfc_dev_err(&dev->interface->dev,
2330 "Error %d when sending data",
2331 params_len);
2332
2333 return params_len;
2334 }
2335
2336 if (params_len > 0 && params[0] != 0) {
2337 nfc_tm_deactivated(dev->nfc_dev);
2338
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002339 dev->tgt_mode = 0;
2340
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002341 return 0;
2342 }
2343
2344 queue_work(dev->wq, &dev->tg_work);
2345
2346 return 0;
2347}
2348
2349static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2350{
2351 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2352 struct pn533_frame *out_frame;
2353 int rc;
2354
2355 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2356
2357 rc = pn533_build_tx_frame(dev, skb, false);
2358 if (rc)
2359 goto error;
2360
2361 out_frame = (struct pn533_frame *) skb->data;
2362
2363 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002364 PN533_NORMAL_FRAME_MAX_LEN,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002365 pn533_tm_send_complete, skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002366 if (rc) {
2367 nfc_dev_err(&dev->interface->dev,
2368 "Error %d when trying to send data", rc);
2369 goto error;
2370 }
2371
2372 return 0;
2373
2374error:
2375 kfree_skb(skb);
2376
2377 return rc;
2378}
2379
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002380static void pn533_wq_mi_recv(struct work_struct *work)
2381{
2382 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2383 struct sk_buff *skb_cmd;
2384 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2385 struct pn533_frame *out_frame, *in_frame;
2386 struct sk_buff *skb_resp;
2387 int skb_resp_len;
2388 int rc;
2389
2390 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2391
2392 /* This is a zero payload size skb */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002393 skb_cmd = alloc_skb(PN533_FRAME_HEADER_LEN +
2394 PN533_CMD_DATAEXCH_HEAD_LEN +
2395 PN533_FRAME_TAIL_LEN,
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002396 GFP_KERNEL);
2397 if (skb_cmd == NULL)
2398 goto error_cmd;
2399
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002400 skb_reserve(skb_cmd,
2401 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002402
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002403 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002404 if (rc)
2405 goto error_frame;
2406
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002407 skb_resp_len = PN533_FRAME_HEADER_LEN +
2408 PN533_CMD_DATAEXCH_HEAD_LEN +
2409 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2410 PN533_FRAME_TAIL_LEN;
2411
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002412 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2413 if (!skb_resp) {
2414 rc = -ENOMEM;
2415 goto error_frame;
2416 }
2417
2418 in_frame = (struct pn533_frame *) skb_resp->data;
2419 out_frame = (struct pn533_frame *) skb_cmd->data;
2420
2421 arg->skb_resp = skb_resp;
2422 arg->skb_out = skb_cmd;
2423
2424 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2425 skb_resp_len,
2426 pn533_data_exchange_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +01002427 dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002428 if (!rc)
2429 return;
2430
2431 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2432 " perform data_exchange", rc);
2433
2434 kfree_skb(skb_resp);
2435
2436error_frame:
2437 kfree_skb(skb_cmd);
2438
2439error_cmd:
2440 pn533_send_ack(dev, GFP_KERNEL);
2441
2442 kfree(arg);
2443
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002444 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002445}
2446
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002447static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2448 u8 cfgdata_len)
2449{
2450 int rc;
2451 u8 *params;
2452
2453 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2454
2455 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2456
2457 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2458 params[0] = cfgitem;
2459 memcpy(&params[1], cfgdata, cfgdata_len);
2460 dev->out_frame->datalen += (1 + cfgdata_len);
2461
2462 pn533_tx_frame_finish(dev->out_frame);
2463
2464 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002465 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466
2467 return rc;
2468}
2469
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002470static int pn533_fw_reset(struct pn533 *dev)
2471{
2472 int rc;
2473 u8 *params;
2474
2475 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2476
2477 pn533_tx_frame_init(dev->out_frame, 0x18);
2478
2479 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2480 params[0] = 0x1;
2481 dev->out_frame->datalen += 1;
2482
2483 pn533_tx_frame_finish(dev->out_frame);
2484
2485 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002486 PN533_NORMAL_FRAME_MAX_LEN);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002487
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002488 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002489}
2490
2491static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002492 .dev_up = NULL,
2493 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002494 .dep_link_up = pn533_dep_link_up,
2495 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002496 .start_poll = pn533_start_poll,
2497 .stop_poll = pn533_stop_poll,
2498 .activate_target = pn533_activate_target,
2499 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002500 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002501 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002502};
2503
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002504static int pn533_setup(struct pn533 *dev)
2505{
2506 struct pn533_config_max_retries max_retries;
2507 struct pn533_config_timing timing;
2508 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2509 int rc;
2510
2511 switch (dev->device_type) {
2512 case PN533_DEVICE_STD:
2513 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2514 max_retries.mx_rty_psl = 2;
2515 max_retries.mx_rty_passive_act =
2516 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2517
2518 timing.rfu = PN533_CONFIG_TIMING_102;
2519 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2520 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2521
2522 break;
2523
2524 case PN533_DEVICE_PASORI:
2525 max_retries.mx_rty_atr = 0x2;
2526 max_retries.mx_rty_psl = 0x1;
2527 max_retries.mx_rty_passive_act =
2528 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2529
2530 timing.rfu = PN533_CONFIG_TIMING_102;
2531 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2532 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2533
2534 break;
2535
2536 default:
2537 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2538 dev->device_type);
2539 return -EINVAL;
2540 }
2541
2542 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2543 (u8 *)&max_retries, sizeof(max_retries));
2544 if (rc) {
2545 nfc_dev_err(&dev->interface->dev,
2546 "Error on setting MAX_RETRIES config");
2547 return rc;
2548 }
2549
2550
2551 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2552 (u8 *)&timing, sizeof(timing));
2553 if (rc) {
2554 nfc_dev_err(&dev->interface->dev,
2555 "Error on setting RF timings");
2556 return rc;
2557 }
2558
2559 switch (dev->device_type) {
2560 case PN533_DEVICE_STD:
2561 break;
2562
2563 case PN533_DEVICE_PASORI:
2564 pn533_fw_reset(dev);
2565
2566 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2567 pasori_cfg, 3);
2568 if (rc) {
2569 nfc_dev_err(&dev->interface->dev,
2570 "Error while settings PASORI config");
2571 return rc;
2572 }
2573
2574 pn533_fw_reset(dev);
2575
2576 break;
2577 }
2578
2579 return 0;
2580}
2581
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002582static int pn533_probe(struct usb_interface *interface,
2583 const struct usb_device_id *id)
2584{
2585 struct pn533_fw_version *fw_ver;
2586 struct pn533 *dev;
2587 struct usb_host_interface *iface_desc;
2588 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002589 int in_endpoint = 0;
2590 int out_endpoint = 0;
2591 int rc = -ENOMEM;
2592 int i;
2593 u32 protocols;
2594
2595 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2596 if (!dev)
2597 return -ENOMEM;
2598
2599 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2600 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002601 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602
2603 iface_desc = interface->cur_altsetting;
2604 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2605 endpoint = &iface_desc->endpoint[i].desc;
2606
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002607 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002608 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002610 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002611 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002612 }
2613
2614 if (!in_endpoint || !out_endpoint) {
2615 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2616 " bulk-out endpoint");
2617 rc = -ENODEV;
2618 goto error;
2619 }
2620
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002621 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002622 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002623 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002624 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2625
2626 if (!dev->in_frame || !dev->out_frame ||
2627 !dev->in_urb || !dev->out_urb)
2628 goto error;
2629
2630 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2631 usb_rcvbulkpipe(dev->udev, in_endpoint),
2632 NULL, 0, NULL, dev);
2633 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2634 usb_sndbulkpipe(dev->udev, out_endpoint),
2635 NULL, 0,
2636 pn533_send_complete, dev);
2637
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002638 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2639 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002640 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002641 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002642 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002643 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002644 if (dev->wq == NULL)
2645 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002646
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002647 init_timer(&dev->listen_timer);
2648 dev->listen_timer.data = (unsigned long) dev;
2649 dev->listen_timer.function = pn533_listen_mode_timer;
2650
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002651 skb_queue_head_init(&dev->resp_q);
2652
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002653 INIT_LIST_HEAD(&dev->cmd_queue);
2654
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655 usb_set_intfdata(interface, dev);
2656
2657 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2658 pn533_tx_frame_finish(dev->out_frame);
2659
2660 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002661 PN533_NORMAL_FRAME_MAX_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002663 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002664
2665 fw_ver = (struct pn533_fw_version *)
2666 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2667 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2668 " attached", fw_ver->ver, fw_ver->rev);
2669
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002670 dev->device_type = id->driver_info;
2671 switch (dev->device_type) {
2672 case PN533_DEVICE_STD:
2673 protocols = PN533_ALL_PROTOCOLS;
2674 break;
2675
2676 case PN533_DEVICE_PASORI:
2677 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2678 break;
2679
2680 default:
2681 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2682 dev->device_type);
2683 rc = -EINVAL;
2684 goto destroy_wq;
2685 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002686
Samuel Ortize8753042011-08-19 15:47:11 +02002687 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002688 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002689 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002690 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002691 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002692 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002693
2694 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2695 nfc_set_drvdata(dev->nfc_dev, dev);
2696
2697 rc = nfc_register_device(dev->nfc_dev);
2698 if (rc)
2699 goto free_nfc_dev;
2700
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002701 rc = pn533_setup(dev);
2702 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002703 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002704
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002705 return 0;
2706
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002707unregister_nfc_dev:
2708 nfc_unregister_device(dev->nfc_dev);
2709
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002710free_nfc_dev:
2711 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002712
Samuel Ortiz4849f852012-04-10 19:43:17 +02002713destroy_wq:
2714 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002715error:
2716 kfree(dev->in_frame);
2717 usb_free_urb(dev->in_urb);
2718 kfree(dev->out_frame);
2719 usb_free_urb(dev->out_urb);
2720 kfree(dev);
2721 return rc;
2722}
2723
2724static void pn533_disconnect(struct usb_interface *interface)
2725{
2726 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002727 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002728
2729 dev = usb_get_intfdata(interface);
2730 usb_set_intfdata(interface, NULL);
2731
2732 nfc_unregister_device(dev->nfc_dev);
2733 nfc_free_device(dev->nfc_dev);
2734
2735 usb_kill_urb(dev->in_urb);
2736 usb_kill_urb(dev->out_urb);
2737
Samuel Ortiz4849f852012-04-10 19:43:17 +02002738 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002739
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002740 skb_queue_purge(&dev->resp_q);
2741
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002742 del_timer(&dev->listen_timer);
2743
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002744 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2745 list_del(&cmd->queue);
2746 kfree(cmd);
2747 }
2748
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002749 kfree(dev->in_frame);
2750 usb_free_urb(dev->in_urb);
2751 kfree(dev->out_frame);
2752 usb_free_urb(dev->out_urb);
2753 kfree(dev);
2754
Dan Carpenter276556d2011-07-08 10:21:15 +03002755 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002756}
2757
2758static struct usb_driver pn533_driver = {
2759 .name = "pn533",
2760 .probe = pn533_probe,
2761 .disconnect = pn533_disconnect,
2762 .id_table = pn533_table,
2763};
2764
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002765module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002766
2767MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2768 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2769MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2770MODULE_VERSION(VERSION);
2771MODULE_LICENSE("GPL");