1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Refactor hal code to use preferred driver detection order

This commit is contained in:
Achilleas Anagnostopoulos 2017-07-18 08:27:50 +01:00
parent 1ef27b3226
commit b53912757c

View File

@ -9,12 +9,16 @@ import (
"gopheros/device/video/console/logo" "gopheros/device/video/console/logo"
"gopheros/kernel/hal/multiboot" "gopheros/kernel/hal/multiboot"
"gopheros/kernel/kfmt" "gopheros/kernel/kfmt"
"sort"
) )
// managedDevices contains the devices discovered by the HAL. // managedDevices contains the devices discovered by the HAL.
type managedDevices struct { type managedDevices struct {
activeConsole console.Device activeConsole console.Device
activeTTY tty.Device activeTTY tty.Device
// activeDrivers tracks all initialized device drivers.
activeDrivers []device.Driver
} }
var ( var (
@ -30,9 +34,70 @@ func ActiveTTY() tty.Device {
// DetectHardware probes for hardware devices and initializes the appropriate // DetectHardware probes for hardware devices and initializes the appropriate
// drivers. // drivers.
func DetectHardware() { func DetectHardware() {
consoles := probe(console.ProbeFuncs) // Get driver list and sort by detection priority
if len(consoles) != 0 { drivers := device.DriverList()
devices.activeConsole = consoles[0].(console.Device) sort.Sort(drivers)
probe(drivers)
}
// probe executes the probe function for each driver and invokes
// onDriverInit for each successfully initialized driver.
func probe(driverInfoList device.DriverInfoList) {
var w = kfmt.PrefixWriter{Sink: kfmt.GetOutputSink()}
for _, info := range driverInfoList {
drv := info.Probe()
if drv == nil {
continue
}
strBuf.Reset()
major, minor, patch := drv.DriverVersion()
kfmt.Fprintf(&strBuf, "[hal] %s(%d.%d.%d): ", drv.DriverName(), major, minor, patch)
w.Prefix = strBuf.Bytes()
if err := drv.DriverInit(&w); err != nil {
kfmt.Fprintf(&w, "init failed: %s\n", err.Message)
continue
}
kfmt.Fprintf(&w, "initialized\n")
onDriverInit(info, drv)
devices.activeDrivers = append(devices.activeDrivers, drv)
}
}
// onDriverInit is invoked by probe() whenever a piece of hardware is detected
// and successfully initialized.
func onDriverInit(info *device.DriverInfo, drv device.Driver) {
switch drvImpl := drv.(type) {
case console.Device:
onConsoleInit(drvImpl)
case tty.Device:
if devices.activeTTY != nil {
return
}
devices.activeTTY = drvImpl
if devices.activeConsole != nil {
linkTTYToConsole()
}
}
}
// onConsoleInit is invoked whenever a console is initialized. If this is the
// first found console it automatically becomes the active console. In
// addition, if the console supports fonts and/or logos this function ensures
// that they are loaded and attached to the console. Finally, if an active TTY
// device is present, it will be automatically linked to the first active
// console via a call to linkTTYToConsole.
func onConsoleInit(cons console.Device) {
if devices.activeConsole != nil {
return
}
devices.activeConsole = cons
if logoSetter, ok := (devices.activeConsole).(console.LogoSetter); ok { if logoSetter, ok := (devices.activeConsole).(console.LogoSetter); ok {
disableLogo := false disableLogo := false
@ -70,47 +135,19 @@ func DetectHardware() {
fontSetter.SetFont(selFont) fontSetter.SetFont(selFont)
} }
if devices.activeTTY != nil {
linkTTYToConsole()
}
} }
ttys := probe(tty.ProbeFuncs) // linkTTYToConsole connects the active TTY device to the active console device
if len(ttys) != 0 { // and syncs their contents.
devices.activeTTY = ttys[0].(tty.Device) func linkTTYToConsole() {
devices.activeTTY.AttachTo(devices.activeConsole) devices.activeTTY.AttachTo(devices.activeConsole)
kfmt.SetOutputSink(devices.activeTTY) kfmt.SetOutputSink(devices.activeTTY)
// Sync terminal contents with console // Sync terminal contents with console
devices.activeTTY.SetState(tty.StateActive) devices.activeTTY.SetState(tty.StateActive)
}
}
// probe executes the supplied hw probe functions and attempts to initialize
// each detected device. The function returns a list of device drivers that
// were successfully initialized.
func probe(hwProbeFns []device.ProbeFn) []device.Driver {
var (
drivers []device.Driver
w = kfmt.PrefixWriter{Sink: kfmt.GetOutputSink()}
)
for _, probeFn := range hwProbeFns {
drv := probeFn()
if drv == nil {
continue
}
strBuf.Reset()
major, minor, patch := drv.DriverVersion()
kfmt.Fprintf(&strBuf, "[hal] %s(%d.%d.%d): ", drv.DriverName(), major, minor, patch)
w.Prefix = strBuf.Bytes()
if err := drv.DriverInit(&w); err != nil {
kfmt.Fprintf(&w, "init failed: %s\n", err.Message)
continue
}
kfmt.Fprintf(&w, "initialized\n")
drivers = append(drivers, drv)
}
return drivers
} }