pn533.c 59.2 KB
Newer Older
1
/*
2 3
 * Driver for NXP PN533 NFC Chip - core functions
 *
4
 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5
 * Copyright (C) 2012-2013 Tieto Poland
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26
 */

#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/nfc.h>
#include <linux/netdevice.h>
27
#include <net/nfc/nfc.h>
28
#include "pn533.h"
29

30
#define VERSION "0.3"
31

S
Samuel Ortiz 已提交
32 33
/* How much time we spend listening for initiators */
#define PN533_LISTEN_TIME 2
34 35
/* Delay between each poll frame (ms) */
#define PN533_POLL_INTERVAL 10
S
Samuel Ortiz 已提交
36

37 38 39 40 41 42 43 44 45 46 47
/* 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 */
48 49
#define PN533_CFGITEM_RF_FIELD    0x01
#define PN533_CFGITEM_TIMING      0x02
50
#define PN533_CFGITEM_MAX_RETRIES 0x05
51 52
#define PN533_CFGITEM_PASORI      0x82

S
Samuel Ortiz 已提交
53 54 55
#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
#define PN533_CFGITEM_RF_FIELD_ON        0x1
#define PN533_CFGITEM_RF_FIELD_OFF       0x0
56

S
Samuel Ortiz 已提交
57 58 59 60 61
#define PN533_CONFIG_TIMING_102 0xb
#define PN533_CONFIG_TIMING_204 0xc
#define PN533_CONFIG_TIMING_409 0xd
#define PN533_CONFIG_TIMING_819 0xe

62 63 64 65 66 67 68 69 70
#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 已提交
71 72 73 74 75 76
struct pn533_config_timing {
	u8 rfu;
	u8 atr_res_timeout;
	u8 dep_timeout;
} __packed;

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 103 104 105 106 107 108 109 110 111 112 113 114
/* 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;
};

115
static const struct pn533_poll_modulations poll_mod[] = {
116 117 118 119 120 121 122 123 124 125 126 127 128 129
	[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,
130
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
131
				.tsn = 0x03,
132 133 134 135 136 137 138 139 140 141 142
			},
		},
		.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,
143
				.rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
144
				.tsn = 0x03,
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			},
		 },
		.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 已提交
168 169 170
	[PN533_LISTEN_MOD] = {
		.len = 0,
	},
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
};

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

187 188 189 190 191 192 193 194 195 196 197 198
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;
199

200 201 202 203 204

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

205 206 207 208
#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
#define PN533_INIT_TARGET_RESP_DEP        0x4

209 210 211 212 213 214
/* 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;
}

215
/* The rule: value + checksum = 0 */
216
static inline u8 pn533_std_checksum(u8 value)
217 218 219 220 221
{
	return ~value + 1;
}

/* The rule: sum(data elements) + checksum = 0 */
222
static u8 pn533_std_data_checksum(u8 *data, int datalen)
223 224 225 226 227 228 229
{
	u8 sum = 0;
	int i;

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

230
	return pn533_std_checksum(sum);
231 232
}

233
static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
234
{
235
	struct pn533_std_frame *frame = _frame;
236

237
	frame->preamble = 0;
238 239
	frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
	PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
240
	PN533_FRAME_CMD(frame) = cmd_code;
241 242 243
	frame->datalen = 2;
}

244
static void pn533_std_tx_frame_finish(void *_frame)
245
{
246
	struct pn533_std_frame *frame = _frame;
247

248
	frame->datalen_checksum = pn533_std_checksum(frame->datalen);
249

250 251
	PN533_STD_FRAME_CHECKSUM(frame) =
		pn533_std_data_checksum(frame->data, frame->datalen);
252

253
	PN533_STD_FRAME_POSTAMBLE(frame) = 0;
254 255
}

256
static void pn533_std_tx_update_payload_len(void *_frame, int len)
257
{
258
	struct pn533_std_frame *frame = _frame;
259 260 261 262

	frame->datalen += len;
}

263
static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
264 265
{
	u8 checksum;
266
	struct pn533_std_frame *stdf = _frame;
267

268
	if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
269 270
		return false;

271 272
	if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
		/* Standard frame code */
273
		dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
274 275 276 277 278 279 280 281 282 283 284 285

		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;

286 287
		dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;

288 289 290 291 292 293 294 295 296 297
		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;
	}
298 299 300 301

	return true;
}

302
bool pn533_rx_frame_is_ack(void *_frame)
303
{
304 305
	struct pn533_std_frame *frame = _frame;

306
	if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
307 308 309 310 311 312 313
		return false;

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

	return true;
}
314
EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
315

316
static inline int pn533_std_rx_frame_size(void *frame)
317
{
318
	struct pn533_std_frame *f = frame;
319

320 321 322 323 324 325 326 327
	/* 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;
	}

328 329
	return sizeof(struct pn533_std_frame) + f->datalen +
	       PN533_STD_FRAME_TAIL_LEN;
330 331
}

332
static u8 pn533_std_get_cmd_code(void *frame)
333
{
334
	struct pn533_std_frame *f = frame;
335
	struct pn533_ext_frame *eif = frame;
336

337 338 339 340
	if (PN533_STD_IS_EXTENDED(f))
		return PN533_FRAME_CMD(eif);
	else
		return PN533_FRAME_CMD(f);
341 342
}

343 344 345 346 347 348 349 350
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);


351
static struct pn533_frame_ops pn533_std_frame_ops = {
352 353 354 355 356 357 358 359 360 361 362 363 364
	.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,
365 366 367 368
};

static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
				  struct sk_buff *skb)
369 370 371
{
	/* payload is already there, just update datalen */
	int payload_len = skb->len;
372
	struct pn533_frame_ops *ops = dev->ops;
373 374


375 376
	skb_push(skb, ops->tx_header_len);
	skb_put(skb, ops->tx_tail_len);
377

378 379 380
	ops->tx_frame_init(skb->data, cmd_code);
	ops->tx_update_payload_len(skb->data, payload_len);
	ops->tx_frame_finish(skb->data);
381 382
}

383
static int pn533_send_async_complete(struct pn533 *dev)
384
{
385
	struct pn533_cmd *cmd = dev->cmd;
386 387
	struct sk_buff *resp;
	int status, rc = 0;
388

389 390 391 392
	if (!cmd) {
		dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
		goto done;
	}
393

394
	dev_kfree_skb(cmd->req);
395

396 397
	status = cmd->status;
	resp = cmd->resp;
398

399
	if (status < 0) {
400 401
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
402
		dev_kfree_skb(resp);
403
		goto done;
404 405
	}

406 407 408 409 410 411 412 413
	/* 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);
	}
414

415
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
416

417
done:
418
	kfree(cmd);
419
	dev->cmd = NULL;
420 421 422 423
	return rc;
}

static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
424
			      struct sk_buff *req,
425 426 427 428 429 430
			      pn533_send_async_complete_t complete_cb,
			      void *complete_cb_context)
{
	struct pn533_cmd *cmd;
	int rc = 0;

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

433 434
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
435 436
		return -ENOMEM;

437
	cmd->code = cmd_code;
438 439 440
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
441

442
	pn533_build_cmd_frame(dev, cmd_code, req);
443 444 445 446

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
447
		dev->cmd = cmd;
448
		rc = dev->phy_ops->send_frame(dev, req);
449 450
		if (rc) {
			dev->cmd = NULL;
451
			goto error;
452
		}
453 454 455 456 457

		dev->cmd_pending = 1;
		goto unlock;
	}

458
	dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
459
		__func__, cmd_code);
460 461 462 463 464 465 466

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

	goto unlock;

error:
467
	kfree(cmd);
468 469 470
unlock:
	mutex_unlock(&dev->cmd_lock);
	return rc;
471 472 473 474 475 476 477 478 479
}

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;

480
	rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
481 482 483
				complete_cb_context);

	return rc;
484 485 486 487 488 489 490 491 492
}

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;

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

	return rc;
}

499 500 501
/*
 * pn533_send_cmd_direct_async
 *
502
 * The function sends a piority cmd directly to the chip omitting the cmd
503 504 505 506 507 508 509 510 511
 * 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)
{
512
	struct pn533_cmd *cmd;
513 514
	int rc;

515
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
516
	if (!cmd)
517 518
		return -ENOMEM;

519
	cmd->code = cmd_code;
520 521 522
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
523

524
	pn533_build_cmd_frame(dev, cmd_code, req);
525

526
	dev->cmd = cmd;
527
	rc = dev->phy_ops->send_frame(dev, req);
528 529
	if (rc < 0) {
		dev->cmd = NULL;
530
		kfree(cmd);
531
	}
532 533 534 535

	return rc;
}

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

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

562 563
	list_del(&cmd->queue);

S
Samuel Ortiz 已提交
564 565
	mutex_unlock(&dev->cmd_lock);

566
	dev->cmd = cmd;
567
	rc = dev->phy_ops->send_frame(dev, cmd->req);
568
	if (rc < 0) {
569
		dev->cmd = NULL;
570
		dev_kfree_skb(cmd->req);
571
		kfree(cmd);
572
		return;
573
	}
574

S
Samuel Ortiz 已提交
575 576
}

577
struct pn533_sync_cmd_response {
578
	struct sk_buff *resp;
579 580 581
	struct completion done;
};

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
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
601
 *     as it's been already freed at the beginning of RX path by
602 603 604 605 606 607 608
 *     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.
 *
609
 */
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
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;
}

630
static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
631 632 633
{
	struct sk_buff *skb;

634
	skb = alloc_skb(dev->ops->tx_header_len +
635
			size +
636
			dev->ops->tx_tail_len, GFP_KERNEL);
637 638

	if (skb)
639
		skb_reserve(skb, dev->ops->tx_header_len);
640 641 642 643

	return skb;
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
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;

676 677 678 679
	/*
	 * The length check of nfcid[] and ats[] are not being performed because
	 * the values are not being used
	 */
680 681 682 683 684 685

	/* 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 &&
686 687 688
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
689 690 691 692 693 694 695 696 697 698 699 700 701 702
		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;

703
	tgt_type_a = (struct pn533_target_type_a *)tgt_data;
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

	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 已提交
726 727
	nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
	memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
728 729 730 731 732 733 734

	return 0;
}

struct pn533_target_felica {
	u8 pol_res;
	u8 opcode;
735
	u8 nfcid2[NFC_NFCID2_MAXSIZE];
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	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;

761
	tgt_felica = (struct pn533_target_felica *)tgt_data;
762 763 764 765

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

766 767
	if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
	    (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
768 769 770 771
		nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
	else
		nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;

S
Samuel Ortiz 已提交
772 773 774
	memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
	nfc_tgt->sensf_res_len = 9;

775 776 777
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	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 &&
800 801 802
	     platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
	    (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
	     platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
803 804 805 806 807 808 809 810 811 812
		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;

813
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
814 815 816 817 818 819

	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);
820 821
	nfc_tgt->nfcid1_len = 4;
	memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
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 860 861 862 863 864 865 866 867 868 869 870 871

	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;

872
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
873 874 875 876

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

877
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
878 879 880 881

	return 0;
}

882
static void pn533_poll_reset_mod_list(struct pn533 *dev);
883 884
static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
			      int tgdata_len)
885 886 887 888
{
	struct nfc_target nfc_tgt;
	int rc;

889
	dev_dbg(dev->dev, "%s: modulation=%d\n",
890
		__func__, dev->poll_mod_curr);
891

892
	if (tg != 1)
893 894
		return -EPROTO;

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

897 898
	switch (dev->poll_mod_curr) {
	case PN533_POLL_MOD_106KBPS_A:
899
		rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
900 901 902
		break;
	case PN533_POLL_MOD_212KBPS_FELICA:
	case PN533_POLL_MOD_424KBPS_FELICA:
903
		rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
904 905
		break;
	case PN533_POLL_MOD_106KBPS_JEWEL:
906
		rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
907 908
		break;
	case PN533_POLL_MOD_847KBPS_B:
909
		rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
910 911
		break;
	default:
912
		nfc_err(dev->dev,
913
			"Unknown current poll modulation\n");
914 915 916 917 918 919 920
		return -EPROTO;
	}

	if (rc)
		return rc;

	if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
921
		dev_dbg(dev->dev,
922
			"The Tg found doesn't have the desired protocol\n");
923 924 925
		return -EAGAIN;
	}

926
	dev_dbg(dev->dev,
927 928
		"Target found - supported protocols: 0x%x\n",
		nfc_tgt.supported_protocols);
929 930 931

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

932
	pn533_poll_reset_mod_list(dev);
933 934 935 936 937
	nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);

	return 0;
}

S
Samuel Ortiz 已提交
938 939 940 941 942
static inline void pn533_poll_next_mod(struct pn533 *dev)
{
	dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
}

943 944 945 946 947 948 949 950
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] =
951
		(struct pn533_poll_modulations *)&poll_mod[mod_index];
952 953 954
	dev->poll_mod_count++;
}

S
Samuel Ortiz 已提交
955 956
static void pn533_poll_create_mod_list(struct pn533 *dev,
				       u32 im_protocols, u32 tm_protocols)
957 958 959
{
	pn533_poll_reset_mod_list(dev);

960 961 962
	if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
	    (im_protocols & NFC_PROTO_ISO14443_MASK) ||
	    (im_protocols & NFC_PROTO_NFC_DEP_MASK))
963 964
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);

965 966
	if (im_protocols & NFC_PROTO_FELICA_MASK ||
	    im_protocols & NFC_PROTO_NFC_DEP_MASK) {
967 968 969 970
		pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
		pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
	}

S
Samuel Ortiz 已提交
971
	if (im_protocols & NFC_PROTO_JEWEL_MASK)
972 973
		pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);

974
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
975 976
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

S
Samuel Ortiz 已提交
977 978
	if (tm_protocols)
		pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
979 980
}

981
static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
982
{
983 984
	u8 nbtg, tg, *tgdata;
	int rc, tgdata_len;
985

986
	/* Toggle the DEP polling */
987 988
	if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
		dev->poll_dep = 1;
989

990 991 992 993 994 995 996
	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);
997 998

		/* We must stop the poll after a valid target found */
999
		if (rc == 0)
S
Samuel Ortiz 已提交
1000
			return 0;
1001 1002
	}

S
Samuel Ortiz 已提交
1003
	return -EAGAIN;
1004 1005
}

1006
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1007
{
1008
	struct sk_buff *skb;
1009
	u8 *felica, *nfcid3;
1010

1011 1012 1013
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1014 1015 1016 1017
	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 */
1018

1019 1020 1021
	u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
			       0x0, 0x0, 0x0,
			       0x40}; /* SEL_RES for DEP */
1022

1023 1024 1025 1026
	unsigned int skb_len = 36 + /*
				     * mode (1), mifare (6),
				     * felica (18), nfcid3 (10), gb_len (1)
				     */
1027 1028
			       gbytes_len +
			       1;  /* len Tk*/
1029

1030
	skb = pn533_alloc_skb(dev, skb_len);
1031 1032
	if (!skb)
		return NULL;
1033 1034

	/* DEP support only */
1035
	skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1036 1037

	/* MIFARE params */
1038
	skb_put_data(skb, mifare_params, 6);
1039 1040

	/* Felica params */
1041
	felica = skb_put_data(skb, felica_params, 18);
1042
	get_random_bytes(felica + 2, 6);
1043 1044

	/* NFCID3 */
J
Johannes Berg 已提交
1045
	nfcid3 = skb_put_zero(skb, 10);
1046
	memcpy(nfcid3, felica, 8);
1047 1048

	/* General bytes */
1049
	skb_put_u8(skb, gbytes_len);
1050

1051
	skb_put_data(skb, gbytes, gbytes_len);
1052

1053
	/* Len Tk */
1054
	skb_put_u8(skb, 0);
1055

1056
	return skb;
1057 1058
}

1059 1060 1061
static void pn533_wq_tm_mi_recv(struct work_struct *work);
static struct sk_buff *pn533_build_response(struct pn533 *dev);

1062
static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1063
				      struct sk_buff *resp)
1064
{
1065 1066 1067
	struct sk_buff *skb;
	u8 status, ret, mi;
	int rc;
1068

1069
	dev_dbg(dev->dev, "%s\n", __func__);
1070

1071 1072
	if (IS_ERR(resp)) {
		skb_queue_purge(&dev->resp_q);
1073
		return PTR_ERR(resp);
1074
	}
1075

1076
	status = resp->data[0];
1077 1078 1079 1080

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

1081
	skb_pull(resp, sizeof(status));
1082

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
	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;
1099 1100
	}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	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;

1118
	dev_dbg(dev->dev, "%s\n", __func__);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

	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);
1132 1133
}

1134 1135 1136 1137 1138 1139 1140 1141
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;

1142
	dev_dbg(dev->dev, "%s\n", __func__);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

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

1164
	dev_err(dev->dev,
1165 1166 1167 1168 1169
		"Error %d when trying to perform set meta data_exchange", rc);

	dev_kfree_skb(skb);

error:
1170
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
1171 1172 1173
	queue_work(dev->wq, &dev->cmd_work);
}

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

1180
	dev_dbg(dev->dev, "%s\n", __func__);
1181

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

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

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

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

1200
	dev_dbg(dev->dev, "%s\n", __func__);
1201

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

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

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

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

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

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

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

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

	return 0;
1233 1234
}

S
Samuel Ortiz 已提交
1235
static void pn533_listen_mode_timer(unsigned long data)
1236
{
1237
	struct pn533 *dev = (struct pn533 *)data;
S
Samuel Ortiz 已提交
1238

1239
	dev_dbg(dev->dev, "Listen mode timeout\n");
S
Samuel Ortiz 已提交
1240 1241 1242 1243 1244

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

1245 1246
	queue_delayed_work(dev->wq, &dev->poll_work,
			   msecs_to_jiffies(PN533_POLL_INTERVAL));
S
Samuel Ortiz 已提交
1247 1248
}

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

1254
	dev_dbg(dev->dev, "%s\n", __func__);
1255 1256 1257 1258

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

1259
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1260 1261 1262 1263

		return rc;
	}

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

	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;

1277
	dev_dbg(dev->dev, "%s\n", __func__);
1278 1279 1280 1281 1282

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

1283 1284
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
	skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1285 1286 1287 1288 1289

	rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
				  pn533_rf_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1290
		nfc_err(dev->dev, "RF setting error %d\n", rc);
1291 1292 1293
	}
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
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;
	}

1316
	dev_dbg(dev->dev, "Creating new target");
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

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

1354
	dev_dbg(dev->dev, "%s", __func__);
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

	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;

1378 1379
	skb_put_u8(skb, 0x01);  /* Active */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1380 1381 1382 1383 1384

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

	/* Copy passive data */
1385
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1386 1387 1388
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1389
	skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1390 1391
	*next |= 2;

1392
	skb_put_data(skb, dev->gb, dev->gb_len);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	*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 已提交
1404
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1405
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1406 1407
{
	struct pn533_poll_modulations *cur_mod;
1408 1409
	int rc;

1410
	dev_dbg(dev->dev, "%s\n", __func__);
1411

1412 1413 1414
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

1415
		nfc_err(dev->dev, "%s  Poll complete error %d\n",
1416
			__func__, rc);
1417 1418 1419 1420

		if (rc == -ENOENT) {
			if (dev->poll_mod_count != 0)
				return rc;
1421
			goto stop_poll;
1422
		} else if (rc < 0) {
1423
			nfc_err(dev->dev,
1424
				"Error %d when running poll\n", rc);
1425 1426
			goto stop_poll;
		}
S
Samuel Ortiz 已提交
1427
	}
1428

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

1431
	if (cur_mod->len == 0) { /* Target mode */
S
Samuel Ortiz 已提交
1432
		del_timer(&dev->listen_timer);
1433 1434
		rc = pn533_init_target_complete(dev, resp);
		goto done;
S
Samuel Ortiz 已提交
1435 1436
	}

1437 1438 1439 1440
	/* Initiator mode */
	rc = pn533_start_poll_complete(dev, resp);
	if (!rc)
		goto done;
S
Samuel Ortiz 已提交
1441

1442
	if (!dev->poll_mod_count) {
1443
		dev_dbg(dev->dev, "Polling has been stopped\n");
1444 1445 1446
		goto done;
	}

1447
	pn533_poll_next_mod(dev);
1448 1449
	/* Not target found, turn radio off */
	queue_work(dev->wq, &dev->rf_work);
S
Samuel Ortiz 已提交
1450

1451 1452 1453
done:
	dev_kfree_skb(resp);
	return rc;
S
Samuel Ortiz 已提交
1454 1455

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

S
Samuel Ortiz 已提交
1458 1459
	pn533_poll_reset_mod_list(dev);
	dev->poll_protocols = 0;
1460
	return rc;
1461 1462
}

1463 1464
static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
					struct pn533_poll_modulations *mod)
1465
{
1466
	struct sk_buff *skb;
1467

1468
	skb = pn533_alloc_skb(dev, mod->len);
1469 1470
	if (!skb)
		return NULL;
1471

1472
	skb_put_data(skb, &mod->data, mod->len);
1473

1474
	return skb;
S
Samuel Ortiz 已提交
1475
}
1476

S
Samuel Ortiz 已提交
1477 1478
static int pn533_send_poll_frame(struct pn533 *dev)
{
1479 1480
	struct pn533_poll_modulations *mod;
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
1481
	int rc;
1482
	u8 cmd_code;
1483

1484
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1485

1486
	dev_dbg(dev->dev, "%s mod len %d\n",
1487
		__func__, mod->len);
1488

1489
	if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1490 1491 1492 1493
		dev->poll_dep = 0;
		return pn533_poll_dep(dev->nfc_dev);
	}

1494 1495
	if (mod->len == 0) {  /* Listen mode */
		cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1496
		skb = pn533_alloc_poll_tg_frame(dev);
1497 1498
	} else {  /* Polling mode */
		cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1499
		skb = pn533_alloc_poll_in_frame(dev, mod);
1500 1501 1502
	}

	if (!skb) {
1503
		nfc_err(dev->dev, "Failed to allocate skb\n");
1504 1505 1506 1507 1508 1509 1510
		return -ENOMEM;
	}

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

S
Samuel Ortiz 已提交
1514 1515 1516 1517 1518
	return rc;
}

static void pn533_wq_poll(struct work_struct *work)
{
1519
	struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
S
Samuel Ortiz 已提交
1520 1521 1522 1523 1524
	struct pn533_poll_modulations *cur_mod;
	int rc;

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

1525
	dev_dbg(dev->dev,
1526 1527
		"%s cancel_listen %d modulation len %d\n",
		__func__, dev->cancel_listen, cur_mod->len);
S
Samuel Ortiz 已提交
1528 1529 1530

	if (dev->cancel_listen == 1) {
		dev->cancel_listen = 0;
1531
		dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1532 1533
	}

S
Samuel Ortiz 已提交
1534 1535 1536
	rc = pn533_send_poll_frame(dev);
	if (rc)
		return;
1537

S
Samuel Ortiz 已提交
1538 1539
	if (cur_mod->len == 0 && dev->poll_mod_count > 1)
		mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1540 1541
}

1542 1543 1544 1545
static int pn533_start_poll(struct nfc_dev *nfc_dev,
			    u32 im_protocols, u32 tm_protocols)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1546
	struct pn533_poll_modulations *cur_mod;
1547
	u8 rand_mod;
1548
	int rc;
1549

1550
	dev_dbg(dev->dev,
1551 1552
		"%s: im protocols 0x%x tm protocols 0x%x\n",
		__func__, im_protocols, tm_protocols);
1553 1554

	if (dev->tgt_active_prot) {
1555
		nfc_err(dev->dev,
1556
			"Cannot poll with a target already activated\n");
1557 1558 1559
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1560
	if (dev->tgt_mode) {
1561
		nfc_err(dev->dev,
1562
			"Cannot poll while already being activated\n");
S
Samuel Ortiz 已提交
1563 1564 1565
		return -EBUSY;
	}

S
Samuel Ortiz 已提交
1566 1567 1568 1569 1570
	if (tm_protocols) {
		dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
		if (dev->gb == NULL)
			tm_protocols = 0;
	}
1571

S
Samuel Ortiz 已提交
1572 1573 1574
	pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
	dev->poll_protocols = im_protocols;
	dev->listen_protocols = tm_protocols;
1575

1576 1577 1578 1579 1580
	/* 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;

1581 1582 1583 1584 1585 1586 1587 1588 1589
	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;
1590 1591
}

1592 1593 1594 1595
static void pn533_stop_poll(struct nfc_dev *nfc_dev)
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

S
Samuel Ortiz 已提交
1596 1597
	del_timer(&dev->listen_timer);

1598
	if (!dev->poll_mod_count) {
1599
		dev_dbg(dev->dev,
1600
			"Polling operation was not running\n");
1601 1602 1603
		return;
	}

1604
	dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1605
	flush_delayed_work(&dev->poll_work);
1606
	pn533_poll_reset_mod_list(dev);
1607 1608 1609 1610
}

static int pn533_activate_target_nfcdep(struct pn533 *dev)
{
1611
	struct pn533_cmd_activate_response *rsp;
S
Samuel Ortiz 已提交
1612
	u16 gt_len;
1613
	int rc;
1614 1615
	struct sk_buff *skb;
	struct sk_buff *resp;
1616

1617
	dev_dbg(dev->dev, "%s\n", __func__);
1618

1619
	skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1620 1621
	if (!skb)
		return -ENOMEM;
1622

1623 1624
	skb_put_u8(skb, 1); /* TG */
	skb_put_u8(skb, 0); /* Next */
1625

1626 1627 1628
	resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1629

1630
	rsp = (struct pn533_cmd_activate_response *)resp->data;
1631
	rc = rsp->status & PN533_CMD_RET_MASK;
1632
	if (rc != PN533_CMD_RET_SUCCESS) {
1633
		nfc_err(dev->dev,
1634
			"Target activation failed (error 0x%x)\n", rc);
1635
		dev_kfree_skb(resp);
1636
		return -EIO;
1637
	}
1638

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

1643
	dev_kfree_skb(resp);
S
Samuel Ortiz 已提交
1644
	return rc;
1645 1646
}

1647 1648
static int pn533_activate_target(struct nfc_dev *nfc_dev,
				 struct nfc_target *target, u32 protocol)
1649 1650 1651 1652
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
	int rc;

1653
	dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1654 1655

	if (dev->poll_mod_count) {
1656
		nfc_err(dev->dev,
1657
			"Cannot activate while polling\n");
1658 1659 1660 1661
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1662
		nfc_err(dev->dev,
1663
			"There is already an active target\n");
1664 1665 1666 1667
		return -EBUSY;
	}

	if (!dev->tgt_available_prots) {
1668
		nfc_err(dev->dev,
1669
			"There is no available target to activate\n");
1670 1671 1672 1673
		return -EINVAL;
	}

	if (!(dev->tgt_available_prots & (1 << protocol))) {
1674
		nfc_err(dev->dev,
1675 1676
			"Target doesn't support requested proto %u\n",
			protocol);
1677 1678 1679 1680 1681 1682
		return -EINVAL;
	}

	if (protocol == NFC_PROTO_NFC_DEP) {
		rc = pn533_activate_target_nfcdep(dev);
		if (rc) {
1683
			nfc_err(dev->dev,
1684
				"Activating target with DEP failed %d\n", rc);
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
			return rc;
		}
	}

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

	return 0;
}

1695 1696 1697 1698 1699
static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
			     struct sk_buff *resp)
{
	int rc = 0;

1700
	dev_dbg(dev->dev, "%s\n", __func__);
1701 1702 1703 1704

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

1705
		nfc_err(dev->dev, "Target release error %d\n", rc);
1706 1707 1708 1709 1710 1711

		return rc;
	}

	rc = resp->data[0] & PN533_CMD_RET_MASK;
	if (rc != PN533_CMD_RET_SUCCESS)
1712
		nfc_err(dev->dev,
1713 1714 1715 1716 1717 1718
			"Error 0x%x when releasing the target\n", rc);

	dev_kfree_skb(resp);
	return rc;
}

1719
static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1720
				    struct nfc_target *target, u8 mode)
1721 1722
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1723
	struct sk_buff *skb;
1724 1725
	int rc;

1726
	dev_dbg(dev->dev, "%s\n", __func__);
1727 1728

	if (!dev->tgt_active_prot) {
1729
		nfc_err(dev->dev, "There is no active target\n");
1730 1731 1732 1733
		return;
	}

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

1736
	skb = pn533_alloc_skb(dev, sizeof(u8));
1737 1738
	if (!skb)
		return;
1739

1740
	skb_put_u8(skb, 1); /* TG*/
1741

1742 1743 1744 1745
	rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
				  pn533_deactivate_target_complete, NULL);
	if (rc < 0) {
		dev_kfree_skb(skb);
1746
		nfc_err(dev->dev, "Target release error %d\n", rc);
1747
	}
1748 1749
}

1750 1751

static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1752
					 struct sk_buff *resp)
1753
{
1754
	struct pn533_cmd_jump_dep_response *rsp;
1755 1756
	u8 target_gt_len;
	int rc;
1757
	u8 active = *(u8 *)arg;
1758 1759

	kfree(arg);
1760

1761 1762
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1763 1764 1765

	if (dev->tgt_available_prots &&
	    !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1766
		nfc_err(dev->dev,
1767
			"The target does not support DEP\n");
1768 1769
		rc =  -EINVAL;
		goto error;
1770 1771
	}

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

	rc = rsp->status & PN533_CMD_RET_MASK;
1775
	if (rc != PN533_CMD_RET_SUCCESS) {
1776
		nfc_err(dev->dev,
1777
			"Bringing DEP link up failed (error 0x%x)\n", rc);
1778
		goto error;
1779 1780 1781
	}

	if (!dev->tgt_available_prots) {
1782 1783
		struct nfc_target nfc_target;

1784
		dev_dbg(dev->dev, "Creating new target\n");
1785 1786

		nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1787
		nfc_target.nfcid1_len = 10;
1788
		memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1789 1790
		rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
		if (rc)
1791
			goto error;
1792 1793 1794 1795 1796 1797 1798

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

	/* ATR_RES general bytes are located at offset 17 */
1799
	target_gt_len = resp->len - 17;
1800
	rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1801
					  rsp->gt, target_gt_len);
1802 1803
	if (rc == 0)
		rc = nfc_dep_link_is_up(dev->nfc_dev,
1804 1805
					dev->nfc_dev->targets[0].idx,
					!active, NFC_RF_INITIATOR);
1806

1807 1808 1809
error:
	dev_kfree_skb(resp);
	return rc;
1810 1811
}

1812
static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1813
static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1814
			     u8 comm_mode, u8 *gb, size_t gb_len)
1815 1816
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1817
	struct sk_buff *skb;
1818 1819
	int rc, skb_len;
	u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1820
	u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1821

1822
	dev_dbg(dev->dev, "%s\n", __func__);
1823 1824

	if (dev->poll_mod_count) {
1825
		nfc_err(dev->dev,
1826
			"Cannot bring the DEP link up while polling\n");
1827 1828 1829 1830
		return -EBUSY;
	}

	if (dev->tgt_active_prot) {
1831
		nfc_err(dev->dev,
1832
			"There is already an active target\n");
1833 1834 1835
		return -EBUSY;
	}

1836
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1837
	skb_len += PASSIVE_DATA_LEN;
1838

1839 1840 1841 1842 1843 1844 1845
	/* NFCID3 */
	skb_len += NFC_NFCID3_MAXSIZE;
	if (target && !target->nfcid2_len) {
		nfcid3[0] = 0x1;
		nfcid3[1] = 0xfe;
		get_random_bytes(nfcid3 + 2, 6);
	}
1846

1847
	skb = pn533_alloc_skb(dev, skb_len);
1848
	if (!skb)
1849 1850
		return -ENOMEM;

1851 1852
	skb_put_u8(skb, !comm_mode);  /* ActPass */
	skb_put_u8(skb, 0x02);  /* 424 kbps */
1853 1854 1855

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

1857
	/* Copy passive data */
1858
	skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1859
	*next |= 1;
1860

1861 1862
	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	if (target && target->nfcid2_len)
1863 1864
		memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
		       target->nfcid2_len);
1865
	else
1866
		skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1867
	*next |= 2;
1868

1869
	if (gb != NULL && gb_len > 0) {
1870
		skb_put_data(skb, gb, gb_len);
1871
		*next |= 4; /* We have some Gi */
1872
	} else {
1873
		*next = 0;
1874 1875
	}

1876 1877 1878 1879 1880
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
1881

1882
	*arg = !comm_mode;
1883

1884 1885
	pn533_rf_field(dev->nfc_dev, 0);

1886 1887 1888 1889 1890 1891 1892
	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);
	}
1893 1894 1895 1896 1897 1898

	return rc;
}

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

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

S
Samuel Ortiz 已提交
1903 1904
	pn533_poll_reset_mod_list(dev);

1905
	if (dev->tgt_mode || dev->tgt_active_prot)
1906
		dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
1907 1908 1909 1910 1911

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

	skb_queue_purge(&dev->resp_q);
1912 1913 1914 1915

	return 0;
}

1916 1917 1918 1919 1920
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

S
Samuel Ortiz 已提交
1921 1922 1923 1924 1925
static struct sk_buff *pn533_build_response(struct pn533 *dev)
{
	struct sk_buff *skb, *tmp, *t;
	unsigned int skb_len = 0, tmp_len = 0;

1926
	dev_dbg(dev->dev, "%s\n", __func__);
S
Samuel Ortiz 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938

	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;

1939
	dev_dbg(dev->dev, "%s total length %d\n",
1940
		__func__, skb_len);
S
Samuel Ortiz 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958

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

1959
static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1960
					struct sk_buff *resp)
1961 1962
{
	struct pn533_data_exchange_arg *arg = _arg;
1963 1964 1965
	struct sk_buff *skb;
	int rc = 0;
	u8 status, ret, mi;
1966

1967
	dev_dbg(dev->dev, "%s\n", __func__);
1968

1969 1970 1971
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
1972 1973
	}

1974 1975 1976 1977 1978
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
1979

1980
	if (ret != PN533_CMD_RET_SUCCESS) {
1981
		nfc_err(dev->dev,
1982
			"Exchanging data failed (error 0x%x)\n", ret);
1983
		rc = -EIO;
1984 1985 1986
		goto error;
	}

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

1989 1990
	if (mi) {
		dev->cmd_complete_mi_arg = arg;
1991 1992 1993 1994 1995 1996 1997 1998 1999
		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 已提交
2000
		return -EINPROGRESS;
2001 2002
	}

S
Samuel Ortiz 已提交
2003
	skb = pn533_build_response(dev);
J
Julia Lawall 已提交
2004 2005
	if (!skb) {
		rc = -ENOMEM;
S
Samuel Ortiz 已提交
2006
		goto error;
J
Julia Lawall 已提交
2007
	}
2008

S
Samuel Ortiz 已提交
2009
	arg->cb(arg->cb_context, skb, 0);
2010 2011 2012 2013
	kfree(arg);
	return 0;

error:
2014 2015
	dev_kfree_skb(resp);
_error:
S
Samuel Ortiz 已提交
2016
	skb_queue_purge(&dev->resp_q);
2017
	arg->cb(arg->cb_context, NULL, rc);
2018
	kfree(arg);
2019
	return rc;
2020 2021
}

2022 2023 2024 2025 2026 2027
/*
 * 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)
{
2028 2029 2030
	if (!dev->cmd)
		goto sched_wq;

2031 2032
	dev->cmd->status = status;

2033 2034 2035 2036 2037
	if (status != 0) {
		dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
		goto sched_wq;
	}

2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
	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);

2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
/* 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;
		}

2087 2088 2089 2090 2091 2092
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2093 2094
				*(u8 *)skb_push(frag, sizeof(u8)) =
						(PN533_CMD_MI_MASK | 1);
2095
			else
2096
				*(u8 *)skb_push(frag, sizeof(u8)) =  1; /* TG */
2097
		}
2098

2099
		skb_put_data(frag, skb->data, frag_size);
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

		/* 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 已提交
2114 2115 2116
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)
2117 2118
{
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2119
	struct pn533_data_exchange_arg *arg = NULL;
2120 2121
	int rc;

2122
	dev_dbg(dev->dev, "%s\n", __func__);
2123 2124

	if (!dev->tgt_active_prot) {
2125
		nfc_err(dev->dev,
2126
			"Can't exchange data if there is no active target\n");
2127 2128 2129 2130
		rc = -EINVAL;
		goto error;
	}

2131
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2132 2133
	if (!arg) {
		rc = -ENOMEM;
2134
		goto error;
2135 2136 2137 2138 2139
	}

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

2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	switch (dev->device_type) {
	case PN533_DEVICE_PASORI:
		if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
			rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
						   skb,
						   pn533_data_exchange_complete,
						   arg);

			break;
		}
	default:
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
		/* 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 {
2163
			*(u8 *)skb_push(skb, sizeof(u8)) =  1; /* TG */
2164
		}
2165 2166 2167 2168 2169 2170

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

		break;
2171 2172
	}

2173 2174 2175
	if (rc < 0) /* rc from send_async */
		goto error;

2176 2177 2178
	return 0;

error:
2179 2180
	kfree(arg);
	dev_kfree_skb(skb);
2181 2182 2183
	return rc;
}

2184
static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2185
				  struct sk_buff *resp)
2186
{
2187
	u8 status;
2188

2189
	dev_dbg(dev->dev, "%s\n", __func__);
2190

2191 2192
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2193

2194
	status = resp->data[0];
2195

2196 2197 2198 2199 2200
	/* Prepare for the next round */
	if (skb_queue_len(&dev->fragment_skb) > 0) {
		queue_work(dev->wq, &dev->mi_tm_tx_work);
		return -EINPROGRESS;
	}
2201
	dev_kfree_skb(resp);
2202

2203
	if (status != 0) {
2204 2205
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2206 2207
		dev->tgt_mode = 0;

2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
		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;

2221
	dev_dbg(dev->dev, "%s\n", __func__);
2222

2223
	/* let's split in multiple chunks if size's too big */
2224
	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
		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);
2242 2243
	}

2244 2245
error:
	if (rc < 0) {
2246
		dev_kfree_skb(skb);
2247 2248
		skb_queue_purge(&dev->fragment_skb);
	}
2249 2250 2251 2252

	return rc;
}

S
Samuel Ortiz 已提交
2253 2254
static void pn533_wq_mi_recv(struct work_struct *work)
{
2255
	struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2256
	struct sk_buff *skb;
S
Samuel Ortiz 已提交
2257 2258
	int rc;

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

2261
	skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2262 2263
	if (!skb)
		goto error;
S
Samuel Ortiz 已提交
2264

2265 2266 2267 2268 2269 2270 2271 2272
	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 已提交
2273

2274 2275 2276
			break;
		}
	default:
2277
		skb_put_u8(skb, 1); /*TG*/
S
Samuel Ortiz 已提交
2278

2279 2280 2281 2282 2283
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_mi_arg);
2284

2285
		break;
S
Samuel Ortiz 已提交
2286 2287
	}

2288
	if (rc == 0) /* success */
S
Samuel Ortiz 已提交
2289 2290
		return;

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

2294
	dev_kfree_skb(skb);
2295
	kfree(dev->cmd_complete_mi_arg);
S
Samuel Ortiz 已提交
2296

2297
error:
2298
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
S
Samuel Ortiz 已提交
2299
	queue_work(dev->wq, &dev->cmd_work);
S
Samuel Ortiz 已提交
2300 2301
}

2302 2303 2304 2305 2306 2307
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;

2308
	dev_dbg(dev->dev, "%s\n", __func__);
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334

	/* 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? */
2335 2336
		rc = pn533_send_cmd_direct_async(dev,
						 PN533_CMD_IN_DATA_EXCHANGE,
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
						 skb,
						 pn533_data_exchange_complete,
						 dev->cmd_complete_dep_arg);

		break;
	}

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

2347
	nfc_err(dev->dev,
2348
		"Error %d when trying to perform data_exchange\n", rc);
2349 2350 2351 2352 2353

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

error:
2354
	dev->phy_ops->send_ack(dev, GFP_KERNEL);
2355 2356 2357
	queue_work(dev->wq, &dev->cmd_work);
}

2358 2359 2360
static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
								u8 cfgdata_len)
{
2361 2362 2363
	struct sk_buff *skb;
	struct sk_buff *resp;
	int skb_len;
2364

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

2367
	skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2368

2369
	skb = pn533_alloc_skb(dev, skb_len);
2370 2371
	if (!skb)
		return -ENOMEM;
2372

2373
	skb_put_u8(skb, cfgitem);
2374
	skb_put_data(skb, cfgdata, cfgdata_len);
2375

2376 2377 2378
	resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2379

2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
	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;

2390
	skb = pn533_alloc_skb(dev, 0);
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
	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;
2405 2406
}

2407
static int pn533_pasori_fw_reset(struct pn533 *dev)
2408
{
2409 2410
	struct sk_buff *skb;
	struct sk_buff *resp;
2411

2412
	dev_dbg(dev->dev, "%s\n", __func__);
2413

2414
	skb = pn533_alloc_skb(dev, sizeof(u8));
2415 2416
	if (!skb)
		return -ENOMEM;
2417

2418
	skb_put_u8(skb, 0x1);
2419

2420 2421 2422
	resp = pn533_send_cmd_sync(dev, 0x18, skb);
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2423

2424
	dev_kfree_skb(resp);
2425

2426
	return 0;
2427 2428
}

2429 2430 2431 2432 2433 2434
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 已提交
2435 2436
	rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;

2437 2438 2439
	rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
				     (u8 *)&rf_field, 1);
	if (rc) {
2440
		nfc_err(dev->dev, "Error on setting RF field\n");
2441 2442 2443 2444 2445 2446
		return rc;
	}

	return rc;
}

2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
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;

2457
	skb_put_u8(skb, 0x01);
2458 2459 2460 2461 2462 2463 2464 2465 2466

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

2467
static int pn533_dev_up(struct nfc_dev *nfc_dev)
2468
{
2469 2470 2471 2472 2473 2474 2475 2476 2477
	struct pn533 *dev = nfc_get_drvdata(nfc_dev);

	if (dev->device_type == PN533_DEVICE_PN532) {
		int rc = pn532_sam_configuration(nfc_dev);

		if (rc)
			return rc;
	}

2478 2479 2480
	return pn533_rf_field(nfc_dev, 1);
}

2481
static int pn533_dev_down(struct nfc_dev *nfc_dev)
2482 2483 2484 2485
{
	return pn533_rf_field(nfc_dev, 0);
}

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 2636
	setup_timer(&priv->listen_timer, pn533_listen_mode_timer,
		    (unsigned long)priv);
2637

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

2641
	INIT_LIST_HEAD(&priv->cmd_queue);
2642

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

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

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

2659
	return priv;
2660 2661

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

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

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

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

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

2682
	skb_queue_purge(&priv->resp_q);
2683

2684
	del_timer(&priv->listen_timer);
2685

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

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


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