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