usbh_bluetooth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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[1 + CONFIG_USBHOST_BLUETOOTH_TX_SIZE];
  15. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[1 + CONFIG_USBHOST_BLUETOOTH_RX_SIZE];
  16. #else
  17. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_cmd_buf[1 + 256];
  18. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_evt_buf[1 + 256];
  19. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_tx_buf[1 + CONFIG_USBHOST_BLUETOOTH_TX_SIZE];
  20. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_bluetooth_rx_buf[1 + CONFIG_USBHOST_BLUETOOTH_RX_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_LOG_INFO("Unregister Bluetooth Class:%s\r\n", hport->config.intf[intf].devname);
  117. usbh_bluetooth_stop(bluetooth_class);
  118. }
  119. memset(bluetooth_class, 0, sizeof(struct usbh_bluetooth));
  120. }
  121. return ret;
  122. }
  123. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_LOG
  124. static void usbh_bluetooth_hci_dump(uint8_t *data, uint32_t len)
  125. {
  126. uint32_t i = 0;
  127. for (i = 0; i < len; i++) {
  128. if (i % 16 == 0) {
  129. USB_LOG_RAW("\r\n");
  130. }
  131. USB_LOG_RAW("%02x ", data[i]);
  132. }
  133. USB_LOG_RAW("\r\n");
  134. }
  135. #else
  136. #define usbh_bluetooth_hci_dump(data, len)
  137. #endif
  138. static int usbh_bluetooth_hci_bulk_out(uint8_t *buffer, uint32_t buflen)
  139. {
  140. struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
  141. struct usbh_urb *urb = &bluetooth_class->bulkout_urb;
  142. int ret;
  143. usbh_bulk_urb_fill(urb, bluetooth_class->hport, bluetooth_class->bulkout, buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);
  144. ret = usbh_submit_urb(urb);
  145. if (ret == 0) {
  146. ret = urb->actual_length;
  147. }
  148. return ret;
  149. }
  150. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  151. int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
  152. {
  153. int ret;
  154. g_bluetooth_tx_buf[0] = hci_type;
  155. memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
  156. usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
  157. ret = usbh_bluetooth_hci_bulk_out(g_bluetooth_tx_buf, buflen + 1);
  158. return ret;
  159. }
  160. void usbh_bluetooth_hci_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  161. {
  162. int ret;
  163. uint32_t ep_mps;
  164. uint8_t retry = 0;
  165. uint16_t actual_len = 0;
  166. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
  167. USB_LOG_INFO("Create hc rx thread\r\n");
  168. while (1) {
  169. 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);
  170. ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
  171. if (ret < 0) {
  172. if (ret == -USB_ERR_SHUTDOWN) {
  173. goto delete;
  174. } else {
  175. retry++;
  176. if (retry == 3) {
  177. retry = 0;
  178. goto delete;
  179. }
  180. continue;
  181. }
  182. }
  183. actual_len += g_bluetooth_class.bulkin_urb.actual_length;
  184. if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
  185. usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len);
  186. usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len);
  187. actual_len = 0;
  188. } else {
  189. /* read continue util read short packet */
  190. }
  191. }
  192. // clang-format off
  193. delete :
  194. USB_LOG_INFO("Delete hc acl rx thread\r\n");
  195. usb_osal_thread_delete(NULL);
  196. // clang-format on
  197. }
  198. #else
  199. static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
  200. {
  201. struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
  202. struct usb_setup_packet *setup;
  203. if (!bluetooth_class || !bluetooth_class->hport) {
  204. return -USB_ERR_INVAL;
  205. }
  206. setup = bluetooth_class->hport->setup;
  207. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
  208. setup->bRequest = 0x00;
  209. setup->wValue = 0;
  210. setup->wIndex = bluetooth_class->intf;
  211. setup->wLength = buflen;
  212. return usbh_control_transfer(bluetooth_class->hport, setup, buffer);
  213. }
  214. int usbh_bluetooth_hci_write(uint8_t hci_type, uint8_t *buffer, uint32_t buflen)
  215. {
  216. int ret;
  217. if (hci_type == USB_BLUETOOTH_HCI_CMD) {
  218. g_bluetooth_cmd_buf[0] = USB_BLUETOOTH_HCI_CMD;
  219. memcpy(&g_bluetooth_cmd_buf[1], buffer, buflen);
  220. usbh_bluetooth_hci_dump(g_bluetooth_cmd_buf, buflen + 1);
  221. ret = usbh_bluetooth_hci_cmd(&g_bluetooth_cmd_buf[1], buflen);
  222. } else if (hci_type == USB_BLUETOOTH_HCI_ACL) {
  223. g_bluetooth_tx_buf[0] = USB_BLUETOOTH_HCI_ACL;
  224. memcpy(&g_bluetooth_tx_buf[1], buffer, buflen);
  225. usbh_bluetooth_hci_dump(g_bluetooth_tx_buf, buflen + 1);
  226. ret = usbh_bluetooth_hci_bulk_out(&g_bluetooth_tx_buf[1], buflen);
  227. } else {
  228. ret = -1;
  229. }
  230. return ret;
  231. }
  232. void usbh_bluetooth_hci_evt_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  233. {
  234. int ret;
  235. uint32_t ep_mps;
  236. uint32_t interval;
  237. uint8_t retry = 0;
  238. uint16_t actual_len = 0;
  239. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.intin->wMaxPacketSize);
  240. interval = g_bluetooth_class.intin->bInterval;
  241. USB_LOG_INFO("Create hc event rx thread\r\n");
  242. while (1) {
  243. 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);
  244. ret = usbh_submit_urb(&g_bluetooth_class.intin_urb);
  245. if (ret < 0) {
  246. if (ret == -USB_ERR_SHUTDOWN) {
  247. goto delete;
  248. } else if (ret == -USB_ERR_NAK) {
  249. usb_osal_msleep(interval);
  250. continue;
  251. } else {
  252. retry++;
  253. if (retry == 3) {
  254. retry = 0;
  255. goto delete;
  256. }
  257. usb_osal_msleep(interval);
  258. continue;
  259. }
  260. }
  261. actual_len += g_bluetooth_class.intin_urb.actual_length;
  262. if (g_bluetooth_class.intin_urb.actual_length != ep_mps) {
  263. g_bluetooth_evt_buf[0] = USB_BLUETOOTH_HCI_EVT;
  264. usbh_bluetooth_hci_dump(g_bluetooth_evt_buf, actual_len + 1);
  265. usbh_bluetooth_hci_read_callback(g_bluetooth_evt_buf, actual_len + 1);
  266. actual_len = 0;
  267. } else {
  268. /* read continue util read short packet */
  269. }
  270. usb_osal_msleep(interval);
  271. }
  272. // clang-format off
  273. delete :
  274. USB_LOG_INFO("Delete hc event rx thread\r\n");
  275. usb_osal_thread_delete(NULL);
  276. // clang-format on
  277. }
  278. void usbh_bluetooth_hci_acl_rx_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  279. {
  280. int ret;
  281. uint32_t ep_mps;
  282. uint8_t retry = 0;
  283. uint16_t actual_len = 0;
  284. ep_mps = USB_GET_MAXPACKETSIZE(g_bluetooth_class.bulkin->wMaxPacketSize);
  285. USB_LOG_INFO("Create hc acl rx thread\r\n");
  286. while (1) {
  287. 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);
  288. ret = usbh_submit_urb(&g_bluetooth_class.bulkin_urb);
  289. if (ret < 0) {
  290. if (ret == -USB_ERR_SHUTDOWN) {
  291. goto delete;
  292. } else {
  293. retry++;
  294. if (retry == 3) {
  295. retry = 0;
  296. goto delete;
  297. }
  298. continue;
  299. }
  300. }
  301. actual_len += g_bluetooth_class.bulkin_urb.actual_length;
  302. if (g_bluetooth_class.bulkin_urb.actual_length != ep_mps) {
  303. g_bluetooth_rx_buf[0] = USB_BLUETOOTH_HCI_ACL;
  304. usbh_bluetooth_hci_dump(g_bluetooth_rx_buf, actual_len + 1);
  305. usbh_bluetooth_hci_read_callback(g_bluetooth_rx_buf, actual_len + 1);
  306. actual_len = 0;
  307. } else {
  308. /* read continue util read short packet */
  309. }
  310. }
  311. // clang-format off
  312. delete :
  313. USB_LOG_INFO("Delete hc acl rx thread\r\n");
  314. usb_osal_thread_delete(NULL);
  315. // clang-format on
  316. }
  317. #endif
  318. __WEAK void usbh_bluetooth_hci_read_callback(uint8_t *data, uint32_t len)
  319. {
  320. (void)data;
  321. (void)len;
  322. }
  323. __WEAK void usbh_bluetooth_run(struct usbh_bluetooth *bluetooth_class)
  324. {
  325. (void)bluetooth_class;
  326. }
  327. __WEAK void usbh_bluetooth_stop(struct usbh_bluetooth *bluetooth_class)
  328. {
  329. (void)bluetooth_class;
  330. }
  331. static const struct usbh_class_driver bluetooth_class_driver = {
  332. .driver_name = "bluetooth",
  333. .connect = usbh_bluetooth_connect,
  334. .disconnect = usbh_bluetooth_disconnect
  335. };
  336. #ifdef CONFIG_USBHOST_BLUETOOTH_HCI_H4
  337. static const uint16_t bluetooth_id_table[][2] = {
  338. { 0x2fe3, 0x000c },
  339. { 0, 0 },
  340. };
  341. CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_h4_nrf_class_info = {
  342. .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
  343. .bInterfaceClass = 0xff,
  344. .bInterfaceSubClass = 0x00,
  345. .bInterfaceProtocol = 0x00,
  346. .id_table = bluetooth_id_table,
  347. .class_driver = &bluetooth_class_driver
  348. };
  349. #else
  350. CLASS_INFO_DEFINE const struct usbh_class_info bluetooth_class_info = {
  351. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  352. .bInterfaceClass = USB_DEVICE_CLASS_WIRELESS,
  353. .bInterfaceSubClass = 0x01,
  354. .bInterfaceProtocol = 0x01,
  355. .id_table = NULL,
  356. .class_driver = &bluetooth_class_driver
  357. };
  358. #endif