xref: /linux/net/bluetooth/hci_sysfs.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
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 
9 #include <net/bluetooth/bluetooth.h>
10 #include <net/bluetooth/hci_core.h>
11 
12 static struct class *bt_class;
13 
14 struct dentry *bt_debugfs = NULL;
15 EXPORT_SYMBOL_GPL(bt_debugfs);
16 
17 static struct workqueue_struct *bt_workq;
18 
19 static inline char *link_typetostr(int type)
20 {
21 	switch (type) {
22 	case ACL_LINK:
23 		return "ACL";
24 	case SCO_LINK:
25 		return "SCO";
26 	case ESCO_LINK:
27 		return "eSCO";
28 	default:
29 		return "UNKNOWN";
30 	}
31 }
32 
33 static ssize_t show_link_type(struct device *dev, struct device_attribute *attr, char *buf)
34 {
35 	struct hci_conn *conn = dev_get_drvdata(dev);
36 	return sprintf(buf, "%s\n", link_typetostr(conn->type));
37 }
38 
39 static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
40 {
41 	struct hci_conn *conn = dev_get_drvdata(dev);
42 	bdaddr_t bdaddr;
43 	baswap(&bdaddr, &conn->dst);
44 	return sprintf(buf, "%s\n", batostr(&bdaddr));
45 }
46 
47 static ssize_t show_link_features(struct device *dev, struct device_attribute *attr, char *buf)
48 {
49 	struct hci_conn *conn = dev_get_drvdata(dev);
50 
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]);
56 }
57 
58 #define LINK_ATTR(_name,_mode,_show,_store) \
59 struct device_attribute link_attr_##_name = __ATTR(_name,_mode,_show,_store)
60 
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);
64 
65 static struct attribute *bt_link_attrs[] = {
66 	&link_attr_type.attr,
67 	&link_attr_address.attr,
68 	&link_attr_features.attr,
69 	NULL
70 };
71 
72 static struct attribute_group bt_link_group = {
73 	.attrs = bt_link_attrs,
74 };
75 
76 static const struct attribute_group *bt_link_groups[] = {
77 	&bt_link_group,
78 	NULL
79 };
80 
81 static void bt_link_release(struct device *dev)
82 {
83 	void *data = dev_get_drvdata(dev);
84 	kfree(data);
85 }
86 
87 static struct device_type bt_link = {
88 	.name    = "link",
89 	.groups  = bt_link_groups,
90 	.release = bt_link_release,
91 };
92 
93 static void add_conn(struct work_struct *work)
94 {
95 	struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
96 	struct hci_dev *hdev = conn->hdev;
97 
98 	dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
99 
100 	dev_set_drvdata(&conn->dev, conn);
101 
102 	if (device_add(&conn->dev) < 0) {
103 		BT_ERR("Failed to register connection device");
104 		return;
105 	}
106 
107 	hci_dev_hold(hdev);
108 }
109 
110 /*
111  * The rfcomm tty device will possibly retain even when conn
112  * is down, and sysfs doesn't support move zombie device,
113  * so we should move the device before conn device is destroyed.
114  */
115 static int __match_tty(struct device *dev, void *data)
116 {
117 	return !strncmp(dev_name(dev), "rfcomm", 6);
118 }
119 
120 static void del_conn(struct work_struct *work)
121 {
122 	struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
123 	struct hci_dev *hdev = conn->hdev;
124 
125 	if (!device_is_registered(&conn->dev))
126 		return;
127 
128 	while (1) {
129 		struct device *dev;
130 
131 		dev = device_find_child(&conn->dev, NULL, __match_tty);
132 		if (!dev)
133 			break;
134 		device_move(dev, NULL, DPM_ORDER_DEV_LAST);
135 		put_device(dev);
136 	}
137 
138 	device_del(&conn->dev);
139 	put_device(&conn->dev);
140 
141 	hci_dev_put(hdev);
142 }
143 
144 void hci_conn_init_sysfs(struct hci_conn *conn)
145 {
146 	struct hci_dev *hdev = conn->hdev;
147 
148 	BT_DBG("conn %p", conn);
149 
150 	conn->dev.type = &bt_link;
151 	conn->dev.class = bt_class;
152 	conn->dev.parent = &hdev->dev;
153 
154 	device_initialize(&conn->dev);
155 
156 	INIT_WORK(&conn->work_add, add_conn);
157 	INIT_WORK(&conn->work_del, del_conn);
158 }
159 
160 void hci_conn_add_sysfs(struct hci_conn *conn)
161 {
162 	BT_DBG("conn %p", conn);
163 
164 	queue_work(bt_workq, &conn->work_add);
165 }
166 
167 void hci_conn_del_sysfs(struct hci_conn *conn)
168 {
169 	BT_DBG("conn %p", conn);
170 
171 	queue_work(bt_workq, &conn->work_del);
172 }
173 
174 static inline char *host_bustostr(int bus)
175 {
176 	switch (bus) {
177 	case HCI_VIRTUAL:
178 		return "VIRTUAL";
179 	case HCI_USB:
180 		return "USB";
181 	case HCI_PCCARD:
182 		return "PCCARD";
183 	case HCI_UART:
184 		return "UART";
185 	case HCI_RS232:
186 		return "RS232";
187 	case HCI_PCI:
188 		return "PCI";
189 	case HCI_SDIO:
190 		return "SDIO";
191 	default:
192 		return "UNKNOWN";
193 	}
194 }
195 
196 static inline char *host_typetostr(int type)
197 {
198 	switch (type) {
199 	case HCI_BREDR:
200 		return "BR/EDR";
201 	case HCI_80211:
202 		return "802.11";
203 	default:
204 		return "UNKNOWN";
205 	}
206 }
207 
208 static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
209 {
210 	struct hci_dev *hdev = dev_get_drvdata(dev);
211 	return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
212 }
213 
214 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
215 {
216 	struct hci_dev *hdev = dev_get_drvdata(dev);
217 	return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
218 }
219 
220 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
221 {
222 	struct hci_dev *hdev = dev_get_drvdata(dev);
223 	char name[249];
224 	int i;
225 
226 	for (i = 0; i < 248; i++)
227 		name[i] = hdev->dev_name[i];
228 
229 	name[248] = '\0';
230 	return sprintf(buf, "%s\n", name);
231 }
232 
233 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
234 {
235 	struct hci_dev *hdev = dev_get_drvdata(dev);
236 	return sprintf(buf, "0x%.2x%.2x%.2x\n",
237 			hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
238 }
239 
240 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
241 {
242 	struct hci_dev *hdev = dev_get_drvdata(dev);
243 	bdaddr_t bdaddr;
244 	baswap(&bdaddr, &hdev->bdaddr);
245 	return sprintf(buf, "%s\n", batostr(&bdaddr));
246 }
247 
248 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
249 {
250 	struct hci_dev *hdev = dev_get_drvdata(dev);
251 
252 	return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
253 				hdev->features[0], hdev->features[1],
254 				hdev->features[2], hdev->features[3],
255 				hdev->features[4], hdev->features[5],
256 				hdev->features[6], hdev->features[7]);
257 }
258 
259 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
260 {
261 	struct hci_dev *hdev = dev_get_drvdata(dev);
262 	return sprintf(buf, "%d\n", hdev->manufacturer);
263 }
264 
265 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
266 {
267 	struct hci_dev *hdev = dev_get_drvdata(dev);
268 	return sprintf(buf, "%d\n", hdev->hci_ver);
269 }
270 
271 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
272 {
273 	struct hci_dev *hdev = dev_get_drvdata(dev);
274 	return sprintf(buf, "%d\n", hdev->hci_rev);
275 }
276 
277 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
278 {
279 	struct hci_dev *hdev = dev_get_drvdata(dev);
280 	return sprintf(buf, "%d\n", hdev->idle_timeout);
281 }
282 
283 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
284 {
285 	struct hci_dev *hdev = dev_get_drvdata(dev);
286 	char *ptr;
287 	__u32 val;
288 
289 	val = simple_strtoul(buf, &ptr, 10);
290 	if (ptr == buf)
291 		return -EINVAL;
292 
293 	if (val != 0 && (val < 500 || val > 3600000))
294 		return -EINVAL;
295 
296 	hdev->idle_timeout = val;
297 
298 	return count;
299 }
300 
301 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
302 {
303 	struct hci_dev *hdev = dev_get_drvdata(dev);
304 	return sprintf(buf, "%d\n", hdev->sniff_max_interval);
305 }
306 
307 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
308 {
309 	struct hci_dev *hdev = dev_get_drvdata(dev);
310 	char *ptr;
311 	__u16 val;
312 
313 	val = simple_strtoul(buf, &ptr, 10);
314 	if (ptr == buf)
315 		return -EINVAL;
316 
317 	if (val < 0x0002 || val > 0xFFFE || val % 2)
318 		return -EINVAL;
319 
320 	if (val < hdev->sniff_min_interval)
321 		return -EINVAL;
322 
323 	hdev->sniff_max_interval = val;
324 
325 	return count;
326 }
327 
328 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
329 {
330 	struct hci_dev *hdev = dev_get_drvdata(dev);
331 	return sprintf(buf, "%d\n", hdev->sniff_min_interval);
332 }
333 
334 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
335 {
336 	struct hci_dev *hdev = dev_get_drvdata(dev);
337 	char *ptr;
338 	__u16 val;
339 
340 	val = simple_strtoul(buf, &ptr, 10);
341 	if (ptr == buf)
342 		return -EINVAL;
343 
344 	if (val < 0x0002 || val > 0xFFFE || val % 2)
345 		return -EINVAL;
346 
347 	if (val > hdev->sniff_max_interval)
348 		return -EINVAL;
349 
350 	hdev->sniff_min_interval = val;
351 
352 	return count;
353 }
354 
355 static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
356 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
357 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
358 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
359 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
360 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
361 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
362 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
363 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
364 
365 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
366 				show_idle_timeout, store_idle_timeout);
367 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
368 				show_sniff_max_interval, store_sniff_max_interval);
369 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
370 				show_sniff_min_interval, store_sniff_min_interval);
371 
372 static struct attribute *bt_host_attrs[] = {
373 	&dev_attr_bus.attr,
374 	&dev_attr_type.attr,
375 	&dev_attr_name.attr,
376 	&dev_attr_class.attr,
377 	&dev_attr_address.attr,
378 	&dev_attr_features.attr,
379 	&dev_attr_manufacturer.attr,
380 	&dev_attr_hci_version.attr,
381 	&dev_attr_hci_revision.attr,
382 	&dev_attr_idle_timeout.attr,
383 	&dev_attr_sniff_max_interval.attr,
384 	&dev_attr_sniff_min_interval.attr,
385 	NULL
386 };
387 
388 static struct attribute_group bt_host_group = {
389 	.attrs = bt_host_attrs,
390 };
391 
392 static const struct attribute_group *bt_host_groups[] = {
393 	&bt_host_group,
394 	NULL
395 };
396 
397 static void bt_host_release(struct device *dev)
398 {
399 	void *data = dev_get_drvdata(dev);
400 	kfree(data);
401 }
402 
403 static struct device_type bt_host = {
404 	.name    = "host",
405 	.groups  = bt_host_groups,
406 	.release = bt_host_release,
407 };
408 
409 static int inquiry_cache_show(struct seq_file *f, void *p)
410 {
411 	struct hci_dev *hdev = f->private;
412 	struct inquiry_cache *cache = &hdev->inq_cache;
413 	struct inquiry_entry *e;
414 
415 	hci_dev_lock_bh(hdev);
416 
417 	for (e = cache->list; e; e = e->next) {
418 		struct inquiry_data *data = &e->data;
419 		bdaddr_t bdaddr;
420 		baswap(&bdaddr, &data->bdaddr);
421 		seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
422 			   batostr(&bdaddr),
423 			   data->pscan_rep_mode, data->pscan_period_mode,
424 			   data->pscan_mode, data->dev_class[2],
425 			   data->dev_class[1], data->dev_class[0],
426 			   __le16_to_cpu(data->clock_offset),
427 			   data->rssi, data->ssp_mode, e->timestamp);
428 	}
429 
430 	hci_dev_unlock_bh(hdev);
431 
432 	return 0;
433 }
434 
435 static int inquiry_cache_open(struct inode *inode, struct file *file)
436 {
437 	return single_open(file, inquiry_cache_show, inode->i_private);
438 }
439 
440 static const struct file_operations inquiry_cache_fops = {
441 	.open		= inquiry_cache_open,
442 	.read		= seq_read,
443 	.llseek		= seq_lseek,
444 	.release	= single_release,
445 };
446 
447 int hci_register_sysfs(struct hci_dev *hdev)
448 {
449 	struct device *dev = &hdev->dev;
450 	int err;
451 
452 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
453 
454 	dev->type = &bt_host;
455 	dev->class = bt_class;
456 	dev->parent = hdev->parent;
457 
458 	dev_set_name(dev, "%s", hdev->name);
459 
460 	dev_set_drvdata(dev, hdev);
461 
462 	err = device_register(dev);
463 	if (err < 0)
464 		return err;
465 
466 	if (!bt_debugfs)
467 		return 0;
468 
469 	hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
470 	if (!hdev->debugfs)
471 		return 0;
472 
473 	debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
474 						hdev, &inquiry_cache_fops);
475 
476 	return 0;
477 }
478 
479 void hci_unregister_sysfs(struct hci_dev *hdev)
480 {
481 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
482 
483 	debugfs_remove_recursive(hdev->debugfs);
484 
485 	device_del(&hdev->dev);
486 }
487 
488 int __init bt_sysfs_init(void)
489 {
490 	bt_workq = create_singlethread_workqueue("bluetooth");
491 	if (!bt_workq)
492 		return -ENOMEM;
493 
494 	bt_debugfs = debugfs_create_dir("bluetooth", NULL);
495 
496 	bt_class = class_create(THIS_MODULE, "bluetooth");
497 	if (IS_ERR(bt_class)) {
498 		destroy_workqueue(bt_workq);
499 		return PTR_ERR(bt_class);
500 	}
501 
502 	return 0;
503 }
504 
505 void bt_sysfs_cleanup(void)
506 {
507 	class_destroy(bt_class);
508 
509 	debugfs_remove_recursive(bt_debugfs);
510 
511 	destroy_workqueue(bt_workq);
512 }
513