win-pvdrivers

view xenusb/xenusb_decode.c @ 965:63bdd096d8d1

Fix KeInitializeCrashDumpHeader - buffer was too small under newer systems
author James Harper <james.harper@bendigoit.com.au>
date Tue Jan 10 12:11:30 2012 +1100 (2012-01-10)
parents 19d7c9dd9f5c
children 1306945ecc59
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenusb.h"
22 #define DECODE_COMPUTE 0x40000000 /* calculate the value - not applicable to all fields */
23 #define DECODE_COPY 0x80000000 /* copy from URB */
24 /* otherwise literal value */
26 typedef struct {
27 PCHAR urb_function_name;
28 BOOLEAN is_simple_control;
29 ULONG bmRequestTypeRecipient;
30 ULONG bmRequestTypeType;
31 ULONG bmRequestTypeDir;
32 ULONG bRequest;
33 ULONG wValueLow;
34 ULONG wValueHigh;
35 ULONG wIndexLow;
36 ULONG wIndexHigh;
37 ULONG wLength;
38 ULONG transfer_flags;
39 } decode_t;
41 static decode_t decodes[] = {
42 /* 0000 */
43 {"URB_FUNCTION_SELECT_CONFIGURATION", FALSE},
44 {"URB_FUNCTION_SELECT_INTERFACE", FALSE},
45 {"URB_FUNCTION_ABORT_PIPE", FALSE},
46 {"URB_FUNCTION_TAKE_FRAME_LENGTH_CONTROL", FALSE},
47 {"URB_FUNCTION_RELEASE_FRAME_LENGTH_CONTROL", FALSE},
48 {"URB_FUNCTION_GET_FRAME_LENGTH", FALSE},
49 {"URB_FUNCTION_SET_FRAME_LENGTH", FALSE},
50 {"URB_FUNCTION_GET_CURRENT_FRAME_NUMBER", FALSE},
51 {"URB_FUNCTION_CONTROL_TRANSFER", TRUE, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
52 {"URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER", FALSE},
53 {"URB_FUNCTION_ISOCH_TRANSFER", FALSE},
54 {"URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE", TRUE, BMREQUEST_TO_DEVICE, BMREQUEST_STANDARD, BMREQUEST_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
55 {"URB_FUNCTION_SET_DESCRIPTOR_TO_DEVICE", FALSE},
56 {"URB_FUNCTION_SET_FEATURE_TO_DEVICE", FALSE},
57 {"URB_FUNCTION_SET_FEATURE_TO_INTERFACE", FALSE},
58 {"URB_FUNCTION_SET_FEATURE_TO_ENDPOINT", FALSE},
59 /* 0010 */
60 {"URB_FUNCTION_CLEAR_FEATURE_TO_DEVICE", FALSE},
61 {"URB_FUNCTION_CLEAR_FEATURE_TO_INTERFACE", FALSE},
62 {"URB_FUNCTION_CLEAR_FEATURE_TO_ENDPOINT", FALSE},
63 {"URB_FUNCTION_GET_STATUS_FROM_DEVICE", FALSE},
64 {"URB_FUNCTION_GET_STATUS_FROM_INTERFACE", FALSE},
65 {"URB_FUNCTION_GET_STATUS_FROM_ENDPOINT", FALSE},
66 {"URB_FUNCTION_RESERVED_0X0016", FALSE},
67 {"URB_FUNCTION_VENDOR_DEVICE", TRUE, BMREQUEST_TO_DEVICE, BMREQUEST_VENDOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
68 {"URB_FUNCTION_VENDOR_INTERFACE", TRUE, BMREQUEST_TO_INTERFACE, BMREQUEST_VENDOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
69 {"URB_FUNCTION_VENDOR_ENDPOINT", TRUE, BMREQUEST_TO_ENDPOINT, BMREQUEST_VENDOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
70 {"URB_FUNCTION_CLASS_DEVICE", TRUE, BMREQUEST_TO_DEVICE, BMREQUEST_CLASS, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
71 {"URB_FUNCTION_CLASS_INTERFACE", TRUE, BMREQUEST_TO_INTERFACE, BMREQUEST_CLASS, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
72 {"URB_FUNCTION_CLASS_ENDPOINT", TRUE, BMREQUEST_TO_ENDPOINT, BMREQUEST_CLASS, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
73 {"URB_FUNCTION_RESERVE_0X001D", FALSE},
74 {"URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL", FALSE},
75 {"URB_FUNCTION_CLASS_OTHER", TRUE, BMREQUEST_TO_OTHER, BMREQUEST_CLASS, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
76 /* 0020 */
77 {"URB_FUNCTION_VENDOR_OTHER", TRUE, BMREQUEST_TO_OTHER, BMREQUEST_VENDOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
78 {"URB_FUNCTION_GET_STATUS_FROM_OTHER", FALSE},
79 {"URB_FUNCTION_CLEAR_FEATURE_TO_OTHER", FALSE},
80 {"URB_FUNCTION_SET_FEATURE_TO_OTHER", FALSE},
81 {"URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT", TRUE, BMREQUEST_TO_ENDPOINT, BMREQUEST_STANDARD, BMREQUEST_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
82 {"URB_FUNCTION_SET_DESCRIPTOR_TO_ENDPOINT", FALSE},
83 {"URB_FUNCTION_GET_CONFIGURATION", FALSE},
84 {"URB_FUNCTION_GET_INTERFACE", FALSE},
85 {"URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE", TRUE, BMREQUEST_TO_INTERFACE, BMREQUEST_STANDARD, BMREQUEST_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
86 {"URB_FUNCTION_SET_DESCRIPTOR_TO_INTERFACE", FALSE},
87 {"URB_FUNCTION_GET_MS_FEATURE_DESCRIPTOR", FALSE},
88 {"URB_FUNCTION_RESERVE_0X002B", FALSE},
89 {"URB_FUNCTION_RESERVE_0X002C", FALSE},
90 {"URB_FUNCTION_RESERVE_0X002D", FALSE},
91 {"URB_FUNCTION_RESERVE_0X002E", FALSE},
92 {"URB_FUNCTION_RESERVE_0X002F", FALSE},
93 /* 0030 */
94 {"URB_FUNCTION_SYNC_RESET_PIPE", FALSE},
95 {"URB_FUNCTION_SYNC_CLEAR_STALL", FALSE},
96 {"URB_FUNCTION_CONTROL_TRANSFER_EX", TRUE, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COPY, DECODE_COMPUTE, DECODE_COMPUTE},
97 {"URB_FUNCTION_RESERVE_0X0033", FALSE},
98 {"URB_FUNCTION_RESERVE_0X0034", FALSE},
99 };
101 /*
102 decode all the funky URB_Xxx functions into a basic 8 byte SetupPacket
103 */
104 ULONG
105 XenUsb_DecodeControlUrb(PURB urb, urb_decode_t *decode_data)
106 {
107 ULONG retval;
108 decode_t *decode;
109 PUSB_DEFAULT_PIPE_SETUP_PACKET setup_packet;
111 if (urb->UrbHeader.Function > ARRAY_SIZE(decodes)) {
112 FUNCTION_MSG("Unknown URB_FUNCTION_%04x\n", urb->UrbHeader.Function);
113 return URB_DECODE_UNKNOWN;
114 }
115 decode = &decodes[urb->UrbHeader.Function];
116 FUNCTION_MSG("decoding %s\n", decode->urb_function_name);
118 if (decode->is_simple_control) {
119 FUNCTION_MSG("is a simple control URB\n");
121 setup_packet = (PUSB_DEFAULT_PIPE_SETUP_PACKET)urb->UrbControlTransfer.SetupPacket;
123 if (decode->bmRequestTypeRecipient == DECODE_COPY)
124 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = setup_packet->bmRequestType.Recipient;
125 else
126 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = (UCHAR)decode->bmRequestTypeRecipient;
127 if (decode->bmRequestTypeType == DECODE_COPY)
128 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = setup_packet->bmRequestType.Type;
129 else
130 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = (UCHAR)decode->bmRequestTypeType;
131 if (decode->bmRequestTypeDir == DECODE_COPY)
132 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = setup_packet->bmRequestType.Dir;
133 else
134 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = (UCHAR)decode->bmRequestTypeDir;
136 if (decode->bRequest == DECODE_COPY)
137 decode_data->setup_packet.default_pipe_setup_packet.bRequest = setup_packet->bRequest;
138 else
139 decode_data->setup_packet.default_pipe_setup_packet.bRequest = (UCHAR)decode->bRequest;
141 if (decode->wValueLow == DECODE_COPY)
142 decode_data->setup_packet.default_pipe_setup_packet.wValue.LowByte = setup_packet->wValue.LowByte;
143 else
144 decode_data->setup_packet.default_pipe_setup_packet.wValue.LowByte = (UCHAR)decode->wValueLow;
146 if (decode->wValueHigh == DECODE_COPY)
147 decode_data->setup_packet.default_pipe_setup_packet.wValue.HiByte = setup_packet->wValue.HiByte;
148 else
149 decode_data->setup_packet.default_pipe_setup_packet.wValue.HiByte = (UCHAR)decode->wValueHigh;
151 if (decode->wIndexLow == DECODE_COPY)
152 decode_data->setup_packet.default_pipe_setup_packet.wIndex.LowByte = setup_packet->wIndex.LowByte;
153 else
154 decode_data->setup_packet.default_pipe_setup_packet.wIndex.LowByte = (UCHAR)decode->wIndexLow;
156 if (decode->wIndexHigh == DECODE_COPY)
157 decode_data->setup_packet.default_pipe_setup_packet.wIndex.HiByte = setup_packet->wIndex.HiByte;
158 else
159 decode_data->setup_packet.default_pipe_setup_packet.wIndex.HiByte = (UCHAR)decode->wIndexHigh;
161 if (decode->wLength == DECODE_COMPUTE)
162 /* use buffer length */
163 decode_data->setup_packet.default_pipe_setup_packet.wLength = (USHORT)urb->UrbControlTransfer.TransferBufferLength;
164 else if (decode->wLength == DECODE_COPY)
165 decode_data->setup_packet.default_pipe_setup_packet.wLength = setup_packet->wLength;
166 else
167 decode_data->setup_packet.default_pipe_setup_packet.wLength = (UCHAR)decode->wLength;
169 if (decode->transfer_flags == DECODE_COMPUTE) {
170 /* Fix up transfer_flags based on direction in bmRequest */
171 if (decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir == BMREQUEST_DEVICE_TO_HOST)
172 decode_data->transfer_flags = USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK;
173 else
174 decode_data->transfer_flags = 0;
175 } else if (decode->transfer_flags == DECODE_COPY) {
176 decode_data->transfer_flags = urb->UrbControlTransfer.TransferFlags;
177 } else {
178 decode_data->transfer_flags = decode->transfer_flags;
179 }
182 decode_data->buffer = urb->UrbControlTransfer.TransferBuffer;
183 decode_data->mdl = urb->UrbControlTransfer.TransferBufferMDL;
184 decode_data->length = &urb->UrbControlTransfer.TransferBufferLength;
186 return URB_DECODE_COMPLETE;
187 }
189 //FUNCTION_ENTER();
190 switch(urb->UrbHeader.Function)
191 {
192 case URB_FUNCTION_SELECT_CONFIGURATION:
193 KdPrint((__DRIVER_NAME " URB_FUNCTION_SELECT_CONFIGURATION\n"));
194 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = BMREQUEST_TO_DEVICE;
195 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = BMREQUEST_STANDARD;
196 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = BMREQUEST_HOST_TO_DEVICE;
197 decode_data->setup_packet.default_pipe_setup_packet.bRequest = USB_REQUEST_SET_CONFIGURATION;
198 decode_data->setup_packet.default_pipe_setup_packet.wLength = 0;
199 decode_data->setup_packet.default_pipe_setup_packet.wValue.W = urb->UrbSelectConfiguration.ConfigurationDescriptor->bConfigurationValue;
200 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = 0;
201 decode_data->transfer_flags = 0;
202 decode_data->buffer = NULL;
203 decode_data->mdl = NULL;
204 retval = URB_DECODE_INCOMPLETE;
205 break;
206 case URB_FUNCTION_SELECT_INTERFACE:
207 KdPrint((__DRIVER_NAME " URB_FUNCTION_SELECT_INTERFACE\n"));
208 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = BMREQUEST_TO_INTERFACE;
209 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = BMREQUEST_STANDARD;
210 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = BMREQUEST_HOST_TO_DEVICE;
211 decode_data->setup_packet.default_pipe_setup_packet.bRequest = USB_REQUEST_SET_INTERFACE;
212 decode_data->setup_packet.default_pipe_setup_packet.wLength = 0;
213 decode_data->setup_packet.default_pipe_setup_packet.wValue.W = urb->UrbSelectInterface.Interface.AlternateSetting;
214 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = urb->UrbSelectInterface.Interface.InterfaceNumber;
215 decode_data->transfer_flags = 0;
216 decode_data->buffer = NULL;
217 decode_data->mdl = NULL;
218 retval = URB_DECODE_INCOMPLETE;
219 break;
220 #if 0
221 case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
222 KdPrint((__DRIVER_NAME " URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE\n"));
223 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = BMREQUEST_TO_DEVICE;
224 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = BMREQUEST_STANDARD;
225 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = BMREQUEST_DEVICE_TO_HOST;
226 decode_data->setup_packet.default_pipe_setup_packet.bRequest = USB_REQUEST_GET_DESCRIPTOR;
227 decode_data->setup_packet.default_pipe_setup_packet.wValue.LowByte = urb->UrbControlDescriptorRequest.Index;
228 decode_data->setup_packet.default_pipe_setup_packet.wValue.HiByte = urb->UrbControlDescriptorRequest.DescriptorType;
229 switch(urb->UrbControlDescriptorRequest.DescriptorType)
230 {
231 case USB_STRING_DESCRIPTOR_TYPE:
232 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = urb->UrbControlDescriptorRequest.LanguageId;
233 break;
234 default:
235 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = 0;
236 break;
237 }
238 decode_data->setup_packet.default_pipe_setup_packet.wLength = (USHORT)urb->UrbControlDescriptorRequest.TransferBufferLength;
239 decode_data->transfer_flags = USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK;
240 decode_data->buffer = urb->UrbControlTransfer.TransferBuffer;
241 decode_data->mdl = urb->UrbControlTransfer.TransferBufferMDL;
242 decode_data->length = &urb->UrbControlTransfer.TransferBufferLength;
243 retval = URB_DECODE_COMPLETE;
244 break;
245 case URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE:
246 KdPrint((__DRIVER_NAME " URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE\n"));
247 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = BMREQUEST_TO_INTERFACE;
248 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = BMREQUEST_STANDARD;
249 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = BMREQUEST_DEVICE_TO_HOST;
250 decode_data->setup_packet.default_pipe_setup_packet.bRequest = USB_REQUEST_GET_DESCRIPTOR;
251 decode_data->setup_packet.default_pipe_setup_packet.wValue.LowByte = urb->UrbControlDescriptorRequest.Index;
252 decode_data->setup_packet.default_pipe_setup_packet.wValue.HiByte = urb->UrbControlDescriptorRequest.DescriptorType;
253 switch(urb->UrbControlDescriptorRequest.DescriptorType)
254 {
255 case USB_STRING_DESCRIPTOR_TYPE:
256 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = urb->UrbControlDescriptorRequest.LanguageId;
257 break;
258 default:
259 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = 0;
260 break;
261 }
262 decode_data->setup_packet.default_pipe_setup_packet.wLength = (USHORT)urb->UrbControlDescriptorRequest.TransferBufferLength;
263 decode_data->transfer_flags = USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK;
264 decode_data->buffer = urb->UrbControlTransfer.TransferBuffer;
265 decode_data->mdl = urb->UrbControlTransfer.TransferBufferMDL;
266 decode_data->length = &urb->UrbControlTransfer.TransferBufferLength;
267 retval = URB_DECODE_COMPLETE;
268 break;
269 case URB_FUNCTION_CLASS_DEVICE: /* CONTROL_TRANSFER has same underlying format as FUNCTION_CLASS_XXX */
270 case URB_FUNCTION_CLASS_OTHER:
271 case URB_FUNCTION_CLASS_INTERFACE: /* not yet tested */
272 //case URB_FUNCTION_GET_STATUS_FROM_DEVICE: // seems to be missing fields...
273 case URB_FUNCTION_CONTROL_TRANSFER:
274 #if (NTDDI_VERSION >= NTDDI_VISTA)
275 case URB_FUNCTION_CONTROL_TRANSFER_EX:
276 #endif
277 decode_data->buffer = urb->UrbControlTransfer.TransferBuffer;
278 decode_data->mdl = urb->UrbControlTransfer.TransferBufferMDL;
279 decode_data->length = &urb->UrbControlTransfer.TransferBufferLength;
280 decode_data->transfer_flags = urb->UrbControlTransfer.TransferFlags;
281 memcpy(decode_data->setup_packet.raw, urb->UrbControlTransfer.SetupPacket, sizeof(decode_data->setup_packet.raw));
282 retval = URB_DECODE_COMPLETE;
283 break;
284 case URB_FUNCTION_GET_STATUS_FROM_DEVICE:
285 KdPrint((__DRIVER_NAME " URB_FUNCTION_GET_STATUS_FROM_DEVICE\n"));
286 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Recipient = BMREQUEST_TO_DEVICE;
287 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Type = BMREQUEST_STANDARD;
288 decode_data->setup_packet.default_pipe_setup_packet.bmRequestType.Dir = BMREQUEST_DEVICE_TO_HOST;
289 decode_data->setup_packet.default_pipe_setup_packet.bRequest = USB_REQUEST_GET_STATUS;
290 decode_data->setup_packet.default_pipe_setup_packet.wLength = 0;
291 decode_data->setup_packet.default_pipe_setup_packet.wValue.W = 0;
292 decode_data->setup_packet.default_pipe_setup_packet.wIndex.W = urb->UrbControlGetStatusRequest.Index;
293 decode_data->buffer = urb->UrbControlTransfer.TransferBuffer;
294 decode_data->mdl = urb->UrbControlTransfer.TransferBufferMDL;
295 decode_data->length = &urb->UrbControlTransfer.TransferBufferLength;
296 decode_data->transfer_flags = urb->UrbControlTransfer.TransferFlags;
297 retval = URB_DECODE_COMPLETE;
298 break;
299 #if 0
300 case URB_FUNCTION_GET_STATUS_FROM_DEVICE:
301 KdPrint((__DRIVER_NAME " URB_FUNCTION_GET_STATUS_FROM_DEVICE\n"));
302 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlGetStatusRequest.TransferBufferLength));
303 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlGetStatusRequest.TransferBuffer));
304 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlGetStatusRequest.TransferBufferMDL));
305 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlGetStatusRequest.Index));
306 if (urb->UrbControlGetStatusRequest.Index == 0)
307 {
308 urb->UrbControlGetStatusRequest.TransferBufferLength = 2;
309 *(PUSHORT)urb->UrbControlGetStatusRequest.TransferBuffer = 0x0003;
310 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
311 WdfRequestComplete(request, STATUS_SUCCESS);
312 }
313 else
314 {
315 KdPrint((__DRIVER_NAME " Unknown Index\n"));
316 urb->UrbHeader.Status = USBD_STATUS_INVALID_URB_FUNCTION;
317 WdfRequestComplete(request, STATUS_UNSUCCESSFUL); //STATUS_UNSUCCESSFUL);
318 }
319 break;
320 case URB_FUNCTION_CLASS_DEVICE:
321 #if 1
322 KdPrint((__DRIVER_NAME " URB_FUNCTION_CLASS_DEVICE\n"));
323 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
324 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
325 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
326 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
327 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
328 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
329 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
330 #endif
331 switch (urb->UrbControlVendorClassRequest.Request)
332 {
333 case USB_REQUEST_GET_DESCRIPTOR:
334 KdPrint((__DRIVER_NAME " URB_FUNCTION_CLASS_DEVICE\n"));
335 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
336 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
337 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
338 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
339 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
340 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
341 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
342 KdPrint((__DRIVER_NAME " USB_REQUEST_GET_DESCRIPTOR\n"));
343 switch (urb->UrbControlVendorClassRequest.Value >> 8)
344 {
345 case 0x00:
346 #if 0
347 memcpy(urb->UrbControlVendorClassRequest.TransferBuffer, &usb_device->device_descriptor, sizeof(USB_DEVICE_DESCRIPTOR));
348 urb->UrbControlVendorClassRequest.TransferBufferLength = sizeof(USB_DEVICE_DESCRIPTOR);
349 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
350 WdfRequestComplete(request, STATUS_SUCCESS);
351 break;
352 #endif
353 case 0x29: // Hub Descriptor
354 KdPrint((__DRIVER_NAME " HUB_DESCRIPTOR\n"));
355 uhd = urb->UrbControlVendorClassRequest.TransferBuffer;
356 urb->UrbControlVendorClassRequest.TransferBufferLength = FIELD_OFFSET(USB_HUB_DESCRIPTOR, bRemoveAndPowerMask[0]) + 2 + 1;
357 uhd->bDescriptorLength = (UCHAR)urb->UrbControlVendorClassRequest.TransferBufferLength;
358 uhd->bDescriptorType = 0x29;
359 uhd->bNumberOfPorts = 8;
360 uhd->wHubCharacteristics = 0x0012; // no power switching no overcurrent protection
361 uhd->bPowerOnToPowerGood = 1; // 2ms units
362 uhd->bHubControlCurrent = 0;
363 // DeviceRemovable bits (includes an extra bit at the start)
364 uhd->bRemoveAndPowerMask[0] = 0;
365 uhd->bRemoveAndPowerMask[1] = 0;
366 // PortPwrCtrlMask
367 uhd->bRemoveAndPowerMask[2] = 0xFF;
368 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
369 WdfRequestComplete(request, STATUS_SUCCESS);
370 break;
371 default:
372 KdPrint((__DRIVER_NAME " Unknown Value %02x\n", urb->UrbControlVendorClassRequest.Value >> 8));
373 urb->UrbHeader.Status = USBD_STATUS_INVALID_URB_FUNCTION;
374 WdfRequestComplete(request, STATUS_UNSUCCESSFUL); //STATUS_UNSUCCESSFUL);
375 break;
376 }
377 break;
378 case USB_REQUEST_GET_STATUS:
379 KdPrint((__DRIVER_NAME " TransferFlags = %08x\n", urb->UrbControlVendorClassRequest.TransferFlags));
380 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
381 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
382 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
383 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
384 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
385 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
386 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
387 KdPrint((__DRIVER_NAME " USB_REQUEST_GET_STATUS\n"));
388 // Check that RequestTypeReservedBits == 0xA0
389 KdPrint((__DRIVER_NAME " GetHubStatus\n"));
390 /* hub status */
391 // shoud be able to get this field from somewhere else...
392 ((PUSHORT)urb->UrbControlVendorClassRequest.TransferBuffer)[0] = 0x0000;
393 ((PUSHORT)urb->UrbControlVendorClassRequest.TransferBuffer)[1] = 0x0000; /* no change occurred */
394 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
395 WdfRequestComplete(request, STATUS_SUCCESS);
396 break;
397 case USB_REQUEST_CLEAR_FEATURE:
398 KdPrint((__DRIVER_NAME " TransferFlags = %08x\n", urb->UrbControlVendorClassRequest.TransferFlags));
399 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
400 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
401 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
402 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
403 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
404 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
405 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
406 KdPrint((__DRIVER_NAME " USB_REQUEST_CLEAR_FEATURE\n"));
407 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
408 WdfRequestComplete(request, STATUS_SUCCESS);
409 break;
410 default:
411 KdPrint((__DRIVER_NAME " TransferFlags = %08x\n", urb->UrbControlVendorClassRequest.TransferFlags));
412 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
413 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
414 KdPrint((__DRIVER_NAME " TransferBufferMDL = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
415 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
416 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
417 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
418 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
419 KdPrint((__DRIVER_NAME " USB_REQUEST_%02x\n", urb->UrbControlVendorClassRequest.Request));
420 urb->UrbHeader.Status = USBD_STATUS_INVALID_URB_FUNCTION;
421 WdfRequestComplete(request, STATUS_UNSUCCESSFUL); //STATUS_UNSUCCESSFUL);
422 break;
423 }
424 break;
425 case URB_FUNCTION_CLASS_OTHER:
426 KdPrint((__DRIVER_NAME " URB_FUNCTION_CLASS_OTHER\n"));
427 KdPrint((__DRIVER_NAME " TransferFlags = %08x\n", urb->UrbControlVendorClassRequest.TransferFlags));
428 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbControlVendorClassRequest.TransferBufferLength));
429 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbControlVendorClassRequest.TransferBuffer));
430 KdPrint((__DRIVER_NAME " TransferBufferMdl = %p\n", urb->UrbControlVendorClassRequest.TransferBufferMDL));
431 KdPrint((__DRIVER_NAME " RequestTypeReservedBits = %02x\n", urb->UrbControlVendorClassRequest.RequestTypeReservedBits));
432 KdPrint((__DRIVER_NAME " Request = %02x\n", urb->UrbControlVendorClassRequest.Request));
433 KdPrint((__DRIVER_NAME " Value = %04x\n", urb->UrbControlVendorClassRequest.Value));
434 KdPrint((__DRIVER_NAME " Index = %04x\n", urb->UrbControlVendorClassRequest.Index));
435 switch (urb->UrbControlVendorClassRequest.Request)
436 {
437 case USB_REQUEST_GET_STATUS:
438 /* port status - 11.24.2.7.1 */
439 KdPrint((__DRIVER_NAME " USB_REQUEST_GET_STATUS\n"));
440 KdPrint((__DRIVER_NAME " GetHubStatus\n"));
441 ((PUSHORT)urb->UrbControlVendorClassRequest.TransferBuffer)[0] = xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_status;
442 ((PUSHORT)urb->UrbControlVendorClassRequest.TransferBuffer)[1] = xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change;
443 break;
444 case USB_REQUEST_SET_FEATURE:
445 KdPrint((__DRIVER_NAME " USB_REQUEST_SET_FEATURE\n"));
446 KdPrint((__DRIVER_NAME " SetPortFeature\n"));
447 switch (urb->UrbControlVendorClassRequest.Value)
448 {
449 case PORT_ENABLE:
450 KdPrint((__DRIVER_NAME " PORT_ENABLE\n"));
451 /* do something here */
452 break;
453 case PORT_RESET:
454 KdPrint((__DRIVER_NAME " PORT_RESET\n"));
455 /* just fake the reset */
456 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change |= (1 << C_PORT_RESET);
457 break;
458 default:
459 KdPrint((__DRIVER_NAME " Unknown Value %04X\n", urb->UrbControlVendorClassRequest.Value));
460 break;
461 }
462 KdPrint((__DRIVER_NAME " status = %04x, change = %04x\n",
463 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_status,
464 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change));
465 break;
466 case USB_REQUEST_CLEAR_FEATURE:
467 KdPrint((__DRIVER_NAME " USB_REQUEST_CLEAR_FEATURE\n"));
468 KdPrint((__DRIVER_NAME " ClearPortFeature\n"));
469 switch (urb->UrbControlVendorClassRequest.Value)
470 {
471 case C_PORT_CONNECTION:
472 KdPrint((__DRIVER_NAME " C_PORT_CONNECTION\n"));
473 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change &= ~(1 << C_PORT_CONNECTION);
474 break;
475 case C_PORT_RESET:
476 KdPrint((__DRIVER_NAME " C_PORT_RESET\n"));
477 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change &= ~(1 << C_PORT_RESET);
478 break;
479 default:
480 KdPrint((__DRIVER_NAME " Unknown Value %04X\n", urb->UrbControlVendorClassRequest.Value));
481 break;
482 }
483 KdPrint((__DRIVER_NAME " status = %04x, change = %04x\n",
484 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_status,
485 xudd->ports[urb->UrbControlVendorClassRequest.Index - 1].port_change));
486 break;
487 default:
488 KdPrint((__DRIVER_NAME " USB_REQUEST_%02x\n", urb->UrbControlVendorClassRequest.Request));
489 break;
490 }
491 //urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
492 //WdfRequestComplete(request, STATUS_SUCCESS);
493 urb->UrbHeader.Status = USBD_STATUS_INVALID_URB_FUNCTION;
494 WdfRequestComplete(request, STATUS_UNSUCCESSFUL); //STATUS_UNSUCCESSFUL);
495 break;
496 case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER: /* 11.12.4 */
497 #if 1
498 KdPrint((__DRIVER_NAME " URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER\n"));
499 KdPrint((__DRIVER_NAME " PipeHandle = %p\n", urb->UrbBulkOrInterruptTransfer.PipeHandle));
500 KdPrint((__DRIVER_NAME " TransferFlags = %08x\n", urb->UrbBulkOrInterruptTransfer.TransferFlags));
501 KdPrint((__DRIVER_NAME " TransferBufferLength = %d\n", urb->UrbBulkOrInterruptTransfer.TransferBufferLength));
502 KdPrint((__DRIVER_NAME " TransferBuffer = %p\n", urb->UrbBulkOrInterruptTransfer.TransferBuffer));
503 KdPrint((__DRIVER_NAME " TransferBufferMdl = %p\n", urb->UrbBulkOrInterruptTransfer.TransferBufferMDL));
504 #endif
505 endpoint = urb->UrbBulkOrInterruptTransfer.PipeHandle;
506 WdfSpinLockAcquire (endpoint->interrupt_lock);
507 if (WdfIoQueueGetState(endpoint->interrupt_queue, NULL, NULL) & WdfIoQueueNoRequests)
508 {
509 status = WdfTimerStart(endpoint->interrupt_timer, WDF_REL_TIMEOUT_IN_MS(100));
510 }
511 status = WdfRequestForwardToIoQueue(request, endpoint->interrupt_queue);
512 WdfSpinLockRelease(endpoint->interrupt_lock);
513 break;
514 case URB_FUNCTION_CONTROL_TRANSFER_EX:
515 setup_packet = (PUSB_DEFAULT_PIPE_SETUP_PACKET)urb->UrbControlTransfer.SetupPacket;
516 #if 1
517 FUNCTION_MSG("URB_FUNCTION_CONTROL_TRANSFER_EX\n");
518 FUNCTION_MSG(" PipeHandle = %p\n", urb->UrbControlTransfer.PipeHandle);
519 FUNCTION_MSG(" TransferFlags = %08x\n", urb->UrbControlTransfer.TransferFlags);
520 FUNCTION_MSG(" TransferBufferLength = %d\n", urb->UrbControlTransfer.TransferBufferLength);
521 FUNCTION_MSG(" TransferBuffer = %p\n", urb->UrbControlTransfer.TransferBuffer);
522 FUNCTION_MSG(" TransferBufferMdl = %p\n", urb->UrbControlTransfer.TransferBufferMDL);
523 FUNCTION_MSG(" Timeout = %p\n", urb->UrbControlTransfer.TransferBufferMDL);
524 FUNCTION_MSG(" SetupPacket.bmRequestType = %02x\n", (ULONG)setup_packet->bmRequestType.B);
525 FUNCTION_MSG(" SetupPacket.bRequest = %02x\n", (ULONG)setup_packet->bRequest);
526 FUNCTION_MSG(" SetupPacket.wValue.LowByte = %02x\n", (ULONG)setup_packet->wValue.LowByte);
527 FUNCTION_MSG(" SetupPacket.wValue.HiByte = %02x\n", (ULONG)setup_packet->wValue.HiByte);
528 FUNCTION_MSG(" SetupPacket.wIndex.LowByte = %02x\n", (ULONG)setup_packet->wIndex.LowByte);
529 FUNCTION_MSG(" SetupPacket.wIndex.HiByte = %02x\n", (ULONG)setup_packet->wIndex.HiByte);
530 FUNCTION_MSG(" SetupPacket.wLength = %04x\n", (ULONG)setup_packet->wLength);
531 #endif
532 switch(setup_packet->bRequest)
533 {
534 case USB_REQUEST_GET_STATUS:
535 FUNCTION_MSG(" USB_REQUEST_GET_STATUS\n");
536 *(PUSHORT)urb->UrbControlDescriptorRequest.TransferBuffer = 0x0003;
537 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
538 break;
539 case USB_REQUEST_GET_DESCRIPTOR:
540 // should be able to reuse this code...
541 FUNCTION_MSG(" USB_REQUEST_GET_DESCRIPTOR\n");
542 switch (setup_packet->wValue.HiByte)
543 {
544 case USB_DEVICE_DESCRIPTOR_TYPE:
545 KdPrint((__DRIVER_NAME " USB_DEVICE_DESCRIPTOR_TYPE\n"));
546 memcpy(urb->UrbControlDescriptorRequest.TransferBuffer, &usb_device->device_descriptor, sizeof(USB_DEVICE_DESCRIPTOR));
547 urb->UrbControlDescriptorRequest.TransferBufferLength = sizeof(USB_DEVICE_DESCRIPTOR);
548 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
549 break;
550 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
551 {
552 xenusb_config_t *usb_config;
553 PUCHAR ptr;
555 KdPrint((__DRIVER_NAME " USB_CONFIGURATION_DESCRIPTOR_TYPE\n"));
556 usb_config = usb_device->active_config;
557 ptr = (PUCHAR)urb->UrbControlDescriptorRequest.TransferBuffer;
558 memcpy(ptr, &usb_config->config_descriptor, sizeof(USB_CONFIGURATION_DESCRIPTOR));
559 ptr += sizeof(USB_CONFIGURATION_DESCRIPTOR);
560 ((PUSB_CONFIGURATION_DESCRIPTOR)urb->UrbControlDescriptorRequest.TransferBuffer)->wTotalLength = sizeof(USB_CONFIGURATION_DESCRIPTOR);
561 if (urb->UrbControlDescriptorRequest.TransferBufferLength > 9)
562 {
563 for (i = 0; i < usb_config->config_descriptor.bNumInterfaces; i++)
564 {
565 memcpy(ptr, &usb_config->interfaces[i]->interface_descriptor, sizeof(USB_INTERFACE_DESCRIPTOR));
566 KdPrint((__DRIVER_NAME " bInterfaceClass = %02x\n", ((PUSB_INTERFACE_DESCRIPTOR)ptr)->bInterfaceClass));
567 ptr += sizeof(USB_INTERFACE_DESCRIPTOR);
568 ((PUSB_CONFIGURATION_DESCRIPTOR)urb->UrbControlDescriptorRequest.TransferBuffer)->wTotalLength += sizeof(USB_INTERFACE_DESCRIPTOR);
569 for (j = 0; j < usb_config->interfaces[i]->interface_descriptor.bNumEndpoints; j++)
570 {
571 memcpy(ptr, &usb_config->interfaces[i]->endpoints[j]->endpoint_descriptor, sizeof(USB_ENDPOINT_DESCRIPTOR));
572 ptr += sizeof(USB_ENDPOINT_DESCRIPTOR);
573 ((PUSB_CONFIGURATION_DESCRIPTOR)urb->UrbControlDescriptorRequest.TransferBuffer)->wTotalLength += sizeof(USB_ENDPOINT_DESCRIPTOR);
574 }
575 }
576 }
577 urb->UrbControlDescriptorRequest.TransferBufferLength = ((PUSB_CONFIGURATION_DESCRIPTOR)urb->UrbControlDescriptorRequest.TransferBuffer)->wTotalLength;
578 if (urb->UrbControlDescriptorRequest.TransferBufferLength == 9)
579 ((PUSB_CONFIGURATION_DESCRIPTOR)urb->UrbControlDescriptorRequest.TransferBuffer)->wTotalLength = 32;
580 urb->UrbHeader.Status = USBD_STATUS_SUCCESS;
581 KdPrint((__DRIVER_NAME " TransferBufferLength returned = %d\n", urb->UrbControlDescriptorRequest.TransferBufferLength));
582 break;
583 }
584 default:
585 FUNCTION_MSG(" USB_%02x_DESCRIPTOR_TYPE\n", (ULONG)setup_packet->wValue.HiByte);
586 break;
587 }
588 break;
589 default:
590 FUNCTION_MSG(" USB_REQUEST_%02x\n", (ULONG)setup_packet->bRequest);
591 break;
592 }
593 KdPrint((__DRIVER_NAME " TransferBufferLength returned = %d\n", urb->UrbControlDescriptorRequest.TransferBufferLength));
594 WdfRequestComplete(request, STATUS_SUCCESS);
595 break;
596 #endif
597 #endif
598 case URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL:
599 case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
600 case URB_FUNCTION_ABORT_PIPE:
601 FUNCTION_MSG("NOT_CONTROL URB_FUNCTION_%04x\n", urb->UrbHeader.Function);
602 retval = URB_DECODE_NOT_CONTROL;
603 break;
604 default:
605 FUNCTION_MSG("NOT IMPLEMENTED\n", urb->UrbHeader.Function);
606 retval = URB_DECODE_UNKNOWN;
607 break;
608 }
609 //FUNCTION_EXIT();
610 return retval;
611 }