Linux 6.10-rc3
[linux-stable.git] / net / bluetooth / hci_sysfs.c
blob367e32fe30eb84e80e3cc9988f910e54a23603a3
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 = {
10 .name = "bluetooth",
13 static void bt_link_release(struct device *dev)
15 struct hci_conn *conn = to_hci_conn(dev);
16 kfree(conn);
19 static const struct device_type bt_link = {
20 .name = "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_dev_dbg(hdev, "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_dev_dbg(hdev, "conn %p", conn);
53 if (device_is_registered(&conn->dev))
54 return;
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");
62 void hci_conn_del_sysfs(struct hci_conn *conn)
64 struct hci_dev *hdev = conn->hdev;
66 bt_dev_dbg(hdev, "conn %p", conn);
68 if (!device_is_registered(&conn->dev)) {
69 /* If device_add() has *not* succeeded, use *only* put_device()
70 * to drop the reference count.
72 put_device(&conn->dev);
73 return;
76 while (1) {
77 struct device *dev;
79 dev = device_find_child(&conn->dev, NULL, __match_tty);
80 if (!dev)
81 break;
82 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
83 put_device(dev);
86 device_unregister(&conn->dev);
89 static void bt_host_release(struct device *dev)
91 struct hci_dev *hdev = to_hci_dev(dev);
93 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
94 hci_release_dev(hdev);
95 else
96 kfree(hdev);
97 module_put(THIS_MODULE);
100 static const struct device_type bt_host = {
101 .name = "host",
102 .release = bt_host_release,
105 void hci_init_sysfs(struct hci_dev *hdev)
107 struct device *dev = &hdev->dev;
109 dev->type = &bt_host;
110 dev->class = &bt_class;
112 __module_get(THIS_MODULE);
113 device_initialize(dev);
116 int __init bt_sysfs_init(void)
118 return class_register(&bt_class);
121 void bt_sysfs_cleanup(void)
123 class_unregister(&bt_class);