pn533.c 64.2 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 492 493
}

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)
{
	int rc;

494
	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
495 496 497
				complete_cb_context);

	return rc;
498 499 500 501 502 503 504 505 506
}

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)
{
	int rc;

507
	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
508
				complete_cb_context);
509 510 511 512

	return rc;
}

513 514 515
/*
 * pn533_send_cmd_direct_async
 *
516
 * The function sends a piority cmd directly to the chip omitting the cmd
517 518 519 520 521 522 523 524 525
 * 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)
{
526
	struct pn533_cmd *cmd;
527 528
	int rc;

529
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
530
	if (!cmd)
531 532
		return -ENOMEM;

533
	cmd->code = cmd_code;
534 535 536
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
537

538
	pn533_build_cmd_frame(dev, cmd_code, req);
539

540
	dev->cmd = cmd;
541
	rc = dev->phy_ops->send_frame(dev, req);
542 543
	if (rc < 0) {
		dev->cmd = NULL;
544
		kfree(cmd);
545
	}
546 547 548 549

	return rc;
}

550 551 552 553 554 555 556 557 558 559
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 已提交
560 561 562 563
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;
564
	int rc;
S
Samuel Ortiz 已提交
565 566 567 568 569 570 571 572 573 574 575

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

576 577
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
578 579
	mutex_unlock(&dev->cmd_lock);

580
	dev->cmd = cmd;
581
	rc = dev->phy_ops->send_frame(dev, cmd->req);
582
	if (rc < 0) {
583
		dev->cmd = NULL;
584
		dev_kfree_skb(cmd->req);
585
		kfree(cmd);
586
		return;
587
	}
588

S
Samuel Ortiz 已提交
589 590
}

591
struct pn533_sync_cmd_response {
592
	struct sk_buff *resp;
593 594 595
	struct completion done;
};

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
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
615
 *     as it's been already freed at the beginning of RX path by
616 617 618 619 620 621 622
 *     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.
 *
623
 */
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
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;
}

644
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
645 646 647
{
	struct sk_buff *skb;

648
	skb = alloc_skb(dev->ops->tx_header_len +
649
			size +
650
			dev->ops->tx_tail_len, GFP_KERNEL);
651 652

	if (skb)
653
		skb_reserve(skb, dev->ops->tx_header_len);
654 655 656 657

	return skb;
}

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

690 691 692 693
	/*
	 * The length check of nfcid[] and ats[] are not being performed because
	 * the values are not being used
	 */
694 695 696 697 698 699

	/* 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 &&
700 701 702
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
703 704 705 706 707 708 709 710 711 712 713 714 715 716
		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;

717
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

	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 已提交
740 741
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
742 743 744 745 746 747 748

	return 0;
}

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

775
	tgt_felica = (struct pn533_target_felica *)tgt_data;
776 777 778 779

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

780 781
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
782 783 784 785
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
786 787 788
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

789 790 791
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

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

827
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
828 829 830 831 832 833

	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);
834 835
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
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 881 882 883 884 885

	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;

886
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
887 888 889 890

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

891
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
892 893 894 895

	return 0;
}

896
static void pn533_poll_reset_mod_list(struct pn533 *dev);
897 898
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
899 900 901 902
{
	struct nfc_target nfc_tgt;
	int rc;

903
	dev_dbg(dev->dev, "%s: modulation=%d\n",
904
		__func__, dev->poll_mod_curr);
905

906
	if (tg != 1)
907 908
		return -EPROTO;

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

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

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
935
		dev_dbg(dev->dev,
936
			"The Tg found doesn't have the desired protocol\n");
937 938 939
		return -EAGAIN;
	}

940
	dev_dbg(dev->dev,
941 942
		"Target found - supported protocols: 0x%x\n",
		nfc_tgt.supported_protocols);
943 944 945

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

946
	pn533_poll_reset_mod_list(dev);
947 948 949 950 951
	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);

	return 0;
}

S
Samuel Ortiz 已提交
952 953 954 955 956
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

957 958 959 960 961 962 963 964
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] =
965
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
966 967 968
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
969 970
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
971 972 973
{
	pn533_poll_reset_mod_list(dev);

974 975 976
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
977 978
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

979 980
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
981 982 983 984
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
985
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
986 987
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

988
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
989 990
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
991 992
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
993 994
}

995
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
996
{
997 998
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
999

1000
	/* Toggle the DEP polling */
1001 1002
	if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
		dev->poll_dep = 1;
1003

1004 1005 1006 1007 1008 1009 1010
	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);
1011 1012

		/* We must stop the poll after a valid target found */
1013
		if (rc == 0)
S
Samuel Ortiz 已提交
1014
			return 0;
1015 1016
	}

S
Samuel Ortiz 已提交
1017
	return -EAGAIN;
1018 1019
}

1020
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1021
{
1022
	struct sk_buff *skb;
1023
	u8 *felica, *nfcid3;
1024

1025 1026 1027
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1028 1029 1030 1031
	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 */
1032

1033 1034 1035
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1036

1037 1038 1039 1040
	unsigned int skb_len = 36 + /*
				     * mode (1), mifare (6),
				     * felica (18), nfcid3 (10), gb_len (1)
				     */
1041 1042
			       gbytes_len +
			       1;  /* len Tk*/
1043

1044
	skb = pn533_alloc_skb(dev, skb_len);
1045 1046
	if (!skb)
		return NULL;
1047 1048

	/* DEP support only */
1049
	skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1050 1051

	/* MIFARE params */
1052
	skb_put_data(skb, mifare_params, 6);
1053 1054

	/* Felica params */
1055
	felica = skb_put_data(skb, felica_params, 18);
1056
	get_random_bytes(felica + 2, 6);
1057 1058

	/* NFCID3 */
J
Johannes Berg 已提交
1059
	nfcid3 = skb_put_zero(skb, 10);
1060
	memcpy(nfcid3, felica, 8);
1061 1062

	/* General bytes */
1063
	skb_put_u8(skb, gbytes_len);
1064

1065
	skb_put_data(skb, gbytes, gbytes_len);
1066

1067
	/* Len Tk */
1068
	skb_put_u8(skb, 0);
1069

1070
	return skb;
1071 1072
}

1073 1074 1075
static void pn533_wq_tm_mi_recv(struct work_struct *work);
static struct sk_buff *pn533_build_response(struct pn533 *dev);

1076
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1077
				      struct sk_buff *resp)
1078
{
1079 1080 1081
	struct sk_buff *skb;
	u8 status, ret, mi;
	int rc;
1082

1083
	dev_dbg(dev->dev, "%s\n", __func__);
1084

1085 1086
	if (IS_ERR(resp)) {
		skb_queue_purge(&dev->resp_q);
1087
		return PTR_ERR(resp);
1088
	}
1089

1090
	status = resp->data[0];
1091 1092 1093 1094

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

1095
	skb_pull(resp, sizeof(status));
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	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;
1113 1114
	}

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	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;

1132
	dev_dbg(dev->dev, "%s\n", __func__);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

	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);
1146 1147
}

1148 1149 1150 1151 1152 1153 1154 1155
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;

1156
	dev_dbg(dev->dev, "%s\n", __func__);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

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

1178
	dev_err(dev->dev,
1179 1180 1181 1182 1183
		"Error %d when trying to perform set meta data_exchange", rc);

	dev_kfree_skb(skb);

error:
1184
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
1185 1186 1187
	queue_work(dev->wq, &dev->cmd_work);
}

1188 1189 1190
static void pn533_wq_tg_get_data(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, tg_work);
1191 1192
	struct sk_buff *skb;
	int rc;
1193

1194
	dev_dbg(dev->dev, "%s\n", __func__);
1195

1196
	skb = pn533_alloc_skb(dev, 0);
1197
	if (!skb)
1198 1199
		return;

1200 1201
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1202

1203 1204
	if (rc < 0)
		dev_kfree_skb(skb);
1205 1206
}

1207
#define ATR_REQ_GB_OFFSET 17
1208
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1209
{
1210
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1211
	size_t gb_len;
1212
	int rc;
1213

1214
	dev_dbg(dev->dev, "%s\n", __func__);
1215

1216
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1217 1218
		return -EINVAL;

1219 1220
	mode = resp->data[0];
	cmd = &resp->data[1];
1221

1222
	dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1223
		mode, resp->len);
1224

1225 1226
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1227 1228
		comm_mode = NFC_COMM_ACTIVE;

1229
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1230 1231
		return -EOPNOTSUPP;

1232 1233
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1234

1235 1236 1237
	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
			      comm_mode, gb, gb_len);
	if (rc < 0) {
1238
		nfc_err(dev->dev,
1239
			"Error when signaling target activation\n");
1240 1241 1242
		return rc;
	}

S
Samuel Ortiz 已提交
1243
	dev->tgt_mode = 1;
1244 1245 1246
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1247 1248
}

1249
static void pn533_listen_mode_timer(struct timer_list *t)
1250
{
1251
	struct pn533 *dev = from_timer(dev, t, listen_timer);
S
Samuel Ortiz 已提交
1252

1253
	dev_dbg(dev->dev, "Listen mode timeout\n");
S
Samuel Ortiz 已提交
1254 1255 1256 1257 1258

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

1259 1260
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
S
Samuel Ortiz 已提交
1261 1262
}

1263 1264 1265 1266 1267
static int pn533_rf_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1268
	dev_dbg(dev->dev, "%s\n", __func__);
1269 1270 1271 1272

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

1273
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1274 1275 1276 1277

		return rc;
	}

1278 1279
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

	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;

1291
	dev_dbg(dev->dev, "%s\n", __func__);
1292 1293 1294 1295 1296

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

1297 1298
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1299 1300 1301 1302 1303

	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
				  pn533_rf_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1304
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1305 1306 1307
	}
}

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
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;
	}

1330
	dev_dbg(dev->dev, "Creating new target");
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

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

1368
	dev_dbg(dev->dev, "%s", __func__);
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

	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;

1392 1393
	skb_put_u8(skb, 0x01);  /* Active */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1394 1395 1396 1397 1398

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

	/* Copy passive data */
1399
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1400 1401 1402
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1403
	skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1404 1405
	*next |= 2;

1406
	skb_put_data(skb, dev->gb, dev->gb_len);
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	*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;
}

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 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
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 已提交
1513
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1514
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1515 1516
{
	struct pn533_poll_modulations *cur_mod;
1517 1518
	int rc;

1519
	dev_dbg(dev->dev, "%s\n", __func__);
1520

1521 1522 1523
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1524
		nfc_err(dev->dev, "%s  Poll complete error %d\n",
1525
			__func__, rc);
1526 1527 1528 1529

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
1530
			goto stop_poll;
1531
		} else if (rc < 0) {
1532
			nfc_err(dev->dev,
1533
				"Error %d when running poll\n", rc);
1534 1535
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
1536
	}
1537

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

1540
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1541
		del_timer(&dev->listen_timer);
1542 1543
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1544 1545
	}

1546 1547 1548 1549
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1550

1551
	if (!dev->poll_mod_count) {
1552
		dev_dbg(dev->dev, "Polling has been stopped\n");
1553 1554 1555
		goto done;
	}

1556
	pn533_poll_next_mod(dev);
1557 1558
	/* Not target found, turn radio off */
	queue_work(dev->wq, &dev->rf_work);
S
Samuel Ortiz 已提交
1559

1560 1561 1562
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1563 1564

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

S
Samuel Ortiz 已提交
1567 1568
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1569
	return rc;
1570 1571
}

1572 1573
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
1574
{
1575
	struct sk_buff *skb;
1576

1577
	skb = pn533_alloc_skb(dev, mod->len);
1578 1579
	if (!skb)
		return NULL;
1580

1581
	skb_put_data(skb, &mod->data, mod->len);
1582

1583
	return skb;
S
Samuel Ortiz 已提交
1584
}
1585

S
Samuel Ortiz 已提交
1586 1587
static int pn533_send_poll_frame(struct pn533 *dev)
{
1588 1589
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1590
	int rc;
1591
	u8 cmd_code;
1592

1593
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1594

1595
	dev_dbg(dev->dev, "%s mod len %d\n",
1596
		__func__, mod->len);
1597

1598
	if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1599 1600 1601 1602
		dev->poll_dep = 0;
		return pn533_poll_dep(dev->nfc_dev);
	}

1603 1604
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1605
		skb = pn533_alloc_poll_tg_frame(dev);
1606 1607
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1608
		skb = pn533_alloc_poll_in_frame(dev, mod);
1609 1610 1611
	}

	if (!skb) {
1612
		nfc_err(dev->dev, "Failed to allocate skb\n");
1613 1614 1615 1616 1617 1618 1619
		return -ENOMEM;
	}

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

S
Samuel Ortiz 已提交
1623 1624 1625 1626 1627
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
1628
	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
S
Samuel Ortiz 已提交
1629 1630 1631 1632 1633
	struct pn533_poll_modulations *cur_mod;
	int rc;

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

1634
	dev_dbg(dev->dev,
1635 1636
		"%s cancel_listen %d modulation len %d\n",
		__func__, dev->cancel_listen, cur_mod->len);
S
Samuel Ortiz 已提交
1637 1638 1639

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
1640
		dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1641 1642
	}

S
Samuel Ortiz 已提交
1643 1644 1645
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1646

S
Samuel Ortiz 已提交
1647 1648
	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1649 1650
}

1651 1652 1653 1654
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1655
	struct pn533_poll_modulations *cur_mod;
1656
	struct sk_buff *skb;
1657
	u8 rand_mod;
1658
	int rc;
1659

1660
	dev_dbg(dev->dev,
1661 1662
		"%s: im protocols 0x%x tm protocols 0x%x\n",
		__func__, im_protocols, tm_protocols);
1663 1664

	if (dev->tgt_active_prot) {
1665
		nfc_err(dev->dev,
1666
			"Cannot poll with a target already activated\n");
1667 1668 1669
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1670
	if (dev->tgt_mode) {
1671
		nfc_err(dev->dev,
1672
			"Cannot poll while already being activated\n");
S
Samuel Ortiz 已提交
1673 1674 1675
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1676 1677 1678 1679 1680
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1681

S
Samuel Ortiz 已提交
1682 1683
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
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 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
	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);
1749

1750 1751 1752 1753 1754
	/* 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;

1755 1756 1757 1758 1759 1760 1761 1762 1763
	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;
1764 1765
}

1766 1767 1768 1769
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

S
Samuel Ortiz 已提交
1770 1771
	del_timer(&dev->listen_timer);

1772
	if (!dev->poll_mod_count) {
1773
		dev_dbg(dev->dev,
1774
			"Polling operation was not running\n");
1775 1776 1777
		return;
	}

1778
	dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1779
	flush_delayed_work(&dev->poll_work);
1780
	pn533_poll_reset_mod_list(dev);
1781 1782 1783 1784
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1785
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1786
	u16 gt_len;
1787
	int rc;
1788 1789
	struct sk_buff *skb;
	struct sk_buff *resp;
1790

1791
	dev_dbg(dev->dev, "%s\n", __func__);
1792

1793
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1794 1795
	if (!skb)
		return -ENOMEM;
1796

1797 1798
	skb_put_u8(skb, 1); /* TG */
	skb_put_u8(skb, 0); /* Next */
1799

1800 1801 1802
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1803

1804
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1805
	rc = rsp->status & PN533_CMD_RET_MASK;
1806
	if (rc != PN533_CMD_RET_SUCCESS) {
1807
		nfc_err(dev->dev,
1808
			"Target activation failed (error 0x%x)\n", rc);
1809
		dev_kfree_skb(resp);
1810
		return -EIO;
1811
	}
1812

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

1817
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1818
	return rc;
1819 1820
}

1821 1822
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1823 1824 1825 1826
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

1827
	dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1828 1829

	if (dev->poll_mod_count) {
1830
		nfc_err(dev->dev,
1831
			"Cannot activate while polling\n");
1832 1833 1834 1835
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1836
		nfc_err(dev->dev,
1837
			"There is already an active target\n");
1838 1839 1840 1841
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1842
		nfc_err(dev->dev,
1843
			"There is no available target to activate\n");
1844 1845 1846 1847
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1848
		nfc_err(dev->dev,
1849 1850
			"Target doesn't support requested proto %u\n",
			protocol);
1851 1852 1853 1854 1855 1856
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1857
			nfc_err(dev->dev,
1858
				"Activating target with DEP failed %d\n", rc);
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
			return rc;
		}
	}

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

	return 0;
}

1869 1870 1871 1872 1873
static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1874
	dev_dbg(dev->dev, "%s\n", __func__);
1875 1876 1877 1878

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

1879
		nfc_err(dev->dev, "Target release error %d\n", rc);
1880 1881 1882 1883 1884 1885

		return rc;
	}

	rc = resp->data[0] & PN533_CMD_RET_MASK;
	if (rc != PN533_CMD_RET_SUCCESS)
1886
		nfc_err(dev->dev,
1887 1888 1889 1890 1891 1892
			"Error 0x%x when releasing the target\n", rc);

	dev_kfree_skb(resp);
	return rc;
}

1893
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1894
				    struct nfc_target *target, u8 mode)
1895 1896
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1897
	struct sk_buff *skb;
1898 1899
	int rc;

1900
	dev_dbg(dev->dev, "%s\n", __func__);
1901 1902

	if (!dev->tgt_active_prot) {
1903
		nfc_err(dev->dev, "There is no active target\n");
1904 1905 1906 1907
		return;
	}

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

1910
	skb = pn533_alloc_skb(dev, sizeof(u8));
1911 1912
	if (!skb)
		return;
1913

1914
	skb_put_u8(skb, 1); /* TG*/
1915

1916 1917 1918 1919
	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
				  pn533_deactivate_target_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1920
		nfc_err(dev->dev, "Target release error %d\n", rc);
1921
	}
1922 1923
}

1924 1925

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1926
					 struct sk_buff *resp)
1927
{
1928
	struct pn533_cmd_jump_dep_response *rsp;
1929 1930
	u8 target_gt_len;
	int rc;
1931
	u8 active = *(u8 *)arg;
1932 1933

	kfree(arg);
1934

1935 1936
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1937 1938 1939

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1940
		nfc_err(dev->dev,
1941
			"The target does not support DEP\n");
1942 1943
		rc =  -EINVAL;
		goto error;
1944 1945
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1949
	if (rc != PN533_CMD_RET_SUCCESS) {
1950
		nfc_err(dev->dev,
1951
			"Bringing DEP link up failed (error 0x%x)\n", rc);
1952
		goto error;
1953 1954 1955
	}

	if (!dev->tgt_available_prots) {
1956 1957
		struct nfc_target nfc_target;

1958
		dev_dbg(dev->dev, "Creating new target\n");
1959 1960

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1961
		nfc_target.nfcid1_len = 10;
1962
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1963 1964
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
1965
			goto error;
1966 1967 1968 1969 1970 1971 1972

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
1973
	target_gt_len = resp->len - 17;
1974
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1975
					  rsp->gt, target_gt_len);
1976 1977
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
1978 1979
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
1980

1981 1982 1983
error:
	dev_kfree_skb(resp);
	return rc;
1984 1985
}

1986
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1987
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1988
			     u8 comm_mode, u8 *gb, size_t gb_len)
1989 1990
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1991
	struct sk_buff *skb;
1992 1993
	int rc, skb_len;
	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1994
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1995

1996
	dev_dbg(dev->dev, "%s\n", __func__);
1997 1998

	if (dev->poll_mod_count) {
1999
		nfc_err(dev->dev,
2000
			"Cannot bring the DEP link up while polling\n");
2001 2002 2003 2004
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
2005
		nfc_err(dev->dev,
2006
			"There is already an active target\n");
2007 2008 2009
		return -EBUSY;
	}

2010
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
2011
	skb_len += PASSIVE_DATA_LEN;
2012

2013 2014 2015 2016 2017 2018 2019
	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	if (target && !target->nfcid2_len) {
		nfcid3[0] = 0x1;
		nfcid3[1] = 0xfe;
		get_random_bytes(nfcid3 + 2, 6);
	}
2020

2021
	skb = pn533_alloc_skb(dev, skb_len);
2022
	if (!skb)
2023 2024
		return -ENOMEM;

2025 2026
	skb_put_u8(skb, !comm_mode);  /* ActPass */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
2027 2028 2029

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

2031
	/* Copy passive data */
2032
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
2033
	*next |= 1;
2034

2035 2036
	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	if (target && target->nfcid2_len)
2037 2038
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
		       target->nfcid2_len);
2039
	else
2040
		skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
2041
	*next |= 2;
2042

2043
	if (gb != NULL && gb_len > 0) {
2044
		skb_put_data(skb, gb, gb_len);
2045
		*next |= 4; /* We have some Gi */
2046
	} else {
2047
		*next = 0;
2048 2049
	}

2050 2051 2052 2053 2054
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
2055

2056
	*arg = !comm_mode;
2057

2058 2059
	pn533_rf_field(dev->nfc_dev, 0);

2060 2061 2062 2063 2064 2065 2066
	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);
	}
2067 2068 2069 2070 2071 2072

	return rc;
}

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

2075
	dev_dbg(dev->dev, "%s\n", __func__);
2076

S
Samuel Ortiz 已提交
2077 2078
	pn533_poll_reset_mod_list(dev);

2079
	if (dev->tgt_mode || dev->tgt_active_prot)
2080
		dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2081 2082 2083 2084 2085

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

	skb_queue_purge(&dev->resp_q);
2086 2087 2088 2089

	return 0;
}

2090 2091 2092 2093 2094
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
2095 2096 2097 2098 2099
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

2100
	dev_dbg(dev->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

	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;

2113
	dev_dbg(dev->dev, "%s total length %d\n",
2114
		__func__, skb_len);
S
Samuel Ortiz 已提交
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

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

2133
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2134
					struct sk_buff *resp)
2135 2136
{
	struct pn533_data_exchange_arg *arg = _arg;
2137 2138 2139
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
2140

2141
	dev_dbg(dev->dev, "%s\n", __func__);
2142

2143 2144 2145
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
2146 2147
	}

2148 2149 2150 2151 2152
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
2153

2154
	if (ret != PN533_CMD_RET_SUCCESS) {
2155
		nfc_err(dev->dev,
2156
			"Exchanging data failed (error 0x%x)\n", ret);
2157
		rc = -EIO;
2158 2159 2160
		goto error;
	}

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

2163 2164
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
2165 2166 2167 2168 2169 2170 2171 2172 2173
		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 已提交
2174
		return -EINPROGRESS;
2175 2176
	}

S
Samuel Ortiz 已提交
2177
	skb = pn533_build_response(dev);
J
Julia Lawall 已提交
2178 2179
	if (!skb) {
		rc = -ENOMEM;
S
Samuel Ortiz 已提交
2180
		goto error;
J
Julia Lawall 已提交
2181
	}
2182

S
Samuel Ortiz 已提交
2183
	arg->cb(arg->cb_context, skb, 0);
2184 2185 2186 2187
	kfree(arg);
	return 0;

error:
2188 2189
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2190
	skb_queue_purge(&dev->resp_q);
2191
	arg->cb(arg->cb_context, NULL, rc);
2192
	kfree(arg);
2193
	return rc;
2194 2195
}

2196 2197 2198 2199 2200 2201
/*
 * 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)
{
2202 2203 2204
	if (!dev->cmd)
		goto sched_wq;

2205 2206
	dev->cmd->status = status;

2207 2208 2209 2210 2211
	if (status != 0) {
		dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
		goto sched_wq;
	}

2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	if (skb == NULL) {
		pr_err("NULL Frame -> link is dead\n");
		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);

2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
/* 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;
		}

2261 2262 2263 2264 2265 2266
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2267 2268
				*(u8 *)skb_push(frag, sizeof(u8)) =
						(PN533_CMD_MI_MASK | 1);
2269
			else
2270
				*(u8 *)skb_push(frag, sizeof(u8)) =  1; /* TG */
2271
		}
2272

2273
		skb_put_data(frag, skb->data, frag_size);
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287

		/* 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 已提交
2288 2289 2290
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)
2291 2292
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2293
	struct pn533_data_exchange_arg *arg = NULL;
2294 2295
	int rc;

2296
	dev_dbg(dev->dev, "%s\n", __func__);
2297 2298

	if (!dev->tgt_active_prot) {
2299
		nfc_err(dev->dev,
2300
			"Can't exchange data if there is no active target\n");
2301 2302 2303 2304
		rc = -EINVAL;
		goto error;
	}

2305
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2306 2307
	if (!arg) {
		rc = -ENOMEM;
2308
		goto error;
2309 2310 2311 2312 2313
	}

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

2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
	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;
		}
2324
		fallthrough;
2325
	default:
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
		/* 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 {
2338
			*(u8 *)skb_push(skb, sizeof(u8)) =  1; /* TG */
2339
		}
2340 2341 2342 2343 2344 2345

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

		break;
2346 2347
	}

2348 2349 2350
	if (rc < 0) /* rc from send_async */
		goto error;

2351 2352 2353
	return 0;

error:
2354 2355
	kfree(arg);
	dev_kfree_skb(skb);
2356 2357 2358
	return rc;
}

2359
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2360
				  struct sk_buff *resp)
2361
{
2362
	u8 status;
2363

2364
	dev_dbg(dev->dev, "%s\n", __func__);
2365

2366 2367
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2368

2369
	status = resp->data[0];
2370

2371 2372 2373 2374 2375
	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		queue_work(dev->wq, &dev->mi_tm_tx_work);
		return -EINPROGRESS;
	}
2376
	dev_kfree_skb(resp);
2377

2378
	if (status != 0) {
2379 2380
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2381 2382
		dev->tgt_mode = 0;

2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
		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;

2396
	dev_dbg(dev->dev, "%s\n", __func__);
2397

2398
	/* let's split in multiple chunks if size's too big */
2399
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
		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);
2417 2418
	}

2419 2420
error:
	if (rc < 0) {
2421
		dev_kfree_skb(skb);
2422 2423
		skb_queue_purge(&dev->fragment_skb);
	}
2424 2425 2426 2427

	return rc;
}

S
Samuel Ortiz 已提交
2428 2429
static void pn533_wq_mi_recv(struct work_struct *work)
{
2430
	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2431
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2432 2433
	int rc;

2434
	dev_dbg(dev->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
2435

2436
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2437 2438
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2439

2440 2441 2442 2443 2444 2445 2446 2447
	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 已提交
2448

2449 2450
			break;
		}
2451
		fallthrough;
2452
	default:
2453
		skb_put_u8(skb, 1); /*TG*/
S
Samuel Ortiz 已提交
2454

2455 2456 2457 2458 2459
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2460

2461
		break;
S
Samuel Ortiz 已提交
2462 2463
	}

2464
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2465 2466
		return;

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

2470
	dev_kfree_skb(skb);
2471
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2472

2473
error:
2474
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2475
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2476 2477
}

2478 2479 2480 2481 2482 2483
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;

2484
	dev_dbg(dev->dev, "%s\n", __func__);
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510

	/* 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? */
2511 2512
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;
	}

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

2523
	nfc_err(dev->dev,
2524
		"Error %d when trying to perform data_exchange\n", rc);
2525 2526 2527 2528 2529

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

error:
2530
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2531 2532 2533
	queue_work(dev->wq, &dev->cmd_work);
}

2534 2535 2536
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2537 2538 2539
	struct sk_buff *skb;
	struct sk_buff *resp;
	int skb_len;
2540

2541
	dev_dbg(dev->dev, "%s\n", __func__);
2542

2543
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2544

2545
	skb = pn533_alloc_skb(dev, skb_len);
2546 2547
	if (!skb)
		return -ENOMEM;
2548

2549
	skb_put_u8(skb, cfgitem);
2550
	skb_put_data(skb, cfgdata, cfgdata_len);
2551

2552 2553 2554
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2555

2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
	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;

2566
	skb = pn533_alloc_skb(dev, 0);
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
	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;
2581 2582
}

2583
static int pn533_pasori_fw_reset(struct pn533 *dev)
2584
{
2585 2586
	struct sk_buff *skb;
	struct sk_buff *resp;
2587

2588
	dev_dbg(dev->dev, "%s\n", __func__);
2589

2590
	skb = pn533_alloc_skb(dev, sizeof(u8));
2591 2592
	if (!skb)
		return -ENOMEM;
2593

2594
	skb_put_u8(skb, 0x1);
2595

2596 2597 2598
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2599

2600
	dev_kfree_skb(resp);
2601

2602
	return 0;
2603 2604
}

2605 2606 2607 2608 2609 2610
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 已提交
2611 2612
	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;

2613 2614 2615
	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
				     (u8 *)&rf_field, 1);
	if (rc) {
2616
		nfc_err(dev->dev, "Error on setting RF field\n");
2617 2618 2619 2620 2621 2622
		return rc;
	}

	return rc;
}

2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
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;

2633
	skb_put_u8(skb, 0x01);
2634 2635 2636 2637 2638 2639 2640 2641 2642

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

2643
static int pn533_dev_up(struct nfc_dev *nfc_dev)
2644
{
2645
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2646
	int rc;
2647

2648 2649 2650 2651 2652
	if (dev->phy_ops->dev_up) {
		rc = dev->phy_ops->dev_up(dev);
		if (rc)
			return rc;
	}
2653

2654 2655
	if ((dev->device_type == PN533_DEVICE_PN532) ||
		(dev->device_type == PN533_DEVICE_PN532_AUTOPOLL)) {
2656
		rc = pn532_sam_configuration(nfc_dev);
2657 2658 2659 2660 2661

		if (rc)
			return rc;
	}

2662 2663 2664
	return pn533_rf_field(nfc_dev, 1);
}

2665
static int pn533_dev_down(struct nfc_dev *nfc_dev)
2666
{
2667 2668 2669 2670 2671
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int ret;

	ret = pn533_rf_field(nfc_dev, 0);
	if (dev->phy_ops->dev_down && !ret)
2672
		ret = dev->phy_ops->dev_down(dev);
2673 2674

	return ret;
2675 2676
}

2677
static struct nfc_ops pn533_nfc_ops = {
2678 2679
	.dev_up = pn533_dev_up,
	.dev_down = pn533_dev_down,
2680 2681
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2682 2683 2684 2685
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2686
	.im_transceive = pn533_transceive,
2687
	.tm_send = pn533_tm_send,
2688 2689
};

2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
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:
2700
	case PN533_DEVICE_ACR122U:
2701
	case PN533_DEVICE_PN532:
2702
	case PN533_DEVICE_PN532_AUTOPOLL:
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
		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:
2715
		nfc_err(dev->dev, "Unknown device type %d\n",
2716
			dev->device_type);
2717 2718 2719 2720 2721 2722
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
2723
		nfc_err(dev->dev,
2724
			"Error on setting MAX_RETRIES config\n");
2725 2726 2727 2728 2729 2730 2731
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
2732
		nfc_err(dev->dev, "Error on setting RF timings\n");
2733 2734 2735 2736 2737
		return rc;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
2738
	case PN533_DEVICE_PN532:
2739
	case PN533_DEVICE_PN532_AUTOPOLL:
2740 2741 2742
		break;

	case PN533_DEVICE_PASORI:
2743
		pn533_pasori_fw_reset(dev);
2744 2745 2746 2747

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
2748
			nfc_err(dev->dev,
2749
				"Error while settings PASORI config\n");
2750 2751 2752
			return rc;
		}

2753
		pn533_pasori_fw_reset(dev);
2754 2755 2756 2757 2758 2759 2760

		break;
	}

	return 0;
}

2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
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);

2786
struct pn533 *pn53x_common_init(u32 device_type,
2787 2788 2789 2790
				enum pn533_protocol_type protocol_type,
				void *phy,
				struct pn533_phy_ops *phy_ops,
				struct pn533_frame_ops *fops,
2791
				struct device *dev)
2792
{
2793
	struct pn533 *priv;
2794 2795
	int rc = -ENOMEM;

2796 2797 2798
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);
2799

2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
	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)
2824
		goto error;
2825

2826
	timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
2827

2828 2829
	skb_queue_head_init(&priv->resp_q);
	skb_queue_head_init(&priv->fragment_skb);
2830

2831 2832
	INIT_LIST_HEAD(&priv->cmd_queue);
	return priv;
2833 2834

error:
2835 2836
	kfree(priv);
	return ERR_PTR(rc);
2837
}
2838
EXPORT_SYMBOL_GPL(pn53x_common_init);
2839

2840
void pn53x_common_clean(struct pn533 *priv)
2841
{
S
Samuel Ortiz 已提交
2842
	struct pn533_cmd *cmd, *n;
2843

2844 2845
	flush_delayed_work(&priv->poll_work);
	destroy_workqueue(priv->wq);
2846

2847
	skb_queue_purge(&priv->resp_q);
2848

2849
	del_timer(&priv->listen_timer);
2850

2851
	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
S
Samuel Ortiz 已提交
2852 2853 2854 2855
		list_del(&cmd->queue);
		kfree(cmd);
	}

2856
	kfree(priv);
2857
}
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
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);
2892

2893 2894 2895 2896 2897 2898
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);
2899

2900 2901 2902
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>");
2903
MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2904 2905
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");