pn533.c 58.9 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

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

193 194 195 196
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

197 198 199 200 201 202
/* 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;
}

203
/* The rule: value + checksum = 0 */
204
static inline u8 pn533_std_checksum(u8 value)
205 206 207 208 209
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
210
static u8 pn533_std_data_checksum(u8 *data, int datalen)
211 212 213 214 215 216 217
{
	u8 sum = 0;
	int i;

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

218
	return pn533_std_checksum(sum);
219 220
}

221
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
222
{
223
	struct pn533_std_frame *frame = _frame;
224

225
	frame->preamble = 0;
226 227
	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
228
	PN533_FRAME_CMD(frame) = cmd_code;
229 230 231
	frame->datalen = 2;
}

232
static void pn533_std_tx_frame_finish(void *_frame)
233
{
234
	struct pn533_std_frame *frame = _frame;
235

236
	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
237

238 239
	PN533_STD_FRAME_CHECKSUM(frame) =
		pn533_std_data_checksum(frame->data, frame->datalen);
240

241
	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
242 243
}

244
static void pn533_std_tx_update_payload_len(void *_frame, int len)
245
{
246
	struct pn533_std_frame *frame = _frame;
247 248 249 250

	frame->datalen += len;
}

251
static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
252 253
{
	u8 checksum;
254
	struct pn533_std_frame *stdf = _frame;
255

256
	if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
257 258
		return false;

259 260
	if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
		/* Standard frame code */
261
		dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
262 263 264 265 266 267 268 269 270 271 272 273

		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;

274 275
		dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;

276 277 278 279 280 281 282 283 284 285
		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;
	}
286 287 288 289

	return true;
}

290
bool pn533_rx_frame_is_ack(void *_frame)
291
{
292 293
	struct pn533_std_frame *frame = _frame;

294
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
295 296 297 298 299 300 301
		return false;

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

	return true;
}
302
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
303

304
static inline int pn533_std_rx_frame_size(void *frame)
305
{
306
	struct pn533_std_frame *f = frame;
307

308 309 310 311 312 313 314 315
	/* 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;
	}

316 317
	return sizeof(struct pn533_std_frame) + f->datalen +
	       PN533_STD_FRAME_TAIL_LEN;
318 319
}

320
static u8 pn533_std_get_cmd_code(void *frame)
321
{
322
	struct pn533_std_frame *f = frame;
323
	struct pn533_ext_frame *eif = frame;
324

325 326 327 328
	if (PN533_STD_IS_EXTENDED(f))
		return PN533_FRAME_CMD(eif);
	else
		return PN533_FRAME_CMD(f);
329 330
}

331 332 333 334 335 336 337 338
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);


339
static struct pn533_frame_ops pn533_std_frame_ops = {
340 341 342 343 344 345 346 347 348 349 350 351 352
	.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,
353 354 355 356
};

static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
				  struct sk_buff *skb)
357 358 359
{
	/* payload is already there, just update datalen */
	int payload_len = skb->len;
360
	struct pn533_frame_ops *ops = dev->ops;
361 362


363 364
	skb_push(skb, ops->tx_header_len);
	skb_put(skb, ops->tx_tail_len);
365

366 367 368
	ops->tx_frame_init(skb->data, cmd_code);
	ops->tx_update_payload_len(skb->data, payload_len);
	ops->tx_frame_finish(skb->data);
369 370
}

371
static int pn533_send_async_complete(struct pn533 *dev)
372
{
373
	struct pn533_cmd *cmd = dev->cmd;
374 375
	struct sk_buff *resp;
	int status, rc = 0;
376

377 378 379 380
	if (!cmd) {
		dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
		goto done;
	}
381

382
	dev_kfree_skb(cmd->req);
383

384 385
	status = cmd->status;
	resp = cmd->resp;
386

387
	if (status < 0) {
388 389
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
390
		dev_kfree_skb(resp);
391
		goto done;
392 393
	}

394 395 396 397 398 399 400 401
	/* 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);
	}
402

403
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
404

405
done:
406
	kfree(cmd);
407
	dev->cmd = NULL;
408 409 410 411
	return rc;
}

static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
412
			      struct sk_buff *req,
413 414 415 416 417 418
			      pn533_send_async_complete_t complete_cb,
			      void *complete_cb_context)
{
	struct pn533_cmd *cmd;
	int rc = 0;

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

421 422
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
423 424
		return -ENOMEM;

425
	cmd->code = cmd_code;
426 427 428
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
429

430
	pn533_build_cmd_frame(dev, cmd_code, req);
431 432 433 434

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
435
		dev->cmd = cmd;
436
		rc = dev->phy_ops->send_frame(dev, req);
437 438
		if (rc) {
			dev->cmd = NULL;
439
			goto error;
440
		}
441 442 443 444 445

		dev->cmd_pending = 1;
		goto unlock;
	}

446
	dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
447
		__func__, cmd_code);
448 449 450 451 452 453 454

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

	goto unlock;

error:
455
	kfree(cmd);
456 457 458
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
459 460 461 462 463 464 465 466 467
}

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;

468
	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
469 470 471
				complete_cb_context);

	return rc;
472 473 474 475 476 477 478 479 480
}

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;

481
	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
482
				complete_cb_context);
483 484 485 486

	return rc;
}

487 488 489
/*
 * pn533_send_cmd_direct_async
 *
490
 * The function sends a piority cmd directly to the chip omitting the cmd
491 492 493 494 495 496 497 498 499
 * 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)
{
500
	struct pn533_cmd *cmd;
501 502
	int rc;

503
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
504
	if (!cmd)
505 506
		return -ENOMEM;

507
	cmd->code = cmd_code;
508 509 510
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
511

512
	pn533_build_cmd_frame(dev, cmd_code, req);
513

514
	dev->cmd = cmd;
515
	rc = dev->phy_ops->send_frame(dev, req);
516 517
	if (rc < 0) {
		dev->cmd = NULL;
518
		kfree(cmd);
519
	}
520 521 522 523

	return rc;
}

524 525 526 527 528 529 530 531 532 533
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 已提交
534 535 536 537
static void pn533_wq_cmd(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, cmd_work);
	struct pn533_cmd *cmd;
538
	int rc;
S
Samuel Ortiz 已提交
539 540 541 542 543 544 545 546 547 548 549

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

550 551
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
552 553
	mutex_unlock(&dev->cmd_lock);

554
	dev->cmd = cmd;
555
	rc = dev->phy_ops->send_frame(dev, cmd->req);
556
	if (rc < 0) {
557
		dev->cmd = NULL;
558
		dev_kfree_skb(cmd->req);
559
		kfree(cmd);
560
		return;
561
	}
562

S
Samuel Ortiz 已提交
563 564
}

565
struct pn533_sync_cmd_response {
566
	struct sk_buff *resp;
567 568 569
	struct completion done;
};

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
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
589
 *     as it's been already freed at the beginning of RX path by
590 591 592 593 594 595 596
 *     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.
 *
597
 */
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
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;
}

618
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
619 620 621
{
	struct sk_buff *skb;

622
	skb = alloc_skb(dev->ops->tx_header_len +
623
			size +
624
			dev->ops->tx_tail_len, GFP_KERNEL);
625 626

	if (skb)
627
		skb_reserve(skb, dev->ops->tx_header_len);
628 629 630 631

	return skb;
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
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;

664 665 666 667
	/*
	 * The length check of nfcid[] and ats[] are not being performed because
	 * the values are not being used
	 */
668 669 670 671 672 673

	/* 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 &&
674 675 676
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
677 678 679 680 681 682 683 684 685 686 687 688 689 690
		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;

691
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

	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 已提交
714 715
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
716 717 718 719 720 721 722

	return 0;
}

struct pn533_target_felica {
	u8 pol_res;
	u8 opcode;
723
	u8 nfcid2[NFC_NFCID2_MAXSIZE];
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	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;

749
	tgt_felica = (struct pn533_target_felica *)tgt_data;
750 751 752 753

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

754 755
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
756 757 758 759
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
760 761 762
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

763 764 765
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	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 &&
788 789 790
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
791 792 793 794 795 796 797 798 799 800
		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;

801
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
802 803 804 805 806 807

	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);
808 809
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

	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;

860
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
861 862 863 864

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

865
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
866 867 868 869

	return 0;
}

870
static void pn533_poll_reset_mod_list(struct pn533 *dev);
871 872
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
873 874 875 876
{
	struct nfc_target nfc_tgt;
	int rc;

877
	dev_dbg(dev->dev, "%s: modulation=%d\n",
878
		__func__, dev->poll_mod_curr);
879

880
	if (tg != 1)
881 882
		return -EPROTO;

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

885 886
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
887
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
888 889 890
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
891
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
892 893
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
894
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
895 896
		break;
	case PN533_POLL_MOD_847KBPS_B:
897
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
898 899
		break;
	default:
900
		nfc_err(dev->dev,
901
			"Unknown current poll modulation\n");
902 903 904 905 906 907 908
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
909
		dev_dbg(dev->dev,
910
			"The Tg found doesn't have the desired protocol\n");
911 912 913
		return -EAGAIN;
	}

914
	dev_dbg(dev->dev,
915 916
		"Target found - supported protocols: 0x%x\n",
		nfc_tgt.supported_protocols);
917 918 919

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

920
	pn533_poll_reset_mod_list(dev);
921 922 923 924 925
	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);

	return 0;
}

S
Samuel Ortiz 已提交
926 927 928 929 930
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

931 932 933 934 935 936 937 938
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] =
939
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
940 941 942
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
943 944
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
945 946 947
{
	pn533_poll_reset_mod_list(dev);

948 949 950
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
951 952
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

953 954
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
955 956 957 958
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
959
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
960 961
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

962
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
963 964
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
965 966
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
967 968
}

969
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
970
{
971 972
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
973

974
	/* Toggle the DEP polling */
975 976
	if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
		dev->poll_dep = 1;
977

978 979 980 981 982 983 984
	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);
985 986

		/* We must stop the poll after a valid target found */
987
		if (rc == 0)
S
Samuel Ortiz 已提交
988
			return 0;
989 990
	}

S
Samuel Ortiz 已提交
991
	return -EAGAIN;
992 993
}

994
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
995
{
996
	struct sk_buff *skb;
997
	u8 *felica, *nfcid3;
998

999 1000 1001
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1002 1003 1004 1005
	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 */
1006

1007 1008 1009
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1010

1011 1012 1013 1014
	unsigned int skb_len = 36 + /*
				     * mode (1), mifare (6),
				     * felica (18), nfcid3 (10), gb_len (1)
				     */
1015 1016
			       gbytes_len +
			       1;  /* len Tk*/
1017

1018
	skb = pn533_alloc_skb(dev, skb_len);
1019 1020
	if (!skb)
		return NULL;
1021 1022

	/* DEP support only */
1023
	skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1024 1025

	/* MIFARE params */
1026
	skb_put_data(skb, mifare_params, 6);
1027 1028

	/* Felica params */
1029
	felica = skb_put_data(skb, felica_params, 18);
1030
	get_random_bytes(felica + 2, 6);
1031 1032

	/* NFCID3 */
J
Johannes Berg 已提交
1033
	nfcid3 = skb_put_zero(skb, 10);
1034
	memcpy(nfcid3, felica, 8);
1035 1036

	/* General bytes */
1037
	skb_put_u8(skb, gbytes_len);
1038

1039
	skb_put_data(skb, gbytes, gbytes_len);
1040

1041
	/* Len Tk */
1042
	skb_put_u8(skb, 0);
1043

1044
	return skb;
1045 1046
}

1047 1048 1049
static void pn533_wq_tm_mi_recv(struct work_struct *work);
static struct sk_buff *pn533_build_response(struct pn533 *dev);

1050
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1051
				      struct sk_buff *resp)
1052
{
1053 1054 1055
	struct sk_buff *skb;
	u8 status, ret, mi;
	int rc;
1056

1057
	dev_dbg(dev->dev, "%s\n", __func__);
1058

1059 1060
	if (IS_ERR(resp)) {
		skb_queue_purge(&dev->resp_q);
1061
		return PTR_ERR(resp);
1062
	}
1063

1064
	status = resp->data[0];
1065 1066 1067 1068

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

1069
	skb_pull(resp, sizeof(status));
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	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;
1087 1088
	}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	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;

1106
	dev_dbg(dev->dev, "%s\n", __func__);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

	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);
1120 1121
}

1122 1123 1124 1125 1126 1127 1128 1129
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;

1130
	dev_dbg(dev->dev, "%s\n", __func__);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

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

1152
	dev_err(dev->dev,
1153 1154 1155 1156 1157
		"Error %d when trying to perform set meta data_exchange", rc);

	dev_kfree_skb(skb);

error:
1158
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
1159 1160 1161
	queue_work(dev->wq, &dev->cmd_work);
}

1162 1163 1164
static void pn533_wq_tg_get_data(struct work_struct *work)
{
	struct pn533 *dev = container_of(work, struct pn533, tg_work);
1165 1166
	struct sk_buff *skb;
	int rc;
1167

1168
	dev_dbg(dev->dev, "%s\n", __func__);
1169

1170
	skb = pn533_alloc_skb(dev, 0);
1171
	if (!skb)
1172 1173
		return;

1174 1175
	rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
				   pn533_tm_get_data_complete, NULL);
1176

1177 1178
	if (rc < 0)
		dev_kfree_skb(skb);
1179 1180
}

1181
#define ATR_REQ_GB_OFFSET 17
1182
static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1183
{
1184
	u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1185
	size_t gb_len;
1186
	int rc;
1187

1188
	dev_dbg(dev->dev, "%s\n", __func__);
1189

1190
	if (resp->len < ATR_REQ_GB_OFFSET + 1)
1191 1192
		return -EINVAL;

1193 1194
	mode = resp->data[0];
	cmd = &resp->data[1];
1195

1196
	dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1197
		mode, resp->len);
1198

1199 1200
	if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
	    PN533_INIT_TARGET_RESP_ACTIVE)
1201 1202
		comm_mode = NFC_COMM_ACTIVE;

1203
	if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1204 1205
		return -EOPNOTSUPP;

1206 1207
	gb = cmd + ATR_REQ_GB_OFFSET;
	gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1208

1209 1210 1211
	rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
			      comm_mode, gb, gb_len);
	if (rc < 0) {
1212
		nfc_err(dev->dev,
1213
			"Error when signaling target activation\n");
1214 1215 1216
		return rc;
	}

S
Samuel Ortiz 已提交
1217
	dev->tgt_mode = 1;
1218 1219 1220
	queue_work(dev->wq, &dev->tg_work);

	return 0;
1221 1222
}

1223
static void pn533_listen_mode_timer(struct timer_list *t)
1224
{
1225
	struct pn533 *dev = from_timer(dev, t, listen_timer);
S
Samuel Ortiz 已提交
1226

1227
	dev_dbg(dev->dev, "Listen mode timeout\n");
S
Samuel Ortiz 已提交
1228 1229 1230 1231 1232

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

1233 1234
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
S
Samuel Ortiz 已提交
1235 1236
}

1237 1238 1239 1240 1241
static int pn533_rf_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1242
	dev_dbg(dev->dev, "%s\n", __func__);
1243 1244 1245 1246

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

1247
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1248 1249 1250 1251

		return rc;
	}

1252 1253
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

	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;

1265
	dev_dbg(dev->dev, "%s\n", __func__);
1266 1267 1268 1269 1270

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

1271 1272
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1273 1274 1275 1276 1277

	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
				  pn533_rf_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1278
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1279 1280 1281
	}
}

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
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;
	}

1304
	dev_dbg(dev->dev, "Creating new target");
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

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

1342
	dev_dbg(dev->dev, "%s", __func__);
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365

	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;

1366 1367
	skb_put_u8(skb, 0x01);  /* Active */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1368 1369 1370 1371 1372

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

	/* Copy passive data */
1373
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1374 1375 1376
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1377
	skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1378 1379
	*next |= 2;

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

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

	if (rc < 0)
		dev_kfree_skb(skb);

	return rc;
}

S
Samuel Ortiz 已提交
1392
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1393
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1394 1395
{
	struct pn533_poll_modulations *cur_mod;
1396 1397
	int rc;

1398
	dev_dbg(dev->dev, "%s\n", __func__);
1399

1400 1401 1402
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1403
		nfc_err(dev->dev, "%s  Poll complete error %d\n",
1404
			__func__, rc);
1405 1406 1407 1408

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
1409
			goto stop_poll;
1410
		} else if (rc < 0) {
1411
			nfc_err(dev->dev,
1412
				"Error %d when running poll\n", rc);
1413 1414
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
1415
	}
1416

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

1419
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1420
		del_timer(&dev->listen_timer);
1421 1422
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1423 1424
	}

1425 1426 1427 1428
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1429

1430
	if (!dev->poll_mod_count) {
1431
		dev_dbg(dev->dev, "Polling has been stopped\n");
1432 1433 1434
		goto done;
	}

1435
	pn533_poll_next_mod(dev);
1436 1437
	/* Not target found, turn radio off */
	queue_work(dev->wq, &dev->rf_work);
S
Samuel Ortiz 已提交
1438

1439 1440 1441
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1442 1443

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

S
Samuel Ortiz 已提交
1446 1447
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1448
	return rc;
1449 1450
}

1451 1452
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
1453
{
1454
	struct sk_buff *skb;
1455

1456
	skb = pn533_alloc_skb(dev, mod->len);
1457 1458
	if (!skb)
		return NULL;
1459

1460
	skb_put_data(skb, &mod->data, mod->len);
1461

1462
	return skb;
S
Samuel Ortiz 已提交
1463
}
1464

S
Samuel Ortiz 已提交
1465 1466
static int pn533_send_poll_frame(struct pn533 *dev)
{
1467 1468
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1469
	int rc;
1470
	u8 cmd_code;
1471

1472
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1473

1474
	dev_dbg(dev->dev, "%s mod len %d\n",
1475
		__func__, mod->len);
1476

1477
	if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1478 1479 1480 1481
		dev->poll_dep = 0;
		return pn533_poll_dep(dev->nfc_dev);
	}

1482 1483
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1484
		skb = pn533_alloc_poll_tg_frame(dev);
1485 1486
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1487
		skb = pn533_alloc_poll_in_frame(dev, mod);
1488 1489 1490
	}

	if (!skb) {
1491
		nfc_err(dev->dev, "Failed to allocate skb\n");
1492 1493 1494 1495 1496 1497 1498
		return -ENOMEM;
	}

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

S
Samuel Ortiz 已提交
1502 1503 1504 1505 1506
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
1507
	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
S
Samuel Ortiz 已提交
1508 1509 1510 1511 1512
	struct pn533_poll_modulations *cur_mod;
	int rc;

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

1513
	dev_dbg(dev->dev,
1514 1515
		"%s cancel_listen %d modulation len %d\n",
		__func__, dev->cancel_listen, cur_mod->len);
S
Samuel Ortiz 已提交
1516 1517 1518

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
1519
		dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1520 1521
	}

S
Samuel Ortiz 已提交
1522 1523 1524
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1525

S
Samuel Ortiz 已提交
1526 1527
	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1528 1529
}

1530 1531 1532 1533
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1534
	struct pn533_poll_modulations *cur_mod;
1535
	u8 rand_mod;
1536
	int rc;
1537

1538
	dev_dbg(dev->dev,
1539 1540
		"%s: im protocols 0x%x tm protocols 0x%x\n",
		__func__, im_protocols, tm_protocols);
1541 1542

	if (dev->tgt_active_prot) {
1543
		nfc_err(dev->dev,
1544
			"Cannot poll with a target already activated\n");
1545 1546 1547
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1548
	if (dev->tgt_mode) {
1549
		nfc_err(dev->dev,
1550
			"Cannot poll while already being activated\n");
S
Samuel Ortiz 已提交
1551 1552 1553
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1554 1555 1556 1557 1558
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1559

S
Samuel Ortiz 已提交
1560 1561 1562
	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
1563

1564 1565 1566 1567 1568
	/* 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;

1569 1570 1571 1572 1573 1574 1575 1576 1577
	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;
1578 1579
}

1580 1581 1582 1583
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

S
Samuel Ortiz 已提交
1584 1585
	del_timer(&dev->listen_timer);

1586
	if (!dev->poll_mod_count) {
1587
		dev_dbg(dev->dev,
1588
			"Polling operation was not running\n");
1589 1590 1591
		return;
	}

1592
	dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1593
	flush_delayed_work(&dev->poll_work);
1594
	pn533_poll_reset_mod_list(dev);
1595 1596 1597 1598
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1599
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1600
	u16 gt_len;
1601
	int rc;
1602 1603
	struct sk_buff *skb;
	struct sk_buff *resp;
1604

1605
	dev_dbg(dev->dev, "%s\n", __func__);
1606

1607
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1608 1609
	if (!skb)
		return -ENOMEM;
1610

1611 1612
	skb_put_u8(skb, 1); /* TG */
	skb_put_u8(skb, 0); /* Next */
1613

1614 1615 1616
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1617

1618
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1619
	rc = rsp->status & PN533_CMD_RET_MASK;
1620
	if (rc != PN533_CMD_RET_SUCCESS) {
1621
		nfc_err(dev->dev,
1622
			"Target activation failed (error 0x%x)\n", rc);
1623
		dev_kfree_skb(resp);
1624
		return -EIO;
1625
	}
1626

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

1631
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1632
	return rc;
1633 1634
}

1635 1636
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1637 1638 1639 1640
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

1641
	dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1642 1643

	if (dev->poll_mod_count) {
1644
		nfc_err(dev->dev,
1645
			"Cannot activate while polling\n");
1646 1647 1648 1649
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1650
		nfc_err(dev->dev,
1651
			"There is already an active target\n");
1652 1653 1654 1655
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1656
		nfc_err(dev->dev,
1657
			"There is no available target to activate\n");
1658 1659 1660 1661
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1662
		nfc_err(dev->dev,
1663 1664
			"Target doesn't support requested proto %u\n",
			protocol);
1665 1666 1667 1668 1669 1670
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1671
			nfc_err(dev->dev,
1672
				"Activating target with DEP failed %d\n", rc);
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
			return rc;
		}
	}

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

	return 0;
}

1683 1684 1685 1686 1687
static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1688
	dev_dbg(dev->dev, "%s\n", __func__);
1689 1690 1691 1692

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

1693
		nfc_err(dev->dev, "Target release error %d\n", rc);
1694 1695 1696 1697 1698 1699

		return rc;
	}

	rc = resp->data[0] & PN533_CMD_RET_MASK;
	if (rc != PN533_CMD_RET_SUCCESS)
1700
		nfc_err(dev->dev,
1701 1702 1703 1704 1705 1706
			"Error 0x%x when releasing the target\n", rc);

	dev_kfree_skb(resp);
	return rc;
}

1707
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1708
				    struct nfc_target *target, u8 mode)
1709 1710
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1711
	struct sk_buff *skb;
1712 1713
	int rc;

1714
	dev_dbg(dev->dev, "%s\n", __func__);
1715 1716

	if (!dev->tgt_active_prot) {
1717
		nfc_err(dev->dev, "There is no active target\n");
1718 1719 1720 1721
		return;
	}

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

1724
	skb = pn533_alloc_skb(dev, sizeof(u8));
1725 1726
	if (!skb)
		return;
1727

1728
	skb_put_u8(skb, 1); /* TG*/
1729

1730 1731 1732 1733
	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
				  pn533_deactivate_target_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1734
		nfc_err(dev->dev, "Target release error %d\n", rc);
1735
	}
1736 1737
}

1738 1739

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1740
					 struct sk_buff *resp)
1741
{
1742
	struct pn533_cmd_jump_dep_response *rsp;
1743 1744
	u8 target_gt_len;
	int rc;
1745
	u8 active = *(u8 *)arg;
1746 1747

	kfree(arg);
1748

1749 1750
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1751 1752 1753

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1754
		nfc_err(dev->dev,
1755
			"The target does not support DEP\n");
1756 1757
		rc =  -EINVAL;
		goto error;
1758 1759
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1763
	if (rc != PN533_CMD_RET_SUCCESS) {
1764
		nfc_err(dev->dev,
1765
			"Bringing DEP link up failed (error 0x%x)\n", rc);
1766
		goto error;
1767 1768 1769
	}

	if (!dev->tgt_available_prots) {
1770 1771
		struct nfc_target nfc_target;

1772
		dev_dbg(dev->dev, "Creating new target\n");
1773 1774

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1775
		nfc_target.nfcid1_len = 10;
1776
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1777 1778
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
1779
			goto error;
1780 1781 1782 1783 1784 1785 1786

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
1787
	target_gt_len = resp->len - 17;
1788
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1789
					  rsp->gt, target_gt_len);
1790 1791
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
1792 1793
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
1794

1795 1796 1797
error:
	dev_kfree_skb(resp);
	return rc;
1798 1799
}

1800
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1801
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1802
			     u8 comm_mode, u8 *gb, size_t gb_len)
1803 1804
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1805
	struct sk_buff *skb;
1806 1807
	int rc, skb_len;
	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1808
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1809

1810
	dev_dbg(dev->dev, "%s\n", __func__);
1811 1812

	if (dev->poll_mod_count) {
1813
		nfc_err(dev->dev,
1814
			"Cannot bring the DEP link up while polling\n");
1815 1816 1817 1818
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1819
		nfc_err(dev->dev,
1820
			"There is already an active target\n");
1821 1822 1823
		return -EBUSY;
	}

1824
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1825
	skb_len += PASSIVE_DATA_LEN;
1826

1827 1828 1829 1830 1831 1832 1833
	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	if (target && !target->nfcid2_len) {
		nfcid3[0] = 0x1;
		nfcid3[1] = 0xfe;
		get_random_bytes(nfcid3 + 2, 6);
	}
1834

1835
	skb = pn533_alloc_skb(dev, skb_len);
1836
	if (!skb)
1837 1838
		return -ENOMEM;

1839 1840
	skb_put_u8(skb, !comm_mode);  /* ActPass */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1841 1842 1843

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

1845
	/* Copy passive data */
1846
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1847
	*next |= 1;
1848

1849 1850
	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	if (target && target->nfcid2_len)
1851 1852
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
		       target->nfcid2_len);
1853
	else
1854
		skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1855
	*next |= 2;
1856

1857
	if (gb != NULL && gb_len > 0) {
1858
		skb_put_data(skb, gb, gb_len);
1859
		*next |= 4; /* We have some Gi */
1860
	} else {
1861
		*next = 0;
1862 1863
	}

1864 1865 1866 1867 1868
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
1869

1870
	*arg = !comm_mode;
1871

1872 1873
	pn533_rf_field(dev->nfc_dev, 0);

1874 1875 1876 1877 1878 1879 1880
	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);
	}
1881 1882 1883 1884 1885 1886

	return rc;
}

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

1889
	dev_dbg(dev->dev, "%s\n", __func__);
1890

S
Samuel Ortiz 已提交
1891 1892
	pn533_poll_reset_mod_list(dev);

1893
	if (dev->tgt_mode || dev->tgt_active_prot)
1894
		dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
1895 1896 1897 1898 1899

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

	skb_queue_purge(&dev->resp_q);
1900 1901 1902 1903

	return 0;
}

1904 1905 1906 1907 1908
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
1909 1910 1911 1912 1913
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

1914
	dev_dbg(dev->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

	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;

1927
	dev_dbg(dev->dev, "%s total length %d\n",
1928
		__func__, skb_len);
S
Samuel Ortiz 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946

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

1947
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1948
					struct sk_buff *resp)
1949 1950
{
	struct pn533_data_exchange_arg *arg = _arg;
1951 1952 1953
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
1954

1955
	dev_dbg(dev->dev, "%s\n", __func__);
1956

1957 1958 1959
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
1960 1961
	}

1962 1963 1964 1965 1966
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
1967

1968
	if (ret != PN533_CMD_RET_SUCCESS) {
1969
		nfc_err(dev->dev,
1970
			"Exchanging data failed (error 0x%x)\n", ret);
1971
		rc = -EIO;
1972 1973 1974
		goto error;
	}

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

1977 1978
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
1979 1980 1981 1982 1983 1984 1985 1986 1987
		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 已提交
1988
		return -EINPROGRESS;
1989 1990
	}

S
Samuel Ortiz 已提交
1991
	skb = pn533_build_response(dev);
J
Julia Lawall 已提交
1992 1993
	if (!skb) {
		rc = -ENOMEM;
S
Samuel Ortiz 已提交
1994
		goto error;
J
Julia Lawall 已提交
1995
	}
1996

S
Samuel Ortiz 已提交
1997
	arg->cb(arg->cb_context, skb, 0);
1998 1999 2000 2001
	kfree(arg);
	return 0;

error:
2002 2003
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2004
	skb_queue_purge(&dev->resp_q);
2005
	arg->cb(arg->cb_context, NULL, rc);
2006
	kfree(arg);
2007
	return rc;
2008 2009
}

2010 2011 2012 2013 2014 2015
/*
 * 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)
{
2016 2017 2018
	if (!dev->cmd)
		goto sched_wq;

2019 2020
	dev->cmd->status = status;

2021 2022 2023 2024 2025
	if (status != 0) {
		dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
		goto sched_wq;
	}

2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	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);

2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
/* 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;
		}

2075 2076 2077 2078 2079 2080
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2081 2082
				*(u8 *)skb_push(frag, sizeof(u8)) =
						(PN533_CMD_MI_MASK | 1);
2083
			else
2084
				*(u8 *)skb_push(frag, sizeof(u8)) =  1; /* TG */
2085
		}
2086

2087
		skb_put_data(frag, skb->data, frag_size);
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

		/* 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 已提交
2102 2103 2104
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)
2105 2106
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2107
	struct pn533_data_exchange_arg *arg = NULL;
2108 2109
	int rc;

2110
	dev_dbg(dev->dev, "%s\n", __func__);
2111 2112

	if (!dev->tgt_active_prot) {
2113
		nfc_err(dev->dev,
2114
			"Can't exchange data if there is no active target\n");
2115 2116 2117 2118
		rc = -EINVAL;
		goto error;
	}

2119
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2120 2121
	if (!arg) {
		rc = -ENOMEM;
2122
		goto error;
2123 2124 2125 2126 2127
	}

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

2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
	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;
		}
2138
		/* fall through */
2139
	default:
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
		/* 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 {
2152
			*(u8 *)skb_push(skb, sizeof(u8)) =  1; /* TG */
2153
		}
2154 2155 2156 2157 2158 2159

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

		break;
2160 2161
	}

2162 2163 2164
	if (rc < 0) /* rc from send_async */
		goto error;

2165 2166 2167
	return 0;

error:
2168 2169
	kfree(arg);
	dev_kfree_skb(skb);
2170 2171 2172
	return rc;
}

2173
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2174
				  struct sk_buff *resp)
2175
{
2176
	u8 status;
2177

2178
	dev_dbg(dev->dev, "%s\n", __func__);
2179

2180 2181
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2182

2183
	status = resp->data[0];
2184

2185 2186 2187 2188 2189
	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		queue_work(dev->wq, &dev->mi_tm_tx_work);
		return -EINPROGRESS;
	}
2190
	dev_kfree_skb(resp);
2191

2192
	if (status != 0) {
2193 2194
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2195 2196
		dev->tgt_mode = 0;

2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
		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;

2210
	dev_dbg(dev->dev, "%s\n", __func__);
2211

2212
	/* let's split in multiple chunks if size's too big */
2213
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
		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);
2231 2232
	}

2233 2234
error:
	if (rc < 0) {
2235
		dev_kfree_skb(skb);
2236 2237
		skb_queue_purge(&dev->fragment_skb);
	}
2238 2239 2240 2241

	return rc;
}

S
Samuel Ortiz 已提交
2242 2243
static void pn533_wq_mi_recv(struct work_struct *work)
{
2244
	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2245
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2246 2247
	int rc;

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

2250
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2251 2252
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2253

2254 2255 2256 2257 2258 2259 2260 2261
	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 已提交
2262

2263 2264
			break;
		}
2265
		/* fall through */
2266
	default:
2267
		skb_put_u8(skb, 1); /*TG*/
S
Samuel Ortiz 已提交
2268

2269 2270 2271 2272 2273
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2274

2275
		break;
S
Samuel Ortiz 已提交
2276 2277
	}

2278
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2279 2280
		return;

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

2284
	dev_kfree_skb(skb);
2285
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2286

2287
error:
2288
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2289
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2290 2291
}

2292 2293 2294 2295 2296 2297
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;

2298
	dev_dbg(dev->dev, "%s\n", __func__);
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324

	/* 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? */
2325 2326
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;
	}

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

2337
	nfc_err(dev->dev,
2338
		"Error %d when trying to perform data_exchange\n", rc);
2339 2340 2341 2342 2343

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

error:
2344
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2345 2346 2347
	queue_work(dev->wq, &dev->cmd_work);
}

2348 2349 2350
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2351 2352 2353
	struct sk_buff *skb;
	struct sk_buff *resp;
	int skb_len;
2354

2355
	dev_dbg(dev->dev, "%s\n", __func__);
2356

2357
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2358

2359
	skb = pn533_alloc_skb(dev, skb_len);
2360 2361
	if (!skb)
		return -ENOMEM;
2362

2363
	skb_put_u8(skb, cfgitem);
2364
	skb_put_data(skb, cfgdata, cfgdata_len);
2365

2366 2367 2368
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2369

2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	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;

2380
	skb = pn533_alloc_skb(dev, 0);
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
	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;
2395 2396
}

2397
static int pn533_pasori_fw_reset(struct pn533 *dev)
2398
{
2399 2400
	struct sk_buff *skb;
	struct sk_buff *resp;
2401

2402
	dev_dbg(dev->dev, "%s\n", __func__);
2403

2404
	skb = pn533_alloc_skb(dev, sizeof(u8));
2405 2406
	if (!skb)
		return -ENOMEM;
2407

2408
	skb_put_u8(skb, 0x1);
2409

2410 2411 2412
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2413

2414
	dev_kfree_skb(resp);
2415

2416
	return 0;
2417 2418
}

2419 2420 2421 2422 2423 2424
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 已提交
2425 2426
	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;

2427 2428 2429
	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
				     (u8 *)&rf_field, 1);
	if (rc) {
2430
		nfc_err(dev->dev, "Error on setting RF field\n");
2431 2432 2433 2434 2435 2436
		return rc;
	}

	return rc;
}

2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
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;

2447
	skb_put_u8(skb, 0x01);
2448 2449 2450 2451 2452 2453 2454 2455 2456

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

2457
static int pn533_dev_up(struct nfc_dev *nfc_dev)
2458
{
2459 2460
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

2461 2462 2463
	if (dev->phy_ops->dev_up)
		dev->phy_ops->dev_up(dev);

2464 2465 2466 2467 2468 2469 2470
	if (dev->device_type == PN533_DEVICE_PN532) {
		int rc = pn532_sam_configuration(nfc_dev);

		if (rc)
			return rc;
	}

2471 2472 2473
	return pn533_rf_field(nfc_dev, 1);
}

2474
static int pn533_dev_down(struct nfc_dev *nfc_dev)
2475
{
2476 2477 2478 2479 2480 2481 2482 2483
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int ret;

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

	return ret;
2484 2485
}

2486
static struct nfc_ops pn533_nfc_ops = {
2487 2488
	.dev_up = pn533_dev_up,
	.dev_down = pn533_dev_down,
2489 2490
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2491 2492 2493 2494
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2495
	.im_transceive = pn533_transceive,
2496
	.tm_send = pn533_tm_send,
2497 2498
};

2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
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:
2509
	case PN533_DEVICE_ACR122U:
2510
	case PN533_DEVICE_PN532:
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
		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:
2523
		nfc_err(dev->dev, "Unknown device type %d\n",
2524
			dev->device_type);
2525 2526 2527 2528 2529 2530
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
2531
		nfc_err(dev->dev,
2532
			"Error on setting MAX_RETRIES config\n");
2533 2534 2535 2536 2537 2538 2539
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
2540
		nfc_err(dev->dev, "Error on setting RF timings\n");
2541 2542 2543 2544 2545
		return rc;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
2546
	case PN533_DEVICE_PN532:
2547 2548 2549
		break;

	case PN533_DEVICE_PASORI:
2550
		pn533_pasori_fw_reset(dev);
2551 2552 2553 2554

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
2555
			nfc_err(dev->dev,
2556
				"Error while settings PASORI config\n");
2557 2558 2559
			return rc;
		}

2560
		pn533_pasori_fw_reset(dev);
2561 2562 2563 2564 2565 2566 2567

		break;
	}

	return 0;
}

2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
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);

2593 2594 2595 2596 2597 2598
struct pn533 *pn533_register_device(u32 device_type,
				u32 protocols,
				enum pn533_protocol_type protocol_type,
				void *phy,
				struct pn533_phy_ops *phy_ops,
				struct pn533_frame_ops *fops,
2599 2600
				struct device *dev,
				struct device *parent)
2601
{
2602
	struct pn533 *priv;
2603 2604
	int rc = -ENOMEM;

2605 2606 2607
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);
2608

2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
	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)
2633
		goto error;
2634

2635
	timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
2636

2637 2638
	skb_queue_head_init(&priv->resp_q);
	skb_queue_head_init(&priv->fragment_skb);
2639

2640
	INIT_LIST_HEAD(&priv->cmd_queue);
2641

2642 2643
	priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
					   priv->ops->tx_header_len +
2644
					   PN533_CMD_DATAEXCH_HEAD_LEN,
2645 2646
					   priv->ops->tx_tail_len);
	if (!priv->nfc_dev) {
2647
		rc = -ENOMEM;
2648
		goto destroy_wq;
2649
	}
2650

2651
	nfc_set_parent_dev(priv->nfc_dev, parent);
2652
	nfc_set_drvdata(priv->nfc_dev, priv);
2653

2654
	rc = nfc_register_device(priv->nfc_dev);
2655 2656 2657
	if (rc)
		goto free_nfc_dev;

2658
	return priv;
2659 2660

free_nfc_dev:
2661
	nfc_free_device(priv->nfc_dev);
2662

2663
destroy_wq:
2664
	destroy_workqueue(priv->wq);
2665
error:
2666 2667
	kfree(priv);
	return ERR_PTR(rc);
2668
}
2669
EXPORT_SYMBOL_GPL(pn533_register_device);
2670

2671
void pn533_unregister_device(struct pn533 *priv)
2672
{
S
Samuel Ortiz 已提交
2673
	struct pn533_cmd *cmd, *n;
2674

2675 2676
	nfc_unregister_device(priv->nfc_dev);
	nfc_free_device(priv->nfc_dev);
2677

2678 2679
	flush_delayed_work(&priv->poll_work);
	destroy_workqueue(priv->wq);
2680

2681
	skb_queue_purge(&priv->resp_q);
2682

2683
	del_timer(&priv->listen_timer);
2684

2685
	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
S
Samuel Ortiz 已提交
2686 2687 2688 2689
		list_del(&cmd->queue);
		kfree(cmd);
	}

2690
	kfree(priv);
2691
}
2692
EXPORT_SYMBOL_GPL(pn533_unregister_device);
2693 2694


2695 2696 2697
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>");
2698
MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2699 2700
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");