st21nfcb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * NCI based Driver for STMicroelectronics NFC Chip
  3. *
  4. * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  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 "st21nfcb.h"
  23. #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
  24. #define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83
  25. static int st21nfcb_nci_open(struct nci_dev *ndev)
  26. {
  27. struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
  28. int r;
  29. if (test_and_set_bit(ST21NFCB_NCI_RUNNING, &info->flags))
  30. return 0;
  31. r = ndlc_open(info->ndlc);
  32. if (r)
  33. clear_bit(ST21NFCB_NCI_RUNNING, &info->flags);
  34. return r;
  35. }
  36. static int st21nfcb_nci_close(struct nci_dev *ndev)
  37. {
  38. struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
  39. if (!test_and_clear_bit(ST21NFCB_NCI_RUNNING, &info->flags))
  40. return 0;
  41. ndlc_close(info->ndlc);
  42. return 0;
  43. }
  44. static int st21nfcb_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  45. {
  46. struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
  47. skb->dev = (void *)ndev;
  48. if (!test_bit(ST21NFCB_NCI_RUNNING, &info->flags))
  49. return -EBUSY;
  50. return ndlc_send(info->ndlc, skb);
  51. }
  52. static __u32 st21nfcb_nci_get_rfprotocol(struct nci_dev *ndev,
  53. __u8 rf_protocol)
  54. {
  55. return rf_protocol == ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 ?
  56. NFC_PROTO_ISO15693_MASK : 0;
  57. }
  58. static struct nci_ops st21nfcb_nci_ops = {
  59. .open = st21nfcb_nci_open,
  60. .close = st21nfcb_nci_close,
  61. .send = st21nfcb_nci_send,
  62. .get_rfprotocol = st21nfcb_nci_get_rfprotocol,
  63. };
  64. int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
  65. int phy_tailroom)
  66. {
  67. struct st21nfcb_nci_info *info;
  68. int r;
  69. u32 protocols;
  70. info = devm_kzalloc(ndlc->dev,
  71. sizeof(struct st21nfcb_nci_info), GFP_KERNEL);
  72. if (!info)
  73. return -ENOMEM;
  74. protocols = NFC_PROTO_JEWEL_MASK
  75. | NFC_PROTO_MIFARE_MASK
  76. | NFC_PROTO_FELICA_MASK
  77. | NFC_PROTO_ISO14443_MASK
  78. | NFC_PROTO_ISO14443_B_MASK
  79. | NFC_PROTO_ISO15693_MASK
  80. | NFC_PROTO_NFC_DEP_MASK;
  81. ndlc->ndev = nci_allocate_device(&st21nfcb_nci_ops, protocols,
  82. phy_headroom, phy_tailroom);
  83. if (!ndlc->ndev) {
  84. pr_err("Cannot allocate nfc ndev\n");
  85. return -ENOMEM;
  86. }
  87. info->ndlc = ndlc;
  88. nci_set_drvdata(ndlc->ndev, info);
  89. r = nci_register_device(ndlc->ndev);
  90. if (r) {
  91. pr_err("Cannot register nfc device to nci core\n");
  92. nci_free_device(ndlc->ndev);
  93. }
  94. return r;
  95. }
  96. EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
  97. void st21nfcb_nci_remove(struct nci_dev *ndev)
  98. {
  99. struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
  100. nci_unregister_device(ndev);
  101. nci_free_device(ndev);
  102. kfree(info);
  103. }
  104. EXPORT_SYMBOL_GPL(st21nfcb_nci_remove);
  105. MODULE_LICENSE("GPL");
  106. MODULE_DESCRIPTION(DRIVER_DESC);