USBDevice.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/OwnPtr.h>
  7. #include <AK/Types.h>
  8. #include <AK/Vector.h>
  9. #include <Kernel/Bus/USB/USBController.h>
  10. #include <Kernel/Bus/USB/USBDescriptors.h>
  11. #include <Kernel/Bus/USB/USBDevice.h>
  12. #include <Kernel/Bus/USB/USBRequest.h>
  13. #include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
  14. #include <Kernel/StdLib.h>
  15. namespace Kernel::USB {
  16. ErrorOr<NonnullLockRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
  17. {
  18. auto pipe = TRY(ControlPipe::create(controller, 0, 8, 0));
  19. auto device = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) Device(controller, port, speed, move(pipe))));
  20. auto sysfs_node = TRY(SysFSUSBDeviceInformation::create(*device));
  21. device->m_sysfs_device_info_node.with([&](auto& node) {
  22. node = move(sysfs_node);
  23. });
  24. TRY(device->enumerate_device());
  25. return device;
  26. }
  27. Device::Device(USBController const& controller, u8 port, DeviceSpeed speed, NonnullOwnPtr<ControlPipe> default_pipe)
  28. : m_device_port(port)
  29. , m_device_speed(speed)
  30. , m_address(0)
  31. , m_controller(controller)
  32. , m_default_pipe(move(default_pipe))
  33. {
  34. }
  35. Device::Device(NonnullLockRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<ControlPipe> default_pipe)
  36. : m_device_port(port)
  37. , m_device_speed(speed)
  38. , m_address(address)
  39. , m_controller(move(controller))
  40. , m_default_pipe(move(default_pipe))
  41. {
  42. }
  43. Device::Device(Device const& device, NonnullOwnPtr<ControlPipe> default_pipe)
  44. : m_device_port(device.port())
  45. , m_device_speed(device.speed())
  46. , m_address(device.address())
  47. , m_device_descriptor(device.device_descriptor())
  48. , m_controller(device.controller())
  49. , m_default_pipe(move(default_pipe))
  50. {
  51. }
  52. Device::~Device() = default;
  53. ErrorOr<void> Device::enumerate_device()
  54. {
  55. USBDeviceDescriptor dev_descriptor {};
  56. // Send 8-bytes to get at least the `max_packet_size` from the device
  57. constexpr u8 short_device_descriptor_length = 8;
  58. auto transfer_length = TRY(m_default_pipe->submit_control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, short_device_descriptor_length, &dev_descriptor));
  59. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  60. if (transfer_length < short_device_descriptor_length) {
  61. dbgln("USB Device: Not enough bytes for short device descriptor. Expected {}, got {}.", short_device_descriptor_length, transfer_length);
  62. return EIO;
  63. }
  64. if constexpr (USB_DEBUG) {
  65. dbgln("USB Short Device Descriptor:");
  66. dbgln("Descriptor length: {}", dev_descriptor.descriptor_header.length);
  67. dbgln("Descriptor type: {}", dev_descriptor.descriptor_header.descriptor_type);
  68. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  69. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  70. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  71. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  72. }
  73. // Ensure that this is actually a valid device descriptor...
  74. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  75. m_default_pipe->set_max_packet_size(dev_descriptor.max_packet_size);
  76. transfer_length = TRY(m_default_pipe->submit_control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_DEVICE << 8), 0, sizeof(USBDeviceDescriptor), &dev_descriptor));
  77. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  78. if (transfer_length < sizeof(USBDeviceDescriptor)) {
  79. dbgln("USB Device: Unexpected device descriptor length. Expected {}, got {}.", sizeof(USBDeviceDescriptor), transfer_length);
  80. return EIO;
  81. }
  82. // Ensure that this is actually a valid device descriptor...
  83. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  84. if constexpr (USB_DEBUG) {
  85. dbgln("USB Device Descriptor for {:04x}:{:04x}", dev_descriptor.vendor_id, dev_descriptor.product_id);
  86. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  87. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  88. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  89. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  90. dbgln("Number of configurations: {:02x}", dev_descriptor.num_configurations);
  91. }
  92. auto new_address = m_controller->allocate_address();
  93. // Attempt to set devices address on the bus
  94. transfer_length = TRY(m_default_pipe->submit_control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, new_address, 0, 0, nullptr));
  95. // This has to be set after we send out the "Set Address" request because it might be sent to the root hub.
  96. // The root hub uses the address to intercept requests to itself.
  97. m_address = new_address;
  98. m_default_pipe->set_device_address(new_address);
  99. dbgln_if(USB_DEBUG, "USB Device: Set address to {}", m_address);
  100. memcpy(&m_device_descriptor, &dev_descriptor, sizeof(USBDeviceDescriptor));
  101. // Fetch the configuration descriptors from the device
  102. m_configurations.ensure_capacity(m_device_descriptor.num_configurations);
  103. for (auto configuration = 0u; configuration < m_device_descriptor.num_configurations; configuration++) {
  104. USBConfigurationDescriptor configuration_descriptor;
  105. transfer_length = TRY(m_default_pipe->submit_control_transfer(USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST, USB_REQUEST_GET_DESCRIPTOR, (DESCRIPTOR_TYPE_CONFIGURATION << 8u) | configuration, 0, sizeof(USBConfigurationDescriptor), &configuration_descriptor));
  106. if constexpr (USB_DEBUG) {
  107. dbgln("USB Configuration Descriptor {}", configuration);
  108. dbgln("Total Length: {}", configuration_descriptor.total_length);
  109. dbgln("Number of interfaces: {}", configuration_descriptor.number_of_interfaces);
  110. dbgln("Configuration Value: {}", configuration_descriptor.configuration_value);
  111. dbgln("Attributes Bitmap: {:08b}", configuration_descriptor.attributes_bitmap);
  112. dbgln("Maximum Power: {}mA", configuration_descriptor.max_power_in_ma * 2u); // This value is in 2mA steps
  113. }
  114. USBConfiguration device_configuration(*this, configuration_descriptor);
  115. TRY(device_configuration.enumerate_interfaces());
  116. m_configurations.append(device_configuration);
  117. }
  118. return {};
  119. }
  120. ErrorOr<size_t> Device::control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data)
  121. {
  122. return TRY(m_default_pipe->submit_control_transfer(request_type, request, value, index, length, data));
  123. }
  124. }