1 // SPDX-License-Identifier: GPL-2.0
2 /* Bluetooth HCI driver model support. */
4 #include <linux/module.h>
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
9 static struct class *bt_class;
11 static void bt_link_release(struct device *dev)
13 struct hci_conn *conn = to_hci_conn(dev);
17 static const struct device_type bt_link = {
19 .release = bt_link_release,
23 * The rfcomm tty device will possibly retain even when conn
24 * is down, and sysfs doesn't support move zombie device,
25 * so we should move the device before conn device is destroyed.
27 static int __match_tty(struct device *dev, void *data)
29 return !strncmp(dev_name(dev), "rfcomm", 6);
32 void hci_conn_init_sysfs(struct hci_conn *conn)
34 struct hci_dev *hdev = conn->hdev;
36 BT_DBG("conn %p", conn);
38 conn->dev.type = &bt_link;
39 conn->dev.class = bt_class;
40 conn->dev.parent = &hdev->dev;
42 device_initialize(&conn->dev);
45 void hci_conn_add_sysfs(struct hci_conn *conn)
47 struct hci_dev *hdev = conn->hdev;
49 BT_DBG("conn %p", conn);
51 if (device_is_registered(&conn->dev))
54 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
56 if (device_add(&conn->dev) < 0) {
57 bt_dev_err(hdev, "failed to register connection device");
64 void hci_conn_del_sysfs(struct hci_conn *conn)
66 struct hci_dev *hdev = conn->hdev;
68 if (!device_is_registered(&conn->dev))
74 dev = device_find_child(&conn->dev, NULL, __match_tty);
77 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
81 device_del(&conn->dev);
86 static void bt_host_release(struct device *dev)
88 struct hci_dev *hdev = to_hci_dev(dev);
90 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
91 hci_release_dev(hdev);
94 module_put(THIS_MODULE);
97 static const struct device_type bt_host = {
99 .release = bt_host_release,
102 void hci_init_sysfs(struct hci_dev *hdev)
104 struct device *dev = &hdev->dev;
106 dev->type = &bt_host;
107 dev->class = bt_class;
109 __module_get(THIS_MODULE);
110 device_initialize(dev);
113 int __init bt_sysfs_init(void)
115 bt_class = class_create(THIS_MODULE, "bluetooth");
117 return PTR_ERR_OR_ZERO(bt_class);
120 void bt_sysfs_cleanup(void)
122 class_destroy(bt_class);