mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +00:00
Kernel/SysFS: Split bulky SysFSUSB file into two separate class files
This commit is contained in:
parent
290eb53cb5
commit
e488245234
7 changed files with 145 additions and 116 deletions
|
@ -9,7 +9,7 @@
|
||||||
#include <Kernel/Bus/USB/USBController.h>
|
#include <Kernel/Bus/USB/USBController.h>
|
||||||
#include <Kernel/Bus/USB/USBHub.h>
|
#include <Kernel/Bus/USB/USBHub.h>
|
||||||
#include <Kernel/Bus/USB/USBRequest.h>
|
#include <Kernel/Bus/USB/USBRequest.h>
|
||||||
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h>
|
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h>
|
||||||
#include <Kernel/StdLib.h>
|
#include <Kernel/StdLib.h>
|
||||||
|
|
||||||
namespace Kernel::USB {
|
namespace Kernel::USB {
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <Kernel/Bus/USB/UHCI/UHCIController.h>
|
#include <Kernel/Bus/USB/UHCI/UHCIController.h>
|
||||||
#include <Kernel/Bus/USB/USBManagement.h>
|
#include <Kernel/Bus/USB/USBManagement.h>
|
||||||
#include <Kernel/CommandLine.h>
|
#include <Kernel/CommandLine.h>
|
||||||
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h>
|
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h>
|
||||||
#include <Kernel/Sections.h>
|
#include <Kernel/Sections.h>
|
||||||
|
|
||||||
namespace Kernel::USB {
|
namespace Kernel::USB {
|
||||||
|
@ -67,7 +67,7 @@ bool USBManagement::initialized()
|
||||||
UNMAP_AFTER_INIT void USBManagement::initialize()
|
UNMAP_AFTER_INIT void USBManagement::initialize()
|
||||||
{
|
{
|
||||||
if (!s_initialized_sys_fs_directory) {
|
if (!s_initialized_sys_fs_directory) {
|
||||||
USB::SysFSUSBBusDirectory::initialize();
|
SysFSUSBBusDirectory::initialize();
|
||||||
s_initialized_sys_fs_directory = true;
|
s_initialized_sys_fs_directory = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,8 @@ set(KERNEL_SOURCES
|
||||||
FileSystem/SysFS.cpp
|
FileSystem/SysFS.cpp
|
||||||
FileSystem/SysFS/Component.cpp
|
FileSystem/SysFS/Component.cpp
|
||||||
FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp
|
FileSystem/SysFS/Subsystems/Bus/PCI/SysFSPCI.cpp
|
||||||
FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.cpp
|
FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
|
||||||
|
FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp
|
||||||
FileSystem/SysFS/Subsystems/Firmware/BIOS.cpp
|
FileSystem/SysFS/Subsystems/Firmware/BIOS.cpp
|
||||||
FileSystem/SysFS/Subsystems/Firmware/Directory.cpp
|
FileSystem/SysFS/Subsystems/Firmware/Directory.cpp
|
||||||
FileSystem/SysFS/Subsystems/Firmware/PowerStateSwitch.cpp
|
FileSystem/SysFS/Subsystems/Firmware/PowerStateSwitch.cpp
|
||||||
|
|
96
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
Normal file
96
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h>
|
||||||
|
#include <Kernel/KBufferBuilder.h>
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
38
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h
Normal file
38
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021-2022, 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/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
|
||||||
|
#include <Kernel/Locking/Spinlock.h>
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
* Copyright (c) 2021-2022, Liav A. <liavalb@hotmail.co.il>
|
||||||
* Copyright (c) 2022, Jesse Buhagiar <jesse.buhagiar@serenityos.org>
|
* Copyright (c) 2022, Jesse Buhagiar <jesse.buhagiar@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
@ -7,12 +7,9 @@
|
||||||
|
|
||||||
#include <AK/JsonArraySerializer.h>
|
#include <AK/JsonArraySerializer.h>
|
||||||
#include <AK/JsonObjectSerializer.h>
|
#include <AK/JsonObjectSerializer.h>
|
||||||
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h>
|
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
|
||||||
#include <Kernel/KBufferBuilder.h>
|
|
||||||
|
|
||||||
namespace Kernel::USB {
|
namespace Kernel {
|
||||||
|
|
||||||
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
|
|
||||||
|
|
||||||
SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device)
|
SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device)
|
||||||
: SysFSComponent()
|
: SysFSComponent()
|
||||||
|
@ -138,86 +135,4 @@ ErrorOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count
|
||||||
return 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
* Copyright (c) 2021-2022, Liav A. <liavalb@hotmail.co.il>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -9,9 +9,10 @@
|
||||||
#include <Kernel/Bus/USB/USBDevice.h>
|
#include <Kernel/Bus/USB/USBDevice.h>
|
||||||
#include <Kernel/FileSystem/SysFS.h>
|
#include <Kernel/FileSystem/SysFS.h>
|
||||||
#include <Kernel/KBufferBuilder.h>
|
#include <Kernel/KBufferBuilder.h>
|
||||||
|
#include <Kernel/KString.h>
|
||||||
#include <Kernel/Locking/Mutex.h>
|
#include <Kernel/Locking/Mutex.h>
|
||||||
|
|
||||||
namespace Kernel::USB {
|
namespace Kernel {
|
||||||
|
|
||||||
class SysFSUSBDeviceInformation : public SysFSComponent {
|
class SysFSUSBDeviceInformation : public SysFSComponent {
|
||||||
friend class SysFSUSBBusDirectory;
|
friend class SysFSUSBBusDirectory;
|
||||||
|
@ -40,26 +41,4 @@ private:
|
||||||
NonnullOwnPtr<KString> m_device_name;
|
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue