[CMAKE]
[reactos.git] / drivers / usb / nt4compat / usbdriver / usb.h
1 #ifndef __USBD_H__
2 #define __USBD_H__
3 /*
4 * Some USB bandwidth allocation constants.
5 */
6
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.) */
11
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)
16
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 */
21
22 #define ns_to_us(ns) ((ns + 500L) / 1000L)
23 /* convert & round nanoseconds to microseconds */
24
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 ) ) )
27
28 #define usb_make_ref( poinTER ) \
29 ( poinTER ^ 0xffffffff )
30
31 #define ptr_from_ref uhci_make_ref
32
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 )
36
37 #define endp_from_handle( pDEV, hanDLE, peNDP ) \
38 {\
39 LONG if_idx, endp_idx;\
40 BOOLEAN def_endp; \
41 endp_idx = endp_idx_from_handle( hanDLE );\
42 if_idx = if_idx_from_handle( hanDLE );\
43 def_endp = ( ( hanDLE & 0xffff ) == 0xffff ); \
44 if( def_endp ) \
45 peNDP = &pdev->default_endp; \
46 else \
47 { \
48 if( if_idx >= pdev->usb_config->if_count ) \
49 peNDP = NULL; \
50 else if( endp_idx >= pdev->usb_config->interf[ if_idx ].endp_count ) \
51 peNDP = NULL; \
52 else \
53 peNDP = &( pDEV )->usb_config->interf[ if_idx ].endp[ endp_idx ]; \
54 } \
55 }
56
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 ) )
61
62
63 //init work data for urb
64 #define urb_init( uRb ) \
65 {\
66 RtlZeroMemory( ( uRb ), sizeof( URB ) ); \
67 InitializeListHead( &( uRb )->trasac_list ); \
68 }
69
70 #define UsbBuildInterruptOrBulkTransferRequest(uRb, \
71 endp_hanDle, \
72 data_Buf, \
73 data_sIze, \
74 completIon, \
75 contExt, \
76 refereNce ) \
77 { \
78 urb_init( ( 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; \
85 }
86
87
88
89 #define UsbBuildGetDescriptorRequest(uRb, \
90 endp_hAndle, \
91 descriPtorType, \
92 descriPtorIndex, \
93 languaGeId, \
94 data_bUffer, \
95 data_sIze, \
96 compleTion, \
97 contexT, \
98 refereNce ) \
99 { \
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;\
114 }
115
116
117
118 #define UsbBuildGetStatusRequest(uRb, \
119 endp_hanDle, \
120 recipiEnt, \
121 inDex, \
122 transferBufFer, \
123 completIon, \
124 contExt, \
125 refereNce ) \
126 { \
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 ); \
138 pseTup->wValue = 0;\
139 pseTup->wLength = sizeof( USHORT );\
140 }
141
142
143 #define UsbBuildFeatureRequest(uRb, \
144 endp_hanDle,\
145 recipiEnt, \
146 featureSelecTor, \
147 inDex, \
148 completIon, \
149 contExt, \
150 refereNce ) \
151 { \
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;\
165 }
166
167 #define UsbBuildSelectConfigurationRequest(uRb, \
168 endp_hanDle,\
169 config_Val,\
170 completIon, \
171 contExt, \
172 refereNce ) \
173 { \
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 );\
185 pseTup->wIndex = 0;\
186 pseTup->wLength = 0;\
187 }
188
189 #define UsbBuildSelectInterfaceRequest(uRb, \
190 endp_hanDle,\
191 if_Num, \
192 alt_Num,\
193 completIon, \
194 contExt, \
195 refereNce ) \
196 { \
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;\
210 }
211
212
213 #define UsbBuildVendorRequest(uRb, \
214 endp_hanDle,\
215 data_bufFer, \
216 data_sIze, \
217 request_tYpe, \
218 requEst, \
219 vaLue, \
220 inDex, \
221 completIon, \
222 contExt, \
223 refereNce ) \
224 { \
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;\
238 }
239
240 #define UsbBuildResetPipeRequest(uRb, \
241 dev_hanDle, \
242 endp_aDdr, \
243 completIon, \
244 contExt, \
245 refereNce ) \
246 {\
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;\
256 }
257
258 // Forward structs declarations
259 struct _URB;
260 struct _HCD;
261 struct _USB_DEV_MANAGER;
262 struct _USB_DEV;
263 struct _USB_ENDPOINT;
264 struct _USB_EVENT;
265 struct _USB_EVENT_POOL;
266 struct _USB_DRIVER;
267
268 /* USB constants */
269
270 #define USB_SPEED_FULL 0x00
271 #define USB_SPEED_LOW 0x01
272 #define USB_SPEED_HIGH 0x02
273
274 /*
275 * Device and/or Interface Class codes
276 */
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
288
289 /*
290 * USB types
291 */
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)
297
298 /*
299 * USB recipients
300 */
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
306
307 /*
308 * USB directions
309 */
310 #define USB_DIR_OUT 0
311 #define USB_DIR_IN 0x80
312
313 /*
314 * Descriptor types
315 */
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
321
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)
326
327 /*
328 * Descriptor sizes per descriptor type
329 */
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
337
338 /*
339 * Endpoints
340 */
341 #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
342 #define USB_ENDPOINT_DIR_MASK 0x80
343
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
349
350 /*
351 * USB Packet IDs (PIDs)
352 */
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 */
370
371 /*
372 * Standard requests
373 */
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
385
386 /*
387 * HID requests
388 */
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
395
396 // HUB request
397 #define HUB_REQ_GET_STATE 0x02
398
399 // usb2.0 hub
400 #define HUB_REQ_CLEAR_TT_BUFFER 0x08
401
402
403 typedef LONG USBD_STATUS;
404
405 //
406 // USBD status codes
407 //
408 // Status values are 32 bit values layed out as follows:
409 //
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 // +---+---------------------------+-------------------------------+
415 //
416 // where
417 //
418 // S - is the state code
419 //
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
424 //
425 //
426 // Code - is the status code
427 //
428
429 //
430 // Generic test for success on any status value (non-negative numbers
431 // indicate success).
432 //
433
434 #define usb_success(Status) ((USBD_STATUS)(Status) >= 0)
435
436 //
437 // Generic test for pending status value.
438 //
439
440 #define usb_pending(Status) ((ULONG)(Status) >> 30 == 1)
441
442 //
443 // Generic test for error on any status value.
444 //
445
446 #define usb_error(Status) ((USBD_STATUS)(Status) < 0)
447
448 //
449 // Generic test for stall on any status value.
450 //
451
452 #define usb_halted(Status) ((ULONG)(Status) >> 30 == 3)
453
454 //
455 // Macro to check the status code only
456 //
457
458 #define usb_status(Status) ((ULONG)(Status) & 0x0FFFFFFFL)
459
460
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)
465
466 //
467 // HC status codes
468 // Note: these status codes have both the error and the stall bit set.
469 //
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)
486
487 //
488 // returned by HCD if a transfer is submitted to an endpoint that is
489 // stalled
490 //
491 #define USB_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000430L)
492
493 //
494 // Software status codes
495 // Note: the following status codes have only the error bit set
496 //
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)
500
501 //
502 // returned if client driver attempts to close an endpoint/interface
503 // or configuration with outstanding transfers.
504 //
505 #define USB_STATUS_ERROR_BUSY ((USBD_STATUS)0x80000400L)
506 //
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.
510 //
511 #define USB_STATUS_REQUEST_FAILED ((USBD_STATUS)0x80000500L)
512
513 #define USB_STATUS_INVALID_PIPE_HANDLE ((USBD_STATUS)0x80000600L)
514
515 // returned when there is not enough bandwidth avialable
516 // to open a requested endpoint
517 #define USB_STATUS_NO_BANDWIDTH ((USBD_STATUS)0x80000700L)
518 //
519 // generic HC error
520 //
521 #define USB_STATUS_INTERNAL_HC_ERROR ((USBD_STATUS)0x80000800L)
522 //
523 // returned when a short packet terminates the transfer
524 // ie USBD_SHORT_TRANSFER_OK bit not set
525 //
526 #define USB_STATUS_ERROR_SHORT_TRANSFER ((USBD_STATUS)0x80000900L)
527 //
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
531 //
532 #define USB_STATUS_BAD_START_FRAME ((USBD_STATUS)0xC0000A00L)
533 //
534 // returned by HCD if all packets in an iso transfer complete with an error
535 //
536 #define USB_STATUS_ISOCH_REQUEST_FAILED ((USBD_STATUS)0xC0000B00L)
537 //
538 // returned by USBD if the frame length control for a given
539 // HC is already taken by anothe driver
540 //
541 #define USB_STATUS_FRAME_CONTROL_OWNED ((USBD_STATUS)0xC0000C00L)
542 //
543 // returned by USBD if the caller does not own frame length control and
544 // attempts to release or modify the HC frame length
545 //
546 #define USB_STATUS_FRAME_CONTROL_NOT_OWNED ((USBD_STATUS)0xC0000D00L)
547
548 //
549 // set when a transfers is completed due to an AbortPipe request from
550 // the client driver
551 //
552 // Note: no error or stall bit is set for these status codes
553 //
554 #define USB_STATUS_CANCELED ((USBD_STATUS)0x00010000L)
555
556 #define USB_STATUS_CANCELING ((USBD_STATUS)0x00020000L)
557
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 )
564
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 ).
568
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.
573
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
578 //buffer
579
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
593 // sizeof( URB );
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
599
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
610 // or bigger
611
612 typedef ULONG DEV_HANDLE, ENDP_HANDLE, IF_HANDLE;
613
614 struct URB;
615 #pragma pack( push, usb_align, 1 )
616
617 //structures for DeviceIoControl
618 typedef struct _ENUM_DEV_ELEMENT
619 {
620 DEV_HANDLE dev_handle;
621 USHORT product_id;
622 USHORT vendor_id;
623 UCHAR dev_addr;
624
625 } ENUM_DEV_ELEMENT, *PENUM_DEV_ELEMENT;
626
627 typedef struct _ENUM_DEV_ARRAY
628 {
629 UCHAR dev_count;
630 ENUM_DEV_ELEMENT dev_arr[ 1 ];
631
632 } ENUM_DEV_ARRAY, *PENUM_DEV_ARRAY;
633
634 typedef struct _GET_DEV_DESC_REQ
635 {
636 DEV_HANDLE dev_handle;
637 UCHAR desc_type;
638 UCHAR desc_idx;
639
640 } GET_DEV_DESC_REQ, *PGET_DEV_DESC_REQ;
641
642 //usb definitions
643 typedef struct _USB_CTRL_SETUP_PACKET
644 {
645 UCHAR bmRequestType;
646 UCHAR bRequest;
647 USHORT wValue;
648 USHORT wIndex;
649 USHORT wLength;
650
651 }USB_CTRL_SETUP_PACKET, *PUSB_CTRL_SETUP_PACKET;
652
653 typedef struct _USB_STRING_DESCRIPTOR
654 {
655 UCHAR bLength;
656 UCHAR bDescriptorType;
657 USHORT wData[1];
658
659 } USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
660
661 typedef struct _USB_DESC_HEADER
662 {
663 UCHAR bLength;
664 UCHAR bDescriptorType;
665
666 } USB_DESC_HEADER, *PUSB_DESC_HEADER;
667
668 typedef struct _USB_ENDPOINT_DESC
669 {
670 UCHAR bLength;
671 UCHAR bDescriptorType;
672 UCHAR bEndpointAddress;
673 UCHAR bmAttributes;
674 USHORT wMaxPacketSize;
675 UCHAR bInterval;
676
677 } USB_ENDPOINT_DESC, *PUSB_ENDPOINT_DESC;
678
679 typedef struct _USB_INTERFACE_DESC
680 {
681 UCHAR bLength;
682 UCHAR bDescriptorType;
683 UCHAR bInterfaceNumber;
684 UCHAR bAlternateSetting;
685 UCHAR bNumEndpoints;
686 UCHAR bInterfaceClass;
687 UCHAR bInterfaceSubClass;
688 UCHAR bInterfaceProtocol;
689 UCHAR iInterface;
690
691 } USB_INTERFACE_DESC, *PUSB_INTERFACE_DESC;
692
693 typedef struct _USB_CONFIGURATION_DESC
694 {
695 UCHAR bLength;
696 UCHAR bDescriptorType;
697 USHORT wTotalLength;
698 UCHAR bNumInterfaces;
699 UCHAR bConfigurationValue;
700 UCHAR iConfiguration;
701 UCHAR bmAttributes;
702 UCHAR MaxPower;
703
704 } USB_CONFIGURATION_DESC, *PUSB_CONFIGURATION_DESC;
705
706 typedef struct _USB_DEVICE_DESC
707 {
708 UCHAR bLength;
709 UCHAR bDescriptorType;
710 USHORT bcdUSB;
711 UCHAR bDeviceClass;
712 UCHAR bDeviceSubClass;
713 UCHAR bDeviceProtocol;
714 UCHAR bMaxPacketSize0;
715 USHORT idVendor;
716 USHORT idProduct;
717 USHORT bcdDevice;
718 UCHAR iManufacturer;
719 UCHAR iProduct;
720 UCHAR iSerialNumber;
721 UCHAR bNumConfigurations;
722
723 } USB_DEVICE_DESC, *PUSB_DEVICE_DESC;
724
725
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
730
731 // USB2.0 state
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
735
736 #define URB_FLAG_IN_SCHEDULE 0x10
737 #define URB_FLAG_FORCE_CANCEL 0x20
738 #define URB_FLAG_SHORT_PACKET 0x80000000
739
740 typedef struct _SPLIT_ISO_BUS_TIME
741 {
742 USHORT bus_time;
743 USHORT start_uframe;
744
745 } SPLIT_ISO_BUS_TIME, *PSPLIT_ISO_BUS_TIME;
746
747 typedef struct _ISO_PACKET_DESC
748 {
749 LONG offset;
750 LONG length; // expected length
751 LONG actual_length;
752 LONG status;
753 union
754 {
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;
757 };
758
759 } ISO_PACKET_DESC, *PISO_PACKET_DESC;
760
761 #define CTRL_PARENT_URB_VALID 1
762
763 typedef void ( *PURBCOMPLETION )( struct _URB *purb, PVOID pcontext);
764
765 typedef struct _CTRL_REQ_STACK
766 {
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
770 PVOID context;
771 ULONG params[ 3 ];
772
773 } CTRL_REQ_STACK, *PCTRL_REQ_STACK;
774
775 #pragma pack( pop, usb_align )
776
777 typedef struct _URB_HS_PIPE_CONTENT
778 {
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; //
792
793 } URB_HS_PIPE_CONTENT, *PURB_HS_PIPE_CONTENT;
794
795 typedef struct _URB_HS_CONTEXT_CONTENT
796 {
797 ULONG hub_addr : 7; // high speed hub addr for split transfer
798 ULONG port_idx : 7;
799 ULONG reserved : 18;
800
801 } URB_HS_CONTEXT_CONTENT, *PURB_HS_CONTEXT_CONTENT;
802
803 typedef struct _URB
804 {
805 LIST_ENTRY urb_link;
806 ULONG flags;
807 DEV_HANDLE endp_handle;
808 LONG status;
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
815
816 union
817 {
818 UCHAR setup_packet[8]; // for control
819 LONG params[ 2 ]; // params[ 0 ] is used in iso transfer as max_packet_size
820 };
821
822 PUCHAR data_buffer; //user data
823 LONG data_length; //user data length
824
825 struct _USB_DEV *pdev;
826 struct _USB_ENDPOINT *pendp; //pipe for current transfer
827
828 PURBCOMPLETION completion;
829 PVOID context; //parameter of completion
830
831 PVOID urb_ext; //for high speed hcd use
832 ULONG hs_context; //for high speed hcd use
833
834 PIRP pirp; //irp from client driver
835 LONG reference; //for caller private use
836
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
843
844 union
845 {
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
849 struct
850 {
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
858 // ctrl_parent_urb
859 } ctrl_req_context;
860 };
861
862 union
863 {
864 LONG iso_frame_count; // uframe for high speed and frame for full/low speed
865 struct _URB* ctrl_parent_urb;
866 };
867
868 union
869 {
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 ];
872 };
873
874 } URB, *PURB;
875
876
877 NTSTATUS
878 usb_set_dev_ext(
879 ULONG dev_ref,
880 PVOID dev_ext,
881 LONG size
882 );
883
884 NTSTATUS
885 usb_set_if_ext(
886 ULONG dev_ref,
887 ULONG if_ref,
888 PVOID if_ext,
889 LONG size
890 );
891
892 PVOID
893 usb_get_dev_ext(
894 ULONG dev_ref
895 );
896
897 PVOID
898 usb_get_if_ext(
899 ULONG dev_ref,
900 ULONG if_ref
901 );
902
903 NTSTATUS
904 usb_claim_interface(
905 ULONG dev_ref,
906 ULONG if_ref,
907 struct _USB_DRIVER *usb_drvr
908 );
909
910 //return reference to the endp
911 ULONG
912 usb_get_endp(
913 ULONG dev_ref,
914 LONG endp_addr
915 );
916
917 //return reference to the interface
918 ULONG
919 usb_get_interface(
920 ULONG dev_ref,
921 LONG if_idx
922 );
923
924 NTSTATUS
925 usb_set_configuration(
926 ULONG dev_ref,
927 LONG config_value
928 );
929
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.
935 NTSTATUS
936 usb_get_config_desc(
937 ULONG dev_ref,
938 LONG config_idx,
939 PCHAR buffer,
940 PLONG psize
941 );
942
943 NTSTATUS
944 usb_submit_urb(
945 struct _USB_DEV_MANAGER* dev_mgr,
946 PURB purb
947 );
948
949 NTSTATUS
950 usb_cancel_urb(
951 ULONG dev_ref,
952 ULONG endp_ref,
953 PURB purb
954 );
955
956 void usb_fill_int_urb(PURB urb,
957 struct _USB_DEV *dev,
958 ULONG pipe,
959 PVOID transfer_buffer,
960 LONG buffer_length,
961 PURBCOMPLETION complete,
962 PVOID context,
963 int interval);
964
965 LONG
966 usb_calc_bus_time(
967 LONG low_speed,
968 LONG input_dir,
969 LONG isoc,
970 LONG bytecount
971 );
972
973 //increment the dev->ref_count to lock the dev
974 NTSTATUS
975 usb_query_and_lock_dev(
976 struct _USB_DEV_MANAGER* dev_mgr,
977 DEV_HANDLE dev_handle,
978 struct _USB_DEV** ppdev
979 );
980
981 //decrement the dev->ref_count
982 NTSTATUS
983 usb_unlock_dev(
984 struct _USB_DEV *dev
985 );
986
987 NTSTATUS
988 usb_reset_pipe(
989 struct _USB_DEV *pdev,
990 struct _USB_ENDPOINT *pendp,
991 PURBCOMPLETION client_completion,
992 PVOID param //parameter for client_completion
993 );
994
995 VOID
996 usb_reset_pipe_completion(
997 PURB purb,
998 PVOID pcontext
999 );
1000
1001 PVOID
1002 usb_alloc_mem(
1003 POOL_TYPE pool_type,
1004 LONG size
1005 );
1006
1007 VOID
1008 usb_free_mem(
1009 PVOID pbuf
1010 );
1011
1012 PUSB_CONFIGURATION_DESC
1013 usb_find_config_desc_by_val(
1014 PUCHAR pbuf,
1015 LONG val,
1016 LONG cfg_count
1017 );
1018
1019 PUSB_CONFIGURATION_DESC
1020 usb_find_config_desc_by_idx(
1021 PUCHAR pbuf,
1022 LONG idx,
1023 LONG cfg_count
1024 );
1025
1026 BOOLEAN
1027 usb_skip_if_and_altif(
1028 PUCHAR* pdesc_BUF
1029 );
1030
1031 BOOLEAN
1032 usb_skip_one_config(
1033 PUCHAR* pconfig_desc_BUF
1034 );
1035
1036 VOID
1037 usb_wait_ms_dpc(
1038 ULONG ms
1039 );
1040
1041 VOID
1042 usb_wait_us_dpc(
1043 ULONG us
1044 );
1045
1046 BOOLEAN
1047 usb_query_clicks(
1048 PLARGE_INTEGER clicks
1049 );
1050
1051 VOID
1052 usb_cal_cpu_freq();
1053
1054 NTSTATUS
1055 usb_reset_pipe_ex(
1056 struct _USB_DEV_MANAGER *dev_mgr,
1057 DEV_HANDLE endp_handle,
1058 PURBCOMPLETION reset_completion,
1059 PVOID param
1060 );
1061
1062 VOID
1063 usb_call_ctrl_completion(
1064 PURB purb
1065 );
1066
1067 BOOLEAN
1068 is_header_match(
1069 PUCHAR pbuf,
1070 ULONG type
1071 ); //used to check descriptor validity
1072 #endif