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_os_desc, CONFIG_USB_DEVICE_LOG_LEVEL);
9 
10 #include <zephyr/kernel.h>
11 
12 #include <zephyr/usb/usb_device.h>
13 #include <os_desc.h>
14 
15 static struct usb_os_descriptor *os_desc;
16 
usb_handle_os_desc(struct usb_setup_packet * setup,int32_t * len,uint8_t ** data)17 int usb_handle_os_desc(struct usb_setup_packet *setup,
18 		       int32_t *len, uint8_t **data)
19 {
20 	if (!os_desc) {
21 		return -ENOTSUP;
22 	}
23 
24 	if (USB_GET_DESCRIPTOR_TYPE(setup->wValue) == USB_DESC_STRING &&
25 	    USB_GET_DESCRIPTOR_INDEX(setup->wValue) == USB_OSDESC_STRING_DESC_INDEX) {
26 		LOG_DBG("MS OS Descriptor string read");
27 		*data = os_desc->string;
28 		*len = os_desc->string_len;
29 
30 		return 0;
31 	}
32 
33 	return -ENOTSUP;
34 }
35 
usb_handle_os_desc_feature(struct usb_setup_packet * setup,int32_t * len,uint8_t ** data)36 int usb_handle_os_desc_feature(struct usb_setup_packet *setup,
37 			       int32_t *len, uint8_t **data)
38 {
39 	LOG_DBG("bRequest 0x%x", setup->bRequest);
40 
41 	if (!os_desc) {
42 		return -ENOTSUP;
43 	}
44 
45 	if (setup->bRequest == os_desc->vendor_code) {
46 		switch (setup->wIndex) {
47 		case USB_OSDESC_EXTENDED_COMPAT_ID:
48 			LOG_DBG("Handle Compat ID");
49 			*data = os_desc->compat_id;
50 			*len = os_desc->compat_id_len;
51 
52 			return 0;
53 		default:
54 			break;
55 		}
56 	}
57 
58 	return -ENOTSUP;
59 }
60 
61 /* Register MS OS Descriptors version 1 */
usb_register_os_desc(struct usb_os_descriptor * desc)62 void usb_register_os_desc(struct usb_os_descriptor *desc)
63 {
64 	os_desc = desc;
65 }
66 
usb_os_desc_enabled(void)67 bool usb_os_desc_enabled(void)
68 {
69 	return !!os_desc;
70 }
71