spi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <linux/nfc.h>
  23. #include <net/nfc/nci_core.h>
  24. #define NCI_SPI_ACK_SHIFT 6
  25. #define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
  26. #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
  27. NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
  28. #define NCI_SPI_DIRECT_WRITE 0x01
  29. #define NCI_SPI_DIRECT_READ 0x02
  30. #define ACKNOWLEDGE_NONE 0
  31. #define ACKNOWLEDGE_ACK 1
  32. #define ACKNOWLEDGE_NACK 2
  33. #define CRC_INIT 0xFFFF
  34. static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb)
  35. {
  36. struct spi_message m;
  37. struct spi_transfer t;
  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_recv_frame(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. req[0] = NCI_SPI_DIRECT_READ;
  139. req[1] = nspi->acknowledge_mode;
  140. tx.tx_buf = req;
  141. tx.len = 2;
  142. tx.cs_change = 0;
  143. spi_message_add_tail(&tx, &m);
  144. rx.rx_buf = resp_hdr;
  145. rx.len = 2;
  146. rx.cs_change = 1;
  147. spi_message_add_tail(&rx, &m);
  148. ret = spi_sync(nspi->spi, &m);
  149. if (ret)
  150. return NULL;
  151. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
  152. rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
  153. resp_hdr[1] + NCI_SPI_CRC_LEN;
  154. else
  155. rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
  156. skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
  157. if (!skb)
  158. return NULL;
  159. spi_message_init(&m);
  160. rx.rx_buf = skb_put(skb, rx_len);
  161. rx.len = rx_len;
  162. rx.cs_change = 0;
  163. rx.delay_usecs = nspi->xfer_udelay;
  164. spi_message_add_tail(&rx, &m);
  165. ret = spi_sync(nspi->spi, &m);
  166. if (ret)
  167. goto receive_error;
  168. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
  169. *skb_push(skb, 1) = resp_hdr[1];
  170. *skb_push(skb, 1) = resp_hdr[0];
  171. }
  172. return skb;
  173. receive_error:
  174. kfree_skb(skb);
  175. return NULL;
  176. }
  177. static int nci_spi_check_crc(struct sk_buff *skb)
  178. {
  179. u16 crc_data = (skb->data[skb->len - 2] << 8) |
  180. skb->data[skb->len - 1];
  181. int ret;
  182. ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
  183. == crc_data);
  184. skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
  185. return ret;
  186. }
  187. static u8 nci_spi_get_ack(struct sk_buff *skb)
  188. {
  189. u8 ret;
  190. ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
  191. /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
  192. skb_pull(skb, 2);
  193. return ret;
  194. }
  195. /**
  196. * nci_spi_recv_frame - receive frame from NCI SPI drivers
  197. *
  198. * @nspi: The nci spi
  199. * Context: can sleep
  200. *
  201. * This call may only be used from a context that may sleep. The sleep
  202. * is non-interruptible, and has no timeout.
  203. *
  204. * It returns zero on success, else a negative error code.
  205. */
  206. int nci_spi_recv_frame(struct nci_spi *nspi)
  207. {
  208. struct sk_buff *skb;
  209. int ret = 0;
  210. nspi->ops->deassert_int(nspi);
  211. /* Retrieve frame from SPI */
  212. skb = __nci_spi_recv_frame(nspi);
  213. if (!skb) {
  214. ret = -EIO;
  215. goto done;
  216. }
  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 == 0) {
  233. kfree_skb(skb);
  234. goto done;
  235. }
  236. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
  237. send_acknowledge(nspi, ACKNOWLEDGE_ACK);
  238. /* Forward skb to NCI core layer */
  239. ret = nci_recv_frame(nspi->ndev, skb);
  240. done:
  241. nspi->ops->assert_int(nspi);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL_GPL(nci_spi_recv_frame);