summaryrefslogtreecommitdiff
path: root/Kernel/Net/NE2000
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2022-12-17 22:11:44 +0200
committerJelle Raaijmakers <jelle@gmta.nl>2023-01-07 12:36:57 +0100
commit0cede94c39bb1ac9ec09925f609a8fe34fa52f66 (patch)
tree2b4ddc77e8bb5b350f46a9463bc148f692325f88 /Kernel/Net/NE2000
parent90ac9d725346cd7add92890c7aaca415f0d2f289 (diff)
downloadserenity-0cede94c39bb1ac9ec09925f609a8fe34fa52f66.zip
Kernel/Net: Introduce a new mechanism to initialize a PCI device
Instead of using a clunky if-statement paradigm, we now have all drivers being declaring two methods for their adapter class - create and probe. These methods are linked in each PCINetworkDriverInitializer structure, in a new s_initializers static list of them. Then, when we probe for a PCI device, we use each probe method and if there's a match, then the corresponding create method is called. After the adapter instance is created, we call the virtual initialize method on it, because many drivers actually require a sort of post-construction initialization sequence to ensure the network adapter can properly function. As a result of this change, it's much more easy to add more drivers and the initialization code is more readable and it's easier to understand when and where things could fail in the whole initialization sequence.
Diffstat (limited to 'Kernel/Net/NE2000')
-rw-r--r--Kernel/Net/NE2000/NetworkAdapter.cpp24
-rw-r--r--Kernel/Net/NE2000/NetworkAdapter.h4
2 files changed, 19 insertions, 9 deletions
diff --git a/Kernel/Net/NE2000/NetworkAdapter.cpp b/Kernel/Net/NE2000/NetworkAdapter.cpp
index 3df63f0be9..8d758926a5 100644
--- a/Kernel/Net/NE2000/NetworkAdapter.cpp
+++ b/Kernel/Net/NE2000/NetworkAdapter.cpp
@@ -138,7 +138,7 @@ struct [[gnu::packed]] received_packet_header {
u16 length;
};
-UNMAP_AFTER_INIT ErrorOr<LockRefPtr<NE2000NetworkAdapter>> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
+UNMAP_AFTER_INIT ErrorOr<bool> NE2000NetworkAdapter::probe(PCI::DeviceIdentifier const& pci_device_identifier)
{
constexpr auto ne2k_ids = Array {
PCI::HardwareID { 0x10EC, 0x8029 }, // RealTek RTL-8029(AS)
@@ -155,19 +155,18 @@ UNMAP_AFTER_INIT ErrorOr<LockRefPtr<NE2000NetworkAdapter>> NE2000NetworkAdapter:
PCI::HardwareID { 0x12c3, 0x5598 }, // Holtek HT80229
PCI::HardwareID { 0x8c4a, 0x1980 }, // Winbond W89C940 (misprogrammed)
};
- if (!ne2k_ids.span().contains_slow(pci_device_identifier.hardware_id()))
- return nullptr;
+ return ne2k_ids.span().contains_slow(pci_device_identifier.hardware_id());
+}
+
+UNMAP_AFTER_INIT ErrorOr<NonnullLockRefPtr<NetworkAdapter>> NE2000NetworkAdapter::create(PCI::DeviceIdentifier const& pci_device_identifier)
+{
u8 irq = pci_device_identifier.interrupt_line().value();
auto interface_name = TRY(NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier));
auto registers_io_window = TRY(IOWindow::create_for_pci_device_bar(pci_device_identifier, PCI::HeaderType0BaseRegister::BAR0));
return TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq, move(registers_io_window), move(interface_name))));
}
-UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<IOWindow> registers_io_window, NonnullOwnPtr<KString> interface_name)
- : NetworkAdapter(move(interface_name))
- , PCI::Device(address)
- , IRQHandler(irq)
- , m_registers_io_window(move(registers_io_window))
+UNMAP_AFTER_INIT ErrorOr<void> NE2000NetworkAdapter::initialize(Badge<NetworkingManagement>)
{
dmesgln_pci(*this, "Found @ {}", pci_address());
@@ -181,6 +180,15 @@ UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address
set_mac_address(m_mac_address);
dmesgln_pci(*this, "MAC address: {}", m_mac_address.to_string());
enable_irq();
+ return {};
+}
+
+UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<IOWindow> registers_io_window, NonnullOwnPtr<KString> interface_name)
+ : NetworkAdapter(move(interface_name))
+ , PCI::Device(address)
+ , IRQHandler(irq)
+ , m_registers_io_window(move(registers_io_window))
+{
}
UNMAP_AFTER_INIT NE2000NetworkAdapter::~NE2000NetworkAdapter() = default;
diff --git a/Kernel/Net/NE2000/NetworkAdapter.h b/Kernel/Net/NE2000/NetworkAdapter.h
index 33328fa0a2..6b73dcd939 100644
--- a/Kernel/Net/NE2000/NetworkAdapter.h
+++ b/Kernel/Net/NE2000/NetworkAdapter.h
@@ -20,7 +20,9 @@ class NE2000NetworkAdapter final : public NetworkAdapter
, public PCI::Device
, public IRQHandler {
public:
- static ErrorOr<LockRefPtr<NE2000NetworkAdapter>> try_to_initialize(PCI::DeviceIdentifier const&);
+ static ErrorOr<bool> probe(PCI::DeviceIdentifier const&);
+ static ErrorOr<NonnullLockRefPtr<NetworkAdapter>> create(PCI::DeviceIdentifier const&);
+ virtual ErrorOr<void> initialize(Badge<NetworkingManagement>) override;
virtual ~NE2000NetworkAdapter() override;