hci_sysfs.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 const struct device_type bt_host = {
  71. .name = "host",
  72. .release = bt_host_release,
  73. };
  74. void hci_init_sysfs(struct hci_dev *hdev)
  75. {
  76. struct device *dev = &hdev->dev;
  77. dev->type = &bt_host;
  78. dev->class = &bt_class;
  79. __module_get(THIS_MODULE);
  80. device_initialize(dev);
  81. }
  82. int __init bt_sysfs_init(void)
  83. {
  84. return class_register(&bt_class);
  85. }
  86. void bt_sysfs_cleanup(void)
  87. {
  88. class_unregister(&bt_class);
  89. }