hci_sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/debugfs.h>
  5. #include <linux/seq_file.h>
  6. #include <net/bluetooth/bluetooth.h>
  7. #include <net/bluetooth/hci_core.h>
  8. static struct class *bt_class;
  9. struct dentry *bt_debugfs = NULL;
  10. EXPORT_SYMBOL_GPL(bt_debugfs);
  11. static struct workqueue_struct *bt_workq;
  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(bt_workq, &conn->work_add);
  128. }
  129. void hci_conn_del_sysfs(struct hci_conn *conn)
  130. {
  131. BT_DBG("conn %p", conn);
  132. queue_work(bt_workq, &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. char *ptr;
  232. __u32 val;
  233. val = simple_strtoul(buf, &ptr, 10);
  234. if (ptr == buf)
  235. return -EINVAL;
  236. if (val != 0 && (val < 500 || val > 3600000))
  237. return -EINVAL;
  238. hdev->idle_timeout = val;
  239. return count;
  240. }
  241. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  242. {
  243. struct hci_dev *hdev = dev_get_drvdata(dev);
  244. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  245. }
  246. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  247. {
  248. struct hci_dev *hdev = dev_get_drvdata(dev);
  249. char *ptr;
  250. __u16 val;
  251. val = simple_strtoul(buf, &ptr, 10);
  252. if (ptr == buf)
  253. return -EINVAL;
  254. if (val < 0x0002 || val > 0xFFFE || val % 2)
  255. return -EINVAL;
  256. if (val < hdev->sniff_min_interval)
  257. return -EINVAL;
  258. hdev->sniff_max_interval = val;
  259. return count;
  260. }
  261. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  262. {
  263. struct hci_dev *hdev = dev_get_drvdata(dev);
  264. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  265. }
  266. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  267. {
  268. struct hci_dev *hdev = dev_get_drvdata(dev);
  269. char *ptr;
  270. __u16 val;
  271. val = simple_strtoul(buf, &ptr, 10);
  272. if (ptr == buf)
  273. return -EINVAL;
  274. if (val < 0x0002 || val > 0xFFFE || val % 2)
  275. return -EINVAL;
  276. if (val > hdev->sniff_max_interval)
  277. return -EINVAL;
  278. hdev->sniff_min_interval = val;
  279. return count;
  280. }
  281. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  282. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  283. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  284. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  285. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  286. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  287. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  288. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  289. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  290. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  291. show_idle_timeout, store_idle_timeout);
  292. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  293. show_sniff_max_interval, store_sniff_max_interval);
  294. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  295. show_sniff_min_interval, store_sniff_min_interval);
  296. static struct attribute *bt_host_attrs[] = {
  297. &dev_attr_bus.attr,
  298. &dev_attr_type.attr,
  299. &dev_attr_name.attr,
  300. &dev_attr_class.attr,
  301. &dev_attr_address.attr,
  302. &dev_attr_features.attr,
  303. &dev_attr_manufacturer.attr,
  304. &dev_attr_hci_version.attr,
  305. &dev_attr_hci_revision.attr,
  306. &dev_attr_idle_timeout.attr,
  307. &dev_attr_sniff_max_interval.attr,
  308. &dev_attr_sniff_min_interval.attr,
  309. NULL
  310. };
  311. static struct attribute_group bt_host_group = {
  312. .attrs = bt_host_attrs,
  313. };
  314. static const struct attribute_group *bt_host_groups[] = {
  315. &bt_host_group,
  316. NULL
  317. };
  318. static void bt_host_release(struct device *dev)
  319. {
  320. void *data = dev_get_drvdata(dev);
  321. kfree(data);
  322. }
  323. static struct device_type bt_host = {
  324. .name = "host",
  325. .groups = bt_host_groups,
  326. .release = bt_host_release,
  327. };
  328. static int inquiry_cache_show(struct seq_file *f, void *p)
  329. {
  330. struct hci_dev *hdev = f->private;
  331. struct inquiry_cache *cache = &hdev->inq_cache;
  332. struct inquiry_entry *e;
  333. hci_dev_lock_bh(hdev);
  334. for (e = cache->list; e; e = e->next) {
  335. struct inquiry_data *data = &e->data;
  336. bdaddr_t bdaddr;
  337. baswap(&bdaddr, &data->bdaddr);
  338. seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  339. batostr(&bdaddr),
  340. data->pscan_rep_mode, data->pscan_period_mode,
  341. data->pscan_mode, data->dev_class[2],
  342. data->dev_class[1], data->dev_class[0],
  343. __le16_to_cpu(data->clock_offset),
  344. data->rssi, data->ssp_mode, e->timestamp);
  345. }
  346. hci_dev_unlock_bh(hdev);
  347. return 0;
  348. }
  349. static int inquiry_cache_open(struct inode *inode, struct file *file)
  350. {
  351. return single_open(file, inquiry_cache_show, inode->i_private);
  352. }
  353. static const struct file_operations inquiry_cache_fops = {
  354. .open = inquiry_cache_open,
  355. .read = seq_read,
  356. .llseek = seq_lseek,
  357. .release = single_release,
  358. };
  359. int hci_register_sysfs(struct hci_dev *hdev)
  360. {
  361. struct device *dev = &hdev->dev;
  362. int err;
  363. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  364. dev->type = &bt_host;
  365. dev->class = bt_class;
  366. dev->parent = hdev->parent;
  367. dev_set_name(dev, "%s", hdev->name);
  368. dev_set_drvdata(dev, hdev);
  369. err = device_register(dev);
  370. if (err < 0)
  371. return err;
  372. if (!bt_debugfs)
  373. return 0;
  374. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  375. if (!hdev->debugfs)
  376. return 0;
  377. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  378. hdev, &inquiry_cache_fops);
  379. return 0;
  380. }
  381. void hci_unregister_sysfs(struct hci_dev *hdev)
  382. {
  383. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  384. debugfs_remove_recursive(hdev->debugfs);
  385. device_del(&hdev->dev);
  386. }
  387. int __init bt_sysfs_init(void)
  388. {
  389. bt_workq = create_singlethread_workqueue("bluetooth");
  390. if (!bt_workq)
  391. return -ENOMEM;
  392. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  393. bt_class = class_create(THIS_MODULE, "bluetooth");
  394. if (IS_ERR(bt_class)) {
  395. destroy_workqueue(bt_workq);
  396. return PTR_ERR(bt_class);
  397. }
  398. return 0;
  399. }
  400. void bt_sysfs_cleanup(void)
  401. {
  402. class_destroy(bt_class);
  403. debugfs_remove_recursive(bt_debugfs);
  404. destroy_workqueue(bt_workq);
  405. }