1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +00:00

Kernel/SysFS: Stop cluttering the codebase with pieces of SysFS parts

Instead, start to put everything in one place to resemble the directory
structure of the SysFS when actually using it.
This commit is contained in:
Liav A 2022-04-22 09:44:31 +03:00 committed by Andreas Kling
parent cba4750921
commit 290eb53cb5
22 changed files with 26 additions and 26 deletions

View file

@ -0,0 +1,139 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Bus/PCI/API.h>
#include <Kernel/Bus/PCI/Access.h>
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.h>
#include <Kernel/Sections.h>
namespace Kernel::PCI {
UNMAP_AFTER_INIT NonnullRefPtr<PCIDeviceSysFSDirectory> PCIDeviceSysFSDirectory::create(SysFSDirectory const& parent_directory, Address address)
{
// FIXME: Handle allocation failure gracefully
auto device_name = MUST(KString::formatted("{:04x}:{:02x}:{:02x}.{}", address.domain(), address.bus(), address.device(), address.function()));
return adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(move(device_name), parent_directory, address));
}
UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(NonnullOwnPtr<KString> device_directory_name, SysFSDirectory const& parent_directory, Address address)
: SysFSDirectory(parent_directory)
, m_address(address)
, m_device_directory_name(move(device_directory_name))
{
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::VENDOR_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::DEVICE_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::CLASS, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBCLASS, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::REVISION_ID, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::PROG_IF, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR0, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR1, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR2, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR3, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR4, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR5, 4));
}
UNMAP_AFTER_INIT void PCIBusSysFSDirectory::initialize()
{
auto pci_directory = adopt_ref(*new (nothrow) PCIBusSysFSDirectory());
SysFSComponentRegistry::the().register_new_bus_directory(pci_directory);
}
UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory()
: SysFSDirectory(SysFSComponentRegistry::the().buses_directory())
{
MUST(PCI::enumerate([&](DeviceIdentifier const& device_identifier) {
auto pci_device = PCI::PCIDeviceSysFSDirectory::create(*this, device_identifier.address());
m_components.append(pci_device);
}));
}
StringView PCIDeviceAttributeSysFSComponent::name() const
{
switch (m_offset) {
case PCI::RegisterOffset::VENDOR_ID:
return "vendor"sv;
case PCI::RegisterOffset::DEVICE_ID:
return "device_id"sv;
case PCI::RegisterOffset::CLASS:
return "class"sv;
case PCI::RegisterOffset::SUBCLASS:
return "subclass"sv;
case PCI::RegisterOffset::REVISION_ID:
return "revision"sv;
case PCI::RegisterOffset::PROG_IF:
return "progif"sv;
case PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID:
return "subsystem_vendor"sv;
case PCI::RegisterOffset::SUBSYSTEM_ID:
return "subsystem_id"sv;
case PCI::RegisterOffset::BAR0:
return "bar0"sv;
case PCI::RegisterOffset::BAR1:
return "bar1"sv;
case PCI::RegisterOffset::BAR2:
return "bar2"sv;
case PCI::RegisterOffset::BAR3:
return "bar3"sv;
case PCI::RegisterOffset::BAR4:
return "bar4"sv;
case PCI::RegisterOffset::BAR5:
return "bar5"sv;
default:
VERIFY_NOT_REACHED();
}
}
NonnullRefPtr<PCIDeviceAttributeSysFSComponent> PCIDeviceAttributeSysFSComponent::create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width)
{
return adopt_ref(*new (nothrow) PCIDeviceAttributeSysFSComponent(device, offset, field_bytes_width));
}
PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width)
: SysFSComponent()
, m_device(device)
, m_offset(offset)
, m_field_bytes_width(field_bytes_width)
{
}
ErrorOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
{
auto blob = TRY(try_to_generate_buffer());
if ((size_t)offset >= blob->size())
return 0;
ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
TRY(buffer.write(blob->data() + offset, nread));
return nread;
}
ErrorOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
{
OwnPtr<KString> value;
switch (m_field_bytes_width) {
case 1:
value = TRY(KString::formatted("{:#x}", PCI::read8(m_device->address(), m_offset)));
break;
case 2:
value = TRY(KString::formatted("{:#x}", PCI::read16(m_device->address(), m_offset)));
break;
case 4:
value = TRY(KString::formatted("{:#x}", PCI::read32(m_device->address(), m_offset)));
break;
default:
VERIFY_NOT_REACHED();
}
return KBuffer::try_create_with_bytes(value->view().bytes());
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <Kernel/Bus/PCI/Definitions.h>
#include <Kernel/FileSystem/SysFS.h>
namespace Kernel::PCI {
class PCIBusSysFSDirectory final : public SysFSDirectory {
public:
static void initialize();
virtual StringView name() const override { return "pci"sv; }
private:
PCIBusSysFSDirectory();
};
class PCIDeviceSysFSDirectory final : public SysFSDirectory {
public:
static NonnullRefPtr<PCIDeviceSysFSDirectory> create(SysFSDirectory const&, Address);
Address const& address() const { return m_address; }
virtual StringView name() const override { return m_device_directory_name->view(); }
private:
PCIDeviceSysFSDirectory(NonnullOwnPtr<KString> device_directory_name, SysFSDirectory const&, Address);
Address m_address;
NonnullOwnPtr<KString> m_device_directory_name;
};
class PCIDeviceAttributeSysFSComponent : public SysFSComponent {
public:
static NonnullRefPtr<PCIDeviceAttributeSysFSComponent> create(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width);
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override;
virtual ~PCIDeviceAttributeSysFSComponent() {};
virtual StringView name() const override;
protected:
ErrorOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
PCIDeviceAttributeSysFSComponent(PCIDeviceSysFSDirectory const& device, PCI::RegisterOffset offset, size_t field_bytes_width);
NonnullRefPtr<PCIDeviceSysFSDirectory> m_device;
PCI::RegisterOffset m_offset;
size_t m_field_bytes_width;
};
}

View file

@ -0,0 +1,223 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2022, Jesse Buhagiar <jesse.buhagiar@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/JsonArraySerializer.h>
#include <AK/JsonObjectSerializer.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h>
#include <Kernel/KBufferBuilder.h>
namespace Kernel::USB {
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device)
: SysFSComponent()
, m_device(device)
, m_device_name(move(device_name))
{
}
SysFSUSBDeviceInformation::~SysFSUSBDeviceInformation() = default;
ErrorOr<void> SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
{
VERIFY(m_lock.is_locked());
auto array = TRY(JsonArraySerializer<>::try_create(builder));
auto obj = TRY(array.add_object());
TRY(obj.add("device_address", m_device->address()));
TRY(obj.add("usb_spec_compliance_bcd", m_device->device_descriptor().usb_spec_compliance_bcd));
TRY(obj.add("device_class", m_device->device_descriptor().device_class));
TRY(obj.add("device_sub_class", m_device->device_descriptor().device_sub_class));
TRY(obj.add("device_protocol", m_device->device_descriptor().device_protocol));
TRY(obj.add("max_packet_size", m_device->device_descriptor().max_packet_size));
TRY(obj.add("vendor_id", m_device->device_descriptor().vendor_id));
TRY(obj.add("product_id", m_device->device_descriptor().product_id));
TRY(obj.add("device_release_bcd", m_device->device_descriptor().device_release_bcd));
TRY(obj.add("manufacturer_id_descriptor_index", m_device->device_descriptor().manufacturer_id_descriptor_index));
TRY(obj.add("product_string_descriptor_index", m_device->device_descriptor().product_string_descriptor_index));
TRY(obj.add("serial_number_descriptor_index", m_device->device_descriptor().serial_number_descriptor_index));
TRY(obj.add("num_configurations", m_device->device_descriptor().num_configurations));
TRY(obj.add("length", m_device->device_descriptor().descriptor_header.length));
TRY(obj.add("descriptor_type", m_device->device_descriptor().descriptor_header.descriptor_type));
auto configuration_array = TRY(obj.add_array("configurations"));
for (auto const& configuration : m_device->configurations()) {
auto configuration_object = TRY(configuration_array.add_object());
auto const& configuration_descriptor = configuration.descriptor();
TRY(configuration_object.add("length", configuration_descriptor.descriptor_header.length));
TRY(configuration_object.add("descriptor_type", configuration_descriptor.descriptor_header.descriptor_type));
TRY(configuration_object.add("total_length", configuration_descriptor.total_length));
TRY(configuration_object.add("number_of_interfaces", configuration_descriptor.number_of_interfaces));
TRY(configuration_object.add("attributes_bitmap", configuration_descriptor.attributes_bitmap));
TRY(configuration_object.add("max_power", configuration_descriptor.max_power_in_ma));
auto interface_array = TRY(configuration_object.add_array("interfaces"));
for (auto const& interface : configuration.interfaces()) {
auto interface_object = TRY(interface_array.add_object());
auto const& interface_descriptor = interface.descriptor();
TRY(interface_object.add("length", interface_descriptor.descriptor_header.length));
TRY(interface_object.add("descriptor_type", interface_descriptor.descriptor_header.descriptor_type));
TRY(interface_object.add("interface_number", interface_descriptor.interface_id));
TRY(interface_object.add("alternate_setting", interface_descriptor.alternate_setting));
TRY(interface_object.add("num_endpoints", interface_descriptor.number_of_endpoints));
TRY(interface_object.add("interface_class_code", interface_descriptor.interface_class_code));
TRY(interface_object.add("interface_sub_class_code", interface_descriptor.interface_sub_class_code));
TRY(interface_object.add("interface_protocol", interface_descriptor.interface_protocol));
TRY(interface_object.add("interface_string_desc_index", interface_descriptor.interface_string_descriptor_index));
auto endpoint_array = TRY(interface_object.add_array("endpoints"));
for (auto const& endpoint : interface.endpoints()) {
auto endpoint_object = TRY(endpoint_array.add_object());
TRY(endpoint_object.add("length", endpoint.descriptor_header.length));
TRY(endpoint_object.add("descriptor_length", endpoint.descriptor_header.descriptor_type));
TRY(endpoint_object.add("endpoint_address", endpoint.endpoint_address));
TRY(endpoint_object.add("attribute_bitmap", endpoint.endpoint_attributes_bitmap));
TRY(endpoint_object.add("max_packet_size", endpoint.max_packet_size));
TRY(endpoint_object.add("polling_interval", endpoint.poll_interval_in_frames));
TRY(endpoint_object.finish());
}
TRY(endpoint_array.finish());
TRY(interface_object.finish());
}
TRY(interface_array.finish());
TRY(configuration_object.finish());
}
TRY(configuration_array.finish());
TRY(obj.finish());
TRY(array.finish());
return {};
}
ErrorOr<void> SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const
{
MutexLocker lock(m_lock);
auto& cached_data = description.data();
if (!cached_data) {
cached_data = TRY(adopt_nonnull_own_or_enomem(new (nothrow) SysFSInodeData));
}
auto builder = TRY(KBufferBuilder::try_create());
TRY(const_cast<SysFSUSBDeviceInformation&>(*this).try_generate(builder));
auto& typed_cached_data = static_cast<SysFSInodeData&>(*cached_data);
typed_cached_data.buffer = builder.build();
if (!typed_cached_data.buffer)
return ENOMEM;
return {};
}
ErrorOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
{
dbgln_if(PROCFS_DEBUG, "SysFSUSBDeviceInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
VERIFY(offset >= 0);
VERIFY(buffer.user_or_kernel_ptr());
if (!description)
return Error::from_errno(EIO);
MutexLocker locker(m_lock);
if (!description->data()) {
dbgln("SysFSUSBDeviceInformation: Do not have cached data!");
return Error::from_errno(EIO);
}
auto& typed_cached_data = static_cast<SysFSInodeData&>(*description->data());
auto& data_buffer = typed_cached_data.buffer;
if (!data_buffer || (size_t)offset >= data_buffer->size())
return 0;
ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
TRY(buffer.write(data_buffer->data() + offset, nread));
return nread;
}
ErrorOr<void> SysFSUSBBusDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
SpinlockLocker lock(m_lock);
// Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory.
VERIFY(m_parent_directory);
TRY(callback({ ".", { fsid, component_index() }, 0 }));
TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
for (auto const& device_node : m_device_nodes) {
InodeIdentifier identifier = { fsid, device_node.component_index() };
TRY(callback({ device_node.name(), identifier, 0 }));
}
return {};
}
RefPtr<SysFSComponent> SysFSUSBBusDirectory::lookup(StringView name)
{
SpinlockLocker lock(m_lock);
for (auto& device_node : m_device_nodes) {
if (device_node.name() == name) {
return device_node;
}
}
return {};
}
RefPtr<SysFSUSBDeviceInformation> SysFSUSBBusDirectory::device_node_for(USB::Device& device)
{
RefPtr<USB::Device> checked_device = device;
for (auto& device_node : m_device_nodes) {
if (device_node.device().ptr() == checked_device.ptr())
return device_node;
}
return {};
}
void SysFSUSBBusDirectory::plug(USB::Device& new_device)
{
SpinlockLocker lock(m_lock);
auto device_node = device_node_for(new_device);
VERIFY(!device_node);
auto sysfs_usb_device_or_error = SysFSUSBDeviceInformation::create(new_device);
if (sysfs_usb_device_or_error.is_error()) {
dbgln("Failed to create SysFSUSBDevice for device id {}", new_device.address());
return;
}
m_device_nodes.append(sysfs_usb_device_or_error.release_value());
}
void SysFSUSBBusDirectory::unplug(USB::Device& deleted_device)
{
SpinlockLocker lock(m_lock);
auto device_node = device_node_for(deleted_device);
VERIFY(device_node);
device_node->m_list_node.remove();
}
SysFSUSBBusDirectory& SysFSUSBBusDirectory::the()
{
VERIFY(s_procfs_usb_bus_directory);
return *s_procfs_usb_bus_directory;
}
UNMAP_AFTER_INIT SysFSUSBBusDirectory::SysFSUSBBusDirectory(SysFSBusDirectory& buses_directory)
: SysFSDirectory(buses_directory)
{
}
UNMAP_AFTER_INIT void SysFSUSBBusDirectory::initialize()
{
auto directory = adopt_ref(*new SysFSUSBBusDirectory(SysFSComponentRegistry::the().buses_directory()));
SysFSComponentRegistry::the().register_new_bus_directory(directory);
s_procfs_usb_bus_directory = directory;
}
ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> SysFSUSBDeviceInformation::create(USB::Device& device)
{
auto device_name = TRY(KString::number(device.address()));
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSUSBDeviceInformation(move(device_name), device));
}
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/Bus/USB/USBDevice.h>
#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/KBufferBuilder.h>
#include <Kernel/Locking/Mutex.h>
namespace Kernel::USB {
class SysFSUSBDeviceInformation : public SysFSComponent {
friend class SysFSUSBBusDirectory;
public:
virtual ~SysFSUSBDeviceInformation() override;
static ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> create(USB::Device&);
virtual StringView name() const override { return m_device_name->view(); }
RefPtr<USB::Device> device() const { return m_device; }
protected:
SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device);
virtual ErrorOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override;
IntrusiveListNode<SysFSUSBDeviceInformation, RefPtr<SysFSUSBDeviceInformation>> m_list_node;
NonnullRefPtr<USB::Device> m_device;
private:
ErrorOr<void> try_generate(KBufferBuilder&);
virtual ErrorOr<void> refresh_data(OpenFileDescription& description) const override;
mutable Mutex m_lock { "SysFSUSBDeviceInformation" };
NonnullOwnPtr<KString> m_device_name;
};
class SysFSUSBBusDirectory final : public SysFSDirectory {
public:
static void initialize();
static SysFSUSBBusDirectory& the();
virtual StringView name() const override { return "usb"sv; }
void plug(USB::Device&);
void unplug(USB::Device&);
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
private:
explicit SysFSUSBBusDirectory(SysFSBusDirectory&);
RefPtr<SysFSUSBDeviceInformation> device_node_for(USB::Device& device);
IntrusiveList<&SysFSUSBDeviceInformation::m_list_node> m_device_nodes;
mutable Spinlock m_lock;
};
}