xref: /openbmc/linux/net/bluetooth/hci_sysfs.c (revision 6db6b729)
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 const struct class bt_class = {
10 	.name = "bluetooth",
11 };
12 
13 static void bt_link_release(struct device *dev)
14 {
15 	struct hci_conn *conn = to_hci_conn(dev);
16 	kfree(conn);
17 }
18 
19 static const struct device_type bt_link = {
20 	.name    = "link",
21 	.release = bt_link_release,
22 };
23 
24 /*
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.
28  */
29 static int __match_tty(struct device *dev, void *data)
30 {
31 	return !strncmp(dev_name(dev), "rfcomm", 6);
32 }
33 
34 void hci_conn_init_sysfs(struct hci_conn *conn)
35 {
36 	struct hci_dev *hdev = conn->hdev;
37 
38 	BT_DBG("conn %p", conn);
39 
40 	conn->dev.type = &bt_link;
41 	conn->dev.class = &bt_class;
42 	conn->dev.parent = &hdev->dev;
43 
44 	device_initialize(&conn->dev);
45 }
46 
47 void hci_conn_add_sysfs(struct hci_conn *conn)
48 {
49 	struct hci_dev *hdev = conn->hdev;
50 
51 	BT_DBG("conn %p", conn);
52 
53 	if (device_is_registered(&conn->dev))
54 		return;
55 
56 	dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
57 
58 	if (device_add(&conn->dev) < 0) {
59 		bt_dev_err(hdev, "failed to register connection device");
60 		return;
61 	}
62 
63 	hci_dev_hold(hdev);
64 }
65 
66 void hci_conn_del_sysfs(struct hci_conn *conn)
67 {
68 	struct hci_dev *hdev = conn->hdev;
69 
70 	if (!device_is_registered(&conn->dev))
71 		return;
72 
73 	while (1) {
74 		struct device *dev;
75 
76 		dev = device_find_child(&conn->dev, NULL, __match_tty);
77 		if (!dev)
78 			break;
79 		device_move(dev, NULL, DPM_ORDER_DEV_LAST);
80 		put_device(dev);
81 	}
82 
83 	device_del(&conn->dev);
84 
85 	hci_dev_put(hdev);
86 }
87 
88 static void bt_host_release(struct device *dev)
89 {
90 	struct hci_dev *hdev = to_hci_dev(dev);
91 
92 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
93 		hci_release_dev(hdev);
94 	else
95 		kfree(hdev);
96 	module_put(THIS_MODULE);
97 }
98 
99 static const struct device_type bt_host = {
100 	.name    = "host",
101 	.release = bt_host_release,
102 };
103 
104 void hci_init_sysfs(struct hci_dev *hdev)
105 {
106 	struct device *dev = &hdev->dev;
107 
108 	dev->type = &bt_host;
109 	dev->class = &bt_class;
110 
111 	__module_get(THIS_MODULE);
112 	device_initialize(dev);
113 }
114 
115 int __init bt_sysfs_init(void)
116 {
117 	return class_register(&bt_class);
118 }
119 
120 void bt_sysfs_cleanup(void)
121 {
122 	class_unregister(&bt_class);
123 }
124