Skip to content

Commit

Permalink
vhci, code cleanup for usb descriptor manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
cezanne committed Jan 31, 2019
1 parent 4bf0e6c commit a32b206
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 53 deletions.
19 changes: 9 additions & 10 deletions driver/lib/devconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
PUSB_INTERFACE_DESCRIPTOR
dsc_find_intf(PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, UCHAR intf_num, USHORT alt_setting)
{
PVOID start = dsc_conf;
return USBD_ParseConfigurationDescriptorEx(dsc_conf, dsc_conf, intf_num, alt_setting, -1, -1, -1);
}

while (TRUE) {
PUSB_INTERFACE_DESCRIPTOR dsc_intf = (PUSB_INTERFACE_DESCRIPTOR)USBD_ParseDescriptors(dsc_conf, dsc_conf->wTotalLength, start, USB_INTERFACE_DESCRIPTOR_TYPE);
if (dsc_intf == NULL)
break;
if (dsc_intf->bInterfaceNumber == intf_num && dsc_intf->bAlternateSetting == alt_setting)
return dsc_intf;
start = NEXT_DESC(dsc_intf);
}
return NULL;
PUSB_ENDPOINT_DESCRIPTOR
dsc_next_ep(PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, PVOID start)
{
PUSB_COMMON_DESCRIPTOR dsc = (PUSB_COMMON_DESCRIPTOR)start;
if (dsc->bDescriptorType == USB_ENDPOINT_DESCRIPTOR_TYPE)
dsc = NEXT_DESC(dsc);
return (PUSB_ENDPOINT_DESCRIPTOR)USBD_ParseDescriptors(dsc_conf, dsc_conf->wTotalLength, dsc, USB_ENDPOINT_DESCRIPTOR_TYPE);
}

ULONG
Expand Down
5 changes: 4 additions & 1 deletion driver/lib/devconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <ntddk.h>
#include <usbdi.h>

#define NEXT_DESC(dsc) (PUSB_COMMON_DESCRIPTOR)((PUCHAR)(dsc) + (dsc)->bLength)
#define NEXT_DESC(dsc) (PUSB_COMMON_DESCRIPTOR)((PUCHAR)(dsc) + ((PUSB_COMMON_DESCRIPTOR)dsc)->bLength)
#define NEXT_DESC_INTF(dsc) (PUSB_INTERFACE_DESCRIPTOR)NEXT_DESC(dsc)
#define NEXT_DESC_EP(dsc) (PUSB_ENDPOINT_DESCRIPTOR)NEXT_DESC(dsc)

PUSB_INTERFACE_DESCRIPTOR
dsc_find_intf(PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, UCHAR intf_num, USHORT alt_setting);

PUSB_ENDPOINT_DESCRIPTOR
dsc_next_ep(PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, PVOID start);

ULONG
dsc_conf_get_n_intfs(PUSB_CONFIGURATION_DESCRIPTOR dsc_conf);
57 changes: 21 additions & 36 deletions driver/vhci/vhci_devconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@
#define TO_INTF_NUM(handle) (UCHAR)(((UINT_PTR)(handle)) >> 8)
#define TO_INTF_ALTSETTING(handle) (UCHAR)((UINT_PTR)(handle) & 0xff)

void
show_pipe(unsigned int num, PUSBD_PIPE_INFORMATION pipe)
#ifdef DBG

static const char *
dbg_pipe(PUSBD_PIPE_INFORMATION pipe)
{
DBGI(DBG_GENERAL, "pipe num %d:\n"
"MaximumPacketSize: %d\n"
"EndpointAddress: 0x%02x\n"
"Interval: %d\n"
"PipeType: %d\n"
"PiPeHandle: 0x%08x\n"
"MaximumTransferSize %d\n"
"PipeFlags 0x%08x\n", num,
pipe->MaximumPacketSize,
pipe->EndpointAddress,
pipe->Interval,
pipe->PipeType,
pipe->PipeHandle,
pipe->MaximumTransferSize,
pipe->PipeFlags);
static char buf[512];

dbg_snprintf(buf, 512, "addr:%02x intv:%d typ:%d mps:%d mts:%d flags:%x",
pipe->EndpointAddress, pipe->Interval, pipe->PipeType, pipe->PipeFlags,
pipe->MaximumPacketSize, pipe->MaximumTransferSize, pipe->PipeFlags);
return buf;
}

void
#endif

static void
set_pipe(PUSBD_PIPE_INFORMATION pipe, PUSB_ENDPOINT_DESCRIPTOR ep_desc, unsigned char speed)
{
pipe->MaximumPacketSize = ep_desc->wMaxPacketSize;
Expand All @@ -51,24 +46,23 @@ set_pipe(PUSBD_PIPE_INFORMATION pipe, PUSB_ENDPOINT_DESCRIPTOR ep_desc, unsigned
}

static NTSTATUS
setup_endpoints(USBD_INTERFACE_INFORMATION *intf, PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, PVOID start, UCHAR speed)
setup_endpoints(USBD_INTERFACE_INFORMATION *intf, PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, PUSB_INTERFACE_DESCRIPTOR dsc_intf, UCHAR speed)
{
PVOID start = dsc_intf;
unsigned int i;

for (i = 0; i < intf->NumberOfPipes; i++) {
PUSB_ENDPOINT_DESCRIPTOR dsc_ep;

show_pipe(i, &intf->Pipes[i]);

dsc_ep = (PUSB_ENDPOINT_DESCRIPTOR)USBD_ParseDescriptors(dsc_conf, dsc_conf->wTotalLength, start, USB_ENDPOINT_DESCRIPTOR_TYPE);
dsc_ep = dsc_next_ep(dsc_conf, start);
if (dsc_ep == NULL) {
DBGW(DBG_IOCTL, "no ep desc\n");
return FALSE;
}

set_pipe(&intf->Pipes[i], dsc_ep, speed);
show_pipe(i, &intf->Pipes[i]);
start = NEXT_DESC(dsc_ep);
DBGI(DBG_IOCTL, "ep setup: %s\n", dbg_pipe(&intf->Pipes[i]));
start = dsc_ep;
}
return TRUE;
}
Expand All @@ -84,13 +78,13 @@ setup_intf(USBD_INTERFACE_INFORMATION *intf, PUSB_CONFIGURATION_DESCRIPTOR dsc_c
return STATUS_SUCCESS;
}

dsc_intf = USBD_ParseConfigurationDescriptorEx(dsc_conf, dsc_conf, intf->InterfaceNumber, intf->AlternateSetting, -1, -1, -1);
dsc_intf = dsc_find_intf(dsc_conf, intf->InterfaceNumber, intf->AlternateSetting);
if (dsc_intf == NULL) {
DBGW(DBG_IOCTL, "no interface desc\n");
return STATUS_INVALID_DEVICE_REQUEST;
}
if (dsc_intf->bNumEndpoints != intf->NumberOfPipes) {
DBGW(DBG_IOCTL, "number of pipes is not same:(%d,%d)\n", dsc_intf->bNumEndpoints, intf->NumberOfPipes);
DBGW(DBG_IOCTL, "numbers of pipes are not same:(%d,%d)\n", dsc_intf->bNumEndpoints, intf->NumberOfPipes);
return STATUS_INVALID_DEVICE_REQUEST;
}

Expand All @@ -104,6 +98,7 @@ setup_intf(USBD_INTERFACE_INFORMATION *intf, PUSB_CONFIGURATION_DESCRIPTOR dsc_c
intf->Class = dsc_intf->bInterfaceClass;
intf->SubClass = dsc_intf->bInterfaceSubClass;
intf->Protocol = dsc_intf->bInterfaceProtocol;
intf->InterfaceHandle = TO_INTF_HANDLE(intf->InterfaceNumber, intf->AlternateSetting);

if (!setup_endpoints(intf, dsc_conf, dsc_intf, speed))
return STATUS_INVALID_DEVICE_REQUEST;
Expand Down Expand Up @@ -134,7 +129,6 @@ select_config(struct _URB_SELECT_CONFIGURATION *urb_selc, UCHAR speed)
if ((status = setup_intf(info_intf, dsc_conf, speed)) != STATUS_SUCCESS)
return status;

info_intf->InterfaceHandle = TO_INTF_HANDLE(info_intf->InterfaceNumber, info_intf->AlternateSetting);
info_intf = NEXT_USBD_INTERFACE_INFO(info_intf);
/* urb_selc may have less info_intf than bNumInterfaces in conf desc */
if ((PVOID)info_intf >= end_urb_selc)
Expand All @@ -148,18 +142,9 @@ select_config(struct _URB_SELECT_CONFIGURATION *urb_selc, UCHAR speed)
NTSTATUS
select_interface(struct _URB_SELECT_INTERFACE *urb_seli, PUSB_CONFIGURATION_DESCRIPTOR dsc_conf, UCHAR speed)
{
PUSB_INTERFACE_DESCRIPTOR dsc_intf;
PUSBD_INTERFACE_INFORMATION info_intf;
UCHAR intf_num, altsetting;

info_intf = &urb_seli->Interface;

intf_num = TO_INTF_NUM(info_intf->InterfaceHandle);
altsetting = TO_INTF_ALTSETTING(info_intf->InterfaceHandle);
dsc_intf = dsc_find_intf(dsc_conf, intf_num, altsetting);
if (dsc_intf == NULL) {
DBGW(DBG_URB, "non-existent interface: intf_num: %hhu %hhu\n", intf_num, altsetting);
return STATUS_INVALID_PARAMETER;
}
return setup_intf(info_intf, dsc_conf, speed);
}
6 changes: 0 additions & 6 deletions driver/vhci/vhci_devconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

#include <usbdi.h>

extern void
show_pipe(unsigned int num, PUSBD_PIPE_INFORMATION pipe);

extern void
set_pipe(PUSBD_PIPE_INFORMATION pipe, PUSB_ENDPOINT_DESCRIPTOR ep_desc, unsigned char speed);

extern NTSTATUS
select_config(struct _URB_SELECT_CONFIGURATION *urb_selc, UCHAR speed);

Expand Down

0 comments on commit a32b206

Please sign in to comment.