From 4cd9252ff6d52bc149cbd1dd01284fbd222b93d4 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 15 Jul 2024 11:48:48 +0300 Subject: [PATCH] Kernel: Fix USB code Fix USB Keyboard scan code table for bottom row of keyboard Support multiple interfaces on a single USB device Add usb mouse to default qemu settings --- kernel/include/kernel/USB/Device.h | 3 +-- kernel/kernel/USB/Device.cpp | 8 ++++---- kernel/kernel/USB/HID/HIDDriver.cpp | 13 +++++++++---- kernel/kernel/USB/HID/Keyboard.cpp | 8 ++++---- script/qemu.sh | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/kernel/include/kernel/USB/Device.h b/kernel/include/kernel/USB/Device.h index 0cb9fedb9e..dc03ebc2fd 100644 --- a/kernel/include/kernel/USB/Device.h +++ b/kernel/include/kernel/USB/Device.h @@ -81,8 +81,7 @@ namespace Kernel DeviceDescriptor m_descriptor; BAN::UniqPtr m_dma_buffer; - // FIXME: support more than one interface from a configuration - BAN::UniqPtr m_class_driver; + BAN::Vector> m_class_drivers; }; } diff --git a/kernel/kernel/USB/Device.cpp b/kernel/kernel/USB/Device.cpp index 4aedebf669..e069e6cff4 100644 --- a/kernel/kernel/USB/Device.cpp +++ b/kernel/kernel/USB/Device.cpp @@ -154,7 +154,7 @@ namespace Kernel break; case USB::InterfaceBaseClass::HID: if (auto result = USBHIDDriver::create(*this, interface, j); !result.is_error()) - m_class_driver = result.release_value(); + TRY(m_class_drivers.push_back(result.release_value())); break; case USB::InterfaceBaseClass::Physical: dprintln_if(DEBUG_USB, "Found Physical interface"); @@ -218,7 +218,7 @@ namespace Kernel break; } - if (m_class_driver) + if (!m_class_drivers.empty()) { dprintln("Successfully initialized USB interface"); return {}; @@ -309,8 +309,8 @@ namespace Kernel void USBDevice::handle_input_data(BAN::ConstByteSpan data, uint8_t endpoint_id) { - if (m_class_driver) - m_class_driver->handle_input_data(data, endpoint_id); + for (auto& driver : m_class_drivers) + driver->handle_input_data(data, endpoint_id); } USB::SpeedClass USBDevice::determine_speed_class(uint64_t bits_per_second) diff --git a/kernel/kernel/USB/HID/HIDDriver.cpp b/kernel/kernel/USB/HID/HIDDriver.cpp index bfa604cdd8..360263c5f7 100644 --- a/kernel/kernel/USB/HID/HIDDriver.cpp +++ b/kernel/kernel/USB/HID/HIDDriver.cpp @@ -6,6 +6,7 @@ #include #define DEBUG_HID 0 +#define DUMP_HID_REPORT 0 namespace Kernel { @@ -57,7 +58,7 @@ namespace Kernel using namespace USBHID; -#if DEBUG_HID +#if DUMP_HID_REPORT static void dump_hid_collection(const Collection& collection, size_t indent); #endif @@ -211,7 +212,7 @@ namespace Kernel return BAN::Error::from_errno(EFAULT); } -#if DEBUG_HID +#if DUMP_HID_REPORT { SpinLockGuard _(Debug::s_debug_lock); dump_hid_collection(collection, 0); @@ -222,9 +223,11 @@ namespace Kernel { case 0x02: m_hid_device = TRY(BAN::RefPtr::create()); + dprintln("Initialized an USB Mouse"); break; case 0x06: m_hid_device = TRY(BAN::RefPtr::create()); + dprintln("Initialized an USB Keyboard"); break; default: dwarnln("Unsupported generic descript page usage 0x{2H}", collection.usage_id); @@ -326,7 +329,9 @@ namespace Kernel void USBHIDDriver::handle_input_data(BAN::ConstByteSpan data, uint8_t endpoint_id) { - ASSERT(m_endpoint_id == endpoint_id); + // If this packet is not for us, skip it + if (m_endpoint_id != endpoint_id) + return; if constexpr(DEBUG_HID) { @@ -672,7 +677,7 @@ namespace Kernel return result.release_value(); } -#if DEBUG_HID +#if DUMP_HID_REPORT static void print_indent(size_t indent) { for (size_t i = 0; i < indent; i++) diff --git a/kernel/kernel/USB/HID/Keyboard.cpp b/kernel/kernel/USB/HID/Keyboard.cpp index 428bfe6f6a..9c38af010f 100644 --- a/kernel/kernel/USB/HID/Keyboard.cpp +++ b/kernel/kernel/USB/HID/Keyboard.cpp @@ -155,10 +155,10 @@ namespace Kernel s_scancode_to_keycode[0x37] = keycode_normal(3, 10); s_scancode_to_keycode[0x38] = keycode_normal(3, 11); s_scancode_to_keycode[0xE5] = keycode_normal(3, 12); - s_scancode_to_keycode[0xE0] = keycode_normal(4, 1); - s_scancode_to_keycode[0xE3] = keycode_normal(4, 2); - s_scancode_to_keycode[0xE2] = keycode_normal(4, 3); - s_scancode_to_keycode[0x2C] = keycode_normal(4, 4); + s_scancode_to_keycode[0xE0] = keycode_normal(4, 0); + s_scancode_to_keycode[0xE3] = keycode_normal(4, 1); + s_scancode_to_keycode[0xE2] = keycode_normal(4, 2); + s_scancode_to_keycode[0x2C] = keycode_normal(4, 3); s_scancode_to_keycode[0xE6] = keycode_normal(4, 5); s_scancode_to_keycode[0xE4] = keycode_normal(4, 6); diff --git a/script/qemu.sh b/script/qemu.sh index b5202a2988..ad4ac749e0 100755 --- a/script/qemu.sh +++ b/script/qemu.sh @@ -31,6 +31,6 @@ qemu-system-$QEMU_ARCH \ -drive format=raw,id=disk,file=${BANAN_DISK_IMAGE_PATH},if=none \ -device e1000e,netdev=net \ -netdev user,id=net \ - -device qemu-xhci -device usb-kbd \ + -device qemu-xhci -device usb-kbd -device usb-mouse \ $DISK_ARGS \ $@ \