Skip to content

Commit

Permalink
usb_raw: the command struct wasn't copied back in some cases.
Browse files Browse the repository at this point in the history
hopefully fixes #14050 after e2e571c.
  • Loading branch information
korli committed Mar 24, 2018
1 parent 8e27fff commit 8234666
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.device))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_device_descriptor *deviceDescriptor =
gUSBModule->get_device_descriptor(device->device);
if (deviceDescriptor == NULL)
Expand All @@ -307,7 +308,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

command.device.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -316,11 +316,12 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.config))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_configuration_info *configurationInfo =
usb_raw_get_configuration(device, command.config.config_index,
&command.config.status);
if (configurationInfo == NULL)
return B_OK;
break;

if (!IS_USER_ADDRESS(command.config.descriptor)
|| user_memcpy(command.config.descriptor,
Expand All @@ -330,7 +331,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

command.config.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -340,17 +340,17 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.alternate))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_configuration_info *configurationInfo =
usb_raw_get_configuration(device,
command.alternate.config_index,
&command.alternate.status);
if (configurationInfo == NULL)
return B_OK;
break;

if (command.alternate.interface_index
>= configurationInfo->interface_count) {
command.alternate.status = B_USB_RAW_STATUS_INVALID_INTERFACE;
status = B_OK;
break;
}

Expand All @@ -369,14 +369,14 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

command.alternate.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

case B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR:
case B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR_ETC:
{
const usb_interface_info *interfaceInfo = NULL;
status = B_OK;
if (op == B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR) {
if (length < sizeof(command.interface))
return B_BUFFER_OVERFLOW;
Expand All @@ -398,7 +398,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

if (interfaceInfo == NULL)
return B_OK;
break;

if (!IS_USER_ADDRESS(command.interface.descriptor)
|| user_memcpy(command.interface.descriptor, interfaceInfo->descr,
Expand All @@ -407,7 +407,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

command.interface.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -416,6 +415,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{
uint32 endpointIndex = 0;
const usb_interface_info *interfaceInfo = NULL;
status = B_OK;
if (op == B_USB_RAW_COMMAND_GET_ENDPOINT_DESCRIPTOR) {
if (length < sizeof(command.endpoint))
return B_BUFFER_OVERFLOW;
Expand All @@ -439,11 +439,10 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

if (interfaceInfo == NULL)
return B_OK;
break;

if (endpointIndex >= interfaceInfo->endpoint_count) {
command.endpoint.status = B_USB_RAW_STATUS_INVALID_ENDPOINT;
status = B_OK;
break;
}

Expand All @@ -455,7 +454,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

command.endpoint.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -465,6 +463,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
uint32 genericIndex = 0;
size_t genericLength = 0;
const usb_interface_info *interfaceInfo = NULL;
status = B_OK;
if (op == B_USB_RAW_COMMAND_GET_GENERIC_DESCRIPTOR) {
if (length < sizeof(command.generic))
return B_BUFFER_OVERFLOW;
Expand All @@ -490,17 +489,16 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
}

if (interfaceInfo == NULL)
return B_OK;
break;

if (genericIndex >= interfaceInfo->generic_count) {
command.endpoint.status = B_USB_RAW_STATUS_INVALID_ENDPOINT;
status = B_OK;
break;
}

usb_descriptor *descriptor = interfaceInfo->generic[genericIndex];
if (descriptor == NULL)
return B_OK;
break;

if (!IS_USER_ADDRESS(command.generic.descriptor)
|| user_memcpy(command.generic.descriptor, descriptor,
Expand All @@ -512,7 +510,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
command.generic.status = B_USB_RAW_STATUS_NO_MEMORY;
else
command.generic.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -523,6 +520,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)

size_t actualLength = 0;
uint8 firstTwoBytes[2];
status = B_OK;

if (gUSBModule->get_descriptor(device->device,
USB_DESCRIPTOR_STRING, command.string.string_index, 0,
Expand All @@ -531,15 +529,16 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
|| firstTwoBytes[1] != USB_DESCRIPTOR_STRING) {
command.string.status = B_USB_RAW_STATUS_ABORTED;
command.string.length = 0;
return B_OK;
break;
}

uint8 stringLength = MIN(firstTwoBytes[0], command.string.length);
char *string = (char *)malloc(stringLength);
if (string == NULL) {
command.string.status = B_USB_RAW_STATUS_ABORTED;
command.string.length = 0;
return B_NO_MEMORY;
status = B_NO_MEMORY;
break;
}

if (gUSBModule->get_descriptor(device->device,
Expand All @@ -549,7 +548,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
command.string.status = B_USB_RAW_STATUS_ABORTED;
command.string.length = 0;
free(string);
status = B_OK;
break;
}

Expand All @@ -563,7 +561,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
command.string.status = B_USB_RAW_STATUS_SUCCESS;
command.string.length = stringLength;
free(string);
status = B_OK;
break;
}

Expand All @@ -574,6 +571,7 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)

size_t actualLength = 0;
uint8 firstTwoBytes[2];
status = B_OK;

if (gUSBModule->get_descriptor(device->device,
command.descriptor.type, command.descriptor.index,
Expand All @@ -583,7 +581,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
|| firstTwoBytes[1] != command.descriptor.type) {
command.descriptor.status = B_USB_RAW_STATUS_ABORTED;
command.descriptor.length = 0;
status = B_OK;
break;
}

Expand All @@ -605,7 +602,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
command.descriptor.status = B_USB_RAW_STATUS_ABORTED;
command.descriptor.length = 0;
free(descriptorBuffer);
status = B_OK;
break;
}

Expand All @@ -619,7 +615,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
command.descriptor.status = B_USB_RAW_STATUS_SUCCESS;
command.descriptor.length = descriptorLength;
free(descriptorBuffer);
status = B_OK;
break;
}

Expand All @@ -628,21 +623,20 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.config))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_configuration_info *configurationInfo =
usb_raw_get_configuration(device, command.config.config_index,
&command.config.status);
if (configurationInfo == NULL)
return B_OK;
break;

if (gUSBModule->set_configuration(device->device,
configurationInfo) < B_OK) {
command.config.status = B_USB_RAW_STATUS_FAILED;
status = B_OK;
break;
}

command.config.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand All @@ -651,37 +645,34 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.alternate))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_configuration_info *configurationInfo =
usb_raw_get_configuration(device,
command.alternate.config_index,
&command.alternate.status);
if (configurationInfo == NULL)
return B_OK;
break;

if (command.alternate.interface_index
>= configurationInfo->interface_count) {
command.alternate.status = B_USB_RAW_STATUS_INVALID_INTERFACE;
status = B_OK;
break;
}

const usb_interface_list *interfaceList =
&configurationInfo->interface[command.alternate.interface_index];
if (command.alternate.alternate_info >= interfaceList->alt_count) {
command.alternate.status = B_USB_RAW_STATUS_INVALID_INTERFACE;
status = B_OK;
break;
}

if (gUSBModule->set_alt_interface(device->device,
&interfaceList->alt[command.alternate.alternate_info]) < B_OK) {
command.alternate.status = B_USB_RAW_STATUS_FAILED;
status = B_OK;
break;
}

command.alternate.status = B_USB_RAW_STATUS_SUCCESS;
status = B_OK;
break;
}

Expand Down Expand Up @@ -735,39 +726,35 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (length < sizeof(command.transfer))
return B_BUFFER_OVERFLOW;

status = B_OK;
const usb_configuration_info *configurationInfo =
gUSBModule->get_configuration(device->device);
if (configurationInfo == NULL) {
command.transfer.status = B_USB_RAW_STATUS_INVALID_CONFIGURATION;
status = B_OK;
break;
}

if (command.transfer.interface >= configurationInfo->interface_count) {
command.transfer.status = B_USB_RAW_STATUS_INVALID_INTERFACE;
status = B_OK;
break;
}

const usb_interface_info *interfaceInfo =
configurationInfo->interface[command.transfer.interface].active;
if (interfaceInfo == NULL) {
command.transfer.status = B_USB_RAW_STATUS_ABORTED;
status = B_OK;
break;
}

if (command.transfer.endpoint >= interfaceInfo->endpoint_count) {
command.transfer.status = B_USB_RAW_STATUS_INVALID_ENDPOINT;
status = B_OK;
break;
}

const usb_endpoint_info *endpointInfo =
&interfaceInfo->endpoint[command.transfer.endpoint];
if (!endpointInfo->handle) {
command.transfer.status = B_USB_RAW_STATUS_INVALID_ENDPOINT;
status = B_OK;
break;
}

Expand All @@ -787,7 +774,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (packetDescriptors == NULL) {
command.transfer.status = B_USB_RAW_STATUS_NO_MEMORY;
command.transfer.length = 0;
status = B_OK;
break;
}

Expand All @@ -804,7 +790,6 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
if (transferData == NULL) {
command.transfer.status = B_USB_RAW_STATUS_NO_MEMORY;
command.transfer.length = 0;
status = B_OK;
break;
}

Expand Down

0 comments on commit 8234666

Please sign in to comment.