]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/bluetooth/hci_sysfs.c
[Bluetooth] Track status of remote Simple Pairing mode
[mirror_ubuntu-bionic-kernel.git] / net / bluetooth / hci_sysfs.c
CommitLineData
1da177e4
LT
1/* Bluetooth HCI driver model support. */
2
1da177e4
LT
3#include <linux/kernel.h>
4#include <linux/init.h>
5
27d35284
MH
6#include <linux/platform_device.h>
7
1da177e4
LT
8#include <net/bluetooth/bluetooth.h>
9#include <net/bluetooth/hci_core.h>
10
11#ifndef CONFIG_BT_HCI_CORE_DEBUG
12#undef BT_DBG
13#define BT_DBG(D...)
14#endif
b6c06321
DY
15static struct workqueue_struct *btaddconn;
16static struct workqueue_struct *btdelconn;
1da177e4 17
4d0eb004 18static inline char *typetostr(int type)
1da177e4 19{
4d0eb004 20 switch (type) {
0ac53939 21 case HCI_VIRTUAL:
4d0eb004
MH
22 return "VIRTUAL";
23 case HCI_USB:
24 return "USB";
25 case HCI_PCCARD:
26 return "PCCARD";
27 case HCI_UART:
28 return "UART";
29 case HCI_RS232:
30 return "RS232";
31 case HCI_PCI:
32 return "PCI";
0ac53939
MH
33 case HCI_SDIO:
34 return "SDIO";
4d0eb004
MH
35 default:
36 return "UNKNOWN";
37 }
1da177e4
LT
38}
39
a91f2e39 40static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 41{
a91f2e39 42 struct hci_dev *hdev = dev_get_drvdata(dev);
4d0eb004 43 return sprintf(buf, "%s\n", typetostr(hdev->type));
1da177e4
LT
44}
45
a9de9248
MH
46static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
47{
48 struct hci_dev *hdev = dev_get_drvdata(dev);
49 char name[249];
50 int i;
51
52 for (i = 0; i < 248; i++)
53 name[i] = hdev->dev_name[i];
54
55 name[248] = '\0';
56 return sprintf(buf, "%s\n", name);
57}
58
59static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
60{
61 struct hci_dev *hdev = dev_get_drvdata(dev);
62 return sprintf(buf, "0x%.2x%.2x%.2x\n",
63 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
64}
65
a91f2e39 66static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 67{
a91f2e39 68 struct hci_dev *hdev = dev_get_drvdata(dev);
1da177e4
LT
69 bdaddr_t bdaddr;
70 baswap(&bdaddr, &hdev->bdaddr);
71 return sprintf(buf, "%s\n", batostr(&bdaddr));
72}
73
a9de9248
MH
74static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
75{
76 struct hci_dev *hdev = dev_get_drvdata(dev);
77
78 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
79 hdev->features[0], hdev->features[1],
80 hdev->features[2], hdev->features[3],
81 hdev->features[4], hdev->features[5],
82 hdev->features[6], hdev->features[7]);
83}
84
1143e5a6
MH
85static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
86{
87 struct hci_dev *hdev = dev_get_drvdata(dev);
88 return sprintf(buf, "%d\n", hdev->manufacturer);
89}
90
91static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
92{
93 struct hci_dev *hdev = dev_get_drvdata(dev);
94 return sprintf(buf, "%d\n", hdev->hci_ver);
95}
96
97static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
98{
99 struct hci_dev *hdev = dev_get_drvdata(dev);
100 return sprintf(buf, "%d\n", hdev->hci_rev);
101}
102
a91f2e39 103static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 104{
a91f2e39 105 struct hci_dev *hdev = dev_get_drvdata(dev);
1da177e4
LT
106 struct inquiry_cache *cache = &hdev->inq_cache;
107 struct inquiry_entry *e;
108 int n = 0;
109
110 hci_dev_lock_bh(hdev);
111
112 for (e = cache->list; e; e = e->next) {
113 struct inquiry_data *data = &e->data;
114 bdaddr_t bdaddr;
115 baswap(&bdaddr, &data->bdaddr);
116 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
117 batostr(&bdaddr),
118 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
119 data->dev_class[2], data->dev_class[1], data->dev_class[0],
120 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
121 }
122
123 hci_dev_unlock_bh(hdev);
124 return n;
125}
126
a91f2e39 127static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 128{
a91f2e39 129 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
130 return sprintf(buf, "%d\n", hdev->idle_timeout);
131}
132
a91f2e39 133static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 134{
a91f2e39 135 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
136 char *ptr;
137 __u32 val;
138
139 val = simple_strtoul(buf, &ptr, 10);
140 if (ptr == buf)
141 return -EINVAL;
142
143 if (val != 0 && (val < 500 || val > 3600000))
144 return -EINVAL;
145
146 hdev->idle_timeout = val;
147
148 return count;
149}
150
a91f2e39 151static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 152{
a91f2e39 153 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
154 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
155}
156
a91f2e39 157static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 158{
a91f2e39 159 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
160 char *ptr;
161 __u16 val;
162
163 val = simple_strtoul(buf, &ptr, 10);
164 if (ptr == buf)
165 return -EINVAL;
166
167 if (val < 0x0002 || val > 0xFFFE || val % 2)
168 return -EINVAL;
169
170 if (val < hdev->sniff_min_interval)
171 return -EINVAL;
172
173 hdev->sniff_max_interval = val;
174
175 return count;
176}
177
a91f2e39 178static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 179{
a91f2e39 180 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
181 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
182}
183
a91f2e39 184static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 185{
a91f2e39 186 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
187 char *ptr;
188 __u16 val;
189
190 val = simple_strtoul(buf, &ptr, 10);
191 if (ptr == buf)
192 return -EINVAL;
193
194 if (val < 0x0002 || val > 0xFFFE || val % 2)
195 return -EINVAL;
196
197 if (val > hdev->sniff_max_interval)
198 return -EINVAL;
199
200 hdev->sniff_min_interval = val;
201
202 return count;
203}
204
a91f2e39 205static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
a9de9248
MH
206static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
207static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
a91f2e39 208static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
a9de9248 209static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
1143e5a6
MH
210static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
211static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
212static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
a91f2e39 213static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
1da177e4 214
a91f2e39 215static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
04837f64 216 show_idle_timeout, store_idle_timeout);
a91f2e39 217static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
04837f64 218 show_sniff_max_interval, store_sniff_max_interval);
a91f2e39 219static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
04837f64
MH
220 show_sniff_min_interval, store_sniff_min_interval);
221
a91f2e39 222static struct device_attribute *bt_attrs[] = {
a91f2e39 223 &dev_attr_type,
a9de9248
MH
224 &dev_attr_name,
225 &dev_attr_class,
a91f2e39 226 &dev_attr_address,
a9de9248 227 &dev_attr_features,
1143e5a6
MH
228 &dev_attr_manufacturer,
229 &dev_attr_hci_version,
230 &dev_attr_hci_revision,
a91f2e39
MH
231 &dev_attr_inquiry_cache,
232 &dev_attr_idle_timeout,
233 &dev_attr_sniff_max_interval,
234 &dev_attr_sniff_min_interval,
1da177e4
LT
235 NULL
236};
237
b219e3ac
MH
238static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
239{
240 struct hci_conn *conn = dev_get_drvdata(dev);
241 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
242}
243
244static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
245{
246 struct hci_conn *conn = dev_get_drvdata(dev);
247 bdaddr_t bdaddr;
248 baswap(&bdaddr, &conn->dst);
249 return sprintf(buf, "%s\n", batostr(&bdaddr));
250}
251
252#define CONN_ATTR(_name,_mode,_show,_store) \
253struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
254
255static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
256static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
257
258static struct device_attribute *conn_attrs[] = {
259 &conn_attr_type,
260 &conn_attr_address,
261 NULL
262};
263
a91f2e39 264struct class *bt_class = NULL;
be9d1227
MH
265EXPORT_SYMBOL_GPL(bt_class);
266
27d35284
MH
267static struct bus_type bt_bus = {
268 .name = "bluetooth",
269};
270
271static struct platform_device *bt_platform;
272
a91f2e39
MH
273static void bt_release(struct device *dev)
274{
b219e3ac
MH
275 void *data = dev_get_drvdata(dev);
276 kfree(data);
277}
278
c4028958 279static void add_conn(struct work_struct *work)
b219e3ac 280{
c4028958 281 struct hci_conn *conn = container_of(work, struct hci_conn, work);
b219e3ac
MH
282 int i;
283
b6c06321 284 flush_workqueue(btdelconn);
5396c935 285
c893779e 286 if (device_add(&conn->dev) < 0) {
df5c37ea
MH
287 BT_ERR("Failed to register connection device");
288 return;
289 }
b219e3ac
MH
290
291 for (i = 0; conn_attrs[i]; i++)
df5c37ea
MH
292 if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
293 BT_ERR("Failed to create connection attribute");
b219e3ac
MH
294}
295
296void hci_conn_add_sysfs(struct hci_conn *conn)
297{
298 struct hci_dev *hdev = conn->hdev;
299 bdaddr_t *ba = &conn->dst;
300
301 BT_DBG("conn %p", conn);
302
e52726de
MH
303 conn->dev.bus = &bt_bus;
304 conn->dev.parent = &hdev->dev;
305
b219e3ac
MH
306 conn->dev.release = bt_release;
307
308 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
309 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
310 conn->type == ACL_LINK ? "acl" : "sco",
311 ba->b[5], ba->b[4], ba->b[3],
312 ba->b[2], ba->b[1], ba->b[0]);
313
314 dev_set_drvdata(&conn->dev, conn);
315
c893779e
MH
316 device_initialize(&conn->dev);
317
c4028958 318 INIT_WORK(&conn->work, add_conn);
b219e3ac 319
b6c06321 320 queue_work(btaddconn, &conn->work);
b219e3ac
MH
321}
322
8ac62dc7
DY
323/*
324 * The rfcomm tty device will possibly retain even when conn
325 * is down, and sysfs doesn't support move zombie device,
326 * so we should move the device before conn device is destroyed.
327 */
acea6852
DY
328static int __match_tty(struct device *dev, void *data)
329{
8ac62dc7 330 return !strncmp(dev->bus_id, "rfcomm", 6);
acea6852
DY
331}
332
c4028958 333static void del_conn(struct work_struct *work)
b219e3ac 334{
c4028958 335 struct hci_conn *conn = container_of(work, struct hci_conn, work);
0cd63c80 336 struct hci_dev *hdev = conn->hdev;
acea6852 337
988d0093
DM
338 while (1) {
339 struct device *dev;
340
341 dev = device_find_child(&conn->dev, NULL, __match_tty);
342 if (!dev)
343 break;
acea6852
DY
344 device_move(dev, NULL);
345 put_device(dev);
346 }
0cd63c80 347
b219e3ac 348 device_del(&conn->dev);
38b7da09 349 put_device(&conn->dev);
0cd63c80 350 hci_dev_put(hdev);
b219e3ac
MH
351}
352
353void hci_conn_del_sysfs(struct hci_conn *conn)
354{
355 BT_DBG("conn %p", conn);
356
c893779e
MH
357 if (!device_is_registered(&conn->dev))
358 return;
359
c4028958 360 INIT_WORK(&conn->work, del_conn);
b219e3ac 361
b6c06321 362 queue_work(btdelconn, &conn->work);
a91f2e39
MH
363}
364
1da177e4
LT
365int hci_register_sysfs(struct hci_dev *hdev)
366{
a91f2e39 367 struct device *dev = &hdev->dev;
1da177e4
LT
368 unsigned int i;
369 int err;
370
371 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
372
53c1d4b0 373 dev->bus = &bt_bus;
e9c4bec6 374 dev->parent = hdev->parent;
a91f2e39
MH
375
376 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
1da177e4 377
a91f2e39 378 dev->release = bt_release;
27d35284 379
a91f2e39 380 dev_set_drvdata(dev, hdev);
27d35284 381
a91f2e39 382 err = device_register(dev);
1da177e4
LT
383 if (err < 0)
384 return err;
385
386 for (i = 0; bt_attrs[i]; i++)
df5c37ea
MH
387 if (device_create_file(dev, bt_attrs[i]) < 0)
388 BT_ERR("Failed to create device attribute");
1da177e4 389
d12db0b0 390 if (sysfs_create_link(&bt_class->subsys.kobj,
53c1d4b0
MH
391 &dev->kobj, kobject_name(&dev->kobj)) < 0)
392 BT_ERR("Failed to create class symlink");
393
1da177e4
LT
394 return 0;
395}
396
397void hci_unregister_sysfs(struct hci_dev *hdev)
398{
1da177e4
LT
399 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
400
d12db0b0 401 sysfs_remove_link(&bt_class->subsys.kobj,
53c1d4b0
MH
402 kobject_name(&hdev->dev.kobj));
403
4d0eb004 404 device_del(&hdev->dev);
1da177e4
LT
405}
406
407int __init bt_sysfs_init(void)
408{
27d35284
MH
409 int err;
410
b6c06321
DY
411 btaddconn = create_singlethread_workqueue("btaddconn");
412 if (!btaddconn) {
413 err = -ENOMEM;
414 goto out;
415 }
5396c935 416
b6c06321
DY
417 btdelconn = create_singlethread_workqueue("btdelconn");
418 if (!btdelconn) {
419 err = -ENOMEM;
420 goto out_del;
421 }
422
27d35284 423 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
b6c06321
DY
424 if (IS_ERR(bt_platform)) {
425 err = PTR_ERR(bt_platform);
426 goto out_platform;
427 }
27d35284
MH
428
429 err = bus_register(&bt_bus);
b6c06321
DY
430 if (err < 0)
431 goto out_bus;
27d35284 432
a91f2e39
MH
433 bt_class = class_create(THIS_MODULE, "bluetooth");
434 if (IS_ERR(bt_class)) {
b6c06321
DY
435 err = PTR_ERR(bt_class);
436 goto out_class;
27d35284
MH
437 }
438
439 return 0;
b6c06321
DY
440
441out_class:
442 bus_unregister(&bt_bus);
443out_bus:
444 platform_device_unregister(bt_platform);
445out_platform:
446 destroy_workqueue(btdelconn);
447out_del:
448 destroy_workqueue(btaddconn);
449out:
450 return err;
1da177e4
LT
451}
452
860e13b5 453void bt_sysfs_cleanup(void)
1da177e4 454{
b6c06321 455 destroy_workqueue(btaddconn);
5396c935 456
b6c06321 457 destroy_workqueue(btdelconn);
5396c935 458
a91f2e39 459 class_destroy(bt_class);
5396c935 460
27d35284 461 bus_unregister(&bt_bus);
5396c935 462
27d35284 463 platform_device_unregister(bt_platform);
1da177e4 464}