hci_sysfs.c 11 KB

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