VBOXUSBTOOL_DECL(NTSTATUS) VBoxUsbToolPipeClear(PDEVICE_OBJECT pDevObj, HANDLE hPipe, bool fReset) { if (!hPipe) { Log(("Resetting the control pipe??\n")); return STATUS_SUCCESS; } USHORT u16Function = fReset ? URB_FUNCTION_RESET_PIPE : URB_FUNCTION_ABORT_PIPE; PURB pUrb = VBoxUsbToolUrbAlloc(u16Function, sizeof (struct _URB_PIPE_REQUEST)); if (!pUrb) { AssertMsgFailed((__FUNCTION__": VBoxUsbToolUrbAlloc failed!\n")); return STATUS_INSUFFICIENT_RESOURCES; } pUrb->UrbPipeRequest.PipeHandle = hPipe; pUrb->UrbPipeRequest.Reserved = 0; NTSTATUS Status = VBoxUsbToolUrbPost(pDevObj, pUrb, RT_INDEFINITE_WAIT); if (!NT_SUCCESS(Status) || !USBD_SUCCESS(pUrb->UrbHeader.Status)) { AssertMsgFailed((__FUNCTION__": vboxUsbToolRequest failed with %x (%x)\n", Status, pUrb->UrbHeader.Status)); } VBoxUsbToolUrbFree(pUrb); return Status; }
void CUsbDkRedirectorStrategy::IsoRWCompletion(WDFREQUEST Request, WDFIOTARGET, PWDF_REQUEST_COMPLETION_PARAMS CompletionParams, WDFCONTEXT) { CRedirectorRequest WdfRequest(Request); auto Context = WdfRequest.Context(); CPreAllocatedWdfMemoryBufferT<URB> urb(CompletionParams->Parameters.Usb.Completion->Parameters.PipeUrb.Buffer); CPreAllocatedWdfMemoryBufferT<USB_DK_ISO_TRANSFER_RESULT> IsoPacketResult(Context->LockedIsochronousResultsArray); ASSERT(urb->UrbIsochronousTransfer.NumberOfPackets == IsoPacketResult.ArraySize()); *Context->BytesTransferred = 0; for (ULONG i = 0; i < urb->UrbIsochronousTransfer.NumberOfPackets; i++) { IsoPacketResult[i].actualLength = urb->UrbIsochronousTransfer.IsoPacket[i].Length; IsoPacketResult[i].transferResult = urb->UrbIsochronousTransfer.IsoPacket[i].Status; *Context->BytesTransferred += IsoPacketResult[i].actualLength; } auto status = CompletionParams->IoStatus.Status; if (!NT_SUCCESS(status)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_REDIRECTOR, "%!FUNC! Request completion error %!STATUS!", status); } else if (!USBD_SUCCESS(urb->UrbHeader.Status)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_REDIRECTOR, "%!FUNC! USB request completion error %lu", urb->UrbHeader.Status); status = STATUS_INVALID_DEVICE_REQUEST; } WdfRequest.SetOutputDataLen(sizeof(*Context->BytesTransferred)); WdfRequest.SetStatus(status); }
NTSTATUS abort_endpoint(libusb_device_t *dev, int endpoint, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB urb; USBMSG("endpoint: 0x%02x timeout: %d\n", endpoint, timeout); memset(&urb, 0, sizeof(struct _URB_PIPE_REQUEST)); if (!dev->config.value) { USBERR0("invalid configuration 0\n"); return STATUS_INVALID_DEVICE_STATE; } if (!get_pipe_handle(dev, endpoint, &urb.UrbPipeRequest.PipeHandle)) { USBERR0("getting endpoint pipe failed\n"); return STATUS_INVALID_PARAMETER; } urb.UrbHeader.Length = (USHORT) sizeof(struct _URB_PIPE_REQUEST); urb.UrbHeader.Function = URB_FUNCTION_ABORT_PIPE; status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { USBERR("request failed: status: 0x%x, urb-status: 0x%x\n", status, urb.UrbHeader.Status); } return status; }
NTSTATUS get_status(libusb_device_t *dev, int recipient, int index, char *status, int *ret, int timeout) { NTSTATUS _status = STATUS_SUCCESS; URB urb; DEBUG_PRINT_NL(); DEBUG_MESSAGE("get_status(): recipient %02d", recipient); DEBUG_MESSAGE("get_status(): index %04d", index); DEBUG_MESSAGE("get_status(): timeout %d", timeout); memset(&urb, 0, sizeof(URB)); switch(recipient) { case USB_RECIP_DEVICE: urb.UrbHeader.Function = URB_FUNCTION_GET_STATUS_FROM_DEVICE; break; case USB_RECIP_INTERFACE: urb.UrbHeader.Function = URB_FUNCTION_GET_STATUS_FROM_INTERFACE; break; case USB_RECIP_ENDPOINT: urb.UrbHeader.Function = URB_FUNCTION_GET_STATUS_FROM_ENDPOINT; break; case USB_RECIP_OTHER: urb.UrbHeader.Function = URB_FUNCTION_GET_STATUS_FROM_OTHER; break; default: DEBUG_ERROR("get_status(): invalid recipient"); return STATUS_INVALID_PARAMETER; } urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_GET_STATUS_REQUEST); urb.UrbControlGetStatusRequest.TransferBufferLength = 2; urb.UrbControlGetStatusRequest.TransferBuffer = status; urb.UrbControlGetStatusRequest.Index = (USHORT)index; _status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if(!NT_SUCCESS(_status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { DEBUG_ERROR("get_status(): getting status failed: " "status: 0x%x, urb-status: 0x%x", _status, urb.UrbHeader.Status); *ret = 0; } else { *ret = urb.UrbControlGetStatusRequest.TransferBufferLength; } return _status; }
NTSTATUS DDKAPI transfer_complete(DEVICE_OBJECT *device_object, IRP *irp, void *context) { context_t *c = (context_t *)context; int transmitted = 0; libusb_device_t *dev = device_object->DeviceExtension; if(irp->PendingReturned) { IoMarkIrpPending(irp); } if(NT_SUCCESS(irp->IoStatus.Status) && USBD_SUCCESS(c->urb->UrbHeader.Status)) { if(c->urb->UrbHeader.Function == URB_FUNCTION_ISOCH_TRANSFER) { transmitted = c->urb->UrbIsochronousTransfer.TransferBufferLength; } if(c->urb->UrbHeader.Function == URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER) { transmitted = c->urb->UrbBulkOrInterruptTransfer.TransferBufferLength; } DEBUG_MESSAGE("transfer_complete(): sequence %d: %d bytes transmitted", c->sequence, transmitted); } else { if(irp->IoStatus.Status == STATUS_CANCELLED) { DEBUG_ERROR("transfer_complete(): sequence %d: timeout error", c->sequence); } else { DEBUG_ERROR("transfer_complete(): sequence %d: transfer failed: " "status: 0x%x, urb-status: 0x%x", c->sequence, irp->IoStatus.Status, c->urb->UrbHeader.Status); } } ExFreePool(c->urb); ExFreePool(c); irp->IoStatus.Information = transmitted; remove_lock_release(dev); return STATUS_SUCCESS; }
static DWORD usbdk_translate_usbd_status(USBD_STATUS UsbdStatus) { if (USBD_SUCCESS(UsbdStatus)) return NO_ERROR; switch (UsbdStatus) { case USBD_STATUS_STALL_PID: case USBD_STATUS_ENDPOINT_HALTED: case USBD_STATUS_BAD_START_FRAME: return ERROR_GEN_FAILURE; case USBD_STATUS_TIMEOUT: return ERROR_SEM_TIMEOUT; case USBD_STATUS_CANCELED: return ERROR_OPERATION_ABORTED; default: return ERROR_FUNCTION_FAILED; } }
NTSTATUS get_interface(libusb_device_t *dev, int interface, unsigned char *altsetting, int *ret, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB urb; DEBUG_PRINT_NL(); DEBUG_MESSAGE("get_interface(): interface %d", interface); DEBUG_MESSAGE("get_interface(): timeout %d", timeout); if(!dev->config.value) { DEBUG_ERROR("get_interface(): invalid configuration 0"); return STATUS_INVALID_DEVICE_STATE; } memset(&urb, 0, sizeof(URB)); urb.UrbHeader.Function = URB_FUNCTION_GET_INTERFACE; urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_GET_INTERFACE_REQUEST); urb.UrbControlGetInterfaceRequest.TransferBufferLength = 1; urb.UrbControlGetInterfaceRequest.TransferBuffer = altsetting; urb.UrbControlGetInterfaceRequest.Interface = (USHORT)interface; status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if(!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { DEBUG_ERROR("get_interface(): getting interface " "failed: status: 0x%x, urb-status: 0x%x", status, urb.UrbHeader.Status); *ret = 0; } else { *ret = urb.UrbControlGetInterfaceRequest.TransferBufferLength; DEBUG_MESSAGE("get_interface(): current altsetting is %d", *altsetting); } return status; }
NTSTATUS get_descriptor(libusb_device_t *dev, void *buffer, int size, int type, int recipient, int index, int language_id, int *received, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB urb; USBMSG("buffer size: %d type: %04d recipient: %04d index: %04d language id: %04d timeout: %d\n", size, type, recipient, index, language_id, timeout); memset(&urb, 0, sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST)); switch (recipient) { case USB_RECIP_DEVICE: urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE; break; case USB_RECIP_INTERFACE: urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE; break; case USB_RECIP_ENDPOINT: urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT; break; default: USBERR0("invalid recipient\n"); return STATUS_INVALID_PARAMETER; } urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST); urb.UrbControlDescriptorRequest.TransferBufferLength = size; urb.UrbControlDescriptorRequest.TransferBuffer = buffer; urb.UrbControlDescriptorRequest.DescriptorType = (UCHAR)type; urb.UrbControlDescriptorRequest.Index = (UCHAR)index; urb.UrbControlDescriptorRequest.LanguageId = (USHORT)language_id; // the device and active config descriptors are cached. If the request // is for one of these and we have already retrieved it, then this is // a non-blocking non-i/o call. if (type == USB_DEVICE_DESCRIPTOR_TYPE && recipient == USB_RECIP_DEVICE && index == 0 && language_id == 0 && size >= sizeof(USB_DEVICE_DESCRIPTOR)) { // this is a device descriptor request. if (dev->device_descriptor.bLength == 0) { // this is the first request made for the device descriptor. // Cache it now and forever status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (NT_SUCCESS(status) && urb.UrbControlDescriptorRequest.TransferBufferLength < sizeof(USB_DEVICE_DESCRIPTOR)) { USBERR("Invalid device decriptor length %d\n", urb.UrbControlDescriptorRequest.TransferBufferLength); status = STATUS_BAD_DEVICE_TYPE; *received = 0; goto Done; } if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { USBERR("getting descriptor failed: status: 0x%x, urb-status: 0x%x\n", status, urb.UrbHeader.Status); *received = 0; goto Done; } // valid device descriptor size = sizeof(USB_DEVICE_DESCRIPTOR); RtlCopyMemory(&dev->device_descriptor, buffer, size); *received = size; } else { // device descriptor is already cached. size = sizeof(USB_DEVICE_DESCRIPTOR); RtlCopyMemory(buffer, &dev->device_descriptor, size); *received = size; } goto Done; } if (type == USB_CONFIGURATION_DESCRIPTOR_TYPE && recipient == USB_RECIP_DEVICE && language_id == 0 && size >= sizeof(USB_CONFIGURATION_DESCRIPTOR)) { if (dev->device_descriptor.bLength != 0 && index >=dev->device_descriptor.bNumConfigurations) { USBWRN("config descriptor index %d out of range.\n", index); status = STATUS_NO_MORE_ENTRIES; *received = 0; goto Done; } // this is a config descriptor request. if (!dev->config.descriptor || dev->config.index != index) { // this is either: // * The first request made for a config descriptor. // * A request for a config descriptor other than the cached index. status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { USBERR("getting descriptor failed: status: 0x%x, urb-status: 0x%x\n", status, urb.UrbHeader.Status); *received = 0; goto Done; } if (!dev->config.descriptor && urb.UrbControlDescriptorRequest.TransferBufferLength >= ((PUSB_CONFIGURATION_DESCRIPTOR)buffer)->wTotalLength) { // // This is a special case scenario where we cache the first // config deescriptor requested when dev->config.descriptor == NULL // and this was a request for the *entire* descriptor. // // Nearly all windows USB devices will only have one configuration. // In the case a the filter driver, it does *not* auto configure the // device which is where the *active* config descriptor caching occurs. // // This code ensures the first config descriptor requested is always cached // even if the device is not configured yet. // PUSB_CONFIGURATION_DESCRIPTOR config_desc; size = ((PUSB_CONFIGURATION_DESCRIPTOR)buffer)->wTotalLength; if (!( config_desc = ExAllocatePool(NonPagedPool, size))) { USBERR0("memory allocation error\n"); status = STATUS_NO_MEMORY; goto Done; } dev->config.descriptor=config_desc; RtlCopyMemory(dev->config.descriptor,buffer,size); dev->config.value=0; dev->config.total_size=size; dev->config.index=index; *received = size; } else { *received = urb.UrbControlDescriptorRequest.TransferBufferLength; } goto Done; } else { // This is a request for the active configuration descriptor. // This is only updated upon a successful set_configuration(). size = (size > dev->config.descriptor->wTotalLength) ? dev->config.descriptor->wTotalLength : size; RtlCopyMemory(buffer, dev->config.descriptor, size); *received = size; goto Done; } } // this is not a device or config descriptor reequest status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { USBERR("getting descriptor failed: status: 0x%x, urb-status: 0x%x\n", status, urb.UrbHeader.Status); *received = 0; } else { *received = urb.UrbControlDescriptorRequest.TransferBufferLength; } Done: return status; }
NTSTATUS set_configuration(libusb_device_t *dev, int configuration, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB urb, *urb_ptr = NULL; USB_CONFIGURATION_DESCRIPTOR *configuration_descriptor = NULL; USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL; USBD_INTERFACE_LIST_ENTRY *interfaces = NULL; int i, j, interface_number, desc_size, config_index, ret; // check if this config value is already set if ((configuration > 0) && dev->config.value == configuration) { return STATUS_SUCCESS; } // check if this config index is already set if ((configuration < 0) && dev->config.value && dev->config.index == (abs(configuration)-1)) { return STATUS_SUCCESS; } memset(&urb, 0, sizeof(URB)); if (configuration == 0) { urb.UrbHeader.Function = URB_FUNCTION_SELECT_CONFIGURATION; urb.UrbHeader.Length = sizeof(struct _URB_SELECT_CONFIGURATION); status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status)) { USBERR("setting configuration %d failed: status: 0x%x, urb-status: 0x%x\n", configuration, status, urb.UrbHeader.Status); return status; } dev->config.handle = urb.UrbSelectConfiguration.ConfigurationHandle; dev->config.value = 0; clear_pipe_info(dev); return status; } if (configuration <= SET_CONFIG_ACTIVE_CONFIG) { // note: as of v1.2.4.0, the active/default configuration is // always the first configuration the device returns. (index 0) configuration=-1; } USBMSG("setting configuration %s %d timeout=%d", (configuration < 0) ? "index" : "value", (configuration < 0) ? abs(configuration) - 1 : configuration, timeout); // If configuration is negative, it is retrieved by index. // configuration_descriptor = get_config_descriptor(dev, configuration, &desc_size, &config_index); if (!configuration_descriptor) { USBERR0("getting configuration descriptor failed"); return STATUS_INVALID_PARAMETER; } // if we passed an index in we can check here to see // if the device is already configured with this value if (dev->config.value == configuration_descriptor->bConfigurationValue) { UpdateContextConfigDescriptor( dev, configuration_descriptor, desc_size, configuration_descriptor->bConfigurationValue, config_index); status = STATUS_SUCCESS; goto SetConfigurationDone; } // MEMORY ALLOCATION BEGINS interfaces = ExAllocatePool(NonPagedPool,(configuration_descriptor->bNumInterfaces + 1) * sizeof(USBD_INTERFACE_LIST_ENTRY)); if (!interfaces) { USBERR0("memory allocation failed\n"); status = STATUS_NO_MEMORY; ExFreePool(configuration_descriptor); goto SetConfigurationDone; } memset(interfaces, 0, (configuration_descriptor->bNumInterfaces + 1) * sizeof(USBD_INTERFACE_LIST_ENTRY)); interface_number = 0; for (i = 0; i < configuration_descriptor->bNumInterfaces; i++) { for (j = interface_number; j < LIBUSB_MAX_NUMBER_OF_INTERFACES; j++) { interface_descriptor = find_interface_desc(configuration_descriptor, desc_size, j, 0); if (interface_descriptor) { interface_number = ++j; break; } } if (!interface_descriptor) { USBERR("unable to find interface descriptor at index %d\n", i); status = STATUS_INVALID_PARAMETER; ExFreePool(configuration_descriptor); goto SetConfigurationDone; } else { USBMSG("found interface %d\n", interface_descriptor->bInterfaceNumber); interfaces[i].InterfaceDescriptor = interface_descriptor; } } urb_ptr = USBD_CreateConfigurationRequestEx(configuration_descriptor, interfaces); if (!urb_ptr) { USBERR0("memory allocation failed\n"); status = STATUS_NO_MEMORY; ExFreePool(configuration_descriptor); goto SetConfigurationDone; } for (i = 0; i < configuration_descriptor->bNumInterfaces; i++) { for (j = 0; j < (int)interfaces[i].Interface->NumberOfPipes; j++) { interfaces[i].Interface->Pipes[j].MaximumTransferSize = LIBUSB_MAX_READ_WRITE; } } USBDBG("#%d %s passing configuration request to target-device.", dev->id, dev->device_id); status = call_usbd(dev, urb_ptr, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb_ptr->UrbHeader.Status)) { USBERR("setting configuration %d failed: status: 0x%x, urb-status: 0x%x\n", configuration, status, urb_ptr->UrbHeader.Status); if (NT_SUCCESS(status)) status = urb_ptr->UrbHeader.Status; ExFreePool(configuration_descriptor); goto SetConfigurationDone; } dev->config.handle = urb_ptr->UrbSelectConfiguration.ConfigurationHandle; clear_pipe_info(dev); for (i = 0; i < configuration_descriptor->bNumInterfaces; i++) { update_pipe_info(dev, interfaces[i].Interface); } UpdateContextConfigDescriptor(dev, configuration_descriptor, desc_size, configuration_descriptor->bConfigurationValue, config_index); SetConfigurationDone: if (interfaces) ExFreePool(interfaces); if (urb_ptr) ExFreePool(urb_ptr); return status; }
NTSTATUS set_interface(libusb_device_t *dev, int interface_number, int alt_interface_number, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB *urb; int i, tmp_size; USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL; USBD_INTERFACE_INFORMATION *interface_information = NULL; USBMSG("interface-number=%d alt-number=%d timeout=%d\n", interface_number,alt_interface_number,timeout); if (!dev->config.value || !dev->config.descriptor) { USBERR0("device is not configured\n"); return STATUS_INVALID_DEVICE_STATE; } interface_descriptor = find_interface_desc(dev->config.descriptor, dev->config.total_size, interface_number, alt_interface_number); if (!interface_descriptor) { USBERR("interface-number=%d alt-number=%d does not exists.\n", interface_number,alt_interface_number,timeout); return STATUS_NO_MORE_ENTRIES; } tmp_size = sizeof(struct _URB_SELECT_INTERFACE) + interface_descriptor->bNumEndpoints * sizeof(USBD_PIPE_INFORMATION); urb = ExAllocatePool(NonPagedPool, tmp_size); if (!urb) { USBERR0("memory_allocation error\n"); return STATUS_NO_MEMORY; } memset(urb, 0, tmp_size); urb->UrbHeader.Function = URB_FUNCTION_SELECT_INTERFACE; urb->UrbHeader.Length = (USHORT)tmp_size; urb->UrbSelectInterface.ConfigurationHandle = dev->config.handle; urb->UrbSelectInterface.Interface.Length = sizeof(struct _USBD_INTERFACE_INFORMATION); urb->UrbSelectInterface.Interface.NumberOfPipes = interface_descriptor->bNumEndpoints; urb->UrbSelectInterface.Interface.Length += interface_descriptor->bNumEndpoints * sizeof(struct _USBD_PIPE_INFORMATION); urb->UrbSelectInterface.Interface.InterfaceNumber = (UCHAR)interface_descriptor->bInterfaceNumber; urb->UrbSelectInterface.Interface.AlternateSetting = (UCHAR)interface_descriptor->bAlternateSetting; interface_information = &urb->UrbSelectInterface.Interface; for (i = 0; i < interface_descriptor->bNumEndpoints; i++) { interface_information->Pipes[i].MaximumTransferSize = LIBUSB_MAX_READ_WRITE; } status = call_usbd(dev, urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if (!NT_SUCCESS(status) || !USBD_SUCCESS(urb->UrbHeader.Status)) { USBERR("setting interface failed: status: 0x%x, urb-status: 0x%x\n", status, urb->UrbHeader.Status); ExFreePool(urb); return STATUS_UNSUCCESSFUL; } update_pipe_info(dev, interface_information); ExFreePool(urb); return status; }
// // handle descriptor and interface operations here // in order to provide enough of a usbport emulation // to convince the usb function drivers above us to // talk to us. In particular PipeHandles have to be // provided upwards that map to endpoint addresses used // by the backend DOM 0 driver. // NTSTATUS PostProcessUrb( IN PUSB_FDO_CONTEXT fdoContext, IN PURB Urb, IN NTSTATUS *usbdStatus, IN ULONG bytesTransferred, IN ULONG startFrame, IN PVOID isoPacketDescriptor) { NTSTATUS Status = USBD_SUCCESS(*usbdStatus) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL; if (*usbdStatus == USBD_STATUS_CANCELED) { // // special case this. // perhaps it would be better to select the NT status in the map backend to frontend // usb status conversion routine? // Status = STATUS_CANCELLED; } ULONG TraceLevel = NT_SUCCESS(Status) ? TRACE_LEVEL_VERBOSE : TRACE_LEVEL_INFORMATION; // --XT-- The stalled status does not seem like an error and fills the log with spew. if (*usbdStatus == USBD_STATUS_STALL_PID) TraceLevel = TRACE_LEVEL_VERBOSE; TraceEvents(TraceLevel, TRACE_URB, __FUNCTION__": %s Device %p Status %x UsbdStatus %x Function %s bytesTransferred %d\n", fdoContext->FrontEndPath, fdoContext->WdfDevice, Status, *usbdStatus, UrbFunctionToString(Urb->UrbHeader.Function), bytesTransferred); switch (Urb->UrbHeader.Function) { // // UsbBuildGetDescriptorRequest // case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE: { Urb->UrbControlDescriptorRequest.TransferBufferLength = bytesTransferred; if (NT_SUCCESS(Status)) { // // capture the descriptor data on return // PVOID buffer = Urb->UrbControlDescriptorRequest.TransferBuffer; // // just copy the descriptor, there can only be one. // if (!buffer) { // // ugh first get the systemva then do the copy. // buffer = MmGetSystemAddressForMdlSafe( Urb->UrbControlDescriptorRequest.TransferBufferMDL, NormalPagePriority); if (!buffer) { // // XXX STUB error handling here! // we preallocated the sysva for the mdl, // so this should never happen // break; } } switch (Urb->UrbControlDescriptorRequest.DescriptorType) { case USB_DEVICE_DESCRIPTOR_TYPE: memcpy(&fdoContext->DeviceDescriptor, buffer, sizeof(fdoContext->DeviceDescriptor)); break; case USB_CONFIGURATION_DESCRIPTOR_TYPE: if (bytesTransferred >= sizeof(USB_CONFIGURATION_DESCRIPTOR)) { TraceEvents(TRACE_LEVEL_VERBOSE, TRACE_DPC, __FUNCTION__": Config descriptor returned length %d\n", bytesTransferred); } break; case USB_STRING_DESCRIPTOR_TYPE: if (bytesTransferred >= sizeof(USB_STRING_DESCRIPTOR)) { PUSB_STRING_DESCRIPTOR stringDescriptor = (PUSB_STRING_DESCRIPTOR) buffer; // // stay away from wchar strings at raised IRQL. // TraceEvents(TRACE_LEVEL_VERBOSE, TRACE_DPC, __FUNCTION__": usb string: length %d type %d\n", stringDescriptor->bLength, stringDescriptor->bDescriptorType); } else { // // hmmmm... this ought to be an error? // TraceEvents(TRACE_LEVEL_WARNING, TRACE_DPC, __FUNCTION__": usb string: bytesTransferred(%d) < USB_STRING_DESCRIPTOR(%d)\n", bytesTransferred, sizeof(USB_STRING_DESCRIPTOR)); } break; case USB_INTERFACE_DESCRIPTOR_TYPE: case USB_ENDPOINT_DESCRIPTOR_TYPE: default: // // XXX unimplemented // break; } } } break; // UsbBuildSelectConfigurationRequest case URB_FUNCTION_SELECT_CONFIGURATION: // // Fill in the URB for the client // if (NT_SUCCESS(Status)) { Status = PostProcessSelectConfig(fdoContext, Urb); } if (!NT_SUCCESS(Status)) { if (fdoContext->ConfigBusy) { fdoContext->ConfigBusy = FALSE; } // // zero out the data? // RtlZeroMemory(Urb, Urb->UrbHeader.Length); } break; case URB_FUNCTION_SELECT_INTERFACE: // // capture the interface data on return and // provide a mapping from endpoint to pipehandle // ///////////////////////////////////////////////////////////////////////////////////// // // It is valid for the device to set a pipe to stalled during the // select interface operation when an error is believed to have // occurred. This is true when there is only one interface available. // // Comment from the backend USB code: // // 9.4.10 (of the USB spec) says devices don't need this and are free to STALL the // request if the interface only has one alternate setting. // // So to handle this situation, we set the status code to indicate success // when these conditons are met. // ///////////////////////////////////////////////////////////////////////////////////// if ((*usbdStatus == USBD_STATUS_STALL_PID) && (fdoContext->NumInterfaces == 1)) { TraceEvents(TRACE_LEVEL_WARNING, TRACE_DPC, __FUNCTION__": USB pipe stalled during select config...clearing failure status code"); *usbdStatus = USBD_STATUS_SUCCESS; Urb->UrbHeader.Status = *usbdStatus; Status = STATUS_SUCCESS; } if (NT_SUCCESS(Status)) { Status = PostProcessSelectInterface(fdoContext, Urb); } if (!NT_SUCCESS(Status)) { if (fdoContext->ConfigBusy) { fdoContext->ConfigBusy = FALSE; } // // zero out the data? // RtlZeroMemory(Urb, Urb->UrbHeader.Length); } break; case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER: case URB_FUNCTION_CONTROL_TRANSFER: case URB_FUNCTION_CONTROL_TRANSFER_EX: Urb->UrbControlTransfer.TransferBufferLength = bytesTransferred; break; case URB_FUNCTION_ISOCH_TRANSFER: if (isoPacketDescriptor) { Status = XenPostProcessIsoResponse(Urb, usbdStatus, bytesTransferred, startFrame, isoPacketDescriptor, Status); } else { TraceEvents(TRACE_LEVEL_WARNING, TRACE_DPC, __FUNCTION__": No isoPacketDescriptor for ISO Urb %p - failing request\n", Urb); Status = STATUS_UNSUCCESSFUL; } fdoContext->ScratchPad.FrameNumber = startFrame + Urb->UrbIsochronousTransfer.NumberOfPackets; break; case URB_FUNCTION_CLASS_DEVICE: case URB_FUNCTION_CLASS_INTERFACE: case URB_FUNCTION_CLASS_ENDPOINT: case URB_FUNCTION_VENDOR_DEVICE: case URB_FUNCTION_VENDOR_INTERFACE: case URB_FUNCTION_VENDOR_ENDPOINT: Urb->UrbControlVendorClassRequest.TransferBufferLength = bytesTransferred; break; case URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL: TraceEvents(TRACE_LEVEL_VERBOSE, TRACE_DPC, __FUNCTION__": Clear stall completed with usb status %x\n", *usbdStatus); break; case URB_FUNCTION_GET_STATUS_FROM_ENDPOINT: TraceEvents(TRACE_LEVEL_VERBOSE, TRACE_DPC, __FUNCTION__": Get status from endpoint %x usb status %x\n", Urb->UrbControlGetStatusRequest.Index, *usbdStatus); if (NT_SUCCESS(Status)) { PUSHORT endpointStatus = NULL; if (Urb->UrbControlGetStatusRequest.TransferBufferMDL) { endpointStatus = (PUSHORT) MmGetSystemAddressForMdlSafe( Urb->UrbControlGetStatusRequest.TransferBufferMDL, NormalPagePriority ); } else if (Urb->UrbControlGetStatusRequest.TransferBuffer) { endpointStatus = (PUSHORT) Urb->UrbControlGetStatusRequest.TransferBuffer; } if (endpointStatus) { TraceEvents(TRACE_LEVEL_VERBOSE, TRACE_DPC, __FUNCTION__": Endpoint status %x\n", *endpointStatus); } else { TraceEvents(TRACE_LEVEL_WARNING, TRACE_DPC, __FUNCTION__": Endpoint status buffer NULL!\n"); } } break; default: // // Nothing to do for anyone else? // break; } return Status; }
NTSTATUS set_interface(libusb_device_t *dev, int interface, int altsetting, int timeout) { NTSTATUS status = STATUS_SUCCESS; URB *urb; int i, config_size, tmp_size; USB_CONFIGURATION_DESCRIPTOR *configuration_descriptor = NULL; USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL; USBD_INTERFACE_INFORMATION *interface_information = NULL; DEBUG_PRINT_NL(); DEBUG_MESSAGE("set_interface(): interface %d", interface); DEBUG_MESSAGE("set_interface(): altsetting %d", altsetting); DEBUG_MESSAGE("set_interface(): timeout %d", timeout); if(!dev->config.value) { DEBUG_ERROR("release_interface(): device is not configured"); return STATUS_INVALID_DEVICE_STATE; } configuration_descriptor = get_config_descriptor(dev, dev->config.value, &config_size); if(!configuration_descriptor) { DEBUG_ERROR("set_interface(): memory_allocation error"); return STATUS_NO_MEMORY; } interface_descriptor = find_interface_desc(configuration_descriptor, config_size, interface, altsetting); if(!interface_descriptor) { DEBUG_ERROR("set_interface(): interface %d or altsetting %d invalid", interface, altsetting); ExFreePool(configuration_descriptor); return STATUS_UNSUCCESSFUL; } tmp_size = sizeof(struct _URB_SELECT_INTERFACE) + interface_descriptor->bNumEndpoints * sizeof(USBD_PIPE_INFORMATION); urb = ExAllocatePool(NonPagedPool, tmp_size); if(!urb) { DEBUG_ERROR("set_interface(): memory_allocation error"); ExFreePool(configuration_descriptor); return STATUS_NO_MEMORY; } memset(urb, 0, tmp_size); urb->UrbHeader.Function = URB_FUNCTION_SELECT_INTERFACE; urb->UrbHeader.Length = (USHORT)tmp_size; urb->UrbSelectInterface.ConfigurationHandle = dev->config.handle; urb->UrbSelectInterface.Interface.Length = sizeof(struct _USBD_INTERFACE_INFORMATION); urb->UrbSelectInterface.Interface.NumberOfPipes = interface_descriptor->bNumEndpoints; urb->UrbSelectInterface.Interface.Length += interface_descriptor->bNumEndpoints * sizeof(struct _USBD_PIPE_INFORMATION); urb->UrbSelectInterface.Interface.InterfaceNumber = (UCHAR)interface; urb->UrbSelectInterface.Interface.AlternateSetting = (UCHAR)altsetting; interface_information = &urb->UrbSelectInterface.Interface; for(i = 0; i < interface_descriptor->bNumEndpoints; i++) { interface_information->Pipes[i].MaximumTransferSize = LIBUSB_MAX_READ_WRITE; } status = call_usbd(dev, urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout); if(!NT_SUCCESS(status) || !USBD_SUCCESS(urb->UrbHeader.Status)) { DEBUG_ERROR("set_interface(): setting interface failed: status: 0x%x, " "urb-status: 0x%x", status, urb->UrbHeader.Status); ExFreePool(configuration_descriptor); ExFreePool(urb); return STATUS_UNSUCCESSFUL; } update_pipe_info(dev, interface_information); ExFreePool(configuration_descriptor); ExFreePool(urb); return status; }