VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/dev/VBoxUsbRt.cpp@ 59743

Last change on this file since 59743 was 58051, checked in by vboxsync, 9 years ago

USB/Win: Keep old configuration if SET INTERFACE fails.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.8 KB
Line 
1/* $Id: VBoxUsbRt.cpp 58051 2015-10-06 13:34:45Z vboxsync $ */
2/** @file
3 * VBox USB R0 runtime
4 */
5/*
6 * Copyright (C) 2011-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "VBoxUsbCmn.h"
18#include "../cmn/VBoxUsbIdc.h"
19#include "../cmn/VBoxUsbTool.h"
20
21#include <VBox/usblib-win.h>
22#include <iprt/assert.h>
23#include <VBox/log.h>
24#define _USBD_
25
26#define USBD_DEFAULT_PIPE_TRANSFER 0x00000008
27
28#define VBOXUSB_MAGIC 0xABCF1423
29
30typedef struct VBOXUSB_URB_CONTEXT
31{
32 PURB pUrb;
33 PMDL pMdlBuf;
34 PVBOXUSBDEV_EXT pDevExt;
35 PVOID pOut;
36 ULONG ulTransferType;
37 ULONG ulMagic;
38} VBOXUSB_URB_CONTEXT, * PVBOXUSB_URB_CONTEXT;
39
40typedef struct VBOXUSB_SETUP
41{
42 uint8_t bmRequestType;
43 uint8_t bRequest;
44 uint16_t wValue;
45 uint16_t wIndex;
46 uint16_t wLength;
47} VBOXUSB_SETUP, *PVBOXUSB_SETUP;
48
49static bool vboxUsbRtCtxSetOwner(PVBOXUSBDEV_EXT pDevExt, PFILE_OBJECT pFObj)
50{
51 bool bRc = ASMAtomicCmpXchgPtr(&pDevExt->Rt.pOwner, pFObj, NULL);
52 if (bRc)
53 {
54 Log((__FUNCTION__": pDevExt (0x%x) Owner(0x%x) acquired\n", pFObj));
55 }
56 else
57 {
58 Log((__FUNCTION__": pDevExt (0x%x) Owner(0x%x) FAILED!!\n", pFObj));
59 }
60 return bRc;
61}
62
63static bool vboxUsbRtCtxReleaseOwner(PVBOXUSBDEV_EXT pDevExt, PFILE_OBJECT pFObj)
64{
65 bool bRc = ASMAtomicCmpXchgPtr(&pDevExt->Rt.pOwner, NULL, pFObj);
66 if (bRc)
67 {
68 Log((__FUNCTION__": pDevExt (0x%x) Owner(0x%x) released\n", pFObj));
69 }
70 else
71 {
72 Log((__FUNCTION__": pDevExt (0x%x) Owner(0x%x) release: is NOT an owner\n", pFObj));
73 }
74 return bRc;
75}
76
77static bool vboxUsbRtCtxIsOwner(PVBOXUSBDEV_EXT pDevExt, PFILE_OBJECT pFObj)
78{
79 PFILE_OBJECT pOwner = (PFILE_OBJECT)ASMAtomicReadPtr((void *volatile *)(&pDevExt->Rt.pOwner));
80 return pOwner == pFObj;
81}
82
83static NTSTATUS vboxUsbRtIdcSubmit(ULONG uCtl, void *pvBuffer)
84{
85 /* we just reuse the standard usb tooling for simplicity here */
86 NTSTATUS Status = VBoxUsbToolIoInternalCtlSendSync(g_VBoxUsbGlobals.RtIdc.pDevice, uCtl, pvBuffer, NULL);
87 Assert(Status == STATUS_SUCCESS);
88 return Status;
89}
90
91static NTSTATUS vboxUsbRtIdcInit()
92{
93 UNICODE_STRING UniName;
94 RtlInitUnicodeString(&UniName, USBMON_DEVICE_NAME_NT);
95 NTSTATUS Status = IoGetDeviceObjectPointer(&UniName, FILE_ALL_ACCESS, &g_VBoxUsbGlobals.RtIdc.pFile, &g_VBoxUsbGlobals.RtIdc.pDevice);
96 if (NT_SUCCESS(Status))
97 {
98 VBOXUSBIDC_VERSION Version;
99 vboxUsbRtIdcSubmit(VBOXUSBIDC_INTERNAL_IOCTL_GET_VERSION, &Version);
100 if (NT_SUCCESS(Status))
101 {
102 if (Version.u32Major == VBOXUSBIDC_VERSION_MAJOR
103 && Version.u32Minor >= VBOXUSBIDC_VERSION_MINOR)
104 return STATUS_SUCCESS;
105 AssertFailed();
106 }
107 else
108 {
109 AssertFailed();
110 }
111
112 /* this will as well dereference the dev obj */
113 ObDereferenceObject(g_VBoxUsbGlobals.RtIdc.pFile);
114 }
115 else
116 {
117 AssertFailed();
118 }
119
120 memset(&g_VBoxUsbGlobals.RtIdc, 0, sizeof (g_VBoxUsbGlobals.RtIdc));
121 return Status;
122}
123
124static VOID vboxUsbRtIdcTerm()
125{
126 Assert(g_VBoxUsbGlobals.RtIdc.pFile);
127 Assert(g_VBoxUsbGlobals.RtIdc.pDevice);
128 ObDereferenceObject(g_VBoxUsbGlobals.RtIdc.pFile);
129 memset(&g_VBoxUsbGlobals.RtIdc, 0, sizeof (g_VBoxUsbGlobals.RtIdc));
130}
131
132static NTSTATUS vboxUsbRtIdcReportDevStart(PDEVICE_OBJECT pPDO, HVBOXUSBIDCDEV *phDev)
133{
134 VBOXUSBIDC_PROXY_STARTUP Start;
135 Start.u.pPDO = pPDO;
136
137 *phDev = NULL;
138
139 NTSTATUS Status = vboxUsbRtIdcSubmit(VBOXUSBIDC_INTERNAL_IOCTL_PROXY_STARTUP, &Start);
140 Assert(Status == STATUS_SUCCESS);
141 if (!NT_SUCCESS(Status))
142 return Status;
143
144 *phDev = Start.u.hDev;
145 return STATUS_SUCCESS;
146}
147
148static NTSTATUS vboxUsbRtIdcReportDevStop(HVBOXUSBIDCDEV hDev)
149{
150 VBOXUSBIDC_PROXY_TEARDOWN Stop;
151 Stop.hDev = hDev;
152
153 NTSTATUS Status = vboxUsbRtIdcSubmit(VBOXUSBIDC_INTERNAL_IOCTL_PROXY_TEARDOWN, &Stop);
154 Assert(Status == STATUS_SUCCESS);
155 return Status;
156}
157
158
159DECLHIDDEN(NTSTATUS) vboxUsbRtGlobalsInit()
160{
161 return vboxUsbRtIdcInit();
162}
163
164DECLHIDDEN(VOID) vboxUsbRtGlobalsTerm()
165{
166 vboxUsbRtIdcTerm();
167}
168
169
170DECLHIDDEN(NTSTATUS) vboxUsbRtInit(PVBOXUSBDEV_EXT pDevExt)
171{
172 RtlZeroMemory(&pDevExt->Rt, sizeof (pDevExt->Rt));
173 NTSTATUS Status = IoRegisterDeviceInterface(pDevExt->pPDO, &GUID_CLASS_VBOXUSB,
174 NULL, /* IN PUNICODE_STRING ReferenceString OPTIONAL */
175 &pDevExt->Rt.IfName);
176 Assert(Status == STATUS_SUCCESS);
177 if (NT_SUCCESS(Status))
178 {
179 Status = vboxUsbRtIdcReportDevStart(pDevExt->pPDO, &pDevExt->Rt.hMonDev);
180 Assert(Status == STATUS_SUCCESS);
181 if (NT_SUCCESS(Status))
182 {
183 Assert(pDevExt->Rt.hMonDev);
184 return STATUS_SUCCESS;
185 }
186
187 NTSTATUS tmpStatus = IoSetDeviceInterfaceState(&pDevExt->Rt.IfName, FALSE);
188 Assert(tmpStatus == STATUS_SUCCESS);
189 if (NT_SUCCESS(tmpStatus))
190 {
191 RtlFreeUnicodeString(&pDevExt->Rt.IfName);
192 }
193 }
194 return Status;
195}
196
197/**
198 * Free cached USB device/configuration descriptors
199 *
200 * @param pDevExt USB DevExt pointer
201 */
202static void vboxUsbRtFreeCachedDescriptors(PVBOXUSBDEV_EXT pDevExt)
203{
204 if (pDevExt->Rt.devdescr)
205 {
206 vboxUsbMemFree(pDevExt->Rt.devdescr);
207 pDevExt->Rt.devdescr = NULL;
208 }
209 for (ULONG i = 0; i < VBOXUSBRT_MAX_CFGS; ++i)
210 {
211 if (pDevExt->Rt.cfgdescr[i])
212 {
213 vboxUsbMemFree(pDevExt->Rt.cfgdescr[i]);
214 pDevExt->Rt.cfgdescr[i] = NULL;
215 }
216 }
217}
218
219/**
220 * Free per-device interface info
221 *
222 * @param pDevExt USB DevExt pointer
223 * @param fAbortPipes If true, also abort any open pipes
224 */
225static void vboxUsbRtFreeInterfaces(PVBOXUSBDEV_EXT pDevExt, BOOLEAN fAbortPipes)
226{
227 unsigned i;
228 unsigned j;
229
230 /*
231 * Free old interface info
232 */
233 if (pDevExt->Rt.pVBIfaceInfo)
234 {
235 for (i=0;i<pDevExt->Rt.uNumInterfaces;i++)
236 {
237 if (pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo)
238 {
239 if (fAbortPipes)
240 {
241 for(j=0; j<pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->NumberOfPipes; j++)
242 {
243 Log(("Aborting Pipe %d handle %x address %x\n", j,
244 pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].PipeHandle,
245 pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].EndpointAddress));
246 VBoxUsbToolPipeClear(pDevExt->pLowerDO, pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].PipeHandle, FALSE);
247 }
248 }
249 vboxUsbMemFree(pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo);
250 }
251 pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo = NULL;
252 if (pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo)
253 vboxUsbMemFree(pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo);
254 pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo = NULL;
255 }
256 vboxUsbMemFree(pDevExt->Rt.pVBIfaceInfo);
257 pDevExt->Rt.pVBIfaceInfo = NULL;
258 }
259}
260
261DECLHIDDEN(VOID) vboxUsbRtClear(PVBOXUSBDEV_EXT pDevExt)
262{
263 vboxUsbRtFreeCachedDescriptors(pDevExt);
264 vboxUsbRtFreeInterfaces(pDevExt, FALSE);
265}
266
267DECLHIDDEN(NTSTATUS) vboxUsbRtRm(PVBOXUSBDEV_EXT pDevExt)
268{
269 if (!pDevExt->Rt.IfName.Buffer)
270 return STATUS_SUCCESS;
271
272 NTSTATUS Status = vboxUsbRtIdcReportDevStop(pDevExt->Rt.hMonDev);
273 Assert(Status == STATUS_SUCCESS);
274 Status = IoSetDeviceInterfaceState(&pDevExt->Rt.IfName, FALSE);
275 Assert(Status == STATUS_SUCCESS);
276 if (NT_SUCCESS(Status))
277 {
278 RtlFreeUnicodeString(&pDevExt->Rt.IfName);
279 pDevExt->Rt.IfName.Buffer = NULL;
280 }
281 return Status;
282}
283
284DECLHIDDEN(NTSTATUS) vboxUsbRtStart(PVBOXUSBDEV_EXT pDevExt)
285{
286 NTSTATUS Status = IoSetDeviceInterfaceState(&pDevExt->Rt.IfName, TRUE);
287 Assert(Status == STATUS_SUCCESS);
288 return Status;
289}
290
291static NTSTATUS vboxUsbRtCacheDescriptors(PVBOXUSBDEV_EXT pDevExt)
292{
293 NTSTATUS Status = STATUS_INSUFFICIENT_RESOURCES;
294// uint32_t uTotalLength;
295// unsigned i;
296
297 /* Read device descriptor */
298 Assert(!pDevExt->Rt.devdescr);
299 pDevExt->Rt.devdescr = (PUSB_DEVICE_DESCRIPTOR)vboxUsbMemAlloc(sizeof (USB_DEVICE_DESCRIPTOR));
300 if (pDevExt->Rt.devdescr)
301 {
302 memset(pDevExt->Rt.devdescr, 0, sizeof (USB_DEVICE_DESCRIPTOR));
303 Status = VBoxUsbToolGetDescriptor(pDevExt->pLowerDO, pDevExt->Rt.devdescr, sizeof (USB_DEVICE_DESCRIPTOR), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, RT_INDEFINITE_WAIT);
304 if (NT_SUCCESS(Status))
305 {
306 Assert(pDevExt->Rt.devdescr->bNumConfigurations > 0);
307 PUSB_CONFIGURATION_DESCRIPTOR pDr = (PUSB_CONFIGURATION_DESCRIPTOR)vboxUsbMemAlloc(sizeof (USB_CONFIGURATION_DESCRIPTOR));
308 Assert(pDr);
309 if (pDr)
310 {
311 UCHAR i = 0;
312 for (; i < pDevExt->Rt.devdescr->bNumConfigurations; ++i)
313 {
314 Status = VBoxUsbToolGetDescriptor(pDevExt->pLowerDO, pDr, sizeof (USB_CONFIGURATION_DESCRIPTOR), USB_CONFIGURATION_DESCRIPTOR_TYPE, i, 0, RT_INDEFINITE_WAIT);
315 if (!NT_SUCCESS(Status))
316 {
317 break;
318 }
319
320 USHORT uTotalLength = pDr->wTotalLength;
321 pDevExt->Rt.cfgdescr[i] = (PUSB_CONFIGURATION_DESCRIPTOR)vboxUsbMemAlloc(uTotalLength);
322 if (!pDevExt->Rt.cfgdescr[i])
323 {
324 Status = STATUS_INSUFFICIENT_RESOURCES;
325 break;
326 }
327
328 Status = VBoxUsbToolGetDescriptor(pDevExt->pLowerDO, pDevExt->Rt.cfgdescr[i], uTotalLength, USB_CONFIGURATION_DESCRIPTOR_TYPE, i, 0, RT_INDEFINITE_WAIT);
329 if (!NT_SUCCESS(Status))
330 {
331 break;
332 }
333 }
334
335 vboxUsbMemFree(pDr);
336
337 if (NT_SUCCESS(Status))
338 return Status;
339
340 /* recources will be freed in vboxUsbRtFreeCachedDescriptors below */
341 }
342 }
343
344 vboxUsbRtFreeCachedDescriptors(pDevExt);
345 }
346
347 /* shoud be only on fail here */
348 Assert(!NT_SUCCESS(Status));
349 return Status;
350}
351
352static NTSTATUS vboxUsbRtDispatchClaimDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
353{
354 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
355 PFILE_OBJECT pFObj = pSl->FileObject;
356 PUSBSUP_CLAIMDEV pDev = (PUSBSUP_CLAIMDEV)pIrp->AssociatedIrp.SystemBuffer;
357 ULONG cbOut = 0;
358 NTSTATUS Status = STATUS_SUCCESS;
359
360 do
361 {
362 if (!pFObj)
363 {
364 AssertFailed();
365 Status = STATUS_INVALID_PARAMETER;
366 break;
367 }
368
369 if ( !pDev
370 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pDev)
371 || pSl->Parameters.DeviceIoControl.OutputBufferLength != sizeof (*pDev))
372 {
373 AssertFailed();
374 Status = STATUS_INVALID_PARAMETER;
375 break;
376 }
377
378 if (!vboxUsbRtCtxSetOwner(pDevExt, pFObj))
379 {
380 AssertFailed();
381 pDev->fClaimed = false;
382 cbOut = sizeof (*pDev);
383 break;
384 }
385
386 vboxUsbRtFreeCachedDescriptors(pDevExt);
387 Status = vboxUsbRtCacheDescriptors(pDevExt);
388 if (NT_SUCCESS(Status))
389 {
390 pDev->fClaimed = true;
391 cbOut = sizeof (*pDev);
392 }
393 } while (0);
394
395 Assert(Status != STATUS_PENDING);
396 VBoxDrvToolIoComplete(pIrp, Status, cbOut);
397 vboxUsbDdiStateRelease(pDevExt);
398 return Status;
399}
400
401static NTSTATUS vboxUsbRtDispatchReleaseDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
402{
403 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
404 PFILE_OBJECT pFObj = pSl->FileObject;
405 NTSTATUS Status= STATUS_SUCCESS;
406
407 if (vboxUsbRtCtxIsOwner(pDevExt, pFObj))
408 {
409 vboxUsbRtFreeCachedDescriptors(pDevExt);
410 bool bRc = vboxUsbRtCtxReleaseOwner(pDevExt, pFObj);
411 Assert(bRc);
412 }
413 else
414 {
415 AssertFailed();
416 Status = STATUS_ACCESS_DENIED;
417 }
418
419 VBoxDrvToolIoComplete(pIrp, STATUS_SUCCESS, 0);
420 vboxUsbDdiStateRelease(pDevExt);
421 return STATUS_SUCCESS;
422}
423
424static NTSTATUS vboxUsbRtGetDeviceDescription(PVBOXUSBDEV_EXT pDevExt)
425{
426 NTSTATUS Status = STATUS_INSUFFICIENT_RESOURCES;
427 PUSB_DEVICE_DESCRIPTOR pDr = (PUSB_DEVICE_DESCRIPTOR)vboxUsbMemAllocZ(sizeof (USB_DEVICE_DESCRIPTOR));
428 if (pDr)
429 {
430 Status = VBoxUsbToolGetDescriptor(pDevExt->pLowerDO, pDr, sizeof(*pDr), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, RT_INDEFINITE_WAIT);
431 if (NT_SUCCESS(Status))
432 {
433 pDevExt->Rt.idVendor = pDr->idVendor;
434 pDevExt->Rt.idProduct = pDr->idProduct;
435 pDevExt->Rt.bcdDevice = pDr->bcdDevice;
436 pDevExt->Rt.szSerial[0] = 0;
437
438 if (pDr->iSerialNumber
439#ifdef DEBUG
440 || pDr->iProduct || pDr->iManufacturer
441#endif
442 )
443 {
444 int langId;
445 Status = VBoxUsbToolGetLangID(pDevExt->pLowerDO, &langId, RT_INDEFINITE_WAIT);
446 if (NT_SUCCESS(Status))
447 {
448 Status = VBoxUsbToolGetStringDescriptorA(pDevExt->pLowerDO, pDevExt->Rt.szSerial, sizeof (pDevExt->Rt.szSerial), pDr->iSerialNumber, langId, RT_INDEFINITE_WAIT);
449 }
450 else
451 {
452 Status = STATUS_SUCCESS;
453 }
454 }
455 }
456 vboxUsbMemFree(pDr);
457 }
458
459 return Status;
460}
461
462static NTSTATUS vboxUsbRtDispatchGetDevice(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
463{
464 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
465 PUSBSUP_GETDEV pDev = (PUSBSUP_GETDEV)pIrp->AssociatedIrp.SystemBuffer;
466 NTSTATUS Status = STATUS_SUCCESS;
467 ULONG cbOut = 0;
468
469 /* don't check for owner since this request is allowed for non-owners as well */
470
471 if (pDev && pSl->Parameters.DeviceIoControl.InputBufferLength == sizeof (*pDev)
472 && pSl->Parameters.DeviceIoControl.OutputBufferLength == sizeof (*pDev))
473 {
474 Status = VBoxUsbToolGetDeviceSpeed(pDevExt->pLowerDO, &pDevExt->Rt.fIsHighSpeed);
475 if (NT_SUCCESS(Status))
476 {
477 pDev->hDevice = pDevExt->Rt.hMonDev;
478 pDev->fAttached = true;
479 pDev->fHiSpeed = pDevExt->Rt.fIsHighSpeed;
480 cbOut = sizeof (*pDev);
481 }
482 }
483 else
484 {
485 Status = STATUS_INVALID_PARAMETER;
486 }
487
488 Assert(Status != STATUS_PENDING);
489 VBoxDrvToolIoComplete(pIrp, Status, cbOut);
490 vboxUsbDdiStateRelease(pDevExt);
491 return Status;
492}
493
494static NTSTATUS vboxUsbRtDispatchUsbReset(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
495{
496 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
497 PFILE_OBJECT pFObj = pSl->FileObject;
498 PUSBSUP_GETDEV pDev = (PUSBSUP_GETDEV)pIrp->AssociatedIrp.SystemBuffer;
499 NTSTATUS Status = STATUS_SUCCESS;
500
501 do
502 {
503 if (!pFObj)
504 {
505 AssertFailed();
506 Status = STATUS_INVALID_PARAMETER;
507 break;
508 }
509
510 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
511 {
512 AssertFailed();
513 Status = STATUS_ACCESS_DENIED;
514 break;
515 }
516
517 if (pIrp->AssociatedIrp.SystemBuffer
518 || pSl->Parameters.DeviceIoControl.InputBufferLength
519 || pSl->Parameters.DeviceIoControl.OutputBufferLength)
520 {
521 AssertFailed();
522 Status = STATUS_INVALID_PARAMETER;
523 break;
524 }
525
526 Status = VBoxUsbToolIoInternalCtlSendSync(pDevExt->pLowerDO, IOCTL_INTERNAL_USB_RESET_PORT, NULL, NULL);
527 Assert(NT_SUCCESS(Status));
528 } while (0);
529
530 Assert(Status != STATUS_PENDING);
531 VBoxDrvToolIoComplete(pIrp, Status, 0);
532 vboxUsbDdiStateRelease(pDevExt);
533 return Status;
534}
535
536static PUSB_CONFIGURATION_DESCRIPTOR vboxUsbRtFindConfigDesc(PVBOXUSBDEV_EXT pDevExt, uint8_t uConfiguration)
537{
538 PUSB_CONFIGURATION_DESCRIPTOR pCfgDr = NULL;
539
540 for (ULONG i = 0; i < VBOXUSBRT_MAX_CFGS; ++i)
541 {
542 if (pDevExt->Rt.cfgdescr[i])
543 {
544 if (pDevExt->Rt.cfgdescr[i]->bConfigurationValue == uConfiguration)
545 {
546 pCfgDr = pDevExt->Rt.cfgdescr[i];
547 break;
548 }
549 }
550 }
551
552 return pCfgDr;
553}
554
555static NTSTATUS vboxUsbRtSetConfig(PVBOXUSBDEV_EXT pDevExt, uint8_t uConfiguration)
556{
557 PURB pUrb = NULL;
558 NTSTATUS Status = STATUS_SUCCESS;
559 uint32_t i;
560
561 if (!uConfiguration)
562 {
563 pUrb = VBoxUsbToolUrbAllocZ(URB_FUNCTION_SELECT_CONFIGURATION, sizeof (struct _URB_SELECT_CONFIGURATION));
564 if(!pUrb)
565 {
566 AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbAlloc failed\n"));
567 return STATUS_INSUFFICIENT_RESOURCES;
568 }
569
570 vboxUsbRtFreeInterfaces(pDevExt, TRUE);
571
572 pUrb->UrbSelectConfiguration.ConfigurationDescriptor = NULL;
573
574 Status = VBoxUsbToolUrbPost(pDevExt->pLowerDO, pUrb, RT_INDEFINITE_WAIT);
575 if(NT_SUCCESS(Status) && USBD_SUCCESS(pUrb->UrbHeader.Status))
576 {
577 pDevExt->Rt.hConfiguration = pUrb->UrbSelectConfiguration.ConfigurationHandle;
578 pDevExt->Rt.uConfigValue = uConfiguration;
579 }
580 else
581 {
582 AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbPost failed Status (0x%x), usb Status (0x%x)\n", Status, pUrb->UrbHeader.Status));
583 }
584
585 VBoxUsbToolUrbFree(pUrb);
586
587 return Status;
588 }
589
590 PUSB_CONFIGURATION_DESCRIPTOR pCfgDr = vboxUsbRtFindConfigDesc(pDevExt, uConfiguration);
591 if (!pCfgDr)
592 {
593 AssertMsgFailed((__FUNCTION__": VBoxUSBFindConfigDesc did not find cfg (%d)\n", uConfiguration));
594 return STATUS_INVALID_PARAMETER;
595 }
596
597 PUSBD_INTERFACE_LIST_ENTRY pIfLe = (PUSBD_INTERFACE_LIST_ENTRY)vboxUsbMemAllocZ((pCfgDr->bNumInterfaces + 1) * sizeof(USBD_INTERFACE_LIST_ENTRY));
598 if (!pIfLe)
599 {
600 AssertMsgFailed((__FUNCTION__": vboxUsbMemAllocZ for pIfLe failed\n"));
601 return STATUS_INSUFFICIENT_RESOURCES;
602 }
603
604 for (i = 0; i < pCfgDr->bNumInterfaces; i++)
605 {
606 pIfLe[i].InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(pCfgDr, pCfgDr, i, 0, -1, -1, -1);
607 if (!pIfLe[i].InterfaceDescriptor)
608 {
609 AssertMsgFailed((__FUNCTION__": interface %d not found\n", i));
610 Status = STATUS_INVALID_PARAMETER;
611 break;
612 }
613 }
614 pIfLe[pCfgDr->bNumInterfaces].InterfaceDescriptor = NULL;
615
616 if (NT_SUCCESS(Status))
617 {
618 pUrb = USBD_CreateConfigurationRequestEx(pCfgDr, pIfLe);
619 if (pUrb)
620 {
621 Status = VBoxUsbToolUrbPost(pDevExt->pLowerDO, pUrb, RT_INDEFINITE_WAIT);
622 if (NT_SUCCESS(Status) && USBD_SUCCESS(pUrb->UrbHeader.Status))
623 {
624 vboxUsbRtFreeInterfaces(pDevExt, FALSE);
625
626 pDevExt->Rt.hConfiguration = pUrb->UrbSelectConfiguration.ConfigurationHandle;
627 pDevExt->Rt.uConfigValue = uConfiguration;
628 pDevExt->Rt.uNumInterfaces = pCfgDr->bNumInterfaces;
629
630 pDevExt->Rt.pVBIfaceInfo = (VBOXUSB_IFACE_INFO*)vboxUsbMemAllocZ(pDevExt->Rt.uNumInterfaces * sizeof (VBOXUSB_IFACE_INFO));
631 if (pDevExt->Rt.pVBIfaceInfo)
632 {
633 Assert(NT_SUCCESS(Status));
634 for (i = 0; i < pDevExt->Rt.uNumInterfaces; i++)
635 {
636 size_t uTotalIfaceInfoLength = GET_USBD_INTERFACE_SIZE(pIfLe[i].Interface->NumberOfPipes);
637 pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION)vboxUsbMemAlloc(uTotalIfaceInfoLength);
638 if (!pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo)
639 {
640 AssertMsgFailed((__FUNCTION__": vboxUsbMemAlloc failed\n"));
641 Status = STATUS_INSUFFICIENT_RESOURCES;
642 break;
643 }
644
645 if (pIfLe[i].Interface->NumberOfPipes > 0)
646 {
647 pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo = (VBOXUSB_PIPE_INFO *)vboxUsbMemAlloc(pIfLe[i].Interface->NumberOfPipes * sizeof(VBOXUSB_PIPE_INFO));
648 if (!pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo)
649 {
650 AssertMsgFailed((__FUNCTION__": vboxUsbMemAlloc failed\n"));
651 Status = STATUS_NO_MEMORY;
652 break;
653 }
654 }
655 else
656 {
657 pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo = NULL;
658 }
659
660 RtlCopyMemory(pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo, pIfLe[i].Interface, uTotalIfaceInfoLength);
661
662 for (ULONG j = 0; j < pIfLe[i].Interface->NumberOfPipes; j++)
663 {
664 pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo[j].EndpointAddress = pIfLe[i].Interface->Pipes[j].EndpointAddress;
665 pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo[j].NextScheduledFrame = 0;
666 }
667 }
668
669// if (NT_SUCCESS(Status))
670// {
671//
672// }
673 }
674 else
675 {
676 AssertMsgFailed((__FUNCTION__": vboxUsbMemAllocZ failed\n"));
677 Status = STATUS_NO_MEMORY;
678 }
679 }
680 else
681 {
682 AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbPost failed Status (0x%x), usb Status (0x%x)\n", Status, pUrb->UrbHeader.Status));
683 }
684 ExFreePool(pUrb);
685 }
686 else
687 {
688 AssertMsgFailed((__FUNCTION__": USBD_CreateConfigurationRequestEx failed\n"));
689 Status = STATUS_INSUFFICIENT_RESOURCES;
690 }
691 }
692
693 vboxUsbMemFree(pIfLe);
694
695 return Status;
696}
697
698static NTSTATUS vboxUsbRtDispatchUsbSetConfig(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
699{
700 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
701 PFILE_OBJECT pFObj = pSl->FileObject;
702 PUSBSUP_SET_CONFIG pCfg = (PUSBSUP_SET_CONFIG)pIrp->AssociatedIrp.SystemBuffer;
703 NTSTATUS Status = STATUS_SUCCESS;
704
705 do
706 {
707 if (!pFObj)
708 {
709 AssertFailed();
710 Status = STATUS_INVALID_PARAMETER;
711 break;
712 }
713
714 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
715 {
716 AssertFailed();
717 Status = STATUS_ACCESS_DENIED;
718 break;
719 }
720
721 if ( !pCfg
722 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pCfg)
723 || pSl->Parameters.DeviceIoControl.OutputBufferLength != 0)
724 {
725 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
726 Status = STATUS_INVALID_PARAMETER;
727 break;
728 }
729
730 Status = vboxUsbRtSetConfig(pDevExt, pCfg->bConfigurationValue);
731 } while (0);
732
733 Assert(Status != STATUS_PENDING);
734 VBoxDrvToolIoComplete(pIrp, Status, 0);
735 vboxUsbDdiStateRelease(pDevExt);
736 return Status;
737}
738
739static NTSTATUS vboxUsbRtSetInterface(PVBOXUSBDEV_EXT pDevExt, uint32_t InterfaceNumber, int AlternateSetting)
740{
741 if (!pDevExt->Rt.uConfigValue)
742 {
743 AssertMsgFailed((__FUNCTION__": Can't select an interface without an active configuration\n"));
744 return STATUS_INVALID_PARAMETER;
745 }
746
747 if (InterfaceNumber >= pDevExt->Rt.uNumInterfaces)
748 {
749 AssertMsgFailed((__FUNCTION__": InterfaceNumber %d too high!!\n", InterfaceNumber));
750 return STATUS_INVALID_PARAMETER;
751 }
752
753 PUSB_CONFIGURATION_DESCRIPTOR pCfgDr = vboxUsbRtFindConfigDesc(pDevExt, pDevExt->Rt.uConfigValue);
754 if (!pCfgDr)
755 {
756 AssertMsgFailed((__FUNCTION__": configuration %d not found!!\n", pDevExt->Rt.uConfigValue));
757 return STATUS_INVALID_PARAMETER;
758 }
759
760 PUSB_INTERFACE_DESCRIPTOR pIfDr = USBD_ParseConfigurationDescriptorEx(pCfgDr, pCfgDr, InterfaceNumber, AlternateSetting, -1, -1, -1);
761 if (!pIfDr)
762 {
763 AssertMsgFailed((__FUNCTION__": invalid interface %d or alternate setting %d\n", InterfaceNumber, AlternateSetting));
764 return STATUS_UNSUCCESSFUL;
765 }
766
767 USHORT uUrbSize = GET_SELECT_INTERFACE_REQUEST_SIZE(pIfDr->bNumEndpoints);
768 ULONG uTotalIfaceInfoLength = GET_USBD_INTERFACE_SIZE(pIfDr->bNumEndpoints);
769 NTSTATUS Status = STATUS_SUCCESS;
770 PURB pUrb;
771 PUSBD_INTERFACE_INFORMATION pNewIFInfo;
772 VBOXUSB_PIPE_INFO *pNewPipeInfo;
773
774 if (pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo)
775 {
776 /* Clear pipes associated with the interface, else Windows may hang. */
777 for(ULONG i = 0; i < pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo->NumberOfPipes; i++)
778 VBoxUsbToolPipeClear(pDevExt->pLowerDO, pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo->Pipes[i].PipeHandle, FALSE);
779 }
780
781 do {
782 /* First allocate all the structures we'll need. */
783 pUrb = VBoxUsbToolUrbAllocZ(0, uUrbSize);
784 if (!pUrb)
785 {
786 AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbAllocZ failed\n"));
787 Status = STATUS_NO_MEMORY;
788 break;
789 }
790
791 pNewIFInfo = (PUSBD_INTERFACE_INFORMATION)vboxUsbMemAlloc(uTotalIfaceInfoLength);
792 if (!pNewIFInfo)
793 {
794 AssertMsgFailed((__FUNCTION__": Failed allocating interface storage\n"));
795 Status = STATUS_NO_MEMORY;
796 break;
797 }
798
799 if (pIfDr->bNumEndpoints > 0)
800 {
801 pNewPipeInfo = (VBOXUSB_PIPE_INFO *)vboxUsbMemAlloc(pIfDr->bNumEndpoints * sizeof(VBOXUSB_PIPE_INFO));
802 if (!pNewPipeInfo)
803 {
804 AssertMsgFailed((__FUNCTION__": Failed allocating pipe info storage\n"));
805 Status = STATUS_NO_MEMORY;
806 break;
807 }
808 }
809 else
810 pNewPipeInfo = NULL;
811
812 /* Now that we have all the bits, select the interface. */
813 UsbBuildSelectInterfaceRequest(pUrb, uUrbSize, pDevExt->Rt.hConfiguration, InterfaceNumber, AlternateSetting);
814 pUrb->UrbSelectInterface.Interface.Length = GET_USBD_INTERFACE_SIZE(pIfDr->bNumEndpoints);
815
816 Status = VBoxUsbToolUrbPost(pDevExt->pLowerDO, pUrb, RT_INDEFINITE_WAIT);
817 if (NT_SUCCESS(Status) && USBD_SUCCESS(pUrb->UrbHeader.Status))
818 {
819 /* Free the old memory and put new in. */
820 if (pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo)
821 vboxUsbMemFree(pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo);
822 pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo = pNewIFInfo;
823 if (pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pPipeInfo)
824 vboxUsbMemFree(pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pPipeInfo);
825 pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pPipeInfo = pNewPipeInfo;
826 pNewPipeInfo = NULL; pNewIFInfo = NULL; /* Don't try to free it again. */
827
828 USBD_INTERFACE_INFORMATION *pIfInfo = &pUrb->UrbSelectInterface.Interface;
829 memcpy(pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo, pIfInfo, GET_USBD_INTERFACE_SIZE(pIfDr->bNumEndpoints));
830
831 Assert(pIfInfo->NumberOfPipes == pIfDr->bNumEndpoints);
832 for(ULONG i = 0; i < pIfInfo->NumberOfPipes; i++)
833 {
834 pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pPipeInfo[i].EndpointAddress = pIfInfo->Pipes[i].EndpointAddress;
835 pDevExt->Rt.pVBIfaceInfo[InterfaceNumber].pPipeInfo[i].NextScheduledFrame = 0;
836 }
837 }
838 else
839 {
840 AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbPost failed Status (0x%x) usb Status (0x%x)\n", Status, pUrb->UrbHeader.Status));
841 }
842 } while (0);
843
844 /* Clean up. */
845 if (pUrb)
846 VBoxUsbToolUrbFree(pUrb);
847 if (pNewIFInfo)
848 vboxUsbMemFree(pNewIFInfo);
849 if (pNewPipeInfo)
850 vboxUsbMemFree(pNewPipeInfo);
851
852 return Status;
853}
854
855static NTSTATUS vboxUsbRtDispatchUsbSelectInterface(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
856{
857 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
858 PFILE_OBJECT pFObj = pSl->FileObject;
859 PUSBSUP_SELECT_INTERFACE pIf = (PUSBSUP_SELECT_INTERFACE)pIrp->AssociatedIrp.SystemBuffer;
860 NTSTATUS Status;
861
862 do
863 {
864 if (!pFObj)
865 {
866 AssertFailed();
867 Status = STATUS_INVALID_PARAMETER;
868 break;
869 }
870
871 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
872 {
873 AssertFailed();
874 Status = STATUS_ACCESS_DENIED;
875 break;
876 }
877
878 if ( !pIf
879 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pIf)
880 || pSl->Parameters.DeviceIoControl.OutputBufferLength != 0)
881 {
882 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
883 Status = STATUS_INVALID_PARAMETER;
884 break;
885 }
886
887 Status = vboxUsbRtSetInterface(pDevExt, pIf->bInterfaceNumber, pIf->bAlternateSetting);
888 } while (0);
889
890 Assert(Status != STATUS_PENDING);
891 VBoxDrvToolIoComplete(pIrp, Status, 0);
892 vboxUsbDdiStateRelease(pDevExt);
893 return Status;
894}
895
896static HANDLE vboxUsbRtGetPipeHandle(PVBOXUSBDEV_EXT pDevExt, uint32_t EndPointAddress)
897{
898 for (ULONG i = 0; i < pDevExt->Rt.uNumInterfaces; i++)
899 {
900 for (ULONG j = 0; j < pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->NumberOfPipes; j++)
901 {
902 /* Note that bit 7 determines pipe direction, but is still significant
903 * because endpoints may be numbered like 0x01, 0x81, 0x02, 0x82 etc.
904 */
905 if (pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].EndpointAddress == EndPointAddress)
906 return pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].PipeHandle;
907 }
908 }
909 return 0;
910}
911
912static VBOXUSB_PIPE_INFO* vboxUsbRtGetPipeInfo(PVBOXUSBDEV_EXT pDevExt, uint32_t EndPointAddress)
913{
914 for (ULONG i = 0; i < pDevExt->Rt.uNumInterfaces; i++)
915 {
916 for (ULONG j = 0; j < pDevExt->Rt.pVBIfaceInfo[i].pInterfaceInfo->NumberOfPipes; j++)
917 {
918 if (pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo[j].EndpointAddress == EndPointAddress)
919 return &pDevExt->Rt.pVBIfaceInfo[i].pPipeInfo[j];
920 }
921 }
922 return NULL;
923}
924
925
926
927static NTSTATUS vboxUsbRtClearEndpoint(PVBOXUSBDEV_EXT pDevExt, uint32_t EndPointAddress, bool fReset)
928{
929 NTSTATUS Status = VBoxUsbToolPipeClear(pDevExt->pLowerDO, vboxUsbRtGetPipeHandle(pDevExt, EndPointAddress), fReset);
930 if (!NT_SUCCESS(Status))
931 {
932 AssertMsgFailed((__FUNCTION__": VBoxUsbToolPipeClear failed Status (0x%x)\n", Status));
933 }
934
935 return Status;
936}
937
938static NTSTATUS vboxUsbRtDispatchUsbClearEndpoint(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
939{
940 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
941 PFILE_OBJECT pFObj = pSl->FileObject;
942 PUSBSUP_CLEAR_ENDPOINT pCe = (PUSBSUP_CLEAR_ENDPOINT)pIrp->AssociatedIrp.SystemBuffer;
943 NTSTATUS Status;
944
945 do
946 {
947 if (!pFObj)
948 {
949 AssertFailed();
950 Status = STATUS_INVALID_PARAMETER;
951 break;
952 }
953
954 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
955 {
956 AssertFailed();
957 Status = STATUS_ACCESS_DENIED;
958 break;
959 }
960
961 if ( !pCe
962 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pCe)
963 || pSl->Parameters.DeviceIoControl.OutputBufferLength != 0)
964 {
965 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
966 Status = STATUS_INVALID_PARAMETER;
967 break;
968 }
969
970 Status = vboxUsbRtClearEndpoint(pDevExt, pCe->bEndpoint, TRUE);
971 } while (0);
972
973 Assert(Status != STATUS_PENDING);
974 VBoxDrvToolIoComplete(pIrp, Status, 0);
975 vboxUsbDdiStateRelease(pDevExt);
976 return Status;
977}
978
979static NTSTATUS vboxUsbRtDispatchUsbAbortEndpoint(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
980{
981 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
982 PFILE_OBJECT pFObj = pSl->FileObject;
983 PUSBSUP_CLEAR_ENDPOINT pCe = (PUSBSUP_CLEAR_ENDPOINT)pIrp->AssociatedIrp.SystemBuffer;
984 NTSTATUS Status;
985
986 do
987 {
988 if (!pFObj)
989 {
990 AssertFailed();
991 Status = STATUS_INVALID_PARAMETER;
992 break;
993 }
994
995 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
996 {
997 AssertFailed();
998 Status = STATUS_ACCESS_DENIED;
999 break;
1000 }
1001
1002 if ( !pCe
1003 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pCe)
1004 || pSl->Parameters.DeviceIoControl.OutputBufferLength != 0)
1005 {
1006 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
1007 Status = STATUS_INVALID_PARAMETER;
1008 break;
1009 }
1010
1011 Status = vboxUsbRtClearEndpoint(pDevExt, pCe->bEndpoint, FALSE);
1012 } while (0);
1013
1014 Assert(Status != STATUS_PENDING);
1015 VBoxDrvToolIoComplete(pIrp, Status, 0);
1016 vboxUsbDdiStateRelease(pDevExt);
1017 return Status;
1018}
1019
1020static NTSTATUS vboxUsbRtUrbSendCompletion(PDEVICE_OBJECT pDevObj, IRP *pIrp, void *pvContext)
1021{
1022 if (!pvContext)
1023 {
1024 AssertMsgFailed((__FUNCTION__": context is NULL\n"));
1025 pIrp->IoStatus.Information = 0;
1026 return STATUS_CONTINUE_COMPLETION;
1027 }
1028
1029 PVBOXUSB_URB_CONTEXT pContext = (PVBOXUSB_URB_CONTEXT)pvContext;
1030
1031 if (pContext->ulMagic != VBOXUSB_MAGIC)
1032 {
1033 AssertMsgFailed((__FUNCTION__": Invalid context magic\n"));
1034 pIrp->IoStatus.Information = 0;
1035 return STATUS_CONTINUE_COMPLETION;
1036 }
1037
1038 PURB pUrb = pContext->pUrb;
1039 PMDL pMdlBuf = pContext->pMdlBuf;
1040 PUSBSUP_URB pUrbInfo = (PUSBSUP_URB)pContext->pOut;
1041 PVBOXUSBDEV_EXT pDevExt = pContext->pDevExt;
1042
1043 if (!pUrb || !pMdlBuf || !pUrbInfo || !pDevExt)
1044 {
1045 AssertMsgFailed((__FUNCTION__": Invalid args\n"));
1046 if (pDevExt)
1047 vboxUsbDdiStateRelease(pDevExt);
1048 pIrp->IoStatus.Information = 0;
1049 return STATUS_CONTINUE_COMPLETION;
1050 }
1051
1052 NTSTATUS Status = pIrp->IoStatus.Status;
1053 if (Status == STATUS_SUCCESS)
1054 {
1055 switch(pUrb->UrbHeader.Status)
1056 {
1057 case USBD_STATUS_CRC:
1058 pUrbInfo->error = USBSUP_XFER_CRC;
1059 break;
1060 case USBD_STATUS_SUCCESS:
1061 pUrbInfo->error = USBSUP_XFER_OK;
1062 break;
1063 case USBD_STATUS_STALL_PID:
1064 pUrbInfo->error = USBSUP_XFER_STALL;
1065 break;
1066 case USBD_STATUS_INVALID_URB_FUNCTION:
1067 case USBD_STATUS_INVALID_PARAMETER:
1068 AssertMsgFailed((__FUNCTION__": sw error, urb Status (0x%x)\n", pUrb->UrbHeader.Status));
1069 case USBD_STATUS_DEV_NOT_RESPONDING:
1070 default:
1071 pUrbInfo->error = USBSUP_XFER_DNR;
1072 break;
1073 }
1074
1075 switch(pContext->ulTransferType)
1076 {
1077 case USBSUP_TRANSFER_TYPE_CTRL:
1078 case USBSUP_TRANSFER_TYPE_MSG:
1079 pUrbInfo->len = pUrb->UrbControlTransfer.TransferBufferLength;
1080 if (pContext->ulTransferType == USBSUP_TRANSFER_TYPE_MSG)
1081 {
1082 /* QUSB_TRANSFER_TYPE_MSG is a control transfer, but it is special
1083 * the first 8 bytes of the buffer is the setup packet so the real
1084 * data length is therefore urb->len - 8
1085 */
1086 pUrbInfo->len += sizeof (pUrb->UrbControlTransfer.SetupPacket);
1087 }
1088 break;
1089 case USBSUP_TRANSFER_TYPE_ISOC:
1090 pUrbInfo->len = pUrb->UrbIsochronousTransfer.TransferBufferLength;
1091 break;
1092 case USBSUP_TRANSFER_TYPE_BULK:
1093 case USBSUP_TRANSFER_TYPE_INTR:
1094 if (pUrbInfo->dir == USBSUP_DIRECTION_IN && pUrbInfo->error == USBSUP_XFER_OK
1095 && !(pUrbInfo->flags & USBSUP_FLAG_SHORT_OK)
1096 && pUrbInfo->len > pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength
1097 )
1098 {
1099 /* If we don't use the USBD_SHORT_TRANSFER_OK flag, the returned buffer lengths are
1100 * wrong for short transfers (always a multiple of max packet size?). So we just figure
1101 * out if this was a data underrun on our own.
1102 */
1103 pUrbInfo->error = USBSUP_XFER_UNDERRUN;
1104 }
1105 pUrbInfo->len = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
1106 break;
1107 default:
1108 break;
1109 }
1110 }
1111 else
1112 {
1113 pUrbInfo->len = 0;
1114
1115 Log((__FUNCTION__": URB failed Status (0x%x) urb Status (0x%x)\n", Status, pUrb->UrbHeader.Status));
1116#ifdef DEBUG
1117 switch(pContext->ulTransferType)
1118 {
1119 case USBSUP_TRANSFER_TYPE_CTRL:
1120 case USBSUP_TRANSFER_TYPE_MSG:
1121 LogRel(("Ctrl/Msg length=%d\n", pUrb->UrbControlTransfer.TransferBufferLength));
1122 break;
1123 case USBSUP_TRANSFER_TYPE_ISOC:
1124 LogRel(("ISOC length=%d\n", pUrb->UrbIsochronousTransfer.TransferBufferLength));
1125 break;
1126 case USBSUP_TRANSFER_TYPE_BULK:
1127 case USBSUP_TRANSFER_TYPE_INTR:
1128 LogRel(("BULK/INTR length=%d\n", pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength));
1129 break;
1130 }
1131#endif
1132 switch(pUrb->UrbHeader.Status)
1133 {
1134 case USBD_STATUS_CRC:
1135 pUrbInfo->error = USBSUP_XFER_CRC;
1136 Status = STATUS_SUCCESS;
1137 break;
1138 case USBD_STATUS_STALL_PID:
1139 pUrbInfo->error = USBSUP_XFER_STALL;
1140 Status = STATUS_SUCCESS;
1141 break;
1142 case USBD_STATUS_DEV_NOT_RESPONDING:
1143 case USBD_STATUS_DEVICE_GONE:
1144 pUrbInfo->error = USBSUP_XFER_DNR;
1145 Status = STATUS_SUCCESS;
1146 break;
1147 case ((USBD_STATUS)0xC0010000L): // USBD_STATUS_CANCELED - too bad usbdi.h and usb.h aren't consistent!
1148 // TODO: What the heck are we really supposed to do here?
1149 pUrbInfo->error = USBSUP_XFER_STALL;
1150 Status = STATUS_SUCCESS;
1151 break;
1152 case USBD_STATUS_BAD_START_FRAME: // This one really shouldn't happen
1153 case USBD_STATUS_ISOCH_REQUEST_FAILED:
1154 pUrbInfo->error = USBSUP_XFER_NAC;
1155 Status = STATUS_SUCCESS;
1156 break;
1157 default:
1158 AssertMsgFailed((__FUNCTION__": err Status (0x%x) (0x%x)\n", Status, pUrb->UrbHeader.Status));
1159 pUrbInfo->error = USBSUP_XFER_DNR;
1160 Status = STATUS_SUCCESS;
1161 break;
1162 }
1163 }
1164 // For isochronous transfers, always update the individual packets
1165 if (pContext->ulTransferType == USBSUP_TRANSFER_TYPE_ISOC)
1166 {
1167 Assert(pUrbInfo->numIsoPkts == pUrb->UrbIsochronousTransfer.NumberOfPackets);
1168 for (ULONG i = 0; i < pUrbInfo->numIsoPkts; ++i)
1169 {
1170 Assert(pUrbInfo->aIsoPkts[i].off == pUrb->UrbIsochronousTransfer.IsoPacket[i].Offset);
1171 pUrbInfo->aIsoPkts[i].cb = (uint16_t)pUrb->UrbIsochronousTransfer.IsoPacket[i].Length;
1172 switch (pUrb->UrbIsochronousTransfer.IsoPacket[i].Status)
1173 {
1174 case USBD_STATUS_SUCCESS:
1175 pUrbInfo->aIsoPkts[i].stat = USBSUP_XFER_OK;
1176 break;
1177 case USBD_STATUS_NOT_ACCESSED:
1178 pUrbInfo->aIsoPkts[i].stat = USBSUP_XFER_NAC;
1179 break;
1180 default:
1181 pUrbInfo->aIsoPkts[i].stat = USBSUP_XFER_STALL;
1182 break;
1183 }
1184 }
1185 }
1186
1187 MmUnlockPages(pMdlBuf);
1188 IoFreeMdl(pMdlBuf);
1189
1190 vboxUsbMemFree(pContext);
1191
1192 vboxUsbDdiStateRelease(pDevExt);
1193
1194 Assert(pIrp->IoStatus.Status != STATUS_IO_TIMEOUT);
1195 pIrp->IoStatus.Information = sizeof(*pUrbInfo);
1196 pIrp->IoStatus.Status = Status;
1197 return STATUS_CONTINUE_COMPLETION;
1198}
1199
1200static NTSTATUS vboxUsbRtUrbSend(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp, PUSBSUP_URB pUrbInfo)
1201{
1202 NTSTATUS Status = STATUS_SUCCESS;
1203 PVBOXUSB_URB_CONTEXT pContext = NULL;
1204 PMDL pMdlBuf = NULL;
1205 ULONG cbUrb;
1206
1207 Assert(pUrbInfo);
1208 if (pUrbInfo->type == USBSUP_TRANSFER_TYPE_ISOC)
1209 {
1210 Assert(pUrbInfo->numIsoPkts <= 8);
1211 cbUrb = GET_ISO_URB_SIZE(pUrbInfo->numIsoPkts);
1212 }
1213 else
1214 cbUrb = sizeof (URB);
1215
1216 do
1217 {
1218 pContext = (PVBOXUSB_URB_CONTEXT)vboxUsbMemAllocZ(cbUrb + sizeof (VBOXUSB_URB_CONTEXT));
1219 if (!pContext)
1220 {
1221 AssertMsgFailed((__FUNCTION__": vboxUsbMemAlloc failed\n"));
1222 Status = STATUS_INSUFFICIENT_RESOURCES;
1223 break;
1224 }
1225
1226 PURB pUrb = (PURB)(pContext + 1);
1227 HANDLE hPipe = NULL;
1228 if (pUrbInfo->ep)
1229 {
1230 hPipe = vboxUsbRtGetPipeHandle(pDevExt, pUrbInfo->ep | ((pUrbInfo->dir == USBSUP_DIRECTION_IN) ? 0x80 : 0x00));
1231 if (!hPipe)
1232 {
1233 AssertMsgFailed((__FUNCTION__": vboxUsbRtGetPipeHandle failed for endpoint (0x%x)\n", pUrbInfo->ep));
1234 Status = STATUS_INVALID_PARAMETER;
1235 break;
1236 }
1237 }
1238
1239 pMdlBuf = IoAllocateMdl(pUrbInfo->buf, (ULONG)pUrbInfo->len, FALSE, FALSE, NULL);
1240 if (!pMdlBuf)
1241 {
1242 AssertMsgFailed((__FUNCTION__": IoAllocateMdl failed for buffer (0x%p) length (%d)\n", pUrbInfo->buf, pUrbInfo->len));
1243 Status = STATUS_INSUFFICIENT_RESOURCES;
1244 break;
1245 }
1246
1247 __try
1248 {
1249 MmProbeAndLockPages(pMdlBuf, KernelMode, IoModifyAccess);
1250 }
1251 __except(EXCEPTION_EXECUTE_HANDLER)
1252 {
1253 Status = GetExceptionCode();
1254 IoFreeMdl(pMdlBuf);
1255 pMdlBuf = NULL;
1256 AssertMsgFailed((__FUNCTION__": Exception Code (0x%x)\n", Status));
1257 break;
1258 }
1259
1260 /* For some reason, passing a MDL in the URB does not work reliably. Notably
1261 * the iPhone when used with iTunes fails.
1262 */
1263 PVOID pBuffer = MmGetSystemAddressForMdlSafe(pMdlBuf, NormalPagePriority);
1264 if (!pBuffer)
1265 {
1266 AssertMsgFailed((__FUNCTION__": MmGetSystemAddressForMdlSafe failed\n"));
1267 Status = STATUS_INSUFFICIENT_RESOURCES;
1268 break;
1269 }
1270
1271 switch (pUrbInfo->type)
1272 {
1273 case USBSUP_TRANSFER_TYPE_CTRL:
1274 case USBSUP_TRANSFER_TYPE_MSG:
1275 {
1276 pUrb->UrbHeader.Function = URB_FUNCTION_CONTROL_TRANSFER;
1277 pUrb->UrbHeader.Length = sizeof (struct _URB_CONTROL_TRANSFER);
1278 pUrb->UrbControlTransfer.PipeHandle = hPipe;
1279 pUrb->UrbControlTransfer.TransferBufferLength = (ULONG)pUrbInfo->len;
1280 pUrb->UrbControlTransfer.TransferFlags = ((pUrbInfo->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
1281 pUrb->UrbControlTransfer.UrbLink = 0;
1282
1283 if (!hPipe)
1284 pUrb->UrbControlTransfer.TransferFlags |= USBD_DEFAULT_PIPE_TRANSFER;
1285
1286 if (pUrbInfo->type == USBSUP_TRANSFER_TYPE_MSG)
1287 {
1288 /* QUSB_TRANSFER_TYPE_MSG is a control transfer, but it is special
1289 * the first 8 bytes of the buffer is the setup packet so the real
1290 * data length is therefore pUrb->len - 8
1291 */
1292 PVBOXUSB_SETUP pSetup = (PVBOXUSB_SETUP)pUrb->UrbControlTransfer.SetupPacket;
1293 memcpy(pUrb->UrbControlTransfer.SetupPacket, pBuffer, min(sizeof (pUrb->UrbControlTransfer.SetupPacket), pUrbInfo->len));
1294
1295 if (pUrb->UrbControlTransfer.TransferBufferLength <= sizeof (pUrb->UrbControlTransfer.SetupPacket))
1296 pUrb->UrbControlTransfer.TransferBufferLength = 0;
1297 else
1298 pUrb->UrbControlTransfer.TransferBufferLength -= sizeof (pUrb->UrbControlTransfer.SetupPacket);
1299
1300 pUrb->UrbControlTransfer.TransferBuffer = (uint8_t *)pBuffer + sizeof(pUrb->UrbControlTransfer.SetupPacket);
1301 pUrb->UrbControlTransfer.TransferBufferMDL = 0;
1302 pUrb->UrbControlTransfer.TransferFlags |= USBD_SHORT_TRANSFER_OK;
1303 }
1304 else
1305 {
1306 pUrb->UrbControlTransfer.TransferBuffer = 0;
1307 pUrb->UrbControlTransfer.TransferBufferMDL = pMdlBuf;
1308 }
1309 break;
1310 }
1311 case USBSUP_TRANSFER_TYPE_ISOC:
1312 {
1313 Assert(hPipe);
1314 VBOXUSB_PIPE_INFO *pPipeInfo = vboxUsbRtGetPipeInfo(pDevExt, pUrbInfo->ep | ((pUrbInfo->dir == USBSUP_DIRECTION_IN) ? 0x80 : 0x00));
1315 if (pPipeInfo == NULL)
1316 {
1317 /* Can happen if the isoc request comes in too early or late. */
1318 AssertMsgFailed((__FUNCTION__": pPipeInfo not found\n"));
1319 Status = STATUS_INVALID_PARAMETER;
1320 break;
1321 }
1322
1323 pUrb->UrbHeader.Function = URB_FUNCTION_ISOCH_TRANSFER;
1324 pUrb->UrbHeader.Length = (USHORT)cbUrb;
1325 pUrb->UrbIsochronousTransfer.PipeHandle = hPipe;
1326 pUrb->UrbIsochronousTransfer.TransferBufferLength = (ULONG)pUrbInfo->len;
1327 pUrb->UrbIsochronousTransfer.TransferBufferMDL = 0;
1328 pUrb->UrbIsochronousTransfer.TransferBuffer = pBuffer;
1329 pUrb->UrbIsochronousTransfer.TransferFlags = ((pUrbInfo->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
1330 pUrb->UrbIsochronousTransfer.TransferFlags |= USBD_SHORT_TRANSFER_OK; // May be implied already
1331 pUrb->UrbIsochronousTransfer.NumberOfPackets = pUrbInfo->numIsoPkts;
1332 pUrb->UrbIsochronousTransfer.ErrorCount = 0;
1333 pUrb->UrbIsochronousTransfer.UrbLink = 0;
1334
1335 Assert(pUrbInfo->numIsoPkts == pUrb->UrbIsochronousTransfer.NumberOfPackets);
1336 for (ULONG i = 0; i < pUrbInfo->numIsoPkts; ++i)
1337 {
1338 pUrb->UrbIsochronousTransfer.IsoPacket[i].Offset = pUrbInfo->aIsoPkts[i].off;
1339 pUrb->UrbIsochronousTransfer.IsoPacket[i].Length = pUrbInfo->aIsoPkts[i].cb;
1340 }
1341
1342 /* We have to schedule the URBs ourselves. There is an ASAP flag but
1343 * that can only be reliably used after pipe creation/reset, ie. it's
1344 * almost completely useless.
1345 */
1346 ULONG iFrame, iStartFrame;
1347 VBoxUsbToolCurrentFrame(pDevExt->pLowerDO, pIrp, &iFrame);
1348 iFrame += 2;
1349 iStartFrame = pPipeInfo->NextScheduledFrame;
1350 if ((iFrame < iStartFrame) || (iStartFrame > iFrame + 512))
1351 iFrame = iStartFrame;
1352 /* For full-speed devices, there must be one transfer per frame (Windows USB
1353 * stack requirement), but URBs can contain multiple packets. For high-speed or
1354 * faster transfers, we expect one URB per frame, regardless of the interval.
1355 */
1356 if (pDevExt->Rt.devdescr->bcdUSB < 0x300 && !pDevExt->Rt.fIsHighSpeed)
1357 pPipeInfo->NextScheduledFrame = iFrame + pUrbInfo->numIsoPkts;
1358 else
1359 pPipeInfo->NextScheduledFrame = iFrame + 1;
1360 pUrb->UrbIsochronousTransfer.StartFrame = iFrame;
1361 break;
1362 }
1363 case USBSUP_TRANSFER_TYPE_BULK:
1364 case USBSUP_TRANSFER_TYPE_INTR:
1365 {
1366 Assert(pUrbInfo->dir != USBSUP_DIRECTION_SETUP);
1367 Assert(pUrbInfo->dir == USBSUP_DIRECTION_IN || pUrbInfo->type == USBSUP_TRANSFER_TYPE_BULK);
1368 Assert(hPipe);
1369
1370 pUrb->UrbHeader.Function = URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER;
1371 pUrb->UrbHeader.Length = sizeof (struct _URB_BULK_OR_INTERRUPT_TRANSFER);
1372 pUrb->UrbBulkOrInterruptTransfer.PipeHandle = hPipe;
1373 pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength = (ULONG)pUrbInfo->len;
1374 pUrb->UrbBulkOrInterruptTransfer.TransferBufferMDL = 0;
1375 pUrb->UrbBulkOrInterruptTransfer.TransferBuffer = pBuffer;
1376 pUrb->UrbBulkOrInterruptTransfer.TransferFlags = ((pUrbInfo->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
1377
1378 if (pUrb->UrbBulkOrInterruptTransfer.TransferFlags & USBD_TRANSFER_DIRECTION_IN)
1379 pUrb->UrbBulkOrInterruptTransfer.TransferFlags |= (USBD_SHORT_TRANSFER_OK);
1380
1381 pUrb->UrbBulkOrInterruptTransfer.UrbLink = 0;
1382 break;
1383 }
1384 default:
1385 {
1386 AssertFailed();
1387 Status = STATUS_INVALID_PARAMETER;
1388 break;
1389 }
1390 }
1391
1392 if (!NT_SUCCESS(Status))
1393 {
1394 break;
1395 }
1396
1397 pContext->pDevExt = pDevExt;
1398 pContext->pMdlBuf = pMdlBuf;
1399 pContext->pUrb = pUrb;
1400 pContext->pOut = pUrbInfo;
1401 pContext->ulTransferType = pUrbInfo->type;
1402 pContext->ulMagic = VBOXUSB_MAGIC;
1403
1404 PIO_STACK_LOCATION pSl = IoGetNextIrpStackLocation(pIrp);
1405 pSl->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
1406 pSl->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
1407 pSl->Parameters.Others.Argument1 = pUrb;
1408 pSl->Parameters.Others.Argument2 = NULL;
1409
1410 IoSetCompletionRoutine(pIrp, vboxUsbRtUrbSendCompletion, pContext, TRUE, TRUE, TRUE);
1411 IoMarkIrpPending(pIrp);
1412 Status = IoCallDriver(pDevExt->pLowerDO, pIrp);
1413 AssertMsg(NT_SUCCESS(Status), (__FUNCTION__": IoCallDriver failed Status (0x%x)\n", Status));
1414 return STATUS_PENDING;
1415 } while (0);
1416
1417 Assert(!NT_SUCCESS(Status));
1418
1419 if (pMdlBuf)
1420 {
1421 MmUnlockPages(pMdlBuf);
1422 IoFreeMdl(pMdlBuf);
1423 }
1424
1425 if (pContext)
1426 vboxUsbMemFree(pContext);
1427
1428 VBoxDrvToolIoComplete(pIrp, Status, 0);
1429 vboxUsbDdiStateRelease(pDevExt);
1430 return Status;
1431}
1432
1433static NTSTATUS vboxUsbRtDispatchSendUrb(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1434{
1435 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1436 PFILE_OBJECT pFObj = pSl->FileObject;
1437 PUSBSUP_URB pUrbInfo = (PUSBSUP_URB)pIrp->AssociatedIrp.SystemBuffer;
1438 NTSTATUS Status;
1439
1440 do
1441 {
1442 if (!pFObj)
1443 {
1444 AssertFailed();
1445 Status = STATUS_INVALID_PARAMETER;
1446 break;
1447 }
1448
1449 if (!vboxUsbRtCtxIsOwner(pDevExt, pFObj))
1450 {
1451 AssertFailed();
1452 Status = STATUS_ACCESS_DENIED;
1453 break;
1454 }
1455
1456 if ( !pUrbInfo
1457 || pSl->Parameters.DeviceIoControl.InputBufferLength != sizeof (*pUrbInfo)
1458 || pSl->Parameters.DeviceIoControl.OutputBufferLength != sizeof (*pUrbInfo))
1459 {
1460 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
1461 Status = STATUS_INVALID_PARAMETER;
1462 break;
1463 }
1464 return vboxUsbRtUrbSend(pDevExt, pIrp, pUrbInfo);
1465 } while (0);
1466
1467 Assert(Status != STATUS_PENDING);
1468 VBoxDrvToolIoComplete(pIrp, Status, 0);
1469 vboxUsbDdiStateRelease(pDevExt);
1470 return Status;
1471}
1472
1473static NTSTATUS vboxUsbRtDispatchIsOperational(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1474{
1475 VBoxDrvToolIoComplete(pIrp, STATUS_SUCCESS, 0);
1476 vboxUsbDdiStateRelease(pDevExt);
1477 return STATUS_SUCCESS;
1478}
1479
1480static NTSTATUS vboxUsbRtDispatchGetVersion(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1481{
1482 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1483 PUSBSUP_VERSION pVer= (PUSBSUP_VERSION)pIrp->AssociatedIrp.SystemBuffer;
1484 NTSTATUS Status = STATUS_SUCCESS;
1485
1486 if (pVer && pSl->Parameters.DeviceIoControl.InputBufferLength == 0
1487 && pSl->Parameters.DeviceIoControl.OutputBufferLength == sizeof (*pVer))
1488 {
1489 pVer->u32Major = USBDRV_MAJOR_VERSION;
1490 pVer->u32Minor = USBDRV_MINOR_VERSION;
1491 }
1492 else
1493 {
1494 AssertMsgFailed((__FUNCTION__": STATUS_INVALID_PARAMETER\n"));
1495 Status = STATUS_INVALID_PARAMETER;
1496 }
1497
1498 Assert(Status != STATUS_PENDING);
1499 VBoxDrvToolIoComplete(pIrp, Status, sizeof (*pVer));
1500 vboxUsbDdiStateRelease(pDevExt);
1501 return Status;
1502}
1503
1504static NTSTATUS vboxUsbRtDispatchDefault(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1505{
1506 VBoxDrvToolIoComplete(pIrp, STATUS_INVALID_DEVICE_REQUEST, 0);
1507 vboxUsbDdiStateRelease(pDevExt);
1508 return STATUS_INVALID_DEVICE_REQUEST;
1509}
1510
1511DECLHIDDEN(NTSTATUS) vboxUsbRtCreate(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1512{
1513 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1514 PFILE_OBJECT pFObj = pSl->FileObject;
1515 if (!pFObj)
1516 {
1517 AssertFailed();
1518 return STATUS_INVALID_PARAMETER;
1519 }
1520
1521 return STATUS_SUCCESS;
1522}
1523
1524DECLHIDDEN(NTSTATUS) vboxUsbRtClose(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1525{
1526 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1527 PFILE_OBJECT pFObj = pSl->FileObject;
1528 Assert(pFObj);
1529
1530 vboxUsbRtCtxReleaseOwner(pDevExt, pFObj);
1531
1532 return STATUS_SUCCESS;
1533}
1534
1535DECLHIDDEN(NTSTATUS) vboxUsbRtDispatch(PVBOXUSBDEV_EXT pDevExt, PIRP pIrp)
1536{
1537 PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
1538 switch (pSl->Parameters.DeviceIoControl.IoControlCode)
1539 {
1540 case SUPUSB_IOCTL_USB_CLAIM_DEVICE:
1541 {
1542 return vboxUsbRtDispatchClaimDevice(pDevExt, pIrp);
1543 }
1544 case SUPUSB_IOCTL_USB_RELEASE_DEVICE:
1545 {
1546 return vboxUsbRtDispatchReleaseDevice(pDevExt, pIrp);
1547 }
1548 case SUPUSB_IOCTL_GET_DEVICE:
1549 {
1550 return vboxUsbRtDispatchGetDevice(pDevExt, pIrp);
1551 }
1552 case SUPUSB_IOCTL_USB_RESET:
1553 {
1554 return vboxUsbRtDispatchUsbReset(pDevExt, pIrp);
1555 }
1556 case SUPUSB_IOCTL_USB_SET_CONFIG:
1557 {
1558 return vboxUsbRtDispatchUsbSetConfig(pDevExt, pIrp);
1559 }
1560 case SUPUSB_IOCTL_USB_SELECT_INTERFACE:
1561 {
1562 return vboxUsbRtDispatchUsbSelectInterface(pDevExt, pIrp);
1563 }
1564 case SUPUSB_IOCTL_USB_CLEAR_ENDPOINT:
1565 {
1566 return vboxUsbRtDispatchUsbClearEndpoint(pDevExt, pIrp);
1567 }
1568 case SUPUSB_IOCTL_USB_ABORT_ENDPOINT:
1569 {
1570 return vboxUsbRtDispatchUsbAbortEndpoint(pDevExt, pIrp);
1571 }
1572 case SUPUSB_IOCTL_SEND_URB:
1573 {
1574 return vboxUsbRtDispatchSendUrb(pDevExt, pIrp);
1575 }
1576 case SUPUSB_IOCTL_IS_OPERATIONAL:
1577 {
1578 return vboxUsbRtDispatchIsOperational(pDevExt, pIrp);
1579 }
1580 case SUPUSB_IOCTL_GET_VERSION:
1581 {
1582 return vboxUsbRtDispatchGetVersion(pDevExt, pIrp);
1583 }
1584 default:
1585 {
1586 return vboxUsbRtDispatchDefault(pDevExt, pIrp);
1587 }
1588 }
1589}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette