bluetooth: remove improper bluetooth class symlinks.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / hci_sysfs.c
blobc85bf8f678dc5f1456788d3ebce5212627211912
1 /* Bluetooth HCI driver model support. */
3 #include <linux/kernel.h>
4 #include <linux/init.h>
6 #include <linux/platform_device.h>
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
11 #ifndef CONFIG_BT_HCI_CORE_DEBUG
12 #undef BT_DBG
13 #define BT_DBG(D...)
14 #endif
15 static struct workqueue_struct *btaddconn;
16 static struct workqueue_struct *btdelconn;
18 static inline char *typetostr(int type)
20 switch (type) {
21 case HCI_VIRTUAL:
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";
33 case HCI_SDIO:
34 return "SDIO";
35 default:
36 return "UNKNOWN";
40 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
42 struct hci_dev *hdev = dev_get_drvdata(dev);
43 return sprintf(buf, "%s\n", typetostr(hdev->type));
46 static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
48 struct hci_dev *hdev = dev_get_drvdata(dev);
49 char name[249];
50 int i;
52 for (i = 0; i < 248; i++)
53 name[i] = hdev->dev_name[i];
55 name[248] = '\0';
56 return sprintf(buf, "%s\n", name);
59 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
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]);
66 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
68 struct hci_dev *hdev = dev_get_drvdata(dev);
69 bdaddr_t bdaddr;
70 baswap(&bdaddr, &hdev->bdaddr);
71 return sprintf(buf, "%s\n", batostr(&bdaddr));
74 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
76 struct hci_dev *hdev = dev_get_drvdata(dev);
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]);
85 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
87 struct hci_dev *hdev = dev_get_drvdata(dev);
88 return sprintf(buf, "%d\n", hdev->manufacturer);
91 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
93 struct hci_dev *hdev = dev_get_drvdata(dev);
94 return sprintf(buf, "%d\n", hdev->hci_ver);
97 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
99 struct hci_dev *hdev = dev_get_drvdata(dev);
100 return sprintf(buf, "%d\n", hdev->hci_rev);
103 static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
105 struct hci_dev *hdev = dev_get_drvdata(dev);
106 struct inquiry_cache *cache = &hdev->inq_cache;
107 struct inquiry_entry *e;
108 int n = 0;
110 hci_dev_lock_bh(hdev);
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 %d %u\n",
117 batostr(&bdaddr),
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);
125 hci_dev_unlock_bh(hdev);
126 return n;
129 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
131 struct hci_dev *hdev = dev_get_drvdata(dev);
132 return sprintf(buf, "%d\n", hdev->idle_timeout);
135 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
137 struct hci_dev *hdev = dev_get_drvdata(dev);
138 char *ptr;
139 __u32 val;
141 val = simple_strtoul(buf, &ptr, 10);
142 if (ptr == buf)
143 return -EINVAL;
145 if (val != 0 && (val < 500 || val > 3600000))
146 return -EINVAL;
148 hdev->idle_timeout = val;
150 return count;
153 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
155 struct hci_dev *hdev = dev_get_drvdata(dev);
156 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
159 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
161 struct hci_dev *hdev = dev_get_drvdata(dev);
162 char *ptr;
163 __u16 val;
165 val = simple_strtoul(buf, &ptr, 10);
166 if (ptr == buf)
167 return -EINVAL;
169 if (val < 0x0002 || val > 0xFFFE || val % 2)
170 return -EINVAL;
172 if (val < hdev->sniff_min_interval)
173 return -EINVAL;
175 hdev->sniff_max_interval = val;
177 return count;
180 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
182 struct hci_dev *hdev = dev_get_drvdata(dev);
183 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
186 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
188 struct hci_dev *hdev = dev_get_drvdata(dev);
189 char *ptr;
190 __u16 val;
192 val = simple_strtoul(buf, &ptr, 10);
193 if (ptr == buf)
194 return -EINVAL;
196 if (val < 0x0002 || val > 0xFFFE || val % 2)
197 return -EINVAL;
199 if (val > hdev->sniff_max_interval)
200 return -EINVAL;
202 hdev->sniff_min_interval = val;
204 return count;
207 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
208 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
209 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
210 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
211 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
212 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
213 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
214 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
215 static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
217 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
218 show_idle_timeout, store_idle_timeout);
219 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
220 show_sniff_max_interval, store_sniff_max_interval);
221 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
222 show_sniff_min_interval, store_sniff_min_interval);
224 static struct device_attribute *bt_attrs[] = {
225 &dev_attr_type,
226 &dev_attr_name,
227 &dev_attr_class,
228 &dev_attr_address,
229 &dev_attr_features,
230 &dev_attr_manufacturer,
231 &dev_attr_hci_version,
232 &dev_attr_hci_revision,
233 &dev_attr_inquiry_cache,
234 &dev_attr_idle_timeout,
235 &dev_attr_sniff_max_interval,
236 &dev_attr_sniff_min_interval,
237 NULL
240 static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
242 struct hci_conn *conn = dev_get_drvdata(dev);
243 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
246 static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
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));
254 static ssize_t show_conn_features(struct device *dev, struct device_attribute *attr, char *buf)
256 struct hci_conn *conn = dev_get_drvdata(dev);
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]);
265 #define CONN_ATTR(_name,_mode,_show,_store) \
266 struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
268 static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
269 static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
270 static CONN_ATTR(features, S_IRUGO, show_conn_features, NULL);
272 static struct device_attribute *conn_attrs[] = {
273 &conn_attr_type,
274 &conn_attr_address,
275 &conn_attr_features,
276 NULL
279 struct class *bt_class = NULL;
280 EXPORT_SYMBOL_GPL(bt_class);
282 static struct bus_type bt_bus = {
283 .name = "bluetooth",
286 static struct platform_device *bt_platform;
288 static void bt_release(struct device *dev)
290 void *data = dev_get_drvdata(dev);
291 kfree(data);
294 static void add_conn(struct work_struct *work)
296 struct hci_conn *conn = container_of(work, struct hci_conn, work);
297 int i;
299 flush_workqueue(btdelconn);
301 if (device_add(&conn->dev) < 0) {
302 BT_ERR("Failed to register connection device");
303 return;
306 for (i = 0; conn_attrs[i]; i++)
307 if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
308 BT_ERR("Failed to create connection attribute");
311 void hci_conn_add_sysfs(struct hci_conn *conn)
313 struct hci_dev *hdev = conn->hdev;
315 BT_DBG("conn %p", conn);
317 conn->dev.bus = &bt_bus;
318 conn->dev.parent = &hdev->dev;
320 conn->dev.release = bt_release;
322 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "%s:%d",
323 hdev->name, conn->handle);
325 dev_set_drvdata(&conn->dev, conn);
327 device_initialize(&conn->dev);
329 INIT_WORK(&conn->work, add_conn);
331 queue_work(btaddconn, &conn->work);
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.
339 static int __match_tty(struct device *dev, void *data)
341 return !strncmp(dev->bus_id, "rfcomm", 6);
344 static void del_conn(struct work_struct *work)
346 struct hci_conn *conn = container_of(work, struct hci_conn, work);
347 struct hci_dev *hdev = conn->hdev;
349 while (1) {
350 struct device *dev;
352 dev = device_find_child(&conn->dev, NULL, __match_tty);
353 if (!dev)
354 break;
355 device_move(dev, NULL);
356 put_device(dev);
359 device_del(&conn->dev);
360 put_device(&conn->dev);
361 hci_dev_put(hdev);
364 void hci_conn_del_sysfs(struct hci_conn *conn)
366 BT_DBG("conn %p", conn);
368 if (!device_is_registered(&conn->dev))
369 return;
371 INIT_WORK(&conn->work, del_conn);
373 queue_work(btdelconn, &conn->work);
376 int hci_register_sysfs(struct hci_dev *hdev)
378 struct device *dev = &hdev->dev;
379 unsigned int i;
380 int err;
382 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
384 dev->bus = &bt_bus;
385 dev->parent = hdev->parent;
387 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
389 dev->release = bt_release;
391 dev_set_drvdata(dev, hdev);
393 err = device_register(dev);
394 if (err < 0)
395 return err;
397 for (i = 0; bt_attrs[i]; i++)
398 if (device_create_file(dev, bt_attrs[i]) < 0)
399 BT_ERR("Failed to create device attribute");
401 return 0;
404 void hci_unregister_sysfs(struct hci_dev *hdev)
406 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
408 device_del(&hdev->dev);
411 int __init bt_sysfs_init(void)
413 int err;
415 btaddconn = create_singlethread_workqueue("btaddconn");
416 if (!btaddconn) {
417 err = -ENOMEM;
418 goto out;
421 btdelconn = create_singlethread_workqueue("btdelconn");
422 if (!btdelconn) {
423 err = -ENOMEM;
424 goto out_del;
427 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
428 if (IS_ERR(bt_platform)) {
429 err = PTR_ERR(bt_platform);
430 goto out_platform;
433 err = bus_register(&bt_bus);
434 if (err < 0)
435 goto out_bus;
437 bt_class = class_create(THIS_MODULE, "bluetooth");
438 if (IS_ERR(bt_class)) {
439 err = PTR_ERR(bt_class);
440 goto out_class;
443 return 0;
445 out_class:
446 bus_unregister(&bt_bus);
447 out_bus:
448 platform_device_unregister(bt_platform);
449 out_platform:
450 destroy_workqueue(btdelconn);
451 out_del:
452 destroy_workqueue(btaddconn);
453 out:
454 return err;
457 void bt_sysfs_cleanup(void)
459 destroy_workqueue(btaddconn);
461 destroy_workqueue(btdelconn);
463 class_destroy(bt_class);
465 bus_unregister(&bt_bus);
467 platform_device_unregister(bt_platform);