windows_driver_libusb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include "windows_driver_libusb.h"
  4. #include "drivers/hci_driver.h"
  5. #include "lib/lusb0_usb.h"
  6. #include "logging/bt_log_impl.h"
  7. #include "common/bt_buf.h"
  8. #include "host/hci_core.h"
  9. // Device configuration and interface id.
  10. #define MY_CONFIG 1
  11. #define MY_INTF 0
  12. #define END_POINT_SCO_R (131)
  13. #define END_POINT_SCO_W (3)
  14. #define END_POINT_ACL_R (130)
  15. #define END_POINT_ACL_W (2)
  16. #define END_POINT_CMD_CTRL (0)
  17. #define END_POINT_EVT_INTR (129) //(129)
  18. static volatile bool is_enable;
  19. static volatile bool is_in_reset;
  20. static volatile bool is_ready_work; // avoid last work have pendding packet.
  21. static struct k_fifo tx_queue;
  22. static pthread_mutex_t tx_lock;
  23. static struct k_fifo rx_queue;
  24. static pthread_mutex_t rx_lock;
  25. uint16_t selected_usb_vid = 0;
  26. uint16_t selected_usb_pid = 0;
  27. usb_dev_handle *usb_dev;
  28. usb_dev_handle *open_dev(uint16_t vid, uint16_t pid)
  29. {
  30. struct usb_bus *bus;
  31. struct usb_device *dev;
  32. for (bus = usb_get_busses(); bus; bus = bus->next)
  33. {
  34. for (dev = bus->devices; dev; dev = dev->next)
  35. {
  36. if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid)
  37. {
  38. selected_usb_vid = vid;
  39. selected_usb_pid = pid;
  40. return usb_open(dev);
  41. }
  42. }
  43. }
  44. printk("Warnning: vip/pid not match, please make sure usb device connect.\n");
  45. return NULL;
  46. }
  47. static void display_devices(void)
  48. {
  49. struct usb_bus *bus;
  50. struct usb_device *dev;
  51. for (bus = usb_get_busses(); bus; bus = bus->next)
  52. {
  53. for (dev = bus->devices; dev; dev = dev->next)
  54. {
  55. printk("display_devices(), idVendor: 0x%x, idProduct: 0x%x\n", dev->descriptor.idVendor,
  56. dev->descriptor.idProduct);
  57. }
  58. }
  59. }
  60. static struct net_buf *pop_tx_queue(void)
  61. {
  62. pthread_mutex_lock(&tx_lock);
  63. struct net_buf *buf = net_buf_get(&tx_queue, Z_FOREVER);
  64. pthread_mutex_unlock(&tx_lock);
  65. return buf;
  66. }
  67. static void push_tx_queue(struct net_buf *buf)
  68. {
  69. pthread_mutex_lock(&tx_lock);
  70. net_buf_put(&tx_queue, buf);
  71. pthread_mutex_unlock(&tx_lock);
  72. }
  73. static struct net_buf *pop_rx_queue(void)
  74. {
  75. pthread_mutex_lock(&rx_lock);
  76. struct net_buf *buf = net_buf_get(&rx_queue, Z_FOREVER);
  77. pthread_mutex_unlock(&rx_lock);
  78. return buf;
  79. }
  80. static void push_rx_queue(struct net_buf *buf)
  81. {
  82. pthread_mutex_lock(&rx_lock);
  83. net_buf_put(&rx_queue, buf);
  84. pthread_mutex_unlock(&rx_lock);
  85. }
  86. pthread_t usb_tx_thread;
  87. static int tx_process_loop(void *args)
  88. {
  89. printk("tx_process_loop\n");
  90. struct net_buf *buf;
  91. int ret = 0;
  92. while (1)
  93. {
  94. if (!is_enable)
  95. {
  96. break;
  97. }
  98. if (!is_ready_work)
  99. {
  100. continue;
  101. }
  102. if (!k_fifo_is_empty(&tx_queue))
  103. {
  104. buf = pop_tx_queue();
  105. byte type = bt_buf_get_type(buf);
  106. if (type == BT_BUF_CMD)
  107. {
  108. ret = usb_control_msg(usb_dev, 0x20, 0, /* set/get test */
  109. 0, /* test type */
  110. 0, /* interface id */
  111. (char *)buf->data, buf->len, 1000);
  112. }
  113. else if (type == BT_BUF_ACL_OUT)
  114. {
  115. ret = usb_interrupt_write(usb_dev, END_POINT_ACL_W, (char *)buf->data, buf->len,
  116. 1000);
  117. }
  118. if (ret < 0)
  119. {
  120. printk("error tx:\n%s\n", usb_strerror());
  121. }
  122. else
  123. {
  124. // printk("success: tx %d bytes\n", ret);
  125. }
  126. net_buf_unref(buf);
  127. }
  128. }
  129. return 0;
  130. }
  131. pthread_t usb_rx_evt_thread;
  132. static int rx_evt_process_loop(void *args)
  133. {
  134. printk("rx_evt_process_loop\n");
  135. uint8_t tmp[1024];
  136. int ret;
  137. while (1)
  138. {
  139. if (!is_enable)
  140. {
  141. break;
  142. }
  143. int reserve_size = bt_buf_reserve_size_controller_tx_evt();
  144. if (reserve_size == 0
  145. #if defined(CONFIG_BT_MONITOR_SLEEP)
  146. && !bt_check_is_in_sleep()
  147. #endif
  148. )
  149. {
  150. printk("rx_evt_process_loop(), reserve buff not enough.\n");
  151. Sleep(10);
  152. continue;
  153. }
  154. ret = usb_interrupt_read(usb_dev, END_POINT_EVT_INTR, (char *)tmp, sizeof(tmp), 1000);
  155. if (ret < 0)
  156. {
  157. is_ready_work = true;
  158. // printk("read data failed, %s", usb_strerror());
  159. if (is_in_reset)
  160. {
  161. printk("error reading:\n%s\n", usb_strerror());
  162. Sleep(100);
  163. }
  164. }
  165. else
  166. {
  167. if (!is_ready_work)
  168. {
  169. printk("warning, rx last packet.\n");
  170. continue;
  171. }
  172. // printk("success: bulk read %d bytes\n", ret);
  173. // printk("data: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", tmp[0], tmp[1], tmp[2],
  174. // tmp[3], tmp[4], tmp[5]);
  175. #if defined(CONFIG_BT_MONITOR_SLEEP)
  176. bt_sleep_wakeup_work_start();
  177. #endif
  178. if (bt_check_rx_evt_need_drop(tmp))
  179. {
  180. printk("rx_evt_process_loop(), no reserve buff, drop adv.\n");
  181. }
  182. else
  183. {
  184. struct net_buf *buf;
  185. buf = bt_buf_get_controller_tx_evt();
  186. if (buf)
  187. {
  188. net_buf_add_mem(buf, tmp, ret);
  189. push_rx_queue(buf);
  190. }
  191. else
  192. {
  193. while (1)
  194. {
  195. printk("rx_evt_process_loop(), no reserve buff\n");
  196. Sleep(1000);
  197. }
  198. }
  199. }
  200. #if defined(CONFIG_BT_MONITOR_SLEEP)
  201. bt_sleep_wakeup_work_end();
  202. #endif
  203. }
  204. }
  205. printk("rx_evt_process_loop end\n");
  206. return 0;
  207. }
  208. #if defined(CONFIG_BT_CONN)
  209. pthread_t usb_rx_acl_thread;
  210. static int rx_acl_process_loop(void *args)
  211. {
  212. printk("rx_acl_process_loop\n");
  213. uint8_t tmp[1024];
  214. int ret;
  215. while (1)
  216. {
  217. if (!is_enable)
  218. {
  219. break;
  220. }
  221. int reserve_size = bt_buf_reserve_size_controller_tx_acl();
  222. if (reserve_size == 0
  223. #if defined(CONFIG_BT_MONITOR_SLEEP)
  224. && !bt_check_is_in_sleep()
  225. #endif
  226. )
  227. {
  228. printk("rx_acl_process_loop(), reserve buff not enough.\n");
  229. Sleep(10);
  230. continue;
  231. }
  232. ret = usb_interrupt_read(usb_dev, END_POINT_ACL_R, (char *)tmp, sizeof(tmp), 1000);
  233. if (ret < 0)
  234. {
  235. if (is_in_reset)
  236. {
  237. printk("acl error reading:\n%s\n", usb_strerror());
  238. Sleep(100);
  239. }
  240. }
  241. else
  242. {
  243. #if defined(CONFIG_BT_MONITOR_SLEEP)
  244. bt_sleep_wakeup_work_start();
  245. #endif
  246. // printk("acl success: bulk read %d bytes\n", ret);
  247. // printk("acl data: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", tmp[0], tmp[1],
  248. // tmp[2], tmp[3], tmp[4], tmp[5]);
  249. struct net_buf *buf;
  250. buf = bt_buf_get_controller_tx_acl();
  251. if (buf)
  252. {
  253. net_buf_add_mem(buf, tmp, ret);
  254. push_rx_queue(buf);
  255. }
  256. else
  257. {
  258. while (1)
  259. {
  260. printk("rx_acl_process_loop(), no reserve buff\n");
  261. Sleep(1000);
  262. }
  263. }
  264. #if defined(CONFIG_BT_MONITOR_SLEEP)
  265. bt_sleep_wakeup_work_end();
  266. #endif
  267. }
  268. }
  269. printk("rx_acl_process_loop end\n");
  270. return 0;
  271. }
  272. #endif
  273. static int hci_driver_open(void)
  274. {
  275. printk("hci_driver_open()\n");
  276. return 0;
  277. }
  278. static int hci_driver_send(struct net_buf *buf)
  279. {
  280. push_tx_queue(buf);
  281. return 0;
  282. }
  283. static const struct bt_hci_driver drv = {
  284. .open = hci_driver_open,
  285. .send = hci_driver_send,
  286. };
  287. static void hci_driver_init(void)
  288. {
  289. bt_hci_driver_register(&drv);
  290. }
  291. int usb_open_process(uint16_t vid, uint16_t pid)
  292. {
  293. usb_init(); /* initialize the library */
  294. usb_find_busses(); /* find all busses */
  295. usb_find_devices(); /* find all connected devices */
  296. display_devices();
  297. usb_dev = open_dev(vid, pid);
  298. if (usb_dev == NULL)
  299. {
  300. return -1;
  301. }
  302. if (usb_set_configuration(usb_dev, MY_CONFIG) < 0)
  303. {
  304. printk("Error, setting config #%d: %s\n", MY_CONFIG, usb_strerror());
  305. usb_close(usb_dev);
  306. return -1;
  307. }
  308. else
  309. {
  310. printk("success: set configuration #%d\n", MY_CONFIG);
  311. }
  312. if (usb_claim_interface(usb_dev, 0) < 0)
  313. {
  314. printk("Error, claiming interface #%d:\n%s\n", MY_INTF, usb_strerror());
  315. usb_close(usb_dev);
  316. return -1;
  317. }
  318. else
  319. {
  320. printk("success: claim_interface #%d\n", MY_INTF);
  321. }
  322. is_enable = true;
  323. is_ready_work = false;
  324. is_in_reset = false;
  325. pthread_mutex_init(&tx_lock, NULL);
  326. pthread_mutex_init(&rx_lock, NULL);
  327. pthread_create(&usb_tx_thread, NULL, (void *)tx_process_loop, NULL);
  328. // pthread_join(usb_tx_thread, NULL);
  329. pthread_create(&usb_rx_evt_thread, NULL, (void *)rx_evt_process_loop, NULL);
  330. // pthread_join(usb_rx_evt_thread, NULL);
  331. #if defined(CONFIG_BT_CONN)
  332. pthread_create(&usb_rx_acl_thread, NULL, (void *)rx_acl_process_loop, NULL);
  333. // pthread_join(usb_rx_acl_thread, NULL);
  334. #endif
  335. k_fifo_init(&tx_queue);
  336. k_fifo_init(&rx_queue);
  337. return 0;
  338. }
  339. int bt_hci_init_usb_device(uint16_t vid, uint16_t pid)
  340. {
  341. int ret = usb_open_process(vid, pid);
  342. if (ret < 0)
  343. {
  344. return ret;
  345. }
  346. hci_driver_init();
  347. return (0);
  348. }
  349. bt_hci_driver_reset_callback_t local_callback;
  350. static int reset_driver_process(void *args)
  351. {
  352. printk("reset_driver_process, wait usb reboot.\n");
  353. Sleep(5000);
  354. printk("reset_driver_process, usb reboot ready.\n");
  355. is_enable = false;
  356. is_in_reset = true;
  357. // wait thread close.
  358. pthread_join(usb_tx_thread, NULL);
  359. pthread_join(usb_rx_evt_thread, NULL);
  360. #if defined(CONFIG_BT_CONN)
  361. pthread_join(usb_rx_acl_thread, NULL);
  362. #endif
  363. int ret = usb_open_process(selected_usb_vid, selected_usb_pid);
  364. if (ret < 0)
  365. {
  366. return ret;
  367. }
  368. local_callback();
  369. return 0;
  370. }
  371. void reset_usb_driver(bt_hci_driver_reset_callback_t callback)
  372. {
  373. local_callback = callback;
  374. pthread_t reset_thread;
  375. pthread_create(&reset_thread, NULL, (void *)reset_driver_process, NULL);
  376. }
  377. void libusb_main_loop(void)
  378. {
  379. if (k_fifo_is_empty(&rx_queue))
  380. {
  381. return;
  382. }
  383. struct net_buf *buf = pop_rx_queue();
  384. if (buf)
  385. {
  386. bt_recv(buf);
  387. }
  388. }