hci_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, struct device_attribute *attr, char *buf)
  29. {
  30. struct hci_conn *conn = dev_get_drvdata(dev);
  31. return sprintf(buf, "%s\n", link_typetostr(conn->type));
  32. }
  33. static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
  34. {
  35. struct hci_conn *conn = dev_get_drvdata(dev);
  36. return sprintf(buf, "%s\n", batostr(&conn->dst));
  37. }
  38. static ssize_t show_link_features(struct device *dev, struct device_attribute *attr, char *buf)
  39. {
  40. struct hci_conn *conn = dev_get_drvdata(dev);
  41. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  42. conn->features[0], conn->features[1],
  43. conn->features[2], conn->features[3],
  44. conn->features[4], conn->features[5],
  45. conn->features[6], conn->features[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. void *data = dev_get_drvdata(dev);
  68. kfree(data);
  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. dev_set_drvdata(&conn->dev, conn);
  99. if (device_add(&conn->dev) < 0) {
  100. BT_ERR("Failed to register connection device");
  101. return;
  102. }
  103. hci_dev_hold(hdev);
  104. }
  105. void hci_conn_del_sysfs(struct hci_conn *conn)
  106. {
  107. struct hci_dev *hdev = conn->hdev;
  108. if (!device_is_registered(&conn->dev))
  109. return;
  110. while (1) {
  111. struct device *dev;
  112. dev = device_find_child(&conn->dev, NULL, __match_tty);
  113. if (!dev)
  114. break;
  115. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  116. put_device(dev);
  117. }
  118. device_del(&conn->dev);
  119. put_device(&conn->dev);
  120. hci_dev_put(hdev);
  121. }
  122. static inline char *host_bustostr(int bus)
  123. {
  124. switch (bus) {
  125. case HCI_VIRTUAL:
  126. return "VIRTUAL";
  127. case HCI_USB:
  128. return "USB";
  129. case HCI_PCCARD:
  130. return "PCCARD";
  131. case HCI_UART:
  132. return "UART";
  133. case HCI_RS232:
  134. return "RS232";
  135. case HCI_PCI:
  136. return "PCI";
  137. case HCI_SDIO:
  138. return "SDIO";
  139. default:
  140. return "UNKNOWN";
  141. }
  142. }
  143. static inline char *host_typetostr(int type)
  144. {
  145. switch (type) {
  146. case HCI_BREDR:
  147. return "BR/EDR";
  148. case HCI_AMP:
  149. return "AMP";
  150. default:
  151. return "UNKNOWN";
  152. }
  153. }
  154. static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
  155. {
  156. struct hci_dev *hdev = dev_get_drvdata(dev);
  157. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  158. }
  159. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  160. {
  161. struct hci_dev *hdev = dev_get_drvdata(dev);
  162. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  163. }
  164. static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
  165. {
  166. struct hci_dev *hdev = dev_get_drvdata(dev);
  167. char name[HCI_MAX_NAME_LENGTH + 1];
  168. int i;
  169. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  170. name[i] = hdev->dev_name[i];
  171. name[HCI_MAX_NAME_LENGTH] = '\0';
  172. return sprintf(buf, "%s\n", name);
  173. }
  174. static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
  175. {
  176. struct hci_dev *hdev = dev_get_drvdata(dev);
  177. return sprintf(buf, "0x%.2x%.2x%.2x\n",
  178. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  179. }
  180. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  181. {
  182. struct hci_dev *hdev = dev_get_drvdata(dev);
  183. return sprintf(buf, "%s\n", batostr(&hdev->bdaddr));
  184. }
  185. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  186. {
  187. struct hci_dev *hdev = dev_get_drvdata(dev);
  188. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  189. hdev->features[0], hdev->features[1],
  190. hdev->features[2], hdev->features[3],
  191. hdev->features[4], hdev->features[5],
  192. hdev->features[6], hdev->features[7]);
  193. }
  194. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  195. {
  196. struct hci_dev *hdev = dev_get_drvdata(dev);
  197. return sprintf(buf, "%d\n", hdev->manufacturer);
  198. }
  199. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  200. {
  201. struct hci_dev *hdev = dev_get_drvdata(dev);
  202. return sprintf(buf, "%d\n", hdev->hci_ver);
  203. }
  204. static ssize_t show_hci_revision(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->hci_rev);
  208. }
  209. static ssize_t show_idle_timeout(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->idle_timeout);
  213. }
  214. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  215. {
  216. struct hci_dev *hdev = dev_get_drvdata(dev);
  217. unsigned int val;
  218. int rv;
  219. rv = kstrtouint(buf, 0, &val);
  220. if (rv < 0)
  221. return rv;
  222. if (val != 0 && (val < 500 || val > 3600000))
  223. return -EINVAL;
  224. hdev->idle_timeout = val;
  225. return count;
  226. }
  227. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  228. {
  229. struct hci_dev *hdev = dev_get_drvdata(dev);
  230. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  231. }
  232. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  233. {
  234. struct hci_dev *hdev = dev_get_drvdata(dev);
  235. u16 val;
  236. int rv;
  237. rv = kstrtou16(buf, 0, &val);
  238. if (rv < 0)
  239. return rv;
  240. if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
  241. return -EINVAL;
  242. hdev->sniff_max_interval = val;
  243. return count;
  244. }
  245. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct hci_dev *hdev = dev_get_drvdata(dev);
  248. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  249. }
  250. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  251. {
  252. struct hci_dev *hdev = dev_get_drvdata(dev);
  253. u16 val;
  254. int rv;
  255. rv = kstrtou16(buf, 0, &val);
  256. if (rv < 0)
  257. return rv;
  258. if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
  259. return -EINVAL;
  260. hdev->sniff_min_interval = val;
  261. return count;
  262. }
  263. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  264. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  265. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  266. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  267. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  268. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  269. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  270. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  271. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  272. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  273. show_idle_timeout, store_idle_timeout);
  274. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  275. show_sniff_max_interval, store_sniff_max_interval);
  276. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  277. show_sniff_min_interval, store_sniff_min_interval);
  278. static struct attribute *bt_host_attrs[] = {
  279. &dev_attr_bus.attr,
  280. &dev_attr_type.attr,
  281. &dev_attr_name.attr,
  282. &dev_attr_class.attr,
  283. &dev_attr_address.attr,
  284. &dev_attr_features.attr,
  285. &dev_attr_manufacturer.attr,
  286. &dev_attr_hci_version.attr,
  287. &dev_attr_hci_revision.attr,
  288. &dev_attr_idle_timeout.attr,
  289. &dev_attr_sniff_max_interval.attr,
  290. &dev_attr_sniff_min_interval.attr,
  291. NULL
  292. };
  293. static struct attribute_group bt_host_group = {
  294. .attrs = bt_host_attrs,
  295. };
  296. static const struct attribute_group *bt_host_groups[] = {
  297. &bt_host_group,
  298. NULL
  299. };
  300. static void bt_host_release(struct device *dev)
  301. {
  302. void *data = dev_get_drvdata(dev);
  303. kfree(data);
  304. }
  305. static struct device_type bt_host = {
  306. .name = "host",
  307. .groups = bt_host_groups,
  308. .release = bt_host_release,
  309. };
  310. static int inquiry_cache_show(struct seq_file *f, void *p)
  311. {
  312. struct hci_dev *hdev = f->private;
  313. struct inquiry_cache *cache = &hdev->inq_cache;
  314. struct inquiry_entry *e;
  315. hci_dev_lock(hdev);
  316. for (e = cache->list; e; e = e->next) {
  317. struct inquiry_data *data = &e->data;
  318. seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  319. batostr(&data->bdaddr),
  320. data->pscan_rep_mode, data->pscan_period_mode,
  321. data->pscan_mode, data->dev_class[2],
  322. data->dev_class[1], data->dev_class[0],
  323. __le16_to_cpu(data->clock_offset),
  324. data->rssi, data->ssp_mode, e->timestamp);
  325. }
  326. hci_dev_unlock(hdev);
  327. return 0;
  328. }
  329. static int inquiry_cache_open(struct inode *inode, struct file *file)
  330. {
  331. return single_open(file, inquiry_cache_show, inode->i_private);
  332. }
  333. static const struct file_operations inquiry_cache_fops = {
  334. .open = inquiry_cache_open,
  335. .read = seq_read,
  336. .llseek = seq_lseek,
  337. .release = single_release,
  338. };
  339. static int blacklist_show(struct seq_file *f, void *p)
  340. {
  341. struct hci_dev *hdev = f->private;
  342. struct bdaddr_list *b;
  343. hci_dev_lock(hdev);
  344. list_for_each_entry(b, &hdev->blacklist, list)
  345. seq_printf(f, "%s\n", batostr(&b->bdaddr));
  346. hci_dev_unlock(hdev);
  347. return 0;
  348. }
  349. static int blacklist_open(struct inode *inode, struct file *file)
  350. {
  351. return single_open(file, blacklist_show, inode->i_private);
  352. }
  353. static const struct file_operations blacklist_fops = {
  354. .open = blacklist_open,
  355. .read = seq_read,
  356. .llseek = seq_lseek,
  357. .release = single_release,
  358. };
  359. static void print_bt_uuid(struct seq_file *f, u8 *uuid)
  360. {
  361. u32 data0, data4;
  362. u16 data1, data2, data3, data5;
  363. memcpy(&data0, &uuid[0], 4);
  364. memcpy(&data1, &uuid[4], 2);
  365. memcpy(&data2, &uuid[6], 2);
  366. memcpy(&data3, &uuid[8], 2);
  367. memcpy(&data4, &uuid[10], 4);
  368. memcpy(&data5, &uuid[14], 2);
  369. seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x\n",
  370. ntohl(data0), ntohs(data1), ntohs(data2),
  371. ntohs(data3), ntohl(data4), ntohs(data5));
  372. }
  373. static int uuids_show(struct seq_file *f, void *p)
  374. {
  375. struct hci_dev *hdev = f->private;
  376. struct bt_uuid *uuid;
  377. hci_dev_lock(hdev);
  378. list_for_each_entry(uuid, &hdev->uuids, list)
  379. print_bt_uuid(f, uuid->uuid);
  380. hci_dev_unlock(hdev);
  381. return 0;
  382. }
  383. static int uuids_open(struct inode *inode, struct file *file)
  384. {
  385. return single_open(file, uuids_show, inode->i_private);
  386. }
  387. static const struct file_operations uuids_fops = {
  388. .open = uuids_open,
  389. .read = seq_read,
  390. .llseek = seq_lseek,
  391. .release = single_release,
  392. };
  393. static int auto_accept_delay_set(void *data, u64 val)
  394. {
  395. struct hci_dev *hdev = data;
  396. hci_dev_lock(hdev);
  397. hdev->auto_accept_delay = val;
  398. hci_dev_unlock(hdev);
  399. return 0;
  400. }
  401. static int auto_accept_delay_get(void *data, u64 *val)
  402. {
  403. struct hci_dev *hdev = data;
  404. hci_dev_lock(hdev);
  405. *val = hdev->auto_accept_delay;
  406. hci_dev_unlock(hdev);
  407. return 0;
  408. }
  409. DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
  410. auto_accept_delay_set, "%llu\n");
  411. void hci_init_sysfs(struct hci_dev *hdev)
  412. {
  413. struct device *dev = &hdev->dev;
  414. dev->type = &bt_host;
  415. dev->class = bt_class;
  416. dev_set_drvdata(dev, hdev);
  417. device_initialize(dev);
  418. }
  419. int hci_add_sysfs(struct hci_dev *hdev)
  420. {
  421. struct device *dev = &hdev->dev;
  422. int err;
  423. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  424. dev->parent = hdev->parent;
  425. dev_set_name(dev, "%s", hdev->name);
  426. err = device_add(dev);
  427. if (err < 0)
  428. return err;
  429. if (!bt_debugfs)
  430. return 0;
  431. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  432. if (!hdev->debugfs)
  433. return 0;
  434. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  435. hdev, &inquiry_cache_fops);
  436. debugfs_create_file("blacklist", 0444, hdev->debugfs,
  437. hdev, &blacklist_fops);
  438. debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
  439. debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
  440. &auto_accept_delay_fops);
  441. return 0;
  442. }
  443. void hci_del_sysfs(struct hci_dev *hdev)
  444. {
  445. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  446. debugfs_remove_recursive(hdev->debugfs);
  447. device_del(&hdev->dev);
  448. }
  449. int __init bt_sysfs_init(void)
  450. {
  451. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  452. bt_class = class_create(THIS_MODULE, "bluetooth");
  453. if (IS_ERR(bt_class))
  454. return PTR_ERR(bt_class);
  455. return 0;
  456. }
  457. void bt_sysfs_cleanup(void)
  458. {
  459. class_destroy(bt_class);
  460. debugfs_remove_recursive(bt_debugfs);
  461. }