Bluetooth: Fix coding style in the subsystem
[deliverable/linux.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>
3a9a231d 8#include <linux/module.h>
1da177e4
LT
9
10#include <net/bluetooth/bluetooth.h>
11#include <net/bluetooth/hci_core.h>
12
aef7d97c 13static struct class *bt_class;
90855d7b 14
602f9887 15struct dentry *bt_debugfs;
ca325f69
MH
16EXPORT_SYMBOL_GPL(bt_debugfs);
17
90855d7b
MH
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";
21061df3
PH
27 case LE_LINK:
28 return "LE";
90855d7b
MH
29 default:
30 return "UNKNOWN";
31 }
32}
33
b80f021f
GP
34static ssize_t show_link_type(struct device *dev,
35 struct device_attribute *attr, char *buf)
90855d7b 36{
3dc07322 37 struct hci_conn *conn = to_hci_conn(dev);
90855d7b
MH
38 return sprintf(buf, "%s\n", link_typetostr(conn->type));
39}
40
b80f021f
GP
41static ssize_t show_link_address(struct device *dev,
42 struct device_attribute *attr, char *buf)
90855d7b 43{
3dc07322 44 struct hci_conn *conn = to_hci_conn(dev);
d6b2eb2f 45 return sprintf(buf, "%s\n", batostr(&conn->dst));
90855d7b
MH
46}
47
b80f021f
GP
48static ssize_t show_link_features(struct device *dev,
49 struct device_attribute *attr, char *buf)
90855d7b 50{
3dc07322 51 struct hci_conn *conn = to_hci_conn(dev);
90855d7b
MH
52
53 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
b80f021f
GP
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]);
90855d7b
MH
58}
59
602f9887
GP
60#define LINK_ATTR(_name, _mode, _show, _store) \
61struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
90855d7b
MH
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
a4dbd674 78static const struct attribute_group *bt_link_groups[] = {
90855d7b
MH
79 &bt_link_group,
80 NULL
81};
82
83static void bt_link_release(struct device *dev)
84{
2dd10688
DH
85 struct hci_conn *conn = to_hci_conn(dev);
86 kfree(conn);
90855d7b
MH
87}
88
89static struct device_type bt_link = {
90 .name = "link",
91 .groups = bt_link_groups,
92 .release = bt_link_release,
93};
94
6d438e33
GP
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)
90855d7b 119{
457ca7bb 120 struct hci_dev *hdev = conn->hdev;
90855d7b 121
6d438e33
GP
122 BT_DBG("conn %p", conn);
123
457ca7bb
MH
124 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
125
90855d7b
MH
126 if (device_add(&conn->dev) < 0) {
127 BT_ERR("Failed to register connection device");
128 return;
129 }
384943ec
MH
130
131 hci_dev_hold(hdev);
90855d7b
MH
132}
133
6d438e33 134void hci_conn_del_sysfs(struct hci_conn *conn)
90855d7b 135{
90855d7b
MH
136 struct hci_dev *hdev = conn->hdev;
137
a67e899c
MH
138 if (!device_is_registered(&conn->dev))
139 return;
f3784d83 140
90855d7b
MH
141 while (1) {
142 struct device *dev;
143
144 dev = device_find_child(&conn->dev, NULL, __match_tty);
145 if (!dev)
146 break;
ffa6a705 147 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
90855d7b
MH
148 put_device(dev);
149 }
150
151 device_del(&conn->dev);
152 put_device(&conn->dev);
384943ec 153
90855d7b
MH
154 hci_dev_put(hdev);
155}
156
c13854ce 157static inline char *host_bustostr(int bus)
1da177e4 158{
c13854ce 159 switch (bus) {
0ac53939 160 case HCI_VIRTUAL:
4d0eb004
MH
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";
0ac53939
MH
172 case HCI_SDIO:
173 return "SDIO";
4d0eb004
MH
174 default:
175 return "UNKNOWN";
176 }
1da177e4
LT
177}
178
943da25d
MH
179static inline char *host_typetostr(int type)
180{
181 switch (type) {
182 case HCI_BREDR:
183 return "BR/EDR";
8f1e1742
DV
184 case HCI_AMP:
185 return "AMP";
943da25d
MH
186 default:
187 return "UNKNOWN";
188 }
189}
190
b80f021f
GP
191static ssize_t show_bus(struct device *dev,
192 struct device_attribute *attr, char *buf)
1da177e4 193{
aa2b86d7 194 struct hci_dev *hdev = to_hci_dev(dev);
c13854ce 195 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
1da177e4
LT
196}
197
b80f021f
GP
198static ssize_t show_type(struct device *dev,
199 struct device_attribute *attr, char *buf)
943da25d 200{
aa2b86d7 201 struct hci_dev *hdev = to_hci_dev(dev);
943da25d
MH
202 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
203}
204
b80f021f
GP
205static ssize_t show_name(struct device *dev,
206 struct device_attribute *attr, char *buf)
a9de9248 207{
aa2b86d7 208 struct hci_dev *hdev = to_hci_dev(dev);
1f6c6378 209 char name[HCI_MAX_NAME_LENGTH + 1];
a9de9248
MH
210 int i;
211
1f6c6378 212 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
a9de9248
MH
213 name[i] = hdev->dev_name[i];
214
1f6c6378 215 name[HCI_MAX_NAME_LENGTH] = '\0';
a9de9248
MH
216 return sprintf(buf, "%s\n", name);
217}
218
b80f021f
GP
219static ssize_t show_class(struct device *dev,
220 struct device_attribute *attr, char *buf)
a9de9248 221{
aa2b86d7 222 struct hci_dev *hdev = to_hci_dev(dev);
8fc9ced3
GP
223 return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
224 hdev->dev_class[1], hdev->dev_class[0]);
a9de9248
MH
225}
226
b80f021f
GP
227static ssize_t show_address(struct device *dev,
228 struct device_attribute *attr, char *buf)
1da177e4 229{
aa2b86d7 230 struct hci_dev *hdev = to_hci_dev(dev);
d6b2eb2f 231 return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
1da177e4
LT
232}
233
b80f021f
GP
234static ssize_t show_features(struct device *dev,
235 struct device_attribute *attr, char *buf)
a9de9248 236{
aa2b86d7 237 struct hci_dev *hdev = to_hci_dev(dev);
a9de9248
MH
238
239 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
b80f021f
GP
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]);
a9de9248
MH
244}
245
b80f021f
GP
246static ssize_t show_manufacturer(struct device *dev,
247 struct device_attribute *attr, char *buf)
1143e5a6 248{
aa2b86d7 249 struct hci_dev *hdev = to_hci_dev(dev);
1143e5a6
MH
250 return sprintf(buf, "%d\n", hdev->manufacturer);
251}
252
b80f021f
GP
253static ssize_t show_hci_version(struct device *dev,
254 struct device_attribute *attr, char *buf)
1143e5a6 255{
aa2b86d7 256 struct hci_dev *hdev = to_hci_dev(dev);
1143e5a6
MH
257 return sprintf(buf, "%d\n", hdev->hci_ver);
258}
259
b80f021f
GP
260static ssize_t show_hci_revision(struct device *dev,
261 struct device_attribute *attr, char *buf)
1143e5a6 262{
aa2b86d7 263 struct hci_dev *hdev = to_hci_dev(dev);
1143e5a6
MH
264 return sprintf(buf, "%d\n", hdev->hci_rev);
265}
266
b80f021f
GP
267static ssize_t show_idle_timeout(struct device *dev,
268 struct device_attribute *attr, char *buf)
04837f64 269{
aa2b86d7 270 struct hci_dev *hdev = to_hci_dev(dev);
04837f64
MH
271 return sprintf(buf, "%d\n", hdev->idle_timeout);
272}
273
b80f021f
GP
274static ssize_t store_idle_timeout(struct device *dev,
275 struct device_attribute *attr,
276 const char *buf, size_t count)
04837f64 277{
aa2b86d7 278 struct hci_dev *hdev = to_hci_dev(dev);
db940cb0
AD
279 unsigned int val;
280 int rv;
04837f64 281
db940cb0
AD
282 rv = kstrtouint(buf, 0, &val);
283 if (rv < 0)
284 return rv;
04837f64
MH
285
286 if (val != 0 && (val < 500 || val > 3600000))
287 return -EINVAL;
288
289 hdev->idle_timeout = val;
290
291 return count;
292}
293
b80f021f
GP
294static ssize_t show_sniff_max_interval(struct device *dev,
295 struct device_attribute *attr, char *buf)
04837f64 296{
aa2b86d7 297 struct hci_dev *hdev = to_hci_dev(dev);
04837f64
MH
298 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
299}
300
b80f021f
GP
301static ssize_t store_sniff_max_interval(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
04837f64 304{
aa2b86d7 305 struct hci_dev *hdev = to_hci_dev(dev);
db940cb0
AD
306 u16 val;
307 int rv;
04837f64 308
db940cb0
AD
309 rv = kstrtou16(buf, 0, &val);
310 if (rv < 0)
311 return rv;
04837f64 312
db940cb0 313 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
04837f64
MH
314 return -EINVAL;
315
316 hdev->sniff_max_interval = val;
317
318 return count;
319}
320
b80f021f
GP
321static ssize_t show_sniff_min_interval(struct device *dev,
322 struct device_attribute *attr, char *buf)
04837f64 323{
aa2b86d7 324 struct hci_dev *hdev = to_hci_dev(dev);
04837f64
MH
325 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
326}
327
b80f021f
GP
328static ssize_t store_sniff_min_interval(struct device *dev,
329 struct device_attribute *attr,
330 const char *buf, size_t count)
04837f64 331{
aa2b86d7 332 struct hci_dev *hdev = to_hci_dev(dev);
db940cb0
AD
333 u16 val;
334 int rv;
04837f64 335
db940cb0
AD
336 rv = kstrtou16(buf, 0, &val);
337 if (rv < 0)
338 return rv;
04837f64 339
db940cb0 340 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
04837f64
MH
341 return -EINVAL;
342
343 hdev->sniff_min_interval = val;
344
345 return count;
346}
347
c13854ce 348static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
943da25d 349static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
a9de9248
MH
350static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
351static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
a91f2e39 352static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
a9de9248 353static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
1143e5a6
MH
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);
1da177e4 357
a91f2e39 358static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
b80f021f 359 show_idle_timeout, store_idle_timeout);
a91f2e39 360static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
b80f021f 361 show_sniff_max_interval, store_sniff_max_interval);
a91f2e39 362static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
b80f021f 363 show_sniff_min_interval, store_sniff_min_interval);
04837f64 364
90855d7b 365static struct attribute *bt_host_attrs[] = {
c13854ce 366 &dev_attr_bus.attr,
943da25d 367 &dev_attr_type.attr,
90855d7b
MH
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,
90855d7b
MH
375 &dev_attr_idle_timeout.attr,
376 &dev_attr_sniff_max_interval.attr,
377 &dev_attr_sniff_min_interval.attr,
1da177e4
LT
378 NULL
379};
380
90855d7b
MH
381static struct attribute_group bt_host_group = {
382 .attrs = bt_host_attrs,
b219e3ac
MH
383};
384
a4dbd674 385static const struct attribute_group *bt_host_groups[] = {
90855d7b
MH
386 &bt_host_group,
387 NULL
27d35284
MH
388};
389
90855d7b 390static void bt_host_release(struct device *dev)
a91f2e39 391{
2dd10688
DH
392 struct hci_dev *hdev = to_hci_dev(dev);
393 kfree(hdev);
46e06531 394 module_put(THIS_MODULE);
b219e3ac
MH
395}
396
90855d7b
MH
397static struct device_type bt_host = {
398 .name = "host",
399 .groups = bt_host_groups,
400 .release = bt_host_release,
401};
a91f2e39 402
d4612cb8 403static int inquiry_cache_show(struct seq_file *f, void *p)
ca325f69 404{
d4612cb8 405 struct hci_dev *hdev = f->private;
30883512 406 struct discovery_state *cache = &hdev->discovery;
ca325f69 407 struct inquiry_entry *e;
ca325f69 408
09fd0de5 409 hci_dev_lock(hdev);
ca325f69 410
561aafbc 411 list_for_each_entry(e, &cache->all, all) {
ca325f69 412 struct inquiry_data *data = &e->data;
d4612cb8 413 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
d6b2eb2f 414 batostr(&data->bdaddr),
d4612cb8
MH
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);
ca325f69
MH
420 }
421
09fd0de5 422 hci_dev_unlock(hdev);
ca325f69 423
d4612cb8
MH
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);
ca325f69
MH
430}
431
432static const struct file_operations inquiry_cache_fops = {
d4612cb8
MH
433 .open = inquiry_cache_open,
434 .read = seq_read,
435 .llseek = seq_lseek,
436 .release = single_release,
ca325f69
MH
437};
438
32c2ece5
JH
439static int blacklist_show(struct seq_file *f, void *p)
440{
441 struct hci_dev *hdev = f->private;
8035ded4 442 struct bdaddr_list *b;
32c2ece5 443
09fd0de5 444 hci_dev_lock(hdev);
32c2ece5 445
8035ded4 446 list_for_each_entry(b, &hdev->blacklist, list)
d6b2eb2f 447 seq_printf(f, "%s\n", batostr(&b->bdaddr));
32c2ece5 448
09fd0de5 449 hci_dev_unlock(hdev);
32c2ece5
JH
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};
930e1336
JH
465
466static void print_bt_uuid(struct seq_file *f, u8 *uuid)
467{
739f43e8
AE
468 __be32 data0, data4;
469 __be16 data1, data2, data3, data5;
930e1336
JH
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",
b80f021f
GP
479 ntohl(data0), ntohs(data1), ntohs(data2), ntohs(data3),
480 ntohl(data4), ntohs(data5));
930e1336
JH
481}
482
483static int uuids_show(struct seq_file *f, void *p)
484{
485 struct hci_dev *hdev = f->private;
8035ded4 486 struct bt_uuid *uuid;
930e1336 487
09fd0de5 488 hci_dev_lock(hdev);
930e1336 489
8035ded4 490 list_for_each_entry(uuid, &hdev->uuids, list)
930e1336 491 print_bt_uuid(f, uuid->uuid);
930e1336 492
09fd0de5 493 hci_dev_unlock(hdev);
930e1336
JH
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
9f61656a
JH
510static int auto_accept_delay_set(void *data, u64 val)
511{
512 struct hci_dev *hdev = data;
513
09fd0de5 514 hci_dev_lock(hdev);
9f61656a
JH
515
516 hdev->auto_accept_delay = val;
517
09fd0de5 518 hci_dev_unlock(hdev);
9f61656a
JH
519
520 return 0;
521}
522
523static int auto_accept_delay_get(void *data, u64 *val)
524{
525 struct hci_dev *hdev = data;
526
09fd0de5 527 hci_dev_lock(hdev);
9f61656a
JH
528
529 *val = hdev->auto_accept_delay;
530
09fd0de5 531 hci_dev_unlock(hdev);
9f61656a
JH
532
533 return 0;
534}
535
536DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
b80f021f 537 auto_accept_delay_set, "%llu\n");
9f61656a 538
0ac7e700
DH
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
46e06531 546 __module_get(THIS_MODULE);
0ac7e700
DH
547 device_initialize(dev);
548}
549
ce242970 550int hci_add_sysfs(struct hci_dev *hdev)
1da177e4 551{
a91f2e39 552 struct device *dev = &hdev->dev;
1da177e4
LT
553 int err;
554
c13854ce 555 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1da177e4 556
2e792995 557 dev_set_name(dev, "%s", hdev->name);
1da177e4 558
0ac7e700 559 err = device_add(dev);
1da177e4
LT
560 if (err < 0)
561 return err;
562
ca325f69
MH
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,
b80f021f 571 hdev, &inquiry_cache_fops);
ca325f69 572
32c2ece5 573 debugfs_create_file("blacklist", 0444, hdev->debugfs,
b80f021f 574 hdev, &blacklist_fops);
32c2ece5 575
930e1336
JH
576 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
577
9f61656a 578 debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
b80f021f 579 &auto_accept_delay_fops);
1da177e4
LT
580 return 0;
581}
582
ce242970 583void hci_del_sysfs(struct hci_dev *hdev)
1da177e4 584{
c13854ce 585 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1da177e4 586
ca325f69
MH
587 debugfs_remove_recursive(hdev->debugfs);
588
4d0eb004 589 device_del(&hdev->dev);
1da177e4
LT
590}
591
592int __init bt_sysfs_init(void)
593{
ca325f69
MH
594 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
595
a91f2e39 596 bt_class = class_create(THIS_MODULE, "bluetooth");
f48fd9c8 597 if (IS_ERR(bt_class))
90855d7b 598 return PTR_ERR(bt_class);
27d35284
MH
599
600 return 0;
1da177e4
LT
601}
602
860e13b5 603void bt_sysfs_cleanup(void)
1da177e4 604{
a91f2e39 605 class_destroy(bt_class);
ca325f69
MH
606
607 debugfs_remove_recursive(bt_debugfs);
1da177e4 608}
This page took 0.664758 seconds and 5 git commands to generate.