1 /*
2  * Copyright (c) 2018 Intel Corporation
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /* Bluetooth HCI USB transport layer implementation with following
9  * endpoint configuration:
10  *  - HCI commands through control endpoint (host-to-device only)
11  *  - HCI events through interrupt IN endpoint
12  *  - ACL data through one bulk IN and one bulk OUT endpoints
13  *
14  * TODO: revise transfer handling so that Bluetooth net_bufs can be used
15  * directly without intermediate copying.
16  *
17  * Limitations:
18  *  - Remote wakeup before IN transfer is not yet supported.
19  *  - H4 transport layer is not yet supported
20  */
21 
22 #include <zephyr/init.h>
23 #include <zephyr/sys/byteorder.h>
24 
25 #include <zephyr/usb/usbd.h>
26 #include <zephyr/usb/usb_ch9.h>
27 #include <zephyr/drivers/usb/udc.h>
28 
29 #include <zephyr/net_buf.h>
30 
31 #include <zephyr/bluetooth/buf.h>
32 #include <zephyr/bluetooth/hci_raw.h>
33 #include <zephyr/bluetooth/l2cap.h>
34 #include <zephyr/bluetooth/hci_vs.h>
35 #include <zephyr/drivers/bluetooth.h>
36 #include <zephyr/sys/atomic.h>
37 
38 #include <zephyr/logging/log.h>
39 LOG_MODULE_REGISTER(bt_hci, CONFIG_USBD_BT_HCI_LOG_LEVEL);
40 
41 #define BT_HCI_SUBCLASS		0x01
42 #define BT_HCI_PROTOCOL		0x01
43 
44 /*
45  * The actual endpoint addresses may differ from the following because
46  * the resources may be reallocated by the stack depending on the number
47  * of functions in a configuration and the properties of the controller.
48  */
49 #define BT_HCI_EP_EVENTS		0x81
50 #define BT_HCI_EP_ACL_DATA_IN		0x82
51 #define BT_HCI_EP_ACL_DATA_OUT		0x02
52 #define BT_HCI_EP_VOICE_IN		0x83
53 #define BT_HCI_EP_VOICE_OUT		0x03
54 
55 #define BT_HCI_EP_MPS_EVENTS		16
56 #define BT_HCI_EP_FS_MPS_ACL_DATA	64
57 #define BT_HCI_EP_HS_MPS_ACL_DATA	512
58 #define BT_HCI_EP_MPS_VOICE		9
59 
60 #define BT_HCI_EP_INTERVAL_EVENTS	1
61 #define BT_HCI_EP_INTERVAL_VOICE	3
62 
63 #define BT_HCI_CLASS_ENABLED		0
64 #define BT_HCI_ACL_RX_ENGAGED		1
65 
66 static K_FIFO_DEFINE(bt_hci_rx_queue);
67 static K_FIFO_DEFINE(bt_hci_tx_queue);
68 
69 /*
70  * Transfers through three endpoints proceed in a synchronous manner,
71  * with maximum packet size of high speed bulk endpoint.
72  *
73  * REVISE: global (bulk, interrupt, iso) specific pools would be more
74  * RAM usage efficient.
75  */
76 UDC_BUF_POOL_DEFINE(bt_hci_ep_pool,
77 		    3, 512,
78 		    sizeof(struct udc_buf_info), NULL);
79 /* HCI RX/TX threads */
80 static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
81 static struct k_thread rx_thread_data;
82 static K_KERNEL_STACK_DEFINE(tx_thread_stack, CONFIG_USBD_BT_HCI_TX_STACK_SIZE);
83 static struct k_thread tx_thread_data;
84 
85 /*
86  * We do not support voice channels and we do not implement
87  * isochronous endpoints handling, these are only available to match
88  * the recomendet configuration in the Bluetooth specification and
89  * avoid issues with Linux kernel btusb driver.
90  */
91 struct usbd_bt_hci_desc {
92 	struct usb_association_descriptor iad;
93 	struct usb_if_descriptor if0;
94 	struct usb_ep_descriptor if0_int_ep;
95 	struct usb_ep_descriptor if0_in_ep;
96 	struct usb_ep_descriptor if0_out_ep;
97 	struct usb_ep_descriptor if0_hs_in_ep;
98 	struct usb_ep_descriptor if0_hs_out_ep;
99 
100 	struct usb_if_descriptor if1_0;
101 	struct usb_ep_descriptor if1_0_iso_in_ep;
102 	struct usb_ep_descriptor if1_0_iso_out_ep;
103 	struct usb_if_descriptor if1_1;
104 	struct usb_ep_descriptor if1_1_iso_in_ep;
105 	struct usb_ep_descriptor if1_1_iso_out_ep;
106 
107 	struct usb_desc_header nil_desc;
108 };
109 
110 struct bt_hci_data {
111 	struct net_buf *acl_buf;
112 	struct usbd_bt_hci_desc *const desc;
113 	const struct usb_desc_header **const fs_desc;
114 	const struct usb_desc_header **const hs_desc;
115 	uint16_t acl_len;
116 	struct k_sem sync_sem;
117 	atomic_t state;
118 };
119 
120 /*
121  * Make supported device request visible for the stack
122  * bRequest 0x00 and bRequest 0xE0.
123  */
124 static const struct usbd_cctx_vendor_req bt_hci_vregs =
125 	USBD_VENDOR_REQ(0x00, 0xe0);
126 
bt_hci_get_int_in(struct usbd_class_data * const c_data)127 static uint8_t bt_hci_get_int_in(struct usbd_class_data *const c_data)
128 {
129 	struct bt_hci_data *data = usbd_class_get_private(c_data);
130 	struct usbd_bt_hci_desc *desc = data->desc;
131 
132 	return desc->if0_int_ep.bEndpointAddress;
133 }
134 
bt_hci_get_bulk_in(struct usbd_class_data * const c_data)135 static uint8_t bt_hci_get_bulk_in(struct usbd_class_data *const c_data)
136 {
137 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
138 	struct bt_hci_data *data = usbd_class_get_private(c_data);
139 	struct usbd_bt_hci_desc *desc = data->desc;
140 
141 	if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
142 		return desc->if0_hs_in_ep.bEndpointAddress;
143 	}
144 
145 	return desc->if0_in_ep.bEndpointAddress;
146 }
147 
bt_hci_get_bulk_out(struct usbd_class_data * const c_data)148 static uint8_t bt_hci_get_bulk_out(struct usbd_class_data *const c_data)
149 {
150 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
151 	struct bt_hci_data *data = usbd_class_get_private(c_data);
152 	struct usbd_bt_hci_desc *desc = data->desc;
153 
154 	if (usbd_bus_speed(uds_ctx) == USBD_SPEED_HS) {
155 		return desc->if0_hs_out_ep.bEndpointAddress;
156 	}
157 
158 	return desc->if0_out_ep.bEndpointAddress;
159 }
160 
bt_hci_buf_alloc(const uint8_t ep)161 struct net_buf *bt_hci_buf_alloc(const uint8_t ep)
162 {
163 	struct net_buf *buf = NULL;
164 	struct udc_buf_info *bi;
165 
166 	buf = net_buf_alloc(&bt_hci_ep_pool, K_NO_WAIT);
167 	if (!buf) {
168 		return NULL;
169 	}
170 
171 	bi = udc_get_buf_info(buf);
172 	bi->ep = ep;
173 
174 	return buf;
175 }
176 
bt_hci_tx_sync_in(struct usbd_class_data * const c_data,struct net_buf * const bt_buf,const uint8_t ep)177 static void bt_hci_tx_sync_in(struct usbd_class_data *const c_data,
178 			      struct net_buf *const bt_buf, const uint8_t ep)
179 {
180 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
181 	struct net_buf *buf;
182 
183 	buf = bt_hci_buf_alloc(ep);
184 	if (buf == NULL) {
185 		LOG_ERR("Failed to allocate buffer");
186 		return;
187 	}
188 
189 	net_buf_add_mem(buf, bt_buf->data, bt_buf->len);
190 	usbd_ep_enqueue(c_data, buf);
191 	k_sem_take(&hci_data->sync_sem, K_FOREVER);
192 	net_buf_unref(buf);
193 }
194 
bt_hci_tx_thread(void * p1,void * p2,void * p3)195 static void bt_hci_tx_thread(void *p1, void *p2, void *p3)
196 {
197 	struct usbd_class_data *const c_data = p1;
198 
199 	ARG_UNUSED(p2);
200 	ARG_UNUSED(p3);
201 
202 	while (true) {
203 		struct net_buf *bt_buf;
204 		uint8_t ep;
205 
206 		bt_buf = k_fifo_get(&bt_hci_tx_queue, K_FOREVER);
207 
208 		switch (bt_buf_get_type(bt_buf)) {
209 		case BT_BUF_EVT:
210 			ep = bt_hci_get_int_in(c_data);
211 			break;
212 		case BT_BUF_ACL_IN:
213 			ep = bt_hci_get_bulk_in(c_data);
214 			break;
215 		default:
216 			LOG_ERR("Unknown type %u", bt_buf_get_type(bt_buf));
217 			continue;
218 		}
219 
220 
221 		bt_hci_tx_sync_in(c_data, bt_buf, ep);
222 		net_buf_unref(bt_buf);
223 	}
224 }
225 
bt_hci_rx_thread(void * a,void * b,void * c)226 static void bt_hci_rx_thread(void *a, void *b, void *c)
227 {
228 	while (true) {
229 		struct net_buf *buf;
230 		int err;
231 
232 		/* FIXME: Do we need a separate thread for bt_send()? */
233 		buf = k_fifo_get(&bt_hci_rx_queue, K_FOREVER);
234 
235 		err = bt_send(buf);
236 		if (err) {
237 			LOG_ERR("Error sending to driver");
238 			net_buf_unref(buf);
239 		}
240 	}
241 }
242 
bt_hci_acl_out_start(struct usbd_class_data * const c_data)243 static int bt_hci_acl_out_start(struct usbd_class_data *const c_data)
244 {
245 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
246 	struct net_buf *buf;
247 	uint8_t ep;
248 	int ret;
249 
250 	if (!atomic_test_bit(&hci_data->state, BT_HCI_CLASS_ENABLED)) {
251 		return -EPERM;
252 	}
253 
254 	if (atomic_test_and_set_bit(&hci_data->state, BT_HCI_ACL_RX_ENGAGED)) {
255 		return -EBUSY;
256 	}
257 
258 	ep = bt_hci_get_bulk_out(c_data);
259 	buf = bt_hci_buf_alloc(ep);
260 	if (buf == NULL) {
261 		return -ENOMEM;
262 	}
263 
264 	ret = usbd_ep_enqueue(c_data, buf);
265 	if (ret) {
266 		LOG_ERR("Failed to enqueue net_buf for 0x%02x", ep);
267 		net_buf_unref(buf);
268 	}
269 
270 	return  ret;
271 }
272 
hci_pkt_get_len(struct net_buf * const buf,const uint8_t * data,const size_t size)273 static uint16_t hci_pkt_get_len(struct net_buf *const buf,
274 				const uint8_t *data, const size_t size)
275 {
276 	size_t hdr_len = 0;
277 	uint16_t len = 0;
278 
279 	switch (bt_buf_get_type(buf)) {
280 	case BT_BUF_CMD: {
281 		struct bt_hci_cmd_hdr *cmd_hdr;
282 
283 		hdr_len = sizeof(*cmd_hdr);
284 		cmd_hdr = (struct bt_hci_cmd_hdr *)data;
285 		len = cmd_hdr->param_len + hdr_len;
286 		break;
287 	}
288 	case BT_BUF_ACL_OUT: {
289 		struct bt_hci_acl_hdr *acl_hdr;
290 
291 		hdr_len = sizeof(*acl_hdr);
292 		acl_hdr = (struct bt_hci_acl_hdr *)data;
293 		len = sys_le16_to_cpu(acl_hdr->len) + hdr_len;
294 		break;
295 	}
296 	case BT_BUF_ISO_OUT: {
297 		struct bt_hci_iso_hdr *iso_hdr;
298 
299 		hdr_len = sizeof(*iso_hdr);
300 		iso_hdr = (struct bt_hci_iso_hdr *)data;
301 		len = bt_iso_hdr_len(sys_le16_to_cpu(iso_hdr->len)) + hdr_len;
302 		break;
303 	}
304 	default:
305 		LOG_ERR("Unknown BT buffer type");
306 		return 0;
307 	}
308 
309 	return (size < hdr_len) ? 0 : len;
310 }
311 
bt_hci_acl_out_cb(struct usbd_class_data * const c_data,struct net_buf * const buf,const int err)312 static int bt_hci_acl_out_cb(struct usbd_class_data *const c_data,
313 			     struct net_buf *const buf, const int err)
314 {
315 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
316 
317 	if (err) {
318 		goto restart_out_transfer;
319 	}
320 
321 	if (hci_data->acl_buf == NULL) {
322 		hci_data->acl_buf = bt_buf_get_tx(BT_BUF_ACL_OUT, K_FOREVER,
323 						  buf->data, buf->len);
324 		if (hci_data->acl_buf == NULL) {
325 			LOG_ERR("Failed to allocate net_buf");
326 			goto restart_out_transfer;
327 		}
328 
329 		hci_data->acl_len = hci_pkt_get_len(hci_data->acl_buf,
330 						    buf->data,
331 						    buf->len);
332 		LOG_DBG("acl_len %u, chunk %u", hci_data->acl_len, buf->len);
333 
334 		if (hci_data->acl_len == 0) {
335 			LOG_ERR("Failed to get packet length");
336 			net_buf_unref(hci_data->acl_buf);
337 			hci_data->acl_buf = NULL;
338 		}
339 	} else {
340 		if (net_buf_tailroom(hci_data->acl_buf) < buf->len) {
341 			LOG_ERR("Buffer tailroom too small");
342 			net_buf_unref(hci_data->acl_buf);
343 			hci_data->acl_buf = NULL;
344 			goto restart_out_transfer;
345 		}
346 
347 		/*
348 		 * Take over the next chunk if HCI packet is
349 		 * larger than USB_MAX_FS_BULK_MPS.
350 		 */
351 		net_buf_add_mem(hci_data->acl_buf, buf->data, buf->len);
352 		LOG_INF("len %u, chunk %u", hci_data->acl_buf->len, buf->len);
353 	}
354 
355 	if (hci_data->acl_buf != NULL && hci_data->acl_len == hci_data->acl_buf->len) {
356 		k_fifo_put(&bt_hci_rx_queue, hci_data->acl_buf);
357 		hci_data->acl_buf = NULL;
358 		hci_data->acl_len = 0;
359 	}
360 
361 restart_out_transfer:
362 	/* TODO: add function to reset transfer buffer and allow to reuse it */
363 	net_buf_unref(buf);
364 	atomic_clear_bit(&hci_data->state, BT_HCI_ACL_RX_ENGAGED);
365 
366 	return bt_hci_acl_out_start(c_data);
367 }
368 
bt_hci_request(struct usbd_class_data * const c_data,struct net_buf * buf,int err)369 static int bt_hci_request(struct usbd_class_data *const c_data,
370 			  struct net_buf *buf, int err)
371 {
372 	struct usbd_context *uds_ctx = usbd_class_get_ctx(c_data);
373 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
374 	struct udc_buf_info *bi;
375 
376 	bi = udc_get_buf_info(buf);
377 
378 	if (bi->ep == bt_hci_get_bulk_out(c_data)) {
379 		return bt_hci_acl_out_cb(c_data, buf, err);
380 	}
381 
382 	if (bi->ep == bt_hci_get_bulk_in(c_data) ||
383 	    bi->ep == bt_hci_get_int_in(c_data)) {
384 		k_sem_give(&hci_data->sync_sem);
385 
386 		return 0;
387 	}
388 
389 	return usbd_ep_buf_free(uds_ctx, buf);
390 }
391 
bt_hci_update(struct usbd_class_data * const c_data,uint8_t iface,uint8_t alternate)392 static void bt_hci_update(struct usbd_class_data *const c_data,
393 			  uint8_t iface, uint8_t alternate)
394 {
395 	LOG_DBG("New configuration, interface %u alternate %u",
396 		iface, alternate);
397 }
398 
bt_hci_enable(struct usbd_class_data * const c_data)399 static void bt_hci_enable(struct usbd_class_data *const c_data)
400 {
401 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
402 
403 	atomic_set_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
404 	LOG_INF("Configuration enabled");
405 
406 	if (bt_hci_acl_out_start(c_data)) {
407 		LOG_ERR("Failed to start ACL OUT transfer");
408 	}
409 }
410 
bt_hci_disable(struct usbd_class_data * const c_data)411 static void bt_hci_disable(struct usbd_class_data *const c_data)
412 {
413 	struct bt_hci_data *hci_data = usbd_class_get_private(c_data);
414 
415 	atomic_clear_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
416 	LOG_INF("Configuration disabled");
417 }
418 
bt_hci_ctd(struct usbd_class_data * const c_data,const struct usb_setup_packet * const setup,const struct net_buf * const buf)419 static int bt_hci_ctd(struct usbd_class_data *const c_data,
420 		      const struct usb_setup_packet *const setup,
421 		      const struct net_buf *const buf)
422 {
423 	struct net_buf *cmd_buf;
424 
425 	/* We expect host-to-device class request */
426 	if (setup->RequestType.type != USB_REQTYPE_TYPE_CLASS) {
427 		errno = -ENOTSUP;
428 
429 		return 0;
430 	}
431 
432 	LOG_DBG("bmRequestType 0x%02x bRequest 0x%02x",
433 		setup->bmRequestType, setup->bRequest);
434 
435 	cmd_buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT, buf->data, buf->len);
436 	if (!cmd_buf) {
437 		LOG_ERR("Cannot get free buffer");
438 
439 		return -ENOMEM;
440 	}
441 
442 	k_fifo_put(&bt_hci_rx_queue, cmd_buf);
443 
444 	return 0;
445 }
446 
bt_hci_get_desc(struct usbd_class_data * const c_data,const enum usbd_speed speed)447 static void *bt_hci_get_desc(struct usbd_class_data *const c_data,
448 			     const enum usbd_speed speed)
449 {
450 	struct bt_hci_data *data = usbd_class_get_private(c_data);
451 
452 	if (speed == USBD_SPEED_HS) {
453 		return data->hs_desc;
454 	}
455 
456 	return data->fs_desc;
457 }
458 
bt_hci_init(struct usbd_class_data * const c_data)459 static int bt_hci_init(struct usbd_class_data *const c_data)
460 {
461 	ARG_UNUSED(c_data);
462 
463 	return 0;
464 }
465 
466 static struct usbd_class_api bt_hci_api = {
467 	.request = bt_hci_request,
468 	.update = bt_hci_update,
469 	.enable = bt_hci_enable,
470 	.disable = bt_hci_disable,
471 	.control_to_dev = bt_hci_ctd,
472 	.get_desc = bt_hci_get_desc,
473 	.init = bt_hci_init,
474 };
475 
476 #define BT_HCI_DESCRIPTOR_DEFINE(n)						\
477 static struct usbd_bt_hci_desc bt_hci_desc_##n = {				\
478 	.iad = {								\
479 		.bLength = sizeof(struct usb_association_descriptor),		\
480 		.bDescriptorType = USB_DESC_INTERFACE_ASSOC,			\
481 		.bFirstInterface = 0,						\
482 		.bInterfaceCount = 0x02,					\
483 		.bFunctionClass = USB_BCC_WIRELESS_CONTROLLER,			\
484 		.bFunctionSubClass = BT_HCI_SUBCLASS,				\
485 		.bFunctionProtocol = BT_HCI_PROTOCOL,				\
486 		.iFunction = 0,							\
487 	},									\
488 										\
489 	.if0 = {								\
490 		.bLength = sizeof(struct usb_if_descriptor),			\
491 		.bDescriptorType = USB_DESC_INTERFACE,				\
492 		.bInterfaceNumber = 0,						\
493 		.bAlternateSetting = 0,						\
494 		.bNumEndpoints = 3,						\
495 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
496 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
497 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
498 		.iInterface = 0,						\
499 	},									\
500 										\
501 	.if0_int_ep = {								\
502 		.bLength = sizeof(struct usb_ep_descriptor),			\
503 		.bDescriptorType = USB_DESC_ENDPOINT,				\
504 		.bEndpointAddress = BT_HCI_EP_EVENTS,				\
505 		.bmAttributes = USB_EP_TYPE_INTERRUPT,				\
506 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_EVENTS),	\
507 		.bInterval = BT_HCI_EP_INTERVAL_EVENTS,				\
508 	},									\
509 										\
510 	.if0_in_ep = {								\
511 		.bLength = sizeof(struct usb_ep_descriptor),			\
512 		.bDescriptorType = USB_DESC_ENDPOINT,				\
513 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_IN,			\
514 		.bmAttributes = USB_EP_TYPE_BULK,				\
515 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_FS_MPS_ACL_DATA),	\
516 		.bInterval = 0,							\
517 	},									\
518 										\
519 	.if0_out_ep = {								\
520 		.bLength = sizeof(struct usb_ep_descriptor),			\
521 		.bDescriptorType = USB_DESC_ENDPOINT,				\
522 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_OUT,			\
523 		.bmAttributes = USB_EP_TYPE_BULK,				\
524 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_FS_MPS_ACL_DATA),	\
525 		.bInterval = 0,							\
526 	},									\
527 										\
528 	.if0_hs_in_ep = {							\
529 		.bLength = sizeof(struct usb_ep_descriptor),			\
530 		.bDescriptorType = USB_DESC_ENDPOINT,				\
531 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_IN,			\
532 		.bmAttributes = USB_EP_TYPE_BULK,				\
533 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_HS_MPS_ACL_DATA),	\
534 		.bInterval = 0,							\
535 	},									\
536 										\
537 	.if0_hs_out_ep = {							\
538 		.bLength = sizeof(struct usb_ep_descriptor),			\
539 		.bDescriptorType = USB_DESC_ENDPOINT,				\
540 		.bEndpointAddress = BT_HCI_EP_ACL_DATA_OUT,			\
541 		.bmAttributes = USB_EP_TYPE_BULK,				\
542 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_HS_MPS_ACL_DATA),	\
543 		.bInterval = 0,							\
544 	},									\
545 	.if1_0 = {								\
546 		.bLength = sizeof(struct usb_if_descriptor),			\
547 		.bDescriptorType = USB_DESC_INTERFACE,				\
548 		.bInterfaceNumber = 1,						\
549 		.bAlternateSetting = 0,						\
550 		.bNumEndpoints = 2,						\
551 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
552 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
553 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
554 		.iInterface = 0,						\
555 	},									\
556 										\
557 	.if1_0_iso_in_ep = {							\
558 		.bLength = sizeof(struct usb_ep_descriptor),			\
559 		.bDescriptorType = USB_DESC_ENDPOINT,				\
560 		.bEndpointAddress = BT_HCI_EP_VOICE_IN,				\
561 		.bmAttributes = USB_EP_TYPE_ISO,				\
562 		.wMaxPacketSize = sys_cpu_to_le16(0),				\
563 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
564 	},									\
565 										\
566 	.if1_0_iso_out_ep = {							\
567 		.bLength = sizeof(struct usb_ep_descriptor),			\
568 		.bDescriptorType = USB_DESC_ENDPOINT,				\
569 		.bEndpointAddress = BT_HCI_EP_VOICE_OUT,			\
570 		.bmAttributes = USB_EP_TYPE_ISO,				\
571 		.wMaxPacketSize = sys_cpu_to_le16(0),				\
572 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
573 	},									\
574 										\
575 	.if1_1 = {								\
576 		.bLength = sizeof(struct usb_if_descriptor),			\
577 		.bDescriptorType = USB_DESC_INTERFACE,				\
578 		.bInterfaceNumber = 1,						\
579 		.bAlternateSetting = 1,						\
580 		.bNumEndpoints = 2,						\
581 		.bInterfaceClass = USB_BCC_WIRELESS_CONTROLLER,			\
582 		.bInterfaceSubClass = BT_HCI_SUBCLASS,				\
583 		.bInterfaceProtocol = BT_HCI_PROTOCOL,				\
584 		.iInterface = 0,						\
585 	},									\
586 										\
587 	.if1_1_iso_in_ep = {							\
588 		.bLength = sizeof(struct usb_ep_descriptor),			\
589 		.bDescriptorType = USB_DESC_ENDPOINT,				\
590 		.bEndpointAddress = BT_HCI_EP_VOICE_IN,				\
591 		.bmAttributes = USB_EP_TYPE_ISO,				\
592 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE),		\
593 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
594 	},									\
595 										\
596 	.if1_1_iso_out_ep = {							\
597 		.bLength = sizeof(struct usb_ep_descriptor),			\
598 		.bDescriptorType = USB_DESC_ENDPOINT,				\
599 		.bEndpointAddress = BT_HCI_EP_VOICE_OUT,			\
600 		.bmAttributes = USB_EP_TYPE_ISO,				\
601 		.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE),		\
602 		.bInterval = BT_HCI_EP_INTERVAL_VOICE,				\
603 	},									\
604 										\
605 	.nil_desc = {								\
606 		.bLength = 0,							\
607 		.bDescriptorType = 0,						\
608 	},									\
609 };										\
610 										\
611 const static struct usb_desc_header *bt_hci_fs_desc_##n[] = {			\
612 	(struct usb_desc_header *) &bt_hci_desc_##n.iad,			\
613 	(struct usb_desc_header *) &bt_hci_desc_##n.if0,			\
614 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_int_ep,			\
615 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_in_ep,			\
616 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_out_ep,			\
617 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0,			\
618 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_in_ep,		\
619 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_out_ep,		\
620 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1,			\
621 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_in_ep,		\
622 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_out_ep,		\
623 	(struct usb_desc_header *) &bt_hci_desc_##n.nil_desc,			\
624 };										\
625 										\
626 const static struct usb_desc_header *bt_hci_hs_desc_##n[] = {			\
627 	(struct usb_desc_header *) &bt_hci_desc_##n.iad,			\
628 	(struct usb_desc_header *) &bt_hci_desc_##n.if0,			\
629 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_int_ep,			\
630 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_hs_in_ep,		\
631 	(struct usb_desc_header *) &bt_hci_desc_##n.if0_hs_out_ep,		\
632 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0,			\
633 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_in_ep,		\
634 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_0_iso_out_ep,		\
635 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1,			\
636 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_in_ep,		\
637 	(struct usb_desc_header *) &bt_hci_desc_##n.if1_1_iso_out_ep,		\
638 	(struct usb_desc_header *) &bt_hci_desc_##n.nil_desc,			\
639 };
640 
641 #define BT_HCI_CLASS_DATA_DEFINE(n)						\
642 	static struct bt_hci_data bt_hci_data_##n = {				\
643 		.sync_sem = Z_SEM_INITIALIZER(bt_hci_data_##n.sync_sem, 0, 1),	\
644 		.desc = &bt_hci_desc_##n,					\
645 		.fs_desc = bt_hci_fs_desc_##n,					\
646 		.hs_desc = bt_hci_hs_desc_##n,					\
647 	};									\
648 										\
649 	USBD_DEFINE_CLASS(bt_hci_##n, &bt_hci_api,				\
650 			  &bt_hci_data_##n, &bt_hci_vregs);
651 
652 /*
653  * Bluetooth subsystem does not support multiple HCI instances,
654  * but we are almost ready for it.
655  */
656 BT_HCI_DESCRIPTOR_DEFINE(0)
657 BT_HCI_CLASS_DATA_DEFINE(0)
658 
bt_hci_preinit(void)659 static int bt_hci_preinit(void)
660 {
661 	int ret;
662 
663 	ret = bt_enable_raw(&bt_hci_tx_queue);
664 	if (ret) {
665 		LOG_ERR("Failed to open Bluetooth raw channel: %d", ret);
666 		return ret;
667 	}
668 
669 	k_thread_create(&rx_thread_data, rx_thread_stack,
670 			K_KERNEL_STACK_SIZEOF(rx_thread_stack),
671 			bt_hci_rx_thread, NULL, NULL, NULL,
672 			K_PRIO_COOP(CONFIG_USBD_BT_HCI_RX_THREAD_PRIORITY),
673 			0, K_NO_WAIT);
674 
675 	k_thread_name_set(&rx_thread_data, "bt_hci_rx");
676 
677 	k_thread_create(&tx_thread_data, tx_thread_stack,
678 			K_KERNEL_STACK_SIZEOF(tx_thread_stack),
679 			bt_hci_tx_thread, (void *)&bt_hci_0, NULL, NULL,
680 			K_PRIO_COOP(CONFIG_USBD_BT_HCI_TX_THREAD_PRIORITY),
681 			0, K_NO_WAIT);
682 
683 	k_thread_name_set(&tx_thread_data, "bt_hci_tx");
684 
685 	return 0;
686 }
687 
688 SYS_INIT(bt_hci_preinit, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
689