hci_sysfs.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/module.h>
  3. #include <net/bluetooth/bluetooth.h>
  4. #include <net/bluetooth/hci_core.h>
  5. static struct class *bt_class;
  6. static inline char *link_typetostr(int type)
  7. {
  8. switch (type) {
  9. case ACL_LINK:
  10. return "ACL";
  11. case SCO_LINK:
  12. return "SCO";
  13. case ESCO_LINK:
  14. return "eSCO";
  15. case LE_LINK:
  16. return "LE";
  17. default:
  18. return "UNKNOWN";
  19. }
  20. }
  21. static ssize_t show_link_type(struct device *dev,
  22. struct device_attribute *attr, char *buf)
  23. {
  24. struct hci_conn *conn = to_hci_conn(dev);
  25. return sprintf(buf, "%s\n", link_typetostr(conn->type));
  26. }
  27. static ssize_t show_link_address(struct device *dev,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. struct hci_conn *conn = to_hci_conn(dev);
  31. return sprintf(buf, "%pMR\n", &conn->dst);
  32. }
  33. static ssize_t show_link_features(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct hci_conn *conn = to_hci_conn(dev);
  37. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  38. conn->features[0][0], conn->features[0][1],
  39. conn->features[0][2], conn->features[0][3],
  40. conn->features[0][4], conn->features[0][5],
  41. conn->features[0][6], conn->features[0][7]);
  42. }
  43. #define LINK_ATTR(_name, _mode, _show, _store) \
  44. struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
  45. static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
  46. static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
  47. static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
  48. static struct attribute *bt_link_attrs[] = {
  49. &link_attr_type.attr,
  50. &link_attr_address.attr,
  51. &link_attr_features.attr,
  52. NULL
  53. };
  54. static struct attribute_group bt_link_group = {
  55. .attrs = bt_link_attrs,
  56. };
  57. static const struct attribute_group *bt_link_groups[] = {
  58. &bt_link_group,
  59. NULL
  60. };
  61. static void bt_link_release(struct device *dev)
  62. {
  63. struct hci_conn *conn = to_hci_conn(dev);
  64. kfree(conn);
  65. }
  66. static struct device_type bt_link = {
  67. .name = "link",
  68. .groups = bt_link_groups,
  69. .release = bt_link_release,
  70. };
  71. /*
  72. * The rfcomm tty device will possibly retain even when conn
  73. * is down, and sysfs doesn't support move zombie device,
  74. * so we should move the device before conn device is destroyed.
  75. */
  76. static int __match_tty(struct device *dev, void *data)
  77. {
  78. return !strncmp(dev_name(dev), "rfcomm", 6);
  79. }
  80. void hci_conn_init_sysfs(struct hci_conn *conn)
  81. {
  82. struct hci_dev *hdev = conn->hdev;
  83. BT_DBG("conn %p", conn);
  84. conn->dev.type = &bt_link;
  85. conn->dev.class = bt_class;
  86. conn->dev.parent = &hdev->dev;
  87. device_initialize(&conn->dev);
  88. }
  89. void hci_conn_add_sysfs(struct hci_conn *conn)
  90. {
  91. struct hci_dev *hdev = conn->hdev;
  92. BT_DBG("conn %p", conn);
  93. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  94. if (device_add(&conn->dev) < 0) {
  95. BT_ERR("Failed to register connection device");
  96. return;
  97. }
  98. hci_dev_hold(hdev);
  99. }
  100. void hci_conn_del_sysfs(struct hci_conn *conn)
  101. {
  102. struct hci_dev *hdev = conn->hdev;
  103. if (!device_is_registered(&conn->dev))
  104. return;
  105. while (1) {
  106. struct device *dev;
  107. dev = device_find_child(&conn->dev, NULL, __match_tty);
  108. if (!dev)
  109. break;
  110. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  111. put_device(dev);
  112. }
  113. device_del(&conn->dev);
  114. hci_dev_put(hdev);
  115. }
  116. static inline char *host_bustostr(int bus)
  117. {
  118. switch (bus) {
  119. case HCI_VIRTUAL:
  120. return "VIRTUAL";
  121. case HCI_USB:
  122. return "USB";
  123. case HCI_PCCARD:
  124. return "PCCARD";
  125. case HCI_UART:
  126. return "UART";
  127. case HCI_RS232:
  128. return "RS232";
  129. case HCI_PCI:
  130. return "PCI";
  131. case HCI_SDIO:
  132. return "SDIO";
  133. default:
  134. return "UNKNOWN";
  135. }
  136. }
  137. static inline char *host_typetostr(int type)
  138. {
  139. switch (type) {
  140. case HCI_BREDR:
  141. return "BR/EDR";
  142. case HCI_AMP:
  143. return "AMP";
  144. default:
  145. return "UNKNOWN";
  146. }
  147. }
  148. static ssize_t show_bus(struct device *dev,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. struct hci_dev *hdev = to_hci_dev(dev);
  152. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  153. }
  154. static ssize_t show_type(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct hci_dev *hdev = to_hci_dev(dev);
  158. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  159. }
  160. static ssize_t show_name(struct device *dev,
  161. struct device_attribute *attr, char *buf)
  162. {
  163. struct hci_dev *hdev = to_hci_dev(dev);
  164. char name[HCI_MAX_NAME_LENGTH + 1];
  165. int i;
  166. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  167. name[i] = hdev->dev_name[i];
  168. name[HCI_MAX_NAME_LENGTH] = '\0';
  169. return sprintf(buf, "%s\n", name);
  170. }
  171. static ssize_t show_class(struct device *dev,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct hci_dev *hdev = to_hci_dev(dev);
  175. return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
  176. hdev->dev_class[1], hdev->dev_class[0]);
  177. }
  178. static ssize_t show_address(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. struct hci_dev *hdev = to_hci_dev(dev);
  182. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  183. }
  184. static ssize_t show_features(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct hci_dev *hdev = to_hci_dev(dev);
  188. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  189. hdev->features[0][0], hdev->features[0][1],
  190. hdev->features[0][2], hdev->features[0][3],
  191. hdev->features[0][4], hdev->features[0][5],
  192. hdev->features[0][6], hdev->features[0][7]);
  193. }
  194. static ssize_t show_manufacturer(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. struct hci_dev *hdev = to_hci_dev(dev);
  198. return sprintf(buf, "%d\n", hdev->manufacturer);
  199. }
  200. static ssize_t show_hci_version(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct hci_dev *hdev = to_hci_dev(dev);
  204. return sprintf(buf, "%d\n", hdev->hci_ver);
  205. }
  206. static ssize_t show_hci_revision(struct device *dev,
  207. struct device_attribute *attr, char *buf)
  208. {
  209. struct hci_dev *hdev = to_hci_dev(dev);
  210. return sprintf(buf, "%d\n", hdev->hci_rev);
  211. }
  212. static ssize_t show_idle_timeout(struct device *dev,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. struct hci_dev *hdev = to_hci_dev(dev);
  216. return sprintf(buf, "%d\n", hdev->idle_timeout);
  217. }
  218. static ssize_t store_idle_timeout(struct device *dev,
  219. struct device_attribute *attr,
  220. const char *buf, size_t count)
  221. {
  222. struct hci_dev *hdev = to_hci_dev(dev);
  223. unsigned int val;
  224. int rv;
  225. rv = kstrtouint(buf, 0, &val);
  226. if (rv < 0)
  227. return rv;
  228. if (val != 0 && (val < 500 || val > 3600000))
  229. return -EINVAL;
  230. hdev->idle_timeout = val;
  231. return count;
  232. }
  233. static ssize_t show_sniff_max_interval(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct hci_dev *hdev = to_hci_dev(dev);
  237. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  238. }
  239. static ssize_t store_sniff_max_interval(struct device *dev,
  240. struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. struct hci_dev *hdev = to_hci_dev(dev);
  244. u16 val;
  245. int rv;
  246. rv = kstrtou16(buf, 0, &val);
  247. if (rv < 0)
  248. return rv;
  249. if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
  250. return -EINVAL;
  251. hdev->sniff_max_interval = val;
  252. return count;
  253. }
  254. static ssize_t show_sniff_min_interval(struct device *dev,
  255. struct device_attribute *attr, char *buf)
  256. {
  257. struct hci_dev *hdev = to_hci_dev(dev);
  258. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  259. }
  260. static ssize_t store_sniff_min_interval(struct device *dev,
  261. struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. struct hci_dev *hdev = to_hci_dev(dev);
  265. u16 val;
  266. int rv;
  267. rv = kstrtou16(buf, 0, &val);
  268. if (rv < 0)
  269. return rv;
  270. if (val == 0 || val % 2 || 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. struct hci_dev *hdev = to_hci_dev(dev);
  315. kfree(hdev);
  316. module_put(THIS_MODULE);
  317. }
  318. static struct device_type bt_host = {
  319. .name = "host",
  320. .groups = bt_host_groups,
  321. .release = bt_host_release,
  322. };
  323. void hci_init_sysfs(struct hci_dev *hdev)
  324. {
  325. struct device *dev = &hdev->dev;
  326. dev->type = &bt_host;
  327. dev->class = bt_class;
  328. __module_get(THIS_MODULE);
  329. device_initialize(dev);
  330. }
  331. int __init bt_sysfs_init(void)
  332. {
  333. bt_class = class_create(THIS_MODULE, "bluetooth");
  334. return PTR_ERR_OR_ZERO(bt_class);
  335. }
  336. void bt_sysfs_cleanup(void)
  337. {
  338. class_destroy(bt_class);
  339. }