blob: 8775933ea837f4c2d7cf624b3861faf0d04e73e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Bluetooth HCI driver model support. */
2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/init.h>
Marcel Holtmannca325f62010-02-08 16:22:31 +01006#include <linux/debugfs.h>
Marcel Holtmannd4612cb2010-03-02 15:48:23 +00007#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#include <net/bluetooth/bluetooth.h>
10#include <net/bluetooth/hci_core.h>
11
Marcel Holtmannaef7d972010-03-21 05:27:45 +010012static struct class *bt_class;
Marcel Holtmann90855d72008-08-18 13:23:53 +020013
Gustavo F. Padovan602f9882011-02-17 19:22:19 -030014struct dentry *bt_debugfs;
Marcel Holtmannca325f62010-02-08 16:22:31 +010015EXPORT_SYMBOL_GPL(bt_debugfs);
16
Marcel Holtmann90855d72008-08-18 13:23:53 +020017static 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);
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -030040 return sprintf(buf, "%s\n", batostr(&conn->dst));
Marcel Holtmann90855d72008-08-18 13:23:53 +020041}
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
Gustavo F. Padovan602f9882011-02-17 19:22:19 -030054#define LINK_ATTR(_name, _mode, _show, _store) \
55struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
Marcel Holtmann90855d72008-08-18 13:23:53 +020056
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
David Brownella4dbd672009-06-24 10:06:31 -070072static const struct attribute_group *bt_link_groups[] = {
Marcel Holtmann90855d72008-08-18 13:23:53 +020073 &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{
Roger Quadrosf3784d82009-04-23 14:50:54 +030091 struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
Marcel Holtmann457ca7b2009-05-05 13:09:01 -070092 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann90855d72008-08-18 13:23:53 +020093
Marcel Holtmann457ca7b2009-05-05 13:09:01 -070094 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
95
Dave Youngf74c77c2009-10-18 20:24:41 +000096 dev_set_drvdata(&conn->dev, conn);
97
Marcel Holtmann90855d72008-08-18 13:23:53 +020098 if (device_add(&conn->dev) < 0) {
99 BT_ERR("Failed to register connection device");
100 return;
101 }
Marcel Holtmann384943e2009-05-08 18:20:43 -0700102
103 hci_dev_hold(hdev);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200104}
105
Marcel Holtmann90855d72008-08-18 13:23:53 +0200106/*
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{
Kay Sieversfb28ad32008-11-10 13:55:14 -0800113 return !strncmp(dev_name(dev), "rfcomm", 6);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200114}
115
116static void del_conn(struct work_struct *work)
117{
Roger Quadrosf3784d82009-04-23 14:50:54 +0300118 struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200119 struct hci_dev *hdev = conn->hdev;
120
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700121 if (!device_is_registered(&conn->dev))
122 return;
Roger Quadrosf3784d82009-04-23 14:50:54 +0300123
Marcel Holtmann90855d72008-08-18 13:23:53 +0200124 while (1) {
125 struct device *dev;
126
127 dev = device_find_child(&conn->dev, NULL, __match_tty);
128 if (!dev)
129 break;
Cornelia Huckffa6a702009-03-04 12:44:00 +0100130 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200131 put_device(dev);
132 }
133
134 device_del(&conn->dev);
135 put_device(&conn->dev);
Marcel Holtmann384943e2009-05-08 18:20:43 -0700136
Marcel Holtmann90855d72008-08-18 13:23:53 +0200137 hci_dev_put(hdev);
138}
139
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700140void 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
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700150 device_initialize(&conn->dev);
151
152 INIT_WORK(&conn->work_add, add_conn);
153 INIT_WORK(&conn->work_del, del_conn);
154}
155
156void hci_conn_add_sysfs(struct hci_conn *conn)
157{
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700158 BT_DBG("conn %p", conn);
159
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100160 queue_work(conn->hdev->workqueue, &conn->work_add);
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700161}
162
Marcel Holtmann90855d72008-08-18 13:23:53 +0200163void hci_conn_del_sysfs(struct hci_conn *conn)
164{
165 BT_DBG("conn %p", conn);
166
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100167 queue_work(conn->hdev->workqueue, &conn->work_del);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200168}
169
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100170static inline char *host_bustostr(int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100172 switch (bus) {
Marcel Holtmann0ac53932006-07-08 13:57:15 +0200173 case HCI_VIRTUAL:
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200174 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";
Marcel Holtmann0ac53932006-07-08 13:57:15 +0200185 case HCI_SDIO:
186 return "SDIO";
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200187 default:
188 return "UNKNOWN";
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Marcel Holtmann943da252010-02-13 02:28:41 +0100192static inline char *host_typetostr(int type)
193{
194 switch (type) {
195 case HCI_BREDR:
196 return "BR/EDR";
David Vrabel8f1e1742010-08-09 17:38:10 -0400197 case HCI_AMP:
198 return "AMP";
Marcel Holtmann943da252010-02-13 02:28:41 +0100199 default:
200 return "UNKNOWN";
201 }
202}
203
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100204static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200206 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100207 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Marcel Holtmann943da252010-02-13 02:28:41 +0100210static 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
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200216static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
217{
218 struct hci_dev *hdev = dev_get_drvdata(dev);
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200219 char name[HCI_MAX_NAME_LENGTH + 1];
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200220 int i;
221
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200222 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200223 name[i] = hdev->dev_name[i];
224
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200225 name[HCI_MAX_NAME_LENGTH] = '\0';
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200226 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
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200236static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200238 struct hci_dev *hdev = dev_get_drvdata(dev);
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300239 return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200242static 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
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200253static 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
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200271static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200272{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200273 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200274 return sprintf(buf, "%d\n", hdev->idle_timeout);
275}
276
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200277static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200278{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200279 struct hci_dev *hdev = dev_get_drvdata(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300280 unsigned int val;
281 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200282
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300283 rv = kstrtouint(buf, 0, &val);
284 if (rv < 0)
285 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200286
287 if (val != 0 && (val < 500 || val > 3600000))
288 return -EINVAL;
289
290 hdev->idle_timeout = val;
291
292 return count;
293}
294
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200295static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200296{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200297 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200298 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
299}
300
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200301static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200302{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200303 struct hci_dev *hdev = dev_get_drvdata(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300304 u16 val;
305 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200306
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300307 rv = kstrtou16(buf, 0, &val);
308 if (rv < 0)
309 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200310
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300311 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200312 return -EINVAL;
313
314 hdev->sniff_max_interval = val;
315
316 return count;
317}
318
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200319static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200320{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200321 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200322 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
323}
324
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200325static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200326{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200327 struct hci_dev *hdev = dev_get_drvdata(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300328 u16 val;
329 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200330
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300331 rv = kstrtou16(buf, 0, &val);
332 if (rv < 0)
333 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200334
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300335 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200336 return -EINVAL;
337
338 hdev->sniff_min_interval = val;
339
340 return count;
341}
342
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100343static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
Marcel Holtmann943da252010-02-13 02:28:41 +0100344static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200345static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
346static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200347static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200348static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200349static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200353static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200354 show_idle_timeout, store_idle_timeout);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200355static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200356 show_sniff_max_interval, store_sniff_max_interval);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200357static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200358 show_sniff_min_interval, store_sniff_min_interval);
359
Marcel Holtmann90855d72008-08-18 13:23:53 +0200360static struct attribute *bt_host_attrs[] = {
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100361 &dev_attr_bus.attr,
Marcel Holtmann943da252010-02-13 02:28:41 +0100362 &dev_attr_type.attr,
Marcel Holtmann90855d72008-08-18 13:23:53 +0200363 &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,
Marcel Holtmann90855d72008-08-18 13:23:53 +0200370 &dev_attr_idle_timeout.attr,
371 &dev_attr_sniff_max_interval.attr,
372 &dev_attr_sniff_min_interval.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 NULL
374};
375
Marcel Holtmann90855d72008-08-18 13:23:53 +0200376static struct attribute_group bt_host_group = {
377 .attrs = bt_host_attrs,
378};
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200379
David Brownella4dbd672009-06-24 10:06:31 -0700380static const struct attribute_group *bt_host_groups[] = {
Marcel Holtmann90855d72008-08-18 13:23:53 +0200381 &bt_host_group,
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200382 NULL
383};
384
Marcel Holtmann90855d72008-08-18 13:23:53 +0200385static void bt_host_release(struct device *dev)
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200386{
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200387 void *data = dev_get_drvdata(dev);
388 kfree(data);
389}
390
Marcel Holtmann90855d72008-08-18 13:23:53 +0200391static struct device_type bt_host = {
392 .name = "host",
393 .groups = bt_host_groups,
394 .release = bt_host_release,
395};
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200396
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000397static int inquiry_cache_show(struct seq_file *f, void *p)
Marcel Holtmannca325f62010-02-08 16:22:31 +0100398{
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000399 struct hci_dev *hdev = f->private;
Marcel Holtmannca325f62010-02-08 16:22:31 +0100400 struct inquiry_cache *cache = &hdev->inq_cache;
401 struct inquiry_entry *e;
Marcel Holtmannca325f62010-02-08 16:22:31 +0100402
403 hci_dev_lock_bh(hdev);
404
405 for (e = cache->list; e; e = e->next) {
406 struct inquiry_data *data = &e->data;
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000407 seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300408 batostr(&data->bdaddr),
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000409 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);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100414 }
415
416 hci_dev_unlock_bh(hdev);
417
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000418 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);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100424}
425
426static const struct file_operations inquiry_cache_fops = {
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000427 .open = inquiry_cache_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
Marcel Holtmannca325f62010-02-08 16:22:31 +0100431};
432
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200433static int blacklist_show(struct seq_file *f, void *p)
434{
435 struct hci_dev *hdev = f->private;
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200436 struct list_head *l;
437
438 hci_dev_lock_bh(hdev);
439
David Millerea4bd8b2010-07-30 21:54:49 -0700440 list_for_each(l, &hdev->blacklist) {
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200441 struct bdaddr_list *b;
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200442
443 b = list_entry(l, struct bdaddr_list, list);
444
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300445 seq_printf(f, "%s\n", batostr(&b->bdaddr));
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200446 }
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};
Johan Hedberg930e1332011-01-04 11:39:44 +0200464
465static void print_bt_uuid(struct seq_file *f, u8 *uuid)
466{
467 u32 data0, data4;
468 u16 data1, data2, data3, data5;
469
470 memcpy(&data0, &uuid[0], 4);
471 memcpy(&data1, &uuid[4], 2);
472 memcpy(&data2, &uuid[6], 2);
473 memcpy(&data3, &uuid[8], 2);
474 memcpy(&data4, &uuid[10], 4);
475 memcpy(&data5, &uuid[14], 2);
476
477 seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x\n",
478 ntohl(data0), ntohs(data1), ntohs(data2),
479 ntohs(data3), ntohl(data4), ntohs(data5));
480}
481
482static int uuids_show(struct seq_file *f, void *p)
483{
484 struct hci_dev *hdev = f->private;
485 struct list_head *l;
486
487 hci_dev_lock_bh(hdev);
488
489 list_for_each(l, &hdev->uuids) {
490 struct bt_uuid *uuid;
491
492 uuid = list_entry(l, struct bt_uuid, list);
493
494 print_bt_uuid(f, uuid->uuid);
495 }
496
497 hci_dev_unlock_bh(hdev);
498
499 return 0;
500}
501
502static int uuids_open(struct inode *inode, struct file *file)
503{
504 return single_open(file, uuids_show, inode->i_private);
505}
506
507static const struct file_operations uuids_fops = {
508 .open = uuids_open,
509 .read = seq_read,
510 .llseek = seq_lseek,
511 .release = single_release,
512};
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514int hci_register_sysfs(struct hci_dev *hdev)
515{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200516 struct device *dev = &hdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int err;
518
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100519 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Marcel Holtmann90855d72008-08-18 13:23:53 +0200521 dev->type = &bt_host;
522 dev->class = bt_class;
Marcel Holtmanne9c4bec2006-10-15 17:30:50 +0200523 dev->parent = hdev->parent;
Marcel Holtmann27d35282006-07-03 10:02:37 +0200524
Marcel Holtmann2e792992008-11-30 12:17:29 +0100525 dev_set_name(dev, "%s", hdev->name);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200526
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200527 dev_set_drvdata(dev, hdev);
528
529 err = device_register(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (err < 0)
531 return err;
532
Marcel Holtmannca325f62010-02-08 16:22:31 +0100533 if (!bt_debugfs)
534 return 0;
535
536 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
537 if (!hdev->debugfs)
538 return 0;
539
540 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
541 hdev, &inquiry_cache_fops);
542
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200543 debugfs_create_file("blacklist", 0444, hdev->debugfs,
544 hdev, &blacklist_fops);
545
Johan Hedberg930e1332011-01-04 11:39:44 +0200546 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
549}
550
551void hci_unregister_sysfs(struct hci_dev *hdev)
552{
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100553 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Marcel Holtmannca325f62010-02-08 16:22:31 +0100555 debugfs_remove_recursive(hdev->debugfs);
556
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200557 device_del(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560int __init bt_sysfs_init(void)
561{
Marcel Holtmannca325f62010-02-08 16:22:31 +0100562 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
563
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200564 bt_class = class_create(THIS_MODULE, "bluetooth");
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100565 if (IS_ERR(bt_class))
Marcel Holtmann90855d72008-08-18 13:23:53 +0200566 return PTR_ERR(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200567
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Arnaud Patard860e13b2006-09-28 15:29:37 -0700571void bt_sysfs_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200573 class_destroy(bt_class);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100574
575 debugfs_remove_recursive(bt_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}