diff options
author | Liav A <liavalb@gmail.com> | 2022-04-22 09:44:31 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-06-17 11:01:27 +0200 |
commit | 290eb53cb531021f41f30cd4abd38f8665d37460 (patch) | |
tree | 05ed20b8c87ec2ff2a175bff0b077eee83bec3a3 /Kernel/Bus/USB | |
parent | cba47509215c2fa7b4cdb08a19ecf31788b9d567 (diff) | |
download | serenity-290eb53cb531021f41f30cd4abd38f8665d37460.zip |
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.
Diffstat (limited to 'Kernel/Bus/USB')
-rw-r--r-- | Kernel/Bus/USB/SysFSUSB.cpp | 223 | ||||
-rw-r--r-- | Kernel/Bus/USB/SysFSUSB.h | 65 | ||||
-rw-r--r-- | Kernel/Bus/USB/USBHub.cpp | 2 | ||||
-rw-r--r-- | Kernel/Bus/USB/USBManagement.cpp | 2 |
4 files changed, 2 insertions, 290 deletions
diff --git a/Kernel/Bus/USB/SysFSUSB.cpp b/Kernel/Bus/USB/SysFSUSB.cpp deleted file mode 100644 index 568bda9b6a..0000000000 --- a/Kernel/Bus/USB/SysFSUSB.cpp +++ /dev/null @@ -1,223 +0,0 @@ -/* - * 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/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)); -} - -} diff --git a/Kernel/Bus/USB/SysFSUSB.h b/Kernel/Bus/USB/SysFSUSB.h deleted file mode 100644 index bf7163dd76..0000000000 --- a/Kernel/Bus/USB/SysFSUSB.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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; -}; - -} diff --git a/Kernel/Bus/USB/USBHub.cpp b/Kernel/Bus/USB/USBHub.cpp index f5684858fc..c2c94df961 100644 --- a/Kernel/Bus/USB/USBHub.cpp +++ b/Kernel/Bus/USB/USBHub.cpp @@ -5,11 +5,11 @@ */ #include <Kernel/Arch/x86/IO.h> -#include <Kernel/Bus/USB/SysFSUSB.h> #include <Kernel/Bus/USB/USBClasses.h> #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/StdLib.h> namespace Kernel::USB { diff --git a/Kernel/Bus/USB/USBManagement.cpp b/Kernel/Bus/USB/USBManagement.cpp index 8b59a1a4e3..5130654fd8 100644 --- a/Kernel/Bus/USB/USBManagement.cpp +++ b/Kernel/Bus/USB/USBManagement.cpp @@ -6,10 +6,10 @@ #include <AK/Singleton.h> #include <Kernel/Bus/PCI/API.h> -#include <Kernel/Bus/USB/SysFSUSB.h> #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/Sections.h> namespace Kernel::USB { |