pn533.c 75.9 KB
Newer Older
1 2
/*
 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3
 * Copyright (C) 2012-2013 Tieto Poland
4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 */

#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/nfc.h>
#include <linux/netdevice.h>
26
#include <net/nfc/nfc.h>
27

28
#define VERSION "0.2"
29 30 31 32 33 34 35

#define PN533_VENDOR_ID 0x4CC
#define PN533_PRODUCT_ID 0x2533

#define SCM_VENDOR_ID 0x4E6
#define SCL3711_PRODUCT_ID 0x5591

36 37 38
#define SONY_VENDOR_ID         0x054c
#define PASORI_PRODUCT_ID      0x02e1

39 40 41 42 43 44
#define ACS_VENDOR_ID 0x072f
#define ACR122U_PRODUCT_ID 0x2200

#define PN533_DEVICE_STD     0x1
#define PN533_DEVICE_PASORI  0x2
#define PN533_DEVICE_ACR122U 0x3
45

46 47 48 49
#define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
			     NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
			     NFC_PROTO_NFC_DEP_MASK |\
			     NFC_PROTO_ISO14443_B_MASK)
50 51 52 53

#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
				   NFC_PROTO_MIFARE_MASK | \
				   NFC_PROTO_FELICA_MASK | \
54
				   NFC_PROTO_ISO14443_MASK | \
55 56
				   NFC_PROTO_NFC_DEP_MASK)

57
static const struct usb_device_id pn533_table[] = {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE,
	  .idVendor		= PN533_VENDOR_ID,
	  .idProduct		= PN533_PRODUCT_ID,
	  .driver_info		= PN533_DEVICE_STD,
	},
	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE,
	  .idVendor		= SCM_VENDOR_ID,
	  .idProduct		= SCL3711_PRODUCT_ID,
	  .driver_info		= PN533_DEVICE_STD,
	},
	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE,
	  .idVendor		= SONY_VENDOR_ID,
	  .idProduct		= PASORI_PRODUCT_ID,
	  .driver_info		= PN533_DEVICE_PASORI,
	},
73 74 75 76 77
	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE,
	  .idVendor		= ACS_VENDOR_ID,
	  .idProduct		= ACR122U_PRODUCT_ID,
	  .driver_info		= PN533_DEVICE_ACR122U,
	},
78 79 80 81
	{ }
};
MODULE_DEVICE_TABLE(usb, pn533_table);

S
Samuel Ortiz 已提交
82 83
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2
84 85
/* Delay between each poll frame (ms) */
#define PN533_POLL_INTERVAL 10
S
Samuel Ortiz 已提交
86

87
/* Standard pn533 frame definitions (standard and extended)*/
88
#define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
89
					+ 2) /* data[0] TFI, data[1] CC */
90
#define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
91

92 93 94 95 96 97
#define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
					+ 2) /* data[0] TFI, data[1] CC */

#define PN533_CMD_DATAEXCH_DATA_MAXLEN	262
#define PN533_CMD_DATAFRAME_MAXLEN	240	/* max data length (send) */

98 99 100 101
/*
 * Max extended frame payload len, excluding TFI and CC
 * which are already in PN533_FRAME_HEADER_LEN.
 */
102
#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
103

104
#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
105
				  Postamble (1) */
106 107
#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
108 109 110 111
/* Half start code (3), LEN (4) should be 0xffff for extended frame */
#define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
					&& (hdr)->datalen_checksum == 0xFF)
#define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
112 113

/* start of frame */
114
#define PN533_STD_FRAME_SOF 0x00FF
115

116 117 118 119
/* standard frame identifier: in/out/error */
#define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
#define PN533_STD_FRAME_DIR_OUT 0xD4
#define PN533_STD_FRAME_DIR_IN 0xD5
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* ACS ACR122 pn533 frame definitions */
#define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
					  + 2)
#define PN533_ACR122_TX_FRAME_TAIL_LEN 0
#define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
					  + 2)
#define PN533_ACR122_RX_FRAME_TAIL_LEN 2
#define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN

/* CCID messages types */
#define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
#define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B

#define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83

136
/* PN533 Commands */
137
#define PN533_FRAME_CMD(f) (f->data[1])
138 139 140 141

#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
#define PN533_CMD_RF_CONFIGURATION 0x32
#define PN533_CMD_IN_DATA_EXCHANGE 0x40
142
#define PN533_CMD_IN_COMM_THRU     0x42
143 144 145
#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
#define PN533_CMD_IN_ATR 0x50
#define PN533_CMD_IN_RELEASE 0x52
146
#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
147

148
#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
149
#define PN533_CMD_TG_GET_DATA 0x86
150
#define PN533_CMD_TG_SET_DATA 0x8e
151
#define PN533_CMD_TG_SET_META_DATA 0x94
152
#define PN533_CMD_UNDEF 0xff
153

154 155 156 157 158 159 160 161 162
#define PN533_CMD_RESPONSE(cmd) (cmd + 1)

/* PN533 Return codes */
#define PN533_CMD_RET_MASK 0x3F
#define PN533_CMD_MI_MASK 0x40
#define PN533_CMD_RET_SUCCESS 0x00

struct pn533;

163 164 165
typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
					struct sk_buff *resp);

166 167 168 169 170 171 172 173 174 175 176
/* structs for pn533 commands */

/* PN533_CMD_GET_FIRMWARE_VERSION */
struct pn533_fw_version {
	u8 ic;
	u8 ver;
	u8 rev;
	u8 support;
};

/* PN533_CMD_RF_CONFIGURATION */
177 178
#define PN533_CFGITEM_RF_FIELD    0x01
#define PN533_CFGITEM_TIMING      0x02
179
#define PN533_CFGITEM_MAX_RETRIES 0x05
180 181
#define PN533_CFGITEM_PASORI      0x82

S
Samuel Ortiz 已提交
182 183 184
#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
#define PN533_CFGITEM_RF_FIELD_ON        0x1
#define PN533_CFGITEM_RF_FIELD_OFF       0x0
185

S
Samuel Ortiz 已提交
186 187 188 189 190
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe

191 192 193 194 195 196 197 198 199
#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF

struct pn533_config_max_retries {
	u8 mx_rty_atr;
	u8 mx_rty_psl;
	u8 mx_rty_passive_act;
} __packed;

S
Samuel Ortiz 已提交
200 201 202 203 204 205
struct pn533_config_timing {
	u8 rfu;
	u8 atr_res_timeout;
	u8 dep_timeout;
} __packed;

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/* PN533_CMD_IN_LIST_PASSIVE_TARGET */

/* felica commands opcode */
#define PN533_FELICA_OPC_SENSF_REQ 0
#define PN533_FELICA_OPC_SENSF_RES 1
/* felica SENSF_REQ parameters */
#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2

/* type B initiator_data values */
#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1

union pn533_cmd_poll_initdata {
	struct {
		u8 afi;
		u8 polling_method;
	} __packed type_b;
	struct {
		u8 opcode;
		__be16 sc;
		u8 rc;
		u8 tsn;
	} __packed felica;
};

/* Poll modulations */
enum {
	PN533_POLL_MOD_106KBPS_A,
	PN533_POLL_MOD_212KBPS_FELICA,
	PN533_POLL_MOD_424KBPS_FELICA,
	PN533_POLL_MOD_106KBPS_JEWEL,
	PN533_POLL_MOD_847KBPS_B,
S
Samuel Ortiz 已提交
242
	PN533_LISTEN_MOD,
243 244 245 246 247 248 249 250 251 252 253 254 255 256

	__PN533_POLL_MOD_AFTER_LAST,
};
#define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)

struct pn533_poll_modulations {
	struct {
		u8 maxtg;
		u8 brty;
		union pn533_cmd_poll_initdata initiator_data;
	} __packed data;
	u8 len;
};

257
static const struct pn533_poll_modulations poll_mod[] = {
258 259 260 261 262 263 264 265 266 267 268 269 270 271
	[PN533_POLL_MOD_106KBPS_A] = {
		.data = {
			.maxtg = 1,
			.brty = 0,
		},
		.len = 2,
	},
	[PN533_POLL_MOD_212KBPS_FELICA] = {
		.data = {
			.maxtg = 1,
			.brty = 1,
			.initiator_data.felica = {
				.opcode = PN533_FELICA_OPC_SENSF_REQ,
				.sc = PN533_FELICA_SENSF_SC_ALL,
272
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
273
				.tsn = 0x03,
274 275 276 277 278 279 280 281 282 283 284
			},
		},
		.len = 7,
	},
	[PN533_POLL_MOD_424KBPS_FELICA] = {
		.data = {
			.maxtg = 1,
			.brty = 2,
			.initiator_data.felica = {
				.opcode = PN533_FELICA_OPC_SENSF_REQ,
				.sc = PN533_FELICA_SENSF_SC_ALL,
285
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
286
				.tsn = 0x03,
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
			},
		 },
		.len = 7,
	},
	[PN533_POLL_MOD_106KBPS_JEWEL] = {
		.data = {
			.maxtg = 1,
			.brty = 4,
		},
		.len = 2,
	},
	[PN533_POLL_MOD_847KBPS_B] = {
		.data = {
			.maxtg = 1,
			.brty = 8,
			.initiator_data.type_b = {
				.afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
				.polling_method =
					PN533_TYPE_B_POLL_METHOD_TIMESLOT,
			},
		},
		.len = 3,
	},
S
Samuel Ortiz 已提交
310 311 312
	[PN533_LISTEN_MOD] = {
		.len = 0,
	},
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
};

/* PN533_CMD_IN_ATR */

struct pn533_cmd_activate_response {
	u8 status;
	u8 nfcid3t[10];
	u8 didt;
	u8 bst;
	u8 brt;
	u8 to;
	u8 ppt;
	/* optional */
	u8 gt[];
} __packed;

329 330 331 332 333 334 335 336 337 338 339 340
struct pn533_cmd_jump_dep_response {
	u8 status;
	u8 tg;
	u8 nfcid3t[10];
	u8 didt;
	u8 bst;
	u8 brt;
	u8 to;
	u8 ppt;
	/* optional */
	u8 gt[];
} __packed;
341

342 343 344 345 346

/* PN533_TG_INIT_AS_TARGET */
#define PN533_INIT_TARGET_PASSIVE 0x1
#define PN533_INIT_TARGET_DEP 0x2

347 348 349 350
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

351 352 353 354 355
enum  pn533_protocol_type {
	PN533_PROTO_REQ_ACK_RESP = 0,
	PN533_PROTO_REQ_RESP
};

356 357 358 359
struct pn533 {
	struct usb_device *udev;
	struct usb_interface *interface;
	struct nfc_dev *nfc_dev;
360
	u32 device_type;
361
	enum pn533_protocol_type protocol_type;
362 363 364 365

	struct urb *out_urb;
	struct urb *in_urb;

S
Samuel Ortiz 已提交
366
	struct sk_buff_head resp_q;
367
	struct sk_buff_head fragment_skb;
S
Samuel Ortiz 已提交
368

369 370
	struct workqueue_struct	*wq;
	struct work_struct cmd_work;
S
Samuel Ortiz 已提交
371
	struct work_struct cmd_complete_work;
372
	struct delayed_work poll_work;
373 374
	struct work_struct mi_rx_work;
	struct work_struct mi_tx_work;
375
	struct work_struct mi_tm_rx_work;
376
	struct work_struct mi_tm_tx_work;
377
	struct work_struct tg_work;
378
	struct work_struct rf_work;
379 380 381 382 383

	struct list_head cmd_queue;
	struct pn533_cmd *cmd;
	u8 cmd_pending;
	struct mutex cmd_lock;  /* protects cmd queue */
384

385
	void *cmd_complete_mi_arg;
386
	void *cmd_complete_dep_arg;
387 388 389 390

	struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
	u8 poll_mod_count;
	u8 poll_mod_curr;
391
	u8 poll_dep;
392
	u32 poll_protocols;
S
Samuel Ortiz 已提交
393
	u32 listen_protocols;
394 395
	struct timer_list listen_timer;
	int cancel_listen;
S
Samuel Ortiz 已提交
396 397 398

	u8 *gb;
	size_t gb_len;
399 400 401

	u8 tgt_available_prots;
	u8 tgt_active_prot;
S
Samuel Ortiz 已提交
402
	u8 tgt_mode;
403

404
	struct pn533_frame_ops *ops;
S
Samuel Ortiz 已提交
405 406 407 408
};

struct pn533_cmd {
	struct list_head queue;
409
	u8 code;
410
	int status;
411 412
	struct sk_buff *req;
	struct sk_buff *resp;
413
	int resp_len;
414 415
	pn533_send_async_complete_t  complete_cb;
	void *complete_cb_context;
416 417
};

418
struct pn533_std_frame {
419 420 421 422 423 424 425
	u8 preamble;
	__be16 start_frame;
	u8 datalen;
	u8 datalen_checksum;
	u8 data[];
} __packed;

426 427 428 429 430 431 432 433 434
struct pn533_ext_frame {	/* Extended Information frame */
	u8 preamble;
	__be16 start_frame;
	__be16 eif_flag;	/* fixed to 0xFFFF */
	__be16 datalen;
	u8 datalen_checksum;
	u8 data[];
} __packed;

435 436 437 438 439 440 441
struct pn533_frame_ops {
	void (*tx_frame_init)(void *frame, u8 cmd_code);
	void (*tx_frame_finish)(void *frame);
	void (*tx_update_payload_len)(void *frame, int len);
	int tx_header_len;
	int tx_tail_len;

442
	bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
443 444 445 446 447 448 449 450
	int (*rx_frame_size)(void *frame);
	int rx_header_len;
	int rx_tail_len;

	int max_payload_len;
	u8 (*get_cmd_code)(void *frame);
};

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
struct pn533_acr122_ccid_hdr {
	u8 type;
	u32 datalen;
	u8 slot;
	u8 seq;
	u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
			 byte for reposnse msg */
	u8 data[]; /* payload */
} __packed;

struct pn533_acr122_apdu_hdr {
	u8 class;
	u8 ins;
	u8 p1;
	u8 p2;
} __packed;

struct pn533_acr122_tx_frame {
	struct pn533_acr122_ccid_hdr ccid;
	struct pn533_acr122_apdu_hdr apdu;
	u8 datalen;
	u8 data[]; /* pn533 frame: TFI ... */
} __packed;

struct pn533_acr122_rx_frame {
	struct pn533_acr122_ccid_hdr ccid;
	u8 data[]; /* pn533 frame : TFI ... */
} __packed;

static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
{
	struct pn533_acr122_tx_frame *frame = _frame;

	frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
	frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
							  sizeof(datalen) */
	frame->ccid.slot = 0;
	frame->ccid.seq = 0;
	frame->ccid.params[0] = 0;
	frame->ccid.params[1] = 0;
	frame->ccid.params[2] = 0;

	frame->data[0] = PN533_STD_FRAME_DIR_OUT;
	frame->data[1] = cmd_code;
	frame->datalen = 2;  /* data[0] + data[1] */

	frame->apdu.class = 0xFF;
	frame->apdu.ins = 0;
	frame->apdu.p1 = 0;
	frame->apdu.p2 = 0;
}

static void pn533_acr122_tx_frame_finish(void *_frame)
{
	struct pn533_acr122_tx_frame *frame = _frame;

	frame->ccid.datalen += frame->datalen;
}

static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
{
	struct pn533_acr122_tx_frame *frame = _frame;

	frame->datalen += len;
}

517
static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	struct pn533_acr122_rx_frame *frame = _frame;

	if (frame->ccid.type != 0x83)
		return false;

	if (frame->data[frame->ccid.datalen - 2] == 0x63)
		return false;

	return true;
}

static int pn533_acr122_rx_frame_size(void *frame)
{
	struct pn533_acr122_rx_frame *f = frame;

	/* f->ccid.datalen already includes tail length */
	return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
}

static u8 pn533_acr122_get_cmd_code(void *frame)
{
	struct pn533_acr122_rx_frame *f = frame;

542
	return PN533_FRAME_CMD(f);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
}

static struct pn533_frame_ops pn533_acr122_frame_ops = {
	.tx_frame_init = pn533_acr122_tx_frame_init,
	.tx_frame_finish = pn533_acr122_tx_frame_finish,
	.tx_update_payload_len = pn533_acr122_tx_update_payload_len,
	.tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
	.tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,

	.rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
	.rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
	.rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
	.rx_frame_size = pn533_acr122_rx_frame_size,

	.max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
	.get_cmd_code = pn533_acr122_get_cmd_code,
};

561 562 563 564 565 566
/* The rule: value(high byte) + value(low byte) + checksum = 0 */
static inline u8 pn533_ext_checksum(u16 value)
{
	return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
}

567
/* The rule: value + checksum = 0 */
568
static inline u8 pn533_std_checksum(u8 value)
569 570 571 572 573
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
574
static u8 pn533_std_data_checksum(u8 *data, int datalen)
575 576 577 578 579 580 581
{
	u8 sum = 0;
	int i;

	for (i = 0; i < datalen; i++)
		sum += data[i];

582
	return pn533_std_checksum(sum);
583 584
}

585
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
586
{
587
	struct pn533_std_frame *frame = _frame;
588

589
	frame->preamble = 0;
590 591
	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
592
	PN533_FRAME_CMD(frame) = cmd_code;
593 594 595
	frame->datalen = 2;
}

596
static void pn533_std_tx_frame_finish(void *_frame)
597
{
598
	struct pn533_std_frame *frame = _frame;
599

600
	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
601

602 603
	PN533_STD_FRAME_CHECKSUM(frame) =
		pn533_std_data_checksum(frame->data, frame->datalen);
604

605
	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
606 607
}

608
static void pn533_std_tx_update_payload_len(void *_frame, int len)
609
{
610
	struct pn533_std_frame *frame = _frame;
611 612 613 614

	frame->datalen += len;
}

615
static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
616 617
{
	u8 checksum;
618
	struct pn533_std_frame *stdf = _frame;
619

620
	if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
621 622
		return false;

623 624
	if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
		/* Standard frame code */
625
		dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
626 627 628 629 630 631 632 633 634 635 636 637

		checksum = pn533_std_checksum(stdf->datalen);
		if (checksum != stdf->datalen_checksum)
			return false;

		checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
		if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
			return false;
	} else {
		/* Extended */
		struct pn533_ext_frame *eif = _frame;

638 639
		dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;

640 641 642 643 644 645 646 647 648 649
		checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
		if (checksum != eif->datalen_checksum)
			return false;

		/* check data checksum */
		checksum = pn533_std_data_checksum(eif->data,
						   be16_to_cpu(eif->datalen));
		if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
			return false;
	}
650 651 652 653

	return true;
}

654
static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
655
{
656
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
657 658 659 660 661 662 663 664
		return false;

	if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
		return false;

	return true;
}

665
static inline int pn533_std_rx_frame_size(void *frame)
666
{
667
	struct pn533_std_frame *f = frame;
668

669 670 671 672 673 674 675 676
	/* check for Extended Information frame */
	if (PN533_STD_IS_EXTENDED(f)) {
		struct pn533_ext_frame *eif = frame;

		return sizeof(struct pn533_ext_frame)
			+ be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
	}

677 678
	return sizeof(struct pn533_std_frame) + f->datalen +
	       PN533_STD_FRAME_TAIL_LEN;
679 680
}

681
static u8 pn533_std_get_cmd_code(void *frame)
682
{
683
	struct pn533_std_frame *f = frame;
684
	struct pn533_ext_frame *eif = frame;
685

686 687 688 689
	if (PN533_STD_IS_EXTENDED(f))
		return PN533_FRAME_CMD(eif);
	else
		return PN533_FRAME_CMD(f);
690 691
}

692
static struct pn533_frame_ops pn533_std_frame_ops = {
693 694 695 696 697 698 699 700 701 702 703 704 705
	.tx_frame_init = pn533_std_tx_frame_init,
	.tx_frame_finish = pn533_std_tx_frame_finish,
	.tx_update_payload_len = pn533_std_tx_update_payload_len,
	.tx_header_len = PN533_STD_FRAME_HEADER_LEN,
	.tx_tail_len = PN533_STD_FRAME_TAIL_LEN,

	.rx_is_frame_valid = pn533_std_rx_frame_is_valid,
	.rx_frame_size = pn533_std_rx_frame_size,
	.rx_header_len = PN533_STD_FRAME_HEADER_LEN,
	.rx_tail_len = PN533_STD_FRAME_TAIL_LEN,

	.max_payload_len =  PN533_STD_FRAME_MAX_PAYLOAD_LEN,
	.get_cmd_code = pn533_std_get_cmd_code,
706 707 708
};

static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
709
{
710
	return (dev->ops->get_cmd_code(frame) ==
711
				PN533_CMD_RESPONSE(dev->cmd->code));
712 713 714 715 716
}

static void pn533_recv_response(struct urb *urb)
{
	struct pn533 *dev = urb->context;
717
	struct pn533_cmd *cmd = dev->cmd;
718
	u8 *in_frame;
719

720 721
	cmd->status = urb->status;

722 723
	switch (urb->status) {
	case 0:
724
		break; /* success */
725 726
	case -ECONNRESET:
	case -ENOENT:
727 728 729
		dev_dbg(&dev->interface->dev,
			"The urb has been canceled (status %d)\n",
			urb->status);
730
		goto sched_wq;
731
	case -ESHUTDOWN:
732
	default:
733 734
		nfc_err(&dev->interface->dev,
			"Urb failure (status %d)\n", urb->status);
735
		goto sched_wq;
736 737 738 739
	}

	in_frame = dev->in_urb->transfer_buffer;

740
	dev_dbg(&dev->interface->dev, "Received a frame\n");
741 742
	print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
			     dev->ops->rx_frame_size(in_frame), false);
743

744
	if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
745
		nfc_err(&dev->interface->dev, "Received an invalid frame\n");
746
		cmd->status = -EIO;
747
		goto sched_wq;
748 749
	}

750
	if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
751 752
		nfc_err(&dev->interface->dev,
			"It it not the response to the last command\n");
753
		cmd->status = -EIO;
754
		goto sched_wq;
755 756
	}

757
sched_wq:
S
Samuel Ortiz 已提交
758
	queue_work(dev->wq, &dev->cmd_complete_work);
759 760 761 762 763 764 765 766 767 768 769 770
}

static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
{
	dev->in_urb->complete = pn533_recv_response;

	return usb_submit_urb(dev->in_urb, flags);
}

static void pn533_recv_ack(struct urb *urb)
{
	struct pn533 *dev = urb->context;
771
	struct pn533_cmd *cmd = dev->cmd;
772
	struct pn533_std_frame *in_frame;
773 774
	int rc;

775 776
	cmd->status = urb->status;

777 778
	switch (urb->status) {
	case 0:
779
		break; /* success */
780 781
	case -ECONNRESET:
	case -ENOENT:
782 783 784
		dev_dbg(&dev->interface->dev,
			"The urb has been stopped (status %d)\n",
			urb->status);
785
		goto sched_wq;
786
	case -ESHUTDOWN:
787
	default:
788 789
		nfc_err(&dev->interface->dev,
			"Urb failure (status %d)\n", urb->status);
790
		goto sched_wq;
791 792 793 794
	}

	in_frame = dev->in_urb->transfer_buffer;

795
	if (!pn533_std_rx_frame_is_ack(in_frame)) {
796
		nfc_err(&dev->interface->dev, "Received an invalid ack\n");
797
		cmd->status = -EIO;
798
		goto sched_wq;
799 800 801 802
	}

	rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
	if (rc) {
803 804
		nfc_err(&dev->interface->dev,
			"usb_submit_urb failed with result %d\n", rc);
805
		cmd->status = rc;
806
		goto sched_wq;
807 808 809 810
	}

	return;

811
sched_wq:
S
Samuel Ortiz 已提交
812
	queue_work(dev->wq, &dev->cmd_complete_work);
813 814 815 816 817 818 819 820 821 822 823
}

static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
{
	dev->in_urb->complete = pn533_recv_ack;

	return usb_submit_urb(dev->in_urb, flags);
}

static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
{
824
	u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
825
	/* spec 7.1.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
826 827
	int rc;

828 829
	dev->out_urb->transfer_buffer = ack;
	dev->out_urb->transfer_buffer_length = sizeof(ack);
830 831 832 833 834
	rc = usb_submit_urb(dev->out_urb, flags);

	return rc;
}

835 836 837
static int __pn533_send_frame_async(struct pn533 *dev,
					struct sk_buff *out,
					struct sk_buff *in,
838
					int in_len)
839 840 841
{
	int rc;

842 843
	dev->out_urb->transfer_buffer = out->data;
	dev->out_urb->transfer_buffer_length = out->len;
844

845 846
	dev->in_urb->transfer_buffer = in->data;
	dev->in_urb->transfer_buffer_length = in_len;
847

848 849
	print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
			     out->data, out->len, false);
850

851
	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
852 853 854
	if (rc)
		return rc;

855 856 857 858 859 860 861 862 863 864 865
	if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
		/* request for response for sent packet directly */
		rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
		if (rc)
			goto error;
	} else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
		/* request for ACK if that's the case */
		rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
		if (rc)
			goto error;
	}
866 867 868 869 870 871 872 873

	return 0;

error:
	usb_unlink_urb(dev->out_urb);
	return rc;
}

874 875
static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
				  struct sk_buff *skb)
876 877 878
{
	/* payload is already there, just update datalen */
	int payload_len = skb->len;
879
	struct pn533_frame_ops *ops = dev->ops;
880 881


882 883
	skb_push(skb, ops->tx_header_len);
	skb_put(skb, ops->tx_tail_len);
884

885 886 887
	ops->tx_frame_init(skb->data, cmd_code);
	ops->tx_update_payload_len(skb->data, payload_len);
	ops->tx_frame_finish(skb->data);
888 889
}

890
static int pn533_send_async_complete(struct pn533 *dev)
891
{
892
	struct pn533_cmd *cmd = dev->cmd;
893
	int status = cmd->status;
894

895 896
	struct sk_buff *req = cmd->req;
	struct sk_buff *resp = cmd->resp;
897 898 899 900 901

	int rc;

	dev_kfree_skb(req);

902
	if (status < 0) {
903 904
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
905
		dev_kfree_skb(resp);
906
		goto done;
907 908
	}

909 910 911
	skb_put(resp, dev->ops->rx_frame_size(resp->data));
	skb_pull(resp, dev->ops->rx_header_len);
	skb_trim(resp, resp->len - dev->ops->rx_tail_len);
912

913
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
914

915
done:
916
	kfree(cmd);
917
	dev->cmd = NULL;
918 919 920 921 922 923 924 925 926 927 928 929
	return rc;
}

static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
			      struct sk_buff *req, struct sk_buff *resp,
			      int resp_len,
			      pn533_send_async_complete_t complete_cb,
			      void *complete_cb_context)
{
	struct pn533_cmd *cmd;
	int rc = 0;

930
	dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code);
931

932 933
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
934 935
		return -ENOMEM;

936
	cmd->code = cmd_code;
937 938 939 940 941
	cmd->req = req;
	cmd->resp = resp;
	cmd->resp_len = resp_len;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
942

943
	pn533_build_cmd_frame(dev, cmd_code, req);
944 945 946 947

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
948
		rc = __pn533_send_frame_async(dev, req, resp, resp_len);
949 950 951 952
		if (rc)
			goto error;

		dev->cmd_pending = 1;
953
		dev->cmd = cmd;
954 955 956
		goto unlock;
	}

957 958
	dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n",
		__func__, cmd_code);
959 960 961 962 963 964 965

	INIT_LIST_HEAD(&cmd->queue);
	list_add_tail(&cmd->queue, &dev->cmd_queue);

	goto unlock;

error:
966
	kfree(cmd);
967 968 969
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
970 971 972 973 974 975 976 977 978
}

static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
				 struct sk_buff *req,
				 pn533_send_async_complete_t complete_cb,
				 void *complete_cb_context)
{
	struct sk_buff *resp;
	int rc;
979 980 981
	int  resp_len = dev->ops->rx_header_len +
			dev->ops->max_payload_len +
			dev->ops->rx_tail_len;
982 983 984 985 986 987 988 989 990 991 992

	resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
	if (!resp)
		return -ENOMEM;

	rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
				complete_cb_context);
	if (rc)
		dev_kfree_skb(resp);

	return rc;
993 994 995 996 997 998 999 1000 1001
}

static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
				struct sk_buff *req,
				pn533_send_async_complete_t complete_cb,
				void *complete_cb_context)
{
	struct sk_buff *resp;
	int rc;
1002 1003 1004
	int  resp_len = dev->ops->rx_header_len +
			dev->ops->max_payload_len +
			dev->ops->rx_tail_len;
1005

1006
	resp = alloc_skb(resp_len, GFP_KERNEL);
1007 1008 1009
	if (!resp)
		return -ENOMEM;

1010 1011
	rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
				complete_cb_context);
1012 1013 1014 1015 1016 1017
	if (rc)
		dev_kfree_skb(resp);

	return rc;
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
/*
 * pn533_send_cmd_direct_async
 *
 * The function sends a piority cmd directly to the chip omiting the cmd
 * queue. It's intended to be used by chaining mechanism of received responses
 * where the host has to request every single chunk of data before scheduling
 * next cmd from the queue.
 */
static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
				       struct sk_buff *req,
				       pn533_send_async_complete_t complete_cb,
				       void *complete_cb_context)
{
	struct sk_buff *resp;
1032
	struct pn533_cmd *cmd;
1033
	int rc;
1034 1035 1036
	int resp_len = dev->ops->rx_header_len +
		       dev->ops->max_payload_len +
		       dev->ops->rx_tail_len;
1037 1038 1039 1040 1041

	resp = alloc_skb(resp_len, GFP_KERNEL);
	if (!resp)
		return -ENOMEM;

1042 1043
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd) {
1044 1045 1046 1047
		dev_kfree_skb(resp);
		return -ENOMEM;
	}

1048
	cmd->code = cmd_code;
1049 1050 1051 1052 1053
	cmd->req = req;
	cmd->resp = resp;
	cmd->resp_len = resp_len;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
1054

1055
	pn533_build_cmd_frame(dev, cmd_code, req);
1056

1057
	rc = __pn533_send_frame_async(dev, req, resp, resp_len);
1058 1059
	if (rc < 0) {
		dev_kfree_skb(resp);
1060
		kfree(cmd);
1061 1062
	} else {
		dev->cmd = cmd;
1063 1064 1065 1066 1067
	}

	return rc;
}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
static void pn533_wq_cmd_complete(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
	int rc;

	rc = pn533_send_async_complete(dev);
	if (rc != -EINPROGRESS)
		queue_work(dev->wq, &dev->cmd_work);
}

S
Samuel Ortiz 已提交
1078 1079 1080 1081
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;
1082
	int rc;
S
Samuel Ortiz 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

	mutex_lock(&dev->cmd_lock);

	if (list_empty(&dev->cmd_queue)) {
		dev->cmd_pending = 0;
		mutex_unlock(&dev->cmd_lock);
		return;
	}

	cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);

1094 1095
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
1096 1097
	mutex_unlock(&dev->cmd_lock);

1098
	rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1099 1100 1101
	if (rc < 0) {
		dev_kfree_skb(cmd->req);
		dev_kfree_skb(cmd->resp);
1102
		kfree(cmd);
1103
		return;
1104
	}
1105 1106

	dev->cmd = cmd;
S
Samuel Ortiz 已提交
1107 1108
}

1109
struct pn533_sync_cmd_response {
1110
	struct sk_buff *resp;
1111 1112 1113
	struct completion done;
};

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
				    struct sk_buff *resp)
{
	struct pn533_sync_cmd_response *arg = _arg;

	arg->resp = resp;
	complete(&arg->done);

	return 0;
}

/*  pn533_send_cmd_sync
 *
 *  Please note the req parameter is freed inside the function to
 *  limit a number of return value interpretations by the caller.
 *
 *  1. negative in case of error during TX path -> req should be freed
 *
 *  2. negative in case of error during RX path -> req should not be freed
 *     as it's been already freed at the begining of RX path by
 *     async_complete_cb.
 *
 *  3. valid pointer in case of succesfult RX path
 *
 *  A caller has to check a return value with IS_ERR macro. If the test pass,
 *  the returned pointer is valid.
 *
 * */
static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
					       struct sk_buff *req)
{
	int rc;
	struct pn533_sync_cmd_response arg;

	init_completion(&arg.done);

	rc = pn533_send_cmd_async(dev, cmd_code, req,
				  pn533_send_sync_complete, &arg);
	if (rc) {
		dev_kfree_skb(req);
		return ERR_PTR(rc);
	}

	wait_for_completion(&arg.done);

	return arg.resp;
}

1162 1163 1164 1165 1166 1167
static void pn533_send_complete(struct urb *urb)
{
	struct pn533 *dev = urb->context;

	switch (urb->status) {
	case 0:
1168
		break; /* success */
1169 1170
	case -ECONNRESET:
	case -ENOENT:
1171 1172 1173
		dev_dbg(&dev->interface->dev,
			"The urb has been stopped (status %d)\n",
			urb->status);
1174
		break;
1175
	case -ESHUTDOWN:
1176
	default:
1177 1178
		nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
			urb->status);
1179 1180 1181
	}
}

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
{
	/* ACR122U does not support any command which aborts last
	 * issued command i.e. as ACK for standard PN533. Additionally,
	 * it behaves stange, sending broken or incorrect responses,
	 * when we cancel urb before the chip will send response.
	 */
	if (dev->device_type == PN533_DEVICE_ACR122U)
		return;

	/* An ack will cancel the last issued command */
	pn533_send_ack(dev, flags);

	/* cancel the urb request */
	usb_kill_urb(dev->in_urb);
}

1199
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1200 1201 1202
{
	struct sk_buff *skb;

1203
	skb = alloc_skb(dev->ops->tx_header_len +
1204
			size +
1205
			dev->ops->tx_tail_len, GFP_KERNEL);
1206 1207

	if (skb)
1208
		skb_reserve(skb, dev->ops->tx_header_len);
1209 1210 1211 1212

	return skb;
}

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
struct pn533_target_type_a {
	__be16 sens_res;
	u8 sel_res;
	u8 nfcid_len;
	u8 nfcid_data[];
} __packed;


#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))

#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C

#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)

#define PN533_TYPE_A_SEL_PROT_MIFARE 0
#define PN533_TYPE_A_SEL_PROT_ISO14443 1
#define PN533_TYPE_A_SEL_PROT_DEP 2
#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3

static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
							int target_data_len)
{
	u8 ssd;
	u8 platconf;

	if (target_data_len < sizeof(struct pn533_target_type_a))
		return false;

	/* The lenght check of nfcid[] and ats[] are not being performed because
	   the values are not being used */

	/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
	ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
	platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);

	if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1253 1254 1255
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
		return false;

	/* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
	if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
		return false;

	return true;
}

static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
							int tgt_data_len)
{
	struct pn533_target_type_a *tgt_type_a;

1270
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

	if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
		return -EPROTO;

	switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
	case PN533_TYPE_A_SEL_PROT_MIFARE:
		nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
		break;
	case PN533_TYPE_A_SEL_PROT_ISO14443:
		nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
		break;
	case PN533_TYPE_A_SEL_PROT_DEP:
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
		break;
	case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
		nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
							NFC_PROTO_NFC_DEP_MASK;
		break;
	}

	nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
	nfc_tgt->sel_res = tgt_type_a->sel_res;
S
Samuel Ortiz 已提交
1293 1294
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1295 1296 1297 1298 1299 1300 1301

	return 0;
}

struct pn533_target_felica {
	u8 pol_res;
	u8 opcode;
1302
	u8 nfcid2[NFC_NFCID2_MAXSIZE];
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	u8 pad[8];
	/* optional */
	u8 syst_code[];
} __packed;

#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE

static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
							int target_data_len)
{
	if (target_data_len < sizeof(struct pn533_target_felica))
		return false;

	if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
		return false;

	return true;
}

static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
							int tgt_data_len)
{
	struct pn533_target_felica *tgt_felica;

1328
	tgt_felica = (struct pn533_target_felica *)tgt_data;
1329 1330 1331 1332

	if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
		return -EPROTO;

1333 1334
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1335 1336 1337 1338
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
1339 1340 1341
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

1342 1343 1344
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	return 0;
}

struct pn533_target_jewel {
	__be16 sens_res;
	u8 jewelid[4];
} __packed;

static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
							int target_data_len)
{
	u8 ssd;
	u8 platconf;

	if (target_data_len < sizeof(struct pn533_target_jewel))
		return false;

	/* Requirement 4.6.3.3 from NFC Forum Digital Spec */
	ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
	platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);

	if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1367 1368 1369
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
		return false;

	return true;
}

static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
							int tgt_data_len)
{
	struct pn533_target_jewel *tgt_jewel;

1380
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1381 1382 1383 1384 1385 1386

	if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
		return -EPROTO;

	nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
	nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1387 1388
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

	return 0;
}

struct pn533_type_b_prot_info {
	u8 bitrate;
	u8 fsci_type;
	u8 fwi_adc_fo;
} __packed;

#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8

struct pn533_type_b_sens_res {
	u8 opcode;
	u8 nfcid[4];
	u8 appdata[4];
	struct pn533_type_b_prot_info prot_info;
} __packed;

#define PN533_TYPE_B_OPC_SENSB_RES 0x50

struct pn533_target_type_b {
	struct pn533_type_b_sens_res sensb_res;
	u8 attrib_res_len;
	u8 attrib_res[];
} __packed;

static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
							int target_data_len)
{
	if (target_data_len < sizeof(struct pn533_target_type_b))
		return false;

	if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
		return false;

	if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
						PN533_TYPE_B_PROT_TYPE_RFU_MASK)
		return false;

	return true;
}

static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
							int tgt_data_len)
{
	struct pn533_target_type_b *tgt_type_b;

1439
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1440 1441 1442 1443

	if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
		return -EPROTO;

1444
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1445 1446 1447 1448

	return 0;
}

1449 1450
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
1451 1452 1453 1454
{
	struct nfc_target nfc_tgt;
	int rc;

J
Joe Perches 已提交
1455
	dev_dbg(&dev->interface->dev, "%s: modulation=%d\n",
1456
		__func__, dev->poll_mod_curr);
1457

1458
	if (tg != 1)
1459 1460
		return -EPROTO;

S
Samuel Ortiz 已提交
1461 1462
	memset(&nfc_tgt, 0, sizeof(struct nfc_target));

1463 1464
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
1465
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1466 1467 1468
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
1469
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1470 1471
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
1472
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1473 1474
		break;
	case PN533_POLL_MOD_847KBPS_B:
1475
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1476 1477
		break;
	default:
1478 1479
		nfc_err(&dev->interface->dev,
			"Unknown current poll modulation\n");
1480 1481 1482 1483 1484 1485 1486
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1487 1488
		dev_dbg(&dev->interface->dev,
			"The Tg found doesn't have the desired protocol\n");
1489 1490 1491
		return -EAGAIN;
	}

1492 1493 1494
	dev_dbg(&dev->interface->dev,
		"Target found - supported protocols: 0x%x\n",
		nfc_tgt.supported_protocols);
1495 1496 1497 1498 1499 1500 1501 1502

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);

	return 0;
}

S
Samuel Ortiz 已提交
1503 1504 1505 1506 1507
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

1508 1509 1510 1511 1512 1513 1514 1515
static void pn533_poll_reset_mod_list(struct pn533 *dev)
{
	dev->poll_mod_count = 0;
}

static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
{
	dev->poll_mod_active[dev->poll_mod_count] =
1516
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
1517 1518 1519
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
1520 1521
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
1522 1523 1524
{
	pn533_poll_reset_mod_list(dev);

1525 1526 1527
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1528 1529
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

1530 1531
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1532 1533 1534 1535
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
1536
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
1537 1538
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

1539
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1540 1541
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
1542 1543
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1544 1545
}

1546
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1547
{
1548 1549
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
1550

1551 1552 1553
	/* Toggle the DEP polling */
	dev->poll_dep = 1;

1554 1555 1556 1557 1558 1559 1560
	nbtg = resp->data[0];
	tg = resp->data[1];
	tgdata = &resp->data[2];
	tgdata_len = resp->len - 2;  /* nbtg + tg */

	if (nbtg) {
		rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1561 1562

		/* We must stop the poll after a valid target found */
S
Samuel Ortiz 已提交
1563 1564 1565 1566
		if (rc == 0) {
			pn533_poll_reset_mod_list(dev);
			return 0;
		}
1567 1568
	}

S
Samuel Ortiz 已提交
1569
	return -EAGAIN;
1570 1571
}

1572
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1573
{
1574 1575 1576
	struct sk_buff *skb;
	u8 *felica, *nfcid3, *gb;

1577 1578 1579
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1580 1581 1582 1583
	u8 felica_params[18] = {0x1, 0xfe, /* DEP */
				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
				0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
				0xff, 0xff}; /* System code */
1584

1585 1586 1587
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1588

1589 1590 1591 1592
	unsigned int skb_len = 36 + /* mode (1), mifare (6),
				       felica (18), nfcid3 (10), gb_len (1) */
			       gbytes_len +
			       1;  /* len Tk*/
1593

1594
	skb = pn533_alloc_skb(dev, skb_len);
1595 1596
	if (!skb)
		return NULL;
1597 1598

	/* DEP support only */
1599
	*skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1600 1601 1602

	/* MIFARE params */
	memcpy(skb_put(skb, 6), mifare_params, 6);
1603 1604

	/* Felica params */
1605 1606 1607
	felica = skb_put(skb, 18);
	memcpy(felica, felica_params, 18);
	get_random_bytes(felica + 2, 6);
1608 1609

	/* NFCID3 */
1610 1611 1612
	nfcid3 = skb_put(skb, 10);
	memset(nfcid3, 0, 10);
	memcpy(nfcid3, felica, 8);
1613 1614

	/* General bytes */
1615
	*skb_put(skb, 1) = gbytes_len;
1616

1617 1618
	gb = skb_put(skb, gbytes_len);
	memcpy(gb, gbytes, gbytes_len);
1619

1620 1621
	/* Len Tk */
	*skb_put(skb, 1) = 0;
1622

1623
	return skb;
1624 1625
}

1626
#define PN533_CMD_DATAEXCH_HEAD_LEN 1
1627
#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1628 1629 1630
static void pn533_wq_tm_mi_recv(struct work_struct *work);
static struct sk_buff *pn533_build_response(struct pn533 *dev);

1631
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1632
				      struct sk_buff *resp)
1633
{
1634 1635 1636
	struct sk_buff *skb;
	u8 status, ret, mi;
	int rc;
1637

1638
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1639

1640 1641
	if (IS_ERR(resp)) {
		skb_queue_purge(&dev->resp_q);
1642
		return PTR_ERR(resp);
1643
	}
1644

1645
	status = resp->data[0];
1646 1647 1648 1649

	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

1650
	skb_pull(resp, sizeof(status));
1651

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	if (ret != PN533_CMD_RET_SUCCESS) {
		rc = -EIO;
		goto error;
	}

	skb_queue_tail(&dev->resp_q, resp);

	if (mi) {
		queue_work(dev->wq, &dev->mi_tm_rx_work);
		return -EINPROGRESS;
	}

	skb = pn533_build_response(dev);
	if (!skb) {
		rc = -EIO;
		goto error;
1668 1669
	}

1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	return nfc_tm_data_received(dev->nfc_dev, skb);

error:
	nfc_tm_deactivated(dev->nfc_dev);
	dev->tgt_mode = 0;
	skb_queue_purge(&dev->resp_q);
	dev_kfree_skb(resp);

	return rc;
}

static void pn533_wq_tm_mi_recv(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
	struct sk_buff *skb;
	int rc;

	dev_dbg(&dev->interface->dev, "%s\n", __func__);

	skb = pn533_alloc_skb(dev, 0);
	if (!skb)
		return;

	rc = pn533_send_cmd_direct_async(dev,
					PN533_CMD_TG_GET_DATA,
					skb,
					pn533_tm_get_data_complete,
					NULL);

	if (rc < 0)
		dev_kfree_skb(skb);

	return;
1703 1704
}

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
				  struct sk_buff *resp);
static void pn533_wq_tm_mi_send(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
	struct sk_buff *skb;
	int rc;

	dev_dbg(&dev->interface->dev, "%s\n", __func__);

	/* Grab the first skb in the queue */
	skb = skb_dequeue(&dev->fragment_skb);
	if (skb == NULL) {	/* No more data */
		/* Reset the queue for future use */
		skb_queue_head_init(&dev->fragment_skb);
		goto error;
	}

	/* last entry - remove MI bit */
	if (skb_queue_len(&dev->fragment_skb) == 0) {
		rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
					skb, pn533_tm_send_complete, NULL);
	} else
		rc = pn533_send_cmd_direct_async(dev,
					PN533_CMD_TG_SET_META_DATA,
					skb, pn533_tm_send_complete, NULL);

	if (rc == 0) /* success */
		return;

	dev_err(&dev->interface->dev,
		"Error %d when trying to perform set meta data_exchange", rc);

	dev_kfree_skb(skb);

error:
	pn533_send_ack(dev, GFP_KERNEL);
	queue_work(dev->wq, &dev->cmd_work);
}

1745 1746 1747
static void pn533_wq_tg_get_data(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, tg_work);
1748 1749
	struct sk_buff *skb;
	int rc;
1750

1751
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1752

1753
	skb = pn533_alloc_skb(dev, 0);
1754
	if (!skb)
1755 1756
		return;

1757 1758
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1759

1760 1761
	if (rc < 0)
		dev_kfree_skb(skb);
1762 1763 1764 1765

	return;
}

1766
#define ATR_REQ_GB_OFFSET 17
1767
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1768
{
1769
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1770
	size_t gb_len;
1771
	int rc;
1772

1773
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1774

1775
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1776 1777
		return -EINVAL;

1778 1779
	mode = resp->data[0];
	cmd = &resp->data[1];
1780

1781 1782
	dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
		mode, resp->len);
1783

1784 1785
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1786 1787
		comm_mode = NFC_COMM_ACTIVE;

1788
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1789 1790
		return -EOPNOTSUPP;

1791 1792
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1793

1794 1795 1796
	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
			      comm_mode, gb, gb_len);
	if (rc < 0) {
1797 1798
		nfc_err(&dev->interface->dev,
			"Error when signaling target activation\n");
1799 1800 1801
		return rc;
	}

S
Samuel Ortiz 已提交
1802
	dev->tgt_mode = 1;
1803 1804 1805
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1806 1807
}

S
Samuel Ortiz 已提交
1808
static void pn533_listen_mode_timer(unsigned long data)
1809
{
1810
	struct pn533 *dev = (struct pn533 *)data;
S
Samuel Ortiz 已提交
1811

1812
	dev_dbg(&dev->interface->dev, "Listen mode timeout\n");
S
Samuel Ortiz 已提交
1813 1814 1815 1816 1817

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

1818 1819
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
S
Samuel Ortiz 已提交
1820 1821
}

1822 1823 1824 1825 1826
static int pn533_rf_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1827
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1828 1829 1830 1831

	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1832
		nfc_err(&dev->interface->dev, "RF setting error %d", rc);
1833 1834 1835 1836

		return rc;
	}

1837 1838
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849

	dev_kfree_skb(resp);
	return rc;
}

static void pn533_wq_rf(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, rf_work);
	struct sk_buff *skb;
	int rc;

1850
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1851 1852 1853 1854 1855 1856

	skb = pn533_alloc_skb(dev, 2);
	if (!skb)
		return;

	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
S
Samuel Ortiz 已提交
1857
	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1858 1859 1860 1861 1862

	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
				  pn533_rf_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1863
		nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
1864 1865 1866 1867 1868
	}

	return;
}

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
				   struct sk_buff *resp)
{
	struct pn533_cmd_jump_dep_response *rsp;
	struct nfc_target nfc_target;
	u8 target_gt_len;
	int rc;

	if (IS_ERR(resp))
		return PTR_ERR(resp);

	rsp = (struct pn533_cmd_jump_dep_response *)resp->data;

	rc = rsp->status & PN533_CMD_RET_MASK;
	if (rc != PN533_CMD_RET_SUCCESS) {
		/* Not target found, turn radio off */
		queue_work(dev->wq, &dev->rf_work);

		dev_kfree_skb(resp);
		return 0;
	}

	dev_dbg(&dev->interface->dev, "Creating new target");

	nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	nfc_target.nfcid1_len = 10;
	memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
	rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
	if (rc)
		goto error;

	dev->tgt_available_prots = 0;
	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
	target_gt_len = resp->len - 17;
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
					  rsp->gt, target_gt_len);
	if (!rc) {
		rc = nfc_dep_link_is_up(dev->nfc_dev,
					dev->nfc_dev->targets[0].idx,
					0, NFC_RF_INITIATOR);

		if (!rc)
			pn533_poll_reset_mod_list(dev);
	}
error:
	dev_kfree_skb(resp);
	return rc;
}

#define PASSIVE_DATA_LEN 5
static int pn533_poll_dep(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	struct sk_buff *skb;
	int rc, skb_len;
	u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};

	dev_dbg(&dev->interface->dev, "%s", __func__);

	if (!dev->gb) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);

		if (!dev->gb || !dev->gb_len) {
			dev->poll_dep = 0;
			queue_work(dev->wq, &dev->rf_work);
		}
	}

	skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
	skb_len += PASSIVE_DATA_LEN;

	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	nfcid3[0] = 0x1;
	nfcid3[1] = 0xfe;
	get_random_bytes(nfcid3 + 2, 6);

	skb = pn533_alloc_skb(dev, skb_len);
	if (!skb)
		return -ENOMEM;

	*skb_put(skb, 1) = 0x01;  /* Active */
	*skb_put(skb, 1) = 0x02;  /* 424 kbps */

	next = skb_put(skb, 1);  /* Next */
	*next = 0;

	/* Copy passive data */
	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
	       NFC_NFCID3_MAXSIZE);
	*next |= 2;

	memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
	*next |= 4; /* We have some Gi */

	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
				  pn533_poll_dep_complete, NULL);

	if (rc < 0)
		dev_kfree_skb(skb);

	return rc;
}

S
Samuel Ortiz 已提交
1980
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1981
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1982 1983
{
	struct pn533_poll_modulations *cur_mod;
1984 1985
	int rc;

1986
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
1987

1988 1989 1990
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1991 1992
		nfc_err(&dev->interface->dev, "%s  Poll complete error %d\n",
			__func__, rc);
1993 1994 1995 1996 1997 1998 1999

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
			else
				goto stop_poll;
		} else if (rc < 0) {
2000 2001
			nfc_err(&dev->interface->dev,
				"Error %d when running poll\n", rc);
2002 2003
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
2004
	}
2005

S
Samuel Ortiz 已提交
2006 2007
	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];

2008
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
2009
		del_timer(&dev->listen_timer);
2010 2011
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
2012 2013
	}

2014 2015 2016 2017
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
2018

2019
	if (!dev->poll_mod_count) {
2020
		dev_dbg(&dev->interface->dev, "Polling has been stopped\n");
2021 2022 2023
		goto done;
	}

2024
	pn533_poll_next_mod(dev);
2025 2026
	/* Not target found, turn radio off */
	queue_work(dev->wq, &dev->rf_work);
S
Samuel Ortiz 已提交
2027

2028 2029 2030
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
2031 2032

stop_poll:
2033
	nfc_err(&dev->interface->dev, "Polling operation has been stopped\n");
2034

S
Samuel Ortiz 已提交
2035 2036
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
2037
	return rc;
2038 2039
}

2040 2041
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
2042
{
2043
	struct sk_buff *skb;
2044

2045
	skb = pn533_alloc_skb(dev, mod->len);
2046 2047
	if (!skb)
		return NULL;
2048

2049
	memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
2050

2051
	return skb;
S
Samuel Ortiz 已提交
2052
}
2053

S
Samuel Ortiz 已提交
2054 2055
static int pn533_send_poll_frame(struct pn533 *dev)
{
2056 2057
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2058
	int rc;
2059
	u8 cmd_code;
2060

2061
	mod = dev->poll_mod_active[dev->poll_mod_curr];
2062

2063 2064
	dev_dbg(&dev->interface->dev, "%s mod len %d\n",
		__func__, mod->len);
2065

2066 2067 2068 2069 2070
	if (dev->poll_dep)  {
		dev->poll_dep = 0;
		return pn533_poll_dep(dev->nfc_dev);
	}

2071 2072
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
2073
		skb = pn533_alloc_poll_tg_frame(dev);
2074 2075
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
2076
		skb = pn533_alloc_poll_in_frame(dev, mod);
2077 2078 2079
	}

	if (!skb) {
2080
		nfc_err(&dev->interface->dev, "Failed to allocate skb\n");
2081 2082 2083 2084 2085 2086 2087
		return -ENOMEM;
	}

	rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
				  NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
2088
		nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc);
2089
	}
2090

S
Samuel Ortiz 已提交
2091 2092 2093 2094 2095
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
2096
	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
S
Samuel Ortiz 已提交
2097 2098 2099 2100 2101
	struct pn533_poll_modulations *cur_mod;
	int rc;

	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];

2102 2103 2104
	dev_dbg(&dev->interface->dev,
		"%s cancel_listen %d modulation len %d\n",
		__func__, dev->cancel_listen, cur_mod->len);
S
Samuel Ortiz 已提交
2105 2106 2107

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
2108
		pn533_abort_cmd(dev, GFP_ATOMIC);
2109 2110
	}

S
Samuel Ortiz 已提交
2111 2112 2113
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
2114

S
Samuel Ortiz 已提交
2115 2116
	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
2117

S
Samuel Ortiz 已提交
2118
	return;
2119 2120
}

2121 2122 2123 2124
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2125
	struct pn533_poll_modulations *cur_mod;
2126
	u8 rand_mod;
2127
	int rc;
2128

2129 2130 2131
	dev_dbg(&dev->interface->dev,
		"%s: im protocols 0x%x tm protocols 0x%x\n",
		__func__, im_protocols, tm_protocols);
2132 2133

	if (dev->tgt_active_prot) {
2134 2135
		nfc_err(&dev->interface->dev,
			"Cannot poll with a target already activated\n");
2136 2137 2138
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
2139
	if (dev->tgt_mode) {
2140 2141
		nfc_err(&dev->interface->dev,
			"Cannot poll while already being activated\n");
S
Samuel Ortiz 已提交
2142 2143 2144
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
2145 2146 2147 2148 2149
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
2150

S
Samuel Ortiz 已提交
2151 2152 2153
	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
2154

2155 2156 2157 2158 2159
	/* Do not always start polling from the same modulation */
	get_random_bytes(&rand_mod, sizeof(rand_mod));
	rand_mod %= dev->poll_mod_count;
	dev->poll_mod_curr = rand_mod;

2160 2161 2162 2163 2164 2165 2166 2167 2168
	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];

	rc = pn533_send_poll_frame(dev);

	/* Start listen timer */
	if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);

	return rc;
2169 2170
}

2171 2172 2173 2174
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

S
Samuel Ortiz 已提交
2175 2176
	del_timer(&dev->listen_timer);

2177
	if (!dev->poll_mod_count) {
2178 2179
		dev_dbg(&dev->interface->dev,
			"Polling operation was not running\n");
2180 2181 2182
		return;
	}

2183
	pn533_abort_cmd(dev, GFP_KERNEL);
2184
	flush_delayed_work(&dev->poll_work);
2185
	pn533_poll_reset_mod_list(dev);
2186 2187 2188 2189
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
2190
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
2191
	u16 gt_len;
2192
	int rc;
2193 2194
	struct sk_buff *skb;
	struct sk_buff *resp;
2195

2196
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2197

2198
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
2199 2200
	if (!skb)
		return -ENOMEM;
2201

2202 2203
	*skb_put(skb, sizeof(u8)) = 1; /* TG */
	*skb_put(skb, sizeof(u8)) = 0; /* Next */
2204

2205 2206 2207
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2208

2209
	rsp = (struct pn533_cmd_activate_response *)resp->data;
2210
	rc = rsp->status & PN533_CMD_RET_MASK;
2211
	if (rc != PN533_CMD_RET_SUCCESS) {
2212 2213
		nfc_err(&dev->interface->dev,
			"Target activation failed (error 0x%x)\n", rc);
2214
		dev_kfree_skb(resp);
2215
		return -EIO;
2216
	}
2217

S
Samuel Ortiz 已提交
2218
	/* ATR_RES general bytes are located at offset 16 */
2219 2220
	gt_len = resp->len - 16;
	rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
S
Samuel Ortiz 已提交
2221

2222
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
2223
	return rc;
2224 2225
}

2226 2227
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
2228 2229 2230 2231
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

J
Joe Perches 已提交
2232
	dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol);
2233 2234

	if (dev->poll_mod_count) {
2235
		nfc_err(&dev->interface->dev,
2236
			"Cannot activate while polling\n");
2237 2238 2239 2240
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
2241 2242
		nfc_err(&dev->interface->dev,
			"There is already an active target\n");
2243 2244 2245 2246
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
2247 2248
		nfc_err(&dev->interface->dev,
			"There is no available target to activate\n");
2249 2250 2251 2252
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
2253 2254 2255
		nfc_err(&dev->interface->dev,
			"Target doesn't support requested proto %u\n",
			protocol);
2256 2257 2258 2259 2260 2261
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
2262 2263
			nfc_err(&dev->interface->dev,
				"Activating target with DEP failed %d\n", rc);
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
			return rc;
		}
	}

	dev->tgt_active_prot = protocol;
	dev->tgt_available_prots = 0;

	return 0;
}

2274 2275
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
				    struct nfc_target *target)
2276 2277
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2278 2279
	struct sk_buff *skb;
	struct sk_buff *resp;
2280 2281
	int rc;

2282
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2283 2284

	if (!dev->tgt_active_prot) {
2285
		nfc_err(&dev->interface->dev, "There is no active target\n");
2286 2287 2288 2289
		return;
	}

	dev->tgt_active_prot = 0;
S
Samuel Ortiz 已提交
2290 2291
	skb_queue_purge(&dev->resp_q);

2292
	skb = pn533_alloc_skb(dev, sizeof(u8));
2293 2294
	if (!skb)
		return;
2295

2296
	*skb_put(skb, 1) = 1; /* TG*/
2297

2298 2299
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
	if (IS_ERR(resp))
2300 2301
		return;

2302
	rc = resp->data[0] & PN533_CMD_RET_MASK;
2303
	if (rc != PN533_CMD_RET_SUCCESS)
2304 2305
		nfc_err(&dev->interface->dev,
			"Error 0x%x when releasing the target\n", rc);
2306

2307
	dev_kfree_skb(resp);
2308 2309 2310
	return;
}

2311 2312

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
2313
					 struct sk_buff *resp)
2314
{
2315
	struct pn533_cmd_jump_dep_response *rsp;
2316 2317
	u8 target_gt_len;
	int rc;
2318
	u8 active = *(u8 *)arg;
2319 2320

	kfree(arg);
2321

2322 2323
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2324 2325 2326

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
2327 2328
		nfc_err(&dev->interface->dev,
			"The target does not support DEP\n");
2329 2330
		rc =  -EINVAL;
		goto error;
2331 2332
	}

2333 2334 2335
	rsp = (struct pn533_cmd_jump_dep_response *)resp->data;

	rc = rsp->status & PN533_CMD_RET_MASK;
2336
	if (rc != PN533_CMD_RET_SUCCESS) {
2337 2338
		nfc_err(&dev->interface->dev,
			"Bringing DEP link up failed (error 0x%x)\n", rc);
2339
		goto error;
2340 2341 2342
	}

	if (!dev->tgt_available_prots) {
2343 2344
		struct nfc_target nfc_target;

2345
		dev_dbg(&dev->interface->dev, "Creating new target\n");
2346 2347

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2348
		nfc_target.nfcid1_len = 10;
2349
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2350 2351
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
2352
			goto error;
2353 2354 2355 2356 2357 2358 2359

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
2360
	target_gt_len = resp->len - 17;
2361
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2362
					  rsp->gt, target_gt_len);
2363 2364
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
2365 2366
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
2367

2368 2369 2370
error:
	dev_kfree_skb(resp);
	return rc;
2371 2372
}

2373
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
2374
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2375
			     u8 comm_mode, u8 *gb, size_t gb_len)
2376 2377
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2378
	struct sk_buff *skb;
2379 2380
	int rc, skb_len;
	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
2381
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2382

2383
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2384 2385

	if (dev->poll_mod_count) {
2386 2387
		nfc_err(&dev->interface->dev,
			"Cannot bring the DEP link up while polling\n");
2388 2389 2390 2391
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
2392 2393
		nfc_err(&dev->interface->dev,
			"There is already an active target\n");
2394 2395 2396
		return -EBUSY;
	}

2397
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
2398
	skb_len += PASSIVE_DATA_LEN;
2399

2400 2401 2402 2403 2404 2405 2406
	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	if (target && !target->nfcid2_len) {
		nfcid3[0] = 0x1;
		nfcid3[1] = 0xfe;
		get_random_bytes(nfcid3 + 2, 6);
	}
2407

2408
	skb = pn533_alloc_skb(dev, skb_len);
2409
	if (!skb)
2410 2411
		return -ENOMEM;

2412
	*skb_put(skb, 1) = !comm_mode;  /* ActPass */
2413
	*skb_put(skb, 1) = 0x02;  /* 424 kbps */
2414 2415 2416

	next = skb_put(skb, 1);  /* Next */
	*next = 0;
2417

2418 2419 2420
	/* Copy passive data */
	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
	*next |= 1;
2421

2422 2423
	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	if (target && target->nfcid2_len)
2424 2425
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
		       target->nfcid2_len);
2426 2427 2428 2429
	else
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
		       NFC_NFCID3_MAXSIZE);
	*next |= 2;
2430

2431
	if (gb != NULL && gb_len > 0) {
2432 2433
		memcpy(skb_put(skb, gb_len), gb, gb_len);
		*next |= 4; /* We have some Gi */
2434
	} else {
2435
		*next = 0;
2436 2437
	}

2438 2439 2440 2441 2442
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
2443

2444
	*arg = !comm_mode;
2445

2446 2447
	pn533_rf_field(dev->nfc_dev, 0);

2448 2449 2450 2451 2452 2453 2454
	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
				  pn533_in_dep_link_up_complete, arg);

	if (rc < 0) {
		dev_kfree_skb(skb);
		kfree(arg);
	}
2455 2456 2457 2458 2459 2460

	return rc;
}

static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
{
S
Samuel Ortiz 已提交
2461 2462
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

2463
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2464

S
Samuel Ortiz 已提交
2465 2466
	pn533_poll_reset_mod_list(dev);

2467 2468
	if (dev->tgt_mode || dev->tgt_active_prot)
		pn533_abort_cmd(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2469 2470 2471 2472 2473

	dev->tgt_active_prot = 0;
	dev->tgt_mode = 0;

	skb_queue_purge(&dev->resp_q);
2474 2475 2476 2477

	return 0;
}

2478 2479 2480 2481 2482
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
2483 2484 2485 2486 2487
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

2488
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500

	if (skb_queue_empty(&dev->resp_q))
		return NULL;

	if (skb_queue_len(&dev->resp_q) == 1) {
		skb = skb_dequeue(&dev->resp_q);
		goto out;
	}

	skb_queue_walk_safe(&dev->resp_q, tmp, t)
		skb_len += tmp->len;

2501 2502
	dev_dbg(&dev->interface->dev, "%s total length %d\n",
		__func__, skb_len);
S
Samuel Ortiz 已提交
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520

	skb = alloc_skb(skb_len, GFP_KERNEL);
	if (skb == NULL)
		goto out;

	skb_put(skb, skb_len);

	skb_queue_walk_safe(&dev->resp_q, tmp, t) {
		memcpy(skb->data + tmp_len, tmp->data, tmp->len);
		tmp_len += tmp->len;
	}

out:
	skb_queue_purge(&dev->resp_q);

	return skb;
}

2521
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2522
					struct sk_buff *resp)
2523 2524
{
	struct pn533_data_exchange_arg *arg = _arg;
2525 2526 2527
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
2528

2529
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2530

2531 2532 2533
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
2534 2535
	}

2536 2537 2538 2539 2540
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
2541

2542
	if (ret != PN533_CMD_RET_SUCCESS) {
2543 2544
		nfc_err(&dev->interface->dev,
			"Exchanging data failed (error 0x%x)\n", ret);
2545
		rc = -EIO;
2546 2547 2548
		goto error;
	}

2549
	skb_queue_tail(&dev->resp_q, resp);
S
Samuel Ortiz 已提交
2550

2551 2552
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
2553 2554 2555 2556 2557 2558 2559 2560 2561
		queue_work(dev->wq, &dev->mi_rx_work);
		return -EINPROGRESS;
	}

	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		dev->cmd_complete_dep_arg = arg;
		queue_work(dev->wq, &dev->mi_tx_work);

S
Samuel Ortiz 已提交
2562
		return -EINPROGRESS;
2563 2564
	}

S
Samuel Ortiz 已提交
2565
	skb = pn533_build_response(dev);
2566
	if (!skb)
S
Samuel Ortiz 已提交
2567
		goto error;
2568

S
Samuel Ortiz 已提交
2569
	arg->cb(arg->cb_context, skb, 0);
2570 2571 2572 2573
	kfree(arg);
	return 0;

error:
2574 2575
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2576
	skb_queue_purge(&dev->resp_q);
2577
	arg->cb(arg->cb_context, NULL, rc);
2578
	kfree(arg);
2579
	return rc;
2580 2581
}

2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
/* Split the Tx skb into small chunks */
static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
{
	struct sk_buff *frag;
	int  frag_size;

	do {
		/* Remaining size */
		if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
			frag_size = PN533_CMD_DATAFRAME_MAXLEN;
		else
			frag_size = skb->len;

		/* Allocate and reserve */
		frag = pn533_alloc_skb(dev, frag_size);
		if (!frag) {
			skb_queue_purge(&dev->fragment_skb);
			break;
		}

2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
				*skb_push(frag, sizeof(u8)) =
							(PN533_CMD_MI_MASK | 1);
			else
				*skb_push(frag, sizeof(u8)) =  1; /* TG */
		}
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628

		memcpy(skb_put(frag, frag_size), skb->data, frag_size);

		/* Reduce the size of incoming buffer */
		skb_pull(skb, frag_size);

		/* Add this to skb_queue */
		skb_queue_tail(&dev->fragment_skb, frag);

	} while (skb->len > 0);

	dev_kfree_skb(skb);

	return skb_queue_len(&dev->fragment_skb);
}

S
Samuel Ortiz 已提交
2629 2630 2631
static int pn533_transceive(struct nfc_dev *nfc_dev,
			    struct nfc_target *target, struct sk_buff *skb,
			    data_exchange_cb_t cb, void *cb_context)
2632 2633
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2634
	struct pn533_data_exchange_arg *arg = NULL;
2635 2636
	int rc;

2637
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2638 2639

	if (!dev->tgt_active_prot) {
2640 2641
		nfc_err(&dev->interface->dev,
			"Can't exchange data if there is no active target\n");
2642 2643 2644 2645
		rc = -EINVAL;
		goto error;
	}

2646
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2647 2648
	if (!arg) {
		rc = -ENOMEM;
2649
		goto error;
2650 2651 2652 2653 2654
	}

	arg->cb = cb;
	arg->cb_context = cb_context;

2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
	switch (dev->device_type) {
	case PN533_DEVICE_PASORI:
		if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
			rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
						   skb,
						   pn533_data_exchange_complete,
						   arg);

			break;
		}
	default:
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
		/* jumbo frame ? */
		if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
			rc = pn533_fill_fragment_skbs(dev, skb);
			if (rc <= 0)
				goto error;

			skb = skb_dequeue(&dev->fragment_skb);
			if (!skb) {
				rc = -EIO;
				goto error;
			}
		} else {
			*skb_push(skb, sizeof(u8)) =  1; /* TG */
		}
2680 2681 2682 2683 2684 2685

		rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
					   skb, pn533_data_exchange_complete,
					   arg);

		break;
2686 2687
	}

2688 2689 2690
	if (rc < 0) /* rc from send_async */
		goto error;

2691 2692 2693
	return 0;

error:
2694 2695
	kfree(arg);
	dev_kfree_skb(skb);
2696 2697 2698
	return rc;
}

2699
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2700
				  struct sk_buff *resp)
2701
{
2702
	u8 status;
2703

2704
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2705

2706 2707
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2708

2709
	status = resp->data[0];
2710

2711 2712 2713 2714 2715
	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		queue_work(dev->wq, &dev->mi_tm_tx_work);
		return -EINPROGRESS;
	}
2716
	dev_kfree_skb(resp);
2717

2718
	if (status != 0) {
2719 2720
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2721 2722
		dev->tgt_mode = 0;

2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
		return 0;
	}

	queue_work(dev->wq, &dev->tg_work);

	return 0;
}

static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

2736
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2737

2738
	/* let's split in multiple chunks if size's too big */
2739
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
		rc = pn533_fill_fragment_skbs(dev, skb);
		if (rc <= 0)
			goto error;

		/* get the first skb */
		skb = skb_dequeue(&dev->fragment_skb);
		if (!skb) {
			rc = -EIO;
			goto error;
		}

		rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
						pn533_tm_send_complete, NULL);
	} else {
		/* Send th skb */
		rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
						pn533_tm_send_complete, NULL);
2757 2758
	}

2759 2760
error:
	if (rc < 0) {
2761
		dev_kfree_skb(skb);
2762 2763
		skb_queue_purge(&dev->fragment_skb);
	}
2764 2765 2766 2767

	return rc;
}

S
Samuel Ortiz 已提交
2768 2769
static void pn533_wq_mi_recv(struct work_struct *work)
{
2770
	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2771
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2772 2773
	int rc;

2774
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
2775

2776
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2777 2778
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2779

2780 2781 2782 2783 2784 2785 2786 2787
	switch (dev->device_type) {
	case PN533_DEVICE_PASORI:
		if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
			rc = pn533_send_cmd_direct_async(dev,
						PN533_CMD_IN_COMM_THRU,
						skb,
						pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2788

2789 2790 2791 2792
			break;
		}
	default:
		*skb_put(skb, sizeof(u8)) =  1; /*TG*/
S
Samuel Ortiz 已提交
2793

2794 2795 2796 2797 2798
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2799

2800
		break;
S
Samuel Ortiz 已提交
2801 2802
	}

2803
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2804 2805
		return;

2806 2807
	nfc_err(&dev->interface->dev,
		"Error %d when trying to perform data_exchange\n", rc);
S
Samuel Ortiz 已提交
2808

2809
	dev_kfree_skb(skb);
2810
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2811

2812
error:
S
Samuel Ortiz 已提交
2813
	pn533_send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2814
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2815 2816
}

2817 2818 2819 2820 2821 2822
static void pn533_wq_mi_send(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
	struct sk_buff *skb;
	int rc;

2823
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860

	/* Grab the first skb in the queue */
	skb = skb_dequeue(&dev->fragment_skb);

	if (skb == NULL) {	/* No more data */
		/* Reset the queue for future use */
		skb_queue_head_init(&dev->fragment_skb);
		goto error;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_PASORI:
		if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
			rc = -EIO;
			break;
		}

		rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;

	default:
		/* Still some fragments? */
		rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;
	}

	if (rc == 0) /* success */
		return;

2861 2862
	nfc_err(&dev->interface->dev,
		"Error %d when trying to perform data_exchange\n", rc);
2863 2864 2865 2866 2867 2868 2869 2870 2871

	dev_kfree_skb(skb);
	kfree(dev->cmd_complete_dep_arg);

error:
	pn533_send_ack(dev, GFP_KERNEL);
	queue_work(dev->wq, &dev->cmd_work);
}

2872 2873 2874
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2875 2876 2877
	struct sk_buff *skb;
	struct sk_buff *resp;
	int skb_len;
2878

2879
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2880

2881
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2882

2883
	skb = pn533_alloc_skb(dev, skb_len);
2884 2885
	if (!skb)
		return -ENOMEM;
2886

2887 2888
	*skb_put(skb, sizeof(cfgitem)) = cfgitem;
	memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2889

2890 2891 2892
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2893

2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
	dev_kfree_skb(resp);
	return 0;
}

static int pn533_get_firmware_version(struct pn533 *dev,
				      struct pn533_fw_version *fv)
{
	struct sk_buff *skb;
	struct sk_buff *resp;

2904
	skb = pn533_alloc_skb(dev, 0);
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
	if (!skb)
		return -ENOMEM;

	resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);

	fv->ic = resp->data[0];
	fv->ver = resp->data[1];
	fv->rev = resp->data[2];
	fv->support = resp->data[3];

	dev_kfree_skb(resp);
	return 0;
2919 2920
}

2921
static int pn533_pasori_fw_reset(struct pn533 *dev)
2922
{
2923 2924
	struct sk_buff *skb;
	struct sk_buff *resp;
2925

2926
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2927

2928
	skb = pn533_alloc_skb(dev, sizeof(u8));
2929 2930
	if (!skb)
		return -ENOMEM;
2931

2932
	*skb_put(skb, sizeof(u8)) = 0x1;
2933

2934 2935 2936
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2937

2938
	dev_kfree_skb(resp);
2939

2940
	return 0;
2941 2942
}

2943 2944 2945 2946 2947 2948 2949 2950 2951
struct pn533_acr122_poweron_rdr_arg {
	int rc;
	struct completion done;
};

static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
{
	struct pn533_acr122_poweron_rdr_arg *arg = urb->context;

2952
	dev_dbg(&urb->dev->dev, "%s\n", __func__);
2953

2954
	print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
		       urb->transfer_buffer, urb->transfer_buffer_length,
		       false);

	arg->rc = urb->status;
	complete(&arg->done);
}

static int pn533_acr122_poweron_rdr(struct pn533 *dev)
{
	/* Power on th reader (CCID cmd) */
	u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
		      0, 0, 0, 0, 0, 0, 3, 0, 0};
	u8 buf[255];
	int rc;
	void *cntx;
	struct pn533_acr122_poweron_rdr_arg arg;

2972
	dev_dbg(&dev->interface->dev, "%s\n", __func__);
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984

	init_completion(&arg.done);
	cntx = dev->in_urb->context;  /* backup context */

	dev->in_urb->transfer_buffer = buf;
	dev->in_urb->transfer_buffer_length = 255;
	dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
	dev->in_urb->context = &arg;

	dev->out_urb->transfer_buffer = cmd;
	dev->out_urb->transfer_buffer_length = sizeof(cmd);

2985
	print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2986 2987 2988 2989
		       cmd, sizeof(cmd), false);

	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
	if (rc) {
2990 2991
		nfc_err(&dev->interface->dev,
			"Reader power on cmd error %d\n", rc);
2992 2993 2994 2995 2996
		return rc;
	}

	rc =  usb_submit_urb(dev->in_urb, GFP_KERNEL);
	if (rc) {
2997 2998
		nfc_err(&dev->interface->dev,
			"Can't submit reader poweron cmd response %d\n", rc);
2999 3000 3001 3002 3003 3004 3005 3006 3007
		return rc;
	}

	wait_for_completion(&arg.done);
	dev->in_urb->context = cntx; /* restore context */

	return arg.rc;
}

3008 3009 3010 3011 3012 3013
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	u8 rf_field = !!rf;
	int rc;

S
Samuel Ortiz 已提交
3014 3015
	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;

3016 3017 3018
	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
				     (u8 *)&rf_field, 1);
	if (rc) {
3019
		nfc_err(&dev->interface->dev, "Error on setting RF field\n");
3020 3021 3022 3023 3024 3025
		return rc;
	}

	return rc;
}

3026
static int pn533_dev_up(struct nfc_dev *nfc_dev)
3027 3028 3029 3030
{
	return pn533_rf_field(nfc_dev, 1);
}

3031
static int pn533_dev_down(struct nfc_dev *nfc_dev)
3032 3033 3034 3035
{
	return pn533_rf_field(nfc_dev, 0);
}

3036
static struct nfc_ops pn533_nfc_ops = {
3037 3038
	.dev_up = pn533_dev_up,
	.dev_down = pn533_dev_down,
3039 3040
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
3041 3042 3043 3044
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
3045
	.im_transceive = pn533_transceive,
3046
	.tm_send = pn533_tm_send,
3047 3048
};

3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
static int pn533_setup(struct pn533 *dev)
{
	struct pn533_config_max_retries max_retries;
	struct pn533_config_timing timing;
	u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
	int rc;

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
	case PN533_DEVICE_PASORI:
3059
	case PN533_DEVICE_ACR122U:
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
		max_retries.mx_rty_atr = 0x2;
		max_retries.mx_rty_psl = 0x1;
		max_retries.mx_rty_passive_act =
			PN533_CONFIG_MAX_RETRIES_NO_RETRY;

		timing.rfu = PN533_CONFIG_TIMING_102;
		timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
		timing.dep_timeout = PN533_CONFIG_TIMING_204;

		break;

	default:
3072 3073
		nfc_err(&dev->interface->dev, "Unknown device type %d\n",
			dev->device_type);
3074 3075 3076 3077 3078 3079
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
3080 3081
		nfc_err(&dev->interface->dev,
			"Error on setting MAX_RETRIES config\n");
3082 3083 3084 3085 3086 3087 3088
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
3089
		nfc_err(&dev->interface->dev, "Error on setting RF timings\n");
3090 3091 3092 3093 3094 3095 3096 3097
		return rc;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
		break;

	case PN533_DEVICE_PASORI:
3098
		pn533_pasori_fw_reset(dev);
3099 3100 3101 3102

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
3103 3104
			nfc_err(&dev->interface->dev,
				"Error while settings PASORI config\n");
3105 3106 3107
			return rc;
		}

3108
		pn533_pasori_fw_reset(dev);
3109 3110 3111 3112 3113 3114 3115

		break;
	}

	return 0;
}

3116 3117 3118
static int pn533_probe(struct usb_interface *interface,
			const struct usb_device_id *id)
{
3119
	struct pn533_fw_version fw_ver;
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
	struct pn533 *dev;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
	int in_endpoint = 0;
	int out_endpoint = 0;
	int rc = -ENOMEM;
	int i;
	u32 protocols;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

	dev->udev = usb_get_dev(interface_to_usbdev(interface));
	dev->interface = interface;
3135
	mutex_init(&dev->cmd_lock);
3136 3137 3138 3139 3140

	iface_desc = interface->cur_altsetting;
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;

3141
		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
3142 3143
			in_endpoint = endpoint->bEndpointAddress;

3144
		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
3145 3146 3147 3148
			out_endpoint = endpoint->bEndpointAddress;
	}

	if (!in_endpoint || !out_endpoint) {
3149 3150
		nfc_err(&interface->dev,
			"Could not find bulk-in or bulk-out endpoint\n");
3151 3152 3153 3154 3155 3156 3157
		rc = -ENODEV;
		goto error;
	}

	dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
	dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);

3158
	if (!dev->in_urb || !dev->out_urb)
3159 3160 3161
		goto error;

	usb_fill_bulk_urb(dev->in_urb, dev->udev,
3162 3163
			  usb_rcvbulkpipe(dev->udev, in_endpoint),
			  NULL, 0, NULL, dev);
3164
	usb_fill_bulk_urb(dev->out_urb, dev->udev,
3165 3166
			  usb_sndbulkpipe(dev->udev, out_endpoint),
			  NULL, 0, pn533_send_complete, dev);
3167

S
Samuel Ortiz 已提交
3168 3169
	INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
	INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
3170 3171
	INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
	INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
3172
	INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
3173
	INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv);
3174
	INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send);
3175
	INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
3176
	INIT_WORK(&dev->rf_work, pn533_wq_rf);
3177
	dev->wq = alloc_ordered_workqueue("pn533", 0);
3178 3179
	if (dev->wq == NULL)
		goto error;
3180

S
Samuel Ortiz 已提交
3181 3182 3183 3184
	init_timer(&dev->listen_timer);
	dev->listen_timer.data = (unsigned long) dev;
	dev->listen_timer.function = pn533_listen_mode_timer;

S
Samuel Ortiz 已提交
3185
	skb_queue_head_init(&dev->resp_q);
3186
	skb_queue_head_init(&dev->fragment_skb);
S
Samuel Ortiz 已提交
3187

S
Samuel Ortiz 已提交
3188 3189
	INIT_LIST_HEAD(&dev->cmd_queue);

3190 3191
	usb_set_intfdata(interface, dev);

3192
	dev->ops = &pn533_std_frame_ops;
3193

3194
	dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
	dev->device_type = id->driver_info;
	switch (dev->device_type) {
	case PN533_DEVICE_STD:
		protocols = PN533_ALL_PROTOCOLS;
		break;

	case PN533_DEVICE_PASORI:
		protocols = PN533_NO_TYPE_B_PROTOCOLS;
		break;

3205 3206 3207 3208 3209 3210 3211
	case PN533_DEVICE_ACR122U:
		protocols = PN533_NO_TYPE_B_PROTOCOLS;
		dev->ops = &pn533_acr122_frame_ops;
		dev->protocol_type = PN533_PROTO_REQ_RESP,

		rc = pn533_acr122_poweron_rdr(dev);
		if (rc < 0) {
3212 3213
			nfc_err(&dev->interface->dev,
				"Couldn't poweron the reader (error %d)\n", rc);
3214 3215 3216 3217
			goto destroy_wq;
		}
		break;

3218
	default:
3219 3220
		nfc_err(&dev->interface->dev, "Unknown device type %d\n",
			dev->device_type);
3221 3222 3223
		rc = -EINVAL;
		goto destroy_wq;
	}
3224

3225 3226 3227 3228 3229
	memset(&fw_ver, 0, sizeof(fw_ver));
	rc = pn533_get_firmware_version(dev, &fw_ver);
	if (rc < 0)
		goto destroy_wq;

3230 3231 3232
	nfc_info(&dev->interface->dev,
		 "NXP PN5%02X firmware ver %d.%d now attached\n",
		 fw_ver.ic, fw_ver.ver, fw_ver.rev);
3233 3234


3235
	dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
3236
					   dev->ops->tx_header_len +
3237
					   PN533_CMD_DATAEXCH_HEAD_LEN,
3238
					   dev->ops->tx_tail_len);
3239 3240
	if (!dev->nfc_dev) {
		rc = -ENOMEM;
3241
		goto destroy_wq;
3242
	}
3243 3244 3245 3246 3247 3248 3249 3250

	nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
	nfc_set_drvdata(dev->nfc_dev, dev);

	rc = nfc_register_device(dev->nfc_dev);
	if (rc)
		goto free_nfc_dev;

3251 3252
	rc = pn533_setup(dev);
	if (rc)
S
Samuel Ortiz 已提交
3253 3254
		goto unregister_nfc_dev;

3255 3256
	return 0;

3257 3258 3259
unregister_nfc_dev:
	nfc_unregister_device(dev->nfc_dev);

3260 3261
free_nfc_dev:
	nfc_free_device(dev->nfc_dev);
3262

3263 3264
destroy_wq:
	destroy_workqueue(dev->wq);
3265 3266 3267
error:
	usb_free_urb(dev->in_urb);
	usb_free_urb(dev->out_urb);
3268
	usb_put_dev(dev->udev);
3269 3270 3271 3272 3273 3274 3275
	kfree(dev);
	return rc;
}

static void pn533_disconnect(struct usb_interface *interface)
{
	struct pn533 *dev;
S
Samuel Ortiz 已提交
3276
	struct pn533_cmd *cmd, *n;
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286

	dev = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);

	nfc_unregister_device(dev->nfc_dev);
	nfc_free_device(dev->nfc_dev);

	usb_kill_urb(dev->in_urb);
	usb_kill_urb(dev->out_urb);

3287
	flush_delayed_work(&dev->poll_work);
3288
	destroy_workqueue(dev->wq);
3289

S
Samuel Ortiz 已提交
3290 3291
	skb_queue_purge(&dev->resp_q);

S
Samuel Ortiz 已提交
3292 3293
	del_timer(&dev->listen_timer);

S
Samuel Ortiz 已提交
3294 3295 3296 3297 3298
	list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
		list_del(&cmd->queue);
		kfree(cmd);
	}

3299 3300 3301 3302
	usb_free_urb(dev->in_urb);
	usb_free_urb(dev->out_urb);
	kfree(dev);

3303
	nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
3304 3305 3306 3307 3308 3309 3310 3311 3312
}

static struct usb_driver pn533_driver = {
	.name =		"pn533",
	.probe =	pn533_probe,
	.disconnect =	pn533_disconnect,
	.id_table =	pn533_table,
};

3313
module_usb_driver(pn533_driver);
3314

3315 3316 3317
MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
3318 3319 3320
MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");