spi.c 7.0 KB

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