pn533.c 63.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3 4
 * Driver for NXP PN533 NFC Chip - core functions
 *
5
 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6
 * Copyright (C) 2012-2013 Tieto Poland
7 8 9 10 11 12 13 14
 */

#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/nfc.h>
#include <linux/netdevice.h>
15
#include <net/nfc/nfc.h>
16
#include "pn533.h"
17

18
#define VERSION "0.3"
19

S
Samuel Ortiz 已提交
20 21
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2
22 23
/* Delay between each poll frame (ms) */
#define PN533_POLL_INTERVAL 10
S
Samuel Ortiz 已提交
24

25 26 27 28 29 30 31 32 33 34 35
/* 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 */
36 37
#define PN533_CFGITEM_RF_FIELD    0x01
#define PN533_CFGITEM_TIMING      0x02
38
#define PN533_CFGITEM_MAX_RETRIES 0x05
39 40
#define PN533_CFGITEM_PASORI      0x82

S
Samuel Ortiz 已提交
41 42 43
#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
#define PN533_CFGITEM_RF_FIELD_ON        0x1
#define PN533_CFGITEM_RF_FIELD_OFF       0x0
44

S
Samuel Ortiz 已提交
45 46 47 48 49
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe

50 51 52 53 54 55 56 57 58
#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 已提交
59 60 61 62 63 64
struct pn533_config_timing {
	u8 rfu;
	u8 atr_res_timeout;
	u8 dep_timeout;
} __packed;

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/* 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;
};

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

103
static const struct pn533_poll_modulations poll_mod[] = {
104 105 106 107 108 109 110 111 112 113 114 115 116 117
	[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,
118
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
119
				.tsn = 0x03,
120 121 122 123 124 125 126 127 128 129 130
			},
		},
		.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,
131
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
132
				.tsn = 0x03,
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			},
		 },
		.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 已提交
156 157 158
	[PN533_LISTEN_MOD] = {
		.len = 0,
	},
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
};

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

175 176 177 178 179 180 181 182 183 184 185 186
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;
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
struct pn532_autopoll_resp {
	u8 type;
	u8 ln;
	u8 tg;
	u8 tgdata[];
};

/* PN532_CMD_IN_AUTOPOLL */
#define PN532_AUTOPOLL_POLLNR_INFINITE	0xff
#define PN532_AUTOPOLL_PERIOD		0x03 /* in units of 150 ms */

#define PN532_AUTOPOLL_TYPE_GENERIC_106		0x00
#define PN532_AUTOPOLL_TYPE_GENERIC_212		0x01
#define PN532_AUTOPOLL_TYPE_GENERIC_424		0x02
#define PN532_AUTOPOLL_TYPE_JEWEL		0x04
#define PN532_AUTOPOLL_TYPE_MIFARE		0x10
#define PN532_AUTOPOLL_TYPE_FELICA212		0x11
#define PN532_AUTOPOLL_TYPE_FELICA424		0x12
#define PN532_AUTOPOLL_TYPE_ISOA		0x20
#define PN532_AUTOPOLL_TYPE_ISOB		0x23
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106	0x40
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212	0x41
#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424	0x42
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106	0x80
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_212	0x81
#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_424	0x82
214 215 216 217 218

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

219 220 221 222
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

223 224 225 226 227 228
/* The rule: value(high byte) + value(low byte) + checksum = 0 */
static inline u8 pn533_ext_checksum(u16 value)
{
	return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
}

229
/* The rule: value + checksum = 0 */
230
static inline u8 pn533_std_checksum(u8 value)
231 232 233 234 235
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
236
static u8 pn533_std_data_checksum(u8 *data, int datalen)
237 238 239 240 241 242 243
{
	u8 sum = 0;
	int i;

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

244
	return pn533_std_checksum(sum);
245 246
}

247
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
248
{
249
	struct pn533_std_frame *frame = _frame;
250

251
	frame->preamble = 0;
252 253
	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
254
	PN533_FRAME_CMD(frame) = cmd_code;
255 256 257
	frame->datalen = 2;
}

258
static void pn533_std_tx_frame_finish(void *_frame)
259
{
260
	struct pn533_std_frame *frame = _frame;
261

262
	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
263

264 265
	PN533_STD_FRAME_CHECKSUM(frame) =
		pn533_std_data_checksum(frame->data, frame->datalen);
266

267
	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
268 269
}

270
static void pn533_std_tx_update_payload_len(void *_frame, int len)
271
{
272
	struct pn533_std_frame *frame = _frame;
273 274 275 276

	frame->datalen += len;
}

277
static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
278 279
{
	u8 checksum;
280
	struct pn533_std_frame *stdf = _frame;
281

282
	if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
283 284
		return false;

285 286
	if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
		/* Standard frame code */
287
		dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
288 289 290 291 292 293 294 295 296 297 298 299

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

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

300 301
		dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;

302 303 304 305 306 307 308 309 310 311
		checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
		if (checksum != eif->datalen_checksum)
			return false;

		/* check data checksum */
		checksum = pn533_std_data_checksum(eif->data,
						   be16_to_cpu(eif->datalen));
		if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
			return false;
	}
312 313 314 315

	return true;
}

316
bool pn533_rx_frame_is_ack(void *_frame)
317
{
318 319
	struct pn533_std_frame *frame = _frame;

320
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
321 322 323 324 325 326 327
		return false;

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

	return true;
}
328
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
329

330
static inline int pn533_std_rx_frame_size(void *frame)
331
{
332
	struct pn533_std_frame *f = frame;
333

334 335 336 337 338 339 340 341
	/* check for Extended Information frame */
	if (PN533_STD_IS_EXTENDED(f)) {
		struct pn533_ext_frame *eif = frame;

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

342 343
	return sizeof(struct pn533_std_frame) + f->datalen +
	       PN533_STD_FRAME_TAIL_LEN;
344 345
}

346
static u8 pn533_std_get_cmd_code(void *frame)
347
{
348
	struct pn533_std_frame *f = frame;
349
	struct pn533_ext_frame *eif = frame;
350

351 352 353 354
	if (PN533_STD_IS_EXTENDED(f))
		return PN533_FRAME_CMD(eif);
	else
		return PN533_FRAME_CMD(f);
355 356
}

357 358 359 360 361 362 363 364
bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
{
	return (dev->ops->get_cmd_code(frame) ==
				PN533_CMD_RESPONSE(dev->cmd->code));
}
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);


365
static struct pn533_frame_ops pn533_std_frame_ops = {
366 367 368 369 370 371 372 373 374 375 376 377 378
	.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,
379 380 381 382
};

static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
				  struct sk_buff *skb)
383 384 385
{
	/* payload is already there, just update datalen */
	int payload_len = skb->len;
386
	struct pn533_frame_ops *ops = dev->ops;
387 388


389 390
	skb_push(skb, ops->tx_header_len);
	skb_put(skb, ops->tx_tail_len);
391

392 393 394
	ops->tx_frame_init(skb->data, cmd_code);
	ops->tx_update_payload_len(skb->data, payload_len);
	ops->tx_frame_finish(skb->data);
395 396
}

397
static int pn533_send_async_complete(struct pn533 *dev)
398
{
399
	struct pn533_cmd *cmd = dev->cmd;
400 401
	struct sk_buff *resp;
	int status, rc = 0;
402

403 404 405 406
	if (!cmd) {
		dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
		goto done;
	}
407

408
	dev_kfree_skb(cmd->req);
409

410 411
	status = cmd->status;
	resp = cmd->resp;
412

413
	if (status < 0) {
414 415
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
416
		dev_kfree_skb(resp);
417
		goto done;
418 419
	}

420 421 422 423 424 425 426 427
	/* when no response is set we got interrupted */
	if (!resp)
		resp = ERR_PTR(-EINTR);

	if (!IS_ERR(resp)) {
		skb_pull(resp, dev->ops->rx_header_len);
		skb_trim(resp, resp->len - dev->ops->rx_tail_len);
	}
428

429
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
430

431
done:
432
	kfree(cmd);
433
	dev->cmd = NULL;
434 435 436 437
	return rc;
}

static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
438
			      struct sk_buff *req,
439 440 441 442 443 444
			      pn533_send_async_complete_t complete_cb,
			      void *complete_cb_context)
{
	struct pn533_cmd *cmd;
	int rc = 0;

445
	dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
446

447 448
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
449 450
		return -ENOMEM;

451
	cmd->code = cmd_code;
452 453 454
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
455

456
	pn533_build_cmd_frame(dev, cmd_code, req);
457 458 459 460

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
461
		dev->cmd = cmd;
462
		rc = dev->phy_ops->send_frame(dev, req);
463 464
		if (rc) {
			dev->cmd = NULL;
465
			goto error;
466
		}
467 468 469 470 471

		dev->cmd_pending = 1;
		goto unlock;
	}

472
	dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
473
		__func__, cmd_code);
474 475 476 477 478 479 480

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

	goto unlock;

error:
481
	kfree(cmd);
482 483 484
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
485 486 487 488 489 490 491
}

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)
{
492
	return __pn533_send_async(dev, cmd_code, req, complete_cb,
493
				complete_cb_context);
494 495 496 497 498 499 500
}

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)
{
501
	return __pn533_send_async(dev, cmd_code, req, complete_cb,
502
				complete_cb_context);
503 504
}

505 506 507
/*
 * pn533_send_cmd_direct_async
 *
W
wengjianfeng 已提交
508
 * The function sends a priority cmd directly to the chip omitting the cmd
509 510 511 512 513 514 515 516 517
 * 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)
{
518
	struct pn533_cmd *cmd;
519 520
	int rc;

521
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
522
	if (!cmd)
523 524
		return -ENOMEM;

525
	cmd->code = cmd_code;
526 527 528
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
529

530
	pn533_build_cmd_frame(dev, cmd_code, req);
531

532
	dev->cmd = cmd;
533
	rc = dev->phy_ops->send_frame(dev, req);
534 535
	if (rc < 0) {
		dev->cmd = NULL;
536
		kfree(cmd);
537
	}
538 539 540 541

	return rc;
}

542 543 544 545 546 547 548 549 550 551
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 已提交
552 553 554 555
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;
556
	int rc;
S
Samuel Ortiz 已提交
557 558 559 560 561 562 563 564 565 566 567

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

568 569
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
570 571
	mutex_unlock(&dev->cmd_lock);

572
	dev->cmd = cmd;
573
	rc = dev->phy_ops->send_frame(dev, cmd->req);
574
	if (rc < 0) {
575
		dev->cmd = NULL;
576
		dev_kfree_skb(cmd->req);
577
		kfree(cmd);
578
		return;
579
	}
580

S
Samuel Ortiz 已提交
581 582
}

583
struct pn533_sync_cmd_response {
584
	struct sk_buff *resp;
585 586 587
	struct completion done;
};

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
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
607
 *     as it's been already freed at the beginning of RX path by
608 609
 *     async_complete_cb.
 *
W
wengjianfeng 已提交
610
 *  3. valid pointer in case of successful RX path
611 612 613 614
 *
 *  A caller has to check a return value with IS_ERR macro. If the test pass,
 *  the returned pointer is valid.
 *
615
 */
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
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;
}

636
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
637 638 639
{
	struct sk_buff *skb;

640
	skb = alloc_skb(dev->ops->tx_header_len +
641
			size +
642
			dev->ops->tx_tail_len, GFP_KERNEL);
643 644

	if (skb)
645
		skb_reserve(skb, dev->ops->tx_header_len);
646 647 648 649

	return skb;
}

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

682 683 684 685
	/*
	 * The length check of nfcid[] and ats[] are not being performed because
	 * the values are not being used
	 */
686 687 688 689 690 691

	/* 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 &&
692 693 694
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
695 696 697 698 699 700
		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;

701 702 703
	if (type_a->nfcid_len > NFC_NFCID1_MAXSIZE)
		return false;

704 705 706 707 708 709 710 711
	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;

712
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

	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 已提交
735 736
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
737 738 739 740 741 742 743

	return 0;
}

struct pn533_target_felica {
	u8 pol_res;
	u8 opcode;
744
	u8 nfcid2[NFC_NFCID2_MAXSIZE];
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	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;

770
	tgt_felica = (struct pn533_target_felica *)tgt_data;
771 772 773 774

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

775 776
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
777 778 779 780
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
781 782 783
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

784 785 786
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	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 &&
809 810 811
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
812 813 814 815 816 817 818 819 820 821
		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;

822
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
823 824 825 826 827 828

	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);
829 830
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

	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;

881
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
882 883 884 885

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

886
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
887 888 889 890

	return 0;
}

891
static void pn533_poll_reset_mod_list(struct pn533 *dev);
892 893
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
894 895 896 897
{
	struct nfc_target nfc_tgt;
	int rc;

898
	dev_dbg(dev->dev, "%s: modulation=%d\n",
899
		__func__, dev->poll_mod_curr);
900

901
	if (tg != 1)
902 903
		return -EPROTO;

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

906 907
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
908
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
909 910 911
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
912
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
913 914
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
915
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
916 917
		break;
	case PN533_POLL_MOD_847KBPS_B:
918
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
919 920
		break;
	default:
921
		nfc_err(dev->dev,
922
			"Unknown current poll modulation\n");
923 924 925 926 927 928 929
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
930
		dev_dbg(dev->dev,
931
			"The Tg found doesn't have the desired protocol\n");
932 933 934
		return -EAGAIN;
	}

935
	dev_dbg(dev->dev,
936 937
		"Target found - supported protocols: 0x%x\n",
		nfc_tgt.supported_protocols);
938 939 940

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

941
	pn533_poll_reset_mod_list(dev);
942 943 944 945 946
	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);

	return 0;
}

S
Samuel Ortiz 已提交
947 948 949 950 951
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

952 953 954 955 956 957 958 959
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] =
960
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
961 962 963
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
964 965
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
966 967 968
{
	pn533_poll_reset_mod_list(dev);

969 970 971
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
972 973
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

974 975
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
976 977 978 979
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
980
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
981 982
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

983
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
984 985
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
986 987
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
988 989
}

990
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
991
{
992 993
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
994

995
	/* Toggle the DEP polling */
996 997
	if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
		dev->poll_dep = 1;
998

999 1000 1001 1002 1003 1004 1005
	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);
1006 1007

		/* We must stop the poll after a valid target found */
1008
		if (rc == 0)
S
Samuel Ortiz 已提交
1009
			return 0;
1010 1011
	}

S
Samuel Ortiz 已提交
1012
	return -EAGAIN;
1013 1014
}

1015
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1016
{
1017
	struct sk_buff *skb;
1018
	u8 *felica, *nfcid3;
1019

1020 1021 1022
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1023 1024 1025 1026
	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 */
1027

1028 1029 1030
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1031

1032 1033 1034 1035
	unsigned int skb_len = 36 + /*
				     * mode (1), mifare (6),
				     * felica (18), nfcid3 (10), gb_len (1)
				     */
1036 1037
			       gbytes_len +
			       1;  /* len Tk*/
1038

1039
	skb = pn533_alloc_skb(dev, skb_len);
1040 1041
	if (!skb)
		return NULL;
1042 1043

	/* DEP support only */
1044
	skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1045 1046

	/* MIFARE params */
1047
	skb_put_data(skb, mifare_params, 6);
1048 1049

	/* Felica params */
1050
	felica = skb_put_data(skb, felica_params, 18);
1051
	get_random_bytes(felica + 2, 6);
1052 1053

	/* NFCID3 */
J
Johannes Berg 已提交
1054
	nfcid3 = skb_put_zero(skb, 10);
1055
	memcpy(nfcid3, felica, 8);
1056 1057

	/* General bytes */
1058
	skb_put_u8(skb, gbytes_len);
1059

1060
	skb_put_data(skb, gbytes, gbytes_len);
1061

1062
	/* Len Tk */
1063
	skb_put_u8(skb, 0);
1064

1065
	return skb;
1066 1067
}

1068 1069 1070
static void pn533_wq_tm_mi_recv(struct work_struct *work);
static struct sk_buff *pn533_build_response(struct pn533 *dev);

1071
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1072
				      struct sk_buff *resp)
1073
{
1074 1075 1076
	struct sk_buff *skb;
	u8 status, ret, mi;
	int rc;
1077

1078 1079
	if (IS_ERR(resp)) {
		skb_queue_purge(&dev->resp_q);
1080
		return PTR_ERR(resp);
1081
	}
1082

1083
	status = resp->data[0];
1084 1085 1086 1087

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

1088
	skb_pull(resp, sizeof(status));
1089

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	if (ret != PN533_CMD_RET_SUCCESS) {
		rc = -EIO;
		goto error;
	}

	skb_queue_tail(&dev->resp_q, resp);

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

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

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
	return nfc_tm_data_received(dev->nfc_dev, skb);

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

	return rc;
}

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

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

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

	if (rc < 0)
		dev_kfree_skb(skb);
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
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
				  struct sk_buff *resp);
static void pn533_wq_tm_mi_send(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
	struct sk_buff *skb;
	int rc;

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

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

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

1167
	dev_err(dev->dev,
1168 1169 1170 1171 1172
		"Error %d when trying to perform set meta data_exchange", rc);

	dev_kfree_skb(skb);

error:
1173
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
1174 1175 1176
	queue_work(dev->wq, &dev->cmd_work);
}

1177 1178 1179
static void pn533_wq_tg_get_data(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, tg_work);
1180 1181
	struct sk_buff *skb;
	int rc;
1182

1183
	skb = pn533_alloc_skb(dev, 0);
1184
	if (!skb)
1185 1186
		return;

1187 1188
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1189

1190 1191
	if (rc < 0)
		dev_kfree_skb(skb);
1192 1193
}

1194
#define ATR_REQ_GB_OFFSET 17
1195
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1196
{
1197
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1198
	size_t gb_len;
1199
	int rc;
1200

1201
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1202 1203
		return -EINVAL;

1204 1205
	mode = resp->data[0];
	cmd = &resp->data[1];
1206

1207
	dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1208
		mode, resp->len);
1209

1210 1211
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1212 1213
		comm_mode = NFC_COMM_ACTIVE;

1214
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1215 1216
		return -EOPNOTSUPP;

1217 1218
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1219

1220 1221 1222
	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
			      comm_mode, gb, gb_len);
	if (rc < 0) {
1223
		nfc_err(dev->dev,
1224
			"Error when signaling target activation\n");
1225 1226 1227
		return rc;
	}

S
Samuel Ortiz 已提交
1228
	dev->tgt_mode = 1;
1229 1230 1231
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1232 1233
}

1234
static void pn533_listen_mode_timer(struct timer_list *t)
1235
{
1236
	struct pn533 *dev = from_timer(dev, t, listen_timer);
S
Samuel Ortiz 已提交
1237 1238 1239 1240 1241

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

1242 1243
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
S
Samuel Ortiz 已提交
1244 1245
}

1246 1247 1248 1249 1250 1251 1252 1253
static int pn533_rf_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

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

1254
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1255 1256 1257 1258

		return rc;
	}

1259 1260
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

	dev_kfree_skb(resp);
	return rc;
}

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

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

1276 1277
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1278 1279 1280 1281 1282

	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
				  pn533_rf_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1283
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1284 1285 1286
	}
}

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
				   struct sk_buff *resp)
{
	struct pn533_cmd_jump_dep_response *rsp;
	struct nfc_target nfc_target;
	u8 target_gt_len;
	int rc;

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

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

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

		dev_kfree_skb(resp);
		return 0;
	}

1309
	dev_dbg(dev->dev, "Creating new target");
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 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

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

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

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

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

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

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

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

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

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

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

1369 1370
	skb_put_u8(skb, 0x01);  /* Active */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1371 1372 1373 1374 1375

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

	/* Copy passive data */
1376
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1377 1378 1379
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1380
	skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1381 1382
	*next |= 2;

1383
	skb_put_data(skb, dev->gb, dev->gb_len);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	*next |= 4; /* We have some Gi */

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

	if (rc < 0)
		dev_kfree_skb(skb);

	return rc;
}

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
static int pn533_autopoll_complete(struct pn533 *dev, void *arg,
			       struct sk_buff *resp)
{
	struct pn532_autopoll_resp *apr;
	struct nfc_target nfc_tgt;
	u8 nbtg;
	int rc;

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

		nfc_err(dev->dev, "%s  autopoll complete error %d\n",
			__func__, rc);

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
			goto stop_poll;
		} else if (rc < 0) {
			nfc_err(dev->dev,
				"Error %d when running autopoll\n", rc);
			goto stop_poll;
		}
	}

	nbtg = resp->data[0];
	if ((nbtg > 2) || (nbtg <= 0))
		return -EAGAIN;

	apr = (struct pn532_autopoll_resp *)&resp->data[1];
	while (nbtg--) {
		memset(&nfc_tgt, 0, sizeof(struct nfc_target));
		switch (apr->type) {
		case PN532_AUTOPOLL_TYPE_ISOA:
			dev_dbg(dev->dev, "ISOA\n");
			rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata,
						       apr->ln - 1);
			break;
		case PN532_AUTOPOLL_TYPE_FELICA212:
		case PN532_AUTOPOLL_TYPE_FELICA424:
			dev_dbg(dev->dev, "FELICA\n");
			rc = pn533_target_found_felica(&nfc_tgt, apr->tgdata,
						       apr->ln - 1);
			break;
		case PN532_AUTOPOLL_TYPE_JEWEL:
			dev_dbg(dev->dev, "JEWEL\n");
			rc = pn533_target_found_jewel(&nfc_tgt, apr->tgdata,
						      apr->ln - 1);
			break;
		case PN532_AUTOPOLL_TYPE_ISOB:
			dev_dbg(dev->dev, "ISOB\n");
			rc = pn533_target_found_type_b(&nfc_tgt, apr->tgdata,
						       apr->ln - 1);
			break;
		case PN532_AUTOPOLL_TYPE_MIFARE:
			dev_dbg(dev->dev, "Mifare\n");
			rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata,
						       apr->ln - 1);
			break;
		default:
			nfc_err(dev->dev,
				    "Unknown current poll modulation\n");
			rc = -EPROTO;
		}

		if (rc)
			goto done;

		if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
			nfc_err(dev->dev,
				    "The Tg found doesn't have the desired protocol\n");
			rc = -EAGAIN;
			goto done;
		}

		dev->tgt_available_prots = nfc_tgt.supported_protocols;
		apr = (struct pn532_autopoll_resp *)
			(apr->tgdata + (apr->ln - 1));
	}

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

done:
	dev_kfree_skb(resp);
	return rc;

stop_poll:
	nfc_err(dev->dev, "autopoll operation has been stopped\n");

	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
	return rc;
}

S
Samuel Ortiz 已提交
1490
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1491
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1492 1493
{
	struct pn533_poll_modulations *cur_mod;
1494 1495
	int rc;

1496 1497 1498
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1499
		nfc_err(dev->dev, "%s  Poll complete error %d\n",
1500
			__func__, rc);
1501 1502 1503 1504

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
1505
			goto stop_poll;
1506
		} else if (rc < 0) {
1507
			nfc_err(dev->dev,
1508
				"Error %d when running poll\n", rc);
1509 1510
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
1511
	}
1512

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

1515
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1516
		del_timer(&dev->listen_timer);
1517 1518
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1519 1520
	}

1521 1522 1523 1524
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1525

1526
	if (!dev->poll_mod_count) {
1527
		dev_dbg(dev->dev, "Polling has been stopped\n");
1528 1529 1530
		goto done;
	}

1531
	pn533_poll_next_mod(dev);
1532 1533
	/* Not target found, turn radio off */
	queue_work(dev->wq, &dev->rf_work);
S
Samuel Ortiz 已提交
1534

1535 1536 1537
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1538 1539

stop_poll:
1540
	nfc_err(dev->dev, "Polling operation has been stopped\n");
1541

S
Samuel Ortiz 已提交
1542 1543
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1544
	return rc;
1545 1546
}

1547 1548
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
1549
{
1550
	struct sk_buff *skb;
1551

1552
	skb = pn533_alloc_skb(dev, mod->len);
1553 1554
	if (!skb)
		return NULL;
1555

1556
	skb_put_data(skb, &mod->data, mod->len);
1557

1558
	return skb;
S
Samuel Ortiz 已提交
1559
}
1560

S
Samuel Ortiz 已提交
1561 1562
static int pn533_send_poll_frame(struct pn533 *dev)
{
1563 1564
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1565
	int rc;
1566
	u8 cmd_code;
1567

1568
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1569

1570
	dev_dbg(dev->dev, "%s mod len %d\n",
1571
		__func__, mod->len);
1572

1573
	if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1574 1575 1576 1577
		dev->poll_dep = 0;
		return pn533_poll_dep(dev->nfc_dev);
	}

1578 1579
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1580
		skb = pn533_alloc_poll_tg_frame(dev);
1581 1582
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1583
		skb = pn533_alloc_poll_in_frame(dev, mod);
1584 1585 1586
	}

	if (!skb) {
1587
		nfc_err(dev->dev, "Failed to allocate skb\n");
1588 1589 1590 1591 1592 1593 1594
		return -ENOMEM;
	}

	rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
				  NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1595
		nfc_err(dev->dev, "Polling loop error %d\n", rc);
1596
	}
1597

S
Samuel Ortiz 已提交
1598 1599 1600 1601 1602
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
1603
	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
S
Samuel Ortiz 已提交
1604 1605 1606 1607 1608
	struct pn533_poll_modulations *cur_mod;
	int rc;

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

1609
	dev_dbg(dev->dev,
1610 1611
		"%s cancel_listen %d modulation len %d\n",
		__func__, dev->cancel_listen, cur_mod->len);
S
Samuel Ortiz 已提交
1612 1613 1614

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
1615
		dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1616 1617
	}

S
Samuel Ortiz 已提交
1618 1619 1620
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1621

S
Samuel Ortiz 已提交
1622 1623
	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1624 1625
}

1626 1627 1628 1629
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1630
	struct pn533_poll_modulations *cur_mod;
1631
	struct sk_buff *skb;
1632
	u8 rand_mod;
1633
	int rc;
1634

1635
	dev_dbg(dev->dev,
1636 1637
		"%s: im protocols 0x%x tm protocols 0x%x\n",
		__func__, im_protocols, tm_protocols);
1638 1639

	if (dev->tgt_active_prot) {
1640
		nfc_err(dev->dev,
1641
			"Cannot poll with a target already activated\n");
1642 1643 1644
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1645
	if (dev->tgt_mode) {
1646
		nfc_err(dev->dev,
1647
			"Cannot poll while already being activated\n");
S
Samuel Ortiz 已提交
1648 1649 1650
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1651 1652 1653 1654 1655
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1656

S
Samuel Ortiz 已提交
1657 1658
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	if (dev->device_type == PN533_DEVICE_PN532_AUTOPOLL) {
		skb = pn533_alloc_skb(dev, 4 + 6);
		if (!skb)
			return -ENOMEM;

		*((u8 *)skb_put(skb, sizeof(u8))) =
			PN532_AUTOPOLL_POLLNR_INFINITE;
		*((u8 *)skb_put(skb, sizeof(u8))) = PN532_AUTOPOLL_PERIOD;

		if ((im_protocols & NFC_PROTO_MIFARE_MASK) &&
				(im_protocols & NFC_PROTO_ISO14443_MASK) &&
				(im_protocols & NFC_PROTO_NFC_DEP_MASK))
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_GENERIC_106;
		else {
			if (im_protocols & NFC_PROTO_MIFARE_MASK)
				*((u8 *)skb_put(skb, sizeof(u8))) =
					PN532_AUTOPOLL_TYPE_MIFARE;

			if (im_protocols & NFC_PROTO_ISO14443_MASK)
				*((u8 *)skb_put(skb, sizeof(u8))) =
					PN532_AUTOPOLL_TYPE_ISOA;

			if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
				*((u8 *)skb_put(skb, sizeof(u8))) =
					PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106;
				*((u8 *)skb_put(skb, sizeof(u8))) =
					PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212;
				*((u8 *)skb_put(skb, sizeof(u8))) =
					PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424;
			}
		}

		if (im_protocols & NFC_PROTO_FELICA_MASK ||
				im_protocols & NFC_PROTO_NFC_DEP_MASK) {
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_FELICA212;
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_FELICA424;
		}

		if (im_protocols & NFC_PROTO_JEWEL_MASK)
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_JEWEL;

		if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_ISOB;

		if (tm_protocols)
			*((u8 *)skb_put(skb, sizeof(u8))) =
				PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106;

		rc = pn533_send_cmd_async(dev, PN533_CMD_IN_AUTOPOLL, skb,
				pn533_autopoll_complete, NULL);

		if (rc < 0)
			dev_kfree_skb(skb);
		else
			dev->poll_mod_count++;

		return rc;
	}

	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1724

1725 1726 1727 1728 1729
	/* Do not always start polling from the same modulation */
	get_random_bytes(&rand_mod, sizeof(rand_mod));
	rand_mod %= dev->poll_mod_count;
	dev->poll_mod_curr = rand_mod;

1730 1731 1732 1733 1734 1735 1736 1737 1738
	cur_mod = dev->poll_mod_active[dev->poll_mod_curr];

	rc = pn533_send_poll_frame(dev);

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

	return rc;
1739 1740
}

1741 1742 1743 1744
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

S
Samuel Ortiz 已提交
1745 1746
	del_timer(&dev->listen_timer);

1747
	if (!dev->poll_mod_count) {
1748
		dev_dbg(dev->dev,
1749
			"Polling operation was not running\n");
1750 1751 1752
		return;
	}

1753
	dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1754
	flush_delayed_work(&dev->poll_work);
1755
	pn533_poll_reset_mod_list(dev);
1756 1757 1758 1759
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1760
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1761
	u16 gt_len;
1762
	int rc;
1763 1764
	struct sk_buff *skb;
	struct sk_buff *resp;
1765

1766
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1767 1768
	if (!skb)
		return -ENOMEM;
1769

1770 1771
	skb_put_u8(skb, 1); /* TG */
	skb_put_u8(skb, 0); /* Next */
1772

1773 1774 1775
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1776

1777
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1778
	rc = rsp->status & PN533_CMD_RET_MASK;
1779
	if (rc != PN533_CMD_RET_SUCCESS) {
1780
		nfc_err(dev->dev,
1781
			"Target activation failed (error 0x%x)\n", rc);
1782
		dev_kfree_skb(resp);
1783
		return -EIO;
1784
	}
1785

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

1790
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1791
	return rc;
1792 1793
}

1794 1795
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1796 1797 1798 1799
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

1800
	dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1801 1802

	if (dev->poll_mod_count) {
1803
		nfc_err(dev->dev,
1804
			"Cannot activate while polling\n");
1805 1806 1807 1808
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1809
		nfc_err(dev->dev,
1810
			"There is already an active target\n");
1811 1812 1813 1814
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1815
		nfc_err(dev->dev,
1816
			"There is no available target to activate\n");
1817 1818 1819 1820
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1821
		nfc_err(dev->dev,
1822 1823
			"Target doesn't support requested proto %u\n",
			protocol);
1824 1825 1826 1827 1828 1829
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1830
			nfc_err(dev->dev,
1831
				"Activating target with DEP failed %d\n", rc);
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
			return rc;
		}
	}

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

	return 0;
}

1842 1843 1844 1845 1846 1847 1848 1849
static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

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

1850
		nfc_err(dev->dev, "Target release error %d\n", rc);
1851 1852 1853 1854 1855 1856

		return rc;
	}

	rc = resp->data[0] & PN533_CMD_RET_MASK;
	if (rc != PN533_CMD_RET_SUCCESS)
1857
		nfc_err(dev->dev,
1858 1859 1860 1861 1862 1863
			"Error 0x%x when releasing the target\n", rc);

	dev_kfree_skb(resp);
	return rc;
}

1864
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1865
				    struct nfc_target *target, u8 mode)
1866 1867
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1868
	struct sk_buff *skb;
1869 1870 1871
	int rc;

	if (!dev->tgt_active_prot) {
1872
		nfc_err(dev->dev, "There is no active target\n");
1873 1874 1875 1876
		return;
	}

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

1879
	skb = pn533_alloc_skb(dev, sizeof(u8));
1880 1881
	if (!skb)
		return;
1882

1883
	skb_put_u8(skb, 1); /* TG*/
1884

1885 1886 1887 1888
	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
				  pn533_deactivate_target_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1889
		nfc_err(dev->dev, "Target release error %d\n", rc);
1890
	}
1891 1892
}

1893 1894

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1895
					 struct sk_buff *resp)
1896
{
1897
	struct pn533_cmd_jump_dep_response *rsp;
1898 1899
	u8 target_gt_len;
	int rc;
1900
	u8 active = *(u8 *)arg;
1901 1902

	kfree(arg);
1903

1904 1905
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1906 1907 1908

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1909
		nfc_err(dev->dev,
1910
			"The target does not support DEP\n");
1911 1912
		rc =  -EINVAL;
		goto error;
1913 1914
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1918
	if (rc != PN533_CMD_RET_SUCCESS) {
1919
		nfc_err(dev->dev,
1920
			"Bringing DEP link up failed (error 0x%x)\n", rc);
1921
		goto error;
1922 1923 1924
	}

	if (!dev->tgt_available_prots) {
1925 1926
		struct nfc_target nfc_target;

1927
		dev_dbg(dev->dev, "Creating new target\n");
1928 1929

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1930
		nfc_target.nfcid1_len = 10;
1931
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1932 1933
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
1934
			goto error;
1935 1936 1937 1938 1939 1940 1941

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
1942
	target_gt_len = resp->len - 17;
1943
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1944
					  rsp->gt, target_gt_len);
1945 1946
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
1947 1948
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
1949

1950 1951 1952
error:
	dev_kfree_skb(resp);
	return rc;
1953 1954
}

1955
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1956
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1957
			     u8 comm_mode, u8 *gb, size_t gb_len)
1958 1959
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1960
	struct sk_buff *skb;
1961 1962
	int rc, skb_len;
	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1963
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1964 1965

	if (dev->poll_mod_count) {
1966
		nfc_err(dev->dev,
1967
			"Cannot bring the DEP link up while polling\n");
1968 1969 1970 1971
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1972
		nfc_err(dev->dev,
1973
			"There is already an active target\n");
1974 1975 1976
		return -EBUSY;
	}

1977
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1978
	skb_len += PASSIVE_DATA_LEN;
1979

1980 1981 1982 1983 1984 1985 1986
	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	if (target && !target->nfcid2_len) {
		nfcid3[0] = 0x1;
		nfcid3[1] = 0xfe;
		get_random_bytes(nfcid3 + 2, 6);
	}
1987

1988
	skb = pn533_alloc_skb(dev, skb_len);
1989
	if (!skb)
1990 1991
		return -ENOMEM;

1992 1993
	skb_put_u8(skb, !comm_mode);  /* ActPass */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1994 1995 1996

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

1998
	/* Copy passive data */
1999
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
2000
	*next |= 1;
2001

2002 2003
	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	if (target && target->nfcid2_len)
2004 2005
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
		       target->nfcid2_len);
2006
	else
2007
		skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
2008
	*next |= 2;
2009

2010
	if (gb != NULL && gb_len > 0) {
2011
		skb_put_data(skb, gb, gb_len);
2012
		*next |= 4; /* We have some Gi */
2013
	} else {
2014
		*next = 0;
2015 2016
	}

2017 2018 2019 2020 2021
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
2022

2023
	*arg = !comm_mode;
2024

2025 2026
	pn533_rf_field(dev->nfc_dev, 0);

2027 2028 2029 2030 2031 2032 2033
	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);
	}
2034 2035 2036 2037 2038 2039

	return rc;
}

static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
{
S
Samuel Ortiz 已提交
2040 2041 2042 2043
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

	pn533_poll_reset_mod_list(dev);

2044
	if (dev->tgt_mode || dev->tgt_active_prot)
2045
		dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2046 2047 2048 2049 2050

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

	skb_queue_purge(&dev->resp_q);
2051 2052 2053 2054

	return 0;
}

2055 2056 2057 2058 2059
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

	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;

2076
	dev_dbg(dev->dev, "%s total length %d\n",
2077
		__func__, skb_len);
S
Samuel Ortiz 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095

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

2096
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2097
					struct sk_buff *resp)
2098 2099
{
	struct pn533_data_exchange_arg *arg = _arg;
2100 2101 2102
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
2103

2104 2105 2106
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
2107 2108
	}

2109 2110 2111 2112 2113
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
2114

2115
	if (ret != PN533_CMD_RET_SUCCESS) {
2116
		nfc_err(dev->dev,
2117
			"Exchanging data failed (error 0x%x)\n", ret);
2118
		rc = -EIO;
2119 2120 2121
		goto error;
	}

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

2124 2125
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
2126 2127 2128 2129 2130 2131 2132 2133 2134
		queue_work(dev->wq, &dev->mi_rx_work);
		return -EINPROGRESS;
	}

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

S
Samuel Ortiz 已提交
2135
		return -EINPROGRESS;
2136 2137
	}

S
Samuel Ortiz 已提交
2138
	skb = pn533_build_response(dev);
J
Julia Lawall 已提交
2139 2140
	if (!skb) {
		rc = -ENOMEM;
S
Samuel Ortiz 已提交
2141
		goto error;
J
Julia Lawall 已提交
2142
	}
2143

S
Samuel Ortiz 已提交
2144
	arg->cb(arg->cb_context, skb, 0);
2145 2146 2147 2148
	kfree(arg);
	return 0;

error:
2149 2150
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2151
	skb_queue_purge(&dev->resp_q);
2152
	arg->cb(arg->cb_context, NULL, rc);
2153
	kfree(arg);
2154
	return rc;
2155 2156
}

2157 2158 2159 2160 2161 2162
/*
 * Receive an incoming pn533 frame. skb contains only header and payload.
 * If skb == NULL, it is a notification that the link below is dead.
 */
void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
{
2163 2164 2165
	if (!dev->cmd)
		goto sched_wq;

2166 2167
	dev->cmd->status = status;

2168 2169 2170 2171 2172
	if (status != 0) {
		dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
		goto sched_wq;
	}

2173
	if (skb == NULL) {
2174
		dev_err(dev->dev, "NULL Frame -> link is dead\n");
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
		goto sched_wq;
	}

	if (pn533_rx_frame_is_ack(skb->data)) {
		dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
		dev_kfree_skb(skb);
		return;
	}

	print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
			     dev->ops->rx_frame_size(skb->data), false);

	if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
		nfc_err(dev->dev, "Received an invalid frame\n");
		dev->cmd->status = -EIO;
	} else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
		nfc_err(dev->dev, "It it not the response to the last command\n");
		dev->cmd->status = -EIO;
	}

	dev->cmd->resp = skb;

sched_wq:
	queue_work(dev->wq, &dev->cmd_complete_work);
}
EXPORT_SYMBOL(pn533_recv_frame);

2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
/* Split the Tx skb into small chunks */
static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
{
	struct sk_buff *frag;
	int  frag_size;

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

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

2222 2223 2224 2225 2226 2227
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2228 2229
				*(u8 *)skb_push(frag, sizeof(u8)) =
						(PN533_CMD_MI_MASK | 1);
2230
			else
2231
				*(u8 *)skb_push(frag, sizeof(u8)) =  1; /* TG */
2232
		}
2233

2234
		skb_put_data(frag, skb->data, frag_size);
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248

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

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

	} while (skb->len > 0);

	dev_kfree_skb(skb);

	return skb_queue_len(&dev->fragment_skb);
}

S
Samuel Ortiz 已提交
2249 2250 2251
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)
2252 2253
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2254
	struct pn533_data_exchange_arg *arg = NULL;
2255 2256 2257
	int rc;

	if (!dev->tgt_active_prot) {
2258
		nfc_err(dev->dev,
2259
			"Can't exchange data if there is no active target\n");
2260 2261 2262 2263
		rc = -EINVAL;
		goto error;
	}

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

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

2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
	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;
		}
2283
		fallthrough;
2284
	default:
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
		/* jumbo frame ? */
		if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
			rc = pn533_fill_fragment_skbs(dev, skb);
			if (rc <= 0)
				goto error;

			skb = skb_dequeue(&dev->fragment_skb);
			if (!skb) {
				rc = -EIO;
				goto error;
			}
		} else {
2297
			*(u8 *)skb_push(skb, sizeof(u8)) =  1; /* TG */
2298
		}
2299 2300 2301 2302 2303 2304

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

		break;
2305 2306
	}

2307 2308 2309
	if (rc < 0) /* rc from send_async */
		goto error;

2310 2311 2312
	return 0;

error:
2313 2314
	kfree(arg);
	dev_kfree_skb(skb);
2315 2316 2317
	return rc;
}

2318
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2319
				  struct sk_buff *resp)
2320
{
2321
	u8 status;
2322

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

2326
	status = resp->data[0];
2327

2328 2329 2330 2331 2332
	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		queue_work(dev->wq, &dev->mi_tm_tx_work);
		return -EINPROGRESS;
	}
2333
	dev_kfree_skb(resp);
2334

2335
	if (status != 0) {
2336 2337
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2338 2339
		dev->tgt_mode = 0;

2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
		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;

2353
	/* let's split in multiple chunks if size's too big */
2354
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
		rc = pn533_fill_fragment_skbs(dev, skb);
		if (rc <= 0)
			goto error;

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

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

2374 2375
error:
	if (rc < 0) {
2376
		dev_kfree_skb(skb);
2377 2378
		skb_queue_purge(&dev->fragment_skb);
	}
2379 2380 2381 2382

	return rc;
}

S
Samuel Ortiz 已提交
2383 2384
static void pn533_wq_mi_recv(struct work_struct *work)
{
2385
	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2386
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2387 2388
	int rc;

2389
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2390 2391
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2392

2393 2394 2395 2396 2397 2398 2399 2400
	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 已提交
2401

2402 2403
			break;
		}
2404
		fallthrough;
2405
	default:
2406
		skb_put_u8(skb, 1); /*TG*/
S
Samuel Ortiz 已提交
2407

2408 2409 2410 2411 2412
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2413

2414
		break;
S
Samuel Ortiz 已提交
2415 2416
	}

2417
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2418 2419
		return;

2420
	nfc_err(dev->dev,
2421
		"Error %d when trying to perform data_exchange\n", rc);
S
Samuel Ortiz 已提交
2422

2423
	dev_kfree_skb(skb);
2424
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2425

2426
error:
2427
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2428
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2429 2430
}

2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
static void pn533_wq_mi_send(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
	struct sk_buff *skb;
	int rc;

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

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

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

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

		break;

	default:
		/* Still some fragments? */
2462 2463
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;
	}

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

2474
	nfc_err(dev->dev,
2475
		"Error %d when trying to perform data_exchange\n", rc);
2476 2477 2478 2479 2480

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

error:
2481
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2482 2483 2484
	queue_work(dev->wq, &dev->cmd_work);
}

2485 2486 2487
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2488 2489 2490
	struct sk_buff *skb;
	struct sk_buff *resp;
	int skb_len;
2491

2492
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2493

2494
	skb = pn533_alloc_skb(dev, skb_len);
2495 2496
	if (!skb)
		return -ENOMEM;
2497

2498
	skb_put_u8(skb, cfgitem);
2499
	skb_put_data(skb, cfgdata, cfgdata_len);
2500

2501 2502 2503
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2504

2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	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;

2515
	skb = pn533_alloc_skb(dev, 0);
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
	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;
2530 2531
}

2532
static int pn533_pasori_fw_reset(struct pn533 *dev)
2533
{
2534 2535
	struct sk_buff *skb;
	struct sk_buff *resp;
2536

2537
	skb = pn533_alloc_skb(dev, sizeof(u8));
2538 2539
	if (!skb)
		return -ENOMEM;
2540

2541
	skb_put_u8(skb, 0x1);
2542

2543 2544 2545
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2546

2547
	dev_kfree_skb(resp);
2548

2549
	return 0;
2550 2551
}

2552 2553 2554 2555 2556 2557
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	u8 rf_field = !!rf;
	int rc;

S
Samuel Ortiz 已提交
2558 2559
	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;

2560 2561 2562
	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
				     (u8 *)&rf_field, 1);
	if (rc) {
2563
		nfc_err(dev->dev, "Error on setting RF field\n");
2564 2565 2566
		return rc;
	}

2567
	return 0;
2568 2569
}

2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	struct sk_buff *skb;
	struct sk_buff *resp;

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

2580
	skb_put_u8(skb, 0x01);
2581 2582 2583 2584 2585 2586 2587 2588 2589

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

	dev_kfree_skb(resp);
	return 0;
}

2590
static int pn533_dev_up(struct nfc_dev *nfc_dev)
2591
{
2592
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2593
	int rc;
2594

2595 2596 2597 2598 2599
	if (dev->phy_ops->dev_up) {
		rc = dev->phy_ops->dev_up(dev);
		if (rc)
			return rc;
	}
2600

2601 2602
	if ((dev->device_type == PN533_DEVICE_PN532) ||
		(dev->device_type == PN533_DEVICE_PN532_AUTOPOLL)) {
2603
		rc = pn532_sam_configuration(nfc_dev);
2604 2605 2606 2607 2608

		if (rc)
			return rc;
	}

2609 2610 2611
	return pn533_rf_field(nfc_dev, 1);
}

2612
static int pn533_dev_down(struct nfc_dev *nfc_dev)
2613
{
2614 2615 2616 2617 2618
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int ret;

	ret = pn533_rf_field(nfc_dev, 0);
	if (dev->phy_ops->dev_down && !ret)
2619
		ret = dev->phy_ops->dev_down(dev);
2620 2621

	return ret;
2622 2623
}

K
Krzysztof Kozlowski 已提交
2624
static const struct nfc_ops pn533_nfc_ops = {
2625 2626
	.dev_up = pn533_dev_up,
	.dev_down = pn533_dev_down,
2627 2628
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2629 2630 2631 2632
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2633
	.im_transceive = pn533_transceive,
2634
	.tm_send = pn533_tm_send,
2635 2636
};

2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
static int pn533_setup(struct pn533 *dev)
{
	struct pn533_config_max_retries max_retries;
	struct pn533_config_timing timing;
	u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
	int rc;

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
	case PN533_DEVICE_PASORI:
2647
	case PN533_DEVICE_ACR122U:
2648
	case PN533_DEVICE_PN532:
2649
	case PN533_DEVICE_PN532_AUTOPOLL:
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
		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:
2662
		nfc_err(dev->dev, "Unknown device type %d\n",
2663
			dev->device_type);
2664 2665 2666 2667 2668 2669
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
2670
		nfc_err(dev->dev,
2671
			"Error on setting MAX_RETRIES config\n");
2672 2673 2674 2675 2676 2677 2678
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
2679
		nfc_err(dev->dev, "Error on setting RF timings\n");
2680 2681 2682 2683 2684
		return rc;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
2685
	case PN533_DEVICE_PN532:
2686
	case PN533_DEVICE_PN532_AUTOPOLL:
2687 2688 2689
		break;

	case PN533_DEVICE_PASORI:
2690
		pn533_pasori_fw_reset(dev);
2691 2692 2693 2694

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
2695
			nfc_err(dev->dev,
2696
				"Error while settings PASORI config\n");
2697 2698 2699
			return rc;
		}

2700
		pn533_pasori_fw_reset(dev);
2701 2702 2703 2704 2705 2706 2707

		break;
	}

	return 0;
}

2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
int pn533_finalize_setup(struct pn533 *dev)
{

	struct pn533_fw_version fw_ver;
	int rc;

	memset(&fw_ver, 0, sizeof(fw_ver));

	rc = pn533_get_firmware_version(dev, &fw_ver);
	if (rc) {
		nfc_err(dev->dev, "Unable to get FW version\n");
		return rc;
	}

	nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
		fw_ver.ic, fw_ver.ver, fw_ver.rev);

	rc = pn533_setup(dev);
	if (rc)
		return rc;

	return 0;
}
EXPORT_SYMBOL_GPL(pn533_finalize_setup);

2733
struct pn533 *pn53x_common_init(u32 device_type,
2734 2735 2736 2737
				enum pn533_protocol_type protocol_type,
				void *phy,
				struct pn533_phy_ops *phy_ops,
				struct pn533_frame_ops *fops,
2738
				struct device *dev)
2739
{
2740
	struct pn533 *priv;
2741

2742 2743 2744
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);
2745

2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
	priv->phy = phy;
	priv->phy_ops = phy_ops;
	priv->dev = dev;
	if (fops != NULL)
		priv->ops = fops;
	else
		priv->ops = &pn533_std_frame_ops;

	priv->protocol_type = protocol_type;
	priv->device_type = device_type;

	mutex_init(&priv->cmd_lock);

	INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
	INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
	INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
	INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
	INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
	INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
	INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
	INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
	INIT_WORK(&priv->rf_work, pn533_wq_rf);
	priv->wq = alloc_ordered_workqueue("pn533", 0);
	if (priv->wq == NULL)
2770
		goto error;
2771

2772
	timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
2773

2774 2775
	skb_queue_head_init(&priv->resp_q);
	skb_queue_head_init(&priv->fragment_skb);
2776

2777 2778
	INIT_LIST_HEAD(&priv->cmd_queue);
	return priv;
2779 2780

error:
2781
	kfree(priv);
2782
	return ERR_PTR(-ENOMEM);
2783
}
2784
EXPORT_SYMBOL_GPL(pn53x_common_init);
2785

2786
void pn53x_common_clean(struct pn533 *priv)
2787
{
S
Samuel Ortiz 已提交
2788
	struct pn533_cmd *cmd, *n;
2789

2790 2791
	flush_delayed_work(&priv->poll_work);
	destroy_workqueue(priv->wq);
2792

2793
	skb_queue_purge(&priv->resp_q);
2794

2795
	del_timer(&priv->listen_timer);
2796

2797
	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
S
Samuel Ortiz 已提交
2798 2799 2800 2801
		list_del(&cmd->queue);
		kfree(cmd);
	}

2802
	kfree(priv);
2803
}
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
EXPORT_SYMBOL_GPL(pn53x_common_clean);

int pn532_i2c_nfc_alloc(struct pn533 *priv, u32 protocols,
			struct device *parent)
{
	priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
					   priv->ops->tx_header_len +
					   PN533_CMD_DATAEXCH_HEAD_LEN,
					   priv->ops->tx_tail_len);
	if (!priv->nfc_dev)
		return -ENOMEM;

	nfc_set_parent_dev(priv->nfc_dev, parent);
	nfc_set_drvdata(priv->nfc_dev, priv);
	return 0;
}
EXPORT_SYMBOL_GPL(pn532_i2c_nfc_alloc);

int pn53x_register_nfc(struct pn533 *priv, u32 protocols,
			struct device *parent)
{
	int rc;

	rc = pn532_i2c_nfc_alloc(priv, protocols, parent);
	if (rc)
		return rc;

	rc = nfc_register_device(priv->nfc_dev);
	if (rc)
		nfc_free_device(priv->nfc_dev);

	return rc;
}
EXPORT_SYMBOL_GPL(pn53x_register_nfc);
2838

2839 2840 2841 2842 2843 2844
void pn53x_unregister_nfc(struct pn533 *priv)
{
	nfc_unregister_device(priv->nfc_dev);
	nfc_free_device(priv->nfc_dev);
}
EXPORT_SYMBOL_GPL(pn53x_unregister_nfc);
2845

2846 2847 2848
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>");
2849
MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2850 2851
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");