⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.h

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 H
📖 第 1 页 / 共 3 页
字号:
#define USB_PID_SOF                            0xa5
#define USB_PID_NYET                           0x96	/* USB 2.0 */
#define USB_PID_DATA2                          0x87	/* USB 2.0 */
#define USB_PID_SPLIT                          0x78	/* USB 2.0 */
#define USB_PID_IN                             0x69
#define USB_PID_NAK                            0x5a
#define USB_PID_DATA1                          0x4b
#define USB_PID_PREAMBLE                       0x3c	/* Token mode */
#define USB_PID_ERR                            0x3c	/* USB 2.0: handshake mode */
#define USB_PID_SETUP                          0x2d
#define USB_PID_STALL                          0x1e
#define USB_PID_MDATA                          0x0f	/* USB 2.0 */

/*
 * Standard requests
 */
#define USB_REQ_GET_STATUS			0x00
#define USB_REQ_CLEAR_FEATURE		0x01
#define USB_REQ_SET_FEATURE			0x03
#define USB_REQ_SET_ADDRESS			0x05
#define USB_REQ_GET_DESCRIPTOR		0x06
#define USB_REQ_SET_DESCRIPTOR		0x07
#define USB_REQ_GET_CONFIGURATION	0x08
#define USB_REQ_SET_CONFIGURATION	0x09
#define USB_REQ_GET_INTERFACE		0x0A
#define USB_REQ_SET_INTERFACE		0x0B
#define USB_REQ_SYNCH_FRAME			0x0C

/*
 * HID requests
 */
#define USB_REQ_GET_REPORT			0x01
#define USB_REQ_GET_IDLE			0x02
#define USB_REQ_GET_PROTOCOL		0x03
#define USB_REQ_SET_REPORT			0x09
#define USB_REQ_SET_IDLE			0x0A
#define USB_REQ_SET_PROTOCOL		0x0B

// HUB request
#define	HUB_REQ_GET_STATE			0x02

// usb2.0 hub
#define HUB_REQ_CLEAR_TT_BUFFER		0x08


typedef LONG USBD_STATUS;

//
// USBD status codes
//
//  Status values are 32 bit values layed out as follows:
//
//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
//  +---+---------------------------+-------------------------------+
//  | S |               Status Code                                 |
//  +---+---------------------------+-------------------------------+
//
//  where
//
//      S - is the state code
//
//          00 - completed with success
//          01 - request is pending
//          10 - completed with error, endpoint not stalled
//          11 - completed with error, endpoint stalled
//
//
//      Code - is the status code
//

//
// Generic test for success on any status value (non-negative numbers
// indicate success).
//

#define usb_success(Status) ((USBD_STATUS)(Status) >= 0)

//
// Generic test for pending status value.
//

#define usb_pending(Status) ((ULONG)(Status) >> 30 == 1)

//
// Generic test for error on any status value.
//

#define usb_error(Status) ((USBD_STATUS)(Status) < 0)

//
// Generic test for stall on any status value.
//

#define usb_halted(Status) ((ULONG)(Status) >> 30 == 3)

//
// Macro to check the status code only
//

#define usb_status(Status) ((ULONG)(Status) & 0x0FFFFFFFL)


#define USB_STATUS_SUCCESS                  ((USBD_STATUS)0x00000000L)
#define USB_STATUS_PENDING                  ((USBD_STATUS)0x40000000L)
#define USB_STATUS_HALTED                   ((USBD_STATUS)0xC0000000L)
#define USB_STATUS_ERROR                    ((USBD_STATUS)0x80000000L)

//
// HC status codes
// Note: these status codes have both the error and the stall bit set.
//
#define USB_STATUS_CRC                      ((USBD_STATUS)0xC0000401L)
#define USB_STATUS_BTSTUFF                  ((USBD_STATUS)0xC0000402L)
#define USB_STATUS_DATA_TOGGLE_MISMATCH     ((USBD_STATUS)0xC0000403L)
#define USB_STATUS_STALL_PID                ((USBD_STATUS)0xC0000404L)
#define USB_STATUS_DEV_NOT_RESPONDING       ((USBD_STATUS)0xC0000405L)
#define USB_STATUS_PID_CHECK_FAILURE        ((USBD_STATUS)0xC0000406L)
#define USB_STATUS_UNEXPECTED_PID           ((USBD_STATUS)0xC0000407L)
#define USB_STATUS_DATA_OVERRUN             ((USBD_STATUS)0xC0000408L)
#define USB_STATUS_DATA_UNDERRUN            ((USBD_STATUS)0xC0000409L)
#define USB_STATUS_RESERVED1                ((USBD_STATUS)0xC000040AL)
#define USB_STATUS_RESERVED2                ((USBD_STATUS)0xC000040BL)
#define USB_STATUS_BUFFER_OVERRUN           ((USBD_STATUS)0xC000040CL)
#define USB_STATUS_BUFFER_UNDERRUN          ((USBD_STATUS)0xC000040DL)
#define USB_STATUS_NOT_ACCESSED             ((USBD_STATUS)0xC000040FL)
#define USB_STATUS_FIFO                     ((USBD_STATUS)0xC0000410L)
#define USB_STATUS_BABBLE_DETECTED			((USBD_STATUS)0xC0000408L)

//
// returned by HCD if a transfer is submitted to an endpoint that is 
// stalled
//
#define USB_STATUS_ENDPOINT_HALTED         ((USBD_STATUS)0xC0000430L)

//
// Software status codes
// Note: the following status codes have only the error bit set
//
#define USB_STATUS_NO_MEMORY                ((USBD_STATUS)0x80000100L)
#define USB_STATUS_INVALID_URB_FUNCTION     ((USBD_STATUS)0x80000200L)
#define USB_STATUS_INVALID_PARAMETER        ((USBD_STATUS)0x80000300L)

//
// returned if client driver attempts to close an endpoint/interface
// or configuration with outstanding transfers.
//
#define USB_STATUS_ERROR_BUSY               ((USBD_STATUS)0x80000400L)
//
// returned by USBD if it cannot complete a URB request, typically this 
// will be returned in the URB status field when the Irp is completed
// with a more specific NT error code in the irp.status field.
//
#define USB_STATUS_REQUEST_FAILED           ((USBD_STATUS)0x80000500L)

#define USB_STATUS_INVALID_PIPE_HANDLE      ((USBD_STATUS)0x80000600L)

// returned when there is not enough bandwidth avialable
// to open a requested endpoint
#define USB_STATUS_NO_BANDWIDTH             ((USBD_STATUS)0x80000700L)
//
// generic HC error
// 
#define USB_STATUS_INTERNAL_HC_ERROR        ((USBD_STATUS)0x80000800L)
//
// returned when a short packet terminates the transfer
// ie USBD_SHORT_TRANSFER_OK bit not set
// 
#define USB_STATUS_ERROR_SHORT_TRANSFER     ((USBD_STATUS)0x80000900L)
// 
// returned if the requested start frame is not within
// USBD_ISO_START_FRAME_RANGE of the current USB frame, 
// note that the stall bit is set
// 
#define USB_STATUS_BAD_START_FRAME          ((USBD_STATUS)0xC0000A00L)
//
// returned by HCD if all packets in an iso transfer complete with an error 
//
#define USB_STATUS_ISOCH_REQUEST_FAILED     ((USBD_STATUS)0xC0000B00L)
//
// returned by USBD if the frame length control for a given 
// HC is already taken by anothe driver
//
#define USB_STATUS_FRAME_CONTROL_OWNED      ((USBD_STATUS)0xC0000C00L)
//
// returned by USBD if the caller does not own frame length control and
// attempts to release or modify the HC frame length
//
#define USB_STATUS_FRAME_CONTROL_NOT_OWNED  ((USBD_STATUS)0xC0000D00L)

//
// set when a transfers is completed due to an AbortPipe request from
// the client driver
//
// Note: no error or stall bit is set for these status codes
//
#define USB_STATUS_CANCELED                 ((USBD_STATUS)0x00010000L)

#define USB_STATUS_CANCELING                ((USBD_STATUS)0x00020000L)

// Device type           -- in the "User Defined" range."
#define FILE_HCD_DEV_TYPE	45000
#define FILE_UHCI_DEV_TYPE 	( FILE_HCD_DEV_TYPE + 1 )
#define FILE_OHCI_DEV_TYPE 	( FILE_HCD_DEV_TYPE + 2 )
#define FILE_EHCI_DEV_TYPE	( FILE_HCD_DEV_TYPE + 3 )
#define FILE_USB_DEV_TYPE	( FILE_HCD_DEV_TYPE + 8 )

#define IOCTL_GET_DEV_COUNT		CTL_CODE( FILE_HCD_DEV_TYPE, 4093, METHOD_BUFFERED, FILE_ANY_ACCESS )
//input_buffer and input_buffer_length is zero, output_buffer is to receive a dword value of the
//dev count, output_buffer_length must be no less than sizeof( unsigned long ).

#define IOCTL_ENUM_DEVICES 		CTL_CODE( FILE_HCD_DEV_TYPE, 4094, METHOD_BUFFERED, FILE_ANY_ACCESS )
//input_buffer is a dword value to indicate the count of elements in the array 
//input_buffer_length is sizeof( unsigned long ), output_buffer is to receive a
//structure ENUM_DEV_ARRAY where dev_count is the elements hold in this array.

#define IOCTL_GET_DEV_DESC		CTL_CODE( FILE_HCD_DEV_TYPE, 4095, METHOD_BUFFERED, FILE_ANY_ACCESS )
//input_buffer is a structure GET_DEV_DESC_REQ, and the input_buffer_length is
//no less than sizeof( input_buffer ), output_buffer is a buffer to receive the
//requested dev's desc, and output_buffer_length specifies the length of the
//buffer

#define IOCTL_SUBMIT_URB_RD		CTL_CODE( FILE_HCD_DEV_TYPE, 4096, METHOD_IN_DIRECT, FILE_ANY_ACCESS )
#define IOCTL_SUBMIT_URB_WR 	CTL_CODE( FILE_HCD_DEV_TYPE, 4097, METHOD_OUT_DIRECT, FILE_ANY_ACCESS )
// if the major_function is IRP_MJ_DEVICE_CONTROL
// input_buffer is a URB, and input_buffer_length is equal to or greater than 
// sizeof( URB ); the output_buffer is a buffer to receive data from or send data
// to device. only the following urb fields can be accessed, others must be zeroed.
//  DEV_HANDLE 			endp_handle;
//	UCHAR             	setup_packet[8];   	// for control pipe
// the choosing of IOCTL_SUBMIT_URB_RD or IOCTL_SUBMIT_URB_WR should be determined
// by the current URB, for example, a request string from device will use XXX_RD, 
// and a write to the bulk endpoint will use XXX_WR
// if the major_function is IRP_MJ_INTERNAL_DEVICE_CONTROL
// input_buffer is a URB, and input_buffer_length is equal to or greater than 
// sizeof( URB ); 
// only the following urb fields can be accessed, others must be zeroed.
//  DEV_HANDLE			endp_handle;
//	UCHAR             	setup_packet[8];   	// for control pipe, or zeroed
//	PUCHAR				data_buffer;		// buffer for READ/WRITE
//	ULONG				data_length;		// buffer size in bytes

#define IOCTL_SUBMIT_URB_NOIO	CTL_CODE( FILE_HCD_DEV_TYPE, 4098, METHOD_BUFFERED,	FILE_ANY_ACCESS )
// input_buffer is a URB, and input_buffer_length is equal to or greater than 
// sizeof( URB ); the output_buffer is null and no output_buffer_length,
// only the following fields in urb can be accessed, others must be zeroed.
//  DEV_HANDLE 			endp_handle;
//	UCHAR             	setup_packet[8];   	//for control pipe
//	there is no difference between IRP_MJ_DEVICE_CONTROL and IRP_MJ_INTERNAL_DEVICE_CONTROL
#define IOCTL_GET_DEV_HANDLE 		CTL_CODE( FILE_HCD_DEV_TYPE, 4099, METHOD_BUFFERED, FILE_ANY_ACCESS )
// input_buffer is null ,and input_buffer_length is zero.
// output_buffer will hold the handle to this dev, output_buffer_length is 4
// or bigger

typedef ULONG 	DEV_HANDLE, ENDP_HANDLE, IF_HANDLE;

struct URB;
#pragma pack( push, usb_align, 1 )

//structures for DeviceIoControl
typedef struct _ENUM_DEV_ELEMENT
{
	DEV_HANDLE 	dev_handle;
	USHORT		product_id;
	USHORT		vendor_id;
	UCHAR		dev_addr;
	
} ENUM_DEV_ELEMENT, *PENUM_DEV_ELEMENT;

typedef struct _ENUM_DEV_ARRAY
{
	UCHAR				dev_count;
	ENUM_DEV_ELEMENT 	dev_arr[ 1 ];

} ENUM_DEV_ARRAY, *PENUM_DEV_ARRAY;

typedef struct _GET_DEV_DESC_REQ
{
	DEV_HANDLE dev_handle;
	UCHAR	desc_type;
	UCHAR	desc_idx;

} GET_DEV_DESC_REQ, *PGET_DEV_DESC_REQ;

//usb definitions
typedef struct _USB_CTRL_SETUP_PACKET
{
	UCHAR 				bmRequestType;
	UCHAR 				bRequest;
	USHORT 				wValue;
	USHORT			 	wIndex;
	USHORT 				wLength;

}USB_CTRL_SETUP_PACKET, *PUSB_CTRL_SETUP_PACKET;

typedef struct _USB_STRING_DESCRIPTOR
{
    UCHAR               bLength;
    UCHAR               bDescriptorType;
    USHORT              wData[1];
    
} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;

typedef struct _USB_DESC_HEADER
{
    UCHAR               bLength;
    UCHAR               bDescriptorType;

} USB_DESC_HEADER, *PUSB_DESC_HEADER;

typedef struct _USB_ENDPOINT_DESC
{
    UCHAR                bLength;
    UCHAR                bDescriptorType;
    UCHAR                bEndpointAddress;
    UCHAR                bmAttributes;
    USHORT               wMaxPacketSize;
    UCHAR                bInterval;

} USB_ENDPOINT_DESC, *PUSB_ENDPOINT_DESC;
                          
typedef struct _USB_INTERFACE_DESC
{
    UCHAR               bLength;
    UCHAR               bDescriptorType;
    UCHAR               bInterfaceNumber;
    UCHAR               bAlternateSetting;
    UCHAR               bNumEndpoints;
    UCHAR               bInterfaceClass;
    UCHAR               bInterfaceSubClass;
    UCHAR               bInterfaceProtocol;
    UCHAR               iInterface;

} USB_INTERFACE_DESC, *PUSB_INTERFACE_DESC;

typedef struct _USB_CONFIGURATION_DESC
{
    UCHAR               bLength;
    UCHAR               bDescriptorType;
    USHORT              wTotalLength;
    UCHAR               bNumInterfaces;
    UCHAR               bConfigurationValue;
    UCHAR               iConfiguration;
    UCHAR               bmAttributes;
    UCHAR               MaxPower;

} USB_CONFIGURATION_DESC, *PUSB_CONFIGURATION_DESC;

typedef struct _USB_DEVICE_DESC
{
    UCHAR               bLength;
    UCHAR               bDescriptorType;
    USHORT              bcdUSB;
    UCHAR               bDeviceClass;
    UCHAR               bDeviceSubClass;
    UCHAR               bDeviceProtocol;
    UCHAR               bMaxPacketSize0;
    USHORT              idVendor;
    USHORT              idProduct;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -