1 /* Bluetooth HCI driver model support. */
3 #include <linux/debugfs.h>
4 #include <linux/module.h>
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
9 static struct class *bt_class;
11 struct dentry *bt_debugfs;
12 EXPORT_SYMBOL_GPL(bt_debugfs);
14 static inline char *link_typetostr(int type)
30 static ssize_t show_link_type(struct device *dev,
31 struct device_attribute *attr, char *buf)
33 struct hci_conn *conn = to_hci_conn(dev);
34 return sprintf(buf, "%s\n", link_typetostr(conn->type));
37 static ssize_t show_link_address(struct device *dev,
38 struct device_attribute *attr, char *buf)
40 struct hci_conn *conn = to_hci_conn(dev);
41 return sprintf(buf, "%pMR\n", &conn->dst);
44 static ssize_t show_link_features(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct hci_conn *conn = to_hci_conn(dev);
49 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50 conn->features[0][0], conn->features[0][1],
51 conn->features[0][2], conn->features[0][3],
52 conn->features[0][4], conn->features[0][5],
53 conn->features[0][6], conn->features[0][7]);
56 #define LINK_ATTR(_name, _mode, _show, _store) \
57 struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
59 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
60 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
61 static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
63 static struct attribute *bt_link_attrs[] = {
65 &link_attr_address.attr,
66 &link_attr_features.attr,
70 static struct attribute_group bt_link_group = {
71 .attrs = bt_link_attrs,
74 static const struct attribute_group *bt_link_groups[] = {
79 static void bt_link_release(struct device *dev)
81 struct hci_conn *conn = to_hci_conn(dev);
85 static struct device_type bt_link = {
87 .groups = bt_link_groups,
88 .release = bt_link_release,
92 * The rfcomm tty device will possibly retain even when conn
93 * is down, and sysfs doesn't support move zombie device,
94 * so we should move the device before conn device is destroyed.
96 static int __match_tty(struct device *dev, void *data)
98 return !strncmp(dev_name(dev), "rfcomm", 6);
101 void hci_conn_init_sysfs(struct hci_conn *conn)
103 struct hci_dev *hdev = conn->hdev;
105 BT_DBG("conn %p", conn);
107 conn->dev.type = &bt_link;
108 conn->dev.class = bt_class;
109 conn->dev.parent = &hdev->dev;
111 device_initialize(&conn->dev);
114 void hci_conn_add_sysfs(struct hci_conn *conn)
116 struct hci_dev *hdev = conn->hdev;
118 BT_DBG("conn %p", conn);
120 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
122 if (device_add(&conn->dev) < 0) {
123 BT_ERR("Failed to register connection device");
130 void hci_conn_del_sysfs(struct hci_conn *conn)
132 struct hci_dev *hdev = conn->hdev;
134 if (!device_is_registered(&conn->dev))
140 dev = device_find_child(&conn->dev, NULL, __match_tty);
143 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
147 device_del(&conn->dev);
152 static inline char *host_bustostr(int bus)
174 static inline char *host_typetostr(int type)
186 static ssize_t show_bus(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct hci_dev *hdev = to_hci_dev(dev);
190 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
193 static ssize_t show_type(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct hci_dev *hdev = to_hci_dev(dev);
197 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
200 static ssize_t show_name(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct hci_dev *hdev = to_hci_dev(dev);
204 char name[HCI_MAX_NAME_LENGTH + 1];
207 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
208 name[i] = hdev->dev_name[i];
210 name[HCI_MAX_NAME_LENGTH] = '\0';
211 return sprintf(buf, "%s\n", name);
214 static ssize_t show_class(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct hci_dev *hdev = to_hci_dev(dev);
218 return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
219 hdev->dev_class[1], hdev->dev_class[0]);
222 static ssize_t show_address(struct device *dev,
223 struct device_attribute *attr, char *buf)
225 struct hci_dev *hdev = to_hci_dev(dev);
226 return sprintf(buf, "%pMR\n", &hdev->bdaddr);
229 static ssize_t show_features(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct hci_dev *hdev = to_hci_dev(dev);
234 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
235 hdev->features[0][0], hdev->features[0][1],
236 hdev->features[0][2], hdev->features[0][3],
237 hdev->features[0][4], hdev->features[0][5],
238 hdev->features[0][6], hdev->features[0][7]);
241 static ssize_t show_manufacturer(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 struct hci_dev *hdev = to_hci_dev(dev);
245 return sprintf(buf, "%d\n", hdev->manufacturer);
248 static ssize_t show_hci_version(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct hci_dev *hdev = to_hci_dev(dev);
252 return sprintf(buf, "%d\n", hdev->hci_ver);
255 static ssize_t show_hci_revision(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct hci_dev *hdev = to_hci_dev(dev);
259 return sprintf(buf, "%d\n", hdev->hci_rev);
262 static ssize_t show_idle_timeout(struct device *dev,
263 struct device_attribute *attr, char *buf)
265 struct hci_dev *hdev = to_hci_dev(dev);
266 return sprintf(buf, "%d\n", hdev->idle_timeout);
269 static ssize_t store_idle_timeout(struct device *dev,
270 struct device_attribute *attr,
271 const char *buf, size_t count)
273 struct hci_dev *hdev = to_hci_dev(dev);
277 rv = kstrtouint(buf, 0, &val);
281 if (val != 0 && (val < 500 || val > 3600000))
284 hdev->idle_timeout = val;
289 static ssize_t show_sniff_max_interval(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct hci_dev *hdev = to_hci_dev(dev);
293 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
296 static ssize_t store_sniff_max_interval(struct device *dev,
297 struct device_attribute *attr,
298 const char *buf, size_t count)
300 struct hci_dev *hdev = to_hci_dev(dev);
304 rv = kstrtou16(buf, 0, &val);
308 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
311 hdev->sniff_max_interval = val;
316 static ssize_t show_sniff_min_interval(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct hci_dev *hdev = to_hci_dev(dev);
320 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
323 static ssize_t store_sniff_min_interval(struct device *dev,
324 struct device_attribute *attr,
325 const char *buf, size_t count)
327 struct hci_dev *hdev = to_hci_dev(dev);
331 rv = kstrtou16(buf, 0, &val);
335 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
338 hdev->sniff_min_interval = val;
343 static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
344 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
345 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
346 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
347 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
348 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
349 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
350 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
351 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
353 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
354 show_idle_timeout, store_idle_timeout);
355 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
356 show_sniff_max_interval, store_sniff_max_interval);
357 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
358 show_sniff_min_interval, store_sniff_min_interval);
360 static struct attribute *bt_host_attrs[] = {
364 &dev_attr_class.attr,
365 &dev_attr_address.attr,
366 &dev_attr_features.attr,
367 &dev_attr_manufacturer.attr,
368 &dev_attr_hci_version.attr,
369 &dev_attr_hci_revision.attr,
370 &dev_attr_idle_timeout.attr,
371 &dev_attr_sniff_max_interval.attr,
372 &dev_attr_sniff_min_interval.attr,
376 static struct attribute_group bt_host_group = {
377 .attrs = bt_host_attrs,
380 static const struct attribute_group *bt_host_groups[] = {
385 static void bt_host_release(struct device *dev)
387 struct hci_dev *hdev = to_hci_dev(dev);
389 module_put(THIS_MODULE);
392 static struct device_type bt_host = {
394 .groups = bt_host_groups,
395 .release = bt_host_release,
398 void hci_init_sysfs(struct hci_dev *hdev)
400 struct device *dev = &hdev->dev;
402 dev->type = &bt_host;
403 dev->class = bt_class;
405 __module_get(THIS_MODULE);
406 device_initialize(dev);
409 int hci_add_sysfs(struct hci_dev *hdev)
411 struct device *dev = &hdev->dev;
414 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
416 dev_set_name(dev, "%s", hdev->name);
418 err = device_add(dev);
425 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
432 void hci_del_sysfs(struct hci_dev *hdev)
434 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
436 debugfs_remove_recursive(hdev->debugfs);
438 device_del(&hdev->dev);
441 int __init bt_sysfs_init(void)
443 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
445 bt_class = class_create(THIS_MODULE, "bluetooth");
447 return PTR_ERR_OR_ZERO(bt_class);
450 void bt_sysfs_cleanup(void)
452 class_destroy(bt_class);
454 debugfs_remove_recursive(bt_debugfs);