1 //
2 //    Copyright (C) Microsoft.  All rights reserved.
3 //
4 #ifndef __FX_USB_UTIL_H__
5 #define __FX_USB_UTIL_H__
6 
7 BOOLEAN
8 __inline
9 FxBitArraySet(
10     __inout_xcount((BitNumber / sizeof(UCHAR)) + 1) PUCHAR BitArray,
11     __in UCHAR  BitNumber
12     )
13 /*++
14 
15 Routine Description:
16     The following function sets a bit in the array and returns a value indicating
17     the bits previous state.
18 
19 Arguments:
20     BitArray - the array to check
21 
22     BitNumber - zero based index into BitArray
23 
24 Return Value:
25     TRUE if the bit was already set
26     FALSE if was previously clear
27 
28   --*/
29 {
30     UCHAR index;
31     UCHAR bit;
32 
33     index = (BitNumber) / sizeof(UCHAR);
34     bit = (UCHAR) (1 << (BitNumber % sizeof(UCHAR)) );
35 
36     if ((BitArray[index] & bit) == 0x0) {
37         //
38         // bit not set
39         //
40         BitArray[index] |= bit;
41         return FALSE;
42     }
43 
44     return TRUE;
45 }
46 
47 VOID
48 __inline
49 FxBitArrayClear(
50     __inout_xcount((BitNumber / sizeof(UCHAR)) + 1) PUCHAR BitArray,
51     __in UCHAR  BitNumber
52     )
53 /*++
54 
55 Routine Description:
56     Clear the bit in the BitArray
57 
58 Arguments:
59     BitArray - bit array to change
60 
61     BitNumber - zero based bit index into the array to clear
62 
63   --*/
64 {
65     UCHAR index;
66     UCHAR bit;
67 
68     index =  BitNumber / sizeof(UCHAR);
69     bit = (UCHAR) (1 << (BitNumber % sizeof(UCHAR)) );
70 
71     BitArray[index] &= ~bit;
72 }
73 
74 VOID
75 FxFormatUsbRequest(
76     __in FxRequestBase* Request,
77     __in PURB Urb,
78     __in FX_URB_TYPE FxUrbType,
79     __drv_when(FxUrbType == FxUrbTypeUsbdAllocated, __in)
80     __drv_when(FxUrbType != FxUrbTypeUsbdAllocated, __in_opt)
81          USBD_HANDLE UsbdHandle
82     );
83 
84 NTSTATUS
85 FxFormatUrbRequest(
86     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
87     __in FxIoTarget* Target,
88     __in FxRequestBase* Request,
89     __in FxRequestBuffer* Buffer,
90     __in FX_URB_TYPE FxUrbType,
91     __drv_when(FxUrbType == FxUrbTypeUsbdAllocated, __in)
92     __drv_when(FxUrbType != FxUrbTypeUsbdAllocated, __in_opt)
93          USBD_HANDLE UsbdHandle
94     );
95 
96 PUSB_INTERFACE_DESCRIPTOR
97 FxUsbParseConfigurationDescriptor(
98     __in PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
99     __in UCHAR InterfaceNumber = -1,
100     __in UCHAR AlternateSetting = 1
101     );
102 
103 PURB
104 FxUsbCreateConfigRequest(
105     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
106     __in PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
107     __in PUSBD_INTERFACE_LIST_ENTRY InterfaceList,
108     __in ULONG DefaultMaxPacketSize
109     );
110 
111 NTSTATUS
112 FxUsbValidateConfigDescriptorHeaders(
113     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
114     __in PUSB_CONFIGURATION_DESCRIPTOR ConfigDescriptor,
115     __in size_t ConfigDescriptorLength
116     );
117 
118 PUSB_COMMON_DESCRIPTOR
119 FxUsbFindDescriptorType(
120     __in PVOID Buffer,
121     __in size_t BufferLength,
122     __in PVOID Start,
123     __in LONG DescriptorType
124     );
125 
126 enum FxUsbValidateDescriptorOp {
127     FxUsbValidateDescriptorOpEqual,
128     FxUsbValidateDescriptorOpAtLeast
129 };
130 
131 NTSTATUS
132 FxUsbValidateDescriptorType(
133     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
134     __in PUSB_CONFIGURATION_DESCRIPTOR ConfigDescriptor,
135     __in PVOID Start,
136     __in PVOID End,
137     __in LONG DescriptorType,
138     __in size_t SizeToValidate,
139     __in FxUsbValidateDescriptorOp Op,
140     __in ULONG MaximumNumDescriptorsToValidate
141     );
142 
143 #if (FX_CORE_MODE == FX_CORE_USER_MODE)
144 VOID
145 FxUsbUmFormatRequest(
146     __in FxRequestBase* Request,
147     __in_xcount(Urb->Length) PUMURB_HEADER Urb,
148     __in IWudfFile* HostFile,
149     __in BOOLEAN Reuse = FALSE
150     );
151 
152 VOID
153 FxUsbUmInitDescriptorUrb(
154     __inout PUMURB UmUrb,
155     __in WINUSB_INTERFACE_HANDLE WinUsbHandle,
156     __in UCHAR DescriptorType,
157     __in ULONG BufferLength,
158     __in PVOID Buffer
159     );
160 
161 VOID
162 FxUsbUmInitControlTransferUrb(
163     __inout PUMURB UmUrb,
164     __in WINUSB_INTERFACE_HANDLE WinUsbHandle,
165     __in ULONG BufferLength,
166     __in PVOID Buffer
167     );
168 
169 VOID
170 FxUsbUmInitInformationUrb(
171     __inout PUMURB UmUrb,
172     __in WINUSB_INTERFACE_HANDLE WinUsbHandle,
173     __in ULONG BufferLength,
174     __in PVOID Buffer
175     );
176 #endif // (FX_CORE_MODE == FX_CORE_USER_MODE)
177 
178 #endif // __FX_USB_UTIL_H__
179