hci_sysfs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 void bt_link_release(struct device *dev)
  7. {
  8. struct hci_conn *conn = to_hci_conn(dev);
  9. kfree(conn);
  10. }
  11. static struct device_type bt_link = {
  12. .name = "link",
  13. .release = bt_link_release,
  14. };
  15. /*
  16. * The rfcomm tty device will possibly retain even when conn
  17. * is down, and sysfs doesn't support move zombie device,
  18. * so we should move the device before conn device is destroyed.
  19. */
  20. static int __match_tty(struct device *dev, void *data)
  21. {
  22. return !strncmp(dev_name(dev), "rfcomm", 6);
  23. }
  24. void hci_conn_init_sysfs(struct hci_conn *conn)
  25. {
  26. struct hci_dev *hdev = conn->hdev;
  27. BT_DBG("conn %p", conn);
  28. conn->dev.type = &bt_link;
  29. conn->dev.class = bt_class;
  30. conn->dev.parent = &hdev->dev;
  31. device_initialize(&conn->dev);
  32. }
  33. void hci_conn_add_sysfs(struct hci_conn *conn)
  34. {
  35. struct hci_dev *hdev = conn->hdev;
  36. BT_DBG("conn %p", conn);
  37. dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
  38. if (device_add(&conn->dev) < 0) {
  39. BT_ERR("Failed to register connection device");
  40. return;
  41. }
  42. hci_dev_hold(hdev);
  43. }
  44. void hci_conn_del_sysfs(struct hci_conn *conn)
  45. {
  46. struct hci_dev *hdev = conn->hdev;
  47. if (!device_is_registered(&conn->dev))
  48. return;
  49. while (1) {
  50. struct device *dev;
  51. dev = device_find_child(&conn->dev, NULL, __match_tty);
  52. if (!dev)
  53. break;
  54. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  55. put_device(dev);
  56. }
  57. device_del(&conn->dev);
  58. hci_dev_put(hdev);
  59. }
  60. static void bt_host_release(struct device *dev)
  61. {
  62. struct hci_dev *hdev = to_hci_dev(dev);
  63. kfree(hdev);
  64. module_put(THIS_MODULE);
  65. }
  66. static struct device_type bt_host = {
  67. .name = "host",
  68. .release = bt_host_release,
  69. };
  70. void hci_init_sysfs(struct hci_dev *hdev)
  71. {
  72. struct device *dev = &hdev->dev;
  73. dev->type = &bt_host;
  74. dev->class = bt_class;
  75. __module_get(THIS_MODULE);
  76. device_initialize(dev);
  77. }
  78. int __init bt_sysfs_init(void)
  79. {
  80. bt_class = class_create(THIS_MODULE, "bluetooth");
  81. return PTR_ERR_OR_ZERO(bt_class);
  82. }
  83. void bt_sysfs_cleanup(void)
  84. {
  85. class_destroy(bt_class);
  86. }