hci_sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 struct workqueue_struct *bt_workq;
  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. default:
  23. return "UNKNOWN";
  24. }
  25. }
  26. static ssize_t show_link_type(struct device *dev, struct device_attribute *attr, char *buf)
  27. {
  28. struct hci_conn *conn = dev_get_drvdata(dev);
  29. return sprintf(buf, "%s\n", link_typetostr(conn->type));
  30. }
  31. static ssize_t show_link_address(struct device *dev, struct device_attribute *attr, char *buf)
  32. {
  33. struct hci_conn *conn = dev_get_drvdata(dev);
  34. bdaddr_t bdaddr;
  35. baswap(&bdaddr, &conn->dst);
  36. return sprintf(buf, "%s\n", batostr(&bdaddr));
  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. static void add_conn(struct work_struct *work)
  76. {
  77. struct hci_conn *conn = container_of(work, struct hci_conn, work_add);
  78. struct hci_dev *hdev = conn->hdev;
  79. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  80. dev_set_drvdata(&conn->dev, conn);
  81. if (device_add(&conn->dev) < 0) {
  82. BT_ERR("Failed to register connection device");
  83. return;
  84. }
  85. hci_dev_hold(hdev);
  86. }
  87. /*
  88. * The rfcomm tty device will possibly retain even when conn
  89. * is down, and sysfs doesn't support move zombie device,
  90. * so we should move the device before conn device is destroyed.
  91. */
  92. static int __match_tty(struct device *dev, void *data)
  93. {
  94. return !strncmp(dev_name(dev), "rfcomm", 6);
  95. }
  96. static void del_conn(struct work_struct *work)
  97. {
  98. struct hci_conn *conn = container_of(work, struct hci_conn, work_del);
  99. struct hci_dev *hdev = conn->hdev;
  100. if (!device_is_registered(&conn->dev))
  101. return;
  102. while (1) {
  103. struct device *dev;
  104. dev = device_find_child(&conn->dev, NULL, __match_tty);
  105. if (!dev)
  106. break;
  107. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  108. put_device(dev);
  109. }
  110. device_del(&conn->dev);
  111. put_device(&conn->dev);
  112. hci_dev_put(hdev);
  113. }
  114. void hci_conn_init_sysfs(struct hci_conn *conn)
  115. {
  116. struct hci_dev *hdev = conn->hdev;
  117. BT_DBG("conn %p", conn);
  118. conn->dev.type = &bt_link;
  119. conn->dev.class = bt_class;
  120. conn->dev.parent = &hdev->dev;
  121. device_initialize(&conn->dev);
  122. INIT_WORK(&conn->work_add, add_conn);
  123. INIT_WORK(&conn->work_del, del_conn);
  124. }
  125. void hci_conn_add_sysfs(struct hci_conn *conn)
  126. {
  127. BT_DBG("conn %p", conn);
  128. queue_work(bt_workq, &conn->work_add);
  129. }
  130. void hci_conn_del_sysfs(struct hci_conn *conn)
  131. {
  132. BT_DBG("conn %p", conn);
  133. queue_work(bt_workq, &conn->work_del);
  134. }
  135. static inline char *host_bustostr(int bus)
  136. {
  137. switch (bus) {
  138. case HCI_VIRTUAL:
  139. return "VIRTUAL";
  140. case HCI_USB:
  141. return "USB";
  142. case HCI_PCCARD:
  143. return "PCCARD";
  144. case HCI_UART:
  145. return "UART";
  146. case HCI_RS232:
  147. return "RS232";
  148. case HCI_PCI:
  149. return "PCI";
  150. case HCI_SDIO:
  151. return "SDIO";
  152. default:
  153. return "UNKNOWN";
  154. }
  155. }
  156. static inline char *host_typetostr(int type)
  157. {
  158. switch (type) {
  159. case HCI_BREDR:
  160. return "BR/EDR";
  161. case HCI_80211:
  162. return "802.11";
  163. default:
  164. return "UNKNOWN";
  165. }
  166. }
  167. static ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
  168. {
  169. struct hci_dev *hdev = dev_get_drvdata(dev);
  170. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  171. }
  172. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  173. {
  174. struct hci_dev *hdev = dev_get_drvdata(dev);
  175. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  176. }
  177. static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
  178. {
  179. struct hci_dev *hdev = dev_get_drvdata(dev);
  180. char name[249];
  181. int i;
  182. for (i = 0; i < 248; i++)
  183. name[i] = hdev->dev_name[i];
  184. name[248] = '\0';
  185. return sprintf(buf, "%s\n", name);
  186. }
  187. static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. struct hci_dev *hdev = dev_get_drvdata(dev);
  190. return sprintf(buf, "0x%.2x%.2x%.2x\n",
  191. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  192. }
  193. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. struct hci_dev *hdev = dev_get_drvdata(dev);
  196. bdaddr_t bdaddr;
  197. baswap(&bdaddr, &hdev->bdaddr);
  198. return sprintf(buf, "%s\n", batostr(&bdaddr));
  199. }
  200. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  201. {
  202. struct hci_dev *hdev = dev_get_drvdata(dev);
  203. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  204. hdev->features[0], hdev->features[1],
  205. hdev->features[2], hdev->features[3],
  206. hdev->features[4], hdev->features[5],
  207. hdev->features[6], hdev->features[7]);
  208. }
  209. static ssize_t show_manufacturer(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->manufacturer);
  213. }
  214. static ssize_t show_hci_version(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_ver);
  218. }
  219. static ssize_t show_hci_revision(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->hci_rev);
  223. }
  224. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  225. {
  226. struct hci_dev *hdev = dev_get_drvdata(dev);
  227. return sprintf(buf, "%d\n", hdev->idle_timeout);
  228. }
  229. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  230. {
  231. struct hci_dev *hdev = dev_get_drvdata(dev);
  232. char *ptr;
  233. __u32 val;
  234. val = simple_strtoul(buf, &ptr, 10);
  235. if (ptr == buf)
  236. return -EINVAL;
  237. if (val != 0 && (val < 500 || val > 3600000))
  238. return -EINVAL;
  239. hdev->idle_timeout = val;
  240. return count;
  241. }
  242. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  243. {
  244. struct hci_dev *hdev = dev_get_drvdata(dev);
  245. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  246. }
  247. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  248. {
  249. struct hci_dev *hdev = dev_get_drvdata(dev);
  250. char *ptr;
  251. __u16 val;
  252. val = simple_strtoul(buf, &ptr, 10);
  253. if (ptr == buf)
  254. return -EINVAL;
  255. if (val < 0x0002 || val > 0xFFFE || val % 2)
  256. return -EINVAL;
  257. if (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, struct device_attribute *attr, char *buf)
  263. {
  264. struct hci_dev *hdev = dev_get_drvdata(dev);
  265. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  266. }
  267. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  268. {
  269. struct hci_dev *hdev = dev_get_drvdata(dev);
  270. char *ptr;
  271. __u16 val;
  272. val = simple_strtoul(buf, &ptr, 10);
  273. if (ptr == buf)
  274. return -EINVAL;
  275. if (val < 0x0002 || val > 0xFFFE || val % 2)
  276. return -EINVAL;
  277. if (val > hdev->sniff_max_interval)
  278. return -EINVAL;
  279. hdev->sniff_min_interval = val;
  280. return count;
  281. }
  282. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  283. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  284. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  285. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  286. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  287. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  288. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  289. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  290. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  291. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  292. show_idle_timeout, store_idle_timeout);
  293. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  294. show_sniff_max_interval, store_sniff_max_interval);
  295. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  296. show_sniff_min_interval, store_sniff_min_interval);
  297. static struct attribute *bt_host_attrs[] = {
  298. &dev_attr_bus.attr,
  299. &dev_attr_type.attr,
  300. &dev_attr_name.attr,
  301. &dev_attr_class.attr,
  302. &dev_attr_address.attr,
  303. &dev_attr_features.attr,
  304. &dev_attr_manufacturer.attr,
  305. &dev_attr_hci_version.attr,
  306. &dev_attr_hci_revision.attr,
  307. &dev_attr_idle_timeout.attr,
  308. &dev_attr_sniff_max_interval.attr,
  309. &dev_attr_sniff_min_interval.attr,
  310. NULL
  311. };
  312. static struct attribute_group bt_host_group = {
  313. .attrs = bt_host_attrs,
  314. };
  315. static const struct attribute_group *bt_host_groups[] = {
  316. &bt_host_group,
  317. NULL
  318. };
  319. static void bt_host_release(struct device *dev)
  320. {
  321. void *data = dev_get_drvdata(dev);
  322. kfree(data);
  323. }
  324. static struct device_type bt_host = {
  325. .name = "host",
  326. .groups = bt_host_groups,
  327. .release = bt_host_release,
  328. };
  329. static int inquiry_cache_show(struct seq_file *f, void *p)
  330. {
  331. struct hci_dev *hdev = f->private;
  332. struct inquiry_cache *cache = &hdev->inq_cache;
  333. struct inquiry_entry *e;
  334. hci_dev_lock_bh(hdev);
  335. for (e = cache->list; e; e = e->next) {
  336. struct inquiry_data *data = &e->data;
  337. bdaddr_t bdaddr;
  338. baswap(&bdaddr, &data->bdaddr);
  339. seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  340. batostr(&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_bh(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. int hci_register_sysfs(struct hci_dev *hdev)
  361. {
  362. struct device *dev = &hdev->dev;
  363. int err;
  364. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  365. dev->type = &bt_host;
  366. dev->class = bt_class;
  367. dev->parent = hdev->parent;
  368. dev_set_name(dev, "%s", hdev->name);
  369. dev_set_drvdata(dev, hdev);
  370. err = device_register(dev);
  371. if (err < 0)
  372. return err;
  373. if (!bt_debugfs)
  374. return 0;
  375. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  376. if (!hdev->debugfs)
  377. return 0;
  378. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  379. hdev, &inquiry_cache_fops);
  380. return 0;
  381. }
  382. void hci_unregister_sysfs(struct hci_dev *hdev)
  383. {
  384. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  385. debugfs_remove_recursive(hdev->debugfs);
  386. device_del(&hdev->dev);
  387. }
  388. int __init bt_sysfs_init(void)
  389. {
  390. bt_workq = create_singlethread_workqueue("bluetooth");
  391. if (!bt_workq)
  392. return -ENOMEM;
  393. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  394. bt_class = class_create(THIS_MODULE, "bluetooth");
  395. if (IS_ERR(bt_class)) {
  396. destroy_workqueue(bt_workq);
  397. return PTR_ERR(bt_class);
  398. }
  399. return 0;
  400. }
  401. void bt_sysfs_cleanup(void)
  402. {
  403. class_destroy(bt_class);
  404. debugfs_remove_recursive(bt_debugfs);
  405. destroy_workqueue(bt_workq);
  406. }