Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc
[linux-2.6/linux-2.6-stable.git] / net / bluetooth / hci_sysfs.c
bloba6c3aa8be1f79796dbfa051af03b3503f23fbe4e
1 /* Bluetooth HCI driver model support. */
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
5 #include <linux/init.h>
6 #include <linux/debugfs.h>
7 #include <linux/seq_file.h>
9 #include <net/bluetooth/bluetooth.h>
10 #include <net/bluetooth/hci_core.h>
12 static struct class *bt_class;
14 struct dentry *bt_debugfs;
15 EXPORT_SYMBOL_GPL(bt_debugfs);
17 static inline char *link_typetostr(int type)
19 switch (type) {
20 case ACL_LINK:
21 return "ACL";
22 case SCO_LINK:
23 return "SCO";
24 case ESCO_LINK:
25 return "eSCO";
26 default:
27 return "UNKNOWN";
31 static ssize_t show_link_type(struct device *dev, struct device_attribute *attr, char *buf)
33 struct hci_conn *conn = dev_get_drvdata(dev);
34 return sprintf(buf, "%s\n", link_typetostr(conn->type));
37 static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
39 struct hci_conn *conn = dev_get_drvdata(dev);
40 return sprintf(buf, "%s\n", batostr(&conn->dst));
43 static ssize_t show_link_features(struct device *dev, struct device_attribute *attr, char *buf)
45 struct hci_conn *conn = dev_get_drvdata(dev);
47 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
48 conn->features[0], conn->features[1],
49 conn->features[2], conn->features[3],
50 conn->features[4], conn->features[5],
51 conn->features[6], conn->features[7]);
54 #define LINK_ATTR(_name, _mode, _show, _store) \
55 struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
57 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
58 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
59 static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
61 static struct attribute *bt_link_attrs[] = {
62 &link_attr_type.attr,
63 &link_attr_address.attr,
64 &link_attr_features.attr,
65 NULL
68 static struct attribute_group bt_link_group = {
69 .attrs = bt_link_attrs,
72 static const struct attribute_group *bt_link_groups[] = {
73 &bt_link_group,
74 NULL
77 static void bt_link_release(struct device *dev)
79 void *data = dev_get_drvdata(dev);
80 kfree(data);
83 static struct device_type bt_link = {
84 .name = "link",
85 .groups = bt_link_groups,
86 .release = bt_link_release,
89 static void add_conn(struct work_struct *work)
91 struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
92 struct hci_dev *hdev = conn->hdev;
94 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
96 dev_set_drvdata(&conn->dev, conn);
98 if (device_add(&conn->dev) < 0) {
99 BT_ERR("Failed to register connection device");
100 return;
103 hci_dev_hold(hdev);
107 * The rfcomm tty device will possibly retain even when conn
108 * is down, and sysfs doesn't support move zombie device,
109 * so we should move the device before conn device is destroyed.
111 static int __match_tty(struct device *dev, void *data)
113 return !strncmp(dev_name(dev), "rfcomm", 6);
116 static void del_conn(struct work_struct *work)
118 struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
119 struct hci_dev *hdev = conn->hdev;
121 if (!device_is_registered(&conn->dev))
122 return;
124 while (1) {
125 struct device *dev;
127 dev = device_find_child(&conn->dev, NULL, __match_tty);
128 if (!dev)
129 break;
130 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
131 put_device(dev);
134 device_del(&conn->dev);
135 put_device(&conn->dev);
137 hci_dev_put(hdev);
140 void hci_conn_init_sysfs(struct hci_conn *conn)
142 struct hci_dev *hdev = conn->hdev;
144 BT_DBG("conn %p", conn);
146 conn->dev.type = &bt_link;
147 conn->dev.class = bt_class;
148 conn->dev.parent = &hdev->dev;
150 device_initialize(&conn->dev);
152 INIT_WORK(&conn->work_add, add_conn);
153 INIT_WORK(&conn->work_del, del_conn);
156 void hci_conn_add_sysfs(struct hci_conn *conn)
158 BT_DBG("conn %p", conn);
160 queue_work(conn->hdev->workqueue, &conn->work_add);
163 void hci_conn_del_sysfs(struct hci_conn *conn)
165 BT_DBG("conn %p", conn);
167 queue_work(conn->hdev->workqueue, &conn->work_del);
170 static inline char *host_bustostr(int bus)
172 switch (bus) {
173 case HCI_VIRTUAL:
174 return "VIRTUAL";
175 case HCI_USB:
176 return "USB";
177 case HCI_PCCARD:
178 return "PCCARD";
179 case HCI_UART:
180 return "UART";
181 case HCI_RS232:
182 return "RS232";
183 case HCI_PCI:
184 return "PCI";
185 case HCI_SDIO:
186 return "SDIO";
187 default:
188 return "UNKNOWN";
192 static inline char *host_typetostr(int type)
194 switch (type) {
195 case HCI_BREDR:
196 return "BR/EDR";
197 case HCI_AMP:
198 return "AMP";
199 default:
200 return "UNKNOWN";
204 static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
206 struct hci_dev *hdev = dev_get_drvdata(dev);
207 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
210 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
212 struct hci_dev *hdev = dev_get_drvdata(dev);
213 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
216 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
218 struct hci_dev *hdev = dev_get_drvdata(dev);
219 char name[HCI_MAX_NAME_LENGTH + 1];
220 int i;
222 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
223 name[i] = hdev->dev_name[i];
225 name[HCI_MAX_NAME_LENGTH] = '\0';
226 return sprintf(buf, "%s\n", name);
229 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
231 struct hci_dev *hdev = dev_get_drvdata(dev);
232 return sprintf(buf, "0x%.2x%.2x%.2x\n",
233 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
236 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
238 struct hci_dev *hdev = dev_get_drvdata(dev);
239 return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
242 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
244 struct hci_dev *hdev = dev_get_drvdata(dev);
246 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
247 hdev->features[0], hdev->features[1],
248 hdev->features[2], hdev->features[3],
249 hdev->features[4], hdev->features[5],
250 hdev->features[6], hdev->features[7]);
253 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
255 struct hci_dev *hdev = dev_get_drvdata(dev);
256 return sprintf(buf, "%d\n", hdev->manufacturer);
259 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
261 struct hci_dev *hdev = dev_get_drvdata(dev);
262 return sprintf(buf, "%d\n", hdev->hci_ver);
265 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
267 struct hci_dev *hdev = dev_get_drvdata(dev);
268 return sprintf(buf, "%d\n", hdev->hci_rev);
271 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
273 struct hci_dev *hdev = dev_get_drvdata(dev);
274 return sprintf(buf, "%d\n", hdev->idle_timeout);
277 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
279 struct hci_dev *hdev = dev_get_drvdata(dev);
280 unsigned int val;
281 int rv;
283 rv = kstrtouint(buf, 0, &val);
284 if (rv < 0)
285 return rv;
287 if (val != 0 && (val < 500 || val > 3600000))
288 return -EINVAL;
290 hdev->idle_timeout = val;
292 return count;
295 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
297 struct hci_dev *hdev = dev_get_drvdata(dev);
298 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
301 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
303 struct hci_dev *hdev = dev_get_drvdata(dev);
304 u16 val;
305 int rv;
307 rv = kstrtou16(buf, 0, &val);
308 if (rv < 0)
309 return rv;
311 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
312 return -EINVAL;
314 hdev->sniff_max_interval = val;
316 return count;
319 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
321 struct hci_dev *hdev = dev_get_drvdata(dev);
322 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
325 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
327 struct hci_dev *hdev = dev_get_drvdata(dev);
328 u16 val;
329 int rv;
331 rv = kstrtou16(buf, 0, &val);
332 if (rv < 0)
333 return rv;
335 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
336 return -EINVAL;
338 hdev->sniff_min_interval = val;
340 return count;
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[] = {
361 &dev_attr_bus.attr,
362 &dev_attr_type.attr,
363 &dev_attr_name.attr,
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,
373 NULL
376 static struct attribute_group bt_host_group = {
377 .attrs = bt_host_attrs,
380 static const struct attribute_group *bt_host_groups[] = {
381 &bt_host_group,
382 NULL
385 static void bt_host_release(struct device *dev)
387 void *data = dev_get_drvdata(dev);
388 kfree(data);
391 static struct device_type bt_host = {
392 .name = "host",
393 .groups = bt_host_groups,
394 .release = bt_host_release,
397 static int inquiry_cache_show(struct seq_file *f, void *p)
399 struct hci_dev *hdev = f->private;
400 struct inquiry_cache *cache = &hdev->inq_cache;
401 struct inquiry_entry *e;
403 hci_dev_lock_bh(hdev);
405 for (e = cache->list; e; e = e->next) {
406 struct inquiry_data *data = &e->data;
407 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
408 batostr(&data->bdaddr),
409 data->pscan_rep_mode, data->pscan_period_mode,
410 data->pscan_mode, data->dev_class[2],
411 data->dev_class[1], data->dev_class[0],
412 __le16_to_cpu(data->clock_offset),
413 data->rssi, data->ssp_mode, e->timestamp);
416 hci_dev_unlock_bh(hdev);
418 return 0;
421 static int inquiry_cache_open(struct inode *inode, struct file *file)
423 return single_open(file, inquiry_cache_show, inode->i_private);
426 static const struct file_operations inquiry_cache_fops = {
427 .open = inquiry_cache_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
433 static int blacklist_show(struct seq_file *f, void *p)
435 struct hci_dev *hdev = f->private;
436 struct list_head *l;
438 hci_dev_lock_bh(hdev);
440 list_for_each(l, &hdev->blacklist) {
441 struct bdaddr_list *b;
443 b = list_entry(l, struct bdaddr_list, list);
445 seq_printf(f, "%s\n", batostr(&b->bdaddr));
448 hci_dev_unlock_bh(hdev);
450 return 0;
453 static int blacklist_open(struct inode *inode, struct file *file)
455 return single_open(file, blacklist_show, inode->i_private);
458 static const struct file_operations blacklist_fops = {
459 .open = blacklist_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = single_release,
465 static void print_bt_uuid(struct seq_file *f, u8 *uuid)
467 u32 data0, data4;
468 u16 data1, data2, data3, data5;
470 memcpy(&data0, &uuid[0], 4);
471 memcpy(&data1, &uuid[4], 2);
472 memcpy(&data2, &uuid[6], 2);
473 memcpy(&data3, &uuid[8], 2);
474 memcpy(&data4, &uuid[10], 4);
475 memcpy(&data5, &uuid[14], 2);
477 seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x\n",
478 ntohl(data0), ntohs(data1), ntohs(data2),
479 ntohs(data3), ntohl(data4), ntohs(data5));
482 static int uuids_show(struct seq_file *f, void *p)
484 struct hci_dev *hdev = f->private;
485 struct list_head *l;
487 hci_dev_lock_bh(hdev);
489 list_for_each(l, &hdev->uuids) {
490 struct bt_uuid *uuid;
492 uuid = list_entry(l, struct bt_uuid, list);
494 print_bt_uuid(f, uuid->uuid);
497 hci_dev_unlock_bh(hdev);
499 return 0;
502 static int uuids_open(struct inode *inode, struct file *file)
504 return single_open(file, uuids_show, inode->i_private);
507 static const struct file_operations uuids_fops = {
508 .open = uuids_open,
509 .read = seq_read,
510 .llseek = seq_lseek,
511 .release = single_release,
514 static int auto_accept_delay_set(void *data, u64 val)
516 struct hci_dev *hdev = data;
518 hci_dev_lock_bh(hdev);
520 hdev->auto_accept_delay = val;
522 hci_dev_unlock_bh(hdev);
524 return 0;
527 static int auto_accept_delay_get(void *data, u64 *val)
529 struct hci_dev *hdev = data;
531 hci_dev_lock_bh(hdev);
533 *val = hdev->auto_accept_delay;
535 hci_dev_unlock_bh(hdev);
537 return 0;
540 DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
541 auto_accept_delay_set, "%llu\n");
543 int hci_register_sysfs(struct hci_dev *hdev)
545 struct device *dev = &hdev->dev;
546 int err;
548 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
550 dev->type = &bt_host;
551 dev->class = bt_class;
552 dev->parent = hdev->parent;
554 dev_set_name(dev, "%s", hdev->name);
556 dev_set_drvdata(dev, hdev);
558 err = device_register(dev);
559 if (err < 0)
560 return err;
562 if (!bt_debugfs)
563 return 0;
565 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
566 if (!hdev->debugfs)
567 return 0;
569 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
570 hdev, &inquiry_cache_fops);
572 debugfs_create_file("blacklist", 0444, hdev->debugfs,
573 hdev, &blacklist_fops);
575 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
577 debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
578 &auto_accept_delay_fops);
579 return 0;
582 void hci_unregister_sysfs(struct hci_dev *hdev)
584 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
586 debugfs_remove_recursive(hdev->debugfs);
588 device_del(&hdev->dev);
591 int __init bt_sysfs_init(void)
593 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
595 bt_class = class_create(THIS_MODULE, "bluetooth");
596 if (IS_ERR(bt_class))
597 return PTR_ERR(bt_class);
599 return 0;
602 void bt_sysfs_cleanup(void)
604 class_destroy(bt_class);
606 debugfs_remove_recursive(bt_debugfs);