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 const struct class bt_class = {
13 static void bt_link_release(struct device *dev)
15 struct hci_conn *conn = to_hci_conn(dev);
19 static const struct device_type bt_link = {
21 .release = bt_link_release,
25 * The rfcomm tty device will possibly retain even when conn
26 * is down, and sysfs doesn't support move zombie device,
27 * so we should move the device before conn device is destroyed.
29 static int __match_tty(struct device *dev, void *data)
31 return !strncmp(dev_name(dev), "rfcomm", 6);
34 void hci_conn_init_sysfs(struct hci_conn *conn)
36 struct hci_dev *hdev = conn->hdev;
38 BT_DBG("conn %p", conn);
40 conn->dev.type = &bt_link;
41 conn->dev.class = &bt_class;
42 conn->dev.parent = &hdev->dev;
44 device_initialize(&conn->dev);
47 void hci_conn_add_sysfs(struct hci_conn *conn)
49 struct hci_dev *hdev = conn->hdev;
51 BT_DBG("conn %p", conn);
53 if (device_is_registered(&conn->dev))
56 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
58 if (device_add(&conn->dev) < 0) {
59 bt_dev_err(hdev, "failed to register connection device");
66 void hci_conn_del_sysfs(struct hci_conn *conn)
68 struct hci_dev *hdev = conn->hdev;
70 if (!device_is_registered(&conn->dev))
76 dev = device_find_child(&conn->dev, NULL, __match_tty);
79 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
83 device_del(&conn->dev);
88 static void bt_host_release(struct device *dev)
90 struct hci_dev *hdev = to_hci_dev(dev);
92 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
93 hci_release_dev(hdev);
96 module_put(THIS_MODULE);
99 static const struct device_type bt_host = {
101 .release = bt_host_release,
104 void hci_init_sysfs(struct hci_dev *hdev)
106 struct device *dev = &hdev->dev;
108 dev->type = &bt_host;
109 dev->class = &bt_class;
111 __module_get(THIS_MODULE);
112 device_initialize(dev);
115 int __init bt_sysfs_init(void)
117 return class_register(&bt_class);
120 void bt_sysfs_cleanup(void)
122 class_unregister(&bt_class);