USB: Store devices in globally accessible array

USB Devices are now stored so that they may be later retrieved and
operated on (i.e, fetching their assigned device address via
ProcFS)
This commit is contained in:
Jesse Buhagiar 2021-06-08 00:08:41 +10:00 committed by Ali Mohammad Pur
parent 0e680cb17a
commit 7b42146f33
Notes: sideshowbarker 2024-07-18 12:05:45 +09:00
4 changed files with 38 additions and 7 deletions

View file

@ -103,6 +103,27 @@ UNMAP_AFTER_INIT UHCIController::~UHCIController()
{
}
RefPtr<USB::Device> const UHCIController::get_device_at_port(USB::Device::PortNumber port)
{
if (!m_devices.at(to_underlying(port)))
return nullptr;
return m_devices.at(to_underlying(port));
}
RefPtr<USB::Device> const UHCIController::get_device_from_address(u8 device_address)
{
for (auto const& device : m_devices) {
if (!device)
continue;
if (device->address() == device_address)
return device;
}
return nullptr;
}
void UHCIController::reset()
{
stop();
@ -545,6 +566,8 @@ void UHCIController::spawn_port_proc()
if (device.is_error())
dmesgln("UHCI: Device creation failed on port 1 ({})", device.error());
m_devices.at(0) = device.value();
} else {
dmesgln("UHCI: Device detach detected on Root Port 1");
}
@ -575,6 +598,8 @@ void UHCIController::spawn_port_proc()
if (device.is_error())
dmesgln("UHCI: Device creation failed on port 2 ({})", device.error());
m_devices.at(1) = device.value();
} else {
dmesgln("UHCI: Device detach detected on Root Port 2");
}

View file

@ -26,6 +26,7 @@ class UHCIController final : public PCI::Device {
public:
static void detect();
static UHCIController& the();
virtual ~UHCIController() override;
virtual const char* purpose() const override { return "UHCI"; }
@ -39,6 +40,9 @@ public:
KResultOr<size_t> submit_control_transfer(Transfer& transfer);
RefPtr<USB::Device> const get_device_at_port(USB::Device::PortNumber);
RefPtr<USB::Device> const get_device_from_address(u8 device_address);
private:
UHCIController(PCI::Address, PCI::ID);
@ -89,6 +93,8 @@ private:
OwnPtr<Region> m_framelist;
OwnPtr<Region> m_qh_pool;
OwnPtr<Region> m_td_pool;
Array<RefPtr<USB::Device>, 2> m_devices; // Devices connected to the root ports (of which there are two)
};
}

View file

@ -16,13 +16,13 @@ static u32 s_next_usb_address = 1; // Next address we hand out to a device once
namespace Kernel::USB {
KResultOr<NonnullOwnPtr<Device>> Device::try_create(PortNumber port, DeviceSpeed speed)
KResultOr<NonnullRefPtr<Device>> Device::try_create(PortNumber port, DeviceSpeed speed)
{
auto pipe_or_error = Pipe::try_create_pipe(Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0);
if (pipe_or_error.is_error())
return pipe_or_error.error();
auto device = adopt_own_if_nonnull(new Device(port, speed, pipe_or_error.release_value()));
auto device = adopt_ref_if_nonnull(new Device(port, speed, pipe_or_error.release_value()));
if (!device)
return ENOMEM;

View file

@ -17,7 +17,7 @@ namespace Kernel::USB {
// glues together:
//
// https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
class Device {
class Device : public RefCounted<Device> {
public:
enum class PortNumber : u8 {
Port1 = 0,
@ -30,8 +30,7 @@ public:
};
public:
static KResultOr<NonnullOwnPtr<Device>> try_create(PortNumber, DeviceSpeed);
static Device* get(PortNumber);
static KResultOr<NonnullRefPtr<Device>> try_create(PortNumber, DeviceSpeed);
Device(PortNumber, DeviceSpeed, NonnullOwnPtr<Pipe> default_pipe);
~Device();
@ -50,8 +49,9 @@ private:
u8 m_address { 0 }; // USB address assigned to this device
// Device description
u16 m_vendor_id { 0 }; // This device's vendor ID assigned by the USB group
u16 m_product_id { 0 }; // This device's product ID assigned by the USB group
u16 m_vendor_id { 0 }; // This device's vendor ID assigned by the USB group
u16 m_product_id { 0 }; // This device's product ID assigned by the USB group
USBDeviceDescriptor m_device_descriptor; // Device Descriptor obtained from USB Device
NonnullOwnPtr<Pipe> m_default_pipe; // Default communication pipe (endpoint0) used during enumeration
};