USBDevice.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/StdLib.h>
  14. namespace Kernel::USB {
  15. ErrorOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
  16. {
  17. auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0));
  18. auto device = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Device(controller, port, speed, move(pipe))));
  19. TRY(device->enumerate_device());
  20. return device;
  21. }
  22. Device::Device(USBController const& controller, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
  23. : m_device_port(port)
  24. , m_device_speed(speed)
  25. , m_address(0)
  26. , m_controller(controller)
  27. , m_default_pipe(move(default_pipe))
  28. {
  29. }
  30. Device::Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
  31. : m_device_port(port)
  32. , m_device_speed(speed)
  33. , m_address(address)
  34. , m_controller(move(controller))
  35. , m_default_pipe(move(default_pipe))
  36. {
  37. }
  38. Device::Device(Device const& device, NonnullOwnPtr<Pipe> default_pipe)
  39. : m_device_port(device.port())
  40. , m_device_speed(device.speed())
  41. , m_address(device.address())
  42. , m_device_descriptor(device.device_descriptor())
  43. , m_controller(device.controller())
  44. , m_default_pipe(move(default_pipe))
  45. {
  46. }
  47. Device::~Device() = default;
  48. ErrorOr<void> Device::enumerate_device()
  49. {
  50. USBDeviceDescriptor dev_descriptor {};
  51. // Send 8-bytes to get at least the `max_packet_size` from the device
  52. constexpr u8 short_device_descriptor_length = 8;
  53. 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));
  54. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  55. if (transfer_length < short_device_descriptor_length) {
  56. dbgln("USB Device: Not enough bytes for short device descriptor. Expected {}, got {}.", short_device_descriptor_length, transfer_length);
  57. return EIO;
  58. }
  59. if constexpr (USB_DEBUG) {
  60. dbgln("USB Short Device Descriptor:");
  61. dbgln("Descriptor length: {}", dev_descriptor.descriptor_header.length);
  62. dbgln("Descriptor type: {}", dev_descriptor.descriptor_header.descriptor_type);
  63. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  64. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  65. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  66. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  67. }
  68. // Ensure that this is actually a valid device descriptor...
  69. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  70. m_default_pipe->set_max_packet_size(dev_descriptor.max_packet_size);
  71. 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));
  72. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  73. if (transfer_length < sizeof(USBDeviceDescriptor)) {
  74. dbgln("USB Device: Unexpected device descriptor length. Expected {}, got {}.", sizeof(USBDeviceDescriptor), transfer_length);
  75. return EIO;
  76. }
  77. // Ensure that this is actually a valid device descriptor...
  78. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  79. if constexpr (USB_DEBUG) {
  80. dbgln("USB Device Descriptor for {:04x}:{:04x}", dev_descriptor.vendor_id, dev_descriptor.product_id);
  81. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  82. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  83. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  84. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  85. dbgln("Number of configurations: {:02x}", dev_descriptor.num_configurations);
  86. }
  87. auto new_address = m_controller->allocate_address();
  88. // Attempt to set devices address on the bus
  89. transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, new_address, 0, 0, nullptr));
  90. // This has to be set after we send out the "Set Address" request because it might be sent to the root hub.
  91. // The root hub uses the address to intercept requests to itself.
  92. m_address = new_address;
  93. m_default_pipe->set_device_address(new_address);
  94. dbgln_if(USB_DEBUG, "USB Device: Set address to {}", m_address);
  95. memcpy(&m_device_descriptor, &dev_descriptor, sizeof(USBDeviceDescriptor));
  96. return {};
  97. }
  98. }