Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

NFC: st21nfca: Adding P2P support to st21nfca in Initiator & Target mode

Support for Initiator and Target mode with ISO18092 commands support:
- ATR_REQ/ATR_RES
- PSL_REQ/PSL_RES
- DEP_REQ/DEP_RES

Work based on net/nfc/digital_dep.c.
st21nfca is using:
- Gate reader F for P2P in initiator mode.
- Gate card F for P2P in target mode.

Felica tag and p2p are differentiated with NFCID2.
When starting with 01FE it is acting in p2p mode.

On complete_target_discovered on ST21NFCA_RF_READER_F_GATE
supported_protocols is set to NFC_PROTO_NFC_DEP_MASK
for P2P.

Tested against: Nexus S, Galaxy S2, Galaxy S3, Galaxy S3 Mini,
Nexus 4 & Nexus 5.

Signed-off-by: Christophe Ricard <christophe-h.ricard@st.com>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>

authored by

Christophe Ricard and committed by
Samuel Ortiz
1892bf84 d57d74eb

+993 -3
+1 -1
drivers/nfc/st21nfca/Makefile
··· 4 4 5 5 st21nfca_i2c-objs = i2c.o 6 6 7 - obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o 7 + obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o st21nfca_dep.o 8 8 obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
+263 -1
drivers/nfc/st21nfca/st21nfca.c
··· 22 22 #include <net/nfc/llc.h> 23 23 24 24 #include "st21nfca.h" 25 + #include "st21nfca_dep.h" 25 26 26 27 #define DRIVER_DESC "HCI NFC driver for ST21NFCA" 27 28 ··· 74 73 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE}, 75 74 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE}, 76 75 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE}, 76 + {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE}, 77 77 }; 78 78 79 79 struct st21nfca_pipe_info { ··· 302 300 u32 im_protocols, u32 tm_protocols) 303 301 { 304 302 int r; 303 + u32 pol_req; 304 + u8 param[19]; 305 + struct sk_buff *datarate_skb; 305 306 306 307 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n", 307 308 __func__, im_protocols, tm_protocols); ··· 337 332 ST21NFCA_RF_READER_F_GATE); 338 333 if (r < 0) 339 334 return r; 335 + } else { 336 + hdev->gb = nfc_get_local_general_bytes(hdev->ndev, 337 + &hdev->gb_len); 338 + 339 + if (hdev->gb == NULL || hdev->gb_len == 0) { 340 + im_protocols &= ~NFC_PROTO_NFC_DEP_MASK; 341 + tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK; 342 + } 343 + 344 + param[0] = ST21NFCA_RF_READER_F_DATARATE_106 | 345 + ST21NFCA_RF_READER_F_DATARATE_212 | 346 + ST21NFCA_RF_READER_F_DATARATE_424; 347 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE, 348 + ST21NFCA_RF_READER_F_DATARATE, 349 + param, 1); 350 + if (r < 0) 351 + return r; 352 + 353 + pol_req = 354 + be32_to_cpu(ST21NFCA_RF_READER_F_POL_REQ_DEFAULT); 355 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE, 356 + ST21NFCA_RF_READER_F_POL_REQ, 357 + (u8 *) &pol_req, 4); 358 + if (r < 0) 359 + return r; 340 360 } 341 361 342 362 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) { ··· 384 354 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, 385 355 NFC_HCI_EVT_END_OPERATION, NULL, 0); 386 356 } 357 + 358 + if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { 359 + r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE, 360 + ST21NFCA_RF_CARD_F_DATARATE, 361 + &datarate_skb); 362 + if (r < 0) 363 + return r; 364 + 365 + /* Configure the maximum supported datarate to 424Kbps */ 366 + if (datarate_skb->len > 0 && 367 + datarate_skb->data[0] != 368 + ST21NFCA_RF_CARD_F_DATARATE_212_424) { 369 + param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424; 370 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 371 + ST21NFCA_RF_CARD_F_DATARATE, 372 + param, 1); 373 + if (r < 0) 374 + return r; 375 + } 376 + 377 + /* 378 + * Configure sens_res 379 + * 380 + * NFC Forum Digital Spec Table 7: 381 + * NFCID1 size: triple (10 bytes) 382 + */ 383 + param[0] = 0x00; 384 + param[1] = 0x08; 385 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 386 + ST21NFCA_RF_CARD_F_SENS_RES, param, 2); 387 + if (r < 0) 388 + return r; 389 + 390 + /* 391 + * Configure sel_res 392 + * 393 + * NFC Forum Digistal Spec Table 17: 394 + * b3 set to 0b (value b7-b6): 395 + * - 10b: Configured for NFC-DEP Protocol 396 + */ 397 + param[0] = 0x40; 398 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 399 + ST21NFCA_RF_CARD_F_SEL_RES, param, 1); 400 + if (r < 0) 401 + return r; 402 + 403 + /* Configure NFCID1 Random uid */ 404 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 405 + ST21NFCA_RF_CARD_F_NFCID1, NULL, 0); 406 + if (r < 0) 407 + return r; 408 + 409 + /* Configure NFCID2_LIST */ 410 + /* System Code */ 411 + param[0] = 0x00; 412 + param[1] = 0x00; 413 + /* NFCID2 */ 414 + param[2] = 0x01; 415 + param[3] = 0xfe; 416 + param[4] = 'S'; 417 + param[5] = 'T'; 418 + param[6] = 'M'; 419 + param[7] = 'i'; 420 + param[8] = 'c'; 421 + param[9] = 'r'; 422 + /* 8 byte Pad bytes used for polling respone frame */ 423 + 424 + /* 425 + * Configuration byte: 426 + * - bit 0: define the default NFCID2 entry used when the 427 + * system code is equal to 'FFFF' 428 + * - bit 1: use a random value for lowest 6 bytes of 429 + * NFCID2 value 430 + * - bit 2: ignore polling request frame if request code 431 + * is equal to '01' 432 + * - Other bits are RFU 433 + */ 434 + param[18] = 0x01; 435 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 436 + ST21NFCA_RF_CARD_F_NFCID2_LIST, param, 437 + 19); 438 + if (r < 0) 439 + return r; 440 + 441 + param[0] = 0x02; 442 + r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE, 443 + ST21NFCA_RF_CARD_F_MODE, param, 1); 444 + } 445 + 387 446 return r; 388 447 } 389 448 ··· 577 458 return r; 578 459 } 579 460 461 + static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev, 462 + struct nfc_target *target, u8 comm_mode, 463 + u8 *gb, size_t gb_len) 464 + { 465 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 466 + 467 + info->dep_info.idx = target->idx; 468 + return st21nfca_im_send_atr_req(hdev, gb, gb_len); 469 + } 470 + 471 + static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev) 472 + { 473 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 474 + 475 + info->state = ST21NFCA_ST_READY; 476 + 477 + return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE, 478 + ST21NFCA_DM_DISCONNECT, NULL, 0, NULL); 479 + } 480 + 580 481 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate, 581 482 struct nfc_target *target) 582 483 { ··· 651 512 return 0; 652 513 } 653 514 515 + static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev, 516 + u8 gate, 517 + struct nfc_target *target) 518 + { 519 + int r; 520 + struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb; 521 + 522 + if (gate == ST21NFCA_RF_READER_F_GATE) { 523 + r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE, 524 + ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb); 525 + if (r < 0) 526 + goto exit; 527 + 528 + if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) { 529 + r = -EPROTO; 530 + goto exit; 531 + } 532 + 533 + /* 534 + * - After the recepton of polling response for type F frame 535 + * at 212 or 424 Kbit/s, NFCID2 registry parameters will be 536 + * updated. 537 + * - After the reception of SEL_RES with NFCIP-1 compliant bit 538 + * set for type A frame NFCID1 will be updated 539 + */ 540 + if (nfcid2_skb->len > 0) { 541 + /* P2P in type F */ 542 + memcpy(target->sensf_res, nfcid2_skb->data, 543 + nfcid2_skb->len); 544 + target->sensf_res_len = nfcid2_skb->len; 545 + /* NFC Forum Digital Protocol Table 44 */ 546 + if (target->sensf_res[0] == 0x01 && 547 + target->sensf_res[1] == 0xfe) 548 + target->supported_protocols = 549 + NFC_PROTO_NFC_DEP_MASK; 550 + else 551 + target->supported_protocols = 552 + NFC_PROTO_FELICA_MASK; 553 + } else { 554 + /* P2P in type A */ 555 + r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE, 556 + ST21NFCA_RF_READER_F_NFCID1, 557 + &nfcid1_skb); 558 + if (r < 0) 559 + goto exit; 560 + 561 + if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) { 562 + r = -EPROTO; 563 + goto exit; 564 + } 565 + memcpy(target->sensf_res, nfcid1_skb->data, 566 + nfcid1_skb->len); 567 + target->sensf_res_len = nfcid1_skb->len; 568 + target->supported_protocols = NFC_PROTO_NFC_DEP_MASK; 569 + } 570 + target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE; 571 + } 572 + r = 1; 573 + exit: 574 + kfree_skb(nfcid2_skb); 575 + return r; 576 + } 577 + 654 578 #define ST21NFCA_CB_TYPE_READER_ISO15693 1 655 579 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb, 656 580 int err) ··· 750 548 751 549 switch (target->hci_reader_gate) { 752 550 case ST21NFCA_RF_READER_F_GATE: 551 + if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK) 552 + return st21nfca_im_send_dep_req(hdev, skb); 553 + 753 554 *skb_push(skb, 1) = 0x1a; 754 555 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate, 755 556 ST21NFCA_WR_XCHG_DATA, skb->data, ··· 781 576 } 782 577 } 783 578 579 + static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb) 580 + { 581 + return st21nfca_tm_send_dep_res(hdev, skb); 582 + } 583 + 784 584 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev, 785 585 struct nfc_target *target) 786 586 { ··· 811 601 } 812 602 } 813 603 604 + /* 605 + * Returns: 606 + * <= 0: driver handled the event, skb consumed 607 + * 1: driver does not handle the event, please do standard processing 608 + */ 609 + static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, 610 + u8 event, struct sk_buff *skb) 611 + { 612 + int r; 613 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 614 + 615 + pr_debug("hci event: %d\n", event); 616 + 617 + switch (event) { 618 + case ST21NFCA_EVT_CARD_ACTIVATED: 619 + if (gate == ST21NFCA_RF_CARD_F_GATE) 620 + info->dep_info.curr_nfc_dep_pni = 0; 621 + break; 622 + case ST21NFCA_EVT_CARD_DEACTIVATED: 623 + break; 624 + case ST21NFCA_EVT_FIELD_ON: 625 + break; 626 + case ST21NFCA_EVT_FIELD_OFF: 627 + break; 628 + case ST21NFCA_EVT_SEND_DATA: 629 + if (gate == ST21NFCA_RF_CARD_F_GATE) { 630 + r = st21nfca_tm_event_send_data(hdev, skb, gate); 631 + if (r < 0) 632 + goto exit; 633 + return 0; 634 + } else { 635 + info->dep_info.curr_nfc_dep_pni = 0; 636 + return 1; 637 + } 638 + break; 639 + default: 640 + return 1; 641 + } 642 + kfree_skb(skb); 643 + return 0; 644 + exit: 645 + return r; 646 + } 647 + 814 648 static struct nfc_hci_ops st21nfca_hci_ops = { 815 649 .open = st21nfca_hci_open, 816 650 .close = st21nfca_hci_close, ··· 863 609 .xmit = st21nfca_hci_xmit, 864 610 .start_poll = st21nfca_hci_start_poll, 865 611 .stop_poll = st21nfca_hci_stop_poll, 612 + .dep_link_up = st21nfca_hci_dep_link_up, 613 + .dep_link_down = st21nfca_hci_dep_link_down, 866 614 .target_from_gate = st21nfca_hci_target_from_gate, 615 + .complete_target_discovered = st21nfca_hci_complete_target_discovered, 867 616 .im_transceive = st21nfca_hci_im_transceive, 617 + .tm_send = st21nfca_hci_tm_send, 868 618 .check_presence = st21nfca_hci_check_presence, 619 + .event_received = st21nfca_hci_event_received, 869 620 }; 870 621 871 622 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, ··· 915 656 NFC_PROTO_FELICA_MASK | 916 657 NFC_PROTO_ISO14443_MASK | 917 658 NFC_PROTO_ISO14443_B_MASK | 918 - NFC_PROTO_ISO15693_MASK; 659 + NFC_PROTO_ISO15693_MASK | 660 + NFC_PROTO_NFC_DEP_MASK; 919 661 920 662 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks); 921 663 ··· 939 679 goto err_regdev; 940 680 941 681 *hdev = info->hdev; 682 + st21nfca_dep_init(info->hdev); 942 683 943 684 return 0; 944 685 ··· 957 696 { 958 697 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 959 698 699 + st21nfca_dep_deinit(hdev); 960 700 nfc_hci_unregister_device(hdev); 961 701 nfc_hci_free_device(hdev); 962 702 kfree(info);
+25 -1
drivers/nfc/st21nfca/st21nfca.h
··· 19 19 20 20 #include <net/nfc/hci.h> 21 21 22 + #include "st21nfca_dep.h" 23 + 22 24 #define HCI_MODE 0 23 25 24 26 /* framing in HCI mode */ ··· 75 73 data_exchange_cb_t async_cb; 76 74 void *async_cb_context; 77 75 78 - } __packed; 76 + struct st21nfca_dep_info dep_info; 77 + }; 79 78 80 79 /* Reader RF commands */ 81 80 #define ST21NFCA_WR_XCHG_DATA 0x10 ··· 86 83 #define ST21NFCA_RF_READER_F_DATARATE_106 0x01 87 84 #define ST21NFCA_RF_READER_F_DATARATE_212 0x02 88 85 #define ST21NFCA_RF_READER_F_DATARATE_424 0x04 86 + #define ST21NFCA_RF_READER_F_POL_REQ 0x02 87 + #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000 88 + #define ST21NFCA_RF_READER_F_NFCID2 0x03 89 + #define ST21NFCA_RF_READER_F_NFCID1 0x04 90 + #define ST21NFCA_RF_READER_F_SENS_RES 0x05 91 + 92 + #define ST21NFCA_RF_CARD_F_GATE 0x24 93 + #define ST21NFCA_RF_CARD_F_MODE 0x01 94 + #define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04 95 + #define ST21NFCA_RF_CARD_F_NFCID1 0x05 96 + #define ST21NFCA_RF_CARD_F_SENS_RES 0x06 97 + #define ST21NFCA_RF_CARD_F_SEL_RES 0x07 98 + #define ST21NFCA_RF_CARD_F_DATARATE 0x08 99 + #define ST21NFCA_RF_CARD_F_DATARATE_106 0x00 100 + #define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01 101 + 102 + #define ST21NFCA_EVT_SEND_DATA 0x10 103 + #define ST21NFCA_EVT_FIELD_ON 0x11 104 + #define ST21NFCA_EVT_CARD_DEACTIVATED 0x12 105 + #define ST21NFCA_EVT_CARD_ACTIVATED 0x13 106 + #define ST21NFCA_EVT_FIELD_OFF 0x14 89 107 90 108 #endif /* __LOCAL_ST21NFCA_H_ */
+661
drivers/nfc/st21nfca/st21nfca_dep.c
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms and conditions of the GNU General Public License, 6 + * version 2, as published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program; if not, see <http://www.gnu.org/licenses/>. 15 + */ 16 + 17 + #include <net/nfc/hci.h> 18 + 19 + #include "st21nfca.h" 20 + #include "st21nfca_dep.h" 21 + 22 + #define ST21NFCA_NFCIP1_INITIATOR 0x00 23 + #define ST21NFCA_NFCIP1_REQ 0xd4 24 + #define ST21NFCA_NFCIP1_RES 0xd5 25 + #define ST21NFCA_NFCIP1_ATR_REQ 0x00 26 + #define ST21NFCA_NFCIP1_ATR_RES 0x01 27 + #define ST21NFCA_NFCIP1_PSL_REQ 0x04 28 + #define ST21NFCA_NFCIP1_PSL_RES 0x05 29 + #define ST21NFCA_NFCIP1_DEP_REQ 0x06 30 + #define ST21NFCA_NFCIP1_DEP_RES 0x07 31 + 32 + #define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03) 33 + #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0) 34 + #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \ 35 + ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT) 36 + #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04) 37 + #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08) 38 + #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10 39 + 40 + #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \ 41 + ((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT) 42 + 43 + #define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00 44 + #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40 45 + #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80 46 + 47 + #define ST21NFCA_ATR_REQ_MIN_SIZE 17 48 + #define ST21NFCA_ATR_REQ_MAX_SIZE 65 49 + #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30 50 + #define ST21NFCA_GB_BIT 0x02 51 + 52 + #define ST21NFCA_EVT_CARD_F_BITRATE 0x16 53 + #define ST21NFCA_EVT_READER_F_BITRATE 0x13 54 + #define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38) 55 + #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07) 56 + #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4) 57 + #define ST21NFCA_CARD_BITRATE_212 0x01 58 + #define ST21NFCA_CARD_BITRATE_424 0x02 59 + 60 + #define ST21NFCA_DEFAULT_TIMEOUT 0x0a 61 + 62 + 63 + #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \ 64 + __LINE__, req) 65 + 66 + struct st21nfca_atr_req { 67 + u8 length; 68 + u8 cmd0; 69 + u8 cmd1; 70 + u8 nfcid3[NFC_NFCID3_MAXSIZE]; 71 + u8 did; 72 + u8 bsi; 73 + u8 bri; 74 + u8 ppi; 75 + u8 gbi[0]; 76 + } __packed; 77 + 78 + struct st21nfca_atr_res { 79 + u8 length; 80 + u8 cmd0; 81 + u8 cmd1; 82 + u8 nfcid3[NFC_NFCID3_MAXSIZE]; 83 + u8 did; 84 + u8 bsi; 85 + u8 bri; 86 + u8 to; 87 + u8 ppi; 88 + u8 gbi[0]; 89 + } __packed; 90 + 91 + struct st21nfca_psl_req { 92 + u8 length; 93 + u8 cmd0; 94 + u8 cmd1; 95 + u8 did; 96 + u8 brs; 97 + u8 fsl; 98 + } __packed; 99 + 100 + struct st21nfca_psl_res { 101 + u8 length; 102 + u8 cmd0; 103 + u8 cmd1; 104 + u8 did; 105 + } __packed; 106 + 107 + struct st21nfca_dep_req_res { 108 + u8 length; 109 + u8 cmd0; 110 + u8 cmd1; 111 + u8 pfb; 112 + u8 did; 113 + u8 nad; 114 + } __packed; 115 + 116 + static void st21nfca_tx_work(struct work_struct *work) 117 + { 118 + struct st21nfca_hci_info *info = container_of(work, 119 + struct st21nfca_hci_info, 120 + dep_info.tx_work); 121 + 122 + struct nfc_dev *dev; 123 + struct sk_buff *skb; 124 + if (info) { 125 + dev = info->hdev->ndev; 126 + skb = info->dep_info.tx_pending; 127 + 128 + device_lock(&dev->dev); 129 + 130 + nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE, 131 + ST21NFCA_WR_XCHG_DATA, 132 + skb->data, skb->len, 133 + info->async_cb, info); 134 + device_unlock(&dev->dev); 135 + kfree_skb(skb); 136 + } 137 + } 138 + 139 + static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info, 140 + struct sk_buff *skb) 141 + { 142 + info->dep_info.tx_pending = skb; 143 + schedule_work(&info->dep_info.tx_work); 144 + } 145 + 146 + static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev, 147 + struct st21nfca_atr_req *atr_req) 148 + { 149 + struct st21nfca_atr_res *atr_res; 150 + struct sk_buff *skb; 151 + size_t gb_len; 152 + int r; 153 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 154 + 155 + gb_len = atr_req->length - sizeof(struct st21nfca_atr_req); 156 + skb = alloc_skb(atr_req->length + 1, GFP_KERNEL); 157 + if (!skb) 158 + return -ENOMEM; 159 + 160 + skb_put(skb, sizeof(struct st21nfca_atr_res)); 161 + 162 + atr_res = (struct st21nfca_atr_res *)skb->data; 163 + memset(atr_res, 0, sizeof(struct st21nfca_atr_res)); 164 + 165 + atr_res->length = atr_req->length + 1; 166 + atr_res->cmd0 = ST21NFCA_NFCIP1_RES; 167 + atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES; 168 + 169 + memcpy(atr_res->nfcid3, atr_req->nfcid3, 6); 170 + atr_res->bsi = 0x00; 171 + atr_res->bri = 0x00; 172 + atr_res->to = ST21NFCA_DEFAULT_TIMEOUT; 173 + atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B; 174 + 175 + if (gb_len) { 176 + skb_put(skb, gb_len); 177 + 178 + atr_res->ppi |= ST21NFCA_GB_BIT; 179 + memcpy(atr_res->gbi, atr_req->gbi, gb_len); 180 + r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi, 181 + gb_len); 182 + if (r < 0) 183 + return r; 184 + } 185 + 186 + info->dep_info.curr_nfc_dep_pni = 0; 187 + 188 + return nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE, 189 + ST21NFCA_EVT_SEND_DATA, skb->data, skb->len); 190 + } 191 + 192 + static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev, 193 + struct sk_buff *skb) 194 + { 195 + struct st21nfca_atr_req *atr_req; 196 + size_t gb_len; 197 + int r; 198 + 199 + skb_trim(skb, skb->len - 1); 200 + if (IS_ERR(skb)) { 201 + r = PTR_ERR(skb); 202 + goto exit; 203 + } 204 + 205 + if (!skb->len) { 206 + r = -EIO; 207 + goto exit; 208 + } 209 + 210 + if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) { 211 + r = -EPROTO; 212 + goto exit; 213 + } 214 + 215 + atr_req = (struct st21nfca_atr_req *)skb->data; 216 + 217 + r = st21nfca_tm_send_atr_res(hdev, atr_req); 218 + if (r) 219 + goto exit; 220 + 221 + gb_len = skb->len - sizeof(struct st21nfca_atr_req); 222 + 223 + r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK, 224 + NFC_COMM_PASSIVE, atr_req->gbi, gb_len); 225 + if (r) 226 + goto exit; 227 + 228 + r = 0; 229 + 230 + exit: 231 + return r; 232 + } 233 + 234 + static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev, 235 + struct st21nfca_psl_req *psl_req) 236 + { 237 + struct st21nfca_psl_res *psl_res; 238 + struct sk_buff *skb; 239 + u8 bitrate[2] = {0, 0}; 240 + 241 + int r; 242 + 243 + skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL); 244 + if (!skb) 245 + return -ENOMEM; 246 + skb_put(skb, sizeof(struct st21nfca_psl_res)); 247 + 248 + psl_res = (struct st21nfca_psl_res *)skb->data; 249 + 250 + psl_res->length = sizeof(struct st21nfca_psl_res); 251 + psl_res->cmd0 = ST21NFCA_NFCIP1_RES; 252 + psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES; 253 + psl_res->did = psl_req->did; 254 + 255 + r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE, 256 + ST21NFCA_EVT_SEND_DATA, skb->data, skb->len); 257 + 258 + /* 259 + * ST21NFCA only support P2P passive. 260 + * PSL_REQ BRS value != 0 has only a meaning to 261 + * change technology to type F. 262 + * We change to BITRATE 424Kbits. 263 + * In other case switch to BITRATE 106Kbits. 264 + */ 265 + if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) && 266 + ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) { 267 + bitrate[0] = ST21NFCA_CARD_BITRATE_424; 268 + bitrate[1] = ST21NFCA_CARD_BITRATE_424; 269 + } 270 + 271 + /* Send an event to change bitrate change event to card f */ 272 + return nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE, 273 + ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2); 274 + } 275 + 276 + static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev, 277 + struct sk_buff *skb) 278 + { 279 + struct st21nfca_psl_req *psl_req; 280 + int r; 281 + 282 + skb_trim(skb, skb->len - 1); 283 + if (IS_ERR(skb)) { 284 + r = PTR_ERR(skb); 285 + skb = NULL; 286 + goto exit; 287 + } 288 + 289 + if (!skb->len) { 290 + r = -EIO; 291 + goto exit; 292 + } 293 + 294 + psl_req = (struct st21nfca_psl_req *)skb->data; 295 + 296 + if (skb->len < sizeof(struct st21nfca_psl_req)) { 297 + r = -EIO; 298 + goto exit; 299 + } 300 + 301 + r = st21nfca_tm_send_psl_res(hdev, psl_req); 302 + exit: 303 + return r; 304 + } 305 + 306 + int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb) 307 + { 308 + int r; 309 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 310 + 311 + *skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni; 312 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES; 313 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_RES; 314 + *skb_push(skb, 1) = skb->len; 315 + 316 + r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE, 317 + ST21NFCA_EVT_SEND_DATA, skb->data, skb->len); 318 + kfree_skb(skb); 319 + 320 + return r; 321 + } 322 + EXPORT_SYMBOL(st21nfca_tm_send_dep_res); 323 + 324 + static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev, 325 + struct sk_buff *skb) 326 + { 327 + struct st21nfca_dep_req_res *dep_req; 328 + u8 size; 329 + int r; 330 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 331 + 332 + skb_trim(skb, skb->len - 1); 333 + if (IS_ERR(skb)) { 334 + r = PTR_ERR(skb); 335 + skb = NULL; 336 + goto exit; 337 + } 338 + 339 + size = 4; 340 + 341 + dep_req = (struct st21nfca_dep_req_res *)skb->data; 342 + if (skb->len < size) { 343 + r = -EIO; 344 + goto exit; 345 + } 346 + 347 + if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb)) 348 + size++; 349 + if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb)) 350 + size++; 351 + 352 + if (skb->len < size) { 353 + r = -EIO; 354 + goto exit; 355 + } 356 + 357 + /* Receiving DEP_REQ - Decoding */ 358 + switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) { 359 + case ST21NFCA_NFC_DEP_PFB_I_PDU: 360 + info->dep_info.curr_nfc_dep_pni = 361 + ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb); 362 + break; 363 + case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU: 364 + pr_err("Received a ACK/NACK PDU\n"); 365 + break; 366 + case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU: 367 + pr_err("Received a SUPERVISOR PDU\n"); 368 + break; 369 + } 370 + 371 + if (IS_ERR(skb)) { 372 + r = PTR_ERR(skb); 373 + skb = NULL; 374 + goto exit; 375 + } 376 + 377 + skb_pull(skb, size); 378 + 379 + return nfc_tm_data_received(hdev->ndev, skb); 380 + exit: 381 + return r; 382 + } 383 + 384 + int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev, struct sk_buff *skb, 385 + u8 gate) 386 + { 387 + u8 cmd0, cmd1; 388 + int r; 389 + 390 + cmd0 = skb->data[1]; 391 + switch (cmd0) { 392 + case ST21NFCA_NFCIP1_REQ: 393 + cmd1 = skb->data[2]; 394 + switch (cmd1) { 395 + case ST21NFCA_NFCIP1_ATR_REQ: 396 + r = st21nfca_tm_recv_atr_req(hdev, skb); 397 + break; 398 + case ST21NFCA_NFCIP1_PSL_REQ: 399 + r = st21nfca_tm_recv_psl_req(hdev, skb); 400 + break; 401 + case ST21NFCA_NFCIP1_DEP_REQ: 402 + r = st21nfca_tm_recv_dep_req(hdev, skb); 403 + break; 404 + default: 405 + return 1; 406 + } 407 + default: 408 + return 1; 409 + } 410 + return r; 411 + } 412 + EXPORT_SYMBOL(st21nfca_tm_event_send_data); 413 + 414 + static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi, 415 + u8 bri, u8 lri) 416 + { 417 + struct sk_buff *skb; 418 + struct st21nfca_psl_req *psl_req; 419 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 420 + 421 + skb = 422 + alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL); 423 + if (!skb) 424 + return; 425 + skb_reserve(skb, 1); 426 + 427 + skb_put(skb, sizeof(struct st21nfca_psl_req)); 428 + psl_req = (struct st21nfca_psl_req *) skb->data; 429 + 430 + psl_req->length = sizeof(struct st21nfca_psl_req); 431 + psl_req->cmd0 = ST21NFCA_NFCIP1_REQ; 432 + psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ; 433 + psl_req->did = did; 434 + psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03); 435 + psl_req->fsl = lri; 436 + 437 + *skb_push(skb, 1) = info->dep_info.to | 0x10; 438 + 439 + st21nfca_im_send_pdu(info, skb); 440 + 441 + kfree_skb(skb); 442 + } 443 + 444 + #define ST21NFCA_CB_TYPE_READER_F 1 445 + static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb, 446 + int err) 447 + { 448 + struct st21nfca_hci_info *info = context; 449 + struct st21nfca_atr_res *atr_res; 450 + int r; 451 + 452 + if (err != 0) 453 + return; 454 + 455 + if (IS_ERR(skb)) 456 + return; 457 + 458 + switch (info->async_cb_type) { 459 + case ST21NFCA_CB_TYPE_READER_F: 460 + skb_trim(skb, skb->len - 1); 461 + atr_res = (struct st21nfca_atr_res *)skb->data; 462 + r = nfc_set_remote_general_bytes(info->hdev->ndev, 463 + atr_res->gbi, 464 + skb->len - sizeof(struct st21nfca_atr_res)); 465 + if (r < 0) 466 + return; 467 + 468 + if (atr_res->to >= 0x0e) 469 + info->dep_info.to = 0x0e; 470 + else 471 + info->dep_info.to = atr_res->to + 1; 472 + 473 + info->dep_info.to |= 0x10; 474 + 475 + r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx, 476 + NFC_COMM_PASSIVE, NFC_RF_INITIATOR); 477 + if (r < 0) 478 + return; 479 + 480 + info->dep_info.curr_nfc_dep_pni = 0; 481 + if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri) 482 + st21nfca_im_send_psl_req(info->hdev, atr_res->did, 483 + atr_res->bsi, atr_res->bri, 484 + ST21NFCA_PP2LRI(atr_res->ppi)); 485 + break; 486 + default: 487 + if (err == 0) 488 + kfree_skb(skb); 489 + break; 490 + } 491 + } 492 + 493 + int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len) 494 + { 495 + struct sk_buff *skb; 496 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 497 + struct st21nfca_atr_req *atr_req; 498 + struct nfc_target *target; 499 + uint size; 500 + 501 + info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT; 502 + size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len; 503 + if (size > ST21NFCA_ATR_REQ_MAX_SIZE) { 504 + PROTOCOL_ERR("14.6.1.1"); 505 + return -EINVAL; 506 + } 507 + 508 + skb = 509 + alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL); 510 + if (!skb) 511 + return -ENOMEM; 512 + 513 + skb_reserve(skb, 1); 514 + 515 + skb_put(skb, sizeof(struct st21nfca_atr_req)); 516 + 517 + atr_req = (struct st21nfca_atr_req *)skb->data; 518 + memset(atr_req, 0, sizeof(struct st21nfca_atr_req)); 519 + 520 + atr_req->cmd0 = ST21NFCA_NFCIP1_REQ; 521 + atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ; 522 + memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE); 523 + target = hdev->ndev->targets; 524 + 525 + if (target->sensf_res) 526 + memcpy(atr_req->nfcid3, target->sensf_res, 527 + target->sensf_res_len); 528 + else 529 + get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE); 530 + 531 + atr_req->did = 0x0; 532 + 533 + atr_req->bsi = 0x00; 534 + atr_req->bri = 0x00; 535 + atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B; 536 + if (gb_len) { 537 + atr_req->ppi |= ST21NFCA_GB_BIT; 538 + memcpy(skb_put(skb, gb_len), gb, gb_len); 539 + } 540 + atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len; 541 + 542 + *skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */ 543 + 544 + info->async_cb_type = ST21NFCA_CB_TYPE_READER_F; 545 + info->async_cb_context = info; 546 + info->async_cb = st21nfca_im_recv_atr_res_cb; 547 + info->dep_info.bri = atr_req->bri; 548 + info->dep_info.bsi = atr_req->bsi; 549 + info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi); 550 + 551 + return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE, 552 + ST21NFCA_WR_XCHG_DATA, skb->data, 553 + skb->len, info->async_cb, info); 554 + } 555 + EXPORT_SYMBOL(st21nfca_im_send_atr_req); 556 + 557 + static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb, 558 + int err) 559 + { 560 + struct st21nfca_hci_info *info = context; 561 + struct st21nfca_dep_req_res *dep_res; 562 + 563 + int size; 564 + 565 + if (err != 0) 566 + return; 567 + 568 + if (IS_ERR(skb)) 569 + return; 570 + 571 + switch (info->async_cb_type) { 572 + case ST21NFCA_CB_TYPE_READER_F: 573 + dep_res = (struct st21nfca_dep_req_res *)skb->data; 574 + 575 + size = 3; 576 + if (skb->len < size) 577 + goto exit; 578 + 579 + if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb)) 580 + size++; 581 + if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb)) 582 + size++; 583 + 584 + if (skb->len < size) 585 + goto exit; 586 + 587 + skb_trim(skb, skb->len - 1); 588 + 589 + /* Receiving DEP_REQ - Decoding */ 590 + switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) { 591 + case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU: 592 + pr_err("Received a ACK/NACK PDU\n"); 593 + case ST21NFCA_NFC_DEP_PFB_I_PDU: 594 + info->dep_info.curr_nfc_dep_pni = 595 + ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1); 596 + size++; 597 + skb_pull(skb, size); 598 + nfc_tm_data_received(info->hdev->ndev, skb); 599 + break; 600 + case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU: 601 + pr_err("Received a SUPERVISOR PDU\n"); 602 + skb_pull(skb, size); 603 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ; 604 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ; 605 + *skb_push(skb, 1) = skb->len; 606 + *skb_push(skb, 1) = info->dep_info.to | 0x10; 607 + 608 + st21nfca_im_send_pdu(info, skb); 609 + break; 610 + } 611 + 612 + return; 613 + default: 614 + break; 615 + } 616 + 617 + exit: 618 + if (err == 0) 619 + kfree_skb(skb); 620 + } 621 + 622 + int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb) 623 + { 624 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 625 + 626 + info->async_cb_type = ST21NFCA_CB_TYPE_READER_F; 627 + info->async_cb_context = info; 628 + info->async_cb = st21nfca_im_recv_dep_res_cb; 629 + 630 + *skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni; 631 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ; 632 + *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ; 633 + *skb_push(skb, 1) = skb->len; 634 + 635 + *skb_push(skb, 1) = info->dep_info.to | 0x10; 636 + 637 + return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE, 638 + ST21NFCA_WR_XCHG_DATA, 639 + skb->data, skb->len, 640 + info->async_cb, info); 641 + } 642 + EXPORT_SYMBOL(st21nfca_im_send_dep_req); 643 + 644 + void st21nfca_dep_init(struct nfc_hci_dev *hdev) 645 + { 646 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 647 + 648 + INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work); 649 + info->dep_info.curr_nfc_dep_pni = 0; 650 + info->dep_info.idx = 0; 651 + info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT; 652 + } 653 + EXPORT_SYMBOL(st21nfca_dep_init); 654 + 655 + void st21nfca_dep_deinit(struct nfc_hci_dev *hdev) 656 + { 657 + struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev); 658 + 659 + cancel_work_sync(&info->dep_info.tx_work); 660 + } 661 + EXPORT_SYMBOL(st21nfca_dep_deinit);
+43
drivers/nfc/st21nfca/st21nfca_dep.h
··· 1 + /* 2 + * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or modify it 5 + * under the terms and conditions of the GNU General Public License, 6 + * version 2, as published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program; if not, see <http://www.gnu.org/licenses/>. 15 + */ 16 + 17 + #ifndef __ST21NFCA_DEP_H 18 + #define __ST21NFCA_DEP_H 19 + 20 + #include <linux/skbuff.h> 21 + #include <linux/workqueue.h> 22 + 23 + struct st21nfca_dep_info { 24 + struct sk_buff *tx_pending; 25 + struct work_struct tx_work; 26 + u8 curr_nfc_dep_pni; 27 + u32 idx; 28 + u8 to; 29 + u8 did; 30 + u8 bsi; 31 + u8 bri; 32 + u8 lri; 33 + } __packed; 34 + 35 + int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev, struct sk_buff *skb, 36 + u8 gate); 37 + int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb); 38 + 39 + int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len); 40 + int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb); 41 + void st21nfca_dep_init(struct nfc_hci_dev *hdev); 42 + void st21nfca_dep_deinit(struct nfc_hci_dev *hdev); 43 + #endif /* __ST21NFCA_DEP_H */