📄 usb.h
字号:
#ifndef __USBD_H__
#define __USBD_H__
/*
* Some USB bandwidth allocation constants.
*/
typedef unsigned long ULONG_PTR, *PULONG_PTR;
#define USB2_HOST_DELAY 5 /* nsec, guess */
#define BW_HOST_DELAY 1000L /* nanoseconds */
#define BW_HUB_LS_SETUP 333L /* nanoseconds */
/* 4 full-speed bit times (est.) */
#define FRAME_TIME_BITS 12000L /* frame = 1 millisecond */
#define FRAME_TIME_MAX_BITS_ALLOC (90L * FRAME_TIME_BITS / 100L)
#define FRAME_TIME_USECS 1000L
#define FRAME_TIME_MAX_USECS_ALLOC (90L * FRAME_TIME_USECS / 100L)
#define bit_time(bytecount) (7 * 8 * bytecount / 6) /* with integer truncation */
/* Trying not to use worst-case bit-stuffing
of (7/6 * 8 * bytecount) = 9.33 * bytecount */
/* bytecount = data payload byte count */
#define ns_to_us(ns) ((ns + 500L) / 1000L)
/* convert & round nanoseconds to microseconds */
#define usb_make_handle( dev_Id, if_iDx, endp_iDx) \
( ( DEV_HANDLE )( ( ( ( ( ULONG )dev_Id ) << 16 ) | ( ( ( ULONG )if_iDx ) << 8 ) ) | ( ( ULONG ) endp_iDx ) ) )
#define usb_make_ref( poinTER ) \
( poinTER ^ 0xffffffff )
#define ptr_from_ref uhci_make_ref
#define dev_id_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) >> 16 )
#define if_idx_from_handle( hanDLE ) ( ( ( ( ULONG ) ( hanDLE ) ) << 16 ) >> 24 )
#define endp_idx_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) & 0xff )
#define endp_from_handle( pDEV, hanDLE, peNDP ) \
{\
LONG if_idx, endp_idx;\
BOOLEAN def_endp; \
endp_idx = endp_idx_from_handle( hanDLE );\
if_idx = if_idx_from_handle( hanDLE );\
def_endp = ( ( hanDLE & 0xffff ) == 0xffff ); \
if( def_endp ) \
peNDP = &pdev->default_endp; \
else \
{ \
if( if_idx >= pdev->usb_config->if_count ) \
peNDP = NULL; \
else if( endp_idx >= pdev->usb_config->interf[ if_idx ].endp_count ) \
peNDP = NULL; \
else \
peNDP = &( pDEV )->usb_config->interf[ if_idx ].endp[ endp_idx ]; \
} \
}
#define endp_type( enDP ) \
( ( enDP->flags & USB_ENDP_FLAG_DEFAULT_ENDP ) \
? USB_ENDPOINT_XFER_CONTROL\
: ( ( enDP )->pusb_endp_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK ) )
//init work data for urb
#define urb_init( uRb ) \
{\
RtlZeroMemory( ( uRb ), sizeof( URB ) ); \
InitializeListHead( &( uRb )->trasac_list ); \
}
#define UsbBuildInterruptOrBulkTransferRequest(uRb, \
endp_hanDle, \
data_Buf, \
data_sIze, \
completIon, \
contExt, \
refereNce ) \
{ \
urb_init( ( uRb ) );\
( uRb )->endp_handle = endp_hanDle;\
( uRb )->data_buffer = data_Buf;\
( uRb )->data_length = data_sIze;\
( uRb )->completion = completIon;\
( uRb )->context = contExt; \
( uRb )->reference = refereNce; \
}
#define UsbBuildGetDescriptorRequest(uRb, \
endp_hAndle, \
descriPtorType, \
descriPtorIndex, \
languaGeId, \
data_bUffer, \
data_sIze, \
compleTion, \
contexT, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup;\
pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hAndle );\
( uRb )->data_length = ( data_sIze ); \
( uRb )->data_buffer = ( ( PUCHAR )data_bUffer ); \
( uRb )->completion = ( compleTion );\
( uRb )->context = ( ( PVOID )contexT ); \
( uRb )->reference = ( ULONG )refereNce; \
pseTup->wValue = ( ( descriPtorType ) << 8 )| ( descriPtorIndex ); \
pseTup->wLength = ( data_sIze ); \
pseTup->wIndex = ( languaGeId );\
pseTup->bRequest = USB_REQ_GET_DESCRIPTOR;\
pseTup->bmRequestType = 0x80;\
}
#define UsbBuildGetStatusRequest(uRb, \
endp_hanDle, \
recipiEnt, \
inDex, \
transferBufFer, \
completIon, \
contExt, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hanDle ); \
( uRb )->data_buffer = ( transferBufFer ); \
( uRb )->data_length = sizeof(USHORT); \
( uRb )->compleiton = ( completIon );\
( uRb )->context = ( contExt );\
( uRb )->reference = ( refereNce );\
pseTup->bmRequestType = ( 0x80 | recipiEnt );\
pseTup->bRequest = USB_REQ_GET_STATUS;\
pseTup->wIndex = ( index ); \
pseTup->wValue = 0;\
pseTup->wLength = sizeof( USHORT );\
}
#define UsbBuildFeatureRequest(uRb, \
endp_hanDle,\
recipiEnt, \
featureSelecTor, \
inDex, \
completIon, \
contExt, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hanDle ); \
( uRb )->data_buffer = NULL;\
( uRb )->data_length = ( 0 );\
( uRb )->completion = ( completIon );\
( uRb )->context = ( contExt ); \
( uRb )->reference = ( refereNce ); \
pseTup->bmRequestType = recipiEnt; \
pseTup->bRequest = USB_REQ_SET_FEATURE;\
pseTup->wValue = ( featureSelecTor );\
pseTup->wIndex = ( inDex );\
pseTup->wLength = 0;\
}
#define UsbBuildSelectConfigurationRequest(uRb, \
endp_hanDle,\
config_Val,\
completIon, \
contExt, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hanDle ); \
( uRb )->data_buffer = NULL;\
( uRb )->data_length = 0;\
( uRb )->completion = ( completIon );\
( uRb )->context = ( contExt ); \
( uRb )->reference = ( refereNce ); \
pseTup->bmRequestType = 0;\
pseTup->bRequest = USB_REQ_SET_CONFIGURATION;\
pseTup->wValue = ( config_Val );\
pseTup->wIndex = 0;\
pseTup->wLength = 0;\
}
#define UsbBuildSelectInterfaceRequest(uRb, \
endp_hanDle,\
if_Num, \
alt_Num,\
completIon, \
contExt, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hanDle ); \
( uRb )->data_buffer = NULL;\
( uRb )->data_length = 0;\
( uRb )->completion = ( completIon );\
( uRb )->context = ( contExt ); \
( uRb )->reference = ( refereNce ); \
pseTup->bmRequestType = 1;\
pseTup->bRequest = USB_REQ_SET_INERFACE;\
pseTup->wValue = ( alt_Num );\
pseTup->wIndex = ( if_Num );\
pseTup->wLength = 0;\
}
#define UsbBuildVendorRequest(uRb, \
endp_hanDle,\
data_bufFer, \
data_sIze, \
request_tYpe, \
requEst, \
vaLue, \
inDex, \
completIon, \
contExt, \
refereNce ) \
{ \
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( endp_hanDle ); \
( uRb )->data_buffer = data_bufFer;\
( uRb )->data_length = data_sIze;\
( uRb )->completion = ( completIon );\
( uRb )->context = ( contExt ); \
( uRb )->reference = ( refereNce ); \
pseTup->bmRequestType = request_tYpe;\
pseTup->bRequest = requEst;\
pseTup->wValue = vaLue;\
pseTup->wIndex = inDex;\
pseTup->wLength = ( USHORT )data_sIze;\
}
#define UsbBuildResetPipeRequest(uRb, \
dev_hanDle, \
endp_aDdr, \
completIon, \
contExt, \
refereNce ) \
{\
PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
urb_init( ( uRb ) );\
( uRb )->endp_handle = ( dev_hanDle | 0xffff ); \
( uRb )->completion = ( completIon );\
( uRb )->context = ( contExt ); \
( uRb )->reference = ( refereNce ); \
pseTup->bmRequestType = 0x02;\
pseTup->bRequest = USB_REQ_CLEAR_FEATURE;\
pseTup->wIndex = endp_aDdr;\
}
// Forward structs declarations
struct _URB;
struct _HCD;
struct _USB_DEV_MANAGER;
struct _USB_DEV;
struct _USB_ENDPOINT;
struct _USB_EVENT;
struct _USB_EVENT_POOL;
struct _USB_DRIVER;
/* USB constants */
#define USB_SPEED_FULL 0x00
#define USB_SPEED_LOW 0x01
#define USB_SPEED_HIGH 0x02
/*
* Device and/or Interface Class codes
*/
#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
#define USB_CLASS_AUDIO 1
#define USB_CLASS_COMM 2
#define USB_CLASS_HID 3
#define USB_CLASS_PHYSICAL 5
#define USB_CLASS_PRINTER 7
#define USB_CLASS_MASS_STORAGE 8
#define USB_CLASS_HUB 9
#define USB_CLASS_DATA 10
#define USB_CLASS_APP_SPEC 0xfe
#define USB_CLASS_VENDOR_SPEC 0xff
/*
* USB types
*/
#define USB_TYPE_MASK (0x03 << 5)
#define USB_TYPE_STANDARD (0x00 << 5)
#define USB_TYPE_CLASS (0x01 << 5)
#define USB_TYPE_VENDOR (0x02 << 5)
#define USB_TYPE_RESERVED (0x03 << 5)
/*
* USB recipients
*/
#define USB_RECIP_MASK 0x1f
#define USB_RECIP_DEVICE 0x00
#define USB_RECIP_INTERFACE 0x01
#define USB_RECIP_ENDPOINT 0x02
#define USB_RECIP_OTHER 0x03
/*
* USB directions
*/
#define USB_DIR_OUT 0
#define USB_DIR_IN 0x80
/*
* Descriptor types
*/
#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05
#define USB_DT_HID (USB_TYPE_CLASS | 0x01)
#define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
#define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
#define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
/*
* Descriptor sizes per descriptor type
*/
#define USB_DT_DEVICE_SIZE 18
#define USB_DT_CONFIG_SIZE 9
#define USB_DT_INTERFACE_SIZE 9
#define USB_DT_ENDPOINT_SIZE 7
#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
#define USB_DT_HUB_NONVAR_SIZE 7
#define USB_DT_HID_SIZE 9
/*
* Endpoints
*/
#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
#define USB_ENDPOINT_DIR_MASK 0x80
#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
#define USB_ENDPOINT_XFER_CONTROL 0
#define USB_ENDPOINT_XFER_ISOC 1
#define USB_ENDPOINT_XFER_BULK 2
#define USB_ENDPOINT_XFER_INT 3
/*
* USB Packet IDs (PIDs)
*/
#define USB_PID_UNDEF_0 0xf0
#define USB_PID_OUT 0xe1
#define USB_PID_ACK 0xd2
#define USB_PID_DATA0 0xc3
#define USB_PID_PING 0xb4 /* USB 2.0 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -