NFC: Add NCI over SPI send
[linux-2.6/btrfs-unstable.git] / net / nfc / nci / spi.c
blob6258461e6998f93f0e05b221446f7c480792eb97
1 /*
2 * Copyright (C) 2013 Intel Corporation. All rights reserved.
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.
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.
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.
19 #define pr_fmt(fmt) "nci_spi: %s: " fmt, __func__
21 #include <linux/export.h>
22 #include <linux/spi/spi.h>
23 #include <linux/crc-ccitt.h>
24 #include <linux/nfc.h>
25 #include <net/nfc/nci_core.h>
27 #define NCI_SPI_HDR_LEN 4
28 #define NCI_SPI_CRC_LEN 2
30 #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
31 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
33 #define NCI_SPI_DIRECT_WRITE 0x01
34 #define NCI_SPI_DIRECT_READ 0x02
36 #define ACKNOWLEDGE_NONE 0
37 #define ACKNOWLEDGE_ACK 1
38 #define ACKNOWLEDGE_NACK 2
40 #define CRC_INIT 0xFFFF
42 static int nci_spi_open(struct nci_dev *nci_dev)
44 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
46 return ndev->ops->open(ndev);
49 static int nci_spi_close(struct nci_dev *nci_dev)
51 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
53 return ndev->ops->close(ndev);
56 static int __nci_spi_send(struct nci_spi_dev *ndev, struct sk_buff *skb)
58 struct spi_message m;
59 struct spi_transfer t;
61 t.tx_buf = skb->data;
62 t.len = skb->len;
63 t.cs_change = 0;
64 t.delay_usecs = ndev->xfer_udelay;
66 spi_message_init(&m);
67 spi_message_add_tail(&t, &m);
69 return spi_sync(ndev->spi, &m);
72 static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb)
74 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
75 unsigned int payload_len = skb->len;
76 unsigned char *hdr;
77 int ret;
78 long completion_rc;
80 ndev->ops->deassert_int(ndev);
82 /* add the NCI SPI header to the start of the buffer */
83 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
84 hdr[0] = NCI_SPI_DIRECT_WRITE;
85 hdr[1] = ndev->acknowledge_mode;
86 hdr[2] = payload_len >> 8;
87 hdr[3] = payload_len & 0xFF;
89 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
90 u16 crc;
92 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
93 *skb_put(skb, 1) = crc >> 8;
94 *skb_put(skb, 1) = crc & 0xFF;
97 ret = __nci_spi_send(ndev, skb);
99 kfree_skb(skb);
100 ndev->ops->assert_int(ndev);
102 if (ret != 0 || ndev->acknowledge_mode == NCI_SPI_CRC_DISABLED)
103 goto done;
105 init_completion(&ndev->req_completion);
106 completion_rc =
107 wait_for_completion_interruptible_timeout(&ndev->req_completion,
108 NCI_SPI_SEND_TIMEOUT);
110 if (completion_rc <= 0 || ndev->req_result == ACKNOWLEDGE_NACK)
111 ret = -EIO;
113 done:
114 return ret;
117 static struct nci_ops nci_spi_ops = {
118 .open = nci_spi_open,
119 .close = nci_spi_close,
120 .send = nci_spi_send,
123 /* ---- Interface to NCI SPI drivers ---- */
126 * nci_spi_allocate_device - allocate a new nci spi device
128 * @spi: SPI device
129 * @ops: device operations
130 * @supported_protocols: NFC protocols supported by the device
131 * @supported_se: NFC Secure Elements supported by the device
132 * @acknowledge_mode: Acknowledge mode used by the device
133 * @delay: delay between transactions in us
135 struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi,
136 struct nci_spi_ops *ops,
137 u32 supported_protocols,
138 u32 supported_se,
139 u8 acknowledge_mode,
140 unsigned int delay)
142 struct nci_spi_dev *ndev;
143 int tailroom = 0;
145 if (!ops->open || !ops->close || !ops->assert_int || !ops->deassert_int)
146 return NULL;
148 if (!supported_protocols)
149 return NULL;
151 ndev = devm_kzalloc(&spi->dev, sizeof(struct nci_dev), GFP_KERNEL);
152 if (!ndev)
153 return NULL;
155 ndev->ops = ops;
156 ndev->acknowledge_mode = acknowledge_mode;
157 ndev->xfer_udelay = delay;
159 if (acknowledge_mode == NCI_SPI_CRC_ENABLED)
160 tailroom += NCI_SPI_CRC_LEN;
162 ndev->nci_dev = nci_allocate_device(&nci_spi_ops, supported_protocols,
163 supported_se, NCI_SPI_HDR_LEN,
164 tailroom);
165 if (!ndev->nci_dev)
166 return NULL;
168 nci_set_drvdata(ndev->nci_dev, ndev);
170 return ndev;
172 EXPORT_SYMBOL_GPL(nci_spi_allocate_device);
175 * nci_spi_free_device - deallocate nci spi device
177 * @ndev: The nci spi device to deallocate
179 void nci_spi_free_device(struct nci_spi_dev *ndev)
181 nci_free_device(ndev->nci_dev);
183 EXPORT_SYMBOL_GPL(nci_spi_free_device);
186 * nci_spi_register_device - register a nci spi device in the nfc subsystem
188 * @pdev: The nci spi device to register
190 int nci_spi_register_device(struct nci_spi_dev *ndev)
192 return nci_register_device(ndev->nci_dev);
194 EXPORT_SYMBOL_GPL(nci_spi_register_device);
197 * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem
199 * @dev: The nci spi device to unregister
201 void nci_spi_unregister_device(struct nci_spi_dev *ndev)
203 nci_unregister_device(ndev->nci_dev);
205 EXPORT_SYMBOL_GPL(nci_spi_unregister_device);