spi.c 7.3 KB

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