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
#define __LIBUSB_DRIVER_C__
22
#include "libusb_driver.h"
24
extern int debug_level;
26
static void DDKAPI unload(DRIVER_OBJECT *driver_object);
28
static NTSTATUS DDKAPI on_usbd_complete(DEVICE_OBJECT *device_object,
32
NTSTATUS DDKAPI DriverEntry(DRIVER_OBJECT *driver_object,
33
UNICODE_STRING *registry_path)
37
DEBUG_MESSAGE("DriverEntry(): loading driver");
39
/* initialize global variables */
40
debug_level = LIBUSB_DEBUG_MSG;
42
/* initialize the driver object's dispatch table */
43
for(i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
45
driver_object->MajorFunction[i] = dispatch;
48
driver_object->DriverExtension->AddDevice = add_device;
49
driver_object->DriverUnload = unload;
51
return STATUS_SUCCESS;
54
NTSTATUS DDKAPI add_device(DRIVER_OBJECT *driver_object,
55
DEVICE_OBJECT *physical_device_object)
58
DEVICE_OBJECT *device_object = NULL;
62
UNICODE_STRING nt_device_name;
63
UNICODE_STRING symbolic_link_name;
64
WCHAR tmp_name_0[128];
65
WCHAR tmp_name_1[128];
69
/* get the hardware ID from the registry */
70
if(!reg_get_hardware_id(physical_device_object, id, sizeof(id)))
72
DEBUG_ERROR("add_device(): unable to read registry");
73
return STATUS_SUCCESS;
76
/* only attach the (filter) driver to USB devices, skip hubs */
77
/* and interfaces of composite devices */
78
if(!strstr(id, "usb\\") || strstr(id, "hub") || strstr(id, "&mi_"))
80
return STATUS_SUCCESS;
83
/* retrieve the type of the lower device object */
84
device_object = IoGetAttachedDeviceReference(physical_device_object);
88
device_type = device_object->DeviceType;
89
ObDereferenceObject(device_object);
93
device_type = FILE_DEVICE_UNKNOWN;
96
/* try to create a new device object */
97
for(i = 1; i < LIBUSB_MAX_NUMBER_OF_DEVICES; i++)
99
/* initialize some unicode strings */
100
_snwprintf(tmp_name_0, sizeof(tmp_name_0)/sizeof(WCHAR), L"%s%04d",
101
LIBUSB_NT_DEVICE_NAME, i);
102
_snwprintf(tmp_name_1, sizeof(tmp_name_1)/sizeof(WCHAR), L"%s%04d",
103
LIBUSB_SYMBOLIC_LINK_NAME, i);
105
RtlInitUnicodeString(&nt_device_name, tmp_name_0);
106
RtlInitUnicodeString(&symbolic_link_name, tmp_name_1);
108
/* create the object */
109
status = IoCreateDevice(driver_object,
110
sizeof(libusb_device_t),
111
&nt_device_name, device_type, 0, FALSE,
114
if(NT_SUCCESS(status))
116
DEBUG_MESSAGE("add_device(): device #%d created", i);
120
device_object = NULL;
122
/* continue until an unused device name is found */
127
DEBUG_ERROR("add_device(): creating device failed");
131
status = IoCreateSymbolicLink(&symbolic_link_name, &nt_device_name);
133
if(!NT_SUCCESS(status))
135
DEBUG_ERROR("add_device(): creating symbolic link failed");
136
IoDeleteDevice(device_object);
140
/* setup the "device object" */
141
dev = device_object->DeviceExtension;
143
memset(dev, 0, sizeof(libusb_device_t));
146
/* attach the newly created device object to the stack */
147
dev->next_stack_device =
148
IoAttachDeviceToDeviceStack(device_object, physical_device_object);
150
if(!dev->next_stack_device)
152
DEBUG_ERROR("add_device(): attaching to device stack failed");
153
IoDeleteSymbolicLink(&symbolic_link_name);
154
IoDeleteDevice(device_object);
155
return STATUS_NO_SUCH_DEVICE;
158
dev->self = device_object;
159
dev->physical_device_object = physical_device_object;
162
/* set initial power states */
163
dev->power_state.DeviceState = PowerDeviceD0;
164
dev->power_state.SystemState = PowerSystemWorking;
166
/* get device properties from the registry */
167
reg_get_properties(dev);
171
/* send all USB requests to the PDO in filter driver mode */
172
dev->target_device = dev->physical_device_object;
174
/* use the same flags as the underlying object */
175
device_object->Flags |= dev->next_stack_device->Flags
176
& (DO_BUFFERED_IO | DO_DIRECT_IO | DO_POWER_PAGABLE);
180
/* send all USB requests to the lower object in device driver mode */
181
dev->target_device = dev->next_stack_device;
183
device_object->Flags |= DO_DIRECT_IO | DO_POWER_PAGABLE;
186
clear_pipe_info(dev);
188
remove_lock_initialize(dev);
190
device_object->Flags &= ~DO_DEVICE_INITIALIZING;
196
VOID DDKAPI unload(DRIVER_OBJECT *driver_object)
198
DEBUG_MESSAGE("unload(): unloading driver");
201
NTSTATUS complete_irp(IRP *irp, NTSTATUS status, ULONG info)
203
irp->IoStatus.Status = status;
204
irp->IoStatus.Information = info;
205
IoCompleteRequest(irp, IO_NO_INCREMENT);
210
NTSTATUS call_usbd(libusb_device_t *dev, void *urb, ULONG control_code,
216
IO_STACK_LOCATION *next_irp_stack;
217
LARGE_INTEGER _timeout;
218
IO_STATUS_BLOCK io_status;
220
if(timeout > LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT)
222
timeout = LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT;
225
KeInitializeEvent(&event, NotificationEvent, FALSE);
227
irp = IoBuildDeviceIoControlRequest(control_code, dev->target_device,
228
NULL, 0, NULL, 0, TRUE,
233
return STATUS_NO_MEMORY;
236
next_irp_stack = IoGetNextIrpStackLocation(irp);
237
next_irp_stack->Parameters.Others.Argument1 = urb;
238
next_irp_stack->Parameters.Others.Argument2 = NULL;
240
IoSetCompletionRoutine(irp, on_usbd_complete, &event, TRUE, TRUE, TRUE);
242
status = IoCallDriver(dev->target_device, irp);
244
if(status == STATUS_PENDING)
246
_timeout.QuadPart = -(timeout * 10000);
248
if(KeWaitForSingleObject(&event, Executive, KernelMode,
249
FALSE, &_timeout) == STATUS_TIMEOUT)
251
DEBUG_ERROR("call_usbd(): request timed out");
256
/* wait until completion routine is called */
257
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
259
status = irp->IoStatus.Status;
261
/* complete the request */
262
IoCompleteRequest(irp, IO_NO_INCREMENT);
268
static NTSTATUS DDKAPI on_usbd_complete(DEVICE_OBJECT *device_object,
269
IRP *irp, void *context)
271
KeSetEvent((KEVENT *) context, IO_NO_INCREMENT, FALSE);
273
return STATUS_MORE_PROCESSING_REQUIRED;
277
NTSTATUS pass_irp_down(libusb_device_t *dev, IRP *irp,
278
PIO_COMPLETION_ROUTINE completion_routine,
281
if(completion_routine)
283
IoCopyCurrentIrpStackLocationToNext(irp);
284
IoSetCompletionRoutine(irp, completion_routine, context,
289
IoSkipCurrentIrpStackLocation(irp);
292
return IoCallDriver(dev->next_stack_device, irp);
295
bool_t accept_irp(libusb_device_t *dev, IRP *irp)
297
/* check if the IRP is sent to libusb's device object or to */
298
/* the lower one. This check is neccassary since the device object */
299
/* might be a filter */
300
if(irp->Tail.Overlay.OriginalFileObject)
302
return irp->Tail.Overlay.OriginalFileObject->DeviceObject
303
== dev->self ? TRUE : FALSE;
309
bool_t get_pipe_handle(libusb_device_t *dev, int endpoint_address,
310
USBD_PIPE_HANDLE *pipe_handle)
316
for(i = 0; i < LIBUSB_MAX_NUMBER_OF_INTERFACES; i++)
318
if(dev->config.interfaces[i].valid)
320
for(j = 0; j < LIBUSB_MAX_NUMBER_OF_ENDPOINTS; j++)
322
if(dev->config.interfaces[i].endpoints[j].address
325
*pipe_handle = dev->config.interfaces[i].endpoints[j].handle;
327
return !*pipe_handle ? FALSE : TRUE;
336
void clear_pipe_info(libusb_device_t *dev)
338
memset(dev->config.interfaces, 0 , sizeof(dev->config.interfaces));
341
bool_t update_pipe_info(libusb_device_t *dev,
342
USBD_INTERFACE_INFORMATION *interface_info)
352
number = interface_info->InterfaceNumber;
354
if(interface_info->InterfaceNumber >= LIBUSB_MAX_NUMBER_OF_INTERFACES)
359
DEBUG_MESSAGE("update_pipe_info(): interface %d", number);
361
dev->config.interfaces[number].valid = TRUE;
363
for(i = 0; i < LIBUSB_MAX_NUMBER_OF_ENDPOINTS; i++)
365
dev->config.interfaces[number].endpoints[i].address = 0;
366
dev->config.interfaces[number].endpoints[i].handle = NULL;
371
for(i = 0; i < (int)interface_info->NumberOfPipes
372
&& i < LIBUSB_MAX_NUMBER_OF_ENDPOINTS; i++)
374
DEBUG_MESSAGE("update_pipe_info(): endpoint address 0x%02x",
375
interface_info->Pipes[i].EndpointAddress);
377
dev->config.interfaces[number].endpoints[i].handle
378
= interface_info->Pipes[i].PipeHandle;
379
dev->config.interfaces[number].endpoints[i].address =
380
interface_info->Pipes[i].EndpointAddress;
388
void remove_lock_initialize(libusb_device_t *dev)
390
KeInitializeEvent(&dev->remove_lock.event, NotificationEvent, FALSE);
391
dev->remove_lock.usage_count = 1;
392
dev->remove_lock.remove_pending = FALSE;
396
NTSTATUS remove_lock_acquire(libusb_device_t *dev)
398
InterlockedIncrement(&dev->remove_lock.usage_count);
400
if(dev->remove_lock.remove_pending)
402
if(InterlockedDecrement(&dev->remove_lock.usage_count) == 0)
404
KeSetEvent(&dev->remove_lock.event, 0, FALSE);
406
return STATUS_DELETE_PENDING;
408
return STATUS_SUCCESS;
412
void remove_lock_release(libusb_device_t *dev)
414
if(InterlockedDecrement(&dev->remove_lock.usage_count) == 0)
416
KeSetEvent(&dev->remove_lock.event, 0, FALSE);
421
void remove_lock_release_and_wait(libusb_device_t *dev)
423
dev->remove_lock.remove_pending = TRUE;
424
remove_lock_release(dev);
425
remove_lock_release(dev);
426
KeWaitForSingleObject(&dev->remove_lock.event, Executive, KernelMode,
431
USB_INTERFACE_DESCRIPTOR *
432
find_interface_desc(USB_CONFIGURATION_DESCRIPTOR *config_desc,
433
unsigned int size, int interface_number, int altsetting)
435
usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc;
436
char *p = (char *)desc;
437
USB_INTERFACE_DESCRIPTOR *if_desc = NULL;
439
if(!config_desc || (size < config_desc->wTotalLength))
442
while(size && desc->length <= size)
444
if(desc->type == USB_INTERFACE_DESCRIPTOR_TYPE)
446
if_desc = (USB_INTERFACE_DESCRIPTOR *)desc;
448
if((if_desc->bInterfaceNumber == (UCHAR)interface_number)
449
&& (if_desc->bAlternateSetting == (UCHAR)altsetting))
455
size -= desc->length;
457
desc = (usb_descriptor_header_t *)p;