blob: 72860569fb1a76dbfb13c983ed550596d6197e5a [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
30#define VERSION "0.1"
31
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define PN533_DEVICE_STD 0x1
42#define PN533_DEVICE_PASORI 0x2
43
Samuel Ortiz01d719a2012-07-04 00:14:04 +020044#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
45 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
46 NFC_PROTO_NFC_DEP_MASK |\
47 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020048
49#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
50 NFC_PROTO_MIFARE_MASK | \
51 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020052 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020053 NFC_PROTO_NFC_DEP_MASK)
54
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030055static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
57 .idVendor = PN533_VENDOR_ID,
58 .idProduct = PN533_PRODUCT_ID,
59 .driver_info = PN533_DEVICE_STD,
60 },
61 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
62 .idVendor = SCM_VENDOR_ID,
63 .idProduct = SCL3711_PRODUCT_ID,
64 .driver_info = PN533_DEVICE_STD,
65 },
66 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
67 .idVendor = SONY_VENDOR_ID,
68 .idProduct = PASORI_PRODUCT_ID,
69 .driver_info = PN533_DEVICE_PASORI,
70 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030071 { }
72};
73MODULE_DEVICE_TABLE(usb, pn533_table);
74
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020075/* How much time we spend listening for initiators */
76#define PN533_LISTEN_TIME 2
77
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020078/* Standard pn533 frame definitions */
79#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010080 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020081#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010083/*
84 * Max extended frame payload len, excluding TFI and CC
85 * which are already in PN533_FRAME_HEADER_LEN.
86 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020087#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010088
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020089#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010090 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020091#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
92#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030093
94/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020095#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030096
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020097/* standard frame identifier: in/out/error */
98#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
99#define PN533_STD_FRAME_DIR_OUT 0xD4
100#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300101
102/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200103#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104
105#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
106#define PN533_CMD_RF_CONFIGURATION 0x32
107#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200108#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300109#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
110#define PN533_CMD_IN_ATR 0x50
111#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100112#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300113
Samuel Ortizad3823c2012-05-30 23:54:55 +0200114#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200115#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200116#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100117#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
120
121/* PN533 Return codes */
122#define PN533_CMD_RET_MASK 0x3F
123#define PN533_CMD_MI_MASK 0x40
124#define PN533_CMD_RET_SUCCESS 0x00
125
126struct pn533;
127
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100128typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300129
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100130typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
131 struct sk_buff *resp);
132
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133/* structs for pn533 commands */
134
135/* PN533_CMD_GET_FIRMWARE_VERSION */
136struct pn533_fw_version {
137 u8 ic;
138 u8 ver;
139 u8 rev;
140 u8 support;
141};
142
143/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200144#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200146#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300147
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200148#define PN533_CONFIG_TIMING_102 0xb
149#define PN533_CONFIG_TIMING_204 0xc
150#define PN533_CONFIG_TIMING_409 0xd
151#define PN533_CONFIG_TIMING_819 0xe
152
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
154#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
155
156struct pn533_config_max_retries {
157 u8 mx_rty_atr;
158 u8 mx_rty_psl;
159 u8 mx_rty_passive_act;
160} __packed;
161
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200162struct pn533_config_timing {
163 u8 rfu;
164 u8 atr_res_timeout;
165 u8 dep_timeout;
166} __packed;
167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300168/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
169
170/* felica commands opcode */
171#define PN533_FELICA_OPC_SENSF_REQ 0
172#define PN533_FELICA_OPC_SENSF_RES 1
173/* felica SENSF_REQ parameters */
174#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
175#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
176#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
177#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
178
179/* type B initiator_data values */
180#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
181#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
182#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
183
184union pn533_cmd_poll_initdata {
185 struct {
186 u8 afi;
187 u8 polling_method;
188 } __packed type_b;
189 struct {
190 u8 opcode;
191 __be16 sc;
192 u8 rc;
193 u8 tsn;
194 } __packed felica;
195};
196
197/* Poll modulations */
198enum {
199 PN533_POLL_MOD_106KBPS_A,
200 PN533_POLL_MOD_212KBPS_FELICA,
201 PN533_POLL_MOD_424KBPS_FELICA,
202 PN533_POLL_MOD_106KBPS_JEWEL,
203 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200204 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205
206 __PN533_POLL_MOD_AFTER_LAST,
207};
208#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
209
210struct pn533_poll_modulations {
211 struct {
212 u8 maxtg;
213 u8 brty;
214 union pn533_cmd_poll_initdata initiator_data;
215 } __packed data;
216 u8 len;
217};
218
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100219static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300220 [PN533_POLL_MOD_106KBPS_A] = {
221 .data = {
222 .maxtg = 1,
223 .brty = 0,
224 },
225 .len = 2,
226 },
227 [PN533_POLL_MOD_212KBPS_FELICA] = {
228 .data = {
229 .maxtg = 1,
230 .brty = 1,
231 .initiator_data.felica = {
232 .opcode = PN533_FELICA_OPC_SENSF_REQ,
233 .sc = PN533_FELICA_SENSF_SC_ALL,
234 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
235 .tsn = 0,
236 },
237 },
238 .len = 7,
239 },
240 [PN533_POLL_MOD_424KBPS_FELICA] = {
241 .data = {
242 .maxtg = 1,
243 .brty = 2,
244 .initiator_data.felica = {
245 .opcode = PN533_FELICA_OPC_SENSF_REQ,
246 .sc = PN533_FELICA_SENSF_SC_ALL,
247 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
248 .tsn = 0,
249 },
250 },
251 .len = 7,
252 },
253 [PN533_POLL_MOD_106KBPS_JEWEL] = {
254 .data = {
255 .maxtg = 1,
256 .brty = 4,
257 },
258 .len = 2,
259 },
260 [PN533_POLL_MOD_847KBPS_B] = {
261 .data = {
262 .maxtg = 1,
263 .brty = 8,
264 .initiator_data.type_b = {
265 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
266 .polling_method =
267 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
268 },
269 },
270 .len = 3,
271 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200272 [PN533_LISTEN_MOD] = {
273 .len = 0,
274 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300275};
276
277/* PN533_CMD_IN_ATR */
278
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300279struct pn533_cmd_activate_response {
280 u8 status;
281 u8 nfcid3t[10];
282 u8 didt;
283 u8 bst;
284 u8 brt;
285 u8 to;
286 u8 ppt;
287 /* optional */
288 u8 gt[];
289} __packed;
290
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100291struct pn533_cmd_jump_dep_response {
292 u8 status;
293 u8 tg;
294 u8 nfcid3t[10];
295 u8 didt;
296 u8 bst;
297 u8 brt;
298 u8 to;
299 u8 ppt;
300 /* optional */
301 u8 gt[];
302} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303
Samuel Ortizad3823c2012-05-30 23:54:55 +0200304
305/* PN533_TG_INIT_AS_TARGET */
306#define PN533_INIT_TARGET_PASSIVE 0x1
307#define PN533_INIT_TARGET_DEP 0x2
308
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200309#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
310#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
311#define PN533_INIT_TARGET_RESP_DEP 0x4
312
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313struct pn533 {
314 struct usb_device *udev;
315 struct usb_interface *interface;
316 struct nfc_dev *nfc_dev;
317
318 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200321 struct sk_buff_head resp_q;
322
Samuel Ortiz4849f852012-04-10 19:43:17 +0200323 struct workqueue_struct *wq;
324 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200325 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200326 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200327 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200328 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200330 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200331 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100333 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200334 struct mutex cmd_lock;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200335 struct pn533_cmd *cmd;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300336
337 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
338 u8 poll_mod_count;
339 u8 poll_mod_curr;
340 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200341 u32 listen_protocols;
342
343 u8 *gb;
344 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300345
346 u8 tgt_available_prots;
347 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200348 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200349
350 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200351
352 struct list_head cmd_queue;
353 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100354
355 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200356};
357
358struct pn533_cmd {
359 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200360 u8 code;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100361 struct sk_buff *req;
362 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100363 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200364 pn533_send_async_complete_t complete_cb;
365 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300366};
367
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200368struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300369 u8 preamble;
370 __be16 start_frame;
371 u8 datalen;
372 u8 datalen_checksum;
373 u8 data[];
374} __packed;
375
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100376struct pn533_frame_ops {
377 void (*tx_frame_init)(void *frame, u8 cmd_code);
378 void (*tx_frame_finish)(void *frame);
379 void (*tx_update_payload_len)(void *frame, int len);
380 int tx_header_len;
381 int tx_tail_len;
382
383 bool (*rx_is_frame_valid)(void *frame);
384 int (*rx_frame_size)(void *frame);
385 int rx_header_len;
386 int rx_tail_len;
387
388 int max_payload_len;
389 u8 (*get_cmd_code)(void *frame);
390};
391
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300392/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200393static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300394{
395 return ~value + 1;
396}
397
398/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200399static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300400{
401 u8 sum = 0;
402 int i;
403
404 for (i = 0; i < datalen; i++)
405 sum += data[i];
406
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200407 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300408}
409
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200410static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300411{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200412 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100413
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300414 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200415 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
416 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
417 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300418 frame->datalen = 2;
419}
420
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200421static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300422{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200423 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100424
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200425 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300426
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200427 PN533_STD_FRAME_CHECKSUM(frame) =
428 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300429
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200430 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300431}
432
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200433static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100434{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200435 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100436
437 frame->datalen += len;
438}
439
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200440static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300441{
442 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200443 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300444
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200445 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300446 return false;
447
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200448 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300449 if (checksum != frame->datalen_checksum)
450 return false;
451
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200452 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
453 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300454 return false;
455
456 return true;
457}
458
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200459static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300460{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200461 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300462 return false;
463
464 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
465 return false;
466
467 return true;
468}
469
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200470static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300471{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200472 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100473
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200474 return sizeof(struct pn533_std_frame) + f->datalen +
475 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100476}
477
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200478static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100479{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200480 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100481
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200482 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100483}
484
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100485static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200486 .tx_frame_init = pn533_std_tx_frame_init,
487 .tx_frame_finish = pn533_std_tx_frame_finish,
488 .tx_update_payload_len = pn533_std_tx_update_payload_len,
489 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
490 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100491
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200492 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
493 .rx_frame_size = pn533_std_rx_frame_size,
494 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
495 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100496
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200497 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
498 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100499};
500
501static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
502{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200503 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200504 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300505}
506
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200507static int pn533_send_async_complete(struct pn533 *dev);
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 Rymarkiewiczddf19d22013-04-03 08:02:08 +0200514 rc = pn533_send_async_complete(dev);
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,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200645 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300646{
647 int rc;
648
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100649 dev->out_urb->transfer_buffer = out->data;
650 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300651
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100652 dev->in_urb->transfer_buffer = in->data;
653 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300654
Samuel Ortize279f842013-03-20 11:27:57 +0100655 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
656 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100657
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100658 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300659 if (rc)
660 return rc;
661
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100662 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300663 if (rc)
664 goto error;
665
666 return 0;
667
668error:
669 usb_unlink_urb(dev->out_urb);
670 return rc;
671}
672
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100673static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
674 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100675{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100676 /* payload is already there, just update datalen */
677 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100678 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100679
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100680
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100681 skb_push(skb, ops->tx_header_len);
682 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100683
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100684 ops->tx_frame_init(skb->data, cmd_code);
685 ops->tx_update_payload_len(skb->data, payload_len);
686 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100687}
688
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200689static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100690{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200691 struct pn533_cmd *cmd = dev->cmd;
692 int status = dev->wq_in_error;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100693
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200694 struct sk_buff *req = cmd->req;
695 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100696
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100697 int rc;
698
699 dev_kfree_skb(req);
700
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100701 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200702 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
703 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100704 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200705 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100706 }
707
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100708 skb_put(resp, dev->ops->rx_frame_size(resp->data));
709 skb_pull(resp, dev->ops->rx_header_len);
710 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100711
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200712 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100713
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200714done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200715 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200716 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100717 return rc;
718}
719
720static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
721 struct sk_buff *req, struct sk_buff *resp,
722 int resp_len,
723 pn533_send_async_complete_t complete_cb,
724 void *complete_cb_context)
725{
726 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100727 int rc = 0;
728
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100729 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100730
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200731 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
732 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100733 return -ENOMEM;
734
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200735 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200736 cmd->req = req;
737 cmd->resp = resp;
738 cmd->resp_len = resp_len;
739 cmd->complete_cb = complete_cb;
740 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100741
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100742 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100743
744 mutex_lock(&dev->cmd_lock);
745
746 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200747 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100748 if (rc)
749 goto error;
750
751 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200752 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100753 goto unlock;
754 }
755
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100756 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
757 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100758
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100759 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100760 list_add_tail(&cmd->queue, &dev->cmd_queue);
761
762 goto unlock;
763
764error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200765 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100766unlock:
767 mutex_unlock(&dev->cmd_lock);
768 return rc;
769}
770
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100771static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
772 struct sk_buff *req,
773 pn533_send_async_complete_t complete_cb,
774 void *complete_cb_context)
775{
776 struct sk_buff *resp;
777 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100778 int resp_len = dev->ops->rx_header_len +
779 dev->ops->max_payload_len +
780 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100781
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100782 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
783 if (!resp)
784 return -ENOMEM;
785
786 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
787 complete_cb_context);
788 if (rc)
789 dev_kfree_skb(resp);
790
791 return rc;
792}
793
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100794static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
795 struct sk_buff *req,
796 pn533_send_async_complete_t complete_cb,
797 void *complete_cb_context)
798{
799 struct sk_buff *resp;
800 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100801 int resp_len = dev->ops->rx_header_len +
802 dev->ops->max_payload_len +
803 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100804
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100805 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100806 if (!resp)
807 return -ENOMEM;
808
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100809 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
810 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100811 if (rc)
812 dev_kfree_skb(resp);
813
814 return rc;
815}
816
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100817/*
818 * pn533_send_cmd_direct_async
819 *
820 * The function sends a piority cmd directly to the chip omiting the cmd
821 * queue. It's intended to be used by chaining mechanism of received responses
822 * where the host has to request every single chunk of data before scheduling
823 * next cmd from the queue.
824 */
825static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
826 struct sk_buff *req,
827 pn533_send_async_complete_t complete_cb,
828 void *complete_cb_context)
829{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100830 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200831 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100832 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100833 int resp_len = dev->ops->rx_header_len +
834 dev->ops->max_payload_len +
835 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100836
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100837 resp = alloc_skb(resp_len, GFP_KERNEL);
838 if (!resp)
839 return -ENOMEM;
840
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200841 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
842 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100843 dev_kfree_skb(resp);
844 return -ENOMEM;
845 }
846
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200847 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200848 cmd->req = req;
849 cmd->resp = resp;
850 cmd->resp_len = resp_len;
851 cmd->complete_cb = complete_cb;
852 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100853
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100854 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100855
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200856 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100857 if (rc < 0) {
858 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200859 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200860 } else {
861 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100862 }
863
864 return rc;
865}
866
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200867static void pn533_wq_cmd(struct work_struct *work)
868{
869 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
870 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200871 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200872
873 mutex_lock(&dev->cmd_lock);
874
875 if (list_empty(&dev->cmd_queue)) {
876 dev->cmd_pending = 0;
877 mutex_unlock(&dev->cmd_lock);
878 return;
879 }
880
881 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
882
Szymon Janc60ad07a2012-10-25 17:29:45 +0200883 list_del(&cmd->queue);
884
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200885 mutex_unlock(&dev->cmd_lock);
886
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200887 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200888 if (rc < 0) {
889 dev_kfree_skb(cmd->req);
890 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200891 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200892 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200893 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200894
895 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200896}
897
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300898struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100899 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300900 struct completion done;
901};
902
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100903static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
904 struct sk_buff *resp)
905{
906 struct pn533_sync_cmd_response *arg = _arg;
907
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100908 arg->resp = resp;
909 complete(&arg->done);
910
911 return 0;
912}
913
914/* pn533_send_cmd_sync
915 *
916 * Please note the req parameter is freed inside the function to
917 * limit a number of return value interpretations by the caller.
918 *
919 * 1. negative in case of error during TX path -> req should be freed
920 *
921 * 2. negative in case of error during RX path -> req should not be freed
922 * as it's been already freed at the begining of RX path by
923 * async_complete_cb.
924 *
925 * 3. valid pointer in case of succesfult RX path
926 *
927 * A caller has to check a return value with IS_ERR macro. If the test pass,
928 * the returned pointer is valid.
929 *
930 * */
931static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
932 struct sk_buff *req)
933{
934 int rc;
935 struct pn533_sync_cmd_response arg;
936
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100937 init_completion(&arg.done);
938
939 rc = pn533_send_cmd_async(dev, cmd_code, req,
940 pn533_send_sync_complete, &arg);
941 if (rc) {
942 dev_kfree_skb(req);
943 return ERR_PTR(rc);
944 }
945
946 wait_for_completion(&arg.done);
947
948 return arg.resp;
949}
950
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300951static void pn533_send_complete(struct urb *urb)
952{
953 struct pn533 *dev = urb->context;
954
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300955 switch (urb->status) {
956 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100957 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300958 case -ECONNRESET:
959 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100960 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100961 "The urb has been stopped (status %d)",
962 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300963 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100964 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300965 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100966 nfc_dev_err(&dev->interface->dev,
967 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300968 }
969}
970
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100971static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100972{
973 struct sk_buff *skb;
974
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100975 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100976 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100977 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100978
979 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100980 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100981
982 return skb;
983}
984
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300985struct pn533_target_type_a {
986 __be16 sens_res;
987 u8 sel_res;
988 u8 nfcid_len;
989 u8 nfcid_data[];
990} __packed;
991
992
993#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
994#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
995#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
996
997#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
998#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
999
1000#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1001#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1002
1003#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1004#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1005#define PN533_TYPE_A_SEL_PROT_DEP 2
1006#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1007
1008static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1009 int target_data_len)
1010{
1011 u8 ssd;
1012 u8 platconf;
1013
1014 if (target_data_len < sizeof(struct pn533_target_type_a))
1015 return false;
1016
1017 /* The lenght check of nfcid[] and ats[] are not being performed because
1018 the values are not being used */
1019
1020 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1021 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1022 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1023
1024 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001025 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1026 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1027 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001028 return false;
1029
1030 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1031 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1032 return false;
1033
1034 return true;
1035}
1036
1037static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1038 int tgt_data_len)
1039{
1040 struct pn533_target_type_a *tgt_type_a;
1041
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001042 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001043
1044 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1045 return -EPROTO;
1046
1047 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1048 case PN533_TYPE_A_SEL_PROT_MIFARE:
1049 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1050 break;
1051 case PN533_TYPE_A_SEL_PROT_ISO14443:
1052 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1053 break;
1054 case PN533_TYPE_A_SEL_PROT_DEP:
1055 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1056 break;
1057 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1058 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1059 NFC_PROTO_NFC_DEP_MASK;
1060 break;
1061 }
1062
1063 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1064 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001065 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1066 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001067
1068 return 0;
1069}
1070
1071struct pn533_target_felica {
1072 u8 pol_res;
1073 u8 opcode;
1074 u8 nfcid2[8];
1075 u8 pad[8];
1076 /* optional */
1077 u8 syst_code[];
1078} __packed;
1079
1080#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1081#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1082
1083static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1084 int target_data_len)
1085{
1086 if (target_data_len < sizeof(struct pn533_target_felica))
1087 return false;
1088
1089 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1090 return false;
1091
1092 return true;
1093}
1094
1095static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1096 int tgt_data_len)
1097{
1098 struct pn533_target_felica *tgt_felica;
1099
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001100 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001101
1102 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1103 return -EPROTO;
1104
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001105 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1106 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001107 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1108 else
1109 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1110
Samuel Ortiz79757542012-03-05 01:03:45 +01001111 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1112 nfc_tgt->sensf_res_len = 9;
1113
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001114 return 0;
1115}
1116
1117struct pn533_target_jewel {
1118 __be16 sens_res;
1119 u8 jewelid[4];
1120} __packed;
1121
1122static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1123 int target_data_len)
1124{
1125 u8 ssd;
1126 u8 platconf;
1127
1128 if (target_data_len < sizeof(struct pn533_target_jewel))
1129 return false;
1130
1131 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1132 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1133 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1134
1135 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001136 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1137 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1138 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001139 return false;
1140
1141 return true;
1142}
1143
1144static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1145 int tgt_data_len)
1146{
1147 struct pn533_target_jewel *tgt_jewel;
1148
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001149 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001150
1151 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1152 return -EPROTO;
1153
1154 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1155 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001156 nfc_tgt->nfcid1_len = 4;
1157 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001158
1159 return 0;
1160}
1161
1162struct pn533_type_b_prot_info {
1163 u8 bitrate;
1164 u8 fsci_type;
1165 u8 fwi_adc_fo;
1166} __packed;
1167
1168#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1169#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1170#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1171
1172struct pn533_type_b_sens_res {
1173 u8 opcode;
1174 u8 nfcid[4];
1175 u8 appdata[4];
1176 struct pn533_type_b_prot_info prot_info;
1177} __packed;
1178
1179#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1180
1181struct pn533_target_type_b {
1182 struct pn533_type_b_sens_res sensb_res;
1183 u8 attrib_res_len;
1184 u8 attrib_res[];
1185} __packed;
1186
1187static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1188 int target_data_len)
1189{
1190 if (target_data_len < sizeof(struct pn533_target_type_b))
1191 return false;
1192
1193 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1194 return false;
1195
1196 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1197 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1198 return false;
1199
1200 return true;
1201}
1202
1203static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1204 int tgt_data_len)
1205{
1206 struct pn533_target_type_b *tgt_type_b;
1207
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001208 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001209
1210 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1211 return -EPROTO;
1212
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001213 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001214
1215 return 0;
1216}
1217
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001218static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1219 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001220{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001221 struct nfc_target nfc_tgt;
1222 int rc;
1223
1224 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001225 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001226
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001227 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001228 return -EPROTO;
1229
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001230 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1231
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001232 switch (dev->poll_mod_curr) {
1233 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001234 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001235 break;
1236 case PN533_POLL_MOD_212KBPS_FELICA:
1237 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001238 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001239 break;
1240 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001241 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001242 break;
1243 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001244 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001245 break;
1246 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001247 nfc_dev_err(&dev->interface->dev,
1248 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001249 return -EPROTO;
1250 }
1251
1252 if (rc)
1253 return rc;
1254
1255 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001256 nfc_dev_dbg(&dev->interface->dev,
1257 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001258 return -EAGAIN;
1259 }
1260
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001261 nfc_dev_dbg(&dev->interface->dev,
1262 "Target found - supported protocols: 0x%x",
1263 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001264
1265 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1266
1267 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1268
1269 return 0;
1270}
1271
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001272static inline void pn533_poll_next_mod(struct pn533 *dev)
1273{
1274 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1275}
1276
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001277static void pn533_poll_reset_mod_list(struct pn533 *dev)
1278{
1279 dev->poll_mod_count = 0;
1280}
1281
1282static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1283{
1284 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001285 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001286 dev->poll_mod_count++;
1287}
1288
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001289static void pn533_poll_create_mod_list(struct pn533 *dev,
1290 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001291{
1292 pn533_poll_reset_mod_list(dev);
1293
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001294 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1295 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1296 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001297 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1298
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001299 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1300 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001301 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1302 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1303 }
1304
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001305 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1307
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001308 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001311 if (tm_protocols)
1312 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001313}
1314
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001315static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001316{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001317 u8 nbtg, tg, *tgdata;
1318 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001319
1320 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1321
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001322 nbtg = resp->data[0];
1323 tg = resp->data[1];
1324 tgdata = &resp->data[2];
1325 tgdata_len = resp->len - 2; /* nbtg + tg */
1326
1327 if (nbtg) {
1328 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001329
1330 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001331 if (rc == 0) {
1332 pn533_poll_reset_mod_list(dev);
1333 return 0;
1334 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001335 }
1336
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001337 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001338}
1339
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001340static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001341{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001342 struct sk_buff *skb;
1343 u8 *felica, *nfcid3, *gb;
1344
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001345 u8 *gbytes = dev->gb;
1346 size_t gbytes_len = dev->gb_len;
1347
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001348 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1349 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1350 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1351 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001352
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001353 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1354 0x0, 0x0, 0x0,
1355 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001356
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001357 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1358 felica (18), nfcid3 (10), gb_len (1) */
1359 gbytes_len +
1360 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001361
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001362 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001363 if (!skb)
1364 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001365
1366 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001367 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001368
1369 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001370 memcpy(skb_put(skb, 6), mifare_params, 6);
1371
1372 /* Felica params */
1373 felica = skb_put(skb, 18);
1374 memcpy(felica, felica_params, 18);
1375 get_random_bytes(felica + 2, 6);
1376
1377 /* NFCID3 */
1378 nfcid3 = skb_put(skb, 10);
1379 memset(nfcid3, 0, 10);
1380 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001381
1382 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001383 *skb_put(skb, 1) = gbytes_len;
1384
1385 gb = skb_put(skb, gbytes_len);
1386 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001387
Samuel Ortizad3823c2012-05-30 23:54:55 +02001388 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001389 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001390
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001391 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001392}
1393
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001394#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001395#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1396static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001397 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001398{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001399 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001400
1401 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1402
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001403 if (IS_ERR(resp))
1404 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001405
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001406 status = resp->data[0];
1407 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001408
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001410 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001411 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001412 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001413 return 0;
1414 }
1415
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001416 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001417}
1418
1419static void pn533_wq_tg_get_data(struct work_struct *work)
1420{
1421 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001422
1423 struct sk_buff *skb;
1424 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001425
1426 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1427
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001428 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001429 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001430 return;
1431
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001432 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1433 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001434
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001435 if (rc < 0)
1436 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001437
1438 return;
1439}
1440
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001441#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001442static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001443{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001444 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001445 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001446 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001447
1448 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1449
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001451 return -EINVAL;
1452
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001453 mode = resp->data[0];
1454 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001455
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1457 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001458
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001459 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1460 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001461 comm_mode = NFC_COMM_ACTIVE;
1462
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001463 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001464 return -EOPNOTSUPP;
1465
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001466 gb = cmd + ATR_REQ_GB_OFFSET;
1467 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001468
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001469 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1470 comm_mode, gb, gb_len);
1471 if (rc < 0) {
1472 nfc_dev_err(&dev->interface->dev,
1473 "Error when signaling target activation");
1474 return rc;
1475 }
1476
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001477 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001478 queue_work(dev->wq, &dev->tg_work);
1479
1480 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001481}
1482
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001483static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001484{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001485 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001486
1487 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1488
1489 /* An ack will cancel the last issued command (poll) */
1490 pn533_send_ack(dev, GFP_ATOMIC);
1491
1492 dev->cancel_listen = 1;
1493
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001494 pn533_poll_next_mod(dev);
1495
1496 queue_work(dev->wq, &dev->poll_work);
1497}
1498
1499static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001500 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001501{
1502 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001503 int rc;
1504
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001505 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1506
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001507 if (IS_ERR(resp)) {
1508 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001509
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001510 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1511 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001512
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001513 if (rc == -ENOENT) {
1514 if (dev->poll_mod_count != 0)
1515 return rc;
1516 else
1517 goto stop_poll;
1518 } else if (rc < 0) {
1519 nfc_dev_err(&dev->interface->dev,
1520 "Error %d when running poll", rc);
1521 goto stop_poll;
1522 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001523 }
1524
1525 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1526
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001527 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001529 rc = pn533_init_target_complete(dev, resp);
1530 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001531 }
1532
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001533 /* Initiator mode */
1534 rc = pn533_start_poll_complete(dev, resp);
1535 if (!rc)
1536 goto done;
1537
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001538 if (!dev->poll_mod_count) {
1539 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1540 goto done;
1541 }
1542
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001543 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001544 queue_work(dev->wq, &dev->poll_work);
1545
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001546done:
1547 dev_kfree_skb(resp);
1548 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001549
1550stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001551 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1552
Samuel Ortizad3823c2012-05-30 23:54:55 +02001553 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001555 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001556}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001557
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001558static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1559 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001561 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001562
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001563 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001564 if (!skb)
1565 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001566
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001567 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001569 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001570}
1571
1572static int pn533_send_poll_frame(struct pn533 *dev)
1573{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001574 struct pn533_poll_modulations *mod;
1575 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001576 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001577 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001578
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001579 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001580
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001581 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1582 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001583
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001584 if (mod->len == 0) { /* Listen mode */
1585 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001586 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001587 } else { /* Polling mode */
1588 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001589 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001590 }
1591
1592 if (!skb) {
1593 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1594 return -ENOMEM;
1595 }
1596
1597 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1598 NULL);
1599 if (rc < 0) {
1600 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001601 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001602 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001603
1604 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001605}
1606
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001607static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001608{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001609 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1610 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001611 int rc;
1612
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001613 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1614
1615 nfc_dev_dbg(&dev->interface->dev,
1616 "%s cancel_listen %d modulation len %d",
1617 __func__, dev->cancel_listen, cur_mod->len);
1618
1619 if (dev->cancel_listen == 1) {
1620 dev->cancel_listen = 0;
1621 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001622 }
1623
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001624 rc = pn533_send_poll_frame(dev);
1625 if (rc)
1626 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001627
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001628 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1629 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001630
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001631 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001632}
1633
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001634static int pn533_start_poll(struct nfc_dev *nfc_dev,
1635 u32 im_protocols, u32 tm_protocols)
1636{
1637 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1638
1639 nfc_dev_dbg(&dev->interface->dev,
1640 "%s: im protocols 0x%x tm protocols 0x%x",
1641 __func__, im_protocols, tm_protocols);
1642
1643 if (dev->tgt_active_prot) {
1644 nfc_dev_err(&dev->interface->dev,
1645 "Cannot poll with a target already activated");
1646 return -EBUSY;
1647 }
1648
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001649 if (dev->tgt_mode) {
1650 nfc_dev_err(&dev->interface->dev,
1651 "Cannot poll while already being activated");
1652 return -EBUSY;
1653 }
1654
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001655 if (tm_protocols) {
1656 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1657 if (dev->gb == NULL)
1658 tm_protocols = 0;
1659 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001660
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001661 dev->poll_mod_curr = 0;
1662 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1663 dev->poll_protocols = im_protocols;
1664 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001665
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001666 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001667}
1668
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001669static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1670{
1671 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1672
1673 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1674
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001675 del_timer(&dev->listen_timer);
1676
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001677 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001678 nfc_dev_dbg(&dev->interface->dev,
1679 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001680 return;
1681 }
1682
1683 /* An ack will cancel the last issued command (poll) */
1684 pn533_send_ack(dev, GFP_KERNEL);
1685
1686 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1687 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001688
1689 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001690}
1691
1692static int pn533_activate_target_nfcdep(struct pn533 *dev)
1693{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001694 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001695 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001696 int rc;
1697
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001698 struct sk_buff *skb;
1699 struct sk_buff *resp;
1700
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001701 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1702
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001703 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001704 if (!skb)
1705 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001706
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001707 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1708 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001709
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001710 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1711 if (IS_ERR(resp))
1712 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001713
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001714 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001715 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001716 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001717 nfc_dev_err(&dev->interface->dev,
1718 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001719 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001721 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001722
Samuel Ortiz541d9202011-12-14 16:43:10 +01001723 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001724 gt_len = resp->len - 16;
1725 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001726
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001727 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001728 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001729}
1730
Eric Lapuyade90099432012-05-07 12:31:13 +02001731static int pn533_activate_target(struct nfc_dev *nfc_dev,
1732 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001733{
1734 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1735 int rc;
1736
1737 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001738 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001739
1740 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001741 nfc_dev_err(&dev->interface->dev,
1742 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001743 return -EBUSY;
1744 }
1745
1746 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001747 nfc_dev_err(&dev->interface->dev,
1748 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001749 return -EBUSY;
1750 }
1751
1752 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001753 nfc_dev_err(&dev->interface->dev,
1754 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001755 return -EINVAL;
1756 }
1757
1758 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001759 nfc_dev_err(&dev->interface->dev,
1760 "Target doesn't support requested proto %u",
1761 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001762 return -EINVAL;
1763 }
1764
1765 if (protocol == NFC_PROTO_NFC_DEP) {
1766 rc = pn533_activate_target_nfcdep(dev);
1767 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001768 nfc_dev_err(&dev->interface->dev,
1769 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001770 return rc;
1771 }
1772 }
1773
1774 dev->tgt_active_prot = protocol;
1775 dev->tgt_available_prots = 0;
1776
1777 return 0;
1778}
1779
Eric Lapuyade90099432012-05-07 12:31:13 +02001780static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1781 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001782{
1783 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001784
1785 struct sk_buff *skb;
1786 struct sk_buff *resp;
1787
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001788 int rc;
1789
1790 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1791
1792 if (!dev->tgt_active_prot) {
1793 nfc_dev_err(&dev->interface->dev, "There is no active target");
1794 return;
1795 }
1796
1797 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001798 skb_queue_purge(&dev->resp_q);
1799
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001800 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001801 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001802 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001803
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001804 *skb_put(skb, 1) = 1; /* TG*/
1805
1806 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1807 if (IS_ERR(resp))
1808 return;
1809
1810 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001811 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001812 nfc_dev_err(&dev->interface->dev,
1813 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001814
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001815 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001816 return;
1817}
1818
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001819
1820static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001821 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001822{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001823 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001824 u8 target_gt_len;
1825 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001826 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001827
1828 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001829
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001830 if (IS_ERR(resp))
1831 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001832
1833 if (dev->tgt_available_prots &&
1834 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1835 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001836 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001837 rc = -EINVAL;
1838 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001839 }
1840
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1842
1843 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001844 if (rc != PN533_CMD_RET_SUCCESS) {
1845 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001846 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001847 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001848 }
1849
1850 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001851 struct nfc_target nfc_target;
1852
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001853 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1854
1855 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001856 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001857 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001858 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1859 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001860 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001861
1862 dev->tgt_available_prots = 0;
1863 }
1864
1865 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1866
1867 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001868 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001870 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001871 if (rc == 0)
1872 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001873 dev->nfc_dev->targets[0].idx,
1874 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001875
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001876error:
1877 dev_kfree_skb(resp);
1878 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879}
1880
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001881static int pn533_mod_to_baud(struct pn533 *dev)
1882{
1883 switch (dev->poll_mod_curr) {
1884 case PN533_POLL_MOD_106KBPS_A:
1885 return 0;
1886 case PN533_POLL_MOD_212KBPS_FELICA:
1887 return 1;
1888 case PN533_POLL_MOD_424KBPS_FELICA:
1889 return 2;
1890 default:
1891 return -EINVAL;
1892 }
1893}
1894
Samuel Ortizd7f33452012-05-29 21:45:21 +02001895#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001896static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001897 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001898{
1899 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001900 struct sk_buff *skb;
1901 int rc, baud, skb_len;
1902 u8 *next, *arg;
1903
Samuel Ortizd7f33452012-05-29 21:45:21 +02001904 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001905
1906 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1907
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001908 if (dev->poll_mod_count) {
1909 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001910 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001911 return -EBUSY;
1912 }
1913
1914 if (dev->tgt_active_prot) {
1915 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001916 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001917 return -EBUSY;
1918 }
1919
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001920 baud = pn533_mod_to_baud(dev);
1921 if (baud < 0) {
1922 nfc_dev_err(&dev->interface->dev,
1923 "Invalid curr modulation %d", dev->poll_mod_curr);
1924 return baud;
1925 }
1926
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001927 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001928 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001929 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001930
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001931 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001932 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001933 return -ENOMEM;
1934
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001935 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1936 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001937
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001938 next = skb_put(skb, 1); /* Next */
1939 *next = 0;
1940
1941 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1942 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1943 PASSIVE_DATA_LEN);
1944 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001945 }
1946
Samuel Ortiz47807d32012-03-05 01:03:50 +01001947 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001948 memcpy(skb_put(skb, gb_len), gb, gb_len);
1949 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001950 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001951 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001952 }
1953
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001954 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1955 if (!arg) {
1956 dev_kfree_skb(skb);
1957 return -ENOMEM;
1958 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001959
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001960 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001961
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001962 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1963 pn533_in_dep_link_up_complete, arg);
1964
1965 if (rc < 0) {
1966 dev_kfree_skb(skb);
1967 kfree(arg);
1968 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001969
1970 return rc;
1971}
1972
1973static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1974{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001975 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1976
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001977 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1978
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001979 pn533_poll_reset_mod_list(dev);
1980
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001981 if (dev->tgt_mode || dev->tgt_active_prot) {
1982 pn533_send_ack(dev, GFP_KERNEL);
1983 usb_kill_urb(dev->in_urb);
1984 }
1985
1986 dev->tgt_active_prot = 0;
1987 dev->tgt_mode = 0;
1988
1989 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001990
1991 return 0;
1992}
1993
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001994struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001995 data_exchange_cb_t cb;
1996 void *cb_context;
1997};
1998
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001999static struct sk_buff *pn533_build_response(struct pn533 *dev)
2000{
2001 struct sk_buff *skb, *tmp, *t;
2002 unsigned int skb_len = 0, tmp_len = 0;
2003
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002004 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002005
2006 if (skb_queue_empty(&dev->resp_q))
2007 return NULL;
2008
2009 if (skb_queue_len(&dev->resp_q) == 1) {
2010 skb = skb_dequeue(&dev->resp_q);
2011 goto out;
2012 }
2013
2014 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2015 skb_len += tmp->len;
2016
2017 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2018 __func__, skb_len);
2019
2020 skb = alloc_skb(skb_len, GFP_KERNEL);
2021 if (skb == NULL)
2022 goto out;
2023
2024 skb_put(skb, skb_len);
2025
2026 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2027 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2028 tmp_len += tmp->len;
2029 }
2030
2031out:
2032 skb_queue_purge(&dev->resp_q);
2033
2034 return skb;
2035}
2036
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002037static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002038 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002039{
2040 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002041 struct sk_buff *skb;
2042 int rc = 0;
2043 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002044
2045 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2046
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002047 if (IS_ERR(resp)) {
2048 rc = PTR_ERR(resp);
2049 goto _error;
2050 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002051
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002052 status = resp->data[0];
2053 ret = status & PN533_CMD_RET_MASK;
2054 mi = status & PN533_CMD_MI_MASK;
2055
2056 skb_pull(resp, sizeof(status));
2057
2058 if (ret != PN533_CMD_RET_SUCCESS) {
2059 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002060 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002061 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002062 goto error;
2063 }
2064
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002065 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002066
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002067 if (mi) {
2068 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002069 queue_work(dev->wq, &dev->mi_work);
2070 return -EINPROGRESS;
2071 }
2072
2073 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002074 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002075 goto error;
2076
2077 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002078 kfree(arg);
2079 return 0;
2080
2081error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002082 dev_kfree_skb(resp);
2083_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002084 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002085 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002086 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002087 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002088}
2089
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002090static int pn533_transceive(struct nfc_dev *nfc_dev,
2091 struct nfc_target *target, struct sk_buff *skb,
2092 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002093{
2094 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002095 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002096 int rc;
2097
2098 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2099
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002100 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2101 /* TODO: Implement support to multi-part data exchange */
2102 nfc_dev_err(&dev->interface->dev,
2103 "Data length greater than the max allowed: %d",
2104 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2105 rc = -ENOSYS;
2106 goto error;
2107 }
2108
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002109 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002110 nfc_dev_err(&dev->interface->dev,
2111 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002112 rc = -EINVAL;
2113 goto error;
2114 }
2115
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002116 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002117 if (!arg) {
2118 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002119 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 }
2121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002122 arg->cb = cb;
2123 arg->cb_context = cb_context;
2124
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002125 switch (dev->device_type) {
2126 case PN533_DEVICE_PASORI:
2127 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2128 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2129 skb,
2130 pn533_data_exchange_complete,
2131 arg);
2132
2133 break;
2134 }
2135 default:
2136 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2137
2138 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2139 skb, pn533_data_exchange_complete,
2140 arg);
2141
2142 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002143 }
2144
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002145 if (rc < 0) /* rc from send_async */
2146 goto error;
2147
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002148 return 0;
2149
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002150error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002151 kfree(arg);
2152 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002153 return rc;
2154}
2155
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002156static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002157 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002158{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002159 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002160
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002161 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2162
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002163 if (IS_ERR(resp))
2164 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002165
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002166 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002167
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002168 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002169
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002170 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002171 nfc_tm_deactivated(dev->nfc_dev);
2172
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002173 dev->tgt_mode = 0;
2174
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175 return 0;
2176 }
2177
2178 queue_work(dev->wq, &dev->tg_work);
2179
2180 return 0;
2181}
2182
2183static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2184{
2185 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002186 int rc;
2187
2188 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2189
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002190 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002191 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002192 "Data length greater than the max allowed: %d",
2193 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2194 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002195 }
2196
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002197 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2198 pn533_tm_send_complete, NULL);
2199 if (rc < 0)
2200 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002201
2202 return rc;
2203}
2204
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002205static void pn533_wq_mi_recv(struct work_struct *work)
2206{
2207 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002208
2209 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002210 int rc;
2211
2212 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2213
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002214 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002215 if (!skb)
2216 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002217
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002218 switch (dev->device_type) {
2219 case PN533_DEVICE_PASORI:
2220 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2221 rc = pn533_send_cmd_direct_async(dev,
2222 PN533_CMD_IN_COMM_THRU,
2223 skb,
2224 pn533_data_exchange_complete,
2225 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002226
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002227 break;
2228 }
2229 default:
2230 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002231
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002232 rc = pn533_send_cmd_direct_async(dev,
2233 PN533_CMD_IN_DATA_EXCHANGE,
2234 skb,
2235 pn533_data_exchange_complete,
2236 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002237
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002238 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002239 }
2240
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002241 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002242 return;
2243
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002244 nfc_dev_err(&dev->interface->dev,
2245 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002246
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002247 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002248 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002249
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002250error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002251 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002252 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002253}
2254
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002255static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2256 u8 cfgdata_len)
2257{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002258 struct sk_buff *skb;
2259 struct sk_buff *resp;
2260
2261 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002262
2263 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2264
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002265 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002266
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002267 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002268 if (!skb)
2269 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002270
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002271 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2272 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002273
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002274 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2275 if (IS_ERR(resp))
2276 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002277
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002278 dev_kfree_skb(resp);
2279 return 0;
2280}
2281
2282static int pn533_get_firmware_version(struct pn533 *dev,
2283 struct pn533_fw_version *fv)
2284{
2285 struct sk_buff *skb;
2286 struct sk_buff *resp;
2287
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002288 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002289 if (!skb)
2290 return -ENOMEM;
2291
2292 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2293 if (IS_ERR(resp))
2294 return PTR_ERR(resp);
2295
2296 fv->ic = resp->data[0];
2297 fv->ver = resp->data[1];
2298 fv->rev = resp->data[2];
2299 fv->support = resp->data[3];
2300
2301 dev_kfree_skb(resp);
2302 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002303}
2304
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002305static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002306{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002307 struct sk_buff *skb;
2308 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002309
2310 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2311
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002312 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002313 if (!skb)
2314 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002315
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002316 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002317
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002318 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2319 if (IS_ERR(resp))
2320 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002321
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002322 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002323
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002324 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002325}
2326
2327static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002328 .dev_up = NULL,
2329 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002330 .dep_link_up = pn533_dep_link_up,
2331 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002332 .start_poll = pn533_start_poll,
2333 .stop_poll = pn533_stop_poll,
2334 .activate_target = pn533_activate_target,
2335 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002336 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002337 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002338};
2339
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002340static int pn533_setup(struct pn533 *dev)
2341{
2342 struct pn533_config_max_retries max_retries;
2343 struct pn533_config_timing timing;
2344 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2345 int rc;
2346
2347 switch (dev->device_type) {
2348 case PN533_DEVICE_STD:
2349 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2350 max_retries.mx_rty_psl = 2;
2351 max_retries.mx_rty_passive_act =
2352 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2353
2354 timing.rfu = PN533_CONFIG_TIMING_102;
2355 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2356 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2357
2358 break;
2359
2360 case PN533_DEVICE_PASORI:
2361 max_retries.mx_rty_atr = 0x2;
2362 max_retries.mx_rty_psl = 0x1;
2363 max_retries.mx_rty_passive_act =
2364 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2365
2366 timing.rfu = PN533_CONFIG_TIMING_102;
2367 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2368 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2369
2370 break;
2371
2372 default:
2373 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2374 dev->device_type);
2375 return -EINVAL;
2376 }
2377
2378 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2379 (u8 *)&max_retries, sizeof(max_retries));
2380 if (rc) {
2381 nfc_dev_err(&dev->interface->dev,
2382 "Error on setting MAX_RETRIES config");
2383 return rc;
2384 }
2385
2386
2387 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2388 (u8 *)&timing, sizeof(timing));
2389 if (rc) {
2390 nfc_dev_err(&dev->interface->dev,
2391 "Error on setting RF timings");
2392 return rc;
2393 }
2394
2395 switch (dev->device_type) {
2396 case PN533_DEVICE_STD:
2397 break;
2398
2399 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002400 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002401
2402 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2403 pasori_cfg, 3);
2404 if (rc) {
2405 nfc_dev_err(&dev->interface->dev,
2406 "Error while settings PASORI config");
2407 return rc;
2408 }
2409
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002410 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002411
2412 break;
2413 }
2414
2415 return 0;
2416}
2417
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002418static int pn533_probe(struct usb_interface *interface,
2419 const struct usb_device_id *id)
2420{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002421 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002422 struct pn533 *dev;
2423 struct usb_host_interface *iface_desc;
2424 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002425 int in_endpoint = 0;
2426 int out_endpoint = 0;
2427 int rc = -ENOMEM;
2428 int i;
2429 u32 protocols;
2430
2431 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2432 if (!dev)
2433 return -ENOMEM;
2434
2435 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2436 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002437 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002438
2439 iface_desc = interface->cur_altsetting;
2440 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2441 endpoint = &iface_desc->endpoint[i].desc;
2442
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002443 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002444 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002446 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002447 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002448 }
2449
2450 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002451 nfc_dev_err(&interface->dev,
2452 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002453 rc = -ENODEV;
2454 goto error;
2455 }
2456
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002457 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2459
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002460 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002461 goto error;
2462
2463 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002464 usb_rcvbulkpipe(dev->udev, in_endpoint),
2465 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002467 usb_sndbulkpipe(dev->udev, out_endpoint),
2468 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002469
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002470 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2471 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002472 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002473 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002474 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002475 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002476 if (dev->wq == NULL)
2477 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002478
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002479 init_timer(&dev->listen_timer);
2480 dev->listen_timer.data = (unsigned long) dev;
2481 dev->listen_timer.function = pn533_listen_mode_timer;
2482
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002483 skb_queue_head_init(&dev->resp_q);
2484
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002485 INIT_LIST_HEAD(&dev->cmd_queue);
2486
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487 usb_set_intfdata(interface, dev);
2488
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002489 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002490
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002491 dev->device_type = id->driver_info;
2492 switch (dev->device_type) {
2493 case PN533_DEVICE_STD:
2494 protocols = PN533_ALL_PROTOCOLS;
2495 break;
2496
2497 case PN533_DEVICE_PASORI:
2498 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2499 break;
2500
2501 default:
2502 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2503 dev->device_type);
2504 rc = -EINVAL;
2505 goto destroy_wq;
2506 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002507
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002508 memset(&fw_ver, 0, sizeof(fw_ver));
2509 rc = pn533_get_firmware_version(dev, &fw_ver);
2510 if (rc < 0)
2511 goto destroy_wq;
2512
2513 nfc_dev_info(&dev->interface->dev,
2514 "NXP PN533 firmware ver %d.%d now attached",
2515 fw_ver.ver, fw_ver.rev);
2516
2517
Samuel Ortize8753042011-08-19 15:47:11 +02002518 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002519 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002520 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002521 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002522 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002523 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002524 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002525
2526 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2527 nfc_set_drvdata(dev->nfc_dev, dev);
2528
2529 rc = nfc_register_device(dev->nfc_dev);
2530 if (rc)
2531 goto free_nfc_dev;
2532
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002533 rc = pn533_setup(dev);
2534 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002535 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002536
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002537 return 0;
2538
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002539unregister_nfc_dev:
2540 nfc_unregister_device(dev->nfc_dev);
2541
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002542free_nfc_dev:
2543 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002544
Samuel Ortiz4849f852012-04-10 19:43:17 +02002545destroy_wq:
2546 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002547error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002548 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002549 usb_free_urb(dev->out_urb);
2550 kfree(dev);
2551 return rc;
2552}
2553
2554static void pn533_disconnect(struct usb_interface *interface)
2555{
2556 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002557 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002558
2559 dev = usb_get_intfdata(interface);
2560 usb_set_intfdata(interface, NULL);
2561
2562 nfc_unregister_device(dev->nfc_dev);
2563 nfc_free_device(dev->nfc_dev);
2564
2565 usb_kill_urb(dev->in_urb);
2566 usb_kill_urb(dev->out_urb);
2567
Samuel Ortiz4849f852012-04-10 19:43:17 +02002568 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002569
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002570 skb_queue_purge(&dev->resp_q);
2571
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002572 del_timer(&dev->listen_timer);
2573
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002574 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2575 list_del(&cmd->queue);
2576 kfree(cmd);
2577 }
2578
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002579 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002580 usb_free_urb(dev->out_urb);
2581 kfree(dev);
2582
Dan Carpenter276556d2011-07-08 10:21:15 +03002583 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002584}
2585
2586static struct usb_driver pn533_driver = {
2587 .name = "pn533",
2588 .probe = pn533_probe,
2589 .disconnect = pn533_disconnect,
2590 .id_table = pn533_table,
2591};
2592
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002593module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002595MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2596MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2597MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002598MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2599MODULE_VERSION(VERSION);
2600MODULE_LICENSE("GPL");