Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[deliverable/linux.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);
a8bd28ba 116 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
1da177e4 117 batostr(&bdaddr),
a8bd28ba
MH
118 data->pscan_rep_mode, data->pscan_period_mode,
119 data->pscan_mode, data->dev_class[2],
120 data->dev_class[1], data->dev_class[0],
121 __le16_to_cpu(data->clock_offset),
122 data->rssi, data->ssp_mode, e->timestamp);
1da177e4
LT
123 }
124
125 hci_dev_unlock_bh(hdev);
126 return n;
127}
128
a91f2e39 129static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 130{
a91f2e39 131 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
132 return sprintf(buf, "%d\n", hdev->idle_timeout);
133}
134
a91f2e39 135static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 136{
a91f2e39 137 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
138 char *ptr;
139 __u32 val;
140
141 val = simple_strtoul(buf, &ptr, 10);
142 if (ptr == buf)
143 return -EINVAL;
144
145 if (val != 0 && (val < 500 || val > 3600000))
146 return -EINVAL;
147
148 hdev->idle_timeout = val;
149
150 return count;
151}
152
a91f2e39 153static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 154{
a91f2e39 155 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
156 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
157}
158
a91f2e39 159static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 160{
a91f2e39 161 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
162 char *ptr;
163 __u16 val;
164
165 val = simple_strtoul(buf, &ptr, 10);
166 if (ptr == buf)
167 return -EINVAL;
168
169 if (val < 0x0002 || val > 0xFFFE || val % 2)
170 return -EINVAL;
171
172 if (val < hdev->sniff_min_interval)
173 return -EINVAL;
174
175 hdev->sniff_max_interval = val;
176
177 return count;
178}
179
a91f2e39 180static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
04837f64 181{
a91f2e39 182 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
183 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
184}
185
a91f2e39 186static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
04837f64 187{
a91f2e39 188 struct hci_dev *hdev = dev_get_drvdata(dev);
04837f64
MH
189 char *ptr;
190 __u16 val;
191
192 val = simple_strtoul(buf, &ptr, 10);
193 if (ptr == buf)
194 return -EINVAL;
195
196 if (val < 0x0002 || val > 0xFFFE || val % 2)
197 return -EINVAL;
198
199 if (val > hdev->sniff_max_interval)
200 return -EINVAL;
201
202 hdev->sniff_min_interval = val;
203
204 return count;
205}
206
a91f2e39 207static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
a9de9248
MH
208static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
209static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
a91f2e39 210static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
a9de9248 211static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
1143e5a6
MH
212static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
213static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
214static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
a91f2e39 215static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
1da177e4 216
a91f2e39 217static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
04837f64 218 show_idle_timeout, store_idle_timeout);
a91f2e39 219static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
04837f64 220 show_sniff_max_interval, store_sniff_max_interval);
a91f2e39 221static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
04837f64
MH
222 show_sniff_min_interval, store_sniff_min_interval);
223
a91f2e39 224static struct device_attribute *bt_attrs[] = {
a91f2e39 225 &dev_attr_type,
a9de9248
MH
226 &dev_attr_name,
227 &dev_attr_class,
a91f2e39 228 &dev_attr_address,
a9de9248 229 &dev_attr_features,
1143e5a6
MH
230 &dev_attr_manufacturer,
231 &dev_attr_hci_version,
232 &dev_attr_hci_revision,
a91f2e39
MH
233 &dev_attr_inquiry_cache,
234 &dev_attr_idle_timeout,
235 &dev_attr_sniff_max_interval,
236 &dev_attr_sniff_min_interval,
1da177e4
LT
237 NULL
238};
239
b219e3ac
MH
240static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
241{
242 struct hci_conn *conn = dev_get_drvdata(dev);
243 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
244}
245
246static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
247{
248 struct hci_conn *conn = dev_get_drvdata(dev);
249 bdaddr_t bdaddr;
250 baswap(&bdaddr, &conn->dst);
251 return sprintf(buf, "%s\n", batostr(&bdaddr));
252}
253
a8bd28ba
MH
254static ssize_t show_conn_features(struct device *dev, struct device_attribute *attr, char *buf)
255{
256 struct hci_conn *conn = dev_get_drvdata(dev);
257
258 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
259 conn->features[0], conn->features[1],
260 conn->features[2], conn->features[3],
261 conn->features[4], conn->features[5],
262 conn->features[6], conn->features[7]);
263}
264
b219e3ac
MH
265#define CONN_ATTR(_name,_mode,_show,_store) \
266struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
267
268static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
269static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
a8bd28ba 270static CONN_ATTR(features, S_IRUGO, show_conn_features, NULL);
b219e3ac
MH
271
272static struct device_attribute *conn_attrs[] = {
273 &conn_attr_type,
274 &conn_attr_address,
a8bd28ba 275 &conn_attr_features,
b219e3ac
MH
276 NULL
277};
278
a91f2e39 279struct class *bt_class = NULL;
be9d1227
MH
280EXPORT_SYMBOL_GPL(bt_class);
281
27d35284
MH
282static struct bus_type bt_bus = {
283 .name = "bluetooth",
284};
285
286static struct platform_device *bt_platform;
287
a91f2e39
MH
288static void bt_release(struct device *dev)
289{
b219e3ac
MH
290 void *data = dev_get_drvdata(dev);
291 kfree(data);
292}
293
c4028958 294static void add_conn(struct work_struct *work)
b219e3ac 295{
c4028958 296 struct hci_conn *conn = container_of(work, struct hci_conn, work);
b219e3ac
MH
297 int i;
298
b6c06321 299 flush_workqueue(btdelconn);
5396c935 300
c893779e 301 if (device_add(&conn->dev) < 0) {
df5c37ea
MH
302 BT_ERR("Failed to register connection device");
303 return;
304 }
b219e3ac
MH
305
306 for (i = 0; conn_attrs[i]; i++)
df5c37ea
MH
307 if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
308 BT_ERR("Failed to create connection attribute");
b219e3ac
MH
309}
310
311void hci_conn_add_sysfs(struct hci_conn *conn)
312{
313 struct hci_dev *hdev = conn->hdev;
b219e3ac
MH
314
315 BT_DBG("conn %p", conn);
316
e52726de
MH
317 conn->dev.bus = &bt_bus;
318 conn->dev.parent = &hdev->dev;
319
b219e3ac
MH
320 conn->dev.release = bt_release;
321
7d0db0a3
MH
322 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "%s:%d",
323 hdev->name, conn->handle);
b219e3ac
MH
324
325 dev_set_drvdata(&conn->dev, conn);
326
c893779e
MH
327 device_initialize(&conn->dev);
328
c4028958 329 INIT_WORK(&conn->work, add_conn);
b219e3ac 330
b6c06321 331 queue_work(btaddconn, &conn->work);
b219e3ac
MH
332}
333
8ac62dc7
DY
334/*
335 * The rfcomm tty device will possibly retain even when conn
336 * is down, and sysfs doesn't support move zombie device,
337 * so we should move the device before conn device is destroyed.
338 */
acea6852
DY
339static int __match_tty(struct device *dev, void *data)
340{
8ac62dc7 341 return !strncmp(dev->bus_id, "rfcomm", 6);
acea6852
DY
342}
343
c4028958 344static void del_conn(struct work_struct *work)
b219e3ac 345{
c4028958 346 struct hci_conn *conn = container_of(work, struct hci_conn, work);
0cd63c80 347 struct hci_dev *hdev = conn->hdev;
acea6852 348
988d0093
DM
349 while (1) {
350 struct device *dev;
351
352 dev = device_find_child(&conn->dev, NULL, __match_tty);
353 if (!dev)
354 break;
acea6852
DY
355 device_move(dev, NULL);
356 put_device(dev);
357 }
0cd63c80 358
b219e3ac 359 device_del(&conn->dev);
38b7da09 360 put_device(&conn->dev);
0cd63c80 361 hci_dev_put(hdev);
b219e3ac
MH
362}
363
364void hci_conn_del_sysfs(struct hci_conn *conn)
365{
366 BT_DBG("conn %p", conn);
367
c893779e
MH
368 if (!device_is_registered(&conn->dev))
369 return;
370
c4028958 371 INIT_WORK(&conn->work, del_conn);
b219e3ac 372
b6c06321 373 queue_work(btdelconn, &conn->work);
a91f2e39
MH
374}
375
1da177e4
LT
376int hci_register_sysfs(struct hci_dev *hdev)
377{
a91f2e39 378 struct device *dev = &hdev->dev;
1da177e4
LT
379 unsigned int i;
380 int err;
381
382 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
383
53c1d4b0 384 dev->bus = &bt_bus;
e9c4bec6 385 dev->parent = hdev->parent;
a91f2e39
MH
386
387 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
1da177e4 388
a91f2e39 389 dev->release = bt_release;
27d35284 390
a91f2e39 391 dev_set_drvdata(dev, hdev);
27d35284 392
a91f2e39 393 err = device_register(dev);
1da177e4
LT
394 if (err < 0)
395 return err;
396
397 for (i = 0; bt_attrs[i]; i++)
df5c37ea
MH
398 if (device_create_file(dev, bt_attrs[i]) < 0)
399 BT_ERR("Failed to create device attribute");
1da177e4
LT
400
401 return 0;
402}
403
404void hci_unregister_sysfs(struct hci_dev *hdev)
405{
1da177e4
LT
406 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
407
4d0eb004 408 device_del(&hdev->dev);
1da177e4
LT
409}
410
411int __init bt_sysfs_init(void)
412{
27d35284
MH
413 int err;
414
b6c06321
DY
415 btaddconn = create_singlethread_workqueue("btaddconn");
416 if (!btaddconn) {
417 err = -ENOMEM;
418 goto out;
419 }
5396c935 420
b6c06321
DY
421 btdelconn = create_singlethread_workqueue("btdelconn");
422 if (!btdelconn) {
423 err = -ENOMEM;
424 goto out_del;
425 }
426
27d35284 427 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
b6c06321
DY
428 if (IS_ERR(bt_platform)) {
429 err = PTR_ERR(bt_platform);
430 goto out_platform;
431 }
27d35284
MH
432
433 err = bus_register(&bt_bus);
b6c06321
DY
434 if (err < 0)
435 goto out_bus;
27d35284 436
a91f2e39
MH
437 bt_class = class_create(THIS_MODULE, "bluetooth");
438 if (IS_ERR(bt_class)) {
b6c06321
DY
439 err = PTR_ERR(bt_class);
440 goto out_class;
27d35284
MH
441 }
442
443 return 0;
b6c06321
DY
444
445out_class:
446 bus_unregister(&bt_bus);
447out_bus:
448 platform_device_unregister(bt_platform);
449out_platform:
450 destroy_workqueue(btdelconn);
451out_del:
452 destroy_workqueue(btaddconn);
453out:
454 return err;
1da177e4
LT
455}
456
860e13b5 457void bt_sysfs_cleanup(void)
1da177e4 458{
b6c06321 459 destroy_workqueue(btaddconn);
5396c935 460
b6c06321 461 destroy_workqueue(btdelconn);
5396c935 462
a91f2e39 463 class_destroy(bt_class);
5396c935 464
27d35284 465 bus_unregister(&bt_bus);
5396c935 466
27d35284 467 platform_device_unregister(bt_platform);
1da177e4 468}
This page took 0.373191 seconds and 5 git commands to generate.