hci_sysfs.c 11 KB

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