Serenity Operating System
1/*
2 * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Badge.h>
10#include <AK/OwnPtr.h>
11#include <AK/Types.h>
12#include <AK/Vector.h>
13#include <Kernel/Bus/USB/USBConfiguration.h>
14#include <Kernel/Bus/USB/USBPipe.h>
15
16namespace Kernel {
17class SysFSUSBDeviceInformation;
18}
19
20namespace Kernel::USB {
21
22class USBController;
23class USBConfiguration;
24
25//
26// Some nice info from FTDI on device enumeration and how some of this
27// glues together:
28//
29// https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
30class Hub;
31class Device : public AtomicRefCounted<Device> {
32public:
33 enum class DeviceSpeed : u8 {
34 FullSpeed = 0,
35 LowSpeed
36 };
37
38 static ErrorOr<NonnullLockRefPtr<Device>> try_create(USBController const&, u8, DeviceSpeed);
39
40 Device(USBController const&, u8, DeviceSpeed, NonnullOwnPtr<ControlPipe> default_pipe);
41 Device(Device const& device, NonnullOwnPtr<ControlPipe> default_pipe);
42 virtual ~Device();
43
44 ErrorOr<void> enumerate_device();
45
46 u8 port() const { return m_device_port; }
47 DeviceSpeed speed() const { return m_device_speed; }
48
49 u8 address() const { return m_address; }
50
51 USBDeviceDescriptor const& device_descriptor() const { return m_device_descriptor; }
52
53 USBController& controller() { return *m_controller; }
54 USBController const& controller() const { return *m_controller; }
55
56 ErrorOr<size_t> control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data);
57
58 Vector<USBConfiguration> const& configurations() const { return m_configurations; }
59
60 SysFSUSBDeviceInformation& sysfs_device_info_node(Badge<USB::Hub>) { return *m_sysfs_device_info_node; }
61
62protected:
63 Device(NonnullLockRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<ControlPipe> default_pipe);
64
65 u8 m_device_port { 0 }; // What port is this device attached to. NOTE: This is 1-based.
66 DeviceSpeed m_device_speed; // What speed is this device running at
67 u8 m_address { 0 }; // USB address assigned to this device
68
69 // Device description
70 u16 m_vendor_id { 0 }; // This device's vendor ID assigned by the USB group
71 u16 m_product_id { 0 }; // This device's product ID assigned by the USB group
72 USBDeviceDescriptor m_device_descriptor {}; // Device Descriptor obtained from USB Device
73 Vector<USBConfiguration> m_configurations; // Configurations for this device
74
75 NonnullLockRefPtr<USBController> m_controller;
76 NonnullOwnPtr<ControlPipe> m_default_pipe; // Default communication pipe (endpoint0) used during enumeration
77
78private:
79 IntrusiveListNode<Device, NonnullLockRefPtr<Device>> m_hub_child_node;
80
81protected:
82 LockRefPtr<SysFSUSBDeviceInformation> m_sysfs_device_info_node;
83
84public:
85 using List = IntrusiveList<&Device::m_hub_child_node>;
86};
87}