History

Merge tag 'nfc-next-3.16-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/nfc-next

Samuel Ortiz <sameo@linux.intel.com> says: "NFC: 3.16: First pull request This is the NFC pull request for 3.16. We have: - STMicroeectronics st21nfca support. The st21nfca is an HCI chipset and thus relies on the HCI stack. This submission provides support for tag redaer/writer mode (including Type 5) and device tree bindings. - PM runtime support and a bunch of bug fixes for TI's trf7970a. - Device tree support for NXP's pn544. Legacy platform data support is obviously kept intact. - NFC Tag type 4B support to the NFC Digital stack. - SOCK_RAW type support to the raw NFC socket, and allow NCI sniffing from that. This can be extended to report HCI frames and also proprietarry ones like e.g. the pn533 ones."

John W. Linville committed 12 years ago

Commit 99abe652 parents 9681047 797472828 files · +2368 −139Browse files

Unified · Split

28 files
  1. Documentation/devicetree/bindings/net/nfc/pn544.txt +35 −0
  2. Documentation/devicetree/bindings/net/nfc/st21nfca.txt +33 −0
  3. Documentation/devicetree/bindings/net/nfc/trf7970a.txt +2 −0
  4. drivers/nfc/Kconfig +1 −0
  5. drivers/nfc/Makefile +1 −0
  6. drivers/nfc/pn544/i2c.c +136 −18
  7. drivers/nfc/st21nfca/Kconfig +23 −0
  8. drivers/nfc/st21nfca/Makefile +8 −0
  9. drivers/nfc/st21nfca/i2c.c +724 −0
  10. drivers/nfc/st21nfca/st21nfca.c +698 −0
  11. drivers/nfc/st21nfca/st21nfca.h +87 −0
  12. drivers/nfc/trf7970a.c +182 −70
  13. include/linux/platform_data/st21nfca.h +32 −0
  14. include/net/nfc/digital.h +4 −0
  15. include/net/nfc/hci.h +1 −0
  16. include/net/nfc/nfc.h +3 −0
  17. include/uapi/linux/nfc.h +12 −4
  18. net/nfc/digital.h +1 −0
  19. net/nfc/digital_core.c +18 −2
  20. net/nfc/digital_technology.c +228 −2
  21. net/nfc/hci/command.c +4 −2
  22. net/nfc/hci/core.c +22 −25
  23. net/nfc/llcp_commands.c +1 −1
  24. net/nfc/llcp_core.c +6 −5
  25. net/nfc/nci/core.c +9 −0
  26. net/nfc/nci/ntf.c +2 −5
  27. net/nfc/nfc.h +6 −0
  28. net/nfc/rawsock.c +89 −5
Documentation/devicetree/bindings/net/nfc/pn544.txt+35−0
@@ -0,0 +1,35 @@
1+* NXP Semiconductors PN544 NFC Controller
2+
3+Required properties:
4+- compatible: Should be "nxp,pn544-i2c".
5+- clock-frequency: I�C work frequency.
6+- reg: address on the bus
7+- interrupt-parent: phandle for the interrupt gpio controller
8+- interrupts: GPIO interrupt to which the chip is connected
9+- enable-gpios: Output GPIO pin used for enabling/disabling the PN544
10+- firmware-gpios: Output GPIO pin used to enter firmware download mode
11+
12+Optional SoC Specific Properties:
13+- pinctrl-names: Contains only one value - "default".
14+- pintctrl-0: Specifies the pin control groups used for this controller.
15+
16+Example (for ARM-based BeagleBone with PN544 on I2C2):
17+
18+&i2c2 {
19+
20+ status = "okay";
21+
22+ pn544: pn544@28 {
23+
24+ compatible = "nxp,pn544-i2c";
25+
26+ reg = <0x28>;
27+ clock-frequency = <400000>;
28+
29+ interrupt-parent = <&gpio1>;
30+ interrupts = <17 GPIO_ACTIVE_HIGH>;
31+
32+ enable-gpios = <&gpio3 21 GPIO_ACTIVE_HIGH>;
33+ firmware-gpios = <&gpio3 19 GPIO_ACTIVE_HIGH>;
34+ };
35+};
Documentation/devicetree/bindings/net/nfc/st21nfca.txt+33−0
@@ -0,0 +1,33 @@
1+* STMicroelectronics SAS. ST21NFCA NFC Controller
2+
3+Required properties:
4+- compatible: Should be "st,st21nfca-i2c".
5+- clock-frequency: I²C work frequency.
6+- reg: address on the bus
7+- interrupt-parent: phandle for the interrupt gpio controller
8+- interrupts: GPIO interrupt to which the chip is connected
9+- enable-gpios: Output GPIO pin used for enabling/disabling the ST21NFCA
10+
11+Optional SoC Specific Properties:
12+- pinctrl-names: Contains only one value - "default".
13+- pintctrl-0: Specifies the pin control groups used for this controller.
14+
15+Example (for ARM-based BeagleBoard xM with ST21NFCA on I2C2):
16+
17+&i2c2 {
18+
19+ status = "okay";
20+
21+ st21nfca: st21nfca@1 {
22+
23+ compatible = "st,st21nfca_i2c";
24+
25+ reg = <0x01>;
26+ clock-frequency = <400000>;
27+
28+ interrupt-parent = <&gpio5>;
29+ interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
30+
31+ enable-gpios = <&gpio5 29 GPIO_ACTIVE_HIGH>;
32+ };
33+};
Documentation/devicetree/bindings/net/nfc/trf7970a.txt+2−0
@@ -12,6 +12,7 @@ Required properties: · 11 unmodified lines
1212 Optional SoC Specific Properties:
1313 - pinctrl-names: Contains only one value - "default".
1414 - pintctrl-0: Specifies the pin control groups used for this controller.
15+- autosuspend-delay: Specify autosuspend delay in milliseconds.
1516
1617 Example (for ARM-based BeagleBone with TRF7970A on SPI1):
1718
@@ -29,6 +30,7 @@ Example (for ARM-based BeagleBone with TRF7970A on SPI1): · 11 unmodified lines
2930 ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>,
3031 <&gpio2 5 GPIO_ACTIVE_LOW>;
3132 vin-supply = <&ldo3_reg>;
33+ autosuspend-delay = <30000>;
3234 status = "okay";
3335 };
3436 };
drivers/nfc/Kconfig+1−0
@@ -71,5 +71,6 @@ config NFC_PORT100 · 70 unmodified lines
7171 source "drivers/nfc/pn544/Kconfig"
7272 source "drivers/nfc/microread/Kconfig"
7373 source "drivers/nfc/nfcmrvl/Kconfig"
74+source "drivers/nfc/st21nfca/Kconfig"
7475
7576 endmenu
drivers/nfc/Makefile+1−0
@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o · 10 unmodified lines
1111 obj-$(CONFIG_NFC_PORT100) += port100.o
1212 obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
1313 obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
14+obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
1415
1516 ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
drivers/nfc/pn544/i2c.c+136−18
@@ -22,6 +22,8 @@ · 21 unmodified lines
2222 #include <linux/module.h>
2323 #include <linux/i2c.h>
2424 #include <linux/gpio.h>
25+#include <linux/of_gpio.h>
26+#include <linux/of_irq.h>
2527 #include <linux/miscdevice.h>
2628 #include <linux/interrupt.h>
2729 #include <linux/delay.h>
@@ -857,6 +859,92 @@ exit_state_wait_secure_write_answer: · 829 unmodified lines
857859 }
858860 }
859861
862+#ifdef CONFIG_OF
863+
864+static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
865+{
866+ struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
867+ struct device_node *pp;
868+ int ret;
869+
870+ pp = client->dev.of_node;
871+ if (!pp) {
872+ ret = -ENODEV;
873+ goto err_dt;
874+ }
875+
876+ /* Obtention of EN GPIO from device tree */
877+ ret = of_get_named_gpio(pp, "enable-gpios", 0);
878+ if (ret < 0) {
879+ if (ret != -EPROBE_DEFER)
880+ nfc_err(&client->dev,
881+ "Failed to get EN gpio, error: %d\n", ret);
882+ goto err_dt;
883+ }
884+ phy->gpio_en = ret;
885+
886+ /* Configuration of EN GPIO */
887+ ret = gpio_request(phy->gpio_en, "pn544_en");
888+ if (ret) {
889+ nfc_err(&client->dev, "Fail EN pin\n");
890+ goto err_dt;
891+ }
892+ ret = gpio_direction_output(phy->gpio_en, 0);
893+ if (ret) {
894+ nfc_err(&client->dev, "Fail EN pin direction\n");
895+ goto err_gpio_en;
896+ }
897+
898+ /* Obtention of FW GPIO from device tree */
899+ ret = of_get_named_gpio(pp, "firmware-gpios", 0);
900+ if (ret < 0) {
901+ if (ret != -EPROBE_DEFER)
902+ nfc_err(&client->dev,
903+ "Failed to get FW gpio, error: %d\n", ret);
904+ goto err_gpio_en;
905+ }
906+ phy->gpio_fw = ret;
907+
908+ /* Configuration of FW GPIO */
909+ ret = gpio_request(phy->gpio_fw, "pn544_fw");
910+ if (ret) {
911+ nfc_err(&client->dev, "Fail FW pin\n");
912+ goto err_gpio_en;
913+ }
914+ ret = gpio_direction_output(phy->gpio_fw, 0);
915+ if (ret) {
916+ nfc_err(&client->dev, "Fail FW pin direction\n");
917+ goto err_gpio_fw;
918+ }
919+
920+ /* IRQ */
921+ ret = irq_of_parse_and_map(pp, 0);
922+ if (ret < 0) {
923+ nfc_err(&client->dev,
924+ "Unable to get irq, error: %d\n", ret);
925+ goto err_gpio_fw;
926+ }
927+ client->irq = ret;
928+
929+ return 0;
930+
931+err_gpio_fw:
932+ gpio_free(phy->gpio_fw);
933+err_gpio_en:
934+ gpio_free(phy->gpio_en);
935+err_dt:
936+ return ret;
937+}
938+
939+#else
940+
941+static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
942+{
943+ return -ENODEV;
944+}
945+
946+#endif
947+
860948 static int pn544_hci_i2c_probe(struct i2c_client *client,
861949 const struct i2c_device_id *id)
862950 {
@@ -887,25 +975,36 @@ static int pn544_hci_i2c_probe(struct i2c_client *client, · 24 unmodified lines
887975 i2c_set_clientdata(client, phy);
888976
889977 pdata = client->dev.platform_data;
890− if (pdata == NULL) {
891− nfc_err(&client->dev, "No platform data\n");
892− return -EINVAL;
893− }
894978
895− if (pdata->request_resources == NULL) {
896− nfc_err(&client->dev, "request_resources() missing\n");
897− return -EINVAL;
898− }
979+ /* No platform data, using device tree. */
980+ if (!pdata && client->dev.of_node) {
981+ r = pn544_hci_i2c_of_request_resources(client);
982+ if (r) {
983+ nfc_err(&client->dev, "No DT data\n");
984+ return r;
985+ }
986+ /* Using platform data. */
987+ } else if (pdata) {
899988
900− r = pdata->request_resources(client);
901− if (r) {
902− nfc_err(&client->dev, "Cannot get platform resources\n");
903− return r;
904− }
989+ if (pdata->request_resources == NULL) {
990+ nfc_err(&client->dev, "request_resources() missing\n");
991+ return -EINVAL;
992+ }
905993
906− phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
907− phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
908− phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
994+ r = pdata->request_resources(client);
995+ if (r) {
996+ nfc_err(&client->dev,
997+ "Cannot get platform resources\n");
998+ return r;
999+ }
1000+
1001+ phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
1002+ phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
1003+ phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
1004+ } else {
1005+ nfc_err(&client->dev, "No platform data\n");
1006+ return -EINVAL;
1007+ }
9091008
9101009 pn544_hci_i2c_platform_init(phy);
9111010
@@ -930,8 +1029,12 @@ err_hci: · 18 unmodified lines
9301029 free_irq(client->irq, phy);
9311030
9321031 err_rti:
933− if (pdata->free_resources != NULL)
1032+ if (!pdata) {
1033+ gpio_free(phy->gpio_en);
1034+ gpio_free(phy->gpio_fw);
1035+ } else if (pdata->free_resources) {
9341036 pdata->free_resources();
1037+ }
9351038
9361039 return r;
9371040 }
@@ -953,15 +1056,30 @@ static int pn544_hci_i2c_remove(struct i2c_client *client) · 15 unmodified lines
9531056 pn544_hci_i2c_disable(phy);
9541057
9551058 free_irq(client->irq, phy);
956− if (pdata->free_resources)
1059+
1060+ /* No platform data, GPIOs have been requested by this driver */
1061+ if (!pdata) {
1062+ gpio_free(phy->gpio_en);
1063+ gpio_free(phy->gpio_fw);
1064+ /* Using platform data */
1065+ } else if (pdata->free_resources) {
9571066 pdata->free_resources();
1067+ }
9581068
9591069 return 0;
9601070 }
9611071
1072+static const struct of_device_id of_pn544_i2c_match[] = {
1073+ { .compatible = "nxp,pn544-i2c", },
1074+ {},
1075+};
1076+MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
1077+
9621078 static struct i2c_driver pn544_hci_i2c_driver = {
9631079 .driver = {
9641080 .name = PN544_HCI_I2C_DRIVER_NAME,
1081+ .owner = THIS_MODULE,
1082+ .of_match_table = of_match_ptr(of_pn544_i2c_match),
9651083 },
9661084 .probe = pn544_hci_i2c_probe,
9671085 .id_table = pn544_hci_i2c_id_table,
drivers/nfc/st21nfca/Kconfig+23−0
@@ -0,0 +1,23 @@
1+config NFC_ST21NFCA
2+ tristate "STMicroelectronics ST21NFCA NFC driver"
3+ depends on NFC_HCI
4+ select CRC_CCITT
5+ default n
6+ ---help---
7+ STMicroelectronics ST21NFCA core driver. It implements the chipset
8+ HCI logic and hooks into the NFC kernel APIs. Physical layers will
9+ register against it.
10+
11+ To compile this driver as a module, choose m here. The module will
12+ be called st21nfca.
13+ Say N if unsure.
14+
15+config NFC_ST21NFCA_I2C
16+ tristate "NFC ST21NFCA i2c support"
17+ depends on NFC_ST21NFCA && I2C && NFC_SHDLC
18+ ---help---
19+ This module adds support for the STMicroelectronics st21nfca i2c interface.
20+ Select this if your platform is using the i2c bus.
21+
22+ If you choose to build a module, it'll be called st21nfca_i2c.
23+ Say N if unsure.
drivers/nfc/st21nfca/Makefile+8−0
@@ -0,0 +1,8 @@
1+#
2+# Makefile for ST21NFCA HCI based NFC driver
3+#
4+
5+st21nfca_i2c-objs = i2c.o
6+
7+obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o
8+obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
drivers/nfc/st21nfca/i2c.c+724−0
@@ -0,0 +1,724 @@
1+/*
2+ * I2C Link Layer for ST21NFCA HCI based Driver
3+ * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
4+ *
5+ * This program is free software; you can redistribute it and/or modify it
6+ * under the terms and conditions of the GNU General Public License,
7+ * version 2, as published by the Free Software Foundation.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19+
20+#include <linux/crc-ccitt.h>
21+#include <linux/module.h>
22+#include <linux/i2c.h>
23+#include <linux/gpio.h>
24+#include <linux/of_irq.h>
25+#include <linux/of_gpio.h>
26+#include <linux/miscdevice.h>
27+#include <linux/interrupt.h>
28+#include <linux/delay.h>
29+#include <linux/nfc.h>
30+#include <linux/firmware.h>
31+#include <linux/unaligned/access_ok.h>
32+#include <linux/platform_data/st21nfca.h>
33+
34+#include <net/nfc/hci.h>
35+#include <net/nfc/llc.h>
36+#include <net/nfc/nfc.h>
37+
38+#include "st21nfca.h"
39+
40+/*
41+ * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
42+ * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
43+ * called byte stuffing has been introduced.
44+ *
45+ * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
46+ * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
47+ * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
48+ */
49+#define ST21NFCA_SOF_EOF 0x7e
50+#define ST21NFCA_BYTE_STUFFING_MASK 0x20
51+#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
52+
53+/* SOF + 00 */
54+#define ST21NFCA_FRAME_HEADROOM 2
55+
56+/* 2 bytes crc + EOF */
57+#define ST21NFCA_FRAME_TAILROOM 3
58+#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
59+ buf[1] == 0)
60+
61+#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
62+
63+static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
64+ {ST21NFCA_HCI_DRIVER_NAME, 0},
65+ {}
66+};
67+
68+MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
69+
70+struct st21nfca_i2c_phy {
71+ struct i2c_client *i2c_dev;
72+ struct nfc_hci_dev *hdev;
73+
74+ unsigned int gpio_ena;
75+ unsigned int gpio_irq;
76+ unsigned int irq_polarity;
77+
78+ struct sk_buff *pending_skb;
79+ int current_read_len;
80+ /*
81+ * crc might have fail because i2c macro
82+ * is disable due to other interface activity
83+ */
84+ int crc_trials;
85+
86+ int powered;
87+ int run_mode;
88+
89+ /*
90+ * < 0 if hardware error occured (e.g. i2c err)
91+ * and prevents normal operation.
92+ */
93+ int hard_fault;
94+ struct mutex phy_lock;
95+};
96+static u8 len_seq[] = { 13, 24, 15, 29 };
97+static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
98+
99+#define I2C_DUMP_SKB(info, skb) \
100+do { \
101+ pr_debug("%s:\n", info); \
102+ print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
103+ 16, 1, (skb)->data, (skb)->len, 0); \
104+} while (0)
105+
106+/*
107+ * In order to get the CLF in a known state we generate an internal reboot
108+ * using a proprietary command.
109+ * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
110+ * fill buffer.
111+ */
112+static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
113+{
114+ u16 wait_reboot[] = { 50, 300, 1000 };
115+ char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
116+ u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
117+ int i, r = -1;
118+
119+ for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
120+ r = i2c_master_send(phy->i2c_dev, reboot_cmd,
121+ sizeof(reboot_cmd));
122+ if (r < 0)
123+ msleep(wait_reboot[i]);
124+ }
125+ if (r < 0)
126+ return r;
127+
128+ /* CLF is spending about 20ms to do an internal reboot */
129+ msleep(20);
130+ r = -1;
131+ for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
132+ r = i2c_master_recv(phy->i2c_dev, tmp,
133+ ST21NFCA_HCI_LLC_MAX_SIZE);
134+ if (r < 0)
135+ msleep(wait_reboot[i]);
136+ }
137+ if (r < 0)
138+ return r;
139+
140+ for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
141+ tmp[i] == ST21NFCA_SOF_EOF; i++)
142+ ;
143+
144+ if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
145+ return -ENODEV;
146+
147+ usleep_range(1000, 1500);
148+ return 0;
149+}
150+
151+static int st21nfca_hci_i2c_enable(void *phy_id)
152+{
153+ struct st21nfca_i2c_phy *phy = phy_id;
154+
155+ gpio_set_value(phy->gpio_ena, 1);
156+ phy->powered = 1;
157+ phy->run_mode = ST21NFCA_HCI_MODE;
158+
159+ usleep_range(10000, 15000);
160+
161+ return 0;
162+}
163+
164+static void st21nfca_hci_i2c_disable(void *phy_id)
165+{
166+ struct st21nfca_i2c_phy *phy = phy_id;
167+
168+ pr_info("\n");
169+ gpio_set_value(phy->gpio_ena, 0);
170+
171+ phy->powered = 0;
172+}
173+
174+static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
175+{
176+ u16 crc;
177+ u8 tmp;
178+
179+ *skb_push(skb, 1) = 0;
180+
181+ crc = crc_ccitt(0xffff, skb->data, skb->len);
182+ crc = ~crc;
183+
184+ tmp = crc & 0x00ff;
185+ *skb_put(skb, 1) = tmp;
186+
187+ tmp = (crc >> 8) & 0x00ff;
188+ *skb_put(skb, 1) = tmp;
189+}
190+
191+static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
192+{
193+ skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
194+ skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
195+}
196+
197+/*
198+ * Writing a frame must not return the number of written bytes.
199+ * It must return either zero for success, or <0 for error.
200+ * In addition, it must not alter the skb
201+ */
202+static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
203+{
204+ int r = -1, i, j;
205+ struct st21nfca_i2c_phy *phy = phy_id;
206+ struct i2c_client *client = phy->i2c_dev;
207+ u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
208+
209+ I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
210+
211+
212+ if (phy->hard_fault != 0)
213+ return phy->hard_fault;
214+
215+ /*
216+ * Compute CRC before byte stuffing computation on frame
217+ * Note st21nfca_hci_add_len_crc is doing a byte stuffing
218+ * on its own value
219+ */
220+ st21nfca_hci_add_len_crc(skb);
221+
222+ /* add ST21NFCA_SOF_EOF on tail */
223+ *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
224+ /* add ST21NFCA_SOF_EOF on head */
225+ *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
226+
227+ /*
228+ * Compute byte stuffing
229+ * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
230+ * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
231+ * xor byte with ST21NFCA_BYTE_STUFFING_MASK
232+ */
233+ tmp[0] = skb->data[0];
234+ for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
235+ if (skb->data[i] == ST21NFCA_SOF_EOF
236+ || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
237+ tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
238+ j++;
239+ tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
240+ } else {
241+ tmp[j] = skb->data[i];
242+ }
243+ }
244+ tmp[j] = skb->data[i];
245+ j++;
246+
247+ /*
248+ * Manage sleep mode
249+ * Try 3 times to send data with delay between each
250+ */
251+ mutex_lock(&phy->phy_lock);
252+ for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
253+ r = i2c_master_send(client, tmp, j);
254+ if (r < 0)
255+ msleep(wait_tab[i]);
256+ }
257+ mutex_unlock(&phy->phy_lock);
258+
259+ if (r >= 0) {
260+ if (r != j)
261+ r = -EREMOTEIO;
262+ else
263+ r = 0;
264+ }
265+
266+ st21nfca_hci_remove_len_crc(skb);
267+
268+ return r;
269+}
270+
271+static int get_frame_size(u8 *buf, int buflen)
272+{
273+ int len = 0;
274+ if (buf[len + 1] == ST21NFCA_SOF_EOF)
275+ return 0;
276+
277+ for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
278+ ;
279+
280+ return len;
281+}
282+
283+static int check_crc(u8 *buf, int buflen)
284+{
285+ u16 crc;
286+
287+ crc = crc_ccitt(0xffff, buf, buflen - 2);
288+ crc = ~crc;
289+
290+ if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
291+ pr_err(ST21NFCA_HCI_DRIVER_NAME
292+ ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
293+ buf[buflen - 2]);
294+
295+ pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
296+ print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
297+ 16, 2, buf, buflen, false);
298+ return -EPERM;
299+ }
300+ return 0;
301+}
302+
303+/*
304+ * Prepare received data for upper layer.
305+ * Received data include byte stuffing, crc and sof/eof
306+ * which is not usable by hci part.
307+ * returns:
308+ * frame size without sof/eof, header and byte stuffing
309+ * -EBADMSG : frame was incorrect and discarded
310+ */
311+static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
312+{
313+ int i, j, r, size;
314+ if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
315+ return -EBADMSG;
316+
317+ size = get_frame_size(skb->data, skb->len);
318+ if (size > 0) {
319+ skb_trim(skb, size);
320+ /* remove ST21NFCA byte stuffing for upper layer */
321+ for (i = 1, j = 0; i < skb->len; i++) {
322+ if (skb->data[i + j] ==
323+ (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
324+ skb->data[i] = skb->data[i + j + 1]
325+ | ST21NFCA_BYTE_STUFFING_MASK;
326+ i++;
327+ j++;
328+ }
329+ skb->data[i] = skb->data[i + j];
330+ }
331+ /* remove byte stuffing useless byte */
332+ skb_trim(skb, i - j);
333+ /* remove ST21NFCA_SOF_EOF from head */
334+ skb_pull(skb, 1);
335+
336+ r = check_crc(skb->data, skb->len);
337+ if (r != 0) {
338+ i = 0;
339+ return -EBADMSG;
340+ }
341+
342+ /* remove headbyte */
343+ skb_pull(skb, 1);
344+ /* remove crc. Byte Stuffing is already removed here */
345+ skb_trim(skb, skb->len - 2);
346+ return skb->len;
347+ }
348+ return 0;
349+}
350+
351+/*
352+ * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
353+ * that i2c bus will be flushed and that next read will start on a new frame.
354+ * returned skb contains only LLC header and payload.
355+ * returns:
356+ * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
357+ * end of read)
358+ * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
359+ * at end of read)
360+ * -EREMOTEIO : i2c read error (fatal)
361+ * -EBADMSG : frame was incorrect and discarded
362+ * (value returned from st21nfca_hci_i2c_repack)
363+ * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
364+ * the read length end sequence
365+ */
366+static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
367+ struct sk_buff *skb)
368+{
369+ int r, i;
370+ u8 len;
371+ u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
372+ struct i2c_client *client = phy->i2c_dev;
373+
374+ if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
375+ len = len_seq[phy->current_read_len];
376+
377+ /*
378+ * Add retry mecanism
379+ * Operation on I2C interface may fail in case of operation on
380+ * RF or SWP interface
381+ */
382+ r = 0;
383+ mutex_lock(&phy->phy_lock);
384+ for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
385+ r = i2c_master_recv(client, buf, len);
386+ if (r < 0)
387+ msleep(wait_tab[i]);
388+ }
389+ mutex_unlock(&phy->phy_lock);
390+
391+ if (r != len) {
392+ phy->current_read_len = 0;
393+ return -EREMOTEIO;
394+ }
395+
396+ /*
397+ * The first read sequence does not start with SOF.
398+ * Data is corrupeted so we drop it.
399+ */
400+ if (!phy->current_read_len && buf[0] != ST21NFCA_SOF_EOF) {
401+ skb_trim(skb, 0);
402+ phy->current_read_len = 0;
403+ return -EIO;
404+ } else if (phy->current_read_len &&
405+ IS_START_OF_FRAME(buf)) {
406+ /*
407+ * Previous frame transmission was interrupted and
408+ * the frame got repeated.
409+ * Received frame start with ST21NFCA_SOF_EOF + 00.
410+ */
411+ skb_trim(skb, 0);
412+ phy->current_read_len = 0;
413+ }
414+
415+ memcpy(skb_put(skb, len), buf, len);
416+
417+ if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
418+ phy->current_read_len = 0;
419+ return st21nfca_hci_i2c_repack(skb);
420+ }
421+ phy->current_read_len++;
422+ return -EAGAIN;
423+ }
424+ return -EIO;
425+}
426+
427+/*
428+ * Reads an shdlc frame from the chip. This is not as straightforward as it
429+ * seems. The frame format is data-crc, and corruption can occur anywhere
430+ * while transiting on i2c bus, such that we could read an invalid data.
431+ * The tricky case is when we read a corrupted data or crc. We must detect
432+ * this here in order to determine that data can be transmitted to the hci
433+ * core. This is the reason why we check the crc here.
434+ * The CLF will repeat a frame until we send a RR on that frame.
435+ *
436+ * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
437+ * available in the incoming data, other IRQ might come. Every IRQ will trigger
438+ * a read sequence with different length and will fill the current frame.
439+ * The reception is complete once we reach a ST21NFCA_SOF_EOF.
440+ */
441+static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
442+{
443+ struct st21nfca_i2c_phy *phy = phy_id;
444+ struct i2c_client *client;
445+
446+ int r;
447+
448+ if (!phy || irq != phy->i2c_dev->irq) {
449+ WARN_ON_ONCE(1);
450+ return IRQ_NONE;
451+ }
452+
453+ client = phy->i2c_dev;
454+ dev_dbg(&client->dev, "IRQ\n");
455+
456+ if (phy->hard_fault != 0)
457+ return IRQ_HANDLED;
458+
459+ r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
460+ if (r == -EREMOTEIO) {
461+ phy->hard_fault = r;
462+
463+ nfc_hci_recv_frame(phy->hdev, NULL);
464+
465+ return IRQ_HANDLED;
466+ } else if (r == -EAGAIN || r == -EIO) {
467+ return IRQ_HANDLED;
468+ } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
469+ /*
470+ * With ST21NFCA, only one interface (I2C, RF or SWP)
471+ * may be active at a time.
472+ * Having incorrect crc is usually due to i2c macrocell
473+ * deactivation in the middle of a transmission.
474+ * It may generate corrupted data on i2c.
475+ * We give sometime to get i2c back.
476+ * The complete frame will be repeated.
477+ */
478+ msleep(wait_tab[phy->crc_trials]);
479+ phy->crc_trials++;
480+ phy->current_read_len = 0;
481+ kfree_skb(phy->pending_skb);
482+ } else if (r > 0) {
483+ /*
484+ * We succeeded to read data from the CLF and
485+ * data is valid.
486+ * Reset counter.
487+ */
488+ nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
489+ phy->crc_trials = 0;
490+ }
491+
492+ phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
493+ if (phy->pending_skb == NULL) {
494+ phy->hard_fault = -ENOMEM;
495+ nfc_hci_recv_frame(phy->hdev, NULL);
496+ }
497+
498+ return IRQ_HANDLED;
499+}
500+
501+static struct nfc_phy_ops i2c_phy_ops = {
502+ .write = st21nfca_hci_i2c_write,
503+ .enable = st21nfca_hci_i2c_enable,
504+ .disable = st21nfca_hci_i2c_disable,
505+};
506+
507+#ifdef CONFIG_OF
508+static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
509+{
510+ struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
511+ struct device_node *pp;
512+ int gpio;
513+ int r;
514+
515+ pp = client->dev.of_node;
516+ if (!pp)
517+ return -ENODEV;
518+
519+ /* Get GPIO from device tree */
520+ gpio = of_get_named_gpio(pp, "enable-gpios", 0);
521+ if (gpio < 0) {
522+ nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
523+ return gpio;
524+ }
525+
526+ /* GPIO request and configuration */
527+ r = devm_gpio_request(&client->dev, gpio, "clf_enable");
528+ if (r) {
529+ nfc_err(&client->dev, "Failed to request enable pin\n");
530+ return -ENODEV;
531+ }
532+
533+ r = gpio_direction_output(gpio, 1);
534+ if (r) {
535+ nfc_err(&client->dev, "Failed to set enable pin direction as output\n");
536+ return -ENODEV;
537+ }
538+ phy->gpio_ena = gpio;
539+
540+ /* IRQ */
541+ r = irq_of_parse_and_map(pp, 0);
542+ if (r < 0) {
543+ nfc_err(&client->dev,
544+ "Unable to get irq, error: %d\n", r);
545+ return r;
546+ }
547+
548+ phy->irq_polarity = irq_get_trigger_type(r);
549+ client->irq = r;
550+
551+ return 0;
552+}
553+#else
554+static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
555+{
556+ return -ENODEV;
557+}
558+#endif
559+
560+static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
561+{
562+ struct st21nfca_nfc_platform_data *pdata;
563+ struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
564+ int r;
565+ int irq;
566+
567+ pdata = client->dev.platform_data;
568+ if (pdata == NULL) {
569+ nfc_err(&client->dev, "No platform data\n");
570+ return -EINVAL;
571+ }
572+
573+ /* store for later use */
574+ phy->gpio_irq = pdata->gpio_irq;
575+ phy->gpio_ena = pdata->gpio_ena;
576+ phy->irq_polarity = pdata->irq_polarity;
577+
578+ r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
579+ if (r) {
580+ pr_err("%s : gpio_request failed\n", __FILE__);
581+ return -ENODEV;
582+ }
583+
584+ r = gpio_direction_input(phy->gpio_irq);
585+ if (r) {
586+ pr_err("%s : gpio_direction_input failed\n", __FILE__);
587+ return -ENODEV;
588+ }
589+
590+ if (phy->gpio_ena > 0) {
591+ r = devm_gpio_request(&client->dev,
592+ phy->gpio_ena, "clf_enable");
593+ if (r) {
594+ pr_err("%s : ena gpio_request failed\n", __FILE__);
595+ return -ENODEV;
596+ }
597+ r = gpio_direction_output(phy->gpio_ena, 1);
598+
599+ if (r) {
600+ pr_err("%s : ena gpio_direction_output failed\n",
601+ __FILE__);
602+ return -ENODEV;
603+ }
604+ }
605+
606+ /* IRQ */
607+ irq = gpio_to_irq(phy->gpio_irq);
608+ if (irq < 0) {
609+ nfc_err(&client->dev,
610+ "Unable to get irq number for GPIO %d error %d\n",
611+ phy->gpio_irq, r);
612+ return -ENODEV;
613+ }
614+ client->irq = irq;
615+
616+ return 0;
617+}
618+
619+static int st21nfca_hci_i2c_probe(struct i2c_client *client,
620+ const struct i2c_device_id *id)
621+{
622+ struct st21nfca_i2c_phy *phy;
623+ struct st21nfca_nfc_platform_data *pdata;
624+ int r;
625+
626+ dev_dbg(&client->dev, "%s\n", __func__);
627+ dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
628+
629+ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
630+ nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
631+ return -ENODEV;
632+ }
633+
634+ phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
635+ GFP_KERNEL);
636+ if (!phy) {
637+ nfc_err(&client->dev,
638+ "Cannot allocate memory for st21nfca i2c phy.\n");
639+ return -ENOMEM;
640+ }
641+
642+ phy->i2c_dev = client;
643+ phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
644+ if (phy->pending_skb == NULL)
645+ return -ENOMEM;
646+
647+ phy->current_read_len = 0;
648+ phy->crc_trials = 0;
649+ mutex_init(&phy->phy_lock);
650+ i2c_set_clientdata(client, phy);
651+
652+ pdata = client->dev.platform_data;
653+ if (!pdata && client->dev.of_node) {
654+ r = st21nfca_hci_i2c_of_request_resources(client);
655+ if (r) {
656+ nfc_err(&client->dev, "No platform data\n");
657+ return r;
658+ }
659+ } else if (pdata) {
660+ r = st21nfca_hci_i2c_request_resources(client);
661+ if (r) {
662+ nfc_err(&client->dev, "Cannot get platform resources\n");
663+ return r;
664+ }
665+ } else {
666+ nfc_err(&client->dev, "st21nfca platform resources not available\n");
667+ return -ENODEV;
668+ }
669+
670+ r = st21nfca_hci_platform_init(phy);
671+ if (r < 0) {
672+ nfc_err(&client->dev, "Unable to reboot st21nfca\n");
673+ return -ENODEV;
674+ }
675+
676+ r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
677+ st21nfca_hci_irq_thread_fn,
678+ phy->irq_polarity | IRQF_ONESHOT,
679+ ST21NFCA_HCI_DRIVER_NAME, phy);
680+ if (r < 0) {
681+ nfc_err(&client->dev, "Unable to register IRQ handler\n");
682+ return r;
683+ }
684+
685+ return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
686+ ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
687+ ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
688+}
689+
690+static int st21nfca_hci_i2c_remove(struct i2c_client *client)
691+{
692+ struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
693+
694+ dev_dbg(&client->dev, "%s\n", __func__);
695+
696+ st21nfca_hci_remove(phy->hdev);
697+
698+ if (phy->powered)
699+ st21nfca_hci_i2c_disable(phy);
700+
701+ return 0;
702+}
703+
704+static const struct of_device_id of_st21nfca_i2c_match[] = {
705+ { .compatible = "st,st21nfca_i2c", },
706+ {}
707+};
708+
709+static struct i2c_driver st21nfca_hci_i2c_driver = {
710+ .driver = {
711+ .owner = THIS_MODULE,
712+ .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
713+ .owner = THIS_MODULE,
714+ .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
715+ },
716+ .probe = st21nfca_hci_i2c_probe,
717+ .id_table = st21nfca_hci_i2c_id_table,
718+ .remove = st21nfca_hci_i2c_remove,
719+};
720+
721+module_i2c_driver(st21nfca_hci_i2c_driver);
722+
723+MODULE_LICENSE("GPL");
724+MODULE_DESCRIPTION(DRIVER_DESC);
drivers/nfc/st21nfca/st21nfca.c+698−0
@@ -0,0 +1,698 @@
1+/*
2+ * HCI based Driver for STMicroelectronics NFC Chip
3+ *
4+ * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
5+ *
6+ * This program is free software; you can redistribute it and/or modify it
7+ * under the terms and conditions of the GNU General Public License,
8+ * version 2, as published by the Free Software Foundation.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
17+ */
18+
19+#include <linux/module.h>
20+#include <linux/nfc.h>
21+#include <net/nfc/hci.h>
22+#include <net/nfc/llc.h>
23+
24+#include "st21nfca.h"
25+
26+#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
27+
28+#define FULL_VERSION_LEN 3
29+
30+/* Proprietary gates, events, commands and registers */
31+
32+/* Commands that apply to all RF readers */
33+#define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
34+
35+#define ST21NFCA_RF_READER_ISO15693_GATE 0x12
36+#define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
37+
38+/*
39+ * Reader gate for communication with contact-less cards using Type A
40+ * protocol ISO14443-3 but not compliant with ISO14443-4
41+ */
42+#define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
43+#define ST21NFCA_RF_READER_14443_3_A_UID 0x02
44+#define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
45+#define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
46+
47+#define ST21NFCA_DEVICE_MGNT_GATE 0x01
48+#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
49+
50+#define ST21NFCA_DM_GETINFO 0x13
51+#define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
52+#define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
53+#define ST21NFCA_DM_PIPE_CREATED 0x02
54+#define ST21NFCA_DM_PIPE_OPEN 0x04
55+#define ST21NFCA_DM_RF_ACTIVE 0x80
56+
57+#define ST21NFCA_DM_IS_PIPE_OPEN(p) \
58+ ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
59+
60+#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
61+
62+static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63+
64+static struct nfc_hci_gate st21nfca_gates[] = {
65+ {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
66+ {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
67+ {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
68+ {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
69+ {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
70+ {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
71+ {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
72+ {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
73+ {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
74+ {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
75+};
76+
77+struct st21nfca_pipe_info {
78+ u8 pipe_state;
79+ u8 src_host_id;
80+ u8 src_gate_id;
81+ u8 dst_host_id;
82+ u8 dst_gate_id;
83+} __packed;
84+
85+/* Largest headroom needed for outgoing custom commands */
86+#define ST21NFCA_CMDS_HEADROOM 7
87+
88+static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
89+{
90+ int i, j, r;
91+ struct sk_buff *skb_pipe_list, *skb_pipe_info;
92+ struct st21nfca_pipe_info *info;
93+
94+ u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
95+ NFC_HCI_TERMINAL_HOST_ID
96+ };
97+ u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
98+ NFC_HCI_TERMINAL_HOST_ID, 0
99+ };
100+
101+ skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
102+ if (!skb_pipe_list) {
103+ r = -ENOMEM;
104+ goto free_list;
105+ }
106+
107+ skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
108+ if (!skb_pipe_info) {
109+ r = -ENOMEM;
110+ goto free_info;
111+ }
112+
113+ /* On ST21NFCA device pipes number are dynamics
114+ * A maximum of 16 pipes can be created at the same time
115+ * If pipes are already created, hci_dev_up will fail.
116+ * Doing a clear all pipe is a bad idea because:
117+ * - It does useless EEPROM cycling
118+ * - It might cause issue for secure elements support
119+ * (such as removing connectivity or APDU reader pipe)
120+ * A better approach on ST21NFCA is to:
121+ * - get a pipe list for each host.
122+ * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
123+ * (TODO Later on UICC HOST and eSE HOST)
124+ * - get pipe information
125+ * - match retrieved pipe list in st21nfca_gates
126+ * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
127+ * with ST21NFCA_DEVICE_MGNT_PIPE.
128+ * Pipe can be closed and need to be open.
129+ */
130+ r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
131+ ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
132+ if (r < 0)
133+ goto free_info;
134+
135+ /* Get pipe list */
136+ r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
137+ ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
138+ &skb_pipe_list);
139+ if (r < 0)
140+ goto free_info;
141+
142+ /* Complete the existing gate_pipe table */
143+ for (i = 0; i < skb_pipe_list->len; i++) {
144+ pipe_info[2] = skb_pipe_list->data[i];
145+ r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
146+ ST21NFCA_DM_GETINFO, pipe_info,
147+ sizeof(pipe_info), &skb_pipe_info);
148+
149+ if (r)
150+ continue;
151+
152+ /*
153+ * Match pipe ID and gate ID
154+ * Output format from ST21NFC_DM_GETINFO is:
155+ * - pipe state (1byte)
156+ * - source hid (1byte)
157+ * - source gid (1byte)
158+ * - destination hid (1byte)
159+ * - destination gid (1byte)
160+ */
161+ info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
162+ for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
163+ (st21nfca_gates[j].gate != info->dst_gate_id);
164+ j++)
165+ ;
166+
167+ if (j < ARRAY_SIZE(st21nfca_gates) &&
168+ st21nfca_gates[j].gate == info->dst_gate_id &&
169+ ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
170+ st21nfca_gates[j].pipe = pipe_info[2];
171+ hdev->gate2pipe[st21nfca_gates[j].gate] =
172+ st21nfca_gates[j].pipe;
173+ }
174+ }
175+
176+ /*
177+ * 3 gates have a well known pipe ID.
178+ * They will never appear in the pipe list
179+ */
180+ if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
181+ for (i = skb_pipe_list->len + 3;
182+ i < ARRAY_SIZE(st21nfca_gates); i++) {
183+ r = nfc_hci_connect_gate(hdev,
184+ NFC_HCI_HOST_CONTROLLER_ID,
185+ st21nfca_gates[i].gate,
186+ st21nfca_gates[i].pipe);
187+ if (r < 0)
188+ goto free_info;
189+ }
190+ }
191+
192+ memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
193+free_info:
194+ kfree_skb(skb_pipe_info);
195+free_list:
196+ kfree_skb(skb_pipe_list);
197+ return r;
198+}
199+
200+static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
201+{
202+ struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
203+ int r;
204+
205+ mutex_lock(&info->info_lock);
206+
207+ if (info->state != ST21NFCA_ST_COLD) {
208+ r = -EBUSY;
209+ goto out;
210+ }
211+
212+ r = info->phy_ops->enable(info->phy_id);
213+
214+ if (r == 0)
215+ info->state = ST21NFCA_ST_READY;
216+
217+out:
218+ mutex_unlock(&info->info_lock);
219+ return r;
220+}
221+
222+static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
223+{
224+ struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
225+
226+ mutex_lock(&info->info_lock);
227+
228+ if (info->state == ST21NFCA_ST_COLD)
229+ goto out;
230+
231+ info->phy_ops->disable(info->phy_id);
232+ info->state = ST21NFCA_ST_COLD;
233+
234+out:
235+ mutex_unlock(&info->info_lock);
236+}
237+
238+static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
239+{
240+ struct sk_buff *skb;
241+
242+ u8 param;
243+ int r;
244+
245+ param = NFC_HCI_UICC_HOST_ID;
246+ r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
247+ NFC_HCI_ADMIN_WHITELIST, &param, 1);
248+ if (r < 0)
249+ return r;
250+
251+ /* Set NFC_MODE in device management gate to enable */
252+ r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
253+ ST21NFCA_NFC_MODE, &skb);
254+ if (r < 0)
255+ return r;
256+
257+ if (skb->data[0] == 0) {
258+ kfree_skb(skb);
259+ param = 1;
260+
261+ r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
262+ ST21NFCA_NFC_MODE, &param, 1);
263+ if (r < 0)
264+ return r;
265+ }
266+
267+ r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
268+ NFC_HCI_EVT_END_OPERATION, NULL, 0);
269+ if (r < 0)
270+ return r;
271+
272+ r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
273+ NFC_HCI_ID_MGMT_VERSION_SW, &skb);
274+ if (r < 0)
275+ return r;
276+
277+ if (skb->len != FULL_VERSION_LEN) {
278+ kfree_skb(skb);
279+ return -EINVAL;
280+ }
281+
282+ print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
283+ DUMP_PREFIX_NONE, 16, 1,
284+ skb->data, FULL_VERSION_LEN, false);
285+
286+ kfree_skb(skb);
287+
288+ return 0;
289+}
290+
291+static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
292+{
293+ struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
294+
295+ return info->phy_ops->write(info->phy_id, skb);
296+}
297+
298+static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
299+ u32 im_protocols, u32 tm_protocols)
300+{
301+ int r;
302+
303+ pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
304+ __func__, im_protocols, tm_protocols);
305+
306+ r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
307+ NFC_HCI_EVT_END_OPERATION, NULL, 0);
308+ if (r < 0)
309+ return r;
310+ if (im_protocols) {
311+ /*
312+ * enable polling according to im_protocols & tm_protocols
313+ * - CLOSE pipe according to im_protocols & tm_protocols
314+ */
315+ if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
316+ r = nfc_hci_disconnect_gate(hdev,
317+ NFC_HCI_RF_READER_B_GATE);
318+ if (r < 0)
319+ return r;
320+ }
321+
322+ if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
323+ r = nfc_hci_disconnect_gate(hdev,
324+ NFC_HCI_RF_READER_A_GATE);
325+ if (r < 0)
326+ return r;
327+ }
328+
329+ if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
330+ r = nfc_hci_disconnect_gate(hdev,
331+ ST21NFCA_RF_READER_F_GATE);
332+ if (r < 0)
333+ return r;
334+ }
335+
336+ if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
337+ r = nfc_hci_disconnect_gate(hdev,
338+ ST21NFCA_RF_READER_14443_3_A_GATE);
339+ if (r < 0)
340+ return r;
341+ }
342+
343+ if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
344+ r = nfc_hci_disconnect_gate(hdev,
345+ ST21NFCA_RF_READER_ISO15693_GATE);
346+ if (r < 0)
347+ return r;
348+ }
349+
350+ r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
351+ NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
352+ if (r < 0)
353+ nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
354+ NFC_HCI_EVT_END_OPERATION, NULL, 0);
355+ }
356+ return r;
357+}
358+
359+static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
360+{
361+ int r;
362+ struct sk_buff *atqa_skb = NULL;
363+
364+ r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
365+ ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
366+ if (r < 0)
367+ goto exit;
368+
369+ if (atqa_skb->len != 2) {
370+ r = -EPROTO;
371+ goto exit;
372+ }
373+
374+ *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
375+
376+exit:
377+ kfree_skb(atqa_skb);
378+ return r;
379+}
380+
381+static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
382+{
383+ int r;
384+ struct sk_buff *sak_skb = NULL;
385+
386+ r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
387+ ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
388+ if (r < 0)
389+ goto exit;
390+
391+ if (sak_skb->len != 1) {
392+ r = -EPROTO;
393+ goto exit;
394+ }
395+
396+ *sak = sak_skb->data[0];
397+
398+exit:
399+ kfree_skb(sak_skb);
400+ return r;
401+}
402+
403+static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
404+ int *len)
405+{
406+ int r;
407+ struct sk_buff *uid_skb = NULL;
408+
409+ r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
410+ ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
411+ if (r < 0)
412+ goto exit;
413+
414+ if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
415+ r = -EPROTO;
416+ goto exit;
417+ }
418+
419+ gate = uid_skb->data;
420+ *len = uid_skb->len;
421+exit:
422+ kfree_skb(uid_skb);
423+ return r;
424+}
425+
426+static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
427+ struct nfc_target *target)
428+{
429+ int r;
430+ struct sk_buff *inventory_skb = NULL;
431+
432+ r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
433+ ST21NFCA_RF_READER_ISO15693_INVENTORY,
434+ &inventory_skb);
435+ if (r < 0)
436+ goto exit;
437+
438+ skb_pull(inventory_skb, 2);
439+
440+ if (inventory_skb->len == 0 ||
441+ inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
442+ r = -EPROTO;
443+ goto exit;
444+ }
445+
446+ memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
447+ target->iso15693_dsfid = inventory_skb->data[1];
448+ target->is_iso15693 = 1;
449+exit:
450+ kfree_skb(inventory_skb);
451+ return r;
452+}
453+
454+static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
455+ struct nfc_target *target)
456+{
457+ int r, len;
458+ u16 atqa;
459+ u8 sak;
460+ u8 uid[NFC_NFCID1_MAXSIZE];
461+
462+ switch (gate) {
463+ case ST21NFCA_RF_READER_F_GATE:
464+ target->supported_protocols = NFC_PROTO_FELICA_MASK;
465+ break;
466+ case ST21NFCA_RF_READER_14443_3_A_GATE:
467+ /* ISO14443-3 type 1 or 2 tags */
468+ r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
469+ if (r < 0)
470+ return r;
471+ if (atqa == 0x000c) {
472+ target->supported_protocols = NFC_PROTO_JEWEL_MASK;
473+ target->sens_res = 0x0c00;
474+ } else {
475+ r = st21nfca_get_iso14443_3_sak(hdev, &sak);
476+ if (r < 0)
477+ return r;
478+
479+ r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
480+ if (r < 0)
481+ return r;
482+
483+ target->supported_protocols =
484+ nfc_hci_sak_to_protocol(sak);
485+ if (target->supported_protocols == 0xffffffff)
486+ return -EPROTO;
487+
488+ target->sens_res = atqa;
489+ target->sel_res = sak;
490+ memcpy(target->nfcid1, uid, len);
491+ target->nfcid1_len = len;
492+ }
493+
494+ break;
495+ case ST21NFCA_RF_READER_ISO15693_GATE:
496+ target->supported_protocols = NFC_PROTO_ISO15693_MASK;
497+ r = st21nfca_get_iso15693_inventory(hdev, target);
498+ if (r < 0)
499+ return r;
500+ break;
501+ default:
502+ return -EPROTO;
503+ }
504+
505+ return 0;
506+}
507+
508+#define ST21NFCA_CB_TYPE_READER_ISO15693 1
509+static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
510+ int err)
511+{
512+ struct st21nfca_hci_info *info = context;
513+
514+ switch (info->async_cb_type) {
515+ case ST21NFCA_CB_TYPE_READER_ISO15693:
516+ if (err == 0)
517+ skb_trim(skb, skb->len - 1);
518+ info->async_cb(info->async_cb_context, skb, err);
519+ break;
520+ default:
521+ if (err == 0)
522+ kfree_skb(skb);
523+ break;
524+ }
525+}
526+
527+/*
528+ * Returns:
529+ * <= 0: driver handled the data exchange
530+ * 1: driver doesn't especially handle, please do standard processing
531+ */
532+static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
533+ struct nfc_target *target,
534+ struct sk_buff *skb,
535+ data_exchange_cb_t cb, void *cb_context)
536+{
537+ struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
538+
539+ pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
540+ target->hci_reader_gate, skb->len);
541+
542+ switch (target->hci_reader_gate) {
543+ case ST21NFCA_RF_READER_F_GATE:
544+ *skb_push(skb, 1) = 0x1a;
545+ return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
546+ ST21NFCA_WR_XCHG_DATA, skb->data,
547+ skb->len, cb, cb_context);
548+ case ST21NFCA_RF_READER_14443_3_A_GATE:
549+ *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
550+
551+ return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
552+ ST21NFCA_WR_XCHG_DATA, skb->data,
553+ skb->len, cb, cb_context);
554+ case ST21NFCA_RF_READER_ISO15693_GATE:
555+ info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
556+ info->async_cb = cb;
557+ info->async_cb_context = cb_context;
558+
559+ *skb_push(skb, 1) = 0x17;
560+
561+ return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
562+ ST21NFCA_WR_XCHG_DATA, skb->data,
563+ skb->len,
564+ st21nfca_hci_data_exchange_cb,
565+ info);
566+ break;
567+ default:
568+ return 1;
569+ }
570+}
571+
572+static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
573+ struct nfc_target *target)
574+{
575+ u8 fwi = 0x11;
576+ switch (target->hci_reader_gate) {
577+ case NFC_HCI_RF_READER_A_GATE:
578+ case NFC_HCI_RF_READER_B_GATE:
579+ /*
580+ * PRESENCE_CHECK on those gates is available
581+ * However, the answer to this command is taking 3 * fwi
582+ * if the card is no present.
583+ * Instead, we send an empty I-Frame with a very short
584+ * configurable fwi ~604µs.
585+ */
586+ return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
587+ ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
588+ case ST21NFCA_RF_READER_14443_3_A_GATE:
589+ return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
590+ ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
591+ NULL, 0, NULL);
592+ default:
593+ return -EOPNOTSUPP;
594+ }
595+}
596+
597+static struct nfc_hci_ops st21nfca_hci_ops = {
598+ .open = st21nfca_hci_open,
599+ .close = st21nfca_hci_close,
600+ .load_session = st21nfca_hci_load_session,
601+ .hci_ready = st21nfca_hci_ready,
602+ .xmit = st21nfca_hci_xmit,
603+ .start_poll = st21nfca_hci_start_poll,
604+ .target_from_gate = st21nfca_hci_target_from_gate,
605+ .im_transceive = st21nfca_hci_im_transceive,
606+ .check_presence = st21nfca_hci_check_presence,
607+};
608+
609+int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
610+ char *llc_name, int phy_headroom, int phy_tailroom,
611+ int phy_payload, struct nfc_hci_dev **hdev)
612+{
613+ struct st21nfca_hci_info *info;
614+ int r = 0;
615+ int dev_num;
616+ u32 protocols;
617+ struct nfc_hci_init_data init_data;
618+ unsigned long quirks = 0;
619+
620+ info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
621+ if (!info) {
622+ r = -ENOMEM;
623+ goto err_alloc_hdev;
624+ }
625+
626+ info->phy_ops = phy_ops;
627+ info->phy_id = phy_id;
628+ info->state = ST21NFCA_ST_COLD;
629+ mutex_init(&info->info_lock);
630+
631+ init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
632+
633+ memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
634+
635+ /*
636+ * Session id must include the driver name + i2c bus addr
637+ * persistent info to discriminate 2 identical chips
638+ */
639+ dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
640+ if (dev_num >= ST21NFCA_NUM_DEVICES)
641+ goto err_alloc_hdev;
642+
643+ scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
644+ "ST21AH", dev_num);
645+
646+ protocols = NFC_PROTO_JEWEL_MASK |
647+ NFC_PROTO_MIFARE_MASK |
648+ NFC_PROTO_FELICA_MASK |
649+ NFC_PROTO_ISO14443_MASK |
650+ NFC_PROTO_ISO14443_B_MASK |
651+ NFC_PROTO_ISO15693_MASK;
652+
653+ set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
654+
655+ info->hdev =
656+ nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
657+ protocols, llc_name,
658+ phy_headroom + ST21NFCA_CMDS_HEADROOM,
659+ phy_tailroom, phy_payload);
660+
661+ if (!info->hdev) {
662+ pr_err("Cannot allocate nfc hdev.\n");
663+ r = -ENOMEM;
664+ goto err_alloc_hdev;
665+ }
666+
667+ nfc_hci_set_clientdata(info->hdev, info);
668+
669+ r = nfc_hci_register_device(info->hdev);
670+ if (r)
671+ goto err_regdev;
672+
673+ *hdev = info->hdev;
674+
675+ return 0;
676+
677+err_regdev:
678+ nfc_hci_free_device(info->hdev);
679+
680+err_alloc_hdev:
681+ kfree(info);
682+
683+ return r;
684+}
685+EXPORT_SYMBOL(st21nfca_hci_probe);
686+
687+void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
688+{
689+ struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
690+
691+ nfc_hci_unregister_device(hdev);
692+ nfc_hci_free_device(hdev);
693+ kfree(info);
694+}
695+EXPORT_SYMBOL(st21nfca_hci_remove);
696+
697+MODULE_LICENSE("GPL");
698+MODULE_DESCRIPTION(DRIVER_DESC);
drivers/nfc/st21nfca/st21nfca.h+87−0
@@ -0,0 +1,87 @@
1+/*
2+ * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
3+ *
4+ * This program is free software; you can redistribute it and/or modify it
5+ * under the terms and conditions of the GNU General Public License,
6+ * version 2, as published by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef __LOCAL_ST21NFCA_H_
18+#define __LOCAL_ST21NFCA_H_
19+
20+#include <net/nfc/hci.h>
21+
22+#define HCI_MODE 0
23+
24+/* framing in HCI mode */
25+#define ST21NFCA_SOF_EOF_LEN 2
26+
27+/* Almost every time value is 0 */
28+#define ST21NFCA_HCI_LLC_LEN 1
29+
30+/* Size in worst case :
31+ * In normal case CRC len = 2 but byte stuffing
32+ * may appear in case one CRC byte = ST21NFCA_SOF_EOF
33+ */
34+#define ST21NFCA_HCI_LLC_CRC 4
35+
36+#define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
37+ ST21NFCA_HCI_LLC_LEN + \
38+ ST21NFCA_HCI_LLC_CRC)
39+#define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
40+
41+/* Worst case when adding byte stuffing between each byte */
42+#define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
43+#define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
44+ ST21NFCA_HCI_LLC_MAX_PAYLOAD)
45+
46+#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
47+
48+#define ST21NFCA_HCI_MODE 0
49+
50+#define ST21NFCA_NUM_DEVICES 256
51+
52+int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
53+ char *llc_name, int phy_headroom, int phy_tailroom,
54+ int phy_payload, struct nfc_hci_dev **hdev);
55+void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
56+
57+enum st21nfca_state {
58+ ST21NFCA_ST_COLD,
59+ ST21NFCA_ST_READY,
60+};
61+
62+struct st21nfca_hci_info {
63+ struct nfc_phy_ops *phy_ops;
64+ void *phy_id;
65+
66+ struct nfc_hci_dev *hdev;
67+
68+ enum st21nfca_state state;
69+
70+ struct mutex info_lock;
71+
72+ int async_cb_type;
73+ data_exchange_cb_t async_cb;
74+ void *async_cb_context;
75+
76+} __packed;
77+
78+/* Reader RF commands */
79+#define ST21NFCA_WR_XCHG_DATA 0x10
80+
81+#define ST21NFCA_RF_READER_F_GATE 0x14
82+#define ST21NFCA_RF_READER_F_DATARATE 0x01
83+#define ST21NFCA_RF_READER_F_DATARATE_106 0x01
84+#define ST21NFCA_RF_READER_F_DATARATE_212 0x02
85+#define ST21NFCA_RF_READER_F_DATARATE_424 0x04
86+
87+#endif /* __LOCAL_ST21NFCA_H_ */
drivers/nfc/trf7970a.c+182−70
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616 #include <linux/device.h>
1717 #include <linux/netdevice.h>
1818 #include <linux/interrupt.h>
19+#include <linux/pm_runtime.h>
1920 #include <linux/nfc.h>
2021 #include <linux/skbuff.h>
2122 #include <linux/delay.h>
@@ -67,14 +68,14 @@ · 45 unmodified lines
6768 * only the SRX bit set, it means that all of the data has been received
6869 * (once what's in the fifo has been read). However, depending on timing
6970 * 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
71+ * those cases, the timeout mechanism is used to wait 20 ms in case more
72+ * data arrives. After 20 ms, it is assumed that all of the data has been
7273 * received and the accumulated rx data is sent upstream. The
7374 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
7475 * (i.e., it indicates that some data has been received but we're not sure
7576 * 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).
77+ * been received and there isn't an error). The delay is 20 ms since delays
78+ * of ~16 ms have been observed during testing.
7879 *
7980 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
8081 * Having only 4 bits in the FIFO won't normally generate an interrupt so
@@ -104,7 +105,9 @@ · 23 unmodified lines
104105
105106 #define TRF7970A_SUPPORTED_PROTOCOLS \
106107 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
107− NFC_PROTO_ISO15693_MASK)
108+ NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_ISO15693_MASK)
109+
110+#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
108111
109112 /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
110113 * on what the current framing is, the address of the TX length byte 1
@@ -120,7 +123,7 @@ · 9 unmodified lines
120123 /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
121124 #define TRF7970A_TX_MAX (4096 - 1)
122125
123−#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5
126+#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
124127 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
125128 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
126129
@@ -330,13 +333,15 @@ struct trf7970a { · 203 unmodified lines
330333 struct regulator *regulator;
331334 struct nfc_digital_dev *ddev;
332335 u32 quirks;
333− bool powering_up;
334336 bool aborting;
335337 struct sk_buff *tx_skb;
336338 struct sk_buff *rx_skb;
337339 nfc_digital_cmd_complete_t cb;
338340 void *cb_arg;
341+ u8 chip_status_ctrl;
339342 u8 iso_ctrl;
343+ u8 iso_ctrl_tech;
344+ u8 modulator_sys_clk_ctrl;
340345 u8 special_fcn_reg1;
341346 int technology;
342347 int framing;
@@ -681,7 +686,9 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id) · 338 unmodified lines
681686 trf->ignore_timeout =
682687 !cancel_delayed_work(&trf->timeout_work);
683688 trf7970a_drain_fifo(trf, status);
684− } else if (!(status & TRF7970A_IRQ_STATUS_TX)) {
689+ } else if (status == TRF7970A_IRQ_STATUS_TX) {
690+ trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
691+ } else {
685692 trf7970a_send_err_upstream(trf, -EIO);
686693 }
687694 break;
@@ -757,8 +764,8 @@ static int trf7970a_init(struct trf7970a *trf) · 69 unmodified lines
757764 if (ret)
758765 goto err_out;
759766
760− ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
761− TRF7970A_MODULATOR_DEPTH_OOK);
767+ /* Must clear NFC Target Detection Level reg due to erratum */
768+ ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
762769 if (ret)
763770 goto err_out;
764771
@@ -774,12 +781,7 @@ static int trf7970a_init(struct trf7970a *trf) · 9 unmodified lines
774781
775782 trf->special_fcn_reg1 = 0;
776783
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−
784+ trf->iso_ctrl = 0xff;
783785 return 0;
784786
785787 err_out:
@@ -791,53 +793,29 @@ static void trf7970a_switch_rf_off(struct trf7970a *trf) · 5 unmodified lines
791793 {
792794 dev_dbg(trf->dev, "Switching rf off\n");
793795
794− gpio_set_value(trf->en_gpio, 0);
795− gpio_set_value(trf->en2_gpio, 0);
796+ trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
797+
798+ trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
796799
797800 trf->aborting = false;
798801 trf->state = TRF7970A_ST_OFF;
802+
803+ pm_runtime_mark_last_busy(trf->dev);
804+ pm_runtime_put_autosuspend(trf->dev);
799805 }
800806
801−static int trf7970a_switch_rf_on(struct trf7970a *trf)
807+static void trf7970a_switch_rf_on(struct trf7970a *trf)
802808 {
803− unsigned long delay;
804− int ret;
805−
806809 dev_dbg(trf->dev, "Switching rf on\n");
807810
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);
811+ pm_runtime_get_sync(trf->dev);
814812
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;
813+ trf->state = TRF7970A_ST_IDLE;
835814 }
836815
837816 static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
838817 {
839818 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
840− int ret = 0;
841819
842820 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
843821
@@ -846,7 +824,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) · 2 unmodified lines
846824 if (on) {
847825 switch (trf->state) {
848826 case TRF7970A_ST_OFF:
849− ret = trf7970a_switch_rf_on(trf);
827+ trf7970a_switch_rf_on(trf);
850828 break;
851829 case TRF7970A_ST_IDLE:
852830 case TRF7970A_ST_IDLE_RX_BLOCKED:
@@ -871,7 +849,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) · 18 unmodified lines
871849 }
872850
873851 mutex_unlock(&trf->lock);
874− return ret;
852+ return 0;
875853 }
876854
877855 static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
@@ -882,10 +860,16 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) · 4 unmodified lines
882860
883861 switch (tech) {
884862 case NFC_DIGITAL_RF_TECH_106A:
885− trf->iso_ctrl = TRF7970A_ISO_CTRL_14443A_106;
863+ trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
864+ trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
865+ break;
866+ case NFC_DIGITAL_RF_TECH_106B:
867+ trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
868+ trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
886869 break;
887870 case NFC_DIGITAL_RF_TECH_ISO15693:
888− trf->iso_ctrl = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
871+ trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
872+ trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
889873 break;
890874 default:
891875 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech);
@@ -899,24 +883,29 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) · 7 unmodified lines
899883
900884 static int trf7970a_config_framing(struct trf7970a *trf, int framing)
901885 {
886+ u8 iso_ctrl = trf->iso_ctrl_tech;
887+ int ret;
888+
902889 dev_dbg(trf->dev, "framing: %d\n", framing);
903890
904891 switch (framing) {
905892 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
906893 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
907894 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
908− trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
895+ iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
909896 break;
910897 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
911898 case NFC_DIGITAL_FRAMING_NFCA_T4T:
899+ case NFC_DIGITAL_FRAMING_NFCB:
900+ case NFC_DIGITAL_FRAMING_NFCB_T4T:
912901 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
913902 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
914903 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
915− trf->iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
904+ iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
916905 break;
917906 case NFC_DIGITAL_FRAMING_NFCA_T2T:
918907 trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
919− trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
908+ iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
920909 break;
921910 default:
922911 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
@@ -925,24 +914,46 @@ static int trf7970a_config_framing(struct trf7970a *trf, int framing) · 2 unmodified lines
925914
926915 trf->framing = framing;
927916
928− return trf7970a_write(trf, TRF7970A_ISO_CTRL, trf->iso_ctrl);
917+ if (iso_ctrl != trf->iso_ctrl) {
918+ ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
919+ if (ret)
920+ return ret;
921+
922+ trf->iso_ctrl = iso_ctrl;
923+
924+ ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
925+ trf->modulator_sys_clk_ctrl);
926+ if (ret)
927+ return ret;
928+ }
929+
930+ if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
931+ ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
932+ trf->chip_status_ctrl |
933+ TRF7970A_CHIP_STATUS_RF_ON);
934+ if (ret)
935+ return ret;
936+
937+ trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
938+
939+ usleep_range(5000, 6000);
940+ }
941+
942+ return 0;
929943 }
930944
931945 static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
932946 int param)
933947 {
934948 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
935− int ret = 0;
949+ int ret;
936950
937951 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
938952
939953 mutex_lock(&trf->lock);
940954
941− if (trf->state == TRF7970A_ST_OFF) {
942− ret = trf7970a_switch_rf_on(trf);
943− if (ret)
944− goto err_out;
945− }
955+ if (trf->state == TRF7970A_ST_OFF)
956+ trf7970a_switch_rf_on(trf);
946957
947958 switch (type) {
948959 case NFC_DIGITAL_CONFIG_RF_TECH:
@@ -956,7 +967,6 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, · 7 unmodified lines
956967 ret = -EINVAL;
957968 }
958969
959−err_out:
960970 mutex_unlock(&trf->lock);
961971 return ret;
962972 }
@@ -1191,7 +1201,18 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) · 228 unmodified lines
11911201 dev_dbg(trf->dev, "Abort process initiated\n");
11921202
11931203 mutex_lock(&trf->lock);
1194− trf->aborting = true;
1204+
1205+ switch (trf->state) {
1206+ case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1207+ case TRF7970A_ST_WAIT_FOR_RX_DATA:
1208+ case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1209+ case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1210+ trf->aborting = true;
1211+ break;
1212+ default:
1213+ break;
1214+ }
1215+
11951216 mutex_unlock(&trf->lock);
11961217 }
11971218
@@ -1206,12 +1227,25 @@ static struct nfc_digital_ops trf7970a_nfc_ops = { · 8 unmodified lines
12061227 .abort_cmd = trf7970a_abort_cmd,
12071228 };
12081229
1230+static int trf7970a_get_autosuspend_delay(struct device_node *np)
1231+{
1232+ int autosuspend_delay, ret;
1233+
1234+ ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
1235+ if (ret)
1236+ autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
1237+
1238+ of_node_put(np);
1239+
1240+ return autosuspend_delay;
1241+}
1242+
12091243 static int trf7970a_probe(struct spi_device *spi)
12101244 {
12111245 struct device_node *np = spi->dev.of_node;
12121246 const struct spi_device_id *id = spi_get_device_id(spi);
12131247 struct trf7970a *trf;
1214− int ret;
1248+ int uvolts, autosuspend_delay, ret;
12151249
12161250 if (!np) {
12171251 dev_err(&spi->dev, "No Device Tree entry\n");
@@ -1281,7 +1315,10 @@ static int trf7970a_probe(struct spi_device *spi) · 63 unmodified lines
12811315 goto err_destroy_lock;
12821316 }
12831317
1284− trf->powering_up = true;
1318+ uvolts = regulator_get_voltage(trf->regulator);
1319+
1320+ if (uvolts > 4000000)
1321+ trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
12851322
12861323 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
12871324 TRF7970A_SUPPORTED_PROTOCOLS,
@@ -1297,6 +1334,12 @@ static int trf7970a_probe(struct spi_device *spi) · 9 unmodified lines
12971334 nfc_digital_set_drvdata(trf->ddev, trf);
12981335 spi_set_drvdata(spi, trf);
12991336
1337+ autosuspend_delay = trf7970a_get_autosuspend_delay(np);
1338+
1339+ pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
1340+ pm_runtime_use_autosuspend(trf->dev);
1341+ pm_runtime_enable(trf->dev);
1342+
13001343 ret = nfc_digital_register_device(trf->ddev);
13011344 if (ret) {
13021345 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
@@ -1307,6 +1350,7 @@ static int trf7970a_probe(struct spi_device *spi) · 4 unmodified lines
13071350 return 0;
13081351
13091352 err_free_ddev:
1353+ pm_runtime_disable(trf->dev);
13101354 nfc_digital_free_device(trf->ddev);
13111355 err_disable_regulator:
13121356 regulator_disable(trf->regulator);
@@ -1321,15 +1365,16 @@ static int trf7970a_remove(struct spi_device *spi) · 8 unmodified lines
13211365
13221366 mutex_lock(&trf->lock);
13231367
1324− trf7970a_switch_rf_off(trf);
1325− trf7970a_init(trf);
1326−
13271368 switch (trf->state) {
13281369 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
13291370 case TRF7970A_ST_WAIT_FOR_RX_DATA:
13301371 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
13311372 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
13321373 trf7970a_send_err_upstream(trf, -ECANCELED);
1374+ /* FALLTHROUGH */
1375+ case TRF7970A_ST_IDLE:
1376+ case TRF7970A_ST_IDLE_RX_BLOCKED:
1377+ pm_runtime_put_sync(trf->dev);
13331378 break;
13341379 default:
13351380 break;
@@ -1337,6 +1382,8 @@ static int trf7970a_remove(struct spi_device *spi) · 1 unmodified line
13371382
13381383 mutex_unlock(&trf->lock);
13391384
1385+ pm_runtime_disable(trf->dev);
1386+
13401387 nfc_digital_unregister_device(trf->ddev);
13411388 nfc_digital_free_device(trf->ddev);
13421389
@@ -1347,6 +1394,70 @@ static int trf7970a_remove(struct spi_device *spi) · 4 unmodified lines
13471394 return 0;
13481395 }
13491396
1397+#ifdef CONFIG_PM_RUNTIME
1398+static int trf7970a_pm_runtime_suspend(struct device *dev)
1399+{
1400+ struct spi_device *spi = container_of(dev, struct spi_device, dev);
1401+ struct trf7970a *trf = spi_get_drvdata(spi);
1402+ int ret;
1403+
1404+ dev_dbg(dev, "Runtime suspend\n");
1405+
1406+ if (trf->state != TRF7970A_ST_OFF) {
1407+ dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n",
1408+ trf->state);
1409+ return -EBUSY;
1410+ }
1411+
1412+ gpio_set_value(trf->en_gpio, 0);
1413+ gpio_set_value(trf->en2_gpio, 0);
1414+
1415+ ret = regulator_disable(trf->regulator);
1416+ if (ret)
1417+ dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret);
1418+
1419+ return ret;
1420+}
1421+
1422+static int trf7970a_pm_runtime_resume(struct device *dev)
1423+{
1424+ struct spi_device *spi = container_of(dev, struct spi_device, dev);
1425+ struct trf7970a *trf = spi_get_drvdata(spi);
1426+ int ret;
1427+
1428+ dev_dbg(dev, "Runtime resume\n");
1429+
1430+ ret = regulator_enable(trf->regulator);
1431+ if (ret) {
1432+ dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret);
1433+ return ret;
1434+ }
1435+
1436+ usleep_range(5000, 6000);
1437+
1438+ gpio_set_value(trf->en2_gpio, 1);
1439+ usleep_range(1000, 2000);
1440+ gpio_set_value(trf->en_gpio, 1);
1441+
1442+ usleep_range(20000, 21000);
1443+
1444+ ret = trf7970a_init(trf);
1445+ if (ret) {
1446+ dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret);
1447+ return ret;
1448+ }
1449+
1450+ pm_runtime_mark_last_busy(dev);
1451+
1452+ return 0;
1453+}
1454+#endif
1455+
1456+static const struct dev_pm_ops trf7970a_pm_ops = {
1457+ SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
1458+ trf7970a_pm_runtime_resume, NULL)
1459+};
1460+
13501461 static const struct spi_device_id trf7970a_id_table[] = {
13511462 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
13521463 { }
@@ -1360,6 +1471,7 @@ static struct spi_driver trf7970a_spi_driver = { · 7 unmodified lines
13601471 .driver = {
13611472 .name = "trf7970a",
13621473 .owner = THIS_MODULE,
1474+ .pm = &trf7970a_pm_ops,
13631475 },
13641476 };
13651477
include/linux/platform_data/st21nfca.h+32−0
@@ -0,0 +1,32 @@
1+/*
2+ * Driver include for the ST21NFCA NFC chip.
3+ *
4+ * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
5+ *
6+ * This program is free software; you can redistribute it and/or modify it
7+ * under the terms and conditions of the GNU General Public License,
8+ * version 2, as published by the Free Software Foundation.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
17+ */
18+
19+#ifndef _ST21NFCA_HCI_H_
20+#define _ST21NFCA_HCI_H_
21+
22+#include <linux/i2c.h>
23+
24+#define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
25+
26+struct st21nfca_nfc_platform_data {
27+ unsigned int gpio_irq;
28+ unsigned int gpio_ena;
29+ unsigned int irq_polarity;
30+};
31+
32+#endif /* _ST21NFCA_HCI_H_ */
include/net/nfc/digital.h+4−0
@@ -36,6 +36,7 @@ enum { · 35 unmodified lines
3636 NFC_DIGITAL_RF_TECH_212F,
3737 NFC_DIGITAL_RF_TECH_424F,
3838 NFC_DIGITAL_RF_TECH_ISO15693,
39+ NFC_DIGITAL_RF_TECH_106B,
3940
4041 NFC_DIGITAL_RF_TECH_LAST,
4142 };
@@ -62,6 +63,9 @@ enum { · 20 unmodified lines
6263 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
6364 NFC_DIGITAL_FRAMING_ISO15693_T5T,
6465
66+ NFC_DIGITAL_FRAMING_NFCB,
67+ NFC_DIGITAL_FRAMING_NFCB_T4T,
68+
6569 NFC_DIGITAL_FRAMING_LAST,
6670 };
6771
include/net/nfc/hci.h+1−0
@@ -27,6 +27,7 @@ struct nfc_hci_dev; · 26 unmodified lines
2727 struct nfc_hci_ops {
2828 int (*open) (struct nfc_hci_dev *hdev);
2929 void (*close) (struct nfc_hci_dev *hdev);
30+ int (*load_session) (struct nfc_hci_dev *hdev);
3031 int (*hci_ready) (struct nfc_hci_dev *hdev);
3132 /*
3233 * xmit must always send the complete buffer before
include/net/nfc/nfc.h+3−0
@@ -264,4 +264,7 @@ int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type); · 263 unmodified lines
264264 int nfc_remove_se(struct nfc_dev *dev, u32 se_idx);
265265 struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx);
266266
267+void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
268+ u8 payload_type, u8 direction);
269+
267270 #endif /* __NET_NFC_H */
include/uapi/linux/nfc.h+12−4
@@ -273,11 +273,19 @@ struct sockaddr_nfc_llcp { · 272 unmodified lines
273273 * First byte is the adapter index
274274 * Second byte contains flags
275275 * - 0x01 - Direction (0=RX, 1=TX)
276− * - 0x02-0x80 - Reserved
276+ * - 0x02-0x04 - Payload type (000=LLCP, 001=NCI, 010=HCI, 011=Digital,
277+ * 100=Proprietary)
278+ * - 0x05-0x80 - Reserved
277279 **/
278−#define NFC_LLCP_RAW_HEADER_SIZE 2
279−#define NFC_LLCP_DIRECTION_RX 0x00
280−#define NFC_LLCP_DIRECTION_TX 0x01
280+#define NFC_RAW_HEADER_SIZE 2
281+#define NFC_DIRECTION_RX 0x00
282+#define NFC_DIRECTION_TX 0x01
283+
284+#define RAW_PAYLOAD_LLCP 0
285+#define RAW_PAYLOAD_NCI 1
286+#define RAW_PAYLOAD_HCI 2
287+#define RAW_PAYLOAD_DIGITAL 3
288+#define RAW_PAYLOAD_PROPRIETARY 4
281289
282290 /* socket option names */
283291 #define NFC_LLCP_RW 0
net/nfc/digital.h+1−0
@@ -71,6 +71,7 @@ static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev, · 70 unmodified lines
7171 void digital_poll_next_tech(struct nfc_digital_dev *ddev);
7272
7373 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
74+int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech);
7475 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
7576 int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech);
7677
net/nfc/digital_core.c+18−2
@@ -22,6 +22,8 @@ · 21 unmodified lines
2222 #define DIGITAL_PROTO_NFCA_RF_TECH \
2323 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
2424
25+#define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
26+
2527 #define DIGITAL_PROTO_NFCF_RF_TECH \
2628 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
2729
@@ -345,6 +347,12 @@ int digital_target_found(struct nfc_digital_dev *ddev, · 317 unmodified lines
345347 add_crc = digital_skb_add_crc_a;
346348 break;
347349
350+ case NFC_PROTO_ISO14443_B:
351+ framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
352+ check_crc = digital_skb_check_crc_b;
353+ add_crc = digital_skb_add_crc_b;
354+ break;
355+
348356 default:
349357 pr_err("Invalid protocol %d\n", protocol);
350358 return -EINVAL;
@@ -475,6 +483,10 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, · 124 unmodified lines
475483 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
476484 digital_in_send_sens_req);
477485
486+ if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
487+ digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
488+ digital_in_send_sensb_req);
489+
478490 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
479491 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
480492 digital_in_send_sensf_req);
@@ -635,7 +647,8 @@ static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg, · 154 unmodified lines
635647 goto done;
636648 }
637649
638− if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
650+ if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
651+ (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
639652 rc = digital_in_iso_dep_pull_sod(ddev, resp);
640653 if (rc)
641654 goto done;
@@ -676,7 +689,8 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target, · 34 unmodified lines
676689 goto exit;
677690 }
678691
679− if (ddev->curr_protocol == NFC_PROTO_ISO14443) {
692+ if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
693+ (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
680694 rc = digital_in_iso_dep_push_sod(ddev, skb);
681695 if (rc)
682696 goto exit;
@@ -747,6 +761,8 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops, · 64 unmodified lines
747761 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
748762 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
749763 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
764+ if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
765+ ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
750766
751767 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
752768 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
net/nfc/digital_technology.c+228−2
@@ -41,6 +41,24 @@ · 40 unmodified lines
4141 #define DIGITAL_MIFARE_READ_RES_LEN 16
4242 #define DIGITAL_MIFARE_ACK_RES 0x0A
4343
44+#define DIGITAL_CMD_SENSB_REQ 0x05
45+#define DIGITAL_SENSB_ADVANCED BIT(5)
46+#define DIGITAL_SENSB_EXTENDED BIT(4)
47+#define DIGITAL_SENSB_ALLB_REQ BIT(3)
48+#define DIGITAL_SENSB_N(n) ((n) & 0x7)
49+
50+#define DIGITAL_CMD_SENSB_RES 0x50
51+
52+#define DIGITAL_CMD_ATTRIB_REQ 0x1D
53+#define DIGITAL_ATTRIB_P1_TR0_DEFAULT (0x0 << 6)
54+#define DIGITAL_ATTRIB_P1_TR1_DEFAULT (0x0 << 4)
55+#define DIGITAL_ATTRIB_P1_SUPRESS_EOS BIT(3)
56+#define DIGITAL_ATTRIB_P1_SUPRESS_SOS BIT(2)
57+#define DIGITAL_ATTRIB_P2_LISTEN_POLL_1 (0x0 << 6)
58+#define DIGITAL_ATTRIB_P2_POLL_LISTEN_1 (0x0 << 4)
59+#define DIGITAL_ATTRIB_P2_MAX_FRAME_256 0x8
60+#define DIGITAL_ATTRIB_P4_DID(n) ((n) & 0xf)
61+
4462 #define DIGITAL_CMD_SENSF_REQ 0x00
4563 #define DIGITAL_CMD_SENSF_RES 0x01
4664
@@ -75,6 +93,7 @@ static const u8 digital_ats_fsc[] = { · 28 unmodified lines
7593 };
7694
7795 #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
96+#define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
7897 #define DIGITAL_ATS_MAX_FSC 256
7998
8099 #define DIGITAL_RATS_BYTE1 0xE0
@@ -92,6 +111,32 @@ struct digital_sel_req { · 11 unmodified lines
92111 u8 bcc;
93112 } __packed;
94113
114+struct digital_sensb_req {
115+ u8 cmd;
116+ u8 afi;
117+ u8 param;
118+} __packed;
119+
120+struct digital_sensb_res {
121+ u8 cmd;
122+ u8 nfcid0[4];
123+ u8 app_data[4];
124+ u8 proto_info[3];
125+} __packed;
126+
127+struct digital_attrib_req {
128+ u8 cmd;
129+ u8 nfcid0[4];
130+ u8 param1;
131+ u8 param2;
132+ u8 param3;
133+ u8 param4;
134+} __packed;
135+
136+struct digital_attrib_res {
137+ u8 mbli_did;
138+} __packed;
139+
95140 struct digital_sensf_req {
96141 u8 cmd;
97142 u8 sc1;
@@ -531,6 +576,175 @@ int digital_in_recv_mifare_res(struct sk_buff *resp) · 433 unmodified lines
531576 return -EIO;
532577 }
533578
579+static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
580+ struct sk_buff *resp)
581+{
582+ struct nfc_target *target = arg;
583+ struct digital_attrib_res *attrib_res;
584+ int rc;
585+
586+ if (IS_ERR(resp)) {
587+ rc = PTR_ERR(resp);
588+ resp = NULL;
589+ goto exit;
590+ }
591+
592+ if (resp->len < sizeof(*attrib_res)) {
593+ PROTOCOL_ERR("12.6.2");
594+ rc = -EIO;
595+ goto exit;
596+ }
597+
598+ attrib_res = (struct digital_attrib_res *)resp->data;
599+
600+ if (attrib_res->mbli_did & 0x0f) {
601+ PROTOCOL_ERR("12.6.2.1");
602+ rc = -EIO;
603+ goto exit;
604+ }
605+
606+ rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
607+
608+exit:
609+ dev_kfree_skb(resp);
610+ kfree(target);
611+
612+ if (rc)
613+ digital_poll_next_tech(ddev);
614+}
615+
616+int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
617+ struct nfc_target *target,
618+ struct digital_sensb_res *sensb_res)
619+{
620+ struct digital_attrib_req *attrib_req;
621+ struct sk_buff *skb;
622+ int rc;
623+
624+ skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
625+ if (!skb)
626+ return -ENOMEM;
627+
628+ attrib_req = (struct digital_attrib_req *)skb_put(skb,
629+ sizeof(*attrib_req));
630+
631+ attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
632+ memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
633+ sizeof(attrib_req->nfcid0));
634+ attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
635+ DIGITAL_ATTRIB_P1_TR1_DEFAULT;
636+ attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
637+ DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
638+ DIGITAL_ATTRIB_P2_MAX_FRAME_256;
639+ attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
640+ attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
641+
642+ rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
643+ target);
644+ if (rc)
645+ kfree_skb(skb);
646+
647+ return rc;
648+}
649+
650+static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
651+ struct sk_buff *resp)
652+{
653+ struct nfc_target *target = NULL;
654+ struct digital_sensb_res *sensb_res;
655+ u8 fsci;
656+ int rc;
657+
658+ if (IS_ERR(resp)) {
659+ rc = PTR_ERR(resp);
660+ resp = NULL;
661+ goto exit;
662+ }
663+
664+ if (resp->len != sizeof(*sensb_res)) {
665+ PROTOCOL_ERR("5.6.2.1");
666+ rc = -EIO;
667+ goto exit;
668+ }
669+
670+ sensb_res = (struct digital_sensb_res *)resp->data;
671+
672+ if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
673+ PROTOCOL_ERR("5.6.2");
674+ rc = -EIO;
675+ goto exit;
676+ }
677+
678+ if (!(sensb_res->proto_info[1] & BIT(0))) {
679+ PROTOCOL_ERR("5.6.2.12");
680+ rc = -EIO;
681+ goto exit;
682+ }
683+
684+ if (sensb_res->proto_info[1] & BIT(3)) {
685+ PROTOCOL_ERR("5.6.2.16");
686+ rc = -EIO;
687+ goto exit;
688+ }
689+
690+ fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
691+ if (fsci >= 8)
692+ ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
693+ else
694+ ddev->target_fsc = digital_ats_fsc[fsci];
695+
696+ target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
697+ if (!target) {
698+ rc = -ENOMEM;
699+ goto exit;
700+ }
701+
702+ rc = digital_in_send_attrib_req(ddev, target, sensb_res);
703+
704+exit:
705+ dev_kfree_skb(resp);
706+
707+ if (rc) {
708+ kfree(target);
709+ digital_poll_next_tech(ddev);
710+ }
711+}
712+
713+int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
714+{
715+ struct digital_sensb_req *sensb_req;
716+ struct sk_buff *skb;
717+ int rc;
718+
719+ rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
720+ NFC_DIGITAL_RF_TECH_106B);
721+ if (rc)
722+ return rc;
723+
724+ rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
725+ NFC_DIGITAL_FRAMING_NFCB);
726+ if (rc)
727+ return rc;
728+
729+ skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
730+ if (!skb)
731+ return -ENOMEM;
732+
733+ sensb_req = (struct digital_sensb_req *)skb_put(skb,
734+ sizeof(*sensb_req));
735+
736+ sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
737+ sensb_req->afi = 0x00; /* All families and sub-families */
738+ sensb_req->param = DIGITAL_SENSB_N(0);
739+
740+ rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
741+ NULL);
742+ if (rc)
743+ kfree_skb(skb);
744+
745+ return rc;
746+}
747+
534748 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
535749 struct sk_buff *resp)
536750 {
@@ -877,6 +1091,18 @@ exit: · 340 unmodified lines
8771091 dev_kfree_skb(resp);
8781092 }
8791093
1094+static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1095+ void *arg, struct sk_buff *resp)
1096+{
1097+ if (!IS_ERR(resp) && (resp->len >= 2) &&
1098+ (resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1099+ digital_tg_recv_sensf_req(ddev, arg, resp);
1100+ else
1101+ digital_tg_recv_atr_req(ddev, arg, resp);
1102+
1103+ return;
1104+}
1105+
8801106 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
8811107 struct digital_sensf_req *sensf_req)
8821108 {
@@ -887,7 +1113,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, · 4 unmodified lines
8871113
8881114 size = sizeof(struct digital_sensf_res);
8891115
890− if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE)
1116+ if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
8911117 size -= sizeof(sensf_res->rd);
8921118
8931119 skb = digital_skb_alloc(ddev, size);
@@ -922,7 +1148,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, · 28 unmodified lines
9221148 digital_skb_add_crc_f(skb);
9231149
9241150 rc = digital_tg_send_cmd(ddev, skb, 300,
925− digital_tg_recv_atr_req, NULL);
1151+ digital_tg_recv_atr_or_sensf_req, NULL);
9261152 if (rc)
9271153 kfree_skb(skb);
9281154
net/nfc/hci/command.c+4−2
@@ -26,6 +26,8 @@ · 25 unmodified lines
2626
2727 #include "hci.h"
2828
29+#define MAX_FWI 4949
30+
2931 static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
3032 const u8 *param, size_t param_len,
3133 data_exchange_cb_t cb, void *cb_context)
@@ -37,7 +39,7 @@ static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, · 5 unmodified lines
3739 * for all commands?
3840 */
3941 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd,
40− param, param_len, cb, cb_context, 3000);
42+ param, param_len, cb, cb_context, MAX_FWI);
4143 }
4244
4345 /*
@@ -82,7 +84,7 @@ static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, · 38 unmodified lines
8284 NFC_HCI_HCP_COMMAND, cmd,
8385 param, param_len,
8486 nfc_hci_execute_cb, &hcp_ew,
85− 3000);
87+ MAX_FWI);
8688 if (hcp_ew.exec_result < 0)
8789 return hcp_ew.exec_result;
8890
net/nfc/hci/core.c+22−25
@@ -225,7 +225,7 @@ int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate) · 224 unmodified lines
225225 goto exit;
226226 }
227227
228− targets->sens_res = be16_to_cpu(*(u16 *)atqa_skb->data);
228+ targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
229229 targets->sel_res = sak_skb->data[0];
230230
231231 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE,
@@ -380,34 +380,31 @@ static int hci_dev_session_init(struct nfc_hci_dev *hdev) · 148 unmodified lines
380380 if (r < 0)
381381 goto disconnect_all;
382382
383− if (skb->len && skb->len == strlen(hdev->init_data.session_id))
384− if (memcmp(hdev->init_data.session_id, skb->data,
385− skb->len) == 0) {
386− /* TODO ELa: restore gate<->pipe table from
387− * some TBD location.
388− * note: it doesn't seem possible to get the chip
389− * currently open gate/pipe table.
390− * It is only possible to obtain the supported
391− * gate list.
392− */
383+ if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
384+ (memcmp(hdev->init_data.session_id, skb->data,
385+ skb->len) == 0) && hdev->ops->load_session) {
386+ /* Restore gate<->pipe table from some proprietary location. */
393387
394− /* goto exit
395− * For now, always do a full initialization */
396− }
388+ r = hdev->ops->load_session(hdev);
397389
398− r = nfc_hci_disconnect_all_gates(hdev);
399− if (r < 0)
400− goto exit;
390+ if (r < 0)
391+ goto disconnect_all;
392+ } else {
401393
402− r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
403− hdev->init_data.gates);
404− if (r < 0)
405− goto disconnect_all;
394+ r = nfc_hci_disconnect_all_gates(hdev);
395+ if (r < 0)
396+ goto exit;
406397
407− r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
408− NFC_HCI_ADMIN_SESSION_IDENTITY,
409− hdev->init_data.session_id,
410− strlen(hdev->init_data.session_id));
398+ r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
399+ hdev->init_data.gates);
400+ if (r < 0)
401+ goto disconnect_all;
402+
403+ r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
404+ NFC_HCI_ADMIN_SESSION_IDENTITY,
405+ hdev->init_data.session_id,
406+ strlen(hdev->init_data.session_id));
407+ }
411408 if (r == 0)
412409 goto exit;
413410
net/nfc/llcp_commands.c+1−1
@@ -387,7 +387,7 @@ int nfc_llcp_send_symm(struct nfc_dev *dev) · 386 unmodified lines
387387
388388 __net_timestamp(skb);
389389
390− nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX);
390+ nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
391391
392392 return nfc_data_exchange(dev, local->target_idx, skb,
393393 nfc_llcp_recv, local);
net/nfc/llcp_core.c+6−5
@@ -680,16 +680,17 @@ void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, · 679 unmodified lines
680680 continue;
681681
682682 if (skb_copy == NULL) {
683− skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE,
683+ skb_copy = __pskb_copy(skb, NFC_RAW_HEADER_SIZE,
684684 GFP_ATOMIC);
685685
686686 if (skb_copy == NULL)
687687 continue;
688688
689− data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE);
689+ data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
690690
691691 data[0] = local->dev ? local->dev->idx : 0xFF;
692− data[1] = direction;
692+ data[1] = direction & 0x01;
693+ data[1] |= (RAW_PAYLOAD_LLCP << 1);
693694 }
694695
695696 nskb = skb_clone(skb_copy, GFP_ATOMIC);
@@ -747,7 +748,7 @@ static void nfc_llcp_tx_work(struct work_struct *work) · 51 unmodified lines
747748 __net_timestamp(skb);
748749
749750 nfc_llcp_send_to_raw_sock(local, skb,
750− NFC_LLCP_DIRECTION_TX);
751+ NFC_DIRECTION_TX);
751752
752753 ret = nfc_data_exchange(local->dev, local->target_idx,
753754 skb, nfc_llcp_recv, local);
@@ -1476,7 +1477,7 @@ static void nfc_llcp_rx_work(struct work_struct *work) · 722 unmodified lines
14761477
14771478 __net_timestamp(skb);
14781479
1479− nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX);
1480+ nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
14801481
14811482 nfc_llcp_rx_skb(local, skb);
14821483
net/nfc/nci/core.c+9−0
@@ -861,6 +861,10 @@ static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb) · 860 unmodified lines
861861 /* Get rid of skb owner, prior to sending to the driver. */
862862 skb_orphan(skb);
863863
864+ /* Send copy to sniffer */
865+ nfc_send_to_raw_sock(ndev->nfc_dev, skb,
866+ RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
867+
864868 return ndev->ops->send(ndev, skb);
865869 }
866870
@@ -935,6 +939,11 @@ static void nci_rx_work(struct work_struct *work) · 68 unmodified lines
935939 struct sk_buff *skb;
936940
937941 while ((skb = skb_dequeue(&ndev->rx_q))) {
942+
943+ /* Send copy to sniffer */
944+ nfc_send_to_raw_sock(ndev->nfc_dev, skb,
945+ RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
946+
938947 /* Process frame */
939948 switch (nci_mt(skb->data)) {
940949 case NCI_MT_RSP_PKT:
net/nfc/nci/ntf.c+2−5
@@ -366,7 +366,6 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev, · 365 unmodified lines
366366 struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
367367 {
368368 struct activation_params_poll_nfc_dep *poll;
369− int i;
370369
371370 switch (ntf->activation_rf_tech_and_mode) {
372371 case NCI_NFC_A_PASSIVE_POLL_MODE:
@@ -374,10 +373,8 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev, · 1 unmodified line
374373 poll = &ntf->activation_params.poll_nfc_dep;
375374 poll->atr_res_len = min_t(__u8, *data++, 63);
376375 pr_debug("atr_res_len %d\n", poll->atr_res_len);
377− if (poll->atr_res_len > 0) {
378− for (i = 0; i < poll->atr_res_len; i++)
379− poll->atr_res[poll->atr_res_len-1-i] = data[i];
380− }
376+ if (poll->atr_res_len > 0)
377+ memcpy(poll->atr_res, data, poll->atr_res_len);
381378 break;
382379
383380 default:
net/nfc/nfc.h+6−0
@@ -40,6 +40,12 @@ struct nfc_rawsock { · 39 unmodified lines
4040 struct work_struct tx_work;
4141 bool tx_work_scheduled;
4242 };
43+
44+struct nfc_sock_list {
45+ struct hlist_head head;
46+ rwlock_t lock;
47+};
48+
4349 #define nfc_rawsock(sk) ((struct nfc_rawsock *) sk)
4450 #define to_rawsock_sk(_tx_work) \
4551 ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work))
net/nfc/rawsock.c+89−5
@@ -27,6 +27,24 @@ · 26 unmodified lines
2727
2828 #include "nfc.h"
2929
30+static struct nfc_sock_list raw_sk_list = {
31+ .lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
32+};
33+
34+void nfc_sock_link(struct nfc_sock_list *l, struct sock *sk)
35+{
36+ write_lock(&l->lock);
37+ sk_add_node(sk, &l->head);
38+ write_unlock(&l->lock);
39+}
40+
41+void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk)
42+{
43+ write_lock(&l->lock);
44+ sk_del_node_init(sk);
45+ write_unlock(&l->lock);
46+}
47+
3048 static void rawsock_write_queue_purge(struct sock *sk)
3149 {
3250 pr_debug("sk=%p\n", sk);
@@ -57,6 +75,9 @@ static int rawsock_release(struct socket *sock) · 24 unmodified lines
5775 if (!sk)
5876 return 0;
5977
78+ if (sock->type == SOCK_RAW)
79+ nfc_sock_unlink(&raw_sk_list, sk);
80+
6081 sock_orphan(sk);
6182 sock_put(sk);
6283
@@ -275,6 +296,26 @@ static const struct proto_ops rawsock_ops = { · 212 unmodified lines
275296 .mmap = sock_no_mmap,
276297 };
277298
299+static const struct proto_ops rawsock_raw_ops = {
300+ .family = PF_NFC,
301+ .owner = THIS_MODULE,
302+ .release = rawsock_release,
303+ .bind = sock_no_bind,
304+ .connect = sock_no_connect,
305+ .socketpair = sock_no_socketpair,
306+ .accept = sock_no_accept,
307+ .getname = sock_no_getname,
308+ .poll = datagram_poll,
309+ .ioctl = sock_no_ioctl,
310+ .listen = sock_no_listen,
311+ .shutdown = sock_no_shutdown,
312+ .setsockopt = sock_no_setsockopt,
313+ .getsockopt = sock_no_getsockopt,
314+ .sendmsg = sock_no_sendmsg,
315+ .recvmsg = rawsock_recvmsg,
316+ .mmap = sock_no_mmap,
317+};
318+
278319 static void rawsock_destruct(struct sock *sk)
279320 {
280321 pr_debug("sk=%p\n", sk);
@@ -300,10 +341,13 @@ static int rawsock_create(struct net *net, struct socket *sock, · 19 unmodified lines
300341
301342 pr_debug("sock=%p\n", sock);
302343
303− if (sock->type != SOCK_SEQPACKET)
344+ if ((sock->type != SOCK_SEQPACKET) && (sock->type != SOCK_RAW))
304345 return -ESOCKTNOSUPPORT;
305346
306− sock->ops = &rawsock_ops;
347+ if (sock->type == SOCK_RAW)
348+ sock->ops = &rawsock_raw_ops;
349+ else
350+ sock->ops = &rawsock_ops;
307351
308352 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
309353 if (!sk)
@@ -313,13 +357,53 @@ static int rawsock_create(struct net *net, struct socket *sock, · 3 unmodified lines
313357 sk->sk_protocol = nfc_proto->id;
314358 sk->sk_destruct = rawsock_destruct;
315359 sock->state = SS_UNCONNECTED;
316−
317− INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
318− nfc_rawsock(sk)->tx_work_scheduled = false;
360+ if (sock->type == SOCK_RAW)
361+ nfc_sock_link(&raw_sk_list, sk);
362+ else {
363+ INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
364+ nfc_rawsock(sk)->tx_work_scheduled = false;
365+ }
319366
320367 return 0;
321368 }
322369
370+void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
371+ u8 payload_type, u8 direction)
372+{
373+ struct sk_buff *skb_copy = NULL, *nskb;
374+ struct sock *sk;
375+ u8 *data;
376+
377+ read_lock(&raw_sk_list.lock);
378+
379+ sk_for_each(sk, &raw_sk_list.head) {
380+ if (!skb_copy) {
381+ skb_copy = __pskb_copy(skb, NFC_RAW_HEADER_SIZE,
382+ GFP_ATOMIC);
383+ if (!skb_copy)
384+ continue;
385+
386+ data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
387+
388+ data[0] = dev ? dev->idx : 0xFF;
389+ data[1] = direction & 0x01;
390+ data[1] |= (payload_type << 1);
391+ }
392+
393+ nskb = skb_clone(skb_copy, GFP_ATOMIC);
394+ if (!nskb)
395+ continue;
396+
397+ if (sock_queue_rcv_skb(sk, nskb))
398+ kfree_skb(nskb);
399+ }
400+
401+ read_unlock(&raw_sk_list.lock);
402+
403+ kfree_skb(skb_copy);
404+}
405+EXPORT_SYMBOL(nfc_send_to_raw_sock);
406+
323407 static struct proto rawsock_proto = {
324408 .name = "NFC_RAW",
325409 .owner = THIS_MODULE,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t