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 <linux/nfc.h>
25 #include <net/nfc/nci_core.h>
27 #define NCI_SPI_HDR_LEN 4
28 #define NCI_SPI_CRC_LEN 2
29 #define NCI_SPI_ACK_SHIFT 6
30 #define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
32 #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
33 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
35 #define NCI_SPI_DIRECT_WRITE 0x01
36 #define NCI_SPI_DIRECT_READ 0x02
38 #define ACKNOWLEDGE_NONE 0
39 #define ACKNOWLEDGE_ACK 1
40 #define ACKNOWLEDGE_NACK 2
42 #define CRC_INIT 0xFFFF
44 static int nci_spi_open(struct nci_dev
*nci_dev
)
46 struct nci_spi_dev
*ndev
= nci_get_drvdata(nci_dev
);
48 return ndev
->ops
->open(ndev
);
51 static int nci_spi_close(struct nci_dev
*nci_dev
)
53 struct nci_spi_dev
*ndev
= nci_get_drvdata(nci_dev
);
55 return ndev
->ops
->close(ndev
);
58 static int __nci_spi_send(struct nci_spi_dev
*ndev
, struct sk_buff
*skb
)
61 struct spi_transfer t
;
66 t
.delay_usecs
= ndev
->xfer_udelay
;
69 spi_message_add_tail(&t
, &m
);
71 return spi_sync(ndev
->spi
, &m
);
74 static int nci_spi_send(struct nci_dev
*nci_dev
, struct sk_buff
*skb
)
76 struct nci_spi_dev
*ndev
= nci_get_drvdata(nci_dev
);
77 unsigned int payload_len
= skb
->len
;
82 ndev
->ops
->deassert_int(ndev
);
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;
91 if (ndev
->acknowledge_mode
== NCI_SPI_CRC_ENABLED
) {
94 crc
= crc_ccitt(CRC_INIT
, skb
->data
, skb
->len
);
95 *skb_put(skb
, 1) = crc
>> 8;
96 *skb_put(skb
, 1) = crc
& 0xFF;
99 ret
= __nci_spi_send(ndev
, skb
);
102 ndev
->ops
->assert_int(ndev
);
104 if (ret
!= 0 || ndev
->acknowledge_mode
== NCI_SPI_CRC_DISABLED
)
107 init_completion(&ndev
->req_completion
);
109 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
110 NCI_SPI_SEND_TIMEOUT
);
112 if (completion_rc
<= 0 || ndev
->req_result
== ACKNOWLEDGE_NACK
)
119 static struct nci_ops nci_spi_ops
= {
120 .open
= nci_spi_open
,
121 .close
= nci_spi_close
,
122 .send
= nci_spi_send
,
125 /* ---- Interface to NCI SPI drivers ---- */
128 * nci_spi_allocate_device - allocate a new nci 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
137 struct nci_spi_dev
*nci_spi_allocate_device(struct spi_device
*spi
,
138 struct nci_spi_ops
*ops
,
139 u32 supported_protocols
,
144 struct nci_spi_dev
*ndev
;
147 if (!ops
->open
|| !ops
->close
|| !ops
->assert_int
|| !ops
->deassert_int
)
150 if (!supported_protocols
)
153 ndev
= devm_kzalloc(&spi
->dev
, sizeof(struct nci_dev
), GFP_KERNEL
);
158 ndev
->acknowledge_mode
= acknowledge_mode
;
159 ndev
->xfer_udelay
= delay
;
161 if (acknowledge_mode
== NCI_SPI_CRC_ENABLED
)
162 tailroom
+= NCI_SPI_CRC_LEN
;
164 ndev
->nci_dev
= nci_allocate_device(&nci_spi_ops
, supported_protocols
,
165 NCI_SPI_HDR_LEN
, tailroom
);
169 nci_set_drvdata(ndev
->nci_dev
, ndev
);
173 EXPORT_SYMBOL_GPL(nci_spi_allocate_device
);
176 * nci_spi_free_device - deallocate nci spi device
178 * @ndev: The nci spi device to deallocate
180 void nci_spi_free_device(struct nci_spi_dev
*ndev
)
182 nci_free_device(ndev
->nci_dev
);
184 EXPORT_SYMBOL_GPL(nci_spi_free_device
);
187 * nci_spi_register_device - register a nci spi device in the nfc subsystem
189 * @pdev: The nci spi device to register
191 int nci_spi_register_device(struct nci_spi_dev
*ndev
)
193 return nci_register_device(ndev
->nci_dev
);
195 EXPORT_SYMBOL_GPL(nci_spi_register_device
);
198 * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem
200 * @dev: The nci spi device to unregister
202 void nci_spi_unregister_device(struct nci_spi_dev
*ndev
)
204 nci_unregister_device(ndev
->nci_dev
);
206 EXPORT_SYMBOL_GPL(nci_spi_unregister_device
);
208 static int send_acknowledge(struct nci_spi_dev
*ndev
, u8 acknowledge
)
215 skb
= nci_skb_alloc(ndev
->nci_dev
, 0, GFP_KERNEL
);
217 /* add the NCI SPI header to the start of the buffer */
218 hdr
= skb_push(skb
, NCI_SPI_HDR_LEN
);
219 hdr
[0] = NCI_SPI_DIRECT_WRITE
;
220 hdr
[1] = NCI_SPI_CRC_ENABLED
;
221 hdr
[2] = acknowledge
<< NCI_SPI_ACK_SHIFT
;
224 crc
= crc_ccitt(CRC_INIT
, skb
->data
, skb
->len
);
225 *skb_put(skb
, 1) = crc
>> 8;
226 *skb_put(skb
, 1) = crc
& 0xFF;
228 ret
= __nci_spi_send(ndev
, skb
);
235 static struct sk_buff
*__nci_spi_recv_frame(struct nci_spi_dev
*ndev
)
238 struct spi_message m
;
239 unsigned char req
[2], resp_hdr
[2];
240 struct spi_transfer tx
, rx
;
241 unsigned short rx_len
= 0;
244 spi_message_init(&m
);
245 req
[0] = NCI_SPI_DIRECT_READ
;
246 req
[1] = ndev
->acknowledge_mode
;
250 spi_message_add_tail(&tx
, &m
);
251 rx
.rx_buf
= resp_hdr
;
254 spi_message_add_tail(&rx
, &m
);
255 ret
= spi_sync(ndev
->spi
, &m
);
260 if (ndev
->acknowledge_mode
== NCI_SPI_CRC_ENABLED
)
261 rx_len
= ((resp_hdr
[0] & NCI_SPI_MSB_PAYLOAD_MASK
) << 8) +
262 resp_hdr
[1] + NCI_SPI_CRC_LEN
;
264 rx_len
= (resp_hdr
[0] << 8) | resp_hdr
[1];
266 skb
= nci_skb_alloc(ndev
->nci_dev
, rx_len
, GFP_KERNEL
);
270 spi_message_init(&m
);
271 rx
.rx_buf
= skb_put(skb
, rx_len
);
274 rx
.delay_usecs
= ndev
->xfer_udelay
;
275 spi_message_add_tail(&rx
, &m
);
276 ret
= spi_sync(ndev
->spi
, &m
);
281 if (ndev
->acknowledge_mode
== NCI_SPI_CRC_ENABLED
) {
282 *skb_push(skb
, 1) = resp_hdr
[1];
283 *skb_push(skb
, 1) = resp_hdr
[0];
294 static int nci_spi_check_crc(struct sk_buff
*skb
)
296 u16 crc_data
= (skb
->data
[skb
->len
- 2] << 8) |
297 skb
->data
[skb
->len
- 1];
300 ret
= (crc_ccitt(CRC_INIT
, skb
->data
, skb
->len
- NCI_SPI_CRC_LEN
)
303 skb_trim(skb
, skb
->len
- NCI_SPI_CRC_LEN
);
308 static u8
nci_spi_get_ack(struct sk_buff
*skb
)
312 ret
= skb
->data
[0] >> NCI_SPI_ACK_SHIFT
;
314 /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
321 * nci_spi_recv_frame - receive frame from NCI SPI drivers
323 * @ndev: The nci spi device
326 * This call may only be used from a context that may sleep. The sleep
327 * is non-interruptible, and has no timeout.
329 * It returns zero on success, else a negative error code.
331 int nci_spi_recv_frame(struct nci_spi_dev
*ndev
)
336 ndev
->ops
->deassert_int(ndev
);
338 /* Retrieve frame from SPI */
339 skb
= __nci_spi_recv_frame(ndev
);
345 if (ndev
->acknowledge_mode
== NCI_SPI_CRC_ENABLED
) {
346 if (!nci_spi_check_crc(skb
)) {
347 send_acknowledge(ndev
, ACKNOWLEDGE_NACK
);
351 /* In case of acknowledged mode: if ACK or NACK received,
352 * unblock completion of latest frame sent.
354 ndev
->req_result
= nci_spi_get_ack(skb
);
355 if (ndev
->req_result
)
356 complete(&ndev
->req_completion
);
359 /* If there is no payload (ACK/NACK only frame),
360 * free the socket buffer
367 if (ndev
->acknowledge_mode
== NCI_SPI_CRC_ENABLED
)
368 send_acknowledge(ndev
, ACKNOWLEDGE_ACK
);
370 /* Forward skb to NCI core layer */
371 ret
= nci_recv_frame(ndev
->nci_dev
, skb
);
374 ndev
->ops
->assert_int(ndev
);
378 EXPORT_SYMBOL_GPL(nci_spi_recv_frame
);