blob: f563cac02f315f83fde4b7ff20d6d2ea7ab937c9 [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
44#define PN533_QUIRKS_TYPE_A BIT(0)
45#define PN533_QUIRKS_TYPE_F BIT(1)
46#define PN533_QUIRKS_DEP BIT(2)
47#define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
48
49#define PN533_DEVICE_STD 0x1
50#define PN533_DEVICE_PASORI 0x2
51
Samuel Ortiz01d719a2012-07-04 00:14:04 +020052#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
53 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
54 NFC_PROTO_NFC_DEP_MASK |\
55 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056
57#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
58 NFC_PROTO_MIFARE_MASK | \
59 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020060 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020061 NFC_PROTO_NFC_DEP_MASK)
62
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030063static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020064 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = PN533_VENDOR_ID,
66 .idProduct = PN533_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SCM_VENDOR_ID,
71 .idProduct = SCL3711_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_STD,
73 },
74 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
75 .idVendor = SONY_VENDOR_ID,
76 .idProduct = PASORI_PRODUCT_ID,
77 .driver_info = PN533_DEVICE_PASORI,
78 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030079 { }
80};
81MODULE_DEVICE_TABLE(usb, pn533_table);
82
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020083/* How much time we spend listening for initiators */
84#define PN533_LISTEN_TIME 2
85
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030086/* frame definitions */
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020087#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
88 254 (DATA)
89 2 (DCS, postamble) */
90
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030091#define PN533_FRAME_TAIL_SIZE 2
92#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
93 PN533_FRAME_TAIL_SIZE)
94#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
95#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
96#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
97
98/* start of frame */
99#define PN533_SOF 0x00FF
100
101/* frame identifier: in/out/error */
102#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
103#define PN533_DIR_OUT 0xD4
104#define PN533_DIR_IN 0xD5
105
106/* PN533 Commands */
107#define PN533_FRAME_CMD(f) (f->data[1])
108#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
109#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
110
111#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
112#define PN533_CMD_RF_CONFIGURATION 0x32
113#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200114#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300115#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
116#define PN533_CMD_IN_ATR 0x50
117#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100118#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119
Samuel Ortizad3823c2012-05-30 23:54:55 +0200120#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200121#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200122#define PN533_CMD_TG_SET_DATA 0x8e
Samuel Ortizad3823c2012-05-30 23:54:55 +0200123
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300124#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
125
126/* PN533 Return codes */
127#define PN533_CMD_RET_MASK 0x3F
128#define PN533_CMD_MI_MASK 0x40
129#define PN533_CMD_RET_SUCCESS 0x00
130
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200131/* PN533 status codes */
132#define PN533_STATUS_TARGET_RELEASED 0x29
133
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300134struct pn533;
135
136typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
137 u8 *params, int params_len);
138
139/* structs for pn533 commands */
140
141/* PN533_CMD_GET_FIRMWARE_VERSION */
142struct pn533_fw_version {
143 u8 ic;
144 u8 ver;
145 u8 rev;
146 u8 support;
147};
148
149/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200150#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300151#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200152#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200154#define PN533_CONFIG_TIMING_102 0xb
155#define PN533_CONFIG_TIMING_204 0xc
156#define PN533_CONFIG_TIMING_409 0xd
157#define PN533_CONFIG_TIMING_819 0xe
158
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300159#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
160#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
161
162struct pn533_config_max_retries {
163 u8 mx_rty_atr;
164 u8 mx_rty_psl;
165 u8 mx_rty_passive_act;
166} __packed;
167
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200168struct pn533_config_timing {
169 u8 rfu;
170 u8 atr_res_timeout;
171 u8 dep_timeout;
172} __packed;
173
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300174/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
175
176/* felica commands opcode */
177#define PN533_FELICA_OPC_SENSF_REQ 0
178#define PN533_FELICA_OPC_SENSF_RES 1
179/* felica SENSF_REQ parameters */
180#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
181#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
182#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
183#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
184
185/* type B initiator_data values */
186#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
187#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
188#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
189
190union pn533_cmd_poll_initdata {
191 struct {
192 u8 afi;
193 u8 polling_method;
194 } __packed type_b;
195 struct {
196 u8 opcode;
197 __be16 sc;
198 u8 rc;
199 u8 tsn;
200 } __packed felica;
201};
202
203/* Poll modulations */
204enum {
205 PN533_POLL_MOD_106KBPS_A,
206 PN533_POLL_MOD_212KBPS_FELICA,
207 PN533_POLL_MOD_424KBPS_FELICA,
208 PN533_POLL_MOD_106KBPS_JEWEL,
209 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200210 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300211
212 __PN533_POLL_MOD_AFTER_LAST,
213};
214#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
215
216struct pn533_poll_modulations {
217 struct {
218 u8 maxtg;
219 u8 brty;
220 union pn533_cmd_poll_initdata initiator_data;
221 } __packed data;
222 u8 len;
223};
224
225const struct pn533_poll_modulations poll_mod[] = {
226 [PN533_POLL_MOD_106KBPS_A] = {
227 .data = {
228 .maxtg = 1,
229 .brty = 0,
230 },
231 .len = 2,
232 },
233 [PN533_POLL_MOD_212KBPS_FELICA] = {
234 .data = {
235 .maxtg = 1,
236 .brty = 1,
237 .initiator_data.felica = {
238 .opcode = PN533_FELICA_OPC_SENSF_REQ,
239 .sc = PN533_FELICA_SENSF_SC_ALL,
240 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
241 .tsn = 0,
242 },
243 },
244 .len = 7,
245 },
246 [PN533_POLL_MOD_424KBPS_FELICA] = {
247 .data = {
248 .maxtg = 1,
249 .brty = 2,
250 .initiator_data.felica = {
251 .opcode = PN533_FELICA_OPC_SENSF_REQ,
252 .sc = PN533_FELICA_SENSF_SC_ALL,
253 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
254 .tsn = 0,
255 },
256 },
257 .len = 7,
258 },
259 [PN533_POLL_MOD_106KBPS_JEWEL] = {
260 .data = {
261 .maxtg = 1,
262 .brty = 4,
263 },
264 .len = 2,
265 },
266 [PN533_POLL_MOD_847KBPS_B] = {
267 .data = {
268 .maxtg = 1,
269 .brty = 8,
270 .initiator_data.type_b = {
271 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
272 .polling_method =
273 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
274 },
275 },
276 .len = 3,
277 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200278 [PN533_LISTEN_MOD] = {
279 .len = 0,
280 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300281};
282
283/* PN533_CMD_IN_ATR */
284
285struct pn533_cmd_activate_param {
286 u8 tg;
287 u8 next;
288} __packed;
289
290struct 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 +0100302/* PN533_CMD_IN_JUMP_FOR_DEP */
303struct pn533_cmd_jump_dep {
304 u8 active;
305 u8 baud;
306 u8 next;
Samuel Ortizd7f33452012-05-29 21:45:21 +0200307 u8 data[];
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100308} __packed;
309
310struct pn533_cmd_jump_dep_response {
311 u8 status;
312 u8 tg;
313 u8 nfcid3t[10];
314 u8 didt;
315 u8 bst;
316 u8 brt;
317 u8 to;
318 u8 ppt;
319 /* optional */
320 u8 gt[];
321} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300322
Samuel Ortizad3823c2012-05-30 23:54:55 +0200323
324/* PN533_TG_INIT_AS_TARGET */
325#define PN533_INIT_TARGET_PASSIVE 0x1
326#define PN533_INIT_TARGET_DEP 0x2
327
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200328#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
329#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
330#define PN533_INIT_TARGET_RESP_DEP 0x4
331
Samuel Ortizad3823c2012-05-30 23:54:55 +0200332struct pn533_cmd_init_target {
333 u8 mode;
334 u8 mifare[6];
335 u8 felica[18];
336 u8 nfcid3[10];
337 u8 gb_len;
338 u8 gb[];
339} __packed;
340
341struct pn533_cmd_init_target_response {
342 u8 mode;
343 u8 cmd[];
344} __packed;
345
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300346struct pn533 {
347 struct usb_device *udev;
348 struct usb_interface *interface;
349 struct nfc_dev *nfc_dev;
350
351 struct urb *out_urb;
352 int out_maxlen;
353 struct pn533_frame *out_frame;
354
355 struct urb *in_urb;
356 int in_maxlen;
357 struct pn533_frame *in_frame;
358
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200359 struct sk_buff_head resp_q;
360
Samuel Ortiz4849f852012-04-10 19:43:17 +0200361 struct workqueue_struct *wq;
362 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200363 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200364 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200365 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200366 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200367 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200368 struct pn533_frame *wq_in_frame;
369 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200370 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300371
372 pn533_cmd_complete_t cmd_complete;
373 void *cmd_complete_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200374 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300375 u8 cmd;
376
377 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
378 u8 poll_mod_count;
379 u8 poll_mod_curr;
380 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200381 u32 listen_protocols;
382
383 u8 *gb;
384 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300385
386 u8 tgt_available_prots;
387 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200388 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200389
390 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200391
392 struct list_head cmd_queue;
393 u8 cmd_pending;
394};
395
396struct pn533_cmd {
397 struct list_head queue;
398 struct pn533_frame *out_frame;
399 struct pn533_frame *in_frame;
400 int in_frame_len;
401 pn533_cmd_complete_t cmd_complete;
402 void *arg;
403 gfp_t flags;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300404};
405
406struct pn533_frame {
407 u8 preamble;
408 __be16 start_frame;
409 u8 datalen;
410 u8 datalen_checksum;
411 u8 data[];
412} __packed;
413
414/* The rule: value + checksum = 0 */
415static inline u8 pn533_checksum(u8 value)
416{
417 return ~value + 1;
418}
419
420/* The rule: sum(data elements) + checksum = 0 */
421static u8 pn533_data_checksum(u8 *data, int datalen)
422{
423 u8 sum = 0;
424 int i;
425
426 for (i = 0; i < datalen; i++)
427 sum += data[i];
428
429 return pn533_checksum(sum);
430}
431
432/**
433 * pn533_tx_frame_ack - create a ack frame
434 * @frame: The frame to be set as ack
435 *
436 * Ack is different type of standard frame. As a standard frame, it has
437 * preamble and start_frame. However the checksum of this frame must fail,
438 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
439 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
440 * After datalen_checksum field, the postamble is placed.
441 */
442static void pn533_tx_frame_ack(struct pn533_frame *frame)
443{
444 frame->preamble = 0;
445 frame->start_frame = cpu_to_be16(PN533_SOF);
446 frame->datalen = 0;
447 frame->datalen_checksum = 0xFF;
448 /* data[0] is used as postamble */
449 frame->data[0] = 0;
450}
451
452static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
453{
454 frame->preamble = 0;
455 frame->start_frame = cpu_to_be16(PN533_SOF);
456 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
457 PN533_FRAME_CMD(frame) = cmd;
458 frame->datalen = 2;
459}
460
461static void pn533_tx_frame_finish(struct pn533_frame *frame)
462{
463 frame->datalen_checksum = pn533_checksum(frame->datalen);
464
465 PN533_FRAME_CHECKSUM(frame) =
466 pn533_data_checksum(frame->data, frame->datalen);
467
468 PN533_FRAME_POSTAMBLE(frame) = 0;
469}
470
471static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
472{
473 u8 checksum;
474
475 if (frame->start_frame != cpu_to_be16(PN533_SOF))
476 return false;
477
478 checksum = pn533_checksum(frame->datalen);
479 if (checksum != frame->datalen_checksum)
480 return false;
481
482 checksum = pn533_data_checksum(frame->data, frame->datalen);
483 if (checksum != PN533_FRAME_CHECKSUM(frame))
484 return false;
485
486 return true;
487}
488
489static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
490{
491 if (frame->start_frame != cpu_to_be16(PN533_SOF))
492 return false;
493
494 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
495 return false;
496
497 return true;
498}
499
500static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
501{
502 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
503}
504
Samuel Ortiz4849f852012-04-10 19:43:17 +0200505
506static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300507{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200508 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200509 struct pn533_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300510 int rc;
511
Samuel Ortiz4849f852012-04-10 19:43:17 +0200512 in_frame = dev->wq_in_frame;
513
514 if (dev->wq_in_error)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300515 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
Samuel Ortiz4849f852012-04-10 19:43:17 +0200516 dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300517 else
518 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
519 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
520 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
521
522 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200523 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524}
525
526static void pn533_recv_response(struct urb *urb)
527{
528 struct pn533 *dev = urb->context;
529 struct pn533_frame *in_frame;
530
Samuel Ortiz4849f852012-04-10 19:43:17 +0200531 dev->wq_in_frame = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300532
533 switch (urb->status) {
534 case 0:
535 /* success */
536 break;
537 case -ECONNRESET:
538 case -ENOENT:
539 case -ESHUTDOWN:
540 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
541 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200542 dev->wq_in_error = urb->status;
543 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300544 default:
545 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
546 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200547 dev->wq_in_error = urb->status;
548 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300549 }
550
551 in_frame = dev->in_urb->transfer_buffer;
552
553 if (!pn533_rx_frame_is_valid(in_frame)) {
554 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200555 dev->wq_in_error = -EIO;
556 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300557 }
558
559 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
560 nfc_dev_err(&dev->interface->dev, "The received frame is not "
561 "response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200562 dev->wq_in_error = -EIO;
563 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300564 }
565
566 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200567 dev->wq_in_error = 0;
568 dev->wq_in_frame = in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300569
Samuel Ortiz4849f852012-04-10 19:43:17 +0200570sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200571 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300572}
573
574static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
575{
576 dev->in_urb->complete = pn533_recv_response;
577
578 return usb_submit_urb(dev->in_urb, flags);
579}
580
581static void pn533_recv_ack(struct urb *urb)
582{
583 struct pn533 *dev = urb->context;
584 struct pn533_frame *in_frame;
585 int rc;
586
587 switch (urb->status) {
588 case 0:
589 /* success */
590 break;
591 case -ECONNRESET:
592 case -ENOENT:
593 case -ESHUTDOWN:
594 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
595 " status: %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200596 dev->wq_in_error = urb->status;
597 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300598 default:
599 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
600 " %d", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200601 dev->wq_in_error = urb->status;
602 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300603 }
604
605 in_frame = dev->in_urb->transfer_buffer;
606
607 if (!pn533_rx_frame_is_ack(in_frame)) {
608 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200609 dev->wq_in_error = -EIO;
610 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300611 }
612
613 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
614
615 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
616 if (rc) {
617 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
618 " result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200619 dev->wq_in_error = rc;
620 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300621 }
622
623 return;
624
Samuel Ortiz4849f852012-04-10 19:43:17 +0200625sched_wq:
626 dev->wq_in_frame = NULL;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200627 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300628}
629
630static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
631{
632 dev->in_urb->complete = pn533_recv_ack;
633
634 return usb_submit_urb(dev->in_urb, flags);
635}
636
637static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
638{
639 int rc;
640
641 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
642
643 pn533_tx_frame_ack(dev->out_frame);
644
645 dev->out_urb->transfer_buffer = dev->out_frame;
646 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
647 rc = usb_submit_urb(dev->out_urb, flags);
648
649 return rc;
650}
651
652static int __pn533_send_cmd_frame_async(struct pn533 *dev,
653 struct pn533_frame *out_frame,
654 struct pn533_frame *in_frame,
655 int in_frame_len,
656 pn533_cmd_complete_t cmd_complete,
657 void *arg, gfp_t flags)
658{
659 int rc;
660
661 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
662 PN533_FRAME_CMD(out_frame));
663
664 dev->cmd = PN533_FRAME_CMD(out_frame);
665 dev->cmd_complete = cmd_complete;
666 dev->cmd_complete_arg = arg;
667
668 dev->out_urb->transfer_buffer = out_frame;
669 dev->out_urb->transfer_buffer_length =
670 PN533_FRAME_SIZE(out_frame);
671
672 dev->in_urb->transfer_buffer = in_frame;
673 dev->in_urb->transfer_buffer_length = in_frame_len;
674
675 rc = usb_submit_urb(dev->out_urb, flags);
676 if (rc)
677 return rc;
678
679 rc = pn533_submit_urb_for_ack(dev, flags);
680 if (rc)
681 goto error;
682
683 return 0;
684
685error:
686 usb_unlink_urb(dev->out_urb);
687 return rc;
688}
689
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200690static void pn533_wq_cmd(struct work_struct *work)
691{
692 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
693 struct pn533_cmd *cmd;
694
695 mutex_lock(&dev->cmd_lock);
696
697 if (list_empty(&dev->cmd_queue)) {
698 dev->cmd_pending = 0;
699 mutex_unlock(&dev->cmd_lock);
700 return;
701 }
702
703 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
704
705 mutex_unlock(&dev->cmd_lock);
706
707 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
708 cmd->in_frame_len, cmd->cmd_complete,
709 cmd->arg, cmd->flags);
710
711 list_del(&cmd->queue);
712 kfree(cmd);
713}
714
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300715static int pn533_send_cmd_frame_async(struct pn533 *dev,
716 struct pn533_frame *out_frame,
717 struct pn533_frame *in_frame,
718 int in_frame_len,
719 pn533_cmd_complete_t cmd_complete,
720 void *arg, gfp_t flags)
721{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200722 struct pn533_cmd *cmd;
Szymon Jancee5e8d82012-09-27 09:16:54 +0200723 int rc = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300724
725 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
726
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200727 mutex_lock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300728
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200729 if (!dev->cmd_pending) {
730 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
731 in_frame_len, cmd_complete,
732 arg, flags);
733 if (!rc)
734 dev->cmd_pending = 1;
735
Szymon Jancee5e8d82012-09-27 09:16:54 +0200736 goto unlock;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200737 }
738
739 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
740
741 cmd = kzalloc(sizeof(struct pn533_cmd), flags);
Szymon Jancee5e8d82012-09-27 09:16:54 +0200742 if (!cmd) {
743 rc = -ENOMEM;
744 goto unlock;
745 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200746
747 INIT_LIST_HEAD(&cmd->queue);
748 cmd->out_frame = out_frame;
749 cmd->in_frame = in_frame;
750 cmd->in_frame_len = in_frame_len;
751 cmd->cmd_complete = cmd_complete;
752 cmd->arg = arg;
753 cmd->flags = flags;
754
755 list_add_tail(&cmd->queue, &dev->cmd_queue);
756
Szymon Jancee5e8d82012-09-27 09:16:54 +0200757unlock:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200758 mutex_unlock(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300759
Szymon Jancee5e8d82012-09-27 09:16:54 +0200760 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300761}
762
763struct pn533_sync_cmd_response {
764 int rc;
765 struct completion done;
766};
767
768static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
769 u8 *params, int params_len)
770{
771 struct pn533_sync_cmd_response *arg = _arg;
772
773 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
774
775 arg->rc = 0;
776
777 if (params_len < 0) /* error */
778 arg->rc = params_len;
779
780 complete(&arg->done);
781
782 return 0;
783}
784
785static int pn533_send_cmd_frame_sync(struct pn533 *dev,
786 struct pn533_frame *out_frame,
787 struct pn533_frame *in_frame,
788 int in_frame_len)
789{
790 int rc;
791 struct pn533_sync_cmd_response arg;
792
793 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
794
795 init_completion(&arg.done);
796
797 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
798 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
799 if (rc)
800 return rc;
801
802 wait_for_completion(&arg.done);
803
804 return arg.rc;
805}
806
807static void pn533_send_complete(struct urb *urb)
808{
809 struct pn533 *dev = urb->context;
810
811 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
812
813 switch (urb->status) {
814 case 0:
815 /* success */
816 break;
817 case -ECONNRESET:
818 case -ENOENT:
819 case -ESHUTDOWN:
820 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
821 " status: %d", urb->status);
822 break;
823 default:
824 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
825 " %d", urb->status);
826 }
827}
828
829struct pn533_target_type_a {
830 __be16 sens_res;
831 u8 sel_res;
832 u8 nfcid_len;
833 u8 nfcid_data[];
834} __packed;
835
836
837#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
838#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
839#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
840
841#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
842#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
843
844#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
845#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
846
847#define PN533_TYPE_A_SEL_PROT_MIFARE 0
848#define PN533_TYPE_A_SEL_PROT_ISO14443 1
849#define PN533_TYPE_A_SEL_PROT_DEP 2
850#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
851
852static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
853 int target_data_len)
854{
855 u8 ssd;
856 u8 platconf;
857
858 if (target_data_len < sizeof(struct pn533_target_type_a))
859 return false;
860
861 /* The lenght check of nfcid[] and ats[] are not being performed because
862 the values are not being used */
863
864 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
865 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
866 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
867
868 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
869 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
870 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
871 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
872 return false;
873
874 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
875 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
876 return false;
877
878 return true;
879}
880
881static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
882 int tgt_data_len)
883{
884 struct pn533_target_type_a *tgt_type_a;
885
886 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
887
888 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
889 return -EPROTO;
890
891 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
892 case PN533_TYPE_A_SEL_PROT_MIFARE:
893 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
894 break;
895 case PN533_TYPE_A_SEL_PROT_ISO14443:
896 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
897 break;
898 case PN533_TYPE_A_SEL_PROT_DEP:
899 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
900 break;
901 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
902 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
903 NFC_PROTO_NFC_DEP_MASK;
904 break;
905 }
906
907 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
908 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +0100909 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
910 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300911
912 return 0;
913}
914
915struct pn533_target_felica {
916 u8 pol_res;
917 u8 opcode;
918 u8 nfcid2[8];
919 u8 pad[8];
920 /* optional */
921 u8 syst_code[];
922} __packed;
923
924#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
925#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
926
927static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
928 int target_data_len)
929{
930 if (target_data_len < sizeof(struct pn533_target_felica))
931 return false;
932
933 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
934 return false;
935
936 return true;
937}
938
939static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
940 int tgt_data_len)
941{
942 struct pn533_target_felica *tgt_felica;
943
944 tgt_felica = (struct pn533_target_felica *) tgt_data;
945
946 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
947 return -EPROTO;
948
949 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
950 tgt_felica->nfcid2[1] ==
951 PN533_FELICA_SENSF_NFCID2_DEP_B2)
952 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
953 else
954 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
955
Samuel Ortiz79757542012-03-05 01:03:45 +0100956 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
957 nfc_tgt->sensf_res_len = 9;
958
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300959 return 0;
960}
961
962struct pn533_target_jewel {
963 __be16 sens_res;
964 u8 jewelid[4];
965} __packed;
966
967static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
968 int target_data_len)
969{
970 u8 ssd;
971 u8 platconf;
972
973 if (target_data_len < sizeof(struct pn533_target_jewel))
974 return false;
975
976 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
977 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
978 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
979
980 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
981 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
982 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
983 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
984 return false;
985
986 return true;
987}
988
989static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
990 int tgt_data_len)
991{
992 struct pn533_target_jewel *tgt_jewel;
993
994 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
995
996 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
997 return -EPROTO;
998
999 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1000 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001001 nfc_tgt->nfcid1_len = 4;
1002 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001003
1004 return 0;
1005}
1006
1007struct pn533_type_b_prot_info {
1008 u8 bitrate;
1009 u8 fsci_type;
1010 u8 fwi_adc_fo;
1011} __packed;
1012
1013#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1014#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1015#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1016
1017struct pn533_type_b_sens_res {
1018 u8 opcode;
1019 u8 nfcid[4];
1020 u8 appdata[4];
1021 struct pn533_type_b_prot_info prot_info;
1022} __packed;
1023
1024#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1025
1026struct pn533_target_type_b {
1027 struct pn533_type_b_sens_res sensb_res;
1028 u8 attrib_res_len;
1029 u8 attrib_res[];
1030} __packed;
1031
1032static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1033 int target_data_len)
1034{
1035 if (target_data_len < sizeof(struct pn533_target_type_b))
1036 return false;
1037
1038 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1039 return false;
1040
1041 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1042 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1043 return false;
1044
1045 return true;
1046}
1047
1048static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1049 int tgt_data_len)
1050{
1051 struct pn533_target_type_b *tgt_type_b;
1052
1053 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1054
1055 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1056 return -EPROTO;
1057
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001058 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001059
1060 return 0;
1061}
1062
1063struct pn533_poll_response {
1064 u8 nbtg;
1065 u8 tg;
1066 u8 target_data[];
1067} __packed;
1068
1069static int pn533_target_found(struct pn533 *dev,
1070 struct pn533_poll_response *resp, int resp_len)
1071{
1072 int target_data_len;
1073 struct nfc_target nfc_tgt;
1074 int rc;
1075
1076 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1077 dev->poll_mod_curr);
1078
1079 if (resp->tg != 1)
1080 return -EPROTO;
1081
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001082 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1083
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001084 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1085
1086 switch (dev->poll_mod_curr) {
1087 case PN533_POLL_MOD_106KBPS_A:
1088 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1089 target_data_len);
1090 break;
1091 case PN533_POLL_MOD_212KBPS_FELICA:
1092 case PN533_POLL_MOD_424KBPS_FELICA:
1093 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1094 target_data_len);
1095 break;
1096 case PN533_POLL_MOD_106KBPS_JEWEL:
1097 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1098 target_data_len);
1099 break;
1100 case PN533_POLL_MOD_847KBPS_B:
1101 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1102 target_data_len);
1103 break;
1104 default:
1105 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1106 " modulation");
1107 return -EPROTO;
1108 }
1109
1110 if (rc)
1111 return rc;
1112
1113 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1114 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1115 " have the desired protocol");
1116 return -EAGAIN;
1117 }
1118
1119 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1120 "0x%x", nfc_tgt.supported_protocols);
1121
1122 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1123
1124 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1125
1126 return 0;
1127}
1128
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001129static inline void pn533_poll_next_mod(struct pn533 *dev)
1130{
1131 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1132}
1133
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001134static void pn533_poll_reset_mod_list(struct pn533 *dev)
1135{
1136 dev->poll_mod_count = 0;
1137}
1138
1139static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1140{
1141 dev->poll_mod_active[dev->poll_mod_count] =
1142 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1143 dev->poll_mod_count++;
1144}
1145
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001146static void pn533_poll_create_mod_list(struct pn533 *dev,
1147 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001148{
1149 pn533_poll_reset_mod_list(dev);
1150
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001151 if (im_protocols & NFC_PROTO_MIFARE_MASK
1152 || im_protocols & NFC_PROTO_ISO14443_MASK
1153 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001154 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1155
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001156 if (im_protocols & NFC_PROTO_FELICA_MASK
1157 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001158 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1159 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1160 }
1161
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001162 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001163 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1164
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001165 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001166 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001167
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001168 if (tm_protocols)
1169 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001170}
1171
1172static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001173 u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001174{
1175 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001176 int rc;
1177
1178 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1179
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001180 resp = (struct pn533_poll_response *) params;
1181 if (resp->nbtg) {
1182 rc = pn533_target_found(dev, resp, params_len);
1183
1184 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001185 if (rc == 0) {
1186 pn533_poll_reset_mod_list(dev);
1187 return 0;
1188 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001189 }
1190
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001191 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001192}
1193
Samuel Ortizad3823c2012-05-30 23:54:55 +02001194static int pn533_init_target_frame(struct pn533_frame *frame,
1195 u8 *gb, size_t gb_len)
1196{
1197 struct pn533_cmd_init_target *cmd;
1198 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001199 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1200 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1201 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1202 0xff, 0xff}; /* System code */
1203 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1204 0x0, 0x0, 0x0,
1205 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001206
1207 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1208 cmd = kzalloc(cmd_len, GFP_KERNEL);
1209 if (cmd == NULL)
1210 return -ENOMEM;
1211
1212 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1213
1214 /* DEP support only */
1215 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001216
1217 /* Felica params */
1218 memcpy(cmd->felica, felica_params, 18);
1219 get_random_bytes(cmd->felica + 2, 6);
1220
1221 /* NFCID3 */
1222 memset(cmd->nfcid3, 0, 10);
1223 memcpy(cmd->nfcid3, cmd->felica, 8);
1224
1225 /* MIFARE params */
1226 memcpy(cmd->mifare, mifare_params, 6);
1227
1228 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001229 cmd->gb_len = gb_len;
1230 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001231
Samuel Ortizad3823c2012-05-30 23:54:55 +02001232 /* Len Tk */
1233 cmd->gb[gb_len] = 0;
1234
1235 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001236
Samuel Ortizad3823c2012-05-30 23:54:55 +02001237 frame->datalen += cmd_len;
1238
1239 pn533_tx_frame_finish(frame);
1240
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001241 kfree(cmd);
1242
Samuel Ortizad3823c2012-05-30 23:54:55 +02001243 return 0;
1244}
1245
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001246#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1247#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1248static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1249 u8 *params, int params_len)
1250{
1251 struct sk_buff *skb_resp = arg;
1252 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1253
1254 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1255
1256 if (params_len < 0) {
1257 nfc_dev_err(&dev->interface->dev,
1258 "Error %d when starting as a target",
1259 params_len);
1260
1261 return params_len;
1262 }
1263
1264 if (params_len > 0 && params[0] != 0) {
1265 nfc_tm_deactivated(dev->nfc_dev);
1266
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001267 dev->tgt_mode = 0;
1268
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001269 kfree_skb(skb_resp);
1270 return 0;
1271 }
1272
1273 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1274 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1275 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1276
1277 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1278}
1279
1280static void pn533_wq_tg_get_data(struct work_struct *work)
1281{
1282 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1283 struct pn533_frame *in_frame;
1284 struct sk_buff *skb_resp;
1285 size_t skb_resp_len;
1286
1287 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1288
1289 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1290 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1291 PN533_FRAME_TAIL_SIZE;
1292
1293 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1294 if (!skb_resp)
1295 return;
1296
1297 in_frame = (struct pn533_frame *)skb_resp->data;
1298
1299 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1300 pn533_tx_frame_finish(dev->out_frame);
1301
1302 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1303 skb_resp_len,
1304 pn533_tm_get_data_complete,
1305 skb_resp, GFP_KERNEL);
1306
1307 return;
1308}
1309
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001310#define ATR_REQ_GB_OFFSET 17
Samuel Ortizad3823c2012-05-30 23:54:55 +02001311static int pn533_init_target_complete(struct pn533 *dev, void *arg,
1312 u8 *params, int params_len)
1313{
1314 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001315 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1316 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001317 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001318
1319 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1320
1321 if (params_len < 0) {
1322 nfc_dev_err(&dev->interface->dev,
1323 "Error %d when starting as a target",
1324 params_len);
1325
1326 return params_len;
1327 }
1328
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001329 if (params_len < ATR_REQ_GB_OFFSET + 1)
1330 return -EINVAL;
1331
Samuel Ortizad3823c2012-05-30 23:54:55 +02001332 resp = (struct pn533_cmd_init_target_response *) params;
1333
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001334 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1335 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001336
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001337 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1338 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1339 comm_mode = NFC_COMM_ACTIVE;
1340
1341 /* Again, only DEP */
1342 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1343 return -EOPNOTSUPP;
1344
1345 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1346 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1347
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001348 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1349 comm_mode, gb, gb_len);
1350 if (rc < 0) {
1351 nfc_dev_err(&dev->interface->dev,
1352 "Error when signaling target activation");
1353 return rc;
1354 }
1355
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001356 dev->tgt_mode = 1;
1357
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001358 queue_work(dev->wq, &dev->tg_work);
1359
1360 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001361}
1362
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001363static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001364{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001365 struct pn533 *dev = (struct pn533 *) data;
1366
1367 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1368
1369 /* An ack will cancel the last issued command (poll) */
1370 pn533_send_ack(dev, GFP_ATOMIC);
1371
1372 dev->cancel_listen = 1;
1373
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001374 pn533_poll_next_mod(dev);
1375
1376 queue_work(dev->wq, &dev->poll_work);
1377}
1378
1379static int pn533_poll_complete(struct pn533 *dev, void *arg,
1380 u8 *params, int params_len)
1381{
1382 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001383 int rc;
1384
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001385 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1386
1387 if (params_len == -ENOENT) {
1388 if (dev->poll_mod_count != 0)
1389 return 0;
1390
1391 nfc_dev_err(&dev->interface->dev,
1392 "Polling operation has been stopped");
1393
1394 goto stop_poll;
1395 }
1396
1397 if (params_len < 0) {
1398 nfc_dev_err(&dev->interface->dev,
1399 "Error %d when running poll", params_len);
1400
1401 goto stop_poll;
1402 }
1403
1404 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1405
1406 if (cur_mod->len == 0) {
1407 del_timer(&dev->listen_timer);
1408
1409 return pn533_init_target_complete(dev, arg, params, params_len);
1410 } else {
1411 rc = pn533_start_poll_complete(dev, arg, params, params_len);
1412 if (!rc)
1413 return rc;
1414 }
1415
1416 pn533_poll_next_mod(dev);
1417
1418 queue_work(dev->wq, &dev->poll_work);
1419
1420 return 0;
1421
1422stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001423 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001424 dev->poll_protocols = 0;
1425 return 0;
1426}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001427
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001428static void pn533_build_poll_frame(struct pn533 *dev,
1429 struct pn533_frame *frame,
1430 struct pn533_poll_modulations *mod)
1431{
1432 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001433
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001434 if (mod->len == 0) {
1435 /* Listen mode */
1436 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1437 } else {
1438 /* Polling mode */
1439 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1440
1441 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1442 frame->datalen += mod->len;
1443
1444 pn533_tx_frame_finish(frame);
1445 }
1446}
1447
1448static int pn533_send_poll_frame(struct pn533 *dev)
1449{
1450 struct pn533_poll_modulations *cur_mod;
1451 int rc;
1452
1453 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1454
1455 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001456
1457 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001458 dev->in_maxlen, pn533_poll_complete,
1459 NULL, GFP_KERNEL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001460 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001461 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001462
1463 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001464}
1465
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001466static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001467{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001468 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1469 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001470 int rc;
1471
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001472 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1473
1474 nfc_dev_dbg(&dev->interface->dev,
1475 "%s cancel_listen %d modulation len %d",
1476 __func__, dev->cancel_listen, cur_mod->len);
1477
1478 if (dev->cancel_listen == 1) {
1479 dev->cancel_listen = 0;
1480 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001481 }
1482
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001483 rc = pn533_send_poll_frame(dev);
1484 if (rc)
1485 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001486
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001487 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1488 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001489
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001490 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001491}
1492
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001493static int pn533_start_poll(struct nfc_dev *nfc_dev,
1494 u32 im_protocols, u32 tm_protocols)
1495{
1496 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1497
1498 nfc_dev_dbg(&dev->interface->dev,
1499 "%s: im protocols 0x%x tm protocols 0x%x",
1500 __func__, im_protocols, tm_protocols);
1501
1502 if (dev->tgt_active_prot) {
1503 nfc_dev_err(&dev->interface->dev,
1504 "Cannot poll with a target already activated");
1505 return -EBUSY;
1506 }
1507
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001508 if (dev->tgt_mode) {
1509 nfc_dev_err(&dev->interface->dev,
1510 "Cannot poll while already being activated");
1511 return -EBUSY;
1512 }
1513
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001514 if (tm_protocols) {
1515 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1516 if (dev->gb == NULL)
1517 tm_protocols = 0;
1518 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001519
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001520 dev->poll_mod_curr = 0;
1521 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1522 dev->poll_protocols = im_protocols;
1523 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001524
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001525 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001526}
1527
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001528static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1529{
1530 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1531
1532 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1533
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001534 del_timer(&dev->listen_timer);
1535
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001536 if (!dev->poll_mod_count) {
1537 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1538 " running");
1539 return;
1540 }
1541
1542 /* An ack will cancel the last issued command (poll) */
1543 pn533_send_ack(dev, GFP_KERNEL);
1544
1545 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1546 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001547
1548 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001549}
1550
1551static int pn533_activate_target_nfcdep(struct pn533 *dev)
1552{
1553 struct pn533_cmd_activate_param param;
1554 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001555 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001556 int rc;
1557
1558 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1559
1560 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1561
1562 param.tg = 1;
1563 param.next = 0;
1564 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1565 sizeof(struct pn533_cmd_activate_param));
1566 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1567
1568 pn533_tx_frame_finish(dev->out_frame);
1569
1570 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1571 dev->in_maxlen);
1572 if (rc)
1573 return rc;
1574
1575 resp = (struct pn533_cmd_activate_response *)
1576 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1577 rc = resp->status & PN533_CMD_RET_MASK;
1578 if (rc != PN533_CMD_RET_SUCCESS)
1579 return -EIO;
1580
Samuel Ortiz541d9202011-12-14 16:43:10 +01001581 /* ATR_RES general bytes are located at offset 16 */
1582 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1583 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1584
1585 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001586}
1587
Eric Lapuyade90099432012-05-07 12:31:13 +02001588static int pn533_activate_target(struct nfc_dev *nfc_dev,
1589 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001590{
1591 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1592 int rc;
1593
1594 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1595 protocol);
1596
1597 if (dev->poll_mod_count) {
1598 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1599 " polling");
1600 return -EBUSY;
1601 }
1602
1603 if (dev->tgt_active_prot) {
1604 nfc_dev_err(&dev->interface->dev, "There is already an active"
1605 " target");
1606 return -EBUSY;
1607 }
1608
1609 if (!dev->tgt_available_prots) {
1610 nfc_dev_err(&dev->interface->dev, "There is no available target"
1611 " to activate");
1612 return -EINVAL;
1613 }
1614
1615 if (!(dev->tgt_available_prots & (1 << protocol))) {
1616 nfc_dev_err(&dev->interface->dev, "The target does not support"
1617 " the requested protocol %u", protocol);
1618 return -EINVAL;
1619 }
1620
1621 if (protocol == NFC_PROTO_NFC_DEP) {
1622 rc = pn533_activate_target_nfcdep(dev);
1623 if (rc) {
1624 nfc_dev_err(&dev->interface->dev, "Error %d when"
1625 " activating target with"
1626 " NFC_DEP protocol", rc);
1627 return rc;
1628 }
1629 }
1630
1631 dev->tgt_active_prot = protocol;
1632 dev->tgt_available_prots = 0;
1633
1634 return 0;
1635}
1636
Eric Lapuyade90099432012-05-07 12:31:13 +02001637static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1638 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001639{
1640 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1641 u8 tg;
1642 u8 status;
1643 int rc;
1644
1645 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1646
1647 if (!dev->tgt_active_prot) {
1648 nfc_dev_err(&dev->interface->dev, "There is no active target");
1649 return;
1650 }
1651
1652 dev->tgt_active_prot = 0;
1653
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001654 skb_queue_purge(&dev->resp_q);
1655
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001656 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1657
1658 tg = 1;
1659 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1660 dev->out_frame->datalen += sizeof(u8);
1661
1662 pn533_tx_frame_finish(dev->out_frame);
1663
1664 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1665 dev->in_maxlen);
1666 if (rc) {
1667 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1668 " command to the controller");
1669 return;
1670 }
1671
1672 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1673 rc = status & PN533_CMD_RET_MASK;
1674 if (rc != PN533_CMD_RET_SUCCESS)
1675 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1676 " the target", rc);
1677
1678 return;
1679}
1680
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001681
1682static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1683 u8 *params, int params_len)
1684{
1685 struct pn533_cmd_jump_dep *cmd;
1686 struct pn533_cmd_jump_dep_response *resp;
1687 struct nfc_target nfc_target;
1688 u8 target_gt_len;
1689 int rc;
1690
1691 if (params_len == -ENOENT) {
1692 nfc_dev_dbg(&dev->interface->dev, "");
1693 return 0;
1694 }
1695
1696 if (params_len < 0) {
1697 nfc_dev_err(&dev->interface->dev,
1698 "Error %d when bringing DEP link up",
1699 params_len);
1700 return 0;
1701 }
1702
1703 if (dev->tgt_available_prots &&
1704 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1705 nfc_dev_err(&dev->interface->dev,
1706 "The target does not support DEP");
1707 return -EINVAL;
1708 }
1709
1710 resp = (struct pn533_cmd_jump_dep_response *) params;
1711 cmd = (struct pn533_cmd_jump_dep *) arg;
1712 rc = resp->status & PN533_CMD_RET_MASK;
1713 if (rc != PN533_CMD_RET_SUCCESS) {
1714 nfc_dev_err(&dev->interface->dev,
1715 "Bringing DEP link up failed %d", rc);
1716 return 0;
1717 }
1718
1719 if (!dev->tgt_available_prots) {
1720 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1721
1722 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001723 nfc_target.nfcid1_len = 10;
1724 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001725 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1726 if (rc)
1727 return 0;
1728
1729 dev->tgt_available_prots = 0;
1730 }
1731
1732 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1733
1734 /* ATR_RES general bytes are located at offset 17 */
1735 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1736 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1737 resp->gt, target_gt_len);
1738 if (rc == 0)
1739 rc = nfc_dep_link_is_up(dev->nfc_dev,
1740 dev->nfc_dev->targets[0].idx,
1741 !cmd->active, NFC_RF_INITIATOR);
1742
1743 return 0;
1744}
1745
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001746static int pn533_mod_to_baud(struct pn533 *dev)
1747{
1748 switch (dev->poll_mod_curr) {
1749 case PN533_POLL_MOD_106KBPS_A:
1750 return 0;
1751 case PN533_POLL_MOD_212KBPS_FELICA:
1752 return 1;
1753 case PN533_POLL_MOD_424KBPS_FELICA:
1754 return 2;
1755 default:
1756 return -EINVAL;
1757 }
1758}
1759
Samuel Ortizd7f33452012-05-29 21:45:21 +02001760#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001761static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001762 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001763{
1764 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1765 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001766 u8 cmd_len, *data_ptr;
1767 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001768 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001769
1770 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1771
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001772 if (dev->poll_mod_count) {
1773 nfc_dev_err(&dev->interface->dev,
1774 "Cannot bring the DEP link up while polling");
1775 return -EBUSY;
1776 }
1777
1778 if (dev->tgt_active_prot) {
1779 nfc_dev_err(&dev->interface->dev,
1780 "There is already an active target");
1781 return -EBUSY;
1782 }
1783
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001784 baud = pn533_mod_to_baud(dev);
1785 if (baud < 0) {
1786 nfc_dev_err(&dev->interface->dev,
1787 "Invalid curr modulation %d", dev->poll_mod_curr);
1788 return baud;
1789 }
1790
Samuel Ortiz47807d32012-03-05 01:03:50 +01001791 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001792 if (comm_mode == NFC_COMM_PASSIVE)
1793 cmd_len += PASSIVE_DATA_LEN;
1794
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001795 cmd = kzalloc(cmd_len, GFP_KERNEL);
1796 if (cmd == NULL)
1797 return -ENOMEM;
1798
1799 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1800
1801 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001802 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001803 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001804 data_ptr = cmd->data;
1805 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
1806 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
1807 cmd->next |= 1;
1808 data_ptr += PASSIVE_DATA_LEN;
1809 }
1810
Samuel Ortiz47807d32012-03-05 01:03:50 +01001811 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02001812 cmd->next |= 4; /* We have some Gi */
1813 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001814 } else {
1815 cmd->next = 0;
1816 }
1817
1818 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1819 dev->out_frame->datalen += cmd_len;
1820
1821 pn533_tx_frame_finish(dev->out_frame);
1822
1823 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1824 dev->in_maxlen, pn533_in_dep_link_up_complete,
1825 cmd, GFP_KERNEL);
1826 if (rc)
1827 goto out;
1828
1829
1830out:
1831 kfree(cmd);
1832
1833 return rc;
1834}
1835
1836static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1837{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001838 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1839
1840 pn533_poll_reset_mod_list(dev);
1841
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001842 if (dev->tgt_mode || dev->tgt_active_prot) {
1843 pn533_send_ack(dev, GFP_KERNEL);
1844 usb_kill_urb(dev->in_urb);
1845 }
1846
1847 dev->tgt_active_prot = 0;
1848 dev->tgt_mode = 0;
1849
1850 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001851
1852 return 0;
1853}
1854
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001855static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
1856 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001857{
1858 int payload_len = skb->len;
1859 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001860 u8 tg;
1861
1862 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1863 payload_len);
1864
1865 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1866 /* TODO: Implement support to multi-part data exchange */
1867 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1868 " max allowed: %d",
1869 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1870 return -ENOSYS;
1871 }
1872
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001873 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001874 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02001875 case PN533_DEVICE_PASORI:
1876 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
1877 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1878 out_frame = (struct pn533_frame *) skb->data;
1879 pn533_tx_frame_init(out_frame,
1880 PN533_CMD_IN_COMM_THRU);
1881
1882 break;
1883 }
1884
1885 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001886 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1887 out_frame = (struct pn533_frame *) skb->data;
1888 pn533_tx_frame_init(out_frame,
1889 PN533_CMD_IN_DATA_EXCHANGE);
1890 tg = 1;
1891 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
1892 &tg, sizeof(u8));
1893 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001894
1895 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001896 }
1897
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001898 } else {
1899 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1900 out_frame = (struct pn533_frame *) skb->data;
1901 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
1902 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001903
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001904
1905 /* The data is already in the out_frame, just update the datalen */
1906 out_frame->datalen += payload_len;
1907
1908 pn533_tx_frame_finish(out_frame);
1909 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1910
1911 return 0;
1912}
1913
1914struct pn533_data_exchange_arg {
1915 struct sk_buff *skb_resp;
1916 struct sk_buff *skb_out;
1917 data_exchange_cb_t cb;
1918 void *cb_context;
1919};
1920
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001921static struct sk_buff *pn533_build_response(struct pn533 *dev)
1922{
1923 struct sk_buff *skb, *tmp, *t;
1924 unsigned int skb_len = 0, tmp_len = 0;
1925
1926 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1927
1928 if (skb_queue_empty(&dev->resp_q))
1929 return NULL;
1930
1931 if (skb_queue_len(&dev->resp_q) == 1) {
1932 skb = skb_dequeue(&dev->resp_q);
1933 goto out;
1934 }
1935
1936 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1937 skb_len += tmp->len;
1938
1939 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1940 __func__, skb_len);
1941
1942 skb = alloc_skb(skb_len, GFP_KERNEL);
1943 if (skb == NULL)
1944 goto out;
1945
1946 skb_put(skb, skb_len);
1947
1948 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1949 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1950 tmp_len += tmp->len;
1951 }
1952
1953out:
1954 skb_queue_purge(&dev->resp_q);
1955
1956 return skb;
1957}
1958
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001959static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1960 u8 *params, int params_len)
1961{
1962 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001963 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001964 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1965 int err = 0;
1966 u8 status;
1967 u8 cmd_ret;
1968
1969 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1970
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001971 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001972
1973 if (params_len < 0) { /* error */
1974 err = params_len;
1975 goto error;
1976 }
1977
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001978 status = params[0];
1979
1980 cmd_ret = status & PN533_CMD_RET_MASK;
1981 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1982 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1983 " exchanging data", cmd_ret);
1984 err = -EIO;
1985 goto error;
1986 }
1987
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001988 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001989 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1990 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001991 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001992
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001993 if (status & PN533_CMD_MI_MASK) {
1994 queue_work(dev->wq, &dev->mi_work);
1995 return -EINPROGRESS;
1996 }
1997
1998 skb = pn533_build_response(dev);
1999 if (skb == NULL)
2000 goto error;
2001
2002 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002003 kfree(arg);
2004 return 0;
2005
2006error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002007 skb_queue_purge(&dev->resp_q);
2008 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002009 arg->cb(arg->cb_context, NULL, err);
2010 kfree(arg);
2011 return 0;
2012}
2013
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002014static int pn533_transceive(struct nfc_dev *nfc_dev,
2015 struct nfc_target *target, struct sk_buff *skb,
2016 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002017{
2018 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2019 struct pn533_frame *out_frame, *in_frame;
2020 struct pn533_data_exchange_arg *arg;
2021 struct sk_buff *skb_resp;
2022 int skb_resp_len;
2023 int rc;
2024
2025 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2026
2027 if (!dev->tgt_active_prot) {
2028 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2029 " there is no active target");
2030 rc = -EINVAL;
2031 goto error;
2032 }
2033
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002034 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002035 if (rc)
2036 goto error;
2037
2038 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
2039 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2040 PN533_FRAME_TAIL_SIZE;
2041
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002042 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002043 if (!skb_resp) {
2044 rc = -ENOMEM;
2045 goto error;
2046 }
2047
2048 in_frame = (struct pn533_frame *) skb_resp->data;
2049 out_frame = (struct pn533_frame *) skb->data;
2050
2051 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2052 if (!arg) {
2053 rc = -ENOMEM;
2054 goto free_skb_resp;
2055 }
2056
2057 arg->skb_resp = skb_resp;
2058 arg->skb_out = skb;
2059 arg->cb = cb;
2060 arg->cb_context = cb_context;
2061
2062 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2063 pn533_data_exchange_complete, arg,
2064 GFP_KERNEL);
2065 if (rc) {
2066 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2067 " perform data_exchange", rc);
2068 goto free_arg;
2069 }
2070
2071 return 0;
2072
2073free_arg:
2074 kfree(arg);
2075free_skb_resp:
2076 kfree_skb(skb_resp);
2077error:
2078 kfree_skb(skb);
2079 return rc;
2080}
2081
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002082static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2083 u8 *params, int params_len)
2084{
2085 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2086
2087 if (params_len < 0) {
2088 nfc_dev_err(&dev->interface->dev,
2089 "Error %d when sending data",
2090 params_len);
2091
2092 return params_len;
2093 }
2094
2095 if (params_len > 0 && params[0] != 0) {
2096 nfc_tm_deactivated(dev->nfc_dev);
2097
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002098 dev->tgt_mode = 0;
2099
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002100 return 0;
2101 }
2102
2103 queue_work(dev->wq, &dev->tg_work);
2104
2105 return 0;
2106}
2107
2108static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2109{
2110 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2111 struct pn533_frame *out_frame;
2112 int rc;
2113
2114 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2115
2116 rc = pn533_build_tx_frame(dev, skb, false);
2117 if (rc)
2118 goto error;
2119
2120 out_frame = (struct pn533_frame *) skb->data;
2121
2122 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2123 dev->in_maxlen, pn533_tm_send_complete,
2124 NULL, GFP_KERNEL);
2125 if (rc) {
2126 nfc_dev_err(&dev->interface->dev,
2127 "Error %d when trying to send data", rc);
2128 goto error;
2129 }
2130
2131 return 0;
2132
2133error:
2134 kfree_skb(skb);
2135
2136 return rc;
2137}
2138
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002139static void pn533_wq_mi_recv(struct work_struct *work)
2140{
2141 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2142 struct sk_buff *skb_cmd;
2143 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2144 struct pn533_frame *out_frame, *in_frame;
2145 struct sk_buff *skb_resp;
2146 int skb_resp_len;
2147 int rc;
2148
2149 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2150
2151 /* This is a zero payload size skb */
2152 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
2153 GFP_KERNEL);
2154 if (skb_cmd == NULL)
2155 goto error_cmd;
2156
2157 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2158
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002159 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002160 if (rc)
2161 goto error_frame;
2162
2163 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
2164 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2165 PN533_FRAME_TAIL_SIZE;
2166 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2167 if (!skb_resp) {
2168 rc = -ENOMEM;
2169 goto error_frame;
2170 }
2171
2172 in_frame = (struct pn533_frame *) skb_resp->data;
2173 out_frame = (struct pn533_frame *) skb_cmd->data;
2174
2175 arg->skb_resp = skb_resp;
2176 arg->skb_out = skb_cmd;
2177
2178 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2179 skb_resp_len,
2180 pn533_data_exchange_complete,
2181 dev->cmd_complete_arg, GFP_KERNEL);
2182 if (!rc)
2183 return;
2184
2185 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2186 " perform data_exchange", rc);
2187
2188 kfree_skb(skb_resp);
2189
2190error_frame:
2191 kfree_skb(skb_cmd);
2192
2193error_cmd:
2194 pn533_send_ack(dev, GFP_KERNEL);
2195
2196 kfree(arg);
2197
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002198 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002199}
2200
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002201static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2202 u8 cfgdata_len)
2203{
2204 int rc;
2205 u8 *params;
2206
2207 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2208
2209 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2210
2211 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2212 params[0] = cfgitem;
2213 memcpy(&params[1], cfgdata, cfgdata_len);
2214 dev->out_frame->datalen += (1 + cfgdata_len);
2215
2216 pn533_tx_frame_finish(dev->out_frame);
2217
2218 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2219 dev->in_maxlen);
2220
2221 return rc;
2222}
2223
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002224static int pn533_fw_reset(struct pn533 *dev)
2225{
2226 int rc;
2227 u8 *params;
2228
2229 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2230
2231 pn533_tx_frame_init(dev->out_frame, 0x18);
2232
2233 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2234 params[0] = 0x1;
2235 dev->out_frame->datalen += 1;
2236
2237 pn533_tx_frame_finish(dev->out_frame);
2238
2239 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2240 dev->in_maxlen);
2241
2242 return rc;
2243}
2244
2245static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002246 .dev_up = NULL,
2247 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002248 .dep_link_up = pn533_dep_link_up,
2249 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002250 .start_poll = pn533_start_poll,
2251 .stop_poll = pn533_stop_poll,
2252 .activate_target = pn533_activate_target,
2253 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002254 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002255 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002256};
2257
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002258static int pn533_setup(struct pn533 *dev)
2259{
2260 struct pn533_config_max_retries max_retries;
2261 struct pn533_config_timing timing;
2262 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2263 int rc;
2264
2265 switch (dev->device_type) {
2266 case PN533_DEVICE_STD:
2267 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2268 max_retries.mx_rty_psl = 2;
2269 max_retries.mx_rty_passive_act =
2270 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2271
2272 timing.rfu = PN533_CONFIG_TIMING_102;
2273 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2274 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2275
2276 break;
2277
2278 case PN533_DEVICE_PASORI:
2279 max_retries.mx_rty_atr = 0x2;
2280 max_retries.mx_rty_psl = 0x1;
2281 max_retries.mx_rty_passive_act =
2282 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2283
2284 timing.rfu = PN533_CONFIG_TIMING_102;
2285 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2286 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2287
2288 break;
2289
2290 default:
2291 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2292 dev->device_type);
2293 return -EINVAL;
2294 }
2295
2296 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2297 (u8 *)&max_retries, sizeof(max_retries));
2298 if (rc) {
2299 nfc_dev_err(&dev->interface->dev,
2300 "Error on setting MAX_RETRIES config");
2301 return rc;
2302 }
2303
2304
2305 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2306 (u8 *)&timing, sizeof(timing));
2307 if (rc) {
2308 nfc_dev_err(&dev->interface->dev,
2309 "Error on setting RF timings");
2310 return rc;
2311 }
2312
2313 switch (dev->device_type) {
2314 case PN533_DEVICE_STD:
2315 break;
2316
2317 case PN533_DEVICE_PASORI:
2318 pn533_fw_reset(dev);
2319
2320 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2321 pasori_cfg, 3);
2322 if (rc) {
2323 nfc_dev_err(&dev->interface->dev,
2324 "Error while settings PASORI config");
2325 return rc;
2326 }
2327
2328 pn533_fw_reset(dev);
2329
2330 break;
2331 }
2332
2333 return 0;
2334}
2335
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002336static int pn533_probe(struct usb_interface *interface,
2337 const struct usb_device_id *id)
2338{
2339 struct pn533_fw_version *fw_ver;
2340 struct pn533 *dev;
2341 struct usb_host_interface *iface_desc;
2342 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002343 int in_endpoint = 0;
2344 int out_endpoint = 0;
2345 int rc = -ENOMEM;
2346 int i;
2347 u32 protocols;
2348
2349 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2350 if (!dev)
2351 return -ENOMEM;
2352
2353 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2354 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002355 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002356
2357 iface_desc = interface->cur_altsetting;
2358 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2359 endpoint = &iface_desc->endpoint[i].desc;
2360
2361 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2362 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
2363 in_endpoint = endpoint->bEndpointAddress;
2364 }
2365
2366 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
2367 dev->out_maxlen =
2368 le16_to_cpu(endpoint->wMaxPacketSize);
2369 out_endpoint = endpoint->bEndpointAddress;
2370 }
2371 }
2372
2373 if (!in_endpoint || !out_endpoint) {
2374 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2375 " bulk-out endpoint");
2376 rc = -ENODEV;
2377 goto error;
2378 }
2379
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002380 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002381 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002382 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002383 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2384
2385 if (!dev->in_frame || !dev->out_frame ||
2386 !dev->in_urb || !dev->out_urb)
2387 goto error;
2388
2389 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2390 usb_rcvbulkpipe(dev->udev, in_endpoint),
2391 NULL, 0, NULL, dev);
2392 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2393 usb_sndbulkpipe(dev->udev, out_endpoint),
2394 NULL, 0,
2395 pn533_send_complete, dev);
2396
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002397 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2398 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002399 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002400 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002401 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002402 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002403 if (dev->wq == NULL)
2404 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002405
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002406 init_timer(&dev->listen_timer);
2407 dev->listen_timer.data = (unsigned long) dev;
2408 dev->listen_timer.function = pn533_listen_mode_timer;
2409
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002410 skb_queue_head_init(&dev->resp_q);
2411
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002412 INIT_LIST_HEAD(&dev->cmd_queue);
2413
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002414 usb_set_intfdata(interface, dev);
2415
2416 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2417 pn533_tx_frame_finish(dev->out_frame);
2418
2419 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2420 dev->in_maxlen);
2421 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002422 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423
2424 fw_ver = (struct pn533_fw_version *)
2425 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2426 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2427 " attached", fw_ver->ver, fw_ver->rev);
2428
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002429 dev->device_type = id->driver_info;
2430 switch (dev->device_type) {
2431 case PN533_DEVICE_STD:
2432 protocols = PN533_ALL_PROTOCOLS;
2433 break;
2434
2435 case PN533_DEVICE_PASORI:
2436 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2437 break;
2438
2439 default:
2440 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2441 dev->device_type);
2442 rc = -EINVAL;
2443 goto destroy_wq;
2444 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445
Samuel Ortize8753042011-08-19 15:47:11 +02002446 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2447 PN533_CMD_DATAEXCH_HEAD_LEN,
2448 PN533_FRAME_TAIL_SIZE);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002449 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002450 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002451
2452 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2453 nfc_set_drvdata(dev->nfc_dev, dev);
2454
2455 rc = nfc_register_device(dev->nfc_dev);
2456 if (rc)
2457 goto free_nfc_dev;
2458
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002459 rc = pn533_setup(dev);
2460 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002461 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002462
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463 return 0;
2464
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002465unregister_nfc_dev:
2466 nfc_unregister_device(dev->nfc_dev);
2467
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002468free_nfc_dev:
2469 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002470
Samuel Ortiz4849f852012-04-10 19:43:17 +02002471destroy_wq:
2472 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002473error:
2474 kfree(dev->in_frame);
2475 usb_free_urb(dev->in_urb);
2476 kfree(dev->out_frame);
2477 usb_free_urb(dev->out_urb);
2478 kfree(dev);
2479 return rc;
2480}
2481
2482static void pn533_disconnect(struct usb_interface *interface)
2483{
2484 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002485 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002486
2487 dev = usb_get_intfdata(interface);
2488 usb_set_intfdata(interface, NULL);
2489
2490 nfc_unregister_device(dev->nfc_dev);
2491 nfc_free_device(dev->nfc_dev);
2492
2493 usb_kill_urb(dev->in_urb);
2494 usb_kill_urb(dev->out_urb);
2495
Samuel Ortiz4849f852012-04-10 19:43:17 +02002496 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002497
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002498 skb_queue_purge(&dev->resp_q);
2499
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002500 del_timer(&dev->listen_timer);
2501
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002502 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2503 list_del(&cmd->queue);
2504 kfree(cmd);
2505 }
2506
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002507 kfree(dev->in_frame);
2508 usb_free_urb(dev->in_urb);
2509 kfree(dev->out_frame);
2510 usb_free_urb(dev->out_urb);
2511 kfree(dev);
2512
Dan Carpenter276556d2011-07-08 10:21:15 +03002513 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002514}
2515
2516static struct usb_driver pn533_driver = {
2517 .name = "pn533",
2518 .probe = pn533_probe,
2519 .disconnect = pn533_disconnect,
2520 .id_table = pn533_table,
2521};
2522
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002523module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002524
2525MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2526 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2527MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2528MODULE_VERSION(VERSION);
2529MODULE_LICENSE("GPL");