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