hci_sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #define LINK_ATTR(_name, _mode, _show, _store) \
  34. struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
  35. static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
  36. static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
  37. static struct attribute *bt_link_attrs[] = {
  38. &link_attr_type.attr,
  39. &link_attr_address.attr,
  40. NULL
  41. };
  42. static struct attribute_group bt_link_group = {
  43. .attrs = bt_link_attrs,
  44. };
  45. static const struct attribute_group *bt_link_groups[] = {
  46. &bt_link_group,
  47. NULL
  48. };
  49. static void bt_link_release(struct device *dev)
  50. {
  51. struct hci_conn *conn = to_hci_conn(dev);
  52. kfree(conn);
  53. }
  54. static struct device_type bt_link = {
  55. .name = "link",
  56. .groups = bt_link_groups,
  57. .release = bt_link_release,
  58. };
  59. /*
  60. * The rfcomm tty device will possibly retain even when conn
  61. * is down, and sysfs doesn't support move zombie device,
  62. * so we should move the device before conn device is destroyed.
  63. */
  64. static int __match_tty(struct device *dev, void *data)
  65. {
  66. return !strncmp(dev_name(dev), "rfcomm", 6);
  67. }
  68. void hci_conn_init_sysfs(struct hci_conn *conn)
  69. {
  70. struct hci_dev *hdev = conn->hdev;
  71. BT_DBG("conn %p", conn);
  72. conn->dev.type = &bt_link;
  73. conn->dev.class = bt_class;
  74. conn->dev.parent = &hdev->dev;
  75. device_initialize(&conn->dev);
  76. }
  77. void hci_conn_add_sysfs(struct hci_conn *conn)
  78. {
  79. struct hci_dev *hdev = conn->hdev;
  80. BT_DBG("conn %p", conn);
  81. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  82. if (device_add(&conn->dev) < 0) {
  83. BT_ERR("Failed to register connection device");
  84. return;
  85. }
  86. hci_dev_hold(hdev);
  87. }
  88. void hci_conn_del_sysfs(struct hci_conn *conn)
  89. {
  90. struct hci_dev *hdev = conn->hdev;
  91. if (!device_is_registered(&conn->dev))
  92. return;
  93. while (1) {
  94. struct device *dev;
  95. dev = device_find_child(&conn->dev, NULL, __match_tty);
  96. if (!dev)
  97. break;
  98. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  99. put_device(dev);
  100. }
  101. device_del(&conn->dev);
  102. hci_dev_put(hdev);
  103. }
  104. static inline char *host_bustostr(int bus)
  105. {
  106. switch (bus) {
  107. case HCI_VIRTUAL:
  108. return "VIRTUAL";
  109. case HCI_USB:
  110. return "USB";
  111. case HCI_PCCARD:
  112. return "PCCARD";
  113. case HCI_UART:
  114. return "UART";
  115. case HCI_RS232:
  116. return "RS232";
  117. case HCI_PCI:
  118. return "PCI";
  119. case HCI_SDIO:
  120. return "SDIO";
  121. default:
  122. return "UNKNOWN";
  123. }
  124. }
  125. static inline char *host_typetostr(int type)
  126. {
  127. switch (type) {
  128. case HCI_BREDR:
  129. return "BR/EDR";
  130. case HCI_AMP:
  131. return "AMP";
  132. default:
  133. return "UNKNOWN";
  134. }
  135. }
  136. static ssize_t show_bus(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct hci_dev *hdev = to_hci_dev(dev);
  140. return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
  141. }
  142. static ssize_t show_type(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct hci_dev *hdev = to_hci_dev(dev);
  146. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  147. }
  148. static ssize_t show_name(struct device *dev,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. struct hci_dev *hdev = to_hci_dev(dev);
  152. char name[HCI_MAX_NAME_LENGTH + 1];
  153. int i;
  154. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  155. name[i] = hdev->dev_name[i];
  156. name[HCI_MAX_NAME_LENGTH] = '\0';
  157. return sprintf(buf, "%s\n", name);
  158. }
  159. static ssize_t show_class(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. struct hci_dev *hdev = to_hci_dev(dev);
  163. return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
  164. hdev->dev_class[1], hdev->dev_class[0]);
  165. }
  166. static ssize_t show_address(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct hci_dev *hdev = to_hci_dev(dev);
  170. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  171. }
  172. static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
  173. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  174. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  175. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  176. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  177. static struct attribute *bt_host_attrs[] = {
  178. &dev_attr_bus.attr,
  179. &dev_attr_type.attr,
  180. &dev_attr_name.attr,
  181. &dev_attr_class.attr,
  182. &dev_attr_address.attr,
  183. NULL
  184. };
  185. static struct attribute_group bt_host_group = {
  186. .attrs = bt_host_attrs,
  187. };
  188. static const struct attribute_group *bt_host_groups[] = {
  189. &bt_host_group,
  190. NULL
  191. };
  192. static void bt_host_release(struct device *dev)
  193. {
  194. struct hci_dev *hdev = to_hci_dev(dev);
  195. kfree(hdev);
  196. module_put(THIS_MODULE);
  197. }
  198. static struct device_type bt_host = {
  199. .name = "host",
  200. .groups = bt_host_groups,
  201. .release = bt_host_release,
  202. };
  203. void hci_init_sysfs(struct hci_dev *hdev)
  204. {
  205. struct device *dev = &hdev->dev;
  206. dev->type = &bt_host;
  207. dev->class = bt_class;
  208. __module_get(THIS_MODULE);
  209. device_initialize(dev);
  210. }
  211. int __init bt_sysfs_init(void)
  212. {
  213. bt_class = class_create(THIS_MODULE, "bluetooth");
  214. return PTR_ERR_OR_ZERO(bt_class);
  215. }
  216. void bt_sysfs_cleanup(void)
  217. {
  218. class_destroy(bt_class);
  219. }