1 /* Bluetooth HCI driver model support. */
3 #include <linux/module.h>
5 #include <net/bluetooth/bluetooth.h>
6 #include <net/bluetooth/hci_core.h>
8 static struct class *bt_class;
10 static inline char *link_typetostr(int type)
26 static ssize_t show_link_type(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct hci_conn *conn = to_hci_conn(dev);
30 return sprintf(buf, "%s\n", link_typetostr(conn->type));
33 static ssize_t show_link_address(struct device *dev,
34 struct device_attribute *attr, char *buf)
36 struct hci_conn *conn = to_hci_conn(dev);
37 return sprintf(buf, "%pMR\n", &conn->dst);
40 #define LINK_ATTR(_name, _mode, _show, _store) \
41 struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
43 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
44 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
46 static struct attribute *bt_link_attrs[] = {
48 &link_attr_address.attr,
52 #ifdef CONFIG_TIZEN_WIP
53 static struct attribute_group bt_link_group = {
54 .attrs = bt_link_attrs,
57 static const struct attribute_group *bt_link_groups[] = {
62 ATTRIBUTE_GROUPS(bt_link);
65 static void bt_link_release(struct device *dev)
67 struct hci_conn *conn = to_hci_conn(dev);
71 static struct device_type bt_link = {
73 .groups = bt_link_groups,
74 .release = bt_link_release,
78 * The rfcomm tty device will possibly retain even when conn
79 * is down, and sysfs doesn't support move zombie device,
80 * so we should move the device before conn device is destroyed.
82 static int __match_tty(struct device *dev, void *data)
84 return !strncmp(dev_name(dev), "rfcomm", 6);
87 void hci_conn_init_sysfs(struct hci_conn *conn)
89 struct hci_dev *hdev = conn->hdev;
91 BT_DBG("conn %p", conn);
93 conn->dev.type = &bt_link;
94 conn->dev.class = bt_class;
95 conn->dev.parent = &hdev->dev;
97 device_initialize(&conn->dev);
100 void hci_conn_add_sysfs(struct hci_conn *conn)
102 struct hci_dev *hdev = conn->hdev;
104 BT_DBG("conn %p", conn);
106 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
108 if (device_add(&conn->dev) < 0) {
109 BT_ERR("Failed to register connection device");
116 void hci_conn_del_sysfs(struct hci_conn *conn)
118 struct hci_dev *hdev = conn->hdev;
120 if (!device_is_registered(&conn->dev))
126 dev = device_find_child(&conn->dev, NULL, __match_tty);
129 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
133 device_del(&conn->dev);
138 static inline char *host_typetostr(int type)
150 static ssize_t show_type(struct device *dev,
151 struct device_attribute *attr, char *buf)
153 struct hci_dev *hdev = to_hci_dev(dev);
154 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
157 static ssize_t show_name(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct hci_dev *hdev = to_hci_dev(dev);
161 char name[HCI_MAX_NAME_LENGTH + 1];
164 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
165 name[i] = hdev->dev_name[i];
167 name[HCI_MAX_NAME_LENGTH] = '\0';
168 return sprintf(buf, "%s\n", name);
171 static ssize_t show_address(struct device *dev,
172 struct device_attribute *attr, char *buf)
174 struct hci_dev *hdev = to_hci_dev(dev);
175 return sprintf(buf, "%pMR\n", &hdev->bdaddr);
178 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
179 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
180 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
182 static struct attribute *bt_host_attrs[] = {
185 &dev_attr_address.attr,
189 #ifdef CONFIG_TIZEN_WIP
190 static struct attribute_group bt_host_group = {
191 .attrs = bt_host_attrs,
194 static const struct attribute_group *bt_host_groups[] = {
199 ATTRIBUTE_GROUPS(bt_host);
202 static void bt_host_release(struct device *dev)
204 struct hci_dev *hdev = to_hci_dev(dev);
206 module_put(THIS_MODULE);
209 static struct device_type bt_host = {
211 .groups = bt_host_groups,
212 .release = bt_host_release,
215 void hci_init_sysfs(struct hci_dev *hdev)
217 struct device *dev = &hdev->dev;
219 dev->type = &bt_host;
220 dev->class = bt_class;
222 __module_get(THIS_MODULE);
223 device_initialize(dev);
226 int __init bt_sysfs_init(void)
228 bt_class = class_create(THIS_MODULE, "bluetooth");
229 #ifdef CONFIG_TIZEN_WIP
230 return PTR_RET(bt_class);
232 return PTR_ERR_OR_ZERO(bt_class);
237 void bt_sysfs_cleanup(void)
239 class_destroy(bt_class);