Fixed MTP to work with TWRP

This commit is contained in:
awab228 2018-06-19 23:16:04 +02:00
commit f6dfaef42e
50820 changed files with 20846062 additions and 0 deletions

134
drivers/nfc/Kconfig Normal file
View file

@ -0,0 +1,134 @@
#
# Near Field Communication (NFC) devices
#
config SAMSUNG_NFC
bool "Samsung NFC driver"
default n
help
Say Y here if you want to build support for NFC (Near field
communication) devices.
To compile this support as a module, choose M here: the module will
be called nfc.
config ESE_P3_LSI
bool "Oberthur ese driver"
depends on SPI
default n
help
Oberthur Driver.
This driver provides support for LSI NXP combined ESE products.
You should select your NFC product and interface.
config ESE_SECURE
bool "ESE Secure Controller Driver"
default n
help
Common eSE Secure controller support.
This option enables device driver support for the eSE with NFC.
It is used by many services. eSE is fully controlled using SPI
to communication
config ESE_SECURE_SPI_PORT
int "ese spi port number"
depends on ESE_SECURE
default 99
config ESE_SECURE_GPIO
depends on ESE_SECURE
string "gpio group for ese"
config NFC_PN533
tristate "NXP PN533 USB driver"
depends on USB
help
NXP PN533 USB driver.
This driver provides support for NFC NXP PN533 devices.
Say Y here to compile support for PN533 devices into the
kernel or say M to compile it as module (pn533).
config NFC_WILINK
tristate "Texas Instruments NFC WiLink driver"
depends on TI_ST && NFC_NCI
help
This enables the NFC driver for Texas Instrument's BT/FM/GPS/NFC
combo devices. This makes use of shared transport line discipline
core driver to communicate with the NFC core of the combo chip.
Say Y here to compile support for Texas Instrument's NFC WiLink driver
into the kernel or say M to compile it as module.
config NFC_TRF7970A
tristate "Texas Instruments TRF7970a NFC driver"
depends on SPI && NFC_DIGITAL
help
This option enables the NFC driver for Texas Instruments' TRF7970a
device. Such device supports 5 different protocols: ISO14443A,
ISO14443B, FeLiCa, ISO15693 and ISO18000-3.
Say Y here to compile support for TRF7970a into the kernel or
say M to compile it as a module. The module will be called
trf7970a.ko.
config NFC_MEI_PHY
tristate "MEI bus NFC device support"
depends on INTEL_MEI && NFC_HCI
help
This adds support to use an mei bus nfc device. Select this if you
will use an HCI NFC driver for an NFC chip connected behind an
Intel's Management Engine chip.
If unsure, say N.
config NFC_SIM
tristate "NFC hardware simulator driver"
help
This driver declares two virtual NFC devices supporting NFC-DEP
protocol. An LLCP connection can be established between them and
all packets sent from one device is sent back to the other, acting as
loopback devices.
If unsure, say N.
config NFC_PORT100
tristate "Sony NFC Port-100 Series USB device support"
depends on USB
depends on NFC_DIGITAL
help
This adds support for Sony Port-100 chip based USB devices such as the
RC-S380 dongle.
If unsure, say N.
source "drivers/nfc/pn544/Kconfig"
source "drivers/nfc/microread/Kconfig"
source "drivers/nfc/nfcmrvl/Kconfig"
source "drivers/nfc/st21nfca/Kconfig"
source "drivers/nfc/st21nfcb/Kconfig"
config SEC_NFC
bool "Samsung NFC driver"
default n
help
SAMSUNG NFC Driver.
This driver provides support for SAMSUNG NFC products.
You should select your NFC product and interface.
Say Y here to compile support for SAMSUNG NFC driver into the
kernel.
choice
prompt "SEC NFC Products"
depends on SEC_NFC
config SEC_NFC_PRODUCT_N5
bool "N5"
endchoice
choice
prompt "SEC NFC Interface"
depends on SEC_NFC
config SEC_NFC_IF_I2C
bool "I2C"
depends on SEC_NFC_PRODUCT_N5
endchoice

19
drivers/nfc/Makefile Normal file
View file

@ -0,0 +1,19 @@
#
# Makefile for nfc devices
#
obj-$(CONFIG_NFC_PN544) += pn544/
obj-$(CONFIG_NFC_MICROREAD) += microread/
obj-$(CONFIG_NFC_PN533) += pn533.o
obj-$(CONFIG_NFC_WILINK) += nfcwilink.o
obj-$(CONFIG_NFC_MEI_PHY) += mei_phy.o
obj-$(CONFIG_NFC_SIM) += nfcsim.o
obj-$(CONFIG_NFC_PORT100) += port100.o
obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
obj-$(CONFIG_NFC_ST21NFCB) += st21nfcb/
obj-$(CONFIG_SEC_NFC) += sec_nfc.o
obj-$(CONFIG_ESE_P3_LSI) += ese_p3.o
ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG

834
drivers/nfc/ese_p3.c Executable file
View file

@ -0,0 +1,834 @@
/*
* Copyright (C) 2015 Samsung Electronics. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/regulator/consumer.h>
#include <linux/ioctl.h>
#ifdef CONFIG_OF
#include <linux/of_gpio.h>
#endif
#include <linux/ese_p3.h>
#include <linux/platform_data/spi-s3c64xx.h>
#include <linux/pm_runtime.h>
#include <linux/spi/spidev.h>
#include <linux/clk.h>
#include <linux/wakelock.h>
#include <linux/smc.h>
#define SPI_DEFAULT_SPEED 6500000L
/* size of maximum read/write buffer supported by driver */
#define MAX_BUFFER_SIZE 259U
/* Different driver debug lever */
enum P3_DEBUG_LEVEL {
P3_DEBUG_OFF,
P3_FULL_DEBUG
};
/* Variable to store current debug level request by ioctl */
static unsigned char debug_level = P3_FULL_DEBUG;
#define P3_DBG_MSG(msg...) do { \
switch (debug_level) { \
case P3_DEBUG_OFF: \
break; \
case P3_FULL_DEBUG: \
pr_info("[ESE-P3] : " msg); \
break; \
default: \
pr_err("[ESE-P3] : debug level %d", debug_level);\
break; \
}; \
} while (0);
#define P3_ERR_MSG(msg...) pr_err("[ESE-P3] : " msg);
#define P3_INFO_MSG(msg...) pr_info("[ESE-P3] : " msg);
/* Device specific macro and structure */
struct p3_data {
wait_queue_head_t read_wq; /* wait queue for read interrupt */
struct mutex buffer_mutex; /* buffer mutex */
struct spi_device *spi; /* spi device structure */
struct miscdevice p3_device; /* char device as misc driver */
bool device_opened;
#ifdef FEATURE_ESE_WAKELOCK
struct wake_lock ese_lock;
#endif
unsigned long speed;
int vdd_1p8_gpio;
struct regulator *regulator_vdd_1p8;
#ifdef CONFIG_ESE_SECURE
struct clk *ese_spi_pclk;
struct clk *ese_spi_sclk;
#endif
const char *ap_vendor;
};
#ifndef CONFIG_ESE_SECURE
static void p3_pinctrl_config(struct device *dev, bool onoff)
{
struct pinctrl *pinctrl = NULL;
P3_INFO_MSG("%s: pinctrol - %s\n", __func__, onoff ? "on": "off");
if (onoff) {
/* ON */
pinctrl = devm_pinctrl_get_select(dev, "ese_active");
if (IS_ERR_OR_NULL(pinctrl)) {
P3_ERR_MSG("%s: Failed to configure ese pin\n", __func__);
} else {
devm_pinctrl_put(pinctrl);
}
} else {
/* OFF */
pinctrl = devm_pinctrl_get_select(dev, "ese_suspend");
if (IS_ERR_OR_NULL(pinctrl)) {
P3_ERR_MSG("%s: Failed to configure ese pin\n", __func__);
} else {
devm_pinctrl_put(pinctrl);
}
}
}
#endif
#ifdef CONFIG_ESE_SECURE
static int p3_suspend(void)
{
u64 r0 = 0, r1 = 0, r2 = 0, r3 = 0;
int ret = 0;
r0 = (0x83000032);
ret = exynos_smc(r0, r1, r2, r3);
if (ret)
P3_ERR_MSG("P3 check suspend status! 0x%X\n", ret);
return 0;
}
static int p3_resume(void)
{
u64 r0 = 0, r1 = 0, r2 = 0, r3 = 0;
int ret = 0;
r0 = (0x83000033);
ret = exynos_smc(r0, r1, r2, r3);
if (ret)
P3_ERR_MSG("P3 check resume status! 0x%X\n", ret);
return 0;
}
/**
* p3_spi_clk_max_rate: finds the nearest lower rate for a clk
* @clk the clock for which to find nearest lower rate
* @rate clock frequency in Hz
* @return nearest lower rate or negative error value
*
* Public clock API extends clk_round_rate which is a ceiling function. This
* function is a floor function implemented as a binary search using the
* ceiling function.
*/
static long p3_spi_clk_max_rate(struct clk *clk, unsigned long rate)
{
long lowest_available, nearest_low, step_size, cur;
long step_direction = -1;
long guess = rate;
int max_steps = 10;
cur = clk_round_rate(clk, rate);
if (cur == rate)
return rate;
/* if we got here then: cur > rate */
lowest_available = clk_round_rate(clk, 0);
if (lowest_available > rate)
return -EINVAL;
step_size = (rate - lowest_available) >> 1;
nearest_low = lowest_available;
while (max_steps-- && step_size) {
guess += step_size * step_direction;
cur = clk_round_rate(clk, guess);
if ((cur < rate) && (cur > nearest_low))
nearest_low = cur;
/*
* if we stepped too far, then start stepping in the other
* direction with half the step size
*/
if (((cur > rate) && (step_direction > 0))
|| ((cur < rate) && (step_direction < 0))) {
step_direction = -step_direction;
step_size >>= 1;
}
}
return nearest_low;
}
static void p3_spi_clock_set(struct p3_data *data, unsigned long speed)
{
long rate;
if (!strcmp(data->ap_vendor, "qualcomm")) {
/* finds the nearest lower rate for a clk */
rate = p3_spi_clk_max_rate(data->ese_spi_sclk, speed);
if (rate < 0) {
P3_ERR_MSG("%s: no match found for requested clock frequency:%lu",
__func__, speed);
return;
}
speed = rate;
} else if (!strcmp(data->ap_vendor, "slsi")) {
/* There is half-multiplier */
speed = speed * 2;
}
clk_set_rate(data->ese_spi_sclk, speed);
}
static int p3_clk_control(struct p3_data *data, bool onoff)
{
static bool old_value;
if (old_value == onoff)
return 0;
if (onoff == true) {
p3_spi_clock_set(data, data->speed);
clk_prepare_enable(data->ese_spi_pclk);
clk_prepare_enable(data->ese_spi_sclk);
usleep_range(5000, 5100);
P3_DBG_MSG("%s clock:%lu\n", __func__, clk_get_rate(data->ese_spi_sclk));
} else {
clk_disable_unprepare(data->ese_spi_pclk);
clk_disable_unprepare(data->ese_spi_sclk);
}
old_value = onoff;
P3_INFO_MSG("clock %s\n", onoff ? "enabled" : "disabled");
return 0;
}
static int p3_clk_setup(struct device *dev, struct p3_data *data)
{
data->ese_spi_pclk = clk_get(dev, "pclk");
if (IS_ERR(data->ese_spi_pclk)) {
P3_ERR_MSG("Can't get %s\n", "pclk");
data->ese_spi_pclk = NULL;
goto err_pclk_get;
}
data->ese_spi_sclk = clk_get(dev, "sclk");
if (IS_ERR(data->ese_spi_sclk)) {
P3_ERR_MSG("Can't get %s\n", "sclk");
data->ese_spi_sclk = NULL;
goto err_sclk_get;
}
return 0;
err_sclk_get:
clk_put(data->ese_spi_pclk);
err_pclk_get:
return -EPERM;
}
#endif
static int p3_regulator_onoff(struct p3_data *data, int onoff)
{
int rc = 0;
static bool old_value;
if (old_value == onoff)
return 0;
P3_DBG_MSG("%s - onoff = %d\n", __func__, onoff);
if (onoff == 1) {
rc = regulator_enable(data->regulator_vdd_1p8);
if (rc) {
P3_ERR_MSG("%s - enable vdd_1p8 failed, rc=%d\n",
__func__, rc);
goto err_ret;
}
msleep(20);
} else {
rc = regulator_disable(data->regulator_vdd_1p8);
if (rc) {
P3_ERR_MSG("%s - disable vdd_1p8 failed, rc=%d\n",
__func__, rc);
goto err_ret;
}
}
old_value = onoff;
err_ret:
return rc;
}
static int p3_power_onoff(struct p3_data *data, int onoff)
{
int ret = 0;
if (gpio_is_valid(data->vdd_1p8_gpio))
ret = gpio_direction_output(data->vdd_1p8_gpio, onoff);
else if (data->regulator_vdd_1p8)
ret = p3_regulator_onoff(data, onoff);
return ret;
}
#ifndef CONFIG_ESE_SECURE
static int p3_xfer(struct p3_data *p3_device, struct p3_ioctl_transfer *tr)
{
int status = 0;
struct spi_message m;
struct spi_transfer t;
unsigned char tx_buffer[MAX_BUFFER_SIZE] = {0x0, };
unsigned char rx_buffer[MAX_BUFFER_SIZE] = {0x0, };
P3_DBG_MSG("%s\n", __func__);
if (p3_device == NULL || tr == NULL)
return -EFAULT;
if (tr->len > DEFAULT_BUFFER_SIZE || !tr->len)
return -EMSGSIZE;
if (copy_from_user(tx_buffer,
tr->tx_buffer, tr->len) != 0)
return -EFAULT;
spi_message_init(&m);
memset(&t, 0, sizeof(t));
t.tx_buf = tx_buffer;
t.rx_buf = rx_buffer;
t.len = tr->len;
spi_message_add_tail(&t, &m);
status = spi_sync(p3_device->spi, &m);
if (copy_to_user(tr->rx_buffer, rx_buffer, tr->len)) {
P3_ERR_MSG("%s : failed to copy to user space\n", __func__);
return -EFAULT;
}
P3_DBG_MSG("%s p3_xfer,length=%d\n", __func__, tr->len);
return status;
}
static int p3_rw_spi_message(struct p3_data *p3_device, unsigned long arg)
{
struct p3_ioctl_transfer dup;
int err = 0;
if (copy_from_user(&dup, (void *)arg,
sizeof(struct p3_ioctl_transfer)) != 0) {
P3_ERR_MSG("%s copy_from_user fail!\n", __func__);
return -EFAULT;
} else {
err = p3_xfer(p3_device, &dup);
if (err != 0) {
P3_ERR_MSG("%s xfer failed!\n", __func__);
return err;
}
}
return 0;
}
#ifdef CONFIG_COMPAT
static int p3_rw_spi_message_32(struct p3_data *p3_device, unsigned long arg)
{
struct p3_ioctl_transfer dup;
struct spip3_ioc_transfer_32 p3transfr_32;
int err = 0;
if (__copy_from_user(&p3transfr_32, (void __user *)arg,
sizeof(struct spip3_ioc_transfer_32))) {
P3_ERR_MSG("%s, failed to copy from user\n", __func__);
return -EFAULT;
}
dup.tx_buffer = (unsigned char *)(unsigned long)(p3transfr_32.tx_buffer);
dup.rx_buffer = (unsigned char *)(unsigned long)(p3transfr_32.rx_buffer);
dup.len = p3transfr_32.len;
err = p3_xfer(p3_device, &dup);
if (err != 0) {
P3_ERR_MSG("%s xfer failed!\n", __func__);
return err;
}
P3_DBG_MSG("%s len:%u\n", __func__, dup.len);
return 0;
}
#endif
#endif
static int spip3_open(struct inode *inode, struct file *filp)
{
struct p3_data *p3_dev = container_of(filp->private_data,
struct p3_data, p3_device);
int ret = 0;
/* for defence MULTI-OPEN */
if (p3_dev->device_opened) {
P3_ERR_MSG("%s - ALREADY opened!\n", __func__);
return -EBUSY;
}
p3_dev->device_opened = true;
P3_INFO_MSG("open\n");
#ifdef FEATURE_ESE_WAKELOCK
wake_lock(&p3_dev->ese_lock);
#endif
#ifndef CONFIG_ESE_SECURE
p3_pinctrl_config(p3_dev->p3_device.parent, true);
#endif
ret = p3_power_onoff(p3_dev, 1);
if (ret < 0)
P3_ERR_MSG(" test: failed to turn on LDO()\n");
usleep_range(5000, 5500);
#ifdef CONFIG_ESE_SECURE
p3_clk_control(p3_dev, true);
p3_resume();
#endif
filp->private_data = p3_dev;
return 0;
}
static int spip3_release(struct inode *inode, struct file *filp)
{
struct p3_data *p3_dev = filp->private_data;
int ret = 0;
P3_INFO_MSG("release\n");
ret = p3_power_onoff(p3_dev, 0);
if (ret < 0)
P3_ERR_MSG(" test: failed to turn off LDO()\n");
#ifdef CONFIG_ESE_SECURE
p3_clk_control(p3_dev, false);
p3_suspend();
#endif
#ifdef FEATURE_ESE_WAKELOCK
if (wake_lock_active(&p3_dev->ese_lock)) {
P3_DBG_MSG("%s: wake unlock at release!!\n", __func__);
wake_unlock(&p3_dev->ese_lock);
}
#endif
#ifndef CONFIG_ESE_SECURE
p3_pinctrl_config(p3_dev->p3_device.parent, false);
usleep_range(15000, 15500);
#endif
p3_dev->device_opened = false;
P3_DBG_MSG("%s : Major No: %d, Minor No: %d\n", __func__,
imajor(inode), iminor(inode));
return 0;
}
static long spip3_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
int ret = 0;
struct p3_data *data = NULL;
if (_IOC_TYPE(cmd) != P3_MAGIC) {
P3_ERR_MSG("%s invalid magic. cmd=0x%X Received=0x%X \
Expected=0x%X\n", __func__, cmd, _IOC_TYPE(cmd), P3_MAGIC);
return -ENOTTY;
}
data = filp->private_data;
mutex_lock(&data->buffer_mutex);
switch (cmd) {
case P3_SET_DBG:
debug_level = (unsigned char)arg;
P3_DBG_MSG(KERN_INFO"[NXP-P3] - Debug level %d", debug_level);
break;
case P3_ENABLE_SPI_CLK:
P3_DBG_MSG("%s P3_ENABLE_SPI_CLK\n", __func__);
#ifdef CONFIG_ESE_SECURE
ret = p3_clk_control(data, true);
#endif
break;
case P3_DISABLE_SPI_CLK:
P3_DBG_MSG("%s P3_DISABLE_SPI_CLK\n", __func__);
#ifdef CONFIG_ESE_SECURE
ret = p3_clk_control(data, false);
#endif
break;
/*non TZ */
#ifndef CONFIG_ESE_SECURE
case P3_RW_SPI_DATA:
ret = p3_rw_spi_message(data, arg);
if (ret < 0)
P3_ERR_MSG("%s P3_RW_SPI_DATA failed [%d].\n",
__func__, ret);
break;
#ifdef CONFIG_COMPAT
case P3_RW_SPI_DATA_32:
ret = p3_rw_spi_message_32(data, arg);
if (ret < 0)
P3_ERR_MSG("%s P3_RW_SPI_DATA_32 failed [%d].\n",
__func__, ret);
break;
#endif
#endif
case P3_SET_PWR:
case P3_SET_POLL:
case P3_SET_SPI_CONFIG:
case P3_ENABLE_SPI_CS:
case P3_DISABLE_SPI_CS:
case P3_ENABLE_CLK_CS:
case P3_DISABLE_CLK_CS:
case P3_SWING_CS:
P3_ERR_MSG("%s deprecated IOCTL:0x%X\n", __func__, cmd);
break;
default:
P3_DBG_MSG("%s no matching ioctl! 0x%X\n", __func__, cmd);
ret = -EINVAL;
}
mutex_unlock(&data->buffer_mutex);
return ret;
}
#ifndef CONFIG_ESE_SECURE
static ssize_t spip3_write(struct file *filp, const char *buf, size_t count,
loff_t *offset)
{
int ret = -1;
struct p3_data *p3_dev;
unsigned char tx_buffer[MAX_BUFFER_SIZE] = {0x0, };
//P3_DBG_MSG("spip3_write -Enter count %zu\n", count);
p3_dev = filp->private_data;
mutex_lock(&p3_dev->buffer_mutex);
if (count > MAX_BUFFER_SIZE)
count = MAX_BUFFER_SIZE;
if (copy_from_user(&tx_buffer[0], &buf[0], count)) {
P3_ERR_MSG("%s : failed to copy from user space\n", __func__);
mutex_unlock(&p3_dev->buffer_mutex);
return -EFAULT;
}
/* Write data */
ret = spi_write(p3_dev->spi, &tx_buffer[0], count);
if (ret < 0)
ret = -EIO;
else
ret = count;
mutex_unlock(&p3_dev->buffer_mutex);
//P3_DBG_MSG(KERN_ALERT "spip3_write ret %d- Exit\n", ret);
return ret;
}
static ssize_t spip3_read(struct file *filp, char *buf, size_t count,
loff_t *offset)
{
int ret = -EIO;
struct p3_data *p3_dev = filp->private_data;
unsigned char rx_buffer[MAX_BUFFER_SIZE] = {0x0, };
//P3_DBG_MSG("spip3_read count %zu - Enter\n", count);
mutex_lock(&p3_dev->buffer_mutex);
/* Read the availabe data along with one byte LRC */
ret = spi_read(p3_dev->spi, (void *)rx_buffer, count);
if (ret < 0) {
P3_ERR_MSG("spi_read failed\n");
ret = -EIO;
goto fail;
}
if (copy_to_user(buf, &rx_buffer[0], count)) {
P3_ERR_MSG("%s : failed to copy to user space\n", __func__);
ret = -EFAULT;
goto fail;
}
ret = count;
fail:
//P3_DBG_MSG("%s ret %d Exit\n", __func__, ret);
mutex_unlock(&p3_dev->buffer_mutex);
return ret;
}
#endif
/* possible fops on the p3 device */
static const struct file_operations spip3_dev_fops = {
.owner = THIS_MODULE,
#ifndef CONFIG_ESE_SECURE
.read = spip3_read,
.write = spip3_write,
#endif
.open = spip3_open,
.release = spip3_release,
.unlocked_ioctl = spip3_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = spip3_ioctl,
#endif
};
static int p3_parse_dt(struct device *dev, struct p3_data *data)
{
struct device_node *np = dev->of_node;
const char *vdd_1p8_str;
int ret = 0;
data->vdd_1p8_gpio = of_get_named_gpio(np, "p3-vdd_1p8-gpio", 0);
if (gpio_is_valid(data->vdd_1p8_gpio)) {
P3_INFO_MSG("%s: p3-vdd-1p8-gpio - %d\n", __func__, data->vdd_1p8_gpio);
ret = gpio_request(data->vdd_1p8_gpio, "ese_vdd_1p8_gpio");
if (ret) {
P3_ERR_MSG("%s - failed to request ese_vdd_1p8_gpio\n", __func__);
}
}
if (!of_property_read_string(np, "p3-vdd_1p8",
&vdd_1p8_str)) {
P3_INFO_MSG("%s: regulator name - %s\n", __func__, vdd_1p8_str);
data->regulator_vdd_1p8 = regulator_get(NULL, vdd_1p8_str);
if (IS_ERR(data->regulator_vdd_1p8)) {
P3_ERR_MSG("%s - %s regulator_get fail\n", __func__, vdd_1p8_str);
}
}
if (!of_property_read_string(np, "p3-ap_vendor",
&data->ap_vendor)) {
P3_INFO_MSG("%s: ap_vendor - %s\n", __func__, data->ap_vendor);
}
return ret;
}
static int spip3_probe(struct spi_device *spi)
{
int ret = -1;
struct p3_data *data = NULL;
P3_INFO_MSG("%s chip select : %d , bus number = %d\n",
__func__, spi->chip_select, spi->master->bus_num);
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (data == NULL) {
P3_ERR_MSG("failed to allocate memory for module data\n");
ret = -ENOMEM;
goto err_exit;
}
ret = p3_parse_dt(&spi->dev, data);
if (ret) {
P3_ERR_MSG("%s - Failed to parse DT\n", __func__);
goto p3_parse_dt_failed;
}
#ifdef CONFIG_ESE_SECURE
ret = p3_clk_setup(&spi->dev, data);
if (ret) {
P3_ERR_MSG("%s - Failed to do clk_setup\n", __func__);
goto p3_clk_setup_failed;
}
#endif
#ifndef CONFIG_ESE_SECURE
spi->bits_per_word = 8;
spi->mode = SPI_MODE_0;
spi->max_speed_hz = SPI_DEFAULT_SPEED;
ret = spi_setup(spi);
if (ret < 0) {
P3_ERR_MSG("failed to do spi_setup()\n");
goto p3_spi_setup_failed;
}
#endif
data->speed = SPI_DEFAULT_SPEED;
data->spi = spi;
data->p3_device.minor = MISC_DYNAMIC_MINOR;
data->p3_device.name = "p3";
data->p3_device.fops = &spip3_dev_fops;
data->p3_device.parent = &spi->dev;
#ifndef CONFIG_ESE_SECURE
p3_pinctrl_config(&spi->dev, false);
#endif
dev_set_drvdata(&spi->dev, data);
/* init mutex and queues */
init_waitqueue_head(&data->read_wq);
mutex_init(&data->buffer_mutex);
#ifdef FEATURE_ESE_WAKELOCK
wake_lock_init(&data->ese_lock,
WAKE_LOCK_SUSPEND, "ese_wake_lock");
#endif
data->device_opened = false;
ret = misc_register(&data->p3_device);
if (ret < 0) {
P3_ERR_MSG("misc_register failed! %d\n", ret);
goto err_misc_regi;
}
P3_INFO_MSG("%s finished...\n", __func__);
return ret;
err_misc_regi:
#ifdef FEATURE_ESE_WAKELOCK
wake_lock_destroy(&data->ese_lock);
#endif
mutex_destroy(&data->buffer_mutex);
#ifndef CONFIG_ESE_SECURE
p3_spi_setup_failed:
#endif
#ifdef CONFIG_ESE_SECURE
if (data->ese_spi_pclk)
clk_put(data->ese_spi_pclk);
if (data->ese_spi_sclk)
clk_put(data->ese_spi_sclk);
p3_clk_setup_failed:
#endif
if (gpio_is_valid(data->vdd_1p8_gpio))
gpio_free(data->vdd_1p8_gpio);
if (data->regulator_vdd_1p8)
regulator_put(data->regulator_vdd_1p8);
p3_parse_dt_failed:
kfree(data);
err_exit:
P3_DBG_MSG("ERROR: Exit : %s ret %d\n", __func__, ret);
return ret;
}
static int spip3_remove(struct spi_device *spi)
{
struct p3_data *p3_dev = dev_get_drvdata(&spi->dev);
P3_DBG_MSG("Entry : %s\n", __func__);
if (p3_dev == NULL) {
P3_ERR_MSG("%s p3_dev is null!\n", __func__);
return 0;
}
#ifdef FEATURE_ESE_WAKELOCK
wake_lock_destroy(&p3_dev->ese_lock);
#endif
mutex_destroy(&p3_dev->buffer_mutex);
misc_deregister(&p3_dev->p3_device);
#ifdef CONFIG_ESE_SECURE
if (p3_dev->ese_spi_pclk)
clk_put(p3_dev->ese_spi_pclk);
if (p3_dev->ese_spi_sclk)
clk_put(p3_dev->ese_spi_sclk);
#endif
kfree(p3_dev);
P3_DBG_MSG("Exit : %s\n", __func__);
return 0;
}
#ifdef CONFIG_OF
static struct of_device_id p3_match_table[] = {
{ .compatible = "ese_p3",},
{},
};
#else
#define ese_match_table NULL
#endif
static struct spi_driver spip3_driver = {
.driver = {
.name = "p3",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = p3_match_table,
#endif
},
.probe = spip3_probe,
.remove = spip3_remove,
};
static int __init spip3_dev_init(void)
{
P3_INFO_MSG("Entry : %s\n", __func__);
return spi_register_driver(&spip3_driver);
}
static void __exit spip3_dev_exit(void)
{
P3_INFO_MSG("Entry : %s\n", __func__);
spi_unregister_driver(&spip3_driver);
}
module_init(spip3_dev_init);
module_exit(spip3_dev_exit);
MODULE_AUTHOR("Sec");
MODULE_DESCRIPTION("ese SPI driver");
MODULE_LICENSE("GPL");

173
drivers/nfc/mei_phy.c Normal file
View file

@ -0,0 +1,173 @@
/*
* MEI Library for mei bus nfc device access
*
* Copyright (C) 2013 Intel Corporation. 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/module.h>
#include <linux/slab.h>
#include <linux/nfc.h>
#include "mei_phy.h"
struct mei_nfc_hdr {
u8 cmd;
u8 status;
u16 req_id;
u32 reserved;
u16 data_size;
} __packed;
#define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
#define MEI_DUMP_SKB_IN(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump_debug("mei in : ", DUMP_PREFIX_OFFSET, \
16, 1, (skb)->data, (skb)->len, false); \
} while (0)
#define MEI_DUMP_SKB_OUT(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump_debug("mei out: ", DUMP_PREFIX_OFFSET, \
16, 1, (skb)->data, (skb)->len, false); \
} while (0)
int nfc_mei_phy_enable(void *phy_id)
{
int r;
struct nfc_mei_phy *phy = phy_id;
pr_info("%s\n", __func__);
if (phy->powered == 1)
return 0;
r = mei_cl_enable_device(phy->device);
if (r < 0) {
pr_err("Could not enable device\n");
return r;
}
r = mei_cl_register_event_cb(phy->device, nfc_mei_event_cb, phy);
if (r) {
pr_err("Event cb registration failed\n");
mei_cl_disable_device(phy->device);
phy->powered = 0;
return r;
}
phy->powered = 1;
return 0;
}
EXPORT_SYMBOL_GPL(nfc_mei_phy_enable);
void nfc_mei_phy_disable(void *phy_id)
{
struct nfc_mei_phy *phy = phy_id;
pr_info("%s\n", __func__);
mei_cl_disable_device(phy->device);
phy->powered = 0;
}
EXPORT_SYMBOL_GPL(nfc_mei_phy_disable);
/*
* 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 nfc_mei_phy_write(void *phy_id, struct sk_buff *skb)
{
struct nfc_mei_phy *phy = phy_id;
int r;
MEI_DUMP_SKB_OUT("mei frame sent", skb);
r = mei_cl_send(phy->device, skb->data, skb->len);
if (r > 0)
r = 0;
return r;
}
void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context)
{
struct nfc_mei_phy *phy = context;
if (phy->hard_fault != 0)
return;
if (events & BIT(MEI_CL_EVENT_RX)) {
struct sk_buff *skb;
int reply_size;
skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
if (!skb)
return;
reply_size = mei_cl_recv(device, skb->data, MEI_NFC_MAX_READ);
if (reply_size < MEI_NFC_HEADER_SIZE) {
kfree_skb(skb);
return;
}
skb_put(skb, reply_size);
skb_pull(skb, MEI_NFC_HEADER_SIZE);
MEI_DUMP_SKB_IN("mei frame read", skb);
nfc_hci_recv_frame(phy->hdev, skb);
}
}
EXPORT_SYMBOL_GPL(nfc_mei_event_cb);
struct nfc_phy_ops mei_phy_ops = {
.write = nfc_mei_phy_write,
.enable = nfc_mei_phy_enable,
.disable = nfc_mei_phy_disable,
};
EXPORT_SYMBOL_GPL(mei_phy_ops);
struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device)
{
struct nfc_mei_phy *phy;
phy = kzalloc(sizeof(struct nfc_mei_phy), GFP_KERNEL);
if (!phy)
return NULL;
phy->device = device;
mei_cl_set_drvdata(device, phy);
return phy;
}
EXPORT_SYMBOL_GPL(nfc_mei_phy_alloc);
void nfc_mei_phy_free(struct nfc_mei_phy *phy)
{
kfree(phy);
}
EXPORT_SYMBOL_GPL(nfc_mei_phy_free);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("mei bus NFC device interface");

30
drivers/nfc/mei_phy.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef __LOCAL_MEI_PHY_H_
#define __LOCAL_MEI_PHY_H_
#include <linux/mei_cl_bus.h>
#include <net/nfc/hci.h>
#define MEI_NFC_HEADER_SIZE 10
#define MEI_NFC_MAX_HCI_PAYLOAD 300
struct nfc_mei_phy {
struct mei_cl_device *device;
struct nfc_hci_dev *hdev;
int powered;
int hard_fault; /*
* < 0 if hardware error occured
* and prevents normal operation.
*/
};
extern struct nfc_phy_ops mei_phy_ops;
int nfc_mei_phy_enable(void *phy_id);
void nfc_mei_phy_disable(void *phy_id);
void nfc_mei_event_cb(struct mei_cl_device *device, u32 events, void *context);
struct nfc_mei_phy *nfc_mei_phy_alloc(struct mei_cl_device *device);
void nfc_mei_phy_free(struct nfc_mei_phy *phy);
#endif /* __LOCAL_MEI_PHY_H_ */

View file

@ -0,0 +1,35 @@
config NFC_MICROREAD
tristate "Inside Secure microread NFC driver"
depends on NFC_HCI
select CRC_CCITT
default n
---help---
This module contains the main code for Inside Secure microread
NFC chipsets. 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 microread.
Say N if unsure.
config NFC_MICROREAD_I2C
tristate "NFC Microread i2c support"
depends on NFC_MICROREAD && I2C && NFC_SHDLC
---help---
This module adds support for the i2c interface of adapters using
Inside microread chipsets. Select this if your platform is using
the i2c bus.
If you choose to build a module, it'll be called microread_i2c.
Say N if unsure.
config NFC_MICROREAD_MEI
tristate "NFC Microread MEI support"
depends on NFC_MICROREAD && NFC_MEI_PHY
---help---
This module adds support for the mei interface of adapters using
Inside microread chipsets. Select this if your microread chipset
is handled by Intel's Management Engine Interface on your platform.
If you choose to build a module, it'll be called microread_mei.
Say N if unsure.

View file

@ -0,0 +1,10 @@
#
# Makefile for Microread HCI based NFC driver
#
microread_i2c-objs = i2c.o
microread_mei-objs = mei.o
obj-$(CONFIG_NFC_MICROREAD) += microread.o
obj-$(CONFIG_NFC_MICROREAD_I2C) += microread_i2c.o
obj-$(CONFIG_NFC_MICROREAD_MEI) += microread_mei.o

328
drivers/nfc/microread/i2c.c Normal file
View file

@ -0,0 +1,328 @@
/*
* HCI based Driver for Inside Secure microread NFC Chip - i2c layer
*
* Copyright (C) 2013 Intel Corporation. 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/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "microread.h"
#define MICROREAD_I2C_DRIVER_NAME "microread"
#define MICROREAD_I2C_FRAME_HEADROOM 1
#define MICROREAD_I2C_FRAME_TAILROOM 1
/* framing in HCI mode */
#define MICROREAD_I2C_LLC_LEN 1
#define MICROREAD_I2C_LLC_CRC 1
#define MICROREAD_I2C_LLC_LEN_CRC (MICROREAD_I2C_LLC_LEN + \
MICROREAD_I2C_LLC_CRC)
#define MICROREAD_I2C_LLC_MIN_SIZE (1 + MICROREAD_I2C_LLC_LEN_CRC)
#define MICROREAD_I2C_LLC_MAX_PAYLOAD 29
#define MICROREAD_I2C_LLC_MAX_SIZE (MICROREAD_I2C_LLC_LEN_CRC + 1 + \
MICROREAD_I2C_LLC_MAX_PAYLOAD)
struct microread_i2c_phy {
struct i2c_client *i2c_dev;
struct nfc_hci_dev *hdev;
int irq;
int hard_fault; /*
* < 0 if hardware error occured (e.g. i2c err)
* and prevents normal operation.
*/
};
#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)
static void microread_i2c_add_len_crc(struct sk_buff *skb)
{
int i;
u8 crc = 0;
int len;
len = skb->len;
*skb_push(skb, 1) = len;
for (i = 0; i < skb->len; i++)
crc = crc ^ skb->data[i];
*skb_put(skb, 1) = crc;
}
static void microread_i2c_remove_len_crc(struct sk_buff *skb)
{
skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
}
static int check_crc(struct sk_buff *skb)
{
int i;
u8 crc = 0;
for (i = 0; i < skb->len - 1; i++)
crc = crc ^ skb->data[i];
if (crc != skb->data[skb->len-1]) {
pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
pr_info("%s: BAD CRC\n", __func__);
return -EPERM;
}
return 0;
}
static int microread_i2c_enable(void *phy_id)
{
return 0;
}
static void microread_i2c_disable(void *phy_id)
{
return;
}
static int microread_i2c_write(void *phy_id, struct sk_buff *skb)
{
int r;
struct microread_i2c_phy *phy = phy_id;
struct i2c_client *client = phy->i2c_dev;
if (phy->hard_fault != 0)
return phy->hard_fault;
usleep_range(3000, 6000);
microread_i2c_add_len_crc(skb);
I2C_DUMP_SKB("i2c frame written", skb);
r = i2c_master_send(client, skb->data, skb->len);
if (r == -EREMOTEIO) { /* Retry, chip was in standby */
usleep_range(6000, 10000);
r = i2c_master_send(client, skb->data, skb->len);
}
if (r >= 0) {
if (r != skb->len)
r = -EREMOTEIO;
else
r = 0;
}
microread_i2c_remove_len_crc(skb);
return r;
}
static int microread_i2c_read(struct microread_i2c_phy *phy,
struct sk_buff **skb)
{
int r;
u8 len;
u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
struct i2c_client *client = phy->i2c_dev;
r = i2c_master_recv(client, &len, 1);
if (r != 1) {
nfc_err(&client->dev, "cannot read len byte\n");
return -EREMOTEIO;
}
if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
(len > MICROREAD_I2C_LLC_MAX_SIZE)) {
nfc_err(&client->dev, "invalid len byte\n");
r = -EBADMSG;
goto flush;
}
*skb = alloc_skb(1 + len, GFP_KERNEL);
if (*skb == NULL) {
r = -ENOMEM;
goto flush;
}
*skb_put(*skb, 1) = len;
r = i2c_master_recv(client, skb_put(*skb, len), len);
if (r != len) {
kfree_skb(*skb);
return -EREMOTEIO;
}
I2C_DUMP_SKB("cc frame read", *skb);
r = check_crc(*skb);
if (r != 0) {
kfree_skb(*skb);
r = -EBADMSG;
goto flush;
}
skb_pull(*skb, 1);
skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
usleep_range(3000, 6000);
return 0;
flush:
if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
r = -EREMOTEIO;
usleep_range(3000, 6000);
return r;
}
static irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
{
struct microread_i2c_phy *phy = phy_id;
struct i2c_client *client;
struct sk_buff *skb = NULL;
int r;
if (!phy || irq != phy->i2c_dev->irq) {
WARN_ON_ONCE(1);
return IRQ_NONE;
}
client = phy->i2c_dev;
if (phy->hard_fault != 0)
return IRQ_HANDLED;
r = microread_i2c_read(phy, &skb);
if (r == -EREMOTEIO) {
phy->hard_fault = r;
nfc_hci_recv_frame(phy->hdev, NULL);
return IRQ_HANDLED;
} else if ((r == -ENOMEM) || (r == -EBADMSG)) {
return IRQ_HANDLED;
}
nfc_hci_recv_frame(phy->hdev, skb);
return IRQ_HANDLED;
}
static struct nfc_phy_ops i2c_phy_ops = {
.write = microread_i2c_write,
.enable = microread_i2c_enable,
.disable = microread_i2c_disable,
};
static int microread_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct microread_i2c_phy *phy;
struct microread_nfc_platform_data *pdata =
dev_get_platdata(&client->dev);
int r;
dev_dbg(&client->dev, "client %p\n", client);
if (!pdata) {
nfc_err(&client->dev, "client %p: missing platform data\n",
client);
return -EINVAL;
}
phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
GFP_KERNEL);
if (!phy)
return -ENOMEM;
i2c_set_clientdata(client, phy);
phy->i2c_dev = client;
r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
IRQF_TRIGGER_RISING | IRQF_ONESHOT,
MICROREAD_I2C_DRIVER_NAME, phy);
if (r) {
nfc_err(&client->dev, "Unable to register IRQ handler\n");
return r;
}
r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
MICROREAD_I2C_FRAME_HEADROOM,
MICROREAD_I2C_FRAME_TAILROOM,
MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
if (r < 0)
goto err_irq;
nfc_info(&client->dev, "Probed");
return 0;
err_irq:
free_irq(client->irq, phy);
return r;
}
static int microread_i2c_remove(struct i2c_client *client)
{
struct microread_i2c_phy *phy = i2c_get_clientdata(client);
microread_remove(phy->hdev);
free_irq(client->irq, phy);
return 0;
}
static struct i2c_device_id microread_i2c_id[] = {
{ MICROREAD_I2C_DRIVER_NAME, 0},
{ }
};
MODULE_DEVICE_TABLE(i2c, microread_i2c_id);
static struct i2c_driver microread_i2c_driver = {
.driver = {
.name = MICROREAD_I2C_DRIVER_NAME,
},
.probe = microread_i2c_probe,
.remove = microread_i2c_remove,
.id_table = microread_i2c_id,
};
module_i2c_driver(microread_i2c_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

109
drivers/nfc/microread/mei.c Normal file
View file

@ -0,0 +1,109 @@
/*
* HCI based Driver for Inside Secure microread NFC Chip
*
* Copyright (C) 2013 Intel Corporation. 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/module.h>
#include <linux/mod_devicetable.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "../mei_phy.h"
#include "microread.h"
#define MICROREAD_DRIVER_NAME "microread"
static int microread_mei_probe(struct mei_cl_device *device,
const struct mei_cl_device_id *id)
{
struct nfc_mei_phy *phy;
int r;
pr_info("Probing NFC microread\n");
phy = nfc_mei_phy_alloc(device);
if (!phy) {
pr_err("Cannot allocate memory for microread mei phy.\n");
return -ENOMEM;
}
r = microread_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
&phy->hdev);
if (r < 0) {
nfc_mei_phy_free(phy);
return r;
}
return 0;
}
static int microread_mei_remove(struct mei_cl_device *device)
{
struct nfc_mei_phy *phy = mei_cl_get_drvdata(device);
microread_remove(phy->hdev);
nfc_mei_phy_free(phy);
return 0;
}
static struct mei_cl_device_id microread_mei_tbl[] = {
{ MICROREAD_DRIVER_NAME },
/* required last entry */
{ }
};
MODULE_DEVICE_TABLE(mei, microread_mei_tbl);
static struct mei_cl_driver microread_driver = {
.id_table = microread_mei_tbl,
.name = MICROREAD_DRIVER_NAME,
.probe = microread_mei_probe,
.remove = microread_mei_remove,
};
static int microread_mei_init(void)
{
int r;
pr_debug(DRIVER_DESC ": %s\n", __func__);
r = mei_cl_driver_register(&microread_driver);
if (r) {
pr_err(MICROREAD_DRIVER_NAME ": driver registration failed\n");
return r;
}
return 0;
}
static void microread_mei_exit(void)
{
mei_cl_driver_unregister(&microread_driver);
}
module_init(microread_mei_init);
module_exit(microread_mei_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

View file

@ -0,0 +1,733 @@
/*
* HCI based Driver for Inside Secure microread NFC Chip
*
* Copyright (C) 2013 Intel Corporation. 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/module.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/crc-ccitt.h>
#include <linux/nfc.h>
#include <net/nfc/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "microread.h"
/* Proprietary gates, events, commands and registers */
/* Admin */
#define MICROREAD_GATE_ID_ADM NFC_HCI_ADMIN_GATE
#define MICROREAD_GATE_ID_MGT 0x01
#define MICROREAD_GATE_ID_OS 0x02
#define MICROREAD_GATE_ID_TESTRF 0x03
#define MICROREAD_GATE_ID_LOOPBACK NFC_HCI_LOOPBACK_GATE
#define MICROREAD_GATE_ID_IDT NFC_HCI_ID_MGMT_GATE
#define MICROREAD_GATE_ID_LMS NFC_HCI_LINK_MGMT_GATE
/* Reader */
#define MICROREAD_GATE_ID_MREAD_GEN 0x10
#define MICROREAD_GATE_ID_MREAD_ISO_B NFC_HCI_RF_READER_B_GATE
#define MICROREAD_GATE_ID_MREAD_NFC_T1 0x12
#define MICROREAD_GATE_ID_MREAD_ISO_A NFC_HCI_RF_READER_A_GATE
#define MICROREAD_GATE_ID_MREAD_NFC_T3 0x14
#define MICROREAD_GATE_ID_MREAD_ISO_15_3 0x15
#define MICROREAD_GATE_ID_MREAD_ISO_15_2 0x16
#define MICROREAD_GATE_ID_MREAD_ISO_B_3 0x17
#define MICROREAD_GATE_ID_MREAD_BPRIME 0x18
#define MICROREAD_GATE_ID_MREAD_ISO_A_3 0x19
/* Card */
#define MICROREAD_GATE_ID_MCARD_GEN 0x20
#define MICROREAD_GATE_ID_MCARD_ISO_B 0x21
#define MICROREAD_GATE_ID_MCARD_BPRIME 0x22
#define MICROREAD_GATE_ID_MCARD_ISO_A 0x23
#define MICROREAD_GATE_ID_MCARD_NFC_T3 0x24
#define MICROREAD_GATE_ID_MCARD_ISO_15_3 0x25
#define MICROREAD_GATE_ID_MCARD_ISO_15_2 0x26
#define MICROREAD_GATE_ID_MCARD_ISO_B_2 0x27
#define MICROREAD_GATE_ID_MCARD_ISO_CUSTOM 0x28
#define MICROREAD_GATE_ID_SECURE_ELEMENT 0x2F
/* P2P */
#define MICROREAD_GATE_ID_P2P_GEN 0x30
#define MICROREAD_GATE_ID_P2P_TARGET 0x31
#define MICROREAD_PAR_P2P_TARGET_MODE 0x01
#define MICROREAD_PAR_P2P_TARGET_GT 0x04
#define MICROREAD_GATE_ID_P2P_INITIATOR 0x32
#define MICROREAD_PAR_P2P_INITIATOR_GI 0x01
#define MICROREAD_PAR_P2P_INITIATOR_GT 0x03
/* Those pipes are created/opened by default in the chip */
#define MICROREAD_PIPE_ID_LMS 0x00
#define MICROREAD_PIPE_ID_ADMIN 0x01
#define MICROREAD_PIPE_ID_MGT 0x02
#define MICROREAD_PIPE_ID_OS 0x03
#define MICROREAD_PIPE_ID_HDS_LOOPBACK 0x04
#define MICROREAD_PIPE_ID_HDS_IDT 0x05
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B 0x08
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_BPRIME 0x09
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_A 0x0A
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_3 0x0B
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_2 0x0C
#define MICROREAD_PIPE_ID_HDS_MCARD_NFC_T3 0x0D
#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B_2 0x0E
#define MICROREAD_PIPE_ID_HDS_MCARD_CUSTOM 0x0F
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B 0x10
#define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1 0x11
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A 0x12
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_3 0x13
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_2 0x14
#define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3 0x15
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B_3 0x16
#define MICROREAD_PIPE_ID_HDS_MREAD_BPRIME 0x17
#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3 0x18
#define MICROREAD_PIPE_ID_HDS_MREAD_GEN 0x1B
#define MICROREAD_PIPE_ID_HDS_STACKED_ELEMENT 0x1C
#define MICROREAD_PIPE_ID_HDS_INSTANCES 0x1D
#define MICROREAD_PIPE_ID_HDS_TESTRF 0x1E
#define MICROREAD_PIPE_ID_HDS_P2P_TARGET 0x1F
#define MICROREAD_PIPE_ID_HDS_P2P_INITIATOR 0x20
/* Events */
#define MICROREAD_EVT_MREAD_DISCOVERY_OCCURED NFC_HCI_EVT_TARGET_DISCOVERED
#define MICROREAD_EVT_MREAD_CARD_FOUND 0x3D
#define MICROREAD_EMCF_A_ATQA 0
#define MICROREAD_EMCF_A_SAK 2
#define MICROREAD_EMCF_A_LEN 3
#define MICROREAD_EMCF_A_UID 4
#define MICROREAD_EMCF_A3_ATQA 0
#define MICROREAD_EMCF_A3_SAK 2
#define MICROREAD_EMCF_A3_LEN 3
#define MICROREAD_EMCF_A3_UID 4
#define MICROREAD_EMCF_B_UID 0
#define MICROREAD_EMCF_T1_ATQA 0
#define MICROREAD_EMCF_T1_UID 4
#define MICROREAD_EMCF_T3_UID 0
#define MICROREAD_EVT_MREAD_DISCOVERY_START NFC_HCI_EVT_READER_REQUESTED
#define MICROREAD_EVT_MREAD_DISCOVERY_START_SOME 0x3E
#define MICROREAD_EVT_MREAD_DISCOVERY_STOP NFC_HCI_EVT_END_OPERATION
#define MICROREAD_EVT_MREAD_SIM_REQUESTS 0x3F
#define MICROREAD_EVT_MCARD_EXCHANGE NFC_HCI_EVT_TARGET_DISCOVERED
#define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF 0x20
#define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF 0x21
#define MICROREAD_EVT_MCARD_FIELD_ON 0x11
#define MICROREAD_EVT_P2P_TARGET_ACTIVATED 0x13
#define MICROREAD_EVT_P2P_TARGET_DEACTIVATED 0x12
#define MICROREAD_EVT_MCARD_FIELD_OFF 0x14
/* Commands */
#define MICROREAD_CMD_MREAD_EXCHANGE 0x10
#define MICROREAD_CMD_MREAD_SUBSCRIBE 0x3F
/* Hosts IDs */
#define MICROREAD_ELT_ID_HDS NFC_HCI_TERMINAL_HOST_ID
#define MICROREAD_ELT_ID_SIM NFC_HCI_UICC_HOST_ID
#define MICROREAD_ELT_ID_SE1 0x03
#define MICROREAD_ELT_ID_SE2 0x04
#define MICROREAD_ELT_ID_SE3 0x05
static struct nfc_hci_gate microread_gates[] = {
{MICROREAD_GATE_ID_ADM, MICROREAD_PIPE_ID_ADMIN},
{MICROREAD_GATE_ID_LOOPBACK, MICROREAD_PIPE_ID_HDS_LOOPBACK},
{MICROREAD_GATE_ID_IDT, MICROREAD_PIPE_ID_HDS_IDT},
{MICROREAD_GATE_ID_LMS, MICROREAD_PIPE_ID_LMS},
{MICROREAD_GATE_ID_MREAD_ISO_B, MICROREAD_PIPE_ID_HDS_MREAD_ISO_B},
{MICROREAD_GATE_ID_MREAD_ISO_A, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A},
{MICROREAD_GATE_ID_MREAD_ISO_A_3, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3},
{MICROREAD_GATE_ID_MGT, MICROREAD_PIPE_ID_MGT},
{MICROREAD_GATE_ID_OS, MICROREAD_PIPE_ID_OS},
{MICROREAD_GATE_ID_MREAD_NFC_T1, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1},
{MICROREAD_GATE_ID_MREAD_NFC_T3, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3},
{MICROREAD_GATE_ID_P2P_TARGET, MICROREAD_PIPE_ID_HDS_P2P_TARGET},
{MICROREAD_GATE_ID_P2P_INITIATOR, MICROREAD_PIPE_ID_HDS_P2P_INITIATOR}
};
/* Largest headroom needed for outgoing custom commands */
#define MICROREAD_CMDS_HEADROOM 2
#define MICROREAD_CMD_TAILROOM 2
struct microread_info {
struct nfc_phy_ops *phy_ops;
void *phy_id;
struct nfc_hci_dev *hdev;
int async_cb_type;
data_exchange_cb_t async_cb;
void *async_cb_context;
};
static int microread_open(struct nfc_hci_dev *hdev)
{
struct microread_info *info = nfc_hci_get_clientdata(hdev);
return info->phy_ops->enable(info->phy_id);
}
static void microread_close(struct nfc_hci_dev *hdev)
{
struct microread_info *info = nfc_hci_get_clientdata(hdev);
info->phy_ops->disable(info->phy_id);
}
static int microread_hci_ready(struct nfc_hci_dev *hdev)
{
int r;
u8 param[4];
param[0] = 0x03;
r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
MICROREAD_CMD_MREAD_SUBSCRIBE, param, 1, NULL);
if (r)
return r;
r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A_3,
MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
if (r)
return r;
param[0] = 0x00;
param[1] = 0x03;
param[2] = 0x00;
r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_B,
MICROREAD_CMD_MREAD_SUBSCRIBE, param, 3, NULL);
if (r)
return r;
r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T1,
MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
if (r)
return r;
param[0] = 0xFF;
param[1] = 0xFF;
param[2] = 0x00;
param[3] = 0x00;
r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T3,
MICROREAD_CMD_MREAD_SUBSCRIBE, param, 4, NULL);
return r;
}
static int microread_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
struct microread_info *info = nfc_hci_get_clientdata(hdev);
return info->phy_ops->write(info->phy_id, skb);
}
static int microread_start_poll(struct nfc_hci_dev *hdev,
u32 im_protocols, u32 tm_protocols)
{
int r;
u8 param[2];
u8 mode;
param[0] = 0x00;
param[1] = 0x00;
if (im_protocols & NFC_PROTO_ISO14443_MASK)
param[0] |= (1 << 2);
if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
param[0] |= 1;
if (im_protocols & NFC_PROTO_MIFARE_MASK)
param[1] |= 1;
if (im_protocols & NFC_PROTO_JEWEL_MASK)
param[0] |= (1 << 1);
if (im_protocols & NFC_PROTO_FELICA_MASK)
param[0] |= (1 << 5);
if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
param[1] |= (1 << 1);
if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
&hdev->gb_len);
if (hdev->gb == NULL || hdev->gb_len == 0) {
im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
}
}
r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
if (r)
return r;
mode = 0xff;
r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
if (r)
return r;
if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
MICROREAD_PAR_P2P_INITIATOR_GI,
hdev->gb, hdev->gb_len);
if (r)
return r;
}
if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
MICROREAD_PAR_P2P_TARGET_GT,
hdev->gb, hdev->gb_len);
if (r)
return r;
mode = 0x02;
r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
if (r)
return r;
}
return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
MICROREAD_EVT_MREAD_DISCOVERY_START_SOME,
param, 2);
}
static int microread_dep_link_up(struct nfc_hci_dev *hdev,
struct nfc_target *target, u8 comm_mode,
u8 *gb, size_t gb_len)
{
struct sk_buff *rgb_skb = NULL;
int r;
r = nfc_hci_get_param(hdev, target->hci_reader_gate,
MICROREAD_PAR_P2P_INITIATOR_GT, &rgb_skb);
if (r < 0)
return r;
if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
r = -EPROTO;
goto exit;
}
r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
rgb_skb->len);
if (r == 0)
r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
NFC_RF_INITIATOR);
exit:
kfree_skb(rgb_skb);
return r;
}
static int microread_dep_link_down(struct nfc_hci_dev *hdev)
{
return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
}
static int microread_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
struct nfc_target *target)
{
switch (gate) {
case MICROREAD_GATE_ID_P2P_INITIATOR:
target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
break;
default:
return -EPROTO;
}
return 0;
}
static int microread_complete_target_discovered(struct nfc_hci_dev *hdev,
u8 gate,
struct nfc_target *target)
{
return 0;
}
#define MICROREAD_CB_TYPE_READER_ALL 1
static void microread_im_transceive_cb(void *context, struct sk_buff *skb,
int err)
{
struct microread_info *info = context;
switch (info->async_cb_type) {
case MICROREAD_CB_TYPE_READER_ALL:
if (err == 0) {
if (skb->len == 0) {
err = -EPROTO;
kfree_skb(skb);
info->async_cb(info->async_cb_context, NULL,
-EPROTO);
return;
}
if (skb->data[skb->len - 1] != 0) {
err = nfc_hci_result_to_errno(
skb->data[skb->len - 1]);
kfree_skb(skb);
info->async_cb(info->async_cb_context, NULL,
err);
return;
}
skb_trim(skb, skb->len - 1); /* RF Error ind. */
}
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 microread_im_transceive(struct nfc_hci_dev *hdev,
struct nfc_target *target,
struct sk_buff *skb, data_exchange_cb_t cb,
void *cb_context)
{
struct microread_info *info = nfc_hci_get_clientdata(hdev);
u8 control_bits;
u16 crc;
pr_info("data exchange to gate 0x%x\n", target->hci_reader_gate);
if (target->hci_reader_gate == MICROREAD_GATE_ID_P2P_INITIATOR) {
*skb_push(skb, 1) = 0;
return nfc_hci_send_event(hdev, target->hci_reader_gate,
MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF,
skb->data, skb->len);
}
switch (target->hci_reader_gate) {
case MICROREAD_GATE_ID_MREAD_ISO_A:
control_bits = 0xCB;
break;
case MICROREAD_GATE_ID_MREAD_ISO_A_3:
control_bits = 0xCB;
break;
case MICROREAD_GATE_ID_MREAD_ISO_B:
control_bits = 0xCB;
break;
case MICROREAD_GATE_ID_MREAD_NFC_T1:
control_bits = 0x1B;
crc = crc_ccitt(0xffff, skb->data, skb->len);
crc = ~crc;
*skb_put(skb, 1) = crc & 0xff;
*skb_put(skb, 1) = crc >> 8;
break;
case MICROREAD_GATE_ID_MREAD_NFC_T3:
control_bits = 0xDB;
break;
default:
pr_info("Abort im_transceive to invalid gate 0x%x\n",
target->hci_reader_gate);
return 1;
}
*skb_push(skb, 1) = control_bits;
info->async_cb_type = MICROREAD_CB_TYPE_READER_ALL;
info->async_cb = cb;
info->async_cb_context = cb_context;
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
MICROREAD_CMD_MREAD_EXCHANGE,
skb->data, skb->len,
microread_im_transceive_cb, info);
}
static int microread_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
int r;
r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_TARGET,
MICROREAD_EVT_MCARD_EXCHANGE,
skb->data, skb->len);
kfree_skb(skb);
return r;
}
static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
struct sk_buff *skb)
{
struct nfc_target *targets;
int r = 0;
pr_info("target discovered to gate 0x%x\n", gate);
targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
if (targets == NULL) {
r = -ENOMEM;
goto exit;
}
targets->hci_reader_gate = gate;
switch (gate) {
case MICROREAD_GATE_ID_MREAD_ISO_A:
targets->supported_protocols =
nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A_SAK]);
targets->sens_res =
be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
r = -EINVAL;
goto exit_free;
}
memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
targets->nfcid1_len);
break;
case MICROREAD_GATE_ID_MREAD_ISO_A_3:
targets->supported_protocols =
nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A3_SAK]);
targets->sens_res =
be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);
targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];
targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
r = -EINVAL;
goto exit_free;
}
memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
targets->nfcid1_len);
break;
case MICROREAD_GATE_ID_MREAD_ISO_B:
targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_B_UID], 4);
targets->nfcid1_len = 4;
break;
case MICROREAD_GATE_ID_MREAD_NFC_T1:
targets->supported_protocols = NFC_PROTO_JEWEL_MASK;
targets->sens_res =
le16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_T1_ATQA]);
memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T1_UID], 4);
targets->nfcid1_len = 4;
break;
case MICROREAD_GATE_ID_MREAD_NFC_T3:
targets->supported_protocols = NFC_PROTO_FELICA_MASK;
memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T3_UID], 8);
targets->nfcid1_len = 8;
break;
default:
pr_info("discard target discovered to gate 0x%x\n", gate);
goto exit_free;
}
r = nfc_targets_found(hdev->ndev, targets, 1);
exit_free:
kfree(targets);
exit:
kfree_skb(skb);
if (r)
pr_err("Failed to handle discovered target err=%d\n", r);
}
static int microread_event_received(struct nfc_hci_dev *hdev, u8 gate,
u8 event, struct sk_buff *skb)
{
int r;
u8 mode;
pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate);
switch (event) {
case MICROREAD_EVT_MREAD_CARD_FOUND:
microread_target_discovered(hdev, gate, skb);
return 0;
case MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF:
if (skb->len < 1) {
kfree_skb(skb);
return -EPROTO;
}
if (skb->data[skb->len - 1]) {
kfree_skb(skb);
return -EIO;
}
skb_trim(skb, skb->len - 1);
r = nfc_tm_data_received(hdev->ndev, skb);
break;
case MICROREAD_EVT_MCARD_FIELD_ON:
case MICROREAD_EVT_MCARD_FIELD_OFF:
kfree_skb(skb);
return 0;
case MICROREAD_EVT_P2P_TARGET_ACTIVATED:
r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
NFC_COMM_PASSIVE, skb->data,
skb->len);
kfree_skb(skb);
break;
case MICROREAD_EVT_MCARD_EXCHANGE:
if (skb->len < 1) {
kfree_skb(skb);
return -EPROTO;
}
if (skb->data[skb->len-1]) {
kfree_skb(skb);
return -EIO;
}
skb_trim(skb, skb->len - 1);
r = nfc_tm_data_received(hdev->ndev, skb);
break;
case MICROREAD_EVT_P2P_TARGET_DEACTIVATED:
kfree_skb(skb);
mode = 0xff;
r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
if (r)
break;
r = nfc_hci_send_event(hdev, gate,
MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL,
0);
break;
default:
return 1;
}
return r;
}
static struct nfc_hci_ops microread_hci_ops = {
.open = microread_open,
.close = microread_close,
.hci_ready = microread_hci_ready,
.xmit = microread_xmit,
.start_poll = microread_start_poll,
.dep_link_up = microread_dep_link_up,
.dep_link_down = microread_dep_link_down,
.target_from_gate = microread_target_from_gate,
.complete_target_discovered = microread_complete_target_discovered,
.im_transceive = microread_im_transceive,
.tm_send = microread_tm_send,
.check_presence = NULL,
.event_received = microread_event_received,
};
int microread_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 microread_info *info;
unsigned long quirks = 0;
u32 protocols;
struct nfc_hci_init_data init_data;
int r;
info = kzalloc(sizeof(struct microread_info), GFP_KERNEL);
if (!info) {
r = -ENOMEM;
goto err_info_alloc;
}
info->phy_ops = phy_ops;
info->phy_id = phy_id;
init_data.gate_count = ARRAY_SIZE(microread_gates);
memcpy(init_data.gates, microread_gates, sizeof(microread_gates));
strcpy(init_data.session_id, "MICROREA");
set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
protocols = NFC_PROTO_JEWEL_MASK |
NFC_PROTO_MIFARE_MASK |
NFC_PROTO_FELICA_MASK |
NFC_PROTO_ISO14443_MASK |
NFC_PROTO_ISO14443_B_MASK |
NFC_PROTO_NFC_DEP_MASK;
info->hdev = nfc_hci_allocate_device(&microread_hci_ops, &init_data,
quirks, protocols, llc_name,
phy_headroom +
MICROREAD_CMDS_HEADROOM,
phy_tailroom +
MICROREAD_CMD_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);
err_info_alloc:
return r;
}
EXPORT_SYMBOL(microread_probe);
void microread_remove(struct nfc_hci_dev *hdev)
{
struct microread_info *info = nfc_hci_get_clientdata(hdev);
nfc_hci_unregister_device(hdev);
nfc_hci_free_device(hdev);
kfree(info);
}
EXPORT_SYMBOL(microread_remove);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

View file

@ -0,0 +1,31 @@
/*
* Copyright (C) 2011 - 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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_MICROREAD_H_
#define __LOCAL_MICROREAD_H_
#include <net/nfc/hci.h>
#define DRIVER_DESC "NFC driver for microread"
int microread_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 microread_remove(struct nfc_hci_dev *hdev);
#endif /* __LOCAL_MICROREAD_H_ */

View file

@ -0,0 +1,23 @@
config NFC_MRVL
tristate "Marvell NFC driver support"
depends on NFC_NCI
help
The core driver to support Marvell NFC devices.
This driver is required if you want to support
Marvell NFC device 8897.
Say Y here to compile Marvell NFC driver into the kernel or
say M to compile it as module.
config NFC_MRVL_USB
tristate "Marvell NFC-over-USB driver"
depends on NFC_MRVL && USB
help
Marvell NFC-over-USB driver.
This driver provides support for Marvell NFC-over-USB devices:
8897.
Say Y here to compile support for Marvell NFC-over-USB driver
into the kernel or say M to compile it as module.

View file

@ -0,0 +1,9 @@
#
# Makefile for NFCMRVL NCI based NFC driver
#
nfcmrvl-y += main.o
obj-$(CONFIG_NFC_MRVL) += nfcmrvl.o
nfcmrvl_usb-y += usb.o
obj-$(CONFIG_NFC_MRVL_USB) += nfcmrvl_usb.o

165
drivers/nfc/nfcmrvl/main.c Normal file
View file

@ -0,0 +1,165 @@
/*
* Marvell NFC driver: major functions
*
* Copyright (C) 2014, Marvell International Ltd.
*
* This software file (the "File") is distributed by Marvell International
* Ltd. under the terms of the GNU General Public License Version 2, June 1991
* (the "License"). You may use, redistribute and/or modify this File in
* accordance with the terms and conditions of the License, a copy of which
* is available on the worldwide web at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
* ARE EXPRESSLY DISCLAIMED. The License provides additional details about
* this warranty disclaimer.
*/
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include "nfcmrvl.h"
#define VERSION "1.0"
static int nfcmrvl_nci_open(struct nci_dev *ndev)
{
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
int err;
if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
return 0;
err = priv->if_ops->nci_open(priv);
if (err)
clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
return err;
}
static int nfcmrvl_nci_close(struct nci_dev *ndev)
{
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
return 0;
priv->if_ops->nci_close(priv);
return 0;
}
static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
{
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
nfc_info(priv->dev, "send entry, len %d\n", skb->len);
skb->dev = (void *)ndev;
if (!test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
return -EBUSY;
return priv->if_ops->nci_send(priv, skb);
}
static int nfcmrvl_nci_setup(struct nci_dev *ndev)
{
__u8 val;
val = NFCMRVL_GPIO_PIN_NFC_NOT_ALLOWED;
nci_set_config(ndev, NFCMRVL_NOT_ALLOWED_ID, 1, &val);
val = NFCMRVL_GPIO_PIN_NFC_ACTIVE;
nci_set_config(ndev, NFCMRVL_ACTIVE_ID, 1, &val);
val = NFCMRVL_EXT_COEX_ENABLE;
nci_set_config(ndev, NFCMRVL_EXT_COEX_ID, 1, &val);
return 0;
}
static struct nci_ops nfcmrvl_nci_ops = {
.open = nfcmrvl_nci_open,
.close = nfcmrvl_nci_close,
.send = nfcmrvl_nci_send,
.setup = nfcmrvl_nci_setup,
};
struct nfcmrvl_private *nfcmrvl_nci_register_dev(void *drv_data,
struct nfcmrvl_if_ops *ops,
struct device *dev)
{
struct nfcmrvl_private *priv;
int rc;
u32 protocols;
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv)
return ERR_PTR(-ENOMEM);
priv->drv_data = drv_data;
priv->if_ops = ops;
priv->dev = dev;
protocols = NFC_PROTO_JEWEL_MASK
| NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
| NFC_PROTO_ISO14443_MASK
| NFC_PROTO_ISO14443_B_MASK
| NFC_PROTO_NFC_DEP_MASK;
priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols, 0, 0);
if (!priv->ndev) {
nfc_err(dev, "nci_allocate_device failed");
rc = -ENOMEM;
goto error;
}
nci_set_drvdata(priv->ndev, priv);
rc = nci_register_device(priv->ndev);
if (rc) {
nfc_err(dev, "nci_register_device failed %d", rc);
nci_free_device(priv->ndev);
goto error;
}
nfc_info(dev, "registered with nci successfully\n");
return priv;
error:
kfree(priv);
return ERR_PTR(rc);
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);
void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
{
struct nci_dev *ndev = priv->ndev;
nci_unregister_device(ndev);
nci_free_device(ndev);
kfree(priv);
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);
int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, void *data, int count)
{
struct sk_buff *skb;
skb = nci_skb_alloc(priv->ndev, count, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
memcpy(skb_put(skb, count), data, count);
nci_recv_frame(priv->ndev, skb);
return count;
}
EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);
MODULE_AUTHOR("Marvell International Ltd.");
MODULE_DESCRIPTION("Marvell NFC driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL v2");

View file

@ -0,0 +1,48 @@
/**
* Marvell NFC driver
*
* Copyright (C) 2014, Marvell International Ltd.
*
* This software file (the "File") is distributed by Marvell International
* Ltd. under the terms of the GNU General Public License Version 2, June 1991
* (the "License"). You may use, redistribute and/or modify this File in
* accordance with the terms and conditions of the License, a copy of which
* is available on the worldwide web at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
* ARE EXPRESSLY DISCLAIMED. The License provides additional details about
* this warranty disclaimer.
**/
/* Define private flags: */
#define NFCMRVL_NCI_RUNNING 1
#define NFCMRVL_EXT_COEX_ID 0xE0
#define NFCMRVL_NOT_ALLOWED_ID 0xE1
#define NFCMRVL_ACTIVE_ID 0xE2
#define NFCMRVL_EXT_COEX_ENABLE 1
#define NFCMRVL_GPIO_PIN_NFC_NOT_ALLOWED 0xA
#define NFCMRVL_GPIO_PIN_NFC_ACTIVE 0xB
#define NFCMRVL_NCI_MAX_EVENT_SIZE 260
struct nfcmrvl_private {
struct nci_dev *ndev;
unsigned long flags;
void *drv_data;
struct device *dev;
struct nfcmrvl_if_ops *if_ops;
};
struct nfcmrvl_if_ops {
int (*nci_open) (struct nfcmrvl_private *priv);
int (*nci_close) (struct nfcmrvl_private *priv);
int (*nci_send) (struct nfcmrvl_private *priv, struct sk_buff *skb);
};
void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv);
int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, void *data, int count);
struct nfcmrvl_private *nfcmrvl_nci_register_dev(void *drv_data,
struct nfcmrvl_if_ops *ops,
struct device *dev);

459
drivers/nfc/nfcmrvl/usb.c Normal file
View file

@ -0,0 +1,459 @@
/**
* Marvell NFC-over-USB driver: USB interface related functions
*
* Copyright (C) 2014, Marvell International Ltd.
*
* This software file (the "File") is distributed by Marvell International
* Ltd. under the terms of the GNU General Public License Version 2, June 1991
* (the "License"). You may use, redistribute and/or modify this File in
* accordance with the terms and conditions of the License, a copy of which
* is available on the worldwide web at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
* IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
* ARE EXPRESSLY DISCLAIMED. The License provides additional details about
* this warranty disclaimer.
**/
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/nfc.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include "nfcmrvl.h"
#define VERSION "1.0"
static struct usb_device_id nfcmrvl_table[] = {
{ USB_DEVICE_INTERFACE_CLASS(0x1286, 0x2046, 0xff) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, nfcmrvl_table);
#define NFCMRVL_USB_BULK_RUNNING 1
#define NFCMRVL_USB_SUSPENDING 2
struct nfcmrvl_usb_drv_data {
struct usb_device *udev;
struct usb_interface *intf;
unsigned long flags;
struct work_struct waker;
struct usb_anchor tx_anchor;
struct usb_anchor bulk_anchor;
struct usb_anchor deferred;
int tx_in_flight;
/* protects tx_in_flight */
spinlock_t txlock;
struct usb_endpoint_descriptor *bulk_tx_ep;
struct usb_endpoint_descriptor *bulk_rx_ep;
int suspend_count;
struct nfcmrvl_private *priv;
};
static int nfcmrvl_inc_tx(struct nfcmrvl_usb_drv_data *drv_data)
{
unsigned long flags;
int rv;
spin_lock_irqsave(&drv_data->txlock, flags);
rv = test_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
if (!rv)
drv_data->tx_in_flight++;
spin_unlock_irqrestore(&drv_data->txlock, flags);
return rv;
}
static void nfcmrvl_bulk_complete(struct urb *urb)
{
struct nfcmrvl_usb_drv_data *drv_data = urb->context;
int err;
dev_dbg(&drv_data->udev->dev, "urb %p status %d count %d",
urb, urb->status, urb->actual_length);
if (!test_bit(NFCMRVL_NCI_RUNNING, &drv_data->flags))
return;
if (!urb->status) {
if (nfcmrvl_nci_recv_frame(drv_data->priv, urb->transfer_buffer,
urb->actual_length) < 0)
nfc_err(&drv_data->udev->dev, "corrupted Rx packet");
}
if (!test_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags))
return;
usb_anchor_urb(urb, &drv_data->bulk_anchor);
usb_mark_last_busy(drv_data->udev);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err) {
/* -EPERM: urb is being killed;
* -ENODEV: device got disconnected
*/
if (err != -EPERM && err != -ENODEV)
nfc_err(&drv_data->udev->dev,
"urb %p failed to resubmit (%d)", urb, -err);
usb_unanchor_urb(urb);
}
}
static int
nfcmrvl_submit_bulk_urb(struct nfcmrvl_usb_drv_data *drv_data, gfp_t mem_flags)
{
struct urb *urb;
unsigned char *buf;
unsigned int pipe;
int err, size = NFCMRVL_NCI_MAX_EVENT_SIZE;
if (!drv_data->bulk_rx_ep)
return -ENODEV;
urb = usb_alloc_urb(0, mem_flags);
if (!urb)
return -ENOMEM;
buf = kmalloc(size, mem_flags);
if (!buf) {
usb_free_urb(urb);
return -ENOMEM;
}
pipe = usb_rcvbulkpipe(drv_data->udev,
drv_data->bulk_rx_ep->bEndpointAddress);
usb_fill_bulk_urb(urb, drv_data->udev, pipe, buf, size,
nfcmrvl_bulk_complete, drv_data);
urb->transfer_flags |= URB_FREE_BUFFER;
usb_mark_last_busy(drv_data->udev);
usb_anchor_urb(urb, &drv_data->bulk_anchor);
err = usb_submit_urb(urb, mem_flags);
if (err) {
if (err != -EPERM && err != -ENODEV)
nfc_err(&drv_data->udev->dev,
"urb %p submission failed (%d)", urb, -err);
usb_unanchor_urb(urb);
}
usb_free_urb(urb);
return err;
}
static void nfcmrvl_tx_complete(struct urb *urb)
{
struct sk_buff *skb = urb->context;
struct nci_dev *ndev = (struct nci_dev *)skb->dev;
struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
nfc_info(priv->dev, "urb %p status %d count %d",
urb, urb->status, urb->actual_length);
spin_lock(&drv_data->txlock);
drv_data->tx_in_flight--;
spin_unlock(&drv_data->txlock);
kfree(urb->setup_packet);
kfree_skb(skb);
}
static int nfcmrvl_usb_nci_open(struct nfcmrvl_private *priv)
{
struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
int err;
err = usb_autopm_get_interface(drv_data->intf);
if (err)
return err;
drv_data->intf->needs_remote_wakeup = 1;
err = nfcmrvl_submit_bulk_urb(drv_data, GFP_KERNEL);
if (err)
goto failed;
set_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
nfcmrvl_submit_bulk_urb(drv_data, GFP_KERNEL);
usb_autopm_put_interface(drv_data->intf);
return 0;
failed:
usb_autopm_put_interface(drv_data->intf);
return err;
}
static void nfcmrvl_usb_stop_traffic(struct nfcmrvl_usb_drv_data *drv_data)
{
usb_kill_anchored_urbs(&drv_data->bulk_anchor);
}
static int nfcmrvl_usb_nci_close(struct nfcmrvl_private *priv)
{
struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
int err;
cancel_work_sync(&drv_data->waker);
clear_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
nfcmrvl_usb_stop_traffic(drv_data);
usb_kill_anchored_urbs(&drv_data->tx_anchor);
err = usb_autopm_get_interface(drv_data->intf);
if (err)
goto failed;
drv_data->intf->needs_remote_wakeup = 0;
usb_autopm_put_interface(drv_data->intf);
failed:
usb_scuttle_anchored_urbs(&drv_data->deferred);
return 0;
}
static int nfcmrvl_usb_nci_send(struct nfcmrvl_private *priv,
struct sk_buff *skb)
{
struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
struct urb *urb;
unsigned int pipe;
int err;
if (!drv_data->bulk_tx_ep)
return -ENODEV;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb)
return -ENOMEM;
pipe = usb_sndbulkpipe(drv_data->udev,
drv_data->bulk_tx_ep->bEndpointAddress);
usb_fill_bulk_urb(urb, drv_data->udev, pipe, skb->data, skb->len,
nfcmrvl_tx_complete, skb);
err = nfcmrvl_inc_tx(drv_data);
if (err) {
usb_anchor_urb(urb, &drv_data->deferred);
schedule_work(&drv_data->waker);
err = 0;
goto done;
}
usb_anchor_urb(urb, &drv_data->tx_anchor);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err) {
if (err != -EPERM && err != -ENODEV)
nfc_err(&drv_data->udev->dev,
"urb %p submission failed (%d)", urb, -err);
kfree(urb->setup_packet);
usb_unanchor_urb(urb);
} else {
usb_mark_last_busy(drv_data->udev);
}
done:
usb_free_urb(urb);
return err;
}
static struct nfcmrvl_if_ops usb_ops = {
.nci_open = nfcmrvl_usb_nci_open,
.nci_close = nfcmrvl_usb_nci_close,
.nci_send = nfcmrvl_usb_nci_send,
};
static void nfcmrvl_waker(struct work_struct *work)
{
struct nfcmrvl_usb_drv_data *drv_data =
container_of(work, struct nfcmrvl_usb_drv_data, waker);
int err;
err = usb_autopm_get_interface(drv_data->intf);
if (err)
return;
usb_autopm_put_interface(drv_data->intf);
}
static int nfcmrvl_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_endpoint_descriptor *ep_desc;
struct nfcmrvl_usb_drv_data *drv_data;
struct nfcmrvl_private *priv;
int i;
struct usb_device *udev = interface_to_usbdev(intf);
nfc_info(&udev->dev, "intf %p id %p", intf, id);
drv_data = devm_kzalloc(&intf->dev, sizeof(*drv_data), GFP_KERNEL);
if (!drv_data)
return -ENOMEM;
for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
ep_desc = &intf->cur_altsetting->endpoint[i].desc;
if (!drv_data->bulk_tx_ep &&
usb_endpoint_is_bulk_out(ep_desc)) {
drv_data->bulk_tx_ep = ep_desc;
continue;
}
if (!drv_data->bulk_rx_ep &&
usb_endpoint_is_bulk_in(ep_desc)) {
drv_data->bulk_rx_ep = ep_desc;
continue;
}
}
if (!drv_data->bulk_tx_ep || !drv_data->bulk_rx_ep)
return -ENODEV;
drv_data->udev = udev;
drv_data->intf = intf;
INIT_WORK(&drv_data->waker, nfcmrvl_waker);
spin_lock_init(&drv_data->txlock);
init_usb_anchor(&drv_data->tx_anchor);
init_usb_anchor(&drv_data->bulk_anchor);
init_usb_anchor(&drv_data->deferred);
priv = nfcmrvl_nci_register_dev(drv_data, &usb_ops,
&drv_data->udev->dev);
if (IS_ERR(priv))
return PTR_ERR(priv);
drv_data->priv = priv;
priv->dev = &drv_data->udev->dev;
usb_set_intfdata(intf, drv_data);
return 0;
}
static void nfcmrvl_disconnect(struct usb_interface *intf)
{
struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
if (!drv_data)
return;
nfc_info(&drv_data->udev->dev, "intf %p", intf);
nfcmrvl_nci_unregister_dev(drv_data->priv);
usb_set_intfdata(drv_data->intf, NULL);
}
#ifdef CONFIG_PM
static int nfcmrvl_suspend(struct usb_interface *intf, pm_message_t message)
{
struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
nfc_info(&drv_data->udev->dev, "intf %p", intf);
if (drv_data->suspend_count++)
return 0;
spin_lock_irq(&drv_data->txlock);
if (!(PMSG_IS_AUTO(message) && drv_data->tx_in_flight)) {
set_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
spin_unlock_irq(&drv_data->txlock);
} else {
spin_unlock_irq(&drv_data->txlock);
drv_data->suspend_count--;
return -EBUSY;
}
nfcmrvl_usb_stop_traffic(drv_data);
usb_kill_anchored_urbs(&drv_data->tx_anchor);
return 0;
}
static void nfcmrvl_play_deferred(struct nfcmrvl_usb_drv_data *drv_data)
{
struct urb *urb;
int err;
while ((urb = usb_get_from_anchor(&drv_data->deferred))) {
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err)
break;
drv_data->tx_in_flight++;
}
usb_scuttle_anchored_urbs(&drv_data->deferred);
}
static int nfcmrvl_resume(struct usb_interface *intf)
{
struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
int err = 0;
nfc_info(&drv_data->udev->dev, "intf %p", intf);
if (--drv_data->suspend_count)
return 0;
if (!test_bit(NFCMRVL_NCI_RUNNING, &drv_data->flags))
goto done;
if (test_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags)) {
err = nfcmrvl_submit_bulk_urb(drv_data, GFP_NOIO);
if (err) {
clear_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
goto failed;
}
nfcmrvl_submit_bulk_urb(drv_data, GFP_NOIO);
}
spin_lock_irq(&drv_data->txlock);
nfcmrvl_play_deferred(drv_data);
clear_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
spin_unlock_irq(&drv_data->txlock);
return 0;
failed:
usb_scuttle_anchored_urbs(&drv_data->deferred);
done:
spin_lock_irq(&drv_data->txlock);
clear_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
spin_unlock_irq(&drv_data->txlock);
return err;
}
#endif
static struct usb_driver nfcmrvl_usb_driver = {
.name = "nfcmrvl",
.probe = nfcmrvl_probe,
.disconnect = nfcmrvl_disconnect,
#ifdef CONFIG_PM
.suspend = nfcmrvl_suspend,
.resume = nfcmrvl_resume,
.reset_resume = nfcmrvl_resume,
#endif
.id_table = nfcmrvl_table,
.supports_autosuspend = 1,
.disable_hub_initiated_lpm = 1,
.soft_unbind = 1,
};
module_usb_driver(nfcmrvl_usb_driver);
MODULE_AUTHOR("Marvell International Ltd.");
MODULE_DESCRIPTION("Marvell NFC-over-USB driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL v2");

541
drivers/nfc/nfcsim.c Normal file
View file

@ -0,0 +1,541 @@
/*
* NFC hardware simulation driver
* Copyright (c) 2013, Intel Corporation.
*
* 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 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.
*
*/
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/nfc.h>
#define DEV_ERR(_dev, fmt, args...) nfc_err(&_dev->nfc_dev->dev, \
"%s: " fmt, __func__, ## args)
#define DEV_DBG(_dev, fmt, args...) dev_dbg(&_dev->nfc_dev->dev, \
"%s: " fmt, __func__, ## args)
#define NFCSIM_VERSION "0.1"
#define NFCSIM_POLL_NONE 0
#define NFCSIM_POLL_INITIATOR 1
#define NFCSIM_POLL_TARGET 2
#define NFCSIM_POLL_DUAL (NFCSIM_POLL_INITIATOR | NFCSIM_POLL_TARGET)
struct nfcsim {
struct nfc_dev *nfc_dev;
struct mutex lock;
struct delayed_work recv_work;
struct sk_buff *clone_skb;
struct delayed_work poll_work;
u8 polling_mode;
u8 curr_polling_mode;
u8 shutting_down;
u8 up;
u8 initiator;
data_exchange_cb_t cb;
void *cb_context;
struct nfcsim *peer_dev;
};
static struct nfcsim *dev0;
static struct nfcsim *dev1;
static struct workqueue_struct *wq;
static void nfcsim_cleanup_dev(struct nfcsim *dev, u8 shutdown)
{
DEV_DBG(dev, "shutdown=%d\n", shutdown);
mutex_lock(&dev->lock);
dev->polling_mode = NFCSIM_POLL_NONE;
dev->shutting_down = shutdown;
dev->cb = NULL;
dev_kfree_skb(dev->clone_skb);
dev->clone_skb = NULL;
mutex_unlock(&dev->lock);
cancel_delayed_work_sync(&dev->poll_work);
cancel_delayed_work_sync(&dev->recv_work);
}
static int nfcsim_target_found(struct nfcsim *dev)
{
struct nfc_target nfc_tgt;
DEV_DBG(dev, "\n");
memset(&nfc_tgt, 0, sizeof(struct nfc_target));
nfc_tgt.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
return 0;
}
static int nfcsim_dev_up(struct nfc_dev *nfc_dev)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "\n");
mutex_lock(&dev->lock);
dev->up = 1;
mutex_unlock(&dev->lock);
return 0;
}
static int nfcsim_dev_down(struct nfc_dev *nfc_dev)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "\n");
mutex_lock(&dev->lock);
dev->up = 0;
mutex_unlock(&dev->lock);
return 0;
}
static int nfcsim_dep_link_up(struct nfc_dev *nfc_dev,
struct nfc_target *target,
u8 comm_mode, u8 *gb, size_t gb_len)
{
int rc;
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
struct nfcsim *peer = dev->peer_dev;
u8 *remote_gb;
size_t remote_gb_len;
DEV_DBG(dev, "target_idx: %d, comm_mode: %d\n", target->idx, comm_mode);
mutex_lock(&peer->lock);
nfc_tm_activated(peer->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
NFC_COMM_ACTIVE, gb, gb_len);
remote_gb = nfc_get_local_general_bytes(peer->nfc_dev, &remote_gb_len);
if (!remote_gb) {
DEV_ERR(peer, "Can't get remote general bytes\n");
mutex_unlock(&peer->lock);
return -EINVAL;
}
mutex_unlock(&peer->lock);
mutex_lock(&dev->lock);
rc = nfc_set_remote_general_bytes(nfc_dev, remote_gb, remote_gb_len);
if (rc) {
DEV_ERR(dev, "Can't set remote general bytes\n");
mutex_unlock(&dev->lock);
return rc;
}
rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_ACTIVE,
NFC_RF_INITIATOR);
mutex_unlock(&dev->lock);
return rc;
}
static int nfcsim_dep_link_down(struct nfc_dev *nfc_dev)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "\n");
nfcsim_cleanup_dev(dev, 0);
return 0;
}
static int nfcsim_start_poll(struct nfc_dev *nfc_dev,
u32 im_protocols, u32 tm_protocols)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
int rc;
mutex_lock(&dev->lock);
if (dev->polling_mode != NFCSIM_POLL_NONE) {
DEV_ERR(dev, "Already in polling mode\n");
rc = -EBUSY;
goto exit;
}
if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
dev->polling_mode |= NFCSIM_POLL_INITIATOR;
if (tm_protocols & NFC_PROTO_NFC_DEP_MASK)
dev->polling_mode |= NFCSIM_POLL_TARGET;
if (dev->polling_mode == NFCSIM_POLL_NONE) {
DEV_ERR(dev, "Unsupported polling mode\n");
rc = -EINVAL;
goto exit;
}
dev->initiator = 0;
dev->curr_polling_mode = NFCSIM_POLL_NONE;
queue_delayed_work(wq, &dev->poll_work, 0);
DEV_DBG(dev, "Start polling: im: 0x%X, tm: 0x%X\n", im_protocols,
tm_protocols);
rc = 0;
exit:
mutex_unlock(&dev->lock);
return rc;
}
static void nfcsim_stop_poll(struct nfc_dev *nfc_dev)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "Stop poll\n");
mutex_lock(&dev->lock);
dev->polling_mode = NFCSIM_POLL_NONE;
mutex_unlock(&dev->lock);
cancel_delayed_work_sync(&dev->poll_work);
}
static int nfcsim_activate_target(struct nfc_dev *nfc_dev,
struct nfc_target *target, u32 protocol)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "\n");
return -ENOTSUPP;
}
static void nfcsim_deactivate_target(struct nfc_dev *nfc_dev,
struct nfc_target *target)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
DEV_DBG(dev, "\n");
}
static void nfcsim_wq_recv(struct work_struct *work)
{
struct nfcsim *dev = container_of(work, struct nfcsim,
recv_work.work);
mutex_lock(&dev->lock);
if (dev->shutting_down || !dev->up || !dev->clone_skb) {
dev_kfree_skb(dev->clone_skb);
goto exit;
}
if (dev->initiator) {
if (!dev->cb) {
DEV_ERR(dev, "Null recv callback\n");
dev_kfree_skb(dev->clone_skb);
goto exit;
}
dev->cb(dev->cb_context, dev->clone_skb, 0);
dev->cb = NULL;
} else {
nfc_tm_data_received(dev->nfc_dev, dev->clone_skb);
}
exit:
dev->clone_skb = NULL;
mutex_unlock(&dev->lock);
}
static int nfcsim_tx(struct nfc_dev *nfc_dev, struct nfc_target *target,
struct sk_buff *skb, data_exchange_cb_t cb,
void *cb_context)
{
struct nfcsim *dev = nfc_get_drvdata(nfc_dev);
struct nfcsim *peer = dev->peer_dev;
int err;
mutex_lock(&dev->lock);
if (dev->shutting_down || !dev->up) {
mutex_unlock(&dev->lock);
err = -ENODEV;
goto exit;
}
dev->cb = cb;
dev->cb_context = cb_context;
mutex_unlock(&dev->lock);
mutex_lock(&peer->lock);
peer->clone_skb = skb_clone(skb, GFP_KERNEL);
if (!peer->clone_skb) {
DEV_ERR(dev, "skb_clone failed\n");
mutex_unlock(&peer->lock);
err = -ENOMEM;
goto exit;
}
/* This simulates an arbitrary transmission delay between the 2 devices.
* If packet transmission occurs immediately between them, we have a
* non-stop flow of several tens of thousands SYMM packets per second
* and a burning cpu.
*
* TODO: Add support for a sysfs entry to control this delay.
*/
queue_delayed_work(wq, &peer->recv_work, msecs_to_jiffies(5));
mutex_unlock(&peer->lock);
err = 0;
exit:
dev_kfree_skb(skb);
return err;
}
static int nfcsim_im_transceive(struct nfc_dev *nfc_dev,
struct nfc_target *target, struct sk_buff *skb,
data_exchange_cb_t cb, void *cb_context)
{
return nfcsim_tx(nfc_dev, target, skb, cb, cb_context);
}
static int nfcsim_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
{
return nfcsim_tx(nfc_dev, NULL, skb, NULL, NULL);
}
static struct nfc_ops nfcsim_nfc_ops = {
.dev_up = nfcsim_dev_up,
.dev_down = nfcsim_dev_down,
.dep_link_up = nfcsim_dep_link_up,
.dep_link_down = nfcsim_dep_link_down,
.start_poll = nfcsim_start_poll,
.stop_poll = nfcsim_stop_poll,
.activate_target = nfcsim_activate_target,
.deactivate_target = nfcsim_deactivate_target,
.im_transceive = nfcsim_im_transceive,
.tm_send = nfcsim_tm_send,
};
static void nfcsim_set_polling_mode(struct nfcsim *dev)
{
if (dev->polling_mode == NFCSIM_POLL_NONE) {
dev->curr_polling_mode = NFCSIM_POLL_NONE;
return;
}
if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
if (dev->polling_mode & NFCSIM_POLL_INITIATOR)
dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
else
dev->curr_polling_mode = NFCSIM_POLL_TARGET;
return;
}
if (dev->polling_mode == NFCSIM_POLL_DUAL) {
if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
dev->curr_polling_mode = NFCSIM_POLL_INITIATOR;
else
dev->curr_polling_mode = NFCSIM_POLL_TARGET;
}
}
static void nfcsim_wq_poll(struct work_struct *work)
{
struct nfcsim *dev = container_of(work, struct nfcsim, poll_work.work);
struct nfcsim *peer = dev->peer_dev;
/* These work items run on an ordered workqueue and are therefore
* serialized. So we can take both mutexes without being dead locked.
*/
mutex_lock(&dev->lock);
mutex_lock(&peer->lock);
nfcsim_set_polling_mode(dev);
if (dev->curr_polling_mode == NFCSIM_POLL_NONE) {
DEV_DBG(dev, "Not polling\n");
goto unlock;
}
DEV_DBG(dev, "Polling as %s",
dev->curr_polling_mode == NFCSIM_POLL_INITIATOR ?
"initiator\n" : "target\n");
if (dev->curr_polling_mode == NFCSIM_POLL_TARGET)
goto sched_work;
if (peer->curr_polling_mode == NFCSIM_POLL_TARGET) {
peer->polling_mode = NFCSIM_POLL_NONE;
dev->polling_mode = NFCSIM_POLL_NONE;
dev->initiator = 1;
nfcsim_target_found(dev);
goto unlock;
}
sched_work:
/* This defines the delay for an initiator to check if the other device
* is polling in target mode.
* If the device starts in dual mode polling, it switches between
* initiator and target at every round.
* Because the wq is ordered and only 1 work item is executed at a time,
* we'll always have one device polling as initiator and the other as
* target at some point, even if both are started in dual mode.
*/
queue_delayed_work(wq, &dev->poll_work, msecs_to_jiffies(200));
unlock:
mutex_unlock(&peer->lock);
mutex_unlock(&dev->lock);
}
static struct nfcsim *nfcsim_init_dev(void)
{
struct nfcsim *dev;
int rc = -ENOMEM;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev == NULL)
return ERR_PTR(-ENOMEM);
mutex_init(&dev->lock);
INIT_DELAYED_WORK(&dev->recv_work, nfcsim_wq_recv);
INIT_DELAYED_WORK(&dev->poll_work, nfcsim_wq_poll);
dev->nfc_dev = nfc_allocate_device(&nfcsim_nfc_ops,
NFC_PROTO_NFC_DEP_MASK,
0, 0);
if (!dev->nfc_dev)
goto error;
nfc_set_drvdata(dev->nfc_dev, dev);
rc = nfc_register_device(dev->nfc_dev);
if (rc)
goto free_nfc_dev;
return dev;
free_nfc_dev:
nfc_free_device(dev->nfc_dev);
error:
kfree(dev);
return ERR_PTR(rc);
}
static void nfcsim_free_device(struct nfcsim *dev)
{
nfc_unregister_device(dev->nfc_dev);
nfc_free_device(dev->nfc_dev);
kfree(dev);
}
static int __init nfcsim_init(void)
{
int rc;
/* We need an ordered wq to ensure that poll_work items are executed
* one at a time.
*/
wq = alloc_ordered_workqueue("nfcsim", 0);
if (!wq) {
rc = -ENOMEM;
goto exit;
}
dev0 = nfcsim_init_dev();
if (IS_ERR(dev0)) {
rc = PTR_ERR(dev0);
goto exit;
}
dev1 = nfcsim_init_dev();
if (IS_ERR(dev1)) {
kfree(dev0);
rc = PTR_ERR(dev1);
goto exit;
}
dev0->peer_dev = dev1;
dev1->peer_dev = dev0;
pr_debug("NFCsim " NFCSIM_VERSION " initialized\n");
rc = 0;
exit:
if (rc)
pr_err("Failed to initialize nfcsim driver (%d)\n",
rc);
return rc;
}
static void __exit nfcsim_exit(void)
{
nfcsim_cleanup_dev(dev0, 1);
nfcsim_cleanup_dev(dev1, 1);
nfcsim_free_device(dev0);
nfcsim_free_device(dev1);
destroy_workqueue(wq);
}
module_init(nfcsim_init);
module_exit(nfcsim_exit);
MODULE_DESCRIPTION("NFCSim driver ver " NFCSIM_VERSION);
MODULE_VERSION(NFCSIM_VERSION);
MODULE_LICENSE("GPL");

579
drivers/nfc/nfcwilink.c Normal file
View file

@ -0,0 +1,579 @@
/*
* Texas Instrument's NFC Driver For Shared Transport.
*
* NFC Driver acts as interface between NCI core and
* TI Shared Transport Layer.
*
* Copyright (C) 2011 Texas Instruments, Inc.
*
* Written by Ilan Elias <ilane@ti.com>
*
* Acknowledgements:
* This file is based on btwilink.c, which was written
* by Raja Mani and Pavan Savoy.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms 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/platform_device.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/firmware.h>
#include <linux/nfc.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include <linux/ti_wilink_st.h>
#define NFCWILINK_CHNL 12
#define NFCWILINK_OPCODE 7
#define NFCWILINK_MAX_FRAME_SIZE 300
#define NFCWILINK_HDR_LEN 4
#define NFCWILINK_OFFSET_LEN_IN_HDR 1
#define NFCWILINK_LEN_SIZE 2
#define NFCWILINK_REGISTER_TIMEOUT 8000 /* 8 sec */
#define NFCWILINK_CMD_TIMEOUT 5000 /* 5 sec */
#define BTS_FILE_NAME_MAX_SIZE 40
#define BTS_FILE_HDR_MAGIC 0x42535442
#define BTS_FILE_CMD_MAX_LEN 0xff
#define BTS_FILE_ACTION_TYPE_SEND_CMD 1
#define NCI_VS_NFCC_INFO_CMD_GID 0x2f
#define NCI_VS_NFCC_INFO_CMD_OID 0x12
#define NCI_VS_NFCC_INFO_RSP_GID 0x4f
#define NCI_VS_NFCC_INFO_RSP_OID 0x12
struct nfcwilink_hdr {
__u8 chnl;
__u8 opcode;
__le16 len;
} __packed;
struct nci_vs_nfcc_info_cmd {
__u8 gid;
__u8 oid;
__u8 plen;
} __packed;
struct nci_vs_nfcc_info_rsp {
__u8 gid;
__u8 oid;
__u8 plen;
__u8 status;
__u8 hw_id;
__u8 sw_ver_x;
__u8 sw_ver_z;
__u8 patch_id;
} __packed;
struct bts_file_hdr {
__le32 magic;
__le32 ver;
__u8 rfu[24];
__u8 actions[0];
} __packed;
struct bts_file_action {
__le16 type;
__le16 len;
__u8 data[0];
} __packed;
struct nfcwilink {
struct platform_device *pdev;
struct nci_dev *ndev;
unsigned long flags;
char st_register_cb_status;
long (*st_write) (struct sk_buff *);
struct completion completed;
struct nci_vs_nfcc_info_rsp nfcc_info;
};
/* NFCWILINK driver flags */
enum {
NFCWILINK_RUNNING,
NFCWILINK_FW_DOWNLOAD,
};
static int nfcwilink_send(struct nci_dev *ndev, struct sk_buff *skb);
static inline struct sk_buff *nfcwilink_skb_alloc(unsigned int len, gfp_t how)
{
struct sk_buff *skb;
skb = alloc_skb(len + NFCWILINK_HDR_LEN, how);
if (skb)
skb_reserve(skb, NFCWILINK_HDR_LEN);
return skb;
}
static void nfcwilink_fw_download_receive(struct nfcwilink *drv,
struct sk_buff *skb)
{
struct nci_vs_nfcc_info_rsp *rsp = (void *)skb->data;
/* Detect NCI_VS_NFCC_INFO_RSP and store the result */
if ((skb->len > 3) && (rsp->gid == NCI_VS_NFCC_INFO_RSP_GID) &&
(rsp->oid == NCI_VS_NFCC_INFO_RSP_OID)) {
memcpy(&drv->nfcc_info, rsp,
sizeof(struct nci_vs_nfcc_info_rsp));
}
kfree_skb(skb);
complete(&drv->completed);
}
static int nfcwilink_get_bts_file_name(struct nfcwilink *drv, char *file_name)
{
struct nci_vs_nfcc_info_cmd *cmd;
struct sk_buff *skb;
unsigned long comp_ret;
int rc;
skb = nfcwilink_skb_alloc(sizeof(struct nci_vs_nfcc_info_cmd),
GFP_KERNEL);
if (!skb) {
nfc_err(&drv->pdev->dev,
"no memory for nci_vs_nfcc_info_cmd\n");
return -ENOMEM;
}
cmd = (struct nci_vs_nfcc_info_cmd *)
skb_put(skb, sizeof(struct nci_vs_nfcc_info_cmd));
cmd->gid = NCI_VS_NFCC_INFO_CMD_GID;
cmd->oid = NCI_VS_NFCC_INFO_CMD_OID;
cmd->plen = 0;
drv->nfcc_info.plen = 0;
rc = nfcwilink_send(drv->ndev, skb);
if (rc)
return rc;
comp_ret = wait_for_completion_timeout(&drv->completed,
msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld\n",
comp_ret);
if (comp_ret == 0) {
nfc_err(&drv->pdev->dev,
"timeout on wait_for_completion_timeout\n");
return -ETIMEDOUT;
}
dev_dbg(&drv->pdev->dev, "nci_vs_nfcc_info_rsp: plen %d, status %d\n",
drv->nfcc_info.plen, drv->nfcc_info.status);
if ((drv->nfcc_info.plen != 5) || (drv->nfcc_info.status != 0)) {
nfc_err(&drv->pdev->dev, "invalid nci_vs_nfcc_info_rsp\n");
return -EINVAL;
}
snprintf(file_name, BTS_FILE_NAME_MAX_SIZE,
"TINfcInit_%d.%d.%d.%d.bts",
drv->nfcc_info.hw_id,
drv->nfcc_info.sw_ver_x,
drv->nfcc_info.sw_ver_z,
drv->nfcc_info.patch_id);
nfc_info(&drv->pdev->dev, "nfcwilink FW file name: %s\n", file_name);
return 0;
}
static int nfcwilink_send_bts_cmd(struct nfcwilink *drv, __u8 *data, int len)
{
struct nfcwilink_hdr *hdr = (struct nfcwilink_hdr *)data;
struct sk_buff *skb;
unsigned long comp_ret;
int rc;
/* verify valid cmd for the NFC channel */
if ((len <= sizeof(struct nfcwilink_hdr)) ||
(len > BTS_FILE_CMD_MAX_LEN) ||
(hdr->chnl != NFCWILINK_CHNL) ||
(hdr->opcode != NFCWILINK_OPCODE)) {
nfc_err(&drv->pdev->dev,
"ignoring invalid bts cmd, len %d, chnl %d, opcode %d\n",
len, hdr->chnl, hdr->opcode);
return 0;
}
/* remove the ST header */
len -= sizeof(struct nfcwilink_hdr);
data += sizeof(struct nfcwilink_hdr);
skb = nfcwilink_skb_alloc(len, GFP_KERNEL);
if (!skb) {
nfc_err(&drv->pdev->dev, "no memory for bts cmd\n");
return -ENOMEM;
}
memcpy(skb_put(skb, len), data, len);
rc = nfcwilink_send(drv->ndev, skb);
if (rc)
return rc;
comp_ret = wait_for_completion_timeout(&drv->completed,
msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld\n",
comp_ret);
if (comp_ret == 0) {
nfc_err(&drv->pdev->dev,
"timeout on wait_for_completion_timeout\n");
return -ETIMEDOUT;
}
return 0;
}
static int nfcwilink_download_fw(struct nfcwilink *drv)
{
unsigned char file_name[BTS_FILE_NAME_MAX_SIZE];
const struct firmware *fw;
__u16 action_type, action_len;
__u8 *ptr;
int len, rc;
set_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
rc = nfcwilink_get_bts_file_name(drv, file_name);
if (rc)
goto exit;
rc = request_firmware(&fw, file_name, &drv->pdev->dev);
if (rc) {
nfc_err(&drv->pdev->dev, "request_firmware failed %d\n", rc);
/* if the file is not found, don't exit with failure */
if (rc == -ENOENT)
rc = 0;
goto exit;
}
len = fw->size;
ptr = (__u8 *)fw->data;
if ((len == 0) || (ptr == NULL)) {
dev_dbg(&drv->pdev->dev,
"request_firmware returned size %d\n", len);
goto release_fw;
}
if (__le32_to_cpu(((struct bts_file_hdr *)ptr)->magic) !=
BTS_FILE_HDR_MAGIC) {
nfc_err(&drv->pdev->dev, "wrong bts magic number\n");
rc = -EINVAL;
goto release_fw;
}
/* remove the BTS header */
len -= sizeof(struct bts_file_hdr);
ptr += sizeof(struct bts_file_hdr);
while (len > 0) {
action_type =
__le16_to_cpu(((struct bts_file_action *)ptr)->type);
action_len =
__le16_to_cpu(((struct bts_file_action *)ptr)->len);
dev_dbg(&drv->pdev->dev, "bts_file_action type %d, len %d\n",
action_type, action_len);
switch (action_type) {
case BTS_FILE_ACTION_TYPE_SEND_CMD:
rc = nfcwilink_send_bts_cmd(drv,
((struct bts_file_action *)ptr)->data,
action_len);
if (rc)
goto release_fw;
break;
}
/* advance to the next action */
len -= (sizeof(struct bts_file_action) + action_len);
ptr += (sizeof(struct bts_file_action) + action_len);
}
release_fw:
release_firmware(fw);
exit:
clear_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
return rc;
}
/* Called by ST when registration is complete */
static void nfcwilink_register_complete(void *priv_data, char data)
{
struct nfcwilink *drv = priv_data;
/* store ST registration status */
drv->st_register_cb_status = data;
/* complete the wait in nfc_st_open() */
complete(&drv->completed);
}
/* Called by ST when receive data is available */
static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
{
struct nfcwilink *drv = priv_data;
int rc;
if (!skb)
return -EFAULT;
if (!drv) {
kfree_skb(skb);
return -EFAULT;
}
dev_dbg(&drv->pdev->dev, "receive entry, len %d\n", skb->len);
/* strip the ST header
(apart for the chnl byte, which is not received in the hdr) */
skb_pull(skb, (NFCWILINK_HDR_LEN-1));
if (test_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags)) {
nfcwilink_fw_download_receive(drv, skb);
return 0;
}
/* Forward skb to NCI core layer */
rc = nci_recv_frame(drv->ndev, skb);
if (rc < 0) {
nfc_err(&drv->pdev->dev, "nci_recv_frame failed %d\n", rc);
return rc;
}
return 0;
}
/* protocol structure registered with ST */
static struct st_proto_s nfcwilink_proto = {
.chnl_id = NFCWILINK_CHNL,
.max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
.hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
.offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
.len_size = NFCWILINK_LEN_SIZE,
.reserve = 0,
.recv = nfcwilink_receive,
.reg_complete_cb = nfcwilink_register_complete,
.write = NULL,
};
static int nfcwilink_open(struct nci_dev *ndev)
{
struct nfcwilink *drv = nci_get_drvdata(ndev);
unsigned long comp_ret;
int rc;
if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
rc = -EBUSY;
goto exit;
}
nfcwilink_proto.priv_data = drv;
init_completion(&drv->completed);
drv->st_register_cb_status = -EINPROGRESS;
rc = st_register(&nfcwilink_proto);
if (rc < 0) {
if (rc == -EINPROGRESS) {
comp_ret = wait_for_completion_timeout(
&drv->completed,
msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
dev_dbg(&drv->pdev->dev,
"wait_for_completion_timeout returned %ld\n",
comp_ret);
if (comp_ret == 0) {
/* timeout */
rc = -ETIMEDOUT;
goto clear_exit;
} else if (drv->st_register_cb_status != 0) {
rc = drv->st_register_cb_status;
nfc_err(&drv->pdev->dev,
"st_register_cb failed %d\n", rc);
goto clear_exit;
}
} else {
nfc_err(&drv->pdev->dev, "st_register failed %d\n", rc);
goto clear_exit;
}
}
/* st_register MUST fill the write callback */
BUG_ON(nfcwilink_proto.write == NULL);
drv->st_write = nfcwilink_proto.write;
if (nfcwilink_download_fw(drv)) {
nfc_err(&drv->pdev->dev, "nfcwilink_download_fw failed %d\n",
rc);
/* open should succeed, even if the FW download failed */
}
goto exit;
clear_exit:
clear_bit(NFCWILINK_RUNNING, &drv->flags);
exit:
return rc;
}
static int nfcwilink_close(struct nci_dev *ndev)
{
struct nfcwilink *drv = nci_get_drvdata(ndev);
int rc;
if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
return 0;
rc = st_unregister(&nfcwilink_proto);
if (rc)
nfc_err(&drv->pdev->dev, "st_unregister failed %d\n", rc);
drv->st_write = NULL;
return rc;
}
static int nfcwilink_send(struct nci_dev *ndev, struct sk_buff *skb)
{
struct nfcwilink *drv = nci_get_drvdata(ndev);
struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
long len;
dev_dbg(&drv->pdev->dev, "send entry, len %d\n", skb->len);
if (!test_bit(NFCWILINK_RUNNING, &drv->flags)) {
kfree_skb(skb);
return -EINVAL;
}
/* add the ST hdr to the start of the buffer */
hdr.len = cpu_to_le16(skb->len);
memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
/* Insert skb to shared transport layer's transmit queue.
* Freeing skb memory is taken care in shared transport layer,
* so don't free skb memory here.
*/
len = drv->st_write(skb);
if (len < 0) {
kfree_skb(skb);
nfc_err(&drv->pdev->dev, "st_write failed %ld\n", len);
return -EFAULT;
}
return 0;
}
static struct nci_ops nfcwilink_ops = {
.open = nfcwilink_open,
.close = nfcwilink_close,
.send = nfcwilink_send,
};
static int nfcwilink_probe(struct platform_device *pdev)
{
static struct nfcwilink *drv;
int rc;
__u32 protocols;
drv = devm_kzalloc(&pdev->dev, sizeof(struct nfcwilink), GFP_KERNEL);
if (!drv) {
rc = -ENOMEM;
goto exit;
}
drv->pdev = pdev;
protocols = NFC_PROTO_JEWEL_MASK
| NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
| NFC_PROTO_ISO14443_MASK
| NFC_PROTO_ISO14443_B_MASK
| NFC_PROTO_NFC_DEP_MASK;
drv->ndev = nci_allocate_device(&nfcwilink_ops,
protocols,
NFCWILINK_HDR_LEN,
0);
if (!drv->ndev) {
nfc_err(&pdev->dev, "nci_allocate_device failed\n");
rc = -ENOMEM;
goto exit;
}
nci_set_parent_dev(drv->ndev, &pdev->dev);
nci_set_drvdata(drv->ndev, drv);
rc = nci_register_device(drv->ndev);
if (rc < 0) {
nfc_err(&pdev->dev, "nci_register_device failed %d\n", rc);
goto free_dev_exit;
}
dev_set_drvdata(&pdev->dev, drv);
goto exit;
free_dev_exit:
nci_free_device(drv->ndev);
exit:
return rc;
}
static int nfcwilink_remove(struct platform_device *pdev)
{
struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
struct nci_dev *ndev;
if (!drv)
return -EFAULT;
ndev = drv->ndev;
nci_unregister_device(ndev);
nci_free_device(ndev);
return 0;
}
static struct platform_driver nfcwilink_driver = {
.probe = nfcwilink_probe,
.remove = nfcwilink_remove,
.driver = {
.name = "nfcwilink",
.owner = THIS_MODULE,
},
};
module_platform_driver(nfcwilink_driver);
/* ------ Module Info ------ */
MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
MODULE_LICENSE("GPL");

3311
drivers/nfc/pn533.c Normal file

File diff suppressed because it is too large Load diff

34
drivers/nfc/pn544/Kconfig Normal file
View file

@ -0,0 +1,34 @@
config NFC_PN544
tristate "NXP PN544 NFC driver"
depends on NFC_HCI
select CRC_CCITT
default n
---help---
NXP PN544 core driver.
This is a driver based on the HCI NFC kernel layers and
will thus not work with NXP libnfc library.
To compile this driver as a module, choose m here. The module will
be called pn544.
Say N if unsure.
config NFC_PN544_I2C
tristate "NFC PN544 i2c support"
depends on NFC_PN544 && I2C && NFC_SHDLC
---help---
This module adds support for the NXP pn544 i2c interface.
Select this if your platform is using the i2c bus.
If you choose to build a module, it'll be called pn544_i2c.
Say N if unsure.
config NFC_PN544_MEI
tristate "NFC PN544 MEI support"
depends on NFC_PN544 && NFC_MEI_PHY
---help---
This module adds support for the mei interface of adapters using
NXP pn544 chipsets. Select this if your pn544 chipset
is handled by Intel's Management Engine Interface on your platform.
If you choose to build a module, it'll be called pn544_mei.
Say N if unsure.

View file

@ -0,0 +1,10 @@
#
# Makefile for PN544 HCI based NFC driver
#
pn544_i2c-objs = i2c.o
pn544_mei-objs = mei.o
obj-$(CONFIG_NFC_PN544) += pn544.o
obj-$(CONFIG_NFC_PN544_I2C) += pn544_i2c.o
obj-$(CONFIG_NFC_PN544_MEI) += pn544_mei.o

1092
drivers/nfc/pn544/i2c.c Normal file

File diff suppressed because it is too large Load diff

109
drivers/nfc/pn544/mei.c Normal file
View file

@ -0,0 +1,109 @@
/*
* HCI based Driver for NXP pn544 NFC Chip
*
* Copyright (C) 2013 Intel Corporation. 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/mod_devicetable.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "../mei_phy.h"
#include "pn544.h"
#define PN544_DRIVER_NAME "pn544"
static int pn544_mei_probe(struct mei_cl_device *device,
const struct mei_cl_device_id *id)
{
struct nfc_mei_phy *phy;
int r;
pr_info("Probing NFC pn544\n");
phy = nfc_mei_phy_alloc(device);
if (!phy) {
pr_err("Cannot allocate memory for pn544 mei phy.\n");
return -ENOMEM;
}
r = pn544_hci_probe(phy, &mei_phy_ops, LLC_NOP_NAME,
MEI_NFC_HEADER_SIZE, 0, MEI_NFC_MAX_HCI_PAYLOAD,
NULL, &phy->hdev);
if (r < 0) {
nfc_mei_phy_free(phy);
return r;
}
return 0;
}
static int pn544_mei_remove(struct mei_cl_device *device)
{
struct nfc_mei_phy *phy = mei_cl_get_drvdata(device);
pr_info("Removing pn544\n");
pn544_hci_remove(phy->hdev);
nfc_mei_phy_free(phy);
return 0;
}
static struct mei_cl_device_id pn544_mei_tbl[] = {
{ PN544_DRIVER_NAME },
/* required last entry */
{ }
};
MODULE_DEVICE_TABLE(mei, pn544_mei_tbl);
static struct mei_cl_driver pn544_driver = {
.id_table = pn544_mei_tbl,
.name = PN544_DRIVER_NAME,
.probe = pn544_mei_probe,
.remove = pn544_mei_remove,
};
static int pn544_mei_init(void)
{
int r;
pr_debug(DRIVER_DESC ": %s\n", __func__);
r = mei_cl_driver_register(&pn544_driver);
if (r) {
pr_err(PN544_DRIVER_NAME ": driver registration failed\n");
return r;
}
return 0;
}
static void pn544_mei_exit(void)
{
mei_cl_driver_unregister(&pn544_driver);
}
module_init(pn544_mei_init);
module_exit(pn544_mei_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

994
drivers/nfc/pn544/pn544.c Normal file
View file

@ -0,0 +1,994 @@
/*
* HCI based Driver for NXP PN544 NFC Chip
*
* Copyright (C) 2012 Intel Corporation. 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/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "pn544.h"
/* Timing restrictions (ms) */
#define PN544_HCI_RESETVEN_TIME 30
enum pn544_state {
PN544_ST_COLD,
PN544_ST_FW_READY,
PN544_ST_READY,
};
#define FULL_VERSION_LEN 11
/* Proprietary commands */
#define PN544_WRITE 0x3f
#define PN544_TEST_SWP 0x21
/* Proprietary gates, events, commands and registers */
/* NFC_HCI_RF_READER_A_GATE additional registers and commands */
#define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
#define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
#define PN544_MIFARE_CMD 0x21
/* Commands that apply to all RF readers */
#define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
#define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
/* NFC_HCI_ID_MGMT_GATE additional registers */
#define PN544_ID_MGMT_FULL_VERSION_SW 0x10
#define PN544_RF_READER_ISO15693_GATE 0x12
#define PN544_RF_READER_F_GATE 0x14
#define PN544_FELICA_ID 0x04
#define PN544_FELICA_RAW 0x20
#define PN544_RF_READER_JEWEL_GATE 0x15
#define PN544_JEWEL_RAW_CMD 0x23
#define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
#define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
#define PN544_SYS_MGMT_GATE 0x90
#define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
#define PN544_POLLING_LOOP_MGMT_GATE 0x94
#define PN544_DEP_MODE 0x01
#define PN544_DEP_ATR_REQ 0x02
#define PN544_DEP_ATR_RES 0x03
#define PN544_DEP_MERGE 0x0D
#define PN544_PL_RDPHASES 0x06
#define PN544_PL_EMULATION 0x07
#define PN544_PL_NFCT_DEACTIVATED 0x09
#define PN544_SWP_MGMT_GATE 0xA0
#define PN544_SWP_DEFAULT_MODE 0x01
#define PN544_NFC_WI_MGMT_GATE 0xA1
#define PN544_NFC_ESE_DEFAULT_MODE 0x01
#define PN544_HCI_EVT_SND_DATA 0x01
#define PN544_HCI_EVT_ACTIVATED 0x02
#define PN544_HCI_EVT_DEACTIVATED 0x03
#define PN544_HCI_EVT_RCV_DATA 0x04
#define PN544_HCI_EVT_CONTINUE_MI 0x05
#define PN544_HCI_EVT_SWITCH_MODE 0x03
#define PN544_HCI_CMD_ATTREQUEST 0x12
#define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
static struct nfc_hci_gate pn544_gates[] = {
{NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_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_INVALID_PIPE},
{NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
{NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
{PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
{PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
{PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
{PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
{PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
{PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
{PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
{PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
{PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
};
/* Largest headroom needed for outgoing custom commands */
#define PN544_CMDS_HEADROOM 2
struct pn544_hci_info {
struct nfc_phy_ops *phy_ops;
void *phy_id;
struct nfc_hci_dev *hdev;
enum pn544_state state;
struct mutex info_lock;
int async_cb_type;
data_exchange_cb_t async_cb;
void *async_cb_context;
fw_download_t fw_download;
};
static int pn544_hci_open(struct nfc_hci_dev *hdev)
{
struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
int r = 0;
mutex_lock(&info->info_lock);
if (info->state != PN544_ST_COLD) {
r = -EBUSY;
goto out;
}
r = info->phy_ops->enable(info->phy_id);
if (r == 0)
info->state = PN544_ST_READY;
out:
mutex_unlock(&info->info_lock);
return r;
}
static void pn544_hci_close(struct nfc_hci_dev *hdev)
{
struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
mutex_lock(&info->info_lock);
if (info->state == PN544_ST_COLD)
goto out;
info->phy_ops->disable(info->phy_id);
info->state = PN544_ST_COLD;
out:
mutex_unlock(&info->info_lock);
}
static int pn544_hci_ready(struct nfc_hci_dev *hdev)
{
struct sk_buff *skb;
static struct hw_config {
u8 adr[2];
u8 value;
} hw_config[] = {
{{0x9f, 0x9a}, 0x00},
{{0x98, 0x10}, 0xbc},
{{0x9e, 0x71}, 0x00},
{{0x98, 0x09}, 0x00},
{{0x9e, 0xb4}, 0x00},
{{0x9c, 0x01}, 0x08},
{{0x9e, 0xaa}, 0x01},
{{0x9b, 0xd1}, 0x17},
{{0x9b, 0xd2}, 0x58},
{{0x9b, 0xd3}, 0x10},
{{0x9b, 0xd4}, 0x47},
{{0x9b, 0xd5}, 0x0c},
{{0x9b, 0xd6}, 0x37},
{{0x9b, 0xdd}, 0x33},
{{0x9b, 0x84}, 0x00},
{{0x99, 0x81}, 0x79},
{{0x99, 0x31}, 0x79},
{{0x98, 0x00}, 0x3f},
{{0x9f, 0x09}, 0x02},
{{0x9f, 0x0a}, 0x05},
{{0x9e, 0xd1}, 0xa1},
{{0x99, 0x23}, 0x01},
{{0x9e, 0x74}, 0x00},
{{0x9e, 0x90}, 0x00},
{{0x9f, 0x28}, 0x10},
{{0x9f, 0x35}, 0x04},
{{0x9f, 0x36}, 0x11},
{{0x9c, 0x31}, 0x00},
{{0x9c, 0x32}, 0x00},
{{0x9c, 0x19}, 0x0a},
{{0x9c, 0x1a}, 0x0a},
{{0x9c, 0x0c}, 0x00},
{{0x9c, 0x0d}, 0x00},
{{0x9c, 0x12}, 0x00},
{{0x9c, 0x13}, 0x00},
{{0x98, 0xa2}, 0x09},
{{0x98, 0x93}, 0x00},
{{0x98, 0x7d}, 0x08},
{{0x98, 0x7e}, 0x00},
{{0x9f, 0xc8}, 0x00},
};
struct hw_config *p = hw_config;
int count = ARRAY_SIZE(hw_config);
struct sk_buff *res_skb;
u8 param[4];
int r;
param[0] = 0;
while (count--) {
param[1] = p->adr[0];
param[2] = p->adr[1];
param[3] = p->value;
r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
param, 4, &res_skb);
if (r < 0)
return r;
if (res_skb->len != 1) {
kfree_skb(res_skb);
return -EPROTO;
}
if (res_skb->data[0] != p->value) {
kfree_skb(res_skb);
return -EIO;
}
kfree_skb(res_skb);
p++;
}
param[0] = 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;
param[0] = 0x3d;
r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
if (r < 0)
return r;
param[0] = 0x0;
r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
PN544_RF_READER_A_AUTO_ACTIVATION, 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;
param[0] = 0x1;
r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
PN544_PL_NFCT_DEACTIVATED, param, 1);
if (r < 0)
return r;
param[0] = 0x0;
r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
PN544_PL_RDPHASES, param, 1);
if (r < 0)
return r;
r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
PN544_ID_MGMT_FULL_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 pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
return info->phy_ops->write(info->phy_id, skb);
}
static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
u32 im_protocols, u32 tm_protocols)
{
u8 phases = 0;
int r;
u8 duration[2];
u8 activated;
u8 i_mode = 0x3f; /* Enable all supported modes */
u8 t_mode = 0x0f;
u8 t_merge = 0x01; /* Enable merge by default */
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;
duration[0] = 0x18;
duration[1] = 0x6a;
r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
PN544_PL_EMULATION, duration, 2);
if (r < 0)
return r;
activated = 0;
r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
PN544_PL_NFCT_DEACTIVATED, &activated, 1);
if (r < 0)
return r;
if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
NFC_PROTO_JEWEL_MASK))
phases |= 1; /* Type A */
if (im_protocols & NFC_PROTO_FELICA_MASK) {
phases |= (1 << 2); /* Type F 212 */
phases |= (1 << 3); /* Type F 424 */
}
phases |= (1 << 5); /* NFC active */
r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
PN544_PL_RDPHASES, &phases, 1);
if (r < 0)
return r;
if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
&hdev->gb_len);
pr_debug("generate local bytes %p\n", hdev->gb);
if (hdev->gb == NULL || hdev->gb_len == 0) {
im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
}
}
if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_send_event(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
if (r < 0)
return r;
r = nfc_hci_set_param(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
PN544_DEP_MODE, &i_mode, 1);
if (r < 0)
return r;
r = nfc_hci_set_param(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
if (r < 0)
return r;
r = nfc_hci_send_event(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
if (r < 0)
nfc_hci_send_event(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
}
if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
PN544_DEP_MODE, &t_mode, 1);
if (r < 0)
return r;
r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
if (r < 0)
return r;
r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
PN544_DEP_MERGE, &t_merge, 1);
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 pn544_hci_dep_link_up(struct nfc_hci_dev *hdev,
struct nfc_target *target, u8 comm_mode,
u8 *gb, size_t gb_len)
{
struct sk_buff *rgb_skb = NULL;
int r;
r = nfc_hci_get_param(hdev, target->hci_reader_gate,
PN544_DEP_ATR_RES, &rgb_skb);
if (r < 0)
return r;
if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
r = -EPROTO;
goto exit;
}
print_hex_dump(KERN_DEBUG, "remote gb: ", DUMP_PREFIX_OFFSET,
16, 1, rgb_skb->data, rgb_skb->len, true);
r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
rgb_skb->len);
if (r == 0)
r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
NFC_RF_INITIATOR);
exit:
kfree_skb(rgb_skb);
return r;
}
static int pn544_hci_dep_link_down(struct nfc_hci_dev *hdev)
{
return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_INITIATOR_GATE,
NFC_HCI_EVT_END_OPERATION, NULL, 0);
}
static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
struct nfc_target *target)
{
switch (gate) {
case PN544_RF_READER_F_GATE:
target->supported_protocols = NFC_PROTO_FELICA_MASK;
break;
case PN544_RF_READER_JEWEL_GATE:
target->supported_protocols = NFC_PROTO_JEWEL_MASK;
target->sens_res = 0x0c00;
break;
case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
break;
default:
return -EPROTO;
}
return 0;
}
static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
u8 gate,
struct nfc_target *target)
{
struct sk_buff *uid_skb;
int r = 0;
if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
return r;
if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_send_cmd(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL);
if (r < 0)
return r;
target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
} else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
target->nfcid1_len != 10)
return -EPROTO;
r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
PN544_RF_READER_CMD_ACTIVATE_NEXT,
target->nfcid1, target->nfcid1_len, NULL);
} else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
PN544_FELICA_ID, &uid_skb);
if (r < 0)
return r;
if (uid_skb->len != 8) {
kfree_skb(uid_skb);
return -EPROTO;
}
/* Type F NFC-DEP IDm has prefix 0x01FE */
if ((uid_skb->data[0] == 0x01) && (uid_skb->data[1] == 0xfe)) {
kfree_skb(uid_skb);
r = nfc_hci_send_cmd(hdev,
PN544_RF_READER_NFCIP1_INITIATOR_GATE,
PN544_HCI_CMD_CONTINUE_ACTIVATION,
NULL, 0, NULL);
if (r < 0)
return r;
target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
target->hci_reader_gate =
PN544_RF_READER_NFCIP1_INITIATOR_GATE;
} else {
r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
PN544_RF_READER_CMD_ACTIVATE_NEXT,
uid_skb->data, uid_skb->len, NULL);
kfree_skb(uid_skb);
}
} else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
/*
* TODO: maybe other ISO 14443 require some kind of continue
* activation, but for now we've seen only this one below.
*/
if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
NULL, 0, NULL);
}
return r;
}
#define PN544_CB_TYPE_READER_F 1
static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
int err)
{
struct pn544_hci_info *info = context;
switch (info->async_cb_type) {
case PN544_CB_TYPE_READER_F:
if (err == 0)
skb_pull(skb, 1);
info->async_cb(info->async_cb_context, skb, err);
break;
default:
if (err == 0)
kfree_skb(skb);
break;
}
}
#define MIFARE_CMD_AUTH_KEY_A 0x60
#define MIFARE_CMD_AUTH_KEY_B 0x61
#define MIFARE_CMD_HEADER 2
#define MIFARE_UID_LEN 4
#define MIFARE_KEY_LEN 6
#define MIFARE_CMD_LEN 12
/*
* Returns:
* <= 0: driver handled the data exchange
* 1: driver doesn't especially handle, please do standard processing
*/
static int pn544_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 pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
target->hci_reader_gate);
switch (target->hci_reader_gate) {
case NFC_HCI_RF_READER_A_GATE:
if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
/*
* It seems that pn544 is inverting key and UID for
* MIFARE authentication commands.
*/
if (skb->len == MIFARE_CMD_LEN &&
(skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
u8 uid[MIFARE_UID_LEN];
u8 *data = skb->data + MIFARE_CMD_HEADER;
memcpy(uid, data + MIFARE_KEY_LEN,
MIFARE_UID_LEN);
memmove(data + MIFARE_UID_LEN, data,
MIFARE_KEY_LEN);
memcpy(data, uid, MIFARE_UID_LEN);
}
return nfc_hci_send_cmd_async(hdev,
target->hci_reader_gate,
PN544_MIFARE_CMD,
skb->data, skb->len,
cb, cb_context);
} else
return 1;
case PN544_RF_READER_F_GATE:
*skb_push(skb, 1) = 0;
*skb_push(skb, 1) = 0;
info->async_cb_type = PN544_CB_TYPE_READER_F;
info->async_cb = cb;
info->async_cb_context = cb_context;
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
PN544_FELICA_RAW, skb->data,
skb->len,
pn544_hci_data_exchange_cb, info);
case PN544_RF_READER_JEWEL_GATE:
return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
PN544_JEWEL_RAW_CMD, skb->data,
skb->len, cb, cb_context);
case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
*skb_push(skb, 1) = 0;
return nfc_hci_send_event(hdev, target->hci_reader_gate,
PN544_HCI_EVT_SND_DATA, skb->data,
skb->len);
default:
return 1;
}
}
static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
int r;
/* Set default false for multiple information chaining */
*skb_push(skb, 1) = 0;
r = nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
kfree_skb(skb);
return r;
}
static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
struct nfc_target *target)
{
pr_debug("supported protocol %d\b", target->supported_protocols);
if (target->supported_protocols & (NFC_PROTO_ISO14443_MASK |
NFC_PROTO_ISO14443_B_MASK)) {
return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
PN544_RF_READER_CMD_PRESENCE_CHECK,
NULL, 0, NULL);
} else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
target->nfcid1_len != 10)
return -EOPNOTSUPP;
return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
PN544_RF_READER_CMD_ACTIVATE_NEXT,
target->nfcid1, target->nfcid1_len, NULL);
} else if (target->supported_protocols & (NFC_PROTO_JEWEL_MASK |
NFC_PROTO_FELICA_MASK)) {
return -EOPNOTSUPP;
} else if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
PN544_HCI_CMD_ATTREQUEST,
NULL, 0, NULL);
}
return 0;
}
/*
* Returns:
* <= 0: driver handled the event, skb consumed
* 1: driver does not handle the event, please do standard processing
*/
static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
struct sk_buff *skb)
{
struct sk_buff *rgb_skb = NULL;
int r;
pr_debug("hci event %d\n", event);
switch (event) {
case PN544_HCI_EVT_ACTIVATED:
if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) {
r = nfc_hci_target_discovered(hdev, gate);
} else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
&rgb_skb);
if (r < 0)
goto exit;
r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
NFC_COMM_PASSIVE, rgb_skb->data,
rgb_skb->len);
kfree_skb(rgb_skb);
} else {
r = -EINVAL;
}
break;
case PN544_HCI_EVT_DEACTIVATED:
r = nfc_hci_send_event(hdev, gate, NFC_HCI_EVT_END_OPERATION,
NULL, 0);
break;
case PN544_HCI_EVT_RCV_DATA:
if (skb->len < 2) {
r = -EPROTO;
goto exit;
}
if (skb->data[0] != 0) {
pr_debug("data0 %d\n", skb->data[0]);
r = -EPROTO;
goto exit;
}
skb_pull(skb, 2);
return nfc_tm_data_received(hdev->ndev, skb);
default:
return 1;
}
exit:
kfree_skb(skb);
return r;
}
static int pn544_hci_fw_download(struct nfc_hci_dev *hdev,
const char *firmware_name)
{
struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
if (info->fw_download == NULL)
return -ENOTSUPP;
return info->fw_download(info->phy_id, firmware_name, hdev->sw_romlib);
}
static int pn544_hci_discover_se(struct nfc_hci_dev *hdev)
{
u32 se_idx = 0;
u8 ese_mode = 0x01; /* Default mode */
struct sk_buff *res_skb;
int r;
r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_TEST_SWP,
NULL, 0, &res_skb);
if (r == 0) {
if (res_skb->len == 2 && res_skb->data[0] == 0x00)
nfc_add_se(hdev->ndev, se_idx++, NFC_SE_UICC);
kfree_skb(res_skb);
}
r = nfc_hci_send_event(hdev, PN544_NFC_WI_MGMT_GATE,
PN544_HCI_EVT_SWITCH_MODE,
&ese_mode, 1);
if (r == 0)
nfc_add_se(hdev->ndev, se_idx++, NFC_SE_EMBEDDED);
return !se_idx;
}
#define PN544_SE_MODE_OFF 0x00
#define PN544_SE_MODE_ON 0x01
static int pn544_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx)
{
struct nfc_se *se;
u8 enable = PN544_SE_MODE_ON;
static struct uicc_gatelist {
u8 head;
u8 adr[2];
u8 value;
} uicc_gatelist[] = {
{0x00, {0x9e, 0xd9}, 0x23},
{0x00, {0x9e, 0xda}, 0x21},
{0x00, {0x9e, 0xdb}, 0x22},
{0x00, {0x9e, 0xdc}, 0x24},
};
struct uicc_gatelist *p = uicc_gatelist;
int count = ARRAY_SIZE(uicc_gatelist);
struct sk_buff *res_skb;
int r;
se = nfc_find_se(hdev->ndev, se_idx);
switch (se->type) {
case NFC_SE_UICC:
while (count--) {
r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE,
PN544_WRITE, (u8 *)p, 4, &res_skb);
if (r < 0)
return r;
if (res_skb->len != 1) {
kfree_skb(res_skb);
return -EPROTO;
}
if (res_skb->data[0] != p->value) {
kfree_skb(res_skb);
return -EIO;
}
kfree_skb(res_skb);
p++;
}
return nfc_hci_set_param(hdev, PN544_SWP_MGMT_GATE,
PN544_SWP_DEFAULT_MODE, &enable, 1);
case NFC_SE_EMBEDDED:
return nfc_hci_set_param(hdev, PN544_NFC_WI_MGMT_GATE,
PN544_NFC_ESE_DEFAULT_MODE, &enable, 1);
default:
return -EINVAL;
}
}
static int pn544_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx)
{
struct nfc_se *se;
u8 disable = PN544_SE_MODE_OFF;
se = nfc_find_se(hdev->ndev, se_idx);
switch (se->type) {
case NFC_SE_UICC:
return nfc_hci_set_param(hdev, PN544_SWP_MGMT_GATE,
PN544_SWP_DEFAULT_MODE, &disable, 1);
case NFC_SE_EMBEDDED:
return nfc_hci_set_param(hdev, PN544_NFC_WI_MGMT_GATE,
PN544_NFC_ESE_DEFAULT_MODE, &disable, 1);
default:
return -EINVAL;
}
}
static struct nfc_hci_ops pn544_hci_ops = {
.open = pn544_hci_open,
.close = pn544_hci_close,
.hci_ready = pn544_hci_ready,
.xmit = pn544_hci_xmit,
.start_poll = pn544_hci_start_poll,
.dep_link_up = pn544_hci_dep_link_up,
.dep_link_down = pn544_hci_dep_link_down,
.target_from_gate = pn544_hci_target_from_gate,
.complete_target_discovered = pn544_hci_complete_target_discovered,
.im_transceive = pn544_hci_im_transceive,
.tm_send = pn544_hci_tm_send,
.check_presence = pn544_hci_check_presence,
.event_received = pn544_hci_event_received,
.fw_download = pn544_hci_fw_download,
.discover_se = pn544_hci_discover_se,
.enable_se = pn544_hci_enable_se,
.disable_se = pn544_hci_disable_se,
};
int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
int phy_headroom, int phy_tailroom, int phy_payload,
fw_download_t fw_download, struct nfc_hci_dev **hdev)
{
struct pn544_hci_info *info;
u32 protocols;
struct nfc_hci_init_data init_data;
int r;
info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
if (!info) {
r = -ENOMEM;
goto err_info_alloc;
}
info->phy_ops = phy_ops;
info->phy_id = phy_id;
info->fw_download = fw_download;
info->state = PN544_ST_COLD;
mutex_init(&info->info_lock);
init_data.gate_count = ARRAY_SIZE(pn544_gates);
memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
/*
* TODO: Session id must include the driver name + some bus addr
* persistent info to discriminate 2 identical chips
*/
strcpy(init_data.session_id, "ID544HCI");
protocols = NFC_PROTO_JEWEL_MASK |
NFC_PROTO_MIFARE_MASK |
NFC_PROTO_FELICA_MASK |
NFC_PROTO_ISO14443_MASK |
NFC_PROTO_ISO14443_B_MASK |
NFC_PROTO_NFC_DEP_MASK;
info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 0,
protocols, llc_name,
phy_headroom + PN544_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);
err_info_alloc:
return r;
}
EXPORT_SYMBOL(pn544_hci_probe);
void pn544_hci_remove(struct nfc_hci_dev *hdev)
{
struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
nfc_hci_unregister_device(hdev);
nfc_hci_free_device(hdev);
kfree(info);
}
EXPORT_SYMBOL(pn544_hci_remove);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

36
drivers/nfc/pn544/pn544.h Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright (C) 2011 - 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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_PN544_H_
#define __LOCAL_PN544_H_
#include <net/nfc/hci.h>
#define DRIVER_DESC "HCI NFC driver for PN544"
#define PN544_HCI_MODE 0
#define PN544_FW_MODE 1
typedef int (*fw_download_t)(void *context, const char *firmware_name,
u8 hw_variant);
int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
int phy_headroom, int phy_tailroom, int phy_payload,
fw_download_t fw_download, struct nfc_hci_dev **hdev);
void pn544_hci_remove(struct nfc_hci_dev *hdev);
#endif /* __LOCAL_PN544_H_ */

1579
drivers/nfc/port100.c Normal file

File diff suppressed because it is too large Load diff

1212
drivers/nfc/sec_nfc.c Executable file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,23 @@
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.

View file

@ -0,0 +1,9 @@
#
# Makefile for ST21NFCA HCI based NFC driver
#
st21nfca_hci-objs = st21nfca.o st21nfca_dep.o
obj-$(CONFIG_NFC_ST21NFCA) += st21nfca_hci.o
st21nfca_i2c-objs = i2c.o
obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o

709
drivers/nfc/st21nfca/i2c.c Normal file
View file

@ -0,0 +1,709 @@
/*
* 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[] = { 16, 24, 12, 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 && !IS_START_OF_FRAME(buf)) {
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;
} else {
kfree_skb(phy->pending_skb);
}
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_one(&client->dev, gpio, GPIOF_OUT_INIT_HIGH,
"clf_enable");
if (r) {
nfc_err(&client->dev, "Failed to request enable pin\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_one(&client->dev, phy->gpio_irq, GPIOF_IN,
"wake_up");
if (r) {
pr_err("%s : gpio_request failed\n", __FILE__);
return -ENODEV;
}
if (phy->gpio_ena > 0) {
r = devm_gpio_request_one(&client->dev, phy->gpio_ena,
GPIOF_OUT_INIT_HIGH, "clf_enable");
if (r) {
pr_err("%s : ena gpio_request 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,
.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);

View file

@ -0,0 +1,989 @@
/*
* 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"
#include "st21nfca_dep.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_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
#define ST21NFCA_RF_READER_F_POL_REQ 0x02
#define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
#define ST21NFCA_RF_READER_F_NFCID2 0x03
#define ST21NFCA_RF_READER_F_NFCID1 0x04
#define ST21NFCA_RF_CARD_F_MODE 0x01
#define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
#define ST21NFCA_RF_CARD_F_NFCID1 0x05
#define ST21NFCA_RF_CARD_F_SENS_RES 0x06
#define ST21NFCA_RF_CARD_F_SEL_RES 0x07
#define ST21NFCA_RF_CARD_F_DATARATE 0x08
#define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
#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_DISCONNECT 0x30
#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*/
#define ST21NFCA_EVT_FIELD_ON 0x11
#define ST21NFCA_EVT_CARD_DEACTIVATED 0x12
#define ST21NFCA_EVT_CARD_ACTIVATED 0x13
#define ST21NFCA_EVT_FIELD_OFF 0x14
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},
{ST21NFCA_RF_CARD_F_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;
u32 pol_req;
u8 param[19];
struct sk_buff *datarate_skb;
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;
} else {
hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
&hdev->gb_len);
if (hdev->gb == NULL || hdev->gb_len == 0) {
im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
}
param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
ST21NFCA_RF_READER_F_DATARATE_212 |
ST21NFCA_RF_READER_F_DATARATE_424;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_RF_READER_F_DATARATE,
param, 1);
if (r < 0)
return r;
pol_req = be32_to_cpu((__force __be32)
ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_RF_READER_F_POL_REQ,
(u8 *) &pol_req, 4);
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);
}
if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_DATARATE,
&datarate_skb);
if (r < 0)
return r;
/* Configure the maximum supported datarate to 424Kbps */
if (datarate_skb->len > 0 &&
datarate_skb->data[0] !=
ST21NFCA_RF_CARD_F_DATARATE_212_424) {
param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_DATARATE,
param, 1);
if (r < 0)
return r;
}
/*
* Configure sens_res
*
* NFC Forum Digital Spec Table 7:
* NFCID1 size: triple (10 bytes)
*/
param[0] = 0x00;
param[1] = 0x08;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
if (r < 0)
return r;
/*
* Configure sel_res
*
* NFC Forum Digistal Spec Table 17:
* b3 set to 0b (value b7-b6):
* - 10b: Configured for NFC-DEP Protocol
*/
param[0] = 0x40;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
if (r < 0)
return r;
/* Configure NFCID1 Random uid */
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
if (r < 0)
return r;
/* Configure NFCID2_LIST */
/* System Code */
param[0] = 0x00;
param[1] = 0x00;
/* NFCID2 */
param[2] = 0x01;
param[3] = 0xfe;
param[4] = 'S';
param[5] = 'T';
param[6] = 'M';
param[7] = 'i';
param[8] = 'c';
param[9] = 'r';
/* 8 byte Pad bytes used for polling respone frame */
/*
* Configuration byte:
* - bit 0: define the default NFCID2 entry used when the
* system code is equal to 'FFFF'
* - bit 1: use a random value for lowest 6 bytes of
* NFCID2 value
* - bit 2: ignore polling request frame if request code
* is equal to '01'
* - Other bits are RFU
*/
param[18] = 0x01;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
19);
if (r < 0)
return r;
param[0] = 0x02;
r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_RF_CARD_F_MODE, param, 1);
}
return r;
}
static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
{
nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
}
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_dep_link_up(struct nfc_hci_dev *hdev,
struct nfc_target *target, u8 comm_mode,
u8 *gb, size_t gb_len)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
info->dep_info.idx = target->idx;
return st21nfca_im_send_atr_req(hdev, gb, gb_len);
}
static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
info->state = ST21NFCA_ST_READY;
return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
}
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;
}
static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
u8 gate,
struct nfc_target *target)
{
int r;
struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
if (gate == ST21NFCA_RF_READER_F_GATE) {
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
if (r < 0)
goto exit;
if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
r = -EPROTO;
goto exit;
}
/*
* - After the recepton of polling response for type F frame
* at 212 or 424 Kbit/s, NFCID2 registry parameters will be
* updated.
* - After the reception of SEL_RES with NFCIP-1 compliant bit
* set for type A frame NFCID1 will be updated
*/
if (nfcid2_skb->len > 0) {
/* P2P in type F */
memcpy(target->sensf_res, nfcid2_skb->data,
nfcid2_skb->len);
target->sensf_res_len = nfcid2_skb->len;
/* NFC Forum Digital Protocol Table 44 */
if (target->sensf_res[0] == 0x01 &&
target->sensf_res[1] == 0xfe)
target->supported_protocols =
NFC_PROTO_NFC_DEP_MASK;
else
target->supported_protocols =
NFC_PROTO_FELICA_MASK;
} else {
/* P2P in type A */
r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_RF_READER_F_NFCID1,
&nfcid1_skb);
if (r < 0)
goto exit;
if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
r = -EPROTO;
goto exit;
}
memcpy(target->sensf_res, nfcid1_skb->data,
nfcid1_skb->len);
target->sensf_res_len = nfcid1_skb->len;
target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
}
target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
}
r = 1;
exit:
kfree_skb(nfcid2_skb);
return r;
}
#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:
if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
return st21nfca_im_send_dep_req(hdev, skb);
*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_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
return st21nfca_tm_send_dep_res(hdev, skb);
}
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;
}
}
/*
* Returns:
* <= 0: driver handled the event, skb consumed
* 1: driver does not handle the event, please do standard processing
*/
static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
u8 event, struct sk_buff *skb)
{
int r;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
pr_debug("hci event: %d\n", event);
switch (event) {
case ST21NFCA_EVT_CARD_ACTIVATED:
if (gate == ST21NFCA_RF_CARD_F_GATE)
info->dep_info.curr_nfc_dep_pni = 0;
break;
case ST21NFCA_EVT_CARD_DEACTIVATED:
break;
case ST21NFCA_EVT_FIELD_ON:
break;
case ST21NFCA_EVT_FIELD_OFF:
break;
case ST21NFCA_EVT_SEND_DATA:
if (gate == ST21NFCA_RF_CARD_F_GATE) {
r = st21nfca_tm_event_send_data(hdev, skb, gate);
if (r < 0)
return r;
return 0;
}
info->dep_info.curr_nfc_dep_pni = 0;
return 1;
default:
return 1;
}
kfree_skb(skb);
return 0;
}
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,
.stop_poll = st21nfca_hci_stop_poll,
.dep_link_up = st21nfca_hci_dep_link_up,
.dep_link_down = st21nfca_hci_dep_link_down,
.target_from_gate = st21nfca_hci_target_from_gate,
.complete_target_discovered = st21nfca_hci_complete_target_discovered,
.im_transceive = st21nfca_hci_im_transceive,
.tm_send = st21nfca_hci_tm_send,
.check_presence = st21nfca_hci_check_presence,
.event_received = st21nfca_hci_event_received,
};
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)
return -ENODEV;
set_bit(dev_num, dev_mask);
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 |
NFC_PROTO_NFC_DEP_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;
st21nfca_dep_init(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);
st21nfca_dep_deinit(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);

View file

@ -0,0 +1,90 @@
/*
* 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>
#include "st21nfca_dep.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;
struct st21nfca_dep_info dep_info;
};
/* Reader RF commands */
#define ST21NFCA_WR_XCHG_DATA 0x10
#define ST21NFCA_RF_READER_F_GATE 0x14
#define ST21NFCA_RF_CARD_F_GATE 0x24
#define ST21NFCA_EVT_SEND_DATA 0x10
#endif /* __LOCAL_ST21NFCA_H_ */

View file

@ -0,0 +1,648 @@
/*
* 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 <net/nfc/hci.h>
#include "st21nfca.h"
#include "st21nfca_dep.h"
#define ST21NFCA_NFCIP1_INITIATOR 0x00
#define ST21NFCA_NFCIP1_REQ 0xd4
#define ST21NFCA_NFCIP1_RES 0xd5
#define ST21NFCA_NFCIP1_ATR_REQ 0x00
#define ST21NFCA_NFCIP1_ATR_RES 0x01
#define ST21NFCA_NFCIP1_PSL_REQ 0x04
#define ST21NFCA_NFCIP1_PSL_RES 0x05
#define ST21NFCA_NFCIP1_DEP_REQ 0x06
#define ST21NFCA_NFCIP1_DEP_RES 0x07
#define ST21NFCA_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
#define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
#define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
#define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
#define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
#define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
#define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
#define ST21NFCA_NFC_DEP_PFB_I_PDU 0x00
#define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU 0x40
#define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
#define ST21NFCA_ATR_REQ_MIN_SIZE 17
#define ST21NFCA_ATR_REQ_MAX_SIZE 65
#define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
#define ST21NFCA_GB_BIT 0x02
#define ST21NFCA_EVT_CARD_F_BITRATE 0x16
#define ST21NFCA_EVT_READER_F_BITRATE 0x13
#define ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
#define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
#define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
#define ST21NFCA_CARD_BITRATE_212 0x01
#define ST21NFCA_CARD_BITRATE_424 0x02
#define ST21NFCA_DEFAULT_TIMEOUT 0x0a
#define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
__LINE__, req)
struct st21nfca_atr_req {
u8 length;
u8 cmd0;
u8 cmd1;
u8 nfcid3[NFC_NFCID3_MAXSIZE];
u8 did;
u8 bsi;
u8 bri;
u8 ppi;
u8 gbi[0];
} __packed;
struct st21nfca_atr_res {
u8 length;
u8 cmd0;
u8 cmd1;
u8 nfcid3[NFC_NFCID3_MAXSIZE];
u8 did;
u8 bsi;
u8 bri;
u8 to;
u8 ppi;
u8 gbi[0];
} __packed;
struct st21nfca_psl_req {
u8 length;
u8 cmd0;
u8 cmd1;
u8 did;
u8 brs;
u8 fsl;
} __packed;
struct st21nfca_psl_res {
u8 length;
u8 cmd0;
u8 cmd1;
u8 did;
} __packed;
struct st21nfca_dep_req_res {
u8 length;
u8 cmd0;
u8 cmd1;
u8 pfb;
u8 did;
u8 nad;
} __packed;
static void st21nfca_tx_work(struct work_struct *work)
{
struct st21nfca_hci_info *info = container_of(work,
struct st21nfca_hci_info,
dep_info.tx_work);
struct nfc_dev *dev;
struct sk_buff *skb;
if (info) {
dev = info->hdev->ndev;
skb = info->dep_info.tx_pending;
device_lock(&dev->dev);
nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
info->async_cb, info);
device_unlock(&dev->dev);
kfree_skb(skb);
}
}
static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info,
struct sk_buff *skb)
{
info->dep_info.tx_pending = skb;
schedule_work(&info->dep_info.tx_work);
}
static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev,
struct st21nfca_atr_req *atr_req)
{
struct st21nfca_atr_res *atr_res;
struct sk_buff *skb;
size_t gb_len;
int r;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
gb_len = atr_req->length - sizeof(struct st21nfca_atr_req);
skb = alloc_skb(atr_req->length + 1, GFP_KERNEL);
if (!skb)
return -ENOMEM;
skb_put(skb, sizeof(struct st21nfca_atr_res));
atr_res = (struct st21nfca_atr_res *)skb->data;
memset(atr_res, 0, sizeof(struct st21nfca_atr_res));
atr_res->length = atr_req->length + 1;
atr_res->cmd0 = ST21NFCA_NFCIP1_RES;
atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES;
memcpy(atr_res->nfcid3, atr_req->nfcid3, 6);
atr_res->bsi = 0x00;
atr_res->bri = 0x00;
atr_res->to = ST21NFCA_DEFAULT_TIMEOUT;
atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
if (gb_len) {
skb_put(skb, gb_len);
atr_res->ppi |= ST21NFCA_GB_BIT;
memcpy(atr_res->gbi, atr_req->gbi, gb_len);
r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi,
gb_len);
if (r < 0)
return r;
}
info->dep_info.curr_nfc_dep_pni = 0;
r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
kfree_skb(skb);
return r;
}
static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
struct sk_buff *skb)
{
struct st21nfca_atr_req *atr_req;
size_t gb_len;
int r;
skb_trim(skb, skb->len - 1);
if (!skb->len) {
r = -EIO;
goto exit;
}
if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
r = -EPROTO;
goto exit;
}
atr_req = (struct st21nfca_atr_req *)skb->data;
if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
r = -EPROTO;
goto exit;
}
r = st21nfca_tm_send_atr_res(hdev, atr_req);
if (r)
goto exit;
gb_len = skb->len - sizeof(struct st21nfca_atr_req);
r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
if (r)
goto exit;
r = 0;
exit:
return r;
}
static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
struct st21nfca_psl_req *psl_req)
{
struct st21nfca_psl_res *psl_res;
struct sk_buff *skb;
u8 bitrate[2] = {0, 0};
int r;
skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
if (!skb)
return -ENOMEM;
skb_put(skb, sizeof(struct st21nfca_psl_res));
psl_res = (struct st21nfca_psl_res *)skb->data;
psl_res->length = sizeof(struct st21nfca_psl_res);
psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
psl_res->did = psl_req->did;
r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
if (r < 0)
goto error;
/*
* ST21NFCA only support P2P passive.
* PSL_REQ BRS value != 0 has only a meaning to
* change technology to type F.
* We change to BITRATE 424Kbits.
* In other case switch to BITRATE 106Kbits.
*/
if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
bitrate[0] = ST21NFCA_CARD_BITRATE_424;
bitrate[1] = ST21NFCA_CARD_BITRATE_424;
}
/* Send an event to change bitrate change event to card f */
r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
error:
kfree_skb(skb);
return r;
}
static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
struct sk_buff *skb)
{
struct st21nfca_psl_req *psl_req;
int r;
skb_trim(skb, skb->len - 1);
if (!skb->len) {
r = -EIO;
goto exit;
}
psl_req = (struct st21nfca_psl_req *)skb->data;
if (skb->len < sizeof(struct st21nfca_psl_req)) {
r = -EIO;
goto exit;
}
r = st21nfca_tm_send_psl_res(hdev, psl_req);
exit:
return r;
}
int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
int r;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
*skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
*skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
*skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
*skb_push(skb, 1) = skb->len;
r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
kfree_skb(skb);
return r;
}
EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
struct sk_buff *skb)
{
struct st21nfca_dep_req_res *dep_req;
u8 size;
int r;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
skb_trim(skb, skb->len - 1);
size = 4;
dep_req = (struct st21nfca_dep_req_res *)skb->data;
if (skb->len < size) {
r = -EIO;
goto exit;
}
if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
size++;
if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
size++;
if (skb->len < size) {
r = -EIO;
goto exit;
}
/* Receiving DEP_REQ - Decoding */
switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
case ST21NFCA_NFC_DEP_PFB_I_PDU:
info->dep_info.curr_nfc_dep_pni =
ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
break;
case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
pr_err("Received a ACK/NACK PDU\n");
break;
case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
pr_err("Received a SUPERVISOR PDU\n");
break;
}
skb_pull(skb, size);
return nfc_tm_data_received(hdev->ndev, skb);
exit:
return r;
}
int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev, struct sk_buff *skb,
u8 gate)
{
u8 cmd0, cmd1;
int r;
cmd0 = skb->data[1];
switch (cmd0) {
case ST21NFCA_NFCIP1_REQ:
cmd1 = skb->data[2];
switch (cmd1) {
case ST21NFCA_NFCIP1_ATR_REQ:
r = st21nfca_tm_recv_atr_req(hdev, skb);
break;
case ST21NFCA_NFCIP1_PSL_REQ:
r = st21nfca_tm_recv_psl_req(hdev, skb);
break;
case ST21NFCA_NFCIP1_DEP_REQ:
r = st21nfca_tm_recv_dep_req(hdev, skb);
break;
default:
return 1;
}
default:
return 1;
}
return r;
}
EXPORT_SYMBOL(st21nfca_tm_event_send_data);
static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
u8 bri, u8 lri)
{
struct sk_buff *skb;
struct st21nfca_psl_req *psl_req;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
skb =
alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
if (!skb)
return;
skb_reserve(skb, 1);
skb_put(skb, sizeof(struct st21nfca_psl_req));
psl_req = (struct st21nfca_psl_req *) skb->data;
psl_req->length = sizeof(struct st21nfca_psl_req);
psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
psl_req->did = did;
psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
psl_req->fsl = lri;
*skb_push(skb, 1) = info->dep_info.to | 0x10;
st21nfca_im_send_pdu(info, skb);
}
#define ST21NFCA_CB_TYPE_READER_F 1
static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
int err)
{
struct st21nfca_hci_info *info = context;
struct st21nfca_atr_res *atr_res;
int r;
if (err != 0)
return;
if (!skb)
return;
switch (info->async_cb_type) {
case ST21NFCA_CB_TYPE_READER_F:
skb_trim(skb, skb->len - 1);
atr_res = (struct st21nfca_atr_res *)skb->data;
r = nfc_set_remote_general_bytes(info->hdev->ndev,
atr_res->gbi,
skb->len - sizeof(struct st21nfca_atr_res));
if (r < 0)
return;
if (atr_res->to >= 0x0e)
info->dep_info.to = 0x0e;
else
info->dep_info.to = atr_res->to + 1;
info->dep_info.to |= 0x10;
r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
if (r < 0)
return;
info->dep_info.curr_nfc_dep_pni = 0;
if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
st21nfca_im_send_psl_req(info->hdev, atr_res->did,
atr_res->bsi, atr_res->bri,
ST21NFCA_PP2LRI(atr_res->ppi));
break;
default:
kfree_skb(skb);
break;
}
}
int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
{
struct sk_buff *skb;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
struct st21nfca_atr_req *atr_req;
struct nfc_target *target;
uint size;
info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
PROTOCOL_ERR("14.6.1.1");
return -EINVAL;
}
skb =
alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
if (!skb)
return -ENOMEM;
skb_reserve(skb, 1);
skb_put(skb, sizeof(struct st21nfca_atr_req));
atr_req = (struct st21nfca_atr_req *)skb->data;
memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
target = hdev->ndev->targets;
if (target->sensf_res_len > 0)
memcpy(atr_req->nfcid3, target->sensf_res,
target->sensf_res_len);
else
get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
atr_req->did = 0x0;
atr_req->bsi = 0x00;
atr_req->bri = 0x00;
atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
if (gb_len) {
atr_req->ppi |= ST21NFCA_GB_BIT;
memcpy(skb_put(skb, gb_len), gb, gb_len);
}
atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
*skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
info->async_cb_context = info;
info->async_cb = st21nfca_im_recv_atr_res_cb;
info->dep_info.bri = atr_req->bri;
info->dep_info.bsi = atr_req->bsi;
info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_WR_XCHG_DATA, skb->data,
skb->len, info->async_cb, info);
}
EXPORT_SYMBOL(st21nfca_im_send_atr_req);
static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
int err)
{
struct st21nfca_hci_info *info = context;
struct st21nfca_dep_req_res *dep_res;
int size;
if (err != 0)
return;
if (!skb)
return;
switch (info->async_cb_type) {
case ST21NFCA_CB_TYPE_READER_F:
dep_res = (struct st21nfca_dep_req_res *)skb->data;
size = 3;
if (skb->len < size)
goto exit;
if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
size++;
if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
size++;
if (skb->len < size)
goto exit;
skb_trim(skb, skb->len - 1);
/* Receiving DEP_REQ - Decoding */
switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
pr_err("Received a ACK/NACK PDU\n");
case ST21NFCA_NFC_DEP_PFB_I_PDU:
info->dep_info.curr_nfc_dep_pni =
ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
size++;
skb_pull(skb, size);
nfc_tm_data_received(info->hdev->ndev, skb);
break;
case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
pr_err("Received a SUPERVISOR PDU\n");
skb_pull(skb, size);
*skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
*skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
*skb_push(skb, 1) = skb->len;
*skb_push(skb, 1) = info->dep_info.to | 0x10;
st21nfca_im_send_pdu(info, skb);
break;
}
return;
default:
break;
}
exit:
kfree_skb(skb);
}
int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
info->async_cb_context = info;
info->async_cb = st21nfca_im_recv_dep_res_cb;
*skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
*skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
*skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
*skb_push(skb, 1) = skb->len;
*skb_push(skb, 1) = info->dep_info.to | 0x10;
return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
ST21NFCA_WR_XCHG_DATA,
skb->data, skb->len,
info->async_cb, info);
}
EXPORT_SYMBOL(st21nfca_im_send_dep_req);
void st21nfca_dep_init(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
info->dep_info.curr_nfc_dep_pni = 0;
info->dep_info.idx = 0;
info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
}
EXPORT_SYMBOL(st21nfca_dep_init);
void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
cancel_work_sync(&info->dep_info.tx_work);
}
EXPORT_SYMBOL(st21nfca_dep_deinit);

View file

@ -0,0 +1,43 @@
/*
* 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_DEP_H
#define __ST21NFCA_DEP_H
#include <linux/skbuff.h>
#include <linux/workqueue.h>
struct st21nfca_dep_info {
struct sk_buff *tx_pending;
struct work_struct tx_work;
u8 curr_nfc_dep_pni;
u32 idx;
u8 to;
u8 did;
u8 bsi;
u8 bri;
u8 lri;
} __packed;
int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev, struct sk_buff *skb,
u8 gate);
int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb);
int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len);
int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb);
void st21nfca_dep_init(struct nfc_hci_dev *hdev);
void st21nfca_dep_deinit(struct nfc_hci_dev *hdev);
#endif /* __ST21NFCA_DEP_H */

View file

@ -0,0 +1,22 @@
config NFC_ST21NFCB
tristate "STMicroelectronics ST21NFCB NFC driver"
depends on NFC_NCI
default n
---help---
STMicroelectronics ST21NFCB core driver. It implements the chipset
NCI 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 st21nfcb.
Say N if unsure.
config NFC_ST21NFCB_I2C
tristate "NFC ST21NFCB i2c support"
depends on NFC_ST21NFCB && I2C
---help---
This module adds support for the STMicroelectronics st21nfcb i2c interface.
Select this if your platform is using the i2c bus.
If you choose to build a module, it'll be called st21nfcb_i2c.
Say N if unsure.

View file

@ -0,0 +1,9 @@
#
# Makefile for ST21NFCB NCI based NFC driver
#
st21nfcb_nci-objs = ndlc.o st21nfcb.o
obj-$(CONFIG_NFC_ST21NFCB) += st21nfcb_nci.o
st21nfcb_i2c-objs = i2c.o
obj-$(CONFIG_NFC_ST21NFCB_I2C) += st21nfcb_i2c.o

423
drivers/nfc/st21nfcb/i2c.c Normal file
View file

@ -0,0 +1,423 @@
/*
* I2C Link Layer for ST21NFCB NCI 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/module.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/nfc.h>
#include <linux/platform_data/st21nfcb.h>
#include "ndlc.h"
#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
/* ndlc header */
#define ST21NFCB_FRAME_HEADROOM 1
#define ST21NFCB_FRAME_TAILROOM 0
#define ST21NFCB_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
#define ST21NFCB_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
#define ST21NFCB_NCI_I2C_DRIVER_NAME "st21nfcb_nci_i2c"
static struct i2c_device_id st21nfcb_nci_i2c_id_table[] = {
{ST21NFCB_NCI_DRIVER_NAME, 0},
{}
};
MODULE_DEVICE_TABLE(i2c, st21nfcb_nci_i2c_id_table);
struct st21nfcb_i2c_phy {
struct i2c_client *i2c_dev;
struct llt_ndlc *ndlc;
unsigned int gpio_irq;
unsigned int gpio_reset;
unsigned int irq_polarity;
int powered;
};
#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)
static int st21nfcb_nci_i2c_enable(void *phy_id)
{
struct st21nfcb_i2c_phy *phy = phy_id;
gpio_set_value(phy->gpio_reset, 0);
usleep_range(10000, 15000);
gpio_set_value(phy->gpio_reset, 1);
phy->powered = 1;
usleep_range(80000, 85000);
return 0;
}
static void st21nfcb_nci_i2c_disable(void *phy_id)
{
struct st21nfcb_i2c_phy *phy = phy_id;
pr_info("\n");
phy->powered = 0;
/* reset chip in order to flush clf */
gpio_set_value(phy->gpio_reset, 0);
usleep_range(10000, 15000);
gpio_set_value(phy->gpio_reset, 1);
}
static void st21nfcb_nci_remove_header(struct sk_buff *skb)
{
skb_pull(skb, ST21NFCB_FRAME_HEADROOM);
}
/*
* 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 st21nfcb_nci_i2c_write(void *phy_id, struct sk_buff *skb)
{
int r = -1;
struct st21nfcb_i2c_phy *phy = phy_id;
struct i2c_client *client = phy->i2c_dev;
I2C_DUMP_SKB("st21nfcb_nci_i2c_write", skb);
if (phy->ndlc->hard_fault != 0)
return phy->ndlc->hard_fault;
r = i2c_master_send(client, skb->data, skb->len);
if (r < 0) { /* Retry, chip was in standby */
usleep_range(1000, 4000);
r = i2c_master_send(client, skb->data, skb->len);
}
if (r >= 0) {
if (r != skb->len)
r = -EREMOTEIO;
else
r = 0;
}
st21nfcb_nci_remove_header(skb);
return r;
}
/*
* Reads an ndlc frame and returns it in a newly allocated sk_buff.
* returns:
* frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
* end of read)
* -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
* at end of read)
* -EREMOTEIO : i2c read error (fatal)
* -EBADMSG : frame was incorrect and discarded
* (value returned from st21nfcb_nci_i2c_repack)
* -EIO : if no ST21NFCB_SOF_EOF is found after reaching
* the read length end sequence
*/
static int st21nfcb_nci_i2c_read(struct st21nfcb_i2c_phy *phy,
struct sk_buff **skb)
{
int r;
u8 len;
u8 buf[ST21NFCB_NCI_I2C_MAX_SIZE];
struct i2c_client *client = phy->i2c_dev;
r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
if (r < 0) { /* Retry, chip was in standby */
usleep_range(1000, 4000);
r = i2c_master_recv(client, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
}
if (r != ST21NFCB_NCI_I2C_MIN_SIZE)
return -EREMOTEIO;
len = be16_to_cpu(*(__be16 *) (buf + 2));
if (len > ST21NFCB_NCI_I2C_MAX_SIZE) {
nfc_err(&client->dev, "invalid frame len\n");
return -EBADMSG;
}
*skb = alloc_skb(ST21NFCB_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
if (*skb == NULL)
return -ENOMEM;
skb_reserve(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
skb_put(*skb, ST21NFCB_NCI_I2C_MIN_SIZE);
memcpy((*skb)->data, buf, ST21NFCB_NCI_I2C_MIN_SIZE);
if (!len)
return 0;
r = i2c_master_recv(client, buf, len);
if (r != len) {
kfree_skb(*skb);
return -EREMOTEIO;
}
skb_put(*skb, len);
memcpy((*skb)->data + ST21NFCB_NCI_I2C_MIN_SIZE, buf, len);
I2C_DUMP_SKB("i2c frame read", *skb);
return 0;
}
/*
* Reads an ndlc frame from the chip.
*
* On ST21NFCB, IRQ goes in idle state when read starts.
*/
static irqreturn_t st21nfcb_nci_irq_thread_fn(int irq, void *phy_id)
{
struct st21nfcb_i2c_phy *phy = phy_id;
struct i2c_client *client;
struct sk_buff *skb = NULL;
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->ndlc->hard_fault)
return IRQ_HANDLED;
if (!phy->powered) {
st21nfcb_nci_i2c_disable(phy);
return IRQ_HANDLED;
}
r = st21nfcb_nci_i2c_read(phy, &skb);
if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
return IRQ_HANDLED;
ndlc_recv(phy->ndlc, skb);
return IRQ_HANDLED;
}
static struct nfc_phy_ops i2c_phy_ops = {
.write = st21nfcb_nci_i2c_write,
.enable = st21nfcb_nci_i2c_enable,
.disable = st21nfcb_nci_i2c_disable,
};
#ifdef CONFIG_OF
static int st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
{
struct st21nfcb_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, "reset-gpios", 0);
if (gpio < 0) {
nfc_err(&client->dev,
"Failed to retrieve reset-gpios from device tree\n");
return gpio;
}
/* GPIO request and configuration */
r = devm_gpio_request_one(&client->dev, gpio,
GPIOF_OUT_INIT_HIGH, "clf_reset");
if (r) {
nfc_err(&client->dev, "Failed to request reset pin\n");
return -ENODEV;
}
phy->gpio_reset = 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 st21nfcb_nci_i2c_of_request_resources(struct i2c_client *client)
{
return -ENODEV;
}
#endif
static int st21nfcb_nci_i2c_request_resources(struct i2c_client *client)
{
struct st21nfcb_nfc_platform_data *pdata;
struct st21nfcb_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_reset = pdata->gpio_reset;
phy->irq_polarity = pdata->irq_polarity;
r = devm_gpio_request_one(&client->dev, phy->gpio_irq,
GPIOF_IN, "clf_irq");
if (r) {
pr_err("%s : gpio_request failed\n", __FILE__);
return -ENODEV;
}
r = devm_gpio_request_one(&client->dev,
phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
if (r) {
pr_err("%s : reset gpio_request 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 st21nfcb_nci_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct st21nfcb_i2c_phy *phy;
struct st21nfcb_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 st21nfcb_i2c_phy),
GFP_KERNEL);
if (!phy) {
nfc_err(&client->dev,
"Cannot allocate memory for st21nfcb i2c phy.\n");
return -ENOMEM;
}
phy->i2c_dev = client;
i2c_set_clientdata(client, phy);
pdata = client->dev.platform_data;
if (!pdata && client->dev.of_node) {
r = st21nfcb_nci_i2c_of_request_resources(client);
if (r) {
nfc_err(&client->dev, "No platform data\n");
return r;
}
} else if (pdata) {
r = st21nfcb_nci_i2c_request_resources(client);
if (r) {
nfc_err(&client->dev,
"Cannot get platform resources\n");
return r;
}
} else {
nfc_err(&client->dev,
"st21nfcb platform resources not available\n");
return -ENODEV;
}
r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
st21nfcb_nci_irq_thread_fn,
phy->irq_polarity | IRQF_ONESHOT,
ST21NFCB_NCI_DRIVER_NAME, phy);
if (r < 0) {
nfc_err(&client->dev, "Unable to register IRQ handler\n");
return r;
}
return ndlc_probe(phy, &i2c_phy_ops, &client->dev,
ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
&phy->ndlc);
}
static int st21nfcb_nci_i2c_remove(struct i2c_client *client)
{
struct st21nfcb_i2c_phy *phy = i2c_get_clientdata(client);
dev_dbg(&client->dev, "%s\n", __func__);
ndlc_remove(phy->ndlc);
if (phy->powered)
st21nfcb_nci_i2c_disable(phy);
return 0;
}
static const struct of_device_id of_st21nfcb_i2c_match[] = {
{ .compatible = "st,st21nfcb_i2c", },
{}
};
static struct i2c_driver st21nfcb_nci_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = ST21NFCB_NCI_I2C_DRIVER_NAME,
.of_match_table = of_match_ptr(of_st21nfcb_i2c_match),
},
.probe = st21nfcb_nci_i2c_probe,
.id_table = st21nfcb_nci_i2c_id_table,
.remove = st21nfcb_nci_i2c_remove,
};
module_i2c_driver(st21nfcb_nci_i2c_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

302
drivers/nfc/st21nfcb/ndlc.c Normal file
View file

@ -0,0 +1,302 @@
/*
* Low Level Transport (NDLC) 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/sched.h>
#include <net/nfc/nci_core.h>
#include "ndlc.h"
#include "st21nfcb.h"
#define NDLC_TIMER_T1 100
#define NDLC_TIMER_T1_WAIT 400
#define NDLC_TIMER_T2 1200
#define PCB_TYPE_DATAFRAME 0x80
#define PCB_TYPE_SUPERVISOR 0xc0
#define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
#define PCB_SYNC_ACK 0x20
#define PCB_SYNC_NACK 0x10
#define PCB_SYNC_WAIT 0x30
#define PCB_SYNC_NOINFO 0x00
#define PCB_SYNC_MASK PCB_SYNC_WAIT
#define PCB_DATAFRAME_RETRANSMIT_YES 0x00
#define PCB_DATAFRAME_RETRANSMIT_NO 0x04
#define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
#define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
#define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
#define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
#define PCB_FRAME_CRC_INFO_PRESENT 0x08
#define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
#define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
#define NDLC_DUMP_SKB(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
16, 1, skb->data, skb->len, 0); \
} while (0)
int ndlc_open(struct llt_ndlc *ndlc)
{
/* toggle reset pin */
ndlc->ops->enable(ndlc->phy_id);
return 0;
}
EXPORT_SYMBOL(ndlc_open);
void ndlc_close(struct llt_ndlc *ndlc)
{
/* toggle reset pin */
ndlc->ops->disable(ndlc->phy_id);
}
EXPORT_SYMBOL(ndlc_close);
int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
{
/* add ndlc header */
u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
PCB_FRAME_CRC_INFO_NOTPRESENT;
*skb_push(skb, 1) = pcb;
skb_queue_tail(&ndlc->send_q, skb);
schedule_work(&ndlc->sm_work);
return 0;
}
EXPORT_SYMBOL(ndlc_send);
static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
{
struct sk_buff *skb;
int r;
unsigned long time_sent;
if (ndlc->send_q.qlen)
pr_debug("sendQlen=%d unackQlen=%d\n",
ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
while (ndlc->send_q.qlen) {
skb = skb_dequeue(&ndlc->send_q);
NDLC_DUMP_SKB("ndlc frame written", skb);
r = ndlc->ops->write(ndlc->phy_id, skb);
if (r < 0) {
ndlc->hard_fault = r;
break;
}
time_sent = jiffies;
*(unsigned long *)skb->cb = time_sent;
skb_queue_tail(&ndlc->ack_pending_q, skb);
/* start timer t1 for ndlc aknowledge */
ndlc->t1_active = true;
mod_timer(&ndlc->t1_timer, time_sent +
msecs_to_jiffies(NDLC_TIMER_T1));
/* start timer t2 for chip availability */
ndlc->t2_active = true;
mod_timer(&ndlc->t2_timer, time_sent +
msecs_to_jiffies(NDLC_TIMER_T2));
}
}
static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
{
struct sk_buff *skb;
u8 pcb;
while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
pcb = skb->data[0];
switch (pcb & PCB_TYPE_MASK) {
case PCB_TYPE_SUPERVISOR:
skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
PCB_SUPERVISOR_RETRANSMIT_YES;
break;
case PCB_TYPE_DATAFRAME:
skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
PCB_DATAFRAME_RETRANSMIT_YES;
break;
default:
pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
kfree_skb(skb);
break;
}
skb_queue_head(&ndlc->send_q, skb);
}
}
static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
{
struct sk_buff *skb;
u8 pcb;
unsigned long time_sent;
if (ndlc->rcv_q.qlen)
pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
pcb = skb->data[0];
skb_pull(skb, 1);
if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
switch (pcb & PCB_SYNC_MASK) {
case PCB_SYNC_ACK:
del_timer_sync(&ndlc->t1_timer);
del_timer_sync(&ndlc->t2_timer);
ndlc->t2_active = false;
ndlc->t1_active = false;
break;
case PCB_SYNC_NACK:
llt_ndlc_requeue_data_pending(ndlc);
llt_ndlc_send_queue(ndlc);
/* start timer t1 for ndlc aknowledge */
time_sent = jiffies;
ndlc->t1_active = true;
mod_timer(&ndlc->t1_timer, time_sent +
msecs_to_jiffies(NDLC_TIMER_T1));
break;
case PCB_SYNC_WAIT:
time_sent = jiffies;
ndlc->t1_active = true;
mod_timer(&ndlc->t1_timer, time_sent +
msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
break;
default:
pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
kfree_skb(skb);
break;
}
} else {
nci_recv_frame(ndlc->ndev, skb);
}
}
}
static void llt_ndlc_sm_work(struct work_struct *work)
{
struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
llt_ndlc_send_queue(ndlc);
llt_ndlc_rcv_queue(ndlc);
if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
pr_debug
("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
ndlc->t1_active = false;
llt_ndlc_requeue_data_pending(ndlc);
llt_ndlc_send_queue(ndlc);
}
if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
ndlc->t2_active = false;
ndlc->t1_active = false;
del_timer_sync(&ndlc->t1_timer);
del_timer_sync(&ndlc->t2_timer);
ndlc_close(ndlc);
ndlc->hard_fault = -EREMOTEIO;
}
}
void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
{
if (skb == NULL) {
pr_err("NULL Frame -> link is dead\n");
ndlc->hard_fault = -EREMOTEIO;
ndlc_close(ndlc);
} else {
NDLC_DUMP_SKB("incoming frame", skb);
skb_queue_tail(&ndlc->rcv_q, skb);
}
schedule_work(&ndlc->sm_work);
}
EXPORT_SYMBOL(ndlc_recv);
static void ndlc_t1_timeout(unsigned long data)
{
struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
pr_debug("\n");
schedule_work(&ndlc->sm_work);
}
static void ndlc_t2_timeout(unsigned long data)
{
struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
pr_debug("\n");
schedule_work(&ndlc->sm_work);
}
int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id)
{
struct llt_ndlc *ndlc;
ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
if (!ndlc) {
nfc_err(dev, "Cannot allocate memory for ndlc.\n");
return -ENOMEM;
}
ndlc->ops = phy_ops;
ndlc->phy_id = phy_id;
ndlc->dev = dev;
*ndlc_id = ndlc;
/* start timers */
init_timer(&ndlc->t1_timer);
ndlc->t1_timer.data = (unsigned long)ndlc;
ndlc->t1_timer.function = ndlc_t1_timeout;
init_timer(&ndlc->t2_timer);
ndlc->t2_timer.data = (unsigned long)ndlc;
ndlc->t2_timer.function = ndlc_t2_timeout;
skb_queue_head_init(&ndlc->rcv_q);
skb_queue_head_init(&ndlc->send_q);
skb_queue_head_init(&ndlc->ack_pending_q);
INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
return st21nfcb_nci_probe(ndlc, phy_headroom, phy_tailroom);
}
EXPORT_SYMBOL(ndlc_probe);
void ndlc_remove(struct llt_ndlc *ndlc)
{
/* cancel timers */
del_timer_sync(&ndlc->t1_timer);
del_timer_sync(&ndlc->t2_timer);
ndlc->t2_active = false;
ndlc->t1_active = false;
skb_queue_purge(&ndlc->rcv_q);
skb_queue_purge(&ndlc->send_q);
st21nfcb_nci_remove(ndlc->ndev);
kfree(ndlc);
}
EXPORT_SYMBOL(ndlc_remove);

View file

@ -0,0 +1,59 @@
/*
* NCI 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/>.
*/
#ifndef __LOCAL_NDLC_H_
#define __LOCAL_NDLC_H_
#include <linux/skbuff.h>
#include <net/nfc/nfc.h>
/* Low Level Transport description */
struct llt_ndlc {
struct nci_dev *ndev;
struct nfc_phy_ops *ops;
void *phy_id;
struct timer_list t1_timer;
bool t1_active;
struct timer_list t2_timer;
bool t2_active;
struct sk_buff_head rcv_q;
struct sk_buff_head send_q;
struct sk_buff_head ack_pending_q;
struct work_struct sm_work;
struct device *dev;
/*
* < 0 if hardware error occured
* and prevents normal operation.
*/
int hard_fault;
};
int ndlc_open(struct llt_ndlc *ndlc);
void ndlc_close(struct llt_ndlc *ndlc);
int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb);
void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb);
int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id);
void ndlc_remove(struct llt_ndlc *ndlc);
#endif /* __LOCAL_NDLC_H__ */

View file

@ -0,0 +1,134 @@
/*
* NCI 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/nci.h>
#include <net/nfc/nci_core.h>
#include "st21nfcb.h"
#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
#define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83
static int st21nfcb_nci_open(struct nci_dev *ndev)
{
struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
int r;
if (test_and_set_bit(ST21NFCB_NCI_RUNNING, &info->flags))
return 0;
r = ndlc_open(info->ndlc);
if (r)
clear_bit(ST21NFCB_NCI_RUNNING, &info->flags);
return r;
}
static int st21nfcb_nci_close(struct nci_dev *ndev)
{
struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
if (!test_and_clear_bit(ST21NFCB_NCI_RUNNING, &info->flags))
return 0;
ndlc_close(info->ndlc);
return 0;
}
static int st21nfcb_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
{
struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
skb->dev = (void *)ndev;
if (!test_bit(ST21NFCB_NCI_RUNNING, &info->flags))
return -EBUSY;
return ndlc_send(info->ndlc, skb);
}
static __u32 st21nfcb_nci_get_rfprotocol(struct nci_dev *ndev,
__u8 rf_protocol)
{
return rf_protocol == ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 ?
NFC_PROTO_ISO15693_MASK : 0;
}
static struct nci_ops st21nfcb_nci_ops = {
.open = st21nfcb_nci_open,
.close = st21nfcb_nci_close,
.send = st21nfcb_nci_send,
.get_rfprotocol = st21nfcb_nci_get_rfprotocol,
};
int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
int phy_tailroom)
{
struct st21nfcb_nci_info *info;
int r;
u32 protocols;
info = devm_kzalloc(ndlc->dev,
sizeof(struct st21nfcb_nci_info), GFP_KERNEL);
if (!info)
return -ENOMEM;
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
| NFC_PROTO_NFC_DEP_MASK;
ndlc->ndev = nci_allocate_device(&st21nfcb_nci_ops, protocols,
phy_headroom, phy_tailroom);
if (!ndlc->ndev) {
pr_err("Cannot allocate nfc ndev\n");
return -ENOMEM;
}
info->ndlc = ndlc;
nci_set_drvdata(ndlc->ndev, info);
r = nci_register_device(ndlc->ndev);
if (r) {
pr_err("Cannot register nfc device to nci core\n");
nci_free_device(ndlc->ndev);
}
return r;
}
EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
void st21nfcb_nci_remove(struct nci_dev *ndev)
{
struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
nci_unregister_device(ndev);
nci_free_device(ndev);
kfree(info);
}
EXPORT_SYMBOL_GPL(st21nfcb_nci_remove);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);

View file

@ -0,0 +1,36 @@
/*
* NCI 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/>.
*/
#ifndef __LOCAL_ST21NFCB_H_
#define __LOCAL_ST21NFCB_H_
#include "ndlc.h"
/* Define private flags: */
#define ST21NFCB_NCI_RUNNING 1
struct st21nfcb_nci_info {
struct llt_ndlc *ndlc;
unsigned long flags;
};
void st21nfcb_nci_remove(struct nci_dev *ndev);
int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
int phy_tailroom);
#endif /* __LOCAL_ST21NFCB_H_ */

2218
drivers/nfc/trf7970a.c Normal file

File diff suppressed because it is too large Load diff