From ea585639700e7c365cfea108211ef4a7f8872a12 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 10 Apr 2020 20:33:30 +0300 Subject: [PATCH] Kernel: Instantiate network adapters in their own detect() methods This commit is one step forward for pluggable driver modules. Instead of creating instances of network adapter classes, we let their detect() methods to figure out if there are existing devices to initialize. --- Kernel/Net/E1000NetworkAdapter.cpp | 18 ++++++++++-------- Kernel/Net/E1000NetworkAdapter.h | 2 +- Kernel/Net/RTL8139NetworkAdapter.cpp | 17 +++++++++-------- Kernel/Net/RTL8139NetworkAdapter.h | 2 +- Kernel/PCI/Initializer.cpp | 5 +---- Kernel/init.cpp | 5 +++++ 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Kernel/Net/E1000NetworkAdapter.cpp b/Kernel/Net/E1000NetworkAdapter.cpp index b83beb62b7..e9bf289974 100644 --- a/Kernel/Net/E1000NetworkAdapter.cpp +++ b/Kernel/Net/E1000NetworkAdapter.cpp @@ -139,16 +139,18 @@ namespace Kernel { #define INTERRUPT_TXD_LOW (1 << 15) #define INTERRUPT_SRPD (1 << 16) -void E1000NetworkAdapter::detect(const PCI::Address& address) +void E1000NetworkAdapter::detect() { - if (address.is_null()) - return; static const PCI::ID qemu_bochs_vbox_id = { 0x8086, 0x100e }; - const PCI::ID id = PCI::get_id(address); - if (id != qemu_bochs_vbox_id) - return; - u8 irq = PCI::get_interrupt_line(address); - (void)adopt(*new E1000NetworkAdapter(address, irq)).leak_ref(); + + PCI::enumerate([&](const PCI::Address& address, PCI::ID id) { + if (address.is_null()) + return; + if (id != qemu_bochs_vbox_id) + return; + u8 irq = PCI::get_interrupt_line(address); + (void)adopt(*new E1000NetworkAdapter(address, irq)).leak_ref(); + }); } E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq) diff --git a/Kernel/Net/E1000NetworkAdapter.h b/Kernel/Net/E1000NetworkAdapter.h index 952bc873ea..2d95f58e9b 100644 --- a/Kernel/Net/E1000NetworkAdapter.h +++ b/Kernel/Net/E1000NetworkAdapter.h @@ -38,7 +38,7 @@ namespace Kernel { class E1000NetworkAdapter final : public NetworkAdapter , public PCI::Device { public: - static void detect(const PCI::Address&); + static void detect(); E1000NetworkAdapter(PCI::Address, u8 irq); virtual ~E1000NetworkAdapter() override; diff --git a/Kernel/Net/RTL8139NetworkAdapter.cpp b/Kernel/Net/RTL8139NetworkAdapter.cpp index ec02973f71..442591fb0a 100644 --- a/Kernel/Net/RTL8139NetworkAdapter.cpp +++ b/Kernel/Net/RTL8139NetworkAdapter.cpp @@ -125,16 +125,17 @@ namespace Kernel { #define RX_BUFFER_SIZE 32768 #define TX_BUFFER_SIZE PACKET_SIZE_MAX -void RTL8139NetworkAdapter::detect(const PCI::Address& address) +void RTL8139NetworkAdapter::detect() { - if (address.is_null()) - return; static const PCI::ID rtl8139_id = { 0x10EC, 0x8139 }; - PCI::ID id = PCI::get_id(address); - if (id != rtl8139_id) - return; - u8 irq = PCI::get_interrupt_line(address); - (void)adopt(*new RTL8139NetworkAdapter(address, irq)).leak_ref(); + PCI::enumerate([&](const PCI::Address& address, PCI::ID id) { + if (address.is_null()) + return; + if (id != rtl8139_id) + return; + u8 irq = PCI::get_interrupt_line(address); + (void)adopt(*new RTL8139NetworkAdapter(address, irq)).leak_ref(); + }); } RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq) diff --git a/Kernel/Net/RTL8139NetworkAdapter.h b/Kernel/Net/RTL8139NetworkAdapter.h index 8898be30ab..20921c75d3 100644 --- a/Kernel/Net/RTL8139NetworkAdapter.h +++ b/Kernel/Net/RTL8139NetworkAdapter.h @@ -39,7 +39,7 @@ namespace Kernel { class RTL8139NetworkAdapter final : public NetworkAdapter , public PCI::Device { public: - static void detect(const PCI::Address&); + static void detect(); RTL8139NetworkAdapter(PCI::Address, u8 irq); virtual ~RTL8139NetworkAdapter() override; diff --git a/Kernel/PCI/Initializer.cpp b/Kernel/PCI/Initializer.cpp index 6ae6575520..d31fc61673 100644 --- a/Kernel/PCI/Initializer.cpp +++ b/Kernel/PCI/Initializer.cpp @@ -58,11 +58,8 @@ void initialize() MMIOAccess::initialize(ACPI::Parser::the()->find_table("MCFG")); else IOAccess::initialize(); - - enumerate([&](const Address& address, ID id) { + PCI::enumerate([&](const Address& address, ID id) { klog() << address << " " << id; - E1000NetworkAdapter::detect(address); - RTL8139NetworkAdapter::detect(address); }); } diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 8a5b49046e..142d4d07be 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -58,8 +58,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -190,6 +192,9 @@ void init_stage2() } } + E1000NetworkAdapter::detect(); + RTL8139NetworkAdapter::detect(); + LoopbackAdapter::the(); Syscall::initialize();