RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
usb_common.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2012-10-01 Yi Qiu first version
9 * 2013-04-26 aozima add DEVICEQUALIFIER support.
10 * 2017-11-15 ZYH fix ep0 transform error
11 */
12
13#ifndef __USB_COMMON_H__
14#define __USB_COMMON_H__
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20#include <rtthread.h>
21
22#define RT_DEBUG_USB 0x00
23#define USB_DYNAMIC 0x00
24
25#define USB_CLASS_DEVICE 0x00
26#define USB_CLASS_AUDIO 0x01
27#define USB_CLASS_CDC 0x02
28#define USB_CLASS_HID 0x03
29#define USB_CLASS_PHYSICAL 0x05
30#define USB_CLASS_IMAGE 0x06
31#define USB_CLASS_PRINTER 0x07
32#define USB_CLASS_MASS_STORAGE 0x08
33#define USB_CLASS_HUB 0x09
34#define USB_CLASS_CDC_DATA 0x0a
35#define USB_CLASS_SMART_CARD 0x0b
36#define USB_CLASS_SECURITY 0x0d
37#define USB_CLASS_VIDEO 0x0e
38#define USB_CLASS_HEALTHCARE 0x0f
39#define USB_CLASS_DIAG_DEVICE 0xdc
40#define USB_CLASS_WIRELESS 0xe0
41#define USB_CLASS_MISC 0xef
42#define USB_CLASS_APP_SPECIFIC 0xfe
43#define USB_CLASS_VEND_SPECIFIC 0xff
44
45#define USB_DESC_TYPE_DEVICE 0x01
46#define USB_DESC_TYPE_CONFIGURATION 0x02
47#define USB_DESC_TYPE_STRING 0x03
48#define USB_DESC_TYPE_INTERFACE 0x04
49#define USB_DESC_TYPE_ENDPOINT 0x05
50#define USB_DESC_TYPE_DEVICEQUALIFIER 0x06
51#define USB_DESC_TYPE_OTHERSPEED 0x07
52#define USB_DESC_TYPE_IAD 0x0b
53#define USB_DESC_TYPE_HID 0x21
54#define USB_DESC_TYPE_REPORT 0x22
55#define USB_DESC_TYPE_PHYSICAL 0x23
56#define USB_DESC_TYPE_HUB 0x29
57
58#define USB_DESC_LENGTH_DEVICE 0x12
59#define USB_DESC_LENGTH_CONFIG 0x9
60#define USB_DESC_LENGTH_IAD 0x8
61#define USB_DESC_LENGTH_STRING 0x4
62#define USB_DESC_LENGTH_INTERFACE 0x9
63#define USB_DESC_LENGTH_ENDPOINT 0x7
64
65#define USB_REQ_TYPE_STANDARD 0x00
66#define USB_REQ_TYPE_CLASS 0x20
67#define USB_REQ_TYPE_VENDOR 0x40
68#define USB_REQ_TYPE_MASK 0x60
69
70#define USB_REQ_TYPE_DIR_OUT 0x00
71#define USB_REQ_TYPE_DIR_IN 0x80
72
73#define USB_REQ_TYPE_DEVICE 0x00
74#define USB_REQ_TYPE_INTERFACE 0x01
75#define USB_REQ_TYPE_ENDPOINT 0x02
76#define USB_REQ_TYPE_OTHER 0x03
77#define USB_REQ_TYPE_RECIPIENT_MASK 0x1f
78
79#define USB_FEATURE_ENDPOINT_HALT 0x00
80#define USB_FEATURE_DEV_REMOTE_WAKEUP 0x01
81#define USB_FEATURE_TEST_MODE 0x02
82
83#define USB_REQ_GET_STATUS 0x00
84#define USB_REQ_CLEAR_FEATURE 0x01
85#define USB_REQ_SET_FEATURE 0x03
86#define USB_REQ_SET_ADDRESS 0x05
87#define USB_REQ_GET_DESCRIPTOR 0x06
88#define USB_REQ_SET_DESCRIPTOR 0x07
89#define USB_REQ_GET_CONFIGURATION 0x08
90#define USB_REQ_SET_CONFIGURATION 0x09
91#define USB_REQ_GET_INTERFACE 0x0A
92#define USB_REQ_SET_INTERFACE 0x0B
93#define USB_REQ_SYNCH_FRAME 0x0C
94#define USB_REQ_SET_ENCRYPTION 0x0D
95#define USB_REQ_GET_ENCRYPTION 0x0E
96#define USB_REQ_RPIPE_ABORT 0x0E
97#define USB_REQ_SET_HANDSHAKE 0x0F
98#define USB_REQ_RPIPE_RESET 0x0F
99#define USB_REQ_GET_HANDSHAKE 0x10
100#define USB_REQ_SET_CONNECTION 0x11
101#define USB_REQ_SET_SECURITY_DATA 0x12
102#define USB_REQ_GET_SECURITY_DATA 0x13
103#define USB_REQ_SET_WUSB_DATA 0x14
104#define USB_REQ_LOOPBACK_DATA_WRITE 0x15
105#define USB_REQ_LOOPBACK_DATA_READ 0x16
106#define USB_REQ_SET_INTERFACE_DS 0x17
107
108#define USB_STRING_LANGID_INDEX 0x00
109#define USB_STRING_MANU_INDEX 0x01
110#define USB_STRING_PRODUCT_INDEX 0x02
111#define USB_STRING_SERIAL_INDEX 0x03
112#define USB_STRING_CONFIG_INDEX 0x04
113#define USB_STRING_INTERFACE_INDEX 0x05
114#define USB_STRING_OS_INDEX 0x06
115#define USB_STRING_MAX 0xff
116
117#define USB_STRING_OS "MSFT100A"
118
119#define USB_PID_OUT 0x01
120#define USB_PID_ACK 0x02
121#define USB_PID_DATA0 0x03
122#define USB_PID_SOF 0x05
123#define USB_PID_IN 0x09
124#define USB_PID_NACK 0x0A
125#define USB_PID_DATA1 0x0B
126#define USB_PID_PRE 0x0C
127#define USB_PID_SETUP 0x0D
128#define USB_PID_STALL 0x0E
129
130#define USB_EP_DESC_OUT 0x00
131#define USB_EP_DESC_IN 0x80
132#define USB_EP_DESC_NUM_MASK 0x0f
133
134#define USB_EP_ATTR_CONTROL 0x00
135#define USB_EP_ATTR_ISOC 0x01
136#define USB_EP_ATTR_BULK 0x02
137#define USB_EP_ATTR_INT 0x03
138#define USB_EP_ATTR_TYPE_MASK 0x03
139
140#define USB_EPNO_MASK 0x7f
141#define USB_DIR_OUT 0x00
142#define USB_DIR_IN 0x80
143#define USB_DIR_INOUT 0x40
144#define USB_DIR_MASK 0x80
145
146#define ID_UNASSIGNED 0
147#define ID_ASSIGNED 1
148
149#define RH_GET_PORT_STATUS 0
150#define RH_SET_PORT_STATUS 1
151#define RH_CLEAR_PORT_FEATURE 2
152#define RH_SET_PORT_FEATURE 3
153
154#define USB_BUS_POWERED 0
155#define USB_SELF_POWERED 1
156#define USB_REMOTE_WAKEUP 1
157#define USB_EP_HALT 0
158
159/*
160 * Port feature numbers
161 */
162#define PORT_FEAT_CONNECTION 0
163#define PORT_FEAT_ENABLE 1
164#define PORT_FEAT_SUSPEND 2
165#define PORT_FEAT_OVER_CURRENT 3
166#define PORT_FEAT_RESET 4
167#define PORT_FEAT_POWER 8
168#define PORT_FEAT_LOWSPEED 9
169#define PORT_FEAT_HIGHSPEED 10
170#define PORT_FEAT_C_CONNECTION 16
171#define PORT_FEAT_C_ENABLE 17
172#define PORT_FEAT_C_SUSPEND 18
173#define PORT_FEAT_C_OVER_CURRENT 19
174#define PORT_FEAT_C_RESET 20
175
176/*
177 The HcRhPortStatus[1:NDP] register is used to control and report port events on a per-port
178 basis. NumberDownstreamPorts represents the number of HcRhPortStatus registers that are
179 implemented in hardware. The lower word is used to reflect the port status, whereas the upper
180 word reflects the status change bits. Some status bits are implemented with special write behavior
181 (see below). If a transaction (token through handshake) is in progress when a write to change
182 port status occurs, the resulting port status change must be postponed until the transaction
183 completes. Reserved bits should always be written '0'.
184*/
185#define PORT_CCS 0x00000001UL /* R:CurrentConnectStatus - W:ClearPortEnable */
186#define PORT_PES 0x00000002UL /* R:PortEnableStatus - W:SetPortEnable */
187#define PORT_PSS 0x00000004UL /* R:PortSuspendStatus - W:SetPortSuspend */
188#define PORT_POCI 0x00000008UL /* R:PortOverCurrentIndicator - W:ClearSuspendStatus */
189#define PORT_PRS 0x00000010UL /* R:PortResetStatus - W: SetPortReset */
190#define PORT_PPS 0x00000100UL /* R:PortPowerStatus - W: SetPortPower */
191#define PORT_LSDA 0x00000200UL /* R:LowSpeedDeviceAttached - W:ClearPortPower */
192#define PORT_CCSC 0x00010000UL
193#define PORT_PESC 0x00020000UL
194#define PORT_PSSC 0x00040000UL
195#define PORT_POCIC 0x00080000UL
196#define PORT_PRSC 0x00100000UL
197
198/*
199 *Hub Status & Hub Change bit masks
200 */
201#define HUB_STATUS_LOCAL_POWER 0x0001
202#define HUB_STATUS_OVERCURRENT 0x0002
203
204#define HUB_CHANGE_LOCAL_POWER 0x0001
205#define HUB_CHANGE_OVERCURRENT 0x0002
206
207#define USB_EP_ATTR(attr) (attr & USB_EP_ATTR_TYPE_MASK)
208#define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
209#define USB_EP_DIR(addr) ((addr & USB_DIR_MASK)>>7)
210
211#define HID_REPORT_ID_KEYBOARD1 1
212#define HID_REPORT_ID_KEYBOARD2 2
213#define HID_REPORT_ID_KEYBOARD3 3
214#define HID_REPORT_ID_KEYBOARD4 7
215#define HID_REPORT_ID_MEDIA 4
216#define HID_REPORT_ID_GENERAL 5
217#define HID_REPORT_ID_MOUSE 6
218
219/*
220 * Time of usb timeout
221 */
222#ifndef USB_TIMEOUT_BASIC
223#define USB_TIMEOUT_BASIC (RT_TICK_PER_SECOND) /* 1s */
224#endif
225#ifndef USB_TIMEOUT_LONG
226#define USB_TIMEOUT_LONG (RT_TICK_PER_SECOND * 5) /* 5s */
227#endif
228#ifndef USB_DEBOUNCE_TIME
229#define USB_DEBOUNCE_TIME (RT_TICK_PER_SECOND / 5) /* 0.2s */
230#endif
231
232#define uswap_32(x) \
233 ((((x) & 0xff000000) >> 24) | \
234 (((x) & 0x00ff0000) >> 8) | \
235 (((x) & 0x0000ff00) << 8) | \
236 (((x) & 0x000000ff) << 24))
237
238#define uswap_8(x) \
239 (((rt_uint16_t)(*((rt_uint8_t *)(x)))) + \
240 (((rt_uint16_t)(*(((rt_uint8_t *)(x)) + 1))) << 8))
241
242typedef void (*func_callback)(void *context);
255
265
266#pragma pack(1)
267
273typedef struct usb_descriptor* udesc_t;
274
293
307
321
322/* Interface Association Descriptor (IAD) */
335
346
354
382
392
411
412// Value Description
413// 1 A NULL-terminated Unicode String (REG_SZ)
414// 2 A NULL-terminated Unicode String that includes environment variables (REG_EXPAND_SZ)
415// 3 Free-form binary (REG_BINARY)
416// 4 A little-endian 32-bit integer (REG_DWORD_LITTLE_ENDIAN)
417// 5 A big-endian 32-bit integer (REG_DWORD_BIG_ENDIAN)
418// 6 A NULL-terminated Unicode string that contains a symbolic link (REG_LINK)
419// 7 Multiple NULL-terminated Unicode strings (REG_MULTI_SZ)
420#define USB_OS_PROPERTY_TYPE_REG_SZ 0x01UL
421#define USB_OS_PROPERTY_TYPE_REG_EXPAND_SZ 0x02UL
422#define USB_OS_PROPERTY_TYPE_REG_BINARY 0x03UL
423#define USB_OS_PROPERTY_TYPE_REG_DWORD_LITTLE_ENDIAN 0x04UL
424#define USB_OS_PROPERTY_TYPE_REG_DWORD_BIG_ENDIAN 0x05UL
425#define USB_OS_PROPERTY_TYPE_REG_LINK 0x06UL
426#define USB_OS_PROPERTY_TYPE_REG_MULTI_SZ 0x07UL
427
428#define USB_OS_PROPERTY_DESC(PropertyDataType,PropertyName,PropertyData) \
429{\
430 .dwSize = sizeof(struct usb_os_proerty)-sizeof(const char *)*2\
431 +sizeof(PropertyName)*2+sizeof(PropertyData)*2,\
432 .dwPropertyDataType = PropertyDataType,\
433 .wPropertyNameLength = sizeof(PropertyName)*2,\
434 .bPropertyName = PropertyName,\
435 .dwPropertyDataLength = sizeof(PropertyData)*2,\
436 .bPropertyData = PropertyData\
437}
438
439
440#ifndef HID_SUB_DESCRIPTOR_MAX
441#define HID_SUB_DESCRIPTOR_MAX 1
442#endif
443
458
465typedef struct hid_report* hid_report_t;
467
476typedef struct urequest* ureq_t;
477
478#ifndef MIN
479#define MIN(a, b) (a < b ? a : b)
480#endif
481#ifndef MAX
482#define MAX(a, b) (a > b ? a : b)
483#endif
484
485/*
486 * the define related to mass storage
487 */
488#define USBREQ_GET_MAX_LUN 0xfe
489#define USBREQ_MASS_STORAGE_RESET 0xff
490
491#define SIZEOF_CSW 0x0d
492#define SIZEOF_CBW 0x1f
493#define SIZEOF_INQUIRY_CMD 0x24
494#define SIZEOF_MODE_SENSE_6 0x4
495#define SIZEOF_READ_CAPACITIES 0xc
496#define SIZEOF_READ_CAPACITY 0x8
497#define SIZEOF_REQUEST_SENSE 0x12
498
499#define CBWFLAGS_DIR_M 0x80
500#define CBWFLAGS_DIR_IN 0x80
501#define CBWFLAGS_DIR_OUT 0x00
502
503#define SCSI_TEST_UNIT_READY 0x00
504#define SCSI_REQUEST_SENSE 0x03
505#define SCSI_INQUIRY_CMD 0x12
506#define SCSI_ALLOW_REMOVAL 0x1e
507#define SCSI_MODE_SENSE_6 0x1a
508#define SCSI_START_STOP 0x1b
509#define SCSI_READ_CAPACITIES 0x23
510#define SCSI_READ_CAPACITY 0x25
511#define SCSI_READ_10 0x28
512#define SCSI_WRITE_10 0x2a
513#define SCSI_VERIFY_10 0x2f
514
515#define CBW_SIGNATURE 0x43425355
516#define CSW_SIGNATURE 0x53425355
517#define CBW_TAG_VALUE 0x12345678
518
530
539
540#pragma pack()
541
548
559
560/*
561 * USB device event loop thread configurations
562 */
563/* the stack size of USB thread */
564#ifndef RT_USBD_THREAD_STACK_SZ
565#define RT_USBD_THREAD_STACK_SZ 512
566#endif
567
568/* the priority of USB thread */
569#ifndef RT_USBD_THREAD_PRIO
570#define RT_USBD_THREAD_PRIO 8
571#endif
572
573
574#ifdef __cplusplus
575}
576#endif
577
578#endif
unsigned char rt_uint8_t
unsigned short rt_uint16_t
struct rt_list_node rt_list_t
unsigned int rt_uint32_t
signed int rt_int32_t
rt_uint8_t report_id
rt_uint8_t size
rt_uint8_t report[63]
rt_uint8_t bConfigurationValue
rt_uint8_t data[2048]
rt_uint8_t bNumConfigurations
rt_uint8_t bNumDescriptors
struct uhid_descriptor::hid_descriptor_list Descriptor[HID_SUB_DESCRIPTOR_MAX]
rt_uint8_t bCountryCode
rt_uint8_t removable[8]
rt_uint16_t characteristics
rt_uint8_t pwron_to_good
rt_uint8_t pwr_ctl[8]
rt_uint8_t bInterfaceCount
rt_uint8_t bFunctionSubClass
rt_uint8_t bFirstInterface
rt_uint8_t bDescriptorType
rt_uint8_t bFunctionClass
rt_uint8_t bFunctionProtocol
rt_uint8_t request_type
rt_uint8_t bRequest
rt_uint16_t wLength
rt_uint16_t wIndex
rt_uint16_t wValue
struct usb_os_header_comp_id_descriptor head_desc
rt_uint32_t dwPropertyDataType
const char * bPropertyData
rt_uint32_t dwPropertyDataLength
const char * bPropertyName
rt_uint16_t wPropertyNameLength
rt_uint32_t xfer_len
rt_uint8_t cb_len
rt_uint32_t tag
rt_uint32_t signature
rt_uint8_t dflags
rt_uint8_t cb[16]
rt_uint32_t tag
rt_uint32_t signature
rt_uint8_t status
rt_int32_t data_reside
rt_uint8_t String[64]
void HID_Report_Received(hid_report_t report)
#define HID_SUB_DESCRIPTOR_MAX
struct uhub_descriptor * uhub_desc_t
struct uiad_descriptor * uiad_desc_t
struct usb_os_comp_id_descriptor * usb_os_comp_id_desc_t
struct usb_os_proerty * usb_os_proerty_t
struct uinterface_descriptor * uintf_desc_t
struct udevice_descriptor * udev_desc_t
struct ustorage_csw * ustorage_csw_t
struct usb_os_header_comp_id_descriptor * usb_os_header_desc_t
void(* func_callback)(void *context)
struct usb_os_header_comp_id_descriptor __attribute__
struct uendpoint_descriptor * uep_desc_t
struct uhid_descriptor * uhid_desc_t
struct urequest * ureq_t
struct uconfig_descriptor * ucfg_desc_t
struct ustorage_cbw * ustorage_cbw_t
struct usb_os_property_header * usb_os_property_header_t
uep0_stage_t
@ STAGE_IDLE
@ STAGE_DIN
@ STAGE_STATUS_OUT
@ STAGE_SETUP
@ STAGE_STATUS_IN
@ STAGE_DOUT
udevice_state_t
@ USB_STATE_POWERED
@ USB_STATE_NOTATTACHED
@ USB_STATE_ADDRESS
@ USB_STATE_SUSPENDED
@ USB_STATE_CONFIGURED
@ USB_STATE_ATTACHED
@ USB_STATE_DEFAULT
@ USB_STATE_UNAUTHENTICATED
@ USB_STATE_RECONNECTING
struct usb_os_function_comp_id_descriptor * usb_os_func_comp_id_desc_t
struct usb_descriptor * udesc_t
struct hid_report * hid_report_t
struct ustring_descriptor * ustr_desc_t