hci_sysfs.c revision b80f021f706f3578a1e80069c8e0f73b9a0ca6a7
1/* Bluetooth HCI driver model support. */
2
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>
8#include <linux/module.h>
9
10#include <net/bluetooth/bluetooth.h>
11#include <net/bluetooth/hci_core.h>
12
13static struct class *bt_class;
14
15struct dentry *bt_debugfs;
16EXPORT_SYMBOL_GPL(bt_debugfs);
17
18static inline char *link_typetostr(int type)
19{
20	switch (type) {
21	case ACL_LINK:
22		return "ACL";
23	case SCO_LINK:
24		return "SCO";
25	case ESCO_LINK:
26		return "eSCO";
27	case LE_LINK:
28		return "LE";
29	default:
30		return "UNKNOWN";
31	}
32}
33
34static ssize_t show_link_type(struct device *dev,
35			      struct device_attribute *attr, char *buf)
36{
37	struct hci_conn *conn = to_hci_conn(dev);
38	return sprintf(buf, "%s\n", link_typetostr(conn->type));
39}
40
41static ssize_t show_link_address(struct device *dev,
42				 struct device_attribute *attr, char *buf)
43{
44	struct hci_conn *conn = to_hci_conn(dev);
45	return sprintf(buf, "%s\n", batostr(&conn->dst));
46}
47
48static ssize_t show_link_features(struct device *dev,
49				  struct device_attribute *attr, char *buf)
50{
51	struct hci_conn *conn = to_hci_conn(dev);
52
53	return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
54		       conn->features[0], conn->features[1],
55		       conn->features[2], conn->features[3],
56		       conn->features[4], conn->features[5],
57		       conn->features[6], conn->features[7]);
58}
59
60#define LINK_ATTR(_name, _mode, _show, _store) \
61struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
62
63static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
64static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
65static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
66
67static struct attribute *bt_link_attrs[] = {
68	&link_attr_type.attr,
69	&link_attr_address.attr,
70	&link_attr_features.attr,
71	NULL
72};
73
74static struct attribute_group bt_link_group = {
75	.attrs = bt_link_attrs,
76};
77
78static const struct attribute_group *bt_link_groups[] = {
79	&bt_link_group,
80	NULL
81};
82
83static void bt_link_release(struct device *dev)
84{
85	struct hci_conn *conn = to_hci_conn(dev);
86	kfree(conn);
87}
88
89static struct device_type bt_link = {
90	.name    = "link",
91	.groups  = bt_link_groups,
92	.release = bt_link_release,
93};
94
95/*
96 * The rfcomm tty device will possibly retain even when conn
97 * is down, and sysfs doesn't support move zombie device,
98 * so we should move the device before conn device is destroyed.
99 */
100static int __match_tty(struct device *dev, void *data)
101{
102	return !strncmp(dev_name(dev), "rfcomm", 6);
103}
104
105void hci_conn_init_sysfs(struct hci_conn *conn)
106{
107	struct hci_dev *hdev = conn->hdev;
108
109	BT_DBG("conn %p", conn);
110
111	conn->dev.type = &bt_link;
112	conn->dev.class = bt_class;
113	conn->dev.parent = &hdev->dev;
114
115	device_initialize(&conn->dev);
116}
117
118void hci_conn_add_sysfs(struct hci_conn *conn)
119{
120	struct hci_dev *hdev = conn->hdev;
121
122	BT_DBG("conn %p", conn);
123
124	dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
125
126	if (device_add(&conn->dev) < 0) {
127		BT_ERR("Failed to register connection device");
128		return;
129	}
130
131	hci_dev_hold(hdev);
132}
133
134void hci_conn_del_sysfs(struct hci_conn *conn)
135{
136	struct hci_dev *hdev = conn->hdev;
137
138	if (!device_is_registered(&conn->dev))
139		return;
140
141	while (1) {
142		struct device *dev;
143
144		dev = device_find_child(&conn->dev, NULL, __match_tty);
145		if (!dev)
146			break;
147		device_move(dev, NULL, DPM_ORDER_DEV_LAST);
148		put_device(dev);
149	}
150
151	device_del(&conn->dev);
152	put_device(&conn->dev);
153
154	hci_dev_put(hdev);
155}
156
157static inline char *host_bustostr(int bus)
158{
159	switch (bus) {
160	case HCI_VIRTUAL:
161		return "VIRTUAL";
162	case HCI_USB:
163		return "USB";
164	case HCI_PCCARD:
165		return "PCCARD";
166	case HCI_UART:
167		return "UART";
168	case HCI_RS232:
169		return "RS232";
170	case HCI_PCI:
171		return "PCI";
172	case HCI_SDIO:
173		return "SDIO";
174	default:
175		return "UNKNOWN";
176	}
177}
178
179static inline char *host_typetostr(int type)
180{
181	switch (type) {
182	case HCI_BREDR:
183		return "BR/EDR";
184	case HCI_AMP:
185		return "AMP";
186	default:
187		return "UNKNOWN";
188	}
189}
190
191static ssize_t show_bus(struct device *dev,
192			struct device_attribute *attr, char *buf)
193{
194	struct hci_dev *hdev = to_hci_dev(dev);
195	return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
196}
197
198static ssize_t show_type(struct device *dev,
199			 struct device_attribute *attr, char *buf)
200{
201	struct hci_dev *hdev = to_hci_dev(dev);
202	return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
203}
204
205static ssize_t show_name(struct device *dev,
206			 struct device_attribute *attr, char *buf)
207{
208	struct hci_dev *hdev = to_hci_dev(dev);
209	char name[HCI_MAX_NAME_LENGTH + 1];
210	int i;
211
212	for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
213		name[i] = hdev->dev_name[i];
214
215	name[HCI_MAX_NAME_LENGTH] = '\0';
216	return sprintf(buf, "%s\n", name);
217}
218
219static ssize_t show_class(struct device *dev,
220			  struct device_attribute *attr, char *buf)
221{
222	struct hci_dev *hdev = to_hci_dev(dev);
223	return sprintf(buf, "0x%.2x%.2x%.2x\n",
224		       hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
225}
226
227static ssize_t show_address(struct device *dev,
228			    struct device_attribute *attr, char *buf)
229{
230	struct hci_dev *hdev = to_hci_dev(dev);
231	return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
232}
233
234static ssize_t show_features(struct device *dev,
235			     struct device_attribute *attr, char *buf)
236{
237	struct hci_dev *hdev = to_hci_dev(dev);
238
239	return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
240		       hdev->features[0], hdev->features[1],
241		       hdev->features[2], hdev->features[3],
242		       hdev->features[4], hdev->features[5],
243		       hdev->features[6], hdev->features[7]);
244}
245
246static ssize_t show_manufacturer(struct device *dev,
247				 struct device_attribute *attr, char *buf)
248{
249	struct hci_dev *hdev = to_hci_dev(dev);
250	return sprintf(buf, "%d\n", hdev->manufacturer);
251}
252
253static ssize_t show_hci_version(struct device *dev,
254				struct device_attribute *attr, char *buf)
255{
256	struct hci_dev *hdev = to_hci_dev(dev);
257	return sprintf(buf, "%d\n", hdev->hci_ver);
258}
259
260static ssize_t show_hci_revision(struct device *dev,
261				 struct device_attribute *attr, char *buf)
262{
263	struct hci_dev *hdev = to_hci_dev(dev);
264	return sprintf(buf, "%d\n", hdev->hci_rev);
265}
266
267static ssize_t show_idle_timeout(struct device *dev,
268				 struct device_attribute *attr, char *buf)
269{
270	struct hci_dev *hdev = to_hci_dev(dev);
271	return sprintf(buf, "%d\n", hdev->idle_timeout);
272}
273
274static ssize_t store_idle_timeout(struct device *dev,
275				  struct device_attribute *attr,
276				  const char *buf, size_t count)
277{
278	struct hci_dev *hdev = to_hci_dev(dev);
279	unsigned int val;
280	int rv;
281
282	rv = kstrtouint(buf, 0, &val);
283	if (rv < 0)
284		return rv;
285
286	if (val != 0 && (val < 500 || val > 3600000))
287		return -EINVAL;
288
289	hdev->idle_timeout = val;
290
291	return count;
292}
293
294static ssize_t show_sniff_max_interval(struct device *dev,
295				       struct device_attribute *attr, char *buf)
296{
297	struct hci_dev *hdev = to_hci_dev(dev);
298	return sprintf(buf, "%d\n", hdev->sniff_max_interval);
299}
300
301static ssize_t store_sniff_max_interval(struct device *dev,
302					struct device_attribute *attr,
303					const char *buf, size_t count)
304{
305	struct hci_dev *hdev = to_hci_dev(dev);
306	u16 val;
307	int rv;
308
309	rv = kstrtou16(buf, 0, &val);
310	if (rv < 0)
311		return rv;
312
313	if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
314		return -EINVAL;
315
316	hdev->sniff_max_interval = val;
317
318	return count;
319}
320
321static ssize_t show_sniff_min_interval(struct device *dev,
322				       struct device_attribute *attr, char *buf)
323{
324	struct hci_dev *hdev = to_hci_dev(dev);
325	return sprintf(buf, "%d\n", hdev->sniff_min_interval);
326}
327
328static ssize_t store_sniff_min_interval(struct device *dev,
329					struct device_attribute *attr,
330					const char *buf, size_t count)
331{
332	struct hci_dev *hdev = to_hci_dev(dev);
333	u16 val;
334	int rv;
335
336	rv = kstrtou16(buf, 0, &val);
337	if (rv < 0)
338		return rv;
339
340	if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
341		return -EINVAL;
342
343	hdev->sniff_min_interval = val;
344
345	return count;
346}
347
348static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
349static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
350static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
351static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
352static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
353static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
354static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
355static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
356static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
357
358static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
359		   show_idle_timeout, store_idle_timeout);
360static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
361		   show_sniff_max_interval, store_sniff_max_interval);
362static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
363		   show_sniff_min_interval, store_sniff_min_interval);
364
365static struct attribute *bt_host_attrs[] = {
366	&dev_attr_bus.attr,
367	&dev_attr_type.attr,
368	&dev_attr_name.attr,
369	&dev_attr_class.attr,
370	&dev_attr_address.attr,
371	&dev_attr_features.attr,
372	&dev_attr_manufacturer.attr,
373	&dev_attr_hci_version.attr,
374	&dev_attr_hci_revision.attr,
375	&dev_attr_idle_timeout.attr,
376	&dev_attr_sniff_max_interval.attr,
377	&dev_attr_sniff_min_interval.attr,
378	NULL
379};
380
381static struct attribute_group bt_host_group = {
382	.attrs = bt_host_attrs,
383};
384
385static const struct attribute_group *bt_host_groups[] = {
386	&bt_host_group,
387	NULL
388};
389
390static void bt_host_release(struct device *dev)
391{
392	struct hci_dev *hdev = to_hci_dev(dev);
393	kfree(hdev);
394	module_put(THIS_MODULE);
395}
396
397static struct device_type bt_host = {
398	.name    = "host",
399	.groups  = bt_host_groups,
400	.release = bt_host_release,
401};
402
403static int inquiry_cache_show(struct seq_file *f, void *p)
404{
405	struct hci_dev *hdev = f->private;
406	struct discovery_state *cache = &hdev->discovery;
407	struct inquiry_entry *e;
408
409	hci_dev_lock(hdev);
410
411	list_for_each_entry(e, &cache->all, all) {
412		struct inquiry_data *data = &e->data;
413		seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
414			   batostr(&data->bdaddr),
415			   data->pscan_rep_mode, data->pscan_period_mode,
416			   data->pscan_mode, data->dev_class[2],
417			   data->dev_class[1], data->dev_class[0],
418			   __le16_to_cpu(data->clock_offset),
419			   data->rssi, data->ssp_mode, e->timestamp);
420	}
421
422	hci_dev_unlock(hdev);
423
424	return 0;
425}
426
427static int inquiry_cache_open(struct inode *inode, struct file *file)
428{
429	return single_open(file, inquiry_cache_show, inode->i_private);
430}
431
432static const struct file_operations inquiry_cache_fops = {
433	.open		= inquiry_cache_open,
434	.read		= seq_read,
435	.llseek		= seq_lseek,
436	.release	= single_release,
437};
438
439static int blacklist_show(struct seq_file *f, void *p)
440{
441	struct hci_dev *hdev = f->private;
442	struct bdaddr_list *b;
443
444	hci_dev_lock(hdev);
445
446	list_for_each_entry(b, &hdev->blacklist, list)
447		seq_printf(f, "%s\n", batostr(&b->bdaddr));
448
449	hci_dev_unlock(hdev);
450
451	return 0;
452}
453
454static int blacklist_open(struct inode *inode, struct file *file)
455{
456	return single_open(file, blacklist_show, inode->i_private);
457}
458
459static const struct file_operations blacklist_fops = {
460	.open		= blacklist_open,
461	.read		= seq_read,
462	.llseek		= seq_lseek,
463	.release	= single_release,
464};
465
466static void print_bt_uuid(struct seq_file *f, u8 *uuid)
467{
468	__be32 data0, data4;
469	__be16 data1, data2, data3, data5;
470
471	memcpy(&data0, &uuid[0], 4);
472	memcpy(&data1, &uuid[4], 2);
473	memcpy(&data2, &uuid[6], 2);
474	memcpy(&data3, &uuid[8], 2);
475	memcpy(&data4, &uuid[10], 4);
476	memcpy(&data5, &uuid[14], 2);
477
478	seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x\n",
479		   ntohl(data0), ntohs(data1), ntohs(data2), ntohs(data3),
480		   ntohl(data4), ntohs(data5));
481}
482
483static int uuids_show(struct seq_file *f, void *p)
484{
485	struct hci_dev *hdev = f->private;
486	struct bt_uuid *uuid;
487
488	hci_dev_lock(hdev);
489
490	list_for_each_entry(uuid, &hdev->uuids, list)
491		print_bt_uuid(f, uuid->uuid);
492
493	hci_dev_unlock(hdev);
494
495	return 0;
496}
497
498static int uuids_open(struct inode *inode, struct file *file)
499{
500	return single_open(file, uuids_show, inode->i_private);
501}
502
503static const struct file_operations uuids_fops = {
504	.open		= uuids_open,
505	.read		= seq_read,
506	.llseek		= seq_lseek,
507	.release	= single_release,
508};
509
510static int auto_accept_delay_set(void *data, u64 val)
511{
512	struct hci_dev *hdev = data;
513
514	hci_dev_lock(hdev);
515
516	hdev->auto_accept_delay = val;
517
518	hci_dev_unlock(hdev);
519
520	return 0;
521}
522
523static int auto_accept_delay_get(void *data, u64 *val)
524{
525	struct hci_dev *hdev = data;
526
527	hci_dev_lock(hdev);
528
529	*val = hdev->auto_accept_delay;
530
531	hci_dev_unlock(hdev);
532
533	return 0;
534}
535
536DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
537			auto_accept_delay_set, "%llu\n");
538
539void hci_init_sysfs(struct hci_dev *hdev)
540{
541	struct device *dev = &hdev->dev;
542
543	dev->type = &bt_host;
544	dev->class = bt_class;
545
546	__module_get(THIS_MODULE);
547	device_initialize(dev);
548}
549
550int hci_add_sysfs(struct hci_dev *hdev)
551{
552	struct device *dev = &hdev->dev;
553	int err;
554
555	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
556
557	dev_set_name(dev, "%s", hdev->name);
558
559	err = device_add(dev);
560	if (err < 0)
561		return err;
562
563	if (!bt_debugfs)
564		return 0;
565
566	hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
567	if (!hdev->debugfs)
568		return 0;
569
570	debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
571			    hdev, &inquiry_cache_fops);
572
573	debugfs_create_file("blacklist", 0444, hdev->debugfs,
574			    hdev, &blacklist_fops);
575
576	debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
577
578	debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
579			    &auto_accept_delay_fops);
580	return 0;
581}
582
583void hci_del_sysfs(struct hci_dev *hdev)
584{
585	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
586
587	debugfs_remove_recursive(hdev->debugfs);
588
589	device_del(&hdev->dev);
590}
591
592int __init bt_sysfs_init(void)
593{
594	bt_debugfs = debugfs_create_dir("bluetooth", NULL);
595
596	bt_class = class_create(THIS_MODULE, "bluetooth");
597	if (IS_ERR(bt_class))
598		return PTR_ERR(bt_class);
599
600	return 0;
601}
602
603void bt_sysfs_cleanup(void)
604{
605	class_destroy(bt_class);
606
607	debugfs_remove_recursive(bt_debugfs);
608}
609