xref: /linux/net/bluetooth/hci_debugfs.c (revision b49abf5f92ea0e03ebc0fe41ce4af87b094fe9b5)
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 	assign_bit(__quirk, hdev->quirk_flags, enable);			      \
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 	if (enable)
781 		hci_dev_set_flag(hdev, HCI_FORCE_STATIC_ADDR);
782 	else
783 		hci_dev_clear_flag(hdev, HCI_FORCE_STATIC_ADDR);
784 
785 	return count;
786 }
787 
788 static const struct file_operations force_static_address_fops = {
789 	.owner		= THIS_MODULE,
790 	.open		= simple_open,
791 	.read		= force_static_address_read,
792 	.write		= force_static_address_write,
793 	.llseek		= default_llseek,
794 };
795 
796 static int white_list_show(struct seq_file *f, void *ptr)
797 {
798 	struct hci_dev *hdev = f->private;
799 	struct bdaddr_list *b;
800 
801 	hci_dev_lock(hdev);
802 	list_for_each_entry(b, &hdev->le_accept_list, list)
803 		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
804 	hci_dev_unlock(hdev);
805 
806 	return 0;
807 }
808 
809 DEFINE_SHOW_ATTRIBUTE(white_list);
810 
811 static int resolv_list_show(struct seq_file *f, void *ptr)
812 {
813 	struct hci_dev *hdev = f->private;
814 	struct bdaddr_list *b;
815 
816 	hci_dev_lock(hdev);
817 	list_for_each_entry(b, &hdev->le_resolv_list, list)
818 		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
819 	hci_dev_unlock(hdev);
820 
821 	return 0;
822 }
823 
824 DEFINE_SHOW_ATTRIBUTE(resolv_list);
825 
826 static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
827 {
828 	struct hci_dev *hdev = f->private;
829 	struct smp_irk *irk;
830 
831 	rcu_read_lock();
832 	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
833 		seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
834 			   &irk->bdaddr, irk->addr_type,
835 			   16, irk->val, &irk->rpa);
836 	}
837 	rcu_read_unlock();
838 
839 	return 0;
840 }
841 
842 DEFINE_SHOW_ATTRIBUTE(identity_resolving_keys);
843 
844 static int long_term_keys_show(struct seq_file *f, void *ptr)
845 {
846 	struct hci_dev *hdev = f->private;
847 	struct smp_ltk *ltk;
848 
849 	rcu_read_lock();
850 	list_for_each_entry_rcu(ltk, &hdev->long_term_keys, list)
851 		seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
852 			   &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
853 			   ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
854 			   __le64_to_cpu(ltk->rand), 16, ltk->val);
855 	rcu_read_unlock();
856 
857 	return 0;
858 }
859 
860 DEFINE_SHOW_ATTRIBUTE(long_term_keys);
861 
862 static int conn_min_interval_set(void *data, u64 val)
863 {
864 	struct hci_dev *hdev = data;
865 
866 	hci_dev_lock(hdev);
867 	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval) {
868 		hci_dev_unlock(hdev);
869 		return -EINVAL;
870 	}
871 
872 	hdev->le_conn_min_interval = val;
873 	hci_dev_unlock(hdev);
874 
875 	return 0;
876 }
877 
878 static int conn_min_interval_get(void *data, u64 *val)
879 {
880 	struct hci_dev *hdev = data;
881 
882 	hci_dev_lock(hdev);
883 	*val = hdev->le_conn_min_interval;
884 	hci_dev_unlock(hdev);
885 
886 	return 0;
887 }
888 
889 DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
890 			  conn_min_interval_set, "%llu\n");
891 
892 static int conn_max_interval_set(void *data, u64 val)
893 {
894 	struct hci_dev *hdev = data;
895 
896 	hci_dev_lock(hdev);
897 	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval) {
898 		hci_dev_unlock(hdev);
899 		return -EINVAL;
900 	}
901 
902 	hdev->le_conn_max_interval = val;
903 	hci_dev_unlock(hdev);
904 
905 	return 0;
906 }
907 
908 static int conn_max_interval_get(void *data, u64 *val)
909 {
910 	struct hci_dev *hdev = data;
911 
912 	hci_dev_lock(hdev);
913 	*val = hdev->le_conn_max_interval;
914 	hci_dev_unlock(hdev);
915 
916 	return 0;
917 }
918 
919 DEFINE_DEBUGFS_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
920 			  conn_max_interval_set, "%llu\n");
921 
922 static int conn_latency_set(void *data, u64 val)
923 {
924 	struct hci_dev *hdev = data;
925 
926 	if (val > 0x01f3)
927 		return -EINVAL;
928 
929 	hci_dev_lock(hdev);
930 	hdev->le_conn_latency = val;
931 	hci_dev_unlock(hdev);
932 
933 	return 0;
934 }
935 
936 static int conn_latency_get(void *data, u64 *val)
937 {
938 	struct hci_dev *hdev = data;
939 
940 	hci_dev_lock(hdev);
941 	*val = hdev->le_conn_latency;
942 	hci_dev_unlock(hdev);
943 
944 	return 0;
945 }
946 
947 DEFINE_DEBUGFS_ATTRIBUTE(conn_latency_fops, conn_latency_get,
948 			  conn_latency_set, "%llu\n");
949 
950 static int supervision_timeout_set(void *data, u64 val)
951 {
952 	struct hci_dev *hdev = data;
953 
954 	if (val < 0x000a || val > 0x0c80)
955 		return -EINVAL;
956 
957 	hci_dev_lock(hdev);
958 	hdev->le_supv_timeout = val;
959 	hci_dev_unlock(hdev);
960 
961 	return 0;
962 }
963 
964 static int supervision_timeout_get(void *data, u64 *val)
965 {
966 	struct hci_dev *hdev = data;
967 
968 	hci_dev_lock(hdev);
969 	*val = hdev->le_supv_timeout;
970 	hci_dev_unlock(hdev);
971 
972 	return 0;
973 }
974 
975 DEFINE_DEBUGFS_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
976 			  supervision_timeout_set, "%llu\n");
977 
978 static int adv_channel_map_set(void *data, u64 val)
979 {
980 	struct hci_dev *hdev = data;
981 
982 	if (val < 0x01 || val > 0x07)
983 		return -EINVAL;
984 
985 	hci_dev_lock(hdev);
986 	hdev->le_adv_channel_map = val;
987 	hci_dev_unlock(hdev);
988 
989 	return 0;
990 }
991 
992 static int adv_channel_map_get(void *data, u64 *val)
993 {
994 	struct hci_dev *hdev = data;
995 
996 	hci_dev_lock(hdev);
997 	*val = hdev->le_adv_channel_map;
998 	hci_dev_unlock(hdev);
999 
1000 	return 0;
1001 }
1002 
1003 DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
1004 			  adv_channel_map_set, "%llu\n");
1005 
1006 static int adv_min_interval_set(void *data, u64 val)
1007 {
1008 	struct hci_dev *hdev = data;
1009 
1010 	hci_dev_lock(hdev);
1011 	if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval) {
1012 		hci_dev_unlock(hdev);
1013 		return -EINVAL;
1014 	}
1015 
1016 	hdev->le_adv_min_interval = val;
1017 	hci_dev_unlock(hdev);
1018 
1019 	return 0;
1020 }
1021 
1022 static int adv_min_interval_get(void *data, u64 *val)
1023 {
1024 	struct hci_dev *hdev = data;
1025 
1026 	hci_dev_lock(hdev);
1027 	*val = hdev->le_adv_min_interval;
1028 	hci_dev_unlock(hdev);
1029 
1030 	return 0;
1031 }
1032 
1033 DEFINE_DEBUGFS_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
1034 			  adv_min_interval_set, "%llu\n");
1035 
1036 static int adv_max_interval_set(void *data, u64 val)
1037 {
1038 	struct hci_dev *hdev = data;
1039 
1040 	hci_dev_lock(hdev);
1041 	if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval) {
1042 		hci_dev_unlock(hdev);
1043 		return -EINVAL;
1044 	}
1045 
1046 	hdev->le_adv_max_interval = val;
1047 	hci_dev_unlock(hdev);
1048 
1049 	return 0;
1050 }
1051 
1052 static int adv_max_interval_get(void *data, u64 *val)
1053 {
1054 	struct hci_dev *hdev = data;
1055 
1056 	hci_dev_lock(hdev);
1057 	*val = hdev->le_adv_max_interval;
1058 	hci_dev_unlock(hdev);
1059 
1060 	return 0;
1061 }
1062 
1063 DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
1064 			  adv_max_interval_set, "%llu\n");
1065 
1066 static int min_key_size_set(void *data, u64 val)
1067 {
1068 	struct hci_dev *hdev = data;
1069 
1070 	hci_dev_lock(hdev);
1071 	if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE) {
1072 		hci_dev_unlock(hdev);
1073 		return -EINVAL;
1074 	}
1075 
1076 	hdev->le_min_key_size = val;
1077 	hci_dev_unlock(hdev);
1078 
1079 	return 0;
1080 }
1081 
1082 static int min_key_size_get(void *data, u64 *val)
1083 {
1084 	struct hci_dev *hdev = data;
1085 
1086 	hci_dev_lock(hdev);
1087 	*val = hdev->le_min_key_size;
1088 	hci_dev_unlock(hdev);
1089 
1090 	return 0;
1091 }
1092 
1093 DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
1094 			  min_key_size_set, "%llu\n");
1095 
1096 static int max_key_size_set(void *data, u64 val)
1097 {
1098 	struct hci_dev *hdev = data;
1099 
1100 	hci_dev_lock(hdev);
1101 	if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size) {
1102 		hci_dev_unlock(hdev);
1103 		return -EINVAL;
1104 	}
1105 
1106 	hdev->le_max_key_size = val;
1107 	hci_dev_unlock(hdev);
1108 
1109 	return 0;
1110 }
1111 
1112 static int max_key_size_get(void *data, u64 *val)
1113 {
1114 	struct hci_dev *hdev = data;
1115 
1116 	hci_dev_lock(hdev);
1117 	*val = hdev->le_max_key_size;
1118 	hci_dev_unlock(hdev);
1119 
1120 	return 0;
1121 }
1122 
1123 DEFINE_DEBUGFS_ATTRIBUTE(max_key_size_fops, max_key_size_get,
1124 			  max_key_size_set, "%llu\n");
1125 
1126 static int auth_payload_timeout_set(void *data, u64 val)
1127 {
1128 	struct hci_dev *hdev = data;
1129 
1130 	if (val < 0x0001 || val > 0xffff)
1131 		return -EINVAL;
1132 
1133 	hci_dev_lock(hdev);
1134 	hdev->auth_payload_timeout = val;
1135 	hci_dev_unlock(hdev);
1136 
1137 	return 0;
1138 }
1139 
1140 static int auth_payload_timeout_get(void *data, u64 *val)
1141 {
1142 	struct hci_dev *hdev = data;
1143 
1144 	hci_dev_lock(hdev);
1145 	*val = hdev->auth_payload_timeout;
1146 	hci_dev_unlock(hdev);
1147 
1148 	return 0;
1149 }
1150 
1151 DEFINE_DEBUGFS_ATTRIBUTE(auth_payload_timeout_fops,
1152 			  auth_payload_timeout_get,
1153 			  auth_payload_timeout_set, "%llu\n");
1154 
1155 static ssize_t force_no_mitm_read(struct file *file,
1156 				  char __user *user_buf,
1157 				  size_t count, loff_t *ppos)
1158 {
1159 	struct hci_dev *hdev = file->private_data;
1160 	char buf[3];
1161 
1162 	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM) ? 'Y' : 'N';
1163 	buf[1] = '\n';
1164 	buf[2] = '\0';
1165 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1166 }
1167 
1168 static ssize_t force_no_mitm_write(struct file *file,
1169 				   const char __user *user_buf,
1170 				   size_t count, loff_t *ppos)
1171 {
1172 	struct hci_dev *hdev = file->private_data;
1173 	bool enable;
1174 	int err;
1175 
1176 	err = kstrtobool_from_user(user_buf, count, &enable);
1177 	if (err)
1178 		return err;
1179 
1180 	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
1181 		return -EALREADY;
1182 
1183 	if (enable)
1184 		hci_dev_set_flag(hdev, HCI_FORCE_NO_MITM);
1185 	else
1186 		hci_dev_clear_flag(hdev, HCI_FORCE_NO_MITM);
1187 
1188 	return count;
1189 }
1190 
1191 static const struct file_operations force_no_mitm_fops = {
1192 	.owner		= THIS_MODULE,
1193 	.open		= simple_open,
1194 	.read		= force_no_mitm_read,
1195 	.write		= force_no_mitm_write,
1196 	.llseek		= default_llseek,
1197 };
1198 
1199 DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1200 		       HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1201 DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1202 		       HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1203 
1204 void hci_debugfs_create_le(struct hci_dev *hdev)
1205 {
1206 	debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
1207 			    &identity_fops);
1208 	debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
1209 			    &rpa_timeout_fops);
1210 	debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
1211 			    &random_address_fops);
1212 	debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
1213 			    &static_address_fops);
1214 
1215 	/* For controllers with a public address, provide a debug
1216 	 * option to force the usage of the configured static
1217 	 * address. By default the public address is used.
1218 	 */
1219 	if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1220 		debugfs_create_file("force_static_address", 0644,
1221 				    hdev->debugfs, hdev,
1222 				    &force_static_address_fops);
1223 
1224 	debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1225 			  &hdev->le_accept_list_size);
1226 	debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1227 			    &white_list_fops);
1228 	debugfs_create_u8("resolv_list_size", 0444, hdev->debugfs,
1229 			  &hdev->le_resolv_list_size);
1230 	debugfs_create_file("resolv_list", 0444, hdev->debugfs, hdev,
1231 			    &resolv_list_fops);
1232 	debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
1233 			    hdev, &identity_resolving_keys_fops);
1234 	debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
1235 			    &long_term_keys_fops);
1236 	debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
1237 			    &conn_min_interval_fops);
1238 	debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
1239 			    &conn_max_interval_fops);
1240 	debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
1241 			    &conn_latency_fops);
1242 	debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
1243 			    &supervision_timeout_fops);
1244 	debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
1245 			    &adv_channel_map_fops);
1246 	debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
1247 			    &adv_min_interval_fops);
1248 	debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
1249 			    &adv_max_interval_fops);
1250 	debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1251 			   &hdev->discov_interleaved_timeout);
1252 	debugfs_create_file("min_key_size", 0644, hdev->debugfs, hdev,
1253 			    &min_key_size_fops);
1254 	debugfs_create_file("max_key_size", 0644, hdev->debugfs, hdev,
1255 			    &max_key_size_fops);
1256 	debugfs_create_file("auth_payload_timeout", 0644, hdev->debugfs, hdev,
1257 			    &auth_payload_timeout_fops);
1258 	debugfs_create_file("force_no_mitm", 0644, hdev->debugfs, hdev,
1259 			    &force_no_mitm_fops);
1260 
1261 	debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1262 			    hdev->debugfs, hdev,
1263 			    &quirk_strict_duplicate_filter_fops);
1264 	debugfs_create_file("quirk_simultaneous_discovery", 0644,
1265 			    hdev->debugfs, hdev,
1266 			    &quirk_simultaneous_discovery_fops);
1267 }
1268 
1269 void hci_debugfs_create_conn(struct hci_conn *conn)
1270 {
1271 	struct hci_dev *hdev = conn->hdev;
1272 	char name[6];
1273 
1274 	if (IS_ERR_OR_NULL(hdev->debugfs) || conn->debugfs)
1275 		return;
1276 
1277 	snprintf(name, sizeof(name), "%u", conn->handle);
1278 	conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
1279 }
1280 
1281 static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
1282 			     size_t count, loff_t *ppos)
1283 {
1284 	struct hci_dev *hdev = file->private_data;
1285 	char buf[3];
1286 
1287 	buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N';
1288 	buf[1] = '\n';
1289 	buf[2] = '\0';
1290 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1291 }
1292 
1293 static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
1294 			      size_t count, loff_t *ppos)
1295 {
1296 	struct hci_dev *hdev = file->private_data;
1297 	struct sk_buff *skb;
1298 	bool enable;
1299 	int err;
1300 
1301 	if (!test_bit(HCI_UP, &hdev->flags))
1302 		return -ENETDOWN;
1303 
1304 	err = kstrtobool_from_user(user_buf, count, &enable);
1305 	if (err)
1306 		return err;
1307 
1308 	if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
1309 		return -EALREADY;
1310 
1311 	hci_req_sync_lock(hdev);
1312 
1313 	/* Check again now that concurrent writers are excluded */
1314 	if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE)) {
1315 		hci_req_sync_unlock(hdev);
1316 		return -EALREADY;
1317 	}
1318 
1319 	if (enable)
1320 		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1321 				     HCI_CMD_TIMEOUT);
1322 	else
1323 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1324 				     HCI_CMD_TIMEOUT);
1325 	if (IS_ERR(skb)) {
1326 		hci_req_sync_unlock(hdev);
1327 		return PTR_ERR(skb);
1328 	}
1329 
1330 	kfree_skb(skb);
1331 
1332 	if (enable)
1333 		hci_dev_set_flag(hdev, HCI_DUT_MODE);
1334 	else
1335 		hci_dev_clear_flag(hdev, HCI_DUT_MODE);
1336 
1337 	hci_req_sync_unlock(hdev);
1338 
1339 	return count;
1340 }
1341 
1342 static const struct file_operations dut_mode_fops = {
1343 	.owner		= THIS_MODULE,
1344 	.open		= simple_open,
1345 	.read		= dut_mode_read,
1346 	.write		= dut_mode_write,
1347 	.llseek		= default_llseek,
1348 };
1349 
1350 static ssize_t vendor_diag_read(struct file *file, char __user *user_buf,
1351 				size_t count, loff_t *ppos)
1352 {
1353 	struct hci_dev *hdev = file->private_data;
1354 	char buf[3];
1355 
1356 	buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N';
1357 	buf[1] = '\n';
1358 	buf[2] = '\0';
1359 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1360 }
1361 
1362 static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf,
1363 				 size_t count, loff_t *ppos)
1364 {
1365 	struct hci_dev *hdev = file->private_data;
1366 	bool enable;
1367 	int err;
1368 
1369 	err = kstrtobool_from_user(user_buf, count, &enable);
1370 	if (err)
1371 		return err;
1372 
1373 	/* When the diagnostic flags are not persistent and the transport
1374 	 * is not active or in user channel operation, then there is no need
1375 	 * for the vendor callback. Instead just store the desired value and
1376 	 * the setting will be programmed when the controller gets powered on.
1377 	 */
1378 	if (hci_test_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG) &&
1379 	    (!test_bit(HCI_RUNNING, &hdev->flags) ||
1380 	     hci_dev_test_flag(hdev, HCI_USER_CHANNEL)))
1381 		goto done;
1382 
1383 	hci_req_sync_lock(hdev);
1384 	err = hdev->set_diag(hdev, enable);
1385 	hci_req_sync_unlock(hdev);
1386 
1387 	if (err < 0)
1388 		return err;
1389 
1390 done:
1391 	if (enable)
1392 		hci_dev_set_flag(hdev, HCI_VENDOR_DIAG);
1393 	else
1394 		hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG);
1395 
1396 	return count;
1397 }
1398 
1399 static const struct file_operations vendor_diag_fops = {
1400 	.owner		= THIS_MODULE,
1401 	.open		= simple_open,
1402 	.read		= vendor_diag_read,
1403 	.write		= vendor_diag_write,
1404 	.llseek		= default_llseek,
1405 };
1406 
1407 void hci_debugfs_create_basic(struct hci_dev *hdev)
1408 {
1409 	debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
1410 			    &dut_mode_fops);
1411 
1412 	if (hdev->set_diag)
1413 		debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev,
1414 				    &vendor_diag_fops);
1415 }
1416