usbh_bluetooth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_bluetooth.h"
  8. #undef USB_DBG_TAG
  9. #define USB_DBG_TAG "usbh_bluetooth"
  10. #include "usb_log.h"
  11. #define DEV_FORMAT "/dev/bluetooth"
  12. static struct usbh_bluetooth g_bluetooth_class;
  13. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  14. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  15. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  16. #else
  17. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
  18. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_evt_buf[USB_ALIGN_UP(256, CONFIG_USB_ALIGN_SIZE)];
  19. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_TX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  20. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[USB_ALIGN_UP(CONFIG_USBHOST_BLUETOOTH_RX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  21. #endif
  22. static int usbh_bluetooth_connect(struct usbh_hubport *hport, uint8_t intf)
  23. {
  24. struct usb_endpoint_descriptor *ep_desc;
  25. int ret = 0;
  26. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  27. uint8_t mult;
  28. uint16_t mps;
  29. #endif
  30. struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
  31. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  32. if (intf != 0) {
  33. return 0;
  34. }
  35. #endif
  36. memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
  37. bluetooth_class->hport = hport;
  38. bluetooth_class->intf = intf;
  39. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  40. bluetooth_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
  41. #endif
  42. hport->config.intf[intf].priv = bluetooth_class;
  43. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  44. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  45. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  46. if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
  47. if (ep_desc->bEndpointAddress & 0x80) {
  48. USBH_EP_INIT(bluetooth_class->intin, ep_desc);
  49. } else {
  50. return -USB_ERR_NOTSUPP;
  51. }
  52. } else {
  53. #endif
  54. if (ep_desc->bEndpointAddress & 0x80) {
  55. USBH_EP_INIT(bluetooth_class->bulkin, ep_desc);
  56. } else {
  57. USBH_EP_INIT(bluetooth_class->bulkout, ep_desc);
  58. }
  59. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  60. }
  61. #endif
  62. }
  63. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  64. USB_LOG_INFO("Num of altsettings:%u\r\n", bluetooth_class->num_of_intf_altsettings);
  65. for (uint8_t i = 0; i < bluetooth_class->num_of_intf_altsettings; i++) {
  66. USB_LOG_INFO("Altsetting:%u\r\n", i);
  67. for (uint8_t j = 0; j < hport->config.intf[intf + 1].altsetting[i].intf_desc.bNumEndpoints; j++) {
  68. ep_desc = &bluetooth_class->hport->config.intf[intf + 1].altsetting[i].ep[j].ep_desc;
  69. mult = USB_GET_MULT(ep_desc->wMaxPacketSize);
  70. mps = USB_GET_MAXPACKETSIZE(ep_desc->wMaxPacketSize);
  71. USB_LOG_INFO("\tEp=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n",
  72. ep_desc->bEndpointAddress,
  73. ep_desc->bmAttributes,
  74. mps,
  75. ep_desc->bInterval,
  76. mult);
  77. }
  78. }
  79. ret = usbh_set_interface(hport, intf, 0);
  80. if (ret < 0) {
  81. return ret;
  82. }
  83. USB_LOG_INFO("Bluetooth select altsetting 0\r\n");
  84. #endif
  85. strncpy(hport->config.intf[intf].devname, DEV_FORMAT, CONFIG_USBHOST_DEV_NAMELEN);
  86. USB_LOG_INFO("Register Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
  87. usbh_bluetooth_run(bluetooth_class);
  88. return ret;
  89. }
  90. static int usbh_bluetooth_disconnect(struct usbh_hubport *hport, uint8_t intf)
  91. {
  92. int ret = 0;
  93. struct usbh_bluetooth *bluetooth_class = (struct usbh_bluetooth *)hport->config.intf[intf].priv;
  94. if (hport->config.config_desc.bNumInterfaces == (intf + 1)) {
  95. return 0;
  96. }
  97. if (bluetooth_class) {
  98. if (bluetooth_class->bulkin) {
  99. usbh_kill_urb(&bluetooth_class->bulkin_urb);
  100. }
  101. if (bluetooth_class->bulkout) {
  102. usbh_kill_urb(&bluetooth_class->bulkout_urb);
  103. }
  104. #ifndef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  105. if (bluetooth_class->intin) {
  106. usbh_kill_urb(&bluetooth_class->intin_urb);
  107. }
  108. // if (bluetooth_class->isoin) {
  109. // usbh_kill_urb(&bluetooth_class->isoin_urb);
  110. // }
  111. // if (bluetooth_class->isoin) {
  112. // usbh_kill_urb(&bluetooth_class->isoinin_urb);
  113. // }
  114. #endif
  115. if (hport->config.intf[intf].devname[0] != '\0') {
  116. usb_osal_thread_schedule_other();
  117. USB_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
  118. usbh_bluetooth_stop(bluetooth_class);
  119. }
  120. memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
  121. }
  122. return ret;
  123. }
  124. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_LOG
  125. static void usbh_bluetooth_hci_dump(uint8_t *data, uint32_t len)
  126. {
  127. uint32_t i = 0;
  128. for (i = 0; i < len; i++) {
  129. if (i % 16 == 0) {
  130. USB_LOG_RAW("\r\n");
  131. }
  132. USB_LOG_RAW("%02x ", data[i]);
  133. }
  134. USB_LOG_RAW("\r\n");
  135. }
  136. #else
  137. #define usbh_bluetooth_hci_dump(data, len)
  138. #endif
  139. static int usbh_bluetooth_hci_bulk_out(uint8_t *buffer, uint32_t buflen)
  140. {
  141. struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
  142. struct usbh_urb *urb = &bluetooth_class->bulkout_urb;
  143. int ret;
  144. usbh_bulk_urb_fill(urb, bluetooth_class->hport, bluetooth_class->bulkout, buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  145. ret = usbh_submit_urb(urb);
  146. if (ret == 0) {
  147. ret = urb->actual_length;
  148. }
  149. return ret;
  150. }
  151. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  152. int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
  153. {
  154. int ret;
  155. g_bluetooth_tx_buf[0] = hci_type;
  156. memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
  157. usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
  158. ret = usbh_bluetooth_hci_bulk_out(g_bluetooth_tx_buf, buflen + 1);
  159. return ret;
  160. }
  161. void usbh_bluetooth_hci_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  162. {
  163. int ret;
  164. uint32_t ep_mps;
  165. uint8_t retry = 0;
  166. uint16_t actual_len = 0;
  167. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
  168. USB_LOG_INFO("Create hc rx thread\r\n");
  169. while (1) {
  170. usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  171. ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
  172. if (ret < 0) {
  173. if (ret == -USB_ERR_SHUTDOWN) {
  174. goto delete;
  175. } else {
  176. retry++;
  177. if (retry == 3) {
  178. retry = 0;
  179. goto delete;
  180. }
  181. continue;
  182. }
  183. }
  184. actual_len += g_bluetooth_class.bulkin_urb.actual_length;
  185. if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
  186. usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len);
  187. usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len);
  188. actual_len = 0;
  189. } else {
  190. /* read continue util read short packet */
  191. }
  192. }
  193. // clang-format off
  194. delete :
  195. USB_LOG_INFO("Delete hc acl rx thread\r\n");
  196. usb_osal_thread_delete(NULL);
  197. // clang-format on
  198. }
  199. #else
  200. static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
  201. {
  202. struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
  203. struct usb_setup_packet *setup;
  204. if (!bluetooth_class || !bluetooth_class->hport) {
  205. return -USB_ERR_INVAL;
  206. }
  207. setup = bluetooth_class->hport->setup;
  208. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
  209. setup->bRequest = 0x00;
  210. setup->wValue = 0;
  211. setup->wIndex = bluetooth_class->intf;
  212. setup->wLength = buflen;
  213. return usbh_control_transfer(bluetooth_class->hport, setup, buffer);
  214. }
  215. int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
  216. {
  217. int ret;
  218. if (hci_type == USB_BLUETOOTH_HCI_CMD) {
  219. g_bluetooth_cmd_buf[0] = USB_BLUETOOTH_HCI_CMD;
  220. memcpy(&g_bluetooth_cmd_buf[1], buffer, buflen);
  221. usbh_bluetooth_hci_dump(g_bluetooth_cmd_buf, buflen + 1);
  222. ret = usbh_bluetooth_hci_cmd(&g_bluetooth_cmd_buf[1], buflen);
  223. } else if (hci_type == USB_BLUETOOTH_HCI_ACL) {
  224. g_bluetooth_tx_buf[0] = USB_BLUETOOTH_HCI_ACL;
  225. memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
  226. usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
  227. ret = usbh_bluetooth_hci_bulk_out(&g_bluetooth_tx_buf[1], buflen);
  228. } else {
  229. ret = -1;
  230. }
  231. return ret;
  232. }
  233. void usbh_bluetooth_hci_evt_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  234. {
  235. int ret;
  236. uint32_t ep_mps;
  237. uint32_t interval;
  238. uint8_t retry = 0;
  239. uint16_t actual_len = 0;
  240. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.intin->wMaxPacketSize);
  241. interval = g_bluetooth_class.intin->bInterval;
  242. USB_LOG_INFO("Create hc event rx thread\r\n");
  243. while (1) {
  244. usbh_int_urb_fill(&g_bluetooth_class.intin_urb, g_bluetooth_class.hport, g_bluetooth_class.intin, &g_bluetooth_evt_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  245. ret = usbh_submit_urb(&g_bluetooth_class.intin_urb);
  246. if (ret < 0) {
  247. if (ret == -USB_ERR_SHUTDOWN) {
  248. goto delete;
  249. } else if (ret == -USB_ERR_NAK) {
  250. usb_osal_msleep(interval);
  251. continue;
  252. } else {
  253. retry++;
  254. if (retry == 3) {
  255. retry = 0;
  256. goto delete;
  257. }
  258. usb_osal_msleep(interval);
  259. continue;
  260. }
  261. }
  262. actual_len += g_bluetooth_class.intin_urb.actual_length;
  263. if (g_bluetooth_class.intin_urb.actual_length != ep_mps) {
  264. g_bluetooth_evt_buf[0] = USB_BLUETOOTH_HCI_EVT;
  265. usbh_bluetooth_hci_dump(g_bluetooth_evt_buf, actual_len + 1);
  266. usbh_bluetooth_hci_read_callback(g_bluetooth_evt_buf, actual_len + 1);
  267. actual_len = 0;
  268. } else {
  269. /* read continue util read short packet */
  270. }
  271. usb_osal_msleep(interval);
  272. }
  273. // clang-format off
  274. delete :
  275. USB_LOG_INFO("Delete hc event rx thread\r\n");
  276. usb_osal_thread_delete(NULL);
  277. // clang-format on
  278. }
  279. void usbh_bluetooth_hci_acl_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  280. {
  281. int ret;
  282. uint32_t ep_mps;
  283. uint8_t retry = 0;
  284. uint16_t actual_len = 0;
  285. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
  286. USB_LOG_INFO("Create hc acl rx thread\r\n");
  287. while (1) {
  288. usbh_bulk_urb_fill(&g_bluetooth_class.bulkin_urb, g_bluetooth_class.hport, g_bluetooth_class.bulkin, &g_bluetooth_rx_buf[1 + actual_len], ep_mps, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  289. ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
  290. if (ret < 0) {
  291. if (ret == -USB_ERR_SHUTDOWN) {
  292. goto delete;
  293. } else {
  294. retry++;
  295. if (retry == 3) {
  296. retry = 0;
  297. goto delete;
  298. }
  299. continue;
  300. }
  301. }
  302. actual_len += g_bluetooth_class.bulkin_urb.actual_length;
  303. if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
  304. g_bluetooth_rx_buf[0] = USB_BLUETOOTH_HCI_ACL;
  305. usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len + 1);
  306. usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len + 1);
  307. actual_len = 0;
  308. } else {
  309. /* read continue util read short packet */
  310. }
  311. }
  312. // clang-format off
  313. delete :
  314. USB_LOG_INFO("Delete hc acl rx thread\r\n");
  315. usb_osal_thread_delete(NULL);
  316. // clang-format on
  317. }
  318. #endif
  319. __WEAK void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
  320. {
  321. (void)data;
  322. (void)len;
  323. }
  324. __WEAK void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
  325. {
  326. (void)bluetooth_class;
  327. }
  328. __WEAK void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
  329. {
  330. (void)bluetooth_class;
  331. }
  332. static const struct usbh_class_driver bluetooth_class_driver = {
  333. .driver_name = "bluetooth",
  334. .connect = usbh_bluetooth_connect,
  335. .disconnect = usbh_bluetooth_disconnect
  336. };
  337. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  338. static const uint16_t bluetooth_id_table[][2] = {
  339. { 0x2fe3, 0x000c },
  340. { 0, 0 },
  341. };
  342. CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_h4_nrf_class_info = {
  343. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  344. .bInterfaceClass = 0xff,
  345. .bInterfaceSubClass = 0x00,
  346. .bInterfaceProtocol = 0x00,
  347. .id_table = bluetooth_id_table,
  348. .class_driver = &bluetooth_class_driver
  349. };
  350. #else
  351. CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_class_info = {
  352. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  353. .bInterfaceClass = USB_DEVICE_CLASS_WIRELESS,
  354. .bInterfaceSubClass = 0x01,
  355. .bInterfaceProtocol = 0x01,
  356. .id_table = NULL,
  357. .class_driver = &bluetooth_class_driver
  358. };
  359. #endif