1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/* libusb-win32, Generic Windows USB Library
* Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "libusb_driver.h"
#include <stdlib.h>
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;
}
|