blob: 0d7caf782571febb24fbe2bc672586311183f1e7 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030031#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030032
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030081/* frame definitions */
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010082#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
83 + 2) /* data[0] TFI, data[1] CC */
84#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020085
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010086/*
87 * Max extended frame payload len, excluding TFI and CC
88 * which are already in PN533_FRAME_HEADER_LEN.
89 */
90#define PN533_FRAME_MAX_PAYLOAD_LEN 263
91
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010092#define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
93 Postamble (1) */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030094#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
95#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
96
97/* start of frame */
98#define PN533_SOF 0x00FF
99
100/* frame identifier: in/out/error */
101#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
102#define PN533_DIR_OUT 0xD4
103#define PN533_DIR_IN 0xD5
104
105/* PN533 Commands */
106#define PN533_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300107
108#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
109#define PN533_CMD_RF_CONFIGURATION 0x32
110#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200111#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300112#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
113#define PN533_CMD_IN_ATR 0x50
114#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100115#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300116
Samuel Ortizad3823c2012-05-30 23:54:55 +0200117#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200118#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200119#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100120#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
123
124/* PN533 Return codes */
125#define PN533_CMD_RET_MASK 0x3F
126#define PN533_CMD_MI_MASK 0x40
127#define PN533_CMD_RET_SUCCESS 0x00
128
129struct pn533;
130
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100131typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300132
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100133typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
134 struct sk_buff *resp);
135
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300136/* structs for pn533 commands */
137
138/* PN533_CMD_GET_FIRMWARE_VERSION */
139struct pn533_fw_version {
140 u8 ic;
141 u8 ver;
142 u8 rev;
143 u8 support;
144};
145
146/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200147#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300148#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200149#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300150
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200151#define PN533_CONFIG_TIMING_102 0xb
152#define PN533_CONFIG_TIMING_204 0xc
153#define PN533_CONFIG_TIMING_409 0xd
154#define PN533_CONFIG_TIMING_819 0xe
155
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300156#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
157#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
158
159struct pn533_config_max_retries {
160 u8 mx_rty_atr;
161 u8 mx_rty_psl;
162 u8 mx_rty_passive_act;
163} __packed;
164
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200165struct pn533_config_timing {
166 u8 rfu;
167 u8 atr_res_timeout;
168 u8 dep_timeout;
169} __packed;
170
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300171/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
172
173/* felica commands opcode */
174#define PN533_FELICA_OPC_SENSF_REQ 0
175#define PN533_FELICA_OPC_SENSF_RES 1
176/* felica SENSF_REQ parameters */
177#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
178#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
179#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
180#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
181
182/* type B initiator_data values */
183#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
184#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
185#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
186
187union pn533_cmd_poll_initdata {
188 struct {
189 u8 afi;
190 u8 polling_method;
191 } __packed type_b;
192 struct {
193 u8 opcode;
194 __be16 sc;
195 u8 rc;
196 u8 tsn;
197 } __packed felica;
198};
199
200/* Poll modulations */
201enum {
202 PN533_POLL_MOD_106KBPS_A,
203 PN533_POLL_MOD_212KBPS_FELICA,
204 PN533_POLL_MOD_424KBPS_FELICA,
205 PN533_POLL_MOD_106KBPS_JEWEL,
206 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200207 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300208
209 __PN533_POLL_MOD_AFTER_LAST,
210};
211#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
212
213struct pn533_poll_modulations {
214 struct {
215 u8 maxtg;
216 u8 brty;
217 union pn533_cmd_poll_initdata initiator_data;
218 } __packed data;
219 u8 len;
220};
221
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100222static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300223 [PN533_POLL_MOD_106KBPS_A] = {
224 .data = {
225 .maxtg = 1,
226 .brty = 0,
227 },
228 .len = 2,
229 },
230 [PN533_POLL_MOD_212KBPS_FELICA] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 1,
234 .initiator_data.felica = {
235 .opcode = PN533_FELICA_OPC_SENSF_REQ,
236 .sc = PN533_FELICA_SENSF_SC_ALL,
237 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
238 .tsn = 0,
239 },
240 },
241 .len = 7,
242 },
243 [PN533_POLL_MOD_424KBPS_FELICA] = {
244 .data = {
245 .maxtg = 1,
246 .brty = 2,
247 .initiator_data.felica = {
248 .opcode = PN533_FELICA_OPC_SENSF_REQ,
249 .sc = PN533_FELICA_SENSF_SC_ALL,
250 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
251 .tsn = 0,
252 },
253 },
254 .len = 7,
255 },
256 [PN533_POLL_MOD_106KBPS_JEWEL] = {
257 .data = {
258 .maxtg = 1,
259 .brty = 4,
260 },
261 .len = 2,
262 },
263 [PN533_POLL_MOD_847KBPS_B] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 8,
267 .initiator_data.type_b = {
268 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
269 .polling_method =
270 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
271 },
272 },
273 .len = 3,
274 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200275 [PN533_LISTEN_MOD] = {
276 .len = 0,
277 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300278};
279
280/* PN533_CMD_IN_ATR */
281
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300282struct pn533_cmd_activate_response {
283 u8 status;
284 u8 nfcid3t[10];
285 u8 didt;
286 u8 bst;
287 u8 brt;
288 u8 to;
289 u8 ppt;
290 /* optional */
291 u8 gt[];
292} __packed;
293
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100294struct pn533_cmd_jump_dep_response {
295 u8 status;
296 u8 tg;
297 u8 nfcid3t[10];
298 u8 didt;
299 u8 bst;
300 u8 brt;
301 u8 to;
302 u8 ppt;
303 /* optional */
304 u8 gt[];
305} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300306
Samuel Ortizad3823c2012-05-30 23:54:55 +0200307
308/* PN533_TG_INIT_AS_TARGET */
309#define PN533_INIT_TARGET_PASSIVE 0x1
310#define PN533_INIT_TARGET_DEP 0x2
311
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200312#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
313#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
314#define PN533_INIT_TARGET_RESP_DEP 0x4
315
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300316struct pn533 {
317 struct usb_device *udev;
318 struct usb_interface *interface;
319 struct nfc_dev *nfc_dev;
320
321 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300322 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300323
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200324 struct sk_buff_head resp_q;
325
Samuel Ortiz4849f852012-04-10 19:43:17 +0200326 struct workqueue_struct *wq;
327 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200328 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200330 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200331 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200332 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200333 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200334 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300335
336 pn533_cmd_complete_t cmd_complete;
337 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100338 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200339 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300340 u8 cmd;
341
342 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
343 u8 poll_mod_count;
344 u8 poll_mod_curr;
345 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200346 u32 listen_protocols;
347
348 u8 *gb;
349 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350
351 u8 tgt_available_prots;
352 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200353 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200354
355 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200356
357 struct list_head cmd_queue;
358 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100359
360 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200361};
362
363struct pn533_cmd {
364 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100365 u8 cmd_code;
366 struct sk_buff *req;
367 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100368 int resp_len;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200369 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300370};
371
372struct pn533_frame {
373 u8 preamble;
374 __be16 start_frame;
375 u8 datalen;
376 u8 datalen_checksum;
377 u8 data[];
378} __packed;
379
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100380struct pn533_frame_ops {
381 void (*tx_frame_init)(void *frame, u8 cmd_code);
382 void (*tx_frame_finish)(void *frame);
383 void (*tx_update_payload_len)(void *frame, int len);
384 int tx_header_len;
385 int tx_tail_len;
386
387 bool (*rx_is_frame_valid)(void *frame);
388 int (*rx_frame_size)(void *frame);
389 int rx_header_len;
390 int rx_tail_len;
391
392 int max_payload_len;
393 u8 (*get_cmd_code)(void *frame);
394};
395
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396/* The rule: value + checksum = 0 */
397static inline u8 pn533_checksum(u8 value)
398{
399 return ~value + 1;
400}
401
402/* The rule: sum(data elements) + checksum = 0 */
403static u8 pn533_data_checksum(u8 *data, int datalen)
404{
405 u8 sum = 0;
406 int i;
407
408 for (i = 0; i < datalen; i++)
409 sum += data[i];
410
411 return pn533_checksum(sum);
412}
413
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100414static void pn533_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300415{
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100416 struct pn533_frame *frame = _frame;
417
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418 frame->preamble = 0;
419 frame->start_frame = cpu_to_be16(PN533_SOF);
420 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100421 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300422 frame->datalen = 2;
423}
424
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100425static void pn533_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300426{
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100427 struct pn533_frame *frame = _frame;
428
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300429 frame->datalen_checksum = pn533_checksum(frame->datalen);
430
431 PN533_FRAME_CHECKSUM(frame) =
432 pn533_data_checksum(frame->data, frame->datalen);
433
434 PN533_FRAME_POSTAMBLE(frame) = 0;
435}
436
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100437static void pn533_tx_update_payload_len(void *_frame, int len)
438{
439 struct pn533_frame *frame = _frame;
440
441 frame->datalen += len;
442}
443
444static bool pn533_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300445{
446 u8 checksum;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100447 struct pn533_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448
449 if (frame->start_frame != cpu_to_be16(PN533_SOF))
450 return false;
451
452 checksum = pn533_checksum(frame->datalen);
453 if (checksum != frame->datalen_checksum)
454 return false;
455
456 checksum = pn533_data_checksum(frame->data, frame->datalen);
457 if (checksum != PN533_FRAME_CHECKSUM(frame))
458 return false;
459
460 return true;
461}
462
463static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
464{
465 if (frame->start_frame != cpu_to_be16(PN533_SOF))
466 return false;
467
468 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
469 return false;
470
471 return true;
472}
473
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100474static inline int pn533_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300475{
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100476 struct pn533_frame *f = frame;
477
478 return sizeof(struct pn533_frame) + f->datalen + PN533_FRAME_TAIL_LEN;
479}
480
481static u8 pn533_get_cmd_code(void *frame)
482{
483 struct pn533_frame *f = frame;
484
485 return PN533_FRAME_CMD(f);
486}
487
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100488static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100489 .tx_frame_init = pn533_tx_frame_init,
490 .tx_frame_finish = pn533_tx_frame_finish,
491 .tx_update_payload_len = pn533_tx_update_payload_len,
492 .tx_header_len = PN533_FRAME_HEADER_LEN,
493 .tx_tail_len = PN533_FRAME_TAIL_LEN,
494
495 .rx_is_frame_valid = pn533_rx_frame_is_valid,
496 .rx_frame_size = pn533_rx_frame_size,
497 .rx_header_len = PN533_FRAME_HEADER_LEN,
498 .rx_tail_len = PN533_FRAME_TAIL_LEN,
499
500 .max_payload_len = PN533_FRAME_MAX_PAYLOAD_LEN,
501 .get_cmd_code = pn533_get_cmd_code,
502};
503
504static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
505{
506 return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300507}
508
Samuel Ortiz4849f852012-04-10 19:43:17 +0200509
510static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200512 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300513 int rc;
514
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100515 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200517 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300518}
519
520static void pn533_recv_response(struct urb *urb)
521{
522 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100523 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300525 switch (urb->status) {
526 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100527 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528 case -ECONNRESET:
529 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100530 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100531 "The urb has been canceled (status %d)",
532 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200533 dev->wq_in_error = urb->status;
534 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100535 break;
536 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100538 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100539 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200540 dev->wq_in_error = urb->status;
541 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300542 }
543
544 in_frame = dev->in_urb->transfer_buffer;
545
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100546 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100547 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100548 in_frame, dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100549
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100550 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300551 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200552 dev->wq_in_error = -EIO;
553 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300554 }
555
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100556 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100557 nfc_dev_err(&dev->interface->dev,
558 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200559 dev->wq_in_error = -EIO;
560 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300561 }
562
Samuel Ortiz4849f852012-04-10 19:43:17 +0200563 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300564
Samuel Ortiz4849f852012-04-10 19:43:17 +0200565sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200566 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567}
568
569static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
570{
571 dev->in_urb->complete = pn533_recv_response;
572
573 return usb_submit_urb(dev->in_urb, flags);
574}
575
576static void pn533_recv_ack(struct urb *urb)
577{
578 struct pn533 *dev = urb->context;
579 struct pn533_frame *in_frame;
580 int rc;
581
582 switch (urb->status) {
583 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100584 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585 case -ECONNRESET:
586 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100587 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100588 "The urb has been stopped (status %d)",
589 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200590 dev->wq_in_error = urb->status;
591 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100592 break;
593 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300594 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100595 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100596 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200597 dev->wq_in_error = urb->status;
598 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599 }
600
601 in_frame = dev->in_urb->transfer_buffer;
602
603 if (!pn533_rx_frame_is_ack(in_frame)) {
604 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200605 dev->wq_in_error = -EIO;
606 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300607 }
608
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300609 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
610 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100611 nfc_dev_err(&dev->interface->dev,
612 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200613 dev->wq_in_error = rc;
614 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300615 }
616
617 return;
618
Samuel Ortiz4849f852012-04-10 19:43:17 +0200619sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200620 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300621}
622
623static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
624{
625 dev->in_urb->complete = pn533_recv_ack;
626
627 return usb_submit_urb(dev->in_urb, flags);
628}
629
630static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
631{
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100632 u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
633 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300634 int rc;
635
636 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
637
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100638 dev->out_urb->transfer_buffer = ack;
639 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300640 rc = usb_submit_urb(dev->out_urb, flags);
641
642 return rc;
643}
644
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100645static int __pn533_send_frame_async(struct pn533 *dev,
646 struct sk_buff *out,
647 struct sk_buff *in,
648 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300649 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100650 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651{
652 int rc;
653
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100654 dev->cmd = dev->ops->get_cmd_code(out->data);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300655 dev->cmd_complete = cmd_complete;
656 dev->cmd_complete_arg = arg;
657
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100658 dev->out_urb->transfer_buffer = out->data;
659 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300660
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100661 dev->in_urb->transfer_buffer = in->data;
662 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300663
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100664 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100665 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100666
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100667 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300668 if (rc)
669 return rc;
670
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100671 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300672 if (rc)
673 goto error;
674
675 return 0;
676
677error:
678 usb_unlink_urb(dev->out_urb);
679 return rc;
680}
681
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100682static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
683 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100685 /* payload is already there, just update datalen */
686 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100687 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100688
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100689
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100690 skb_push(skb, ops->tx_header_len);
691 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100692
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100693 ops->tx_frame_init(skb->data, cmd_code);
694 ops->tx_update_payload_len(skb->data, payload_len);
695 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100696}
697
698struct pn533_send_async_complete_arg {
699 pn533_send_async_complete_t complete_cb;
700 void *complete_cb_context;
701 struct sk_buff *resp;
702 struct sk_buff *req;
703};
704
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100705static int pn533_send_async_complete(struct pn533 *dev, void *_arg, int status)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100706{
707 struct pn533_send_async_complete_arg *arg = _arg;
708
709 struct sk_buff *req = arg->req;
710 struct sk_buff *resp = arg->resp;
711
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100712 int rc;
713
714 dev_kfree_skb(req);
715
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100716 if (status < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100717 arg->complete_cb(dev, arg->complete_cb_context,
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100718 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100719 dev_kfree_skb(resp);
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100720 kfree(arg);
721 return status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100722 }
723
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100724 skb_put(resp, dev->ops->rx_frame_size(resp->data));
725 skb_pull(resp, dev->ops->rx_header_len);
726 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100727
728 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
729
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100730 kfree(arg);
731 return rc;
732}
733
734static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
735 struct sk_buff *req, struct sk_buff *resp,
736 int resp_len,
737 pn533_send_async_complete_t complete_cb,
738 void *complete_cb_context)
739{
740 struct pn533_cmd *cmd;
741 struct pn533_send_async_complete_arg *arg;
742 int rc = 0;
743
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100744 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100745
Thierry Escande858ce022013-01-12 19:37:12 +0100746 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100747 if (!arg)
748 return -ENOMEM;
749
750 arg->complete_cb = complete_cb;
751 arg->complete_cb_context = complete_cb_context;
752 arg->resp = resp;
753 arg->req = req;
754
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100755 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100756
757 mutex_lock(&dev->cmd_lock);
758
759 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100760 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
761 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100762 if (rc)
763 goto error;
764
765 dev->cmd_pending = 1;
766 goto unlock;
767 }
768
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100769 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
770 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100771
772 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
773 if (!cmd) {
774 rc = -ENOMEM;
775 goto error;
776 }
777
778 INIT_LIST_HEAD(&cmd->queue);
779 cmd->cmd_code = cmd_code;
780 cmd->req = req;
781 cmd->resp = resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100782 cmd->resp_len = resp_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100783 cmd->arg = arg;
784
785 list_add_tail(&cmd->queue, &dev->cmd_queue);
786
787 goto unlock;
788
789error:
790 kfree(arg);
791unlock:
792 mutex_unlock(&dev->cmd_lock);
793 return rc;
794}
795
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100796static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
797 struct sk_buff *req,
798 pn533_send_async_complete_t complete_cb,
799 void *complete_cb_context)
800{
801 struct sk_buff *resp;
802 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100803 int resp_len = dev->ops->rx_header_len +
804 dev->ops->max_payload_len +
805 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100806
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100807 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
808 if (!resp)
809 return -ENOMEM;
810
811 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
812 complete_cb_context);
813 if (rc)
814 dev_kfree_skb(resp);
815
816 return rc;
817}
818
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100819static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
820 struct sk_buff *req,
821 pn533_send_async_complete_t complete_cb,
822 void *complete_cb_context)
823{
824 struct sk_buff *resp;
825 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100826 int resp_len = dev->ops->rx_header_len +
827 dev->ops->max_payload_len +
828 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100829
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100830 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100831 if (!resp)
832 return -ENOMEM;
833
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100834 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
835 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100836 if (rc)
837 dev_kfree_skb(resp);
838
839 return rc;
840}
841
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100842/*
843 * pn533_send_cmd_direct_async
844 *
845 * The function sends a piority cmd directly to the chip omiting the cmd
846 * queue. It's intended to be used by chaining mechanism of received responses
847 * where the host has to request every single chunk of data before scheduling
848 * next cmd from the queue.
849 */
850static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
851 struct sk_buff *req,
852 pn533_send_async_complete_t complete_cb,
853 void *complete_cb_context)
854{
855 struct pn533_send_async_complete_arg *arg;
856 struct sk_buff *resp;
857 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100858 int resp_len = dev->ops->rx_header_len +
859 dev->ops->max_payload_len +
860 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100861
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100862 resp = alloc_skb(resp_len, GFP_KERNEL);
863 if (!resp)
864 return -ENOMEM;
865
Thierry Escande858ce022013-01-12 19:37:12 +0100866 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100867 if (!arg) {
868 dev_kfree_skb(resp);
869 return -ENOMEM;
870 }
871
872 arg->complete_cb = complete_cb;
873 arg->complete_cb_context = complete_cb_context;
874 arg->resp = resp;
875 arg->req = req;
876
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100877 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100878
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100879 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
880 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100881 if (rc < 0) {
882 dev_kfree_skb(resp);
883 kfree(arg);
884 }
885
886 return rc;
887}
888
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200889static void pn533_wq_cmd(struct work_struct *work)
890{
891 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
892 struct pn533_cmd *cmd;
893
894 mutex_lock(&dev->cmd_lock);
895
896 if (list_empty(&dev->cmd_queue)) {
897 dev->cmd_pending = 0;
898 mutex_unlock(&dev->cmd_lock);
899 return;
900 }
901
902 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
903
Szymon Janc60ad07a2012-10-25 17:29:45 +0200904 list_del(&cmd->queue);
905
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200906 mutex_unlock(&dev->cmd_lock);
907
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100908 __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
909 pn533_send_async_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200910
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200911 kfree(cmd);
912}
913
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300914struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100915 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300916 struct completion done;
917};
918
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100919static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
920 struct sk_buff *resp)
921{
922 struct pn533_sync_cmd_response *arg = _arg;
923
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100924 arg->resp = resp;
925 complete(&arg->done);
926
927 return 0;
928}
929
930/* pn533_send_cmd_sync
931 *
932 * Please note the req parameter is freed inside the function to
933 * limit a number of return value interpretations by the caller.
934 *
935 * 1. negative in case of error during TX path -> req should be freed
936 *
937 * 2. negative in case of error during RX path -> req should not be freed
938 * as it's been already freed at the begining of RX path by
939 * async_complete_cb.
940 *
941 * 3. valid pointer in case of succesfult RX path
942 *
943 * A caller has to check a return value with IS_ERR macro. If the test pass,
944 * the returned pointer is valid.
945 *
946 * */
947static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
948 struct sk_buff *req)
949{
950 int rc;
951 struct pn533_sync_cmd_response arg;
952
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100953 init_completion(&arg.done);
954
955 rc = pn533_send_cmd_async(dev, cmd_code, req,
956 pn533_send_sync_complete, &arg);
957 if (rc) {
958 dev_kfree_skb(req);
959 return ERR_PTR(rc);
960 }
961
962 wait_for_completion(&arg.done);
963
964 return arg.resp;
965}
966
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300967static void pn533_send_complete(struct urb *urb)
968{
969 struct pn533 *dev = urb->context;
970
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300971 switch (urb->status) {
972 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100973 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300974 case -ECONNRESET:
975 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100976 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100977 "The urb has been stopped (status %d)",
978 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300979 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100980 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300981 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100982 nfc_dev_err(&dev->interface->dev,
983 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300984 }
985}
986
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100987static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100988{
989 struct sk_buff *skb;
990
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100991 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100992 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100993 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100994
995 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100996 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100997
998 return skb;
999}
1000
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001001struct pn533_target_type_a {
1002 __be16 sens_res;
1003 u8 sel_res;
1004 u8 nfcid_len;
1005 u8 nfcid_data[];
1006} __packed;
1007
1008
1009#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1010#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1011#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1012
1013#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1014#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1015
1016#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1017#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1018
1019#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1020#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1021#define PN533_TYPE_A_SEL_PROT_DEP 2
1022#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1023
1024static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1025 int target_data_len)
1026{
1027 u8 ssd;
1028 u8 platconf;
1029
1030 if (target_data_len < sizeof(struct pn533_target_type_a))
1031 return false;
1032
1033 /* The lenght check of nfcid[] and ats[] are not being performed because
1034 the values are not being used */
1035
1036 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1037 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1038 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1039
1040 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001041 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1042 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1043 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001044 return false;
1045
1046 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1047 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1048 return false;
1049
1050 return true;
1051}
1052
1053static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1054 int tgt_data_len)
1055{
1056 struct pn533_target_type_a *tgt_type_a;
1057
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001058 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001059
1060 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1061 return -EPROTO;
1062
1063 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1064 case PN533_TYPE_A_SEL_PROT_MIFARE:
1065 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1066 break;
1067 case PN533_TYPE_A_SEL_PROT_ISO14443:
1068 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1069 break;
1070 case PN533_TYPE_A_SEL_PROT_DEP:
1071 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1072 break;
1073 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1074 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1075 NFC_PROTO_NFC_DEP_MASK;
1076 break;
1077 }
1078
1079 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1080 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001081 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1082 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001083
1084 return 0;
1085}
1086
1087struct pn533_target_felica {
1088 u8 pol_res;
1089 u8 opcode;
1090 u8 nfcid2[8];
1091 u8 pad[8];
1092 /* optional */
1093 u8 syst_code[];
1094} __packed;
1095
1096#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1097#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1098
1099static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1100 int target_data_len)
1101{
1102 if (target_data_len < sizeof(struct pn533_target_felica))
1103 return false;
1104
1105 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1106 return false;
1107
1108 return true;
1109}
1110
1111static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1112 int tgt_data_len)
1113{
1114 struct pn533_target_felica *tgt_felica;
1115
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001116 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001117
1118 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1119 return -EPROTO;
1120
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001121 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1122 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001123 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1124 else
1125 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1126
Samuel Ortiz79757542012-03-05 01:03:45 +01001127 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1128 nfc_tgt->sensf_res_len = 9;
1129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001130 return 0;
1131}
1132
1133struct pn533_target_jewel {
1134 __be16 sens_res;
1135 u8 jewelid[4];
1136} __packed;
1137
1138static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1139 int target_data_len)
1140{
1141 u8 ssd;
1142 u8 platconf;
1143
1144 if (target_data_len < sizeof(struct pn533_target_jewel))
1145 return false;
1146
1147 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1148 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1149 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1150
1151 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001152 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1153 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1154 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001155 return false;
1156
1157 return true;
1158}
1159
1160static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1161 int tgt_data_len)
1162{
1163 struct pn533_target_jewel *tgt_jewel;
1164
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001165 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001166
1167 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1168 return -EPROTO;
1169
1170 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1171 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001172 nfc_tgt->nfcid1_len = 4;
1173 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001174
1175 return 0;
1176}
1177
1178struct pn533_type_b_prot_info {
1179 u8 bitrate;
1180 u8 fsci_type;
1181 u8 fwi_adc_fo;
1182} __packed;
1183
1184#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1185#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1186#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1187
1188struct pn533_type_b_sens_res {
1189 u8 opcode;
1190 u8 nfcid[4];
1191 u8 appdata[4];
1192 struct pn533_type_b_prot_info prot_info;
1193} __packed;
1194
1195#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1196
1197struct pn533_target_type_b {
1198 struct pn533_type_b_sens_res sensb_res;
1199 u8 attrib_res_len;
1200 u8 attrib_res[];
1201} __packed;
1202
1203static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1204 int target_data_len)
1205{
1206 if (target_data_len < sizeof(struct pn533_target_type_b))
1207 return false;
1208
1209 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1210 return false;
1211
1212 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1213 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1214 return false;
1215
1216 return true;
1217}
1218
1219static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1220 int tgt_data_len)
1221{
1222 struct pn533_target_type_b *tgt_type_b;
1223
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001224 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001225
1226 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1227 return -EPROTO;
1228
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001229 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001230
1231 return 0;
1232}
1233
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001234static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1235 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001237 struct nfc_target nfc_tgt;
1238 int rc;
1239
1240 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001241 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001242
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001243 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001244 return -EPROTO;
1245
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001246 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1247
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001248 switch (dev->poll_mod_curr) {
1249 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001250 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001251 break;
1252 case PN533_POLL_MOD_212KBPS_FELICA:
1253 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001254 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001255 break;
1256 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001257 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001258 break;
1259 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001260 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001261 break;
1262 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001263 nfc_dev_err(&dev->interface->dev,
1264 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001265 return -EPROTO;
1266 }
1267
1268 if (rc)
1269 return rc;
1270
1271 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001272 nfc_dev_dbg(&dev->interface->dev,
1273 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001274 return -EAGAIN;
1275 }
1276
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001277 nfc_dev_dbg(&dev->interface->dev,
1278 "Target found - supported protocols: 0x%x",
1279 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001280
1281 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1282
1283 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1284
1285 return 0;
1286}
1287
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001288static inline void pn533_poll_next_mod(struct pn533 *dev)
1289{
1290 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1291}
1292
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001293static void pn533_poll_reset_mod_list(struct pn533 *dev)
1294{
1295 dev->poll_mod_count = 0;
1296}
1297
1298static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1299{
1300 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001301 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001302 dev->poll_mod_count++;
1303}
1304
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001305static void pn533_poll_create_mod_list(struct pn533 *dev,
1306 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001307{
1308 pn533_poll_reset_mod_list(dev);
1309
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001310 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1311 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1312 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001313 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1314
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001315 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1316 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1318 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1319 }
1320
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001321 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001322 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1323
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001324 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001326
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001327 if (tm_protocols)
1328 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001329}
1330
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001331static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001332{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001333 u8 nbtg, tg, *tgdata;
1334 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001335
1336 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1337
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001338 nbtg = resp->data[0];
1339 tg = resp->data[1];
1340 tgdata = &resp->data[2];
1341 tgdata_len = resp->len - 2; /* nbtg + tg */
1342
1343 if (nbtg) {
1344 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001345
1346 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001347 if (rc == 0) {
1348 pn533_poll_reset_mod_list(dev);
1349 return 0;
1350 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001351 }
1352
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001353 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001354}
1355
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001356static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001357{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001358 struct sk_buff *skb;
1359 u8 *felica, *nfcid3, *gb;
1360
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001361 u8 *gbytes = dev->gb;
1362 size_t gbytes_len = dev->gb_len;
1363
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001364 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1366 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1367 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001368
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001369 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1370 0x0, 0x0, 0x0,
1371 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001372
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001373 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1374 felica (18), nfcid3 (10), gb_len (1) */
1375 gbytes_len +
1376 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001377
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001378 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001379 if (!skb)
1380 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001381
1382 /* DEP support only */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001383 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001384
1385 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001386 memcpy(skb_put(skb, 6), mifare_params, 6);
1387
1388 /* Felica params */
1389 felica = skb_put(skb, 18);
1390 memcpy(felica, felica_params, 18);
1391 get_random_bytes(felica + 2, 6);
1392
1393 /* NFCID3 */
1394 nfcid3 = skb_put(skb, 10);
1395 memset(nfcid3, 0, 10);
1396 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001397
1398 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001399 *skb_put(skb, 1) = gbytes_len;
1400
1401 gb = skb_put(skb, gbytes_len);
1402 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001403
Samuel Ortizad3823c2012-05-30 23:54:55 +02001404 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001405 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001406
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001407 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001408}
1409
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001410#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001411#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1412static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001413 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001414{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001415 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001416
1417 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1418
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001419 if (IS_ERR(resp))
1420 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001421
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001422 status = resp->data[0];
1423 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001424
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001425 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001426 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001427 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001428 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001429 return 0;
1430 }
1431
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001432 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001433}
1434
1435static void pn533_wq_tg_get_data(struct work_struct *work)
1436{
1437 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001438
1439 struct sk_buff *skb;
1440 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001441
1442 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1443
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001444 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001445 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001446 return;
1447
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001448 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1449 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001450
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001451 if (rc < 0)
1452 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001453
1454 return;
1455}
1456
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001457#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001458static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001459{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001461 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001462 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001463
1464 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1465
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001466 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001467 return -EINVAL;
1468
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001469 mode = resp->data[0];
1470 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001471
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001472 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1473 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001474
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001475 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1476 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001477 comm_mode = NFC_COMM_ACTIVE;
1478
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001479 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001480 return -EOPNOTSUPP;
1481
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001482 gb = cmd + ATR_REQ_GB_OFFSET;
1483 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001484
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001485 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1486 comm_mode, gb, gb_len);
1487 if (rc < 0) {
1488 nfc_dev_err(&dev->interface->dev,
1489 "Error when signaling target activation");
1490 return rc;
1491 }
1492
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001493 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001494 queue_work(dev->wq, &dev->tg_work);
1495
1496 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001497}
1498
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001499static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001500{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001501 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502
1503 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1504
1505 /* An ack will cancel the last issued command (poll) */
1506 pn533_send_ack(dev, GFP_ATOMIC);
1507
1508 dev->cancel_listen = 1;
1509
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510 pn533_poll_next_mod(dev);
1511
1512 queue_work(dev->wq, &dev->poll_work);
1513}
1514
1515static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001516 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001517{
1518 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001519 int rc;
1520
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001521 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1522
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001523 if (IS_ERR(resp)) {
1524 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001525
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001526 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1527 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001529 if (rc == -ENOENT) {
1530 if (dev->poll_mod_count != 0)
1531 return rc;
1532 else
1533 goto stop_poll;
1534 } else if (rc < 0) {
1535 nfc_dev_err(&dev->interface->dev,
1536 "Error %d when running poll", rc);
1537 goto stop_poll;
1538 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001539 }
1540
1541 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1542
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001543 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545 rc = pn533_init_target_complete(dev, resp);
1546 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001547 }
1548
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001549 /* Initiator mode */
1550 rc = pn533_start_poll_complete(dev, resp);
1551 if (!rc)
1552 goto done;
1553
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001555 queue_work(dev->wq, &dev->poll_work);
1556
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001557done:
1558 dev_kfree_skb(resp);
1559 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560
1561stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001562 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1563
Samuel Ortizad3823c2012-05-30 23:54:55 +02001564 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001565 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001566 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001567}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001568
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001569static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1570 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001572 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001573
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001574 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001575 if (!skb)
1576 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001577
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001578 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001579
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001580 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581}
1582
1583static int pn533_send_poll_frame(struct pn533 *dev)
1584{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 struct pn533_poll_modulations *mod;
1586 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001587 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001588 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001589
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001590 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001591
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1593 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001594
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 if (mod->len == 0) { /* Listen mode */
1596 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001597 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001598 } else { /* Polling mode */
1599 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001600 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001601 }
1602
1603 if (!skb) {
1604 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1605 return -ENOMEM;
1606 }
1607
1608 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1609 NULL);
1610 if (rc < 0) {
1611 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001612 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001613 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001614
1615 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001616}
1617
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001618static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001619{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001620 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1621 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001622 int rc;
1623
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001624 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1625
1626 nfc_dev_dbg(&dev->interface->dev,
1627 "%s cancel_listen %d modulation len %d",
1628 __func__, dev->cancel_listen, cur_mod->len);
1629
1630 if (dev->cancel_listen == 1) {
1631 dev->cancel_listen = 0;
1632 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001633 }
1634
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001635 rc = pn533_send_poll_frame(dev);
1636 if (rc)
1637 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001638
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001639 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1640 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001641
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001642 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001643}
1644
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001645static int pn533_start_poll(struct nfc_dev *nfc_dev,
1646 u32 im_protocols, u32 tm_protocols)
1647{
1648 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1649
1650 nfc_dev_dbg(&dev->interface->dev,
1651 "%s: im protocols 0x%x tm protocols 0x%x",
1652 __func__, im_protocols, tm_protocols);
1653
1654 if (dev->tgt_active_prot) {
1655 nfc_dev_err(&dev->interface->dev,
1656 "Cannot poll with a target already activated");
1657 return -EBUSY;
1658 }
1659
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001660 if (dev->tgt_mode) {
1661 nfc_dev_err(&dev->interface->dev,
1662 "Cannot poll while already being activated");
1663 return -EBUSY;
1664 }
1665
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001666 if (tm_protocols) {
1667 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1668 if (dev->gb == NULL)
1669 tm_protocols = 0;
1670 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001671
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001672 dev->poll_mod_curr = 0;
1673 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1674 dev->poll_protocols = im_protocols;
1675 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001676
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001677 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001678}
1679
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001680static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1681{
1682 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1683
1684 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1685
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001686 del_timer(&dev->listen_timer);
1687
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001689 nfc_dev_dbg(&dev->interface->dev,
1690 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001691 return;
1692 }
1693
1694 /* An ack will cancel the last issued command (poll) */
1695 pn533_send_ack(dev, GFP_KERNEL);
1696
1697 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1698 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001699
1700 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001701}
1702
1703static int pn533_activate_target_nfcdep(struct pn533 *dev)
1704{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001705 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001706 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707 int rc;
1708
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001709 struct sk_buff *skb;
1710 struct sk_buff *resp;
1711
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001712 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1713
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001714 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001715 if (!skb)
1716 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001718 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1719 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001721 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1722 if (IS_ERR(resp))
1723 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001724
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001725 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001726 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001727 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001728 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001729 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001730 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001731
Samuel Ortiz541d9202011-12-14 16:43:10 +01001732 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001733 gt_len = resp->len - 16;
1734 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001735
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001736 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001737 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001738}
1739
Eric Lapuyade90099432012-05-07 12:31:13 +02001740static int pn533_activate_target(struct nfc_dev *nfc_dev,
1741 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001742{
1743 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1744 int rc;
1745
1746 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001747 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001748
1749 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001750 nfc_dev_err(&dev->interface->dev,
1751 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001752 return -EBUSY;
1753 }
1754
1755 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001756 nfc_dev_err(&dev->interface->dev,
1757 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001758 return -EBUSY;
1759 }
1760
1761 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001762 nfc_dev_err(&dev->interface->dev,
1763 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001764 return -EINVAL;
1765 }
1766
1767 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001768 nfc_dev_err(&dev->interface->dev,
1769 "Target doesn't support requested proto %u",
1770 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001771 return -EINVAL;
1772 }
1773
1774 if (protocol == NFC_PROTO_NFC_DEP) {
1775 rc = pn533_activate_target_nfcdep(dev);
1776 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001777 nfc_dev_err(&dev->interface->dev,
1778 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001779 return rc;
1780 }
1781 }
1782
1783 dev->tgt_active_prot = protocol;
1784 dev->tgt_available_prots = 0;
1785
1786 return 0;
1787}
1788
Eric Lapuyade90099432012-05-07 12:31:13 +02001789static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1790 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001791{
1792 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001793
1794 struct sk_buff *skb;
1795 struct sk_buff *resp;
1796
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001797 int rc;
1798
1799 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1800
1801 if (!dev->tgt_active_prot) {
1802 nfc_dev_err(&dev->interface->dev, "There is no active target");
1803 return;
1804 }
1805
1806 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001807 skb_queue_purge(&dev->resp_q);
1808
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001809 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001810 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001811 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001812
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001813 *skb_put(skb, 1) = 1; /* TG*/
1814
1815 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1816 if (IS_ERR(resp))
1817 return;
1818
1819 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001820 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001821 nfc_dev_err(&dev->interface->dev,
1822 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001824 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001825 return;
1826}
1827
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001828
1829static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001830 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001831{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001832 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001833 u8 target_gt_len;
1834 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001835 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001836
1837 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001838
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 if (IS_ERR(resp))
1840 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001841
1842 if (dev->tgt_available_prots &&
1843 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1844 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001845 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001846 rc = -EINVAL;
1847 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001848 }
1849
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001850 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1851
1852 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001853 if (rc != PN533_CMD_RET_SUCCESS) {
1854 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001855 "Bringing DEP link up failed %d", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001856 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001857 }
1858
1859 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001860 struct nfc_target nfc_target;
1861
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1863
1864 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001865 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001866 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1868 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001870
1871 dev->tgt_available_prots = 0;
1872 }
1873
1874 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1875
1876 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001877 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001878 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001879 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001880 if (rc == 0)
1881 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001882 dev->nfc_dev->targets[0].idx,
1883 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001884
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001885error:
1886 dev_kfree_skb(resp);
1887 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001888}
1889
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001890static int pn533_mod_to_baud(struct pn533 *dev)
1891{
1892 switch (dev->poll_mod_curr) {
1893 case PN533_POLL_MOD_106KBPS_A:
1894 return 0;
1895 case PN533_POLL_MOD_212KBPS_FELICA:
1896 return 1;
1897 case PN533_POLL_MOD_424KBPS_FELICA:
1898 return 2;
1899 default:
1900 return -EINVAL;
1901 }
1902}
1903
Samuel Ortizd7f33452012-05-29 21:45:21 +02001904#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001905static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001906 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001907{
1908 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001909 struct sk_buff *skb;
1910 int rc, baud, skb_len;
1911 u8 *next, *arg;
1912
Samuel Ortizd7f33452012-05-29 21:45:21 +02001913 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001914
1915 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1916
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001917 if (dev->poll_mod_count) {
1918 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001919 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001920 return -EBUSY;
1921 }
1922
1923 if (dev->tgt_active_prot) {
1924 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001925 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001926 return -EBUSY;
1927 }
1928
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001929 baud = pn533_mod_to_baud(dev);
1930 if (baud < 0) {
1931 nfc_dev_err(&dev->interface->dev,
1932 "Invalid curr modulation %d", dev->poll_mod_curr);
1933 return baud;
1934 }
1935
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001936 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001937 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001938 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001939
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001940 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001941 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001942 return -ENOMEM;
1943
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001944 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1945 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001946
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 next = skb_put(skb, 1); /* Next */
1948 *next = 0;
1949
1950 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1951 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1952 PASSIVE_DATA_LEN);
1953 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001954 }
1955
Samuel Ortiz47807d32012-03-05 01:03:50 +01001956 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001957 memcpy(skb_put(skb, gb_len), gb, gb_len);
1958 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001959 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001960 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001961 }
1962
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001963 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1964 if (!arg) {
1965 dev_kfree_skb(skb);
1966 return -ENOMEM;
1967 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001968
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001969 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001970
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001971 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1972 pn533_in_dep_link_up_complete, arg);
1973
1974 if (rc < 0) {
1975 dev_kfree_skb(skb);
1976 kfree(arg);
1977 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001978
1979 return rc;
1980}
1981
1982static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1983{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001984 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1985
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001986 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1987
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001988 pn533_poll_reset_mod_list(dev);
1989
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001990 if (dev->tgt_mode || dev->tgt_active_prot) {
1991 pn533_send_ack(dev, GFP_KERNEL);
1992 usb_kill_urb(dev->in_urb);
1993 }
1994
1995 dev->tgt_active_prot = 0;
1996 dev->tgt_mode = 0;
1997
1998 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001999
2000 return 0;
2001}
2002
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002003struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002004 data_exchange_cb_t cb;
2005 void *cb_context;
2006};
2007
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002008static struct sk_buff *pn533_build_response(struct pn533 *dev)
2009{
2010 struct sk_buff *skb, *tmp, *t;
2011 unsigned int skb_len = 0, tmp_len = 0;
2012
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002013 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002014
2015 if (skb_queue_empty(&dev->resp_q))
2016 return NULL;
2017
2018 if (skb_queue_len(&dev->resp_q) == 1) {
2019 skb = skb_dequeue(&dev->resp_q);
2020 goto out;
2021 }
2022
2023 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2024 skb_len += tmp->len;
2025
2026 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2027 __func__, skb_len);
2028
2029 skb = alloc_skb(skb_len, GFP_KERNEL);
2030 if (skb == NULL)
2031 goto out;
2032
2033 skb_put(skb, skb_len);
2034
2035 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2036 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2037 tmp_len += tmp->len;
2038 }
2039
2040out:
2041 skb_queue_purge(&dev->resp_q);
2042
2043 return skb;
2044}
2045
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002046static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002047 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002048{
2049 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002050 struct sk_buff *skb;
2051 int rc = 0;
2052 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002053
2054 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2055
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002056 if (IS_ERR(resp)) {
2057 rc = PTR_ERR(resp);
2058 goto _error;
2059 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002060
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002061 status = resp->data[0];
2062 ret = status & PN533_CMD_RET_MASK;
2063 mi = status & PN533_CMD_MI_MASK;
2064
2065 skb_pull(resp, sizeof(status));
2066
2067 if (ret != PN533_CMD_RET_SUCCESS) {
2068 nfc_dev_err(&dev->interface->dev,
2069 "PN533 reported error %d when exchanging data",
2070 ret);
2071 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002072 goto error;
2073 }
2074
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002075 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002076
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002077 if (mi) {
2078 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002079 queue_work(dev->wq, &dev->mi_work);
2080 return -EINPROGRESS;
2081 }
2082
2083 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002084 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002085 goto error;
2086
2087 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002088 kfree(arg);
2089 return 0;
2090
2091error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002092 dev_kfree_skb(resp);
2093_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002094 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002095 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002096 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002097 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002098}
2099
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002100static int pn533_transceive(struct nfc_dev *nfc_dev,
2101 struct nfc_target *target, struct sk_buff *skb,
2102 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002103{
2104 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002105 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002106 int rc;
2107
2108 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2109
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002110 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2111 /* TODO: Implement support to multi-part data exchange */
2112 nfc_dev_err(&dev->interface->dev,
2113 "Data length greater than the max allowed: %d",
2114 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2115 rc = -ENOSYS;
2116 goto error;
2117 }
2118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002119 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002120 nfc_dev_err(&dev->interface->dev,
2121 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002122 rc = -EINVAL;
2123 goto error;
2124 }
2125
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002126 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002127 if (!arg) {
2128 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002129 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130 }
2131
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002132 arg->cb = cb;
2133 arg->cb_context = cb_context;
2134
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002135 switch (dev->device_type) {
2136 case PN533_DEVICE_PASORI:
2137 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2138 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2139 skb,
2140 pn533_data_exchange_complete,
2141 arg);
2142
2143 break;
2144 }
2145 default:
2146 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2147
2148 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2149 skb, pn533_data_exchange_complete,
2150 arg);
2151
2152 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002153 }
2154
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002155 if (rc < 0) /* rc from send_async */
2156 goto error;
2157
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158 return 0;
2159
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002160error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002161 kfree(arg);
2162 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002163 return rc;
2164}
2165
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002166static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002168{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002169 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002170
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002171 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2172
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002173 if (IS_ERR(resp))
2174 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002175
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002176 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002177
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002178 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002179
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002180 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002181 nfc_tm_deactivated(dev->nfc_dev);
2182
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002183 dev->tgt_mode = 0;
2184
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002185 return 0;
2186 }
2187
2188 queue_work(dev->wq, &dev->tg_work);
2189
2190 return 0;
2191}
2192
2193static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2194{
2195 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002196 int rc;
2197
2198 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2199
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002200 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002201 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002202 "Data length greater than the max allowed: %d",
2203 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2204 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002205 }
2206
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002207 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2208 pn533_tm_send_complete, NULL);
2209 if (rc < 0)
2210 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002211
2212 return rc;
2213}
2214
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002215static void pn533_wq_mi_recv(struct work_struct *work)
2216{
2217 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002218
2219 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002220 int rc;
2221
2222 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2223
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002224 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002225 if (!skb)
2226 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002227
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002228 switch (dev->device_type) {
2229 case PN533_DEVICE_PASORI:
2230 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2231 rc = pn533_send_cmd_direct_async(dev,
2232 PN533_CMD_IN_COMM_THRU,
2233 skb,
2234 pn533_data_exchange_complete,
2235 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002236
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002237 break;
2238 }
2239 default:
2240 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002241
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002242 rc = pn533_send_cmd_direct_async(dev,
2243 PN533_CMD_IN_DATA_EXCHANGE,
2244 skb,
2245 pn533_data_exchange_complete,
2246 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002247
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002248 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002249 }
2250
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002251 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002252 return;
2253
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002254 nfc_dev_err(&dev->interface->dev,
2255 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002256
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002257 dev_kfree_skb(skb);
2258 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002259
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002260error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002261 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002262 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002263}
2264
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002265static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2266 u8 cfgdata_len)
2267{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002268 struct sk_buff *skb;
2269 struct sk_buff *resp;
2270
2271 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002272
2273 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2274
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002275 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002276
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002277 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002278 if (!skb)
2279 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002280
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002281 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2282 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002283
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002284 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2285 if (IS_ERR(resp))
2286 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002287
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002288 dev_kfree_skb(resp);
2289 return 0;
2290}
2291
2292static int pn533_get_firmware_version(struct pn533 *dev,
2293 struct pn533_fw_version *fv)
2294{
2295 struct sk_buff *skb;
2296 struct sk_buff *resp;
2297
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002298 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002299 if (!skb)
2300 return -ENOMEM;
2301
2302 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2303 if (IS_ERR(resp))
2304 return PTR_ERR(resp);
2305
2306 fv->ic = resp->data[0];
2307 fv->ver = resp->data[1];
2308 fv->rev = resp->data[2];
2309 fv->support = resp->data[3];
2310
2311 dev_kfree_skb(resp);
2312 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002313}
2314
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002315static int pn533_fw_reset(struct pn533 *dev)
2316{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002317 struct sk_buff *skb;
2318 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002319
2320 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2321
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002322 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002323 if (!skb)
2324 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002325
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002326 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002327
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002328 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2329 if (IS_ERR(resp))
2330 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002331
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002332 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002333
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002334 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002335}
2336
2337static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002338 .dev_up = NULL,
2339 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002340 .dep_link_up = pn533_dep_link_up,
2341 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002342 .start_poll = pn533_start_poll,
2343 .stop_poll = pn533_stop_poll,
2344 .activate_target = pn533_activate_target,
2345 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002346 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002347 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002348};
2349
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002350static int pn533_setup(struct pn533 *dev)
2351{
2352 struct pn533_config_max_retries max_retries;
2353 struct pn533_config_timing timing;
2354 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2355 int rc;
2356
2357 switch (dev->device_type) {
2358 case PN533_DEVICE_STD:
2359 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2360 max_retries.mx_rty_psl = 2;
2361 max_retries.mx_rty_passive_act =
2362 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2363
2364 timing.rfu = PN533_CONFIG_TIMING_102;
2365 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2366 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2367
2368 break;
2369
2370 case PN533_DEVICE_PASORI:
2371 max_retries.mx_rty_atr = 0x2;
2372 max_retries.mx_rty_psl = 0x1;
2373 max_retries.mx_rty_passive_act =
2374 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2375
2376 timing.rfu = PN533_CONFIG_TIMING_102;
2377 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2378 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2379
2380 break;
2381
2382 default:
2383 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2384 dev->device_type);
2385 return -EINVAL;
2386 }
2387
2388 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2389 (u8 *)&max_retries, sizeof(max_retries));
2390 if (rc) {
2391 nfc_dev_err(&dev->interface->dev,
2392 "Error on setting MAX_RETRIES config");
2393 return rc;
2394 }
2395
2396
2397 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2398 (u8 *)&timing, sizeof(timing));
2399 if (rc) {
2400 nfc_dev_err(&dev->interface->dev,
2401 "Error on setting RF timings");
2402 return rc;
2403 }
2404
2405 switch (dev->device_type) {
2406 case PN533_DEVICE_STD:
2407 break;
2408
2409 case PN533_DEVICE_PASORI:
2410 pn533_fw_reset(dev);
2411
2412 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2413 pasori_cfg, 3);
2414 if (rc) {
2415 nfc_dev_err(&dev->interface->dev,
2416 "Error while settings PASORI config");
2417 return rc;
2418 }
2419
2420 pn533_fw_reset(dev);
2421
2422 break;
2423 }
2424
2425 return 0;
2426}
2427
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428static int pn533_probe(struct usb_interface *interface,
2429 const struct usb_device_id *id)
2430{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002431 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002432 struct pn533 *dev;
2433 struct usb_host_interface *iface_desc;
2434 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002435 int in_endpoint = 0;
2436 int out_endpoint = 0;
2437 int rc = -ENOMEM;
2438 int i;
2439 u32 protocols;
2440
2441 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2442 if (!dev)
2443 return -ENOMEM;
2444
2445 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2446 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002447 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002448
2449 iface_desc = interface->cur_altsetting;
2450 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2451 endpoint = &iface_desc->endpoint[i].desc;
2452
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002453 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002454 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002456 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002457 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458 }
2459
2460 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002461 nfc_dev_err(&interface->dev,
2462 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463 rc = -ENODEV;
2464 goto error;
2465 }
2466
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002467 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002468 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2469
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002470 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002471 goto error;
2472
2473 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002474 usb_rcvbulkpipe(dev->udev, in_endpoint),
2475 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002476 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002477 usb_sndbulkpipe(dev->udev, out_endpoint),
2478 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002479
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002480 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2481 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002482 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002483 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002484 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002485 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002486 if (dev->wq == NULL)
2487 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002488
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002489 init_timer(&dev->listen_timer);
2490 dev->listen_timer.data = (unsigned long) dev;
2491 dev->listen_timer.function = pn533_listen_mode_timer;
2492
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002493 skb_queue_head_init(&dev->resp_q);
2494
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002495 INIT_LIST_HEAD(&dev->cmd_queue);
2496
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002497 usb_set_intfdata(interface, dev);
2498
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002499 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002500
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002501 dev->device_type = id->driver_info;
2502 switch (dev->device_type) {
2503 case PN533_DEVICE_STD:
2504 protocols = PN533_ALL_PROTOCOLS;
2505 break;
2506
2507 case PN533_DEVICE_PASORI:
2508 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2509 break;
2510
2511 default:
2512 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2513 dev->device_type);
2514 rc = -EINVAL;
2515 goto destroy_wq;
2516 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002517
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002518 memset(&fw_ver, 0, sizeof(fw_ver));
2519 rc = pn533_get_firmware_version(dev, &fw_ver);
2520 if (rc < 0)
2521 goto destroy_wq;
2522
2523 nfc_dev_info(&dev->interface->dev,
2524 "NXP PN533 firmware ver %d.%d now attached",
2525 fw_ver.ver, fw_ver.rev);
2526
2527
Samuel Ortize8753042011-08-19 15:47:11 +02002528 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002529 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002530 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002531 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002532 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002533 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002534 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002535
2536 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2537 nfc_set_drvdata(dev->nfc_dev, dev);
2538
2539 rc = nfc_register_device(dev->nfc_dev);
2540 if (rc)
2541 goto free_nfc_dev;
2542
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002543 rc = pn533_setup(dev);
2544 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002545 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002546
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002547 return 0;
2548
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002549unregister_nfc_dev:
2550 nfc_unregister_device(dev->nfc_dev);
2551
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002552free_nfc_dev:
2553 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002554
Samuel Ortiz4849f852012-04-10 19:43:17 +02002555destroy_wq:
2556 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002557error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002558 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002559 usb_free_urb(dev->out_urb);
2560 kfree(dev);
2561 return rc;
2562}
2563
2564static void pn533_disconnect(struct usb_interface *interface)
2565{
2566 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002567 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002568
2569 dev = usb_get_intfdata(interface);
2570 usb_set_intfdata(interface, NULL);
2571
2572 nfc_unregister_device(dev->nfc_dev);
2573 nfc_free_device(dev->nfc_dev);
2574
2575 usb_kill_urb(dev->in_urb);
2576 usb_kill_urb(dev->out_urb);
2577
Samuel Ortiz4849f852012-04-10 19:43:17 +02002578 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002579
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002580 skb_queue_purge(&dev->resp_q);
2581
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002582 del_timer(&dev->listen_timer);
2583
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002584 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2585 list_del(&cmd->queue);
2586 kfree(cmd);
2587 }
2588
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002589 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002590 usb_free_urb(dev->out_urb);
2591 kfree(dev);
2592
Dan Carpenter276556d2011-07-08 10:21:15 +03002593 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594}
2595
2596static struct usb_driver pn533_driver = {
2597 .name = "pn533",
2598 .probe = pn533_probe,
2599 .disconnect = pn533_disconnect,
2600 .id_table = pn533_table,
2601};
2602
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002603module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002604
2605MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2606 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2607MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2608MODULE_VERSION(VERSION);
2609MODULE_LICENSE("GPL");