blob: 75bffd8bb3eb5d4fae01a470b751adbd50f18a45 [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
Samuel Ortiz46f793b2013-07-03 14:50:36 +020086/* Delay between each poll frame (ms) */
87#define PN533_POLL_INTERVAL 10
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020088
Olivier Guiter1575b9d2013-06-13 15:43:27 +020089/* Standard pn533 frame definitions (standard and extended)*/
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020090#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010091 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020092#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020093
Olivier Guiter1575b9d2013-06-13 15:43:27 +020094#define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
95 + 2) /* data[0] TFI, data[1] CC */
96
97#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
98#define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
99
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100100/*
101 * Max extended frame payload len, excluding TFI and CC
102 * which are already in PN533_FRAME_HEADER_LEN.
103 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200104#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100105
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200106#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100107 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200108#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
109#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200110/* Half start code (3), LEN (4) should be 0xffff for extended frame */
111#define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
112 && (hdr)->datalen_checksum == 0xFF)
113#define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300114
115/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200116#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300117
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200118/* standard frame identifier: in/out/error */
119#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
120#define PN533_STD_FRAME_DIR_OUT 0xD4
121#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200123/* ACS ACR122 pn533 frame definitions */
124#define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
125 + 2)
126#define PN533_ACR122_TX_FRAME_TAIL_LEN 0
127#define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
128 + 2)
129#define PN533_ACR122_RX_FRAME_TAIL_LEN 2
130#define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
131
132/* CCID messages types */
133#define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
134#define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
135
136#define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
137
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300138/* PN533 Commands */
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200139#define PN533_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300140
141#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
142#define PN533_CMD_RF_CONFIGURATION 0x32
143#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200144#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
146#define PN533_CMD_IN_ATR 0x50
147#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100148#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300149
Samuel Ortizad3823c2012-05-30 23:54:55 +0200150#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200151#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200152#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100153#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200154
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300155#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
156
157/* PN533 Return codes */
158#define PN533_CMD_RET_MASK 0x3F
159#define PN533_CMD_MI_MASK 0x40
160#define PN533_CMD_RET_SUCCESS 0x00
161
162struct pn533;
163
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100164typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
165 struct sk_buff *resp);
166
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300167/* structs for pn533 commands */
168
169/* PN533_CMD_GET_FIRMWARE_VERSION */
170struct pn533_fw_version {
171 u8 ic;
172 u8 ver;
173 u8 rev;
174 u8 support;
175};
176
177/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200178#define PN533_CFGITEM_RF_FIELD 0x01
179#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300180#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200181#define PN533_CFGITEM_PASORI 0x82
182
Samuel Ortiz3a8eab32013-07-01 17:26:58 +0200183#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
184#define PN533_CFGITEM_RF_FIELD_ON 0x1
185#define PN533_CFGITEM_RF_FIELD_OFF 0x0
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300186
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200187#define PN533_CONFIG_TIMING_102 0xb
188#define PN533_CONFIG_TIMING_204 0xc
189#define PN533_CONFIG_TIMING_409 0xd
190#define PN533_CONFIG_TIMING_819 0xe
191
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300192#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
193#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
194
195struct pn533_config_max_retries {
196 u8 mx_rty_atr;
197 u8 mx_rty_psl;
198 u8 mx_rty_passive_act;
199} __packed;
200
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200201struct pn533_config_timing {
202 u8 rfu;
203 u8 atr_res_timeout;
204 u8 dep_timeout;
205} __packed;
206
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300207/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
208
209/* felica commands opcode */
210#define PN533_FELICA_OPC_SENSF_REQ 0
211#define PN533_FELICA_OPC_SENSF_RES 1
212/* felica SENSF_REQ parameters */
213#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
214#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
215#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
216#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
217
218/* type B initiator_data values */
219#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
220#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
221#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
222
223union pn533_cmd_poll_initdata {
224 struct {
225 u8 afi;
226 u8 polling_method;
227 } __packed type_b;
228 struct {
229 u8 opcode;
230 __be16 sc;
231 u8 rc;
232 u8 tsn;
233 } __packed felica;
234};
235
236/* Poll modulations */
237enum {
238 PN533_POLL_MOD_106KBPS_A,
239 PN533_POLL_MOD_212KBPS_FELICA,
240 PN533_POLL_MOD_424KBPS_FELICA,
241 PN533_POLL_MOD_106KBPS_JEWEL,
242 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200243 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300244
245 __PN533_POLL_MOD_AFTER_LAST,
246};
247#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
248
249struct pn533_poll_modulations {
250 struct {
251 u8 maxtg;
252 u8 brty;
253 union pn533_cmd_poll_initdata initiator_data;
254 } __packed data;
255 u8 len;
256};
257
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100258static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300259 [PN533_POLL_MOD_106KBPS_A] = {
260 .data = {
261 .maxtg = 1,
262 .brty = 0,
263 },
264 .len = 2,
265 },
266 [PN533_POLL_MOD_212KBPS_FELICA] = {
267 .data = {
268 .maxtg = 1,
269 .brty = 1,
270 .initiator_data.felica = {
271 .opcode = PN533_FELICA_OPC_SENSF_REQ,
272 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200273 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200274 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300275 },
276 },
277 .len = 7,
278 },
279 [PN533_POLL_MOD_424KBPS_FELICA] = {
280 .data = {
281 .maxtg = 1,
282 .brty = 2,
283 .initiator_data.felica = {
284 .opcode = PN533_FELICA_OPC_SENSF_REQ,
285 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200286 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200287 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300288 },
289 },
290 .len = 7,
291 },
292 [PN533_POLL_MOD_106KBPS_JEWEL] = {
293 .data = {
294 .maxtg = 1,
295 .brty = 4,
296 },
297 .len = 2,
298 },
299 [PN533_POLL_MOD_847KBPS_B] = {
300 .data = {
301 .maxtg = 1,
302 .brty = 8,
303 .initiator_data.type_b = {
304 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
305 .polling_method =
306 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
307 },
308 },
309 .len = 3,
310 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200311 [PN533_LISTEN_MOD] = {
312 .len = 0,
313 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300314};
315
316/* PN533_CMD_IN_ATR */
317
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300318struct pn533_cmd_activate_response {
319 u8 status;
320 u8 nfcid3t[10];
321 u8 didt;
322 u8 bst;
323 u8 brt;
324 u8 to;
325 u8 ppt;
326 /* optional */
327 u8 gt[];
328} __packed;
329
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100330struct pn533_cmd_jump_dep_response {
331 u8 status;
332 u8 tg;
333 u8 nfcid3t[10];
334 u8 didt;
335 u8 bst;
336 u8 brt;
337 u8 to;
338 u8 ppt;
339 /* optional */
340 u8 gt[];
341} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300342
Samuel Ortizad3823c2012-05-30 23:54:55 +0200343
344/* PN533_TG_INIT_AS_TARGET */
345#define PN533_INIT_TARGET_PASSIVE 0x1
346#define PN533_INIT_TARGET_DEP 0x2
347
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200348#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
349#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
350#define PN533_INIT_TARGET_RESP_DEP 0x4
351
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200352enum pn533_protocol_type {
353 PN533_PROTO_REQ_ACK_RESP = 0,
354 PN533_PROTO_REQ_RESP
355};
356
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300357struct pn533 {
358 struct usb_device *udev;
359 struct usb_interface *interface;
360 struct nfc_dev *nfc_dev;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200361 u32 device_type;
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200362 enum pn533_protocol_type protocol_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300363
364 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300365 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300366
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200367 struct sk_buff_head resp_q;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000368 struct sk_buff_head fragment_skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200369
Samuel Ortiz4849f852012-04-10 19:43:17 +0200370 struct workqueue_struct *wq;
371 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200372 struct work_struct cmd_complete_work;
Samuel Ortiz46f793b2013-07-03 14:50:36 +0200373 struct delayed_work poll_work;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000374 struct work_struct mi_rx_work;
375 struct work_struct mi_tx_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200376 struct work_struct tg_work;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +0200377 struct work_struct rf_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200378
379 struct list_head cmd_queue;
380 struct pn533_cmd *cmd;
381 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200382 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300383
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100384 void *cmd_complete_mi_arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000385 void *cmd_complete_dep_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300386
387 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
388 u8 poll_mod_count;
389 u8 poll_mod_curr;
Samuel Ortiz673088f2013-08-21 15:06:55 +0200390 u8 poll_dep;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300391 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200392 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200393 struct timer_list listen_timer;
394 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200395
396 u8 *gb;
397 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300398
399 u8 tgt_available_prots;
400 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200401 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200402
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100403 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200404};
405
406struct pn533_cmd {
407 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200408 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200409 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100410 struct sk_buff *req;
411 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100412 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200413 pn533_send_async_complete_t complete_cb;
414 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300415};
416
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200417struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418 u8 preamble;
419 __be16 start_frame;
420 u8 datalen;
421 u8 datalen_checksum;
422 u8 data[];
423} __packed;
424
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200425struct pn533_ext_frame { /* Extended Information frame */
426 u8 preamble;
427 __be16 start_frame;
428 __be16 eif_flag; /* fixed to 0xFFFF */
429 __be16 datalen;
430 u8 datalen_checksum;
431 u8 data[];
432} __packed;
433
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100434struct pn533_frame_ops {
435 void (*tx_frame_init)(void *frame, u8 cmd_code);
436 void (*tx_frame_finish)(void *frame);
437 void (*tx_update_payload_len)(void *frame, int len);
438 int tx_header_len;
439 int tx_tail_len;
440
Olivier Guiter56a63c822013-06-13 13:43:29 +0000441 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100442 int (*rx_frame_size)(void *frame);
443 int rx_header_len;
444 int rx_tail_len;
445
446 int max_payload_len;
447 u8 (*get_cmd_code)(void *frame);
448};
449
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200450struct pn533_acr122_ccid_hdr {
451 u8 type;
452 u32 datalen;
453 u8 slot;
454 u8 seq;
455 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
456 byte for reposnse msg */
457 u8 data[]; /* payload */
458} __packed;
459
460struct pn533_acr122_apdu_hdr {
461 u8 class;
462 u8 ins;
463 u8 p1;
464 u8 p2;
465} __packed;
466
467struct pn533_acr122_tx_frame {
468 struct pn533_acr122_ccid_hdr ccid;
469 struct pn533_acr122_apdu_hdr apdu;
470 u8 datalen;
471 u8 data[]; /* pn533 frame: TFI ... */
472} __packed;
473
474struct pn533_acr122_rx_frame {
475 struct pn533_acr122_ccid_hdr ccid;
476 u8 data[]; /* pn533 frame : TFI ... */
477} __packed;
478
479static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
480{
481 struct pn533_acr122_tx_frame *frame = _frame;
482
483 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
484 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
485 sizeof(datalen) */
486 frame->ccid.slot = 0;
487 frame->ccid.seq = 0;
488 frame->ccid.params[0] = 0;
489 frame->ccid.params[1] = 0;
490 frame->ccid.params[2] = 0;
491
492 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
493 frame->data[1] = cmd_code;
494 frame->datalen = 2; /* data[0] + data[1] */
495
496 frame->apdu.class = 0xFF;
497 frame->apdu.ins = 0;
498 frame->apdu.p1 = 0;
499 frame->apdu.p2 = 0;
500}
501
502static void pn533_acr122_tx_frame_finish(void *_frame)
503{
504 struct pn533_acr122_tx_frame *frame = _frame;
505
506 frame->ccid.datalen += frame->datalen;
507}
508
509static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
510{
511 struct pn533_acr122_tx_frame *frame = _frame;
512
513 frame->datalen += len;
514}
515
Olivier Guiter56a63c822013-06-13 13:43:29 +0000516static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200517{
518 struct pn533_acr122_rx_frame *frame = _frame;
519
520 if (frame->ccid.type != 0x83)
521 return false;
522
523 if (frame->data[frame->ccid.datalen - 2] == 0x63)
524 return false;
525
526 return true;
527}
528
529static int pn533_acr122_rx_frame_size(void *frame)
530{
531 struct pn533_acr122_rx_frame *f = frame;
532
533 /* f->ccid.datalen already includes tail length */
534 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
535}
536
537static u8 pn533_acr122_get_cmd_code(void *frame)
538{
539 struct pn533_acr122_rx_frame *f = frame;
540
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200541 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200542}
543
544static struct pn533_frame_ops pn533_acr122_frame_ops = {
545 .tx_frame_init = pn533_acr122_tx_frame_init,
546 .tx_frame_finish = pn533_acr122_tx_frame_finish,
547 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
548 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
549 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
550
551 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
552 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
553 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
554 .rx_frame_size = pn533_acr122_rx_frame_size,
555
556 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
557 .get_cmd_code = pn533_acr122_get_cmd_code,
558};
559
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200560/* The rule: value(high byte) + value(low byte) + checksum = 0 */
561static inline u8 pn533_ext_checksum(u16 value)
562{
563 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
564}
565
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300566/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200567static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300568{
569 return ~value + 1;
570}
571
572/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200573static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300574{
575 u8 sum = 0;
576 int i;
577
578 for (i = 0; i < datalen; i++)
579 sum += data[i];
580
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200581 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300582}
583
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200584static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200586 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100587
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300588 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200589 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
590 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200591 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300592 frame->datalen = 2;
593}
594
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200595static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300596{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200597 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100598
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200599 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300600
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200601 PN533_STD_FRAME_CHECKSUM(frame) =
602 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300603
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200604 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300605}
606
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200607static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100608{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200609 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100610
611 frame->datalen += len;
612}
613
Olivier Guiter56a63c822013-06-13 13:43:29 +0000614static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300615{
616 u8 checksum;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200617 struct pn533_std_frame *stdf = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300618
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200619 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300620 return false;
621
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200622 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
623 /* Standard frame code */
Olivier Guiter56a63c822013-06-13 13:43:29 +0000624 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300625
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200626 checksum = pn533_std_checksum(stdf->datalen);
627 if (checksum != stdf->datalen_checksum)
628 return false;
629
630 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
631 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
632 return false;
633 } else {
634 /* Extended */
635 struct pn533_ext_frame *eif = _frame;
636
Olivier Guiter56a63c822013-06-13 13:43:29 +0000637 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
638
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200639 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
640 if (checksum != eif->datalen_checksum)
641 return false;
642
643 /* check data checksum */
644 checksum = pn533_std_data_checksum(eif->data,
645 be16_to_cpu(eif->datalen));
646 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
647 return false;
648 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300649
650 return true;
651}
652
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200653static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300654{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200655 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300656 return false;
657
658 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
659 return false;
660
661 return true;
662}
663
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200664static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300665{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200666 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100667
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200668 /* check for Extended Information frame */
669 if (PN533_STD_IS_EXTENDED(f)) {
670 struct pn533_ext_frame *eif = frame;
671
672 return sizeof(struct pn533_ext_frame)
673 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
674 }
675
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200676 return sizeof(struct pn533_std_frame) + f->datalen +
677 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100678}
679
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200680static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100681{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200682 struct pn533_std_frame *f = frame;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200683 struct pn533_ext_frame *eif = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100684
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200685 if (PN533_STD_IS_EXTENDED(f))
686 return PN533_FRAME_CMD(eif);
687 else
688 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100689}
690
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100691static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200692 .tx_frame_init = pn533_std_tx_frame_init,
693 .tx_frame_finish = pn533_std_tx_frame_finish,
694 .tx_update_payload_len = pn533_std_tx_update_payload_len,
695 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
696 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100697
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200698 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
699 .rx_frame_size = pn533_std_rx_frame_size,
700 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
701 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100702
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200703 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
704 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100705};
706
707static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
708{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200709 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200710 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300711}
712
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300713static void pn533_recv_response(struct urb *urb)
714{
715 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200716 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100717 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300718
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200719 cmd->status = urb->status;
720
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300721 switch (urb->status) {
722 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100723 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300724 case -ECONNRESET:
725 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -0700726 dev_dbg(&dev->interface->dev,
727 "The urb has been canceled (status %d)\n",
728 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200729 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100730 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300731 default:
Joe Perches073a6252013-04-05 12:27:38 -0700732 nfc_err(&dev->interface->dev,
733 "Urb failure (status %d)\n", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200734 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300735 }
736
737 in_frame = dev->in_urb->transfer_buffer;
738
Joe Perchesb4834832013-04-05 12:27:37 -0700739 dev_dbg(&dev->interface->dev, "Received a frame\n");
Samuel Ortize279f842013-03-20 11:27:57 +0100740 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
741 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100742
Olivier Guiter56a63c822013-06-13 13:43:29 +0000743 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
Joe Perches073a6252013-04-05 12:27:38 -0700744 nfc_err(&dev->interface->dev, "Received an invalid frame\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200745 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200746 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300747 }
748
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100749 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Joe Perches073a6252013-04-05 12:27:38 -0700750 nfc_err(&dev->interface->dev,
751 "It it not the response to the last command\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200752 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200753 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300754 }
755
Samuel Ortiz4849f852012-04-10 19:43:17 +0200756sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200757 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300758}
759
760static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
761{
762 dev->in_urb->complete = pn533_recv_response;
763
764 return usb_submit_urb(dev->in_urb, flags);
765}
766
767static void pn533_recv_ack(struct urb *urb)
768{
769 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200770 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200771 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300772 int rc;
773
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200774 cmd->status = urb->status;
775
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300776 switch (urb->status) {
777 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100778 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300779 case -ECONNRESET:
780 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -0700781 dev_dbg(&dev->interface->dev,
782 "The urb has been stopped (status %d)\n",
783 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200784 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100785 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300786 default:
Joe Perches073a6252013-04-05 12:27:38 -0700787 nfc_err(&dev->interface->dev,
788 "Urb failure (status %d)\n", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200789 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300790 }
791
792 in_frame = dev->in_urb->transfer_buffer;
793
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200794 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Joe Perches073a6252013-04-05 12:27:38 -0700795 nfc_err(&dev->interface->dev, "Received an invalid ack\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200796 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200797 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300798 }
799
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300800 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
801 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -0700802 nfc_err(&dev->interface->dev,
803 "usb_submit_urb failed with result %d\n", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200804 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200805 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300806 }
807
808 return;
809
Samuel Ortiz4849f852012-04-10 19:43:17 +0200810sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200811 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300812}
813
814static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
815{
816 dev->in_urb->complete = pn533_recv_ack;
817
818 return usb_submit_urb(dev->in_urb, flags);
819}
820
821static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
822{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200823 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100824 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300825 int rc;
826
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100827 dev->out_urb->transfer_buffer = ack;
828 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300829 rc = usb_submit_urb(dev->out_urb, flags);
830
831 return rc;
832}
833
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100834static int __pn533_send_frame_async(struct pn533 *dev,
835 struct sk_buff *out,
836 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200837 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300838{
839 int rc;
840
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100841 dev->out_urb->transfer_buffer = out->data;
842 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300843
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100844 dev->in_urb->transfer_buffer = in->data;
845 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300846
Samuel Ortize279f842013-03-20 11:27:57 +0100847 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
848 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100849
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100850 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300851 if (rc)
852 return rc;
853
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200854 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
855 /* request for response for sent packet directly */
856 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
857 if (rc)
858 goto error;
859 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
860 /* request for ACK if that's the case */
861 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
862 if (rc)
863 goto error;
864 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300865
866 return 0;
867
868error:
869 usb_unlink_urb(dev->out_urb);
870 return rc;
871}
872
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100873static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
874 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100875{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100876 /* payload is already there, just update datalen */
877 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100878 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100879
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100880
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100881 skb_push(skb, ops->tx_header_len);
882 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100883
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100884 ops->tx_frame_init(skb->data, cmd_code);
885 ops->tx_update_payload_len(skb->data, payload_len);
886 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100887}
888
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200889static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100890{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200891 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200892 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100893
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200894 struct sk_buff *req = cmd->req;
895 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100896
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100897 int rc;
898
899 dev_kfree_skb(req);
900
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100901 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200902 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
903 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100904 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200905 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100906 }
907
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100908 skb_put(resp, dev->ops->rx_frame_size(resp->data));
909 skb_pull(resp, dev->ops->rx_header_len);
910 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100911
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200912 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100913
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200914done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200915 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200916 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100917 return rc;
918}
919
920static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
921 struct sk_buff *req, struct sk_buff *resp,
922 int resp_len,
923 pn533_send_async_complete_t complete_cb,
924 void *complete_cb_context)
925{
926 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100927 int rc = 0;
928
Joe Perchesb4834832013-04-05 12:27:37 -0700929 dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100930
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200931 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
932 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100933 return -ENOMEM;
934
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200935 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200936 cmd->req = req;
937 cmd->resp = resp;
938 cmd->resp_len = resp_len;
939 cmd->complete_cb = complete_cb;
940 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100941
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100942 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100943
944 mutex_lock(&dev->cmd_lock);
945
946 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200947 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100948 if (rc)
949 goto error;
950
951 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200952 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100953 goto unlock;
954 }
955
Joe Perchesb4834832013-04-05 12:27:37 -0700956 dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n",
957 __func__, cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100958
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100959 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100960 list_add_tail(&cmd->queue, &dev->cmd_queue);
961
962 goto unlock;
963
964error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200965 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100966unlock:
967 mutex_unlock(&dev->cmd_lock);
968 return rc;
969}
970
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100971static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
972 struct sk_buff *req,
973 pn533_send_async_complete_t complete_cb,
974 void *complete_cb_context)
975{
976 struct sk_buff *resp;
977 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100978 int resp_len = dev->ops->rx_header_len +
979 dev->ops->max_payload_len +
980 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100981
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100982 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
983 if (!resp)
984 return -ENOMEM;
985
986 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
987 complete_cb_context);
988 if (rc)
989 dev_kfree_skb(resp);
990
991 return rc;
992}
993
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100994static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
995 struct sk_buff *req,
996 pn533_send_async_complete_t complete_cb,
997 void *complete_cb_context)
998{
999 struct sk_buff *resp;
1000 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001001 int resp_len = dev->ops->rx_header_len +
1002 dev->ops->max_payload_len +
1003 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001004
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001005 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001006 if (!resp)
1007 return -ENOMEM;
1008
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001009 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1010 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001011 if (rc)
1012 dev_kfree_skb(resp);
1013
1014 return rc;
1015}
1016
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001017/*
1018 * pn533_send_cmd_direct_async
1019 *
1020 * The function sends a piority cmd directly to the chip omiting the cmd
1021 * queue. It's intended to be used by chaining mechanism of received responses
1022 * where the host has to request every single chunk of data before scheduling
1023 * next cmd from the queue.
1024 */
1025static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1026 struct sk_buff *req,
1027 pn533_send_async_complete_t complete_cb,
1028 void *complete_cb_context)
1029{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001030 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001031 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001032 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001033 int resp_len = dev->ops->rx_header_len +
1034 dev->ops->max_payload_len +
1035 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001036
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001037 resp = alloc_skb(resp_len, GFP_KERNEL);
1038 if (!resp)
1039 return -ENOMEM;
1040
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001041 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1042 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001043 dev_kfree_skb(resp);
1044 return -ENOMEM;
1045 }
1046
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +02001047 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001048 cmd->req = req;
1049 cmd->resp = resp;
1050 cmd->resp_len = resp_len;
1051 cmd->complete_cb = complete_cb;
1052 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001053
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001054 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001055
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001056 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001057 if (rc < 0) {
1058 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001059 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001060 } else {
1061 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001062 }
1063
1064 return rc;
1065}
1066
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +02001067static void pn533_wq_cmd_complete(struct work_struct *work)
1068{
1069 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1070 int rc;
1071
1072 rc = pn533_send_async_complete(dev);
1073 if (rc != -EINPROGRESS)
1074 queue_work(dev->wq, &dev->cmd_work);
1075}
1076
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001077static void pn533_wq_cmd(struct work_struct *work)
1078{
1079 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1080 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001081 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001082
1083 mutex_lock(&dev->cmd_lock);
1084
1085 if (list_empty(&dev->cmd_queue)) {
1086 dev->cmd_pending = 0;
1087 mutex_unlock(&dev->cmd_lock);
1088 return;
1089 }
1090
1091 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1092
Szymon Janc60ad07a2012-10-25 17:29:45 +02001093 list_del(&cmd->queue);
1094
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001095 mutex_unlock(&dev->cmd_lock);
1096
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001097 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001098 if (rc < 0) {
1099 dev_kfree_skb(cmd->req);
1100 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001101 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001102 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001103 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001104
1105 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001106}
1107
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001108struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001109 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001110 struct completion done;
1111};
1112
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001113static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1114 struct sk_buff *resp)
1115{
1116 struct pn533_sync_cmd_response *arg = _arg;
1117
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001118 arg->resp = resp;
1119 complete(&arg->done);
1120
1121 return 0;
1122}
1123
1124/* pn533_send_cmd_sync
1125 *
1126 * Please note the req parameter is freed inside the function to
1127 * limit a number of return value interpretations by the caller.
1128 *
1129 * 1. negative in case of error during TX path -> req should be freed
1130 *
1131 * 2. negative in case of error during RX path -> req should not be freed
1132 * as it's been already freed at the begining of RX path by
1133 * async_complete_cb.
1134 *
1135 * 3. valid pointer in case of succesfult RX path
1136 *
1137 * A caller has to check a return value with IS_ERR macro. If the test pass,
1138 * the returned pointer is valid.
1139 *
1140 * */
1141static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1142 struct sk_buff *req)
1143{
1144 int rc;
1145 struct pn533_sync_cmd_response arg;
1146
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001147 init_completion(&arg.done);
1148
1149 rc = pn533_send_cmd_async(dev, cmd_code, req,
1150 pn533_send_sync_complete, &arg);
1151 if (rc) {
1152 dev_kfree_skb(req);
1153 return ERR_PTR(rc);
1154 }
1155
1156 wait_for_completion(&arg.done);
1157
1158 return arg.resp;
1159}
1160
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001161static void pn533_send_complete(struct urb *urb)
1162{
1163 struct pn533 *dev = urb->context;
1164
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001165 switch (urb->status) {
1166 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001167 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001168 case -ECONNRESET:
1169 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -07001170 dev_dbg(&dev->interface->dev,
1171 "The urb has been stopped (status %d)\n",
1172 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001173 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001174 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001175 default:
Joe Perches073a6252013-04-05 12:27:38 -07001176 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
1177 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001178 }
1179}
1180
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001181static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1182{
1183 /* ACR122U does not support any command which aborts last
1184 * issued command i.e. as ACK for standard PN533. Additionally,
1185 * it behaves stange, sending broken or incorrect responses,
1186 * when we cancel urb before the chip will send response.
1187 */
1188 if (dev->device_type == PN533_DEVICE_ACR122U)
1189 return;
1190
1191 /* An ack will cancel the last issued command */
1192 pn533_send_ack(dev, flags);
1193
1194 /* cancel the urb request */
1195 usb_kill_urb(dev->in_urb);
1196}
1197
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001198static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001199{
1200 struct sk_buff *skb;
1201
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001202 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001203 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001204 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001205
1206 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001207 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001208
1209 return skb;
1210}
1211
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001212struct pn533_target_type_a {
1213 __be16 sens_res;
1214 u8 sel_res;
1215 u8 nfcid_len;
1216 u8 nfcid_data[];
1217} __packed;
1218
1219
1220#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1221#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1222#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1223
1224#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1225#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1226
1227#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1228#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1229
1230#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1231#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1232#define PN533_TYPE_A_SEL_PROT_DEP 2
1233#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1234
1235static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1236 int target_data_len)
1237{
1238 u8 ssd;
1239 u8 platconf;
1240
1241 if (target_data_len < sizeof(struct pn533_target_type_a))
1242 return false;
1243
1244 /* The lenght check of nfcid[] and ats[] are not being performed because
1245 the values are not being used */
1246
1247 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1248 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1249 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1250
1251 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001252 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1253 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1254 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001255 return false;
1256
1257 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1258 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1259 return false;
1260
1261 return true;
1262}
1263
1264static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1265 int tgt_data_len)
1266{
1267 struct pn533_target_type_a *tgt_type_a;
1268
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001269 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001270
1271 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1272 return -EPROTO;
1273
1274 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1275 case PN533_TYPE_A_SEL_PROT_MIFARE:
1276 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1277 break;
1278 case PN533_TYPE_A_SEL_PROT_ISO14443:
1279 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1280 break;
1281 case PN533_TYPE_A_SEL_PROT_DEP:
1282 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1283 break;
1284 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1285 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1286 NFC_PROTO_NFC_DEP_MASK;
1287 break;
1288 }
1289
1290 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1291 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001292 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1293 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001294
1295 return 0;
1296}
1297
1298struct pn533_target_felica {
1299 u8 pol_res;
1300 u8 opcode;
Samuel Ortiz322bce92013-05-27 15:29:11 +02001301 u8 nfcid2[NFC_NFCID2_MAXSIZE];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001302 u8 pad[8];
1303 /* optional */
1304 u8 syst_code[];
1305} __packed;
1306
1307#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1308#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1309
1310static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1311 int target_data_len)
1312{
1313 if (target_data_len < sizeof(struct pn533_target_felica))
1314 return false;
1315
1316 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1317 return false;
1318
1319 return true;
1320}
1321
1322static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1323 int tgt_data_len)
1324{
1325 struct pn533_target_felica *tgt_felica;
1326
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001327 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328
1329 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1330 return -EPROTO;
1331
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001332 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1333 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001334 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1335 else
1336 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1337
Samuel Ortiz79757542012-03-05 01:03:45 +01001338 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1339 nfc_tgt->sensf_res_len = 9;
1340
Samuel Ortiz322bce92013-05-27 15:29:11 +02001341 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1342 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1343
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001344 return 0;
1345}
1346
1347struct pn533_target_jewel {
1348 __be16 sens_res;
1349 u8 jewelid[4];
1350} __packed;
1351
1352static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1353 int target_data_len)
1354{
1355 u8 ssd;
1356 u8 platconf;
1357
1358 if (target_data_len < sizeof(struct pn533_target_jewel))
1359 return false;
1360
1361 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1362 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1363 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1364
1365 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001366 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1367 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1368 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001369 return false;
1370
1371 return true;
1372}
1373
1374static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1375 int tgt_data_len)
1376{
1377 struct pn533_target_jewel *tgt_jewel;
1378
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001379 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001380
1381 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1382 return -EPROTO;
1383
1384 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1385 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001386 nfc_tgt->nfcid1_len = 4;
1387 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001388
1389 return 0;
1390}
1391
1392struct pn533_type_b_prot_info {
1393 u8 bitrate;
1394 u8 fsci_type;
1395 u8 fwi_adc_fo;
1396} __packed;
1397
1398#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1399#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1400#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1401
1402struct pn533_type_b_sens_res {
1403 u8 opcode;
1404 u8 nfcid[4];
1405 u8 appdata[4];
1406 struct pn533_type_b_prot_info prot_info;
1407} __packed;
1408
1409#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1410
1411struct pn533_target_type_b {
1412 struct pn533_type_b_sens_res sensb_res;
1413 u8 attrib_res_len;
1414 u8 attrib_res[];
1415} __packed;
1416
1417static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1418 int target_data_len)
1419{
1420 if (target_data_len < sizeof(struct pn533_target_type_b))
1421 return false;
1422
1423 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1424 return false;
1425
1426 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1427 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1428 return false;
1429
1430 return true;
1431}
1432
1433static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1434 int tgt_data_len)
1435{
1436 struct pn533_target_type_b *tgt_type_b;
1437
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001438 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001439
1440 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1441 return -EPROTO;
1442
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001443 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001444
1445 return 0;
1446}
1447
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001448static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1449 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001450{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001451 struct nfc_target nfc_tgt;
1452 int rc;
1453
Joe Perches17936b42013-04-05 12:27:39 -07001454 dev_dbg(&dev->interface->dev, "%s: modulation=%d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001455 __func__, dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001456
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001457 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001458 return -EPROTO;
1459
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001460 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1461
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001462 switch (dev->poll_mod_curr) {
1463 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001464 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001465 break;
1466 case PN533_POLL_MOD_212KBPS_FELICA:
1467 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001468 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001469 break;
1470 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001472 break;
1473 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001475 break;
1476 default:
Joe Perches073a6252013-04-05 12:27:38 -07001477 nfc_err(&dev->interface->dev,
1478 "Unknown current poll modulation\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001479 return -EPROTO;
1480 }
1481
1482 if (rc)
1483 return rc;
1484
1485 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Joe Perchesb4834832013-04-05 12:27:37 -07001486 dev_dbg(&dev->interface->dev,
1487 "The Tg found doesn't have the desired protocol\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001488 return -EAGAIN;
1489 }
1490
Joe Perchesb4834832013-04-05 12:27:37 -07001491 dev_dbg(&dev->interface->dev,
1492 "Target found - supported protocols: 0x%x\n",
1493 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001494
1495 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1496
1497 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1498
1499 return 0;
1500}
1501
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502static inline void pn533_poll_next_mod(struct pn533 *dev)
1503{
1504 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1505}
1506
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001507static void pn533_poll_reset_mod_list(struct pn533 *dev)
1508{
1509 dev->poll_mod_count = 0;
1510}
1511
1512static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1513{
1514 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001515 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001516 dev->poll_mod_count++;
1517}
1518
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001519static void pn533_poll_create_mod_list(struct pn533 *dev,
1520 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001521{
1522 pn533_poll_reset_mod_list(dev);
1523
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001524 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1525 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1526 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001527 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1528
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001529 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1530 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001531 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1532 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1533 }
1534
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001535 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001536 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1537
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001538 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001539 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001540
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001541 if (tm_protocols)
1542 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001543}
1544
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001546{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001547 u8 nbtg, tg, *tgdata;
1548 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001549
Samuel Ortiz673088f2013-08-21 15:06:55 +02001550 /* Toggle the DEP polling */
1551 dev->poll_dep = 1;
1552
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001553 nbtg = resp->data[0];
1554 tg = resp->data[1];
1555 tgdata = &resp->data[2];
1556 tgdata_len = resp->len - 2; /* nbtg + tg */
1557
1558 if (nbtg) {
1559 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001560
1561 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562 if (rc == 0) {
1563 pn533_poll_reset_mod_list(dev);
1564 return 0;
1565 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001566 }
1567
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001569}
1570
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001571static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001572{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001573 struct sk_buff *skb;
1574 u8 *felica, *nfcid3, *gb;
1575
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001576 u8 *gbytes = dev->gb;
1577 size_t gbytes_len = dev->gb_len;
1578
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001579 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1580 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1581 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1582 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001583
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001584 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1585 0x0, 0x0, 0x0,
1586 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001587
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001588 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1589 felica (18), nfcid3 (10), gb_len (1) */
1590 gbytes_len +
1591 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001592
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001593 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001594 if (!skb)
1595 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001596
1597 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001598 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001599
1600 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001601 memcpy(skb_put(skb, 6), mifare_params, 6);
1602
1603 /* Felica params */
1604 felica = skb_put(skb, 18);
1605 memcpy(felica, felica_params, 18);
1606 get_random_bytes(felica + 2, 6);
1607
1608 /* NFCID3 */
1609 nfcid3 = skb_put(skb, 10);
1610 memset(nfcid3, 0, 10);
1611 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001612
1613 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001614 *skb_put(skb, 1) = gbytes_len;
1615
1616 gb = skb_put(skb, gbytes_len);
1617 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001618
Samuel Ortizad3823c2012-05-30 23:54:55 +02001619 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001620 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001621
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001622 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001623}
1624
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001625#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001626#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1627static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001628 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001629{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001630 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001631
Joe Perchesb4834832013-04-05 12:27:37 -07001632 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001633
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001634 if (IS_ERR(resp))
1635 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001636
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001637 status = resp->data[0];
1638 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001639
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001640 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001641 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001642 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001643 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001644 return 0;
1645 }
1646
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001647 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001648}
1649
1650static void pn533_wq_tg_get_data(struct work_struct *work)
1651{
1652 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001653 struct sk_buff *skb;
1654 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001655
Joe Perchesb4834832013-04-05 12:27:37 -07001656 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001657
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001658 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001659 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001660 return;
1661
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001662 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1663 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001664
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001665 if (rc < 0)
1666 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001667
1668 return;
1669}
1670
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001671#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001672static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001673{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001674 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001675 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001676 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001677
Joe Perchesb4834832013-04-05 12:27:37 -07001678 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001679
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001680 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001681 return -EINVAL;
1682
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001683 mode = resp->data[0];
1684 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001685
Joe Perchesb4834832013-04-05 12:27:37 -07001686 dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1687 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001688
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001689 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1690 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001691 comm_mode = NFC_COMM_ACTIVE;
1692
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001693 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001694 return -EOPNOTSUPP;
1695
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001696 gb = cmd + ATR_REQ_GB_OFFSET;
1697 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001698
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001699 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1700 comm_mode, gb, gb_len);
1701 if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07001702 nfc_err(&dev->interface->dev,
1703 "Error when signaling target activation\n");
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001704 return rc;
1705 }
1706
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001707 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001708 queue_work(dev->wq, &dev->tg_work);
1709
1710 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001711}
1712
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001713static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001714{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001715 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001716
Joe Perchesb4834832013-04-05 12:27:37 -07001717 dev_dbg(&dev->interface->dev, "Listen mode timeout\n");
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001718
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001719 dev->cancel_listen = 1;
1720
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001721 pn533_poll_next_mod(dev);
1722
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001723 queue_delayed_work(dev->wq, &dev->poll_work,
1724 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001725}
1726
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001727static int pn533_rf_complete(struct pn533 *dev, void *arg,
1728 struct sk_buff *resp)
1729{
1730 int rc = 0;
1731
Joe Perchesb4834832013-04-05 12:27:37 -07001732 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001733
1734 if (IS_ERR(resp)) {
1735 rc = PTR_ERR(resp);
1736
Joe Perches073a6252013-04-05 12:27:38 -07001737 nfc_err(&dev->interface->dev, "RF setting error %d", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001738
1739 return rc;
1740 }
1741
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001742 queue_delayed_work(dev->wq, &dev->poll_work,
1743 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001744
1745 dev_kfree_skb(resp);
1746 return rc;
1747}
1748
1749static void pn533_wq_rf(struct work_struct *work)
1750{
1751 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1752 struct sk_buff *skb;
1753 int rc;
1754
Joe Perchesb4834832013-04-05 12:27:37 -07001755 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001756
1757 skb = pn533_alloc_skb(dev, 2);
1758 if (!skb)
1759 return;
1760
1761 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02001762 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001763
1764 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1765 pn533_rf_complete, NULL);
1766 if (rc < 0) {
1767 dev_kfree_skb(skb);
Joe Perches073a6252013-04-05 12:27:38 -07001768 nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001769 }
1770
1771 return;
1772}
1773
Samuel Ortiz673088f2013-08-21 15:06:55 +02001774static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1775 struct sk_buff *resp)
1776{
1777 struct pn533_cmd_jump_dep_response *rsp;
1778 struct nfc_target nfc_target;
1779 u8 target_gt_len;
1780 int rc;
1781
1782 if (IS_ERR(resp))
1783 return PTR_ERR(resp);
1784
1785 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1786
1787 rc = rsp->status & PN533_CMD_RET_MASK;
1788 if (rc != PN533_CMD_RET_SUCCESS) {
1789 /* Not target found, turn radio off */
1790 queue_work(dev->wq, &dev->rf_work);
1791
1792 dev_kfree_skb(resp);
1793 return 0;
1794 }
1795
1796 dev_dbg(&dev->interface->dev, "Creating new target");
1797
1798 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1799 nfc_target.nfcid1_len = 10;
1800 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1801 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1802 if (rc)
1803 goto error;
1804
1805 dev->tgt_available_prots = 0;
1806 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1807
1808 /* ATR_RES general bytes are located at offset 17 */
1809 target_gt_len = resp->len - 17;
1810 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1811 rsp->gt, target_gt_len);
1812 if (!rc) {
1813 rc = nfc_dep_link_is_up(dev->nfc_dev,
1814 dev->nfc_dev->targets[0].idx,
1815 0, NFC_RF_INITIATOR);
1816
1817 if (!rc)
1818 pn533_poll_reset_mod_list(dev);
1819 }
1820error:
1821 dev_kfree_skb(resp);
1822 return rc;
1823}
1824
1825#define PASSIVE_DATA_LEN 5
1826static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1827{
1828 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1829 struct sk_buff *skb;
1830 int rc, skb_len;
1831 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1832 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1833
1834 dev_dbg(&dev->interface->dev, "%s", __func__);
1835
1836 if (!dev->gb) {
1837 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1838
1839 if (!dev->gb || !dev->gb_len) {
1840 dev->poll_dep = 0;
1841 queue_work(dev->wq, &dev->rf_work);
1842 }
1843 }
1844
1845 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1846 skb_len += PASSIVE_DATA_LEN;
1847
1848 /* NFCID3 */
1849 skb_len += NFC_NFCID3_MAXSIZE;
1850 nfcid3[0] = 0x1;
1851 nfcid3[1] = 0xfe;
1852 get_random_bytes(nfcid3 + 2, 6);
1853
1854 skb = pn533_alloc_skb(dev, skb_len);
1855 if (!skb)
1856 return -ENOMEM;
1857
1858 *skb_put(skb, 1) = 0x01; /* Active */
1859 *skb_put(skb, 1) = 0x02; /* 424 kbps */
1860
1861 next = skb_put(skb, 1); /* Next */
1862 *next = 0;
1863
1864 /* Copy passive data */
1865 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1866 *next |= 1;
1867
1868 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1869 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1870 NFC_NFCID3_MAXSIZE);
1871 *next |= 2;
1872
1873 memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1874 *next |= 4; /* We have some Gi */
1875
1876 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1877 pn533_poll_dep_complete, NULL);
1878
1879 if (rc < 0)
1880 dev_kfree_skb(skb);
1881
1882 return rc;
1883}
1884
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001885static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001886 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001887{
1888 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001889 int rc;
1890
Joe Perchesb4834832013-04-05 12:27:37 -07001891 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001892
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001893 if (IS_ERR(resp)) {
1894 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001895
Joe Perches073a6252013-04-05 12:27:38 -07001896 nfc_err(&dev->interface->dev, "%s Poll complete error %d\n",
1897 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001898
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001899 if (rc == -ENOENT) {
1900 if (dev->poll_mod_count != 0)
1901 return rc;
1902 else
1903 goto stop_poll;
1904 } else if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07001905 nfc_err(&dev->interface->dev,
1906 "Error %d when running poll\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001907 goto stop_poll;
1908 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001909 }
1910
1911 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1912
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001913 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001914 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001915 rc = pn533_init_target_complete(dev, resp);
1916 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001917 }
1918
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001919 /* Initiator mode */
1920 rc = pn533_start_poll_complete(dev, resp);
1921 if (!rc)
1922 goto done;
1923
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001924 if (!dev->poll_mod_count) {
Joe Perchesb4834832013-04-05 12:27:37 -07001925 dev_dbg(&dev->interface->dev, "Polling has been stopped\n");
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001926 goto done;
1927 }
1928
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001929 pn533_poll_next_mod(dev);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001930 /* Not target found, turn radio off */
1931 queue_work(dev->wq, &dev->rf_work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001932
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001933done:
1934 dev_kfree_skb(resp);
1935 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001936
1937stop_poll:
Joe Perches073a6252013-04-05 12:27:38 -07001938 nfc_err(&dev->interface->dev, "Polling operation has been stopped\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001939
Samuel Ortizad3823c2012-05-30 23:54:55 +02001940 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001941 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001942 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001943}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001944
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001945static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1946 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001947{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001948 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001949
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001950 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001951 if (!skb)
1952 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001953
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001954 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001955
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001956 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001957}
1958
1959static int pn533_send_poll_frame(struct pn533 *dev)
1960{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001961 struct pn533_poll_modulations *mod;
1962 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001963 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001964 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001965
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001966 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001967
Joe Perchesb4834832013-04-05 12:27:37 -07001968 dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1969 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001970
Samuel Ortiz673088f2013-08-21 15:06:55 +02001971 if (dev->poll_dep) {
1972 dev->poll_dep = 0;
1973 return pn533_poll_dep(dev->nfc_dev);
1974 }
1975
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001976 if (mod->len == 0) { /* Listen mode */
1977 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001978 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001979 } else { /* Polling mode */
1980 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001981 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001982 }
1983
1984 if (!skb) {
Joe Perches073a6252013-04-05 12:27:38 -07001985 nfc_err(&dev->interface->dev, "Failed to allocate skb\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001986 return -ENOMEM;
1987 }
1988
1989 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1990 NULL);
1991 if (rc < 0) {
1992 dev_kfree_skb(skb);
Joe Perches073a6252013-04-05 12:27:38 -07001993 nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001994 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001995
1996 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001997}
1998
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001999static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002000{
Samuel Ortiz46f793b2013-07-03 14:50:36 +02002001 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002002 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002003 int rc;
2004
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002005 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2006
Joe Perchesb4834832013-04-05 12:27:37 -07002007 dev_dbg(&dev->interface->dev,
2008 "%s cancel_listen %d modulation len %d\n",
2009 __func__, dev->cancel_listen, cur_mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002010
2011 if (dev->cancel_listen == 1) {
2012 dev->cancel_listen = 0;
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002013 pn533_abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002014 }
2015
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002016 rc = pn533_send_poll_frame(dev);
2017 if (rc)
2018 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002019
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002020 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
2021 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002022
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002023 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002024}
2025
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002026static int pn533_start_poll(struct nfc_dev *nfc_dev,
2027 u32 im_protocols, u32 tm_protocols)
2028{
2029 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002030 struct pn533_poll_modulations *cur_mod;
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02002031 u8 rand_mod;
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002032 int rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002033
Joe Perchesb4834832013-04-05 12:27:37 -07002034 dev_dbg(&dev->interface->dev,
2035 "%s: im protocols 0x%x tm protocols 0x%x\n",
2036 __func__, im_protocols, tm_protocols);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002037
2038 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002039 nfc_err(&dev->interface->dev,
2040 "Cannot poll with a target already activated\n");
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002041 return -EBUSY;
2042 }
2043
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002044 if (dev->tgt_mode) {
Joe Perches073a6252013-04-05 12:27:38 -07002045 nfc_err(&dev->interface->dev,
2046 "Cannot poll while already being activated\n");
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002047 return -EBUSY;
2048 }
2049
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002050 if (tm_protocols) {
2051 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
2052 if (dev->gb == NULL)
2053 tm_protocols = 0;
2054 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02002055
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002056 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
2057 dev->poll_protocols = im_protocols;
2058 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02002059
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02002060 /* Do not always start polling from the same modulation */
2061 get_random_bytes(&rand_mod, sizeof(rand_mod));
2062 rand_mod %= dev->poll_mod_count;
2063 dev->poll_mod_curr = rand_mod;
2064
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002065 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2066
2067 rc = pn533_send_poll_frame(dev);
2068
2069 /* Start listen timer */
2070 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
2071 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
2072
2073 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002074}
2075
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002076static void pn533_stop_poll(struct nfc_dev *nfc_dev)
2077{
2078 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2079
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002080 del_timer(&dev->listen_timer);
2081
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002082 if (!dev->poll_mod_count) {
Joe Perchesb4834832013-04-05 12:27:37 -07002083 dev_dbg(&dev->interface->dev,
2084 "Polling operation was not running\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085 return;
2086 }
2087
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002088 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02002089 flush_delayed_work(&dev->poll_work);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02002090 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002091}
2092
2093static int pn533_activate_target_nfcdep(struct pn533 *dev)
2094{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002095 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01002096 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002097 int rc;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002098 struct sk_buff *skb;
2099 struct sk_buff *resp;
2100
Joe Perchesb4834832013-04-05 12:27:37 -07002101 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002102
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002103 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002104 if (!skb)
2105 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002106
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002107 *skb_put(skb, sizeof(u8)) = 1; /* TG */
2108 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002109
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002110 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
2111 if (IS_ERR(resp))
2112 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002113
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002114 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002115 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01002116 if (rc != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002117 nfc_err(&dev->interface->dev,
2118 "Target activation failed (error 0x%x)\n", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002119 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01002121 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002122
Samuel Ortiz541d9202011-12-14 16:43:10 +01002123 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002124 gt_len = resp->len - 16;
2125 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01002126
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002127 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01002128 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002129}
2130
Eric Lapuyade90099432012-05-07 12:31:13 +02002131static int pn533_activate_target(struct nfc_dev *nfc_dev,
2132 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002133{
2134 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2135 int rc;
2136
Joe Perches17936b42013-04-05 12:27:39 -07002137 dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002138
2139 if (dev->poll_mod_count) {
Joe Perches073a6252013-04-05 12:27:38 -07002140 nfc_err(&dev->interface->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07002141 "Cannot activate while polling\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002142 return -EBUSY;
2143 }
2144
2145 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002146 nfc_err(&dev->interface->dev,
2147 "There is already an active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002148 return -EBUSY;
2149 }
2150
2151 if (!dev->tgt_available_prots) {
Joe Perches073a6252013-04-05 12:27:38 -07002152 nfc_err(&dev->interface->dev,
2153 "There is no available target to activate\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002154 return -EINVAL;
2155 }
2156
2157 if (!(dev->tgt_available_prots & (1 << protocol))) {
Joe Perches073a6252013-04-05 12:27:38 -07002158 nfc_err(&dev->interface->dev,
2159 "Target doesn't support requested proto %u\n",
2160 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161 return -EINVAL;
2162 }
2163
2164 if (protocol == NFC_PROTO_NFC_DEP) {
2165 rc = pn533_activate_target_nfcdep(dev);
2166 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002167 nfc_err(&dev->interface->dev,
2168 "Activating target with DEP failed %d\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002169 return rc;
2170 }
2171 }
2172
2173 dev->tgt_active_prot = protocol;
2174 dev->tgt_available_prots = 0;
2175
2176 return 0;
2177}
2178
Eric Lapuyade90099432012-05-07 12:31:13 +02002179static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2180 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002181{
2182 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002183 struct sk_buff *skb;
2184 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002185 int rc;
2186
Joe Perchesb4834832013-04-05 12:27:37 -07002187 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002188
2189 if (!dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002190 nfc_err(&dev->interface->dev, "There is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002191 return;
2192 }
2193
2194 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002195 skb_queue_purge(&dev->resp_q);
2196
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002197 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002198 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002199 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002200
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002201 *skb_put(skb, 1) = 1; /* TG*/
2202
2203 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2204 if (IS_ERR(resp))
2205 return;
2206
2207 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002208 if (rc != PN533_CMD_RET_SUCCESS)
Joe Perches073a6252013-04-05 12:27:38 -07002209 nfc_err(&dev->interface->dev,
2210 "Error 0x%x when releasing the target\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002211
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002212 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002213 return;
2214}
2215
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002216
2217static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002218 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002219{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002220 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002221 u8 target_gt_len;
2222 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002223 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02002224
2225 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002226
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002227 if (IS_ERR(resp))
2228 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002229
2230 if (dev->tgt_available_prots &&
2231 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
Joe Perches073a6252013-04-05 12:27:38 -07002232 nfc_err(&dev->interface->dev,
2233 "The target does not support DEP\n");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002234 rc = -EINVAL;
2235 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002236 }
2237
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002238 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2239
2240 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002241 if (rc != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002242 nfc_err(&dev->interface->dev,
2243 "Bringing DEP link up failed (error 0x%x)\n", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002244 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002245 }
2246
2247 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002248 struct nfc_target nfc_target;
2249
Joe Perchesb4834832013-04-05 12:27:37 -07002250 dev_dbg(&dev->interface->dev, "Creating new target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002251
2252 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01002253 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002254 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002255 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2256 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002257 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002258
2259 dev->tgt_available_prots = 0;
2260 }
2261
2262 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2263
2264 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002265 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002266 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002267 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002268 if (rc == 0)
2269 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002270 dev->nfc_dev->targets[0].idx,
2271 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002272
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002273error:
2274 dev_kfree_skb(resp);
2275 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002276}
2277
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002278static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
Eric Lapuyade90099432012-05-07 12:31:13 +02002279static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002280 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002281{
2282 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002283 struct sk_buff *skb;
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002284 int rc, skb_len;
2285 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
Samuel Ortizd7f33452012-05-29 21:45:21 +02002286 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002287
Joe Perchesb4834832013-04-05 12:27:37 -07002288 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002289
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002290 if (dev->poll_mod_count) {
Joe Perches073a6252013-04-05 12:27:38 -07002291 nfc_err(&dev->interface->dev,
2292 "Cannot bring the DEP link up while polling\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002293 return -EBUSY;
2294 }
2295
2296 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002297 nfc_err(&dev->interface->dev,
2298 "There is already an active target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002299 return -EBUSY;
2300 }
2301
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002302 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002303 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002304
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002305 /* NFCID3 */
2306 skb_len += NFC_NFCID3_MAXSIZE;
2307 if (target && !target->nfcid2_len) {
2308 nfcid3[0] = 0x1;
2309 nfcid3[1] = 0xfe;
2310 get_random_bytes(nfcid3 + 2, 6);
2311 }
Samuel Ortiz322bce92013-05-27 15:29:11 +02002312
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002313 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002314 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002315 return -ENOMEM;
2316
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002317 *skb_put(skb, 1) = !comm_mode; /* ActPass */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002318 *skb_put(skb, 1) = 0x02; /* 424 kbps */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002319
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002320 next = skb_put(skb, 1); /* Next */
2321 *next = 0;
2322
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002323 /* Copy passive data */
2324 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2325 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002326
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002327 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2328 if (target && target->nfcid2_len)
Samuel Ortiz322bce92013-05-27 15:29:11 +02002329 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2330 target->nfcid2_len);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002331 else
2332 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2333 NFC_NFCID3_MAXSIZE);
2334 *next |= 2;
Samuel Ortiz322bce92013-05-27 15:29:11 +02002335
Samuel Ortiz47807d32012-03-05 01:03:50 +01002336 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002337 memcpy(skb_put(skb, gb_len), gb, gb_len);
2338 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002339 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002340 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002341 }
2342
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002343 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2344 if (!arg) {
2345 dev_kfree_skb(skb);
2346 return -ENOMEM;
2347 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002348
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002349 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002350
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002351 pn533_rf_field(dev->nfc_dev, 0);
2352
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002353 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2354 pn533_in_dep_link_up_complete, arg);
2355
2356 if (rc < 0) {
2357 dev_kfree_skb(skb);
2358 kfree(arg);
2359 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002360
2361 return rc;
2362}
2363
2364static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2365{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002366 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2367
Joe Perchesb4834832013-04-05 12:27:37 -07002368 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002369
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002370 pn533_poll_reset_mod_list(dev);
2371
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002372 if (dev->tgt_mode || dev->tgt_active_prot)
2373 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002374
2375 dev->tgt_active_prot = 0;
2376 dev->tgt_mode = 0;
2377
2378 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002379
2380 return 0;
2381}
2382
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002383struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002384 data_exchange_cb_t cb;
2385 void *cb_context;
2386};
2387
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002388static struct sk_buff *pn533_build_response(struct pn533 *dev)
2389{
2390 struct sk_buff *skb, *tmp, *t;
2391 unsigned int skb_len = 0, tmp_len = 0;
2392
Joe Perchesb4834832013-04-05 12:27:37 -07002393 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002394
2395 if (skb_queue_empty(&dev->resp_q))
2396 return NULL;
2397
2398 if (skb_queue_len(&dev->resp_q) == 1) {
2399 skb = skb_dequeue(&dev->resp_q);
2400 goto out;
2401 }
2402
2403 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2404 skb_len += tmp->len;
2405
Joe Perchesb4834832013-04-05 12:27:37 -07002406 dev_dbg(&dev->interface->dev, "%s total length %d\n",
2407 __func__, skb_len);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002408
2409 skb = alloc_skb(skb_len, GFP_KERNEL);
2410 if (skb == NULL)
2411 goto out;
2412
2413 skb_put(skb, skb_len);
2414
2415 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2416 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2417 tmp_len += tmp->len;
2418 }
2419
2420out:
2421 skb_queue_purge(&dev->resp_q);
2422
2423 return skb;
2424}
2425
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002426static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002427 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428{
2429 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002430 struct sk_buff *skb;
2431 int rc = 0;
2432 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002433
Joe Perchesb4834832013-04-05 12:27:37 -07002434 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002435
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002436 if (IS_ERR(resp)) {
2437 rc = PTR_ERR(resp);
2438 goto _error;
2439 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002440
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002441 status = resp->data[0];
2442 ret = status & PN533_CMD_RET_MASK;
2443 mi = status & PN533_CMD_MI_MASK;
2444
2445 skb_pull(resp, sizeof(status));
2446
2447 if (ret != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002448 nfc_err(&dev->interface->dev,
2449 "Exchanging data failed (error 0x%x)\n", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002450 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002451 goto error;
2452 }
2453
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002454 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002456 if (mi) {
2457 dev->cmd_complete_mi_arg = arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +00002458 queue_work(dev->wq, &dev->mi_rx_work);
2459 return -EINPROGRESS;
2460 }
2461
2462 /* Prepare for the next round */
2463 if (skb_queue_len(&dev->fragment_skb) > 0) {
2464 dev->cmd_complete_dep_arg = arg;
2465 queue_work(dev->wq, &dev->mi_tx_work);
2466
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002467 return -EINPROGRESS;
2468 }
2469
2470 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002471 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002472 goto error;
2473
2474 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002475 kfree(arg);
2476 return 0;
2477
2478error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002479 dev_kfree_skb(resp);
2480_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002481 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002482 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002483 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002484 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002485}
2486
Olivier Guiter963a82e2013-06-13 13:43:28 +00002487/* Split the Tx skb into small chunks */
2488static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2489{
2490 struct sk_buff *frag;
2491 int frag_size;
2492
2493 do {
2494 /* Remaining size */
2495 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2496 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2497 else
2498 frag_size = skb->len;
2499
2500 /* Allocate and reserve */
2501 frag = pn533_alloc_skb(dev, frag_size);
2502 if (!frag) {
2503 skb_queue_purge(&dev->fragment_skb);
2504 break;
2505 }
2506
2507 /* Reserve the TG/MI byte */
2508 skb_reserve(frag, 1);
2509
2510 /* MI + TG */
2511 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2512 *skb_push(frag, sizeof(u8)) = (PN533_CMD_MI_MASK | 1);
2513 else
2514 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2515
2516 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2517
2518 /* Reduce the size of incoming buffer */
2519 skb_pull(skb, frag_size);
2520
2521 /* Add this to skb_queue */
2522 skb_queue_tail(&dev->fragment_skb, frag);
2523
2524 } while (skb->len > 0);
2525
2526 dev_kfree_skb(skb);
2527
2528 return skb_queue_len(&dev->fragment_skb);
2529}
2530
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002531static int pn533_transceive(struct nfc_dev *nfc_dev,
2532 struct nfc_target *target, struct sk_buff *skb,
2533 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002534{
2535 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002536 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537 int rc;
2538
Joe Perchesb4834832013-04-05 12:27:37 -07002539 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002540
2541 if (!dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002542 nfc_err(&dev->interface->dev,
2543 "Can't exchange data if there is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002544 rc = -EINVAL;
2545 goto error;
2546 }
2547
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002548 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549 if (!arg) {
2550 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002551 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002552 }
2553
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002554 arg->cb = cb;
2555 arg->cb_context = cb_context;
2556
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002557 switch (dev->device_type) {
2558 case PN533_DEVICE_PASORI:
2559 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2560 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2561 skb,
2562 pn533_data_exchange_complete,
2563 arg);
2564
2565 break;
2566 }
2567 default:
Olivier Guiter963a82e2013-06-13 13:43:28 +00002568 /* jumbo frame ? */
2569 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2570 rc = pn533_fill_fragment_skbs(dev, skb);
2571 if (rc <= 0)
2572 goto error;
2573
2574 skb = skb_dequeue(&dev->fragment_skb);
2575 if (!skb) {
2576 rc = -EIO;
2577 goto error;
2578 }
2579 } else {
2580 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2581 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002582
2583 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2584 skb, pn533_data_exchange_complete,
2585 arg);
2586
2587 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002588 }
2589
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002590 if (rc < 0) /* rc from send_async */
2591 goto error;
2592
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002593 return 0;
2594
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002596 kfree(arg);
2597 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002598 return rc;
2599}
2600
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002601static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002602 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002603{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002604 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002605
Joe Perchesb4834832013-04-05 12:27:37 -07002606 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002607
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002608 if (IS_ERR(resp))
2609 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002610
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002611 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002612
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002613 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002614
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002615 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002616 nfc_tm_deactivated(dev->nfc_dev);
2617
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002618 dev->tgt_mode = 0;
2619
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002620 return 0;
2621 }
2622
2623 queue_work(dev->wq, &dev->tg_work);
2624
2625 return 0;
2626}
2627
2628static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2629{
2630 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002631 int rc;
2632
Joe Perchesb4834832013-04-05 12:27:37 -07002633 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002634
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002635 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Joe Perches073a6252013-04-05 12:27:38 -07002636 nfc_err(&dev->interface->dev,
2637 "Data length greater than the max allowed: %d\n",
2638 PN533_CMD_DATAEXCH_DATA_MAXLEN);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002639 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002640 }
2641
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002642 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2643 pn533_tm_send_complete, NULL);
2644 if (rc < 0)
2645 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002646
2647 return rc;
2648}
2649
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002650static void pn533_wq_mi_recv(struct work_struct *work)
2651{
Olivier Guiter963a82e2013-06-13 13:43:28 +00002652 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002653 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002654 int rc;
2655
Joe Perchesb4834832013-04-05 12:27:37 -07002656 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002657
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002658 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002659 if (!skb)
2660 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002661
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002662 switch (dev->device_type) {
2663 case PN533_DEVICE_PASORI:
2664 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2665 rc = pn533_send_cmd_direct_async(dev,
2666 PN533_CMD_IN_COMM_THRU,
2667 skb,
2668 pn533_data_exchange_complete,
2669 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002670
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002671 break;
2672 }
2673 default:
2674 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002675
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002676 rc = pn533_send_cmd_direct_async(dev,
2677 PN533_CMD_IN_DATA_EXCHANGE,
2678 skb,
2679 pn533_data_exchange_complete,
2680 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002681
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002682 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002683 }
2684
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002685 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002686 return;
2687
Joe Perches073a6252013-04-05 12:27:38 -07002688 nfc_err(&dev->interface->dev,
2689 "Error %d when trying to perform data_exchange\n", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002690
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002691 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002692 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002693
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002694error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002695 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002696 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002697}
2698
Olivier Guiter963a82e2013-06-13 13:43:28 +00002699static void pn533_wq_mi_send(struct work_struct *work)
2700{
2701 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2702 struct sk_buff *skb;
2703 int rc;
2704
Joe Perchesb4834832013-04-05 12:27:37 -07002705 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002706
2707 /* Grab the first skb in the queue */
2708 skb = skb_dequeue(&dev->fragment_skb);
2709
2710 if (skb == NULL) { /* No more data */
2711 /* Reset the queue for future use */
2712 skb_queue_head_init(&dev->fragment_skb);
2713 goto error;
2714 }
2715
2716 switch (dev->device_type) {
2717 case PN533_DEVICE_PASORI:
2718 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2719 rc = -EIO;
2720 break;
2721 }
2722
2723 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2724 skb,
2725 pn533_data_exchange_complete,
2726 dev->cmd_complete_dep_arg);
2727
2728 break;
2729
2730 default:
2731 /* Still some fragments? */
2732 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2733 skb,
2734 pn533_data_exchange_complete,
2735 dev->cmd_complete_dep_arg);
2736
2737 break;
2738 }
2739
2740 if (rc == 0) /* success */
2741 return;
2742
Joe Perches073a6252013-04-05 12:27:38 -07002743 nfc_err(&dev->interface->dev,
2744 "Error %d when trying to perform data_exchange\n", rc);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002745
2746 dev_kfree_skb(skb);
2747 kfree(dev->cmd_complete_dep_arg);
2748
2749error:
2750 pn533_send_ack(dev, GFP_KERNEL);
2751 queue_work(dev->wq, &dev->cmd_work);
2752}
2753
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002754static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2755 u8 cfgdata_len)
2756{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002757 struct sk_buff *skb;
2758 struct sk_buff *resp;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002759 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002760
Joe Perchesb4834832013-04-05 12:27:37 -07002761 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002762
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002763 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002764
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002765 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002766 if (!skb)
2767 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002768
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002769 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2770 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002771
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002772 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2773 if (IS_ERR(resp))
2774 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002775
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002776 dev_kfree_skb(resp);
2777 return 0;
2778}
2779
2780static int pn533_get_firmware_version(struct pn533 *dev,
2781 struct pn533_fw_version *fv)
2782{
2783 struct sk_buff *skb;
2784 struct sk_buff *resp;
2785
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002786 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002787 if (!skb)
2788 return -ENOMEM;
2789
2790 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2791 if (IS_ERR(resp))
2792 return PTR_ERR(resp);
2793
2794 fv->ic = resp->data[0];
2795 fv->ver = resp->data[1];
2796 fv->rev = resp->data[2];
2797 fv->support = resp->data[3];
2798
2799 dev_kfree_skb(resp);
2800 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002801}
2802
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002803static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002804{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002805 struct sk_buff *skb;
2806 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002807
Joe Perchesb4834832013-04-05 12:27:37 -07002808 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002809
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002810 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002811 if (!skb)
2812 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002813
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002814 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002815
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002816 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2817 if (IS_ERR(resp))
2818 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002819
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002820 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002821
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002822 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002823}
2824
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002825struct pn533_acr122_poweron_rdr_arg {
2826 int rc;
2827 struct completion done;
2828};
2829
2830static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2831{
2832 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2833
Joe Perchesb4834832013-04-05 12:27:37 -07002834 dev_dbg(&urb->dev->dev, "%s\n", __func__);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002835
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002836 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002837 urb->transfer_buffer, urb->transfer_buffer_length,
2838 false);
2839
2840 arg->rc = urb->status;
2841 complete(&arg->done);
2842}
2843
2844static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2845{
2846 /* Power on th reader (CCID cmd) */
2847 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2848 0, 0, 0, 0, 0, 0, 3, 0, 0};
2849 u8 buf[255];
2850 int rc;
2851 void *cntx;
2852 struct pn533_acr122_poweron_rdr_arg arg;
2853
Joe Perchesb4834832013-04-05 12:27:37 -07002854 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002855
2856 init_completion(&arg.done);
2857 cntx = dev->in_urb->context; /* backup context */
2858
2859 dev->in_urb->transfer_buffer = buf;
2860 dev->in_urb->transfer_buffer_length = 255;
2861 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2862 dev->in_urb->context = &arg;
2863
2864 dev->out_urb->transfer_buffer = cmd;
2865 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2866
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002867 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002868 cmd, sizeof(cmd), false);
2869
2870 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2871 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002872 nfc_err(&dev->interface->dev,
2873 "Reader power on cmd error %d\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002874 return rc;
2875 }
2876
2877 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2878 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002879 nfc_err(&dev->interface->dev,
2880 "Can't submit reader poweron cmd response %d\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002881 return rc;
2882 }
2883
2884 wait_for_completion(&arg.done);
2885 dev->in_urb->context = cntx; /* restore context */
2886
2887 return arg.rc;
2888}
2889
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002890static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2891{
2892 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2893 u8 rf_field = !!rf;
2894 int rc;
2895
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02002896 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2897
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002898 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2899 (u8 *)&rf_field, 1);
2900 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002901 nfc_err(&dev->interface->dev, "Error on setting RF field\n");
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002902 return rc;
2903 }
2904
2905 return rc;
2906}
2907
Sachin Kamate44666b2013-09-20 14:30:41 +05302908static int pn533_dev_up(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002909{
2910 return pn533_rf_field(nfc_dev, 1);
2911}
2912
Sachin Kamate44666b2013-09-20 14:30:41 +05302913static int pn533_dev_down(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002914{
2915 return pn533_rf_field(nfc_dev, 0);
2916}
2917
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002918static struct nfc_ops pn533_nfc_ops = {
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002919 .dev_up = pn533_dev_up,
2920 .dev_down = pn533_dev_down,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002921 .dep_link_up = pn533_dep_link_up,
2922 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002923 .start_poll = pn533_start_poll,
2924 .stop_poll = pn533_stop_poll,
2925 .activate_target = pn533_activate_target,
2926 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002927 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002928 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002929};
2930
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002931static int pn533_setup(struct pn533 *dev)
2932{
2933 struct pn533_config_max_retries max_retries;
2934 struct pn533_config_timing timing;
2935 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2936 int rc;
2937
2938 switch (dev->device_type) {
2939 case PN533_DEVICE_STD:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002940 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002941 case PN533_DEVICE_ACR122U:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002942 max_retries.mx_rty_atr = 0x2;
2943 max_retries.mx_rty_psl = 0x1;
2944 max_retries.mx_rty_passive_act =
2945 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2946
2947 timing.rfu = PN533_CONFIG_TIMING_102;
2948 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2949 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2950
2951 break;
2952
2953 default:
Joe Perches073a6252013-04-05 12:27:38 -07002954 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
2955 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002956 return -EINVAL;
2957 }
2958
2959 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2960 (u8 *)&max_retries, sizeof(max_retries));
2961 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002962 nfc_err(&dev->interface->dev,
2963 "Error on setting MAX_RETRIES config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002964 return rc;
2965 }
2966
2967
2968 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2969 (u8 *)&timing, sizeof(timing));
2970 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002971 nfc_err(&dev->interface->dev, "Error on setting RF timings\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002972 return rc;
2973 }
2974
2975 switch (dev->device_type) {
2976 case PN533_DEVICE_STD:
2977 break;
2978
2979 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002980 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002981
2982 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2983 pasori_cfg, 3);
2984 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002985 nfc_err(&dev->interface->dev,
2986 "Error while settings PASORI config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002987 return rc;
2988 }
2989
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002990 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002991
2992 break;
2993 }
2994
2995 return 0;
2996}
2997
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002998static int pn533_probe(struct usb_interface *interface,
2999 const struct usb_device_id *id)
3000{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01003001 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003002 struct pn533 *dev;
3003 struct usb_host_interface *iface_desc;
3004 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003005 int in_endpoint = 0;
3006 int out_endpoint = 0;
3007 int rc = -ENOMEM;
3008 int i;
3009 u32 protocols;
3010
3011 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3012 if (!dev)
3013 return -ENOMEM;
3014
3015 dev->udev = usb_get_dev(interface_to_usbdev(interface));
3016 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02003017 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003018
3019 iface_desc = interface->cur_altsetting;
3020 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
3021 endpoint = &iface_desc->endpoint[i].desc;
3022
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01003023 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003024 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003025
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01003026 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003027 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003028 }
3029
3030 if (!in_endpoint || !out_endpoint) {
Joe Perches073a6252013-04-05 12:27:38 -07003031 nfc_err(&interface->dev,
3032 "Could not find bulk-in or bulk-out endpoint\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003033 rc = -ENODEV;
3034 goto error;
3035 }
3036
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003037 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003038 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
3039
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01003040 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003041 goto error;
3042
3043 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01003044 usb_rcvbulkpipe(dev->udev, in_endpoint),
3045 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003046 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01003047 usb_sndbulkpipe(dev->udev, out_endpoint),
3048 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003049
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003050 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
3051 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Olivier Guiter963a82e2013-06-13 13:43:28 +00003052 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
3053 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02003054 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02003055 INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02003056 INIT_WORK(&dev->rf_work, pn533_wq_rf);
Tejun Heo58637c92012-08-22 16:28:46 -07003057 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02003058 if (dev->wq == NULL)
3059 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003060
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02003061 init_timer(&dev->listen_timer);
3062 dev->listen_timer.data = (unsigned long) dev;
3063 dev->listen_timer.function = pn533_listen_mode_timer;
3064
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003065 skb_queue_head_init(&dev->resp_q);
Olivier Guiter963a82e2013-06-13 13:43:28 +00003066 skb_queue_head_init(&dev->fragment_skb);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003067
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003068 INIT_LIST_HEAD(&dev->cmd_queue);
3069
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003070 usb_set_intfdata(interface, dev);
3071
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003072 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003073
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +02003074 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003075 dev->device_type = id->driver_info;
3076 switch (dev->device_type) {
3077 case PN533_DEVICE_STD:
3078 protocols = PN533_ALL_PROTOCOLS;
3079 break;
3080
3081 case PN533_DEVICE_PASORI:
3082 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3083 break;
3084
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003085 case PN533_DEVICE_ACR122U:
3086 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3087 dev->ops = &pn533_acr122_frame_ops;
3088 dev->protocol_type = PN533_PROTO_REQ_RESP,
3089
3090 rc = pn533_acr122_poweron_rdr(dev);
3091 if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07003092 nfc_err(&dev->interface->dev,
3093 "Couldn't poweron the reader (error %d)\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003094 goto destroy_wq;
3095 }
3096 break;
3097
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003098 default:
Joe Perches073a6252013-04-05 12:27:38 -07003099 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
3100 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003101 rc = -EINVAL;
3102 goto destroy_wq;
3103 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003104
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003105 memset(&fw_ver, 0, sizeof(fw_ver));
3106 rc = pn533_get_firmware_version(dev, &fw_ver);
3107 if (rc < 0)
3108 goto destroy_wq;
3109
Joe Perches073a6252013-04-05 12:27:38 -07003110 nfc_info(&dev->interface->dev,
3111 "NXP PN5%02X firmware ver %d.%d now attached\n",
3112 fw_ver.ic, fw_ver.ver, fw_ver.rev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003113
3114
Samuel Ortize8753042011-08-19 15:47:11 +02003115 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003116 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02003117 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003118 dev->ops->tx_tail_len);
Wei Yongjun4674d0f2013-05-07 20:07:52 +08003119 if (!dev->nfc_dev) {
3120 rc = -ENOMEM;
Samuel Ortiz4849f852012-04-10 19:43:17 +02003121 goto destroy_wq;
Wei Yongjun4674d0f2013-05-07 20:07:52 +08003122 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003123
3124 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3125 nfc_set_drvdata(dev->nfc_dev, dev);
3126
3127 rc = nfc_register_device(dev->nfc_dev);
3128 if (rc)
3129 goto free_nfc_dev;
3130
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003131 rc = pn533_setup(dev);
3132 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003133 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02003134
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003135 return 0;
3136
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003137unregister_nfc_dev:
3138 nfc_unregister_device(dev->nfc_dev);
3139
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003140free_nfc_dev:
3141 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003142
Samuel Ortiz4849f852012-04-10 19:43:17 +02003143destroy_wq:
3144 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003145error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003146 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003147 usb_free_urb(dev->out_urb);
Marina Makienko7c5a54f2013-02-26 11:41:18 +04003148 usb_put_dev(dev->udev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003149 kfree(dev);
3150 return rc;
3151}
3152
3153static void pn533_disconnect(struct usb_interface *interface)
3154{
3155 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003156 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003157
3158 dev = usb_get_intfdata(interface);
3159 usb_set_intfdata(interface, NULL);
3160
3161 nfc_unregister_device(dev->nfc_dev);
3162 nfc_free_device(dev->nfc_dev);
3163
3164 usb_kill_urb(dev->in_urb);
3165 usb_kill_urb(dev->out_urb);
3166
Samuel Ortiz46f793b2013-07-03 14:50:36 +02003167 flush_delayed_work(&dev->poll_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +02003168 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003169
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003170 skb_queue_purge(&dev->resp_q);
3171
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02003172 del_timer(&dev->listen_timer);
3173
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003174 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3175 list_del(&cmd->queue);
3176 kfree(cmd);
3177 }
3178
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003179 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003180 usb_free_urb(dev->out_urb);
3181 kfree(dev);
3182
Joe Perches073a6252013-04-05 12:27:38 -07003183 nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003184}
3185
3186static struct usb_driver pn533_driver = {
3187 .name = "pn533",
3188 .probe = pn533_probe,
3189 .disconnect = pn533_disconnect,
3190 .id_table = pn533_table,
3191};
3192
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08003193module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003194
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003195MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3196MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3197MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003198MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3199MODULE_VERSION(VERSION);
3200MODULE_LICENSE("GPL");