blob: 2f39209507d4c46c132cc827db2d01eda3438e06 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
30#define VERSION "0.1"
31
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define PN533_DEVICE_STD 0x1
42#define PN533_DEVICE_PASORI 0x2
43
Samuel Ortiz01d719a2012-07-04 00:14:04 +020044#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
45 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
46 NFC_PROTO_NFC_DEP_MASK |\
47 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020048
49#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
50 NFC_PROTO_MIFARE_MASK | \
51 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020052 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020053 NFC_PROTO_NFC_DEP_MASK)
54
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030055static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
57 .idVendor = PN533_VENDOR_ID,
58 .idProduct = PN533_PRODUCT_ID,
59 .driver_info = PN533_DEVICE_STD,
60 },
61 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
62 .idVendor = SCM_VENDOR_ID,
63 .idProduct = SCL3711_PRODUCT_ID,
64 .driver_info = PN533_DEVICE_STD,
65 },
66 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
67 .idVendor = SONY_VENDOR_ID,
68 .idProduct = PASORI_PRODUCT_ID,
69 .driver_info = PN533_DEVICE_PASORI,
70 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030071 { }
72};
73MODULE_DEVICE_TABLE(usb, pn533_table);
74
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020075/* How much time we spend listening for initiators */
76#define PN533_LISTEN_TIME 2
77
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020078/* Standard pn533 frame definitions */
79#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010080 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020081#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010083/*
84 * Max extended frame payload len, excluding TFI and CC
85 * which are already in PN533_FRAME_HEADER_LEN.
86 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020087#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010088
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020089#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010090 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020091#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
92#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030093
94/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020095#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030096
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020097/* standard frame identifier: in/out/error */
98#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
99#define PN533_STD_FRAME_DIR_OUT 0xD4
100#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300101
102/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200103#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104
105#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
106#define PN533_CMD_RF_CONFIGURATION 0x32
107#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200108#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300109#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
110#define PN533_CMD_IN_ATR 0x50
111#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100112#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300113
Samuel Ortizad3823c2012-05-30 23:54:55 +0200114#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200115#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200116#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100117#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
120
121/* PN533 Return codes */
122#define PN533_CMD_RET_MASK 0x3F
123#define PN533_CMD_MI_MASK 0x40
124#define PN533_CMD_RET_SUCCESS 0x00
125
126struct pn533;
127
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100128typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300129
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100130typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
131 struct sk_buff *resp);
132
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133/* structs for pn533 commands */
134
135/* PN533_CMD_GET_FIRMWARE_VERSION */
136struct pn533_fw_version {
137 u8 ic;
138 u8 ver;
139 u8 rev;
140 u8 support;
141};
142
143/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200144#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200146#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300147
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200148#define PN533_CONFIG_TIMING_102 0xb
149#define PN533_CONFIG_TIMING_204 0xc
150#define PN533_CONFIG_TIMING_409 0xd
151#define PN533_CONFIG_TIMING_819 0xe
152
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
154#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
155
156struct pn533_config_max_retries {
157 u8 mx_rty_atr;
158 u8 mx_rty_psl;
159 u8 mx_rty_passive_act;
160} __packed;
161
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200162struct pn533_config_timing {
163 u8 rfu;
164 u8 atr_res_timeout;
165 u8 dep_timeout;
166} __packed;
167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300168/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
169
170/* felica commands opcode */
171#define PN533_FELICA_OPC_SENSF_REQ 0
172#define PN533_FELICA_OPC_SENSF_RES 1
173/* felica SENSF_REQ parameters */
174#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
175#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
176#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
177#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
178
179/* type B initiator_data values */
180#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
181#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
182#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
183
184union pn533_cmd_poll_initdata {
185 struct {
186 u8 afi;
187 u8 polling_method;
188 } __packed type_b;
189 struct {
190 u8 opcode;
191 __be16 sc;
192 u8 rc;
193 u8 tsn;
194 } __packed felica;
195};
196
197/* Poll modulations */
198enum {
199 PN533_POLL_MOD_106KBPS_A,
200 PN533_POLL_MOD_212KBPS_FELICA,
201 PN533_POLL_MOD_424KBPS_FELICA,
202 PN533_POLL_MOD_106KBPS_JEWEL,
203 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200204 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205
206 __PN533_POLL_MOD_AFTER_LAST,
207};
208#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
209
210struct pn533_poll_modulations {
211 struct {
212 u8 maxtg;
213 u8 brty;
214 union pn533_cmd_poll_initdata initiator_data;
215 } __packed data;
216 u8 len;
217};
218
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100219static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300220 [PN533_POLL_MOD_106KBPS_A] = {
221 .data = {
222 .maxtg = 1,
223 .brty = 0,
224 },
225 .len = 2,
226 },
227 [PN533_POLL_MOD_212KBPS_FELICA] = {
228 .data = {
229 .maxtg = 1,
230 .brty = 1,
231 .initiator_data.felica = {
232 .opcode = PN533_FELICA_OPC_SENSF_REQ,
233 .sc = PN533_FELICA_SENSF_SC_ALL,
234 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
235 .tsn = 0,
236 },
237 },
238 .len = 7,
239 },
240 [PN533_POLL_MOD_424KBPS_FELICA] = {
241 .data = {
242 .maxtg = 1,
243 .brty = 2,
244 .initiator_data.felica = {
245 .opcode = PN533_FELICA_OPC_SENSF_REQ,
246 .sc = PN533_FELICA_SENSF_SC_ALL,
247 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
248 .tsn = 0,
249 },
250 },
251 .len = 7,
252 },
253 [PN533_POLL_MOD_106KBPS_JEWEL] = {
254 .data = {
255 .maxtg = 1,
256 .brty = 4,
257 },
258 .len = 2,
259 },
260 [PN533_POLL_MOD_847KBPS_B] = {
261 .data = {
262 .maxtg = 1,
263 .brty = 8,
264 .initiator_data.type_b = {
265 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
266 .polling_method =
267 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
268 },
269 },
270 .len = 3,
271 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200272 [PN533_LISTEN_MOD] = {
273 .len = 0,
274 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300275};
276
277/* PN533_CMD_IN_ATR */
278
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300279struct pn533_cmd_activate_response {
280 u8 status;
281 u8 nfcid3t[10];
282 u8 didt;
283 u8 bst;
284 u8 brt;
285 u8 to;
286 u8 ppt;
287 /* optional */
288 u8 gt[];
289} __packed;
290
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100291struct pn533_cmd_jump_dep_response {
292 u8 status;
293 u8 tg;
294 u8 nfcid3t[10];
295 u8 didt;
296 u8 bst;
297 u8 brt;
298 u8 to;
299 u8 ppt;
300 /* optional */
301 u8 gt[];
302} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303
Samuel Ortizad3823c2012-05-30 23:54:55 +0200304
305/* PN533_TG_INIT_AS_TARGET */
306#define PN533_INIT_TARGET_PASSIVE 0x1
307#define PN533_INIT_TARGET_DEP 0x2
308
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200309#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
310#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
311#define PN533_INIT_TARGET_RESP_DEP 0x4
312
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313struct pn533 {
314 struct usb_device *udev;
315 struct usb_interface *interface;
316 struct nfc_dev *nfc_dev;
317
318 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200321 struct sk_buff_head resp_q;
322
Samuel Ortiz4849f852012-04-10 19:43:17 +0200323 struct workqueue_struct *wq;
324 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200325 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200326 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200327 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200328 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200330 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200331 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332
333 pn533_cmd_complete_t cmd_complete;
334 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100335 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200336 struct mutex cmd_lock;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300337 u8 cmd;
338
339 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
340 u8 poll_mod_count;
341 u8 poll_mod_curr;
342 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200343 u32 listen_protocols;
344
345 u8 *gb;
346 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300347
348 u8 tgt_available_prots;
349 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200350 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200351
352 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200353
354 struct list_head cmd_queue;
355 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100356
357 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200358};
359
360struct pn533_cmd {
361 struct list_head queue;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100362 u8 cmd_code;
363 struct sk_buff *req;
364 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100365 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200366 pn533_send_async_complete_t complete_cb;
367 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300368};
369
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200370struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300371 u8 preamble;
372 __be16 start_frame;
373 u8 datalen;
374 u8 datalen_checksum;
375 u8 data[];
376} __packed;
377
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100378struct pn533_frame_ops {
379 void (*tx_frame_init)(void *frame, u8 cmd_code);
380 void (*tx_frame_finish)(void *frame);
381 void (*tx_update_payload_len)(void *frame, int len);
382 int tx_header_len;
383 int tx_tail_len;
384
385 bool (*rx_is_frame_valid)(void *frame);
386 int (*rx_frame_size)(void *frame);
387 int rx_header_len;
388 int rx_tail_len;
389
390 int max_payload_len;
391 u8 (*get_cmd_code)(void *frame);
392};
393
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300394/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200395static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396{
397 return ~value + 1;
398}
399
400/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200401static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300402{
403 u8 sum = 0;
404 int i;
405
406 for (i = 0; i < datalen; i++)
407 sum += data[i];
408
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200409 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300410}
411
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200412static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300413{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200414 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100415
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300416 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200417 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
418 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
419 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300420 frame->datalen = 2;
421}
422
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200423static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300424{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200425 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100426
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200427 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300428
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200429 PN533_STD_FRAME_CHECKSUM(frame) =
430 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300431
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200432 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300433}
434
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200435static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100436{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200437 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100438
439 frame->datalen += len;
440}
441
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200442static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300443{
444 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200445 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300446
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200447 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448 return false;
449
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200450 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300451 if (checksum != frame->datalen_checksum)
452 return false;
453
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200454 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
455 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300456 return false;
457
458 return true;
459}
460
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200461static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300462{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200463 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300464 return false;
465
466 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
467 return false;
468
469 return true;
470}
471
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200472static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300473{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200474 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100475
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200476 return sizeof(struct pn533_std_frame) + f->datalen +
477 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100478}
479
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200480static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100481{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200482 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100483
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200484 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100485}
486
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100487static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200488 .tx_frame_init = pn533_std_tx_frame_init,
489 .tx_frame_finish = pn533_std_tx_frame_finish,
490 .tx_update_payload_len = pn533_std_tx_update_payload_len,
491 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
492 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100493
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200494 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
495 .rx_frame_size = pn533_std_rx_frame_size,
496 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
497 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100498
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200499 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
500 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100501};
502
503static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
504{
505 return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300506}
507
Samuel Ortiz4849f852012-04-10 19:43:17 +0200508
509static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300510{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200511 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300512 int rc;
513
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100514 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300515 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200516 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300517}
518
519static void pn533_recv_response(struct urb *urb)
520{
521 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100522 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300523
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524 switch (urb->status) {
525 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100526 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300527 case -ECONNRESET:
528 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100529 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100530 "The urb has been canceled (status %d)",
531 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200532 dev->wq_in_error = urb->status;
533 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100534 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300535 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100536 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100537 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200538 dev->wq_in_error = urb->status;
539 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300540 }
541
542 in_frame = dev->in_urb->transfer_buffer;
543
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100544 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100545 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
546 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100547
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100548 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300549 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200550 dev->wq_in_error = -EIO;
551 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300552 }
553
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100554 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100555 nfc_dev_err(&dev->interface->dev,
556 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200557 dev->wq_in_error = -EIO;
558 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300559 }
560
Samuel Ortiz4849f852012-04-10 19:43:17 +0200561 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300562
Samuel Ortiz4849f852012-04-10 19:43:17 +0200563sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200564 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565}
566
567static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
568{
569 dev->in_urb->complete = pn533_recv_response;
570
571 return usb_submit_urb(dev->in_urb, flags);
572}
573
574static void pn533_recv_ack(struct urb *urb)
575{
576 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200577 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300578 int rc;
579
580 switch (urb->status) {
581 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100582 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300583 case -ECONNRESET:
584 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100585 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100586 "The urb has been stopped (status %d)",
587 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200588 dev->wq_in_error = urb->status;
589 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100590 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300591 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100592 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100593 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200594 dev->wq_in_error = urb->status;
595 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300596 }
597
598 in_frame = dev->in_urb->transfer_buffer;
599
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200600 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300601 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200602 dev->wq_in_error = -EIO;
603 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300604 }
605
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300606 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
607 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100608 nfc_dev_err(&dev->interface->dev,
609 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200610 dev->wq_in_error = rc;
611 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300612 }
613
614 return;
615
Samuel Ortiz4849f852012-04-10 19:43:17 +0200616sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200617 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300618}
619
620static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
621{
622 dev->in_urb->complete = pn533_recv_ack;
623
624 return usb_submit_urb(dev->in_urb, flags);
625}
626
627static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
628{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200629 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100630 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300631 int rc;
632
633 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
634
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100635 dev->out_urb->transfer_buffer = ack;
636 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300637 rc = usb_submit_urb(dev->out_urb, flags);
638
639 return rc;
640}
641
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100642static int __pn533_send_frame_async(struct pn533 *dev,
643 struct sk_buff *out,
644 struct sk_buff *in,
645 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300646 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100647 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300648{
649 int rc;
650
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100651 dev->cmd = dev->ops->get_cmd_code(out->data);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300652 dev->cmd_complete = cmd_complete;
653 dev->cmd_complete_arg = arg;
654
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100655 dev->out_urb->transfer_buffer = out->data;
656 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100658 dev->in_urb->transfer_buffer = in->data;
659 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300660
Samuel Ortize279f842013-03-20 11:27:57 +0100661 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
662 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100663
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100664 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300665 if (rc)
666 return rc;
667
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100668 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300669 if (rc)
670 goto error;
671
672 return 0;
673
674error:
675 usb_unlink_urb(dev->out_urb);
676 return rc;
677}
678
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100679static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
680 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100682 /* payload is already there, just update datalen */
683 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100684 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100685
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100686
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100687 skb_push(skb, ops->tx_header_len);
688 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100689
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100690 ops->tx_frame_init(skb->data, cmd_code);
691 ops->tx_update_payload_len(skb->data, payload_len);
692 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100693}
694
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200695static int pn533_send_async_complete(struct pn533 *dev, void *arg, int status)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100696{
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200697 struct pn533_cmd *cmd = arg;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100698
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200699 struct sk_buff *req = cmd->req;
700 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100701
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100702 int rc;
703
704 dev_kfree_skb(req);
705
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100706 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200707 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
708 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100709 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200710 kfree(cmd);
711 return rc;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100712 }
713
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100714 skb_put(resp, dev->ops->rx_frame_size(resp->data));
715 skb_pull(resp, dev->ops->rx_header_len);
716 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100717
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200718 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100719
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200720 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100721 return rc;
722}
723
724static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
725 struct sk_buff *req, struct sk_buff *resp,
726 int resp_len,
727 pn533_send_async_complete_t complete_cb,
728 void *complete_cb_context)
729{
730 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100731 int rc = 0;
732
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100733 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100734
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200735 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
736 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100737 return -ENOMEM;
738
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200739 cmd->cmd_code = cmd_code;
740 cmd->req = req;
741 cmd->resp = resp;
742 cmd->resp_len = resp_len;
743 cmd->complete_cb = complete_cb;
744 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100745
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100746 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100747
748 mutex_lock(&dev->cmd_lock);
749
750 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100751 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200752 pn533_send_async_complete, cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100753 if (rc)
754 goto error;
755
756 dev->cmd_pending = 1;
757 goto unlock;
758 }
759
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100760 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
761 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100762
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100763 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100764 list_add_tail(&cmd->queue, &dev->cmd_queue);
765
766 goto unlock;
767
768error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200769 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100770unlock:
771 mutex_unlock(&dev->cmd_lock);
772 return rc;
773}
774
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100775static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
776 struct sk_buff *req,
777 pn533_send_async_complete_t complete_cb,
778 void *complete_cb_context)
779{
780 struct sk_buff *resp;
781 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100782 int resp_len = dev->ops->rx_header_len +
783 dev->ops->max_payload_len +
784 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100785
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100786 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
787 if (!resp)
788 return -ENOMEM;
789
790 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
791 complete_cb_context);
792 if (rc)
793 dev_kfree_skb(resp);
794
795 return rc;
796}
797
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100798static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
799 struct sk_buff *req,
800 pn533_send_async_complete_t complete_cb,
801 void *complete_cb_context)
802{
803 struct sk_buff *resp;
804 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100805 int resp_len = dev->ops->rx_header_len +
806 dev->ops->max_payload_len +
807 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100808
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100809 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100810 if (!resp)
811 return -ENOMEM;
812
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100813 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
814 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100815 if (rc)
816 dev_kfree_skb(resp);
817
818 return rc;
819}
820
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100821/*
822 * pn533_send_cmd_direct_async
823 *
824 * The function sends a piority cmd directly to the chip omiting the cmd
825 * queue. It's intended to be used by chaining mechanism of received responses
826 * where the host has to request every single chunk of data before scheduling
827 * next cmd from the queue.
828 */
829static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
830 struct sk_buff *req,
831 pn533_send_async_complete_t complete_cb,
832 void *complete_cb_context)
833{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100834 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200835 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100836 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100837 int resp_len = dev->ops->rx_header_len +
838 dev->ops->max_payload_len +
839 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100840
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100841 resp = alloc_skb(resp_len, GFP_KERNEL);
842 if (!resp)
843 return -ENOMEM;
844
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200845 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
846 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100847 dev_kfree_skb(resp);
848 return -ENOMEM;
849 }
850
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200851 cmd->cmd_code = cmd_code;
852 cmd->req = req;
853 cmd->resp = resp;
854 cmd->resp_len = resp_len;
855 cmd->complete_cb = complete_cb;
856 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100857
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100858 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100859
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100860 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200861 pn533_send_async_complete, cmd);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100862 if (rc < 0) {
863 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200864 kfree(cmd);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100865 }
866
867 return rc;
868}
869
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200870static void pn533_wq_cmd(struct work_struct *work)
871{
872 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
873 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200874 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200875
876 mutex_lock(&dev->cmd_lock);
877
878 if (list_empty(&dev->cmd_queue)) {
879 dev->cmd_pending = 0;
880 mutex_unlock(&dev->cmd_lock);
881 return;
882 }
883
884 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
885
Szymon Janc60ad07a2012-10-25 17:29:45 +0200886 list_del(&cmd->queue);
887
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200888 mutex_unlock(&dev->cmd_lock);
889
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200890 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200891 pn533_send_async_complete, cmd);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200892 if (rc < 0) {
893 dev_kfree_skb(cmd->req);
894 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200895 kfree(cmd);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200896 }
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200897}
898
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300899struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100900 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300901 struct completion done;
902};
903
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100904static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
905 struct sk_buff *resp)
906{
907 struct pn533_sync_cmd_response *arg = _arg;
908
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100909 arg->resp = resp;
910 complete(&arg->done);
911
912 return 0;
913}
914
915/* pn533_send_cmd_sync
916 *
917 * Please note the req parameter is freed inside the function to
918 * limit a number of return value interpretations by the caller.
919 *
920 * 1. negative in case of error during TX path -> req should be freed
921 *
922 * 2. negative in case of error during RX path -> req should not be freed
923 * as it's been already freed at the begining of RX path by
924 * async_complete_cb.
925 *
926 * 3. valid pointer in case of succesfult RX path
927 *
928 * A caller has to check a return value with IS_ERR macro. If the test pass,
929 * the returned pointer is valid.
930 *
931 * */
932static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
933 struct sk_buff *req)
934{
935 int rc;
936 struct pn533_sync_cmd_response arg;
937
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100938 init_completion(&arg.done);
939
940 rc = pn533_send_cmd_async(dev, cmd_code, req,
941 pn533_send_sync_complete, &arg);
942 if (rc) {
943 dev_kfree_skb(req);
944 return ERR_PTR(rc);
945 }
946
947 wait_for_completion(&arg.done);
948
949 return arg.resp;
950}
951
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300952static void pn533_send_complete(struct urb *urb)
953{
954 struct pn533 *dev = urb->context;
955
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300956 switch (urb->status) {
957 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100958 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300959 case -ECONNRESET:
960 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100961 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100962 "The urb has been stopped (status %d)",
963 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300964 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100965 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300966 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100967 nfc_dev_err(&dev->interface->dev,
968 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300969 }
970}
971
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100972static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100973{
974 struct sk_buff *skb;
975
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100976 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100977 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100978 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100979
980 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100981 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100982
983 return skb;
984}
985
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300986struct pn533_target_type_a {
987 __be16 sens_res;
988 u8 sel_res;
989 u8 nfcid_len;
990 u8 nfcid_data[];
991} __packed;
992
993
994#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
995#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
996#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
997
998#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
999#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1000
1001#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1002#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1003
1004#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1005#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1006#define PN533_TYPE_A_SEL_PROT_DEP 2
1007#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1008
1009static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1010 int target_data_len)
1011{
1012 u8 ssd;
1013 u8 platconf;
1014
1015 if (target_data_len < sizeof(struct pn533_target_type_a))
1016 return false;
1017
1018 /* The lenght check of nfcid[] and ats[] are not being performed because
1019 the values are not being used */
1020
1021 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1022 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1023 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1024
1025 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001026 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1027 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1028 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001029 return false;
1030
1031 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1032 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1033 return false;
1034
1035 return true;
1036}
1037
1038static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1039 int tgt_data_len)
1040{
1041 struct pn533_target_type_a *tgt_type_a;
1042
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001043 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001044
1045 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1046 return -EPROTO;
1047
1048 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1049 case PN533_TYPE_A_SEL_PROT_MIFARE:
1050 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1051 break;
1052 case PN533_TYPE_A_SEL_PROT_ISO14443:
1053 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1054 break;
1055 case PN533_TYPE_A_SEL_PROT_DEP:
1056 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1057 break;
1058 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1059 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1060 NFC_PROTO_NFC_DEP_MASK;
1061 break;
1062 }
1063
1064 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1065 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001066 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1067 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001068
1069 return 0;
1070}
1071
1072struct pn533_target_felica {
1073 u8 pol_res;
1074 u8 opcode;
1075 u8 nfcid2[8];
1076 u8 pad[8];
1077 /* optional */
1078 u8 syst_code[];
1079} __packed;
1080
1081#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1082#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1083
1084static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1085 int target_data_len)
1086{
1087 if (target_data_len < sizeof(struct pn533_target_felica))
1088 return false;
1089
1090 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1091 return false;
1092
1093 return true;
1094}
1095
1096static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1097 int tgt_data_len)
1098{
1099 struct pn533_target_felica *tgt_felica;
1100
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001101 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001102
1103 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1104 return -EPROTO;
1105
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001106 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1107 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001108 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1109 else
1110 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1111
Samuel Ortiz79757542012-03-05 01:03:45 +01001112 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1113 nfc_tgt->sensf_res_len = 9;
1114
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001115 return 0;
1116}
1117
1118struct pn533_target_jewel {
1119 __be16 sens_res;
1120 u8 jewelid[4];
1121} __packed;
1122
1123static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1124 int target_data_len)
1125{
1126 u8 ssd;
1127 u8 platconf;
1128
1129 if (target_data_len < sizeof(struct pn533_target_jewel))
1130 return false;
1131
1132 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1133 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1134 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1135
1136 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001137 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1138 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1139 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001140 return false;
1141
1142 return true;
1143}
1144
1145static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1146 int tgt_data_len)
1147{
1148 struct pn533_target_jewel *tgt_jewel;
1149
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001150 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001151
1152 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1153 return -EPROTO;
1154
1155 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1156 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001157 nfc_tgt->nfcid1_len = 4;
1158 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001159
1160 return 0;
1161}
1162
1163struct pn533_type_b_prot_info {
1164 u8 bitrate;
1165 u8 fsci_type;
1166 u8 fwi_adc_fo;
1167} __packed;
1168
1169#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1170#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1171#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1172
1173struct pn533_type_b_sens_res {
1174 u8 opcode;
1175 u8 nfcid[4];
1176 u8 appdata[4];
1177 struct pn533_type_b_prot_info prot_info;
1178} __packed;
1179
1180#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1181
1182struct pn533_target_type_b {
1183 struct pn533_type_b_sens_res sensb_res;
1184 u8 attrib_res_len;
1185 u8 attrib_res[];
1186} __packed;
1187
1188static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1189 int target_data_len)
1190{
1191 if (target_data_len < sizeof(struct pn533_target_type_b))
1192 return false;
1193
1194 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1195 return false;
1196
1197 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1198 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1199 return false;
1200
1201 return true;
1202}
1203
1204static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1205 int tgt_data_len)
1206{
1207 struct pn533_target_type_b *tgt_type_b;
1208
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001209 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001210
1211 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1212 return -EPROTO;
1213
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001214 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001215
1216 return 0;
1217}
1218
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001219static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1220 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001221{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001222 struct nfc_target nfc_tgt;
1223 int rc;
1224
1225 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001226 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001227
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001228 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229 return -EPROTO;
1230
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001231 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1232
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001233 switch (dev->poll_mod_curr) {
1234 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001235 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236 break;
1237 case PN533_POLL_MOD_212KBPS_FELICA:
1238 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001239 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001240 break;
1241 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001242 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243 break;
1244 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001245 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001246 break;
1247 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001248 nfc_dev_err(&dev->interface->dev,
1249 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001250 return -EPROTO;
1251 }
1252
1253 if (rc)
1254 return rc;
1255
1256 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001257 nfc_dev_dbg(&dev->interface->dev,
1258 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001259 return -EAGAIN;
1260 }
1261
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001262 nfc_dev_dbg(&dev->interface->dev,
1263 "Target found - supported protocols: 0x%x",
1264 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001265
1266 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1267
1268 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1269
1270 return 0;
1271}
1272
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001273static inline void pn533_poll_next_mod(struct pn533 *dev)
1274{
1275 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1276}
1277
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001278static void pn533_poll_reset_mod_list(struct pn533 *dev)
1279{
1280 dev->poll_mod_count = 0;
1281}
1282
1283static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1284{
1285 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001286 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001287 dev->poll_mod_count++;
1288}
1289
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001290static void pn533_poll_create_mod_list(struct pn533 *dev,
1291 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001292{
1293 pn533_poll_reset_mod_list(dev);
1294
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001295 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1296 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1297 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001298 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1299
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001300 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1301 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001302 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1303 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1304 }
1305
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001306 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001307 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1308
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001309 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001311
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001312 if (tm_protocols)
1313 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001314}
1315
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001316static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001318 u8 nbtg, tg, *tgdata;
1319 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001320
1321 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1322
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001323 nbtg = resp->data[0];
1324 tg = resp->data[1];
1325 tgdata = &resp->data[2];
1326 tgdata_len = resp->len - 2; /* nbtg + tg */
1327
1328 if (nbtg) {
1329 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001330
1331 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001332 if (rc == 0) {
1333 pn533_poll_reset_mod_list(dev);
1334 return 0;
1335 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001336 }
1337
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001338 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001339}
1340
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001341static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001342{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001343 struct sk_buff *skb;
1344 u8 *felica, *nfcid3, *gb;
1345
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001346 u8 *gbytes = dev->gb;
1347 size_t gbytes_len = dev->gb_len;
1348
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001349 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1350 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1351 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1352 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001353
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001354 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1355 0x0, 0x0, 0x0,
1356 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001357
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001358 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1359 felica (18), nfcid3 (10), gb_len (1) */
1360 gbytes_len +
1361 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001362
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001363 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001364 if (!skb)
1365 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001366
1367 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001368 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001369
1370 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001371 memcpy(skb_put(skb, 6), mifare_params, 6);
1372
1373 /* Felica params */
1374 felica = skb_put(skb, 18);
1375 memcpy(felica, felica_params, 18);
1376 get_random_bytes(felica + 2, 6);
1377
1378 /* NFCID3 */
1379 nfcid3 = skb_put(skb, 10);
1380 memset(nfcid3, 0, 10);
1381 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001382
1383 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001384 *skb_put(skb, 1) = gbytes_len;
1385
1386 gb = skb_put(skb, gbytes_len);
1387 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001388
Samuel Ortizad3823c2012-05-30 23:54:55 +02001389 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001390 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001391
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001392 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001393}
1394
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001395#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001396#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1397static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001398 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001399{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001400 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001401
1402 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1403
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001404 if (IS_ERR(resp))
1405 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001406
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001407 status = resp->data[0];
1408 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001409
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001410 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001411 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001412 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001413 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001414 return 0;
1415 }
1416
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001417 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001418}
1419
1420static void pn533_wq_tg_get_data(struct work_struct *work)
1421{
1422 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001423
1424 struct sk_buff *skb;
1425 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001426
1427 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1428
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001429 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001430 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001431 return;
1432
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001433 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1434 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001435
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001436 if (rc < 0)
1437 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001438
1439 return;
1440}
1441
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001442#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001443static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001444{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001445 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001446 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001447 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001448
1449 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1450
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001451 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001452 return -EINVAL;
1453
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001454 mode = resp->data[0];
1455 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001456
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001457 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1458 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001459
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1461 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001462 comm_mode = NFC_COMM_ACTIVE;
1463
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001464 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001465 return -EOPNOTSUPP;
1466
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 gb = cmd + ATR_REQ_GB_OFFSET;
1468 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001469
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001470 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1471 comm_mode, gb, gb_len);
1472 if (rc < 0) {
1473 nfc_dev_err(&dev->interface->dev,
1474 "Error when signaling target activation");
1475 return rc;
1476 }
1477
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001478 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001479 queue_work(dev->wq, &dev->tg_work);
1480
1481 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001482}
1483
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001484static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001485{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001486 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001487
1488 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1489
1490 /* An ack will cancel the last issued command (poll) */
1491 pn533_send_ack(dev, GFP_ATOMIC);
1492
1493 dev->cancel_listen = 1;
1494
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001495 pn533_poll_next_mod(dev);
1496
1497 queue_work(dev->wq, &dev->poll_work);
1498}
1499
1500static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001501 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502{
1503 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001504 int rc;
1505
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001506 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1507
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001508 if (IS_ERR(resp)) {
1509 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001511 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1512 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001513
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001514 if (rc == -ENOENT) {
1515 if (dev->poll_mod_count != 0)
1516 return rc;
1517 else
1518 goto stop_poll;
1519 } else if (rc < 0) {
1520 nfc_dev_err(&dev->interface->dev,
1521 "Error %d when running poll", rc);
1522 goto stop_poll;
1523 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001524 }
1525
1526 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1527
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001528 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001529 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001530 rc = pn533_init_target_complete(dev, resp);
1531 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001532 }
1533
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001534 /* Initiator mode */
1535 rc = pn533_start_poll_complete(dev, resp);
1536 if (!rc)
1537 goto done;
1538
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001539 if (!dev->poll_mod_count) {
1540 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1541 goto done;
1542 }
1543
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001545 queue_work(dev->wq, &dev->poll_work);
1546
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001547done:
1548 dev_kfree_skb(resp);
1549 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001550
1551stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001552 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1553
Samuel Ortizad3823c2012-05-30 23:54:55 +02001554 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001555 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001556 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001558
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001559static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1560 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001561{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001562 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001563
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001564 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001565 if (!skb)
1566 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001567
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001568 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001569
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001570 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001571}
1572
1573static int pn533_send_poll_frame(struct pn533 *dev)
1574{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001575 struct pn533_poll_modulations *mod;
1576 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001577 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001578 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001579
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001580 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001581
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1583 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001584
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 if (mod->len == 0) { /* Listen mode */
1586 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001587 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001588 } else { /* Polling mode */
1589 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001590 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001591 }
1592
1593 if (!skb) {
1594 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1595 return -ENOMEM;
1596 }
1597
1598 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1599 NULL);
1600 if (rc < 0) {
1601 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001602 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001603 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001604
1605 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001606}
1607
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001608static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001609{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1611 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001612 int rc;
1613
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001614 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1615
1616 nfc_dev_dbg(&dev->interface->dev,
1617 "%s cancel_listen %d modulation len %d",
1618 __func__, dev->cancel_listen, cur_mod->len);
1619
1620 if (dev->cancel_listen == 1) {
1621 dev->cancel_listen = 0;
1622 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001623 }
1624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625 rc = pn533_send_poll_frame(dev);
1626 if (rc)
1627 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001628
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001629 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1630 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001631
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001632 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001633}
1634
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001635static int pn533_start_poll(struct nfc_dev *nfc_dev,
1636 u32 im_protocols, u32 tm_protocols)
1637{
1638 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1639
1640 nfc_dev_dbg(&dev->interface->dev,
1641 "%s: im protocols 0x%x tm protocols 0x%x",
1642 __func__, im_protocols, tm_protocols);
1643
1644 if (dev->tgt_active_prot) {
1645 nfc_dev_err(&dev->interface->dev,
1646 "Cannot poll with a target already activated");
1647 return -EBUSY;
1648 }
1649
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001650 if (dev->tgt_mode) {
1651 nfc_dev_err(&dev->interface->dev,
1652 "Cannot poll while already being activated");
1653 return -EBUSY;
1654 }
1655
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001656 if (tm_protocols) {
1657 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1658 if (dev->gb == NULL)
1659 tm_protocols = 0;
1660 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001661
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001662 dev->poll_mod_curr = 0;
1663 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1664 dev->poll_protocols = im_protocols;
1665 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001666
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001667 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001668}
1669
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001670static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1671{
1672 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1673
1674 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1675
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001676 del_timer(&dev->listen_timer);
1677
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001678 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001679 nfc_dev_dbg(&dev->interface->dev,
1680 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001681 return;
1682 }
1683
1684 /* An ack will cancel the last issued command (poll) */
1685 pn533_send_ack(dev, GFP_KERNEL);
1686
1687 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1688 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001689
1690 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001691}
1692
1693static int pn533_activate_target_nfcdep(struct pn533 *dev)
1694{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001695 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001696 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001697 int rc;
1698
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001699 struct sk_buff *skb;
1700 struct sk_buff *resp;
1701
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001702 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1703
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001704 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001705 if (!skb)
1706 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001707
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001708 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1709 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001710
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001711 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1712 if (IS_ERR(resp))
1713 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001715 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001716 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001717 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001718 nfc_dev_err(&dev->interface->dev,
1719 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001720 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001721 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001722 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001723
Samuel Ortiz541d9202011-12-14 16:43:10 +01001724 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 gt_len = resp->len - 16;
1726 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001727
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001728 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001729 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001730}
1731
Eric Lapuyade90099432012-05-07 12:31:13 +02001732static int pn533_activate_target(struct nfc_dev *nfc_dev,
1733 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001734{
1735 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1736 int rc;
1737
1738 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001739 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001740
1741 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001742 nfc_dev_err(&dev->interface->dev,
1743 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001744 return -EBUSY;
1745 }
1746
1747 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001748 nfc_dev_err(&dev->interface->dev,
1749 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001750 return -EBUSY;
1751 }
1752
1753 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001754 nfc_dev_err(&dev->interface->dev,
1755 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001756 return -EINVAL;
1757 }
1758
1759 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001760 nfc_dev_err(&dev->interface->dev,
1761 "Target doesn't support requested proto %u",
1762 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001763 return -EINVAL;
1764 }
1765
1766 if (protocol == NFC_PROTO_NFC_DEP) {
1767 rc = pn533_activate_target_nfcdep(dev);
1768 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001769 nfc_dev_err(&dev->interface->dev,
1770 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001771 return rc;
1772 }
1773 }
1774
1775 dev->tgt_active_prot = protocol;
1776 dev->tgt_available_prots = 0;
1777
1778 return 0;
1779}
1780
Eric Lapuyade90099432012-05-07 12:31:13 +02001781static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1782 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001783{
1784 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001785
1786 struct sk_buff *skb;
1787 struct sk_buff *resp;
1788
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001789 int rc;
1790
1791 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1792
1793 if (!dev->tgt_active_prot) {
1794 nfc_dev_err(&dev->interface->dev, "There is no active target");
1795 return;
1796 }
1797
1798 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001799 skb_queue_purge(&dev->resp_q);
1800
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001801 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001802 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001803 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001804
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001805 *skb_put(skb, 1) = 1; /* TG*/
1806
1807 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1808 if (IS_ERR(resp))
1809 return;
1810
1811 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001812 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001813 nfc_dev_err(&dev->interface->dev,
1814 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001815
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001816 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001817 return;
1818}
1819
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001820
1821static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001822 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001823{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001824 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001825 u8 target_gt_len;
1826 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001827 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001828
1829 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001830
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001831 if (IS_ERR(resp))
1832 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001833
1834 if (dev->tgt_available_prots &&
1835 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1836 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001837 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 rc = -EINVAL;
1839 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840 }
1841
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001842 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1843
1844 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001845 if (rc != PN533_CMD_RET_SUCCESS) {
1846 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001847 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001848 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001849 }
1850
1851 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001852 struct nfc_target nfc_target;
1853
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001854 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1855
1856 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001857 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001858 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001859 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1860 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001861 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862
1863 dev->tgt_available_prots = 0;
1864 }
1865
1866 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1867
1868 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001869 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001870 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001871 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001872 if (rc == 0)
1873 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001874 dev->nfc_dev->targets[0].idx,
1875 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001877error:
1878 dev_kfree_skb(resp);
1879 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001880}
1881
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001882static int pn533_mod_to_baud(struct pn533 *dev)
1883{
1884 switch (dev->poll_mod_curr) {
1885 case PN533_POLL_MOD_106KBPS_A:
1886 return 0;
1887 case PN533_POLL_MOD_212KBPS_FELICA:
1888 return 1;
1889 case PN533_POLL_MOD_424KBPS_FELICA:
1890 return 2;
1891 default:
1892 return -EINVAL;
1893 }
1894}
1895
Samuel Ortizd7f33452012-05-29 21:45:21 +02001896#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001897static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001898 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001899{
1900 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001901 struct sk_buff *skb;
1902 int rc, baud, skb_len;
1903 u8 *next, *arg;
1904
Samuel Ortizd7f33452012-05-29 21:45:21 +02001905 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906
1907 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1908
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001909 if (dev->poll_mod_count) {
1910 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001911 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001912 return -EBUSY;
1913 }
1914
1915 if (dev->tgt_active_prot) {
1916 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001917 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001918 return -EBUSY;
1919 }
1920
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001921 baud = pn533_mod_to_baud(dev);
1922 if (baud < 0) {
1923 nfc_dev_err(&dev->interface->dev,
1924 "Invalid curr modulation %d", dev->poll_mod_curr);
1925 return baud;
1926 }
1927
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001928 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001929 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001930 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001931
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001932 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001933 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001934 return -ENOMEM;
1935
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001936 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1937 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001938
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001939 next = skb_put(skb, 1); /* Next */
1940 *next = 0;
1941
1942 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1943 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1944 PASSIVE_DATA_LEN);
1945 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001946 }
1947
Samuel Ortiz47807d32012-03-05 01:03:50 +01001948 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001949 memcpy(skb_put(skb, gb_len), gb, gb_len);
1950 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001951 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001952 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001953 }
1954
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001955 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1956 if (!arg) {
1957 dev_kfree_skb(skb);
1958 return -ENOMEM;
1959 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001960
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001961 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001962
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001963 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1964 pn533_in_dep_link_up_complete, arg);
1965
1966 if (rc < 0) {
1967 dev_kfree_skb(skb);
1968 kfree(arg);
1969 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001970
1971 return rc;
1972}
1973
1974static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1975{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001976 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1977
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001978 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1979
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001980 pn533_poll_reset_mod_list(dev);
1981
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001982 if (dev->tgt_mode || dev->tgt_active_prot) {
1983 pn533_send_ack(dev, GFP_KERNEL);
1984 usb_kill_urb(dev->in_urb);
1985 }
1986
1987 dev->tgt_active_prot = 0;
1988 dev->tgt_mode = 0;
1989
1990 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001991
1992 return 0;
1993}
1994
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001995struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001996 data_exchange_cb_t cb;
1997 void *cb_context;
1998};
1999
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002000static struct sk_buff *pn533_build_response(struct pn533 *dev)
2001{
2002 struct sk_buff *skb, *tmp, *t;
2003 unsigned int skb_len = 0, tmp_len = 0;
2004
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002005 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002006
2007 if (skb_queue_empty(&dev->resp_q))
2008 return NULL;
2009
2010 if (skb_queue_len(&dev->resp_q) == 1) {
2011 skb = skb_dequeue(&dev->resp_q);
2012 goto out;
2013 }
2014
2015 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2016 skb_len += tmp->len;
2017
2018 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2019 __func__, skb_len);
2020
2021 skb = alloc_skb(skb_len, GFP_KERNEL);
2022 if (skb == NULL)
2023 goto out;
2024
2025 skb_put(skb, skb_len);
2026
2027 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2028 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2029 tmp_len += tmp->len;
2030 }
2031
2032out:
2033 skb_queue_purge(&dev->resp_q);
2034
2035 return skb;
2036}
2037
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002038static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002039 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002040{
2041 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002042 struct sk_buff *skb;
2043 int rc = 0;
2044 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002045
2046 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2047
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002048 if (IS_ERR(resp)) {
2049 rc = PTR_ERR(resp);
2050 goto _error;
2051 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002052
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002053 status = resp->data[0];
2054 ret = status & PN533_CMD_RET_MASK;
2055 mi = status & PN533_CMD_MI_MASK;
2056
2057 skb_pull(resp, sizeof(status));
2058
2059 if (ret != PN533_CMD_RET_SUCCESS) {
2060 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002061 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002062 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002063 goto error;
2064 }
2065
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002066 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002067
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002068 if (mi) {
2069 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002070 queue_work(dev->wq, &dev->mi_work);
2071 return -EINPROGRESS;
2072 }
2073
2074 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002075 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002076 goto error;
2077
2078 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002079 kfree(arg);
2080 return 0;
2081
2082error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002083 dev_kfree_skb(resp);
2084_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002085 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002086 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002087 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002088 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002089}
2090
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002091static int pn533_transceive(struct nfc_dev *nfc_dev,
2092 struct nfc_target *target, struct sk_buff *skb,
2093 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002094{
2095 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002096 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002097 int rc;
2098
2099 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2100
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002101 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2102 /* TODO: Implement support to multi-part data exchange */
2103 nfc_dev_err(&dev->interface->dev,
2104 "Data length greater than the max allowed: %d",
2105 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2106 rc = -ENOSYS;
2107 goto error;
2108 }
2109
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002110 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002111 nfc_dev_err(&dev->interface->dev,
2112 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002113 rc = -EINVAL;
2114 goto error;
2115 }
2116
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002117 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002118 if (!arg) {
2119 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002120 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002121 }
2122
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123 arg->cb = cb;
2124 arg->cb_context = cb_context;
2125
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002126 switch (dev->device_type) {
2127 case PN533_DEVICE_PASORI:
2128 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2129 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2130 skb,
2131 pn533_data_exchange_complete,
2132 arg);
2133
2134 break;
2135 }
2136 default:
2137 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2138
2139 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2140 skb, pn533_data_exchange_complete,
2141 arg);
2142
2143 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002144 }
2145
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002146 if (rc < 0) /* rc from send_async */
2147 goto error;
2148
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002149 return 0;
2150
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002152 kfree(arg);
2153 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002154 return rc;
2155}
2156
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002157static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002158 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002159{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002160 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002161
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002162 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2163
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002164 if (IS_ERR(resp))
2165 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002166
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002168
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002169 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002170
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002171 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002172 nfc_tm_deactivated(dev->nfc_dev);
2173
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002174 dev->tgt_mode = 0;
2175
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002176 return 0;
2177 }
2178
2179 queue_work(dev->wq, &dev->tg_work);
2180
2181 return 0;
2182}
2183
2184static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2185{
2186 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002187 int rc;
2188
2189 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2190
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002191 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002192 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002193 "Data length greater than the max allowed: %d",
2194 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2195 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002196 }
2197
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002198 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2199 pn533_tm_send_complete, NULL);
2200 if (rc < 0)
2201 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002202
2203 return rc;
2204}
2205
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002206static void pn533_wq_mi_recv(struct work_struct *work)
2207{
2208 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002209
2210 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002211 int rc;
2212
2213 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2214
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002215 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002216 if (!skb)
2217 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002218
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002219 switch (dev->device_type) {
2220 case PN533_DEVICE_PASORI:
2221 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2222 rc = pn533_send_cmd_direct_async(dev,
2223 PN533_CMD_IN_COMM_THRU,
2224 skb,
2225 pn533_data_exchange_complete,
2226 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002227
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002228 break;
2229 }
2230 default:
2231 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002232
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002233 rc = pn533_send_cmd_direct_async(dev,
2234 PN533_CMD_IN_DATA_EXCHANGE,
2235 skb,
2236 pn533_data_exchange_complete,
2237 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002238
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002239 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002240 }
2241
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002242 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002243 return;
2244
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 nfc_dev_err(&dev->interface->dev,
2246 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002247
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002248 dev_kfree_skb(skb);
2249 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002250
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002251error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002252 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002253 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002254}
2255
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002256static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2257 u8 cfgdata_len)
2258{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002259 struct sk_buff *skb;
2260 struct sk_buff *resp;
2261
2262 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002263
2264 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2265
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002266 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002267
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002268 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002269 if (!skb)
2270 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002271
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002272 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2273 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002274
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002275 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2276 if (IS_ERR(resp))
2277 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002278
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002279 dev_kfree_skb(resp);
2280 return 0;
2281}
2282
2283static int pn533_get_firmware_version(struct pn533 *dev,
2284 struct pn533_fw_version *fv)
2285{
2286 struct sk_buff *skb;
2287 struct sk_buff *resp;
2288
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002289 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002290 if (!skb)
2291 return -ENOMEM;
2292
2293 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2294 if (IS_ERR(resp))
2295 return PTR_ERR(resp);
2296
2297 fv->ic = resp->data[0];
2298 fv->ver = resp->data[1];
2299 fv->rev = resp->data[2];
2300 fv->support = resp->data[3];
2301
2302 dev_kfree_skb(resp);
2303 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002304}
2305
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002306static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002307{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002308 struct sk_buff *skb;
2309 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002310
2311 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2312
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002313 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002314 if (!skb)
2315 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002316
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002317 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002318
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002319 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2320 if (IS_ERR(resp))
2321 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002322
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002323 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002324
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002325 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002326}
2327
2328static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002329 .dev_up = NULL,
2330 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002331 .dep_link_up = pn533_dep_link_up,
2332 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002333 .start_poll = pn533_start_poll,
2334 .stop_poll = pn533_stop_poll,
2335 .activate_target = pn533_activate_target,
2336 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002337 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002338 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002339};
2340
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002341static int pn533_setup(struct pn533 *dev)
2342{
2343 struct pn533_config_max_retries max_retries;
2344 struct pn533_config_timing timing;
2345 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2346 int rc;
2347
2348 switch (dev->device_type) {
2349 case PN533_DEVICE_STD:
2350 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2351 max_retries.mx_rty_psl = 2;
2352 max_retries.mx_rty_passive_act =
2353 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2354
2355 timing.rfu = PN533_CONFIG_TIMING_102;
2356 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2357 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2358
2359 break;
2360
2361 case PN533_DEVICE_PASORI:
2362 max_retries.mx_rty_atr = 0x2;
2363 max_retries.mx_rty_psl = 0x1;
2364 max_retries.mx_rty_passive_act =
2365 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2366
2367 timing.rfu = PN533_CONFIG_TIMING_102;
2368 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2369 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2370
2371 break;
2372
2373 default:
2374 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2375 dev->device_type);
2376 return -EINVAL;
2377 }
2378
2379 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2380 (u8 *)&max_retries, sizeof(max_retries));
2381 if (rc) {
2382 nfc_dev_err(&dev->interface->dev,
2383 "Error on setting MAX_RETRIES config");
2384 return rc;
2385 }
2386
2387
2388 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2389 (u8 *)&timing, sizeof(timing));
2390 if (rc) {
2391 nfc_dev_err(&dev->interface->dev,
2392 "Error on setting RF timings");
2393 return rc;
2394 }
2395
2396 switch (dev->device_type) {
2397 case PN533_DEVICE_STD:
2398 break;
2399
2400 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002401 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002402
2403 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2404 pasori_cfg, 3);
2405 if (rc) {
2406 nfc_dev_err(&dev->interface->dev,
2407 "Error while settings PASORI config");
2408 return rc;
2409 }
2410
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002411 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002412
2413 break;
2414 }
2415
2416 return 0;
2417}
2418
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002419static int pn533_probe(struct usb_interface *interface,
2420 const struct usb_device_id *id)
2421{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002422 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002423 struct pn533 *dev;
2424 struct usb_host_interface *iface_desc;
2425 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002426 int in_endpoint = 0;
2427 int out_endpoint = 0;
2428 int rc = -ENOMEM;
2429 int i;
2430 u32 protocols;
2431
2432 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2433 if (!dev)
2434 return -ENOMEM;
2435
2436 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2437 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002438 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002439
2440 iface_desc = interface->cur_altsetting;
2441 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2442 endpoint = &iface_desc->endpoint[i].desc;
2443
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002444 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002446
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002447 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002448 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002449 }
2450
2451 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002452 nfc_dev_err(&interface->dev,
2453 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002454 rc = -ENODEV;
2455 goto error;
2456 }
2457
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002459 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2460
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002461 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002462 goto error;
2463
2464 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002465 usb_rcvbulkpipe(dev->udev, in_endpoint),
2466 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002467 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002468 usb_sndbulkpipe(dev->udev, out_endpoint),
2469 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002470
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002471 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2472 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002473 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002474 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002475 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002476 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002477 if (dev->wq == NULL)
2478 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002479
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002480 init_timer(&dev->listen_timer);
2481 dev->listen_timer.data = (unsigned long) dev;
2482 dev->listen_timer.function = pn533_listen_mode_timer;
2483
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002484 skb_queue_head_init(&dev->resp_q);
2485
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002486 INIT_LIST_HEAD(&dev->cmd_queue);
2487
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002488 usb_set_intfdata(interface, dev);
2489
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002490 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002491
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002492 dev->device_type = id->driver_info;
2493 switch (dev->device_type) {
2494 case PN533_DEVICE_STD:
2495 protocols = PN533_ALL_PROTOCOLS;
2496 break;
2497
2498 case PN533_DEVICE_PASORI:
2499 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2500 break;
2501
2502 default:
2503 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2504 dev->device_type);
2505 rc = -EINVAL;
2506 goto destroy_wq;
2507 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002508
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002509 memset(&fw_ver, 0, sizeof(fw_ver));
2510 rc = pn533_get_firmware_version(dev, &fw_ver);
2511 if (rc < 0)
2512 goto destroy_wq;
2513
2514 nfc_dev_info(&dev->interface->dev,
2515 "NXP PN533 firmware ver %d.%d now attached",
2516 fw_ver.ver, fw_ver.rev);
2517
2518
Samuel Ortize8753042011-08-19 15:47:11 +02002519 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002520 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002521 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002522 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002523 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002524 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002525 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002526
2527 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2528 nfc_set_drvdata(dev->nfc_dev, dev);
2529
2530 rc = nfc_register_device(dev->nfc_dev);
2531 if (rc)
2532 goto free_nfc_dev;
2533
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002534 rc = pn533_setup(dev);
2535 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002536 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002537
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002538 return 0;
2539
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002540unregister_nfc_dev:
2541 nfc_unregister_device(dev->nfc_dev);
2542
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002543free_nfc_dev:
2544 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002545
Samuel Ortiz4849f852012-04-10 19:43:17 +02002546destroy_wq:
2547 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002548error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002550 usb_free_urb(dev->out_urb);
2551 kfree(dev);
2552 return rc;
2553}
2554
2555static void pn533_disconnect(struct usb_interface *interface)
2556{
2557 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002558 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002559
2560 dev = usb_get_intfdata(interface);
2561 usb_set_intfdata(interface, NULL);
2562
2563 nfc_unregister_device(dev->nfc_dev);
2564 nfc_free_device(dev->nfc_dev);
2565
2566 usb_kill_urb(dev->in_urb);
2567 usb_kill_urb(dev->out_urb);
2568
Samuel Ortiz4849f852012-04-10 19:43:17 +02002569 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002570
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002571 skb_queue_purge(&dev->resp_q);
2572
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002573 del_timer(&dev->listen_timer);
2574
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002575 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2576 list_del(&cmd->queue);
2577 kfree(cmd);
2578 }
2579
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002580 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002581 usb_free_urb(dev->out_urb);
2582 kfree(dev);
2583
Dan Carpenter276556d2011-07-08 10:21:15 +03002584 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002585}
2586
2587static struct usb_driver pn533_driver = {
2588 .name = "pn533",
2589 .probe = pn533_probe,
2590 .disconnect = pn533_disconnect,
2591 .id_table = pn533_table,
2592};
2593
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002594module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002596MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2597MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2598MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002599MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2600MODULE_VERSION(VERSION);
2601MODULE_LICENSE("GPL");