hci_sysfs.c 3.6 KB

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