hci_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/debugfs.h>
  3. #include <linux/module.h>
  4. #include <asm/unaligned.h>
  5. #include <net/bluetooth/bluetooth.h>
  6. #include <net/bluetooth/hci_core.h>
  7. static struct class *bt_class;
  8. struct dentry *bt_debugfs;
  9. EXPORT_SYMBOL_GPL(bt_debugfs);
  10. static inline char *link_typetostr(int type)
  11. {
  12. switch (type) {
  13. case ACL_LINK:
  14. return "ACL";
  15. case SCO_LINK:
  16. return "SCO";
  17. case ESCO_LINK:
  18. return "eSCO";
  19. case LE_LINK:
  20. return "LE";
  21. default:
  22. return "UNKNOWN";
  23. }
  24. }
  25. static ssize_t show_link_type(struct device *dev,
  26. struct device_attribute *attr, char *buf)
  27. {
  28. struct hci_conn *conn = to_hci_conn(dev);
  29. return sprintf(buf, "%s\n", link_typetostr(conn->type));
  30. }
  31. static ssize_t show_link_address(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct hci_conn *conn = to_hci_conn(dev);
  35. return sprintf(buf, "%pMR\n", &conn->dst);
  36. }
  37. static ssize_t show_link_features(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct hci_conn *conn = to_hci_conn(dev);
  41. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  42. conn->features[0][0], conn->features[0][1],
  43. conn->features[0][2], conn->features[0][3],
  44. conn->features[0][4], conn->features[0][5],
  45. conn->features[0][6], conn->features[0][7]);
  46. }
  47. #define LINK_ATTR(_name, _mode, _show, _store) \
  48. struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
  49. static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
  50. static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
  51. static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
  52. static struct attribute *bt_link_attrs[] = {
  53. &link_attr_type.attr,
  54. &link_attr_address.attr,
  55. &link_attr_features.attr,
  56. NULL
  57. };
  58. static struct attribute_group bt_link_group = {
  59. .attrs = bt_link_attrs,
  60. };
  61. static const struct attribute_group *bt_link_groups[] = {
  62. &bt_link_group,
  63. NULL
  64. };
  65. static void bt_link_release(struct device *dev)
  66. {
  67. struct hci_conn *conn = to_hci_conn(dev);
  68. kfree(conn);
  69. }
  70. static struct device_type bt_link = {
  71. .name = "link",
  72. .groups = bt_link_groups,
  73. .release = bt_link_release,
  74. };
  75. /*
  76. * The rfcomm tty device will possibly retain even when conn
  77. * is down, and sysfs doesn't support move zombie device,
  78. * so we should move the device before conn device is destroyed.
  79. */
  80. static int __match_tty(struct device *dev, void *data)
  81. {
  82. return !strncmp(dev_name(dev), "rfcomm", 6);
  83. }
  84. void hci_conn_init_sysfs(struct hci_conn *conn)
  85. {
  86. struct hci_dev *hdev = conn->hdev;
  87. BT_DBG("conn %p", conn);
  88. conn->dev.type = &bt_link;
  89. conn->dev.class = bt_class;
  90. conn->dev.parent = &hdev->dev;
  91. device_initialize(&conn->dev);
  92. }
  93. void hci_conn_add_sysfs(struct hci_conn *conn)
  94. {
  95. struct hci_dev *hdev = conn->hdev;
  96. BT_DBG("conn %p", conn);
  97. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  98. if (device_add(&conn->dev) < 0) {
  99. BT_ERR("Failed to register connection device");
  100. return;
  101. }
  102. hci_dev_hold(hdev);
  103. }
  104. void hci_conn_del_sysfs(struct hci_conn *conn)
  105. {
  106. struct hci_dev *hdev = conn->hdev;
  107. if (!device_is_registered(&conn->dev))
  108. return;
  109. while (1) {
  110. struct device *dev;
  111. dev = device_find_child(&conn->dev, NULL, __match_tty);
  112. if (!dev)
  113. break;
  114. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  115. put_device(dev);
  116. }
  117. device_del(&conn->dev);
  118. hci_dev_put(hdev);
  119. }
  120. static inline char *host_bustostr(int bus)
  121. {
  122. switch (bus) {
  123. case HCI_VIRTUAL:
  124. return "VIRTUAL";
  125. case HCI_USB:
  126. return "USB";
  127. case HCI_PCCARD:
  128. return "PCCARD";
  129. case HCI_UART:
  130. return "UART";
  131. case HCI_RS232:
  132. return "RS232";
  133. case HCI_PCI:
  134. return "PCI";
  135. case HCI_SDIO:
  136. return "SDIO";
  137. default:
  138. return "UNKNOWN";
  139. }
  140. }
  141. static inline char *host_typetostr(int type)
  142. {
  143. switch (type) {
  144. case HCI_BREDR:
  145. return "BR/EDR";
  146. case HCI_AMP:
  147. return "AMP";
  148. default:
  149. return "UNKNOWN";
  150. }
  151. }
  152. static ssize_t show_bus(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct hci_dev *hdev = to_hci_dev(dev);
  156. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  157. }
  158. static ssize_t show_type(struct device *dev,
  159. struct device_attribute *attr, char *buf)
  160. {
  161. struct hci_dev *hdev = to_hci_dev(dev);
  162. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  163. }
  164. static ssize_t show_name(struct device *dev,
  165. struct device_attribute *attr, char *buf)
  166. {
  167. struct hci_dev *hdev = to_hci_dev(dev);
  168. char name[HCI_MAX_NAME_LENGTH + 1];
  169. int i;
  170. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  171. name[i] = hdev->dev_name[i];
  172. name[HCI_MAX_NAME_LENGTH] = '\0';
  173. return sprintf(buf, "%s\n", name);
  174. }
  175. static ssize_t show_class(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct hci_dev *hdev = to_hci_dev(dev);
  179. return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
  180. hdev->dev_class[1], hdev->dev_class[0]);
  181. }
  182. static ssize_t show_address(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. struct hci_dev *hdev = to_hci_dev(dev);
  186. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  187. }
  188. static ssize_t show_features(struct device *dev,
  189. struct device_attribute *attr, char *buf)
  190. {
  191. struct hci_dev *hdev = to_hci_dev(dev);
  192. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  193. hdev->features[0][0], hdev->features[0][1],
  194. hdev->features[0][2], hdev->features[0][3],
  195. hdev->features[0][4], hdev->features[0][5],
  196. hdev->features[0][6], hdev->features[0][7]);
  197. }
  198. static ssize_t show_manufacturer(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct hci_dev *hdev = to_hci_dev(dev);
  202. return sprintf(buf, "%d\n", hdev->manufacturer);
  203. }
  204. static ssize_t show_hci_version(struct device *dev,
  205. struct device_attribute *attr, char *buf)
  206. {
  207. struct hci_dev *hdev = to_hci_dev(dev);
  208. return sprintf(buf, "%d\n", hdev->hci_ver);
  209. }
  210. static ssize_t show_hci_revision(struct device *dev,
  211. struct device_attribute *attr, char *buf)
  212. {
  213. struct hci_dev *hdev = to_hci_dev(dev);
  214. return sprintf(buf, "%d\n", hdev->hci_rev);
  215. }
  216. static ssize_t show_idle_timeout(struct device *dev,
  217. struct device_attribute *attr, char *buf)
  218. {
  219. struct hci_dev *hdev = to_hci_dev(dev);
  220. return sprintf(buf, "%d\n", hdev->idle_timeout);
  221. }
  222. static ssize_t store_idle_timeout(struct device *dev,
  223. struct device_attribute *attr,
  224. const char *buf, size_t count)
  225. {
  226. struct hci_dev *hdev = to_hci_dev(dev);
  227. unsigned int val;
  228. int rv;
  229. rv = kstrtouint(buf, 0, &val);
  230. if (rv < 0)
  231. return rv;
  232. if (val != 0 && (val < 500 || val > 3600000))
  233. return -EINVAL;
  234. hdev->idle_timeout = val;
  235. return count;
  236. }
  237. static ssize_t show_sniff_max_interval(struct device *dev,
  238. struct device_attribute *attr, char *buf)
  239. {
  240. struct hci_dev *hdev = to_hci_dev(dev);
  241. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  242. }
  243. static ssize_t store_sniff_max_interval(struct device *dev,
  244. struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct hci_dev *hdev = to_hci_dev(dev);
  248. u16 val;
  249. int rv;
  250. rv = kstrtou16(buf, 0, &val);
  251. if (rv < 0)
  252. return rv;
  253. if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
  254. return -EINVAL;
  255. hdev->sniff_max_interval = val;
  256. return count;
  257. }
  258. static ssize_t show_sniff_min_interval(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct hci_dev *hdev = to_hci_dev(dev);
  262. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  263. }
  264. static ssize_t store_sniff_min_interval(struct device *dev,
  265. struct device_attribute *attr,
  266. const char *buf, size_t count)
  267. {
  268. struct hci_dev *hdev = to_hci_dev(dev);
  269. u16 val;
  270. int rv;
  271. rv = kstrtou16(buf, 0, &val);
  272. if (rv < 0)
  273. return rv;
  274. if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
  275. return -EINVAL;
  276. hdev->sniff_min_interval = val;
  277. return count;
  278. }
  279. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  280. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  281. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  282. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  283. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  284. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  285. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  286. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  287. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  288. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  289. show_idle_timeout, store_idle_timeout);
  290. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  291. show_sniff_max_interval, store_sniff_max_interval);
  292. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  293. show_sniff_min_interval, store_sniff_min_interval);
  294. static struct attribute *bt_host_attrs[] = {
  295. &dev_attr_bus.attr,
  296. &dev_attr_type.attr,
  297. &dev_attr_name.attr,
  298. &dev_attr_class.attr,
  299. &dev_attr_address.attr,
  300. &dev_attr_features.attr,
  301. &dev_attr_manufacturer.attr,
  302. &dev_attr_hci_version.attr,
  303. &dev_attr_hci_revision.attr,
  304. &dev_attr_idle_timeout.attr,
  305. &dev_attr_sniff_max_interval.attr,
  306. &dev_attr_sniff_min_interval.attr,
  307. NULL
  308. };
  309. static struct attribute_group bt_host_group = {
  310. .attrs = bt_host_attrs,
  311. };
  312. static const struct attribute_group *bt_host_groups[] = {
  313. &bt_host_group,
  314. NULL
  315. };
  316. static void bt_host_release(struct device *dev)
  317. {
  318. struct hci_dev *hdev = to_hci_dev(dev);
  319. kfree(hdev);
  320. module_put(THIS_MODULE);
  321. }
  322. static struct device_type bt_host = {
  323. .name = "host",
  324. .groups = bt_host_groups,
  325. .release = bt_host_release,
  326. };
  327. static int inquiry_cache_show(struct seq_file *f, void *p)
  328. {
  329. struct hci_dev *hdev = f->private;
  330. struct discovery_state *cache = &hdev->discovery;
  331. struct inquiry_entry *e;
  332. hci_dev_lock(hdev);
  333. list_for_each_entry(e, &cache->all, all) {
  334. struct inquiry_data *data = &e->data;
  335. seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  336. &data->bdaddr,
  337. data->pscan_rep_mode, data->pscan_period_mode,
  338. data->pscan_mode, data->dev_class[2],
  339. data->dev_class[1], data->dev_class[0],
  340. __le16_to_cpu(data->clock_offset),
  341. data->rssi, data->ssp_mode, e->timestamp);
  342. }
  343. hci_dev_unlock(hdev);
  344. return 0;
  345. }
  346. static int inquiry_cache_open(struct inode *inode, struct file *file)
  347. {
  348. return single_open(file, inquiry_cache_show, inode->i_private);
  349. }
  350. static const struct file_operations inquiry_cache_fops = {
  351. .open = inquiry_cache_open,
  352. .read = seq_read,
  353. .llseek = seq_lseek,
  354. .release = single_release,
  355. };
  356. static int blacklist_show(struct seq_file *f, void *p)
  357. {
  358. struct hci_dev *hdev = f->private;
  359. struct bdaddr_list *b;
  360. hci_dev_lock(hdev);
  361. list_for_each_entry(b, &hdev->blacklist, list)
  362. seq_printf(f, "%pMR\n", &b->bdaddr);
  363. hci_dev_unlock(hdev);
  364. return 0;
  365. }
  366. static int blacklist_open(struct inode *inode, struct file *file)
  367. {
  368. return single_open(file, blacklist_show, inode->i_private);
  369. }
  370. static const struct file_operations blacklist_fops = {
  371. .open = blacklist_open,
  372. .read = seq_read,
  373. .llseek = seq_lseek,
  374. .release = single_release,
  375. };
  376. static void print_bt_uuid(struct seq_file *f, u8 *uuid)
  377. {
  378. u32 data0, data5;
  379. u16 data1, data2, data3, data4;
  380. data5 = get_unaligned_le32(uuid);
  381. data4 = get_unaligned_le16(uuid + 4);
  382. data3 = get_unaligned_le16(uuid + 6);
  383. data2 = get_unaligned_le16(uuid + 8);
  384. data1 = get_unaligned_le16(uuid + 10);
  385. data0 = get_unaligned_le32(uuid + 12);
  386. seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.4x%.8x\n",
  387. data0, data1, data2, data3, data4, data5);
  388. }
  389. static int uuids_show(struct seq_file *f, void *p)
  390. {
  391. struct hci_dev *hdev = f->private;
  392. struct bt_uuid *uuid;
  393. hci_dev_lock(hdev);
  394. list_for_each_entry(uuid, &hdev->uuids, list)
  395. print_bt_uuid(f, uuid->uuid);
  396. hci_dev_unlock(hdev);
  397. return 0;
  398. }
  399. static int uuids_open(struct inode *inode, struct file *file)
  400. {
  401. return single_open(file, uuids_show, inode->i_private);
  402. }
  403. static const struct file_operations uuids_fops = {
  404. .open = uuids_open,
  405. .read = seq_read,
  406. .llseek = seq_lseek,
  407. .release = single_release,
  408. };
  409. static int auto_accept_delay_set(void *data, u64 val)
  410. {
  411. struct hci_dev *hdev = data;
  412. hci_dev_lock(hdev);
  413. hdev->auto_accept_delay = val;
  414. hci_dev_unlock(hdev);
  415. return 0;
  416. }
  417. static int auto_accept_delay_get(void *data, u64 *val)
  418. {
  419. struct hci_dev *hdev = data;
  420. hci_dev_lock(hdev);
  421. *val = hdev->auto_accept_delay;
  422. hci_dev_unlock(hdev);
  423. return 0;
  424. }
  425. DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
  426. auto_accept_delay_set, "%llu\n");
  427. void hci_init_sysfs(struct hci_dev *hdev)
  428. {
  429. struct device *dev = &hdev->dev;
  430. dev->type = &bt_host;
  431. dev->class = bt_class;
  432. __module_get(THIS_MODULE);
  433. device_initialize(dev);
  434. }
  435. int hci_add_sysfs(struct hci_dev *hdev)
  436. {
  437. struct device *dev = &hdev->dev;
  438. int err;
  439. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  440. dev_set_name(dev, "%s", hdev->name);
  441. err = device_add(dev);
  442. if (err < 0)
  443. return err;
  444. if (!bt_debugfs)
  445. return 0;
  446. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  447. if (!hdev->debugfs)
  448. return 0;
  449. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  450. hdev, &inquiry_cache_fops);
  451. debugfs_create_file("blacklist", 0444, hdev->debugfs,
  452. hdev, &blacklist_fops);
  453. debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
  454. debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
  455. &auto_accept_delay_fops);
  456. return 0;
  457. }
  458. void hci_del_sysfs(struct hci_dev *hdev)
  459. {
  460. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  461. debugfs_remove_recursive(hdev->debugfs);
  462. device_del(&hdev->dev);
  463. }
  464. int __init bt_sysfs_init(void)
  465. {
  466. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  467. bt_class = class_create(THIS_MODULE, "bluetooth");
  468. return PTR_RET(bt_class);
  469. }
  470. void bt_sysfs_cleanup(void)
  471. {
  472. class_destroy(bt_class);
  473. debugfs_remove_recursive(bt_debugfs);
  474. }