blob: 4d56ad4c8bfb417173c4a27dfa32301de81ded87 [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
Waldemar Rymarkiewicz495af722013-04-03 08:02:16 +020030#define VERSION "0.2"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030031
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 Rymarkiewiczaada17a2012-11-26 14:18:34 +0100152typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
153 struct sk_buff *resp);
154
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155/* structs for pn533 commands */
156
157/* PN533_CMD_GET_FIRMWARE_VERSION */
158struct pn533_fw_version {
159 u8 ic;
160 u8 ver;
161 u8 rev;
162 u8 support;
163};
164
165/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200166#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300167#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200168#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300169
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200170#define PN533_CONFIG_TIMING_102 0xb
171#define PN533_CONFIG_TIMING_204 0xc
172#define PN533_CONFIG_TIMING_409 0xd
173#define PN533_CONFIG_TIMING_819 0xe
174
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300175#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
176#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
177
178struct pn533_config_max_retries {
179 u8 mx_rty_atr;
180 u8 mx_rty_psl;
181 u8 mx_rty_passive_act;
182} __packed;
183
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200184struct pn533_config_timing {
185 u8 rfu;
186 u8 atr_res_timeout;
187 u8 dep_timeout;
188} __packed;
189
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300190/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
191
192/* felica commands opcode */
193#define PN533_FELICA_OPC_SENSF_REQ 0
194#define PN533_FELICA_OPC_SENSF_RES 1
195/* felica SENSF_REQ parameters */
196#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
197#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
198#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
199#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
200
201/* type B initiator_data values */
202#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
203#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
204#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
205
206union pn533_cmd_poll_initdata {
207 struct {
208 u8 afi;
209 u8 polling_method;
210 } __packed type_b;
211 struct {
212 u8 opcode;
213 __be16 sc;
214 u8 rc;
215 u8 tsn;
216 } __packed felica;
217};
218
219/* Poll modulations */
220enum {
221 PN533_POLL_MOD_106KBPS_A,
222 PN533_POLL_MOD_212KBPS_FELICA,
223 PN533_POLL_MOD_424KBPS_FELICA,
224 PN533_POLL_MOD_106KBPS_JEWEL,
225 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200226 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300227
228 __PN533_POLL_MOD_AFTER_LAST,
229};
230#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
231
232struct pn533_poll_modulations {
233 struct {
234 u8 maxtg;
235 u8 brty;
236 union pn533_cmd_poll_initdata initiator_data;
237 } __packed data;
238 u8 len;
239};
240
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100241static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300242 [PN533_POLL_MOD_106KBPS_A] = {
243 .data = {
244 .maxtg = 1,
245 .brty = 0,
246 },
247 .len = 2,
248 },
249 [PN533_POLL_MOD_212KBPS_FELICA] = {
250 .data = {
251 .maxtg = 1,
252 .brty = 1,
253 .initiator_data.felica = {
254 .opcode = PN533_FELICA_OPC_SENSF_REQ,
255 .sc = PN533_FELICA_SENSF_SC_ALL,
256 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
257 .tsn = 0,
258 },
259 },
260 .len = 7,
261 },
262 [PN533_POLL_MOD_424KBPS_FELICA] = {
263 .data = {
264 .maxtg = 1,
265 .brty = 2,
266 .initiator_data.felica = {
267 .opcode = PN533_FELICA_OPC_SENSF_REQ,
268 .sc = PN533_FELICA_SENSF_SC_ALL,
269 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
270 .tsn = 0,
271 },
272 },
273 .len = 7,
274 },
275 [PN533_POLL_MOD_106KBPS_JEWEL] = {
276 .data = {
277 .maxtg = 1,
278 .brty = 4,
279 },
280 .len = 2,
281 },
282 [PN533_POLL_MOD_847KBPS_B] = {
283 .data = {
284 .maxtg = 1,
285 .brty = 8,
286 .initiator_data.type_b = {
287 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
288 .polling_method =
289 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
290 },
291 },
292 .len = 3,
293 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200294 [PN533_LISTEN_MOD] = {
295 .len = 0,
296 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300297};
298
299/* PN533_CMD_IN_ATR */
300
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300301struct pn533_cmd_activate_response {
302 u8 status;
303 u8 nfcid3t[10];
304 u8 didt;
305 u8 bst;
306 u8 brt;
307 u8 to;
308 u8 ppt;
309 /* optional */
310 u8 gt[];
311} __packed;
312
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100313struct pn533_cmd_jump_dep_response {
314 u8 status;
315 u8 tg;
316 u8 nfcid3t[10];
317 u8 didt;
318 u8 bst;
319 u8 brt;
320 u8 to;
321 u8 ppt;
322 /* optional */
323 u8 gt[];
324} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300325
Samuel Ortizad3823c2012-05-30 23:54:55 +0200326
327/* PN533_TG_INIT_AS_TARGET */
328#define PN533_INIT_TARGET_PASSIVE 0x1
329#define PN533_INIT_TARGET_DEP 0x2
330
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200331#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
332#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
333#define PN533_INIT_TARGET_RESP_DEP 0x4
334
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200335enum pn533_protocol_type {
336 PN533_PROTO_REQ_ACK_RESP = 0,
337 PN533_PROTO_REQ_RESP
338};
339
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300340struct pn533 {
341 struct usb_device *udev;
342 struct usb_interface *interface;
343 struct nfc_dev *nfc_dev;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200344 u32 device_type;
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200345 enum pn533_protocol_type protocol_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300346
347 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300348 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300349
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200350 struct sk_buff_head resp_q;
351
Samuel Ortiz4849f852012-04-10 19:43:17 +0200352 struct workqueue_struct *wq;
353 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200354 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200355 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200356 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200357 struct work_struct tg_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200358
359 struct list_head cmd_queue;
360 struct pn533_cmd *cmd;
361 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200362 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300363
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100364 void *cmd_complete_mi_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300365
366 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
367 u8 poll_mod_count;
368 u8 poll_mod_curr;
369 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200370 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200371 struct timer_list listen_timer;
372 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200373
374 u8 *gb;
375 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300376
377 u8 tgt_available_prots;
378 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200379 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200380
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100381 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200382};
383
384struct pn533_cmd {
385 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200386 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200387 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100388 struct sk_buff *req;
389 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100390 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200391 pn533_send_async_complete_t complete_cb;
392 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300393};
394
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200395struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396 u8 preamble;
397 __be16 start_frame;
398 u8 datalen;
399 u8 datalen_checksum;
400 u8 data[];
401} __packed;
402
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100403struct pn533_frame_ops {
404 void (*tx_frame_init)(void *frame, u8 cmd_code);
405 void (*tx_frame_finish)(void *frame);
406 void (*tx_update_payload_len)(void *frame, int len);
407 int tx_header_len;
408 int tx_tail_len;
409
410 bool (*rx_is_frame_valid)(void *frame);
411 int (*rx_frame_size)(void *frame);
412 int rx_header_len;
413 int rx_tail_len;
414
415 int max_payload_len;
416 u8 (*get_cmd_code)(void *frame);
417};
418
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200419struct pn533_acr122_ccid_hdr {
420 u8 type;
421 u32 datalen;
422 u8 slot;
423 u8 seq;
424 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
425 byte for reposnse msg */
426 u8 data[]; /* payload */
427} __packed;
428
429struct pn533_acr122_apdu_hdr {
430 u8 class;
431 u8 ins;
432 u8 p1;
433 u8 p2;
434} __packed;
435
436struct pn533_acr122_tx_frame {
437 struct pn533_acr122_ccid_hdr ccid;
438 struct pn533_acr122_apdu_hdr apdu;
439 u8 datalen;
440 u8 data[]; /* pn533 frame: TFI ... */
441} __packed;
442
443struct pn533_acr122_rx_frame {
444 struct pn533_acr122_ccid_hdr ccid;
445 u8 data[]; /* pn533 frame : TFI ... */
446} __packed;
447
448static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
449{
450 struct pn533_acr122_tx_frame *frame = _frame;
451
452 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
453 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
454 sizeof(datalen) */
455 frame->ccid.slot = 0;
456 frame->ccid.seq = 0;
457 frame->ccid.params[0] = 0;
458 frame->ccid.params[1] = 0;
459 frame->ccid.params[2] = 0;
460
461 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
462 frame->data[1] = cmd_code;
463 frame->datalen = 2; /* data[0] + data[1] */
464
465 frame->apdu.class = 0xFF;
466 frame->apdu.ins = 0;
467 frame->apdu.p1 = 0;
468 frame->apdu.p2 = 0;
469}
470
471static void pn533_acr122_tx_frame_finish(void *_frame)
472{
473 struct pn533_acr122_tx_frame *frame = _frame;
474
475 frame->ccid.datalen += frame->datalen;
476}
477
478static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
479{
480 struct pn533_acr122_tx_frame *frame = _frame;
481
482 frame->datalen += len;
483}
484
485static bool pn533_acr122_is_rx_frame_valid(void *_frame)
486{
487 struct pn533_acr122_rx_frame *frame = _frame;
488
489 if (frame->ccid.type != 0x83)
490 return false;
491
492 if (frame->data[frame->ccid.datalen - 2] == 0x63)
493 return false;
494
495 return true;
496}
497
498static int pn533_acr122_rx_frame_size(void *frame)
499{
500 struct pn533_acr122_rx_frame *f = frame;
501
502 /* f->ccid.datalen already includes tail length */
503 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
504}
505
506static u8 pn533_acr122_get_cmd_code(void *frame)
507{
508 struct pn533_acr122_rx_frame *f = frame;
509
510 return PN533_STD_FRAME_CMD(f);
511}
512
513static struct pn533_frame_ops pn533_acr122_frame_ops = {
514 .tx_frame_init = pn533_acr122_tx_frame_init,
515 .tx_frame_finish = pn533_acr122_tx_frame_finish,
516 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
517 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
518 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
519
520 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
521 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
522 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
523 .rx_frame_size = pn533_acr122_rx_frame_size,
524
525 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
526 .get_cmd_code = pn533_acr122_get_cmd_code,
527};
528
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300529/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200530static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300531{
532 return ~value + 1;
533}
534
535/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200536static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537{
538 u8 sum = 0;
539 int i;
540
541 for (i = 0; i < datalen; i++)
542 sum += data[i];
543
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200544 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300545}
546
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200547static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200549 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100550
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300551 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200552 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
553 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
554 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300555 frame->datalen = 2;
556}
557
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200558static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300559{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200560 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100561
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200562 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200564 PN533_STD_FRAME_CHECKSUM(frame) =
565 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300566
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200567 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568}
569
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200570static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100571{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200572 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100573
574 frame->datalen += len;
575}
576
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200577static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300578{
579 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200580 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300581
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200582 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300583 return false;
584
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200585 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300586 if (checksum != frame->datalen_checksum)
587 return false;
588
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200589 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
590 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300591 return false;
592
593 return true;
594}
595
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200596static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300597{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200598 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599 return false;
600
601 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
602 return false;
603
604 return true;
605}
606
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200607static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300608{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200609 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100610
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200611 return sizeof(struct pn533_std_frame) + f->datalen +
612 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100613}
614
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200615static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100616{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200617 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100618
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200619 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100620}
621
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100622static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200623 .tx_frame_init = pn533_std_tx_frame_init,
624 .tx_frame_finish = pn533_std_tx_frame_finish,
625 .tx_update_payload_len = pn533_std_tx_update_payload_len,
626 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
627 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100628
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200629 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
630 .rx_frame_size = pn533_std_rx_frame_size,
631 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
632 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100633
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200634 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
635 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100636};
637
638static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
639{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200640 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200641 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300642}
643
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300644static void pn533_recv_response(struct urb *urb)
645{
646 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200647 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100648 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300649
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200650 cmd->status = urb->status;
651
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300652 switch (urb->status) {
653 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100654 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300655 case -ECONNRESET:
656 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100657 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100658 "The urb has been canceled (status %d)",
659 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200660 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100661 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300662 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100663 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100664 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200665 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300666 }
667
668 in_frame = dev->in_urb->transfer_buffer;
669
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100670 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100671 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
672 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100673
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100674 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300675 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200676 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200677 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300678 }
679
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100680 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100681 nfc_dev_err(&dev->interface->dev,
682 "It it not the response to the last command");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200683 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200684 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300685 }
686
Samuel Ortiz4849f852012-04-10 19:43:17 +0200687sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200688 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300689}
690
691static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
692{
693 dev->in_urb->complete = pn533_recv_response;
694
695 return usb_submit_urb(dev->in_urb, flags);
696}
697
698static void pn533_recv_ack(struct urb *urb)
699{
700 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200701 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200702 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300703 int rc;
704
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200705 cmd->status = urb->status;
706
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300707 switch (urb->status) {
708 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100709 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300710 case -ECONNRESET:
711 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100712 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100713 "The urb has been stopped (status %d)",
714 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200715 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100716 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300717 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100718 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100719 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200720 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300721 }
722
723 in_frame = dev->in_urb->transfer_buffer;
724
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200725 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300726 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200727 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200728 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300729 }
730
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300731 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
732 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100733 nfc_dev_err(&dev->interface->dev,
734 "usb_submit_urb failed with result %d", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200735 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200736 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300737 }
738
739 return;
740
Samuel Ortiz4849f852012-04-10 19:43:17 +0200741sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200742 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300743}
744
745static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
746{
747 dev->in_urb->complete = pn533_recv_ack;
748
749 return usb_submit_urb(dev->in_urb, flags);
750}
751
752static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
753{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200754 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100755 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300756 int rc;
757
758 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
759
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100760 dev->out_urb->transfer_buffer = ack;
761 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300762 rc = usb_submit_urb(dev->out_urb, flags);
763
764 return rc;
765}
766
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100767static int __pn533_send_frame_async(struct pn533 *dev,
768 struct sk_buff *out,
769 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200770 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300771{
772 int rc;
773
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100774 dev->out_urb->transfer_buffer = out->data;
775 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300776
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100777 dev->in_urb->transfer_buffer = in->data;
778 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300779
Samuel Ortize279f842013-03-20 11:27:57 +0100780 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
781 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100782
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100783 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300784 if (rc)
785 return rc;
786
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200787 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
788 /* request for response for sent packet directly */
789 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
790 if (rc)
791 goto error;
792 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
793 /* request for ACK if that's the case */
794 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
795 if (rc)
796 goto error;
797 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300798
799 return 0;
800
801error:
802 usb_unlink_urb(dev->out_urb);
803 return rc;
804}
805
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100806static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
807 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100808{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100809 /* payload is already there, just update datalen */
810 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100811 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100812
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100813
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100814 skb_push(skb, ops->tx_header_len);
815 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100816
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100817 ops->tx_frame_init(skb->data, cmd_code);
818 ops->tx_update_payload_len(skb->data, payload_len);
819 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100820}
821
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200822static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100823{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200824 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200825 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100826
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200827 struct sk_buff *req = cmd->req;
828 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100829
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100830 int rc;
831
832 dev_kfree_skb(req);
833
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100834 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200835 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
836 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100837 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200838 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100839 }
840
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100841 skb_put(resp, dev->ops->rx_frame_size(resp->data));
842 skb_pull(resp, dev->ops->rx_header_len);
843 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100844
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200845 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100846
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200847done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200848 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200849 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100850 return rc;
851}
852
853static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
854 struct sk_buff *req, struct sk_buff *resp,
855 int resp_len,
856 pn533_send_async_complete_t complete_cb,
857 void *complete_cb_context)
858{
859 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100860 int rc = 0;
861
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100862 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100863
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200864 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
865 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100866 return -ENOMEM;
867
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200868 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200869 cmd->req = req;
870 cmd->resp = resp;
871 cmd->resp_len = resp_len;
872 cmd->complete_cb = complete_cb;
873 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100874
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100875 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100876
877 mutex_lock(&dev->cmd_lock);
878
879 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200880 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100881 if (rc)
882 goto error;
883
884 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200885 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100886 goto unlock;
887 }
888
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100889 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
890 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100891
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100892 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100893 list_add_tail(&cmd->queue, &dev->cmd_queue);
894
895 goto unlock;
896
897error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200898 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100899unlock:
900 mutex_unlock(&dev->cmd_lock);
901 return rc;
902}
903
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100904static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
905 struct sk_buff *req,
906 pn533_send_async_complete_t complete_cb,
907 void *complete_cb_context)
908{
909 struct sk_buff *resp;
910 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100911 int resp_len = dev->ops->rx_header_len +
912 dev->ops->max_payload_len +
913 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100914
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100915 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
916 if (!resp)
917 return -ENOMEM;
918
919 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
920 complete_cb_context);
921 if (rc)
922 dev_kfree_skb(resp);
923
924 return rc;
925}
926
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100927static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
928 struct sk_buff *req,
929 pn533_send_async_complete_t complete_cb,
930 void *complete_cb_context)
931{
932 struct sk_buff *resp;
933 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100934 int resp_len = dev->ops->rx_header_len +
935 dev->ops->max_payload_len +
936 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100937
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100938 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100939 if (!resp)
940 return -ENOMEM;
941
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100942 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
943 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100944 if (rc)
945 dev_kfree_skb(resp);
946
947 return rc;
948}
949
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100950/*
951 * pn533_send_cmd_direct_async
952 *
953 * The function sends a piority cmd directly to the chip omiting the cmd
954 * queue. It's intended to be used by chaining mechanism of received responses
955 * where the host has to request every single chunk of data before scheduling
956 * next cmd from the queue.
957 */
958static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
959 struct sk_buff *req,
960 pn533_send_async_complete_t complete_cb,
961 void *complete_cb_context)
962{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100963 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200964 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100965 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100966 int resp_len = dev->ops->rx_header_len +
967 dev->ops->max_payload_len +
968 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100969
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100970 resp = alloc_skb(resp_len, GFP_KERNEL);
971 if (!resp)
972 return -ENOMEM;
973
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200974 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
975 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100976 dev_kfree_skb(resp);
977 return -ENOMEM;
978 }
979
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200980 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200981 cmd->req = req;
982 cmd->resp = resp;
983 cmd->resp_len = resp_len;
984 cmd->complete_cb = complete_cb;
985 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100986
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100987 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100988
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200989 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100990 if (rc < 0) {
991 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200992 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200993 } else {
994 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100995 }
996
997 return rc;
998}
999
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +02001000static void pn533_wq_cmd_complete(struct work_struct *work)
1001{
1002 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1003 int rc;
1004
1005 rc = pn533_send_async_complete(dev);
1006 if (rc != -EINPROGRESS)
1007 queue_work(dev->wq, &dev->cmd_work);
1008}
1009
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001010static void pn533_wq_cmd(struct work_struct *work)
1011{
1012 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1013 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001014 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001015
1016 mutex_lock(&dev->cmd_lock);
1017
1018 if (list_empty(&dev->cmd_queue)) {
1019 dev->cmd_pending = 0;
1020 mutex_unlock(&dev->cmd_lock);
1021 return;
1022 }
1023
1024 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1025
Szymon Janc60ad07a2012-10-25 17:29:45 +02001026 list_del(&cmd->queue);
1027
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001028 mutex_unlock(&dev->cmd_lock);
1029
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001030 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001031 if (rc < 0) {
1032 dev_kfree_skb(cmd->req);
1033 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001034 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001035 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001036 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001037
1038 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001039}
1040
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001041struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001042 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001043 struct completion done;
1044};
1045
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001046static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1047 struct sk_buff *resp)
1048{
1049 struct pn533_sync_cmd_response *arg = _arg;
1050
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001051 arg->resp = resp;
1052 complete(&arg->done);
1053
1054 return 0;
1055}
1056
1057/* pn533_send_cmd_sync
1058 *
1059 * Please note the req parameter is freed inside the function to
1060 * limit a number of return value interpretations by the caller.
1061 *
1062 * 1. negative in case of error during TX path -> req should be freed
1063 *
1064 * 2. negative in case of error during RX path -> req should not be freed
1065 * as it's been already freed at the begining of RX path by
1066 * async_complete_cb.
1067 *
1068 * 3. valid pointer in case of succesfult RX path
1069 *
1070 * A caller has to check a return value with IS_ERR macro. If the test pass,
1071 * the returned pointer is valid.
1072 *
1073 * */
1074static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1075 struct sk_buff *req)
1076{
1077 int rc;
1078 struct pn533_sync_cmd_response arg;
1079
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001080 init_completion(&arg.done);
1081
1082 rc = pn533_send_cmd_async(dev, cmd_code, req,
1083 pn533_send_sync_complete, &arg);
1084 if (rc) {
1085 dev_kfree_skb(req);
1086 return ERR_PTR(rc);
1087 }
1088
1089 wait_for_completion(&arg.done);
1090
1091 return arg.resp;
1092}
1093
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001094static void pn533_send_complete(struct urb *urb)
1095{
1096 struct pn533 *dev = urb->context;
1097
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001098 switch (urb->status) {
1099 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001100 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001101 case -ECONNRESET:
1102 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001103 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001104 "The urb has been stopped (status %d)",
1105 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001106 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001107 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001108 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001109 nfc_dev_err(&dev->interface->dev,
1110 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001111 }
1112}
1113
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001114static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1115{
1116 /* ACR122U does not support any command which aborts last
1117 * issued command i.e. as ACK for standard PN533. Additionally,
1118 * it behaves stange, sending broken or incorrect responses,
1119 * when we cancel urb before the chip will send response.
1120 */
1121 if (dev->device_type == PN533_DEVICE_ACR122U)
1122 return;
1123
1124 /* An ack will cancel the last issued command */
1125 pn533_send_ack(dev, flags);
1126
1127 /* cancel the urb request */
1128 usb_kill_urb(dev->in_urb);
1129}
1130
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001131static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001132{
1133 struct sk_buff *skb;
1134
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001135 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001136 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001137 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001138
1139 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001140 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001141
1142 return skb;
1143}
1144
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001145struct pn533_target_type_a {
1146 __be16 sens_res;
1147 u8 sel_res;
1148 u8 nfcid_len;
1149 u8 nfcid_data[];
1150} __packed;
1151
1152
1153#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1154#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1155#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1156
1157#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1158#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1159
1160#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1161#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1162
1163#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1164#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1165#define PN533_TYPE_A_SEL_PROT_DEP 2
1166#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1167
1168static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1169 int target_data_len)
1170{
1171 u8 ssd;
1172 u8 platconf;
1173
1174 if (target_data_len < sizeof(struct pn533_target_type_a))
1175 return false;
1176
1177 /* The lenght check of nfcid[] and ats[] are not being performed because
1178 the values are not being used */
1179
1180 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1181 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1182 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1183
1184 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001185 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1186 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1187 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001188 return false;
1189
1190 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1191 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1192 return false;
1193
1194 return true;
1195}
1196
1197static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1198 int tgt_data_len)
1199{
1200 struct pn533_target_type_a *tgt_type_a;
1201
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001202 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001203
1204 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1205 return -EPROTO;
1206
1207 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1208 case PN533_TYPE_A_SEL_PROT_MIFARE:
1209 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1210 break;
1211 case PN533_TYPE_A_SEL_PROT_ISO14443:
1212 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1213 break;
1214 case PN533_TYPE_A_SEL_PROT_DEP:
1215 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1216 break;
1217 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1218 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1219 NFC_PROTO_NFC_DEP_MASK;
1220 break;
1221 }
1222
1223 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1224 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001225 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1226 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001227
1228 return 0;
1229}
1230
1231struct pn533_target_felica {
1232 u8 pol_res;
1233 u8 opcode;
1234 u8 nfcid2[8];
1235 u8 pad[8];
1236 /* optional */
1237 u8 syst_code[];
1238} __packed;
1239
1240#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1241#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1242
1243static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1244 int target_data_len)
1245{
1246 if (target_data_len < sizeof(struct pn533_target_felica))
1247 return false;
1248
1249 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1250 return false;
1251
1252 return true;
1253}
1254
1255static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1256 int tgt_data_len)
1257{
1258 struct pn533_target_felica *tgt_felica;
1259
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001260 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001261
1262 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1263 return -EPROTO;
1264
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001265 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1266 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001267 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1268 else
1269 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1270
Samuel Ortiz79757542012-03-05 01:03:45 +01001271 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1272 nfc_tgt->sensf_res_len = 9;
1273
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001274 return 0;
1275}
1276
1277struct pn533_target_jewel {
1278 __be16 sens_res;
1279 u8 jewelid[4];
1280} __packed;
1281
1282static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1283 int target_data_len)
1284{
1285 u8 ssd;
1286 u8 platconf;
1287
1288 if (target_data_len < sizeof(struct pn533_target_jewel))
1289 return false;
1290
1291 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1292 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1293 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1294
1295 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001296 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1297 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1298 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299 return false;
1300
1301 return true;
1302}
1303
1304static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1305 int tgt_data_len)
1306{
1307 struct pn533_target_jewel *tgt_jewel;
1308
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001309 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310
1311 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1312 return -EPROTO;
1313
1314 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1315 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001316 nfc_tgt->nfcid1_len = 4;
1317 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318
1319 return 0;
1320}
1321
1322struct pn533_type_b_prot_info {
1323 u8 bitrate;
1324 u8 fsci_type;
1325 u8 fwi_adc_fo;
1326} __packed;
1327
1328#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1329#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1330#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1331
1332struct pn533_type_b_sens_res {
1333 u8 opcode;
1334 u8 nfcid[4];
1335 u8 appdata[4];
1336 struct pn533_type_b_prot_info prot_info;
1337} __packed;
1338
1339#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1340
1341struct pn533_target_type_b {
1342 struct pn533_type_b_sens_res sensb_res;
1343 u8 attrib_res_len;
1344 u8 attrib_res[];
1345} __packed;
1346
1347static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1348 int target_data_len)
1349{
1350 if (target_data_len < sizeof(struct pn533_target_type_b))
1351 return false;
1352
1353 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1354 return false;
1355
1356 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1357 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1358 return false;
1359
1360 return true;
1361}
1362
1363static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1364 int tgt_data_len)
1365{
1366 struct pn533_target_type_b *tgt_type_b;
1367
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001368 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001369
1370 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1371 return -EPROTO;
1372
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001373 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001374
1375 return 0;
1376}
1377
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001378static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1379 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001380{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001381 struct nfc_target nfc_tgt;
1382 int rc;
1383
1384 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001385 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001386
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001387 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001388 return -EPROTO;
1389
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001390 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1391
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001392 switch (dev->poll_mod_curr) {
1393 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001394 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001395 break;
1396 case PN533_POLL_MOD_212KBPS_FELICA:
1397 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001398 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001399 break;
1400 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001401 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001402 break;
1403 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001404 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001405 break;
1406 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001407 nfc_dev_err(&dev->interface->dev,
1408 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001409 return -EPROTO;
1410 }
1411
1412 if (rc)
1413 return rc;
1414
1415 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001416 nfc_dev_dbg(&dev->interface->dev,
1417 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001418 return -EAGAIN;
1419 }
1420
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001421 nfc_dev_dbg(&dev->interface->dev,
1422 "Target found - supported protocols: 0x%x",
1423 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001424
1425 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1426
1427 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1428
1429 return 0;
1430}
1431
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001432static inline void pn533_poll_next_mod(struct pn533 *dev)
1433{
1434 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1435}
1436
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001437static void pn533_poll_reset_mod_list(struct pn533 *dev)
1438{
1439 dev->poll_mod_count = 0;
1440}
1441
1442static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1443{
1444 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001445 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001446 dev->poll_mod_count++;
1447}
1448
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001449static void pn533_poll_create_mod_list(struct pn533 *dev,
1450 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001451{
1452 pn533_poll_reset_mod_list(dev);
1453
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001454 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1455 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1456 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001457 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1458
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001459 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1460 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001461 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1462 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1463 }
1464
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001465 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001466 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1467
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001468 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001469 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001470
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001471 if (tm_protocols)
1472 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001473}
1474
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001475static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001476{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001477 u8 nbtg, tg, *tgdata;
1478 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001479
1480 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1481
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001482 nbtg = resp->data[0];
1483 tg = resp->data[1];
1484 tgdata = &resp->data[2];
1485 tgdata_len = resp->len - 2; /* nbtg + tg */
1486
1487 if (nbtg) {
1488 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001489
1490 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001491 if (rc == 0) {
1492 pn533_poll_reset_mod_list(dev);
1493 return 0;
1494 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001495 }
1496
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001497 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001498}
1499
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001500static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001501{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001502 struct sk_buff *skb;
1503 u8 *felica, *nfcid3, *gb;
1504
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001505 u8 *gbytes = dev->gb;
1506 size_t gbytes_len = dev->gb_len;
1507
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001508 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1509 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1510 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1511 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001512
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001513 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1514 0x0, 0x0, 0x0,
1515 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001516
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001517 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1518 felica (18), nfcid3 (10), gb_len (1) */
1519 gbytes_len +
1520 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001521
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001522 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001523 if (!skb)
1524 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001525
1526 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001527 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001528
1529 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001530 memcpy(skb_put(skb, 6), mifare_params, 6);
1531
1532 /* Felica params */
1533 felica = skb_put(skb, 18);
1534 memcpy(felica, felica_params, 18);
1535 get_random_bytes(felica + 2, 6);
1536
1537 /* NFCID3 */
1538 nfcid3 = skb_put(skb, 10);
1539 memset(nfcid3, 0, 10);
1540 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001541
1542 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001543 *skb_put(skb, 1) = gbytes_len;
1544
1545 gb = skb_put(skb, gbytes_len);
1546 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001547
Samuel Ortizad3823c2012-05-30 23:54:55 +02001548 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001549 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001550
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001552}
1553
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001554#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001555#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1556static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001557 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001558{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001559 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001560
1561 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1562
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001563 if (IS_ERR(resp))
1564 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001565
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001566 status = resp->data[0];
1567 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001568
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001569 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001570 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001571 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001572 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001573 return 0;
1574 }
1575
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001576 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001577}
1578
1579static void pn533_wq_tg_get_data(struct work_struct *work)
1580{
1581 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001582
1583 struct sk_buff *skb;
1584 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001585
1586 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1587
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001588 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001589 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001590 return;
1591
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001592 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1593 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001594
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001595 if (rc < 0)
1596 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001597
1598 return;
1599}
1600
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001601#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001603{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001604 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001605 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001606 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001607
1608 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1609
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001610 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001611 return -EINVAL;
1612
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001613 mode = resp->data[0];
1614 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001615
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001616 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1617 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001618
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001619 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1620 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001621 comm_mode = NFC_COMM_ACTIVE;
1622
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001623 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001624 return -EOPNOTSUPP;
1625
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001626 gb = cmd + ATR_REQ_GB_OFFSET;
1627 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001628
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001629 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1630 comm_mode, gb, gb_len);
1631 if (rc < 0) {
1632 nfc_dev_err(&dev->interface->dev,
1633 "Error when signaling target activation");
1634 return rc;
1635 }
1636
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001637 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001638 queue_work(dev->wq, &dev->tg_work);
1639
1640 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001641}
1642
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001644{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001645 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646
1647 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1648
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001649 dev->cancel_listen = 1;
1650
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001651 pn533_poll_next_mod(dev);
1652
1653 queue_work(dev->wq, &dev->poll_work);
1654}
1655
1656static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001657 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001658{
1659 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001660 int rc;
1661
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001662 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1663
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001664 if (IS_ERR(resp)) {
1665 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001666
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001667 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1668 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001669
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001670 if (rc == -ENOENT) {
1671 if (dev->poll_mod_count != 0)
1672 return rc;
1673 else
1674 goto stop_poll;
1675 } else if (rc < 0) {
1676 nfc_dev_err(&dev->interface->dev,
1677 "Error %d when running poll", rc);
1678 goto stop_poll;
1679 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001680 }
1681
1682 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1683
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001684 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001685 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001686 rc = pn533_init_target_complete(dev, resp);
1687 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001688 }
1689
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001690 /* Initiator mode */
1691 rc = pn533_start_poll_complete(dev, resp);
1692 if (!rc)
1693 goto done;
1694
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001695 if (!dev->poll_mod_count) {
1696 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1697 goto done;
1698 }
1699
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001700 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001701 queue_work(dev->wq, &dev->poll_work);
1702
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001703done:
1704 dev_kfree_skb(resp);
1705 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001706
1707stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001708 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1709
Samuel Ortizad3823c2012-05-30 23:54:55 +02001710 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001711 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001712 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001713}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001714
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001715static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1716 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001717{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001718 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001719
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001720 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001721 if (!skb)
1722 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001723
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001724 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001725
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001726 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001727}
1728
1729static int pn533_send_poll_frame(struct pn533 *dev)
1730{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001731 struct pn533_poll_modulations *mod;
1732 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001733 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001734 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001735
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001736 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001737
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001738 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1739 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001740
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001741 if (mod->len == 0) { /* Listen mode */
1742 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001743 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001744 } else { /* Polling mode */
1745 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001746 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001747 }
1748
1749 if (!skb) {
1750 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1751 return -ENOMEM;
1752 }
1753
1754 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1755 NULL);
1756 if (rc < 0) {
1757 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001758 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001759 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001760
1761 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001762}
1763
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001764static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001765{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001766 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1767 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001768 int rc;
1769
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001770 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1771
1772 nfc_dev_dbg(&dev->interface->dev,
1773 "%s cancel_listen %d modulation len %d",
1774 __func__, dev->cancel_listen, cur_mod->len);
1775
1776 if (dev->cancel_listen == 1) {
1777 dev->cancel_listen = 0;
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001778 pn533_abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001779 }
1780
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001781 rc = pn533_send_poll_frame(dev);
1782 if (rc)
1783 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001784
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001785 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1786 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001787
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001788 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001789}
1790
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001791static int pn533_start_poll(struct nfc_dev *nfc_dev,
1792 u32 im_protocols, u32 tm_protocols)
1793{
1794 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1795
1796 nfc_dev_dbg(&dev->interface->dev,
1797 "%s: im protocols 0x%x tm protocols 0x%x",
1798 __func__, im_protocols, tm_protocols);
1799
1800 if (dev->tgt_active_prot) {
1801 nfc_dev_err(&dev->interface->dev,
1802 "Cannot poll with a target already activated");
1803 return -EBUSY;
1804 }
1805
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001806 if (dev->tgt_mode) {
1807 nfc_dev_err(&dev->interface->dev,
1808 "Cannot poll while already being activated");
1809 return -EBUSY;
1810 }
1811
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001812 if (tm_protocols) {
1813 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1814 if (dev->gb == NULL)
1815 tm_protocols = 0;
1816 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001817
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001818 dev->poll_mod_curr = 0;
1819 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1820 dev->poll_protocols = im_protocols;
1821 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001822
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001823 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001824}
1825
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001826static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1827{
1828 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1829
1830 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1831
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001832 del_timer(&dev->listen_timer);
1833
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001834 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001835 nfc_dev_dbg(&dev->interface->dev,
1836 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001837 return;
1838 }
1839
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001840 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001841 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001842}
1843
1844static int pn533_activate_target_nfcdep(struct pn533 *dev)
1845{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001846 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001847 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001848 int rc;
1849
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001850 struct sk_buff *skb;
1851 struct sk_buff *resp;
1852
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001853 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1854
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001855 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001856 if (!skb)
1857 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001858
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001859 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1860 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001861
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001862 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1863 if (IS_ERR(resp))
1864 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001865
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001866 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001867 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001868 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001869 nfc_dev_err(&dev->interface->dev,
1870 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001871 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001872 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001873 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001874
Samuel Ortiz541d9202011-12-14 16:43:10 +01001875 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001876 gt_len = resp->len - 16;
1877 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001878
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001879 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001880 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001881}
1882
Eric Lapuyade90099432012-05-07 12:31:13 +02001883static int pn533_activate_target(struct nfc_dev *nfc_dev,
1884 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001885{
1886 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1887 int rc;
1888
1889 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001890 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001891
1892 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001893 nfc_dev_err(&dev->interface->dev,
1894 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001895 return -EBUSY;
1896 }
1897
1898 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001899 nfc_dev_err(&dev->interface->dev,
1900 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001901 return -EBUSY;
1902 }
1903
1904 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001905 nfc_dev_err(&dev->interface->dev,
1906 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001907 return -EINVAL;
1908 }
1909
1910 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001911 nfc_dev_err(&dev->interface->dev,
1912 "Target doesn't support requested proto %u",
1913 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001914 return -EINVAL;
1915 }
1916
1917 if (protocol == NFC_PROTO_NFC_DEP) {
1918 rc = pn533_activate_target_nfcdep(dev);
1919 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001920 nfc_dev_err(&dev->interface->dev,
1921 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001922 return rc;
1923 }
1924 }
1925
1926 dev->tgt_active_prot = protocol;
1927 dev->tgt_available_prots = 0;
1928
1929 return 0;
1930}
1931
Eric Lapuyade90099432012-05-07 12:31:13 +02001932static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1933 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001934{
1935 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001936
1937 struct sk_buff *skb;
1938 struct sk_buff *resp;
1939
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001940 int rc;
1941
1942 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1943
1944 if (!dev->tgt_active_prot) {
1945 nfc_dev_err(&dev->interface->dev, "There is no active target");
1946 return;
1947 }
1948
1949 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001950 skb_queue_purge(&dev->resp_q);
1951
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001952 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001953 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001954 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001955
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001956 *skb_put(skb, 1) = 1; /* TG*/
1957
1958 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1959 if (IS_ERR(resp))
1960 return;
1961
1962 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001963 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001964 nfc_dev_err(&dev->interface->dev,
1965 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001966
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001967 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001968 return;
1969}
1970
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001971
1972static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001973 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001974{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001975 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001976 u8 target_gt_len;
1977 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001978 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001979
1980 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001981
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001982 if (IS_ERR(resp))
1983 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001984
1985 if (dev->tgt_available_prots &&
1986 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1987 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001988 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001989 rc = -EINVAL;
1990 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001991 }
1992
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001993 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1994
1995 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001996 if (rc != PN533_CMD_RET_SUCCESS) {
1997 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001998 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001999 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002000 }
2001
2002 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002003 struct nfc_target nfc_target;
2004
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002005 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
2006
2007 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01002008 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002009 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002010 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2011 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002012 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002013
2014 dev->tgt_available_prots = 0;
2015 }
2016
2017 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2018
2019 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002020 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002021 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002022 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002023 if (rc == 0)
2024 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002025 dev->nfc_dev->targets[0].idx,
2026 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002027
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002028error:
2029 dev_kfree_skb(resp);
2030 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002031}
2032
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002033static int pn533_mod_to_baud(struct pn533 *dev)
2034{
2035 switch (dev->poll_mod_curr) {
2036 case PN533_POLL_MOD_106KBPS_A:
2037 return 0;
2038 case PN533_POLL_MOD_212KBPS_FELICA:
2039 return 1;
2040 case PN533_POLL_MOD_424KBPS_FELICA:
2041 return 2;
2042 default:
2043 return -EINVAL;
2044 }
2045}
2046
Samuel Ortizd7f33452012-05-29 21:45:21 +02002047#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02002048static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002049 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002050{
2051 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002052 struct sk_buff *skb;
2053 int rc, baud, skb_len;
2054 u8 *next, *arg;
2055
Samuel Ortizd7f33452012-05-29 21:45:21 +02002056 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002057
2058 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2059
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002060 if (dev->poll_mod_count) {
2061 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002062 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002063 return -EBUSY;
2064 }
2065
2066 if (dev->tgt_active_prot) {
2067 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002068 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002069 return -EBUSY;
2070 }
2071
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02002072 baud = pn533_mod_to_baud(dev);
2073 if (baud < 0) {
2074 nfc_dev_err(&dev->interface->dev,
2075 "Invalid curr modulation %d", dev->poll_mod_curr);
2076 return baud;
2077 }
2078
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002079 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02002080 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002081 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002082
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002083 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002084 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002085 return -ENOMEM;
2086
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002087 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2088 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002089
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002090 next = skb_put(skb, 1); /* Next */
2091 *next = 0;
2092
2093 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
2094 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
2095 PASSIVE_DATA_LEN);
2096 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002097 }
2098
Samuel Ortiz47807d32012-03-05 01:03:50 +01002099 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002100 memcpy(skb_put(skb, gb_len), gb, gb_len);
2101 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002102 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002103 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002104 }
2105
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002106 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2107 if (!arg) {
2108 dev_kfree_skb(skb);
2109 return -ENOMEM;
2110 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002111
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002112 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002113
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002114 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2115 pn533_in_dep_link_up_complete, arg);
2116
2117 if (rc < 0) {
2118 dev_kfree_skb(skb);
2119 kfree(arg);
2120 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002121
2122 return rc;
2123}
2124
2125static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2126{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002127 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2128
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002129 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2130
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002131 pn533_poll_reset_mod_list(dev);
2132
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002133 if (dev->tgt_mode || dev->tgt_active_prot)
2134 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002135
2136 dev->tgt_active_prot = 0;
2137 dev->tgt_mode = 0;
2138
2139 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002140
2141 return 0;
2142}
2143
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002144struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002145 data_exchange_cb_t cb;
2146 void *cb_context;
2147};
2148
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002149static struct sk_buff *pn533_build_response(struct pn533 *dev)
2150{
2151 struct sk_buff *skb, *tmp, *t;
2152 unsigned int skb_len = 0, tmp_len = 0;
2153
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002154 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002155
2156 if (skb_queue_empty(&dev->resp_q))
2157 return NULL;
2158
2159 if (skb_queue_len(&dev->resp_q) == 1) {
2160 skb = skb_dequeue(&dev->resp_q);
2161 goto out;
2162 }
2163
2164 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2165 skb_len += tmp->len;
2166
2167 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2168 __func__, skb_len);
2169
2170 skb = alloc_skb(skb_len, GFP_KERNEL);
2171 if (skb == NULL)
2172 goto out;
2173
2174 skb_put(skb, skb_len);
2175
2176 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2177 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2178 tmp_len += tmp->len;
2179 }
2180
2181out:
2182 skb_queue_purge(&dev->resp_q);
2183
2184 return skb;
2185}
2186
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002187static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002188 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002189{
2190 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002191 struct sk_buff *skb;
2192 int rc = 0;
2193 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002194
2195 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2196
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002197 if (IS_ERR(resp)) {
2198 rc = PTR_ERR(resp);
2199 goto _error;
2200 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002201
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002202 status = resp->data[0];
2203 ret = status & PN533_CMD_RET_MASK;
2204 mi = status & PN533_CMD_MI_MASK;
2205
2206 skb_pull(resp, sizeof(status));
2207
2208 if (ret != PN533_CMD_RET_SUCCESS) {
2209 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002210 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002211 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002212 goto error;
2213 }
2214
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002215 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002216
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002217 if (mi) {
2218 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002219 queue_work(dev->wq, &dev->mi_work);
2220 return -EINPROGRESS;
2221 }
2222
2223 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002224 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002225 goto error;
2226
2227 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002228 kfree(arg);
2229 return 0;
2230
2231error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002232 dev_kfree_skb(resp);
2233_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002234 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002235 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002236 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002237 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002238}
2239
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002240static int pn533_transceive(struct nfc_dev *nfc_dev,
2241 struct nfc_target *target, struct sk_buff *skb,
2242 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002243{
2244 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002246 int rc;
2247
2248 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2249
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002250 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2251 /* TODO: Implement support to multi-part data exchange */
2252 nfc_dev_err(&dev->interface->dev,
2253 "Data length greater than the max allowed: %d",
2254 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2255 rc = -ENOSYS;
2256 goto error;
2257 }
2258
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002259 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002260 nfc_dev_err(&dev->interface->dev,
2261 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002262 rc = -EINVAL;
2263 goto error;
2264 }
2265
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002266 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002267 if (!arg) {
2268 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002269 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002270 }
2271
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002272 arg->cb = cb;
2273 arg->cb_context = cb_context;
2274
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002275 switch (dev->device_type) {
2276 case PN533_DEVICE_PASORI:
2277 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2278 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2279 skb,
2280 pn533_data_exchange_complete,
2281 arg);
2282
2283 break;
2284 }
2285 default:
2286 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2287
2288 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2289 skb, pn533_data_exchange_complete,
2290 arg);
2291
2292 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002293 }
2294
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002295 if (rc < 0) /* rc from send_async */
2296 goto error;
2297
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002298 return 0;
2299
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002300error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002301 kfree(arg);
2302 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002303 return rc;
2304}
2305
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002306static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002307 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002308{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002309 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002310
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002311 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2312
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002313 if (IS_ERR(resp))
2314 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002315
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002316 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002317
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002318 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002319
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002320 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002321 nfc_tm_deactivated(dev->nfc_dev);
2322
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002323 dev->tgt_mode = 0;
2324
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002325 return 0;
2326 }
2327
2328 queue_work(dev->wq, &dev->tg_work);
2329
2330 return 0;
2331}
2332
2333static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2334{
2335 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002336 int rc;
2337
2338 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2339
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002340 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002341 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002342 "Data length greater than the max allowed: %d",
2343 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2344 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002345 }
2346
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002347 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2348 pn533_tm_send_complete, NULL);
2349 if (rc < 0)
2350 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002351
2352 return rc;
2353}
2354
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002355static void pn533_wq_mi_recv(struct work_struct *work)
2356{
2357 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002358
2359 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002360 int rc;
2361
2362 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2363
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002364 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002365 if (!skb)
2366 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002367
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002368 switch (dev->device_type) {
2369 case PN533_DEVICE_PASORI:
2370 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2371 rc = pn533_send_cmd_direct_async(dev,
2372 PN533_CMD_IN_COMM_THRU,
2373 skb,
2374 pn533_data_exchange_complete,
2375 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002376
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002377 break;
2378 }
2379 default:
2380 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002381
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002382 rc = pn533_send_cmd_direct_async(dev,
2383 PN533_CMD_IN_DATA_EXCHANGE,
2384 skb,
2385 pn533_data_exchange_complete,
2386 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002387
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002388 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002389 }
2390
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002391 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002392 return;
2393
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002394 nfc_dev_err(&dev->interface->dev,
2395 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002396
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002397 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002398 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002399
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002400error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002401 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002402 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002403}
2404
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002405static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2406 u8 cfgdata_len)
2407{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002408 struct sk_buff *skb;
2409 struct sk_buff *resp;
2410
2411 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002412
2413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2414
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002415 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002416
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002417 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002418 if (!skb)
2419 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002420
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002421 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2422 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002424 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2425 if (IS_ERR(resp))
2426 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002427
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002428 dev_kfree_skb(resp);
2429 return 0;
2430}
2431
2432static int pn533_get_firmware_version(struct pn533 *dev,
2433 struct pn533_fw_version *fv)
2434{
2435 struct sk_buff *skb;
2436 struct sk_buff *resp;
2437
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002438 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002439 if (!skb)
2440 return -ENOMEM;
2441
2442 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2443 if (IS_ERR(resp))
2444 return PTR_ERR(resp);
2445
2446 fv->ic = resp->data[0];
2447 fv->ver = resp->data[1];
2448 fv->rev = resp->data[2];
2449 fv->support = resp->data[3];
2450
2451 dev_kfree_skb(resp);
2452 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002453}
2454
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002455static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002456{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002457 struct sk_buff *skb;
2458 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002459
2460 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2461
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002462 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002463 if (!skb)
2464 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002465
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002466 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002467
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002468 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2469 if (IS_ERR(resp))
2470 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002471
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002472 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002473
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002474 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002475}
2476
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002477struct pn533_acr122_poweron_rdr_arg {
2478 int rc;
2479 struct completion done;
2480};
2481
2482static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2483{
2484 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2485
2486 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2487
2488 print_hex_dump(KERN_ERR, "ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2489 urb->transfer_buffer, urb->transfer_buffer_length,
2490 false);
2491
2492 arg->rc = urb->status;
2493 complete(&arg->done);
2494}
2495
2496static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2497{
2498 /* Power on th reader (CCID cmd) */
2499 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2500 0, 0, 0, 0, 0, 0, 3, 0, 0};
2501 u8 buf[255];
2502 int rc;
2503 void *cntx;
2504 struct pn533_acr122_poweron_rdr_arg arg;
2505
2506 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2507
2508 init_completion(&arg.done);
2509 cntx = dev->in_urb->context; /* backup context */
2510
2511 dev->in_urb->transfer_buffer = buf;
2512 dev->in_urb->transfer_buffer_length = 255;
2513 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2514 dev->in_urb->context = &arg;
2515
2516 dev->out_urb->transfer_buffer = cmd;
2517 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2518
2519 print_hex_dump(KERN_ERR, "ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2520 cmd, sizeof(cmd), false);
2521
2522 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2523 if (rc) {
2524 nfc_dev_err(&dev->interface->dev,
2525 "Reader power on cmd error %d", rc);
2526 return rc;
2527 }
2528
2529 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2530 if (rc) {
2531 nfc_dev_err(&dev->interface->dev,
2532 "Can't submit for reader power on cmd response %d",
2533 rc);
2534 return rc;
2535 }
2536
2537 wait_for_completion(&arg.done);
2538 dev->in_urb->context = cntx; /* restore context */
2539
2540 return arg.rc;
2541}
2542
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002543static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002544 .dev_up = NULL,
2545 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002546 .dep_link_up = pn533_dep_link_up,
2547 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002548 .start_poll = pn533_start_poll,
2549 .stop_poll = pn533_stop_poll,
2550 .activate_target = pn533_activate_target,
2551 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002552 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002553 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002554};
2555
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002556static int pn533_setup(struct pn533 *dev)
2557{
2558 struct pn533_config_max_retries max_retries;
2559 struct pn533_config_timing timing;
2560 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2561 int rc;
2562
2563 switch (dev->device_type) {
2564 case PN533_DEVICE_STD:
2565 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2566 max_retries.mx_rty_psl = 2;
2567 max_retries.mx_rty_passive_act =
2568 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2569
2570 timing.rfu = PN533_CONFIG_TIMING_102;
2571 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2572 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2573
2574 break;
2575
2576 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002577 case PN533_DEVICE_ACR122U:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002578 max_retries.mx_rty_atr = 0x2;
2579 max_retries.mx_rty_psl = 0x1;
2580 max_retries.mx_rty_passive_act =
2581 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2582
2583 timing.rfu = PN533_CONFIG_TIMING_102;
2584 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2585 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2586
2587 break;
2588
2589 default:
2590 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2591 dev->device_type);
2592 return -EINVAL;
2593 }
2594
2595 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2596 (u8 *)&max_retries, sizeof(max_retries));
2597 if (rc) {
2598 nfc_dev_err(&dev->interface->dev,
2599 "Error on setting MAX_RETRIES config");
2600 return rc;
2601 }
2602
2603
2604 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2605 (u8 *)&timing, sizeof(timing));
2606 if (rc) {
2607 nfc_dev_err(&dev->interface->dev,
2608 "Error on setting RF timings");
2609 return rc;
2610 }
2611
2612 switch (dev->device_type) {
2613 case PN533_DEVICE_STD:
2614 break;
2615
2616 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002617 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002618
2619 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2620 pasori_cfg, 3);
2621 if (rc) {
2622 nfc_dev_err(&dev->interface->dev,
2623 "Error while settings PASORI config");
2624 return rc;
2625 }
2626
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002627 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002628
2629 break;
2630 }
2631
2632 return 0;
2633}
2634
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002635static int pn533_probe(struct usb_interface *interface,
2636 const struct usb_device_id *id)
2637{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002638 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002639 struct pn533 *dev;
2640 struct usb_host_interface *iface_desc;
2641 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002642 int in_endpoint = 0;
2643 int out_endpoint = 0;
2644 int rc = -ENOMEM;
2645 int i;
2646 u32 protocols;
2647
2648 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2649 if (!dev)
2650 return -ENOMEM;
2651
2652 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2653 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002654 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655
2656 iface_desc = interface->cur_altsetting;
2657 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2658 endpoint = &iface_desc->endpoint[i].desc;
2659
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002660 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002661 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002663 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002664 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002665 }
2666
2667 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002668 nfc_dev_err(&interface->dev,
2669 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002670 rc = -ENODEV;
2671 goto error;
2672 }
2673
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002674 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002675 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2676
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002677 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002678 goto error;
2679
2680 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002681 usb_rcvbulkpipe(dev->udev, in_endpoint),
2682 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002683 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002684 usb_sndbulkpipe(dev->udev, out_endpoint),
2685 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002686
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002687 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2688 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002689 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002690 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002691 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002692 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002693 if (dev->wq == NULL)
2694 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002695
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002696 init_timer(&dev->listen_timer);
2697 dev->listen_timer.data = (unsigned long) dev;
2698 dev->listen_timer.function = pn533_listen_mode_timer;
2699
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002700 skb_queue_head_init(&dev->resp_q);
2701
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002702 INIT_LIST_HEAD(&dev->cmd_queue);
2703
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002704 usb_set_intfdata(interface, dev);
2705
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002706 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002707
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +02002708 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002709 dev->device_type = id->driver_info;
2710 switch (dev->device_type) {
2711 case PN533_DEVICE_STD:
2712 protocols = PN533_ALL_PROTOCOLS;
2713 break;
2714
2715 case PN533_DEVICE_PASORI:
2716 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2717 break;
2718
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002719 case PN533_DEVICE_ACR122U:
2720 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2721 dev->ops = &pn533_acr122_frame_ops;
2722 dev->protocol_type = PN533_PROTO_REQ_RESP,
2723
2724 rc = pn533_acr122_poweron_rdr(dev);
2725 if (rc < 0) {
2726 nfc_dev_err(&dev->interface->dev,
2727 "Couldn't poweron the reader (error %d)",
2728 rc);
2729 goto destroy_wq;
2730 }
2731 break;
2732
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002733 default:
2734 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2735 dev->device_type);
2736 rc = -EINVAL;
2737 goto destroy_wq;
2738 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002739
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002740 memset(&fw_ver, 0, sizeof(fw_ver));
2741 rc = pn533_get_firmware_version(dev, &fw_ver);
2742 if (rc < 0)
2743 goto destroy_wq;
2744
2745 nfc_dev_info(&dev->interface->dev,
2746 "NXP PN533 firmware ver %d.%d now attached",
2747 fw_ver.ver, fw_ver.rev);
2748
2749
Samuel Ortize8753042011-08-19 15:47:11 +02002750 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002751 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002752 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002753 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002754 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002755 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002756 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002757
2758 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2759 nfc_set_drvdata(dev->nfc_dev, dev);
2760
2761 rc = nfc_register_device(dev->nfc_dev);
2762 if (rc)
2763 goto free_nfc_dev;
2764
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002765 rc = pn533_setup(dev);
2766 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002767 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002768
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002769 return 0;
2770
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002771unregister_nfc_dev:
2772 nfc_unregister_device(dev->nfc_dev);
2773
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002774free_nfc_dev:
2775 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002776
Samuel Ortiz4849f852012-04-10 19:43:17 +02002777destroy_wq:
2778 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002779error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002780 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002781 usb_free_urb(dev->out_urb);
2782 kfree(dev);
2783 return rc;
2784}
2785
2786static void pn533_disconnect(struct usb_interface *interface)
2787{
2788 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002789 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002790
2791 dev = usb_get_intfdata(interface);
2792 usb_set_intfdata(interface, NULL);
2793
2794 nfc_unregister_device(dev->nfc_dev);
2795 nfc_free_device(dev->nfc_dev);
2796
2797 usb_kill_urb(dev->in_urb);
2798 usb_kill_urb(dev->out_urb);
2799
Samuel Ortiz4849f852012-04-10 19:43:17 +02002800 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002801
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002802 skb_queue_purge(&dev->resp_q);
2803
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002804 del_timer(&dev->listen_timer);
2805
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002806 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2807 list_del(&cmd->queue);
2808 kfree(cmd);
2809 }
2810
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002811 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002812 usb_free_urb(dev->out_urb);
2813 kfree(dev);
2814
Dan Carpenter276556d2011-07-08 10:21:15 +03002815 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002816}
2817
2818static struct usb_driver pn533_driver = {
2819 .name = "pn533",
2820 .probe = pn533_probe,
2821 .disconnect = pn533_disconnect,
2822 .id_table = pn533_table,
2823};
2824
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002825module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002826
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002827MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2828MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2829MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002830MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2831MODULE_VERSION(VERSION);
2832MODULE_LICENSE("GPL");