main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Marvell NFC driver: major functions
  3. *
  4. * Copyright (C) 2014-2015 Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available on the worldwide web at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. *
  13. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  16. * this warranty disclaimer.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/nfc.h>
  23. #include <net/nfc/nci.h>
  24. #include <net/nfc/nci_core.h>
  25. #include "nfcmrvl.h"
  26. static int nfcmrvl_nci_open(struct nci_dev *ndev)
  27. {
  28. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  29. int err;
  30. if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  31. return 0;
  32. /* Reset possible fault of previous session */
  33. clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
  34. err = priv->if_ops->nci_open(priv);
  35. if (err)
  36. clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
  37. return err;
  38. }
  39. static int nfcmrvl_nci_close(struct nci_dev *ndev)
  40. {
  41. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  42. if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  43. return 0;
  44. priv->if_ops->nci_close(priv);
  45. return 0;
  46. }
  47. static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  48. {
  49. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  50. nfc_info(priv->dev, "send entry, len %d\n", skb->len);
  51. skb->dev = (void *)ndev;
  52. if (priv->config.hci_muxed) {
  53. unsigned char *hdr;
  54. unsigned char len = skb->len;
  55. hdr = (char *) skb_push(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
  56. hdr[0] = NFCMRVL_HCI_COMMAND_CODE;
  57. hdr[1] = NFCMRVL_HCI_OGF;
  58. hdr[2] = NFCMRVL_HCI_OCF;
  59. hdr[3] = len;
  60. }
  61. return priv->if_ops->nci_send(priv, skb);
  62. }
  63. static int nfcmrvl_nci_setup(struct nci_dev *ndev)
  64. {
  65. __u8 val = 1;
  66. nci_set_config(ndev, NFCMRVL_PB_BAIL_OUT, 1, &val);
  67. return 0;
  68. }
  69. static int nfcmrvl_nci_fw_download(struct nci_dev *ndev,
  70. const char *firmware_name)
  71. {
  72. return nfcmrvl_fw_dnld_start(ndev, firmware_name);
  73. }
  74. static struct nci_ops nfcmrvl_nci_ops = {
  75. .open = nfcmrvl_nci_open,
  76. .close = nfcmrvl_nci_close,
  77. .send = nfcmrvl_nci_send,
  78. .setup = nfcmrvl_nci_setup,
  79. .fw_download = nfcmrvl_nci_fw_download,
  80. };
  81. struct nfcmrvl_private *nfcmrvl_nci_register_dev(enum nfcmrvl_phy phy,
  82. void *drv_data,
  83. struct nfcmrvl_if_ops *ops,
  84. struct device *dev,
  85. struct nfcmrvl_platform_data *pdata)
  86. {
  87. struct nfcmrvl_private *priv;
  88. int rc;
  89. int headroom;
  90. int tailroom;
  91. u32 protocols;
  92. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  93. if (!priv)
  94. return ERR_PTR(-ENOMEM);
  95. priv->drv_data = drv_data;
  96. priv->if_ops = ops;
  97. priv->dev = dev;
  98. priv->phy = phy;
  99. memcpy(&priv->config, pdata, sizeof(*pdata));
  100. if (priv->config.reset_n_io) {
  101. rc = gpio_request_one(priv->config.reset_n_io,
  102. GPIOF_OUT_INIT_LOW,
  103. "nfcmrvl_reset_n");
  104. if (rc < 0) {
  105. priv->config.reset_n_io = 0;
  106. nfc_err(dev, "failed to request reset_n io\n");
  107. }
  108. }
  109. if (phy == NFCMRVL_PHY_SPI) {
  110. headroom = NCI_SPI_HDR_LEN;
  111. tailroom = 1;
  112. } else
  113. headroom = tailroom = 0;
  114. if (priv->config.hci_muxed)
  115. headroom += NFCMRVL_HCI_EVENT_HEADER_SIZE;
  116. protocols = NFC_PROTO_JEWEL_MASK
  117. | NFC_PROTO_MIFARE_MASK
  118. | NFC_PROTO_FELICA_MASK
  119. | NFC_PROTO_ISO14443_MASK
  120. | NFC_PROTO_ISO14443_B_MASK
  121. | NFC_PROTO_ISO15693_MASK
  122. | NFC_PROTO_NFC_DEP_MASK;
  123. priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols,
  124. headroom, tailroom);
  125. if (!priv->ndev) {
  126. nfc_err(dev, "nci_allocate_device failed\n");
  127. rc = -ENOMEM;
  128. goto error_free_gpio;
  129. }
  130. rc = nfcmrvl_fw_dnld_init(priv);
  131. if (rc) {
  132. nfc_err(dev, "failed to initialize FW download %d\n", rc);
  133. goto error_free_dev;
  134. }
  135. nci_set_drvdata(priv->ndev, priv);
  136. rc = nci_register_device(priv->ndev);
  137. if (rc) {
  138. nfc_err(dev, "nci_register_device failed %d\n", rc);
  139. goto error_fw_dnld_deinit;
  140. }
  141. /* Ensure that controller is powered off */
  142. nfcmrvl_chip_halt(priv);
  143. nfc_info(dev, "registered with nci successfully\n");
  144. return priv;
  145. error_fw_dnld_deinit:
  146. nfcmrvl_fw_dnld_deinit(priv);
  147. error_free_dev:
  148. nci_free_device(priv->ndev);
  149. error_free_gpio:
  150. if (priv->config.reset_n_io)
  151. gpio_free(priv->config.reset_n_io);
  152. kfree(priv);
  153. return ERR_PTR(rc);
  154. }
  155. EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);
  156. void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
  157. {
  158. struct nci_dev *ndev = priv->ndev;
  159. if (priv->ndev->nfc_dev->fw_download_in_progress)
  160. nfcmrvl_fw_dnld_abort(priv);
  161. nfcmrvl_fw_dnld_deinit(priv);
  162. if (priv->config.reset_n_io)
  163. gpio_free(priv->config.reset_n_io);
  164. nci_unregister_device(ndev);
  165. nci_free_device(ndev);
  166. kfree(priv);
  167. }
  168. EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);
  169. int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb)
  170. {
  171. if (priv->config.hci_muxed) {
  172. if (skb->data[0] == NFCMRVL_HCI_EVENT_CODE &&
  173. skb->data[1] == NFCMRVL_HCI_NFC_EVENT_CODE) {
  174. /* Data packet, let's extract NCI payload */
  175. skb_pull(skb, NFCMRVL_HCI_EVENT_HEADER_SIZE);
  176. } else {
  177. /* Skip this packet */
  178. kfree_skb(skb);
  179. return 0;
  180. }
  181. }
  182. if (priv->ndev->nfc_dev->fw_download_in_progress) {
  183. nfcmrvl_fw_dnld_recv_frame(priv, skb);
  184. return 0;
  185. }
  186. if (test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  187. nci_recv_frame(priv->ndev, skb);
  188. else {
  189. /* Drop this packet since nobody wants it */
  190. kfree_skb(skb);
  191. return 0;
  192. }
  193. return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);
  196. void nfcmrvl_chip_reset(struct nfcmrvl_private *priv)
  197. {
  198. /* Reset possible fault of previous session */
  199. clear_bit(NFCMRVL_PHY_ERROR, &priv->flags);
  200. if (priv->config.reset_n_io) {
  201. nfc_info(priv->dev, "reset the chip\n");
  202. gpio_set_value(priv->config.reset_n_io, 0);
  203. usleep_range(5000, 10000);
  204. gpio_set_value(priv->config.reset_n_io, 1);
  205. } else
  206. nfc_info(priv->dev, "no reset available on this interface\n");
  207. }
  208. void nfcmrvl_chip_halt(struct nfcmrvl_private *priv)
  209. {
  210. if (priv->config.reset_n_io)
  211. gpio_set_value(priv->config.reset_n_io, 0);
  212. }
  213. int nfcmrvl_parse_dt(struct device_node *node,
  214. struct nfcmrvl_platform_data *pdata)
  215. {
  216. int reset_n_io;
  217. reset_n_io = of_get_named_gpio(node, "reset-n-io", 0);
  218. if (reset_n_io < 0) {
  219. pr_info("no reset-n-io config\n");
  220. reset_n_io = 0;
  221. } else if (!gpio_is_valid(reset_n_io)) {
  222. pr_err("invalid reset-n-io GPIO\n");
  223. return reset_n_io;
  224. }
  225. pdata->reset_n_io = reset_n_io;
  226. if (of_find_property(node, "hci-muxed", NULL))
  227. pdata->hci_muxed = 1;
  228. else
  229. pdata->hci_muxed = 0;
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(nfcmrvl_parse_dt);
  233. MODULE_AUTHOR("Marvell International Ltd.");
  234. MODULE_DESCRIPTION("Marvell NFC driver");
  235. MODULE_LICENSE("GPL v2");