Commit | Line | Data |
---|---|---|
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 | |
15 | ||
4d0eb004 | 16 | static inline char *typetostr(int type) |
1da177e4 | 17 | { |
4d0eb004 | 18 | switch (type) { |
0ac53939 | 19 | case HCI_VIRTUAL: |
4d0eb004 MH |
20 | 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"; | |
0ac53939 MH |
31 | case HCI_SDIO: |
32 | return "SDIO"; | |
4d0eb004 MH |
33 | default: |
34 | return "UNKNOWN"; | |
35 | } | |
1da177e4 LT |
36 | } |
37 | ||
a91f2e39 | 38 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 | 39 | { |
a91f2e39 | 40 | struct hci_dev *hdev = dev_get_drvdata(dev); |
4d0eb004 | 41 | return sprintf(buf, "%s\n", typetostr(hdev->type)); |
1da177e4 LT |
42 | } |
43 | ||
a91f2e39 | 44 | static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 | 45 | { |
a91f2e39 | 46 | struct hci_dev *hdev = dev_get_drvdata(dev); |
1da177e4 LT |
47 | bdaddr_t bdaddr; |
48 | baswap(&bdaddr, &hdev->bdaddr); | |
49 | return sprintf(buf, "%s\n", batostr(&bdaddr)); | |
50 | } | |
51 | ||
a91f2e39 | 52 | static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 | 53 | { |
a91f2e39 | 54 | struct hci_dev *hdev = dev_get_drvdata(dev); |
1da177e4 LT |
55 | 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 | ||
a91f2e39 | 76 | static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf) |
04837f64 | 77 | { |
a91f2e39 | 78 | struct hci_dev *hdev = dev_get_drvdata(dev); |
04837f64 MH |
79 | return sprintf(buf, "%d\n", hdev->idle_timeout); |
80 | } | |
81 | ||
a91f2e39 | 82 | static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
04837f64 | 83 | { |
a91f2e39 | 84 | struct hci_dev *hdev = dev_get_drvdata(dev); |
04837f64 MH |
85 | 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 | ||
a91f2e39 | 100 | static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf) |
04837f64 | 101 | { |
a91f2e39 | 102 | struct hci_dev *hdev = dev_get_drvdata(dev); |
04837f64 MH |
103 | return sprintf(buf, "%d\n", hdev->sniff_max_interval); |
104 | } | |
105 | ||
a91f2e39 | 106 | static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
04837f64 | 107 | { |
a91f2e39 | 108 | struct hci_dev *hdev = dev_get_drvdata(dev); |
04837f64 MH |
109 | 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 | ||
a91f2e39 | 127 | static ssize_t show_sniff_min_interval(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->sniff_min_interval); |
131 | } | |
132 | ||
a91f2e39 | 133 | static ssize_t store_sniff_min_interval(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 | __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 | ||
a91f2e39 MH |
154 | static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); |
155 | static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); | |
a91f2e39 | 156 | static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL); |
1da177e4 | 157 | |
a91f2e39 | 158 | static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR, |
04837f64 | 159 | show_idle_timeout, store_idle_timeout); |
a91f2e39 | 160 | static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR, |
04837f64 | 161 | show_sniff_max_interval, store_sniff_max_interval); |
a91f2e39 | 162 | static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR, |
04837f64 MH |
163 | show_sniff_min_interval, store_sniff_min_interval); |
164 | ||
a91f2e39 | 165 | static struct device_attribute *bt_attrs[] = { |
a91f2e39 MH |
166 | &dev_attr_type, |
167 | &dev_attr_address, | |
a91f2e39 MH |
168 | &dev_attr_inquiry_cache, |
169 | &dev_attr_idle_timeout, | |
170 | &dev_attr_sniff_max_interval, | |
171 | &dev_attr_sniff_min_interval, | |
1da177e4 LT |
172 | NULL |
173 | }; | |
174 | ||
b219e3ac MH |
175 | static 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 | ||
181 | static 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) \ | |
190 | struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store) | |
191 | ||
192 | static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL); | |
193 | static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL); | |
194 | ||
195 | static struct device_attribute *conn_attrs[] = { | |
196 | &conn_attr_type, | |
197 | &conn_attr_address, | |
198 | NULL | |
199 | }; | |
200 | ||
a91f2e39 | 201 | struct class *bt_class = NULL; |
be9d1227 MH |
202 | EXPORT_SYMBOL_GPL(bt_class); |
203 | ||
27d35284 MH |
204 | static struct bus_type bt_bus = { |
205 | .name = "bluetooth", | |
206 | }; | |
207 | ||
208 | static struct platform_device *bt_platform; | |
209 | ||
a91f2e39 MH |
210 | static void bt_release(struct device *dev) |
211 | { | |
b219e3ac MH |
212 | void *data = dev_get_drvdata(dev); |
213 | kfree(data); | |
214 | } | |
215 | ||
216 | static 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 | ||
227 | void 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 | ||
250 | static void del_conn(void *data) | |
251 | { | |
252 | struct hci_conn *conn = data; | |
253 | device_del(&conn->dev); | |
254 | } | |
255 | ||
256 | void 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); | |
a91f2e39 MH |
263 | } |
264 | ||
1da177e4 LT |
265 | int hci_register_sysfs(struct hci_dev *hdev) |
266 | { | |
a91f2e39 | 267 | struct device *dev = &hdev->dev; |
1da177e4 LT |
268 | unsigned int i; |
269 | int err; | |
270 | ||
271 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); | |
272 | ||
a91f2e39 MH |
273 | dev->class = bt_class; |
274 | ||
275 | if (hdev->parent) | |
276 | dev->parent = hdev->parent; | |
277 | else | |
278 | dev->parent = &bt_platform->dev; | |
279 | ||
280 | strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE); | |
1da177e4 | 281 | |
a91f2e39 | 282 | dev->release = bt_release; |
27d35284 | 283 | |
a91f2e39 | 284 | dev_set_drvdata(dev, hdev); |
27d35284 | 285 | |
a91f2e39 | 286 | err = device_register(dev); |
1da177e4 LT |
287 | if (err < 0) |
288 | return err; | |
289 | ||
290 | for (i = 0; bt_attrs[i]; i++) | |
a91f2e39 | 291 | device_create_file(dev, bt_attrs[i]); |
1da177e4 LT |
292 | |
293 | return 0; | |
294 | } | |
295 | ||
296 | void hci_unregister_sysfs(struct hci_dev *hdev) | |
297 | { | |
1da177e4 LT |
298 | BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type); |
299 | ||
4d0eb004 | 300 | device_del(&hdev->dev); |
1da177e4 LT |
301 | } |
302 | ||
303 | int __init bt_sysfs_init(void) | |
304 | { | |
27d35284 MH |
305 | 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 | ||
a91f2e39 MH |
317 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
318 | if (IS_ERR(bt_class)) { | |
27d35284 MH |
319 | bus_unregister(&bt_bus); |
320 | platform_device_unregister(bt_platform); | |
a91f2e39 | 321 | return PTR_ERR(bt_class); |
27d35284 MH |
322 | } |
323 | ||
324 | return 0; | |
1da177e4 LT |
325 | } |
326 | ||
327 | void __exit bt_sysfs_cleanup(void) | |
328 | { | |
a91f2e39 | 329 | class_destroy(bt_class); |
27d35284 MH |
330 | |
331 | bus_unregister(&bt_bus); | |
332 | ||
333 | platform_device_unregister(bt_platform); | |
1da177e4 | 334 | } |