1 /* usb_descriptor.h - header for common device descriptor */
2 
3 /*
4  * Copyright (c) 2017 PHYTEC Messtechnik GmbH
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef __USB_DESCRIPTOR_H__
10 #define __USB_DESCRIPTOR_H__
11 
12 /*
13  * The USB Unicode bString is encoded in UTF16LE, which means it takes up
14  * twice the amount of bytes than the same string encoded in ASCII7.
15  * Use this macro to determine the length of the bString array.
16  *
17  * bString length without null character:
18  *   bString_length = (sizeof(initializer_string) - 1) * 2
19  * or:
20  *   bString_length = sizeof(initializer_string) * 2 - 2
21  */
22 #define USB_BSTRING_LENGTH(s)		(sizeof(s) * 2 - 2)
23 
24 /*
25  * The length of the string descriptor (bLength) is calculated from the
26  * size of the two octets bLength and bDescriptorType plus the
27  * length of the UTF16LE string:
28  *
29  *   bLength = 2 + bString_length
30  *   bLength = 2 + sizeof(initializer_string) * 2 - 2
31  *   bLength = sizeof(initializer_string) * 2
32  * Use this macro to determine the bLength of the string descriptor.
33  */
34 #define USB_STRING_DESCRIPTOR_LENGTH(s)	(sizeof(s) * 2)
35 
36 /* Automatic endpoint assignment */
37 #define AUTO_EP_IN			0x80
38 #define AUTO_EP_OUT			0x00
39 
40 /* Common part of device data */
41 struct usb_dev_data {
42 	const struct device *dev;
43 	sys_snode_t node;
44 };
45 
46 struct usb_dev_data *usb_get_dev_data_by_cfg(sys_slist_t *list,
47 					     struct usb_cfg_data *cfg);
48 struct usb_dev_data *usb_get_dev_data_by_iface(sys_slist_t *list,
49 					       uint8_t iface_num);
50 struct usb_dev_data *usb_get_dev_data_by_ep(sys_slist_t *list, uint8_t ep);
51 
52 int usb_get_str_descriptor_idx(void *ptr);
53 
54 uint8_t *usb_update_sn_string_descriptor(void);
55 uint8_t *usb_get_device_descriptor(void);
56 
57 #endif /* __USB_DESCRIPTOR_H__ */
58