spi.c 7.1 KB

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