blob: 3eeeb7a86e753cc638993e1ef9214de51adbdfbe [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>
4#include <linux/init.h>
5
Marcel Holtmann27d35282006-07-03 10:02:37 +02006#include <linux/platform_device.h>
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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
15
Marcel Holtmann4d0eb002006-07-06 12:34:41 +020016static inline char *typetostr(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
Marcel Holtmann4d0eb002006-07-06 12:34:41 +020018 switch (type) {
Marcel Holtmann0ac53932006-07-08 13:57:15 +020019 case HCI_VIRTUAL:
Marcel Holtmann4d0eb002006-07-06 12:34:41 +020020 return "VIRTUAL";
21 case HCI_USB:
22 return "USB";
23 case HCI_PCCARD:
24 return "PCCARD";
25 case HCI_UART:
26 return "UART";
27 case HCI_RS232:
28 return "RS232";
29 case HCI_PCI:
30 return "PCI";
Marcel Holtmann0ac53932006-07-08 13:57:15 +020031 case HCI_SDIO:
32 return "SDIO";
Marcel Holtmann4d0eb002006-07-06 12:34:41 +020033 default:
34 return "UNKNOWN";
35 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020038static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020040 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann4d0eb002006-07-06 12:34:41 +020041 return sprintf(buf, "%s\n", typetostr(hdev->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020044static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020046 struct hci_dev *hdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 bdaddr_t bdaddr;
48 baswap(&bdaddr, &hdev->bdaddr);
49 return sprintf(buf, "%s\n", batostr(&bdaddr));
50}
51
Marcel Holtmann1143e5a2006-09-23 09:57:20 +020052static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
53{
54 struct hci_dev *hdev = dev_get_drvdata(dev);
55 return sprintf(buf, "%d\n", hdev->manufacturer);
56}
57
58static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
59{
60 struct hci_dev *hdev = dev_get_drvdata(dev);
61 return sprintf(buf, "%d\n", hdev->hci_ver);
62}
63
64static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
65{
66 struct hci_dev *hdev = dev_get_drvdata(dev);
67 return sprintf(buf, "%d\n", hdev->hci_rev);
68}
69
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020070static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020072 struct hci_dev *hdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct inquiry_cache *cache = &hdev->inq_cache;
74 struct inquiry_entry *e;
75 int n = 0;
76
77 hci_dev_lock_bh(hdev);
78
79 for (e = cache->list; e; e = e->next) {
80 struct inquiry_data *data = &e->data;
81 bdaddr_t bdaddr;
82 baswap(&bdaddr, &data->bdaddr);
83 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
84 batostr(&bdaddr),
85 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
86 data->dev_class[2], data->dev_class[1], data->dev_class[0],
87 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
88 }
89
90 hci_dev_unlock_bh(hdev);
91 return n;
92}
93
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020094static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +020095{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020096 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +020097 return sprintf(buf, "%d\n", hdev->idle_timeout);
98}
99
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200100static 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 +0200101{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200102 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200103 char *ptr;
104 __u32 val;
105
106 val = simple_strtoul(buf, &ptr, 10);
107 if (ptr == buf)
108 return -EINVAL;
109
110 if (val != 0 && (val < 500 || val > 3600000))
111 return -EINVAL;
112
113 hdev->idle_timeout = val;
114
115 return count;
116}
117
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200118static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200119{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200120 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200121 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
122}
123
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200124static 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 +0200125{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200126 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200127 char *ptr;
128 __u16 val;
129
130 val = simple_strtoul(buf, &ptr, 10);
131 if (ptr == buf)
132 return -EINVAL;
133
134 if (val < 0x0002 || val > 0xFFFE || val % 2)
135 return -EINVAL;
136
137 if (val < hdev->sniff_min_interval)
138 return -EINVAL;
139
140 hdev->sniff_max_interval = val;
141
142 return count;
143}
144
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200145static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200146{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200147 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200148 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
149}
150
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200151static 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 +0200152{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200153 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200154 char *ptr;
155 __u16 val;
156
157 val = simple_strtoul(buf, &ptr, 10);
158 if (ptr == buf)
159 return -EINVAL;
160
161 if (val < 0x0002 || val > 0xFFFE || val % 2)
162 return -EINVAL;
163
164 if (val > hdev->sniff_max_interval)
165 return -EINVAL;
166
167 hdev->sniff_min_interval = val;
168
169 return count;
170}
171
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200172static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
173static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200174static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
175static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
176static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200177static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200179static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200180 show_idle_timeout, store_idle_timeout);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200181static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200182 show_sniff_max_interval, store_sniff_max_interval);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200183static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200184 show_sniff_min_interval, store_sniff_min_interval);
185
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200186static struct device_attribute *bt_attrs[] = {
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200187 &dev_attr_type,
188 &dev_attr_address,
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200189 &dev_attr_manufacturer,
190 &dev_attr_hci_version,
191 &dev_attr_hci_revision,
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200192 &dev_attr_inquiry_cache,
193 &dev_attr_idle_timeout,
194 &dev_attr_sniff_max_interval,
195 &dev_attr_sniff_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 NULL
197};
198
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200199static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
200{
201 struct hci_conn *conn = dev_get_drvdata(dev);
202 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
203}
204
205static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
206{
207 struct hci_conn *conn = dev_get_drvdata(dev);
208 bdaddr_t bdaddr;
209 baswap(&bdaddr, &conn->dst);
210 return sprintf(buf, "%s\n", batostr(&bdaddr));
211}
212
213#define CONN_ATTR(_name,_mode,_show,_store) \
214struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
215
216static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
217static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
218
219static struct device_attribute *conn_attrs[] = {
220 &conn_attr_type,
221 &conn_attr_address,
222 NULL
223};
224
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200225struct class *bt_class = NULL;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800226EXPORT_SYMBOL_GPL(bt_class);
227
Marcel Holtmann27d35282006-07-03 10:02:37 +0200228static struct bus_type bt_bus = {
229 .name = "bluetooth",
230};
231
232static struct platform_device *bt_platform;
233
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200234static void bt_release(struct device *dev)
235{
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200236 void *data = dev_get_drvdata(dev);
237 kfree(data);
238}
239
240static void add_conn(void *data)
241{
242 struct hci_conn *conn = data;
243 int i;
244
Marcel Holtmanndf5c37e2006-10-15 17:30:45 +0200245 if (device_register(&conn->dev) < 0) {
246 BT_ERR("Failed to register connection device");
247 return;
248 }
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200249
250 for (i = 0; conn_attrs[i]; i++)
Marcel Holtmanndf5c37e2006-10-15 17:30:45 +0200251 if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
252 BT_ERR("Failed to create connection attribute");
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200253}
254
255void hci_conn_add_sysfs(struct hci_conn *conn)
256{
257 struct hci_dev *hdev = conn->hdev;
258 bdaddr_t *ba = &conn->dst;
259
260 BT_DBG("conn %p", conn);
261
Marcel Holtmanne52726d2006-11-18 22:14:05 +0100262 conn->dev.bus = &bt_bus;
263 conn->dev.parent = &hdev->dev;
264
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200265 conn->dev.release = bt_release;
266
267 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
268 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
269 conn->type == ACL_LINK ? "acl" : "sco",
270 ba->b[5], ba->b[4], ba->b[3],
271 ba->b[2], ba->b[1], ba->b[0]);
272
273 dev_set_drvdata(&conn->dev, conn);
274
275 INIT_WORK(&conn->work, add_conn, (void *) conn);
276
277 schedule_work(&conn->work);
278}
279
280static void del_conn(void *data)
281{
282 struct hci_conn *conn = data;
283 device_del(&conn->dev);
284}
285
286void hci_conn_del_sysfs(struct hci_conn *conn)
287{
288 BT_DBG("conn %p", conn);
289
290 INIT_WORK(&conn->work, del_conn, (void *) conn);
291
292 schedule_work(&conn->work);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295int hci_register_sysfs(struct hci_dev *hdev)
296{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200297 struct device *dev = &hdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned int i;
299 int err;
300
301 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
302
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200303 dev->class = bt_class;
Marcel Holtmanne9c4bec2006-10-15 17:30:50 +0200304 dev->parent = hdev->parent;
Marcel Holtmann27d35282006-07-03 10:02:37 +0200305
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200306 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200307
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200308 dev->release = bt_release;
309
310 dev_set_drvdata(dev, hdev);
311
312 err = device_register(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (err < 0)
314 return err;
315
316 for (i = 0; bt_attrs[i]; i++)
Marcel Holtmanndf5c37e2006-10-15 17:30:45 +0200317 if (device_create_file(dev, bt_attrs[i]) < 0)
318 BT_ERR("Failed to create device attribute");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 return 0;
321}
322
323void hci_unregister_sysfs(struct hci_dev *hdev)
324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
326
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200327 device_del(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330int __init bt_sysfs_init(void)
331{
Marcel Holtmann27d35282006-07-03 10:02:37 +0200332 int err;
333
334 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
335 if (IS_ERR(bt_platform))
336 return PTR_ERR(bt_platform);
337
338 err = bus_register(&bt_bus);
339 if (err < 0) {
340 platform_device_unregister(bt_platform);
341 return err;
342 }
343
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200344 bt_class = class_create(THIS_MODULE, "bluetooth");
345 if (IS_ERR(bt_class)) {
Marcel Holtmann27d35282006-07-03 10:02:37 +0200346 bus_unregister(&bt_bus);
347 platform_device_unregister(bt_platform);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200348 return PTR_ERR(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200349 }
350
351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Arnaud Patard860e13b2006-09-28 15:29:37 -0700354void bt_sysfs_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200356 class_destroy(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200357
358 bus_unregister(&bt_bus);
359
360 platform_device_unregister(bt_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}