USBHub.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/Types.h>
  9. #include <Kernel/Bus/USB/USBDevice.h>
  10. namespace Kernel::USB {
  11. // USB 2.0 Specification Page 421 Table 11-16
  12. enum HubRequest : u8 {
  13. GET_STATUS = 0,
  14. CLEAR_FEATURE = 1,
  15. // 2 is reserved.
  16. SET_FEATURE = 3,
  17. // 4-5 are reserved.
  18. GET_DESCRIPTOR = 6,
  19. SET_DESCRIPTOR = 7,
  20. CLEAR_TT_BUFFER = 8,
  21. RESET_TT = 9,
  22. GET_TT_STATE = 10,
  23. STOP_TT = 11,
  24. };
  25. // USB 2.0 Specification Pages 421-422 Table 11-17
  26. enum HubFeatureSelector : u8 {
  27. C_HUB_LOCAL_POWER = 0,
  28. C_HUB_OVER_CURRENT = 1,
  29. PORT_CONNECTION = 0,
  30. PORT_ENABLE = 1,
  31. PORT_SUSPEND = 2,
  32. PORT_OVER_CURRENT = 3,
  33. PORT_RESET = 4,
  34. PORT_POWER = 8,
  35. PORT_LOW_SPEED = 9,
  36. C_PORT_CONNECTION = 16,
  37. C_PORT_ENABLE = 17,
  38. C_PORT_SUSPEND = 18,
  39. C_PORT_OVER_CURRENT = 19,
  40. C_PORT_RESET = 20,
  41. PORT_TEST = 21,
  42. PORT_INDICATOR = 22,
  43. };
  44. // USB 2.0 Specification Section 11.24.2.{6,7}
  45. // This is used to store both the hub status and port status, as they have the same layout.
  46. struct [[gnu::packed]] HubStatus {
  47. u16 status { 0 };
  48. u16 change { 0 };
  49. };
  50. static_assert(sizeof(HubStatus) == 4);
  51. static constexpr u16 HUB_STATUS_LOCAL_POWER_SOURCE = (1 << 0);
  52. static constexpr u16 HUB_STATUS_OVER_CURRENT = (1 << 1);
  53. static constexpr u16 HUB_STATUS_LOCAL_POWER_SOURCE_CHANGED = (1 << 0);
  54. static constexpr u16 HUB_STATUS_OVER_CURRENT_CHANGED = (1 << 1);
  55. static constexpr u16 PORT_STATUS_CURRENT_CONNECT_STATUS = (1 << 0);
  56. static constexpr u16 PORT_STATUS_PORT_ENABLED = (1 << 1);
  57. static constexpr u16 PORT_STATUS_SUSPEND = (1 << 2);
  58. static constexpr u16 PORT_STATUS_OVER_CURRENT = (1 << 3);
  59. static constexpr u16 PORT_STATUS_RESET = (1 << 4);
  60. static constexpr u16 PORT_STATUS_PORT_POWER = (1 << 8);
  61. static constexpr u16 PORT_STATUS_LOW_SPEED_DEVICE_ATTACHED = (1 << 9);
  62. static constexpr u16 PORT_STATUS_HIGH_SPEED_DEVICE_ATTACHED = (1 << 10);
  63. static constexpr u16 PORT_STATUS_PORT_STATUS_MODE = (1 << 11);
  64. static constexpr u16 PORT_STATUS_PORT_INDICATOR_CONTROL = (1 << 12);
  65. static constexpr u16 PORT_STATUS_CONNECT_STATUS_CHANGED = (1 << 0);
  66. static constexpr u16 PORT_STATUS_PORT_ENABLED_CHANGED = (1 << 1);
  67. static constexpr u16 PORT_STATUS_SUSPEND_CHANGED = (1 << 2);
  68. static constexpr u16 PORT_STATUS_OVER_CURRENT_INDICATOR_CHANGED = (1 << 3);
  69. static constexpr u16 PORT_STATUS_RESET_CHANGED = (1 << 4);
  70. class Hub : public Device {
  71. public:
  72. static KResultOr<NonnullRefPtr<Hub>> try_create_root_hub(NonnullRefPtr<USBController>, DeviceSpeed);
  73. static KResultOr<NonnullRefPtr<Hub>> try_create_from_device(Device const&);
  74. // Root Hub constructor
  75. Hub(NonnullRefPtr<USBController>, DeviceSpeed, NonnullOwnPtr<Pipe> default_pipe);
  76. Hub(Device const&, NonnullOwnPtr<Pipe> default_pipe);
  77. virtual ~Hub() override = default;
  78. KResult enumerate_and_power_on_hub();
  79. KResult get_port_status(u8, HubStatus&);
  80. KResult clear_port_feature(u8, HubFeatureSelector);
  81. KResult set_port_feature(u8, HubFeatureSelector);
  82. KResult reset_port(u8);
  83. void check_for_port_updates();
  84. private:
  85. USBHubDescriptor m_hub_descriptor;
  86. Device::List m_children;
  87. void remove_children_from_sysfs();
  88. };
  89. }