blob: 2f1ebbd8cba6de121b8913c0057fe8bfff10fe45 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
Waldemar Rymarkiewicz495af722013-04-03 08:02:16 +020030#define VERSION "0.2"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030031
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +020041#define ACS_VENDOR_ID 0x072f
42#define ACR122U_PRODUCT_ID 0x2200
43
44#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46#define PN533_DEVICE_ACR122U 0x3
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020047
Samuel Ortiz01d719a2012-07-04 00:14:04 +020048#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
49 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
50 NFC_PROTO_NFC_DEP_MASK |\
51 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020052
53#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
54 NFC_PROTO_MIFARE_MASK | \
55 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020056 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020057 NFC_PROTO_NFC_DEP_MASK)
58
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030059static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020060 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
61 .idVendor = PN533_VENDOR_ID,
62 .idProduct = PN533_PRODUCT_ID,
63 .driver_info = PN533_DEVICE_STD,
64 },
65 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
66 .idVendor = SCM_VENDOR_ID,
67 .idProduct = SCL3711_PRODUCT_ID,
68 .driver_info = PN533_DEVICE_STD,
69 },
70 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
71 .idVendor = SONY_VENDOR_ID,
72 .idProduct = PASORI_PRODUCT_ID,
73 .driver_info = PN533_DEVICE_PASORI,
74 },
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +020075 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
76 .idVendor = ACS_VENDOR_ID,
77 .idProduct = ACR122U_PRODUCT_ID,
78 .driver_info = PN533_DEVICE_ACR122U,
79 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030080 { }
81};
82MODULE_DEVICE_TABLE(usb, pn533_table);
83
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020084/* How much time we spend listening for initiators */
85#define PN533_LISTEN_TIME 2
86
Olivier Guiter1575b9d2013-06-13 15:43:27 +020087/* Standard pn533 frame definitions (standard and extended)*/
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020088#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010089 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020090#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020091
Olivier Guiter1575b9d2013-06-13 15:43:27 +020092#define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
93 + 2) /* data[0] TFI, data[1] CC */
94
95#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
96#define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
97
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010098/*
99 * Max extended frame payload len, excluding TFI and CC
100 * which are already in PN533_FRAME_HEADER_LEN.
101 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200102#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100103
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200104#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100105 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200106#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
107#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200108/* Half start code (3), LEN (4) should be 0xffff for extended frame */
109#define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
110 && (hdr)->datalen_checksum == 0xFF)
111#define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300112
113/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200114#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300115
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200116/* standard frame identifier: in/out/error */
117#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
118#define PN533_STD_FRAME_DIR_OUT 0xD4
119#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300120
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200121/* ACS ACR122 pn533 frame definitions */
122#define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
123 + 2)
124#define PN533_ACR122_TX_FRAME_TAIL_LEN 0
125#define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
126 + 2)
127#define PN533_ACR122_RX_FRAME_TAIL_LEN 2
128#define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
129
130/* CCID messages types */
131#define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
132#define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
133
134#define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
135
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300136/* PN533 Commands */
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200137#define PN533_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300138
139#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
140#define PN533_CMD_RF_CONFIGURATION 0x32
141#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200142#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300143#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
144#define PN533_CMD_IN_ATR 0x50
145#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100146#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300147
Samuel Ortizad3823c2012-05-30 23:54:55 +0200148#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200149#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200150#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100151#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200152
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
154
155/* PN533 Return codes */
156#define PN533_CMD_RET_MASK 0x3F
157#define PN533_CMD_MI_MASK 0x40
158#define PN533_CMD_RET_SUCCESS 0x00
159
160struct pn533;
161
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100162typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
163 struct sk_buff *resp);
164
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300165/* structs for pn533 commands */
166
167/* PN533_CMD_GET_FIRMWARE_VERSION */
168struct pn533_fw_version {
169 u8 ic;
170 u8 ver;
171 u8 rev;
172 u8 support;
173};
174
175/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200176#define PN533_CFGITEM_RF_FIELD 0x01
177#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300178#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz60d9edd52013-04-11 11:45:41 +0200179#define PN533_CFGITEM_PASORI 0x82
180
Samuel Ortiz3a8eab32013-07-01 17:26:58 +0200181#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
182#define PN533_CFGITEM_RF_FIELD_ON 0x1
183#define PN533_CFGITEM_RF_FIELD_OFF 0x0
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300184
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200185#define PN533_CONFIG_TIMING_102 0xb
186#define PN533_CONFIG_TIMING_204 0xc
187#define PN533_CONFIG_TIMING_409 0xd
188#define PN533_CONFIG_TIMING_819 0xe
189
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300190#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
191#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
192
193struct pn533_config_max_retries {
194 u8 mx_rty_atr;
195 u8 mx_rty_psl;
196 u8 mx_rty_passive_act;
197} __packed;
198
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200199struct pn533_config_timing {
200 u8 rfu;
201 u8 atr_res_timeout;
202 u8 dep_timeout;
203} __packed;
204
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
206
207/* felica commands opcode */
208#define PN533_FELICA_OPC_SENSF_REQ 0
209#define PN533_FELICA_OPC_SENSF_RES 1
210/* felica SENSF_REQ parameters */
211#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
212#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
213#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
214#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
215
216/* type B initiator_data values */
217#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
218#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
219#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
220
221union pn533_cmd_poll_initdata {
222 struct {
223 u8 afi;
224 u8 polling_method;
225 } __packed type_b;
226 struct {
227 u8 opcode;
228 __be16 sc;
229 u8 rc;
230 u8 tsn;
231 } __packed felica;
232};
233
234/* Poll modulations */
235enum {
236 PN533_POLL_MOD_106KBPS_A,
237 PN533_POLL_MOD_212KBPS_FELICA,
238 PN533_POLL_MOD_424KBPS_FELICA,
239 PN533_POLL_MOD_106KBPS_JEWEL,
240 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200241 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300242
243 __PN533_POLL_MOD_AFTER_LAST,
244};
245#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
246
247struct pn533_poll_modulations {
248 struct {
249 u8 maxtg;
250 u8 brty;
251 union pn533_cmd_poll_initdata initiator_data;
252 } __packed data;
253 u8 len;
254};
255
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100256static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300257 [PN533_POLL_MOD_106KBPS_A] = {
258 .data = {
259 .maxtg = 1,
260 .brty = 0,
261 },
262 .len = 2,
263 },
264 [PN533_POLL_MOD_212KBPS_FELICA] = {
265 .data = {
266 .maxtg = 1,
267 .brty = 1,
268 .initiator_data.felica = {
269 .opcode = PN533_FELICA_OPC_SENSF_REQ,
270 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200271 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200272 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300273 },
274 },
275 .len = 7,
276 },
277 [PN533_POLL_MOD_424KBPS_FELICA] = {
278 .data = {
279 .maxtg = 1,
280 .brty = 2,
281 .initiator_data.felica = {
282 .opcode = PN533_FELICA_OPC_SENSF_REQ,
283 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200284 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200285 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300286 },
287 },
288 .len = 7,
289 },
290 [PN533_POLL_MOD_106KBPS_JEWEL] = {
291 .data = {
292 .maxtg = 1,
293 .brty = 4,
294 },
295 .len = 2,
296 },
297 [PN533_POLL_MOD_847KBPS_B] = {
298 .data = {
299 .maxtg = 1,
300 .brty = 8,
301 .initiator_data.type_b = {
302 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
303 .polling_method =
304 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
305 },
306 },
307 .len = 3,
308 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200309 [PN533_LISTEN_MOD] = {
310 .len = 0,
311 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300312};
313
314/* PN533_CMD_IN_ATR */
315
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300316struct pn533_cmd_activate_response {
317 u8 status;
318 u8 nfcid3t[10];
319 u8 didt;
320 u8 bst;
321 u8 brt;
322 u8 to;
323 u8 ppt;
324 /* optional */
325 u8 gt[];
326} __packed;
327
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100328struct pn533_cmd_jump_dep_response {
329 u8 status;
330 u8 tg;
331 u8 nfcid3t[10];
332 u8 didt;
333 u8 bst;
334 u8 brt;
335 u8 to;
336 u8 ppt;
337 /* optional */
338 u8 gt[];
339} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300340
Samuel Ortizad3823c2012-05-30 23:54:55 +0200341
342/* PN533_TG_INIT_AS_TARGET */
343#define PN533_INIT_TARGET_PASSIVE 0x1
344#define PN533_INIT_TARGET_DEP 0x2
345
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200346#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
347#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
348#define PN533_INIT_TARGET_RESP_DEP 0x4
349
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200350enum pn533_protocol_type {
351 PN533_PROTO_REQ_ACK_RESP = 0,
352 PN533_PROTO_REQ_RESP
353};
354
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300355struct pn533 {
356 struct usb_device *udev;
357 struct usb_interface *interface;
358 struct nfc_dev *nfc_dev;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200359 u32 device_type;
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200360 enum pn533_protocol_type protocol_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300361
362 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300363 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300364
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200365 struct sk_buff_head resp_q;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000366 struct sk_buff_head fragment_skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200367
Samuel Ortiz4849f852012-04-10 19:43:17 +0200368 struct workqueue_struct *wq;
369 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200370 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200371 struct work_struct poll_work;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000372 struct work_struct mi_rx_work;
373 struct work_struct mi_tx_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200374 struct work_struct tg_work;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +0200375 struct work_struct rf_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200376
377 struct list_head cmd_queue;
378 struct pn533_cmd *cmd;
379 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200380 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300381
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100382 void *cmd_complete_mi_arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +0000383 void *cmd_complete_dep_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300384
385 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
386 u8 poll_mod_count;
387 u8 poll_mod_curr;
388 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200389 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200390 struct timer_list listen_timer;
391 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200392
393 u8 *gb;
394 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300395
396 u8 tgt_available_prots;
397 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200398 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200399
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100400 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200401};
402
403struct pn533_cmd {
404 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200405 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200406 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100407 struct sk_buff *req;
408 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100409 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200410 pn533_send_async_complete_t complete_cb;
411 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300412};
413
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200414struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300415 u8 preamble;
416 __be16 start_frame;
417 u8 datalen;
418 u8 datalen_checksum;
419 u8 data[];
420} __packed;
421
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200422struct pn533_ext_frame { /* Extended Information frame */
423 u8 preamble;
424 __be16 start_frame;
425 __be16 eif_flag; /* fixed to 0xFFFF */
426 __be16 datalen;
427 u8 datalen_checksum;
428 u8 data[];
429} __packed;
430
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100431struct pn533_frame_ops {
432 void (*tx_frame_init)(void *frame, u8 cmd_code);
433 void (*tx_frame_finish)(void *frame);
434 void (*tx_update_payload_len)(void *frame, int len);
435 int tx_header_len;
436 int tx_tail_len;
437
Olivier Guiter56a63c822013-06-13 13:43:29 +0000438 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100439 int (*rx_frame_size)(void *frame);
440 int rx_header_len;
441 int rx_tail_len;
442
443 int max_payload_len;
444 u8 (*get_cmd_code)(void *frame);
445};
446
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200447struct pn533_acr122_ccid_hdr {
448 u8 type;
449 u32 datalen;
450 u8 slot;
451 u8 seq;
452 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
453 byte for reposnse msg */
454 u8 data[]; /* payload */
455} __packed;
456
457struct pn533_acr122_apdu_hdr {
458 u8 class;
459 u8 ins;
460 u8 p1;
461 u8 p2;
462} __packed;
463
464struct pn533_acr122_tx_frame {
465 struct pn533_acr122_ccid_hdr ccid;
466 struct pn533_acr122_apdu_hdr apdu;
467 u8 datalen;
468 u8 data[]; /* pn533 frame: TFI ... */
469} __packed;
470
471struct pn533_acr122_rx_frame {
472 struct pn533_acr122_ccid_hdr ccid;
473 u8 data[]; /* pn533 frame : TFI ... */
474} __packed;
475
476static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
477{
478 struct pn533_acr122_tx_frame *frame = _frame;
479
480 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
481 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
482 sizeof(datalen) */
483 frame->ccid.slot = 0;
484 frame->ccid.seq = 0;
485 frame->ccid.params[0] = 0;
486 frame->ccid.params[1] = 0;
487 frame->ccid.params[2] = 0;
488
489 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
490 frame->data[1] = cmd_code;
491 frame->datalen = 2; /* data[0] + data[1] */
492
493 frame->apdu.class = 0xFF;
494 frame->apdu.ins = 0;
495 frame->apdu.p1 = 0;
496 frame->apdu.p2 = 0;
497}
498
499static void pn533_acr122_tx_frame_finish(void *_frame)
500{
501 struct pn533_acr122_tx_frame *frame = _frame;
502
503 frame->ccid.datalen += frame->datalen;
504}
505
506static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
507{
508 struct pn533_acr122_tx_frame *frame = _frame;
509
510 frame->datalen += len;
511}
512
Olivier Guiter56a63c822013-06-13 13:43:29 +0000513static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200514{
515 struct pn533_acr122_rx_frame *frame = _frame;
516
517 if (frame->ccid.type != 0x83)
518 return false;
519
520 if (frame->data[frame->ccid.datalen - 2] == 0x63)
521 return false;
522
523 return true;
524}
525
526static int pn533_acr122_rx_frame_size(void *frame)
527{
528 struct pn533_acr122_rx_frame *f = frame;
529
530 /* f->ccid.datalen already includes tail length */
531 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
532}
533
534static u8 pn533_acr122_get_cmd_code(void *frame)
535{
536 struct pn533_acr122_rx_frame *f = frame;
537
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200538 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +0200539}
540
541static struct pn533_frame_ops pn533_acr122_frame_ops = {
542 .tx_frame_init = pn533_acr122_tx_frame_init,
543 .tx_frame_finish = pn533_acr122_tx_frame_finish,
544 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
545 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
546 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
547
548 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
549 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
550 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
551 .rx_frame_size = pn533_acr122_rx_frame_size,
552
553 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
554 .get_cmd_code = pn533_acr122_get_cmd_code,
555};
556
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200557/* The rule: value(high byte) + value(low byte) + checksum = 0 */
558static inline u8 pn533_ext_checksum(u16 value)
559{
560 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
561}
562
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200564static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565{
566 return ~value + 1;
567}
568
569/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200570static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300571{
572 u8 sum = 0;
573 int i;
574
575 for (i = 0; i < datalen; i++)
576 sum += data[i];
577
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200578 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300579}
580
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200581static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300582{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200583 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100584
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200586 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
587 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200588 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300589 frame->datalen = 2;
590}
591
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200592static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300593{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200594 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100595
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200596 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300597
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200598 PN533_STD_FRAME_CHECKSUM(frame) =
599 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300600
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200601 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602}
603
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200604static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100605{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200606 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100607
608 frame->datalen += len;
609}
610
Olivier Guiter56a63c822013-06-13 13:43:29 +0000611static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300612{
613 u8 checksum;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200614 struct pn533_std_frame *stdf = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300615
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200616 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300617 return false;
618
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200619 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
620 /* Standard frame code */
Olivier Guiter56a63c822013-06-13 13:43:29 +0000621 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300622
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200623 checksum = pn533_std_checksum(stdf->datalen);
624 if (checksum != stdf->datalen_checksum)
625 return false;
626
627 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
628 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
629 return false;
630 } else {
631 /* Extended */
632 struct pn533_ext_frame *eif = _frame;
633
Olivier Guiter56a63c822013-06-13 13:43:29 +0000634 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
635
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200636 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
637 if (checksum != eif->datalen_checksum)
638 return false;
639
640 /* check data checksum */
641 checksum = pn533_std_data_checksum(eif->data,
642 be16_to_cpu(eif->datalen));
643 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
644 return false;
645 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300646
647 return true;
648}
649
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200650static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200652 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300653 return false;
654
655 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
656 return false;
657
658 return true;
659}
660
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200661static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300662{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200663 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100664
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200665 /* check for Extended Information frame */
666 if (PN533_STD_IS_EXTENDED(f)) {
667 struct pn533_ext_frame *eif = frame;
668
669 return sizeof(struct pn533_ext_frame)
670 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
671 }
672
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200673 return sizeof(struct pn533_std_frame) + f->datalen +
674 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100675}
676
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200677static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100678{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200679 struct pn533_std_frame *f = frame;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200680 struct pn533_ext_frame *eif = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100681
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200682 if (PN533_STD_IS_EXTENDED(f))
683 return PN533_FRAME_CMD(eif);
684 else
685 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100686}
687
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100688static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200689 .tx_frame_init = pn533_std_tx_frame_init,
690 .tx_frame_finish = pn533_std_tx_frame_finish,
691 .tx_update_payload_len = pn533_std_tx_update_payload_len,
692 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
693 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100694
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200695 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
696 .rx_frame_size = pn533_std_rx_frame_size,
697 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
698 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100699
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200700 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
701 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100702};
703
704static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
705{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200706 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200707 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300708}
709
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300710static void pn533_recv_response(struct urb *urb)
711{
712 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200713 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100714 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300715
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200716 cmd->status = urb->status;
717
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300718 switch (urb->status) {
719 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100720 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300721 case -ECONNRESET:
722 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100723 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100724 "The urb has been canceled (status %d)",
725 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200726 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100727 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300728 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100729 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100730 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200731 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300732 }
733
734 in_frame = dev->in_urb->transfer_buffer;
735
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100736 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100737 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
738 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100739
Olivier Guiter56a63c822013-06-13 13:43:29 +0000740 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300741 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200742 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200743 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300744 }
745
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100746 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100747 nfc_dev_err(&dev->interface->dev,
748 "It it not the response to the last command");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200749 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200750 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300751 }
752
Samuel Ortiz4849f852012-04-10 19:43:17 +0200753sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200754 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300755}
756
757static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
758{
759 dev->in_urb->complete = pn533_recv_response;
760
761 return usb_submit_urb(dev->in_urb, flags);
762}
763
764static void pn533_recv_ack(struct urb *urb)
765{
766 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200767 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200768 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300769 int rc;
770
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200771 cmd->status = urb->status;
772
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300773 switch (urb->status) {
774 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100775 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300776 case -ECONNRESET:
777 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100778 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100779 "The urb has been stopped (status %d)",
780 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200781 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100782 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300783 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100784 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100785 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200786 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300787 }
788
789 in_frame = dev->in_urb->transfer_buffer;
790
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200791 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300792 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200793 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200794 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300795 }
796
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300797 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
798 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100799 nfc_dev_err(&dev->interface->dev,
800 "usb_submit_urb failed with result %d", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200801 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200802 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300803 }
804
805 return;
806
Samuel Ortiz4849f852012-04-10 19:43:17 +0200807sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200808 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300809}
810
811static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
812{
813 dev->in_urb->complete = pn533_recv_ack;
814
815 return usb_submit_urb(dev->in_urb, flags);
816}
817
818static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
819{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200820 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100821 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300822 int rc;
823
824 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
825
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100826 dev->out_urb->transfer_buffer = ack;
827 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300828 rc = usb_submit_urb(dev->out_urb, flags);
829
830 return rc;
831}
832
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100833static int __pn533_send_frame_async(struct pn533 *dev,
834 struct sk_buff *out,
835 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200836 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300837{
838 int rc;
839
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100840 dev->out_urb->transfer_buffer = out->data;
841 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300842
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100843 dev->in_urb->transfer_buffer = in->data;
844 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300845
Samuel Ortize279f842013-03-20 11:27:57 +0100846 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
847 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100848
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100849 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300850 if (rc)
851 return rc;
852
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +0200853 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
854 /* request for response for sent packet directly */
855 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
856 if (rc)
857 goto error;
858 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
859 /* request for ACK if that's the case */
860 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
861 if (rc)
862 goto error;
863 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300864
865 return 0;
866
867error:
868 usb_unlink_urb(dev->out_urb);
869 return rc;
870}
871
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100872static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
873 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100874{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100875 /* payload is already there, just update datalen */
876 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100877 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100878
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100879
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100880 skb_push(skb, ops->tx_header_len);
881 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100882
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100883 ops->tx_frame_init(skb->data, cmd_code);
884 ops->tx_update_payload_len(skb->data, payload_len);
885 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100886}
887
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200888static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100889{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200890 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200891 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100892
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200893 struct sk_buff *req = cmd->req;
894 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100895
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100896 int rc;
897
898 dev_kfree_skb(req);
899
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100900 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200901 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
902 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100903 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200904 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100905 }
906
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100907 skb_put(resp, dev->ops->rx_frame_size(resp->data));
908 skb_pull(resp, dev->ops->rx_header_len);
909 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100910
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200911 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100912
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200913done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200914 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200915 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100916 return rc;
917}
918
919static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
920 struct sk_buff *req, struct sk_buff *resp,
921 int resp_len,
922 pn533_send_async_complete_t complete_cb,
923 void *complete_cb_context)
924{
925 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100926 int rc = 0;
927
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100928 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100929
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200930 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
931 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100932 return -ENOMEM;
933
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200934 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200935 cmd->req = req;
936 cmd->resp = resp;
937 cmd->resp_len = resp_len;
938 cmd->complete_cb = complete_cb;
939 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100940
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100941 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100942
943 mutex_lock(&dev->cmd_lock);
944
945 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200946 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100947 if (rc)
948 goto error;
949
950 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200951 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100952 goto unlock;
953 }
954
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100955 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
956 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100957
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100958 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100959 list_add_tail(&cmd->queue, &dev->cmd_queue);
960
961 goto unlock;
962
963error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200964 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100965unlock:
966 mutex_unlock(&dev->cmd_lock);
967 return rc;
968}
969
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100970static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
971 struct sk_buff *req,
972 pn533_send_async_complete_t complete_cb,
973 void *complete_cb_context)
974{
975 struct sk_buff *resp;
976 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100977 int resp_len = dev->ops->rx_header_len +
978 dev->ops->max_payload_len +
979 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100980
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100981 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
982 if (!resp)
983 return -ENOMEM;
984
985 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
986 complete_cb_context);
987 if (rc)
988 dev_kfree_skb(resp);
989
990 return rc;
991}
992
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100993static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
994 struct sk_buff *req,
995 pn533_send_async_complete_t complete_cb,
996 void *complete_cb_context)
997{
998 struct sk_buff *resp;
999 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001000 int resp_len = dev->ops->rx_header_len +
1001 dev->ops->max_payload_len +
1002 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001003
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001004 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001005 if (!resp)
1006 return -ENOMEM;
1007
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001008 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1009 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +01001010 if (rc)
1011 dev_kfree_skb(resp);
1012
1013 return rc;
1014}
1015
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001016/*
1017 * pn533_send_cmd_direct_async
1018 *
1019 * The function sends a piority cmd directly to the chip omiting the cmd
1020 * queue. It's intended to be used by chaining mechanism of received responses
1021 * where the host has to request every single chunk of data before scheduling
1022 * next cmd from the queue.
1023 */
1024static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1025 struct sk_buff *req,
1026 pn533_send_async_complete_t complete_cb,
1027 void *complete_cb_context)
1028{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001029 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001030 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001031 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001032 int resp_len = dev->ops->rx_header_len +
1033 dev->ops->max_payload_len +
1034 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001035
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001036 resp = alloc_skb(resp_len, GFP_KERNEL);
1037 if (!resp)
1038 return -ENOMEM;
1039
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001040 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1041 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001042 dev_kfree_skb(resp);
1043 return -ENOMEM;
1044 }
1045
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +02001046 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001047 cmd->req = req;
1048 cmd->resp = resp;
1049 cmd->resp_len = resp_len;
1050 cmd->complete_cb = complete_cb;
1051 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001052
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001053 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001054
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001055 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001056 if (rc < 0) {
1057 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001058 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001059 } else {
1060 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001061 }
1062
1063 return rc;
1064}
1065
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +02001066static void pn533_wq_cmd_complete(struct work_struct *work)
1067{
1068 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1069 int rc;
1070
1071 rc = pn533_send_async_complete(dev);
1072 if (rc != -EINPROGRESS)
1073 queue_work(dev->wq, &dev->cmd_work);
1074}
1075
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001076static void pn533_wq_cmd(struct work_struct *work)
1077{
1078 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1079 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001080 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001081
1082 mutex_lock(&dev->cmd_lock);
1083
1084 if (list_empty(&dev->cmd_queue)) {
1085 dev->cmd_pending = 0;
1086 mutex_unlock(&dev->cmd_lock);
1087 return;
1088 }
1089
1090 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1091
Szymon Janc60ad07a2012-10-25 17:29:45 +02001092 list_del(&cmd->queue);
1093
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001094 mutex_unlock(&dev->cmd_lock);
1095
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +02001096 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001097 if (rc < 0) {
1098 dev_kfree_skb(cmd->req);
1099 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +02001100 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001101 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +02001102 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +02001103
1104 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02001105}
1106
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001107struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001108 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001109 struct completion done;
1110};
1111
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001112static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1113 struct sk_buff *resp)
1114{
1115 struct pn533_sync_cmd_response *arg = _arg;
1116
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001117 arg->resp = resp;
1118 complete(&arg->done);
1119
1120 return 0;
1121}
1122
1123/* pn533_send_cmd_sync
1124 *
1125 * Please note the req parameter is freed inside the function to
1126 * limit a number of return value interpretations by the caller.
1127 *
1128 * 1. negative in case of error during TX path -> req should be freed
1129 *
1130 * 2. negative in case of error during RX path -> req should not be freed
1131 * as it's been already freed at the begining of RX path by
1132 * async_complete_cb.
1133 *
1134 * 3. valid pointer in case of succesfult RX path
1135 *
1136 * A caller has to check a return value with IS_ERR macro. If the test pass,
1137 * the returned pointer is valid.
1138 *
1139 * */
1140static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1141 struct sk_buff *req)
1142{
1143 int rc;
1144 struct pn533_sync_cmd_response arg;
1145
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01001146 init_completion(&arg.done);
1147
1148 rc = pn533_send_cmd_async(dev, cmd_code, req,
1149 pn533_send_sync_complete, &arg);
1150 if (rc) {
1151 dev_kfree_skb(req);
1152 return ERR_PTR(rc);
1153 }
1154
1155 wait_for_completion(&arg.done);
1156
1157 return arg.resp;
1158}
1159
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001160static void pn533_send_complete(struct urb *urb)
1161{
1162 struct pn533 *dev = urb->context;
1163
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001164 switch (urb->status) {
1165 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001166 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001167 case -ECONNRESET:
1168 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001169 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001170 "The urb has been stopped (status %d)",
1171 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001172 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001173 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001174 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +01001175 nfc_dev_err(&dev->interface->dev,
1176 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001177 }
1178}
1179
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001180static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1181{
1182 /* ACR122U does not support any command which aborts last
1183 * issued command i.e. as ACK for standard PN533. Additionally,
1184 * it behaves stange, sending broken or incorrect responses,
1185 * when we cancel urb before the chip will send response.
1186 */
1187 if (dev->device_type == PN533_DEVICE_ACR122U)
1188 return;
1189
1190 /* An ack will cancel the last issued command */
1191 pn533_send_ack(dev, flags);
1192
1193 /* cancel the urb request */
1194 usb_kill_urb(dev->in_urb);
1195}
1196
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001197static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001198{
1199 struct sk_buff *skb;
1200
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001201 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001202 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001203 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001204
1205 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001206 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +01001207
1208 return skb;
1209}
1210
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001211struct pn533_target_type_a {
1212 __be16 sens_res;
1213 u8 sel_res;
1214 u8 nfcid_len;
1215 u8 nfcid_data[];
1216} __packed;
1217
1218
1219#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1220#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1221#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1222
1223#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1224#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1225
1226#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1227#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1228
1229#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1230#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1231#define PN533_TYPE_A_SEL_PROT_DEP 2
1232#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1233
1234static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1235 int target_data_len)
1236{
1237 u8 ssd;
1238 u8 platconf;
1239
1240 if (target_data_len < sizeof(struct pn533_target_type_a))
1241 return false;
1242
1243 /* The lenght check of nfcid[] and ats[] are not being performed because
1244 the values are not being used */
1245
1246 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1247 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1248 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1249
1250 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001251 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1252 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1253 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001254 return false;
1255
1256 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1257 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1258 return false;
1259
1260 return true;
1261}
1262
1263static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1264 int tgt_data_len)
1265{
1266 struct pn533_target_type_a *tgt_type_a;
1267
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001268 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001269
1270 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1271 return -EPROTO;
1272
1273 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1274 case PN533_TYPE_A_SEL_PROT_MIFARE:
1275 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1276 break;
1277 case PN533_TYPE_A_SEL_PROT_ISO14443:
1278 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1279 break;
1280 case PN533_TYPE_A_SEL_PROT_DEP:
1281 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1282 break;
1283 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1284 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1285 NFC_PROTO_NFC_DEP_MASK;
1286 break;
1287 }
1288
1289 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1290 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001291 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1292 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001293
1294 return 0;
1295}
1296
1297struct pn533_target_felica {
1298 u8 pol_res;
1299 u8 opcode;
Samuel Ortiz322bce92013-05-27 15:29:11 +02001300 u8 nfcid2[NFC_NFCID2_MAXSIZE];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001301 u8 pad[8];
1302 /* optional */
1303 u8 syst_code[];
1304} __packed;
1305
1306#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1307#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1308
1309static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1310 int target_data_len)
1311{
1312 if (target_data_len < sizeof(struct pn533_target_felica))
1313 return false;
1314
1315 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1316 return false;
1317
1318 return true;
1319}
1320
1321static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1322 int tgt_data_len)
1323{
1324 struct pn533_target_felica *tgt_felica;
1325
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001326 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327
1328 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1329 return -EPROTO;
1330
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001331 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1332 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001333 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1334 else
1335 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1336
Samuel Ortiz79757542012-03-05 01:03:45 +01001337 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1338 nfc_tgt->sensf_res_len = 9;
1339
Samuel Ortiz322bce92013-05-27 15:29:11 +02001340 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1341 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1342
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001343 return 0;
1344}
1345
1346struct pn533_target_jewel {
1347 __be16 sens_res;
1348 u8 jewelid[4];
1349} __packed;
1350
1351static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1352 int target_data_len)
1353{
1354 u8 ssd;
1355 u8 platconf;
1356
1357 if (target_data_len < sizeof(struct pn533_target_jewel))
1358 return false;
1359
1360 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1361 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1362 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1363
1364 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001365 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1366 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1367 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001368 return false;
1369
1370 return true;
1371}
1372
1373static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1374 int tgt_data_len)
1375{
1376 struct pn533_target_jewel *tgt_jewel;
1377
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001378 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001379
1380 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1381 return -EPROTO;
1382
1383 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1384 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001385 nfc_tgt->nfcid1_len = 4;
1386 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001387
1388 return 0;
1389}
1390
1391struct pn533_type_b_prot_info {
1392 u8 bitrate;
1393 u8 fsci_type;
1394 u8 fwi_adc_fo;
1395} __packed;
1396
1397#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1398#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1399#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1400
1401struct pn533_type_b_sens_res {
1402 u8 opcode;
1403 u8 nfcid[4];
1404 u8 appdata[4];
1405 struct pn533_type_b_prot_info prot_info;
1406} __packed;
1407
1408#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1409
1410struct pn533_target_type_b {
1411 struct pn533_type_b_sens_res sensb_res;
1412 u8 attrib_res_len;
1413 u8 attrib_res[];
1414} __packed;
1415
1416static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1417 int target_data_len)
1418{
1419 if (target_data_len < sizeof(struct pn533_target_type_b))
1420 return false;
1421
1422 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1423 return false;
1424
1425 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1426 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1427 return false;
1428
1429 return true;
1430}
1431
1432static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1433 int tgt_data_len)
1434{
1435 struct pn533_target_type_b *tgt_type_b;
1436
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001437 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001438
1439 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1440 return -EPROTO;
1441
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001442 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001443
1444 return 0;
1445}
1446
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001447static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1448 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001449{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001450 struct nfc_target nfc_tgt;
1451 int rc;
1452
1453 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001454 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001455
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001457 return -EPROTO;
1458
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001459 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1460
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001461 switch (dev->poll_mod_curr) {
1462 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001463 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001464 break;
1465 case PN533_POLL_MOD_212KBPS_FELICA:
1466 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001468 break;
1469 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001470 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001471 break;
1472 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001473 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001474 break;
1475 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001476 nfc_dev_err(&dev->interface->dev,
1477 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001478 return -EPROTO;
1479 }
1480
1481 if (rc)
1482 return rc;
1483
1484 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001485 nfc_dev_dbg(&dev->interface->dev,
1486 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001487 return -EAGAIN;
1488 }
1489
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001490 nfc_dev_dbg(&dev->interface->dev,
1491 "Target found - supported protocols: 0x%x",
1492 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001493
1494 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1495
1496 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1497
1498 return 0;
1499}
1500
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001501static inline void pn533_poll_next_mod(struct pn533 *dev)
1502{
1503 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1504}
1505
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001506static void pn533_poll_reset_mod_list(struct pn533 *dev)
1507{
1508 dev->poll_mod_count = 0;
1509}
1510
1511static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1512{
1513 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001514 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001515 dev->poll_mod_count++;
1516}
1517
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518static void pn533_poll_create_mod_list(struct pn533 *dev,
1519 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001520{
1521 pn533_poll_reset_mod_list(dev);
1522
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001523 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1524 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1525 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001526 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1527
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001528 if (im_protocols & NFC_PROTO_FELICA_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_212KBPS_FELICA);
1531 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1532 }
1533
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001534 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001535 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1536
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001537 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001538 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001539
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001540 if (tm_protocols)
1541 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001542}
1543
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001544static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001545{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001546 u8 nbtg, tg, *tgdata;
1547 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001548
1549 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1550
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 nbtg = resp->data[0];
1552 tg = resp->data[1];
1553 tgdata = &resp->data[2];
1554 tgdata_len = resp->len - 2; /* nbtg + tg */
1555
1556 if (nbtg) {
1557 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001558
1559 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560 if (rc == 0) {
1561 pn533_poll_reset_mod_list(dev);
1562 return 0;
1563 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001564 }
1565
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001566 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001567}
1568
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001569static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001570{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001571 struct sk_buff *skb;
1572 u8 *felica, *nfcid3, *gb;
1573
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001574 u8 *gbytes = dev->gb;
1575 size_t gbytes_len = dev->gb_len;
1576
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001577 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1578 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1579 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1580 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001581
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001582 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1583 0x0, 0x0, 0x0,
1584 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001585
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001586 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1587 felica (18), nfcid3 (10), gb_len (1) */
1588 gbytes_len +
1589 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001590
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001591 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 if (!skb)
1593 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001594
1595 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001596 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001597
1598 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001599 memcpy(skb_put(skb, 6), mifare_params, 6);
1600
1601 /* Felica params */
1602 felica = skb_put(skb, 18);
1603 memcpy(felica, felica_params, 18);
1604 get_random_bytes(felica + 2, 6);
1605
1606 /* NFCID3 */
1607 nfcid3 = skb_put(skb, 10);
1608 memset(nfcid3, 0, 10);
1609 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001610
1611 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001612 *skb_put(skb, 1) = gbytes_len;
1613
1614 gb = skb_put(skb, gbytes_len);
1615 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001616
Samuel Ortizad3823c2012-05-30 23:54:55 +02001617 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001618 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001619
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001620 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001621}
1622
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001623#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001624#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1625static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001626 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001627{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001628 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001629
1630 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1631
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001632 if (IS_ERR(resp))
1633 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001634
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001635 status = resp->data[0];
1636 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001637
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001638 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001639 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001640 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001641 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001642 return 0;
1643 }
1644
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001645 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001646}
1647
1648static void pn533_wq_tg_get_data(struct work_struct *work)
1649{
1650 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001651
1652 struct sk_buff *skb;
1653 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001654
1655 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1656
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001657 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001658 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001659 return;
1660
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001661 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1662 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001663
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001664 if (rc < 0)
1665 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001666
1667 return;
1668}
1669
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001670#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001671static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001672{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001673 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001674 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001675 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001676
1677 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1678
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001679 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001680 return -EINVAL;
1681
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001682 mode = resp->data[0];
1683 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001684
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001685 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1686 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001687
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001688 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1689 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001690 comm_mode = NFC_COMM_ACTIVE;
1691
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001692 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001693 return -EOPNOTSUPP;
1694
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001695 gb = cmd + ATR_REQ_GB_OFFSET;
1696 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001697
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001698 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1699 comm_mode, gb, gb_len);
1700 if (rc < 0) {
1701 nfc_dev_err(&dev->interface->dev,
1702 "Error when signaling target activation");
1703 return rc;
1704 }
1705
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001706 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001707 queue_work(dev->wq, &dev->tg_work);
1708
1709 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001710}
1711
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001712static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001713{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001714 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001715
1716 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1717
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001718 dev->cancel_listen = 1;
1719
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001720 pn533_poll_next_mod(dev);
1721
1722 queue_work(dev->wq, &dev->poll_work);
1723}
1724
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001725static int pn533_rf_complete(struct pn533 *dev, void *arg,
1726 struct sk_buff *resp)
1727{
1728 int rc = 0;
1729
1730 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1731
1732 if (IS_ERR(resp)) {
1733 rc = PTR_ERR(resp);
1734
1735 nfc_dev_err(&dev->interface->dev, "%s RF setting error %d",
1736 __func__, rc);
1737
1738 return rc;
1739 }
1740
1741 queue_work(dev->wq, &dev->poll_work);
1742
1743 dev_kfree_skb(resp);
1744 return rc;
1745}
1746
1747static void pn533_wq_rf(struct work_struct *work)
1748{
1749 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1750 struct sk_buff *skb;
1751 int rc;
1752
1753 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1754
1755 skb = pn533_alloc_skb(dev, 2);
1756 if (!skb)
1757 return;
1758
1759 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02001760 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001761
1762 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1763 pn533_rf_complete, NULL);
1764 if (rc < 0) {
1765 dev_kfree_skb(skb);
1766 nfc_dev_err(&dev->interface->dev, "RF setting error %d", rc);
1767 }
1768
1769 return;
1770}
1771
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001772static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001773 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001774{
1775 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001776 int rc;
1777
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001778 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1779
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001780 if (IS_ERR(resp)) {
1781 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001782
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001783 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1784 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001785
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001786 if (rc == -ENOENT) {
1787 if (dev->poll_mod_count != 0)
1788 return rc;
1789 else
1790 goto stop_poll;
1791 } else if (rc < 0) {
1792 nfc_dev_err(&dev->interface->dev,
1793 "Error %d when running poll", rc);
1794 goto stop_poll;
1795 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001796 }
1797
1798 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1799
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001800 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001801 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001802 rc = pn533_init_target_complete(dev, resp);
1803 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001804 }
1805
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001806 /* Initiator mode */
1807 rc = pn533_start_poll_complete(dev, resp);
1808 if (!rc)
1809 goto done;
1810
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001811 if (!dev->poll_mod_count) {
Masanari Iida8b513d02013-05-21 23:13:12 +09001812 nfc_dev_dbg(&dev->interface->dev, "Polling has been stopped.");
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001813 goto done;
1814 }
1815
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001816 pn533_poll_next_mod(dev);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001817 /* Not target found, turn radio off */
1818 queue_work(dev->wq, &dev->rf_work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001819
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001820done:
1821 dev_kfree_skb(resp);
1822 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001823
1824stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001825 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1826
Samuel Ortizad3823c2012-05-30 23:54:55 +02001827 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001828 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001829 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001830}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001831
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001832static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1833 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001834{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001835 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001836
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001837 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001838 if (!skb)
1839 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001840
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001841 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001842
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001843 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001844}
1845
1846static int pn533_send_poll_frame(struct pn533 *dev)
1847{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001848 struct pn533_poll_modulations *mod;
1849 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001850 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001851 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001852
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001853 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001854
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001855 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1856 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001857
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001858 if (mod->len == 0) { /* Listen mode */
1859 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001860 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001861 } else { /* Polling mode */
1862 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001863 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001864 }
1865
1866 if (!skb) {
1867 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1868 return -ENOMEM;
1869 }
1870
1871 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1872 NULL);
1873 if (rc < 0) {
1874 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001875 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001876 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001877
1878 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001879}
1880
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001881static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001882{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001883 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1884 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001885 int rc;
1886
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001887 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1888
1889 nfc_dev_dbg(&dev->interface->dev,
1890 "%s cancel_listen %d modulation len %d",
1891 __func__, dev->cancel_listen, cur_mod->len);
1892
1893 if (dev->cancel_listen == 1) {
1894 dev->cancel_listen = 0;
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001895 pn533_abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001896 }
1897
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001898 rc = pn533_send_poll_frame(dev);
1899 if (rc)
1900 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001901
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001902 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1903 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001904
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001905 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001906}
1907
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001908static int pn533_start_poll(struct nfc_dev *nfc_dev,
1909 u32 im_protocols, u32 tm_protocols)
1910{
1911 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1912
1913 nfc_dev_dbg(&dev->interface->dev,
1914 "%s: im protocols 0x%x tm protocols 0x%x",
1915 __func__, im_protocols, tm_protocols);
1916
1917 if (dev->tgt_active_prot) {
1918 nfc_dev_err(&dev->interface->dev,
1919 "Cannot poll with a target already activated");
1920 return -EBUSY;
1921 }
1922
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001923 if (dev->tgt_mode) {
1924 nfc_dev_err(&dev->interface->dev,
1925 "Cannot poll while already being activated");
1926 return -EBUSY;
1927 }
1928
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001929 if (tm_protocols) {
1930 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1931 if (dev->gb == NULL)
1932 tm_protocols = 0;
1933 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001934
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001935 dev->poll_mod_curr = 0;
1936 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1937 dev->poll_protocols = im_protocols;
1938 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001939
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001940 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001941}
1942
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001943static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1944{
1945 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1946
1947 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1948
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001949 del_timer(&dev->listen_timer);
1950
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001951 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001952 nfc_dev_dbg(&dev->interface->dev,
1953 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001954 return;
1955 }
1956
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001957 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001958 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001959}
1960
1961static int pn533_activate_target_nfcdep(struct pn533 *dev)
1962{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001963 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001964 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001965 int rc;
1966
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001967 struct sk_buff *skb;
1968 struct sk_buff *resp;
1969
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001970 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1971
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001972 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001973 if (!skb)
1974 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001975
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001976 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1977 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001978
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001979 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1980 if (IS_ERR(resp))
1981 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001982
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001983 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001984 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001985 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001986 nfc_dev_err(&dev->interface->dev,
1987 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001988 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001989 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001990 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001991
Samuel Ortiz541d9202011-12-14 16:43:10 +01001992 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001993 gt_len = resp->len - 16;
1994 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001995
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001996 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001997 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001998}
1999
Eric Lapuyade90099432012-05-07 12:31:13 +02002000static int pn533_activate_target(struct nfc_dev *nfc_dev,
2001 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002002{
2003 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2004 int rc;
2005
2006 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002007 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002008
2009 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002010 nfc_dev_err(&dev->interface->dev,
2011 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002012 return -EBUSY;
2013 }
2014
2015 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002016 nfc_dev_err(&dev->interface->dev,
2017 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002018 return -EBUSY;
2019 }
2020
2021 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002022 nfc_dev_err(&dev->interface->dev,
2023 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002024 return -EINVAL;
2025 }
2026
2027 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002028 nfc_dev_err(&dev->interface->dev,
2029 "Target doesn't support requested proto %u",
2030 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002031 return -EINVAL;
2032 }
2033
2034 if (protocol == NFC_PROTO_NFC_DEP) {
2035 rc = pn533_activate_target_nfcdep(dev);
2036 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002037 nfc_dev_err(&dev->interface->dev,
2038 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002039 return rc;
2040 }
2041 }
2042
2043 dev->tgt_active_prot = protocol;
2044 dev->tgt_available_prots = 0;
2045
2046 return 0;
2047}
2048
Eric Lapuyade90099432012-05-07 12:31:13 +02002049static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2050 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002051{
2052 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002053
2054 struct sk_buff *skb;
2055 struct sk_buff *resp;
2056
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002057 int rc;
2058
2059 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2060
2061 if (!dev->tgt_active_prot) {
2062 nfc_dev_err(&dev->interface->dev, "There is no active target");
2063 return;
2064 }
2065
2066 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002067 skb_queue_purge(&dev->resp_q);
2068
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002069 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002070 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002071 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002072
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002073 *skb_put(skb, 1) = 1; /* TG*/
2074
2075 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2076 if (IS_ERR(resp))
2077 return;
2078
2079 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002080 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002081 nfc_dev_err(&dev->interface->dev,
2082 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002083
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002084 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085 return;
2086}
2087
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002088
2089static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002090 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002091{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002092 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002093 u8 target_gt_len;
2094 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002095 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02002096
2097 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002098
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002099 if (IS_ERR(resp))
2100 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002101
2102 if (dev->tgt_available_prots &&
2103 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
2104 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002105 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002106 rc = -EINVAL;
2107 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002108 }
2109
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002110 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2111
2112 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002113 if (rc != PN533_CMD_RET_SUCCESS) {
2114 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002115 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002116 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002117 }
2118
2119 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002120 struct nfc_target nfc_target;
2121
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002122 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
2123
2124 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01002125 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002126 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002127 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2128 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002129 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002130
2131 dev->tgt_available_prots = 0;
2132 }
2133
2134 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2135
2136 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002137 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002138 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002139 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002140 if (rc == 0)
2141 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002142 dev->nfc_dev->targets[0].idx,
2143 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002144
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002145error:
2146 dev_kfree_skb(resp);
2147 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002148}
2149
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002150static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
Samuel Ortizd7f33452012-05-29 21:45:21 +02002151#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02002152static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01002153 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002154{
2155 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002156 struct sk_buff *skb;
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002157 int rc, skb_len;
2158 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002159
Samuel Ortizd7f33452012-05-29 21:45:21 +02002160 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002161
2162 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2163
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002164 if (dev->poll_mod_count) {
2165 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002166 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002167 return -EBUSY;
2168 }
2169
2170 if (dev->tgt_active_prot) {
2171 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002172 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002173 return -EBUSY;
2174 }
2175
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002176 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002177 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002178
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002179 /* NFCID3 */
2180 skb_len += NFC_NFCID3_MAXSIZE;
2181 if (target && !target->nfcid2_len) {
2182 nfcid3[0] = 0x1;
2183 nfcid3[1] = 0xfe;
2184 get_random_bytes(nfcid3 + 2, 6);
2185 }
Samuel Ortiz322bce92013-05-27 15:29:11 +02002186
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002187 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002188 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002189 return -ENOMEM;
2190
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002191 *skb_put(skb, 1) = !comm_mode; /* ActPass */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002192 *skb_put(skb, 1) = 0x02; /* 424 kbps */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002193
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002194 next = skb_put(skb, 1); /* Next */
2195 *next = 0;
2196
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002197 /* Copy passive data */
2198 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2199 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02002200
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002201 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2202 if (target && target->nfcid2_len)
Samuel Ortiz322bce92013-05-27 15:29:11 +02002203 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2204 target->nfcid2_len);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02002205 else
2206 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2207 NFC_NFCID3_MAXSIZE);
2208 *next |= 2;
Samuel Ortiz322bce92013-05-27 15:29:11 +02002209
Samuel Ortiz47807d32012-03-05 01:03:50 +01002210 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002211 memcpy(skb_put(skb, gb_len), gb, gb_len);
2212 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002213 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002214 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002215 }
2216
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002217 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2218 if (!arg) {
2219 dev_kfree_skb(skb);
2220 return -ENOMEM;
2221 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002222
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002223 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002224
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002225 pn533_rf_field(dev->nfc_dev, 0);
2226
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01002227 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2228 pn533_in_dep_link_up_complete, arg);
2229
2230 if (rc < 0) {
2231 dev_kfree_skb(skb);
2232 kfree(arg);
2233 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002234
2235 return rc;
2236}
2237
2238static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2239{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002240 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2241
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002242 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2243
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002244 pn533_poll_reset_mod_list(dev);
2245
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02002246 if (dev->tgt_mode || dev->tgt_active_prot)
2247 pn533_abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002248
2249 dev->tgt_active_prot = 0;
2250 dev->tgt_mode = 0;
2251
2252 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002253
2254 return 0;
2255}
2256
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002257struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002258 data_exchange_cb_t cb;
2259 void *cb_context;
2260};
2261
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002262static struct sk_buff *pn533_build_response(struct pn533 *dev)
2263{
2264 struct sk_buff *skb, *tmp, *t;
2265 unsigned int skb_len = 0, tmp_len = 0;
2266
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002267 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002268
2269 if (skb_queue_empty(&dev->resp_q))
2270 return NULL;
2271
2272 if (skb_queue_len(&dev->resp_q) == 1) {
2273 skb = skb_dequeue(&dev->resp_q);
2274 goto out;
2275 }
2276
2277 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2278 skb_len += tmp->len;
2279
2280 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2281 __func__, skb_len);
2282
2283 skb = alloc_skb(skb_len, GFP_KERNEL);
2284 if (skb == NULL)
2285 goto out;
2286
2287 skb_put(skb, skb_len);
2288
2289 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2290 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2291 tmp_len += tmp->len;
2292 }
2293
2294out:
2295 skb_queue_purge(&dev->resp_q);
2296
2297 return skb;
2298}
2299
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002300static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002301 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002302{
2303 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002304 struct sk_buff *skb;
2305 int rc = 0;
2306 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002307
2308 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2309
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002310 if (IS_ERR(resp)) {
2311 rc = PTR_ERR(resp);
2312 goto _error;
2313 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002314
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002315 status = resp->data[0];
2316 ret = status & PN533_CMD_RET_MASK;
2317 mi = status & PN533_CMD_MI_MASK;
2318
2319 skb_pull(resp, sizeof(status));
2320
2321 if (ret != PN533_CMD_RET_SUCCESS) {
2322 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002323 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002324 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002325 goto error;
2326 }
2327
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002328 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002329
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002330 if (mi) {
2331 dev->cmd_complete_mi_arg = arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +00002332 queue_work(dev->wq, &dev->mi_rx_work);
2333 return -EINPROGRESS;
2334 }
2335
2336 /* Prepare for the next round */
2337 if (skb_queue_len(&dev->fragment_skb) > 0) {
2338 dev->cmd_complete_dep_arg = arg;
2339 queue_work(dev->wq, &dev->mi_tx_work);
2340
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002341 return -EINPROGRESS;
2342 }
2343
2344 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002345 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002346 goto error;
2347
2348 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002349 kfree(arg);
2350 return 0;
2351
2352error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002353 dev_kfree_skb(resp);
2354_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002355 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002356 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002357 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002358 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002359}
2360
Olivier Guiter963a82e2013-06-13 13:43:28 +00002361/* Split the Tx skb into small chunks */
2362static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2363{
2364 struct sk_buff *frag;
2365 int frag_size;
2366
2367 do {
2368 /* Remaining size */
2369 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2370 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2371 else
2372 frag_size = skb->len;
2373
2374 /* Allocate and reserve */
2375 frag = pn533_alloc_skb(dev, frag_size);
2376 if (!frag) {
2377 skb_queue_purge(&dev->fragment_skb);
2378 break;
2379 }
2380
2381 /* Reserve the TG/MI byte */
2382 skb_reserve(frag, 1);
2383
2384 /* MI + TG */
2385 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2386 *skb_push(frag, sizeof(u8)) = (PN533_CMD_MI_MASK | 1);
2387 else
2388 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2389
2390 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2391
2392 /* Reduce the size of incoming buffer */
2393 skb_pull(skb, frag_size);
2394
2395 /* Add this to skb_queue */
2396 skb_queue_tail(&dev->fragment_skb, frag);
2397
2398 } while (skb->len > 0);
2399
2400 dev_kfree_skb(skb);
2401
2402 return skb_queue_len(&dev->fragment_skb);
2403}
2404
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002405static int pn533_transceive(struct nfc_dev *nfc_dev,
2406 struct nfc_target *target, struct sk_buff *skb,
2407 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002408{
2409 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002410 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002411 int rc;
2412
2413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2414
2415 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002416 nfc_dev_err(&dev->interface->dev,
2417 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418 rc = -EINVAL;
2419 goto error;
2420 }
2421
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002422 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423 if (!arg) {
2424 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002425 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002426 }
2427
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002428 arg->cb = cb;
2429 arg->cb_context = cb_context;
2430
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002431 switch (dev->device_type) {
2432 case PN533_DEVICE_PASORI:
2433 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2434 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2435 skb,
2436 pn533_data_exchange_complete,
2437 arg);
2438
2439 break;
2440 }
2441 default:
Olivier Guiter963a82e2013-06-13 13:43:28 +00002442 /* jumbo frame ? */
2443 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2444 rc = pn533_fill_fragment_skbs(dev, skb);
2445 if (rc <= 0)
2446 goto error;
2447
2448 skb = skb_dequeue(&dev->fragment_skb);
2449 if (!skb) {
2450 rc = -EIO;
2451 goto error;
2452 }
2453 } else {
2454 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2455 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002456
2457 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2458 skb, pn533_data_exchange_complete,
2459 arg);
2460
2461 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002462 }
2463
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002464 if (rc < 0) /* rc from send_async */
2465 goto error;
2466
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002467 return 0;
2468
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002469error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002470 kfree(arg);
2471 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002472 return rc;
2473}
2474
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002475static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002476 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002477{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002478 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002479
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002480 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2481
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002482 if (IS_ERR(resp))
2483 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002484
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002485 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002486
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002487 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002488
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002489 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002490 nfc_tm_deactivated(dev->nfc_dev);
2491
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002492 dev->tgt_mode = 0;
2493
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002494 return 0;
2495 }
2496
2497 queue_work(dev->wq, &dev->tg_work);
2498
2499 return 0;
2500}
2501
2502static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2503{
2504 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002505 int rc;
2506
2507 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2508
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002509 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002510 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002511 "Data length greater than the max allowed: %d",
2512 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2513 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002514 }
2515
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002516 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2517 pn533_tm_send_complete, NULL);
2518 if (rc < 0)
2519 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002520
2521 return rc;
2522}
2523
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002524static void pn533_wq_mi_recv(struct work_struct *work)
2525{
Olivier Guiter963a82e2013-06-13 13:43:28 +00002526 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002527
2528 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002529 int rc;
2530
2531 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2532
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002533 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002534 if (!skb)
2535 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002536
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002537 switch (dev->device_type) {
2538 case PN533_DEVICE_PASORI:
2539 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2540 rc = pn533_send_cmd_direct_async(dev,
2541 PN533_CMD_IN_COMM_THRU,
2542 skb,
2543 pn533_data_exchange_complete,
2544 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002545
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002546 break;
2547 }
2548 default:
2549 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002550
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002551 rc = pn533_send_cmd_direct_async(dev,
2552 PN533_CMD_IN_DATA_EXCHANGE,
2553 skb,
2554 pn533_data_exchange_complete,
2555 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002556
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002557 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002558 }
2559
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002560 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002561 return;
2562
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002563 nfc_dev_err(&dev->interface->dev,
2564 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002565
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002566 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002567 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002568
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002569error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002570 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002571 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002572}
2573
Olivier Guiter963a82e2013-06-13 13:43:28 +00002574static void pn533_wq_mi_send(struct work_struct *work)
2575{
2576 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2577 struct sk_buff *skb;
2578 int rc;
2579
2580 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2581
2582 /* Grab the first skb in the queue */
2583 skb = skb_dequeue(&dev->fragment_skb);
2584
2585 if (skb == NULL) { /* No more data */
2586 /* Reset the queue for future use */
2587 skb_queue_head_init(&dev->fragment_skb);
2588 goto error;
2589 }
2590
2591 switch (dev->device_type) {
2592 case PN533_DEVICE_PASORI:
2593 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2594 rc = -EIO;
2595 break;
2596 }
2597
2598 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2599 skb,
2600 pn533_data_exchange_complete,
2601 dev->cmd_complete_dep_arg);
2602
2603 break;
2604
2605 default:
2606 /* Still some fragments? */
2607 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2608 skb,
2609 pn533_data_exchange_complete,
2610 dev->cmd_complete_dep_arg);
2611
2612 break;
2613 }
2614
2615 if (rc == 0) /* success */
2616 return;
2617
2618 nfc_dev_err(&dev->interface->dev,
2619 "Error %d when trying to perform data_exchange", rc);
2620
2621 dev_kfree_skb(skb);
2622 kfree(dev->cmd_complete_dep_arg);
2623
2624error:
2625 pn533_send_ack(dev, GFP_KERNEL);
2626 queue_work(dev->wq, &dev->cmd_work);
2627}
2628
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002629static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2630 u8 cfgdata_len)
2631{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002632 struct sk_buff *skb;
2633 struct sk_buff *resp;
2634
2635 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002636
2637 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2638
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002639 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002640
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002641 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002642 if (!skb)
2643 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002644
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002645 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2646 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002647
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002648 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2649 if (IS_ERR(resp))
2650 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002651
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002652 dev_kfree_skb(resp);
2653 return 0;
2654}
2655
2656static int pn533_get_firmware_version(struct pn533 *dev,
2657 struct pn533_fw_version *fv)
2658{
2659 struct sk_buff *skb;
2660 struct sk_buff *resp;
2661
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002662 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002663 if (!skb)
2664 return -ENOMEM;
2665
2666 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2667 if (IS_ERR(resp))
2668 return PTR_ERR(resp);
2669
2670 fv->ic = resp->data[0];
2671 fv->ver = resp->data[1];
2672 fv->rev = resp->data[2];
2673 fv->support = resp->data[3];
2674
2675 dev_kfree_skb(resp);
2676 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002677}
2678
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002679static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002680{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002681 struct sk_buff *skb;
2682 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002683
2684 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2685
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002686 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002687 if (!skb)
2688 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002689
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002690 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002691
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002692 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2693 if (IS_ERR(resp))
2694 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002695
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002696 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002697
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002698 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002699}
2700
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002701struct pn533_acr122_poweron_rdr_arg {
2702 int rc;
2703 struct completion done;
2704};
2705
2706static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2707{
2708 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2709
2710 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2711
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002712 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002713 urb->transfer_buffer, urb->transfer_buffer_length,
2714 false);
2715
2716 arg->rc = urb->status;
2717 complete(&arg->done);
2718}
2719
2720static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2721{
2722 /* Power on th reader (CCID cmd) */
2723 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2724 0, 0, 0, 0, 0, 0, 3, 0, 0};
2725 u8 buf[255];
2726 int rc;
2727 void *cntx;
2728 struct pn533_acr122_poweron_rdr_arg arg;
2729
2730 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2731
2732 init_completion(&arg.done);
2733 cntx = dev->in_urb->context; /* backup context */
2734
2735 dev->in_urb->transfer_buffer = buf;
2736 dev->in_urb->transfer_buffer_length = 255;
2737 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2738 dev->in_urb->context = &arg;
2739
2740 dev->out_urb->transfer_buffer = cmd;
2741 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2742
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002743 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002744 cmd, sizeof(cmd), false);
2745
2746 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2747 if (rc) {
2748 nfc_dev_err(&dev->interface->dev,
2749 "Reader power on cmd error %d", rc);
2750 return rc;
2751 }
2752
2753 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2754 if (rc) {
2755 nfc_dev_err(&dev->interface->dev,
2756 "Can't submit for reader power on cmd response %d",
2757 rc);
2758 return rc;
2759 }
2760
2761 wait_for_completion(&arg.done);
2762 dev->in_urb->context = cntx; /* restore context */
2763
2764 return arg.rc;
2765}
2766
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002767static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2768{
2769 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2770 u8 rf_field = !!rf;
2771 int rc;
2772
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02002773 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2774
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002775 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2776 (u8 *)&rf_field, 1);
2777 if (rc) {
2778 nfc_dev_err(&dev->interface->dev,
2779 "Error on setting RF field");
2780 return rc;
2781 }
2782
2783 return rc;
2784}
2785
2786int pn533_dev_up(struct nfc_dev *nfc_dev)
2787{
2788 return pn533_rf_field(nfc_dev, 1);
2789}
2790
2791int pn533_dev_down(struct nfc_dev *nfc_dev)
2792{
2793 return pn533_rf_field(nfc_dev, 0);
2794}
2795
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002796static struct nfc_ops pn533_nfc_ops = {
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002797 .dev_up = pn533_dev_up,
2798 .dev_down = pn533_dev_down,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002799 .dep_link_up = pn533_dep_link_up,
2800 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002801 .start_poll = pn533_start_poll,
2802 .stop_poll = pn533_stop_poll,
2803 .activate_target = pn533_activate_target,
2804 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002805 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002806 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002807};
2808
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002809static int pn533_setup(struct pn533 *dev)
2810{
2811 struct pn533_config_max_retries max_retries;
2812 struct pn533_config_timing timing;
2813 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2814 int rc;
2815
2816 switch (dev->device_type) {
2817 case PN533_DEVICE_STD:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002818 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002819 case PN533_DEVICE_ACR122U:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002820 max_retries.mx_rty_atr = 0x2;
2821 max_retries.mx_rty_psl = 0x1;
2822 max_retries.mx_rty_passive_act =
2823 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2824
2825 timing.rfu = PN533_CONFIG_TIMING_102;
2826 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2827 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2828
2829 break;
2830
2831 default:
2832 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2833 dev->device_type);
2834 return -EINVAL;
2835 }
2836
2837 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2838 (u8 *)&max_retries, sizeof(max_retries));
2839 if (rc) {
2840 nfc_dev_err(&dev->interface->dev,
2841 "Error on setting MAX_RETRIES config");
2842 return rc;
2843 }
2844
2845
2846 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2847 (u8 *)&timing, sizeof(timing));
2848 if (rc) {
2849 nfc_dev_err(&dev->interface->dev,
2850 "Error on setting RF timings");
2851 return rc;
2852 }
2853
2854 switch (dev->device_type) {
2855 case PN533_DEVICE_STD:
2856 break;
2857
2858 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002859 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002860
2861 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2862 pasori_cfg, 3);
2863 if (rc) {
2864 nfc_dev_err(&dev->interface->dev,
2865 "Error while settings PASORI config");
2866 return rc;
2867 }
2868
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002869 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002870
2871 break;
2872 }
2873
2874 return 0;
2875}
2876
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002877static int pn533_probe(struct usb_interface *interface,
2878 const struct usb_device_id *id)
2879{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002880 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002881 struct pn533 *dev;
2882 struct usb_host_interface *iface_desc;
2883 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002884 int in_endpoint = 0;
2885 int out_endpoint = 0;
2886 int rc = -ENOMEM;
2887 int i;
2888 u32 protocols;
2889
2890 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2891 if (!dev)
2892 return -ENOMEM;
2893
2894 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2895 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002896 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002897
2898 iface_desc = interface->cur_altsetting;
2899 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2900 endpoint = &iface_desc->endpoint[i].desc;
2901
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002902 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002903 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002904
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002905 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002906 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002907 }
2908
2909 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002910 nfc_dev_err(&interface->dev,
2911 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002912 rc = -ENODEV;
2913 goto error;
2914 }
2915
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002916 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002917 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2918
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002919 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002920 goto error;
2921
2922 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002923 usb_rcvbulkpipe(dev->udev, in_endpoint),
2924 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002925 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002926 usb_sndbulkpipe(dev->udev, out_endpoint),
2927 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002928
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002929 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2930 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002931 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
2932 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002933 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002934 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02002935 INIT_WORK(&dev->rf_work, pn533_wq_rf);
Tejun Heo58637c92012-08-22 16:28:46 -07002936 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002937 if (dev->wq == NULL)
2938 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002939
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002940 init_timer(&dev->listen_timer);
2941 dev->listen_timer.data = (unsigned long) dev;
2942 dev->listen_timer.function = pn533_listen_mode_timer;
2943
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002944 skb_queue_head_init(&dev->resp_q);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002945 skb_queue_head_init(&dev->fragment_skb);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002946
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002947 INIT_LIST_HEAD(&dev->cmd_queue);
2948
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002949 usb_set_intfdata(interface, dev);
2950
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002951 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002952
Waldemar Rymarkiewicz58520372013-04-03 08:02:12 +02002953 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002954 dev->device_type = id->driver_info;
2955 switch (dev->device_type) {
2956 case PN533_DEVICE_STD:
2957 protocols = PN533_ALL_PROTOCOLS;
2958 break;
2959
2960 case PN533_DEVICE_PASORI:
2961 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2962 break;
2963
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002964 case PN533_DEVICE_ACR122U:
2965 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2966 dev->ops = &pn533_acr122_frame_ops;
2967 dev->protocol_type = PN533_PROTO_REQ_RESP,
2968
2969 rc = pn533_acr122_poweron_rdr(dev);
2970 if (rc < 0) {
2971 nfc_dev_err(&dev->interface->dev,
2972 "Couldn't poweron the reader (error %d)",
2973 rc);
2974 goto destroy_wq;
2975 }
2976 break;
2977
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002978 default:
2979 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2980 dev->device_type);
2981 rc = -EINVAL;
2982 goto destroy_wq;
2983 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002984
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002985 memset(&fw_ver, 0, sizeof(fw_ver));
2986 rc = pn533_get_firmware_version(dev, &fw_ver);
2987 if (rc < 0)
2988 goto destroy_wq;
2989
2990 nfc_dev_info(&dev->interface->dev,
Olivier Guiter86eca4e2013-06-03 12:02:29 +02002991 "NXP PN5%02X firmware ver %d.%d now attached",
2992 fw_ver.ic, fw_ver.ver, fw_ver.rev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002993
2994
Samuel Ortize8753042011-08-19 15:47:11 +02002995 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002996 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002997 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002998 dev->ops->tx_tail_len);
Wei Yongjun4674d0f2013-05-07 20:07:52 +08002999 if (!dev->nfc_dev) {
3000 rc = -ENOMEM;
Samuel Ortiz4849f852012-04-10 19:43:17 +02003001 goto destroy_wq;
Wei Yongjun4674d0f2013-05-07 20:07:52 +08003002 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003003
3004 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3005 nfc_set_drvdata(dev->nfc_dev, dev);
3006
3007 rc = nfc_register_device(dev->nfc_dev);
3008 if (rc)
3009 goto free_nfc_dev;
3010
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02003011 rc = pn533_setup(dev);
3012 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003013 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02003014
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003015 return 0;
3016
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003017unregister_nfc_dev:
3018 nfc_unregister_device(dev->nfc_dev);
3019
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003020free_nfc_dev:
3021 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02003022
Samuel Ortiz4849f852012-04-10 19:43:17 +02003023destroy_wq:
3024 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003025error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003026 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003027 usb_free_urb(dev->out_urb);
Marina Makienko7c5a54f2013-02-26 11:41:18 +04003028 usb_put_dev(dev->udev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003029 kfree(dev);
3030 return rc;
3031}
3032
3033static void pn533_disconnect(struct usb_interface *interface)
3034{
3035 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003036 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003037
3038 dev = usb_get_intfdata(interface);
3039 usb_set_intfdata(interface, NULL);
3040
3041 nfc_unregister_device(dev->nfc_dev);
3042 nfc_free_device(dev->nfc_dev);
3043
3044 usb_kill_urb(dev->in_urb);
3045 usb_kill_urb(dev->out_urb);
3046
Samuel Ortiz4849f852012-04-10 19:43:17 +02003047 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003048
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02003049 skb_queue_purge(&dev->resp_q);
3050
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02003051 del_timer(&dev->listen_timer);
3052
Samuel Ortiz5d50b362012-08-17 23:47:54 +02003053 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3054 list_del(&cmd->queue);
3055 kfree(cmd);
3056 }
3057
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003058 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003059 usb_free_urb(dev->out_urb);
3060 kfree(dev);
3061
Dan Carpenter276556d2011-07-08 10:21:15 +03003062 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003063}
3064
3065static struct usb_driver pn533_driver = {
3066 .name = "pn533",
3067 .probe = pn533_probe,
3068 .disconnect = pn533_disconnect,
3069 .id_table = pn533_table,
3070};
3071
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08003072module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003073
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003074MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3075MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3076MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03003077MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3078MODULE_VERSION(VERSION);
3079MODULE_LICENSE("GPL");