summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2021-08-14 00:20:53 +0100
committerAndreas Kling <kling@serenityos.org>2021-08-14 21:22:44 +0200
commitb6a2bbba3b20a748f53e976d22608be6746a8c3b (patch)
treeca8fafd46cb713458fd3ff54904fd6b90090d610 /Kernel
parent86ccacf6b52ddfdfadd0617b8d35c91e64cb1939 (diff)
downloadserenity-b6a2bbba3b20a748f53e976d22608be6746a8c3b.zip
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.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/Bus/USB/USBDevice.cpp6
-rw-r--r--Kernel/Bus/USB/USBDevice.h15
-rw-r--r--Kernel/Bus/USB/USBHub.cpp8
3 files changed, 11 insertions, 18 deletions
diff --git a/Kernel/Bus/USB/USBDevice.cpp b/Kernel/Bus/USB/USBDevice.cpp
index 69586c3129..ac3d175311 100644
--- a/Kernel/Bus/USB/USBDevice.cpp
+++ b/Kernel/Bus/USB/USBDevice.cpp
@@ -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)
diff --git a/Kernel/Bus/USB/USBDevice.h b/Kernel/Bus/USB/USBDevice.h
index 4d23b54310..e55aff775d 100644
--- a/Kernel/Bus/USB/USBDevice.h
+++ b/Kernel/Bus/USB/USBDevice.h
@@ -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
diff --git a/Kernel/Bus/USB/USBHub.cpp b/Kernel/Bus/USB/USBHub.cpp
index 1b35a05b47..1ba1728565 100644
--- a/Kernel/Bus/USB/USBHub.cpp
+++ b/Kernel/Bus/USB/USBHub.cpp
@@ -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;
}