core.c 3.8 KB

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