File Index
Files and directories
Sign in
FILE

digital_technology.c

File preview with syntax highlighting.

linux-6.19.14/net/nfc/digital_technology.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * NFC Digital Protocol stack
   4 * Copyright (c) 2013, Intel Corporation.
   5 */
   6
   7#define pr_fmt(fmt) "digital: %s: " fmt, __func__
   8
   9#include "digital.h"
  10
  11#define DIGITAL_CMD_SENS_REQ    0x26
  12#define DIGITAL_CMD_ALL_REQ     0x52
  13#define DIGITAL_CMD_SEL_REQ_CL1 0x93
  14#define DIGITAL_CMD_SEL_REQ_CL2 0x95
  15#define DIGITAL_CMD_SEL_REQ_CL3 0x97
  16
  17#define DIGITAL_SDD_REQ_SEL_PAR 0x20
  18
  19#define DIGITAL_SDD_RES_CT  0x88
  20#define DIGITAL_SDD_RES_LEN 5
  21#define DIGITAL_SEL_RES_LEN 1
  22
  23#define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
  24#define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
  25#define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
  26#define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
  27
  28#define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
  29#define DIGITAL_SENS_RES_IS_VALID(sens_res) \
  30	((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
  31	(((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
  32
  33#define DIGITAL_MIFARE_READ_RES_LEN 16
  34#define DIGITAL_MIFARE_ACK_RES	0x0A
  35
  36#define DIGITAL_CMD_SENSB_REQ			0x05
  37#define DIGITAL_SENSB_ADVANCED			BIT(5)
  38#define DIGITAL_SENSB_EXTENDED			BIT(4)
  39#define DIGITAL_SENSB_ALLB_REQ			BIT(3)
  40#define DIGITAL_SENSB_N(n)			((n) & 0x7)
  41
  42#define DIGITAL_CMD_SENSB_RES			0x50
  43
  44#define DIGITAL_CMD_ATTRIB_REQ			0x1D
  45#define DIGITAL_ATTRIB_P1_TR0_DEFAULT		(0x0 << 6)
  46#define DIGITAL_ATTRIB_P1_TR1_DEFAULT		(0x0 << 4)
  47#define DIGITAL_ATTRIB_P1_SUPRESS_EOS		BIT(3)
  48#define DIGITAL_ATTRIB_P1_SUPRESS_SOS		BIT(2)
  49#define DIGITAL_ATTRIB_P2_LISTEN_POLL_1		(0x0 << 6)
  50#define DIGITAL_ATTRIB_P2_POLL_LISTEN_1		(0x0 << 4)
  51#define DIGITAL_ATTRIB_P2_MAX_FRAME_256		0x8
  52#define DIGITAL_ATTRIB_P4_DID(n)		((n) & 0xf)
  53
  54#define DIGITAL_CMD_SENSF_REQ	0x00
  55#define DIGITAL_CMD_SENSF_RES	0x01
  56
  57#define DIGITAL_SENSF_RES_MIN_LENGTH 17
  58#define DIGITAL_SENSF_RES_RD_AP_B1   0x00
  59#define DIGITAL_SENSF_RES_RD_AP_B2   0x8F
  60
  61#define DIGITAL_SENSF_REQ_RC_NONE 0
  62#define DIGITAL_SENSF_REQ_RC_SC   1
  63#define DIGITAL_SENSF_REQ_RC_AP   2
  64
  65#define DIGITAL_CMD_ISO15693_INVENTORY_REQ	0x01
  66
  67#define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE	BIT(1)
  68#define DIGITAL_ISO15693_REQ_FLAG_INVENTORY	BIT(2)
  69#define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS	BIT(5)
  70#define DIGITAL_ISO15693_RES_FLAG_ERROR		BIT(0)
  71#define DIGITAL_ISO15693_RES_IS_VALID(flags) \
  72	(!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
  73
  74#define DIGITAL_ISO_DEP_I_PCB	 0x02
  75#define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
  76
  77#define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
  78
  79#define DIGITAL_ISO_DEP_I_BLOCK 0x00
  80
  81#define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
  82
  83static const u8 digital_ats_fsc[] = {
  84	 16,  24,  32,  40,  48,  64,  96, 128,
  85};
  86
  87#define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
  88#define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
  89#define DIGITAL_ATS_MAX_FSC  256
  90
  91#define DIGITAL_RATS_BYTE1 0xE0
  92#define DIGITAL_RATS_PARAM 0x80
  93
  94struct digital_sdd_res {
  95	u8 nfcid1[4];
  96	u8 bcc;
  97} __packed;
  98
  99struct digital_sel_req {
 100	u8 sel_cmd;
 101	u8 b2;
 102	u8 nfcid1[4];
 103	u8 bcc;
 104} __packed;
 105
 106struct digital_sensb_req {
 107	u8 cmd;
 108	u8 afi;
 109	u8 param;
 110} __packed;
 111
 112struct digital_sensb_res {
 113	u8 cmd;
 114	u8 nfcid0[4];
 115	u8 app_data[4];
 116	u8 proto_info[3];
 117} __packed;
 118
 119struct digital_attrib_req {
 120	u8 cmd;
 121	u8 nfcid0[4];
 122	u8 param1;
 123	u8 param2;
 124	u8 param3;
 125	u8 param4;
 126} __packed;
 127
 128struct digital_attrib_res {
 129	u8 mbli_did;
 130} __packed;
 131
 132struct digital_sensf_req {
 133	u8 cmd;
 134	u8 sc1;
 135	u8 sc2;
 136	u8 rc;
 137	u8 tsn;
 138} __packed;
 139
 140struct digital_sensf_res {
 141	u8 cmd;
 142	u8 nfcid2[8];
 143	u8 pad0[2];
 144	u8 pad1[3];
 145	u8 mrti_check;
 146	u8 mrti_update;
 147	u8 pad2;
 148	u8 rd[2];
 149} __packed;
 150
 151struct digital_iso15693_inv_req {
 152	u8 flags;
 153	u8 cmd;
 154	u8 mask_len;
 155	u64 mask;
 156} __packed;
 157
 158struct digital_iso15693_inv_res {
 159	u8 flags;
 160	u8 dsfid;
 161	u64 uid;
 162} __packed;
 163
 164static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
 165				   struct nfc_target *target);
 166
 167int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
 168				struct sk_buff *skb)
 169{
 170	u8 pcb;
 171	u8 block_type;
 172
 173	if (skb->len < 1)
 174		return -EIO;
 175
 176	pcb = *skb->data;
 177	block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
 178
 179	/* No support fo R-block nor S-block */
 180	if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
 181		pr_err("ISO_DEP R-block and S-block not supported\n");
 182		return -EIO;
 183	}
 184
 185	if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
 186		pr_err("DID field in ISO_DEP PCB not supported\n");
 187		return -EIO;
 188	}
 189
 190	skb_pull(skb, 1);
 191
 192	return 0;
 193}
 194
 195int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
 196				struct sk_buff *skb)
 197{
 198	/*
 199	 * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
 200	 * not be greater than remote FSC
 201	 */
 202	if (skb->len + 3 > ddev->target_fsc)
 203		return -EIO;
 204
 205	skb_push(skb, 1);
 206
 207	*skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
 208
 209	ddev->curr_nfc_dep_pni =
 210		DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
 211
 212	return 0;
 213}
 214
 215static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
 216				struct sk_buff *resp)
 217{
 218	struct nfc_target *target = arg;
 219	u8 fsdi;
 220	int rc;
 221
 222	if (IS_ERR(resp)) {
 223		rc = PTR_ERR(resp);
 224		resp = NULL;
 225		goto exit;
 226	}
 227
 228	if (resp->len < 2) {
 229		rc = -EIO;
 230		goto exit;
 231	}
 232
 233	fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
 234	if (fsdi >= 8)
 235		ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
 236	else
 237		ddev->target_fsc = digital_ats_fsc[fsdi];
 238
 239	ddev->curr_nfc_dep_pni = 0;
 240
 241	rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
 242
 243exit:
 244	dev_kfree_skb(resp);
 245	kfree(target);
 246
 247	if (rc)
 248		digital_poll_next_tech(ddev);
 249}
 250
 251static int digital_in_send_rats(struct nfc_digital_dev *ddev,
 252				struct nfc_target *target)
 253{
 254	int rc;
 255	struct sk_buff *skb;
 256
 257	skb = digital_skb_alloc(ddev, 2);
 258	if (!skb)
 259		return -ENOMEM;
 260
 261	skb_put_u8(skb, DIGITAL_RATS_BYTE1);
 262	skb_put_u8(skb, DIGITAL_RATS_PARAM);
 263
 264	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
 265				 target);
 266	if (rc)
 267		kfree_skb(skb);
 268
 269	return rc;
 270}
 271
 272static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
 273				    struct sk_buff *resp)
 274{
 275	struct nfc_target *target = arg;
 276	int rc;
 277	u8 sel_res;
 278	u8 nfc_proto;
 279
 280	if (IS_ERR(resp)) {
 281		rc = PTR_ERR(resp);
 282		resp = NULL;
 283		goto exit;
 284	}
 285
 286	if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
 287		rc = digital_skb_check_crc_a(resp);
 288		if (rc) {
 289			PROTOCOL_ERR("4.4.1.3");
 290			goto exit;
 291		}
 292	}
 293
 294	if (resp->len != DIGITAL_SEL_RES_LEN) {
 295		rc = -EIO;
 296		goto exit;
 297	}
 298
 299	sel_res = resp->data[0];
 300
 301	if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
 302		rc = digital_in_send_sdd_req(ddev, target);
 303		if (rc)
 304			goto exit;
 305
 306		goto exit_free_skb;
 307	}
 308
 309	target->sel_res = sel_res;
 310
 311	if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
 312		nfc_proto = NFC_PROTO_MIFARE;
 313	} else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
 314		nfc_proto = NFC_PROTO_NFC_DEP;
 315	} else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
 316		rc = digital_in_send_rats(ddev, target);
 317		if (rc)
 318			goto exit;
 319		/*
 320		 * Skip target_found and don't free it for now. This will be
 321		 * done when receiving the ATS
 322		 */
 323		goto exit_free_skb;
 324	} else {
 325		rc = -EOPNOTSUPP;
 326		goto exit;
 327	}
 328
 329	rc = digital_target_found(ddev, target, nfc_proto);
 330
 331exit:
 332	kfree(target);
 333
 334exit_free_skb:
 335	dev_kfree_skb(resp);
 336
 337	if (rc)
 338		digital_poll_next_tech(ddev);
 339}
 340
 341static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
 342				   struct nfc_target *target,
 343				   struct digital_sdd_res *sdd_res)
 344{
 345	struct sk_buff *skb;
 346	struct digital_sel_req *sel_req;
 347	u8 sel_cmd;
 348	int rc;
 349
 350	skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
 351	if (!skb)
 352		return -ENOMEM;
 353
 354	skb_put(skb, sizeof(struct digital_sel_req));
 355	sel_req = (struct digital_sel_req *)skb->data;
 356
 357	if (target->nfcid1_len <= 4)
 358		sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
 359	else if (target->nfcid1_len < 10)
 360		sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
 361	else
 362		sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
 363
 364	sel_req->sel_cmd = sel_cmd;
 365	sel_req->b2 = 0x70;
 366	memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
 367	sel_req->bcc = sdd_res->bcc;
 368
 369	if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
 370		rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 371				NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
 372		if (rc)
 373			goto exit;
 374	} else {
 375		digital_skb_add_crc_a(skb);
 376	}
 377
 378	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
 379				 target);
 380exit:
 381	if (rc)
 382		kfree_skb(skb);
 383
 384	return rc;
 385}
 386
 387static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
 388				    struct sk_buff *resp)
 389{
 390	struct nfc_target *target = arg;
 391	struct digital_sdd_res *sdd_res;
 392	int rc;
 393	u8 offset, size;
 394	u8 i, bcc;
 395
 396	if (IS_ERR(resp)) {
 397		rc = PTR_ERR(resp);
 398		resp = NULL;
 399		goto exit;
 400	}
 401
 402	if (resp->len < DIGITAL_SDD_RES_LEN) {
 403		PROTOCOL_ERR("4.7.2.8");
 404		rc = -EINVAL;
 405		goto exit;
 406	}
 407
 408	sdd_res = (struct digital_sdd_res *)resp->data;
 409
 410	for (i = 0, bcc = 0; i < 4; i++)
 411		bcc ^= sdd_res->nfcid1[i];
 412
 413	if (bcc != sdd_res->bcc) {
 414		PROTOCOL_ERR("4.7.2.6");
 415		rc = -EINVAL;
 416		goto exit;
 417	}
 418
 419	if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
 420		offset = 1;
 421		size = 3;
 422	} else {
 423		offset = 0;
 424		size = 4;
 425	}
 426
 427	if (target->nfcid1_len + size > NFC_NFCID1_MAXSIZE) {
 428		PROTOCOL_ERR("4.7.2.1");
 429		rc = -EPROTO;
 430		goto exit;
 431	}
 432
 433	memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
 434	       size);
 435	target->nfcid1_len += size;
 436
 437	rc = digital_in_send_sel_req(ddev, target, sdd_res);
 438
 439exit:
 440	dev_kfree_skb(resp);
 441
 442	if (rc) {
 443		kfree(target);
 444		digital_poll_next_tech(ddev);
 445	}
 446}
 447
 448static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
 449				   struct nfc_target *target)
 450{
 451	int rc;
 452	struct sk_buff *skb;
 453	u8 sel_cmd;
 454
 455	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 456				     NFC_DIGITAL_FRAMING_NFCA_STANDARD);
 457	if (rc)
 458		return rc;
 459
 460	skb = digital_skb_alloc(ddev, 2);
 461	if (!skb)
 462		return -ENOMEM;
 463
 464	if (target->nfcid1_len == 0)
 465		sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
 466	else if (target->nfcid1_len == 3)
 467		sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
 468	else
 469		sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
 470
 471	skb_put_u8(skb, sel_cmd);
 472	skb_put_u8(skb, DIGITAL_SDD_REQ_SEL_PAR);
 473
 474	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
 475				 target);
 476	if (rc)
 477		kfree_skb(skb);
 478
 479	return rc;
 480}
 481
 482static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
 483				     struct sk_buff *resp)
 484{
 485	struct nfc_target *target = NULL;
 486	int rc;
 487
 488	if (IS_ERR(resp)) {
 489		rc = PTR_ERR(resp);
 490		resp = NULL;
 491		goto exit;
 492	}
 493
 494	if (resp->len < sizeof(u16)) {
 495		rc = -EIO;
 496		goto exit;
 497	}
 498
 499	target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
 500	if (!target) {
 501		rc = -ENOMEM;
 502		goto exit;
 503	}
 504
 505	target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
 506
 507	if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
 508		PROTOCOL_ERR("4.6.3.3");
 509		rc = -EINVAL;
 510		goto exit;
 511	}
 512
 513	if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
 514		rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
 515	else
 516		rc = digital_in_send_sdd_req(ddev, target);
 517
 518exit:
 519	dev_kfree_skb(resp);
 520
 521	if (rc) {
 522		kfree(target);
 523		digital_poll_next_tech(ddev);
 524	}
 525}
 526
 527int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
 528{
 529	struct sk_buff *skb;
 530	int rc;
 531
 532	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
 533				     NFC_DIGITAL_RF_TECH_106A);
 534	if (rc)
 535		return rc;
 536
 537	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 538				     NFC_DIGITAL_FRAMING_NFCA_SHORT);
 539	if (rc)
 540		return rc;
 541
 542	skb = digital_skb_alloc(ddev, 1);
 543	if (!skb)
 544		return -ENOMEM;
 545
 546	skb_put_u8(skb, DIGITAL_CMD_SENS_REQ);
 547
 548	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
 549	if (rc)
 550		kfree_skb(skb);
 551
 552	return rc;
 553}
 554
 555int digital_in_recv_mifare_res(struct sk_buff *resp)
 556{
 557	/* Successful READ command response is 16 data bytes + 2 CRC bytes long.
 558	 * Since the driver can't differentiate a ACK/NACK response from a valid
 559	 * READ response, the CRC calculation must be handled at digital level
 560	 * even if the driver supports it for this technology.
 561	 */
 562	if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
 563		if (digital_skb_check_crc_a(resp)) {
 564			PROTOCOL_ERR("9.4.1.2");
 565			return -EIO;
 566		}
 567
 568		return 0;
 569	}
 570
 571	/* ACK response (i.e. successful WRITE). */
 572	if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
 573		resp->data[0] = 0;
 574		return 0;
 575	}
 576
 577	/* NACK and any other responses are treated as error. */
 578	return -EIO;
 579}
 580
 581static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
 582				       struct sk_buff *resp)
 583{
 584	struct nfc_target *target = arg;
 585	struct digital_attrib_res *attrib_res;
 586	int rc;
 587
 588	if (IS_ERR(resp)) {
 589		rc = PTR_ERR(resp);
 590		resp = NULL;
 591		goto exit;
 592	}
 593
 594	if (resp->len < sizeof(*attrib_res)) {
 595		PROTOCOL_ERR("12.6.2");
 596		rc = -EIO;
 597		goto exit;
 598	}
 599
 600	attrib_res = (struct digital_attrib_res *)resp->data;
 601
 602	if (attrib_res->mbli_did & 0x0f) {
 603		PROTOCOL_ERR("12.6.2.1");
 604		rc = -EIO;
 605		goto exit;
 606	}
 607
 608	rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
 609
 610exit:
 611	dev_kfree_skb(resp);
 612	kfree(target);
 613
 614	if (rc)
 615		digital_poll_next_tech(ddev);
 616}
 617
 618static int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
 619			       struct nfc_target *target,
 620			       struct digital_sensb_res *sensb_res)
 621{
 622	struct digital_attrib_req *attrib_req;
 623	struct sk_buff *skb;
 624	int rc;
 625
 626	skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
 627	if (!skb)
 628		return -ENOMEM;
 629
 630	attrib_req = skb_put(skb, sizeof(*attrib_req));
 631
 632	attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
 633	memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
 634	       sizeof(attrib_req->nfcid0));
 635	attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
 636			     DIGITAL_ATTRIB_P1_TR1_DEFAULT;
 637	attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
 638			     DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
 639			     DIGITAL_ATTRIB_P2_MAX_FRAME_256;
 640	attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
 641	attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
 642
 643	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
 644				 target);
 645	if (rc)
 646		kfree_skb(skb);
 647
 648	return rc;
 649}
 650
 651static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
 652				      struct sk_buff *resp)
 653{
 654	struct nfc_target *target = NULL;
 655	struct digital_sensb_res *sensb_res;
 656	u8 fsci;
 657	int rc;
 658
 659	if (IS_ERR(resp)) {
 660		rc = PTR_ERR(resp);
 661		resp = NULL;
 662		goto exit;
 663	}
 664
 665	if (resp->len != sizeof(*sensb_res)) {
 666		PROTOCOL_ERR("5.6.2.1");
 667		rc = -EIO;
 668		goto exit;
 669	}
 670
 671	sensb_res = (struct digital_sensb_res *)resp->data;
 672
 673	if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
 674		PROTOCOL_ERR("5.6.2");
 675		rc = -EIO;
 676		goto exit;
 677	}
 678
 679	if (!(sensb_res->proto_info[1] & BIT(0))) {
 680		PROTOCOL_ERR("5.6.2.12");
 681		rc = -EIO;
 682		goto exit;
 683	}
 684
 685	if (sensb_res->proto_info[1] & BIT(3)) {
 686		PROTOCOL_ERR("5.6.2.16");
 687		rc = -EIO;
 688		goto exit;
 689	}
 690
 691	fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
 692	if (fsci >= 8)
 693		ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
 694	else
 695		ddev->target_fsc = digital_ats_fsc[fsci];
 696
 697	target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
 698	if (!target) {
 699		rc = -ENOMEM;
 700		goto exit;
 701	}
 702
 703	rc = digital_in_send_attrib_req(ddev, target, sensb_res);
 704
 705exit:
 706	dev_kfree_skb(resp);
 707
 708	if (rc) {
 709		kfree(target);
 710		digital_poll_next_tech(ddev);
 711	}
 712}
 713
 714int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
 715{
 716	struct digital_sensb_req *sensb_req;
 717	struct sk_buff *skb;
 718	int rc;
 719
 720	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
 721				     NFC_DIGITAL_RF_TECH_106B);
 722	if (rc)
 723		return rc;
 724
 725	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 726				     NFC_DIGITAL_FRAMING_NFCB);
 727	if (rc)
 728		return rc;
 729
 730	skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
 731	if (!skb)
 732		return -ENOMEM;
 733
 734	sensb_req = skb_put(skb, sizeof(*sensb_req));
 735
 736	sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
 737	sensb_req->afi = 0x00; /* All families and sub-families */
 738	sensb_req->param = DIGITAL_SENSB_N(0);
 739
 740	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
 741				 NULL);
 742	if (rc)
 743		kfree_skb(skb);
 744
 745	return rc;
 746}
 747
 748static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
 749				   struct sk_buff *resp)
 750{
 751	int rc;
 752	u8 proto;
 753	struct nfc_target target;
 754	struct digital_sensf_res *sensf_res;
 755
 756	if (IS_ERR(resp)) {
 757		rc = PTR_ERR(resp);
 758		resp = NULL;
 759		goto exit;
 760	}
 761
 762	if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
 763		rc = -EIO;
 764		goto exit;
 765	}
 766
 767	if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
 768		rc = digital_skb_check_crc_f(resp);
 769		if (rc) {
 770			PROTOCOL_ERR("6.4.1.8");
 771			goto exit;
 772		}
 773	}
 774
 775	skb_pull(resp, 1);
 776
 777	memset(&target, 0, sizeof(struct nfc_target));
 778
 779	sensf_res = (struct digital_sensf_res *)resp->data;
 780
 781	memcpy(target.sensf_res, sensf_res, resp->len);
 782	target.sensf_res_len = resp->len;
 783
 784	memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
 785	target.nfcid2_len = NFC_NFCID2_MAXSIZE;
 786
 787	if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
 788	    target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
 789		proto = NFC_PROTO_NFC_DEP;
 790	else
 791		proto = NFC_PROTO_FELICA;
 792
 793	rc = digital_target_found(ddev, &target, proto);
 794
 795exit:
 796	dev_kfree_skb(resp);
 797
 798	if (rc)
 799		digital_poll_next_tech(ddev);
 800}
 801
 802int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
 803{
 804	struct digital_sensf_req *sensf_req;
 805	struct sk_buff *skb;
 806	int rc;
 807	u8 size;
 808
 809	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
 810	if (rc)
 811		return rc;
 812
 813	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 814				     NFC_DIGITAL_FRAMING_NFCF);
 815	if (rc)
 816		return rc;
 817
 818	size = sizeof(struct digital_sensf_req);
 819
 820	skb = digital_skb_alloc(ddev, size);
 821	if (!skb)
 822		return -ENOMEM;
 823
 824	skb_put(skb, size);
 825
 826	sensf_req = (struct digital_sensf_req *)skb->data;
 827	sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
 828	sensf_req->sc1 = 0xFF;
 829	sensf_req->sc2 = 0xFF;
 830	sensf_req->rc = 0;
 831	sensf_req->tsn = 0;
 832
 833	*(u8 *)skb_push(skb, 1) = size + 1;
 834
 835	if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
 836		digital_skb_add_crc_f(skb);
 837
 838	rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
 839				 NULL);
 840	if (rc)
 841		kfree_skb(skb);
 842
 843	return rc;
 844}
 845
 846static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
 847		void *arg, struct sk_buff *resp)
 848{
 849	struct digital_iso15693_inv_res *res;
 850	struct nfc_target *target = NULL;
 851	int rc;
 852
 853	if (IS_ERR(resp)) {
 854		rc = PTR_ERR(resp);
 855		resp = NULL;
 856		goto out_free_skb;
 857	}
 858
 859	if (resp->len != sizeof(*res)) {
 860		rc = -EIO;
 861		goto out_free_skb;
 862	}
 863
 864	res = (struct digital_iso15693_inv_res *)resp->data;
 865
 866	if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
 867		PROTOCOL_ERR("ISO15693 - 10.3.1");
 868		rc = -EINVAL;
 869		goto out_free_skb;
 870	}
 871
 872	target = kzalloc(sizeof(*target), GFP_KERNEL);
 873	if (!target) {
 874		rc = -ENOMEM;
 875		goto out_free_skb;
 876	}
 877
 878	target->is_iso15693 = 1;
 879	target->iso15693_dsfid = res->dsfid;
 880	memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
 881
 882	rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
 883
 884	kfree(target);
 885
 886out_free_skb:
 887	dev_kfree_skb(resp);
 888
 889	if (rc)
 890		digital_poll_next_tech(ddev);
 891}
 892
 893int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
 894{
 895	struct digital_iso15693_inv_req *req;
 896	struct sk_buff *skb;
 897	int rc;
 898
 899	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
 900				     NFC_DIGITAL_RF_TECH_ISO15693);
 901	if (rc)
 902		return rc;
 903
 904	rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 905				     NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
 906	if (rc)
 907		return rc;
 908
 909	skb = digital_skb_alloc(ddev, sizeof(*req));
 910	if (!skb)
 911		return -ENOMEM;
 912
 913	skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
 914	req = (struct digital_iso15693_inv_req *)skb->data;
 915
 916	/* Single sub-carrier, high data rate, no AFI, single slot
 917	 * Inventory command
 918	 */
 919	req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
 920		     DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
 921		     DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
 922	req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
 923	req->mask_len = 0;
 924
 925	rc = digital_in_send_cmd(ddev, skb, 30,
 926				 digital_in_recv_iso15693_inv_res, NULL);
 927	if (rc)
 928		kfree_skb(skb);
 929
 930	return rc;
 931}
 932
 933static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
 934{
 935	struct sk_buff *skb;
 936	int rc;
 937
 938	skb = digital_skb_alloc(ddev, 1);
 939	if (!skb)
 940		return -ENOMEM;
 941
 942	skb_put_u8(skb, DIGITAL_SEL_RES_NFC_DEP);
 943
 944	if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
 945		digital_skb_add_crc_a(skb);
 946
 947	rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 948				     NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE);
 949	if (rc) {
 950		kfree_skb(skb);
 951		return rc;
 952	}
 953
 954	rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
 955				 NULL);
 956	if (rc)
 957		kfree_skb(skb);
 958
 959	return rc;
 960}
 961
 962static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
 963				    struct sk_buff *resp)
 964{
 965	int rc;
 966
 967	if (IS_ERR(resp)) {
 968		rc = PTR_ERR(resp);
 969		resp = NULL;
 970		goto exit;
 971	}
 972
 973	if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
 974		rc = digital_skb_check_crc_a(resp);
 975		if (rc) {
 976			PROTOCOL_ERR("4.4.1.3");
 977			goto exit;
 978		}
 979	}
 980
 981	/* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
 982
 983	rc = digital_tg_send_sel_res(ddev);
 984
 985exit:
 986	if (rc)
 987		digital_poll_next_tech(ddev);
 988
 989	dev_kfree_skb(resp);
 990}
 991
 992static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
 993{
 994	struct sk_buff *skb;
 995	struct digital_sdd_res *sdd_res;
 996	int rc, i;
 997
 998	skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
 999	if (!skb)
1000		return -ENOMEM;
1001
1002	skb_put(skb, sizeof(struct digital_sdd_res));
1003	sdd_res = (struct digital_sdd_res *)skb->data;
1004
1005	sdd_res->nfcid1[0] = 0x08;
1006	get_random_bytes(sdd_res->nfcid1 + 1, 3);
1007
1008	sdd_res->bcc = 0;
1009	for (i = 0; i < 4; i++)
1010		sdd_res->bcc ^= sdd_res->nfcid1[i];
1011
1012	rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1013				NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
1014	if (rc) {
1015		kfree_skb(skb);
1016		return rc;
1017	}
1018
1019	rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
1020				 NULL);
1021	if (rc)
1022		kfree_skb(skb);
1023
1024	return rc;
1025}
1026
1027static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
1028				    struct sk_buff *resp)
1029{
1030	u8 *sdd_req;
1031	int rc;
1032
1033	if (IS_ERR(resp)) {
1034		rc = PTR_ERR(resp);
1035		resp = NULL;
1036		goto exit;
1037	}
1038
1039	sdd_req = resp->data;
1040
1041	if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
1042	    sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
1043		rc = -EINVAL;
1044		goto exit;
1045	}
1046
1047	rc = digital_tg_send_sdd_res(ddev);
1048
1049exit:
1050	if (rc)
1051		digital_poll_next_tech(ddev);
1052
1053	dev_kfree_skb(resp);
1054}
1055
1056static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
1057{
1058	struct sk_buff *skb;
1059	u8 *sens_res;
1060	int rc;
1061
1062	skb = digital_skb_alloc(ddev, 2);
1063	if (!skb)
1064		return -ENOMEM;
1065
1066	sens_res = skb_put(skb, 2);
1067
1068	sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
1069	sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
1070
1071	rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1072				     NFC_DIGITAL_FRAMING_NFCA_STANDARD);
1073	if (rc) {
1074		kfree_skb(skb);
1075		return rc;
1076	}
1077
1078	rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
1079				 NULL);
1080	if (rc)
1081		kfree_skb(skb);
1082
1083	return rc;
1084}
1085
1086void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
1087			      struct sk_buff *resp)
1088{
1089	u8 sens_req;
1090	int rc;
1091
1092	if (IS_ERR(resp)) {
1093		rc = PTR_ERR(resp);
1094		resp = NULL;
1095		goto exit;
1096	}
1097
1098	sens_req = resp->data[0];
1099
1100	if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
1101	    sens_req != DIGITAL_CMD_ALL_REQ)) {
1102		rc = -EINVAL;
1103		goto exit;
1104	}
1105
1106	rc = digital_tg_send_sens_res(ddev);
1107
1108exit:
1109	if (rc)
1110		digital_poll_next_tech(ddev);
1111
1112	dev_kfree_skb(resp);
1113}
1114
1115static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1116		void *arg, struct sk_buff *resp)
1117{
1118	if (!IS_ERR(resp) && (resp->len >= 2) &&
1119			(resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1120		digital_tg_recv_sensf_req(ddev, arg, resp);
1121	else
1122		digital_tg_recv_atr_req(ddev, arg, resp);
1123
1124	return;
1125}
1126
1127static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
1128			      struct digital_sensf_req *sensf_req)
1129{
1130	struct sk_buff *skb;
1131	u8 size;
1132	int rc;
1133	struct digital_sensf_res *sensf_res;
1134
1135	size = sizeof(struct digital_sensf_res);
1136
1137	if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
1138		size -= sizeof(sensf_res->rd);
1139
1140	skb = digital_skb_alloc(ddev, size);
1141	if (!skb)
1142		return -ENOMEM;
1143
1144	skb_put(skb, size);
1145
1146	sensf_res = (struct digital_sensf_res *)skb->data;
1147
1148	memset(sensf_res, 0, size);
1149
1150	sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
1151	sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1152	sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1153	get_random_bytes(&sensf_res->nfcid2[2], 6);
1154
1155	switch (sensf_req->rc) {
1156	case DIGITAL_SENSF_REQ_RC_SC:
1157		sensf_res->rd[0] = sensf_req->sc1;
1158		sensf_res->rd[1] = sensf_req->sc2;
1159		break;
1160	case DIGITAL_SENSF_REQ_RC_AP:
1161		sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
1162		sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
1163		break;
1164	}
1165
1166	*(u8 *)skb_push(skb, sizeof(u8)) = size + 1;
1167
1168	if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
1169		digital_skb_add_crc_f(skb);
1170
1171	rc = digital_tg_send_cmd(ddev, skb, 300,
1172				 digital_tg_recv_atr_or_sensf_req, NULL);
1173	if (rc)
1174		kfree_skb(skb);
1175
1176	return rc;
1177}
1178
1179void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
1180			       struct sk_buff *resp)
1181{
1182	struct digital_sensf_req *sensf_req;
1183	int rc;
1184
1185	if (IS_ERR(resp)) {
1186		rc = PTR_ERR(resp);
1187		resp = NULL;
1188		goto exit;
1189	}
1190
1191	if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
1192		rc = digital_skb_check_crc_f(resp);
1193		if (rc) {
1194			PROTOCOL_ERR("6.4.1.8");
1195			goto exit;
1196		}
1197	}
1198
1199	if (resp->len != sizeof(struct digital_sensf_req) + 1) {
1200		rc = -EINVAL;
1201		goto exit;
1202	}
1203
1204	skb_pull(resp, 1);
1205	sensf_req = (struct digital_sensf_req *)resp->data;
1206
1207	if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
1208		rc = -EINVAL;
1209		goto exit;
1210	}
1211
1212	rc = digital_tg_send_sensf_res(ddev, sensf_req);
1213
1214exit:
1215	if (rc)
1216		digital_poll_next_tech(ddev);
1217
1218	dev_kfree_skb(resp);
1219}
1220
1221static int digital_tg_config_nfca(struct nfc_digital_dev *ddev)
1222{
1223	int rc;
1224
1225	rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1226				     NFC_DIGITAL_RF_TECH_106A);
1227	if (rc)
1228		return rc;
1229
1230	return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1231				       NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1232}
1233
1234int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
1235{
1236	int rc;
1237
1238	rc = digital_tg_config_nfca(ddev);
1239	if (rc)
1240		return rc;
1241
1242	return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
1243}
1244
1245static int digital_tg_config_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1246{
1247	int rc;
1248
1249	rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1250	if (rc)
1251		return rc;
1252
1253	return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1254				       NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
1255}
1256
1257int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1258{
1259	int rc;
1260
1261	rc = digital_tg_config_nfcf(ddev, rf_tech);
1262	if (rc)
1263		return rc;
1264
1265	return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, NULL);
1266}
1267
1268void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg,
1269			    struct sk_buff *resp)
1270{
1271	u8 rf_tech;
1272	int rc;
1273
1274	if (IS_ERR(resp)) {
1275		resp = NULL;
1276		goto exit_free_skb;
1277	}
1278
1279	rc = ddev->ops->tg_get_rf_tech(ddev, &rf_tech);
1280	if (rc)
1281		goto exit_free_skb;
1282
1283	switch (rf_tech) {
1284	case NFC_DIGITAL_RF_TECH_106A:
1285		rc = digital_tg_config_nfca(ddev);
1286		if (rc)
1287			goto exit_free_skb;
1288		digital_tg_recv_sens_req(ddev, arg, resp);
1289		break;
1290	case NFC_DIGITAL_RF_TECH_212F:
1291	case NFC_DIGITAL_RF_TECH_424F:
1292		rc = digital_tg_config_nfcf(ddev, rf_tech);
1293		if (rc)
1294			goto exit_free_skb;
1295		digital_tg_recv_sensf_req(ddev, arg, resp);
1296		break;
1297	default:
1298		goto exit_free_skb;
1299	}
1300
1301	return;
1302
1303exit_free_skb:
1304	digital_poll_next_tech(ddev);
1305	dev_kfree_skb(resp);
1306}
~/ Read-only file preview.