aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn W. Linville <linville@tuxdriver.com>2014-05-22 13:56:46 -0400
committerJohn W. Linville <linville@tuxdriver.com>2014-05-22 13:56:46 -0400
commit99abe65ff18b6bbac2e55524827b571c3eccfa86 (patch)
treeead3a34e03ea6f7900d98376e3f37f0571144fad
parent96810471673393c931595a013f0f3094b564b1e9 (diff)
parent7974728094d35f38775417a26d8f30ea3602496a (diff)
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." Signed-off-by: John W. Linville <linville@tuxdriver.com>
-rw-r--r--Documentation/devicetree/bindings/net/nfc/pn544.txt35
-rw-r--r--Documentation/devicetree/bindings/net/nfc/st21nfca.txt33
-rw-r--r--Documentation/devicetree/bindings/net/nfc/trf7970a.txt2
-rw-r--r--drivers/nfc/Kconfig1
-rw-r--r--drivers/nfc/Makefile1
-rw-r--r--drivers/nfc/pn544/i2c.c154
-rw-r--r--drivers/nfc/st21nfca/Kconfig23
-rw-r--r--drivers/nfc/st21nfca/Makefile8
-rw-r--r--drivers/nfc/st21nfca/i2c.c724
-rw-r--r--drivers/nfc/st21nfca/st21nfca.c698
-rw-r--r--drivers/nfc/st21nfca/st21nfca.h87
-rw-r--r--drivers/nfc/trf7970a.c252
-rw-r--r--include/linux/platform_data/st21nfca.h32
-rw-r--r--include/net/nfc/digital.h4
-rw-r--r--include/net/nfc/hci.h1
-rw-r--r--include/net/nfc/nfc.h3
-rw-r--r--include/uapi/linux/nfc.h16
-rw-r--r--net/nfc/digital.h1
-rw-r--r--net/nfc/digital_core.c20
-rw-r--r--net/nfc/digital_technology.c230
-rw-r--r--net/nfc/hci/command.c6
-rw-r--r--net/nfc/hci/core.c47
-rw-r--r--net/nfc/llcp_commands.c2
-rw-r--r--net/nfc/llcp_core.c11
-rw-r--r--net/nfc/nci/core.c9
-rw-r--r--net/nfc/nci/ntf.c7
-rw-r--r--net/nfc/nfc.h6
-rw-r--r--net/nfc/rawsock.c94
28 files changed, 2368 insertions, 139 deletions
diff --git a/Documentation/devicetree/bindings/net/nfc/pn544.txt b/Documentation/devicetree/bindings/net/nfc/pn544.txt
new file mode 100644
index 000000000000..dab69f36167c
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/nfc/pn544.txt
@@ -0,0 +1,35 @@
1* NXP Semiconductors PN544 NFC Controller
2
3Required 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
12Optional SoC Specific Properties:
13- pinctrl-names: Contains only one value - "default".
14- pintctrl-0: Specifies the pin control groups used for this controller.
15
16Example (for ARM-based BeagleBone with 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};
diff --git a/Documentation/devicetree/bindings/net/nfc/st21nfca.txt b/Documentation/devicetree/bindings/net/nfc/st21nfca.txt
new file mode 100644
index 000000000000..4724fe669172
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/nfc/st21nfca.txt
@@ -0,0 +1,33 @@
1* STMicroelectronics SAS. ST21NFCA NFC Controller
2
3Required 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
11Optional SoC Specific Properties:
12- pinctrl-names: Contains only one value - "default".
13- pintctrl-0: Specifies the pin control groups used for this controller.
14
15Example (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};
diff --git a/Documentation/devicetree/bindings/net/nfc/trf7970a.txt b/Documentation/devicetree/bindings/net/nfc/trf7970a.txt
index 8dd3ef7bc56b..1e436133685f 100644
--- a/Documentation/devicetree/bindings/net/nfc/trf7970a.txt
+++ b/Documentation/devicetree/bindings/net/nfc/trf7970a.txt
@@ -12,6 +12,7 @@ Required properties:
12Optional SoC Specific Properties: 12Optional SoC Specific Properties:
13- pinctrl-names: Contains only one value - "default". 13- pinctrl-names: Contains only one value - "default".
14- pintctrl-0: Specifies the pin control groups used for this controller. 14- pintctrl-0: Specifies the pin control groups used for this controller.
15- autosuspend-delay: Specify autosuspend delay in milliseconds.
15 16
16Example (for ARM-based BeagleBone with TRF7970A on SPI1): 17Example (for ARM-based BeagleBone with TRF7970A on SPI1):
17 18
@@ -29,6 +30,7 @@ Example (for ARM-based BeagleBone with TRF7970A on SPI1):
29 ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>, 30 ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>,
30 <&gpio2 5 GPIO_ACTIVE_LOW>; 31 <&gpio2 5 GPIO_ACTIVE_LOW>;
31 vin-supply = <&ldo3_reg>; 32 vin-supply = <&ldo3_reg>;
33 autosuspend-delay = <30000>;
32 status = "okay"; 34 status = "okay";
33 }; 35 };
34}; 36};
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index 65d4ca19d132..26c66a126551 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -71,5 +71,6 @@ config NFC_PORT100
71source "drivers/nfc/pn544/Kconfig" 71source "drivers/nfc/pn544/Kconfig"
72source "drivers/nfc/microread/Kconfig" 72source "drivers/nfc/microread/Kconfig"
73source "drivers/nfc/nfcmrvl/Kconfig" 73source "drivers/nfc/nfcmrvl/Kconfig"
74source "drivers/nfc/st21nfca/Kconfig"
74 75
75endmenu 76endmenu
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index ae42a3fa60c9..23225b0287fd 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o
11obj-$(CONFIG_NFC_PORT100) += port100.o 11obj-$(CONFIG_NFC_PORT100) += port100.o
12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/ 12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o 13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
14obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
14 15
15ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG 16ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c
index f2acd85be86e..440291ab7263 100644
--- a/drivers/nfc/pn544/i2c.c
+++ b/drivers/nfc/pn544/i2c.c
@@ -22,6 +22,8 @@
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/i2c.h> 23#include <linux/i2c.h>
24#include <linux/gpio.h> 24#include <linux/gpio.h>
25#include <linux/of_gpio.h>
26#include <linux/of_irq.h>
25#include <linux/miscdevice.h> 27#include <linux/miscdevice.h>
26#include <linux/interrupt.h> 28#include <linux/interrupt.h>
27#include <linux/delay.h> 29#include <linux/delay.h>
@@ -857,6 +859,92 @@ exit_state_wait_secure_write_answer:
857 } 859 }
858} 860}
859 861
862#ifdef CONFIG_OF
863
864static 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
931err_gpio_fw:
932 gpio_free(phy->gpio_fw);
933err_gpio_en:
934 gpio_free(phy->gpio_en);
935err_dt:
936 return ret;
937}
938
939#else
940
941static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
942{
943 return -ENODEV;
944}
945
946#endif
947
860static int pn544_hci_i2c_probe(struct i2c_client *client, 948static int pn544_hci_i2c_probe(struct i2c_client *client,
861 const struct i2c_device_id *id) 949 const struct i2c_device_id *id)
862{ 950{
@@ -887,25 +975,36 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
887 i2c_set_clientdata(client, phy); 975 i2c_set_clientdata(client, phy);
888 976
889 pdata = client->dev.platform_data; 977 pdata = client->dev.platform_data;
890 if (pdata == NULL) {
891 nfc_err(&client->dev, "No platform data\n");
892 return -EINVAL;
893 }
894 978
895 if (pdata->request_resources == NULL) { 979 /* No platform data, using device tree. */
896 nfc_err(&client->dev, "request_resources() missing\n"); 980 if (!pdata && client->dev.of_node) {
897 return -EINVAL; 981 r = pn544_hci_i2c_of_request_resources(client);
898 } 982 if (r) {
983 nfc_err(&client->dev, "No DT data\n");
984 return r;
985 }
986 /* Using platform data. */
987 } else if (pdata) {
899 988
900 r = pdata->request_resources(client); 989 if (pdata->request_resources == NULL) {
901 if (r) { 990 nfc_err(&client->dev, "request_resources() missing\n");
902 nfc_err(&client->dev, "Cannot get platform resources\n"); 991 return -EINVAL;
903 return r; 992 }
904 }
905 993
906 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); 994 r = pdata->request_resources(client);
907 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); 995 if (r) {
908 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); 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 }
909 1008
910 pn544_hci_i2c_platform_init(phy); 1009 pn544_hci_i2c_platform_init(phy);
911 1010
@@ -930,8 +1029,12 @@ err_hci:
930 free_irq(client->irq, phy); 1029 free_irq(client->irq, phy);
931 1030
932err_rti: 1031err_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) {
934 pdata->free_resources(); 1036 pdata->free_resources();
1037 }
935 1038
936 return r; 1039 return r;
937} 1040}
@@ -953,15 +1056,30 @@ static int pn544_hci_i2c_remove(struct i2c_client *client)
953 pn544_hci_i2c_disable(phy); 1056 pn544_hci_i2c_disable(phy);
954 1057
955 free_irq(client->irq, phy); 1058 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) {
957 pdata->free_resources(); 1066 pdata->free_resources();
1067 }
958 1068
959 return 0; 1069 return 0;
960} 1070}
961 1071
1072static const struct of_device_id of_pn544_i2c_match[] = {
1073 { .compatible = "nxp,pn544-i2c", },
1074 {},
1075};
1076MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
1077
962static struct i2c_driver pn544_hci_i2c_driver = { 1078static struct i2c_driver pn544_hci_i2c_driver = {
963 .driver = { 1079 .driver = {
964 .name = PN544_HCI_I2C_DRIVER_NAME, 1080 .name = PN544_HCI_I2C_DRIVER_NAME,
1081 .owner = THIS_MODULE,
1082 .of_match_table = of_match_ptr(of_pn544_i2c_match),
965 }, 1083 },
966 .probe = pn544_hci_i2c_probe, 1084 .probe = pn544_hci_i2c_probe,
967 .id_table = pn544_hci_i2c_id_table, 1085 .id_table = pn544_hci_i2c_id_table,
diff --git a/drivers/nfc/st21nfca/Kconfig b/drivers/nfc/st21nfca/Kconfig
new file mode 100644
index 000000000000..ee459f066ade
--- /dev/null
+++ b/drivers/nfc/st21nfca/Kconfig
@@ -0,0 +1,23 @@
1config 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
15config 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.
diff --git a/drivers/nfc/st21nfca/Makefile b/drivers/nfc/st21nfca/Makefile
new file mode 100644
index 000000000000..038ed093a119
--- /dev/null
+++ b/drivers/nfc/st21nfca/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for ST21NFCA HCI based NFC driver
3#
4
5st21nfca_i2c-objs = i2c.o
6
7obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o
8obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
diff --git a/drivers/nfc/st21nfca/i2c.c b/drivers/nfc/st21nfca/i2c.c
new file mode 100644
index 000000000000..3f954ed86d98
--- /dev/null
+++ b/drivers/nfc/st21nfca/i2c.c
@@ -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
63static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
64 {ST21NFCA_HCI_DRIVER_NAME, 0},
65 {}
66};
67
68MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
69
70struct 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};
96static u8 len_seq[] = { 13, 24, 15, 29 };
97static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
98
99#define I2C_DUMP_SKB(info, skb) \
100do { \
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 */
112static 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
151static 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
164static 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
174static 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
191static 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 */
202static 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
271static 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
283static 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 */
311static 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 */
366static 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 */
441static 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
501static 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
508static 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
554static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
555{
556 return -ENODEV;
557}
558#endif
559
560static 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
619static 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
690static 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
704static const struct of_device_id of_st21nfca_i2c_match[] = {
705 { .compatible = "st,st21nfca_i2c", },
706 {}
707};
708
709static 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
721module_i2c_driver(st21nfca_hci_i2c_driver);
722
723MODULE_LICENSE("GPL");
724MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.c b/drivers/nfc/st21nfca/st21nfca.c
new file mode 100644
index 000000000000..51e0f00b3a4f
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.c
@@ -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
62static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63
64static 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
77struct 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
88static 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));
193free_info:
194 kfree_skb(skb_pipe_info);
195free_list:
196 kfree_skb(skb_pipe_list);
197 return r;
198}
199
200static 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
217out:
218 mutex_unlock(&info->info_lock);
219 return r;
220}
221
222static 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
234out:
235 mutex_unlock(&info->info_lock);
236}
237
238static 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
291static 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
298static 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
359static 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
376exit:
377 kfree_skb(atqa_skb);
378 return r;
379}
380
381static 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
398exit:
399 kfree_skb(sak_skb);
400 return r;
401}
402
403static 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;
421exit:
422 kfree_skb(uid_skb);
423 return r;
424}
425
426static 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;
449exit:
450 kfree_skb(inventory_skb);
451 return r;
452}
453
454static 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
509static 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 */
532static 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
572static 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
597static 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
609int 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
677err_regdev:
678 nfc_hci_free_device(info->hdev);
679
680err_alloc_hdev:
681 kfree(info);
682
683 return r;
684}
685EXPORT_SYMBOL(st21nfca_hci_probe);
686
687void 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}
695EXPORT_SYMBOL(st21nfca_hci_remove);
696
697MODULE_LICENSE("GPL");
698MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.h b/drivers/nfc/st21nfca/st21nfca.h
new file mode 100644
index 000000000000..334cd90bcc8c
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.h
@@ -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
52int 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);
55void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
56
57enum st21nfca_state {
58 ST21NFCA_ST_COLD,
59 ST21NFCA_ST_READY,
60};
61
62struct 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_ */
diff --git a/drivers/nfc/trf7970a.c b/drivers/nfc/trf7970a.c
index d9babe986473..efb36593ecb4 100644
--- a/drivers/nfc/trf7970a.c
+++ b/drivers/nfc/trf7970a.c
@@ -16,6 +16,7 @@
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/netdevice.h> 17#include <linux/netdevice.h>
18#include <linux/interrupt.h> 18#include <linux/interrupt.h>
19#include <linux/pm_runtime.h>
19#include <linux/nfc.h> 20#include <linux/nfc.h>
20#include <linux/skbuff.h> 21#include <linux/skbuff.h>
21#include <linux/delay.h> 22#include <linux/delay.h>
@@ -67,14 +68,14 @@
67 * only the SRX bit set, it means that all of the data has been received 68 * only the SRX bit set, it means that all of the data has been received
68 * (once what's in the fifo has been read). However, depending on timing 69 * (once what's in the fifo has been read). However, depending on timing
69 * an interrupt status with only the SRX bit set may not be recived. In 70 * 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 * those cases, the timeout mechanism is used to wait 20 ms in case more
71 * data arrives. After 5 ms, it is assumed that all of the data has been 72 * data arrives. After 20 ms, it is assumed that all of the data has been
72 * received and the accumulated rx data is sent upstream. The 73 * received and the accumulated rx data is sent upstream. The
73 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose 74 * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
74 * (i.e., it indicates that some data has been received but we're not sure 75 * (i.e., it indicates that some data has been received but we're not sure
75 * if there is more coming so a timeout in this state means all data has 76 * 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 * been received and there isn't an error). The delay is 20 ms since delays
77 * over 2 ms have been observed during testing (a little extra just in case). 78 * of ~16 ms have been observed during testing.
78 * 79 *
79 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK. 80 * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
80 * Having only 4 bits in the FIFO won't normally generate an interrupt so 81 * Having only 4 bits in the FIFO won't normally generate an interrupt so
@@ -104,7 +105,9 @@
104 105
105#define TRF7970A_SUPPORTED_PROTOCOLS \ 106#define TRF7970A_SUPPORTED_PROTOCOLS \
106 (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ 107 (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 */
108 111
109/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends 112/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
110 * on what the current framing is, the address of the TX length byte 1 113 * on what the current framing is, the address of the TX length byte 1
@@ -120,7 +123,7 @@
120/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ 123/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
121#define TRF7970A_TX_MAX (4096 - 1) 124#define TRF7970A_TX_MAX (4096 - 1)
122 125
123#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5 126#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
124#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3 127#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
125#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20 128#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
126 129
@@ -330,13 +333,15 @@ struct trf7970a {
330 struct regulator *regulator; 333 struct regulator *regulator;
331 struct nfc_digital_dev *ddev; 334 struct nfc_digital_dev *ddev;
332 u32 quirks; 335 u32 quirks;
333 bool powering_up;
334 bool aborting; 336 bool aborting;
335 struct sk_buff *tx_skb; 337 struct sk_buff *tx_skb;
336 struct sk_buff *rx_skb; 338 struct sk_buff *rx_skb;
337 nfc_digital_cmd_complete_t cb; 339 nfc_digital_cmd_complete_t cb;
338 void *cb_arg; 340 void *cb_arg;
341 u8 chip_status_ctrl;
339 u8 iso_ctrl; 342 u8 iso_ctrl;
343 u8 iso_ctrl_tech;
344 u8 modulator_sys_clk_ctrl;
340 u8 special_fcn_reg1; 345 u8 special_fcn_reg1;
341 int technology; 346 int technology;
342 int framing; 347 int framing;
@@ -681,7 +686,9 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
681 trf->ignore_timeout = 686 trf->ignore_timeout =
682 !cancel_delayed_work(&trf->timeout_work); 687 !cancel_delayed_work(&trf->timeout_work);
683 trf7970a_drain_fifo(trf, status); 688 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 {
685 trf7970a_send_err_upstream(trf, -EIO); 692 trf7970a_send_err_upstream(trf, -EIO);
686 } 693 }
687 break; 694 break;
@@ -757,8 +764,8 @@ static int trf7970a_init(struct trf7970a *trf)
757 if (ret) 764 if (ret)
758 goto err_out; 765 goto err_out;
759 766
760 ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, 767 /* Must clear NFC Target Detection Level reg due to erratum */
761 TRF7970A_MODULATOR_DEPTH_OOK); 768 ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
762 if (ret) 769 if (ret)
763 goto err_out; 770 goto err_out;
764 771
@@ -774,12 +781,7 @@ static int trf7970a_init(struct trf7970a *trf)
774 781
775 trf->special_fcn_reg1 = 0; 782 trf->special_fcn_reg1 = 0;
776 783
777 ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, 784 trf->iso_ctrl = 0xff;
778 TRF7970A_CHIP_STATUS_RF_ON |
779 TRF7970A_CHIP_STATUS_VRS5_3);
780 if (ret)
781 goto err_out;
782
783 return 0; 785 return 0;
784 786
785err_out: 787err_out:
@@ -791,53 +793,29 @@ static void trf7970a_switch_rf_off(struct trf7970a *trf)
791{ 793{
792 dev_dbg(trf->dev, "Switching rf off\n"); 794 dev_dbg(trf->dev, "Switching rf off\n");
793 795
794 gpio_set_value(trf->en_gpio, 0); 796 trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
795 gpio_set_value(trf->en2_gpio, 0); 797
798 trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
796 799
797 trf->aborting = false; 800 trf->aborting = false;
798 trf->state = TRF7970A_ST_OFF; 801 trf->state = TRF7970A_ST_OFF;
802
803 pm_runtime_mark_last_busy(trf->dev);
804 pm_runtime_put_autosuspend(trf->dev);
799} 805}
800 806
801static int trf7970a_switch_rf_on(struct trf7970a *trf) 807static void trf7970a_switch_rf_on(struct trf7970a *trf)
802{ 808{
803 unsigned long delay;
804 int ret;
805
806 dev_dbg(trf->dev, "Switching rf on\n"); 809 dev_dbg(trf->dev, "Switching rf on\n");
807 810
808 if (trf->powering_up) 811 pm_runtime_get_sync(trf->dev);
809 usleep_range(5000, 6000);
810
811 gpio_set_value(trf->en2_gpio, 1);
812 usleep_range(1000, 2000);
813 gpio_set_value(trf->en_gpio, 1);
814 812
815 /* The delay between enabling the trf7970a and issuing the first 813 trf->state = TRF7970A_ST_IDLE;
816 * command is significantly longer the very first time after powering
817 * up. Make sure the longer delay is only done the first time.
818 */
819 if (trf->powering_up) {
820 delay = 20000;
821 trf->powering_up = false;
822 } else {
823 delay = 5000;
824 }
825
826 usleep_range(delay, delay + 1000);
827
828 ret = trf7970a_init(trf);
829 if (ret)
830 trf7970a_switch_rf_off(trf);
831 else
832 trf->state = TRF7970A_ST_IDLE;
833
834 return ret;
835} 814}
836 815
837static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) 816static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
838{ 817{
839 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 818 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
840 int ret = 0;
841 819
842 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); 820 dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
843 821
@@ -846,7 +824,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
846 if (on) { 824 if (on) {
847 switch (trf->state) { 825 switch (trf->state) {
848 case TRF7970A_ST_OFF: 826 case TRF7970A_ST_OFF:
849 ret = trf7970a_switch_rf_on(trf); 827 trf7970a_switch_rf_on(trf);
850 break; 828 break;
851 case TRF7970A_ST_IDLE: 829 case TRF7970A_ST_IDLE:
852 case TRF7970A_ST_IDLE_RX_BLOCKED: 830 case TRF7970A_ST_IDLE_RX_BLOCKED:
@@ -871,7 +849,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
871 } 849 }
872 850
873 mutex_unlock(&trf->lock); 851 mutex_unlock(&trf->lock);
874 return ret; 852 return 0;
875} 853}
876 854
877static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) 855static 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)
882 860
883 switch (tech) { 861 switch (tech) {
884 case NFC_DIGITAL_RF_TECH_106A: 862 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;
886 break; 869 break;
887 case NFC_DIGITAL_RF_TECH_ISO15693: 870 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;
889 break; 873 break;
890 default: 874 default:
891 dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); 875 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)
899 883
900static int trf7970a_config_framing(struct trf7970a *trf, int framing) 884static int trf7970a_config_framing(struct trf7970a *trf, int framing)
901{ 885{
886 u8 iso_ctrl = trf->iso_ctrl_tech;
887 int ret;
888
902 dev_dbg(trf->dev, "framing: %d\n", framing); 889 dev_dbg(trf->dev, "framing: %d\n", framing);
903 890
904 switch (framing) { 891 switch (framing) {
905 case NFC_DIGITAL_FRAMING_NFCA_SHORT: 892 case NFC_DIGITAL_FRAMING_NFCA_SHORT:
906 case NFC_DIGITAL_FRAMING_NFCA_STANDARD: 893 case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
907 trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; 894 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;
909 break; 896 break;
910 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: 897 case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
911 case NFC_DIGITAL_FRAMING_NFCA_T4T: 898 case NFC_DIGITAL_FRAMING_NFCA_T4T:
899 case NFC_DIGITAL_FRAMING_NFCB:
900 case NFC_DIGITAL_FRAMING_NFCB_T4T:
912 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: 901 case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
913 case NFC_DIGITAL_FRAMING_ISO15693_T5T: 902 case NFC_DIGITAL_FRAMING_ISO15693_T5T:
914 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; 903 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;
916 break; 905 break;
917 case NFC_DIGITAL_FRAMING_NFCA_T2T: 906 case NFC_DIGITAL_FRAMING_NFCA_T2T:
918 trf->tx_cmd = TRF7970A_CMD_TRANSMIT; 907 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;
920 break; 909 break;
921 default: 910 default:
922 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); 911 dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
@@ -925,24 +914,46 @@ static int trf7970a_config_framing(struct trf7970a *trf, int framing)
925 914
926 trf->framing = framing; 915 trf->framing = framing;
927 916
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;
929} 943}
930 944
931static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, 945static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
932 int param) 946 int param)
933{ 947{
934 struct trf7970a *trf = nfc_digital_get_drvdata(ddev); 948 struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
935 int ret = 0; 949 int ret;
936 950
937 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); 951 dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
938 952
939 mutex_lock(&trf->lock); 953 mutex_lock(&trf->lock);
940 954
941 if (trf->state == TRF7970A_ST_OFF) { 955 if (trf->state == TRF7970A_ST_OFF)
942 ret = trf7970a_switch_rf_on(trf); 956 trf7970a_switch_rf_on(trf);
943 if (ret)
944 goto err_out;
945 }
946 957
947 switch (type) { 958 switch (type) {
948 case NFC_DIGITAL_CONFIG_RF_TECH: 959 case NFC_DIGITAL_CONFIG_RF_TECH:
@@ -956,7 +967,6 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
956 ret = -EINVAL; 967 ret = -EINVAL;
957 } 968 }
958 969
959err_out:
960 mutex_unlock(&trf->lock); 970 mutex_unlock(&trf->lock);
961 return ret; 971 return ret;
962} 972}
@@ -1191,7 +1201,18 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
1191 dev_dbg(trf->dev, "Abort process initiated\n"); 1201 dev_dbg(trf->dev, "Abort process initiated\n");
1192 1202
1193 mutex_lock(&trf->lock); 1203 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
1195 mutex_unlock(&trf->lock); 1216 mutex_unlock(&trf->lock);
1196} 1217}
1197 1218
@@ -1206,12 +1227,25 @@ static struct nfc_digital_ops trf7970a_nfc_ops = {
1206 .abort_cmd = trf7970a_abort_cmd, 1227 .abort_cmd = trf7970a_abort_cmd,
1207}; 1228};
1208 1229
1230static 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
1209static int trf7970a_probe(struct spi_device *spi) 1243static int trf7970a_probe(struct spi_device *spi)
1210{ 1244{
1211 struct device_node *np = spi->dev.of_node; 1245 struct device_node *np = spi->dev.of_node;
1212 const struct spi_device_id *id = spi_get_device_id(spi); 1246 const struct spi_device_id *id = spi_get_device_id(spi);
1213 struct trf7970a *trf; 1247 struct trf7970a *trf;
1214 int ret; 1248 int uvolts, autosuspend_delay, ret;
1215 1249
1216 if (!np) { 1250 if (!np) {
1217 dev_err(&spi->dev, "No Device Tree entry\n"); 1251 dev_err(&spi->dev, "No Device Tree entry\n");
@@ -1281,7 +1315,10 @@ static int trf7970a_probe(struct spi_device *spi)
1281 goto err_destroy_lock; 1315 goto err_destroy_lock;
1282 } 1316 }
1283 1317
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;
1285 1322
1286 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, 1323 trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
1287 TRF7970A_SUPPORTED_PROTOCOLS, 1324 TRF7970A_SUPPORTED_PROTOCOLS,
@@ -1297,6 +1334,12 @@ static int trf7970a_probe(struct spi_device *spi)
1297 nfc_digital_set_drvdata(trf->ddev, trf); 1334 nfc_digital_set_drvdata(trf->ddev, trf);
1298 spi_set_drvdata(spi, trf); 1335 spi_set_drvdata(spi, trf);
1299 1336
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
1300 ret = nfc_digital_register_device(trf->ddev); 1343 ret = nfc_digital_register_device(trf->ddev);
1301 if (ret) { 1344 if (ret) {
1302 dev_err(trf->dev, "Can't register NFC digital device: %d\n", 1345 dev_err(trf->dev, "Can't register NFC digital device: %d\n",
@@ -1307,6 +1350,7 @@ static int trf7970a_probe(struct spi_device *spi)
1307 return 0; 1350 return 0;
1308 1351
1309err_free_ddev: 1352err_free_ddev:
1353 pm_runtime_disable(trf->dev);
1310 nfc_digital_free_device(trf->ddev); 1354 nfc_digital_free_device(trf->ddev);
1311err_disable_regulator: 1355err_disable_regulator:
1312 regulator_disable(trf->regulator); 1356 regulator_disable(trf->regulator);
@@ -1321,15 +1365,16 @@ static int trf7970a_remove(struct spi_device *spi)
1321 1365
1322 mutex_lock(&trf->lock); 1366 mutex_lock(&trf->lock);
1323 1367
1324 trf7970a_switch_rf_off(trf);
1325 trf7970a_init(trf);
1326
1327 switch (trf->state) { 1368 switch (trf->state) {
1328 case TRF7970A_ST_WAIT_FOR_TX_FIFO: 1369 case TRF7970A_ST_WAIT_FOR_TX_FIFO:
1329 case TRF7970A_ST_WAIT_FOR_RX_DATA: 1370 case TRF7970A_ST_WAIT_FOR_RX_DATA:
1330 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: 1371 case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
1331 case TRF7970A_ST_WAIT_TO_ISSUE_EOF: 1372 case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
1332 trf7970a_send_err_upstream(trf, -ECANCELED); 1373 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);
1333 break; 1378 break;
1334 default: 1379 default:
1335 break; 1380 break;
@@ -1337,6 +1382,8 @@ static int trf7970a_remove(struct spi_device *spi)
1337 1382
1338 mutex_unlock(&trf->lock); 1383 mutex_unlock(&trf->lock);
1339 1384
1385 pm_runtime_disable(trf->dev);
1386
1340 nfc_digital_unregister_device(trf->ddev); 1387 nfc_digital_unregister_device(trf->ddev);
1341 nfc_digital_free_device(trf->ddev); 1388 nfc_digital_free_device(trf->ddev);
1342 1389
@@ -1347,6 +1394,70 @@ static int trf7970a_remove(struct spi_device *spi)
1347 return 0; 1394 return 0;
1348} 1395}
1349 1396
1397#ifdef CONFIG_PM_RUNTIME
1398static 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
1422static 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
1456static 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
1350static const struct spi_device_id trf7970a_id_table[] = { 1461static const struct spi_device_id trf7970a_id_table[] = {
1351 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, 1462 { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
1352 { } 1463 { }
@@ -1360,6 +1471,7 @@ static struct spi_driver trf7970a_spi_driver = {
1360 .driver = { 1471 .driver = {
1361 .name = "trf7970a", 1472 .name = "trf7970a",
1362 .owner = THIS_MODULE, 1473 .owner = THIS_MODULE,
1474 .pm = &trf7970a_pm_ops,
1363 }, 1475 },
1364}; 1476};
1365 1477
diff --git a/include/linux/platform_data/st21nfca.h b/include/linux/platform_data/st21nfca.h
new file mode 100644
index 000000000000..1730312398ff
--- /dev/null
+++ b/include/linux/platform_data/st21nfca.h
@@ -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
26struct 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_ */
diff --git a/include/net/nfc/digital.h b/include/net/nfc/digital.h
index 7655cfe27c34..bdf55c3b7a19 100644
--- a/include/net/nfc/digital.h
+++ b/include/net/nfc/digital.h
@@ -36,6 +36,7 @@ enum {
36 NFC_DIGITAL_RF_TECH_212F, 36 NFC_DIGITAL_RF_TECH_212F,
37 NFC_DIGITAL_RF_TECH_424F, 37 NFC_DIGITAL_RF_TECH_424F,
38 NFC_DIGITAL_RF_TECH_ISO15693, 38 NFC_DIGITAL_RF_TECH_ISO15693,
39 NFC_DIGITAL_RF_TECH_106B,
39 40
40 NFC_DIGITAL_RF_TECH_LAST, 41 NFC_DIGITAL_RF_TECH_LAST,
41}; 42};
@@ -62,6 +63,9 @@ enum {
62 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY, 63 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
63 NFC_DIGITAL_FRAMING_ISO15693_T5T, 64 NFC_DIGITAL_FRAMING_ISO15693_T5T,
64 65
66 NFC_DIGITAL_FRAMING_NFCB,
67 NFC_DIGITAL_FRAMING_NFCB_T4T,
68
65 NFC_DIGITAL_FRAMING_LAST, 69 NFC_DIGITAL_FRAMING_LAST,
66}; 70};
67 71
diff --git a/include/net/nfc/hci.h b/include/net/nfc/hci.h
index 03c4650b548c..61286db54388 100644
--- a/include/net/nfc/hci.h
+++ b/include/net/nfc/hci.h
@@ -27,6 +27,7 @@ struct nfc_hci_dev;
27struct nfc_hci_ops { 27struct nfc_hci_ops {
28 int (*open) (struct nfc_hci_dev *hdev); 28 int (*open) (struct nfc_hci_dev *hdev);
29 void (*close) (struct nfc_hci_dev *hdev); 29 void (*close) (struct nfc_hci_dev *hdev);
30 int (*load_session) (struct nfc_hci_dev *hdev);
30 int (*hci_ready) (struct nfc_hci_dev *hdev); 31 int (*hci_ready) (struct nfc_hci_dev *hdev);
31 /* 32 /*
32 * xmit must always send the complete buffer before 33 * xmit must always send the complete buffer before
diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index 2e8b40c16274..6c583e244de2 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -264,4 +264,7 @@ int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type);
264int nfc_remove_se(struct nfc_dev *dev, u32 se_idx); 264int nfc_remove_se(struct nfc_dev *dev, u32 se_idx);
265struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx); 265struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx);
266 266
267void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
268 u8 payload_type, u8 direction);
269
267#endif /* __NET_NFC_H */ 270#endif /* __NET_NFC_H */
diff --git a/include/uapi/linux/nfc.h b/include/uapi/linux/nfc.h
index 9789dc95b6a8..9b19b4461928 100644
--- a/include/uapi/linux/nfc.h
+++ b/include/uapi/linux/nfc.h
@@ -273,11 +273,19 @@ struct sockaddr_nfc_llcp {
273 * First byte is the adapter index 273 * First byte is the adapter index
274 * Second byte contains flags 274 * Second byte contains flags
275 * - 0x01 - Direction (0=RX, 1=TX) 275 * - 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
277 **/ 279 **/
278#define NFC_LLCP_RAW_HEADER_SIZE 2 280#define NFC_RAW_HEADER_SIZE 2
279#define NFC_LLCP_DIRECTION_RX 0x00 281#define NFC_DIRECTION_RX 0x00
280#define NFC_LLCP_DIRECTION_TX 0x01 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
281 289
282/* socket option names */ 290/* socket option names */
283#define NFC_LLCP_RW 0 291#define NFC_LLCP_RW 0
diff --git a/net/nfc/digital.h b/net/nfc/digital.h
index 3759add68b1b..71ad7eefddd4 100644
--- a/net/nfc/digital.h
+++ b/net/nfc/digital.h
@@ -71,6 +71,7 @@ static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
71void digital_poll_next_tech(struct nfc_digital_dev *ddev); 71void digital_poll_next_tech(struct nfc_digital_dev *ddev);
72 72
73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech); 73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
74int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech);
74int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech); 75int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
75int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech); 76int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech);
76 77
diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
index e01e15dbf1ab..b105cfb00e76 100644
--- a/net/nfc/digital_core.c
+++ b/net/nfc/digital_core.c
@@ -22,6 +22,8 @@
22#define DIGITAL_PROTO_NFCA_RF_TECH \ 22#define DIGITAL_PROTO_NFCA_RF_TECH \
23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK) 23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
24 24
25#define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
26
25#define DIGITAL_PROTO_NFCF_RF_TECH \ 27#define DIGITAL_PROTO_NFCF_RF_TECH \
26 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK) 28 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
27 29
@@ -345,6 +347,12 @@ int digital_target_found(struct nfc_digital_dev *ddev,
345 add_crc = digital_skb_add_crc_a; 347 add_crc = digital_skb_add_crc_a;
346 break; 348 break;
347 349
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
348 default: 356 default:
349 pr_err("Invalid protocol %d\n", protocol); 357 pr_err("Invalid protocol %d\n", protocol);
350 return -EINVAL; 358 return -EINVAL;
@@ -475,6 +483,10 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
475 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, 483 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
476 digital_in_send_sens_req); 484 digital_in_send_sens_req);
477 485
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
478 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { 490 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
479 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, 491 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
480 digital_in_send_sensf_req); 492 digital_in_send_sensf_req);
@@ -635,7 +647,8 @@ static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
635 goto done; 647 goto done;
636 } 648 }
637 649
638 if (ddev->curr_protocol == NFC_PROTO_ISO14443) { 650 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
651 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
639 rc = digital_in_iso_dep_pull_sod(ddev, resp); 652 rc = digital_in_iso_dep_pull_sod(ddev, resp);
640 if (rc) 653 if (rc)
641 goto done; 654 goto done;
@@ -676,7 +689,8 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
676 goto exit; 689 goto exit;
677 } 690 }
678 691
679 if (ddev->curr_protocol == NFC_PROTO_ISO14443) { 692 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
693 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
680 rc = digital_in_iso_dep_push_sod(ddev, skb); 694 rc = digital_in_iso_dep_push_sod(ddev, skb);
681 if (rc) 695 if (rc)
682 goto exit; 696 goto exit;
@@ -747,6 +761,8 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
747 ddev->protocols |= NFC_PROTO_ISO15693_MASK; 761 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
748 if (supported_protocols & NFC_PROTO_ISO14443_MASK) 762 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
749 ddev->protocols |= NFC_PROTO_ISO14443_MASK; 763 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
764 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
765 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
750 766
751 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; 767 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
752 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; 768 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
diff --git a/net/nfc/digital_technology.c b/net/nfc/digital_technology.c
index 278c3fed27e0..12a233e9ece5 100644
--- a/net/nfc/digital_technology.c
+++ b/net/nfc/digital_technology.c
@@ -41,6 +41,24 @@
41#define DIGITAL_MIFARE_READ_RES_LEN 16 41#define DIGITAL_MIFARE_READ_RES_LEN 16
42#define DIGITAL_MIFARE_ACK_RES 0x0A 42#define DIGITAL_MIFARE_ACK_RES 0x0A
43 43
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
44#define DIGITAL_CMD_SENSF_REQ 0x00 62#define DIGITAL_CMD_SENSF_REQ 0x00
45#define DIGITAL_CMD_SENSF_RES 0x01 63#define DIGITAL_CMD_SENSF_RES 0x01
46 64
@@ -75,6 +93,7 @@ static const u8 digital_ats_fsc[] = {
75}; 93};
76 94
77#define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F) 95#define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
96#define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
78#define DIGITAL_ATS_MAX_FSC 256 97#define DIGITAL_ATS_MAX_FSC 256
79 98
80#define DIGITAL_RATS_BYTE1 0xE0 99#define DIGITAL_RATS_BYTE1 0xE0
@@ -92,6 +111,32 @@ struct digital_sel_req {
92 u8 bcc; 111 u8 bcc;
93} __packed; 112} __packed;
94 113
114struct digital_sensb_req {
115 u8 cmd;
116 u8 afi;
117 u8 param;
118} __packed;
119
120struct digital_sensb_res {
121 u8 cmd;
122 u8 nfcid0[4];
123 u8 app_data[4];
124 u8 proto_info[3];
125} __packed;
126
127struct 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
136struct digital_attrib_res {
137 u8 mbli_did;
138} __packed;
139
95struct digital_sensf_req { 140struct digital_sensf_req {
96 u8 cmd; 141 u8 cmd;
97 u8 sc1; 142 u8 sc1;
@@ -531,6 +576,175 @@ int digital_in_recv_mifare_res(struct sk_buff *resp)
531 return -EIO; 576 return -EIO;
532} 577}
533 578
579static 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
608exit:
609 dev_kfree_skb(resp);
610 kfree(target);
611
612 if (rc)
613 digital_poll_next_tech(ddev);
614}
615
616int 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
650static 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
704exit:
705 dev_kfree_skb(resp);
706
707 if (rc) {
708 kfree(target);
709 digital_poll_next_tech(ddev);
710 }
711}
712
713int 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
534static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg, 748static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
535 struct sk_buff *resp) 749 struct sk_buff *resp)
536{ 750{
@@ -877,6 +1091,18 @@ exit:
877 dev_kfree_skb(resp); 1091 dev_kfree_skb(resp);
878} 1092}
879 1093
1094static 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
880static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, 1106static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
881 struct digital_sensf_req *sensf_req) 1107 struct digital_sensf_req *sensf_req)
882{ 1108{
@@ -887,7 +1113,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
887 1113
888 size = sizeof(struct digital_sensf_res); 1114 size = sizeof(struct digital_sensf_res);
889 1115
890 if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE) 1116 if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
891 size -= sizeof(sensf_res->rd); 1117 size -= sizeof(sensf_res->rd);
892 1118
893 skb = digital_skb_alloc(ddev, size); 1119 skb = digital_skb_alloc(ddev, size);
@@ -922,7 +1148,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
922 digital_skb_add_crc_f(skb); 1148 digital_skb_add_crc_f(skb);
923 1149
924 rc = digital_tg_send_cmd(ddev, skb, 300, 1150 rc = digital_tg_send_cmd(ddev, skb, 300,
925 digital_tg_recv_atr_req, NULL); 1151 digital_tg_recv_atr_or_sensf_req, NULL);
926 if (rc) 1152 if (rc)
927 kfree_skb(skb); 1153 kfree_skb(skb);
928 1154
diff --git a/net/nfc/hci/command.c b/net/nfc/hci/command.c
index a9f4d2e62d8d..677d24bb70f8 100644
--- a/net/nfc/hci/command.c
+++ b/net/nfc/hci/command.c
@@ -26,6 +26,8 @@
26 26
27#include "hci.h" 27#include "hci.h"
28 28
29#define MAX_FWI 4949
30
29static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, 31static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
30 const u8 *param, size_t param_len, 32 const u8 *param, size_t param_len,
31 data_exchange_cb_t cb, void *cb_context) 33 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,
37 * for all commands? 39 * for all commands?
38 */ 40 */
39 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd, 41 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);
41} 43}
42 44
43/* 45/*
@@ -82,7 +84,7 @@ static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
82 NFC_HCI_HCP_COMMAND, cmd, 84 NFC_HCI_HCP_COMMAND, cmd,
83 param, param_len, 85 param, param_len,
84 nfc_hci_execute_cb, &hcp_ew, 86 nfc_hci_execute_cb, &hcp_ew,
85 3000); 87 MAX_FWI);
86 if (hcp_ew.exec_result < 0) 88 if (hcp_ew.exec_result < 0)
87 return hcp_ew.exec_result; 89 return hcp_ew.exec_result;
88 90
diff --git a/net/nfc/hci/core.c b/net/nfc/hci/core.c
index d45b638e77c7..47403705197e 100644
--- a/net/nfc/hci/core.c
+++ b/net/nfc/hci/core.c
@@ -225,7 +225,7 @@ int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
225 goto exit; 225 goto exit;
226 } 226 }
227 227
228 targets->sens_res = be16_to_cpu(*(u16 *)atqa_skb->data); 228 targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
229 targets->sel_res = sak_skb->data[0]; 229 targets->sel_res = sak_skb->data[0];
230 230
231 r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE, 231 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)
380 if (r < 0) 380 if (r < 0)
381 goto disconnect_all; 381 goto disconnect_all;
382 382
383 if (skb->len && skb->len == strlen(hdev->init_data.session_id)) 383 if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
384 if (memcmp(hdev->init_data.session_id, skb->data, 384 (memcmp(hdev->init_data.session_id, skb->data,
385 skb->len) == 0) { 385 skb->len) == 0) && hdev->ops->load_session) {
386 /* TODO ELa: restore gate<->pipe table from 386 /* Restore gate<->pipe table from some proprietary location. */
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 */
393 387
394 /* goto exit 388 r = hdev->ops->load_session(hdev);
395 * For now, always do a full initialization */
396 }
397 389
398 r = nfc_hci_disconnect_all_gates(hdev); 390 if (r < 0)
399 if (r < 0) 391 goto disconnect_all;
400 goto exit; 392 } else {
401 393
402 r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count, 394 r = nfc_hci_disconnect_all_gates(hdev);
403 hdev->init_data.gates); 395 if (r < 0)
404 if (r < 0) 396 goto exit;
405 goto disconnect_all;
406 397
407 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, 398 r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
408 NFC_HCI_ADMIN_SESSION_IDENTITY, 399 hdev->init_data.gates);
409 hdev->init_data.session_id, 400 if (r < 0)
410 strlen(hdev->init_data.session_id)); 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 }
411 if (r == 0) 408 if (r == 0)
412 goto exit; 409 goto exit;
413 410
diff --git a/net/nfc/llcp_commands.c b/net/nfc/llcp_commands.c
index bec6ed15f503..a3ad69a4c648 100644
--- a/net/nfc/llcp_commands.c
+++ b/net/nfc/llcp_commands.c
@@ -387,7 +387,7 @@ int nfc_llcp_send_symm(struct nfc_dev *dev)
387 387
388 __net_timestamp(skb); 388 __net_timestamp(skb);
389 389
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);
391 391
392 return nfc_data_exchange(dev, local->target_idx, skb, 392 return nfc_data_exchange(dev, local->target_idx, skb,
393 nfc_llcp_recv, local); 393 nfc_llcp_recv, local);
diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index b4671958fcf9..f6278da68763 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -680,16 +680,17 @@ void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
680 continue; 680 continue;
681 681
682 if (skb_copy == NULL) { 682 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,
684 GFP_ATOMIC); 684 GFP_ATOMIC);
685 685
686 if (skb_copy == NULL) 686 if (skb_copy == NULL)
687 continue; 687 continue;
688 688
689 data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE); 689 data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
690 690
691 data[0] = local->dev ? local->dev->idx : 0xFF; 691 data[0] = local->dev ? local->dev->idx : 0xFF;
692 data[1] = direction; 692 data[1] = direction & 0x01;
693 data[1] |= (RAW_PAYLOAD_LLCP << 1);
693 } 694 }
694 695
695 nskb = skb_clone(skb_copy, GFP_ATOMIC); 696 nskb = skb_clone(skb_copy, GFP_ATOMIC);
@@ -747,7 +748,7 @@ static void nfc_llcp_tx_work(struct work_struct *work)
747 __net_timestamp(skb); 748 __net_timestamp(skb);
748 749
749 nfc_llcp_send_to_raw_sock(local, skb, 750 nfc_llcp_send_to_raw_sock(local, skb,
750 NFC_LLCP_DIRECTION_TX); 751 NFC_DIRECTION_TX);
751 752
752 ret = nfc_data_exchange(local->dev, local->target_idx, 753 ret = nfc_data_exchange(local->dev, local->target_idx,
753 skb, nfc_llcp_recv, local); 754 skb, nfc_llcp_recv, local);
@@ -1476,7 +1477,7 @@ static void nfc_llcp_rx_work(struct work_struct *work)
1476 1477
1477 __net_timestamp(skb); 1478 __net_timestamp(skb);
1478 1479
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);
1480 1481
1481 nfc_llcp_rx_skb(local, skb); 1482 nfc_llcp_rx_skb(local, skb);
1482 1483
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 6c34ac978501..2b400e1a8695 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -861,6 +861,10 @@ static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
861 /* Get rid of skb owner, prior to sending to the driver. */ 861 /* Get rid of skb owner, prior to sending to the driver. */
862 skb_orphan(skb); 862 skb_orphan(skb);
863 863
864 /* Send copy to sniffer */
865 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
866 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
867
864 return ndev->ops->send(ndev, skb); 868 return ndev->ops->send(ndev, skb);
865} 869}
866 870
@@ -935,6 +939,11 @@ static void nci_rx_work(struct work_struct *work)
935 struct sk_buff *skb; 939 struct sk_buff *skb;
936 940
937 while ((skb = skb_dequeue(&ndev->rx_q))) { 941 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
938 /* Process frame */ 947 /* Process frame */
939 switch (nci_mt(skb->data)) { 948 switch (nci_mt(skb->data)) {
940 case NCI_MT_RSP_PKT: 949 case NCI_MT_RSP_PKT:
diff --git a/net/nfc/nci/ntf.c b/net/nfc/nci/ntf.c
index 1e905097456b..f8f6af231381 100644
--- a/net/nfc/nci/ntf.c
+++ b/net/nfc/nci/ntf.c
@@ -366,7 +366,6 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
366 struct nci_rf_intf_activated_ntf *ntf, __u8 *data) 366 struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
367{ 367{
368 struct activation_params_poll_nfc_dep *poll; 368 struct activation_params_poll_nfc_dep *poll;
369 int i;
370 369
371 switch (ntf->activation_rf_tech_and_mode) { 370 switch (ntf->activation_rf_tech_and_mode) {
372 case NCI_NFC_A_PASSIVE_POLL_MODE: 371 case NCI_NFC_A_PASSIVE_POLL_MODE:
@@ -374,10 +373,8 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
374 poll = &ntf->activation_params.poll_nfc_dep; 373 poll = &ntf->activation_params.poll_nfc_dep;
375 poll->atr_res_len = min_t(__u8, *data++, 63); 374 poll->atr_res_len = min_t(__u8, *data++, 63);
376 pr_debug("atr_res_len %d\n", poll->atr_res_len); 375 pr_debug("atr_res_len %d\n", poll->atr_res_len);
377 if (poll->atr_res_len > 0) { 376 if (poll->atr_res_len > 0)
378 for (i = 0; i < poll->atr_res_len; i++) 377 memcpy(poll->atr_res, data, poll->atr_res_len);
379 poll->atr_res[poll->atr_res_len-1-i] = data[i];
380 }
381 break; 378 break;
382 379
383 default: 380 default:
diff --git a/net/nfc/nfc.h b/net/nfc/nfc.h
index 9d6e74f7e6b3..88d60064890e 100644
--- a/net/nfc/nfc.h
+++ b/net/nfc/nfc.h
@@ -40,6 +40,12 @@ struct nfc_rawsock {
40 struct work_struct tx_work; 40 struct work_struct tx_work;
41 bool tx_work_scheduled; 41 bool tx_work_scheduled;
42}; 42};
43
44struct nfc_sock_list {
45 struct hlist_head head;
46 rwlock_t lock;
47};
48
43#define nfc_rawsock(sk) ((struct nfc_rawsock *) sk) 49#define nfc_rawsock(sk) ((struct nfc_rawsock *) sk)
44#define to_rawsock_sk(_tx_work) \ 50#define to_rawsock_sk(_tx_work) \
45 ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work)) 51 ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work))
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index c27a6e86cae4..8627c75063e2 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -27,6 +27,24 @@
27 27
28#include "nfc.h" 28#include "nfc.h"
29 29
30static struct nfc_sock_list raw_sk_list = {
31 .lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
32};
33
34void 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
41void 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
30static void rawsock_write_queue_purge(struct sock *sk) 48static void rawsock_write_queue_purge(struct sock *sk)
31{ 49{
32 pr_debug("sk=%p\n", sk); 50 pr_debug("sk=%p\n", sk);
@@ -57,6 +75,9 @@ static int rawsock_release(struct socket *sock)
57 if (!sk) 75 if (!sk)
58 return 0; 76 return 0;
59 77
78 if (sock->type == SOCK_RAW)
79 nfc_sock_unlink(&raw_sk_list, sk);
80
60 sock_orphan(sk); 81 sock_orphan(sk);
61 sock_put(sk); 82 sock_put(sk);
62 83
@@ -275,6 +296,26 @@ static const struct proto_ops rawsock_ops = {
275 .mmap = sock_no_mmap, 296 .mmap = sock_no_mmap,
276}; 297};
277 298
299static 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
278static void rawsock_destruct(struct sock *sk) 319static void rawsock_destruct(struct sock *sk)
279{ 320{
280 pr_debug("sk=%p\n", sk); 321 pr_debug("sk=%p\n", sk);
@@ -300,10 +341,13 @@ static int rawsock_create(struct net *net, struct socket *sock,
300 341
301 pr_debug("sock=%p\n", sock); 342 pr_debug("sock=%p\n", sock);
302 343
303 if (sock->type != SOCK_SEQPACKET) 344 if ((sock->type != SOCK_SEQPACKET) && (sock->type != SOCK_RAW))
304 return -ESOCKTNOSUPPORT; 345 return -ESOCKTNOSUPPORT;
305 346
306 sock->ops = &rawsock_ops; 347 if (sock->type == SOCK_RAW)
348 sock->ops = &rawsock_raw_ops;
349 else
350 sock->ops = &rawsock_ops;
307 351
308 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto); 352 sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
309 if (!sk) 353 if (!sk)
@@ -313,13 +357,53 @@ static int rawsock_create(struct net *net, struct socket *sock,
313 sk->sk_protocol = nfc_proto->id; 357 sk->sk_protocol = nfc_proto->id;
314 sk->sk_destruct = rawsock_destruct; 358 sk->sk_destruct = rawsock_destruct;
315 sock->state = SS_UNCONNECTED; 359 sock->state = SS_UNCONNECTED;
316 360 if (sock->type == SOCK_RAW)
317 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work); 361 nfc_sock_link(&raw_sk_list, sk);
318 nfc_rawsock(sk)->tx_work_scheduled = false; 362 else {
363 INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
364 nfc_rawsock(sk)->tx_work_scheduled = false;
365 }
319 366
320 return 0; 367 return 0;
321} 368}
322 369
370void 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}
405EXPORT_SYMBOL(nfc_send_to_raw_sock);
406
323static struct proto rawsock_proto = { 407static struct proto rawsock_proto = {
324 .name = "NFC_RAW", 408 .name = "NFC_RAW",
325 .owner = THIS_MODULE, 409 .owner = THIS_MODULE,