1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(usb_bos, CONFIG_USB_DEVICE_LOG_LEVEL);
9 
10 #include <zephyr/kernel.h>
11 
12 #include <zephyr/usb/usb_device.h>
13 
14 #include <zephyr/usb/bos.h>
15 
16 extern const uint8_t __usb_bos_desc_start[];
17 extern const uint8_t __usb_bos_desc_end[];
18 
19 USB_DEVICE_BOS_DESC_DEFINE_HDR struct usb_bos_descriptor bos_hdr = {
20 	.bLength = sizeof(struct usb_bos_descriptor),
21 	.bDescriptorType = USB_DESC_BOS,
22 	.wTotalLength = 0, /* should be corrected with register */
23 	.bNumDeviceCaps = 0, /* should be set with register */
24 };
25 
usb_bos_get_length(void)26 size_t usb_bos_get_length(void)
27 {
28 	return __usb_bos_desc_end - __usb_bos_desc_start;
29 }
30 
usb_bos_get_header(void)31 const void *usb_bos_get_header(void)
32 {
33 	return __usb_bos_desc_start;
34 }
35 
usb_bos_fix_total_length(void)36 void usb_bos_fix_total_length(void)
37 {
38 	bos_hdr.wTotalLength = usb_bos_get_length();
39 }
40 
usb_bos_register_cap(struct usb_bos_platform_descriptor * desc)41 void usb_bos_register_cap(struct usb_bos_platform_descriptor *desc)
42 {
43 	/* Has effect only on first register */
44 	bos_hdr.wTotalLength = usb_bos_get_length();
45 
46 	bos_hdr.bNumDeviceCaps += 1U;
47 }
48 
usb_handle_bos(struct usb_setup_packet * setup,int32_t * len,uint8_t ** data)49 int usb_handle_bos(struct usb_setup_packet *setup,
50 		   int32_t *len, uint8_t **data)
51 {
52 	if (USB_GET_DESCRIPTOR_TYPE(setup->wValue) == USB_DESC_BOS) {
53 		LOG_DBG("Read BOS descriptor");
54 		*data = (uint8_t *)usb_bos_get_header();
55 		*len = usb_bos_get_length();
56 
57 		return 0;
58 	}
59 
60 	return -ENOTSUP;
61 }
62