Merge feature/USB into develop #13

Merged
admin merged 10 commits from feature/USB into develop 2026-03-02 19:14:07 +00:00
Owner

Full USB stack implementation with interrupt-driven host controllers,
HID class drivers, mass storage, hotplug support, and HID layer
restructuring.

USB Host Controllers:

  • UHCI, OHCI, EHCI, XHCI drivers moved to src/driver/bus/usb/ with
    full initialization, transfer queuing, and interrupt-driven completion
  • Each HC registers a PCI ISR, acknowledges hardware status in the
    handler, and uses a re-entrancy guard to prevent ISR/poll races
  • Hardware interrupt handlers on all four HC types

USB Core & Hub:

  • New usbcore unit: device enumeration, control/bulk transfers with
    timer-based timeouts, completion hook system, stall recovery
    (usb_clear_halt), device tracking via per-HC linked lists,
    usb_remove_device cleanup, hotplug detection (usb_check_hotplug)
  • New usbtypes unit: shared descriptors, constants, HC abstraction,
    aligned allocation helpers
  • New usbhub unit: hub descriptor parsing, port power/reset/status

USB Class Drivers:

  • usb_keyboard: USB HID boot keyboard with HID-to-ASCII translation,
    completion-hook-driven polling, 3-strike self-deactivation, disconnect
    handler, dynamic device list
  • usb_mouse: USB HID boot mouse with delta/scroll/button processing,
    completion-hook-driven polling, disconnect handler, dynamic device list
  • usb_storage: USB Mass Storage BOT driver with CBW/CSW transport,
    SCSI INQUIRY/READ_CAPACITY/READ10/WRITE10, storagemanagement
    integration via read/write callbacks, disconnect handler

HID Layer Restructuring:

  • keyboard.pas stripped to abstract API (hook + reportKeyEvent),
    removing PS/2 dependency
  • mouse.pas absorbed mousestate.pas; now owns position, buttons,
    scroll, and hooks directly
  • Moved ps2_keyboard.pas and ps2_mouse.pas units under
    /driver/hid/ps2/
  • PS/2 keyboard fix: properly report key-up events for break codes
    (bit 7 detection, modifier release handling)

Hotplug & Kernel Integration:

  • Disconnect callbacks (fnDisconnect) on TUSBDevice for safe unplug
  • HotplugArmed/PortChangePending flags for deferred ISR-context hotplug
    (OHCI RHSC, EHCI PCD, xHCI PSC)
  • 'USB' terminal command showing HC info, devices, speeds, and endpoints
Full USB stack implementation with interrupt-driven host controllers, HID class drivers, mass storage, hotplug support, and HID layer restructuring. **USB Host Controllers:** - UHCI, OHCI, EHCI, XHCI drivers moved to src/driver/bus/usb/ with full initialization, transfer queuing, and interrupt-driven completion - Each HC registers a PCI ISR, acknowledges hardware status in the handler, and uses a re-entrancy guard to prevent ISR/poll races - Hardware interrupt handlers on all four HC types **USB Core & Hub:** - New usbcore unit: device enumeration, control/bulk transfers with timer-based timeouts, completion hook system, stall recovery (usb_clear_halt), device tracking via per-HC linked lists, usb_remove_device cleanup, hotplug detection (usb_check_hotplug) - New usbtypes unit: shared descriptors, constants, HC abstraction, aligned allocation helpers - New usbhub unit: hub descriptor parsing, port power/reset/status **USB Class Drivers:** - usb_keyboard: USB HID boot keyboard with HID-to-ASCII translation, completion-hook-driven polling, 3-strike self-deactivation, disconnect handler, dynamic device list - usb_mouse: USB HID boot mouse with delta/scroll/button processing, completion-hook-driven polling, disconnect handler, dynamic device list - usb_storage: USB Mass Storage BOT driver with CBW/CSW transport, SCSI INQUIRY/READ_CAPACITY/READ10/WRITE10, storagemanagement integration via read/write callbacks, disconnect handler **HID Layer Restructuring:** - keyboard.pas stripped to abstract API (hook + reportKeyEvent), removing PS/2 dependency - mouse.pas absorbed mousestate.pas; now owns position, buttons, scroll, and hooks directly - Moved ps2_keyboard.pas and ps2_mouse.pas units under /driver/hid/ps2/ - PS/2 keyboard fix: properly report key-up events for break codes (bit 7 detection, modifier release handling) **Hotplug & Kernel Integration:** - Disconnect callbacks (fnDisconnect) on TUSBDevice for safe unplug - HotplugArmed/PortChangePending flags for deferred ISR-context hotplug (OHCI RHSC, EHCI PCD, xHCI PSC) - 'USB' terminal command showing HC info, devices, speeds, and endpoints
admin added 10 commits 2026-03-02 19:09:42 +00:00
- New units: usbtypes, usbcore, usbhub, UHCI, OHCI, usb_keyboard,
  usb_mouse
- usbtypes: shared USB descriptors, constants, HC abstraction,
  aligned alloc, unit tests
- usb_keyboard: USB HID boot keyboard driver with HID-to-ASCII
  translation, polling, unit tests
- usb_mouse: USB HID boot mouse driver with delta/scroll/button
  processing, polling, unit tests

HID Layer Restructure:
- keyboard.pas: stripped to abstract API (hook + reportKeyEvent),
  no PS/2 dependency
- mouse.pas: absorbed mousestate.pas, now owns
  position/buttons/scroll/hooks directly
- mousestate.pas: deleted (merged into mouse.pas)
- New ps2_keyboard.pas: PS/2 scan code translation,
  calls keyboard.reportKeyEvent
- New ps2_mouse.pas: PS/2 packet handling, calls
  mouse.setMousePos/fireMouseEvent

LVGL Integration:
- lvgl.pas: updated to use mouse.pas directly (was mousestate),
  improved keyboard input handling with proper key held/release tracking
- vterminal.pas: removed serial debug logging, dropped serial dependency

Kernel:
- kernel.pas: added USB units, USB polling in main loop, unit test
  calls, ps2_keyboard/ps2_mouse init
- Beginnings of a functional EHCI driver implemented.
- Beginnings of a functional XHCI driver implemented.
Replace main-loop polling (poll_all, poll_keyboards, poll_mice) with
hardware interrupt handlers for all four USB host controller drivers.

Each HC driver (XHCI, EHCI, OHCI, UHCI) now:
- Registers an ISR on its PCI interrupt line via isrmanager
- Enables hardware interrupts after HC initialization
- Acknowledges interrupt status in the ISR before calling poll,
  preventing interrupt storms on level-triggered PCI lines
- Uses a PollBusy re-entrancy guard to prevent ISR/inline poll races

usbcore gains a completion hook system (register_completion_hook /
fire_completion_hooks) so HID drivers (keyboard, mouse) are notified
directly from the ISR after transfer completion.

Files changed:
- usbcore.pas: completion hook infrastructure
- XHCI.pas: ISR, enable_interrupts, submit ordering fix
- EHCI.pas: ISR, enable_interrupts, status ack in ISR
- OHCI.pas: ISR, enable_interrupts, status ack in ISR
- UHCI.pas: ISR, enable_interrupts, status ack in ISR
- usb_keyboard.pas: register completion hook
- usb_mouse.pas: register completion hook
The PS/2 keyboard callback hardcoded is_down_code := true for every
scancode, so key releases were never reported. This caused keys to
repeat endlessly until the next key was pressed.

In PS/2 scancode set 1, bit 7 indicates a break (release) code.
The callback now:
- Detects break codes via (scancode AND $80)
- Masks to base code (scancode AND $7F) for key matrix lookup
- Sets is_down_code := false for break codes
- Updates modifier state (shift/ctrl/alt) before building the
  key event, so modifier releases take effect immediately

Files changed:
- ps2_keyboard.pas: rewrite callback to handle make/break codes
- PCI.pas -> pci.pas
- Track enumerated devices per-HC via linked list (hc^.Devices)
- Add usb_clear_halt() for endpoint stall recovery (CLEAR_FEATURE)
- Add usb_remove_device() to clean up and free device records
- Add get_hc() for indexed host controller access
- Implement 'USB' terminal command showing HC info, connected devices,
  speeds, class descriptors, and endpoint details
- Add disconnect callback (fnDisconnect) to TUSBDevice for safe unplug handling
- Add HotplugArmed/PortChangePending flags to TUSBHCDriver for deferred
  hotplug processing from ISR context (OHCI RHSC, EHCI PCD, xHCI PSC)
- Implement usb_rescan_port() and usb_check_hotplug() in usbcore
- Add usb_bulk_transfer_wait() with timer-based timeout (bios_data_area)
- Add timer-based timeout to usb_control_msg polling loop
- Replace static device arrays with dynamic linked lists in class drivers:
  usb_keyboard (KeyboardList), usb_mouse (MouseList), usb_storage (StorageList)
- Add 3-strike fail-count self-deactivation in keyboard/mouse poll loops
- Add unload() disconnect handlers for keyboard, mouse, and storage drivers
- Implement USB Mass Storage BOT driver (usb_storage.pas):
  CBW/CSW transport, SCSI INQUIRY/READ_CAPACITY/READ10/WRITE10,
  storagemanagement integration with read/write callbacks
- xHCI: acknowledge port status change W1C bits in event handler
- Integrate usb_check_hotplug + poll_keyboards/poll_mice into kernel main loop
update: Kernel.pas changes to align with USB implementation
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
547251dbd0
- Kernel.pas updated to init USB devices & check for hotplug.
- USB Poll removed given USB stack is now interrupt driven.
admin merged commit 431e5bc8f4 into develop 2026-03-02 19:14:07 +00:00
Sign in to join this conversation.