hci_sysfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Bluetooth HCI driver model support. */
  3. #include <linux/module.h>
  4. #include <net/bluetooth/bluetooth.h>
  5. #include <net/bluetooth/hci_core.h>
  6. static const struct class bt_class = {
  7. .name = "bluetooth",
  8. };
  9. static void bt_link_release(struct device *dev)
  10. {
  11. struct hci_conn *conn = to_hci_conn(dev);
  12. kfree(conn);
  13. }
  14. static const struct device_type bt_link = {
  15. .name = "link",
  16. .release = bt_link_release,
  17. };
  18. void hci_conn_init_sysfs(struct hci_conn *conn)
  19. {
  20. struct hci_dev *hdev = conn->hdev;
  21. bt_dev_dbg(hdev, "conn %p", conn);
  22. conn->dev.type = &bt_link;
  23. conn->dev.class = &bt_class;
  24. conn->dev.parent = &hdev->dev;
  25. device_initialize(&conn->dev);
  26. }
  27. void hci_conn_add_sysfs(struct hci_conn *conn)
  28. {
  29. struct hci_dev *hdev = conn->hdev;
  30. bt_dev_dbg(hdev, "conn %p", conn);
  31. if (device_is_registered(&conn->dev))
  32. return;
  33. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  34. if (device_add(&conn->dev) < 0)
  35. bt_dev_err(hdev, "failed to register connection device");
  36. }
  37. void hci_conn_del_sysfs(struct hci_conn *conn)
  38. {
  39. struct hci_dev *hdev = conn->hdev;
  40. bt_dev_dbg(hdev, "conn %p", conn);
  41. if (!device_is_registered(&conn->dev)) {
  42. /* If device_add() has *not* succeeded, use *only* put_device()
  43. * to drop the reference count.
  44. */
  45. put_device(&conn->dev);
  46. return;
  47. }
  48. /* If there are devices using the connection as parent reset it to NULL
  49. * before unregistering the device.
  50. */
  51. while (1) {
  52. struct device *dev;
  53. dev = device_find_any_child(&conn->dev);
  54. if (!dev)
  55. break;
  56. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  57. put_device(dev);
  58. }
  59. device_unregister(&conn->dev);
  60. }
  61. static void bt_host_release(struct device *dev)
  62. {
  63. struct hci_dev *hdev = to_hci_dev(dev);
  64. if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
  65. hci_release_dev(hdev);
  66. else
  67. kfree(hdev);
  68. module_put(THIS_MODULE);
  69. }
  70. static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
  71. const char *buf, size_t count)
  72. {
  73. struct hci_dev *hdev = to_hci_dev(dev);
  74. if (hdev->reset)
  75. hdev->reset(hdev);
  76. return count;
  77. }
  78. static DEVICE_ATTR_WO(reset);
  79. static struct attribute *bt_host_attrs[] = {
  80. &dev_attr_reset.attr,
  81. NULL,
  82. };
  83. ATTRIBUTE_GROUPS(bt_host);
  84. static const struct device_type bt_host = {
  85. .name = "host",
  86. .release = bt_host_release,
  87. .groups = bt_host_groups,
  88. };
  89. void hci_init_sysfs(struct hci_dev *hdev)
  90. {
  91. struct device *dev = &hdev->dev;
  92. dev->type = &bt_host;
  93. dev->class = &bt_class;
  94. __module_get(THIS_MODULE);
  95. device_initialize(dev);
  96. }
  97. int __init bt_sysfs_init(void)
  98. {
  99. return class_register(&bt_class);
  100. }
  101. void bt_sysfs_cleanup(void)
  102. {
  103. class_unregister(&bt_class);
  104. }