NFC: Add NCI over SPI send
[deliverable/linux.git] / net / nfc / nci / spi.c
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/crc-ccitt.h>
24 #include <linux/nfc.h>
25 #include <net/nfc/nci_core.h>
26
27 #define NCI_SPI_HDR_LEN 4
28 #define NCI_SPI_CRC_LEN 2
29
30 #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
31 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
32
33 #define NCI_SPI_DIRECT_WRITE 0x01
34 #define NCI_SPI_DIRECT_READ 0x02
35
36 #define ACKNOWLEDGE_NONE 0
37 #define ACKNOWLEDGE_ACK 1
38 #define ACKNOWLEDGE_NACK 2
39
40 #define CRC_INIT 0xFFFF
41
42 static int nci_spi_open(struct nci_dev *nci_dev)
43 {
44 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
45
46 return ndev->ops->open(ndev);
47 }
48
49 static int nci_spi_close(struct nci_dev *nci_dev)
50 {
51 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
52
53 return ndev->ops->close(ndev);
54 }
55
56 static int __nci_spi_send(struct nci_spi_dev *ndev, struct sk_buff *skb)
57 {
58 struct spi_message m;
59 struct spi_transfer t;
60
61 t.tx_buf = skb->data;
62 t.len = skb->len;
63 t.cs_change = 0;
64 t.delay_usecs = ndev->xfer_udelay;
65
66 spi_message_init(&m);
67 spi_message_add_tail(&t, &m);
68
69 return spi_sync(ndev->spi, &m);
70 }
71
72 static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb)
73 {
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;
79
80 ndev->ops->deassert_int(ndev);
81
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;
88
89 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
90 u16 crc;
91
92 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
93 *skb_put(skb, 1) = crc >> 8;
94 *skb_put(skb, 1) = crc & 0xFF;
95 }
96
97 ret = __nci_spi_send(ndev, skb);
98
99 kfree_skb(skb);
100 ndev->ops->assert_int(ndev);
101
102 if (ret != 0 || ndev->acknowledge_mode == NCI_SPI_CRC_DISABLED)
103 goto done;
104
105 init_completion(&ndev->req_completion);
106 completion_rc =
107 wait_for_completion_interruptible_timeout(&ndev->req_completion,
108 NCI_SPI_SEND_TIMEOUT);
109
110 if (completion_rc <= 0 || ndev->req_result == ACKNOWLEDGE_NACK)
111 ret = -EIO;
112
113 done:
114 return ret;
115 }
116
117 static struct nci_ops nci_spi_ops = {
118 .open = nci_spi_open,
119 .close = nci_spi_close,
120 .send = nci_spi_send,
121 };
122
123 /* ---- Interface to NCI SPI drivers ---- */
124
125 /**
126 * nci_spi_allocate_device - allocate a new nci spi device
127 *
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
134 */
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)
141 {
142 struct nci_spi_dev *ndev;
143 int tailroom = 0;
144
145 if (!ops->open || !ops->close || !ops->assert_int || !ops->deassert_int)
146 return NULL;
147
148 if (!supported_protocols)
149 return NULL;
150
151 ndev = devm_kzalloc(&spi->dev, sizeof(struct nci_dev), GFP_KERNEL);
152 if (!ndev)
153 return NULL;
154
155 ndev->ops = ops;
156 ndev->acknowledge_mode = acknowledge_mode;
157 ndev->xfer_udelay = delay;
158
159 if (acknowledge_mode == NCI_SPI_CRC_ENABLED)
160 tailroom += NCI_SPI_CRC_LEN;
161
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;
167
168 nci_set_drvdata(ndev->nci_dev, ndev);
169
170 return ndev;
171 }
172 EXPORT_SYMBOL_GPL(nci_spi_allocate_device);
173
174 /**
175 * nci_spi_free_device - deallocate nci spi device
176 *
177 * @ndev: The nci spi device to deallocate
178 */
179 void nci_spi_free_device(struct nci_spi_dev *ndev)
180 {
181 nci_free_device(ndev->nci_dev);
182 }
183 EXPORT_SYMBOL_GPL(nci_spi_free_device);
184
185 /**
186 * nci_spi_register_device - register a nci spi device in the nfc subsystem
187 *
188 * @pdev: The nci spi device to register
189 */
190 int nci_spi_register_device(struct nci_spi_dev *ndev)
191 {
192 return nci_register_device(ndev->nci_dev);
193 }
194 EXPORT_SYMBOL_GPL(nci_spi_register_device);
195
196 /**
197 * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem
198 *
199 * @dev: The nci spi device to unregister
200 */
201 void nci_spi_unregister_device(struct nci_spi_dev *ndev)
202 {
203 nci_unregister_device(ndev->nci_dev);
204 }
205 EXPORT_SYMBOL_GPL(nci_spi_unregister_device);
This page took 0.034072 seconds and 5 git commands to generate.