blob: 697f154331ed4b951b9fb314c15a086b984f9add [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
30#define VERSION "0.1"
31
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +020041#define ACS_VENDOR_ID 0x072f
42#define ACR122U_PRODUCT_ID 0x2200
43
44#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46#define PN533_DEVICE_ACR122U 0x3
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020047
Samuel Ortiz01d719a2012-07-04 00:14:04 +020048#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
49 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
50 NFC_PROTO_NFC_DEP_MASK |\
51 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020052
53#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
54 NFC_PROTO_MIFARE_MASK | \
55 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020056 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020057 NFC_PROTO_NFC_DEP_MASK)
58
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030059static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020060 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
61 .idVendor = PN533_VENDOR_ID,
62 .idProduct = PN533_PRODUCT_ID,
63 .driver_info = PN533_DEVICE_STD,
64 },
65 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
66 .idVendor = SCM_VENDOR_ID,
67 .idProduct = SCL3711_PRODUCT_ID,
68 .driver_info = PN533_DEVICE_STD,
69 },
70 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
71 .idVendor = SONY_VENDOR_ID,
72 .idProduct = PASORI_PRODUCT_ID,
73 .driver_info = PN533_DEVICE_PASORI,
74 },
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +020075 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
76 .idVendor = ACS_VENDOR_ID,
77 .idProduct = ACR122U_PRODUCT_ID,
78 .driver_info = PN533_DEVICE_ACR122U,
79 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030080 { }
81};
82MODULE_DEVICE_TABLE(usb, pn533_table);
83
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020084/* How much time we spend listening for initiators */
85#define PN533_LISTEN_TIME 2
86
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020087/* Standard pn533 frame definitions */
88#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010089 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020090#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020091
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010092/*
93 * Max extended frame payload len, excluding TFI and CC
94 * which are already in PN533_FRAME_HEADER_LEN.
95 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020096#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010097
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020098#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010099 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200100#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
101#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300102
103/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200104#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300105
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200106/* standard frame identifier: in/out/error */
107#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
108#define PN533_STD_FRAME_DIR_OUT 0xD4
109#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300110
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200111/* ACS ACR122 pn533 frame definitions */
112#define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
113 + 2)
114#define PN533_ACR122_TX_FRAME_TAIL_LEN 0
115#define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
116 + 2)
117#define PN533_ACR122_RX_FRAME_TAIL_LEN 2
118#define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
119
120/* CCID messages types */
121#define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
122#define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
123
124#define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
125
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300126/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200127#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300128
129#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
130#define PN533_CMD_RF_CONFIGURATION 0x32
131#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200132#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
134#define PN533_CMD_IN_ATR 0x50
135#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100136#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300137
Samuel Ortizad3823c2012-05-30 23:54:55 +0200138#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200139#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200140#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100141#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200142
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
144
145/* PN533 Return codes */
146#define PN533_CMD_RET_MASK 0x3F
147#define PN533_CMD_MI_MASK 0x40
148#define PN533_CMD_RET_SUCCESS 0x00
149
150struct pn533;
151
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100152typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100154typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
155 struct sk_buff *resp);
156
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300157/* structs for pn533 commands */
158
159/* PN533_CMD_GET_FIRMWARE_VERSION */
160struct pn533_fw_version {
161 u8 ic;
162 u8 ver;
163 u8 rev;
164 u8 support;
165};
166
167/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200168#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300169#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200170#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300171
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200172#define PN533_CONFIG_TIMING_102 0xb
173#define PN533_CONFIG_TIMING_204 0xc
174#define PN533_CONFIG_TIMING_409 0xd
175#define PN533_CONFIG_TIMING_819 0xe
176
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300177#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
178#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
179
180struct pn533_config_max_retries {
181 u8 mx_rty_atr;
182 u8 mx_rty_psl;
183 u8 mx_rty_passive_act;
184} __packed;
185
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200186struct pn533_config_timing {
187 u8 rfu;
188 u8 atr_res_timeout;
189 u8 dep_timeout;
190} __packed;
191
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300192/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
193
194/* felica commands opcode */
195#define PN533_FELICA_OPC_SENSF_REQ 0
196#define PN533_FELICA_OPC_SENSF_RES 1
197/* felica SENSF_REQ parameters */
198#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
199#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
200#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
201#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
202
203/* type B initiator_data values */
204#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
205#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
206#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
207
208union pn533_cmd_poll_initdata {
209 struct {
210 u8 afi;
211 u8 polling_method;
212 } __packed type_b;
213 struct {
214 u8 opcode;
215 __be16 sc;
216 u8 rc;
217 u8 tsn;
218 } __packed felica;
219};
220
221/* Poll modulations */
222enum {
223 PN533_POLL_MOD_106KBPS_A,
224 PN533_POLL_MOD_212KBPS_FELICA,
225 PN533_POLL_MOD_424KBPS_FELICA,
226 PN533_POLL_MOD_106KBPS_JEWEL,
227 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200228 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300229
230 __PN533_POLL_MOD_AFTER_LAST,
231};
232#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
233
234struct pn533_poll_modulations {
235 struct {
236 u8 maxtg;
237 u8 brty;
238 union pn533_cmd_poll_initdata initiator_data;
239 } __packed data;
240 u8 len;
241};
242
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100243static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300244 [PN533_POLL_MOD_106KBPS_A] = {
245 .data = {
246 .maxtg = 1,
247 .brty = 0,
248 },
249 .len = 2,
250 },
251 [PN533_POLL_MOD_212KBPS_FELICA] = {
252 .data = {
253 .maxtg = 1,
254 .brty = 1,
255 .initiator_data.felica = {
256 .opcode = PN533_FELICA_OPC_SENSF_REQ,
257 .sc = PN533_FELICA_SENSF_SC_ALL,
258 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
259 .tsn = 0,
260 },
261 },
262 .len = 7,
263 },
264 [PN533_POLL_MOD_424KBPS_FELICA] = {
265 .data = {
266 .maxtg = 1,
267 .brty = 2,
268 .initiator_data.felica = {
269 .opcode = PN533_FELICA_OPC_SENSF_REQ,
270 .sc = PN533_FELICA_SENSF_SC_ALL,
271 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
272 .tsn = 0,
273 },
274 },
275 .len = 7,
276 },
277 [PN533_POLL_MOD_106KBPS_JEWEL] = {
278 .data = {
279 .maxtg = 1,
280 .brty = 4,
281 },
282 .len = 2,
283 },
284 [PN533_POLL_MOD_847KBPS_B] = {
285 .data = {
286 .maxtg = 1,
287 .brty = 8,
288 .initiator_data.type_b = {
289 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
290 .polling_method =
291 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
292 },
293 },
294 .len = 3,
295 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200296 [PN533_LISTEN_MOD] = {
297 .len = 0,
298 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300299};
300
301/* PN533_CMD_IN_ATR */
302
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303struct pn533_cmd_activate_response {
304 u8 status;
305 u8 nfcid3t[10];
306 u8 didt;
307 u8 bst;
308 u8 brt;
309 u8 to;
310 u8 ppt;
311 /* optional */
312 u8 gt[];
313} __packed;
314
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100315struct pn533_cmd_jump_dep_response {
316 u8 status;
317 u8 tg;
318 u8 nfcid3t[10];
319 u8 didt;
320 u8 bst;
321 u8 brt;
322 u8 to;
323 u8 ppt;
324 /* optional */
325 u8 gt[];
326} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300327
Samuel Ortizad3823c2012-05-30 23:54:55 +0200328
329/* PN533_TG_INIT_AS_TARGET */
330#define PN533_INIT_TARGET_PASSIVE 0x1
331#define PN533_INIT_TARGET_DEP 0x2
332
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200333#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
334#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
335#define PN533_INIT_TARGET_RESP_DEP 0x4
336
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200337enum pn533_protocol_type {
338 PN533_PROTO_REQ_ACK_RESP = 0,
339 PN533_PROTO_REQ_RESP
340};
341
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300342struct pn533 {
343 struct usb_device *udev;
344 struct usb_interface *interface;
345 struct nfc_dev *nfc_dev;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200346 u32 device_type;
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200347 enum pn533_protocol_type protocol_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300348
349 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300351
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200352 struct sk_buff_head resp_q;
353
Samuel Ortiz4849f852012-04-10 19:43:17 +0200354 struct workqueue_struct *wq;
355 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200356 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200357 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200358 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200359 struct work_struct tg_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200360
361 struct list_head cmd_queue;
362 struct pn533_cmd *cmd;
363 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200364 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300365
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100366 void *cmd_complete_mi_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300367
368 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
369 u8 poll_mod_count;
370 u8 poll_mod_curr;
371 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200372 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200373 struct timer_list listen_timer;
374 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200375
376 u8 *gb;
377 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300378
379 u8 tgt_available_prots;
380 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200381 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200382
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100383 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200384};
385
386struct pn533_cmd {
387 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200388 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200389 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100390 struct sk_buff *req;
391 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100392 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200393 pn533_send_async_complete_t complete_cb;
394 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300395};
396
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200397struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300398 u8 preamble;
399 __be16 start_frame;
400 u8 datalen;
401 u8 datalen_checksum;
402 u8 data[];
403} __packed;
404
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100405struct pn533_frame_ops {
406 void (*tx_frame_init)(void *frame, u8 cmd_code);
407 void (*tx_frame_finish)(void *frame);
408 void (*tx_update_payload_len)(void *frame, int len);
409 int tx_header_len;
410 int tx_tail_len;
411
412 bool (*rx_is_frame_valid)(void *frame);
413 int (*rx_frame_size)(void *frame);
414 int rx_header_len;
415 int rx_tail_len;
416
417 int max_payload_len;
418 u8 (*get_cmd_code)(void *frame);
419};
420
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200421struct pn533_acr122_ccid_hdr {
422 u8 type;
423 u32 datalen;
424 u8 slot;
425 u8 seq;
426 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
427 byte for reposnse msg */
428 u8 data[]; /* payload */
429} __packed;
430
431struct pn533_acr122_apdu_hdr {
432 u8 class;
433 u8 ins;
434 u8 p1;
435 u8 p2;
436} __packed;
437
438struct pn533_acr122_tx_frame {
439 struct pn533_acr122_ccid_hdr ccid;
440 struct pn533_acr122_apdu_hdr apdu;
441 u8 datalen;
442 u8 data[]; /* pn533 frame: TFI ... */
443} __packed;
444
445struct pn533_acr122_rx_frame {
446 struct pn533_acr122_ccid_hdr ccid;
447 u8 data[]; /* pn533 frame : TFI ... */
448} __packed;
449
450static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
451{
452 struct pn533_acr122_tx_frame *frame = _frame;
453
454 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
455 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
456 sizeof(datalen) */
457 frame->ccid.slot = 0;
458 frame->ccid.seq = 0;
459 frame->ccid.params[0] = 0;
460 frame->ccid.params[1] = 0;
461 frame->ccid.params[2] = 0;
462
463 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
464 frame->data[1] = cmd_code;
465 frame->datalen = 2; /* data[0] + data[1] */
466
467 frame->apdu.class = 0xFF;
468 frame->apdu.ins = 0;
469 frame->apdu.p1 = 0;
470 frame->apdu.p2 = 0;
471}
472
473static void pn533_acr122_tx_frame_finish(void *_frame)
474{
475 struct pn533_acr122_tx_frame *frame = _frame;
476
477 frame->ccid.datalen += frame->datalen;
478}
479
480static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
481{
482 struct pn533_acr122_tx_frame *frame = _frame;
483
484 frame->datalen += len;
485}
486
487static bool pn533_acr122_is_rx_frame_valid(void *_frame)
488{
489 struct pn533_acr122_rx_frame *frame = _frame;
490
491 if (frame->ccid.type != 0x83)
492 return false;
493
494 if (frame->data[frame->ccid.datalen - 2] == 0x63)
495 return false;
496
497 return true;
498}
499
500static int pn533_acr122_rx_frame_size(void *frame)
501{
502 struct pn533_acr122_rx_frame *f = frame;
503
504 /* f->ccid.datalen already includes tail length */
505 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
506}
507
508static u8 pn533_acr122_get_cmd_code(void *frame)
509{
510 struct pn533_acr122_rx_frame *f = frame;
511
512 return PN533_STD_FRAME_CMD(f);
513}
514
515static struct pn533_frame_ops pn533_acr122_frame_ops = {
516 .tx_frame_init = pn533_acr122_tx_frame_init,
517 .tx_frame_finish = pn533_acr122_tx_frame_finish,
518 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
519 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
520 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
521
522 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
523 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
524 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
525 .rx_frame_size = pn533_acr122_rx_frame_size,
526
527 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
528 .get_cmd_code = pn533_acr122_get_cmd_code,
529};
530
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300531/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200532static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300533{
534 return ~value + 1;
535}
536
537/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200538static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300539{
540 u8 sum = 0;
541 int i;
542
543 for (i = 0; i < datalen; i++)
544 sum += data[i];
545
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200546 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300547}
548
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200549static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300550{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200551 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100552
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300553 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200554 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
555 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
556 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300557 frame->datalen = 2;
558}
559
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200560static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300561{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200562 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100563
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200564 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200566 PN533_STD_FRAME_CHECKSUM(frame) =
567 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200569 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300570}
571
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200572static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100573{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200574 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100575
576 frame->datalen += len;
577}
578
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200579static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300580{
581 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200582 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300583
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200584 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585 return false;
586
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200587 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300588 if (checksum != frame->datalen_checksum)
589 return false;
590
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200591 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
592 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300593 return false;
594
595 return true;
596}
597
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200598static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200600 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300601 return false;
602
603 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
604 return false;
605
606 return true;
607}
608
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200609static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300610{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200611 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100612
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200613 return sizeof(struct pn533_std_frame) + f->datalen +
614 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100615}
616
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200617static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100618{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200619 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100620
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200621 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100622}
623
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100624static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200625 .tx_frame_init = pn533_std_tx_frame_init,
626 .tx_frame_finish = pn533_std_tx_frame_finish,
627 .tx_update_payload_len = pn533_std_tx_update_payload_len,
628 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
629 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100630
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200631 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
632 .rx_frame_size = pn533_std_rx_frame_size,
633 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
634 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100635
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200636 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
637 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100638};
639
640static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
641{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200642 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200643 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300644}
645
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300646static void pn533_recv_response(struct urb *urb)
647{
648 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200649 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100650 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200652 cmd->status = urb->status;
653
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300654 switch (urb->status) {
655 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100656 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657 case -ECONNRESET:
658 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100659 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100660 "The urb has been canceled (status %d)",
661 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200662 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100663 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300664 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100665 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100666 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200667 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300668 }
669
670 in_frame = dev->in_urb->transfer_buffer;
671
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100672 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100673 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
674 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100675
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100676 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300677 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200678 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200679 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300680 }
681
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100682 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100683 nfc_dev_err(&dev->interface->dev,
684 "It it not the response to the last command");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200685 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200686 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300687 }
688
Samuel Ortiz4849f852012-04-10 19:43:17 +0200689sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200690 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300691}
692
693static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
694{
695 dev->in_urb->complete = pn533_recv_response;
696
697 return usb_submit_urb(dev->in_urb, flags);
698}
699
700static void pn533_recv_ack(struct urb *urb)
701{
702 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200703 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200704 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300705 int rc;
706
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200707 cmd->status = urb->status;
708
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300709 switch (urb->status) {
710 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100711 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300712 case -ECONNRESET:
713 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100714 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100715 "The urb has been stopped (status %d)",
716 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200717 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100718 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300719 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100720 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100721 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200722 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300723 }
724
725 in_frame = dev->in_urb->transfer_buffer;
726
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200727 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300728 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200729 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200730 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300731 }
732
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300733 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
734 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100735 nfc_dev_err(&dev->interface->dev,
736 "usb_submit_urb failed with result %d", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200737 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200738 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300739 }
740
741 return;
742
Samuel Ortiz4849f852012-04-10 19:43:17 +0200743sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200744 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300745}
746
747static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
748{
749 dev->in_urb->complete = pn533_recv_ack;
750
751 return usb_submit_urb(dev->in_urb, flags);
752}
753
754static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
755{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200756 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100757 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300758 int rc;
759
760 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
761
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100762 dev->out_urb->transfer_buffer = ack;
763 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300764 rc = usb_submit_urb(dev->out_urb, flags);
765
766 return rc;
767}
768
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100769static int __pn533_send_frame_async(struct pn533 *dev,
770 struct sk_buff *out,
771 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200772 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300773{
774 int rc;
775
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100776 dev->out_urb->transfer_buffer = out->data;
777 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300778
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100779 dev->in_urb->transfer_buffer = in->data;
780 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300781
Samuel Ortize279f842013-03-20 11:27:57 +0100782 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
783 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100784
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100785 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300786 if (rc)
787 return rc;
788
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200789 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
790 /* request for response for sent packet directly */
791 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
792 if (rc)
793 goto error;
794 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
795 /* request for ACK if that's the case */
796 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
797 if (rc)
798 goto error;
799 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300800
801 return 0;
802
803error:
804 usb_unlink_urb(dev->out_urb);
805 return rc;
806}
807
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100808static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
809 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100810{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100811 /* payload is already there, just update datalen */
812 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100813 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100814
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100815
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100816 skb_push(skb, ops->tx_header_len);
817 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100818
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100819 ops->tx_frame_init(skb->data, cmd_code);
820 ops->tx_update_payload_len(skb->data, payload_len);
821 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100822}
823
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200824static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100825{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200826 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200827 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100828
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200829 struct sk_buff *req = cmd->req;
830 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100831
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100832 int rc;
833
834 dev_kfree_skb(req);
835
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100836 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200837 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
838 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100839 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200840 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100841 }
842
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100843 skb_put(resp, dev->ops->rx_frame_size(resp->data));
844 skb_pull(resp, dev->ops->rx_header_len);
845 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100846
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200847 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100848
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200849done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200850 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200851 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100852 return rc;
853}
854
855static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
856 struct sk_buff *req, struct sk_buff *resp,
857 int resp_len,
858 pn533_send_async_complete_t complete_cb,
859 void *complete_cb_context)
860{
861 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100862 int rc = 0;
863
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100864 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100865
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200866 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
867 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100868 return -ENOMEM;
869
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200870 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200871 cmd->req = req;
872 cmd->resp = resp;
873 cmd->resp_len = resp_len;
874 cmd->complete_cb = complete_cb;
875 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100876
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100877 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100878
879 mutex_lock(&dev->cmd_lock);
880
881 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200882 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100883 if (rc)
884 goto error;
885
886 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200887 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100888 goto unlock;
889 }
890
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100891 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
892 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100893
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100894 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100895 list_add_tail(&cmd->queue, &dev->cmd_queue);
896
897 goto unlock;
898
899error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200900 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100901unlock:
902 mutex_unlock(&dev->cmd_lock);
903 return rc;
904}
905
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100906static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
907 struct sk_buff *req,
908 pn533_send_async_complete_t complete_cb,
909 void *complete_cb_context)
910{
911 struct sk_buff *resp;
912 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100913 int resp_len = dev->ops->rx_header_len +
914 dev->ops->max_payload_len +
915 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100916
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100917 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
918 if (!resp)
919 return -ENOMEM;
920
921 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
922 complete_cb_context);
923 if (rc)
924 dev_kfree_skb(resp);
925
926 return rc;
927}
928
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100929static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
930 struct sk_buff *req,
931 pn533_send_async_complete_t complete_cb,
932 void *complete_cb_context)
933{
934 struct sk_buff *resp;
935 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100936 int resp_len = dev->ops->rx_header_len +
937 dev->ops->max_payload_len +
938 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100939
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100940 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100941 if (!resp)
942 return -ENOMEM;
943
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100944 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
945 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100946 if (rc)
947 dev_kfree_skb(resp);
948
949 return rc;
950}
951
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100952/*
953 * pn533_send_cmd_direct_async
954 *
955 * The function sends a piority cmd directly to the chip omiting the cmd
956 * queue. It's intended to be used by chaining mechanism of received responses
957 * where the host has to request every single chunk of data before scheduling
958 * next cmd from the queue.
959 */
960static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
961 struct sk_buff *req,
962 pn533_send_async_complete_t complete_cb,
963 void *complete_cb_context)
964{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100965 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200966 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100967 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100968 int resp_len = dev->ops->rx_header_len +
969 dev->ops->max_payload_len +
970 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100971
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100972 resp = alloc_skb(resp_len, GFP_KERNEL);
973 if (!resp)
974 return -ENOMEM;
975
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200976 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
977 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100978 dev_kfree_skb(resp);
979 return -ENOMEM;
980 }
981
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200982 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200983 cmd->req = req;
984 cmd->resp = resp;
985 cmd->resp_len = resp_len;
986 cmd->complete_cb = complete_cb;
987 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100988
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100989 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100990
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200991 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100992 if (rc < 0) {
993 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200994 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200995 } else {
996 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100997 }
998
999 return rc;
1000}
1001
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +02001002static void pn533_wq_cmd_complete(struct work_struct *work)
1003{
1004 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1005 int rc;
1006
1007 rc = pn533_send_async_complete(dev);
1008 if (rc != -EINPROGRESS)
1009 queue_work(dev->wq, &dev->cmd_work);
1010}
1011
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001012static void pn533_wq_cmd(struct work_struct *work)
1013{
1014 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1015 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001016 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001017
1018 mutex_lock(&dev->cmd_lock);
1019
1020 if (list_empty(&dev->cmd_queue)) {
1021 dev->cmd_pending = 0;
1022 mutex_unlock(&dev->cmd_lock);
1023 return;
1024 }
1025
1026 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1027
Szymon Janc60ad07a2012-10-25 17:29:45 +02001028 list_del(&cmd->queue);
1029
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001030 mutex_unlock(&dev->cmd_lock);
1031
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001032 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001033 if (rc < 0) {
1034 dev_kfree_skb(cmd->req);
1035 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001036 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001037 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001038 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001039
1040 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001041}
1042
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001043struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001044 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001045 struct completion done;
1046};
1047
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001048static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1049 struct sk_buff *resp)
1050{
1051 struct pn533_sync_cmd_response *arg = _arg;
1052
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001053 arg->resp = resp;
1054 complete(&arg->done);
1055
1056 return 0;
1057}
1058
1059/* pn533_send_cmd_sync
1060 *
1061 * Please note the req parameter is freed inside the function to
1062 * limit a number of return value interpretations by the caller.
1063 *
1064 * 1. negative in case of error during TX path -> req should be freed
1065 *
1066 * 2. negative in case of error during RX path -> req should not be freed
1067 * as it's been already freed at the begining of RX path by
1068 * async_complete_cb.
1069 *
1070 * 3. valid pointer in case of succesfult RX path
1071 *
1072 * A caller has to check a return value with IS_ERR macro. If the test pass,
1073 * the returned pointer is valid.
1074 *
1075 * */
1076static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1077 struct sk_buff *req)
1078{
1079 int rc;
1080 struct pn533_sync_cmd_response arg;
1081
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001082 init_completion(&arg.done);
1083
1084 rc = pn533_send_cmd_async(dev, cmd_code, req,
1085 pn533_send_sync_complete, &arg);
1086 if (rc) {
1087 dev_kfree_skb(req);
1088 return ERR_PTR(rc);
1089 }
1090
1091 wait_for_completion(&arg.done);
1092
1093 return arg.resp;
1094}
1095
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001096static void pn533_send_complete(struct urb *urb)
1097{
1098 struct pn533 *dev = urb->context;
1099
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001100 switch (urb->status) {
1101 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001102 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001103 case -ECONNRESET:
1104 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001105 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001106 "The urb has been stopped (status %d)",
1107 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001108 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001109 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001110 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001111 nfc_dev_err(&dev->interface->dev,
1112 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001113 }
1114}
1115
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001116static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001117{
1118 struct sk_buff *skb;
1119
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001120 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001121 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001122 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001123
1124 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001125 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001126
1127 return skb;
1128}
1129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001130struct pn533_target_type_a {
1131 __be16 sens_res;
1132 u8 sel_res;
1133 u8 nfcid_len;
1134 u8 nfcid_data[];
1135} __packed;
1136
1137
1138#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1139#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1140#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1141
1142#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1143#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1144
1145#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1146#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1147
1148#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1149#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1150#define PN533_TYPE_A_SEL_PROT_DEP 2
1151#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1152
1153static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1154 int target_data_len)
1155{
1156 u8 ssd;
1157 u8 platconf;
1158
1159 if (target_data_len < sizeof(struct pn533_target_type_a))
1160 return false;
1161
1162 /* The lenght check of nfcid[] and ats[] are not being performed because
1163 the values are not being used */
1164
1165 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1166 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1167 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1168
1169 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001170 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1171 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1172 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001173 return false;
1174
1175 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1176 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1177 return false;
1178
1179 return true;
1180}
1181
1182static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1183 int tgt_data_len)
1184{
1185 struct pn533_target_type_a *tgt_type_a;
1186
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001187 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001188
1189 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1190 return -EPROTO;
1191
1192 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1193 case PN533_TYPE_A_SEL_PROT_MIFARE:
1194 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1195 break;
1196 case PN533_TYPE_A_SEL_PROT_ISO14443:
1197 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1198 break;
1199 case PN533_TYPE_A_SEL_PROT_DEP:
1200 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1201 break;
1202 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1203 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1204 NFC_PROTO_NFC_DEP_MASK;
1205 break;
1206 }
1207
1208 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1209 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001210 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1211 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001212
1213 return 0;
1214}
1215
1216struct pn533_target_felica {
1217 u8 pol_res;
1218 u8 opcode;
1219 u8 nfcid2[8];
1220 u8 pad[8];
1221 /* optional */
1222 u8 syst_code[];
1223} __packed;
1224
1225#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1226#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1227
1228static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1229 int target_data_len)
1230{
1231 if (target_data_len < sizeof(struct pn533_target_felica))
1232 return false;
1233
1234 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1235 return false;
1236
1237 return true;
1238}
1239
1240static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1241 int tgt_data_len)
1242{
1243 struct pn533_target_felica *tgt_felica;
1244
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001245 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001246
1247 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1248 return -EPROTO;
1249
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001250 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1251 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001252 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1253 else
1254 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1255
Samuel Ortiz79757542012-03-05 01:03:45 +01001256 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1257 nfc_tgt->sensf_res_len = 9;
1258
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001259 return 0;
1260}
1261
1262struct pn533_target_jewel {
1263 __be16 sens_res;
1264 u8 jewelid[4];
1265} __packed;
1266
1267static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1268 int target_data_len)
1269{
1270 u8 ssd;
1271 u8 platconf;
1272
1273 if (target_data_len < sizeof(struct pn533_target_jewel))
1274 return false;
1275
1276 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1277 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1278 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1279
1280 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001281 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1282 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1283 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001284 return false;
1285
1286 return true;
1287}
1288
1289static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1290 int tgt_data_len)
1291{
1292 struct pn533_target_jewel *tgt_jewel;
1293
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001294 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295
1296 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1297 return -EPROTO;
1298
1299 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1300 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001301 nfc_tgt->nfcid1_len = 4;
1302 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001303
1304 return 0;
1305}
1306
1307struct pn533_type_b_prot_info {
1308 u8 bitrate;
1309 u8 fsci_type;
1310 u8 fwi_adc_fo;
1311} __packed;
1312
1313#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1314#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1315#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1316
1317struct pn533_type_b_sens_res {
1318 u8 opcode;
1319 u8 nfcid[4];
1320 u8 appdata[4];
1321 struct pn533_type_b_prot_info prot_info;
1322} __packed;
1323
1324#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1325
1326struct pn533_target_type_b {
1327 struct pn533_type_b_sens_res sensb_res;
1328 u8 attrib_res_len;
1329 u8 attrib_res[];
1330} __packed;
1331
1332static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1333 int target_data_len)
1334{
1335 if (target_data_len < sizeof(struct pn533_target_type_b))
1336 return false;
1337
1338 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1339 return false;
1340
1341 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1342 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1343 return false;
1344
1345 return true;
1346}
1347
1348static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1349 int tgt_data_len)
1350{
1351 struct pn533_target_type_b *tgt_type_b;
1352
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001353 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001354
1355 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1356 return -EPROTO;
1357
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001358 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001359
1360 return 0;
1361}
1362
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001363static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1364 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001365{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001366 struct nfc_target nfc_tgt;
1367 int rc;
1368
1369 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001370 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001371
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001372 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001373 return -EPROTO;
1374
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001375 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1376
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001377 switch (dev->poll_mod_curr) {
1378 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001379 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001380 break;
1381 case PN533_POLL_MOD_212KBPS_FELICA:
1382 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001383 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001384 break;
1385 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001386 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001387 break;
1388 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001389 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001390 break;
1391 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001392 nfc_dev_err(&dev->interface->dev,
1393 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001394 return -EPROTO;
1395 }
1396
1397 if (rc)
1398 return rc;
1399
1400 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001401 nfc_dev_dbg(&dev->interface->dev,
1402 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001403 return -EAGAIN;
1404 }
1405
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001406 nfc_dev_dbg(&dev->interface->dev,
1407 "Target found - supported protocols: 0x%x",
1408 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001409
1410 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1411
1412 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1413
1414 return 0;
1415}
1416
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001417static inline void pn533_poll_next_mod(struct pn533 *dev)
1418{
1419 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1420}
1421
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001422static void pn533_poll_reset_mod_list(struct pn533 *dev)
1423{
1424 dev->poll_mod_count = 0;
1425}
1426
1427static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1428{
1429 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001430 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001431 dev->poll_mod_count++;
1432}
1433
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001434static void pn533_poll_create_mod_list(struct pn533 *dev,
1435 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001436{
1437 pn533_poll_reset_mod_list(dev);
1438
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001439 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1440 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1441 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001442 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1443
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001444 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1445 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001446 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1447 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1448 }
1449
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001450 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001451 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1452
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001453 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001454 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001455
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001456 if (tm_protocols)
1457 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001458}
1459
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001461{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 u8 nbtg, tg, *tgdata;
1463 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001464
1465 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1466
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 nbtg = resp->data[0];
1468 tg = resp->data[1];
1469 tgdata = &resp->data[2];
1470 tgdata_len = resp->len - 2; /* nbtg + tg */
1471
1472 if (nbtg) {
1473 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001474
1475 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001476 if (rc == 0) {
1477 pn533_poll_reset_mod_list(dev);
1478 return 0;
1479 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001480 }
1481
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001482 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001483}
1484
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001485static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001486{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001487 struct sk_buff *skb;
1488 u8 *felica, *nfcid3, *gb;
1489
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001490 u8 *gbytes = dev->gb;
1491 size_t gbytes_len = dev->gb_len;
1492
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001493 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1494 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1495 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1496 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001497
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001498 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1499 0x0, 0x0, 0x0,
1500 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001501
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001502 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1503 felica (18), nfcid3 (10), gb_len (1) */
1504 gbytes_len +
1505 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001506
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001507 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001508 if (!skb)
1509 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001510
1511 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001512 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001513
1514 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001515 memcpy(skb_put(skb, 6), mifare_params, 6);
1516
1517 /* Felica params */
1518 felica = skb_put(skb, 18);
1519 memcpy(felica, felica_params, 18);
1520 get_random_bytes(felica + 2, 6);
1521
1522 /* NFCID3 */
1523 nfcid3 = skb_put(skb, 10);
1524 memset(nfcid3, 0, 10);
1525 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001526
1527 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001528 *skb_put(skb, 1) = gbytes_len;
1529
1530 gb = skb_put(skb, gbytes_len);
1531 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001532
Samuel Ortizad3823c2012-05-30 23:54:55 +02001533 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001534 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001535
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001536 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001537}
1538
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001539#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001540#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1541static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001542 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001543{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001544 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001545
1546 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1547
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001548 if (IS_ERR(resp))
1549 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001550
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001551 status = resp->data[0];
1552 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001553
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001554 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001555 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001556 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001557 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001558 return 0;
1559 }
1560
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001561 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001562}
1563
1564static void pn533_wq_tg_get_data(struct work_struct *work)
1565{
1566 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001567
1568 struct sk_buff *skb;
1569 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001570
1571 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1572
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001573 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001574 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001575 return;
1576
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001577 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1578 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001579
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001580 if (rc < 0)
1581 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001582
1583 return;
1584}
1585
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001586#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001587static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001588{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001589 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001590 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001591 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001592
1593 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1594
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001596 return -EINVAL;
1597
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001598 mode = resp->data[0];
1599 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001600
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001601 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1602 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001603
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001604 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1605 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001606 comm_mode = NFC_COMM_ACTIVE;
1607
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001608 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001609 return -EOPNOTSUPP;
1610
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001611 gb = cmd + ATR_REQ_GB_OFFSET;
1612 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001613
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001614 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1615 comm_mode, gb, gb_len);
1616 if (rc < 0) {
1617 nfc_dev_err(&dev->interface->dev,
1618 "Error when signaling target activation");
1619 return rc;
1620 }
1621
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001622 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001623 queue_work(dev->wq, &dev->tg_work);
1624
1625 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001626}
1627
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001628static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001629{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001630 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001631
1632 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1633
1634 /* An ack will cancel the last issued command (poll) */
1635 pn533_send_ack(dev, GFP_ATOMIC);
1636
1637 dev->cancel_listen = 1;
1638
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001639 pn533_poll_next_mod(dev);
1640
1641 queue_work(dev->wq, &dev->poll_work);
1642}
1643
1644static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001645 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646{
1647 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001648 int rc;
1649
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001650 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1651
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001652 if (IS_ERR(resp)) {
1653 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001654
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001655 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1656 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001657
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001658 if (rc == -ENOENT) {
1659 if (dev->poll_mod_count != 0)
1660 return rc;
1661 else
1662 goto stop_poll;
1663 } else if (rc < 0) {
1664 nfc_dev_err(&dev->interface->dev,
1665 "Error %d when running poll", rc);
1666 goto stop_poll;
1667 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001668 }
1669
1670 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1671
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001672 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001673 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001674 rc = pn533_init_target_complete(dev, resp);
1675 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001676 }
1677
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001678 /* Initiator mode */
1679 rc = pn533_start_poll_complete(dev, resp);
1680 if (!rc)
1681 goto done;
1682
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001683 if (!dev->poll_mod_count) {
1684 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1685 goto done;
1686 }
1687
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001688 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001689 queue_work(dev->wq, &dev->poll_work);
1690
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001691done:
1692 dev_kfree_skb(resp);
1693 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001694
1695stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001696 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1697
Samuel Ortizad3823c2012-05-30 23:54:55 +02001698 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001699 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001700 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001701}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001702
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001703static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1704 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001705{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001706 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001707
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001708 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001709 if (!skb)
1710 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001711
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001712 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001713
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001714 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001715}
1716
1717static int pn533_send_poll_frame(struct pn533 *dev)
1718{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001719 struct pn533_poll_modulations *mod;
1720 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001721 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001722 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001723
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001724 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001725
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001726 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1727 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001728
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001729 if (mod->len == 0) { /* Listen mode */
1730 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001731 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001732 } else { /* Polling mode */
1733 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001734 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001735 }
1736
1737 if (!skb) {
1738 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1739 return -ENOMEM;
1740 }
1741
1742 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1743 NULL);
1744 if (rc < 0) {
1745 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001746 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001747 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001748
1749 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001750}
1751
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001752static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001753{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001754 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1755 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001756 int rc;
1757
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001758 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1759
1760 nfc_dev_dbg(&dev->interface->dev,
1761 "%s cancel_listen %d modulation len %d",
1762 __func__, dev->cancel_listen, cur_mod->len);
1763
1764 if (dev->cancel_listen == 1) {
1765 dev->cancel_listen = 0;
1766 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 }
1768
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001769 rc = pn533_send_poll_frame(dev);
1770 if (rc)
1771 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001772
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001773 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1774 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001775
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001776 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001777}
1778
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001779static int pn533_start_poll(struct nfc_dev *nfc_dev,
1780 u32 im_protocols, u32 tm_protocols)
1781{
1782 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1783
1784 nfc_dev_dbg(&dev->interface->dev,
1785 "%s: im protocols 0x%x tm protocols 0x%x",
1786 __func__, im_protocols, tm_protocols);
1787
1788 if (dev->tgt_active_prot) {
1789 nfc_dev_err(&dev->interface->dev,
1790 "Cannot poll with a target already activated");
1791 return -EBUSY;
1792 }
1793
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001794 if (dev->tgt_mode) {
1795 nfc_dev_err(&dev->interface->dev,
1796 "Cannot poll while already being activated");
1797 return -EBUSY;
1798 }
1799
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001800 if (tm_protocols) {
1801 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1802 if (dev->gb == NULL)
1803 tm_protocols = 0;
1804 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001805
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001806 dev->poll_mod_curr = 0;
1807 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1808 dev->poll_protocols = im_protocols;
1809 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001810
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001811 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001812}
1813
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1815{
1816 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1817
1818 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1819
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001820 del_timer(&dev->listen_timer);
1821
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001822 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001823 nfc_dev_dbg(&dev->interface->dev,
1824 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001825 return;
1826 }
1827
1828 /* An ack will cancel the last issued command (poll) */
1829 pn533_send_ack(dev, GFP_KERNEL);
1830
1831 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1832 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001833
1834 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001835}
1836
1837static int pn533_activate_target_nfcdep(struct pn533 *dev)
1838{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001839 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001840 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001841 int rc;
1842
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001843 struct sk_buff *skb;
1844 struct sk_buff *resp;
1845
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001846 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1847
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001848 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001849 if (!skb)
1850 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001851
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001852 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1853 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001854
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001855 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1856 if (IS_ERR(resp))
1857 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001858
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001859 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001860 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001861 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001862 nfc_dev_err(&dev->interface->dev,
1863 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001864 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001865 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001866 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001867
Samuel Ortiz541d9202011-12-14 16:43:10 +01001868 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001869 gt_len = resp->len - 16;
1870 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001871
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001872 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001873 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001874}
1875
Eric Lapuyade90099432012-05-07 12:31:13 +02001876static int pn533_activate_target(struct nfc_dev *nfc_dev,
1877 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001878{
1879 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1880 int rc;
1881
1882 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001883 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001884
1885 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001886 nfc_dev_err(&dev->interface->dev,
1887 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001888 return -EBUSY;
1889 }
1890
1891 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001892 nfc_dev_err(&dev->interface->dev,
1893 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001894 return -EBUSY;
1895 }
1896
1897 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001898 nfc_dev_err(&dev->interface->dev,
1899 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001900 return -EINVAL;
1901 }
1902
1903 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001904 nfc_dev_err(&dev->interface->dev,
1905 "Target doesn't support requested proto %u",
1906 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001907 return -EINVAL;
1908 }
1909
1910 if (protocol == NFC_PROTO_NFC_DEP) {
1911 rc = pn533_activate_target_nfcdep(dev);
1912 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001913 nfc_dev_err(&dev->interface->dev,
1914 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001915 return rc;
1916 }
1917 }
1918
1919 dev->tgt_active_prot = protocol;
1920 dev->tgt_available_prots = 0;
1921
1922 return 0;
1923}
1924
Eric Lapuyade90099432012-05-07 12:31:13 +02001925static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1926 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001927{
1928 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001929
1930 struct sk_buff *skb;
1931 struct sk_buff *resp;
1932
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001933 int rc;
1934
1935 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1936
1937 if (!dev->tgt_active_prot) {
1938 nfc_dev_err(&dev->interface->dev, "There is no active target");
1939 return;
1940 }
1941
1942 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001943 skb_queue_purge(&dev->resp_q);
1944
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001945 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001946 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001947 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001948
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001949 *skb_put(skb, 1) = 1; /* TG*/
1950
1951 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1952 if (IS_ERR(resp))
1953 return;
1954
1955 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001956 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001957 nfc_dev_err(&dev->interface->dev,
1958 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001959
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001960 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001961 return;
1962}
1963
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001964
1965static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001967{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001968 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001969 u8 target_gt_len;
1970 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001971 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001972
1973 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001974
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001975 if (IS_ERR(resp))
1976 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001977
1978 if (dev->tgt_available_prots &&
1979 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1980 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001981 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001982 rc = -EINVAL;
1983 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001984 }
1985
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001986 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1987
1988 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001989 if (rc != PN533_CMD_RET_SUCCESS) {
1990 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001991 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001992 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001993 }
1994
1995 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001996 struct nfc_target nfc_target;
1997
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001998 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1999
2000 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01002001 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002002 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002003 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2004 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002005 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002006
2007 dev->tgt_available_prots = 0;
2008 }
2009
2010 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2011
2012 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002013 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002014 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002015 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002016 if (rc == 0)
2017 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002018 dev->nfc_dev->targets[0].idx,
2019 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002020
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002021error:
2022 dev_kfree_skb(resp);
2023 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002024}
2025
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002026static int pn533_mod_to_baud(struct pn533 *dev)
2027{
2028 switch (dev->poll_mod_curr) {
2029 case PN533_POLL_MOD_106KBPS_A:
2030 return 0;
2031 case PN533_POLL_MOD_212KBPS_FELICA:
2032 return 1;
2033 case PN533_POLL_MOD_424KBPS_FELICA:
2034 return 2;
2035 default:
2036 return -EINVAL;
2037 }
2038}
2039
Samuel Ortizd7f33452012-05-29 21:45:21 +02002040#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02002041static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002042 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002043{
2044 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002045 struct sk_buff *skb;
2046 int rc, baud, skb_len;
2047 u8 *next, *arg;
2048
Samuel Ortizd7f33452012-05-29 21:45:21 +02002049 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002050
2051 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2052
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002053 if (dev->poll_mod_count) {
2054 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002055 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002056 return -EBUSY;
2057 }
2058
2059 if (dev->tgt_active_prot) {
2060 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002061 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002062 return -EBUSY;
2063 }
2064
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002065 baud = pn533_mod_to_baud(dev);
2066 if (baud < 0) {
2067 nfc_dev_err(&dev->interface->dev,
2068 "Invalid curr modulation %d", dev->poll_mod_curr);
2069 return baud;
2070 }
2071
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002072 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02002073 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002074 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002075
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002076 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002077 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002078 return -ENOMEM;
2079
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002080 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2081 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002082
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002083 next = skb_put(skb, 1); /* Next */
2084 *next = 0;
2085
2086 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
2087 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
2088 PASSIVE_DATA_LEN);
2089 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002090 }
2091
Samuel Ortiz47807d32012-03-05 01:03:50 +01002092 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002093 memcpy(skb_put(skb, gb_len), gb, gb_len);
2094 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002095 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002096 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002097 }
2098
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002099 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2100 if (!arg) {
2101 dev_kfree_skb(skb);
2102 return -ENOMEM;
2103 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002104
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002105 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002106
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002107 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2108 pn533_in_dep_link_up_complete, arg);
2109
2110 if (rc < 0) {
2111 dev_kfree_skb(skb);
2112 kfree(arg);
2113 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002114
2115 return rc;
2116}
2117
2118static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2119{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002120 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2121
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002122 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2123
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002124 pn533_poll_reset_mod_list(dev);
2125
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002126 if (dev->tgt_mode || dev->tgt_active_prot) {
2127 pn533_send_ack(dev, GFP_KERNEL);
2128 usb_kill_urb(dev->in_urb);
2129 }
2130
2131 dev->tgt_active_prot = 0;
2132 dev->tgt_mode = 0;
2133
2134 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002135
2136 return 0;
2137}
2138
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002139struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002140 data_exchange_cb_t cb;
2141 void *cb_context;
2142};
2143
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002144static struct sk_buff *pn533_build_response(struct pn533 *dev)
2145{
2146 struct sk_buff *skb, *tmp, *t;
2147 unsigned int skb_len = 0, tmp_len = 0;
2148
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002149 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002150
2151 if (skb_queue_empty(&dev->resp_q))
2152 return NULL;
2153
2154 if (skb_queue_len(&dev->resp_q) == 1) {
2155 skb = skb_dequeue(&dev->resp_q);
2156 goto out;
2157 }
2158
2159 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2160 skb_len += tmp->len;
2161
2162 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2163 __func__, skb_len);
2164
2165 skb = alloc_skb(skb_len, GFP_KERNEL);
2166 if (skb == NULL)
2167 goto out;
2168
2169 skb_put(skb, skb_len);
2170
2171 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2172 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2173 tmp_len += tmp->len;
2174 }
2175
2176out:
2177 skb_queue_purge(&dev->resp_q);
2178
2179 return skb;
2180}
2181
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002182static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002183 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002184{
2185 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002186 struct sk_buff *skb;
2187 int rc = 0;
2188 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002189
2190 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2191
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002192 if (IS_ERR(resp)) {
2193 rc = PTR_ERR(resp);
2194 goto _error;
2195 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002196
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002197 status = resp->data[0];
2198 ret = status & PN533_CMD_RET_MASK;
2199 mi = status & PN533_CMD_MI_MASK;
2200
2201 skb_pull(resp, sizeof(status));
2202
2203 if (ret != PN533_CMD_RET_SUCCESS) {
2204 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002205 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002206 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002207 goto error;
2208 }
2209
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002210 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002211
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002212 if (mi) {
2213 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002214 queue_work(dev->wq, &dev->mi_work);
2215 return -EINPROGRESS;
2216 }
2217
2218 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002219 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002220 goto error;
2221
2222 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002223 kfree(arg);
2224 return 0;
2225
2226error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002227 dev_kfree_skb(resp);
2228_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002229 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002230 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002231 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002232 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002233}
2234
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002235static int pn533_transceive(struct nfc_dev *nfc_dev,
2236 struct nfc_target *target, struct sk_buff *skb,
2237 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002238{
2239 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002240 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002241 int rc;
2242
2243 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2244
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2246 /* TODO: Implement support to multi-part data exchange */
2247 nfc_dev_err(&dev->interface->dev,
2248 "Data length greater than the max allowed: %d",
2249 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2250 rc = -ENOSYS;
2251 goto error;
2252 }
2253
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002254 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002255 nfc_dev_err(&dev->interface->dev,
2256 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002257 rc = -EINVAL;
2258 goto error;
2259 }
2260
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002261 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002262 if (!arg) {
2263 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002264 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002265 }
2266
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002267 arg->cb = cb;
2268 arg->cb_context = cb_context;
2269
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002270 switch (dev->device_type) {
2271 case PN533_DEVICE_PASORI:
2272 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2273 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2274 skb,
2275 pn533_data_exchange_complete,
2276 arg);
2277
2278 break;
2279 }
2280 default:
2281 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2282
2283 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2284 skb, pn533_data_exchange_complete,
2285 arg);
2286
2287 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002288 }
2289
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002290 if (rc < 0) /* rc from send_async */
2291 goto error;
2292
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002293 return 0;
2294
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002295error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002296 kfree(arg);
2297 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002298 return rc;
2299}
2300
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002301static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002302 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002303{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002304 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002305
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002306 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2307
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002308 if (IS_ERR(resp))
2309 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002310
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002311 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002312
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002313 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002314
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002315 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002316 nfc_tm_deactivated(dev->nfc_dev);
2317
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002318 dev->tgt_mode = 0;
2319
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002320 return 0;
2321 }
2322
2323 queue_work(dev->wq, &dev->tg_work);
2324
2325 return 0;
2326}
2327
2328static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2329{
2330 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002331 int rc;
2332
2333 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2334
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002335 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002336 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002337 "Data length greater than the max allowed: %d",
2338 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2339 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002340 }
2341
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002342 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2343 pn533_tm_send_complete, NULL);
2344 if (rc < 0)
2345 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002346
2347 return rc;
2348}
2349
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002350static void pn533_wq_mi_recv(struct work_struct *work)
2351{
2352 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002353
2354 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002355 int rc;
2356
2357 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2358
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002359 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002360 if (!skb)
2361 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002362
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002363 switch (dev->device_type) {
2364 case PN533_DEVICE_PASORI:
2365 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2366 rc = pn533_send_cmd_direct_async(dev,
2367 PN533_CMD_IN_COMM_THRU,
2368 skb,
2369 pn533_data_exchange_complete,
2370 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002371
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002372 break;
2373 }
2374 default:
2375 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002376
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002377 rc = pn533_send_cmd_direct_async(dev,
2378 PN533_CMD_IN_DATA_EXCHANGE,
2379 skb,
2380 pn533_data_exchange_complete,
2381 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002382
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002383 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002384 }
2385
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002386 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002387 return;
2388
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002389 nfc_dev_err(&dev->interface->dev,
2390 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002391
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002392 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002393 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002394
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002395error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002396 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002397 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002398}
2399
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002400static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2401 u8 cfgdata_len)
2402{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002403 struct sk_buff *skb;
2404 struct sk_buff *resp;
2405
2406 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002407
2408 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2409
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002410 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002411
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002412 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002413 if (!skb)
2414 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002415
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002416 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2417 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002419 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2420 if (IS_ERR(resp))
2421 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002422
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002423 dev_kfree_skb(resp);
2424 return 0;
2425}
2426
2427static int pn533_get_firmware_version(struct pn533 *dev,
2428 struct pn533_fw_version *fv)
2429{
2430 struct sk_buff *skb;
2431 struct sk_buff *resp;
2432
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002433 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002434 if (!skb)
2435 return -ENOMEM;
2436
2437 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2438 if (IS_ERR(resp))
2439 return PTR_ERR(resp);
2440
2441 fv->ic = resp->data[0];
2442 fv->ver = resp->data[1];
2443 fv->rev = resp->data[2];
2444 fv->support = resp->data[3];
2445
2446 dev_kfree_skb(resp);
2447 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002448}
2449
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002450static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002451{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002452 struct sk_buff *skb;
2453 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002454
2455 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2456
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002457 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002458 if (!skb)
2459 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002460
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002461 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002462
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002463 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2464 if (IS_ERR(resp))
2465 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002466
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002467 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002468
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002469 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002470}
2471
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002472struct pn533_acr122_poweron_rdr_arg {
2473 int rc;
2474 struct completion done;
2475};
2476
2477static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2478{
2479 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2480
2481 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2482
2483 print_hex_dump(KERN_ERR, "ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2484 urb->transfer_buffer, urb->transfer_buffer_length,
2485 false);
2486
2487 arg->rc = urb->status;
2488 complete(&arg->done);
2489}
2490
2491static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2492{
2493 /* Power on th reader (CCID cmd) */
2494 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2495 0, 0, 0, 0, 0, 0, 3, 0, 0};
2496 u8 buf[255];
2497 int rc;
2498 void *cntx;
2499 struct pn533_acr122_poweron_rdr_arg arg;
2500
2501 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2502
2503 init_completion(&arg.done);
2504 cntx = dev->in_urb->context; /* backup context */
2505
2506 dev->in_urb->transfer_buffer = buf;
2507 dev->in_urb->transfer_buffer_length = 255;
2508 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2509 dev->in_urb->context = &arg;
2510
2511 dev->out_urb->transfer_buffer = cmd;
2512 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2513
2514 print_hex_dump(KERN_ERR, "ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2515 cmd, sizeof(cmd), false);
2516
2517 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2518 if (rc) {
2519 nfc_dev_err(&dev->interface->dev,
2520 "Reader power on cmd error %d", rc);
2521 return rc;
2522 }
2523
2524 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2525 if (rc) {
2526 nfc_dev_err(&dev->interface->dev,
2527 "Can't submit for reader power on cmd response %d",
2528 rc);
2529 return rc;
2530 }
2531
2532 wait_for_completion(&arg.done);
2533 dev->in_urb->context = cntx; /* restore context */
2534
2535 return arg.rc;
2536}
2537
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002538static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002539 .dev_up = NULL,
2540 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002541 .dep_link_up = pn533_dep_link_up,
2542 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002543 .start_poll = pn533_start_poll,
2544 .stop_poll = pn533_stop_poll,
2545 .activate_target = pn533_activate_target,
2546 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002547 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002548 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549};
2550
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002551static int pn533_setup(struct pn533 *dev)
2552{
2553 struct pn533_config_max_retries max_retries;
2554 struct pn533_config_timing timing;
2555 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2556 int rc;
2557
2558 switch (dev->device_type) {
2559 case PN533_DEVICE_STD:
2560 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2561 max_retries.mx_rty_psl = 2;
2562 max_retries.mx_rty_passive_act =
2563 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2564
2565 timing.rfu = PN533_CONFIG_TIMING_102;
2566 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2567 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2568
2569 break;
2570
2571 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002572 case PN533_DEVICE_ACR122U:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002573 max_retries.mx_rty_atr = 0x2;
2574 max_retries.mx_rty_psl = 0x1;
2575 max_retries.mx_rty_passive_act =
2576 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2577
2578 timing.rfu = PN533_CONFIG_TIMING_102;
2579 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2580 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2581
2582 break;
2583
2584 default:
2585 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2586 dev->device_type);
2587 return -EINVAL;
2588 }
2589
2590 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2591 (u8 *)&max_retries, sizeof(max_retries));
2592 if (rc) {
2593 nfc_dev_err(&dev->interface->dev,
2594 "Error on setting MAX_RETRIES config");
2595 return rc;
2596 }
2597
2598
2599 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2600 (u8 *)&timing, sizeof(timing));
2601 if (rc) {
2602 nfc_dev_err(&dev->interface->dev,
2603 "Error on setting RF timings");
2604 return rc;
2605 }
2606
2607 switch (dev->device_type) {
2608 case PN533_DEVICE_STD:
2609 break;
2610
2611 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002612 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002613
2614 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2615 pasori_cfg, 3);
2616 if (rc) {
2617 nfc_dev_err(&dev->interface->dev,
2618 "Error while settings PASORI config");
2619 return rc;
2620 }
2621
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002622 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002623
2624 break;
2625 }
2626
2627 return 0;
2628}
2629
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002630static int pn533_probe(struct usb_interface *interface,
2631 const struct usb_device_id *id)
2632{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002633 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002634 struct pn533 *dev;
2635 struct usb_host_interface *iface_desc;
2636 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002637 int in_endpoint = 0;
2638 int out_endpoint = 0;
2639 int rc = -ENOMEM;
2640 int i;
2641 u32 protocols;
2642
2643 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2644 if (!dev)
2645 return -ENOMEM;
2646
2647 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2648 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002649 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002650
2651 iface_desc = interface->cur_altsetting;
2652 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2653 endpoint = &iface_desc->endpoint[i].desc;
2654
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002655 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002656 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002657
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002658 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002659 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002660 }
2661
2662 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002663 nfc_dev_err(&interface->dev,
2664 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002665 rc = -ENODEV;
2666 goto error;
2667 }
2668
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002669 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002670 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2671
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002672 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002673 goto error;
2674
2675 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002676 usb_rcvbulkpipe(dev->udev, in_endpoint),
2677 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002678 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002679 usb_sndbulkpipe(dev->udev, out_endpoint),
2680 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002681
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002682 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2683 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002684 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002685 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002686 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002687 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002688 if (dev->wq == NULL)
2689 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002690
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002691 init_timer(&dev->listen_timer);
2692 dev->listen_timer.data = (unsigned long) dev;
2693 dev->listen_timer.function = pn533_listen_mode_timer;
2694
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002695 skb_queue_head_init(&dev->resp_q);
2696
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002697 INIT_LIST_HEAD(&dev->cmd_queue);
2698
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002699 usb_set_intfdata(interface, dev);
2700
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002701 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002702
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +02002703 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002704 dev->device_type = id->driver_info;
2705 switch (dev->device_type) {
2706 case PN533_DEVICE_STD:
2707 protocols = PN533_ALL_PROTOCOLS;
2708 break;
2709
2710 case PN533_DEVICE_PASORI:
2711 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2712 break;
2713
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002714 case PN533_DEVICE_ACR122U:
2715 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2716 dev->ops = &pn533_acr122_frame_ops;
2717 dev->protocol_type = PN533_PROTO_REQ_RESP,
2718
2719 rc = pn533_acr122_poweron_rdr(dev);
2720 if (rc < 0) {
2721 nfc_dev_err(&dev->interface->dev,
2722 "Couldn't poweron the reader (error %d)",
2723 rc);
2724 goto destroy_wq;
2725 }
2726 break;
2727
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002728 default:
2729 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2730 dev->device_type);
2731 rc = -EINVAL;
2732 goto destroy_wq;
2733 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002734
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002735 memset(&fw_ver, 0, sizeof(fw_ver));
2736 rc = pn533_get_firmware_version(dev, &fw_ver);
2737 if (rc < 0)
2738 goto destroy_wq;
2739
2740 nfc_dev_info(&dev->interface->dev,
2741 "NXP PN533 firmware ver %d.%d now attached",
2742 fw_ver.ver, fw_ver.rev);
2743
2744
Samuel Ortize8753042011-08-19 15:47:11 +02002745 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002746 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002747 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002748 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002749 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002750 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002751 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002752
2753 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2754 nfc_set_drvdata(dev->nfc_dev, dev);
2755
2756 rc = nfc_register_device(dev->nfc_dev);
2757 if (rc)
2758 goto free_nfc_dev;
2759
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002760 rc = pn533_setup(dev);
2761 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002762 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002763
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002764 return 0;
2765
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002766unregister_nfc_dev:
2767 nfc_unregister_device(dev->nfc_dev);
2768
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002769free_nfc_dev:
2770 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002771
Samuel Ortiz4849f852012-04-10 19:43:17 +02002772destroy_wq:
2773 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002774error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002775 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002776 usb_free_urb(dev->out_urb);
2777 kfree(dev);
2778 return rc;
2779}
2780
2781static void pn533_disconnect(struct usb_interface *interface)
2782{
2783 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002784 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002785
2786 dev = usb_get_intfdata(interface);
2787 usb_set_intfdata(interface, NULL);
2788
2789 nfc_unregister_device(dev->nfc_dev);
2790 nfc_free_device(dev->nfc_dev);
2791
2792 usb_kill_urb(dev->in_urb);
2793 usb_kill_urb(dev->out_urb);
2794
Samuel Ortiz4849f852012-04-10 19:43:17 +02002795 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002796
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002797 skb_queue_purge(&dev->resp_q);
2798
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002799 del_timer(&dev->listen_timer);
2800
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002801 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2802 list_del(&cmd->queue);
2803 kfree(cmd);
2804 }
2805
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002806 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002807 usb_free_urb(dev->out_urb);
2808 kfree(dev);
2809
Dan Carpenter276556d2011-07-08 10:21:15 +03002810 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002811}
2812
2813static struct usb_driver pn533_driver = {
2814 .name = "pn533",
2815 .probe = pn533_probe,
2816 .disconnect = pn533_disconnect,
2817 .id_table = pn533_table,
2818};
2819
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002820module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002821
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002822MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2823MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2824MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002825MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2826MODULE_VERSION(VERSION);
2827MODULE_LICENSE("GPL");