blob: 5bc0c7cfea9a9b695148674fdfa01fe0a6c48e1f [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
609static int __pn533_send_cmd_frame_async(struct pn533 *dev,
610 struct pn533_frame *out_frame,
611 struct pn533_frame *in_frame,
612 int in_frame_len,
613 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
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300618 dev->cmd = PN533_FRAME_CMD(out_frame);
619 dev->cmd_complete = cmd_complete;
620 dev->cmd_complete_arg = arg;
621
622 dev->out_urb->transfer_buffer = out_frame;
623 dev->out_urb->transfer_buffer_length =
624 PN533_FRAME_SIZE(out_frame);
625
626 dev->in_urb->transfer_buffer = in_frame;
627 dev->in_urb->transfer_buffer_length = in_frame_len;
628
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100629 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
630 out_frame, PN533_FRAME_SIZE(out_frame), false);
631
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100632 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300633 if (rc)
634 return rc;
635
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100636 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300637 if (rc)
638 goto error;
639
640 return 0;
641
642error:
643 usb_unlink_urb(dev->out_urb);
644 return rc;
645}
646
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100647static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
648{
649 struct pn533_frame *frame;
650 /* payload is already there, just update datalen */
651 int payload_len = skb->len;
652
653 skb_push(skb, PN533_FRAME_HEADER_LEN);
654 skb_put(skb, PN533_FRAME_TAIL_LEN);
655
656 frame = (struct pn533_frame *)skb->data;
657
658 pn533_tx_frame_init(frame, cmd_code);
659 frame->datalen += payload_len;
660 pn533_tx_frame_finish(frame);
661}
662
663struct pn533_send_async_complete_arg {
664 pn533_send_async_complete_t complete_cb;
665 void *complete_cb_context;
666 struct sk_buff *resp;
667 struct sk_buff *req;
668};
669
670static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
671 int params_len)
672{
673 struct pn533_send_async_complete_arg *arg = _arg;
674
675 struct sk_buff *req = arg->req;
676 struct sk_buff *resp = arg->resp;
677
678 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
679 int rc;
680
681 dev_kfree_skb(req);
682
683 if (params_len < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684 arg->complete_cb(dev, arg->complete_cb_context,
685 ERR_PTR(params_len));
686 rc = params_len;
687 dev_kfree_skb(resp);
688 goto out;
689 }
690
691 skb_put(resp, PN533_FRAME_SIZE(frame));
692 skb_pull(resp, PN533_FRAME_HEADER_LEN);
693 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
694
695 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
696
697out:
698 kfree(arg);
699 return rc;
700}
701
702static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
703 struct sk_buff *req, struct sk_buff *resp,
704 int resp_len,
705 pn533_send_async_complete_t complete_cb,
706 void *complete_cb_context)
707{
708 struct pn533_cmd *cmd;
709 struct pn533_send_async_complete_arg *arg;
710 int rc = 0;
711
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100712 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100713
714 arg = kzalloc(sizeof(arg), GFP_KERNEL);
715 if (!arg)
716 return -ENOMEM;
717
718 arg->complete_cb = complete_cb;
719 arg->complete_cb_context = complete_cb_context;
720 arg->resp = resp;
721 arg->req = req;
722
723 pn533_build_cmd_frame(cmd_code, req);
724
725 mutex_lock(&dev->cmd_lock);
726
727 if (!dev->cmd_pending) {
728 rc = __pn533_send_cmd_frame_async(dev,
729 (struct pn533_frame *)req->data,
730 (struct pn533_frame *)resp->data,
731 resp_len, pn533_send_async_complete,
732 arg);
733 if (rc)
734 goto error;
735
736 dev->cmd_pending = 1;
737 goto unlock;
738 }
739
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100740 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
741 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100742
743 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
744 if (!cmd) {
745 rc = -ENOMEM;
746 goto error;
747 }
748
749 INIT_LIST_HEAD(&cmd->queue);
750 cmd->cmd_code = cmd_code;
751 cmd->req = req;
752 cmd->resp = resp;
753 cmd->arg = arg;
754
755 list_add_tail(&cmd->queue, &dev->cmd_queue);
756
757 goto unlock;
758
759error:
760 kfree(arg);
761unlock:
762 mutex_unlock(&dev->cmd_lock);
763 return rc;
764}
765
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100766static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
767 struct sk_buff *req,
768 pn533_send_async_complete_t complete_cb,
769 void *complete_cb_context)
770{
771 struct sk_buff *resp;
772 int rc;
773 int resp_len = PN533_FRAME_HEADER_LEN +
774 PN533_FRAME_MAX_PAYLOAD_LEN +
775 PN533_FRAME_TAIL_LEN;
776
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100777 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
778 if (!resp)
779 return -ENOMEM;
780
781 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
782 complete_cb_context);
783 if (rc)
784 dev_kfree_skb(resp);
785
786 return rc;
787}
788
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100789static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
790 struct sk_buff *req,
791 pn533_send_async_complete_t complete_cb,
792 void *complete_cb_context)
793{
794 struct sk_buff *resp;
795 int rc;
796
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100797 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
798 if (!resp)
799 return -ENOMEM;
800
801 rc = __pn533_send_async(dev, cmd_code, req, resp,
802 PN533_NORMAL_FRAME_MAX_LEN,
803 complete_cb, complete_cb_context);
804 if (rc)
805 dev_kfree_skb(resp);
806
807 return rc;
808}
809
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100810/*
811 * pn533_send_cmd_direct_async
812 *
813 * The function sends a piority cmd directly to the chip omiting the cmd
814 * queue. It's intended to be used by chaining mechanism of received responses
815 * where the host has to request every single chunk of data before scheduling
816 * next cmd from the queue.
817 */
818static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
819 struct sk_buff *req,
820 pn533_send_async_complete_t complete_cb,
821 void *complete_cb_context)
822{
823 struct pn533_send_async_complete_arg *arg;
824 struct sk_buff *resp;
825 int rc;
826 int resp_len = PN533_FRAME_HEADER_LEN +
827 PN533_FRAME_MAX_PAYLOAD_LEN +
828 PN533_FRAME_TAIL_LEN;
829
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100830 resp = alloc_skb(resp_len, GFP_KERNEL);
831 if (!resp)
832 return -ENOMEM;
833
834 arg = kzalloc(sizeof(arg), GFP_KERNEL);
835 if (!arg) {
836 dev_kfree_skb(resp);
837 return -ENOMEM;
838 }
839
840 arg->complete_cb = complete_cb;
841 arg->complete_cb_context = complete_cb_context;
842 arg->resp = resp;
843 arg->req = req;
844
845 pn533_build_cmd_frame(cmd_code, req);
846
847 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
848 (struct pn533_frame *)resp->data,
849 resp_len, pn533_send_async_complete,
850 arg);
851 if (rc < 0) {
852 dev_kfree_skb(resp);
853 kfree(arg);
854 }
855
856 return rc;
857}
858
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200859static void pn533_wq_cmd(struct work_struct *work)
860{
861 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
862 struct pn533_cmd *cmd;
863
864 mutex_lock(&dev->cmd_lock);
865
866 if (list_empty(&dev->cmd_queue)) {
867 dev->cmd_pending = 0;
868 mutex_unlock(&dev->cmd_lock);
869 return;
870 }
871
872 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
873
Szymon Janc60ad07a2012-10-25 17:29:45 +0200874 list_del(&cmd->queue);
875
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200876 mutex_unlock(&dev->cmd_lock);
877
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +0100878 __pn533_send_cmd_frame_async(dev,
879 (struct pn533_frame *)cmd->req->data,
880 (struct pn533_frame *)cmd->resp->data,
881 PN533_NORMAL_FRAME_MAX_LEN,
882 pn533_send_async_complete,
883 cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200884
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200885 kfree(cmd);
886}
887
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300888struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100889 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300890 struct completion done;
891};
892
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100893static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
894 struct sk_buff *resp)
895{
896 struct pn533_sync_cmd_response *arg = _arg;
897
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100898 arg->resp = resp;
899 complete(&arg->done);
900
901 return 0;
902}
903
904/* pn533_send_cmd_sync
905 *
906 * Please note the req parameter is freed inside the function to
907 * limit a number of return value interpretations by the caller.
908 *
909 * 1. negative in case of error during TX path -> req should be freed
910 *
911 * 2. negative in case of error during RX path -> req should not be freed
912 * as it's been already freed at the begining of RX path by
913 * async_complete_cb.
914 *
915 * 3. valid pointer in case of succesfult RX path
916 *
917 * A caller has to check a return value with IS_ERR macro. If the test pass,
918 * the returned pointer is valid.
919 *
920 * */
921static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
922 struct sk_buff *req)
923{
924 int rc;
925 struct pn533_sync_cmd_response arg;
926
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100927 init_completion(&arg.done);
928
929 rc = pn533_send_cmd_async(dev, cmd_code, req,
930 pn533_send_sync_complete, &arg);
931 if (rc) {
932 dev_kfree_skb(req);
933 return ERR_PTR(rc);
934 }
935
936 wait_for_completion(&arg.done);
937
938 return arg.resp;
939}
940
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300941static void pn533_send_complete(struct urb *urb)
942{
943 struct pn533 *dev = urb->context;
944
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300945 switch (urb->status) {
946 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100947 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300948 case -ECONNRESET:
949 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100950 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100951 "The urb has been stopped (status %d)",
952 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300953 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100954 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300955 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100956 nfc_dev_err(&dev->interface->dev,
957 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300958 }
959}
960
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100961static struct sk_buff *pn533_alloc_skb(unsigned int size)
962{
963 struct sk_buff *skb;
964
965 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
966 size +
967 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
968
969 if (skb)
970 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
971
972 return skb;
973}
974
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300975struct pn533_target_type_a {
976 __be16 sens_res;
977 u8 sel_res;
978 u8 nfcid_len;
979 u8 nfcid_data[];
980} __packed;
981
982
983#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
984#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
985#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
986
987#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
988#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
989
990#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
991#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
992
993#define PN533_TYPE_A_SEL_PROT_MIFARE 0
994#define PN533_TYPE_A_SEL_PROT_ISO14443 1
995#define PN533_TYPE_A_SEL_PROT_DEP 2
996#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
997
998static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
999 int target_data_len)
1000{
1001 u8 ssd;
1002 u8 platconf;
1003
1004 if (target_data_len < sizeof(struct pn533_target_type_a))
1005 return false;
1006
1007 /* The lenght check of nfcid[] and ats[] are not being performed because
1008 the values are not being used */
1009
1010 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1011 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1012 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1013
1014 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001015 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1016 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1017 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001018 return false;
1019
1020 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1021 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1022 return false;
1023
1024 return true;
1025}
1026
1027static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1028 int tgt_data_len)
1029{
1030 struct pn533_target_type_a *tgt_type_a;
1031
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001032 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001033
1034 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1035 return -EPROTO;
1036
1037 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1038 case PN533_TYPE_A_SEL_PROT_MIFARE:
1039 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1040 break;
1041 case PN533_TYPE_A_SEL_PROT_ISO14443:
1042 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1043 break;
1044 case PN533_TYPE_A_SEL_PROT_DEP:
1045 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1046 break;
1047 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1048 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1049 NFC_PROTO_NFC_DEP_MASK;
1050 break;
1051 }
1052
1053 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1054 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001055 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1056 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001057
1058 return 0;
1059}
1060
1061struct pn533_target_felica {
1062 u8 pol_res;
1063 u8 opcode;
1064 u8 nfcid2[8];
1065 u8 pad[8];
1066 /* optional */
1067 u8 syst_code[];
1068} __packed;
1069
1070#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1071#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1072
1073static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1074 int target_data_len)
1075{
1076 if (target_data_len < sizeof(struct pn533_target_felica))
1077 return false;
1078
1079 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1080 return false;
1081
1082 return true;
1083}
1084
1085static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1086 int tgt_data_len)
1087{
1088 struct pn533_target_felica *tgt_felica;
1089
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001090 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001091
1092 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1093 return -EPROTO;
1094
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001095 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1096 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001097 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1098 else
1099 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1100
Samuel Ortiz79757542012-03-05 01:03:45 +01001101 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1102 nfc_tgt->sensf_res_len = 9;
1103
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001104 return 0;
1105}
1106
1107struct pn533_target_jewel {
1108 __be16 sens_res;
1109 u8 jewelid[4];
1110} __packed;
1111
1112static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1113 int target_data_len)
1114{
1115 u8 ssd;
1116 u8 platconf;
1117
1118 if (target_data_len < sizeof(struct pn533_target_jewel))
1119 return false;
1120
1121 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1122 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1123 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1124
1125 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001126 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1127 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1128 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001129 return false;
1130
1131 return true;
1132}
1133
1134static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1135 int tgt_data_len)
1136{
1137 struct pn533_target_jewel *tgt_jewel;
1138
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001139 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001140
1141 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1142 return -EPROTO;
1143
1144 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1145 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001146 nfc_tgt->nfcid1_len = 4;
1147 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001148
1149 return 0;
1150}
1151
1152struct pn533_type_b_prot_info {
1153 u8 bitrate;
1154 u8 fsci_type;
1155 u8 fwi_adc_fo;
1156} __packed;
1157
1158#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1159#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1160#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1161
1162struct pn533_type_b_sens_res {
1163 u8 opcode;
1164 u8 nfcid[4];
1165 u8 appdata[4];
1166 struct pn533_type_b_prot_info prot_info;
1167} __packed;
1168
1169#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1170
1171struct pn533_target_type_b {
1172 struct pn533_type_b_sens_res sensb_res;
1173 u8 attrib_res_len;
1174 u8 attrib_res[];
1175} __packed;
1176
1177static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1178 int target_data_len)
1179{
1180 if (target_data_len < sizeof(struct pn533_target_type_b))
1181 return false;
1182
1183 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1184 return false;
1185
1186 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1187 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1188 return false;
1189
1190 return true;
1191}
1192
1193static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1194 int tgt_data_len)
1195{
1196 struct pn533_target_type_b *tgt_type_b;
1197
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001198 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001199
1200 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1201 return -EPROTO;
1202
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001203 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001204
1205 return 0;
1206}
1207
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001208static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1209 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001210{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001211 struct nfc_target nfc_tgt;
1212 int rc;
1213
1214 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001215 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001216
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001217 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001218 return -EPROTO;
1219
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001220 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1221
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001222 switch (dev->poll_mod_curr) {
1223 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001224 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001225 break;
1226 case PN533_POLL_MOD_212KBPS_FELICA:
1227 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001228 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229 break;
1230 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001231 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001232 break;
1233 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001234 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001235 break;
1236 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001237 nfc_dev_err(&dev->interface->dev,
1238 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001239 return -EPROTO;
1240 }
1241
1242 if (rc)
1243 return rc;
1244
1245 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001246 nfc_dev_dbg(&dev->interface->dev,
1247 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001248 return -EAGAIN;
1249 }
1250
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001251 nfc_dev_dbg(&dev->interface->dev,
1252 "Target found - supported protocols: 0x%x",
1253 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001254
1255 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1256
1257 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1258
1259 return 0;
1260}
1261
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001262static inline void pn533_poll_next_mod(struct pn533 *dev)
1263{
1264 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1265}
1266
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001267static void pn533_poll_reset_mod_list(struct pn533 *dev)
1268{
1269 dev->poll_mod_count = 0;
1270}
1271
1272static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1273{
1274 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001275 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001276 dev->poll_mod_count++;
1277}
1278
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001279static void pn533_poll_create_mod_list(struct pn533 *dev,
1280 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001281{
1282 pn533_poll_reset_mod_list(dev);
1283
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001284 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1285 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1286 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001287 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1288
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001289 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1290 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001291 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1292 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1293 }
1294
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001295 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001296 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1297
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001298 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001300
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001301 if (tm_protocols)
1302 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001303}
1304
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001305static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001307 u8 nbtg, tg, *tgdata;
1308 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309
1310 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1311
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001312 nbtg = resp->data[0];
1313 tg = resp->data[1];
1314 tgdata = &resp->data[2];
1315 tgdata_len = resp->len - 2; /* nbtg + tg */
1316
1317 if (nbtg) {
1318 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001319
1320 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001321 if (rc == 0) {
1322 pn533_poll_reset_mod_list(dev);
1323 return 0;
1324 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325 }
1326
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001327 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328}
1329
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001330static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001331{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001332 struct sk_buff *skb;
1333 u8 *felica, *nfcid3, *gb;
1334
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001335 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1336 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1337 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1338 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001339
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001340 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1341 0x0, 0x0, 0x0,
1342 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001343
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001344 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1345 felica (18), nfcid3 (10), gb_len (1) */
1346 gbytes_len +
1347 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001348
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001349 skb = pn533_alloc_skb(skb_len);
1350 if (!skb)
1351 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001352
1353 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001354 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001355
1356 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001357 memcpy(skb_put(skb, 6), mifare_params, 6);
1358
1359 /* Felica params */
1360 felica = skb_put(skb, 18);
1361 memcpy(felica, felica_params, 18);
1362 get_random_bytes(felica + 2, 6);
1363
1364 /* NFCID3 */
1365 nfcid3 = skb_put(skb, 10);
1366 memset(nfcid3, 0, 10);
1367 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001368
1369 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001370 *skb_put(skb, 1) = gbytes_len;
1371
1372 gb = skb_put(skb, gbytes_len);
1373 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001374
Samuel Ortizad3823c2012-05-30 23:54:55 +02001375 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001376 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001377
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001378 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001379}
1380
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001381#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001382#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1383static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001384 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001385{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001386 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001387
1388 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1389
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001390 if (IS_ERR(resp))
1391 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001392
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001393 status = resp->data[0];
1394 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001395
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001396 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001397 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001398 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001399 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001400 return 0;
1401 }
1402
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001403 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001404}
1405
1406static void pn533_wq_tg_get_data(struct work_struct *work)
1407{
1408 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409
1410 struct sk_buff *skb;
1411 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001412
1413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1414
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001415 skb = pn533_alloc_skb(0);
1416 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001417 return;
1418
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001419 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1420 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001421
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001422 if (rc < 0)
1423 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001424
1425 return;
1426}
1427
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001428#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001429static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001430{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001431 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001432 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001433 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001434
1435 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1436
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001437 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001438 return -EINVAL;
1439
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001440 mode = resp->data[0];
1441 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001442
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001443 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1444 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001445
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001446 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1447 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001448 comm_mode = NFC_COMM_ACTIVE;
1449
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001451 return -EOPNOTSUPP;
1452
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001453 gb = cmd + ATR_REQ_GB_OFFSET;
1454 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001455
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001456 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1457 comm_mode, gb, gb_len);
1458 if (rc < 0) {
1459 nfc_dev_err(&dev->interface->dev,
1460 "Error when signaling target activation");
1461 return rc;
1462 }
1463
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001464 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001465 queue_work(dev->wq, &dev->tg_work);
1466
1467 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001468}
1469
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001470static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001471{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001472 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001473
1474 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1475
1476 /* An ack will cancel the last issued command (poll) */
1477 pn533_send_ack(dev, GFP_ATOMIC);
1478
1479 dev->cancel_listen = 1;
1480
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001481 pn533_poll_next_mod(dev);
1482
1483 queue_work(dev->wq, &dev->poll_work);
1484}
1485
1486static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001487 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001488{
1489 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001490 int rc;
1491
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001492 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1493
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001494 if (IS_ERR(resp)) {
1495 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001496
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001497 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1498 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001499
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001500 if (rc == -ENOENT) {
1501 if (dev->poll_mod_count != 0)
1502 return rc;
1503 else
1504 goto stop_poll;
1505 } else if (rc < 0) {
1506 nfc_dev_err(&dev->interface->dev,
1507 "Error %d when running poll", rc);
1508 goto stop_poll;
1509 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510 }
1511
1512 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1513
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001514 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001515 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001516 rc = pn533_init_target_complete(dev, resp);
1517 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518 }
1519
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001520 /* Initiator mode */
1521 rc = pn533_start_poll_complete(dev, resp);
1522 if (!rc)
1523 goto done;
1524
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001525 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001526 queue_work(dev->wq, &dev->poll_work);
1527
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001528done:
1529 dev_kfree_skb(resp);
1530 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001531
1532stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001533 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1534
Samuel Ortizad3823c2012-05-30 23:54:55 +02001535 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001536 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001537 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001538}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001539
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001540static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1541 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001542{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001543 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001544
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545 skb = pn533_alloc_skb(mod->len);
1546 if (!skb)
1547 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001548
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001549 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001550
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552}
1553
1554static int pn533_send_poll_frame(struct pn533 *dev)
1555{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001556 struct pn533_poll_modulations *mod;
1557 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001558 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001559 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001561 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001563 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1564 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001565
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001566 if (mod->len == 0) { /* Listen mode */
1567 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1568 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1569 } else { /* Polling mode */
1570 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1571 skb = pn533_alloc_poll_in_frame(mod);
1572 }
1573
1574 if (!skb) {
1575 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1576 return -ENOMEM;
1577 }
1578
1579 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1580 NULL);
1581 if (rc < 0) {
1582 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001583 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001584 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001585
1586 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001587}
1588
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001589static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001590{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001591 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1592 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001593 int rc;
1594
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001595 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1596
1597 nfc_dev_dbg(&dev->interface->dev,
1598 "%s cancel_listen %d modulation len %d",
1599 __func__, dev->cancel_listen, cur_mod->len);
1600
1601 if (dev->cancel_listen == 1) {
1602 dev->cancel_listen = 0;
1603 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001604 }
1605
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001606 rc = pn533_send_poll_frame(dev);
1607 if (rc)
1608 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001609
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1611 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001612
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001613 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001614}
1615
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001616static int pn533_start_poll(struct nfc_dev *nfc_dev,
1617 u32 im_protocols, u32 tm_protocols)
1618{
1619 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1620
1621 nfc_dev_dbg(&dev->interface->dev,
1622 "%s: im protocols 0x%x tm protocols 0x%x",
1623 __func__, im_protocols, tm_protocols);
1624
1625 if (dev->tgt_active_prot) {
1626 nfc_dev_err(&dev->interface->dev,
1627 "Cannot poll with a target already activated");
1628 return -EBUSY;
1629 }
1630
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001631 if (dev->tgt_mode) {
1632 nfc_dev_err(&dev->interface->dev,
1633 "Cannot poll while already being activated");
1634 return -EBUSY;
1635 }
1636
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001637 if (tm_protocols) {
1638 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1639 if (dev->gb == NULL)
1640 tm_protocols = 0;
1641 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001642
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643 dev->poll_mod_curr = 0;
1644 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1645 dev->poll_protocols = im_protocols;
1646 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001647
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001648 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001649}
1650
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001651static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1652{
1653 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1654
1655 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1656
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001657 del_timer(&dev->listen_timer);
1658
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001659 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001660 nfc_dev_dbg(&dev->interface->dev,
1661 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001662 return;
1663 }
1664
1665 /* An ack will cancel the last issued command (poll) */
1666 pn533_send_ack(dev, GFP_KERNEL);
1667
1668 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1669 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001670
1671 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001672}
1673
1674static int pn533_activate_target_nfcdep(struct pn533 *dev)
1675{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001676 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001677 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678 int rc;
1679
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001680 struct sk_buff *skb;
1681 struct sk_buff *resp;
1682
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001683 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1684
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001685 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1686 if (!skb)
1687 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001689 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1690 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001691
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001692 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1693 if (IS_ERR(resp))
1694 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001695
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001696 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001697 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001699 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001700 return -EIO;
1701
Samuel Ortiz541d9202011-12-14 16:43:10 +01001702 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001703 gt_len = resp->len - 16;
1704 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001705
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001706 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001707 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001708}
1709
Eric Lapuyade90099432012-05-07 12:31:13 +02001710static int pn533_activate_target(struct nfc_dev *nfc_dev,
1711 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001712{
1713 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1714 int rc;
1715
1716 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001717 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718
1719 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001720 nfc_dev_err(&dev->interface->dev,
1721 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001722 return -EBUSY;
1723 }
1724
1725 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001726 nfc_dev_err(&dev->interface->dev,
1727 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728 return -EBUSY;
1729 }
1730
1731 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001732 nfc_dev_err(&dev->interface->dev,
1733 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734 return -EINVAL;
1735 }
1736
1737 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001738 nfc_dev_err(&dev->interface->dev,
1739 "Target doesn't support requested proto %u",
1740 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741 return -EINVAL;
1742 }
1743
1744 if (protocol == NFC_PROTO_NFC_DEP) {
1745 rc = pn533_activate_target_nfcdep(dev);
1746 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001747 nfc_dev_err(&dev->interface->dev,
1748 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001749 return rc;
1750 }
1751 }
1752
1753 dev->tgt_active_prot = protocol;
1754 dev->tgt_available_prots = 0;
1755
1756 return 0;
1757}
1758
Eric Lapuyade90099432012-05-07 12:31:13 +02001759static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1760 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001761{
1762 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001763
1764 struct sk_buff *skb;
1765 struct sk_buff *resp;
1766
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 int rc;
1768
1769 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1770
1771 if (!dev->tgt_active_prot) {
1772 nfc_dev_err(&dev->interface->dev, "There is no active target");
1773 return;
1774 }
1775
1776 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001777 skb_queue_purge(&dev->resp_q);
1778
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001779 skb = pn533_alloc_skb(sizeof(u8));
1780 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001781 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001782
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001783 *skb_put(skb, 1) = 1; /* TG*/
1784
1785 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1786 if (IS_ERR(resp))
1787 return;
1788
1789 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001790 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001791 nfc_dev_err(&dev->interface->dev,
1792 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001793
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001794 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001795 return;
1796}
1797
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001798
1799static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001800 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001801{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001802 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001803 u8 target_gt_len;
1804 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001805 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001806
1807 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001808
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001809 if (IS_ERR(resp))
1810 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001811
1812 if (dev->tgt_available_prots &&
1813 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1814 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001815 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001816 rc = -EINVAL;
1817 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001818 }
1819
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001820 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1821
1822 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001823 if (rc != PN533_CMD_RET_SUCCESS) {
1824 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001825 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001826 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001827 }
1828
1829 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001830 struct nfc_target nfc_target;
1831
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001832 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1833
1834 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001835 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001836 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1838 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840
1841 dev->tgt_available_prots = 0;
1842 }
1843
1844 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1845
1846 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001847 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001848 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001849 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001850 if (rc == 0)
1851 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001852 dev->nfc_dev->targets[0].idx,
1853 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001854
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855error:
1856 dev_kfree_skb(resp);
1857 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001858}
1859
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001860static int pn533_mod_to_baud(struct pn533 *dev)
1861{
1862 switch (dev->poll_mod_curr) {
1863 case PN533_POLL_MOD_106KBPS_A:
1864 return 0;
1865 case PN533_POLL_MOD_212KBPS_FELICA:
1866 return 1;
1867 case PN533_POLL_MOD_424KBPS_FELICA:
1868 return 2;
1869 default:
1870 return -EINVAL;
1871 }
1872}
1873
Samuel Ortizd7f33452012-05-29 21:45:21 +02001874#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001875static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001876 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877{
1878 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001879 struct sk_buff *skb;
1880 int rc, baud, skb_len;
1881 u8 *next, *arg;
1882
Samuel Ortizd7f33452012-05-29 21:45:21 +02001883 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001884
1885 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1886
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001887 if (dev->poll_mod_count) {
1888 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001889 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001890 return -EBUSY;
1891 }
1892
1893 if (dev->tgt_active_prot) {
1894 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001895 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001896 return -EBUSY;
1897 }
1898
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001899 baud = pn533_mod_to_baud(dev);
1900 if (baud < 0) {
1901 nfc_dev_err(&dev->interface->dev,
1902 "Invalid curr modulation %d", dev->poll_mod_curr);
1903 return baud;
1904 }
1905
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001906 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001907 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001908 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001909
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001910 skb = pn533_alloc_skb(skb_len);
1911 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001912 return -ENOMEM;
1913
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001914 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1915 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001916
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001917 next = skb_put(skb, 1); /* Next */
1918 *next = 0;
1919
1920 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1921 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1922 PASSIVE_DATA_LEN);
1923 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001924 }
1925
Samuel Ortiz47807d32012-03-05 01:03:50 +01001926 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001927 memcpy(skb_put(skb, gb_len), gb, gb_len);
1928 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001929 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001930 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001931 }
1932
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001933 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1934 if (!arg) {
1935 dev_kfree_skb(skb);
1936 return -ENOMEM;
1937 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001938
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001939 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001940
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001941 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1942 pn533_in_dep_link_up_complete, arg);
1943
1944 if (rc < 0) {
1945 dev_kfree_skb(skb);
1946 kfree(arg);
1947 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001948
1949 return rc;
1950}
1951
1952static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1953{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001954 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1955
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001956 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1957
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001958 pn533_poll_reset_mod_list(dev);
1959
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001960 if (dev->tgt_mode || dev->tgt_active_prot) {
1961 pn533_send_ack(dev, GFP_KERNEL);
1962 usb_kill_urb(dev->in_urb);
1963 }
1964
1965 dev->tgt_active_prot = 0;
1966 dev->tgt_mode = 0;
1967
1968 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001969
1970 return 0;
1971}
1972
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001973struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001974 data_exchange_cb_t cb;
1975 void *cb_context;
1976};
1977
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001978static struct sk_buff *pn533_build_response(struct pn533 *dev)
1979{
1980 struct sk_buff *skb, *tmp, *t;
1981 unsigned int skb_len = 0, tmp_len = 0;
1982
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001983 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001984
1985 if (skb_queue_empty(&dev->resp_q))
1986 return NULL;
1987
1988 if (skb_queue_len(&dev->resp_q) == 1) {
1989 skb = skb_dequeue(&dev->resp_q);
1990 goto out;
1991 }
1992
1993 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1994 skb_len += tmp->len;
1995
1996 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1997 __func__, skb_len);
1998
1999 skb = alloc_skb(skb_len, GFP_KERNEL);
2000 if (skb == NULL)
2001 goto out;
2002
2003 skb_put(skb, skb_len);
2004
2005 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2006 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2007 tmp_len += tmp->len;
2008 }
2009
2010out:
2011 skb_queue_purge(&dev->resp_q);
2012
2013 return skb;
2014}
2015
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002016static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002017 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002018{
2019 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002020 struct sk_buff *skb;
2021 int rc = 0;
2022 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002023
2024 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2025
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002026 if (IS_ERR(resp)) {
2027 rc = PTR_ERR(resp);
2028 goto _error;
2029 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002030
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002031 status = resp->data[0];
2032 ret = status & PN533_CMD_RET_MASK;
2033 mi = status & PN533_CMD_MI_MASK;
2034
2035 skb_pull(resp, sizeof(status));
2036
2037 if (ret != PN533_CMD_RET_SUCCESS) {
2038 nfc_dev_err(&dev->interface->dev,
2039 "PN533 reported error %d when exchanging data",
2040 ret);
2041 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002042 goto error;
2043 }
2044
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002045 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002046
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002047 if (mi) {
2048 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002049 queue_work(dev->wq, &dev->mi_work);
2050 return -EINPROGRESS;
2051 }
2052
2053 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002054 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002055 goto error;
2056
2057 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002058 kfree(arg);
2059 return 0;
2060
2061error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002062 dev_kfree_skb(resp);
2063_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002064 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002065 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002066 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002067 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002068}
2069
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002070static int pn533_transceive(struct nfc_dev *nfc_dev,
2071 struct nfc_target *target, struct sk_buff *skb,
2072 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002073{
2074 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002075 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002076 int rc;
2077
2078 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2079
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002080 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2081 /* TODO: Implement support to multi-part data exchange */
2082 nfc_dev_err(&dev->interface->dev,
2083 "Data length greater than the max allowed: %d",
2084 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2085 rc = -ENOSYS;
2086 goto error;
2087 }
2088
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002089 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002090 nfc_dev_err(&dev->interface->dev,
2091 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002092 rc = -EINVAL;
2093 goto error;
2094 }
2095
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002096 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002097 if (!arg) {
2098 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002099 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002100 }
2101
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002102 arg->cb = cb;
2103 arg->cb_context = cb_context;
2104
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002105 switch (dev->device_type) {
2106 case PN533_DEVICE_PASORI:
2107 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2108 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2109 skb,
2110 pn533_data_exchange_complete,
2111 arg);
2112
2113 break;
2114 }
2115 default:
2116 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2117
2118 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2119 skb, pn533_data_exchange_complete,
2120 arg);
2121
2122 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123 }
2124
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002125 if (rc < 0) /* rc from send_async */
2126 goto error;
2127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002128 return 0;
2129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002131 kfree(arg);
2132 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002133 return rc;
2134}
2135
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002136static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002137 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002138{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002139 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002140
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002141 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2142
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002143 if (IS_ERR(resp))
2144 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002145
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002146 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002147
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002148 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002149
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002150 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002151 nfc_tm_deactivated(dev->nfc_dev);
2152
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002153 dev->tgt_mode = 0;
2154
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002155 return 0;
2156 }
2157
2158 queue_work(dev->wq, &dev->tg_work);
2159
2160 return 0;
2161}
2162
2163static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2164{
2165 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002166 int rc;
2167
2168 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2169
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002170 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002171 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002172 "Data length greater than the max allowed: %d",
2173 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2174 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175 }
2176
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002177 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2178 pn533_tm_send_complete, NULL);
2179 if (rc < 0)
2180 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002181
2182 return rc;
2183}
2184
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002185static void pn533_wq_mi_recv(struct work_struct *work)
2186{
2187 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002188
2189 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002190 int rc;
2191
2192 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2193
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002194 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2195 if (!skb)
2196 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002197
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002198 switch (dev->device_type) {
2199 case PN533_DEVICE_PASORI:
2200 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2201 rc = pn533_send_cmd_direct_async(dev,
2202 PN533_CMD_IN_COMM_THRU,
2203 skb,
2204 pn533_data_exchange_complete,
2205 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002206
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002207 break;
2208 }
2209 default:
2210 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002211
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002212 rc = pn533_send_cmd_direct_async(dev,
2213 PN533_CMD_IN_DATA_EXCHANGE,
2214 skb,
2215 pn533_data_exchange_complete,
2216 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002217
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002218 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002219 }
2220
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002221 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002222 return;
2223
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002224 nfc_dev_err(&dev->interface->dev,
2225 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002226
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002227 dev_kfree_skb(skb);
2228 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002229
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002230error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002231 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002232 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002233}
2234
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002235static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2236 u8 cfgdata_len)
2237{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002238 struct sk_buff *skb;
2239 struct sk_buff *resp;
2240
2241 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002242
2243 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2244
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002245 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002246
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002247 skb = pn533_alloc_skb(skb_len);
2248 if (!skb)
2249 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002250
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002251 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2252 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002253
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002254 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2255 if (IS_ERR(resp))
2256 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002257
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002258 dev_kfree_skb(resp);
2259 return 0;
2260}
2261
2262static int pn533_get_firmware_version(struct pn533 *dev,
2263 struct pn533_fw_version *fv)
2264{
2265 struct sk_buff *skb;
2266 struct sk_buff *resp;
2267
2268 skb = pn533_alloc_skb(0);
2269 if (!skb)
2270 return -ENOMEM;
2271
2272 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2273 if (IS_ERR(resp))
2274 return PTR_ERR(resp);
2275
2276 fv->ic = resp->data[0];
2277 fv->ver = resp->data[1];
2278 fv->rev = resp->data[2];
2279 fv->support = resp->data[3];
2280
2281 dev_kfree_skb(resp);
2282 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002283}
2284
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002285static int pn533_fw_reset(struct pn533 *dev)
2286{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002287 struct sk_buff *skb;
2288 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002289
2290 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2291
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002292 skb = pn533_alloc_skb(sizeof(u8));
2293 if (!skb)
2294 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002295
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002296 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002297
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002298 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2299 if (IS_ERR(resp))
2300 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002301
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002302 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002303
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002304 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002305}
2306
2307static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002308 .dev_up = NULL,
2309 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002310 .dep_link_up = pn533_dep_link_up,
2311 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002312 .start_poll = pn533_start_poll,
2313 .stop_poll = pn533_stop_poll,
2314 .activate_target = pn533_activate_target,
2315 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002316 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002317 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002318};
2319
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002320static int pn533_setup(struct pn533 *dev)
2321{
2322 struct pn533_config_max_retries max_retries;
2323 struct pn533_config_timing timing;
2324 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2325 int rc;
2326
2327 switch (dev->device_type) {
2328 case PN533_DEVICE_STD:
2329 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2330 max_retries.mx_rty_psl = 2;
2331 max_retries.mx_rty_passive_act =
2332 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2333
2334 timing.rfu = PN533_CONFIG_TIMING_102;
2335 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2336 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2337
2338 break;
2339
2340 case PN533_DEVICE_PASORI:
2341 max_retries.mx_rty_atr = 0x2;
2342 max_retries.mx_rty_psl = 0x1;
2343 max_retries.mx_rty_passive_act =
2344 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2345
2346 timing.rfu = PN533_CONFIG_TIMING_102;
2347 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2348 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2349
2350 break;
2351
2352 default:
2353 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2354 dev->device_type);
2355 return -EINVAL;
2356 }
2357
2358 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2359 (u8 *)&max_retries, sizeof(max_retries));
2360 if (rc) {
2361 nfc_dev_err(&dev->interface->dev,
2362 "Error on setting MAX_RETRIES config");
2363 return rc;
2364 }
2365
2366
2367 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2368 (u8 *)&timing, sizeof(timing));
2369 if (rc) {
2370 nfc_dev_err(&dev->interface->dev,
2371 "Error on setting RF timings");
2372 return rc;
2373 }
2374
2375 switch (dev->device_type) {
2376 case PN533_DEVICE_STD:
2377 break;
2378
2379 case PN533_DEVICE_PASORI:
2380 pn533_fw_reset(dev);
2381
2382 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2383 pasori_cfg, 3);
2384 if (rc) {
2385 nfc_dev_err(&dev->interface->dev,
2386 "Error while settings PASORI config");
2387 return rc;
2388 }
2389
2390 pn533_fw_reset(dev);
2391
2392 break;
2393 }
2394
2395 return 0;
2396}
2397
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002398static int pn533_probe(struct usb_interface *interface,
2399 const struct usb_device_id *id)
2400{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002401 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002402 struct pn533 *dev;
2403 struct usb_host_interface *iface_desc;
2404 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002405 int in_endpoint = 0;
2406 int out_endpoint = 0;
2407 int rc = -ENOMEM;
2408 int i;
2409 u32 protocols;
2410
2411 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2412 if (!dev)
2413 return -ENOMEM;
2414
2415 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2416 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002417 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418
2419 iface_desc = interface->cur_altsetting;
2420 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2421 endpoint = &iface_desc->endpoint[i].desc;
2422
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002423 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002424 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002425
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002426 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002427 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428 }
2429
2430 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002431 nfc_dev_err(&interface->dev,
2432 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002433 rc = -ENODEV;
2434 goto error;
2435 }
2436
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002437 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002438 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002439 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2440
Waldemar Rymarkiewicz89fb2022012-12-10 14:42:53 +01002441 if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002442 goto error;
2443
2444 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002445 usb_rcvbulkpipe(dev->udev, in_endpoint),
2446 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002447 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002448 usb_sndbulkpipe(dev->udev, out_endpoint),
2449 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002450
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002451 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2452 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002453 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002454 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002455 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002456 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002457 if (dev->wq == NULL)
2458 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002459
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002460 init_timer(&dev->listen_timer);
2461 dev->listen_timer.data = (unsigned long) dev;
2462 dev->listen_timer.function = pn533_listen_mode_timer;
2463
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002464 skb_queue_head_init(&dev->resp_q);
2465
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002466 INIT_LIST_HEAD(&dev->cmd_queue);
2467
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002468 usb_set_intfdata(interface, dev);
2469
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002470 memset(&fw_ver, 0, sizeof(fw_ver));
2471 rc = pn533_get_firmware_version(dev, &fw_ver);
2472 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002473 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002474
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002475 nfc_dev_info(&dev->interface->dev,
2476 "NXP PN533 firmware ver %d.%d now attached",
2477 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002478
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002479 dev->device_type = id->driver_info;
2480 switch (dev->device_type) {
2481 case PN533_DEVICE_STD:
2482 protocols = PN533_ALL_PROTOCOLS;
2483 break;
2484
2485 case PN533_DEVICE_PASORI:
2486 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2487 break;
2488
2489 default:
2490 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2491 dev->device_type);
2492 rc = -EINVAL;
2493 goto destroy_wq;
2494 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002495
Samuel Ortize8753042011-08-19 15:47:11 +02002496 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002497 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002498 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002499 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002500 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002501 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002502
2503 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2504 nfc_set_drvdata(dev->nfc_dev, dev);
2505
2506 rc = nfc_register_device(dev->nfc_dev);
2507 if (rc)
2508 goto free_nfc_dev;
2509
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002510 rc = pn533_setup(dev);
2511 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002512 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002513
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002514 return 0;
2515
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002516unregister_nfc_dev:
2517 nfc_unregister_device(dev->nfc_dev);
2518
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002519free_nfc_dev:
2520 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002521
Samuel Ortiz4849f852012-04-10 19:43:17 +02002522destroy_wq:
2523 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002524error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002525 usb_free_urb(dev->in_urb);
2526 kfree(dev->out_frame);
2527 usb_free_urb(dev->out_urb);
2528 kfree(dev);
2529 return rc;
2530}
2531
2532static void pn533_disconnect(struct usb_interface *interface)
2533{
2534 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002535 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002536
2537 dev = usb_get_intfdata(interface);
2538 usb_set_intfdata(interface, NULL);
2539
2540 nfc_unregister_device(dev->nfc_dev);
2541 nfc_free_device(dev->nfc_dev);
2542
2543 usb_kill_urb(dev->in_urb);
2544 usb_kill_urb(dev->out_urb);
2545
Samuel Ortiz4849f852012-04-10 19:43:17 +02002546 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002547
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002548 skb_queue_purge(&dev->resp_q);
2549
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002550 del_timer(&dev->listen_timer);
2551
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002552 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2553 list_del(&cmd->queue);
2554 kfree(cmd);
2555 }
2556
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002557 usb_free_urb(dev->in_urb);
2558 kfree(dev->out_frame);
2559 usb_free_urb(dev->out_urb);
2560 kfree(dev);
2561
Dan Carpenter276556d2011-07-08 10:21:15 +03002562 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002563}
2564
2565static struct usb_driver pn533_driver = {
2566 .name = "pn533",
2567 .probe = pn533_probe,
2568 .disconnect = pn533_disconnect,
2569 .id_table = pn533_table,
2570};
2571
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002572module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002573
2574MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2575 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2576MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2577MODULE_VERSION(VERSION);
2578MODULE_LICENSE("GPL");