blob: 5fb5b8f1ef3f9b83e44330b7db2ffe94cd31d952 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030031#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030032
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081/* frame definitions */
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
83 254 (DATA)
84 2 (DCS, postamble) */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010085#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020088
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010089/*
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
92 */
93#define PN533_FRAME_MAX_PAYLOAD_LEN 263
94
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030095#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010096 PN533_FRAME_TAIL_LEN)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030097#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
100
101/* start of frame */
102#define PN533_SOF 0x00FF
103
104/* frame identifier: in/out/error */
105#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106#define PN533_DIR_OUT 0xD4
107#define PN533_DIR_IN 0xD5
108
109/* PN533 Commands */
110#define PN533_FRAME_CMD(f) (f->data[1])
111#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
113
114#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115#define PN533_CMD_RF_CONFIGURATION 0x32
116#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200117#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300118#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119#define PN533_CMD_IN_ATR 0x50
120#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100121#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122
Samuel Ortizad3823c2012-05-30 23:54:55 +0200123#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200124#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200125#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100126#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300128#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
129
130/* PN533 Return codes */
131#define PN533_CMD_RET_MASK 0x3F
132#define PN533_CMD_MI_MASK 0x40
133#define PN533_CMD_RET_SUCCESS 0x00
134
135struct pn533;
136
137typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
139
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100140typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143/* structs for pn533 commands */
144
145/* PN533_CMD_GET_FIRMWARE_VERSION */
146struct pn533_fw_version {
147 u8 ic;
148 u8 ver;
149 u8 rev;
150 u8 support;
151};
152
153/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200154#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200156#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200158#define PN533_CONFIG_TIMING_102 0xb
159#define PN533_CONFIG_TIMING_204 0xc
160#define PN533_CONFIG_TIMING_409 0xd
161#define PN533_CONFIG_TIMING_819 0xe
162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300163#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
165
166struct pn533_config_max_retries {
167 u8 mx_rty_atr;
168 u8 mx_rty_psl;
169 u8 mx_rty_passive_act;
170} __packed;
171
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200172struct pn533_config_timing {
173 u8 rfu;
174 u8 atr_res_timeout;
175 u8 dep_timeout;
176} __packed;
177
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300178/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
179
180/* felica commands opcode */
181#define PN533_FELICA_OPC_SENSF_REQ 0
182#define PN533_FELICA_OPC_SENSF_RES 1
183/* felica SENSF_REQ parameters */
184#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
188
189/* type B initiator_data values */
190#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
193
194union pn533_cmd_poll_initdata {
195 struct {
196 u8 afi;
197 u8 polling_method;
198 } __packed type_b;
199 struct {
200 u8 opcode;
201 __be16 sc;
202 u8 rc;
203 u8 tsn;
204 } __packed felica;
205};
206
207/* Poll modulations */
208enum {
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200214 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300215
216 __PN533_POLL_MOD_AFTER_LAST,
217};
218#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
219
220struct pn533_poll_modulations {
221 struct {
222 u8 maxtg;
223 u8 brty;
224 union pn533_cmd_poll_initdata initiator_data;
225 } __packed data;
226 u8 len;
227};
228
229const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 0,
234 },
235 .len = 2,
236 },
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
238 .data = {
239 .maxtg = 1,
240 .brty = 1,
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
245 .tsn = 0,
246 },
247 },
248 .len = 7,
249 },
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
251 .data = {
252 .maxtg = 1,
253 .brty = 2,
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
258 .tsn = 0,
259 },
260 },
261 .len = 7,
262 },
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 4,
267 },
268 .len = 2,
269 },
270 [PN533_POLL_MOD_847KBPS_B] = {
271 .data = {
272 .maxtg = 1,
273 .brty = 8,
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
276 .polling_method =
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
278 },
279 },
280 .len = 3,
281 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200282 [PN533_LISTEN_MOD] = {
283 .len = 0,
284 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300285};
286
287/* PN533_CMD_IN_ATR */
288
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300289struct pn533_cmd_activate_response {
290 u8 status;
291 u8 nfcid3t[10];
292 u8 didt;
293 u8 bst;
294 u8 brt;
295 u8 to;
296 u8 ppt;
297 /* optional */
298 u8 gt[];
299} __packed;
300
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100301struct pn533_cmd_jump_dep_response {
302 u8 status;
303 u8 tg;
304 u8 nfcid3t[10];
305 u8 didt;
306 u8 bst;
307 u8 brt;
308 u8 to;
309 u8 ppt;
310 /* optional */
311 u8 gt[];
312} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313
Samuel Ortizad3823c2012-05-30 23:54:55 +0200314
315/* PN533_TG_INIT_AS_TARGET */
316#define PN533_INIT_TARGET_PASSIVE 0x1
317#define PN533_INIT_TARGET_DEP 0x2
318
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200319#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321#define PN533_INIT_TARGET_RESP_DEP 0x4
322
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300323struct pn533 {
324 struct usb_device *udev;
325 struct usb_interface *interface;
326 struct nfc_dev *nfc_dev;
327
328 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300329 struct pn533_frame *out_frame;
330
331 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200333 struct sk_buff_head resp_q;
334
Samuel Ortiz4849f852012-04-10 19:43:17 +0200335 struct workqueue_struct *wq;
336 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200337 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200338 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200339 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200340 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200341 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200342 struct pn533_frame *wq_in_frame;
343 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200344 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300345
346 pn533_cmd_complete_t cmd_complete;
347 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100348 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200349 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350 u8 cmd;
351
352 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
353 u8 poll_mod_count;
354 u8 poll_mod_curr;
355 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200356 u32 listen_protocols;
357
358 u8 *gb;
359 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300360
361 u8 tgt_available_prots;
362 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200363 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200364
365 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200366
367 struct list_head cmd_queue;
368 u8 cmd_pending;
369};
370
371struct pn533_cmd {
372 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100373 u8 cmd_code;
374 struct sk_buff *req;
375 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200376 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300377};
378
379struct pn533_frame {
380 u8 preamble;
381 __be16 start_frame;
382 u8 datalen;
383 u8 datalen_checksum;
384 u8 data[];
385} __packed;
386
387/* The rule: value + checksum = 0 */
388static inline u8 pn533_checksum(u8 value)
389{
390 return ~value + 1;
391}
392
393/* The rule: sum(data elements) + checksum = 0 */
394static u8 pn533_data_checksum(u8 *data, int datalen)
395{
396 u8 sum = 0;
397 int i;
398
399 for (i = 0; i < datalen; i++)
400 sum += data[i];
401
402 return pn533_checksum(sum);
403}
404
405/**
406 * pn533_tx_frame_ack - create a ack frame
407 * @frame: The frame to be set as ack
408 *
409 * Ack is different type of standard frame. As a standard frame, it has
410 * preamble and start_frame. However the checksum of this frame must fail,
411 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
412 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
413 * After datalen_checksum field, the postamble is placed.
414 */
415static void pn533_tx_frame_ack(struct pn533_frame *frame)
416{
417 frame->preamble = 0;
418 frame->start_frame = cpu_to_be16(PN533_SOF);
419 frame->datalen = 0;
420 frame->datalen_checksum = 0xFF;
421 /* data[0] is used as postamble */
422 frame->data[0] = 0;
423}
424
425static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
426{
427 frame->preamble = 0;
428 frame->start_frame = cpu_to_be16(PN533_SOF);
429 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
430 PN533_FRAME_CMD(frame) = cmd;
431 frame->datalen = 2;
432}
433
434static void pn533_tx_frame_finish(struct pn533_frame *frame)
435{
436 frame->datalen_checksum = pn533_checksum(frame->datalen);
437
438 PN533_FRAME_CHECKSUM(frame) =
439 pn533_data_checksum(frame->data, frame->datalen);
440
441 PN533_FRAME_POSTAMBLE(frame) = 0;
442}
443
444static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
445{
446 u8 checksum;
447
448 if (frame->start_frame != cpu_to_be16(PN533_SOF))
449 return false;
450
451 checksum = pn533_checksum(frame->datalen);
452 if (checksum != frame->datalen_checksum)
453 return false;
454
455 checksum = pn533_data_checksum(frame->data, frame->datalen);
456 if (checksum != PN533_FRAME_CHECKSUM(frame))
457 return false;
458
459 return true;
460}
461
462static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
463{
464 if (frame->start_frame != cpu_to_be16(PN533_SOF))
465 return false;
466
467 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
468 return false;
469
470 return true;
471}
472
473static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
474{
475 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
476}
477
Samuel Ortiz4849f852012-04-10 19:43:17 +0200478
479static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300480{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200481 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200482 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300483 int rc;
484
Samuel Ortiz4849f852012-04-10 19:43:17 +0200485 in_frame = dev->wq_in_frame;
486
487 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300488 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200489 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300490 else
491 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
492 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
493 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
494
495 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200496 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300497}
498
499static void pn533_recv_response(struct urb *urb)
500{
501 struct pn533 *dev = urb->context;
502 struct pn533_frame *in_frame;
503
Samuel Ortiz4849f852012-04-10 19:43:17 +0200504 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300505
506 switch (urb->status) {
507 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100508 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300509 case -ECONNRESET:
510 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100511 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100512 "The urb has been canceled (status %d)",
513 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200514 dev->wq_in_error = urb->status;
515 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100516 break;
517 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300518 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100519 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100520 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200521 dev->wq_in_error = urb->status;
522 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523 }
524
525 in_frame = dev->in_urb->transfer_buffer;
526
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100527 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100528 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
529 in_frame, PN533_FRAME_SIZE(in_frame), false);
530
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300531 if (!pn533_rx_frame_is_valid(in_frame)) {
532 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200533 dev->wq_in_error = -EIO;
534 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300535 }
536
537 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100538 nfc_dev_err(&dev->interface->dev,
539 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200540 dev->wq_in_error = -EIO;
541 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300542 }
543
Samuel Ortiz4849f852012-04-10 19:43:17 +0200544 dev->wq_in_error = 0;
545 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300546
Samuel Ortiz4849f852012-04-10 19:43:17 +0200547sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200548 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300549}
550
551static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
552{
553 dev->in_urb->complete = pn533_recv_response;
554
555 return usb_submit_urb(dev->in_urb, flags);
556}
557
558static void pn533_recv_ack(struct urb *urb)
559{
560 struct pn533 *dev = urb->context;
561 struct pn533_frame *in_frame;
562 int rc;
563
564 switch (urb->status) {
565 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100566 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567 case -ECONNRESET:
568 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100569 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100570 "The urb has been stopped (status %d)",
571 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200572 dev->wq_in_error = urb->status;
573 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100574 break;
575 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300576 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100577 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100578 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200579 dev->wq_in_error = urb->status;
580 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300581 }
582
583 in_frame = dev->in_urb->transfer_buffer;
584
585 if (!pn533_rx_frame_is_ack(in_frame)) {
586 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200587 dev->wq_in_error = -EIO;
588 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300589 }
590
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300591 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
592 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100593 nfc_dev_err(&dev->interface->dev,
594 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200595 dev->wq_in_error = rc;
596 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300597 }
598
599 return;
600
Samuel Ortiz4849f852012-04-10 19:43:17 +0200601sched_wq:
602 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200603 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300604}
605
606static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
607{
608 dev->in_urb->complete = pn533_recv_ack;
609
610 return usb_submit_urb(dev->in_urb, flags);
611}
612
613static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
614{
615 int rc;
616
617 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
618
619 pn533_tx_frame_ack(dev->out_frame);
620
621 dev->out_urb->transfer_buffer = dev->out_frame;
622 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
623 rc = usb_submit_urb(dev->out_urb, flags);
624
625 return rc;
626}
627
628static int __pn533_send_cmd_frame_async(struct pn533 *dev,
629 struct pn533_frame *out_frame,
630 struct pn533_frame *in_frame,
631 int in_frame_len,
632 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100633 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300634{
635 int rc;
636
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300637 dev->cmd = PN533_FRAME_CMD(out_frame);
638 dev->cmd_complete = cmd_complete;
639 dev->cmd_complete_arg = arg;
640
641 dev->out_urb->transfer_buffer = out_frame;
642 dev->out_urb->transfer_buffer_length =
643 PN533_FRAME_SIZE(out_frame);
644
645 dev->in_urb->transfer_buffer = in_frame;
646 dev->in_urb->transfer_buffer_length = in_frame_len;
647
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100648 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
649 out_frame, PN533_FRAME_SIZE(out_frame), false);
650
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100651 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300652 if (rc)
653 return rc;
654
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100655 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300656 if (rc)
657 goto error;
658
659 return 0;
660
661error:
662 usb_unlink_urb(dev->out_urb);
663 return rc;
664}
665
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100666static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
667{
668 struct pn533_frame *frame;
669 /* payload is already there, just update datalen */
670 int payload_len = skb->len;
671
672 skb_push(skb, PN533_FRAME_HEADER_LEN);
673 skb_put(skb, PN533_FRAME_TAIL_LEN);
674
675 frame = (struct pn533_frame *)skb->data;
676
677 pn533_tx_frame_init(frame, cmd_code);
678 frame->datalen += payload_len;
679 pn533_tx_frame_finish(frame);
680}
681
682struct pn533_send_async_complete_arg {
683 pn533_send_async_complete_t complete_cb;
684 void *complete_cb_context;
685 struct sk_buff *resp;
686 struct sk_buff *req;
687};
688
689static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
690 int params_len)
691{
692 struct pn533_send_async_complete_arg *arg = _arg;
693
694 struct sk_buff *req = arg->req;
695 struct sk_buff *resp = arg->resp;
696
697 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
698 int rc;
699
700 dev_kfree_skb(req);
701
702 if (params_len < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100703 arg->complete_cb(dev, arg->complete_cb_context,
704 ERR_PTR(params_len));
705 rc = params_len;
706 dev_kfree_skb(resp);
707 goto out;
708 }
709
710 skb_put(resp, PN533_FRAME_SIZE(frame));
711 skb_pull(resp, PN533_FRAME_HEADER_LEN);
712 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
713
714 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
715
716out:
717 kfree(arg);
718 return rc;
719}
720
721static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
722 struct sk_buff *req, struct sk_buff *resp,
723 int resp_len,
724 pn533_send_async_complete_t complete_cb,
725 void *complete_cb_context)
726{
727 struct pn533_cmd *cmd;
728 struct pn533_send_async_complete_arg *arg;
729 int rc = 0;
730
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100731 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100732
733 arg = kzalloc(sizeof(arg), GFP_KERNEL);
734 if (!arg)
735 return -ENOMEM;
736
737 arg->complete_cb = complete_cb;
738 arg->complete_cb_context = complete_cb_context;
739 arg->resp = resp;
740 arg->req = req;
741
742 pn533_build_cmd_frame(cmd_code, req);
743
744 mutex_lock(&dev->cmd_lock);
745
746 if (!dev->cmd_pending) {
747 rc = __pn533_send_cmd_frame_async(dev,
748 (struct pn533_frame *)req->data,
749 (struct pn533_frame *)resp->data,
750 resp_len, pn533_send_async_complete,
751 arg);
752 if (rc)
753 goto error;
754
755 dev->cmd_pending = 1;
756 goto unlock;
757 }
758
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100759 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
760 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100761
762 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
763 if (!cmd) {
764 rc = -ENOMEM;
765 goto error;
766 }
767
768 INIT_LIST_HEAD(&cmd->queue);
769 cmd->cmd_code = cmd_code;
770 cmd->req = req;
771 cmd->resp = resp;
772 cmd->arg = arg;
773
774 list_add_tail(&cmd->queue, &dev->cmd_queue);
775
776 goto unlock;
777
778error:
779 kfree(arg);
780unlock:
781 mutex_unlock(&dev->cmd_lock);
782 return rc;
783}
784
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100785static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
786 struct sk_buff *req,
787 pn533_send_async_complete_t complete_cb,
788 void *complete_cb_context)
789{
790 struct sk_buff *resp;
791 int rc;
792 int resp_len = PN533_FRAME_HEADER_LEN +
793 PN533_FRAME_MAX_PAYLOAD_LEN +
794 PN533_FRAME_TAIL_LEN;
795
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100796 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
797 if (!resp)
798 return -ENOMEM;
799
800 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
801 complete_cb_context);
802 if (rc)
803 dev_kfree_skb(resp);
804
805 return rc;
806}
807
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100808static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
809 struct sk_buff *req,
810 pn533_send_async_complete_t complete_cb,
811 void *complete_cb_context)
812{
813 struct sk_buff *resp;
814 int rc;
815
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100816 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
817 if (!resp)
818 return -ENOMEM;
819
820 rc = __pn533_send_async(dev, cmd_code, req, resp,
821 PN533_NORMAL_FRAME_MAX_LEN,
822 complete_cb, complete_cb_context);
823 if (rc)
824 dev_kfree_skb(resp);
825
826 return rc;
827}
828
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100829/*
830 * pn533_send_cmd_direct_async
831 *
832 * The function sends a piority cmd directly to the chip omiting the cmd
833 * queue. It's intended to be used by chaining mechanism of received responses
834 * where the host has to request every single chunk of data before scheduling
835 * next cmd from the queue.
836 */
837static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
838 struct sk_buff *req,
839 pn533_send_async_complete_t complete_cb,
840 void *complete_cb_context)
841{
842 struct pn533_send_async_complete_arg *arg;
843 struct sk_buff *resp;
844 int rc;
845 int resp_len = PN533_FRAME_HEADER_LEN +
846 PN533_FRAME_MAX_PAYLOAD_LEN +
847 PN533_FRAME_TAIL_LEN;
848
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100849 resp = alloc_skb(resp_len, GFP_KERNEL);
850 if (!resp)
851 return -ENOMEM;
852
853 arg = kzalloc(sizeof(arg), GFP_KERNEL);
854 if (!arg) {
855 dev_kfree_skb(resp);
856 return -ENOMEM;
857 }
858
859 arg->complete_cb = complete_cb;
860 arg->complete_cb_context = complete_cb_context;
861 arg->resp = resp;
862 arg->req = req;
863
864 pn533_build_cmd_frame(cmd_code, req);
865
866 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
867 (struct pn533_frame *)resp->data,
868 resp_len, pn533_send_async_complete,
869 arg);
870 if (rc < 0) {
871 dev_kfree_skb(resp);
872 kfree(arg);
873 }
874
875 return rc;
876}
877
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200878static void pn533_wq_cmd(struct work_struct *work)
879{
880 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
881 struct pn533_cmd *cmd;
882
883 mutex_lock(&dev->cmd_lock);
884
885 if (list_empty(&dev->cmd_queue)) {
886 dev->cmd_pending = 0;
887 mutex_unlock(&dev->cmd_lock);
888 return;
889 }
890
891 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
892
Szymon Janc60ad07a2012-10-25 17:29:45 +0200893 list_del(&cmd->queue);
894
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200895 mutex_unlock(&dev->cmd_lock);
896
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +0100897 __pn533_send_cmd_frame_async(dev,
898 (struct pn533_frame *)cmd->req->data,
899 (struct pn533_frame *)cmd->resp->data,
900 PN533_NORMAL_FRAME_MAX_LEN,
901 pn533_send_async_complete,
902 cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200903
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200904 kfree(cmd);
905}
906
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300907struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100908 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300909 struct completion done;
910};
911
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100912static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
913 struct sk_buff *resp)
914{
915 struct pn533_sync_cmd_response *arg = _arg;
916
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100917 arg->resp = resp;
918 complete(&arg->done);
919
920 return 0;
921}
922
923/* pn533_send_cmd_sync
924 *
925 * Please note the req parameter is freed inside the function to
926 * limit a number of return value interpretations by the caller.
927 *
928 * 1. negative in case of error during TX path -> req should be freed
929 *
930 * 2. negative in case of error during RX path -> req should not be freed
931 * as it's been already freed at the begining of RX path by
932 * async_complete_cb.
933 *
934 * 3. valid pointer in case of succesfult RX path
935 *
936 * A caller has to check a return value with IS_ERR macro. If the test pass,
937 * the returned pointer is valid.
938 *
939 * */
940static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
941 struct sk_buff *req)
942{
943 int rc;
944 struct pn533_sync_cmd_response arg;
945
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100946 init_completion(&arg.done);
947
948 rc = pn533_send_cmd_async(dev, cmd_code, req,
949 pn533_send_sync_complete, &arg);
950 if (rc) {
951 dev_kfree_skb(req);
952 return ERR_PTR(rc);
953 }
954
955 wait_for_completion(&arg.done);
956
957 return arg.resp;
958}
959
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300960static void pn533_send_complete(struct urb *urb)
961{
962 struct pn533 *dev = urb->context;
963
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300964 switch (urb->status) {
965 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100966 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300967 case -ECONNRESET:
968 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100969 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100970 "The urb has been stopped (status %d)",
971 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300972 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100973 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300974 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100975 nfc_dev_err(&dev->interface->dev,
976 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300977 }
978}
979
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100980static struct sk_buff *pn533_alloc_skb(unsigned int size)
981{
982 struct sk_buff *skb;
983
984 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
985 size +
986 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
987
988 if (skb)
989 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
990
991 return skb;
992}
993
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300994struct pn533_target_type_a {
995 __be16 sens_res;
996 u8 sel_res;
997 u8 nfcid_len;
998 u8 nfcid_data[];
999} __packed;
1000
1001
1002#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1003#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1004#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1005
1006#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1007#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1008
1009#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1010#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1011
1012#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1013#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1014#define PN533_TYPE_A_SEL_PROT_DEP 2
1015#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1016
1017static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1018 int target_data_len)
1019{
1020 u8 ssd;
1021 u8 platconf;
1022
1023 if (target_data_len < sizeof(struct pn533_target_type_a))
1024 return false;
1025
1026 /* The lenght check of nfcid[] and ats[] are not being performed because
1027 the values are not being used */
1028
1029 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1030 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1031 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1032
1033 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001034 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1035 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1036 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001037 return false;
1038
1039 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1040 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1041 return false;
1042
1043 return true;
1044}
1045
1046static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1047 int tgt_data_len)
1048{
1049 struct pn533_target_type_a *tgt_type_a;
1050
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001051 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001052
1053 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1054 return -EPROTO;
1055
1056 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1057 case PN533_TYPE_A_SEL_PROT_MIFARE:
1058 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1059 break;
1060 case PN533_TYPE_A_SEL_PROT_ISO14443:
1061 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1062 break;
1063 case PN533_TYPE_A_SEL_PROT_DEP:
1064 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1065 break;
1066 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1067 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1068 NFC_PROTO_NFC_DEP_MASK;
1069 break;
1070 }
1071
1072 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1073 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001074 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1075 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001076
1077 return 0;
1078}
1079
1080struct pn533_target_felica {
1081 u8 pol_res;
1082 u8 opcode;
1083 u8 nfcid2[8];
1084 u8 pad[8];
1085 /* optional */
1086 u8 syst_code[];
1087} __packed;
1088
1089#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1090#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1091
1092static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1093 int target_data_len)
1094{
1095 if (target_data_len < sizeof(struct pn533_target_felica))
1096 return false;
1097
1098 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1099 return false;
1100
1101 return true;
1102}
1103
1104static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1105 int tgt_data_len)
1106{
1107 struct pn533_target_felica *tgt_felica;
1108
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001109 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001110
1111 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1112 return -EPROTO;
1113
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001114 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1115 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001116 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1117 else
1118 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1119
Samuel Ortiz79757542012-03-05 01:03:45 +01001120 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1121 nfc_tgt->sensf_res_len = 9;
1122
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001123 return 0;
1124}
1125
1126struct pn533_target_jewel {
1127 __be16 sens_res;
1128 u8 jewelid[4];
1129} __packed;
1130
1131static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1132 int target_data_len)
1133{
1134 u8 ssd;
1135 u8 platconf;
1136
1137 if (target_data_len < sizeof(struct pn533_target_jewel))
1138 return false;
1139
1140 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1141 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1142 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1143
1144 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001145 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1146 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1147 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001148 return false;
1149
1150 return true;
1151}
1152
1153static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1154 int tgt_data_len)
1155{
1156 struct pn533_target_jewel *tgt_jewel;
1157
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001158 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001159
1160 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1161 return -EPROTO;
1162
1163 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1164 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001165 nfc_tgt->nfcid1_len = 4;
1166 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001167
1168 return 0;
1169}
1170
1171struct pn533_type_b_prot_info {
1172 u8 bitrate;
1173 u8 fsci_type;
1174 u8 fwi_adc_fo;
1175} __packed;
1176
1177#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1178#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1179#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1180
1181struct pn533_type_b_sens_res {
1182 u8 opcode;
1183 u8 nfcid[4];
1184 u8 appdata[4];
1185 struct pn533_type_b_prot_info prot_info;
1186} __packed;
1187
1188#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1189
1190struct pn533_target_type_b {
1191 struct pn533_type_b_sens_res sensb_res;
1192 u8 attrib_res_len;
1193 u8 attrib_res[];
1194} __packed;
1195
1196static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1197 int target_data_len)
1198{
1199 if (target_data_len < sizeof(struct pn533_target_type_b))
1200 return false;
1201
1202 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1203 return false;
1204
1205 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1206 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1207 return false;
1208
1209 return true;
1210}
1211
1212static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1213 int tgt_data_len)
1214{
1215 struct pn533_target_type_b *tgt_type_b;
1216
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001217 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001218
1219 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1220 return -EPROTO;
1221
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001222 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001223
1224 return 0;
1225}
1226
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001227static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1228 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001230 struct nfc_target nfc_tgt;
1231 int rc;
1232
1233 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001234 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001235
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001236 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001237 return -EPROTO;
1238
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001239 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1240
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001241 switch (dev->poll_mod_curr) {
1242 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001243 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001244 break;
1245 case PN533_POLL_MOD_212KBPS_FELICA:
1246 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001247 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001248 break;
1249 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001250 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001251 break;
1252 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001253 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001254 break;
1255 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 nfc_dev_err(&dev->interface->dev,
1257 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001258 return -EPROTO;
1259 }
1260
1261 if (rc)
1262 return rc;
1263
1264 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001265 nfc_dev_dbg(&dev->interface->dev,
1266 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001267 return -EAGAIN;
1268 }
1269
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001270 nfc_dev_dbg(&dev->interface->dev,
1271 "Target found - supported protocols: 0x%x",
1272 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001273
1274 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1275
1276 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1277
1278 return 0;
1279}
1280
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001281static inline void pn533_poll_next_mod(struct pn533 *dev)
1282{
1283 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1284}
1285
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001286static void pn533_poll_reset_mod_list(struct pn533 *dev)
1287{
1288 dev->poll_mod_count = 0;
1289}
1290
1291static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1292{
1293 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001294 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295 dev->poll_mod_count++;
1296}
1297
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001298static void pn533_poll_create_mod_list(struct pn533 *dev,
1299 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001300{
1301 pn533_poll_reset_mod_list(dev);
1302
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001303 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1304 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1305 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1307
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001308 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1309 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1311 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1312 }
1313
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001314 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001315 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1316
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001317 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001319
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001320 if (tm_protocols)
1321 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001322}
1323
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001324static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001326 u8 nbtg, tg, *tgdata;
1327 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328
1329 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1330
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001331 nbtg = resp->data[0];
1332 tg = resp->data[1];
1333 tgdata = &resp->data[2];
1334 tgdata_len = resp->len - 2; /* nbtg + tg */
1335
1336 if (nbtg) {
1337 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001338
1339 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001340 if (rc == 0) {
1341 pn533_poll_reset_mod_list(dev);
1342 return 0;
1343 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001344 }
1345
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001346 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001347}
1348
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001349static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001350{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001351 struct sk_buff *skb;
1352 u8 *felica, *nfcid3, *gb;
1353
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001354 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1355 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1356 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1357 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001358
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001359 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1360 0x0, 0x0, 0x0,
1361 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001362
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001363 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1364 felica (18), nfcid3 (10), gb_len (1) */
1365 gbytes_len +
1366 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001367
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001368 skb = pn533_alloc_skb(skb_len);
1369 if (!skb)
1370 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001371
1372 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001373 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001374
1375 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001376 memcpy(skb_put(skb, 6), mifare_params, 6);
1377
1378 /* Felica params */
1379 felica = skb_put(skb, 18);
1380 memcpy(felica, felica_params, 18);
1381 get_random_bytes(felica + 2, 6);
1382
1383 /* NFCID3 */
1384 nfcid3 = skb_put(skb, 10);
1385 memset(nfcid3, 0, 10);
1386 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001387
1388 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001389 *skb_put(skb, 1) = gbytes_len;
1390
1391 gb = skb_put(skb, gbytes_len);
1392 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001393
Samuel Ortizad3823c2012-05-30 23:54:55 +02001394 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001395 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001396
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001397 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001398}
1399
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001400#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001401#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1402static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001403 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001404{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001405 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001406
1407 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1408
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409 if (IS_ERR(resp))
1410 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001411
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001412 status = resp->data[0];
1413 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001414
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001415 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001416 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001417 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001418 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001419 return 0;
1420 }
1421
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001422 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001423}
1424
1425static void pn533_wq_tg_get_data(struct work_struct *work)
1426{
1427 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001428
1429 struct sk_buff *skb;
1430 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001431
1432 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1433
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001434 skb = pn533_alloc_skb(0);
1435 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001436 return;
1437
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001438 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1439 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001440
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001441 if (rc < 0)
1442 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001443
1444 return;
1445}
1446
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001447#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001448static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001449{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001451 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001452 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001453
1454 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1455
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001457 return -EINVAL;
1458
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001459 mode = resp->data[0];
1460 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001461
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1463 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001464
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001465 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1466 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001467 comm_mode = NFC_COMM_ACTIVE;
1468
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001469 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001470 return -EOPNOTSUPP;
1471
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001472 gb = cmd + ATR_REQ_GB_OFFSET;
1473 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001474
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001475 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1476 comm_mode, gb, gb_len);
1477 if (rc < 0) {
1478 nfc_dev_err(&dev->interface->dev,
1479 "Error when signaling target activation");
1480 return rc;
1481 }
1482
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001483 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001484 queue_work(dev->wq, &dev->tg_work);
1485
1486 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001487}
1488
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001489static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001490{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001491 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001492
1493 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1494
1495 /* An ack will cancel the last issued command (poll) */
1496 pn533_send_ack(dev, GFP_ATOMIC);
1497
1498 dev->cancel_listen = 1;
1499
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001500 pn533_poll_next_mod(dev);
1501
1502 queue_work(dev->wq, &dev->poll_work);
1503}
1504
1505static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001506 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001507{
1508 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001509 int rc;
1510
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001511 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1512
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001513 if (IS_ERR(resp)) {
1514 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001515
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001516 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1517 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001519 if (rc == -ENOENT) {
1520 if (dev->poll_mod_count != 0)
1521 return rc;
1522 else
1523 goto stop_poll;
1524 } else if (rc < 0) {
1525 nfc_dev_err(&dev->interface->dev,
1526 "Error %d when running poll", rc);
1527 goto stop_poll;
1528 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001529 }
1530
1531 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1532
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001533 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001534 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001535 rc = pn533_init_target_complete(dev, resp);
1536 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001537 }
1538
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001539 /* Initiator mode */
1540 rc = pn533_start_poll_complete(dev, resp);
1541 if (!rc)
1542 goto done;
1543
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001545 queue_work(dev->wq, &dev->poll_work);
1546
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001547done:
1548 dev_kfree_skb(resp);
1549 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001550
1551stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001552 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1553
Samuel Ortizad3823c2012-05-30 23:54:55 +02001554 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001555 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001556 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001558
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001559static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1560 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001561{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001562 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001563
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001564 skb = pn533_alloc_skb(mod->len);
1565 if (!skb)
1566 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001567
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001568 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001569
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001570 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571}
1572
1573static int pn533_send_poll_frame(struct pn533 *dev)
1574{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001575 struct pn533_poll_modulations *mod;
1576 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001577 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001578 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001579
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001580 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1583 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001584
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 if (mod->len == 0) { /* Listen mode */
1586 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1587 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1588 } else { /* Polling mode */
1589 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1590 skb = pn533_alloc_poll_in_frame(mod);
1591 }
1592
1593 if (!skb) {
1594 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1595 return -ENOMEM;
1596 }
1597
1598 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1599 NULL);
1600 if (rc < 0) {
1601 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001602 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001603 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001604
1605 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001606}
1607
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001608static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001609{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1611 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001612 int rc;
1613
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001614 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1615
1616 nfc_dev_dbg(&dev->interface->dev,
1617 "%s cancel_listen %d modulation len %d",
1618 __func__, dev->cancel_listen, cur_mod->len);
1619
1620 if (dev->cancel_listen == 1) {
1621 dev->cancel_listen = 0;
1622 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001623 }
1624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625 rc = pn533_send_poll_frame(dev);
1626 if (rc)
1627 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001628
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001629 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1630 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001631
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001632 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001633}
1634
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001635static int pn533_start_poll(struct nfc_dev *nfc_dev,
1636 u32 im_protocols, u32 tm_protocols)
1637{
1638 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1639
1640 nfc_dev_dbg(&dev->interface->dev,
1641 "%s: im protocols 0x%x tm protocols 0x%x",
1642 __func__, im_protocols, tm_protocols);
1643
1644 if (dev->tgt_active_prot) {
1645 nfc_dev_err(&dev->interface->dev,
1646 "Cannot poll with a target already activated");
1647 return -EBUSY;
1648 }
1649
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001650 if (dev->tgt_mode) {
1651 nfc_dev_err(&dev->interface->dev,
1652 "Cannot poll while already being activated");
1653 return -EBUSY;
1654 }
1655
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001656 if (tm_protocols) {
1657 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1658 if (dev->gb == NULL)
1659 tm_protocols = 0;
1660 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001661
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001662 dev->poll_mod_curr = 0;
1663 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1664 dev->poll_protocols = im_protocols;
1665 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001666
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001668}
1669
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001670static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1671{
1672 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1673
1674 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1675
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001676 del_timer(&dev->listen_timer);
1677
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001679 nfc_dev_dbg(&dev->interface->dev,
1680 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001681 return;
1682 }
1683
1684 /* An ack will cancel the last issued command (poll) */
1685 pn533_send_ack(dev, GFP_KERNEL);
1686
1687 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1688 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001689
1690 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001691}
1692
1693static int pn533_activate_target_nfcdep(struct pn533 *dev)
1694{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001695 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001696 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001697 int rc;
1698
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001699 struct sk_buff *skb;
1700 struct sk_buff *resp;
1701
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001702 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1703
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001704 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1705 if (!skb)
1706 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001708 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1709 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001710
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001711 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1712 if (IS_ERR(resp))
1713 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001715 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001716 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001718 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001719 return -EIO;
1720
Samuel Ortiz541d9202011-12-14 16:43:10 +01001721 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001722 gt_len = resp->len - 16;
1723 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001724
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001726 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001727}
1728
Eric Lapuyade90099432012-05-07 12:31:13 +02001729static int pn533_activate_target(struct nfc_dev *nfc_dev,
1730 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001731{
1732 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1733 int rc;
1734
1735 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001736 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001737
1738 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001739 nfc_dev_err(&dev->interface->dev,
1740 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741 return -EBUSY;
1742 }
1743
1744 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001745 nfc_dev_err(&dev->interface->dev,
1746 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747 return -EBUSY;
1748 }
1749
1750 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001751 nfc_dev_err(&dev->interface->dev,
1752 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001753 return -EINVAL;
1754 }
1755
1756 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001757 nfc_dev_err(&dev->interface->dev,
1758 "Target doesn't support requested proto %u",
1759 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001760 return -EINVAL;
1761 }
1762
1763 if (protocol == NFC_PROTO_NFC_DEP) {
1764 rc = pn533_activate_target_nfcdep(dev);
1765 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001766 nfc_dev_err(&dev->interface->dev,
1767 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001768 return rc;
1769 }
1770 }
1771
1772 dev->tgt_active_prot = protocol;
1773 dev->tgt_available_prots = 0;
1774
1775 return 0;
1776}
1777
Eric Lapuyade90099432012-05-07 12:31:13 +02001778static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1779 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001780{
1781 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001782
1783 struct sk_buff *skb;
1784 struct sk_buff *resp;
1785
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001786 int rc;
1787
1788 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1789
1790 if (!dev->tgt_active_prot) {
1791 nfc_dev_err(&dev->interface->dev, "There is no active target");
1792 return;
1793 }
1794
1795 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001796 skb_queue_purge(&dev->resp_q);
1797
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001798 skb = pn533_alloc_skb(sizeof(u8));
1799 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001801
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001802 *skb_put(skb, 1) = 1; /* TG*/
1803
1804 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1805 if (IS_ERR(resp))
1806 return;
1807
1808 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001809 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001810 nfc_dev_err(&dev->interface->dev,
1811 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001812
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001813 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814 return;
1815}
1816
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001817
1818static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001819 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001820{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001821 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001822 u8 target_gt_len;
1823 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001824 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001825
1826 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001827
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001828 if (IS_ERR(resp))
1829 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001830
1831 if (dev->tgt_available_prots &&
1832 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1833 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001834 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001835 rc = -EINVAL;
1836 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837 }
1838
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1840
1841 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001842 if (rc != PN533_CMD_RET_SUCCESS) {
1843 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001844 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001845 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001846 }
1847
1848 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001849 struct nfc_target nfc_target;
1850
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001851 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1852
1853 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001854 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001856 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1857 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001858 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001859
1860 dev->tgt_available_prots = 0;
1861 }
1862
1863 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1864
1865 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001866 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001868 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869 if (rc == 0)
1870 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001871 dev->nfc_dev->targets[0].idx,
1872 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001873
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001874error:
1875 dev_kfree_skb(resp);
1876 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877}
1878
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001879static int pn533_mod_to_baud(struct pn533 *dev)
1880{
1881 switch (dev->poll_mod_curr) {
1882 case PN533_POLL_MOD_106KBPS_A:
1883 return 0;
1884 case PN533_POLL_MOD_212KBPS_FELICA:
1885 return 1;
1886 case PN533_POLL_MOD_424KBPS_FELICA:
1887 return 2;
1888 default:
1889 return -EINVAL;
1890 }
1891}
1892
Samuel Ortizd7f33452012-05-29 21:45:21 +02001893#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001894static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001895 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001896{
1897 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001898 struct sk_buff *skb;
1899 int rc, baud, skb_len;
1900 u8 *next, *arg;
1901
Samuel Ortizd7f33452012-05-29 21:45:21 +02001902 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001903
1904 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1905
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906 if (dev->poll_mod_count) {
1907 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001908 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001909 return -EBUSY;
1910 }
1911
1912 if (dev->tgt_active_prot) {
1913 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001914 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001915 return -EBUSY;
1916 }
1917
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001918 baud = pn533_mod_to_baud(dev);
1919 if (baud < 0) {
1920 nfc_dev_err(&dev->interface->dev,
1921 "Invalid curr modulation %d", dev->poll_mod_curr);
1922 return baud;
1923 }
1924
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001925 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001926 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001927 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001928
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001929 skb = pn533_alloc_skb(skb_len);
1930 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001931 return -ENOMEM;
1932
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001933 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1934 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001935
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001936 next = skb_put(skb, 1); /* Next */
1937 *next = 0;
1938
1939 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1940 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1941 PASSIVE_DATA_LEN);
1942 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001943 }
1944
Samuel Ortiz47807d32012-03-05 01:03:50 +01001945 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001946 memcpy(skb_put(skb, gb_len), gb, gb_len);
1947 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001948 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001949 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001950 }
1951
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001952 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1953 if (!arg) {
1954 dev_kfree_skb(skb);
1955 return -ENOMEM;
1956 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001957
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001958 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001959
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001960 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1961 pn533_in_dep_link_up_complete, arg);
1962
1963 if (rc < 0) {
1964 dev_kfree_skb(skb);
1965 kfree(arg);
1966 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001967
1968 return rc;
1969}
1970
1971static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1972{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001973 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1974
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001975 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1976
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001977 pn533_poll_reset_mod_list(dev);
1978
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001979 if (dev->tgt_mode || dev->tgt_active_prot) {
1980 pn533_send_ack(dev, GFP_KERNEL);
1981 usb_kill_urb(dev->in_urb);
1982 }
1983
1984 dev->tgt_active_prot = 0;
1985 dev->tgt_mode = 0;
1986
1987 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001988
1989 return 0;
1990}
1991
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001992struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001993 data_exchange_cb_t cb;
1994 void *cb_context;
1995};
1996
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001997static struct sk_buff *pn533_build_response(struct pn533 *dev)
1998{
1999 struct sk_buff *skb, *tmp, *t;
2000 unsigned int skb_len = 0, tmp_len = 0;
2001
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002002 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002003
2004 if (skb_queue_empty(&dev->resp_q))
2005 return NULL;
2006
2007 if (skb_queue_len(&dev->resp_q) == 1) {
2008 skb = skb_dequeue(&dev->resp_q);
2009 goto out;
2010 }
2011
2012 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2013 skb_len += tmp->len;
2014
2015 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2016 __func__, skb_len);
2017
2018 skb = alloc_skb(skb_len, GFP_KERNEL);
2019 if (skb == NULL)
2020 goto out;
2021
2022 skb_put(skb, skb_len);
2023
2024 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2025 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2026 tmp_len += tmp->len;
2027 }
2028
2029out:
2030 skb_queue_purge(&dev->resp_q);
2031
2032 return skb;
2033}
2034
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002035static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002036 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002037{
2038 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002039 struct sk_buff *skb;
2040 int rc = 0;
2041 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002042
2043 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2044
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002045 if (IS_ERR(resp)) {
2046 rc = PTR_ERR(resp);
2047 goto _error;
2048 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002049
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002050 status = resp->data[0];
2051 ret = status & PN533_CMD_RET_MASK;
2052 mi = status & PN533_CMD_MI_MASK;
2053
2054 skb_pull(resp, sizeof(status));
2055
2056 if (ret != PN533_CMD_RET_SUCCESS) {
2057 nfc_dev_err(&dev->interface->dev,
2058 "PN533 reported error %d when exchanging data",
2059 ret);
2060 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002061 goto error;
2062 }
2063
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002064 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002065
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002066 if (mi) {
2067 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002068 queue_work(dev->wq, &dev->mi_work);
2069 return -EINPROGRESS;
2070 }
2071
2072 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002073 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002074 goto error;
2075
2076 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002077 kfree(arg);
2078 return 0;
2079
2080error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002081 dev_kfree_skb(resp);
2082_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002083 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002084 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002086 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002087}
2088
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002089static int pn533_transceive(struct nfc_dev *nfc_dev,
2090 struct nfc_target *target, struct sk_buff *skb,
2091 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002092{
2093 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002094 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002095 int rc;
2096
2097 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2098
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002099 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2100 /* TODO: Implement support to multi-part data exchange */
2101 nfc_dev_err(&dev->interface->dev,
2102 "Data length greater than the max allowed: %d",
2103 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2104 rc = -ENOSYS;
2105 goto error;
2106 }
2107
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002109 nfc_dev_err(&dev->interface->dev,
2110 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111 rc = -EINVAL;
2112 goto error;
2113 }
2114
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002115 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002116 if (!arg) {
2117 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002118 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002119 }
2120
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002121 arg->cb = cb;
2122 arg->cb_context = cb_context;
2123
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002124 switch (dev->device_type) {
2125 case PN533_DEVICE_PASORI:
2126 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2127 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2128 skb,
2129 pn533_data_exchange_complete,
2130 arg);
2131
2132 break;
2133 }
2134 default:
2135 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2136
2137 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2138 skb, pn533_data_exchange_complete,
2139 arg);
2140
2141 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002142 }
2143
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002144 if (rc < 0) /* rc from send_async */
2145 goto error;
2146
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002147 return 0;
2148
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002149error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002150 kfree(arg);
2151 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002152 return rc;
2153}
2154
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002155static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002156 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002157{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002158 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002159
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002160 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2161
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002162 if (IS_ERR(resp))
2163 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002164
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002165 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002166
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002168
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002169 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002170 nfc_tm_deactivated(dev->nfc_dev);
2171
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002172 dev->tgt_mode = 0;
2173
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002174 return 0;
2175 }
2176
2177 queue_work(dev->wq, &dev->tg_work);
2178
2179 return 0;
2180}
2181
2182static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2183{
2184 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002185 int rc;
2186
2187 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2188
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002189 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002190 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002191 "Data length greater than the max allowed: %d",
2192 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2193 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002194 }
2195
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002196 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2197 pn533_tm_send_complete, NULL);
2198 if (rc < 0)
2199 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002200
2201 return rc;
2202}
2203
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002204static void pn533_wq_mi_recv(struct work_struct *work)
2205{
2206 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002207
2208 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002209 int rc;
2210
2211 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2212
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002213 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2214 if (!skb)
2215 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002216
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002217 switch (dev->device_type) {
2218 case PN533_DEVICE_PASORI:
2219 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2220 rc = pn533_send_cmd_direct_async(dev,
2221 PN533_CMD_IN_COMM_THRU,
2222 skb,
2223 pn533_data_exchange_complete,
2224 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002225
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002226 break;
2227 }
2228 default:
2229 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002230
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002231 rc = pn533_send_cmd_direct_async(dev,
2232 PN533_CMD_IN_DATA_EXCHANGE,
2233 skb,
2234 pn533_data_exchange_complete,
2235 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002236
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002237 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002238 }
2239
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002240 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002241 return;
2242
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002243 nfc_dev_err(&dev->interface->dev,
2244 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002245
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002246 dev_kfree_skb(skb);
2247 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002248
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002249error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002250 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002251 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002252}
2253
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002254static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2255 u8 cfgdata_len)
2256{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002257 struct sk_buff *skb;
2258 struct sk_buff *resp;
2259
2260 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002261
2262 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2263
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002264 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002265
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002266 skb = pn533_alloc_skb(skb_len);
2267 if (!skb)
2268 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002269
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002270 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2271 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002272
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002273 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2274 if (IS_ERR(resp))
2275 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002276
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002277 dev_kfree_skb(resp);
2278 return 0;
2279}
2280
2281static int pn533_get_firmware_version(struct pn533 *dev,
2282 struct pn533_fw_version *fv)
2283{
2284 struct sk_buff *skb;
2285 struct sk_buff *resp;
2286
2287 skb = pn533_alloc_skb(0);
2288 if (!skb)
2289 return -ENOMEM;
2290
2291 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2292 if (IS_ERR(resp))
2293 return PTR_ERR(resp);
2294
2295 fv->ic = resp->data[0];
2296 fv->ver = resp->data[1];
2297 fv->rev = resp->data[2];
2298 fv->support = resp->data[3];
2299
2300 dev_kfree_skb(resp);
2301 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002302}
2303
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002304static int pn533_fw_reset(struct pn533 *dev)
2305{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002306 struct sk_buff *skb;
2307 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002308
2309 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2310
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002311 skb = pn533_alloc_skb(sizeof(u8));
2312 if (!skb)
2313 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002314
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002315 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002316
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002317 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2318 if (IS_ERR(resp))
2319 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002320
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002321 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002322
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002323 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002324}
2325
2326static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002327 .dev_up = NULL,
2328 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002329 .dep_link_up = pn533_dep_link_up,
2330 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002331 .start_poll = pn533_start_poll,
2332 .stop_poll = pn533_stop_poll,
2333 .activate_target = pn533_activate_target,
2334 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002335 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002336 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002337};
2338
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002339static int pn533_setup(struct pn533 *dev)
2340{
2341 struct pn533_config_max_retries max_retries;
2342 struct pn533_config_timing timing;
2343 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2344 int rc;
2345
2346 switch (dev->device_type) {
2347 case PN533_DEVICE_STD:
2348 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2349 max_retries.mx_rty_psl = 2;
2350 max_retries.mx_rty_passive_act =
2351 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2352
2353 timing.rfu = PN533_CONFIG_TIMING_102;
2354 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2355 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2356
2357 break;
2358
2359 case PN533_DEVICE_PASORI:
2360 max_retries.mx_rty_atr = 0x2;
2361 max_retries.mx_rty_psl = 0x1;
2362 max_retries.mx_rty_passive_act =
2363 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2364
2365 timing.rfu = PN533_CONFIG_TIMING_102;
2366 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2367 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2368
2369 break;
2370
2371 default:
2372 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2373 dev->device_type);
2374 return -EINVAL;
2375 }
2376
2377 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2378 (u8 *)&max_retries, sizeof(max_retries));
2379 if (rc) {
2380 nfc_dev_err(&dev->interface->dev,
2381 "Error on setting MAX_RETRIES config");
2382 return rc;
2383 }
2384
2385
2386 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2387 (u8 *)&timing, sizeof(timing));
2388 if (rc) {
2389 nfc_dev_err(&dev->interface->dev,
2390 "Error on setting RF timings");
2391 return rc;
2392 }
2393
2394 switch (dev->device_type) {
2395 case PN533_DEVICE_STD:
2396 break;
2397
2398 case PN533_DEVICE_PASORI:
2399 pn533_fw_reset(dev);
2400
2401 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2402 pasori_cfg, 3);
2403 if (rc) {
2404 nfc_dev_err(&dev->interface->dev,
2405 "Error while settings PASORI config");
2406 return rc;
2407 }
2408
2409 pn533_fw_reset(dev);
2410
2411 break;
2412 }
2413
2414 return 0;
2415}
2416
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002417static int pn533_probe(struct usb_interface *interface,
2418 const struct usb_device_id *id)
2419{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002420 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002421 struct pn533 *dev;
2422 struct usb_host_interface *iface_desc;
2423 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002424 int in_endpoint = 0;
2425 int out_endpoint = 0;
2426 int rc = -ENOMEM;
2427 int i;
2428 u32 protocols;
2429
2430 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2431 if (!dev)
2432 return -ENOMEM;
2433
2434 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2435 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002436 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002437
2438 iface_desc = interface->cur_altsetting;
2439 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2440 endpoint = &iface_desc->endpoint[i].desc;
2441
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002442 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002443 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002444
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002445 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002446 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002447 }
2448
2449 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002450 nfc_dev_err(&interface->dev,
2451 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002452 rc = -ENODEV;
2453 goto error;
2454 }
2455
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002456 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002457 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2459
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +01002460 if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002461 goto error;
2462
2463 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002464 usb_rcvbulkpipe(dev->udev, in_endpoint),
2465 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002467 usb_sndbulkpipe(dev->udev, out_endpoint),
2468 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002469
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002470 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2471 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002472 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002473 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002474 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002475 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002476 if (dev->wq == NULL)
2477 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002478
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002479 init_timer(&dev->listen_timer);
2480 dev->listen_timer.data = (unsigned long) dev;
2481 dev->listen_timer.function = pn533_listen_mode_timer;
2482
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002483 skb_queue_head_init(&dev->resp_q);
2484
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002485 INIT_LIST_HEAD(&dev->cmd_queue);
2486
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487 usb_set_intfdata(interface, dev);
2488
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002489 memset(&fw_ver, 0, sizeof(fw_ver));
2490 rc = pn533_get_firmware_version(dev, &fw_ver);
2491 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002492 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002493
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002494 nfc_dev_info(&dev->interface->dev,
2495 "NXP PN533 firmware ver %d.%d now attached",
2496 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002497
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002498 dev->device_type = id->driver_info;
2499 switch (dev->device_type) {
2500 case PN533_DEVICE_STD:
2501 protocols = PN533_ALL_PROTOCOLS;
2502 break;
2503
2504 case PN533_DEVICE_PASORI:
2505 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2506 break;
2507
2508 default:
2509 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2510 dev->device_type);
2511 rc = -EINVAL;
2512 goto destroy_wq;
2513 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002514
Samuel Ortize8753042011-08-19 15:47:11 +02002515 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002516 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002517 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002518 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002519 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002520 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002521
2522 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2523 nfc_set_drvdata(dev->nfc_dev, dev);
2524
2525 rc = nfc_register_device(dev->nfc_dev);
2526 if (rc)
2527 goto free_nfc_dev;
2528
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002529 rc = pn533_setup(dev);
2530 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002531 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002532
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002533 return 0;
2534
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002535unregister_nfc_dev:
2536 nfc_unregister_device(dev->nfc_dev);
2537
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538free_nfc_dev:
2539 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002540
Samuel Ortiz4849f852012-04-10 19:43:17 +02002541destroy_wq:
2542 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002543error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002544 usb_free_urb(dev->in_urb);
2545 kfree(dev->out_frame);
2546 usb_free_urb(dev->out_urb);
2547 kfree(dev);
2548 return rc;
2549}
2550
2551static void pn533_disconnect(struct usb_interface *interface)
2552{
2553 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002554 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555
2556 dev = usb_get_intfdata(interface);
2557 usb_set_intfdata(interface, NULL);
2558
2559 nfc_unregister_device(dev->nfc_dev);
2560 nfc_free_device(dev->nfc_dev);
2561
2562 usb_kill_urb(dev->in_urb);
2563 usb_kill_urb(dev->out_urb);
2564
Samuel Ortiz4849f852012-04-10 19:43:17 +02002565 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002566
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002567 skb_queue_purge(&dev->resp_q);
2568
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002569 del_timer(&dev->listen_timer);
2570
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002571 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2572 list_del(&cmd->queue);
2573 kfree(cmd);
2574 }
2575
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002576 usb_free_urb(dev->in_urb);
2577 kfree(dev->out_frame);
2578 usb_free_urb(dev->out_urb);
2579 kfree(dev);
2580
Dan Carpenter276556d2011-07-08 10:21:15 +03002581 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002582}
2583
2584static struct usb_driver pn533_driver = {
2585 .name = "pn533",
2586 .probe = pn533_probe,
2587 .disconnect = pn533_disconnect,
2588 .id_table = pn533_table,
2589};
2590
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002591module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002592
2593MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2594 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2595MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2596MODULE_VERSION(VERSION);
2597MODULE_LICENSE("GPL");