pn533.c 64.4 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 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#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>
28
#include <net/nfc/nfc.h>
29 30 31 32 33 34 35 36 37

#define VERSION "0.1"

#define PN533_VENDOR_ID 0x4CC
#define PN533_PRODUCT_ID 0x2533

#define SCM_VENDOR_ID 0x4E6
#define SCL3711_PRODUCT_ID 0x5591

38 39 40
#define SONY_VENDOR_ID         0x054c
#define PASORI_PRODUCT_ID      0x02e1

41 42 43 44 45 46
#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
47

48 49 50 51
#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)
52 53 54 55

#define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
				   NFC_PROTO_MIFARE_MASK | \
				   NFC_PROTO_FELICA_MASK | \
56
				   NFC_PROTO_ISO14443_MASK | \
57 58
				   NFC_PROTO_NFC_DEP_MASK)

59
static const struct usb_device_id pn533_table[] = {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	{ .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,
	},
75 76 77 78 79
	{ .match_flags		= USB_DEVICE_ID_MATCH_DEVICE,
	  .idVendor		= ACS_VENDOR_ID,
	  .idProduct		= ACR122U_PRODUCT_ID,
	  .driver_info		= PN533_DEVICE_ACR122U,
	},
80 81 82 83
	{ }
};
MODULE_DEVICE_TABLE(usb, pn533_table);

S
Samuel Ortiz 已提交
84 85 86
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2

87 88
/* Standard pn533 frame definitions */
#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
/*
 * Max extended frame payload len, excluding TFI and CC
 * which are already in PN533_FRAME_HEADER_LEN.
 */
96
#define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
97

98
#define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
99
				  Postamble (1) */
100 101
#define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
#define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
102 103

/* start of frame */
104
#define PN533_STD_FRAME_SOF 0x00FF
105

106 107 108 109
/* 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
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* 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

126
/* PN533 Commands */
127
#define PN533_STD_FRAME_CMD(f) (f->data[1])
128 129 130 131

#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
#define PN533_CMD_RF_CONFIGURATION 0x32
#define PN533_CMD_IN_DATA_EXCHANGE 0x40
132
#define PN533_CMD_IN_COMM_THRU     0x42
133 134 135
#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
#define PN533_CMD_IN_ATR 0x50
#define PN533_CMD_IN_RELEASE 0x52
136
#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
137

138
#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
139
#define PN533_CMD_TG_GET_DATA 0x86
140
#define PN533_CMD_TG_SET_DATA 0x8e
141
#define PN533_CMD_UNDEF 0xff
142

143 144 145 146 147 148 149 150 151
#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;

152
typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
153

154 155 156
typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
					struct sk_buff *resp);

157 158 159 160 161 162 163 164 165 166 167
/* 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 */
S
Samuel Ortiz 已提交
168
#define PN533_CFGITEM_TIMING 0x02
169
#define PN533_CFGITEM_MAX_RETRIES 0x05
170
#define PN533_CFGITEM_PASORI 0x82
171

S
Samuel Ortiz 已提交
172 173 174 175 176
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe

177 178 179 180 181 182 183 184 185
#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 已提交
186 187 188 189 190 191
struct pn533_config_timing {
	u8 rfu;
	u8 atr_res_timeout;
	u8 dep_timeout;
} __packed;

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/* 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 已提交
228
	PN533_LISTEN_MOD,
229 230 231 232 233 234 235 236 237 238 239 240 241 242

	__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;
};

243
static const struct pn533_poll_modulations poll_mod[] = {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	[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,
				.rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
				.tsn = 0,
			},
		},
		.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,
				.rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
				.tsn = 0,
			},
		 },
		.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 已提交
296 297 298
	[PN533_LISTEN_MOD] = {
		.len = 0,
	},
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
};

/* 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;

315 316 317 318 319 320 321 322 323 324 325 326
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;
327

328 329 330 331 332

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

333 334 335 336
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

337 338 339 340 341
enum  pn533_protocol_type {
	PN533_PROTO_REQ_ACK_RESP = 0,
	PN533_PROTO_REQ_RESP
};

342 343 344 345
struct pn533 {
	struct usb_device *udev;
	struct usb_interface *interface;
	struct nfc_dev *nfc_dev;
346
	u32 device_type;
347
	enum pn533_protocol_type protocol_type;
348 349 350 351

	struct urb *out_urb;
	struct urb *in_urb;

S
Samuel Ortiz 已提交
352 353
	struct sk_buff_head resp_q;

354 355
	struct workqueue_struct	*wq;
	struct work_struct cmd_work;
S
Samuel Ortiz 已提交
356
	struct work_struct cmd_complete_work;
S
Samuel Ortiz 已提交
357
	struct work_struct poll_work;
S
Samuel Ortiz 已提交
358
	struct work_struct mi_work;
359
	struct work_struct tg_work;
360 361 362 363 364

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

366
	void *cmd_complete_mi_arg;
367 368 369 370 371

	struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
	u8 poll_mod_count;
	u8 poll_mod_curr;
	u32 poll_protocols;
S
Samuel Ortiz 已提交
372
	u32 listen_protocols;
373 374
	struct timer_list listen_timer;
	int cancel_listen;
S
Samuel Ortiz 已提交
375 376 377

	u8 *gb;
	size_t gb_len;
378 379 380

	u8 tgt_available_prots;
	u8 tgt_active_prot;
S
Samuel Ortiz 已提交
381
	u8 tgt_mode;
382

383
	struct pn533_frame_ops *ops;
S
Samuel Ortiz 已提交
384 385 386 387
};

struct pn533_cmd {
	struct list_head queue;
388
	u8 code;
389
	int status;
390 391
	struct sk_buff *req;
	struct sk_buff *resp;
392
	int resp_len;
393 394
	pn533_send_async_complete_t  complete_cb;
	void *complete_cb_context;
395 396
};

397
struct pn533_std_frame {
398 399 400 401 402 403 404
	u8 preamble;
	__be16 start_frame;
	u8 datalen;
	u8 datalen_checksum;
	u8 data[];
} __packed;

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
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;

	bool (*rx_is_frame_valid)(void *frame);
	int (*rx_frame_size)(void *frame);
	int rx_header_len;
	int rx_tail_len;

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

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530
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;
}

static bool pn533_acr122_is_rx_frame_valid(void *_frame)
{
	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;

	return PN533_STD_FRAME_CMD(f);
}

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,
};

531
/* The rule: value + checksum = 0 */
532
static inline u8 pn533_std_checksum(u8 value)
533 534 535 536 537
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
538
static u8 pn533_std_data_checksum(u8 *data, int datalen)
539 540 541 542 543 544 545
{
	u8 sum = 0;
	int i;

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

546
	return pn533_std_checksum(sum);
547 548
}

549
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
550
{
551
	struct pn533_std_frame *frame = _frame;
552

553
	frame->preamble = 0;
554 555 556
	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
	PN533_STD_FRAME_CMD(frame) = cmd_code;
557 558 559
	frame->datalen = 2;
}

560
static void pn533_std_tx_frame_finish(void *_frame)
561
{
562
	struct pn533_std_frame *frame = _frame;
563

564
	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
565

566 567
	PN533_STD_FRAME_CHECKSUM(frame) =
		pn533_std_data_checksum(frame->data, frame->datalen);
568

569
	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
570 571
}

572
static void pn533_std_tx_update_payload_len(void *_frame, int len)
573
{
574
	struct pn533_std_frame *frame = _frame;
575 576 577 578

	frame->datalen += len;
}

579
static bool pn533_std_rx_frame_is_valid(void *_frame)
580 581
{
	u8 checksum;
582
	struct pn533_std_frame *frame = _frame;
583

584
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
585 586
		return false;

587
	checksum = pn533_std_checksum(frame->datalen);
588 589 590
	if (checksum != frame->datalen_checksum)
		return false;

591 592
	checksum = pn533_std_data_checksum(frame->data, frame->datalen);
	if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
593 594 595 596 597
		return false;

	return true;
}

598
static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
599
{
600
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
601 602 603 604 605 606 607 608
		return false;

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

	return true;
}

609
static inline int pn533_std_rx_frame_size(void *frame)
610
{
611
	struct pn533_std_frame *f = frame;
612

613 614
	return sizeof(struct pn533_std_frame) + f->datalen +
	       PN533_STD_FRAME_TAIL_LEN;
615 616
}

617
static u8 pn533_std_get_cmd_code(void *frame)
618
{
619
	struct pn533_std_frame *f = frame;
620

621
	return PN533_STD_FRAME_CMD(f);
622 623
}

624
static struct pn533_frame_ops pn533_std_frame_ops = {
625 626 627 628 629 630 631 632 633 634 635 636 637
	.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,
638 639 640
};

static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
641
{
642
	return (dev->ops->get_cmd_code(frame) ==
643
				PN533_CMD_RESPONSE(dev->cmd->code));
644 645 646 647 648
}

static void pn533_recv_response(struct urb *urb)
{
	struct pn533 *dev = urb->context;
649
	struct pn533_cmd *cmd = dev->cmd;
650
	u8 *in_frame;
651

652 653
	cmd->status = urb->status;

654 655
	switch (urb->status) {
	case 0:
656
		break; /* success */
657 658
	case -ECONNRESET:
	case -ENOENT:
659
		nfc_dev_dbg(&dev->interface->dev,
660 661
			    "The urb has been canceled (status %d)",
			    urb->status);
662
		goto sched_wq;
663
	case -ESHUTDOWN:
664
	default:
665
		nfc_dev_err(&dev->interface->dev,
666
			    "Urb failure (status %d)", urb->status);
667
		goto sched_wq;
668 669 670 671
	}

	in_frame = dev->in_urb->transfer_buffer;

672
	nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
673 674
	print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
			     dev->ops->rx_frame_size(in_frame), false);
675

676
	if (!dev->ops->rx_is_frame_valid(in_frame)) {
677
		nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
678
		cmd->status = -EIO;
679
		goto sched_wq;
680 681
	}

682
	if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
683 684
		nfc_dev_err(&dev->interface->dev,
			    "It it not the response to the last command");
685
		cmd->status = -EIO;
686
		goto sched_wq;
687 688
	}

689
sched_wq:
S
Samuel Ortiz 已提交
690
	queue_work(dev->wq, &dev->cmd_complete_work);
691 692 693 694 695 696 697 698 699 700 701 702
}

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;
703
	struct pn533_cmd *cmd = dev->cmd;
704
	struct pn533_std_frame *in_frame;
705 706
	int rc;

707 708
	cmd->status = urb->status;

709 710
	switch (urb->status) {
	case 0:
711
		break; /* success */
712 713
	case -ECONNRESET:
	case -ENOENT:
714
		nfc_dev_dbg(&dev->interface->dev,
715 716
			    "The urb has been stopped (status %d)",
			    urb->status);
717
		goto sched_wq;
718
	case -ESHUTDOWN:
719
	default:
720
		nfc_dev_err(&dev->interface->dev,
721
			    "Urb failure (status %d)", urb->status);
722
		goto sched_wq;
723 724 725 726
	}

	in_frame = dev->in_urb->transfer_buffer;

727
	if (!pn533_std_rx_frame_is_ack(in_frame)) {
728
		nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
729
		cmd->status = -EIO;
730
		goto sched_wq;
731 732 733 734
	}

	rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
	if (rc) {
735 736
		nfc_dev_err(&dev->interface->dev,
			    "usb_submit_urb failed with result %d", rc);
737
		cmd->status = rc;
738
		goto sched_wq;
739 740 741 742
	}

	return;

743
sched_wq:
S
Samuel Ortiz 已提交
744
	queue_work(dev->wq, &dev->cmd_complete_work);
745 746 747 748 749 750 751 752 753 754 755
}

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)
{
756
	u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
757
	/* spec 7.1.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
758 759 760 761
	int rc;

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

762 763
	dev->out_urb->transfer_buffer = ack;
	dev->out_urb->transfer_buffer_length = sizeof(ack);
764 765 766 767 768
	rc = usb_submit_urb(dev->out_urb, flags);

	return rc;
}

769 770 771
static int __pn533_send_frame_async(struct pn533 *dev,
					struct sk_buff *out,
					struct sk_buff *in,
772
					int in_len)
773 774 775
{
	int rc;

776 777
	dev->out_urb->transfer_buffer = out->data;
	dev->out_urb->transfer_buffer_length = out->len;
778

779 780
	dev->in_urb->transfer_buffer = in->data;
	dev->in_urb->transfer_buffer_length = in_len;
781

782 783
	print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
			     out->data, out->len, false);
784

785
	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
786 787 788
	if (rc)
		return rc;

789 790 791 792 793 794 795 796 797 798 799
	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;
	}
800 801 802 803 804 805 806 807

	return 0;

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

808 809
static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
				  struct sk_buff *skb)
810 811 812
{
	/* payload is already there, just update datalen */
	int payload_len = skb->len;
813
	struct pn533_frame_ops *ops = dev->ops;
814 815


816 817
	skb_push(skb, ops->tx_header_len);
	skb_put(skb, ops->tx_tail_len);
818

819 820 821
	ops->tx_frame_init(skb->data, cmd_code);
	ops->tx_update_payload_len(skb->data, payload_len);
	ops->tx_frame_finish(skb->data);
822 823
}

824
static int pn533_send_async_complete(struct pn533 *dev)
825
{
826
	struct pn533_cmd *cmd = dev->cmd;
827
	int status = cmd->status;
828

829 830
	struct sk_buff *req = cmd->req;
	struct sk_buff *resp = cmd->resp;
831 832 833 834 835

	int rc;

	dev_kfree_skb(req);

836
	if (status < 0) {
837 838
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
839
		dev_kfree_skb(resp);
840
		goto done;
841 842
	}

843 844 845
	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);
846

847
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
848

849
done:
850
	kfree(cmd);
851
	dev->cmd = NULL;
852 853 854 855 856 857 858 859 860 861 862 863
	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;

864
	nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
865

866 867
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
868 869
		return -ENOMEM;

870
	cmd->code = cmd_code;
871 872 873 874 875
	cmd->req = req;
	cmd->resp = resp;
	cmd->resp_len = resp_len;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
876

877
	pn533_build_cmd_frame(dev, cmd_code, req);
878 879 880 881

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
882
		rc = __pn533_send_frame_async(dev, req, resp, resp_len);
883 884 885 886
		if (rc)
			goto error;

		dev->cmd_pending = 1;
887
		dev->cmd = cmd;
888 889 890
		goto unlock;
	}

891 892
	nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
		    cmd_code);
893 894 895 896 897 898 899

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

	goto unlock;

error:
900
	kfree(cmd);
901 902 903
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
904 905 906 907 908 909 910 911 912
}

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;
913 914 915
	int  resp_len = dev->ops->rx_header_len +
			dev->ops->max_payload_len +
			dev->ops->rx_tail_len;
916 917 918 919 920 921 922 923 924 925 926

	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;
927 928 929 930 931 932 933 934 935
}

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;
936 937 938
	int  resp_len = dev->ops->rx_header_len +
			dev->ops->max_payload_len +
			dev->ops->rx_tail_len;
939

940
	resp = alloc_skb(resp_len, GFP_KERNEL);
941 942 943
	if (!resp)
		return -ENOMEM;

944 945
	rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
				complete_cb_context);
946 947 948 949 950 951
	if (rc)
		dev_kfree_skb(resp);

	return rc;
}

952 953 954 955 956 957 958 959 960 961 962 963 964 965
/*
 * 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;
966
	struct pn533_cmd *cmd;
967
	int rc;
968 969 970
	int resp_len = dev->ops->rx_header_len +
		       dev->ops->max_payload_len +
		       dev->ops->rx_tail_len;
971 972 973 974 975

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

976 977
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd) {
978 979 980 981
		dev_kfree_skb(resp);
		return -ENOMEM;
	}

982
	cmd->code = cmd_code;
983 984 985 986 987
	cmd->req = req;
	cmd->resp = resp;
	cmd->resp_len = resp_len;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
988

989
	pn533_build_cmd_frame(dev, cmd_code, req);
990

991
	rc = __pn533_send_frame_async(dev, req, resp, resp_len);
992 993
	if (rc < 0) {
		dev_kfree_skb(resp);
994
		kfree(cmd);
995 996
	} else {
		dev->cmd = cmd;
997 998 999 1000 1001
	}

	return rc;
}

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
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 已提交
1012 1013 1014 1015
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;
1016
	int rc;
S
Samuel Ortiz 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

	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);

1028 1029
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
1030 1031
	mutex_unlock(&dev->cmd_lock);

1032
	rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1033 1034 1035
	if (rc < 0) {
		dev_kfree_skb(cmd->req);
		dev_kfree_skb(cmd->resp);
1036
		kfree(cmd);
1037
		return;
1038
	}
1039 1040

	dev->cmd = cmd;
S
Samuel Ortiz 已提交
1041 1042
}

1043
struct pn533_sync_cmd_response {
1044
	struct sk_buff *resp;
1045 1046 1047
	struct completion done;
};

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
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;
}

1096 1097 1098 1099 1100 1101
static void pn533_send_complete(struct urb *urb)
{
	struct pn533 *dev = urb->context;

	switch (urb->status) {
	case 0:
1102
		break; /* success */
1103 1104
	case -ECONNRESET:
	case -ENOENT:
1105
		nfc_dev_dbg(&dev->interface->dev,
1106 1107
			    "The urb has been stopped (status %d)",
			    urb->status);
1108
		break;
1109
	case -ESHUTDOWN:
1110
	default:
1111 1112
		nfc_dev_err(&dev->interface->dev,
			    "Urb failure (status %d)", urb->status);
1113 1114 1115
	}
}

1116
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1117 1118 1119
{
	struct sk_buff *skb;

1120
	skb = alloc_skb(dev->ops->tx_header_len +
1121
			size +
1122
			dev->ops->tx_tail_len, GFP_KERNEL);
1123 1124

	if (skb)
1125
		skb_reserve(skb, dev->ops->tx_header_len);
1126 1127 1128 1129

	return skb;
}

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 1162 1163 1164 1165 1166 1167 1168 1169
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 &&
1170 1171 1172
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
		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;

1187
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	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 已提交
1210 1211
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1212 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

	return 0;
}

struct pn533_target_felica {
	u8 pol_res;
	u8 opcode;
	u8 nfcid2[8];
	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;

1245
	tgt_felica = (struct pn533_target_felica *)tgt_data;
1246 1247 1248 1249

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

1250 1251
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1252 1253 1254 1255
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
1256 1257 1258
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	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 &&
1281 1282 1283
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		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;

1294
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1295 1296 1297 1298 1299 1300

	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);
1301 1302
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
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 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

	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;

1353
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1354 1355 1356 1357

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

1358
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1359 1360 1361 1362

	return 0;
}

1363 1364
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
1365 1366 1367 1368 1369
{
	struct nfc_target nfc_tgt;
	int rc;

	nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1370
		    dev->poll_mod_curr);
1371

1372
	if (tg != 1)
1373 1374
		return -EPROTO;

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

1377 1378
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
1379
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1380 1381 1382
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
1383
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1384 1385
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
1386
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1387 1388
		break;
	case PN533_POLL_MOD_847KBPS_B:
1389
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1390 1391
		break;
	default:
1392 1393
		nfc_dev_err(&dev->interface->dev,
			    "Unknown current poll modulation");
1394 1395 1396 1397 1398 1399 1400
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1401 1402
		nfc_dev_dbg(&dev->interface->dev,
			    "The Tg found doesn't have the desired protocol");
1403 1404 1405
		return -EAGAIN;
	}

1406 1407 1408
	nfc_dev_dbg(&dev->interface->dev,
		    "Target found - supported protocols: 0x%x",
		    nfc_tgt.supported_protocols);
1409 1410 1411 1412 1413 1414 1415 1416

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

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

	return 0;
}

S
Samuel Ortiz 已提交
1417 1418 1419 1420 1421
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

1422 1423 1424 1425 1426 1427 1428 1429
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] =
1430
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
1431 1432 1433
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
1434 1435
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
1436 1437 1438
{
	pn533_poll_reset_mod_list(dev);

1439 1440 1441
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1442 1443
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

1444 1445
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1446 1447 1448 1449
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
1450
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
1451 1452
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

1453
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1454 1455
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
1456 1457
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1458 1459
}

1460
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1461
{
1462 1463
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
1464 1465 1466

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

1467 1468 1469 1470 1471 1472 1473
	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);
1474 1475

		/* We must stop the poll after a valid target found */
S
Samuel Ortiz 已提交
1476 1477 1478 1479
		if (rc == 0) {
			pn533_poll_reset_mod_list(dev);
			return 0;
		}
1480 1481
	}

S
Samuel Ortiz 已提交
1482
	return -EAGAIN;
1483 1484
}

1485
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1486
{
1487 1488 1489
	struct sk_buff *skb;
	u8 *felica, *nfcid3, *gb;

1490 1491 1492
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1493 1494 1495 1496
	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 */
1497

1498 1499 1500
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1501

1502 1503 1504 1505
	unsigned int skb_len = 36 + /* mode (1), mifare (6),
				       felica (18), nfcid3 (10), gb_len (1) */
			       gbytes_len +
			       1;  /* len Tk*/
1506

1507
	skb = pn533_alloc_skb(dev, skb_len);
1508 1509
	if (!skb)
		return NULL;
1510 1511

	/* DEP support only */
1512
	*skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1513 1514 1515

	/* MIFARE params */
	memcpy(skb_put(skb, 6), mifare_params, 6);
1516 1517

	/* Felica params */
1518 1519 1520
	felica = skb_put(skb, 18);
	memcpy(felica, felica_params, 18);
	get_random_bytes(felica + 2, 6);
1521 1522

	/* NFCID3 */
1523 1524 1525
	nfcid3 = skb_put(skb, 10);
	memset(nfcid3, 0, 10);
	memcpy(nfcid3, felica, 8);
1526 1527

	/* General bytes */
1528
	*skb_put(skb, 1) = gbytes_len;
1529

1530 1531
	gb = skb_put(skb, gbytes_len);
	memcpy(gb, gbytes, gbytes_len);
1532

1533 1534
	/* Len Tk */
	*skb_put(skb, 1) = 0;
1535

1536
	return skb;
1537 1538
}

1539
#define PN533_CMD_DATAEXCH_HEAD_LEN 1
1540 1541
#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1542
				      struct sk_buff *resp)
1543
{
1544
	u8 status;
1545 1546 1547

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

1548 1549
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1550

1551 1552
	status = resp->data[0];
	skb_pull(resp, sizeof(status));
1553

1554
	if (status != 0) {
1555
		nfc_tm_deactivated(dev->nfc_dev);
S
Samuel Ortiz 已提交
1556
		dev->tgt_mode = 0;
1557
		dev_kfree_skb(resp);
1558 1559 1560
		return 0;
	}

1561
	return nfc_tm_data_received(dev->nfc_dev, resp);
1562 1563 1564 1565 1566 1567
}

static void pn533_wq_tg_get_data(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, tg_work);

1568 1569
	struct sk_buff *skb;
	int rc;
1570

1571
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1572

1573
	skb = pn533_alloc_skb(dev, 0);
1574
	if (!skb)
1575 1576
		return;

1577 1578
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1579

1580 1581
	if (rc < 0)
		dev_kfree_skb(skb);
1582 1583 1584 1585

	return;
}

1586
#define ATR_REQ_GB_OFFSET 17
1587
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1588
{
1589
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1590
	size_t gb_len;
1591
	int rc;
1592 1593 1594

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

1595
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1596 1597
		return -EINVAL;

1598 1599
	mode = resp->data[0];
	cmd = &resp->data[1];
1600

1601 1602
	nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
		    mode, resp->len);
1603

1604 1605
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1606 1607
		comm_mode = NFC_COMM_ACTIVE;

1608
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1609 1610
		return -EOPNOTSUPP;

1611 1612
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1613

1614 1615 1616 1617 1618 1619 1620 1621
	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
			      comm_mode, gb, gb_len);
	if (rc < 0) {
		nfc_dev_err(&dev->interface->dev,
			    "Error when signaling target activation");
		return rc;
	}

S
Samuel Ortiz 已提交
1622
	dev->tgt_mode = 1;
1623 1624 1625
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1626 1627
}

S
Samuel Ortiz 已提交
1628
static void pn533_listen_mode_timer(unsigned long data)
1629
{
1630
	struct pn533 *dev = (struct pn533 *)data;
S
Samuel Ortiz 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

	nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");

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

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

	queue_work(dev->wq, &dev->poll_work);
}

static int pn533_poll_complete(struct pn533 *dev, void *arg,
1645
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1646 1647
{
	struct pn533_poll_modulations *cur_mod;
1648 1649
	int rc;

S
Samuel Ortiz 已提交
1650
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1651

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

		nfc_dev_err(&dev->interface->dev, "%s  Poll complete error %d",
			    __func__, rc);

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
			else
				goto stop_poll;
		} else if (rc < 0) {
			nfc_dev_err(&dev->interface->dev,
				    "Error %d when running poll", rc);
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
1668
	}
1669

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

1672
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1673
		del_timer(&dev->listen_timer);
1674 1675
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1676 1677
	}

1678 1679 1680 1681
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1682

1683 1684 1685 1686 1687
	if (!dev->poll_mod_count) {
		nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
		goto done;
	}

1688
	pn533_poll_next_mod(dev);
S
Samuel Ortiz 已提交
1689 1690
	queue_work(dev->wq, &dev->poll_work);

1691 1692 1693
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1694 1695

stop_poll:
1696 1697
	nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");

S
Samuel Ortiz 已提交
1698 1699
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1700
	return rc;
1701 1702
}

1703 1704
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
1705
{
1706
	struct sk_buff *skb;
1707

1708
	skb = pn533_alloc_skb(dev, mod->len);
1709 1710
	if (!skb)
		return NULL;
1711

1712
	memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1713

1714
	return skb;
S
Samuel Ortiz 已提交
1715
}
1716

S
Samuel Ortiz 已提交
1717 1718
static int pn533_send_poll_frame(struct pn533 *dev)
{
1719 1720
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1721
	int rc;
1722
	u8 cmd_code;
1723

1724
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1725

1726 1727
	nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
		    __func__, mod->len);
1728

1729 1730
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1731
		skb = pn533_alloc_poll_tg_frame(dev);
1732 1733
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1734
		skb = pn533_alloc_poll_in_frame(dev, mod);
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
	}

	if (!skb) {
		nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
		return -ENOMEM;
	}

	rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
				  NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
S
Samuel Ortiz 已提交
1746
		nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1747
	}
1748

S
Samuel Ortiz 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, poll_work);
	struct pn533_poll_modulations *cur_mod;
	int rc;

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

	nfc_dev_dbg(&dev->interface->dev,
		    "%s cancel_listen %d modulation len %d",
		    __func__, dev->cancel_listen, cur_mod->len);

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
		usb_kill_urb(dev->in_urb);
1767 1768
	}

S
Samuel Ortiz 已提交
1769 1770 1771
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1772

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

S
Samuel Ortiz 已提交
1776
	return;
1777 1778
}

1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

	nfc_dev_dbg(&dev->interface->dev,
		    "%s: im protocols 0x%x tm protocols 0x%x",
		    __func__, im_protocols, tm_protocols);

	if (dev->tgt_active_prot) {
		nfc_dev_err(&dev->interface->dev,
			    "Cannot poll with a target already activated");
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1794 1795 1796 1797 1798 1799
	if (dev->tgt_mode) {
		nfc_dev_err(&dev->interface->dev,
			    "Cannot poll while already being activated");
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1800 1801 1802 1803 1804
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1805

S
Samuel Ortiz 已提交
1806 1807 1808 1809
	dev->poll_mod_curr = 0;
	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
1810

S
Samuel Ortiz 已提交
1811
	return pn533_send_poll_frame(dev);
1812 1813
}

1814 1815 1816 1817 1818 1819
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

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

S
Samuel Ortiz 已提交
1820 1821
	del_timer(&dev->listen_timer);

1822
	if (!dev->poll_mod_count) {
1823 1824
		nfc_dev_dbg(&dev->interface->dev,
			    "Polling operation was not running");
1825 1826 1827 1828 1829 1830 1831 1832
		return;
	}

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

	/* prevent pn533_start_poll_complete to issue a new poll meanwhile */
	usb_kill_urb(dev->in_urb);
1833 1834

	pn533_poll_reset_mod_list(dev);
1835 1836 1837 1838
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1839
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1840
	u16 gt_len;
1841 1842
	int rc;

1843 1844
	struct sk_buff *skb;
	struct sk_buff *resp;
1845

1846
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1847

1848
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1849 1850
	if (!skb)
		return -ENOMEM;
1851

1852 1853
	*skb_put(skb, sizeof(u8)) = 1; /* TG */
	*skb_put(skb, sizeof(u8)) = 0; /* Next */
1854

1855 1856 1857
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1858

1859
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1860
	rc = rsp->status & PN533_CMD_RET_MASK;
1861
	if (rc != PN533_CMD_RET_SUCCESS) {
1862 1863
		nfc_dev_err(&dev->interface->dev,
			    "Target activation failed (error 0x%x)", rc);
1864
		dev_kfree_skb(resp);
1865
		return -EIO;
1866
	}
1867

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

1872
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1873
	return rc;
1874 1875
}

1876 1877
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1878 1879 1880 1881 1882
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

	nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1883
		    protocol);
1884 1885

	if (dev->poll_mod_count) {
1886 1887
		nfc_dev_err(&dev->interface->dev,
			    "Cannot activate while polling");
1888 1889 1890 1891
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1892 1893
		nfc_dev_err(&dev->interface->dev,
			    "There is already an active target");
1894 1895 1896 1897
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1898 1899
		nfc_dev_err(&dev->interface->dev,
			    "There is no available target to activate");
1900 1901 1902 1903
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1904 1905 1906
		nfc_dev_err(&dev->interface->dev,
			    "Target doesn't support requested proto %u",
			    protocol);
1907 1908 1909 1910 1911 1912
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1913 1914
			nfc_dev_err(&dev->interface->dev,
				    "Activating target with DEP failed %d", rc);
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
			return rc;
		}
	}

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

	return 0;
}

1925 1926
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
				    struct nfc_target *target)
1927 1928
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1929 1930 1931 1932

	struct sk_buff *skb;
	struct sk_buff *resp;

1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
	int rc;

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

	if (!dev->tgt_active_prot) {
		nfc_dev_err(&dev->interface->dev, "There is no active target");
		return;
	}

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

1945
	skb = pn533_alloc_skb(dev, sizeof(u8));
1946 1947
	if (!skb)
		return;
1948

1949
	*skb_put(skb, 1) = 1; /* TG*/
1950

1951 1952
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
	if (IS_ERR(resp))
1953 1954
		return;

1955
	rc = resp->data[0] & PN533_CMD_RET_MASK;
1956
	if (rc != PN533_CMD_RET_SUCCESS)
1957 1958
		nfc_dev_err(&dev->interface->dev,
			    "Error 0x%x when releasing the target", rc);
1959

1960
	dev_kfree_skb(resp);
1961 1962 1963
	return;
}

1964 1965

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1966
					 struct sk_buff *resp)
1967
{
1968
	struct pn533_cmd_jump_dep_response *rsp;
1969 1970
	u8 target_gt_len;
	int rc;
1971
	u8 active = *(u8 *)arg;
1972 1973

	kfree(arg);
1974

1975 1976
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1977 1978 1979 1980

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
		nfc_dev_err(&dev->interface->dev,
1981
			    "The target does not support DEP");
1982 1983
		rc =  -EINVAL;
		goto error;
1984 1985
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1989 1990
	if (rc != PN533_CMD_RET_SUCCESS) {
		nfc_dev_err(&dev->interface->dev,
1991
			    "Bringing DEP link up failed (error 0x%x)", rc);
1992
		goto error;
1993 1994 1995
	}

	if (!dev->tgt_available_prots) {
1996 1997
		struct nfc_target nfc_target;

1998 1999 2000
		nfc_dev_dbg(&dev->interface->dev, "Creating new target");

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2001
		nfc_target.nfcid1_len = 10;
2002
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2003 2004
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
2005
			goto error;
2006 2007 2008 2009 2010 2011 2012

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
2013
	target_gt_len = resp->len - 17;
2014
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2015
					  rsp->gt, target_gt_len);
2016 2017
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
2018 2019
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
2020

2021 2022 2023
error:
	dev_kfree_skb(resp);
	return rc;
2024 2025
}

2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
static int pn533_mod_to_baud(struct pn533 *dev)
{
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
		return 0;
	case PN533_POLL_MOD_212KBPS_FELICA:
		return 1;
	case PN533_POLL_MOD_424KBPS_FELICA:
		return 2;
	default:
		return -EINVAL;
	}
}

2040
#define PASSIVE_DATA_LEN 5
2041
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2042
			     u8 comm_mode, u8 *gb, size_t gb_len)
2043 2044
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2045 2046 2047 2048
	struct sk_buff *skb;
	int rc, baud, skb_len;
	u8 *next, *arg;

2049
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2050 2051 2052 2053 2054

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

	if (dev->poll_mod_count) {
		nfc_dev_err(&dev->interface->dev,
2055
			    "Cannot bring the DEP link up while polling");
2056 2057 2058 2059 2060
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
		nfc_dev_err(&dev->interface->dev,
2061
			    "There is already an active target");
2062 2063 2064
		return -EBUSY;
	}

2065 2066 2067 2068 2069 2070 2071
	baud = pn533_mod_to_baud(dev);
	if (baud < 0) {
		nfc_dev_err(&dev->interface->dev,
			    "Invalid curr modulation %d", dev->poll_mod_curr);
		return baud;
	}

2072
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
2073
	if (comm_mode == NFC_COMM_PASSIVE)
2074
		skb_len += PASSIVE_DATA_LEN;
2075

2076
	skb = pn533_alloc_skb(dev, skb_len);
2077
	if (!skb)
2078 2079
		return -ENOMEM;

2080 2081 2082 2083 2084
	*skb_put(skb, 1) = !comm_mode;  /* ActPass */
	*skb_put(skb, 1) = baud;  /* Baud rate */

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

2086 2087 2088 2089
	if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
		memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
		       PASSIVE_DATA_LEN);
		*next |= 1;
2090 2091
	}

2092
	if (gb != NULL && gb_len > 0) {
2093 2094
		memcpy(skb_put(skb, gb_len), gb, gb_len);
		*next |= 4; /* We have some Gi */
2095
	} else {
2096
		*next = 0;
2097 2098
	}

2099 2100 2101 2102 2103
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
2104

2105
	*arg = !comm_mode;
2106

2107 2108 2109 2110 2111 2112 2113
	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);
	}
2114 2115 2116 2117 2118 2119

	return rc;
}

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

2122 2123
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);

S
Samuel Ortiz 已提交
2124 2125
	pn533_poll_reset_mod_list(dev);

S
Samuel Ortiz 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134
	if (dev->tgt_mode || dev->tgt_active_prot) {
		pn533_send_ack(dev, GFP_KERNEL);
		usb_kill_urb(dev->in_urb);
	}

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

	skb_queue_purge(&dev->resp_q);
2135 2136 2137 2138

	return 0;
}

2139 2140 2141 2142 2143
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
2144 2145 2146 2147 2148
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

2149
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
S
Samuel Ortiz 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181

	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;

	nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
		    __func__, skb_len);

	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;
}

2182
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2183
					struct sk_buff *resp)
2184 2185
{
	struct pn533_data_exchange_arg *arg = _arg;
2186 2187 2188
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
2189 2190 2191

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

2192 2193 2194
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
2195 2196
	}

2197 2198 2199 2200 2201
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
2202

2203 2204
	if (ret != PN533_CMD_RET_SUCCESS) {
		nfc_dev_err(&dev->interface->dev,
2205
			    "Exchanging data failed (error 0x%x)", ret);
2206
		rc = -EIO;
2207 2208 2209
		goto error;
	}

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

2212 2213
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
S
Samuel Ortiz 已提交
2214 2215
		queue_work(dev->wq, &dev->mi_work);
		return -EINPROGRESS;
2216 2217
	}

S
Samuel Ortiz 已提交
2218
	skb = pn533_build_response(dev);
2219
	if (!skb)
S
Samuel Ortiz 已提交
2220
		goto error;
2221

S
Samuel Ortiz 已提交
2222
	arg->cb(arg->cb_context, skb, 0);
2223 2224 2225 2226
	kfree(arg);
	return 0;

error:
2227 2228
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2229
	skb_queue_purge(&dev->resp_q);
2230
	arg->cb(arg->cb_context, NULL, rc);
2231
	kfree(arg);
2232
	return rc;
2233 2234
}

S
Samuel Ortiz 已提交
2235 2236 2237
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)
2238 2239
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2240
	struct pn533_data_exchange_arg *arg = NULL;
2241 2242 2243 2244
	int rc;

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

2245 2246 2247 2248 2249 2250 2251 2252 2253
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
		/* TODO: Implement support to multi-part data exchange */
		nfc_dev_err(&dev->interface->dev,
			    "Data length greater than the max allowed: %d",
			    PN533_CMD_DATAEXCH_DATA_MAXLEN);
		rc = -ENOSYS;
		goto error;
	}

2254
	if (!dev->tgt_active_prot) {
2255 2256
		nfc_dev_err(&dev->interface->dev,
			    "Can't exchange data if there is no active target");
2257 2258 2259 2260
		rc = -EINVAL;
		goto error;
	}

2261
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2262 2263
	if (!arg) {
		rc = -ENOMEM;
2264
		goto error;
2265 2266 2267 2268 2269
	}

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

2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
	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:
		*skb_push(skb, sizeof(u8)) =  1; /*TG*/

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

		break;
2288 2289
	}

2290 2291 2292
	if (rc < 0) /* rc from send_async */
		goto error;

2293 2294 2295
	return 0;

error:
2296 2297
	kfree(arg);
	dev_kfree_skb(skb);
2298 2299 2300
	return rc;
}

2301
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2302
				  struct sk_buff *resp)
2303
{
2304
	u8 status;
2305

2306 2307
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);

2308 2309
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2310

2311
	status = resp->data[0];
2312

2313
	dev_kfree_skb(resp);
2314

2315
	if (status != 0) {
2316 2317
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2318 2319
		dev->tgt_mode = 0;

2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
		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;

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

2335
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2336
		nfc_dev_err(&dev->interface->dev,
2337 2338 2339
			    "Data length greater than the max allowed: %d",
			    PN533_CMD_DATAEXCH_DATA_MAXLEN);
		return -ENOSYS;
2340 2341
	}

2342 2343 2344 2345
	rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
				   pn533_tm_send_complete, NULL);
	if (rc < 0)
		dev_kfree_skb(skb);
2346 2347 2348 2349

	return rc;
}

S
Samuel Ortiz 已提交
2350 2351 2352
static void pn533_wq_mi_recv(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_work);
2353 2354

	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2355 2356 2357 2358
	int rc;

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

2359
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2360 2361
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2362

2363 2364 2365 2366 2367 2368 2369 2370
	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 已提交
2371

2372 2373 2374 2375
			break;
		}
	default:
		*skb_put(skb, sizeof(u8)) =  1; /*TG*/
S
Samuel Ortiz 已提交
2376

2377 2378 2379 2380 2381
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2382

2383
		break;
S
Samuel Ortiz 已提交
2384 2385
	}

2386
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2387 2388
		return;

2389 2390
	nfc_dev_err(&dev->interface->dev,
		    "Error %d when trying to perform data_exchange", rc);
S
Samuel Ortiz 已提交
2391

2392
	dev_kfree_skb(skb);
2393
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2394

2395
error:
S
Samuel Ortiz 已提交
2396
	pn533_send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2397
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2398 2399
}

2400 2401 2402
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2403 2404 2405 2406
	struct sk_buff *skb;
	struct sk_buff *resp;

	int skb_len;
2407 2408 2409

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

2410
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2411

2412
	skb = pn533_alloc_skb(dev, skb_len);
2413 2414
	if (!skb)
		return -ENOMEM;
2415

2416 2417
	*skb_put(skb, sizeof(cfgitem)) = cfgitem;
	memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2418

2419 2420 2421
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2422

2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
	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;

2433
	skb = pn533_alloc_skb(dev, 0);
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
	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;
2448 2449
}

2450
static int pn533_pasori_fw_reset(struct pn533 *dev)
2451
{
2452 2453
	struct sk_buff *skb;
	struct sk_buff *resp;
2454 2455 2456

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

2457
	skb = pn533_alloc_skb(dev, sizeof(u8));
2458 2459
	if (!skb)
		return -ENOMEM;
2460

2461
	*skb_put(skb, sizeof(u8)) = 0x1;
2462

2463 2464 2465
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2466

2467
	dev_kfree_skb(resp);
2468

2469
	return 0;
2470 2471
}

2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
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;

	nfc_dev_dbg(&urb->dev->dev, "%s", __func__);

	print_hex_dump(KERN_ERR, "ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
		       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;

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

	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);

	print_hex_dump(KERN_ERR, "ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
		       cmd, sizeof(cmd), false);

	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
	if (rc) {
		nfc_dev_err(&dev->interface->dev,
			    "Reader power on cmd error %d", rc);
		return rc;
	}

	rc =  usb_submit_urb(dev->in_urb, GFP_KERNEL);
	if (rc) {
		nfc_dev_err(&dev->interface->dev,
			    "Can't submit for reader power on cmd response %d",
			    rc);
		return rc;
	}

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

	return arg.rc;
}

2538
static struct nfc_ops pn533_nfc_ops = {
2539 2540
	.dev_up = NULL,
	.dev_down = NULL,
2541 2542
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2543 2544 2545 2546
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2547
	.im_transceive = pn533_transceive,
2548
	.tm_send = pn533_tm_send,
2549 2550
};

2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
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:
		max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
		max_retries.mx_rty_psl = 2;
		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_204;
		timing.dep_timeout = PN533_CONFIG_TIMING_409;

		break;

	case PN533_DEVICE_PASORI:
2572
	case PN533_DEVICE_ACR122U:
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
		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:
		nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
			    dev->device_type);
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
		nfc_dev_err(&dev->interface->dev,
			    "Error on setting MAX_RETRIES config");
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
		nfc_dev_err(&dev->interface->dev,
			    "Error on setting RF timings");
		return rc;
	}

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

	case PN533_DEVICE_PASORI:
2612
		pn533_pasori_fw_reset(dev);
2613 2614 2615 2616 2617 2618 2619 2620 2621

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
			nfc_dev_err(&dev->interface->dev,
				    "Error while settings PASORI config");
			return rc;
		}

2622
		pn533_pasori_fw_reset(dev);
2623 2624 2625 2626 2627 2628 2629

		break;
	}

	return 0;
}

2630 2631 2632
static int pn533_probe(struct usb_interface *interface,
			const struct usb_device_id *id)
{
2633
	struct pn533_fw_version fw_ver;
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
	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;
2649
	mutex_init(&dev->cmd_lock);
2650 2651 2652 2653 2654

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

2655
		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2656 2657
			in_endpoint = endpoint->bEndpointAddress;

2658
		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2659 2660 2661 2662
			out_endpoint = endpoint->bEndpointAddress;
	}

	if (!in_endpoint || !out_endpoint) {
2663 2664
		nfc_dev_err(&interface->dev,
			    "Could not find bulk-in or bulk-out endpoint");
2665 2666 2667 2668 2669 2670 2671
		rc = -ENODEV;
		goto error;
	}

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

2672
	if (!dev->in_urb || !dev->out_urb)
2673 2674 2675
		goto error;

	usb_fill_bulk_urb(dev->in_urb, dev->udev,
2676 2677
			  usb_rcvbulkpipe(dev->udev, in_endpoint),
			  NULL, 0, NULL, dev);
2678
	usb_fill_bulk_urb(dev->out_urb, dev->udev,
2679 2680
			  usb_sndbulkpipe(dev->udev, out_endpoint),
			  NULL, 0, pn533_send_complete, dev);
2681

S
Samuel Ortiz 已提交
2682 2683
	INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
	INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
S
Samuel Ortiz 已提交
2684
	INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2685
	INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
S
Samuel Ortiz 已提交
2686
	INIT_WORK(&dev->poll_work, pn533_wq_poll);
2687
	dev->wq = alloc_ordered_workqueue("pn533", 0);
2688 2689
	if (dev->wq == NULL)
		goto error;
2690

S
Samuel Ortiz 已提交
2691 2692 2693 2694
	init_timer(&dev->listen_timer);
	dev->listen_timer.data = (unsigned long) dev;
	dev->listen_timer.function = pn533_listen_mode_timer;

S
Samuel Ortiz 已提交
2695 2696
	skb_queue_head_init(&dev->resp_q);

S
Samuel Ortiz 已提交
2697 2698
	INIT_LIST_HEAD(&dev->cmd_queue);

2699 2700
	usb_set_intfdata(interface, dev);

2701
	dev->ops = &pn533_std_frame_ops;
2702

2703
	dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
	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;

2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727
	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) {
			nfc_dev_err(&dev->interface->dev,
				    "Couldn't poweron the reader (error %d)",
				    rc);
			goto destroy_wq;
		}
		break;

2728 2729 2730 2731 2732 2733
	default:
		nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
			    dev->device_type);
		rc = -EINVAL;
		goto destroy_wq;
	}
2734

2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
	memset(&fw_ver, 0, sizeof(fw_ver));
	rc = pn533_get_firmware_version(dev, &fw_ver);
	if (rc < 0)
		goto destroy_wq;

	nfc_dev_info(&dev->interface->dev,
		     "NXP PN533 firmware ver %d.%d now attached",
		     fw_ver.ver, fw_ver.rev);


2745
	dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
S
Samuel Ortiz 已提交
2746
					   NFC_SE_NONE,
2747
					   dev->ops->tx_header_len +
2748
					   PN533_CMD_DATAEXCH_HEAD_LEN,
2749
					   dev->ops->tx_tail_len);
2750
	if (!dev->nfc_dev)
2751
		goto destroy_wq;
2752 2753 2754 2755 2756 2757 2758 2759

	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;

2760 2761
	rc = pn533_setup(dev);
	if (rc)
S
Samuel Ortiz 已提交
2762 2763
		goto unregister_nfc_dev;

2764 2765
	return 0;

2766 2767 2768
unregister_nfc_dev:
	nfc_unregister_device(dev->nfc_dev);

2769 2770
free_nfc_dev:
	nfc_free_device(dev->nfc_dev);
2771

2772 2773
destroy_wq:
	destroy_workqueue(dev->wq);
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
error:
	usb_free_urb(dev->in_urb);
	usb_free_urb(dev->out_urb);
	kfree(dev);
	return rc;
}

static void pn533_disconnect(struct usb_interface *interface)
{
	struct pn533 *dev;
S
Samuel Ortiz 已提交
2784
	struct pn533_cmd *cmd, *n;
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794

	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);

2795
	destroy_workqueue(dev->wq);
2796

S
Samuel Ortiz 已提交
2797 2798
	skb_queue_purge(&dev->resp_q);

S
Samuel Ortiz 已提交
2799 2800
	del_timer(&dev->listen_timer);

S
Samuel Ortiz 已提交
2801 2802 2803 2804 2805
	list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
		list_del(&cmd->queue);
		kfree(cmd);
	}

2806 2807 2808 2809
	usb_free_urb(dev->in_urb);
	usb_free_urb(dev->out_urb);
	kfree(dev);

2810
	nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2811 2812 2813 2814 2815 2816 2817 2818 2819
}

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

2820
module_usb_driver(pn533_driver);
2821

2822 2823 2824
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>");
2825 2826 2827
MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");