1 /* Bluetooth HCI driver model support. */
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/debugfs.h>
6 #include <linux/seq_file.h>
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
11 struct class *bt_class = NULL;
12 EXPORT_SYMBOL_GPL(bt_class);
14 struct dentry *bt_debugfs = NULL;
15 EXPORT_SYMBOL_GPL(bt_debugfs);
17 static struct workqueue_struct *bt_workq;
19 static inline char *link_typetostr(int type)
33 static ssize_t show_link_type(struct device *dev, struct device_attribute *attr, char *buf)
35 struct hci_conn *conn = dev_get_drvdata(dev);
36 return sprintf(buf, "%s\n", link_typetostr(conn->type));
39 static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
41 struct hci_conn *conn = dev_get_drvdata(dev);
43 baswap(&bdaddr, &conn->dst);
44 return sprintf(buf, "%s\n", batostr(&bdaddr));
47 static ssize_t show_link_features(struct device *dev, struct device_attribute *attr, char *buf)
49 struct hci_conn *conn = dev_get_drvdata(dev);
51 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
52 conn->features[0], conn->features[1],
53 conn->features[2], conn->features[3],
54 conn->features[4], conn->features[5],
55 conn->features[6], conn->features[7]);
58 #define LINK_ATTR(_name,_mode,_show,_store) \
59 struct device_attribute link_attr_##_name = __ATTR(_name,_mode,_show,_store)
61 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
62 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
63 static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
65 static struct attribute *bt_link_attrs[] = {
67 &link_attr_address.attr,
68 &link_attr_features.attr,
72 static struct attribute_group bt_link_group = {
73 .attrs = bt_link_attrs,
76 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31))
77 static const struct attribute_group *bt_link_groups[] = {
79 static struct attribute_group *bt_link_groups[] = {
85 static void bt_link_release(struct device *dev)
87 void *data = dev_get_drvdata(dev);
91 static struct device_type bt_link = {
93 .groups = bt_link_groups,
94 .release = bt_link_release,
97 static void add_conn(struct work_struct *work)
99 struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
100 struct hci_dev *hdev = conn->hdev;
102 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
104 dev_set_drvdata(&conn->dev, conn);
106 if (device_add(&conn->dev) < 0) {
107 BT_ERR("Failed to register connection device");
115 * The rfcomm tty device will possibly retain even when conn
116 * is down, and sysfs doesn't support move zombie device,
117 * so we should move the device before conn device is destroyed.
119 static int __match_tty(struct device *dev, void *data)
121 return !strncmp(dev_name(dev), "rfcomm", 6);
124 static void del_conn(struct work_struct *work)
126 struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
127 struct hci_dev *hdev = conn->hdev;
129 if (!device_is_registered(&conn->dev))
135 dev = device_find_child(&conn->dev, NULL, __match_tty);
138 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,29))
139 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
141 device_move(dev, NULL);
146 device_del(&conn->dev);
147 put_device(&conn->dev);
152 void hci_conn_init_sysfs(struct hci_conn *conn)
154 struct hci_dev *hdev = conn->hdev;
156 BT_DBG("conn %p", conn);
158 conn->dev.type = &bt_link;
159 conn->dev.class = bt_class;
160 conn->dev.parent = &hdev->dev;
162 device_initialize(&conn->dev);
164 INIT_WORK(&conn->work_add, add_conn);
165 INIT_WORK(&conn->work_del, del_conn);
168 void hci_conn_add_sysfs(struct hci_conn *conn)
170 BT_DBG("conn %p", conn);
172 queue_work(bt_workq, &conn->work_add);
175 void hci_conn_del_sysfs(struct hci_conn *conn)
177 BT_DBG("conn %p", conn);
179 queue_work(bt_workq, &conn->work_del);
182 static inline char *host_bustostr(int bus)
204 static inline char *host_typetostr(int type)
216 static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
218 struct hci_dev *hdev = dev_get_drvdata(dev);
219 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
222 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
224 struct hci_dev *hdev = dev_get_drvdata(dev);
225 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
228 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
230 struct hci_dev *hdev = dev_get_drvdata(dev);
234 for (i = 0; i < 248; i++)
235 name[i] = hdev->dev_name[i];
238 return sprintf(buf, "%s\n", name);
241 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
243 struct hci_dev *hdev = dev_get_drvdata(dev);
244 return sprintf(buf, "0x%.2x%.2x%.2x\n",
245 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
248 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
250 struct hci_dev *hdev = dev_get_drvdata(dev);
252 baswap(&bdaddr, &hdev->bdaddr);
253 return sprintf(buf, "%s\n", batostr(&bdaddr));
256 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
258 struct hci_dev *hdev = dev_get_drvdata(dev);
260 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
261 hdev->features[0], hdev->features[1],
262 hdev->features[2], hdev->features[3],
263 hdev->features[4], hdev->features[5],
264 hdev->features[6], hdev->features[7]);
267 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
269 struct hci_dev *hdev = dev_get_drvdata(dev);
270 return sprintf(buf, "%d\n", hdev->manufacturer);
273 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
275 struct hci_dev *hdev = dev_get_drvdata(dev);
276 return sprintf(buf, "%d\n", hdev->hci_ver);
279 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
281 struct hci_dev *hdev = dev_get_drvdata(dev);
282 return sprintf(buf, "%d\n", hdev->hci_rev);
285 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
287 struct hci_dev *hdev = dev_get_drvdata(dev);
288 return sprintf(buf, "%d\n", hdev->idle_timeout);
291 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
293 struct hci_dev *hdev = dev_get_drvdata(dev);
297 val = simple_strtoul(buf, &ptr, 10);
301 if (val != 0 && (val < 500 || val > 3600000))
304 hdev->idle_timeout = val;
309 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
311 struct hci_dev *hdev = dev_get_drvdata(dev);
312 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
315 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
317 struct hci_dev *hdev = dev_get_drvdata(dev);
321 val = simple_strtoul(buf, &ptr, 10);
325 if (val < 0x0002 || val > 0xFFFE || val % 2)
328 if (val < hdev->sniff_min_interval)
331 hdev->sniff_max_interval = val;
336 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
338 struct hci_dev *hdev = dev_get_drvdata(dev);
339 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
342 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
344 struct hci_dev *hdev = dev_get_drvdata(dev);
348 val = simple_strtoul(buf, &ptr, 10);
352 if (val < 0x0002 || val > 0xFFFE || val % 2)
355 if (val > hdev->sniff_max_interval)
358 hdev->sniff_min_interval = val;
363 static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
364 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
365 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
366 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
367 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
368 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
369 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
370 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
371 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
373 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
374 show_idle_timeout, store_idle_timeout);
375 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
376 show_sniff_max_interval, store_sniff_max_interval);
377 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
378 show_sniff_min_interval, store_sniff_min_interval);
380 static struct attribute *bt_host_attrs[] = {
384 &dev_attr_class.attr,
385 &dev_attr_address.attr,
386 &dev_attr_features.attr,
387 &dev_attr_manufacturer.attr,
388 &dev_attr_hci_version.attr,
389 &dev_attr_hci_revision.attr,
390 &dev_attr_idle_timeout.attr,
391 &dev_attr_sniff_max_interval.attr,
392 &dev_attr_sniff_min_interval.attr,
396 static struct attribute_group bt_host_group = {
397 .attrs = bt_host_attrs,
400 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31))
401 static const struct attribute_group *bt_host_groups[] = {
403 static struct attribute_group *bt_host_groups[] = {
409 static void bt_host_release(struct device *dev)
411 void *data = dev_get_drvdata(dev);
415 static struct device_type bt_host = {
417 .groups = bt_host_groups,
418 .release = bt_host_release,
421 static int inquiry_cache_show(struct seq_file *f, void *p)
423 struct hci_dev *hdev = f->private;
424 struct inquiry_cache *cache = &hdev->inq_cache;
425 struct inquiry_entry *e;
427 hci_dev_lock_bh(hdev);
429 for (e = cache->list; e; e = e->next) {
430 struct inquiry_data *data = &e->data;
432 baswap(&bdaddr, &data->bdaddr);
433 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
435 data->pscan_rep_mode, data->pscan_period_mode,
436 data->pscan_mode, data->dev_class[2],
437 data->dev_class[1], data->dev_class[0],
438 __le16_to_cpu(data->clock_offset),
439 data->rssi, data->ssp_mode, e->timestamp);
442 hci_dev_unlock_bh(hdev);
447 static int inquiry_cache_open(struct inode *inode, struct file *file)
449 return single_open(file, inquiry_cache_show, inode->i_private);
452 static const struct file_operations inquiry_cache_fops = {
453 .open = inquiry_cache_open,
456 .release = single_release,
459 int hci_register_sysfs(struct hci_dev *hdev)
461 struct device *dev = &hdev->dev;
464 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
466 dev->type = &bt_host;
467 dev->class = bt_class;
468 dev->parent = hdev->parent;
470 dev_set_name(dev, "%s", hdev->name);
472 dev_set_drvdata(dev, hdev);
474 err = device_register(dev);
481 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
485 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
486 hdev, &inquiry_cache_fops);
491 void hci_unregister_sysfs(struct hci_dev *hdev)
493 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
495 debugfs_remove_recursive(hdev->debugfs);
497 device_del(&hdev->dev);
500 int __init bt_sysfs_init(void)
502 bt_workq = create_singlethread_workqueue("bluetooth");
506 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
508 bt_class = class_create(THIS_MODULE, "bluetooth");
509 if (IS_ERR(bt_class)) {
510 destroy_workqueue(bt_workq);
511 return PTR_ERR(bt_class);
517 void bt_sysfs_cleanup(void)
519 class_destroy(bt_class);
521 debugfs_remove_recursive(bt_debugfs);
523 destroy_workqueue(bt_workq);