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
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 <net/nfc/nci_core.h>
26 #define NCI_SPI_ACK_SHIFT 6
27 #define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
29 #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
30 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
32 #define NCI_SPI_DIRECT_WRITE 0x01
33 #define NCI_SPI_DIRECT_READ 0x02
35 #define ACKNOWLEDGE_NONE 0
36 #define ACKNOWLEDGE_ACK 1
37 #define ACKNOWLEDGE_NACK 2
39 #define CRC_INIT 0xFFFF
41 static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb,
45 struct spi_transfer t;
47 memset(&t, 0, sizeof(struct spi_transfer));
48 /* a NULL skb means we just want the SPI chip select line to raise */
53 /* still set tx_buf non NULL to make the driver happy */
57 t.cs_change = cs_change;
58 t.delay_usecs = nspi->xfer_udelay;
61 spi_message_add_tail(&t, &m);
63 return spi_sync(nspi->spi, &m);
66 int nci_spi_send(struct nci_spi *nspi,
67 struct completion *write_handshake_completion,
70 unsigned int payload_len = skb->len;
75 /* add the NCI SPI header to the start of the buffer */
76 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
77 hdr[0] = NCI_SPI_DIRECT_WRITE;
78 hdr[1] = nspi->acknowledge_mode;
79 hdr[2] = payload_len >> 8;
80 hdr[3] = payload_len & 0xFF;
82 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
85 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
86 *skb_put(skb, 1) = crc >> 8;
87 *skb_put(skb, 1) = crc & 0xFF;
90 if (write_handshake_completion) {
91 /* Trick SPI driver to raise chip select */
92 ret = __nci_spi_send(nspi, NULL, 1);
96 /* wait for NFC chip hardware handshake to complete */
97 if (wait_for_completion_timeout(write_handshake_completion,
98 msecs_to_jiffies(1000)) == 0) {
104 ret = __nci_spi_send(nspi, skb, 0);
105 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
108 init_completion(&nspi->req_completion);
109 completion_rc = wait_for_completion_interruptible_timeout(
110 &nspi->req_completion,
111 NCI_SPI_SEND_TIMEOUT);
113 if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK)
121 EXPORT_SYMBOL_GPL(nci_spi_send);
123 /* ---- Interface to NCI SPI drivers ---- */
126 * nci_spi_allocate_spi - allocate a new nci spi
129 * @acknowledge_mode: Acknowledge mode used by the NFC device
130 * @delay: delay between transactions in us
131 * @ndev: nci dev to send incoming nci frames to
133 struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
134 u8 acknowledge_mode, unsigned int delay,
135 struct nci_dev *ndev)
137 struct nci_spi *nspi;
139 nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL);
143 nspi->acknowledge_mode = acknowledge_mode;
144 nspi->xfer_udelay = delay;
151 EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
153 static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
160 skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
162 /* add the NCI SPI header to the start of the buffer */
163 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
164 hdr[0] = NCI_SPI_DIRECT_WRITE;
165 hdr[1] = NCI_SPI_CRC_ENABLED;
166 hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
169 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
170 *skb_put(skb, 1) = crc >> 8;
171 *skb_put(skb, 1) = crc & 0xFF;
173 ret = __nci_spi_send(nspi, skb, 0);
180 static struct sk_buff *__nci_spi_read(struct nci_spi *nspi)
183 struct spi_message m;
184 unsigned char req[2], resp_hdr[2];
185 struct spi_transfer tx, rx;
186 unsigned short rx_len = 0;
189 spi_message_init(&m);
191 memset(&tx, 0, sizeof(struct spi_transfer));
192 req[0] = NCI_SPI_DIRECT_READ;
193 req[1] = nspi->acknowledge_mode;
197 spi_message_add_tail(&tx, &m);
199 memset(&rx, 0, sizeof(struct spi_transfer));
200 rx.rx_buf = resp_hdr;
203 spi_message_add_tail(&rx, &m);
205 ret = spi_sync(nspi->spi, &m);
209 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
210 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
211 resp_hdr[1] + NCI_SPI_CRC_LEN;
213 rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
215 skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
219 spi_message_init(&m);
221 memset(&rx, 0, sizeof(struct spi_transfer));
222 rx.rx_buf = skb_put(skb, rx_len);
225 rx.delay_usecs = nspi->xfer_udelay;
226 spi_message_add_tail(&rx, &m);
228 ret = spi_sync(nspi->spi, &m);
232 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
233 *skb_push(skb, 1) = resp_hdr[1];
234 *skb_push(skb, 1) = resp_hdr[0];
245 static int nci_spi_check_crc(struct sk_buff *skb)
247 u16 crc_data = (skb->data[skb->len - 2] << 8) |
248 skb->data[skb->len - 1];
251 ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
254 skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
259 static u8 nci_spi_get_ack(struct sk_buff *skb)
263 ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
265 /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
272 * nci_spi_read - read frame from NCI SPI drivers
277 * This call may only be used from a context that may sleep. The sleep
278 * is non-interruptible, and has no timeout.
280 * It returns an allocated skb containing the frame on success, or NULL.
282 struct sk_buff *nci_spi_read(struct nci_spi *nspi)
286 /* Retrieve frame from SPI */
287 skb = __nci_spi_read(nspi);
291 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
292 if (!nci_spi_check_crc(skb)) {
293 send_acknowledge(nspi, ACKNOWLEDGE_NACK);
297 /* In case of acknowledged mode: if ACK or NACK received,
298 * unblock completion of latest frame sent.
300 nspi->req_result = nci_spi_get_ack(skb);
301 if (nspi->req_result)
302 complete(&nspi->req_completion);
305 /* If there is no payload (ACK/NACK only frame),
306 * free the socket buffer
314 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
315 send_acknowledge(nspi, ACKNOWLEDGE_ACK);
321 EXPORT_SYMBOL_GPL(nci_spi_read);