diff options
-rw-r--r-- | Kernel/Bus/USB/USBHub.cpp | 2 | ||||
-rw-r--r-- | Kernel/Bus/USB/USBManagement.cpp | 4 | ||||
-rw-r--r-- | Kernel/CMakeLists.txt | 3 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp | 96 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h | 38 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp (renamed from Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.cpp) | 91 | ||||
-rw-r--r-- | Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h (renamed from Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h) | 27 |
7 files changed, 145 insertions, 116 deletions
diff --git a/Kernel/Bus/USB/USBHub.cpp b/Kernel/Bus/USB/USBHub.cpp index c2c94df961..8376bb1a4f 100644 --- a/Kernel/Bus/USB/USBHub.cpp +++ b/Kernel/Bus/USB/USBHub.cpp @@ -9,7 +9,7 @@ #include <Kernel/Bus/USB/USBController.h> #include <Kernel/Bus/USB/USBHub.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> namespace Kernel::USB { diff --git a/Kernel/Bus/USB/USBManagement.cpp b/Kernel/Bus/USB/USBManagement.cpp index 5130654fd8..3bb784e659 100644 --- a/Kernel/Bus/USB/USBManagement.cpp +++ b/Kernel/Bus/USB/USBManagement.cpp @@ -9,7 +9,7 @@ #include <Kernel/Bus/USB/UHCI/UHCIController.h> #include <Kernel/Bus/USB/USBManagement.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> namespace Kernel::USB { @@ -67,7 +67,7 @@ bool USBManagement::initialized() UNMAP_AFTER_INIT void USBManagement::initialize() { if (!s_initialized_sys_fs_directory) { - USB::SysFSUSBBusDirectory::initialize(); + SysFSUSBBusDirectory::initialize(); s_initialized_sys_fs_directory = true; } diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 13c75f1ae0..b386093a00 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -144,7 +144,8 @@ set(KERNEL_SOURCES FileSystem/SysFS.cpp FileSystem/SysFS/Component.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/Directory.cpp FileSystem/SysFS/Subsystems/Firmware/PowerStateSwitch.cpp diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp new file mode 100644 index 0000000000..6b25377019 --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp @@ -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)); +} + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h new file mode 100644 index 0000000000..440be5474c --- /dev/null +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h @@ -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; +}; + +} diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.cpp b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp index 361cb7ea22..e411fba780 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.cpp +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp @@ -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> * * SPDX-License-Identifier: BSD-2-Clause @@ -7,12 +7,9 @@ #include <AK/JsonArraySerializer.h> #include <AK/JsonObjectSerializer.h> -#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h> -#include <Kernel/KBufferBuilder.h> +#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h> -namespace Kernel::USB { - -static SysFSUSBBusDirectory* s_procfs_usb_bus_directory; +namespace Kernel { SysFSUSBDeviceInformation::SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device) : SysFSComponent() @@ -138,86 +135,4 @@ ErrorOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count 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)); -} - } diff --git a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h index bf7163dd76..ca9dc94d28 100644 --- a/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/SysFSUSB.h +++ b/Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h @@ -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 */ @@ -9,9 +9,10 @@ #include <Kernel/Bus/USB/USBDevice.h> #include <Kernel/FileSystem/SysFS.h> #include <Kernel/KBufferBuilder.h> +#include <Kernel/KString.h> #include <Kernel/Locking/Mutex.h> -namespace Kernel::USB { +namespace Kernel { class SysFSUSBDeviceInformation : public SysFSComponent { friend class SysFSUSBBusDirectory; @@ -40,26 +41,4 @@ private: 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; -}; - } |