blob: 09c61615e961b5d423b58bea608282a875705354 [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 Holtmanna91f2e32006-07-03 10:02:41 +020052static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020054 struct hci_dev *hdev = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct inquiry_cache *cache = &hdev->inq_cache;
56 struct inquiry_entry *e;
57 int n = 0;
58
59 hci_dev_lock_bh(hdev);
60
61 for (e = cache->list; e; e = e->next) {
62 struct inquiry_data *data = &e->data;
63 bdaddr_t bdaddr;
64 baswap(&bdaddr, &data->bdaddr);
65 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
66 batostr(&bdaddr),
67 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
68 data->dev_class[2], data->dev_class[1], data->dev_class[0],
69 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
70 }
71
72 hci_dev_unlock_bh(hdev);
73 return n;
74}
75
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020076static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +020077{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020078 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +020079 return sprintf(buf, "%d\n", hdev->idle_timeout);
80}
81
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020082static 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 +020083{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020084 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +020085 char *ptr;
86 __u32 val;
87
88 val = simple_strtoul(buf, &ptr, 10);
89 if (ptr == buf)
90 return -EINVAL;
91
92 if (val != 0 && (val < 500 || val > 3600000))
93 return -EINVAL;
94
95 hdev->idle_timeout = val;
96
97 return count;
98}
99
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200100static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
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 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
104}
105
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200106static 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 +0200107{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200108 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200109 char *ptr;
110 __u16 val;
111
112 val = simple_strtoul(buf, &ptr, 10);
113 if (ptr == buf)
114 return -EINVAL;
115
116 if (val < 0x0002 || val > 0xFFFE || val % 2)
117 return -EINVAL;
118
119 if (val < hdev->sniff_min_interval)
120 return -EINVAL;
121
122 hdev->sniff_max_interval = val;
123
124 return count;
125}
126
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200127static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200128{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200129 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200130 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
131}
132
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200133static 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 +0200134{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200135 struct hci_dev *hdev = dev_get_drvdata(dev);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200136 char *ptr;
137 __u16 val;
138
139 val = simple_strtoul(buf, &ptr, 10);
140 if (ptr == buf)
141 return -EINVAL;
142
143 if (val < 0x0002 || val > 0xFFFE || val % 2)
144 return -EINVAL;
145
146 if (val > hdev->sniff_max_interval)
147 return -EINVAL;
148
149 hdev->sniff_min_interval = val;
150
151 return count;
152}
153
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200154static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
155static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200156static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200158static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200159 show_idle_timeout, store_idle_timeout);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200160static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200161 show_sniff_max_interval, store_sniff_max_interval);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200162static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200163 show_sniff_min_interval, store_sniff_min_interval);
164
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200165static struct device_attribute *bt_attrs[] = {
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200166 &dev_attr_type,
167 &dev_attr_address,
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200168 &dev_attr_inquiry_cache,
169 &dev_attr_idle_timeout,
170 &dev_attr_sniff_max_interval,
171 &dev_attr_sniff_min_interval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 NULL
173};
174
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200175static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
176{
177 struct hci_conn *conn = dev_get_drvdata(dev);
178 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
179}
180
181static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
182{
183 struct hci_conn *conn = dev_get_drvdata(dev);
184 bdaddr_t bdaddr;
185 baswap(&bdaddr, &conn->dst);
186 return sprintf(buf, "%s\n", batostr(&bdaddr));
187}
188
189#define CONN_ATTR(_name,_mode,_show,_store) \
190struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
191
192static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
193static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
194
195static struct device_attribute *conn_attrs[] = {
196 &conn_attr_type,
197 &conn_attr_address,
198 NULL
199};
200
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200201struct class *bt_class = NULL;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -0800202EXPORT_SYMBOL_GPL(bt_class);
203
Marcel Holtmann27d35282006-07-03 10:02:37 +0200204static struct bus_type bt_bus = {
205 .name = "bluetooth",
206};
207
208static struct platform_device *bt_platform;
209
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200210static void bt_release(struct device *dev)
211{
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200212 void *data = dev_get_drvdata(dev);
213 kfree(data);
214}
215
216static void add_conn(void *data)
217{
218 struct hci_conn *conn = data;
219 int i;
220
221 device_register(&conn->dev);
222
223 for (i = 0; conn_attrs[i]; i++)
224 device_create_file(&conn->dev, conn_attrs[i]);
225}
226
227void hci_conn_add_sysfs(struct hci_conn *conn)
228{
229 struct hci_dev *hdev = conn->hdev;
230 bdaddr_t *ba = &conn->dst;
231
232 BT_DBG("conn %p", conn);
233
234 conn->dev.parent = &hdev->dev;
235 conn->dev.release = bt_release;
236
237 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
238 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
239 conn->type == ACL_LINK ? "acl" : "sco",
240 ba->b[5], ba->b[4], ba->b[3],
241 ba->b[2], ba->b[1], ba->b[0]);
242
243 dev_set_drvdata(&conn->dev, conn);
244
245 INIT_WORK(&conn->work, add_conn, (void *) conn);
246
247 schedule_work(&conn->work);
248}
249
250static void del_conn(void *data)
251{
252 struct hci_conn *conn = data;
253 device_del(&conn->dev);
254}
255
256void hci_conn_del_sysfs(struct hci_conn *conn)
257{
258 BT_DBG("conn %p", conn);
259
260 INIT_WORK(&conn->work, del_conn, (void *) conn);
261
262 schedule_work(&conn->work);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265int hci_register_sysfs(struct hci_dev *hdev)
266{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200267 struct device *dev = &hdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int i;
269 int err;
270
271 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
272
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200273 dev->class = bt_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200275 if (hdev->parent)
276 dev->parent = hdev->parent;
277 else
278 dev->parent = &bt_platform->dev;
Marcel Holtmann27d35282006-07-03 10:02:37 +0200279
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200280 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200281
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200282 dev->release = bt_release;
283
284 dev_set_drvdata(dev, hdev);
285
286 err = device_register(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (err < 0)
288 return err;
289
290 for (i = 0; bt_attrs[i]; i++)
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200291 device_create_file(dev, bt_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return 0;
294}
295
296void hci_unregister_sysfs(struct hci_dev *hdev)
297{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
299
Marcel Holtmann4d0eb002006-07-06 12:34:41 +0200300 device_del(&hdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303int __init bt_sysfs_init(void)
304{
Marcel Holtmann27d35282006-07-03 10:02:37 +0200305 int err;
306
307 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
308 if (IS_ERR(bt_platform))
309 return PTR_ERR(bt_platform);
310
311 err = bus_register(&bt_bus);
312 if (err < 0) {
313 platform_device_unregister(bt_platform);
314 return err;
315 }
316
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200317 bt_class = class_create(THIS_MODULE, "bluetooth");
318 if (IS_ERR(bt_class)) {
Marcel Holtmann27d35282006-07-03 10:02:37 +0200319 bus_unregister(&bt_bus);
320 platform_device_unregister(bt_platform);
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200321 return PTR_ERR(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200322 }
323
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327void __exit bt_sysfs_cleanup(void)
328{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200329 class_destroy(bt_class);
Marcel Holtmann27d35282006-07-03 10:02:37 +0200330
331 bus_unregister(&bt_bus);
332
333 platform_device_unregister(bt_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}