hci_sysfs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/debugfs.h>
  3. #include <linux/module.h>
  4. #include <net/bluetooth/bluetooth.h>
  5. #include <net/bluetooth/hci_core.h>
  6. static struct class *bt_class;
  7. struct dentry *bt_debugfs;
  8. EXPORT_SYMBOL_GPL(bt_debugfs);
  9. static inline char *link_typetostr(int type)
  10. {
  11. switch (type) {
  12. case ACL_LINK:
  13. return "ACL";
  14. case SCO_LINK:
  15. return "SCO";
  16. case ESCO_LINK:
  17. return "eSCO";
  18. case LE_LINK:
  19. return "LE";
  20. default:
  21. return "UNKNOWN";
  22. }
  23. }
  24. static ssize_t show_link_type(struct device *dev,
  25. struct device_attribute *attr, char *buf)
  26. {
  27. struct hci_conn *conn = to_hci_conn(dev);
  28. return sprintf(buf, "%s\n", link_typetostr(conn->type));
  29. }
  30. static ssize_t show_link_address(struct device *dev,
  31. struct device_attribute *attr, char *buf)
  32. {
  33. struct hci_conn *conn = to_hci_conn(dev);
  34. return sprintf(buf, "%pMR\n", &conn->dst);
  35. }
  36. static ssize_t show_link_features(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct hci_conn *conn = to_hci_conn(dev);
  40. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  41. conn->features[0][0], conn->features[0][1],
  42. conn->features[0][2], conn->features[0][3],
  43. conn->features[0][4], conn->features[0][5],
  44. conn->features[0][6], conn->features[0][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. struct hci_conn *conn = to_hci_conn(dev);
  67. kfree(conn);
  68. }
  69. static struct device_type bt_link = {
  70. .name = "link",
  71. .groups = bt_link_groups,
  72. .release = bt_link_release,
  73. };
  74. /*
  75. * The rfcomm tty device will possibly retain even when conn
  76. * is down, and sysfs doesn't support move zombie device,
  77. * so we should move the device before conn device is destroyed.
  78. */
  79. static int __match_tty(struct device *dev, void *data)
  80. {
  81. return !strncmp(dev_name(dev), "rfcomm", 6);
  82. }
  83. void hci_conn_init_sysfs(struct hci_conn *conn)
  84. {
  85. struct hci_dev *hdev = conn->hdev;
  86. BT_DBG("conn %p", conn);
  87. conn->dev.type = &bt_link;
  88. conn->dev.class = bt_class;
  89. conn->dev.parent = &hdev->dev;
  90. device_initialize(&conn->dev);
  91. }
  92. void hci_conn_add_sysfs(struct hci_conn *conn)
  93. {
  94. struct hci_dev *hdev = conn->hdev;
  95. BT_DBG("conn %p", conn);
  96. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  97. if (device_add(&conn->dev) < 0) {
  98. BT_ERR("Failed to register connection device");
  99. return;
  100. }
  101. hci_dev_hold(hdev);
  102. }
  103. void hci_conn_del_sysfs(struct hci_conn *conn)
  104. {
  105. struct hci_dev *hdev = conn->hdev;
  106. if (!device_is_registered(&conn->dev))
  107. return;
  108. while (1) {
  109. struct device *dev;
  110. dev = device_find_child(&conn->dev, NULL, __match_tty);
  111. if (!dev)
  112. break;
  113. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  114. put_device(dev);
  115. }
  116. device_del(&conn->dev);
  117. hci_dev_put(hdev);
  118. }
  119. static inline char *host_bustostr(int bus)
  120. {
  121. switch (bus) {
  122. case HCI_VIRTUAL:
  123. return "VIRTUAL";
  124. case HCI_USB:
  125. return "USB";
  126. case HCI_PCCARD:
  127. return "PCCARD";
  128. case HCI_UART:
  129. return "UART";
  130. case HCI_RS232:
  131. return "RS232";
  132. case HCI_PCI:
  133. return "PCI";
  134. case HCI_SDIO:
  135. return "SDIO";
  136. default:
  137. return "UNKNOWN";
  138. }
  139. }
  140. static inline char *host_typetostr(int type)
  141. {
  142. switch (type) {
  143. case HCI_BREDR:
  144. return "BR/EDR";
  145. case HCI_AMP:
  146. return "AMP";
  147. default:
  148. return "UNKNOWN";
  149. }
  150. }
  151. static ssize_t show_bus(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct hci_dev *hdev = to_hci_dev(dev);
  155. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  156. }
  157. static ssize_t show_type(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. struct hci_dev *hdev = to_hci_dev(dev);
  161. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  162. }
  163. static ssize_t show_name(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct hci_dev *hdev = to_hci_dev(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,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct hci_dev *hdev = to_hci_dev(dev);
  178. return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
  179. hdev->dev_class[1], hdev->dev_class[0]);
  180. }
  181. static ssize_t show_address(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct hci_dev *hdev = to_hci_dev(dev);
  185. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  186. }
  187. static ssize_t show_features(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. struct hci_dev *hdev = to_hci_dev(dev);
  191. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  192. hdev->features[0][0], hdev->features[0][1],
  193. hdev->features[0][2], hdev->features[0][3],
  194. hdev->features[0][4], hdev->features[0][5],
  195. hdev->features[0][6], hdev->features[0][7]);
  196. }
  197. static ssize_t show_manufacturer(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct hci_dev *hdev = to_hci_dev(dev);
  201. return sprintf(buf, "%d\n", hdev->manufacturer);
  202. }
  203. static ssize_t show_hci_version(struct device *dev,
  204. struct device_attribute *attr, char *buf)
  205. {
  206. struct hci_dev *hdev = to_hci_dev(dev);
  207. return sprintf(buf, "%d\n", hdev->hci_ver);
  208. }
  209. static ssize_t show_hci_revision(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct hci_dev *hdev = to_hci_dev(dev);
  213. return sprintf(buf, "%d\n", hdev->hci_rev);
  214. }
  215. static ssize_t show_idle_timeout(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. struct hci_dev *hdev = to_hci_dev(dev);
  219. return sprintf(buf, "%d\n", hdev->idle_timeout);
  220. }
  221. static ssize_t store_idle_timeout(struct device *dev,
  222. struct device_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. struct hci_dev *hdev = to_hci_dev(dev);
  226. unsigned int val;
  227. int rv;
  228. rv = kstrtouint(buf, 0, &val);
  229. if (rv < 0)
  230. return rv;
  231. if (val != 0 && (val < 500 || val > 3600000))
  232. return -EINVAL;
  233. hdev->idle_timeout = val;
  234. return count;
  235. }
  236. static ssize_t show_sniff_max_interval(struct device *dev,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. struct hci_dev *hdev = to_hci_dev(dev);
  240. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  241. }
  242. static ssize_t store_sniff_max_interval(struct device *dev,
  243. struct device_attribute *attr,
  244. const char *buf, size_t count)
  245. {
  246. struct hci_dev *hdev = to_hci_dev(dev);
  247. u16 val;
  248. int rv;
  249. rv = kstrtou16(buf, 0, &val);
  250. if (rv < 0)
  251. return rv;
  252. if (val == 0 || val % 2 || 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,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct hci_dev *hdev = to_hci_dev(dev);
  261. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  262. }
  263. static ssize_t store_sniff_min_interval(struct device *dev,
  264. struct device_attribute *attr,
  265. const char *buf, size_t count)
  266. {
  267. struct hci_dev *hdev = to_hci_dev(dev);
  268. u16 val;
  269. int rv;
  270. rv = kstrtou16(buf, 0, &val);
  271. if (rv < 0)
  272. return rv;
  273. if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
  274. return -EINVAL;
  275. hdev->sniff_min_interval = val;
  276. return count;
  277. }
  278. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  279. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  280. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  281. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  282. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  283. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  284. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  285. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  286. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  287. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  288. show_idle_timeout, store_idle_timeout);
  289. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  290. show_sniff_max_interval, store_sniff_max_interval);
  291. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  292. show_sniff_min_interval, store_sniff_min_interval);
  293. static struct attribute *bt_host_attrs[] = {
  294. &dev_attr_bus.attr,
  295. &dev_attr_type.attr,
  296. &dev_attr_name.attr,
  297. &dev_attr_class.attr,
  298. &dev_attr_address.attr,
  299. &dev_attr_features.attr,
  300. &dev_attr_manufacturer.attr,
  301. &dev_attr_hci_version.attr,
  302. &dev_attr_hci_revision.attr,
  303. &dev_attr_idle_timeout.attr,
  304. &dev_attr_sniff_max_interval.attr,
  305. &dev_attr_sniff_min_interval.attr,
  306. NULL
  307. };
  308. static struct attribute_group bt_host_group = {
  309. .attrs = bt_host_attrs,
  310. };
  311. static const struct attribute_group *bt_host_groups[] = {
  312. &bt_host_group,
  313. NULL
  314. };
  315. static void bt_host_release(struct device *dev)
  316. {
  317. struct hci_dev *hdev = to_hci_dev(dev);
  318. kfree(hdev);
  319. module_put(THIS_MODULE);
  320. }
  321. static struct device_type bt_host = {
  322. .name = "host",
  323. .groups = bt_host_groups,
  324. .release = bt_host_release,
  325. };
  326. void hci_init_sysfs(struct hci_dev *hdev)
  327. {
  328. struct device *dev = &hdev->dev;
  329. dev->type = &bt_host;
  330. dev->class = bt_class;
  331. __module_get(THIS_MODULE);
  332. device_initialize(dev);
  333. }
  334. int hci_add_sysfs(struct hci_dev *hdev)
  335. {
  336. struct device *dev = &hdev->dev;
  337. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  338. dev_set_name(dev, "%s", hdev->name);
  339. return device_add(dev);
  340. }
  341. void hci_del_sysfs(struct hci_dev *hdev)
  342. {
  343. BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
  344. device_del(&hdev->dev);
  345. }
  346. int __init bt_sysfs_init(void)
  347. {
  348. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  349. bt_class = class_create(THIS_MODULE, "bluetooth");
  350. return PTR_ERR_OR_ZERO(bt_class);
  351. }
  352. void bt_sysfs_cleanup(void)
  353. {
  354. class_destroy(bt_class);
  355. debugfs_remove_recursive(bt_debugfs);
  356. }