hci_sysfs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <net/bluetooth/bluetooth.h>
  5. #include <net/bluetooth/hci_core.h>
  6. #ifndef CONFIG_BT_HCI_CORE_DEBUG
  7. #undef BT_DBG
  8. #define BT_DBG(D...)
  9. #endif
  10. static ssize_t show_name(struct class_device *cdev, char *buf)
  11. {
  12. struct hci_dev *hdev = class_get_devdata(cdev);
  13. return sprintf(buf, "%s\n", hdev->name);
  14. }
  15. static ssize_t show_type(struct class_device *cdev, char *buf)
  16. {
  17. struct hci_dev *hdev = class_get_devdata(cdev);
  18. return sprintf(buf, "%d\n", hdev->type);
  19. }
  20. static ssize_t show_address(struct class_device *cdev, char *buf)
  21. {
  22. struct hci_dev *hdev = class_get_devdata(cdev);
  23. bdaddr_t bdaddr;
  24. baswap(&bdaddr, &hdev->bdaddr);
  25. return sprintf(buf, "%s\n", batostr(&bdaddr));
  26. }
  27. static ssize_t show_flags(struct class_device *cdev, char *buf)
  28. {
  29. struct hci_dev *hdev = class_get_devdata(cdev);
  30. return sprintf(buf, "0x%lx\n", hdev->flags);
  31. }
  32. static ssize_t show_inquiry_cache(struct class_device *cdev, char *buf)
  33. {
  34. struct hci_dev *hdev = class_get_devdata(cdev);
  35. struct inquiry_cache *cache = &hdev->inq_cache;
  36. struct inquiry_entry *e;
  37. int n = 0;
  38. hci_dev_lock_bh(hdev);
  39. for (e = cache->list; e; e = e->next) {
  40. struct inquiry_data *data = &e->data;
  41. bdaddr_t bdaddr;
  42. baswap(&bdaddr, &data->bdaddr);
  43. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
  44. batostr(&bdaddr),
  45. data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
  46. data->dev_class[2], data->dev_class[1], data->dev_class[0],
  47. __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
  48. }
  49. hci_dev_unlock_bh(hdev);
  50. return n;
  51. }
  52. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  53. static CLASS_DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  54. static CLASS_DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  55. static CLASS_DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  56. static CLASS_DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  57. static struct class_device_attribute *bt_attrs[] = {
  58. &class_device_attr_name,
  59. &class_device_attr_type,
  60. &class_device_attr_address,
  61. &class_device_attr_flags,
  62. &class_device_attr_inquiry_cache,
  63. NULL
  64. };
  65. #ifdef CONFIG_HOTPLUG
  66. static int bt_uevent(struct class_device *cdev, char **envp, int num_envp, char *buf, int size)
  67. {
  68. struct hci_dev *hdev = class_get_devdata(cdev);
  69. int n, i = 0;
  70. envp[i++] = buf;
  71. n = snprintf(buf, size, "INTERFACE=%s", hdev->name) + 1;
  72. buf += n;
  73. size -= n;
  74. if ((size <= 0) || (i >= num_envp))
  75. return -ENOMEM;
  76. envp[i] = NULL;
  77. return 0;
  78. }
  79. #endif
  80. static void bt_release(struct class_device *cdev)
  81. {
  82. struct hci_dev *hdev = class_get_devdata(cdev);
  83. kfree(hdev);
  84. }
  85. struct class bt_class = {
  86. .name = "bluetooth",
  87. .release = bt_release,
  88. #ifdef CONFIG_HOTPLUG
  89. .uevent = bt_uevent,
  90. #endif
  91. };
  92. EXPORT_SYMBOL_GPL(bt_class);
  93. int hci_register_sysfs(struct hci_dev *hdev)
  94. {
  95. struct class_device *cdev = &hdev->class_dev;
  96. unsigned int i;
  97. int err;
  98. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  99. cdev->class = &bt_class;
  100. class_set_devdata(cdev, hdev);
  101. strlcpy(cdev->class_id, hdev->name, BUS_ID_SIZE);
  102. err = class_device_register(cdev);
  103. if (err < 0)
  104. return err;
  105. for (i = 0; bt_attrs[i]; i++)
  106. class_device_create_file(cdev, bt_attrs[i]);
  107. return 0;
  108. }
  109. void hci_unregister_sysfs(struct hci_dev *hdev)
  110. {
  111. struct class_device * cdev = &hdev->class_dev;
  112. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  113. class_device_del(cdev);
  114. }
  115. int __init bt_sysfs_init(void)
  116. {
  117. return class_register(&bt_class);
  118. }
  119. void __exit bt_sysfs_cleanup(void)
  120. {
  121. class_unregister(&bt_class);
  122. }