vboxdev.cpp revision 59190ecd61435d19ba3515b876272aee7bd12298
/*++
Copyright (c) 2000 Microsoft Corporation
Module Name:
Abstract:
This file contains dispatch routines for create,
close and selective suspend.
The selective suspend feature is enabled if
the SSRegistryEnable key in the registry is set to 1.
Environment:
Kernel mode
Notes:
Copyright (c) 2000 Microsoft Corporation.
All Rights Reserved.
--*/
#include "vboxusb.h"
#include "vboxpnp.h"
#include "vboxpwr.h"
#include "vboxdev.h"
#include "vboxrwr.h"
#include <usbioctl.h>
#define _USBD_
#include <usbdlib.h>
#include <usbbusif.h>
static NTSTATUS VBoxUSBSetInterface(PDEVICE_EXTENSION pDevice, uint32_t InterfaceNumber, int AlternateSetting);
static NTSTATUS VBoxUSBClearEndpoint(PDEVICE_EXTENSION pDevice, uint32_t EndPointAddress, bool fReset);
NTSTATUS VBoxUSBSyncSendRequest(PDEVICE_EXTENSION deviceExtension, ULONG control_code, void *buffer);
/**
* Get USB descriptor
*
* @returns NT Status
* @param Pdo Device object
* @param buffer Descriptor buffer
* @param size size of buffer
* @param type descriptor type
* @param index descriptor index
* @param language_id descriptor language id
*/
NTSTATUS VBoxUSBGetDescriptor(PDEVICE_EXTENSION pDevice, void *buffer, int size, int type, int index, int language_id)
{
{
dprintf(("Failed to alloc mem for urb\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
{
dprintf(("VBoxUSBGetDescriptor: VBoxUSBSendIOCTL failed with %x (%x)\n", rc, urb->UrbHeader.Status));
}
return rc;
}
/**
* Free cached USB device/configuration descriptors
*
* @param pDevice USB device pointer
*/
{
unsigned i;
{
}
for (i = 0; i < MAX_CFGS; ++i)
{
{
}
}
}
/**
* Cache USB device/configuration descriptors
*
* @returns NT Status
* @param pDevice USB device pointer
*/
{
unsigned i;
/* Reading descriptors is relatively expensive and they aren't going to change
* except possibly when the device is reset (and even then only very rarely).
*/
/* Read device descriptor */
pDevice->usbdev.devdescr = (PUSB_DEVICE_DESCRIPTOR)ExAllocatePool(NonPagedPool, sizeof(USB_DEVICE_DESCRIPTOR));
{
dprintf(("Failed to alloc mem for device descriptor\n"));
goto end;
}
status = VBoxUSBGetDescriptor(pDevice, pDevice->usbdev.devdescr, sizeof(USB_DEVICE_DESCRIPTOR), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0);
if (!NT_SUCCESS(status))
{
AssertMsgFailed(("VBoxUSBCacheDescriptors: getting device descriptor failed\n"));
goto end;
}
tmp_cfgdescr = (USB_CONFIGURATION_DESCRIPTOR *) ExAllocatePool(NonPagedPool, sizeof(USB_CONFIGURATION_DESCRIPTOR));
if (!tmp_cfgdescr)
{
AssertMsgFailed(("VBoxUSBCacheDescriptors: ExAllocatePool failed\n"));
goto end;
}
/* Read descriptors for all configurations */
{
status = VBoxUSBGetDescriptor(pDevice, tmp_cfgdescr, sizeof(USB_CONFIGURATION_DESCRIPTOR), USB_CONFIGURATION_DESCRIPTOR_TYPE, i, 0);
if (!NT_SUCCESS(status))
{
AssertMsgFailed(("VBoxUSBCacheDescriptors: VBoxUSBGetDescriptor (cfg %d) failed with %x\n", i + 1, status));
goto end;
}
pDevice->usbdev.cfgdescr[i] = (USB_CONFIGURATION_DESCRIPTOR *)ExAllocatePool(NonPagedPool, uTotalLength);
{
AssertMsgFailed(("VBoxUSBCacheDescriptors: ExAllocatePool failed!\n"));
goto end;
}
status = VBoxUSBGetDescriptor(pDevice, pDevice->usbdev.cfgdescr[i], uTotalLength, USB_CONFIGURATION_DESCRIPTOR_TYPE, i, 0);
if (!NT_SUCCESS(status))
{
AssertMsgFailed(("VBoxUSBCacheDescriptors: VBoxUSBGetDescriptor (cfg %d) failed with %x\n", i + 1, status));
goto end;
}
}
end:
return status;
}
/**
* Free memory allocated by this module
*
* @param pDevice USB device pointer
*/
{
}
)
/*++
Routine Description:
Dispatch routine for create.
Arguments:
DeviceObject - pointer to device object
Irp - I/O request packet.
Return Value:
NT status value
--*/
{
// ULONG i;
// PVBOXUSB_PIPE_CONTEXT pipeContext;
// PUSBD_INTERFACE_INFORMATION interface;
PAGED_CODE();
dprintf(("VBoxUSB_DispatchCreate - begins\n"));
//
// initialize variables
//
}
#if 0
if(deviceExtension->UsbInterface) {
}
else {
dprintf(("UsbInterface not found\n"));
}
#endif
//
// FsContext is Null for the device
//
if(fileObject) {
}
else {
}
//
// opening a device as opposed to pipe.
//
//
// the device is idle if it has no open handles or pending PnP Irps
// since we just received an open handle request, cancel idle req.
//
if(deviceExtension->SSEnable) {
}
}
#if 0
if(pipeContext == NULL) {
}
for(i=0; i<interface->NumberOfPipes; i++) {
//
// found a match
//
dprintf(("open pipe %d\n", i));
//
// increment OpenHandleCounts
//
//
// the device is idle if it has no open handles or pending PnP Irps
// since we just received an open handle request, cancel idle req.
//
if(deviceExtension->SSEnable) {
}
}
}
#endif
dprintf(("VBoxUSB_DispatchCreate - ends\n"));
return ntStatus;
}
)
/*++
Routine Description:
Dispatch routine for close.
Arguments:
DeviceObject - pointer to device object
Irp - I/O request packet
Return Value:
NT status value
--*/
{
PAGED_CODE();
//
// initialize variables
//
pipeContext = NULL;
dprintf(("VBoxUSB_DispatchClose - begins\n"));
&fileObject->FileName);
}
}
}
//
// set ntStatus to STATUS_SUCCESS
//
dprintf(("VBoxUSB_DispatchClose - ends\n"));
#if 0
/* Force an unplug and re-plug to load the original Windows driver (or to give it to another running VM)
* Only when the device was actually claimed and it's still operational (not removed already for instance).
*/
if ( deviceExtension->OpenHandleCount == 0
{
}
else
dprintf(("Didn't cycle port OpenHandleCount=%d fClaimed=%d DeviceState=%d\n", deviceExtension->OpenHandleCount, deviceExtension->usbdev.fClaimed, deviceExtension->DeviceState));
#endif
return ntStatus;
}
)
/*++
Routine Description:
Dispatch routine for IRP_MJ_DEVICE_CONTROL
Arguments:
DeviceObject - pointer to device object
Irp - I/O request packet
Return Value:
NT status value
--*/
{
//
// initialize variables
//
info = 0;
{
dprintf(("Invalid device state\n"));
return ntStatus;
}
dprintf(("VBoxUSB_DispatchDevCtrl::"));
//
// It is true that the client driver cancelled the selective suspend
// request in the dispatch routine for create.
// But there is no guarantee that it has indeed been completed.
// so wait on the NoIdleReqPendEvent and proceed only if this event
// is signalled.
//
dprintf(("Waiting on the IdleReqPendEvent\n"));
//
// make sure that the selective suspend request has been completed.
//
if(deviceExtension->SSEnable)
{
NULL);
}
switch(code) {
{
dprintf(("SUPUSB_IOCTL_USB_CLAIM_DEVICE\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_GRAB_DEVICE: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
break;
}
{
if (NT_SUCCESS(ntStatus))
{
}
}
else
{
}
break;
}
{
/* Don't set fClaimed to false here, or else the device won't be returned to Windows afterwards */
break;
}
case SUPUSB_IOCTL_GET_DEVICE:
{
dprintf(("SUPUSB_IOCTL_GET_DEVICE\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_GET_DEVICE: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
break;
}
if (!NT_SUCCESS(ntStatus))
{
break;
}
if (!NT_SUCCESS(ntStatus))
{
break;
}
dprintf(("New device vid=%x pid=%x rev=%x\n", deviceExtension->usbdev.idVendor, deviceExtension->usbdev.idProduct, deviceExtension->usbdev.bcdDevice));
break;
}
case SUPUSB_IOCTL_USB_RESET:
{
dprintf(("SUPUSB_IOCTL_USB_RESET\n"));
if (ioBuffer || inputBufferLength != 0)
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_RESET: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
break;
}
break;
}
{
dprintf(("SUPUSB_IOCTL_USB_SET_CONFIG\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_SET_CONFIG: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
break;
}
break;
}
{
dprintf(("SUPUSB_IOCTL_USB_SELECT_INTERFACE\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_SELECT_INTERFACE: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
inputBufferLength, 0, outputBufferLength, 0));
break;
}
break;
}
{
dprintf(("SUPUSB_IOCTL_USB_CLEAR_ENDPOINT\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_CLEAR_ENDPOINT: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
inputBufferLength, 0, outputBufferLength, 0));
break;
}
break;
}
{
dprintf(("SUPUSB_IOCTL_USB_ABORT_ENDPOINT\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_USB_ABORT_ENDPOINT: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
inputBufferLength, 0, outputBufferLength, 0));
break;
}
break;
}
case SUPUSB_IOCTL_SEND_URB:
{
dprintf(("SUPUSB_IOCTL_SEND_URB\n"));
{
AssertMsgFailed(("SUPUSB_IOCTL_SEND_URB: Invalid input/output sizes. inputBufferLength=%d expected %d. outputBufferLength=%d expected %d.\n",
break;
}
break;
}
{
// if we get this far, then we're still up and running
break;
}
case SUPUSB_IOCTL_GET_VERSION:
{
{
AssertMsgFailed(("SUPUSB_IOCTL_GET_VERSION: Invalid input/output sizes. cbIn=%d expected %d. cbOut=%d expected %d.\n",
break;
}
break;
}
default :
break;
}
if (ntStatus != STATUS_PENDING)
{
}
else
{
// pIrp status already filled in by the lower layer (as we reuse the IRP for sending URBs)
}
dprintf(("VBoxUSB_DispatchDevCtrl::"));
return ntStatus;
}
)
/*++
Routine Description:
This routine synchronously submits a URB_FUNCTION_RESET_PIPE
request down the stack.
Arguments:
DeviceObject - pointer to device object
PipeHandle - pipe handle
Return Value:
NT status value
--*/
{
//
// initialize variables
//
if(urb) {
}
else {
}
if(NT_SUCCESS(ntStatus)) {
dprintf(("VBoxUSB_ResetPipe - success\n"));
}
else {
dprintf(("VBoxUSB_ResetPipe - failed\n"));
}
return ntStatus;
}
)
/*++
Routine Description:
This routine invokes VBoxUSB_ResetParentPort to reset the device
Arguments:
DeviceObject - pointer to device object
Return Value:
NT status value
--*/
{
dprintf(("VBoxUSB_ResetDevice - begins\n"));
if((NT_SUCCESS(ntStatus)) &&
(!(portStatus & USBD_PORT_ENABLED)) &&
(portStatus & USBD_PORT_CONNECTED)) {
}
dprintf(("VBoxUSB_ResetDevice - ends\n"));
return ntStatus;
}
)
/*++
Routine Description:
This routine retrieves the status value
Arguments:
DeviceObject - pointer to device object
PortStatus - port status
Return Value:
NT status value
--*/
{
//
// initialize variables
//
*PortStatus = 0;
dprintf(("VBoxUSB_GetPortStatus - begins\n"));
NULL,
0,
NULL,
0,
TRUE,
&event,
&ioStatus);
dprintf(("memory alloc for irp failed\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
if(STATUS_PENDING == ntStatus) {
}
else {
}
dprintf(("VBoxUSB_GetPortStatus - ends\n"));
return ntStatus;
}
)
/*++
Routine Description:
This routine sends an IOCTL_INTERNAL_USB_RESET_PORT
synchronously down the stack.
Arguments:
Return Value:
--*/
{
//
// initialize variables
//
dprintf(("VBoxUSB_ResetParentPort - begins\n"));
NULL,
0,
NULL,
0,
TRUE,
&event,
&ioStatus);
dprintf(("memory alloc for irp failed\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
if(STATUS_PENDING == ntStatus) {
}
else {
}
dprintf(("VBoxUSB_ResetParentPort - ends\n"));
return ntStatus;
}
)
/*++
Routine Description:
This routine builds an idle request irp with an associated callback routine
and a completion routine in the driver and passes the irp down the stack.
Arguments:
DeviceExtension - pointer to device extension
Return Value:
NT status value
--*/
{
//
// initialize variables
//
dprintf(("SubmitIdleRequest - begins\n"));
}
dprintf(("Idle request pending..\n"));
}
//
// clear the NoIdleReqPendEvent because we are about
// to submit an idle request. Since we are so early
// to clear this event, make sure that if we fail this
// request we set back the event.
//
idleCallbackInfo = (PUSB_IDLE_CALLBACK_INFO)ExAllocatePool(NonPagedPool, sizeof(struct _USB_IDLE_CALLBACK_INFO));
if(idleCallbackInfo) {
//
// we use IoAllocateIrp to create an irp to selectively suspend the
// device. This irp lies pending with the hub driver. When appropriate
// the hub driver will invoked callback, where we power down. The completion
// routine is invoked when we power back.
//
FALSE);
dprintf(("cannot build idle request irp\n"));
FALSE);
}
sizeof(struct _USB_IDLE_CALLBACK_INFO);
TRUE,
TRUE,
TRUE);
//
// we initialize the count to 2.
// The reason is, if the CancelSelectSuspend routine manages
// to grab the irp from the device extension, then the last of the
// CancelSelectSuspend routine/IdleNotificationRequestComplete routine
// to execute will free this irp. We need to have this schema so that
// 1. completion routine does not attempt to touch the irp freed by
// CancelSelectSuspend routine.
// 2. CancelSelectSuspend routine doesnt wait for ever for the completion
// routine to complete!
//
//
// check if the device is idle.
// A check here ensures that a race condition did not
// completely reverse the call sequence of SubmitIdleRequestIrp
// and CancelSelectiveSuspend
//
if(!CanDeviceSuspend(DeviceExtension) ||
//
// IRPs created using IoBuildDeviceIoControlRequest should be
// completed by calling IoCompleteRequest and not merely
// deallocated.
//
dprintf(("Device is not idle\n"));
FALSE);
if(idleCallbackInfo) {
}
//
// it is still safe to touch the local variable "irp" here.
// the irp has not been passed down the stack, the irp has
// no cancellation routine. The worse position is that the
// CancelSelectSuspend has run after we released the spin
// lock above. It is still essential to free the irp.
//
if(irp) {
}
}
dprintf(("Cancel the timers\n"));
//
// Cancel the timer so that the DPCs are no longer fired.
// Thus, we are making judicious usage of our resources.
// we do not need DPCs because we already have an idle irp pending.
// The timers are re-initialized in the completion routine.
//
if(!NT_SUCCESS(ntStatus)) {
dprintf(("IoCallDriver failed\n"));
}
}
else {
dprintf(("Memory allocation for idleCallbackInfo failed\n"));
FALSE);
}
dprintf(("SubmitIdleRequest - ends\n"));
return ntStatus;
}
)
/*++
Routine Description:
"A pointer to a callback function in your driver is passed down the stack with
this IOCTL, and it is this callback function that is called by USBHUB when it
safe for your device to power down."
"When the callback in your driver is called, all you really need to do is to
to first ensure that a WaitWake Irp has been submitted for your device, if
remote wake is possible for your device and then request a SetD2 (or DeviceWake)"
Arguments:
DeviceExtension - pointer to device extension
Return Value:
NT status value
--*/
{
dprintf(("IdleNotificationCallback - begins\n"));
//
// Dont idle, if the device was just disconnected or being stopped
// i.e. return for the following DeviceState(s)
// NotStarted, Stopped, PendingStop, PendingRemove, SurpriseRemoved, Removed
//
return;
}
//
// If there is not already a WW IRP pending, submit one now
//
if(DeviceExtension->WaitWakeEnable) {
}
//
// power down the device
//
sizeof(IRP_COMPLETION_CONTEXT));
if(!irpContext) {
dprintf(("Failed to alloc memory for irpContext\n"));
}
else {
//
// increment the count. In the HoldIoRequestWorkerRoutine, the
// count is decremented twice (one for the system Irp and the
// other for the device Irp. An increment here compensates for
// the sytem irp..The decrement corresponding to this increment
// is in the completion function
//
dprintf(("IdleNotificationCallback::"));
NULL);
if(STATUS_PENDING == ntStatus) {
dprintf(("IdleNotificationCallback::"
"waiting for the power irp to complete\n"));
NULL);
}
}
if(!NT_SUCCESS(ntStatus)) {
if(irpContext) {
}
}
dprintf(("IdleNotificationCallback - ends\n"));
}
)
/*++
Routine Description:
Completion routine for idle notification irp
Arguments:
DeviceObject - pointer to device object
Irp - I/O request packet
DeviceExtension - pointer to device extension
Return Value:
NT status value
--*/
{
dprintf(("IdleNotificationRequestCompete - begins\n"));
//
// check the Irp status
//
dprintf(("Idle irp completes with error::"));
switch(ntStatus) {
dprintf(("STATUS_INVALID_DEVICE_REQUEST\n"));
break;
case STATUS_CANCELLED:
dprintf(("STATUS_CANCELLED\n"));
break;
dprintf(("STATUS_POWER_STATE_INVALID\n"));
case STATUS_DEVICE_BUSY:
dprintf(("STATUS_DEVICE_BUSY\n"));
break;
default:
break;
}
//
// if in error, issue a SetD0 (only when not in D0)
//
dprintf(("IdleNotificationRequestComplete::"));
NULL);
if(!NT_SUCCESS(ntStatus)) {
dprintf(("PoRequestPowerIrp failed\n"));
}
}
}
NULL);
if(idleCallbackInfo) {
}
//
// since the irp was created using IoAllocateIrp,
// the Irp needs to be freed using IoFreeIrp.
// Also return STATUS_MORE_PROCESSING_REQUIRED so that
// the kernel does not reference this in the near future.
//
if(idleIrp) {
dprintf(("completion routine has a valid irp and frees it\n"));
FALSE);
}
else {
//
// The CancelSelectiveSuspend routine has grabbed the Irp from the device
// extension. Now the last one to decrement the FreeIdleIrpCount should
// free the irp.
//
dprintf(("completion routine frees the irp\n"));
FALSE);
}
}
if(DeviceExtension->SSEnable) {
dprintf(("Set the timer to fire DPCs\n"));
IDLE_INTERVAL, // 5000 ms
dprintf(("IdleNotificationRequestCompete - ends\n"));
}
return STATUS_MORE_PROCESSING_REQUIRED;
}
)
/*++
Routine Description:
This routine is invoked to cancel selective suspend request.
Arguments:
DeviceExtension - pointer to device extension
Return Value:
None.
--*/
{
dprintf(("CancelSelectSuspend - begins\n"));
{
dprintf(("Device is not idle\n"));
NULL);
}
//
// since we have a valid Irp ptr,
// we can call IoCancelIrp on it,
// without the fear of the irp
// being freed underneath us.
//
if(irp) {
//
// This routine has the irp pointer.
// It is safe to call IoCancelIrp because we know that
// the compleiton routine will not free this irp unless...
//
//
if(IoCancelIrp(irp)) {
dprintf(("IoCancelIrp returns TRUE\n"));
}
else {
dprintf(("IoCancelIrp returns FALSE\n"));
}
//
// ....we decrement the FreeIdleIrpCount from 2 to 1.
// if completion routine runs ahead of us, then this routine
// decrements the FreeIdleIrpCount from 1 to 0 and hence shall
// free the irp.
//
dprintf(("CancelSelectSuspend frees the irp\n"));
FALSE);
}
}
dprintf(("CancelSelectSuspend - ends\n"));
return;
}
)
/*++
Routine Description:
Completion routine for power irp PoRequested in
IdleNotificationCallback.
Arguments:
DeviceObject - pointer to device object
MinorFunciton - minor function for the irp.
PowerState - irp power state
Context - context passed to the completion function
IoStatus - status block.
Return Value:
None
--*/
{
//
// initialize variables
//
irpContext = NULL;
if(Context) {
}
//
// all we do is set the event and decrement the count
//
if(irpContext) {
dprintf(("PoIrpCompletionFunc::"));
}
return;
}
)
/*++
Routine Description:
Completion routine for power irp PoRequested in IdleNotification
RequestComplete routine.
Arguments:
DeviceObject - pointer to device object
MinorFunciton - minor function for the irp.
PowerState - irp power state
Context - context passed to the completion function
IoStatus - status block.
Return Value:
None
--*/
{
//
// initialize variables
//
//
// all we do is decrement the count
//
dprintf(("PoIrpAsyncCompletionFunc::"));
return;
}
)
/*++
Routine Description:
Completion routine for PoRequest wait wake irp
Arguments:
DeviceObject - pointer to device object
MinorFunciton - minor function for the irp.
PowerState - irp power state
Context - context passed to the completion function
IoStatus - status block.
Return Value:
None
--*/
{
//
// initialize variables
//
//
// all we do is decrement the count
//
dprintf(("WWIrpCompletionFunc::"));
return;
}
/**
* Free per-device interface info
*
* @param pDevice USB device pointer
* @param fAbortPipes If true, also abort any open pipes
*/
{
unsigned i;
unsigned j;
/*
* Free old interface info
*/
{
{
{
if (fAbortPipes)
{
{
dprintf(("Aborting Pipe %d handle %x address %x\n", j,
VBoxUSBClearPipe(pDevice, pDevice->usbdev.pVBIfaceInfo[i].pInterfaceInfo->Pipes[j].PipeHandle, FALSE);
}
}
}
}
}
}
/**
* Return handle of pipe that corresponds to given endpoint address
*
* @returns Pipe handle (or 0 when not found)
* @param pDevice USB device pointer
* @param EndPointAddress end point address
*/
{
unsigned i, j;
{
{
/* Note that bit 7 determines pipe direction, but is still significant
* because endpoints may be numbered like 0x01, 0x81, 0x02, 0x82 etc.
*/
}
}
return 0;
}
/**
* Return pipe state information for given endpoint address
*
* @returns Pointer to pipe state (or NULL if not found)
* @param pDevice USB device pointer
* @param EndPointAddress end point address
*/
{
unsigned i, j;
{
{
}
}
return NULL;
}
/**
* Get a valid USB string descriptor language ID (the first ID found).
*
* @returns NT Status
* @param pDevice device extension
* @param lang_id pointer to language id
*/
{
unsigned length;
char buffer[MAXIMUM_USB_STRING_LENGTH];
*lang_id = 0;
if (!NT_SUCCESS(status))
{
dprintf(("VBoxUSBGetLangID: language ID table not present (?)\n"));
goto fail;
}
/* Just grab the first lang ID if available. In 99% cases, it will be US English (0x0409).*/
{
}
else
fail:
return status;
}
/**
* Query device descriptor
*
* @returns NT Status
* @param pDevice USB device pointer
*/
{
{
dprintf(("Failed to alloc mem for urb\n"));
goto fail;
}
status = VBoxUSBGetDescriptor(pDevice, devdescr, sizeof(*devdescr), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0);
if (!NT_SUCCESS(status))
{
AssertMsgFailed(("VBoxUSBGetDeviceDescription: getting device descriptor failed\n"));
goto fail;
}
dprintf(("Device pid=%x vid=%x rev=%x\n", devdescr->idVendor, devdescr->idProduct, devdescr->bcdDevice));
int langId = 0;
if (devdescr->iSerialNumber
#ifdef DEBUG
#endif
)
{
if (!NT_SUCCESS(status))
{
dprintf(("VBoxUSBGetDeviceDescription: no language ID (?)\n"));
goto fail;
}
if (!pstrdescr)
{
AssertMsgFailed(("VBoxUSBGetDeviceDescription: ExAllocatePool failed\n"));
goto fail;
}
status = VBoxUSBGetDescriptor(pDevice, pstrdescr, uLength, USB_STRING_DESCRIPTOR_TYPE, devdescr->iSerialNumber, langId);
if (!NT_SUCCESS(status))
{
dprintf(("VBoxUSBGetDeviceDescription: no serial string present (?)\n"));
goto fail;
}
/* Did we get a string back or not? */
{
}
}
#ifdef DEBUG
if (devdescr->iManufacturer)
{
status = VBoxUSBGetDescriptor(pDevice, pstrdescr, uLength, USB_STRING_DESCRIPTOR_TYPE, devdescr->iManufacturer, langId);
if (!NT_SUCCESS(status))
{
dprintf(("VBoxUSBGetDeviceDescription: no manufacturer string present (?)\n"));
goto fail;
}
{
}
}
{
status = VBoxUSBGetDescriptor(pDevice, pstrdescr, uLength, USB_STRING_DESCRIPTOR_TYPE, devdescr->iProduct, langId);
if (!NT_SUCCESS(status))
{
dprintf(("VBoxUSBGetDeviceDescription: no product string present (?)\n"));
goto fail;
}
{
}
}
#endif
fail:
if (pstrdescr)
if (devdescr)
return status;
}
{
if (!urb)
{
AssertMsgFailed(("VBoxUSBClearPipe: ExAllocatePool failed!\n"));
goto end;
}
if (fReset)
else
{
// pretend success
dprintf(("Resetting the control pipe??\n"));
goto end;
}
{
AssertMsgFailed(("VBoxUSBClearPipe: VBoxUSBSendIOCTL failed with %x (%x)\n", status, urb->UrbHeader.Status));
goto end;
}
end:
return status;
}
static NTSTATUS VBoxUSBClearEndpoint(PDEVICE_EXTENSION pDevice, uint32_t EndPointAddress, bool fReset)
{
if (!NT_SUCCESS(status))
{
AssertMsgFailed(("VBoxUSBClearEndpoint: VBoxUSBSendIOCTL failed with %x (%x)\n", status, urb->UrbHeader.Status));
}
return status;
}
static USB_CONFIGURATION_DESCRIPTOR *VBoxUSBFindConfigDesc(PDEVICE_EXTENSION pDevice, unsigned uConfigValue)
{
unsigned i;
for (i = 0; i < MAX_CFGS; ++i)
{
{
{
break;
}
}
}
return cfgdescr;
}
/**
* Select USB interface
*
* @returns NT Status
* @param pDevice USB device pointer
* @param InterfaceNumber interface number
* @param AlternateSetting alternate setting
*/
NTSTATUS VBoxUSBSetInterface(PDEVICE_EXTENSION pDevice, uint32_t InterfaceNumber, int AlternateSetting)
{
unsigned i;
{
AssertMsgFailed(("Can't select an interface without an active configuration\n"));
goto end;
}
{
goto end;
}
if (!cfgdescr)
{
AssertMsgFailed(("VBoxUSBSetInterface: configuration %d not found!!\n", pDevice->usbdev.uConfigValue));
goto end;
}
dprintf(("Calling USBD_ParseConfigurationDescriptorEx...\n"));
ifacedesc = USBD_ParseConfigurationDescriptorEx(cfgdescr, cfgdescr, InterfaceNumber, AlternateSetting, -1, -1, -1);
if (!ifacedesc)
{
AssertMsgFailed(("VBoxUSBSetInterface: invalid interface %d or alternate setting %d\n", InterfaceNumber, AlternateSetting));
goto end;
}
dprintf(("USBD_ParseConfigurationDescriptorEx successful\n"));
if (!urb)
{
AssertMsgFailed(("VBoxUSBSetInterface: ExAllocatePool failed!\n"));
goto end;
}
/*
* Free old interface and pipe info, allocate new again
*/
/* Clear pipes associated with the interface, else Windows may hang. */
{
dprintf(("Aborting Pipe %d handle %x address %x\n", i,
VBoxUSBClearPipe(pDevice, pDevice->usbdev.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo->Pipes[i].PipeHandle, FALSE);
}
}
pDevice->usbdev.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION) ExAllocatePool(NonPagedPool, uTotalIfaceInfoLength);
{
AssertMsgFailed(("VBoxUSBSetInterface: ExAllocatePool failed!\n"));
goto end;
}
if (ifacedesc->bNumEndpoints > 0) {
pDevice->usbdev.pVBIfaceInfo[InterfaceNumber].pPipeInfo = (VBOXUSB_PIPE_INFO *) ExAllocatePool(NonPagedPool, ifacedesc->bNumEndpoints * sizeof(VBOXUSB_PIPE_INFO));
{
AssertMsgFailed(("VBoxUSBSetInterface: ExAllocatePool failed!\n"));
goto end;
}
}
else
UsbBuildSelectInterfaceRequest(urb, uUrbSize, pDevice->usbdev.hConfiguration, InterfaceNumber, AlternateSetting);
// status = VBoxUSBSyncSendRequest(pDevice, IOCTL_INTERNAL_USB_SUBMIT_URB, urb);
{
AssertMsgFailed(("VBoxUSBSetInterface: VBoxUSBSendIOCTL failed with %x (%x)\n", status, urb->UrbHeader.Status));
goto end;
}
memcpy(pDevice->usbdev.pVBIfaceInfo[InterfaceNumber].pInterfaceInfo, ifaceinfo, GET_USBD_INTERFACE_SIZE(ifacedesc->bNumEndpoints));
for(i=0;i<ifaceinfo->NumberOfPipes;i++)
{
dprintf(("Pipe %d: handle %x address %x transfer size=%d\n", i, ifaceinfo->Pipes[i].PipeHandle, ifaceinfo->Pipes[i].EndpointAddress, ifaceinfo->Pipes[i].MaximumTransferSize));
pDevice->usbdev.pVBIfaceInfo[InterfaceNumber].pPipeInfo[i].EndpointAddress = ifaceinfo->Pipes[i].EndpointAddress;
}
end:
return status;
}
/**
* Select USB configuration
*
* @returns NT Status
* @param pDevice USB device pointer
* @param uConfiguration configuration value
*/
{
unsigned i;
if (uConfiguration == 0)
{
{
dprintf(("VBoxUSBSetConfig: Failed to alloc mem for urb\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
/* Before setting the configuration, free any existing interface information.
* Also abort any open pipes, else the set config request will hang.
*/
{
AssertMsgFailed(("VBoxUSBSetConfig: VBoxUSBSendIOCTL failed with %x (%x)\n", status, urb->UrbHeader.Status));
return status;
}
return status;
}
if (!cfgdescr)
{
goto end;
}
interfaces = (USBD_INTERFACE_LIST_ENTRY *) ExAllocatePool(NonPagedPool,(cfgdescr->bNumInterfaces + 1) * sizeof(USBD_INTERFACE_LIST_ENTRY));
if (!interfaces)
{
AssertMsgFailed(("VBoxUSBSetConfig: ExAllocatePool failed!\n"));
goto end;
}
for(i=0;i<cfgdescr->bNumInterfaces;i++)
{
interfaces[i].InterfaceDescriptor = USBD_ParseConfigurationDescriptorEx(cfgdescr, cfgdescr, i, 0, -1, -1, -1);
if (!interfaces[i].InterfaceDescriptor)
{
AssertMsgFailed(("VBoxUSBSetConfig: interface %d not found\n", i));
goto end;
}
else
{
dprintf(("VBoxUSBSetConfig: interface %d found\n", i));
}
}
if (!urb)
{
AssertMsgFailed(("VBoxUSBSetConfig: USBD_CreateConfigurationRequestEx failed!\n"));
goto end;
}
{
AssertMsgFailed(("VBoxUSBSetConfig: VBoxUSBSendIOCTL failed with %x (%x)\n", status, urb->UrbHeader.Status));
goto end;
}
/*
* Free per-device interface info
*/
/*
* Allocate room for interface pointer array
*/
pDevice->usbdev.pVBIfaceInfo = (VBOXUSB_IFACE_INFO *)ExAllocatePool(NonPagedPool, pDevice->usbdev.uNumInterfaces * sizeof(VBOXUSB_IFACE_INFO));
{
AssertMsgFailed(("VBoxUSBSetConfig: ExAllocatePool failed!\n"));
goto end;
}
memset(pDevice->usbdev.pVBIfaceInfo, 0, pDevice->usbdev.uNumInterfaces * sizeof(VBOXUSB_IFACE_INFO));
/*
* And fill in the information for all interfaces
*/
{
uint32_t uTotalIfaceInfoLength = sizeof(struct _URB_SELECT_INTERFACE) + ((interfaces[i].Interface->NumberOfPipes > 0) ? (interfaces[i].Interface->NumberOfPipes - 1) : 0) * sizeof(USBD_PIPE_INFORMATION);
pDevice->usbdev.pVBIfaceInfo[i].pInterfaceInfo = (PUSBD_INTERFACE_INFORMATION) ExAllocatePool(NonPagedPool, uTotalIfaceInfoLength);
{
AssertMsgFailed(("VBoxUSBSetConfig: ExAllocatePool failed!\n"));
goto end;
}
{
pDevice->usbdev.pVBIfaceInfo[i].pPipeInfo = (VBOXUSB_PIPE_INFO *) ExAllocatePool(NonPagedPool, interfaces[i].Interface->NumberOfPipes * sizeof(VBOXUSB_PIPE_INFO));
{
AssertMsgFailed(("VBoxUSBSetConfig: ExAllocatePool failed!\n"));
goto end;
}
}
else
{
dprintf(("Pipe %d: handle %x address %x transfer size=%d\n", j, interfaces[i].Interface->Pipes[j].PipeHandle, interfaces[i].Interface->Pipes[j].EndpointAddress, interfaces[i].Interface->Pipes[j].MaximumTransferSize));
pDevice->usbdev.pVBIfaceInfo[i].pPipeInfo[j].EndpointAddress = interfaces[i].Interface->Pipes[j].EndpointAddress;
}
}
end:
return status;
}
// Generic completion routine for simple requests
static NTSTATUS _stdcall VBoxUSB_StopCompletion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
{
return STATUS_MORE_PROCESSING_REQUIRED;
}
{
struct _URB_GET_CURRENT_FRAME_NUMBER urb;
// initialize the urb
dprintf(("VBoxUSB_GetCurrentFrame::"));
dprintf(("VBoxUSB_GetCurrentFrame::"));
return urb.FrameNumber;
}
/**
* Query device speed (High-speed vs. low/full-speed)
*
* @returns NT Status
* @param pDevice USB device pointer
*/
{
AssertMsgFailed(("VBoxUSBGetDeviceSpeed: Failed to allocate IRP!\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
// All PnP IRPs need the status field initialized to STATUS_NOT_SUPPORTED
// Set the completion routine, which will signal the event
&event,
TRUE, // InvokeOnSuccess
TRUE, // InvokeOnError
TRUE); // InvokeOnCancel
// Allocate memory for an interface of type
// USB_BUS_INTERFACE_USBDI_V1 and set up the IRP
dprintf(("VBoxUSBGetDeviceSpeed::"));
if(STATUS_PENDING == ntStatus) {
}
if(NT_SUCCESS(ntStatus)) {
}
dprintf(("VBoxUSBGetDeviceSpeed::"));
return ntStatus;
}
static NTSTATUS _stdcall VBoxUSBIOCTLAsyncCompletion(DEVICE_OBJECT *device_object, IRP *pIrp, void *context)
{
if (!context)
{
AssertFailed();
return STATUS_CONTINUE_COMPLETION;
}
{
AssertMsgFailed(("Invalid context magic!!\n"));
return STATUS_CONTINUE_COMPLETION;
}
dprintf(("VBoxURBAsyncCompletion %p status=%x URB %p IRQL=%d\n", pIrp, pIrp->IoStatus.Status, urb, KeGetCurrentIrql()));
{
AssertFailed();
if (pDevExt)
return STATUS_CONTINUE_COMPLETION;
}
////rt(MmIsAddressValid(pOut) == TRUE);
if (ntStatus == STATUS_SUCCESS)
{
{
case USBD_STATUS_CRC:
break;
case USBD_STATUS_SUCCESS:
break;
case USBD_STATUS_STALL_PID:
break;
AssertFailed(); // SW error - we probably messed up
// fall through
default:
break;
}
switch(pContext->ulTransferType)
{
case USBSUP_TRANSFER_TYPE_MSG:
{
/* QUSB_TRANSFER_TYPE_MSG is a control transfer, but it is special
* the first 8 bytes of the buffer is the setup packet so the real
* data length is therefore urb->len - 8
*/
}
break;
break;
&& !(pOut->flags & USBSUP_FLAG_SHORT_OK) && (pOut->len > urb->UrbBulkOrInterruptTransfer.TransferBufferLength))
{
/* If we don't use the USBD_SHORT_TRANSFER_OK flag, the returned buffer lengths are
* wrong for short transfers (always a multiple of max packet size?). So we just figure
* out if this was a data underrun on our own.
*/
}
break;
}
}
else
{
#ifdef DEBUG
switch(pContext->ulTransferType)
{
case USBSUP_TRANSFER_TYPE_MSG:
break;
break;
break;
}
#endif
{
case USBD_STATUS_CRC:
break;
case USBD_STATUS_STALL_PID:
break;
break;
case ((USBD_STATUS)0xC0010000L): // USBD_STATUS_CANCELED - too bad usbdi.h and usb.h aren't consistent!
// TODO: What the heck are we really supposed to do here?
break;
case USBD_STATUS_BAD_START_FRAME: // This one really shouldn't happen
break;
default:
break;
}
}
// For isochronous transfers, always update the individual packets
{
for (unsigned i = 0; i < pOut->numIsoPkts; ++i)
{
{
case USBD_STATUS_SUCCESS:
break;
case USBD_STATUS_NOT_ACCESSED:
break;
default:
break;
}
}
}
dprintf(("VBoxUSBIOCTLAsyncCompletion::"));
// Number of bytes returned
return STATUS_CONTINUE_COMPLETION;
}
{
// Isochronous transfers use multiple packets -> variable URB size
{
}
else
pContext = (PVBOXUSB_URB_CONTEXT)ExAllocatePool(NonPagedPool, urbSize + sizeof(VBOXUSB_URB_CONTEXT));
{
dprintf(("Failed to alloc mem for urb\n"));
goto SendUrbFailure;
}
PipeHandle = 0;
{
PipeHandle = VBoxUSBGetPipeHandle(pDevExt, pIn->ep | ((pIn->dir == USBSUP_DIRECTION_IN) ? 0x80 : 0x00));
if (PipeHandle == 0)
{
goto SendUrbFailure;
}
}
/*
* Allocate pMdl for the user mode data buffer
*/
if (!pMdlBuf)
{
goto SendUrbFailure;
}
{
}
{
status = GetExceptionCode();
goto SendUrbFailure;
}
/* For some reason, passing a MDL in the URB does not work reliably. Notably
* the iPhone when used with iTunes fails.
*/
if (!pBuffer)
{
AssertMsgFailed(("MmGetSystemAddressForMdlSafe failed for buffer!!\n"));
goto SendUrbFailure;
}
/* Setup URB */
{
case USBSUP_TRANSFER_TYPE_MSG:
urb->UrbControlTransfer.TransferFlags = ((pIn->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
if (PipeHandle == 0)
{
/* QUSB_TRANSFER_TYPE_MSG is a control transfer, but it is special
* the first 8 bytes of the buffer is the setup packet so the real
* data length is therefore urb->len - 8
*/
memcpy(urb->UrbControlTransfer.SetupPacket, pBuffer, min(sizeof(urb->UrbControlTransfer.SetupPacket), pIn->len));
else
urb->UrbControlTransfer.TransferBuffer = (uint8_t *)pBuffer + sizeof(urb->UrbControlTransfer.SetupPacket);
}
else
{
}
break;
pipeInfo = VBoxUSBGetPipeState(pDevExt, pIn->ep | ((pIn->dir == USBSUP_DIRECTION_IN) ? 0x80 : 0x00));
{
/* Can happen if the isoc request comes in too early or late. */
AssertMsgFailed(("pipeInfo not found!!\n"));
goto SendUrbFailure;
}
urb->UrbIsochronousTransfer.TransferFlags = ((pIn->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
for (unsigned i = 0; i < pOut->numIsoPkts; ++i)
{
}
/* We have to schedule the URBs ourselves. There is an ASAP flag but
* almost completely useless.
*/
break;
// Interrupt transfers must have USBD_TRANSFER_DIRECTION_IN according to the DDK
urb->UrbBulkOrInterruptTransfer.TransferFlags = ((pIn->dir == USBSUP_DIRECTION_IN) ? USBD_TRANSFER_DIRECTION_IN : USBD_TRANSFER_DIRECTION_OUT);
break;
default:
AssertFailed();
goto SendUrbFailure;
}
// Reuse the original IRP
dprintf(("VBoxUSBSendURB::"));
if (!NT_SUCCESS(status))
{
}
/* We've marked the IRP as pending, so we must return STATUS_PENDING regardless of what IoCallDriver returns. */
return STATUS_PENDING;
if (pMdlBuf)
{
}
if (pContext)
return status;
}
{
pIrp = IoBuildDeviceIoControlRequest(control_code, pDevExt->TopOfStackDeviceObject, NULL, 0, NULL, 0, TRUE, &event, &io_status);
if (!pIrp)
{
AssertMsgFailed(("IoBuildDeviceIoControlRequest failed!!\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
dprintf(("VBoxUSBSendIOCTL::"));
if (status == STATUS_PENDING)
{
dprintf(("IoCallDriver returned STATUS_PENDING!!\n"));
}
dprintf(("VBoxUSBSendIOCTL::"));
return status;
}
#if 0 /* dead code */
{
return STATUS_MORE_PROCESSING_REQUIRED;
}
NTSTATUS VBoxUSBSyncSendRequest(PDEVICE_EXTENSION deviceExtension, ULONG control_code, void *buffer)
{
// VBoxUsb_DbgPrint(2, ("enter: VBoxUsb_SyncSendUsbRequest\n"));
// Initialize the event we'll wait on
// Allocate the Irp
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Set the Irp parameters
// Set the completion routine, which will signal the event
TRUE, // InvokeOnSuccess
TRUE, // InvokeOnError
TRUE); // InvokeOnCancel
// Pass the Irp & Urb down the stack
// If the request is pending, block until it completes
if (ntStatus == STATUS_PENDING)
{
// Specify a timeout of 5 seconds to wait for this call to complete.
if (ntStatus == STATUS_TIMEOUT)
{
// Cancel the Irp we just sent.
// And wait until the cancel completes
}
else
{
}
}
// Done with the Irp, now free it.
// VBoxUsb_DbgPrint(2, ("exit: VBoxUsb_SyncSendUsbRequest %08X\n", ntStatus));
return ntStatus;
}
#endif