1 #ifndef __SWM341_USB_H__
2 #define __SWM341_USB_H__
3 
4 #include <stdint.h>
5 
6 
7 typedef struct {
8     uint8_t  bRequestType;
9     uint8_t  bRequest;
10     uint16_t wValue;
11     uint16_t wIndex;
12     uint16_t wLength;
13 } USB_Setup_Packet_t;
14 
15 
16 /* bRequestType D7 Data Phase Transfer Direction  */
17 #define  USB_REQ_DIR_MASK           0x80
18 #define  USB_REQ_H2D                0x00
19 #define  USB_REQ_D2H                0x80
20 
21 /* bRequestType D6..5 Type */
22 #define  USB_REQ_STANDARD           0x00
23 #define  USB_REQ_CLASS              0x20
24 #define  USB_REQ_VENDOR             0x40
25 
26 /* bRequestType D4..0 Recipient */
27 #define  USB_REQ_TO_DEVICE          0x00
28 #define  USB_REQ_TO_INTERFACE       0x01
29 #define  USB_REQ_TO_ENDPOINT        0x02
30 
31 /* USB Standard Request */
32 #define USB_GET_STATUS              0x00
33 #define USB_CLEAR_FEATURE           0x01
34 #define USB_SET_FEATURE             0x03
35 #define USB_SET_ADDRESS             0x05
36 #define USB_GET_DESCRIPTOR          0x06
37 #define USB_SET_DESCRIPTOR          0x07
38 #define USB_GET_CONFIGURATION       0x08
39 #define USB_SET_CONFIGURATION       0x09
40 #define USB_GET_INTERFACE           0x0A
41 #define USB_SET_INTERFACE           0x0B
42 #define USB_SYNC_FRAME              0x0C
43 
44 /* USB Descriptor Type */
45 #define USB_DESC_DEVICE             0x01
46 #define USB_DESC_CONFIG             0x02
47 #define USB_DESC_STRING             0x03
48 #define USB_DESC_INTERFACE          0x04
49 #define USB_DESC_ENDPOINT           0x05
50 #define USB_DESC_QUALIFIER          0x06
51 #define USB_DESC_OTHERSPEED         0x07
52 #define USB_DESC_IFPOWER            0x08
53 #define USB_DESC_OTG                0x09
54 #define USB_DESC_BOS                0x0F
55 #define USB_DESC_CAPABILITY         0x10
56 #define USB_DESC_CS_INTERFACE       0x24    // Class Specific Interface
57 
58 /* USB HID Descriptor Type */
59 #define USB_DESC_HID                0x21
60 #define USB_DESC_HID_RPT            0x22
61 
62 /* USB Endpoint Type */
63 #define USB_EP_CTRL                 0x00
64 #define USB_EP_ISO                  0x01
65 #define USB_EP_BULK                 0x02
66 #define USB_EP_INT                  0x03
67 
68 #define USB_EP_IN                   0x80
69 #define USB_EP_OUT                  0x00
70 
71 /* USB Feature Selector */
72 #define USB_FEATURE_REMOTE_WAKEUP   0x01
73 #define USB_FEATURE_ENDPOINT_HALT   0x00
74 
75 /* USB HID Class Report Type */
76 #define HID_RPT_TYPE_INPUT          0x01
77 #define HID_RPT_TYPE_OUTPUT         0x02
78 #define HID_RPT_TYPE_FEATURE        0x03
79 
80 /* Define HID Class Specific Request */
81 #define USB_HID_GET_REPORT          0x01
82 #define USB_HID_GET_IDLE            0x02
83 #define USB_HID_GET_PROTOCOL        0x03
84 #define USB_HID_SET_REPORT          0x09
85 #define USB_HID_SET_IDLE            0x0A
86 #define USB_HID_SET_PROTOCOL        0x0B
87 
88 
89 /* Class */
90 #define USB_CDC_CLASS               0x02    // for Device
91 #define USB_CDC_CTRL_CLASS          0x02    // for Interface
92 #define USB_CDC_DATA_CLASS          0x0A    // for Interface
93 #define USB_HID_CLASS               0x03    // for Interface
94 #define USB_MTP_CLASS               0x06    // for Interface
95 #define USB_MSC_CLASS               0x08    // for Interface
96 #define USB_UVC_CLASS               0x0E    // for Interface
97 
98 
99 /* SubClass */
100 #define USB_CDC_ACM                 0x02    // Abstract Control Model
101 #define USB_HID_BOOT                0x01
102 #define USB_UVC_VIDEOCONTROL                0x01
103 #define USB_UVC_VIDEOSTREAMING              0x02
104 #define USB_UVC_VIDEO_INTERFACE_COLLECTION  0x03
105 
106 /* Protocol */
107 #define USB_CDC_ATCMD               0x01    // AT Commands defined by ITU-T V.250
108 #define USB_HID_NONE                0x00
109 #define USB_HID_KEYBD               0x01
110 #define USB_HID_MOUSE               0x02
111 #define USB_MSC_BOT                 0x50    // Bulk-Only Transport
112 
113 
114 typedef struct {
115     uint8_t  bLength;
116     uint8_t  bDescriptorType;
117 } USB_DescHeader_t;
118 
119 typedef struct __attribute__((packed)) {
120     uint8_t   bLength;
121     uint8_t   bDescriptorType;
122     uint16_t  bcdUSB;               // USB Specification Number which device complies to
123     uint8_t   bDeviceClass;         // 0x00: each interface specifies its own class code
124     uint8_t   bDeviceSubClass;
125     uint8_t   bDeviceProtocol;
126     uint8_t   bMaxPacketSize;
127     uint16_t  idVendor;             // Vendor ID (Assigned by USB Org)
128     uint16_t  idProduct;            // Product ID (Assigned by Manufacturer)
129     uint16_t  bcdDevice;            // Device Release Number
130     uint8_t   iManufacturer;        // Index of Manufacturer String Descriptor
131     uint8_t   iProduct;             // Index of Product String Descriptor
132     uint8_t   iSerialNumber;        // Index of Serial Number String Descriptor
133     uint8_t   bNumConfigurations;   // Number of Possible Configurations
134 } USB_DevDesc_t;
135 
136 typedef struct __attribute__((packed)) {
137     uint8_t   bLength;
138     uint8_t   bDescriptorType;
139     uint16_t  wTotalLength;         // Total Length
140     uint8_t   bNumInterfaces;       // Number of Interfaces
141     uint8_t   bConfigurationValue;  // Value to use as an argument to select this configuration
142     uint8_t   iConfiguration;       // Index of String Descriptor Describing this configuration
143     uint8_t   bmAttributes;         // D7 Bus Powered , D6 Self Powered, D5 Remote Wakeup , D4..0 Reserved (0)
144     uint8_t   bMaxPower;            // Maximum Power Consumption
145 } USB_CfgDesc_t;
146 
147 typedef struct __attribute__((packed)) {
148     uint8_t bLength;
149     uint8_t bDescriptorType;
150     uint8_t bInterfaceNumber;
151     uint8_t bAlternateSetting;      // Value used to select alternative setting
152     uint8_t bNumEndpoints;          // Number of Endpoints used for this interface
153     uint8_t bInterfaceClass;
154     uint8_t bInterfaceSubClass;
155     uint8_t bInterfaceProtocol;
156     uint8_t iInterface;             // Index of String Descriptor Describing this interface
157 } USB_IntfDesc_t;
158 
159 typedef struct __attribute__((packed)) {
160     uint8_t   bLength;
161     uint8_t   bDescriptorType;
162     uint8_t   bEndpointAddress;     // indicates what endpoint this descriptor is describing
163     uint8_t   bmAttributes;         // specifies the transfer type.
164     uint16_t  wMaxPacketSize;       // Maximum Packet Size this endpoint is capable of sending or receiving
165     uint8_t   bInterval;            // is used to specify the polling interval of certain transfers.
166 } USB_EpDesc_t;
167 
168 typedef struct __attribute__((packed)) {
169     uint8_t   bLength;
170     uint8_t   bDescriptorType;
171     uint16_t  bcdHID;               // indicates what endpoint this descriptor is describing
172     uint8_t   bCountryCode;
173     uint8_t   bNumDescriptors;
174     uint8_t   bReportDescriptorType;
175     uint16_t  wItemLength;
176 } USB_HIDDesc_t;
177 
178 
179 /* Header Functional Descriptor, which marks the beginning of the
180    concatenated set of functional descriptors for the interface. */
181 typedef struct __attribute__((packed)) {
182     uint8_t  bLength;
183     uint8_t  bDescriptorType;       // CS_INTERFACE (0x24)
184     uint8_t  bDescriptorSubType;    // 0x00
185     uint16_t bcdCDC;
186 } USB_CDC_HeaderFuncDesc_t;
187 
188 
189 /* Call Management Functional Descriptor */
190 typedef struct __attribute__((packed)) {
191     uint8_t  bLength;
192     uint8_t  bDescriptorType;       // CS_INTERFACE (0x24)
193     uint8_t  bDescriptorSubType;    // 0x01
194     uint8_t  bmCapabilities;
195     uint8_t  bDataInterface;
196 } USB_CDC_CallMgmtFuncDesc_t;
197 
198 
199 /* Abstract Control Management Functional Descriptor */
200 typedef struct __attribute__((packed)) {
201     uint8_t  bLength;
202     uint8_t  bDescriptorType;       // CS_INTERFACE (0x24)
203     uint8_t  bDescriptorSubType;    // 0x02
204     uint8_t  bmCapabilities;
205 } USB_CDC_AbstCntrlMgmtFuncDesc_t;
206 
207 
208 /* Union Functional Descriptor */
209 typedef struct __attribute__((packed)) {
210     uint8_t  bLength;
211     uint8_t  bDescriptorType;       // CS_INTERFACE (0x24)
212     uint8_t  bDescriptorSubType;    // 0x06
213     uint8_t  bMasterInterface;      // Interface number of the Communication or Data Class interface
214     uint8_t  bSlaveInterface0;      // Interface number of first slave
215 } USB_CDC_UnionFuncDesc_t;
216 
217 
218 #endif //__SWM341_USB_H__
219