Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocation / OS check optimization, Mingw updates #36

Merged
merged 10 commits into from
Sep 19, 2023
12 changes: 11 additions & 1 deletion libusb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ endif
ifdef cflags
DBG_DEFINE = $(cflags)
endif
ifdef ddk
DDK_INCLUDE = -I$(ddk)/include/ddk
endif

CC = $(host_prefix)gcc
LD = $(host_prefix)ld
Expand Down Expand Up @@ -87,8 +90,10 @@ LIBWDI_OBJECTS = $(LIBWDI_DIR)/logging.5.o \


INCLUDES = -I./src -I./src/driver -I.
driver: INCLUDES += $(DDK_INCLUDE)

CFLAGS = -O2 -Wall -DWINVER=0x500 $(DBG_DEFINE)
CFLAGS += -Wno-unknown-pragmas -Wno-multichar
WIN_CFLAGS = $(CFLAGS) -mwindows

WINDRES_FLAGS = -I$(SRC_DIR)
Expand Down Expand Up @@ -121,9 +126,14 @@ LIBWDI_DLL_LDFLAGS = -s -shared \
-Wl,--enable-stdcall-fixup \
-L. -lnewdev -lsetupapi -lole32

DRIVER_LDFLAGS = -s -shared -Wl,--entry,_DriverEntry@8 \
DRIVER_LDFLAGS = -s -shared -Wl,--entry,$(DRIVERENTRY) \
-nostartfiles -nostdlib -L. -lusbd -lntoskrnl -lhal

ifneq (,$(findstring x86, $(host_prefix)))
DRIVERENTRY = DriverEntry
else
DRIVERENTRY = _DriverEntry@8
endif

.PHONY: all
all: dll filter test testwin
Expand Down
8 changes: 8 additions & 0 deletions libusb/src/driver/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp)

case IRP_MJ_CREATE:

USBDBG("IRP_MJ_CREATE: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);

if (dev->is_started)
{
// only one driver can act as power policy owner and
Expand All @@ -79,6 +83,10 @@ NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp)

case IRP_MJ_CLOSE:

USBDBG("IRP_MJ_CLOSE: is-filter=%c %s\n",
dev->is_filter ? 'Y' : 'N',
dev->device_id);

/* release all interfaces bound to this file object */
release_all_interfaces(dev, stack_location->FileObject);
return complete_irp(irp, STATUS_SUCCESS, 0);
Expand Down
1 change: 1 addition & 0 deletions libusb/src/driver/driver_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "libusb_driver.h"
#include "lusb_defdi_guids.h"
#include <guiddef.h>

/* missing in mingw's ddk headers */
#ifndef PLUGPLAY_REGKEY_DEVICE
Expand Down
1 change: 0 additions & 1 deletion libusb/src/driver/get_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ NTSTATUS get_interface(libusb_device_t *dev,
{
NTSTATUS status = STATUS_SUCCESS;
URB urb;
PUSB_INTERFACE_DESCRIPTOR interface_descriptor;

USBMSG("interface: %d timeout: %d\n", interface_number, timeout);

Expand Down
4 changes: 2 additions & 2 deletions libusb/src/driver/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp)
}
else
{
status = get_configuration(dev, output_buffer, &ret, request->timeout);
status = get_configuration(dev, (unsigned char *)output_buffer, &ret, request->timeout);
}
break;

Expand All @@ -370,7 +370,7 @@ NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp)
status = get_interface(
dev,
request->intf.interface_number,
output_buffer,
(unsigned char *)output_buffer,
request->timeout);

if (NT_SUCCESS(status))
Expand Down
62 changes: 29 additions & 33 deletions libusb/src/driver/libusb_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const char* attached_driver_wdf_list[] =

static bool_t match_driver(PDEVICE_OBJECT deviceObject, const char* driverString);

static RTL_OSVERSIONINFOW os_version;
static POOL_TYPE alloc_pool;

#ifdef DBG

Expand Down Expand Up @@ -121,6 +123,26 @@ NTSTATUS DDKAPI DriverEntry(DRIVER_OBJECT *driver_object,
driver_object->DriverExtension->AddDevice = add_device;
driver_object->DriverUnload = unload;

/* Determine running OS version */
os_version.dwOSVersionInfoSize = sizeof(os_version);
if (!NT_SUCCESS(RtlGetVersion(&os_version)))
{
os_version.dwMajorVersion = 5;
os_version.dwMinorVersion = 0;
}

/* Windows 8 needs non-executable paged pool to pass verification test */
if (os_version.dwMajorVersion > 6 ||
(os_version.dwMajorVersion == 6 && os_version.dwMinorVersion >= 2))
{
alloc_pool = 512; /* NonPagedPoolNx - Windows 8 and up */
}
else
{
/* Windows 7 and below only has normal paged pool */
alloc_pool = NonPagedPool;
}

return STATUS_SUCCESS;
}

Expand Down Expand Up @@ -771,26 +793,25 @@ USB_INTERFACE_DESCRIPTOR* find_interface_desc_ex(USB_CONFIGURATION_DESCRIPTOR *c
#define INTF_FIELD 0
#define ALTF_FIELD 1

usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc;
usb_descriptor_header_t *desc = (usb_descriptor_header_t *)config_desc;
char *p = (char *)desc;
int lastInfNumber, lastAltNumber;
int currentInfIndex;
short InterfacesByIndex[LIBUSB_MAX_NUMBER_OF_INTERFACES][2];
int currentInfIndex;
short InterfacesByIndex[LIBUSB_MAX_NUMBER_OF_INTERFACES][2];

USB_INTERFACE_DESCRIPTOR *if_desc = NULL;

memset(InterfacesByIndex,0xFF,sizeof(InterfacesByIndex));
memset(InterfacesByIndex,0xFF,sizeof(InterfacesByIndex));

if (!config_desc)
return NULL;

size = size > config_desc->wTotalLength ? config_desc->wTotalLength : size;
size = size > config_desc->wTotalLength ? config_desc->wTotalLength : size;

while (size && desc->length <= size)
{
if (desc->type == USB_INTERFACE_DESCRIPTOR_TYPE)
{
// this is a new interface or alternate interface
// this is a new interface or alternate interface
if_desc = (USB_INTERFACE_DESCRIPTOR *)desc;
for (currentInfIndex=0; currentInfIndex<LIBUSB_MAX_NUMBER_OF_INTERFACES;currentInfIndex++)
{
Expand Down Expand Up @@ -987,30 +1008,5 @@ Return Value:

PVOID allocate_pool(SIZE_T bytes)
{
ULONG major;
ULONG minor;
NTSTATUS status;
RTL_OSVERSIONINFOW version;
/* Windwos 7 and below only has normal paged pool */
POOL_TYPE pool = NonPagedPool;

version.dwOSVersionInfoSize = sizeof(version);
status = RtlGetVersion(&version);
if(!NT_VERIFY(NT_SUCCESS(status)))
{
major = 5;
minor = 0;
}
else
{
major = version.dwMajorVersion;
minor = version.dwMinorVersion;
}

/* Windwos 8 needs non-executable paged pool to pass verification test */
if((major > 6) || ((major == 6) && (minor >= 2)))
{
pool = 512; /* NonPagedPoolNx - Windows 8 and up */
}
return ExAllocatePool(pool, bytes);
return ExAllocatePool(alloc_pool, bytes);
}
28 changes: 8 additions & 20 deletions libusb/src/driver/libusb_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
//#define SKIP_DEVICES_WINUSB
//#define SKIP_DEVICES_PICOPP

#include <ntifs.h>
#ifdef __GNUC__
#include <ddk/usb100.h>
#include <ddk/usbdi.h>
#include <ddk/winddk.h>
#include "usbdlib_gcc.h"
#define NTSTATUS LONG
#else
#include <ntifs.h>
#include <wdm.h>
#endif
#include "usbdi.h"
#include "usbdlib.h"
#endif

#include <wchar.h>
#include <initguid.h>
Expand All @@ -45,19 +42,6 @@
#include "error.h"
#include "driver_api.h"

/* some missing defines */
#ifdef __GNUC__

#define USBD_TRANSFER_DIRECTION_OUT 0
#define USBD_TRANSFER_DIRECTION_BIT 0
#define USBD_TRANSFER_DIRECTION_IN (1 << USBD_TRANSFER_DIRECTION_BIT)
#define USBD_SHORT_TRANSFER_OK_BIT 1
#define USBD_SHORT_TRANSFER_OK (1 << USBD_SHORT_TRANSFER_OK_BIT)
#define USBD_START_ISO_TRANSFER_ASAP_BIT 2
#define USBD_START_ISO_TRANSFER_ASAP (1 << USBD_START_ISO_TRANSFER_ASAP_BIT)

#endif

#define SET_CONFIG_ACTIVE_CONFIG -258

#define USB_RECIP_DEVICE 0x00
Expand All @@ -81,7 +65,9 @@
#define LIBUSB_MAX_CONTROL_TRANSFER_TIMEOUT 5000


#ifndef __GNUC__
#ifdef __GNUC__
#define DDKAPI __stdcall
#else
#define DDKAPI
#endif

Expand Down Expand Up @@ -140,7 +126,9 @@ typedef int bool_t;

#endif

#ifndef __GNUC__
#define USB_ENDPOINT_ADDRESS_MASK 0x0F
#endif
#define USB_ENDPOINT_DIR_MASK 0x80
#define LBYTE(w) (w & 0xFF)
#define HBYTE(w) ((w>>8) & 0xFF)
Expand Down
6 changes: 2 additions & 4 deletions libusb/src/driver/lusb_defdi_guids.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#define __LUSB_DEFDI_GUIDS_

#ifndef DEFINE_TO_STR
#define _DEFINE_TO_STR(x) #x
#define DEFINE_TO_STR(x) _DEFINE_TO_STR(x)
#define DEFINE_TO_STR(x) #x
#endif

#ifndef DEFINE_TO_STRW
#define _DEFINE_TO_STRW(x) L#x
#define DEFINE_TO_STRW(x) _DEFINE_TO_STRW(x)
#define DEFINE_TO_STRW(x) L ## #x
#endif

#define DEFINE_DEVICE_INTERFACE_GUID(BaseFieldName,BaseGuidName,L1,W1,W2,B1,B2,B3,B4,B5,B6,B7,B8) \
Expand Down
2 changes: 1 addition & 1 deletion libusb/src/driver/set_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ NTSTATUS set_configuration(libusb_device_t *dev,
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;
int i, j, interface_number, desc_size, config_index;

// check if this config value is already set
if ((configuration > 0) && dev->config.value == configuration)
Expand Down
2 changes: 0 additions & 2 deletions libusb/src/driver/set_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ NTSTATUS set_interface_ex(libusb_device_t *dev,
interface_request_t* interface_request,
int timeout)
{
NTSTATUS status = STATUS_SUCCESS;

USB_INTERFACE_DESCRIPTOR *interface_descriptor = NULL;

USBMSG("interface-%s=%d alt-%s=%d timeout=%d\n",
Expand Down
3 changes: 1 addition & 2 deletions libusb/src/driver/transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ NTSTATUS large_transfer(IN libusb_device_t* dev,
IN PMDL mdlAddress,
IN int totalLength)
{
PIO_STACK_LOCATION irpStack;
BOOLEAN read;
ULONG stageSize;
ULONG numIrps;
Expand Down Expand Up @@ -424,7 +423,6 @@ NTSTATUS large_transfer(IN libusb_device_t* dev,
//
// initialize vars
//
irpStack = IoGetCurrentIrpStackLocation(irp);
sequenceID = InterlockedIncrement(&sequence);
subRequestContextArray = NULL;

Expand Down Expand Up @@ -987,6 +985,7 @@ NTSTATUS large_transfer_complete(IN PDEVICE_OBJECT DeviceObjectIsNULL,
subRequestByteCount = MmGetMdlByteCount(subUrb->UrbBulkOrInterruptTransfer.TransferBufferMDL);
subRequestByteOffset = MmGetMdlByteOffset(subUrb->UrbBulkOrInterruptTransfer.TransferBufferMDL);
information = subUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
(void) subRequestByteOffset;
USBDBG("[%s #%d] offset=%d requested=%d transferred=%d\n",
dispTransfer,
sequenceID,
Expand Down
10 changes: 1 addition & 9 deletions libusb/src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@
#include <stdio.h>

#if IS_DRIVER
#ifdef __GNUC__
#define OBJ_KERNEL_HANDLE 0x00000200L
#include <ddk/usb100.h>
#include <ddk/usbdi.h>
#include <ddk/winddk.h>
#include "usbdlib_gcc.h"
#else
#include <ntddk.h>
#endif
#include <ntddk.h>
#else
#include <windows.h>
#endif
Expand Down
10 changes: 2 additions & 8 deletions libusb/src/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@
#include <richedit.h>
#include <conio.h>
#include <ctype.h>

#ifdef __GNUC__
#if defined(_WIN64)
#include <cfgmgr32.h>
#else
#include <ddk/cfgmgr32.h>
#endif
#else
#include <cfgmgr32.h>

#ifndef __GNUC__
#define strlwr(p) _strlwr(p)
#endif

Expand Down
10 changes: 2 additions & 8 deletions libusb/src/install_filter_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
#define INITGUID

#include <windows.h>
#include <cfgmgr32.h>

#ifdef __GNUC__
#if defined(_WIN64)
#include <cfgmgr32.h>
#else
#include <ddk/cfgmgr32.h>
#endif
#else
#include <cfgmgr32.h>
#ifndef __GNUC__
#define strlwr(p) _strlwr(p)
#endif

Expand Down
10 changes: 2 additions & 8 deletions libusb/src/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __GNUC__
#if defined(_WIN64)
#include <cfgmgr32.h>
#else
#include <ddk/cfgmgr32.h>
#endif
#else
#include <cfgmgr32.h>

#ifndef __GNUC__
#define strlwr(p) _strlwr(p)
#endif

Expand Down