core.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * NCI based Driver for STMicroelectronics NFC Chip
  3. *
  4. * Copyright (C) 2014-2015 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 <linux/gpio.h>
  23. #include <linux/delay.h>
  24. #include "st-nci.h"
  25. #include "st-nci_se.h"
  26. #define DRIVER_DESC "NCI NFC driver for ST_NCI"
  27. #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
  28. static int st_nci_init(struct nci_dev *ndev)
  29. {
  30. struct nci_mode_set_cmd cmd;
  31. cmd.cmd_type = ST_NCI_SET_NFC_MODE;
  32. cmd.mode = 1;
  33. return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
  34. sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
  35. }
  36. static int st_nci_open(struct nci_dev *ndev)
  37. {
  38. struct st_nci_info *info = nci_get_drvdata(ndev);
  39. int r;
  40. if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
  41. return 0;
  42. r = ndlc_open(info->ndlc);
  43. if (r)
  44. clear_bit(ST_NCI_RUNNING, &info->flags);
  45. return r;
  46. }
  47. static int st_nci_close(struct nci_dev *ndev)
  48. {
  49. struct st_nci_info *info = nci_get_drvdata(ndev);
  50. if (!test_bit(ST_NCI_RUNNING, &info->flags))
  51. return 0;
  52. ndlc_close(info->ndlc);
  53. clear_bit(ST_NCI_RUNNING, &info->flags);
  54. return 0;
  55. }
  56. static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
  57. {
  58. struct st_nci_info *info = nci_get_drvdata(ndev);
  59. skb->dev = (void *)ndev;
  60. if (!test_bit(ST_NCI_RUNNING, &info->flags))
  61. return -EBUSY;
  62. return ndlc_send(info->ndlc, skb);
  63. }
  64. static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
  65. __u8 rf_protocol)
  66. {
  67. return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
  68. NFC_PROTO_ISO15693_MASK : 0;
  69. }
  70. static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
  71. struct sk_buff *skb)
  72. {
  73. __u8 status = skb->data[0];
  74. nci_req_complete(ndev, status);
  75. return 0;
  76. }
  77. static struct nci_prop_ops st_nci_prop_ops[] = {
  78. {
  79. .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
  80. ST_NCI_CORE_PROP),
  81. .rsp = st_nci_prop_rsp_packet,
  82. },
  83. };
  84. static struct nci_ops st_nci_ops = {
  85. .init = st_nci_init,
  86. .open = st_nci_open,
  87. .close = st_nci_close,
  88. .send = st_nci_send,
  89. .get_rfprotocol = st_nci_get_rfprotocol,
  90. .discover_se = st_nci_discover_se,
  91. .enable_se = st_nci_enable_se,
  92. .disable_se = st_nci_disable_se,
  93. .se_io = st_nci_se_io,
  94. .hci_load_session = st_nci_hci_load_session,
  95. .hci_event_received = st_nci_hci_event_received,
  96. .hci_cmd_received = st_nci_hci_cmd_received,
  97. .prop_ops = st_nci_prop_ops,
  98. .n_prop_ops = ARRAY_SIZE(st_nci_prop_ops),
  99. };
  100. int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
  101. int phy_tailroom)
  102. {
  103. struct st_nci_info *info;
  104. int r;
  105. u32 protocols;
  106. info = devm_kzalloc(ndlc->dev,
  107. sizeof(struct st_nci_info), GFP_KERNEL);
  108. if (!info)
  109. return -ENOMEM;
  110. protocols = NFC_PROTO_JEWEL_MASK
  111. | NFC_PROTO_MIFARE_MASK
  112. | NFC_PROTO_FELICA_MASK
  113. | NFC_PROTO_ISO14443_MASK
  114. | NFC_PROTO_ISO14443_B_MASK
  115. | NFC_PROTO_ISO15693_MASK
  116. | NFC_PROTO_NFC_DEP_MASK;
  117. ndlc->ndev = nci_allocate_device(&st_nci_ops, protocols,
  118. phy_headroom, phy_tailroom);
  119. if (!ndlc->ndev) {
  120. pr_err("Cannot allocate nfc ndev\n");
  121. return -ENOMEM;
  122. }
  123. info->ndlc = ndlc;
  124. nci_set_drvdata(ndlc->ndev, info);
  125. r = nci_register_device(ndlc->ndev);
  126. if (r) {
  127. pr_err("Cannot register nfc device to nci core\n");
  128. nci_free_device(ndlc->ndev);
  129. return r;
  130. }
  131. return st_nci_se_init(ndlc->ndev);
  132. }
  133. EXPORT_SYMBOL_GPL(st_nci_probe);
  134. void st_nci_remove(struct nci_dev *ndev)
  135. {
  136. struct st_nci_info *info = nci_get_drvdata(ndev);
  137. ndlc_close(info->ndlc);
  138. nci_unregister_device(ndev);
  139. nci_free_device(ndev);
  140. }
  141. EXPORT_SYMBOL_GPL(st_nci_remove);
  142. MODULE_LICENSE("GPL");
  143. MODULE_DESCRIPTION(DRIVER_DESC);