1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #ifndef LIBRARIES_MAXUSB_INCLUDE_CORE_USB_PROTOCOL_H_
22 #define LIBRARIES_MAXUSB_INCLUDE_CORE_USB_PROTOCOL_H_
23 
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* SETUP message byte offsets */
31 #define SETUP_bmRequestType   0
32 #define SETUP_bRequest        1
33 #define SETUP_wValueL         2
34 #define SETUP_wValueH         3
35 #define SETUP_wIndexL         4
36 #define SETUP_wIndexH         5
37 #define SETUP_wLengthL        6
38 #define SETUP_wLengthH        7
39 
40 typedef struct {
41   uint8_t  bmRequestType;
42   uint8_t  bRequest;
43   uint16_t wValue;
44   uint16_t wIndex;
45   uint16_t wLength;
46 } MXC_USB_SetupPkt;
47 
48 /* Bitmasks for the bit-field bmRequestType */
49 #define RT_DEV_TO_HOST            0x80
50 
51 #define RT_TYPE_MASK              0x60
52 #define RT_TYPE_STD               0x00
53 #define RT_TYPE_CLASS             0x20
54 #define RT_TYPE_VENDOR            0x40
55 
56 #define RT_RECIP_MASK             0x1f
57 #define RT_RECIP_DEVICE           0x00
58 #define RT_RECIP_IFACE            0x01
59 #define RT_RECIP_ENDP             0x02
60 #define RT_RECIP_OTHER            0x03
61 
62 /* Standard Device Requests for bRequest */
63 #define SDR_GET_STATUS            0x00
64 #define SDR_CLEAR_FEATURE         0x01
65 #define SDR_SET_FEATURE           0x03
66 #define SDR_SET_ADDRESS           0x05
67 #define SDR_GET_DESCRIPTOR        0x06
68 #define SDR_SET_DESCRIPTOR        0x07
69 #define SDR_GET_CONFIG            0x08
70 #define SDR_SET_CONFIG            0x09
71 #define SDR_GET_INTERFACE         0x0a
72 #define SDR_SET_INTERFACE         0x0b
73 #define SDR_SYNCH_FRAME           0x0c
74 
75 /* Descriptor types for *_DESCRIPTOR */
76 #define DESC_DEVICE               1
77 #define DESC_CONFIG               2
78 #define DESC_STRING               3
79 #define DESC_INTERFACE            4
80 #define DESC_ENDPOINT             5
81 #define DESC_DEVICE_QUAL          6
82 #define DESC_OTHER_SPEED          7
83 #define DESC_IFACE_PWR            8
84 
85 /* Feature types for *_FEATURE */
86 #define FEAT_ENDPOINT_HALT        0
87 #define FEAT_REMOTE_WAKE          1
88 #define FEAT_TEST_MODE            2
89 
90 /* Get Status bit positions */
91 #define STATUS_EP_HALT            0x1
92 #define STATUS_DEV_SELF_POWERED   0x1
93 #define STATUS_DEV_REMOTE_WAKE    0x2
94 
95 /* bmAttributes bit positions */
96 #define BMATT_REMOTE_WAKE         0x20
97 #define BMATT_SELF_POWERED        0x40
98 
99 #if defined(__GNUC__)
100 typedef struct __attribute__((packed)) {
101 #else
102 typedef __packed struct {
103 #endif
104   uint8_t  bLength;
105   uint8_t  bDescriptorType;
106   uint16_t bcdUSB;
107   uint8_t  bDeviceClass;
108   uint8_t  bDeviceSubClass;
109   uint8_t  bDeviceProtocol;
110   uint8_t  bMaxPacketSize;
111   uint16_t idVendor;
112   uint16_t idProduct;
113   uint16_t bcdDevice;
114   uint8_t  iManufacturer;
115   uint8_t  iProduct;
116   uint8_t  iSerialNumber;
117   uint8_t  bNumConfigurations;
118 } MXC_USB_device_descriptor_t;
119 
120 #if defined(__GNUC__)
121 typedef struct __attribute__((packed)) {
122 #else
123 typedef __packed struct {
124 #endif
125   uint8_t  bLength;
126   uint8_t  bDescriptorType;
127   uint16_t wTotalLength;
128   uint8_t  bNumInterfaces;
129   uint8_t  bConfigurationValue;
130   uint8_t  iConfiguration;
131   uint8_t  bmAttributes;
132   uint8_t  bMaxPower;
133 } MXC_USB_configuration_descriptor_t;
134 
135 #if defined(__GNUC__)
136 typedef struct __attribute__((packed)) {
137 #else
138 typedef __packed struct {
139 #endif
140   uint8_t  bLength;
141   uint8_t  bDescriptorType;
142   uint8_t  bInterfaceNumber;
143   uint8_t  bAlternateSetting;
144   uint8_t  bNumEndpoints;
145   uint8_t  bInterfaceClass;
146   uint8_t  bInterfaceSubClass;
147   uint8_t  bInterfaceProtocol;
148   uint8_t  iInterface;
149 } MXC_USB_interface_descriptor_t;
150 
151 #define USB_EP_NUM_MASK   0x0F
152 
153 #ifndef USE_ZEPHYR_USB_STACK
154 #define USB_EP_DIR_MASK   0x80
155 #endif
156 
157 #if defined(__GNUC__)
158 typedef struct __attribute__((packed)) {
159 #else
160 typedef __packed struct {
161 #endif
162   uint8_t  bLength;
163   uint8_t  bDescriptorType;
164   uint8_t  bEndpointAddress;
165   uint8_t  bmAttributes;
166   uint16_t wMaxPacketSize;
167   uint8_t  bInterval;
168 } MXC_USB_endpoint_descriptor_t;
169 
170 #if defined(__GNUC__)
171 typedef struct __attribute__((packed)) {
172 #else
173 typedef __packed struct {
174 #endif
175   uint8_t  bLength;
176   uint8_t  bDescriptorType;
177   uint16_t bcdUSB;
178   uint8_t  bDeviceClass;
179   uint8_t  bDeviceSubClass;
180   uint8_t  bDeviceProtocol;
181   uint8_t  bMaxPacketSize;
182   uint8_t  bNumConfigurations;
183   uint8_t  bReserved;
184 } MXC_USB_device_qualifier_descriptor_t;
185 
186 #if defined(__GNUC__)
187 typedef struct __attribute__((packed)) {
188 #else
189 typedef __packed struct {
190 #endif
191   uint8_t  bLength;
192   uint8_t  bDescriptorType;
193   uint16_t wTotalLength;
194   uint8_t  bNumInterfaces;
195   uint8_t  bConfigurationValue;
196   uint8_t  iConfiguration;
197   uint8_t  bmAttributes;
198   uint8_t  bMaxPower;
199 } MXC_USB_other_speed_configuration_descriptor_t;
200 
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #endif //LIBRARIES_MAXUSB_INCLUDE_CORE_USB_PROTOCOL_H_
206