blob: 898375d95f365e9f1344c2ce103ea2ae4612c3a0 [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 Rymarkiewicz99e591b2012-12-10 14:42:47 +0100527 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
528 in_frame, PN533_FRAME_SIZE(in_frame), false);
529
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300530 if (!pn533_rx_frame_is_valid(in_frame)) {
531 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200532 dev->wq_in_error = -EIO;
533 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300534 }
535
536 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100537 nfc_dev_err(&dev->interface->dev,
538 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200539 dev->wq_in_error = -EIO;
540 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300541 }
542
543 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
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
591 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
592
593 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
594 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100595 nfc_dev_err(&dev->interface->dev,
596 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200597 dev->wq_in_error = rc;
598 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599 }
600
601 return;
602
Samuel Ortiz4849f852012-04-10 19:43:17 +0200603sched_wq:
604 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200605 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606}
607
608static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
609{
610 dev->in_urb->complete = pn533_recv_ack;
611
612 return usb_submit_urb(dev->in_urb, flags);
613}
614
615static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
616{
617 int rc;
618
619 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
620
621 pn533_tx_frame_ack(dev->out_frame);
622
623 dev->out_urb->transfer_buffer = dev->out_frame;
624 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
625 rc = usb_submit_urb(dev->out_urb, flags);
626
627 return rc;
628}
629
630static int __pn533_send_cmd_frame_async(struct pn533 *dev,
631 struct pn533_frame *out_frame,
632 struct pn533_frame *in_frame,
633 int in_frame_len,
634 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100635 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300636{
637 int rc;
638
639 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100640 PN533_FRAME_CMD(out_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300641
642 dev->cmd = PN533_FRAME_CMD(out_frame);
643 dev->cmd_complete = cmd_complete;
644 dev->cmd_complete_arg = arg;
645
646 dev->out_urb->transfer_buffer = out_frame;
647 dev->out_urb->transfer_buffer_length =
648 PN533_FRAME_SIZE(out_frame);
649
650 dev->in_urb->transfer_buffer = in_frame;
651 dev->in_urb->transfer_buffer_length = in_frame_len;
652
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100653 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
654 out_frame, PN533_FRAME_SIZE(out_frame), false);
655
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100656 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657 if (rc)
658 return rc;
659
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100660 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300661 if (rc)
662 goto error;
663
664 return 0;
665
666error:
667 usb_unlink_urb(dev->out_urb);
668 return rc;
669}
670
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100671static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
672{
673 struct pn533_frame *frame;
674 /* payload is already there, just update datalen */
675 int payload_len = skb->len;
676
677 skb_push(skb, PN533_FRAME_HEADER_LEN);
678 skb_put(skb, PN533_FRAME_TAIL_LEN);
679
680 frame = (struct pn533_frame *)skb->data;
681
682 pn533_tx_frame_init(frame, cmd_code);
683 frame->datalen += payload_len;
684 pn533_tx_frame_finish(frame);
685}
686
687struct pn533_send_async_complete_arg {
688 pn533_send_async_complete_t complete_cb;
689 void *complete_cb_context;
690 struct sk_buff *resp;
691 struct sk_buff *req;
692};
693
694static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
695 int params_len)
696{
697 struct pn533_send_async_complete_arg *arg = _arg;
698
699 struct sk_buff *req = arg->req;
700 struct sk_buff *resp = arg->resp;
701
702 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
703 int rc;
704
705 dev_kfree_skb(req);
706
707 if (params_len < 0) {
708 nfc_dev_err(&dev->interface->dev,
709 "Error %d when starting as a target",
710 params_len);
711
712 arg->complete_cb(dev, arg->complete_cb_context,
713 ERR_PTR(params_len));
714 rc = params_len;
715 dev_kfree_skb(resp);
716 goto out;
717 }
718
719 skb_put(resp, PN533_FRAME_SIZE(frame));
720 skb_pull(resp, PN533_FRAME_HEADER_LEN);
721 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
722
723 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
724
725out:
726 kfree(arg);
727 return rc;
728}
729
730static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
731 struct sk_buff *req, struct sk_buff *resp,
732 int resp_len,
733 pn533_send_async_complete_t complete_cb,
734 void *complete_cb_context)
735{
736 struct pn533_cmd *cmd;
737 struct pn533_send_async_complete_arg *arg;
738 int rc = 0;
739
740 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
741
742 arg = kzalloc(sizeof(arg), GFP_KERNEL);
743 if (!arg)
744 return -ENOMEM;
745
746 arg->complete_cb = complete_cb;
747 arg->complete_cb_context = complete_cb_context;
748 arg->resp = resp;
749 arg->req = req;
750
751 pn533_build_cmd_frame(cmd_code, req);
752
753 mutex_lock(&dev->cmd_lock);
754
755 if (!dev->cmd_pending) {
756 rc = __pn533_send_cmd_frame_async(dev,
757 (struct pn533_frame *)req->data,
758 (struct pn533_frame *)resp->data,
759 resp_len, pn533_send_async_complete,
760 arg);
761 if (rc)
762 goto error;
763
764 dev->cmd_pending = 1;
765 goto unlock;
766 }
767
768 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
769
770 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
771 if (!cmd) {
772 rc = -ENOMEM;
773 goto error;
774 }
775
776 INIT_LIST_HEAD(&cmd->queue);
777 cmd->cmd_code = cmd_code;
778 cmd->req = req;
779 cmd->resp = resp;
780 cmd->arg = arg;
781
782 list_add_tail(&cmd->queue, &dev->cmd_queue);
783
784 goto unlock;
785
786error:
787 kfree(arg);
788unlock:
789 mutex_unlock(&dev->cmd_lock);
790 return rc;
791}
792
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100793static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
794 struct sk_buff *req,
795 pn533_send_async_complete_t complete_cb,
796 void *complete_cb_context)
797{
798 struct sk_buff *resp;
799 int rc;
800 int resp_len = PN533_FRAME_HEADER_LEN +
801 PN533_FRAME_MAX_PAYLOAD_LEN +
802 PN533_FRAME_TAIL_LEN;
803
804 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
805
806 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
807 if (!resp)
808 return -ENOMEM;
809
810 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
811 complete_cb_context);
812 if (rc)
813 dev_kfree_skb(resp);
814
815 return rc;
816}
817
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100818static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
819 struct sk_buff *req,
820 pn533_send_async_complete_t complete_cb,
821 void *complete_cb_context)
822{
823 struct sk_buff *resp;
824 int rc;
825
826 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
827
828 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
829 if (!resp)
830 return -ENOMEM;
831
832 rc = __pn533_send_async(dev, cmd_code, req, resp,
833 PN533_NORMAL_FRAME_MAX_LEN,
834 complete_cb, complete_cb_context);
835 if (rc)
836 dev_kfree_skb(resp);
837
838 return rc;
839}
840
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100841/*
842 * pn533_send_cmd_direct_async
843 *
844 * The function sends a piority cmd directly to the chip omiting the cmd
845 * queue. It's intended to be used by chaining mechanism of received responses
846 * where the host has to request every single chunk of data before scheduling
847 * next cmd from the queue.
848 */
849static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
850 struct sk_buff *req,
851 pn533_send_async_complete_t complete_cb,
852 void *complete_cb_context)
853{
854 struct pn533_send_async_complete_arg *arg;
855 struct sk_buff *resp;
856 int rc;
857 int resp_len = PN533_FRAME_HEADER_LEN +
858 PN533_FRAME_MAX_PAYLOAD_LEN +
859 PN533_FRAME_TAIL_LEN;
860
861 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
862
863 resp = alloc_skb(resp_len, GFP_KERNEL);
864 if (!resp)
865 return -ENOMEM;
866
867 arg = kzalloc(sizeof(arg), GFP_KERNEL);
868 if (!arg) {
869 dev_kfree_skb(resp);
870 return -ENOMEM;
871 }
872
873 arg->complete_cb = complete_cb;
874 arg->complete_cb_context = complete_cb_context;
875 arg->resp = resp;
876 arg->req = req;
877
878 pn533_build_cmd_frame(cmd_code, req);
879
880 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
881 (struct pn533_frame *)resp->data,
882 resp_len, pn533_send_async_complete,
883 arg);
884 if (rc < 0) {
885 dev_kfree_skb(resp);
886 kfree(arg);
887 }
888
889 return rc;
890}
891
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200892static void pn533_wq_cmd(struct work_struct *work)
893{
894 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
895 struct pn533_cmd *cmd;
896
897 mutex_lock(&dev->cmd_lock);
898
899 if (list_empty(&dev->cmd_queue)) {
900 dev->cmd_pending = 0;
901 mutex_unlock(&dev->cmd_lock);
902 return;
903 }
904
905 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
906
Szymon Janc60ad07a2012-10-25 17:29:45 +0200907 list_del(&cmd->queue);
908
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200909 mutex_unlock(&dev->cmd_lock);
910
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +0100911 __pn533_send_cmd_frame_async(dev,
912 (struct pn533_frame *)cmd->req->data,
913 (struct pn533_frame *)cmd->resp->data,
914 PN533_NORMAL_FRAME_MAX_LEN,
915 pn533_send_async_complete,
916 cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200917
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200918 kfree(cmd);
919}
920
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300921struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100922 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300923 struct completion done;
924};
925
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100926static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
927 struct sk_buff *resp)
928{
929 struct pn533_sync_cmd_response *arg = _arg;
930
931 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
932
933 arg->resp = resp;
934 complete(&arg->done);
935
936 return 0;
937}
938
939/* pn533_send_cmd_sync
940 *
941 * Please note the req parameter is freed inside the function to
942 * limit a number of return value interpretations by the caller.
943 *
944 * 1. negative in case of error during TX path -> req should be freed
945 *
946 * 2. negative in case of error during RX path -> req should not be freed
947 * as it's been already freed at the begining of RX path by
948 * async_complete_cb.
949 *
950 * 3. valid pointer in case of succesfult RX path
951 *
952 * A caller has to check a return value with IS_ERR macro. If the test pass,
953 * the returned pointer is valid.
954 *
955 * */
956static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
957 struct sk_buff *req)
958{
959 int rc;
960 struct pn533_sync_cmd_response arg;
961
962 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
963
964 init_completion(&arg.done);
965
966 rc = pn533_send_cmd_async(dev, cmd_code, req,
967 pn533_send_sync_complete, &arg);
968 if (rc) {
969 dev_kfree_skb(req);
970 return ERR_PTR(rc);
971 }
972
973 wait_for_completion(&arg.done);
974
975 return arg.resp;
976}
977
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300978static void pn533_send_complete(struct urb *urb)
979{
980 struct pn533 *dev = urb->context;
981
982 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
983
984 switch (urb->status) {
985 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100986 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300987 case -ECONNRESET:
988 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100989 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100990 "The urb has been stopped (status %d)",
991 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300992 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100993 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300994 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100995 nfc_dev_err(&dev->interface->dev,
996 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300997 }
998}
999
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001000static struct sk_buff *pn533_alloc_skb(unsigned int size)
1001{
1002 struct sk_buff *skb;
1003
1004 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
1005 size +
1006 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
1007
1008 if (skb)
1009 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
1010
1011 return skb;
1012}
1013
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001014struct pn533_target_type_a {
1015 __be16 sens_res;
1016 u8 sel_res;
1017 u8 nfcid_len;
1018 u8 nfcid_data[];
1019} __packed;
1020
1021
1022#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1023#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1024#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1025
1026#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1027#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1028
1029#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1030#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1031
1032#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1033#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1034#define PN533_TYPE_A_SEL_PROT_DEP 2
1035#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1036
1037static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1038 int target_data_len)
1039{
1040 u8 ssd;
1041 u8 platconf;
1042
1043 if (target_data_len < sizeof(struct pn533_target_type_a))
1044 return false;
1045
1046 /* The lenght check of nfcid[] and ats[] are not being performed because
1047 the values are not being used */
1048
1049 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1050 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1051 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1052
1053 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001054 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1055 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1056 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001057 return false;
1058
1059 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1060 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1061 return false;
1062
1063 return true;
1064}
1065
1066static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1067 int tgt_data_len)
1068{
1069 struct pn533_target_type_a *tgt_type_a;
1070
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001071 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001072
1073 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1074 return -EPROTO;
1075
1076 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1077 case PN533_TYPE_A_SEL_PROT_MIFARE:
1078 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1079 break;
1080 case PN533_TYPE_A_SEL_PROT_ISO14443:
1081 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1082 break;
1083 case PN533_TYPE_A_SEL_PROT_DEP:
1084 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1085 break;
1086 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1087 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1088 NFC_PROTO_NFC_DEP_MASK;
1089 break;
1090 }
1091
1092 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1093 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001094 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1095 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001096
1097 return 0;
1098}
1099
1100struct pn533_target_felica {
1101 u8 pol_res;
1102 u8 opcode;
1103 u8 nfcid2[8];
1104 u8 pad[8];
1105 /* optional */
1106 u8 syst_code[];
1107} __packed;
1108
1109#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1110#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1111
1112static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1113 int target_data_len)
1114{
1115 if (target_data_len < sizeof(struct pn533_target_felica))
1116 return false;
1117
1118 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1119 return false;
1120
1121 return true;
1122}
1123
1124static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1125 int tgt_data_len)
1126{
1127 struct pn533_target_felica *tgt_felica;
1128
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001129 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001130
1131 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1132 return -EPROTO;
1133
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001134 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1135 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001136 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1137 else
1138 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1139
Samuel Ortiz79757542012-03-05 01:03:45 +01001140 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1141 nfc_tgt->sensf_res_len = 9;
1142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001143 return 0;
1144}
1145
1146struct pn533_target_jewel {
1147 __be16 sens_res;
1148 u8 jewelid[4];
1149} __packed;
1150
1151static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1152 int target_data_len)
1153{
1154 u8 ssd;
1155 u8 platconf;
1156
1157 if (target_data_len < sizeof(struct pn533_target_jewel))
1158 return false;
1159
1160 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1161 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1162 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1163
1164 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001165 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1166 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1167 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001168 return false;
1169
1170 return true;
1171}
1172
1173static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1174 int tgt_data_len)
1175{
1176 struct pn533_target_jewel *tgt_jewel;
1177
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001178 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001179
1180 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1181 return -EPROTO;
1182
1183 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1184 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001185 nfc_tgt->nfcid1_len = 4;
1186 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001187
1188 return 0;
1189}
1190
1191struct pn533_type_b_prot_info {
1192 u8 bitrate;
1193 u8 fsci_type;
1194 u8 fwi_adc_fo;
1195} __packed;
1196
1197#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1198#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1199#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1200
1201struct pn533_type_b_sens_res {
1202 u8 opcode;
1203 u8 nfcid[4];
1204 u8 appdata[4];
1205 struct pn533_type_b_prot_info prot_info;
1206} __packed;
1207
1208#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1209
1210struct pn533_target_type_b {
1211 struct pn533_type_b_sens_res sensb_res;
1212 u8 attrib_res_len;
1213 u8 attrib_res[];
1214} __packed;
1215
1216static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1217 int target_data_len)
1218{
1219 if (target_data_len < sizeof(struct pn533_target_type_b))
1220 return false;
1221
1222 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1223 return false;
1224
1225 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1226 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1227 return false;
1228
1229 return true;
1230}
1231
1232static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1233 int tgt_data_len)
1234{
1235 struct pn533_target_type_b *tgt_type_b;
1236
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001237 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001238
1239 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1240 return -EPROTO;
1241
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001242 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243
1244 return 0;
1245}
1246
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001247static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1248 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001249{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001250 struct nfc_target nfc_tgt;
1251 int rc;
1252
1253 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001254 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001255
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257 return -EPROTO;
1258
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001259 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1260
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001261 switch (dev->poll_mod_curr) {
1262 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001263 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001264 break;
1265 case PN533_POLL_MOD_212KBPS_FELICA:
1266 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001267 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001268 break;
1269 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001270 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001271 break;
1272 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001273 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001274 break;
1275 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001276 nfc_dev_err(&dev->interface->dev,
1277 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001278 return -EPROTO;
1279 }
1280
1281 if (rc)
1282 return rc;
1283
1284 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001285 nfc_dev_dbg(&dev->interface->dev,
1286 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001287 return -EAGAIN;
1288 }
1289
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001290 nfc_dev_dbg(&dev->interface->dev,
1291 "Target found - supported protocols: 0x%x",
1292 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001293
1294 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1295
1296 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1297
1298 return 0;
1299}
1300
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001301static inline void pn533_poll_next_mod(struct pn533 *dev)
1302{
1303 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1304}
1305
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306static void pn533_poll_reset_mod_list(struct pn533 *dev)
1307{
1308 dev->poll_mod_count = 0;
1309}
1310
1311static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1312{
1313 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001314 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001315 dev->poll_mod_count++;
1316}
1317
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001318static void pn533_poll_create_mod_list(struct pn533 *dev,
1319 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001320{
1321 pn533_poll_reset_mod_list(dev);
1322
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001323 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1324 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1325 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001326 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1327
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001328 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1329 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001330 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1331 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1332 }
1333
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001334 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001335 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1336
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001337 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001338 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001339
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001340 if (tm_protocols)
1341 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001342}
1343
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001344static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001345{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001346 u8 nbtg, tg, *tgdata;
1347 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001348
1349 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1350
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001351 nbtg = resp->data[0];
1352 tg = resp->data[1];
1353 tgdata = &resp->data[2];
1354 tgdata_len = resp->len - 2; /* nbtg + tg */
1355
1356 if (nbtg) {
1357 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001358
1359 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001360 if (rc == 0) {
1361 pn533_poll_reset_mod_list(dev);
1362 return 0;
1363 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001364 }
1365
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001366 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001367}
1368
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001369static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001370{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001371 struct sk_buff *skb;
1372 u8 *felica, *nfcid3, *gb;
1373
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001374 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1375 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1376 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1377 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001378
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001379 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1380 0x0, 0x0, 0x0,
1381 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001382
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001383 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1384 felica (18), nfcid3 (10), gb_len (1) */
1385 gbytes_len +
1386 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001387
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001388 skb = pn533_alloc_skb(skb_len);
1389 if (!skb)
1390 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001391
1392 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001393 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001394
1395 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001396 memcpy(skb_put(skb, 6), mifare_params, 6);
1397
1398 /* Felica params */
1399 felica = skb_put(skb, 18);
1400 memcpy(felica, felica_params, 18);
1401 get_random_bytes(felica + 2, 6);
1402
1403 /* NFCID3 */
1404 nfcid3 = skb_put(skb, 10);
1405 memset(nfcid3, 0, 10);
1406 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001407
1408 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001409 *skb_put(skb, 1) = gbytes_len;
1410
1411 gb = skb_put(skb, gbytes_len);
1412 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001413
Samuel Ortizad3823c2012-05-30 23:54:55 +02001414 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001415 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001416
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001417 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001418}
1419
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001420#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001421#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1422static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001423 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001424{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001425 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001426
1427 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1428
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001429 if (IS_ERR(resp))
1430 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001431
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001432 status = resp->data[0];
1433 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001434
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001435 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001436 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001437 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001438 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001439 return 0;
1440 }
1441
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001442 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001443}
1444
1445static void pn533_wq_tg_get_data(struct work_struct *work)
1446{
1447 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001448
1449 struct sk_buff *skb;
1450 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001451
1452 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1453
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001454 skb = pn533_alloc_skb(0);
1455 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001456 return;
1457
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001458 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1459 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001460
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001461 if (rc < 0)
1462 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001463
1464 return;
1465}
1466
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001467#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001468static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001469{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001470 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001471 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001472 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001473
1474 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1475
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001476 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001477 return -EINVAL;
1478
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001479 mode = resp->data[0];
1480 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001481
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001482 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1483 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001484
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001485 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1486 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001487 comm_mode = NFC_COMM_ACTIVE;
1488
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001489 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001490 return -EOPNOTSUPP;
1491
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001492 gb = cmd + ATR_REQ_GB_OFFSET;
1493 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001494
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001495 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1496 comm_mode, gb, gb_len);
1497 if (rc < 0) {
1498 nfc_dev_err(&dev->interface->dev,
1499 "Error when signaling target activation");
1500 return rc;
1501 }
1502
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001503 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001504 queue_work(dev->wq, &dev->tg_work);
1505
1506 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001507}
1508
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001509static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001510{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001511 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001512
1513 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1514
1515 /* An ack will cancel the last issued command (poll) */
1516 pn533_send_ack(dev, GFP_ATOMIC);
1517
1518 dev->cancel_listen = 1;
1519
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001520 pn533_poll_next_mod(dev);
1521
1522 queue_work(dev->wq, &dev->poll_work);
1523}
1524
1525static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001526 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001527{
1528 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001529 int rc;
1530
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001531 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1532
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001533 if (IS_ERR(resp)) {
1534 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001535
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001536 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1537 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001538
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001539 if (rc == -ENOENT) {
1540 if (dev->poll_mod_count != 0)
1541 return rc;
1542 else
1543 goto stop_poll;
1544 } else if (rc < 0) {
1545 nfc_dev_err(&dev->interface->dev,
1546 "Error %d when running poll", rc);
1547 goto stop_poll;
1548 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001549 }
1550
1551 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1552
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001553 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001555 rc = pn533_init_target_complete(dev, resp);
1556 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557 }
1558
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001559 /* Initiator mode */
1560 rc = pn533_start_poll_complete(dev, resp);
1561 if (!rc)
1562 goto done;
1563
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001564 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001565 queue_work(dev->wq, &dev->poll_work);
1566
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001567done:
1568 dev_kfree_skb(resp);
1569 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001570
1571stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001572 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1573
Samuel Ortizad3823c2012-05-30 23:54:55 +02001574 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001575 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001577}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001578
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1580 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001583
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001584 skb = pn533_alloc_skb(mod->len);
1585 if (!skb)
1586 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001587
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001588 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001589
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001590 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001591}
1592
1593static int pn533_send_poll_frame(struct pn533 *dev)
1594{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 struct pn533_poll_modulations *mod;
1596 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001597 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001598 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001599
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001600 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001601
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1603 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001604
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001605 if (mod->len == 0) { /* Listen mode */
1606 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1607 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1608 } else { /* Polling mode */
1609 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1610 skb = pn533_alloc_poll_in_frame(mod);
1611 }
1612
1613 if (!skb) {
1614 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1615 return -ENOMEM;
1616 }
1617
1618 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1619 NULL);
1620 if (rc < 0) {
1621 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001622 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001623 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001624
1625 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001626}
1627
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001628static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001629{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001630 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1631 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001632 int rc;
1633
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001634 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1635
1636 nfc_dev_dbg(&dev->interface->dev,
1637 "%s cancel_listen %d modulation len %d",
1638 __func__, dev->cancel_listen, cur_mod->len);
1639
1640 if (dev->cancel_listen == 1) {
1641 dev->cancel_listen = 0;
1642 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001643 }
1644
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001645 rc = pn533_send_poll_frame(dev);
1646 if (rc)
1647 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001648
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001649 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1650 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001651
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001652 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001653}
1654
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001655static int pn533_start_poll(struct nfc_dev *nfc_dev,
1656 u32 im_protocols, u32 tm_protocols)
1657{
1658 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1659
1660 nfc_dev_dbg(&dev->interface->dev,
1661 "%s: im protocols 0x%x tm protocols 0x%x",
1662 __func__, im_protocols, tm_protocols);
1663
1664 if (dev->tgt_active_prot) {
1665 nfc_dev_err(&dev->interface->dev,
1666 "Cannot poll with a target already activated");
1667 return -EBUSY;
1668 }
1669
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001670 if (dev->tgt_mode) {
1671 nfc_dev_err(&dev->interface->dev,
1672 "Cannot poll while already being activated");
1673 return -EBUSY;
1674 }
1675
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001676 if (tm_protocols) {
1677 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1678 if (dev->gb == NULL)
1679 tm_protocols = 0;
1680 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001681
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001682 dev->poll_mod_curr = 0;
1683 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1684 dev->poll_protocols = im_protocols;
1685 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001686
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001687 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001688}
1689
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001690static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1691{
1692 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1693
1694 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1695
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001696 del_timer(&dev->listen_timer);
1697
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001699 nfc_dev_dbg(&dev->interface->dev,
1700 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001701 return;
1702 }
1703
1704 /* An ack will cancel the last issued command (poll) */
1705 pn533_send_ack(dev, GFP_KERNEL);
1706
1707 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1708 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001709
1710 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001711}
1712
1713static int pn533_activate_target_nfcdep(struct pn533 *dev)
1714{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001715 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001716 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717 int rc;
1718
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001719 struct sk_buff *skb;
1720 struct sk_buff *resp;
1721
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001722 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1723
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001724 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1725 if (!skb)
1726 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001727
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001728 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1729 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001730
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001731 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1732 if (IS_ERR(resp))
1733 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001735 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001736 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001737 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001738 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001739 return -EIO;
1740
Samuel Ortiz541d9202011-12-14 16:43:10 +01001741 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001742 gt_len = resp->len - 16;
1743 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001744
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001745 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001746 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747}
1748
Eric Lapuyade90099432012-05-07 12:31:13 +02001749static int pn533_activate_target(struct nfc_dev *nfc_dev,
1750 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751{
1752 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1753 int rc;
1754
1755 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001756 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001757
1758 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001759 nfc_dev_err(&dev->interface->dev,
1760 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001761 return -EBUSY;
1762 }
1763
1764 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001765 nfc_dev_err(&dev->interface->dev,
1766 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 return -EBUSY;
1768 }
1769
1770 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001771 nfc_dev_err(&dev->interface->dev,
1772 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 return -EINVAL;
1774 }
1775
1776 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001777 nfc_dev_err(&dev->interface->dev,
1778 "Target doesn't support requested proto %u",
1779 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001780 return -EINVAL;
1781 }
1782
1783 if (protocol == NFC_PROTO_NFC_DEP) {
1784 rc = pn533_activate_target_nfcdep(dev);
1785 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001786 nfc_dev_err(&dev->interface->dev,
1787 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001788 return rc;
1789 }
1790 }
1791
1792 dev->tgt_active_prot = protocol;
1793 dev->tgt_available_prots = 0;
1794
1795 return 0;
1796}
1797
Eric Lapuyade90099432012-05-07 12:31:13 +02001798static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1799 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800{
1801 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001802
1803 struct sk_buff *skb;
1804 struct sk_buff *resp;
1805
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001806 int rc;
1807
1808 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1809
1810 if (!dev->tgt_active_prot) {
1811 nfc_dev_err(&dev->interface->dev, "There is no active target");
1812 return;
1813 }
1814
1815 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001816 skb_queue_purge(&dev->resp_q);
1817
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001818 skb = pn533_alloc_skb(sizeof(u8));
1819 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001820 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001821
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001822 *skb_put(skb, 1) = 1; /* TG*/
1823
1824 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1825 if (IS_ERR(resp))
1826 return;
1827
1828 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001829 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001830 nfc_dev_err(&dev->interface->dev,
1831 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001832
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001833 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001834 return;
1835}
1836
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837
1838static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001842 u8 target_gt_len;
1843 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001844 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001845
1846 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001847
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001848 if (IS_ERR(resp))
1849 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001850
1851 if (dev->tgt_available_prots &&
1852 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1853 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001854 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855 rc = -EINVAL;
1856 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001857 }
1858
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001859 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1860
1861 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862 if (rc != PN533_CMD_RET_SUCCESS) {
1863 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001864 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001865 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001866 }
1867
1868 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 struct nfc_target nfc_target;
1870
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001871 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1872
1873 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001874 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001875 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1877 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001878 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879
1880 dev->tgt_available_prots = 0;
1881 }
1882
1883 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1884
1885 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001886 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001887 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001888 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001889 if (rc == 0)
1890 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001891 dev->nfc_dev->targets[0].idx,
1892 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001893
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001894error:
1895 dev_kfree_skb(resp);
1896 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001897}
1898
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001899static int pn533_mod_to_baud(struct pn533 *dev)
1900{
1901 switch (dev->poll_mod_curr) {
1902 case PN533_POLL_MOD_106KBPS_A:
1903 return 0;
1904 case PN533_POLL_MOD_212KBPS_FELICA:
1905 return 1;
1906 case PN533_POLL_MOD_424KBPS_FELICA:
1907 return 2;
1908 default:
1909 return -EINVAL;
1910 }
1911}
1912
Samuel Ortizd7f33452012-05-29 21:45:21 +02001913#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001914static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001915 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001916{
1917 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001918 struct sk_buff *skb;
1919 int rc, baud, skb_len;
1920 u8 *next, *arg;
1921
Samuel Ortizd7f33452012-05-29 21:45:21 +02001922 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001923
1924 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1925
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001926 if (dev->poll_mod_count) {
1927 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001928 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001929 return -EBUSY;
1930 }
1931
1932 if (dev->tgt_active_prot) {
1933 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001934 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001935 return -EBUSY;
1936 }
1937
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001938 baud = pn533_mod_to_baud(dev);
1939 if (baud < 0) {
1940 nfc_dev_err(&dev->interface->dev,
1941 "Invalid curr modulation %d", dev->poll_mod_curr);
1942 return baud;
1943 }
1944
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001945 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001946 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001948
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001949 skb = pn533_alloc_skb(skb_len);
1950 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001951 return -ENOMEM;
1952
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001953 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1954 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001955
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001956 next = skb_put(skb, 1); /* Next */
1957 *next = 0;
1958
1959 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1960 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1961 PASSIVE_DATA_LEN);
1962 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001963 }
1964
Samuel Ortiz47807d32012-03-05 01:03:50 +01001965 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 memcpy(skb_put(skb, gb_len), gb, gb_len);
1967 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001968 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001969 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001970 }
1971
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001972 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1973 if (!arg) {
1974 dev_kfree_skb(skb);
1975 return -ENOMEM;
1976 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001977
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001978 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001979
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001980 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1981 pn533_in_dep_link_up_complete, arg);
1982
1983 if (rc < 0) {
1984 dev_kfree_skb(skb);
1985 kfree(arg);
1986 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001987
1988 return rc;
1989}
1990
1991static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1992{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001993 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1994
1995 pn533_poll_reset_mod_list(dev);
1996
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001997 if (dev->tgt_mode || dev->tgt_active_prot) {
1998 pn533_send_ack(dev, GFP_KERNEL);
1999 usb_kill_urb(dev->in_urb);
2000 }
2001
2002 dev->tgt_active_prot = 0;
2003 dev->tgt_mode = 0;
2004
2005 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002006
2007 return 0;
2008}
2009
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002010struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002011 data_exchange_cb_t cb;
2012 void *cb_context;
2013};
2014
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002015static struct sk_buff *pn533_build_response(struct pn533 *dev)
2016{
2017 struct sk_buff *skb, *tmp, *t;
2018 unsigned int skb_len = 0, tmp_len = 0;
2019
2020 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2021
2022 if (skb_queue_empty(&dev->resp_q))
2023 return NULL;
2024
2025 if (skb_queue_len(&dev->resp_q) == 1) {
2026 skb = skb_dequeue(&dev->resp_q);
2027 goto out;
2028 }
2029
2030 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2031 skb_len += tmp->len;
2032
2033 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2034 __func__, skb_len);
2035
2036 skb = alloc_skb(skb_len, GFP_KERNEL);
2037 if (skb == NULL)
2038 goto out;
2039
2040 skb_put(skb, skb_len);
2041
2042 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2043 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2044 tmp_len += tmp->len;
2045 }
2046
2047out:
2048 skb_queue_purge(&dev->resp_q);
2049
2050 return skb;
2051}
2052
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002053static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002054 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002055{
2056 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002057 struct sk_buff *skb;
2058 int rc = 0;
2059 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002060
2061 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2062
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002063 if (IS_ERR(resp)) {
2064 rc = PTR_ERR(resp);
2065 goto _error;
2066 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002067
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002068 status = resp->data[0];
2069 ret = status & PN533_CMD_RET_MASK;
2070 mi = status & PN533_CMD_MI_MASK;
2071
2072 skb_pull(resp, sizeof(status));
2073
2074 if (ret != PN533_CMD_RET_SUCCESS) {
2075 nfc_dev_err(&dev->interface->dev,
2076 "PN533 reported error %d when exchanging data",
2077 ret);
2078 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002079 goto error;
2080 }
2081
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002082 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002083
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002084 if (mi) {
2085 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002086 queue_work(dev->wq, &dev->mi_work);
2087 return -EINPROGRESS;
2088 }
2089
2090 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002091 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002092 goto error;
2093
2094 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002095 kfree(arg);
2096 return 0;
2097
2098error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002099 dev_kfree_skb(resp);
2100_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002101 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002102 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002103 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002104 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002105}
2106
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002107static int pn533_transceive(struct nfc_dev *nfc_dev,
2108 struct nfc_target *target, struct sk_buff *skb,
2109 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002110{
2111 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002112 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002113 int rc;
2114
2115 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2116
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002117 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2118 /* TODO: Implement support to multi-part data exchange */
2119 nfc_dev_err(&dev->interface->dev,
2120 "Data length greater than the max allowed: %d",
2121 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2122 rc = -ENOSYS;
2123 goto error;
2124 }
2125
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002126 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002127 nfc_dev_err(&dev->interface->dev,
2128 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002129 rc = -EINVAL;
2130 goto error;
2131 }
2132
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002133 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002134 if (!arg) {
2135 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002136 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002137 }
2138
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002139 arg->cb = cb;
2140 arg->cb_context = cb_context;
2141
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002142 switch (dev->device_type) {
2143 case PN533_DEVICE_PASORI:
2144 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2145 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2146 skb,
2147 pn533_data_exchange_complete,
2148 arg);
2149
2150 break;
2151 }
2152 default:
2153 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2154
2155 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2156 skb, pn533_data_exchange_complete,
2157 arg);
2158
2159 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002160 }
2161
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002162 if (rc < 0) /* rc from send_async */
2163 goto error;
2164
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002165 return 0;
2166
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002167error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002168 kfree(arg);
2169 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002170 return rc;
2171}
2172
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002174 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002176 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002177
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002178 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2179
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002180 if (IS_ERR(resp))
2181 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002182
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002183 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002184
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002185 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002186
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002187 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002188 nfc_tm_deactivated(dev->nfc_dev);
2189
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002190 dev->tgt_mode = 0;
2191
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002192 return 0;
2193 }
2194
2195 queue_work(dev->wq, &dev->tg_work);
2196
2197 return 0;
2198}
2199
2200static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2201{
2202 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002203 int rc;
2204
2205 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2206
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002207 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002208 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002209 "Data length greater than the max allowed: %d",
2210 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2211 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002212 }
2213
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002214 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2215 pn533_tm_send_complete, NULL);
2216 if (rc < 0)
2217 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002218
2219 return rc;
2220}
2221
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002222static void pn533_wq_mi_recv(struct work_struct *work)
2223{
2224 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002225
2226 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002227 int rc;
2228
2229 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2230
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002231 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2232 if (!skb)
2233 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002234
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002235 switch (dev->device_type) {
2236 case PN533_DEVICE_PASORI:
2237 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2238 rc = pn533_send_cmd_direct_async(dev,
2239 PN533_CMD_IN_COMM_THRU,
2240 skb,
2241 pn533_data_exchange_complete,
2242 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002243
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002244 break;
2245 }
2246 default:
2247 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002248
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002249 rc = pn533_send_cmd_direct_async(dev,
2250 PN533_CMD_IN_DATA_EXCHANGE,
2251 skb,
2252 pn533_data_exchange_complete,
2253 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002254
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002255 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002256 }
2257
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002258 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002259 return;
2260
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002261 nfc_dev_err(&dev->interface->dev,
2262 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002263
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002264 dev_kfree_skb(skb);
2265 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002266
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002267error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002268 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002269 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002270}
2271
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002272static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2273 u8 cfgdata_len)
2274{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002275 struct sk_buff *skb;
2276 struct sk_buff *resp;
2277
2278 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002279
2280 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2281
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002282 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002283
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002284 skb = pn533_alloc_skb(skb_len);
2285 if (!skb)
2286 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002287
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002288 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2289 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002290
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002291 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2292 if (IS_ERR(resp))
2293 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002294
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002295 dev_kfree_skb(resp);
2296 return 0;
2297}
2298
2299static int pn533_get_firmware_version(struct pn533 *dev,
2300 struct pn533_fw_version *fv)
2301{
2302 struct sk_buff *skb;
2303 struct sk_buff *resp;
2304
2305 skb = pn533_alloc_skb(0);
2306 if (!skb)
2307 return -ENOMEM;
2308
2309 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2310 if (IS_ERR(resp))
2311 return PTR_ERR(resp);
2312
2313 fv->ic = resp->data[0];
2314 fv->ver = resp->data[1];
2315 fv->rev = resp->data[2];
2316 fv->support = resp->data[3];
2317
2318 dev_kfree_skb(resp);
2319 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002320}
2321
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002322static int pn533_fw_reset(struct pn533 *dev)
2323{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002324 struct sk_buff *skb;
2325 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002326
2327 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2328
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002329 skb = pn533_alloc_skb(sizeof(u8));
2330 if (!skb)
2331 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002332
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002333 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002334
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002335 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2336 if (IS_ERR(resp))
2337 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002338
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002339 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002340
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002341 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002342}
2343
2344static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002345 .dev_up = NULL,
2346 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002347 .dep_link_up = pn533_dep_link_up,
2348 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002349 .start_poll = pn533_start_poll,
2350 .stop_poll = pn533_stop_poll,
2351 .activate_target = pn533_activate_target,
2352 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002353 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002354 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002355};
2356
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002357static int pn533_setup(struct pn533 *dev)
2358{
2359 struct pn533_config_max_retries max_retries;
2360 struct pn533_config_timing timing;
2361 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2362 int rc;
2363
2364 switch (dev->device_type) {
2365 case PN533_DEVICE_STD:
2366 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2367 max_retries.mx_rty_psl = 2;
2368 max_retries.mx_rty_passive_act =
2369 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2370
2371 timing.rfu = PN533_CONFIG_TIMING_102;
2372 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2373 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2374
2375 break;
2376
2377 case PN533_DEVICE_PASORI:
2378 max_retries.mx_rty_atr = 0x2;
2379 max_retries.mx_rty_psl = 0x1;
2380 max_retries.mx_rty_passive_act =
2381 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2382
2383 timing.rfu = PN533_CONFIG_TIMING_102;
2384 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2385 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2386
2387 break;
2388
2389 default:
2390 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2391 dev->device_type);
2392 return -EINVAL;
2393 }
2394
2395 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2396 (u8 *)&max_retries, sizeof(max_retries));
2397 if (rc) {
2398 nfc_dev_err(&dev->interface->dev,
2399 "Error on setting MAX_RETRIES config");
2400 return rc;
2401 }
2402
2403
2404 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2405 (u8 *)&timing, sizeof(timing));
2406 if (rc) {
2407 nfc_dev_err(&dev->interface->dev,
2408 "Error on setting RF timings");
2409 return rc;
2410 }
2411
2412 switch (dev->device_type) {
2413 case PN533_DEVICE_STD:
2414 break;
2415
2416 case PN533_DEVICE_PASORI:
2417 pn533_fw_reset(dev);
2418
2419 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2420 pasori_cfg, 3);
2421 if (rc) {
2422 nfc_dev_err(&dev->interface->dev,
2423 "Error while settings PASORI config");
2424 return rc;
2425 }
2426
2427 pn533_fw_reset(dev);
2428
2429 break;
2430 }
2431
2432 return 0;
2433}
2434
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002435static int pn533_probe(struct usb_interface *interface,
2436 const struct usb_device_id *id)
2437{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002438 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002439 struct pn533 *dev;
2440 struct usb_host_interface *iface_desc;
2441 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002442 int in_endpoint = 0;
2443 int out_endpoint = 0;
2444 int rc = -ENOMEM;
2445 int i;
2446 u32 protocols;
2447
2448 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2449 if (!dev)
2450 return -ENOMEM;
2451
2452 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2453 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002454 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455
2456 iface_desc = interface->cur_altsetting;
2457 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2458 endpoint = &iface_desc->endpoint[i].desc;
2459
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002460 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002461 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002462
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002463 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002464 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002465 }
2466
2467 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002468 nfc_dev_err(&interface->dev,
2469 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002470 rc = -ENODEV;
2471 goto error;
2472 }
2473
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002474 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002475 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002476 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2477
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +01002478 if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002479 goto error;
2480
2481 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002482 usb_rcvbulkpipe(dev->udev, in_endpoint),
2483 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002485 usb_sndbulkpipe(dev->udev, out_endpoint),
2486 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002488 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2489 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002490 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002491 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002492 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002493 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002494 if (dev->wq == NULL)
2495 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002496
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002497 init_timer(&dev->listen_timer);
2498 dev->listen_timer.data = (unsigned long) dev;
2499 dev->listen_timer.function = pn533_listen_mode_timer;
2500
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002501 skb_queue_head_init(&dev->resp_q);
2502
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002503 INIT_LIST_HEAD(&dev->cmd_queue);
2504
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505 usb_set_intfdata(interface, dev);
2506
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002507 memset(&fw_ver, 0, sizeof(fw_ver));
2508 rc = pn533_get_firmware_version(dev, &fw_ver);
2509 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002510 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002511
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002512 nfc_dev_info(&dev->interface->dev,
2513 "NXP PN533 firmware ver %d.%d now attached",
2514 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002515
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002516 dev->device_type = id->driver_info;
2517 switch (dev->device_type) {
2518 case PN533_DEVICE_STD:
2519 protocols = PN533_ALL_PROTOCOLS;
2520 break;
2521
2522 case PN533_DEVICE_PASORI:
2523 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2524 break;
2525
2526 default:
2527 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2528 dev->device_type);
2529 rc = -EINVAL;
2530 goto destroy_wq;
2531 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002532
Samuel Ortize8753042011-08-19 15:47:11 +02002533 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002534 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002535 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002536 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002538 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002539
2540 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2541 nfc_set_drvdata(dev->nfc_dev, dev);
2542
2543 rc = nfc_register_device(dev->nfc_dev);
2544 if (rc)
2545 goto free_nfc_dev;
2546
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002547 rc = pn533_setup(dev);
2548 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002549 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002550
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002551 return 0;
2552
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002553unregister_nfc_dev:
2554 nfc_unregister_device(dev->nfc_dev);
2555
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002556free_nfc_dev:
2557 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002558
Samuel Ortiz4849f852012-04-10 19:43:17 +02002559destroy_wq:
2560 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002561error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002562 usb_free_urb(dev->in_urb);
2563 kfree(dev->out_frame);
2564 usb_free_urb(dev->out_urb);
2565 kfree(dev);
2566 return rc;
2567}
2568
2569static void pn533_disconnect(struct usb_interface *interface)
2570{
2571 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002572 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002573
2574 dev = usb_get_intfdata(interface);
2575 usb_set_intfdata(interface, NULL);
2576
2577 nfc_unregister_device(dev->nfc_dev);
2578 nfc_free_device(dev->nfc_dev);
2579
2580 usb_kill_urb(dev->in_urb);
2581 usb_kill_urb(dev->out_urb);
2582
Samuel Ortiz4849f852012-04-10 19:43:17 +02002583 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002584
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002585 skb_queue_purge(&dev->resp_q);
2586
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002587 del_timer(&dev->listen_timer);
2588
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002589 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2590 list_del(&cmd->queue);
2591 kfree(cmd);
2592 }
2593
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594 usb_free_urb(dev->in_urb);
2595 kfree(dev->out_frame);
2596 usb_free_urb(dev->out_urb);
2597 kfree(dev);
2598
Dan Carpenter276556d2011-07-08 10:21:15 +03002599 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002600}
2601
2602static struct usb_driver pn533_driver = {
2603 .name = "pn533",
2604 .probe = pn533_probe,
2605 .disconnect = pn533_disconnect,
2606 .id_table = pn533_table,
2607};
2608
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002609module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002610
2611MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2612 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2613MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2614MODULE_VERSION(VERSION);
2615MODULE_LICENSE("GPL");