hci_sysfs.c 13 KB

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