hci_sysfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Bluetooth HCI driver model support. */
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/platform_device.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 device *dev, struct device_attribute *attr, char *buf)
  12. {
  13. struct hci_dev *hdev = dev_get_drvdata(dev);
  14. return sprintf(buf, "%s\n", hdev->name);
  15. }
  16. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  17. {
  18. struct hci_dev *hdev = dev_get_drvdata(dev);
  19. return sprintf(buf, "%d\n", hdev->type);
  20. }
  21. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  22. {
  23. struct hci_dev *hdev = dev_get_drvdata(dev);
  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 device *dev, struct device_attribute *attr, char *buf)
  29. {
  30. struct hci_dev *hdev = dev_get_drvdata(dev);
  31. return sprintf(buf, "0x%lx\n", hdev->flags);
  32. }
  33. static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
  34. {
  35. struct hci_dev *hdev = dev_get_drvdata(dev);
  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 ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  54. {
  55. struct hci_dev *hdev = dev_get_drvdata(dev);
  56. return sprintf(buf, "%d\n", hdev->idle_timeout);
  57. }
  58. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  59. {
  60. struct hci_dev *hdev = dev_get_drvdata(dev);
  61. char *ptr;
  62. __u32 val;
  63. val = simple_strtoul(buf, &ptr, 10);
  64. if (ptr == buf)
  65. return -EINVAL;
  66. if (val != 0 && (val < 500 || val > 3600000))
  67. return -EINVAL;
  68. hdev->idle_timeout = val;
  69. return count;
  70. }
  71. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  72. {
  73. struct hci_dev *hdev = dev_get_drvdata(dev);
  74. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  75. }
  76. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  77. {
  78. struct hci_dev *hdev = dev_get_drvdata(dev);
  79. char *ptr;
  80. __u16 val;
  81. val = simple_strtoul(buf, &ptr, 10);
  82. if (ptr == buf)
  83. return -EINVAL;
  84. if (val < 0x0002 || val > 0xFFFE || val % 2)
  85. return -EINVAL;
  86. if (val < hdev->sniff_min_interval)
  87. return -EINVAL;
  88. hdev->sniff_max_interval = val;
  89. return count;
  90. }
  91. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  92. {
  93. struct hci_dev *hdev = dev_get_drvdata(dev);
  94. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  95. }
  96. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  97. {
  98. struct hci_dev *hdev = dev_get_drvdata(dev);
  99. char *ptr;
  100. __u16 val;
  101. val = simple_strtoul(buf, &ptr, 10);
  102. if (ptr == buf)
  103. return -EINVAL;
  104. if (val < 0x0002 || val > 0xFFFE || val % 2)
  105. return -EINVAL;
  106. if (val > hdev->sniff_max_interval)
  107. return -EINVAL;
  108. hdev->sniff_min_interval = val;
  109. return count;
  110. }
  111. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  112. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  113. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  114. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  115. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  116. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  117. show_idle_timeout, store_idle_timeout);
  118. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  119. show_sniff_max_interval, store_sniff_max_interval);
  120. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  121. show_sniff_min_interval, store_sniff_min_interval);
  122. static struct device_attribute *bt_attrs[] = {
  123. &dev_attr_name,
  124. &dev_attr_type,
  125. &dev_attr_address,
  126. &dev_attr_flags,
  127. &dev_attr_inquiry_cache,
  128. &dev_attr_idle_timeout,
  129. &dev_attr_sniff_max_interval,
  130. &dev_attr_sniff_min_interval,
  131. NULL
  132. };
  133. struct class *bt_class = NULL;
  134. EXPORT_SYMBOL_GPL(bt_class);
  135. static struct bus_type bt_bus = {
  136. .name = "bluetooth",
  137. };
  138. static struct platform_device *bt_platform;
  139. static void bt_release(struct device *dev)
  140. {
  141. struct hci_dev *hdev = dev_get_drvdata(dev);
  142. kfree(hdev);
  143. }
  144. int hci_register_sysfs(struct hci_dev *hdev)
  145. {
  146. struct device *dev = &hdev->dev;
  147. unsigned int i;
  148. int err;
  149. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  150. dev->class = bt_class;
  151. if (hdev->parent)
  152. dev->parent = hdev->parent;
  153. else
  154. dev->parent = &bt_platform->dev;
  155. strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
  156. dev->release = bt_release;
  157. dev_set_drvdata(dev, hdev);
  158. err = device_register(dev);
  159. if (err < 0)
  160. return err;
  161. for (i = 0; bt_attrs[i]; i++)
  162. device_create_file(dev, bt_attrs[i]);
  163. return 0;
  164. }
  165. void hci_unregister_sysfs(struct hci_dev *hdev)
  166. {
  167. struct device *dev = &hdev->dev;
  168. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  169. device_del(dev);
  170. }
  171. int __init bt_sysfs_init(void)
  172. {
  173. int err;
  174. bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
  175. if (IS_ERR(bt_platform))
  176. return PTR_ERR(bt_platform);
  177. err = bus_register(&bt_bus);
  178. if (err < 0) {
  179. platform_device_unregister(bt_platform);
  180. return err;
  181. }
  182. bt_class = class_create(THIS_MODULE, "bluetooth");
  183. if (IS_ERR(bt_class)) {
  184. bus_unregister(&bt_bus);
  185. platform_device_unregister(bt_platform);
  186. return PTR_ERR(bt_class);
  187. }
  188. return 0;
  189. }
  190. void __exit bt_sysfs_cleanup(void)
  191. {
  192. class_destroy(bt_class);
  193. bus_unregister(&bt_bus);
  194. platform_device_unregister(bt_platform);
  195. }