main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Marvell NFC driver: major functions
  3. *
  4. * Copyright (C) 2014, 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/nfc.h>
  20. #include <net/nfc/nci.h>
  21. #include <net/nfc/nci_core.h>
  22. #include "nfcmrvl.h"
  23. #define VERSION "1.0"
  24. static int nfcmrvl_nci_open(struct nci_dev *ndev)
  25. {
  26. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  27. int err;
  28. if (test_and_set_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  29. return 0;
  30. err = priv->if_ops->nci_open(priv);
  31. if (err)
  32. clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags);
  33. return err;
  34. }
  35. static int nfcmrvl_nci_close(struct nci_dev *ndev)
  36. {
  37. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  38. if (!test_and_clear_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  39. return 0;
  40. priv->if_ops->nci_close(priv);
  41. return 0;
  42. }
  43. static int nfcmrvl_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  44. {
  45. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  46. nfc_info(priv->dev, "send entry, len %d\n", skb->len);
  47. skb->dev = (void *)ndev;
  48. if (!test_bit(NFCMRVL_NCI_RUNNING, &priv->flags))
  49. return -EBUSY;
  50. return priv->if_ops->nci_send(priv, skb);
  51. }
  52. static int nfcmrvl_nci_setup(struct nci_dev *ndev)
  53. {
  54. __u8 val;
  55. val = NFCMRVL_GPIO_PIN_NFC_NOT_ALLOWED;
  56. nci_set_config(ndev, NFCMRVL_NOT_ALLOWED_ID, 1, &val);
  57. val = NFCMRVL_GPIO_PIN_NFC_ACTIVE;
  58. nci_set_config(ndev, NFCMRVL_ACTIVE_ID, 1, &val);
  59. val = NFCMRVL_EXT_COEX_ENABLE;
  60. nci_set_config(ndev, NFCMRVL_EXT_COEX_ID, 1, &val);
  61. return 0;
  62. }
  63. static struct nci_ops nfcmrvl_nci_ops = {
  64. .open = nfcmrvl_nci_open,
  65. .close = nfcmrvl_nci_close,
  66. .send = nfcmrvl_nci_send,
  67. .setup = nfcmrvl_nci_setup,
  68. };
  69. struct nfcmrvl_private *nfcmrvl_nci_register_dev(void *drv_data,
  70. struct nfcmrvl_if_ops *ops,
  71. struct device *dev)
  72. {
  73. struct nfcmrvl_private *priv;
  74. int rc;
  75. u32 protocols;
  76. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  77. if (!priv)
  78. return ERR_PTR(-ENOMEM);
  79. priv->drv_data = drv_data;
  80. priv->if_ops = ops;
  81. priv->dev = dev;
  82. protocols = NFC_PROTO_JEWEL_MASK
  83. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  84. | NFC_PROTO_ISO14443_MASK
  85. | NFC_PROTO_ISO14443_B_MASK
  86. | NFC_PROTO_NFC_DEP_MASK;
  87. priv->ndev = nci_allocate_device(&nfcmrvl_nci_ops, protocols, 0, 0);
  88. if (!priv->ndev) {
  89. nfc_err(dev, "nci_allocate_device failed");
  90. rc = -ENOMEM;
  91. goto error;
  92. }
  93. nci_set_drvdata(priv->ndev, priv);
  94. rc = nci_register_device(priv->ndev);
  95. if (rc) {
  96. nfc_err(dev, "nci_register_device failed %d", rc);
  97. nci_free_device(priv->ndev);
  98. goto error;
  99. }
  100. nfc_info(dev, "registered with nci successfully\n");
  101. return priv;
  102. error:
  103. kfree(priv);
  104. return ERR_PTR(rc);
  105. }
  106. EXPORT_SYMBOL_GPL(nfcmrvl_nci_register_dev);
  107. void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
  108. {
  109. struct nci_dev *ndev = priv->ndev;
  110. nci_unregister_device(ndev);
  111. nci_free_device(ndev);
  112. kfree(priv);
  113. }
  114. EXPORT_SYMBOL_GPL(nfcmrvl_nci_unregister_dev);
  115. int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, void *data, int count)
  116. {
  117. struct sk_buff *skb;
  118. skb = nci_skb_alloc(priv->ndev, count, GFP_ATOMIC);
  119. if (!skb)
  120. return -ENOMEM;
  121. memcpy(skb_put(skb, count), data, count);
  122. nci_recv_frame(priv->ndev, skb);
  123. return count;
  124. }
  125. EXPORT_SYMBOL_GPL(nfcmrvl_nci_recv_frame);
  126. MODULE_AUTHOR("Marvell International Ltd.");
  127. MODULE_DESCRIPTION("Marvell NFC driver ver " VERSION);
  128. MODULE_VERSION(VERSION);
  129. MODULE_LICENSE("GPL v2");