spi.c 7.4 KB

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