hci_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 %d %u\n",
  98. batostr(&bdaddr),
  99. data->pscan_rep_mode, data->pscan_period_mode,
  100. data->pscan_mode, data->dev_class[2],
  101. data->dev_class[1], data->dev_class[0],
  102. __le16_to_cpu(data->clock_offset),
  103. data->rssi, data->ssp_mode, e->timestamp);
  104. }
  105. hci_dev_unlock_bh(hdev);
  106. return n;
  107. }
  108. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  109. {
  110. struct hci_dev *hdev = dev_get_drvdata(dev);
  111. return sprintf(buf, "%d\n", hdev->idle_timeout);
  112. }
  113. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  114. {
  115. struct hci_dev *hdev = dev_get_drvdata(dev);
  116. char *ptr;
  117. __u32 val;
  118. val = simple_strtoul(buf, &ptr, 10);
  119. if (ptr == buf)
  120. return -EINVAL;
  121. if (val != 0 && (val < 500 || val > 3600000))
  122. return -EINVAL;
  123. hdev->idle_timeout = val;
  124. return count;
  125. }
  126. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  127. {
  128. struct hci_dev *hdev = dev_get_drvdata(dev);
  129. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  130. }
  131. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. struct hci_dev *hdev = dev_get_drvdata(dev);
  134. char *ptr;
  135. __u16 val;
  136. val = simple_strtoul(buf, &ptr, 10);
  137. if (ptr == buf)
  138. return -EINVAL;
  139. if (val < 0x0002 || val > 0xFFFE || val % 2)
  140. return -EINVAL;
  141. if (val < hdev->sniff_min_interval)
  142. return -EINVAL;
  143. hdev->sniff_max_interval = val;
  144. return count;
  145. }
  146. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct hci_dev *hdev = dev_get_drvdata(dev);
  149. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  150. }
  151. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  152. {
  153. struct hci_dev *hdev = dev_get_drvdata(dev);
  154. char *ptr;
  155. __u16 val;
  156. val = simple_strtoul(buf, &ptr, 10);
  157. if (ptr == buf)
  158. return -EINVAL;
  159. if (val < 0x0002 || val > 0xFFFE || val % 2)
  160. return -EINVAL;
  161. if (val > hdev->sniff_max_interval)
  162. return -EINVAL;
  163. hdev->sniff_min_interval = val;
  164. return count;
  165. }
  166. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  167. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  168. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  169. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  170. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  171. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  172. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  173. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  174. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  175. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  176. show_idle_timeout, store_idle_timeout);
  177. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  178. show_sniff_max_interval, store_sniff_max_interval);
  179. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  180. show_sniff_min_interval, store_sniff_min_interval);
  181. static struct device_attribute *bt_attrs[] = {
  182. &dev_attr_type,
  183. &dev_attr_name,
  184. &dev_attr_class,
  185. &dev_attr_address,
  186. &dev_attr_features,
  187. &dev_attr_manufacturer,
  188. &dev_attr_hci_version,
  189. &dev_attr_hci_revision,
  190. &dev_attr_inquiry_cache,
  191. &dev_attr_idle_timeout,
  192. &dev_attr_sniff_max_interval,
  193. &dev_attr_sniff_min_interval,
  194. NULL
  195. };
  196. static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
  197. {
  198. struct hci_conn *conn = dev_get_drvdata(dev);
  199. return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
  200. }
  201. static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
  202. {
  203. struct hci_conn *conn = dev_get_drvdata(dev);
  204. bdaddr_t bdaddr;
  205. baswap(&bdaddr, &conn->dst);
  206. return sprintf(buf, "%s\n", batostr(&bdaddr));
  207. }
  208. static ssize_t show_conn_features(struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct hci_conn *conn = dev_get_drvdata(dev);
  211. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  212. conn->features[0], conn->features[1],
  213. conn->features[2], conn->features[3],
  214. conn->features[4], conn->features[5],
  215. conn->features[6], conn->features[7]);
  216. }
  217. #define CONN_ATTR(_name,_mode,_show,_store) \
  218. struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
  219. static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
  220. static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
  221. static CONN_ATTR(features, S_IRUGO, show_conn_features, NULL);
  222. static struct device_attribute *conn_attrs[] = {
  223. &conn_attr_type,
  224. &conn_attr_address,
  225. &conn_attr_features,
  226. NULL
  227. };
  228. struct class *bt_class = NULL;
  229. EXPORT_SYMBOL_GPL(bt_class);
  230. static struct bus_type bt_bus = {
  231. .name = "bluetooth",
  232. };
  233. static struct platform_device *bt_platform;
  234. static void bt_release(struct device *dev)
  235. {
  236. void *data = dev_get_drvdata(dev);
  237. kfree(data);
  238. }
  239. static void add_conn(struct work_struct *work)
  240. {
  241. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  242. int i;
  243. flush_workqueue(btdelconn);
  244. if (device_add(&conn->dev) < 0) {
  245. BT_ERR("Failed to register connection device");
  246. return;
  247. }
  248. for (i = 0; conn_attrs[i]; i++)
  249. if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
  250. BT_ERR("Failed to create connection attribute");
  251. }
  252. void hci_conn_add_sysfs(struct hci_conn *conn)
  253. {
  254. struct hci_dev *hdev = conn->hdev;
  255. BT_DBG("conn %p", conn);
  256. conn->dev.bus = &bt_bus;
  257. conn->dev.parent = &hdev->dev;
  258. conn->dev.release = bt_release;
  259. snprintf(conn->dev.bus_id, BUS_ID_SIZE, "%s:%d",
  260. hdev->name, conn->handle);
  261. dev_set_drvdata(&conn->dev, conn);
  262. device_initialize(&conn->dev);
  263. INIT_WORK(&conn->work, add_conn);
  264. queue_work(btaddconn, &conn->work);
  265. }
  266. /*
  267. * The rfcomm tty device will possibly retain even when conn
  268. * is down, and sysfs doesn't support move zombie device,
  269. * so we should move the device before conn device is destroyed.
  270. */
  271. static int __match_tty(struct device *dev, void *data)
  272. {
  273. return !strncmp(dev->bus_id, "rfcomm", 6);
  274. }
  275. static void del_conn(struct work_struct *work)
  276. {
  277. struct hci_conn *conn = container_of(work, struct hci_conn, work);
  278. struct hci_dev *hdev = conn->hdev;
  279. while (1) {
  280. struct device *dev;
  281. dev = device_find_child(&conn->dev, NULL, __match_tty);
  282. if (!dev)
  283. break;
  284. device_move(dev, NULL);
  285. put_device(dev);
  286. }
  287. device_del(&conn->dev);
  288. put_device(&conn->dev);
  289. hci_dev_put(hdev);
  290. }
  291. void hci_conn_del_sysfs(struct hci_conn *conn)
  292. {
  293. BT_DBG("conn %p", conn);
  294. if (!device_is_registered(&conn->dev))
  295. return;
  296. INIT_WORK(&conn->work, del_conn);
  297. queue_work(btdelconn, &conn->work);
  298. }
  299. int hci_register_sysfs(struct hci_dev *hdev)
  300. {
  301. struct device *dev = &hdev->dev;
  302. unsigned int i;
  303. int err;
  304. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  305. dev->bus = &bt_bus;
  306. dev->parent = hdev->parent;
  307. strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
  308. dev->release = bt_release;
  309. dev_set_drvdata(dev, hdev);
  310. err = device_register(dev);
  311. if (err < 0)
  312. return err;
  313. for (i = 0; bt_attrs[i]; i++)
  314. if (device_create_file(dev, bt_attrs[i]) < 0)
  315. BT_ERR("Failed to create device attribute");
  316. return 0;
  317. }
  318. void hci_unregister_sysfs(struct hci_dev *hdev)
  319. {
  320. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  321. device_del(&hdev->dev);
  322. }
  323. int __init bt_sysfs_init(void)
  324. {
  325. int err;
  326. btaddconn = create_singlethread_workqueue("btaddconn");
  327. if (!btaddconn) {
  328. err = -ENOMEM;
  329. goto out;
  330. }
  331. btdelconn = create_singlethread_workqueue("btdelconn");
  332. if (!btdelconn) {
  333. err = -ENOMEM;
  334. goto out_del;
  335. }
  336. bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
  337. if (IS_ERR(bt_platform)) {
  338. err = PTR_ERR(bt_platform);
  339. goto out_platform;
  340. }
  341. err = bus_register(&bt_bus);
  342. if (err < 0)
  343. goto out_bus;
  344. bt_class = class_create(THIS_MODULE, "bluetooth");
  345. if (IS_ERR(bt_class)) {
  346. err = PTR_ERR(bt_class);
  347. goto out_class;
  348. }
  349. return 0;
  350. out_class:
  351. bus_unregister(&bt_bus);
  352. out_bus:
  353. platform_device_unregister(bt_platform);
  354. out_platform:
  355. destroy_workqueue(btdelconn);
  356. out_del:
  357. destroy_workqueue(btaddconn);
  358. out:
  359. return err;
  360. }
  361. void bt_sysfs_cleanup(void)
  362. {
  363. destroy_workqueue(btaddconn);
  364. destroy_workqueue(btdelconn);
  365. class_destroy(bt_class);
  366. bus_unregister(&bt_bus);
  367. platform_device_unregister(bt_platform);
  368. }