core.c 4.3 KB

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