upload tizen1.0 source
[kernel/linux-2.6.36.git] / net / bluetooth / hci_sysfs.c
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 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
31 static 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
37 static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
38 {
39         struct hci_conn *conn = dev_get_drvdata(dev);
40         return sprintf(buf, "%s\n", batostr(&conn->dst));
41 }
42
43 static 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) \
55 struct device_attribute link_attr_##_name = __ATTR(_name,_mode,_show,_store)
56
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);
60
61 static struct attribute *bt_link_attrs[] = {
62         &link_attr_type.attr,
63         &link_attr_address.attr,
64         &link_attr_features.attr,
65         NULL
66 };
67
68 static struct attribute_group bt_link_group = {
69         .attrs = bt_link_attrs,
70 };
71
72 static const struct attribute_group *bt_link_groups[] = {
73         &bt_link_group,
74         NULL
75 };
76
77 static void bt_link_release(struct device *dev)
78 {
79         void *data = dev_get_drvdata(dev);
80         kfree(data);
81 }
82
83 static struct device_type bt_link = {
84         .name    = "link",
85         .groups  = bt_link_groups,
86         .release = bt_link_release,
87 };
88
89 static void add_conn(struct work_struct *work)
90 {
91         struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
92         struct hci_dev *hdev = conn->hdev;
93
94         dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
95
96         dev_set_drvdata(&conn->dev, conn);
97
98         if (device_add(&conn->dev) < 0) {
99                 BT_ERR("Failed to register connection device");
100                 return;
101         }
102
103         hci_dev_hold(hdev);
104 }
105
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  */
111 static int __match_tty(struct device *dev, void *data)
112 {
113         return !strncmp(dev_name(dev), "rfcomm", 6);
114 }
115
116 static void del_conn(struct work_struct *work)
117 {
118         struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
119         struct hci_dev *hdev = conn->hdev;
120
121         if (!device_is_registered(&conn->dev))
122                 return;
123
124         while (1) {
125                 struct device *dev;
126
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);
132         }
133
134         device_del(&conn->dev);
135         put_device(&conn->dev);
136
137         hci_dev_put(hdev);
138 }
139
140 void hci_conn_init_sysfs(struct hci_conn *conn)
141 {
142         struct hci_dev *hdev = conn->hdev;
143
144         BT_DBG("conn %p", conn);
145
146         conn->dev.type = &bt_link;
147         conn->dev.class = bt_class;
148         conn->dev.parent = &hdev->dev;
149
150         device_initialize(&conn->dev);
151
152         INIT_WORK(&conn->work_add, add_conn);
153         INIT_WORK(&conn->work_del, del_conn);
154 }
155
156 void hci_conn_add_sysfs(struct hci_conn *conn)
157 {
158         BT_DBG("conn %p", conn);
159
160         queue_work(conn->hdev->workqueue, &conn->work_add);
161 }
162
163 void hci_conn_del_sysfs(struct hci_conn *conn)
164 {
165         BT_DBG("conn %p", conn);
166
167 #ifdef FEATURE_DELAYED_HCI_UNREGISTER
168         if(conn->hdev->workqueue)
169                 queue_work(conn->hdev->workqueue, &conn->work_del);
170 #else
171         queue_work(conn->hdev->workqueue, &conn->work_del);
172 #endif
173 }
174
175 static inline char *host_bustostr(int bus)
176 {
177         switch (bus) {
178         case HCI_VIRTUAL:
179                 return "VIRTUAL";
180         case HCI_USB:
181                 return "USB";
182         case HCI_PCCARD:
183                 return "PCCARD";
184         case HCI_UART:
185                 return "UART";
186         case HCI_RS232:
187                 return "RS232";
188         case HCI_PCI:
189                 return "PCI";
190         case HCI_SDIO:
191                 return "SDIO";
192         default:
193                 return "UNKNOWN";
194         }
195 }
196
197 static inline char *host_typetostr(int type)
198 {
199         switch (type) {
200         case HCI_BREDR:
201                 return "BR/EDR";
202         case HCI_AMP:
203                 return "AMP";
204         default:
205                 return "UNKNOWN";
206         }
207 }
208
209 static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
210 {
211         struct hci_dev *hdev = dev_get_drvdata(dev);
212         return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
213 }
214
215 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
216 {
217         struct hci_dev *hdev = dev_get_drvdata(dev);
218         return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
219 }
220
221 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
222 {
223         struct hci_dev *hdev = dev_get_drvdata(dev);
224         char name[249];
225         int i;
226
227         for (i = 0; i < 248; i++)
228                 name[i] = hdev->dev_name[i];
229
230         name[248] = '\0';
231         return sprintf(buf, "%s\n", name);
232 }
233
234 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
235 {
236         struct hci_dev *hdev = dev_get_drvdata(dev);
237         return sprintf(buf, "0x%.2x%.2x%.2x\n",
238                         hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
239 }
240
241 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
242 {
243         struct hci_dev *hdev = dev_get_drvdata(dev);
244         return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
245 }
246
247 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
248 {
249         struct hci_dev *hdev = dev_get_drvdata(dev);
250
251         return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
252                                 hdev->features[0], hdev->features[1],
253                                 hdev->features[2], hdev->features[3],
254                                 hdev->features[4], hdev->features[5],
255                                 hdev->features[6], hdev->features[7]);
256 }
257
258 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
259 {
260         struct hci_dev *hdev = dev_get_drvdata(dev);
261         return sprintf(buf, "%d\n", hdev->manufacturer);
262 }
263
264 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
265 {
266         struct hci_dev *hdev = dev_get_drvdata(dev);
267         return sprintf(buf, "%d\n", hdev->hci_ver);
268 }
269
270 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
271 {
272         struct hci_dev *hdev = dev_get_drvdata(dev);
273         return sprintf(buf, "%d\n", hdev->hci_rev);
274 }
275
276 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
277 {
278         struct hci_dev *hdev = dev_get_drvdata(dev);
279         return sprintf(buf, "%d\n", hdev->idle_timeout);
280 }
281
282 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
283 {
284         struct hci_dev *hdev = dev_get_drvdata(dev);
285         unsigned long val;
286
287         if (strict_strtoul(buf, 0, &val) < 0)
288                 return -EINVAL;
289
290         if (val != 0 && (val < 500 || val > 3600000))
291                 return -EINVAL;
292
293         hdev->idle_timeout = val;
294
295         return count;
296 }
297
298 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
299 {
300         struct hci_dev *hdev = dev_get_drvdata(dev);
301         return sprintf(buf, "%d\n", hdev->sniff_max_interval);
302 }
303
304 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
305 {
306         struct hci_dev *hdev = dev_get_drvdata(dev);
307         unsigned long val;
308
309         if (strict_strtoul(buf, 0, &val) < 0)
310                 return -EINVAL;
311
312         if (val < 0x0002 || val > 0xFFFE || val % 2)
313                 return -EINVAL;
314
315         if (val < hdev->sniff_min_interval)
316                 return -EINVAL;
317
318         hdev->sniff_max_interval = val;
319
320         return count;
321 }
322
323 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
324 {
325         struct hci_dev *hdev = dev_get_drvdata(dev);
326         return sprintf(buf, "%d\n", hdev->sniff_min_interval);
327 }
328
329 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
330 {
331         struct hci_dev *hdev = dev_get_drvdata(dev);
332         unsigned long val;
333
334         if (strict_strtoul(buf, 0, &val) < 0)
335                 return -EINVAL;
336
337         if (val < 0x0002 || val > 0xFFFE || val % 2)
338                 return -EINVAL;
339
340         if (val > hdev->sniff_max_interval)
341                 return -EINVAL;
342
343         hdev->sniff_min_interval = val;
344
345         return count;
346 }
347
348 static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
349 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
350 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
351 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
352 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
353 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
354 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
355 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
356 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
357
358 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
359                                 show_idle_timeout, store_idle_timeout);
360 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
361                                 show_sniff_max_interval, store_sniff_max_interval);
362 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
363                                 show_sniff_min_interval, store_sniff_min_interval);
364
365 static 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
381 static struct attribute_group bt_host_group = {
382         .attrs = bt_host_attrs,
383 };
384
385 static const struct attribute_group *bt_host_groups[] = {
386         &bt_host_group,
387         NULL
388 };
389
390 static void bt_host_release(struct device *dev)
391 {
392         void *data = dev_get_drvdata(dev);
393         kfree(data);
394 }
395
396 static struct device_type bt_host = {
397         .name    = "host",
398         .groups  = bt_host_groups,
399         .release = bt_host_release,
400 };
401
402 static int inquiry_cache_show(struct seq_file *f, void *p)
403 {
404         struct hci_dev *hdev = f->private;
405         struct inquiry_cache *cache = &hdev->inq_cache;
406         struct inquiry_entry *e;
407
408         hci_dev_lock_bh(hdev);
409
410         for (e = cache->list; e; e = e->next) {
411                 struct inquiry_data *data = &e->data;
412                 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
413                            batostr(&data->bdaddr),
414                            data->pscan_rep_mode, data->pscan_period_mode,
415                            data->pscan_mode, data->dev_class[2],
416                            data->dev_class[1], data->dev_class[0],
417                            __le16_to_cpu(data->clock_offset),
418                            data->rssi, data->ssp_mode, e->timestamp);
419         }
420
421         hci_dev_unlock_bh(hdev);
422
423         return 0;
424 }
425
426 static int inquiry_cache_open(struct inode *inode, struct file *file)
427 {
428         return single_open(file, inquiry_cache_show, inode->i_private);
429 }
430
431 static const struct file_operations inquiry_cache_fops = {
432         .open           = inquiry_cache_open,
433         .read           = seq_read,
434         .llseek         = seq_lseek,
435         .release        = single_release,
436 };
437
438 static int blacklist_show(struct seq_file *f, void *p)
439 {
440         struct hci_dev *hdev = f->private;
441         struct list_head *l;
442
443         hci_dev_lock_bh(hdev);
444
445         list_for_each(l, &hdev->blacklist) {
446                 struct bdaddr_list *b;
447
448                 b = list_entry(l, struct bdaddr_list, list);
449
450                 seq_printf(f, "%s\n", batostr(&b->bdaddr));
451         }
452
453         hci_dev_unlock_bh(hdev);
454
455         return 0;
456 }
457
458 static int blacklist_open(struct inode *inode, struct file *file)
459 {
460         return single_open(file, blacklist_show, inode->i_private);
461 }
462
463 static const struct file_operations blacklist_fops = {
464         .open           = blacklist_open,
465         .read           = seq_read,
466         .llseek         = seq_lseek,
467         .release        = single_release,
468 };
469 int hci_register_sysfs(struct hci_dev *hdev)
470 {
471         struct device *dev = &hdev->dev;
472         int err;
473
474         BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
475
476         dev->type = &bt_host;
477         dev->class = bt_class;
478         dev->parent = hdev->parent;
479
480         dev_set_name(dev, "%s", hdev->name);
481
482         dev_set_drvdata(dev, hdev);
483
484         err = device_register(dev);
485         if (err < 0)
486                 return err;
487
488         if (!bt_debugfs)
489                 return 0;
490
491         hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
492         if (!hdev->debugfs)
493                 return 0;
494
495         debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
496                                                 hdev, &inquiry_cache_fops);
497
498         debugfs_create_file("blacklist", 0444, hdev->debugfs,
499                                                 hdev, &blacklist_fops);
500
501         return 0;
502 }
503
504 void hci_unregister_sysfs(struct hci_dev *hdev)
505 {
506         BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
507
508         debugfs_remove_recursive(hdev->debugfs);
509
510         device_del(&hdev->dev);
511 }
512
513 int __init bt_sysfs_init(void)
514 {
515         bt_debugfs = debugfs_create_dir("bluetooth", NULL);
516
517         bt_class = class_create(THIS_MODULE, "bluetooth");
518         if (IS_ERR(bt_class))
519                 return PTR_ERR(bt_class);
520
521         return 0;
522 }
523
524 void bt_sysfs_cleanup(void)
525 {
526         class_destroy(bt_class);
527
528         debugfs_remove_recursive(bt_debugfs);
529 }