blob: cfbfc1183926287c93b7fd7dc8e4b8095db79d5d [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)
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010097#define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
98 Postamble (1) */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030099#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
100#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
101
102/* start of frame */
103#define PN533_SOF 0x00FF
104
105/* frame identifier: in/out/error */
106#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
107#define PN533_DIR_OUT 0xD4
108#define PN533_DIR_IN 0xD5
109
110/* PN533 Commands */
111#define PN533_FRAME_CMD(f) (f->data[1])
112#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
113#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
114
115#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
116#define PN533_CMD_RF_CONFIGURATION 0x32
117#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200118#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
120#define PN533_CMD_IN_ATR 0x50
121#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100122#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300123
Samuel Ortizad3823c2012-05-30 23:54:55 +0200124#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200125#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200126#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100127#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200128
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300129#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
130
131/* PN533 Return codes */
132#define PN533_CMD_RET_MASK 0x3F
133#define PN533_CMD_MI_MASK 0x40
134#define PN533_CMD_RET_SUCCESS 0x00
135
136struct pn533;
137
138typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
139 u8 *params, int params_len);
140
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100141typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
142 struct sk_buff *resp);
143
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300144/* structs for pn533 commands */
145
146/* PN533_CMD_GET_FIRMWARE_VERSION */
147struct pn533_fw_version {
148 u8 ic;
149 u8 ver;
150 u8 rev;
151 u8 support;
152};
153
154/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200155#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300156#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200157#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300158
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200159#define PN533_CONFIG_TIMING_102 0xb
160#define PN533_CONFIG_TIMING_204 0xc
161#define PN533_CONFIG_TIMING_409 0xd
162#define PN533_CONFIG_TIMING_819 0xe
163
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300164#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
165#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
166
167struct pn533_config_max_retries {
168 u8 mx_rty_atr;
169 u8 mx_rty_psl;
170 u8 mx_rty_passive_act;
171} __packed;
172
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200173struct pn533_config_timing {
174 u8 rfu;
175 u8 atr_res_timeout;
176 u8 dep_timeout;
177} __packed;
178
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300179/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
180
181/* felica commands opcode */
182#define PN533_FELICA_OPC_SENSF_REQ 0
183#define PN533_FELICA_OPC_SENSF_RES 1
184/* felica SENSF_REQ parameters */
185#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
186#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
187#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
188#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
189
190/* type B initiator_data values */
191#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
192#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
193#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
194
195union pn533_cmd_poll_initdata {
196 struct {
197 u8 afi;
198 u8 polling_method;
199 } __packed type_b;
200 struct {
201 u8 opcode;
202 __be16 sc;
203 u8 rc;
204 u8 tsn;
205 } __packed felica;
206};
207
208/* Poll modulations */
209enum {
210 PN533_POLL_MOD_106KBPS_A,
211 PN533_POLL_MOD_212KBPS_FELICA,
212 PN533_POLL_MOD_424KBPS_FELICA,
213 PN533_POLL_MOD_106KBPS_JEWEL,
214 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200215 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300216
217 __PN533_POLL_MOD_AFTER_LAST,
218};
219#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
220
221struct pn533_poll_modulations {
222 struct {
223 u8 maxtg;
224 u8 brty;
225 union pn533_cmd_poll_initdata initiator_data;
226 } __packed data;
227 u8 len;
228};
229
230const struct pn533_poll_modulations poll_mod[] = {
231 [PN533_POLL_MOD_106KBPS_A] = {
232 .data = {
233 .maxtg = 1,
234 .brty = 0,
235 },
236 .len = 2,
237 },
238 [PN533_POLL_MOD_212KBPS_FELICA] = {
239 .data = {
240 .maxtg = 1,
241 .brty = 1,
242 .initiator_data.felica = {
243 .opcode = PN533_FELICA_OPC_SENSF_REQ,
244 .sc = PN533_FELICA_SENSF_SC_ALL,
245 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
246 .tsn = 0,
247 },
248 },
249 .len = 7,
250 },
251 [PN533_POLL_MOD_424KBPS_FELICA] = {
252 .data = {
253 .maxtg = 1,
254 .brty = 2,
255 .initiator_data.felica = {
256 .opcode = PN533_FELICA_OPC_SENSF_REQ,
257 .sc = PN533_FELICA_SENSF_SC_ALL,
258 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
259 .tsn = 0,
260 },
261 },
262 .len = 7,
263 },
264 [PN533_POLL_MOD_106KBPS_JEWEL] = {
265 .data = {
266 .maxtg = 1,
267 .brty = 4,
268 },
269 .len = 2,
270 },
271 [PN533_POLL_MOD_847KBPS_B] = {
272 .data = {
273 .maxtg = 1,
274 .brty = 8,
275 .initiator_data.type_b = {
276 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
277 .polling_method =
278 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
279 },
280 },
281 .len = 3,
282 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200283 [PN533_LISTEN_MOD] = {
284 .len = 0,
285 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300286};
287
288/* PN533_CMD_IN_ATR */
289
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300290struct pn533_cmd_activate_response {
291 u8 status;
292 u8 nfcid3t[10];
293 u8 didt;
294 u8 bst;
295 u8 brt;
296 u8 to;
297 u8 ppt;
298 /* optional */
299 u8 gt[];
300} __packed;
301
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100302struct pn533_cmd_jump_dep_response {
303 u8 status;
304 u8 tg;
305 u8 nfcid3t[10];
306 u8 didt;
307 u8 bst;
308 u8 brt;
309 u8 to;
310 u8 ppt;
311 /* optional */
312 u8 gt[];
313} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300314
Samuel Ortizad3823c2012-05-30 23:54:55 +0200315
316/* PN533_TG_INIT_AS_TARGET */
317#define PN533_INIT_TARGET_PASSIVE 0x1
318#define PN533_INIT_TARGET_DEP 0x2
319
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200320#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
321#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
322#define PN533_INIT_TARGET_RESP_DEP 0x4
323
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300324struct pn533 {
325 struct usb_device *udev;
326 struct usb_interface *interface;
327 struct nfc_dev *nfc_dev;
328
329 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300330 struct pn533_frame *out_frame;
331
332 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300333
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200334 struct sk_buff_head resp_q;
335
Samuel Ortiz4849f852012-04-10 19:43:17 +0200336 struct workqueue_struct *wq;
337 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200338 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200339 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200340 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200341 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200342 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200343 struct pn533_frame *wq_in_frame;
344 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200345 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300346
347 pn533_cmd_complete_t cmd_complete;
348 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100349 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200350 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300351 u8 cmd;
352
353 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
354 u8 poll_mod_count;
355 u8 poll_mod_curr;
356 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200357 u32 listen_protocols;
358
359 u8 *gb;
360 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300361
362 u8 tgt_available_prots;
363 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200364 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200365
366 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200367
368 struct list_head cmd_queue;
369 u8 cmd_pending;
370};
371
372struct pn533_cmd {
373 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100374 u8 cmd_code;
375 struct sk_buff *req;
376 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200377 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300378};
379
380struct pn533_frame {
381 u8 preamble;
382 __be16 start_frame;
383 u8 datalen;
384 u8 datalen_checksum;
385 u8 data[];
386} __packed;
387
388/* The rule: value + checksum = 0 */
389static inline u8 pn533_checksum(u8 value)
390{
391 return ~value + 1;
392}
393
394/* The rule: sum(data elements) + checksum = 0 */
395static u8 pn533_data_checksum(u8 *data, int datalen)
396{
397 u8 sum = 0;
398 int i;
399
400 for (i = 0; i < datalen; i++)
401 sum += data[i];
402
403 return pn533_checksum(sum);
404}
405
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300406static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
407{
408 frame->preamble = 0;
409 frame->start_frame = cpu_to_be16(PN533_SOF);
410 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
411 PN533_FRAME_CMD(frame) = cmd;
412 frame->datalen = 2;
413}
414
415static void pn533_tx_frame_finish(struct pn533_frame *frame)
416{
417 frame->datalen_checksum = pn533_checksum(frame->datalen);
418
419 PN533_FRAME_CHECKSUM(frame) =
420 pn533_data_checksum(frame->data, frame->datalen);
421
422 PN533_FRAME_POSTAMBLE(frame) = 0;
423}
424
425static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
426{
427 u8 checksum;
428
429 if (frame->start_frame != cpu_to_be16(PN533_SOF))
430 return false;
431
432 checksum = pn533_checksum(frame->datalen);
433 if (checksum != frame->datalen_checksum)
434 return false;
435
436 checksum = pn533_data_checksum(frame->data, frame->datalen);
437 if (checksum != PN533_FRAME_CHECKSUM(frame))
438 return false;
439
440 return true;
441}
442
443static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
444{
445 if (frame->start_frame != cpu_to_be16(PN533_SOF))
446 return false;
447
448 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
449 return false;
450
451 return true;
452}
453
454static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
455{
456 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
457}
458
Samuel Ortiz4849f852012-04-10 19:43:17 +0200459
460static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300461{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200462 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200463 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300464 int rc;
465
Samuel Ortiz4849f852012-04-10 19:43:17 +0200466 in_frame = dev->wq_in_frame;
467
468 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300469 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200470 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300471 else
472 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
473 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
474 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
475
476 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200477 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300478}
479
480static void pn533_recv_response(struct urb *urb)
481{
482 struct pn533 *dev = urb->context;
483 struct pn533_frame *in_frame;
484
Samuel Ortiz4849f852012-04-10 19:43:17 +0200485 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300486
487 switch (urb->status) {
488 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100489 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300490 case -ECONNRESET:
491 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100492 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100493 "The urb has been canceled (status %d)",
494 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200495 dev->wq_in_error = urb->status;
496 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100497 break;
498 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300499 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100500 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100501 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200502 dev->wq_in_error = urb->status;
503 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300504 }
505
506 in_frame = dev->in_urb->transfer_buffer;
507
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100508 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100509 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
510 in_frame, PN533_FRAME_SIZE(in_frame), false);
511
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300512 if (!pn533_rx_frame_is_valid(in_frame)) {
513 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200514 dev->wq_in_error = -EIO;
515 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516 }
517
518 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100519 nfc_dev_err(&dev->interface->dev,
520 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200521 dev->wq_in_error = -EIO;
522 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523 }
524
Samuel Ortiz4849f852012-04-10 19:43:17 +0200525 dev->wq_in_error = 0;
526 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300527
Samuel Ortiz4849f852012-04-10 19:43:17 +0200528sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200529 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300530}
531
532static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
533{
534 dev->in_urb->complete = pn533_recv_response;
535
536 return usb_submit_urb(dev->in_urb, flags);
537}
538
539static void pn533_recv_ack(struct urb *urb)
540{
541 struct pn533 *dev = urb->context;
542 struct pn533_frame *in_frame;
543 int rc;
544
545 switch (urb->status) {
546 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100547 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 case -ECONNRESET:
549 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100550 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100551 "The urb has been stopped (status %d)",
552 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200553 dev->wq_in_error = urb->status;
554 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100555 break;
556 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300557 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100558 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100559 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200560 dev->wq_in_error = urb->status;
561 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300562 }
563
564 in_frame = dev->in_urb->transfer_buffer;
565
566 if (!pn533_rx_frame_is_ack(in_frame)) {
567 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200568 dev->wq_in_error = -EIO;
569 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300570 }
571
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300572 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
573 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100574 nfc_dev_err(&dev->interface->dev,
575 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200576 dev->wq_in_error = rc;
577 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300578 }
579
580 return;
581
Samuel Ortiz4849f852012-04-10 19:43:17 +0200582sched_wq:
583 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200584 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585}
586
587static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
588{
589 dev->in_urb->complete = pn533_recv_ack;
590
591 return usb_submit_urb(dev->in_urb, flags);
592}
593
594static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
595{
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100596 u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
597 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300598 int rc;
599
600 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
601
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100602 dev->out_urb->transfer_buffer = ack;
603 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300604 rc = usb_submit_urb(dev->out_urb, flags);
605
606 return rc;
607}
608
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100609static int __pn533_send_frame_async(struct pn533 *dev,
610 struct sk_buff *out,
611 struct sk_buff *in,
612 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300613 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100614 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300615{
616 int rc;
617
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100618 dev->cmd = PN533_FRAME_CMD(((struct pn533_frame *)out->data));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300619 dev->cmd_complete = cmd_complete;
620 dev->cmd_complete_arg = arg;
621
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100622 dev->out_urb->transfer_buffer = out->data;
623 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300624
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100625 dev->in_urb->transfer_buffer = in->data;
626 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300627
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100628 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100629 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100630
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100631 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300632 if (rc)
633 return rc;
634
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100635 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300636 if (rc)
637 goto error;
638
639 return 0;
640
641error:
642 usb_unlink_urb(dev->out_urb);
643 return rc;
644}
645
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100646static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
647{
648 struct pn533_frame *frame;
649 /* payload is already there, just update datalen */
650 int payload_len = skb->len;
651
652 skb_push(skb, PN533_FRAME_HEADER_LEN);
653 skb_put(skb, PN533_FRAME_TAIL_LEN);
654
655 frame = (struct pn533_frame *)skb->data;
656
657 pn533_tx_frame_init(frame, cmd_code);
658 frame->datalen += payload_len;
659 pn533_tx_frame_finish(frame);
660}
661
662struct pn533_send_async_complete_arg {
663 pn533_send_async_complete_t complete_cb;
664 void *complete_cb_context;
665 struct sk_buff *resp;
666 struct sk_buff *req;
667};
668
669static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
670 int params_len)
671{
672 struct pn533_send_async_complete_arg *arg = _arg;
673
674 struct sk_buff *req = arg->req;
675 struct sk_buff *resp = arg->resp;
676
677 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
678 int rc;
679
680 dev_kfree_skb(req);
681
682 if (params_len < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100683 arg->complete_cb(dev, arg->complete_cb_context,
684 ERR_PTR(params_len));
685 rc = params_len;
686 dev_kfree_skb(resp);
687 goto out;
688 }
689
690 skb_put(resp, PN533_FRAME_SIZE(frame));
691 skb_pull(resp, PN533_FRAME_HEADER_LEN);
692 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
693
694 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
695
696out:
697 kfree(arg);
698 return rc;
699}
700
701static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
702 struct sk_buff *req, struct sk_buff *resp,
703 int resp_len,
704 pn533_send_async_complete_t complete_cb,
705 void *complete_cb_context)
706{
707 struct pn533_cmd *cmd;
708 struct pn533_send_async_complete_arg *arg;
709 int rc = 0;
710
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100711 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100712
713 arg = kzalloc(sizeof(arg), GFP_KERNEL);
714 if (!arg)
715 return -ENOMEM;
716
717 arg->complete_cb = complete_cb;
718 arg->complete_cb_context = complete_cb_context;
719 arg->resp = resp;
720 arg->req = req;
721
722 pn533_build_cmd_frame(cmd_code, req);
723
724 mutex_lock(&dev->cmd_lock);
725
726 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100727 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
728 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100729 if (rc)
730 goto error;
731
732 dev->cmd_pending = 1;
733 goto unlock;
734 }
735
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100736 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
737 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100738
739 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
740 if (!cmd) {
741 rc = -ENOMEM;
742 goto error;
743 }
744
745 INIT_LIST_HEAD(&cmd->queue);
746 cmd->cmd_code = cmd_code;
747 cmd->req = req;
748 cmd->resp = resp;
749 cmd->arg = arg;
750
751 list_add_tail(&cmd->queue, &dev->cmd_queue);
752
753 goto unlock;
754
755error:
756 kfree(arg);
757unlock:
758 mutex_unlock(&dev->cmd_lock);
759 return rc;
760}
761
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100762static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
763 struct sk_buff *req,
764 pn533_send_async_complete_t complete_cb,
765 void *complete_cb_context)
766{
767 struct sk_buff *resp;
768 int rc;
769 int resp_len = PN533_FRAME_HEADER_LEN +
770 PN533_FRAME_MAX_PAYLOAD_LEN +
771 PN533_FRAME_TAIL_LEN;
772
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100773 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
774 if (!resp)
775 return -ENOMEM;
776
777 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
778 complete_cb_context);
779 if (rc)
780 dev_kfree_skb(resp);
781
782 return rc;
783}
784
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100785static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
786 struct sk_buff *req,
787 pn533_send_async_complete_t complete_cb,
788 void *complete_cb_context)
789{
790 struct sk_buff *resp;
791 int rc;
792
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100793 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
794 if (!resp)
795 return -ENOMEM;
796
797 rc = __pn533_send_async(dev, cmd_code, req, resp,
798 PN533_NORMAL_FRAME_MAX_LEN,
799 complete_cb, complete_cb_context);
800 if (rc)
801 dev_kfree_skb(resp);
802
803 return rc;
804}
805
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100806/*
807 * pn533_send_cmd_direct_async
808 *
809 * The function sends a piority cmd directly to the chip omiting the cmd
810 * queue. It's intended to be used by chaining mechanism of received responses
811 * where the host has to request every single chunk of data before scheduling
812 * next cmd from the queue.
813 */
814static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
815 struct sk_buff *req,
816 pn533_send_async_complete_t complete_cb,
817 void *complete_cb_context)
818{
819 struct pn533_send_async_complete_arg *arg;
820 struct sk_buff *resp;
821 int rc;
822 int resp_len = PN533_FRAME_HEADER_LEN +
823 PN533_FRAME_MAX_PAYLOAD_LEN +
824 PN533_FRAME_TAIL_LEN;
825
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100826 resp = alloc_skb(resp_len, GFP_KERNEL);
827 if (!resp)
828 return -ENOMEM;
829
830 arg = kzalloc(sizeof(arg), GFP_KERNEL);
831 if (!arg) {
832 dev_kfree_skb(resp);
833 return -ENOMEM;
834 }
835
836 arg->complete_cb = complete_cb;
837 arg->complete_cb_context = complete_cb_context;
838 arg->resp = resp;
839 arg->req = req;
840
841 pn533_build_cmd_frame(cmd_code, req);
842
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100843 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
844 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100845 if (rc < 0) {
846 dev_kfree_skb(resp);
847 kfree(arg);
848 }
849
850 return rc;
851}
852
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200853static void pn533_wq_cmd(struct work_struct *work)
854{
855 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
856 struct pn533_cmd *cmd;
857
858 mutex_lock(&dev->cmd_lock);
859
860 if (list_empty(&dev->cmd_queue)) {
861 dev->cmd_pending = 0;
862 mutex_unlock(&dev->cmd_lock);
863 return;
864 }
865
866 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
867
Szymon Janc60ad07a2012-10-25 17:29:45 +0200868 list_del(&cmd->queue);
869
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200870 mutex_unlock(&dev->cmd_lock);
871
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100872 __pn533_send_frame_async(dev, cmd->req, cmd->resp,
873 PN533_NORMAL_FRAME_MAX_LEN,
874 pn533_send_async_complete,
875 cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200876
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200877 kfree(cmd);
878}
879
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300880struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100881 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300882 struct completion done;
883};
884
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100885static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
886 struct sk_buff *resp)
887{
888 struct pn533_sync_cmd_response *arg = _arg;
889
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100890 arg->resp = resp;
891 complete(&arg->done);
892
893 return 0;
894}
895
896/* pn533_send_cmd_sync
897 *
898 * Please note the req parameter is freed inside the function to
899 * limit a number of return value interpretations by the caller.
900 *
901 * 1. negative in case of error during TX path -> req should be freed
902 *
903 * 2. negative in case of error during RX path -> req should not be freed
904 * as it's been already freed at the begining of RX path by
905 * async_complete_cb.
906 *
907 * 3. valid pointer in case of succesfult RX path
908 *
909 * A caller has to check a return value with IS_ERR macro. If the test pass,
910 * the returned pointer is valid.
911 *
912 * */
913static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
914 struct sk_buff *req)
915{
916 int rc;
917 struct pn533_sync_cmd_response arg;
918
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100919 init_completion(&arg.done);
920
921 rc = pn533_send_cmd_async(dev, cmd_code, req,
922 pn533_send_sync_complete, &arg);
923 if (rc) {
924 dev_kfree_skb(req);
925 return ERR_PTR(rc);
926 }
927
928 wait_for_completion(&arg.done);
929
930 return arg.resp;
931}
932
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300933static void pn533_send_complete(struct urb *urb)
934{
935 struct pn533 *dev = urb->context;
936
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300937 switch (urb->status) {
938 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100939 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300940 case -ECONNRESET:
941 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100942 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100943 "The urb has been stopped (status %d)",
944 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300945 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100946 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300947 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100948 nfc_dev_err(&dev->interface->dev,
949 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300950 }
951}
952
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100953static struct sk_buff *pn533_alloc_skb(unsigned int size)
954{
955 struct sk_buff *skb;
956
957 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
958 size +
959 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
960
961 if (skb)
962 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
963
964 return skb;
965}
966
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300967struct pn533_target_type_a {
968 __be16 sens_res;
969 u8 sel_res;
970 u8 nfcid_len;
971 u8 nfcid_data[];
972} __packed;
973
974
975#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
976#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
977#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
978
979#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
980#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
981
982#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
983#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
984
985#define PN533_TYPE_A_SEL_PROT_MIFARE 0
986#define PN533_TYPE_A_SEL_PROT_ISO14443 1
987#define PN533_TYPE_A_SEL_PROT_DEP 2
988#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
989
990static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
991 int target_data_len)
992{
993 u8 ssd;
994 u8 platconf;
995
996 if (target_data_len < sizeof(struct pn533_target_type_a))
997 return false;
998
999 /* The lenght check of nfcid[] and ats[] are not being performed because
1000 the values are not being used */
1001
1002 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1003 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1004 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1005
1006 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001007 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1008 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1009 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001010 return false;
1011
1012 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1013 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1014 return false;
1015
1016 return true;
1017}
1018
1019static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1020 int tgt_data_len)
1021{
1022 struct pn533_target_type_a *tgt_type_a;
1023
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001024 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001025
1026 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1027 return -EPROTO;
1028
1029 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1030 case PN533_TYPE_A_SEL_PROT_MIFARE:
1031 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1032 break;
1033 case PN533_TYPE_A_SEL_PROT_ISO14443:
1034 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1035 break;
1036 case PN533_TYPE_A_SEL_PROT_DEP:
1037 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1038 break;
1039 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1040 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1041 NFC_PROTO_NFC_DEP_MASK;
1042 break;
1043 }
1044
1045 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1046 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001047 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1048 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001049
1050 return 0;
1051}
1052
1053struct pn533_target_felica {
1054 u8 pol_res;
1055 u8 opcode;
1056 u8 nfcid2[8];
1057 u8 pad[8];
1058 /* optional */
1059 u8 syst_code[];
1060} __packed;
1061
1062#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1063#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1064
1065static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1066 int target_data_len)
1067{
1068 if (target_data_len < sizeof(struct pn533_target_felica))
1069 return false;
1070
1071 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1072 return false;
1073
1074 return true;
1075}
1076
1077static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1078 int tgt_data_len)
1079{
1080 struct pn533_target_felica *tgt_felica;
1081
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001082 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001083
1084 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1085 return -EPROTO;
1086
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001087 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1088 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001089 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1090 else
1091 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1092
Samuel Ortiz79757542012-03-05 01:03:45 +01001093 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1094 nfc_tgt->sensf_res_len = 9;
1095
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001096 return 0;
1097}
1098
1099struct pn533_target_jewel {
1100 __be16 sens_res;
1101 u8 jewelid[4];
1102} __packed;
1103
1104static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1105 int target_data_len)
1106{
1107 u8 ssd;
1108 u8 platconf;
1109
1110 if (target_data_len < sizeof(struct pn533_target_jewel))
1111 return false;
1112
1113 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1114 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1115 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1116
1117 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001118 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1119 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1120 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001121 return false;
1122
1123 return true;
1124}
1125
1126static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1127 int tgt_data_len)
1128{
1129 struct pn533_target_jewel *tgt_jewel;
1130
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001131 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001132
1133 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1134 return -EPROTO;
1135
1136 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1137 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001138 nfc_tgt->nfcid1_len = 4;
1139 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001140
1141 return 0;
1142}
1143
1144struct pn533_type_b_prot_info {
1145 u8 bitrate;
1146 u8 fsci_type;
1147 u8 fwi_adc_fo;
1148} __packed;
1149
1150#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1151#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1152#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1153
1154struct pn533_type_b_sens_res {
1155 u8 opcode;
1156 u8 nfcid[4];
1157 u8 appdata[4];
1158 struct pn533_type_b_prot_info prot_info;
1159} __packed;
1160
1161#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1162
1163struct pn533_target_type_b {
1164 struct pn533_type_b_sens_res sensb_res;
1165 u8 attrib_res_len;
1166 u8 attrib_res[];
1167} __packed;
1168
1169static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1170 int target_data_len)
1171{
1172 if (target_data_len < sizeof(struct pn533_target_type_b))
1173 return false;
1174
1175 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1176 return false;
1177
1178 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1179 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1180 return false;
1181
1182 return true;
1183}
1184
1185static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1186 int tgt_data_len)
1187{
1188 struct pn533_target_type_b *tgt_type_b;
1189
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001190 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001191
1192 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1193 return -EPROTO;
1194
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001195 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001196
1197 return 0;
1198}
1199
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001200static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1201 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001202{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001203 struct nfc_target nfc_tgt;
1204 int rc;
1205
1206 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001207 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001208
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001209 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001210 return -EPROTO;
1211
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001212 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1213
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001214 switch (dev->poll_mod_curr) {
1215 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001216 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001217 break;
1218 case PN533_POLL_MOD_212KBPS_FELICA:
1219 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001220 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001221 break;
1222 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001223 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001224 break;
1225 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001226 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001227 break;
1228 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001229 nfc_dev_err(&dev->interface->dev,
1230 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001231 return -EPROTO;
1232 }
1233
1234 if (rc)
1235 return rc;
1236
1237 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001238 nfc_dev_dbg(&dev->interface->dev,
1239 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001240 return -EAGAIN;
1241 }
1242
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001243 nfc_dev_dbg(&dev->interface->dev,
1244 "Target found - supported protocols: 0x%x",
1245 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001246
1247 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1248
1249 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1250
1251 return 0;
1252}
1253
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001254static inline void pn533_poll_next_mod(struct pn533 *dev)
1255{
1256 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1257}
1258
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001259static void pn533_poll_reset_mod_list(struct pn533 *dev)
1260{
1261 dev->poll_mod_count = 0;
1262}
1263
1264static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1265{
1266 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001267 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001268 dev->poll_mod_count++;
1269}
1270
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001271static void pn533_poll_create_mod_list(struct pn533 *dev,
1272 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001273{
1274 pn533_poll_reset_mod_list(dev);
1275
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001276 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1277 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1278 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001279 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1280
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001281 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1282 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001283 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1284 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1285 }
1286
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001287 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001288 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1289
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001290 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001291 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001292
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001293 if (tm_protocols)
1294 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295}
1296
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001297static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001298{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001299 u8 nbtg, tg, *tgdata;
1300 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001301
1302 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1303
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001304 nbtg = resp->data[0];
1305 tg = resp->data[1];
1306 tgdata = &resp->data[2];
1307 tgdata_len = resp->len - 2; /* nbtg + tg */
1308
1309 if (nbtg) {
1310 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001311
1312 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001313 if (rc == 0) {
1314 pn533_poll_reset_mod_list(dev);
1315 return 0;
1316 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317 }
1318
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001319 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001320}
1321
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001322static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001323{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001324 struct sk_buff *skb;
1325 u8 *felica, *nfcid3, *gb;
1326
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001327 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1328 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1329 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1330 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001331
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001332 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1333 0x0, 0x0, 0x0,
1334 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001335
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001336 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1337 felica (18), nfcid3 (10), gb_len (1) */
1338 gbytes_len +
1339 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001340
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001341 skb = pn533_alloc_skb(skb_len);
1342 if (!skb)
1343 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001344
1345 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001346 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001347
1348 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001349 memcpy(skb_put(skb, 6), mifare_params, 6);
1350
1351 /* Felica params */
1352 felica = skb_put(skb, 18);
1353 memcpy(felica, felica_params, 18);
1354 get_random_bytes(felica + 2, 6);
1355
1356 /* NFCID3 */
1357 nfcid3 = skb_put(skb, 10);
1358 memset(nfcid3, 0, 10);
1359 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001360
1361 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001362 *skb_put(skb, 1) = gbytes_len;
1363
1364 gb = skb_put(skb, gbytes_len);
1365 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001366
Samuel Ortizad3823c2012-05-30 23:54:55 +02001367 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001368 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001369
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001370 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001371}
1372
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001373#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001374#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1375static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001376 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001377{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001378 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001379
1380 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1381
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001382 if (IS_ERR(resp))
1383 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001384
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001385 status = resp->data[0];
1386 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001387
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001388 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001389 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001390 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001391 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001392 return 0;
1393 }
1394
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001395 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001396}
1397
1398static void pn533_wq_tg_get_data(struct work_struct *work)
1399{
1400 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001401
1402 struct sk_buff *skb;
1403 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001404
1405 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1406
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001407 skb = pn533_alloc_skb(0);
1408 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001409 return;
1410
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001411 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1412 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001413
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001414 if (rc < 0)
1415 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001416
1417 return;
1418}
1419
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001420#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001421static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001422{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001423 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001424 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001425 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001426
1427 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1428
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001429 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001430 return -EINVAL;
1431
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001432 mode = resp->data[0];
1433 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001434
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001435 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1436 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001437
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001438 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1439 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001440 comm_mode = NFC_COMM_ACTIVE;
1441
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001442 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001443 return -EOPNOTSUPP;
1444
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001445 gb = cmd + ATR_REQ_GB_OFFSET;
1446 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001447
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001448 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1449 comm_mode, gb, gb_len);
1450 if (rc < 0) {
1451 nfc_dev_err(&dev->interface->dev,
1452 "Error when signaling target activation");
1453 return rc;
1454 }
1455
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001456 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001457 queue_work(dev->wq, &dev->tg_work);
1458
1459 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001460}
1461
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001462static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001463{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001464 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001465
1466 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1467
1468 /* An ack will cancel the last issued command (poll) */
1469 pn533_send_ack(dev, GFP_ATOMIC);
1470
1471 dev->cancel_listen = 1;
1472
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001473 pn533_poll_next_mod(dev);
1474
1475 queue_work(dev->wq, &dev->poll_work);
1476}
1477
1478static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001479 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001480{
1481 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001482 int rc;
1483
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001484 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1485
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001486 if (IS_ERR(resp)) {
1487 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001488
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001489 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1490 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001491
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001492 if (rc == -ENOENT) {
1493 if (dev->poll_mod_count != 0)
1494 return rc;
1495 else
1496 goto stop_poll;
1497 } else if (rc < 0) {
1498 nfc_dev_err(&dev->interface->dev,
1499 "Error %d when running poll", rc);
1500 goto stop_poll;
1501 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502 }
1503
1504 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1505
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001506 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001507 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001508 rc = pn533_init_target_complete(dev, resp);
1509 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510 }
1511
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001512 /* Initiator mode */
1513 rc = pn533_start_poll_complete(dev, resp);
1514 if (!rc)
1515 goto done;
1516
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001517 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518 queue_work(dev->wq, &dev->poll_work);
1519
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001520done:
1521 dev_kfree_skb(resp);
1522 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001523
1524stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001525 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1526
Samuel Ortizad3823c2012-05-30 23:54:55 +02001527 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001529 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001530}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001531
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001532static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1533 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001534{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001535 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001536
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001537 skb = pn533_alloc_skb(mod->len);
1538 if (!skb)
1539 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001540
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001541 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001542
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001543 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544}
1545
1546static int pn533_send_poll_frame(struct pn533 *dev)
1547{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001548 struct pn533_poll_modulations *mod;
1549 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001550 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001553 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001555 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1556 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001557
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001558 if (mod->len == 0) { /* Listen mode */
1559 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1560 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1561 } else { /* Polling mode */
1562 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1563 skb = pn533_alloc_poll_in_frame(mod);
1564 }
1565
1566 if (!skb) {
1567 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1568 return -ENOMEM;
1569 }
1570
1571 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1572 NULL);
1573 if (rc < 0) {
1574 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001575 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001577
1578 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001579}
1580
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001582{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001583 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1584 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001585 int rc;
1586
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001587 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1588
1589 nfc_dev_dbg(&dev->interface->dev,
1590 "%s cancel_listen %d modulation len %d",
1591 __func__, dev->cancel_listen, cur_mod->len);
1592
1593 if (dev->cancel_listen == 1) {
1594 dev->cancel_listen = 0;
1595 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001596 }
1597
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001598 rc = pn533_send_poll_frame(dev);
1599 if (rc)
1600 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001601
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001602 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1603 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001604
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001605 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001606}
1607
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001608static int pn533_start_poll(struct nfc_dev *nfc_dev,
1609 u32 im_protocols, u32 tm_protocols)
1610{
1611 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1612
1613 nfc_dev_dbg(&dev->interface->dev,
1614 "%s: im protocols 0x%x tm protocols 0x%x",
1615 __func__, im_protocols, tm_protocols);
1616
1617 if (dev->tgt_active_prot) {
1618 nfc_dev_err(&dev->interface->dev,
1619 "Cannot poll with a target already activated");
1620 return -EBUSY;
1621 }
1622
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001623 if (dev->tgt_mode) {
1624 nfc_dev_err(&dev->interface->dev,
1625 "Cannot poll while already being activated");
1626 return -EBUSY;
1627 }
1628
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001629 if (tm_protocols) {
1630 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1631 if (dev->gb == NULL)
1632 tm_protocols = 0;
1633 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001634
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001635 dev->poll_mod_curr = 0;
1636 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1637 dev->poll_protocols = im_protocols;
1638 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001639
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001640 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001641}
1642
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001643static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1644{
1645 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1646
1647 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1648
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001649 del_timer(&dev->listen_timer);
1650
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001651 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001652 nfc_dev_dbg(&dev->interface->dev,
1653 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001654 return;
1655 }
1656
1657 /* An ack will cancel the last issued command (poll) */
1658 pn533_send_ack(dev, GFP_KERNEL);
1659
1660 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1661 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001662
1663 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001664}
1665
1666static int pn533_activate_target_nfcdep(struct pn533 *dev)
1667{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001668 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001669 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001670 int rc;
1671
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001672 struct sk_buff *skb;
1673 struct sk_buff *resp;
1674
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001675 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1676
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001677 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1678 if (!skb)
1679 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001680
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001681 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1682 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001683
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001684 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1685 if (IS_ERR(resp))
1686 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001687
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001688 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001689 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001690 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001691 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001692 return -EIO;
1693
Samuel Ortiz541d9202011-12-14 16:43:10 +01001694 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001695 gt_len = resp->len - 16;
1696 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001697
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001698 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001699 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001700}
1701
Eric Lapuyade90099432012-05-07 12:31:13 +02001702static int pn533_activate_target(struct nfc_dev *nfc_dev,
1703 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704{
1705 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1706 int rc;
1707
1708 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001709 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001710
1711 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001712 nfc_dev_err(&dev->interface->dev,
1713 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714 return -EBUSY;
1715 }
1716
1717 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001718 nfc_dev_err(&dev->interface->dev,
1719 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720 return -EBUSY;
1721 }
1722
1723 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001724 nfc_dev_err(&dev->interface->dev,
1725 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001726 return -EINVAL;
1727 }
1728
1729 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001730 nfc_dev_err(&dev->interface->dev,
1731 "Target doesn't support requested proto %u",
1732 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001733 return -EINVAL;
1734 }
1735
1736 if (protocol == NFC_PROTO_NFC_DEP) {
1737 rc = pn533_activate_target_nfcdep(dev);
1738 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001739 nfc_dev_err(&dev->interface->dev,
1740 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741 return rc;
1742 }
1743 }
1744
1745 dev->tgt_active_prot = protocol;
1746 dev->tgt_available_prots = 0;
1747
1748 return 0;
1749}
1750
Eric Lapuyade90099432012-05-07 12:31:13 +02001751static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1752 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001753{
1754 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001755
1756 struct sk_buff *skb;
1757 struct sk_buff *resp;
1758
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001759 int rc;
1760
1761 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1762
1763 if (!dev->tgt_active_prot) {
1764 nfc_dev_err(&dev->interface->dev, "There is no active target");
1765 return;
1766 }
1767
1768 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001769 skb_queue_purge(&dev->resp_q);
1770
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001771 skb = pn533_alloc_skb(sizeof(u8));
1772 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001774
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001775 *skb_put(skb, 1) = 1; /* TG*/
1776
1777 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1778 if (IS_ERR(resp))
1779 return;
1780
1781 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001782 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001783 nfc_dev_err(&dev->interface->dev,
1784 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001785
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001786 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001787 return;
1788}
1789
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001790
1791static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001792 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001793{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001794 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001795 u8 target_gt_len;
1796 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001797 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001798
1799 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001800
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001801 if (IS_ERR(resp))
1802 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001803
1804 if (dev->tgt_available_prots &&
1805 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1806 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001807 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001808 rc = -EINVAL;
1809 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001810 }
1811
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001812 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1813
1814 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001815 if (rc != PN533_CMD_RET_SUCCESS) {
1816 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001817 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001818 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001819 }
1820
1821 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001822 struct nfc_target nfc_target;
1823
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001824 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1825
1826 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001827 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001828 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001829 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1830 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001831 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001832
1833 dev->tgt_available_prots = 0;
1834 }
1835
1836 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1837
1838 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001842 if (rc == 0)
1843 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001844 dev->nfc_dev->targets[0].idx,
1845 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001846
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001847error:
1848 dev_kfree_skb(resp);
1849 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001850}
1851
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001852static int pn533_mod_to_baud(struct pn533 *dev)
1853{
1854 switch (dev->poll_mod_curr) {
1855 case PN533_POLL_MOD_106KBPS_A:
1856 return 0;
1857 case PN533_POLL_MOD_212KBPS_FELICA:
1858 return 1;
1859 case PN533_POLL_MOD_424KBPS_FELICA:
1860 return 2;
1861 default:
1862 return -EINVAL;
1863 }
1864}
1865
Samuel Ortizd7f33452012-05-29 21:45:21 +02001866#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001867static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001868 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869{
1870 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001871 struct sk_buff *skb;
1872 int rc, baud, skb_len;
1873 u8 *next, *arg;
1874
Samuel Ortizd7f33452012-05-29 21:45:21 +02001875 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876
1877 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1878
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879 if (dev->poll_mod_count) {
1880 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001881 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001882 return -EBUSY;
1883 }
1884
1885 if (dev->tgt_active_prot) {
1886 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001887 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001888 return -EBUSY;
1889 }
1890
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001891 baud = pn533_mod_to_baud(dev);
1892 if (baud < 0) {
1893 nfc_dev_err(&dev->interface->dev,
1894 "Invalid curr modulation %d", dev->poll_mod_curr);
1895 return baud;
1896 }
1897
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001898 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001899 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001900 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001901
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001902 skb = pn533_alloc_skb(skb_len);
1903 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001904 return -ENOMEM;
1905
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001906 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1907 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001908
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001909 next = skb_put(skb, 1); /* Next */
1910 *next = 0;
1911
1912 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1913 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1914 PASSIVE_DATA_LEN);
1915 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001916 }
1917
Samuel Ortiz47807d32012-03-05 01:03:50 +01001918 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001919 memcpy(skb_put(skb, gb_len), gb, gb_len);
1920 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001921 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001922 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001923 }
1924
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001925 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1926 if (!arg) {
1927 dev_kfree_skb(skb);
1928 return -ENOMEM;
1929 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001930
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001931 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001932
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001933 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1934 pn533_in_dep_link_up_complete, arg);
1935
1936 if (rc < 0) {
1937 dev_kfree_skb(skb);
1938 kfree(arg);
1939 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001940
1941 return rc;
1942}
1943
1944static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1945{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001946 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1947
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001948 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1949
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001950 pn533_poll_reset_mod_list(dev);
1951
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001952 if (dev->tgt_mode || dev->tgt_active_prot) {
1953 pn533_send_ack(dev, GFP_KERNEL);
1954 usb_kill_urb(dev->in_urb);
1955 }
1956
1957 dev->tgt_active_prot = 0;
1958 dev->tgt_mode = 0;
1959
1960 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001961
1962 return 0;
1963}
1964
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001965struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001966 data_exchange_cb_t cb;
1967 void *cb_context;
1968};
1969
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001970static struct sk_buff *pn533_build_response(struct pn533 *dev)
1971{
1972 struct sk_buff *skb, *tmp, *t;
1973 unsigned int skb_len = 0, tmp_len = 0;
1974
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001975 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001976
1977 if (skb_queue_empty(&dev->resp_q))
1978 return NULL;
1979
1980 if (skb_queue_len(&dev->resp_q) == 1) {
1981 skb = skb_dequeue(&dev->resp_q);
1982 goto out;
1983 }
1984
1985 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1986 skb_len += tmp->len;
1987
1988 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1989 __func__, skb_len);
1990
1991 skb = alloc_skb(skb_len, GFP_KERNEL);
1992 if (skb == NULL)
1993 goto out;
1994
1995 skb_put(skb, skb_len);
1996
1997 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1998 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1999 tmp_len += tmp->len;
2000 }
2001
2002out:
2003 skb_queue_purge(&dev->resp_q);
2004
2005 return skb;
2006}
2007
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002008static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002009 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002010{
2011 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002012 struct sk_buff *skb;
2013 int rc = 0;
2014 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002015
2016 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2017
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002018 if (IS_ERR(resp)) {
2019 rc = PTR_ERR(resp);
2020 goto _error;
2021 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002022
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002023 status = resp->data[0];
2024 ret = status & PN533_CMD_RET_MASK;
2025 mi = status & PN533_CMD_MI_MASK;
2026
2027 skb_pull(resp, sizeof(status));
2028
2029 if (ret != PN533_CMD_RET_SUCCESS) {
2030 nfc_dev_err(&dev->interface->dev,
2031 "PN533 reported error %d when exchanging data",
2032 ret);
2033 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002034 goto error;
2035 }
2036
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002037 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002038
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002039 if (mi) {
2040 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002041 queue_work(dev->wq, &dev->mi_work);
2042 return -EINPROGRESS;
2043 }
2044
2045 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002046 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002047 goto error;
2048
2049 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002050 kfree(arg);
2051 return 0;
2052
2053error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002054 dev_kfree_skb(resp);
2055_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002056 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002057 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002058 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002059 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002060}
2061
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002062static int pn533_transceive(struct nfc_dev *nfc_dev,
2063 struct nfc_target *target, struct sk_buff *skb,
2064 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002065{
2066 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002067 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002068 int rc;
2069
2070 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2071
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002072 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2073 /* TODO: Implement support to multi-part data exchange */
2074 nfc_dev_err(&dev->interface->dev,
2075 "Data length greater than the max allowed: %d",
2076 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2077 rc = -ENOSYS;
2078 goto error;
2079 }
2080
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002081 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002082 nfc_dev_err(&dev->interface->dev,
2083 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002084 rc = -EINVAL;
2085 goto error;
2086 }
2087
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002088 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002089 if (!arg) {
2090 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002091 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002092 }
2093
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002094 arg->cb = cb;
2095 arg->cb_context = cb_context;
2096
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002097 switch (dev->device_type) {
2098 case PN533_DEVICE_PASORI:
2099 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2100 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2101 skb,
2102 pn533_data_exchange_complete,
2103 arg);
2104
2105 break;
2106 }
2107 default:
2108 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2109
2110 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2111 skb, pn533_data_exchange_complete,
2112 arg);
2113
2114 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002115 }
2116
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002117 if (rc < 0) /* rc from send_async */
2118 goto error;
2119
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 return 0;
2121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002122error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002123 kfree(arg);
2124 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002125 return rc;
2126}
2127
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002128static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002129 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002130{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002131 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002132
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002133 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2134
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002135 if (IS_ERR(resp))
2136 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002137
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002138 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002139
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002140 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002141
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002142 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002143 nfc_tm_deactivated(dev->nfc_dev);
2144
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002145 dev->tgt_mode = 0;
2146
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002147 return 0;
2148 }
2149
2150 queue_work(dev->wq, &dev->tg_work);
2151
2152 return 0;
2153}
2154
2155static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2156{
2157 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002158 int rc;
2159
2160 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2161
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002162 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002163 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002164 "Data length greater than the max allowed: %d",
2165 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2166 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002167 }
2168
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002169 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2170 pn533_tm_send_complete, NULL);
2171 if (rc < 0)
2172 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173
2174 return rc;
2175}
2176
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002177static void pn533_wq_mi_recv(struct work_struct *work)
2178{
2179 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002180
2181 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002182 int rc;
2183
2184 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2185
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002186 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2187 if (!skb)
2188 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002189
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002190 switch (dev->device_type) {
2191 case PN533_DEVICE_PASORI:
2192 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2193 rc = pn533_send_cmd_direct_async(dev,
2194 PN533_CMD_IN_COMM_THRU,
2195 skb,
2196 pn533_data_exchange_complete,
2197 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002198
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002199 break;
2200 }
2201 default:
2202 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002203
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002204 rc = pn533_send_cmd_direct_async(dev,
2205 PN533_CMD_IN_DATA_EXCHANGE,
2206 skb,
2207 pn533_data_exchange_complete,
2208 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002209
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002210 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002211 }
2212
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002213 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002214 return;
2215
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002216 nfc_dev_err(&dev->interface->dev,
2217 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002218
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002219 dev_kfree_skb(skb);
2220 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002221
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002222error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002223 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002224 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002225}
2226
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002227static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2228 u8 cfgdata_len)
2229{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002230 struct sk_buff *skb;
2231 struct sk_buff *resp;
2232
2233 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002234
2235 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2236
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002237 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002238
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002239 skb = pn533_alloc_skb(skb_len);
2240 if (!skb)
2241 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002242
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002243 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2244 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002245
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002246 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2247 if (IS_ERR(resp))
2248 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002249
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002250 dev_kfree_skb(resp);
2251 return 0;
2252}
2253
2254static int pn533_get_firmware_version(struct pn533 *dev,
2255 struct pn533_fw_version *fv)
2256{
2257 struct sk_buff *skb;
2258 struct sk_buff *resp;
2259
2260 skb = pn533_alloc_skb(0);
2261 if (!skb)
2262 return -ENOMEM;
2263
2264 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2265 if (IS_ERR(resp))
2266 return PTR_ERR(resp);
2267
2268 fv->ic = resp->data[0];
2269 fv->ver = resp->data[1];
2270 fv->rev = resp->data[2];
2271 fv->support = resp->data[3];
2272
2273 dev_kfree_skb(resp);
2274 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002275}
2276
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002277static int pn533_fw_reset(struct pn533 *dev)
2278{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002279 struct sk_buff *skb;
2280 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002281
2282 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2283
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002284 skb = pn533_alloc_skb(sizeof(u8));
2285 if (!skb)
2286 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002287
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002288 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002289
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002290 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2291 if (IS_ERR(resp))
2292 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002293
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002294 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002295
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002296 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002297}
2298
2299static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002300 .dev_up = NULL,
2301 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002302 .dep_link_up = pn533_dep_link_up,
2303 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002304 .start_poll = pn533_start_poll,
2305 .stop_poll = pn533_stop_poll,
2306 .activate_target = pn533_activate_target,
2307 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002308 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002309 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002310};
2311
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002312static int pn533_setup(struct pn533 *dev)
2313{
2314 struct pn533_config_max_retries max_retries;
2315 struct pn533_config_timing timing;
2316 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2317 int rc;
2318
2319 switch (dev->device_type) {
2320 case PN533_DEVICE_STD:
2321 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2322 max_retries.mx_rty_psl = 2;
2323 max_retries.mx_rty_passive_act =
2324 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2325
2326 timing.rfu = PN533_CONFIG_TIMING_102;
2327 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2328 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2329
2330 break;
2331
2332 case PN533_DEVICE_PASORI:
2333 max_retries.mx_rty_atr = 0x2;
2334 max_retries.mx_rty_psl = 0x1;
2335 max_retries.mx_rty_passive_act =
2336 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2337
2338 timing.rfu = PN533_CONFIG_TIMING_102;
2339 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2340 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2341
2342 break;
2343
2344 default:
2345 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2346 dev->device_type);
2347 return -EINVAL;
2348 }
2349
2350 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2351 (u8 *)&max_retries, sizeof(max_retries));
2352 if (rc) {
2353 nfc_dev_err(&dev->interface->dev,
2354 "Error on setting MAX_RETRIES config");
2355 return rc;
2356 }
2357
2358
2359 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2360 (u8 *)&timing, sizeof(timing));
2361 if (rc) {
2362 nfc_dev_err(&dev->interface->dev,
2363 "Error on setting RF timings");
2364 return rc;
2365 }
2366
2367 switch (dev->device_type) {
2368 case PN533_DEVICE_STD:
2369 break;
2370
2371 case PN533_DEVICE_PASORI:
2372 pn533_fw_reset(dev);
2373
2374 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2375 pasori_cfg, 3);
2376 if (rc) {
2377 nfc_dev_err(&dev->interface->dev,
2378 "Error while settings PASORI config");
2379 return rc;
2380 }
2381
2382 pn533_fw_reset(dev);
2383
2384 break;
2385 }
2386
2387 return 0;
2388}
2389
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002390static int pn533_probe(struct usb_interface *interface,
2391 const struct usb_device_id *id)
2392{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002393 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002394 struct pn533 *dev;
2395 struct usb_host_interface *iface_desc;
2396 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002397 int in_endpoint = 0;
2398 int out_endpoint = 0;
2399 int rc = -ENOMEM;
2400 int i;
2401 u32 protocols;
2402
2403 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2404 if (!dev)
2405 return -ENOMEM;
2406
2407 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2408 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002409 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002410
2411 iface_desc = interface->cur_altsetting;
2412 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2413 endpoint = &iface_desc->endpoint[i].desc;
2414
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002415 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002416 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002417
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002418 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002419 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002420 }
2421
2422 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002423 nfc_dev_err(&interface->dev,
2424 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002425 rc = -ENODEV;
2426 goto error;
2427 }
2428
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002429 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002430 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002431 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2432
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +01002433 if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002434 goto error;
2435
2436 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002437 usb_rcvbulkpipe(dev->udev, in_endpoint),
2438 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002439 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002440 usb_sndbulkpipe(dev->udev, out_endpoint),
2441 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002442
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002443 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2444 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002445 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002446 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002447 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002448 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002449 if (dev->wq == NULL)
2450 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002451
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002452 init_timer(&dev->listen_timer);
2453 dev->listen_timer.data = (unsigned long) dev;
2454 dev->listen_timer.function = pn533_listen_mode_timer;
2455
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002456 skb_queue_head_init(&dev->resp_q);
2457
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002458 INIT_LIST_HEAD(&dev->cmd_queue);
2459
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002460 usb_set_intfdata(interface, dev);
2461
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002462 memset(&fw_ver, 0, sizeof(fw_ver));
2463 rc = pn533_get_firmware_version(dev, &fw_ver);
2464 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002465 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002467 nfc_dev_info(&dev->interface->dev,
2468 "NXP PN533 firmware ver %d.%d now attached",
2469 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002470
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002471 dev->device_type = id->driver_info;
2472 switch (dev->device_type) {
2473 case PN533_DEVICE_STD:
2474 protocols = PN533_ALL_PROTOCOLS;
2475 break;
2476
2477 case PN533_DEVICE_PASORI:
2478 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2479 break;
2480
2481 default:
2482 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2483 dev->device_type);
2484 rc = -EINVAL;
2485 goto destroy_wq;
2486 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487
Samuel Ortize8753042011-08-19 15:47:11 +02002488 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002489 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002490 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002491 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002492 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002493 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002494
2495 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2496 nfc_set_drvdata(dev->nfc_dev, dev);
2497
2498 rc = nfc_register_device(dev->nfc_dev);
2499 if (rc)
2500 goto free_nfc_dev;
2501
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002502 rc = pn533_setup(dev);
2503 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002504 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002505
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002506 return 0;
2507
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002508unregister_nfc_dev:
2509 nfc_unregister_device(dev->nfc_dev);
2510
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002511free_nfc_dev:
2512 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002513
Samuel Ortiz4849f852012-04-10 19:43:17 +02002514destroy_wq:
2515 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002516error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002517 usb_free_urb(dev->in_urb);
2518 kfree(dev->out_frame);
2519 usb_free_urb(dev->out_urb);
2520 kfree(dev);
2521 return rc;
2522}
2523
2524static void pn533_disconnect(struct usb_interface *interface)
2525{
2526 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002527 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002528
2529 dev = usb_get_intfdata(interface);
2530 usb_set_intfdata(interface, NULL);
2531
2532 nfc_unregister_device(dev->nfc_dev);
2533 nfc_free_device(dev->nfc_dev);
2534
2535 usb_kill_urb(dev->in_urb);
2536 usb_kill_urb(dev->out_urb);
2537
Samuel Ortiz4849f852012-04-10 19:43:17 +02002538 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002539
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002540 skb_queue_purge(&dev->resp_q);
2541
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002542 del_timer(&dev->listen_timer);
2543
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002544 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2545 list_del(&cmd->queue);
2546 kfree(cmd);
2547 }
2548
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549 usb_free_urb(dev->in_urb);
2550 kfree(dev->out_frame);
2551 usb_free_urb(dev->out_urb);
2552 kfree(dev);
2553
Dan Carpenter276556d2011-07-08 10:21:15 +03002554 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555}
2556
2557static struct usb_driver pn533_driver = {
2558 .name = "pn533",
2559 .probe = pn533_probe,
2560 .disconnect = pn533_disconnect,
2561 .id_table = pn533_table,
2562};
2563
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002564module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002565
2566MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2567 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2568MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2569MODULE_VERSION(VERSION);
2570MODULE_LICENSE("GPL");