hci_sysfs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_typetostr(int type)
  105. {
  106. switch (type) {
  107. case HCI_BREDR:
  108. return "BR/EDR";
  109. case HCI_AMP:
  110. return "AMP";
  111. default:
  112. return "UNKNOWN";
  113. }
  114. }
  115. static ssize_t show_type(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct hci_dev *hdev = to_hci_dev(dev);
  119. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  120. }
  121. static ssize_t show_name(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct hci_dev *hdev = to_hci_dev(dev);
  125. char name[HCI_MAX_NAME_LENGTH + 1];
  126. int i;
  127. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  128. name[i] = hdev->dev_name[i];
  129. name[HCI_MAX_NAME_LENGTH] = '\0';
  130. return sprintf(buf, "%s\n", name);
  131. }
  132. static ssize_t show_address(struct device *dev,
  133. struct device_attribute *attr, char *buf)
  134. {
  135. struct hci_dev *hdev = to_hci_dev(dev);
  136. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  137. }
  138. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  139. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  140. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  141. static struct attribute *bt_host_attrs[] = {
  142. &dev_attr_type.attr,
  143. &dev_attr_name.attr,
  144. &dev_attr_address.attr,
  145. NULL
  146. };
  147. static struct attribute_group bt_host_group = {
  148. .attrs = bt_host_attrs,
  149. };
  150. static const struct attribute_group *bt_host_groups[] = {
  151. &bt_host_group,
  152. NULL
  153. };
  154. static void bt_host_release(struct device *dev)
  155. {
  156. struct hci_dev *hdev = to_hci_dev(dev);
  157. kfree(hdev);
  158. module_put(THIS_MODULE);
  159. }
  160. static struct device_type bt_host = {
  161. .name = "host",
  162. .groups = bt_host_groups,
  163. .release = bt_host_release,
  164. };
  165. void hci_init_sysfs(struct hci_dev *hdev)
  166. {
  167. struct device *dev = &hdev->dev;
  168. dev->type = &bt_host;
  169. dev->class = bt_class;
  170. __module_get(THIS_MODULE);
  171. device_initialize(dev);
  172. }
  173. int __init bt_sysfs_init(void)
  174. {
  175. bt_class = class_create(THIS_MODULE, "bluetooth");
  176. return PTR_ERR_OR_ZERO(bt_class);
  177. }
  178. void bt_sysfs_cleanup(void)
  179. {
  180. class_destroy(bt_class);
  181. }