blob: 2daf04c073383c53ae4374d8a9613546fe13d09b [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
Olivier Guiter93ad4202013-09-23 12:24:39 +0200153#define PN533_CMD_TG_SET_META_DATA 0x94
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100154#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200155
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300156#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
157
158/* PN533 Return codes */
159#define PN533_CMD_RET_MASK 0x3F
160#define PN533_CMD_MI_MASK 0x40
161#define PN533_CMD_RET_SUCCESS 0x00
162
163struct pn533;
164
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100165typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
166 struct sk_buff *resp);
167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300168/* structs for pn533 commands */
169
170/* PN533_CMD_GET_FIRMWARE_VERSION */
171struct pn533_fw_version {
172 u8 ic;
173 u8 ver;
174 u8 rev;
175 u8 support;
176};
177
178/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200179#define PN533_CFGITEM_RF_FIELD 0x01
180#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300181#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200182#define PN533_CFGITEM_PASORI 0x82
183
Samuel Ortiz3a8eab32013-07-01 17:26:58 +0200184#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
185#define PN533_CFGITEM_RF_FIELD_ON 0x1
186#define PN533_CFGITEM_RF_FIELD_OFF 0x0
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300187
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200188#define PN533_CONFIG_TIMING_102 0xb
189#define PN533_CONFIG_TIMING_204 0xc
190#define PN533_CONFIG_TIMING_409 0xd
191#define PN533_CONFIG_TIMING_819 0xe
192
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300193#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
194#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
195
196struct pn533_config_max_retries {
197 u8 mx_rty_atr;
198 u8 mx_rty_psl;
199 u8 mx_rty_passive_act;
200} __packed;
201
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200202struct pn533_config_timing {
203 u8 rfu;
204 u8 atr_res_timeout;
205 u8 dep_timeout;
206} __packed;
207
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300208/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
209
210/* felica commands opcode */
211#define PN533_FELICA_OPC_SENSF_REQ 0
212#define PN533_FELICA_OPC_SENSF_RES 1
213/* felica SENSF_REQ parameters */
214#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
215#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
216#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
217#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
218
219/* type B initiator_data values */
220#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
221#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
222#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
223
224union pn533_cmd_poll_initdata {
225 struct {
226 u8 afi;
227 u8 polling_method;
228 } __packed type_b;
229 struct {
230 u8 opcode;
231 __be16 sc;
232 u8 rc;
233 u8 tsn;
234 } __packed felica;
235};
236
237/* Poll modulations */
238enum {
239 PN533_POLL_MOD_106KBPS_A,
240 PN533_POLL_MOD_212KBPS_FELICA,
241 PN533_POLL_MOD_424KBPS_FELICA,
242 PN533_POLL_MOD_106KBPS_JEWEL,
243 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200244 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300245
246 __PN533_POLL_MOD_AFTER_LAST,
247};
248#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
249
250struct pn533_poll_modulations {
251 struct {
252 u8 maxtg;
253 u8 brty;
254 union pn533_cmd_poll_initdata initiator_data;
255 } __packed data;
256 u8 len;
257};
258
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100259static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300260 [PN533_POLL_MOD_106KBPS_A] = {
261 .data = {
262 .maxtg = 1,
263 .brty = 0,
264 },
265 .len = 2,
266 },
267 [PN533_POLL_MOD_212KBPS_FELICA] = {
268 .data = {
269 .maxtg = 1,
270 .brty = 1,
271 .initiator_data.felica = {
272 .opcode = PN533_FELICA_OPC_SENSF_REQ,
273 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200274 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200275 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300276 },
277 },
278 .len = 7,
279 },
280 [PN533_POLL_MOD_424KBPS_FELICA] = {
281 .data = {
282 .maxtg = 1,
283 .brty = 2,
284 .initiator_data.felica = {
285 .opcode = PN533_FELICA_OPC_SENSF_REQ,
286 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200287 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200288 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300289 },
290 },
291 .len = 7,
292 },
293 [PN533_POLL_MOD_106KBPS_JEWEL] = {
294 .data = {
295 .maxtg = 1,
296 .brty = 4,
297 },
298 .len = 2,
299 },
300 [PN533_POLL_MOD_847KBPS_B] = {
301 .data = {
302 .maxtg = 1,
303 .brty = 8,
304 .initiator_data.type_b = {
305 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
306 .polling_method =
307 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
308 },
309 },
310 .len = 3,
311 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200312 [PN533_LISTEN_MOD] = {
313 .len = 0,
314 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300315};
316
317/* PN533_CMD_IN_ATR */
318
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319struct pn533_cmd_activate_response {
320 u8 status;
321 u8 nfcid3t[10];
322 u8 didt;
323 u8 bst;
324 u8 brt;
325 u8 to;
326 u8 ppt;
327 /* optional */
328 u8 gt[];
329} __packed;
330
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100331struct pn533_cmd_jump_dep_response {
332 u8 status;
333 u8 tg;
334 u8 nfcid3t[10];
335 u8 didt;
336 u8 bst;
337 u8 brt;
338 u8 to;
339 u8 ppt;
340 /* optional */
341 u8 gt[];
342} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300343
Samuel Ortizad3823c2012-05-30 23:54:55 +0200344
345/* PN533_TG_INIT_AS_TARGET */
346#define PN533_INIT_TARGET_PASSIVE 0x1
347#define PN533_INIT_TARGET_DEP 0x2
348
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200349#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
350#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
351#define PN533_INIT_TARGET_RESP_DEP 0x4
352
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200353enum pn533_protocol_type {
354 PN533_PROTO_REQ_ACK_RESP = 0,
355 PN533_PROTO_REQ_RESP
356};
357
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300358struct pn533 {
359 struct usb_device *udev;
360 struct usb_interface *interface;
361 struct nfc_dev *nfc_dev;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200362 u32 device_type;
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200363 enum pn533_protocol_type protocol_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300364
365 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300366 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300367
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200368 struct sk_buff_head resp_q;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000369 struct sk_buff_head fragment_skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200370
Samuel Ortiz4849f852012-04-10 19:43:17 +0200371 struct workqueue_struct *wq;
372 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200373 struct work_struct cmd_complete_work;
Samuel Ortiz46f793b2013-07-03 14:50:36 +0200374 struct delayed_work poll_work;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000375 struct work_struct mi_rx_work;
376 struct work_struct mi_tx_work;
Olivier Guiter3c13b242013-09-23 12:24:38 +0200377 struct work_struct mi_tm_rx_work;
Olivier Guiter93ad4202013-09-23 12:24:39 +0200378 struct work_struct mi_tm_tx_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200379 struct work_struct tg_work;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +0200380 struct work_struct rf_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200381
382 struct list_head cmd_queue;
383 struct pn533_cmd *cmd;
384 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200385 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300386
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100387 void *cmd_complete_mi_arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000388 void *cmd_complete_dep_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300389
390 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
391 u8 poll_mod_count;
392 u8 poll_mod_curr;
Samuel Ortiz673088f2013-08-21 15:06:55 +0200393 u8 poll_dep;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300394 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200395 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200396 struct timer_list listen_timer;
397 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200398
399 u8 *gb;
400 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300401
402 u8 tgt_available_prots;
403 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200404 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200405
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100406 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200407};
408
409struct pn533_cmd {
410 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200411 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200412 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100413 struct sk_buff *req;
414 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100415 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200416 pn533_send_async_complete_t complete_cb;
417 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418};
419
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200420struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300421 u8 preamble;
422 __be16 start_frame;
423 u8 datalen;
424 u8 datalen_checksum;
425 u8 data[];
426} __packed;
427
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200428struct pn533_ext_frame { /* Extended Information frame */
429 u8 preamble;
430 __be16 start_frame;
431 __be16 eif_flag; /* fixed to 0xFFFF */
432 __be16 datalen;
433 u8 datalen_checksum;
434 u8 data[];
435} __packed;
436
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100437struct pn533_frame_ops {
438 void (*tx_frame_init)(void *frame, u8 cmd_code);
439 void (*tx_frame_finish)(void *frame);
440 void (*tx_update_payload_len)(void *frame, int len);
441 int tx_header_len;
442 int tx_tail_len;
443
Olivier Guiter56a63c822013-06-13 13:43:29 +0000444 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100445 int (*rx_frame_size)(void *frame);
446 int rx_header_len;
447 int rx_tail_len;
448
449 int max_payload_len;
450 u8 (*get_cmd_code)(void *frame);
451};
452
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200453struct pn533_acr122_ccid_hdr {
454 u8 type;
455 u32 datalen;
456 u8 slot;
457 u8 seq;
458 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
459 byte for reposnse msg */
460 u8 data[]; /* payload */
461} __packed;
462
463struct pn533_acr122_apdu_hdr {
464 u8 class;
465 u8 ins;
466 u8 p1;
467 u8 p2;
468} __packed;
469
470struct pn533_acr122_tx_frame {
471 struct pn533_acr122_ccid_hdr ccid;
472 struct pn533_acr122_apdu_hdr apdu;
473 u8 datalen;
474 u8 data[]; /* pn533 frame: TFI ... */
475} __packed;
476
477struct pn533_acr122_rx_frame {
478 struct pn533_acr122_ccid_hdr ccid;
479 u8 data[]; /* pn533 frame : TFI ... */
480} __packed;
481
482static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
483{
484 struct pn533_acr122_tx_frame *frame = _frame;
485
486 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
487 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
488 sizeof(datalen) */
489 frame->ccid.slot = 0;
490 frame->ccid.seq = 0;
491 frame->ccid.params[0] = 0;
492 frame->ccid.params[1] = 0;
493 frame->ccid.params[2] = 0;
494
495 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
496 frame->data[1] = cmd_code;
497 frame->datalen = 2; /* data[0] + data[1] */
498
499 frame->apdu.class = 0xFF;
500 frame->apdu.ins = 0;
501 frame->apdu.p1 = 0;
502 frame->apdu.p2 = 0;
503}
504
505static void pn533_acr122_tx_frame_finish(void *_frame)
506{
507 struct pn533_acr122_tx_frame *frame = _frame;
508
509 frame->ccid.datalen += frame->datalen;
510}
511
512static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
513{
514 struct pn533_acr122_tx_frame *frame = _frame;
515
516 frame->datalen += len;
517}
518
Olivier Guiter56a63c822013-06-13 13:43:29 +0000519static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200520{
521 struct pn533_acr122_rx_frame *frame = _frame;
522
523 if (frame->ccid.type != 0x83)
524 return false;
525
526 if (frame->data[frame->ccid.datalen - 2] == 0x63)
527 return false;
528
529 return true;
530}
531
532static int pn533_acr122_rx_frame_size(void *frame)
533{
534 struct pn533_acr122_rx_frame *f = frame;
535
536 /* f->ccid.datalen already includes tail length */
537 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
538}
539
540static u8 pn533_acr122_get_cmd_code(void *frame)
541{
542 struct pn533_acr122_rx_frame *f = frame;
543
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200544 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200545}
546
547static struct pn533_frame_ops pn533_acr122_frame_ops = {
548 .tx_frame_init = pn533_acr122_tx_frame_init,
549 .tx_frame_finish = pn533_acr122_tx_frame_finish,
550 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
551 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
552 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
553
554 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
555 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
556 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
557 .rx_frame_size = pn533_acr122_rx_frame_size,
558
559 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
560 .get_cmd_code = pn533_acr122_get_cmd_code,
561};
562
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200563/* The rule: value(high byte) + value(low byte) + checksum = 0 */
564static inline u8 pn533_ext_checksum(u16 value)
565{
566 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
567}
568
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300569/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200570static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300571{
572 return ~value + 1;
573}
574
575/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200576static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300577{
578 u8 sum = 0;
579 int i;
580
581 for (i = 0; i < datalen; i++)
582 sum += data[i];
583
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200584 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585}
586
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200587static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300588{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200589 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100590
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300591 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200592 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
593 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200594 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300595 frame->datalen = 2;
596}
597
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200598static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200600 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100601
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200602 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300603
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200604 PN533_STD_FRAME_CHECKSUM(frame) =
605 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200607 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300608}
609
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200610static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100611{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200612 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100613
614 frame->datalen += len;
615}
616
Olivier Guiter56a63c822013-06-13 13:43:29 +0000617static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300618{
619 u8 checksum;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200620 struct pn533_std_frame *stdf = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300621
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200622 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300623 return false;
624
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200625 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
626 /* Standard frame code */
Olivier Guiter56a63c822013-06-13 13:43:29 +0000627 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300628
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200629 checksum = pn533_std_checksum(stdf->datalen);
630 if (checksum != stdf->datalen_checksum)
631 return false;
632
633 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
634 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
635 return false;
636 } else {
637 /* Extended */
638 struct pn533_ext_frame *eif = _frame;
639
Olivier Guiter56a63c822013-06-13 13:43:29 +0000640 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
641
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200642 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
643 if (checksum != eif->datalen_checksum)
644 return false;
645
646 /* check data checksum */
647 checksum = pn533_std_data_checksum(eif->data,
648 be16_to_cpu(eif->datalen));
649 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
650 return false;
651 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300652
653 return true;
654}
655
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200656static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200658 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300659 return false;
660
661 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
662 return false;
663
664 return true;
665}
666
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200667static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300668{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200669 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100670
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200671 /* check for Extended Information frame */
672 if (PN533_STD_IS_EXTENDED(f)) {
673 struct pn533_ext_frame *eif = frame;
674
675 return sizeof(struct pn533_ext_frame)
676 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
677 }
678
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200679 return sizeof(struct pn533_std_frame) + f->datalen +
680 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100681}
682
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200683static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100684{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200685 struct pn533_std_frame *f = frame;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200686 struct pn533_ext_frame *eif = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100687
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200688 if (PN533_STD_IS_EXTENDED(f))
689 return PN533_FRAME_CMD(eif);
690 else
691 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100692}
693
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100694static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200695 .tx_frame_init = pn533_std_tx_frame_init,
696 .tx_frame_finish = pn533_std_tx_frame_finish,
697 .tx_update_payload_len = pn533_std_tx_update_payload_len,
698 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
699 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100700
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200701 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
702 .rx_frame_size = pn533_std_rx_frame_size,
703 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
704 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100705
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200706 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
707 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100708};
709
710static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
711{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200712 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200713 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300714}
715
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300716static void pn533_recv_response(struct urb *urb)
717{
718 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200719 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100720 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300721
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200722 cmd->status = urb->status;
723
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300724 switch (urb->status) {
725 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100726 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300727 case -ECONNRESET:
728 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -0700729 dev_dbg(&dev->interface->dev,
730 "The urb has been canceled (status %d)\n",
731 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200732 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100733 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300734 default:
Joe Perches073a6252013-04-05 12:27:38 -0700735 nfc_err(&dev->interface->dev,
736 "Urb failure (status %d)\n", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200737 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300738 }
739
740 in_frame = dev->in_urb->transfer_buffer;
741
Joe Perchesb4834832013-04-05 12:27:37 -0700742 dev_dbg(&dev->interface->dev, "Received a frame\n");
Samuel Ortize279f842013-03-20 11:27:57 +0100743 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
744 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100745
Olivier Guiter56a63c822013-06-13 13:43:29 +0000746 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
Joe Perches073a6252013-04-05 12:27:38 -0700747 nfc_err(&dev->interface->dev, "Received an invalid frame\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200748 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200749 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300750 }
751
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100752 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Joe Perches073a6252013-04-05 12:27:38 -0700753 nfc_err(&dev->interface->dev,
754 "It it not the response to the last command\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200755 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200756 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300757 }
758
Samuel Ortiz4849f852012-04-10 19:43:17 +0200759sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200760 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300761}
762
763static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
764{
765 dev->in_urb->complete = pn533_recv_response;
766
767 return usb_submit_urb(dev->in_urb, flags);
768}
769
770static void pn533_recv_ack(struct urb *urb)
771{
772 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200773 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200774 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300775 int rc;
776
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200777 cmd->status = urb->status;
778
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300779 switch (urb->status) {
780 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100781 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300782 case -ECONNRESET:
783 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -0700784 dev_dbg(&dev->interface->dev,
785 "The urb has been stopped (status %d)\n",
786 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200787 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100788 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300789 default:
Joe Perches073a6252013-04-05 12:27:38 -0700790 nfc_err(&dev->interface->dev,
791 "Urb failure (status %d)\n", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200792 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300793 }
794
795 in_frame = dev->in_urb->transfer_buffer;
796
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200797 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Joe Perches073a6252013-04-05 12:27:38 -0700798 nfc_err(&dev->interface->dev, "Received an invalid ack\n");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200799 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200800 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300801 }
802
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300803 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
804 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -0700805 nfc_err(&dev->interface->dev,
806 "usb_submit_urb failed with result %d\n", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200807 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200808 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300809 }
810
811 return;
812
Samuel Ortiz4849f852012-04-10 19:43:17 +0200813sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200814 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300815}
816
817static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
818{
819 dev->in_urb->complete = pn533_recv_ack;
820
821 return usb_submit_urb(dev->in_urb, flags);
822}
823
824static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
825{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200826 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100827 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300828 int rc;
829
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100830 dev->out_urb->transfer_buffer = ack;
831 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300832 rc = usb_submit_urb(dev->out_urb, flags);
833
834 return rc;
835}
836
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100837static int __pn533_send_frame_async(struct pn533 *dev,
838 struct sk_buff *out,
839 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200840 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300841{
842 int rc;
843
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100844 dev->out_urb->transfer_buffer = out->data;
845 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300846
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100847 dev->in_urb->transfer_buffer = in->data;
848 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300849
Samuel Ortize279f842013-03-20 11:27:57 +0100850 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
851 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100852
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100853 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300854 if (rc)
855 return rc;
856
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200857 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
858 /* request for response for sent packet directly */
859 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
860 if (rc)
861 goto error;
862 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
863 /* request for ACK if that's the case */
864 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
865 if (rc)
866 goto error;
867 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300868
869 return 0;
870
871error:
872 usb_unlink_urb(dev->out_urb);
873 return rc;
874}
875
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100876static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
877 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100878{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100879 /* payload is already there, just update datalen */
880 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100881 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100882
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100883
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100884 skb_push(skb, ops->tx_header_len);
885 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100886
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100887 ops->tx_frame_init(skb->data, cmd_code);
888 ops->tx_update_payload_len(skb->data, payload_len);
889 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100890}
891
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200892static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100893{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200894 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200895 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100896
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200897 struct sk_buff *req = cmd->req;
898 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100899
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100900 int rc;
901
902 dev_kfree_skb(req);
903
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100904 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200905 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
906 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100907 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200908 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100909 }
910
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100911 skb_put(resp, dev->ops->rx_frame_size(resp->data));
912 skb_pull(resp, dev->ops->rx_header_len);
913 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100914
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200915 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100916
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200917done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200918 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200919 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100920 return rc;
921}
922
923static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
924 struct sk_buff *req, struct sk_buff *resp,
925 int resp_len,
926 pn533_send_async_complete_t complete_cb,
927 void *complete_cb_context)
928{
929 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100930 int rc = 0;
931
Joe Perchesb4834832013-04-05 12:27:37 -0700932 dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100933
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200934 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
935 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100936 return -ENOMEM;
937
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200938 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200939 cmd->req = req;
940 cmd->resp = resp;
941 cmd->resp_len = resp_len;
942 cmd->complete_cb = complete_cb;
943 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100944
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100945 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100946
947 mutex_lock(&dev->cmd_lock);
948
949 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200950 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100951 if (rc)
952 goto error;
953
954 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200955 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100956 goto unlock;
957 }
958
Joe Perchesb4834832013-04-05 12:27:37 -0700959 dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n",
960 __func__, cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100961
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100962 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100963 list_add_tail(&cmd->queue, &dev->cmd_queue);
964
965 goto unlock;
966
967error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200968 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100969unlock:
970 mutex_unlock(&dev->cmd_lock);
971 return rc;
972}
973
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100974static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
975 struct sk_buff *req,
976 pn533_send_async_complete_t complete_cb,
977 void *complete_cb_context)
978{
979 struct sk_buff *resp;
980 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100981 int resp_len = dev->ops->rx_header_len +
982 dev->ops->max_payload_len +
983 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100984
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100985 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
986 if (!resp)
987 return -ENOMEM;
988
989 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
990 complete_cb_context);
991 if (rc)
992 dev_kfree_skb(resp);
993
994 return rc;
995}
996
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100997static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
998 struct sk_buff *req,
999 pn533_send_async_complete_t complete_cb,
1000 void *complete_cb_context)
1001{
1002 struct sk_buff *resp;
1003 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001004 int resp_len = dev->ops->rx_header_len +
1005 dev->ops->max_payload_len +
1006 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001007
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001008 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001009 if (!resp)
1010 return -ENOMEM;
1011
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001012 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1013 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001014 if (rc)
1015 dev_kfree_skb(resp);
1016
1017 return rc;
1018}
1019
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001020/*
1021 * pn533_send_cmd_direct_async
1022 *
1023 * The function sends a piority cmd directly to the chip omiting the cmd
1024 * queue. It's intended to be used by chaining mechanism of received responses
1025 * where the host has to request every single chunk of data before scheduling
1026 * next cmd from the queue.
1027 */
1028static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1029 struct sk_buff *req,
1030 pn533_send_async_complete_t complete_cb,
1031 void *complete_cb_context)
1032{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001033 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001034 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001035 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001036 int resp_len = dev->ops->rx_header_len +
1037 dev->ops->max_payload_len +
1038 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001039
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001040 resp = alloc_skb(resp_len, GFP_KERNEL);
1041 if (!resp)
1042 return -ENOMEM;
1043
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001044 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1045 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001046 dev_kfree_skb(resp);
1047 return -ENOMEM;
1048 }
1049
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +02001050 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001051 cmd->req = req;
1052 cmd->resp = resp;
1053 cmd->resp_len = resp_len;
1054 cmd->complete_cb = complete_cb;
1055 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001056
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001057 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001058
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001059 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001060 if (rc < 0) {
1061 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001062 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001063 } else {
1064 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001065 }
1066
1067 return rc;
1068}
1069
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +02001070static void pn533_wq_cmd_complete(struct work_struct *work)
1071{
1072 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1073 int rc;
1074
1075 rc = pn533_send_async_complete(dev);
1076 if (rc != -EINPROGRESS)
1077 queue_work(dev->wq, &dev->cmd_work);
1078}
1079
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001080static void pn533_wq_cmd(struct work_struct *work)
1081{
1082 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1083 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001084 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001085
1086 mutex_lock(&dev->cmd_lock);
1087
1088 if (list_empty(&dev->cmd_queue)) {
1089 dev->cmd_pending = 0;
1090 mutex_unlock(&dev->cmd_lock);
1091 return;
1092 }
1093
1094 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1095
Szymon Janc60ad07a2012-10-25 17:29:45 +02001096 list_del(&cmd->queue);
1097
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001098 mutex_unlock(&dev->cmd_lock);
1099
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001100 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001101 if (rc < 0) {
1102 dev_kfree_skb(cmd->req);
1103 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001104 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001105 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001106 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001107
1108 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001109}
1110
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001111struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001112 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001113 struct completion done;
1114};
1115
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001116static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1117 struct sk_buff *resp)
1118{
1119 struct pn533_sync_cmd_response *arg = _arg;
1120
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001121 arg->resp = resp;
1122 complete(&arg->done);
1123
1124 return 0;
1125}
1126
1127/* pn533_send_cmd_sync
1128 *
1129 * Please note the req parameter is freed inside the function to
1130 * limit a number of return value interpretations by the caller.
1131 *
1132 * 1. negative in case of error during TX path -> req should be freed
1133 *
1134 * 2. negative in case of error during RX path -> req should not be freed
1135 * as it's been already freed at the begining of RX path by
1136 * async_complete_cb.
1137 *
1138 * 3. valid pointer in case of succesfult RX path
1139 *
1140 * A caller has to check a return value with IS_ERR macro. If the test pass,
1141 * the returned pointer is valid.
1142 *
1143 * */
1144static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1145 struct sk_buff *req)
1146{
1147 int rc;
1148 struct pn533_sync_cmd_response arg;
1149
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001150 init_completion(&arg.done);
1151
1152 rc = pn533_send_cmd_async(dev, cmd_code, req,
1153 pn533_send_sync_complete, &arg);
1154 if (rc) {
1155 dev_kfree_skb(req);
1156 return ERR_PTR(rc);
1157 }
1158
1159 wait_for_completion(&arg.done);
1160
1161 return arg.resp;
1162}
1163
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001164static void pn533_send_complete(struct urb *urb)
1165{
1166 struct pn533 *dev = urb->context;
1167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001168 switch (urb->status) {
1169 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001170 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001171 case -ECONNRESET:
1172 case -ENOENT:
Joe Perchesb4834832013-04-05 12:27:37 -07001173 dev_dbg(&dev->interface->dev,
1174 "The urb has been stopped (status %d)\n",
1175 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001176 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001177 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001178 default:
Joe Perches073a6252013-04-05 12:27:38 -07001179 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
1180 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001181 }
1182}
1183
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001184static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1185{
1186 /* ACR122U does not support any command which aborts last
1187 * issued command i.e. as ACK for standard PN533. Additionally,
1188 * it behaves stange, sending broken or incorrect responses,
1189 * when we cancel urb before the chip will send response.
1190 */
1191 if (dev->device_type == PN533_DEVICE_ACR122U)
1192 return;
1193
1194 /* An ack will cancel the last issued command */
1195 pn533_send_ack(dev, flags);
1196
1197 /* cancel the urb request */
1198 usb_kill_urb(dev->in_urb);
1199}
1200
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001201static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001202{
1203 struct sk_buff *skb;
1204
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001205 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001206 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001207 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001208
1209 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001210 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001211
1212 return skb;
1213}
1214
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001215struct pn533_target_type_a {
1216 __be16 sens_res;
1217 u8 sel_res;
1218 u8 nfcid_len;
1219 u8 nfcid_data[];
1220} __packed;
1221
1222
1223#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1224#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1225#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1226
1227#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1228#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1229
1230#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1231#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1232
1233#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1234#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1235#define PN533_TYPE_A_SEL_PROT_DEP 2
1236#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1237
1238static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1239 int target_data_len)
1240{
1241 u8 ssd;
1242 u8 platconf;
1243
1244 if (target_data_len < sizeof(struct pn533_target_type_a))
1245 return false;
1246
1247 /* The lenght check of nfcid[] and ats[] are not being performed because
1248 the values are not being used */
1249
1250 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1251 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1252 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1253
1254 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001255 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1256 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1257 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001258 return false;
1259
1260 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1261 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1262 return false;
1263
1264 return true;
1265}
1266
1267static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1268 int tgt_data_len)
1269{
1270 struct pn533_target_type_a *tgt_type_a;
1271
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001272 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001273
1274 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1275 return -EPROTO;
1276
1277 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1278 case PN533_TYPE_A_SEL_PROT_MIFARE:
1279 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1280 break;
1281 case PN533_TYPE_A_SEL_PROT_ISO14443:
1282 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1283 break;
1284 case PN533_TYPE_A_SEL_PROT_DEP:
1285 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1286 break;
1287 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1288 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1289 NFC_PROTO_NFC_DEP_MASK;
1290 break;
1291 }
1292
1293 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1294 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001295 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1296 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001297
1298 return 0;
1299}
1300
1301struct pn533_target_felica {
1302 u8 pol_res;
1303 u8 opcode;
Samuel Ortiz322bce92013-05-27 15:29:11 +02001304 u8 nfcid2[NFC_NFCID2_MAXSIZE];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001305 u8 pad[8];
1306 /* optional */
1307 u8 syst_code[];
1308} __packed;
1309
1310#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1311#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1312
1313static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1314 int target_data_len)
1315{
1316 if (target_data_len < sizeof(struct pn533_target_felica))
1317 return false;
1318
1319 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1320 return false;
1321
1322 return true;
1323}
1324
1325static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1326 int tgt_data_len)
1327{
1328 struct pn533_target_felica *tgt_felica;
1329
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001330 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331
1332 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1333 return -EPROTO;
1334
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001335 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1336 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001337 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1338 else
1339 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1340
Samuel Ortiz79757542012-03-05 01:03:45 +01001341 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1342 nfc_tgt->sensf_res_len = 9;
1343
Samuel Ortiz322bce92013-05-27 15:29:11 +02001344 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1345 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1346
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001347 return 0;
1348}
1349
1350struct pn533_target_jewel {
1351 __be16 sens_res;
1352 u8 jewelid[4];
1353} __packed;
1354
1355static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1356 int target_data_len)
1357{
1358 u8 ssd;
1359 u8 platconf;
1360
1361 if (target_data_len < sizeof(struct pn533_target_jewel))
1362 return false;
1363
1364 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1365 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1366 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1367
1368 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001369 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1370 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1371 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001372 return false;
1373
1374 return true;
1375}
1376
1377static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1378 int tgt_data_len)
1379{
1380 struct pn533_target_jewel *tgt_jewel;
1381
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001382 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001383
1384 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1385 return -EPROTO;
1386
1387 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1388 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001389 nfc_tgt->nfcid1_len = 4;
1390 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001391
1392 return 0;
1393}
1394
1395struct pn533_type_b_prot_info {
1396 u8 bitrate;
1397 u8 fsci_type;
1398 u8 fwi_adc_fo;
1399} __packed;
1400
1401#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1402#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1403#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1404
1405struct pn533_type_b_sens_res {
1406 u8 opcode;
1407 u8 nfcid[4];
1408 u8 appdata[4];
1409 struct pn533_type_b_prot_info prot_info;
1410} __packed;
1411
1412#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1413
1414struct pn533_target_type_b {
1415 struct pn533_type_b_sens_res sensb_res;
1416 u8 attrib_res_len;
1417 u8 attrib_res[];
1418} __packed;
1419
1420static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1421 int target_data_len)
1422{
1423 if (target_data_len < sizeof(struct pn533_target_type_b))
1424 return false;
1425
1426 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1427 return false;
1428
1429 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1430 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1431 return false;
1432
1433 return true;
1434}
1435
1436static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1437 int tgt_data_len)
1438{
1439 struct pn533_target_type_b *tgt_type_b;
1440
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001441 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001442
1443 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1444 return -EPROTO;
1445
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001446 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001447
1448 return 0;
1449}
1450
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001451static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1452 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001453{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001454 struct nfc_target nfc_tgt;
1455 int rc;
1456
Joe Perches17936b42013-04-05 12:27:39 -07001457 dev_dbg(&dev->interface->dev, "%s: modulation=%d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001458 __func__, dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001459
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001461 return -EPROTO;
1462
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001463 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1464
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001465 switch (dev->poll_mod_curr) {
1466 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001468 break;
1469 case PN533_POLL_MOD_212KBPS_FELICA:
1470 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001472 break;
1473 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001475 break;
1476 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001477 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001478 break;
1479 default:
Joe Perches073a6252013-04-05 12:27:38 -07001480 nfc_err(&dev->interface->dev,
1481 "Unknown current poll modulation\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001482 return -EPROTO;
1483 }
1484
1485 if (rc)
1486 return rc;
1487
1488 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Joe Perchesb4834832013-04-05 12:27:37 -07001489 dev_dbg(&dev->interface->dev,
1490 "The Tg found doesn't have the desired protocol\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001491 return -EAGAIN;
1492 }
1493
Joe Perchesb4834832013-04-05 12:27:37 -07001494 dev_dbg(&dev->interface->dev,
1495 "Target found - supported protocols: 0x%x\n",
1496 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001497
1498 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1499
1500 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1501
1502 return 0;
1503}
1504
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001505static inline void pn533_poll_next_mod(struct pn533 *dev)
1506{
1507 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1508}
1509
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001510static void pn533_poll_reset_mod_list(struct pn533 *dev)
1511{
1512 dev->poll_mod_count = 0;
1513}
1514
1515static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1516{
1517 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001518 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001519 dev->poll_mod_count++;
1520}
1521
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001522static void pn533_poll_create_mod_list(struct pn533 *dev,
1523 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001524{
1525 pn533_poll_reset_mod_list(dev);
1526
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001527 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1528 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1529 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001530 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1531
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001532 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1533 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001534 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1535 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1536 }
1537
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001538 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001539 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1540
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001541 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001542 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001543
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544 if (tm_protocols)
1545 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001546}
1547
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001548static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001549{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001550 u8 nbtg, tg, *tgdata;
1551 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001552
Samuel Ortiz673088f2013-08-21 15:06:55 +02001553 /* Toggle the DEP polling */
1554 dev->poll_dep = 1;
1555
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001556 nbtg = resp->data[0];
1557 tg = resp->data[1];
1558 tgdata = &resp->data[2];
1559 tgdata_len = resp->len - 2; /* nbtg + tg */
1560
1561 if (nbtg) {
1562 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001563
1564 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001565 if (rc == 0) {
1566 pn533_poll_reset_mod_list(dev);
1567 return 0;
1568 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001569 }
1570
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001572}
1573
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001574static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001575{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 struct sk_buff *skb;
1577 u8 *felica, *nfcid3, *gb;
1578
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001579 u8 *gbytes = dev->gb;
1580 size_t gbytes_len = dev->gb_len;
1581
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001582 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1583 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1584 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1585 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001586
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001587 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1588 0x0, 0x0, 0x0,
1589 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001590
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001591 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1592 felica (18), nfcid3 (10), gb_len (1) */
1593 gbytes_len +
1594 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001595
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001596 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001597 if (!skb)
1598 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001599
1600 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001601 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001602
1603 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001604 memcpy(skb_put(skb, 6), mifare_params, 6);
1605
1606 /* Felica params */
1607 felica = skb_put(skb, 18);
1608 memcpy(felica, felica_params, 18);
1609 get_random_bytes(felica + 2, 6);
1610
1611 /* NFCID3 */
1612 nfcid3 = skb_put(skb, 10);
1613 memset(nfcid3, 0, 10);
1614 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001615
1616 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001617 *skb_put(skb, 1) = gbytes_len;
1618
1619 gb = skb_put(skb, gbytes_len);
1620 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001621
Samuel Ortizad3823c2012-05-30 23:54:55 +02001622 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001623 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001624
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001625 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001626}
1627
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001628#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001629#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
Olivier Guiter3c13b242013-09-23 12:24:38 +02001630static void pn533_wq_tm_mi_recv(struct work_struct *work);
1631static struct sk_buff *pn533_build_response(struct pn533 *dev);
1632
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001633static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001634 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001635{
Olivier Guiter3c13b242013-09-23 12:24:38 +02001636 struct sk_buff *skb;
1637 u8 status, ret, mi;
1638 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001639
Joe Perchesb4834832013-04-05 12:27:37 -07001640 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001641
Olivier Guiter3c13b242013-09-23 12:24:38 +02001642 if (IS_ERR(resp)) {
1643 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001644 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001645 }
1646
Olivier Guiter3c13b242013-09-23 12:24:38 +02001647 status = resp->data[0];
1648
1649 ret = status & PN533_CMD_RET_MASK;
1650 mi = status & PN533_CMD_MI_MASK;
1651
1652 skb_pull(resp, sizeof(status));
1653
1654 if (ret != PN533_CMD_RET_SUCCESS) {
1655 rc = -EIO;
1656 goto error;
1657 }
1658
1659 skb_queue_tail(&dev->resp_q, resp);
1660
1661 if (mi) {
1662 queue_work(dev->wq, &dev->mi_tm_rx_work);
1663 return -EINPROGRESS;
1664 }
1665
1666 skb = pn533_build_response(dev);
1667 if (!skb) {
1668 rc = -EIO;
1669 goto error;
1670 }
1671
1672 return nfc_tm_data_received(dev->nfc_dev, skb);
1673
1674error:
1675 nfc_tm_deactivated(dev->nfc_dev);
1676 dev->tgt_mode = 0;
1677 skb_queue_purge(&dev->resp_q);
1678 dev_kfree_skb(resp);
1679
1680 return rc;
1681}
1682
1683static void pn533_wq_tm_mi_recv(struct work_struct *work)
1684{
1685 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1686 struct sk_buff *skb;
1687 int rc;
1688
1689 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1690
1691 skb = pn533_alloc_skb(dev, 0);
1692 if (!skb)
1693 return;
1694
1695 rc = pn533_send_cmd_direct_async(dev,
1696 PN533_CMD_TG_GET_DATA,
1697 skb,
1698 pn533_tm_get_data_complete,
1699 NULL);
1700
1701 if (rc < 0)
1702 dev_kfree_skb(skb);
1703
1704 return;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001705}
1706
Olivier Guiter93ad4202013-09-23 12:24:39 +02001707static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1708 struct sk_buff *resp);
1709static void pn533_wq_tm_mi_send(struct work_struct *work)
1710{
1711 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1712 struct sk_buff *skb;
1713 int rc;
1714
1715 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1716
1717 /* Grab the first skb in the queue */
1718 skb = skb_dequeue(&dev->fragment_skb);
1719 if (skb == NULL) { /* No more data */
1720 /* Reset the queue for future use */
1721 skb_queue_head_init(&dev->fragment_skb);
1722 goto error;
1723 }
1724
1725 /* last entry - remove MI bit */
1726 if (skb_queue_len(&dev->fragment_skb) == 0) {
1727 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1728 skb, pn533_tm_send_complete, NULL);
1729 } else
1730 rc = pn533_send_cmd_direct_async(dev,
1731 PN533_CMD_TG_SET_META_DATA,
1732 skb, pn533_tm_send_complete, NULL);
1733
1734 if (rc == 0) /* success */
1735 return;
1736
1737 dev_err(&dev->interface->dev,
1738 "Error %d when trying to perform set meta data_exchange", rc);
1739
1740 dev_kfree_skb(skb);
1741
1742error:
1743 pn533_send_ack(dev, GFP_KERNEL);
1744 queue_work(dev->wq, &dev->cmd_work);
1745}
1746
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001747static void pn533_wq_tg_get_data(struct work_struct *work)
1748{
1749 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001750 struct sk_buff *skb;
1751 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001752
Joe Perchesb4834832013-04-05 12:27:37 -07001753 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001754
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001755 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001756 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001757 return;
1758
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001759 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1760 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001761
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001762 if (rc < 0)
1763 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001764
1765 return;
1766}
1767
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001768#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001769static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001770{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001771 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001772 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001773 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001774
Joe Perchesb4834832013-04-05 12:27:37 -07001775 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001776
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001777 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001778 return -EINVAL;
1779
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001780 mode = resp->data[0];
1781 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001782
Joe Perchesb4834832013-04-05 12:27:37 -07001783 dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1784 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001785
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001786 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1787 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001788 comm_mode = NFC_COMM_ACTIVE;
1789
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001790 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001791 return -EOPNOTSUPP;
1792
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001793 gb = cmd + ATR_REQ_GB_OFFSET;
1794 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001795
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001796 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1797 comm_mode, gb, gb_len);
1798 if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07001799 nfc_err(&dev->interface->dev,
1800 "Error when signaling target activation\n");
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001801 return rc;
1802 }
1803
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001804 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001805 queue_work(dev->wq, &dev->tg_work);
1806
1807 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001808}
1809
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001810static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001811{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001812 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001813
Joe Perchesb4834832013-04-05 12:27:37 -07001814 dev_dbg(&dev->interface->dev, "Listen mode timeout\n");
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001815
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001816 dev->cancel_listen = 1;
1817
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001818 pn533_poll_next_mod(dev);
1819
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001820 queue_delayed_work(dev->wq, &dev->poll_work,
1821 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001822}
1823
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001824static int pn533_rf_complete(struct pn533 *dev, void *arg,
1825 struct sk_buff *resp)
1826{
1827 int rc = 0;
1828
Joe Perchesb4834832013-04-05 12:27:37 -07001829 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001830
1831 if (IS_ERR(resp)) {
1832 rc = PTR_ERR(resp);
1833
Joe Perches073a6252013-04-05 12:27:38 -07001834 nfc_err(&dev->interface->dev, "RF setting error %d", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001835
1836 return rc;
1837 }
1838
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001839 queue_delayed_work(dev->wq, &dev->poll_work,
1840 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001841
1842 dev_kfree_skb(resp);
1843 return rc;
1844}
1845
1846static void pn533_wq_rf(struct work_struct *work)
1847{
1848 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1849 struct sk_buff *skb;
1850 int rc;
1851
Joe Perchesb4834832013-04-05 12:27:37 -07001852 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001853
1854 skb = pn533_alloc_skb(dev, 2);
1855 if (!skb)
1856 return;
1857
1858 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02001859 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001860
1861 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1862 pn533_rf_complete, NULL);
1863 if (rc < 0) {
1864 dev_kfree_skb(skb);
Joe Perches073a6252013-04-05 12:27:38 -07001865 nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001866 }
1867
1868 return;
1869}
1870
Samuel Ortiz673088f2013-08-21 15:06:55 +02001871static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1872 struct sk_buff *resp)
1873{
1874 struct pn533_cmd_jump_dep_response *rsp;
1875 struct nfc_target nfc_target;
1876 u8 target_gt_len;
1877 int rc;
1878
1879 if (IS_ERR(resp))
1880 return PTR_ERR(resp);
1881
1882 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1883
1884 rc = rsp->status & PN533_CMD_RET_MASK;
1885 if (rc != PN533_CMD_RET_SUCCESS) {
1886 /* Not target found, turn radio off */
1887 queue_work(dev->wq, &dev->rf_work);
1888
1889 dev_kfree_skb(resp);
1890 return 0;
1891 }
1892
1893 dev_dbg(&dev->interface->dev, "Creating new target");
1894
1895 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1896 nfc_target.nfcid1_len = 10;
1897 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1898 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1899 if (rc)
1900 goto error;
1901
1902 dev->tgt_available_prots = 0;
1903 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1904
1905 /* ATR_RES general bytes are located at offset 17 */
1906 target_gt_len = resp->len - 17;
1907 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1908 rsp->gt, target_gt_len);
1909 if (!rc) {
1910 rc = nfc_dep_link_is_up(dev->nfc_dev,
1911 dev->nfc_dev->targets[0].idx,
1912 0, NFC_RF_INITIATOR);
1913
1914 if (!rc)
1915 pn533_poll_reset_mod_list(dev);
1916 }
1917error:
1918 dev_kfree_skb(resp);
1919 return rc;
1920}
1921
1922#define PASSIVE_DATA_LEN 5
1923static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1924{
1925 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1926 struct sk_buff *skb;
1927 int rc, skb_len;
1928 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1929 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1930
1931 dev_dbg(&dev->interface->dev, "%s", __func__);
1932
1933 if (!dev->gb) {
1934 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1935
1936 if (!dev->gb || !dev->gb_len) {
1937 dev->poll_dep = 0;
1938 queue_work(dev->wq, &dev->rf_work);
1939 }
1940 }
1941
1942 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1943 skb_len += PASSIVE_DATA_LEN;
1944
1945 /* NFCID3 */
1946 skb_len += NFC_NFCID3_MAXSIZE;
1947 nfcid3[0] = 0x1;
1948 nfcid3[1] = 0xfe;
1949 get_random_bytes(nfcid3 + 2, 6);
1950
1951 skb = pn533_alloc_skb(dev, skb_len);
1952 if (!skb)
1953 return -ENOMEM;
1954
1955 *skb_put(skb, 1) = 0x01; /* Active */
1956 *skb_put(skb, 1) = 0x02; /* 424 kbps */
1957
1958 next = skb_put(skb, 1); /* Next */
1959 *next = 0;
1960
1961 /* Copy passive data */
1962 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1963 *next |= 1;
1964
1965 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1966 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1967 NFC_NFCID3_MAXSIZE);
1968 *next |= 2;
1969
1970 memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1971 *next |= 4; /* We have some Gi */
1972
1973 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1974 pn533_poll_dep_complete, NULL);
1975
1976 if (rc < 0)
1977 dev_kfree_skb(skb);
1978
1979 return rc;
1980}
1981
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001982static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001983 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001984{
1985 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001986 int rc;
1987
Joe Perchesb4834832013-04-05 12:27:37 -07001988 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001989
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001990 if (IS_ERR(resp)) {
1991 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001992
Joe Perches073a6252013-04-05 12:27:38 -07001993 nfc_err(&dev->interface->dev, "%s Poll complete error %d\n",
1994 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001995
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001996 if (rc == -ENOENT) {
1997 if (dev->poll_mod_count != 0)
1998 return rc;
1999 else
2000 goto stop_poll;
2001 } else if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07002002 nfc_err(&dev->interface->dev,
2003 "Error %d when running poll\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002004 goto stop_poll;
2005 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002006 }
2007
2008 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2009
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002010 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002011 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002012 rc = pn533_init_target_complete(dev, resp);
2013 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002014 }
2015
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002016 /* Initiator mode */
2017 rc = pn533_start_poll_complete(dev, resp);
2018 if (!rc)
2019 goto done;
2020
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02002021 if (!dev->poll_mod_count) {
Joe Perchesb4834832013-04-05 12:27:37 -07002022 dev_dbg(&dev->interface->dev, "Polling has been stopped\n");
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02002023 goto done;
2024 }
2025
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002026 pn533_poll_next_mod(dev);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002027 /* Not target found, turn radio off */
2028 queue_work(dev->wq, &dev->rf_work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002029
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002030done:
2031 dev_kfree_skb(resp);
2032 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002033
2034stop_poll:
Joe Perches073a6252013-04-05 12:27:38 -07002035 nfc_err(&dev->interface->dev, "Polling operation has been stopped\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002036
Samuel Ortizad3823c2012-05-30 23:54:55 +02002037 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002038 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002039 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002040}
Samuel Ortizad3823c2012-05-30 23:54:55 +02002041
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002042static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
2043 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002044{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002045 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02002046
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002047 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002048 if (!skb)
2049 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002050
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002051 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002052
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002053 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002054}
2055
2056static int pn533_send_poll_frame(struct pn533 *dev)
2057{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002058 struct pn533_poll_modulations *mod;
2059 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002060 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002061 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002062
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002063 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002064
Joe Perchesb4834832013-04-05 12:27:37 -07002065 dev_dbg(&dev->interface->dev, "%s mod len %d\n",
2066 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02002067
Samuel Ortiz673088f2013-08-21 15:06:55 +02002068 if (dev->poll_dep) {
2069 dev->poll_dep = 0;
2070 return pn533_poll_dep(dev->nfc_dev);
2071 }
2072
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002073 if (mod->len == 0) { /* Listen mode */
2074 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002075 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002076 } else { /* Polling mode */
2077 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002078 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002079 }
2080
2081 if (!skb) {
Joe Perches073a6252013-04-05 12:27:38 -07002082 nfc_err(&dev->interface->dev, "Failed to allocate skb\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002083 return -ENOMEM;
2084 }
2085
2086 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
2087 NULL);
2088 if (rc < 0) {
2089 dev_kfree_skb(skb);
Joe Perches073a6252013-04-05 12:27:38 -07002090 nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01002091 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02002092
2093 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002094}
2095
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002096static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002097{
Samuel Ortiz46f793b2013-07-03 14:50:36 +02002098 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002099 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002100 int rc;
2101
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002102 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2103
Joe Perchesb4834832013-04-05 12:27:37 -07002104 dev_dbg(&dev->interface->dev,
2105 "%s cancel_listen %d modulation len %d\n",
2106 __func__, dev->cancel_listen, cur_mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002107
2108 if (dev->cancel_listen == 1) {
2109 dev->cancel_listen = 0;
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002110 pn533_abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111 }
2112
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002113 rc = pn533_send_poll_frame(dev);
2114 if (rc)
2115 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002116
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002117 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
2118 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002119
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002120 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002121}
2122
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002123static int pn533_start_poll(struct nfc_dev *nfc_dev,
2124 u32 im_protocols, u32 tm_protocols)
2125{
2126 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002127 struct pn533_poll_modulations *cur_mod;
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02002128 u8 rand_mod;
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002129 int rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002130
Joe Perchesb4834832013-04-05 12:27:37 -07002131 dev_dbg(&dev->interface->dev,
2132 "%s: im protocols 0x%x tm protocols 0x%x\n",
2133 __func__, im_protocols, tm_protocols);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002134
2135 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002136 nfc_err(&dev->interface->dev,
2137 "Cannot poll with a target already activated\n");
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002138 return -EBUSY;
2139 }
2140
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002141 if (dev->tgt_mode) {
Joe Perches073a6252013-04-05 12:27:38 -07002142 nfc_err(&dev->interface->dev,
2143 "Cannot poll while already being activated\n");
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002144 return -EBUSY;
2145 }
2146
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002147 if (tm_protocols) {
2148 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
2149 if (dev->gb == NULL)
2150 tm_protocols = 0;
2151 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02002152
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002153 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
2154 dev->poll_protocols = im_protocols;
2155 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02002156
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02002157 /* Do not always start polling from the same modulation */
2158 get_random_bytes(&rand_mod, sizeof(rand_mod));
2159 rand_mod %= dev->poll_mod_count;
2160 dev->poll_mod_curr = rand_mod;
2161
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02002162 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2163
2164 rc = pn533_send_poll_frame(dev);
2165
2166 /* Start listen timer */
2167 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
2168 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
2169
2170 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02002171}
2172
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002173static void pn533_stop_poll(struct nfc_dev *nfc_dev)
2174{
2175 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2176
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002177 del_timer(&dev->listen_timer);
2178
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002179 if (!dev->poll_mod_count) {
Joe Perchesb4834832013-04-05 12:27:37 -07002180 dev_dbg(&dev->interface->dev,
2181 "Polling operation was not running\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002182 return;
2183 }
2184
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002185 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02002186 flush_delayed_work(&dev->poll_work);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02002187 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002188}
2189
2190static int pn533_activate_target_nfcdep(struct pn533 *dev)
2191{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002192 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01002193 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002194 int rc;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002195 struct sk_buff *skb;
2196 struct sk_buff *resp;
2197
Joe Perchesb4834832013-04-05 12:27:37 -07002198 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002199
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002200 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002201 if (!skb)
2202 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002203
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002204 *skb_put(skb, sizeof(u8)) = 1; /* TG */
2205 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002206
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002207 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
2208 if (IS_ERR(resp))
2209 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002210
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002211 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002212 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01002213 if (rc != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002214 nfc_err(&dev->interface->dev,
2215 "Target activation failed (error 0x%x)\n", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002216 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002217 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01002218 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002219
Samuel Ortiz541d9202011-12-14 16:43:10 +01002220 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002221 gt_len = resp->len - 16;
2222 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01002223
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002224 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01002225 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002226}
2227
Eric Lapuyade90099432012-05-07 12:31:13 +02002228static int pn533_activate_target(struct nfc_dev *nfc_dev,
2229 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002230{
2231 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2232 int rc;
2233
Joe Perches17936b42013-04-05 12:27:39 -07002234 dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002235
2236 if (dev->poll_mod_count) {
Joe Perches073a6252013-04-05 12:27:38 -07002237 nfc_err(&dev->interface->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07002238 "Cannot activate while polling\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002239 return -EBUSY;
2240 }
2241
2242 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002243 nfc_err(&dev->interface->dev,
2244 "There is already an active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002245 return -EBUSY;
2246 }
2247
2248 if (!dev->tgt_available_prots) {
Joe Perches073a6252013-04-05 12:27:38 -07002249 nfc_err(&dev->interface->dev,
2250 "There is no available target to activate\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002251 return -EINVAL;
2252 }
2253
2254 if (!(dev->tgt_available_prots & (1 << protocol))) {
Joe Perches073a6252013-04-05 12:27:38 -07002255 nfc_err(&dev->interface->dev,
2256 "Target doesn't support requested proto %u\n",
2257 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002258 return -EINVAL;
2259 }
2260
2261 if (protocol == NFC_PROTO_NFC_DEP) {
2262 rc = pn533_activate_target_nfcdep(dev);
2263 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002264 nfc_err(&dev->interface->dev,
2265 "Activating target with DEP failed %d\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002266 return rc;
2267 }
2268 }
2269
2270 dev->tgt_active_prot = protocol;
2271 dev->tgt_available_prots = 0;
2272
2273 return 0;
2274}
2275
Eric Lapuyade90099432012-05-07 12:31:13 +02002276static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2277 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002278{
2279 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002280 struct sk_buff *skb;
2281 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002282 int rc;
2283
Joe Perchesb4834832013-04-05 12:27:37 -07002284 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002285
2286 if (!dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002287 nfc_err(&dev->interface->dev, "There is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002288 return;
2289 }
2290
2291 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002292 skb_queue_purge(&dev->resp_q);
2293
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002294 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002295 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002296 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002297
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002298 *skb_put(skb, 1) = 1; /* TG*/
2299
2300 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2301 if (IS_ERR(resp))
2302 return;
2303
2304 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002305 if (rc != PN533_CMD_RET_SUCCESS)
Joe Perches073a6252013-04-05 12:27:38 -07002306 nfc_err(&dev->interface->dev,
2307 "Error 0x%x when releasing the target\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002308
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002309 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002310 return;
2311}
2312
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002313
2314static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002315 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002316{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002317 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002318 u8 target_gt_len;
2319 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002320 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02002321
2322 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002323
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002324 if (IS_ERR(resp))
2325 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002326
2327 if (dev->tgt_available_prots &&
2328 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
Joe Perches073a6252013-04-05 12:27:38 -07002329 nfc_err(&dev->interface->dev,
2330 "The target does not support DEP\n");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002331 rc = -EINVAL;
2332 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002333 }
2334
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002335 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2336
2337 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002338 if (rc != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002339 nfc_err(&dev->interface->dev,
2340 "Bringing DEP link up failed (error 0x%x)\n", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002341 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002342 }
2343
2344 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002345 struct nfc_target nfc_target;
2346
Joe Perchesb4834832013-04-05 12:27:37 -07002347 dev_dbg(&dev->interface->dev, "Creating new target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002348
2349 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01002350 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002351 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002352 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2353 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002354 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002355
2356 dev->tgt_available_prots = 0;
2357 }
2358
2359 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2360
2361 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002362 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002363 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002364 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002365 if (rc == 0)
2366 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002367 dev->nfc_dev->targets[0].idx,
2368 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002369
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002370error:
2371 dev_kfree_skb(resp);
2372 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002373}
2374
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002375static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
Eric Lapuyade90099432012-05-07 12:31:13 +02002376static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002377 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002378{
2379 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002380 struct sk_buff *skb;
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002381 int rc, skb_len;
2382 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
Samuel Ortizd7f33452012-05-29 21:45:21 +02002383 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002384
Joe Perchesb4834832013-04-05 12:27:37 -07002385 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002386
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002387 if (dev->poll_mod_count) {
Joe Perches073a6252013-04-05 12:27:38 -07002388 nfc_err(&dev->interface->dev,
2389 "Cannot bring the DEP link up while polling\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002390 return -EBUSY;
2391 }
2392
2393 if (dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002394 nfc_err(&dev->interface->dev,
2395 "There is already an active target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002396 return -EBUSY;
2397 }
2398
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002399 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002400 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002401
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002402 /* NFCID3 */
2403 skb_len += NFC_NFCID3_MAXSIZE;
2404 if (target && !target->nfcid2_len) {
2405 nfcid3[0] = 0x1;
2406 nfcid3[1] = 0xfe;
2407 get_random_bytes(nfcid3 + 2, 6);
2408 }
Samuel Ortiz322bce92013-05-27 15:29:11 +02002409
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002410 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002411 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002412 return -ENOMEM;
2413
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002414 *skb_put(skb, 1) = !comm_mode; /* ActPass */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002415 *skb_put(skb, 1) = 0x02; /* 424 kbps */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002416
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002417 next = skb_put(skb, 1); /* Next */
2418 *next = 0;
2419
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002420 /* Copy passive data */
2421 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2422 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002423
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002424 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2425 if (target && target->nfcid2_len)
Samuel Ortiz322bce92013-05-27 15:29:11 +02002426 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2427 target->nfcid2_len);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002428 else
2429 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2430 NFC_NFCID3_MAXSIZE);
2431 *next |= 2;
Samuel Ortiz322bce92013-05-27 15:29:11 +02002432
Samuel Ortiz47807d32012-03-05 01:03:50 +01002433 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002434 memcpy(skb_put(skb, gb_len), gb, gb_len);
2435 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002436 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002437 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002438 }
2439
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002440 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2441 if (!arg) {
2442 dev_kfree_skb(skb);
2443 return -ENOMEM;
2444 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002445
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002446 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002447
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002448 pn533_rf_field(dev->nfc_dev, 0);
2449
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002450 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2451 pn533_in_dep_link_up_complete, arg);
2452
2453 if (rc < 0) {
2454 dev_kfree_skb(skb);
2455 kfree(arg);
2456 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002457
2458 return rc;
2459}
2460
2461static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2462{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002463 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2464
Joe Perchesb4834832013-04-05 12:27:37 -07002465 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002466
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002467 pn533_poll_reset_mod_list(dev);
2468
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002469 if (dev->tgt_mode || dev->tgt_active_prot)
2470 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002471
2472 dev->tgt_active_prot = 0;
2473 dev->tgt_mode = 0;
2474
2475 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002476
2477 return 0;
2478}
2479
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002480struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002481 data_exchange_cb_t cb;
2482 void *cb_context;
2483};
2484
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002485static struct sk_buff *pn533_build_response(struct pn533 *dev)
2486{
2487 struct sk_buff *skb, *tmp, *t;
2488 unsigned int skb_len = 0, tmp_len = 0;
2489
Joe Perchesb4834832013-04-05 12:27:37 -07002490 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002491
2492 if (skb_queue_empty(&dev->resp_q))
2493 return NULL;
2494
2495 if (skb_queue_len(&dev->resp_q) == 1) {
2496 skb = skb_dequeue(&dev->resp_q);
2497 goto out;
2498 }
2499
2500 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2501 skb_len += tmp->len;
2502
Joe Perchesb4834832013-04-05 12:27:37 -07002503 dev_dbg(&dev->interface->dev, "%s total length %d\n",
2504 __func__, skb_len);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002505
2506 skb = alloc_skb(skb_len, GFP_KERNEL);
2507 if (skb == NULL)
2508 goto out;
2509
2510 skb_put(skb, skb_len);
2511
2512 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2513 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2514 tmp_len += tmp->len;
2515 }
2516
2517out:
2518 skb_queue_purge(&dev->resp_q);
2519
2520 return skb;
2521}
2522
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002523static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002524 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002525{
2526 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002527 struct sk_buff *skb;
2528 int rc = 0;
2529 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002530
Joe Perchesb4834832013-04-05 12:27:37 -07002531 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002532
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002533 if (IS_ERR(resp)) {
2534 rc = PTR_ERR(resp);
2535 goto _error;
2536 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002538 status = resp->data[0];
2539 ret = status & PN533_CMD_RET_MASK;
2540 mi = status & PN533_CMD_MI_MASK;
2541
2542 skb_pull(resp, sizeof(status));
2543
2544 if (ret != PN533_CMD_RET_SUCCESS) {
Joe Perches073a6252013-04-05 12:27:38 -07002545 nfc_err(&dev->interface->dev,
2546 "Exchanging data failed (error 0x%x)\n", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002547 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002548 goto error;
2549 }
2550
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002551 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002552
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002553 if (mi) {
2554 dev->cmd_complete_mi_arg = arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +00002555 queue_work(dev->wq, &dev->mi_rx_work);
2556 return -EINPROGRESS;
2557 }
2558
2559 /* Prepare for the next round */
2560 if (skb_queue_len(&dev->fragment_skb) > 0) {
2561 dev->cmd_complete_dep_arg = arg;
2562 queue_work(dev->wq, &dev->mi_tx_work);
2563
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002564 return -EINPROGRESS;
2565 }
2566
2567 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002568 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002569 goto error;
2570
2571 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002572 kfree(arg);
2573 return 0;
2574
2575error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002576 dev_kfree_skb(resp);
2577_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002578 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002579 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002580 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002581 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002582}
2583
Olivier Guiter963a82e2013-06-13 13:43:28 +00002584/* Split the Tx skb into small chunks */
2585static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2586{
2587 struct sk_buff *frag;
2588 int frag_size;
2589
2590 do {
2591 /* Remaining size */
2592 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2593 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2594 else
2595 frag_size = skb->len;
2596
2597 /* Allocate and reserve */
2598 frag = pn533_alloc_skb(dev, frag_size);
2599 if (!frag) {
2600 skb_queue_purge(&dev->fragment_skb);
2601 break;
2602 }
2603
Olivier Guiter22953f92013-09-23 12:24:37 +02002604 if (!dev->tgt_mode) {
2605 /* Reserve the TG/MI byte */
2606 skb_reserve(frag, 1);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002607
Olivier Guiter22953f92013-09-23 12:24:37 +02002608 /* MI + TG */
2609 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2610 *skb_push(frag, sizeof(u8)) =
2611 (PN533_CMD_MI_MASK | 1);
2612 else
2613 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2614 }
Olivier Guiter963a82e2013-06-13 13:43:28 +00002615
2616 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2617
2618 /* Reduce the size of incoming buffer */
2619 skb_pull(skb, frag_size);
2620
2621 /* Add this to skb_queue */
2622 skb_queue_tail(&dev->fragment_skb, frag);
2623
2624 } while (skb->len > 0);
2625
2626 dev_kfree_skb(skb);
2627
2628 return skb_queue_len(&dev->fragment_skb);
2629}
2630
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002631static int pn533_transceive(struct nfc_dev *nfc_dev,
2632 struct nfc_target *target, struct sk_buff *skb,
2633 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002634{
2635 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002636 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002637 int rc;
2638
Joe Perchesb4834832013-04-05 12:27:37 -07002639 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002640
2641 if (!dev->tgt_active_prot) {
Joe Perches073a6252013-04-05 12:27:38 -07002642 nfc_err(&dev->interface->dev,
2643 "Can't exchange data if there is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002644 rc = -EINVAL;
2645 goto error;
2646 }
2647
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002648 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002649 if (!arg) {
2650 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002651 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002652 }
2653
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002654 arg->cb = cb;
2655 arg->cb_context = cb_context;
2656
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002657 switch (dev->device_type) {
2658 case PN533_DEVICE_PASORI:
2659 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2660 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2661 skb,
2662 pn533_data_exchange_complete,
2663 arg);
2664
2665 break;
2666 }
2667 default:
Olivier Guiter963a82e2013-06-13 13:43:28 +00002668 /* jumbo frame ? */
2669 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2670 rc = pn533_fill_fragment_skbs(dev, skb);
2671 if (rc <= 0)
2672 goto error;
2673
2674 skb = skb_dequeue(&dev->fragment_skb);
2675 if (!skb) {
2676 rc = -EIO;
2677 goto error;
2678 }
2679 } else {
2680 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2681 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002682
2683 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2684 skb, pn533_data_exchange_complete,
2685 arg);
2686
2687 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002688 }
2689
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002690 if (rc < 0) /* rc from send_async */
2691 goto error;
2692
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002693 return 0;
2694
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002695error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002696 kfree(arg);
2697 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002698 return rc;
2699}
2700
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002701static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002702 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002703{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002704 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002705
Joe Perchesb4834832013-04-05 12:27:37 -07002706 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002707
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002708 if (IS_ERR(resp))
2709 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002710
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002711 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002712
Olivier Guiter93ad4202013-09-23 12:24:39 +02002713 /* Prepare for the next round */
2714 if (skb_queue_len(&dev->fragment_skb) > 0) {
2715 queue_work(dev->wq, &dev->mi_tm_tx_work);
2716 return -EINPROGRESS;
2717 }
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002718 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002719
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002720 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002721 nfc_tm_deactivated(dev->nfc_dev);
2722
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002723 dev->tgt_mode = 0;
2724
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002725 return 0;
2726 }
2727
2728 queue_work(dev->wq, &dev->tg_work);
2729
2730 return 0;
2731}
2732
2733static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2734{
2735 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002736 int rc;
2737
Joe Perchesb4834832013-04-05 12:27:37 -07002738 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002739
Olivier Guiter93ad4202013-09-23 12:24:39 +02002740 /* let's split in multiple chunks if size's too big */
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002741 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Olivier Guiter93ad4202013-09-23 12:24:39 +02002742 rc = pn533_fill_fragment_skbs(dev, skb);
2743 if (rc <= 0)
2744 goto error;
2745
2746 /* get the first skb */
2747 skb = skb_dequeue(&dev->fragment_skb);
2748 if (!skb) {
2749 rc = -EIO;
2750 goto error;
2751 }
2752
2753 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2754 pn533_tm_send_complete, NULL);
2755 } else {
2756 /* Send th skb */
2757 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2758 pn533_tm_send_complete, NULL);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002759 }
2760
Olivier Guiter93ad4202013-09-23 12:24:39 +02002761error:
2762 if (rc < 0) {
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002763 dev_kfree_skb(skb);
Olivier Guiter93ad4202013-09-23 12:24:39 +02002764 skb_queue_purge(&dev->fragment_skb);
2765 }
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002766
2767 return rc;
2768}
2769
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002770static void pn533_wq_mi_recv(struct work_struct *work)
2771{
Olivier Guiter963a82e2013-06-13 13:43:28 +00002772 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002773 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002774 int rc;
2775
Joe Perchesb4834832013-04-05 12:27:37 -07002776 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002777
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002778 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002779 if (!skb)
2780 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002781
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002782 switch (dev->device_type) {
2783 case PN533_DEVICE_PASORI:
2784 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2785 rc = pn533_send_cmd_direct_async(dev,
2786 PN533_CMD_IN_COMM_THRU,
2787 skb,
2788 pn533_data_exchange_complete,
2789 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002790
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002791 break;
2792 }
2793 default:
2794 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002795
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002796 rc = pn533_send_cmd_direct_async(dev,
2797 PN533_CMD_IN_DATA_EXCHANGE,
2798 skb,
2799 pn533_data_exchange_complete,
2800 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002801
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002802 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002803 }
2804
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002805 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002806 return;
2807
Joe Perches073a6252013-04-05 12:27:38 -07002808 nfc_err(&dev->interface->dev,
2809 "Error %d when trying to perform data_exchange\n", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002810
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002811 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002812 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002813
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002814error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002815 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002816 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002817}
2818
Olivier Guiter963a82e2013-06-13 13:43:28 +00002819static void pn533_wq_mi_send(struct work_struct *work)
2820{
2821 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2822 struct sk_buff *skb;
2823 int rc;
2824
Joe Perchesb4834832013-04-05 12:27:37 -07002825 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002826
2827 /* Grab the first skb in the queue */
2828 skb = skb_dequeue(&dev->fragment_skb);
2829
2830 if (skb == NULL) { /* No more data */
2831 /* Reset the queue for future use */
2832 skb_queue_head_init(&dev->fragment_skb);
2833 goto error;
2834 }
2835
2836 switch (dev->device_type) {
2837 case PN533_DEVICE_PASORI:
2838 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2839 rc = -EIO;
2840 break;
2841 }
2842
2843 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2844 skb,
2845 pn533_data_exchange_complete,
2846 dev->cmd_complete_dep_arg);
2847
2848 break;
2849
2850 default:
2851 /* Still some fragments? */
2852 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2853 skb,
2854 pn533_data_exchange_complete,
2855 dev->cmd_complete_dep_arg);
2856
2857 break;
2858 }
2859
2860 if (rc == 0) /* success */
2861 return;
2862
Joe Perches073a6252013-04-05 12:27:38 -07002863 nfc_err(&dev->interface->dev,
2864 "Error %d when trying to perform data_exchange\n", rc);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002865
2866 dev_kfree_skb(skb);
2867 kfree(dev->cmd_complete_dep_arg);
2868
2869error:
2870 pn533_send_ack(dev, GFP_KERNEL);
2871 queue_work(dev->wq, &dev->cmd_work);
2872}
2873
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002874static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2875 u8 cfgdata_len)
2876{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002877 struct sk_buff *skb;
2878 struct sk_buff *resp;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002879 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002880
Joe Perchesb4834832013-04-05 12:27:37 -07002881 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002882
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002883 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002884
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002885 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002886 if (!skb)
2887 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002888
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002889 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2890 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002891
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002892 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2893 if (IS_ERR(resp))
2894 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002895
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002896 dev_kfree_skb(resp);
2897 return 0;
2898}
2899
2900static int pn533_get_firmware_version(struct pn533 *dev,
2901 struct pn533_fw_version *fv)
2902{
2903 struct sk_buff *skb;
2904 struct sk_buff *resp;
2905
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002906 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002907 if (!skb)
2908 return -ENOMEM;
2909
2910 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2911 if (IS_ERR(resp))
2912 return PTR_ERR(resp);
2913
2914 fv->ic = resp->data[0];
2915 fv->ver = resp->data[1];
2916 fv->rev = resp->data[2];
2917 fv->support = resp->data[3];
2918
2919 dev_kfree_skb(resp);
2920 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002921}
2922
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002923static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002924{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002925 struct sk_buff *skb;
2926 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002927
Joe Perchesb4834832013-04-05 12:27:37 -07002928 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002929
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002930 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002931 if (!skb)
2932 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002933
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002934 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002935
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002936 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2937 if (IS_ERR(resp))
2938 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002939
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002940 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002941
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002942 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002943}
2944
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002945struct pn533_acr122_poweron_rdr_arg {
2946 int rc;
2947 struct completion done;
2948};
2949
2950static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2951{
2952 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2953
Joe Perchesb4834832013-04-05 12:27:37 -07002954 dev_dbg(&urb->dev->dev, "%s\n", __func__);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002955
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002956 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002957 urb->transfer_buffer, urb->transfer_buffer_length,
2958 false);
2959
2960 arg->rc = urb->status;
2961 complete(&arg->done);
2962}
2963
2964static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2965{
2966 /* Power on th reader (CCID cmd) */
2967 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2968 0, 0, 0, 0, 0, 0, 3, 0, 0};
2969 u8 buf[255];
2970 int rc;
2971 void *cntx;
2972 struct pn533_acr122_poweron_rdr_arg arg;
2973
Joe Perchesb4834832013-04-05 12:27:37 -07002974 dev_dbg(&dev->interface->dev, "%s\n", __func__);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002975
2976 init_completion(&arg.done);
2977 cntx = dev->in_urb->context; /* backup context */
2978
2979 dev->in_urb->transfer_buffer = buf;
2980 dev->in_urb->transfer_buffer_length = 255;
2981 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2982 dev->in_urb->context = &arg;
2983
2984 dev->out_urb->transfer_buffer = cmd;
2985 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2986
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002987 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002988 cmd, sizeof(cmd), false);
2989
2990 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2991 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002992 nfc_err(&dev->interface->dev,
2993 "Reader power on cmd error %d\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002994 return rc;
2995 }
2996
2997 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2998 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07002999 nfc_err(&dev->interface->dev,
3000 "Can't submit reader poweron cmd response %d\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003001 return rc;
3002 }
3003
3004 wait_for_completion(&arg.done);
3005 dev->in_urb->context = cntx; /* restore context */
3006
3007 return arg.rc;
3008}
3009
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003010static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
3011{
3012 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
3013 u8 rf_field = !!rf;
3014 int rc;
3015
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02003016 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
3017
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003018 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
3019 (u8 *)&rf_field, 1);
3020 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07003021 nfc_err(&dev->interface->dev, "Error on setting RF field\n");
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003022 return rc;
3023 }
3024
3025 return rc;
3026}
3027
Sachin Kamate44666b2013-09-20 14:30:41 +05303028static int pn533_dev_up(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003029{
3030 return pn533_rf_field(nfc_dev, 1);
3031}
3032
Sachin Kamate44666b2013-09-20 14:30:41 +05303033static int pn533_dev_down(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003034{
3035 return pn533_rf_field(nfc_dev, 0);
3036}
3037
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003038static struct nfc_ops pn533_nfc_ops = {
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02003039 .dev_up = pn533_dev_up,
3040 .dev_down = pn533_dev_down,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01003041 .dep_link_up = pn533_dep_link_up,
3042 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003043 .start_poll = pn533_start_poll,
3044 .stop_poll = pn533_stop_poll,
3045 .activate_target = pn533_activate_target,
3046 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02003047 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02003048 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003049};
3050
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003051static int pn533_setup(struct pn533 *dev)
3052{
3053 struct pn533_config_max_retries max_retries;
3054 struct pn533_config_timing timing;
3055 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
3056 int rc;
3057
3058 switch (dev->device_type) {
3059 case PN533_DEVICE_STD:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003060 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003061 case PN533_DEVICE_ACR122U:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003062 max_retries.mx_rty_atr = 0x2;
3063 max_retries.mx_rty_psl = 0x1;
3064 max_retries.mx_rty_passive_act =
3065 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
3066
3067 timing.rfu = PN533_CONFIG_TIMING_102;
3068 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
3069 timing.dep_timeout = PN533_CONFIG_TIMING_204;
3070
3071 break;
3072
3073 default:
Joe Perches073a6252013-04-05 12:27:38 -07003074 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
3075 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003076 return -EINVAL;
3077 }
3078
3079 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
3080 (u8 *)&max_retries, sizeof(max_retries));
3081 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07003082 nfc_err(&dev->interface->dev,
3083 "Error on setting MAX_RETRIES config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003084 return rc;
3085 }
3086
3087
3088 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
3089 (u8 *)&timing, sizeof(timing));
3090 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07003091 nfc_err(&dev->interface->dev, "Error on setting RF timings\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003092 return rc;
3093 }
3094
3095 switch (dev->device_type) {
3096 case PN533_DEVICE_STD:
3097 break;
3098
3099 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02003100 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003101
3102 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
3103 pasori_cfg, 3);
3104 if (rc) {
Joe Perches073a6252013-04-05 12:27:38 -07003105 nfc_err(&dev->interface->dev,
3106 "Error while settings PASORI config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003107 return rc;
3108 }
3109
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02003110 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003111
3112 break;
3113 }
3114
3115 return 0;
3116}
3117
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003118static int pn533_probe(struct usb_interface *interface,
3119 const struct usb_device_id *id)
3120{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01003121 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003122 struct pn533 *dev;
3123 struct usb_host_interface *iface_desc;
3124 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003125 int in_endpoint = 0;
3126 int out_endpoint = 0;
3127 int rc = -ENOMEM;
3128 int i;
3129 u32 protocols;
3130
3131 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3132 if (!dev)
3133 return -ENOMEM;
3134
3135 dev->udev = usb_get_dev(interface_to_usbdev(interface));
3136 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02003137 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003138
3139 iface_desc = interface->cur_altsetting;
3140 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
3141 endpoint = &iface_desc->endpoint[i].desc;
3142
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01003143 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003144 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003145
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01003146 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003147 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003148 }
3149
3150 if (!in_endpoint || !out_endpoint) {
Joe Perches073a6252013-04-05 12:27:38 -07003151 nfc_err(&interface->dev,
3152 "Could not find bulk-in or bulk-out endpoint\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003153 rc = -ENODEV;
3154 goto error;
3155 }
3156
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003157 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003158 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
3159
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01003160 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003161 goto error;
3162
3163 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01003164 usb_rcvbulkpipe(dev->udev, in_endpoint),
3165 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003166 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01003167 usb_sndbulkpipe(dev->udev, out_endpoint),
3168 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003169
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003170 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
3171 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Olivier Guiter963a82e2013-06-13 13:43:28 +00003172 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
3173 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02003174 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Olivier Guiter3c13b242013-09-23 12:24:38 +02003175 INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv);
Olivier Guiter93ad4202013-09-23 12:24:39 +02003176 INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02003177 INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02003178 INIT_WORK(&dev->rf_work, pn533_wq_rf);
Tejun Heo58637c92012-08-22 16:28:46 -07003179 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02003180 if (dev->wq == NULL)
3181 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003182
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02003183 init_timer(&dev->listen_timer);
3184 dev->listen_timer.data = (unsigned long) dev;
3185 dev->listen_timer.function = pn533_listen_mode_timer;
3186
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003187 skb_queue_head_init(&dev->resp_q);
Olivier Guiter963a82e2013-06-13 13:43:28 +00003188 skb_queue_head_init(&dev->fragment_skb);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003189
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003190 INIT_LIST_HEAD(&dev->cmd_queue);
3191
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003192 usb_set_intfdata(interface, dev);
3193
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003194 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003195
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +02003196 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003197 dev->device_type = id->driver_info;
3198 switch (dev->device_type) {
3199 case PN533_DEVICE_STD:
3200 protocols = PN533_ALL_PROTOCOLS;
3201 break;
3202
3203 case PN533_DEVICE_PASORI:
3204 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3205 break;
3206
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003207 case PN533_DEVICE_ACR122U:
3208 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3209 dev->ops = &pn533_acr122_frame_ops;
3210 dev->protocol_type = PN533_PROTO_REQ_RESP,
3211
3212 rc = pn533_acr122_poweron_rdr(dev);
3213 if (rc < 0) {
Joe Perches073a6252013-04-05 12:27:38 -07003214 nfc_err(&dev->interface->dev,
3215 "Couldn't poweron the reader (error %d)\n", rc);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02003216 goto destroy_wq;
3217 }
3218 break;
3219
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003220 default:
Joe Perches073a6252013-04-05 12:27:38 -07003221 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
3222 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003223 rc = -EINVAL;
3224 goto destroy_wq;
3225 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003226
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003227 memset(&fw_ver, 0, sizeof(fw_ver));
3228 rc = pn533_get_firmware_version(dev, &fw_ver);
3229 if (rc < 0)
3230 goto destroy_wq;
3231
Joe Perches073a6252013-04-05 12:27:38 -07003232 nfc_info(&dev->interface->dev,
3233 "NXP PN5%02X firmware ver %d.%d now attached\n",
3234 fw_ver.ic, fw_ver.ver, fw_ver.rev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003235
3236
Samuel Ortize8753042011-08-19 15:47:11 +02003237 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003238 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02003239 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01003240 dev->ops->tx_tail_len);
Wei Yongjun4674d0f2013-05-07 20:07:52 +08003241 if (!dev->nfc_dev) {
3242 rc = -ENOMEM;
Samuel Ortiz4849f852012-04-10 19:43:17 +02003243 goto destroy_wq;
Wei Yongjun4674d0f2013-05-07 20:07:52 +08003244 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003245
3246 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3247 nfc_set_drvdata(dev->nfc_dev, dev);
3248
3249 rc = nfc_register_device(dev->nfc_dev);
3250 if (rc)
3251 goto free_nfc_dev;
3252
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003253 rc = pn533_setup(dev);
3254 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003255 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02003256
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003257 return 0;
3258
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003259unregister_nfc_dev:
3260 nfc_unregister_device(dev->nfc_dev);
3261
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003262free_nfc_dev:
3263 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003264
Samuel Ortiz4849f852012-04-10 19:43:17 +02003265destroy_wq:
3266 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003267error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003268 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003269 usb_free_urb(dev->out_urb);
Marina Makienko7c5a54f2013-02-26 11:41:18 +04003270 usb_put_dev(dev->udev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003271 kfree(dev);
3272 return rc;
3273}
3274
3275static void pn533_disconnect(struct usb_interface *interface)
3276{
3277 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003278 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003279
3280 dev = usb_get_intfdata(interface);
3281 usb_set_intfdata(interface, NULL);
3282
3283 nfc_unregister_device(dev->nfc_dev);
3284 nfc_free_device(dev->nfc_dev);
3285
3286 usb_kill_urb(dev->in_urb);
3287 usb_kill_urb(dev->out_urb);
3288
Samuel Ortiz46f793b2013-07-03 14:50:36 +02003289 flush_delayed_work(&dev->poll_work);
Samuel Ortiz4849f852012-04-10 19:43:17 +02003290 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003291
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003292 skb_queue_purge(&dev->resp_q);
3293
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02003294 del_timer(&dev->listen_timer);
3295
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003296 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3297 list_del(&cmd->queue);
3298 kfree(cmd);
3299 }
3300
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003301 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003302 usb_free_urb(dev->out_urb);
3303 kfree(dev);
3304
Joe Perches073a6252013-04-05 12:27:38 -07003305 nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003306}
3307
3308static struct usb_driver pn533_driver = {
3309 .name = "pn533",
3310 .probe = pn533_probe,
3311 .disconnect = pn533_disconnect,
3312 .id_table = pn533_table,
3313};
3314
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08003315module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003316
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003317MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3318MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3319MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003320MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3321MODULE_VERSION(VERSION);
3322MODULE_LICENSE("GPL");