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