hci_sysfs.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_VIRTUAL:
  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. case HCI_SDIO:
  27. return "SDIO";
  28. default:
  29. return "UNKNOWN";
  30. }
  31. }
  32. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  33. {
  34. struct hci_dev *hdev = dev_get_drvdata(dev);
  35. return sprintf(buf, "%s\n", typetostr(hdev->type));
  36. }
  37. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  38. {
  39. struct hci_dev *hdev = dev_get_drvdata(dev);
  40. bdaddr_t bdaddr;
  41. baswap(&bdaddr, &hdev->bdaddr);
  42. return sprintf(buf, "%s\n", batostr(&bdaddr));
  43. }
  44. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  45. {
  46. struct hci_dev *hdev = dev_get_drvdata(dev);
  47. return sprintf(buf, "%d\n", hdev->manufacturer);
  48. }
  49. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  50. {
  51. struct hci_dev *hdev = dev_get_drvdata(dev);
  52. return sprintf(buf, "%d\n", hdev->hci_ver);
  53. }
  54. static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
  55. {
  56. struct hci_dev *hdev = dev_get_drvdata(dev);
  57. return sprintf(buf, "%d\n", hdev->hci_rev);
  58. }
  59. static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
  60. {
  61. struct hci_dev *hdev = dev_get_drvdata(dev);
  62. struct inquiry_cache *cache = &hdev->inq_cache;
  63. struct inquiry_entry *e;
  64. int n = 0;
  65. hci_dev_lock_bh(hdev);
  66. for (e = cache->list; e; e = e->next) {
  67. struct inquiry_data *data = &e->data;
  68. bdaddr_t bdaddr;
  69. baswap(&bdaddr, &data->bdaddr);
  70. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
  71. batostr(&bdaddr),
  72. data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
  73. data->dev_class[2], data->dev_class[1], data->dev_class[0],
  74. __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
  75. }
  76. hci_dev_unlock_bh(hdev);
  77. return n;
  78. }
  79. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  80. {
  81. struct hci_dev *hdev = dev_get_drvdata(dev);
  82. return sprintf(buf, "%d\n", hdev->idle_timeout);
  83. }
  84. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  85. {
  86. struct hci_dev *hdev = dev_get_drvdata(dev);
  87. char *ptr;
  88. __u32 val;
  89. val = simple_strtoul(buf, &ptr, 10);
  90. if (ptr == buf)
  91. return -EINVAL;
  92. if (val != 0 && (val < 500 || val > 3600000))
  93. return -EINVAL;
  94. hdev->idle_timeout = val;
  95. return count;
  96. }
  97. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  98. {
  99. struct hci_dev *hdev = dev_get_drvdata(dev);
  100. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  101. }
  102. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  103. {
  104. struct hci_dev *hdev = dev_get_drvdata(dev);
  105. char *ptr;
  106. __u16 val;
  107. val = simple_strtoul(buf, &ptr, 10);
  108. if (ptr == buf)
  109. return -EINVAL;
  110. if (val < 0x0002 || val > 0xFFFE || val % 2)
  111. return -EINVAL;
  112. if (val < hdev->sniff_min_interval)
  113. return -EINVAL;
  114. hdev->sniff_max_interval = val;
  115. return count;
  116. }
  117. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  118. {
  119. struct hci_dev *hdev = dev_get_drvdata(dev);
  120. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  121. }
  122. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  123. {
  124. struct hci_dev *hdev = dev_get_drvdata(dev);
  125. char *ptr;
  126. __u16 val;
  127. val = simple_strtoul(buf, &ptr, 10);
  128. if (ptr == buf)
  129. return -EINVAL;
  130. if (val < 0x0002 || val > 0xFFFE || val % 2)
  131. return -EINVAL;
  132. if (val > hdev->sniff_max_interval)
  133. return -EINVAL;
  134. hdev->sniff_min_interval = val;
  135. return count;
  136. }
  137. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  138. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  139. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  140. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  141. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  142. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  143. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  144. show_idle_timeout, store_idle_timeout);
  145. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  146. show_sniff_max_interval, store_sniff_max_interval);
  147. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  148. show_sniff_min_interval, store_sniff_min_interval);
  149. static struct device_attribute *bt_attrs[] = {
  150. &dev_attr_type,
  151. &dev_attr_address,
  152. &dev_attr_manufacturer,
  153. &dev_attr_hci_version,
  154. &dev_attr_hci_revision,
  155. &dev_attr_inquiry_cache,
  156. &dev_attr_idle_timeout,
  157. &dev_attr_sniff_max_interval,
  158. &dev_attr_sniff_min_interval,
  159. NULL
  160. };
  161. static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
  162. {
  163. struct hci_conn *conn = dev_get_drvdata(dev);
  164. return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
  165. }
  166. static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
  167. {
  168. struct hci_conn *conn = dev_get_drvdata(dev);
  169. bdaddr_t bdaddr;
  170. baswap(&bdaddr, &conn->dst);
  171. return sprintf(buf, "%s\n", batostr(&bdaddr));
  172. }
  173. #define CONN_ATTR(_name,_mode,_show,_store) \
  174. struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
  175. static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
  176. static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
  177. static struct device_attribute *conn_attrs[] = {
  178. &conn_attr_type,
  179. &conn_attr_address,
  180. NULL
  181. };
  182. struct class *bt_class = NULL;
  183. EXPORT_SYMBOL_GPL(bt_class);
  184. static struct bus_type bt_bus = {
  185. .name = "bluetooth",
  186. };
  187. static struct platform_device *bt_platform;
  188. static void bt_release(struct device *dev)
  189. {
  190. void *data = dev_get_drvdata(dev);
  191. kfree(data);
  192. }
  193. static void add_conn(struct work_struct *work)
  194. {
  195. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  196. int i;
  197. if (device_add(&conn->dev) < 0) {
  198. BT_ERR("Failed to register connection device");
  199. return;
  200. }
  201. for (i = 0; conn_attrs[i]; i++)
  202. if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
  203. BT_ERR("Failed to create connection attribute");
  204. }
  205. void hci_conn_add_sysfs(struct hci_conn *conn)
  206. {
  207. struct hci_dev *hdev = conn->hdev;
  208. bdaddr_t *ba = &conn->dst;
  209. BT_DBG("conn %p", conn);
  210. conn->dev.bus = &bt_bus;
  211. conn->dev.parent = &hdev->dev;
  212. conn->dev.release = bt_release;
  213. snprintf(conn->dev.bus_id, BUS_ID_SIZE,
  214. "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
  215. conn->type == ACL_LINK ? "acl" : "sco",
  216. ba->b[5], ba->b[4], ba->b[3],
  217. ba->b[2], ba->b[1], ba->b[0]);
  218. dev_set_drvdata(&conn->dev, conn);
  219. device_initialize(&conn->dev);
  220. INIT_WORK(&conn->work, add_conn);
  221. schedule_work(&conn->work);
  222. }
  223. static void del_conn(struct work_struct *work)
  224. {
  225. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  226. device_del(&conn->dev);
  227. }
  228. void hci_conn_del_sysfs(struct hci_conn *conn)
  229. {
  230. BT_DBG("conn %p", conn);
  231. if (!device_is_registered(&conn->dev))
  232. return;
  233. INIT_WORK(&conn->work, del_conn);
  234. schedule_work(&conn->work);
  235. }
  236. int hci_register_sysfs(struct hci_dev *hdev)
  237. {
  238. struct device *dev = &hdev->dev;
  239. unsigned int i;
  240. int err;
  241. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  242. dev->class = bt_class;
  243. dev->parent = hdev->parent;
  244. strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
  245. dev->release = bt_release;
  246. dev_set_drvdata(dev, hdev);
  247. err = device_register(dev);
  248. if (err < 0)
  249. return err;
  250. for (i = 0; bt_attrs[i]; i++)
  251. if (device_create_file(dev, bt_attrs[i]) < 0)
  252. BT_ERR("Failed to create device attribute");
  253. return 0;
  254. }
  255. void hci_unregister_sysfs(struct hci_dev *hdev)
  256. {
  257. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  258. device_del(&hdev->dev);
  259. }
  260. int __init bt_sysfs_init(void)
  261. {
  262. int err;
  263. bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
  264. if (IS_ERR(bt_platform))
  265. return PTR_ERR(bt_platform);
  266. err = bus_register(&bt_bus);
  267. if (err < 0) {
  268. platform_device_unregister(bt_platform);
  269. return err;
  270. }
  271. bt_class = class_create(THIS_MODULE, "bluetooth");
  272. if (IS_ERR(bt_class)) {
  273. bus_unregister(&bt_bus);
  274. platform_device_unregister(bt_platform);
  275. return PTR_ERR(bt_class);
  276. }
  277. return 0;
  278. }
  279. void bt_sysfs_cleanup(void)
  280. {
  281. class_destroy(bt_class);
  282. bus_unregister(&bt_bus);
  283. platform_device_unregister(bt_platform);
  284. }