1
/* LIBUSB-WIN32, Generic Windows USB Library
2
* Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include "libusb_driver.h"
24
NTSTATUS get_descriptor(libusb_device_t *dev,
25
void *buffer, int size, int type, int recipient,
26
int index, int language_id, int *received, int timeout)
28
NTSTATUS status = STATUS_SUCCESS;
32
DEBUG_MESSAGE("get_descriptor(): buffer size %d", size);
33
DEBUG_MESSAGE("get_descriptor(): type %04d", type);
34
DEBUG_MESSAGE("get_descriptor(): recipient %04d", recipient);
35
DEBUG_MESSAGE("get_descriptor(): index %04d", index);
36
DEBUG_MESSAGE("get_descriptor(): language id %04d", language_id);
37
DEBUG_MESSAGE("get_descriptor(): timeout %d", timeout);
40
memset(&urb, 0, sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
44
case USB_RECIP_DEVICE:
45
urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE;
47
case USB_RECIP_INTERFACE:
48
urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE;
50
case USB_RECIP_ENDPOINT:
51
urb.UrbHeader.Function = URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT;
54
DEBUG_ERROR("get_descriptor(): invalid recipient");
55
return STATUS_INVALID_PARAMETER;
59
urb.UrbHeader.Length = sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
60
urb.UrbControlDescriptorRequest.TransferBufferLength = size;
61
urb.UrbControlDescriptorRequest.TransferBuffer = buffer;
62
urb.UrbControlDescriptorRequest.DescriptorType = (UCHAR)type;
63
urb.UrbControlDescriptorRequest.Index = (UCHAR)index;
64
urb.UrbControlDescriptorRequest.LanguageId = (USHORT)language_id;
67
status = call_usbd(dev, &urb, IOCTL_INTERNAL_USB_SUBMIT_URB, timeout);
70
if(!NT_SUCCESS(status) || !USBD_SUCCESS(urb.UrbHeader.Status))
72
DEBUG_ERROR("get_descriptor(): getting descriptor "
73
"failed: status: 0x%x, urb-status: 0x%x",
74
status, urb.UrbHeader.Status);
79
*received = urb.UrbControlDescriptorRequest.TransferBufferLength;
85
USB_CONFIGURATION_DESCRIPTOR *
86
get_config_descriptor(libusb_device_t *dev, int value, int *size)
89
USB_CONFIGURATION_DESCRIPTOR *desc = NULL;
90
USB_DEVICE_DESCRIPTOR device_descriptor;
92
volatile int desc_size;
94
status = get_descriptor(dev, &device_descriptor,
95
sizeof(USB_DEVICE_DESCRIPTOR),
96
USB_DEVICE_DESCRIPTOR_TYPE,
98
0, 0, size, LIBUSB_DEFAULT_TIMEOUT);
100
if(!NT_SUCCESS(status) || *size != sizeof(USB_DEVICE_DESCRIPTOR))
102
DEBUG_ERROR("get_config_descriptor(): getting device descriptor failed");
106
if(!(desc = ExAllocatePool(NonPagedPool,
107
sizeof(USB_CONFIGURATION_DESCRIPTOR))))
109
DEBUG_ERROR("get_config_descriptor(): memory allocation error");
113
for(i = 0; i < device_descriptor.bNumConfigurations; i++)
116
if(!NT_SUCCESS(get_descriptor(dev, desc,
117
sizeof(USB_CONFIGURATION_DESCRIPTOR),
118
USB_CONFIGURATION_DESCRIPTOR_TYPE,
120
i, 0, size, LIBUSB_DEFAULT_TIMEOUT)))
122
DEBUG_ERROR("get_config_descriptor(): getting configuration "
123
"descriptor failed");
127
if(desc->bConfigurationValue == value)
129
desc_size = desc->wTotalLength;
132
if(!(desc = ExAllocatePool(NonPagedPool, desc_size)))
134
DEBUG_ERROR("get_config_descriptor(): memory allocation error");
138
if(!NT_SUCCESS(get_descriptor(dev, desc, desc_size,
139
USB_CONFIGURATION_DESCRIPTOR_TYPE,
141
i, 0, size, LIBUSB_DEFAULT_TIMEOUT)))
143
DEBUG_ERROR("get_config_descriptor(): getting configuration "
144
"descriptor failed");