Commit 99abe65f authored by John W. Linville's avatar John W. Linville

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: default avatarJohn W. Linville <linville@tuxdriver.com>
parents 96810471 79747280
* NXP Semiconductors PN544 NFC Controller
Required properties:
- compatible: Should be "nxp,pn544-i2c".
- clock-frequency: IC work frequency.
- reg: address on the bus
- interrupt-parent: phandle for the interrupt gpio controller
- interrupts: GPIO interrupt to which the chip is connected
- enable-gpios: Output GPIO pin used for enabling/disabling the PN544
- firmware-gpios: Output GPIO pin used to enter firmware download mode
Optional SoC Specific Properties:
- pinctrl-names: Contains only one value - "default".
- pintctrl-0: Specifies the pin control groups used for this controller.
Example (for ARM-based BeagleBone with PN544 on I2C2):
&i2c2 {
status = "okay";
pn544: pn544@28 {
compatible = "nxp,pn544-i2c";
reg = <0x28>;
clock-frequency = <400000>;
interrupt-parent = <&gpio1>;
interrupts = <17 GPIO_ACTIVE_HIGH>;
enable-gpios = <&gpio3 21 GPIO_ACTIVE_HIGH>;
firmware-gpios = <&gpio3 19 GPIO_ACTIVE_HIGH>;
};
};
* STMicroelectronics SAS. ST21NFCA NFC Controller
Required properties:
- compatible: Should be "st,st21nfca-i2c".
- clock-frequency: I²C work frequency.
- reg: address on the bus
- interrupt-parent: phandle for the interrupt gpio controller
- interrupts: GPIO interrupt to which the chip is connected
- enable-gpios: Output GPIO pin used for enabling/disabling the ST21NFCA
Optional SoC Specific Properties:
- pinctrl-names: Contains only one value - "default".
- pintctrl-0: Specifies the pin control groups used for this controller.
Example (for ARM-based BeagleBoard xM with ST21NFCA on I2C2):
&i2c2 {
status = "okay";
st21nfca: st21nfca@1 {
compatible = "st,st21nfca_i2c";
reg = <0x01>;
clock-frequency = <400000>;
interrupt-parent = <&gpio5>;
interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
enable-gpios = <&gpio5 29 GPIO_ACTIVE_HIGH>;
};
};
...@@ -12,6 +12,7 @@ Required properties: ...@@ -12,6 +12,7 @@ Required properties:
Optional SoC Specific Properties: Optional SoC Specific Properties:
- pinctrl-names: Contains only one value - "default". - pinctrl-names: Contains only one value - "default".
- pintctrl-0: Specifies the pin control groups used for this controller. - pintctrl-0: Specifies the pin control groups used for this controller.
- autosuspend-delay: Specify autosuspend delay in milliseconds.
Example (for ARM-based BeagleBone with TRF7970A on SPI1): Example (for ARM-based BeagleBone with TRF7970A on SPI1):
...@@ -29,6 +30,7 @@ Example (for ARM-based BeagleBone with TRF7970A on SPI1): ...@@ -29,6 +30,7 @@ Example (for ARM-based BeagleBone with TRF7970A on SPI1):
ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>, ti,enable-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>,
<&gpio2 5 GPIO_ACTIVE_LOW>; <&gpio2 5 GPIO_ACTIVE_LOW>;
vin-supply = <&ldo3_reg>; vin-supply = <&ldo3_reg>;
autosuspend-delay = <30000>;
status = "okay"; status = "okay";
}; };
}; };
...@@ -71,5 +71,6 @@ config NFC_PORT100 ...@@ -71,5 +71,6 @@ config NFC_PORT100
source "drivers/nfc/pn544/Kconfig" source "drivers/nfc/pn544/Kconfig"
source "drivers/nfc/microread/Kconfig" source "drivers/nfc/microread/Kconfig"
source "drivers/nfc/nfcmrvl/Kconfig" source "drivers/nfc/nfcmrvl/Kconfig"
source "drivers/nfc/st21nfca/Kconfig"
endmenu endmenu
...@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o ...@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o
obj-$(CONFIG_NFC_PORT100) += port100.o obj-$(CONFIG_NFC_PORT100) += port100.o
obj-$(CONFIG_NFC_MRVL) += nfcmrvl/ obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/gpio.h> #include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/delay.h> #include <linux/delay.h>
...@@ -857,6 +859,92 @@ static void pn544_hci_i2c_fw_work(struct work_struct *work) ...@@ -857,6 +859,92 @@ static void pn544_hci_i2c_fw_work(struct work_struct *work)
} }
} }
#ifdef CONFIG_OF
static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
{
struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
struct device_node *pp;
int ret;
pp = client->dev.of_node;
if (!pp) {
ret = -ENODEV;
goto err_dt;
}
/* Obtention of EN GPIO from device tree */
ret = of_get_named_gpio(pp, "enable-gpios", 0);
if (ret < 0) {
if (ret != -EPROBE_DEFER)
nfc_err(&client->dev,
"Failed to get EN gpio, error: %d\n", ret);
goto err_dt;
}
phy->gpio_en = ret;
/* Configuration of EN GPIO */
ret = gpio_request(phy->gpio_en, "pn544_en");
if (ret) {
nfc_err(&client->dev, "Fail EN pin\n");
goto err_dt;
}
ret = gpio_direction_output(phy->gpio_en, 0);
if (ret) {
nfc_err(&client->dev, "Fail EN pin direction\n");
goto err_gpio_en;
}
/* Obtention of FW GPIO from device tree */
ret = of_get_named_gpio(pp, "firmware-gpios", 0);
if (ret < 0) {
if (ret != -EPROBE_DEFER)
nfc_err(&client->dev,
"Failed to get FW gpio, error: %d\n", ret);
goto err_gpio_en;
}
phy->gpio_fw = ret;
/* Configuration of FW GPIO */
ret = gpio_request(phy->gpio_fw, "pn544_fw");
if (ret) {
nfc_err(&client->dev, "Fail FW pin\n");
goto err_gpio_en;
}
ret = gpio_direction_output(phy->gpio_fw, 0);
if (ret) {
nfc_err(&client->dev, "Fail FW pin direction\n");
goto err_gpio_fw;
}
/* IRQ */
ret = irq_of_parse_and_map(pp, 0);
if (ret < 0) {
nfc_err(&client->dev,
"Unable to get irq, error: %d\n", ret);
goto err_gpio_fw;
}
client->irq = ret;
return 0;
err_gpio_fw:
gpio_free(phy->gpio_fw);
err_gpio_en:
gpio_free(phy->gpio_en);
err_dt:
return ret;
}
#else
static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
{
return -ENODEV;
}
#endif
static int pn544_hci_i2c_probe(struct i2c_client *client, static int pn544_hci_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
...@@ -887,25 +975,36 @@ static int pn544_hci_i2c_probe(struct i2c_client *client, ...@@ -887,25 +975,36 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
i2c_set_clientdata(client, phy); i2c_set_clientdata(client, phy);
pdata = client->dev.platform_data; pdata = client->dev.platform_data;
if (pdata == NULL) {
nfc_err(&client->dev, "No platform data\n");
return -EINVAL;
}
if (pdata->request_resources == NULL) { /* No platform data, using device tree. */
nfc_err(&client->dev, "request_resources() missing\n"); if (!pdata && client->dev.of_node) {
return -EINVAL; r = pn544_hci_i2c_of_request_resources(client);
} if (r) {
nfc_err(&client->dev, "No DT data\n");
return r;
}
/* Using platform data. */
} else if (pdata) {
r = pdata->request_resources(client); if (pdata->request_resources == NULL) {
if (r) { nfc_err(&client->dev, "request_resources() missing\n");
nfc_err(&client->dev, "Cannot get platform resources\n"); return -EINVAL;
return r; }
}
phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); r = pdata->request_resources(client);
phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET); if (r) {
phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ); nfc_err(&client->dev,
"Cannot get platform resources\n");
return r;
}
phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
} else {
nfc_err(&client->dev, "No platform data\n");
return -EINVAL;
}
pn544_hci_i2c_platform_init(phy); pn544_hci_i2c_platform_init(phy);
...@@ -930,8 +1029,12 @@ static int pn544_hci_i2c_probe(struct i2c_client *client, ...@@ -930,8 +1029,12 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
free_irq(client->irq, phy); free_irq(client->irq, phy);
err_rti: err_rti:
if (pdata->free_resources != NULL) if (!pdata) {
gpio_free(phy->gpio_en);
gpio_free(phy->gpio_fw);
} else if (pdata->free_resources) {
pdata->free_resources(); pdata->free_resources();
}
return r; return r;
} }
...@@ -953,15 +1056,30 @@ static int pn544_hci_i2c_remove(struct i2c_client *client) ...@@ -953,15 +1056,30 @@ static int pn544_hci_i2c_remove(struct i2c_client *client)
pn544_hci_i2c_disable(phy); pn544_hci_i2c_disable(phy);
free_irq(client->irq, phy); free_irq(client->irq, phy);
if (pdata->free_resources)
/* No platform data, GPIOs have been requested by this driver */
if (!pdata) {
gpio_free(phy->gpio_en);
gpio_free(phy->gpio_fw);
/* Using platform data */
} else if (pdata->free_resources) {
pdata->free_resources(); pdata->free_resources();
}
return 0; return 0;
} }
static const struct of_device_id of_pn544_i2c_match[] = {
{ .compatible = "nxp,pn544-i2c", },
{},
};
MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
static struct i2c_driver pn544_hci_i2c_driver = { static struct i2c_driver pn544_hci_i2c_driver = {
.driver = { .driver = {
.name = PN544_HCI_I2C_DRIVER_NAME, .name = PN544_HCI_I2C_DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(of_pn544_i2c_match),
}, },
.probe = pn544_hci_i2c_probe, .probe = pn544_hci_i2c_probe,
.id_table = pn544_hci_i2c_id_table, .id_table = pn544_hci_i2c_id_table,
......
config NFC_ST21NFCA
tristate "STMicroelectronics ST21NFCA NFC driver"
depends on NFC_HCI
select CRC_CCITT
default n
---help---
STMicroelectronics ST21NFCA core driver. It implements the chipset
HCI logic and hooks into the NFC kernel APIs. Physical layers will
register against it.
To compile this driver as a module, choose m here. The module will
be called st21nfca.
Say N if unsure.
config NFC_ST21NFCA_I2C
tristate "NFC ST21NFCA i2c support"
depends on NFC_ST21NFCA && I2C && NFC_SHDLC
---help---
This module adds support for the STMicroelectronics st21nfca i2c interface.
Select this if your platform is using the i2c bus.
If you choose to build a module, it'll be called st21nfca_i2c.
Say N if unsure.
#
# Makefile for ST21NFCA HCI based NFC driver
#
st21nfca_i2c-objs = i2c.o
obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o
obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
/*
* I2C Link Layer for ST21NFCA HCI based Driver
* Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/crc-ccitt.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/nfc.h>
#include <linux/firmware.h>
#include <linux/unaligned/access_ok.h>
#include <linux/platform_data/st21nfca.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include <net/nfc/nfc.h>
#include "st21nfca.h"
/*
* Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
* Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
* called byte stuffing has been introduced.
*
* if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
* - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
* - xor byte with ST21NFCA_BYTE_STUFFING_MASK
*/
#define ST21NFCA_SOF_EOF 0x7e
#define ST21NFCA_BYTE_STUFFING_MASK 0x20
#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
/* SOF + 00 */
#define ST21NFCA_FRAME_HEADROOM 2
/* 2 bytes crc + EOF */
#define ST21NFCA_FRAME_TAILROOM 3
#define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
buf[1] == 0)
#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
{ST21NFCA_HCI_DRIVER_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
struct st21nfca_i2c_phy {
struct i2c_client *i2c_dev;
struct nfc_hci_dev *hdev;
unsigned int gpio_ena;
unsigned int gpio_irq;
unsigned int irq_polarity;
struct sk_buff *pending_skb;
int current_read_len;
/*
* crc might have fail because i2c macro
* is disable due to other interface activity
*/
int crc_trials;
int powered;
int run_mode;
/*
* < 0 if hardware error occured (e.g. i2c err)
* and prevents normal operation.
*/
int hard_fault;
struct mutex phy_lock;
};
static u8 len_seq[] = { 13, 24, 15, 29 };
static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
#define I2C_DUMP_SKB(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
16, 1, (skb)->data, (skb)->len, 0); \
} while (0)
/*
* In order to get the CLF in a known state we generate an internal reboot
* using a proprietary command.
* Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
* fill buffer.
*/
static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
{
u16 wait_reboot[] = { 50, 300, 1000 };
char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
int i, r = -1;
for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
r = i2c_master_send(phy->i2c_dev, reboot_cmd,
sizeof(reboot_cmd));
if (r < 0)
msleep(wait_reboot[i]);
}
if (r < 0)
return r;
/* CLF is spending about 20ms to do an internal reboot */
msleep(20);
r = -1;
for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
r = i2c_master_recv(phy->i2c_dev, tmp,
ST21NFCA_HCI_LLC_MAX_SIZE);
if (r < 0)
msleep(wait_reboot[i]);
}
if (r < 0)
return r;
for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
tmp[i] == ST21NFCA_SOF_EOF; i++)
;
if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
return -ENODEV;
usleep_range(1000, 1500);
return 0;
}
static int st21nfca_hci_i2c_enable(void *phy_id)
{
struct st21nfca_i2c_phy *phy = phy_id;
gpio_set_value(phy->gpio_ena, 1);
phy->powered = 1;
phy->run_mode = ST21NFCA_HCI_MODE;
usleep_range(10000, 15000);
return 0;
}
static void st21nfca_hci_i2c_disable(void *phy_id)
{
struct st21nfca_i2c_phy *phy = phy_id;
pr_info("\n");
gpio_set_value(phy->gpio_ena, 0);
phy->powered = 0;
}
static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
{
u16 crc;
u8 tmp;
*skb_push(skb, 1) = 0;
crc = crc_ccitt(0xffff, skb->data, skb->len);
crc = ~crc;
tmp = crc & 0x00ff;
*skb_put(skb, 1) = tmp;
tmp = (crc >> 8) & 0x00ff;
*skb_put(skb, 1) = tmp;
}
static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
{
skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
}
/*
* Writing a frame must not return the number of written bytes.
* It must return either zero for success, or <0 for error.
* In addition, it must not alter the skb
*/
static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
{
int r = -1, i, j;
struct st21nfca_i2c_phy *phy = phy_id;
struct i2c_client *client = phy->i2c_dev;
u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
if (phy->hard_fault != 0)
return phy->hard_fault;
/*
* Compute CRC before byte stuffing computation on frame
* Note st21nfca_hci_add_len_crc is doing a byte stuffing
* on its own value
*/
st21nfca_hci_add_len_crc(skb);
/* add ST21NFCA_SOF_EOF on tail */
*skb_put(skb, 1) = ST21NFCA_SOF_EOF;
/* add ST21NFCA_SOF_EOF on head */
*skb_push(skb, 1) = ST21NFCA_SOF_EOF;
/*
* Compute byte stuffing
* if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
* insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
* xor byte with ST21NFCA_BYTE_STUFFING_MASK
*/
tmp[0] = skb->data[0];
for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
if (skb->data[i] == ST21NFCA_SOF_EOF
|| skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
j++;
tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
} else {
tmp[j] = skb->data[i];
}
}
tmp[j] = skb->data[i];
j++;
/*
* Manage sleep mode
* Try 3 times to send data with delay between each
*/
mutex_lock(&phy->phy_lock);
for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
r = i2c_master_send(client, tmp, j);
if (r < 0)
msleep(wait_tab[i]);
}
mutex_unlock(&phy->phy_lock);
if (r >= 0) {
if (r != j)
r = -EREMOTEIO;
else
r = 0;
}
st21nfca_hci_remove_len_crc(skb);
return r;
}
static int get_frame_size(u8 *buf, int buflen)
{
int len = 0;
if (buf[len + 1] == ST21NFCA_SOF_EOF)
return 0;
for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
;
return len;
}
static int check_crc(u8 *buf, int buflen)
{
u16 crc;
crc = crc_ccitt(0xffff, buf, buflen - 2);
crc = ~crc;
if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
pr_err(ST21NFCA_HCI_DRIVER_NAME
": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
buf[buflen - 2]);
pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
16, 2, buf, buflen, false);
return -EPERM;
}
return 0;
}
/*
* Prepare received data for upper layer.
* Received data include byte stuffing, crc and sof/eof
* which is not usable by hci part.
* returns:
* frame size without sof/eof, header and byte stuffing
* -EBADMSG : frame was incorrect and discarded
*/
static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
{
int i, j, r, size;
if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
return -EBADMSG;
size = get_frame_size(skb->data, skb->len);
if (size > 0) {
skb_trim(skb, size);
/* remove ST21NFCA byte stuffing for upper layer */
for (i = 1, j = 0; i < skb->len; i++) {
if (skb->data[i + j] ==
(u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
skb->data[i] = skb->data[i + j + 1]
| ST21NFCA_BYTE_STUFFING_MASK;
i++;
j++;
}
skb->data[i] = skb->data[i + j];
}
/* remove byte stuffing useless byte */
skb_trim(skb, i - j);
/* remove ST21NFCA_SOF_EOF from head */
skb_pull(skb, 1);
r = check_crc(skb->data, skb->len);
if (r != 0) {
i = 0;
return -EBADMSG;
}
/* remove headbyte */
skb_pull(skb, 1);
/* remove crc. Byte Stuffing is already removed here */
skb_trim(skb, skb->len - 2);
return skb->len;
}
return 0;
}
/*
* Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
* that i2c bus will be flushed and that next read will start on a new frame.
* returned skb contains only LLC header and payload.
* returns:
* frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
* end of read)
* -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
* at end of read)
* -EREMOTEIO : i2c read error (fatal)
* -EBADMSG : frame was incorrect and discarded
* (value returned from st21nfca_hci_i2c_repack)
* -EIO : if no ST21NFCA_SOF_EOF is found after reaching
* the read length end sequence
*/
static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
struct sk_buff *skb)
{
int r, i;
u8 len;
u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
struct i2c_client *client = phy->i2c_dev;
if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
len = len_seq[phy->current_read_len];
/*
* Add retry mecanism
* Operation on I2C interface may fail in case of operation on
* RF or SWP interface
*/
r = 0;
mutex_lock(&phy->phy_lock);
for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
r = i2c_master_recv(client, buf, len);
if (r < 0)
msleep(wait_tab[i]);
}
mutex_unlock(&phy->phy_lock);
if (r != len) {
phy->current_read_len = 0;
return -EREMOTEIO;
}
/*
* The first read sequence does not start with SOF.
* Data is corrupeted so we drop it.
*/
if (!phy->current_read_len && buf[0] != ST21NFCA_SOF_EOF) {
skb_trim(skb, 0);
phy->current_read_len = 0;
return -EIO;
} else if (phy->current_read_len &&
IS_START_OF_FRAME(buf)) {
/*
* Previous frame transmission was interrupted and
* the frame got repeated.
* Received frame start with ST21NFCA_SOF_EOF + 00.
*/
skb_trim(skb, 0);
phy->current_read_len = 0;
}
memcpy(skb_put(skb, len), buf, len);
if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
phy->current_read_len = 0;
return st21nfca_hci_i2c_repack(skb);
}
phy->current_read_len++;
return -EAGAIN;
}
return -EIO;
}
/*
* Reads an shdlc frame from the chip. This is not as straightforward as it
* seems. The frame format is data-crc, and corruption can occur anywhere
* while transiting on i2c bus, such that we could read an invalid data.
* The tricky case is when we read a corrupted data or crc. We must detect
* this here in order to determine that data can be transmitted to the hci
* core. This is the reason why we check the crc here.
* The CLF will repeat a frame until we send a RR on that frame.
*
* On ST21NFCA, IRQ goes in idle when read starts. As no size information are
* available in the incoming data, other IRQ might come. Every IRQ will trigger
* a read sequence with different length and will fill the current frame.
* The reception is complete once we reach a ST21NFCA_SOF_EOF.
*/
static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
{
struct st21nfca_i2c_phy *phy = phy_id;
struct i2c_client *client;
int r;
if (!phy || irq != phy->i2c_dev->irq) {
WARN_ON_ONCE(1);
return IRQ_NONE;
}
client = phy->i2c_dev;
dev_dbg(&client->dev, "IRQ\n");
if (phy->hard_fault != 0)
return IRQ_HANDLED;
r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
if (r == -EREMOTEIO) {
phy->hard_fault = r;
nfc_hci_recv_frame(phy->hdev, NULL);
return IRQ_HANDLED;
} else if (r == -EAGAIN || r == -EIO) {
return IRQ_HANDLED;
} else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
/*
* With ST21NFCA, only one interface (I2C, RF or SWP)
* may be active at a time.
* Having incorrect crc is usually due to i2c macrocell
* deactivation in the middle of a transmission.
* It may generate corrupted data on i2c.
* We give sometime to get i2c back.
* The complete frame will be repeated.
*/
msleep(wait_tab[phy->crc_trials]);
phy->crc_trials++;
phy->current_read_len = 0;
kfree_skb(phy->pending_skb);
} else if (r > 0) {
/*
* We succeeded to read data from the CLF and
* data is valid.
* Reset counter.
*/
nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
phy->crc_trials = 0;
}
phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
if (phy->pending_skb == NULL) {
phy->hard_fault = -ENOMEM;
nfc_hci_recv_frame(phy->hdev, NULL);
}
return IRQ_HANDLED;
}
static struct nfc_phy_ops i2c_phy_ops = {
.write = st21nfca_hci_i2c_write,
.enable = st21nfca_hci_i2c_enable,
.disable = st21nfca_hci_i2c_disable,
};
#ifdef CONFIG_OF
static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
{
struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
struct device_node *pp;
int gpio;
int r;
pp = client->dev.of_node;
if (!pp)
return -ENODEV;
/* Get GPIO from device tree */
gpio = of_get_named_gpio(pp, "enable-gpios", 0);
if (gpio < 0) {
nfc_err(&client->dev, "Failed to retrieve enable-gpios from device tree\n");
return gpio;
}
/* GPIO request and configuration */
r = devm_gpio_request(&client->dev, gpio, "clf_enable");
if (r) {
nfc_err(&client->dev, "Failed to request enable pin\n");
return -ENODEV;
}
r = gpio_direction_output(gpio, 1);
if (r) {
nfc_err(&client->dev, "Failed to set enable pin direction as output\n");
return -ENODEV;
}
phy->gpio_ena = gpio;
/* IRQ */
r = irq_of_parse_and_map(pp, 0);
if (r < 0) {
nfc_err(&client->dev,
"Unable to get irq, error: %d\n", r);
return r;
}
phy->irq_polarity = irq_get_trigger_type(r);
client->irq = r;
return 0;
}
#else
static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
{
return -ENODEV;
}
#endif
static int st21nfca_hci_i2c_request_resources(struct i2c_client *client)
{
struct st21nfca_nfc_platform_data *pdata;
struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
int r;
int irq;
pdata = client->dev.platform_data;
if (pdata == NULL) {
nfc_err(&client->dev, "No platform data\n");
return -EINVAL;
}
/* store for later use */
phy->gpio_irq = pdata->gpio_irq;
phy->gpio_ena = pdata->gpio_ena;
phy->irq_polarity = pdata->irq_polarity;
r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
if (r) {
pr_err("%s : gpio_request failed\n", __FILE__);
return -ENODEV;
}
r = gpio_direction_input(phy->gpio_irq);
if (r) {
pr_err("%s : gpio_direction_input failed\n", __FILE__);
return -ENODEV;
}
if (phy->gpio_ena > 0) {
r = devm_gpio_request(&client->dev,
phy->gpio_ena, "clf_enable");
if (r) {
pr_err("%s : ena gpio_request failed\n", __FILE__);
return -ENODEV;
}
r = gpio_direction_output(phy->gpio_ena, 1);
if (r) {
pr_err("%s : ena gpio_direction_output failed\n",
__FILE__);
return -ENODEV;
}
}
/* IRQ */
irq = gpio_to_irq(phy->gpio_irq);
if (irq < 0) {
nfc_err(&client->dev,
"Unable to get irq number for GPIO %d error %d\n",
phy->gpio_irq, r);
return -ENODEV;
}
client->irq = irq;
return 0;
}
static int st21nfca_hci_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct st21nfca_i2c_phy *phy;
struct st21nfca_nfc_platform_data *pdata;
int r;
dev_dbg(&client->dev, "%s\n", __func__);
dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
return -ENODEV;
}
phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
GFP_KERNEL);
if (!phy) {
nfc_err(&client->dev,
"Cannot allocate memory for st21nfca i2c phy.\n");
return -ENOMEM;
}
phy->i2c_dev = client;
phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
if (phy->pending_skb == NULL)
return -ENOMEM;
phy->current_read_len = 0;
phy->crc_trials = 0;
mutex_init(&phy->phy_lock);
i2c_set_clientdata(client, phy);
pdata = client->dev.platform_data;
if (!pdata && client->dev.of_node) {
r = st21nfca_hci_i2c_of_request_resources(client);
if (r) {
nfc_err(&client->dev, "No platform data\n");
return r;
}
} else if (pdata) {
r = st21nfca_hci_i2c_request_resources(client);
if (r) {
nfc_err(&client->dev, "Cannot get platform resources\n");
return r;
}
} else {
nfc_err(&client->dev, "st21nfca platform resources not available\n");
return -ENODEV;
}
r = st21nfca_hci_platform_init(phy);
if (r < 0) {
nfc_err(&client->dev, "Unable to reboot st21nfca\n");
return -ENODEV;
}
r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
st21nfca_hci_irq_thread_fn,
phy->irq_polarity | IRQF_ONESHOT,
ST21NFCA_HCI_DRIVER_NAME, phy);
if (r < 0) {
nfc_err(&client->dev, "Unable to register IRQ handler\n");
return r;
}
return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
}
static int st21nfca_hci_i2c_remove(struct i2c_client *client)
{
struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
dev_dbg(&client->dev, "%s\n", __func__);
st21nfca_hci_remove(phy->hdev);
if (phy->powered)
st21nfca_hci_i2c_disable(phy);
return 0;
}
static const struct of_device_id of_st21nfca_i2c_match[] = {
{ .compatible = "st,st21nfca_i2c", },
{}
};
static struct i2c_driver st21nfca_hci_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = ST21NFCA_HCI_I2C_DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(of_st21nfca_i2c_match),
},
.probe = st21nfca_hci_i2c_probe,
.id_table = st21nfca_hci_i2c_id_table,
.remove = st21nfca_hci_i2c_remove,
};
module_i2c_driver(st21nfca_hci_i2c_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);
/*
* HCI based Driver for STMicroelectronics NFC Chip
*
* Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "st21nfca.h"
#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
#define FULL_VERSION_LEN 3
/* Proprietary gates, events, commands and registers */
/* Commands that apply to all RF readers */
#define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
#define ST21NFCA_RF_READER_ISO15693_GATE 0x12
#define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
/*
* Reader gate for communication with contact-less cards using Type A
* protocol ISO14443-3 but not compliant with ISO14443-4
*/
#define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
#define ST21NFCA_RF_READER_14443_3_A_UID 0x02
#define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
#define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
#define ST21NFCA_DEVICE_MGNT_GATE 0x01
#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
#define ST21NFCA_DM_GETINFO 0x13
#define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
#define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
#define ST21NFCA_DM_PIPE_CREATED 0x02
#define ST21NFCA_DM_PIPE_OPEN 0x04
#define ST21NFCA_DM_RF_ACTIVE 0x80
#define ST21NFCA_DM_IS_PIPE_OPEN(p) \
((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
static struct nfc_hci_gate st21nfca_gates[] = {
{NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
{NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
{NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
{NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
{NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
{NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
{ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
{ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
{ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
{ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
};
struct st21nfca_pipe_info {
u8 pipe_state;
u8 src_host_id;
u8 src_gate_id;
u8 dst_host_id;
u8 dst_gate_id;
} __packed;
/* Largest headroom needed for outgoing custom commands */
#define ST21NFCA_CMDS_HEADROOM 7
static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
{
int i, j, r;
struct sk_buff *skb_pipe_list, *skb_pipe_info;
struct st21nfca_pipe_info *info;
u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
NFC_HCI_TERMINAL_HOST_ID
};
u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
NFC_HCI_TERMINAL_HOST_ID, 0
};
skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
if (!skb_pipe_list) {
r = -ENOMEM;
goto free_list;
}
skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
if (!skb_pipe_info) {
r = -ENOMEM;
goto free_info;
}
/* On ST21NFCA device pipes number are dynamics
* A maximum of 16 pipes can be created at the same time
* If pipes are already created, hci_dev_up will fail.
* Doing a clear all pipe is a bad idea because:
* - It does useless EEPROM cycling
* - It might cause issue for secure elements support
* (such as removing connectivity or APDU reader pipe)
* A better approach on ST21NFCA is to:
* - get a pipe list for each host.
* (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
* (TODO Later on UICC HOST and eSE HOST)
* - get pipe information
* - match retrieved pipe list in st21nfca_gates
* ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
* with ST21NFCA_DEVICE_MGNT_PIPE.
* Pipe can be closed and need to be open.
*/
r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
if (r < 0)
goto free_info;
/* Get pipe list */
r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
&skb_pipe_list);
if (r < 0)
goto free_info;
/* Complete the existing gate_pipe table */
for (i = 0; i < skb_pipe_list->len; i++) {
pipe_info[2] = skb_pipe_list->data[i];
r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_DM_GETINFO, pipe_info,
sizeof(pipe_info), &skb_pipe_info);
if (r)
continue;
/*
* Match pipe ID and gate ID
* Output format from ST21NFC_DM_GETINFO is:
* - pipe state (1byte)
* - source hid (1byte)
* - source gid (1byte)
* - destination hid (1byte)
* - destination gid (1byte)
*/
info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
(st21nfca_gates[j].gate != info->dst_gate_id);
j++)
;
if (j < ARRAY_SIZE(st21nfca_gates) &&
st21nfca_gates[j].gate == info->dst_gate_id &&
ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
st21nfca_gates[j].pipe = pipe_info[2];
hdev->gate2pipe[st21nfca_gates[j].gate] =
st21nfca_gates[j].pipe;
}
}
/*
* 3 gates have a well known pipe ID.
* They will never appear in the pipe list
*/
if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
for (i = skb_pipe_list->len + 3;
i < ARRAY_SIZE(st21nfca_gates); i++) {
r = nfc_hci_connect_gate(hdev,
NFC_HCI_HOST_CONTROLLER_ID,
st21nfca_gates[i].gate,
st21nfca_gates[i].pipe);
if (r < 0)
goto free_info;
}
}
memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
free_info:
kfree_skb(skb_pipe_info);
free_list:
kfree_skb(skb_pipe_list);
return r;
}
static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
int r;
mutex_lock(&info->info_lock);
if (info->state != ST21NFCA_ST_COLD) {
r = -EBUSY;
goto out;
}
r = info->phy_ops->enable(info->phy_id);
if (r == 0)
info->state = ST21NFCA_ST_READY;
out:
mutex_unlock(&info->info_lock);
return r;
}
static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
mutex_lock(&info->info_lock);
if (info->state == ST21NFCA_ST_COLD)
goto out;
info->phy_ops->disable(info->phy_id);
info->state = ST21NFCA_ST_COLD;
out:
mutex_unlock(&info->info_lock);
}
static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
{
struct sk_buff *skb;
u8 param;
int r;
param = NFC_HCI_UICC_HOST_ID;
r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
NFC_HCI_ADMIN_WHITELIST, &param, 1);
if (r < 0)
return r;
/* Set NFC_MODE in device management gate to enable */
r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_NFC_MODE, &skb);
if (r < 0)
return r;
if (skb->data[0] == 0) {
kfree_skb(skb);
param = 1;
r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_NFC_MODE, &param, 1);
if (r < 0)
return r;
}
r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
if (r < 0)
return r;
r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
NFC_HCI_ID_MGMT_VERSION_SW, &skb);
if (r < 0)
return r;
if (skb->len != FULL_VERSION_LEN) {
kfree_skb(skb);
return -EINVAL;
}
print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
DUMP_PREFIX_NONE, 16, 1,
skb->data, FULL_VERSION_LEN, false);
kfree_skb(skb);
return 0;
}
static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
return info->phy_ops->write(info->phy_id, skb);
}
static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
u32 im_protocols, u32 tm_protocols)
{
int r;
pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
__func__, im_protocols, tm_protocols);
r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
if (r < 0)
return r;
if (im_protocols) {
/*
* enable polling according to im_protocols & tm_protocols
* - CLOSE pipe according to im_protocols & tm_protocols
*/
if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
r = nfc_hci_disconnect_gate(hdev,
NFC_HCI_RF_READER_B_GATE);
if (r < 0)
return r;
}
if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
r = nfc_hci_disconnect_gate(hdev,
NFC_HCI_RF_READER_A_GATE);
if (r < 0)
return r;
}
if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
r = nfc_hci_disconnect_gate(hdev,
ST21NFCA_RF_READER_F_GATE);
if (r < 0)
return r;
}
if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
r = nfc_hci_disconnect_gate(hdev,
ST21NFCA_RF_READER_14443_3_A_GATE);
if (r < 0)
return r;
}
if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
r = nfc_hci_disconnect_gate(hdev,
ST21NFCA_RF_READER_ISO15693_GATE);
if (r < 0)
return r;
}
r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
if (r < 0)
nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
}
return r;
}
static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
{
int r;
struct sk_buff *atqa_skb = NULL;
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
if (r < 0)
goto exit;
if (atqa_skb->len != 2) {
r = -EPROTO;
goto exit;
}
*atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
exit:
kfree_skb(atqa_skb);
return r;
}
static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
{
int r;
struct sk_buff *sak_skb = NULL;
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
if (r < 0)
goto exit;
if (sak_skb->len != 1) {
r = -EPROTO;
goto exit;
}
*sak = sak_skb->data[0];
exit:
kfree_skb(sak_skb);
return r;
}
static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
int *len)
{
int r;
struct sk_buff *uid_skb = NULL;
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
if (r < 0)
goto exit;
if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
r = -EPROTO;
goto exit;
}
gate = uid_skb->data;
*len = uid_skb->len;
exit:
kfree_skb(uid_skb);
return r;
}
static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
struct nfc_target *target)
{
int r;
struct sk_buff *inventory_skb = NULL;
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
ST21NFCA_RF_READER_ISO15693_INVENTORY,
&inventory_skb);
if (r < 0)
goto exit;
skb_pull(inventory_skb, 2);
if (inventory_skb->len == 0 ||
inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
r = -EPROTO;
goto exit;
}
memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
target->iso15693_dsfid = inventory_skb->data[1];
target->is_iso15693 = 1;
exit:
kfree_skb(inventory_skb);
return r;
}
static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
struct nfc_target *target)
{
int r, len;
u16 atqa;
u8 sak;
u8 uid[NFC_NFCID1_MAXSIZE];
switch (gate) {
case ST21NFCA_RF_READER_F_GATE:
target->supported_protocols = NFC_PROTO_FELICA_MASK;
break;
case ST21NFCA_RF_READER_14443_3_A_GATE:
/* ISO14443-3 type 1 or 2 tags */
r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
if (r < 0)
return r;
if (atqa == 0x000c) {
target->supported_protocols = NFC_PROTO_JEWEL_MASK;
target->sens_res = 0x0c00;
} else {
r = st21nfca_get_iso14443_3_sak(hdev, &sak);
if (r < 0)
return r;
r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
if (r < 0)
return r;
target->supported_protocols =
nfc_hci_sak_to_protocol(sak);
if (target->supported_protocols == 0xffffffff)
return -EPROTO;
target->sens_res = atqa;
target->sel_res = sak;
memcpy(target->nfcid1, uid, len);
target->nfcid1_len = len;
}
break;
case ST21NFCA_RF_READER_ISO15693_GATE:
target->supported_protocols = NFC_PROTO_ISO15693_MASK;
r = st21nfca_get_iso15693_inventory(hdev, target);
if (r < 0)
return r;
break;
default:
return -EPROTO;
}
return 0;
}
#define ST21NFCA_CB_TYPE_READER_ISO15693 1
static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
int err)
{
struct st21nfca_hci_info *info = context;
switch (info->async_cb_type) {
case ST21NFCA_CB_TYPE_READER_ISO15693:
if (err == 0)
skb_trim(skb, skb->len - 1);
info->async_cb(info->async_cb_context, skb, err);
break;
default:
if (err == 0)
kfree_skb(skb);
break;
}
}
/*
* Returns:
* <= 0: driver handled the data exchange
* 1: driver doesn't especially handle, please do standard processing
*/
static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
struct nfc_target *target,
struct sk_buff *skb,
data_exchange_cb_t cb, void *cb_context)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
target->hci_reader_gate, skb->len);
switch (target->hci_reader_gate) {
case ST21NFCA_RF_READER_F_GATE:
*skb_push(skb, 1) = 0x1a;
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
ST21NFCA_WR_XCHG_DATA, skb->data,
skb->len, cb, cb_context);
case ST21NFCA_RF_READER_14443_3_A_GATE:
*skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
ST21NFCA_WR_XCHG_DATA, skb->data,
skb->len, cb, cb_context);
case ST21NFCA_RF_READER_ISO15693_GATE:
info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
info->async_cb = cb;
info->async_cb_context = cb_context;
*skb_push(skb, 1) = 0x17;
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
ST21NFCA_WR_XCHG_DATA, skb->data,
skb->len,
st21nfca_hci_data_exchange_cb,
info);
break;
default:
return 1;
}
}
static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
struct nfc_target *target)
{
u8 fwi = 0x11;
switch (target->hci_reader_gate) {
case NFC_HCI_RF_READER_A_GATE:
case NFC_HCI_RF_READER_B_GATE:
/*
* PRESENCE_CHECK on those gates is available
* However, the answer to this command is taking 3 * fwi
* if the card is no present.
* Instead, we send an empty I-Frame with a very short
* configurable fwi ~604µs.
*/
return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
case ST21NFCA_RF_READER_14443_3_A_GATE:
return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
NULL, 0, NULL);
default:
return -EOPNOTSUPP;
}
}
static struct nfc_hci_ops st21nfca_hci_ops = {
.open = st21nfca_hci_open,
.close = st21nfca_hci_close,
.load_session = st21nfca_hci_load_session,
.hci_ready = st21nfca_hci_ready,
.xmit = st21nfca_hci_xmit,
.start_poll = st21nfca_hci_start_poll,
.target_from_gate = st21nfca_hci_target_from_gate,
.im_transceive = st21nfca_hci_im_transceive,
.check_presence = st21nfca_hci_check_presence,
};
int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
char *llc_name, int phy_headroom, int phy_tailroom,
int phy_payload, struct nfc_hci_dev **hdev)
{
struct st21nfca_hci_info *info;
int r = 0;
int dev_num;
u32 protocols;
struct nfc_hci_init_data init_data;
unsigned long quirks = 0;
info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
if (!info) {
r = -ENOMEM;
goto err_alloc_hdev;
}
info->phy_ops = phy_ops;
info->phy_id = phy_id;
info->state = ST21NFCA_ST_COLD;
mutex_init(&info->info_lock);
init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
/*
* Session id must include the driver name + i2c bus addr
* persistent info to discriminate 2 identical chips
*/
dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
if (dev_num >= ST21NFCA_NUM_DEVICES)
goto err_alloc_hdev;
scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
"ST21AH", dev_num);
protocols = NFC_PROTO_JEWEL_MASK |
NFC_PROTO_MIFARE_MASK |
NFC_PROTO_FELICA_MASK |
NFC_PROTO_ISO14443_MASK |
NFC_PROTO_ISO14443_B_MASK |
NFC_PROTO_ISO15693_MASK;
set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
info->hdev =
nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
protocols, llc_name,
phy_headroom + ST21NFCA_CMDS_HEADROOM,
phy_tailroom, phy_payload);
if (!info->hdev) {
pr_err("Cannot allocate nfc hdev.\n");
r = -ENOMEM;
goto err_alloc_hdev;
}
nfc_hci_set_clientdata(info->hdev, info);
r = nfc_hci_register_device(info->hdev);
if (r)
goto err_regdev;
*hdev = info->hdev;
return 0;
err_regdev:
nfc_hci_free_device(info->hdev);
err_alloc_hdev:
kfree(info);
return r;
}
EXPORT_SYMBOL(st21nfca_hci_probe);
void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
nfc_hci_unregister_device(hdev);
nfc_hci_free_device(hdev);
kfree(info);
}
EXPORT_SYMBOL(st21nfca_hci_remove);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);
/*
* Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LOCAL_ST21NFCA_H_
#define __LOCAL_ST21NFCA_H_
#include <net/nfc/hci.h>
#define HCI_MODE 0
/* framing in HCI mode */
#define ST21NFCA_SOF_EOF_LEN 2
/* Almost every time value is 0 */
#define ST21NFCA_HCI_LLC_LEN 1
/* Size in worst case :
* In normal case CRC len = 2 but byte stuffing
* may appear in case one CRC byte = ST21NFCA_SOF_EOF
*/
#define ST21NFCA_HCI_LLC_CRC 4
#define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
ST21NFCA_HCI_LLC_LEN + \
ST21NFCA_HCI_LLC_CRC)
#define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
/* Worst case when adding byte stuffing between each byte */
#define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
#define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
ST21NFCA_HCI_LLC_MAX_PAYLOAD)
#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
#define ST21NFCA_HCI_MODE 0
#define ST21NFCA_NUM_DEVICES 256
int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
char *llc_name, int phy_headroom, int phy_tailroom,
int phy_payload, struct nfc_hci_dev **hdev);
void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
enum st21nfca_state {
ST21NFCA_ST_COLD,
ST21NFCA_ST_READY,
};
struct st21nfca_hci_info {
struct nfc_phy_ops *phy_ops;
void *phy_id;
struct nfc_hci_dev *hdev;
enum st21nfca_state state;
struct mutex info_lock;
int async_cb_type;
data_exchange_cb_t async_cb;
void *async_cb_context;
} __packed;
/* Reader RF commands */
#define ST21NFCA_WR_XCHG_DATA 0x10
#define ST21NFCA_RF_READER_F_GATE 0x14
#define ST21NFCA_RF_READER_F_DATARATE 0x01
#define ST21NFCA_RF_READER_F_DATARATE_106 0x01
#define ST21NFCA_RF_READER_F_DATARATE_212 0x02
#define ST21NFCA_RF_READER_F_DATARATE_424 0x04
#endif /* __LOCAL_ST21NFCA_H_ */
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/pm_runtime.h>
#include <linux/nfc.h> #include <linux/nfc.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
#include <linux/delay.h> #include <linux/delay.h>
...@@ -67,14 +68,14 @@ ...@@ -67,14 +68,14 @@
* only the SRX bit set, it means that all of the data has been received * only the SRX bit set, it means that all of the data has been received
* (once what's in the fifo has been read). However, depending on timing * (once what's in the fifo has been read). However, depending on timing
* an interrupt status with only the SRX bit set may not be recived. In * an interrupt status with only the SRX bit set may not be recived. In
* those cases, the timeout mechanism is used to wait 5 ms in case more * those cases, the timeout mechanism is used to wait 20 ms in case more
* data arrives. After 5 ms, it is assumed that all of the data has been * data arrives. After 20 ms, it is assumed that all of the data has been
* received and the accumulated rx data is sent upstream. The * received and the accumulated rx data is sent upstream. The
* 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose * 'TRF7970A_ST_WAIT_FOR_RX_DATA_CONT' state is used for this purpose
* (i.e., it indicates that some data has been received but we're not sure * (i.e., it indicates that some data has been received but we're not sure
* if there is more coming so a timeout in this state means all data has * if there is more coming so a timeout in this state means all data has
* been received and there isn't an error). The delay is 5 ms since delays * been received and there isn't an error). The delay is 20 ms since delays
* over 2 ms have been observed during testing (a little extra just in case). * of ~16 ms have been observed during testing.
* *
* Type 2 write and sector select commands respond with a 4-bit ACK or NACK. * Type 2 write and sector select commands respond with a 4-bit ACK or NACK.
* Having only 4 bits in the FIFO won't normally generate an interrupt so * Having only 4 bits in the FIFO won't normally generate an interrupt so
...@@ -104,7 +105,9 @@ ...@@ -104,7 +105,9 @@
#define TRF7970A_SUPPORTED_PROTOCOLS \ #define TRF7970A_SUPPORTED_PROTOCOLS \
(NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \ (NFC_PROTO_MIFARE_MASK | NFC_PROTO_ISO14443_MASK | \
NFC_PROTO_ISO15693_MASK) NFC_PROTO_ISO14443_B_MASK | NFC_PROTO_ISO15693_MASK)
#define TRF7970A_AUTOSUSPEND_DELAY 30000 /* 30 seconds */
/* TX data must be prefixed with a FIFO reset cmd, a cmd that depends /* TX data must be prefixed with a FIFO reset cmd, a cmd that depends
* on what the current framing is, the address of the TX length byte 1 * on what the current framing is, the address of the TX length byte 1
...@@ -120,7 +123,7 @@ ...@@ -120,7 +123,7 @@
/* TX length is 3 nibbles long ==> 4KB - 1 bytes max */ /* TX length is 3 nibbles long ==> 4KB - 1 bytes max */
#define TRF7970A_TX_MAX (4096 - 1) #define TRF7970A_TX_MAX (4096 - 1)
#define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 5 #define TRF7970A_WAIT_FOR_RX_DATA_TIMEOUT 20
#define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3 #define TRF7970A_WAIT_FOR_FIFO_DRAIN_TIMEOUT 3
#define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20 #define TRF7970A_WAIT_TO_ISSUE_ISO15693_EOF 20
...@@ -330,13 +333,15 @@ struct trf7970a { ...@@ -330,13 +333,15 @@ struct trf7970a {
struct regulator *regulator; struct regulator *regulator;
struct nfc_digital_dev *ddev; struct nfc_digital_dev *ddev;
u32 quirks; u32 quirks;
bool powering_up;
bool aborting; bool aborting;
struct sk_buff *tx_skb; struct sk_buff *tx_skb;
struct sk_buff *rx_skb; struct sk_buff *rx_skb;
nfc_digital_cmd_complete_t cb; nfc_digital_cmd_complete_t cb;
void *cb_arg; void *cb_arg;
u8 chip_status_ctrl;
u8 iso_ctrl; u8 iso_ctrl;
u8 iso_ctrl_tech;
u8 modulator_sys_clk_ctrl;
u8 special_fcn_reg1; u8 special_fcn_reg1;
int technology; int technology;
int framing; int framing;
...@@ -681,7 +686,9 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id) ...@@ -681,7 +686,9 @@ static irqreturn_t trf7970a_irq(int irq, void *dev_id)
trf->ignore_timeout = trf->ignore_timeout =
!cancel_delayed_work(&trf->timeout_work); !cancel_delayed_work(&trf->timeout_work);
trf7970a_drain_fifo(trf, status); trf7970a_drain_fifo(trf, status);
} else if (!(status & TRF7970A_IRQ_STATUS_TX)) { } else if (status == TRF7970A_IRQ_STATUS_TX) {
trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
} else {
trf7970a_send_err_upstream(trf, -EIO); trf7970a_send_err_upstream(trf, -EIO);
} }
break; break;
...@@ -757,8 +764,8 @@ static int trf7970a_init(struct trf7970a *trf) ...@@ -757,8 +764,8 @@ static int trf7970a_init(struct trf7970a *trf)
if (ret) if (ret)
goto err_out; goto err_out;
ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL, /* Must clear NFC Target Detection Level reg due to erratum */
TRF7970A_MODULATOR_DEPTH_OOK); ret = trf7970a_write(trf, TRF7970A_NFC_TARGET_LEVEL, 0);
if (ret) if (ret)
goto err_out; goto err_out;
...@@ -774,12 +781,7 @@ static int trf7970a_init(struct trf7970a *trf) ...@@ -774,12 +781,7 @@ static int trf7970a_init(struct trf7970a *trf)
trf->special_fcn_reg1 = 0; trf->special_fcn_reg1 = 0;
ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->iso_ctrl = 0xff;
TRF7970A_CHIP_STATUS_RF_ON |
TRF7970A_CHIP_STATUS_VRS5_3);
if (ret)
goto err_out;
return 0; return 0;
err_out: err_out:
...@@ -791,53 +793,29 @@ static void trf7970a_switch_rf_off(struct trf7970a *trf) ...@@ -791,53 +793,29 @@ static void trf7970a_switch_rf_off(struct trf7970a *trf)
{ {
dev_dbg(trf->dev, "Switching rf off\n"); dev_dbg(trf->dev, "Switching rf off\n");
gpio_set_value(trf->en_gpio, 0); trf->chip_status_ctrl &= ~TRF7970A_CHIP_STATUS_RF_ON;
gpio_set_value(trf->en2_gpio, 0);
trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL, trf->chip_status_ctrl);
trf->aborting = false; trf->aborting = false;
trf->state = TRF7970A_ST_OFF; trf->state = TRF7970A_ST_OFF;
pm_runtime_mark_last_busy(trf->dev);
pm_runtime_put_autosuspend(trf->dev);
} }
static int trf7970a_switch_rf_on(struct trf7970a *trf) static void trf7970a_switch_rf_on(struct trf7970a *trf)
{ {
unsigned long delay;
int ret;
dev_dbg(trf->dev, "Switching rf on\n"); dev_dbg(trf->dev, "Switching rf on\n");
if (trf->powering_up) pm_runtime_get_sync(trf->dev);
usleep_range(5000, 6000);
gpio_set_value(trf->en2_gpio, 1);
usleep_range(1000, 2000);
gpio_set_value(trf->en_gpio, 1);
/* The delay between enabling the trf7970a and issuing the first trf->state = TRF7970A_ST_IDLE;
* command is significantly longer the very first time after powering
* up. Make sure the longer delay is only done the first time.
*/
if (trf->powering_up) {
delay = 20000;
trf->powering_up = false;
} else {
delay = 5000;
}
usleep_range(delay, delay + 1000);
ret = trf7970a_init(trf);
if (ret)
trf7970a_switch_rf_off(trf);
else
trf->state = TRF7970A_ST_IDLE;
return ret;
} }
static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
{ {
struct trf7970a *trf = nfc_digital_get_drvdata(ddev); struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
int ret = 0;
dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on); dev_dbg(trf->dev, "Switching RF - state: %d, on: %d\n", trf->state, on);
...@@ -846,7 +824,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) ...@@ -846,7 +824,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
if (on) { if (on) {
switch (trf->state) { switch (trf->state) {
case TRF7970A_ST_OFF: case TRF7970A_ST_OFF:
ret = trf7970a_switch_rf_on(trf); trf7970a_switch_rf_on(trf);
break; break;
case TRF7970A_ST_IDLE: case TRF7970A_ST_IDLE:
case TRF7970A_ST_IDLE_RX_BLOCKED: case TRF7970A_ST_IDLE_RX_BLOCKED:
...@@ -871,7 +849,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on) ...@@ -871,7 +849,7 @@ static int trf7970a_switch_rf(struct nfc_digital_dev *ddev, bool on)
} }
mutex_unlock(&trf->lock); mutex_unlock(&trf->lock);
return ret; return 0;
} }
static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
...@@ -882,10 +860,16 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech) ...@@ -882,10 +860,16 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
switch (tech) { switch (tech) {
case NFC_DIGITAL_RF_TECH_106A: case NFC_DIGITAL_RF_TECH_106A:
trf->iso_ctrl = TRF7970A_ISO_CTRL_14443A_106; trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443A_106;
trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
break;
case NFC_DIGITAL_RF_TECH_106B:
trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_14443B_106;
trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_ASK10;
break; break;
case NFC_DIGITAL_RF_TECH_ISO15693: case NFC_DIGITAL_RF_TECH_ISO15693:
trf->iso_ctrl = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648; trf->iso_ctrl_tech = TRF7970A_ISO_CTRL_15693_SGL_1OF4_2648;
trf->modulator_sys_clk_ctrl = TRF7970A_MODULATOR_DEPTH_OOK;
break; break;
default: default:
dev_dbg(trf->dev, "Unsupported rf technology: %d\n", tech); 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,24 +883,29 @@ static int trf7970a_config_rf_tech(struct trf7970a *trf, int tech)
static int trf7970a_config_framing(struct trf7970a *trf, int framing) static int trf7970a_config_framing(struct trf7970a *trf, int framing)
{ {
u8 iso_ctrl = trf->iso_ctrl_tech;
int ret;
dev_dbg(trf->dev, "framing: %d\n", framing); dev_dbg(trf->dev, "framing: %d\n", framing);
switch (framing) { switch (framing) {
case NFC_DIGITAL_FRAMING_NFCA_SHORT: case NFC_DIGITAL_FRAMING_NFCA_SHORT:
case NFC_DIGITAL_FRAMING_NFCA_STANDARD: case NFC_DIGITAL_FRAMING_NFCA_STANDARD:
trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC; trf->tx_cmd = TRF7970A_CMD_TRANSMIT_NO_CRC;
trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
break; break;
case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A: case NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A:
case NFC_DIGITAL_FRAMING_NFCA_T4T: case NFC_DIGITAL_FRAMING_NFCA_T4T:
case NFC_DIGITAL_FRAMING_NFCB:
case NFC_DIGITAL_FRAMING_NFCB_T4T:
case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY: case NFC_DIGITAL_FRAMING_ISO15693_INVENTORY:
case NFC_DIGITAL_FRAMING_ISO15693_T5T: case NFC_DIGITAL_FRAMING_ISO15693_T5T:
trf->tx_cmd = TRF7970A_CMD_TRANSMIT; trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
trf->iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N; iso_ctrl &= ~TRF7970A_ISO_CTRL_RX_CRC_N;
break; break;
case NFC_DIGITAL_FRAMING_NFCA_T2T: case NFC_DIGITAL_FRAMING_NFCA_T2T:
trf->tx_cmd = TRF7970A_CMD_TRANSMIT; trf->tx_cmd = TRF7970A_CMD_TRANSMIT;
trf->iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N; iso_ctrl |= TRF7970A_ISO_CTRL_RX_CRC_N;
break; break;
default: default:
dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing); dev_dbg(trf->dev, "Unsupported Framing: %d\n", framing);
...@@ -925,24 +914,46 @@ static int trf7970a_config_framing(struct trf7970a *trf, int framing) ...@@ -925,24 +914,46 @@ static int trf7970a_config_framing(struct trf7970a *trf, int framing)
trf->framing = framing; trf->framing = framing;
return trf7970a_write(trf, TRF7970A_ISO_CTRL, trf->iso_ctrl); if (iso_ctrl != trf->iso_ctrl) {
ret = trf7970a_write(trf, TRF7970A_ISO_CTRL, iso_ctrl);
if (ret)
return ret;
trf->iso_ctrl = iso_ctrl;
ret = trf7970a_write(trf, TRF7970A_MODULATOR_SYS_CLK_CTRL,
trf->modulator_sys_clk_ctrl);
if (ret)
return ret;
}
if (!(trf->chip_status_ctrl & TRF7970A_CHIP_STATUS_RF_ON)) {
ret = trf7970a_write(trf, TRF7970A_CHIP_STATUS_CTRL,
trf->chip_status_ctrl |
TRF7970A_CHIP_STATUS_RF_ON);
if (ret)
return ret;
trf->chip_status_ctrl |= TRF7970A_CHIP_STATUS_RF_ON;
usleep_range(5000, 6000);
}
return 0;
} }
static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
int param) int param)
{ {
struct trf7970a *trf = nfc_digital_get_drvdata(ddev); struct trf7970a *trf = nfc_digital_get_drvdata(ddev);
int ret = 0; int ret;
dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param); dev_dbg(trf->dev, "Configure hw - type: %d, param: %d\n", type, param);
mutex_lock(&trf->lock); mutex_lock(&trf->lock);
if (trf->state == TRF7970A_ST_OFF) { if (trf->state == TRF7970A_ST_OFF)
ret = trf7970a_switch_rf_on(trf); trf7970a_switch_rf_on(trf);
if (ret)
goto err_out;
}
switch (type) { switch (type) {
case NFC_DIGITAL_CONFIG_RF_TECH: case NFC_DIGITAL_CONFIG_RF_TECH:
...@@ -956,7 +967,6 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type, ...@@ -956,7 +967,6 @@ static int trf7970a_in_configure_hw(struct nfc_digital_dev *ddev, int type,
ret = -EINVAL; ret = -EINVAL;
} }
err_out:
mutex_unlock(&trf->lock); mutex_unlock(&trf->lock);
return ret; return ret;
} }
...@@ -1191,7 +1201,18 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev) ...@@ -1191,7 +1201,18 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
dev_dbg(trf->dev, "Abort process initiated\n"); dev_dbg(trf->dev, "Abort process initiated\n");
mutex_lock(&trf->lock); mutex_lock(&trf->lock);
trf->aborting = true;
switch (trf->state) {
case TRF7970A_ST_WAIT_FOR_TX_FIFO:
case TRF7970A_ST_WAIT_FOR_RX_DATA:
case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
trf->aborting = true;
break;
default:
break;
}
mutex_unlock(&trf->lock); mutex_unlock(&trf->lock);
} }
...@@ -1206,12 +1227,25 @@ static struct nfc_digital_ops trf7970a_nfc_ops = { ...@@ -1206,12 +1227,25 @@ static struct nfc_digital_ops trf7970a_nfc_ops = {
.abort_cmd = trf7970a_abort_cmd, .abort_cmd = trf7970a_abort_cmd,
}; };
static int trf7970a_get_autosuspend_delay(struct device_node *np)
{
int autosuspend_delay, ret;
ret = of_property_read_u32(np, "autosuspend-delay", &autosuspend_delay);
if (ret)
autosuspend_delay = TRF7970A_AUTOSUSPEND_DELAY;
of_node_put(np);
return autosuspend_delay;
}
static int trf7970a_probe(struct spi_device *spi) static int trf7970a_probe(struct spi_device *spi)
{ {
struct device_node *np = spi->dev.of_node; struct device_node *np = spi->dev.of_node;
const struct spi_device_id *id = spi_get_device_id(spi); const struct spi_device_id *id = spi_get_device_id(spi);
struct trf7970a *trf; struct trf7970a *trf;
int ret; int uvolts, autosuspend_delay, ret;
if (!np) { if (!np) {
dev_err(&spi->dev, "No Device Tree entry\n"); dev_err(&spi->dev, "No Device Tree entry\n");
...@@ -1281,7 +1315,10 @@ static int trf7970a_probe(struct spi_device *spi) ...@@ -1281,7 +1315,10 @@ static int trf7970a_probe(struct spi_device *spi)
goto err_destroy_lock; goto err_destroy_lock;
} }
trf->powering_up = true; uvolts = regulator_get_voltage(trf->regulator);
if (uvolts > 4000000)
trf->chip_status_ctrl = TRF7970A_CHIP_STATUS_VRS5_3;
trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops, trf->ddev = nfc_digital_allocate_device(&trf7970a_nfc_ops,
TRF7970A_SUPPORTED_PROTOCOLS, TRF7970A_SUPPORTED_PROTOCOLS,
...@@ -1297,6 +1334,12 @@ static int trf7970a_probe(struct spi_device *spi) ...@@ -1297,6 +1334,12 @@ static int trf7970a_probe(struct spi_device *spi)
nfc_digital_set_drvdata(trf->ddev, trf); nfc_digital_set_drvdata(trf->ddev, trf);
spi_set_drvdata(spi, trf); spi_set_drvdata(spi, trf);
autosuspend_delay = trf7970a_get_autosuspend_delay(np);
pm_runtime_set_autosuspend_delay(trf->dev, autosuspend_delay);
pm_runtime_use_autosuspend(trf->dev);
pm_runtime_enable(trf->dev);
ret = nfc_digital_register_device(trf->ddev); ret = nfc_digital_register_device(trf->ddev);
if (ret) { if (ret) {
dev_err(trf->dev, "Can't register NFC digital device: %d\n", 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,6 +1350,7 @@ static int trf7970a_probe(struct spi_device *spi)
return 0; return 0;
err_free_ddev: err_free_ddev:
pm_runtime_disable(trf->dev);
nfc_digital_free_device(trf->ddev); nfc_digital_free_device(trf->ddev);
err_disable_regulator: err_disable_regulator:
regulator_disable(trf->regulator); regulator_disable(trf->regulator);
...@@ -1321,15 +1365,16 @@ static int trf7970a_remove(struct spi_device *spi) ...@@ -1321,15 +1365,16 @@ static int trf7970a_remove(struct spi_device *spi)
mutex_lock(&trf->lock); mutex_lock(&trf->lock);
trf7970a_switch_rf_off(trf);
trf7970a_init(trf);
switch (trf->state) { switch (trf->state) {
case TRF7970A_ST_WAIT_FOR_TX_FIFO: case TRF7970A_ST_WAIT_FOR_TX_FIFO:
case TRF7970A_ST_WAIT_FOR_RX_DATA: case TRF7970A_ST_WAIT_FOR_RX_DATA:
case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT: case TRF7970A_ST_WAIT_FOR_RX_DATA_CONT:
case TRF7970A_ST_WAIT_TO_ISSUE_EOF: case TRF7970A_ST_WAIT_TO_ISSUE_EOF:
trf7970a_send_err_upstream(trf, -ECANCELED); trf7970a_send_err_upstream(trf, -ECANCELED);
/* FALLTHROUGH */
case TRF7970A_ST_IDLE:
case TRF7970A_ST_IDLE_RX_BLOCKED:
pm_runtime_put_sync(trf->dev);
break; break;
default: default:
break; break;
...@@ -1337,6 +1382,8 @@ static int trf7970a_remove(struct spi_device *spi) ...@@ -1337,6 +1382,8 @@ static int trf7970a_remove(struct spi_device *spi)
mutex_unlock(&trf->lock); mutex_unlock(&trf->lock);
pm_runtime_disable(trf->dev);
nfc_digital_unregister_device(trf->ddev); nfc_digital_unregister_device(trf->ddev);
nfc_digital_free_device(trf->ddev); nfc_digital_free_device(trf->ddev);
...@@ -1347,6 +1394,70 @@ static int trf7970a_remove(struct spi_device *spi) ...@@ -1347,6 +1394,70 @@ static int trf7970a_remove(struct spi_device *spi)
return 0; return 0;
} }
#ifdef CONFIG_PM_RUNTIME
static int trf7970a_pm_runtime_suspend(struct device *dev)
{
struct spi_device *spi = container_of(dev, struct spi_device, dev);
struct trf7970a *trf = spi_get_drvdata(spi);
int ret;
dev_dbg(dev, "Runtime suspend\n");
if (trf->state != TRF7970A_ST_OFF) {
dev_dbg(dev, "Can't suspend - not in OFF state (%d)\n",
trf->state);
return -EBUSY;
}
gpio_set_value(trf->en_gpio, 0);
gpio_set_value(trf->en2_gpio, 0);
ret = regulator_disable(trf->regulator);
if (ret)
dev_err(dev, "%s - Can't disable VIN: %d\n", __func__, ret);
return ret;
}
static int trf7970a_pm_runtime_resume(struct device *dev)
{
struct spi_device *spi = container_of(dev, struct spi_device, dev);
struct trf7970a *trf = spi_get_drvdata(spi);
int ret;
dev_dbg(dev, "Runtime resume\n");
ret = regulator_enable(trf->regulator);
if (ret) {
dev_err(dev, "%s - Can't enable VIN: %d\n", __func__, ret);
return ret;
}
usleep_range(5000, 6000);
gpio_set_value(trf->en2_gpio, 1);
usleep_range(1000, 2000);
gpio_set_value(trf->en_gpio, 1);
usleep_range(20000, 21000);
ret = trf7970a_init(trf);
if (ret) {
dev_err(dev, "%s - Can't initialize: %d\n", __func__, ret);
return ret;
}
pm_runtime_mark_last_busy(dev);
return 0;
}
#endif
static const struct dev_pm_ops trf7970a_pm_ops = {
SET_RUNTIME_PM_OPS(trf7970a_pm_runtime_suspend,
trf7970a_pm_runtime_resume, NULL)
};
static const struct spi_device_id trf7970a_id_table[] = { static const struct spi_device_id trf7970a_id_table[] = {
{ "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA }, { "trf7970a", TRF7970A_QUIRK_IRQ_STATUS_READ_ERRATA },
{ } { }
...@@ -1360,6 +1471,7 @@ static struct spi_driver trf7970a_spi_driver = { ...@@ -1360,6 +1471,7 @@ static struct spi_driver trf7970a_spi_driver = {
.driver = { .driver = {
.name = "trf7970a", .name = "trf7970a",
.owner = THIS_MODULE, .owner = THIS_MODULE,
.pm = &trf7970a_pm_ops,
}, },
}; };
......
/*
* Driver include for the ST21NFCA NFC chip.
*
* Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _ST21NFCA_HCI_H_
#define _ST21NFCA_HCI_H_
#include <linux/i2c.h>
#define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
struct st21nfca_nfc_platform_data {
unsigned int gpio_irq;
unsigned int gpio_ena;
unsigned int irq_polarity;
};
#endif /* _ST21NFCA_HCI_H_ */
...@@ -36,6 +36,7 @@ enum { ...@@ -36,6 +36,7 @@ enum {
NFC_DIGITAL_RF_TECH_212F, NFC_DIGITAL_RF_TECH_212F,
NFC_DIGITAL_RF_TECH_424F, NFC_DIGITAL_RF_TECH_424F,
NFC_DIGITAL_RF_TECH_ISO15693, NFC_DIGITAL_RF_TECH_ISO15693,
NFC_DIGITAL_RF_TECH_106B,
NFC_DIGITAL_RF_TECH_LAST, NFC_DIGITAL_RF_TECH_LAST,
}; };
...@@ -62,6 +63,9 @@ enum { ...@@ -62,6 +63,9 @@ enum {
NFC_DIGITAL_FRAMING_ISO15693_INVENTORY, NFC_DIGITAL_FRAMING_ISO15693_INVENTORY,
NFC_DIGITAL_FRAMING_ISO15693_T5T, NFC_DIGITAL_FRAMING_ISO15693_T5T,
NFC_DIGITAL_FRAMING_NFCB,
NFC_DIGITAL_FRAMING_NFCB_T4T,
NFC_DIGITAL_FRAMING_LAST, NFC_DIGITAL_FRAMING_LAST,
}; };
......
...@@ -27,6 +27,7 @@ struct nfc_hci_dev; ...@@ -27,6 +27,7 @@ struct nfc_hci_dev;
struct nfc_hci_ops { struct nfc_hci_ops {
int (*open) (struct nfc_hci_dev *hdev); int (*open) (struct nfc_hci_dev *hdev);
void (*close) (struct nfc_hci_dev *hdev); void (*close) (struct nfc_hci_dev *hdev);
int (*load_session) (struct nfc_hci_dev *hdev);
int (*hci_ready) (struct nfc_hci_dev *hdev); int (*hci_ready) (struct nfc_hci_dev *hdev);
/* /*
* xmit must always send the complete buffer before * xmit must always send the complete buffer before
......
...@@ -264,4 +264,7 @@ int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type); ...@@ -264,4 +264,7 @@ int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type);
int nfc_remove_se(struct nfc_dev *dev, u32 se_idx); int nfc_remove_se(struct nfc_dev *dev, u32 se_idx);
struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx); struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx);
void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
u8 payload_type, u8 direction);
#endif /* __NET_NFC_H */ #endif /* __NET_NFC_H */
...@@ -273,11 +273,19 @@ struct sockaddr_nfc_llcp { ...@@ -273,11 +273,19 @@ struct sockaddr_nfc_llcp {
* First byte is the adapter index * First byte is the adapter index
* Second byte contains flags * Second byte contains flags
* - 0x01 - Direction (0=RX, 1=TX) * - 0x01 - Direction (0=RX, 1=TX)
* - 0x02-0x80 - Reserved * - 0x02-0x04 - Payload type (000=LLCP, 001=NCI, 010=HCI, 011=Digital,
* 100=Proprietary)
* - 0x05-0x80 - Reserved
**/ **/
#define NFC_LLCP_RAW_HEADER_SIZE 2 #define NFC_RAW_HEADER_SIZE 2
#define NFC_LLCP_DIRECTION_RX 0x00 #define NFC_DIRECTION_RX 0x00
#define NFC_LLCP_DIRECTION_TX 0x01 #define NFC_DIRECTION_TX 0x01
#define RAW_PAYLOAD_LLCP 0
#define RAW_PAYLOAD_NCI 1
#define RAW_PAYLOAD_HCI 2
#define RAW_PAYLOAD_DIGITAL 3
#define RAW_PAYLOAD_PROPRIETARY 4
/* socket option names */ /* socket option names */
#define NFC_LLCP_RW 0 #define NFC_LLCP_RW 0
......
...@@ -71,6 +71,7 @@ static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev, ...@@ -71,6 +71,7 @@ static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
void digital_poll_next_tech(struct nfc_digital_dev *ddev); void digital_poll_next_tech(struct nfc_digital_dev *ddev);
int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech); int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech);
int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech); int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech); int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech);
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#define DIGITAL_PROTO_NFCA_RF_TECH \ #define DIGITAL_PROTO_NFCA_RF_TECH \
(NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK) (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
#define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
#define DIGITAL_PROTO_NFCF_RF_TECH \ #define DIGITAL_PROTO_NFCF_RF_TECH \
(NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK) (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
...@@ -345,6 +347,12 @@ int digital_target_found(struct nfc_digital_dev *ddev, ...@@ -345,6 +347,12 @@ int digital_target_found(struct nfc_digital_dev *ddev,
add_crc = digital_skb_add_crc_a; add_crc = digital_skb_add_crc_a;
break; break;
case NFC_PROTO_ISO14443_B:
framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
check_crc = digital_skb_check_crc_b;
add_crc = digital_skb_add_crc_b;
break;
default: default:
pr_err("Invalid protocol %d\n", protocol); pr_err("Invalid protocol %d\n", protocol);
return -EINVAL; return -EINVAL;
...@@ -475,6 +483,10 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, ...@@ -475,6 +483,10 @@ static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
digital_in_send_sens_req); digital_in_send_sens_req);
if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
digital_in_send_sensb_req);
if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
digital_in_send_sensf_req); digital_in_send_sensf_req);
...@@ -635,7 +647,8 @@ static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg, ...@@ -635,7 +647,8 @@ static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
goto done; goto done;
} }
if (ddev->curr_protocol == NFC_PROTO_ISO14443) { if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
(ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
rc = digital_in_iso_dep_pull_sod(ddev, resp); rc = digital_in_iso_dep_pull_sod(ddev, resp);
if (rc) if (rc)
goto done; goto done;
...@@ -676,7 +689,8 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target, ...@@ -676,7 +689,8 @@ static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
goto exit; goto exit;
} }
if (ddev->curr_protocol == NFC_PROTO_ISO14443) { if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
(ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
rc = digital_in_iso_dep_push_sod(ddev, skb); rc = digital_in_iso_dep_push_sod(ddev, skb);
if (rc) if (rc)
goto exit; goto exit;
...@@ -747,6 +761,8 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops, ...@@ -747,6 +761,8 @@ struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
ddev->protocols |= NFC_PROTO_ISO15693_MASK; ddev->protocols |= NFC_PROTO_ISO15693_MASK;
if (supported_protocols & NFC_PROTO_ISO14443_MASK) if (supported_protocols & NFC_PROTO_ISO14443_MASK)
ddev->protocols |= NFC_PROTO_ISO14443_MASK; ddev->protocols |= NFC_PROTO_ISO14443_MASK;
if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
......
...@@ -41,6 +41,24 @@ ...@@ -41,6 +41,24 @@
#define DIGITAL_MIFARE_READ_RES_LEN 16 #define DIGITAL_MIFARE_READ_RES_LEN 16
#define DIGITAL_MIFARE_ACK_RES 0x0A #define DIGITAL_MIFARE_ACK_RES 0x0A
#define DIGITAL_CMD_SENSB_REQ 0x05
#define DIGITAL_SENSB_ADVANCED BIT(5)
#define DIGITAL_SENSB_EXTENDED BIT(4)
#define DIGITAL_SENSB_ALLB_REQ BIT(3)
#define DIGITAL_SENSB_N(n) ((n) & 0x7)
#define DIGITAL_CMD_SENSB_RES 0x50
#define DIGITAL_CMD_ATTRIB_REQ 0x1D
#define DIGITAL_ATTRIB_P1_TR0_DEFAULT (0x0 << 6)
#define DIGITAL_ATTRIB_P1_TR1_DEFAULT (0x0 << 4)
#define DIGITAL_ATTRIB_P1_SUPRESS_EOS BIT(3)
#define DIGITAL_ATTRIB_P1_SUPRESS_SOS BIT(2)
#define DIGITAL_ATTRIB_P2_LISTEN_POLL_1 (0x0 << 6)
#define DIGITAL_ATTRIB_P2_POLL_LISTEN_1 (0x0 << 4)
#define DIGITAL_ATTRIB_P2_MAX_FRAME_256 0x8
#define DIGITAL_ATTRIB_P4_DID(n) ((n) & 0xf)
#define DIGITAL_CMD_SENSF_REQ 0x00 #define DIGITAL_CMD_SENSF_REQ 0x00
#define DIGITAL_CMD_SENSF_RES 0x01 #define DIGITAL_CMD_SENSF_RES 0x01
...@@ -75,6 +93,7 @@ static const u8 digital_ats_fsc[] = { ...@@ -75,6 +93,7 @@ static const u8 digital_ats_fsc[] = {
}; };
#define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F) #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
#define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
#define DIGITAL_ATS_MAX_FSC 256 #define DIGITAL_ATS_MAX_FSC 256
#define DIGITAL_RATS_BYTE1 0xE0 #define DIGITAL_RATS_BYTE1 0xE0
...@@ -92,6 +111,32 @@ struct digital_sel_req { ...@@ -92,6 +111,32 @@ struct digital_sel_req {
u8 bcc; u8 bcc;
} __packed; } __packed;
struct digital_sensb_req {
u8 cmd;
u8 afi;
u8 param;
} __packed;
struct digital_sensb_res {
u8 cmd;
u8 nfcid0[4];
u8 app_data[4];
u8 proto_info[3];
} __packed;
struct digital_attrib_req {
u8 cmd;
u8 nfcid0[4];
u8 param1;
u8 param2;
u8 param3;
u8 param4;
} __packed;
struct digital_attrib_res {
u8 mbli_did;
} __packed;
struct digital_sensf_req { struct digital_sensf_req {
u8 cmd; u8 cmd;
u8 sc1; u8 sc1;
...@@ -531,6 +576,175 @@ int digital_in_recv_mifare_res(struct sk_buff *resp) ...@@ -531,6 +576,175 @@ int digital_in_recv_mifare_res(struct sk_buff *resp)
return -EIO; return -EIO;
} }
static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
struct sk_buff *resp)
{
struct nfc_target *target = arg;
struct digital_attrib_res *attrib_res;
int rc;
if (IS_ERR(resp)) {
rc = PTR_ERR(resp);
resp = NULL;
goto exit;
}
if (resp->len < sizeof(*attrib_res)) {
PROTOCOL_ERR("12.6.2");
rc = -EIO;
goto exit;
}
attrib_res = (struct digital_attrib_res *)resp->data;
if (attrib_res->mbli_did & 0x0f) {
PROTOCOL_ERR("12.6.2.1");
rc = -EIO;
goto exit;
}
rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
exit:
dev_kfree_skb(resp);
kfree(target);
if (rc)
digital_poll_next_tech(ddev);
}
int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
struct nfc_target *target,
struct digital_sensb_res *sensb_res)
{
struct digital_attrib_req *attrib_req;
struct sk_buff *skb;
int rc;
skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
if (!skb)
return -ENOMEM;
attrib_req = (struct digital_attrib_req *)skb_put(skb,
sizeof(*attrib_req));
attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
sizeof(attrib_req->nfcid0));
attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
DIGITAL_ATTRIB_P1_TR1_DEFAULT;
attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
DIGITAL_ATTRIB_P2_MAX_FRAME_256;
attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
target);
if (rc)
kfree_skb(skb);
return rc;
}
static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
struct sk_buff *resp)
{
struct nfc_target *target = NULL;
struct digital_sensb_res *sensb_res;
u8 fsci;
int rc;
if (IS_ERR(resp)) {
rc = PTR_ERR(resp);
resp = NULL;
goto exit;
}
if (resp->len != sizeof(*sensb_res)) {
PROTOCOL_ERR("5.6.2.1");
rc = -EIO;
goto exit;
}
sensb_res = (struct digital_sensb_res *)resp->data;
if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
PROTOCOL_ERR("5.6.2");
rc = -EIO;
goto exit;
}
if (!(sensb_res->proto_info[1] & BIT(0))) {
PROTOCOL_ERR("5.6.2.12");
rc = -EIO;
goto exit;
}
if (sensb_res->proto_info[1] & BIT(3)) {
PROTOCOL_ERR("5.6.2.16");
rc = -EIO;
goto exit;
}
fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
if (fsci >= 8)
ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
else
ddev->target_fsc = digital_ats_fsc[fsci];
target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
if (!target) {
rc = -ENOMEM;
goto exit;
}
rc = digital_in_send_attrib_req(ddev, target, sensb_res);
exit:
dev_kfree_skb(resp);
if (rc) {
kfree(target);
digital_poll_next_tech(ddev);
}
}
int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
{
struct digital_sensb_req *sensb_req;
struct sk_buff *skb;
int rc;
rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
NFC_DIGITAL_RF_TECH_106B);
if (rc)
return rc;
rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
NFC_DIGITAL_FRAMING_NFCB);
if (rc)
return rc;
skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
if (!skb)
return -ENOMEM;
sensb_req = (struct digital_sensb_req *)skb_put(skb,
sizeof(*sensb_req));
sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
sensb_req->afi = 0x00; /* All families and sub-families */
sensb_req->param = DIGITAL_SENSB_N(0);
rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
NULL);
if (rc)
kfree_skb(skb);
return rc;
}
static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg, static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
struct sk_buff *resp) struct sk_buff *resp)
{ {
...@@ -877,6 +1091,18 @@ void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg, ...@@ -877,6 +1091,18 @@ void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
dev_kfree_skb(resp); dev_kfree_skb(resp);
} }
static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
void *arg, struct sk_buff *resp)
{
if (!IS_ERR(resp) && (resp->len >= 2) &&
(resp->data[1] == DIGITAL_CMD_SENSF_REQ))
digital_tg_recv_sensf_req(ddev, arg, resp);
else
digital_tg_recv_atr_req(ddev, arg, resp);
return;
}
static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
struct digital_sensf_req *sensf_req) struct digital_sensf_req *sensf_req)
{ {
...@@ -887,7 +1113,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, ...@@ -887,7 +1113,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
size = sizeof(struct digital_sensf_res); size = sizeof(struct digital_sensf_res);
if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE) if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
size -= sizeof(sensf_res->rd); size -= sizeof(sensf_res->rd);
skb = digital_skb_alloc(ddev, size); skb = digital_skb_alloc(ddev, size);
...@@ -922,7 +1148,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, ...@@ -922,7 +1148,7 @@ static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
digital_skb_add_crc_f(skb); digital_skb_add_crc_f(skb);
rc = digital_tg_send_cmd(ddev, skb, 300, rc = digital_tg_send_cmd(ddev, skb, 300,
digital_tg_recv_atr_req, NULL); digital_tg_recv_atr_or_sensf_req, NULL);
if (rc) if (rc)
kfree_skb(skb); kfree_skb(skb);
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
#include "hci.h" #include "hci.h"
#define MAX_FWI 4949
static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
const u8 *param, size_t param_len, const u8 *param, size_t param_len,
data_exchange_cb_t cb, void *cb_context) 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,7 +39,7 @@ static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
* for all commands? * for all commands?
*/ */
return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd, return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd,
param, param_len, cb, cb_context, 3000); param, param_len, cb, cb_context, MAX_FWI);
} }
/* /*
...@@ -82,7 +84,7 @@ static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd, ...@@ -82,7 +84,7 @@ static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
NFC_HCI_HCP_COMMAND, cmd, NFC_HCI_HCP_COMMAND, cmd,
param, param_len, param, param_len,
nfc_hci_execute_cb, &hcp_ew, nfc_hci_execute_cb, &hcp_ew,
3000); MAX_FWI);
if (hcp_ew.exec_result < 0) if (hcp_ew.exec_result < 0)
return hcp_ew.exec_result; return hcp_ew.exec_result;
......
...@@ -225,7 +225,7 @@ int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate) ...@@ -225,7 +225,7 @@ int nfc_hci_target_discovered(struct nfc_hci_dev *hdev, u8 gate)
goto exit; goto exit;
} }
targets->sens_res = be16_to_cpu(*(u16 *)atqa_skb->data); targets->sens_res = be16_to_cpu(*(__be16 *)atqa_skb->data);
targets->sel_res = sak_skb->data[0]; targets->sel_res = sak_skb->data[0];
r = nfc_hci_get_param(hdev, NFC_HCI_RF_READER_A_GATE, 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,34 +380,31 @@ static int hci_dev_session_init(struct nfc_hci_dev *hdev)
if (r < 0) if (r < 0)
goto disconnect_all; goto disconnect_all;
if (skb->len && skb->len == strlen(hdev->init_data.session_id)) if (skb->len && skb->len == strlen(hdev->init_data.session_id) &&
if (memcmp(hdev->init_data.session_id, skb->data, (memcmp(hdev->init_data.session_id, skb->data,
skb->len) == 0) { skb->len) == 0) && hdev->ops->load_session) {
/* TODO ELa: restore gate<->pipe table from /* Restore gate<->pipe table from some proprietary location. */
* some TBD location.
* note: it doesn't seem possible to get the chip
* currently open gate/pipe table.
* It is only possible to obtain the supported
* gate list.
*/
/* goto exit r = hdev->ops->load_session(hdev);
* For now, always do a full initialization */
}
r = nfc_hci_disconnect_all_gates(hdev); if (r < 0)
if (r < 0) goto disconnect_all;
goto exit; } else {
r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count, r = nfc_hci_disconnect_all_gates(hdev);
hdev->init_data.gates); if (r < 0)
if (r < 0) goto exit;
goto disconnect_all;
r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE, r = hci_dev_connect_gates(hdev, hdev->init_data.gate_count,
NFC_HCI_ADMIN_SESSION_IDENTITY, hdev->init_data.gates);
hdev->init_data.session_id, if (r < 0)
strlen(hdev->init_data.session_id)); goto disconnect_all;
r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
NFC_HCI_ADMIN_SESSION_IDENTITY,
hdev->init_data.session_id,
strlen(hdev->init_data.session_id));
}
if (r == 0) if (r == 0)
goto exit; goto exit;
......
...@@ -387,7 +387,7 @@ int nfc_llcp_send_symm(struct nfc_dev *dev) ...@@ -387,7 +387,7 @@ int nfc_llcp_send_symm(struct nfc_dev *dev)
__net_timestamp(skb); __net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_TX); nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
return nfc_data_exchange(dev, local->target_idx, skb, return nfc_data_exchange(dev, local->target_idx, skb,
nfc_llcp_recv, local); nfc_llcp_recv, local);
......
...@@ -680,16 +680,17 @@ void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local, ...@@ -680,16 +680,17 @@ void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
continue; continue;
if (skb_copy == NULL) { if (skb_copy == NULL) {
skb_copy = __pskb_copy(skb, NFC_LLCP_RAW_HEADER_SIZE, skb_copy = __pskb_copy(skb, NFC_RAW_HEADER_SIZE,
GFP_ATOMIC); GFP_ATOMIC);
if (skb_copy == NULL) if (skb_copy == NULL)
continue; continue;
data = skb_push(skb_copy, NFC_LLCP_RAW_HEADER_SIZE); data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
data[0] = local->dev ? local->dev->idx : 0xFF; data[0] = local->dev ? local->dev->idx : 0xFF;
data[1] = direction; data[1] = direction & 0x01;
data[1] |= (RAW_PAYLOAD_LLCP << 1);
} }
nskb = skb_clone(skb_copy, GFP_ATOMIC); nskb = skb_clone(skb_copy, GFP_ATOMIC);
...@@ -747,7 +748,7 @@ static void nfc_llcp_tx_work(struct work_struct *work) ...@@ -747,7 +748,7 @@ static void nfc_llcp_tx_work(struct work_struct *work)
__net_timestamp(skb); __net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, nfc_llcp_send_to_raw_sock(local, skb,
NFC_LLCP_DIRECTION_TX); NFC_DIRECTION_TX);
ret = nfc_data_exchange(local->dev, local->target_idx, ret = nfc_data_exchange(local->dev, local->target_idx,
skb, nfc_llcp_recv, local); skb, nfc_llcp_recv, local);
...@@ -1476,7 +1477,7 @@ static void nfc_llcp_rx_work(struct work_struct *work) ...@@ -1476,7 +1477,7 @@ static void nfc_llcp_rx_work(struct work_struct *work)
__net_timestamp(skb); __net_timestamp(skb);
nfc_llcp_send_to_raw_sock(local, skb, NFC_LLCP_DIRECTION_RX); nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
nfc_llcp_rx_skb(local, skb); nfc_llcp_rx_skb(local, skb);
......
...@@ -861,6 +861,10 @@ static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb) ...@@ -861,6 +861,10 @@ static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
/* Get rid of skb owner, prior to sending to the driver. */ /* Get rid of skb owner, prior to sending to the driver. */
skb_orphan(skb); skb_orphan(skb);
/* Send copy to sniffer */
nfc_send_to_raw_sock(ndev->nfc_dev, skb,
RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
return ndev->ops->send(ndev, skb); return ndev->ops->send(ndev, skb);
} }
...@@ -935,6 +939,11 @@ static void nci_rx_work(struct work_struct *work) ...@@ -935,6 +939,11 @@ static void nci_rx_work(struct work_struct *work)
struct sk_buff *skb; struct sk_buff *skb;
while ((skb = skb_dequeue(&ndev->rx_q))) { while ((skb = skb_dequeue(&ndev->rx_q))) {
/* Send copy to sniffer */
nfc_send_to_raw_sock(ndev->nfc_dev, skb,
RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
/* Process frame */ /* Process frame */
switch (nci_mt(skb->data)) { switch (nci_mt(skb->data)) {
case NCI_MT_RSP_PKT: case NCI_MT_RSP_PKT:
......
...@@ -366,7 +366,6 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev, ...@@ -366,7 +366,6 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
struct nci_rf_intf_activated_ntf *ntf, __u8 *data) struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
{ {
struct activation_params_poll_nfc_dep *poll; struct activation_params_poll_nfc_dep *poll;
int i;
switch (ntf->activation_rf_tech_and_mode) { switch (ntf->activation_rf_tech_and_mode) {
case NCI_NFC_A_PASSIVE_POLL_MODE: case NCI_NFC_A_PASSIVE_POLL_MODE:
...@@ -374,10 +373,8 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev, ...@@ -374,10 +373,8 @@ static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
poll = &ntf->activation_params.poll_nfc_dep; poll = &ntf->activation_params.poll_nfc_dep;
poll->atr_res_len = min_t(__u8, *data++, 63); poll->atr_res_len = min_t(__u8, *data++, 63);
pr_debug("atr_res_len %d\n", poll->atr_res_len); pr_debug("atr_res_len %d\n", poll->atr_res_len);
if (poll->atr_res_len > 0) { if (poll->atr_res_len > 0)
for (i = 0; i < poll->atr_res_len; i++) memcpy(poll->atr_res, data, poll->atr_res_len);
poll->atr_res[poll->atr_res_len-1-i] = data[i];
}
break; break;
default: default:
......
...@@ -40,6 +40,12 @@ struct nfc_rawsock { ...@@ -40,6 +40,12 @@ struct nfc_rawsock {
struct work_struct tx_work; struct work_struct tx_work;
bool tx_work_scheduled; bool tx_work_scheduled;
}; };
struct nfc_sock_list {
struct hlist_head head;
rwlock_t lock;
};
#define nfc_rawsock(sk) ((struct nfc_rawsock *) sk) #define nfc_rawsock(sk) ((struct nfc_rawsock *) sk)
#define to_rawsock_sk(_tx_work) \ #define to_rawsock_sk(_tx_work) \
((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work)) ((struct sock *) container_of(_tx_work, struct nfc_rawsock, tx_work))
......
...@@ -27,6 +27,24 @@ ...@@ -27,6 +27,24 @@
#include "nfc.h" #include "nfc.h"
static struct nfc_sock_list raw_sk_list = {
.lock = __RW_LOCK_UNLOCKED(raw_sk_list.lock)
};
void nfc_sock_link(struct nfc_sock_list *l, struct sock *sk)
{
write_lock(&l->lock);
sk_add_node(sk, &l->head);
write_unlock(&l->lock);
}
void nfc_sock_unlink(struct nfc_sock_list *l, struct sock *sk)
{
write_lock(&l->lock);
sk_del_node_init(sk);
write_unlock(&l->lock);
}
static void rawsock_write_queue_purge(struct sock *sk) static void rawsock_write_queue_purge(struct sock *sk)
{ {
pr_debug("sk=%p\n", sk); pr_debug("sk=%p\n", sk);
...@@ -57,6 +75,9 @@ static int rawsock_release(struct socket *sock) ...@@ -57,6 +75,9 @@ static int rawsock_release(struct socket *sock)
if (!sk) if (!sk)
return 0; return 0;
if (sock->type == SOCK_RAW)
nfc_sock_unlink(&raw_sk_list, sk);
sock_orphan(sk); sock_orphan(sk);
sock_put(sk); sock_put(sk);
...@@ -275,6 +296,26 @@ static const struct proto_ops rawsock_ops = { ...@@ -275,6 +296,26 @@ static const struct proto_ops rawsock_ops = {
.mmap = sock_no_mmap, .mmap = sock_no_mmap,
}; };
static const struct proto_ops rawsock_raw_ops = {
.family = PF_NFC,
.owner = THIS_MODULE,
.release = rawsock_release,
.bind = sock_no_bind,
.connect = sock_no_connect,
.socketpair = sock_no_socketpair,
.accept = sock_no_accept,
.getname = sock_no_getname,
.poll = datagram_poll,
.ioctl = sock_no_ioctl,
.listen = sock_no_listen,
.shutdown = sock_no_shutdown,
.setsockopt = sock_no_setsockopt,
.getsockopt = sock_no_getsockopt,
.sendmsg = sock_no_sendmsg,
.recvmsg = rawsock_recvmsg,
.mmap = sock_no_mmap,
};
static void rawsock_destruct(struct sock *sk) static void rawsock_destruct(struct sock *sk)
{ {
pr_debug("sk=%p\n", sk); pr_debug("sk=%p\n", sk);
...@@ -300,10 +341,13 @@ static int rawsock_create(struct net *net, struct socket *sock, ...@@ -300,10 +341,13 @@ static int rawsock_create(struct net *net, struct socket *sock,
pr_debug("sock=%p\n", sock); pr_debug("sock=%p\n", sock);
if (sock->type != SOCK_SEQPACKET) if ((sock->type != SOCK_SEQPACKET) && (sock->type != SOCK_RAW))
return -ESOCKTNOSUPPORT; return -ESOCKTNOSUPPORT;
sock->ops = &rawsock_ops; if (sock->type == SOCK_RAW)
sock->ops = &rawsock_raw_ops;
else
sock->ops = &rawsock_ops;
sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto); sk = sk_alloc(net, PF_NFC, GFP_ATOMIC, nfc_proto->proto);
if (!sk) if (!sk)
...@@ -313,13 +357,53 @@ static int rawsock_create(struct net *net, struct socket *sock, ...@@ -313,13 +357,53 @@ static int rawsock_create(struct net *net, struct socket *sock,
sk->sk_protocol = nfc_proto->id; sk->sk_protocol = nfc_proto->id;
sk->sk_destruct = rawsock_destruct; sk->sk_destruct = rawsock_destruct;
sock->state = SS_UNCONNECTED; sock->state = SS_UNCONNECTED;
if (sock->type == SOCK_RAW)
INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work); nfc_sock_link(&raw_sk_list, sk);
nfc_rawsock(sk)->tx_work_scheduled = false; else {
INIT_WORK(&nfc_rawsock(sk)->tx_work, rawsock_tx_work);
nfc_rawsock(sk)->tx_work_scheduled = false;
}
return 0; return 0;
} }
void nfc_send_to_raw_sock(struct nfc_dev *dev, struct sk_buff *skb,
u8 payload_type, u8 direction)
{
struct sk_buff *skb_copy = NULL, *nskb;
struct sock *sk;
u8 *data;
read_lock(&raw_sk_list.lock);
sk_for_each(sk, &raw_sk_list.head) {
if (!skb_copy) {
skb_copy = __pskb_copy(skb, NFC_RAW_HEADER_SIZE,
GFP_ATOMIC);
if (!skb_copy)
continue;
data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
data[0] = dev ? dev->idx : 0xFF;
data[1] = direction & 0x01;
data[1] |= (payload_type << 1);
}
nskb = skb_clone(skb_copy, GFP_ATOMIC);
if (!nskb)
continue;
if (sock_queue_rcv_skb(sk, nskb))
kfree_skb(nskb);
}
read_unlock(&raw_sk_list.lock);
kfree_skb(skb_copy);
}
EXPORT_SYMBOL(nfc_send_to_raw_sock);
static struct proto rawsock_proto = { static struct proto rawsock_proto = {
.name = "NFC_RAW", .name = "NFC_RAW",
.owner = THIS_MODULE, .owner = THIS_MODULE,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment