pn533.c 59.3 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
	int status = cmd->status;
387

388 389
	struct sk_buff *req = cmd->req;
	struct sk_buff *resp = cmd->resp;
390 391 392 393 394

	int rc;

	dev_kfree_skb(req);

395
	if (status < 0) {
396 397
		rc = cmd->complete_cb(dev, cmd->complete_cb_context,
				      ERR_PTR(status));
398
		dev_kfree_skb(resp);
399
		goto done;
400 401
	}

402 403
	skb_pull(resp, dev->ops->rx_header_len);
	skb_trim(resp, resp->len - dev->ops->rx_tail_len);
404

405
	rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
406

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

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

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

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

427
	cmd->code = cmd_code;
428 429 430
	cmd->req = req;
	cmd->complete_cb = complete_cb;
	cmd->complete_cb_context = complete_cb_context;
431

432
	pn533_build_cmd_frame(dev, cmd_code, req);
433 434 435 436

	mutex_lock(&dev->cmd_lock);

	if (!dev->cmd_pending) {
437
		rc = dev->phy_ops->send_frame(dev, req);
438 439 440 441
		if (rc)
			goto error;

		dev->cmd_pending = 1;
442
		dev->cmd = cmd;
443 444 445
		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 490 491 492 493 494 495 496 497 498 499
/*
 * pn533_send_cmd_direct_async
 *
 * The function sends a piority cmd directly to the chip omiting the cmd
 * queue. It's intended to be used by chaining mechanism of received responses
 * where the host has to request every single chunk of data before scheduling
 * next cmd from the queue.
 */
static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
				       struct sk_buff *req,
				       pn533_send_async_complete_t complete_cb,
				       void *complete_cb_context)
{
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 515
	rc = dev->phy_ops->send_frame(dev, req);
	if (rc < 0)
516
		kfree(cmd);
517
	else
518
		dev->cmd = cmd;
519 520 521 522

	return rc;
}

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

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

549 550
	list_del(&cmd->queue);

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

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

	dev->cmd = cmd;
S
Samuel Ortiz 已提交
561 562
}

563
struct pn533_sync_cmd_response {
564
	struct sk_buff *resp;
565 566 567
	struct completion done;
};

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

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

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

	if (skb)
625
		skb_reserve(skb, dev->ops->tx_header_len);
626 627 628 629

	return skb;
}

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

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

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

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

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

	return 0;
}

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

747
	tgt_felica = (struct pn533_target_felica *)tgt_data;
748 749 750 751

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

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

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

761 762 763
	memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
	nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;

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

799
	tgt_jewel = (struct pn533_target_jewel *)tgt_data;
800 801 802 803 804 805

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

	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;

858
	tgt_type_b = (struct pn533_target_type_b *)tgt_data;
859 860 861 862

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

863
	nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
864 865 866 867

	return 0;
}

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

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

878
	if (tg != 1)
879 880
		return -EPROTO;

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

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

	if (rc)
		return rc;

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

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

	dev->tgt_available_prots = nfc_tgt.supported_protocols;

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

	return 0;
}

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

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

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

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

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

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

960
	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
961 962
		pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);

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

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

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

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

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

S
Samuel Ortiz 已提交
989
	return -EAGAIN;
990 991
}

992
static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
993
{
994 995 996
	struct sk_buff *skb;
	u8 *felica, *nfcid3, *gb;

997 998 999
	u8 *gbytes = dev->gb;
	size_t gbytes_len = dev->gb_len;

1000 1001 1002 1003
	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 */
1004

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

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

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

	/* DEP support only */
1021
	*skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1022 1023 1024

	/* MIFARE params */
	memcpy(skb_put(skb, 6), mifare_params, 6);
1025 1026

	/* Felica params */
1027 1028 1029
	felica = skb_put(skb, 18);
	memcpy(felica, felica_params, 18);
	get_random_bytes(felica + 2, 6);
1030 1031

	/* NFCID3 */
1032 1033 1034
	nfcid3 = skb_put(skb, 10);
	memset(nfcid3, 0, 10);
	memcpy(nfcid3, felica, 8);
1035 1036

	/* General bytes */
1037
	*skb_put(skb, 1) = gbytes_len;
1038

1039 1040
	gb = skb_put(skb, gbytes_len);
	memcpy(gb, gbytes, gbytes_len);
1041

1042 1043
	/* Len Tk */
	*skb_put(skb, 1) = 0;
1044

1045
	return skb;
1046 1047
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	dev_kfree_skb(skb);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
1222 1223
}

S
Samuel Ortiz 已提交
1224
static void pn533_listen_mode_timer(unsigned long data)
1225
{
1226
	struct pn533 *dev = (struct pn533 *)data;
S
Samuel Ortiz 已提交
1227

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

	dev->cancel_listen = 1;

	pn533_poll_next_mod(dev);

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

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

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

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

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

		return rc;
	}

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

	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;

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

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

	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
S
Samuel Ortiz 已提交
1273
	*skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1274 1275 1276 1277 1278

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

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

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

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

1343
	dev_dbg(dev->dev, "%s", __func__);
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

	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;

	*skb_put(skb, 1) = 0x01;  /* Active */
	*skb_put(skb, 1) = 0x02;  /* 424 kbps */

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

	/* Copy passive data */
	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
	*next |= 1;

	/* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
	memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
	       NFC_NFCID3_MAXSIZE);
	*next |= 2;

	memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
	*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 已提交
1394
static int pn533_poll_complete(struct pn533 *dev, void *arg,
1395
			       struct sk_buff *resp)
S
Samuel Ortiz 已提交
1396 1397
{
	struct pn533_poll_modulations *cur_mod;
1398 1399
	int rc;

1400
	dev_dbg(dev->dev, "%s\n", __func__);
1401

1402 1403 1404
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);

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

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

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

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

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

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

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

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

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

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

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

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

1462
	memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1463

1464
	return skb;
S
Samuel Ortiz 已提交
1465
}
1466

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

1474
	mod = dev->poll_mod_active[dev->poll_mod_curr];
1475

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

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

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

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

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

S
Samuel Ortiz 已提交
1504 1505 1506 1507 1508
	return rc;
}

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

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

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

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

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

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

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

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

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

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

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

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

1566 1567 1568 1569 1570
	/* 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;

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

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

S
Samuel Ortiz 已提交
1586 1587
	del_timer(&dev->listen_timer);

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

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

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

1607
	dev_dbg(dev->dev, "%s\n", __func__);
1608

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

1613 1614
	*skb_put(skb, sizeof(u8)) = 1; /* TG */
	*skb_put(skb, sizeof(u8)) = 0; /* Next */
1615

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
}

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

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

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

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

		return rc;
	}

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

	dev_kfree_skb(resp);
	return rc;
}

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

1716
	dev_dbg(dev->dev, "%s\n", __func__);
1717 1718

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

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

1726
	skb = pn533_alloc_skb(dev, sizeof(u8));
1727 1728
	if (!skb)
		return;
1729

1730
	*skb_put(skb, 1) = 1; /* TG*/
1731

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

1740 1741

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

	kfree(arg);
1750

1751 1752
	if (IS_ERR(resp))
		return PTR_ERR(resp);
1753 1754 1755

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

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

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

	if (!dev->tgt_available_prots) {
1772 1773
		struct nfc_target nfc_target;

1774
		dev_dbg(dev->dev, "Creating new target\n");
1775 1776

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

		dev->tgt_available_prots = 0;
	}

	dev->tgt_active_prot = NFC_PROTO_NFC_DEP;

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

1797 1798 1799
error:
	dev_kfree_skb(resp);
	return rc;
1800 1801
}

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

1812
	dev_dbg(dev->dev, "%s\n", __func__);
1813 1814

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

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

1826
	skb_len = 3 + gb_len; /* ActPass + BR + Next */
1827
	skb_len += PASSIVE_DATA_LEN;
1828

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

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

1841
	*skb_put(skb, 1) = !comm_mode;  /* ActPass */
1842
	*skb_put(skb, 1) = 0x02;  /* 424 kbps */
1843 1844 1845

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

1847 1848 1849
	/* Copy passive data */
	memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
	*next |= 1;
1850

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

1860
	if (gb != NULL && gb_len > 0) {
1861 1862
		memcpy(skb_put(skb, gb_len), gb, gb_len);
		*next |= 4; /* We have some Gi */
1863
	} else {
1864
		*next = 0;
1865 1866
	}

1867 1868 1869 1870 1871
	arg = kmalloc(sizeof(*arg), GFP_KERNEL);
	if (!arg) {
		dev_kfree_skb(skb);
		return -ENOMEM;
	}
1872

1873
	*arg = !comm_mode;
1874

1875 1876
	pn533_rf_field(dev->nfc_dev, 0);

1877 1878 1879 1880 1881 1882 1883
	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);
	}
1884 1885 1886 1887 1888 1889

	return rc;
}

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

1892
	dev_dbg(dev->dev, "%s\n", __func__);
1893

S
Samuel Ortiz 已提交
1894 1895
	pn533_poll_reset_mod_list(dev);

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

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

	skb_queue_purge(&dev->resp_q);
1903 1904 1905 1906

	return 0;
}

1907 1908 1909 1910 1911
struct pn533_data_exchange_arg {
	data_exchange_cb_t cb;
	void *cb_context;
};

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

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

	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;

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

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

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

1958
	dev_dbg(dev->dev, "%s\n", __func__);
1959

1960 1961 1962
	if (IS_ERR(resp)) {
		rc = PTR_ERR(resp);
		goto _error;
1963 1964
	}

1965 1966 1967 1968 1969
	status = resp->data[0];
	ret = status & PN533_CMD_RET_MASK;
	mi = status & PN533_CMD_MI_MASK;

	skb_pull(resp, sizeof(status));
1970

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

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

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

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

S
Samuel Ortiz 已提交
2000
	arg->cb(arg->cb_context, skb, 0);
2001 2002 2003 2004
	kfree(arg);
	return 0;

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

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

2022 2023
	dev->cmd->status = status;

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

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 2055 2056 2057
	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);

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

2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
		if (!dev->tgt_mode) {
			/* Reserve the TG/MI byte */
			skb_reserve(frag, 1);

			/* MI + TG */
			if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
				*skb_push(frag, sizeof(u8)) =
							(PN533_CMD_MI_MASK | 1);
			else
				*skb_push(frag, sizeof(u8)) =  1; /* TG */
		}
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104

		memcpy(skb_put(frag, frag_size), skb->data, frag_size);

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

2113
	dev_dbg(dev->dev, "%s\n", __func__);
2114 2115

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

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

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

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

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

		break;
2162 2163
	}

2164 2165 2166
	if (rc < 0) /* rc from send_async */
		goto error;

2167 2168 2169
	return 0;

error:
2170 2171
	kfree(arg);
	dev_kfree_skb(skb);
2172 2173 2174
	return rc;
}

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

2180
	dev_dbg(dev->dev, "%s\n", __func__);
2181

2182 2183
	if (IS_ERR(resp))
		return PTR_ERR(resp);
2184

2185
	status = resp->data[0];
2186

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

2194
	if (status != 0) {
2195 2196
		nfc_tm_deactivated(dev->nfc_dev);

S
Samuel Ortiz 已提交
2197 2198
		dev->tgt_mode = 0;

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

2212
	dev_dbg(dev->dev, "%s\n", __func__);
2213

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

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

	return rc;
}

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

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

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

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

2265 2266 2267 2268
			break;
		}
	default:
		*skb_put(skb, sizeof(u8)) =  1; /*TG*/
S
Samuel Ortiz 已提交
2269

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

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

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

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

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

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

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

2299
	dev_dbg(dev->dev, "%s\n", __func__);
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 2325

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

		break;
	}

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

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

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

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

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

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

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

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

2364 2365
	*skb_put(skb, sizeof(cfgitem)) = cfgitem;
	memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2366

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

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

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

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

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

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

2409
	*skb_put(skb, sizeof(u8)) = 0x1;
2410

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

2415
	dev_kfree_skb(resp);
2416

2417
	return 0;
2418 2419
}

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

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

	return rc;
}

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
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;

	*skb_put(skb, 1) = 0x01;

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

2458
static int pn533_dev_up(struct nfc_dev *nfc_dev)
2459
{
2460 2461 2462 2463 2464 2465 2466 2467 2468
	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;
	}

2469 2470 2471
	return pn533_rf_field(nfc_dev, 1);
}

2472
static int pn533_dev_down(struct nfc_dev *nfc_dev)
2473 2474 2475 2476
{
	return pn533_rf_field(nfc_dev, 0);
}

2477
static struct nfc_ops pn533_nfc_ops = {
2478 2479
	.dev_up = pn533_dev_up,
	.dev_down = pn533_dev_down,
2480 2481
	.dep_link_up = pn533_dep_link_up,
	.dep_link_down = pn533_dep_link_down,
2482 2483 2484 2485
	.start_poll = pn533_start_poll,
	.stop_poll = pn533_stop_poll,
	.activate_target = pn533_activate_target,
	.deactivate_target = pn533_deactivate_target,
S
Samuel Ortiz 已提交
2486
	.im_transceive = pn533_transceive,
2487
	.tm_send = pn533_tm_send,
2488 2489
};

2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
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:
2500
	case PN533_DEVICE_ACR122U:
2501
	case PN533_DEVICE_PN532:
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
		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:
2514
		nfc_err(dev->dev, "Unknown device type %d\n",
2515
			dev->device_type);
2516 2517 2518 2519 2520 2521
		return -EINVAL;
	}

	rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
				     (u8 *)&max_retries, sizeof(max_retries));
	if (rc) {
2522
		nfc_err(dev->dev,
2523
			"Error on setting MAX_RETRIES config\n");
2524 2525 2526 2527 2528 2529 2530
		return rc;
	}


	rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
				     (u8 *)&timing, sizeof(timing));
	if (rc) {
2531
		nfc_err(dev->dev, "Error on setting RF timings\n");
2532 2533 2534 2535 2536
		return rc;
	}

	switch (dev->device_type) {
	case PN533_DEVICE_STD:
2537
	case PN533_DEVICE_PN532:
2538 2539 2540
		break;

	case PN533_DEVICE_PASORI:
2541
		pn533_pasori_fw_reset(dev);
2542 2543 2544 2545

		rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
					     pasori_cfg, 3);
		if (rc) {
2546
			nfc_err(dev->dev,
2547
				"Error while settings PASORI config\n");
2548 2549 2550
			return rc;
		}

2551
		pn533_pasori_fw_reset(dev);
2552 2553 2554 2555 2556 2557 2558

		break;
	}

	return 0;
}

2559 2560 2561 2562 2563 2564
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,
2565 2566
				struct device *dev,
				struct device *parent)
2567
{
2568
	struct pn533_fw_version fw_ver;
2569
	struct pn533 *priv;
2570 2571
	int rc = -ENOMEM;

2572 2573 2574
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);
2575

2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599
	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)
2600
		goto error;
2601

2602 2603 2604
	init_timer(&priv->listen_timer);
	priv->listen_timer.data = (unsigned long) priv;
	priv->listen_timer.function = pn533_listen_mode_timer;
2605

2606 2607
	skb_queue_head_init(&priv->resp_q);
	skb_queue_head_init(&priv->fragment_skb);
2608

2609
	INIT_LIST_HEAD(&priv->cmd_queue);
2610

2611
	memset(&fw_ver, 0, sizeof(fw_ver));
2612
	rc = pn533_get_firmware_version(priv, &fw_ver);
2613 2614 2615
	if (rc < 0)
		goto destroy_wq;

2616
	nfc_info(dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2617
		 fw_ver.ic, fw_ver.ver, fw_ver.rev);
2618 2619


2620 2621
	priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
					   priv->ops->tx_header_len +
2622
					   PN533_CMD_DATAEXCH_HEAD_LEN,
2623 2624
					   priv->ops->tx_tail_len);
	if (!priv->nfc_dev) {
2625
		rc = -ENOMEM;
2626
		goto destroy_wq;
2627
	}
2628

2629
	nfc_set_parent_dev(priv->nfc_dev, parent);
2630
	nfc_set_drvdata(priv->nfc_dev, priv);
2631

2632
	rc = nfc_register_device(priv->nfc_dev);
2633 2634 2635
	if (rc)
		goto free_nfc_dev;

2636
	rc = pn533_setup(priv);
2637
	if (rc)
S
Samuel Ortiz 已提交
2638 2639
		goto unregister_nfc_dev;

2640
	return priv;
2641

2642
unregister_nfc_dev:
2643
	nfc_unregister_device(priv->nfc_dev);
2644

2645
free_nfc_dev:
2646
	nfc_free_device(priv->nfc_dev);
2647

2648
destroy_wq:
2649
	destroy_workqueue(priv->wq);
2650
error:
2651 2652
	kfree(priv);
	return ERR_PTR(rc);
2653
}
2654
EXPORT_SYMBOL_GPL(pn533_register_device);
2655

2656
void pn533_unregister_device(struct pn533 *priv)
2657
{
S
Samuel Ortiz 已提交
2658
	struct pn533_cmd *cmd, *n;
2659

2660 2661
	nfc_unregister_device(priv->nfc_dev);
	nfc_free_device(priv->nfc_dev);
2662

2663 2664
	flush_delayed_work(&priv->poll_work);
	destroy_workqueue(priv->wq);
2665

2666
	skb_queue_purge(&priv->resp_q);
2667

2668
	del_timer(&priv->listen_timer);
2669

2670
	list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
S
Samuel Ortiz 已提交
2671 2672 2673 2674
		list_del(&cmd->queue);
		kfree(cmd);
	}

2675
	kfree(priv);
2676
}
2677
EXPORT_SYMBOL_GPL(pn533_unregister_device);
2678 2679


2680 2681 2682
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>");
2683
MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2684 2685
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");