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