Compare commits

...
Author SHA1 Message Date
t3hn3rd 547251dbd0 update: Kernel.pas changes to align with USB implementation
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
- Kernel.pas updated to init USB devices & check for hotplug.
- USB Poll removed given USB stack is now interrupt driven.
2026-03-02 15:53:40 +00:00
t3hn3rd 7bbcc44c5e Feature: USB - Hotplug, mass storage, disconnect callbacks, dynamic device lists
- 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
2026-03-02 15:52:49 +00:00
t3hn3rd 25475a88a7 featur: USB - Add device tracking, stall recovery, removal, and terminal command
- 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
2026-03-02 15:50:55 +00:00
t3hn3rd db49c29627 fix: lowercase names for units
- PCI.pas -> pci.pas
2026-03-02 13:38:10 +00:00
t3hn3rd c237553508 fix: ps2_keyboard report key-up events for break codes
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
2026-03-02 13:37:29 +00:00
t3hn3rd 0c96d32754 feature: move all USB HC drivers from polling to interrupt-driven completion
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
2026-03-02 13:36:40 +00:00
t3hn3rd 3f83f279d7 XHCI driver implementation
- Beginnings of a functional XHCI driver implemented.
2026-03-02 12:24:02 +00:00
t3hn3rd 82fe9fa6df EHCI Implementation
- Beginnings of a functional EHCI driver implemented.
2026-03-02 12:23:35 +00:00
t3hn3rd 2fa77d8295 Merge branch 'feature/tracer-optimizations' into feature/USB 2026-03-01 21:26:56 +00:00
t3hn3rd cb7144f282 USB Stack & HID Refactor
- 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
2026-03-01 21:23:33 +00:00
26 changed files with 12398 additions and 1146 deletions
+1 -1
View File
@@ -4,4 +4,4 @@ echo "======================="
echo " "
echo "Compiling FPC Sources..."
echo " "
fpc -Aelf -gw -g -gl -n -v0ew -O3 -OpPENTIUM3 -Si -Sc -Sg -Xd -CX -XXs -CfSSE -CfSSE2 -Rintel -Pi386 -Tlinux -FElib/ -Fusrc/* -Fusrc/driver/* -Fusrc/driver/net/* src/kernel.pas
fpc -Aelf -gw -g -gl -n -v0ew -O3 -OpPENTIUM3 -Si -Sc -Sg -Xd -CX -XXs -CfSSE -CfSSE2 -Rintel -Pi386 -Tlinux -FElib/ -Fusrc/* -Fusrc/driver/* -Fusrc/driver/net/* -Fusrc/driver/bus/* -Fusrc/driver/bus/usb/* -Fusrc/driver/hid/* src/kernel.pas
-65
View File
@@ -1,65 +0,0 @@
// Copyright 2021 Kieron Morris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
{
Driver->Bus->EHCI - Enhanced Host Controller Interface Driver.
@author(Kieron Morris <[email protected]>)
}
unit EHCI;
interface
uses
tracer,
syslog,
PCI,
drivertypes,
pmemorymanager,
vmemorymanager,
util,
drivermanagement;
function load : boolean;
implementation
function load : boolean;
var
devices : TDeviceArray;
count : uint32;
i : uint32;
begin
tracer.push_trace('EHCI.load');
devices:= PCI.getDeviceInfo($0C, $03, $20, count);
syslog.log('USB-EHCI Driver', 'Found ');
syslog.writeint(count);
syslog.writestringln(' USB Controller(s).');
if count > 0 then begin
for i:=0 to count-1 do begin
syslog.log('USB-EHCI Driver', 'Controller[');
syslog.writeint(i);
syslog.writestring(']: ');
syslog.writehex(devices[i].device_id);
syslog.writestring(' ');
syslog.writehex(devices[i].vendor_id);
syslog.writestring(' ');
syslog.writehexln(devices[i].prog_if);
end;
end;
load:= true;
end;
end.
-96
View File
@@ -1,96 +0,0 @@
// Copyright 2021 Kieron Morris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
{
Driver->Bus->OHCI - Open Host Controller Interface Driver.
@author(Kieron Morris <[email protected]>)
}
unit OHCI;
interface
uses
tracer,
syslog,
PCI,
drivertypes,
pmemorymanager,
vmemorymanager,
util,
drivermanagement;
type
POHCI_MMR = ^TOHCI_MMR;
TOHCI_MMR = packed record
HcRevision : uint32;
HcControl : uint32;
HcCommandStatus : uint32;
HcIntStatus : uint32;
HcIntEnable : uint32;
HcIntDisable : uint32;
HcHCCA : uint32;
HcPeriodCurrentED : uint32;
HcControlHeadED : uint32;
HcControlCurrentED : uint32;
HcBulkHeadED : uint32;
HcBulkCurrentED : uint32;
HcDoneHead : uint32;
HcFmRemaining : uint32;
HcFmNumber : uint32;
HcPeriodicStart : uint32;
HcLSThreshold : uint32;
HcRhDescriptorA : uint32;
HcRhDescriptorB : uint32;
HcRhStatus : uint32;
end;
function load : boolean;
implementation
function load : boolean;
var
devices : TDeviceArray;
count : uint32;
i : uint32;
block : uint32;
MMR : POHCI_MMR;
begin
tracer.push_trace('OHCI.load');
devices:= PCI.getDeviceInfo($0C, $03, $10, count);
syslog.log('USB-OHCI Driver', 'Found ');
syslog.writeint(count);
syslog.writestringln(' USB Controller(s).');
if count > 0 then begin
for i:=0 to count-1 do begin
syslog.log('USB-OHCI Driver', 'Controller[');
syslog.writeint(i);
syslog.writestring(']: ');
syslog.writehex(devices[i].device_id);
syslog.writestring(' ');
syslog.writehex(devices[i].vendor_id);
syslog.writestring(' ');
syslog.writehexln(devices[i].prog_if);
block:= devices[i].address0 SHR 22;
force_alloc_block(block, 0);
map_page(block, block);
MMR:= POHCI_MMR(devices[i].address0);
end;
end;
load:= true;
end;
end.
+1 -1
View File
@@ -18,7 +18,7 @@
@author(Aaron Hance <[email protected]>)
@author(Kieron Morris <[email protected]>)
}
unit PCI;
unit pci;
interface
-65
View File
@@ -1,65 +0,0 @@
// Copyright 2021 Kieron Morris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
{
Driver->Bus->UHCI - Universal Host Controller Interface Driver.
@author(Kieron Morris <[email protected]>)
}
unit UHCI;
interface
uses
tracer,
syslog,
PCI,
drivertypes,
pmemorymanager,
vmemorymanager,
util,
drivermanagement;
function load : boolean;
implementation
function load : boolean;
var
devices : TDeviceArray;
count : uint32;
i : uint32;
begin
tracer.push_trace('UHCI.load');
devices:= PCI.getDeviceInfo($0C, $03, $00, count);
syslog.log('USB-UHCI Driver','Found ');
syslog.writeint(count);
syslog.writestringln(' USB Controller(s).');
if count > 0 then begin
for i:=0 to count-1 do begin
syslog.log('USB-UHCI Driver','Controller[');
syslog.writeint(i);
syslog.writestring(']: ');
syslog.writehex(devices[i].device_id);
syslog.writestring(' ');
syslog.writehex(devices[i].vendor_id);
syslog.writestring(' ');
syslog.writehexln(devices[i].prog_if);
end;
end;
load:= true;
end;
end.
-65
View File
@@ -1,65 +0,0 @@
// Copyright 2021 Kieron Morris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
{
Driver->Bus->XHCI - eXtensible Host Controller Interface Driver.
@author(Kieron Morris <[email protected]>)
}
unit XHCI;
interface
uses
tracer,
syslog,
PCI,
drivertypes,
pmemorymanager,
vmemorymanager,
util,
drivermanagement;
function load : boolean;
implementation
function load : boolean;
var
devices : TDeviceArray;
count : uint32;
i : uint32;
begin
tracer.push_trace('XHCI.load');
devices:= PCI.getDeviceInfo($0C, $03, $30, count);
syslog.log('USB-XHCI Driver', 'Found ');
syslog.writeint(count);
syslog.writestringln(' USB Controller(s).');
if count > 0 then begin
for i:=0 to count-1 do begin
syslog.log('USB-XHCI Driver', 'Controller[');
syslog.writeint(i);
syslog.writestring(']: ');
syslog.writehex(devices[i].device_id);
syslog.writestring(' ');
syslog.writehex(devices[i].vendor_id);
syslog.writestring(' ');
syslog.writehexln(devices[i].prog_if);
end;
end;
load:= true;
end;
end.
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -17,7 +17,7 @@
@author(Kieron Morris <kjm@kieronmorris.me>)
}
unit USB;
unit usb;
interface
@@ -30,6 +30,12 @@ uses
vmemorymanager,
util,
drivermanagement,
usbtypes,
usbcore,
usbhub,
usb_keyboard,
usb_mouse,
usb_storage,
OHCI, UHCI, EHCI, XHCI;
procedure init;
@@ -40,24 +46,28 @@ function loadXHCI(ptr : void) : boolean;
begin
push_trace('USB.loadXHCI');
loadXHCI:= XHCI.load;
pop_trace;
end;
function loadEHCI(ptr : void) : boolean;
begin
push_trace('USB.loadEHCI');
loadEHCI:= EHCI.load;
pop_trace;
end;
function loadOHCI(ptr : void) : boolean;
begin
push_trace('USB.loadOHCI');
loadOHCI:= OHCI.load;
pop_trace;
end;
function loadUHCI(ptr : void) : boolean;
begin
push_trace('USB.loadUHCI');
loadUHCI:= UHCI.load;
pop_trace;
end;
procedure init;
@@ -70,6 +80,19 @@ var
begin
push_trace('USB.init');
syslog.logln('USB Driver', 'INIT BEGIN.');
{ Initialize USB core (HC list, etc.) }
usbcore.init;
{ Initialize hub driver (registers class driver for $09) }
usbhub.init;
{ Initialize HID class drivers }
usb_keyboard.init;
usb_mouse.init;
{ Initialize storage class driver }
usb_storage.init;
UHCI_ID.Bus:= biPCI;
UHCI_ID.id0:= idANY;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+108 -309
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More