1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:27:43 +00:00

Kernel/SysFS: Adapt USB plug code to work with SysFS patterns

This commit is contained in:
Liav A 2022-04-23 09:45:31 +03:00 committed by Andreas Kling
parent 70afa0b171
commit cdab213750
6 changed files with 36 additions and 47 deletions

View file

@ -11,6 +11,7 @@
#include <Kernel/Bus/USB/USBDescriptors.h> #include <Kernel/Bus/USB/USBDescriptors.h>
#include <Kernel/Bus/USB/USBDevice.h> #include <Kernel/Bus/USB/USBDevice.h>
#include <Kernel/Bus/USB/USBRequest.h> #include <Kernel/Bus/USB/USBRequest.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
#include <Kernel/StdLib.h> #include <Kernel/StdLib.h>
namespace Kernel::USB { namespace Kernel::USB {

View file

@ -12,6 +12,10 @@
#include <Kernel/Bus/USB/USBConfiguration.h> #include <Kernel/Bus/USB/USBConfiguration.h>
#include <Kernel/Bus/USB/USBPipe.h> #include <Kernel/Bus/USB/USBPipe.h>
namespace Kernel {
class SysFSUSBDeviceInformation;
}
namespace Kernel::USB { namespace Kernel::USB {
class USBController; class USBController;
@ -22,6 +26,7 @@ class USBConfiguration;
// glues together: // glues together:
// //
// https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf // https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
class Hub;
class Device : public RefCounted<Device> { class Device : public RefCounted<Device> {
public: public:
enum class DeviceSpeed : u8 { enum class DeviceSpeed : u8 {
@ -51,6 +56,8 @@ public:
Vector<USBConfiguration> const& configurations() const { return m_configurations; } Vector<USBConfiguration> const& configurations() const { return m_configurations; }
SysFSUSBDeviceInformation& sysfs_device_info_node(Badge<USB::Hub>) { return *m_sysfs_device_info_node; }
protected: protected:
Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe); Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe);
@ -70,6 +77,9 @@ protected:
private: private:
IntrusiveListNode<Device, NonnullRefPtr<Device>> m_hub_child_node; IntrusiveListNode<Device, NonnullRefPtr<Device>> m_hub_child_node;
protected:
RefPtr<SysFSUSBDeviceInformation> m_sysfs_device_info_node;
public: public:
using List = IntrusiveList<&Device::m_hub_child_node>; using List = IntrusiveList<&Device::m_hub_child_node>;
}; };

View file

@ -45,6 +45,8 @@ ErrorOr<void> Hub::enumerate_and_power_on_hub()
// USBDevice::enumerate_device must be called before this. // USBDevice::enumerate_device must be called before this.
VERIFY(m_address > 0); VERIFY(m_address > 0);
m_sysfs_device_info_node = TRY(SysFSUSBDeviceInformation::create(*this));
if (m_device_descriptor.device_class != USB_CLASS_HUB) { if (m_device_descriptor.device_class != USB_CLASS_HUB) {
dbgln("USB Hub: Trying to enumerate and power on a device that says it isn't a hub."); dbgln("USB Hub: Trying to enumerate and power on a device that says it isn't a hub.");
return EINVAL; return EINVAL;
@ -131,7 +133,7 @@ ErrorOr<void> Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector
void Hub::remove_children_from_sysfs() void Hub::remove_children_from_sysfs()
{ {
for (auto& child : m_children) for (auto& child : m_children)
SysFSUSBBusDirectory::the().unplug(child); SysFSUSBBusDirectory::the().unplug({}, child.sysfs_device_info_node({}));
} }
void Hub::check_for_port_updates() void Hub::check_for_port_updates()
@ -257,10 +259,10 @@ void Hub::check_for_port_updates()
auto hub = hub_or_error.release_value(); auto hub = hub_or_error.release_value();
m_children.append(hub); m_children.append(hub);
SysFSUSBBusDirectory::the().plug(hub); SysFSUSBBusDirectory::the().plug({}, hub->sysfs_device_info_node({}));
} else { } else {
m_children.append(device); m_children.append(device);
SysFSUSBBusDirectory::the().plug(device); SysFSUSBBusDirectory::the().plug({}, device->sysfs_device_info_node({}));
} }
} else { } else {
@ -275,13 +277,12 @@ void Hub::check_for_port_updates()
} }
if (device_to_remove) { if (device_to_remove) {
m_children.remove(*device_to_remove); SysFSUSBBusDirectory::the().unplug({}, device_to_remove->sysfs_device_info_node({}));
SysFSUSBBusDirectory::the().unplug(*device_to_remove);
if (device_to_remove->device_descriptor().device_class == USB_CLASS_HUB) { if (device_to_remove->device_descriptor().device_class == USB_CLASS_HUB) {
auto* hub_child = static_cast<Hub*>(device_to_remove.ptr()); auto* hub_child = static_cast<Hub*>(device_to_remove.ptr());
hub_child->remove_children_from_sysfs(); hub_child->remove_children_from_sysfs();
} }
m_children.remove(*device_to_remove);
} else { } else {
dbgln_if(USB_DEBUG, "USB Hub: No child set up on port {}, ignoring detachment.", port_number); dbgln_if(USB_DEBUG, "USB Hub: No child set up on port {}, ignoring detachment.", port_number);
} }

View file

@ -10,44 +10,27 @@
namespace Kernel { namespace Kernel {
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory; static SysFSUSBBusDirectory* s_sysfs_usb_bus_directory;
RefPtr<SysFSUSBDeviceInformation> SysFSUSBBusDirectory::device_node_for(USB::Device& device) void SysFSUSBBusDirectory::plug(Badge<USB::Hub>, SysFSUSBDeviceInformation& new_device_info_node)
{ {
RefPtr<USB::Device> checked_device = device; MUST(m_child_components.with([&](auto& list) -> ErrorOr<void> {
for (auto& device_node : m_device_nodes) { list.append(new_device_info_node);
if (device_node.device().ptr() == checked_device.ptr()) return {};
return device_node; }));
}
return {};
} }
void SysFSUSBBusDirectory::unplug(Badge<USB::Hub>, SysFSUSBDeviceInformation& removed_device_info_node)
void SysFSUSBBusDirectory::plug(USB::Device& new_device)
{ {
SpinlockLocker lock(m_lock); MUST(m_child_components.with([&](auto& list) -> ErrorOr<void> {
auto device_node = device_node_for(new_device); list.remove(removed_device_info_node);
VERIFY(!device_node); return {};
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() SysFSUSBBusDirectory& SysFSUSBBusDirectory::the()
{ {
VERIFY(s_procfs_usb_bus_directory); VERIFY(s_sysfs_usb_bus_directory);
return *s_procfs_usb_bus_directory; return *s_sysfs_usb_bus_directory;
} }
UNMAP_AFTER_INIT SysFSUSBBusDirectory::SysFSUSBBusDirectory(SysFSBusDirectory& buses_directory) UNMAP_AFTER_INIT SysFSUSBBusDirectory::SysFSUSBBusDirectory(SysFSBusDirectory& buses_directory)
@ -59,7 +42,7 @@ UNMAP_AFTER_INIT void SysFSUSBBusDirectory::initialize()
{ {
auto directory = adopt_ref(*new SysFSUSBBusDirectory(SysFSComponentRegistry::the().buses_directory())); auto directory = adopt_ref(*new SysFSUSBBusDirectory(SysFSComponentRegistry::the().buses_directory()));
SysFSComponentRegistry::the().register_new_bus_directory(directory); SysFSComponentRegistry::the().register_new_bus_directory(directory);
s_procfs_usb_bus_directory = directory; s_sysfs_usb_bus_directory = directory;
} }
ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> SysFSUSBDeviceInformation::create(USB::Device& device) ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> SysFSUSBDeviceInformation::create(USB::Device& device)

View file

@ -6,7 +6,9 @@
#pragma once #pragma once
#include <AK/Badge.h>
#include <Kernel/Bus/USB/USBDevice.h> #include <Kernel/Bus/USB/USBDevice.h>
#include <Kernel/Bus/USB/USBHub.h>
#include <Kernel/FileSystem/SysFS/Component.h> #include <Kernel/FileSystem/SysFS/Component.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h> #include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
#include <Kernel/Locking/Spinlock.h> #include <Kernel/Locking/Spinlock.h>
@ -20,15 +22,11 @@ public:
virtual StringView name() const override { return "usb"sv; } virtual StringView name() const override { return "usb"sv; }
void plug(USB::Device&); void plug(Badge<USB::Hub>, SysFSUSBDeviceInformation&);
void unplug(USB::Device&); void unplug(Badge<USB::Hub>, SysFSUSBDeviceInformation&);
private: private:
explicit SysFSUSBBusDirectory(SysFSBusDirectory&); explicit SysFSUSBBusDirectory(SysFSBusDirectory&);
RefPtr<SysFSUSBDeviceInformation> device_node_for(USB::Device& device);
IntrusiveList<&SysFSUSBDeviceInformation::m_list_node> m_device_nodes;
mutable Spinlock m_lock; mutable Spinlock m_lock;
}; };

View file

@ -23,15 +23,11 @@ public:
static ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> create(USB::Device&); static ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> create(USB::Device&);
virtual StringView name() const override { return m_device_name->view(); } virtual StringView name() const override { return m_device_name->view(); }
RefPtr<USB::Device> device() const { return m_device; }
protected: protected:
SysFSUSBDeviceInformation(NonnullOwnPtr<KString> device_name, USB::Device& device); 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; 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; NonnullRefPtr<USB::Device> m_device;
private: private: