core.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Generic driver for NXP NCI NFC chips
  3. *
  4. * Copyright (C) 2014 NXP Semiconductors All rights reserved.
  5. *
  6. * Authors: Clément Perrochaud <clement.perrochaud@nxp.com>
  7. *
  8. * Derived from PN544 device driver:
  9. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/gpio.h>
  25. #include <linux/module.h>
  26. #include <linux/nfc.h>
  27. #include <linux/platform_data/nxp-nci.h>
  28. #include <net/nfc/nci_core.h>
  29. #include "nxp-nci.h"
  30. #define NXP_NCI_HDR_LEN 4
  31. #define NXP_NCI_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  32. NFC_PROTO_MIFARE_MASK | \
  33. NFC_PROTO_FELICA_MASK | \
  34. NFC_PROTO_ISO14443_MASK | \
  35. NFC_PROTO_ISO14443_B_MASK | \
  36. NFC_PROTO_NFC_DEP_MASK)
  37. static int nxp_nci_open(struct nci_dev *ndev)
  38. {
  39. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  40. int r = 0;
  41. mutex_lock(&info->info_lock);
  42. if (info->mode != NXP_NCI_MODE_COLD) {
  43. r = -EBUSY;
  44. goto open_exit;
  45. }
  46. if (info->phy_ops->set_mode)
  47. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_NCI);
  48. info->mode = NXP_NCI_MODE_NCI;
  49. open_exit:
  50. mutex_unlock(&info->info_lock);
  51. return r;
  52. }
  53. static int nxp_nci_close(struct nci_dev *ndev)
  54. {
  55. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  56. int r = 0;
  57. mutex_lock(&info->info_lock);
  58. if (info->phy_ops->set_mode)
  59. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
  60. info->mode = NXP_NCI_MODE_COLD;
  61. mutex_unlock(&info->info_lock);
  62. return r;
  63. }
  64. static int nxp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  65. {
  66. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  67. int r;
  68. if (!info->phy_ops->write) {
  69. r = -ENOTSUPP;
  70. goto send_exit;
  71. }
  72. if (info->mode != NXP_NCI_MODE_NCI) {
  73. r = -EINVAL;
  74. goto send_exit;
  75. }
  76. r = info->phy_ops->write(info->phy_id, skb);
  77. if (r < 0)
  78. kfree_skb(skb);
  79. send_exit:
  80. return r;
  81. }
  82. static struct nci_ops nxp_nci_ops = {
  83. .open = nxp_nci_open,
  84. .close = nxp_nci_close,
  85. .send = nxp_nci_send,
  86. .fw_download = nxp_nci_fw_download,
  87. };
  88. int nxp_nci_probe(void *phy_id, struct device *pdev,
  89. const struct nxp_nci_phy_ops *phy_ops,
  90. unsigned int max_payload,
  91. struct nci_dev **ndev)
  92. {
  93. struct nxp_nci_info *info;
  94. int r;
  95. info = devm_kzalloc(pdev, sizeof(struct nxp_nci_info), GFP_KERNEL);
  96. if (!info) {
  97. r = -ENOMEM;
  98. goto probe_exit;
  99. }
  100. info->phy_id = phy_id;
  101. info->pdev = pdev;
  102. info->phy_ops = phy_ops;
  103. info->max_payload = max_payload;
  104. INIT_WORK(&info->fw_info.work, nxp_nci_fw_work);
  105. init_completion(&info->fw_info.cmd_completion);
  106. mutex_init(&info->info_lock);
  107. if (info->phy_ops->set_mode) {
  108. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
  109. if (r < 0)
  110. goto probe_exit;
  111. }
  112. info->mode = NXP_NCI_MODE_COLD;
  113. info->ndev = nci_allocate_device(&nxp_nci_ops, NXP_NCI_NFC_PROTOCOLS,
  114. NXP_NCI_HDR_LEN, 0);
  115. if (!info->ndev) {
  116. r = -ENOMEM;
  117. goto probe_exit;
  118. }
  119. nci_set_parent_dev(info->ndev, pdev);
  120. nci_set_drvdata(info->ndev, info);
  121. r = nci_register_device(info->ndev);
  122. if (r < 0)
  123. goto probe_exit_free_nci;
  124. *ndev = info->ndev;
  125. goto probe_exit;
  126. probe_exit_free_nci:
  127. nci_free_device(info->ndev);
  128. probe_exit:
  129. return r;
  130. }
  131. EXPORT_SYMBOL(nxp_nci_probe);
  132. void nxp_nci_remove(struct nci_dev *ndev)
  133. {
  134. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  135. if (info->mode == NXP_NCI_MODE_FW)
  136. nxp_nci_fw_work_complete(info, -ESHUTDOWN);
  137. cancel_work_sync(&info->fw_info.work);
  138. mutex_lock(&info->info_lock);
  139. if (info->phy_ops->set_mode)
  140. info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
  141. nci_unregister_device(ndev);
  142. nci_free_device(ndev);
  143. mutex_unlock(&info->info_lock);
  144. }
  145. EXPORT_SYMBOL(nxp_nci_remove);
  146. MODULE_LICENSE("GPL");
  147. MODULE_DESCRIPTION("NXP NCI NFC driver");
  148. MODULE_AUTHOR("Clément Perrochaud <clement.perrochaud@nxp.com>");