hci_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 = NULL;
  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. bdaddr_t bdaddr;
  34. baswap(&bdaddr, &conn->dst);
  35. return sprintf(buf, "%s\n", batostr(&bdaddr));
  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_80211:
  161. return "802.11";
  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[249];
  180. int i;
  181. for (i = 0; i < 248; i++)
  182. name[i] = hdev->dev_name[i];
  183. name[248] = '\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. bdaddr_t bdaddr;
  196. baswap(&bdaddr, &hdev->bdaddr);
  197. return sprintf(buf, "%s\n", batostr(&bdaddr));
  198. }
  199. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  200. {
  201. struct hci_dev *hdev = dev_get_drvdata(dev);
  202. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  203. hdev->features[0], hdev->features[1],
  204. hdev->features[2], hdev->features[3],
  205. hdev->features[4], hdev->features[5],
  206. hdev->features[6], hdev->features[7]);
  207. }
  208. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct hci_dev *hdev = dev_get_drvdata(dev);
  211. return sprintf(buf, "%d\n", hdev->manufacturer);
  212. }
  213. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  214. {
  215. struct hci_dev *hdev = dev_get_drvdata(dev);
  216. return sprintf(buf, "%d\n", hdev->hci_ver);
  217. }
  218. static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
  219. {
  220. struct hci_dev *hdev = dev_get_drvdata(dev);
  221. return sprintf(buf, "%d\n", hdev->hci_rev);
  222. }
  223. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  224. {
  225. struct hci_dev *hdev = dev_get_drvdata(dev);
  226. return sprintf(buf, "%d\n", hdev->idle_timeout);
  227. }
  228. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  229. {
  230. struct hci_dev *hdev = dev_get_drvdata(dev);
  231. unsigned long val;
  232. if (strict_strtoul(buf, 0, &val) < 0)
  233. return -EINVAL;
  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. unsigned long val;
  248. if (strict_strtoul(buf, 0, &val) < 0)
  249. return -EINVAL;
  250. if (val < 0x0002 || val > 0xFFFE || val % 2)
  251. return -EINVAL;
  252. if (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. unsigned long val;
  266. if (strict_strtoul(buf, 0, &val) < 0)
  267. return -EINVAL;
  268. if (val < 0x0002 || val > 0xFFFE || val % 2)
  269. return -EINVAL;
  270. if (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. bdaddr_t bdaddr;
  331. baswap(&bdaddr, &data->bdaddr);
  332. seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  333. batostr(&bdaddr),
  334. data->pscan_rep_mode, data->pscan_period_mode,
  335. data->pscan_mode, data->dev_class[2],
  336. data->dev_class[1], data->dev_class[0],
  337. __le16_to_cpu(data->clock_offset),
  338. data->rssi, data->ssp_mode, e->timestamp);
  339. }
  340. hci_dev_unlock_bh(hdev);
  341. return 0;
  342. }
  343. static int inquiry_cache_open(struct inode *inode, struct file *file)
  344. {
  345. return single_open(file, inquiry_cache_show, inode->i_private);
  346. }
  347. static const struct file_operations inquiry_cache_fops = {
  348. .open = inquiry_cache_open,
  349. .read = seq_read,
  350. .llseek = seq_lseek,
  351. .release = single_release,
  352. };
  353. static int blacklist_show(struct seq_file *f, void *p)
  354. {
  355. struct hci_dev *hdev = f->private;
  356. struct list_head *l;
  357. hci_dev_lock_bh(hdev);
  358. list_for_each(l, &hdev->blacklist) {
  359. struct bdaddr_list *b;
  360. bdaddr_t bdaddr;
  361. b = list_entry(l, struct bdaddr_list, list);
  362. baswap(&bdaddr, &b->bdaddr);
  363. seq_printf(f, "%s\n", batostr(&bdaddr));
  364. }
  365. hci_dev_unlock_bh(hdev);
  366. return 0;
  367. }
  368. static int blacklist_open(struct inode *inode, struct file *file)
  369. {
  370. return single_open(file, blacklist_show, inode->i_private);
  371. }
  372. static const struct file_operations blacklist_fops = {
  373. .open = blacklist_open,
  374. .read = seq_read,
  375. .llseek = seq_lseek,
  376. .release = single_release,
  377. };
  378. int hci_register_sysfs(struct hci_dev *hdev)
  379. {
  380. struct device *dev = &hdev->dev;
  381. int err;
  382. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  383. dev->type = &bt_host;
  384. dev->class = bt_class;
  385. dev->parent = hdev->parent;
  386. dev_set_name(dev, "%s", hdev->name);
  387. dev_set_drvdata(dev, hdev);
  388. err = device_register(dev);
  389. if (err < 0)
  390. return err;
  391. if (!bt_debugfs)
  392. return 0;
  393. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  394. if (!hdev->debugfs)
  395. return 0;
  396. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  397. hdev, &inquiry_cache_fops);
  398. debugfs_create_file("blacklist", 0444, hdev->debugfs,
  399. hdev, &blacklist_fops);
  400. return 0;
  401. }
  402. void hci_unregister_sysfs(struct hci_dev *hdev)
  403. {
  404. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  405. debugfs_remove_recursive(hdev->debugfs);
  406. device_del(&hdev->dev);
  407. }
  408. int __init bt_sysfs_init(void)
  409. {
  410. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  411. bt_class = class_create(THIS_MODULE, "bluetooth");
  412. if (IS_ERR(bt_class))
  413. return PTR_ERR(bt_class);
  414. return 0;
  415. }
  416. void bt_sysfs_cleanup(void)
  417. {
  418. class_destroy(bt_class);
  419. debugfs_remove_recursive(bt_debugfs);
  420. }