blob: 24ffbe04108ba2a820e1a714cc00c3dfc7c4cd80 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
30#define VERSION "0.1"
31
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define PN533_DEVICE_STD 0x1
42#define PN533_DEVICE_PASORI 0x2
43
Samuel Ortiz01d719a2012-07-04 00:14:04 +020044#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
45 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
46 NFC_PROTO_NFC_DEP_MASK |\
47 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020048
49#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
50 NFC_PROTO_MIFARE_MASK | \
51 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020052 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020053 NFC_PROTO_NFC_DEP_MASK)
54
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030055static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
57 .idVendor = PN533_VENDOR_ID,
58 .idProduct = PN533_PRODUCT_ID,
59 .driver_info = PN533_DEVICE_STD,
60 },
61 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
62 .idVendor = SCM_VENDOR_ID,
63 .idProduct = SCL3711_PRODUCT_ID,
64 .driver_info = PN533_DEVICE_STD,
65 },
66 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
67 .idVendor = SONY_VENDOR_ID,
68 .idProduct = PASORI_PRODUCT_ID,
69 .driver_info = PN533_DEVICE_PASORI,
70 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030071 { }
72};
73MODULE_DEVICE_TABLE(usb, pn533_table);
74
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020075/* How much time we spend listening for initiators */
76#define PN533_LISTEN_TIME 2
77
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020078/* Standard pn533 frame definitions */
79#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010080 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020081#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010083/*
84 * Max extended frame payload len, excluding TFI and CC
85 * which are already in PN533_FRAME_HEADER_LEN.
86 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020087#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010088
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020089#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010090 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020091#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
92#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030093
94/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020095#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030096
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020097/* standard frame identifier: in/out/error */
98#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
99#define PN533_STD_FRAME_DIR_OUT 0xD4
100#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300101
102/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200103#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104
105#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
106#define PN533_CMD_RF_CONFIGURATION 0x32
107#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200108#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300109#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
110#define PN533_CMD_IN_ATR 0x50
111#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100112#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300113
Samuel Ortizad3823c2012-05-30 23:54:55 +0200114#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200115#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200116#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100117#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
120
121/* PN533 Return codes */
122#define PN533_CMD_RET_MASK 0x3F
123#define PN533_CMD_MI_MASK 0x40
124#define PN533_CMD_RET_SUCCESS 0x00
125
126struct pn533;
127
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100128typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300129
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100130typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
131 struct sk_buff *resp);
132
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133/* structs for pn533 commands */
134
135/* PN533_CMD_GET_FIRMWARE_VERSION */
136struct pn533_fw_version {
137 u8 ic;
138 u8 ver;
139 u8 rev;
140 u8 support;
141};
142
143/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200144#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200146#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300147
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200148#define PN533_CONFIG_TIMING_102 0xb
149#define PN533_CONFIG_TIMING_204 0xc
150#define PN533_CONFIG_TIMING_409 0xd
151#define PN533_CONFIG_TIMING_819 0xe
152
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
154#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
155
156struct pn533_config_max_retries {
157 u8 mx_rty_atr;
158 u8 mx_rty_psl;
159 u8 mx_rty_passive_act;
160} __packed;
161
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200162struct pn533_config_timing {
163 u8 rfu;
164 u8 atr_res_timeout;
165 u8 dep_timeout;
166} __packed;
167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300168/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
169
170/* felica commands opcode */
171#define PN533_FELICA_OPC_SENSF_REQ 0
172#define PN533_FELICA_OPC_SENSF_RES 1
173/* felica SENSF_REQ parameters */
174#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
175#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
176#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
177#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
178
179/* type B initiator_data values */
180#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
181#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
182#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
183
184union pn533_cmd_poll_initdata {
185 struct {
186 u8 afi;
187 u8 polling_method;
188 } __packed type_b;
189 struct {
190 u8 opcode;
191 __be16 sc;
192 u8 rc;
193 u8 tsn;
194 } __packed felica;
195};
196
197/* Poll modulations */
198enum {
199 PN533_POLL_MOD_106KBPS_A,
200 PN533_POLL_MOD_212KBPS_FELICA,
201 PN533_POLL_MOD_424KBPS_FELICA,
202 PN533_POLL_MOD_106KBPS_JEWEL,
203 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200204 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205
206 __PN533_POLL_MOD_AFTER_LAST,
207};
208#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
209
210struct pn533_poll_modulations {
211 struct {
212 u8 maxtg;
213 u8 brty;
214 union pn533_cmd_poll_initdata initiator_data;
215 } __packed data;
216 u8 len;
217};
218
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100219static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300220 [PN533_POLL_MOD_106KBPS_A] = {
221 .data = {
222 .maxtg = 1,
223 .brty = 0,
224 },
225 .len = 2,
226 },
227 [PN533_POLL_MOD_212KBPS_FELICA] = {
228 .data = {
229 .maxtg = 1,
230 .brty = 1,
231 .initiator_data.felica = {
232 .opcode = PN533_FELICA_OPC_SENSF_REQ,
233 .sc = PN533_FELICA_SENSF_SC_ALL,
234 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
235 .tsn = 0,
236 },
237 },
238 .len = 7,
239 },
240 [PN533_POLL_MOD_424KBPS_FELICA] = {
241 .data = {
242 .maxtg = 1,
243 .brty = 2,
244 .initiator_data.felica = {
245 .opcode = PN533_FELICA_OPC_SENSF_REQ,
246 .sc = PN533_FELICA_SENSF_SC_ALL,
247 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
248 .tsn = 0,
249 },
250 },
251 .len = 7,
252 },
253 [PN533_POLL_MOD_106KBPS_JEWEL] = {
254 .data = {
255 .maxtg = 1,
256 .brty = 4,
257 },
258 .len = 2,
259 },
260 [PN533_POLL_MOD_847KBPS_B] = {
261 .data = {
262 .maxtg = 1,
263 .brty = 8,
264 .initiator_data.type_b = {
265 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
266 .polling_method =
267 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
268 },
269 },
270 .len = 3,
271 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200272 [PN533_LISTEN_MOD] = {
273 .len = 0,
274 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300275};
276
277/* PN533_CMD_IN_ATR */
278
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300279struct pn533_cmd_activate_response {
280 u8 status;
281 u8 nfcid3t[10];
282 u8 didt;
283 u8 bst;
284 u8 brt;
285 u8 to;
286 u8 ppt;
287 /* optional */
288 u8 gt[];
289} __packed;
290
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100291struct pn533_cmd_jump_dep_response {
292 u8 status;
293 u8 tg;
294 u8 nfcid3t[10];
295 u8 didt;
296 u8 bst;
297 u8 brt;
298 u8 to;
299 u8 ppt;
300 /* optional */
301 u8 gt[];
302} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303
Samuel Ortizad3823c2012-05-30 23:54:55 +0200304
305/* PN533_TG_INIT_AS_TARGET */
306#define PN533_INIT_TARGET_PASSIVE 0x1
307#define PN533_INIT_TARGET_DEP 0x2
308
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200309#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
310#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
311#define PN533_INIT_TARGET_RESP_DEP 0x4
312
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313struct pn533 {
314 struct usb_device *udev;
315 struct usb_interface *interface;
316 struct nfc_dev *nfc_dev;
317
318 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200321 struct sk_buff_head resp_q;
322
Samuel Ortiz4849f852012-04-10 19:43:17 +0200323 struct workqueue_struct *wq;
324 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200325 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200326 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200327 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200328 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200330 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200331 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332
333 pn533_cmd_complete_t cmd_complete;
334 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100335 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200336 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300337 u8 cmd;
338
339 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
340 u8 poll_mod_count;
341 u8 poll_mod_curr;
342 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200343 u32 listen_protocols;
344
345 u8 *gb;
346 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300347
348 u8 tgt_available_prots;
349 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200350 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200351
352 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200353
354 struct list_head cmd_queue;
355 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100356
357 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200358};
359
360struct pn533_cmd {
361 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100362 u8 cmd_code;
363 struct sk_buff *req;
364 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100365 int resp_len;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200366 void *arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300367};
368
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200369struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300370 u8 preamble;
371 __be16 start_frame;
372 u8 datalen;
373 u8 datalen_checksum;
374 u8 data[];
375} __packed;
376
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100377struct pn533_frame_ops {
378 void (*tx_frame_init)(void *frame, u8 cmd_code);
379 void (*tx_frame_finish)(void *frame);
380 void (*tx_update_payload_len)(void *frame, int len);
381 int tx_header_len;
382 int tx_tail_len;
383
384 bool (*rx_is_frame_valid)(void *frame);
385 int (*rx_frame_size)(void *frame);
386 int rx_header_len;
387 int rx_tail_len;
388
389 int max_payload_len;
390 u8 (*get_cmd_code)(void *frame);
391};
392
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300393/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200394static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300395{
396 return ~value + 1;
397}
398
399/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200400static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300401{
402 u8 sum = 0;
403 int i;
404
405 for (i = 0; i < datalen; i++)
406 sum += data[i];
407
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200408 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300409}
410
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200411static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300412{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200413 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100414
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300415 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200416 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
417 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
418 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300419 frame->datalen = 2;
420}
421
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200422static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300423{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200424 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100425
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200426 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300427
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200428 PN533_STD_FRAME_CHECKSUM(frame) =
429 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300430
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200431 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300432}
433
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200434static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100435{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200436 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100437
438 frame->datalen += len;
439}
440
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200441static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300442{
443 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200444 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300445
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200446 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300447 return false;
448
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200449 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300450 if (checksum != frame->datalen_checksum)
451 return false;
452
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200453 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
454 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300455 return false;
456
457 return true;
458}
459
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200460static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300461{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200462 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300463 return false;
464
465 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
466 return false;
467
468 return true;
469}
470
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200471static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300472{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200473 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100474
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200475 return sizeof(struct pn533_std_frame) + f->datalen +
476 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100477}
478
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200479static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100480{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200481 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100482
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200483 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100484}
485
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100486static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200487 .tx_frame_init = pn533_std_tx_frame_init,
488 .tx_frame_finish = pn533_std_tx_frame_finish,
489 .tx_update_payload_len = pn533_std_tx_update_payload_len,
490 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
491 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100492
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200493 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
494 .rx_frame_size = pn533_std_rx_frame_size,
495 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
496 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100497
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200498 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
499 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100500};
501
502static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
503{
504 return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300505}
506
Samuel Ortiz4849f852012-04-10 19:43:17 +0200507
508static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300509{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200510 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511 int rc;
512
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100513 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200515 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516}
517
518static void pn533_recv_response(struct urb *urb)
519{
520 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100521 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300522
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523 switch (urb->status) {
524 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100525 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300526 case -ECONNRESET:
527 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100528 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100529 "The urb has been canceled (status %d)",
530 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200531 dev->wq_in_error = urb->status;
532 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100533 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300534 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100535 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100536 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200537 dev->wq_in_error = urb->status;
538 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300539 }
540
541 in_frame = dev->in_urb->transfer_buffer;
542
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100543 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100544 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
545 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100546
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100547 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300548 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200549 dev->wq_in_error = -EIO;
550 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300551 }
552
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100553 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100554 nfc_dev_err(&dev->interface->dev,
555 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200556 dev->wq_in_error = -EIO;
557 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300558 }
559
Samuel Ortiz4849f852012-04-10 19:43:17 +0200560 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300561
Samuel Ortiz4849f852012-04-10 19:43:17 +0200562sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200563 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300564}
565
566static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
567{
568 dev->in_urb->complete = pn533_recv_response;
569
570 return usb_submit_urb(dev->in_urb, flags);
571}
572
573static void pn533_recv_ack(struct urb *urb)
574{
575 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200576 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300577 int rc;
578
579 switch (urb->status) {
580 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100581 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300582 case -ECONNRESET:
583 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100584 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100585 "The urb has been stopped (status %d)",
586 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200587 dev->wq_in_error = urb->status;
588 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100589 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300590 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100591 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100592 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200593 dev->wq_in_error = urb->status;
594 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300595 }
596
597 in_frame = dev->in_urb->transfer_buffer;
598
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200599 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300600 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200601 dev->wq_in_error = -EIO;
602 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300603 }
604
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300605 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
606 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100607 nfc_dev_err(&dev->interface->dev,
608 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200609 dev->wq_in_error = rc;
610 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300611 }
612
613 return;
614
Samuel Ortiz4849f852012-04-10 19:43:17 +0200615sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200616 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300617}
618
619static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
620{
621 dev->in_urb->complete = pn533_recv_ack;
622
623 return usb_submit_urb(dev->in_urb, flags);
624}
625
626static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
627{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200628 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100629 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300630 int rc;
631
632 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
633
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100634 dev->out_urb->transfer_buffer = ack;
635 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300636 rc = usb_submit_urb(dev->out_urb, flags);
637
638 return rc;
639}
640
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100641static int __pn533_send_frame_async(struct pn533 *dev,
642 struct sk_buff *out,
643 struct sk_buff *in,
644 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300645 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100646 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300647{
648 int rc;
649
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100650 dev->cmd = dev->ops->get_cmd_code(out->data);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651 dev->cmd_complete = cmd_complete;
652 dev->cmd_complete_arg = arg;
653
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100654 dev->out_urb->transfer_buffer = out->data;
655 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300656
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100657 dev->in_urb->transfer_buffer = in->data;
658 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300659
Samuel Ortize279f842013-03-20 11:27:57 +0100660 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
661 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100662
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100663 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300664 if (rc)
665 return rc;
666
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100667 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300668 if (rc)
669 goto error;
670
671 return 0;
672
673error:
674 usb_unlink_urb(dev->out_urb);
675 return rc;
676}
677
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100678static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
679 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100680{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681 /* payload is already there, just update datalen */
682 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100683 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100685
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100686 skb_push(skb, ops->tx_header_len);
687 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100688
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100689 ops->tx_frame_init(skb->data, cmd_code);
690 ops->tx_update_payload_len(skb->data, payload_len);
691 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100692}
693
694struct pn533_send_async_complete_arg {
695 pn533_send_async_complete_t complete_cb;
696 void *complete_cb_context;
697 struct sk_buff *resp;
698 struct sk_buff *req;
699};
700
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100701static int pn533_send_async_complete(struct pn533 *dev, void *_arg, int status)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100702{
703 struct pn533_send_async_complete_arg *arg = _arg;
704
705 struct sk_buff *req = arg->req;
706 struct sk_buff *resp = arg->resp;
707
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100708 int rc;
709
710 dev_kfree_skb(req);
711
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100712 if (status < 0) {
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100713 arg->complete_cb(dev, arg->complete_cb_context,
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100714 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100715 dev_kfree_skb(resp);
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100716 kfree(arg);
717 return status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100718 }
719
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100720 skb_put(resp, dev->ops->rx_frame_size(resp->data));
721 skb_pull(resp, dev->ops->rx_header_len);
722 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100723
724 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
725
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100726 kfree(arg);
727 return rc;
728}
729
730static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
731 struct sk_buff *req, struct sk_buff *resp,
732 int resp_len,
733 pn533_send_async_complete_t complete_cb,
734 void *complete_cb_context)
735{
736 struct pn533_cmd *cmd;
737 struct pn533_send_async_complete_arg *arg;
738 int rc = 0;
739
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100740 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100741
Thierry Escande858ce022013-01-12 19:37:12 +0100742 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100743 if (!arg)
744 return -ENOMEM;
745
746 arg->complete_cb = complete_cb;
747 arg->complete_cb_context = complete_cb_context;
748 arg->resp = resp;
749 arg->req = req;
750
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100751 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100752
753 mutex_lock(&dev->cmd_lock);
754
755 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100756 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
757 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100758 if (rc)
759 goto error;
760
761 dev->cmd_pending = 1;
762 goto unlock;
763 }
764
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100765 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
766 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100767
768 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
769 if (!cmd) {
770 rc = -ENOMEM;
771 goto error;
772 }
773
774 INIT_LIST_HEAD(&cmd->queue);
775 cmd->cmd_code = cmd_code;
776 cmd->req = req;
777 cmd->resp = resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100778 cmd->resp_len = resp_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100779 cmd->arg = arg;
780
781 list_add_tail(&cmd->queue, &dev->cmd_queue);
782
783 goto unlock;
784
785error:
786 kfree(arg);
787unlock:
788 mutex_unlock(&dev->cmd_lock);
789 return rc;
790}
791
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100792static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
793 struct sk_buff *req,
794 pn533_send_async_complete_t complete_cb,
795 void *complete_cb_context)
796{
797 struct sk_buff *resp;
798 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100799 int resp_len = dev->ops->rx_header_len +
800 dev->ops->max_payload_len +
801 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100802
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100803 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
804 if (!resp)
805 return -ENOMEM;
806
807 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
808 complete_cb_context);
809 if (rc)
810 dev_kfree_skb(resp);
811
812 return rc;
813}
814
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100815static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
816 struct sk_buff *req,
817 pn533_send_async_complete_t complete_cb,
818 void *complete_cb_context)
819{
820 struct sk_buff *resp;
821 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100822 int resp_len = dev->ops->rx_header_len +
823 dev->ops->max_payload_len +
824 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100825
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100826 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100827 if (!resp)
828 return -ENOMEM;
829
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100830 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
831 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100832 if (rc)
833 dev_kfree_skb(resp);
834
835 return rc;
836}
837
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100838/*
839 * pn533_send_cmd_direct_async
840 *
841 * The function sends a piority cmd directly to the chip omiting the cmd
842 * queue. It's intended to be used by chaining mechanism of received responses
843 * where the host has to request every single chunk of data before scheduling
844 * next cmd from the queue.
845 */
846static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
847 struct sk_buff *req,
848 pn533_send_async_complete_t complete_cb,
849 void *complete_cb_context)
850{
851 struct pn533_send_async_complete_arg *arg;
852 struct sk_buff *resp;
853 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100854 int resp_len = dev->ops->rx_header_len +
855 dev->ops->max_payload_len +
856 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100857
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100858 resp = alloc_skb(resp_len, GFP_KERNEL);
859 if (!resp)
860 return -ENOMEM;
861
Thierry Escande858ce022013-01-12 19:37:12 +0100862 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100863 if (!arg) {
864 dev_kfree_skb(resp);
865 return -ENOMEM;
866 }
867
868 arg->complete_cb = complete_cb;
869 arg->complete_cb_context = complete_cb_context;
870 arg->resp = resp;
871 arg->req = req;
872
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100873 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100874
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100875 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
876 pn533_send_async_complete, arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100877 if (rc < 0) {
878 dev_kfree_skb(resp);
879 kfree(arg);
880 }
881
882 return rc;
883}
884
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200885static void pn533_wq_cmd(struct work_struct *work)
886{
887 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
888 struct pn533_cmd *cmd;
889
890 mutex_lock(&dev->cmd_lock);
891
892 if (list_empty(&dev->cmd_queue)) {
893 dev->cmd_pending = 0;
894 mutex_unlock(&dev->cmd_lock);
895 return;
896 }
897
898 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
899
Szymon Janc60ad07a2012-10-25 17:29:45 +0200900 list_del(&cmd->queue);
901
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200902 mutex_unlock(&dev->cmd_lock);
903
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100904 __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
905 pn533_send_async_complete, cmd->arg);
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200906
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200907 kfree(cmd);
908}
909
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300910struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100911 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300912 struct completion done;
913};
914
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100915static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
916 struct sk_buff *resp)
917{
918 struct pn533_sync_cmd_response *arg = _arg;
919
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100920 arg->resp = resp;
921 complete(&arg->done);
922
923 return 0;
924}
925
926/* pn533_send_cmd_sync
927 *
928 * Please note the req parameter is freed inside the function to
929 * limit a number of return value interpretations by the caller.
930 *
931 * 1. negative in case of error during TX path -> req should be freed
932 *
933 * 2. negative in case of error during RX path -> req should not be freed
934 * as it's been already freed at the begining of RX path by
935 * async_complete_cb.
936 *
937 * 3. valid pointer in case of succesfult RX path
938 *
939 * A caller has to check a return value with IS_ERR macro. If the test pass,
940 * the returned pointer is valid.
941 *
942 * */
943static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
944 struct sk_buff *req)
945{
946 int rc;
947 struct pn533_sync_cmd_response arg;
948
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100949 init_completion(&arg.done);
950
951 rc = pn533_send_cmd_async(dev, cmd_code, req,
952 pn533_send_sync_complete, &arg);
953 if (rc) {
954 dev_kfree_skb(req);
955 return ERR_PTR(rc);
956 }
957
958 wait_for_completion(&arg.done);
959
960 return arg.resp;
961}
962
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300963static void pn533_send_complete(struct urb *urb)
964{
965 struct pn533 *dev = urb->context;
966
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300967 switch (urb->status) {
968 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100969 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300970 case -ECONNRESET:
971 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100972 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100973 "The urb has been stopped (status %d)",
974 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300975 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100976 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300977 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100978 nfc_dev_err(&dev->interface->dev,
979 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300980 }
981}
982
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100983static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100984{
985 struct sk_buff *skb;
986
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100987 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100988 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100989 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100990
991 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100992 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100993
994 return skb;
995}
996
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300997struct pn533_target_type_a {
998 __be16 sens_res;
999 u8 sel_res;
1000 u8 nfcid_len;
1001 u8 nfcid_data[];
1002} __packed;
1003
1004
1005#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1006#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1007#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1008
1009#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1010#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1011
1012#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1013#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1014
1015#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1016#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1017#define PN533_TYPE_A_SEL_PROT_DEP 2
1018#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1019
1020static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1021 int target_data_len)
1022{
1023 u8 ssd;
1024 u8 platconf;
1025
1026 if (target_data_len < sizeof(struct pn533_target_type_a))
1027 return false;
1028
1029 /* The lenght check of nfcid[] and ats[] are not being performed because
1030 the values are not being used */
1031
1032 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1033 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1034 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1035
1036 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001037 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1038 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1039 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001040 return false;
1041
1042 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1043 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1044 return false;
1045
1046 return true;
1047}
1048
1049static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1050 int tgt_data_len)
1051{
1052 struct pn533_target_type_a *tgt_type_a;
1053
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001054 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001055
1056 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1057 return -EPROTO;
1058
1059 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1060 case PN533_TYPE_A_SEL_PROT_MIFARE:
1061 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1062 break;
1063 case PN533_TYPE_A_SEL_PROT_ISO14443:
1064 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1065 break;
1066 case PN533_TYPE_A_SEL_PROT_DEP:
1067 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1068 break;
1069 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1070 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1071 NFC_PROTO_NFC_DEP_MASK;
1072 break;
1073 }
1074
1075 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1076 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001077 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1078 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001079
1080 return 0;
1081}
1082
1083struct pn533_target_felica {
1084 u8 pol_res;
1085 u8 opcode;
1086 u8 nfcid2[8];
1087 u8 pad[8];
1088 /* optional */
1089 u8 syst_code[];
1090} __packed;
1091
1092#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1093#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1094
1095static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1096 int target_data_len)
1097{
1098 if (target_data_len < sizeof(struct pn533_target_felica))
1099 return false;
1100
1101 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1102 return false;
1103
1104 return true;
1105}
1106
1107static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1108 int tgt_data_len)
1109{
1110 struct pn533_target_felica *tgt_felica;
1111
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001112 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001113
1114 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1115 return -EPROTO;
1116
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001117 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1118 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001119 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1120 else
1121 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1122
Samuel Ortiz79757542012-03-05 01:03:45 +01001123 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1124 nfc_tgt->sensf_res_len = 9;
1125
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001126 return 0;
1127}
1128
1129struct pn533_target_jewel {
1130 __be16 sens_res;
1131 u8 jewelid[4];
1132} __packed;
1133
1134static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1135 int target_data_len)
1136{
1137 u8 ssd;
1138 u8 platconf;
1139
1140 if (target_data_len < sizeof(struct pn533_target_jewel))
1141 return false;
1142
1143 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1144 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1145 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1146
1147 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001148 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1149 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1150 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001151 return false;
1152
1153 return true;
1154}
1155
1156static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1157 int tgt_data_len)
1158{
1159 struct pn533_target_jewel *tgt_jewel;
1160
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001161 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001162
1163 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1164 return -EPROTO;
1165
1166 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1167 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001168 nfc_tgt->nfcid1_len = 4;
1169 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001170
1171 return 0;
1172}
1173
1174struct pn533_type_b_prot_info {
1175 u8 bitrate;
1176 u8 fsci_type;
1177 u8 fwi_adc_fo;
1178} __packed;
1179
1180#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1181#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1182#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1183
1184struct pn533_type_b_sens_res {
1185 u8 opcode;
1186 u8 nfcid[4];
1187 u8 appdata[4];
1188 struct pn533_type_b_prot_info prot_info;
1189} __packed;
1190
1191#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1192
1193struct pn533_target_type_b {
1194 struct pn533_type_b_sens_res sensb_res;
1195 u8 attrib_res_len;
1196 u8 attrib_res[];
1197} __packed;
1198
1199static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1200 int target_data_len)
1201{
1202 if (target_data_len < sizeof(struct pn533_target_type_b))
1203 return false;
1204
1205 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1206 return false;
1207
1208 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1209 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1210 return false;
1211
1212 return true;
1213}
1214
1215static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1216 int tgt_data_len)
1217{
1218 struct pn533_target_type_b *tgt_type_b;
1219
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001220 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001221
1222 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1223 return -EPROTO;
1224
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001225 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001226
1227 return 0;
1228}
1229
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001230static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1231 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001232{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001233 struct nfc_target nfc_tgt;
1234 int rc;
1235
1236 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001237 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001238
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001239 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001240 return -EPROTO;
1241
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001242 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1243
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001244 switch (dev->poll_mod_curr) {
1245 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001246 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001247 break;
1248 case PN533_POLL_MOD_212KBPS_FELICA:
1249 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001250 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001251 break;
1252 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001253 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001254 break;
1255 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257 break;
1258 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001259 nfc_dev_err(&dev->interface->dev,
1260 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001261 return -EPROTO;
1262 }
1263
1264 if (rc)
1265 return rc;
1266
1267 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001268 nfc_dev_dbg(&dev->interface->dev,
1269 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001270 return -EAGAIN;
1271 }
1272
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001273 nfc_dev_dbg(&dev->interface->dev,
1274 "Target found - supported protocols: 0x%x",
1275 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001276
1277 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1278
1279 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1280
1281 return 0;
1282}
1283
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001284static inline void pn533_poll_next_mod(struct pn533 *dev)
1285{
1286 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1287}
1288
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001289static void pn533_poll_reset_mod_list(struct pn533 *dev)
1290{
1291 dev->poll_mod_count = 0;
1292}
1293
1294static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1295{
1296 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001297 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001298 dev->poll_mod_count++;
1299}
1300
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001301static void pn533_poll_create_mod_list(struct pn533 *dev,
1302 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001303{
1304 pn533_poll_reset_mod_list(dev);
1305
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001306 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1307 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1308 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1310
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001311 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1312 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001313 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1314 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1315 }
1316
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001317 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1319
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001320 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001321 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001322
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001323 if (tm_protocols)
1324 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001325}
1326
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001327static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001329 u8 nbtg, tg, *tgdata;
1330 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331
1332 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1333
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001334 nbtg = resp->data[0];
1335 tg = resp->data[1];
1336 tgdata = &resp->data[2];
1337 tgdata_len = resp->len - 2; /* nbtg + tg */
1338
1339 if (nbtg) {
1340 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001341
1342 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001343 if (rc == 0) {
1344 pn533_poll_reset_mod_list(dev);
1345 return 0;
1346 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001347 }
1348
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001349 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001350}
1351
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001352static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001353{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001354 struct sk_buff *skb;
1355 u8 *felica, *nfcid3, *gb;
1356
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001357 u8 *gbytes = dev->gb;
1358 size_t gbytes_len = dev->gb_len;
1359
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001360 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1361 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1362 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1363 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001364
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001365 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1366 0x0, 0x0, 0x0,
1367 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001368
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001369 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1370 felica (18), nfcid3 (10), gb_len (1) */
1371 gbytes_len +
1372 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001373
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001374 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001375 if (!skb)
1376 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001377
1378 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001379 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001380
1381 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001382 memcpy(skb_put(skb, 6), mifare_params, 6);
1383
1384 /* Felica params */
1385 felica = skb_put(skb, 18);
1386 memcpy(felica, felica_params, 18);
1387 get_random_bytes(felica + 2, 6);
1388
1389 /* NFCID3 */
1390 nfcid3 = skb_put(skb, 10);
1391 memset(nfcid3, 0, 10);
1392 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001393
1394 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001395 *skb_put(skb, 1) = gbytes_len;
1396
1397 gb = skb_put(skb, gbytes_len);
1398 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001399
Samuel Ortizad3823c2012-05-30 23:54:55 +02001400 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001401 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001402
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001403 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001404}
1405
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001406#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001407#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1408static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001410{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001411 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001412
1413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1414
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001415 if (IS_ERR(resp))
1416 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001417
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001418 status = resp->data[0];
1419 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001420
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001421 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001422 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001423 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001424 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001425 return 0;
1426 }
1427
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001428 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001429}
1430
1431static void pn533_wq_tg_get_data(struct work_struct *work)
1432{
1433 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001434
1435 struct sk_buff *skb;
1436 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001437
1438 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1439
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001440 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001441 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001442 return;
1443
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001444 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1445 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001446
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001447 if (rc < 0)
1448 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001449
1450 return;
1451}
1452
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001453#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001454static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001455{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001457 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001458 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001459
1460 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1461
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001463 return -EINVAL;
1464
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001465 mode = resp->data[0];
1466 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001467
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001468 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1469 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001470
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1472 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001473 comm_mode = NFC_COMM_ACTIVE;
1474
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001475 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001476 return -EOPNOTSUPP;
1477
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001478 gb = cmd + ATR_REQ_GB_OFFSET;
1479 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001480
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001481 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1482 comm_mode, gb, gb_len);
1483 if (rc < 0) {
1484 nfc_dev_err(&dev->interface->dev,
1485 "Error when signaling target activation");
1486 return rc;
1487 }
1488
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001489 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001490 queue_work(dev->wq, &dev->tg_work);
1491
1492 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001493}
1494
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001495static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001496{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001497 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001498
1499 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1500
1501 /* An ack will cancel the last issued command (poll) */
1502 pn533_send_ack(dev, GFP_ATOMIC);
1503
1504 dev->cancel_listen = 1;
1505
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001506 pn533_poll_next_mod(dev);
1507
1508 queue_work(dev->wq, &dev->poll_work);
1509}
1510
1511static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001512 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001513{
1514 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001515 int rc;
1516
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001517 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1518
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001519 if (IS_ERR(resp)) {
1520 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001521
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001522 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1523 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001524
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001525 if (rc == -ENOENT) {
1526 if (dev->poll_mod_count != 0)
1527 return rc;
1528 else
1529 goto stop_poll;
1530 } else if (rc < 0) {
1531 nfc_dev_err(&dev->interface->dev,
1532 "Error %d when running poll", rc);
1533 goto stop_poll;
1534 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001535 }
1536
1537 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1538
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001539 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001540 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001541 rc = pn533_init_target_complete(dev, resp);
1542 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001543 }
1544
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545 /* Initiator mode */
1546 rc = pn533_start_poll_complete(dev, resp);
1547 if (!rc)
1548 goto done;
1549
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001550 if (!dev->poll_mod_count) {
1551 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1552 goto done;
1553 }
1554
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001555 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001556 queue_work(dev->wq, &dev->poll_work);
1557
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001558done:
1559 dev_kfree_skb(resp);
1560 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001561
1562stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001563 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1564
Samuel Ortizad3823c2012-05-30 23:54:55 +02001565 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001566 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001567 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001569
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001570static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1571 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001572{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001573 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001574
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001575 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 if (!skb)
1577 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001578
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001580
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001581 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001582}
1583
1584static int pn533_send_poll_frame(struct pn533 *dev)
1585{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001586 struct pn533_poll_modulations *mod;
1587 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001588 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001589 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001590
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001591 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001592
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001593 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1594 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001595
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001596 if (mod->len == 0) { /* Listen mode */
1597 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001598 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001599 } else { /* Polling mode */
1600 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001601 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602 }
1603
1604 if (!skb) {
1605 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1606 return -ENOMEM;
1607 }
1608
1609 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1610 NULL);
1611 if (rc < 0) {
1612 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001613 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001614 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001615
1616 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001617}
1618
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001619static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001620{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1622 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001623 int rc;
1624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1626
1627 nfc_dev_dbg(&dev->interface->dev,
1628 "%s cancel_listen %d modulation len %d",
1629 __func__, dev->cancel_listen, cur_mod->len);
1630
1631 if (dev->cancel_listen == 1) {
1632 dev->cancel_listen = 0;
1633 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001634 }
1635
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001636 rc = pn533_send_poll_frame(dev);
1637 if (rc)
1638 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001639
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001640 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1641 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001642
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001643 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001644}
1645
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001646static int pn533_start_poll(struct nfc_dev *nfc_dev,
1647 u32 im_protocols, u32 tm_protocols)
1648{
1649 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1650
1651 nfc_dev_dbg(&dev->interface->dev,
1652 "%s: im protocols 0x%x tm protocols 0x%x",
1653 __func__, im_protocols, tm_protocols);
1654
1655 if (dev->tgt_active_prot) {
1656 nfc_dev_err(&dev->interface->dev,
1657 "Cannot poll with a target already activated");
1658 return -EBUSY;
1659 }
1660
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001661 if (dev->tgt_mode) {
1662 nfc_dev_err(&dev->interface->dev,
1663 "Cannot poll while already being activated");
1664 return -EBUSY;
1665 }
1666
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 if (tm_protocols) {
1668 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1669 if (dev->gb == NULL)
1670 tm_protocols = 0;
1671 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001672
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001673 dev->poll_mod_curr = 0;
1674 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1675 dev->poll_protocols = im_protocols;
1676 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001677
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001678 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001679}
1680
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001681static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1682{
1683 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1684
1685 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1686
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001687 del_timer(&dev->listen_timer);
1688
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001689 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001690 nfc_dev_dbg(&dev->interface->dev,
1691 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001692 return;
1693 }
1694
1695 /* An ack will cancel the last issued command (poll) */
1696 pn533_send_ack(dev, GFP_KERNEL);
1697
1698 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1699 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001700
1701 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001702}
1703
1704static int pn533_activate_target_nfcdep(struct pn533 *dev)
1705{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001706 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001707 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001708 int rc;
1709
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001710 struct sk_buff *skb;
1711 struct sk_buff *resp;
1712
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001713 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1714
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001715 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001716 if (!skb)
1717 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001719 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1720 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001721
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001722 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1723 if (IS_ERR(resp))
1724 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001725
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001726 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001727 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001728 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001729 nfc_dev_err(&dev->interface->dev,
1730 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001731 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001732 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001733 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734
Samuel Ortiz541d9202011-12-14 16:43:10 +01001735 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001736 gt_len = resp->len - 16;
1737 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001738
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001739 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001740 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741}
1742
Eric Lapuyade90099432012-05-07 12:31:13 +02001743static int pn533_activate_target(struct nfc_dev *nfc_dev,
1744 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001745{
1746 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1747 int rc;
1748
1749 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001750 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751
1752 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001753 nfc_dev_err(&dev->interface->dev,
1754 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001755 return -EBUSY;
1756 }
1757
1758 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001759 nfc_dev_err(&dev->interface->dev,
1760 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001761 return -EBUSY;
1762 }
1763
1764 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001765 nfc_dev_err(&dev->interface->dev,
1766 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 return -EINVAL;
1768 }
1769
1770 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001771 nfc_dev_err(&dev->interface->dev,
1772 "Target doesn't support requested proto %u",
1773 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001774 return -EINVAL;
1775 }
1776
1777 if (protocol == NFC_PROTO_NFC_DEP) {
1778 rc = pn533_activate_target_nfcdep(dev);
1779 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001780 nfc_dev_err(&dev->interface->dev,
1781 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001782 return rc;
1783 }
1784 }
1785
1786 dev->tgt_active_prot = protocol;
1787 dev->tgt_available_prots = 0;
1788
1789 return 0;
1790}
1791
Eric Lapuyade90099432012-05-07 12:31:13 +02001792static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1793 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001794{
1795 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001796
1797 struct sk_buff *skb;
1798 struct sk_buff *resp;
1799
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800 int rc;
1801
1802 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1803
1804 if (!dev->tgt_active_prot) {
1805 nfc_dev_err(&dev->interface->dev, "There is no active target");
1806 return;
1807 }
1808
1809 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001810 skb_queue_purge(&dev->resp_q);
1811
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001812 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001813 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001815
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001816 *skb_put(skb, 1) = 1; /* TG*/
1817
1818 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1819 if (IS_ERR(resp))
1820 return;
1821
1822 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001823 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001824 nfc_dev_err(&dev->interface->dev,
1825 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001826
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001827 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001828 return;
1829}
1830
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001831
1832static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001833 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001834{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001835 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001836 u8 target_gt_len;
1837 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001839
1840 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001841
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001842 if (IS_ERR(resp))
1843 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001844
1845 if (dev->tgt_available_prots &&
1846 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1847 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001848 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001849 rc = -EINVAL;
1850 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001851 }
1852
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001853 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1854
1855 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001856 if (rc != PN533_CMD_RET_SUCCESS) {
1857 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001858 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001859 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001860 }
1861
1862 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001863 struct nfc_target nfc_target;
1864
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001865 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1866
1867 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001868 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001870 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1871 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001872 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001873
1874 dev->tgt_available_prots = 0;
1875 }
1876
1877 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1878
1879 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001880 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001881 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001882 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001883 if (rc == 0)
1884 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001885 dev->nfc_dev->targets[0].idx,
1886 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001887
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001888error:
1889 dev_kfree_skb(resp);
1890 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001891}
1892
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001893static int pn533_mod_to_baud(struct pn533 *dev)
1894{
1895 switch (dev->poll_mod_curr) {
1896 case PN533_POLL_MOD_106KBPS_A:
1897 return 0;
1898 case PN533_POLL_MOD_212KBPS_FELICA:
1899 return 1;
1900 case PN533_POLL_MOD_424KBPS_FELICA:
1901 return 2;
1902 default:
1903 return -EINVAL;
1904 }
1905}
1906
Samuel Ortizd7f33452012-05-29 21:45:21 +02001907#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001908static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001909 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001910{
1911 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001912 struct sk_buff *skb;
1913 int rc, baud, skb_len;
1914 u8 *next, *arg;
1915
Samuel Ortizd7f33452012-05-29 21:45:21 +02001916 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001917
1918 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1919
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001920 if (dev->poll_mod_count) {
1921 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001922 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001923 return -EBUSY;
1924 }
1925
1926 if (dev->tgt_active_prot) {
1927 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001928 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001929 return -EBUSY;
1930 }
1931
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001932 baud = pn533_mod_to_baud(dev);
1933 if (baud < 0) {
1934 nfc_dev_err(&dev->interface->dev,
1935 "Invalid curr modulation %d", dev->poll_mod_curr);
1936 return baud;
1937 }
1938
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001939 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001940 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001941 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001942
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001943 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001944 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001945 return -ENOMEM;
1946
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1948 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001949
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001950 next = skb_put(skb, 1); /* Next */
1951 *next = 0;
1952
1953 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1954 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1955 PASSIVE_DATA_LEN);
1956 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001957 }
1958
Samuel Ortiz47807d32012-03-05 01:03:50 +01001959 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001960 memcpy(skb_put(skb, gb_len), gb, gb_len);
1961 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001962 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001963 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001964 }
1965
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1967 if (!arg) {
1968 dev_kfree_skb(skb);
1969 return -ENOMEM;
1970 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001971
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001972 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001973
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001974 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1975 pn533_in_dep_link_up_complete, arg);
1976
1977 if (rc < 0) {
1978 dev_kfree_skb(skb);
1979 kfree(arg);
1980 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001981
1982 return rc;
1983}
1984
1985static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1986{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001987 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1988
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001989 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1990
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001991 pn533_poll_reset_mod_list(dev);
1992
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001993 if (dev->tgt_mode || dev->tgt_active_prot) {
1994 pn533_send_ack(dev, GFP_KERNEL);
1995 usb_kill_urb(dev->in_urb);
1996 }
1997
1998 dev->tgt_active_prot = 0;
1999 dev->tgt_mode = 0;
2000
2001 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002002
2003 return 0;
2004}
2005
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002006struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002007 data_exchange_cb_t cb;
2008 void *cb_context;
2009};
2010
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002011static struct sk_buff *pn533_build_response(struct pn533 *dev)
2012{
2013 struct sk_buff *skb, *tmp, *t;
2014 unsigned int skb_len = 0, tmp_len = 0;
2015
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002016 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002017
2018 if (skb_queue_empty(&dev->resp_q))
2019 return NULL;
2020
2021 if (skb_queue_len(&dev->resp_q) == 1) {
2022 skb = skb_dequeue(&dev->resp_q);
2023 goto out;
2024 }
2025
2026 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2027 skb_len += tmp->len;
2028
2029 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2030 __func__, skb_len);
2031
2032 skb = alloc_skb(skb_len, GFP_KERNEL);
2033 if (skb == NULL)
2034 goto out;
2035
2036 skb_put(skb, skb_len);
2037
2038 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2039 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2040 tmp_len += tmp->len;
2041 }
2042
2043out:
2044 skb_queue_purge(&dev->resp_q);
2045
2046 return skb;
2047}
2048
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002049static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002050 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002051{
2052 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002053 struct sk_buff *skb;
2054 int rc = 0;
2055 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002056
2057 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2058
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002059 if (IS_ERR(resp)) {
2060 rc = PTR_ERR(resp);
2061 goto _error;
2062 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002063
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002064 status = resp->data[0];
2065 ret = status & PN533_CMD_RET_MASK;
2066 mi = status & PN533_CMD_MI_MASK;
2067
2068 skb_pull(resp, sizeof(status));
2069
2070 if (ret != PN533_CMD_RET_SUCCESS) {
2071 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002072 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002073 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002074 goto error;
2075 }
2076
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002077 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002078
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002079 if (mi) {
2080 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002081 queue_work(dev->wq, &dev->mi_work);
2082 return -EINPROGRESS;
2083 }
2084
2085 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002086 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002087 goto error;
2088
2089 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002090 kfree(arg);
2091 return 0;
2092
2093error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002094 dev_kfree_skb(resp);
2095_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002096 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002097 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002098 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002099 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002100}
2101
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002102static int pn533_transceive(struct nfc_dev *nfc_dev,
2103 struct nfc_target *target, struct sk_buff *skb,
2104 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002105{
2106 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002107 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108 int rc;
2109
2110 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2111
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002112 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2113 /* TODO: Implement support to multi-part data exchange */
2114 nfc_dev_err(&dev->interface->dev,
2115 "Data length greater than the max allowed: %d",
2116 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2117 rc = -ENOSYS;
2118 goto error;
2119 }
2120
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002121 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002122 nfc_dev_err(&dev->interface->dev,
2123 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002124 rc = -EINVAL;
2125 goto error;
2126 }
2127
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002128 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002129 if (!arg) {
2130 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002131 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002132 }
2133
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002134 arg->cb = cb;
2135 arg->cb_context = cb_context;
2136
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002137 switch (dev->device_type) {
2138 case PN533_DEVICE_PASORI:
2139 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2140 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2141 skb,
2142 pn533_data_exchange_complete,
2143 arg);
2144
2145 break;
2146 }
2147 default:
2148 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2149
2150 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2151 skb, pn533_data_exchange_complete,
2152 arg);
2153
2154 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002155 }
2156
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002157 if (rc < 0) /* rc from send_async */
2158 goto error;
2159
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002160 return 0;
2161
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002162error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002163 kfree(arg);
2164 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002165 return rc;
2166}
2167
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002168static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002169 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002170{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002171 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002172
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2174
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002175 if (IS_ERR(resp))
2176 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002177
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002178 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002179
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002180 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002181
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002182 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002183 nfc_tm_deactivated(dev->nfc_dev);
2184
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002185 dev->tgt_mode = 0;
2186
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002187 return 0;
2188 }
2189
2190 queue_work(dev->wq, &dev->tg_work);
2191
2192 return 0;
2193}
2194
2195static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2196{
2197 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002198 int rc;
2199
2200 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2201
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002202 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002203 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002204 "Data length greater than the max allowed: %d",
2205 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2206 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002207 }
2208
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002209 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2210 pn533_tm_send_complete, NULL);
2211 if (rc < 0)
2212 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002213
2214 return rc;
2215}
2216
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002217static void pn533_wq_mi_recv(struct work_struct *work)
2218{
2219 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002220
2221 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002222 int rc;
2223
2224 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2225
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002226 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002227 if (!skb)
2228 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002229
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002230 switch (dev->device_type) {
2231 case PN533_DEVICE_PASORI:
2232 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2233 rc = pn533_send_cmd_direct_async(dev,
2234 PN533_CMD_IN_COMM_THRU,
2235 skb,
2236 pn533_data_exchange_complete,
2237 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002238
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002239 break;
2240 }
2241 default:
2242 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002243
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002244 rc = pn533_send_cmd_direct_async(dev,
2245 PN533_CMD_IN_DATA_EXCHANGE,
2246 skb,
2247 pn533_data_exchange_complete,
2248 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002249
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002250 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002251 }
2252
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002253 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002254 return;
2255
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002256 nfc_dev_err(&dev->interface->dev,
2257 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002258
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002259 dev_kfree_skb(skb);
2260 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002261
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002262error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002263 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002264 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002265}
2266
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002267static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2268 u8 cfgdata_len)
2269{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002270 struct sk_buff *skb;
2271 struct sk_buff *resp;
2272
2273 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002274
2275 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2276
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002277 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002278
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002279 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002280 if (!skb)
2281 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002282
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002283 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2284 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002285
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2287 if (IS_ERR(resp))
2288 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002289
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002290 dev_kfree_skb(resp);
2291 return 0;
2292}
2293
2294static int pn533_get_firmware_version(struct pn533 *dev,
2295 struct pn533_fw_version *fv)
2296{
2297 struct sk_buff *skb;
2298 struct sk_buff *resp;
2299
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002300 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002301 if (!skb)
2302 return -ENOMEM;
2303
2304 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2305 if (IS_ERR(resp))
2306 return PTR_ERR(resp);
2307
2308 fv->ic = resp->data[0];
2309 fv->ver = resp->data[1];
2310 fv->rev = resp->data[2];
2311 fv->support = resp->data[3];
2312
2313 dev_kfree_skb(resp);
2314 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002315}
2316
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002317static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002318{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002319 struct sk_buff *skb;
2320 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002321
2322 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2323
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002324 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002325 if (!skb)
2326 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002327
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002328 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002329
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002330 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2331 if (IS_ERR(resp))
2332 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002333
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002334 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002335
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002336 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002337}
2338
2339static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002340 .dev_up = NULL,
2341 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002342 .dep_link_up = pn533_dep_link_up,
2343 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002344 .start_poll = pn533_start_poll,
2345 .stop_poll = pn533_stop_poll,
2346 .activate_target = pn533_activate_target,
2347 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002348 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002349 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002350};
2351
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002352static int pn533_setup(struct pn533 *dev)
2353{
2354 struct pn533_config_max_retries max_retries;
2355 struct pn533_config_timing timing;
2356 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2357 int rc;
2358
2359 switch (dev->device_type) {
2360 case PN533_DEVICE_STD:
2361 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2362 max_retries.mx_rty_psl = 2;
2363 max_retries.mx_rty_passive_act =
2364 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2365
2366 timing.rfu = PN533_CONFIG_TIMING_102;
2367 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2368 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2369
2370 break;
2371
2372 case PN533_DEVICE_PASORI:
2373 max_retries.mx_rty_atr = 0x2;
2374 max_retries.mx_rty_psl = 0x1;
2375 max_retries.mx_rty_passive_act =
2376 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2377
2378 timing.rfu = PN533_CONFIG_TIMING_102;
2379 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2380 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2381
2382 break;
2383
2384 default:
2385 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2386 dev->device_type);
2387 return -EINVAL;
2388 }
2389
2390 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2391 (u8 *)&max_retries, sizeof(max_retries));
2392 if (rc) {
2393 nfc_dev_err(&dev->interface->dev,
2394 "Error on setting MAX_RETRIES config");
2395 return rc;
2396 }
2397
2398
2399 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2400 (u8 *)&timing, sizeof(timing));
2401 if (rc) {
2402 nfc_dev_err(&dev->interface->dev,
2403 "Error on setting RF timings");
2404 return rc;
2405 }
2406
2407 switch (dev->device_type) {
2408 case PN533_DEVICE_STD:
2409 break;
2410
2411 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002412 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002413
2414 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2415 pasori_cfg, 3);
2416 if (rc) {
2417 nfc_dev_err(&dev->interface->dev,
2418 "Error while settings PASORI config");
2419 return rc;
2420 }
2421
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002422 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002423
2424 break;
2425 }
2426
2427 return 0;
2428}
2429
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002430static int pn533_probe(struct usb_interface *interface,
2431 const struct usb_device_id *id)
2432{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002433 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002434 struct pn533 *dev;
2435 struct usb_host_interface *iface_desc;
2436 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002437 int in_endpoint = 0;
2438 int out_endpoint = 0;
2439 int rc = -ENOMEM;
2440 int i;
2441 u32 protocols;
2442
2443 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2444 if (!dev)
2445 return -ENOMEM;
2446
2447 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2448 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002449 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002450
2451 iface_desc = interface->cur_altsetting;
2452 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2453 endpoint = &iface_desc->endpoint[i].desc;
2454
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002455 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002456 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002457
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002458 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002459 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002460 }
2461
2462 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002463 nfc_dev_err(&interface->dev,
2464 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002465 rc = -ENODEV;
2466 goto error;
2467 }
2468
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002469 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002470 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2471
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002472 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002473 goto error;
2474
2475 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002476 usb_rcvbulkpipe(dev->udev, in_endpoint),
2477 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002478 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002479 usb_sndbulkpipe(dev->udev, out_endpoint),
2480 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002481
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002482 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2483 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002484 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002485 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002486 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002487 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002488 if (dev->wq == NULL)
2489 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002490
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002491 init_timer(&dev->listen_timer);
2492 dev->listen_timer.data = (unsigned long) dev;
2493 dev->listen_timer.function = pn533_listen_mode_timer;
2494
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002495 skb_queue_head_init(&dev->resp_q);
2496
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002497 INIT_LIST_HEAD(&dev->cmd_queue);
2498
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002499 usb_set_intfdata(interface, dev);
2500
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002501 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002502
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002503 dev->device_type = id->driver_info;
2504 switch (dev->device_type) {
2505 case PN533_DEVICE_STD:
2506 protocols = PN533_ALL_PROTOCOLS;
2507 break;
2508
2509 case PN533_DEVICE_PASORI:
2510 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2511 break;
2512
2513 default:
2514 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2515 dev->device_type);
2516 rc = -EINVAL;
2517 goto destroy_wq;
2518 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002519
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002520 memset(&fw_ver, 0, sizeof(fw_ver));
2521 rc = pn533_get_firmware_version(dev, &fw_ver);
2522 if (rc < 0)
2523 goto destroy_wq;
2524
2525 nfc_dev_info(&dev->interface->dev,
2526 "NXP PN533 firmware ver %d.%d now attached",
2527 fw_ver.ver, fw_ver.rev);
2528
2529
Samuel Ortize8753042011-08-19 15:47:11 +02002530 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002531 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002532 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002533 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002534 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002535 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002536 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537
2538 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2539 nfc_set_drvdata(dev->nfc_dev, dev);
2540
2541 rc = nfc_register_device(dev->nfc_dev);
2542 if (rc)
2543 goto free_nfc_dev;
2544
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002545 rc = pn533_setup(dev);
2546 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002547 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002548
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549 return 0;
2550
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002551unregister_nfc_dev:
2552 nfc_unregister_device(dev->nfc_dev);
2553
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002554free_nfc_dev:
2555 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002556
Samuel Ortiz4849f852012-04-10 19:43:17 +02002557destroy_wq:
2558 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002559error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002560 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002561 usb_free_urb(dev->out_urb);
2562 kfree(dev);
2563 return rc;
2564}
2565
2566static void pn533_disconnect(struct usb_interface *interface)
2567{
2568 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002569 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002570
2571 dev = usb_get_intfdata(interface);
2572 usb_set_intfdata(interface, NULL);
2573
2574 nfc_unregister_device(dev->nfc_dev);
2575 nfc_free_device(dev->nfc_dev);
2576
2577 usb_kill_urb(dev->in_urb);
2578 usb_kill_urb(dev->out_urb);
2579
Samuel Ortiz4849f852012-04-10 19:43:17 +02002580 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002581
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002582 skb_queue_purge(&dev->resp_q);
2583
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002584 del_timer(&dev->listen_timer);
2585
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002586 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2587 list_del(&cmd->queue);
2588 kfree(cmd);
2589 }
2590
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002591 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002592 usb_free_urb(dev->out_urb);
2593 kfree(dev);
2594
Dan Carpenter276556d2011-07-08 10:21:15 +03002595 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002596}
2597
2598static struct usb_driver pn533_driver = {
2599 .name = "pn533",
2600 .probe = pn533_probe,
2601 .disconnect = pn533_disconnect,
2602 .id_table = pn533_table,
2603};
2604
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002605module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002606
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002607MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2608MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2609MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002610MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2611MODULE_VERSION(VERSION);
2612MODULE_LICENSE("GPL");