pn533.c 57.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2011 Instituto Nokia de Tecnologia
 *
 * Authors:
 *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
 *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
 *
 * 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>
31
#include <net/nfc/nfc.h>
32 33 34 35 36 37 38 39 40

#define VERSION "0.1"

#define PN533_VENDOR_ID 0x4CC
#define PN533_PRODUCT_ID 0x2533

#define SCM_VENDOR_ID 0x4E6
#define SCL3711_PRODUCT_ID 0x5591

41 42 43 44 45 46
#define SONY_VENDOR_ID         0x054c
#define PASORI_PRODUCT_ID      0x02e1

#define PN533_DEVICE_STD    0x1
#define PN533_DEVICE_PASORI 0x2

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

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

58
static const struct usb_device_id pn533_table[] = {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	{ .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,
	},
74 75 76 77
	{ }
};
MODULE_DEVICE_TABLE(usb, pn533_table);

S
Samuel Ortiz 已提交
78 79 80
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2

81
/* frame definitions */
82 83 84
#define PN533_NORMAL_FRAME_MAX_LEN 262  /* 6   (PREAMBLE, SOF, LEN, LCS, TFI)
					   254 (DATA)
					   2   (DCS, postamble) */
85 86 87
#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
					+ 2) /* data[0] TFI, data[1] CC */
#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
88

89 90 91 92 93 94
/*
 * Max extended frame payload len, excluding TFI and CC
 * which are already in PN533_FRAME_HEADER_LEN.
 */
#define PN533_FRAME_MAX_PAYLOAD_LEN 263

95
#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
96
				PN533_FRAME_TAIL_LEN)
97 98
#define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
				  Postamble (1) */
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])

/* start of frame */
#define PN533_SOF 0x00FF

/* frame identifier: in/out/error */
#define PN533_FRAME_IDENTIFIER(f) (f->data[0])
#define PN533_DIR_OUT 0xD4
#define PN533_DIR_IN 0xD5

/* PN533 Commands */
#define PN533_FRAME_CMD(f) (f->data[1])
#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)

#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
#define PN533_CMD_RF_CONFIGURATION 0x32
#define PN533_CMD_IN_DATA_EXCHANGE 0x40
118
#define PN533_CMD_IN_COMM_THRU     0x42
119 120 121
#define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
#define PN533_CMD_IN_ATR 0x50
#define PN533_CMD_IN_RELEASE 0x52
122
#define PN533_CMD_IN_JUMP_FOR_DEP 0x56
123

124
#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
125
#define PN533_CMD_TG_GET_DATA 0x86
126
#define PN533_CMD_TG_SET_DATA 0x8e
127
#define PN533_CMD_UNDEF 0xff
128

129 130 131 132 133 134 135 136 137 138 139 140
#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;

typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
					u8 *params, int params_len);

141 142 143
typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
					struct sk_buff *resp);

144 145 146 147 148 149 150 151 152 153 154
/* 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 已提交
155
#define PN533_CFGITEM_TIMING 0x02
156
#define PN533_CFGITEM_MAX_RETRIES 0x05
157
#define PN533_CFGITEM_PASORI 0x82
158

S
Samuel Ortiz 已提交
159 160 161 162 163
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe

164 165 166 167 168 169 170 171 172
#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 已提交
173 174 175 176 177 178
struct pn533_config_timing {
	u8 rfu;
	u8 atr_res_timeout;
	u8 dep_timeout;
} __packed;

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/* 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 已提交
215
	PN533_LISTEN_MOD,
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 242 243 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

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

const struct pn533_poll_modulations poll_mod[] = {
	[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 已提交
283 284 285
	[PN533_LISTEN_MOD] = {
		.len = 0,
	},
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
};

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

302 303 304 305 306 307 308 309 310 311 312 313
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;
314

315 316 317 318 319

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

320 321 322 323
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

324 325 326 327 328 329 330 331 332 333
struct pn533 {
	struct usb_device *udev;
	struct usb_interface *interface;
	struct nfc_dev *nfc_dev;

	struct urb *out_urb;
	struct pn533_frame *out_frame;

	struct urb *in_urb;

S
Samuel Ortiz 已提交
334 335
	struct sk_buff_head resp_q;

336 337
	struct workqueue_struct	*wq;
	struct work_struct cmd_work;
S
Samuel Ortiz 已提交
338
	struct work_struct cmd_complete_work;
S
Samuel Ortiz 已提交
339
	struct work_struct poll_work;
S
Samuel Ortiz 已提交
340
	struct work_struct mi_work;
341
	struct work_struct tg_work;
S
Samuel Ortiz 已提交
342
	struct timer_list listen_timer;
343 344
	struct pn533_frame *wq_in_frame;
	int wq_in_error;
S
Samuel Ortiz 已提交
345
	int cancel_listen;
346 347 348

	pn533_cmd_complete_t cmd_complete;
	void *cmd_complete_arg;
349
	void *cmd_complete_mi_arg;
350
	struct mutex cmd_lock;
351 352 353 354 355 356
	u8 cmd;

	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 已提交
357 358 359 360
	u32 listen_protocols;

	u8 *gb;
	size_t gb_len;
361 362 363

	u8 tgt_available_prots;
	u8 tgt_active_prot;
S
Samuel Ortiz 已提交
364
	u8 tgt_mode;
365 366

	u32 device_type;
S
Samuel Ortiz 已提交
367 368 369 370 371 372 373

	struct list_head cmd_queue;
	u8 cmd_pending;
};

struct pn533_cmd {
	struct list_head queue;
374 375 376
	u8 cmd_code;
	struct sk_buff *req;
	struct sk_buff *resp;
S
Samuel Ortiz 已提交
377
	void *arg;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 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
};

struct pn533_frame {
	u8 preamble;
	__be16 start_frame;
	u8 datalen;
	u8 datalen_checksum;
	u8 data[];
} __packed;

/* The rule: value + checksum = 0 */
static inline u8 pn533_checksum(u8 value)
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
static u8 pn533_data_checksum(u8 *data, int datalen)
{
	u8 sum = 0;
	int i;

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

	return pn533_checksum(sum);
}

static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
{
	frame->preamble = 0;
	frame->start_frame = cpu_to_be16(PN533_SOF);
	PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
	PN533_FRAME_CMD(frame) = cmd;
	frame->datalen = 2;
}

static void pn533_tx_frame_finish(struct pn533_frame *frame)
{
	frame->datalen_checksum = pn533_checksum(frame->datalen);

	PN533_FRAME_CHECKSUM(frame) =
		pn533_data_checksum(frame->data, frame->datalen);

	PN533_FRAME_POSTAMBLE(frame) = 0;
}

static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
{
	u8 checksum;

	if (frame->start_frame != cpu_to_be16(PN533_SOF))
		return false;

	checksum = pn533_checksum(frame->datalen);
	if (checksum != frame->datalen_checksum)
		return false;

	checksum = pn533_data_checksum(frame->data, frame->datalen);
	if (checksum != PN533_FRAME_CHECKSUM(frame))
		return false;

	return true;
}

static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
{
	if (frame->start_frame != cpu_to_be16(PN533_SOF))
		return false;

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

	return true;
}

static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
{
	return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
}

459 460

static void pn533_wq_cmd_complete(struct work_struct *work)
461
{
S
Samuel Ortiz 已提交
462
	struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
463
	struct pn533_frame *in_frame;
464 465
	int rc;

466 467 468
	in_frame = dev->wq_in_frame;

	if (dev->wq_in_error)
469
		rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
470
							dev->wq_in_error);
471 472 473 474 475 476
	else
		rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
					PN533_FRAME_CMD_PARAMS_PTR(in_frame),
					PN533_FRAME_CMD_PARAMS_LEN(in_frame));

	if (rc != -EINPROGRESS)
S
Samuel Ortiz 已提交
477
		queue_work(dev->wq, &dev->cmd_work);
478 479 480 481 482 483 484
}

static void pn533_recv_response(struct urb *urb)
{
	struct pn533 *dev = urb->context;
	struct pn533_frame *in_frame;

485
	dev->wq_in_frame = NULL;
486 487 488

	switch (urb->status) {
	case 0:
489
		break; /* success */
490 491
	case -ECONNRESET:
	case -ENOENT:
492
		nfc_dev_dbg(&dev->interface->dev,
493 494
			    "The urb has been canceled (status %d)",
			    urb->status);
495 496
		dev->wq_in_error = urb->status;
		goto sched_wq;
497 498
		break;
	case -ESHUTDOWN:
499
	default:
500
		nfc_dev_err(&dev->interface->dev,
501
			    "Urb failure (status %d)", urb->status);
502 503
		dev->wq_in_error = urb->status;
		goto sched_wq;
504 505 506 507
	}

	in_frame = dev->in_urb->transfer_buffer;

508
	nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
509 510 511
	print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
		       in_frame, PN533_FRAME_SIZE(in_frame), false);

512 513
	if (!pn533_rx_frame_is_valid(in_frame)) {
		nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
514 515
		dev->wq_in_error = -EIO;
		goto sched_wq;
516 517 518
	}

	if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
519 520
		nfc_dev_err(&dev->interface->dev,
			    "It it not the response to the last command");
521 522
		dev->wq_in_error = -EIO;
		goto sched_wq;
523 524
	}

525 526
	dev->wq_in_error = 0;
	dev->wq_in_frame = in_frame;
527

528
sched_wq:
S
Samuel Ortiz 已提交
529
	queue_work(dev->wq, &dev->cmd_complete_work);
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
}

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;
	struct pn533_frame *in_frame;
	int rc;

	switch (urb->status) {
	case 0:
547
		break; /* success */
548 549
	case -ECONNRESET:
	case -ENOENT:
550
		nfc_dev_dbg(&dev->interface->dev,
551 552
			    "The urb has been stopped (status %d)",
			    urb->status);
553 554
		dev->wq_in_error = urb->status;
		goto sched_wq;
555 556
		break;
	case -ESHUTDOWN:
557
	default:
558
		nfc_dev_err(&dev->interface->dev,
559
			    "Urb failure (status %d)", urb->status);
560 561
		dev->wq_in_error = urb->status;
		goto sched_wq;
562 563 564 565 566 567
	}

	in_frame = dev->in_urb->transfer_buffer;

	if (!pn533_rx_frame_is_ack(in_frame)) {
		nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
568 569
		dev->wq_in_error = -EIO;
		goto sched_wq;
570 571 572 573
	}

	rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
	if (rc) {
574 575
		nfc_dev_err(&dev->interface->dev,
			    "usb_submit_urb failed with result %d", rc);
576 577
		dev->wq_in_error = rc;
		goto sched_wq;
578 579 580 581
	}

	return;

582 583
sched_wq:
	dev->wq_in_frame = NULL;
S
Samuel Ortiz 已提交
584
	queue_work(dev->wq, &dev->cmd_complete_work);
585 586 587 588 589 590 591 592 593 594 595
}

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)
{
596 597
	u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
	/* spec 7.1.1.3:  Preamble, SoPC (2), ACK Code (2), Postamble */
598 599 600 601
	int rc;

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

602 603
	dev->out_urb->transfer_buffer = ack;
	dev->out_urb->transfer_buffer_length = sizeof(ack);
604 605 606 607 608
	rc = usb_submit_urb(dev->out_urb, flags);

	return rc;
}

609 610 611 612
static int __pn533_send_frame_async(struct pn533 *dev,
					struct sk_buff *out,
					struct sk_buff *in,
					int in_len,
613
					pn533_cmd_complete_t cmd_complete,
614
					void *arg)
615 616 617
{
	int rc;

618
	dev->cmd = PN533_FRAME_CMD(((struct pn533_frame *)out->data));
619 620 621
	dev->cmd_complete = cmd_complete;
	dev->cmd_complete_arg = arg;

622 623
	dev->out_urb->transfer_buffer = out->data;
	dev->out_urb->transfer_buffer_length = out->len;
624

625 626
	dev->in_urb->transfer_buffer = in->data;
	dev->in_urb->transfer_buffer_length = in_len;
627

628
	print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
629
		       out->data, out->len, false);
630

631
	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
632 633 634
	if (rc)
		return rc;

635
	rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
636 637 638 639 640 641 642 643 644 645
	if (rc)
		goto error;

	return 0;

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

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
{
	struct pn533_frame *frame;
	/* payload is already there, just update datalen */
	int payload_len = skb->len;

	skb_push(skb, PN533_FRAME_HEADER_LEN);
	skb_put(skb, PN533_FRAME_TAIL_LEN);

	frame = (struct pn533_frame *)skb->data;

	pn533_tx_frame_init(frame, cmd_code);
	frame->datalen += payload_len;
	pn533_tx_frame_finish(frame);
}

struct pn533_send_async_complete_arg {
	pn533_send_async_complete_t  complete_cb;
	void *complete_cb_context;
	struct sk_buff *resp;
	struct sk_buff *req;
};

static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
				     int params_len)
{
	struct pn533_send_async_complete_arg *arg = _arg;

	struct sk_buff *req = arg->req;
	struct sk_buff *resp = arg->resp;

	struct pn533_frame *frame = (struct pn533_frame *)resp->data;
	int rc;

	dev_kfree_skb(req);

	if (params_len < 0) {
		arg->complete_cb(dev, arg->complete_cb_context,
				 ERR_PTR(params_len));
		rc = params_len;
		dev_kfree_skb(resp);
		goto out;
	}

	skb_put(resp, PN533_FRAME_SIZE(frame));
	skb_pull(resp, PN533_FRAME_HEADER_LEN);
	skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);

	rc = arg->complete_cb(dev, arg->complete_cb_context, resp);

out:
	kfree(arg);
	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;
	struct pn533_send_async_complete_arg *arg;
	int rc = 0;

711
	nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

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

	arg->complete_cb = complete_cb;
	arg->complete_cb_context = complete_cb_context;
	arg->resp = resp;
	arg->req = req;

	pn533_build_cmd_frame(cmd_code, req);

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
727 728
		rc = __pn533_send_frame_async(dev, req, resp, resp_len,
					      pn533_send_async_complete, arg);
729 730 731 732 733 734 735
		if (rc)
			goto error;

		dev->cmd_pending = 1;
		goto unlock;
	}

736 737
	nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
		    cmd_code);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

	cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
	if (!cmd) {
		rc = -ENOMEM;
		goto error;
	}

	INIT_LIST_HEAD(&cmd->queue);
	cmd->cmd_code = cmd_code;
	cmd->req = req;
	cmd->resp = resp;
	cmd->arg = arg;

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

	goto unlock;

error:
	kfree(arg);
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
}

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;
	int  resp_len = PN533_FRAME_HEADER_LEN +
			PN533_FRAME_MAX_PAYLOAD_LEN +
			PN533_FRAME_TAIL_LEN;

	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;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
}

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;

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

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

	return rc;
}

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
/*
 * 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 pn533_send_async_complete_arg *arg;
	struct sk_buff *resp;
	int rc;
	int resp_len = PN533_FRAME_HEADER_LEN +
		       PN533_FRAME_MAX_PAYLOAD_LEN +
		       PN533_FRAME_TAIL_LEN;

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

	arg = kzalloc(sizeof(arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(resp);
		return -ENOMEM;
	}

	arg->complete_cb = complete_cb;
	arg->complete_cb_context = complete_cb_context;
	arg->resp = resp;
	arg->req = req;

	pn533_build_cmd_frame(cmd_code, req);

843 844
	rc = __pn533_send_frame_async(dev, req, resp, resp_len,
				      pn533_send_async_complete, arg);
845 846 847 848 849 850 851 852
	if (rc < 0) {
		dev_kfree_skb(resp);
		kfree(arg);
	}

	return rc;
}

S
Samuel Ortiz 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;

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

868 869
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
870 871
	mutex_unlock(&dev->cmd_lock);

872 873 874 875
	__pn533_send_frame_async(dev, cmd->req, cmd->resp,
				 PN533_NORMAL_FRAME_MAX_LEN,
				 pn533_send_async_complete,
				 cmd->arg);
S
Samuel Ortiz 已提交
876 877 878 879

	kfree(cmd);
}

880
struct pn533_sync_cmd_response {
881
	struct sk_buff *resp;
882 883 884
	struct completion done;
};

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
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;
}

933 934 935 936 937 938
static void pn533_send_complete(struct urb *urb)
{
	struct pn533 *dev = urb->context;

	switch (urb->status) {
	case 0:
939
		break; /* success */
940 941
	case -ECONNRESET:
	case -ENOENT:
942
		nfc_dev_dbg(&dev->interface->dev,
943 944
			    "The urb has been stopped (status %d)",
			    urb->status);
945
		break;
946
	case -ESHUTDOWN:
947
	default:
948 949
		nfc_dev_err(&dev->interface->dev,
			    "Urb failure (status %d)", urb->status);
950 951 952
	}
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966
static struct sk_buff *pn533_alloc_skb(unsigned int size)
{
	struct sk_buff *skb;

	skb = alloc_skb(PN533_FRAME_HEADER_LEN +
			size +
			PN533_FRAME_TAIL_LEN, GFP_KERNEL);

	if (skb)
		skb_reserve(skb, PN533_FRAME_HEADER_LEN);

	return skb;
}

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
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 &&
1007 1008 1009
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
		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;

1024
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

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

	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;

1082
	tgt_felica = (struct pn533_target_felica *)tgt_data;
1083 1084 1085 1086

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

1087 1088
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1089 1090 1091 1092
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
1093 1094 1095
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	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 &&
1118 1119 1120
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		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;

1131
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1132 1133 1134 1135 1136 1137

	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);
1138 1139
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
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 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

	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;

1190
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1191 1192 1193 1194

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

1195
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1196 1197 1198 1199

	return 0;
}

1200 1201
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
1202 1203 1204 1205 1206
{
	struct nfc_target nfc_tgt;
	int rc;

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

1209
	if (tg != 1)
1210 1211
		return -EPROTO;

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

1214 1215
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
1216
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1217 1218 1219
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
1220
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1221 1222
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
1223
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1224 1225
		break;
	case PN533_POLL_MOD_847KBPS_B:
1226
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1227 1228
		break;
	default:
1229 1230
		nfc_dev_err(&dev->interface->dev,
			    "Unknown current poll modulation");
1231 1232 1233 1234 1235 1236 1237
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1238 1239
		nfc_dev_dbg(&dev->interface->dev,
			    "The Tg found doesn't have the desired protocol");
1240 1241 1242
		return -EAGAIN;
	}

1243 1244 1245
	nfc_dev_dbg(&dev->interface->dev,
		    "Target found - supported protocols: 0x%x",
		    nfc_tgt.supported_protocols);
1246 1247 1248 1249 1250 1251 1252 1253

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

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

	return 0;
}

S
Samuel Ortiz 已提交
1254 1255 1256 1257 1258
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

1259 1260 1261 1262 1263 1264 1265 1266
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] =
1267
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
1268 1269 1270
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
1271 1272
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
1273 1274 1275
{
	pn533_poll_reset_mod_list(dev);

1276 1277 1278
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1279 1280
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

1281 1282
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1283 1284 1285 1286
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
1287
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
1288 1289
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

1290
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1291 1292
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
1293 1294
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1295 1296
}

1297
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1298
{
1299 1300
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
1301 1302 1303

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

1304 1305 1306 1307 1308 1309 1310
	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);
1311 1312

		/* We must stop the poll after a valid target found */
S
Samuel Ortiz 已提交
1313 1314 1315 1316
		if (rc == 0) {
			pn533_poll_reset_mod_list(dev);
			return 0;
		}
1317 1318
	}

S
Samuel Ortiz 已提交
1319
	return -EAGAIN;
1320 1321
}

1322
static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
1323
{
1324 1325 1326
	struct sk_buff *skb;
	u8 *felica, *nfcid3, *gb;

1327 1328 1329 1330
	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 */
1331

1332 1333 1334
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1335

1336 1337 1338 1339
	unsigned int skb_len = 36 + /* mode (1), mifare (6),
				       felica (18), nfcid3 (10), gb_len (1) */
			       gbytes_len +
			       1;  /* len Tk*/
1340

1341 1342 1343
	skb = pn533_alloc_skb(skb_len);
	if (!skb)
		return NULL;
1344 1345

	/* DEP support only */
1346 1347 1348 1349
	*skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;

	/* MIFARE params */
	memcpy(skb_put(skb, 6), mifare_params, 6);
1350 1351

	/* Felica params */
1352 1353 1354
	felica = skb_put(skb, 18);
	memcpy(felica, felica_params, 18);
	get_random_bytes(felica + 2, 6);
1355 1356

	/* NFCID3 */
1357 1358 1359
	nfcid3 = skb_put(skb, 10);
	memset(nfcid3, 0, 10);
	memcpy(nfcid3, felica, 8);
1360 1361

	/* General bytes */
1362
	*skb_put(skb, 1) = gbytes_len;
1363

1364 1365
	gb = skb_put(skb, gbytes_len);
	memcpy(gb, gbytes, gbytes_len);
1366

1367 1368
	/* Len Tk */
	*skb_put(skb, 1) = 0;
1369

1370
	return skb;
1371 1372
}

1373
#define PN533_CMD_DATAEXCH_HEAD_LEN 1
1374 1375
#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1376
				      struct sk_buff *resp)
1377
{
1378
	u8 status;
1379 1380 1381

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

1382 1383
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1384

1385 1386
	status = resp->data[0];
	skb_pull(resp, sizeof(status));
1387

1388
	if (status != 0) {
1389
		nfc_tm_deactivated(dev->nfc_dev);
S
Samuel Ortiz 已提交
1390
		dev->tgt_mode = 0;
1391
		dev_kfree_skb(resp);
1392 1393 1394
		return 0;
	}

1395
	return nfc_tm_data_received(dev->nfc_dev, resp);
1396 1397 1398 1399 1400 1401
}

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

1402 1403
	struct sk_buff *skb;
	int rc;
1404

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

1407 1408
	skb = pn533_alloc_skb(0);
	if (!skb)
1409 1410
		return;

1411 1412
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1413

1414 1415
	if (rc < 0)
		dev_kfree_skb(skb);
1416 1417 1418 1419

	return;
}

1420
#define ATR_REQ_GB_OFFSET 17
1421
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1422
{
1423
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1424
	size_t gb_len;
1425
	int rc;
1426 1427 1428

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

1429
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1430 1431
		return -EINVAL;

1432 1433
	mode = resp->data[0];
	cmd = &resp->data[1];
1434

1435 1436
	nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
		    mode, resp->len);
1437

1438 1439
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1440 1441
		comm_mode = NFC_COMM_ACTIVE;

1442
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1443 1444
		return -EOPNOTSUPP;

1445 1446
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1447

1448 1449 1450 1451 1452 1453 1454 1455
	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 已提交
1456
	dev->tgt_mode = 1;
1457 1458 1459
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1460 1461
}

S
Samuel Ortiz 已提交
1462
static void pn533_listen_mode_timer(unsigned long data)
1463
{
1464
	struct pn533 *dev = (struct pn533 *)data;
S
Samuel Ortiz 已提交
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

	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,
1479
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1480 1481
{
	struct pn533_poll_modulations *cur_mod;
1482 1483
	int rc;

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

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
	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 已提交
1502
	}
1503

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

1506
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1507
		del_timer(&dev->listen_timer);
1508 1509
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1510 1511
	}

1512 1513 1514 1515
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1516

1517
	pn533_poll_next_mod(dev);
S
Samuel Ortiz 已提交
1518 1519
	queue_work(dev->wq, &dev->poll_work);

1520 1521 1522
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1523 1524

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

S
Samuel Ortiz 已提交
1527 1528
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1529
	return rc;
1530 1531
}

1532 1533
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
						 *mod)
1534
{
1535
	struct sk_buff *skb;
1536

1537 1538 1539
	skb = pn533_alloc_skb(mod->len);
	if (!skb)
		return NULL;
1540

1541
	memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1542

1543
	return skb;
S
Samuel Ortiz 已提交
1544
}
1545

S
Samuel Ortiz 已提交
1546 1547
static int pn533_send_poll_frame(struct pn533 *dev)
{
1548 1549
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1550
	int rc;
1551
	u8 cmd_code;
1552

1553
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1554

1555 1556
	nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
		    __func__, mod->len);
1557

1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
		skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
		skb = pn533_alloc_poll_in_frame(mod);
	}

	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 已提交
1575
		nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1576
	}
1577

S
Samuel Ortiz 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
	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);
1596 1597
	}

S
Samuel Ortiz 已提交
1598 1599 1600
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1601

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

S
Samuel Ortiz 已提交
1605
	return;
1606 1607
}

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
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 已提交
1623 1624 1625 1626 1627 1628
	if (dev->tgt_mode) {
		nfc_dev_err(&dev->interface->dev,
			    "Cannot poll while already being activated");
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1629 1630 1631 1632 1633
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1634

S
Samuel Ortiz 已提交
1635 1636 1637 1638
	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;
1639

S
Samuel Ortiz 已提交
1640
	return pn533_send_poll_frame(dev);
1641 1642
}

1643 1644 1645 1646 1647 1648
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 已提交
1649 1650
	del_timer(&dev->listen_timer);

1651
	if (!dev->poll_mod_count) {
1652 1653
		nfc_dev_dbg(&dev->interface->dev,
			    "Polling operation was not running");
1654 1655 1656 1657 1658 1659 1660 1661
		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);
1662 1663

	pn533_poll_reset_mod_list(dev);
1664 1665 1666 1667
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1668
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1669
	u16 gt_len;
1670 1671
	int rc;

1672 1673
	struct sk_buff *skb;
	struct sk_buff *resp;
1674

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

1677 1678 1679
	skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
	if (!skb)
		return -ENOMEM;
1680

1681 1682
	*skb_put(skb, sizeof(u8)) = 1; /* TG */
	*skb_put(skb, sizeof(u8)) = 0; /* Next */
1683

1684 1685 1686
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1687

1688
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1689
	rc = rsp->status & PN533_CMD_RET_MASK;
1690
	if (rc != PN533_CMD_RET_SUCCESS)
1691
		dev_kfree_skb(resp);
1692 1693
		return -EIO;

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

1698
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1699
	return rc;
1700 1701
}

1702 1703
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1704 1705 1706 1707 1708
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

	nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1709
		    protocol);
1710 1711

	if (dev->poll_mod_count) {
1712 1713
		nfc_dev_err(&dev->interface->dev,
			    "Cannot activate while polling");
1714 1715 1716 1717
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1718 1719
		nfc_dev_err(&dev->interface->dev,
			    "There is already an active target");
1720 1721 1722 1723
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1724 1725
		nfc_dev_err(&dev->interface->dev,
			    "There is no available target to activate");
1726 1727 1728 1729
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1730 1731 1732
		nfc_dev_err(&dev->interface->dev,
			    "Target doesn't support requested proto %u",
			    protocol);
1733 1734 1735 1736 1737 1738
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1739 1740
			nfc_dev_err(&dev->interface->dev,
				    "Activating target with DEP failed %d", rc);
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
			return rc;
		}
	}

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

	return 0;
}

1751 1752
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
				    struct nfc_target *target)
1753 1754
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1755 1756 1757 1758

	struct sk_buff *skb;
	struct sk_buff *resp;

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
	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 已提交
1769 1770
	skb_queue_purge(&dev->resp_q);

1771 1772 1773
	skb = pn533_alloc_skb(sizeof(u8));
	if (!skb)
		return;
1774

1775
	*skb_put(skb, 1) = 1; /* TG*/
1776

1777 1778
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
	if (IS_ERR(resp))
1779 1780
		return;

1781
	rc = resp->data[0] & PN533_CMD_RET_MASK;
1782
	if (rc != PN533_CMD_RET_SUCCESS)
1783 1784
		nfc_dev_err(&dev->interface->dev,
			    "Error 0x%x when releasing the target", rc);
1785

1786
	dev_kfree_skb(resp);
1787 1788 1789
	return;
}

1790 1791

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1792
					 struct sk_buff *resp)
1793
{
1794
	struct pn533_cmd_jump_dep_response *rsp;
1795 1796
	u8 target_gt_len;
	int rc;
1797
	u8 active = *(u8 *)arg;
1798 1799

	kfree(arg);
1800

1801 1802
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1803 1804 1805 1806

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
		nfc_dev_err(&dev->interface->dev,
1807
			    "The target does not support DEP");
1808 1809
		rc =  -EINVAL;
		goto error;
1810 1811
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1815 1816
	if (rc != PN533_CMD_RET_SUCCESS) {
		nfc_dev_err(&dev->interface->dev,
1817
			    "Bringing DEP link up failed %d", rc);
1818
		goto error;
1819 1820 1821
	}

	if (!dev->tgt_available_prots) {
1822 1823
		struct nfc_target nfc_target;

1824 1825 1826
		nfc_dev_dbg(&dev->interface->dev, "Creating new target");

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1827
		nfc_target.nfcid1_len = 10;
1828
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1829 1830
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
1831
			goto error;
1832 1833 1834 1835 1836 1837 1838

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
1839
	target_gt_len = resp->len - 17;
1840
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1841
					  rsp->gt, target_gt_len);
1842 1843
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
1844 1845
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
1846

1847 1848 1849
error:
	dev_kfree_skb(resp);
	return rc;
1850 1851
}

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
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;
	}
}

1866
#define PASSIVE_DATA_LEN 5
1867
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1868
			     u8 comm_mode, u8 *gb, size_t gb_len)
1869 1870
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1871 1872 1873 1874
	struct sk_buff *skb;
	int rc, baud, skb_len;
	u8 *next, *arg;

1875
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1876 1877 1878 1879 1880

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

	if (dev->poll_mod_count) {
		nfc_dev_err(&dev->interface->dev,
1881
			    "Cannot bring the DEP link up while polling");
1882 1883 1884 1885 1886
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
		nfc_dev_err(&dev->interface->dev,
1887
			    "There is already an active target");
1888 1889 1890
		return -EBUSY;
	}

1891 1892 1893 1894 1895 1896 1897
	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;
	}

1898
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1899
	if (comm_mode == NFC_COMM_PASSIVE)
1900
		skb_len += PASSIVE_DATA_LEN;
1901

1902 1903
	skb = pn533_alloc_skb(skb_len);
	if (!skb)
1904 1905
		return -ENOMEM;

1906 1907 1908 1909 1910
	*skb_put(skb, 1) = !comm_mode;  /* ActPass */
	*skb_put(skb, 1) = baud;  /* Baud rate */

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

1912 1913 1914 1915
	if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
		memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
		       PASSIVE_DATA_LEN);
		*next |= 1;
1916 1917
	}

1918
	if (gb != NULL && gb_len > 0) {
1919 1920
		memcpy(skb_put(skb, gb_len), gb, gb_len);
		*next |= 4; /* We have some Gi */
1921
	} else {
1922
		*next = 0;
1923 1924
	}

1925 1926 1927 1928 1929
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
1930

1931
	*arg = !comm_mode;
1932

1933 1934 1935 1936 1937 1938 1939
	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);
	}
1940 1941 1942 1943 1944 1945

	return rc;
}

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

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

S
Samuel Ortiz 已提交
1950 1951
	pn533_poll_reset_mod_list(dev);

S
Samuel Ortiz 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960
	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);
1961 1962 1963 1964

	return 0;
}

1965 1966 1967 1968 1969
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
1970 1971 1972 1973 1974
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

1975
	nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
S
Samuel Ortiz 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007

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

2008
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2009
					struct sk_buff *resp)
2010 2011
{
	struct pn533_data_exchange_arg *arg = _arg;
2012 2013 2014
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
2015 2016 2017

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

2018 2019 2020
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
2021 2022
	}

2023 2024 2025 2026 2027
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
2028

2029 2030 2031 2032 2033
	if (ret != PN533_CMD_RET_SUCCESS) {
		nfc_dev_err(&dev->interface->dev,
			    "PN533 reported error %d when exchanging data",
			    ret);
		rc = -EIO;
2034 2035 2036
		goto error;
	}

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

2039 2040
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
S
Samuel Ortiz 已提交
2041 2042
		queue_work(dev->wq, &dev->mi_work);
		return -EINPROGRESS;
2043 2044
	}

S
Samuel Ortiz 已提交
2045
	skb = pn533_build_response(dev);
2046
	if (!skb)
S
Samuel Ortiz 已提交
2047
		goto error;
2048

S
Samuel Ortiz 已提交
2049
	arg->cb(arg->cb_context, skb, 0);
2050 2051 2052 2053
	kfree(arg);
	return 0;

error:
2054 2055
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2056
	skb_queue_purge(&dev->resp_q);
2057
	arg->cb(arg->cb_context, NULL, rc);
2058
	kfree(arg);
2059
	return rc;
2060 2061
}

S
Samuel Ortiz 已提交
2062 2063 2064
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)
2065 2066
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2067
	struct pn533_data_exchange_arg *arg = NULL;
2068 2069 2070 2071
	int rc;

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

2072 2073 2074 2075 2076 2077 2078 2079 2080
	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;
	}

2081
	if (!dev->tgt_active_prot) {
2082 2083
		nfc_dev_err(&dev->interface->dev,
			    "Can't exchange data if there is no active target");
2084 2085 2086 2087
		rc = -EINVAL;
		goto error;
	}

2088
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2089 2090
	if (!arg) {
		rc = -ENOMEM;
2091
		goto error;
2092 2093 2094 2095 2096
	}

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

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
	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;
2115 2116
	}

2117 2118 2119
	if (rc < 0) /* rc from send_async */
		goto error;

2120 2121 2122
	return 0;

error:
2123 2124
	kfree(arg);
	dev_kfree_skb(skb);
2125 2126 2127
	return rc;
}

2128
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2129
				  struct sk_buff *resp)
2130
{
2131
	u8 status;
2132

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

2135 2136
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2137

2138
	status = resp->data[0];
2139

2140
	dev_kfree_skb(resp);
2141

2142
	if (status != 0) {
2143 2144
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2145 2146
		dev->tgt_mode = 0;

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
		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__);

2162
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2163
		nfc_dev_err(&dev->interface->dev,
2164 2165 2166
			    "Data length greater than the max allowed: %d",
			    PN533_CMD_DATAEXCH_DATA_MAXLEN);
		return -ENOSYS;
2167 2168
	}

2169 2170 2171 2172
	rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
				   pn533_tm_send_complete, NULL);
	if (rc < 0)
		dev_kfree_skb(skb);
2173 2174 2175 2176

	return rc;
}

S
Samuel Ortiz 已提交
2177 2178 2179
static void pn533_wq_mi_recv(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_work);
2180 2181

	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2182 2183 2184 2185
	int rc;

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

2186 2187 2188
	skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2189

2190 2191 2192 2193 2194 2195 2196 2197
	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 已提交
2198

2199 2200 2201 2202
			break;
		}
	default:
		*skb_put(skb, sizeof(u8)) =  1; /*TG*/
S
Samuel Ortiz 已提交
2203

2204 2205 2206 2207 2208
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2209

2210
		break;
S
Samuel Ortiz 已提交
2211 2212
	}

2213
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2214 2215
		return;

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

2219 2220
	dev_kfree_skb(skb);
	kfree(dev->cmd_complete_arg);
S
Samuel Ortiz 已提交
2221

2222
error:
S
Samuel Ortiz 已提交
2223
	pn533_send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2224
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2225 2226
}

2227 2228 2229
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2230 2231 2232 2233
	struct sk_buff *skb;
	struct sk_buff *resp;

	int skb_len;
2234 2235 2236

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

2237
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2238

2239 2240 2241
	skb = pn533_alloc_skb(skb_len);
	if (!skb)
		return -ENOMEM;
2242

2243 2244
	*skb_put(skb, sizeof(cfgitem)) = cfgitem;
	memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2245

2246 2247 2248
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2249

2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
	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;

	skb = pn533_alloc_skb(0);
	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;
2275 2276
}

2277 2278
static int pn533_fw_reset(struct pn533 *dev)
{
2279 2280
	struct sk_buff *skb;
	struct sk_buff *resp;
2281 2282 2283

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

2284 2285 2286
	skb = pn533_alloc_skb(sizeof(u8));
	if (!skb)
		return -ENOMEM;
2287

2288
	*skb_put(skb, sizeof(u8)) = 0x1;
2289

2290 2291 2292
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2293

2294
	dev_kfree_skb(resp);
2295

2296
	return 0;
2297 2298 2299
}

static struct nfc_ops pn533_nfc_ops = {
2300 2301
	.dev_up = NULL,
	.dev_down = NULL,
2302 2303
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2304 2305 2306 2307
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2308
	.im_transceive = pn533_transceive,
2309
	.tm_send = pn533_tm_send,
2310 2311
};

2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
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:
		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:
		pn533_fw_reset(dev);

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

		pn533_fw_reset(dev);

		break;
	}

	return 0;
}

2390 2391 2392
static int pn533_probe(struct usb_interface *interface,
			const struct usb_device_id *id)
{
2393
	struct pn533_fw_version fw_ver;
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
	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;
2409
	mutex_init(&dev->cmd_lock);
2410 2411 2412 2413 2414

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

2415
		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2416 2417
			in_endpoint = endpoint->bEndpointAddress;

2418
		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2419 2420 2421 2422
			out_endpoint = endpoint->bEndpointAddress;
	}

	if (!in_endpoint || !out_endpoint) {
2423 2424
		nfc_dev_err(&interface->dev,
			    "Could not find bulk-in or bulk-out endpoint");
2425 2426 2427 2428 2429
		rc = -ENODEV;
		goto error;
	}

	dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2430
	dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2431 2432
	dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);

2433
	if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
2434 2435 2436
		goto error;

	usb_fill_bulk_urb(dev->in_urb, dev->udev,
2437 2438
			  usb_rcvbulkpipe(dev->udev, in_endpoint),
			  NULL, 0, NULL, dev);
2439
	usb_fill_bulk_urb(dev->out_urb, dev->udev,
2440 2441
			  usb_sndbulkpipe(dev->udev, out_endpoint),
			  NULL, 0, pn533_send_complete, dev);
2442

S
Samuel Ortiz 已提交
2443 2444
	INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
	INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
S
Samuel Ortiz 已提交
2445
	INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2446
	INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
S
Samuel Ortiz 已提交
2447
	INIT_WORK(&dev->poll_work, pn533_wq_poll);
2448
	dev->wq = alloc_ordered_workqueue("pn533", 0);
2449 2450
	if (dev->wq == NULL)
		goto error;
2451

S
Samuel Ortiz 已提交
2452 2453 2454 2455
	init_timer(&dev->listen_timer);
	dev->listen_timer.data = (unsigned long) dev;
	dev->listen_timer.function = pn533_listen_mode_timer;

S
Samuel Ortiz 已提交
2456 2457
	skb_queue_head_init(&dev->resp_q);

S
Samuel Ortiz 已提交
2458 2459
	INIT_LIST_HEAD(&dev->cmd_queue);

2460 2461
	usb_set_intfdata(interface, dev);

2462 2463 2464
	memset(&fw_ver, 0, sizeof(fw_ver));
	rc = pn533_get_firmware_version(dev, &fw_ver);
	if (rc < 0)
2465
		goto destroy_wq;
2466

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

2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
	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;

	default:
		nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
			    dev->device_type);
		rc = -EINVAL;
		goto destroy_wq;
	}
2487

2488
	dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2489
					   PN533_FRAME_HEADER_LEN +
2490
					   PN533_CMD_DATAEXCH_HEAD_LEN,
2491
					   PN533_FRAME_TAIL_LEN);
2492
	if (!dev->nfc_dev)
2493
		goto destroy_wq;
2494 2495 2496 2497 2498 2499 2500 2501

	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;

2502 2503
	rc = pn533_setup(dev);
	if (rc)
S
Samuel Ortiz 已提交
2504 2505
		goto unregister_nfc_dev;

2506 2507
	return 0;

2508 2509 2510
unregister_nfc_dev:
	nfc_unregister_device(dev->nfc_dev);

2511 2512
free_nfc_dev:
	nfc_free_device(dev->nfc_dev);
2513

2514 2515
destroy_wq:
	destroy_workqueue(dev->wq);
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
error:
	usb_free_urb(dev->in_urb);
	kfree(dev->out_frame);
	usb_free_urb(dev->out_urb);
	kfree(dev);
	return rc;
}

static void pn533_disconnect(struct usb_interface *interface)
{
	struct pn533 *dev;
S
Samuel Ortiz 已提交
2527
	struct pn533_cmd *cmd, *n;
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537

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

2538
	destroy_workqueue(dev->wq);
2539

S
Samuel Ortiz 已提交
2540 2541
	skb_queue_purge(&dev->resp_q);

S
Samuel Ortiz 已提交
2542 2543
	del_timer(&dev->listen_timer);

S
Samuel Ortiz 已提交
2544 2545 2546 2547 2548
	list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
		list_del(&cmd->queue);
		kfree(cmd);
	}

2549 2550 2551 2552 2553
	usb_free_urb(dev->in_urb);
	kfree(dev->out_frame);
	usb_free_urb(dev->out_urb);
	kfree(dev);

2554
	nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2555 2556 2557 2558 2559 2560 2561 2562 2563
}

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

2564
module_usb_driver(pn533_driver);
2565 2566 2567 2568 2569 2570

MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
			" Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");