blob: 70afc387a9659f1bb28ea977b28aaebcfe8bc6c0 [file] [log] [blame]
Frederic Danis8a00a612013-05-29 15:35:02 +02001/*
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>
Frederic Danisee9596d2013-05-29 15:35:03 +020023#include <linux/crc-ccitt.h>
Frederic Danis8a00a612013-05-29 15:35:02 +020024#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
Frederic Danis391d8a22013-05-29 15:35:04 +020029#define NCI_SPI_ACK_SHIFT 6
30#define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
Frederic Danis8a00a612013-05-29 15:35:02 +020031
Frederic Danisee9596d2013-05-29 15:35:03 +020032#define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
33 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
34
35#define NCI_SPI_DIRECT_WRITE 0x01
36#define NCI_SPI_DIRECT_READ 0x02
37
38#define ACKNOWLEDGE_NONE 0
39#define ACKNOWLEDGE_ACK 1
40#define ACKNOWLEDGE_NACK 2
41
42#define CRC_INIT 0xFFFF
43
Frederic Danis8a00a612013-05-29 15:35:02 +020044static int nci_spi_open(struct nci_dev *nci_dev)
45{
46 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
47
48 return ndev->ops->open(ndev);
49}
50
51static int nci_spi_close(struct nci_dev *nci_dev)
52{
53 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
54
55 return ndev->ops->close(ndev);
56}
57
Frederic Danisee9596d2013-05-29 15:35:03 +020058static int __nci_spi_send(struct nci_spi_dev *ndev, struct sk_buff *skb)
59{
60 struct spi_message m;
61 struct spi_transfer t;
62
63 t.tx_buf = skb->data;
64 t.len = skb->len;
65 t.cs_change = 0;
66 t.delay_usecs = ndev->xfer_udelay;
67
68 spi_message_init(&m);
69 spi_message_add_tail(&t, &m);
70
71 return spi_sync(ndev->spi, &m);
72}
73
Frederic Danis8a00a612013-05-29 15:35:02 +020074static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb)
75{
Frederic Danisee9596d2013-05-29 15:35:03 +020076 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
77 unsigned int payload_len = skb->len;
78 unsigned char *hdr;
79 int ret;
80 long completion_rc;
81
82 ndev->ops->deassert_int(ndev);
83
84 /* add the NCI SPI header to the start of the buffer */
85 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
86 hdr[0] = NCI_SPI_DIRECT_WRITE;
87 hdr[1] = ndev->acknowledge_mode;
88 hdr[2] = payload_len >> 8;
89 hdr[3] = payload_len & 0xFF;
90
91 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
92 u16 crc;
93
94 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
95 *skb_put(skb, 1) = crc >> 8;
96 *skb_put(skb, 1) = crc & 0xFF;
97 }
98
99 ret = __nci_spi_send(ndev, skb);
100
101 kfree_skb(skb);
102 ndev->ops->assert_int(ndev);
103
104 if (ret != 0 || ndev->acknowledge_mode == NCI_SPI_CRC_DISABLED)
105 goto done;
106
107 init_completion(&ndev->req_completion);
108 completion_rc =
109 wait_for_completion_interruptible_timeout(&ndev->req_completion,
110 NCI_SPI_SEND_TIMEOUT);
111
112 if (completion_rc <= 0 || ndev->req_result == ACKNOWLEDGE_NACK)
113 ret = -EIO;
114
115done:
116 return ret;
Frederic Danis8a00a612013-05-29 15:35:02 +0200117}
118
119static struct nci_ops nci_spi_ops = {
120 .open = nci_spi_open,
121 .close = nci_spi_close,
122 .send = nci_spi_send,
123};
124
125/* ---- Interface to NCI SPI drivers ---- */
126
127/**
128 * nci_spi_allocate_device - allocate a new nci spi device
129 *
130 * @spi: SPI device
131 * @ops: device operations
132 * @supported_protocols: NFC protocols supported by the device
133 * @supported_se: NFC Secure Elements supported by the device
134 * @acknowledge_mode: Acknowledge mode used by the device
135 * @delay: delay between transactions in us
136 */
137struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi,
138 struct nci_spi_ops *ops,
139 u32 supported_protocols,
140 u32 supported_se,
141 u8 acknowledge_mode,
142 unsigned int delay)
143{
144 struct nci_spi_dev *ndev;
145 int tailroom = 0;
146
147 if (!ops->open || !ops->close || !ops->assert_int || !ops->deassert_int)
148 return NULL;
149
150 if (!supported_protocols)
151 return NULL;
152
153 ndev = devm_kzalloc(&spi->dev, sizeof(struct nci_dev), GFP_KERNEL);
154 if (!ndev)
155 return NULL;
156
157 ndev->ops = ops;
158 ndev->acknowledge_mode = acknowledge_mode;
159 ndev->xfer_udelay = delay;
160
161 if (acknowledge_mode == NCI_SPI_CRC_ENABLED)
162 tailroom += NCI_SPI_CRC_LEN;
163
164 ndev->nci_dev = nci_allocate_device(&nci_spi_ops, supported_protocols,
165 supported_se, NCI_SPI_HDR_LEN,
166 tailroom);
167 if (!ndev->nci_dev)
168 return NULL;
169
170 nci_set_drvdata(ndev->nci_dev, ndev);
171
172 return ndev;
173}
174EXPORT_SYMBOL_GPL(nci_spi_allocate_device);
175
176/**
177 * nci_spi_free_device - deallocate nci spi device
178 *
179 * @ndev: The nci spi device to deallocate
180 */
181void nci_spi_free_device(struct nci_spi_dev *ndev)
182{
183 nci_free_device(ndev->nci_dev);
184}
185EXPORT_SYMBOL_GPL(nci_spi_free_device);
186
187/**
188 * nci_spi_register_device - register a nci spi device in the nfc subsystem
189 *
190 * @pdev: The nci spi device to register
191 */
192int nci_spi_register_device(struct nci_spi_dev *ndev)
193{
194 return nci_register_device(ndev->nci_dev);
195}
196EXPORT_SYMBOL_GPL(nci_spi_register_device);
197
198/**
199 * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem
200 *
201 * @dev: The nci spi device to unregister
202 */
203void nci_spi_unregister_device(struct nci_spi_dev *ndev)
204{
205 nci_unregister_device(ndev->nci_dev);
206}
207EXPORT_SYMBOL_GPL(nci_spi_unregister_device);
Frederic Danis391d8a22013-05-29 15:35:04 +0200208
209static int send_acknowledge(struct nci_spi_dev *ndev, u8 acknowledge)
210{
211 struct sk_buff *skb;
212 unsigned char *hdr;
213 u16 crc;
214 int ret;
215
216 skb = nci_skb_alloc(ndev->nci_dev, 0, GFP_KERNEL);
217
218 /* add the NCI SPI header to the start of the buffer */
219 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
220 hdr[0] = NCI_SPI_DIRECT_WRITE;
221 hdr[1] = NCI_SPI_CRC_ENABLED;
222 hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
223 hdr[3] = 0;
224
225 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
226 *skb_put(skb, 1) = crc >> 8;
227 *skb_put(skb, 1) = crc & 0xFF;
228
229 ret = __nci_spi_send(ndev, skb);
230
231 kfree_skb(skb);
232
233 return ret;
234}
235
236static struct sk_buff *__nci_spi_recv_frame(struct nci_spi_dev *ndev)
237{
238 struct sk_buff *skb;
239 struct spi_message m;
240 unsigned char req[2], resp_hdr[2];
241 struct spi_transfer tx, rx;
242 unsigned short rx_len = 0;
243 int ret;
244
245 spi_message_init(&m);
246 req[0] = NCI_SPI_DIRECT_READ;
247 req[1] = ndev->acknowledge_mode;
248 tx.tx_buf = req;
249 tx.len = 2;
250 tx.cs_change = 0;
251 spi_message_add_tail(&tx, &m);
252 rx.rx_buf = resp_hdr;
253 rx.len = 2;
254 rx.cs_change = 1;
255 spi_message_add_tail(&rx, &m);
256 ret = spi_sync(ndev->spi, &m);
257
258 if (ret)
259 return NULL;
260
261 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED)
262 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
263 resp_hdr[1] + NCI_SPI_CRC_LEN;
264 else
265 rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
266
267 skb = nci_skb_alloc(ndev->nci_dev, rx_len, GFP_KERNEL);
268 if (!skb)
269 return NULL;
270
271 spi_message_init(&m);
272 rx.rx_buf = skb_put(skb, rx_len);
273 rx.len = rx_len;
274 rx.cs_change = 0;
275 rx.delay_usecs = ndev->xfer_udelay;
276 spi_message_add_tail(&rx, &m);
277 ret = spi_sync(ndev->spi, &m);
278
279 if (ret)
280 goto receive_error;
281
282 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
283 *skb_push(skb, 1) = resp_hdr[1];
284 *skb_push(skb, 1) = resp_hdr[0];
285 }
286
287 return skb;
288
289receive_error:
290 kfree_skb(skb);
291
292 return NULL;
293}
294
295static int nci_spi_check_crc(struct sk_buff *skb)
296{
297 u16 crc_data = (skb->data[skb->len - 2] << 8) |
298 skb->data[skb->len - 1];
299 int ret;
300
301 ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
302 == crc_data);
303
304 skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
305
306 return ret;
307}
308
309static u8 nci_spi_get_ack(struct sk_buff *skb)
310{
311 u8 ret;
312
313 ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
314
315 /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
316 skb_pull(skb, 2);
317
318 return ret;
319}
320
321/**
322 * nci_spi_recv_frame - receive frame from NCI SPI drivers
323 *
324 * @ndev: The nci spi device
325 * Context: can sleep
326 *
327 * This call may only be used from a context that may sleep. The sleep
328 * is non-interruptible, and has no timeout.
329 *
330 * It returns zero on success, else a negative error code.
331 */
332int nci_spi_recv_frame(struct nci_spi_dev *ndev)
333{
334 struct sk_buff *skb;
335 int ret = 0;
336
337 ndev->ops->deassert_int(ndev);
338
339 /* Retrieve frame from SPI */
340 skb = __nci_spi_recv_frame(ndev);
341 if (!skb) {
342 ret = -EIO;
343 goto done;
344 }
345
346 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
347 if (!nci_spi_check_crc(skb)) {
348 send_acknowledge(ndev, ACKNOWLEDGE_NACK);
349 goto done;
350 }
351
352 /* In case of acknowledged mode: if ACK or NACK received,
353 * unblock completion of latest frame sent.
354 */
355 ndev->req_result = nci_spi_get_ack(skb);
356 if (ndev->req_result)
357 complete(&ndev->req_completion);
358 }
359
360 /* If there is no payload (ACK/NACK only frame),
361 * free the socket buffer
362 */
363 if (skb->len == 0) {
364 kfree_skb(skb);
365 goto done;
366 }
367
368 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED)
369 send_acknowledge(ndev, ACKNOWLEDGE_ACK);
370
371 /* Forward skb to NCI core layer */
372 ret = nci_recv_frame(ndev->nci_dev, skb);
373
374done:
375 ndev->ops->assert_int(ndev);
376
377 return ret;
378}
379EXPORT_SYMBOL_GPL(nci_spi_recv_frame);