USBDevice.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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()
  48. {
  49. }
  50. ErrorOr<void> Device::enumerate_device()
  51. {
  52. USBDeviceDescriptor dev_descriptor {};
  53. // Send 8-bytes to get at least the `max_packet_size` from the device
  54. constexpr u8 short_device_descriptor_length = 8;
  55. 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));
  56. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  57. if (transfer_length < short_device_descriptor_length) {
  58. dbgln("USB Device: Not enough bytes for short device descriptor. Expected {}, got {}.", short_device_descriptor_length, transfer_length);
  59. return EIO;
  60. }
  61. if constexpr (USB_DEBUG) {
  62. dbgln("USB Short Device Descriptor:");
  63. dbgln("Descriptor length: {}", dev_descriptor.descriptor_header.length);
  64. dbgln("Descriptor type: {}", dev_descriptor.descriptor_header.descriptor_type);
  65. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  66. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  67. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  68. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  69. }
  70. // Ensure that this is actually a valid device descriptor...
  71. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  72. m_default_pipe->set_max_packet_size(dev_descriptor.max_packet_size);
  73. 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));
  74. // FIXME: This be "not equal to" instead of "less than", but control transfers report a higher transfer length than expected.
  75. if (transfer_length < sizeof(USBDeviceDescriptor)) {
  76. dbgln("USB Device: Unexpected device descriptor length. Expected {}, got {}.", sizeof(USBDeviceDescriptor), transfer_length);
  77. return EIO;
  78. }
  79. // Ensure that this is actually a valid device descriptor...
  80. VERIFY(dev_descriptor.descriptor_header.descriptor_type == DESCRIPTOR_TYPE_DEVICE);
  81. if constexpr (USB_DEBUG) {
  82. dbgln("USB Device Descriptor for {:04x}:{:04x}", dev_descriptor.vendor_id, dev_descriptor.product_id);
  83. dbgln("Device Class: {:02x}", dev_descriptor.device_class);
  84. dbgln("Device Sub-Class: {:02x}", dev_descriptor.device_sub_class);
  85. dbgln("Device Protocol: {:02x}", dev_descriptor.device_protocol);
  86. dbgln("Max Packet Size: {:02x} bytes", dev_descriptor.max_packet_size);
  87. dbgln("Number of configurations: {:02x}", dev_descriptor.num_configurations);
  88. }
  89. auto new_address = m_controller->allocate_address();
  90. // Attempt to set devices address on the bus
  91. transfer_length = TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, new_address, 0, 0, nullptr));
  92. // This has to be set after we send out the "Set Address" request because it might be sent to the root hub.
  93. // The root hub uses the address to intercept requests to itself.
  94. m_address = new_address;
  95. m_default_pipe->set_device_address(new_address);
  96. dbgln_if(USB_DEBUG, "USB Device: Set address to {}", m_address);
  97. memcpy(&m_device_descriptor, &dev_descriptor, sizeof(USBDeviceDescriptor));
  98. return {};
  99. }
  100. }