4 * Some USB bandwidth allocation constants.
7 #define USB2_HOST_DELAY 5 /* nsec, guess */
8 #define BW_HOST_DELAY 1000L /* nanoseconds */
9 #define BW_HUB_LS_SETUP 333L /* nanoseconds */
10 /* 4 full-speed bit times (est.) */
12 #define FRAME_TIME_BITS 12000L /* frame = 1 millisecond */
13 #define FRAME_TIME_MAX_BITS_ALLOC (90L * FRAME_TIME_BITS / 100L)
14 #define FRAME_TIME_USECS 1000L
15 #define FRAME_TIME_MAX_USECS_ALLOC (90L * FRAME_TIME_USECS / 100L)
17 #define bit_time(bytecount) (7 * 8 * bytecount / 6) /* with integer truncation */
18 /* Trying not to use worst-case bit-stuffing
19 of (7/6 * 8 * bytecount) = 9.33 * bytecount */
20 /* bytecount = data payload byte count */
22 #define ns_to_us(ns) ((ns + 500L) / 1000L)
23 /* convert & round nanoseconds to microseconds */
25 #define usb_make_handle( dev_Id, if_iDx, endp_iDx) \
26 ( ( DEV_HANDLE )( ( ( ( ( ULONG )dev_Id ) << 16 ) | ( ( ( ULONG )if_iDx ) << 8 ) ) | ( ( ULONG ) endp_iDx ) ) )
28 #define usb_make_ref( poinTER ) \
29 ( poinTER ^ 0xffffffff )
31 #define ptr_from_ref uhci_make_ref
33 #define dev_id_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) >> 16 )
34 #define if_idx_from_handle( hanDLE ) ( ( ( ( ULONG ) ( hanDLE ) ) << 16 ) >> 24 )
35 #define endp_idx_from_handle( hanDLE ) ( ( ( ULONG ) ( hanDLE ) ) & 0xff )
37 #define endp_from_handle( pDEV, hanDLE, peNDP ) \
39 LONG if_idx, endp_idx;\
41 endp_idx = endp_idx_from_handle( hanDLE );\
42 if_idx = if_idx_from_handle( hanDLE );\
43 def_endp = ( ( hanDLE & 0xffff ) == 0xffff ); \
45 peNDP = &pdev->default_endp; \
48 if( if_idx >= pdev->usb_config->if_count ) \
50 else if( endp_idx >= pdev->usb_config->interf[ if_idx ].endp_count ) \
53 peNDP = &( pDEV )->usb_config->interf[ if_idx ].endp[ endp_idx ]; \
57 #define endp_type( enDP ) \
58 ( ( enDP->flags & USB_ENDP_FLAG_DEFAULT_ENDP ) \
59 ? USB_ENDPOINT_XFER_CONTROL\
60 : ( ( enDP )->pusb_endp_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK ) )
63 //init work data for urb
64 #define urb_init( uRb ) \
66 RtlZeroMemory( ( uRb ), sizeof( URB ) ); \
67 InitializeListHead( &( uRb )->trasac_list ); \
70 #define UsbBuildInterruptOrBulkTransferRequest(uRb, \
79 ( uRb )->endp_handle = endp_hanDle;\
80 ( uRb )->data_buffer = data_Buf;\
81 ( uRb )->data_length = data_sIze;\
82 ( uRb )->completion = completIon;\
83 ( uRb )->context = contExt; \
84 ( uRb )->reference = refereNce; \
89 #define UsbBuildGetDescriptorRequest(uRb, \
100 PUSB_CTRL_SETUP_PACKET pseTup;\
101 pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
102 urb_init( ( uRb ) );\
103 ( uRb )->endp_handle = ( endp_hAndle );\
104 ( uRb )->data_length = ( data_sIze ); \
105 ( uRb )->data_buffer = ( ( PUCHAR )data_bUffer ); \
106 ( uRb )->completion = ( compleTion );\
107 ( uRb )->context = ( ( PVOID )contexT ); \
108 ( uRb )->reference = ( ULONG )refereNce; \
109 pseTup->wValue = ( ( descriPtorType ) << 8 )| ( descriPtorIndex ); \
110 pseTup->wLength = ( data_sIze ); \
111 pseTup->wIndex = ( languaGeId );\
112 pseTup->bRequest = USB_REQ_GET_DESCRIPTOR;\
113 pseTup->bmRequestType = 0x80;\
118 #define UsbBuildGetStatusRequest(uRb, \
127 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
128 urb_init( ( uRb ) );\
129 ( uRb )->endp_handle = ( endp_hanDle ); \
130 ( uRb )->data_buffer = ( transferBufFer ); \
131 ( uRb )->data_length = sizeof(USHORT); \
132 ( uRb )->completion = ( completIon );\
133 ( uRb )->context = ( contExt );\
134 ( uRb )->reference = ( refereNce );\
135 pseTup->bmRequestType = ( 0x80 | recipiEnt );\
136 pseTup->bRequest = USB_REQ_GET_STATUS;\
137 pseTup->wIndex = ( inDex ); \
139 pseTup->wLength = sizeof( USHORT );\
143 #define UsbBuildFeatureRequest(uRb, \
152 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
153 urb_init( ( uRb ) );\
154 ( uRb )->endp_handle = ( endp_hanDle ); \
155 ( uRb )->data_buffer = NULL;\
156 ( uRb )->data_length = ( 0 );\
157 ( uRb )->completion = ( completIon );\
158 ( uRb )->context = ( contExt ); \
159 ( uRb )->reference = ( refereNce ); \
160 pseTup->bmRequestType = recipiEnt; \
161 pseTup->bRequest = USB_REQ_SET_FEATURE;\
162 pseTup->wValue = ( featureSelecTor );\
163 pseTup->wIndex = ( inDex );\
164 pseTup->wLength = 0;\
167 #define UsbBuildSelectConfigurationRequest(uRb, \
174 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
175 urb_init( ( uRb ) );\
176 ( uRb )->endp_handle = ( endp_hanDle ); \
177 ( uRb )->data_buffer = NULL;\
178 ( uRb )->data_length = 0;\
179 ( uRb )->completion = ( completIon );\
180 ( uRb )->context = ( contExt ); \
181 ( uRb )->reference = ( refereNce ); \
182 pseTup->bmRequestType = 0;\
183 pseTup->bRequest = USB_REQ_SET_CONFIGURATION;\
184 pseTup->wValue = ( config_Val );\
186 pseTup->wLength = 0;\
189 #define UsbBuildSelectInterfaceRequest(uRb, \
197 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
198 urb_init( ( uRb ) );\
199 ( uRb )->endp_handle = ( endp_hanDle ); \
200 ( uRb )->data_buffer = NULL;\
201 ( uRb )->data_length = 0;\
202 ( uRb )->completion = ( completIon );\
203 ( uRb )->context = ( contExt ); \
204 ( uRb )->reference = ( refereNce ); \
205 pseTup->bmRequestType = 1;\
206 pseTup->bRequest = USB_REQ_SET_INERFACE;\
207 pseTup->wValue = ( alt_Num );\
208 pseTup->wIndex = ( if_Num );\
209 pseTup->wLength = 0;\
213 #define UsbBuildVendorRequest(uRb, \
225 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
226 urb_init( ( uRb ) );\
227 ( uRb )->endp_handle = ( endp_hanDle ); \
228 ( uRb )->data_buffer = data_bufFer;\
229 ( uRb )->data_length = data_sIze;\
230 ( uRb )->completion = ( completIon );\
231 ( uRb )->context = ( contExt ); \
232 ( uRb )->reference = ( refereNce ); \
233 pseTup->bmRequestType = request_tYpe;\
234 pseTup->bRequest = requEst;\
235 pseTup->wValue = vaLue;\
236 pseTup->wIndex = inDex;\
237 pseTup->wLength = ( USHORT )data_sIze;\
240 #define UsbBuildResetPipeRequest(uRb, \
247 PUSB_CTRL_SETUP_PACKET pseTup = ( PUSB_CTRL_SETUP_PACKET )( uRb )->setup_packet;\
248 urb_init( ( uRb ) );\
249 ( uRb )->endp_handle = ( dev_hanDle | 0xffff ); \
250 ( uRb )->completion = ( completIon );\
251 ( uRb )->context = ( contExt ); \
252 ( uRb )->reference = ( refereNce ); \
253 pseTup->bmRequestType = 0x02;\
254 pseTup->bRequest = USB_REQ_CLEAR_FEATURE;\
255 pseTup->wIndex = endp_aDdr;\
258 // Forward structs declarations
261 struct _USB_DEV_MANAGER
;
263 struct _USB_ENDPOINT
;
265 struct _USB_EVENT_POOL
;
270 #define USB_SPEED_FULL 0x00
271 #define USB_SPEED_LOW 0x01
272 #define USB_SPEED_HIGH 0x02
275 * Device and/or Interface Class codes
277 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
278 #define USB_CLASS_AUDIO 1
279 #define USB_CLASS_COMM 2
280 #define USB_CLASS_HID 3
281 #define USB_CLASS_PHYSICAL 5
282 #define USB_CLASS_PRINTER 7
283 #define USB_CLASS_MASS_STORAGE 8
284 #define USB_CLASS_HUB 9
285 #define USB_CLASS_DATA 10
286 #define USB_CLASS_APP_SPEC 0xfe
287 #define USB_CLASS_VENDOR_SPEC 0xff
292 #define USB_TYPE_MASK (0x03 << 5)
293 #define USB_TYPE_STANDARD (0x00 << 5)
294 #define USB_TYPE_CLASS (0x01 << 5)
295 #define USB_TYPE_VENDOR (0x02 << 5)
296 #define USB_TYPE_RESERVED (0x03 << 5)
301 #define USB_RECIP_MASK 0x1f
302 #define USB_RECIP_DEVICE 0x00
303 #define USB_RECIP_INTERFACE 0x01
304 #define USB_RECIP_ENDPOINT 0x02
305 #define USB_RECIP_OTHER 0x03
310 #define USB_DIR_OUT 0
311 #define USB_DIR_IN 0x80
316 #define USB_DT_DEVICE 0x01
317 #define USB_DT_CONFIG 0x02
318 #define USB_DT_STRING 0x03
319 #define USB_DT_INTERFACE 0x04
320 #define USB_DT_ENDPOINT 0x05
322 #define USB_DT_HID (USB_TYPE_CLASS | 0x01)
323 #define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)
324 #define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)
325 #define USB_DT_HUB (USB_TYPE_CLASS | 0x09)
328 * Descriptor sizes per descriptor type
330 #define USB_DT_DEVICE_SIZE 18
331 #define USB_DT_CONFIG_SIZE 9
332 #define USB_DT_INTERFACE_SIZE 9
333 #define USB_DT_ENDPOINT_SIZE 7
334 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
335 #define USB_DT_HUB_NONVAR_SIZE 7
336 #define USB_DT_HID_SIZE 9
341 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
342 #define USB_ENDPOINT_DIR_MASK 0x80
344 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
345 #define USB_ENDPOINT_XFER_CONTROL 0
346 #define USB_ENDPOINT_XFER_ISOC 1
347 #define USB_ENDPOINT_XFER_BULK 2
348 #define USB_ENDPOINT_XFER_INT 3
351 * USB Packet IDs (PIDs)
353 #define USB_PID_UNDEF_0 0xf0
354 #define USB_PID_OUT 0xe1
355 #define USB_PID_ACK 0xd2
356 #define USB_PID_DATA0 0xc3
357 #define USB_PID_PING 0xb4 /* USB 2.0 */
358 #define USB_PID_SOF 0xa5
359 #define USB_PID_NYET 0x96 /* USB 2.0 */
360 #define USB_PID_DATA2 0x87 /* USB 2.0 */
361 #define USB_PID_SPLIT 0x78 /* USB 2.0 */
362 #define USB_PID_IN 0x69
363 #define USB_PID_NAK 0x5a
364 #define USB_PID_DATA1 0x4b
365 #define USB_PID_PREAMBLE 0x3c /* Token mode */
366 #define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */
367 #define USB_PID_SETUP 0x2d
368 #define USB_PID_STALL 0x1e
369 #define USB_PID_MDATA 0x0f /* USB 2.0 */
374 #define USB_REQ_GET_STATUS 0x00
375 #define USB_REQ_CLEAR_FEATURE 0x01
376 #define USB_REQ_SET_FEATURE 0x03
377 #define USB_REQ_SET_ADDRESS 0x05
378 #define USB_REQ_GET_DESCRIPTOR 0x06
379 #define USB_REQ_SET_DESCRIPTOR 0x07
380 #define USB_REQ_GET_CONFIGURATION 0x08
381 #define USB_REQ_SET_CONFIGURATION 0x09
382 #define USB_REQ_GET_INTERFACE 0x0A
383 #define USB_REQ_SET_INTERFACE 0x0B
384 #define USB_REQ_SYNCH_FRAME 0x0C
389 #define USB_REQ_GET_REPORT 0x01
390 #define USB_REQ_GET_IDLE 0x02
391 #define USB_REQ_GET_PROTOCOL 0x03
392 #define USB_REQ_SET_REPORT 0x09
393 #define USB_REQ_SET_IDLE 0x0A
394 #define USB_REQ_SET_PROTOCOL 0x0B
397 #define HUB_REQ_GET_STATE 0x02
400 #define HUB_REQ_CLEAR_TT_BUFFER 0x08
403 typedef LONG USBD_STATUS
;
408 // Status values are 32 bit values layed out as follows:
410 // 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
411 // 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
412 // +---+---------------------------+-------------------------------+
413 // | S | Status Code |
414 // +---+---------------------------+-------------------------------+
418 // S - is the state code
420 // 00 - completed with success
421 // 01 - request is pending
422 // 10 - completed with error, endpoint not stalled
423 // 11 - completed with error, endpoint stalled
426 // Code - is the status code
430 // Generic test for success on any status value (non-negative numbers
431 // indicate success).
434 #define usb_success(Status) ((USBD_STATUS)(Status) >= 0)
437 // Generic test for pending status value.
440 #define usb_pending(Status) ((ULONG)(Status) >> 30 == 1)
443 // Generic test for error on any status value.
446 #define usb_error(Status) ((USBD_STATUS)(Status) < 0)
449 // Generic test for stall on any status value.
452 #define usb_halted(Status) ((ULONG)(Status) >> 30 == 3)
455 // Macro to check the status code only
458 #define usb_status(Status) ((ULONG)(Status) & 0x0FFFFFFFL)
461 #define USB_STATUS_SUCCESS ((USBD_STATUS)0x00000000L)
462 #define USB_STATUS_PENDING ((USBD_STATUS)0x40000000L)
463 #define USB_STATUS_HALTED ((USBD_STATUS)0xC0000000L)
464 #define USB_STATUS_ERROR ((USBD_STATUS)0x80000000L)
468 // Note: these status codes have both the error and the stall bit set.
470 #define USB_STATUS_CRC ((USBD_STATUS)0xC0000401L)
471 #define USB_STATUS_BTSTUFF ((USBD_STATUS)0xC0000402L)
472 #define USB_STATUS_DATA_TOGGLE_MISMATCH ((USBD_STATUS)0xC0000403L)
473 #define USB_STATUS_STALL_PID ((USBD_STATUS)0xC0000404L)
474 #define USB_STATUS_DEV_NOT_RESPONDING ((USBD_STATUS)0xC0000405L)
475 #define USB_STATUS_PID_CHECK_FAILURE ((USBD_STATUS)0xC0000406L)
476 #define USB_STATUS_UNEXPECTED_PID ((USBD_STATUS)0xC0000407L)
477 #define USB_STATUS_DATA_OVERRUN ((USBD_STATUS)0xC0000408L)
478 #define USB_STATUS_DATA_UNDERRUN ((USBD_STATUS)0xC0000409L)
479 #define USB_STATUS_RESERVED1 ((USBD_STATUS)0xC000040AL)
480 #define USB_STATUS_RESERVED2 ((USBD_STATUS)0xC000040BL)
481 #define USB_STATUS_BUFFER_OVERRUN ((USBD_STATUS)0xC000040CL)
482 #define USB_STATUS_BUFFER_UNDERRUN ((USBD_STATUS)0xC000040DL)
483 #define USB_STATUS_NOT_ACCESSED ((USBD_STATUS)0xC000040FL)
484 #define USB_STATUS_FIFO ((USBD_STATUS)0xC0000410L)
485 #define USB_STATUS_BABBLE_DETECTED ((USBD_STATUS)0xC0000408L)
488 // returned by HCD if a transfer is submitted to an endpoint that is
491 #define USB_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000430L)
494 // Software status codes
495 // Note: the following status codes have only the error bit set
497 #define USB_STATUS_NO_MEMORY ((USBD_STATUS)0x80000100L)
498 #define USB_STATUS_INVALID_URB_FUNCTION ((USBD_STATUS)0x80000200L)
499 #define USB_STATUS_INVALID_PARAMETER ((USBD_STATUS)0x80000300L)
502 // returned if client driver attempts to close an endpoint/interface
503 // or configuration with outstanding transfers.
505 #define USB_STATUS_ERROR_BUSY ((USBD_STATUS)0x80000400L)
507 // returned by USBD if it cannot complete a URB request, typically this
508 // will be returned in the URB status field when the Irp is completed
509 // with a more specific NT error code in the irp.status field.
511 #define USB_STATUS_REQUEST_FAILED ((USBD_STATUS)0x80000500L)
513 #define USB_STATUS_INVALID_PIPE_HANDLE ((USBD_STATUS)0x80000600L)
515 // returned when there is not enough bandwidth avialable
516 // to open a requested endpoint
517 #define USB_STATUS_NO_BANDWIDTH ((USBD_STATUS)0x80000700L)
521 #define USB_STATUS_INTERNAL_HC_ERROR ((USBD_STATUS)0x80000800L)
523 // returned when a short packet terminates the transfer
524 // ie USBD_SHORT_TRANSFER_OK bit not set
526 #define USB_STATUS_ERROR_SHORT_TRANSFER ((USBD_STATUS)0x80000900L)
528 // returned if the requested start frame is not within
529 // USBD_ISO_START_FRAME_RANGE of the current USB frame,
530 // note that the stall bit is set
532 #define USB_STATUS_BAD_START_FRAME ((USBD_STATUS)0xC0000A00L)
534 // returned by HCD if all packets in an iso transfer complete with an error
536 #define USB_STATUS_ISOCH_REQUEST_FAILED ((USBD_STATUS)0xC0000B00L)
538 // returned by USBD if the frame length control for a given
539 // HC is already taken by anothe driver
541 #define USB_STATUS_FRAME_CONTROL_OWNED ((USBD_STATUS)0xC0000C00L)
543 // returned by USBD if the caller does not own frame length control and
544 // attempts to release or modify the HC frame length
546 #define USB_STATUS_FRAME_CONTROL_NOT_OWNED ((USBD_STATUS)0xC0000D00L)
549 // set when a transfers is completed due to an AbortPipe request from
552 // Note: no error or stall bit is set for these status codes
554 #define USB_STATUS_CANCELED ((USBD_STATUS)0x00010000L)
556 #define USB_STATUS_CANCELING ((USBD_STATUS)0x00020000L)
558 // Device type -- in the "User Defined" range."
559 #define FILE_HCD_DEV_TYPE 45000
560 #define FILE_UHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 1 )
561 #define FILE_OHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 2 )
562 #define FILE_EHCI_DEV_TYPE ( FILE_HCD_DEV_TYPE + 3 )
563 #define FILE_USB_DEV_TYPE ( FILE_HCD_DEV_TYPE + 8 )
565 #define IOCTL_GET_DEV_COUNT CTL_CODE( FILE_HCD_DEV_TYPE, 4093, METHOD_BUFFERED, FILE_ANY_ACCESS )
566 //input_buffer and input_buffer_length is zero, output_buffer is to receive a dword value of the
567 //dev count, output_buffer_length must be no less than sizeof( unsigned long ).
569 #define IOCTL_ENUM_DEVICES CTL_CODE( FILE_HCD_DEV_TYPE, 4094, METHOD_BUFFERED, FILE_ANY_ACCESS )
570 //input_buffer is a dword value to indicate the count of elements in the array
571 //input_buffer_length is sizeof( unsigned long ), output_buffer is to receive a
572 //structure ENUM_DEV_ARRAY where dev_count is the elements hold in this array.
574 #define IOCTL_GET_DEV_DESC CTL_CODE( FILE_HCD_DEV_TYPE, 4095, METHOD_BUFFERED, FILE_ANY_ACCESS )
575 //input_buffer is a structure GET_DEV_DESC_REQ, and the input_buffer_length is
576 //no less than sizeof( input_buffer ), output_buffer is a buffer to receive the
577 //requested dev's desc, and output_buffer_length specifies the length of the
580 #define IOCTL_SUBMIT_URB_RD CTL_CODE( FILE_HCD_DEV_TYPE, 4096, METHOD_IN_DIRECT, FILE_ANY_ACCESS )
581 #define IOCTL_SUBMIT_URB_WR CTL_CODE( FILE_HCD_DEV_TYPE, 4097, METHOD_OUT_DIRECT, FILE_ANY_ACCESS )
582 // if the major_function is IRP_MJ_DEVICE_CONTROL
583 // input_buffer is a URB, and input_buffer_length is equal to or greater than
584 // sizeof( URB ); the output_buffer is a buffer to receive data from or send data
585 // to device. only the following urb fields can be accessed, others must be zeroed.
586 // DEV_HANDLE endp_handle;
587 // UCHAR setup_packet[8]; // for control pipe
588 // the choosing of IOCTL_SUBMIT_URB_RD or IOCTL_SUBMIT_URB_WR should be determined
589 // by the current URB, for example, a request string from device will use XXX_RD,
590 // and a write to the bulk endpoint will use XXX_WR
591 // if the major_function is IRP_MJ_INTERNAL_DEVICE_CONTROL
592 // input_buffer is a URB, and input_buffer_length is equal to or greater than
594 // only the following urb fields can be accessed, others must be zeroed.
595 // DEV_HANDLE endp_handle;
596 // UCHAR setup_packet[8]; // for control pipe, or zeroed
597 // PUCHAR data_buffer; // buffer for READ/WRITE
598 // ULONG data_length; // buffer size in bytes
600 #define IOCTL_SUBMIT_URB_NOIO CTL_CODE( FILE_HCD_DEV_TYPE, 4098, METHOD_BUFFERED, FILE_ANY_ACCESS )
601 // input_buffer is a URB, and input_buffer_length is equal to or greater than
602 // sizeof( URB ); the output_buffer is null and no output_buffer_length,
603 // only the following fields in urb can be accessed, others must be zeroed.
604 // DEV_HANDLE endp_handle;
605 // UCHAR setup_packet[8]; //for control pipe
606 // there is no difference between IRP_MJ_DEVICE_CONTROL and IRP_MJ_INTERNAL_DEVICE_CONTROL
607 #define IOCTL_GET_DEV_HANDLE CTL_CODE( FILE_HCD_DEV_TYPE, 4099, METHOD_BUFFERED, FILE_ANY_ACCESS )
608 // input_buffer is null ,and input_buffer_length is zero.
609 // output_buffer will hold the handle to this dev, output_buffer_length is 4
612 typedef ULONG DEV_HANDLE
, ENDP_HANDLE
, IF_HANDLE
;
615 #pragma pack( push, usb_align, 1 )
617 //structures for DeviceIoControl
618 typedef struct _ENUM_DEV_ELEMENT
620 DEV_HANDLE dev_handle
;
625 } ENUM_DEV_ELEMENT
, *PENUM_DEV_ELEMENT
;
627 typedef struct _ENUM_DEV_ARRAY
630 ENUM_DEV_ELEMENT dev_arr
[ 1 ];
632 } ENUM_DEV_ARRAY
, *PENUM_DEV_ARRAY
;
634 typedef struct _GET_DEV_DESC_REQ
636 DEV_HANDLE dev_handle
;
640 } GET_DEV_DESC_REQ
, *PGET_DEV_DESC_REQ
;
643 typedef struct _USB_CTRL_SETUP_PACKET
651 }USB_CTRL_SETUP_PACKET
, *PUSB_CTRL_SETUP_PACKET
;
653 typedef struct _USB_STRING_DESCRIPTOR
656 UCHAR bDescriptorType
;
659 } USB_STRING_DESCRIPTOR
, *PUSB_STRING_DESCRIPTOR
;
661 typedef struct _USB_DESC_HEADER
664 UCHAR bDescriptorType
;
666 } USB_DESC_HEADER
, *PUSB_DESC_HEADER
;
668 typedef struct _USB_ENDPOINT_DESC
671 UCHAR bDescriptorType
;
672 UCHAR bEndpointAddress
;
674 USHORT wMaxPacketSize
;
677 } USB_ENDPOINT_DESC
, *PUSB_ENDPOINT_DESC
;
679 typedef struct _USB_INTERFACE_DESC
682 UCHAR bDescriptorType
;
683 UCHAR bInterfaceNumber
;
684 UCHAR bAlternateSetting
;
686 UCHAR bInterfaceClass
;
687 UCHAR bInterfaceSubClass
;
688 UCHAR bInterfaceProtocol
;
691 } USB_INTERFACE_DESC
, *PUSB_INTERFACE_DESC
;
693 typedef struct _USB_CONFIGURATION_DESC
696 UCHAR bDescriptorType
;
698 UCHAR bNumInterfaces
;
699 UCHAR bConfigurationValue
;
700 UCHAR iConfiguration
;
704 } USB_CONFIGURATION_DESC
, *PUSB_CONFIGURATION_DESC
;
706 typedef struct _USB_DEVICE_DESC
709 UCHAR bDescriptorType
;
712 UCHAR bDeviceSubClass
;
713 UCHAR bDeviceProtocol
;
714 UCHAR bMaxPacketSize0
;
721 UCHAR bNumConfigurations
;
723 } USB_DEVICE_DESC
, *PUSB_DEVICE_DESC
;
726 #define URB_FLAG_STATE_MASK 0x0f
727 #define URB_FLAG_STATE_PENDING 0x00
728 #define URB_FLAG_STATE_IN_PROCESS 0x01
729 #define URB_FLAG_STATE_FINISHED 0x02
732 #define URB_FLAG_STATE_DOORBELL 0x03 // for async request removal
733 #define URB_FLAG_STATE_WAIT_FRAME 0x04 // for sync request removal( for cancel only )
734 #define URB_FLAG_STATE_ERROR 0x05
736 #define URB_FLAG_IN_SCHEDULE 0x10
737 #define URB_FLAG_FORCE_CANCEL 0x20
738 #define URB_FLAG_SHORT_PACKET 0x80000000
740 typedef struct _SPLIT_ISO_BUS_TIME
745 } SPLIT_ISO_BUS_TIME
, *PSPLIT_ISO_BUS_TIME
;
747 typedef struct _ISO_PACKET_DESC
750 LONG length
; // expected length
755 LONG bus_time
; //opaque for client request of split iso, the bus_time is the start_uframe for each transaction
756 SPLIT_ISO_BUS_TIME params
;
759 } ISO_PACKET_DESC
, *PISO_PACKET_DESC
;
761 #define CTRL_PARENT_URB_VALID 1
763 typedef void ( *PURBCOMPLETION
)( struct _URB
*purb
, PVOID pcontext
);
765 typedef struct _CTRL_REQ_STACK
767 PURBCOMPLETION urb_completion
; // the last step of the urb_completion is to call the
768 // the prevoius stack's callback if has, and the last
769 // one is purb->completion
773 } CTRL_REQ_STACK
, *PCTRL_REQ_STACK
;
775 #pragma pack( pop, usb_align )
777 typedef struct _URB_HS_PIPE_CONTENT
779 ULONG trans_type
: 2; // bit 0-1
780 ULONG mult_count
: 2; // bit 2-3, used in high speed int and iso requests
781 ULONG reserved
: 1; // bit 1
782 ULONG speed_high
: 1; // bit 5
783 ULONG speed_low
: 1; // bit 6
784 ULONG trans_dir
: 1; // bit 7
785 ULONG dev_addr
: 7; // bit 8-14
786 ULONG endp_addr
: 4; // bit 15-18
787 ULONG data_toggle
: 1; // bit 19
788 ULONG max_packet_size
: 4; // bit 20-23 log( max_packet_size )
789 ULONG interval
: 4; // bit 24-27 the same definition in USB2.0, for high or full/low speed
790 ULONG start_uframe
: 3; // bit 28-30
791 ULONG reserved1
: 1; //
793 } URB_HS_PIPE_CONTENT
, *PURB_HS_PIPE_CONTENT
;
795 typedef struct _URB_HS_CONTEXT_CONTENT
797 ULONG hub_addr
: 7; // high speed hub addr for split transfer
801 } URB_HS_CONTEXT_CONTENT
, *PURB_HS_CONTEXT_CONTENT
;
807 DEV_HANDLE endp_handle
;
809 //record info for isr use, similar to td.status
810 //int pipe has different content in the 8 msb
811 //the eight bits contain interrupt interval.
812 //and max packet length is encoded in 3 bits from 23-21
813 //that means 2^(x) bytes in the packet.
814 ULONG pipe
; // bit0-1: endp type, bit 6: ls or fs. bit 7: dir
818 UCHAR setup_packet
[8]; // for control
819 LONG params
[ 2 ]; // params[ 0 ] is used in iso transfer as max_packet_size
822 PUCHAR data_buffer
; //user data
823 LONG data_length
; //user data length
825 struct _USB_DEV
*pdev
;
826 struct _USB_ENDPOINT
*pendp
; //pipe for current transfer
828 PURBCOMPLETION completion
;
829 PVOID context
; //parameter of completion
831 PVOID urb_ext
; //for high speed hcd use
832 ULONG hs_context
; //for high speed hcd use
834 PIRP pirp
; //irp from client driver
835 LONG reference
; //for caller private use
837 LONG td_count
; //for any kinds of transfer
838 LONG rest_bytes
; //for split bulk transfer involving more than 1024 tds
839 LONG bytes_to_transfer
;
840 LONG bytes_transfered
; //( bulk transfer )accumulate one split-transfer by xfered bytes of the executed transactions
841 PLIST_ENTRY last_finished_td
; //last inactive td useful for large amount transfer
842 LIST_ENTRY trasac_list
; //list of tds or qhs
846 LONG iso_start_frame
; // for high speed endp, this is uframe index, and not used for full/low speed endp, instead,
847 // iso_packet_desc.param.start_uframe is used.
848 LONG int_start_frame
; // frame( ms ) index for high/full/low speed endp
851 UCHAR ctrl_req_flags
;
852 UCHAR ctrl_stack_count
;
853 UCHAR ctrl_cur_stack
; // the receiver uses this by increment the stack pointer first. if the requester
854 UCHAR ctrl_reserved
; // send it down with ctrl_stack_count zero, that means the stack is not initialized,
855 // and can be initialized by receiver to 1 and only 1.
856 // If the initializer found the stack size won't meet the number down the drivers, it must
857 // reallocate one urb with the required stack size. and store the previous urb in
864 LONG iso_frame_count
; // uframe for high speed and frame for full/low speed
865 struct _URB
* ctrl_parent_urb
;
870 ISO_PACKET_DESC iso_packet_desc
[ 1 ]; //used to build up trasac_list for iso transfer and claim bandwidth
871 CTRL_REQ_STACK ctrl_req_stack
[ 1 ];
907 struct _USB_DRIVER
*usb_drvr
910 //return reference to the endp
917 //return reference to the interface
925 usb_set_configuration(
930 // each call will return full size of the config desc and
931 // its if, endp descs.
932 // return value is the bytes actually returned.
933 // if the return value is equal to wTotalLength, all the descs of the
934 // configuration returned.
945 struct _USB_DEV_MANAGER
* dev_mgr
,
956 void usb_fill_int_urb(PURB urb
,
957 struct _USB_DEV
*dev
,
959 PVOID transfer_buffer
,
961 PURBCOMPLETION complete
,
973 //increment the dev->ref_count to lock the dev
975 usb_query_and_lock_dev(
976 struct _USB_DEV_MANAGER
* dev_mgr
,
977 DEV_HANDLE dev_handle
,
978 struct _USB_DEV
** ppdev
981 //decrement the dev->ref_count
989 struct _USB_DEV
*pdev
,
990 struct _USB_ENDPOINT
*pendp
,
991 PURBCOMPLETION client_completion
,
992 PVOID param
//parameter for client_completion
996 usb_reset_pipe_completion(
1003 POOL_TYPE pool_type
,
1012 PUSB_CONFIGURATION_DESC
1013 usb_find_config_desc_by_val(
1019 PUSB_CONFIGURATION_DESC
1020 usb_find_config_desc_by_idx(
1027 usb_skip_if_and_altif(
1032 usb_skip_one_config(
1033 PUCHAR
* pconfig_desc_BUF
1048 PLARGE_INTEGER clicks
1056 struct _USB_DEV_MANAGER
*dev_mgr
,
1057 DEV_HANDLE endp_handle
,
1058 PURBCOMPLETION reset_completion
,
1063 usb_call_ctrl_completion(
1071 ); //used to check descriptor validity