Skip to content

Commit

Permalink
Merge pull request #53 from mcuee/libusb-max-transfer
Browse files Browse the repository at this point in the history
dll: do not limit the transfer size towards the driver, as the driver…
  • Loading branch information
dontech authored Jan 25, 2024
2 parents 8771703 + 5b867bd commit 9d730db
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
3 changes: 2 additions & 1 deletion libusb/src/driver/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
request->endpoint.transfer_flags, \
request->endpoint.iso_start_frame_latency, \
transfer_buffer_mdl, \
transfer_buffer_length);
transfer_buffer_length, \
maxTransferSize);

NTSTATUS dispatch_ioctl(libusb_device_t *dev, IRP *irp)
{
Expand Down
3 changes: 2 additions & 1 deletion libusb/src/driver/libusb_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ NTSTATUS transfer(libusb_device_t* dev,
IN int transferFlags,
IN int isoLatency,
IN PMDL mdlAddress,
IN int totalLength);
IN int totalLength,
IN int maxTransferSize);

ULONG get_current_frame(IN PDEVICE_EXTENSION dev, IN PIRP Irp);

Expand Down
28 changes: 17 additions & 11 deletions libusb/src/driver/transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct
int totalLength;
int information;
int maximum_packet_size;
int maxTransferSize;
IN PMDL mdlAddress;
PMDL subMdl;
} context_t;
Expand Down Expand Up @@ -89,12 +90,14 @@ NTSTATUS transfer(libusb_device_t* dev,
IN int transferFlags,
IN int isoLatency,
IN PMDL mdlAddress,
IN int totalLength)
IN int totalLength,
IN int maxTransferSize)
{
context_t *context;
NTSTATUS status = STATUS_SUCCESS;
int sequenceID = InterlockedIncrement(&sequence);
const char* dispTransfer = GetPipeDisplayName(endpoint);
int first_size;

// TODO: reset pipe flag
// status = reset_endpoint(dev,endpoint->address, LIBUSB_DEFAULT_TIMEOUT);
Expand All @@ -109,8 +112,8 @@ NTSTATUS transfer(libusb_device_t* dev,
}
else
{
USBMSG("[%s #%d] EP%02Xh length %d\n",
dispTransfer, sequenceID, endpoint->address, totalLength);
USBMSG("[%s #%d] EP%02Xh length=%d, packetSize=%d, maxTransferSize=%d\n",
dispTransfer, sequenceID, endpoint->address, totalLength, packetSize, maxTransferSize);
}
context = allocate_pool(sizeof(context_t));

Expand All @@ -128,9 +131,12 @@ NTSTATUS transfer(libusb_device_t* dev,
context->mdlAddress = mdlAddress;
context->subMdl = NULL;
context->information = 0;
context->maxTransferSize = maxTransferSize;

first_size = (totalLength > context->maxTransferSize) ? context->maxTransferSize : totalLength;

status = create_urb(dev, &context->urb, direction, urbFunction,
endpoint, packetSize, mdlAddress, totalLength);
endpoint, packetSize, mdlAddress, first_size);
if (!NT_SUCCESS(status))
{
ExFreePool(context);
Expand Down Expand Up @@ -175,8 +181,8 @@ NTSTATUS DDKAPI transfer_complete(DEVICE_OBJECT *device_object, IRP *irp,
= c->urb->UrbBulkOrInterruptTransfer.TransferBufferLength;
}

USBMSG("sequence %d: %d bytes transmitted\n",
c->sequence, transmitted);
USBMSG("sequence %d: %d bytes transmitted, maximum_packet_size=%d, totalLength=%d\n",
c->sequence, transmitted, c->maximum_packet_size, c->totalLength);
}
else
{
Expand All @@ -203,10 +209,10 @@ NTSTATUS DDKAPI transfer_complete(DEVICE_OBJECT *device_object, IRP *irp,
if(NT_SUCCESS(irp->IoStatus.Status)
&& USBD_SUCCESS(c->urb->UrbHeader.Status)
&& (c->urb->UrbHeader.Function == URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER)
&& (c->urb->UrbBulkOrInterruptTransfer.TransferFlags & USBD_TRANSFER_DIRECTION_IN)
&& (transmitted == c->maximum_packet_size)
&& !(transmitted % c->maximum_packet_size)
&& c->totalLength)
{
int next_size = (c->totalLength > c->maxTransferSize) ? c->maxTransferSize : c->totalLength;
PUCHAR virtualAddress = (PUCHAR)MmGetMdlVirtualAddress(c->mdlAddress);
if(!virtualAddress)
{
Expand All @@ -219,18 +225,18 @@ NTSTATUS DDKAPI transfer_complete(DEVICE_OBJECT *device_object, IRP *irp,
virtualAddress += c->information;

c->subMdl = IoAllocateMdl((PVOID)(virtualAddress),
c->totalLength, FALSE, FALSE, NULL);
next_size, FALSE, FALSE, NULL);
if(c->subMdl == NULL)
{
USBERR("[#%d] failed allocating subMdl\n", c->sequence);
status = STATUS_INSUFFICIENT_RESOURCES;
goto transfer_free;
}

IoBuildPartialMdl(irp->MdlAddress, c->subMdl, (PVOID)virtualAddress, c->totalLength);
IoBuildPartialMdl(irp->MdlAddress, c->subMdl, (PVOID)virtualAddress, next_size);

/* Re-use URB for another reception */
c->urb->UrbBulkOrInterruptTransfer.TransferBufferLength = c->totalLength;
c->urb->UrbBulkOrInterruptTransfer.TransferBufferLength = next_size;
c->urb->UrbBulkOrInterruptTransfer.TransferBufferMDL = c->subMdl;

status = transfer_next(dev, irp, c);
Expand Down
33 changes: 4 additions & 29 deletions libusb/src/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include "registry.h"
#include "libusb-win32_version.h"

#define LIBUSB_WIN32_DLL_LARGE_TRANSFER_SUPPORT

#define LIBUSB_DEFAULT_TIMEOUT 5000
#define LIBUSB_DEVICE_NAME "\\\\.\\libusb0-"
#define LIBUSB_BUS_NAME "bus-0"
Expand Down Expand Up @@ -562,7 +560,6 @@ static int _usb_transfer_sync(usb_dev_handle *dev, int control_code,
void *context = NULL;
int transmitted = 0;
int ret;
int requested;

if (!timeout) timeout=INFINITE;
ret = _usb_setup_async(dev, &context, control_code, (unsigned char )ep,
Expand All @@ -573,34 +570,12 @@ static int _usb_transfer_sync(usb_dev_handle *dev, int control_code,
return ret;
}

do
ret = usb_submit_async(context, bytes, size);
if(ret >= 0)
{
#ifdef LIBUSB_WIN32_DLL_LARGE_TRANSFER_SUPPORT
requested = size > LIBUSB_MAX_READ_WRITE ? LIBUSB_MAX_READ_WRITE : size;
#else
requested = size;
#endif
ret = usb_submit_async(context, bytes, requested);

if (ret < 0)
{
transmitted = ret;
break;
}

ret = usb_reap_async(context, timeout);

if (ret < 0)
{
transmitted = ret;
break;
}

transmitted += ret;
bytes += ret;
size -= ret;
ret = usb_reap_async(context, timeout);
transmitted = ret;
}
while (size > 0 && ret == requested);

usb_free_async(&context);

Expand Down

0 comments on commit 9d730db

Please sign in to comment.