hci_sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/debugfs.h>
  5. #include <net/bluetooth/bluetooth.h>
  6. #include <net/bluetooth/hci_core.h>
  7. struct class *bt_class = NULL;
  8. EXPORT_SYMBOL_GPL(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 ssize_t show_bus(struct device *dev, struct device_attribute *attr, char *buf)
  156. {
  157. struct hci_dev *hdev = dev_get_drvdata(dev);
  158. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  159. }
  160. static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
  161. {
  162. struct hci_dev *hdev = dev_get_drvdata(dev);
  163. char name[249];
  164. int i;
  165. for (i = 0; i < 248; i++)
  166. name[i] = hdev->dev_name[i];
  167. name[248] = '\0';
  168. return sprintf(buf, "%s\n", name);
  169. }
  170. static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
  171. {
  172. struct hci_dev *hdev = dev_get_drvdata(dev);
  173. return sprintf(buf, "0x%.2x%.2x%.2x\n",
  174. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  175. }
  176. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. struct hci_dev *hdev = dev_get_drvdata(dev);
  179. bdaddr_t bdaddr;
  180. baswap(&bdaddr, &hdev->bdaddr);
  181. return sprintf(buf, "%s\n", batostr(&bdaddr));
  182. }
  183. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  184. {
  185. struct hci_dev *hdev = dev_get_drvdata(dev);
  186. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  187. hdev->features[0], hdev->features[1],
  188. hdev->features[2], hdev->features[3],
  189. hdev->features[4], hdev->features[5],
  190. hdev->features[6], hdev->features[7]);
  191. }
  192. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  193. {
  194. struct hci_dev *hdev = dev_get_drvdata(dev);
  195. return sprintf(buf, "%d\n", hdev->manufacturer);
  196. }
  197. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  198. {
  199. struct hci_dev *hdev = dev_get_drvdata(dev);
  200. return sprintf(buf, "%d\n", hdev->hci_ver);
  201. }
  202. static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct hci_dev *hdev = dev_get_drvdata(dev);
  205. return sprintf(buf, "%d\n", hdev->hci_rev);
  206. }
  207. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  208. {
  209. struct hci_dev *hdev = dev_get_drvdata(dev);
  210. return sprintf(buf, "%d\n", hdev->idle_timeout);
  211. }
  212. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  213. {
  214. struct hci_dev *hdev = dev_get_drvdata(dev);
  215. char *ptr;
  216. __u32 val;
  217. val = simple_strtoul(buf, &ptr, 10);
  218. if (ptr == buf)
  219. return -EINVAL;
  220. if (val != 0 && (val < 500 || val > 3600000))
  221. return -EINVAL;
  222. hdev->idle_timeout = val;
  223. return count;
  224. }
  225. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  226. {
  227. struct hci_dev *hdev = dev_get_drvdata(dev);
  228. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  229. }
  230. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  231. {
  232. struct hci_dev *hdev = dev_get_drvdata(dev);
  233. char *ptr;
  234. __u16 val;
  235. val = simple_strtoul(buf, &ptr, 10);
  236. if (ptr == buf)
  237. return -EINVAL;
  238. if (val < 0x0002 || val > 0xFFFE || val % 2)
  239. return -EINVAL;
  240. if (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. char *ptr;
  254. __u16 val;
  255. val = simple_strtoul(buf, &ptr, 10);
  256. if (ptr == buf)
  257. return -EINVAL;
  258. if (val < 0x0002 || val > 0xFFFE || val % 2)
  259. return -EINVAL;
  260. if (val > hdev->sniff_max_interval)
  261. return -EINVAL;
  262. hdev->sniff_min_interval = val;
  263. return count;
  264. }
  265. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  266. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  267. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  268. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  269. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  270. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  271. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  272. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  273. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  274. show_idle_timeout, store_idle_timeout);
  275. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  276. show_sniff_max_interval, store_sniff_max_interval);
  277. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  278. show_sniff_min_interval, store_sniff_min_interval);
  279. static struct attribute *bt_host_attrs[] = {
  280. &dev_attr_bus.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_open(struct inode *inode, struct file *file)
  311. {
  312. file->private_data = inode->i_private;
  313. return 0;
  314. }
  315. static ssize_t inquiry_cache_read(struct file *file, char __user *userbuf,
  316. size_t count, loff_t *ppos)
  317. {
  318. struct hci_dev *hdev = file->private_data;
  319. struct inquiry_cache *cache = &hdev->inq_cache;
  320. struct inquiry_entry *e;
  321. char buf[4096];
  322. int n = 0;
  323. hci_dev_lock_bh(hdev);
  324. for (e = cache->list; e; e = e->next) {
  325. struct inquiry_data *data = &e->data;
  326. bdaddr_t bdaddr;
  327. baswap(&bdaddr, &data->bdaddr);
  328. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  329. batostr(&bdaddr),
  330. data->pscan_rep_mode, data->pscan_period_mode,
  331. data->pscan_mode, data->dev_class[2],
  332. data->dev_class[1], data->dev_class[0],
  333. __le16_to_cpu(data->clock_offset),
  334. data->rssi, data->ssp_mode, e->timestamp);
  335. }
  336. hci_dev_unlock_bh(hdev);
  337. return simple_read_from_buffer(userbuf, count, ppos, buf, n);
  338. }
  339. static const struct file_operations inquiry_cache_fops = {
  340. .open = inquiry_cache_open,
  341. .read = inquiry_cache_read,
  342. };
  343. int hci_register_sysfs(struct hci_dev *hdev)
  344. {
  345. struct device *dev = &hdev->dev;
  346. int err;
  347. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  348. dev->type = &bt_host;
  349. dev->class = bt_class;
  350. dev->parent = hdev->parent;
  351. dev_set_name(dev, "%s", hdev->name);
  352. dev_set_drvdata(dev, hdev);
  353. err = device_register(dev);
  354. if (err < 0)
  355. return err;
  356. if (!bt_debugfs)
  357. return 0;
  358. hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
  359. if (!hdev->debugfs)
  360. return 0;
  361. debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
  362. hdev, &inquiry_cache_fops);
  363. return 0;
  364. }
  365. void hci_unregister_sysfs(struct hci_dev *hdev)
  366. {
  367. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  368. debugfs_remove_recursive(hdev->debugfs);
  369. device_del(&hdev->dev);
  370. }
  371. int __init bt_sysfs_init(void)
  372. {
  373. bt_workq = create_singlethread_workqueue("bluetooth");
  374. if (!bt_workq)
  375. return -ENOMEM;
  376. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  377. bt_class = class_create(THIS_MODULE, "bluetooth");
  378. if (IS_ERR(bt_class)) {
  379. destroy_workqueue(bt_workq);
  380. return PTR_ERR(bt_class);
  381. }
  382. return 0;
  383. }
  384. void bt_sysfs_cleanup(void)
  385. {
  386. class_destroy(bt_class);
  387. debugfs_remove_recursive(bt_debugfs);
  388. destroy_workqueue(bt_workq);
  389. }