aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn W. Linville <linville@tuxdriver.com>2014-03-17 13:16:50 -0400
committerJohn W. Linville <linville@tuxdriver.com>2014-03-17 13:16:50 -0400
commit20d83f2464c0d1e27b24d654aa5495dcc44759c0 (patch)
treea6c36d9e48920f6d81b7dac9ad6b1f99dfdaf592
parent5f667642f4b290b04d88d5ca926fba81fed6180d (diff)
parent29e27dd86b5c4f8e6feb62d7b6a8491539ff1ef1 (diff)
Merge tag 'nfc-next-3.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/nfc-next
Samuel Ortiz <sameo@linux.intel.com> says: "NFC: 3.15: First pull request This is the NFC pull request for 3.15. With this one we have: - Support for ISO 15693 a.k.a. NFC vicinity a.k.a. Type 5 tags. ISO 15693 are long range (1 - 2 meters) vicinity tags/cards. The kernel now supports those through the NFC netlink and digital APIs. - Support for TI's trf7970a chipset. This chipset relies on the NFC digital layer and the driver currently supports type 2, 4A and 5 tags. - Support for NXP's pn544 secure firmare download. The pn544 C3 chipsets relies on a different firmware download protocal than the C2 one. We now support both and use the right one depending on the version we detect at runtime. - Support for 4A tags from the NFC digital layer. - A bunch of cleanups and minor fixes from Axel Lin and Thierry Escande." Signed-off-by: John W. Linville <linville@tuxdriver.com>
-rw-r--r--Documentation/devicetree/bindings/net/nfc/trf7970a.txt34
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/nfc/Kconfig12
-rw-r--r--drivers/nfc/Makefile1
-rw-r--r--drivers/nfc/pn533.c28
-rw-r--r--drivers/nfc/pn544/i2c.c194
-rw-r--r--drivers/nfc/pn544/pn544.c2
-rw-r--r--drivers/nfc/pn544/pn544.h3
-rw-r--r--drivers/nfc/port100.c25
-rw-r--r--drivers/nfc/trf7970a.c1370
-rw-r--r--include/net/nfc/digital.h7
-rw-r--r--include/net/nfc/nfc.h3
-rw-r--r--include/uapi/linux/nfc.h9
-rw-r--r--net/nfc/core.c10
-rw-r--r--net/nfc/digital.h6
-rw-r--r--net/nfc/digital_core.c67
-rw-r--r--net/nfc/digital_technology.c247
-rw-r--r--net/nfc/hci/llc.c4
-rw-r--r--net/nfc/llcp_core.c16
-rw-r--r--net/nfc/nci/core.c3
-rw-r--r--net/nfc/nci/spi.c3
-rw-r--r--net/nfc/netlink.c8
22 files changed, 1985 insertions, 68 deletions
diff --git a/Documentation/devicetree/bindings/net/nfc/trf7970a.txt b/Documentation/devicetree/bindings/net/nfc/trf7970a.txt
new file mode 100644
index 000000000000..8dd3ef7bc56b
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/nfc/trf7970a.txt
@@ -0,0 +1,34 @@
1* Texas Instruments TRF7970A RFID/NFC/15693 Transceiver
2
3Required properties:
4- compatible: Should be "ti,trf7970a".
5- spi-max-frequency: Maximum SPI frequency (<= 2000000).
6- interrupt-parent: phandle of parent interrupt handler.
7- interrupts: A single interrupt specifier.
8- ti,enable-gpios: Two GPIO entries used for 'EN' and 'EN2' pins on the
9 TRF7970A.
10- vin-supply: Regulator for supply voltage to VIN pin
11
12Optional SoC Specific Properties:
13- pinctrl-names: Contains only one value - "default".
14- pintctrl-0: Specifies the pin control groups used for this controller.
15
16Example (for ARM-based BeagleBone with TRF7970A on SPI1):
17
18&spi1 {
19 status = "okay";
20
21 nfc@0 {
22 compatible = "ti,trf7970a";
23 reg = <0>;
24 pinctrl-names = "default";
25 pinctrl-0 = <&trf7970a_default>;
26 spi-max-frequency = <2000000>;
27 interrupt-parent = <&gpio2>;
28 interrupts = <14 0>;
29 ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>,
30 <&gpio2 5 GPIO_ACTIVE_LOW>;
31 vin-supply = <&ldo3_reg>;
32 status = "okay";
33 };
34};
diff --git a/MAINTAINERS b/MAINTAINERS
index b2cf5cfb4d29..ec12265ac67b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6067,6 +6067,7 @@ F: include/net/nfc/
6067F: include/uapi/linux/nfc.h 6067F: include/uapi/linux/nfc.h
6068F: drivers/nfc/ 6068F: drivers/nfc/
6069F: include/linux/platform_data/pn544.h 6069F: include/linux/platform_data/pn544.h
6070F: Documentation/devicetree/bindings/net/nfc/
6070 6071
6071NFS, SUNRPC, AND LOCKD CLIENTS 6072NFS, SUNRPC, AND LOCKD CLIENTS
6072M: Trond Myklebust <trond.myklebust@primarydata.com> 6073M: Trond Myklebust <trond.myklebust@primarydata.com>
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index fe20e1cc0545..65d4ca19d132 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -26,6 +26,18 @@ config NFC_WILINK
26 Say Y here to compile support for Texas Instrument's NFC WiLink driver 26 Say Y here to compile support for Texas Instrument's NFC WiLink driver
27 into the kernel or say M to compile it as module. 27 into the kernel or say M to compile it as module.
28 28
29config NFC_TRF7970A
30 tristate "Texas Instruments TRF7970a NFC driver"
31 depends on SPI && NFC_DIGITAL
32 help
33 This option enables the NFC driver for Texas Instruments' TRF7970a
34 device. Such device supports 5 different protocols: ISO14443A,
35 ISO14443B, FeLiCa, ISO15693 and ISO18000-3.
36
37 Say Y here to compile support for TRF7970a into the kernel or
38 say M to compile it as a module. The module will be called
39 trf7970a.ko.
40
29config NFC_MEI_PHY 41config NFC_MEI_PHY
30 tristate "MEI bus NFC device support" 42 tristate "MEI bus NFC device support"
31 depends on INTEL_MEI && NFC_HCI 43 depends on INTEL_MEI && NFC_HCI
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index 56ab822ba03d..ae42a3fa60c9 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -10,5 +10,6 @@ obj-$(CONFIG_NFC_MEI_PHY) += mei_phy.o
10obj-$(CONFIG_NFC_SIM) += nfcsim.o 10obj-$(CONFIG_NFC_SIM) += nfcsim.o
11obj-$(CONFIG_NFC_PORT100) += port100.o 11obj-$(CONFIG_NFC_PORT100) += port100.o
12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/ 12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
13 14
14ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG 15ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
diff --git a/drivers/nfc/pn533.c b/drivers/nfc/pn533.c
index cf1a87bb74f8..d46a700a9637 100644
--- a/drivers/nfc/pn533.c
+++ b/drivers/nfc/pn533.c
@@ -55,26 +55,14 @@
55 NFC_PROTO_NFC_DEP_MASK) 55 NFC_PROTO_NFC_DEP_MASK)
56 56
57static const struct usb_device_id pn533_table[] = { 57static const struct usb_device_id pn533_table[] = {
58 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 58 { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID),
59 .idVendor = PN533_VENDOR_ID, 59 .driver_info = PN533_DEVICE_STD },
60 .idProduct = PN533_PRODUCT_ID, 60 { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID),
61 .driver_info = PN533_DEVICE_STD, 61 .driver_info = PN533_DEVICE_STD },
62 }, 62 { USB_DEVICE(SONY_VENDOR_ID, PASORI_PRODUCT_ID),
63 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 63 .driver_info = PN533_DEVICE_PASORI },
64 .idVendor = SCM_VENDOR_ID, 64 { USB_DEVICE(ACS_VENDOR_ID, ACR122U_PRODUCT_ID),
65 .idProduct = SCL3711_PRODUCT_ID, 65 .driver_info = PN533_DEVICE_ACR122U },
66 .driver_info = PN533_DEVICE_STD,
67 },
68 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
69 .idVendor = SONY_VENDOR_ID,
70 .idProduct = PASORI_PRODUCT_ID,
71 .driver_info = PN533_DEVICE_PASORI,
72 },
73 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
74 .idVendor = ACS_VENDOR_ID,
75 .idProduct = ACR122U_PRODUCT_ID,
76 .driver_info = PN533_DEVICE_ACR122U,
77 },
78 { } 66 { }
79}; 67};
80MODULE_DEVICE_TABLE(usb, pn533_table); 68MODULE_DEVICE_TABLE(usb, pn533_table);
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c
index d6185ff2f87b..f2acd85be86e 100644
--- a/drivers/nfc/pn544/i2c.c
+++ b/drivers/nfc/pn544/i2c.c
@@ -58,8 +58,19 @@ MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
58 58
59#define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c" 59#define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
60 60
61/*
62 * Exposed through the 4 most significant bytes
63 * from the HCI SW_VERSION first byte, a.k.a.
64 * SW RomLib.
65 */
66#define PN544_HW_VARIANT_C2 0xa
67#define PN544_HW_VARIANT_C3 0xb
68
69#define PN544_FW_CMD_RESET 0x01
61#define PN544_FW_CMD_WRITE 0x08 70#define PN544_FW_CMD_WRITE 0x08
62#define PN544_FW_CMD_CHECK 0x06 71#define PN544_FW_CMD_CHECK 0x06
72#define PN544_FW_CMD_SECURE_WRITE 0x0C
73#define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
63 74
64struct pn544_i2c_fw_frame_write { 75struct pn544_i2c_fw_frame_write {
65 u8 cmd; 76 u8 cmd;
@@ -88,13 +99,31 @@ struct pn544_i2c_fw_blob {
88 u8 data[]; 99 u8 data[];
89}; 100};
90 101
102struct pn544_i2c_fw_secure_frame {
103 u8 cmd;
104 u16 be_datalen;
105 u8 data[];
106} __packed;
107
108struct pn544_i2c_fw_secure_blob {
109 u64 header;
110 u8 data[];
111};
112
91#define PN544_FW_CMD_RESULT_TIMEOUT 0x01 113#define PN544_FW_CMD_RESULT_TIMEOUT 0x01
92#define PN544_FW_CMD_RESULT_BAD_CRC 0x02 114#define PN544_FW_CMD_RESULT_BAD_CRC 0x02
93#define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08 115#define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
94#define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B 116#define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
95#define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11 117#define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
118#define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
96#define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18 119#define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
120#define PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR 0x19
121#define PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR 0x1D
122#define PN544_FW_CMD_RESULT_MEMORY_ERROR 0x20
123#define PN544_FW_CMD_RESULT_CHUNK_OK 0x21
97#define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74 124#define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
125#define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
126#define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
98 127
99#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 128#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
100 129
@@ -104,11 +133,17 @@ struct pn544_i2c_fw_blob {
104#define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\ 133#define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
105 PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\ 134 PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
106 PN544_FW_WRITE_BUFFER_MAX_LEN) 135 PN544_FW_WRITE_BUFFER_MAX_LEN)
136#define PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN 3
137#define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
138 PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN)
139#define PN544_FW_SECURE_FRAME_HEADER_LEN 3
140#define PN544_FW_SECURE_BLOB_HEADER_LEN 8
107 141
108#define FW_WORK_STATE_IDLE 1 142#define FW_WORK_STATE_IDLE 1
109#define FW_WORK_STATE_START 2 143#define FW_WORK_STATE_START 2
110#define FW_WORK_STATE_WAIT_WRITE_ANSWER 3 144#define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
111#define FW_WORK_STATE_WAIT_CHECK_ANSWER 4 145#define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
146#define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
112 147
113struct pn544_i2c_phy { 148struct pn544_i2c_phy {
114 struct i2c_client *i2c_dev; 149 struct i2c_client *i2c_dev;
@@ -119,6 +154,8 @@ struct pn544_i2c_phy {
119 unsigned int gpio_fw; 154 unsigned int gpio_fw;
120 unsigned int en_polarity; 155 unsigned int en_polarity;
121 156
157 u8 hw_variant;
158
122 struct work_struct fw_work; 159 struct work_struct fw_work;
123 int fw_work_state; 160 int fw_work_state;
124 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; 161 char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
@@ -127,6 +164,8 @@ struct pn544_i2c_phy {
127 size_t fw_blob_size; 164 size_t fw_blob_size;
128 const u8 *fw_blob_data; 165 const u8 *fw_blob_data;
129 size_t fw_written; 166 size_t fw_written;
167 size_t fw_size;
168
130 int fw_cmd_result; 169 int fw_cmd_result;
131 170
132 int powered; 171 int powered;
@@ -390,6 +429,8 @@ static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
390 switch (response.status) { 429 switch (response.status) {
391 case 0: 430 case 0:
392 return 0; 431 return 0;
432 case PN544_FW_CMD_RESULT_CHUNK_OK:
433 return response.status;
393 case PN544_FW_CMD_RESULT_TIMEOUT: 434 case PN544_FW_CMD_RESULT_TIMEOUT:
394 return -ETIMEDOUT; 435 return -ETIMEDOUT;
395 case PN544_FW_CMD_RESULT_BAD_CRC: 436 case PN544_FW_CMD_RESULT_BAD_CRC:
@@ -400,9 +441,20 @@ static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
400 return -EPROTO; 441 return -EPROTO;
401 case PN544_FW_CMD_RESULT_INVALID_PARAMETER: 442 case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
402 return -EINVAL; 443 return -EINVAL;
444 case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND:
445 return -ENOTSUPP;
403 case PN544_FW_CMD_RESULT_INVALID_LENGTH: 446 case PN544_FW_CMD_RESULT_INVALID_LENGTH:
404 return -EBADMSG; 447 return -EBADMSG;
448 case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR:
449 return -ENOKEY;
450 case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR:
451 return -EINVAL;
452 case PN544_FW_CMD_RESULT_MEMORY_ERROR:
453 return -ENOMEM;
454 case PN544_FW_CMD_RESULT_COMMAND_REJECTED:
455 return -EACCES;
405 case PN544_FW_CMD_RESULT_WRITE_FAILED: 456 case PN544_FW_CMD_RESULT_WRITE_FAILED:
457 case PN544_FW_CMD_RESULT_CHUNK_ERROR:
406 return -EIO; 458 return -EIO;
407 default: 459 default:
408 return -EIO; 460 return -EIO;
@@ -469,7 +521,8 @@ static struct nfc_phy_ops i2c_phy_ops = {
469 .disable = pn544_hci_i2c_disable, 521 .disable = pn544_hci_i2c_disable,
470}; 522};
471 523
472static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name) 524static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name,
525 u8 hw_variant)
473{ 526{
474 struct pn544_i2c_phy *phy = phy_id; 527 struct pn544_i2c_phy *phy = phy_id;
475 528
@@ -477,6 +530,7 @@ static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name)
477 530
478 strcpy(phy->firmware_name, firmware_name); 531 strcpy(phy->firmware_name, firmware_name);
479 532
533 phy->hw_variant = hw_variant;
480 phy->fw_work_state = FW_WORK_STATE_START; 534 phy->fw_work_state = FW_WORK_STATE_START;
481 535
482 schedule_work(&phy->fw_work); 536 schedule_work(&phy->fw_work);
@@ -598,12 +652,93 @@ static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
598 return 0; 652 return 0;
599} 653}
600 654
655static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy *phy,
656 const u8 *data, u16 datalen)
657{
658 u8 buf[PN544_FW_I2C_MAX_PAYLOAD];
659 struct pn544_i2c_fw_secure_frame *chunk;
660 int chunklen;
661 int r;
662
663 if (datalen > PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN)
664 datalen = PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN;
665
666 chunk = (struct pn544_i2c_fw_secure_frame *) buf;
667
668 chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE;
669
670 put_unaligned_be16(datalen, &chunk->be_datalen);
671
672 memcpy(chunk->data, data, datalen);
673
674 chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen;
675
676 r = i2c_master_send(phy->i2c_dev, buf, chunklen);
677
678 if (r == chunklen)
679 return datalen;
680 else if (r < 0)
681 return r;
682 else
683 return -EIO;
684
685}
686
687static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy *phy)
688{
689 struct pn544_i2c_fw_secure_frame *framep;
690 int r;
691
692 framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data;
693 if (phy->fw_written == 0)
694 phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen)
695 + PN544_FW_SECURE_FRAME_HEADER_LEN;
696
697 /* Only secure write command can be chunked*/
698 if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD &&
699 framep->cmd != PN544_FW_CMD_SECURE_WRITE)
700 return -EINVAL;
701
702 /* The firmware also have other commands, we just send them directly */
703 if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) {
704 r = i2c_master_send(phy->i2c_dev,
705 (const char *) phy->fw_blob_data, phy->fw_blob_size);
706
707 if (r == phy->fw_blob_size)
708 goto exit;
709 else if (r < 0)
710 return r;
711 else
712 return -EIO;
713 }
714
715 r = pn544_hci_i2c_fw_secure_write_frame_cmd(phy,
716 phy->fw_blob_data + phy->fw_written,
717 phy->fw_blob_size - phy->fw_written);
718 if (r < 0)
719 return r;
720
721exit:
722 phy->fw_written += r;
723 phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER;
724
725 /* SW reset command will not trig any response from PN544 */
726 if (framep->cmd == PN544_FW_CMD_RESET) {
727 pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
728 phy->fw_cmd_result = 0;
729 schedule_work(&phy->fw_work);
730 }
731
732 return 0;
733}
734
601static void pn544_hci_i2c_fw_work(struct work_struct *work) 735static void pn544_hci_i2c_fw_work(struct work_struct *work)
602{ 736{
603 struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy, 737 struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
604 fw_work); 738 fw_work);
605 int r; 739 int r;
606 struct pn544_i2c_fw_blob *blob; 740 struct pn544_i2c_fw_blob *blob;
741 struct pn544_i2c_fw_secure_blob *secure_blob;
607 742
608 switch (phy->fw_work_state) { 743 switch (phy->fw_work_state) {
609 case FW_WORK_STATE_START: 744 case FW_WORK_STATE_START:
@@ -614,13 +749,29 @@ static void pn544_hci_i2c_fw_work(struct work_struct *work)
614 if (r < 0) 749 if (r < 0)
615 goto exit_state_start; 750 goto exit_state_start;
616 751
617 blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
618 phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
619 phy->fw_blob_dest_addr = get_unaligned_be32(&blob->be_destaddr);
620 phy->fw_blob_data = blob->data;
621
622 phy->fw_written = 0; 752 phy->fw_written = 0;
623 r = pn544_hci_i2c_fw_write_chunk(phy); 753
754 switch (phy->hw_variant) {
755 case PN544_HW_VARIANT_C2:
756 blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
757 phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
758 phy->fw_blob_dest_addr = get_unaligned_be32(
759 &blob->be_destaddr);
760 phy->fw_blob_data = blob->data;
761
762 r = pn544_hci_i2c_fw_write_chunk(phy);
763 break;
764 case PN544_HW_VARIANT_C3:
765 secure_blob = (struct pn544_i2c_fw_secure_blob *)
766 phy->fw->data;
767 phy->fw_blob_data = secure_blob->data;
768 phy->fw_size = phy->fw->size;
769 r = pn544_hci_i2c_fw_secure_write_frame(phy);
770 break;
771 default:
772 r = -ENOTSUPP;
773 break;
774 }
624 775
625exit_state_start: 776exit_state_start:
626 if (r < 0) 777 if (r < 0)
@@ -672,6 +823,35 @@ exit_state_wait_check_answer:
672 pn544_hci_i2c_fw_work_complete(phy, r); 823 pn544_hci_i2c_fw_work_complete(phy, r);
673 break; 824 break;
674 825
826 case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER:
827 r = phy->fw_cmd_result;
828 if (r < 0)
829 goto exit_state_wait_secure_write_answer;
830
831 if (r == PN544_FW_CMD_RESULT_CHUNK_OK) {
832 r = pn544_hci_i2c_fw_secure_write_frame(phy);
833 goto exit_state_wait_secure_write_answer;
834 }
835
836 if (phy->fw_written == phy->fw_blob_size) {
837 secure_blob = (struct pn544_i2c_fw_secure_blob *)
838 (phy->fw_blob_data + phy->fw_blob_size);
839 phy->fw_size -= phy->fw_blob_size +
840 PN544_FW_SECURE_BLOB_HEADER_LEN;
841 if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN
842 + PN544_FW_SECURE_FRAME_HEADER_LEN) {
843 phy->fw_blob_data = secure_blob->data;
844
845 phy->fw_written = 0;
846 r = pn544_hci_i2c_fw_secure_write_frame(phy);
847 }
848 }
849
850exit_state_wait_secure_write_answer:
851 if (r < 0 || phy->fw_size == 0)
852 pn544_hci_i2c_fw_work_complete(phy, r);
853 break;
854
675 default: 855 default:
676 break; 856 break;
677 } 857 }
diff --git a/drivers/nfc/pn544/pn544.c b/drivers/nfc/pn544/pn544.c
index 3df4a109cfad..9c8051d20cea 100644
--- a/drivers/nfc/pn544/pn544.c
+++ b/drivers/nfc/pn544/pn544.c
@@ -786,7 +786,7 @@ static int pn544_hci_fw_download(struct nfc_hci_dev *hdev,
786 if (info->fw_download == NULL) 786 if (info->fw_download == NULL)
787 return -ENOTSUPP; 787 return -ENOTSUPP;
788 788
789 return info->fw_download(info->phy_id, firmware_name); 789 return info->fw_download(info->phy_id, firmware_name, hdev->sw_romlib);
790} 790}
791 791
792static int pn544_hci_discover_se(struct nfc_hci_dev *hdev) 792static int pn544_hci_discover_se(struct nfc_hci_dev *hdev)
diff --git a/drivers/nfc/pn544/pn544.h b/drivers/nfc/pn544/pn544.h
index 491bf45da358..2aa9233e8086 100644
--- a/drivers/nfc/pn544/pn544.h
+++ b/drivers/nfc/pn544/pn544.h
@@ -25,7 +25,8 @@
25#define PN544_HCI_MODE 0 25#define PN544_HCI_MODE 0
26#define PN544_FW_MODE 1 26#define PN544_FW_MODE 1
27 27
28typedef int (*fw_download_t)(void *context, const char *firmware_name); 28typedef int (*fw_download_t)(void *context, const char *firmware_name,
29 u8 hw_variant);
29 30
30int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name, 31int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
31 int phy_headroom, int phy_tailroom, int phy_payload, 32 int phy_headroom, int phy_tailroom, int phy_payload,
diff --git a/drivers/nfc/port100.c b/drivers/nfc/port100.c
index a8555f81cbba..b7a372af5eb7 100644
--- a/drivers/nfc/port100.c
+++ b/drivers/nfc/port100.c
@@ -27,7 +27,8 @@
27#define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ 27#define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
28 NFC_PROTO_MIFARE_MASK | \ 28 NFC_PROTO_MIFARE_MASK | \
29 NFC_PROTO_FELICA_MASK | \ 29 NFC_PROTO_FELICA_MASK | \
30 NFC_PROTO_NFC_DEP_MASK) 30 NFC_PROTO_NFC_DEP_MASK | \
31 NFC_PROTO_ISO14443_MASK)
31 32
32#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \ 33#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
33 NFC_DIGITAL_DRV_CAPS_TG_CRC) 34 NFC_DIGITAL_DRV_CAPS_TG_CRC)
@@ -139,6 +140,8 @@ static const struct port100_in_rf_setting in_rf_settings[] = {
139 .in_recv_set_number = 15, 140 .in_recv_set_number = 15,
140 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A, 141 .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
141 }, 142 },
143 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
144 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
142}; 145};
143 146
144/** 147/**
@@ -174,6 +177,9 @@ static const struct port100_tg_rf_setting tg_rf_settings[] = {
174 .tg_set_number = 8, 177 .tg_set_number = 8,
175 .tg_comm_type = PORT100_COMM_TYPE_TG_424F, 178 .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
176 }, 179 },
180 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
181 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
182
177}; 183};
178 184
179#define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00 185#define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
@@ -293,6 +299,10 @@ in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
293 { PORT100_IN_PROT_CHECK_CRC, 0 }, 299 { PORT100_IN_PROT_CHECK_CRC, 0 },
294 { PORT100_IN_PROT_END, 0 }, 300 { PORT100_IN_PROT_END, 0 },
295 }, 301 },
302 [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
303 /* nfc_digital_framing_nfca_standard_with_crc_a */
304 { PORT100_IN_PROT_END, 0 },
305 },
296 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = { 306 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
297 /* nfc_digital_framing_nfca_standard */ 307 /* nfc_digital_framing_nfca_standard */
298 { PORT100_IN_PROT_END, 0 }, 308 { PORT100_IN_PROT_END, 0 },
@@ -330,6 +340,10 @@ in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
330 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = { 340 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
331 { PORT100_IN_PROT_END, 0 }, 341 { PORT100_IN_PROT_END, 0 },
332 }, 342 },
343 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
344 [NFC_DIGITAL_FRAMING_LAST] = {
345 { PORT100_IN_PROT_END, 0 },
346 },
333}; 347};
334 348
335static struct port100_protocol 349static struct port100_protocol
@@ -371,6 +385,10 @@ tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
371 { PORT100_TG_PROT_RF_OFF, 1 }, 385 { PORT100_TG_PROT_RF_OFF, 1 },
372 { PORT100_TG_PROT_END, 0 }, 386 { PORT100_TG_PROT_END, 0 },
373 }, 387 },
388 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
389 [NFC_DIGITAL_FRAMING_LAST] = {
390 { PORT100_TG_PROT_END, 0 },
391 },
374}; 392};
375 393
376struct port100 { 394struct port100 {
@@ -1356,10 +1374,7 @@ static struct nfc_digital_ops port100_digital_ops = {
1356}; 1374};
1357 1375
1358static const struct usb_device_id port100_table[] = { 1376static const struct usb_device_id port100_table[] = {
1359 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 1377 { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
1360 .idVendor = SONY_VENDOR_ID,
1361 .idProduct = RCS380_PRODUCT_ID,
1362 },
1363 { } 1378 { }
1364}; 1379};
1365MODULE_DEVICE_TABLE(usb, port100_table); 1380MODULE_DEVICE_TABLE(usb, port100_table);
diff --git a/drivers/nfc/trf7970a.c b/drivers/nfc/trf7970a.c
new file mode 100644
index 000000000000..d9babe986473
--- /dev/null
+++ b/drivers/nfc/trf7970a.c
@@ -0,0 +1,1370 @@
1/*
2 * TI TRF7970a RFID/NFC Transceiver Driver
3 *
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Author: Erick Macias <emacias@ti.com>
7 * Author: Felipe Balbi <balbi@ti.com>
8 * Author: Mark A. Greer <mgreer@animalcreek.com>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 of
12 * the License as published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/netdevice.h>
18#include <linux/interrupt.h>
19#include <linux/nfc.h>
20#include <linux/skbuff.h>
21#include <linux/delay.h>
22#include <linux/gpio.h>
23#include <linux/of.h>
24#include <linux/of_gpio.h>
25#include <linux/spi/spi.h>
26#include <linux/regulator/consumer.h>
27
28#include <net/nfc/nfc.h>
29#include <net/nfc/digital.h>
30
31/* There are 3 ways the host can communicate with the trf7970a:
32 * parallel mode, SPI with Slave Select (SS) mode, and SPI without
33 * SS mode. The driver only supports the two SPI modes.
34 *
35 * The trf7970a is very timing sensitive and the VIN, EN2, and EN
36 * pins must asserted in that order and with specific delays in between.
37 * The delays used in the driver were provided by TI and have been
38 * confirmed to work with this driver.
39 *
40 * Timeouts are implemented using the delayed workqueue kernel facility.
41 * Timeouts are required so things don't hang when there is no response
42 * from the trf7970a (or tag). Using this mechanism creates a race with
43 * interrupts, however. That is, an interrupt and a timeout could occur
44 * closely enough together that one is blocked by the mutex while the other
45 * executes. When the timeout handler executes first and blocks the
46 * interrupt handler, it will eventually set the state to IDLE so the
47 * interrupt handler will check the state and exit with no harm done.
48 * When the interrupt handler executes first and blocks the timeout handler,
49 * the cancel_delayed_work() call will know that it didn't cancel the
50 * work item (i.e., timeout) and will return zero. That return code is
51 * used by the timer handler to indicate that it should ignore the timeout
52 * once its unblocked.
53 *
54 * Aborting an active command isn't as simple as it seems because the only
55 * way to abort a command that's already been sent to the tag is so turn
56 * off power to the tag. If we do that, though, we'd have to go through
57 * the entire anticollision procedure again but the digital layer doesn't
58 * support that. So, if an abort is received before trf7970a_in_send_cmd()
59 * has sent the command to the tag, it simply returns -ECANCELED. If the
60 * command has already been sent to the tag, then the driver continues
61 * normally and recieves the response data (or error) but just before
62 * sending the data upstream, it frees the rx_skb and sends -ECANCELED
63 * upstream instead. If the command failed, that error will be sent
64 * upstream.
65 *
66 * When recieving data from a tag and the interrupt status register has
67 * only the SRX bit set, it means that all of the data has been received
68 * (once what's in the fifo has been read). However, depending on timing
69 * an interrupt status with only the SRX bit set may not be recived. In
70 * those cases, the timeout mechanism is used to wait 5 ms in case more
71 * data arrives. After 5 ms, it is assumed that all of the data has been
72 * received and the accumulated rx data is sent upstream. The
73 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
74 * (i.e., it indicates that some data has been received but we're not sure
75 * if there is more coming so a timeout in this state means all data has
76 * been received and there isn't an error). The delay is 5 ms since delays
77 * over 2 ms have been observed during testing (a little extra just in case).
78 *
79 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
80 * Having only 4 bits in the FIFO won't normally generate an interrupt so
81 * driver enables the '4_bit_RX' bit of the Special Functions register 1
82 * to cause an interrupt in that case. Leaving that bit for a read command
83 * messes up the data returned so it is only enabled when the framing is
84 * 'NFC_DIGITAL_FRAMING_NFCA_T2T' and the command is not a read command.
85 * Unfortunately, that means that the driver has to peek into tx frames
86 * when the framing is 'NFC_DIGITAL_FRAMING_NFCA_T2T'. This is done by
87 * the trf7970a_per_cmd_config() routine.
88 *
89 * ISO/IEC 15693 frames specify whether to use single or double sub-carrier
90 * frequencies and whether to use low or high data rates in the flags byte
91 * of the frame. This means that the driver has to peek at all 15693 frames
92 * to determine what speed to set the communication to. In addition, write
93 * and lock commands use the OPTION flag to indicate that an EOF must be
94 * sent to the tag before it will send its response. So the driver has to
95 * examine all frames for that reason too.
96 *
97 * It is unclear how long to wait before sending the EOF. According to the
98 * Note under Table 1-1 in section 1.6 of
99 * http://www.ti.com/lit/ug/scbu011/scbu011.pdf, that wait should be at least
100 * 10 ms for TI Tag-it HF-I tags; however testing has shown that is not long
101 * enough. For this reason, the driver waits 20 ms which seems to work
102 * reliably.
103 */
104
105#define TRF7970A_SUPPORTED_PROTOCOLS \
106 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
107 NFC_PROTO_ISO15693_MASK)
108
109/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
110 * on what the current framing is, the address of the TX length byte 1
111 * register (0x1d), and the 2 byte length of the data to be transmitted.
112 * That totals 5 bytes.
113 */
114#define TRF7970A_TX_SKB_HEADROOM 5
115
116#define TRF7970A_RX_SKB_ALLOC_SIZE 256
117
118#define TRF7970A_FIFO_SIZE 128
119
120/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
121#define TRF7970A_TX_MAX (4096 - 1)
122
123#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5
124#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
125#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
126
127/* Quirks */
128/* Erratum: When reading IRQ Status register on trf7970a, we must issue a
129 * read continuous command for IRQ Status and Collision Position registers.
130 */
131#define TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA BIT(0)
132
133/* Direct commands */
134#define TRF7970A_CMD_IDLE 0x00
135#define TRF7970A_CMD_SOFT_INIT 0x03
136#define TRF7970A_CMD_RF_COLLISION 0x04
137#define TRF7970A_CMD_RF_COLLISION_RESPONSE_N 0x05
138#define TRF7970A_CMD_RF_COLLISION_RESPONSE_0 0x06
139#define TRF7970A_CMD_FIFO_RESET 0x0f
140#define TRF7970A_CMD_TRANSMIT_NO_CRC 0x10
141#define TRF7970A_CMD_TRANSMIT 0x11
142#define TRF7970A_CMD_DELAY_TRANSMIT_NO_CRC 0x12
143#define TRF7970A_CMD_DELAY_TRANSMIT 0x13
144#define TRF7970A_CMD_EOF 0x14
145#define TRF7970A_CMD_CLOSE_SLOT 0x15
146#define TRF7970A_CMD_BLOCK_RX 0x16
147#define TRF7970A_CMD_ENABLE_RX 0x17
148#define TRF7970A_CMD_TEST_EXT_RF 0x18
149#define TRF7970A_CMD_TEST_INT_RF 0x19
150#define TRF7970A_CMD_RX_GAIN_ADJUST 0x1a
151
152/* Bits determining whether its a direct command or register R/W,
153 * whether to use a continuous SPI transaction or not, and the actual
154 * direct cmd opcode or regster address.
155 */
156#define TRF7970A_CMD_BIT_CTRL BIT(7)
157#define TRF7970A_CMD_BIT_RW BIT(6)
158#define TRF7970A_CMD_BIT_CONTINUOUS BIT(5)
159#define TRF7970A_CMD_BIT_OPCODE(opcode) ((opcode) & 0x1f)
160
161/* Registers addresses */
162#define TRF7970A_CHIP_STATUS_CTRL 0x00
163#define TRF7970A_ISO_CTRL 0x01
164#define TRF7970A_ISO14443B_TX_OPTIONS 0x02
165#define TRF7970A_ISO14443A_HIGH_BITRATE_OPTIONS 0x03
166#define TRF7970A_TX_TIMER_SETTING_H_BYTE 0x04
167#define TRF7970A_TX_TIMER_SETTING_L_BYTE 0x05
168#define TRF7970A_TX_PULSE_LENGTH_CTRL 0x06
169#define TRF7970A_RX_NO_RESPONSE_WAIT 0x07
170#define TRF7970A_RX_WAIT_TIME 0x08
171#define TRF7970A_MODULATOR_SYS_CLK_CTRL 0x09
172#define TRF7970A_RX_SPECIAL_SETTINGS 0x0a
173#define TRF7970A_REG_IO_CTRL 0x0b
174#define TRF7970A_IRQ_STATUS 0x0c
175#define TRF7970A_COLLISION_IRQ_MASK 0x0d
176#define TRF7970A_COLLISION_POSITION 0x0e
177#define TRF7970A_RSSI_OSC_STATUS 0x0f
178#define TRF7970A_SPECIAL_FCN_REG1 0x10
179#define TRF7970A_SPECIAL_FCN_REG2 0x11
180#define TRF7970A_RAM1 0x12
181#define TRF7970A_RAM2 0x13
182#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS 0x14
183#define TRF7970A_NFC_LOW_FIELD_LEVEL 0x16
184#define TRF7970A_NFCID1 0x17
185#define TRF7970A_NFC_TARGET_LEVEL 0x18
186#define TRF79070A_NFC_TARGET_PROTOCOL 0x19
187#define TRF7970A_TEST_REGISTER1 0x1a
188#define TRF7970A_TEST_REGISTER2 0x1b
189#define TRF7970A_FIFO_STATUS 0x1c
190#define TRF7970A_TX_LENGTH_BYTE1 0x1d
191#define TRF7970A_TX_LENGTH_BYTE2 0x1e
192#define TRF7970A_FIFO_IO_REGISTER 0x1f
193
194/* Chip Status Control Register Bits */
195#define TRF7970A_CHIP_STATUS_VRS5_3 BIT(0)
196#define TRF7970A_CHIP_STATUS_REC_ON BIT(1)
197#define TRF7970A_CHIP_STATUS_AGC_ON BIT(2)
198#define TRF7970A_CHIP_STATUS_PM_ON BIT(3)
199#define TRF7970A_CHIP_STATUS_RF_PWR BIT(4)
200#define TRF7970A_CHIP_STATUS_RF_ON BIT(5)
201#define TRF7970A_CHIP_STATUS_DIRECT BIT(6)
202#define TRF7970A_CHIP_STATUS_STBY BIT(7)
203
204/* ISO Control Register Bits */
205#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_662 0x00
206#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_662 0x01
207#define TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648 0x02
208#define TRF7970A_ISO_CTRL_15693_SGL_1OF256_2648 0x03
209#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a 0x04
210#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_667 0x05
211#define TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669 0x06
212#define TRF7970A_ISO_CTRL_15693_DBL_1OF256_2669 0x07
213#define TRF7970A_ISO_CTRL_14443A_106 0x08
214#define TRF7970A_ISO_CTRL_14443A_212 0x09
215#define TRF7970A_ISO_CTRL_14443A_424 0x0a
216#define TRF7970A_ISO_CTRL_14443A_848 0x0b
217#define TRF7970A_ISO_CTRL_14443B_106 0x0c
218#define TRF7970A_ISO_CTRL_14443B_212 0x0d
219#define TRF7970A_ISO_CTRL_14443B_424 0x0e
220#define TRF7970A_ISO_CTRL_14443B_848 0x0f
221#define TRF7970A_ISO_CTRL_FELICA_212 0x1a
222#define TRF7970A_ISO_CTRL_FELICA_424 0x1b
223#define TRF7970A_ISO_CTRL_RFID BIT(5)
224#define TRF7970A_ISO_CTRL_DIR_MODE BIT(6)
225#define TRF7970A_ISO_CTRL_RX_CRC_N BIT(7) /* true == No CRC */
226
227#define TRF7970A_ISO_CTRL_RFID_SPEED_MASK 0x1f
228
229/* Modulator and SYS_CLK Control Register Bits */
230#define TRF7970A_MODULATOR_DEPTH(n) ((n) & 0x7)
231#define TRF7970A_MODULATOR_DEPTH_ASK10 (TRF7970A_MODULATOR_DEPTH(0))
232#define TRF7970A_MODULATOR_DEPTH_OOK (TRF7970A_MODULATOR_DEPTH(1))
233#define TRF7970A_MODULATOR_DEPTH_ASK7 (TRF7970A_MODULATOR_DEPTH(2))
234#define TRF7970A_MODULATOR_DEPTH_ASK8_5 (TRF7970A_MODULATOR_DEPTH(3))
235#define TRF7970A_MODULATOR_DEPTH_ASK13 (TRF7970A_MODULATOR_DEPTH(4))
236#define TRF7970A_MODULATOR_DEPTH_ASK16 (TRF7970A_MODULATOR_DEPTH(5))
237#define TRF7970A_MODULATOR_DEPTH_ASK22 (TRF7970A_MODULATOR_DEPTH(6))
238#define TRF7970A_MODULATOR_DEPTH_ASK30 (TRF7970A_MODULATOR_DEPTH(7))
239#define TRF7970A_MODULATOR_EN_ANA BIT(3)
240#define TRF7970A_MODULATOR_CLK(n) (((n) & 0x3) << 4)
241#define TRF7970A_MODULATOR_CLK_DISABLED (TRF7970A_MODULATOR_CLK(0))
242#define TRF7970A_MODULATOR_CLK_3_6 (TRF7970A_MODULATOR_CLK(1))
243#define TRF7970A_MODULATOR_CLK_6_13 (TRF7970A_MODULATOR_CLK(2))
244#define TRF7970A_MODULATOR_CLK_13_27 (TRF7970A_MODULATOR_CLK(3))
245#define TRF7970A_MODULATOR_EN_OOK BIT(6)
246#define TRF7970A_MODULATOR_27MHZ BIT(7)
247
248/* IRQ Status Register Bits */
249#define TRF7970A_IRQ_STATUS_NORESP BIT(0) /* ISO15693 only */
250#define TRF7970A_IRQ_STATUS_COL BIT(1)
251#define TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR BIT(2)
252#define TRF7970A_IRQ_STATUS_PARITY_ERROR BIT(3)
253#define TRF7970A_IRQ_STATUS_CRC_ERROR BIT(4)
254#define TRF7970A_IRQ_STATUS_FIFO BIT(5)
255#define TRF7970A_IRQ_STATUS_SRX BIT(6)
256#define TRF7970A_IRQ_STATUS_TX BIT(7)
257
258#define TRF7970A_IRQ_STATUS_ERROR \
259 (TRF7970A_IRQ_STATUS_COL | \
260 TRF7970A_IRQ_STATUS_FRAMING_EOF_ERROR | \
261 TRF7970A_IRQ_STATUS_PARITY_ERROR | \
262 TRF7970A_IRQ_STATUS_CRC_ERROR)
263
264#define TRF7970A_SPECIAL_FCN_REG1_COL_7_6 BIT(0)
265#define TRF7970A_SPECIAL_FCN_REG1_14_ANTICOLL BIT(1)
266#define TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX BIT(2)
267#define TRF7970A_SPECIAL_FCN_REG1_SP_DIR_MODE BIT(3)
268#define TRF7970A_SPECIAL_FCN_REG1_NEXT_SLOT_37US BIT(4)
269#define TRF7970A_SPECIAL_FCN_REG1_PAR43 BIT(5)
270
271#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_124 (0x0 << 2)
272#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_120 (0x1 << 2)
273#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_112 (0x2 << 2)
274#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 (0x3 << 2)
275#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_4 0x0
276#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_8 0x1
277#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_16 0x2
278#define TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32 0x3
279
280#define TRF7970A_FIFO_STATUS_OVERFLOW BIT(7)
281
282/* NFC (ISO/IEC 14443A) Type 2 Tag commands */
283#define NFC_T2T_CMD_READ 0x30
284
285/* ISO 15693 commands codes */
286#define ISO15693_CMD_INVENTORY 0x01
287#define ISO15693_CMD_READ_SINGLE_BLOCK 0x20
288#define ISO15693_CMD_WRITE_SINGLE_BLOCK 0x21
289#define ISO15693_CMD_LOCK_BLOCK 0x22
290#define ISO15693_CMD_READ_MULTIPLE_BLOCK 0x23
291#define ISO15693_CMD_WRITE_MULTIPLE_BLOCK 0x24
292#define ISO15693_CMD_SELECT 0x25
293#define ISO15693_CMD_RESET_TO_READY 0x26
294#define ISO15693_CMD_WRITE_AFI 0x27
295#define ISO15693_CMD_LOCK_AFI 0x28
296#define ISO15693_CMD_WRITE_DSFID 0x29
297#define ISO15693_CMD_LOCK_DSFID 0x2a
298#define ISO15693_CMD_GET_SYSTEM_INFO 0x2b
299#define ISO15693_CMD_GET_MULTIPLE_BLOCK_SECURITY_STATUS 0x2c
300
301/* ISO 15693 request and response flags */
302#define ISO15693_REQ_FLAG_SUB_CARRIER BIT(0)
303#define ISO15693_REQ_FLAG_DATA_RATE BIT(1)
304#define ISO15693_REQ_FLAG_INVENTORY BIT(2)
305#define ISO15693_REQ_FLAG_PROTOCOL_EXT BIT(3)
306#define ISO15693_REQ_FLAG_SELECT BIT(4)
307#define ISO15693_REQ_FLAG_AFI BIT(4)
308#define ISO15693_REQ_FLAG_ADDRESS BIT(5)
309#define ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
310#define ISO15693_REQ_FLAG_OPTION BIT(6)
311
312#define ISO15693_REQ_FLAG_SPEED_MASK \
313 (ISO15693_REQ_FLAG_SUB_CARRIER | ISO15693_REQ_FLAG_DATA_RATE)
314
315enum trf7970a_state {
316 TRF7970A_ST_OFF,
317 TRF7970A_ST_IDLE,
318 TRF7970A_ST_IDLE_RX_BLOCKED,
319 TRF7970A_ST_WAIT_FOR_TX_FIFO,
320 TRF7970A_ST_WAIT_FOR_RX_DATA,
321 TRF7970A_ST_WAIT_FOR_RX_DATA_CONT,
322 TRF7970A_ST_WAIT_TO_ISSUE_EOF,
323 TRF7970A_ST_MAX
324};
325
326struct trf7970a {
327 enum trf7970a_state state;
328 struct device *dev;
329 struct spi_device *spi;
330 struct regulator *regulator;
331 struct nfc_digital_dev *ddev;
332 u32 quirks;
333 bool powering_up;
334 bool aborting;
335 struct sk_buff *tx_skb;
336 struct sk_buff *rx_skb;
337 nfc_digital_cmd_complete_t cb;
338 void *cb_arg;
339 u8 iso_ctrl;
340 u8 special_fcn_reg1;
341 int technology;
342 int framing;
343 u8 tx_cmd;
344 bool issue_eof;
345 int en2_gpio;
346 int en_gpio;
347 struct mutex lock;
348 unsigned int timeout;
349 bool ignore_timeout;
350 struct delayed_work timeout_work;
351};
352
353
354static int trf7970a_cmd(struct trf7970a *trf, u8 opcode)
355{
356 u8 cmd = TRF7970A_CMD_BIT_CTRL | TRF7970A_CMD_BIT_OPCODE(opcode);
357 int ret;
358
359 dev_dbg(trf->dev, "cmd: 0x%x\n", cmd);
360
361 ret = spi_write(trf->spi, &cmd, 1);
362 if (ret)
363 dev_err(trf->dev, "%s - cmd: 0x%x, ret: %d\n", __func__, cmd,
364 ret);
365 return ret;
366}
367
368static int trf7970a_read(struct trf7970a *trf, u8 reg, u8 *val)
369{
370 u8 addr = TRF7970A_CMD_BIT_RW | reg;
371 int ret;
372
373 ret = spi_write_then_read(trf->spi, &addr, 1, val, 1);
374 if (ret)
375 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
376 ret);
377
378 dev_dbg(trf->dev, "read(0x%x): 0x%x\n", addr, *val);
379
380 return ret;
381}
382
383static int trf7970a_read_cont(struct trf7970a *trf, u8 reg,
384 u8 *buf, size_t len)
385{
386 u8 addr = reg | TRF7970A_CMD_BIT_RW | TRF7970A_CMD_BIT_CONTINUOUS;
387 int ret;
388
389 dev_dbg(trf->dev, "read_cont(0x%x, %zd)\n", addr, len);
390
391 ret = spi_write_then_read(trf->spi, &addr, 1, buf, len);
392 if (ret)
393 dev_err(trf->dev, "%s - addr: 0x%x, ret: %d\n", __func__, addr,
394 ret);
395 return ret;
396}
397
398static int trf7970a_write(struct trf7970a *trf, u8 reg, u8 val)
399{
400 u8 buf[2] = { reg, val };
401 int ret;
402
403 dev_dbg(trf->dev, "write(0x%x): 0x%x\n", reg, val);
404
405 ret = spi_write(trf->spi, buf, 2);
406 if (ret)
407 dev_err(trf->dev, "%s - write: 0x%x 0x%x, ret: %d\n", __func__,
408 buf[0], buf[1], ret);
409
410 return ret;
411}
412
413static int trf7970a_read_irqstatus(struct trf7970a *trf, u8 *status)
414{
415 int ret;
416 u8 buf[2];
417 u8 addr;
418
419 addr = TRF7970A_IRQ_STATUS | TRF7970A_CMD_BIT_RW;
420
421 if (trf->quirks & TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA) {
422 addr |= TRF7970A_CMD_BIT_CONTINUOUS;
423 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 2);
424 } else {
425 ret = spi_write_then_read(trf->spi, &addr, 1, buf, 1);
426 }
427
428 if (ret)
429 dev_err(trf->dev, "%s - irqstatus: Status read failed: %d\n",
430 __func__, ret);
431 else
432 *status = buf[0];
433
434 return ret;
435}
436
437static void trf7970a_send_upstream(struct trf7970a *trf)
438{
439 u8 rssi;
440
441 dev_kfree_skb_any(trf->tx_skb);
442 trf->tx_skb = NULL;
443
444 if (trf->rx_skb && !IS_ERR(trf->rx_skb) && !trf->aborting)
445 print_hex_dump_debug("trf7970a rx data: ", DUMP_PREFIX_NONE,
446 16, 1, trf->rx_skb->data, trf->rx_skb->len,
447 false);
448
449 /* According to the manual it is "good form" to reset the fifo and
450 * read the RSSI levels & oscillator status register here. It doesn't
451 * explain why.
452 */
453 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
454 trf7970a_read(trf, TRF7970A_RSSI_OSC_STATUS, &rssi);
455
456 trf->state = TRF7970A_ST_IDLE;
457
458 if (trf->aborting) {
459 dev_dbg(trf->dev, "Abort process complete\n");
460
461 if (!IS_ERR(trf->rx_skb)) {
462 kfree_skb(trf->rx_skb);
463 trf->rx_skb = ERR_PTR(-ECANCELED);
464 }
465
466 trf->aborting = false;
467 }
468
469 trf->cb(trf->ddev, trf->cb_arg, trf->rx_skb);
470
471 trf->rx_skb = NULL;
472}
473
474static void trf7970a_send_err_upstream(struct trf7970a *trf, int errno)
475{
476 dev_dbg(trf->dev, "Error - state: %d, errno: %d\n", trf->state, errno);
477
478 kfree_skb(trf->rx_skb);
479 trf->rx_skb = ERR_PTR(errno);
480
481 trf7970a_send_upstream(trf);
482}
483
484static int trf7970a_transmit(struct trf7970a *trf, struct sk_buff *skb,
485 unsigned int len)
486{
487 unsigned int timeout;
488 int ret;
489
490 print_hex_dump_debug("trf7970a tx data: ", DUMP_PREFIX_NONE,
491 16, 1, skb->data, len, false);
492
493 ret = spi_write(trf->spi, skb->data, len);
494 if (ret) {
495 dev_err(trf->dev, "%s - Can't send tx data: %d\n", __func__,
496 ret);
497 return ret;
498 }
499
500 skb_pull(skb, len);
501
502 if (skb->len > 0) {
503 trf->state = TRF7970A_ST_WAIT_FOR_TX_FIFO;
504 timeout = TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT;
505 } else {
506 if (trf->issue_eof) {
507 trf->state = TRF7970A_ST_WAIT_TO_ISSUE_EOF;
508 timeout = TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF;
509 } else {
510 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
511 timeout = trf->timeout;
512 }
513 }
514
515 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n", timeout,
516 trf->state);
517
518 schedule_delayed_work(&trf->timeout_work, msecs_to_jiffies(timeout));
519
520 return 0;
521}
522
523static void trf7970a_fill_fifo(struct trf7970a *trf)
524{
525 struct sk_buff *skb = trf->tx_skb;
526 unsigned int len;
527 int ret;
528 u8 fifo_bytes;
529
530 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
531 if (ret) {
532 trf7970a_send_err_upstream(trf, ret);
533 return;
534 }
535
536 dev_dbg(trf->dev, "Filling FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
537
538 if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
539 dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
540 fifo_bytes);
541 trf7970a_send_err_upstream(trf, -EIO);
542 return;
543 }
544
545 /* Calculate how much more data can be written to the fifo */
546 len = TRF7970A_FIFO_SIZE - fifo_bytes;
547 len = min(skb->len, len);
548
549 ret = trf7970a_transmit(trf, skb, len);
550 if (ret)
551 trf7970a_send_err_upstream(trf, ret);
552}
553
554static void trf7970a_drain_fifo(struct trf7970a *trf, u8 status)
555{
556 struct sk_buff *skb = trf->rx_skb;
557 int ret;
558 u8 fifo_bytes;
559
560 if (status & TRF7970A_IRQ_STATUS_ERROR) {
561 trf7970a_send_err_upstream(trf, -EIO);
562 return;
563 }
564
565 ret = trf7970a_read(trf, TRF7970A_FIFO_STATUS, &fifo_bytes);
566 if (ret) {
567 trf7970a_send_err_upstream(trf, ret);
568 return;
569 }
570
571 dev_dbg(trf->dev, "Draining FIFO - fifo_bytes: 0x%x\n", fifo_bytes);
572
573 if (!fifo_bytes)
574 goto no_rx_data;
575
576 if (fifo_bytes & TRF7970A_FIFO_STATUS_OVERFLOW) {
577 dev_err(trf->dev, "%s - fifo overflow: 0x%x\n", __func__,
578 fifo_bytes);
579 trf7970a_send_err_upstream(trf, -EIO);
580 return;
581 }
582
583 if (fifo_bytes > skb_tailroom(skb)) {
584 skb = skb_copy_expand(skb, skb_headroom(skb),
585 max_t(int, fifo_bytes,
586 TRF7970A_RX_SKB_ALLOC_SIZE),
587 GFP_KERNEL);
588 if (!skb) {
589 trf7970a_send_err_upstream(trf, -ENOMEM);
590 return;
591 }
592
593 kfree_skb(trf->rx_skb);
594 trf->rx_skb = skb;
595 }
596
597 ret = trf7970a_read_cont(trf, TRF7970A_FIFO_IO_REGISTER,
598 skb_put(skb, fifo_bytes), fifo_bytes);
599 if (ret) {
600 trf7970a_send_err_upstream(trf, ret);
601 return;
602 }
603
604 /* If received Type 2 ACK/NACK, shift right 4 bits and pass up */
605 if ((trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T) && (skb->len == 1) &&
606 (trf->special_fcn_reg1 ==
607 TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX)) {
608 skb->data[0] >>= 4;
609 status = TRF7970A_IRQ_STATUS_SRX;
610 } else {
611 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA_CONT;
612 }
613
614no_rx_data:
615 if (status == TRF7970A_IRQ_STATUS_SRX) { /* Receive complete */
616 trf7970a_send_upstream(trf);
617 return;
618 }
619
620 dev_dbg(trf->dev, "Setting timeout for %d ms\n",
621 TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT);
622
623 schedule_delayed_work(&trf->timeout_work,
624 msecs_to_jiffies(TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT));
625}
626
627static irqreturn_t trf7970a_irq(int irq, void *dev_id)
628{
629 struct trf7970a *trf = dev_id;
630 int ret;
631 u8 status;
632
633 mutex_lock(&trf->lock);
634
635 if (trf->state == TRF7970A_ST_OFF) {
636 mutex_unlock(&trf->lock);
637 return IRQ_NONE;
638 }
639
640 ret = trf7970a_read_irqstatus(trf, &status);
641 if (ret) {
642 mutex_unlock(&trf->lock);
643 return IRQ_NONE;
644 }
645
646 dev_dbg(trf->dev, "IRQ - state: %d, status: 0x%x\n", trf->state,
647 status);
648
649 if (!status) {
650 mutex_unlock(&trf->lock);
651 return IRQ_NONE;
652 }
653
654 switch (trf->state) {
655 case TRF7970A_ST_IDLE:
656 case TRF7970A_ST_IDLE_RX_BLOCKED:
657 /* If getting interrupts caused by RF noise, turn off the
658 * receiver to avoid unnecessary interrupts. It will be
659 * turned back on in trf7970a_in_send_cmd() when the next
660 * command is issued.
661 */
662 if (status & TRF7970A_IRQ_STATUS_ERROR) {
663 trf7970a_cmd(trf, TRF7970A_CMD_BLOCK_RX);
664 trf->state = TRF7970A_ST_IDLE_RX_BLOCKED;
665 }
666
667 trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
668 break;
669 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
670 if (status & TRF7970A_IRQ_STATUS_TX) {
671 trf->ignore_timeout =
672 !cancel_delayed_work(&trf->timeout_work);
673 trf7970a_fill_fifo(trf);
674 } else {
675 trf7970a_send_err_upstream(trf, -EIO);
676 }
677 break;
678 case TRF7970A_ST_WAIT_FOR_RX_DATA:
679 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
680 if (status & TRF7970A_IRQ_STATUS_SRX) {
681 trf->ignore_timeout =
682 !cancel_delayed_work(&trf->timeout_work);
683 trf7970a_drain_fifo(trf, status);
684 } else if (!(status & TRF7970A_IRQ_STATUS_TX)) {
685 trf7970a_send_err_upstream(trf, -EIO);
686 }
687 break;
688 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
689 if (status != TRF7970A_IRQ_STATUS_TX)
690 trf7970a_send_err_upstream(trf, -EIO);
691 break;
692 default:
693 dev_err(trf->dev, "%s - Driver in invalid state: %d\n",
694 __func__, trf->state);
695 }
696
697 mutex_unlock(&trf->lock);
698 return IRQ_HANDLED;
699}
700
701static void trf7970a_issue_eof(struct trf7970a *trf)
702{
703 int ret;
704
705 dev_dbg(trf->dev, "Issuing EOF\n");
706
707 ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
708 if (ret)
709 trf7970a_send_err_upstream(trf, ret);
710
711 ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
712 if (ret)
713 trf7970a_send_err_upstream(trf, ret);
714
715 trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
716
717 dev_dbg(trf->dev, "Setting timeout for %d ms, state: %d\n",
718 trf->timeout, trf->state);
719
720 schedule_delayed_work(&trf->timeout_work,
721 msecs_to_jiffies(trf->timeout));
722}
723
724static void trf7970a_timeout_work_handler(struct work_struct *work)
725{
726 struct trf7970a *trf = container_of(work, struct trf7970a,
727 timeout_work.work);
728
729 dev_dbg(trf->dev, "Timeout - state: %d, ignore_timeout: %d\n",
730 trf->state, trf->ignore_timeout);
731
732 mutex_lock(&trf->lock);
733
734 if (trf->ignore_timeout)
735 trf->ignore_timeout = false;
736 else if (trf->state == TRF7970A_ST_WAIT_FOR_RX_DATA_CONT)
737 trf7970a_send_upstream(trf); /* No more rx data so send up */
738 else if (trf->state == TRF7970A_ST_WAIT_TO_ISSUE_EOF)
739 trf7970a_issue_eof(trf);
740 else
741 trf7970a_send_err_upstream(trf, -ETIMEDOUT);
742
743 mutex_unlock(&trf->lock);
744}
745
746static int trf7970a_init(struct trf7970a *trf)
747{
748 int ret;
749
750 dev_dbg(trf->dev, "Initializing device - state: %d\n", trf->state);
751
752 ret = trf7970a_cmd(trf, TRF7970A_CMD_SOFT_INIT);
753 if (ret)
754 goto err_out;
755
756 ret = trf7970a_cmd(trf, TRF7970A_CMD_IDLE);
757 if (ret)
758 goto err_out;
759
760 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
761 TRF7970A_MODULATOR_DEPTH_OOK);
762 if (ret)
763 goto err_out;
764
765 ret = trf7970a_write(trf, TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS,
766 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLH_96 |
767 TRF7970A_ADJUTABLE_FIFO_IRQ_LEVELS_WLL_32);
768 if (ret)
769 goto err_out;
770
771 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1, 0);
772 if (ret)
773 goto err_out;
774
775 trf->special_fcn_reg1 = 0;
776
777 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
778 TRF7970A_CHIP_STATUS_RF_ON |
779 TRF7970A_CHIP_STATUS_VRS5_3);
780 if (ret)
781 goto err_out;
782
783 return 0;
784
785err_out:
786 dev_dbg(trf->dev, "Couldn't init device: %d\n", ret);
787 return ret;
788}
789
790static void trf7970a_switch_rf_off(struct trf7970a *trf)
791{
792 dev_dbg(trf->dev, "Switching rf off\n");
793
794 gpio_set_value(trf->en_gpio, 0);
795 gpio_set_value(trf->en2_gpio, 0);
796
797 trf->aborting = false;
798 trf->state = TRF7970A_ST_OFF;
799}
800
801static int trf7970a_switch_rf_on(struct trf7970a *trf)
802{
803 unsigned long delay;
804 int ret;
805
806 dev_dbg(trf->dev, "Switching rf on\n");
807
808 if (trf->powering_up)
809 usleep_range(5000, 6000);
810
811 gpio_set_value(trf->en2_gpio, 1);
812 usleep_range(1000, 2000);
813 gpio_set_value(trf->en_gpio, 1);
814
815 /* The delay between enabling the trf7970a and issuing the first
816 * command is significantly longer the very first time after powering
817 * up. Make sure the longer delay is only done the first time.
818 */
819 if (trf->powering_up) {
820 delay = 20000;
821 trf->powering_up = false;
822 } else {
823 delay = 5000;
824 }
825
826 usleep_range(delay, delay + 1000);
827
828 ret = trf7970a_init(trf);
829 if (ret)
830 trf7970a_switch_rf_off(trf);
831 else
832 trf->state = TRF7970A_ST_IDLE;
833
834 return ret;
835}
836
837static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
838{
839 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
840 int ret = 0;
841
842 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
843
844 mutex_lock(&trf->lock);
845
846 if (on) {
847 switch (trf->state) {
848 case TRF7970A_ST_OFF:
849 ret = trf7970a_switch_rf_on(trf);
850 break;
851 case TRF7970A_ST_IDLE:
852 case TRF7970A_ST_IDLE_RX_BLOCKED:
853 break;
854 default:
855 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
856 __func__, trf->state, on);
857 trf7970a_switch_rf_off(trf);
858 }
859 } else {
860 switch (trf->state) {
861 case TRF7970A_ST_OFF:
862 break;
863 default:
864 dev_err(trf->dev, "%s - Invalid request: %d %d\n",
865 __func__, trf->state, on);
866 /* FALLTHROUGH */
867 case TRF7970A_ST_IDLE:
868 case TRF7970A_ST_IDLE_RX_BLOCKED:
869 trf7970a_switch_rf_off(trf);
870 }
871 }
872
873 mutex_unlock(&trf->lock);
874 return ret;
875}
876
877static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
878{
879 int ret = 0;
880
881 dev_dbg(trf->dev, "rf technology: %d\n", tech);
882
883 switch (tech) {
884 case NFC_DIGITAL_RF_TECH_106A:
885 trf->iso_ctrl = TRF7970A_ISO_CTRL_14443A_106;
886 break;
887 case NFC_DIGITAL_RF_TECH_ISO15693:
888 trf->iso_ctrl = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
889 break;
890 default:
891 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
892 return -EINVAL;
893 }
894
895 trf->technology = tech;
896
897 return ret;
898}
899
900static int trf7970a_config_framing(struct trf7970a *trf, int framing)
901{
902 dev_dbg(trf->dev, "framing: %d\n", framing);
903
904 switch (framing) {
905 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
906 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
907 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
908 trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
909 break;
910 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
911 case NFC_DIGITAL_FRAMING_NFCA_T4T:
912 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
913 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
914 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
915 trf->iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
916 break;
917 case NFC_DIGITAL_FRAMING_NFCA_T2T:
918 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
919 trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
920 break;
921 default:
922 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
923 return -EINVAL;
924 }
925
926 trf->framing = framing;
927
928 return trf7970a_write(trf, TRF7970A_ISO_CTRL, trf->iso_ctrl);
929}
930
931static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
932 int param)
933{
934 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
935 int ret = 0;
936
937 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
938
939 mutex_lock(&trf->lock);
940
941 if (trf->state == TRF7970A_ST_OFF) {
942 ret = trf7970a_switch_rf_on(trf);
943 if (ret)
944 goto err_out;
945 }
946
947 switch (type) {
948 case NFC_DIGITAL_CONFIG_RF_TECH:
949 ret = trf7970a_config_rf_tech(trf, param);
950 break;
951 case NFC_DIGITAL_CONFIG_FRAMING:
952 ret = trf7970a_config_framing(trf, param);
953 break;
954 default:
955 dev_dbg(trf->dev, "Unknown type: %d\n", type);
956 ret = -EINVAL;
957 }
958
959err_out:
960 mutex_unlock(&trf->lock);
961 return ret;
962}
963
964static int trf7970a_is_iso15693_write_or_lock(u8 cmd)
965{
966 switch (cmd) {
967 case ISO15693_CMD_WRITE_SINGLE_BLOCK:
968 case ISO15693_CMD_LOCK_BLOCK:
969 case ISO15693_CMD_WRITE_MULTIPLE_BLOCK:
970 case ISO15693_CMD_WRITE_AFI:
971 case ISO15693_CMD_LOCK_AFI:
972 case ISO15693_CMD_WRITE_DSFID:
973 case ISO15693_CMD_LOCK_DSFID:
974 return 1;
975 break;
976 default:
977 return 0;
978 }
979}
980
981static int trf7970a_per_cmd_config(struct trf7970a *trf, struct sk_buff *skb)
982{
983 u8 *req = skb->data;
984 u8 special_fcn_reg1, iso_ctrl;
985 int ret;
986
987 trf->issue_eof = false;
988
989 /* When issuing Type 2 read command, make sure the '4_bit_RX' bit in
990 * special functions register 1 is cleared; otherwise, its a write or
991 * sector select command and '4_bit_RX' must be set.
992 *
993 * When issuing an ISO 15693 command, inspect the flags byte to see
994 * what speed to use. Also, remember if the OPTION flag is set on
995 * a Type 5 write or lock command so the driver will know that it
996 * has to send an EOF in order to get a response.
997 */
998 if ((trf->technology == NFC_DIGITAL_RF_TECH_106A) &&
999 (trf->framing == NFC_DIGITAL_FRAMING_NFCA_T2T)) {
1000 if (req[0] == NFC_T2T_CMD_READ)
1001 special_fcn_reg1 = 0;
1002 else
1003 special_fcn_reg1 = TRF7970A_SPECIAL_FCN_REG1_4_BIT_RX;
1004
1005 if (special_fcn_reg1 != trf->special_fcn_reg1) {
1006 ret = trf7970a_write(trf, TRF7970A_SPECIAL_FCN_REG1,
1007 special_fcn_reg1);
1008 if (ret)
1009 return ret;
1010
1011 trf->special_fcn_reg1 = special_fcn_reg1;
1012 }
1013 } else if (trf->technology == NFC_DIGITAL_RF_TECH_ISO15693) {
1014 iso_ctrl = trf->iso_ctrl & ~TRF7970A_ISO_CTRL_RFID_SPEED_MASK;
1015
1016 switch (req[0] & ISO15693_REQ_FLAG_SPEED_MASK) {
1017 case 0x00:
1018 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_662;
1019 break;
1020 case ISO15693_REQ_FLAG_SUB_CARRIER:
1021 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_667a;
1022 break;
1023 case ISO15693_REQ_FLAG_DATA_RATE:
1024 iso_ctrl |= TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
1025 break;
1026 case (ISO15693_REQ_FLAG_SUB_CARRIER |
1027 ISO15693_REQ_FLAG_DATA_RATE):
1028 iso_ctrl |= TRF7970A_ISO_CTRL_15693_DBL_1OF4_2669;
1029 break;
1030 }
1031
1032 if (iso_ctrl != trf->iso_ctrl) {
1033 ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
1034 if (ret)
1035 return ret;
1036
1037 trf->iso_ctrl = iso_ctrl;
1038 }
1039
1040 if ((trf->framing == NFC_DIGITAL_FRAMING_ISO15693_T5T) &&
1041 trf7970a_is_iso15693_write_or_lock(req[1]) &&
1042 (req[0] & ISO15693_REQ_FLAG_OPTION))
1043 trf->issue_eof = true;
1044 }
1045
1046 return 0;
1047}
1048
1049static int trf7970a_in_send_cmd(struct nfc_digital_dev *ddev,
1050 struct sk_buff *skb, u16 timeout,
1051 nfc_digital_cmd_complete_t cb, void *arg)
1052{
1053 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1054 char *prefix;
1055 unsigned int len;
1056 int ret;
1057
1058 dev_dbg(trf->dev, "New request - state: %d, timeout: %d ms, len: %d\n",
1059 trf->state, timeout, skb->len);
1060
1061 if (skb->len > TRF7970A_TX_MAX)
1062 return -EINVAL;
1063
1064 mutex_lock(&trf->lock);
1065
1066 if ((trf->state != TRF7970A_ST_IDLE) &&
1067 (trf->state != TRF7970A_ST_IDLE_RX_BLOCKED)) {
1068 dev_err(trf->dev, "%s - Bogus state: %d\n", __func__,
1069 trf->state);
1070 ret = -EIO;
1071 goto out_err;
1072 }
1073
1074 if (trf->aborting) {
1075 dev_dbg(trf->dev, "Abort process complete\n");
1076 trf->aborting = false;
1077 ret = -ECANCELED;
1078 goto out_err;
1079 }
1080
1081 trf->rx_skb = nfc_alloc_recv_skb(TRF7970A_RX_SKB_ALLOC_SIZE,
1082 GFP_KERNEL);
1083 if (!trf->rx_skb) {
1084 dev_dbg(trf->dev, "Can't alloc rx_skb\n");
1085 ret = -ENOMEM;
1086 goto out_err;
1087 }
1088
1089 if (trf->state == TRF7970A_ST_IDLE_RX_BLOCKED) {
1090 ret = trf7970a_cmd(trf, TRF7970A_CMD_ENABLE_RX);
1091 if (ret)
1092 goto out_err;
1093
1094 trf->state = TRF7970A_ST_IDLE;
1095 }
1096
1097 ret = trf7970a_per_cmd_config(trf, skb);
1098 if (ret)
1099 goto out_err;
1100
1101 trf->ddev = ddev;
1102 trf->tx_skb = skb;
1103 trf->cb = cb;
1104 trf->cb_arg = arg;
1105 trf->timeout = timeout;
1106 trf->ignore_timeout = false;
1107
1108 len = skb->len;
1109 prefix = skb_push(skb, TRF7970A_TX_SKB_HEADROOM);
1110
1111 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
1112 * on what the current framing is, the address of the TX length byte 1
1113 * register (0x1d), and the 2 byte length of the data to be transmitted.
1114 */
1115 prefix[0] = TRF7970A_CMD_BIT_CTRL |
1116 TRF7970A_CMD_BIT_OPCODE(TRF7970A_CMD_FIFO_RESET);
1117 prefix[1] = TRF7970A_CMD_BIT_CTRL |
1118 TRF7970A_CMD_BIT_OPCODE(trf->tx_cmd);
1119 prefix[2] = TRF7970A_CMD_BIT_CONTINUOUS | TRF7970A_TX_LENGTH_BYTE1;
1120
1121 if (trf->framing == NFC_DIGITAL_FRAMING_NFCA_SHORT) {
1122 prefix[3] = 0x00;
1123 prefix[4] = 0x0f; /* 7 bits */
1124 } else {
1125 prefix[3] = (len & 0xf00) >> 4;
1126 prefix[3] |= ((len & 0xf0) >> 4);
1127 prefix[4] = ((len & 0x0f) << 4);
1128 }
1129
1130 len = min_t(int, skb->len, TRF7970A_FIFO_SIZE);
1131
1132 usleep_range(1000, 2000);
1133
1134 ret = trf7970a_transmit(trf, skb, len);
1135 if (ret) {
1136 kfree_skb(trf->rx_skb);
1137 trf->rx_skb = NULL;
1138 }
1139
1140out_err:
1141 mutex_unlock(&trf->lock);
1142 return ret;
1143}
1144
1145static int trf7970a_tg_configure_hw(struct nfc_digital_dev *ddev,
1146 int type, int param)
1147{
1148 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1149
1150 dev_dbg(trf->dev, "Unsupported interface\n");
1151
1152 return -EINVAL;
1153}
1154
1155static int trf7970a_tg_send_cmd(struct nfc_digital_dev *ddev,
1156 struct sk_buff *skb, u16 timeout,
1157 nfc_digital_cmd_complete_t cb, void *arg)
1158{
1159 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1160
1161 dev_dbg(trf->dev, "Unsupported interface\n");
1162
1163 return -EINVAL;
1164}
1165
1166static int trf7970a_tg_listen(struct nfc_digital_dev *ddev,
1167 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1168{
1169 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1170
1171 dev_dbg(trf->dev, "Unsupported interface\n");
1172
1173 return -EINVAL;
1174}
1175
1176static int trf7970a_tg_listen_mdaa(struct nfc_digital_dev *ddev,
1177 struct digital_tg_mdaa_params *mdaa_params,
1178 u16 timeout, nfc_digital_cmd_complete_t cb, void *arg)
1179{
1180 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1181
1182 dev_dbg(trf->dev, "Unsupported interface\n");
1183
1184 return -EINVAL;
1185}
1186
1187static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1188{
1189 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
1190
1191 dev_dbg(trf->dev, "Abort process initiated\n");
1192
1193 mutex_lock(&trf->lock);
1194 trf->aborting = true;
1195 mutex_unlock(&trf->lock);
1196}
1197
1198static struct nfc_digital_ops trf7970a_nfc_ops = {
1199 .in_configure_hw = trf7970a_in_configure_hw,
1200 .in_send_cmd = trf7970a_in_send_cmd,
1201 .tg_configure_hw = trf7970a_tg_configure_hw,
1202 .tg_send_cmd = trf7970a_tg_send_cmd,
1203 .tg_listen = trf7970a_tg_listen,
1204 .tg_listen_mdaa = trf7970a_tg_listen_mdaa,
1205 .switch_rf = trf7970a_switch_rf,
1206 .abort_cmd = trf7970a_abort_cmd,
1207};
1208
1209static int trf7970a_probe(struct spi_device *spi)
1210{
1211 struct device_node *np = spi->dev.of_node;
1212 const struct spi_device_id *id = spi_get_device_id(spi);
1213 struct trf7970a *trf;
1214 int ret;
1215
1216 if (!np) {
1217 dev_err(&spi->dev, "No Device Tree entry\n");
1218 return -EINVAL;
1219 }
1220
1221 trf = devm_kzalloc(&spi->dev, sizeof(*trf), GFP_KERNEL);
1222 if (!trf)
1223 return -ENOMEM;
1224
1225 trf->state = TRF7970A_ST_OFF;
1226 trf->dev = &spi->dev;
1227 trf->spi = spi;
1228 trf->quirks = id->driver_data;
1229
1230 spi->mode = SPI_MODE_1;
1231 spi->bits_per_word = 8;
1232
1233 /* There are two enable pins - both must be present */
1234 trf->en_gpio = of_get_named_gpio(np, "ti,enable-gpios", 0);
1235 if (!gpio_is_valid(trf->en_gpio)) {
1236 dev_err(trf->dev, "No EN GPIO property\n");
1237 return trf->en_gpio;
1238 }
1239
1240 ret = devm_gpio_request_one(trf->dev, trf->en_gpio,
1241 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN");
1242 if (ret) {
1243 dev_err(trf->dev, "Can't request EN GPIO: %d\n", ret);
1244 return ret;
1245 }
1246
1247 trf->en2_gpio = of_get_named_gpio(np, "ti,enable-gpios", 1);
1248 if (!gpio_is_valid(trf->en2_gpio)) {
1249 dev_err(trf->dev, "No EN2 GPIO property\n");
1250 return trf->en2_gpio;
1251 }
1252
1253 ret = devm_gpio_request_one(trf->dev, trf->en2_gpio,
1254 GPIOF_DIR_OUT | GPIOF_INIT_LOW, "EN2");
1255 if (ret) {
1256 dev_err(trf->dev, "Can't request EN2 GPIO: %d\n", ret);
1257 return ret;
1258 }
1259
1260 ret = devm_request_threaded_irq(trf->dev, spi->irq, NULL,
1261 trf7970a_irq, IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1262 "trf7970a", trf);
1263 if (ret) {
1264 dev_err(trf->dev, "Can't request IRQ#%d: %d\n", spi->irq, ret);
1265 return ret;
1266 }
1267
1268 mutex_init(&trf->lock);
1269 INIT_DELAYED_WORK(&trf->timeout_work, trf7970a_timeout_work_handler);
1270
1271 trf->regulator = devm_regulator_get(&spi->dev, "vin");
1272 if (IS_ERR(trf->regulator)) {
1273 ret = PTR_ERR(trf->regulator);
1274 dev_err(trf->dev, "Can't get VIN regulator: %d\n", ret);
1275 goto err_destroy_lock;
1276 }
1277
1278 ret = regulator_enable(trf->regulator);
1279 if (ret) {
1280 dev_err(trf->dev, "Can't enable VIN: %d\n", ret);
1281 goto err_destroy_lock;
1282 }
1283
1284 trf->powering_up = true;
1285
1286 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1287 TRF7970A_SUPPORTED_PROTOCOLS,
1288 NFC_DIGITAL_DRV_CAPS_IN_CRC, TRF7970A_TX_SKB_HEADROOM,
1289 0);
1290 if (!trf->ddev) {
1291 dev_err(trf->dev, "Can't allocate NFC digital device\n");
1292 ret = -ENOMEM;
1293 goto err_disable_regulator;
1294 }
1295
1296 nfc_digital_set_parent_dev(trf->ddev, trf->dev);
1297 nfc_digital_set_drvdata(trf->ddev, trf);
1298 spi_set_drvdata(spi, trf);
1299
1300 ret = nfc_digital_register_device(trf->ddev);
1301 if (ret) {
1302 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
1303 ret);
1304 goto err_free_ddev;
1305 }
1306
1307 return 0;
1308
1309err_free_ddev:
1310 nfc_digital_free_device(trf->ddev);
1311err_disable_regulator:
1312 regulator_disable(trf->regulator);
1313err_destroy_lock:
1314 mutex_destroy(&trf->lock);
1315 return ret;
1316}
1317
1318static int trf7970a_remove(struct spi_device *spi)
1319{
1320 struct trf7970a *trf = spi_get_drvdata(spi);
1321
1322 mutex_lock(&trf->lock);
1323
1324 trf7970a_switch_rf_off(trf);
1325 trf7970a_init(trf);
1326
1327 switch (trf->state) {
1328 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1329 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1330 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1331 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1332 trf7970a_send_err_upstream(trf, -ECANCELED);
1333 break;
1334 default:
1335 break;
1336 }
1337
1338 mutex_unlock(&trf->lock);
1339
1340 nfc_digital_unregister_device(trf->ddev);
1341 nfc_digital_free_device(trf->ddev);
1342
1343 regulator_disable(trf->regulator);
1344
1345 mutex_destroy(&trf->lock);
1346
1347 return 0;
1348}
1349
1350static const struct spi_device_id trf7970a_id_table[] = {
1351 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
1352 { }
1353};
1354MODULE_DEVICE_TABLE(spi, trf7970a_id_table);
1355
1356static struct spi_driver trf7970a_spi_driver = {
1357 .probe = trf7970a_probe,
1358 .remove = trf7970a_remove,
1359 .id_table = trf7970a_id_table,
1360 .driver = {
1361 .name = "trf7970a",
1362 .owner = THIS_MODULE,
1363 },
1364};
1365
1366module_spi_driver(trf7970a_spi_driver);
1367
1368MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
1369MODULE_LICENSE("GPL v2");
1370MODULE_DESCRIPTION("TI trf7970a RFID/NFC Transceiver Driver");
diff --git a/include/net/nfc/digital.h b/include/net/nfc/digital.h
index 81af21e9bcd4..7655cfe27c34 100644
--- a/include/net/nfc/digital.h
+++ b/include/net/nfc/digital.h
@@ -35,6 +35,7 @@ enum {
35 NFC_DIGITAL_RF_TECH_106A = 0, 35 NFC_DIGITAL_RF_TECH_106A = 0,
36 NFC_DIGITAL_RF_TECH_212F, 36 NFC_DIGITAL_RF_TECH_212F,
37 NFC_DIGITAL_RF_TECH_424F, 37 NFC_DIGITAL_RF_TECH_424F,
38 NFC_DIGITAL_RF_TECH_ISO15693,
38 39
39 NFC_DIGITAL_RF_TECH_LAST, 40 NFC_DIGITAL_RF_TECH_LAST,
40}; 41};
@@ -50,6 +51,7 @@ enum {
50 51
51 NFC_DIGITAL_FRAMING_NFCA_T1T, 52 NFC_DIGITAL_FRAMING_NFCA_T1T,
52 NFC_DIGITAL_FRAMING_NFCA_T2T, 53 NFC_DIGITAL_FRAMING_NFCA_T2T,
54 NFC_DIGITAL_FRAMING_NFCA_T4T,
53 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP, 55 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP,
54 56
55 NFC_DIGITAL_FRAMING_NFCF, 57 NFC_DIGITAL_FRAMING_NFCF,
@@ -57,6 +59,9 @@ enum {
57 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP, 59 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP,
58 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED, 60 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED,
59 61
62 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
63 NFC_DIGITAL_FRAMING_ISO15693_T5T,
64
60 NFC_DIGITAL_FRAMING_LAST, 65 NFC_DIGITAL_FRAMING_LAST,
61}; 66};
62 67
@@ -204,6 +209,8 @@ struct nfc_digital_dev {
204 u8 curr_rf_tech; 209 u8 curr_rf_tech;
205 u8 curr_nfc_dep_pni; 210 u8 curr_nfc_dep_pni;
206 211
212 u16 target_fsc;
213
207 int (*skb_check_crc)(struct sk_buff *skb); 214 int (*skb_check_crc)(struct sk_buff *skb);
208 void (*skb_add_crc)(struct sk_buff *skb); 215 void (*skb_add_crc)(struct sk_buff *skb);
209}; 216};
diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index e80894bca1d0..2e8b40c16274 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -111,6 +111,9 @@ struct nfc_target {
111 u8 sensf_res[NFC_SENSF_RES_MAXSIZE]; 111 u8 sensf_res[NFC_SENSF_RES_MAXSIZE];
112 u8 hci_reader_gate; 112 u8 hci_reader_gate;
113 u8 logical_idx; 113 u8 logical_idx;
114 u8 is_iso15693;
115 u8 iso15693_dsfid;
116 u8 iso15693_uid[NFC_ISO15693_UID_MAXSIZE];
114}; 117};
115 118
116/** 119/**
diff --git a/include/uapi/linux/nfc.h b/include/uapi/linux/nfc.h
index 6ad6cc03ccd3..9789dc95b6a8 100644
--- a/include/uapi/linux/nfc.h
+++ b/include/uapi/linux/nfc.h
@@ -150,6 +150,8 @@ enum nfc_commands {
150 * @NFC_ATTR_SE_TYPE: Secure element type (UICC or EMBEDDED) 150 * @NFC_ATTR_SE_TYPE: Secure element type (UICC or EMBEDDED)
151 * @NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS: Firmware download operation status 151 * @NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS: Firmware download operation status
152 * @NFC_ATTR_APDU: Secure element APDU 152 * @NFC_ATTR_APDU: Secure element APDU
153 * @NFC_ATTR_TARGET_ISO15693_DSFID: ISO 15693 Data Storage Format Identifier
154 * @NFC_ATTR_TARGET_ISO15693_UID: ISO 15693 Unique Identifier
153 */ 155 */
154enum nfc_attrs { 156enum nfc_attrs {
155 NFC_ATTR_UNSPEC, 157 NFC_ATTR_UNSPEC,
@@ -178,6 +180,8 @@ enum nfc_attrs {
178 NFC_ATTR_SE_AID, 180 NFC_ATTR_SE_AID,
179 NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, 181 NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS,
180 NFC_ATTR_SE_APDU, 182 NFC_ATTR_SE_APDU,
183 NFC_ATTR_TARGET_ISO15693_DSFID,
184 NFC_ATTR_TARGET_ISO15693_UID,
181/* private: internal use only */ 185/* private: internal use only */
182 __NFC_ATTR_AFTER_LAST 186 __NFC_ATTR_AFTER_LAST
183}; 187};
@@ -200,6 +204,7 @@ enum nfc_sdp_attr {
200#define NFC_SENSF_RES_MAXSIZE 18 204#define NFC_SENSF_RES_MAXSIZE 18
201#define NFC_GB_MAXSIZE 48 205#define NFC_GB_MAXSIZE 48
202#define NFC_FIRMWARE_NAME_MAXSIZE 32 206#define NFC_FIRMWARE_NAME_MAXSIZE 32
207#define NFC_ISO15693_UID_MAXSIZE 8
203 208
204/* NFC protocols */ 209/* NFC protocols */
205#define NFC_PROTO_JEWEL 1 210#define NFC_PROTO_JEWEL 1
@@ -208,8 +213,9 @@ enum nfc_sdp_attr {
208#define NFC_PROTO_ISO14443 4 213#define NFC_PROTO_ISO14443 4
209#define NFC_PROTO_NFC_DEP 5 214#define NFC_PROTO_NFC_DEP 5
210#define NFC_PROTO_ISO14443_B 6 215#define NFC_PROTO_ISO14443_B 6
216#define NFC_PROTO_ISO15693 7
211 217
212#define NFC_PROTO_MAX 7 218#define NFC_PROTO_MAX 8
213 219
214/* NFC communication modes */ 220/* NFC communication modes */
215#define NFC_COMM_ACTIVE 0 221#define NFC_COMM_ACTIVE 0
@@ -227,6 +233,7 @@ enum nfc_sdp_attr {
227#define NFC_PROTO_ISO14443_MASK (1 << NFC_PROTO_ISO14443) 233#define NFC_PROTO_ISO14443_MASK (1 << NFC_PROTO_ISO14443)
228#define NFC_PROTO_NFC_DEP_MASK (1 << NFC_PROTO_NFC_DEP) 234#define NFC_PROTO_NFC_DEP_MASK (1 << NFC_PROTO_NFC_DEP)
229#define NFC_PROTO_ISO14443_B_MASK (1 << NFC_PROTO_ISO14443_B) 235#define NFC_PROTO_ISO14443_B_MASK (1 << NFC_PROTO_ISO14443_B)
236#define NFC_PROTO_ISO15693_MASK (1 << NFC_PROTO_ISO15693)
230 237
231/* NFC Secure Elements */ 238/* NFC Secure Elements */
232#define NFC_SE_UICC 0x1 239#define NFC_SE_UICC 0x1
diff --git a/net/nfc/core.c b/net/nfc/core.c
index ca1e65f4b133..819b87702b70 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -280,9 +280,6 @@ static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
280{ 280{
281 int i; 281 int i;
282 282
283 if (dev->n_targets == 0)
284 return NULL;
285
286 for (i = 0; i < dev->n_targets; i++) { 283 for (i = 0; i < dev->n_targets; i++) {
287 if (dev->targets[i].idx == target_idx) 284 if (dev->targets[i].idx == target_idx)
288 return &dev->targets[i]; 285 return &dev->targets[i];
@@ -546,9 +543,9 @@ error:
546 543
547struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx) 544struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
548{ 545{
549 struct nfc_se *se, *n; 546 struct nfc_se *se;
550 547
551 list_for_each_entry_safe(se, n, &dev->secure_elements, list) 548 list_for_each_entry(se, &dev->secure_elements, list)
552 if (se->idx == se_idx) 549 if (se->idx == se_idx)
553 return se; 550 return se;
554 551
@@ -655,9 +652,6 @@ int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
655{ 652{
656 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); 653 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
657 654
658 if (gb_len > NFC_MAX_GT_LEN)
659 return -EINVAL;
660
661 return nfc_llcp_set_remote_gb(dev, gb, gb_len); 655 return nfc_llcp_set_remote_gb(dev, gb, gb_len);
662} 656}
663EXPORT_SYMBOL(nfc_set_remote_general_bytes); 657EXPORT_SYMBOL(nfc_set_remote_general_bytes);
diff --git a/net/nfc/digital.h b/net/nfc/digital.h
index 08b29b55ea63..3759add68b1b 100644
--- a/net/nfc/digital.h
+++ b/net/nfc/digital.h
@@ -72,6 +72,12 @@ void digital_poll_next_tech(struct nfc_digital_dev *ddev);
72 72
73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech); 73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
74int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech); 74int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
75int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech);
76
77int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
78 struct sk_buff *skb);
79int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
80 struct sk_buff *skb);
75 81
76int digital_target_found(struct nfc_digital_dev *ddev, 82int digital_target_found(struct nfc_digital_dev *ddev,
77 struct nfc_target *target, u8 protocol); 83 struct nfc_target *target, u8 protocol);
diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
index c129d1571ca6..e01e15dbf1ab 100644
--- a/net/nfc/digital_core.c
+++ b/net/nfc/digital_core.c
@@ -25,6 +25,8 @@
25#define DIGITAL_PROTO_NFCF_RF_TECH \ 25#define DIGITAL_PROTO_NFCF_RF_TECH \
26 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK) 26 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
27 27
28#define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
29
28struct digital_cmd { 30struct digital_cmd {
29 struct list_head queue; 31 struct list_head queue;
30 32
@@ -331,6 +333,18 @@ int digital_target_found(struct nfc_digital_dev *ddev,
331 } 333 }
332 break; 334 break;
333 335
336 case NFC_PROTO_ISO15693:
337 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
338 check_crc = digital_skb_check_crc_b;
339 add_crc = digital_skb_add_crc_b;
340 break;
341
342 case NFC_PROTO_ISO14443:
343 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
344 check_crc = digital_skb_check_crc_a;
345 add_crc = digital_skb_add_crc_a;
346 break;
347
334 default: 348 default:
335 pr_err("Invalid protocol %d\n", protocol); 349 pr_err("Invalid protocol %d\n", protocol);
336 return -EINVAL; 350 return -EINVAL;
@@ -461,7 +475,7 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
461 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, 475 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
462 digital_in_send_sens_req); 476 digital_in_send_sens_req);
463 477
464 if (im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { 478 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
465 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, 479 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
466 digital_in_send_sensf_req); 480 digital_in_send_sensf_req);
467 481
@@ -469,7 +483,11 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
469 digital_in_send_sensf_req); 483 digital_in_send_sensf_req);
470 } 484 }
471 485
472 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { 486 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
487 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
488 digital_in_send_iso15693_inv_req);
489
490 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
473 if (ddev->ops->tg_listen_mdaa) { 491 if (ddev->ops->tg_listen_mdaa) {
474 digital_add_poll_tech(ddev, 0, 492 digital_add_poll_tech(ddev, 0,
475 digital_tg_listen_mdaa); 493 digital_tg_listen_mdaa);
@@ -607,20 +625,30 @@ static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
607 625
608 if (IS_ERR(resp)) { 626 if (IS_ERR(resp)) {
609 rc = PTR_ERR(resp); 627 rc = PTR_ERR(resp);
628 resp = NULL;
610 goto done; 629 goto done;
611 } 630 }
612 631
613 if (ddev->curr_protocol == NFC_PROTO_MIFARE) 632 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
614 rc = digital_in_recv_mifare_res(resp); 633 rc = digital_in_recv_mifare_res(resp);
615 else 634 /* crc check is done in digital_in_recv_mifare_res() */
616 rc = ddev->skb_check_crc(resp); 635 goto done;
636 }
617 637
638 if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
639 rc = digital_in_iso_dep_pull_sod(ddev, resp);
640 if (rc)
641 goto done;
642 }
643
644 rc = ddev->skb_check_crc(resp);
645
646done:
618 if (rc) { 647 if (rc) {
619 kfree_skb(resp); 648 kfree_skb(resp);
620 resp = NULL; 649 resp = NULL;
621 } 650 }
622 651
623done:
624 data_exch->cb(data_exch->cb_context, resp, rc); 652 data_exch->cb(data_exch->cb_context, resp, rc);
625 653
626 kfree(data_exch); 654 kfree(data_exch);
@@ -632,6 +660,7 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
632{ 660{
633 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); 661 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
634 struct digital_data_exch *data_exch; 662 struct digital_data_exch *data_exch;
663 int rc;
635 664
636 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL); 665 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
637 if (!data_exch) { 666 if (!data_exch) {
@@ -642,13 +671,27 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
642 data_exch->cb = cb; 671 data_exch->cb = cb;
643 data_exch->cb_context = cb_context; 672 data_exch->cb_context = cb_context;
644 673
645 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) 674 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
646 return digital_in_send_dep_req(ddev, target, skb, data_exch); 675 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
676 goto exit;
677 }
678
679 if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
680 rc = digital_in_iso_dep_push_sod(ddev, skb);
681 if (rc)
682 goto exit;
683 }
647 684
648 ddev->skb_add_crc(skb); 685 ddev->skb_add_crc(skb);
649 686
650 return digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete, 687 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
651 data_exch); 688 data_exch);
689
690exit:
691 if (rc)
692 kfree(data_exch);
693
694 return rc;
652} 695}
653 696
654static struct nfc_ops digital_nfc_ops = { 697static struct nfc_ops digital_nfc_ops = {
@@ -700,6 +743,10 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
700 ddev->protocols |= NFC_PROTO_FELICA_MASK; 743 ddev->protocols |= NFC_PROTO_FELICA_MASK;
701 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK) 744 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
702 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK; 745 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
746 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
747 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
748 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
749 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
703 750
704 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; 751 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
705 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; 752 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
diff --git a/net/nfc/digital_technology.c b/net/nfc/digital_technology.c
index 251c8c753ebe..278c3fed27e0 100644
--- a/net/nfc/digital_technology.c
+++ b/net/nfc/digital_technology.c
@@ -30,6 +30,7 @@
30 30
31#define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04)) 31#define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
32#define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60)) 32#define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
33#define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
33#define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40) 34#define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
34 35
35#define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00) 36#define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
@@ -51,6 +52,34 @@
51#define DIGITAL_SENSF_REQ_RC_SC 1 52#define DIGITAL_SENSF_REQ_RC_SC 1
52#define DIGITAL_SENSF_REQ_RC_AP 2 53#define DIGITAL_SENSF_REQ_RC_AP 2
53 54
55#define DIGITAL_CMD_ISO15693_INVENTORY_REQ 0x01
56
57#define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE BIT(1)
58#define DIGITAL_ISO15693_REQ_FLAG_INVENTORY BIT(2)
59#define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
60#define DIGITAL_ISO15693_RES_FLAG_ERROR BIT(0)
61#define DIGITAL_ISO15693_RES_IS_VALID(flags) \
62 (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
63
64#define DIGITAL_ISO_DEP_I_PCB 0x02
65#define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
66
67#define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
68
69#define DIGITAL_ISO_DEP_I_BLOCK 0x00
70
71#define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
72
73static const u8 digital_ats_fsc[] = {
74 16, 24, 32, 40, 48, 64, 96, 128,
75};
76
77#define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
78#define DIGITAL_ATS_MAX_FSC 256
79
80#define DIGITAL_RATS_BYTE1 0xE0
81#define DIGITAL_RATS_PARAM 0x80
82
54struct digital_sdd_res { 83struct digital_sdd_res {
55 u8 nfcid1[4]; 84 u8 nfcid1[4];
56 u8 bcc; 85 u8 bcc;
@@ -82,9 +111,127 @@ struct digital_sensf_res {
82 u8 rd[2]; 111 u8 rd[2];
83} __packed; 112} __packed;
84 113
114struct digital_iso15693_inv_req {
115 u8 flags;
116 u8 cmd;
117 u8 mask_len;
118 u64 mask;
119} __packed;
120
121struct digital_iso15693_inv_res {
122 u8 flags;
123 u8 dsfid;
124 u64 uid;
125} __packed;
126
85static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, 127static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
86 struct nfc_target *target); 128 struct nfc_target *target);
87 129
130int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
131 struct sk_buff *skb)
132{
133 u8 pcb;
134 u8 block_type;
135
136 if (skb->len < 1)
137 return -EIO;
138
139 pcb = *skb->data;
140 block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
141
142 /* No support fo R-block nor S-block */
143 if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
144 pr_err("ISO_DEP R-block and S-block not supported\n");
145 return -EIO;
146 }
147
148 if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
149 pr_err("DID field in ISO_DEP PCB not supported\n");
150 return -EIO;
151 }
152
153 skb_pull(skb, 1);
154
155 return 0;
156}
157
158int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
159 struct sk_buff *skb)
160{
161 /*
162 * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
163 * not be greater than remote FSC
164 */
165 if (skb->len + 3 > ddev->target_fsc)
166 return -EIO;
167
168 skb_push(skb, 1);
169
170 *skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
171
172 ddev->curr_nfc_dep_pni =
173 DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
174
175 return 0;
176}
177
178static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
179 struct sk_buff *resp)
180{
181 struct nfc_target *target = arg;
182 u8 fsdi;
183 int rc;
184
185 if (IS_ERR(resp)) {
186 rc = PTR_ERR(resp);
187 resp = NULL;
188 goto exit;
189 }
190
191 if (resp->len < 2) {
192 rc = -EIO;
193 goto exit;
194 }
195
196 fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
197 if (fsdi >= 8)
198 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
199 else
200 ddev->target_fsc = digital_ats_fsc[fsdi];
201
202 ddev->curr_nfc_dep_pni = 0;
203
204 rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
205
206exit:
207 dev_kfree_skb(resp);
208 kfree(target);
209
210 if (rc)
211 digital_poll_next_tech(ddev);
212}
213
214static int digital_in_send_rats(struct nfc_digital_dev *ddev,
215 struct nfc_target *target)
216{
217 int rc;
218 struct sk_buff *skb;
219
220 skb = digital_skb_alloc(ddev, 2);
221 if (!skb)
222 return -ENOMEM;
223
224 *skb_put(skb, 1) = DIGITAL_RATS_BYTE1;
225 *skb_put(skb, 1) = DIGITAL_RATS_PARAM;
226
227 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
228 target);
229 if (rc)
230 kfree_skb(skb);
231
232 return rc;
233}
234
88static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg, 235static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
89 struct sk_buff *resp) 236 struct sk_buff *resp)
90{ 237{
@@ -122,8 +269,19 @@ static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
122 goto exit_free_skb; 269 goto exit_free_skb;
123 } 270 }
124 271
272 target->sel_res = sel_res;
273
125 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) { 274 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
126 nfc_proto = NFC_PROTO_MIFARE; 275 nfc_proto = NFC_PROTO_MIFARE;
276 } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
277 rc = digital_in_send_rats(ddev, target);
278 if (rc)
279 goto exit;
280 /*
281 * Skip target_found and don't free it for now. This will be
282 * done when receiving the ATS
283 */
284 goto exit_free_skb;
127 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) { 285 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
128 nfc_proto = NFC_PROTO_NFC_DEP; 286 nfc_proto = NFC_PROTO_NFC_DEP;
129 } else { 287 } else {
@@ -131,8 +289,6 @@ static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
131 goto exit; 289 goto exit;
132 } 290 }
133 291
134 target->sel_res = sel_res;
135
136 rc = digital_target_found(ddev, target, nfc_proto); 292 rc = digital_target_found(ddev, target, nfc_proto);
137 293
138exit: 294exit:
@@ -473,6 +629,93 @@ int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
473 return rc; 629 return rc;
474} 630}
475 631
632static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
633 void *arg, struct sk_buff *resp)
634{
635 struct digital_iso15693_inv_res *res;
636 struct nfc_target *target = NULL;
637 int rc;
638
639 if (IS_ERR(resp)) {
640 rc = PTR_ERR(resp);
641 resp = NULL;
642 goto out_free_skb;
643 }
644
645 if (resp->len != sizeof(*res)) {
646 rc = -EIO;
647 goto out_free_skb;
648 }
649
650 res = (struct digital_iso15693_inv_res *)resp->data;
651
652 if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
653 PROTOCOL_ERR("ISO15693 - 10.3.1");
654 rc = -EINVAL;
655 goto out_free_skb;
656 }
657
658 target = kzalloc(sizeof(*target), GFP_KERNEL);
659 if (!target) {
660 rc = -ENOMEM;
661 goto out_free_skb;
662 }
663
664 target->is_iso15693 = 1;
665 target->iso15693_dsfid = res->dsfid;
666 memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
667
668 rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
669
670 kfree(target);
671
672out_free_skb:
673 dev_kfree_skb(resp);
674
675 if (rc)
676 digital_poll_next_tech(ddev);
677}
678
679int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
680{
681 struct digital_iso15693_inv_req *req;
682 struct sk_buff *skb;
683 int rc;
684
685 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
686 NFC_DIGITAL_RF_TECH_ISO15693);
687 if (rc)
688 return rc;
689
690 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
691 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
692 if (rc)
693 return rc;
694
695 skb = digital_skb_alloc(ddev, sizeof(*req));
696 if (!skb)
697 return -ENOMEM;
698
699 skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
700 req = (struct digital_iso15693_inv_req *)skb->data;
701
702 /* Single sub-carrier, high data rate, no AFI, single slot
703 * Inventory command
704 */
705 req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
706 DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
707 DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
708 req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
709 req->mask_len = 0;
710
711 rc = digital_in_send_cmd(ddev, skb, 30,
712 digital_in_recv_iso15693_inv_res, NULL);
713 if (rc)
714 kfree_skb(skb);
715
716 return rc;
717}
718
476static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev) 719static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
477{ 720{
478 struct sk_buff *skb; 721 struct sk_buff *skb;
diff --git a/net/nfc/hci/llc.c b/net/nfc/hci/llc.c
index a07d2b818487..1b90c0531852 100644
--- a/net/nfc/hci/llc.c
+++ b/net/nfc/hci/llc.c
@@ -20,14 +20,12 @@
20 20
21#include "llc.h" 21#include "llc.h"
22 22
23static struct list_head llc_engines; 23static LIST_HEAD(llc_engines);
24 24
25int nfc_llc_init(void) 25int nfc_llc_init(void)
26{ 26{
27 int r; 27 int r;
28 28
29 INIT_LIST_HEAD(&llc_engines);
30
31 r = nfc_llc_nop_register(); 29 r = nfc_llc_nop_register();
32 if (r) 30 if (r)
33 goto exit; 31 goto exit;
diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index 6184bd1fba3a..b486f12ae243 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -27,7 +27,7 @@
27 27
28static u8 llcp_magic[3] = {0x46, 0x66, 0x6d}; 28static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
29 29
30static struct list_head llcp_devices; 30static LIST_HEAD(llcp_devices);
31 31
32static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb); 32static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
33 33
@@ -293,9 +293,9 @@ static void nfc_llcp_sdreq_timer(unsigned long data)
293 293
294struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev) 294struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
295{ 295{
296 struct nfc_llcp_local *local, *n; 296 struct nfc_llcp_local *local;
297 297
298 list_for_each_entry_safe(local, n, &llcp_devices, list) 298 list_for_each_entry(local, &llcp_devices, list)
299 if (local->dev == dev) 299 if (local->dev == dev)
300 return local; 300 return local;
301 301
@@ -609,14 +609,16 @@ u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
609 609
610int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len) 610int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
611{ 611{
612 struct nfc_llcp_local *local = nfc_llcp_find_local(dev); 612 struct nfc_llcp_local *local;
613
614 if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
615 return -EINVAL;
613 616
617 local = nfc_llcp_find_local(dev);
614 if (local == NULL) { 618 if (local == NULL) {
615 pr_err("No LLCP device\n"); 619 pr_err("No LLCP device\n");
616 return -ENODEV; 620 return -ENODEV;
617 } 621 }
618 if (gb_len < 3)
619 return -EINVAL;
620 622
621 memset(local->remote_gb, 0, NFC_MAX_GT_LEN); 623 memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
622 memcpy(local->remote_gb, gb, gb_len); 624 memcpy(local->remote_gb, gb, gb_len);
@@ -1622,8 +1624,6 @@ void nfc_llcp_unregister_device(struct nfc_dev *dev)
1622 1624
1623int __init nfc_llcp_init(void) 1625int __init nfc_llcp_init(void)
1624{ 1626{
1625 INIT_LIST_HEAD(&llcp_devices);
1626
1627 return nfc_llcp_sock_init(); 1627 return nfc_llcp_sock_init();
1628} 1628}
1629 1629
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 56db888b1cd5..6c34ac978501 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -74,7 +74,7 @@ static int __nci_request(struct nci_dev *ndev,
74 74
75 ndev->req_status = NCI_REQ_PEND; 75 ndev->req_status = NCI_REQ_PEND;
76 76
77 init_completion(&ndev->req_completion); 77 reinit_completion(&ndev->req_completion);
78 req(ndev, opt); 78 req(ndev, opt);
79 completion_rc = 79 completion_rc =
80 wait_for_completion_interruptible_timeout(&ndev->req_completion, 80 wait_for_completion_interruptible_timeout(&ndev->req_completion,
@@ -709,6 +709,7 @@ struct nci_dev *nci_allocate_device(struct nci_ops *ops,
709 ndev->ops = ops; 709 ndev->ops = ops;
710 ndev->tx_headroom = tx_headroom; 710 ndev->tx_headroom = tx_headroom;
711 ndev->tx_tailroom = tx_tailroom; 711 ndev->tx_tailroom = tx_tailroom;
712 init_completion(&ndev->req_completion);
712 713
713 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, 714 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
714 supported_protocols, 715 supported_protocols,
diff --git a/net/nfc/nci/spi.c b/net/nfc/nci/spi.c
index f1d426f10cce..ec250e77763a 100644
--- a/net/nfc/nci/spi.c
+++ b/net/nfc/nci/spi.c
@@ -105,7 +105,7 @@ int nci_spi_send(struct nci_spi *nspi,
105 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED) 105 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
106 goto done; 106 goto done;
107 107
108 init_completion(&nspi->req_completion); 108 reinit_completion(&nspi->req_completion);
109 completion_rc = wait_for_completion_interruptible_timeout( 109 completion_rc = wait_for_completion_interruptible_timeout(
110 &nspi->req_completion, 110 &nspi->req_completion,
111 NCI_SPI_SEND_TIMEOUT); 111 NCI_SPI_SEND_TIMEOUT);
@@ -145,6 +145,7 @@ struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
145 145
146 nspi->spi = spi; 146 nspi->spi = spi;
147 nspi->ndev = ndev; 147 nspi->ndev = ndev;
148 init_completion(&nspi->req_completion);
148 149
149 return nspi; 150 return nspi;
150} 151}
diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index ebbf6fb88b35..43cb1c17e267 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -94,6 +94,14 @@ static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
94 target->sensf_res)) 94 target->sensf_res))
95 goto nla_put_failure; 95 goto nla_put_failure;
96 96
97 if (target->is_iso15693) {
98 if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
99 target->iso15693_dsfid) ||
100 nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
101 sizeof(target->iso15693_uid), target->iso15693_uid))
102 goto nla_put_failure;
103 }
104
97 return genlmsg_end(msg, hdr); 105 return genlmsg_end(msg, hdr);
98 106
99nla_put_failure: 107nla_put_failure: