blob: 847a8445c53c3d931aedc66fb07a767e6195de9b [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>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -04008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <net/bluetooth/bluetooth.h>
11#include <net/bluetooth/hci_core.h>
12
Marcel Holtmannaef7d972010-03-21 05:27:45 +010013static struct class *bt_class;
Marcel Holtmann90855d72008-08-18 13:23:53 +020014
Gustavo F. Padovan602f9882011-02-17 19:22:19 -030015struct dentry *bt_debugfs;
Marcel Holtmannca325f62010-02-08 16:22:31 +010016EXPORT_SYMBOL_GPL(bt_debugfs);
17
Marcel Holtmann90855d72008-08-18 13:23:53 +020018static 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";
Peter Hurley21061df2011-08-24 10:04:56 -040027 case LE_LINK:
28 return "LE";
Marcel Holtmann90855d72008-08-18 13:23:53 +020029 default:
30 return "UNKNOWN";
31 }
32}
33
Gustavo Padovanb80f0212012-05-17 00:36:23 -030034static ssize_t show_link_type(struct device *dev,
35 struct device_attribute *attr, char *buf)
Marcel Holtmann90855d72008-08-18 13:23:53 +020036{
David Herrmann3dc07322012-02-09 21:58:33 +010037 struct hci_conn *conn = to_hci_conn(dev);
Marcel Holtmann90855d72008-08-18 13:23:53 +020038 return sprintf(buf, "%s\n", link_typetostr(conn->type));
39}
40
Gustavo Padovanb80f0212012-05-17 00:36:23 -030041static ssize_t show_link_address(struct device *dev,
42 struct device_attribute *attr, char *buf)
Marcel Holtmann90855d72008-08-18 13:23:53 +020043{
David Herrmann3dc07322012-02-09 21:58:33 +010044 struct hci_conn *conn = to_hci_conn(dev);
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -030045 return sprintf(buf, "%s\n", batostr(&conn->dst));
Marcel Holtmann90855d72008-08-18 13:23:53 +020046}
47
Gustavo Padovanb80f0212012-05-17 00:36:23 -030048static ssize_t show_link_features(struct device *dev,
49 struct device_attribute *attr, char *buf)
Marcel Holtmann90855d72008-08-18 13:23:53 +020050{
David Herrmann3dc07322012-02-09 21:58:33 +010051 struct hci_conn *conn = to_hci_conn(dev);
Marcel Holtmann90855d72008-08-18 13:23:53 +020052
53 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
Gustavo Padovanb80f0212012-05-17 00:36:23 -030054 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]);
Marcel Holtmann90855d72008-08-18 13:23:53 +020058}
59
Gustavo F. Padovan602f9882011-02-17 19:22:19 -030060#define LINK_ATTR(_name, _mode, _show, _store) \
61struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
Marcel Holtmann90855d72008-08-18 13:23:53 +020062
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
David Brownella4dbd672009-06-24 10:06:31 -070078static const struct attribute_group *bt_link_groups[] = {
Marcel Holtmann90855d72008-08-18 13:23:53 +020079 &bt_link_group,
80 NULL
81};
82
83static void bt_link_release(struct device *dev)
84{
David Herrmann2dd10682012-02-09 21:58:34 +010085 struct hci_conn *conn = to_hci_conn(dev);
86 kfree(conn);
Marcel Holtmann90855d72008-08-18 13:23:53 +020087}
88
89static struct device_type bt_link = {
90 .name = "link",
91 .groups = bt_link_groups,
92 .release = bt_link_release,
93};
94
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -020095/*
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)
Marcel Holtmann90855d72008-08-18 13:23:53 +0200101{
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -0200102 return !strncmp(dev_name(dev), "rfcomm", 6);
103}
104
105void hci_conn_init_sysfs(struct hci_conn *conn)
106{
Marcel Holtmann457ca7b2009-05-05 13:09:01 -0700107 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann90855d72008-08-18 13:23:53 +0200108
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -0200109 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)
119{
120 struct hci_dev *hdev = conn->hdev;
121
122 BT_DBG("conn %p", conn);
123
Marcel Holtmann457ca7b2009-05-05 13:09:01 -0700124 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
125
Marcel Holtmann90855d72008-08-18 13:23:53 +0200126 if (device_add(&conn->dev) < 0) {
127 BT_ERR("Failed to register connection device");
128 return;
129 }
Marcel Holtmann384943e2009-05-08 18:20:43 -0700130
131 hci_dev_hold(hdev);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200132}
133
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -0200134void hci_conn_del_sysfs(struct hci_conn *conn)
Marcel Holtmann90855d72008-08-18 13:23:53 +0200135{
Marcel Holtmann90855d72008-08-18 13:23:53 +0200136 struct hci_dev *hdev = conn->hdev;
137
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700138 if (!device_is_registered(&conn->dev))
139 return;
Roger Quadrosf3784d82009-04-23 14:50:54 +0300140
Marcel Holtmann90855d72008-08-18 13:23:53 +0200141 while (1) {
142 struct device *dev;
143
144 dev = device_find_child(&conn->dev, NULL, __match_tty);
145 if (!dev)
146 break;
Cornelia Huckffa6a702009-03-04 12:44:00 +0100147 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
Marcel Holtmann90855d72008-08-18 13:23:53 +0200148 put_device(dev);
149 }
150
151 device_del(&conn->dev);
152 put_device(&conn->dev);
Marcel Holtmann384943e2009-05-08 18:20:43 -0700153
Marcel Holtmann90855d72008-08-18 13:23:53 +0200154 hci_dev_put(hdev);
155}
156
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100157static inline char *host_bustostr(int bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100159 switch (bus) {
Marcel Holtmann0ac53932006-07-08 13:57:15 +0200160 case HCI_VIRTUAL:
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200161 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";
Marcel Holtmann0ac53932006-07-08 13:57:15 +0200172 case HCI_SDIO:
173 return "SDIO";
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200174 default:
175 return "UNKNOWN";
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Marcel Holtmann943da252010-02-13 02:28:41 +0100179static inline char *host_typetostr(int type)
180{
181 switch (type) {
182 case HCI_BREDR:
183 return "BR/EDR";
David Vrabel8f1e1742010-08-09 17:38:10 -0400184 case HCI_AMP:
185 return "AMP";
Marcel Holtmann943da252010-02-13 02:28:41 +0100186 default:
187 return "UNKNOWN";
188 }
189}
190
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300191static ssize_t show_bus(struct device *dev,
192 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100194 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100195 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300198static ssize_t show_type(struct device *dev,
199 struct device_attribute *attr, char *buf)
Marcel Holtmann943da252010-02-13 02:28:41 +0100200{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100201 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann943da252010-02-13 02:28:41 +0100202 return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
203}
204
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300205static ssize_t show_name(struct device *dev,
206 struct device_attribute *attr, char *buf)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200207{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100208 struct hci_dev *hdev = to_hci_dev(dev);
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200209 char name[HCI_MAX_NAME_LENGTH + 1];
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200210 int i;
211
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200212 for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200213 name[i] = hdev->dev_name[i];
214
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200215 name[HCI_MAX_NAME_LENGTH] = '\0';
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200216 return sprintf(buf, "%s\n", name);
217}
218
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300219static ssize_t show_class(struct device *dev,
220 struct device_attribute *attr, char *buf)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200221{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100222 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200223 return sprintf(buf, "0x%.2x%.2x%.2x\n",
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300224 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200225}
226
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300227static ssize_t show_address(struct device *dev,
228 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100230 struct hci_dev *hdev = to_hci_dev(dev);
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300231 return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300234static ssize_t show_features(struct device *dev,
235 struct device_attribute *attr, char *buf)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200236{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100237 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200238
239 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300240 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]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200244}
245
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300246static ssize_t show_manufacturer(struct device *dev,
247 struct device_attribute *attr, char *buf)
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200248{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100249 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200250 return sprintf(buf, "%d\n", hdev->manufacturer);
251}
252
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300253static ssize_t show_hci_version(struct device *dev,
254 struct device_attribute *attr, char *buf)
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200255{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100256 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200257 return sprintf(buf, "%d\n", hdev->hci_ver);
258}
259
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300260static ssize_t show_hci_revision(struct device *dev,
261 struct device_attribute *attr, char *buf)
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200262{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100263 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200264 return sprintf(buf, "%d\n", hdev->hci_rev);
265}
266
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300267static ssize_t show_idle_timeout(struct device *dev,
268 struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200269{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100270 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200271 return sprintf(buf, "%d\n", hdev->idle_timeout);
272}
273
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300274static ssize_t store_idle_timeout(struct device *dev,
275 struct device_attribute *attr,
276 const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200277{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100278 struct hci_dev *hdev = to_hci_dev(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300279 unsigned int val;
280 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200281
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300282 rv = kstrtouint(buf, 0, &val);
283 if (rv < 0)
284 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200285
286 if (val != 0 && (val < 500 || val > 3600000))
287 return -EINVAL;
288
289 hdev->idle_timeout = val;
290
291 return count;
292}
293
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300294static ssize_t show_sniff_max_interval(struct device *dev,
295 struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200296{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100297 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200298 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
299}
300
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300301static ssize_t store_sniff_max_interval(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200304{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100305 struct hci_dev *hdev = to_hci_dev(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300306 u16 val;
307 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200308
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300309 rv = kstrtou16(buf, 0, &val);
310 if (rv < 0)
311 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200312
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300313 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200314 return -EINVAL;
315
316 hdev->sniff_max_interval = val;
317
318 return count;
319}
320
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300321static ssize_t show_sniff_min_interval(struct device *dev,
322 struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200323{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100324 struct hci_dev *hdev = to_hci_dev(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200325 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
326}
327
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300328static ssize_t store_sniff_min_interval(struct device *dev,
329 struct device_attribute *attr,
330 const char *buf, size_t count)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200331{
David Herrmannaa2b86d2012-02-09 21:58:30 +0100332 struct hci_dev *hdev = to_hci_dev(dev);
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300333 u16 val;
334 int rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200335
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300336 rv = kstrtou16(buf, 0, &val);
337 if (rv < 0)
338 return rv;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200339
Alexey Dobriyandb940cb2011-04-02 14:19:41 +0300340 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200341 return -EINVAL;
342
343 hdev->sniff_min_interval = val;
344
345 return count;
346}
347
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100348static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
Marcel Holtmann943da252010-02-13 02:28:41 +0100349static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200350static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
351static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200352static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200353static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200354static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200358static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300359 show_idle_timeout, store_idle_timeout);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200360static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300361 show_sniff_max_interval, store_sniff_max_interval);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200362static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300363 show_sniff_min_interval, store_sniff_min_interval);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200364
Marcel Holtmann90855d72008-08-18 13:23:53 +0200365static struct attribute *bt_host_attrs[] = {
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100366 &dev_attr_bus.attr,
Marcel Holtmann943da252010-02-13 02:28:41 +0100367 &dev_attr_type.attr,
Marcel Holtmann90855d72008-08-18 13:23:53 +0200368 &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,
Marcel Holtmann90855d72008-08-18 13:23:53 +0200375 &dev_attr_idle_timeout.attr,
376 &dev_attr_sniff_max_interval.attr,
377 &dev_attr_sniff_min_interval.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 NULL
379};
380
Marcel Holtmann90855d72008-08-18 13:23:53 +0200381static struct attribute_group bt_host_group = {
382 .attrs = bt_host_attrs,
383};
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200384
David Brownella4dbd672009-06-24 10:06:31 -0700385static const struct attribute_group *bt_host_groups[] = {
Marcel Holtmann90855d72008-08-18 13:23:53 +0200386 &bt_host_group,
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200387 NULL
388};
389
Marcel Holtmann90855d72008-08-18 13:23:53 +0200390static void bt_host_release(struct device *dev)
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200391{
David Herrmann2dd10682012-02-09 21:58:34 +0100392 struct hci_dev *hdev = to_hci_dev(dev);
393 kfree(hdev);
David Herrmann46e06532012-01-07 15:47:21 +0100394 module_put(THIS_MODULE);
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200395}
396
Marcel Holtmann90855d72008-08-18 13:23:53 +0200397static struct device_type bt_host = {
398 .name = "host",
399 .groups = bt_host_groups,
400 .release = bt_host_release,
401};
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200402
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000403static int inquiry_cache_show(struct seq_file *f, void *p)
Marcel Holtmannca325f62010-02-08 16:22:31 +0100404{
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000405 struct hci_dev *hdev = f->private;
Johan Hedberg30883512012-01-04 14:16:21 +0200406 struct discovery_state *cache = &hdev->discovery;
Marcel Holtmannca325f62010-02-08 16:22:31 +0100407 struct inquiry_entry *e;
Marcel Holtmannca325f62010-02-08 16:22:31 +0100408
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300409 hci_dev_lock(hdev);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100410
Johan Hedberg561aafb2012-01-04 13:31:59 +0200411 list_for_each_entry(e, &cache->all, all) {
Marcel Holtmannca325f62010-02-08 16:22:31 +0100412 struct inquiry_data *data = &e->data;
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000413 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 -0300414 batostr(&data->bdaddr),
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000415 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);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100420 }
421
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300422 hci_dev_unlock(hdev);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100423
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000424 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);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100430}
431
432static const struct file_operations inquiry_cache_fops = {
Marcel Holtmannd4612cb2010-03-02 15:48:23 +0000433 .open = inquiry_cache_open,
434 .read = seq_read,
435 .llseek = seq_lseek,
436 .release = single_release,
Marcel Holtmannca325f62010-02-08 16:22:31 +0100437};
438
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200439static int blacklist_show(struct seq_file *f, void *p)
440{
441 struct hci_dev *hdev = f->private;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200442 struct bdaddr_list *b;
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200443
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300444 hci_dev_lock(hdev);
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200445
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200446 list_for_each_entry(b, &hdev->blacklist, list)
Gustavo F. Padovand6b2eb22010-09-03 18:29:46 -0300447 seq_printf(f, "%s\n", batostr(&b->bdaddr));
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200448
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300449 hci_dev_unlock(hdev);
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200450
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};
Johan Hedberg930e1332011-01-04 11:39:44 +0200465
466static void print_bt_uuid(struct seq_file *f, u8 *uuid)
467{
Andrei Emeltchenko739f43e2012-03-12 15:59:31 +0200468 __be32 data0, data4;
469 __be16 data1, data2, data3, data5;
Johan Hedberg930e1332011-01-04 11:39:44 +0200470
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",
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300479 ntohl(data0), ntohs(data1), ntohs(data2), ntohs(data3),
480 ntohl(data4), ntohs(data5));
Johan Hedberg930e1332011-01-04 11:39:44 +0200481}
482
483static int uuids_show(struct seq_file *f, void *p)
484{
485 struct hci_dev *hdev = f->private;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200486 struct bt_uuid *uuid;
Johan Hedberg930e1332011-01-04 11:39:44 +0200487
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300488 hci_dev_lock(hdev);
Johan Hedberg930e1332011-01-04 11:39:44 +0200489
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200490 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg930e1332011-01-04 11:39:44 +0200491 print_bt_uuid(f, uuid->uuid);
Johan Hedberg930e1332011-01-04 11:39:44 +0200492
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300493 hci_dev_unlock(hdev);
Johan Hedberg930e1332011-01-04 11:39:44 +0200494
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
Johan Hedberg9f616562011-04-28 11:28:54 -0700510static int auto_accept_delay_set(void *data, u64 val)
511{
512 struct hci_dev *hdev = data;
513
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300514 hci_dev_lock(hdev);
Johan Hedberg9f616562011-04-28 11:28:54 -0700515
516 hdev->auto_accept_delay = val;
517
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300518 hci_dev_unlock(hdev);
Johan Hedberg9f616562011-04-28 11:28:54 -0700519
520 return 0;
521}
522
523static int auto_accept_delay_get(void *data, u64 *val)
524{
525 struct hci_dev *hdev = data;
526
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300527 hci_dev_lock(hdev);
Johan Hedberg9f616562011-04-28 11:28:54 -0700528
529 *val = hdev->auto_accept_delay;
530
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300531 hci_dev_unlock(hdev);
Johan Hedberg9f616562011-04-28 11:28:54 -0700532
533 return 0;
534}
535
536DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300537 auto_accept_delay_set, "%llu\n");
Johan Hedberg9f616562011-04-28 11:28:54 -0700538
David Herrmann0ac7e702011-10-08 14:58:47 +0200539void 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
David Herrmann46e06532012-01-07 15:47:21 +0100546 __module_get(THIS_MODULE);
David Herrmann0ac7e702011-10-08 14:58:47 +0200547 device_initialize(dev);
548}
549
David Herrmannce242972011-10-08 14:58:48 +0200550int hci_add_sysfs(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200552 struct device *dev = &hdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int err;
554
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100555 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Marcel Holtmann2e792992008-11-30 12:17:29 +0100557 dev_set_name(dev, "%s", hdev->name);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200558
David Herrmann0ac7e702011-10-08 14:58:47 +0200559 err = device_add(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (err < 0)
561 return err;
562
Marcel Holtmannca325f62010-02-08 16:22:31 +0100563 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,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300571 hdev, &inquiry_cache_fops);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100572
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200573 debugfs_create_file("blacklist", 0444, hdev->debugfs,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300574 hdev, &blacklist_fops);
Johan Hedberg32c2ece2010-05-18 13:54:49 +0200575
Johan Hedberg930e1332011-01-04 11:39:44 +0200576 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
577
Johan Hedberg9f616562011-04-28 11:28:54 -0700578 debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
Gustavo Padovanb80f0212012-05-17 00:36:23 -0300579 &auto_accept_delay_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581}
582
David Herrmannce242972011-10-08 14:58:48 +0200583void hci_del_sysfs(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100585 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Marcel Holtmannca325f62010-02-08 16:22:31 +0100587 debugfs_remove_recursive(hdev->debugfs);
588
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200589 device_del(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592int __init bt_sysfs_init(void)
593{
Marcel Holtmannca325f62010-02-08 16:22:31 +0100594 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
595
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200596 bt_class = class_create(THIS_MODULE, "bluetooth");
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100597 if (IS_ERR(bt_class))
Marcel Holtmann90855d72008-08-18 13:23:53 +0200598 return PTR_ERR(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200599
600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Arnaud Patard860e13b2006-09-28 15:29:37 -0700603void bt_sysfs_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200605 class_destroy(bt_class);
Marcel Holtmannca325f62010-02-08 16:22:31 +0100606
607 debugfs_remove_recursive(bt_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}