From b24d095e90f8206809ba8fae04a4057443e4567a Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sun, 12 Aug 2018 18:11:03 +0200 Subject: [PATCH] usb: support for retrieving full configuration descriptors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In USB, the interface and endpoint descriptors, and possibly other vendor-specific descriptors, are nested inside the configuration descriptors. This makes it possible to retrieve the complete information about a configuration in one single call. Our drivers do this, and so does libusb (the Device Kit doesn't, it only cares about individual descriptors and does not provide access to the vendor-specific ones). The driver did not expose the full descriptor, only the part that belongs strictly to the configuration. libusb worked around this by getting the descriptor from the device directly, using a control transfer. This should be ok, but apparently some devices get confused when you do this too often or at unexpected times. These changes introduce a variation of the GET_CONFIGURATION_DESCRIPTOR ioctl that allows the caller to specify a size. This way, one can get the complete descriptor (after getting the configuration-only part to figure out the size needed, most likely). The data is copied from structures stored by the driver, so no further communication with the device is necessary, making this safe to the problems mentioned above, and faster. Change-Id: Id97e40ea0d45b8c051ae8548486c4751fc6aad2a Reviewed-on: https://review.haiku-os.org/453 Reviewed-by: Jérôme Duval --- src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp | 18 +++++++++++++++--- src/add-ons/kernel/drivers/bus/usb/usb_raw.h | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp b/src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp index cd7caabb34..56a15573c2 100644 --- a/src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp +++ b/src/add-ons/kernel/drivers/bus/usb/usb_raw.cpp @@ -1,15 +1,17 @@ /* - * Copyright 2006-2010, Haiku Inc. All rights reserved. + * Copyright 2006-2018, Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Michael Lotz + * Adrien Destugues */ #include "usb_raw.h" #include #include +#include #include #include #include @@ -311,7 +313,12 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length) } case B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR: + case B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR_ETC: { + if (op == B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR_ETC + && length < sizeof(command.config_etc)) + return B_BUFFER_OVERFLOW; + if (length < sizeof(command.config)) return B_BUFFER_OVERFLOW; @@ -322,10 +329,15 @@ usb_raw_ioctl(void *cookie, uint32 op, void *buffer, size_t length) if (configurationInfo == NULL) break; + size_t sizeToCopy = sizeof(usb_configuration_descriptor); + if (op == B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR_ETC) { + sizeToCopy = std::min(command.config_etc.length, + (size_t)configurationInfo->descr->total_length); + } + if (!IS_USER_ADDRESS(command.config.descriptor) || user_memcpy(command.config.descriptor, - configurationInfo->descr, - sizeof(usb_configuration_descriptor)) != B_OK) { + configurationInfo->descr, sizeToCopy) != B_OK) { return B_BAD_ADDRESS; } diff --git a/src/add-ons/kernel/drivers/bus/usb/usb_raw.h b/src/add-ons/kernel/drivers/bus/usb/usb_raw.h index 54112c2202..9968fa825e 100644 --- a/src/add-ons/kernel/drivers/bus/usb/usb_raw.h +++ b/src/add-ons/kernel/drivers/bus/usb/usb_raw.h @@ -25,6 +25,7 @@ typedef enum { B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR_ETC, B_USB_RAW_COMMAND_GET_ENDPOINT_DESCRIPTOR_ETC, B_USB_RAW_COMMAND_GET_GENERIC_DESCRIPTOR_ETC, + B_USB_RAW_COMMAND_GET_CONFIGURATION_DESCRIPTOR_ETC, B_USB_RAW_COMMAND_SET_CONFIGURATION = 0x3000, B_USB_RAW_COMMAND_SET_FEATURE, @@ -74,6 +75,13 @@ typedef union { uint32 config_index; } config; + struct { + status_t status; + usb_configuration_descriptor *descriptor; + uint32 config_index; + size_t length; + } config_etc; + struct { status_t status; uint32 alternate_info;