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