2020-04-15 14:43:18 +00:00
|
|
|
/*
|
|
|
|
* USB root device enumerator using libusb
|
|
|
|
*
|
|
|
|
* Copyright 2020 Zebediah Figura
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2022-05-11 23:43:23 +00:00
|
|
|
#include <stdbool.h>
|
2022-05-25 23:08:54 +00:00
|
|
|
#include <stdint.h>
|
2020-04-15 14:43:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winioctl.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "ddk/wdm.h"
|
|
|
|
#include "ddk/usb.h"
|
|
|
|
#include "ddk/usbioctl.h"
|
|
|
|
#include "wine/asm.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/list.h"
|
|
|
|
|
2022-05-25 20:41:19 +00:00
|
|
|
#include "unixlib.h"
|
|
|
|
|
2020-04-15 14:43:18 +00:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wineusb);
|
|
|
|
|
2021-10-13 19:13:27 +00:00
|
|
|
#ifdef __ASM_USE_FASTCALL_WRAPPER
|
2020-04-15 14:43:23 +00:00
|
|
|
|
|
|
|
extern void * WINAPI wrap_fastcall_func1( void *func, const void *a );
|
|
|
|
__ASM_STDCALL_FUNC( wrap_fastcall_func1, 8,
|
|
|
|
"popl %ecx\n\t"
|
|
|
|
"popl %eax\n\t"
|
|
|
|
"xchgl (%esp),%ecx\n\t"
|
|
|
|
"jmp *%eax" );
|
|
|
|
|
|
|
|
#define call_fastcall_func1(func,a) wrap_fastcall_func1(func,a)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define call_fastcall_func1(func,a) func(a)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
#define DECLARE_CRITICAL_SECTION(cs) \
|
|
|
|
static CRITICAL_SECTION cs; \
|
|
|
|
static CRITICAL_SECTION_DEBUG cs##_debug = \
|
|
|
|
{ 0, 0, &cs, { &cs##_debug.ProcessLocksList, &cs##_debug.ProcessLocksList }, \
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": " # cs) }}; \
|
|
|
|
static CRITICAL_SECTION cs = { &cs##_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
DECLARE_CRITICAL_SECTION(wineusb_cs);
|
|
|
|
|
|
|
|
static struct list device_list = LIST_INIT(device_list);
|
|
|
|
|
|
|
|
struct usb_device
|
|
|
|
{
|
|
|
|
struct list entry;
|
2020-04-24 16:44:01 +00:00
|
|
|
BOOL removed;
|
2020-04-15 14:43:22 +00:00
|
|
|
|
|
|
|
DEVICE_OBJECT *device_obj;
|
|
|
|
|
2022-05-25 23:03:11 +00:00
|
|
|
bool interface;
|
2022-05-12 00:01:50 +00:00
|
|
|
uint8_t interface_index;
|
|
|
|
|
|
|
|
uint8_t class, subclass, protocol;
|
|
|
|
|
2022-05-11 23:10:48 +00:00
|
|
|
uint16_t vendor, product, revision;
|
|
|
|
|
2022-05-24 23:31:27 +00:00
|
|
|
struct unix_device *unix_device;
|
2020-04-17 23:03:04 +00:00
|
|
|
|
|
|
|
LIST_ENTRY irp_list;
|
2020-04-15 14:43:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static DRIVER_OBJECT *driver_obj;
|
2020-04-15 14:43:19 +00:00
|
|
|
static DEVICE_OBJECT *bus_fdo, *bus_pdo;
|
|
|
|
|
2022-07-06 04:53:45 +00:00
|
|
|
static void destroy_unix_device(struct unix_device *unix_device)
|
2022-05-11 23:43:23 +00:00
|
|
|
{
|
2022-07-06 04:53:45 +00:00
|
|
|
struct usb_destroy_device_params params =
|
2022-05-24 23:39:08 +00:00
|
|
|
{
|
2022-07-06 04:53:45 +00:00
|
|
|
.device = unix_device,
|
|
|
|
};
|
2022-05-11 23:43:23 +00:00
|
|
|
|
2022-11-24 09:34:28 +00:00
|
|
|
WINE_UNIX_CALL(unix_usb_destroy_device, ¶ms);
|
2022-05-24 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 22:37:37 +00:00
|
|
|
static void add_unix_device(const struct usb_add_device_event *event)
|
2020-04-15 14:43:22 +00:00
|
|
|
{
|
|
|
|
static unsigned int name_index;
|
|
|
|
struct usb_device *device;
|
|
|
|
DEVICE_OBJECT *device_obj;
|
|
|
|
UNICODE_STRING string;
|
|
|
|
NTSTATUS status;
|
2021-08-22 16:19:35 +00:00
|
|
|
WCHAR name[26];
|
2020-04-15 14:43:22 +00:00
|
|
|
|
2022-05-25 22:37:37 +00:00
|
|
|
TRACE("Adding new device %p, vendor %04x, product %04x.\n", event->device,
|
|
|
|
event->vendor, event->product);
|
2020-04-15 14:43:22 +00:00
|
|
|
|
2022-05-25 23:08:54 +00:00
|
|
|
swprintf(name, ARRAY_SIZE(name), L"\\Device\\USBPDO-%u", name_index++);
|
2020-04-15 14:43:22 +00:00
|
|
|
RtlInitUnicodeString(&string, name);
|
|
|
|
if ((status = IoCreateDevice(driver_obj, sizeof(*device), &string,
|
|
|
|
FILE_DEVICE_USB, 0, FALSE, &device_obj)))
|
|
|
|
{
|
2022-11-17 10:17:42 +00:00
|
|
|
ERR("Failed to create device, status %#lx.\n", status);
|
2020-04-15 14:43:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = device_obj->DeviceExtension;
|
|
|
|
device->device_obj = device_obj;
|
2022-05-25 22:37:37 +00:00
|
|
|
device->unix_device = event->device;
|
2020-04-17 23:03:04 +00:00
|
|
|
InitializeListHead(&device->irp_list);
|
2020-04-24 16:44:01 +00:00
|
|
|
device->removed = FALSE;
|
2020-04-15 14:43:22 +00:00
|
|
|
|
2022-05-25 22:37:37 +00:00
|
|
|
device->class = event->class;
|
|
|
|
device->subclass = event->subclass;
|
|
|
|
device->protocol = event->protocol;
|
|
|
|
device->vendor = event->vendor;
|
|
|
|
device->product = event->product;
|
|
|
|
device->revision = event->revision;
|
2022-05-12 00:01:50 +00:00
|
|
|
|
2022-07-09 23:13:37 +00:00
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
list_add_tail(&device_list, &device->entry);
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
IoInvalidateDeviceRelations(bus_pdo, BusRelations);
|
|
|
|
}
|
|
|
|
|
2022-05-24 23:31:27 +00:00
|
|
|
static void remove_unix_device(struct unix_device *unix_device)
|
2020-04-15 14:43:22 +00:00
|
|
|
{
|
|
|
|
struct usb_device *device;
|
|
|
|
|
2022-05-24 23:31:27 +00:00
|
|
|
TRACE("Removing device %p.\n", unix_device);
|
2020-04-15 14:43:22 +00:00
|
|
|
|
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
LIST_FOR_EACH_ENTRY(device, &device_list, struct usb_device, entry)
|
|
|
|
{
|
2022-05-24 23:31:27 +00:00
|
|
|
if (device->unix_device == unix_device)
|
2020-04-15 14:43:22 +00:00
|
|
|
{
|
2021-08-21 00:01:27 +00:00
|
|
|
if (!device->removed)
|
|
|
|
{
|
|
|
|
device->removed = TRUE;
|
|
|
|
list_remove(&device->entry);
|
|
|
|
}
|
2020-04-15 14:43:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
2020-04-24 16:44:01 +00:00
|
|
|
|
|
|
|
IoInvalidateDeviceRelations(bus_pdo, BusRelations);
|
2020-04-15 14:43:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 08:47:37 +00:00
|
|
|
static HANDLE event_thread;
|
2020-04-15 14:43:21 +00:00
|
|
|
|
2022-05-25 21:15:01 +00:00
|
|
|
static void complete_irp(IRP *irp)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
RemoveEntryList(&irp->Tail.Overlay.ListEntry);
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
}
|
|
|
|
|
2022-05-11 23:43:23 +00:00
|
|
|
static DWORD CALLBACK event_thread_proc(void *arg)
|
|
|
|
{
|
|
|
|
struct usb_event event;
|
2022-09-04 08:47:37 +00:00
|
|
|
struct usb_main_loop_params params =
|
2022-05-11 23:43:23 +00:00
|
|
|
{
|
2022-09-04 08:47:37 +00:00
|
|
|
.event = &event,
|
|
|
|
};
|
2022-07-06 04:53:45 +00:00
|
|
|
|
2022-09-04 08:47:37 +00:00
|
|
|
TRACE("Starting event thread.\n");
|
2022-05-11 23:43:23 +00:00
|
|
|
|
2022-09-04 08:47:37 +00:00
|
|
|
if (WINE_UNIX_CALL(unix_usb_init, NULL) != STATUS_SUCCESS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (WINE_UNIX_CALL(unix_usb_main_loop, ¶ms) == STATUS_PENDING)
|
|
|
|
{
|
2022-05-11 23:43:23 +00:00
|
|
|
switch (event.type)
|
|
|
|
{
|
2022-05-24 23:46:22 +00:00
|
|
|
case USB_EVENT_ADD_DEVICE:
|
2022-05-25 22:37:37 +00:00
|
|
|
add_unix_device(&event.u.added_device);
|
2022-05-24 23:46:22 +00:00
|
|
|
break;
|
|
|
|
|
2022-05-24 23:39:08 +00:00
|
|
|
case USB_EVENT_REMOVE_DEVICE:
|
|
|
|
remove_unix_device(event.u.removed_device);
|
|
|
|
break;
|
|
|
|
|
2022-05-25 21:15:01 +00:00
|
|
|
case USB_EVENT_TRANSFER_COMPLETE:
|
|
|
|
complete_irp(event.u.completed_irp);
|
|
|
|
break;
|
2022-05-11 23:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-04 08:47:37 +00:00
|
|
|
|
|
|
|
TRACE("Shutting down event thread.\n");
|
|
|
|
return 0;
|
2022-05-11 23:43:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:20 +00:00
|
|
|
static NTSTATUS fdo_pnp(IRP *irp)
|
|
|
|
{
|
|
|
|
IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation(irp);
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
TRACE("irp %p, minor function %#x.\n", irp, stack->MinorFunction);
|
|
|
|
|
|
|
|
switch (stack->MinorFunction)
|
|
|
|
{
|
2020-04-15 14:43:23 +00:00
|
|
|
case IRP_MN_QUERY_DEVICE_RELATIONS:
|
|
|
|
{
|
|
|
|
struct usb_device *device;
|
|
|
|
DEVICE_RELATIONS *devices;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
if (stack->Parameters.QueryDeviceRelations.Type != BusRelations)
|
|
|
|
{
|
|
|
|
FIXME("Unhandled device relations type %#x.\n", stack->Parameters.QueryDeviceRelations.Type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
if (!(devices = ExAllocatePool(PagedPool,
|
|
|
|
offsetof(DEVICE_RELATIONS, Objects[list_count(&device_list)]))))
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
irp->IoStatus.Status = STATUS_NO_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(device, &device_list, struct usb_device, entry)
|
|
|
|
{
|
|
|
|
devices->Objects[i++] = device->device_obj;
|
|
|
|
call_fastcall_func1(ObfReferenceObject, device->device_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
devices->Count = i;
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)devices;
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:20 +00:00
|
|
|
case IRP_MN_START_DEVICE:
|
2022-07-06 04:28:38 +00:00
|
|
|
event_thread = CreateThread(NULL, 0, event_thread_proc, NULL, 0, NULL);
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
2020-04-15 14:43:20 +00:00
|
|
|
case IRP_MN_SURPRISE_REMOVAL:
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
2020-04-15 14:43:22 +00:00
|
|
|
{
|
|
|
|
struct usb_device *device, *cursor;
|
|
|
|
|
2022-11-24 09:34:28 +00:00
|
|
|
WINE_UNIX_CALL(unix_usb_exit, NULL);
|
2020-04-15 14:43:21 +00:00
|
|
|
WaitForSingleObject(event_thread, INFINITE);
|
|
|
|
CloseHandle(event_thread);
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
EnterCriticalSection(&wineusb_cs);
|
2021-08-21 00:01:27 +00:00
|
|
|
/* Normally we unlink all devices either:
|
|
|
|
*
|
|
|
|
* - as a result of hot-unplug, which unlinks the device, and causes
|
|
|
|
* a subsequent IRP_MN_REMOVE_DEVICE which will free it;
|
|
|
|
*
|
|
|
|
* - if the parent is deleted (at shutdown time), in which case
|
|
|
|
* ntoskrnl will send us IRP_MN_SURPRISE_REMOVAL and
|
|
|
|
* IRP_MN_REMOVE_DEVICE unprompted.
|
|
|
|
*
|
|
|
|
* But we can get devices hotplugged between when shutdown starts
|
|
|
|
* and now, in which case they'll be stuck in this list and never
|
|
|
|
* freed.
|
|
|
|
*
|
|
|
|
* FIXME: This is still broken, though. If a device is hotplugged
|
|
|
|
* and then removed, it'll be unlinked and never freed. */
|
2020-04-15 14:43:22 +00:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(device, cursor, &device_list, struct usb_device, entry)
|
|
|
|
{
|
2021-08-21 00:01:27 +00:00
|
|
|
assert(!device->removed);
|
2022-05-25 23:03:11 +00:00
|
|
|
destroy_unix_device(device->unix_device);
|
2020-04-15 14:43:22 +00:00
|
|
|
list_remove(&device->entry);
|
|
|
|
IoDeleteDevice(device->device_obj);
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
2020-04-15 14:43:20 +00:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
IoSkipCurrentIrpStackLocation(irp);
|
|
|
|
ret = IoCallDriver(bus_pdo, irp);
|
|
|
|
IoDetachDevice(bus_pdo);
|
|
|
|
IoDeleteDevice(bus_fdo);
|
|
|
|
return ret;
|
2020-04-15 14:43:22 +00:00
|
|
|
}
|
2020-04-15 14:43:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Unhandled minor function %#x.\n", stack->MinorFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
IoSkipCurrentIrpStackLocation(irp);
|
|
|
|
return IoCallDriver(bus_pdo, irp);
|
|
|
|
}
|
|
|
|
|
2022-05-12 00:01:48 +00:00
|
|
|
struct string_buffer
|
|
|
|
{
|
|
|
|
WCHAR *string;
|
|
|
|
size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void WINAPIV append_id(struct string_buffer *buffer, const WCHAR *format, ...)
|
|
|
|
{
|
|
|
|
__ms_va_list args;
|
|
|
|
WCHAR *string;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
__ms_va_start(args, format);
|
|
|
|
|
|
|
|
len = _vsnwprintf(NULL, 0, format, args) + 1;
|
|
|
|
if (!(string = ExAllocatePool(PagedPool, (buffer->len + len) * sizeof(WCHAR))))
|
|
|
|
{
|
|
|
|
if (buffer->string)
|
|
|
|
ExFreePool(buffer->string);
|
|
|
|
buffer->string = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (buffer->string)
|
|
|
|
{
|
|
|
|
memcpy(string, buffer->string, buffer->len * sizeof(WCHAR));
|
|
|
|
ExFreePool(buffer->string);
|
|
|
|
}
|
|
|
|
_vsnwprintf(string + buffer->len, len, format, args);
|
|
|
|
buffer->string = string;
|
|
|
|
buffer->len += len;
|
|
|
|
|
|
|
|
__ms_va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_device_id(const struct usb_device *device, struct string_buffer *buffer)
|
2020-04-17 00:58:15 +00:00
|
|
|
{
|
2022-05-25 23:03:11 +00:00
|
|
|
if (device->interface)
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"USB\\VID_%04X&PID_%04X&MI_%02X",
|
|
|
|
device->vendor, device->product, device->interface_index);
|
2022-05-12 00:01:50 +00:00
|
|
|
else
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"USB\\VID_%04X&PID_%04X", device->vendor, device->product);
|
2020-04-17 00:58:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 00:01:48 +00:00
|
|
|
static void get_hardware_ids(const struct usb_device *device, struct string_buffer *buffer)
|
2020-04-17 00:58:16 +00:00
|
|
|
{
|
2022-05-25 23:03:11 +00:00
|
|
|
if (device->interface)
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"USB\\VID_%04X&PID_%04X&REV_%04X&MI_%02X",
|
|
|
|
device->vendor, device->product, device->revision, device->interface_index);
|
2022-05-12 00:01:50 +00:00
|
|
|
else
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"USB\\VID_%04X&PID_%04X&REV_%04X",
|
|
|
|
device->vendor, device->product, device->revision);
|
|
|
|
|
2020-04-17 00:58:16 +00:00
|
|
|
get_device_id(device, buffer);
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"");
|
2020-04-17 00:58:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 00:01:48 +00:00
|
|
|
static void get_compatible_ids(const struct usb_device *device, struct string_buffer *buffer)
|
2020-04-17 00:58:17 +00:00
|
|
|
{
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(buffer, L"USB\\Class_%02x&SubClass_%02x&Prot_%02x",
|
|
|
|
device->class, device->subclass, device->protocol);
|
|
|
|
append_id(buffer, L"USB\\Class_%02x&SubClass_%02x", device->class, device->subclass);
|
|
|
|
append_id(buffer, L"USB\\Class_%02x", device->class);
|
|
|
|
append_id(buffer, L"");
|
2020-04-17 00:58:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 00:01:50 +00:00
|
|
|
static NTSTATUS query_id(struct usb_device *device, IRP *irp, BUS_QUERY_ID_TYPE type)
|
2020-04-17 00:58:15 +00:00
|
|
|
{
|
2022-05-12 00:01:48 +00:00
|
|
|
struct string_buffer buffer = {0};
|
2020-04-17 00:58:15 +00:00
|
|
|
|
2022-05-12 00:01:49 +00:00
|
|
|
TRACE("type %#x.\n", type);
|
|
|
|
|
2020-04-17 00:58:15 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case BusQueryDeviceID:
|
2022-05-12 00:01:48 +00:00
|
|
|
get_device_id(device, &buffer);
|
2020-04-17 00:58:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BusQueryInstanceID:
|
2022-05-25 23:08:54 +00:00
|
|
|
append_id(&buffer, L"0");
|
2020-04-17 00:58:15 +00:00
|
|
|
break;
|
|
|
|
|
2020-04-17 00:58:16 +00:00
|
|
|
case BusQueryHardwareIDs:
|
2022-05-12 00:01:48 +00:00
|
|
|
get_hardware_ids(device, &buffer);
|
2020-04-17 00:58:16 +00:00
|
|
|
break;
|
|
|
|
|
2020-04-17 00:58:17 +00:00
|
|
|
case BusQueryCompatibleIDs:
|
2022-05-12 00:01:48 +00:00
|
|
|
get_compatible_ids(device, &buffer);
|
2020-04-17 00:58:17 +00:00
|
|
|
break;
|
|
|
|
|
2020-04-17 00:58:15 +00:00
|
|
|
default:
|
|
|
|
FIXME("Unhandled ID query type %#x.\n", type);
|
|
|
|
return irp->IoStatus.Status;
|
|
|
|
}
|
|
|
|
|
2022-05-12 00:01:48 +00:00
|
|
|
if (!buffer.string)
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)buffer.string;
|
2020-04-17 00:58:15 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-07-01 07:51:22 +00:00
|
|
|
static void remove_pending_irps(struct usb_device *device)
|
|
|
|
{
|
|
|
|
LIST_ENTRY *entry;
|
|
|
|
IRP *irp;
|
|
|
|
|
|
|
|
while ((entry = RemoveHeadList(&device->irp_list)) != &device->irp_list)
|
|
|
|
{
|
|
|
|
irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
static NTSTATUS pdo_pnp(DEVICE_OBJECT *device_obj, IRP *irp)
|
|
|
|
{
|
|
|
|
IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation(irp);
|
2020-04-17 00:58:15 +00:00
|
|
|
struct usb_device *device = device_obj->DeviceExtension;
|
2020-04-15 14:43:22 +00:00
|
|
|
NTSTATUS ret = irp->IoStatus.Status;
|
|
|
|
|
|
|
|
TRACE("device_obj %p, irp %p, minor function %#x.\n", device_obj, irp, stack->MinorFunction);
|
|
|
|
|
|
|
|
switch (stack->MinorFunction)
|
|
|
|
{
|
2020-04-17 00:58:15 +00:00
|
|
|
case IRP_MN_QUERY_ID:
|
|
|
|
ret = query_id(device, irp, stack->Parameters.QueryId.IdType);
|
|
|
|
break;
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
case IRP_MN_QUERY_CAPABILITIES:
|
2021-04-11 02:57:16 +00:00
|
|
|
{
|
|
|
|
DEVICE_CAPABILITIES *caps = stack->Parameters.DeviceCapabilities.Capabilities;
|
|
|
|
|
|
|
|
caps->RawDeviceOK = 1;
|
|
|
|
|
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IRP_MN_START_DEVICE:
|
2020-04-24 16:44:01 +00:00
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
2021-07-01 07:51:23 +00:00
|
|
|
case IRP_MN_SURPRISE_REMOVAL:
|
2020-04-24 16:44:01 +00:00
|
|
|
EnterCriticalSection(&wineusb_cs);
|
2021-07-01 07:51:22 +00:00
|
|
|
remove_pending_irps(device);
|
2021-08-21 00:01:27 +00:00
|
|
|
if (!device->removed)
|
|
|
|
{
|
|
|
|
device->removed = TRUE;
|
|
|
|
list_remove(&device->entry);
|
|
|
|
}
|
2020-04-24 16:44:01 +00:00
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
2021-07-01 07:51:23 +00:00
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
break;
|
2020-04-24 16:44:01 +00:00
|
|
|
|
2021-07-01 07:51:23 +00:00
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
2021-08-21 00:01:27 +00:00
|
|
|
assert(device->removed);
|
2021-07-01 07:51:23 +00:00
|
|
|
remove_pending_irps(device);
|
2020-04-24 16:44:01 +00:00
|
|
|
|
2022-05-25 23:03:11 +00:00
|
|
|
destroy_unix_device(device->unix_device);
|
2020-04-24 16:44:01 +00:00
|
|
|
|
2021-07-01 07:51:23 +00:00
|
|
|
IoDeleteDevice(device->device_obj);
|
2020-04-15 14:43:22 +00:00
|
|
|
ret = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Unhandled minor function %#x.\n", stack->MinorFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
irp->IoStatus.Status = ret;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:20 +00:00
|
|
|
static NTSTATUS WINAPI driver_pnp(DEVICE_OBJECT *device, IRP *irp)
|
|
|
|
{
|
2020-04-15 14:43:22 +00:00
|
|
|
if (device == bus_fdo)
|
|
|
|
return fdo_pnp(irp);
|
|
|
|
return pdo_pnp(device, irp);
|
2020-04-15 14:43:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 22:00:02 +00:00
|
|
|
static NTSTATUS usb_submit_urb(struct usb_device *device, IRP *irp)
|
|
|
|
{
|
|
|
|
URB *urb = IoGetCurrentIrpStackLocation(irp)->Parameters.Others.Argument1;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
TRACE("type %#x.\n", urb->UrbHeader.Function);
|
|
|
|
|
|
|
|
switch (urb->UrbHeader.Function)
|
|
|
|
{
|
|
|
|
case URB_FUNCTION_ABORT_PIPE:
|
|
|
|
{
|
|
|
|
LIST_ENTRY *entry, *mark;
|
|
|
|
|
|
|
|
/* The documentation states that URB_FUNCTION_ABORT_PIPE may
|
|
|
|
* complete before outstanding requests complete, so we don't need
|
|
|
|
* to wait for them. */
|
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
mark = &device->irp_list;
|
|
|
|
for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
{
|
|
|
|
IRP *queued_irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
|
|
|
|
struct usb_cancel_transfer_params params =
|
|
|
|
{
|
|
|
|
.transfer = queued_irp->Tail.Overlay.DriverContext[0],
|
|
|
|
};
|
|
|
|
|
2022-11-24 09:34:28 +00:00
|
|
|
WINE_UNIX_CALL(unix_usb_cancel_transfer, ¶ms);
|
2022-05-25 22:00:02 +00:00
|
|
|
}
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
case URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL:
|
|
|
|
case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
|
|
|
|
case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
|
|
|
|
case URB_FUNCTION_SELECT_CONFIGURATION:
|
|
|
|
case URB_FUNCTION_VENDOR_INTERFACE:
|
|
|
|
{
|
2022-07-06 04:53:45 +00:00
|
|
|
struct usb_submit_urb_params params =
|
|
|
|
{
|
|
|
|
.device = device->unix_device,
|
|
|
|
.irp = irp,
|
|
|
|
};
|
|
|
|
|
2022-09-10 14:15:29 +00:00
|
|
|
switch (urb->UrbHeader.Function)
|
|
|
|
{
|
|
|
|
case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
|
|
|
|
{
|
|
|
|
struct _URB_BULK_OR_INTERRUPT_TRANSFER *req = &urb->UrbBulkOrInterruptTransfer;
|
|
|
|
if (req->TransferBufferMDL)
|
|
|
|
params.transfer_buffer = MmGetSystemAddressForMdlSafe(req->TransferBufferMDL, NormalPagePriority);
|
|
|
|
else
|
|
|
|
params.transfer_buffer = req->TransferBuffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
|
|
|
|
{
|
|
|
|
struct _URB_CONTROL_DESCRIPTOR_REQUEST *req = &urb->UrbControlDescriptorRequest;
|
|
|
|
if (req->TransferBufferMDL)
|
|
|
|
params.transfer_buffer = MmGetSystemAddressForMdlSafe(req->TransferBufferMDL, NormalPagePriority);
|
|
|
|
else
|
|
|
|
params.transfer_buffer = req->TransferBuffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case URB_FUNCTION_VENDOR_INTERFACE:
|
|
|
|
{
|
|
|
|
struct _URB_CONTROL_VENDOR_OR_CLASS_REQUEST *req = &urb->UrbControlVendorClassRequest;
|
|
|
|
if (req->TransferBufferMDL)
|
|
|
|
params.transfer_buffer = MmGetSystemAddressForMdlSafe(req->TransferBufferMDL, NormalPagePriority);
|
|
|
|
else
|
|
|
|
params.transfer_buffer = req->TransferBuffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 22:00:02 +00:00
|
|
|
/* Hold the wineusb lock while submitting and queuing, and
|
|
|
|
* similarly hold it in complete_irp(). That way, if libusb reports
|
|
|
|
* completion between submitting and queuing, we won't try to
|
|
|
|
* dequeue the IRP until it's actually been queued. */
|
|
|
|
EnterCriticalSection(&wineusb_cs);
|
2022-11-24 09:34:28 +00:00
|
|
|
status = WINE_UNIX_CALL(unix_usb_submit_urb, ¶ms);
|
2022-05-25 22:00:02 +00:00
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
IoMarkIrpPending(irp);
|
|
|
|
InsertTailList(&device->irp_list, &irp->Tail.Overlay.ListEntry);
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Unhandled function %#x.\n", urb->UrbHeader.Function);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:03:04 +00:00
|
|
|
static NTSTATUS WINAPI driver_internal_ioctl(DEVICE_OBJECT *device_obj, IRP *irp)
|
|
|
|
{
|
|
|
|
IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation(irp);
|
|
|
|
ULONG code = stack->Parameters.DeviceIoControl.IoControlCode;
|
|
|
|
struct usb_device *device = device_obj->DeviceExtension;
|
|
|
|
NTSTATUS status = STATUS_NOT_IMPLEMENTED;
|
2021-07-01 07:51:24 +00:00
|
|
|
BOOL removed;
|
2020-04-17 23:03:04 +00:00
|
|
|
|
2022-11-17 10:17:42 +00:00
|
|
|
TRACE("device_obj %p, irp %p, code %#lx.\n", device_obj, irp, code);
|
2020-04-17 23:03:04 +00:00
|
|
|
|
2021-07-01 07:51:24 +00:00
|
|
|
EnterCriticalSection(&wineusb_cs);
|
|
|
|
removed = device->removed;
|
|
|
|
LeaveCriticalSection(&wineusb_cs);
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
return STATUS_DELETE_PENDING;
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:03:04 +00:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case IOCTL_INTERNAL_USB_SUBMIT_URB:
|
|
|
|
status = usb_submit_urb(device, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-17 10:17:42 +00:00
|
|
|
FIXME("Unhandled ioctl %#lx (device %#lx, access %#lx, function %#lx, method %#lx).\n",
|
2020-04-17 23:03:04 +00:00
|
|
|
code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
|
|
|
|
}
|
|
|
|
|
2020-04-25 15:37:19 +00:00
|
|
|
if (status != STATUS_PENDING)
|
2020-04-17 23:03:04 +00:00
|
|
|
{
|
|
|
|
irp->IoStatus.Status = status;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:19 +00:00
|
|
|
static NTSTATUS WINAPI driver_add_device(DRIVER_OBJECT *driver, DEVICE_OBJECT *pdo)
|
|
|
|
{
|
|
|
|
NTSTATUS ret;
|
|
|
|
|
|
|
|
TRACE("driver %p, pdo %p.\n", driver, pdo);
|
|
|
|
|
|
|
|
if ((ret = IoCreateDevice(driver, 0, NULL, FILE_DEVICE_BUS_EXTENDER, 0, FALSE, &bus_fdo)))
|
|
|
|
{
|
2022-11-17 10:17:42 +00:00
|
|
|
ERR("Failed to create FDO, status %#lx.\n", ret);
|
2020-04-15 14:43:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IoAttachDeviceToDeviceStack(bus_fdo, pdo);
|
|
|
|
bus_pdo = pdo;
|
|
|
|
bus_fdo->Flags &= ~DO_DEVICE_INITIALIZING;
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:18 +00:00
|
|
|
static void WINAPI driver_unload(DRIVER_OBJECT *driver)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS WINAPI DriverEntry(DRIVER_OBJECT *driver, UNICODE_STRING *path)
|
|
|
|
{
|
2022-05-25 20:41:19 +00:00
|
|
|
NTSTATUS status;
|
2020-04-15 14:43:18 +00:00
|
|
|
|
|
|
|
TRACE("driver %p, path %s.\n", driver, debugstr_w(path->Buffer));
|
|
|
|
|
2022-11-24 09:34:28 +00:00
|
|
|
if ((status = __wine_init_unix_call()))
|
2022-05-25 20:41:19 +00:00
|
|
|
{
|
2022-11-17 10:17:42 +00:00
|
|
|
ERR("Failed to initialize Unix library, status %#lx.\n", status);
|
2022-05-25 20:41:19 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:43:22 +00:00
|
|
|
driver_obj = driver;
|
|
|
|
|
2020-04-15 14:43:19 +00:00
|
|
|
driver->DriverExtension->AddDevice = driver_add_device;
|
2020-04-15 14:43:18 +00:00
|
|
|
driver->DriverUnload = driver_unload;
|
2020-04-15 14:43:20 +00:00
|
|
|
driver->MajorFunction[IRP_MJ_PNP] = driver_pnp;
|
2020-04-17 23:03:04 +00:00
|
|
|
driver->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = driver_internal_ioctl;
|
2020-04-15 14:43:18 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|