blob: edee0d55d8f436e54edddd3dd3e60d73fdf52856 [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;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200317 u32 device_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300318
319 struct urb *out_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300320 struct urb *in_urb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300321
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200322 struct sk_buff_head resp_q;
323
Samuel Ortiz4849f852012-04-10 19:43:17 +0200324 struct workqueue_struct *wq;
325 struct work_struct cmd_work;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200326 struct work_struct cmd_complete_work;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200327 struct work_struct poll_work;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +0200328 struct work_struct mi_work;
Samuel Ortiz103b34c2012-05-31 00:07:51 +0200329 struct work_struct tg_work;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200330
331 struct list_head cmd_queue;
332 struct pn533_cmd *cmd;
333 u8 cmd_pending;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200334 struct mutex cmd_lock; /* protects cmd queue */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300335
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100336 void *cmd_complete_mi_arg;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300337
338 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
339 u8 poll_mod_count;
340 u8 poll_mod_curr;
341 u32 poll_protocols;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200342 u32 listen_protocols;
Waldemar Rymarkiewiczd5590bb2013-04-03 08:02:10 +0200343 struct timer_list listen_timer;
344 int cancel_listen;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200345
346 u8 *gb;
347 size_t gb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300348
349 u8 tgt_available_prots;
350 u8 tgt_active_prot;
Samuel Ortiz51ad3042012-05-31 20:01:32 +0200351 u8 tgt_mode;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +0200352
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100353 struct pn533_frame_ops *ops;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200354};
355
356struct pn533_cmd {
357 struct list_head queue;
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200358 u8 code;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200359 int status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100360 struct sk_buff *req;
361 struct sk_buff *resp;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100362 int resp_len;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200363 pn533_send_async_complete_t complete_cb;
364 void *complete_cb_context;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300365};
366
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200367struct pn533_std_frame {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300368 u8 preamble;
369 __be16 start_frame;
370 u8 datalen;
371 u8 datalen_checksum;
372 u8 data[];
373} __packed;
374
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100375struct pn533_frame_ops {
376 void (*tx_frame_init)(void *frame, u8 cmd_code);
377 void (*tx_frame_finish)(void *frame);
378 void (*tx_update_payload_len)(void *frame, int len);
379 int tx_header_len;
380 int tx_tail_len;
381
382 bool (*rx_is_frame_valid)(void *frame);
383 int (*rx_frame_size)(void *frame);
384 int rx_header_len;
385 int rx_tail_len;
386
387 int max_payload_len;
388 u8 (*get_cmd_code)(void *frame);
389};
390
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300391/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200392static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300393{
394 return ~value + 1;
395}
396
397/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200398static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300399{
400 u8 sum = 0;
401 int i;
402
403 for (i = 0; i < datalen; i++)
404 sum += data[i];
405
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200406 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300407}
408
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200409static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300410{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200411 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100412
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300413 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200414 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
415 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
416 PN533_STD_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300417 frame->datalen = 2;
418}
419
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200420static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300421{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200422 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100423
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200424 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300425
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200426 PN533_STD_FRAME_CHECKSUM(frame) =
427 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300428
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200429 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300430}
431
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200432static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100433{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200434 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100435
436 frame->datalen += len;
437}
438
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200439static bool pn533_std_rx_frame_is_valid(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300440{
441 u8 checksum;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200442 struct pn533_std_frame *frame = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300443
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200444 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300445 return false;
446
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200447 checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300448 if (checksum != frame->datalen_checksum)
449 return false;
450
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200451 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
452 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300453 return false;
454
455 return true;
456}
457
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200458static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300459{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200460 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300461 return false;
462
463 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
464 return false;
465
466 return true;
467}
468
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200469static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300470{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200471 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100472
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200473 return sizeof(struct pn533_std_frame) + f->datalen +
474 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100475}
476
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200477static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100478{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200479 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100480
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200481 return PN533_STD_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100482}
483
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100484static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200485 .tx_frame_init = pn533_std_tx_frame_init,
486 .tx_frame_finish = pn533_std_tx_frame_finish,
487 .tx_update_payload_len = pn533_std_tx_update_payload_len,
488 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
489 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100490
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200491 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
492 .rx_frame_size = pn533_std_rx_frame_size,
493 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
494 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100495
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200496 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
497 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100498};
499
500static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
501{
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200502 return (dev->ops->get_cmd_code(frame) ==
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200503 PN533_CMD_RESPONSE(dev->cmd->code));
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300504}
505
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300506static void pn533_recv_response(struct urb *urb)
507{
508 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200509 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100510 u8 *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300511
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200512 cmd->status = urb->status;
513
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300514 switch (urb->status) {
515 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100516 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300517 case -ECONNRESET:
518 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100519 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100520 "The urb has been canceled (status %d)",
521 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200522 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100523 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300524 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100525 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100526 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200527 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300528 }
529
530 in_frame = dev->in_urb->transfer_buffer;
531
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100532 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
Samuel Ortize279f842013-03-20 11:27:57 +0100533 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
534 dev->ops->rx_frame_size(in_frame), false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100535
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100536 if (!dev->ops->rx_is_frame_valid(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300537 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200538 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200539 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300540 }
541
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100542 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100543 nfc_dev_err(&dev->interface->dev,
544 "It it not the response to the last command");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200545 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200546 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300547 }
548
Samuel Ortiz4849f852012-04-10 19:43:17 +0200549sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200550 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300551}
552
553static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
554{
555 dev->in_urb->complete = pn533_recv_response;
556
557 return usb_submit_urb(dev->in_urb, flags);
558}
559
560static void pn533_recv_ack(struct urb *urb)
561{
562 struct pn533 *dev = urb->context;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200563 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200564 struct pn533_std_frame *in_frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565 int rc;
566
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200567 cmd->status = urb->status;
568
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300569 switch (urb->status) {
570 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100571 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300572 case -ECONNRESET:
573 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100574 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100575 "The urb has been stopped (status %d)",
576 urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200577 goto sched_wq;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100578 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300579 default:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100580 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100581 "Urb failure (status %d)", urb->status);
Samuel Ortiz4849f852012-04-10 19:43:17 +0200582 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300583 }
584
585 in_frame = dev->in_urb->transfer_buffer;
586
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200587 if (!pn533_std_rx_frame_is_ack(in_frame)) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300588 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200589 cmd->status = -EIO;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200590 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300591 }
592
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300593 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
594 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100595 nfc_dev_err(&dev->interface->dev,
596 "usb_submit_urb failed with result %d", rc);
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200597 cmd->status = rc;
Samuel Ortiz4849f852012-04-10 19:43:17 +0200598 goto sched_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300599 }
600
601 return;
602
Samuel Ortiz4849f852012-04-10 19:43:17 +0200603sched_wq:
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200604 queue_work(dev->wq, &dev->cmd_complete_work);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300605}
606
607static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
608{
609 dev->in_urb->complete = pn533_recv_ack;
610
611 return usb_submit_urb(dev->in_urb, flags);
612}
613
614static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
615{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200616 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100617 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300618 int rc;
619
620 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
621
Waldemar Rymarkiewicz5b5a4432012-12-10 14:42:56 +0100622 dev->out_urb->transfer_buffer = ack;
623 dev->out_urb->transfer_buffer_length = sizeof(ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300624 rc = usb_submit_urb(dev->out_urb, flags);
625
626 return rc;
627}
628
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100629static int __pn533_send_frame_async(struct pn533 *dev,
630 struct sk_buff *out,
631 struct sk_buff *in,
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200632 int in_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300633{
634 int rc;
635
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100636 dev->out_urb->transfer_buffer = out->data;
637 dev->out_urb->transfer_buffer_length = out->len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300638
Waldemar Rymarkiewicze8f40532012-12-10 14:42:57 +0100639 dev->in_urb->transfer_buffer = in->data;
640 dev->in_urb->transfer_buffer_length = in_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300641
Samuel Ortize279f842013-03-20 11:27:57 +0100642 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
643 out->data, out->len, false);
Waldemar Rymarkiewicz99e591b2012-12-10 14:42:47 +0100644
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100645 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300646 if (rc)
647 return rc;
648
Waldemar Rymarkiewiczd94ea4f2012-11-26 14:18:33 +0100649 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300650 if (rc)
651 goto error;
652
653 return 0;
654
655error:
656 usb_unlink_urb(dev->out_urb);
657 return rc;
658}
659
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100660static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
661 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100662{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100663 /* payload is already there, just update datalen */
664 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100665 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100666
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100667
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100668 skb_push(skb, ops->tx_header_len);
669 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100670
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100671 ops->tx_frame_init(skb->data, cmd_code);
672 ops->tx_update_payload_len(skb->data, payload_len);
673 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100674}
675
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200676static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100677{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200678 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200679 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100680
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200681 struct sk_buff *req = cmd->req;
682 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100683
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100684 int rc;
685
686 dev_kfree_skb(req);
687
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100688 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200689 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
690 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100691 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200692 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100693 }
694
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100695 skb_put(resp, dev->ops->rx_frame_size(resp->data));
696 skb_pull(resp, dev->ops->rx_header_len);
697 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100698
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200699 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100700
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200701done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200702 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200703 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100704 return rc;
705}
706
707static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
708 struct sk_buff *req, struct sk_buff *resp,
709 int resp_len,
710 pn533_send_async_complete_t complete_cb,
711 void *complete_cb_context)
712{
713 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100714 int rc = 0;
715
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100716 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100717
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200718 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
719 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100720 return -ENOMEM;
721
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200722 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200723 cmd->req = req;
724 cmd->resp = resp;
725 cmd->resp_len = resp_len;
726 cmd->complete_cb = complete_cb;
727 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100728
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100729 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100730
731 mutex_lock(&dev->cmd_lock);
732
733 if (!dev->cmd_pending) {
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200734 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100735 if (rc)
736 goto error;
737
738 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200739 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100740 goto unlock;
741 }
742
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +0100743 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
744 cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100745
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100746 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100747 list_add_tail(&cmd->queue, &dev->cmd_queue);
748
749 goto unlock;
750
751error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200752 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100753unlock:
754 mutex_unlock(&dev->cmd_lock);
755 return rc;
756}
757
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100758static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
759 struct sk_buff *req,
760 pn533_send_async_complete_t complete_cb,
761 void *complete_cb_context)
762{
763 struct sk_buff *resp;
764 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100765 int resp_len = dev->ops->rx_header_len +
766 dev->ops->max_payload_len +
767 dev->ops->rx_tail_len;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100768
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100769 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
770 if (!resp)
771 return -ENOMEM;
772
773 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
774 complete_cb_context);
775 if (rc)
776 dev_kfree_skb(resp);
777
778 return rc;
779}
780
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100781static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
782 struct sk_buff *req,
783 pn533_send_async_complete_t complete_cb,
784 void *complete_cb_context)
785{
786 struct sk_buff *resp;
787 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100788 int resp_len = dev->ops->rx_header_len +
789 dev->ops->max_payload_len +
790 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100791
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100792 resp = alloc_skb(resp_len, GFP_KERNEL);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100793 if (!resp)
794 return -ENOMEM;
795
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100796 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
797 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100798 if (rc)
799 dev_kfree_skb(resp);
800
801 return rc;
802}
803
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100804/*
805 * pn533_send_cmd_direct_async
806 *
807 * The function sends a piority cmd directly to the chip omiting the cmd
808 * queue. It's intended to be used by chaining mechanism of received responses
809 * where the host has to request every single chunk of data before scheduling
810 * next cmd from the queue.
811 */
812static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
813 struct sk_buff *req,
814 pn533_send_async_complete_t complete_cb,
815 void *complete_cb_context)
816{
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100817 struct sk_buff *resp;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200818 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100819 int rc;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100820 int resp_len = dev->ops->rx_header_len +
821 dev->ops->max_payload_len +
822 dev->ops->rx_tail_len;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100823
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100824 resp = alloc_skb(resp_len, GFP_KERNEL);
825 if (!resp)
826 return -ENOMEM;
827
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200828 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
829 if (!cmd) {
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100830 dev_kfree_skb(resp);
831 return -ENOMEM;
832 }
833
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200834 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200835 cmd->req = req;
836 cmd->resp = resp;
837 cmd->resp_len = resp_len;
838 cmd->complete_cb = complete_cb;
839 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100840
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100841 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100842
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200843 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100844 if (rc < 0) {
845 dev_kfree_skb(resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200846 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200847 } else {
848 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100849 }
850
851 return rc;
852}
853
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +0200854static void pn533_wq_cmd_complete(struct work_struct *work)
855{
856 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
857 int rc;
858
859 rc = pn533_send_async_complete(dev);
860 if (rc != -EINPROGRESS)
861 queue_work(dev->wq, &dev->cmd_work);
862}
863
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200864static void pn533_wq_cmd(struct work_struct *work)
865{
866 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
867 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200868 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200869
870 mutex_lock(&dev->cmd_lock);
871
872 if (list_empty(&dev->cmd_queue)) {
873 dev->cmd_pending = 0;
874 mutex_unlock(&dev->cmd_lock);
875 return;
876 }
877
878 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
879
Szymon Janc60ad07a2012-10-25 17:29:45 +0200880 list_del(&cmd->queue);
881
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200882 mutex_unlock(&dev->cmd_lock);
883
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200884 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200885 if (rc < 0) {
886 dev_kfree_skb(cmd->req);
887 dev_kfree_skb(cmd->resp);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200888 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200889 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200890 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200891
892 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200893}
894
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300895struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100896 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300897 struct completion done;
898};
899
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100900static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
901 struct sk_buff *resp)
902{
903 struct pn533_sync_cmd_response *arg = _arg;
904
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100905 arg->resp = resp;
906 complete(&arg->done);
907
908 return 0;
909}
910
911/* pn533_send_cmd_sync
912 *
913 * Please note the req parameter is freed inside the function to
914 * limit a number of return value interpretations by the caller.
915 *
916 * 1. negative in case of error during TX path -> req should be freed
917 *
918 * 2. negative in case of error during RX path -> req should not be freed
919 * as it's been already freed at the begining of RX path by
920 * async_complete_cb.
921 *
922 * 3. valid pointer in case of succesfult RX path
923 *
924 * A caller has to check a return value with IS_ERR macro. If the test pass,
925 * the returned pointer is valid.
926 *
927 * */
928static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
929 struct sk_buff *req)
930{
931 int rc;
932 struct pn533_sync_cmd_response arg;
933
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100934 init_completion(&arg.done);
935
936 rc = pn533_send_cmd_async(dev, cmd_code, req,
937 pn533_send_sync_complete, &arg);
938 if (rc) {
939 dev_kfree_skb(req);
940 return ERR_PTR(rc);
941 }
942
943 wait_for_completion(&arg.done);
944
945 return arg.resp;
946}
947
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300948static void pn533_send_complete(struct urb *urb)
949{
950 struct pn533 *dev = urb->context;
951
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300952 switch (urb->status) {
953 case 0:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100954 break; /* success */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300955 case -ECONNRESET:
956 case -ENOENT:
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +0100957 nfc_dev_dbg(&dev->interface->dev,
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100958 "The urb has been stopped (status %d)",
959 urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300960 break;
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100961 case -ESHUTDOWN:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300962 default:
Waldemar Rymarkiewiczf8f99172012-12-10 14:42:54 +0100963 nfc_dev_err(&dev->interface->dev,
964 "Urb failure (status %d)", urb->status);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300965 }
966}
967
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100968static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100969{
970 struct sk_buff *skb;
971
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100972 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100973 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100974 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100975
976 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100977 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100978
979 return skb;
980}
981
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300982struct pn533_target_type_a {
983 __be16 sens_res;
984 u8 sel_res;
985 u8 nfcid_len;
986 u8 nfcid_data[];
987} __packed;
988
989
990#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
991#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
992#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
993
994#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
995#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
996
997#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
998#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
999
1000#define PN533_TYPE_A_SEL_PROT_MIFARE 0
1001#define PN533_TYPE_A_SEL_PROT_ISO14443 1
1002#define PN533_TYPE_A_SEL_PROT_DEP 2
1003#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1004
1005static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1006 int target_data_len)
1007{
1008 u8 ssd;
1009 u8 platconf;
1010
1011 if (target_data_len < sizeof(struct pn533_target_type_a))
1012 return false;
1013
1014 /* The lenght check of nfcid[] and ats[] are not being performed because
1015 the values are not being used */
1016
1017 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1018 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1019 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1020
1021 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001022 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1023 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1024 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001025 return false;
1026
1027 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1028 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1029 return false;
1030
1031 return true;
1032}
1033
1034static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1035 int tgt_data_len)
1036{
1037 struct pn533_target_type_a *tgt_type_a;
1038
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001039 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001040
1041 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1042 return -EPROTO;
1043
1044 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1045 case PN533_TYPE_A_SEL_PROT_MIFARE:
1046 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1047 break;
1048 case PN533_TYPE_A_SEL_PROT_ISO14443:
1049 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1050 break;
1051 case PN533_TYPE_A_SEL_PROT_DEP:
1052 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1053 break;
1054 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1055 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1056 NFC_PROTO_NFC_DEP_MASK;
1057 break;
1058 }
1059
1060 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1061 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +01001062 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1063 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001064
1065 return 0;
1066}
1067
1068struct pn533_target_felica {
1069 u8 pol_res;
1070 u8 opcode;
1071 u8 nfcid2[8];
1072 u8 pad[8];
1073 /* optional */
1074 u8 syst_code[];
1075} __packed;
1076
1077#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1078#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1079
1080static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1081 int target_data_len)
1082{
1083 if (target_data_len < sizeof(struct pn533_target_felica))
1084 return false;
1085
1086 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1087 return false;
1088
1089 return true;
1090}
1091
1092static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1093 int tgt_data_len)
1094{
1095 struct pn533_target_felica *tgt_felica;
1096
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001097 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001098
1099 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1100 return -EPROTO;
1101
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001102 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1103 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001104 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1105 else
1106 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1107
Samuel Ortiz79757542012-03-05 01:03:45 +01001108 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1109 nfc_tgt->sensf_res_len = 9;
1110
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001111 return 0;
1112}
1113
1114struct pn533_target_jewel {
1115 __be16 sens_res;
1116 u8 jewelid[4];
1117} __packed;
1118
1119static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1120 int target_data_len)
1121{
1122 u8 ssd;
1123 u8 platconf;
1124
1125 if (target_data_len < sizeof(struct pn533_target_jewel))
1126 return false;
1127
1128 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1129 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1130 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1131
1132 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001133 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1134 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1135 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001136 return false;
1137
1138 return true;
1139}
1140
1141static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1142 int tgt_data_len)
1143{
1144 struct pn533_target_jewel *tgt_jewel;
1145
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001146 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001147
1148 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1149 return -EPROTO;
1150
1151 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1152 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +01001153 nfc_tgt->nfcid1_len = 4;
1154 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001155
1156 return 0;
1157}
1158
1159struct pn533_type_b_prot_info {
1160 u8 bitrate;
1161 u8 fsci_type;
1162 u8 fwi_adc_fo;
1163} __packed;
1164
1165#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1166#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1167#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1168
1169struct pn533_type_b_sens_res {
1170 u8 opcode;
1171 u8 nfcid[4];
1172 u8 appdata[4];
1173 struct pn533_type_b_prot_info prot_info;
1174} __packed;
1175
1176#define PN533_TYPE_B_OPC_SENSB_RES 0x50
1177
1178struct pn533_target_type_b {
1179 struct pn533_type_b_sens_res sensb_res;
1180 u8 attrib_res_len;
1181 u8 attrib_res[];
1182} __packed;
1183
1184static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1185 int target_data_len)
1186{
1187 if (target_data_len < sizeof(struct pn533_target_type_b))
1188 return false;
1189
1190 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1191 return false;
1192
1193 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1194 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1195 return false;
1196
1197 return true;
1198}
1199
1200static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1201 int tgt_data_len)
1202{
1203 struct pn533_target_type_b *tgt_type_b;
1204
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001205 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001206
1207 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1208 return -EPROTO;
1209
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001210 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001211
1212 return 0;
1213}
1214
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001215static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1216 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001217{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001218 struct nfc_target nfc_tgt;
1219 int rc;
1220
1221 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001222 dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001223
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001224 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001225 return -EPROTO;
1226
Samuel Ortiz98b3ac12012-03-05 01:03:39 +01001227 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1228
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001229 switch (dev->poll_mod_curr) {
1230 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001231 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001232 break;
1233 case PN533_POLL_MOD_212KBPS_FELICA:
1234 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001235 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001236 break;
1237 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001238 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001239 break;
1240 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001241 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001242 break;
1243 default:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001244 nfc_dev_err(&dev->interface->dev,
1245 "Unknown current poll modulation");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001246 return -EPROTO;
1247 }
1248
1249 if (rc)
1250 return rc;
1251
1252 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001253 nfc_dev_dbg(&dev->interface->dev,
1254 "The Tg found doesn't have the desired protocol");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001255 return -EAGAIN;
1256 }
1257
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001258 nfc_dev_dbg(&dev->interface->dev,
1259 "Target found - supported protocols: 0x%x",
1260 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001261
1262 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1263
1264 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1265
1266 return 0;
1267}
1268
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001269static inline void pn533_poll_next_mod(struct pn533 *dev)
1270{
1271 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1272}
1273
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001274static void pn533_poll_reset_mod_list(struct pn533 *dev)
1275{
1276 dev->poll_mod_count = 0;
1277}
1278
1279static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1280{
1281 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001282 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001283 dev->poll_mod_count++;
1284}
1285
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001286static void pn533_poll_create_mod_list(struct pn533 *dev,
1287 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001288{
1289 pn533_poll_reset_mod_list(dev);
1290
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001291 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1292 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1293 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001294 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1295
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +01001296 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1297 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001298 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1299 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1300 }
1301
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001302 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001303 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1304
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001305 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001306 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001307
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001308 if (tm_protocols)
1309 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001310}
1311
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001312static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001313{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001314 u8 nbtg, tg, *tgdata;
1315 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001316
1317 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1318
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001319 nbtg = resp->data[0];
1320 tg = resp->data[1];
1321 tgdata = &resp->data[2];
1322 tgdata_len = resp->len - 2; /* nbtg + tg */
1323
1324 if (nbtg) {
1325 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001326
1327 /* We must stop the poll after a valid target found */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001328 if (rc == 0) {
1329 pn533_poll_reset_mod_list(dev);
1330 return 0;
1331 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001332 }
1333
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001334 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001335}
1336
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001337static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001338{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001339 struct sk_buff *skb;
1340 u8 *felica, *nfcid3, *gb;
1341
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001342 u8 *gbytes = dev->gb;
1343 size_t gbytes_len = dev->gb_len;
1344
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001345 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1346 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1347 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1348 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001349
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001350 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1351 0x0, 0x0, 0x0,
1352 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001353
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001354 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1355 felica (18), nfcid3 (10), gb_len (1) */
1356 gbytes_len +
1357 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001358
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001359 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001360 if (!skb)
1361 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001362
1363 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001364 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001365
1366 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001367 memcpy(skb_put(skb, 6), mifare_params, 6);
1368
1369 /* Felica params */
1370 felica = skb_put(skb, 18);
1371 memcpy(felica, felica_params, 18);
1372 get_random_bytes(felica + 2, 6);
1373
1374 /* NFCID3 */
1375 nfcid3 = skb_put(skb, 10);
1376 memset(nfcid3, 0, 10);
1377 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001378
1379 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001380 *skb_put(skb, 1) = gbytes_len;
1381
1382 gb = skb_put(skb, gbytes_len);
1383 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001384
Samuel Ortizad3823c2012-05-30 23:54:55 +02001385 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001386 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001387
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001388 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001389}
1390
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01001391#define PN533_CMD_DATAEXCH_HEAD_LEN 1
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001392#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1393static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001394 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001395{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001396 u8 status;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001397
1398 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1399
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001400 if (IS_ERR(resp))
1401 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001402
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001403 status = resp->data[0];
1404 skb_pull(resp, sizeof(status));
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001405
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001406 if (status != 0) {
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001407 nfc_tm_deactivated(dev->nfc_dev);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001408 dev->tgt_mode = 0;
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001409 dev_kfree_skb(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001410 return 0;
1411 }
1412
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001413 return nfc_tm_data_received(dev->nfc_dev, resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001414}
1415
1416static void pn533_wq_tg_get_data(struct work_struct *work)
1417{
1418 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001419
1420 struct sk_buff *skb;
1421 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001422
1423 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1424
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001425 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001426 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001427 return;
1428
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001429 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1430 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001431
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001432 if (rc < 0)
1433 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001434
1435 return;
1436}
1437
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001438#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001439static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001440{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001441 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001442 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001443 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001444
1445 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1446
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001447 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001448 return -EINVAL;
1449
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450 mode = resp->data[0];
1451 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001452
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001453 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1454 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001455
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1457 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001458 comm_mode = NFC_COMM_ACTIVE;
1459
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001460 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001461 return -EOPNOTSUPP;
1462
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001463 gb = cmd + ATR_REQ_GB_OFFSET;
1464 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001465
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001466 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1467 comm_mode, gb, gb_len);
1468 if (rc < 0) {
1469 nfc_dev_err(&dev->interface->dev,
1470 "Error when signaling target activation");
1471 return rc;
1472 }
1473
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001474 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001475 queue_work(dev->wq, &dev->tg_work);
1476
1477 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001478}
1479
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001480static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001481{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001482 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001483
1484 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1485
1486 /* An ack will cancel the last issued command (poll) */
1487 pn533_send_ack(dev, GFP_ATOMIC);
1488
1489 dev->cancel_listen = 1;
1490
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001491 pn533_poll_next_mod(dev);
1492
1493 queue_work(dev->wq, &dev->poll_work);
1494}
1495
1496static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001497 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001498{
1499 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001500 int rc;
1501
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001502 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1503
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001504 if (IS_ERR(resp)) {
1505 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001506
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001507 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1508 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001509
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001510 if (rc == -ENOENT) {
1511 if (dev->poll_mod_count != 0)
1512 return rc;
1513 else
1514 goto stop_poll;
1515 } else if (rc < 0) {
1516 nfc_dev_err(&dev->interface->dev,
1517 "Error %d when running poll", rc);
1518 goto stop_poll;
1519 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001520 }
1521
1522 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1523
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001524 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001525 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001526 rc = pn533_init_target_complete(dev, resp);
1527 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528 }
1529
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001530 /* Initiator mode */
1531 rc = pn533_start_poll_complete(dev, resp);
1532 if (!rc)
1533 goto done;
1534
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001535 if (!dev->poll_mod_count) {
1536 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1537 goto done;
1538 }
1539
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001540 pn533_poll_next_mod(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001541 queue_work(dev->wq, &dev->poll_work);
1542
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001543done:
1544 dev_kfree_skb(resp);
1545 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001546
1547stop_poll:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001548 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1549
Samuel Ortizad3823c2012-05-30 23:54:55 +02001550 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001551 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001552 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001553}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001554
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001555static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1556 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001557{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001558 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001559
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001560 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001561 if (!skb)
1562 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001563
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001564 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001565
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001566 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001567}
1568
1569static int pn533_send_poll_frame(struct pn533 *dev)
1570{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001571 struct pn533_poll_modulations *mod;
1572 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001573 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001574 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001575
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001576 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001577
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001578 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1579 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001580
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001581 if (mod->len == 0) { /* Listen mode */
1582 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001583 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001584 } else { /* Polling mode */
1585 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001586 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001587 }
1588
1589 if (!skb) {
1590 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1591 return -ENOMEM;
1592 }
1593
1594 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1595 NULL);
1596 if (rc < 0) {
1597 dev_kfree_skb(skb);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001598 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001599 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001600
1601 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001602}
1603
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001604static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001605{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001606 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1607 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001608 int rc;
1609
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001610 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1611
1612 nfc_dev_dbg(&dev->interface->dev,
1613 "%s cancel_listen %d modulation len %d",
1614 __func__, dev->cancel_listen, cur_mod->len);
1615
1616 if (dev->cancel_listen == 1) {
1617 dev->cancel_listen = 0;
1618 usb_kill_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001619 }
1620
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001621 rc = pn533_send_poll_frame(dev);
1622 if (rc)
1623 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001624
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001625 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1626 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001627
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001628 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001629}
1630
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001631static int pn533_start_poll(struct nfc_dev *nfc_dev,
1632 u32 im_protocols, u32 tm_protocols)
1633{
1634 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1635
1636 nfc_dev_dbg(&dev->interface->dev,
1637 "%s: im protocols 0x%x tm protocols 0x%x",
1638 __func__, im_protocols, tm_protocols);
1639
1640 if (dev->tgt_active_prot) {
1641 nfc_dev_err(&dev->interface->dev,
1642 "Cannot poll with a target already activated");
1643 return -EBUSY;
1644 }
1645
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001646 if (dev->tgt_mode) {
1647 nfc_dev_err(&dev->interface->dev,
1648 "Cannot poll while already being activated");
1649 return -EBUSY;
1650 }
1651
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001652 if (tm_protocols) {
1653 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1654 if (dev->gb == NULL)
1655 tm_protocols = 0;
1656 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001657
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001658 dev->poll_mod_curr = 0;
1659 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1660 dev->poll_protocols = im_protocols;
1661 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001662
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001663 return pn533_send_poll_frame(dev);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001664}
1665
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001666static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1667{
1668 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1669
1670 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1671
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001672 del_timer(&dev->listen_timer);
1673
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001674 if (!dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001675 nfc_dev_dbg(&dev->interface->dev,
1676 "Polling operation was not running");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001677 return;
1678 }
1679
1680 /* An ack will cancel the last issued command (poll) */
1681 pn533_send_ack(dev, GFP_KERNEL);
1682
1683 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1684 usb_kill_urb(dev->in_urb);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001685
1686 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001687}
1688
1689static int pn533_activate_target_nfcdep(struct pn533 *dev)
1690{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001691 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001692 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001693 int rc;
1694
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001695 struct sk_buff *skb;
1696 struct sk_buff *resp;
1697
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001698 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1699
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001700 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001701 if (!skb)
1702 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001703
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001704 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1705 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001706
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001707 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1708 if (IS_ERR(resp))
1709 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001710
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001711 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001712 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001713 if (rc != PN533_CMD_RET_SUCCESS) {
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001714 nfc_dev_err(&dev->interface->dev,
1715 "Target activation failed (error 0x%x)", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001716 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001718 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001719
Samuel Ortiz541d9202011-12-14 16:43:10 +01001720 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001721 gt_len = resp->len - 16;
1722 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001723
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001724 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001725 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001726}
1727
Eric Lapuyade90099432012-05-07 12:31:13 +02001728static int pn533_activate_target(struct nfc_dev *nfc_dev,
1729 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001730{
1731 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1732 int rc;
1733
1734 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001735 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001736
1737 if (dev->poll_mod_count) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001738 nfc_dev_err(&dev->interface->dev,
1739 "Cannot activate while polling");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001740 return -EBUSY;
1741 }
1742
1743 if (dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001744 nfc_dev_err(&dev->interface->dev,
1745 "There is already an active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001746 return -EBUSY;
1747 }
1748
1749 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001750 nfc_dev_err(&dev->interface->dev,
1751 "There is no available target to activate");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001752 return -EINVAL;
1753 }
1754
1755 if (!(dev->tgt_available_prots & (1 << protocol))) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001756 nfc_dev_err(&dev->interface->dev,
1757 "Target doesn't support requested proto %u",
1758 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001759 return -EINVAL;
1760 }
1761
1762 if (protocol == NFC_PROTO_NFC_DEP) {
1763 rc = pn533_activate_target_nfcdep(dev);
1764 if (rc) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001765 nfc_dev_err(&dev->interface->dev,
1766 "Activating target with DEP failed %d", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001767 return rc;
1768 }
1769 }
1770
1771 dev->tgt_active_prot = protocol;
1772 dev->tgt_available_prots = 0;
1773
1774 return 0;
1775}
1776
Eric Lapuyade90099432012-05-07 12:31:13 +02001777static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1778 struct nfc_target *target)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001779{
1780 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001781
1782 struct sk_buff *skb;
1783 struct sk_buff *resp;
1784
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001785 int rc;
1786
1787 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1788
1789 if (!dev->tgt_active_prot) {
1790 nfc_dev_err(&dev->interface->dev, "There is no active target");
1791 return;
1792 }
1793
1794 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001795 skb_queue_purge(&dev->resp_q);
1796
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001797 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001798 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001799 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001800
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001801 *skb_put(skb, 1) = 1; /* TG*/
1802
1803 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1804 if (IS_ERR(resp))
1805 return;
1806
1807 rc = resp->data[0] & PN533_CMD_RET_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001808 if (rc != PN533_CMD_RET_SUCCESS)
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01001809 nfc_dev_err(&dev->interface->dev,
1810 "Error 0x%x when releasing the target", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001811
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001812 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001813 return;
1814}
1815
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001816
1817static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001818 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001819{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001820 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001821 u8 target_gt_len;
1822 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001823 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001824
1825 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001826
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001827 if (IS_ERR(resp))
1828 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001829
1830 if (dev->tgt_available_prots &&
1831 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1832 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001833 "The target does not support DEP");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001834 rc = -EINVAL;
1835 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001836 }
1837
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1839
1840 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001841 if (rc != PN533_CMD_RET_SUCCESS) {
1842 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02001843 "Bringing DEP link up failed (error 0x%x)", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001844 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001845 }
1846
1847 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001848 struct nfc_target nfc_target;
1849
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001850 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1851
1852 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001853 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001854 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001855 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1856 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001857 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001858
1859 dev->tgt_available_prots = 0;
1860 }
1861
1862 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1863
1864 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001865 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001866 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001867 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001868 if (rc == 0)
1869 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001870 dev->nfc_dev->targets[0].idx,
1871 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001872
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001873error:
1874 dev_kfree_skb(resp);
1875 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001876}
1877
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001878static int pn533_mod_to_baud(struct pn533 *dev)
1879{
1880 switch (dev->poll_mod_curr) {
1881 case PN533_POLL_MOD_106KBPS_A:
1882 return 0;
1883 case PN533_POLL_MOD_212KBPS_FELICA:
1884 return 1;
1885 case PN533_POLL_MOD_424KBPS_FELICA:
1886 return 2;
1887 default:
1888 return -EINVAL;
1889 }
1890}
1891
Samuel Ortizd7f33452012-05-29 21:45:21 +02001892#define PASSIVE_DATA_LEN 5
Eric Lapuyade90099432012-05-07 12:31:13 +02001893static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001894 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001895{
1896 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001897 struct sk_buff *skb;
1898 int rc, baud, skb_len;
1899 u8 *next, *arg;
1900
Samuel Ortizd7f33452012-05-29 21:45:21 +02001901 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001902
1903 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1904
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001905 if (dev->poll_mod_count) {
1906 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001907 "Cannot bring the DEP link up while polling");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001908 return -EBUSY;
1909 }
1910
1911 if (dev->tgt_active_prot) {
1912 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01001913 "There is already an active target");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001914 return -EBUSY;
1915 }
1916
Samuel Ortiz41a8ec42012-05-31 17:44:44 +02001917 baud = pn533_mod_to_baud(dev);
1918 if (baud < 0) {
1919 nfc_dev_err(&dev->interface->dev,
1920 "Invalid curr modulation %d", dev->poll_mod_curr);
1921 return baud;
1922 }
1923
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001924 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortizd7f33452012-05-29 21:45:21 +02001925 if (comm_mode == NFC_COMM_PASSIVE)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001926 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001927
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001928 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001929 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001930 return -ENOMEM;
1931
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001932 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1933 *skb_put(skb, 1) = baud; /* Baud rate */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001934
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001935 next = skb_put(skb, 1); /* Next */
1936 *next = 0;
1937
1938 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1939 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1940 PASSIVE_DATA_LEN);
1941 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001942 }
1943
Samuel Ortiz47807d32012-03-05 01:03:50 +01001944 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001945 memcpy(skb_put(skb, gb_len), gb, gb_len);
1946 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001947 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001948 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001949 }
1950
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001951 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1952 if (!arg) {
1953 dev_kfree_skb(skb);
1954 return -ENOMEM;
1955 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001956
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001957 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001958
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001959 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1960 pn533_in_dep_link_up_complete, arg);
1961
1962 if (rc < 0) {
1963 dev_kfree_skb(skb);
1964 kfree(arg);
1965 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001966
1967 return rc;
1968}
1969
1970static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1971{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001972 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1973
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001974 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1975
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001976 pn533_poll_reset_mod_list(dev);
1977
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001978 if (dev->tgt_mode || dev->tgt_active_prot) {
1979 pn533_send_ack(dev, GFP_KERNEL);
1980 usb_kill_urb(dev->in_urb);
1981 }
1982
1983 dev->tgt_active_prot = 0;
1984 dev->tgt_mode = 0;
1985
1986 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001987
1988 return 0;
1989}
1990
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001991struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001992 data_exchange_cb_t cb;
1993 void *cb_context;
1994};
1995
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001996static struct sk_buff *pn533_build_response(struct pn533 *dev)
1997{
1998 struct sk_buff *skb, *tmp, *t;
1999 unsigned int skb_len = 0, tmp_len = 0;
2000
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01002001 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002002
2003 if (skb_queue_empty(&dev->resp_q))
2004 return NULL;
2005
2006 if (skb_queue_len(&dev->resp_q) == 1) {
2007 skb = skb_dequeue(&dev->resp_q);
2008 goto out;
2009 }
2010
2011 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2012 skb_len += tmp->len;
2013
2014 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2015 __func__, skb_len);
2016
2017 skb = alloc_skb(skb_len, GFP_KERNEL);
2018 if (skb == NULL)
2019 goto out;
2020
2021 skb_put(skb, skb_len);
2022
2023 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2024 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2025 tmp_len += tmp->len;
2026 }
2027
2028out:
2029 skb_queue_purge(&dev->resp_q);
2030
2031 return skb;
2032}
2033
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002034static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002035 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002036{
2037 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002038 struct sk_buff *skb;
2039 int rc = 0;
2040 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002041
2042 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2043
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002044 if (IS_ERR(resp)) {
2045 rc = PTR_ERR(resp);
2046 goto _error;
2047 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002048
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002049 status = resp->data[0];
2050 ret = status & PN533_CMD_RET_MASK;
2051 mi = status & PN533_CMD_MI_MASK;
2052
2053 skb_pull(resp, sizeof(status));
2054
2055 if (ret != PN533_CMD_RET_SUCCESS) {
2056 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicza45e1c82013-04-03 08:01:59 +02002057 "Exchanging data failed (error 0x%x)", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002058 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002059 goto error;
2060 }
2061
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002062 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002063
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002064 if (mi) {
2065 dev->cmd_complete_mi_arg = arg;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002066 queue_work(dev->wq, &dev->mi_work);
2067 return -EINPROGRESS;
2068 }
2069
2070 skb = pn533_build_response(dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002071 if (!skb)
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002072 goto error;
2073
2074 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002075 kfree(arg);
2076 return 0;
2077
2078error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002079 dev_kfree_skb(resp);
2080_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002081 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002082 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002083 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002084 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002085}
2086
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002087static int pn533_transceive(struct nfc_dev *nfc_dev,
2088 struct nfc_target *target, struct sk_buff *skb,
2089 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002090{
2091 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002092 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002093 int rc;
2094
2095 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2096
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002097 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2098 /* TODO: Implement support to multi-part data exchange */
2099 nfc_dev_err(&dev->interface->dev,
2100 "Data length greater than the max allowed: %d",
2101 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2102 rc = -ENOSYS;
2103 goto error;
2104 }
2105
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002106 if (!dev->tgt_active_prot) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002107 nfc_dev_err(&dev->interface->dev,
2108 "Can't exchange data if there is no active target");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002109 rc = -EINVAL;
2110 goto error;
2111 }
2112
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002113 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002114 if (!arg) {
2115 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002116 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002117 }
2118
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002119 arg->cb = cb;
2120 arg->cb_context = cb_context;
2121
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002122 switch (dev->device_type) {
2123 case PN533_DEVICE_PASORI:
2124 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2125 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2126 skb,
2127 pn533_data_exchange_complete,
2128 arg);
2129
2130 break;
2131 }
2132 default:
2133 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2134
2135 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2136 skb, pn533_data_exchange_complete,
2137 arg);
2138
2139 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002140 }
2141
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002142 if (rc < 0) /* rc from send_async */
2143 goto error;
2144
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002145 return 0;
2146
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002147error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002148 kfree(arg);
2149 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002150 return rc;
2151}
2152
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002153static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002154 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002155{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002156 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002157
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002158 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2159
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002160 if (IS_ERR(resp))
2161 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002162
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002163 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002164
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002165 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002166
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002167 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002168 nfc_tm_deactivated(dev->nfc_dev);
2169
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002170 dev->tgt_mode = 0;
2171
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002172 return 0;
2173 }
2174
2175 queue_work(dev->wq, &dev->tg_work);
2176
2177 return 0;
2178}
2179
2180static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2181{
2182 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002183 int rc;
2184
2185 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2186
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002187 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002188 nfc_dev_err(&dev->interface->dev,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002189 "Data length greater than the max allowed: %d",
2190 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2191 return -ENOSYS;
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002192 }
2193
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002194 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2195 pn533_tm_send_complete, NULL);
2196 if (rc < 0)
2197 dev_kfree_skb(skb);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002198
2199 return rc;
2200}
2201
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002202static void pn533_wq_mi_recv(struct work_struct *work)
2203{
2204 struct pn533 *dev = container_of(work, struct pn533, mi_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002205
2206 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002207 int rc;
2208
2209 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2210
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002211 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002212 if (!skb)
2213 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002214
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002215 switch (dev->device_type) {
2216 case PN533_DEVICE_PASORI:
2217 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2218 rc = pn533_send_cmd_direct_async(dev,
2219 PN533_CMD_IN_COMM_THRU,
2220 skb,
2221 pn533_data_exchange_complete,
2222 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002223
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002224 break;
2225 }
2226 default:
2227 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002228
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002229 rc = pn533_send_cmd_direct_async(dev,
2230 PN533_CMD_IN_DATA_EXCHANGE,
2231 skb,
2232 pn533_data_exchange_complete,
2233 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002234
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002235 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002236 }
2237
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002238 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002239 return;
2240
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002241 nfc_dev_err(&dev->interface->dev,
2242 "Error %d when trying to perform data_exchange", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002243
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002244 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002245 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002246
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002247error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002248 pn533_send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002249 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002250}
2251
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002252static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2253 u8 cfgdata_len)
2254{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002255 struct sk_buff *skb;
2256 struct sk_buff *resp;
2257
2258 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002259
2260 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2261
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002262 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002263
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002264 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002265 if (!skb)
2266 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002267
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002268 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2269 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002270
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002271 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2272 if (IS_ERR(resp))
2273 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002274
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002275 dev_kfree_skb(resp);
2276 return 0;
2277}
2278
2279static int pn533_get_firmware_version(struct pn533 *dev,
2280 struct pn533_fw_version *fv)
2281{
2282 struct sk_buff *skb;
2283 struct sk_buff *resp;
2284
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002285 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002286 if (!skb)
2287 return -ENOMEM;
2288
2289 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2290 if (IS_ERR(resp))
2291 return PTR_ERR(resp);
2292
2293 fv->ic = resp->data[0];
2294 fv->ver = resp->data[1];
2295 fv->rev = resp->data[2];
2296 fv->support = resp->data[3];
2297
2298 dev_kfree_skb(resp);
2299 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002300}
2301
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002302static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002303{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002304 struct sk_buff *skb;
2305 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002306
2307 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2308
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002309 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002310 if (!skb)
2311 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002312
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002313 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002314
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002315 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2316 if (IS_ERR(resp))
2317 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002318
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002319 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002320
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002321 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002322}
2323
2324static struct nfc_ops pn533_nfc_ops = {
Ilan Elias8b3fe7b2011-09-18 11:19:33 +03002325 .dev_up = NULL,
2326 .dev_down = NULL,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002327 .dep_link_up = pn533_dep_link_up,
2328 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002329 .start_poll = pn533_start_poll,
2330 .stop_poll = pn533_stop_poll,
2331 .activate_target = pn533_activate_target,
2332 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002333 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002334 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002335};
2336
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002337static int pn533_setup(struct pn533 *dev)
2338{
2339 struct pn533_config_max_retries max_retries;
2340 struct pn533_config_timing timing;
2341 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2342 int rc;
2343
2344 switch (dev->device_type) {
2345 case PN533_DEVICE_STD:
2346 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2347 max_retries.mx_rty_psl = 2;
2348 max_retries.mx_rty_passive_act =
2349 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2350
2351 timing.rfu = PN533_CONFIG_TIMING_102;
2352 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2353 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2354
2355 break;
2356
2357 case PN533_DEVICE_PASORI:
2358 max_retries.mx_rty_atr = 0x2;
2359 max_retries.mx_rty_psl = 0x1;
2360 max_retries.mx_rty_passive_act =
2361 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2362
2363 timing.rfu = PN533_CONFIG_TIMING_102;
2364 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2365 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2366
2367 break;
2368
2369 default:
2370 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2371 dev->device_type);
2372 return -EINVAL;
2373 }
2374
2375 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2376 (u8 *)&max_retries, sizeof(max_retries));
2377 if (rc) {
2378 nfc_dev_err(&dev->interface->dev,
2379 "Error on setting MAX_RETRIES config");
2380 return rc;
2381 }
2382
2383
2384 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2385 (u8 *)&timing, sizeof(timing));
2386 if (rc) {
2387 nfc_dev_err(&dev->interface->dev,
2388 "Error on setting RF timings");
2389 return rc;
2390 }
2391
2392 switch (dev->device_type) {
2393 case PN533_DEVICE_STD:
2394 break;
2395
2396 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002397 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002398
2399 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2400 pasori_cfg, 3);
2401 if (rc) {
2402 nfc_dev_err(&dev->interface->dev,
2403 "Error while settings PASORI config");
2404 return rc;
2405 }
2406
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002407 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002408
2409 break;
2410 }
2411
2412 return 0;
2413}
2414
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002415static int pn533_probe(struct usb_interface *interface,
2416 const struct usb_device_id *id)
2417{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002418 struct pn533_fw_version fw_ver;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002419 struct pn533 *dev;
2420 struct usb_host_interface *iface_desc;
2421 struct usb_endpoint_descriptor *endpoint;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002422 int in_endpoint = 0;
2423 int out_endpoint = 0;
2424 int rc = -ENOMEM;
2425 int i;
2426 u32 protocols;
2427
2428 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2429 if (!dev)
2430 return -ENOMEM;
2431
2432 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2433 dev->interface = interface;
Samuel Ortiz0201ed02012-05-31 17:56:46 +02002434 mutex_init(&dev->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002435
2436 iface_desc = interface->cur_altsetting;
2437 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2438 endpoint = &iface_desc->endpoint[i].desc;
2439
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002440 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002441 in_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002442
Waldemar Rymarkiewicz8d25ca72012-11-26 14:18:30 +01002443 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002444 out_endpoint = endpoint->bEndpointAddress;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002445 }
2446
2447 if (!in_endpoint || !out_endpoint) {
Waldemar Rymarkiewicz6ca55372012-12-10 14:42:49 +01002448 nfc_dev_err(&interface->dev,
2449 "Could not find bulk-in or bulk-out endpoint");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002450 rc = -ENODEV;
2451 goto error;
2452 }
2453
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002454 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002455 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2456
Waldemar Rymarkiewicza5798092012-12-10 14:42:58 +01002457 if (!dev->in_urb || !dev->out_urb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002458 goto error;
2459
2460 usb_fill_bulk_urb(dev->in_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002461 usb_rcvbulkpipe(dev->udev, in_endpoint),
2462 NULL, 0, NULL, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002463 usb_fill_bulk_urb(dev->out_urb, dev->udev,
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +01002464 usb_sndbulkpipe(dev->udev, out_endpoint),
2465 NULL, 0, pn533_send_complete, dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002466
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002467 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2468 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002469 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02002470 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002471 INIT_WORK(&dev->poll_work, pn533_wq_poll);
Tejun Heo58637c92012-08-22 16:28:46 -07002472 dev->wq = alloc_ordered_workqueue("pn533", 0);
Samuel Ortiz4849f852012-04-10 19:43:17 +02002473 if (dev->wq == NULL)
2474 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002475
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002476 init_timer(&dev->listen_timer);
2477 dev->listen_timer.data = (unsigned long) dev;
2478 dev->listen_timer.function = pn533_listen_mode_timer;
2479
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002480 skb_queue_head_init(&dev->resp_q);
2481
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002482 INIT_LIST_HEAD(&dev->cmd_queue);
2483
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002484 usb_set_intfdata(interface, dev);
2485
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002486 dev->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002488 dev->device_type = id->driver_info;
2489 switch (dev->device_type) {
2490 case PN533_DEVICE_STD:
2491 protocols = PN533_ALL_PROTOCOLS;
2492 break;
2493
2494 case PN533_DEVICE_PASORI:
2495 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2496 break;
2497
2498 default:
2499 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2500 dev->device_type);
2501 rc = -EINVAL;
2502 goto destroy_wq;
2503 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002504
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002505 memset(&fw_ver, 0, sizeof(fw_ver));
2506 rc = pn533_get_firmware_version(dev, &fw_ver);
2507 if (rc < 0)
2508 goto destroy_wq;
2509
2510 nfc_dev_info(&dev->interface->dev,
2511 "NXP PN533 firmware ver %d.%d now attached",
2512 fw_ver.ver, fw_ver.rev);
2513
2514
Samuel Ortize8753042011-08-19 15:47:11 +02002515 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +01002516 NFC_SE_NONE,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002517 dev->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002518 PN533_CMD_DATAEXCH_HEAD_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002519 dev->ops->tx_tail_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002520 if (!dev->nfc_dev)
Samuel Ortiz4849f852012-04-10 19:43:17 +02002521 goto destroy_wq;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002522
2523 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2524 nfc_set_drvdata(dev->nfc_dev, dev);
2525
2526 rc = nfc_register_device(dev->nfc_dev);
2527 if (rc)
2528 goto free_nfc_dev;
2529
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002530 rc = pn533_setup(dev);
2531 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002532 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002533
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002534 return 0;
2535
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002536unregister_nfc_dev:
2537 nfc_unregister_device(dev->nfc_dev);
2538
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002539free_nfc_dev:
2540 nfc_free_device(dev->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002541
Samuel Ortiz4849f852012-04-10 19:43:17 +02002542destroy_wq:
2543 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002544error:
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002545 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002546 usb_free_urb(dev->out_urb);
2547 kfree(dev);
2548 return rc;
2549}
2550
2551static void pn533_disconnect(struct usb_interface *interface)
2552{
2553 struct pn533 *dev;
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002554 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002555
2556 dev = usb_get_intfdata(interface);
2557 usb_set_intfdata(interface, NULL);
2558
2559 nfc_unregister_device(dev->nfc_dev);
2560 nfc_free_device(dev->nfc_dev);
2561
2562 usb_kill_urb(dev->in_urb);
2563 usb_kill_urb(dev->out_urb);
2564
Samuel Ortiz4849f852012-04-10 19:43:17 +02002565 destroy_workqueue(dev->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002566
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002567 skb_queue_purge(&dev->resp_q);
2568
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02002569 del_timer(&dev->listen_timer);
2570
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002571 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2572 list_del(&cmd->queue);
2573 kfree(cmd);
2574 }
2575
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002576 usb_free_urb(dev->in_urb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002577 usb_free_urb(dev->out_urb);
2578 kfree(dev);
2579
Dan Carpenter276556d2011-07-08 10:21:15 +03002580 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002581}
2582
2583static struct usb_driver pn533_driver = {
2584 .name = "pn533",
2585 .probe = pn533_probe,
2586 .disconnect = pn533_disconnect,
2587 .id_table = pn533_table,
2588};
2589
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -08002590module_usb_driver(pn533_driver);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002591
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002592MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2593MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2594MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002595MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2596MODULE_VERSION(VERSION);
2597MODULE_LICENSE("GPL");