Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Intel Corporation. All rights reserved. |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) "nci_spi: %s: " fmt, __func__ |
| 7 | |
Vincent Cuissard | fcd9d04 | 2015-10-26 10:27:42 +0100 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 10 | #include <linux/export.h> |
| 11 | #include <linux/spi/spi.h> |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 12 | #include <linux/crc-ccitt.h> |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 13 | #include <net/nfc/nci_core.h> |
| 14 | |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 15 | #define NCI_SPI_ACK_SHIFT 6 |
| 16 | #define NCI_SPI_MSB_PAYLOAD_MASK 0x3F |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 17 | |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 18 | #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \ |
| 19 | NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT) |
| 20 | |
| 21 | #define NCI_SPI_DIRECT_WRITE 0x01 |
| 22 | #define NCI_SPI_DIRECT_READ 0x02 |
| 23 | |
| 24 | #define ACKNOWLEDGE_NONE 0 |
| 25 | #define ACKNOWLEDGE_ACK 1 |
| 26 | #define ACKNOWLEDGE_NACK 2 |
| 27 | |
| 28 | #define CRC_INIT 0xFFFF |
| 29 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 30 | static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb, |
| 31 | int cs_change) |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 32 | { |
| 33 | struct spi_message m; |
| 34 | struct spi_transfer t; |
| 35 | |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 36 | memset(&t, 0, sizeof(struct spi_transfer)); |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 37 | /* a NULL skb means we just want the SPI chip select line to raise */ |
| 38 | if (skb) { |
| 39 | t.tx_buf = skb->data; |
| 40 | t.len = skb->len; |
| 41 | } else { |
| 42 | /* still set tx_buf non NULL to make the driver happy */ |
| 43 | t.tx_buf = &t; |
| 44 | t.len = 0; |
| 45 | } |
| 46 | t.cs_change = cs_change; |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 47 | t.delay_usecs = nspi->xfer_udelay; |
Vincent Cuissard | 2bd8324 | 2015-10-26 10:27:43 +0100 | [diff] [blame] | 48 | t.speed_hz = nspi->xfer_speed_hz; |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 49 | |
| 50 | spi_message_init(&m); |
| 51 | spi_message_add_tail(&t, &m); |
| 52 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 53 | return spi_sync(nspi->spi, &m); |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 56 | int nci_spi_send(struct nci_spi *nspi, |
| 57 | struct completion *write_handshake_completion, |
| 58 | struct sk_buff *skb) |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 59 | { |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 60 | unsigned int payload_len = skb->len; |
| 61 | unsigned char *hdr; |
| 62 | int ret; |
| 63 | long completion_rc; |
| 64 | |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 65 | /* add the NCI SPI header to the start of the buffer */ |
| 66 | hdr = skb_push(skb, NCI_SPI_HDR_LEN); |
| 67 | hdr[0] = NCI_SPI_DIRECT_WRITE; |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 68 | hdr[1] = nspi->acknowledge_mode; |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 69 | hdr[2] = payload_len >> 8; |
| 70 | hdr[3] = payload_len & 0xFF; |
| 71 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 72 | if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) { |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 73 | u16 crc; |
| 74 | |
| 75 | crc = crc_ccitt(CRC_INIT, skb->data, skb->len); |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 76 | skb_put_u8(skb, crc >> 8); |
| 77 | skb_put_u8(skb, crc & 0xFF); |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 80 | if (write_handshake_completion) { |
| 81 | /* Trick SPI driver to raise chip select */ |
| 82 | ret = __nci_spi_send(nspi, NULL, 1); |
| 83 | if (ret) |
| 84 | goto done; |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 85 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 86 | /* wait for NFC chip hardware handshake to complete */ |
| 87 | if (wait_for_completion_timeout(write_handshake_completion, |
| 88 | msecs_to_jiffies(1000)) == 0) { |
| 89 | ret = -ETIME; |
| 90 | goto done; |
| 91 | } |
| 92 | } |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 93 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 94 | ret = __nci_spi_send(nspi, skb, 0); |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 95 | if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED) |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 96 | goto done; |
| 97 | |
Axel Lin | 9bec44b | 2014-02-13 13:25:48 +0800 | [diff] [blame] | 98 | reinit_completion(&nspi->req_completion); |
Eric Lapuyade | d593751 | 2013-09-02 12:35:39 +0200 | [diff] [blame] | 99 | completion_rc = wait_for_completion_interruptible_timeout( |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 100 | &nspi->req_completion, |
Eric Lapuyade | d593751 | 2013-09-02 12:35:39 +0200 | [diff] [blame] | 101 | NCI_SPI_SEND_TIMEOUT); |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 102 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 103 | if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK) |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 104 | ret = -EIO; |
| 105 | |
| 106 | done: |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 107 | kfree_skb(skb); |
| 108 | |
Frederic Danis | ee9596d | 2013-05-29 15:35:03 +0200 | [diff] [blame] | 109 | return ret; |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 110 | } |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 111 | EXPORT_SYMBOL_GPL(nci_spi_send); |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 112 | |
| 113 | /* ---- Interface to NCI SPI drivers ---- */ |
| 114 | |
| 115 | /** |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 116 | * nci_spi_allocate_spi - allocate a new nci spi |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 117 | * |
| 118 | * @spi: SPI device |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 119 | * @acknowledge_mode: Acknowledge mode used by the NFC device |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 120 | * @delay: delay between transactions in us |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 121 | * @ndev: nci dev to send incoming nci frames to |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 122 | */ |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 123 | struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi, |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 124 | u8 acknowledge_mode, unsigned int delay, |
| 125 | struct nci_dev *ndev) |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 126 | { |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 127 | struct nci_spi *nspi; |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 128 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 129 | nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL); |
| 130 | if (!nspi) |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 131 | return NULL; |
| 132 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 133 | nspi->acknowledge_mode = acknowledge_mode; |
| 134 | nspi->xfer_udelay = delay; |
Vincent Cuissard | 2bd8324 | 2015-10-26 10:27:43 +0100 | [diff] [blame] | 135 | /* Use controller max SPI speed by default */ |
| 136 | nspi->xfer_speed_hz = 0; |
Eric Lapuyade | 645d508 | 2013-09-19 16:52:06 +0200 | [diff] [blame] | 137 | nspi->spi = spi; |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 138 | nspi->ndev = ndev; |
Axel Lin | 9bec44b | 2014-02-13 13:25:48 +0800 | [diff] [blame] | 139 | init_completion(&nspi->req_completion); |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 140 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 141 | return nspi; |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 142 | } |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 143 | EXPORT_SYMBOL_GPL(nci_spi_allocate_spi); |
Frederic Danis | 8a00a61 | 2013-05-29 15:35:02 +0200 | [diff] [blame] | 144 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 145 | static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge) |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 146 | { |
| 147 | struct sk_buff *skb; |
| 148 | unsigned char *hdr; |
| 149 | u16 crc; |
| 150 | int ret; |
| 151 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 152 | skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 153 | |
| 154 | /* add the NCI SPI header to the start of the buffer */ |
| 155 | hdr = skb_push(skb, NCI_SPI_HDR_LEN); |
| 156 | hdr[0] = NCI_SPI_DIRECT_WRITE; |
| 157 | hdr[1] = NCI_SPI_CRC_ENABLED; |
| 158 | hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT; |
| 159 | hdr[3] = 0; |
| 160 | |
| 161 | crc = crc_ccitt(CRC_INIT, skb->data, skb->len); |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 162 | skb_put_u8(skb, crc >> 8); |
| 163 | skb_put_u8(skb, crc & 0xFF); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 164 | |
Eric Lapuyade | 2bed278 | 2013-09-23 17:56:43 +0200 | [diff] [blame] | 165 | ret = __nci_spi_send(nspi, skb, 0); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 166 | |
| 167 | kfree_skb(skb); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 172 | static struct sk_buff *__nci_spi_read(struct nci_spi *nspi) |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 173 | { |
| 174 | struct sk_buff *skb; |
| 175 | struct spi_message m; |
| 176 | unsigned char req[2], resp_hdr[2]; |
| 177 | struct spi_transfer tx, rx; |
| 178 | unsigned short rx_len = 0; |
| 179 | int ret; |
| 180 | |
| 181 | spi_message_init(&m); |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 182 | |
| 183 | memset(&tx, 0, sizeof(struct spi_transfer)); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 184 | req[0] = NCI_SPI_DIRECT_READ; |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 185 | req[1] = nspi->acknowledge_mode; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 186 | tx.tx_buf = req; |
| 187 | tx.len = 2; |
| 188 | tx.cs_change = 0; |
Vincent Cuissard | 2bd8324 | 2015-10-26 10:27:43 +0100 | [diff] [blame] | 189 | tx.speed_hz = nspi->xfer_speed_hz; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 190 | spi_message_add_tail(&tx, &m); |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 191 | |
| 192 | memset(&rx, 0, sizeof(struct spi_transfer)); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 193 | rx.rx_buf = resp_hdr; |
| 194 | rx.len = 2; |
| 195 | rx.cs_change = 1; |
Vincent Cuissard | 2bd8324 | 2015-10-26 10:27:43 +0100 | [diff] [blame] | 196 | rx.speed_hz = nspi->xfer_speed_hz; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 197 | spi_message_add_tail(&rx, &m); |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 198 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 199 | ret = spi_sync(nspi->spi, &m); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 200 | if (ret) |
| 201 | return NULL; |
| 202 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 203 | if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 204 | rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) + |
| 205 | resp_hdr[1] + NCI_SPI_CRC_LEN; |
| 206 | else |
| 207 | rx_len = (resp_hdr[0] << 8) | resp_hdr[1]; |
| 208 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 209 | skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 210 | if (!skb) |
| 211 | return NULL; |
| 212 | |
| 213 | spi_message_init(&m); |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 214 | |
| 215 | memset(&rx, 0, sizeof(struct spi_transfer)); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 216 | rx.rx_buf = skb_put(skb, rx_len); |
| 217 | rx.len = rx_len; |
| 218 | rx.cs_change = 0; |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 219 | rx.delay_usecs = nspi->xfer_udelay; |
Vincent Cuissard | 2bd8324 | 2015-10-26 10:27:43 +0100 | [diff] [blame] | 220 | rx.speed_hz = nspi->xfer_speed_hz; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 221 | spi_message_add_tail(&rx, &m); |
Eric Lapuyade | a4ada6c | 2013-09-23 18:02:43 +0200 | [diff] [blame] | 222 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 223 | ret = spi_sync(nspi->spi, &m); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 224 | if (ret) |
| 225 | goto receive_error; |
| 226 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 227 | if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) { |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 228 | *(u8 *)skb_push(skb, 1) = resp_hdr[1]; |
| 229 | *(u8 *)skb_push(skb, 1) = resp_hdr[0]; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | return skb; |
| 233 | |
| 234 | receive_error: |
| 235 | kfree_skb(skb); |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static int nci_spi_check_crc(struct sk_buff *skb) |
| 241 | { |
| 242 | u16 crc_data = (skb->data[skb->len - 2] << 8) | |
| 243 | skb->data[skb->len - 1]; |
| 244 | int ret; |
| 245 | |
| 246 | ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN) |
| 247 | == crc_data); |
| 248 | |
| 249 | skb_trim(skb, skb->len - NCI_SPI_CRC_LEN); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static u8 nci_spi_get_ack(struct sk_buff *skb) |
| 255 | { |
| 256 | u8 ret; |
| 257 | |
| 258 | ret = skb->data[0] >> NCI_SPI_ACK_SHIFT; |
| 259 | |
| 260 | /* Remove NFCC part of the header: ACK, NACK and MSB payload len */ |
| 261 | skb_pull(skb, 2); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | /** |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 267 | * nci_spi_read - read frame from NCI SPI drivers |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 268 | * |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 269 | * @nspi: The nci spi |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 270 | * Context: can sleep |
| 271 | * |
| 272 | * This call may only be used from a context that may sleep. The sleep |
| 273 | * is non-interruptible, and has no timeout. |
| 274 | * |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 275 | * It returns an allocated skb containing the frame on success, or NULL. |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 276 | */ |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 277 | struct sk_buff *nci_spi_read(struct nci_spi *nspi) |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 278 | { |
| 279 | struct sk_buff *skb; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 280 | |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 281 | /* Retrieve frame from SPI */ |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 282 | skb = __nci_spi_read(nspi); |
| 283 | if (!skb) |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 284 | goto done; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 285 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 286 | if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) { |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 287 | if (!nci_spi_check_crc(skb)) { |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 288 | send_acknowledge(nspi, ACKNOWLEDGE_NACK); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 289 | goto done; |
| 290 | } |
| 291 | |
| 292 | /* In case of acknowledged mode: if ACK or NACK received, |
| 293 | * unblock completion of latest frame sent. |
| 294 | */ |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 295 | nspi->req_result = nci_spi_get_ack(skb); |
| 296 | if (nspi->req_result) |
| 297 | complete(&nspi->req_completion); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* If there is no payload (ACK/NACK only frame), |
| 301 | * free the socket buffer |
| 302 | */ |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 303 | if (!skb->len) { |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 304 | kfree_skb(skb); |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 305 | skb = NULL; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 306 | goto done; |
| 307 | } |
| 308 | |
Eric Lapuyade | fa544ff | 2013-09-05 11:02:21 +0200 | [diff] [blame] | 309 | if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) |
| 310 | send_acknowledge(nspi, ACKNOWLEDGE_ACK); |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 311 | |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 312 | done: |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 313 | |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 314 | return skb; |
Frederic Danis | 391d8a2 | 2013-05-29 15:35:04 +0200 | [diff] [blame] | 315 | } |
Eric Lapuyade | 22d4aae | 2013-09-23 17:56:31 +0200 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(nci_spi_read); |
Vincent Cuissard | fcd9d04 | 2015-10-26 10:27:42 +0100 | [diff] [blame] | 317 | |
| 318 | MODULE_LICENSE("GPL"); |