blob: c20778a56f4f6441bf3863c4d16ca2c3a5301fe4 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 *
4 * Authors:
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <linux/device.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/usb.h>
29#include <linux/nfc.h>
30#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030031#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030032
33#define VERSION "0.1"
34
35#define PN533_VENDOR_ID 0x4CC
36#define PN533_PRODUCT_ID 0x2533
37
38#define SCM_VENDOR_ID 0x4E6
39#define SCL3711_PRODUCT_ID 0x5591
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1
43
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020044#define PN533_DEVICE_STD 0x1
45#define PN533_DEVICE_PASORI 0x2
46
Samuel Ortiz01d719a2012-07-04 00:14:04 +020047#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020051
52#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020055 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 NFC_PROTO_NFC_DEP_MASK)
57
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030058static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020059 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
63 },
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
68 },
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
73 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030074 { }
75};
76MODULE_DEVICE_TABLE(usb, pn533_table);
77
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020078/* How much time we spend listening for initiators */
79#define PN533_LISTEN_TIME 2
80
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020081/* Standard pn533 frame definitions */
82#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010083 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020084#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020085
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010086/*
87 * Max extended frame payload len, excluding TFI and CC
88 * which are already in PN533_FRAME_HEADER_LEN.
89 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020090#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010091
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020092#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010093 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020094#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
95#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030096
97/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020098#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030099
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200100/* standard frame identifier: in/out/error */
101#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
102#define PN533_STD_FRAME_DIR_OUT 0xD4
103#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104
105/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200106#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300107
108#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
109#define PN533_CMD_RF_CONFIGURATION 0x32
110#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200111#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300112#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
113#define PN533_CMD_IN_ATR 0x50
114#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100115#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300116
Samuel Ortizad3823c2012-05-30 23:54:55 +0200117#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200118#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200119#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100120#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300122#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
123
124/* PN533 Return codes */
125#define PN533_CMD_RET_MASK 0x3F
126#define PN533_CMD_MI_MASK 0x40
127#define PN533_CMD_RET_SUCCESS 0x00
128
129struct pn533;
130
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100131typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300132
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100133typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
134 struct sk_buff *resp);
135
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300136/* structs for pn533 commands */
137
138/* PN533_CMD_GET_FIRMWARE_VERSION */
139struct pn533_fw_version {
140 u8 ic;
141 u8 ver;
142 u8 rev;
143 u8 support;
144};
145
146/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200147#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300148#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200149#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300150
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200151#define PN533_CONFIG_TIMING_102 0xb
152#define PN533_CONFIG_TIMING_204 0xc
153#define PN533_CONFIG_TIMING_409 0xd
154#define PN533_CONFIG_TIMING_819 0xe
155
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300156#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
157#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
158
159struct pn533_config_max_retries {
160 u8 mx_rty_atr;
161 u8 mx_rty_psl;
162 u8 mx_rty_passive_act;
163} __packed;
164
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200165struct pn533_config_timing {
166 u8 rfu;
167 u8 atr_res_timeout;
168 u8 dep_timeout;
169} __packed;
170
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300171/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
172
173/* felica commands opcode */
174#define PN533_FELICA_OPC_SENSF_REQ 0
175#define PN533_FELICA_OPC_SENSF_RES 1
176/* felica SENSF_REQ parameters */
177#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
178#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
179#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
180#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
181
182/* type B initiator_data values */
183#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
184#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
185#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
186
187union pn533_cmd_poll_initdata {
188 struct {
189 u8 afi;
190 u8 polling_method;
191 } __packed type_b;
192 struct {
193 u8 opcode;
194 __be16 sc;
195 u8 rc;
196 u8 tsn;
197 } __packed felica;
198};
199
200/* Poll modulations */
201enum {
202 PN533_POLL_MOD_106KBPS_A,
203 PN533_POLL_MOD_212KBPS_FELICA,
204 PN533_POLL_MOD_424KBPS_FELICA,
205 PN533_POLL_MOD_106KBPS_JEWEL,
206 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200207 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300208
209 __PN533_POLL_MOD_AFTER_LAST,
210};
211#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
212
213struct pn533_poll_modulations {
214 struct {
215 u8 maxtg;
216 u8 brty;
217 union pn533_cmd_poll_initdata initiator_data;
218 } __packed data;
219 u8 len;
220};
221
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100222static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300223 [PN533_POLL_MOD_106KBPS_A] = {
224 .data = {
225 .maxtg = 1,
226 .brty = 0,
227 },
228 .len = 2,
229 },
230 [PN533_POLL_MOD_212KBPS_FELICA] = {
231 .data = {
232 .maxtg = 1,
233 .brty = 1,
234 .initiator_data.felica = {
235 .opcode = PN533_FELICA_OPC_SENSF_REQ,
236 .sc = PN533_FELICA_SENSF_SC_ALL,
237 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
238 .tsn = 0,
239 },
240 },
241 .len = 7,
242 },
243 [PN533_POLL_MOD_424KBPS_FELICA] = {
244 .data = {
245 .maxtg = 1,
246 .brty = 2,
247 .initiator_data.felica = {
248 .opcode = PN533_FELICA_OPC_SENSF_REQ,
249 .sc = PN533_FELICA_SENSF_SC_ALL,
250 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
251 .tsn = 0,
252 },
253 },
254 .len = 7,
255 },
256 [PN533_POLL_MOD_106KBPS_JEWEL] = {
257 .data = {
258 .maxtg = 1,
259 .brty = 4,
260 },
261 .len = 2,
262 },
263 [PN533_POLL_MOD_847KBPS_B] = {
264 .data = {
265 .maxtg = 1,
266 .brty = 8,
267 .initiator_data.type_b = {
268 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
269 .polling_method =
270 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
271 },
272 },
273 .len = 3,
274 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200275 [PN533_LISTEN_MOD] = {
276 .len = 0,
277 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300278};
279
280/* PN533_CMD_IN_ATR */
281
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300282struct pn533_cmd_activate_response {
283 u8 status;
284 u8 nfcid3t[10];
285 u8 didt;
286 u8 bst;
287 u8 brt;
288 u8 to;
289 u8 ppt;
290 /* optional */
291 u8 gt[];
292} __packed;
293
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100294struct pn533_cmd_jump_dep_response {
295 u8 status;
296 u8 tg;
297 u8 nfcid3t[10];
298 u8 didt;
299 u8 bst;
300 u8 brt;
301 u8 to;
302 u8 ppt;
303 /* optional */
304 u8 gt[];
305} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300306
Samuel Ortizad3823c2012-05-30 23:54:55 +0200307
308/* PN533_TG_INIT_AS_TARGET */
309#define PN533_INIT_TARGET_PASSIVE 0x1
310#define PN533_INIT_TARGET_DEP 0x2
311
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200312#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
313#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
314#define PN533_INIT_TARGET_RESP_DEP 0x4
315
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300316struct pn533 {
317 struct usb_device *udev;
318 struct usb_interface *interface;
319 struct nfc_dev *nfc_dev;
320
321 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300322 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300323
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200324 struct sk_buff_head resp_q;
325
Samuel Ortiz4849f852012-04-10 19:43:17 +0200326 struct workqueue_struct *wq;
327 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200328 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200330 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200331 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200332 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200333 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200334 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300335
336 pn533_cmd_complete_t cmd_complete;
337 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100338 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200339 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300340 u8 cmd;
341
342 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
343 u8 poll_mod_count;
344 u8 poll_mod_curr;
345 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200346 u32 listen_protocols;
347
348 u8 *gb;
349 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300350
351 u8 tgt_available_prots;
352 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200353 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200354
355 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200356
357 struct list_head cmd_queue;
358 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100359
360 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200361};
362
363struct pn533_cmd {
364 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100365 u8 cmd_code;
366 struct sk_buff *req;
367 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100368 int resp_len;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200369 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300370};
371
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200372struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300373 u8 preamble;
374 __be16 start_frame;
375 u8 datalen;
376 u8 datalen_checksum;
377 u8 data[];
378} __packed;
379
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100380struct pn533_frame_ops {
381 void (*tx_frame_init)(void *frame, u8 cmd_code);
382 void (*tx_frame_finish)(void *frame);
383 void (*tx_update_payload_len)(void *frame, int len);
384 int tx_header_len;
385 int tx_tail_len;
386
387 bool (*rx_is_frame_valid)(void *frame);
388 int (*rx_frame_size)(void *frame);
389 int rx_header_len;
390 int rx_tail_len;
391
392 int max_payload_len;
393 u8 (*get_cmd_code)(void *frame);
394};
395
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200397static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300398{
399 return ~value + 1;
400}
401
402/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200403static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300404{
405 u8 sum = 0;
406 int i;
407
408 for (i = 0; i < datalen; i++)
409 sum += data[i];
410
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200411 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300412}
413
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200414static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300415{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200416 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100417
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200419 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
420 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
421 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300422 frame->datalen = 2;
423}
424
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200425static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300426{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200427 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100428
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200429 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300430
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200431 PN533_STD_FRAME_CHECKSUM(frame) =
432 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300433
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200434 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300435}
436
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200437static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100438{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200439 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100440
441 frame->datalen += len;
442}
443
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200444static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300445{
446 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200447 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200449 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300450 return false;
451
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200452 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300453 if (checksum != frame->datalen_checksum)
454 return false;
455
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200456 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
457 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300458 return false;
459
460 return true;
461}
462
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200463static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300464{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200465 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300466 return false;
467
468 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
469 return false;
470
471 return true;
472}
473
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200474static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300475{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200476 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100477
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200478 return sizeof(struct pn533_std_frame) + f->datalen +
479 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100480}
481
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200482static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100483{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200484 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100485
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200486 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100487}
488
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100489static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200490 .tx_frame_init = pn533_std_tx_frame_init,
491 .tx_frame_finish = pn533_std_tx_frame_finish,
492 .tx_update_payload_len = pn533_std_tx_update_payload_len,
493 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
494 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100495
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200496 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
497 .rx_frame_size = pn533_std_rx_frame_size,
498 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
499 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100500
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200501 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
502 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100503};
504
505static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
506{
507 return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300508}
509
Samuel Ortiz4849f852012-04-10 19:43:17 +0200510
511static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300512{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200513 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 int rc;
515
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100516 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300517 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200518 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300519}
520
521static void pn533_recv_response(struct urb *urb)
522{
523 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100524 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300525
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300526 switch (urb->status) {
527 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100528 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300529 case -ECONNRESET:
530 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100531 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100532 "The urb has been canceled (status %d)",
533 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200534 dev->wq_in_error = urb->status;
535 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100536 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100538 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100539 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200540 dev->wq_in_error = urb->status;
541 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300542 }
543
544 in_frame = dev->in_urb->transfer_buffer;
545
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100546 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100547 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
548 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100549
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100550 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300551 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200552 dev->wq_in_error = -EIO;
553 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300554 }
555
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100556 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100557 nfc_dev_err(&dev->interface->dev,
558 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200559 dev->wq_in_error = -EIO;
560 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300561 }
562
Samuel Ortiz4849f852012-04-10 19:43:17 +0200563 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300564
Samuel Ortiz4849f852012-04-10 19:43:17 +0200565sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200566 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567}
568
569static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
570{
571 dev->in_urb->complete = pn533_recv_response;
572
573 return usb_submit_urb(dev->in_urb, flags);
574}
575
576static void pn533_recv_ack(struct urb *urb)
577{
578 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200579 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300580 int rc;
581
582 switch (urb->status) {
583 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100584 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300585 case -ECONNRESET:
586 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100587 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100588 "The urb has been stopped (status %d)",
589 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200590 dev->wq_in_error = urb->status;
591 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100592 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300593 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100594 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100595 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200596 dev->wq_in_error = urb->status;
597 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300598 }
599
600 in_frame = dev->in_urb->transfer_buffer;
601
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200602 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300603 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200604 dev->wq_in_error = -EIO;
605 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606 }
607
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300608 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
609 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100610 nfc_dev_err(&dev->interface->dev,
611 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200612 dev->wq_in_error = rc;
613 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300614 }
615
616 return;
617
Samuel Ortiz4849f852012-04-10 19:43:17 +0200618sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200619 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300620}
621
622static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
623{
624 dev->in_urb->complete = pn533_recv_ack;
625
626 return usb_submit_urb(dev->in_urb, flags);
627}
628
629static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
630{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200631 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100632 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300633 int rc;
634
635 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
636
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100637 dev->out_urb->transfer_buffer = ack;
638 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300639 rc = usb_submit_urb(dev->out_urb, flags);
640
641 return rc;
642}
643
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100644static int __pn533_send_frame_async(struct pn533 *dev,
645 struct sk_buff *out,
646 struct sk_buff *in,
647 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300648 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100649 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300650{
651 int rc;
652
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100653 dev->cmd = dev->ops->get_cmd_code(out->data);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300654 dev->cmd_complete = cmd_complete;
655 dev->cmd_complete_arg = arg;
656
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100657 dev->out_urb->transfer_buffer = out->data;
658 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300659
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100660 dev->in_urb->transfer_buffer = in->data;
661 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300662
Samuel Ortize279f842013-03-20 11:27:57 +0100663 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
664 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100665
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100666 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300667 if (rc)
668 return rc;
669
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100670 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300671 if (rc)
672 goto error;
673
674 return 0;
675
676error:
677 usb_unlink_urb(dev->out_urb);
678 return rc;
679}
680
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100681static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
682 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100683{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684 /* payload is already there, just update datalen */
685 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100686 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100687
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100688
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100689 skb_push(skb, ops->tx_header_len);
690 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100691
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100692 ops->tx_frame_init(skb->data, cmd_code);
693 ops->tx_update_payload_len(skb->data, payload_len);
694 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100695}
696
697struct pn533_send_async_complete_arg {
698 pn533_send_async_complete_t complete_cb;
699 void *complete_cb_context;
700 struct sk_buff *resp;
701 struct sk_buff *req;
702};
703
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100704static int pn533_send_async_complete(struct pn533 *dev, void *_arg, int status)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100705{
706 struct pn533_send_async_complete_arg *arg = _arg;
707
708 struct sk_buff *req = arg->req;
709 struct sk_buff *resp = arg->resp;
710
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100711 int rc;
712
713 dev_kfree_skb(req);
714
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100715 if (status < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100716 arg->complete_cb(dev, arg->complete_cb_context,
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100717 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100718 dev_kfree_skb(resp);
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100719 kfree(arg);
720 return status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100721 }
722
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100723 skb_put(resp, dev->ops->rx_frame_size(resp->data));
724 skb_pull(resp, dev->ops->rx_header_len);
725 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100726
727 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
728
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100729 kfree(arg);
730 return rc;
731}
732
733static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
734 struct sk_buff *req, struct sk_buff *resp,
735 int resp_len,
736 pn533_send_async_complete_t complete_cb,
737 void *complete_cb_context)
738{
739 struct pn533_cmd *cmd;
740 struct pn533_send_async_complete_arg *arg;
741 int rc = 0;
742
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100743 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100744
Thierry Escande858ce022013-01-12 19:37:12 +0100745 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100746 if (!arg)
747 return -ENOMEM;
748
749 arg->complete_cb = complete_cb;
750 arg->complete_cb_context = complete_cb_context;
751 arg->resp = resp;
752 arg->req = req;
753
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100754 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100755
756 mutex_lock(&dev->cmd_lock);
757
758 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100759 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
760 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100761 if (rc)
762 goto error;
763
764 dev->cmd_pending = 1;
765 goto unlock;
766 }
767
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100768 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
769 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100770
771 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
772 if (!cmd) {
773 rc = -ENOMEM;
774 goto error;
775 }
776
777 INIT_LIST_HEAD(&cmd->queue);
778 cmd->cmd_code = cmd_code;
779 cmd->req = req;
780 cmd->resp = resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100781 cmd->resp_len = resp_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100782 cmd->arg = arg;
783
784 list_add_tail(&cmd->queue, &dev->cmd_queue);
785
786 goto unlock;
787
788error:
789 kfree(arg);
790unlock:
791 mutex_unlock(&dev->cmd_lock);
792 return rc;
793}
794
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100795static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
796 struct sk_buff *req,
797 pn533_send_async_complete_t complete_cb,
798 void *complete_cb_context)
799{
800 struct sk_buff *resp;
801 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100802 int resp_len = dev->ops->rx_header_len +
803 dev->ops->max_payload_len +
804 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100805
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100806 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
807 if (!resp)
808 return -ENOMEM;
809
810 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
811 complete_cb_context);
812 if (rc)
813 dev_kfree_skb(resp);
814
815 return rc;
816}
817
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100818static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
819 struct sk_buff *req,
820 pn533_send_async_complete_t complete_cb,
821 void *complete_cb_context)
822{
823 struct sk_buff *resp;
824 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100825 int resp_len = dev->ops->rx_header_len +
826 dev->ops->max_payload_len +
827 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100828
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100829 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100830 if (!resp)
831 return -ENOMEM;
832
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100833 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
834 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100835 if (rc)
836 dev_kfree_skb(resp);
837
838 return rc;
839}
840
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100841/*
842 * pn533_send_cmd_direct_async
843 *
844 * The function sends a piority cmd directly to the chip omiting the cmd
845 * queue. It's intended to be used by chaining mechanism of received responses
846 * where the host has to request every single chunk of data before scheduling
847 * next cmd from the queue.
848 */
849static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
850 struct sk_buff *req,
851 pn533_send_async_complete_t complete_cb,
852 void *complete_cb_context)
853{
854 struct pn533_send_async_complete_arg *arg;
855 struct sk_buff *resp;
856 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100857 int resp_len = dev->ops->rx_header_len +
858 dev->ops->max_payload_len +
859 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100860
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100861 resp = alloc_skb(resp_len, GFP_KERNEL);
862 if (!resp)
863 return -ENOMEM;
864
Thierry Escande858ce022013-01-12 19:37:12 +0100865 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100866 if (!arg) {
867 dev_kfree_skb(resp);
868 return -ENOMEM;
869 }
870
871 arg->complete_cb = complete_cb;
872 arg->complete_cb_context = complete_cb_context;
873 arg->resp = resp;
874 arg->req = req;
875
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100876 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100877
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100878 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
879 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100880 if (rc < 0) {
881 dev_kfree_skb(resp);
882 kfree(arg);
883 }
884
885 return rc;
886}
887
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200888static void pn533_wq_cmd(struct work_struct *work)
889{
890 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
891 struct pn533_cmd *cmd;
892
893 mutex_lock(&dev->cmd_lock);
894
895 if (list_empty(&dev->cmd_queue)) {
896 dev->cmd_pending = 0;
897 mutex_unlock(&dev->cmd_lock);
898 return;
899 }
900
901 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
902
Szymon Janc60ad07a2012-10-25 17:29:45 +0200903 list_del(&cmd->queue);
904
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200905 mutex_unlock(&dev->cmd_lock);
906
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100907 __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
908 pn533_send_async_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200909
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200910 kfree(cmd);
911}
912
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300913struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100914 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300915 struct completion done;
916};
917
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100918static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
919 struct sk_buff *resp)
920{
921 struct pn533_sync_cmd_response *arg = _arg;
922
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100923 arg->resp = resp;
924 complete(&arg->done);
925
926 return 0;
927}
928
929/* pn533_send_cmd_sync
930 *
931 * Please note the req parameter is freed inside the function to
932 * limit a number of return value interpretations by the caller.
933 *
934 * 1. negative in case of error during TX path -> req should be freed
935 *
936 * 2. negative in case of error during RX path -> req should not be freed
937 * as it's been already freed at the begining of RX path by
938 * async_complete_cb.
939 *
940 * 3. valid pointer in case of succesfult RX path
941 *
942 * A caller has to check a return value with IS_ERR macro. If the test pass,
943 * the returned pointer is valid.
944 *
945 * */
946static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
947 struct sk_buff *req)
948{
949 int rc;
950 struct pn533_sync_cmd_response arg;
951
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100952 init_completion(&arg.done);
953
954 rc = pn533_send_cmd_async(dev, cmd_code, req,
955 pn533_send_sync_complete, &arg);
956 if (rc) {
957 dev_kfree_skb(req);
958 return ERR_PTR(rc);
959 }
960
961 wait_for_completion(&arg.done);
962
963 return arg.resp;
964}
965
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300966static void pn533_send_complete(struct urb *urb)
967{
968 struct pn533 *dev = urb->context;
969
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300970 switch (urb->status) {
971 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100972 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300973 case -ECONNRESET:
974 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100975 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100976 "The urb has been stopped (status %d)",
977 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300978 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100979 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300980 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100981 nfc_dev_err(&dev->interface->dev,
982 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300983 }
984}
985
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100986static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100987{
988 struct sk_buff *skb;
989
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100990 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100991 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100992 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100993
994 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100995 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100996
997 return skb;
998}
999
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001000struct pn533_target_type_a {
1001 __be16 sens_res;
1002 u8 sel_res;
1003 u8 nfcid_len;
1004 u8 nfcid_data[];
1005} __packed;
1006
1007
1008#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1009#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1010#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1011
1012#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1013#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1014
1015#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1016#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1017
1018#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1019#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1020#define PN533_TYPE_A_SEL_PROT_DEP 2
1021#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1022
1023static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1024 int target_data_len)
1025{
1026 u8 ssd;
1027 u8 platconf;
1028
1029 if (target_data_len < sizeof(struct pn533_target_type_a))
1030 return false;
1031
1032 /* The lenght check of nfcid[] and ats[] are not being performed because
1033 the values are not being used */
1034
1035 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1036 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1037 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1038
1039 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001040 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1041 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1042 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001043 return false;
1044
1045 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1046 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1047 return false;
1048
1049 return true;
1050}
1051
1052static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1053 int tgt_data_len)
1054{
1055 struct pn533_target_type_a *tgt_type_a;
1056
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001057 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001058
1059 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1060 return -EPROTO;
1061
1062 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1063 case PN533_TYPE_A_SEL_PROT_MIFARE:
1064 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1065 break;
1066 case PN533_TYPE_A_SEL_PROT_ISO14443:
1067 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1068 break;
1069 case PN533_TYPE_A_SEL_PROT_DEP:
1070 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1071 break;
1072 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1073 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1074 NFC_PROTO_NFC_DEP_MASK;
1075 break;
1076 }
1077
1078 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1079 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001080 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1081 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001082
1083 return 0;
1084}
1085
1086struct pn533_target_felica {
1087 u8 pol_res;
1088 u8 opcode;
1089 u8 nfcid2[8];
1090 u8 pad[8];
1091 /* optional */
1092 u8 syst_code[];
1093} __packed;
1094
1095#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1096#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1097
1098static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1099 int target_data_len)
1100{
1101 if (target_data_len < sizeof(struct pn533_target_felica))
1102 return false;
1103
1104 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1105 return false;
1106
1107 return true;
1108}
1109
1110static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1111 int tgt_data_len)
1112{
1113 struct pn533_target_felica *tgt_felica;
1114
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001115 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001116
1117 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1118 return -EPROTO;
1119
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001120 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1121 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001122 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1123 else
1124 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1125
Samuel Ortiz79757542012-03-05 01:03:45 +01001126 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1127 nfc_tgt->sensf_res_len = 9;
1128
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001129 return 0;
1130}
1131
1132struct pn533_target_jewel {
1133 __be16 sens_res;
1134 u8 jewelid[4];
1135} __packed;
1136
1137static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1138 int target_data_len)
1139{
1140 u8 ssd;
1141 u8 platconf;
1142
1143 if (target_data_len < sizeof(struct pn533_target_jewel))
1144 return false;
1145
1146 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1147 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1148 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1149
1150 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001151 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1152 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1153 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001154 return false;
1155
1156 return true;
1157}
1158
1159static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1160 int tgt_data_len)
1161{
1162 struct pn533_target_jewel *tgt_jewel;
1163
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001164 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001165
1166 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1167 return -EPROTO;
1168
1169 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1170 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001171 nfc_tgt->nfcid1_len = 4;
1172 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001173
1174 return 0;
1175}
1176
1177struct pn533_type_b_prot_info {
1178 u8 bitrate;
1179 u8 fsci_type;
1180 u8 fwi_adc_fo;
1181} __packed;
1182
1183#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1184#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1185#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1186
1187struct pn533_type_b_sens_res {
1188 u8 opcode;
1189 u8 nfcid[4];
1190 u8 appdata[4];
1191 struct pn533_type_b_prot_info prot_info;
1192} __packed;
1193
1194#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1195
1196struct pn533_target_type_b {
1197 struct pn533_type_b_sens_res sensb_res;
1198 u8 attrib_res_len;
1199 u8 attrib_res[];
1200} __packed;
1201
1202static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1203 int target_data_len)
1204{
1205 if (target_data_len < sizeof(struct pn533_target_type_b))
1206 return false;
1207
1208 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1209 return false;
1210
1211 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1212 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1213 return false;
1214
1215 return true;
1216}
1217
1218static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1219 int tgt_data_len)
1220{
1221 struct pn533_target_type_b *tgt_type_b;
1222
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001223 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001224
1225 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1226 return -EPROTO;
1227
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001228 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229
1230 return 0;
1231}
1232
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001233static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1234 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001235{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236 struct nfc_target nfc_tgt;
1237 int rc;
1238
1239 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001240 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001241
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001242 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243 return -EPROTO;
1244
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001245 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1246
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001247 switch (dev->poll_mod_curr) {
1248 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001249 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001250 break;
1251 case PN533_POLL_MOD_212KBPS_FELICA:
1252 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001253 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001254 break;
1255 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257 break;
1258 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001259 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001260 break;
1261 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001262 nfc_dev_err(&dev->interface->dev,
1263 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001264 return -EPROTO;
1265 }
1266
1267 if (rc)
1268 return rc;
1269
1270 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001271 nfc_dev_dbg(&dev->interface->dev,
1272 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001273 return -EAGAIN;
1274 }
1275
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001276 nfc_dev_dbg(&dev->interface->dev,
1277 "Target found - supported protocols: 0x%x",
1278 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001279
1280 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1281
1282 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1283
1284 return 0;
1285}
1286
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001287static inline void pn533_poll_next_mod(struct pn533 *dev)
1288{
1289 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1290}
1291
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001292static void pn533_poll_reset_mod_list(struct pn533 *dev)
1293{
1294 dev->poll_mod_count = 0;
1295}
1296
1297static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1298{
1299 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001300 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001301 dev->poll_mod_count++;
1302}
1303
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001304static void pn533_poll_create_mod_list(struct pn533 *dev,
1305 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306{
1307 pn533_poll_reset_mod_list(dev);
1308
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001309 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1310 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1311 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001312 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1313
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001314 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1315 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001316 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1317 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1318 }
1319
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001320 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001321 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1322
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001323 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001324 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001326 if (tm_protocols)
1327 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328}
1329
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001330static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001332 u8 nbtg, tg, *tgdata;
1333 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001334
1335 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1336
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001337 nbtg = resp->data[0];
1338 tg = resp->data[1];
1339 tgdata = &resp->data[2];
1340 tgdata_len = resp->len - 2; /* nbtg + tg */
1341
1342 if (nbtg) {
1343 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001344
1345 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001346 if (rc == 0) {
1347 pn533_poll_reset_mod_list(dev);
1348 return 0;
1349 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001350 }
1351
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001352 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001353}
1354
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001355static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001356{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001357 struct sk_buff *skb;
1358 u8 *felica, *nfcid3, *gb;
1359
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001360 u8 *gbytes = dev->gb;
1361 size_t gbytes_len = dev->gb_len;
1362
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001363 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1364 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1366 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001367
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001368 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1369 0x0, 0x0, 0x0,
1370 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001371
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001372 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1373 felica (18), nfcid3 (10), gb_len (1) */
1374 gbytes_len +
1375 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001376
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001377 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001378 if (!skb)
1379 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001380
1381 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001382 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001383
1384 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001385 memcpy(skb_put(skb, 6), mifare_params, 6);
1386
1387 /* Felica params */
1388 felica = skb_put(skb, 18);
1389 memcpy(felica, felica_params, 18);
1390 get_random_bytes(felica + 2, 6);
1391
1392 /* NFCID3 */
1393 nfcid3 = skb_put(skb, 10);
1394 memset(nfcid3, 0, 10);
1395 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001396
1397 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001398 *skb_put(skb, 1) = gbytes_len;
1399
1400 gb = skb_put(skb, gbytes_len);
1401 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001402
Samuel Ortizad3823c2012-05-30 23:54:55 +02001403 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001404 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001405
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001406 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001407}
1408
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001409#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001410#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1411static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001412 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001413{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001414 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001415
1416 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1417
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001418 if (IS_ERR(resp))
1419 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001420
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001421 status = resp->data[0];
1422 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001423
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001424 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001425 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001426 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001427 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001428 return 0;
1429 }
1430
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001431 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001432}
1433
1434static void pn533_wq_tg_get_data(struct work_struct *work)
1435{
1436 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001437
1438 struct sk_buff *skb;
1439 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001440
1441 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1442
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001443 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001444 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001445 return;
1446
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001447 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1448 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001449
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001450 if (rc < 0)
1451 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001452
1453 return;
1454}
1455
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001456#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001457static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001458{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001459 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001460 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001461 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001462
1463 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1464
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001465 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001466 return -EINVAL;
1467
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001468 mode = resp->data[0];
1469 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001470
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1472 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001473
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1475 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001476 comm_mode = NFC_COMM_ACTIVE;
1477
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001478 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001479 return -EOPNOTSUPP;
1480
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001481 gb = cmd + ATR_REQ_GB_OFFSET;
1482 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001483
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001484 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1485 comm_mode, gb, gb_len);
1486 if (rc < 0) {
1487 nfc_dev_err(&dev->interface->dev,
1488 "Error when signaling target activation");
1489 return rc;
1490 }
1491
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001492 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001493 queue_work(dev->wq, &dev->tg_work);
1494
1495 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001496}
1497
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001498static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001499{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001500 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001501
1502 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1503
1504 /* An ack will cancel the last issued command (poll) */
1505 pn533_send_ack(dev, GFP_ATOMIC);
1506
1507 dev->cancel_listen = 1;
1508
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001509 pn533_poll_next_mod(dev);
1510
1511 queue_work(dev->wq, &dev->poll_work);
1512}
1513
1514static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001515 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001516{
1517 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001518 int rc;
1519
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001520 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1521
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001522 if (IS_ERR(resp)) {
1523 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001524
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001525 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1526 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001527
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001528 if (rc == -ENOENT) {
1529 if (dev->poll_mod_count != 0)
1530 return rc;
1531 else
1532 goto stop_poll;
1533 } else if (rc < 0) {
1534 nfc_dev_err(&dev->interface->dev,
1535 "Error %d when running poll", rc);
1536 goto stop_poll;
1537 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001538 }
1539
1540 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1541
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001542 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001543 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001544 rc = pn533_init_target_complete(dev, resp);
1545 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001546 }
1547
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001548 /* Initiator mode */
1549 rc = pn533_start_poll_complete(dev, resp);
1550 if (!rc)
1551 goto done;
1552
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001553 if (!dev->poll_mod_count) {
1554 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1555 goto done;
1556 }
1557
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001558 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001559 queue_work(dev->wq, &dev->poll_work);
1560
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001561done:
1562 dev_kfree_skb(resp);
1563 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001564
1565stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001566 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1567
Samuel Ortizad3823c2012-05-30 23:54:55 +02001568 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001569 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001570 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001572
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001573static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1574 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001575{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001577
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001578 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579 if (!skb)
1580 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001583
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001584 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001585}
1586
1587static int pn533_send_poll_frame(struct pn533 *dev)
1588{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001589 struct pn533_poll_modulations *mod;
1590 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001591 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001593
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001594 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001595
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001596 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1597 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001598
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001599 if (mod->len == 0) { /* Listen mode */
1600 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001601 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602 } else { /* Polling mode */
1603 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001604 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001605 }
1606
1607 if (!skb) {
1608 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1609 return -ENOMEM;
1610 }
1611
1612 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1613 NULL);
1614 if (rc < 0) {
1615 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001616 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001617 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001618
1619 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001620}
1621
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001622static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001623{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001624 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1625 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001626 int rc;
1627
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001628 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1629
1630 nfc_dev_dbg(&dev->interface->dev,
1631 "%s cancel_listen %d modulation len %d",
1632 __func__, dev->cancel_listen, cur_mod->len);
1633
1634 if (dev->cancel_listen == 1) {
1635 dev->cancel_listen = 0;
1636 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001637 }
1638
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001639 rc = pn533_send_poll_frame(dev);
1640 if (rc)
1641 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001642
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1644 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001645
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001647}
1648
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001649static int pn533_start_poll(struct nfc_dev *nfc_dev,
1650 u32 im_protocols, u32 tm_protocols)
1651{
1652 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1653
1654 nfc_dev_dbg(&dev->interface->dev,
1655 "%s: im protocols 0x%x tm protocols 0x%x",
1656 __func__, im_protocols, tm_protocols);
1657
1658 if (dev->tgt_active_prot) {
1659 nfc_dev_err(&dev->interface->dev,
1660 "Cannot poll with a target already activated");
1661 return -EBUSY;
1662 }
1663
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001664 if (dev->tgt_mode) {
1665 nfc_dev_err(&dev->interface->dev,
1666 "Cannot poll while already being activated");
1667 return -EBUSY;
1668 }
1669
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001670 if (tm_protocols) {
1671 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1672 if (dev->gb == NULL)
1673 tm_protocols = 0;
1674 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001675
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001676 dev->poll_mod_curr = 0;
1677 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1678 dev->poll_protocols = im_protocols;
1679 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001680
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001681 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001682}
1683
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001684static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1685{
1686 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1687
1688 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1689
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001690 del_timer(&dev->listen_timer);
1691
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001692 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001693 nfc_dev_dbg(&dev->interface->dev,
1694 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001695 return;
1696 }
1697
1698 /* An ack will cancel the last issued command (poll) */
1699 pn533_send_ack(dev, GFP_KERNEL);
1700
1701 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1702 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001703
1704 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001705}
1706
1707static int pn533_activate_target_nfcdep(struct pn533 *dev)
1708{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001709 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001710 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001711 int rc;
1712
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001713 struct sk_buff *skb;
1714 struct sk_buff *resp;
1715
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001716 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1717
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001718 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001719 if (!skb)
1720 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001721
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001722 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1723 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001724
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1726 if (IS_ERR(resp))
1727 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001729 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001730 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001731 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001732 nfc_dev_err(&dev->interface->dev,
1733 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001734 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001735 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001736 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001737
Samuel Ortiz541d9202011-12-14 16:43:10 +01001738 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001739 gt_len = resp->len - 16;
1740 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001741
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001742 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001743 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001744}
1745
Eric Lapuyade90099432012-05-07 12:31:13 +02001746static int pn533_activate_target(struct nfc_dev *nfc_dev,
1747 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001748{
1749 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1750 int rc;
1751
1752 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001753 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001754
1755 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001756 nfc_dev_err(&dev->interface->dev,
1757 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001758 return -EBUSY;
1759 }
1760
1761 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001762 nfc_dev_err(&dev->interface->dev,
1763 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001764 return -EBUSY;
1765 }
1766
1767 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001768 nfc_dev_err(&dev->interface->dev,
1769 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001770 return -EINVAL;
1771 }
1772
1773 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001774 nfc_dev_err(&dev->interface->dev,
1775 "Target doesn't support requested proto %u",
1776 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001777 return -EINVAL;
1778 }
1779
1780 if (protocol == NFC_PROTO_NFC_DEP) {
1781 rc = pn533_activate_target_nfcdep(dev);
1782 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001783 nfc_dev_err(&dev->interface->dev,
1784 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001785 return rc;
1786 }
1787 }
1788
1789 dev->tgt_active_prot = protocol;
1790 dev->tgt_available_prots = 0;
1791
1792 return 0;
1793}
1794
Eric Lapuyade90099432012-05-07 12:31:13 +02001795static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1796 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001797{
1798 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001799
1800 struct sk_buff *skb;
1801 struct sk_buff *resp;
1802
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001803 int rc;
1804
1805 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1806
1807 if (!dev->tgt_active_prot) {
1808 nfc_dev_err(&dev->interface->dev, "There is no active target");
1809 return;
1810 }
1811
1812 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001813 skb_queue_purge(&dev->resp_q);
1814
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001815 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001816 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001817 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001818
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001819 *skb_put(skb, 1) = 1; /* TG*/
1820
1821 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1822 if (IS_ERR(resp))
1823 return;
1824
1825 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001826 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001827 nfc_dev_err(&dev->interface->dev,
1828 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001829
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001830 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001831 return;
1832}
1833
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001834
1835static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001836 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001839 u8 target_gt_len;
1840 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001842
1843 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001844
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001845 if (IS_ERR(resp))
1846 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001847
1848 if (dev->tgt_available_prots &&
1849 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1850 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001851 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001852 rc = -EINVAL;
1853 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001854 }
1855
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001856 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1857
1858 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001859 if (rc != PN533_CMD_RET_SUCCESS) {
1860 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001861 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001862 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001863 }
1864
1865 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001866 struct nfc_target nfc_target;
1867
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001868 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1869
1870 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001871 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001872 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001873 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1874 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001875 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876
1877 dev->tgt_available_prots = 0;
1878 }
1879
1880 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1881
1882 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001883 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001884 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001885 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001886 if (rc == 0)
1887 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001888 dev->nfc_dev->targets[0].idx,
1889 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001890
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001891error:
1892 dev_kfree_skb(resp);
1893 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001894}
1895
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001896static int pn533_mod_to_baud(struct pn533 *dev)
1897{
1898 switch (dev->poll_mod_curr) {
1899 case PN533_POLL_MOD_106KBPS_A:
1900 return 0;
1901 case PN533_POLL_MOD_212KBPS_FELICA:
1902 return 1;
1903 case PN533_POLL_MOD_424KBPS_FELICA:
1904 return 2;
1905 default:
1906 return -EINVAL;
1907 }
1908}
1909
Samuel Ortizd7f33452012-05-29 21:45:21 +02001910#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001911static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001912 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001913{
1914 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001915 struct sk_buff *skb;
1916 int rc, baud, skb_len;
1917 u8 *next, *arg;
1918
Samuel Ortizd7f33452012-05-29 21:45:21 +02001919 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001920
1921 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1922
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001923 if (dev->poll_mod_count) {
1924 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001925 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001926 return -EBUSY;
1927 }
1928
1929 if (dev->tgt_active_prot) {
1930 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001931 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001932 return -EBUSY;
1933 }
1934
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001935 baud = pn533_mod_to_baud(dev);
1936 if (baud < 0) {
1937 nfc_dev_err(&dev->interface->dev,
1938 "Invalid curr modulation %d", dev->poll_mod_curr);
1939 return baud;
1940 }
1941
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001942 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001943 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001944 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001945
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001946 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001948 return -ENOMEM;
1949
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001950 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1951 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001952
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001953 next = skb_put(skb, 1); /* Next */
1954 *next = 0;
1955
1956 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1957 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1958 PASSIVE_DATA_LEN);
1959 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001960 }
1961
Samuel Ortiz47807d32012-03-05 01:03:50 +01001962 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001963 memcpy(skb_put(skb, gb_len), gb, gb_len);
1964 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001965 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001967 }
1968
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001969 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1970 if (!arg) {
1971 dev_kfree_skb(skb);
1972 return -ENOMEM;
1973 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001974
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001975 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001976
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001977 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1978 pn533_in_dep_link_up_complete, arg);
1979
1980 if (rc < 0) {
1981 dev_kfree_skb(skb);
1982 kfree(arg);
1983 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001984
1985 return rc;
1986}
1987
1988static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1989{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001990 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1991
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001992 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1993
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001994 pn533_poll_reset_mod_list(dev);
1995
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001996 if (dev->tgt_mode || dev->tgt_active_prot) {
1997 pn533_send_ack(dev, GFP_KERNEL);
1998 usb_kill_urb(dev->in_urb);
1999 }
2000
2001 dev->tgt_active_prot = 0;
2002 dev->tgt_mode = 0;
2003
2004 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002005
2006 return 0;
2007}
2008
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002009struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002010 data_exchange_cb_t cb;
2011 void *cb_context;
2012};
2013
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002014static struct sk_buff *pn533_build_response(struct pn533 *dev)
2015{
2016 struct sk_buff *skb, *tmp, *t;
2017 unsigned int skb_len = 0, tmp_len = 0;
2018
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002019 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002020
2021 if (skb_queue_empty(&dev->resp_q))
2022 return NULL;
2023
2024 if (skb_queue_len(&dev->resp_q) == 1) {
2025 skb = skb_dequeue(&dev->resp_q);
2026 goto out;
2027 }
2028
2029 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2030 skb_len += tmp->len;
2031
2032 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2033 __func__, skb_len);
2034
2035 skb = alloc_skb(skb_len, GFP_KERNEL);
2036 if (skb == NULL)
2037 goto out;
2038
2039 skb_put(skb, skb_len);
2040
2041 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2042 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2043 tmp_len += tmp->len;
2044 }
2045
2046out:
2047 skb_queue_purge(&dev->resp_q);
2048
2049 return skb;
2050}
2051
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002052static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002053 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002054{
2055 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002056 struct sk_buff *skb;
2057 int rc = 0;
2058 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002059
2060 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2061
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002062 if (IS_ERR(resp)) {
2063 rc = PTR_ERR(resp);
2064 goto _error;
2065 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002066
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002067 status = resp->data[0];
2068 ret = status & PN533_CMD_RET_MASK;
2069 mi = status & PN533_CMD_MI_MASK;
2070
2071 skb_pull(resp, sizeof(status));
2072
2073 if (ret != PN533_CMD_RET_SUCCESS) {
2074 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002075 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002076 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002077 goto error;
2078 }
2079
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002080 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002081
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002082 if (mi) {
2083 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002084 queue_work(dev->wq, &dev->mi_work);
2085 return -EINPROGRESS;
2086 }
2087
2088 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002089 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002090 goto error;
2091
2092 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002093 kfree(arg);
2094 return 0;
2095
2096error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002097 dev_kfree_skb(resp);
2098_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002099 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002100 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002101 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002102 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002103}
2104
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002105static int pn533_transceive(struct nfc_dev *nfc_dev,
2106 struct nfc_target *target, struct sk_buff *skb,
2107 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108{
2109 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002110 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111 int rc;
2112
2113 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2114
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002115 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2116 /* TODO: Implement support to multi-part data exchange */
2117 nfc_dev_err(&dev->interface->dev,
2118 "Data length greater than the max allowed: %d",
2119 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2120 rc = -ENOSYS;
2121 goto error;
2122 }
2123
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002124 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002125 nfc_dev_err(&dev->interface->dev,
2126 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002127 rc = -EINVAL;
2128 goto error;
2129 }
2130
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002131 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002132 if (!arg) {
2133 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002134 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002135 }
2136
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002137 arg->cb = cb;
2138 arg->cb_context = cb_context;
2139
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002140 switch (dev->device_type) {
2141 case PN533_DEVICE_PASORI:
2142 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2143 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2144 skb,
2145 pn533_data_exchange_complete,
2146 arg);
2147
2148 break;
2149 }
2150 default:
2151 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2152
2153 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2154 skb, pn533_data_exchange_complete,
2155 arg);
2156
2157 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158 }
2159
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002160 if (rc < 0) /* rc from send_async */
2161 goto error;
2162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002163 return 0;
2164
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002165error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002166 kfree(arg);
2167 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002168 return rc;
2169}
2170
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002171static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002172 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002174 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002175
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002176 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2177
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002178 if (IS_ERR(resp))
2179 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002180
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002181 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002182
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002183 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002184
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002185 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002186 nfc_tm_deactivated(dev->nfc_dev);
2187
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002188 dev->tgt_mode = 0;
2189
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002190 return 0;
2191 }
2192
2193 queue_work(dev->wq, &dev->tg_work);
2194
2195 return 0;
2196}
2197
2198static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2199{
2200 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002201 int rc;
2202
2203 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2204
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002205 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002206 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002207 "Data length greater than the max allowed: %d",
2208 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2209 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002210 }
2211
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002212 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2213 pn533_tm_send_complete, NULL);
2214 if (rc < 0)
2215 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002216
2217 return rc;
2218}
2219
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002220static void pn533_wq_mi_recv(struct work_struct *work)
2221{
2222 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002223
2224 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002225 int rc;
2226
2227 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2228
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002229 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002230 if (!skb)
2231 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002232
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002233 switch (dev->device_type) {
2234 case PN533_DEVICE_PASORI:
2235 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2236 rc = pn533_send_cmd_direct_async(dev,
2237 PN533_CMD_IN_COMM_THRU,
2238 skb,
2239 pn533_data_exchange_complete,
2240 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002241
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002242 break;
2243 }
2244 default:
2245 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002246
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002247 rc = pn533_send_cmd_direct_async(dev,
2248 PN533_CMD_IN_DATA_EXCHANGE,
2249 skb,
2250 pn533_data_exchange_complete,
2251 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002252
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002253 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002254 }
2255
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002256 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002257 return;
2258
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002259 nfc_dev_err(&dev->interface->dev,
2260 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002261
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002262 dev_kfree_skb(skb);
2263 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002264
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002265error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002266 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002267 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002268}
2269
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002270static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2271 u8 cfgdata_len)
2272{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002273 struct sk_buff *skb;
2274 struct sk_buff *resp;
2275
2276 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002277
2278 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2279
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002280 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002281
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002282 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002283 if (!skb)
2284 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002285
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2287 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002288
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002289 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2290 if (IS_ERR(resp))
2291 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002292
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002293 dev_kfree_skb(resp);
2294 return 0;
2295}
2296
2297static int pn533_get_firmware_version(struct pn533 *dev,
2298 struct pn533_fw_version *fv)
2299{
2300 struct sk_buff *skb;
2301 struct sk_buff *resp;
2302
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002303 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002304 if (!skb)
2305 return -ENOMEM;
2306
2307 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2308 if (IS_ERR(resp))
2309 return PTR_ERR(resp);
2310
2311 fv->ic = resp->data[0];
2312 fv->ver = resp->data[1];
2313 fv->rev = resp->data[2];
2314 fv->support = resp->data[3];
2315
2316 dev_kfree_skb(resp);
2317 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002318}
2319
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002320static int pn533_fw_reset(struct pn533 *dev)
2321{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002322 struct sk_buff *skb;
2323 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002324
2325 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2326
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002327 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002328 if (!skb)
2329 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002330
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002331 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002332
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002333 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2334 if (IS_ERR(resp))
2335 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002336
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002337 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002338
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002339 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002340}
2341
2342static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002343 .dev_up = NULL,
2344 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002345 .dep_link_up = pn533_dep_link_up,
2346 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002347 .start_poll = pn533_start_poll,
2348 .stop_poll = pn533_stop_poll,
2349 .activate_target = pn533_activate_target,
2350 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002351 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002352 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002353};
2354
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002355static int pn533_setup(struct pn533 *dev)
2356{
2357 struct pn533_config_max_retries max_retries;
2358 struct pn533_config_timing timing;
2359 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2360 int rc;
2361
2362 switch (dev->device_type) {
2363 case PN533_DEVICE_STD:
2364 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2365 max_retries.mx_rty_psl = 2;
2366 max_retries.mx_rty_passive_act =
2367 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2368
2369 timing.rfu = PN533_CONFIG_TIMING_102;
2370 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2371 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2372
2373 break;
2374
2375 case PN533_DEVICE_PASORI:
2376 max_retries.mx_rty_atr = 0x2;
2377 max_retries.mx_rty_psl = 0x1;
2378 max_retries.mx_rty_passive_act =
2379 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2380
2381 timing.rfu = PN533_CONFIG_TIMING_102;
2382 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2383 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2384
2385 break;
2386
2387 default:
2388 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2389 dev->device_type);
2390 return -EINVAL;
2391 }
2392
2393 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2394 (u8 *)&max_retries, sizeof(max_retries));
2395 if (rc) {
2396 nfc_dev_err(&dev->interface->dev,
2397 "Error on setting MAX_RETRIES config");
2398 return rc;
2399 }
2400
2401
2402 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2403 (u8 *)&timing, sizeof(timing));
2404 if (rc) {
2405 nfc_dev_err(&dev->interface->dev,
2406 "Error on setting RF timings");
2407 return rc;
2408 }
2409
2410 switch (dev->device_type) {
2411 case PN533_DEVICE_STD:
2412 break;
2413
2414 case PN533_DEVICE_PASORI:
2415 pn533_fw_reset(dev);
2416
2417 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2418 pasori_cfg, 3);
2419 if (rc) {
2420 nfc_dev_err(&dev->interface->dev,
2421 "Error while settings PASORI config");
2422 return rc;
2423 }
2424
2425 pn533_fw_reset(dev);
2426
2427 break;
2428 }
2429
2430 return 0;
2431}
2432
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002433static int pn533_probe(struct usb_interface *interface,
2434 const struct usb_device_id *id)
2435{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002436 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002437 struct pn533 *dev;
2438 struct usb_host_interface *iface_desc;
2439 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002440 int in_endpoint = 0;
2441 int out_endpoint = 0;
2442 int rc = -ENOMEM;
2443 int i;
2444 u32 protocols;
2445
2446 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2447 if (!dev)
2448 return -ENOMEM;
2449
2450 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2451 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002452 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002453
2454 iface_desc = interface->cur_altsetting;
2455 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2456 endpoint = &iface_desc->endpoint[i].desc;
2457
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002458 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002459 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002460
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002461 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002462 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463 }
2464
2465 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002466 nfc_dev_err(&interface->dev,
2467 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002468 rc = -ENODEV;
2469 goto error;
2470 }
2471
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002472 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002473 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2474
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002475 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002476 goto error;
2477
2478 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002479 usb_rcvbulkpipe(dev->udev, in_endpoint),
2480 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002481 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002482 usb_sndbulkpipe(dev->udev, out_endpoint),
2483 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002485 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2486 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002487 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002488 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002489 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002490 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002491 if (dev->wq == NULL)
2492 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002493
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002494 init_timer(&dev->listen_timer);
2495 dev->listen_timer.data = (unsigned long) dev;
2496 dev->listen_timer.function = pn533_listen_mode_timer;
2497
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002498 skb_queue_head_init(&dev->resp_q);
2499
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002500 INIT_LIST_HEAD(&dev->cmd_queue);
2501
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002502 usb_set_intfdata(interface, dev);
2503
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002504 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002506 dev->device_type = id->driver_info;
2507 switch (dev->device_type) {
2508 case PN533_DEVICE_STD:
2509 protocols = PN533_ALL_PROTOCOLS;
2510 break;
2511
2512 case PN533_DEVICE_PASORI:
2513 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2514 break;
2515
2516 default:
2517 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2518 dev->device_type);
2519 rc = -EINVAL;
2520 goto destroy_wq;
2521 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002522
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002523 memset(&fw_ver, 0, sizeof(fw_ver));
2524 rc = pn533_get_firmware_version(dev, &fw_ver);
2525 if (rc < 0)
2526 goto destroy_wq;
2527
2528 nfc_dev_info(&dev->interface->dev,
2529 "NXP PN533 firmware ver %d.%d now attached",
2530 fw_ver.ver, fw_ver.rev);
2531
2532
Samuel Ortize8753042011-08-19 15:47:11 +02002533 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002534 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002535 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002536 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002537 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002539 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002540
2541 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2542 nfc_set_drvdata(dev->nfc_dev, dev);
2543
2544 rc = nfc_register_device(dev->nfc_dev);
2545 if (rc)
2546 goto free_nfc_dev;
2547
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002548 rc = pn533_setup(dev);
2549 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002550 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002551
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002552 return 0;
2553
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002554unregister_nfc_dev:
2555 nfc_unregister_device(dev->nfc_dev);
2556
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002557free_nfc_dev:
2558 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002559
Samuel Ortiz4849f852012-04-10 19:43:17 +02002560destroy_wq:
2561 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002562error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002563 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002564 usb_free_urb(dev->out_urb);
2565 kfree(dev);
2566 return rc;
2567}
2568
2569static void pn533_disconnect(struct usb_interface *interface)
2570{
2571 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002572 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002573
2574 dev = usb_get_intfdata(interface);
2575 usb_set_intfdata(interface, NULL);
2576
2577 nfc_unregister_device(dev->nfc_dev);
2578 nfc_free_device(dev->nfc_dev);
2579
2580 usb_kill_urb(dev->in_urb);
2581 usb_kill_urb(dev->out_urb);
2582
Samuel Ortiz4849f852012-04-10 19:43:17 +02002583 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002584
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002585 skb_queue_purge(&dev->resp_q);
2586
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002587 del_timer(&dev->listen_timer);
2588
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002589 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2590 list_del(&cmd->queue);
2591 kfree(cmd);
2592 }
2593
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595 usb_free_urb(dev->out_urb);
2596 kfree(dev);
2597
Dan Carpenter276556d2011-07-08 10:21:15 +03002598 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002599}
2600
2601static struct usb_driver pn533_driver = {
2602 .name = "pn533",
2603 .probe = pn533_probe,
2604 .disconnect = pn533_disconnect,
2605 .id_table = pn533_table,
2606};
2607
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002608module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609
2610MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2611 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2612MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2613MODULE_VERSION(VERSION);
2614MODULE_LICENSE("GPL");