blob: 82867a036ccc4a268e7c566eff8505f41fcb47a3 [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 urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300331
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200332 struct sk_buff_head resp_q;
333
Samuel Ortiz4849f852012-04-10 19:43:17 +0200334 struct workqueue_struct *wq;
335 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200336 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200337 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200338 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200339 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200340 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200341 struct pn533_frame *wq_in_frame;
342 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200343 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300344
345 pn533_cmd_complete_t cmd_complete;
346 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100347 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200348 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300349 u8 cmd;
350
351 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
352 u8 poll_mod_count;
353 u8 poll_mod_curr;
354 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200355 u32 listen_protocols;
356
357 u8 *gb;
358 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300359
360 u8 tgt_available_prots;
361 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200362 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200363
364 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200365
366 struct list_head cmd_queue;
367 u8 cmd_pending;
368};
369
370struct pn533_cmd {
371 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100372 u8 cmd_code;
373 struct sk_buff *req;
374 struct sk_buff *resp;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200375 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300376};
377
378struct pn533_frame {
379 u8 preamble;
380 __be16 start_frame;
381 u8 datalen;
382 u8 datalen_checksum;
383 u8 data[];
384} __packed;
385
386/* The rule: value + checksum = 0 */
387static inline u8 pn533_checksum(u8 value)
388{
389 return ~value + 1;
390}
391
392/* The rule: sum(data elements) + checksum = 0 */
393static u8 pn533_data_checksum(u8 *data, int datalen)
394{
395 u8 sum = 0;
396 int i;
397
398 for (i = 0; i < datalen; i++)
399 sum += data[i];
400
401 return pn533_checksum(sum);
402}
403
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300404static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
405{
406 frame->preamble = 0;
407 frame->start_frame = cpu_to_be16(PN533_SOF);
408 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
409 PN533_FRAME_CMD(frame) = cmd;
410 frame->datalen = 2;
411}
412
413static void pn533_tx_frame_finish(struct pn533_frame *frame)
414{
415 frame->datalen_checksum = pn533_checksum(frame->datalen);
416
417 PN533_FRAME_CHECKSUM(frame) =
418 pn533_data_checksum(frame->data, frame->datalen);
419
420 PN533_FRAME_POSTAMBLE(frame) = 0;
421}
422
423static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
424{
425 u8 checksum;
426
427 if (frame->start_frame != cpu_to_be16(PN533_SOF))
428 return false;
429
430 checksum = pn533_checksum(frame->datalen);
431 if (checksum != frame->datalen_checksum)
432 return false;
433
434 checksum = pn533_data_checksum(frame->data, frame->datalen);
435 if (checksum != PN533_FRAME_CHECKSUM(frame))
436 return false;
437
438 return true;
439}
440
441static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
442{
443 if (frame->start_frame != cpu_to_be16(PN533_SOF))
444 return false;
445
446 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
447 return false;
448
449 return true;
450}
451
452static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
453{
454 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
455}
456
Samuel Ortiz4849f852012-04-10 19:43:17 +0200457
458static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300459{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200460 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200461 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300462 int rc;
463
Samuel Ortiz4849f852012-04-10 19:43:17 +0200464 in_frame = dev->wq_in_frame;
465
466 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300467 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200468 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300469 else
470 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
471 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
472 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
473
474 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200475 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300476}
477
478static void pn533_recv_response(struct urb *urb)
479{
480 struct pn533 *dev = urb->context;
481 struct pn533_frame *in_frame;
482
Samuel Ortiz4849f852012-04-10 19:43:17 +0200483 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300484
485 switch (urb->status) {
486 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100487 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300488 case -ECONNRESET:
489 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100490 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100491 "The urb has been canceled (status %d)",
492 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200493 dev->wq_in_error = urb->status;
494 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100495 break;
496 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300497 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100498 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100499 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200500 dev->wq_in_error = urb->status;
501 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300502 }
503
504 in_frame = dev->in_urb->transfer_buffer;
505
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100506 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100507 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
508 in_frame, PN533_FRAME_SIZE(in_frame), false);
509
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300510 if (!pn533_rx_frame_is_valid(in_frame)) {
511 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200512 dev->wq_in_error = -EIO;
513 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 }
515
516 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100517 nfc_dev_err(&dev->interface->dev,
518 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200519 dev->wq_in_error = -EIO;
520 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300521 }
522
Samuel Ortiz4849f852012-04-10 19:43:17 +0200523 dev->wq_in_error = 0;
524 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300525
Samuel Ortiz4849f852012-04-10 19:43:17 +0200526sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200527 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528}
529
530static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
531{
532 dev->in_urb->complete = pn533_recv_response;
533
534 return usb_submit_urb(dev->in_urb, flags);
535}
536
537static void pn533_recv_ack(struct urb *urb)
538{
539 struct pn533 *dev = urb->context;
540 struct pn533_frame *in_frame;
541 int rc;
542
543 switch (urb->status) {
544 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100545 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300546 case -ECONNRESET:
547 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100548 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100549 "The urb has been stopped (status %d)",
550 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200551 dev->wq_in_error = urb->status;
552 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100553 break;
554 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300555 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100556 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100557 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200558 dev->wq_in_error = urb->status;
559 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300560 }
561
562 in_frame = dev->in_urb->transfer_buffer;
563
564 if (!pn533_rx_frame_is_ack(in_frame)) {
565 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200566 dev->wq_in_error = -EIO;
567 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568 }
569
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300570 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
571 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100572 nfc_dev_err(&dev->interface->dev,
573 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200574 dev->wq_in_error = rc;
575 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300576 }
577
578 return;
579
Samuel Ortiz4849f852012-04-10 19:43:17 +0200580sched_wq:
581 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200582 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300583}
584
585static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
586{
587 dev->in_urb->complete = pn533_recv_ack;
588
589 return usb_submit_urb(dev->in_urb, flags);
590}
591
592static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
593{
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100594 u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
595 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300596 int rc;
597
598 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
599
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100600 dev->out_urb->transfer_buffer = ack;
601 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 rc = usb_submit_urb(dev->out_urb, flags);
603
604 return rc;
605}
606
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100607static int __pn533_send_frame_async(struct pn533 *dev,
608 struct sk_buff *out,
609 struct sk_buff *in,
610 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300611 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100612 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300613{
614 int rc;
615
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100616 dev->cmd = PN533_FRAME_CMD(((struct pn533_frame *)out->data));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300617 dev->cmd_complete = cmd_complete;
618 dev->cmd_complete_arg = arg;
619
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100620 dev->out_urb->transfer_buffer = out->data;
621 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300622
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100623 dev->in_urb->transfer_buffer = in->data;
624 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300625
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100626 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100627 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100628
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100629 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300630 if (rc)
631 return rc;
632
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100633 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300634 if (rc)
635 goto error;
636
637 return 0;
638
639error:
640 usb_unlink_urb(dev->out_urb);
641 return rc;
642}
643
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100644static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
645{
646 struct pn533_frame *frame;
647 /* payload is already there, just update datalen */
648 int payload_len = skb->len;
649
650 skb_push(skb, PN533_FRAME_HEADER_LEN);
651 skb_put(skb, PN533_FRAME_TAIL_LEN);
652
653 frame = (struct pn533_frame *)skb->data;
654
655 pn533_tx_frame_init(frame, cmd_code);
656 frame->datalen += payload_len;
657 pn533_tx_frame_finish(frame);
658}
659
660struct pn533_send_async_complete_arg {
661 pn533_send_async_complete_t complete_cb;
662 void *complete_cb_context;
663 struct sk_buff *resp;
664 struct sk_buff *req;
665};
666
667static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
668 int params_len)
669{
670 struct pn533_send_async_complete_arg *arg = _arg;
671
672 struct sk_buff *req = arg->req;
673 struct sk_buff *resp = arg->resp;
674
675 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
676 int rc;
677
678 dev_kfree_skb(req);
679
680 if (params_len < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681 arg->complete_cb(dev, arg->complete_cb_context,
682 ERR_PTR(params_len));
683 rc = params_len;
684 dev_kfree_skb(resp);
685 goto out;
686 }
687
688 skb_put(resp, PN533_FRAME_SIZE(frame));
689 skb_pull(resp, PN533_FRAME_HEADER_LEN);
690 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
691
692 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
693
694out:
695 kfree(arg);
696 return rc;
697}
698
699static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
700 struct sk_buff *req, struct sk_buff *resp,
701 int resp_len,
702 pn533_send_async_complete_t complete_cb,
703 void *complete_cb_context)
704{
705 struct pn533_cmd *cmd;
706 struct pn533_send_async_complete_arg *arg;
707 int rc = 0;
708
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100709 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100710
711 arg = kzalloc(sizeof(arg), GFP_KERNEL);
712 if (!arg)
713 return -ENOMEM;
714
715 arg->complete_cb = complete_cb;
716 arg->complete_cb_context = complete_cb_context;
717 arg->resp = resp;
718 arg->req = req;
719
720 pn533_build_cmd_frame(cmd_code, req);
721
722 mutex_lock(&dev->cmd_lock);
723
724 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100725 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
726 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100727 if (rc)
728 goto error;
729
730 dev->cmd_pending = 1;
731 goto unlock;
732 }
733
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100734 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
735 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100736
737 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
738 if (!cmd) {
739 rc = -ENOMEM;
740 goto error;
741 }
742
743 INIT_LIST_HEAD(&cmd->queue);
744 cmd->cmd_code = cmd_code;
745 cmd->req = req;
746 cmd->resp = resp;
747 cmd->arg = arg;
748
749 list_add_tail(&cmd->queue, &dev->cmd_queue);
750
751 goto unlock;
752
753error:
754 kfree(arg);
755unlock:
756 mutex_unlock(&dev->cmd_lock);
757 return rc;
758}
759
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100760static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
761 struct sk_buff *req,
762 pn533_send_async_complete_t complete_cb,
763 void *complete_cb_context)
764{
765 struct sk_buff *resp;
766 int rc;
767 int resp_len = PN533_FRAME_HEADER_LEN +
768 PN533_FRAME_MAX_PAYLOAD_LEN +
769 PN533_FRAME_TAIL_LEN;
770
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100771 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
772 if (!resp)
773 return -ENOMEM;
774
775 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
776 complete_cb_context);
777 if (rc)
778 dev_kfree_skb(resp);
779
780 return rc;
781}
782
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100783static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
784 struct sk_buff *req,
785 pn533_send_async_complete_t complete_cb,
786 void *complete_cb_context)
787{
788 struct sk_buff *resp;
789 int rc;
790
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100791 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
792 if (!resp)
793 return -ENOMEM;
794
795 rc = __pn533_send_async(dev, cmd_code, req, resp,
796 PN533_NORMAL_FRAME_MAX_LEN,
797 complete_cb, complete_cb_context);
798 if (rc)
799 dev_kfree_skb(resp);
800
801 return rc;
802}
803
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100804/*
805 * pn533_send_cmd_direct_async
806 *
807 * The function sends a piority cmd directly to the chip omiting the cmd
808 * queue. It's intended to be used by chaining mechanism of received responses
809 * where the host has to request every single chunk of data before scheduling
810 * next cmd from the queue.
811 */
812static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
813 struct sk_buff *req,
814 pn533_send_async_complete_t complete_cb,
815 void *complete_cb_context)
816{
817 struct pn533_send_async_complete_arg *arg;
818 struct sk_buff *resp;
819 int rc;
820 int resp_len = PN533_FRAME_HEADER_LEN +
821 PN533_FRAME_MAX_PAYLOAD_LEN +
822 PN533_FRAME_TAIL_LEN;
823
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100824 resp = alloc_skb(resp_len, GFP_KERNEL);
825 if (!resp)
826 return -ENOMEM;
827
828 arg = kzalloc(sizeof(arg), GFP_KERNEL);
829 if (!arg) {
830 dev_kfree_skb(resp);
831 return -ENOMEM;
832 }
833
834 arg->complete_cb = complete_cb;
835 arg->complete_cb_context = complete_cb_context;
836 arg->resp = resp;
837 arg->req = req;
838
839 pn533_build_cmd_frame(cmd_code, req);
840
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100841 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
842 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100843 if (rc < 0) {
844 dev_kfree_skb(resp);
845 kfree(arg);
846 }
847
848 return rc;
849}
850
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200851static void pn533_wq_cmd(struct work_struct *work)
852{
853 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
854 struct pn533_cmd *cmd;
855
856 mutex_lock(&dev->cmd_lock);
857
858 if (list_empty(&dev->cmd_queue)) {
859 dev->cmd_pending = 0;
860 mutex_unlock(&dev->cmd_lock);
861 return;
862 }
863
864 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
865
Szymon Janc60ad07a2012-10-25 17:29:45 +0200866 list_del(&cmd->queue);
867
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200868 mutex_unlock(&dev->cmd_lock);
869
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100870 __pn533_send_frame_async(dev, cmd->req, cmd->resp,
871 PN533_NORMAL_FRAME_MAX_LEN,
872 pn533_send_async_complete,
873 cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200874
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200875 kfree(cmd);
876}
877
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300878struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100879 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300880 struct completion done;
881};
882
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100883static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
884 struct sk_buff *resp)
885{
886 struct pn533_sync_cmd_response *arg = _arg;
887
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100888 arg->resp = resp;
889 complete(&arg->done);
890
891 return 0;
892}
893
894/* pn533_send_cmd_sync
895 *
896 * Please note the req parameter is freed inside the function to
897 * limit a number of return value interpretations by the caller.
898 *
899 * 1. negative in case of error during TX path -> req should be freed
900 *
901 * 2. negative in case of error during RX path -> req should not be freed
902 * as it's been already freed at the begining of RX path by
903 * async_complete_cb.
904 *
905 * 3. valid pointer in case of succesfult RX path
906 *
907 * A caller has to check a return value with IS_ERR macro. If the test pass,
908 * the returned pointer is valid.
909 *
910 * */
911static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
912 struct sk_buff *req)
913{
914 int rc;
915 struct pn533_sync_cmd_response arg;
916
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100917 init_completion(&arg.done);
918
919 rc = pn533_send_cmd_async(dev, cmd_code, req,
920 pn533_send_sync_complete, &arg);
921 if (rc) {
922 dev_kfree_skb(req);
923 return ERR_PTR(rc);
924 }
925
926 wait_for_completion(&arg.done);
927
928 return arg.resp;
929}
930
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300931static void pn533_send_complete(struct urb *urb)
932{
933 struct pn533 *dev = urb->context;
934
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300935 switch (urb->status) {
936 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100937 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300938 case -ECONNRESET:
939 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100940 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100941 "The urb has been stopped (status %d)",
942 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300943 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100944 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300945 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100946 nfc_dev_err(&dev->interface->dev,
947 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300948 }
949}
950
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100951static struct sk_buff *pn533_alloc_skb(unsigned int size)
952{
953 struct sk_buff *skb;
954
955 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
956 size +
957 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
958
959 if (skb)
960 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
961
962 return skb;
963}
964
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300965struct pn533_target_type_a {
966 __be16 sens_res;
967 u8 sel_res;
968 u8 nfcid_len;
969 u8 nfcid_data[];
970} __packed;
971
972
973#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
974#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
975#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
976
977#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
978#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
979
980#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
981#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
982
983#define PN533_TYPE_A_SEL_PROT_MIFARE 0
984#define PN533_TYPE_A_SEL_PROT_ISO14443 1
985#define PN533_TYPE_A_SEL_PROT_DEP 2
986#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
987
988static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
989 int target_data_len)
990{
991 u8 ssd;
992 u8 platconf;
993
994 if (target_data_len < sizeof(struct pn533_target_type_a))
995 return false;
996
997 /* The lenght check of nfcid[] and ats[] are not being performed because
998 the values are not being used */
999
1000 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1001 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1002 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1003
1004 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001005 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1006 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1007 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001008 return false;
1009
1010 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1011 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1012 return false;
1013
1014 return true;
1015}
1016
1017static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1018 int tgt_data_len)
1019{
1020 struct pn533_target_type_a *tgt_type_a;
1021
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001022 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001023
1024 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1025 return -EPROTO;
1026
1027 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1028 case PN533_TYPE_A_SEL_PROT_MIFARE:
1029 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1030 break;
1031 case PN533_TYPE_A_SEL_PROT_ISO14443:
1032 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1033 break;
1034 case PN533_TYPE_A_SEL_PROT_DEP:
1035 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1036 break;
1037 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1038 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1039 NFC_PROTO_NFC_DEP_MASK;
1040 break;
1041 }
1042
1043 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1044 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001045 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1046 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001047
1048 return 0;
1049}
1050
1051struct pn533_target_felica {
1052 u8 pol_res;
1053 u8 opcode;
1054 u8 nfcid2[8];
1055 u8 pad[8];
1056 /* optional */
1057 u8 syst_code[];
1058} __packed;
1059
1060#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1061#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1062
1063static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1064 int target_data_len)
1065{
1066 if (target_data_len < sizeof(struct pn533_target_felica))
1067 return false;
1068
1069 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1070 return false;
1071
1072 return true;
1073}
1074
1075static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1076 int tgt_data_len)
1077{
1078 struct pn533_target_felica *tgt_felica;
1079
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001080 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001081
1082 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1083 return -EPROTO;
1084
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001085 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1086 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001087 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1088 else
1089 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1090
Samuel Ortiz79757542012-03-05 01:03:45 +01001091 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1092 nfc_tgt->sensf_res_len = 9;
1093
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001094 return 0;
1095}
1096
1097struct pn533_target_jewel {
1098 __be16 sens_res;
1099 u8 jewelid[4];
1100} __packed;
1101
1102static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1103 int target_data_len)
1104{
1105 u8 ssd;
1106 u8 platconf;
1107
1108 if (target_data_len < sizeof(struct pn533_target_jewel))
1109 return false;
1110
1111 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1112 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1113 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1114
1115 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001116 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1117 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1118 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001119 return false;
1120
1121 return true;
1122}
1123
1124static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1125 int tgt_data_len)
1126{
1127 struct pn533_target_jewel *tgt_jewel;
1128
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001129 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001130
1131 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1132 return -EPROTO;
1133
1134 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1135 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001136 nfc_tgt->nfcid1_len = 4;
1137 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001138
1139 return 0;
1140}
1141
1142struct pn533_type_b_prot_info {
1143 u8 bitrate;
1144 u8 fsci_type;
1145 u8 fwi_adc_fo;
1146} __packed;
1147
1148#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1149#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1150#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1151
1152struct pn533_type_b_sens_res {
1153 u8 opcode;
1154 u8 nfcid[4];
1155 u8 appdata[4];
1156 struct pn533_type_b_prot_info prot_info;
1157} __packed;
1158
1159#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1160
1161struct pn533_target_type_b {
1162 struct pn533_type_b_sens_res sensb_res;
1163 u8 attrib_res_len;
1164 u8 attrib_res[];
1165} __packed;
1166
1167static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1168 int target_data_len)
1169{
1170 if (target_data_len < sizeof(struct pn533_target_type_b))
1171 return false;
1172
1173 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1174 return false;
1175
1176 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1177 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1178 return false;
1179
1180 return true;
1181}
1182
1183static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1184 int tgt_data_len)
1185{
1186 struct pn533_target_type_b *tgt_type_b;
1187
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001188 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001189
1190 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1191 return -EPROTO;
1192
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001193 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001194
1195 return 0;
1196}
1197
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001198static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1199 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001200{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001201 struct nfc_target nfc_tgt;
1202 int rc;
1203
1204 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001205 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001206
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001207 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001208 return -EPROTO;
1209
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001210 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1211
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001212 switch (dev->poll_mod_curr) {
1213 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001214 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001215 break;
1216 case PN533_POLL_MOD_212KBPS_FELICA:
1217 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001218 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001219 break;
1220 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001221 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001222 break;
1223 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001224 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001225 break;
1226 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001227 nfc_dev_err(&dev->interface->dev,
1228 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229 return -EPROTO;
1230 }
1231
1232 if (rc)
1233 return rc;
1234
1235 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001236 nfc_dev_dbg(&dev->interface->dev,
1237 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001238 return -EAGAIN;
1239 }
1240
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001241 nfc_dev_dbg(&dev->interface->dev,
1242 "Target found - supported protocols: 0x%x",
1243 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001244
1245 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1246
1247 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1248
1249 return 0;
1250}
1251
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001252static inline void pn533_poll_next_mod(struct pn533 *dev)
1253{
1254 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1255}
1256
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257static void pn533_poll_reset_mod_list(struct pn533 *dev)
1258{
1259 dev->poll_mod_count = 0;
1260}
1261
1262static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1263{
1264 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001265 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001266 dev->poll_mod_count++;
1267}
1268
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001269static void pn533_poll_create_mod_list(struct pn533 *dev,
1270 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001271{
1272 pn533_poll_reset_mod_list(dev);
1273
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001274 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1275 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1276 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001277 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1278
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001279 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1280 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001281 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1282 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1283 }
1284
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001285 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001286 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1287
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001288 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001289 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001290
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001291 if (tm_protocols)
1292 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001293}
1294
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001295static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001296{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001297 u8 nbtg, tg, *tgdata;
1298 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299
1300 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1301
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001302 nbtg = resp->data[0];
1303 tg = resp->data[1];
1304 tgdata = &resp->data[2];
1305 tgdata_len = resp->len - 2; /* nbtg + tg */
1306
1307 if (nbtg) {
1308 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309
1310 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001311 if (rc == 0) {
1312 pn533_poll_reset_mod_list(dev);
1313 return 0;
1314 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001315 }
1316
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001317 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318}
1319
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001320static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001321{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001322 struct sk_buff *skb;
1323 u8 *felica, *nfcid3, *gb;
1324
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001325 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1326 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1327 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1328 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001329
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001330 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1331 0x0, 0x0, 0x0,
1332 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001333
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001334 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1335 felica (18), nfcid3 (10), gb_len (1) */
1336 gbytes_len +
1337 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001338
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001339 skb = pn533_alloc_skb(skb_len);
1340 if (!skb)
1341 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001342
1343 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001344 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001345
1346 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001347 memcpy(skb_put(skb, 6), mifare_params, 6);
1348
1349 /* Felica params */
1350 felica = skb_put(skb, 18);
1351 memcpy(felica, felica_params, 18);
1352 get_random_bytes(felica + 2, 6);
1353
1354 /* NFCID3 */
1355 nfcid3 = skb_put(skb, 10);
1356 memset(nfcid3, 0, 10);
1357 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001358
1359 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001360 *skb_put(skb, 1) = gbytes_len;
1361
1362 gb = skb_put(skb, gbytes_len);
1363 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001364
Samuel Ortizad3823c2012-05-30 23:54:55 +02001365 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001366 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001367
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001368 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001369}
1370
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001371#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001372#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1373static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001374 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001375{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001376 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001377
1378 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1379
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001380 if (IS_ERR(resp))
1381 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001382
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001383 status = resp->data[0];
1384 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001385
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001386 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001387 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001388 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001389 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001390 return 0;
1391 }
1392
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001393 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001394}
1395
1396static void pn533_wq_tg_get_data(struct work_struct *work)
1397{
1398 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001399
1400 struct sk_buff *skb;
1401 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001402
1403 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1404
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001405 skb = pn533_alloc_skb(0);
1406 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001407 return;
1408
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1410 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001411
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001412 if (rc < 0)
1413 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001414
1415 return;
1416}
1417
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001418#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001419static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001420{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001421 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001422 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001423 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001424
1425 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1426
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001427 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001428 return -EINVAL;
1429
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001430 mode = resp->data[0];
1431 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001432
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001433 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1434 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001435
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001436 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1437 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001438 comm_mode = NFC_COMM_ACTIVE;
1439
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001440 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001441 return -EOPNOTSUPP;
1442
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001443 gb = cmd + ATR_REQ_GB_OFFSET;
1444 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001445
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001446 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1447 comm_mode, gb, gb_len);
1448 if (rc < 0) {
1449 nfc_dev_err(&dev->interface->dev,
1450 "Error when signaling target activation");
1451 return rc;
1452 }
1453
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001454 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001455 queue_work(dev->wq, &dev->tg_work);
1456
1457 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001458}
1459
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001460static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001461{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001462 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001463
1464 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1465
1466 /* An ack will cancel the last issued command (poll) */
1467 pn533_send_ack(dev, GFP_ATOMIC);
1468
1469 dev->cancel_listen = 1;
1470
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001471 pn533_poll_next_mod(dev);
1472
1473 queue_work(dev->wq, &dev->poll_work);
1474}
1475
1476static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001477 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001478{
1479 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001480 int rc;
1481
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001482 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1483
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001484 if (IS_ERR(resp)) {
1485 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001486
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001487 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1488 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001489
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001490 if (rc == -ENOENT) {
1491 if (dev->poll_mod_count != 0)
1492 return rc;
1493 else
1494 goto stop_poll;
1495 } else if (rc < 0) {
1496 nfc_dev_err(&dev->interface->dev,
1497 "Error %d when running poll", rc);
1498 goto stop_poll;
1499 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001500 }
1501
1502 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1503
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001504 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001505 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001506 rc = pn533_init_target_complete(dev, resp);
1507 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001508 }
1509
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001510 /* Initiator mode */
1511 rc = pn533_start_poll_complete(dev, resp);
1512 if (!rc)
1513 goto done;
1514
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001515 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001516 queue_work(dev->wq, &dev->poll_work);
1517
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001518done:
1519 dev_kfree_skb(resp);
1520 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001521
1522stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001523 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1524
Samuel Ortizad3823c2012-05-30 23:54:55 +02001525 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001526 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001527 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001529
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001530static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1531 *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001532{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001533 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001534
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001535 skb = pn533_alloc_skb(mod->len);
1536 if (!skb)
1537 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001538
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001539 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001540
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001541 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001542}
1543
1544static int pn533_send_poll_frame(struct pn533 *dev)
1545{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001546 struct pn533_poll_modulations *mod;
1547 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001548 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001549 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001550
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001553 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1554 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001555
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001556 if (mod->len == 0) { /* Listen mode */
1557 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1558 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1559 } else { /* Polling mode */
1560 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1561 skb = pn533_alloc_poll_in_frame(mod);
1562 }
1563
1564 if (!skb) {
1565 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1566 return -ENOMEM;
1567 }
1568
1569 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1570 NULL);
1571 if (rc < 0) {
1572 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001573 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001574 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001575
1576 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001577}
1578
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001579static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001580{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1582 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001583 int rc;
1584
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001585 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1586
1587 nfc_dev_dbg(&dev->interface->dev,
1588 "%s cancel_listen %d modulation len %d",
1589 __func__, dev->cancel_listen, cur_mod->len);
1590
1591 if (dev->cancel_listen == 1) {
1592 dev->cancel_listen = 0;
1593 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001594 }
1595
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001596 rc = pn533_send_poll_frame(dev);
1597 if (rc)
1598 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001599
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001600 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1601 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001602
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001603 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001604}
1605
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001606static int pn533_start_poll(struct nfc_dev *nfc_dev,
1607 u32 im_protocols, u32 tm_protocols)
1608{
1609 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1610
1611 nfc_dev_dbg(&dev->interface->dev,
1612 "%s: im protocols 0x%x tm protocols 0x%x",
1613 __func__, im_protocols, tm_protocols);
1614
1615 if (dev->tgt_active_prot) {
1616 nfc_dev_err(&dev->interface->dev,
1617 "Cannot poll with a target already activated");
1618 return -EBUSY;
1619 }
1620
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001621 if (dev->tgt_mode) {
1622 nfc_dev_err(&dev->interface->dev,
1623 "Cannot poll while already being activated");
1624 return -EBUSY;
1625 }
1626
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001627 if (tm_protocols) {
1628 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1629 if (dev->gb == NULL)
1630 tm_protocols = 0;
1631 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001632
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001633 dev->poll_mod_curr = 0;
1634 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1635 dev->poll_protocols = im_protocols;
1636 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001637
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001638 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001639}
1640
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001641static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1642{
1643 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1644
1645 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1646
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001647 del_timer(&dev->listen_timer);
1648
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001649 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001650 nfc_dev_dbg(&dev->interface->dev,
1651 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001652 return;
1653 }
1654
1655 /* An ack will cancel the last issued command (poll) */
1656 pn533_send_ack(dev, GFP_KERNEL);
1657
1658 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1659 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001660
1661 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001662}
1663
1664static int pn533_activate_target_nfcdep(struct pn533 *dev)
1665{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001666 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001667 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001668 int rc;
1669
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001670 struct sk_buff *skb;
1671 struct sk_buff *resp;
1672
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001673 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1674
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001675 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1676 if (!skb)
1677 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001679 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1680 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001681
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001682 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1683 if (IS_ERR(resp))
1684 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001685
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001686 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001687 rc = rsp->status & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001689 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001690 return -EIO;
1691
Samuel Ortiz541d9202011-12-14 16:43:10 +01001692 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001693 gt_len = resp->len - 16;
1694 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001695
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001696 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001697 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698}
1699
Eric Lapuyade90099432012-05-07 12:31:13 +02001700static int pn533_activate_target(struct nfc_dev *nfc_dev,
1701 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001702{
1703 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1704 int rc;
1705
1706 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001707 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001708
1709 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001710 nfc_dev_err(&dev->interface->dev,
1711 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001712 return -EBUSY;
1713 }
1714
1715 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001716 nfc_dev_err(&dev->interface->dev,
1717 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718 return -EBUSY;
1719 }
1720
1721 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001722 nfc_dev_err(&dev->interface->dev,
1723 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001724 return -EINVAL;
1725 }
1726
1727 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001728 nfc_dev_err(&dev->interface->dev,
1729 "Target doesn't support requested proto %u",
1730 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001731 return -EINVAL;
1732 }
1733
1734 if (protocol == NFC_PROTO_NFC_DEP) {
1735 rc = pn533_activate_target_nfcdep(dev);
1736 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001737 nfc_dev_err(&dev->interface->dev,
1738 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001739 return rc;
1740 }
1741 }
1742
1743 dev->tgt_active_prot = protocol;
1744 dev->tgt_available_prots = 0;
1745
1746 return 0;
1747}
1748
Eric Lapuyade90099432012-05-07 12:31:13 +02001749static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1750 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751{
1752 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001753
1754 struct sk_buff *skb;
1755 struct sk_buff *resp;
1756
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001757 int rc;
1758
1759 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1760
1761 if (!dev->tgt_active_prot) {
1762 nfc_dev_err(&dev->interface->dev, "There is no active target");
1763 return;
1764 }
1765
1766 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001767 skb_queue_purge(&dev->resp_q);
1768
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001769 skb = pn533_alloc_skb(sizeof(u8));
1770 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001771 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001772
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001773 *skb_put(skb, 1) = 1; /* TG*/
1774
1775 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1776 if (IS_ERR(resp))
1777 return;
1778
1779 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001780 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001781 nfc_dev_err(&dev->interface->dev,
1782 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001783
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001784 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001785 return;
1786}
1787
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001788
1789static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001790 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001791{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001792 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001793 u8 target_gt_len;
1794 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001795 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001796
1797 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001798
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001799 if (IS_ERR(resp))
1800 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001801
1802 if (dev->tgt_available_prots &&
1803 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1804 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001805 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001806 rc = -EINVAL;
1807 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001808 }
1809
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001810 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1811
1812 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001813 if (rc != PN533_CMD_RET_SUCCESS) {
1814 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001815 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001816 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001817 }
1818
1819 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001820 struct nfc_target nfc_target;
1821
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001822 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1823
1824 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001825 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001826 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001827 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1828 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001829 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001830
1831 dev->tgt_available_prots = 0;
1832 }
1833
1834 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1835
1836 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001837 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001838 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840 if (rc == 0)
1841 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001842 dev->nfc_dev->targets[0].idx,
1843 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001844
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001845error:
1846 dev_kfree_skb(resp);
1847 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001848}
1849
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001850static int pn533_mod_to_baud(struct pn533 *dev)
1851{
1852 switch (dev->poll_mod_curr) {
1853 case PN533_POLL_MOD_106KBPS_A:
1854 return 0;
1855 case PN533_POLL_MOD_212KBPS_FELICA:
1856 return 1;
1857 case PN533_POLL_MOD_424KBPS_FELICA:
1858 return 2;
1859 default:
1860 return -EINVAL;
1861 }
1862}
1863
Samuel Ortizd7f33452012-05-29 21:45:21 +02001864#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001865static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001866 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867{
1868 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 struct sk_buff *skb;
1870 int rc, baud, skb_len;
1871 u8 *next, *arg;
1872
Samuel Ortizd7f33452012-05-29 21:45:21 +02001873 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001874
1875 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1876
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877 if (dev->poll_mod_count) {
1878 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001879 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001880 return -EBUSY;
1881 }
1882
1883 if (dev->tgt_active_prot) {
1884 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001885 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001886 return -EBUSY;
1887 }
1888
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001889 baud = pn533_mod_to_baud(dev);
1890 if (baud < 0) {
1891 nfc_dev_err(&dev->interface->dev,
1892 "Invalid curr modulation %d", dev->poll_mod_curr);
1893 return baud;
1894 }
1895
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001896 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001897 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001898 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001899
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001900 skb = pn533_alloc_skb(skb_len);
1901 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001902 return -ENOMEM;
1903
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001904 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1905 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001907 next = skb_put(skb, 1); /* Next */
1908 *next = 0;
1909
1910 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1911 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1912 PASSIVE_DATA_LEN);
1913 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001914 }
1915
Samuel Ortiz47807d32012-03-05 01:03:50 +01001916 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001917 memcpy(skb_put(skb, gb_len), gb, gb_len);
1918 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001919 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001920 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001921 }
1922
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001923 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1924 if (!arg) {
1925 dev_kfree_skb(skb);
1926 return -ENOMEM;
1927 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001928
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001929 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001930
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001931 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1932 pn533_in_dep_link_up_complete, arg);
1933
1934 if (rc < 0) {
1935 dev_kfree_skb(skb);
1936 kfree(arg);
1937 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001938
1939 return rc;
1940}
1941
1942static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1943{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001944 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1945
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001946 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1947
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001948 pn533_poll_reset_mod_list(dev);
1949
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001950 if (dev->tgt_mode || dev->tgt_active_prot) {
1951 pn533_send_ack(dev, GFP_KERNEL);
1952 usb_kill_urb(dev->in_urb);
1953 }
1954
1955 dev->tgt_active_prot = 0;
1956 dev->tgt_mode = 0;
1957
1958 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001959
1960 return 0;
1961}
1962
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001963struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001964 data_exchange_cb_t cb;
1965 void *cb_context;
1966};
1967
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001968static struct sk_buff *pn533_build_response(struct pn533 *dev)
1969{
1970 struct sk_buff *skb, *tmp, *t;
1971 unsigned int skb_len = 0, tmp_len = 0;
1972
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001973 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001974
1975 if (skb_queue_empty(&dev->resp_q))
1976 return NULL;
1977
1978 if (skb_queue_len(&dev->resp_q) == 1) {
1979 skb = skb_dequeue(&dev->resp_q);
1980 goto out;
1981 }
1982
1983 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1984 skb_len += tmp->len;
1985
1986 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1987 __func__, skb_len);
1988
1989 skb = alloc_skb(skb_len, GFP_KERNEL);
1990 if (skb == NULL)
1991 goto out;
1992
1993 skb_put(skb, skb_len);
1994
1995 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1996 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1997 tmp_len += tmp->len;
1998 }
1999
2000out:
2001 skb_queue_purge(&dev->resp_q);
2002
2003 return skb;
2004}
2005
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002006static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002007 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002008{
2009 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002010 struct sk_buff *skb;
2011 int rc = 0;
2012 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002013
2014 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2015
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002016 if (IS_ERR(resp)) {
2017 rc = PTR_ERR(resp);
2018 goto _error;
2019 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002020
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002021 status = resp->data[0];
2022 ret = status & PN533_CMD_RET_MASK;
2023 mi = status & PN533_CMD_MI_MASK;
2024
2025 skb_pull(resp, sizeof(status));
2026
2027 if (ret != PN533_CMD_RET_SUCCESS) {
2028 nfc_dev_err(&dev->interface->dev,
2029 "PN533 reported error %d when exchanging data",
2030 ret);
2031 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002032 goto error;
2033 }
2034
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002035 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002036
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002037 if (mi) {
2038 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002039 queue_work(dev->wq, &dev->mi_work);
2040 return -EINPROGRESS;
2041 }
2042
2043 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002044 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002045 goto error;
2046
2047 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002048 kfree(arg);
2049 return 0;
2050
2051error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002052 dev_kfree_skb(resp);
2053_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002054 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002055 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002056 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002057 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002058}
2059
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002060static int pn533_transceive(struct nfc_dev *nfc_dev,
2061 struct nfc_target *target, struct sk_buff *skb,
2062 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002063{
2064 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002065 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002066 int rc;
2067
2068 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2069
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002070 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2071 /* TODO: Implement support to multi-part data exchange */
2072 nfc_dev_err(&dev->interface->dev,
2073 "Data length greater than the max allowed: %d",
2074 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2075 rc = -ENOSYS;
2076 goto error;
2077 }
2078
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002079 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002080 nfc_dev_err(&dev->interface->dev,
2081 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002082 rc = -EINVAL;
2083 goto error;
2084 }
2085
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002086 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002087 if (!arg) {
2088 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002089 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002090 }
2091
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002092 arg->cb = cb;
2093 arg->cb_context = cb_context;
2094
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002095 switch (dev->device_type) {
2096 case PN533_DEVICE_PASORI:
2097 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2098 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2099 skb,
2100 pn533_data_exchange_complete,
2101 arg);
2102
2103 break;
2104 }
2105 default:
2106 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2107
2108 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2109 skb, pn533_data_exchange_complete,
2110 arg);
2111
2112 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002113 }
2114
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002115 if (rc < 0) /* rc from send_async */
2116 goto error;
2117
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002118 return 0;
2119
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002121 kfree(arg);
2122 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123 return rc;
2124}
2125
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002126static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002127 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002128{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002129 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002130
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002131 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2132
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002133 if (IS_ERR(resp))
2134 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002135
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002136 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002137
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002138 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002139
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002140 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002141 nfc_tm_deactivated(dev->nfc_dev);
2142
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002143 dev->tgt_mode = 0;
2144
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002145 return 0;
2146 }
2147
2148 queue_work(dev->wq, &dev->tg_work);
2149
2150 return 0;
2151}
2152
2153static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2154{
2155 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002156 int rc;
2157
2158 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2159
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002160 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002161 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002162 "Data length greater than the max allowed: %d",
2163 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2164 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002165 }
2166
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2168 pn533_tm_send_complete, NULL);
2169 if (rc < 0)
2170 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002171
2172 return rc;
2173}
2174
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002175static void pn533_wq_mi_recv(struct work_struct *work)
2176{
2177 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002178
2179 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002180 int rc;
2181
2182 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2183
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002184 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2185 if (!skb)
2186 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002187
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002188 switch (dev->device_type) {
2189 case PN533_DEVICE_PASORI:
2190 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2191 rc = pn533_send_cmd_direct_async(dev,
2192 PN533_CMD_IN_COMM_THRU,
2193 skb,
2194 pn533_data_exchange_complete,
2195 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002196
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002197 break;
2198 }
2199 default:
2200 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002201
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002202 rc = pn533_send_cmd_direct_async(dev,
2203 PN533_CMD_IN_DATA_EXCHANGE,
2204 skb,
2205 pn533_data_exchange_complete,
2206 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002207
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002208 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002209 }
2210
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002211 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002212 return;
2213
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002214 nfc_dev_err(&dev->interface->dev,
2215 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002216
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002217 dev_kfree_skb(skb);
2218 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002219
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002220error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002221 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002222 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002223}
2224
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002225static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2226 u8 cfgdata_len)
2227{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002228 struct sk_buff *skb;
2229 struct sk_buff *resp;
2230
2231 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002232
2233 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2234
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002235 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002236
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002237 skb = pn533_alloc_skb(skb_len);
2238 if (!skb)
2239 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002240
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002241 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2242 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002243
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002244 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2245 if (IS_ERR(resp))
2246 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002247
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002248 dev_kfree_skb(resp);
2249 return 0;
2250}
2251
2252static int pn533_get_firmware_version(struct pn533 *dev,
2253 struct pn533_fw_version *fv)
2254{
2255 struct sk_buff *skb;
2256 struct sk_buff *resp;
2257
2258 skb = pn533_alloc_skb(0);
2259 if (!skb)
2260 return -ENOMEM;
2261
2262 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2263 if (IS_ERR(resp))
2264 return PTR_ERR(resp);
2265
2266 fv->ic = resp->data[0];
2267 fv->ver = resp->data[1];
2268 fv->rev = resp->data[2];
2269 fv->support = resp->data[3];
2270
2271 dev_kfree_skb(resp);
2272 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002273}
2274
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002275static int pn533_fw_reset(struct pn533 *dev)
2276{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002277 struct sk_buff *skb;
2278 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002279
2280 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2281
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002282 skb = pn533_alloc_skb(sizeof(u8));
2283 if (!skb)
2284 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002285
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002287
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002288 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2289 if (IS_ERR(resp))
2290 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002291
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002292 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002293
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002294 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002295}
2296
2297static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002298 .dev_up = NULL,
2299 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002300 .dep_link_up = pn533_dep_link_up,
2301 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002302 .start_poll = pn533_start_poll,
2303 .stop_poll = pn533_stop_poll,
2304 .activate_target = pn533_activate_target,
2305 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002306 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002307 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002308};
2309
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002310static int pn533_setup(struct pn533 *dev)
2311{
2312 struct pn533_config_max_retries max_retries;
2313 struct pn533_config_timing timing;
2314 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2315 int rc;
2316
2317 switch (dev->device_type) {
2318 case PN533_DEVICE_STD:
2319 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2320 max_retries.mx_rty_psl = 2;
2321 max_retries.mx_rty_passive_act =
2322 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2323
2324 timing.rfu = PN533_CONFIG_TIMING_102;
2325 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2326 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2327
2328 break;
2329
2330 case PN533_DEVICE_PASORI:
2331 max_retries.mx_rty_atr = 0x2;
2332 max_retries.mx_rty_psl = 0x1;
2333 max_retries.mx_rty_passive_act =
2334 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2335
2336 timing.rfu = PN533_CONFIG_TIMING_102;
2337 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2338 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2339
2340 break;
2341
2342 default:
2343 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2344 dev->device_type);
2345 return -EINVAL;
2346 }
2347
2348 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2349 (u8 *)&max_retries, sizeof(max_retries));
2350 if (rc) {
2351 nfc_dev_err(&dev->interface->dev,
2352 "Error on setting MAX_RETRIES config");
2353 return rc;
2354 }
2355
2356
2357 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2358 (u8 *)&timing, sizeof(timing));
2359 if (rc) {
2360 nfc_dev_err(&dev->interface->dev,
2361 "Error on setting RF timings");
2362 return rc;
2363 }
2364
2365 switch (dev->device_type) {
2366 case PN533_DEVICE_STD:
2367 break;
2368
2369 case PN533_DEVICE_PASORI:
2370 pn533_fw_reset(dev);
2371
2372 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2373 pasori_cfg, 3);
2374 if (rc) {
2375 nfc_dev_err(&dev->interface->dev,
2376 "Error while settings PASORI config");
2377 return rc;
2378 }
2379
2380 pn533_fw_reset(dev);
2381
2382 break;
2383 }
2384
2385 return 0;
2386}
2387
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002388static int pn533_probe(struct usb_interface *interface,
2389 const struct usb_device_id *id)
2390{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002391 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002392 struct pn533 *dev;
2393 struct usb_host_interface *iface_desc;
2394 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002395 int in_endpoint = 0;
2396 int out_endpoint = 0;
2397 int rc = -ENOMEM;
2398 int i;
2399 u32 protocols;
2400
2401 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2402 if (!dev)
2403 return -ENOMEM;
2404
2405 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2406 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002407 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002408
2409 iface_desc = interface->cur_altsetting;
2410 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2411 endpoint = &iface_desc->endpoint[i].desc;
2412
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002413 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002414 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002415
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002416 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002417 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418 }
2419
2420 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002421 nfc_dev_err(&interface->dev,
2422 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423 rc = -ENODEV;
2424 goto error;
2425 }
2426
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002427 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2429
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002430 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002431 goto error;
2432
2433 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002434 usb_rcvbulkpipe(dev->udev, in_endpoint),
2435 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002436 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002437 usb_sndbulkpipe(dev->udev, out_endpoint),
2438 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002439
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002440 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2441 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002442 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002443 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002444 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002445 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002446 if (dev->wq == NULL)
2447 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002448
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002449 init_timer(&dev->listen_timer);
2450 dev->listen_timer.data = (unsigned long) dev;
2451 dev->listen_timer.function = pn533_listen_mode_timer;
2452
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002453 skb_queue_head_init(&dev->resp_q);
2454
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002455 INIT_LIST_HEAD(&dev->cmd_queue);
2456
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002457 usb_set_intfdata(interface, dev);
2458
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002459 memset(&fw_ver, 0, sizeof(fw_ver));
2460 rc = pn533_get_firmware_version(dev, &fw_ver);
2461 if (rc < 0)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002462 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002464 nfc_dev_info(&dev->interface->dev,
2465 "NXP PN533 firmware ver %d.%d now attached",
2466 fw_ver.ver, fw_ver.rev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002467
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002468 dev->device_type = id->driver_info;
2469 switch (dev->device_type) {
2470 case PN533_DEVICE_STD:
2471 protocols = PN533_ALL_PROTOCOLS;
2472 break;
2473
2474 case PN533_DEVICE_PASORI:
2475 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2476 break;
2477
2478 default:
2479 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2480 dev->device_type);
2481 rc = -EINVAL;
2482 goto destroy_wq;
2483 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484
Samuel Ortize8753042011-08-19 15:47:11 +02002485 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002486 PN533_FRAME_HEADER_LEN +
Samuel Ortize8753042011-08-19 15:47:11 +02002487 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002488 PN533_FRAME_TAIL_LEN);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002489 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002490 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002491
2492 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2493 nfc_set_drvdata(dev->nfc_dev, dev);
2494
2495 rc = nfc_register_device(dev->nfc_dev);
2496 if (rc)
2497 goto free_nfc_dev;
2498
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002499 rc = pn533_setup(dev);
2500 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002501 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002502
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002503 return 0;
2504
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002505unregister_nfc_dev:
2506 nfc_unregister_device(dev->nfc_dev);
2507
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002508free_nfc_dev:
2509 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002510
Samuel Ortiz4849f852012-04-10 19:43:17 +02002511destroy_wq:
2512 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002513error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002514 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002515 usb_free_urb(dev->out_urb);
2516 kfree(dev);
2517 return rc;
2518}
2519
2520static void pn533_disconnect(struct usb_interface *interface)
2521{
2522 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002523 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002524
2525 dev = usb_get_intfdata(interface);
2526 usb_set_intfdata(interface, NULL);
2527
2528 nfc_unregister_device(dev->nfc_dev);
2529 nfc_free_device(dev->nfc_dev);
2530
2531 usb_kill_urb(dev->in_urb);
2532 usb_kill_urb(dev->out_urb);
2533
Samuel Ortiz4849f852012-04-10 19:43:17 +02002534 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002535
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002536 skb_queue_purge(&dev->resp_q);
2537
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002538 del_timer(&dev->listen_timer);
2539
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002540 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2541 list_del(&cmd->queue);
2542 kfree(cmd);
2543 }
2544
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002545 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002546 usb_free_urb(dev->out_urb);
2547 kfree(dev);
2548
Dan Carpenter276556d2011-07-08 10:21:15 +03002549 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002550}
2551
2552static struct usb_driver pn533_driver = {
2553 .name = "pn533",
2554 .probe = pn533_probe,
2555 .disconnect = pn533_disconnect,
2556 .id_table = pn533_table,
2557};
2558
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002559module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002560
2561MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2562 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2563MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2564MODULE_VERSION(VERSION);
2565MODULE_LICENSE("GPL");