USBDevice.cpp 6.8 KB

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