hci_sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. ATTRIBUTE_GROUPS(bt_link);
  43. static void bt_link_release(struct device *dev)
  44. {
  45. struct hci_conn *conn = to_hci_conn(dev);
  46. kfree(conn);
  47. }
  48. static struct device_type bt_link = {
  49. .name = "link",
  50. .groups = bt_link_groups,
  51. .release = bt_link_release,
  52. };
  53. /*
  54. * The rfcomm tty device will possibly retain even when conn
  55. * is down, and sysfs doesn't support move zombie device,
  56. * so we should move the device before conn device is destroyed.
  57. */
  58. static int __match_tty(struct device *dev, void *data)
  59. {
  60. return !strncmp(dev_name(dev), "rfcomm", 6);
  61. }
  62. void hci_conn_init_sysfs(struct hci_conn *conn)
  63. {
  64. struct hci_dev *hdev = conn->hdev;
  65. BT_DBG("conn %p", conn);
  66. conn->dev.type = &bt_link;
  67. conn->dev.class = bt_class;
  68. conn->dev.parent = &hdev->dev;
  69. device_initialize(&conn->dev);
  70. }
  71. void hci_conn_add_sysfs(struct hci_conn *conn)
  72. {
  73. struct hci_dev *hdev = conn->hdev;
  74. BT_DBG("conn %p", conn);
  75. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  76. if (device_add(&conn->dev) < 0) {
  77. BT_ERR("Failed to register connection device");
  78. return;
  79. }
  80. hci_dev_hold(hdev);
  81. }
  82. void hci_conn_del_sysfs(struct hci_conn *conn)
  83. {
  84. struct hci_dev *hdev = conn->hdev;
  85. if (!device_is_registered(&conn->dev))
  86. return;
  87. while (1) {
  88. struct device *dev;
  89. dev = device_find_child(&conn->dev, NULL, __match_tty);
  90. if (!dev)
  91. break;
  92. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  93. put_device(dev);
  94. }
  95. device_del(&conn->dev);
  96. hci_dev_put(hdev);
  97. }
  98. static inline char *host_typetostr(int type)
  99. {
  100. switch (type) {
  101. case HCI_BREDR:
  102. return "BR/EDR";
  103. case HCI_AMP:
  104. return "AMP";
  105. default:
  106. return "UNKNOWN";
  107. }
  108. }
  109. static ssize_t show_type(struct device *dev,
  110. struct device_attribute *attr, char *buf)
  111. {
  112. struct hci_dev *hdev = to_hci_dev(dev);
  113. return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
  114. }
  115. static ssize_t show_name(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct hci_dev *hdev = to_hci_dev(dev);
  119. char name[HCI_MAX_NAME_LENGTH + 1];
  120. int i;
  121. for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
  122. name[i] = hdev->dev_name[i];
  123. name[HCI_MAX_NAME_LENGTH] = '\0';
  124. return sprintf(buf, "%s\n", name);
  125. }
  126. static ssize_t show_address(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. struct hci_dev *hdev = to_hci_dev(dev);
  130. return sprintf(buf, "%pMR\n", &hdev->bdaddr);
  131. }
  132. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  133. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  134. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  135. static struct attribute *bt_host_attrs[] = {
  136. &dev_attr_type.attr,
  137. &dev_attr_name.attr,
  138. &dev_attr_address.attr,
  139. NULL
  140. };
  141. ATTRIBUTE_GROUPS(bt_host);
  142. static void bt_host_release(struct device *dev)
  143. {
  144. struct hci_dev *hdev = to_hci_dev(dev);
  145. kfree(hdev);
  146. module_put(THIS_MODULE);
  147. }
  148. static struct device_type bt_host = {
  149. .name = "host",
  150. .groups = bt_host_groups,
  151. .release = bt_host_release,
  152. };
  153. void hci_init_sysfs(struct hci_dev *hdev)
  154. {
  155. struct device *dev = &hdev->dev;
  156. dev->type = &bt_host;
  157. dev->class = bt_class;
  158. __module_get(THIS_MODULE);
  159. device_initialize(dev);
  160. }
  161. int __init bt_sysfs_init(void)
  162. {
  163. bt_class = class_create(THIS_MODULE, "bluetooth");
  164. return PTR_ERR_OR_ZERO(bt_class);
  165. }
  166. void bt_sysfs_cleanup(void)
  167. {
  168. class_destroy(bt_class);
  169. }