Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* Bluetooth HCI driver model support. */ |
3 | ||
3a9a231d | 4 | #include <linux/module.h> |
1da177e4 LT |
5 | |
6 | #include <net/bluetooth/bluetooth.h> | |
7 | #include <net/bluetooth/hci_core.h> | |
8 | ||
aef7d97c | 9 | static struct class *bt_class; |
90855d7b | 10 | |
90855d7b MH |
11 | static void bt_link_release(struct device *dev) |
12 | { | |
2dd10688 DH |
13 | struct hci_conn *conn = to_hci_conn(dev); |
14 | kfree(conn); | |
90855d7b MH |
15 | } |
16 | ||
9374bf18 | 17 | static const struct device_type bt_link = { |
90855d7b | 18 | .name = "link", |
90855d7b MH |
19 | .release = bt_link_release, |
20 | }; | |
21 | ||
6d438e33 GP |
22 | /* |
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. | |
26 | */ | |
27 | static int __match_tty(struct device *dev, void *data) | |
28 | { | |
29 | return !strncmp(dev_name(dev), "rfcomm", 6); | |
30 | } | |
31 | ||
32 | void hci_conn_init_sysfs(struct hci_conn *conn) | |
33 | { | |
34 | struct hci_dev *hdev = conn->hdev; | |
35 | ||
36 | BT_DBG("conn %p", conn); | |
37 | ||
38 | conn->dev.type = &bt_link; | |
39 | conn->dev.class = bt_class; | |
40 | conn->dev.parent = &hdev->dev; | |
41 | ||
42 | device_initialize(&conn->dev); | |
43 | } | |
44 | ||
45 | void hci_conn_add_sysfs(struct hci_conn *conn) | |
90855d7b | 46 | { |
457ca7bb | 47 | struct hci_dev *hdev = conn->hdev; |
90855d7b | 48 | |
6d438e33 GP |
49 | BT_DBG("conn %p", conn); |
50 | ||
457ca7bb MH |
51 | dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle); |
52 | ||
90855d7b | 53 | if (device_add(&conn->dev) < 0) { |
2064ee33 | 54 | bt_dev_err(hdev, "failed to register connection device"); |
90855d7b MH |
55 | return; |
56 | } | |
384943ec MH |
57 | |
58 | hci_dev_hold(hdev); | |
90855d7b MH |
59 | } |
60 | ||
6d438e33 | 61 | void hci_conn_del_sysfs(struct hci_conn *conn) |
90855d7b | 62 | { |
90855d7b MH |
63 | struct hci_dev *hdev = conn->hdev; |
64 | ||
a67e899c MH |
65 | if (!device_is_registered(&conn->dev)) |
66 | return; | |
f3784d83 | 67 | |
90855d7b MH |
68 | while (1) { |
69 | struct device *dev; | |
70 | ||
71 | dev = device_find_child(&conn->dev, NULL, __match_tty); | |
72 | if (!dev) | |
73 | break; | |
ffa6a705 | 74 | device_move(dev, NULL, DPM_ORDER_DEV_LAST); |
90855d7b MH |
75 | put_device(dev); |
76 | } | |
77 | ||
78 | device_del(&conn->dev); | |
384943ec | 79 | |
90855d7b MH |
80 | hci_dev_put(hdev); |
81 | } | |
82 | ||
90855d7b | 83 | static void bt_host_release(struct device *dev) |
a91f2e39 | 84 | { |
2dd10688 DH |
85 | struct hci_dev *hdev = to_hci_dev(dev); |
86 | kfree(hdev); | |
46e06531 | 87 | module_put(THIS_MODULE); |
b219e3ac MH |
88 | } |
89 | ||
9374bf18 | 90 | static const struct device_type bt_host = { |
90855d7b | 91 | .name = "host", |
90855d7b MH |
92 | .release = bt_host_release, |
93 | }; | |
a91f2e39 | 94 | |
0ac7e700 DH |
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 | ||
46e06531 | 102 | __module_get(THIS_MODULE); |
0ac7e700 DH |
103 | device_initialize(dev); |
104 | } | |
105 | ||
1da177e4 LT |
106 | int __init bt_sysfs_init(void) |
107 | { | |
a91f2e39 | 108 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
27d35284 | 109 | |
8c6ffba0 | 110 | return PTR_ERR_OR_ZERO(bt_class); |
1da177e4 LT |
111 | } |
112 | ||
860e13b5 | 113 | void bt_sysfs_cleanup(void) |
1da177e4 | 114 | { |
a91f2e39 | 115 | class_destroy(bt_class); |
1da177e4 | 116 | } |