Kernel/USB: Move USB bus information from /proc to /sys

This patch moves all the USB data from /proc/bus/usb to /sys/bus/usb.
This commit is contained in:
Andreas Kling 2021-07-18 00:49:53 +02:00
parent 72a5347f91
commit b975a74a1d
8 changed files with 93 additions and 80 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Jesse Buhagiar <jooster669@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -12,9 +12,9 @@
#include <Kernel/Bus/USB/USBRequest.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Debug.h>
#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/KBufferBuilder.h>
#include <Kernel/Process.h>
#include <Kernel/ProcessExposed.h>
#include <Kernel/Sections.h>
#include <Kernel/StdLib.h>
#include <Kernel/Time/TimeManagement.h>
@ -69,29 +69,29 @@ static constexpr u16 UHCI_PORTSC_SUSPEND = 0x1000;
static constexpr u8 UHCI_NUMBER_OF_ISOCHRONOUS_TDS = 128;
static constexpr u16 UHCI_NUMBER_OF_FRAMES = 1024;
class ProcFSUSBBusDirectory;
static ProcFSUSBBusDirectory* s_procfs_usb_bus_directory;
class SysFSUSBBusDirectory;
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
class ProcFSUSBDeviceInformation : public ProcFSGlobalInformation {
friend class ProcFSUSBBusDirectory;
class SysFSUSBDeviceInformation : public SysFSComponent {
friend class SysFSUSBBusDirectory;
public:
virtual ~ProcFSUSBDeviceInformation() override {};
virtual ~SysFSUSBDeviceInformation() override {};
static NonnullRefPtr<ProcFSUSBDeviceInformation> create(USB::Device&);
static NonnullRefPtr<SysFSUSBDeviceInformation> create(USB::Device&);
RefPtr<USB::Device> device() const { return m_device; }
protected:
explicit ProcFSUSBDeviceInformation(USB::Device& device)
: ProcFSGlobalInformation(String::formatted("{}", device.address()))
explicit SysFSUSBDeviceInformation(USB::Device& device)
: SysFSComponent(String::number(device.address()))
, m_device(device)
{
}
virtual bool output(KBufferBuilder& builder) override
{
VERIFY(m_device); // Something has gone very wrong if this isn't true
virtual KResultOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription*) const override
{
KBufferBuilder builder;
JsonArraySerializer array { builder };
auto obj = array.add_object();
@ -109,14 +109,24 @@ protected:
obj.add("num_configurations", m_device->device_descriptor().num_configurations);
obj.finish();
array.finish();
return true;
auto data = builder.build();
if (!data)
return ENOMEM;
ssize_t nread = min(static_cast<off_t>(data->size() - offset), static_cast<off_t>(count));
if (!buffer.write(data->data() + offset, nread))
return EFAULT;
return nread;
}
IntrusiveListNode<ProcFSUSBDeviceInformation, RefPtr<ProcFSUSBDeviceInformation>> m_list_node;
RefPtr<USB::Device> m_device;
IntrusiveListNode<SysFSUSBDeviceInformation, RefPtr<SysFSUSBDeviceInformation>> m_list_node;
NonnullRefPtr<USB::Device> m_device;
};
class ProcFSUSBBusDirectory final : public ProcFSExposedDirectory {
friend class ProcFSComponentsRegistrar;
class SysFSUSBBusDirectory final : public SysFSDirectory {
friend class SysFSComponentsRegistrar;
public:
static void initialize();
@ -124,25 +134,24 @@ public:
void unplug(USB::Device&);
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<ProcFSExposedComponent> lookup(StringView name) override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
private:
ProcFSUSBBusDirectory(const ProcFSBusDirectory&);
explicit SysFSUSBBusDirectory(SysFSBusDirectory&);
RefPtr<ProcFSUSBDeviceInformation> device_node_for(USB::Device& device);
RefPtr<SysFSUSBDeviceInformation> device_node_for(USB::Device& device);
IntrusiveList<ProcFSUSBDeviceInformation, RefPtr<ProcFSUSBDeviceInformation>, &ProcFSUSBDeviceInformation::m_list_node> m_device_nodes;
IntrusiveList<SysFSUSBDeviceInformation, RefPtr<SysFSUSBDeviceInformation>, &SysFSUSBDeviceInformation::m_list_node> m_device_nodes;
mutable SpinLock<u8> m_lock;
};
KResult ProcFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
{
ScopedSpinLock lock(m_lock);
auto parent_directory = m_parent_directory.strong_ref();
// Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory.
VERIFY(parent_directory);
VERIFY(m_parent_directory);
callback({ ".", { fsid, component_index() }, 0 });
callback({ "..", { fsid, parent_directory->component_index() }, 0 });
callback({ "..", { fsid, m_parent_directory->component_index() }, 0 });
for (auto& device_node : m_device_nodes) {
InodeIdentifier identifier = { fsid, device_node.component_index() };
@ -150,7 +159,7 @@ KResult ProcFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<boo
}
return KSuccess;
}
RefPtr<ProcFSExposedComponent> ProcFSUSBBusDirectory::lookup(StringView name)
RefPtr<SysFSComponent> SysFSUSBBusDirectory::lookup(StringView name)
{
ScopedSpinLock lock(m_lock);
for (auto& device_node : m_device_nodes) {
@ -161,7 +170,7 @@ RefPtr<ProcFSExposedComponent> ProcFSUSBBusDirectory::lookup(StringView name)
return {};
}
RefPtr<ProcFSUSBDeviceInformation> ProcFSUSBBusDirectory::device_node_for(USB::Device& device)
RefPtr<SysFSUSBDeviceInformation> SysFSUSBBusDirectory::device_node_for(USB::Device& device)
{
RefPtr<USB::Device> checked_device = device;
for (auto& device_node : m_device_nodes) {
@ -171,14 +180,15 @@ RefPtr<ProcFSUSBDeviceInformation> ProcFSUSBBusDirectory::device_node_for(USB::D
return {};
}
void ProcFSUSBBusDirectory::plug(USB::Device& new_device)
void SysFSUSBBusDirectory::plug(USB::Device& new_device)
{
ScopedSpinLock lock(m_lock);
auto device_node = device_node_for(new_device);
VERIFY(!device_node);
m_device_nodes.append(ProcFSUSBDeviceInformation::create(new_device));
m_device_nodes.append(SysFSUSBDeviceInformation::create(new_device));
}
void ProcFSUSBBusDirectory::unplug(USB::Device& deleted_device)
void SysFSUSBBusDirectory::unplug(USB::Device& deleted_device)
{
ScopedSpinLock lock(m_lock);
auto device_node = device_node_for(deleted_device);
@ -186,21 +196,21 @@ void ProcFSUSBBusDirectory::unplug(USB::Device& deleted_device)
device_node->m_list_node.remove();
}
UNMAP_AFTER_INIT ProcFSUSBBusDirectory::ProcFSUSBBusDirectory(const ProcFSBusDirectory& buses_directory)
: ProcFSExposedDirectory("usb"sv, buses_directory)
UNMAP_AFTER_INIT SysFSUSBBusDirectory::SysFSUSBBusDirectory(SysFSBusDirectory& buses_directory)
: SysFSDirectory("usb"sv, buses_directory)
{
}
UNMAP_AFTER_INIT void ProcFSUSBBusDirectory::initialize()
UNMAP_AFTER_INIT void SysFSUSBBusDirectory::initialize()
{
auto directory = adopt_ref(*new ProcFSUSBBusDirectory(ProcFSComponentRegistry::the().buses_directory()));
ProcFSComponentRegistry::the().register_new_bus_directory(directory);
auto directory = adopt_ref(*new SysFSUSBBusDirectory(SysFSComponentRegistry::the().buses_directory()));
SysFSComponentRegistry::the().register_new_bus_directory(directory);
s_procfs_usb_bus_directory = directory;
}
NonnullRefPtr<ProcFSUSBDeviceInformation> ProcFSUSBDeviceInformation::create(USB::Device& device)
NonnullRefPtr<SysFSUSBDeviceInformation> SysFSUSBDeviceInformation::create(USB::Device& device)
{
return adopt_ref(*new ProcFSUSBDeviceInformation(device));
return adopt_ref(*new SysFSUSBDeviceInformation(device));
}
UHCIController& UHCIController::the()
@ -215,7 +225,7 @@ UNMAP_AFTER_INIT void UHCIController::detect()
// FIXME: We create the /proc/bus/usb representation here, but it should really be handled
// in a more broad singleton than this once we refactor things in USB subsystem.
ProcFSUSBBusDirectory::initialize();
SysFSUSBBusDirectory::initialize();
PCI::enumerate([&](const PCI::Address& address, PCI::ID id) {
if (address.is_null())

View file

@ -36,17 +36,6 @@ UNMAP_AFTER_INIT ProcFSComponentRegistry::ProcFSComponentRegistry()
{
}
const ProcFSBusDirectory& ProcFSComponentRegistry::buses_directory() const
{
return *m_root_directory->m_buses_directory;
}
void ProcFSComponentRegistry::register_new_bus_directory(ProcFSExposedDirectory& new_bus_directory)
{
VERIFY(!m_root_directory->m_buses_directory.is_null());
m_root_directory->m_buses_directory->m_components.append(new_bus_directory);
}
void ProcFSComponentRegistry::register_new_process(Process& new_process)
{
Locker locker(m_lock);

View file

@ -56,6 +56,9 @@ KResult SysFSRootDirectory::traverse_as_directory(unsigned fsid, Function<bool(F
SysFSRootDirectory::SysFSRootDirectory()
: SysFSDirectory(".")
{
auto buses_directory = SysFSBusDirectory::must_create(*this);
m_components.append(buses_directory);
m_buses_directory = buses_directory;
}
NonnullRefPtr<SysFS> SysFS::create()
@ -174,6 +177,7 @@ SysFSDirectoryInode::SysFSDirectoryInode(SysFS const& fs, SysFSComponent const&
SysFSDirectoryInode::~SysFSDirectoryInode()
{
}
InodeMetadata SysFSDirectoryInode::metadata() const
{
Locker locker(m_inode_lock);
@ -201,4 +205,26 @@ RefPtr<Inode> SysFSDirectoryInode::lookup(StringView name)
return component->to_inode(m_parent_fs);
}
SysFSBusDirectory& SysFSComponentRegistry::buses_directory()
{
return *m_root_directory->m_buses_directory;
}
void SysFSComponentRegistry::register_new_bus_directory(SysFSDirectory& new_bus_directory)
{
VERIFY(!m_root_directory->m_buses_directory.is_null());
m_root_directory->m_buses_directory->m_components.append(new_bus_directory);
}
UNMAP_AFTER_INIT NonnullRefPtr<SysFSBusDirectory> SysFSBusDirectory::must_create(SysFSRootDirectory const& parent_directory)
{
auto directory = adopt_ref(*new (nothrow) SysFSBusDirectory(parent_directory));
return directory;
}
UNMAP_AFTER_INIT SysFSBusDirectory::SysFSBusDirectory(SysFSRootDirectory const& parent_directory)
: SysFSDirectory("bus"sv, parent_directory)
{
}
}

View file

@ -21,6 +21,17 @@ public:
private:
SysFSRootDirectory();
RefPtr<SysFSBusDirectory> m_buses_directory;
};
class SysFSBusDirectory : public SysFSDirectory {
friend class SysFSComponentRegistry;
public:
static NonnullRefPtr<SysFSBusDirectory> must_create(SysFSRootDirectory const&);
private:
explicit SysFSBusDirectory(SysFSRootDirectory const&);
};
class SysFSComponentRegistry {
@ -35,6 +46,9 @@ public:
SysFSDirectory& root_directory() { return m_root_directory; }
Mutex& get_lock() { return m_lock; }
void register_new_bus_directory(SysFSDirectory&);
SysFSBusDirectory& buses_directory();
private:
Mutex m_lock;
NonnullRefPtr<SysFSRootDirectory> m_root_directory;

View file

@ -40,7 +40,6 @@ class PerformanceEventBuffer;
class PhysicalPage;
class PhysicalRegion;
class ProcFS;
class ProcFSBusDirectory;
class ProcFSDirectoryInode;
class ProcFSExposedComponent;
class ProcFSExposedDirectory;
@ -61,6 +60,8 @@ class SharedInodeVMObject;
class Socket;
class Space;
class SysFS;
class SysFSDirectory;
class SysFSBusDirectory;
class SysFSDirectoryInode;
class SysFSInode;
class TCPSocket;

View file

@ -813,12 +813,6 @@ UNMAP_AFTER_INIT ProcFSProfile::ProcFSProfile()
{
}
UNMAP_AFTER_INIT NonnullRefPtr<ProcFSBusDirectory> ProcFSBusDirectory::must_create(const ProcFSRootDirectory& parent_directory)
{
auto directory = adopt_ref(*new (nothrow) ProcFSBusDirectory(parent_directory));
return directory;
}
UNMAP_AFTER_INIT NonnullRefPtr<ProcFSSystemDirectory> ProcFSSystemDirectory::must_create(const ProcFSRootDirectory& parent_directory)
{
auto directory = adopt_ref(*new (nothrow) ProcFSSystemDirectory(parent_directory));
@ -828,10 +822,6 @@ UNMAP_AFTER_INIT NonnullRefPtr<ProcFSSystemDirectory> ProcFSSystemDirectory::mus
return directory;
}
UNMAP_AFTER_INIT ProcFSBusDirectory::ProcFSBusDirectory(const ProcFSRootDirectory& parent_directory)
: ProcFSExposedDirectory("bus"sv, parent_directory)
{
}
UNMAP_AFTER_INIT ProcFSSystemDirectory::ProcFSSystemDirectory(const ProcFSRootDirectory& parent_directory)
: ProcFSExposedDirectory("sys"sv, parent_directory)
{
@ -856,9 +846,6 @@ UNMAP_AFTER_INIT NonnullRefPtr<ProcFSRootDirectory> ProcFSRootDirectory::must_cr
directory->m_components.append(ProcFSProfile::must_create());
directory->m_components.append(ProcFSNetworkDirectory::must_create(*directory));
auto buses_directory = ProcFSBusDirectory::must_create(*directory);
directory->m_components.append(buses_directory);
directory->m_buses_directory = buses_directory;
directory->m_components.append(ProcFSSystemDirectory::must_create(*directory));
return directory;
}

View file

@ -30,9 +30,6 @@ public:
InodeIndex allocate_inode_index() const;
ProcFSComponentRegistry();
void register_new_bus_directory(ProcFSExposedDirectory&);
const ProcFSBusDirectory& buses_directory() const;
void register_new_process(Process&);
void unregister_process(Process&);
@ -157,16 +154,6 @@ private:
RefPtr<Process> m_associated_process;
};
class ProcFSBusDirectory : public ProcFSExposedDirectory {
friend class ProcFSComponentRegistry;
public:
static NonnullRefPtr<ProcFSBusDirectory> must_create(const ProcFSRootDirectory& parent_directory);
private:
ProcFSBusDirectory(const ProcFSRootDirectory& parent_directory);
};
class ProcFSRootDirectory final : public ProcFSExposedDirectory {
friend class ProcFSComponentRegistry;
@ -181,7 +168,6 @@ private:
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
ProcFSRootDirectory();
RefPtr<ProcFSBusDirectory> m_buses_directory;
IntrusiveList<ProcFSProcessDirectory, RefPtr<ProcFSProcessDirectory>, &ProcFSProcessDirectory::m_list_node> m_process_directories;
};

View file

@ -23,7 +23,7 @@ int main(int argc, char** argv)
return 1;
}
if (unveil("/proc/bus/usb", "r") < 0) {
if (unveil("/sys/bus/usb", "r") < 0) {
perror("unveil");
return 1;
}
@ -42,7 +42,7 @@ int main(int argc, char** argv)
args.set_general_help("List USB devices.");
args.parse(argc, argv);
Core::DirIterator usb_devices("/proc/bus/usb", Core::DirIterator::SkipDots);
Core::DirIterator usb_devices("/sys/bus/usb", Core::DirIterator::SkipDots);
RefPtr<USBDB::Database> usb_db = USBDB::Database::open();
if (!usb_db) {