hci_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 *bluetooth;
  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 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. /* ensure previous add/del is complete */
  75. flush_workqueue(bluetooth);
  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, add_conn);
  92. queue_work(bluetooth, &conn->work_add);
  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_del);
  106. struct hci_dev *hdev = conn->hdev;
  107. /* ensure previous add/del is complete */
  108. flush_workqueue(bluetooth);
  109. while (1) {
  110. struct device *dev;
  111. dev = device_find_child(&conn->dev, NULL, __match_tty);
  112. if (!dev)
  113. break;
  114. device_move(dev, NULL, DPM_ORDER_DEV_LAST);
  115. put_device(dev);
  116. }
  117. device_del(&conn->dev);
  118. put_device(&conn->dev);
  119. hci_dev_put(hdev);
  120. }
  121. void hci_conn_del_sysfs(struct hci_conn *conn)
  122. {
  123. BT_DBG("conn %p", conn);
  124. if (!device_is_registered(&conn->dev))
  125. return;
  126. INIT_WORK(&conn->work_del, del_conn);
  127. queue_work(bluetooth, &conn->work_del);
  128. }
  129. static inline char *host_typetostr(int type)
  130. {
  131. switch (type) {
  132. case HCI_VIRTUAL:
  133. return "VIRTUAL";
  134. case HCI_USB:
  135. return "USB";
  136. case HCI_PCCARD:
  137. return "PCCARD";
  138. case HCI_UART:
  139. return "UART";
  140. case HCI_RS232:
  141. return "RS232";
  142. case HCI_PCI:
  143. return "PCI";
  144. case HCI_SDIO:
  145. return "SDIO";
  146. default:
  147. return "UNKNOWN";
  148. }
  149. }
  150. static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
  151. {
  152. struct hci_dev *hdev = dev_get_drvdata(dev);
  153. return sprintf(buf, "%s\n", host_typetostr(hdev->type));
  154. }
  155. static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
  156. {
  157. struct hci_dev *hdev = dev_get_drvdata(dev);
  158. char name[249];
  159. int i;
  160. for (i = 0; i < 248; i++)
  161. name[i] = hdev->dev_name[i];
  162. name[248] = '\0';
  163. return sprintf(buf, "%s\n", name);
  164. }
  165. static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
  166. {
  167. struct hci_dev *hdev = dev_get_drvdata(dev);
  168. return sprintf(buf, "0x%.2x%.2x%.2x\n",
  169. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  170. }
  171. static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
  172. {
  173. struct hci_dev *hdev = dev_get_drvdata(dev);
  174. bdaddr_t bdaddr;
  175. baswap(&bdaddr, &hdev->bdaddr);
  176. return sprintf(buf, "%s\n", batostr(&bdaddr));
  177. }
  178. static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
  179. {
  180. struct hci_dev *hdev = dev_get_drvdata(dev);
  181. return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  182. hdev->features[0], hdev->features[1],
  183. hdev->features[2], hdev->features[3],
  184. hdev->features[4], hdev->features[5],
  185. hdev->features[6], hdev->features[7]);
  186. }
  187. static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. struct hci_dev *hdev = dev_get_drvdata(dev);
  190. return sprintf(buf, "%d\n", hdev->manufacturer);
  191. }
  192. static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
  193. {
  194. struct hci_dev *hdev = dev_get_drvdata(dev);
  195. return sprintf(buf, "%d\n", hdev->hci_ver);
  196. }
  197. static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
  198. {
  199. struct hci_dev *hdev = dev_get_drvdata(dev);
  200. return sprintf(buf, "%d\n", hdev->hci_rev);
  201. }
  202. static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
  203. {
  204. struct hci_dev *hdev = dev_get_drvdata(dev);
  205. struct inquiry_cache *cache = &hdev->inq_cache;
  206. struct inquiry_entry *e;
  207. int n = 0;
  208. hci_dev_lock_bh(hdev);
  209. for (e = cache->list; e; e = e->next) {
  210. struct inquiry_data *data = &e->data;
  211. bdaddr_t bdaddr;
  212. baswap(&bdaddr, &data->bdaddr);
  213. n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
  214. batostr(&bdaddr),
  215. data->pscan_rep_mode, data->pscan_period_mode,
  216. data->pscan_mode, data->dev_class[2],
  217. data->dev_class[1], data->dev_class[0],
  218. __le16_to_cpu(data->clock_offset),
  219. data->rssi, data->ssp_mode, e->timestamp);
  220. }
  221. hci_dev_unlock_bh(hdev);
  222. return n;
  223. }
  224. static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
  225. {
  226. struct hci_dev *hdev = dev_get_drvdata(dev);
  227. return sprintf(buf, "%d\n", hdev->idle_timeout);
  228. }
  229. static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  230. {
  231. struct hci_dev *hdev = dev_get_drvdata(dev);
  232. char *ptr;
  233. __u32 val;
  234. val = simple_strtoul(buf, &ptr, 10);
  235. if (ptr == buf)
  236. return -EINVAL;
  237. if (val != 0 && (val < 500 || val > 3600000))
  238. return -EINVAL;
  239. hdev->idle_timeout = val;
  240. return count;
  241. }
  242. static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
  243. {
  244. struct hci_dev *hdev = dev_get_drvdata(dev);
  245. return sprintf(buf, "%d\n", hdev->sniff_max_interval);
  246. }
  247. static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  248. {
  249. struct hci_dev *hdev = dev_get_drvdata(dev);
  250. char *ptr;
  251. __u16 val;
  252. val = simple_strtoul(buf, &ptr, 10);
  253. if (ptr == buf)
  254. return -EINVAL;
  255. if (val < 0x0002 || val > 0xFFFE || val % 2)
  256. return -EINVAL;
  257. if (val < hdev->sniff_min_interval)
  258. return -EINVAL;
  259. hdev->sniff_max_interval = val;
  260. return count;
  261. }
  262. static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
  263. {
  264. struct hci_dev *hdev = dev_get_drvdata(dev);
  265. return sprintf(buf, "%d\n", hdev->sniff_min_interval);
  266. }
  267. static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  268. {
  269. struct hci_dev *hdev = dev_get_drvdata(dev);
  270. char *ptr;
  271. __u16 val;
  272. val = simple_strtoul(buf, &ptr, 10);
  273. if (ptr == buf)
  274. return -EINVAL;
  275. if (val < 0x0002 || val > 0xFFFE || val % 2)
  276. return -EINVAL;
  277. if (val > hdev->sniff_max_interval)
  278. return -EINVAL;
  279. hdev->sniff_min_interval = val;
  280. return count;
  281. }
  282. static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
  283. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  284. static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
  285. static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
  286. static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
  287. static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
  288. static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
  289. static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
  290. static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
  291. static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
  292. show_idle_timeout, store_idle_timeout);
  293. static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
  294. show_sniff_max_interval, store_sniff_max_interval);
  295. static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
  296. show_sniff_min_interval, store_sniff_min_interval);
  297. static struct attribute *bt_host_attrs[] = {
  298. &dev_attr_type.attr,
  299. &dev_attr_name.attr,
  300. &dev_attr_class.attr,
  301. &dev_attr_address.attr,
  302. &dev_attr_features.attr,
  303. &dev_attr_manufacturer.attr,
  304. &dev_attr_hci_version.attr,
  305. &dev_attr_hci_revision.attr,
  306. &dev_attr_inquiry_cache.attr,
  307. &dev_attr_idle_timeout.attr,
  308. &dev_attr_sniff_max_interval.attr,
  309. &dev_attr_sniff_min_interval.attr,
  310. NULL
  311. };
  312. static struct attribute_group bt_host_group = {
  313. .attrs = bt_host_attrs,
  314. };
  315. static struct attribute_group *bt_host_groups[] = {
  316. &bt_host_group,
  317. NULL
  318. };
  319. static void bt_host_release(struct device *dev)
  320. {
  321. void *data = dev_get_drvdata(dev);
  322. kfree(data);
  323. }
  324. static struct device_type bt_host = {
  325. .name = "host",
  326. .groups = bt_host_groups,
  327. .release = bt_host_release,
  328. };
  329. int hci_register_sysfs(struct hci_dev *hdev)
  330. {
  331. struct device *dev = &hdev->dev;
  332. int err;
  333. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  334. dev->type = &bt_host;
  335. dev->class = bt_class;
  336. dev->parent = hdev->parent;
  337. dev_set_name(dev, "%s", hdev->name);
  338. dev_set_drvdata(dev, hdev);
  339. err = device_register(dev);
  340. if (err < 0)
  341. return err;
  342. return 0;
  343. }
  344. void hci_unregister_sysfs(struct hci_dev *hdev)
  345. {
  346. BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
  347. device_del(&hdev->dev);
  348. }
  349. int __init bt_sysfs_init(void)
  350. {
  351. bluetooth = create_singlethread_workqueue("bluetooth");
  352. if (!bluetooth)
  353. return -ENOMEM;
  354. bt_class = class_create(THIS_MODULE, "bluetooth");
  355. if (IS_ERR(bt_class)) {
  356. destroy_workqueue(bluetooth);
  357. return PTR_ERR(bt_class);
  358. }
  359. return 0;
  360. }
  361. void bt_sysfs_cleanup(void)
  362. {
  363. destroy_workqueue(bluetooth);
  364. class_destroy(bt_class);
  365. }