]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bluetooth/hci_sysfs.c
8139cp: fix checksum broken
[net-next-2.6.git] / net / bluetooth / hci_sysfs.c
CommitLineData
1da177e4
LT
1/* Bluetooth HCI driver model support. */
2
1da177e4 3#include <linux/kernel.h>
5a0e3ad6 4#include <linux/slab.h>
1da177e4 5#include <linux/init.h>
ca325f69 6#include <linux/debugfs.h>
d4612cb8 7#include <linux/seq_file.h>
1da177e4
LT
8
9#include <net/bluetooth/bluetooth.h>
10#include <net/bluetooth/hci_core.h>
11
aef7d97c 12static struct class *bt_class;
90855d7b 13
ca325f69
MH
14struct dentry *bt_debugfs = NULL;
15EXPORT_SYMBOL_GPL(bt_debugfs);
16
90855d7b
MH
17static inline char *link_typetostr(int type)
18{
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";
28 }
29}
30
31static ssize_t show_link_type(struct device *dev, struct device_attribute *attr, char *buf)
32{
33 struct hci_conn *conn = dev_get_drvdata(dev);
34 return sprintf(buf, "%s\n", link_typetostr(conn->type));
35}
36
37static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
38{
39 struct hci_conn *conn = dev_get_drvdata(dev);
d6b2eb2f 40 return sprintf(buf, "%s\n", batostr(&conn->dst));
90855d7b
MH
41}
42
43static ssize_t show_link_features(struct device *dev, struct device_attribute *attr, char *buf)
44{
45 struct hci_conn *conn = dev_get_drvdata(dev);
46
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]);
52}
53
54#define LINK_ATTR(_name,_mode,_show,_store) \
55struct device_attribute link_attr_##_name = __ATTR(_name,_mode,_show,_store)
56
57static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
58static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
59static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
60
61static struct attribute *bt_link_attrs[] = {
62 &link_attr_type.attr,
63 &link_attr_address.attr,
64 &link_attr_features.attr,
65 NULL
66};
67
68static struct attribute_group bt_link_group = {
69 .attrs = bt_link_attrs,
70};
71
a4dbd674 72static const struct attribute_group *bt_link_groups[] = {
90855d7b
MH
73 &bt_link_group,
74 NULL
75};
76
77static void bt_link_release(struct device *dev)
78{
79 void *data = dev_get_drvdata(dev);
80 kfree(data);
81}
82
83static struct device_type bt_link = {
84 .name = "link",
85 .groups = bt_link_groups,
86 .release = bt_link_release,
87};
88
89static void add_conn(struct work_struct *work)
90{
f3784d83 91 struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
457ca7bb 92 struct hci_dev *hdev = conn->hdev;
90855d7b 93
457ca7bb
MH
94 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
95
f74c77cb
DY
96 dev_set_drvdata(&conn->dev, conn);
97
90855d7b
MH
98 if (device_add(&conn->dev) < 0) {
99 BT_ERR("Failed to register connection device");
100 return;
101 }
384943ec
MH
102
103 hci_dev_hold(hdev);
90855d7b
MH
104}
105
90855d7b
MH
106/*
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.
110 */
111static int __match_tty(struct device *dev, void *data)
112{
fb28ad35 113 return !strncmp(dev_name(dev), "rfcomm", 6);
90855d7b
MH
114}
115
116static void del_conn(struct work_struct *work)
117{
f3784d83 118 struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
90855d7b
MH
119 struct hci_dev *hdev = conn->hdev;
120
a67e899c
MH
121 if (!device_is_registered(&conn->dev))
122 return;
f3784d83 123
90855d7b
MH
124 while (1) {
125 struct device *dev;
126
127 dev = device_find_child(&conn->dev, NULL, __match_tty);
128 if (!dev)
129 break;
ffa6a705 130 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
90855d7b
MH
131 put_device(dev);
132 }
133
134 device_del(&conn->dev);
135 put_device(&conn->dev);
384943ec 136
90855d7b
MH
137 hci_dev_put(hdev);
138}
139
a67e899c 140void hci_conn_init_sysfs(struct hci_conn *conn)
90855d7b 141{
a67e899c
MH
142 struct hci_dev *hdev = conn->hdev;
143
90855d7b
MH
144 BT_DBG("conn %p", conn);
145
a67e899c
MH
146 conn->dev.type = &bt_link;
147 conn->dev.class = bt_class;
148 conn->dev.parent = &hdev->dev;
149
a67e899c
MH
150 device_initialize(&conn->dev);
151
152 INIT_WORK(&conn->work_add, add_conn);
f3784d83 153 INIT_WORK(&conn->work_del, del_conn);
a67e899c
MH
154}
155
156void hci_conn_add_sysfs(struct hci_conn *conn)
157{
a67e899c
MH
158 BT_DBG("conn %p", conn);
159
f48fd9c8 160 queue_work(conn->hdev->workqueue, &conn->work_add);
a67e899c
MH
161}
162
163void hci_conn_del_sysfs(struct hci_conn *conn)
164{
165 BT_DBG("conn %p", conn);
90855d7b 166
f48fd9c8 167 queue_work(conn->hdev->workqueue, &conn->work_del);
90855d7b
MH
168}
169
c13854ce 170static inline char *host_bustostr(int bus)
1da177e4 171{
c13854ce 172 switch (bus) {
0ac53939 173 case HCI_VIRTUAL:
4d0eb004
MH
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";
0ac53939
MH
185 case HCI_SDIO:
186 return "SDIO";
4d0eb004
MH
187 default:
188 return "UNKNOWN";
189 }
1da177e4
LT
190}
191
943da25d
MH
192static inline char *host_typetostr(int type)
193{
194 switch (type) {
195 case HCI_BREDR:
196 return "BR/EDR";
8f1e1742
DV
197 case HCI_AMP:
198 return "AMP";
943da25d
MH
199 default:
200 return "UNKNOWN";
201 }
202}
203
c13854ce 204static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 205{
a91f2e39 206 struct hci_dev *hdev = dev_get_drvdata(dev);
c13854ce 207 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
1da177e4
LT
208}
209
943da25d
MH
210static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
211{
212 struct hci_dev *hdev = dev_get_drvdata(dev);
213 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
214}
215
a9de9248
MH
216static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
217{
218 struct hci_dev *hdev = dev_get_drvdata(dev);
219 char name[249];
220 int i;
221
222 for (i = 0; i < 248; i++)
223 name[i] = hdev->dev_name[i];
224
225 name[248] = '\0';
226 return sprintf(buf, "%s\n", name);
227}
228
229static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
230{
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]);
234}
235
a91f2e39 236static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 237{
a91f2e39 238 struct hci_dev *hdev = dev_get_drvdata(dev);
d6b2eb2f 239 return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
1da177e4
LT
240}
241
a9de9248
MH
242static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
243{
244 struct hci_dev *hdev = dev_get_drvdata(dev);
245
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]);
251}
252
1143e5a6
MH
253static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
254{
255 struct hci_dev *hdev = dev_get_drvdata(dev);
256 return sprintf(buf, "%d\n", hdev->manufacturer);
257}
258
259static ssize_t show_hci_version(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->hci_ver);
263}
264
265static ssize_t show_hci_revision(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_rev);
269}
270
a91f2e39 271static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 272{
a91f2e39 273 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
274 return sprintf(buf, "%d\n", hdev->idle_timeout);
275}
276
a91f2e39 277static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 278{
a91f2e39 279 struct hci_dev *hdev = dev_get_drvdata(dev);
7b767cad 280 unsigned long val;
04837f64 281
7b767cad 282 if (strict_strtoul(buf, 0, &val) < 0)
04837f64
MH
283 return -EINVAL;
284
285 if (val != 0 && (val < 500 || val > 3600000))
286 return -EINVAL;
287
288 hdev->idle_timeout = val;
289
290 return count;
291}
292
a91f2e39 293static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 294{
a91f2e39 295 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
296 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
297}
298
a91f2e39 299static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 300{
a91f2e39 301 struct hci_dev *hdev = dev_get_drvdata(dev);
7b767cad 302 unsigned long val;
04837f64 303
7b767cad 304 if (strict_strtoul(buf, 0, &val) < 0)
04837f64
MH
305 return -EINVAL;
306
307 if (val < 0x0002 || val > 0xFFFE || val % 2)
308 return -EINVAL;
309
310 if (val < hdev->sniff_min_interval)
311 return -EINVAL;
312
313 hdev->sniff_max_interval = val;
314
315 return count;
316}
317
a91f2e39 318static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 319{
a91f2e39 320 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
321 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
322}
323
a91f2e39 324static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 325{
a91f2e39 326 struct hci_dev *hdev = dev_get_drvdata(dev);
7b767cad 327 unsigned long val;
04837f64 328
7b767cad 329 if (strict_strtoul(buf, 0, &val) < 0)
04837f64
MH
330 return -EINVAL;
331
332 if (val < 0x0002 || val > 0xFFFE || val % 2)
333 return -EINVAL;
334
335 if (val > hdev->sniff_max_interval)
336 return -EINVAL;
337
338 hdev->sniff_min_interval = val;
339
340 return count;
341}
342
c13854ce 343static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
943da25d 344static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
a9de9248
MH
345static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
346static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
a91f2e39 347static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
a9de9248 348static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
1143e5a6
MH
349static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
350static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
351static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
1da177e4 352
a91f2e39 353static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
04837f64 354 show_idle_timeout, store_idle_timeout);
a91f2e39 355static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
04837f64 356 show_sniff_max_interval, store_sniff_max_interval);
a91f2e39 357static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
04837f64
MH
358 show_sniff_min_interval, store_sniff_min_interval);
359
90855d7b 360static struct attribute *bt_host_attrs[] = {
c13854ce 361 &dev_attr_bus.attr,
943da25d 362 &dev_attr_type.attr,
90855d7b
MH
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,
90855d7b
MH
370 &dev_attr_idle_timeout.attr,
371 &dev_attr_sniff_max_interval.attr,
372 &dev_attr_sniff_min_interval.attr,
1da177e4
LT
373 NULL
374};
375
90855d7b
MH
376static struct attribute_group bt_host_group = {
377 .attrs = bt_host_attrs,
b219e3ac
MH
378};
379
a4dbd674 380static const struct attribute_group *bt_host_groups[] = {
90855d7b
MH
381 &bt_host_group,
382 NULL
27d35284
MH
383};
384
90855d7b 385static void bt_host_release(struct device *dev)
a91f2e39 386{
b219e3ac
MH
387 void *data = dev_get_drvdata(dev);
388 kfree(data);
389}
390
90855d7b
MH
391static struct device_type bt_host = {
392 .name = "host",
393 .groups = bt_host_groups,
394 .release = bt_host_release,
395};
a91f2e39 396
d4612cb8 397static int inquiry_cache_show(struct seq_file *f, void *p)
ca325f69 398{
d4612cb8 399 struct hci_dev *hdev = f->private;
ca325f69
MH
400 struct inquiry_cache *cache = &hdev->inq_cache;
401 struct inquiry_entry *e;
ca325f69
MH
402
403 hci_dev_lock_bh(hdev);
404
405 for (e = cache->list; e; e = e->next) {
406 struct inquiry_data *data = &e->data;
d4612cb8 407 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
d6b2eb2f 408 batostr(&data->bdaddr),
d4612cb8
MH
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);
ca325f69
MH
414 }
415
416 hci_dev_unlock_bh(hdev);
417
d4612cb8
MH
418 return 0;
419}
420
421static int inquiry_cache_open(struct inode *inode, struct file *file)
422{
423 return single_open(file, inquiry_cache_show, inode->i_private);
ca325f69
MH
424}
425
426static const struct file_operations inquiry_cache_fops = {
d4612cb8
MH
427 .open = inquiry_cache_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
ca325f69
MH
431};
432
32c2ece5
JH
433static int blacklist_show(struct seq_file *f, void *p)
434{
435 struct hci_dev *hdev = f->private;
32c2ece5
JH
436 struct list_head *l;
437
438 hci_dev_lock_bh(hdev);
439
ea4bd8ba 440 list_for_each(l, &hdev->blacklist) {
32c2ece5 441 struct bdaddr_list *b;
32c2ece5
JH
442
443 b = list_entry(l, struct bdaddr_list, list);
444
d6b2eb2f 445 seq_printf(f, "%s\n", batostr(&b->bdaddr));
32c2ece5
JH
446 }
447
448 hci_dev_unlock_bh(hdev);
449
450 return 0;
451}
452
453static int blacklist_open(struct inode *inode, struct file *file)
454{
455 return single_open(file, blacklist_show, inode->i_private);
456}
457
458static const struct file_operations blacklist_fops = {
459 .open = blacklist_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = single_release,
463};
1da177e4
LT
464int hci_register_sysfs(struct hci_dev *hdev)
465{
a91f2e39 466 struct device *dev = &hdev->dev;
1da177e4
LT
467 int err;
468
c13854ce 469 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1da177e4 470
90855d7b
MH
471 dev->type = &bt_host;
472 dev->class = bt_class;
e9c4bec6 473 dev->parent = hdev->parent;
a91f2e39 474
2e792995 475 dev_set_name(dev, "%s", hdev->name);
1da177e4 476
a91f2e39 477 dev_set_drvdata(dev, hdev);
27d35284 478
a91f2e39 479 err = device_register(dev);
1da177e4
LT
480 if (err < 0)
481 return err;
482
ca325f69
MH
483 if (!bt_debugfs)
484 return 0;
485
486 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
487 if (!hdev->debugfs)
488 return 0;
489
490 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
491 hdev, &inquiry_cache_fops);
492
32c2ece5
JH
493 debugfs_create_file("blacklist", 0444, hdev->debugfs,
494 hdev, &blacklist_fops);
495
1da177e4
LT
496 return 0;
497}
498
499void hci_unregister_sysfs(struct hci_dev *hdev)
500{
c13854ce 501 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1da177e4 502
ca325f69
MH
503 debugfs_remove_recursive(hdev->debugfs);
504
4d0eb004 505 device_del(&hdev->dev);
1da177e4
LT
506}
507
508int __init bt_sysfs_init(void)
509{
ca325f69
MH
510 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
511
a91f2e39 512 bt_class = class_create(THIS_MODULE, "bluetooth");
f48fd9c8 513 if (IS_ERR(bt_class))
90855d7b 514 return PTR_ERR(bt_class);
27d35284
MH
515
516 return 0;
1da177e4
LT
517}
518
860e13b5 519void bt_sysfs_cleanup(void)
1da177e4 520{
a91f2e39 521 class_destroy(bt_class);
ca325f69
MH
522
523 debugfs_remove_recursive(bt_debugfs);
1da177e4 524}