Files
serenity/Kernel/Firmware/DeviceTree/Device.h
Sönke Holz 9f47b4b490 Kernel: Introduce a new model for writing devicetree drivers
Devicetree drivers are created by using the `DEVICETREE_DRIVER` macro.
That macro creates a new class deriving from `DeviceTree::Driver` and
inserts it into the driver init section.
The driver code then has to implement the `probe` member function, which
will be called if `DeviceTree::Management` finds a node with a
compatible property entry that was in the array passed as the second
argument to the `DEVICETREE_DRIVER` macro.
The `probe` function then will check if it supports the given node and
if so, registers a `DeviceTree::DeviceRecipe` at the appropriate
subsystem, which will then create a device from that recipe once it is
initialized.
The driver can store the necessary info it got from the devicetree,
such as the physical address and interrupt numbers, in the capture
list of the callback lambda stored in `DeviceRecipe::create_device`.

The `DeviceTree::DeviceRecipe`s are necessary, as the `probe` functions
might not be able to create an instance of the actual device class,
since doing so can depend on some subsystems being initialized first.
2024-11-02 13:53:10 -04:00

47 lines
1.0 KiB
C++

/*
* Copyright (c) 2024, Sönke Holz <sholz8530@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/Noncopyable.h>
#include <LibDeviceTree/DeviceTree.h>
namespace Kernel::DeviceTree {
class Driver;
class Management;
class Device {
AK_MAKE_NONCOPYABLE(Device);
AK_MAKE_DEFAULT_MOVABLE(Device);
public:
Device(::DeviceTree::DeviceTreeNodeView const& node, StringView node_name)
: m_node(&node)
, m_node_name(node_name)
{
}
::DeviceTree::DeviceTreeNodeView const& node() const { return *m_node; }
StringView node_name() const { return m_node_name; }
Driver const* driver() const { return m_driver; }
void set_driver(Badge<Management>, Driver const& driver)
{
VERIFY(m_driver == nullptr);
m_driver = &driver;
}
private:
// This needs to be a pointer for the class to be movable.
::DeviceTree::DeviceTreeNodeView const* m_node;
StringView m_node_name;
Driver const* m_driver { nullptr };
};
}