blob: 80a6e7ceb1612141c636633d0bd638fbec7836f8 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02003 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/usb.h>
26#include <linux/nfc.h>
27#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030028#include <net/nfc/nfc.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
30#define VERSION "0.1"
31
32#define PN533_VENDOR_ID 0x4CC
33#define PN533_PRODUCT_ID 0x2533
34
35#define SCM_VENDOR_ID 0x4E6
36#define SCL3711_PRODUCT_ID 0x5591
37
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020038#define SONY_VENDOR_ID 0x054c
39#define PASORI_PRODUCT_ID 0x02e1
40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020041#define PN533_DEVICE_STD 0x1
42#define PN533_DEVICE_PASORI 0x2
43
Samuel Ortiz01d719a2012-07-04 00:14:04 +020044#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
45 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
46 NFC_PROTO_NFC_DEP_MASK |\
47 NFC_PROTO_ISO14443_B_MASK)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020048
49#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
50 NFC_PROTO_MIFARE_MASK | \
51 NFC_PROTO_FELICA_MASK | \
Samuel Ortiz01d719a2012-07-04 00:14:04 +020052 NFC_PROTO_ISO14443_MASK | \
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020053 NFC_PROTO_NFC_DEP_MASK)
54
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030055static const struct usb_device_id pn533_table[] = {
Samuel Ortiz5c7b0532012-07-02 20:04:01 +020056 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
57 .idVendor = PN533_VENDOR_ID,
58 .idProduct = PN533_PRODUCT_ID,
59 .driver_info = PN533_DEVICE_STD,
60 },
61 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
62 .idVendor = SCM_VENDOR_ID,
63 .idProduct = SCL3711_PRODUCT_ID,
64 .driver_info = PN533_DEVICE_STD,
65 },
66 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
67 .idVendor = SONY_VENDOR_ID,
68 .idProduct = PASORI_PRODUCT_ID,
69 .driver_info = PN533_DEVICE_PASORI,
70 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030071 { }
72};
73MODULE_DEVICE_TABLE(usb, pn533_table);
74
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020075/* How much time we spend listening for initiators */
76#define PN533_LISTEN_TIME 2
77
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020078/* Standard pn533 frame definitions */
79#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +010080 + 2) /* data[0] TFI, data[1] CC */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020081#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
Waldemar Rymarkiewicz82dec342012-10-11 14:03:58 +020082
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010083/*
84 * Max extended frame payload len, excluding TFI and CC
85 * which are already in PN533_FRAME_HEADER_LEN.
86 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020087#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +010088
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020089#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +010090 Postamble (1) */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020091#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
92#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030093
94/* start of frame */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020095#define PN533_STD_FRAME_SOF 0x00FF
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030096
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +020097/* standard frame identifier: in/out/error */
98#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
99#define PN533_STD_FRAME_DIR_OUT 0xD4
100#define PN533_STD_FRAME_DIR_IN 0xD5
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300101
102/* PN533 Commands */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200103#define PN533_STD_FRAME_CMD(f) (f->data[1])
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104
105#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
106#define PN533_CMD_RF_CONFIGURATION 0x32
107#define PN533_CMD_IN_DATA_EXCHANGE 0x40
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200108#define PN533_CMD_IN_COMM_THRU 0x42
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300109#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
110#define PN533_CMD_IN_ATR 0x50
111#define PN533_CMD_IN_RELEASE 0x52
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100112#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300113
Samuel Ortizad3823c2012-05-30 23:54:55 +0200114#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200115#define PN533_CMD_TG_GET_DATA 0x86
Samuel Ortizdadb06f2012-05-31 00:09:11 +0200116#define PN533_CMD_TG_SET_DATA 0x8e
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100117#define PN533_CMD_UNDEF 0xff
Samuel Ortizad3823c2012-05-30 23:54:55 +0200118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300119#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
120
121/* PN533 Return codes */
122#define PN533_CMD_RET_MASK 0x3F
123#define PN533_CMD_MI_MASK 0x40
124#define PN533_CMD_RET_SUCCESS 0x00
125
126struct pn533;
127
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100128typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300129
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100130typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
131 struct sk_buff *resp);
132
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133/* structs for pn533 commands */
134
135/* PN533_CMD_GET_FIRMWARE_VERSION */
136struct pn533_fw_version {
137 u8 ic;
138 u8 ver;
139 u8 rev;
140 u8 support;
141};
142
143/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200144#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200146#define PN533_CFGITEM_PASORI 0x82
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300147
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200148#define PN533_CONFIG_TIMING_102 0xb
149#define PN533_CONFIG_TIMING_204 0xc
150#define PN533_CONFIG_TIMING_409 0xd
151#define PN533_CONFIG_TIMING_819 0xe
152
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300153#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
154#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
155
156struct pn533_config_max_retries {
157 u8 mx_rty_atr;
158 u8 mx_rty_psl;
159 u8 mx_rty_passive_act;
160} __packed;
161
Samuel Ortiz34a85bf2012-05-29 21:34:08 +0200162struct pn533_config_timing {
163 u8 rfu;
164 u8 atr_res_timeout;
165 u8 dep_timeout;
166} __packed;
167
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300168/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
169
170/* felica commands opcode */
171#define PN533_FELICA_OPC_SENSF_REQ 0
172#define PN533_FELICA_OPC_SENSF_RES 1
173/* felica SENSF_REQ parameters */
174#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
175#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
176#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
177#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
178
179/* type B initiator_data values */
180#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
181#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
182#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
183
184union pn533_cmd_poll_initdata {
185 struct {
186 u8 afi;
187 u8 polling_method;
188 } __packed type_b;
189 struct {
190 u8 opcode;
191 __be16 sc;
192 u8 rc;
193 u8 tsn;
194 } __packed felica;
195};
196
197/* Poll modulations */
198enum {
199 PN533_POLL_MOD_106KBPS_A,
200 PN533_POLL_MOD_212KBPS_FELICA,
201 PN533_POLL_MOD_424KBPS_FELICA,
202 PN533_POLL_MOD_106KBPS_JEWEL,
203 PN533_POLL_MOD_847KBPS_B,
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200204 PN533_LISTEN_MOD,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205
206 __PN533_POLL_MOD_AFTER_LAST,
207};
208#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
209
210struct pn533_poll_modulations {
211 struct {
212 u8 maxtg;
213 u8 brty;
214 union pn533_cmd_poll_initdata initiator_data;
215 } __packed data;
216 u8 len;
217};
218
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100219static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300220 [PN533_POLL_MOD_106KBPS_A] = {
221 .data = {
222 .maxtg = 1,
223 .brty = 0,
224 },
225 .len = 2,
226 },
227 [PN533_POLL_MOD_212KBPS_FELICA] = {
228 .data = {
229 .maxtg = 1,
230 .brty = 1,
231 .initiator_data.felica = {
232 .opcode = PN533_FELICA_OPC_SENSF_REQ,
233 .sc = PN533_FELICA_SENSF_SC_ALL,
234 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
235 .tsn = 0,
236 },
237 },
238 .len = 7,
239 },
240 [PN533_POLL_MOD_424KBPS_FELICA] = {
241 .data = {
242 .maxtg = 1,
243 .brty = 2,
244 .initiator_data.felica = {
245 .opcode = PN533_FELICA_OPC_SENSF_REQ,
246 .sc = PN533_FELICA_SENSF_SC_ALL,
247 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
248 .tsn = 0,
249 },
250 },
251 .len = 7,
252 },
253 [PN533_POLL_MOD_106KBPS_JEWEL] = {
254 .data = {
255 .maxtg = 1,
256 .brty = 4,
257 },
258 .len = 2,
259 },
260 [PN533_POLL_MOD_847KBPS_B] = {
261 .data = {
262 .maxtg = 1,
263 .brty = 8,
264 .initiator_data.type_b = {
265 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
266 .polling_method =
267 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
268 },
269 },
270 .len = 3,
271 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200272 [PN533_LISTEN_MOD] = {
273 .len = 0,
274 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300275};
276
277/* PN533_CMD_IN_ATR */
278
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300279struct pn533_cmd_activate_response {
280 u8 status;
281 u8 nfcid3t[10];
282 u8 didt;
283 u8 bst;
284 u8 brt;
285 u8 to;
286 u8 ppt;
287 /* optional */
288 u8 gt[];
289} __packed;
290
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100291struct pn533_cmd_jump_dep_response {
292 u8 status;
293 u8 tg;
294 u8 nfcid3t[10];
295 u8 didt;
296 u8 bst;
297 u8 brt;
298 u8 to;
299 u8 ppt;
300 /* optional */
301 u8 gt[];
302} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303
Samuel Ortizad3823c2012-05-30 23:54:55 +0200304
305/* PN533_TG_INIT_AS_TARGET */
306#define PN533_INIT_TARGET_PASSIVE 0x1
307#define PN533_INIT_TARGET_DEP 0x2
308
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200309#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
310#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
311#define PN533_INIT_TARGET_RESP_DEP 0x4
312
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300313struct pn533 {
314 struct usb_device *udev;
315 struct usb_interface *interface;
316 struct nfc_dev *nfc_dev;
317
318 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300319 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200321 struct sk_buff_head resp_q;
322
Samuel Ortiz4849f852012-04-10 19:43:17 +0200323 struct workqueue_struct *wq;
324 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200325 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200326 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200327 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200328 struct work_struct tg_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200329 struct timer_list listen_timer;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200330 int wq_in_error;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200331 int cancel_listen;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300332
333 pn533_cmd_complete_t cmd_complete;
334 void *cmd_complete_arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100335 void *cmd_complete_mi_arg;
Samuel Ortiz0201ed02012-05-31 17:56:46 +0200336 struct mutex cmd_lock;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200337 struct pn533_cmd *cmd;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300338
339 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
340 u8 poll_mod_count;
341 u8 poll_mod_curr;
342 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200343 u32 listen_protocols;
344
345 u8 *gb;
346 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300347
348 u8 tgt_available_prots;
349 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200350 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200351
352 u32 device_type;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200353
354 struct list_head cmd_queue;
355 u8 cmd_pending;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100356
357 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200358};
359
360struct pn533_cmd {
361 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200362 u8 code;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100363 struct sk_buff *req;
364 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100365 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200366 pn533_send_async_complete_t complete_cb;
367 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300368};
369
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200370struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300371 u8 preamble;
372 __be16 start_frame;
373 u8 datalen;
374 u8 datalen_checksum;
375 u8 data[];
376} __packed;
377
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100378struct pn533_frame_ops {
379 void (*tx_frame_init)(void *frame, u8 cmd_code);
380 void (*tx_frame_finish)(void *frame);
381 void (*tx_update_payload_len)(void *frame, int len);
382 int tx_header_len;
383 int tx_tail_len;
384
385 bool (*rx_is_frame_valid)(void *frame);
386 int (*rx_frame_size)(void *frame);
387 int rx_header_len;
388 int rx_tail_len;
389
390 int max_payload_len;
391 u8 (*get_cmd_code)(void *frame);
392};
393
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300394/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200395static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300396{
397 return ~value + 1;
398}
399
400/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200401static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300402{
403 u8 sum = 0;
404 int i;
405
406 for (i = 0; i < datalen; i++)
407 sum += data[i];
408
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200409 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300410}
411
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200412static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300413{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200414 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100415
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300416 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200417 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
418 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
419 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300420 frame->datalen = 2;
421}
422
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200423static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300424{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200425 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100426
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200427 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300428
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200429 PN533_STD_FRAME_CHECKSUM(frame) =
430 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300431
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200432 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300433}
434
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200435static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100436{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200437 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100438
439 frame->datalen += len;
440}
441
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200442static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300443{
444 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200445 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300446
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200447 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448 return false;
449
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200450 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300451 if (checksum != frame->datalen_checksum)
452 return false;
453
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200454 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
455 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300456 return false;
457
458 return true;
459}
460
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200461static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300462{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200463 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300464 return false;
465
466 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
467 return false;
468
469 return true;
470}
471
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200472static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300473{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200474 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100475
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200476 return sizeof(struct pn533_std_frame) + f->datalen +
477 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100478}
479
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200480static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100481{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200482 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100483
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200484 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100485}
486
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100487static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200488 .tx_frame_init = pn533_std_tx_frame_init,
489 .tx_frame_finish = pn533_std_tx_frame_finish,
490 .tx_update_payload_len = pn533_std_tx_update_payload_len,
491 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
492 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100493
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200494 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
495 .rx_frame_size = pn533_std_rx_frame_size,
496 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
497 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100498
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200499 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
500 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100501};
502
503static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
504{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200505 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200506 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300507}
508
Samuel Ortiz4849f852012-04-10 19:43:17 +0200509
510static void pn533_wq_cmd_complete(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511{
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200512 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300513 int rc;
514
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100515 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300516 if (rc != -EINPROGRESS)
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200517 queue_work(dev->wq, &dev->cmd_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300518}
519
520static void pn533_recv_response(struct urb *urb)
521{
522 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100523 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300525 switch (urb->status) {
526 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100527 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528 case -ECONNRESET:
529 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100530 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100531 "The urb has been canceled (status %d)",
532 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200533 dev->wq_in_error = urb->status;
534 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100535 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300536 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100537 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100538 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200539 dev->wq_in_error = urb->status;
540 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300541 }
542
543 in_frame = dev->in_urb->transfer_buffer;
544
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100545 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100546 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
547 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100548
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100549 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300550 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200551 dev->wq_in_error = -EIO;
552 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300553 }
554
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100555 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100556 nfc_dev_err(&dev->interface->dev,
557 "It it not the response to the last command");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200558 dev->wq_in_error = -EIO;
559 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300560 }
561
Samuel Ortiz4849f852012-04-10 19:43:17 +0200562 dev->wq_in_error = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563
Samuel Ortiz4849f852012-04-10 19:43:17 +0200564sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200565 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300566}
567
568static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
569{
570 dev->in_urb->complete = pn533_recv_response;
571
572 return usb_submit_urb(dev->in_urb, flags);
573}
574
575static void pn533_recv_ack(struct urb *urb)
576{
577 struct pn533 *dev = urb->context;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200578 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300579 int rc;
580
581 switch (urb->status) {
582 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100583 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300584 case -ECONNRESET:
585 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100586 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100587 "The urb has been stopped (status %d)",
588 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200589 dev->wq_in_error = urb->status;
590 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100591 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300592 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100593 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100594 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200595 dev->wq_in_error = urb->status;
596 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300597 }
598
599 in_frame = dev->in_urb->transfer_buffer;
600
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200601 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300602 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Samuel Ortiz4849f852012-04-10 19:43:17 +0200603 dev->wq_in_error = -EIO;
604 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300605 }
606
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300607 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
608 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100609 nfc_dev_err(&dev->interface->dev,
610 "usb_submit_urb failed with result %d", rc);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200611 dev->wq_in_error = rc;
612 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300613 }
614
615 return;
616
Samuel Ortiz4849f852012-04-10 19:43:17 +0200617sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200618 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300619}
620
621static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
622{
623 dev->in_urb->complete = pn533_recv_ack;
624
625 return usb_submit_urb(dev->in_urb, flags);
626}
627
628static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
629{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200630 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100631 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300632 int rc;
633
634 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
635
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100636 dev->out_urb->transfer_buffer = ack;
637 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300638 rc = usb_submit_urb(dev->out_urb, flags);
639
640 return rc;
641}
642
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100643static int __pn533_send_frame_async(struct pn533 *dev,
644 struct sk_buff *out,
645 struct sk_buff *in,
646 int in_len,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300647 pn533_cmd_complete_t cmd_complete,
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100648 void *arg)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300649{
650 int rc;
651
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300652 dev->cmd_complete = cmd_complete;
653 dev->cmd_complete_arg = arg;
654
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100655 dev->out_urb->transfer_buffer = out->data;
656 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300657
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100658 dev->in_urb->transfer_buffer = in->data;
659 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300660
Samuel Ortize279f842013-03-20 11:27:57 +0100661 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
662 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100663
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100664 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300665 if (rc)
666 return rc;
667
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100668 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300669 if (rc)
670 goto error;
671
672 return 0;
673
674error:
675 usb_unlink_urb(dev->out_urb);
676 return rc;
677}
678
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100679static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
680 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100681{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100682 /* payload is already there, just update datalen */
683 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100684 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100685
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100686
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100687 skb_push(skb, ops->tx_header_len);
688 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100689
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100690 ops->tx_frame_init(skb->data, cmd_code);
691 ops->tx_update_payload_len(skb->data, payload_len);
692 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100693}
694
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200695static int pn533_send_async_complete(struct pn533 *dev, void *arg, int status)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100696{
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200697 struct pn533_cmd *cmd = arg;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100698
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200699 struct sk_buff *req = cmd->req;
700 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100701
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100702 int rc;
703
704 dev_kfree_skb(req);
705
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100706 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200707 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
708 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100709 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200710 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100711 }
712
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100713 skb_put(resp, dev->ops->rx_frame_size(resp->data));
714 skb_pull(resp, dev->ops->rx_header_len);
715 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100716
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200717 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100718
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200719done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200720 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200721 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100722 return rc;
723}
724
725static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
726 struct sk_buff *req, struct sk_buff *resp,
727 int resp_len,
728 pn533_send_async_complete_t complete_cb,
729 void *complete_cb_context)
730{
731 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100732 int rc = 0;
733
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100734 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100735
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200736 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
737 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100738 return -ENOMEM;
739
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200740 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200741 cmd->req = req;
742 cmd->resp = resp;
743 cmd->resp_len = resp_len;
744 cmd->complete_cb = complete_cb;
745 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100746
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100747 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100748
749 mutex_lock(&dev->cmd_lock);
750
751 if (!dev->cmd_pending) {
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100752 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200753 pn533_send_async_complete, cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100754 if (rc)
755 goto error;
756
757 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200758 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100759 goto unlock;
760 }
761
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100762 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
763 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100764
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100765 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100766 list_add_tail(&cmd->queue, &dev->cmd_queue);
767
768 goto unlock;
769
770error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200771 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100772unlock:
773 mutex_unlock(&dev->cmd_lock);
774 return rc;
775}
776
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100777static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
778 struct sk_buff *req,
779 pn533_send_async_complete_t complete_cb,
780 void *complete_cb_context)
781{
782 struct sk_buff *resp;
783 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100784 int resp_len = dev->ops->rx_header_len +
785 dev->ops->max_payload_len +
786 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100787
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100788 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
789 if (!resp)
790 return -ENOMEM;
791
792 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
793 complete_cb_context);
794 if (rc)
795 dev_kfree_skb(resp);
796
797 return rc;
798}
799
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100800static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
801 struct sk_buff *req,
802 pn533_send_async_complete_t complete_cb,
803 void *complete_cb_context)
804{
805 struct sk_buff *resp;
806 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100807 int resp_len = dev->ops->rx_header_len +
808 dev->ops->max_payload_len +
809 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100810
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100811 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100812 if (!resp)
813 return -ENOMEM;
814
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100815 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
816 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100817 if (rc)
818 dev_kfree_skb(resp);
819
820 return rc;
821}
822
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100823/*
824 * pn533_send_cmd_direct_async
825 *
826 * The function sends a piority cmd directly to the chip omiting the cmd
827 * queue. It's intended to be used by chaining mechanism of received responses
828 * where the host has to request every single chunk of data before scheduling
829 * next cmd from the queue.
830 */
831static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
832 struct sk_buff *req,
833 pn533_send_async_complete_t complete_cb,
834 void *complete_cb_context)
835{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100836 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200837 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100838 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100839 int resp_len = dev->ops->rx_header_len +
840 dev->ops->max_payload_len +
841 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100842
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100843 resp = alloc_skb(resp_len, GFP_KERNEL);
844 if (!resp)
845 return -ENOMEM;
846
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200847 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
848 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100849 dev_kfree_skb(resp);
850 return -ENOMEM;
851 }
852
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200853 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200854 cmd->req = req;
855 cmd->resp = resp;
856 cmd->resp_len = resp_len;
857 cmd->complete_cb = complete_cb;
858 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100859
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100860 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100861
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100862 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200863 pn533_send_async_complete, cmd);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100864 if (rc < 0) {
865 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200866 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200867 } else {
868 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100869 }
870
871 return rc;
872}
873
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200874static void pn533_wq_cmd(struct work_struct *work)
875{
876 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
877 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200878 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200879
880 mutex_lock(&dev->cmd_lock);
881
882 if (list_empty(&dev->cmd_queue)) {
883 dev->cmd_pending = 0;
884 mutex_unlock(&dev->cmd_lock);
885 return;
886 }
887
888 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
889
Szymon Janc60ad07a2012-10-25 17:29:45 +0200890 list_del(&cmd->queue);
891
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200892 mutex_unlock(&dev->cmd_lock);
893
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200894 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200895 pn533_send_async_complete, cmd);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200896 if (rc < 0) {
897 dev_kfree_skb(cmd->req);
898 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200899 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200900 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200901 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200902
903 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200904}
905
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300906struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100907 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300908 struct completion done;
909};
910
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100911static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
912 struct sk_buff *resp)
913{
914 struct pn533_sync_cmd_response *arg = _arg;
915
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100916 arg->resp = resp;
917 complete(&arg->done);
918
919 return 0;
920}
921
922/* pn533_send_cmd_sync
923 *
924 * Please note the req parameter is freed inside the function to
925 * limit a number of return value interpretations by the caller.
926 *
927 * 1. negative in case of error during TX path -> req should be freed
928 *
929 * 2. negative in case of error during RX path -> req should not be freed
930 * as it's been already freed at the begining of RX path by
931 * async_complete_cb.
932 *
933 * 3. valid pointer in case of succesfult RX path
934 *
935 * A caller has to check a return value with IS_ERR macro. If the test pass,
936 * the returned pointer is valid.
937 *
938 * */
939static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
940 struct sk_buff *req)
941{
942 int rc;
943 struct pn533_sync_cmd_response arg;
944
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100945 init_completion(&arg.done);
946
947 rc = pn533_send_cmd_async(dev, cmd_code, req,
948 pn533_send_sync_complete, &arg);
949 if (rc) {
950 dev_kfree_skb(req);
951 return ERR_PTR(rc);
952 }
953
954 wait_for_completion(&arg.done);
955
956 return arg.resp;
957}
958
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300959static void pn533_send_complete(struct urb *urb)
960{
961 struct pn533 *dev = urb->context;
962
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300963 switch (urb->status) {
964 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100965 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300966 case -ECONNRESET:
967 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100968 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100969 "The urb has been stopped (status %d)",
970 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300971 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100972 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300973 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100974 nfc_dev_err(&dev->interface->dev,
975 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300976 }
977}
978
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100979static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100980{
981 struct sk_buff *skb;
982
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100983 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100984 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100985 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100986
987 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100988 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100989
990 return skb;
991}
992
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300993struct pn533_target_type_a {
994 __be16 sens_res;
995 u8 sel_res;
996 u8 nfcid_len;
997 u8 nfcid_data[];
998} __packed;
999
1000
1001#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1002#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1003#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1004
1005#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1006#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1007
1008#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1009#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1010
1011#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1012#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1013#define PN533_TYPE_A_SEL_PROT_DEP 2
1014#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1015
1016static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1017 int target_data_len)
1018{
1019 u8 ssd;
1020 u8 platconf;
1021
1022 if (target_data_len < sizeof(struct pn533_target_type_a))
1023 return false;
1024
1025 /* The lenght check of nfcid[] and ats[] are not being performed because
1026 the values are not being used */
1027
1028 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1029 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1030 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1031
1032 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001033 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1034 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1035 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001036 return false;
1037
1038 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1039 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1040 return false;
1041
1042 return true;
1043}
1044
1045static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1046 int tgt_data_len)
1047{
1048 struct pn533_target_type_a *tgt_type_a;
1049
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001050 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001051
1052 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1053 return -EPROTO;
1054
1055 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1056 case PN533_TYPE_A_SEL_PROT_MIFARE:
1057 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1058 break;
1059 case PN533_TYPE_A_SEL_PROT_ISO14443:
1060 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1061 break;
1062 case PN533_TYPE_A_SEL_PROT_DEP:
1063 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1064 break;
1065 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1066 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1067 NFC_PROTO_NFC_DEP_MASK;
1068 break;
1069 }
1070
1071 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1072 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001073 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1074 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001075
1076 return 0;
1077}
1078
1079struct pn533_target_felica {
1080 u8 pol_res;
1081 u8 opcode;
1082 u8 nfcid2[8];
1083 u8 pad[8];
1084 /* optional */
1085 u8 syst_code[];
1086} __packed;
1087
1088#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1089#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1090
1091static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1092 int target_data_len)
1093{
1094 if (target_data_len < sizeof(struct pn533_target_felica))
1095 return false;
1096
1097 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1098 return false;
1099
1100 return true;
1101}
1102
1103static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1104 int tgt_data_len)
1105{
1106 struct pn533_target_felica *tgt_felica;
1107
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001108 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001109
1110 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1111 return -EPROTO;
1112
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001113 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1114 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001115 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1116 else
1117 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1118
Samuel Ortiz79757542012-03-05 01:03:45 +01001119 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1120 nfc_tgt->sensf_res_len = 9;
1121
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001122 return 0;
1123}
1124
1125struct pn533_target_jewel {
1126 __be16 sens_res;
1127 u8 jewelid[4];
1128} __packed;
1129
1130static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1131 int target_data_len)
1132{
1133 u8 ssd;
1134 u8 platconf;
1135
1136 if (target_data_len < sizeof(struct pn533_target_jewel))
1137 return false;
1138
1139 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1140 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1141 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1142
1143 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001144 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1145 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1146 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001147 return false;
1148
1149 return true;
1150}
1151
1152static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1153 int tgt_data_len)
1154{
1155 struct pn533_target_jewel *tgt_jewel;
1156
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001157 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001158
1159 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1160 return -EPROTO;
1161
1162 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1163 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001164 nfc_tgt->nfcid1_len = 4;
1165 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001166
1167 return 0;
1168}
1169
1170struct pn533_type_b_prot_info {
1171 u8 bitrate;
1172 u8 fsci_type;
1173 u8 fwi_adc_fo;
1174} __packed;
1175
1176#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1177#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1178#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1179
1180struct pn533_type_b_sens_res {
1181 u8 opcode;
1182 u8 nfcid[4];
1183 u8 appdata[4];
1184 struct pn533_type_b_prot_info prot_info;
1185} __packed;
1186
1187#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1188
1189struct pn533_target_type_b {
1190 struct pn533_type_b_sens_res sensb_res;
1191 u8 attrib_res_len;
1192 u8 attrib_res[];
1193} __packed;
1194
1195static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1196 int target_data_len)
1197{
1198 if (target_data_len < sizeof(struct pn533_target_type_b))
1199 return false;
1200
1201 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1202 return false;
1203
1204 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1205 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1206 return false;
1207
1208 return true;
1209}
1210
1211static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1212 int tgt_data_len)
1213{
1214 struct pn533_target_type_b *tgt_type_b;
1215
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001216 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001217
1218 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1219 return -EPROTO;
1220
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001221 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001222
1223 return 0;
1224}
1225
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001226static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1227 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001228{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229 struct nfc_target nfc_tgt;
1230 int rc;
1231
1232 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001233 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001234
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001235 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236 return -EPROTO;
1237
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001238 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1239
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001240 switch (dev->poll_mod_curr) {
1241 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001242 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001243 break;
1244 case PN533_POLL_MOD_212KBPS_FELICA:
1245 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001246 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001247 break;
1248 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001249 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001250 break;
1251 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001252 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001253 break;
1254 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001255 nfc_dev_err(&dev->interface->dev,
1256 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001257 return -EPROTO;
1258 }
1259
1260 if (rc)
1261 return rc;
1262
1263 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001264 nfc_dev_dbg(&dev->interface->dev,
1265 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001266 return -EAGAIN;
1267 }
1268
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001269 nfc_dev_dbg(&dev->interface->dev,
1270 "Target found - supported protocols: 0x%x",
1271 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001272
1273 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1274
1275 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1276
1277 return 0;
1278}
1279
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001280static inline void pn533_poll_next_mod(struct pn533 *dev)
1281{
1282 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1283}
1284
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001285static void pn533_poll_reset_mod_list(struct pn533 *dev)
1286{
1287 dev->poll_mod_count = 0;
1288}
1289
1290static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1291{
1292 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001293 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001294 dev->poll_mod_count++;
1295}
1296
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001297static void pn533_poll_create_mod_list(struct pn533 *dev,
1298 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001299{
1300 pn533_poll_reset_mod_list(dev);
1301
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001302 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1303 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1304 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001305 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1306
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001307 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1308 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001309 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1310 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1311 }
1312
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001313 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001314 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1315
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001316 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001317 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001318
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001319 if (tm_protocols)
1320 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001321}
1322
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001323static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001324{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001325 u8 nbtg, tg, *tgdata;
1326 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001327
1328 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1329
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001330 nbtg = resp->data[0];
1331 tg = resp->data[1];
1332 tgdata = &resp->data[2];
1333 tgdata_len = resp->len - 2; /* nbtg + tg */
1334
1335 if (nbtg) {
1336 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001337
1338 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001339 if (rc == 0) {
1340 pn533_poll_reset_mod_list(dev);
1341 return 0;
1342 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001343 }
1344
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001345 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001346}
1347
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001348static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001349{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001350 struct sk_buff *skb;
1351 u8 *felica, *nfcid3, *gb;
1352
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001353 u8 *gbytes = dev->gb;
1354 size_t gbytes_len = dev->gb_len;
1355
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001356 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1357 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1358 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1359 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001360
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001361 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1362 0x0, 0x0, 0x0,
1363 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001364
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001365 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1366 felica (18), nfcid3 (10), gb_len (1) */
1367 gbytes_len +
1368 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001369
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001370 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001371 if (!skb)
1372 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001373
1374 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001375 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001376
1377 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001378 memcpy(skb_put(skb, 6), mifare_params, 6);
1379
1380 /* Felica params */
1381 felica = skb_put(skb, 18);
1382 memcpy(felica, felica_params, 18);
1383 get_random_bytes(felica + 2, 6);
1384
1385 /* NFCID3 */
1386 nfcid3 = skb_put(skb, 10);
1387 memset(nfcid3, 0, 10);
1388 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001389
1390 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001391 *skb_put(skb, 1) = gbytes_len;
1392
1393 gb = skb_put(skb, gbytes_len);
1394 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001395
Samuel Ortizad3823c2012-05-30 23:54:55 +02001396 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001397 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001398
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001399 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001400}
1401
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001402#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001403#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1404static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001405 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001406{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001407 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001408
1409 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1410
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001411 if (IS_ERR(resp))
1412 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001413
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001414 status = resp->data[0];
1415 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001416
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001417 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001418 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001419 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001420 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001421 return 0;
1422 }
1423
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001424 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001425}
1426
1427static void pn533_wq_tg_get_data(struct work_struct *work)
1428{
1429 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001430
1431 struct sk_buff *skb;
1432 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001433
1434 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1435
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001436 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001437 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001438 return;
1439
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001440 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1441 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001442
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001443 if (rc < 0)
1444 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001445
1446 return;
1447}
1448
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001449#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001451{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001452 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001453 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001454 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001455
1456 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1457
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001458 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001459 return -EINVAL;
1460
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001461 mode = resp->data[0];
1462 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001463
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001464 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1465 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001466
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1468 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001469 comm_mode = NFC_COMM_ACTIVE;
1470
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001471 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001472 return -EOPNOTSUPP;
1473
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 gb = cmd + ATR_REQ_GB_OFFSET;
1475 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001476
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001477 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1478 comm_mode, gb, gb_len);
1479 if (rc < 0) {
1480 nfc_dev_err(&dev->interface->dev,
1481 "Error when signaling target activation");
1482 return rc;
1483 }
1484
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001485 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001486 queue_work(dev->wq, &dev->tg_work);
1487
1488 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001489}
1490
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001491static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001492{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001493 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001494
1495 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1496
1497 /* An ack will cancel the last issued command (poll) */
1498 pn533_send_ack(dev, GFP_ATOMIC);
1499
1500 dev->cancel_listen = 1;
1501
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502 pn533_poll_next_mod(dev);
1503
1504 queue_work(dev->wq, &dev->poll_work);
1505}
1506
1507static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001508 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001509{
1510 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001511 int rc;
1512
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001513 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1514
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001515 if (IS_ERR(resp)) {
1516 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001517
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001518 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1519 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001520
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001521 if (rc == -ENOENT) {
1522 if (dev->poll_mod_count != 0)
1523 return rc;
1524 else
1525 goto stop_poll;
1526 } else if (rc < 0) {
1527 nfc_dev_err(&dev->interface->dev,
1528 "Error %d when running poll", rc);
1529 goto stop_poll;
1530 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001531 }
1532
1533 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1534
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001535 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001536 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001537 rc = pn533_init_target_complete(dev, resp);
1538 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001539 }
1540
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001541 /* Initiator mode */
1542 rc = pn533_start_poll_complete(dev, resp);
1543 if (!rc)
1544 goto done;
1545
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001546 if (!dev->poll_mod_count) {
1547 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1548 goto done;
1549 }
1550
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001551 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001552 queue_work(dev->wq, &dev->poll_work);
1553
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001554done:
1555 dev_kfree_skb(resp);
1556 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557
1558stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001559 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1560
Samuel Ortizad3823c2012-05-30 23:54:55 +02001561 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001563 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001564}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001565
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001566static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1567 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001568{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001569 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001570
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001571 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001572 if (!skb)
1573 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001574
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001575 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001576
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001577 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001578}
1579
1580static int pn533_send_poll_frame(struct pn533 *dev)
1581{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001582 struct pn533_poll_modulations *mod;
1583 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001584 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001585 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001586
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001587 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001588
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001589 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1590 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001591
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001592 if (mod->len == 0) { /* Listen mode */
1593 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001594 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001595 } else { /* Polling mode */
1596 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001597 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001598 }
1599
1600 if (!skb) {
1601 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1602 return -ENOMEM;
1603 }
1604
1605 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1606 NULL);
1607 if (rc < 0) {
1608 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001609 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001610 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001611
1612 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001613}
1614
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001615static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001616{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001617 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1618 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001619 int rc;
1620
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1622
1623 nfc_dev_dbg(&dev->interface->dev,
1624 "%s cancel_listen %d modulation len %d",
1625 __func__, dev->cancel_listen, cur_mod->len);
1626
1627 if (dev->cancel_listen == 1) {
1628 dev->cancel_listen = 0;
1629 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001630 }
1631
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001632 rc = pn533_send_poll_frame(dev);
1633 if (rc)
1634 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001635
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001636 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1637 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001638
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001639 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001640}
1641
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001642static int pn533_start_poll(struct nfc_dev *nfc_dev,
1643 u32 im_protocols, u32 tm_protocols)
1644{
1645 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1646
1647 nfc_dev_dbg(&dev->interface->dev,
1648 "%s: im protocols 0x%x tm protocols 0x%x",
1649 __func__, im_protocols, tm_protocols);
1650
1651 if (dev->tgt_active_prot) {
1652 nfc_dev_err(&dev->interface->dev,
1653 "Cannot poll with a target already activated");
1654 return -EBUSY;
1655 }
1656
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001657 if (dev->tgt_mode) {
1658 nfc_dev_err(&dev->interface->dev,
1659 "Cannot poll while already being activated");
1660 return -EBUSY;
1661 }
1662
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001663 if (tm_protocols) {
1664 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1665 if (dev->gb == NULL)
1666 tm_protocols = 0;
1667 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001668
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001669 dev->poll_mod_curr = 0;
1670 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1671 dev->poll_protocols = im_protocols;
1672 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001673
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001674 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001675}
1676
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001677static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1678{
1679 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1680
1681 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1682
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001683 del_timer(&dev->listen_timer);
1684
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001685 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001686 nfc_dev_dbg(&dev->interface->dev,
1687 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001688 return;
1689 }
1690
1691 /* An ack will cancel the last issued command (poll) */
1692 pn533_send_ack(dev, GFP_KERNEL);
1693
1694 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1695 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001696
1697 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698}
1699
1700static int pn533_activate_target_nfcdep(struct pn533 *dev)
1701{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001702 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001703 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001704 int rc;
1705
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001706 struct sk_buff *skb;
1707 struct sk_buff *resp;
1708
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001709 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1710
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001711 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001712 if (!skb)
1713 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001715 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1716 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001718 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1719 if (IS_ERR(resp))
1720 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001721
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001722 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001723 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001724 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001725 nfc_dev_err(&dev->interface->dev,
1726 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001727 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001729 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001730
Samuel Ortiz541d9202011-12-14 16:43:10 +01001731 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001732 gt_len = resp->len - 16;
1733 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001734
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001735 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001736 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001737}
1738
Eric Lapuyade90099432012-05-07 12:31:13 +02001739static int pn533_activate_target(struct nfc_dev *nfc_dev,
1740 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001741{
1742 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1743 int rc;
1744
1745 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001746 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001747
1748 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001749 nfc_dev_err(&dev->interface->dev,
1750 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001751 return -EBUSY;
1752 }
1753
1754 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001755 nfc_dev_err(&dev->interface->dev,
1756 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001757 return -EBUSY;
1758 }
1759
1760 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001761 nfc_dev_err(&dev->interface->dev,
1762 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001763 return -EINVAL;
1764 }
1765
1766 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001767 nfc_dev_err(&dev->interface->dev,
1768 "Target doesn't support requested proto %u",
1769 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001770 return -EINVAL;
1771 }
1772
1773 if (protocol == NFC_PROTO_NFC_DEP) {
1774 rc = pn533_activate_target_nfcdep(dev);
1775 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001776 nfc_dev_err(&dev->interface->dev,
1777 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001778 return rc;
1779 }
1780 }
1781
1782 dev->tgt_active_prot = protocol;
1783 dev->tgt_available_prots = 0;
1784
1785 return 0;
1786}
1787
Eric Lapuyade90099432012-05-07 12:31:13 +02001788static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1789 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001790{
1791 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001792
1793 struct sk_buff *skb;
1794 struct sk_buff *resp;
1795
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001796 int rc;
1797
1798 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1799
1800 if (!dev->tgt_active_prot) {
1801 nfc_dev_err(&dev->interface->dev, "There is no active target");
1802 return;
1803 }
1804
1805 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001806 skb_queue_purge(&dev->resp_q);
1807
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001808 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001809 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001810 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001811
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001812 *skb_put(skb, 1) = 1; /* TG*/
1813
1814 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1815 if (IS_ERR(resp))
1816 return;
1817
1818 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001819 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001820 nfc_dev_err(&dev->interface->dev,
1821 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001822
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001823 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001824 return;
1825}
1826
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001827
1828static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001829 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001830{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001831 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001832 u8 target_gt_len;
1833 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001834 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001835
1836 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 if (IS_ERR(resp))
1839 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001840
1841 if (dev->tgt_available_prots &&
1842 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1843 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001844 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001845 rc = -EINVAL;
1846 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001847 }
1848
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001849 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1850
1851 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001852 if (rc != PN533_CMD_RET_SUCCESS) {
1853 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001854 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001855 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001856 }
1857
1858 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001859 struct nfc_target nfc_target;
1860
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001861 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1862
1863 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001864 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001865 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001866 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1867 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001868 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869
1870 dev->tgt_available_prots = 0;
1871 }
1872
1873 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1874
1875 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001876 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001877 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001878 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001879 if (rc == 0)
1880 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001881 dev->nfc_dev->targets[0].idx,
1882 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001883
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001884error:
1885 dev_kfree_skb(resp);
1886 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001887}
1888
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001889static int pn533_mod_to_baud(struct pn533 *dev)
1890{
1891 switch (dev->poll_mod_curr) {
1892 case PN533_POLL_MOD_106KBPS_A:
1893 return 0;
1894 case PN533_POLL_MOD_212KBPS_FELICA:
1895 return 1;
1896 case PN533_POLL_MOD_424KBPS_FELICA:
1897 return 2;
1898 default:
1899 return -EINVAL;
1900 }
1901}
1902
Samuel Ortizd7f33452012-05-29 21:45:21 +02001903#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001904static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001905 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001906{
1907 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001908 struct sk_buff *skb;
1909 int rc, baud, skb_len;
1910 u8 *next, *arg;
1911
Samuel Ortizd7f33452012-05-29 21:45:21 +02001912 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001913
1914 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1915
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001916 if (dev->poll_mod_count) {
1917 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001918 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001919 return -EBUSY;
1920 }
1921
1922 if (dev->tgt_active_prot) {
1923 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001924 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001925 return -EBUSY;
1926 }
1927
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001928 baud = pn533_mod_to_baud(dev);
1929 if (baud < 0) {
1930 nfc_dev_err(&dev->interface->dev,
1931 "Invalid curr modulation %d", dev->poll_mod_curr);
1932 return baud;
1933 }
1934
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001935 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001936 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001937 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001938
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001939 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001940 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001941 return -ENOMEM;
1942
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001943 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1944 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001945
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001946 next = skb_put(skb, 1); /* Next */
1947 *next = 0;
1948
1949 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1950 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1951 PASSIVE_DATA_LEN);
1952 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001953 }
1954
Samuel Ortiz47807d32012-03-05 01:03:50 +01001955 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001956 memcpy(skb_put(skb, gb_len), gb, gb_len);
1957 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001958 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001959 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001960 }
1961
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001962 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1963 if (!arg) {
1964 dev_kfree_skb(skb);
1965 return -ENOMEM;
1966 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001967
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001968 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001969
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001970 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1971 pn533_in_dep_link_up_complete, arg);
1972
1973 if (rc < 0) {
1974 dev_kfree_skb(skb);
1975 kfree(arg);
1976 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001977
1978 return rc;
1979}
1980
1981static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1982{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001983 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1984
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001985 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1986
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001987 pn533_poll_reset_mod_list(dev);
1988
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001989 if (dev->tgt_mode || dev->tgt_active_prot) {
1990 pn533_send_ack(dev, GFP_KERNEL);
1991 usb_kill_urb(dev->in_urb);
1992 }
1993
1994 dev->tgt_active_prot = 0;
1995 dev->tgt_mode = 0;
1996
1997 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001998
1999 return 0;
2000}
2001
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002002struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002003 data_exchange_cb_t cb;
2004 void *cb_context;
2005};
2006
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002007static struct sk_buff *pn533_build_response(struct pn533 *dev)
2008{
2009 struct sk_buff *skb, *tmp, *t;
2010 unsigned int skb_len = 0, tmp_len = 0;
2011
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002012 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002013
2014 if (skb_queue_empty(&dev->resp_q))
2015 return NULL;
2016
2017 if (skb_queue_len(&dev->resp_q) == 1) {
2018 skb = skb_dequeue(&dev->resp_q);
2019 goto out;
2020 }
2021
2022 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2023 skb_len += tmp->len;
2024
2025 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2026 __func__, skb_len);
2027
2028 skb = alloc_skb(skb_len, GFP_KERNEL);
2029 if (skb == NULL)
2030 goto out;
2031
2032 skb_put(skb, skb_len);
2033
2034 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2035 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2036 tmp_len += tmp->len;
2037 }
2038
2039out:
2040 skb_queue_purge(&dev->resp_q);
2041
2042 return skb;
2043}
2044
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002045static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002046 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002047{
2048 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002049 struct sk_buff *skb;
2050 int rc = 0;
2051 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002052
2053 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2054
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002055 if (IS_ERR(resp)) {
2056 rc = PTR_ERR(resp);
2057 goto _error;
2058 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002059
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002060 status = resp->data[0];
2061 ret = status & PN533_CMD_RET_MASK;
2062 mi = status & PN533_CMD_MI_MASK;
2063
2064 skb_pull(resp, sizeof(status));
2065
2066 if (ret != PN533_CMD_RET_SUCCESS) {
2067 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002068 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002069 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002070 goto error;
2071 }
2072
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002073 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002074
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002075 if (mi) {
2076 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002077 queue_work(dev->wq, &dev->mi_work);
2078 return -EINPROGRESS;
2079 }
2080
2081 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002082 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002083 goto error;
2084
2085 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002086 kfree(arg);
2087 return 0;
2088
2089error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002090 dev_kfree_skb(resp);
2091_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002092 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002093 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002094 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002095 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002096}
2097
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002098static int pn533_transceive(struct nfc_dev *nfc_dev,
2099 struct nfc_target *target, struct sk_buff *skb,
2100 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002101{
2102 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002103 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002104 int rc;
2105
2106 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2107
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002108 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2109 /* TODO: Implement support to multi-part data exchange */
2110 nfc_dev_err(&dev->interface->dev,
2111 "Data length greater than the max allowed: %d",
2112 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2113 rc = -ENOSYS;
2114 goto error;
2115 }
2116
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002117 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002118 nfc_dev_err(&dev->interface->dev,
2119 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 rc = -EINVAL;
2121 goto error;
2122 }
2123
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002124 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002125 if (!arg) {
2126 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002127 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002128 }
2129
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002130 arg->cb = cb;
2131 arg->cb_context = cb_context;
2132
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002133 switch (dev->device_type) {
2134 case PN533_DEVICE_PASORI:
2135 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2136 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2137 skb,
2138 pn533_data_exchange_complete,
2139 arg);
2140
2141 break;
2142 }
2143 default:
2144 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2145
2146 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2147 skb, pn533_data_exchange_complete,
2148 arg);
2149
2150 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002151 }
2152
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002153 if (rc < 0) /* rc from send_async */
2154 goto error;
2155
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002156 return 0;
2157
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002158error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002159 kfree(arg);
2160 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002161 return rc;
2162}
2163
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002164static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002165 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002166{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002168
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002169 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2170
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002171 if (IS_ERR(resp))
2172 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002173
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002174 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002176 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002177
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002178 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002179 nfc_tm_deactivated(dev->nfc_dev);
2180
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002181 dev->tgt_mode = 0;
2182
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002183 return 0;
2184 }
2185
2186 queue_work(dev->wq, &dev->tg_work);
2187
2188 return 0;
2189}
2190
2191static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2192{
2193 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002194 int rc;
2195
2196 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2197
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002198 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002199 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002200 "Data length greater than the max allowed: %d",
2201 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2202 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002203 }
2204
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002205 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2206 pn533_tm_send_complete, NULL);
2207 if (rc < 0)
2208 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002209
2210 return rc;
2211}
2212
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002213static void pn533_wq_mi_recv(struct work_struct *work)
2214{
2215 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002216
2217 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002218 int rc;
2219
2220 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2221
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002222 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002223 if (!skb)
2224 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002225
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002226 switch (dev->device_type) {
2227 case PN533_DEVICE_PASORI:
2228 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2229 rc = pn533_send_cmd_direct_async(dev,
2230 PN533_CMD_IN_COMM_THRU,
2231 skb,
2232 pn533_data_exchange_complete,
2233 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002234
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002235 break;
2236 }
2237 default:
2238 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002239
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002240 rc = pn533_send_cmd_direct_async(dev,
2241 PN533_CMD_IN_DATA_EXCHANGE,
2242 skb,
2243 pn533_data_exchange_complete,
2244 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002245
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002246 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002247 }
2248
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002249 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002250 return;
2251
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002252 nfc_dev_err(&dev->interface->dev,
2253 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002254
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002255 dev_kfree_skb(skb);
2256 kfree(dev->cmd_complete_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002257
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002258error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002259 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002260 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002261}
2262
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002263static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2264 u8 cfgdata_len)
2265{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002266 struct sk_buff *skb;
2267 struct sk_buff *resp;
2268
2269 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002270
2271 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2272
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002273 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002274
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002275 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002276 if (!skb)
2277 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002278
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002279 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2280 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002281
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002282 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2283 if (IS_ERR(resp))
2284 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002285
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 dev_kfree_skb(resp);
2287 return 0;
2288}
2289
2290static int pn533_get_firmware_version(struct pn533 *dev,
2291 struct pn533_fw_version *fv)
2292{
2293 struct sk_buff *skb;
2294 struct sk_buff *resp;
2295
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002296 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002297 if (!skb)
2298 return -ENOMEM;
2299
2300 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2301 if (IS_ERR(resp))
2302 return PTR_ERR(resp);
2303
2304 fv->ic = resp->data[0];
2305 fv->ver = resp->data[1];
2306 fv->rev = resp->data[2];
2307 fv->support = resp->data[3];
2308
2309 dev_kfree_skb(resp);
2310 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002311}
2312
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002313static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002314{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002315 struct sk_buff *skb;
2316 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002317
2318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2319
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002320 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002321 if (!skb)
2322 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002323
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002324 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002325
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002326 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2327 if (IS_ERR(resp))
2328 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002329
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002330 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002331
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002332 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002333}
2334
2335static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002336 .dev_up = NULL,
2337 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002338 .dep_link_up = pn533_dep_link_up,
2339 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002340 .start_poll = pn533_start_poll,
2341 .stop_poll = pn533_stop_poll,
2342 .activate_target = pn533_activate_target,
2343 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002344 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002345 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002346};
2347
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002348static int pn533_setup(struct pn533 *dev)
2349{
2350 struct pn533_config_max_retries max_retries;
2351 struct pn533_config_timing timing;
2352 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2353 int rc;
2354
2355 switch (dev->device_type) {
2356 case PN533_DEVICE_STD:
2357 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2358 max_retries.mx_rty_psl = 2;
2359 max_retries.mx_rty_passive_act =
2360 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2361
2362 timing.rfu = PN533_CONFIG_TIMING_102;
2363 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2364 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2365
2366 break;
2367
2368 case PN533_DEVICE_PASORI:
2369 max_retries.mx_rty_atr = 0x2;
2370 max_retries.mx_rty_psl = 0x1;
2371 max_retries.mx_rty_passive_act =
2372 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2373
2374 timing.rfu = PN533_CONFIG_TIMING_102;
2375 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2376 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2377
2378 break;
2379
2380 default:
2381 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2382 dev->device_type);
2383 return -EINVAL;
2384 }
2385
2386 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2387 (u8 *)&max_retries, sizeof(max_retries));
2388 if (rc) {
2389 nfc_dev_err(&dev->interface->dev,
2390 "Error on setting MAX_RETRIES config");
2391 return rc;
2392 }
2393
2394
2395 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2396 (u8 *)&timing, sizeof(timing));
2397 if (rc) {
2398 nfc_dev_err(&dev->interface->dev,
2399 "Error on setting RF timings");
2400 return rc;
2401 }
2402
2403 switch (dev->device_type) {
2404 case PN533_DEVICE_STD:
2405 break;
2406
2407 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002408 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002409
2410 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2411 pasori_cfg, 3);
2412 if (rc) {
2413 nfc_dev_err(&dev->interface->dev,
2414 "Error while settings PASORI config");
2415 return rc;
2416 }
2417
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002418 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002419
2420 break;
2421 }
2422
2423 return 0;
2424}
2425
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002426static int pn533_probe(struct usb_interface *interface,
2427 const struct usb_device_id *id)
2428{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002429 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002430 struct pn533 *dev;
2431 struct usb_host_interface *iface_desc;
2432 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002433 int in_endpoint = 0;
2434 int out_endpoint = 0;
2435 int rc = -ENOMEM;
2436 int i;
2437 u32 protocols;
2438
2439 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2440 if (!dev)
2441 return -ENOMEM;
2442
2443 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2444 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002445 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002446
2447 iface_desc = interface->cur_altsetting;
2448 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2449 endpoint = &iface_desc->endpoint[i].desc;
2450
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002451 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002452 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002453
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002454 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002456 }
2457
2458 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002459 nfc_dev_err(&interface->dev,
2460 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002461 rc = -ENODEV;
2462 goto error;
2463 }
2464
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002465 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2467
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002468 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002469 goto error;
2470
2471 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002472 usb_rcvbulkpipe(dev->udev, in_endpoint),
2473 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002474 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002475 usb_sndbulkpipe(dev->udev, out_endpoint),
2476 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002477
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002478 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2479 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002480 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002481 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002482 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002483 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002484 if (dev->wq == NULL)
2485 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002486
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002487 init_timer(&dev->listen_timer);
2488 dev->listen_timer.data = (unsigned long) dev;
2489 dev->listen_timer.function = pn533_listen_mode_timer;
2490
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002491 skb_queue_head_init(&dev->resp_q);
2492
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002493 INIT_LIST_HEAD(&dev->cmd_queue);
2494
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002495 usb_set_intfdata(interface, dev);
2496
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002497 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002498
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002499 dev->device_type = id->driver_info;
2500 switch (dev->device_type) {
2501 case PN533_DEVICE_STD:
2502 protocols = PN533_ALL_PROTOCOLS;
2503 break;
2504
2505 case PN533_DEVICE_PASORI:
2506 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2507 break;
2508
2509 default:
2510 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2511 dev->device_type);
2512 rc = -EINVAL;
2513 goto destroy_wq;
2514 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002515
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002516 memset(&fw_ver, 0, sizeof(fw_ver));
2517 rc = pn533_get_firmware_version(dev, &fw_ver);
2518 if (rc < 0)
2519 goto destroy_wq;
2520
2521 nfc_dev_info(&dev->interface->dev,
2522 "NXP PN533 firmware ver %d.%d now attached",
2523 fw_ver.ver, fw_ver.rev);
2524
2525
Samuel Ortize8753042011-08-19 15:47:11 +02002526 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002527 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002528 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002529 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002530 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002531 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002532 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002533
2534 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2535 nfc_set_drvdata(dev->nfc_dev, dev);
2536
2537 rc = nfc_register_device(dev->nfc_dev);
2538 if (rc)
2539 goto free_nfc_dev;
2540
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002541 rc = pn533_setup(dev);
2542 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002543 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002544
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002545 return 0;
2546
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002547unregister_nfc_dev:
2548 nfc_unregister_device(dev->nfc_dev);
2549
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002550free_nfc_dev:
2551 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002552
Samuel Ortiz4849f852012-04-10 19:43:17 +02002553destroy_wq:
2554 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002556 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002557 usb_free_urb(dev->out_urb);
2558 kfree(dev);
2559 return rc;
2560}
2561
2562static void pn533_disconnect(struct usb_interface *interface)
2563{
2564 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002565 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002566
2567 dev = usb_get_intfdata(interface);
2568 usb_set_intfdata(interface, NULL);
2569
2570 nfc_unregister_device(dev->nfc_dev);
2571 nfc_free_device(dev->nfc_dev);
2572
2573 usb_kill_urb(dev->in_urb);
2574 usb_kill_urb(dev->out_urb);
2575
Samuel Ortiz4849f852012-04-10 19:43:17 +02002576 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002577
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002578 skb_queue_purge(&dev->resp_q);
2579
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002580 del_timer(&dev->listen_timer);
2581
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002582 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2583 list_del(&cmd->queue);
2584 kfree(cmd);
2585 }
2586
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002587 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002588 usb_free_urb(dev->out_urb);
2589 kfree(dev);
2590
Dan Carpenter276556d2011-07-08 10:21:15 +03002591 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002592}
2593
2594static struct usb_driver pn533_driver = {
2595 .name = "pn533",
2596 .probe = pn533_probe,
2597 .disconnect = pn533_disconnect,
2598 .id_table = pn533_table,
2599};
2600
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002601module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002602
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002603MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2604MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2605MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002606MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2607MODULE_VERSION(VERSION);
2608MODULE_LICENSE("GPL");