spi.c 7.1 KB

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