hci_debugfs.c 30 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
   BlueZ - Bluetooth protocol stack for Linux

   Copyright (C) 2014 Intel Corporation

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
   SOFTWARE IS DISCLAIMED.
*/

#include <linux/debugfs.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>

29
#include "smp.h"
30 31
#include "hci_debugfs.h"

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#define DEFINE_QUIRK_ATTRIBUTE(__name, __quirk)				      \
static ssize_t __name ## _read(struct file *file,			      \
				char __user *user_buf,			      \
				size_t count, loff_t *ppos)		      \
{									      \
	struct hci_dev *hdev = file->private_data;			      \
	char buf[3];							      \
									      \
	buf[0] = test_bit(__quirk, &hdev->quirks) ? 'Y' : 'N';		      \
	buf[1] = '\n';							      \
	buf[2] = '\0';							      \
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);	      \
}									      \
									      \
static ssize_t __name ## _write(struct file *file,			      \
				 const char __user *user_buf,		      \
				 size_t count, loff_t *ppos)		      \
{									      \
	struct hci_dev *hdev = file->private_data;			      \
	bool enable;							      \
52
	int err;							      \
53 54 55 56
									      \
	if (test_bit(HCI_UP, &hdev->flags))				      \
		return -EBUSY;						      \
									      \
57 58 59
	err = kstrtobool_from_user(user_buf, count, &enable);		      \
	if (err)							      \
		return err;						      \
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
									      \
	if (enable == test_bit(__quirk, &hdev->quirks))			      \
		return -EALREADY;					      \
									      \
	change_bit(__quirk, &hdev->quirks);				      \
									      \
	return count;							      \
}									      \
									      \
static const struct file_operations __name ## _fops = {			      \
	.open		= simple_open,					      \
	.read		= __name ## _read,				      \
	.write		= __name ## _write,				      \
	.llseek		= default_llseek,				      \
}									      \

76 77 78 79 80 81 82 83 84 85 86 87
#define DEFINE_INFO_ATTRIBUTE(__name, __field)				      \
static int __name ## _show(struct seq_file *f, void *ptr)		      \
{									      \
	struct hci_dev *hdev = f->private;				      \
									      \
	hci_dev_lock(hdev);						      \
	seq_printf(f, "%s\n", hdev->__field ? : "");			      \
	hci_dev_unlock(hdev);						      \
									      \
	return 0;							      \
}									      \
									      \
88 89
DEFINE_SHOW_ATTRIBUTE(__name)

90 91 92 93 94 95
static int features_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	u8 p;

	hci_dev_lock(hdev);
96 97
	for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++)
		seq_printf(f, "%2u: %8ph\n", p, hdev->features[p]);
98
	if (lmp_le_capable(hdev))
99
		seq_printf(f, "LE: %8ph\n", hdev->le_features);
100 101 102 103 104
	hci_dev_unlock(hdev);

	return 0;
}

105
DEFINE_SHOW_ATTRIBUTE(features);
106

107 108 109 110 111 112 113 114 115 116 117 118
static int device_id_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;

	hci_dev_lock(hdev);
	seq_printf(f, "%4.4x:%4.4x:%4.4x:%4.4x\n", hdev->devid_source,
		  hdev->devid_vendor, hdev->devid_product, hdev->devid_version);
	hci_dev_unlock(hdev);

	return 0;
}

119
DEFINE_SHOW_ATTRIBUTE(device_id);
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static int device_list_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct hci_conn_params *p;
	struct bdaddr_list *b;

	hci_dev_lock(hdev);
	list_for_each_entry(b, &hdev->whitelist, list)
		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
	list_for_each_entry(p, &hdev->le_conn_params, list) {
		seq_printf(f, "%pMR (type %u) %u\n", &p->addr, p->addr_type,
			   p->auto_connect);
	}
	hci_dev_unlock(hdev);

	return 0;
}

139
DEFINE_SHOW_ATTRIBUTE(device_list);
140 141 142 143 144 145 146 147 148 149 150 151 152 153

static int blacklist_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;
	struct bdaddr_list *b;

	hci_dev_lock(hdev);
	list_for_each_entry(b, &hdev->blacklist, list)
		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
	hci_dev_unlock(hdev);

	return 0;
}

154
DEFINE_SHOW_ATTRIBUTE(blacklist);
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static int blocked_keys_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;
	struct blocked_key *key;

	rcu_read_lock();
	list_for_each_entry_rcu(key, &hdev->blocked_keys, list)
		seq_printf(f, "%u %*phN\n", key->type, 16, key->val);
	rcu_read_unlock();

	return 0;
}

DEFINE_SHOW_ATTRIBUTE(blocked_keys);

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static int uuids_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;
	struct bt_uuid *uuid;

	hci_dev_lock(hdev);
	list_for_each_entry(uuid, &hdev->uuids, list) {
		u8 i, val[16];

		/* The Bluetooth UUID values are stored in big endian,
		 * but with reversed byte order. So convert them into
		 * the right order for the %pUb modifier.
		 */
		for (i = 0; i < 16; i++)
			val[i] = uuid->uuid[15 - i];

		seq_printf(f, "%pUb\n", val);
	}
	hci_dev_unlock(hdev);

       return 0;
}

194
DEFINE_SHOW_ATTRIBUTE(uuids);
195

196 197 198 199 200 201 202 203 204 205
static int remote_oob_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct oob_data *data;

	hci_dev_lock(hdev);
	list_for_each_entry(data, &hdev->remote_oob_data, list) {
		seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n",
			   &data->bdaddr, data->bdaddr_type, data->present,
			   16, data->hash192, 16, data->rand192,
206
			   16, data->hash256, 16, data->rand256);
207 208 209 210 211 212
	}
	hci_dev_unlock(hdev);

	return 0;
}

213
DEFINE_SHOW_ATTRIBUTE(remote_oob);
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static int conn_info_min_age_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val == 0 || val > hdev->conn_info_max_age)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->conn_info_min_age = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int conn_info_min_age_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->conn_info_min_age;
	hci_dev_unlock(hdev);

	return 0;
}

240 241
DEFINE_DEBUGFS_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
			  conn_info_min_age_set, "%llu\n");
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

static int conn_info_max_age_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val == 0 || val < hdev->conn_info_min_age)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->conn_info_max_age = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int conn_info_max_age_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->conn_info_max_age;
	hci_dev_unlock(hdev);

	return 0;
}

268 269
DEFINE_DEBUGFS_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
			  conn_info_max_age_set, "%llu\n");
270

271 272 273 274 275 276
static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
				   size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

277
	buf[0] = hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS) ? 'Y': 'N';
278 279 280 281 282 283 284 285 286 287 288
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static const struct file_operations use_debug_keys_fops = {
	.open		= simple_open,
	.read		= use_debug_keys_read,
	.llseek		= default_llseek,
};

289 290 291 292 293 294
static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
				 size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

295
	buf[0] = hci_dev_test_flag(hdev, HCI_SC_ONLY) ? 'Y': 'N';
296 297 298 299 300 301 302 303 304 305 306
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static const struct file_operations sc_only_mode_fops = {
	.open		= simple_open,
	.read		= sc_only_mode_read,
	.llseek		= default_llseek,
};

307 308 309
DEFINE_INFO_ATTRIBUTE(hardware_info, hw_info);
DEFINE_INFO_ATTRIBUTE(firmware_info, fw_info);

310 311
void hci_debugfs_create_common(struct hci_dev *hdev)
{
312 313 314 315 316 317
	debugfs_create_file("features", 0444, hdev->debugfs, hdev,
			    &features_fops);
	debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
			   &hdev->manufacturer);
	debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
	debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
318 319
	debugfs_create_u8("hardware_error", 0444, hdev->debugfs,
			  &hdev->hw_error_code);
320 321
	debugfs_create_file("device_id", 0444, hdev->debugfs, hdev,
			    &device_id_fops);
322

323 324 325 326
	debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
			    &device_list_fops);
	debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
			    &blacklist_fops);
327 328
	debugfs_create_file("blocked_keys", 0444, hdev->debugfs, hdev,
			    &blocked_keys_fops);
329
	debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
330 331
	debugfs_create_file("remote_oob", 0400, hdev->debugfs, hdev,
			    &remote_oob_fops);
332 333 334 335 336

	debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev,
			    &conn_info_min_age_fops);
	debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev,
			    &conn_info_max_age_fops);
337

338 339 340 341
	if (lmp_ssp_capable(hdev) || lmp_le_capable(hdev))
		debugfs_create_file("use_debug_keys", 0444, hdev->debugfs,
				    hdev, &use_debug_keys_fops);

342 343 344
	if (lmp_sc_capable(hdev) || lmp_le_capable(hdev))
		debugfs_create_file("sc_only_mode", 0444, hdev->debugfs,
				    hdev, &sc_only_mode_fops);
345 346 347 348 349 350 351 352

	if (hdev->hw_info)
		debugfs_create_file("hardware_info", 0444, hdev->debugfs,
				    hdev, &hardware_info_fops);

	if (hdev->fw_info)
		debugfs_create_file("firmware_info", 0444, hdev->debugfs,
				    hdev, &firmware_info_fops);
353 354
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static int inquiry_cache_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;
	struct discovery_state *cache = &hdev->discovery;
	struct inquiry_entry *e;

	hci_dev_lock(hdev);

	list_for_each_entry(e, &cache->all, all) {
		struct inquiry_data *data = &e->data;
		seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
			   &data->bdaddr,
			   data->pscan_rep_mode, data->pscan_period_mode,
			   data->pscan_mode, data->dev_class[2],
			   data->dev_class[1], data->dev_class[0],
			   __le16_to_cpu(data->clock_offset),
			   data->rssi, data->ssp_mode, e->timestamp);
	}

	hci_dev_unlock(hdev);

	return 0;
}

379
DEFINE_SHOW_ATTRIBUTE(inquiry_cache);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

static int link_keys_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct link_key *key;

	rcu_read_lock();
	list_for_each_entry_rcu(key, &hdev->link_keys, list)
		seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
			   HCI_LINK_KEY_SIZE, key->val, key->pin_len);
	rcu_read_unlock();

	return 0;
}

395
DEFINE_SHOW_ATTRIBUTE(link_keys);
396 397 398 399 400 401 402 403 404 405 406 407 408

static int dev_class_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;

	hci_dev_lock(hdev);
	seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
		   hdev->dev_class[1], hdev->dev_class[0]);
	hci_dev_unlock(hdev);

	return 0;
}

409
DEFINE_SHOW_ATTRIBUTE(dev_class);
410 411 412 413 414 415 416 417 418 419 420 421

static int voice_setting_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->voice_setting;
	hci_dev_unlock(hdev);

	return 0;
}

422 423
DEFINE_DEBUGFS_ATTRIBUTE(voice_setting_fops, voice_setting_get,
			  NULL, "0x%4.4llx\n");
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
static ssize_t ssp_debug_mode_read(struct file *file, char __user *user_buf,
				   size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

	buf[0] = hdev->ssp_debug_mode ? 'Y': 'N';
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static const struct file_operations ssp_debug_mode_fops = {
	.open		= simple_open,
	.read		= ssp_debug_mode_read,
	.llseek		= default_llseek,
};

443 444 445 446 447 448 449 450 451 452 453
static int auto_accept_delay_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	hdev->auto_accept_delay = val;
	hci_dev_unlock(hdev);

	return 0;
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
static int min_encrypt_key_size_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 1 || val > 16)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->min_enc_key_size = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int min_encrypt_key_size_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->min_enc_key_size;
	hci_dev_unlock(hdev);

	return 0;
}

479 480 481
DEFINE_DEBUGFS_ATTRIBUTE(min_encrypt_key_size_fops,
			  min_encrypt_key_size_get,
			  min_encrypt_key_size_set, "%llu\n");
482

483 484 485 486 487 488 489 490 491 492 493
static int auto_accept_delay_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->auto_accept_delay;
	hci_dev_unlock(hdev);

	return 0;
}

494 495
DEFINE_DEBUGFS_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
			  auto_accept_delay_set, "%llu\n");
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static ssize_t force_bredr_smp_read(struct file *file,
				    char __user *user_buf,
				    size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y' : 'N';
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static ssize_t force_bredr_smp_write(struct file *file,
				     const char __user *user_buf,
				     size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	bool enable;
	int err;

	err = kstrtobool_from_user(user_buf, count, &enable);
	if (err)
		return err;

	err = smp_force_bredr(hdev, enable);
	if (err)
		return err;

	return count;
}

static const struct file_operations force_bredr_smp_fops = {
	.open		= simple_open,
	.read		= force_bredr_smp_read,
	.write		= force_bredr_smp_write,
	.llseek		= default_llseek,
};

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
static int idle_timeout_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val != 0 && (val < 500 || val > 3600000))
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->idle_timeout = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int idle_timeout_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->idle_timeout;
	hci_dev_unlock(hdev);

	return 0;
}

561 562
DEFINE_DEBUGFS_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
			  idle_timeout_set, "%llu\n");
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

static int sniff_min_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->sniff_min_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int sniff_min_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->sniff_min_interval;
	hci_dev_unlock(hdev);

	return 0;
}

589 590
DEFINE_DEBUGFS_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
			  sniff_min_interval_set, "%llu\n");
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

static int sniff_max_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->sniff_max_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int sniff_max_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->sniff_max_interval;
	hci_dev_unlock(hdev);

	return 0;
}

617 618
DEFINE_DEBUGFS_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
			  sniff_max_interval_set, "%llu\n");
619

620 621
void hci_debugfs_create_bredr(struct hci_dev *hdev)
{
622 623 624 625 626 627 628 629 630
	debugfs_create_file("inquiry_cache", 0444, hdev->debugfs, hdev,
			    &inquiry_cache_fops);
	debugfs_create_file("link_keys", 0400, hdev->debugfs, hdev,
			    &link_keys_fops);
	debugfs_create_file("dev_class", 0444, hdev->debugfs, hdev,
			    &dev_class_fops);
	debugfs_create_file("voice_setting", 0444, hdev->debugfs, hdev,
			    &voice_setting_fops);

631 632 633 634 635 636 637 638 639 640 641
	/* If the controller does not support BR/EDR Secure Connections
	 * feature, then the BR/EDR SMP channel shall not be present.
	 *
	 * To test this with Bluetooth 4.0 controllers, create a debugfs
	 * switch that allows forcing BR/EDR SMP support and accepting
	 * cross-transport pairing on non-AES encrypted connections.
	 */
	if (!lmp_sc_capable(hdev))
		debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
				    hdev, &force_bredr_smp_fops);

642 643 644
	if (lmp_ssp_capable(hdev)) {
		debugfs_create_file("ssp_debug_mode", 0444, hdev->debugfs,
				    hdev, &ssp_debug_mode_fops);
645 646
		debugfs_create_file("min_encrypt_key_size", 0644, hdev->debugfs,
				    hdev, &min_encrypt_key_size_fops);
647 648
		debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
				    hdev, &auto_accept_delay_fops);
649
	}
650 651 652 653 654 655 656 657 658

	if (lmp_sniff_capable(hdev)) {
		debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
				    hdev, &idle_timeout_fops);
		debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
				    hdev, &sniff_min_interval_fops);
		debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
				    hdev, &sniff_max_interval_fops);
	}
659 660
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
static int identity_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;
	bdaddr_t addr;
	u8 addr_type;

	hci_dev_lock(hdev);

	hci_copy_identity_address(hdev, &addr, &addr_type);

	seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,
		   16, hdev->irk, &hdev->rpa);

	hci_dev_unlock(hdev);

	return 0;
}

679
DEFINE_SHOW_ATTRIBUTE(identity);
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

static int rpa_timeout_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	/* Require the RPA timeout to be at least 30 seconds and at most
	 * 24 hours.
	 */
	if (val < 30 || val > (60 * 60 * 24))
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->rpa_timeout = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int rpa_timeout_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->rpa_timeout;
	hci_dev_unlock(hdev);

	return 0;
}

709 710
DEFINE_DEBUGFS_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
			  rpa_timeout_set, "%llu\n");
711 712 713 714 715 716 717 718 719 720 721 722

static int random_address_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;

	hci_dev_lock(hdev);
	seq_printf(f, "%pMR\n", &hdev->random_addr);
	hci_dev_unlock(hdev);

	return 0;
}

723
DEFINE_SHOW_ATTRIBUTE(random_address);
724 725 726 727 728 729 730 731 732 733 734 735

static int static_address_show(struct seq_file *f, void *p)
{
	struct hci_dev *hdev = f->private;

	hci_dev_lock(hdev);
	seq_printf(f, "%pMR\n", &hdev->static_addr);
	hci_dev_unlock(hdev);

	return 0;
}

736
DEFINE_SHOW_ATTRIBUTE(static_address);
737 738 739 740 741 742 743 744

static ssize_t force_static_address_read(struct file *file,
					 char __user *user_buf,
					 size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

745
	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ? 'Y': 'N';
746 747 748 749 750 751 752 753 754 755 756
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static ssize_t force_static_address_write(struct file *file,
					  const char __user *user_buf,
					  size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	bool enable;
757
	int err;
758 759 760 761

	if (test_bit(HCI_UP, &hdev->flags))
		return -EBUSY;

762 763 764
	err = kstrtobool_from_user(user_buf, count, &enable);
	if (err)
		return err;
765

766
	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
767 768
		return -EALREADY;

769
	hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

	return count;
}

static const struct file_operations force_static_address_fops = {
	.open		= simple_open,
	.read		= force_static_address_read,
	.write		= force_static_address_write,
	.llseek		= default_llseek,
};

static int white_list_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct bdaddr_list *b;

	hci_dev_lock(hdev);
	list_for_each_entry(b, &hdev->le_white_list, list)
		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
	hci_dev_unlock(hdev);

	return 0;
}

794
DEFINE_SHOW_ATTRIBUTE(white_list);
795

796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static int resolv_list_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct bdaddr_list *b;

	hci_dev_lock(hdev);
	list_for_each_entry(b, &hdev->le_resolv_list, list)
		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
	hci_dev_unlock(hdev);

	return 0;
}

DEFINE_SHOW_ATTRIBUTE(resolv_list);

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct smp_irk *irk;

	rcu_read_lock();
	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
		seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
			   &irk->bdaddr, irk->addr_type,
			   16, irk->val, &irk->rpa);
	}
	rcu_read_unlock();

	return 0;
}

827
DEFINE_SHOW_ATTRIBUTE(identity_resolving_keys);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

static int long_term_keys_show(struct seq_file *f, void *ptr)
{
	struct hci_dev *hdev = f->private;
	struct smp_ltk *ltk;

	rcu_read_lock();
	list_for_each_entry_rcu(ltk, &hdev->long_term_keys, list)
		seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
			   &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
			   ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
			   __le64_to_cpu(ltk->rand), 16, ltk->val);
	rcu_read_unlock();

	return 0;
}

845
DEFINE_SHOW_ATTRIBUTE(long_term_keys);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

static int conn_min_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_conn_min_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int conn_min_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_conn_min_interval;
	hci_dev_unlock(hdev);

	return 0;
}

872 873
DEFINE_DEBUGFS_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
			  conn_min_interval_set, "%llu\n");
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

static int conn_max_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_conn_max_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int conn_max_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_conn_max_interval;
	hci_dev_unlock(hdev);

	return 0;
}

900 901
DEFINE_DEBUGFS_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
			  conn_max_interval_set, "%llu\n");
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

static int conn_latency_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val > 0x01f3)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_conn_latency = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int conn_latency_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_conn_latency;
	hci_dev_unlock(hdev);

	return 0;
}

928 929
DEFINE_DEBUGFS_ATTRIBUTE(conn_latency_fops, conn_latency_get,
			  conn_latency_set, "%llu\n");
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

static int supervision_timeout_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x000a || val > 0x0c80)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_supv_timeout = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int supervision_timeout_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_supv_timeout;
	hci_dev_unlock(hdev);

	return 0;
}

956 957
DEFINE_DEBUGFS_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
			  supervision_timeout_set, "%llu\n");
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

static int adv_channel_map_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x01 || val > 0x07)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_adv_channel_map = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int adv_channel_map_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_adv_channel_map;
	hci_dev_unlock(hdev);

	return 0;
}

984 985
DEFINE_DEBUGFS_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
			  adv_channel_map_set, "%llu\n");
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

static int adv_min_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x0020 || val > 0x4000 || val > hdev->le_adv_max_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_adv_min_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int adv_min_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_adv_min_interval;
	hci_dev_unlock(hdev);

	return 0;
}

1012 1013
DEFINE_DEBUGFS_ATTRIBUTE(adv_min_interval_fops, adv_min_interval_get,
			  adv_min_interval_set, "%llu\n");
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

static int adv_max_interval_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x0020 || val > 0x4000 || val < hdev->le_adv_min_interval)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_adv_max_interval = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int adv_max_interval_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_adv_max_interval;
	hci_dev_unlock(hdev);

	return 0;
}

1040 1041
DEFINE_DEBUGFS_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
			  adv_max_interval_set, "%llu\n");
1042

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
static int min_key_size_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val > hdev->le_max_key_size || val < SMP_MIN_ENC_KEY_SIZE)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_min_key_size = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int min_key_size_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_min_key_size;
	hci_dev_unlock(hdev);

	return 0;
}

1068 1069
DEFINE_DEBUGFS_ATTRIBUTE(min_key_size_fops, min_key_size_get,
			  min_key_size_set, "%llu\n");
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

static int max_key_size_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val > SMP_MAX_ENC_KEY_SIZE || val < hdev->le_min_key_size)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->le_max_key_size = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int max_key_size_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->le_max_key_size;
	hci_dev_unlock(hdev);

	return 0;
}

1096 1097
DEFINE_DEBUGFS_ATTRIBUTE(max_key_size_fops, max_key_size_get,
			  max_key_size_set, "%llu\n");
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
static int auth_payload_timeout_set(void *data, u64 val)
{
	struct hci_dev *hdev = data;

	if (val < 0x0001 || val > 0xffff)
		return -EINVAL;

	hci_dev_lock(hdev);
	hdev->auth_payload_timeout = val;
	hci_dev_unlock(hdev);

	return 0;
}

static int auth_payload_timeout_get(void *data, u64 *val)
{
	struct hci_dev *hdev = data;

	hci_dev_lock(hdev);
	*val = hdev->auth_payload_timeout;
	hci_dev_unlock(hdev);

	return 0;
}

1124 1125 1126
DEFINE_DEBUGFS_ATTRIBUTE(auth_payload_timeout_fops,
			  auth_payload_timeout_get,
			  auth_payload_timeout_set, "%llu\n");
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
static ssize_t force_no_mitm_read(struct file *file,
				  char __user *user_buf,
				  size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[3];

	buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM) ? 'Y' : 'N';
	buf[1] = '\n';
	buf[2] = '\0';
	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
}

static ssize_t force_no_mitm_write(struct file *file,
				   const char __user *user_buf,
				   size_t count, loff_t *ppos)
{
	struct hci_dev *hdev = file->private_data;
	char buf[32];
	size_t buf_size = min(count, (sizeof(buf) - 1));
	bool enable;

	if (copy_from_user(buf, user_buf, buf_size))
		return -EFAULT;

	buf[buf_size] = '\0';
	if (strtobool(buf, &enable))
		return -EINVAL;

	if (enable == hci_dev_test_flag(hdev, HCI_FORCE_NO_MITM))
		return -EALREADY;

	hci_dev_change_flag(hdev, HCI_FORCE_NO_MITM);

	return count;
}

static const struct file_operations force_no_mitm_fops = {
	.open		= simple_open,
	.read		= force_no_mitm_read,
	.write		= force_no_mitm_write,
	.llseek		= default_llseek,
};

1172 1173 1174 1175 1176
DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
		       HCI_QUIRK_STRICT_DUPLICATE_FILTER);
DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
		       HCI_QUIRK_SIMULTANEOUS_DISCOVERY);

1177 1178
void hci_debugfs_create_le(struct hci_dev *hdev)
{
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
			    &identity_fops);
	debugfs_create_file("rpa_timeout", 0644, hdev->debugfs, hdev,
			    &rpa_timeout_fops);
	debugfs_create_file("random_address", 0444, hdev->debugfs, hdev,
			    &random_address_fops);
	debugfs_create_file("static_address", 0444, hdev->debugfs, hdev,
			    &static_address_fops);

	/* For controllers with a public address, provide a debug
	 * option to force the usage of the configured static
	 * address. By default the public address is used.
	 */
	if (bacmp(&hdev->bdaddr, BDADDR_ANY))
		debugfs_create_file("force_static_address", 0644,
				    hdev->debugfs, hdev,
				    &force_static_address_fops);

	debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
			  &hdev->le_white_list_size);
	debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
			    &white_list_fops);
1201 1202 1203 1204
	debugfs_create_u8("resolv_list_size", 0444, hdev->debugfs,
			  &hdev->le_resolv_list_size);
	debugfs_create_file("resolv_list", 0444, hdev->debugfs, hdev,
			    &resolv_list_fops);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
	debugfs_create_file("identity_resolving_keys", 0400, hdev->debugfs,
			    hdev, &identity_resolving_keys_fops);
	debugfs_create_file("long_term_keys", 0400, hdev->debugfs, hdev,
			    &long_term_keys_fops);
	debugfs_create_file("conn_min_interval", 0644, hdev->debugfs, hdev,
			    &conn_min_interval_fops);
	debugfs_create_file("conn_max_interval", 0644, hdev->debugfs, hdev,
			    &conn_max_interval_fops);
	debugfs_create_file("conn_latency", 0644, hdev->debugfs, hdev,
			    &conn_latency_fops);
	debugfs_create_file("supervision_timeout", 0644, hdev->debugfs, hdev,
			    &supervision_timeout_fops);
	debugfs_create_file("adv_channel_map", 0644, hdev->debugfs, hdev,
			    &adv_channel_map_fops);
	debugfs_create_file("adv_min_interval", 0644, hdev->debugfs, hdev,
			    &adv_min_interval_fops);
	debugfs_create_file("adv_max_interval", 0644, hdev->debugfs, hdev,
			    &adv_max_interval_fops);
	debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
			   &hdev->discov_interleaved_timeout);
1225 1226 1227 1228
	debugfs_create_file("min_key_size", 0644, hdev->debugfs, hdev,
			    &min_key_size_fops);
	debugfs_create_file("max_key_size", 0644, hdev->debugfs, hdev,
			    &max_key_size_fops);
1229 1230
	debugfs_create_file("auth_payload_timeout", 0644, hdev->debugfs, hdev,
			    &auth_payload_timeout_fops);
1231 1232
	debugfs_create_file("force_no_mitm", 0644, hdev->debugfs, hdev,
			    &force_no_mitm_fops);
1233 1234 1235 1236 1237 1238 1239

	debugfs_create_file("quirk_strict_duplicate_filter", 0644,
			    hdev->debugfs, hdev,
			    &quirk_strict_duplicate_filter_fops);
	debugfs_create_file("quirk_simultaneous_discovery", 0644,
			    hdev->debugfs, hdev,
			    &quirk_simultaneous_discovery_fops);
1240
}
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

void hci_debugfs_create_conn(struct hci_conn *conn)
{
	struct hci_dev *hdev = conn->hdev;
	char name[6];

	if (IS_ERR_OR_NULL(hdev->debugfs))
		return;

	snprintf(name, sizeof(name), "%u", conn->handle);
	conn->debugfs = debugfs_create_dir(name, hdev->debugfs);
}