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