hci_sysfs.c 14 KB

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