Kernel/USB: Replace PortNumber enum with a raw u8

A hub can technically have up to 255 ports, given that bNbrPorts is a
u8 and the DeviceRemovable field is a VLA to support up to 255 ports.

Source: USB 2.0 Specification Section 11.23.2.1

That means this enum is not going to scale well in terms of size.
Replacing it with a raw u8 allows me to remove the two port assumption
and a cast.
This commit is contained in:
Luke 2021-08-14 00:20:53 +01:00 committed by Andreas Kling
parent 86ccacf6b5
commit b6a2bbba3b
Notes: sideshowbarker 2024-07-18 05:43:43 +09:00
3 changed files with 11 additions and 18 deletions

View file

@ -16,7 +16,7 @@
namespace Kernel::USB {
KResultOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, PortNumber port, DeviceSpeed speed)
KResultOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
{
auto pipe_or_error = Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0);
if (pipe_or_error.is_error())
@ -33,7 +33,7 @@ KResultOr<NonnullRefPtr<Device>> Device::try_create(USBController const& control
return device.release_nonnull();
}
Device::Device(USBController const& controller, PortNumber port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
Device::Device(USBController const& controller, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
: m_device_port(port)
, m_device_speed(speed)
, m_address(0)
@ -42,7 +42,7 @@ Device::Device(USBController const& controller, PortNumber port, DeviceSpeed spe
{
}
Device::Device(NonnullRefPtr<USBController> controller, u8 address, PortNumber port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
Device::Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
: m_device_port(port)
, m_device_speed(speed)
, m_address(address)

View file

@ -21,26 +21,21 @@ class USBController;
// https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
class Device : public RefCounted<Device> {
public:
enum class PortNumber : u8 {
Port1 = 0,
Port2
};
enum class DeviceSpeed : u8 {
FullSpeed = 0,
LowSpeed
};
public:
static KResultOr<NonnullRefPtr<Device>> try_create(USBController const&, PortNumber, DeviceSpeed);
static KResultOr<NonnullRefPtr<Device>> try_create(USBController const&, u8, DeviceSpeed);
Device(USBController const&, PortNumber, DeviceSpeed, NonnullOwnPtr<Pipe> default_pipe);
Device(USBController const&, u8, DeviceSpeed, NonnullOwnPtr<Pipe> default_pipe);
Device(Device const& device, NonnullOwnPtr<Pipe> default_pipe);
virtual ~Device();
KResult enumerate_device();
PortNumber port() const { return m_device_port; }
u8 port() const { return m_device_port; }
DeviceSpeed speed() const { return m_device_speed; }
u8 address() const { return m_address; }
@ -51,9 +46,9 @@ public:
USBController const& controller() const { return *m_controller; }
protected:
Device(NonnullRefPtr<USBController> controller, u8 address, PortNumber port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe);
Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe);
PortNumber m_device_port; // What port is this device attached to
u8 m_device_port { 0 }; // What port is this device attached to. NOTE: This is 1-based.
DeviceSpeed m_device_speed; // What speed is this device running at
u8 m_address { 0 }; // USB address assigned to this device

View file

@ -45,7 +45,7 @@ KResultOr<NonnullRefPtr<Hub>> Hub::try_create_from_device(Device const& device)
}
Hub::Hub(NonnullRefPtr<USBController> controller, DeviceSpeed device_speed, NonnullOwnPtr<Pipe> default_pipe)
: Device(move(controller), PortNumber::Port1, device_speed, move(default_pipe))
: Device(move(controller), 1 /* Port 1 */, device_speed, move(default_pipe))
{
}
@ -260,8 +260,7 @@ void Hub::check_for_port_updates()
// FIXME: Check for high speed.
auto speed = port_status.status & PORT_STATUS_LOW_SPEED_DEVICE_ATTACHED ? USB::Device::DeviceSpeed::LowSpeed : USB::Device::DeviceSpeed::FullSpeed;
// FIXME: This only assumes two ports.
auto device_or_error = USB::Device::try_create(m_controller, port_number == 1 ? PortNumber::Port1 : PortNumber::Port2, speed);
auto device_or_error = USB::Device::try_create(m_controller, port_number, speed);
if (device_or_error.is_error()) {
dbgln("USB Hub: Failed to create device for port {}: {}", port_number, device_or_error.error());
return;
@ -290,8 +289,7 @@ void Hub::check_for_port_updates()
Device* device_to_remove = nullptr;
for (auto& child : m_children) {
// FIXME: This kinda sucks.
if (port_number - 1 == (u8)child.port()) {
if (port_number == child.port()) {
device_to_remove = &child;
break;
}