blob: 48902e58cacb9d256fcfd6b4d6f5619a337f9531 [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;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200889 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200890
891 mutex_lock(&dev->cmd_lock);
892
893 if (list_empty(&dev->cmd_queue)) {
894 dev->cmd_pending = 0;
895 mutex_unlock(&dev->cmd_lock);
896 return;
897 }
898
899 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
900
Szymon Janc60ad07a2012-10-25 17:29:45 +0200901 list_del(&cmd->queue);
902
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200903 mutex_unlock(&dev->cmd_lock);
904
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200905 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
906 pn533_send_async_complete, cmd->arg);
907 if (rc < 0) {
908 dev_kfree_skb(cmd->req);
909 dev_kfree_skb(cmd->resp);
910 kfree(cmd->arg);
911 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200912
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200913 kfree(cmd);
914}
915
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300916struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100917 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300918 struct completion done;
919};
920
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100921static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
922 struct sk_buff *resp)
923{
924 struct pn533_sync_cmd_response *arg = _arg;
925
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100926 arg->resp = resp;
927 complete(&arg->done);
928
929 return 0;
930}
931
932/* pn533_send_cmd_sync
933 *
934 * Please note the req parameter is freed inside the function to
935 * limit a number of return value interpretations by the caller.
936 *
937 * 1. negative in case of error during TX path -> req should be freed
938 *
939 * 2. negative in case of error during RX path -> req should not be freed
940 * as it's been already freed at the begining of RX path by
941 * async_complete_cb.
942 *
943 * 3. valid pointer in case of succesfult RX path
944 *
945 * A caller has to check a return value with IS_ERR macro. If the test pass,
946 * the returned pointer is valid.
947 *
948 * */
949static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
950 struct sk_buff *req)
951{
952 int rc;
953 struct pn533_sync_cmd_response arg;
954
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100955 init_completion(&arg.done);
956
957 rc = pn533_send_cmd_async(dev, cmd_code, req,
958 pn533_send_sync_complete, &arg);
959 if (rc) {
960 dev_kfree_skb(req);
961 return ERR_PTR(rc);
962 }
963
964 wait_for_completion(&arg.done);
965
966 return arg.resp;
967}
968
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300969static void pn533_send_complete(struct urb *urb)
970{
971 struct pn533 *dev = urb->context;
972
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300973 switch (urb->status) {
974 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100975 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300976 case -ECONNRESET:
977 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100978 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100979 "The urb has been stopped (status %d)",
980 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300981 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100982 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300983 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100984 nfc_dev_err(&dev->interface->dev,
985 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300986 }
987}
988
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100989static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100990{
991 struct sk_buff *skb;
992
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100993 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100994 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100995 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100996
997 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100998 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100999
1000 return skb;
1001}
1002
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001003struct pn533_target_type_a {
1004 __be16 sens_res;
1005 u8 sel_res;
1006 u8 nfcid_len;
1007 u8 nfcid_data[];
1008} __packed;
1009
1010
1011#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1012#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1013#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1014
1015#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1016#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1017
1018#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1019#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1020
1021#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1022#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1023#define PN533_TYPE_A_SEL_PROT_DEP 2
1024#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1025
1026static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1027 int target_data_len)
1028{
1029 u8 ssd;
1030 u8 platconf;
1031
1032 if (target_data_len < sizeof(struct pn533_target_type_a))
1033 return false;
1034
1035 /* The lenght check of nfcid[] and ats[] are not being performed because
1036 the values are not being used */
1037
1038 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1039 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1040 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1041
1042 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001043 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1044 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1045 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001046 return false;
1047
1048 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1049 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1050 return false;
1051
1052 return true;
1053}
1054
1055static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1056 int tgt_data_len)
1057{
1058 struct pn533_target_type_a *tgt_type_a;
1059
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001060 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001061
1062 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1063 return -EPROTO;
1064
1065 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1066 case PN533_TYPE_A_SEL_PROT_MIFARE:
1067 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1068 break;
1069 case PN533_TYPE_A_SEL_PROT_ISO14443:
1070 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1071 break;
1072 case PN533_TYPE_A_SEL_PROT_DEP:
1073 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1074 break;
1075 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1076 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1077 NFC_PROTO_NFC_DEP_MASK;
1078 break;
1079 }
1080
1081 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1082 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001083 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1084 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001085
1086 return 0;
1087}
1088
1089struct pn533_target_felica {
1090 u8 pol_res;
1091 u8 opcode;
1092 u8 nfcid2[8];
1093 u8 pad[8];
1094 /* optional */
1095 u8 syst_code[];
1096} __packed;
1097
1098#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1099#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1100
1101static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1102 int target_data_len)
1103{
1104 if (target_data_len < sizeof(struct pn533_target_felica))
1105 return false;
1106
1107 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1108 return false;
1109
1110 return true;
1111}
1112
1113static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1114 int tgt_data_len)
1115{
1116 struct pn533_target_felica *tgt_felica;
1117
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001118 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001119
1120 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1121 return -EPROTO;
1122
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001123 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1124 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001125 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1126 else
1127 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1128
Samuel Ortiz79757542012-03-05 01:03:45 +01001129 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1130 nfc_tgt->sensf_res_len = 9;
1131
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001132 return 0;
1133}
1134
1135struct pn533_target_jewel {
1136 __be16 sens_res;
1137 u8 jewelid[4];
1138} __packed;
1139
1140static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1141 int target_data_len)
1142{
1143 u8 ssd;
1144 u8 platconf;
1145
1146 if (target_data_len < sizeof(struct pn533_target_jewel))
1147 return false;
1148
1149 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1150 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1151 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1152
1153 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001154 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1155 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1156 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001157 return false;
1158
1159 return true;
1160}
1161
1162static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1163 int tgt_data_len)
1164{
1165 struct pn533_target_jewel *tgt_jewel;
1166
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001167 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001168
1169 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1170 return -EPROTO;
1171
1172 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1173 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001174 nfc_tgt->nfcid1_len = 4;
1175 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001176
1177 return 0;
1178}
1179
1180struct pn533_type_b_prot_info {
1181 u8 bitrate;
1182 u8 fsci_type;
1183 u8 fwi_adc_fo;
1184} __packed;
1185
1186#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1187#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1188#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1189
1190struct pn533_type_b_sens_res {
1191 u8 opcode;
1192 u8 nfcid[4];
1193 u8 appdata[4];
1194 struct pn533_type_b_prot_info prot_info;
1195} __packed;
1196
1197#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1198
1199struct pn533_target_type_b {
1200 struct pn533_type_b_sens_res sensb_res;
1201 u8 attrib_res_len;
1202 u8 attrib_res[];
1203} __packed;
1204
1205static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1206 int target_data_len)
1207{
1208 if (target_data_len < sizeof(struct pn533_target_type_b))
1209 return false;
1210
1211 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1212 return false;
1213
1214 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1215 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1216 return false;
1217
1218 return true;
1219}
1220
1221static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1222 int tgt_data_len)
1223{
1224 struct pn533_target_type_b *tgt_type_b;
1225
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001226 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001227
1228 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1229 return -EPROTO;
1230
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001231 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001232
1233 return 0;
1234}
1235
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001236static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1237 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001238{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001239 struct nfc_target nfc_tgt;
1240 int rc;
1241
1242 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001243 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001244
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001245 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001246 return -EPROTO;
1247
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001248 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1249
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001250 switch (dev->poll_mod_curr) {
1251 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001252 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001253 break;
1254 case PN533_POLL_MOD_212KBPS_FELICA:
1255 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257 break;
1258 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001259 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001260 break;
1261 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001262 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001263 break;
1264 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001265 nfc_dev_err(&dev->interface->dev,
1266 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001267 return -EPROTO;
1268 }
1269
1270 if (rc)
1271 return rc;
1272
1273 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001274 nfc_dev_dbg(&dev->interface->dev,
1275 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001276 return -EAGAIN;
1277 }
1278
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001279 nfc_dev_dbg(&dev->interface->dev,
1280 "Target found - supported protocols: 0x%x",
1281 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001282
1283 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1284
1285 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1286
1287 return 0;
1288}
1289
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001290static inline void pn533_poll_next_mod(struct pn533 *dev)
1291{
1292 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1293}
1294
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295static void pn533_poll_reset_mod_list(struct pn533 *dev)
1296{
1297 dev->poll_mod_count = 0;
1298}
1299
1300static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1301{
1302 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001303 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001304 dev->poll_mod_count++;
1305}
1306
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001307static void pn533_poll_create_mod_list(struct pn533 *dev,
1308 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309{
1310 pn533_poll_reset_mod_list(dev);
1311
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001312 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1313 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1314 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001315 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1316
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001317 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1318 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001319 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1320 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1321 }
1322
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001323 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001324 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1325
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001326 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001328
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001329 if (tm_protocols)
1330 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001331}
1332
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001333static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001334{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001335 u8 nbtg, tg, *tgdata;
1336 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001337
1338 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1339
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001340 nbtg = resp->data[0];
1341 tg = resp->data[1];
1342 tgdata = &resp->data[2];
1343 tgdata_len = resp->len - 2; /* nbtg + tg */
1344
1345 if (nbtg) {
1346 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001347
1348 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001349 if (rc == 0) {
1350 pn533_poll_reset_mod_list(dev);
1351 return 0;
1352 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001353 }
1354
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001355 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001356}
1357
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001358static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001359{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001360 struct sk_buff *skb;
1361 u8 *felica, *nfcid3, *gb;
1362
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001363 u8 *gbytes = dev->gb;
1364 size_t gbytes_len = dev->gb_len;
1365
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001366 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1367 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1368 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1369 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001370
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001371 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1372 0x0, 0x0, 0x0,
1373 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001374
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001375 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1376 felica (18), nfcid3 (10), gb_len (1) */
1377 gbytes_len +
1378 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001379
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001380 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001381 if (!skb)
1382 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001383
1384 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001385 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001386
1387 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001388 memcpy(skb_put(skb, 6), mifare_params, 6);
1389
1390 /* Felica params */
1391 felica = skb_put(skb, 18);
1392 memcpy(felica, felica_params, 18);
1393 get_random_bytes(felica + 2, 6);
1394
1395 /* NFCID3 */
1396 nfcid3 = skb_put(skb, 10);
1397 memset(nfcid3, 0, 10);
1398 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001399
1400 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001401 *skb_put(skb, 1) = gbytes_len;
1402
1403 gb = skb_put(skb, gbytes_len);
1404 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001405
Samuel Ortizad3823c2012-05-30 23:54:55 +02001406 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001407 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001408
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001409 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001410}
1411
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001412#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001413#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1414static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001415 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001416{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001417 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001418
1419 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1420
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001421 if (IS_ERR(resp))
1422 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001423
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001424 status = resp->data[0];
1425 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001426
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001427 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001428 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001429 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001430 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001431 return 0;
1432 }
1433
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001434 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001435}
1436
1437static void pn533_wq_tg_get_data(struct work_struct *work)
1438{
1439 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001440
1441 struct sk_buff *skb;
1442 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001443
1444 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1445
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001446 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001447 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001448 return;
1449
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001450 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1451 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001452
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001453 if (rc < 0)
1454 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001455
1456 return;
1457}
1458
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001459#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001461{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001463 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001464 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001465
1466 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1467
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001468 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001469 return -EINVAL;
1470
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 mode = resp->data[0];
1472 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001473
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1475 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001476
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001477 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1478 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001479 comm_mode = NFC_COMM_ACTIVE;
1480
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001481 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001482 return -EOPNOTSUPP;
1483
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001484 gb = cmd + ATR_REQ_GB_OFFSET;
1485 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001486
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001487 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1488 comm_mode, gb, gb_len);
1489 if (rc < 0) {
1490 nfc_dev_err(&dev->interface->dev,
1491 "Error when signaling target activation");
1492 return rc;
1493 }
1494
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001495 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001496 queue_work(dev->wq, &dev->tg_work);
1497
1498 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001499}
1500
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001501static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001502{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001503 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001504
1505 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1506
1507 /* An ack will cancel the last issued command (poll) */
1508 pn533_send_ack(dev, GFP_ATOMIC);
1509
1510 dev->cancel_listen = 1;
1511
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001512 pn533_poll_next_mod(dev);
1513
1514 queue_work(dev->wq, &dev->poll_work);
1515}
1516
1517static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001518 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001519{
1520 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001521 int rc;
1522
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001523 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1524
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001525 if (IS_ERR(resp)) {
1526 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001527
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001528 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1529 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001530
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001531 if (rc == -ENOENT) {
1532 if (dev->poll_mod_count != 0)
1533 return rc;
1534 else
1535 goto stop_poll;
1536 } else if (rc < 0) {
1537 nfc_dev_err(&dev->interface->dev,
1538 "Error %d when running poll", rc);
1539 goto stop_poll;
1540 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001541 }
1542
1543 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1544
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001545 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001546 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001547 rc = pn533_init_target_complete(dev, resp);
1548 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001549 }
1550
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 /* Initiator mode */
1552 rc = pn533_start_poll_complete(dev, resp);
1553 if (!rc)
1554 goto done;
1555
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001556 if (!dev->poll_mod_count) {
1557 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1558 goto done;
1559 }
1560
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001561 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562 queue_work(dev->wq, &dev->poll_work);
1563
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001564done:
1565 dev_kfree_skb(resp);
1566 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001567
1568stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001569 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1570
Samuel Ortizad3823c2012-05-30 23:54:55 +02001571 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001572 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001573 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001574}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001575
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001576static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1577 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001578{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001580
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001581 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 if (!skb)
1583 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001584
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001586
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001587 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001588}
1589
1590static int pn533_send_poll_frame(struct pn533 *dev)
1591{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 struct pn533_poll_modulations *mod;
1593 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001594 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001596
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001597 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001598
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001599 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1600 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001601
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602 if (mod->len == 0) { /* Listen mode */
1603 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001604 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001605 } else { /* Polling mode */
1606 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001607 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001608 }
1609
1610 if (!skb) {
1611 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1612 return -ENOMEM;
1613 }
1614
1615 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1616 NULL);
1617 if (rc < 0) {
1618 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001619 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001620 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001621
1622 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001623}
1624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001626{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001627 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1628 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001629 int rc;
1630
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001631 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1632
1633 nfc_dev_dbg(&dev->interface->dev,
1634 "%s cancel_listen %d modulation len %d",
1635 __func__, dev->cancel_listen, cur_mod->len);
1636
1637 if (dev->cancel_listen == 1) {
1638 dev->cancel_listen = 0;
1639 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001640 }
1641
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001642 rc = pn533_send_poll_frame(dev);
1643 if (rc)
1644 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001645
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001646 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1647 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001648
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001649 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001650}
1651
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001652static int pn533_start_poll(struct nfc_dev *nfc_dev,
1653 u32 im_protocols, u32 tm_protocols)
1654{
1655 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1656
1657 nfc_dev_dbg(&dev->interface->dev,
1658 "%s: im protocols 0x%x tm protocols 0x%x",
1659 __func__, im_protocols, tm_protocols);
1660
1661 if (dev->tgt_active_prot) {
1662 nfc_dev_err(&dev->interface->dev,
1663 "Cannot poll with a target already activated");
1664 return -EBUSY;
1665 }
1666
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001667 if (dev->tgt_mode) {
1668 nfc_dev_err(&dev->interface->dev,
1669 "Cannot poll while already being activated");
1670 return -EBUSY;
1671 }
1672
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001673 if (tm_protocols) {
1674 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1675 if (dev->gb == NULL)
1676 tm_protocols = 0;
1677 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001678
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001679 dev->poll_mod_curr = 0;
1680 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1681 dev->poll_protocols = im_protocols;
1682 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001683
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001684 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001685}
1686
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001687static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1688{
1689 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1690
1691 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1692
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001693 del_timer(&dev->listen_timer);
1694
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001695 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001696 nfc_dev_dbg(&dev->interface->dev,
1697 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698 return;
1699 }
1700
1701 /* An ack will cancel the last issued command (poll) */
1702 pn533_send_ack(dev, GFP_KERNEL);
1703
1704 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1705 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001706
1707 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001708}
1709
1710static int pn533_activate_target_nfcdep(struct pn533 *dev)
1711{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001712 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001713 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714 int rc;
1715
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001716 struct sk_buff *skb;
1717 struct sk_buff *resp;
1718
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001719 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1720
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001721 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001722 if (!skb)
1723 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001724
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1726 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001727
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001728 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1729 if (IS_ERR(resp))
1730 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001731
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001732 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001733 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001734 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001735 nfc_dev_err(&dev->interface->dev,
1736 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001737 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001738 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001739 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001740
Samuel Ortiz541d9202011-12-14 16:43:10 +01001741 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001742 gt_len = resp->len - 16;
1743 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001744
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001745 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001746 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747}
1748
Eric Lapuyade90099432012-05-07 12:31:13 +02001749static int pn533_activate_target(struct nfc_dev *nfc_dev,
1750 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751{
1752 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1753 int rc;
1754
1755 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001756 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001757
1758 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001759 nfc_dev_err(&dev->interface->dev,
1760 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001761 return -EBUSY;
1762 }
1763
1764 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001765 nfc_dev_err(&dev->interface->dev,
1766 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 return -EBUSY;
1768 }
1769
1770 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001771 nfc_dev_err(&dev->interface->dev,
1772 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001773 return -EINVAL;
1774 }
1775
1776 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001777 nfc_dev_err(&dev->interface->dev,
1778 "Target doesn't support requested proto %u",
1779 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001780 return -EINVAL;
1781 }
1782
1783 if (protocol == NFC_PROTO_NFC_DEP) {
1784 rc = pn533_activate_target_nfcdep(dev);
1785 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001786 nfc_dev_err(&dev->interface->dev,
1787 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001788 return rc;
1789 }
1790 }
1791
1792 dev->tgt_active_prot = protocol;
1793 dev->tgt_available_prots = 0;
1794
1795 return 0;
1796}
1797
Eric Lapuyade90099432012-05-07 12:31:13 +02001798static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1799 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800{
1801 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001802
1803 struct sk_buff *skb;
1804 struct sk_buff *resp;
1805
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001806 int rc;
1807
1808 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1809
1810 if (!dev->tgt_active_prot) {
1811 nfc_dev_err(&dev->interface->dev, "There is no active target");
1812 return;
1813 }
1814
1815 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001816 skb_queue_purge(&dev->resp_q);
1817
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001818 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001819 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001820 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001821
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001822 *skb_put(skb, 1) = 1; /* TG*/
1823
1824 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1825 if (IS_ERR(resp))
1826 return;
1827
1828 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001829 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001830 nfc_dev_err(&dev->interface->dev,
1831 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001832
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001833 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001834 return;
1835}
1836
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837
1838static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001842 u8 target_gt_len;
1843 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001844 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001845
1846 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001847
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001848 if (IS_ERR(resp))
1849 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001850
1851 if (dev->tgt_available_prots &&
1852 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1853 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001854 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855 rc = -EINVAL;
1856 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001857 }
1858
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001859 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1860
1861 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862 if (rc != PN533_CMD_RET_SUCCESS) {
1863 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001864 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001865 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001866 }
1867
1868 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 struct nfc_target nfc_target;
1870
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001871 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1872
1873 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001874 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001875 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1877 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001878 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879
1880 dev->tgt_available_prots = 0;
1881 }
1882
1883 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1884
1885 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001886 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001887 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001888 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001889 if (rc == 0)
1890 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001891 dev->nfc_dev->targets[0].idx,
1892 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001893
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001894error:
1895 dev_kfree_skb(resp);
1896 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001897}
1898
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001899static int pn533_mod_to_baud(struct pn533 *dev)
1900{
1901 switch (dev->poll_mod_curr) {
1902 case PN533_POLL_MOD_106KBPS_A:
1903 return 0;
1904 case PN533_POLL_MOD_212KBPS_FELICA:
1905 return 1;
1906 case PN533_POLL_MOD_424KBPS_FELICA:
1907 return 2;
1908 default:
1909 return -EINVAL;
1910 }
1911}
1912
Samuel Ortizd7f33452012-05-29 21:45:21 +02001913#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001914static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001915 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001916{
1917 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001918 struct sk_buff *skb;
1919 int rc, baud, skb_len;
1920 u8 *next, *arg;
1921
Samuel Ortizd7f33452012-05-29 21:45:21 +02001922 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001923
1924 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1925
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001926 if (dev->poll_mod_count) {
1927 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001928 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001929 return -EBUSY;
1930 }
1931
1932 if (dev->tgt_active_prot) {
1933 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001934 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001935 return -EBUSY;
1936 }
1937
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001938 baud = pn533_mod_to_baud(dev);
1939 if (baud < 0) {
1940 nfc_dev_err(&dev->interface->dev,
1941 "Invalid curr modulation %d", dev->poll_mod_curr);
1942 return baud;
1943 }
1944
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001945 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001946 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001947 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001948
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001949 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001950 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001951 return -ENOMEM;
1952
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001953 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1954 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001955
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001956 next = skb_put(skb, 1); /* Next */
1957 *next = 0;
1958
1959 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1960 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1961 PASSIVE_DATA_LEN);
1962 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001963 }
1964
Samuel Ortiz47807d32012-03-05 01:03:50 +01001965 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001966 memcpy(skb_put(skb, gb_len), gb, gb_len);
1967 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001968 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001969 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001970 }
1971
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001972 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1973 if (!arg) {
1974 dev_kfree_skb(skb);
1975 return -ENOMEM;
1976 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001977
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001978 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001979
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001980 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1981 pn533_in_dep_link_up_complete, arg);
1982
1983 if (rc < 0) {
1984 dev_kfree_skb(skb);
1985 kfree(arg);
1986 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001987
1988 return rc;
1989}
1990
1991static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1992{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001993 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1994
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001995 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1996
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001997 pn533_poll_reset_mod_list(dev);
1998
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001999 if (dev->tgt_mode || dev->tgt_active_prot) {
2000 pn533_send_ack(dev, GFP_KERNEL);
2001 usb_kill_urb(dev->in_urb);
2002 }
2003
2004 dev->tgt_active_prot = 0;
2005 dev->tgt_mode = 0;
2006
2007 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002008
2009 return 0;
2010}
2011
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002012struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002013 data_exchange_cb_t cb;
2014 void *cb_context;
2015};
2016
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002017static struct sk_buff *pn533_build_response(struct pn533 *dev)
2018{
2019 struct sk_buff *skb, *tmp, *t;
2020 unsigned int skb_len = 0, tmp_len = 0;
2021
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002022 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002023
2024 if (skb_queue_empty(&dev->resp_q))
2025 return NULL;
2026
2027 if (skb_queue_len(&dev->resp_q) == 1) {
2028 skb = skb_dequeue(&dev->resp_q);
2029 goto out;
2030 }
2031
2032 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2033 skb_len += tmp->len;
2034
2035 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2036 __func__, skb_len);
2037
2038 skb = alloc_skb(skb_len, GFP_KERNEL);
2039 if (skb == NULL)
2040 goto out;
2041
2042 skb_put(skb, skb_len);
2043
2044 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2045 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2046 tmp_len += tmp->len;
2047 }
2048
2049out:
2050 skb_queue_purge(&dev->resp_q);
2051
2052 return skb;
2053}
2054
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002055static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002056 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002057{
2058 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002059 struct sk_buff *skb;
2060 int rc = 0;
2061 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002062
2063 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2064
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002065 if (IS_ERR(resp)) {
2066 rc = PTR_ERR(resp);
2067 goto _error;
2068 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002069
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002070 status = resp->data[0];
2071 ret = status & PN533_CMD_RET_MASK;
2072 mi = status & PN533_CMD_MI_MASK;
2073
2074 skb_pull(resp, sizeof(status));
2075
2076 if (ret != PN533_CMD_RET_SUCCESS) {
2077 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002078 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002079 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002080 goto error;
2081 }
2082
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002083 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002084
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002085 if (mi) {
2086 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002087 queue_work(dev->wq, &dev->mi_work);
2088 return -EINPROGRESS;
2089 }
2090
2091 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002092 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002093 goto error;
2094
2095 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002096 kfree(arg);
2097 return 0;
2098
2099error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002100 dev_kfree_skb(resp);
2101_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002102 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002103 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002104 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002105 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002106}
2107
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002108static int pn533_transceive(struct nfc_dev *nfc_dev,
2109 struct nfc_target *target, struct sk_buff *skb,
2110 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111{
2112 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002113 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002114 int rc;
2115
2116 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2117
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002118 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2119 /* TODO: Implement support to multi-part data exchange */
2120 nfc_dev_err(&dev->interface->dev,
2121 "Data length greater than the max allowed: %d",
2122 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2123 rc = -ENOSYS;
2124 goto error;
2125 }
2126
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002127 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002128 nfc_dev_err(&dev->interface->dev,
2129 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130 rc = -EINVAL;
2131 goto error;
2132 }
2133
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002134 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002135 if (!arg) {
2136 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002137 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002138 }
2139
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002140 arg->cb = cb;
2141 arg->cb_context = cb_context;
2142
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002143 switch (dev->device_type) {
2144 case PN533_DEVICE_PASORI:
2145 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2146 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2147 skb,
2148 pn533_data_exchange_complete,
2149 arg);
2150
2151 break;
2152 }
2153 default:
2154 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2155
2156 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2157 skb, pn533_data_exchange_complete,
2158 arg);
2159
2160 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161 }
2162
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002163 if (rc < 0) /* rc from send_async */
2164 goto error;
2165
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002166 return 0;
2167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002168error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002169 kfree(arg);
2170 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002171 return rc;
2172}
2173
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002174static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002175 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002176{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002177 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002178
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002179 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2180
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002181 if (IS_ERR(resp))
2182 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002183
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002184 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002185
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002186 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002187
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002188 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002189 nfc_tm_deactivated(dev->nfc_dev);
2190
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002191 dev->tgt_mode = 0;
2192
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002193 return 0;
2194 }
2195
2196 queue_work(dev->wq, &dev->tg_work);
2197
2198 return 0;
2199}
2200
2201static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2202{
2203 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002204 int rc;
2205
2206 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2207
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002208 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002209 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002210 "Data length greater than the max allowed: %d",
2211 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2212 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002213 }
2214
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002215 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2216 pn533_tm_send_complete, NULL);
2217 if (rc < 0)
2218 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002219
2220 return rc;
2221}
2222
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002223static void pn533_wq_mi_recv(struct work_struct *work)
2224{
2225 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002226
2227 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002228 int rc;
2229
2230 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2231
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002232 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002233 if (!skb)
2234 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002235
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002236 switch (dev->device_type) {
2237 case PN533_DEVICE_PASORI:
2238 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2239 rc = pn533_send_cmd_direct_async(dev,
2240 PN533_CMD_IN_COMM_THRU,
2241 skb,
2242 pn533_data_exchange_complete,
2243 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002244
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 break;
2246 }
2247 default:
2248 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002249
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002250 rc = pn533_send_cmd_direct_async(dev,
2251 PN533_CMD_IN_DATA_EXCHANGE,
2252 skb,
2253 pn533_data_exchange_complete,
2254 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002255
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002256 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002257 }
2258
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002259 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002260 return;
2261
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002262 nfc_dev_err(&dev->interface->dev,
2263 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002264
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002265 dev_kfree_skb(skb);
2266 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002267
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002268error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002269 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002270 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002271}
2272
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002273static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2274 u8 cfgdata_len)
2275{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002276 struct sk_buff *skb;
2277 struct sk_buff *resp;
2278
2279 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002280
2281 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2282
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002283 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002284
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002285 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 if (!skb)
2287 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002288
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002289 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2290 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002291
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002292 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2293 if (IS_ERR(resp))
2294 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002295
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002296 dev_kfree_skb(resp);
2297 return 0;
2298}
2299
2300static int pn533_get_firmware_version(struct pn533 *dev,
2301 struct pn533_fw_version *fv)
2302{
2303 struct sk_buff *skb;
2304 struct sk_buff *resp;
2305
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002306 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002307 if (!skb)
2308 return -ENOMEM;
2309
2310 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2311 if (IS_ERR(resp))
2312 return PTR_ERR(resp);
2313
2314 fv->ic = resp->data[0];
2315 fv->ver = resp->data[1];
2316 fv->rev = resp->data[2];
2317 fv->support = resp->data[3];
2318
2319 dev_kfree_skb(resp);
2320 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002321}
2322
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002323static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002324{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002325 struct sk_buff *skb;
2326 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002327
2328 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2329
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002330 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002331 if (!skb)
2332 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002333
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002334 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002335
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002336 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2337 if (IS_ERR(resp))
2338 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002339
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002340 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002341
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002342 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002343}
2344
2345static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002346 .dev_up = NULL,
2347 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002348 .dep_link_up = pn533_dep_link_up,
2349 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002350 .start_poll = pn533_start_poll,
2351 .stop_poll = pn533_stop_poll,
2352 .activate_target = pn533_activate_target,
2353 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002354 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002355 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002356};
2357
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002358static int pn533_setup(struct pn533 *dev)
2359{
2360 struct pn533_config_max_retries max_retries;
2361 struct pn533_config_timing timing;
2362 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2363 int rc;
2364
2365 switch (dev->device_type) {
2366 case PN533_DEVICE_STD:
2367 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2368 max_retries.mx_rty_psl = 2;
2369 max_retries.mx_rty_passive_act =
2370 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2371
2372 timing.rfu = PN533_CONFIG_TIMING_102;
2373 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2374 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2375
2376 break;
2377
2378 case PN533_DEVICE_PASORI:
2379 max_retries.mx_rty_atr = 0x2;
2380 max_retries.mx_rty_psl = 0x1;
2381 max_retries.mx_rty_passive_act =
2382 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2383
2384 timing.rfu = PN533_CONFIG_TIMING_102;
2385 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2386 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2387
2388 break;
2389
2390 default:
2391 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2392 dev->device_type);
2393 return -EINVAL;
2394 }
2395
2396 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2397 (u8 *)&max_retries, sizeof(max_retries));
2398 if (rc) {
2399 nfc_dev_err(&dev->interface->dev,
2400 "Error on setting MAX_RETRIES config");
2401 return rc;
2402 }
2403
2404
2405 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2406 (u8 *)&timing, sizeof(timing));
2407 if (rc) {
2408 nfc_dev_err(&dev->interface->dev,
2409 "Error on setting RF timings");
2410 return rc;
2411 }
2412
2413 switch (dev->device_type) {
2414 case PN533_DEVICE_STD:
2415 break;
2416
2417 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002418 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002419
2420 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2421 pasori_cfg, 3);
2422 if (rc) {
2423 nfc_dev_err(&dev->interface->dev,
2424 "Error while settings PASORI config");
2425 return rc;
2426 }
2427
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002428 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002429
2430 break;
2431 }
2432
2433 return 0;
2434}
2435
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002436static int pn533_probe(struct usb_interface *interface,
2437 const struct usb_device_id *id)
2438{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002439 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002440 struct pn533 *dev;
2441 struct usb_host_interface *iface_desc;
2442 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002443 int in_endpoint = 0;
2444 int out_endpoint = 0;
2445 int rc = -ENOMEM;
2446 int i;
2447 u32 protocols;
2448
2449 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2450 if (!dev)
2451 return -ENOMEM;
2452
2453 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2454 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002455 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002456
2457 iface_desc = interface->cur_altsetting;
2458 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2459 endpoint = &iface_desc->endpoint[i].desc;
2460
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002461 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002462 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002464 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002465 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466 }
2467
2468 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002469 nfc_dev_err(&interface->dev,
2470 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002471 rc = -ENODEV;
2472 goto error;
2473 }
2474
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002475 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002476 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2477
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002478 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002479 goto error;
2480
2481 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002482 usb_rcvbulkpipe(dev->udev, in_endpoint),
2483 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002485 usb_sndbulkpipe(dev->udev, out_endpoint),
2486 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002488 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2489 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002490 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002491 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002492 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002493 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002494 if (dev->wq == NULL)
2495 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002496
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002497 init_timer(&dev->listen_timer);
2498 dev->listen_timer.data = (unsigned long) dev;
2499 dev->listen_timer.function = pn533_listen_mode_timer;
2500
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002501 skb_queue_head_init(&dev->resp_q);
2502
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002503 INIT_LIST_HEAD(&dev->cmd_queue);
2504
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505 usb_set_intfdata(interface, dev);
2506
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002507 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002508
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002509 dev->device_type = id->driver_info;
2510 switch (dev->device_type) {
2511 case PN533_DEVICE_STD:
2512 protocols = PN533_ALL_PROTOCOLS;
2513 break;
2514
2515 case PN533_DEVICE_PASORI:
2516 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2517 break;
2518
2519 default:
2520 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2521 dev->device_type);
2522 rc = -EINVAL;
2523 goto destroy_wq;
2524 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002525
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002526 memset(&fw_ver, 0, sizeof(fw_ver));
2527 rc = pn533_get_firmware_version(dev, &fw_ver);
2528 if (rc < 0)
2529 goto destroy_wq;
2530
2531 nfc_dev_info(&dev->interface->dev,
2532 "NXP PN533 firmware ver %d.%d now attached",
2533 fw_ver.ver, fw_ver.rev);
2534
2535
Samuel Ortize8753042011-08-19 15:47:11 +02002536 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002537 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002538 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002539 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002540 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002541 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002542 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002543
2544 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2545 nfc_set_drvdata(dev->nfc_dev, dev);
2546
2547 rc = nfc_register_device(dev->nfc_dev);
2548 if (rc)
2549 goto free_nfc_dev;
2550
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002551 rc = pn533_setup(dev);
2552 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002553 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002554
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555 return 0;
2556
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002557unregister_nfc_dev:
2558 nfc_unregister_device(dev->nfc_dev);
2559
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002560free_nfc_dev:
2561 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002562
Samuel Ortiz4849f852012-04-10 19:43:17 +02002563destroy_wq:
2564 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002565error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002566 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002567 usb_free_urb(dev->out_urb);
2568 kfree(dev);
2569 return rc;
2570}
2571
2572static void pn533_disconnect(struct usb_interface *interface)
2573{
2574 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002575 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002576
2577 dev = usb_get_intfdata(interface);
2578 usb_set_intfdata(interface, NULL);
2579
2580 nfc_unregister_device(dev->nfc_dev);
2581 nfc_free_device(dev->nfc_dev);
2582
2583 usb_kill_urb(dev->in_urb);
2584 usb_kill_urb(dev->out_urb);
2585
Samuel Ortiz4849f852012-04-10 19:43:17 +02002586 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002587
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002588 skb_queue_purge(&dev->resp_q);
2589
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002590 del_timer(&dev->listen_timer);
2591
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002592 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2593 list_del(&cmd->queue);
2594 kfree(cmd);
2595 }
2596
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002597 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002598 usb_free_urb(dev->out_urb);
2599 kfree(dev);
2600
Dan Carpenter276556d2011-07-08 10:21:15 +03002601 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602}
2603
2604static struct usb_driver pn533_driver = {
2605 .name = "pn533",
2606 .probe = pn533_probe,
2607 .disconnect = pn533_disconnect,
2608 .id_table = pn533_table,
2609};
2610
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002611module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002612
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002613MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2614MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2615MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002616MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2617MODULE_VERSION(VERSION);
2618MODULE_LICENSE("GPL");