xref: /linux/net/bluetooth/hci_debugfs.c (revision 056e065a6b6e01ab54bb9770c0d5a15350e571e2)
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)				      \
31 static 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 									      \
44 static 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 									      \
67 static 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)				      \
75 static 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 									      \
86 DEFINE_SHOW_ATTRIBUTE(__name)
87 
88 static 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 
103 DEFINE_SHOW_ATTRIBUTE(features);
104 
105 static 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 
117 DEFINE_SHOW_ATTRIBUTE(device_id);
118 
119 static 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 
137 DEFINE_SHOW_ATTRIBUTE(device_list);
138 
139 static 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 
152 DEFINE_SHOW_ATTRIBUTE(blacklist);
153 
154 static 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 
167 DEFINE_SHOW_ATTRIBUTE(blocked_keys);
168 
169 static 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 
192 DEFINE_SHOW_ATTRIBUTE(uuids);
193 
194 static 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 
211 DEFINE_SHOW_ATTRIBUTE(remote_oob);
212 
213 static 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 
229 static 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 
240 DEFINE_DEBUGFS_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
241 			  conn_info_min_age_set, "%llu\n");
242 
243 static 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 
259 static 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 
270 DEFINE_DEBUGFS_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
271 			  conn_info_max_age_set, "%llu\n");
272 
273 static 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 
285 static const struct file_operations use_debug_keys_fops = {
286 	.open		= simple_open,
287 	.read		= use_debug_keys_read,
288 	.llseek		= default_llseek,
289 };
290 
291 static 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 
303 static const struct file_operations sc_only_mode_fops = {
304 	.open		= simple_open,
305 	.read		= sc_only_mode_read,
306 	.llseek		= default_llseek,
307 };
308 
309 DEFINE_INFO_ATTRIBUTE(hardware_info, hw_info);
310 DEFINE_INFO_ATTRIBUTE(firmware_info, fw_info);
311 
312 void 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 
357 static 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 %u\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 
381 DEFINE_SHOW_ATTRIBUTE(inquiry_cache);
382 
383 static 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 
397 DEFINE_SHOW_ATTRIBUTE(link_keys);
398 
399 static 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 
411 DEFINE_SHOW_ATTRIBUTE(dev_class);
412 
413 static 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 
424 DEFINE_DEBUGFS_ATTRIBUTE(voice_setting_fops, voice_setting_get,
425 			  NULL, "0x%4.4llx\n");
426 
427 static 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 
439 static const struct file_operations ssp_debug_mode_fops = {
440 	.open		= simple_open,
441 	.read		= ssp_debug_mode_read,
442 	.llseek		= default_llseek,
443 };
444 
445 static 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 
456 static 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 
470 static 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 
481 DEFINE_DEBUGFS_ATTRIBUTE(min_encrypt_key_size_fops,
482 			  min_encrypt_key_size_get,
483 			  min_encrypt_key_size_set, "%llu\n");
484 
485 static 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 
496 DEFINE_DEBUGFS_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
497 			  auto_accept_delay_set, "%llu\n");
498 
499 static 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 
512 static 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 
531 static 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 
538 static 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 
552 static 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 
563 DEFINE_DEBUGFS_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
564 			  idle_timeout_set, "%llu\n");
565 
566 static 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 
582 static 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 
593 DEFINE_DEBUGFS_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
594 			  sniff_min_interval_set, "%llu\n");
595 
596 static 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 
612 static 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 
623 DEFINE_DEBUGFS_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
624 			  sniff_max_interval_set, "%llu\n");
625 
626 void 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 
667 static 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 
685 DEFINE_SHOW_ATTRIBUTE(identity);
686 
687 static 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 
704 static 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 
715 DEFINE_DEBUGFS_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
716 			  rpa_timeout_set, "%llu\n");
717 
718 static 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 
729 DEFINE_SHOW_ATTRIBUTE(random_address);
730 
731 static 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 
742 DEFINE_SHOW_ATTRIBUTE(static_address);
743 
744 static 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 
757 static 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 
780 static 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 
787 static 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 
800 DEFINE_SHOW_ATTRIBUTE(white_list);
801 
802 static 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 
815 DEFINE_SHOW_ATTRIBUTE(resolv_list);
816 
817 static 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 
833 DEFINE_SHOW_ATTRIBUTE(identity_resolving_keys);
834 
835 static 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 
851 DEFINE_SHOW_ATTRIBUTE(long_term_keys);
852 
853 static 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 
869 static 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 
880 DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
881 			  conn_min_interval_set, "%llu\n");
882 
883 static 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 
899 static 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 
910 DEFINE_DEBUGFS_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
911 			  conn_max_interval_set, "%llu\n");
912 
913 static 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 
927 static 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 
938 DEFINE_DEBUGFS_ATTRIBUTE(conn_latency_fops, conn_latency_get,
939 			  conn_latency_set, "%llu\n");
940 
941 static 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 
955 static 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 
966 DEFINE_DEBUGFS_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
967 			  supervision_timeout_set, "%llu\n");
968 
969 static 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 
983 static 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 
994 DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
995 			  adv_channel_map_set, "%llu\n");
996 
997 static 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 
1013 static 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 
1024 DEFINE_DEBUGFS_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
1025 			  adv_min_interval_set, "%llu\n");
1026 
1027 static 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 
1043 static 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 
1054 DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
1055 			  adv_max_interval_set, "%llu\n");
1056 
1057 static 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 
1073 static 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 
1084 DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
1085 			  min_key_size_set, "%llu\n");
1086 
1087 static 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 
1103 static 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 
1114 DEFINE_DEBUGFS_ATTRIBUTE(max_key_size_fops, max_key_size_get,
1115 			  max_key_size_set, "%llu\n");
1116 
1117 static 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 
1131 static 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 
1142 DEFINE_DEBUGFS_ATTRIBUTE(auth_payload_timeout_fops,
1143 			  auth_payload_timeout_get,
1144 			  auth_payload_timeout_set, "%llu\n");
1145 
1146 static 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 
1159 static 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 	char buf[32];
1165 	size_t buf_size = min(count, (sizeof(buf) - 1));
1166 	bool enable;
1167 
1168 	if (copy_from_user(buf, user_buf, buf_size))
1169 		return -EFAULT;
1170 
1171 	buf[buf_size] = '\0';
1172 	if (kstrtobool(buf, &enable))
1173 		return -EINVAL;
1174 
1175 	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
1176 		return -EALREADY;
1177 
1178 	hci_dev_change_flag(hdev, HCI_FORCE_NO_MITM);
1179 
1180 	return count;
1181 }
1182 
1183 static const struct file_operations force_no_mitm_fops = {
1184 	.open		= simple_open,
1185 	.read		= force_no_mitm_read,
1186 	.write		= force_no_mitm_write,
1187 	.llseek		= default_llseek,
1188 };
1189 
1190 DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1191 		       HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1192 DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1193 		       HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1194 
1195 void hci_debugfs_create_le(struct hci_dev *hdev)
1196 {
1197 	debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
1198 			    &identity_fops);
1199 	debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
1200 			    &rpa_timeout_fops);
1201 	debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
1202 			    &random_address_fops);
1203 	debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
1204 			    &static_address_fops);
1205 
1206 	/* For controllers with a public address, provide a debug
1207 	 * option to force the usage of the configured static
1208 	 * address. By default the public address is used.
1209 	 */
1210 	if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1211 		debugfs_create_file("force_static_address", 0644,
1212 				    hdev->debugfs, hdev,
1213 				    &force_static_address_fops);
1214 
1215 	debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1216 			  &hdev->le_accept_list_size);
1217 	debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1218 			    &white_list_fops);
1219 	debugfs_create_u8("resolv_list_size", 0444, hdev->debugfs,
1220 			  &hdev->le_resolv_list_size);
1221 	debugfs_create_file("resolv_list", 0444, hdev->debugfs, hdev,
1222 			    &resolv_list_fops);
1223 	debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
1224 			    hdev, &identity_resolving_keys_fops);
1225 	debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
1226 			    &long_term_keys_fops);
1227 	debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
1228 			    &conn_min_interval_fops);
1229 	debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
1230 			    &conn_max_interval_fops);
1231 	debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
1232 			    &conn_latency_fops);
1233 	debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
1234 			    &supervision_timeout_fops);
1235 	debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
1236 			    &adv_channel_map_fops);
1237 	debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
1238 			    &adv_min_interval_fops);
1239 	debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
1240 			    &adv_max_interval_fops);
1241 	debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1242 			   &hdev->discov_interleaved_timeout);
1243 	debugfs_create_file("min_key_size", 0644, hdev->debugfs, hdev,
1244 			    &min_key_size_fops);
1245 	debugfs_create_file("max_key_size", 0644, hdev->debugfs, hdev,
1246 			    &max_key_size_fops);
1247 	debugfs_create_file("auth_payload_timeout", 0644, hdev->debugfs, hdev,
1248 			    &auth_payload_timeout_fops);
1249 	debugfs_create_file("force_no_mitm", 0644, hdev->debugfs, hdev,
1250 			    &force_no_mitm_fops);
1251 
1252 	debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1253 			    hdev->debugfs, hdev,
1254 			    &quirk_strict_duplicate_filter_fops);
1255 	debugfs_create_file("quirk_simultaneous_discovery", 0644,
1256 			    hdev->debugfs, hdev,
1257 			    &quirk_simultaneous_discovery_fops);
1258 }
1259 
1260 void hci_debugfs_create_conn(struct hci_conn *conn)
1261 {
1262 	struct hci_dev *hdev = conn->hdev;
1263 	char name[6];
1264 
1265 	if (IS_ERR_OR_NULL(hdev->debugfs) || conn->debugfs)
1266 		return;
1267 
1268 	snprintf(name, sizeof(name), "%u", conn->handle);
1269 	conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
1270 }
1271 
1272 static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
1273 			     size_t count, loff_t *ppos)
1274 {
1275 	struct hci_dev *hdev = file->private_data;
1276 	char buf[3];
1277 
1278 	buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N';
1279 	buf[1] = '\n';
1280 	buf[2] = '\0';
1281 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1282 }
1283 
1284 static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
1285 			      size_t count, loff_t *ppos)
1286 {
1287 	struct hci_dev *hdev = file->private_data;
1288 	struct sk_buff *skb;
1289 	bool enable;
1290 	int err;
1291 
1292 	if (!test_bit(HCI_UP, &hdev->flags))
1293 		return -ENETDOWN;
1294 
1295 	err = kstrtobool_from_user(user_buf, count, &enable);
1296 	if (err)
1297 		return err;
1298 
1299 	if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
1300 		return -EALREADY;
1301 
1302 	hci_req_sync_lock(hdev);
1303 	if (enable)
1304 		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1305 				     HCI_CMD_TIMEOUT);
1306 	else
1307 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1308 				     HCI_CMD_TIMEOUT);
1309 	hci_req_sync_unlock(hdev);
1310 
1311 	if (IS_ERR(skb))
1312 		return PTR_ERR(skb);
1313 
1314 	kfree_skb(skb);
1315 
1316 	hci_dev_change_flag(hdev, HCI_DUT_MODE);
1317 
1318 	return count;
1319 }
1320 
1321 static const struct file_operations dut_mode_fops = {
1322 	.open		= simple_open,
1323 	.read		= dut_mode_read,
1324 	.write		= dut_mode_write,
1325 	.llseek		= default_llseek,
1326 };
1327 
1328 static ssize_t vendor_diag_read(struct file *file, char __user *user_buf,
1329 				size_t count, loff_t *ppos)
1330 {
1331 	struct hci_dev *hdev = file->private_data;
1332 	char buf[3];
1333 
1334 	buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N';
1335 	buf[1] = '\n';
1336 	buf[2] = '\0';
1337 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1338 }
1339 
1340 static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf,
1341 				 size_t count, loff_t *ppos)
1342 {
1343 	struct hci_dev *hdev = file->private_data;
1344 	bool enable;
1345 	int err;
1346 
1347 	err = kstrtobool_from_user(user_buf, count, &enable);
1348 	if (err)
1349 		return err;
1350 
1351 	/* When the diagnostic flags are not persistent and the transport
1352 	 * is not active or in user channel operation, then there is no need
1353 	 * for the vendor callback. Instead just store the desired value and
1354 	 * the setting will be programmed when the controller gets powered on.
1355 	 */
1356 	if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG) &&
1357 	    (!test_bit(HCI_RUNNING, &hdev->flags) ||
1358 	     hci_dev_test_flag(hdev, HCI_USER_CHANNEL)))
1359 		goto done;
1360 
1361 	hci_req_sync_lock(hdev);
1362 	err = hdev->set_diag(hdev, enable);
1363 	hci_req_sync_unlock(hdev);
1364 
1365 	if (err < 0)
1366 		return err;
1367 
1368 done:
1369 	if (enable)
1370 		hci_dev_set_flag(hdev, HCI_VENDOR_DIAG);
1371 	else
1372 		hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG);
1373 
1374 	return count;
1375 }
1376 
1377 static const struct file_operations vendor_diag_fops = {
1378 	.open		= simple_open,
1379 	.read		= vendor_diag_read,
1380 	.write		= vendor_diag_write,
1381 	.llseek		= default_llseek,
1382 };
1383 
1384 void hci_debugfs_create_basic(struct hci_dev *hdev)
1385 {
1386 	debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
1387 			    &dut_mode_fops);
1388 
1389 	if (hdev->set_diag)
1390 		debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev,
1391 				    &vendor_diag_fops);
1392 }
1393