hci_sysfs.c 11 KB

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