projects/linux·blob master · refs

1388 lines · 33.0 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 BlueZ - Bluetooth protocol stack for Linux
4
5 Copyright (C) 2014 Intel Corporation
6
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
10 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
11 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
17 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
18 SOFTWARE IS DISCLAIMED.
19*/
20
21#include <linux/debugfs.h>
22#include <linux/kstrtox.h>
23
24#include <net/bluetooth/bluetooth.h>
25#include <net/bluetooth/hci_core.h>
26
27#include "smp.h"
28#include "hci_debugfs.h"
29
30#define DEFINE_QUIRK_ATTRIBUTE(__name, __quirk) \
31static ssize_t __name ## _read(struct file *file, \
32 char __user *user_buf, \
33 size_t count, loff_t *ppos) \
34{ \
35 struct hci_dev *hdev = file->private_data; \
36 char buf[3]; \
37 \
38 buf[0] = test_bit(__quirk, hdev->quirk_flags) ? 'Y' : 'N'; \
39 buf[1] = '\n'; \
40 buf[2] = '\0'; \
41 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); \
42} \
43 \
44static ssize_t __name ## _write(struct file *file, \
45 const char __user *user_buf, \
46 size_t count, loff_t *ppos) \
47{ \
48 struct hci_dev *hdev = file->private_data; \
49 bool enable; \
50 int err; \
51 \
52 if (test_bit(HCI_UP, &hdev->flags)) \
53 return -EBUSY; \
54 \
55 err = kstrtobool_from_user(user_buf, count, &enable); \
56 if (err) \
57 return err; \
58 \
59 if (enable == test_bit(__quirk, hdev->quirk_flags)) \
60 return -EALREADY; \
61 \
62 change_bit(__quirk, hdev->quirk_flags); \
63 \
64 return count; \
65} \
66 \
67static const struct file_operations __name ## _fops = { \
68 .open = simple_open, \
69 .read = __name ## _read, \
70 .write = __name ## _write, \
71 .llseek = default_llseek, \
72} \
73
74#define DEFINE_INFO_ATTRIBUTE(__name, __field) \
75static int __name ## _show(struct seq_file *f, void *ptr) \
76{ \
77 struct hci_dev *hdev = f->private; \
78 \
79 hci_dev_lock(hdev); \
80 seq_printf(f, "%s\n", hdev->__field ? : ""); \
81 hci_dev_unlock(hdev); \
82 \
83 return 0; \
84} \
85 \
86DEFINE_SHOW_ATTRIBUTE(__name)
87
88static int features_show(struct seq_file *f, void *ptr)
89{
90 struct hci_dev *hdev = f->private;
91 u8 p;
92
93 hci_dev_lock(hdev);
94 for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++)
95 seq_printf(f, "%2u: %8ph\n", p, hdev->features[p]);
96 if (lmp_le_capable(hdev))
97 seq_printf(f, "LE: %8ph\n", hdev->le_features);
98 hci_dev_unlock(hdev);
99
100 return 0;
101}
102
103DEFINE_SHOW_ATTRIBUTE(features);
104
105static int device_id_show(struct seq_file *f, void *ptr)
106{
107 struct hci_dev *hdev = f->private;
108
109 hci_dev_lock(hdev);
110 seq_printf(f, "%4.4x:%4.4x:%4.4x:%4.4x\n", hdev->devid_source,
111 hdev->devid_vendor, hdev->devid_product, hdev->devid_version);
112 hci_dev_unlock(hdev);
113
114 return 0;
115}
116
117DEFINE_SHOW_ATTRIBUTE(device_id);
118
119static int device_list_show(struct seq_file *f, void *ptr)
120{
121 struct hci_dev *hdev = f->private;
122 struct hci_conn_params *p;
123 struct bdaddr_list *b;
124
125 hci_dev_lock(hdev);
126 list_for_each_entry(b, &hdev->accept_list, list)
127 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
128 list_for_each_entry(p, &hdev->le_conn_params, list) {
129 seq_printf(f, "%pMR (type %u) %u\n", &p->addr, p->addr_type,
130 p->auto_connect);
131 }
132 hci_dev_unlock(hdev);
133
134 return 0;
135}
136
137DEFINE_SHOW_ATTRIBUTE(device_list);
138
139static int blacklist_show(struct seq_file *f, void *p)
140{
141 struct hci_dev *hdev = f->private;
142 struct bdaddr_list *b;
143
144 hci_dev_lock(hdev);
145 list_for_each_entry(b, &hdev->reject_list, list)
146 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
147 hci_dev_unlock(hdev);
148
149 return 0;
150}
151
152DEFINE_SHOW_ATTRIBUTE(blacklist);
153
154static int blocked_keys_show(struct seq_file *f, void *p)
155{
156 struct hci_dev *hdev = f->private;
157 struct blocked_key *key;
158
159 rcu_read_lock();
160 list_for_each_entry_rcu(key, &hdev->blocked_keys, list)
161 seq_printf(f, "%u %*phN\n", key->type, 16, key->val);
162 rcu_read_unlock();
163
164 return 0;
165}
166
167DEFINE_SHOW_ATTRIBUTE(blocked_keys);
168
169static int uuids_show(struct seq_file *f, void *p)
170{
171 struct hci_dev *hdev = f->private;
172 struct bt_uuid *uuid;
173
174 hci_dev_lock(hdev);
175 list_for_each_entry(uuid, &hdev->uuids, list) {
176 u8 i, val[16];
177
178 /* The Bluetooth UUID values are stored in big endian,
179 * but with reversed byte order. So convert them into
180 * the right order for the %pUb modifier.
181 */
182 for (i = 0; i < 16; i++)
183 val[i] = uuid->uuid[15 - i];
184
185 seq_printf(f, "%pUb\n", val);
186 }
187 hci_dev_unlock(hdev);
188
189 return 0;
190}
191
192DEFINE_SHOW_ATTRIBUTE(uuids);
193
194static int remote_oob_show(struct seq_file *f, void *ptr)
195{
196 struct hci_dev *hdev = f->private;
197 struct oob_data *data;
198
199 hci_dev_lock(hdev);
200 list_for_each_entry(data, &hdev->remote_oob_data, list) {
201 seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n",
202 &data->bdaddr, data->bdaddr_type, data->present,
203 16, data->hash192, 16, data->rand192,
204 16, data->hash256, 16, data->rand256);
205 }
206 hci_dev_unlock(hdev);
207
208 return 0;
209}
210
211DEFINE_SHOW_ATTRIBUTE(remote_oob);
212
213static int conn_info_min_age_set(void *data, u64 val)
214{
215 struct hci_dev *hdev = data;
216
217 hci_dev_lock(hdev);
218 if (val == 0 || val > hdev->conn_info_max_age) {
219 hci_dev_unlock(hdev);
220 return -EINVAL;
221 }
222
223 hdev->conn_info_min_age = val;
224 hci_dev_unlock(hdev);
225
226 return 0;
227}
228
229static int conn_info_min_age_get(void *data, u64 *val)
230{
231 struct hci_dev *hdev = data;
232
233 hci_dev_lock(hdev);
234 *val = hdev->conn_info_min_age;
235 hci_dev_unlock(hdev);
236
237 return 0;
238}
239
240DEFINE_DEBUGFS_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
241 conn_info_min_age_set, "%llu\n");
242
243static int conn_info_max_age_set(void *data, u64 val)
244{
245 struct hci_dev *hdev = data;
246
247 hci_dev_lock(hdev);
248 if (val == 0 || val < hdev->conn_info_min_age) {
249 hci_dev_unlock(hdev);
250 return -EINVAL;
251 }
252
253 hdev->conn_info_max_age = val;
254 hci_dev_unlock(hdev);
255
256 return 0;
257}
258
259static int conn_info_max_age_get(void *data, u64 *val)
260{
261 struct hci_dev *hdev = data;
262
263 hci_dev_lock(hdev);
264 *val = hdev->conn_info_max_age;
265 hci_dev_unlock(hdev);
266
267 return 0;
268}
269
270DEFINE_DEBUGFS_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
271 conn_info_max_age_set, "%llu\n");
272
273static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
274 size_t count, loff_t *ppos)
275{
276 struct hci_dev *hdev = file->private_data;
277 char buf[3];
278
279 buf[0] = hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS) ? 'Y' : 'N';
280 buf[1] = '\n';
281 buf[2] = '\0';
282 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
283}
284
285static const struct file_operations use_debug_keys_fops = {
286 .open = simple_open,
287 .read = use_debug_keys_read,
288 .llseek = default_llseek,
289};
290
291static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
292 size_t count, loff_t *ppos)
293{
294 struct hci_dev *hdev = file->private_data;
295 char buf[3];
296
297 buf[0] = hci_dev_test_flag(hdev, HCI_SC_ONLY) ? 'Y' : 'N';
298 buf[1] = '\n';
299 buf[2] = '\0';
300 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
301}
302
303static const struct file_operations sc_only_mode_fops = {
304 .open = simple_open,
305 .read = sc_only_mode_read,
306 .llseek = default_llseek,
307};
308
309DEFINE_INFO_ATTRIBUTE(hardware_info, hw_info);
310DEFINE_INFO_ATTRIBUTE(firmware_info, fw_info);
311
312void hci_debugfs_create_common(struct hci_dev *hdev)
313{
314 debugfs_create_file("features", 0444, hdev->debugfs, hdev,
315 &features_fops);
316 debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
317 &hdev->manufacturer);
318 debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
319 debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
320 debugfs_create_u8("hardware_error", 0444, hdev->debugfs,
321 &hdev->hw_error_code);
322 debugfs_create_file("device_id", 0444, hdev->debugfs, hdev,
323 &device_id_fops);
324
325 debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
326 &device_list_fops);
327 debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
328 &blacklist_fops);
329 debugfs_create_file("blocked_keys", 0444, hdev->debugfs, hdev,
330 &blocked_keys_fops);
331 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
332 debugfs_create_file("remote_oob", 0400, hdev->debugfs, hdev,
333 &remote_oob_fops);
334
335 debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev,
336 &conn_info_min_age_fops);
337 debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev,
338 &conn_info_max_age_fops);
339
340 if (lmp_ssp_capable(hdev) || lmp_le_capable(hdev))
341 debugfs_create_file("use_debug_keys", 0444, hdev->debugfs,
342 hdev, &use_debug_keys_fops);
343
344 if (lmp_sc_capable(hdev) || lmp_le_capable(hdev))
345 debugfs_create_file("sc_only_mode", 0444, hdev->debugfs,
346 hdev, &sc_only_mode_fops);
347
348 if (hdev->hw_info)
349 debugfs_create_file("hardware_info", 0444, hdev->debugfs,
350 hdev, &hardware_info_fops);
351
352 if (hdev->fw_info)
353 debugfs_create_file("firmware_info", 0444, hdev->debugfs,
354 hdev, &firmware_info_fops);
355}
356
357static int inquiry_cache_show(struct seq_file *f, void *p)
358{
359 struct hci_dev *hdev = f->private;
360 struct discovery_state *cache = &hdev->discovery;
361 struct inquiry_entry *e;
362
363 hci_dev_lock(hdev);
364
365 list_for_each_entry(e, &cache->all, all) {
366 struct inquiry_data *data = &e->data;
367 seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %lu\n",
368 &data->bdaddr,
369 data->pscan_rep_mode, data->pscan_period_mode,
370 data->pscan_mode, data->dev_class[2],
371 data->dev_class[1], data->dev_class[0],
372 __le16_to_cpu(data->clock_offset),
373 data->rssi, data->ssp_mode, e->timestamp);
374 }
375
376 hci_dev_unlock(hdev);
377
378 return 0;
379}
380
381DEFINE_SHOW_ATTRIBUTE(inquiry_cache);
382
383static int link_keys_show(struct seq_file *f, void *ptr)
384{
385 struct hci_dev *hdev = f->private;
386 struct link_key *key;
387
388 rcu_read_lock();
389 list_for_each_entry_rcu(key, &hdev->link_keys, list)
390 seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
391 HCI_LINK_KEY_SIZE, key->val, key->pin_len);
392 rcu_read_unlock();
393
394 return 0;
395}
396
397DEFINE_SHOW_ATTRIBUTE(link_keys);
398
399static int dev_class_show(struct seq_file *f, void *ptr)
400{
401 struct hci_dev *hdev = f->private;
402
403 hci_dev_lock(hdev);
404 seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
405 hdev->dev_class[1], hdev->dev_class[0]);
406 hci_dev_unlock(hdev);
407
408 return 0;
409}
410
411DEFINE_SHOW_ATTRIBUTE(dev_class);
412
413static int voice_setting_get(void *data, u64 *val)
414{
415 struct hci_dev *hdev = data;
416
417 hci_dev_lock(hdev);
418 *val = hdev->voice_setting;
419 hci_dev_unlock(hdev);
420
421 return 0;
422}
423
424DEFINE_DEBUGFS_ATTRIBUTE(voice_setting_fops, voice_setting_get,
425 NULL, "0x%4.4llx\n");
426
427static ssize_t ssp_debug_mode_read(struct file *file, char __user *user_buf,
428 size_t count, loff_t *ppos)
429{
430 struct hci_dev *hdev = file->private_data;
431 char buf[3];
432
433 buf[0] = hdev->ssp_debug_mode ? 'Y' : 'N';
434 buf[1] = '\n';
435 buf[2] = '\0';
436 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
437}
438
439static const struct file_operations ssp_debug_mode_fops = {
440 .open = simple_open,
441 .read = ssp_debug_mode_read,
442 .llseek = default_llseek,
443};
444
445static int auto_accept_delay_set(void *data, u64 val)
446{
447 struct hci_dev *hdev = data;
448
449 hci_dev_lock(hdev);
450 hdev->auto_accept_delay = val;
451 hci_dev_unlock(hdev);
452
453 return 0;
454}
455
456static int min_encrypt_key_size_set(void *data, u64 val)
457{
458 struct hci_dev *hdev = data;
459
460 if (val < 1 || val > 16)
461 return -EINVAL;
462
463 hci_dev_lock(hdev);
464 hdev->min_enc_key_size = val;
465 hci_dev_unlock(hdev);
466
467 return 0;
468}
469
470static int min_encrypt_key_size_get(void *data, u64 *val)
471{
472 struct hci_dev *hdev = data;
473
474 hci_dev_lock(hdev);
475 *val = hdev->min_enc_key_size;
476 hci_dev_unlock(hdev);
477
478 return 0;
479}
480
481DEFINE_DEBUGFS_ATTRIBUTE(min_encrypt_key_size_fops,
482 min_encrypt_key_size_get,
483 min_encrypt_key_size_set, "%llu\n");
484
485static int auto_accept_delay_get(void *data, u64 *val)
486{
487 struct hci_dev *hdev = data;
488
489 hci_dev_lock(hdev);
490 *val = hdev->auto_accept_delay;
491 hci_dev_unlock(hdev);
492
493 return 0;
494}
495
496DEFINE_DEBUGFS_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
497 auto_accept_delay_set, "%llu\n");
498
499static ssize_t force_bredr_smp_read(struct file *file,
500 char __user *user_buf,
501 size_t count, loff_t *ppos)
502{
503 struct hci_dev *hdev = file->private_data;
504 char buf[3];
505
506 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y' : 'N';
507 buf[1] = '\n';
508 buf[2] = '\0';
509 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
510}
511
512static ssize_t force_bredr_smp_write(struct file *file,
513 const char __user *user_buf,
514 size_t count, loff_t *ppos)
515{
516 struct hci_dev *hdev = file->private_data;
517 bool enable;
518 int err;
519
520 err = kstrtobool_from_user(user_buf, count, &enable);
521 if (err)
522 return err;
523
524 err = smp_force_bredr(hdev, enable);
525 if (err)
526 return err;
527
528 return count;
529}
530
531static const struct file_operations force_bredr_smp_fops = {
532 .open = simple_open,
533 .read = force_bredr_smp_read,
534 .write = force_bredr_smp_write,
535 .llseek = default_llseek,
536};
537
538static int idle_timeout_set(void *data, u64 val)
539{
540 struct hci_dev *hdev = data;
541
542 if (val != 0 && (val < 500 || val > 3600000))
543 return -EINVAL;
544
545 hci_dev_lock(hdev);
546 hdev->idle_timeout = val;
547 hci_dev_unlock(hdev);
548
549 return 0;
550}
551
552static int idle_timeout_get(void *data, u64 *val)
553{
554 struct hci_dev *hdev = data;
555
556 hci_dev_lock(hdev);
557 *val = hdev->idle_timeout;
558 hci_dev_unlock(hdev);
559
560 return 0;
561}
562
563DEFINE_DEBUGFS_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
564 idle_timeout_set, "%llu\n");
565
566static int sniff_min_interval_set(void *data, u64 val)
567{
568 struct hci_dev *hdev = data;
569
570 hci_dev_lock(hdev);
571 if (val == 0 || val % 2 || val > hdev->sniff_max_interval) {
572 hci_dev_unlock(hdev);
573 return -EINVAL;
574 }
575
576 hdev->sniff_min_interval = val;
577 hci_dev_unlock(hdev);
578
579 return 0;
580}
581
582static int sniff_min_interval_get(void *data, u64 *val)
583{
584 struct hci_dev *hdev = data;
585
586 hci_dev_lock(hdev);
587 *val = hdev->sniff_min_interval;
588 hci_dev_unlock(hdev);
589
590 return 0;
591}
592
593DEFINE_DEBUGFS_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
594 sniff_min_interval_set, "%llu\n");
595
596static int sniff_max_interval_set(void *data, u64 val)
597{
598 struct hci_dev *hdev = data;
599
600 hci_dev_lock(hdev);
601 if (val == 0 || val % 2 || val < hdev->sniff_min_interval) {
602 hci_dev_unlock(hdev);
603 return -EINVAL;
604 }
605
606 hdev->sniff_max_interval = val;
607 hci_dev_unlock(hdev);
608
609 return 0;
610}
611
612static int sniff_max_interval_get(void *data, u64 *val)
613{
614 struct hci_dev *hdev = data;
615
616 hci_dev_lock(hdev);
617 *val = hdev->sniff_max_interval;
618 hci_dev_unlock(hdev);
619
620 return 0;
621}
622
623DEFINE_DEBUGFS_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
624 sniff_max_interval_set, "%llu\n");
625
626void hci_debugfs_create_bredr(struct hci_dev *hdev)
627{
628 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, hdev,
629 &inquiry_cache_fops);
630 debugfs_create_file("link_keys", 0400, hdev->debugfs, hdev,
631 &link_keys_fops);
632 debugfs_create_file("dev_class", 0444, hdev->debugfs, hdev,
633 &dev_class_fops);
634 debugfs_create_file("voice_setting", 0444, hdev->debugfs, hdev,
635 &voice_setting_fops);
636
637 /* If the controller does not support BR/EDR Secure Connections
638 * feature, then the BR/EDR SMP channel shall not be present.
639 *
640 * To test this with Bluetooth 4.0 controllers, create a debugfs
641 * switch that allows forcing BR/EDR SMP support and accepting
642 * cross-transport pairing on non-AES encrypted connections.
643 */
644 if (!lmp_sc_capable(hdev))
645 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
646 hdev, &force_bredr_smp_fops);
647
648 if (lmp_ssp_capable(hdev)) {
649 debugfs_create_file("ssp_debug_mode", 0444, hdev->debugfs,
650 hdev, &ssp_debug_mode_fops);
651 debugfs_create_file("min_encrypt_key_size", 0644, hdev->debugfs,
652 hdev, &min_encrypt_key_size_fops);
653 debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
654 hdev, &auto_accept_delay_fops);
655 }
656
657 if (lmp_sniff_capable(hdev)) {
658 debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
659 hdev, &idle_timeout_fops);
660 debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
661 hdev, &sniff_min_interval_fops);
662 debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
663 hdev, &sniff_max_interval_fops);
664 }
665}
666
667static int identity_show(struct seq_file *f, void *p)
668{
669 struct hci_dev *hdev = f->private;
670 bdaddr_t addr;
671 u8 addr_type;
672
673 hci_dev_lock(hdev);
674
675 hci_copy_identity_address(hdev, &addr, &addr_type);
676
677 seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,
678 16, hdev->irk, &hdev->rpa);
679
680 hci_dev_unlock(hdev);
681
682 return 0;
683}
684
685DEFINE_SHOW_ATTRIBUTE(identity);
686
687static int rpa_timeout_set(void *data, u64 val)
688{
689 struct hci_dev *hdev = data;
690
691 /* Require the RPA timeout to be at least 30 seconds and at most
692 * 24 hours.
693 */
694 if (val < 30 || val > (60 * 60 * 24))
695 return -EINVAL;
696
697 hci_dev_lock(hdev);
698 hdev->rpa_timeout = val;
699 hci_dev_unlock(hdev);
700
701 return 0;
702}
703
704static int rpa_timeout_get(void *data, u64 *val)
705{
706 struct hci_dev *hdev = data;
707
708 hci_dev_lock(hdev);
709 *val = hdev->rpa_timeout;
710 hci_dev_unlock(hdev);
711
712 return 0;
713}
714
715DEFINE_DEBUGFS_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
716 rpa_timeout_set, "%llu\n");
717
718static int random_address_show(struct seq_file *f, void *p)
719{
720 struct hci_dev *hdev = f->private;
721
722 hci_dev_lock(hdev);
723 seq_printf(f, "%pMR\n", &hdev->random_addr);
724 hci_dev_unlock(hdev);
725
726 return 0;
727}
728
729DEFINE_SHOW_ATTRIBUTE(random_address);
730
731static int static_address_show(struct seq_file *f, void *p)
732{
733 struct hci_dev *hdev = f->private;
734
735 hci_dev_lock(hdev);
736 seq_printf(f, "%pMR\n", &hdev->static_addr);
737 hci_dev_unlock(hdev);
738
739 return 0;
740}
741
742DEFINE_SHOW_ATTRIBUTE(static_address);
743
744static ssize_t force_static_address_read(struct file *file,
745 char __user *user_buf,
746 size_t count, loff_t *ppos)
747{
748 struct hci_dev *hdev = file->private_data;
749 char buf[3];
750
751 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ? 'Y' : 'N';
752 buf[1] = '\n';
753 buf[2] = '\0';
754 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
755}
756
757static ssize_t force_static_address_write(struct file *file,
758 const char __user *user_buf,
759 size_t count, loff_t *ppos)
760{
761 struct hci_dev *hdev = file->private_data;
762 bool enable;
763 int err;
764
765 if (hdev_is_powered(hdev))
766 return -EBUSY;
767
768 err = kstrtobool_from_user(user_buf, count, &enable);
769 if (err)
770 return err;
771
772 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
773 return -EALREADY;
774
775 hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
776
777 return count;
778}
779
780static const struct file_operations force_static_address_fops = {
781 .open = simple_open,
782 .read = force_static_address_read,
783 .write = force_static_address_write,
784 .llseek = default_llseek,
785};
786
787static int white_list_show(struct seq_file *f, void *ptr)
788{
789 struct hci_dev *hdev = f->private;
790 struct bdaddr_list *b;
791
792 hci_dev_lock(hdev);
793 list_for_each_entry(b, &hdev->le_accept_list, list)
794 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
795 hci_dev_unlock(hdev);
796
797 return 0;
798}
799
800DEFINE_SHOW_ATTRIBUTE(white_list);
801
802static int resolv_list_show(struct seq_file *f, void *ptr)
803{
804 struct hci_dev *hdev = f->private;
805 struct bdaddr_list *b;
806
807 hci_dev_lock(hdev);
808 list_for_each_entry(b, &hdev->le_resolv_list, list)
809 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
810 hci_dev_unlock(hdev);
811
812 return 0;
813}
814
815DEFINE_SHOW_ATTRIBUTE(resolv_list);
816
817static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
818{
819 struct hci_dev *hdev = f->private;
820 struct smp_irk *irk;
821
822 rcu_read_lock();
823 list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
824 seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
825 &irk->bdaddr, irk->addr_type,
826 16, irk->val, &irk->rpa);
827 }
828 rcu_read_unlock();
829
830 return 0;
831}
832
833DEFINE_SHOW_ATTRIBUTE(identity_resolving_keys);
834
835static int long_term_keys_show(struct seq_file *f, void *ptr)
836{
837 struct hci_dev *hdev = f->private;
838 struct smp_ltk *ltk;
839
840 rcu_read_lock();
841 list_for_each_entry_rcu(ltk, &hdev->long_term_keys, list)
842 seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
843 &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
844 ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
845 __le64_to_cpu(ltk->rand), 16, ltk->val);
846 rcu_read_unlock();
847
848 return 0;
849}
850
851DEFINE_SHOW_ATTRIBUTE(long_term_keys);
852
853static int conn_min_interval_set(void *data, u64 val)
854{
855 struct hci_dev *hdev = data;
856
857 hci_dev_lock(hdev);
858 if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) {
859 hci_dev_unlock(hdev);
860 return -EINVAL;
861 }
862
863 hdev->le_conn_min_interval = val;
864 hci_dev_unlock(hdev);
865
866 return 0;
867}
868
869static int conn_min_interval_get(void *data, u64 *val)
870{
871 struct hci_dev *hdev = data;
872
873 hci_dev_lock(hdev);
874 *val = hdev->le_conn_min_interval;
875 hci_dev_unlock(hdev);
876
877 return 0;
878}
879
880DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
881 conn_min_interval_set, "%llu\n");
882
883static int conn_max_interval_set(void *data, u64 val)
884{
885 struct hci_dev *hdev = data;
886
887 hci_dev_lock(hdev);
888 if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) {
889 hci_dev_unlock(hdev);
890 return -EINVAL;
891 }
892
893 hdev->le_conn_max_interval = val;
894 hci_dev_unlock(hdev);
895
896 return 0;
897}
898
899static int conn_max_interval_get(void *data, u64 *val)
900{
901 struct hci_dev *hdev = data;
902
903 hci_dev_lock(hdev);
904 *val = hdev->le_conn_max_interval;
905 hci_dev_unlock(hdev);
906
907 return 0;
908}
909
910DEFINE_DEBUGFS_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
911 conn_max_interval_set, "%llu\n");
912
913static int conn_latency_set(void *data, u64 val)
914{
915 struct hci_dev *hdev = data;
916
917 if (val > 0x01f3)
918 return -EINVAL;
919
920 hci_dev_lock(hdev);
921 hdev->le_conn_latency = val;
922 hci_dev_unlock(hdev);
923
924 return 0;
925}
926
927static int conn_latency_get(void *data, u64 *val)
928{
929 struct hci_dev *hdev = data;
930
931 hci_dev_lock(hdev);
932 *val = hdev->le_conn_latency;
933 hci_dev_unlock(hdev);
934
935 return 0;
936}
937
938DEFINE_DEBUGFS_ATTRIBUTE(conn_latency_fops, conn_latency_get,
939 conn_latency_set, "%llu\n");
940
941static int supervision_timeout_set(void *data, u64 val)
942{
943 struct hci_dev *hdev = data;
944
945 if (val < 0x000a || val > 0x0c80)
946 return -EINVAL;
947
948 hci_dev_lock(hdev);
949 hdev->le_supv_timeout = val;
950 hci_dev_unlock(hdev);
951
952 return 0;
953}
954
955static int supervision_timeout_get(void *data, u64 *val)
956{
957 struct hci_dev *hdev = data;
958
959 hci_dev_lock(hdev);
960 *val = hdev->le_supv_timeout;
961 hci_dev_unlock(hdev);
962
963 return 0;
964}
965
966DEFINE_DEBUGFS_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
967 supervision_timeout_set, "%llu\n");
968
969static int adv_channel_map_set(void *data, u64 val)
970{
971 struct hci_dev *hdev = data;
972
973 if (val < 0x01 || val > 0x07)
974 return -EINVAL;
975
976 hci_dev_lock(hdev);
977 hdev->le_adv_channel_map = val;
978 hci_dev_unlock(hdev);
979
980 return 0;
981}
982
983static int adv_channel_map_get(void *data, u64 *val)
984{
985 struct hci_dev *hdev = data;
986
987 hci_dev_lock(hdev);
988 *val = hdev->le_adv_channel_map;
989 hci_dev_unlock(hdev);
990
991 return 0;
992}
993
994DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
995 adv_channel_map_set, "%llu\n");
996
997static int adv_min_interval_set(void *data, u64 val)
998{
999 struct hci_dev *hdev = data;
1000
1001 hci_dev_lock(hdev);
1002 if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval) {
1003 hci_dev_unlock(hdev);
1004 return -EINVAL;
1005 }
1006
1007 hdev->le_adv_min_interval = val;
1008 hci_dev_unlock(hdev);
1009
1010 return 0;
1011}
1012
1013static int adv_min_interval_get(void *data, u64 *val)
1014{
1015 struct hci_dev *hdev = data;
1016
1017 hci_dev_lock(hdev);
1018 *val = hdev->le_adv_min_interval;
1019 hci_dev_unlock(hdev);
1020
1021 return 0;
1022}
1023
1024DEFINE_DEBUGFS_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
1025 adv_min_interval_set, "%llu\n");
1026
1027static int adv_max_interval_set(void *data, u64 val)
1028{
1029 struct hci_dev *hdev = data;
1030
1031 hci_dev_lock(hdev);
1032 if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval) {
1033 hci_dev_unlock(hdev);
1034 return -EINVAL;
1035 }
1036
1037 hdev->le_adv_max_interval = val;
1038 hci_dev_unlock(hdev);
1039
1040 return 0;
1041}
1042
1043static int adv_max_interval_get(void *data, u64 *val)
1044{
1045 struct hci_dev *hdev = data;
1046
1047 hci_dev_lock(hdev);
1048 *val = hdev->le_adv_max_interval;
1049 hci_dev_unlock(hdev);
1050
1051 return 0;
1052}
1053
1054DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
1055 adv_max_interval_set, "%llu\n");
1056
1057static int min_key_size_set(void *data, u64 val)
1058{
1059 struct hci_dev *hdev = data;
1060
1061 hci_dev_lock(hdev);
1062 if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) {
1063 hci_dev_unlock(hdev);
1064 return -EINVAL;
1065 }
1066
1067 hdev->le_min_key_size = val;
1068 hci_dev_unlock(hdev);
1069
1070 return 0;
1071}
1072
1073static int min_key_size_get(void *data, u64 *val)
1074{
1075 struct hci_dev *hdev = data;
1076
1077 hci_dev_lock(hdev);
1078 *val = hdev->le_min_key_size;
1079 hci_dev_unlock(hdev);
1080
1081 return 0;
1082}
1083
1084DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
1085 min_key_size_set, "%llu\n");
1086
1087static int max_key_size_set(void *data, u64 val)
1088{
1089 struct hci_dev *hdev = data;
1090
1091 hci_dev_lock(hdev);
1092 if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) {
1093 hci_dev_unlock(hdev);
1094 return -EINVAL;
1095 }
1096
1097 hdev->le_max_key_size = val;
1098 hci_dev_unlock(hdev);
1099
1100 return 0;
1101}
1102
1103static int max_key_size_get(void *data, u64 *val)
1104{
1105 struct hci_dev *hdev = data;
1106
1107 hci_dev_lock(hdev);
1108 *val = hdev->le_max_key_size;
1109 hci_dev_unlock(hdev);
1110
1111 return 0;
1112}
1113
1114DEFINE_DEBUGFS_ATTRIBUTE(max_key_size_fops, max_key_size_get,
1115 max_key_size_set, "%llu\n");
1116
1117static int auth_payload_timeout_set(void *data, u64 val)
1118{
1119 struct hci_dev *hdev = data;
1120
1121 if (val < 0x0001 || val > 0xffff)
1122 return -EINVAL;
1123
1124 hci_dev_lock(hdev);
1125 hdev->auth_payload_timeout = val;
1126 hci_dev_unlock(hdev);
1127
1128 return 0;
1129}
1130
1131static int auth_payload_timeout_get(void *data, u64 *val)
1132{
1133 struct hci_dev *hdev = data;
1134
1135 hci_dev_lock(hdev);
1136 *val = hdev->auth_payload_timeout;
1137 hci_dev_unlock(hdev);
1138
1139 return 0;
1140}
1141
1142DEFINE_DEBUGFS_ATTRIBUTE(auth_payload_timeout_fops,
1143 auth_payload_timeout_get,
1144 auth_payload_timeout_set, "%llu\n");
1145
1146static ssize_t force_no_mitm_read(struct file *file,
1147 char __user *user_buf,
1148 size_t count, loff_t *ppos)
1149{
1150 struct hci_dev *hdev = file->private_data;
1151 char buf[3];
1152
1153 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM) ? 'Y' : 'N';
1154 buf[1] = '\n';
1155 buf[2] = '\0';
1156 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1157}
1158
1159static ssize_t force_no_mitm_write(struct file *file,
1160 const char __user *user_buf,
1161 size_t count, loff_t *ppos)
1162{
1163 struct hci_dev *hdev = file->private_data;
1164 bool enable;
1165 int err;
1166
1167 err = kstrtobool_from_user(user_buf, count, &enable);
1168 if (err)
1169 return err;
1170
1171 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
1172 return -EALREADY;
1173
1174 hci_dev_change_flag(hdev, HCI_FORCE_NO_MITM);
1175
1176 return count;
1177}
1178
1179static const struct file_operations force_no_mitm_fops = {
1180 .open = simple_open,
1181 .read = force_no_mitm_read,
1182 .write = force_no_mitm_write,
1183 .llseek = default_llseek,
1184};
1185
1186DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1187 HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1188DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1189 HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1190
1191void hci_debugfs_create_le(struct hci_dev *hdev)
1192{
1193 debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
1194 &identity_fops);
1195 debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
1196 &rpa_timeout_fops);
1197 debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
1198 &random_address_fops);
1199 debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
1200 &static_address_fops);
1201
1202 /* For controllers with a public address, provide a debug
1203 * option to force the usage of the configured static
1204 * address. By default the public address is used.
1205 */
1206 if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1207 debugfs_create_file("force_static_address", 0644,
1208 hdev->debugfs, hdev,
1209 &force_static_address_fops);
1210
1211 debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1212 &hdev->le_accept_list_size);
1213 debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1214 &white_list_fops);
1215 debugfs_create_u8("resolv_list_size", 0444, hdev->debugfs,
1216 &hdev->le_resolv_list_size);
1217 debugfs_create_file("resolv_list", 0444, hdev->debugfs, hdev,
1218 &resolv_list_fops);
1219 debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
1220 hdev, &identity_resolving_keys_fops);
1221 debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
1222 &long_term_keys_fops);
1223 debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
1224 &conn_min_interval_fops);
1225 debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
1226 &conn_max_interval_fops);
1227 debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
1228 &conn_latency_fops);
1229 debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
1230 &supervision_timeout_fops);
1231 debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
1232 &adv_channel_map_fops);
1233 debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
1234 &adv_min_interval_fops);
1235 debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
1236 &adv_max_interval_fops);
1237 debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1238 &hdev->discov_interleaved_timeout);
1239 debugfs_create_file("min_key_size", 0644, hdev->debugfs, hdev,
1240 &min_key_size_fops);
1241 debugfs_create_file("max_key_size", 0644, hdev->debugfs, hdev,
1242 &max_key_size_fops);
1243 debugfs_create_file("auth_payload_timeout", 0644, hdev->debugfs, hdev,
1244 &auth_payload_timeout_fops);
1245 debugfs_create_file("force_no_mitm", 0644, hdev->debugfs, hdev,
1246 &force_no_mitm_fops);
1247
1248 debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1249 hdev->debugfs, hdev,
1250 &quirk_strict_duplicate_filter_fops);
1251 debugfs_create_file("quirk_simultaneous_discovery", 0644,
1252 hdev->debugfs, hdev,
1253 &quirk_simultaneous_discovery_fops);
1254}
1255
1256void hci_debugfs_create_conn(struct hci_conn *conn)
1257{
1258 struct hci_dev *hdev = conn->hdev;
1259 char name[6];
1260
1261 if (IS_ERR_OR_NULL(hdev->debugfs) || conn->debugfs)
1262 return;
1263
1264 snprintf(name, sizeof(name), "%u", conn->handle);
1265 conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
1266}
1267
1268static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
1269 size_t count, loff_t *ppos)
1270{
1271 struct hci_dev *hdev = file->private_data;
1272 char buf[3];
1273
1274 buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N';
1275 buf[1] = '\n';
1276 buf[2] = '\0';
1277 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1278}
1279
1280static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
1281 size_t count, loff_t *ppos)
1282{
1283 struct hci_dev *hdev = file->private_data;
1284 struct sk_buff *skb;
1285 bool enable;
1286 int err;
1287
1288 if (!test_bit(HCI_UP, &hdev->flags))
1289 return -ENETDOWN;
1290
1291 err = kstrtobool_from_user(user_buf, count, &enable);
1292 if (err)
1293 return err;
1294
1295 if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
1296 return -EALREADY;
1297
1298 hci_req_sync_lock(hdev);
1299 if (enable)
1300 skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1301 HCI_CMD_TIMEOUT);
1302 else
1303 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1304 HCI_CMD_TIMEOUT);
1305 hci_req_sync_unlock(hdev);
1306
1307 if (IS_ERR(skb))
1308 return PTR_ERR(skb);
1309
1310 kfree_skb(skb);
1311
1312 hci_dev_change_flag(hdev, HCI_DUT_MODE);
1313
1314 return count;
1315}
1316
1317static const struct file_operations dut_mode_fops = {
1318 .open = simple_open,
1319 .read = dut_mode_read,
1320 .write = dut_mode_write,
1321 .llseek = default_llseek,
1322};
1323
1324static ssize_t vendor_diag_read(struct file *file, char __user *user_buf,
1325 size_t count, loff_t *ppos)
1326{
1327 struct hci_dev *hdev = file->private_data;
1328 char buf[3];
1329
1330 buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N';
1331 buf[1] = '\n';
1332 buf[2] = '\0';
1333 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1334}
1335
1336static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf,
1337 size_t count, loff_t *ppos)
1338{
1339 struct hci_dev *hdev = file->private_data;
1340 bool enable;
1341 int err;
1342
1343 err = kstrtobool_from_user(user_buf, count, &enable);
1344 if (err)
1345 return err;
1346
1347 /* When the diagnostic flags are not persistent and the transport
1348 * is not active or in user channel operation, then there is no need
1349 * for the vendor callback. Instead just store the desired value and
1350 * the setting will be programmed when the controller gets powered on.
1351 */
1352 if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG) &&
1353 (!test_bit(HCI_RUNNING, &hdev->flags) ||
1354 hci_dev_test_flag(hdev, HCI_USER_CHANNEL)))
1355 goto done;
1356
1357 hci_req_sync_lock(hdev);
1358 err = hdev->set_diag(hdev, enable);
1359 hci_req_sync_unlock(hdev);
1360
1361 if (err < 0)
1362 return err;
1363
1364done:
1365 if (enable)
1366 hci_dev_set_flag(hdev, HCI_VENDOR_DIAG);
1367 else
1368 hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG);
1369
1370 return count;
1371}
1372
1373static const struct file_operations vendor_diag_fops = {
1374 .open = simple_open,
1375 .read = vendor_diag_read,
1376 .write = vendor_diag_write,
1377 .llseek = default_llseek,
1378};
1379
1380void hci_debugfs_create_basic(struct hci_dev *hdev)
1381{
1382 debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
1383 &dut_mode_fops);
1384
1385 if (hdev->set_diag)
1386 debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev,
1387 &vendor_diag_fops);
1388}