spi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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->ndev = ndev;
  105. return nspi;
  106. }
  107. EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
  108. static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
  109. {
  110. struct sk_buff *skb;
  111. unsigned char *hdr;
  112. u16 crc;
  113. int ret;
  114. skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
  115. /* add the NCI SPI header to the start of the buffer */
  116. hdr = skb_push(skb, NCI_SPI_HDR_LEN);
  117. hdr[0] = NCI_SPI_DIRECT_WRITE;
  118. hdr[1] = NCI_SPI_CRC_ENABLED;
  119. hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
  120. hdr[3] = 0;
  121. crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
  122. *skb_put(skb, 1) = crc >> 8;
  123. *skb_put(skb, 1) = crc & 0xFF;
  124. ret = __nci_spi_send(nspi, skb);
  125. kfree_skb(skb);
  126. return ret;
  127. }
  128. static struct sk_buff *__nci_spi_recv_frame(struct nci_spi *nspi)
  129. {
  130. struct sk_buff *skb;
  131. struct spi_message m;
  132. unsigned char req[2], resp_hdr[2];
  133. struct spi_transfer tx, rx;
  134. unsigned short rx_len = 0;
  135. int ret;
  136. spi_message_init(&m);
  137. req[0] = NCI_SPI_DIRECT_READ;
  138. req[1] = nspi->acknowledge_mode;
  139. tx.tx_buf = req;
  140. tx.len = 2;
  141. tx.cs_change = 0;
  142. spi_message_add_tail(&tx, &m);
  143. rx.rx_buf = resp_hdr;
  144. rx.len = 2;
  145. rx.cs_change = 1;
  146. spi_message_add_tail(&rx, &m);
  147. ret = spi_sync(nspi->spi, &m);
  148. if (ret)
  149. return NULL;
  150. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
  151. rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
  152. resp_hdr[1] + NCI_SPI_CRC_LEN;
  153. else
  154. rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
  155. skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
  156. if (!skb)
  157. return NULL;
  158. spi_message_init(&m);
  159. rx.rx_buf = skb_put(skb, rx_len);
  160. rx.len = rx_len;
  161. rx.cs_change = 0;
  162. rx.delay_usecs = nspi->xfer_udelay;
  163. spi_message_add_tail(&rx, &m);
  164. ret = spi_sync(nspi->spi, &m);
  165. if (ret)
  166. goto receive_error;
  167. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
  168. *skb_push(skb, 1) = resp_hdr[1];
  169. *skb_push(skb, 1) = resp_hdr[0];
  170. }
  171. return skb;
  172. receive_error:
  173. kfree_skb(skb);
  174. return NULL;
  175. }
  176. static int nci_spi_check_crc(struct sk_buff *skb)
  177. {
  178. u16 crc_data = (skb->data[skb->len - 2] << 8) |
  179. skb->data[skb->len - 1];
  180. int ret;
  181. ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
  182. == crc_data);
  183. skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
  184. return ret;
  185. }
  186. static u8 nci_spi_get_ack(struct sk_buff *skb)
  187. {
  188. u8 ret;
  189. ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
  190. /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
  191. skb_pull(skb, 2);
  192. return ret;
  193. }
  194. /**
  195. * nci_spi_recv_frame - receive frame from NCI SPI drivers
  196. *
  197. * @nspi: The nci spi
  198. * Context: can sleep
  199. *
  200. * This call may only be used from a context that may sleep. The sleep
  201. * is non-interruptible, and has no timeout.
  202. *
  203. * It returns zero on success, else a negative error code.
  204. */
  205. int nci_spi_recv_frame(struct nci_spi *nspi)
  206. {
  207. struct sk_buff *skb;
  208. int ret = 0;
  209. nspi->ops->deassert_int(nspi);
  210. /* Retrieve frame from SPI */
  211. skb = __nci_spi_recv_frame(nspi);
  212. if (!skb) {
  213. ret = -EIO;
  214. goto done;
  215. }
  216. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
  217. if (!nci_spi_check_crc(skb)) {
  218. send_acknowledge(nspi, ACKNOWLEDGE_NACK);
  219. goto done;
  220. }
  221. /* In case of acknowledged mode: if ACK or NACK received,
  222. * unblock completion of latest frame sent.
  223. */
  224. nspi->req_result = nci_spi_get_ack(skb);
  225. if (nspi->req_result)
  226. complete(&nspi->req_completion);
  227. }
  228. /* If there is no payload (ACK/NACK only frame),
  229. * free the socket buffer
  230. */
  231. if (skb->len == 0) {
  232. kfree_skb(skb);
  233. goto done;
  234. }
  235. if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
  236. send_acknowledge(nspi, ACKNOWLEDGE_ACK);
  237. /* Forward skb to NCI core layer */
  238. ret = nci_recv_frame(nspi->ndev, skb);
  239. done:
  240. nspi->ops->assert_int(nspi);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(nci_spi_recv_frame);