hci_sysfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  213. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  214. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  215. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  216. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  217. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  218. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  219. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  220. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  221. static struct attribute *bt_host_attrs[] = {
  222. &dev_attr_bus.attr,
  223. &dev_attr_type.attr,
  224. &dev_attr_name.attr,
  225. &dev_attr_class.attr,
  226. &dev_attr_address.attr,
  227. &dev_attr_features.attr,
  228. &dev_attr_manufacturer.attr,
  229. &dev_attr_hci_version.attr,
  230. &dev_attr_hci_revision.attr,
  231. NULL
  232. };
  233. static struct attribute_group bt_host_group = {
  234. .attrs = bt_host_attrs,
  235. };
  236. static const struct attribute_group *bt_host_groups[] = {
  237. &bt_host_group,
  238. NULL
  239. };
  240. static void bt_host_release(struct device *dev)
  241. {
  242. struct hci_dev *hdev = to_hci_dev(dev);
  243. kfree(hdev);
  244. module_put(THIS_MODULE);
  245. }
  246. static struct device_type bt_host = {
  247. .name = "host",
  248. .groups = bt_host_groups,
  249. .release = bt_host_release,
  250. };
  251. void hci_init_sysfs(struct hci_dev *hdev)
  252. {
  253. struct device *dev = &hdev->dev;
  254. dev->type = &bt_host;
  255. dev->class = bt_class;
  256. __module_get(THIS_MODULE);
  257. device_initialize(dev);
  258. }
  259. int __init bt_sysfs_init(void)
  260. {
  261. bt_class = class_create(THIS_MODULE, "bluetooth");
  262. return PTR_ERR_OR_ZERO(bt_class);
  263. }
  264. void bt_sysfs_cleanup(void)
  265. {
  266. class_destroy(bt_class);
  267. }