blob: 18e279d3e836abc559d00809080a51b55302e766 [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
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001172static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001173{
1174 struct pn533_poll_response *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001175 int rc;
1176
1177 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1178
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001179 resp = (struct pn533_poll_response *) params;
1180 if (resp->nbtg) {
1181 rc = pn533_target_found(dev, resp, params_len);
1182
1183 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001184 if (rc == 0) {
1185 pn533_poll_reset_mod_list(dev);
1186 return 0;
1187 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001188 }
1189
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001190 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001191}
1192
Samuel Ortizad3823c2012-05-30 23:54:55 +02001193static int pn533_init_target_frame(struct pn533_frame *frame,
1194 u8 *gb, size_t gb_len)
1195{
1196 struct pn533_cmd_init_target *cmd;
1197 size_t cmd_len;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001198 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1199 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1200 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1201 0xff, 0xff}; /* System code */
1202 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1203 0x0, 0x0, 0x0,
1204 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001205
1206 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1207 cmd = kzalloc(cmd_len, GFP_KERNEL);
1208 if (cmd == NULL)
1209 return -ENOMEM;
1210
1211 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1212
1213 /* DEP support only */
1214 cmd->mode |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001215
1216 /* Felica params */
1217 memcpy(cmd->felica, felica_params, 18);
1218 get_random_bytes(cmd->felica + 2, 6);
1219
1220 /* NFCID3 */
1221 memset(cmd->nfcid3, 0, 10);
1222 memcpy(cmd->nfcid3, cmd->felica, 8);
1223
1224 /* MIFARE params */
1225 memcpy(cmd->mifare, mifare_params, 6);
1226
1227 /* General bytes */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001228 cmd->gb_len = gb_len;
1229 memcpy(cmd->gb, gb, gb_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001230
Samuel Ortizad3823c2012-05-30 23:54:55 +02001231 /* Len Tk */
1232 cmd->gb[gb_len] = 0;
1233
1234 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001235
Samuel Ortizad3823c2012-05-30 23:54:55 +02001236 frame->datalen += cmd_len;
1237
1238 pn533_tx_frame_finish(frame);
1239
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001240 kfree(cmd);
1241
Samuel Ortizad3823c2012-05-30 23:54:55 +02001242 return 0;
1243}
1244
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001245#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1246#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1247static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1248 u8 *params, int params_len)
1249{
1250 struct sk_buff *skb_resp = arg;
1251 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1252
1253 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1254
1255 if (params_len < 0) {
1256 nfc_dev_err(&dev->interface->dev,
1257 "Error %d when starting as a target",
1258 params_len);
1259
1260 return params_len;
1261 }
1262
1263 if (params_len > 0 && params[0] != 0) {
1264 nfc_tm_deactivated(dev->nfc_dev);
1265
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001266 dev->tgt_mode = 0;
1267
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001268 kfree_skb(skb_resp);
1269 return 0;
1270 }
1271
1272 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1273 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1274 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1275
1276 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1277}
1278
1279static void pn533_wq_tg_get_data(struct work_struct *work)
1280{
1281 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1282 struct pn533_frame *in_frame;
1283 struct sk_buff *skb_resp;
1284 size_t skb_resp_len;
1285
1286 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1287
1288 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1289 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1290 PN533_FRAME_TAIL_SIZE;
1291
1292 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1293 if (!skb_resp)
1294 return;
1295
1296 in_frame = (struct pn533_frame *)skb_resp->data;
1297
1298 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1299 pn533_tx_frame_finish(dev->out_frame);
1300
1301 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1302 skb_resp_len,
1303 pn533_tm_get_data_complete,
1304 skb_resp, GFP_KERNEL);
1305
1306 return;
1307}
1308
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001309#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001310static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001311{
1312 struct pn533_cmd_init_target_response *resp;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001313 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1314 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001315 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001316
1317 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1318
1319 if (params_len < 0) {
1320 nfc_dev_err(&dev->interface->dev,
1321 "Error %d when starting as a target",
1322 params_len);
1323
1324 return params_len;
1325 }
1326
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001327 if (params_len < ATR_REQ_GB_OFFSET + 1)
1328 return -EINVAL;
1329
Samuel Ortizad3823c2012-05-30 23:54:55 +02001330 resp = (struct pn533_cmd_init_target_response *) params;
1331
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001332 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1333 resp->mode, params_len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001334
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001335 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1336 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1337 comm_mode = NFC_COMM_ACTIVE;
1338
1339 /* Again, only DEP */
1340 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1341 return -EOPNOTSUPP;
1342
1343 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1344 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1345
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001346 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1347 comm_mode, gb, gb_len);
1348 if (rc < 0) {
1349 nfc_dev_err(&dev->interface->dev,
1350 "Error when signaling target activation");
1351 return rc;
1352 }
1353
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001354 dev->tgt_mode = 1;
1355
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001356 queue_work(dev->wq, &dev->tg_work);
1357
1358 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001359}
1360
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001361static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001362{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001363 struct pn533 *dev = (struct pn533 *) data;
1364
1365 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1366
1367 /* An ack will cancel the last issued command (poll) */
1368 pn533_send_ack(dev, GFP_ATOMIC);
1369
1370 dev->cancel_listen = 1;
1371
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001372 pn533_poll_next_mod(dev);
1373
1374 queue_work(dev->wq, &dev->poll_work);
1375}
1376
1377static int pn533_poll_complete(struct pn533 *dev, void *arg,
1378 u8 *params, int params_len)
1379{
1380 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001381 int rc;
1382
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001383 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1384
1385 if (params_len == -ENOENT) {
1386 if (dev->poll_mod_count != 0)
1387 return 0;
1388
1389 nfc_dev_err(&dev->interface->dev,
1390 "Polling operation has been stopped");
1391
1392 goto stop_poll;
1393 }
1394
1395 if (params_len < 0) {
1396 nfc_dev_err(&dev->interface->dev,
1397 "Error %d when running poll", params_len);
1398
1399 goto stop_poll;
1400 }
1401
1402 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1403
1404 if (cur_mod->len == 0) {
1405 del_timer(&dev->listen_timer);
1406
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001407 return pn533_init_target_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001408 } else {
Waldemar Rymarkiewiczab34a182012-10-11 14:04:01 +02001409 rc = pn533_start_poll_complete(dev, params, params_len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001410 if (!rc)
1411 return rc;
1412 }
1413
1414 pn533_poll_next_mod(dev);
1415
1416 queue_work(dev->wq, &dev->poll_work);
1417
1418 return 0;
1419
1420stop_poll:
Samuel Ortizad3823c2012-05-30 23:54:55 +02001421 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001422 dev->poll_protocols = 0;
1423 return 0;
1424}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001425
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001426static void pn533_build_poll_frame(struct pn533 *dev,
1427 struct pn533_frame *frame,
1428 struct pn533_poll_modulations *mod)
1429{
1430 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001431
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001432 if (mod->len == 0) {
1433 /* Listen mode */
1434 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1435 } else {
1436 /* Polling mode */
1437 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1438
1439 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1440 frame->datalen += mod->len;
1441
1442 pn533_tx_frame_finish(frame);
1443 }
1444}
1445
1446static int pn533_send_poll_frame(struct pn533 *dev)
1447{
1448 struct pn533_poll_modulations *cur_mod;
1449 int rc;
1450
1451 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1452
1453 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001454
1455 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001456 dev->in_maxlen, pn533_poll_complete,
1457 NULL, GFP_KERNEL);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001458 if (rc)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001459 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001460
1461 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001462}
1463
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001464static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001465{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001466 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1467 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001468 int rc;
1469
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001470 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1471
1472 nfc_dev_dbg(&dev->interface->dev,
1473 "%s cancel_listen %d modulation len %d",
1474 __func__, dev->cancel_listen, cur_mod->len);
1475
1476 if (dev->cancel_listen == 1) {
1477 dev->cancel_listen = 0;
1478 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001479 }
1480
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001481 rc = pn533_send_poll_frame(dev);
1482 if (rc)
1483 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001484
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001485 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1486 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001487
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001488 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001489}
1490
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001491static int pn533_start_poll(struct nfc_dev *nfc_dev,
1492 u32 im_protocols, u32 tm_protocols)
1493{
1494 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1495
1496 nfc_dev_dbg(&dev->interface->dev,
1497 "%s: im protocols 0x%x tm protocols 0x%x",
1498 __func__, im_protocols, tm_protocols);
1499
1500 if (dev->tgt_active_prot) {
1501 nfc_dev_err(&dev->interface->dev,
1502 "Cannot poll with a target already activated");
1503 return -EBUSY;
1504 }
1505
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001506 if (dev->tgt_mode) {
1507 nfc_dev_err(&dev->interface->dev,
1508 "Cannot poll while already being activated");
1509 return -EBUSY;
1510 }
1511
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001512 if (tm_protocols) {
1513 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1514 if (dev->gb == NULL)
1515 tm_protocols = 0;
1516 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001517
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518 dev->poll_mod_curr = 0;
1519 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1520 dev->poll_protocols = im_protocols;
1521 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001522
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001523 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001524}
1525
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001526static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1527{
1528 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1529
1530 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1531
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001532 del_timer(&dev->listen_timer);
1533
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001534 if (!dev->poll_mod_count) {
1535 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1536 " running");
1537 return;
1538 }
1539
1540 /* An ack will cancel the last issued command (poll) */
1541 pn533_send_ack(dev, GFP_KERNEL);
1542
1543 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1544 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001545
1546 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001547}
1548
1549static int pn533_activate_target_nfcdep(struct pn533 *dev)
1550{
1551 struct pn533_cmd_activate_param param;
1552 struct pn533_cmd_activate_response *resp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001553 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001554 int rc;
1555
1556 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1557
1558 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1559
1560 param.tg = 1;
1561 param.next = 0;
1562 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
1563 sizeof(struct pn533_cmd_activate_param));
1564 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1565
1566 pn533_tx_frame_finish(dev->out_frame);
1567
1568 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1569 dev->in_maxlen);
1570 if (rc)
1571 return rc;
1572
1573 resp = (struct pn533_cmd_activate_response *)
1574 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1575 rc = resp->status & PN533_CMD_RET_MASK;
1576 if (rc != PN533_CMD_RET_SUCCESS)
1577 return -EIO;
1578
Samuel Ortiz541d9202011-12-14 16:43:10 +01001579 /* ATR_RES general bytes are located at offset 16 */
1580 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1581 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1582
1583 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001584}
1585
Eric Lapuyade90099432012-05-07 12:31:13 +02001586static int pn533_activate_target(struct nfc_dev *nfc_dev,
1587 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001588{
1589 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1590 int rc;
1591
1592 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1593 protocol);
1594
1595 if (dev->poll_mod_count) {
1596 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1597 " polling");
1598 return -EBUSY;
1599 }
1600
1601 if (dev->tgt_active_prot) {
1602 nfc_dev_err(&dev->interface->dev, "There is already an active"
1603 " target");
1604 return -EBUSY;
1605 }
1606
1607 if (!dev->tgt_available_prots) {
1608 nfc_dev_err(&dev->interface->dev, "There is no available target"
1609 " to activate");
1610 return -EINVAL;
1611 }
1612
1613 if (!(dev->tgt_available_prots & (1 << protocol))) {
1614 nfc_dev_err(&dev->interface->dev, "The target does not support"
1615 " the requested protocol %u", protocol);
1616 return -EINVAL;
1617 }
1618
1619 if (protocol == NFC_PROTO_NFC_DEP) {
1620 rc = pn533_activate_target_nfcdep(dev);
1621 if (rc) {
1622 nfc_dev_err(&dev->interface->dev, "Error %d when"
1623 " activating target with"
1624 " NFC_DEP protocol", rc);
1625 return rc;
1626 }
1627 }
1628
1629 dev->tgt_active_prot = protocol;
1630 dev->tgt_available_prots = 0;
1631
1632 return 0;
1633}
1634
Eric Lapuyade90099432012-05-07 12:31:13 +02001635static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1636 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001637{
1638 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1639 u8 tg;
1640 u8 status;
1641 int rc;
1642
1643 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1644
1645 if (!dev->tgt_active_prot) {
1646 nfc_dev_err(&dev->interface->dev, "There is no active target");
1647 return;
1648 }
1649
1650 dev->tgt_active_prot = 0;
1651
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001652 skb_queue_purge(&dev->resp_q);
1653
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001654 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1655
1656 tg = 1;
1657 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1658 dev->out_frame->datalen += sizeof(u8);
1659
1660 pn533_tx_frame_finish(dev->out_frame);
1661
1662 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1663 dev->in_maxlen);
1664 if (rc) {
1665 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1666 " command to the controller");
1667 return;
1668 }
1669
1670 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1671 rc = status & PN533_CMD_RET_MASK;
1672 if (rc != PN533_CMD_RET_SUCCESS)
1673 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1674 " the target", rc);
1675
1676 return;
1677}
1678
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001679
1680static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1681 u8 *params, int params_len)
1682{
1683 struct pn533_cmd_jump_dep *cmd;
1684 struct pn533_cmd_jump_dep_response *resp;
1685 struct nfc_target nfc_target;
1686 u8 target_gt_len;
1687 int rc;
1688
1689 if (params_len == -ENOENT) {
1690 nfc_dev_dbg(&dev->interface->dev, "");
1691 return 0;
1692 }
1693
1694 if (params_len < 0) {
1695 nfc_dev_err(&dev->interface->dev,
1696 "Error %d when bringing DEP link up",
1697 params_len);
1698 return 0;
1699 }
1700
1701 if (dev->tgt_available_prots &&
1702 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1703 nfc_dev_err(&dev->interface->dev,
1704 "The target does not support DEP");
1705 return -EINVAL;
1706 }
1707
1708 resp = (struct pn533_cmd_jump_dep_response *) params;
1709 cmd = (struct pn533_cmd_jump_dep *) arg;
1710 rc = resp->status & PN533_CMD_RET_MASK;
1711 if (rc != PN533_CMD_RET_SUCCESS) {
1712 nfc_dev_err(&dev->interface->dev,
1713 "Bringing DEP link up failed %d", rc);
1714 return 0;
1715 }
1716
1717 if (!dev->tgt_available_prots) {
1718 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1719
1720 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001721 nfc_target.nfcid1_len = 10;
1722 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001723 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1724 if (rc)
1725 return 0;
1726
1727 dev->tgt_available_prots = 0;
1728 }
1729
1730 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1731
1732 /* ATR_RES general bytes are located at offset 17 */
1733 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1734 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1735 resp->gt, target_gt_len);
1736 if (rc == 0)
1737 rc = nfc_dep_link_is_up(dev->nfc_dev,
1738 dev->nfc_dev->targets[0].idx,
1739 !cmd->active, NFC_RF_INITIATOR);
1740
1741 return 0;
1742}
1743
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001744static int pn533_mod_to_baud(struct pn533 *dev)
1745{
1746 switch (dev->poll_mod_curr) {
1747 case PN533_POLL_MOD_106KBPS_A:
1748 return 0;
1749 case PN533_POLL_MOD_212KBPS_FELICA:
1750 return 1;
1751 case PN533_POLL_MOD_424KBPS_FELICA:
1752 return 2;
1753 default:
1754 return -EINVAL;
1755 }
1756}
1757
Samuel Ortizd7f33452012-05-29 21:45:21 +02001758#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001759static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Samuel Ortiz47807d32012-03-05 01:03:50 +01001760 u8 comm_mode, u8* gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001761{
1762 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1763 struct pn533_cmd_jump_dep *cmd;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001764 u8 cmd_len, *data_ptr;
1765 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001766 int rc, baud;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001767
1768 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1769
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001770 if (dev->poll_mod_count) {
1771 nfc_dev_err(&dev->interface->dev,
1772 "Cannot bring the DEP link up while polling");
1773 return -EBUSY;
1774 }
1775
1776 if (dev->tgt_active_prot) {
1777 nfc_dev_err(&dev->interface->dev,
1778 "There is already an active target");
1779 return -EBUSY;
1780 }
1781
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001782 baud = pn533_mod_to_baud(dev);
1783 if (baud < 0) {
1784 nfc_dev_err(&dev->interface->dev,
1785 "Invalid curr modulation %d", dev->poll_mod_curr);
1786 return baud;
1787 }
1788
Samuel Ortiz47807d32012-03-05 01:03:50 +01001789 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001790 if (comm_mode == NFC_COMM_PASSIVE)
1791 cmd_len += PASSIVE_DATA_LEN;
1792
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001793 cmd = kzalloc(cmd_len, GFP_KERNEL);
1794 if (cmd == NULL)
1795 return -ENOMEM;
1796
1797 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1798
1799 cmd->active = !comm_mode;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001800 cmd->next = 0;
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001801 cmd->baud = baud;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001802 data_ptr = cmd->data;
1803 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
1804 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
1805 cmd->next |= 1;
1806 data_ptr += PASSIVE_DATA_LEN;
1807 }
1808
Samuel Ortiz47807d32012-03-05 01:03:50 +01001809 if (gb != NULL && gb_len > 0) {
Samuel Ortizd7f33452012-05-29 21:45:21 +02001810 cmd->next |= 4; /* We have some Gi */
1811 memcpy(data_ptr, gb, gb_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001812 } else {
1813 cmd->next = 0;
1814 }
1815
1816 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1817 dev->out_frame->datalen += cmd_len;
1818
1819 pn533_tx_frame_finish(dev->out_frame);
1820
1821 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1822 dev->in_maxlen, pn533_in_dep_link_up_complete,
1823 cmd, GFP_KERNEL);
1824 if (rc)
1825 goto out;
1826
1827
1828out:
1829 kfree(cmd);
1830
1831 return rc;
1832}
1833
1834static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1835{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001836 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1837
1838 pn533_poll_reset_mod_list(dev);
1839
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001840 if (dev->tgt_mode || dev->tgt_active_prot) {
1841 pn533_send_ack(dev, GFP_KERNEL);
1842 usb_kill_urb(dev->in_urb);
1843 }
1844
1845 dev->tgt_active_prot = 0;
1846 dev->tgt_mode = 0;
1847
1848 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001849
1850 return 0;
1851}
1852
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001853static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
1854 bool target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001855{
1856 int payload_len = skb->len;
1857 struct pn533_frame *out_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001858 u8 tg;
1859
1860 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1861 payload_len);
1862
1863 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1864 /* TODO: Implement support to multi-part data exchange */
1865 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1866 " max allowed: %d",
1867 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1868 return -ENOSYS;
1869 }
1870
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001871 if (target == true) {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001872 switch (dev->device_type) {
Samuel Ortiza1fbbf12012-07-03 23:45:26 +02001873 case PN533_DEVICE_PASORI:
1874 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
1875 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1876 out_frame = (struct pn533_frame *) skb->data;
1877 pn533_tx_frame_init(out_frame,
1878 PN533_CMD_IN_COMM_THRU);
1879
1880 break;
1881 }
1882
1883 default:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001884 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1885 out_frame = (struct pn533_frame *) skb->data;
1886 pn533_tx_frame_init(out_frame,
1887 PN533_CMD_IN_DATA_EXCHANGE);
1888 tg = 1;
1889 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
1890 &tg, sizeof(u8));
1891 out_frame->datalen += sizeof(u8);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001892
1893 break;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02001894 }
1895
Samuel Ortizdadb06f2012-05-31 00:09:11 +02001896 } else {
1897 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1898 out_frame = (struct pn533_frame *) skb->data;
1899 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
1900 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001901
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001902
1903 /* The data is already in the out_frame, just update the datalen */
1904 out_frame->datalen += payload_len;
1905
1906 pn533_tx_frame_finish(out_frame);
1907 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1908
1909 return 0;
1910}
1911
1912struct pn533_data_exchange_arg {
1913 struct sk_buff *skb_resp;
1914 struct sk_buff *skb_out;
1915 data_exchange_cb_t cb;
1916 void *cb_context;
1917};
1918
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001919static struct sk_buff *pn533_build_response(struct pn533 *dev)
1920{
1921 struct sk_buff *skb, *tmp, *t;
1922 unsigned int skb_len = 0, tmp_len = 0;
1923
1924 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1925
1926 if (skb_queue_empty(&dev->resp_q))
1927 return NULL;
1928
1929 if (skb_queue_len(&dev->resp_q) == 1) {
1930 skb = skb_dequeue(&dev->resp_q);
1931 goto out;
1932 }
1933
1934 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1935 skb_len += tmp->len;
1936
1937 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1938 __func__, skb_len);
1939
1940 skb = alloc_skb(skb_len, GFP_KERNEL);
1941 if (skb == NULL)
1942 goto out;
1943
1944 skb_put(skb, skb_len);
1945
1946 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1947 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1948 tmp_len += tmp->len;
1949 }
1950
1951out:
1952 skb_queue_purge(&dev->resp_q);
1953
1954 return skb;
1955}
1956
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001957static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1958 u8 *params, int params_len)
1959{
1960 struct pn533_data_exchange_arg *arg = _arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001961 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001962 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1963 int err = 0;
1964 u8 status;
1965 u8 cmd_ret;
1966
1967 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1968
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001969 dev_kfree_skb(arg->skb_out);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001970
1971 if (params_len < 0) { /* error */
1972 err = params_len;
1973 goto error;
1974 }
1975
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001976 status = params[0];
1977
1978 cmd_ret = status & PN533_CMD_RET_MASK;
1979 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1980 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1981 " exchanging data", cmd_ret);
1982 err = -EIO;
1983 goto error;
1984 }
1985
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001986 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001987 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1988 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001989 skb_queue_tail(&dev->resp_q, skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001990
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001991 if (status & PN533_CMD_MI_MASK) {
1992 queue_work(dev->wq, &dev->mi_work);
1993 return -EINPROGRESS;
1994 }
1995
1996 skb = pn533_build_response(dev);
1997 if (skb == NULL)
1998 goto error;
1999
2000 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002001 kfree(arg);
2002 return 0;
2003
2004error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002005 skb_queue_purge(&dev->resp_q);
2006 dev_kfree_skb(skb_resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002007 arg->cb(arg->cb_context, NULL, err);
2008 kfree(arg);
2009 return 0;
2010}
2011
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002012static int pn533_transceive(struct nfc_dev *nfc_dev,
2013 struct nfc_target *target, struct sk_buff *skb,
2014 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002015{
2016 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2017 struct pn533_frame *out_frame, *in_frame;
2018 struct pn533_data_exchange_arg *arg;
2019 struct sk_buff *skb_resp;
2020 int skb_resp_len;
2021 int rc;
2022
2023 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2024
2025 if (!dev->tgt_active_prot) {
2026 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2027 " there is no active target");
2028 rc = -EINVAL;
2029 goto error;
2030 }
2031
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002032 rc = pn533_build_tx_frame(dev, skb, true);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002033 if (rc)
2034 goto error;
2035
2036 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
2037 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2038 PN533_FRAME_TAIL_SIZE;
2039
Samuel Ortiz7c7cd3b2011-12-14 16:43:06 +01002040 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002041 if (!skb_resp) {
2042 rc = -ENOMEM;
2043 goto error;
2044 }
2045
2046 in_frame = (struct pn533_frame *) skb_resp->data;
2047 out_frame = (struct pn533_frame *) skb->data;
2048
2049 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2050 if (!arg) {
2051 rc = -ENOMEM;
2052 goto free_skb_resp;
2053 }
2054
2055 arg->skb_resp = skb_resp;
2056 arg->skb_out = skb;
2057 arg->cb = cb;
2058 arg->cb_context = cb_context;
2059
2060 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2061 pn533_data_exchange_complete, arg,
2062 GFP_KERNEL);
2063 if (rc) {
2064 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2065 " perform data_exchange", rc);
2066 goto free_arg;
2067 }
2068
2069 return 0;
2070
2071free_arg:
2072 kfree(arg);
2073free_skb_resp:
2074 kfree_skb(skb_resp);
2075error:
2076 kfree_skb(skb);
2077 return rc;
2078}
2079
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002080static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2081 u8 *params, int params_len)
2082{
2083 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2084
2085 if (params_len < 0) {
2086 nfc_dev_err(&dev->interface->dev,
2087 "Error %d when sending data",
2088 params_len);
2089
2090 return params_len;
2091 }
2092
2093 if (params_len > 0 && params[0] != 0) {
2094 nfc_tm_deactivated(dev->nfc_dev);
2095
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002096 dev->tgt_mode = 0;
2097
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002098 return 0;
2099 }
2100
2101 queue_work(dev->wq, &dev->tg_work);
2102
2103 return 0;
2104}
2105
2106static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2107{
2108 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2109 struct pn533_frame *out_frame;
2110 int rc;
2111
2112 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2113
2114 rc = pn533_build_tx_frame(dev, skb, false);
2115 if (rc)
2116 goto error;
2117
2118 out_frame = (struct pn533_frame *) skb->data;
2119
2120 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2121 dev->in_maxlen, pn533_tm_send_complete,
2122 NULL, GFP_KERNEL);
2123 if (rc) {
2124 nfc_dev_err(&dev->interface->dev,
2125 "Error %d when trying to send data", rc);
2126 goto error;
2127 }
2128
2129 return 0;
2130
2131error:
2132 kfree_skb(skb);
2133
2134 return rc;
2135}
2136
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002137static void pn533_wq_mi_recv(struct work_struct *work)
2138{
2139 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2140 struct sk_buff *skb_cmd;
2141 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2142 struct pn533_frame *out_frame, *in_frame;
2143 struct sk_buff *skb_resp;
2144 int skb_resp_len;
2145 int rc;
2146
2147 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2148
2149 /* This is a zero payload size skb */
2150 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
2151 GFP_KERNEL);
2152 if (skb_cmd == NULL)
2153 goto error_cmd;
2154
2155 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2156
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002157 rc = pn533_build_tx_frame(dev, skb_cmd, true);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002158 if (rc)
2159 goto error_frame;
2160
2161 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
2162 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2163 PN533_FRAME_TAIL_SIZE;
2164 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2165 if (!skb_resp) {
2166 rc = -ENOMEM;
2167 goto error_frame;
2168 }
2169
2170 in_frame = (struct pn533_frame *) skb_resp->data;
2171 out_frame = (struct pn533_frame *) skb_cmd->data;
2172
2173 arg->skb_resp = skb_resp;
2174 arg->skb_out = skb_cmd;
2175
2176 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2177 skb_resp_len,
2178 pn533_data_exchange_complete,
2179 dev->cmd_complete_arg, GFP_KERNEL);
2180 if (!rc)
2181 return;
2182
2183 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2184 " perform data_exchange", rc);
2185
2186 kfree_skb(skb_resp);
2187
2188error_frame:
2189 kfree_skb(skb_cmd);
2190
2191error_cmd:
2192 pn533_send_ack(dev, GFP_KERNEL);
2193
2194 kfree(arg);
2195
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002196 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002197}
2198
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002199static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2200 u8 cfgdata_len)
2201{
2202 int rc;
2203 u8 *params;
2204
2205 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2206
2207 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2208
2209 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2210 params[0] = cfgitem;
2211 memcpy(&params[1], cfgdata, cfgdata_len);
2212 dev->out_frame->datalen += (1 + cfgdata_len);
2213
2214 pn533_tx_frame_finish(dev->out_frame);
2215
2216 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2217 dev->in_maxlen);
2218
2219 return rc;
2220}
2221
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002222static int pn533_fw_reset(struct pn533 *dev)
2223{
2224 int rc;
2225 u8 *params;
2226
2227 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2228
2229 pn533_tx_frame_init(dev->out_frame, 0x18);
2230
2231 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2232 params[0] = 0x1;
2233 dev->out_frame->datalen += 1;
2234
2235 pn533_tx_frame_finish(dev->out_frame);
2236
2237 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2238 dev->in_maxlen);
2239
2240 return rc;
2241}
2242
2243static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002244 .dev_up = NULL,
2245 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002246 .dep_link_up = pn533_dep_link_up,
2247 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002248 .start_poll = pn533_start_poll,
2249 .stop_poll = pn533_stop_poll,
2250 .activate_target = pn533_activate_target,
2251 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002252 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002253 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002254};
2255
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002256static int pn533_setup(struct pn533 *dev)
2257{
2258 struct pn533_config_max_retries max_retries;
2259 struct pn533_config_timing timing;
2260 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2261 int rc;
2262
2263 switch (dev->device_type) {
2264 case PN533_DEVICE_STD:
2265 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2266 max_retries.mx_rty_psl = 2;
2267 max_retries.mx_rty_passive_act =
2268 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2269
2270 timing.rfu = PN533_CONFIG_TIMING_102;
2271 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2272 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2273
2274 break;
2275
2276 case PN533_DEVICE_PASORI:
2277 max_retries.mx_rty_atr = 0x2;
2278 max_retries.mx_rty_psl = 0x1;
2279 max_retries.mx_rty_passive_act =
2280 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2281
2282 timing.rfu = PN533_CONFIG_TIMING_102;
2283 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2284 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2285
2286 break;
2287
2288 default:
2289 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2290 dev->device_type);
2291 return -EINVAL;
2292 }
2293
2294 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2295 (u8 *)&max_retries, sizeof(max_retries));
2296 if (rc) {
2297 nfc_dev_err(&dev->interface->dev,
2298 "Error on setting MAX_RETRIES config");
2299 return rc;
2300 }
2301
2302
2303 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2304 (u8 *)&timing, sizeof(timing));
2305 if (rc) {
2306 nfc_dev_err(&dev->interface->dev,
2307 "Error on setting RF timings");
2308 return rc;
2309 }
2310
2311 switch (dev->device_type) {
2312 case PN533_DEVICE_STD:
2313 break;
2314
2315 case PN533_DEVICE_PASORI:
2316 pn533_fw_reset(dev);
2317
2318 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2319 pasori_cfg, 3);
2320 if (rc) {
2321 nfc_dev_err(&dev->interface->dev,
2322 "Error while settings PASORI config");
2323 return rc;
2324 }
2325
2326 pn533_fw_reset(dev);
2327
2328 break;
2329 }
2330
2331 return 0;
2332}
2333
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002334static int pn533_probe(struct usb_interface *interface,
2335 const struct usb_device_id *id)
2336{
2337 struct pn533_fw_version *fw_ver;
2338 struct pn533 *dev;
2339 struct usb_host_interface *iface_desc;
2340 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002341 int in_endpoint = 0;
2342 int out_endpoint = 0;
2343 int rc = -ENOMEM;
2344 int i;
2345 u32 protocols;
2346
2347 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2348 if (!dev)
2349 return -ENOMEM;
2350
2351 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2352 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002353 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002354
2355 iface_desc = interface->cur_altsetting;
2356 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2357 endpoint = &iface_desc->endpoint[i].desc;
2358
2359 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2360 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
2361 in_endpoint = endpoint->bEndpointAddress;
2362 }
2363
2364 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
2365 dev->out_maxlen =
2366 le16_to_cpu(endpoint->wMaxPacketSize);
2367 out_endpoint = endpoint->bEndpointAddress;
2368 }
2369 }
2370
2371 if (!in_endpoint || !out_endpoint) {
2372 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2373 " bulk-out endpoint");
2374 rc = -ENODEV;
2375 goto error;
2376 }
2377
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002378 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002379 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +02002380 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002381 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2382
2383 if (!dev->in_frame || !dev->out_frame ||
2384 !dev->in_urb || !dev->out_urb)
2385 goto error;
2386
2387 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2388 usb_rcvbulkpipe(dev->udev, in_endpoint),
2389 NULL, 0, NULL, dev);
2390 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2391 usb_sndbulkpipe(dev->udev, out_endpoint),
2392 NULL, 0,
2393 pn533_send_complete, dev);
2394
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002395 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2396 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002397 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002398 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002399 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002400 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002401 if (dev->wq == NULL)
2402 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002403
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002404 init_timer(&dev->listen_timer);
2405 dev->listen_timer.data = (unsigned long) dev;
2406 dev->listen_timer.function = pn533_listen_mode_timer;
2407
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002408 skb_queue_head_init(&dev->resp_q);
2409
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002410 INIT_LIST_HEAD(&dev->cmd_queue);
2411
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002412 usb_set_intfdata(interface, dev);
2413
2414 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2415 pn533_tx_frame_finish(dev->out_frame);
2416
2417 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2418 dev->in_maxlen);
2419 if (rc)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002420 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002421
2422 fw_ver = (struct pn533_fw_version *)
2423 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2424 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2425 " attached", fw_ver->ver, fw_ver->rev);
2426
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002427 dev->device_type = id->driver_info;
2428 switch (dev->device_type) {
2429 case PN533_DEVICE_STD:
2430 protocols = PN533_ALL_PROTOCOLS;
2431 break;
2432
2433 case PN533_DEVICE_PASORI:
2434 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2435 break;
2436
2437 default:
2438 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2439 dev->device_type);
2440 rc = -EINVAL;
2441 goto destroy_wq;
2442 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002443
Samuel Ortize8753042011-08-19 15:47:11 +02002444 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2445 PN533_CMD_DATAEXCH_HEAD_LEN,
2446 PN533_FRAME_TAIL_SIZE);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002447 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002448 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002449
2450 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2451 nfc_set_drvdata(dev->nfc_dev, dev);
2452
2453 rc = nfc_register_device(dev->nfc_dev);
2454 if (rc)
2455 goto free_nfc_dev;
2456
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002457 rc = pn533_setup(dev);
2458 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002459 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002460
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002461 return 0;
2462
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002463unregister_nfc_dev:
2464 nfc_unregister_device(dev->nfc_dev);
2465
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466free_nfc_dev:
2467 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002468
Samuel Ortiz4849f852012-04-10 19:43:17 +02002469destroy_wq:
2470 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002471error:
2472 kfree(dev->in_frame);
2473 usb_free_urb(dev->in_urb);
2474 kfree(dev->out_frame);
2475 usb_free_urb(dev->out_urb);
2476 kfree(dev);
2477 return rc;
2478}
2479
2480static void pn533_disconnect(struct usb_interface *interface)
2481{
2482 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002483 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484
2485 dev = usb_get_intfdata(interface);
2486 usb_set_intfdata(interface, NULL);
2487
2488 nfc_unregister_device(dev->nfc_dev);
2489 nfc_free_device(dev->nfc_dev);
2490
2491 usb_kill_urb(dev->in_urb);
2492 usb_kill_urb(dev->out_urb);
2493
Samuel Ortiz4849f852012-04-10 19:43:17 +02002494 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002495
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002496 skb_queue_purge(&dev->resp_q);
2497
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002498 del_timer(&dev->listen_timer);
2499
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002500 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2501 list_del(&cmd->queue);
2502 kfree(cmd);
2503 }
2504
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505 kfree(dev->in_frame);
2506 usb_free_urb(dev->in_urb);
2507 kfree(dev->out_frame);
2508 usb_free_urb(dev->out_urb);
2509 kfree(dev);
2510
Dan Carpenter276556d2011-07-08 10:21:15 +03002511 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002512}
2513
2514static struct usb_driver pn533_driver = {
2515 .name = "pn533",
2516 .probe = pn533_probe,
2517 .disconnect = pn533_disconnect,
2518 .id_table = pn533_table,
2519};
2520
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002521module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002522
2523MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2524 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2525MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2526MODULE_VERSION(VERSION);
2527MODULE_LICENSE("GPL");