]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bluetooth/hci_sysfs.c
Bluetooth: Move uuids debugfs entry creation into hci_core.c
[karo-tx-linux.git] / net / bluetooth / hci_sysfs.c
1 /* Bluetooth HCI driver model support. */
2
3 #include <linux/debugfs.h>
4 #include <linux/module.h>
5
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
8
9 static struct class *bt_class;
10
11 struct dentry *bt_debugfs;
12 EXPORT_SYMBOL_GPL(bt_debugfs);
13
14 static inline char *link_typetostr(int type)
15 {
16         switch (type) {
17         case ACL_LINK:
18                 return "ACL";
19         case SCO_LINK:
20                 return "SCO";
21         case ESCO_LINK:
22                 return "eSCO";
23         case LE_LINK:
24                 return "LE";
25         default:
26                 return "UNKNOWN";
27         }
28 }
29
30 static ssize_t show_link_type(struct device *dev,
31                               struct device_attribute *attr, char *buf)
32 {
33         struct hci_conn *conn = to_hci_conn(dev);
34         return sprintf(buf, "%s\n", link_typetostr(conn->type));
35 }
36
37 static ssize_t show_link_address(struct device *dev,
38                                  struct device_attribute *attr, char *buf)
39 {
40         struct hci_conn *conn = to_hci_conn(dev);
41         return sprintf(buf, "%pMR\n", &conn->dst);
42 }
43
44 static ssize_t show_link_features(struct device *dev,
45                                   struct device_attribute *attr, char *buf)
46 {
47         struct hci_conn *conn = to_hci_conn(dev);
48
49         return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
50                        conn->features[0][0], conn->features[0][1],
51                        conn->features[0][2], conn->features[0][3],
52                        conn->features[0][4], conn->features[0][5],
53                        conn->features[0][6], conn->features[0][7]);
54 }
55
56 #define LINK_ATTR(_name, _mode, _show, _store) \
57 struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
58
59 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
60 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
61 static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
62
63 static struct attribute *bt_link_attrs[] = {
64         &link_attr_type.attr,
65         &link_attr_address.attr,
66         &link_attr_features.attr,
67         NULL
68 };
69
70 static struct attribute_group bt_link_group = {
71         .attrs = bt_link_attrs,
72 };
73
74 static const struct attribute_group *bt_link_groups[] = {
75         &bt_link_group,
76         NULL
77 };
78
79 static void bt_link_release(struct device *dev)
80 {
81         struct hci_conn *conn = to_hci_conn(dev);
82         kfree(conn);
83 }
84
85 static struct device_type bt_link = {
86         .name    = "link",
87         .groups  = bt_link_groups,
88         .release = bt_link_release,
89 };
90
91 /*
92  * The rfcomm tty device will possibly retain even when conn
93  * is down, and sysfs doesn't support move zombie device,
94  * so we should move the device before conn device is destroyed.
95  */
96 static int __match_tty(struct device *dev, void *data)
97 {
98         return !strncmp(dev_name(dev), "rfcomm", 6);
99 }
100
101 void hci_conn_init_sysfs(struct hci_conn *conn)
102 {
103         struct hci_dev *hdev = conn->hdev;
104
105         BT_DBG("conn %p", conn);
106
107         conn->dev.type = &bt_link;
108         conn->dev.class = bt_class;
109         conn->dev.parent = &hdev->dev;
110
111         device_initialize(&conn->dev);
112 }
113
114 void hci_conn_add_sysfs(struct hci_conn *conn)
115 {
116         struct hci_dev *hdev = conn->hdev;
117
118         BT_DBG("conn %p", conn);
119
120         dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
121
122         if (device_add(&conn->dev) < 0) {
123                 BT_ERR("Failed to register connection device");
124                 return;
125         }
126
127         hci_dev_hold(hdev);
128 }
129
130 void hci_conn_del_sysfs(struct hci_conn *conn)
131 {
132         struct hci_dev *hdev = conn->hdev;
133
134         if (!device_is_registered(&conn->dev))
135                 return;
136
137         while (1) {
138                 struct device *dev;
139
140                 dev = device_find_child(&conn->dev, NULL, __match_tty);
141                 if (!dev)
142                         break;
143                 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
144                 put_device(dev);
145         }
146
147         device_del(&conn->dev);
148
149         hci_dev_put(hdev);
150 }
151
152 static inline char *host_bustostr(int bus)
153 {
154         switch (bus) {
155         case HCI_VIRTUAL:
156                 return "VIRTUAL";
157         case HCI_USB:
158                 return "USB";
159         case HCI_PCCARD:
160                 return "PCCARD";
161         case HCI_UART:
162                 return "UART";
163         case HCI_RS232:
164                 return "RS232";
165         case HCI_PCI:
166                 return "PCI";
167         case HCI_SDIO:
168                 return "SDIO";
169         default:
170                 return "UNKNOWN";
171         }
172 }
173
174 static inline char *host_typetostr(int type)
175 {
176         switch (type) {
177         case HCI_BREDR:
178                 return "BR/EDR";
179         case HCI_AMP:
180                 return "AMP";
181         default:
182                 return "UNKNOWN";
183         }
184 }
185
186 static ssize_t show_bus(struct device *dev,
187                         struct device_attribute *attr, char *buf)
188 {
189         struct hci_dev *hdev = to_hci_dev(dev);
190         return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
191 }
192
193 static ssize_t show_type(struct device *dev,
194                          struct device_attribute *attr, char *buf)
195 {
196         struct hci_dev *hdev = to_hci_dev(dev);
197         return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
198 }
199
200 static ssize_t show_name(struct device *dev,
201                          struct device_attribute *attr, char *buf)
202 {
203         struct hci_dev *hdev = to_hci_dev(dev);
204         char name[HCI_MAX_NAME_LENGTH + 1];
205         int i;
206
207         for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
208                 name[i] = hdev->dev_name[i];
209
210         name[HCI_MAX_NAME_LENGTH] = '\0';
211         return sprintf(buf, "%s\n", name);
212 }
213
214 static ssize_t show_class(struct device *dev,
215                           struct device_attribute *attr, char *buf)
216 {
217         struct hci_dev *hdev = to_hci_dev(dev);
218         return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
219                        hdev->dev_class[1], hdev->dev_class[0]);
220 }
221
222 static ssize_t show_address(struct device *dev,
223                             struct device_attribute *attr, char *buf)
224 {
225         struct hci_dev *hdev = to_hci_dev(dev);
226         return sprintf(buf, "%pMR\n", &hdev->bdaddr);
227 }
228
229 static ssize_t show_features(struct device *dev,
230                              struct device_attribute *attr, char *buf)
231 {
232         struct hci_dev *hdev = to_hci_dev(dev);
233
234         return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
235                        hdev->features[0][0], hdev->features[0][1],
236                        hdev->features[0][2], hdev->features[0][3],
237                        hdev->features[0][4], hdev->features[0][5],
238                        hdev->features[0][6], hdev->features[0][7]);
239 }
240
241 static ssize_t show_manufacturer(struct device *dev,
242                                  struct device_attribute *attr, char *buf)
243 {
244         struct hci_dev *hdev = to_hci_dev(dev);
245         return sprintf(buf, "%d\n", hdev->manufacturer);
246 }
247
248 static ssize_t show_hci_version(struct device *dev,
249                                 struct device_attribute *attr, char *buf)
250 {
251         struct hci_dev *hdev = to_hci_dev(dev);
252         return sprintf(buf, "%d\n", hdev->hci_ver);
253 }
254
255 static ssize_t show_hci_revision(struct device *dev,
256                                  struct device_attribute *attr, char *buf)
257 {
258         struct hci_dev *hdev = to_hci_dev(dev);
259         return sprintf(buf, "%d\n", hdev->hci_rev);
260 }
261
262 static ssize_t show_idle_timeout(struct device *dev,
263                                  struct device_attribute *attr, char *buf)
264 {
265         struct hci_dev *hdev = to_hci_dev(dev);
266         return sprintf(buf, "%d\n", hdev->idle_timeout);
267 }
268
269 static ssize_t store_idle_timeout(struct device *dev,
270                                   struct device_attribute *attr,
271                                   const char *buf, size_t count)
272 {
273         struct hci_dev *hdev = to_hci_dev(dev);
274         unsigned int val;
275         int rv;
276
277         rv = kstrtouint(buf, 0, &val);
278         if (rv < 0)
279                 return rv;
280
281         if (val != 0 && (val < 500 || val > 3600000))
282                 return -EINVAL;
283
284         hdev->idle_timeout = val;
285
286         return count;
287 }
288
289 static ssize_t show_sniff_max_interval(struct device *dev,
290                                        struct device_attribute *attr, char *buf)
291 {
292         struct hci_dev *hdev = to_hci_dev(dev);
293         return sprintf(buf, "%d\n", hdev->sniff_max_interval);
294 }
295
296 static ssize_t store_sniff_max_interval(struct device *dev,
297                                         struct device_attribute *attr,
298                                         const char *buf, size_t count)
299 {
300         struct hci_dev *hdev = to_hci_dev(dev);
301         u16 val;
302         int rv;
303
304         rv = kstrtou16(buf, 0, &val);
305         if (rv < 0)
306                 return rv;
307
308         if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
309                 return -EINVAL;
310
311         hdev->sniff_max_interval = val;
312
313         return count;
314 }
315
316 static ssize_t show_sniff_min_interval(struct device *dev,
317                                        struct device_attribute *attr, char *buf)
318 {
319         struct hci_dev *hdev = to_hci_dev(dev);
320         return sprintf(buf, "%d\n", hdev->sniff_min_interval);
321 }
322
323 static ssize_t store_sniff_min_interval(struct device *dev,
324                                         struct device_attribute *attr,
325                                         const char *buf, size_t count)
326 {
327         struct hci_dev *hdev = to_hci_dev(dev);
328         u16 val;
329         int rv;
330
331         rv = kstrtou16(buf, 0, &val);
332         if (rv < 0)
333                 return rv;
334
335         if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
336                 return -EINVAL;
337
338         hdev->sniff_min_interval = val;
339
340         return count;
341 }
342
343 static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
344 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
345 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
346 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
347 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
348 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
349 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
350 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
351 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
352
353 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
354                    show_idle_timeout, store_idle_timeout);
355 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
356                    show_sniff_max_interval, store_sniff_max_interval);
357 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
358                    show_sniff_min_interval, store_sniff_min_interval);
359
360 static struct attribute *bt_host_attrs[] = {
361         &dev_attr_bus.attr,
362         &dev_attr_type.attr,
363         &dev_attr_name.attr,
364         &dev_attr_class.attr,
365         &dev_attr_address.attr,
366         &dev_attr_features.attr,
367         &dev_attr_manufacturer.attr,
368         &dev_attr_hci_version.attr,
369         &dev_attr_hci_revision.attr,
370         &dev_attr_idle_timeout.attr,
371         &dev_attr_sniff_max_interval.attr,
372         &dev_attr_sniff_min_interval.attr,
373         NULL
374 };
375
376 static struct attribute_group bt_host_group = {
377         .attrs = bt_host_attrs,
378 };
379
380 static const struct attribute_group *bt_host_groups[] = {
381         &bt_host_group,
382         NULL
383 };
384
385 static void bt_host_release(struct device *dev)
386 {
387         struct hci_dev *hdev = to_hci_dev(dev);
388         kfree(hdev);
389         module_put(THIS_MODULE);
390 }
391
392 static struct device_type bt_host = {
393         .name    = "host",
394         .groups  = bt_host_groups,
395         .release = bt_host_release,
396 };
397
398 void hci_init_sysfs(struct hci_dev *hdev)
399 {
400         struct device *dev = &hdev->dev;
401
402         dev->type = &bt_host;
403         dev->class = bt_class;
404
405         __module_get(THIS_MODULE);
406         device_initialize(dev);
407 }
408
409 int hci_add_sysfs(struct hci_dev *hdev)
410 {
411         struct device *dev = &hdev->dev;
412         int err;
413
414         BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
415
416         dev_set_name(dev, "%s", hdev->name);
417
418         err = device_add(dev);
419         if (err < 0)
420                 return err;
421
422         if (!bt_debugfs)
423                 return 0;
424
425         hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
426         if (!hdev->debugfs)
427                 return 0;
428
429         return 0;
430 }
431
432 void hci_del_sysfs(struct hci_dev *hdev)
433 {
434         BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
435
436         debugfs_remove_recursive(hdev->debugfs);
437
438         device_del(&hdev->dev);
439 }
440
441 int __init bt_sysfs_init(void)
442 {
443         bt_debugfs = debugfs_create_dir("bluetooth", NULL);
444
445         bt_class = class_create(THIS_MODULE, "bluetooth");
446
447         return PTR_ERR_OR_ZERO(bt_class);
448 }
449
450 void bt_sysfs_cleanup(void)
451 {
452         class_destroy(bt_class);
453
454         debugfs_remove_recursive(bt_debugfs);
455 }