diff options
| author | Frederic Danis <frederic.danis@linux.intel.com> | 2013-05-29 09:35:02 -0400 |
|---|---|---|
| committer | Samuel Ortiz <sameo@linux.intel.com> | 2013-06-14 07:44:03 -0400 |
| commit | 8a00a61b0ef2bfd1b468dd20c0d0b1a94a8f7475 (patch) | |
| tree | 04438ab0d31ae79e0970cc7f901dd5605a5f4ec1 | |
| parent | a395298c9c96748cbd6acee4cb9a5ba13fbb3ab8 (diff) | |
NFC: Add basic NCI over SPI
The NFC Forum defines a transport interface based on
Serial Peripheral Interface (SPI) for the NFC Controller
Interface (NCI).
This module implements the SPI transport of NCI, calling SPI module
directly to read/write data to NFC controller (NFCC).
NFCC driver should provide functions performing device open and close.
It should also provide functions asserting/de-asserting interruption
to prevent TX/RX race conditions.
NFCC driver can also fix a delay between transactions if needed by
the hardware.
Signed-off-by: Frederic Danis <frederic.danis@linux.intel.com>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
| -rw-r--r-- | include/net/nfc/nci_core.h | 49 | ||||
| -rw-r--r-- | net/nfc/nci/Kconfig | 10 | ||||
| -rw-r--r-- | net/nfc/nci/Makefile | 4 | ||||
| -rw-r--r-- | net/nfc/nci/spi.c | 136 |
4 files changed, 198 insertions, 1 deletions
diff --git a/include/net/nfc/nci_core.h b/include/net/nfc/nci_core.h index 1009d3dcb316..3b05bebd8235 100644 --- a/include/net/nfc/nci_core.h +++ b/include/net/nfc/nci_core.h | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | * NFC Controller (NFCC) and a Device Host (DH). | 3 | * NFC Controller (NFCC) and a Device Host (DH). |
| 4 | * | 4 | * |
| 5 | * Copyright (C) 2011 Texas Instruments, Inc. | 5 | * Copyright (C) 2011 Texas Instruments, Inc. |
| 6 | * Copyright (C) 2013 Intel Corporation. All rights reserved. | ||
| 6 | * | 7 | * |
| 7 | * Written by Ilan Elias <ilane@ti.com> | 8 | * Written by Ilan Elias <ilane@ti.com> |
| 8 | * | 9 | * |
| @@ -202,4 +203,52 @@ void nci_req_complete(struct nci_dev *ndev, int result); | |||
| 202 | /* ----- NCI status code ----- */ | 203 | /* ----- NCI status code ----- */ |
| 203 | int nci_to_errno(__u8 code); | 204 | int nci_to_errno(__u8 code); |
| 204 | 205 | ||
| 206 | /* ----- NCI over SPI acknowledge modes ----- */ | ||
| 207 | #define NCI_SPI_CRC_DISABLED 0x00 | ||
| 208 | #define NCI_SPI_CRC_ENABLED 0x01 | ||
| 209 | |||
| 210 | /* ----- NCI SPI structures ----- */ | ||
| 211 | struct nci_spi_dev; | ||
| 212 | |||
| 213 | struct nci_spi_ops { | ||
| 214 | int (*open)(struct nci_spi_dev *ndev); | ||
| 215 | int (*close)(struct nci_spi_dev *ndev); | ||
| 216 | void (*assert_int)(struct nci_spi_dev *ndev); | ||
| 217 | void (*deassert_int)(struct nci_spi_dev *ndev); | ||
| 218 | }; | ||
| 219 | |||
| 220 | struct nci_spi_dev { | ||
| 221 | struct nci_dev *nci_dev; | ||
| 222 | struct spi_device *spi; | ||
| 223 | struct nci_spi_ops *ops; | ||
| 224 | |||
| 225 | unsigned int xfer_udelay; /* microseconds delay between | ||
| 226 | transactions */ | ||
| 227 | u8 acknowledge_mode; | ||
| 228 | |||
| 229 | void *driver_data; | ||
| 230 | }; | ||
| 231 | |||
| 232 | /* ----- NCI SPI Devices ----- */ | ||
| 233 | struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi, | ||
| 234 | struct nci_spi_ops *ops, | ||
| 235 | u32 supported_protocols, | ||
| 236 | u32 supported_se, | ||
| 237 | u8 acknowledge_mode, | ||
| 238 | unsigned int delay); | ||
| 239 | void nci_spi_free_device(struct nci_spi_dev *ndev); | ||
| 240 | int nci_spi_register_device(struct nci_spi_dev *ndev); | ||
| 241 | void nci_spi_unregister_device(struct nci_spi_dev *ndev); | ||
| 242 | |||
| 243 | static inline void nci_spi_set_drvdata(struct nci_spi_dev *ndev, | ||
| 244 | void *data) | ||
| 245 | { | ||
| 246 | ndev->driver_data = data; | ||
| 247 | } | ||
| 248 | |||
| 249 | static inline void *nci_spi_get_drvdata(struct nci_spi_dev *ndev) | ||
| 250 | { | ||
| 251 | return ndev->driver_data; | ||
| 252 | } | ||
| 253 | |||
| 205 | #endif /* __NCI_CORE_H */ | 254 | #endif /* __NCI_CORE_H */ |
diff --git a/net/nfc/nci/Kconfig b/net/nfc/nci/Kconfig index 6d69b5f0f19b..2a2416080b4f 100644 --- a/net/nfc/nci/Kconfig +++ b/net/nfc/nci/Kconfig | |||
| @@ -8,3 +8,13 @@ config NFC_NCI | |||
| 8 | 8 | ||
| 9 | Say Y here to compile NCI support into the kernel or say M to | 9 | Say Y here to compile NCI support into the kernel or say M to |
| 10 | compile it as module (nci). | 10 | compile it as module (nci). |
| 11 | |||
| 12 | config NFC_NCI_SPI | ||
| 13 | depends on NFC_NCI && SPI | ||
| 14 | bool "NCI over SPI protocol support" | ||
| 15 | default n | ||
| 16 | help | ||
| 17 | NCI (NFC Controller Interface) is a communication protocol between | ||
| 18 | an NFC Controller (NFCC) and a Device Host (DH). | ||
| 19 | |||
| 20 | Say yes if you use an NCI driver that requires SPI link layer. | ||
diff --git a/net/nfc/nci/Makefile b/net/nfc/nci/Makefile index cdb3a2e44471..7aeedc43187d 100644 --- a/net/nfc/nci/Makefile +++ b/net/nfc/nci/Makefile | |||
| @@ -4,4 +4,6 @@ | |||
| 4 | 4 | ||
| 5 | obj-$(CONFIG_NFC_NCI) += nci.o | 5 | obj-$(CONFIG_NFC_NCI) += nci.o |
| 6 | 6 | ||
| 7 | nci-objs := core.o data.o lib.o ntf.o rsp.o \ No newline at end of file | 7 | nci-objs := core.o data.o lib.o ntf.o rsp.o |
| 8 | |||
| 9 | nci-$(CONFIG_NFC_NCI_SPI) += spi.o | ||
diff --git a/net/nfc/nci/spi.c b/net/nfc/nci/spi.c new file mode 100644 index 000000000000..ebcdba51418c --- /dev/null +++ b/net/nfc/nci/spi.c | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2013 Intel Corporation. All rights reserved. | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify it | ||
| 5 | * under the terms and conditions of the GNU General Public License, | ||
| 6 | * version 2, as published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 11 | * more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License along with | ||
| 14 | * this program; if not, write to the Free Software Foundation, Inc., | ||
| 15 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 16 | * | ||
| 17 | */ | ||
| 18 | |||
| 19 | #define pr_fmt(fmt) "nci_spi: %s: " fmt, __func__ | ||
| 20 | |||
| 21 | #include <linux/export.h> | ||
| 22 | #include <linux/spi/spi.h> | ||
| 23 | #include <linux/nfc.h> | ||
| 24 | #include <net/nfc/nci_core.h> | ||
| 25 | |||
| 26 | #define NCI_SPI_HDR_LEN 4 | ||
| 27 | #define NCI_SPI_CRC_LEN 2 | ||
| 28 | |||
| 29 | static int nci_spi_open(struct nci_dev *nci_dev) | ||
| 30 | { | ||
| 31 | struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev); | ||
| 32 | |||
| 33 | return ndev->ops->open(ndev); | ||
| 34 | } | ||
| 35 | |||
| 36 | static int nci_spi_close(struct nci_dev *nci_dev) | ||
| 37 | { | ||
| 38 | struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev); | ||
| 39 | |||
| 40 | return ndev->ops->close(ndev); | ||
| 41 | } | ||
| 42 | |||
| 43 | static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb) | ||
| 44 | { | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | |||
| 48 | static struct nci_ops nci_spi_ops = { | ||
| 49 | .open = nci_spi_open, | ||
| 50 | .close = nci_spi_close, | ||
| 51 | .send = nci_spi_send, | ||
| 52 | }; | ||
| 53 | |||
| 54 | /* ---- Interface to NCI SPI drivers ---- */ | ||
| 55 | |||
| 56 | /** | ||
| 57 | * nci_spi_allocate_device - allocate a new nci spi device | ||
| 58 | * | ||
| 59 | * @spi: SPI device | ||
| 60 | * @ops: device operations | ||
| 61 | * @supported_protocols: NFC protocols supported by the device | ||
| 62 | * @supported_se: NFC Secure Elements supported by the device | ||
| 63 | * @acknowledge_mode: Acknowledge mode used by the device | ||
| 64 | * @delay: delay between transactions in us | ||
| 65 | */ | ||
| 66 | struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi, | ||
| 67 | struct nci_spi_ops *ops, | ||
| 68 | u32 supported_protocols, | ||
| 69 | u32 supported_se, | ||
| 70 | u8 acknowledge_mode, | ||
| 71 | unsigned int delay) | ||
| 72 | { | ||
| 73 | struct nci_spi_dev *ndev; | ||
| 74 | int tailroom = 0; | ||
| 75 | |||
| 76 | if (!ops->open || !ops->close || !ops->assert_int || !ops->deassert_int) | ||
| 77 | return NULL; | ||
| 78 | |||
| 79 | if (!supported_protocols) | ||
| 80 | return NULL; | ||
| 81 | |||
| 82 | ndev = devm_kzalloc(&spi->dev, sizeof(struct nci_dev), GFP_KERNEL); | ||
| 83 | if (!ndev) | ||
| 84 | return NULL; | ||
| 85 | |||
| 86 | ndev->ops = ops; | ||
| 87 | ndev->acknowledge_mode = acknowledge_mode; | ||
| 88 | ndev->xfer_udelay = delay; | ||
| 89 | |||
| 90 | if (acknowledge_mode == NCI_SPI_CRC_ENABLED) | ||
| 91 | tailroom += NCI_SPI_CRC_LEN; | ||
| 92 | |||
| 93 | ndev->nci_dev = nci_allocate_device(&nci_spi_ops, supported_protocols, | ||
| 94 | supported_se, NCI_SPI_HDR_LEN, | ||
| 95 | tailroom); | ||
| 96 | if (!ndev->nci_dev) | ||
| 97 | return NULL; | ||
| 98 | |||
| 99 | nci_set_drvdata(ndev->nci_dev, ndev); | ||
| 100 | |||
| 101 | return ndev; | ||
| 102 | } | ||
| 103 | EXPORT_SYMBOL_GPL(nci_spi_allocate_device); | ||
| 104 | |||
| 105 | /** | ||
| 106 | * nci_spi_free_device - deallocate nci spi device | ||
| 107 | * | ||
| 108 | * @ndev: The nci spi device to deallocate | ||
| 109 | */ | ||
| 110 | void nci_spi_free_device(struct nci_spi_dev *ndev) | ||
| 111 | { | ||
| 112 | nci_free_device(ndev->nci_dev); | ||
| 113 | } | ||
| 114 | EXPORT_SYMBOL_GPL(nci_spi_free_device); | ||
| 115 | |||
| 116 | /** | ||
| 117 | * nci_spi_register_device - register a nci spi device in the nfc subsystem | ||
| 118 | * | ||
| 119 | * @pdev: The nci spi device to register | ||
| 120 | */ | ||
| 121 | int nci_spi_register_device(struct nci_spi_dev *ndev) | ||
| 122 | { | ||
| 123 | return nci_register_device(ndev->nci_dev); | ||
| 124 | } | ||
| 125 | EXPORT_SYMBOL_GPL(nci_spi_register_device); | ||
| 126 | |||
| 127 | /** | ||
| 128 | * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem | ||
| 129 | * | ||
| 130 | * @dev: The nci spi device to unregister | ||
| 131 | */ | ||
| 132 | void nci_spi_unregister_device(struct nci_spi_dev *ndev) | ||
| 133 | { | ||
| 134 | nci_unregister_device(ndev->nci_dev); | ||
| 135 | } | ||
| 136 | EXPORT_SYMBOL_GPL(nci_spi_unregister_device); | ||
