Exemple #1
0
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;
}
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, 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;
}