blob: faf377aec73c106b2b8e70352be85c08cc96342f [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
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100333 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200334 struct mutex cmd_lock;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200335 struct pn533_cmd *cmd;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300336
337 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
338 u8 poll_mod_count;
339 u8 poll_mod_curr;
340 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200341 u32 listen_protocols;
342
343 u8 *gb;
344 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300345
346 u8 tgt_available_prots;
347 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200348 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200349
350 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200351
352 struct list_head cmd_queue;
353 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100354
355 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200356};
357
358struct pn533_cmd {
359 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200360 u8 code;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100361 struct sk_buff *req;
362 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100363 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200364 pn533_send_async_complete_t complete_cb;
365 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300366};
367
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200368struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300369 u8 preamble;
370 __be16 start_frame;
371 u8 datalen;
372 u8 datalen_checksum;
373 u8 data[];
374} __packed;
375
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100376struct pn533_frame_ops {
377 void (*tx_frame_init)(void *frame, u8 cmd_code);
378 void (*tx_frame_finish)(void *frame);
379 void (*tx_update_payload_len)(void *frame, int len);
380 int tx_header_len;
381 int tx_tail_len;
382
383 bool (*rx_is_frame_valid)(void *frame);
384 int (*rx_frame_size)(void *frame);
385 int rx_header_len;
386 int rx_tail_len;
387
388 int max_payload_len;
389 u8 (*get_cmd_code)(void *frame);
390};
391
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300392/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200393static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300394{
395 return ~value + 1;
396}
397
398/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200399static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300400{
401 u8 sum = 0;
402 int i;
403
404 for (i = 0; i < datalen; i++)
405 sum += data[i];
406
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200407 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300408}
409
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200410static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300411{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200412 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100413
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300414 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200415 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
416 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
417 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418 frame->datalen = 2;
419}
420
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200421static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300422{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200423 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100424
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200425 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300426
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200427 PN533_STD_FRAME_CHECKSUM(frame) =
428 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300429
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200430 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300431}
432
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200433static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100434{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200435 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100436
437 frame->datalen += len;
438}
439
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200440static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300441{
442 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200443 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300444
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200445 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300446 return false;
447
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200448 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300449 if (checksum != frame->datalen_checksum)
450 return false;
451
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200452 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
453 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300454 return false;
455
456 return true;
457}
458
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200459static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300460{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200461 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300462 return false;
463
464 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
465 return false;
466
467 return true;
468}
469
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200470static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300471{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200472 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100473
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200474 return sizeof(struct pn533_std_frame) + f->datalen +
475 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100476}
477
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200478static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100479{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200480 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100481
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200482 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100483}
484
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100485static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200486 .tx_frame_init = pn533_std_tx_frame_init,
487 .tx_frame_finish = pn533_std_tx_frame_finish,
488 .tx_update_payload_len = pn533_std_tx_update_payload_len,
489 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
490 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100491
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200492 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
493 .rx_frame_size = pn533_std_rx_frame_size,
494 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
495 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100496
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200497 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
498 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100499};
500
501static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
502{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200503 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200504 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300505}
506
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300507static void pn533_recv_response(struct urb *urb)
508{
509 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100510 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300512 switch (urb->status) {
513 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100514 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300515 case -ECONNRESET:
516 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100517 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100518 "The urb has been canceled (status %d)",
519 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200520 dev->wq_in_error = urb->status;
521 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100522 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100524 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100525 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200526 dev->wq_in_error = urb->status;
527 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528 }
529
530 in_frame = dev->in_urb->transfer_buffer;
531
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100532 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100533 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
534 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100535
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100536 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200538 dev->wq_in_error = -EIO;
539 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300540 }
541
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100542 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100543 nfc_dev_err(&dev->interface->dev,
544 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200545 dev->wq_in_error = -EIO;
546 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300547 }
548
Samuel Ortiz4849f852012-04-10 19:43:17 +0200549 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300550
Samuel Ortiz4849f852012-04-10 19:43:17 +0200551sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200552 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300553}
554
555static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
556{
557 dev->in_urb->complete = pn533_recv_response;
558
559 return usb_submit_urb(dev->in_urb, flags);
560}
561
562static void pn533_recv_ack(struct urb *urb)
563{
564 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200565 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300566 int rc;
567
568 switch (urb->status) {
569 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100570 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300571 case -ECONNRESET:
572 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100573 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100574 "The urb has been stopped (status %d)",
575 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200576 dev->wq_in_error = urb->status;
577 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100578 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300579 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100580 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100581 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200582 dev->wq_in_error = urb->status;
583 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300584 }
585
586 in_frame = dev->in_urb->transfer_buffer;
587
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200588 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300589 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200590 dev->wq_in_error = -EIO;
591 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300592 }
593
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300594 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
595 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100596 nfc_dev_err(&dev->interface->dev,
597 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200598 dev->wq_in_error = rc;
599 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300600 }
601
602 return;
603
Samuel Ortiz4849f852012-04-10 19:43:17 +0200604sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200605 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606}
607
608static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
609{
610 dev->in_urb->complete = pn533_recv_ack;
611
612 return usb_submit_urb(dev->in_urb, flags);
613}
614
615static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
616{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200617 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100618 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300619 int rc;
620
621 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
622
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100623 dev->out_urb->transfer_buffer = ack;
624 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300625 rc = usb_submit_urb(dev->out_urb, flags);
626
627 return rc;
628}
629
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100630static int __pn533_send_frame_async(struct pn533 *dev,
631 struct sk_buff *out,
632 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200633 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300634{
635 int rc;
636
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100637 dev->out_urb->transfer_buffer = out->data;
638 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300639
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100640 dev->in_urb->transfer_buffer = in->data;
641 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300642
Samuel Ortize279f842013-03-20 11:27:57 +0100643 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
644 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100645
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100646 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300647 if (rc)
648 return rc;
649
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100650 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651 if (rc)
652 goto error;
653
654 return 0;
655
656error:
657 usb_unlink_urb(dev->out_urb);
658 return rc;
659}
660
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100661static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
662 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100663{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100664 /* payload is already there, just update datalen */
665 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100666 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100667
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100668
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100669 skb_push(skb, ops->tx_header_len);
670 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100671
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100672 ops->tx_frame_init(skb->data, cmd_code);
673 ops->tx_update_payload_len(skb->data, payload_len);
674 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100675}
676
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200677static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100678{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200679 struct pn533_cmd *cmd = dev->cmd;
680 int status = dev->wq_in_error;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200682 struct sk_buff *req = cmd->req;
683 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100685 int rc;
686
687 dev_kfree_skb(req);
688
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100689 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200690 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
691 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100692 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200693 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100694 }
695
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100696 skb_put(resp, dev->ops->rx_frame_size(resp->data));
697 skb_pull(resp, dev->ops->rx_header_len);
698 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100699
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200700 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100701
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200702done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200703 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200704 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100705 return rc;
706}
707
708static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
709 struct sk_buff *req, struct sk_buff *resp,
710 int resp_len,
711 pn533_send_async_complete_t complete_cb,
712 void *complete_cb_context)
713{
714 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100715 int rc = 0;
716
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100717 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100718
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200719 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
720 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100721 return -ENOMEM;
722
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200723 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200724 cmd->req = req;
725 cmd->resp = resp;
726 cmd->resp_len = resp_len;
727 cmd->complete_cb = complete_cb;
728 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100729
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100730 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100731
732 mutex_lock(&dev->cmd_lock);
733
734 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200735 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100736 if (rc)
737 goto error;
738
739 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200740 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100741 goto unlock;
742 }
743
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100744 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
745 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100746
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100747 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100748 list_add_tail(&cmd->queue, &dev->cmd_queue);
749
750 goto unlock;
751
752error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200753 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100754unlock:
755 mutex_unlock(&dev->cmd_lock);
756 return rc;
757}
758
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100759static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
760 struct sk_buff *req,
761 pn533_send_async_complete_t complete_cb,
762 void *complete_cb_context)
763{
764 struct sk_buff *resp;
765 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100766 int resp_len = dev->ops->rx_header_len +
767 dev->ops->max_payload_len +
768 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100769
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100770 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
771 if (!resp)
772 return -ENOMEM;
773
774 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
775 complete_cb_context);
776 if (rc)
777 dev_kfree_skb(resp);
778
779 return rc;
780}
781
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100782static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
783 struct sk_buff *req,
784 pn533_send_async_complete_t complete_cb,
785 void *complete_cb_context)
786{
787 struct sk_buff *resp;
788 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100789 int resp_len = dev->ops->rx_header_len +
790 dev->ops->max_payload_len +
791 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100792
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100793 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100794 if (!resp)
795 return -ENOMEM;
796
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100797 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
798 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100799 if (rc)
800 dev_kfree_skb(resp);
801
802 return rc;
803}
804
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100805/*
806 * pn533_send_cmd_direct_async
807 *
808 * The function sends a piority cmd directly to the chip omiting the cmd
809 * queue. It's intended to be used by chaining mechanism of received responses
810 * where the host has to request every single chunk of data before scheduling
811 * next cmd from the queue.
812 */
813static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
814 struct sk_buff *req,
815 pn533_send_async_complete_t complete_cb,
816 void *complete_cb_context)
817{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100818 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200819 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100820 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100821 int resp_len = dev->ops->rx_header_len +
822 dev->ops->max_payload_len +
823 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100824
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100825 resp = alloc_skb(resp_len, GFP_KERNEL);
826 if (!resp)
827 return -ENOMEM;
828
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200829 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
830 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100831 dev_kfree_skb(resp);
832 return -ENOMEM;
833 }
834
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200835 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200836 cmd->req = req;
837 cmd->resp = resp;
838 cmd->resp_len = resp_len;
839 cmd->complete_cb = complete_cb;
840 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100841
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100842 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100843
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200844 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100845 if (rc < 0) {
846 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200847 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200848 } else {
849 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100850 }
851
852 return rc;
853}
854
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +0200855static void pn533_wq_cmd_complete(struct work_struct *work)
856{
857 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
858 int rc;
859
860 rc = pn533_send_async_complete(dev);
861 if (rc != -EINPROGRESS)
862 queue_work(dev->wq, &dev->cmd_work);
863}
864
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200865static void pn533_wq_cmd(struct work_struct *work)
866{
867 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
868 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200869 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200870
871 mutex_lock(&dev->cmd_lock);
872
873 if (list_empty(&dev->cmd_queue)) {
874 dev->cmd_pending = 0;
875 mutex_unlock(&dev->cmd_lock);
876 return;
877 }
878
879 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
880
Szymon Janc60ad07a2012-10-25 17:29:45 +0200881 list_del(&cmd->queue);
882
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200883 mutex_unlock(&dev->cmd_lock);
884
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200885 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200886 if (rc < 0) {
887 dev_kfree_skb(cmd->req);
888 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200889 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200890 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200891 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200892
893 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200894}
895
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300896struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100897 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300898 struct completion done;
899};
900
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100901static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
902 struct sk_buff *resp)
903{
904 struct pn533_sync_cmd_response *arg = _arg;
905
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100906 arg->resp = resp;
907 complete(&arg->done);
908
909 return 0;
910}
911
912/* pn533_send_cmd_sync
913 *
914 * Please note the req parameter is freed inside the function to
915 * limit a number of return value interpretations by the caller.
916 *
917 * 1. negative in case of error during TX path -> req should be freed
918 *
919 * 2. negative in case of error during RX path -> req should not be freed
920 * as it's been already freed at the begining of RX path by
921 * async_complete_cb.
922 *
923 * 3. valid pointer in case of succesfult RX path
924 *
925 * A caller has to check a return value with IS_ERR macro. If the test pass,
926 * the returned pointer is valid.
927 *
928 * */
929static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
930 struct sk_buff *req)
931{
932 int rc;
933 struct pn533_sync_cmd_response arg;
934
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100935 init_completion(&arg.done);
936
937 rc = pn533_send_cmd_async(dev, cmd_code, req,
938 pn533_send_sync_complete, &arg);
939 if (rc) {
940 dev_kfree_skb(req);
941 return ERR_PTR(rc);
942 }
943
944 wait_for_completion(&arg.done);
945
946 return arg.resp;
947}
948
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300949static void pn533_send_complete(struct urb *urb)
950{
951 struct pn533 *dev = urb->context;
952
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300953 switch (urb->status) {
954 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100955 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300956 case -ECONNRESET:
957 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100958 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100959 "The urb has been stopped (status %d)",
960 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300961 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100962 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300963 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100964 nfc_dev_err(&dev->interface->dev,
965 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300966 }
967}
968
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100969static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100970{
971 struct sk_buff *skb;
972
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100973 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100974 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100975 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100976
977 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100978 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100979
980 return skb;
981}
982
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300983struct pn533_target_type_a {
984 __be16 sens_res;
985 u8 sel_res;
986 u8 nfcid_len;
987 u8 nfcid_data[];
988} __packed;
989
990
991#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
992#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
993#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
994
995#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
996#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
997
998#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
999#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1000
1001#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1002#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1003#define PN533_TYPE_A_SEL_PROT_DEP 2
1004#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1005
1006static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1007 int target_data_len)
1008{
1009 u8 ssd;
1010 u8 platconf;
1011
1012 if (target_data_len < sizeof(struct pn533_target_type_a))
1013 return false;
1014
1015 /* The lenght check of nfcid[] and ats[] are not being performed because
1016 the values are not being used */
1017
1018 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1019 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1020 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1021
1022 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001023 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1024 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1025 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001026 return false;
1027
1028 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1029 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1030 return false;
1031
1032 return true;
1033}
1034
1035static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1036 int tgt_data_len)
1037{
1038 struct pn533_target_type_a *tgt_type_a;
1039
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001040 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001041
1042 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1043 return -EPROTO;
1044
1045 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1046 case PN533_TYPE_A_SEL_PROT_MIFARE:
1047 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1048 break;
1049 case PN533_TYPE_A_SEL_PROT_ISO14443:
1050 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1051 break;
1052 case PN533_TYPE_A_SEL_PROT_DEP:
1053 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1054 break;
1055 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1056 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1057 NFC_PROTO_NFC_DEP_MASK;
1058 break;
1059 }
1060
1061 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1062 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001063 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1064 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001065
1066 return 0;
1067}
1068
1069struct pn533_target_felica {
1070 u8 pol_res;
1071 u8 opcode;
1072 u8 nfcid2[8];
1073 u8 pad[8];
1074 /* optional */
1075 u8 syst_code[];
1076} __packed;
1077
1078#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1079#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1080
1081static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1082 int target_data_len)
1083{
1084 if (target_data_len < sizeof(struct pn533_target_felica))
1085 return false;
1086
1087 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1088 return false;
1089
1090 return true;
1091}
1092
1093static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1094 int tgt_data_len)
1095{
1096 struct pn533_target_felica *tgt_felica;
1097
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001098 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001099
1100 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1101 return -EPROTO;
1102
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001103 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1104 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001105 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1106 else
1107 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1108
Samuel Ortiz79757542012-03-05 01:03:45 +01001109 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1110 nfc_tgt->sensf_res_len = 9;
1111
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001112 return 0;
1113}
1114
1115struct pn533_target_jewel {
1116 __be16 sens_res;
1117 u8 jewelid[4];
1118} __packed;
1119
1120static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1121 int target_data_len)
1122{
1123 u8 ssd;
1124 u8 platconf;
1125
1126 if (target_data_len < sizeof(struct pn533_target_jewel))
1127 return false;
1128
1129 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1130 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1131 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1132
1133 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001134 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1135 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1136 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001137 return false;
1138
1139 return true;
1140}
1141
1142static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1143 int tgt_data_len)
1144{
1145 struct pn533_target_jewel *tgt_jewel;
1146
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001147 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001148
1149 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1150 return -EPROTO;
1151
1152 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1153 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001154 nfc_tgt->nfcid1_len = 4;
1155 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001156
1157 return 0;
1158}
1159
1160struct pn533_type_b_prot_info {
1161 u8 bitrate;
1162 u8 fsci_type;
1163 u8 fwi_adc_fo;
1164} __packed;
1165
1166#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1167#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1168#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1169
1170struct pn533_type_b_sens_res {
1171 u8 opcode;
1172 u8 nfcid[4];
1173 u8 appdata[4];
1174 struct pn533_type_b_prot_info prot_info;
1175} __packed;
1176
1177#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1178
1179struct pn533_target_type_b {
1180 struct pn533_type_b_sens_res sensb_res;
1181 u8 attrib_res_len;
1182 u8 attrib_res[];
1183} __packed;
1184
1185static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1186 int target_data_len)
1187{
1188 if (target_data_len < sizeof(struct pn533_target_type_b))
1189 return false;
1190
1191 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1192 return false;
1193
1194 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1195 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1196 return false;
1197
1198 return true;
1199}
1200
1201static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1202 int tgt_data_len)
1203{
1204 struct pn533_target_type_b *tgt_type_b;
1205
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001206 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001207
1208 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1209 return -EPROTO;
1210
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001211 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001212
1213 return 0;
1214}
1215
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001216static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1217 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001218{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001219 struct nfc_target nfc_tgt;
1220 int rc;
1221
1222 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001223 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001224
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001225 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001226 return -EPROTO;
1227
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001228 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1229
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001230 switch (dev->poll_mod_curr) {
1231 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001232 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001233 break;
1234 case PN533_POLL_MOD_212KBPS_FELICA:
1235 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001236 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001237 break;
1238 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001239 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001240 break;
1241 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001242 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243 break;
1244 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001245 nfc_dev_err(&dev->interface->dev,
1246 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001247 return -EPROTO;
1248 }
1249
1250 if (rc)
1251 return rc;
1252
1253 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001254 nfc_dev_dbg(&dev->interface->dev,
1255 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001256 return -EAGAIN;
1257 }
1258
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001259 nfc_dev_dbg(&dev->interface->dev,
1260 "Target found - supported protocols: 0x%x",
1261 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001262
1263 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1264
1265 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1266
1267 return 0;
1268}
1269
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001270static inline void pn533_poll_next_mod(struct pn533 *dev)
1271{
1272 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1273}
1274
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001275static void pn533_poll_reset_mod_list(struct pn533 *dev)
1276{
1277 dev->poll_mod_count = 0;
1278}
1279
1280static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1281{
1282 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001283 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001284 dev->poll_mod_count++;
1285}
1286
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001287static void pn533_poll_create_mod_list(struct pn533 *dev,
1288 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001289{
1290 pn533_poll_reset_mod_list(dev);
1291
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001292 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1293 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1294 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001295 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1296
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001297 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1298 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1300 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1301 }
1302
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001303 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001304 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1305
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001306 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001307 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001308
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001309 if (tm_protocols)
1310 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001311}
1312
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001313static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001314{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001315 u8 nbtg, tg, *tgdata;
1316 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317
1318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1319
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001320 nbtg = resp->data[0];
1321 tg = resp->data[1];
1322 tgdata = &resp->data[2];
1323 tgdata_len = resp->len - 2; /* nbtg + tg */
1324
1325 if (nbtg) {
1326 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327
1328 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001329 if (rc == 0) {
1330 pn533_poll_reset_mod_list(dev);
1331 return 0;
1332 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001333 }
1334
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001335 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001336}
1337
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001338static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001339{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001340 struct sk_buff *skb;
1341 u8 *felica, *nfcid3, *gb;
1342
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001343 u8 *gbytes = dev->gb;
1344 size_t gbytes_len = dev->gb_len;
1345
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001346 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1347 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1348 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1349 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001350
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001351 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1352 0x0, 0x0, 0x0,
1353 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001354
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001355 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1356 felica (18), nfcid3 (10), gb_len (1) */
1357 gbytes_len +
1358 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001359
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001360 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001361 if (!skb)
1362 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001363
1364 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001365 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001366
1367 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001368 memcpy(skb_put(skb, 6), mifare_params, 6);
1369
1370 /* Felica params */
1371 felica = skb_put(skb, 18);
1372 memcpy(felica, felica_params, 18);
1373 get_random_bytes(felica + 2, 6);
1374
1375 /* NFCID3 */
1376 nfcid3 = skb_put(skb, 10);
1377 memset(nfcid3, 0, 10);
1378 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001379
1380 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001381 *skb_put(skb, 1) = gbytes_len;
1382
1383 gb = skb_put(skb, gbytes_len);
1384 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001385
Samuel Ortizad3823c2012-05-30 23:54:55 +02001386 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001387 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001388
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001389 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001390}
1391
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001392#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001393#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1394static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001395 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001396{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001397 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001398
1399 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1400
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001401 if (IS_ERR(resp))
1402 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001403
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001404 status = resp->data[0];
1405 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001406
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001407 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001408 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001409 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001410 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001411 return 0;
1412 }
1413
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001414 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001415}
1416
1417static void pn533_wq_tg_get_data(struct work_struct *work)
1418{
1419 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001420
1421 struct sk_buff *skb;
1422 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001423
1424 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1425
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001426 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001427 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001428 return;
1429
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001430 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1431 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001432
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001433 if (rc < 0)
1434 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001435
1436 return;
1437}
1438
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001439#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001440static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001441{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001442 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001443 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001444 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001445
1446 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1447
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001448 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001449 return -EINVAL;
1450
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001451 mode = resp->data[0];
1452 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001453
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001454 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1455 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001456
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001457 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1458 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001459 comm_mode = NFC_COMM_ACTIVE;
1460
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001461 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001462 return -EOPNOTSUPP;
1463
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001464 gb = cmd + ATR_REQ_GB_OFFSET;
1465 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001466
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001467 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1468 comm_mode, gb, gb_len);
1469 if (rc < 0) {
1470 nfc_dev_err(&dev->interface->dev,
1471 "Error when signaling target activation");
1472 return rc;
1473 }
1474
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001475 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001476 queue_work(dev->wq, &dev->tg_work);
1477
1478 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001479}
1480
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001481static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001482{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001483 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001484
1485 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1486
1487 /* An ack will cancel the last issued command (poll) */
1488 pn533_send_ack(dev, GFP_ATOMIC);
1489
1490 dev->cancel_listen = 1;
1491
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001492 pn533_poll_next_mod(dev);
1493
1494 queue_work(dev->wq, &dev->poll_work);
1495}
1496
1497static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001498 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001499{
1500 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001501 int rc;
1502
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001503 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1504
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001505 if (IS_ERR(resp)) {
1506 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001507
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001508 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1509 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001511 if (rc == -ENOENT) {
1512 if (dev->poll_mod_count != 0)
1513 return rc;
1514 else
1515 goto stop_poll;
1516 } else if (rc < 0) {
1517 nfc_dev_err(&dev->interface->dev,
1518 "Error %d when running poll", rc);
1519 goto stop_poll;
1520 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001521 }
1522
1523 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1524
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001525 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001526 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001527 rc = pn533_init_target_complete(dev, resp);
1528 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001529 }
1530
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001531 /* Initiator mode */
1532 rc = pn533_start_poll_complete(dev, resp);
1533 if (!rc)
1534 goto done;
1535
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001536 if (!dev->poll_mod_count) {
1537 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1538 goto done;
1539 }
1540
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001541 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001542 queue_work(dev->wq, &dev->poll_work);
1543
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001544done:
1545 dev_kfree_skb(resp);
1546 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001547
1548stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001549 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1550
Samuel Ortizad3823c2012-05-30 23:54:55 +02001551 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001553 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001555
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001556static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1557 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001558{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001559 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001560
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001561 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001562 if (!skb)
1563 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001564
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001565 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001566
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001567 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568}
1569
1570static int pn533_send_poll_frame(struct pn533 *dev)
1571{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001572 struct pn533_poll_modulations *mod;
1573 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001574 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001575 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001576
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001577 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001578
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1580 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001581
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 if (mod->len == 0) { /* Listen mode */
1583 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001584 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 } else { /* Polling mode */
1586 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001587 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001588 }
1589
1590 if (!skb) {
1591 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1592 return -ENOMEM;
1593 }
1594
1595 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1596 NULL);
1597 if (rc < 0) {
1598 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001599 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001600 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001601
1602 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001603}
1604
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001605static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001606{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001607 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1608 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001609 int rc;
1610
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001611 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1612
1613 nfc_dev_dbg(&dev->interface->dev,
1614 "%s cancel_listen %d modulation len %d",
1615 __func__, dev->cancel_listen, cur_mod->len);
1616
1617 if (dev->cancel_listen == 1) {
1618 dev->cancel_listen = 0;
1619 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001620 }
1621
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001622 rc = pn533_send_poll_frame(dev);
1623 if (rc)
1624 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001625
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001626 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1627 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001628
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001629 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001630}
1631
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001632static int pn533_start_poll(struct nfc_dev *nfc_dev,
1633 u32 im_protocols, u32 tm_protocols)
1634{
1635 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1636
1637 nfc_dev_dbg(&dev->interface->dev,
1638 "%s: im protocols 0x%x tm protocols 0x%x",
1639 __func__, im_protocols, tm_protocols);
1640
1641 if (dev->tgt_active_prot) {
1642 nfc_dev_err(&dev->interface->dev,
1643 "Cannot poll with a target already activated");
1644 return -EBUSY;
1645 }
1646
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001647 if (dev->tgt_mode) {
1648 nfc_dev_err(&dev->interface->dev,
1649 "Cannot poll while already being activated");
1650 return -EBUSY;
1651 }
1652
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001653 if (tm_protocols) {
1654 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1655 if (dev->gb == NULL)
1656 tm_protocols = 0;
1657 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001658
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001659 dev->poll_mod_curr = 0;
1660 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1661 dev->poll_protocols = im_protocols;
1662 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001663
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001664 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001665}
1666
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001667static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1668{
1669 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1670
1671 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1672
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001673 del_timer(&dev->listen_timer);
1674
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001675 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001676 nfc_dev_dbg(&dev->interface->dev,
1677 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678 return;
1679 }
1680
1681 /* An ack will cancel the last issued command (poll) */
1682 pn533_send_ack(dev, GFP_KERNEL);
1683
1684 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1685 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001686
1687 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688}
1689
1690static int pn533_activate_target_nfcdep(struct pn533 *dev)
1691{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001692 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001693 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001694 int rc;
1695
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001696 struct sk_buff *skb;
1697 struct sk_buff *resp;
1698
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001699 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1700
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001701 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001702 if (!skb)
1703 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001705 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1706 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001708 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1709 if (IS_ERR(resp))
1710 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001711
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001712 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001713 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001714 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001715 nfc_dev_err(&dev->interface->dev,
1716 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001717 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001719 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720
Samuel Ortiz541d9202011-12-14 16:43:10 +01001721 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001722 gt_len = resp->len - 16;
1723 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001724
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001726 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001727}
1728
Eric Lapuyade90099432012-05-07 12:31:13 +02001729static int pn533_activate_target(struct nfc_dev *nfc_dev,
1730 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001731{
1732 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1733 int rc;
1734
1735 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001736 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001737
1738 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001739 nfc_dev_err(&dev->interface->dev,
1740 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741 return -EBUSY;
1742 }
1743
1744 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001745 nfc_dev_err(&dev->interface->dev,
1746 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747 return -EBUSY;
1748 }
1749
1750 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001751 nfc_dev_err(&dev->interface->dev,
1752 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001753 return -EINVAL;
1754 }
1755
1756 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001757 nfc_dev_err(&dev->interface->dev,
1758 "Target doesn't support requested proto %u",
1759 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001760 return -EINVAL;
1761 }
1762
1763 if (protocol == NFC_PROTO_NFC_DEP) {
1764 rc = pn533_activate_target_nfcdep(dev);
1765 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001766 nfc_dev_err(&dev->interface->dev,
1767 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001768 return rc;
1769 }
1770 }
1771
1772 dev->tgt_active_prot = protocol;
1773 dev->tgt_available_prots = 0;
1774
1775 return 0;
1776}
1777
Eric Lapuyade90099432012-05-07 12:31:13 +02001778static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1779 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001780{
1781 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001782
1783 struct sk_buff *skb;
1784 struct sk_buff *resp;
1785
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001786 int rc;
1787
1788 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1789
1790 if (!dev->tgt_active_prot) {
1791 nfc_dev_err(&dev->interface->dev, "There is no active target");
1792 return;
1793 }
1794
1795 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001796 skb_queue_purge(&dev->resp_q);
1797
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001798 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001799 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001801
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001802 *skb_put(skb, 1) = 1; /* TG*/
1803
1804 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1805 if (IS_ERR(resp))
1806 return;
1807
1808 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001809 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001810 nfc_dev_err(&dev->interface->dev,
1811 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001812
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001813 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814 return;
1815}
1816
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001817
1818static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001819 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001820{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001821 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001822 u8 target_gt_len;
1823 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001824 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001825
1826 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001827
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001828 if (IS_ERR(resp))
1829 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001830
1831 if (dev->tgt_available_prots &&
1832 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1833 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001834 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001835 rc = -EINVAL;
1836 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837 }
1838
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001839 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1840
1841 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001842 if (rc != PN533_CMD_RET_SUCCESS) {
1843 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001844 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001845 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001846 }
1847
1848 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001849 struct nfc_target nfc_target;
1850
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001851 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1852
1853 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001854 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001856 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1857 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001858 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001859
1860 dev->tgt_available_prots = 0;
1861 }
1862
1863 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1864
1865 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001866 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001867 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001868 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869 if (rc == 0)
1870 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001871 dev->nfc_dev->targets[0].idx,
1872 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001873
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001874error:
1875 dev_kfree_skb(resp);
1876 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877}
1878
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001879static int pn533_mod_to_baud(struct pn533 *dev)
1880{
1881 switch (dev->poll_mod_curr) {
1882 case PN533_POLL_MOD_106KBPS_A:
1883 return 0;
1884 case PN533_POLL_MOD_212KBPS_FELICA:
1885 return 1;
1886 case PN533_POLL_MOD_424KBPS_FELICA:
1887 return 2;
1888 default:
1889 return -EINVAL;
1890 }
1891}
1892
Samuel Ortizd7f33452012-05-29 21:45:21 +02001893#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001894static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001895 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001896{
1897 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001898 struct sk_buff *skb;
1899 int rc, baud, skb_len;
1900 u8 *next, *arg;
1901
Samuel Ortizd7f33452012-05-29 21:45:21 +02001902 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001903
1904 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1905
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906 if (dev->poll_mod_count) {
1907 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001908 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001909 return -EBUSY;
1910 }
1911
1912 if (dev->tgt_active_prot) {
1913 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001914 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001915 return -EBUSY;
1916 }
1917
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001918 baud = pn533_mod_to_baud(dev);
1919 if (baud < 0) {
1920 nfc_dev_err(&dev->interface->dev,
1921 "Invalid curr modulation %d", dev->poll_mod_curr);
1922 return baud;
1923 }
1924
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001925 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001926 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001927 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001928
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001929 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001930 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001931 return -ENOMEM;
1932
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001933 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1934 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001935
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001936 next = skb_put(skb, 1); /* Next */
1937 *next = 0;
1938
1939 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1940 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1941 PASSIVE_DATA_LEN);
1942 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001943 }
1944
Samuel Ortiz47807d32012-03-05 01:03:50 +01001945 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001946 memcpy(skb_put(skb, gb_len), gb, gb_len);
1947 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001948 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001949 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001950 }
1951
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001952 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1953 if (!arg) {
1954 dev_kfree_skb(skb);
1955 return -ENOMEM;
1956 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001957
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001958 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001959
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001960 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1961 pn533_in_dep_link_up_complete, arg);
1962
1963 if (rc < 0) {
1964 dev_kfree_skb(skb);
1965 kfree(arg);
1966 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001967
1968 return rc;
1969}
1970
1971static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1972{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001973 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1974
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001975 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1976
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001977 pn533_poll_reset_mod_list(dev);
1978
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001979 if (dev->tgt_mode || dev->tgt_active_prot) {
1980 pn533_send_ack(dev, GFP_KERNEL);
1981 usb_kill_urb(dev->in_urb);
1982 }
1983
1984 dev->tgt_active_prot = 0;
1985 dev->tgt_mode = 0;
1986
1987 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001988
1989 return 0;
1990}
1991
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001992struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001993 data_exchange_cb_t cb;
1994 void *cb_context;
1995};
1996
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001997static struct sk_buff *pn533_build_response(struct pn533 *dev)
1998{
1999 struct sk_buff *skb, *tmp, *t;
2000 unsigned int skb_len = 0, tmp_len = 0;
2001
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002002 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002003
2004 if (skb_queue_empty(&dev->resp_q))
2005 return NULL;
2006
2007 if (skb_queue_len(&dev->resp_q) == 1) {
2008 skb = skb_dequeue(&dev->resp_q);
2009 goto out;
2010 }
2011
2012 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2013 skb_len += tmp->len;
2014
2015 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2016 __func__, skb_len);
2017
2018 skb = alloc_skb(skb_len, GFP_KERNEL);
2019 if (skb == NULL)
2020 goto out;
2021
2022 skb_put(skb, skb_len);
2023
2024 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2025 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2026 tmp_len += tmp->len;
2027 }
2028
2029out:
2030 skb_queue_purge(&dev->resp_q);
2031
2032 return skb;
2033}
2034
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002035static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002036 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002037{
2038 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002039 struct sk_buff *skb;
2040 int rc = 0;
2041 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002042
2043 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2044
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002045 if (IS_ERR(resp)) {
2046 rc = PTR_ERR(resp);
2047 goto _error;
2048 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002049
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002050 status = resp->data[0];
2051 ret = status & PN533_CMD_RET_MASK;
2052 mi = status & PN533_CMD_MI_MASK;
2053
2054 skb_pull(resp, sizeof(status));
2055
2056 if (ret != PN533_CMD_RET_SUCCESS) {
2057 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002058 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002059 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002060 goto error;
2061 }
2062
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002063 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002064
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002065 if (mi) {
2066 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002067 queue_work(dev->wq, &dev->mi_work);
2068 return -EINPROGRESS;
2069 }
2070
2071 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002072 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002073 goto error;
2074
2075 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002076 kfree(arg);
2077 return 0;
2078
2079error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002080 dev_kfree_skb(resp);
2081_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002082 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002083 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002084 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002085 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002086}
2087
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002088static int pn533_transceive(struct nfc_dev *nfc_dev,
2089 struct nfc_target *target, struct sk_buff *skb,
2090 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002091{
2092 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002093 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002094 int rc;
2095
2096 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2097
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002098 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2099 /* TODO: Implement support to multi-part data exchange */
2100 nfc_dev_err(&dev->interface->dev,
2101 "Data length greater than the max allowed: %d",
2102 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2103 rc = -ENOSYS;
2104 goto error;
2105 }
2106
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002107 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002108 nfc_dev_err(&dev->interface->dev,
2109 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002110 rc = -EINVAL;
2111 goto error;
2112 }
2113
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002114 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002115 if (!arg) {
2116 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002117 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002118 }
2119
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 arg->cb = cb;
2121 arg->cb_context = cb_context;
2122
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002123 switch (dev->device_type) {
2124 case PN533_DEVICE_PASORI:
2125 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2126 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2127 skb,
2128 pn533_data_exchange_complete,
2129 arg);
2130
2131 break;
2132 }
2133 default:
2134 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2135
2136 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2137 skb, pn533_data_exchange_complete,
2138 arg);
2139
2140 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002141 }
2142
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002143 if (rc < 0) /* rc from send_async */
2144 goto error;
2145
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002146 return 0;
2147
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002148error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002149 kfree(arg);
2150 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151 return rc;
2152}
2153
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002154static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002155 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002156{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002157 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002158
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002159 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2160
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002161 if (IS_ERR(resp))
2162 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002163
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002164 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002165
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002166 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002167
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002168 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002169 nfc_tm_deactivated(dev->nfc_dev);
2170
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002171 dev->tgt_mode = 0;
2172
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173 return 0;
2174 }
2175
2176 queue_work(dev->wq, &dev->tg_work);
2177
2178 return 0;
2179}
2180
2181static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2182{
2183 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002184 int rc;
2185
2186 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2187
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002188 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002189 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002190 "Data length greater than the max allowed: %d",
2191 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2192 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002193 }
2194
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002195 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2196 pn533_tm_send_complete, NULL);
2197 if (rc < 0)
2198 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002199
2200 return rc;
2201}
2202
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002203static void pn533_wq_mi_recv(struct work_struct *work)
2204{
2205 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002206
2207 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002208 int rc;
2209
2210 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2211
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002212 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002213 if (!skb)
2214 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002215
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002216 switch (dev->device_type) {
2217 case PN533_DEVICE_PASORI:
2218 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2219 rc = pn533_send_cmd_direct_async(dev,
2220 PN533_CMD_IN_COMM_THRU,
2221 skb,
2222 pn533_data_exchange_complete,
2223 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002224
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002225 break;
2226 }
2227 default:
2228 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002229
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002230 rc = pn533_send_cmd_direct_async(dev,
2231 PN533_CMD_IN_DATA_EXCHANGE,
2232 skb,
2233 pn533_data_exchange_complete,
2234 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002235
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002236 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002237 }
2238
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002239 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002240 return;
2241
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002242 nfc_dev_err(&dev->interface->dev,
2243 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002244
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002246 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002247
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002248error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002249 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002250 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002251}
2252
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002253static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2254 u8 cfgdata_len)
2255{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002256 struct sk_buff *skb;
2257 struct sk_buff *resp;
2258
2259 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002260
2261 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2262
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002263 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002264
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002265 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002266 if (!skb)
2267 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002268
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002269 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2270 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002271
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002272 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2273 if (IS_ERR(resp))
2274 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002275
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002276 dev_kfree_skb(resp);
2277 return 0;
2278}
2279
2280static int pn533_get_firmware_version(struct pn533 *dev,
2281 struct pn533_fw_version *fv)
2282{
2283 struct sk_buff *skb;
2284 struct sk_buff *resp;
2285
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002286 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002287 if (!skb)
2288 return -ENOMEM;
2289
2290 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2291 if (IS_ERR(resp))
2292 return PTR_ERR(resp);
2293
2294 fv->ic = resp->data[0];
2295 fv->ver = resp->data[1];
2296 fv->rev = resp->data[2];
2297 fv->support = resp->data[3];
2298
2299 dev_kfree_skb(resp);
2300 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002301}
2302
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002303static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002304{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002305 struct sk_buff *skb;
2306 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002307
2308 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2309
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002310 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002311 if (!skb)
2312 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002313
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002314 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002315
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002316 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2317 if (IS_ERR(resp))
2318 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002319
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002320 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002321
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002322 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002323}
2324
2325static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002326 .dev_up = NULL,
2327 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002328 .dep_link_up = pn533_dep_link_up,
2329 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002330 .start_poll = pn533_start_poll,
2331 .stop_poll = pn533_stop_poll,
2332 .activate_target = pn533_activate_target,
2333 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002334 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002335 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002336};
2337
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002338static int pn533_setup(struct pn533 *dev)
2339{
2340 struct pn533_config_max_retries max_retries;
2341 struct pn533_config_timing timing;
2342 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2343 int rc;
2344
2345 switch (dev->device_type) {
2346 case PN533_DEVICE_STD:
2347 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2348 max_retries.mx_rty_psl = 2;
2349 max_retries.mx_rty_passive_act =
2350 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2351
2352 timing.rfu = PN533_CONFIG_TIMING_102;
2353 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2354 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2355
2356 break;
2357
2358 case PN533_DEVICE_PASORI:
2359 max_retries.mx_rty_atr = 0x2;
2360 max_retries.mx_rty_psl = 0x1;
2361 max_retries.mx_rty_passive_act =
2362 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2363
2364 timing.rfu = PN533_CONFIG_TIMING_102;
2365 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2366 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2367
2368 break;
2369
2370 default:
2371 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2372 dev->device_type);
2373 return -EINVAL;
2374 }
2375
2376 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2377 (u8 *)&max_retries, sizeof(max_retries));
2378 if (rc) {
2379 nfc_dev_err(&dev->interface->dev,
2380 "Error on setting MAX_RETRIES config");
2381 return rc;
2382 }
2383
2384
2385 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2386 (u8 *)&timing, sizeof(timing));
2387 if (rc) {
2388 nfc_dev_err(&dev->interface->dev,
2389 "Error on setting RF timings");
2390 return rc;
2391 }
2392
2393 switch (dev->device_type) {
2394 case PN533_DEVICE_STD:
2395 break;
2396
2397 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002398 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002399
2400 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2401 pasori_cfg, 3);
2402 if (rc) {
2403 nfc_dev_err(&dev->interface->dev,
2404 "Error while settings PASORI config");
2405 return rc;
2406 }
2407
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002408 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002409
2410 break;
2411 }
2412
2413 return 0;
2414}
2415
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002416static int pn533_probe(struct usb_interface *interface,
2417 const struct usb_device_id *id)
2418{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002419 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002420 struct pn533 *dev;
2421 struct usb_host_interface *iface_desc;
2422 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423 int in_endpoint = 0;
2424 int out_endpoint = 0;
2425 int rc = -ENOMEM;
2426 int i;
2427 u32 protocols;
2428
2429 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2430 if (!dev)
2431 return -ENOMEM;
2432
2433 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2434 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002435 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002436
2437 iface_desc = interface->cur_altsetting;
2438 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2439 endpoint = &iface_desc->endpoint[i].desc;
2440
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002441 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002442 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002443
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002444 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002446 }
2447
2448 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002449 nfc_dev_err(&interface->dev,
2450 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002451 rc = -ENODEV;
2452 goto error;
2453 }
2454
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002456 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2457
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002458 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002459 goto error;
2460
2461 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002462 usb_rcvbulkpipe(dev->udev, in_endpoint),
2463 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002464 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002465 usb_sndbulkpipe(dev->udev, out_endpoint),
2466 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002467
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002468 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2469 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002470 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002471 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002472 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002473 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002474 if (dev->wq == NULL)
2475 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002476
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002477 init_timer(&dev->listen_timer);
2478 dev->listen_timer.data = (unsigned long) dev;
2479 dev->listen_timer.function = pn533_listen_mode_timer;
2480
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002481 skb_queue_head_init(&dev->resp_q);
2482
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002483 INIT_LIST_HEAD(&dev->cmd_queue);
2484
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002485 usb_set_intfdata(interface, dev);
2486
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002487 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002488
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002489 dev->device_type = id->driver_info;
2490 switch (dev->device_type) {
2491 case PN533_DEVICE_STD:
2492 protocols = PN533_ALL_PROTOCOLS;
2493 break;
2494
2495 case PN533_DEVICE_PASORI:
2496 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2497 break;
2498
2499 default:
2500 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2501 dev->device_type);
2502 rc = -EINVAL;
2503 goto destroy_wq;
2504 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002505
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002506 memset(&fw_ver, 0, sizeof(fw_ver));
2507 rc = pn533_get_firmware_version(dev, &fw_ver);
2508 if (rc < 0)
2509 goto destroy_wq;
2510
2511 nfc_dev_info(&dev->interface->dev,
2512 "NXP PN533 firmware ver %d.%d now attached",
2513 fw_ver.ver, fw_ver.rev);
2514
2515
Samuel Ortize8753042011-08-19 15:47:11 +02002516 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002517 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002518 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002519 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002520 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002521 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002522 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002523
2524 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2525 nfc_set_drvdata(dev->nfc_dev, dev);
2526
2527 rc = nfc_register_device(dev->nfc_dev);
2528 if (rc)
2529 goto free_nfc_dev;
2530
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002531 rc = pn533_setup(dev);
2532 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002533 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002534
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002535 return 0;
2536
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002537unregister_nfc_dev:
2538 nfc_unregister_device(dev->nfc_dev);
2539
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002540free_nfc_dev:
2541 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002542
Samuel Ortiz4849f852012-04-10 19:43:17 +02002543destroy_wq:
2544 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002545error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002546 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002547 usb_free_urb(dev->out_urb);
2548 kfree(dev);
2549 return rc;
2550}
2551
2552static void pn533_disconnect(struct usb_interface *interface)
2553{
2554 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002555 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002556
2557 dev = usb_get_intfdata(interface);
2558 usb_set_intfdata(interface, NULL);
2559
2560 nfc_unregister_device(dev->nfc_dev);
2561 nfc_free_device(dev->nfc_dev);
2562
2563 usb_kill_urb(dev->in_urb);
2564 usb_kill_urb(dev->out_urb);
2565
Samuel Ortiz4849f852012-04-10 19:43:17 +02002566 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002567
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002568 skb_queue_purge(&dev->resp_q);
2569
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002570 del_timer(&dev->listen_timer);
2571
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002572 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2573 list_del(&cmd->queue);
2574 kfree(cmd);
2575 }
2576
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002577 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002578 usb_free_urb(dev->out_urb);
2579 kfree(dev);
2580
Dan Carpenter276556d2011-07-08 10:21:15 +03002581 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002582}
2583
2584static struct usb_driver pn533_driver = {
2585 .name = "pn533",
2586 .probe = pn533_probe,
2587 .disconnect = pn533_disconnect,
2588 .id_table = pn533_table,
2589};
2590
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002591module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002592
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002593MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2594MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2595MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002596MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2597MODULE_VERSION(VERSION);
2598MODULE_LICENSE("GPL");