From 3b4f71e4df90d5c56aff0b135c8b672cc6da39b2 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 10 Aug 2021 03:10:54 +0100 Subject: [PATCH] Kernel/USB: Add all USB 2.0 bmRequestType fields --- Kernel/Bus/USB/UHCIController.cpp | 2 +- Kernel/Bus/USB/USBDevice.cpp | 7 +++---- Kernel/Bus/USB/USBRequest.h | 24 ++++++++++++++---------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Kernel/Bus/USB/UHCIController.cpp b/Kernel/Bus/USB/UHCIController.cpp index 7d36c3ae58e..bb197432f22 100644 --- a/Kernel/Bus/USB/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCIController.cpp @@ -579,7 +579,7 @@ void UHCIController::free_descriptor_chain(TransferDescriptor* first_descriptor) KResultOr UHCIController::submit_control_transfer(Transfer& transfer) { Pipe& pipe = transfer.pipe(); // Short circuit the pipe related to this transfer - bool direction_in = (transfer.request().request_type & USB_DEVICE_REQUEST_DEVICE_TO_HOST) == USB_DEVICE_REQUEST_DEVICE_TO_HOST; + bool direction_in = (transfer.request().request_type & USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST) == USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST; TransferDescriptor* setup_td = create_transfer_descriptor(pipe, PacketID::SETUP, sizeof(USBRequestData)); if (!setup_td) diff --git a/Kernel/Bus/USB/USBDevice.cpp b/Kernel/Bus/USB/USBDevice.cpp index 29026b89a4a..b4866ca96d2 100644 --- a/Kernel/Bus/USB/USBDevice.cpp +++ b/Kernel/Bus/USB/USBDevice.cpp @@ -46,9 +46,8 @@ KResult Device::enumerate() { USBDeviceDescriptor dev_descriptor {}; - // FIXME: 0x100 is a magic number for now, as I'm not quite sure how these are constructed.... // Send 8-bytes to get at least the `max_packet_size` from the device - auto transfer_length_or_error = m_default_pipe->control_transfer(USB_DEVICE_REQUEST_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, 0x100, 0, 8, &dev_descriptor); + auto transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, 8, &dev_descriptor); if (transfer_length_or_error.is_error()) return transfer_length_or_error.error(); @@ -62,7 +61,7 @@ KResult Device::enumerate() VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE); m_default_pipe->set_max_packet_size(dev_descriptor.max_packet_size); - transfer_length_or_error = m_default_pipe->control_transfer(USB_DEVICE_REQUEST_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, 0x100, 0, sizeof(USBDeviceDescriptor), &dev_descriptor); + transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, sizeof(USBDeviceDescriptor), &dev_descriptor); if (transfer_length_or_error.is_error()) return transfer_length_or_error.error(); @@ -87,7 +86,7 @@ KResult Device::enumerate() m_address = m_controller->allocate_address(); // Attempt to set devices address on the bus - transfer_length_or_error = m_default_pipe->control_transfer(USB_DEVICE_REQUEST_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, m_address, 0, 0, nullptr); + transfer_length_or_error = m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, m_address, 0, 0, nullptr); if (transfer_length_or_error.is_error()) return transfer_length_or_error.error(); diff --git a/Kernel/Bus/USB/USBRequest.h b/Kernel/Bus/USB/USBRequest.h index 38f184de1f0..b307919848d 100644 --- a/Kernel/Bus/USB/USBRequest.h +++ b/Kernel/Bus/USB/USBRequest.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Jesse Buhagiar + * Copyright (c) 2021, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,18 +10,21 @@ #include // -// USB Request directions +// bmRequestType fields // -// As per Section 9.4 of the USB Specification, it is noted that Requeset Types that -// Device to Host have bit 7 of `bmRequestType` set. These are here as a convenience, -// as we construct the request at the call-site to make reading transfers easier. +// As per Section 9.3 of the USB 2.0 Specification. +// Note that while some of these values are zero, there are here for convenience. +// This is because it makes reading the request type easier to read when constructing a USB request. // -static constexpr u8 USB_DEVICE_REQUEST_DEVICE_TO_HOST = 0x80; -static constexpr u8 USB_DEVICE_REQUEST_HOST_TO_DEVICE = 0x00; -static constexpr u8 USB_INTERFACE_REQUEST_DEVICE_TO_HOST = 0x81; -static constexpr u8 USB_INTERFACE_REQUEST_HOST_TO_DEVICE = 0x01; -static constexpr u8 USB_ENDPOINT_REQUEST_DEVICE_TO_HOST = 0x82; -static constexpr u8 USB_ENDPOINT_REQUEST_HOST_TO_DEVICE = 0x02; +static constexpr u8 USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST = 0x80; +static constexpr u8 USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE = 0x00; +static constexpr u8 USB_REQUEST_TYPE_STANDARD = 0x00; +static constexpr u8 USB_REQUEST_TYPE_CLASS = 0x20; +static constexpr u8 USB_REQUEST_TYPE_VENDOR = 0x40; +static constexpr u8 USB_REQUEST_RECIPIENT_DEVICE = 0x00; +static constexpr u8 USB_REQUEST_RECIPIENT_INTERFACE = 0x01; +static constexpr u8 USB_REQUEST_RECIPIENT_ENDPOINT = 0x02; +static constexpr u8 USB_REQUEST_RECIPIENT_OTHER = 0x03; // // Standard USB request types