hci_sysfs.c 11 KB

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