4 * Some USB bandwidth allocation constants.
7 typedef unsigned long ULONG_PTR
, *PULONG_PTR
;
9 #define USB2_HOST_DELAY 5 /* nsec, guess */
10 #define BW_HOST_DELAY 1000L /* nanoseconds */
11 #define BW_HUB_LS_SETUP 333L /* nanoseconds */
12 /* 4 full-speed bit times (est.) */
14 #define FRAME_TIME_BITS 12000L /* frame = 1 millisecond */
15 #define FRAME_TIME_MAX_BITS_ALLOC (90L * FRAME_TIME_BITS / 100L)
16 #define FRAME_TIME_USECS 1000L
17 #define FRAME_TIME_MAX_USECS_ALLOC (90L * FRAME_TIME_USECS / 100L)
19 #define bit_time(bytecount) (7 * 8 * bytecount / 6) /* with integer truncation */
20 /* Trying not to use worst-case bit-stuffing
21 of (7/6 * 8 * bytecount) = 9.33 * bytecount */
22 /* bytecount = data payload byte count */
24 #define ns_to_us(ns) ((ns + 500L) / 1000L)
25 /* convert & round nanoseconds to microseconds */
27 #define usb_make_handle( dev_Id, if_iDx, endp_iDx) \
28 ( ( DEV_HANDLE )( ( ( ( ( ULONG )dev_Id ) << 16 ) | ( ( ( ULONG )if_iDx ) << 8 ) ) | ( ( ULONG ) endp_iDx ) ) )
30 #define usb_make_ref( poinTER ) \
31 ( poinTER ^ 0xffffffff )
33 #define ptr_from_ref uhci_make_ref
35 #define dev_id_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) >> 16 )
36 #define if_idx_from_handle( hanDLE ) ( ( ( ( ULONG ) ( hanDLE ) ) << 16 ) >> 24 )
37 #define endp_idx_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) & 0xff )
39 #define endp_from_handle( pDEV, hanDLE, peNDP ) \
41 LONG if_idx, endp_idx;\
43 endp_idx = endp_idx_from_handle( hanDLE );\
44 if_idx = if_idx_from_handle( hanDLE );\
45 def_endp = ( ( hanDLE & 0xffff ) == 0xffff ); \
47 peNDP = &pdev->default_endp; \
50 if( if_idx >= pdev->usb_config->if_count ) \
52 else if( endp_idx >= pdev->usb_config->interf[ if_idx ].endp_count ) \
55 peNDP = &( pDEV )->usb_config->interf[ if_idx ].endp[ endp_idx ]; \
59 #define endp_type( enDP ) \
60 ( ( enDP->flags & USB_ENDP_FLAG_DEFAULT_ENDP ) \
61 ? USB_ENDPOINT_XFER_CONTROL\
62 : ( ( enDP )->pusb_endp_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK ) )
65 //init work data for urb
66 #define urb_init( uRb ) \
68 RtlZeroMemory( ( uRb ), sizeof( URB ) ); \
69 InitializeListHead( &( uRb )->trasac_list ); \
72 #define UsbBuildInterruptOrBulkTransferRequest(uRb, \
81 ( uRb )->endp_handle = endp_hanDle;\
82 ( uRb )->data_buffer = data_Buf;\
83 ( uRb )->data_length = data_sIze;\
84 ( uRb )->completion = completIon;\
85 ( uRb )->context = contExt; \
86 ( uRb )->reference = refereNce; \
91 #define UsbBuildGetDescriptorRequest(uRb, \
102 PUSB_CTRL_SETUP_PACKET pseTup;\
103 pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
104 urb_init( ( uRb ) );\
105 ( uRb )->endp_handle = ( endp_hAndle );\
106 ( uRb )->data_length = ( data_sIze ); \
107 ( uRb )->data_buffer = ( ( PUCHAR )data_bUffer ); \
108 ( uRb )->completion = ( compleTion );\
109 ( uRb )->context = ( ( PVOID )contexT ); \
110 ( uRb )->reference = ( ULONG )refereNce; \
111 pseTup->wValue = ( ( descriPtorType ) << 8 )| ( descriPtorIndex ); \
112 pseTup->wLength = ( data_sIze ); \
113 pseTup->wIndex = ( languaGeId );\
114 pseTup->bRequest = USB_REQ_GET_DESCRIPTOR;\
115 pseTup->bmRequestType = 0x80;\
120 #define UsbBuildGetStatusRequest(uRb, \
129 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
130 urb_init( ( uRb ) );\
131 ( uRb )->endp_handle = ( endp_hanDle ); \
132 ( uRb )->data_buffer = ( transferBufFer ); \
133 ( uRb )->data_length = sizeof(USHORT); \
134 ( uRb )->compleiton = ( completIon );\
135 ( uRb )->context = ( contExt );\
136 ( uRb )->reference = ( refereNce );\
137 pseTup->bmRequestType = ( 0x80 | recipiEnt );\
138 pseTup->bRequest = USB_REQ_GET_STATUS;\
139 pseTup->wIndex = ( index ); \
141 pseTup->wLength = sizeof( USHORT );\
145 #define UsbBuildFeatureRequest(uRb, \
154 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
155 urb_init( ( uRb ) );\
156 ( uRb )->endp_handle = ( endp_hanDle ); \
157 ( uRb )->data_buffer = NULL;\
158 ( uRb )->data_length = ( 0 );\
159 ( uRb )->completion = ( completIon );\
160 ( uRb )->context = ( contExt ); \
161 ( uRb )->reference = ( refereNce ); \
162 pseTup->bmRequestType = recipiEnt; \
163 pseTup->bRequest = USB_REQ_SET_FEATURE;\
164 pseTup->wValue = ( featureSelecTor );\
165 pseTup->wIndex = ( inDex );\
166 pseTup->wLength = 0;\
169 #define UsbBuildSelectConfigurationRequest(uRb, \
176 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
177 urb_init( ( uRb ) );\
178 ( uRb )->endp_handle = ( endp_hanDle ); \
179 ( uRb )->data_buffer = NULL;\
180 ( uRb )->data_length = 0;\
181 ( uRb )->completion = ( completIon );\
182 ( uRb )->context = ( contExt ); \
183 ( uRb )->reference = ( refereNce ); \
184 pseTup->bmRequestType = 0;\
185 pseTup->bRequest = USB_REQ_SET_CONFIGURATION;\
186 pseTup->wValue = ( config_Val );\
188 pseTup->wLength = 0;\
191 #define UsbBuildSelectInterfaceRequest(uRb, \
199 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
200 urb_init( ( uRb ) );\
201 ( uRb )->endp_handle = ( endp_hanDle ); \
202 ( uRb )->data_buffer = NULL;\
203 ( uRb )->data_length = 0;\
204 ( uRb )->completion = ( completIon );\
205 ( uRb )->context = ( contExt ); \
206 ( uRb )->reference = ( refereNce ); \
207 pseTup->bmRequestType = 1;\
208 pseTup->bRequest = USB_REQ_SET_INERFACE;\
209 pseTup->wValue = ( alt_Num );\
210 pseTup->wIndex = ( if_Num );\
211 pseTup->wLength = 0;\
215 #define UsbBuildVendorRequest(uRb, \
227 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
228 urb_init( ( uRb ) );\
229 ( uRb )->endp_handle = ( endp_hanDle ); \
230 ( uRb )->data_buffer = data_bufFer;\
231 ( uRb )->data_length = data_sIze;\
232 ( uRb )->completion = ( completIon );\
233 ( uRb )->context = ( contExt ); \
234 ( uRb )->reference = ( refereNce ); \
235 pseTup->bmRequestType = request_tYpe;\
236 pseTup->bRequest = requEst;\
237 pseTup->wValue = vaLue;\
238 pseTup->wIndex = inDex;\
239 pseTup->wLength = ( USHORT )data_sIze;\
242 #define UsbBuildResetPipeRequest(uRb, \
249 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
250 urb_init( ( uRb ) );\
251 ( uRb )->endp_handle = ( dev_hanDle | 0xffff ); \
252 ( uRb )->completion = ( completIon );\
253 ( uRb )->context = ( contExt ); \
254 ( uRb )->reference = ( refereNce ); \
255 pseTup->bmRequestType = 0x02;\
256 pseTup->bRequest = USB_REQ_CLEAR_FEATURE;\
257 pseTup->wIndex = endp_aDdr;\
260 // Forward structs declarations
263 struct _USB_DEV_MANAGER
;
265 struct _USB_ENDPOINT
;
267 struct _USB_EVENT_POOL
;
272 #define USB_SPEED_FULL 0x00
273 #define USB_SPEED_LOW 0x01
274 #define USB_SPEED_HIGH 0x02
277 * Device and/or Interface Class codes
279 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
280 #define USB_CLASS_AUDIO 1
281 #define USB_CLASS_COMM 2
282 #define USB_CLASS_HID 3
283 #define USB_CLASS_PHYSICAL 5
284 #define USB_CLASS_PRINTER 7
285 #define USB_CLASS_MASS_STORAGE 8
286 #define USB_CLASS_HUB 9
287 #define USB_CLASS_DATA 10
288 #define USB_CLASS_APP_SPEC 0xfe
289 #define USB_CLASS_VENDOR_SPEC 0xff
294 #define USB_TYPE_MASK (0x03 << 5)
295 #define USB_TYPE_STANDARD (0x00 << 5)
296 #define USB_TYPE_CLASS (0x01 << 5)
297 #define USB_TYPE_VENDOR (0x02 << 5)
298 #define USB_TYPE_RESERVED (0x03 << 5)
303 #define USB_RECIP_MASK 0x1f
304 #define USB_RECIP_DEVICE 0x00
305 #define USB_RECIP_INTERFACE 0x01
306 #define USB_RECIP_ENDPOINT 0x02
307 #define USB_RECIP_OTHER 0x03
312 #define USB_DIR_OUT 0
313 #define USB_DIR_IN 0x80
318 #define USB_DT_DEVICE 0x01
319 #define USB_DT_CONFIG 0x02
320 #define USB_DT_STRING 0x03
321 #define USB_DT_INTERFACE 0x04
322 #define USB_DT_ENDPOINT 0x05
324 #define USB_DT_HID (USB_TYPE_CLASS | 0x01)
325 #define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
326 #define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
327 #define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
330 * Descriptor sizes per descriptor type
332 #define USB_DT_DEVICE_SIZE 18
333 #define USB_DT_CONFIG_SIZE 9
334 #define USB_DT_INTERFACE_SIZE 9
335 #define USB_DT_ENDPOINT_SIZE 7
336 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
337 #define USB_DT_HUB_NONVAR_SIZE 7
338 #define USB_DT_HID_SIZE 9
343 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
344 #define USB_ENDPOINT_DIR_MASK 0x80
346 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
347 #define USB_ENDPOINT_XFER_CONTROL 0
348 #define USB_ENDPOINT_XFER_ISOC 1
349 #define USB_ENDPOINT_XFER_BULK 2
350 #define USB_ENDPOINT_XFER_INT 3
353 * USB Packet IDs (PIDs)
355 #define USB_PID_UNDEF_0 0xf0
356 #define USB_PID_OUT 0xe1
357 #define USB_PID_ACK 0xd2
358 #define USB_PID_DATA0 0xc3
359 #define USB_PID_PING 0xb4 /* USB 2.0 */
360 #define USB_PID_SOF 0xa5
361 #define USB_PID_NYET 0x96 /* USB 2.0 */
362 #define USB_PID_DATA2 0x87 /* USB 2.0 */
363 #define USB_PID_SPLIT 0x78 /* USB 2.0 */
364 #define USB_PID_IN 0x69
365 #define USB_PID_NAK 0x5a
366 #define USB_PID_DATA1 0x4b
367 #define USB_PID_PREAMBLE 0x3c /* Token mode */
368 #define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */
369 #define USB_PID_SETUP 0x2d
370 #define USB_PID_STALL 0x1e
371 #define USB_PID_MDATA 0x0f /* USB 2.0 */
376 #define USB_REQ_GET_STATUS 0x00
377 #define USB_REQ_CLEAR_FEATURE 0x01
378 #define USB_REQ_SET_FEATURE 0x03
379 #define USB_REQ_SET_ADDRESS 0x05
380 #define USB_REQ_GET_DESCRIPTOR 0x06
381 #define USB_REQ_SET_DESCRIPTOR 0x07
382 #define USB_REQ_GET_CONFIGURATION 0x08
383 #define USB_REQ_SET_CONFIGURATION 0x09
384 #define USB_REQ_GET_INTERFACE 0x0A
385 #define USB_REQ_SET_INTERFACE 0x0B
386 #define USB_REQ_SYNCH_FRAME 0x0C
391 #define USB_REQ_GET_REPORT 0x01
392 #define USB_REQ_GET_IDLE 0x02
393 #define USB_REQ_GET_PROTOCOL 0x03
394 #define USB_REQ_SET_REPORT 0x09
395 #define USB_REQ_SET_IDLE 0x0A
396 #define USB_REQ_SET_PROTOCOL 0x0B
399 #define HUB_REQ_GET_STATE 0x02
402 #define HUB_REQ_CLEAR_TT_BUFFER 0x08
405 typedef LONG USBD_STATUS
;
410 // Status values are 32 bit values layed out as follows:
412 // 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
413 // 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
414 // +---+---------------------------+-------------------------------+
415 // | S | Status Code |
416 // +---+---------------------------+-------------------------------+
420 // S - is the state code
422 // 00 - completed with success
423 // 01 - request is pending
424 // 10 - completed with error, endpoint not stalled
425 // 11 - completed with error, endpoint stalled
428 // Code - is the status code
432 // Generic test for success on any status value (non-negative numbers
433 // indicate success).
436 #define usb_success(Status) ((USBD_STATUS)(Status) >= 0)
439 // Generic test for pending status value.
442 #define usb_pending(Status) ((ULONG)(Status) >> 30 == 1)
445 // Generic test for error on any status value.
448 #define usb_error(Status) ((USBD_STATUS)(Status) < 0)
451 // Generic test for stall on any status value.
454 #define usb_halted(Status) ((ULONG)(Status) >> 30 == 3)
457 // Macro to check the status code only
460 #define usb_status(Status) ((ULONG)(Status) & 0x0FFFFFFFL)
463 #define USB_STATUS_SUCCESS ((USBD_STATUS)0x00000000L)
464 #define USB_STATUS_PENDING ((USBD_STATUS)0x40000000L)
465 #define USB_STATUS_HALTED ((USBD_STATUS)0xC0000000L)
466 #define USB_STATUS_ERROR ((USBD_STATUS)0x80000000L)
470 // Note: these status codes have both the error and the stall bit set.
472 #define USB_STATUS_CRC ((USBD_STATUS)0xC0000401L)
473 #define USB_STATUS_BTSTUFF ((USBD_STATUS)0xC0000402L)
474 #define USB_STATUS_DATA_TOGGLE_MISMATCH ((USBD_STATUS)0xC0000403L)
475 #define USB_STATUS_STALL_PID ((USBD_STATUS)0xC0000404L)
476 #define USB_STATUS_DEV_NOT_RESPONDING ((USBD_STATUS)0xC0000405L)
477 #define USB_STATUS_PID_CHECK_FAILURE ((USBD_STATUS)0xC0000406L)
478 #define USB_STATUS_UNEXPECTED_PID ((USBD_STATUS)0xC0000407L)
479 #define USB_STATUS_DATA_OVERRUN ((USBD_STATUS)0xC0000408L)
480 #define USB_STATUS_DATA_UNDERRUN ((USBD_STATUS)0xC0000409L)
481 #define USB_STATUS_RESERVED1 ((USBD_STATUS)0xC000040AL)
482 #define USB_STATUS_RESERVED2 ((USBD_STATUS)0xC000040BL)
483 #define USB_STATUS_BUFFER_OVERRUN ((USBD_STATUS)0xC000040CL)
484 #define USB_STATUS_BUFFER_UNDERRUN ((USBD_STATUS)0xC000040DL)
485 #define USB_STATUS_NOT_ACCESSED ((USBD_STATUS)0xC000040FL)
486 #define USB_STATUS_FIFO ((USBD_STATUS)0xC0000410L)
487 #define USB_STATUS_BABBLE_DETECTED ((USBD_STATUS)0xC0000408L)
490 // returned by HCD if a transfer is submitted to an endpoint that is
493 #define USB_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000430L)
496 // Software status codes
497 // Note: the following status codes have only the error bit set
499 #define USB_STATUS_NO_MEMORY ((USBD_STATUS)0x80000100L)
500 #define USB_STATUS_INVALID_URB_FUNCTION ((USBD_STATUS)0x80000200L)
501 #define USB_STATUS_INVALID_PARAMETER ((USBD_STATUS)0x80000300L)
504 // returned if client driver attempts to close an endpoint/interface
505 // or configuration with outstanding transfers.
507 #define USB_STATUS_ERROR_BUSY ((USBD_STATUS)0x80000400L)
509 // returned by USBD if it cannot complete a URB request, typically this
510 // will be returned in the URB status field when the Irp is completed
511 // with a more specific NT error code in the irp.status field.
513 #define USB_STATUS_REQUEST_FAILED ((USBD_STATUS)0x80000500L)
515 #define USB_STATUS_INVALID_PIPE_HANDLE ((USBD_STATUS)0x80000600L)
517 // returned when there is not enough bandwidth avialable
518 // to open a requested endpoint
519 #define USB_STATUS_NO_BANDWIDTH ((USBD_STATUS)0x80000700L)
523 #define USB_STATUS_INTERNAL_HC_ERROR ((USBD_STATUS)0x80000800L)
525 // returned when a short packet terminates the transfer
526 // ie USBD_SHORT_TRANSFER_OK bit not set
528 #define USB_STATUS_ERROR_SHORT_TRANSFER ((USBD_STATUS)0x80000900L)
530 // returned if the requested start frame is not within
531 // USBD_ISO_START_FRAME_RANGE of the current USB frame,
532 // note that the stall bit is set
534 #define USB_STATUS_BAD_START_FRAME ((USBD_STATUS)0xC0000A00L)
536 // returned by HCD if all packets in an iso transfer complete with an error
538 #define USB_STATUS_ISOCH_REQUEST_FAILED ((USBD_STATUS)0xC0000B00L)
540 // returned by USBD if the frame length control for a given
541 // HC is already taken by anothe driver
543 #define USB_STATUS_FRAME_CONTROL_OWNED ((USBD_STATUS)0xC0000C00L)
545 // returned by USBD if the caller does not own frame length control and
546 // attempts to release or modify the HC frame length
548 #define USB_STATUS_FRAME_CONTROL_NOT_OWNED ((USBD_STATUS)0xC0000D00L)
551 // set when a transfers is completed due to an AbortPipe request from
554 // Note: no error or stall bit is set for these status codes
556 #define USB_STATUS_CANCELED ((USBD_STATUS)0x00010000L)
558 #define USB_STATUS_CANCELING ((USBD_STATUS)0x00020000L)
560 // Device type -- in the "User Defined" range."
561 #define FILE_HCD_DEV_TYPE 45000
562 #define FILE_UHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 1 )
563 #define FILE_OHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 2 )
564 #define FILE_EHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 3 )
565 #define FILE_USB_DEV_TYPE ( FILE_HCD_DEV_TYPE + 8 )
567 #define IOCTL_GET_DEV_COUNT CTL_CODE( FILE_HCD_DEV_TYPE, 4093, METHOD_BUFFERED, FILE_ANY_ACCESS )
568 //input_buffer and input_buffer_length is zero, output_buffer is to receive a dword value of the
569 //dev count, output_buffer_length must be no less than sizeof( unsigned long ).
571 #define IOCTL_ENUM_DEVICES CTL_CODE( FILE_HCD_DEV_TYPE, 4094, METHOD_BUFFERED, FILE_ANY_ACCESS )
572 //input_buffer is a dword value to indicate the count of elements in the array
573 //input_buffer_length is sizeof( unsigned long ), output_buffer is to receive a
574 //structure ENUM_DEV_ARRAY where dev_count is the elements hold in this array.
576 #define IOCTL_GET_DEV_DESC CTL_CODE( FILE_HCD_DEV_TYPE, 4095, METHOD_BUFFERED, FILE_ANY_ACCESS )
577 //input_buffer is a structure GET_DEV_DESC_REQ, and the input_buffer_length is
578 //no less than sizeof( input_buffer ), output_buffer is a buffer to receive the
579 //requested dev's desc, and output_buffer_length specifies the length of the
582 #define IOCTL_SUBMIT_URB_RD CTL_CODE( FILE_HCD_DEV_TYPE, 4096, METHOD_IN_DIRECT, FILE_ANY_ACCESS )
583 #define IOCTL_SUBMIT_URB_WR CTL_CODE( FILE_HCD_DEV_TYPE, 4097, METHOD_OUT_DIRECT, FILE_ANY_ACCESS )
584 // if the major_function is IRP_MJ_DEVICE_CONTROL
585 // input_buffer is a URB, and input_buffer_length is equal to or greater than
586 // sizeof( URB ); the output_buffer is a buffer to receive data from or send data
587 // to device. only the following urb fields can be accessed, others must be zeroed.
588 // DEV_HANDLE endp_handle;
589 // UCHAR setup_packet[8]; // for control pipe
590 // the choosing of IOCTL_SUBMIT_URB_RD or IOCTL_SUBMIT_URB_WR should be determined
591 // by the current URB, for example, a request string from device will use XXX_RD,
592 // and a write to the bulk endpoint will use XXX_WR
593 // if the major_function is IRP_MJ_INTERNAL_DEVICE_CONTROL
594 // input_buffer is a URB, and input_buffer_length is equal to or greater than
596 // only the following urb fields can be accessed, others must be zeroed.
597 // DEV_HANDLE endp_handle;
598 // UCHAR setup_packet[8]; // for control pipe, or zeroed
599 // PUCHAR data_buffer; // buffer for READ/WRITE
600 // ULONG data_length; // buffer size in bytes
602 #define IOCTL_SUBMIT_URB_NOIO CTL_CODE( FILE_HCD_DEV_TYPE, 4098, METHOD_BUFFERED, FILE_ANY_ACCESS )
603 // input_buffer is a URB, and input_buffer_length is equal to or greater than
604 // sizeof( URB ); the output_buffer is null and no output_buffer_length,
605 // only the following fields in urb can be accessed, others must be zeroed.
606 // DEV_HANDLE endp_handle;
607 // UCHAR setup_packet[8]; //for control pipe
608 // there is no difference between IRP_MJ_DEVICE_CONTROL and IRP_MJ_INTERNAL_DEVICE_CONTROL
609 #define IOCTL_GET_DEV_HANDLE CTL_CODE( FILE_HCD_DEV_TYPE, 4099, METHOD_BUFFERED, FILE_ANY_ACCESS )
610 // input_buffer is null ,and input_buffer_length is zero.
611 // output_buffer will hold the handle to this dev, output_buffer_length is 4
614 typedef ULONG DEV_HANDLE
, ENDP_HANDLE
, IF_HANDLE
;
617 #pragma pack( push, usb_align, 1 )
619 //structures for DeviceIoControl
620 typedef struct _ENUM_DEV_ELEMENT
622 DEV_HANDLE dev_handle
;
627 } ENUM_DEV_ELEMENT
, *PENUM_DEV_ELEMENT
;
629 typedef struct _ENUM_DEV_ARRAY
632 ENUM_DEV_ELEMENT dev_arr
[ 1 ];
634 } ENUM_DEV_ARRAY
, *PENUM_DEV_ARRAY
;
636 typedef struct _GET_DEV_DESC_REQ
638 DEV_HANDLE dev_handle
;
642 } GET_DEV_DESC_REQ
, *PGET_DEV_DESC_REQ
;
645 typedef struct _USB_CTRL_SETUP_PACKET
653 }USB_CTRL_SETUP_PACKET
, *PUSB_CTRL_SETUP_PACKET
;
655 typedef struct _USB_STRING_DESCRIPTOR
658 UCHAR bDescriptorType
;
661 } USB_STRING_DESCRIPTOR
, *PUSB_STRING_DESCRIPTOR
;
663 typedef struct _USB_DESC_HEADER
666 UCHAR bDescriptorType
;
668 } USB_DESC_HEADER
, *PUSB_DESC_HEADER
;
670 typedef struct _USB_ENDPOINT_DESC
673 UCHAR bDescriptorType
;
674 UCHAR bEndpointAddress
;
676 USHORT wMaxPacketSize
;
679 } USB_ENDPOINT_DESC
, *PUSB_ENDPOINT_DESC
;
681 typedef struct _USB_INTERFACE_DESC
684 UCHAR bDescriptorType
;
685 UCHAR bInterfaceNumber
;
686 UCHAR bAlternateSetting
;
688 UCHAR bInterfaceClass
;
689 UCHAR bInterfaceSubClass
;
690 UCHAR bInterfaceProtocol
;
693 } USB_INTERFACE_DESC
, *PUSB_INTERFACE_DESC
;
695 typedef struct _USB_CONFIGURATION_DESC
698 UCHAR bDescriptorType
;
700 UCHAR bNumInterfaces
;
701 UCHAR bConfigurationValue
;
702 UCHAR iConfiguration
;
706 } USB_CONFIGURATION_DESC
, *PUSB_CONFIGURATION_DESC
;
708 typedef struct _USB_DEVICE_DESC
711 UCHAR bDescriptorType
;
714 UCHAR bDeviceSubClass
;
715 UCHAR bDeviceProtocol
;
716 UCHAR bMaxPacketSize0
;
723 UCHAR bNumConfigurations
;
725 } USB_DEVICE_DESC
, *PUSB_DEVICE_DESC
;
728 #define URB_FLAG_STATE_MASK 0x0f
729 #define URB_FLAG_STATE_PENDING 0x00
730 #define URB_FLAG_STATE_IN_PROCESS 0x01
731 #define URB_FLAG_STATE_FINISHED 0x02
734 #define URB_FLAG_STATE_DOORBELL 0x03 // for async request removal
735 #define URB_FLAG_STATE_WAIT_FRAME 0x04 // for sync request removal( for cancel only )
736 #define URB_FLAG_STATE_ERROR 0x05
738 #define URB_FLAG_IN_SCHEDULE 0x10
739 #define URB_FLAG_FORCE_CANCEL 0x20
740 #define URB_FLAG_SHORT_PACKET 0x80000000
742 typedef struct _SPLIT_ISO_BUS_TIME
747 } SPLIT_ISO_BUS_TIME
, *PSPLIT_ISO_BUS_TIME
;
749 typedef struct _ISO_PACKET_DESC
752 LONG length
; // expected length
757 LONG bus_time
; //opaque for client request of split iso, the bus_time is the start_uframe for each transaction
758 SPLIT_ISO_BUS_TIME params
;
761 } ISO_PACKET_DESC
, *PISO_PACKET_DESC
;
763 #define CTRL_PARENT_URB_VALID 1
765 typedef void ( *PURBCOMPLETION
)( struct _URB
*purb
, PVOID pcontext
);
767 typedef struct _CTRL_REQ_STACK
769 PURBCOMPLETION urb_completion
; // the last step of the urb_completion is to call the
770 // the prevoius stack's callback if has, and the last
771 // one is purb->completion
775 } CTRL_REQ_STACK
, *PCTRL_REQ_STACK
;
777 #pragma pack( pop, usb_align )
779 typedef struct _URB_HS_PIPE_CONTENT
781 ULONG trans_type
: 2; // bit 0-1
782 ULONG mult_count
: 2; // bit 2-3, used in high speed int and iso requests
783 ULONG reserved
: 1; // bit 1
784 ULONG speed_high
: 1; // bit 5
785 ULONG speed_low
: 1; // bit 6
786 ULONG trans_dir
: 1; // bit 7
787 ULONG dev_addr
: 7; // bit 8-14
788 ULONG endp_addr
: 4; // bit 15-18
789 ULONG data_toggle
: 1; // bit 19
790 ULONG max_packet_size
: 4; // bit 20-23 log( max_packet_size )
791 ULONG interval
: 4; // bit 24-27 the same definition in USB2.0, for high or full/low speed
792 ULONG start_uframe
: 3; // bit 28-30
793 ULONG reserved1
: 1; //
795 } URB_HS_PIPE_CONTENT
, *PURB_HS_PIPE_CONTENT
;
797 typedef struct _URB_HS_CONTEXT_CONTENT
799 ULONG hub_addr
: 7; // high speed hub addr for split transfer
803 } URB_HS_CONTEXT_CONTENT
, *PURB_HS_CONTEXT_CONTENT
;
809 DEV_HANDLE endp_handle
;
811 //record info for isr use, similar to td.status
812 //int pipe has different content in the 8 msb
813 //the eight bits contain interrupt interval.
814 //and max packet length is encoded in 3 bits from 23-21
815 //that means 2^(x) bytes in the packet.
816 ULONG pipe
; // bit0-1: endp type, bit 6: ls or fs. bit 7: dir
820 UCHAR setup_packet
[8]; // for control
821 LONG params
[ 2 ]; // params[ 0 ] is used in iso transfer as max_packet_size
824 PUCHAR data_buffer
; //user data
825 LONG data_length
; //user data length
827 struct _USB_DEV
*pdev
;
828 struct _USB_ENDPOINT
*pendp
; //pipe for current transfer
830 PURBCOMPLETION completion
;
831 PVOID context
; //parameter of completion
833 PVOID urb_ext
; //for high speed hcd use
834 ULONG hs_context
; //for high speed hcd use
836 PIRP pirp
; //irp from client driver
837 LONG reference
; //for caller private use
839 LONG td_count
; //for any kinds of transfer
840 LONG rest_bytes
; //for split bulk transfer involving more than 1024 tds
841 LONG bytes_to_transfer
;
842 LONG bytes_transfered
; //( bulk transfer )accumulate one split-transfer by xfered bytes of the executed transactions
843 PLIST_ENTRY last_finished_td
; //last inactive td useful for large amount transfer
844 LIST_ENTRY trasac_list
; //list of tds or qhs
848 LONG iso_start_frame
; // for high speed endp, this is uframe index, and not used for full/low speed endp, instead,
849 // iso_packet_desc.param.start_uframe is used.
850 LONG int_start_frame
; // frame( ms ) index for high/full/low speed endp
853 UCHAR ctrl_req_flags
;
854 UCHAR ctrl_stack_count
;
855 UCHAR ctrl_cur_stack
; // the receiver uses this by increment the stack pointer first. if the requester
856 UCHAR ctrl_reserved
; // send it down with ctrl_stack_count zero, that means the stack is not initialized,
857 // and can be initialized by receiver to 1 and only 1.
858 // If the initializer found the stack size won't meet the number down the drivers, it must
859 // reallocate one urb with the required stack size. and store the previous urb in
866 LONG iso_frame_count
; // uframe for high speed and frame for full/low speed
867 struct _URB
* ctrl_parent_urb
;
872 ISO_PACKET_DESC iso_packet_desc
[ 1 ]; //used to build up trasac_list for iso transfer and claim bandwidth
873 CTRL_REQ_STACK ctrl_req_stack
[ 1 ];
909 struct _USB_DRIVER
*usb_drvr
912 //return reference to the endp
919 //return reference to the interface
927 usb_set_configuration(
932 // each call will return full size of the config desc and
933 // its if, endp descs.
934 // return value is the bytes actually returned.
935 // if the return value is equal to wTotalLength, all the descs of the
936 // configuration returned.
947 struct _USB_DEV_MANAGER
* dev_mgr
,
958 void usb_fill_int_urb(PURB urb
,
959 struct _USB_DEV
*dev
,
961 PVOID transfer_buffer
,
963 PURBCOMPLETION complete
,
975 //increment the dev->ref_count to lock the dev
977 usb_query_and_lock_dev(
978 struct _USB_DEV_MANAGER
* dev_mgr
,
979 DEV_HANDLE dev_handle
,
980 struct _USB_DEV
** ppdev
983 //decrement the dev->ref_count
991 struct _USB_DEV
*pdev
,
992 struct _USB_ENDPOINT
*pendp
,
993 PURBCOMPLETION client_completion
,
994 PVOID param
//parameter for client_completion
998 usb_reset_pipe_completion(
1005 POOL_TYPE pool_type
,
1014 PUSB_CONFIGURATION_DESC
1015 usb_find_config_desc_by_val(
1021 PUSB_CONFIGURATION_DESC
1022 usb_find_config_desc_by_idx(
1029 usb_skip_if_and_altif(
1034 usb_skip_one_config(
1035 PUCHAR
* pconfig_desc_BUF
1050 PLARGE_INTEGER clicks
1058 struct _USB_DEV_MANAGER
*dev_mgr
,
1059 DEV_HANDLE endp_handle
,
1060 PURBCOMPLETION reset_completion
,
1065 usb_call_ctrl_completion(
1073 ); //used to check descriptor validity