hci_sysfs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 inline char *typetostr(int type)
  12. {
  13. switch (type) {
  14. case HCI_VHCI:
  15. return "VIRTUAL";
  16. case HCI_USB:
  17. return "USB";
  18. case HCI_PCCARD:
  19. return "PCCARD";
  20. case HCI_UART:
  21. return "UART";
  22. case HCI_RS232:
  23. return "RS232";
  24. case HCI_PCI:
  25. return "PCI";
  26. default:
  27. return "UNKNOWN";
  28. }
  29. }
  30. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  31. {
  32. struct hci_dev *hdev = dev_get_drvdata(dev);
  33. return sprintf(buf, "%s\n", typetostr(hdev->type));
  34. }
  35. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  36. {
  37. struct hci_dev *hdev = dev_get_drvdata(dev);
  38. bdaddr_t bdaddr;
  39. baswap(&bdaddr, &hdev->bdaddr);
  40. return sprintf(buf, "%s\n", batostr(&bdaddr));
  41. }
  42. static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
  43. {
  44. struct hci_dev *hdev = dev_get_drvdata(dev);
  45. struct inquiry_cache *cache = &hdev->inq_cache;
  46. struct inquiry_entry *e;
  47. int n = 0;
  48. hci_dev_lock_bh(hdev);
  49. for (e = cache->list; e; e = e->next) {
  50. struct inquiry_data *data = &e->data;
  51. bdaddr_t bdaddr;
  52. baswap(&bdaddr, &data->bdaddr);
  53. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
  54. batostr(&bdaddr),
  55. data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
  56. data->dev_class[2], data->dev_class[1], data->dev_class[0],
  57. __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
  58. }
  59. hci_dev_unlock_bh(hdev);
  60. return n;
  61. }
  62. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  63. {
  64. struct hci_dev *hdev = dev_get_drvdata(dev);
  65. return sprintf(buf, "%d\n", hdev->idle_timeout);
  66. }
  67. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  68. {
  69. struct hci_dev *hdev = dev_get_drvdata(dev);
  70. char *ptr;
  71. __u32 val;
  72. val = simple_strtoul(buf, &ptr, 10);
  73. if (ptr == buf)
  74. return -EINVAL;
  75. if (val != 0 && (val < 500 || val > 3600000))
  76. return -EINVAL;
  77. hdev->idle_timeout = val;
  78. return count;
  79. }
  80. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  81. {
  82. struct hci_dev *hdev = dev_get_drvdata(dev);
  83. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  84. }
  85. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  86. {
  87. struct hci_dev *hdev = dev_get_drvdata(dev);
  88. char *ptr;
  89. __u16 val;
  90. val = simple_strtoul(buf, &ptr, 10);
  91. if (ptr == buf)
  92. return -EINVAL;
  93. if (val < 0x0002 || val > 0xFFFE || val % 2)
  94. return -EINVAL;
  95. if (val < hdev->sniff_min_interval)
  96. return -EINVAL;
  97. hdev->sniff_max_interval = val;
  98. return count;
  99. }
  100. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  101. {
  102. struct hci_dev *hdev = dev_get_drvdata(dev);
  103. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  104. }
  105. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  106. {
  107. struct hci_dev *hdev = dev_get_drvdata(dev);
  108. char *ptr;
  109. __u16 val;
  110. val = simple_strtoul(buf, &ptr, 10);
  111. if (ptr == buf)
  112. return -EINVAL;
  113. if (val < 0x0002 || val > 0xFFFE || val % 2)
  114. return -EINVAL;
  115. if (val > hdev->sniff_max_interval)
  116. return -EINVAL;
  117. hdev->sniff_min_interval = val;
  118. return count;
  119. }
  120. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  121. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  122. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  123. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  124. show_idle_timeout, store_idle_timeout);
  125. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  126. show_sniff_max_interval, store_sniff_max_interval);
  127. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  128. show_sniff_min_interval, store_sniff_min_interval);
  129. static struct device_attribute *bt_attrs[] = {
  130. &dev_attr_type,
  131. &dev_attr_address,
  132. &dev_attr_inquiry_cache,
  133. &dev_attr_idle_timeout,
  134. &dev_attr_sniff_max_interval,
  135. &dev_attr_sniff_min_interval,
  136. NULL
  137. };
  138. struct class *bt_class = NULL;
  139. EXPORT_SYMBOL_GPL(bt_class);
  140. static struct bus_type bt_bus = {
  141. .name = "bluetooth",
  142. };
  143. static struct platform_device *bt_platform;
  144. static void bt_release(struct device *dev)
  145. {
  146. struct hci_dev *hdev = dev_get_drvdata(dev);
  147. kfree(hdev);
  148. }
  149. int hci_register_sysfs(struct hci_dev *hdev)
  150. {
  151. struct device *dev = &hdev->dev;
  152. unsigned int i;
  153. int err;
  154. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  155. dev->class = bt_class;
  156. if (hdev->parent)
  157. dev->parent = hdev->parent;
  158. else
  159. dev->parent = &bt_platform->dev;
  160. strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
  161. dev->release = bt_release;
  162. dev_set_drvdata(dev, hdev);
  163. err = device_register(dev);
  164. if (err < 0)
  165. return err;
  166. for (i = 0; bt_attrs[i]; i++)
  167. device_create_file(dev, bt_attrs[i]);
  168. return 0;
  169. }
  170. void hci_unregister_sysfs(struct hci_dev *hdev)
  171. {
  172. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  173. device_del(&hdev->dev);
  174. }
  175. int __init bt_sysfs_init(void)
  176. {
  177. int err;
  178. bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
  179. if (IS_ERR(bt_platform))
  180. return PTR_ERR(bt_platform);
  181. err = bus_register(&bt_bus);
  182. if (err < 0) {
  183. platform_device_unregister(bt_platform);
  184. return err;
  185. }
  186. bt_class = class_create(THIS_MODULE, "bluetooth");
  187. if (IS_ERR(bt_class)) {
  188. bus_unregister(&bt_bus);
  189. platform_device_unregister(bt_platform);
  190. return PTR_ERR(bt_class);
  191. }
  192. return 0;
  193. }
  194. void __exit bt_sysfs_cleanup(void)
  195. {
  196. class_destroy(bt_class);
  197. bus_unregister(&bt_bus);
  198. platform_device_unregister(bt_platform);
  199. }