From e3c96aa9f84f1edbecfb52eec7f93db5d077f7a6 Mon Sep 17 00:00:00 2001
From: kieron <kieron@6dbc8c32-bb84-406f-8558-d1cf31a0ab0c>
Date: Thu, 5 Apr 2018 10:42:12 +0000
Subject: [PATCH] Drivers now using DriverManagement.

git-svn-id: https://spexeah.com:8443/svn/Asuro@302 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
---
 src/driver/PCI.pas       |  20 ++++-
 src/driver/USB.pas       |  74 ++++++++++++-----
 src/driver/keyboard.pas  |  21 ++++-
 src/driver/mouse.pas     |  21 ++++-
 src/drivermanagement.pas | 170 ++++++++++++++++++++++++++++++++++++---
 src/kernel.pas           |   2 +
 6 files changed, 270 insertions(+), 38 deletions(-)

diff --git a/src/driver/PCI.pas b/src/driver/PCI.pas
index 6f20864e..aa6a5e48 100644
--- a/src/driver/PCI.pas
+++ b/src/driver/PCI.pas
@@ -75,14 +75,13 @@ function getDeviceInfo(class_code : uint8; subclass_code : uint8; prog_if : uint
 
 implementation 
 
-procedure init();
+function load(ptr : void) : boolean;
 var
     current_bus : uint8;
+
 begin
-    console.writestringln('PCI: INIT BEGIN.');
     console.writestringln('PCI: Scanning Bus: 0');
     scanBus(0);
-
     //while unscanned busses scan busses
     current_bus := 1;
     while true do begin
@@ -93,7 +92,22 @@ begin
             current_bus := current_bus + 1;
         end else break;
     end;
+    load:= true;
+end;
 
+procedure init();
+var
+    DevID : TDeviceIdentifier;
+
+begin
+    console.writestringln('PCI: INIT BEGIN.');
+    DevID.Bus:= biUnknown;
+    DevID.id0:= 0;
+    DevID.id1:= 0;
+    DevID.id2:= 0;
+    DevID.id3:= 0;
+    DevID.ex:= nil;
+    drivermanagement.register_driver_ex('PCI Driver', @DevID, @load, true);
     console.writestringln('PCI: INIT END.');
 end;
 
diff --git a/src/driver/USB.pas b/src/driver/USB.pas
index a4ed9a27..adcd8e9d 100644
--- a/src/driver/USB.pas
+++ b/src/driver/USB.pas
@@ -8,7 +8,8 @@ uses
     drivertypes,
     pmemorymanager,
     vmemorymanager,
-    util;
+    util,
+    drivermanagement;
 
 type
     POHCI_MMR = ^TOHCI_MMR;
@@ -39,7 +40,7 @@ procedure init;
 
 implementation
 
-procedure init;
+function loadOHCI(ptr : void) : boolean;
 var
     devices : TDeviceArray;
     count   : uint32;
@@ -48,25 +49,6 @@ var
     MMR     : POHCI_MMR;
 
 begin
-    console.writestringln('USB: INIT BEGIN.');
-    
-    devices:= PCI.getDeviceInfo($0C, $03, $00, count);
-    console.writestring('USB-UHCI: Found ');
-    console.writeint(count);
-    console.writestringln(' USB Controller(s).');
-    if count > 0 then begin
-        for i:=0 to count-1 do begin
-            console.writestring('USB: Controller[');
-            console.writeint(i);
-            console.writestring(']: ');
-            console.writehex(devices[i].device_id);
-            console.writestring(' ');
-            console.writehex(devices[i].vendor_id);
-            console.writestring(' ');
-            console.writehexln(devices[i].prog_if);
-        end;
-    end;
-
     devices:= PCI.getDeviceInfo($0C, $03, $10, count);
     console.writestring('USB-OHCI: Found ');
     console.writeint(count);
@@ -87,7 +69,57 @@ begin
             MMR:= POHCI_MMR(devices[i].address0);
         end;
     end;
+end;
 
+function loadUHCI(ptr : void) : boolean;
+var
+    devices : TDeviceArray;
+    count   : uint32;
+    i       : uint32;
+
+begin
+    devices:= PCI.getDeviceInfo($0C, $03, $00, count);
+    console.writestring('USB-UHCI: Found ');
+    console.writeint(count);
+    console.writestringln(' USB Controller(s).');
+    if count > 0 then begin
+        for i:=0 to count-1 do begin
+            console.writestring('USB: Controller[');
+            console.writeint(i);
+            console.writestring(']: ');
+            console.writehex(devices[i].device_id);
+            console.writestring(' ');
+            console.writehex(devices[i].vendor_id);
+            console.writestring(' ');
+            console.writehexln(devices[i].prog_if);
+        end;
+    end;
+end;
+
+procedure init;
+var
+    UHCI_ID, OHCI_ID : TDeviceIdentifier;
+
+begin
+    console.writestringln('USB: INIT BEGIN.');
+    
+    UHCI_ID.Bus:= biPCI;
+    UHCI_ID.id0:= idANY;
+    UHCI_ID.id1:= $0000000C;
+    UHCI_ID.id2:= $00000003;
+    UHCI_ID.id3:= $00000000;
+    UHCI_ID.ex:= nil;
+
+    OHCI_ID.Bus:= biPCI;
+    OHCI_ID.id0:= idANY;
+    OHCI_ID.id1:= $0000000C;
+    OHCI_ID.id2:= $00000003;
+    OHCI_ID.id3:= $00000010;
+    OHCI_ID.ex:= nil;
+
+    drivermanagement.register_driver('USB-UHCI Driver', @UHCI_ID, @loadUHCI);
+    drivermanagement.register_driver('USB-OHCI Driver', @OHCI_ID, @loadOHCI);
+    
     console.writestringln('USB: INIT END.');
 end;
 
diff --git a/src/driver/keyboard.pas b/src/driver/keyboard.pas
index d12017bf..2463d032 100644
--- a/src/driver/keyboard.pas
+++ b/src/driver/keyboard.pas
@@ -32,7 +32,6 @@ var
     key_matrix_shift : array [1..256] of TKeyInfo;
     captin_hook      : pp_hook_method = nil;
     is_shift         : boolean = false;      
-    
 
 procedure init(keyboard_layout : array of TKeyInfo);
 procedure hook(proc : pp_hook_method);
@@ -40,6 +39,8 @@ procedure lang_USA();
 
 implementation
 
+uses
+    drivermanagement;
 
 procedure callback(scan_code : void);
 begin
@@ -58,11 +59,27 @@ begin
     if uint8(scan_code) = 170 then is_shift := false;
 end;
 
+function load(ptr : void) : boolean;
+begin
+    isr33.hook(uint32(@callback));
+    console.writestringln('PS/2 KEYBOARD: LOADED.');
+    load:= true;
+end;
+
 procedure init(keyboard_layout : array of TKeyInfo);  
+var
+    devid : TDeviceIdentifier;
+
 begin
     console.writestringln('PS/2 KEYBOARD: INIT BEGIN.');
     if keyboard_layout[1].key_code = 0 then lang_USA();
-    isr33.hook(uint32(@callback));
+    devid.bus:= biUnknown;
+    devid.id0:= 0;
+    devid.id1:= 0;
+    devid.id2:= 0;
+    devid.id3:= 0;
+    devid.ex:= nil;
+    drivermanagement.register_driver_ex('PS/2 Keyboard', @devid, @load, true);
     console.writestringln('PS/2 KEYBOARD: INIT END.');
 end;
 //2A AA
diff --git a/src/driver/mouse.pas b/src/driver/mouse.pas
index 27ac53be..fbe1061d 100644
--- a/src/driver/mouse.pas
+++ b/src/driver/mouse.pas
@@ -16,7 +16,8 @@ uses
     util,
     isr44,
     lmemorymanager,
-    strings;
+    strings,
+    drivermanagement;
 
 type
     PMousePacket = ^TMousePacket;
@@ -103,10 +104,26 @@ begin
     //console.writehexln(DWORD(raw));
 end;
 
+function load(ptr : void) : boolean;
+begin
+    isr44.hook(uint32(@callback));
+    console.writestringln('PS/2 MOUSE: LOADED.');  
+    load:= true;
+end;
+
 procedure init();
+var
+    devid : TDeviceIdentifier;
+
 begin
     console.writestringln('PS/2 MOUSE: INIT BEGIN.');
-    isr44.hook(uint32(@callback));
+    devid.bus:= biUnknown;
+    devid.id0:= 0;
+    devid.id1:= 0;
+    devid.id2:= 0;
+    devid.id3:= 0;
+    devid.ex:= nil;
+    drivermanagement.register_driver_ex('PS/2 Mouse', @devid, @load, true);
     console.writestringln('PS/2 MOUSE: INIT END.');
 end;
 
diff --git a/src/drivermanagement.pas b/src/drivermanagement.pas
index ab5ea443..9fa88af8 100644
--- a/src/drivermanagement.pas
+++ b/src/drivermanagement.pas
@@ -11,7 +11,7 @@ unit drivermanagement;
 interface
 
 uses
-    console, util, strings, lmemorymanager;
+    console, util, strings, lmemorymanager, terminal;
 
 const
     idANY = $FFFFFFFF;
@@ -55,7 +55,9 @@ type
         Next          : PDeviceRegistration;
     end;
 
+procedure init;
 procedure register_driver(Driver_Name : PChar; DeviceID : PDeviceIdentifier; Load_Callback : TDriverLoadCallback);
+procedure register_driver_ex(Driver_Name : PChar; DeviceID : PDeviceIdentifier; Load_Callback : TDriverLoadCallback; force_load : boolean);
 procedure register_device(Device_Name : PChar; DeviceID : PDeviceIdentifier; ptr : void);
 
 var
@@ -64,6 +66,138 @@ var
 
 implementation
 
+procedure writeBusType(Bus : TBusIdentifier);
+begin
+    case Bus of
+        biUnknown : console.writestring('Unknown');
+        biANY     : console.writestring('ANY');
+        bii2c     : console.writestring('i2c');
+        biPCI     : console.writestring('PCI');
+        biPCIe    : console.writestring('PCIe');
+        biUSB     : console.writestring('USB');
+    end;
+end;
+
+{ Terminal Commands }
+
+procedure terminal_command_drivers(Params : PParamList);
+var
+    Drv : PDriverRegistration;
+    ex  : PDevEx;
+    i   : uint32;
+
+begin
+    Drv:= Root;
+    i:= 1;
+    while Drv <> nil do begin
+        if Drv^.Loaded then begin
+            console.writeint(i);
+            console.writestring(') ');
+            console.writestring(Drv^.Driver_Name);
+            console.writestring(' [');
+            writeBusType(Drv^.Identifier^.Bus);
+            console.writestring(' - ID:');
+            console.writeHex(Drv^.Identifier^.id0);
+            console.writestring('-');
+            console.writeHex(Drv^.Identifier^.id1);
+            console.writestring('-');
+            console.writeHex(Drv^.Identifier^.id2);
+            console.writestring('-');
+            console.writeHex(Drv^.Identifier^.id3);
+            ex:= Drv^.Identifier^.ex;
+            while ex <> nil do begin
+                console.writestring('-');
+                console.writeHex(ex^.idN);
+                ex:= ex^.ex;   
+            end;
+            console.writestringln(']');
+            i:= i + 1;
+        end;
+        Drv:= Drv^.Next;
+    end;
+end;
+
+procedure terminal_command_driversex(Params : PParamList);
+var
+    Drv : PDriverRegistration;
+    ex  : PDevEx;
+    i   : uint32;
+
+begin
+    Drv:= Root;
+    i:= 1;
+    while Drv <> nil do begin
+        console.writeint(i);
+        if Drv^.Loaded then console.writestring('L');
+        console.writestring(') ');
+        console.writestring(Drv^.Driver_Name);
+        console.writestring(' [');
+        writeBusType(Drv^.Identifier^.Bus);
+        console.writestring(' - ID:');
+        console.writeHex(Drv^.Identifier^.id0);
+        console.writestring('-');
+        console.writeHex(Drv^.Identifier^.id1);
+        console.writestring('-');
+        console.writeHex(Drv^.Identifier^.id2);
+        console.writestring('-');
+        console.writeHex(Drv^.Identifier^.id3);
+        ex:= Drv^.Identifier^.ex;
+        while ex <> nil do begin
+            console.writestring('-');
+            console.writeHex(ex^.idN);
+            ex:= ex^.ex;   
+        end;
+        console.writestringln(']');
+        i:= i + 1;
+        Drv:= Drv^.Next;
+    end;
+end;
+
+procedure terminal_command_devices(Params : PParamList);
+var
+    Dv : PDeviceRegistration;
+    ex : PDevEx;
+    i  : uint32;
+
+begin
+    Dv:= Dev;
+    i:= 1;
+    while Dv <> nil do begin
+        console.writeint(i);
+        console.writestring(') ');
+        console.writestring(Dv^.Device_Name);
+        console.writestring(' [');
+        writeBusType(Dv^.Identifier^.Bus);
+        console.writestring(' - ID:');
+        console.writeHex(Dv^.Identifier^.id0);
+        console.writestring('-');
+        console.writeHex(Dv^.Identifier^.id1);
+        console.writestring('-');
+        console.writeHex(Dv^.Identifier^.id2);
+        console.writestring('-');
+        console.writeHex(Dv^.Identifier^.id3);
+        ex:= Dv^.Identifier^.ex;
+        while ex <> nil do begin
+            console.writestring('-');
+            console.writeHex(ex^.idN);
+            ex:= ex^.ex;   
+        end;
+        console.writestringln(']');
+        if Dv^.Driver_Loaded then begin
+            console.writestring('   -- Driver Loaded: ');
+            if Dv^.Driver <> nil then begin
+                console.writestringln(Dv^.Driver^.Driver_Name);
+            end else begin
+                console.writestringln('Unknown')
+            end;
+        end;
+        i:= i + 1;
+        Dv:= Dv^.Next; 
+    end;
+end;
+
+{ Main Functions }
+
 function copy_identifier(DeviceID : PDeviceIdentifier) : PDeviceIdentifier;
 var
     New_DevID : PDeviceIdentifier;
@@ -127,7 +261,19 @@ begin
     end;
 end;
 
+procedure init;
+begin
+    terminal.registerCommand('DRIVERSEX', @terminal_command_driversex, 'List all available drivers.');
+    terminal.registerCommand('DRIVERS', @terminal_command_drivers, 'List loaded drivers.');
+    terminal.registerCommand('DEVICES', @terminal_command_devices, 'List devices.');
+end;
+
 procedure register_driver(Driver_Name : PChar; DeviceID : PDeviceIdentifier; Load_Callback : TDriverLoadCallback);
+begin
+    register_driver_ex(Driver_Name, DeviceID, Load_Callback, false);
+end;
+
+procedure register_driver_ex(Driver_Name : PChar; DeviceID : PDeviceIdentifier; Load_Callback : TDriverLoadCallback; force_load : boolean);
 var
     NewReg : PDriverRegistration; 
     RegList : PDriverRegistration;
@@ -149,6 +295,10 @@ begin
         end;
         RegList^.Next:= NewReg;
     end;
+    if force_load then begin
+        NewReg^.Loaded:= True;
+        NewReg^.Driver_Load(nil);
+    end;
 end;
 
 procedure register_device(Device_Name : PChar; DeviceID : PDeviceIdentifier; ptr : void);
@@ -165,6 +315,15 @@ begin
     new_dev^.Driver_Loaded:= false;
     new_dev^.Driver:= nil;
     new_dev^.next:= nil;
+    if Dev = nil then begin
+        Dev:= new_dev;
+    end else begin    
+        dev_list:= Dev;
+        While dev_list^.Next <> nil do begin
+            dev_list:= dev_list^.Next;
+        end;
+        dev_list^.Next:= new_dev;
+    end;
     while drv <> nil do begin
         if identifiers_match(drv^.Identifier, DeviceID) then begin
             if drv^.Driver_Load(ptr) then begin
@@ -176,15 +335,6 @@ begin
         end;
         drv:= drv^.Next;
     end;
-    if Dev = nil then begin
-        Dev:= new_dev;
-    end else begin
-        dev_list:= Dev;
-        While dev_list^.Next <> nil do begin
-            dev_list:= dev_list^.Next;
-        end;
-        dev_list^.Next:= new_dev;
-    end;
 end;
 
 end.
\ No newline at end of file
diff --git a/src/kernel.pas b/src/kernel.pas
index 1b832402..f88c2bd4 100644
--- a/src/kernel.pas
+++ b/src/kernel.pas
@@ -78,6 +78,8 @@ begin
      terminal.init();
      terminal.registerCommand('MEMINFO', @terminal_command_meminfo, 'Print Simple Memory Information.');
 
+     drivermanagement.init();
+
      console.init();
 
      console.writestringln('Booting Asuro...');