hci_sysfs.c 11 KB

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