1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef USB_DDK_H
17 #define USB_DDK_H
18
19 #include "hdf_base.h"
20 #include <endian.h>
21
22 #if __BYTE_ORDER == __LITTLE_ENDIAN
23 #define CpuToLe16(x) (x)
24 #define CpuToLe32(x) (x)
25 #else
26 #define CpuToLe16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
27 #define CpuToLe32(x) \
28 ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
29 (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
30 #endif
31
32 #define Le16ToCpu CpuToLe16
33 #define Le32ToCpu CpuToLe32
34
35 #define USB_CFG_BUS_POWERED 0x80
36 #define USB_CFG_SELF_POWERED 0x40
37 #define USB_CFG_REMOTE_WAKEUP 0x20
38
39 #define USB_DIR_OFFSET 0x07
40 #define USB_TYPE_OFFSET 0x05
41 #define USB_RECIP_OFFSET 0x00
42
43 enum {
44 USB_FUNC_MANUFACTURER_IDX,
45 USB_FUNC_PRODUCT_IDX,
46 USB_FUNC_SERIAL_IDX,
47 USB_FUNC_FIRST_AVAIL_IDX,
48 };
49
50 enum {
51 FUNCTIONFS_DESCRIPTORS_MAGIC = 1,
52 FUNCTIONFS_STRINGS_MAGIC = 2,
53 FUNCTIONFS_DESCRIPTORS_MAGIC_V2 = 3,
54 };
55
56 enum functionfs_flags {
57 FUNCTIONFS_HAS_FS_DESC = 1,
58 FUNCTIONFS_HAS_HS_DESC = 2,
59 FUNCTIONFS_HAS_SS_DESC = 4,
60 FUNCTIONFS_HAS_MS_OS_DESC = 8,
61 FUNCTIONFS_VIRTUAL_ADDR = 16,
62 FUNCTIONFS_EVENTFD = 32,
63 FUNCTIONFS_ALL_CTRL_RECIP = 64,
64 FUNCTIONFS_CONFIG0_SETUP = 128,
65 };
66
67 enum UsbDeviceSpeed {
68 USB_DDK_SPEED_UNKNOWN = 0,
69 USB_DDK_SPEED_LOW,
70 USB_DDK_SPEED_FULL,
71 USB_DDK_SPEED_HIGH,
72 USB_DDK_SPEED_WIRELESS,
73 USB_DDK_SPEED_SUPER,
74 USB_DDK_SPEED_SUPER_PLUS,
75 };
76
77 struct UsbCtrlRequest {
78 uint8_t bRequestType;
79 uint8_t bRequest;
80 uint16_t wValue;
81 uint16_t wIndex;
82 uint16_t wLength;
83 } __attribute__ ((packed));
84
85 struct UsbDescriptorHeader {
86 uint8_t bLength;
87 uint8_t bDescriptorType;
88 } __attribute__ ((packed));
89
90 struct UsbDeviceDescriptor {
91 uint8_t bLength;
92 uint8_t bDescriptorType;
93 uint16_t bcdUSB;
94 uint8_t bDeviceClass;
95 uint8_t bDeviceSubClass;
96 uint8_t bDeviceProtocol;
97 uint8_t bMaxPacketSize0;
98 uint16_t idVendor;
99 uint16_t idProduct;
100 uint16_t bcdDevice;
101 uint8_t iManufacturer;
102 uint8_t iProduct;
103 uint8_t iSerialNumber;
104 uint8_t bNumConfigurations;
105 } __attribute__ ((packed));
106
107 struct UsbConfigDescriptor {
108 uint8_t bLength;
109 uint8_t bDescriptorType;
110 uint16_t wTotalLength;
111 uint8_t bNumInterfaces;
112 uint8_t bConfigurationValue;
113 uint8_t iConfiguration;
114 uint8_t bmAttributes;
115 uint8_t bMaxPower;
116 } __attribute__ ((packed));
117
118 struct UsbStringDescriptor {
119 uint8_t bLength;
120 uint8_t bDescriptorType;
121 uint16_t wData[1];
122 } __attribute__ ((packed));
123
124 struct UsbInterfaceDescriptor {
125 uint8_t bLength;
126 uint8_t bDescriptorType;
127 uint8_t bInterfaceNumber;
128 uint8_t bAlternateSetting;
129 uint8_t bNumEndpoints;
130 uint8_t bInterfaceClass;
131 uint8_t bInterfaceSubClass;
132 uint8_t bInterfaceProtocol;
133 uint8_t iInterface;
134 } __attribute__ ((packed));
135
136 struct UsbEndpointDescriptor {
137 uint8_t bLength;
138 uint8_t bDescriptorType;
139 uint8_t bEndpointAddress;
140 uint8_t bmAttributes;
141 uint16_t wMaxPacketSize;
142 uint8_t bInterval;
143 uint8_t bRefresh;
144 uint8_t bSynchAddress;
145 } __attribute__ ((packed));
146
147 struct UsbSspIsocEpCompDescriptor {
148 uint8_t bLength;
149 uint8_t bDescriptorType;
150 uint16_t wReseved;
151 uint32_t dwBytesPerInterval;
152 } __attribute__ ((packed));
153
154 struct UsbSsEpCompDescriptor {
155 uint8_t bLength;
156 uint8_t bDescriptorType;
157 uint8_t bMaxBurst;
158 uint8_t bmAttributes;
159 uint16_t wBytesPerInterval;
160 } __attribute__ ((packed));
161
162 struct UsbQualifierDescriptor {
163 uint8_t bLength;
164 uint8_t bDescriptorType;
165 uint16_t bcdUSB;
166 uint8_t bDeviceClass;
167 uint8_t bDeviceSubClass;
168 uint8_t bDeviceProtocol;
169 uint8_t bMaxPacketSize0;
170 uint8_t bNumConfigurations;
171 uint8_t bRESERVED;
172 } __attribute__ ((packed));
173
174 struct UsbOtgDescriptor {
175 uint8_t bLength;
176 uint8_t bDescriptorType;
177 uint8_t bmAttributes;
178 } __attribute__ ((packed));
179
180 struct UsbOtg20Descriptor {
181 uint8_t bLength;
182 uint8_t bDescriptorType;
183 uint8_t bmAttributes;
184 uint16_t bcdOTG;
185 } __attribute__ ((packed));
186
187 struct UsbInterfaceAssocDescriptor {
188 uint8_t bLength;
189 uint8_t bDescriptorType;
190 uint8_t bFirstInterface;
191 uint8_t bInterfaceCount;
192 uint8_t bFunctionClass;
193 uint8_t bFunctionSubClass;
194 uint8_t bFunctionProtocol;
195 uint8_t iFunction;
196 } __attribute__ ((packed));
197
198 struct UsbFunctionfsStringsHead {
199 uint32_t magic;
200 uint32_t length;
201 uint32_t str_count;
202 uint32_t lang_count;
203 } __attribute__((packed));
204
205 struct UsbFunctionfsDescsHeadV2 {
206 uint32_t magic;
207 uint32_t length;
208 uint32_t flags;
209 } __attribute__((packed));
210
211 struct UsbSecurityDescriptor {
212 uint8_t bLength;
213 uint8_t bDescriptorType;
214 uint16_t wTotalLength;
215 uint8_t bNumEncryptionTypes;
216 } __attribute__((packed));
217
218 struct UsbCdcLineCoding {
219 uint32_t dwDTERate;
220 uint8_t bCharFormat;
221
222 #define USB_CDC_1_STOP_BITS 0
223 #define USB_CDC_1_5_STOP_BITS 1
224 #define USB_CDC_2_STOP_BITS 2
225 uint8_t bParityType;
226
227 #define USB_CDC_NO_PARITY 0
228 #define USB_CDC_ODD_PARITY 1
229 #define USB_CDC_EVEN_PARITY 2
230 #define USB_CDC_MARK_PARITY 3
231 #define USB_CDC_SPACE_PARITY 4
232 uint8_t bDataBits;
233 } __attribute__ ((packed));
234
235 struct UsbCdcNotification {
236 uint8_t bmRequestType;
237 uint8_t bNotificationType;
238 uint16_t wValue;
239 uint16_t wIndex;
240 uint16_t wLength;
241 } __attribute__ ((packed));
242
243 struct UsbCdcHeaderDesc {
244 uint8_t bLength;
245 uint8_t bDescriptorType;
246 uint8_t bDescriptorSubType;
247 uint16_t bcdCDC;
248 } __attribute__ ((packed));
249
250 struct UsbCdcCallMgmtDescriptor {
251 uint8_t bLength;
252 uint8_t bDescriptorType;
253 uint8_t bDescriptorSubType;
254 uint8_t bmCapabilities;
255
256 #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01
257 #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02
258 uint8_t bDataInterface;
259 } __attribute__ ((packed));
260
261 struct UsbCdcAcmDescriptor {
262 uint8_t bLength;
263 uint8_t bDescriptorType;
264 uint8_t bDescriptorSubType;
265 uint8_t bmCapabilities;
266 } __attribute__ ((packed));
267
268 struct UsbCdcUnionDesc {
269 uint8_t bLength;
270 uint8_t bDescriptorType;
271 uint8_t bDescriptorSubType;
272 uint8_t bMasterInterface0;
273 uint8_t bSlaveInterface0;
274 } __attribute__ ((packed));
275
276 struct UsbDebugDescriptor {
277 uint8_t bLength;
278 uint8_t bDescriptorType;
279 uint8_t bDebugInEndpoint;
280 uint8_t bDebugOutEndpoint;
281 } __attribute__((packed));
282
283 struct UsbCdcEtherDesc {
284 uint8_t bLength;
285 uint8_t bDescriptorType;
286 uint8_t bDescriptorSubType;
287
288 uint8_t iMACAddress;
289 uint32_t bmEthernetStatistics;
290 uint16_t wMaxSegmentSize;
291 uint16_t wNumberMCFilters;
292 uint8_t bNumberPowerFilters;
293 } __attribute__ ((packed));
294
295 #define USB_DDK_ENDPOINT_NUMBER_MASK 0x0F
296 #define USB_DDK_ENDPOINT_DIR_MASK 0x80
297 #define USB_DDK_DIR_OUT 0x00
298 #define USB_DDK_DIR_IN 0x80
299
300 #define USB_DDK_DT_DEVICE 0x01
301 #define USB_DDK_DT_CONFIG 0x02
302 #define USB_DDK_DT_STRING 0x03
303 #define USB_DDK_DT_INTERFACE 0x04
304 #define USB_DDK_DT_ENDPOINT 0x05
305 #define USB_DDK_DT_DEVICE_QUALIFIER 0x06
306 #define USB_DDK_DT_OTHER_SPEED_CONFIG 0x07
307 #define USB_DDK_DT_INTERFACE_POWER 0x08
308 #define USB_DDK_DT_OTG 0x09
309 #define USB_DDK_DT_DEBUG 0x0A
310 #define USB_DDK_DT_INTERFACE_ASSOCIATION 0x0B
311 #define USB_DDK_DT_SECURITY 0x0C
312 #define USB_DDK_DT_KEY 0x0D
313 #define USB_DDK_DT_ENCRYPTION_TYPE 0x0E
314 #define USB_DDK_DT_BOS 0x0F
315 #define USB_DDK_DT_DEVICE_CAPABILITY 0x10
316 #define USB_DDK_DT_WIRELESS_ENDPOINT_COMP 0x11
317 #define USB_DDK_DT_WIRE_ADAPTER 0x21
318 #define USB_DDK_DT_RPIPE 0x22
319 #define USB_DDK_DT_CS_RADIO_CONTROL 0x23
320 #define USB_DDK_DT_PIPE_USAGE 0x24
321 #define USB_DDK_DT_SS_ENDPOINT_COMP 0x30
322 #define USB_DDK_DT_SSP_ISOC_ENDPOINT_COMP 0x31
323 #define USB_DDK_DT_CS_DEVICE (USB_DDK_TYPE_CLASS | USB_DDK_DT_DEVICE)
324 #define USB_DDK_DT_CS_CONFIG (USB_DDK_TYPE_CLASS | USB_DDK_DT_CONFIG)
325 #define USB_DDK_DT_CS_STRING (USB_DDK_TYPE_CLASS | USB_DDK_DT_STRING)
326 #define USB_DDK_DT_CS_INTERFACE (USB_DDK_TYPE_CLASS | USB_DDK_DT_INTERFACE)
327 #define USB_DDK_DT_CS_ENDPOINT (USB_DDK_TYPE_CLASS | USB_DDK_DT_ENDPOINT)
328
329 #define USB_DDK_DT_SS_EP_COMP_SIZE 0x06
330 #define USB_DDK_DT_ENDPOINT_SIZE 0x07
331 #define USB_DDK_DT_SSP_ISOC_EP_COMP_SIZE 0x08
332 #define USB_DDK_DT_INTERFACE_ASSOCIATION_SIZE 0x08
333 #define USB_DDK_DT_CONFIG_SIZE 0x09
334 #define USB_DDK_DT_INTERFACE_SIZE 0x09
335 #define USB_DDK_DT_ENDPOINT_AUDIO_SIZE 0x09
336 #define USB_DDK_DT_DEVICE_SIZE 0x12
337
338 #define USB_DDK_CLASS_PER_INTERFACE 0x00
339 #define USB_DDK_CLASS_AUDIO 0x01
340 #define USB_DDK_CLASS_COMM 0x02
341 #define USB_DDK_CLASS_HID 0x03
342 #define USB_DDK_CLASS_PHYSICAL 0x05
343 #define USB_DDK_CLASS_STILL_IMAGE 0x06
344 #define USB_DDK_CLASS_PRINTER 0x07
345 #define USB_DDK_CLASS_MASS_STORAGE 0x08
346 #define USB_DDK_CLASS_HUB 0x09
347 #define USB_DDK_CLASS_CDC_DATA 0x0A
348 #define USB_DDK_CLASS_CSCID 0x0B
349 #define USB_DDK_CLASS_CONTENT_SEC 0x0D
350 #define USB_DDK_CLASS_VIDEO 0x0E
351 #define USB_DDK_CLASS_WIRELESS_CONTROLLER 0xE0
352 #define USB_DDK_CLASS_MISC 0xEF
353 #define USB_DDK_CLASS_APP_SPEC 0xFE
354 #define USB_DDK_CLASS_VENDOR_SPEC 0xFF
355 #define USB_DDK_SUBCLASS_VENDOR_SPEC 0xFF
356
357 #define USB_DDK_CDC_NOTIFY_NETWORK_CONNECTION 0x00
358 #define USB_DDK_CDC_NOTIFY_RESPONSE_AVAILABLE 0x01
359 #define USB_DDK_CDC_NOTIFY_SERIAL_STATE 0x20
360 #define USB_DDK_CDC_NOTIFY_SPEED_CHANGE 0x2A
361
362 #define USB_DDK_CDC_SEND_ENCAPSULATED_COMMAND 0x00
363 #define USB_DDK_CDC_GET_ENCAPSULATED_RESPONSE 0x01
364 #define USB_DDK_CDC_REQ_SET_LINE_CODING 0x20
365 #define USB_DDK_CDC_REQ_GET_LINE_CODING 0x21
366 #define USB_DDK_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
367 #define USB_DDK_CDC_REQ_SEND_BREAK 0x23
368 #define USB_DDK_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
369 #define USB_DDK_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
370 #define USB_DDK_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
371 #define USB_DDK_CDC_SET_ETHERNET_PACKET_FILTER 0x43
372 #define USB_DDK_CDC_GET_ETHERNET_STATISTIC 0x44
373 #define USB_DDK_CDC_GET_NTB_PARAMETERS 0x80
374 #define USB_DDK_CDC_GET_NET_ADDRESS 0x81
375 #define USB_DDK_CDC_SET_NET_ADDRESS 0x82
376 #define USB_DDK_CDC_GET_NTB_FORMAT 0x83
377 #define USB_DDK_CDC_SET_NTB_FORMAT 0x84
378 #define USB_DDK_CDC_GET_NTB_INPUT_SIZE 0x85
379 #define USB_DDK_CDC_SET_NTB_INPUT_SIZE 0x86
380 #define USB_DDK_CDC_GET_MAX_DATAGRAM_SIZE 0x87
381 #define USB_DDK_CDC_SET_MAX_DATAGRAM_SIZE 0x88
382 #define USB_DDK_CDC_GET_CRC_MODE 0x89
383 #define USB_DDK_CDC_SET_CRC_MODE 0x8A
384
385 #define USB_DDK_CDC_COMM_FEATURE 0x01
386 #define USB_DDK_CDC_CAP_LINE 0x02
387 #define USB_DDK_CDC_CAP_BRK 0x04
388 #define USB_DDK_CDC_CAP_NOTIFY 0x08
389
390 #define USB_DDK_CDC_HEADER_TYPE 0x00
391 #define USB_DDK_CDC_CALL_MANAGEMENT_TYPE 0x01
392 #define USB_DDK_CDC_ACM_TYPE 0x02
393 #define USB_DDK_CDC_UNION_TYPE 0x06
394 #define USB_DDK_CDC_COUNTRY_TYPE 0x07
395 #define USB_DDK_CDC_NETWORK_TERMINAL_TYPE 0x0a
396 #define USB_DDK_CDC_ETHERNET_TYPE 0x0f
397 #define USB_DDK_CDC_WHCM_TYPE 0x11
398 #define USB_DDK_CDC_MDLM_TYPE 0x12
399 #define USB_DDK_CDC_MDLM_DETAIL_TYPE 0x13
400 #define USB_DDK_CDC_DMM_TYPE 0x14
401 #define USB_DDK_CDC_OBEX_TYPE 0x15
402 #define USB_DDK_CDC_NCM_TYPE 0x1A
403 #define USB_DDK_CDC_MBIM_TYPE 0x1B
404 #define USB_DDK_CDC_MBIM_EXTENDED_TYPE 0x1C
405
406 #define USB_DDK_CDC_PROTO_NONE 0x00
407 #define USB_DDK_CDC_ACM_PROTO_AT_V25TER 0x01
408 #define USB_DDK_CDC_ACM_PROTO_AT_PCCA101 0x02
409 #define USB_DDK_CDC_ACM_PROTO_AT_PCCA101_WAKE 0x03
410 #define USB_DDK_CDC_ACM_PROTO_AT_GSM 0x04
411 #define USB_DDK_CDC_ACM_PROTO_AT_3G 0x05
412 #define USB_DDK_CDC_ACM_PROTO_AT_CDMA 0x06
413 #define USB_DDK_CDC_PROTO_EEM 0x07
414
415 #define USB_DDK_CDC_NCM_PROTO_NTB 0x01
416 #define USB_DDK_CDC_MBIM_PROTO_NTB 0x02
417
418 #define USB_DDK_CDC_SUBCLASS_ACM 0x02
419 #define USB_DDK_CDC_SUBCLASS_ETHERNET 0x06
420 #define USB_DDK_CDC_SUBCLASS_WHCM 0x08
421 #define USB_DDK_CDC_SUBCLASS_DMM 0x09
422 #define USB_DDK_CDC_SUBCLASS_MDLM 0x0A
423 #define USB_DDK_CDC_SUBCLASS_OBEX 0x0B
424 #define USB_DDK_CDC_SUBCLASS_EEM 0x0C
425 #define USB_DDK_CDC_SUBCLASS_NCM 0x0D
426 #define USB_DDK_CDC_SUBCLASS_MBIM 0x0E
427
428 #define USB_DDK_CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
429 #define USB_DDK_CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1)
430 #define USB_DDK_CDC_PACKET_TYPE_DIRECTED (1 << 2)
431 #define USB_DDK_CDC_PACKET_TYPE_BROADCAST (1 << 3)
432 #define USB_DDK_CDC_PACKET_TYPE_MULTICAST (1 << 4)
433
434 #define USB_DDK_CDC_ACM_PROTO_VENDOR 0xFF
435
436 #define USB_DDK_TYPE_MASK (0x03 << 5)
437 #define USB_DDK_TYPE_STANDARD (0x00 << 5)
438 #define USB_DDK_TYPE_CLASS (0x01 << 5)
439 #define USB_DDK_TYPE_VENDOR (0x02 << 5)
440 #define USB_DDK_TYPE_RESERVED (0x03 << 5)
441
442 #define USB_DDK_ENDPOINT_XFERTYPE_MASK 0x03
443 #define USB_DDK_ENDPOINT_XFER_CONTROL 0x00
444 #define USB_DDK_ENDPOINT_XFER_ISOC 0x01
445 #define USB_DDK_ENDPOINT_XFER_BULK 0x02
446 #define USB_DDK_ENDPOINT_XFER_INT 0x03
447 #define USB_DDK_ENDPOINT_MAX_ADJUSTABLE 0x80
448
449 #define USB_DDK_RECIP_MASK 0x1F
450 #define USB_DDK_RECIP_DEVICE 0x00
451 #define USB_DDK_RECIP_INTERFACE 0x01
452 #define USB_DDK_RECIP_ENDPOINT 0x02
453 #define USB_DDK_RECIP_OTHER 0x03
454
455 #define USB_DDK_REQ_GET_STATUS 0x00
456 #define USB_DDK_REQ_CLEAR_FEATURE 0x01
457 #define USB_DDK_REQ_SET_FEATURE 0x03
458 #define USB_DDK_REQ_SET_ADDRESS 0x05
459 #define USB_DDK_REQ_GET_DESCRIPTOR 0x06
460 #define USB_DDK_REQ_SET_DESCRIPTOR 0x07
461 #define USB_DDK_REQ_GET_CONFIGURATION 0x08
462 #define USB_DDK_REQ_SET_CONFIGURATION 0x09
463 #define USB_DDK_REQ_GET_INTERFACE 0x0A
464 #define USB_DDK_REQ_SET_INTERFACE 0x0B
465 #define USB_DDK_REQ_SYNCH_FRAME 0x0C
466 #define USB_DDK_REQ_SET_SEL 0x30
467 #define USB_DDK_REQ_SET_ISOCH_DELAY 0x31
468
469 #define UsbiDescriptorHeader UsbDescriptorHeader
470 #define UsbiConfigurationDescriptor UsbConfigDescriptor
471 #define UsbiInterfaceDescriptor UsbInterfaceDescriptor
472
UsbEndpointDirIn(uint8_t ep)473 static inline int UsbEndpointDirIn(uint8_t ep)
474 {
475 return ((ep & USB_DDK_ENDPOINT_DIR_MASK) == USB_DDK_DIR_IN);
476 }
477
UsbEndpointDirOut(uint8_t ep)478 static inline int UsbEndpointDirOut(uint8_t ep)
479 {
480 return ((ep & USB_DDK_ENDPOINT_DIR_MASK) == USB_DDK_DIR_OUT);
481 }
482
483 #endif /* USB_DDK_H */
484