hci_sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_name(struct device *dev, struct device_attribute *attr, char *buf)
  38. {
  39. struct hci_dev *hdev = dev_get_drvdata(dev);
  40. char name[249];
  41. int i;
  42. for (i = 0; i < 248; i++)
  43. name[i] = hdev->dev_name[i];
  44. name[248] = '\0';
  45. return sprintf(buf, "%s\n", name);
  46. }
  47. static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
  48. {
  49. struct hci_dev *hdev = dev_get_drvdata(dev);
  50. return sprintf(buf, "0x%.2x%.2x%.2x\n",
  51. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  52. }
  53. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  54. {
  55. struct hci_dev *hdev = dev_get_drvdata(dev);
  56. bdaddr_t bdaddr;
  57. baswap(&bdaddr, &hdev->bdaddr);
  58. return sprintf(buf, "%s\n", batostr(&bdaddr));
  59. }
  60. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  61. {
  62. struct hci_dev *hdev = dev_get_drvdata(dev);
  63. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  64. hdev->features[0], hdev->features[1],
  65. hdev->features[2], hdev->features[3],
  66. hdev->features[4], hdev->features[5],
  67. hdev->features[6], hdev->features[7]);
  68. }
  69. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  70. {
  71. struct hci_dev *hdev = dev_get_drvdata(dev);
  72. return sprintf(buf, "%d\n", hdev->manufacturer);
  73. }
  74. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  75. {
  76. struct hci_dev *hdev = dev_get_drvdata(dev);
  77. return sprintf(buf, "%d\n", hdev->hci_ver);
  78. }
  79. static ssize_t show_hci_revision(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->hci_rev);
  83. }
  84. static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
  85. {
  86. struct hci_dev *hdev = dev_get_drvdata(dev);
  87. struct inquiry_cache *cache = &hdev->inq_cache;
  88. struct inquiry_entry *e;
  89. int n = 0;
  90. hci_dev_lock_bh(hdev);
  91. for (e = cache->list; e; e = e->next) {
  92. struct inquiry_data *data = &e->data;
  93. bdaddr_t bdaddr;
  94. baswap(&bdaddr, &data->bdaddr);
  95. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
  96. batostr(&bdaddr),
  97. data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
  98. data->dev_class[2], data->dev_class[1], data->dev_class[0],
  99. __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
  100. }
  101. hci_dev_unlock_bh(hdev);
  102. return n;
  103. }
  104. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  105. {
  106. struct hci_dev *hdev = dev_get_drvdata(dev);
  107. return sprintf(buf, "%d\n", hdev->idle_timeout);
  108. }
  109. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  110. {
  111. struct hci_dev *hdev = dev_get_drvdata(dev);
  112. char *ptr;
  113. __u32 val;
  114. val = simple_strtoul(buf, &ptr, 10);
  115. if (ptr == buf)
  116. return -EINVAL;
  117. if (val != 0 && (val < 500 || val > 3600000))
  118. return -EINVAL;
  119. hdev->idle_timeout = val;
  120. return count;
  121. }
  122. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  123. {
  124. struct hci_dev *hdev = dev_get_drvdata(dev);
  125. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  126. }
  127. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  128. {
  129. struct hci_dev *hdev = dev_get_drvdata(dev);
  130. char *ptr;
  131. __u16 val;
  132. val = simple_strtoul(buf, &ptr, 10);
  133. if (ptr == buf)
  134. return -EINVAL;
  135. if (val < 0x0002 || val > 0xFFFE || val % 2)
  136. return -EINVAL;
  137. if (val < hdev->sniff_min_interval)
  138. return -EINVAL;
  139. hdev->sniff_max_interval = val;
  140. return count;
  141. }
  142. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  143. {
  144. struct hci_dev *hdev = dev_get_drvdata(dev);
  145. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  146. }
  147. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  148. {
  149. struct hci_dev *hdev = dev_get_drvdata(dev);
  150. char *ptr;
  151. __u16 val;
  152. val = simple_strtoul(buf, &ptr, 10);
  153. if (ptr == buf)
  154. return -EINVAL;
  155. if (val < 0x0002 || val > 0xFFFE || val % 2)
  156. return -EINVAL;
  157. if (val > hdev->sniff_max_interval)
  158. return -EINVAL;
  159. hdev->sniff_min_interval = val;
  160. return count;
  161. }
  162. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  163. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  164. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  165. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  166. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  167. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  168. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  169. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  170. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  171. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  172. show_idle_timeout, store_idle_timeout);
  173. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  174. show_sniff_max_interval, store_sniff_max_interval);
  175. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  176. show_sniff_min_interval, store_sniff_min_interval);
  177. static struct device_attribute *bt_attrs[] = {
  178. &dev_attr_type,
  179. &dev_attr_name,
  180. &dev_attr_class,
  181. &dev_attr_address,
  182. &dev_attr_features,
  183. &dev_attr_manufacturer,
  184. &dev_attr_hci_version,
  185. &dev_attr_hci_revision,
  186. &dev_attr_inquiry_cache,
  187. &dev_attr_idle_timeout,
  188. &dev_attr_sniff_max_interval,
  189. &dev_attr_sniff_min_interval,
  190. NULL
  191. };
  192. static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
  193. {
  194. struct hci_conn *conn = dev_get_drvdata(dev);
  195. return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
  196. }
  197. static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
  198. {
  199. struct hci_conn *conn = dev_get_drvdata(dev);
  200. bdaddr_t bdaddr;
  201. baswap(&bdaddr, &conn->dst);
  202. return sprintf(buf, "%s\n", batostr(&bdaddr));
  203. }
  204. #define CONN_ATTR(_name,_mode,_show,_store) \
  205. struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
  206. static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
  207. static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
  208. static struct device_attribute *conn_attrs[] = {
  209. &conn_attr_type,
  210. &conn_attr_address,
  211. NULL
  212. };
  213. struct class *bt_class = NULL;
  214. EXPORT_SYMBOL_GPL(bt_class);
  215. static struct bus_type bt_bus = {
  216. .name = "bluetooth",
  217. };
  218. static struct platform_device *bt_platform;
  219. static void bt_release(struct device *dev)
  220. {
  221. void *data = dev_get_drvdata(dev);
  222. kfree(data);
  223. }
  224. static void add_conn(struct work_struct *work)
  225. {
  226. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  227. int i;
  228. if (device_add(&conn->dev) < 0) {
  229. BT_ERR("Failed to register connection device");
  230. return;
  231. }
  232. for (i = 0; conn_attrs[i]; i++)
  233. if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
  234. BT_ERR("Failed to create connection attribute");
  235. }
  236. void hci_conn_add_sysfs(struct hci_conn *conn)
  237. {
  238. struct hci_dev *hdev = conn->hdev;
  239. bdaddr_t *ba = &conn->dst;
  240. BT_DBG("conn %p", conn);
  241. conn->dev.bus = &bt_bus;
  242. conn->dev.parent = &hdev->dev;
  243. conn->dev.release = bt_release;
  244. snprintf(conn->dev.bus_id, BUS_ID_SIZE,
  245. "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
  246. conn->type == ACL_LINK ? "acl" : "sco",
  247. ba->b[5], ba->b[4], ba->b[3],
  248. ba->b[2], ba->b[1], ba->b[0]);
  249. dev_set_drvdata(&conn->dev, conn);
  250. device_initialize(&conn->dev);
  251. INIT_WORK(&conn->work, add_conn);
  252. schedule_work(&conn->work);
  253. }
  254. static int __match_tty(struct device *dev, void *data)
  255. {
  256. /* The rfcomm tty device will possibly retain even when conn
  257. * is down, and sysfs doesn't support move zombie device,
  258. * so we should move the device before conn device is destroyed.
  259. * Due to the only child device of hci_conn dev is rfcomm
  260. * tty_dev, here just return 1
  261. */
  262. return 1;
  263. }
  264. static void del_conn(struct work_struct *work)
  265. {
  266. struct device *dev;
  267. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  268. while (dev = device_find_child(&conn->dev, NULL, __match_tty)) {
  269. device_move(dev, NULL);
  270. put_device(dev);
  271. }
  272. device_del(&conn->dev);
  273. put_device(&conn->dev);
  274. }
  275. void hci_conn_del_sysfs(struct hci_conn *conn)
  276. {
  277. BT_DBG("conn %p", conn);
  278. if (!device_is_registered(&conn->dev))
  279. return;
  280. INIT_WORK(&conn->work, del_conn);
  281. schedule_work(&conn->work);
  282. }
  283. int hci_register_sysfs(struct hci_dev *hdev)
  284. {
  285. struct device *dev = &hdev->dev;
  286. unsigned int i;
  287. int err;
  288. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  289. dev->bus = &bt_bus;
  290. dev->parent = hdev->parent;
  291. strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
  292. dev->release = bt_release;
  293. dev_set_drvdata(dev, hdev);
  294. err = device_register(dev);
  295. if (err < 0)
  296. return err;
  297. for (i = 0; bt_attrs[i]; i++)
  298. if (device_create_file(dev, bt_attrs[i]) < 0)
  299. BT_ERR("Failed to create device attribute");
  300. if (sysfs_create_link(&bt_class->subsys.kobj,
  301. &dev->kobj, kobject_name(&dev->kobj)) < 0)
  302. BT_ERR("Failed to create class symlink");
  303. return 0;
  304. }
  305. void hci_unregister_sysfs(struct hci_dev *hdev)
  306. {
  307. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  308. sysfs_remove_link(&bt_class->subsys.kobj,
  309. kobject_name(&hdev->dev.kobj));
  310. device_del(&hdev->dev);
  311. }
  312. int __init bt_sysfs_init(void)
  313. {
  314. int err;
  315. bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
  316. if (IS_ERR(bt_platform))
  317. return PTR_ERR(bt_platform);
  318. err = bus_register(&bt_bus);
  319. if (err < 0) {
  320. platform_device_unregister(bt_platform);
  321. return err;
  322. }
  323. bt_class = class_create(THIS_MODULE, "bluetooth");
  324. if (IS_ERR(bt_class)) {
  325. bus_unregister(&bt_bus);
  326. platform_device_unregister(bt_platform);
  327. return PTR_ERR(bt_class);
  328. }
  329. return 0;
  330. }
  331. void bt_sysfs_cleanup(void)
  332. {
  333. class_destroy(bt_class);
  334. bus_unregister(&bt_bus);
  335. platform_device_unregister(bt_platform);
  336. }