Commit c6d3b204 authored by John W. Linville's avatar John W. Linville

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

Samuel says:

"This is the 2nd NFC pull request.

With this one we have a new NFC driver for Inside Secure microread and a few
pn533 fixes.
Microread is an HCI based NFC IP and the driver we're pushing supports tags
R/W, and NFC p2p. It's supported over the i2c and MEI busses."
Signed-off-by: default avatarJohn W. Linville <linville@tuxdriver.com>
parents 4d25a75b 52f2eaee
...@@ -27,5 +27,6 @@ config NFC_WILINK ...@@ -27,5 +27,6 @@ config NFC_WILINK
into the kernel or say M to compile it as module. into the kernel or say M to compile it as module.
source "drivers/nfc/pn544/Kconfig" source "drivers/nfc/pn544/Kconfig"
source "drivers/nfc/microread/Kconfig"
endmenu endmenu
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# #
obj-$(CONFIG_NFC_PN544) += pn544/ obj-$(CONFIG_NFC_PN544) += pn544/
obj-$(CONFIG_NFC_MICROREAD) += microread/
obj-$(CONFIG_NFC_PN533) += pn533.o obj-$(CONFIG_NFC_PN533) += pn533.o
obj-$(CONFIG_NFC_WILINK) += nfcwilink.o obj-$(CONFIG_NFC_WILINK) += nfcwilink.o
......
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 && INTEL_MEI_BUS_NFC
---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.
#
# 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
/*
* 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, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#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(MICROREAD_I2C_DRIVER_NAME
": CRC error 0x%x != 0x%x\n",
crc, skb->data[skb->len-1]);
pr_info(DRIVER_DESC ": %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;
pr_debug("%s\n", __func__);
r = i2c_master_recv(client, &len, 1);
if (r != 1) {
dev_err(&client->dev, "cannot read len byte\n");
return -EREMOTEIO;
}
if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
(len > MICROREAD_I2C_LLC_MAX_SIZE)) {
dev_err(&client->dev, "invalid len byte\n");
pr_err("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;
dev_dbg(&client->dev, "IRQ\n");
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", client);
if (!pdata) {
dev_err(&client->dev, "client %p: missing platform data",
client);
return -EINVAL;
}
phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
GFP_KERNEL);
if (!phy) {
dev_err(&client->dev, "Can't allocate microread 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) {
dev_err(&client->dev, "Unable to register IRQ handler");
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;
dev_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);
dev_dbg(&client->dev, "%s\n", __func__);
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);
/*
* 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, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/mei_bus.h>
#include <linux/nfc.h>
#include <net/nfc/hci.h>
#include <net/nfc/llc.h>
#include "microread.h"
#define MICROREAD_DRIVER_NAME "microread"
#define MICROREAD_UUID UUID_LE(0x0bb17a78, 0x2a8e, 0x4c50, 0x94, \
0xd4, 0x50, 0x26, 0x67, 0x23, 0x77, 0x5c)
struct mei_nfc_hdr {
u8 cmd;
u8 status;
u16 req_id;
u32 reserved;
u16 data_size;
} __attribute__((packed));
#define MEI_NFC_HEADER_SIZE 10
#define MEI_NFC_MAX_HCI_PAYLOAD 300
#define MEI_NFC_MAX_READ (MEI_NFC_HEADER_SIZE + MEI_NFC_MAX_HCI_PAYLOAD)
struct microread_mei_phy {
struct mei_bus_client *client;
struct nfc_hci_dev *hdev;
int powered;
int hard_fault; /*
* < 0 if hardware error occured (e.g. i2c err)
* and prevents normal operation.
*/
};
#define MEI_DUMP_SKB_IN(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump(KERN_DEBUG, "mei in : ", DUMP_PREFIX_OFFSET, \
16, 1, (skb)->data, (skb)->len, 0); \
} while (0)
#define MEI_DUMP_SKB_OUT(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump(KERN_DEBUG, "mei out: ", DUMP_PREFIX_OFFSET, \
16, 1, (skb)->data, (skb)->len, 0); \
} while (0)
static int microread_mei_enable(void *phy_id)
{
struct microread_mei_phy *phy = phy_id;
pr_info(DRIVER_DESC ": %s\n", __func__);
phy->powered = 1;
return 0;
}
static void microread_mei_disable(void *phy_id)
{
struct microread_mei_phy *phy = phy_id;
pr_info(DRIVER_DESC ": %s\n", __func__);
phy->powered = 0;
}
/*
* 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 microread_mei_write(void *phy_id, struct sk_buff *skb)
{
struct microread_mei_phy *phy = phy_id;
int r;
MEI_DUMP_SKB_OUT("mei frame sent", skb);
r = mei_bus_send(phy->client, skb->data, skb->len);
if (r > 0)
r = 0;
return r;
}
static void microread_event_cb(struct mei_bus_client *client, u32 events,
void *context)
{
struct microread_mei_phy *phy = context;
if (phy->hard_fault != 0)
return;
if (events & BIT(MEI_BUS_EVENT_RX)) {
struct sk_buff *skb;
int reply_size;
skb = alloc_skb(MEI_NFC_MAX_READ, GFP_KERNEL);
if (!skb)
return;
reply_size = mei_bus_recv(client, skb->data, MEI_NFC_MAX_READ);
if (reply_size < MEI_NFC_HEADER_SIZE) {
kfree(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);
}
}
static struct nfc_phy_ops mei_phy_ops = {
.write = microread_mei_write,
.enable = microread_mei_enable,
.disable = microread_mei_disable,
};
static int microread_mei_probe(struct mei_bus_client *client)
{
struct microread_mei_phy *phy;
int r;
pr_info("Probing NFC microread\n");
phy = kzalloc(sizeof(struct microread_mei_phy), GFP_KERNEL);
if (!phy) {
pr_err("Cannot allocate memory for microread mei phy.\n");
return -ENOMEM;
}
phy->client = client;
mei_bus_set_clientdata(client, phy);
r = mei_bus_register_event_cb(client, microread_event_cb, phy);
if (r) {
pr_err(MICROREAD_DRIVER_NAME ": event cb registration failed\n");
goto err_out;
}
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)
goto err_out;
return 0;
err_out:
kfree(phy);
return r;
}
static int microread_mei_remove(struct mei_bus_client *client)
{
struct microread_mei_phy *phy = mei_bus_get_clientdata(client);
pr_info("Removing microread\n");
microread_remove(phy->hdev);
if (phy->powered)
microread_mei_disable(phy);
kfree(phy);
return 0;
}
static struct mei_bus_driver microread_driver = {
.driver = {
.name = MICROREAD_DRIVER_NAME,
},
.id = {
.name = MICROREAD_DRIVER_NAME,
.uuid = MICROREAD_UUID,
},
.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_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_driver_unregister(&microread_driver);
}
module_init(microread_mei_init);
module_exit(microread_mei_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);
This diff is collapsed.
/*
* 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, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#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_ */
...@@ -219,7 +219,7 @@ struct pn533_poll_modulations { ...@@ -219,7 +219,7 @@ struct pn533_poll_modulations {
u8 len; u8 len;
}; };
const struct pn533_poll_modulations poll_mod[] = { static const struct pn533_poll_modulations poll_mod[] = {
[PN533_POLL_MOD_106KBPS_A] = { [PN533_POLL_MOD_106KBPS_A] = {
.data = { .data = {
.maxtg = 1, .maxtg = 1,
...@@ -485,7 +485,7 @@ static u8 pn533_get_cmd_code(void *frame) ...@@ -485,7 +485,7 @@ static u8 pn533_get_cmd_code(void *frame)
return PN533_FRAME_CMD(f); return PN533_FRAME_CMD(f);
} }
struct pn533_frame_ops pn533_std_frame_ops = { static struct pn533_frame_ops pn533_std_frame_ops = {
.tx_frame_init = pn533_tx_frame_init, .tx_frame_init = pn533_tx_frame_init,
.tx_frame_finish = pn533_tx_frame_finish, .tx_frame_finish = pn533_tx_frame_finish,
.tx_update_payload_len = pn533_tx_update_payload_len, .tx_update_payload_len = pn533_tx_update_payload_len,
...@@ -532,7 +532,6 @@ static void pn533_recv_response(struct urb *urb) ...@@ -532,7 +532,6 @@ static void pn533_recv_response(struct urb *urb)
urb->status); urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
break;
case -ESHUTDOWN: case -ESHUTDOWN:
default: default:
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
...@@ -589,7 +588,6 @@ static void pn533_recv_ack(struct urb *urb) ...@@ -589,7 +588,6 @@ static void pn533_recv_ack(struct urb *urb)
urb->status); urb->status);
dev->wq_in_error = urb->status; dev->wq_in_error = urb->status;
goto sched_wq; goto sched_wq;
break;
case -ESHUTDOWN: case -ESHUTDOWN:
default: default:
nfc_dev_err(&dev->interface->dev, nfc_dev_err(&dev->interface->dev,
...@@ -1380,7 +1378,7 @@ static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev) ...@@ -1380,7 +1378,7 @@ static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
return NULL; return NULL;
/* DEP support only */ /* DEP support only */
*skb_put(skb, 1) |= PN533_INIT_TARGET_DEP; *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
/* MIFARE params */ /* MIFARE params */
memcpy(skb_put(skb, 6), mifare_params, 6); memcpy(skb_put(skb, 6), mifare_params, 6);
......
/*
* Driver include for the PN544 NFC chip.
*
* Copyright (C) 2011 Tieto Poland
* Copyright (C) 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
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _MICROREAD_H
#define _MICROREAD_H
#include <linux/i2c.h>
#define MICROREAD_DRIVER_NAME "microread"
/* board config platform data for microread */
struct microread_nfc_platform_data {
unsigned int rst_gpio;
unsigned int irq_gpio;
unsigned int ioh_gpio;
};
#endif /* _MICROREAD_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment