]> Git Repo - linux.git/blob - net/bluetooth/hci_debugfs.c
Bluetooth: Utilize %*ph specifier
[linux.git] / net / bluetooth / hci_debugfs.c
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3
4    Copyright (C) 2014 Intel Corporation
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License version 2 as
8    published by the Free Software Foundation;
9
10    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21    SOFTWARE IS DISCLAIMED.
22 */
23
24 #include <linux/debugfs.h>
25
26 #include <net/bluetooth/bluetooth.h>
27 #include <net/bluetooth/hci_core.h>
28
29 #include "hci_debugfs.h"
30
31 #define DEFINE_QUIRK_ATTRIBUTE(__name, __quirk)                               \
32 static ssize_t __name ## _read(struct file *file,                             \
33                                 char __user *user_buf,                        \
34                                 size_t count, loff_t *ppos)                   \
35 {                                                                             \
36         struct hci_dev *hdev = file->private_data;                            \
37         char buf[3];                                                          \
38                                                                               \
39         buf[0] = test_bit(__quirk, &hdev->quirks) ? 'Y' : 'N';                \
40         buf[1] = '\n';                                                        \
41         buf[2] = '\0';                                                        \
42         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);        \
43 }                                                                             \
44                                                                               \
45 static ssize_t __name ## _write(struct file *file,                            \
46                                  const char __user *user_buf,                 \
47                                  size_t count, loff_t *ppos)                  \
48 {                                                                             \
49         struct hci_dev *hdev = file->private_data;                            \
50         char buf[32];                                                         \
51         size_t buf_size = min(count, (sizeof(buf) - 1));                      \
52         bool enable;                                                          \
53                                                                               \
54         if (test_bit(HCI_UP, &hdev->flags))                                   \
55                 return -EBUSY;                                                \
56                                                                               \
57         if (copy_from_user(buf, user_buf, buf_size))                          \
58                 return -EFAULT;                                               \
59                                                                               \
60         buf[buf_size] = '\0';                                                 \
61         if (strtobool(buf, &enable))                                          \
62                 return -EINVAL;                                               \
63                                                                               \
64         if (enable == test_bit(__quirk, &hdev->quirks))                       \
65                 return -EALREADY;                                             \
66                                                                               \
67         change_bit(__quirk, &hdev->quirks);                                   \
68                                                                               \
69         return count;                                                         \
70 }                                                                             \
71                                                                               \
72 static const struct file_operations __name ## _fops = {                       \
73         .open           = simple_open,                                        \
74         .read           = __name ## _read,                                    \
75         .write          = __name ## _write,                                   \
76         .llseek         = default_llseek,                                     \
77 }                                                                             \
78
79 #define DEFINE_INFO_ATTRIBUTE(__name, __field)                                \
80 static int __name ## _show(struct seq_file *f, void *ptr)                     \
81 {                                                                             \
82         struct hci_dev *hdev = f->private;                                    \
83                                                                               \
84         hci_dev_lock(hdev);                                                   \
85         seq_printf(f, "%s\n", hdev->__field ? : "");                          \
86         hci_dev_unlock(hdev);                                                 \
87                                                                               \
88         return 0;                                                             \
89 }                                                                             \
90                                                                               \
91 static int __name ## _open(struct inode *inode, struct file *file)            \
92 {                                                                             \
93         return single_open(file, __name ## _show, inode->i_private);          \
94 }                                                                             \
95                                                                               \
96 static const struct file_operations __name ## _fops = {                       \
97         .open           = __name ## _open,                                    \
98         .read           = seq_read,                                           \
99         .llseek         = seq_lseek,                                          \
100         .release        = single_release,                                     \
101 }                                                                             \
102
103 static int features_show(struct seq_file *f, void *ptr)
104 {
105         struct hci_dev *hdev = f->private;
106         u8 p;
107
108         hci_dev_lock(hdev);
109         for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++)
110                 seq_printf(f, "%2u: %8ph\n", p, hdev->features[p]);
111         if (lmp_le_capable(hdev))
112                 seq_printf(f, "LE: %8ph\n", hdev->le_features);
113         hci_dev_unlock(hdev);
114
115         return 0;
116 }
117
118 static int features_open(struct inode *inode, struct file *file)
119 {
120         return single_open(file, features_show, inode->i_private);
121 }
122
123 static const struct file_operations features_fops = {
124         .open           = features_open,
125         .read           = seq_read,
126         .llseek         = seq_lseek,
127         .release        = single_release,
128 };
129
130 static int device_id_show(struct seq_file *f, void *ptr)
131 {
132         struct hci_dev *hdev = f->private;
133
134         hci_dev_lock(hdev);
135         seq_printf(f, "%4.4x:%4.4x:%4.4x:%4.4x\n", hdev->devid_source,
136                   hdev->devid_vendor, hdev->devid_product, hdev->devid_version);
137         hci_dev_unlock(hdev);
138
139         return 0;
140 }
141
142 static int device_id_open(struct inode *inode, struct file *file)
143 {
144         return single_open(file, device_id_show, inode->i_private);
145 }
146
147 static const struct file_operations device_id_fops = {
148         .open           = device_id_open,
149         .read           = seq_read,
150         .llseek         = seq_lseek,
151         .release        = single_release,
152 };
153
154 static int device_list_show(struct seq_file *f, void *ptr)
155 {
156         struct hci_dev *hdev = f->private;
157         struct hci_conn_params *p;
158         struct bdaddr_list *b;
159
160         hci_dev_lock(hdev);
161         list_for_each_entry(b, &hdev->whitelist, list)
162                 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
163         list_for_each_entry(p, &hdev->le_conn_params, list) {
164                 seq_printf(f, "%pMR (type %u) %u\n", &p->addr, p->addr_type,
165                            p->auto_connect);
166         }
167         hci_dev_unlock(hdev);
168
169         return 0;
170 }
171
172 static int device_list_open(struct inode *inode, struct file *file)
173 {
174         return single_open(file, device_list_show, inode->i_private);
175 }
176
177 static const struct file_operations device_list_fops = {
178         .open           = device_list_open,
179         .read           = seq_read,
180         .llseek         = seq_lseek,
181         .release        = single_release,
182 };
183
184 static int blacklist_show(struct seq_file *f, void *p)
185 {
186         struct hci_dev *hdev = f->private;
187         struct bdaddr_list *b;
188
189         hci_dev_lock(hdev);
190         list_for_each_entry(b, &hdev->blacklist, list)
191                 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
192         hci_dev_unlock(hdev);
193
194         return 0;
195 }
196
197 static int blacklist_open(struct inode *inode, struct file *file)
198 {
199         return single_open(file, blacklist_show, inode->i_private);
200 }
201
202 static const struct file_operations blacklist_fops = {
203         .open           = blacklist_open,
204         .read           = seq_read,
205         .llseek         = seq_lseek,
206         .release        = single_release,
207 };
208
209 static int uuids_show(struct seq_file *f, void *p)
210 {
211         struct hci_dev *hdev = f->private;
212         struct bt_uuid *uuid;
213
214         hci_dev_lock(hdev);
215         list_for_each_entry(uuid, &hdev->uuids, list) {
216                 u8 i, val[16];
217
218                 /* The Bluetooth UUID values are stored in big endian,
219                  * but with reversed byte order. So convert them into
220                  * the right order for the %pUb modifier.
221                  */
222                 for (i = 0; i < 16; i++)
223                         val[i] = uuid->uuid[15 - i];
224
225                 seq_printf(f, "%pUb\n", val);
226         }
227         hci_dev_unlock(hdev);
228
229        return 0;
230 }
231
232 static int uuids_open(struct inode *inode, struct file *file)
233 {
234         return single_open(file, uuids_show, inode->i_private);
235 }
236
237 static const struct file_operations uuids_fops = {
238         .open           = uuids_open,
239         .read           = seq_read,
240         .llseek         = seq_lseek,
241         .release        = single_release,
242 };
243
244 static int remote_oob_show(struct seq_file *f, void *ptr)
245 {
246         struct hci_dev *hdev = f->private;
247         struct oob_data *data;
248
249         hci_dev_lock(hdev);
250         list_for_each_entry(data, &hdev->remote_oob_data, list) {
251                 seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n",
252                            &data->bdaddr, data->bdaddr_type, data->present,
253                            16, data->hash192, 16, data->rand192,
254                            16, data->hash256, 16, data->rand256);
255         }
256         hci_dev_unlock(hdev);
257
258         return 0;
259 }
260
261 static int remote_oob_open(struct inode *inode, struct file *file)
262 {
263         return single_open(file, remote_oob_show, inode->i_private);
264 }
265
266 static const struct file_operations remote_oob_fops = {
267         .open           = remote_oob_open,
268         .read           = seq_read,
269         .llseek         = seq_lseek,
270         .release        = single_release,
271 };
272
273 static int conn_info_min_age_set(void *data, u64 val)
274 {
275         struct hci_dev *hdev = data;
276
277         if (val == 0 || val > hdev->conn_info_max_age)
278                 return -EINVAL;
279
280         hci_dev_lock(hdev);
281         hdev->conn_info_min_age = val;
282         hci_dev_unlock(hdev);
283
284         return 0;
285 }
286
287 static int conn_info_min_age_get(void *data, u64 *val)
288 {
289         struct hci_dev *hdev = data;
290
291         hci_dev_lock(hdev);
292         *val = hdev->conn_info_min_age;
293         hci_dev_unlock(hdev);
294
295         return 0;
296 }
297
298 DEFINE_SIMPLE_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
299                         conn_info_min_age_set, "%llu\n");
300
301 static int conn_info_max_age_set(void *data, u64 val)
302 {
303         struct hci_dev *hdev = data;
304
305         if (val == 0 || val < hdev->conn_info_min_age)
306                 return -EINVAL;
307
308         hci_dev_lock(hdev);
309         hdev->conn_info_max_age = val;
310         hci_dev_unlock(hdev);
311
312         return 0;
313 }
314
315 static int conn_info_max_age_get(void *data, u64 *val)
316 {
317         struct hci_dev *hdev = data;
318
319         hci_dev_lock(hdev);
320         *val = hdev->conn_info_max_age;
321         hci_dev_unlock(hdev);
322
323         return 0;
324 }
325
326 DEFINE_SIMPLE_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
327                         conn_info_max_age_set, "%llu\n");
328
329 static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
330                                    size_t count, loff_t *ppos)
331 {
332         struct hci_dev *hdev = file->private_data;
333         char buf[3];
334
335         buf[0] = hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS) ? 'Y': 'N';
336         buf[1] = '\n';
337         buf[2] = '\0';
338         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
339 }
340
341 static const struct file_operations use_debug_keys_fops = {
342         .open           = simple_open,
343         .read           = use_debug_keys_read,
344         .llseek         = default_llseek,
345 };
346
347 static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
348                                  size_t count, loff_t *ppos)
349 {
350         struct hci_dev *hdev = file->private_data;
351         char buf[3];
352
353         buf[0] = hci_dev_test_flag(hdev, HCI_SC_ONLY) ? 'Y': 'N';
354         buf[1] = '\n';
355         buf[2] = '\0';
356         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
357 }
358
359 static const struct file_operations sc_only_mode_fops = {
360         .open           = simple_open,
361         .read           = sc_only_mode_read,
362         .llseek         = default_llseek,
363 };
364
365 DEFINE_INFO_ATTRIBUTE(hardware_info, hw_info);
366 DEFINE_INFO_ATTRIBUTE(firmware_info, fw_info);
367
368 void hci_debugfs_create_common(struct hci_dev *hdev)
369 {
370         debugfs_create_file("features", 0444, hdev->debugfs, hdev,
371                             &features_fops);
372         debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
373                            &hdev->manufacturer);
374         debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
375         debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
376         debugfs_create_u8("hardware_error", 0444, hdev->debugfs,
377                           &hdev->hw_error_code);
378         debugfs_create_file("device_id", 0444, hdev->debugfs, hdev,
379                             &device_id_fops);
380
381         debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
382                             &device_list_fops);
383         debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
384                             &blacklist_fops);
385         debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
386         debugfs_create_file("remote_oob", 0400, hdev->debugfs, hdev,
387                             &remote_oob_fops);
388
389         debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev,
390                             &conn_info_min_age_fops);
391         debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev,
392                             &conn_info_max_age_fops);
393
394         if (lmp_ssp_capable(hdev) || lmp_le_capable(hdev))
395                 debugfs_create_file("use_debug_keys", 0444, hdev->debugfs,
396                                     hdev, &use_debug_keys_fops);
397
398         if (lmp_sc_capable(hdev) || lmp_le_capable(hdev))
399                 debugfs_create_file("sc_only_mode", 0444, hdev->debugfs,
400                                     hdev, &sc_only_mode_fops);
401
402         if (hdev->hw_info)
403                 debugfs_create_file("hardware_info", 0444, hdev->debugfs,
404                                     hdev, &hardware_info_fops);
405
406         if (hdev->fw_info)
407                 debugfs_create_file("firmware_info", 0444, hdev->debugfs,
408                                     hdev, &firmware_info_fops);
409 }
410
411 static int inquiry_cache_show(struct seq_file *f, void *p)
412 {
413         struct hci_dev *hdev = f->private;
414         struct discovery_state *cache = &hdev->discovery;
415         struct inquiry_entry *e;
416
417         hci_dev_lock(hdev);
418
419         list_for_each_entry(e, &cache->all, all) {
420                 struct inquiry_data *data = &e->data;
421                 seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
422                            &data->bdaddr,
423                            data->pscan_rep_mode, data->pscan_period_mode,
424                            data->pscan_mode, data->dev_class[2],
425                            data->dev_class[1], data->dev_class[0],
426                            __le16_to_cpu(data->clock_offset),
427                            data->rssi, data->ssp_mode, e->timestamp);
428         }
429
430         hci_dev_unlock(hdev);
431
432         return 0;
433 }
434
435 static int inquiry_cache_open(struct inode *inode, struct file *file)
436 {
437         return single_open(file, inquiry_cache_show, inode->i_private);
438 }
439
440 static const struct file_operations inquiry_cache_fops = {
441         .open           = inquiry_cache_open,
442         .read           = seq_read,
443         .llseek         = seq_lseek,
444         .release        = single_release,
445 };
446
447 static int link_keys_show(struct seq_file *f, void *ptr)
448 {
449         struct hci_dev *hdev = f->private;
450         struct link_key *key;
451
452         rcu_read_lock();
453         list_for_each_entry_rcu(key, &hdev->link_keys, list)
454                 seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
455                            HCI_LINK_KEY_SIZE, key->val, key->pin_len);
456         rcu_read_unlock();
457
458         return 0;
459 }
460
461 static int link_keys_open(struct inode *inode, struct file *file)
462 {
463         return single_open(file, link_keys_show, inode->i_private);
464 }
465
466 static const struct file_operations link_keys_fops = {
467         .open           = link_keys_open,
468         .read           = seq_read,
469         .llseek         = seq_lseek,
470         .release        = single_release,
471 };
472
473 static int dev_class_show(struct seq_file *f, void *ptr)
474 {
475         struct hci_dev *hdev = f->private;
476
477         hci_dev_lock(hdev);
478         seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
479                    hdev->dev_class[1], hdev->dev_class[0]);
480         hci_dev_unlock(hdev);
481
482         return 0;
483 }
484
485 static int dev_class_open(struct inode *inode, struct file *file)
486 {
487         return single_open(file, dev_class_show, inode->i_private);
488 }
489
490 static const struct file_operations dev_class_fops = {
491         .open           = dev_class_open,
492         .read           = seq_read,
493         .llseek         = seq_lseek,
494         .release        = single_release,
495 };
496
497 static int voice_setting_get(void *data, u64 *val)
498 {
499         struct hci_dev *hdev = data;
500
501         hci_dev_lock(hdev);
502         *val = hdev->voice_setting;
503         hci_dev_unlock(hdev);
504
505         return 0;
506 }
507
508 DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get,
509                         NULL, "0x%4.4llx\n");
510
511 static ssize_t ssp_debug_mode_read(struct file *file, char __user *user_buf,
512                                    size_t count, loff_t *ppos)
513 {
514         struct hci_dev *hdev = file->private_data;
515         char buf[3];
516
517         buf[0] = hdev->ssp_debug_mode ? 'Y': 'N';
518         buf[1] = '\n';
519         buf[2] = '\0';
520         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
521 }
522
523 static const struct file_operations ssp_debug_mode_fops = {
524         .open           = simple_open,
525         .read           = ssp_debug_mode_read,
526         .llseek         = default_llseek,
527 };
528
529 static int auto_accept_delay_set(void *data, u64 val)
530 {
531         struct hci_dev *hdev = data;
532
533         hci_dev_lock(hdev);
534         hdev->auto_accept_delay = val;
535         hci_dev_unlock(hdev);
536
537         return 0;
538 }
539
540 static int auto_accept_delay_get(void *data, u64 *val)
541 {
542         struct hci_dev *hdev = data;
543
544         hci_dev_lock(hdev);
545         *val = hdev->auto_accept_delay;
546         hci_dev_unlock(hdev);
547
548         return 0;
549 }
550
551 DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
552                         auto_accept_delay_set, "%llu\n");
553
554 static int idle_timeout_set(void *data, u64 val)
555 {
556         struct hci_dev *hdev = data;
557
558         if (val != 0 && (val < 500 || val > 3600000))
559                 return -EINVAL;
560
561         hci_dev_lock(hdev);
562         hdev->idle_timeout = val;
563         hci_dev_unlock(hdev);
564
565         return 0;
566 }
567
568 static int idle_timeout_get(void *data, u64 *val)
569 {
570         struct hci_dev *hdev = data;
571
572         hci_dev_lock(hdev);
573         *val = hdev->idle_timeout;
574         hci_dev_unlock(hdev);
575
576         return 0;
577 }
578
579 DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
580                         idle_timeout_set, "%llu\n");
581
582 static int sniff_min_interval_set(void *data, u64 val)
583 {
584         struct hci_dev *hdev = data;
585
586         if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
587                 return -EINVAL;
588
589         hci_dev_lock(hdev);
590         hdev->sniff_min_interval = val;
591         hci_dev_unlock(hdev);
592
593         return 0;
594 }
595
596 static int sniff_min_interval_get(void *data, u64 *val)
597 {
598         struct hci_dev *hdev = data;
599
600         hci_dev_lock(hdev);
601         *val = hdev->sniff_min_interval;
602         hci_dev_unlock(hdev);
603
604         return 0;
605 }
606
607 DEFINE_SIMPLE_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
608                         sniff_min_interval_set, "%llu\n");
609
610 static int sniff_max_interval_set(void *data, u64 val)
611 {
612         struct hci_dev *hdev = data;
613
614         if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
615                 return -EINVAL;
616
617         hci_dev_lock(hdev);
618         hdev->sniff_max_interval = val;
619         hci_dev_unlock(hdev);
620
621         return 0;
622 }
623
624 static int sniff_max_interval_get(void *data, u64 *val)
625 {
626         struct hci_dev *hdev = data;
627
628         hci_dev_lock(hdev);
629         *val = hdev->sniff_max_interval;
630         hci_dev_unlock(hdev);
631
632         return 0;
633 }
634
635 DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
636                         sniff_max_interval_set, "%llu\n");
637
638 void hci_debugfs_create_bredr(struct hci_dev *hdev)
639 {
640         debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, hdev,
641                             &inquiry_cache_fops);
642         debugfs_create_file("link_keys", 0400, hdev->debugfs, hdev,
643                             &link_keys_fops);
644         debugfs_create_file("dev_class", 0444, hdev->debugfs, hdev,
645                             &dev_class_fops);
646         debugfs_create_file("voice_setting", 0444, hdev->debugfs, hdev,
647                             &voice_setting_fops);
648
649         if (lmp_ssp_capable(hdev)) {
650                 debugfs_create_file("ssp_debug_mode", 0444, hdev->debugfs,
651                                     hdev, &ssp_debug_mode_fops);
652                 debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
653                                     hdev, &auto_accept_delay_fops);
654         }
655
656         if (lmp_sniff_capable(hdev)) {
657                 debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
658                                     hdev, &idle_timeout_fops);
659                 debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
660                                     hdev, &sniff_min_interval_fops);
661                 debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
662                                     hdev, &sniff_max_interval_fops);
663         }
664 }
665
666 static int identity_show(struct seq_file *f, void *p)
667 {
668         struct hci_dev *hdev = f->private;
669         bdaddr_t addr;
670         u8 addr_type;
671
672         hci_dev_lock(hdev);
673
674         hci_copy_identity_address(hdev, &addr, &addr_type);
675
676         seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,
677                    16, hdev->irk, &hdev->rpa);
678
679         hci_dev_unlock(hdev);
680
681         return 0;
682 }
683
684 static int identity_open(struct inode *inode, struct file *file)
685 {
686         return single_open(file, identity_show, inode->i_private);
687 }
688
689 static const struct file_operations identity_fops = {
690         .open           = identity_open,
691         .read           = seq_read,
692         .llseek         = seq_lseek,
693         .release        = single_release,
694 };
695
696 static int rpa_timeout_set(void *data, u64 val)
697 {
698         struct hci_dev *hdev = data;
699
700         /* Require the RPA timeout to be at least 30 seconds and at most
701          * 24 hours.
702          */
703         if (val < 30 || val > (60 * 60 * 24))
704                 return -EINVAL;
705
706         hci_dev_lock(hdev);
707         hdev->rpa_timeout = val;
708         hci_dev_unlock(hdev);
709
710         return 0;
711 }
712
713 static int rpa_timeout_get(void *data, u64 *val)
714 {
715         struct hci_dev *hdev = data;
716
717         hci_dev_lock(hdev);
718         *val = hdev->rpa_timeout;
719         hci_dev_unlock(hdev);
720
721         return 0;
722 }
723
724 DEFINE_SIMPLE_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
725                         rpa_timeout_set, "%llu\n");
726
727 static int random_address_show(struct seq_file *f, void *p)
728 {
729         struct hci_dev *hdev = f->private;
730
731         hci_dev_lock(hdev);
732         seq_printf(f, "%pMR\n", &hdev->random_addr);
733         hci_dev_unlock(hdev);
734
735         return 0;
736 }
737
738 static int random_address_open(struct inode *inode, struct file *file)
739 {
740         return single_open(file, random_address_show, inode->i_private);
741 }
742
743 static const struct file_operations random_address_fops = {
744         .open           = random_address_open,
745         .read           = seq_read,
746         .llseek         = seq_lseek,
747         .release        = single_release,
748 };
749
750 static int static_address_show(struct seq_file *f, void *p)
751 {
752         struct hci_dev *hdev = f->private;
753
754         hci_dev_lock(hdev);
755         seq_printf(f, "%pMR\n", &hdev->static_addr);
756         hci_dev_unlock(hdev);
757
758         return 0;
759 }
760
761 static int static_address_open(struct inode *inode, struct file *file)
762 {
763         return single_open(file, static_address_show, inode->i_private);
764 }
765
766 static const struct file_operations static_address_fops = {
767         .open           = static_address_open,
768         .read           = seq_read,
769         .llseek         = seq_lseek,
770         .release        = single_release,
771 };
772
773 static ssize_t force_static_address_read(struct file *file,
774                                          char __user *user_buf,
775                                          size_t count, loff_t *ppos)
776 {
777         struct hci_dev *hdev = file->private_data;
778         char buf[3];
779
780         buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ? 'Y': 'N';
781         buf[1] = '\n';
782         buf[2] = '\0';
783         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
784 }
785
786 static ssize_t force_static_address_write(struct file *file,
787                                           const char __user *user_buf,
788                                           size_t count, loff_t *ppos)
789 {
790         struct hci_dev *hdev = file->private_data;
791         char buf[32];
792         size_t buf_size = min(count, (sizeof(buf)-1));
793         bool enable;
794
795         if (test_bit(HCI_UP, &hdev->flags))
796                 return -EBUSY;
797
798         if (copy_from_user(buf, user_buf, buf_size))
799                 return -EFAULT;
800
801         buf[buf_size] = '\0';
802         if (strtobool(buf, &enable))
803                 return -EINVAL;
804
805         if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
806                 return -EALREADY;
807
808         hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
809
810         return count;
811 }
812
813 static const struct file_operations force_static_address_fops = {
814         .open           = simple_open,
815         .read           = force_static_address_read,
816         .write          = force_static_address_write,
817         .llseek         = default_llseek,
818 };
819
820 static int white_list_show(struct seq_file *f, void *ptr)
821 {
822         struct hci_dev *hdev = f->private;
823         struct bdaddr_list *b;
824
825         hci_dev_lock(hdev);
826         list_for_each_entry(b, &hdev->le_white_list, list)
827                 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
828         hci_dev_unlock(hdev);
829
830         return 0;
831 }
832
833 static int white_list_open(struct inode *inode, struct file *file)
834 {
835         return single_open(file, white_list_show, inode->i_private);
836 }
837
838 static const struct file_operations white_list_fops = {
839         .open           = white_list_open,
840         .read           = seq_read,
841         .llseek         = seq_lseek,
842         .release        = single_release,
843 };
844
845 static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
846 {
847         struct hci_dev *hdev = f->private;
848         struct smp_irk *irk;
849
850         rcu_read_lock();
851         list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
852                 seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
853                            &irk->bdaddr, irk->addr_type,
854                            16, irk->val, &irk->rpa);
855         }
856         rcu_read_unlock();
857
858         return 0;
859 }
860
861 static int identity_resolving_keys_open(struct inode *inode, struct file *file)
862 {
863         return single_open(file, identity_resolving_keys_show,
864                            inode->i_private);
865 }
866
867 static const struct file_operations identity_resolving_keys_fops = {
868         .open           = identity_resolving_keys_open,
869         .read           = seq_read,
870         .llseek         = seq_lseek,
871         .release        = single_release,
872 };
873
874 static int long_term_keys_show(struct seq_file *f, void *ptr)
875 {
876         struct hci_dev *hdev = f->private;
877         struct smp_ltk *ltk;
878
879         rcu_read_lock();
880         list_for_each_entry_rcu(ltk, &hdev->long_term_keys, list)
881                 seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
882                            &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
883                            ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
884                            __le64_to_cpu(ltk->rand), 16, ltk->val);
885         rcu_read_unlock();
886
887         return 0;
888 }
889
890 static int long_term_keys_open(struct inode *inode, struct file *file)
891 {
892         return single_open(file, long_term_keys_show, inode->i_private);
893 }
894
895 static const struct file_operations long_term_keys_fops = {
896         .open           = long_term_keys_open,
897         .read           = seq_read,
898         .llseek         = seq_lseek,
899         .release        = single_release,
900 };
901
902 static int conn_min_interval_set(void *data, u64 val)
903 {
904         struct hci_dev *hdev = data;
905
906         if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval)
907                 return -EINVAL;
908
909         hci_dev_lock(hdev);
910         hdev->le_conn_min_interval = val;
911         hci_dev_unlock(hdev);
912
913         return 0;
914 }
915
916 static int conn_min_interval_get(void *data, u64 *val)
917 {
918         struct hci_dev *hdev = data;
919
920         hci_dev_lock(hdev);
921         *val = hdev->le_conn_min_interval;
922         hci_dev_unlock(hdev);
923
924         return 0;
925 }
926
927 DEFINE_SIMPLE_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
928                         conn_min_interval_set, "%llu\n");
929
930 static int conn_max_interval_set(void *data, u64 val)
931 {
932         struct hci_dev *hdev = data;
933
934         if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval)
935                 return -EINVAL;
936
937         hci_dev_lock(hdev);
938         hdev->le_conn_max_interval = val;
939         hci_dev_unlock(hdev);
940
941         return 0;
942 }
943
944 static int conn_max_interval_get(void *data, u64 *val)
945 {
946         struct hci_dev *hdev = data;
947
948         hci_dev_lock(hdev);
949         *val = hdev->le_conn_max_interval;
950         hci_dev_unlock(hdev);
951
952         return 0;
953 }
954
955 DEFINE_SIMPLE_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
956                         conn_max_interval_set, "%llu\n");
957
958 static int conn_latency_set(void *data, u64 val)
959 {
960         struct hci_dev *hdev = data;
961
962         if (val > 0x01f3)
963                 return -EINVAL;
964
965         hci_dev_lock(hdev);
966         hdev->le_conn_latency = val;
967         hci_dev_unlock(hdev);
968
969         return 0;
970 }
971
972 static int conn_latency_get(void *data, u64 *val)
973 {
974         struct hci_dev *hdev = data;
975
976         hci_dev_lock(hdev);
977         *val = hdev->le_conn_latency;
978         hci_dev_unlock(hdev);
979
980         return 0;
981 }
982
983 DEFINE_SIMPLE_ATTRIBUTE(conn_latency_fops, conn_latency_get,
984                         conn_latency_set, "%llu\n");
985
986 static int supervision_timeout_set(void *data, u64 val)
987 {
988         struct hci_dev *hdev = data;
989
990         if (val < 0x000a || val > 0x0c80)
991                 return -EINVAL;
992
993         hci_dev_lock(hdev);
994         hdev->le_supv_timeout = val;
995         hci_dev_unlock(hdev);
996
997         return 0;
998 }
999
1000 static int supervision_timeout_get(void *data, u64 *val)
1001 {
1002         struct hci_dev *hdev = data;
1003
1004         hci_dev_lock(hdev);
1005         *val = hdev->le_supv_timeout;
1006         hci_dev_unlock(hdev);
1007
1008         return 0;
1009 }
1010
1011 DEFINE_SIMPLE_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
1012                         supervision_timeout_set, "%llu\n");
1013
1014 static int adv_channel_map_set(void *data, u64 val)
1015 {
1016         struct hci_dev *hdev = data;
1017
1018         if (val < 0x01 || val > 0x07)
1019                 return -EINVAL;
1020
1021         hci_dev_lock(hdev);
1022         hdev->le_adv_channel_map = val;
1023         hci_dev_unlock(hdev);
1024
1025         return 0;
1026 }
1027
1028 static int adv_channel_map_get(void *data, u64 *val)
1029 {
1030         struct hci_dev *hdev = data;
1031
1032         hci_dev_lock(hdev);
1033         *val = hdev->le_adv_channel_map;
1034         hci_dev_unlock(hdev);
1035
1036         return 0;
1037 }
1038
1039 DEFINE_SIMPLE_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
1040                         adv_channel_map_set, "%llu\n");
1041
1042 static int adv_min_interval_set(void *data, u64 val)
1043 {
1044         struct hci_dev *hdev = data;
1045
1046         if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval)
1047                 return -EINVAL;
1048
1049         hci_dev_lock(hdev);
1050         hdev->le_adv_min_interval = val;
1051         hci_dev_unlock(hdev);
1052
1053         return 0;
1054 }
1055
1056 static int adv_min_interval_get(void *data, u64 *val)
1057 {
1058         struct hci_dev *hdev = data;
1059
1060         hci_dev_lock(hdev);
1061         *val = hdev->le_adv_min_interval;
1062         hci_dev_unlock(hdev);
1063
1064         return 0;
1065 }
1066
1067 DEFINE_SIMPLE_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
1068                         adv_min_interval_set, "%llu\n");
1069
1070 static int adv_max_interval_set(void *data, u64 val)
1071 {
1072         struct hci_dev *hdev = data;
1073
1074         if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval)
1075                 return -EINVAL;
1076
1077         hci_dev_lock(hdev);
1078         hdev->le_adv_max_interval = val;
1079         hci_dev_unlock(hdev);
1080
1081         return 0;
1082 }
1083
1084 static int adv_max_interval_get(void *data, u64 *val)
1085 {
1086         struct hci_dev *hdev = data;
1087
1088         hci_dev_lock(hdev);
1089         *val = hdev->le_adv_max_interval;
1090         hci_dev_unlock(hdev);
1091
1092         return 0;
1093 }
1094
1095 DEFINE_SIMPLE_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
1096                         adv_max_interval_set, "%llu\n");
1097
1098 DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1099                        HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1100 DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1101                        HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1102
1103 void hci_debugfs_create_le(struct hci_dev *hdev)
1104 {
1105         debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
1106                             &identity_fops);
1107         debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
1108                             &rpa_timeout_fops);
1109         debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
1110                             &random_address_fops);
1111         debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
1112                             &static_address_fops);
1113
1114         /* For controllers with a public address, provide a debug
1115          * option to force the usage of the configured static
1116          * address. By default the public address is used.
1117          */
1118         if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1119                 debugfs_create_file("force_static_address", 0644,
1120                                     hdev->debugfs, hdev,
1121                                     &force_static_address_fops);
1122
1123         debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1124                           &hdev->le_white_list_size);
1125         debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1126                             &white_list_fops);
1127         debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
1128                             hdev, &identity_resolving_keys_fops);
1129         debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
1130                             &long_term_keys_fops);
1131         debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
1132                             &conn_min_interval_fops);
1133         debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
1134                             &conn_max_interval_fops);
1135         debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
1136                             &conn_latency_fops);
1137         debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
1138                             &supervision_timeout_fops);
1139         debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
1140                             &adv_channel_map_fops);
1141         debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
1142                             &adv_min_interval_fops);
1143         debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
1144                             &adv_max_interval_fops);
1145         debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1146                            &hdev->discov_interleaved_timeout);
1147
1148         debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1149                             hdev->debugfs, hdev,
1150                             &quirk_strict_duplicate_filter_fops);
1151         debugfs_create_file("quirk_simultaneous_discovery", 0644,
1152                             hdev->debugfs, hdev,
1153                             &quirk_simultaneous_discovery_fops);
1154 }
1155
1156 void hci_debugfs_create_conn(struct hci_conn *conn)
1157 {
1158         struct hci_dev *hdev = conn->hdev;
1159         char name[6];
1160
1161         if (IS_ERR_OR_NULL(hdev->debugfs))
1162                 return;
1163
1164         snprintf(name, sizeof(name), "%u", conn->handle);
1165         conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
1166 }
This page took 0.093308 seconds and 4 git commands to generate.