blob: d904cd2f14423ce108e9d4bfc28b22be353d93f2 [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
Vincent Cuissardfcd9d042015-10-26 10:27:42 +010021#include <linux/module.h>
22
Frederic Danis8a00a612013-05-29 15:35:02 +020023#include <linux/export.h>
24#include <linux/spi/spi.h>
Frederic Danisee9596d2013-05-29 15:35:03 +020025#include <linux/crc-ccitt.h>
Frederic Danis8a00a612013-05-29 15:35:02 +020026#include <net/nfc/nci_core.h>
27
Frederic Danis391d8a22013-05-29 15:35:04 +020028#define NCI_SPI_ACK_SHIFT 6
29#define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
Frederic Danis8a00a612013-05-29 15:35:02 +020030
Frederic Danisee9596d2013-05-29 15:35:03 +020031#define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
32 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
33
34#define NCI_SPI_DIRECT_WRITE 0x01
35#define NCI_SPI_DIRECT_READ 0x02
36
37#define ACKNOWLEDGE_NONE 0
38#define ACKNOWLEDGE_ACK 1
39#define ACKNOWLEDGE_NACK 2
40
41#define CRC_INIT 0xFFFF
42
Eric Lapuyade2bed2782013-09-23 17:56:43 +020043static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb,
44 int cs_change)
Frederic Danisee9596d2013-05-29 15:35:03 +020045{
46 struct spi_message m;
47 struct spi_transfer t;
48
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +020049 memset(&t, 0, sizeof(struct spi_transfer));
Eric Lapuyade2bed2782013-09-23 17:56:43 +020050 /* a NULL skb means we just want the SPI chip select line to raise */
51 if (skb) {
52 t.tx_buf = skb->data;
53 t.len = skb->len;
54 } else {
55 /* still set tx_buf non NULL to make the driver happy */
56 t.tx_buf = &t;
57 t.len = 0;
58 }
59 t.cs_change = cs_change;
Eric Lapuyadefa544ff2013-09-05 11:02:21 +020060 t.delay_usecs = nspi->xfer_udelay;
Vincent Cuissard2bd83242015-10-26 10:27:43 +010061 t.speed_hz = nspi->xfer_speed_hz;
Frederic Danisee9596d2013-05-29 15:35:03 +020062
63 spi_message_init(&m);
64 spi_message_add_tail(&t, &m);
65
Eric Lapuyadefa544ff2013-09-05 11:02:21 +020066 return spi_sync(nspi->spi, &m);
Frederic Danisee9596d2013-05-29 15:35:03 +020067}
68
Eric Lapuyade2bed2782013-09-23 17:56:43 +020069int nci_spi_send(struct nci_spi *nspi,
70 struct completion *write_handshake_completion,
71 struct sk_buff *skb)
Frederic Danis8a00a612013-05-29 15:35:02 +020072{
Frederic Danisee9596d2013-05-29 15:35:03 +020073 unsigned int payload_len = skb->len;
74 unsigned char *hdr;
75 int ret;
76 long completion_rc;
77
Frederic Danisee9596d2013-05-29 15:35:03 +020078 /* add the NCI SPI header to the start of the buffer */
79 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
80 hdr[0] = NCI_SPI_DIRECT_WRITE;
Eric Lapuyadefa544ff2013-09-05 11:02:21 +020081 hdr[1] = nspi->acknowledge_mode;
Frederic Danisee9596d2013-05-29 15:35:03 +020082 hdr[2] = payload_len >> 8;
83 hdr[3] = payload_len & 0xFF;
84
Eric Lapuyadefa544ff2013-09-05 11:02:21 +020085 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
Frederic Danisee9596d2013-05-29 15:35:03 +020086 u16 crc;
87
88 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
89 *skb_put(skb, 1) = crc >> 8;
90 *skb_put(skb, 1) = crc & 0xFF;
91 }
92
Eric Lapuyade2bed2782013-09-23 17:56:43 +020093 if (write_handshake_completion) {
94 /* Trick SPI driver to raise chip select */
95 ret = __nci_spi_send(nspi, NULL, 1);
96 if (ret)
97 goto done;
Frederic Danisee9596d2013-05-29 15:35:03 +020098
Eric Lapuyade2bed2782013-09-23 17:56:43 +020099 /* wait for NFC chip hardware handshake to complete */
100 if (wait_for_completion_timeout(write_handshake_completion,
101 msecs_to_jiffies(1000)) == 0) {
102 ret = -ETIME;
103 goto done;
104 }
105 }
Frederic Danisee9596d2013-05-29 15:35:03 +0200106
Eric Lapuyade2bed2782013-09-23 17:56:43 +0200107 ret = __nci_spi_send(nspi, skb, 0);
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200108 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
Frederic Danisee9596d2013-05-29 15:35:03 +0200109 goto done;
110
Axel Lin9bec44b2014-02-13 13:25:48 +0800111 reinit_completion(&nspi->req_completion);
Eric Lapuyaded5937512013-09-02 12:35:39 +0200112 completion_rc = wait_for_completion_interruptible_timeout(
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200113 &nspi->req_completion,
Eric Lapuyaded5937512013-09-02 12:35:39 +0200114 NCI_SPI_SEND_TIMEOUT);
Frederic Danisee9596d2013-05-29 15:35:03 +0200115
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200116 if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK)
Frederic Danisee9596d2013-05-29 15:35:03 +0200117 ret = -EIO;
118
119done:
Eric Lapuyade2bed2782013-09-23 17:56:43 +0200120 kfree_skb(skb);
121
Frederic Danisee9596d2013-05-29 15:35:03 +0200122 return ret;
Frederic Danis8a00a612013-05-29 15:35:02 +0200123}
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200124EXPORT_SYMBOL_GPL(nci_spi_send);
Frederic Danis8a00a612013-05-29 15:35:02 +0200125
126/* ---- Interface to NCI SPI drivers ---- */
127
128/**
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200129 * nci_spi_allocate_spi - allocate a new nci spi
Frederic Danis8a00a612013-05-29 15:35:02 +0200130 *
131 * @spi: SPI device
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200132 * @acknowledge_mode: Acknowledge mode used by the NFC device
Frederic Danis8a00a612013-05-29 15:35:02 +0200133 * @delay: delay between transactions in us
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200134 * @ndev: nci dev to send incoming nci frames to
Frederic Danis8a00a612013-05-29 15:35:02 +0200135 */
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200136struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200137 u8 acknowledge_mode, unsigned int delay,
138 struct nci_dev *ndev)
Frederic Danis8a00a612013-05-29 15:35:02 +0200139{
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200140 struct nci_spi *nspi;
Frederic Danis8a00a612013-05-29 15:35:02 +0200141
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200142 nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL);
143 if (!nspi)
Frederic Danis8a00a612013-05-29 15:35:02 +0200144 return NULL;
145
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200146 nspi->acknowledge_mode = acknowledge_mode;
147 nspi->xfer_udelay = delay;
Vincent Cuissard2bd83242015-10-26 10:27:43 +0100148 /* Use controller max SPI speed by default */
149 nspi->xfer_speed_hz = 0;
Eric Lapuyade645d5082013-09-19 16:52:06 +0200150 nspi->spi = spi;
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200151 nspi->ndev = ndev;
Axel Lin9bec44b2014-02-13 13:25:48 +0800152 init_completion(&nspi->req_completion);
Frederic Danis8a00a612013-05-29 15:35:02 +0200153
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200154 return nspi;
Frederic Danis8a00a612013-05-29 15:35:02 +0200155}
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200156EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
Frederic Danis8a00a612013-05-29 15:35:02 +0200157
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200158static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
Frederic Danis391d8a22013-05-29 15:35:04 +0200159{
160 struct sk_buff *skb;
161 unsigned char *hdr;
162 u16 crc;
163 int ret;
164
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200165 skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
Frederic Danis391d8a22013-05-29 15:35:04 +0200166
167 /* add the NCI SPI header to the start of the buffer */
168 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
169 hdr[0] = NCI_SPI_DIRECT_WRITE;
170 hdr[1] = NCI_SPI_CRC_ENABLED;
171 hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
172 hdr[3] = 0;
173
174 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
175 *skb_put(skb, 1) = crc >> 8;
176 *skb_put(skb, 1) = crc & 0xFF;
177
Eric Lapuyade2bed2782013-09-23 17:56:43 +0200178 ret = __nci_spi_send(nspi, skb, 0);
Frederic Danis391d8a22013-05-29 15:35:04 +0200179
180 kfree_skb(skb);
181
182 return ret;
183}
184
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200185static struct sk_buff *__nci_spi_read(struct nci_spi *nspi)
Frederic Danis391d8a22013-05-29 15:35:04 +0200186{
187 struct sk_buff *skb;
188 struct spi_message m;
189 unsigned char req[2], resp_hdr[2];
190 struct spi_transfer tx, rx;
191 unsigned short rx_len = 0;
192 int ret;
193
194 spi_message_init(&m);
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +0200195
196 memset(&tx, 0, sizeof(struct spi_transfer));
Frederic Danis391d8a22013-05-29 15:35:04 +0200197 req[0] = NCI_SPI_DIRECT_READ;
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200198 req[1] = nspi->acknowledge_mode;
Frederic Danis391d8a22013-05-29 15:35:04 +0200199 tx.tx_buf = req;
200 tx.len = 2;
201 tx.cs_change = 0;
Vincent Cuissard2bd83242015-10-26 10:27:43 +0100202 tx.speed_hz = nspi->xfer_speed_hz;
Frederic Danis391d8a22013-05-29 15:35:04 +0200203 spi_message_add_tail(&tx, &m);
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +0200204
205 memset(&rx, 0, sizeof(struct spi_transfer));
Frederic Danis391d8a22013-05-29 15:35:04 +0200206 rx.rx_buf = resp_hdr;
207 rx.len = 2;
208 rx.cs_change = 1;
Vincent Cuissard2bd83242015-10-26 10:27:43 +0100209 rx.speed_hz = nspi->xfer_speed_hz;
Frederic Danis391d8a22013-05-29 15:35:04 +0200210 spi_message_add_tail(&rx, &m);
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +0200211
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200212 ret = spi_sync(nspi->spi, &m);
Frederic Danis391d8a22013-05-29 15:35:04 +0200213 if (ret)
214 return NULL;
215
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200216 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
Frederic Danis391d8a22013-05-29 15:35:04 +0200217 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
218 resp_hdr[1] + NCI_SPI_CRC_LEN;
219 else
220 rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
221
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200222 skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
Frederic Danis391d8a22013-05-29 15:35:04 +0200223 if (!skb)
224 return NULL;
225
226 spi_message_init(&m);
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +0200227
228 memset(&rx, 0, sizeof(struct spi_transfer));
Frederic Danis391d8a22013-05-29 15:35:04 +0200229 rx.rx_buf = skb_put(skb, rx_len);
230 rx.len = rx_len;
231 rx.cs_change = 0;
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200232 rx.delay_usecs = nspi->xfer_udelay;
Vincent Cuissard2bd83242015-10-26 10:27:43 +0100233 rx.speed_hz = nspi->xfer_speed_hz;
Frederic Danis391d8a22013-05-29 15:35:04 +0200234 spi_message_add_tail(&rx, &m);
Eric Lapuyadea4ada6c2013-09-23 18:02:43 +0200235
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200236 ret = spi_sync(nspi->spi, &m);
Frederic Danis391d8a22013-05-29 15:35:04 +0200237 if (ret)
238 goto receive_error;
239
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200240 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
Frederic Danis391d8a22013-05-29 15:35:04 +0200241 *skb_push(skb, 1) = resp_hdr[1];
242 *skb_push(skb, 1) = resp_hdr[0];
243 }
244
245 return skb;
246
247receive_error:
248 kfree_skb(skb);
249
250 return NULL;
251}
252
253static int nci_spi_check_crc(struct sk_buff *skb)
254{
255 u16 crc_data = (skb->data[skb->len - 2] << 8) |
256 skb->data[skb->len - 1];
257 int ret;
258
259 ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
260 == crc_data);
261
262 skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
263
264 return ret;
265}
266
267static u8 nci_spi_get_ack(struct sk_buff *skb)
268{
269 u8 ret;
270
271 ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
272
273 /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
274 skb_pull(skb, 2);
275
276 return ret;
277}
278
279/**
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200280 * nci_spi_read - read frame from NCI SPI drivers
Frederic Danis391d8a22013-05-29 15:35:04 +0200281 *
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200282 * @nspi: The nci spi
Frederic Danis391d8a22013-05-29 15:35:04 +0200283 * Context: can sleep
284 *
285 * This call may only be used from a context that may sleep. The sleep
286 * is non-interruptible, and has no timeout.
287 *
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200288 * It returns an allocated skb containing the frame on success, or NULL.
Frederic Danis391d8a22013-05-29 15:35:04 +0200289 */
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200290struct sk_buff *nci_spi_read(struct nci_spi *nspi)
Frederic Danis391d8a22013-05-29 15:35:04 +0200291{
292 struct sk_buff *skb;
Frederic Danis391d8a22013-05-29 15:35:04 +0200293
Frederic Danis391d8a22013-05-29 15:35:04 +0200294 /* Retrieve frame from SPI */
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200295 skb = __nci_spi_read(nspi);
296 if (!skb)
Frederic Danis391d8a22013-05-29 15:35:04 +0200297 goto done;
Frederic Danis391d8a22013-05-29 15:35:04 +0200298
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200299 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
Frederic Danis391d8a22013-05-29 15:35:04 +0200300 if (!nci_spi_check_crc(skb)) {
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200301 send_acknowledge(nspi, ACKNOWLEDGE_NACK);
Frederic Danis391d8a22013-05-29 15:35:04 +0200302 goto done;
303 }
304
305 /* In case of acknowledged mode: if ACK or NACK received,
306 * unblock completion of latest frame sent.
307 */
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200308 nspi->req_result = nci_spi_get_ack(skb);
309 if (nspi->req_result)
310 complete(&nspi->req_completion);
Frederic Danis391d8a22013-05-29 15:35:04 +0200311 }
312
313 /* If there is no payload (ACK/NACK only frame),
314 * free the socket buffer
315 */
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200316 if (!skb->len) {
Frederic Danis391d8a22013-05-29 15:35:04 +0200317 kfree_skb(skb);
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200318 skb = NULL;
Frederic Danis391d8a22013-05-29 15:35:04 +0200319 goto done;
320 }
321
Eric Lapuyadefa544ff2013-09-05 11:02:21 +0200322 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
323 send_acknowledge(nspi, ACKNOWLEDGE_ACK);
Frederic Danis391d8a22013-05-29 15:35:04 +0200324
Frederic Danis391d8a22013-05-29 15:35:04 +0200325done:
Frederic Danis391d8a22013-05-29 15:35:04 +0200326
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200327 return skb;
Frederic Danis391d8a22013-05-29 15:35:04 +0200328}
Eric Lapuyade22d4aae2013-09-23 17:56:31 +0200329EXPORT_SYMBOL_GPL(nci_spi_read);
Vincent Cuissardfcd9d042015-10-26 10:27:42 +0100330
331MODULE_LICENSE("GPL");