USBDevice.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/String.h>
  8. #include <AK/Types.h>
  9. #include <AK/Vector.h>
  10. #include <Kernel/Bus/USB/USBController.h>
  11. #include <Kernel/Bus/USB/USBDescriptors.h>
  12. #include <Kernel/Bus/USB/USBDevice.h>
  13. #include <Kernel/Bus/USB/USBRequest.h>
  14. #include <Kernel/StdLib.h>
  15. namespace Kernel::USB {
  16. KResultOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
  17. {
  18. auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0));
  19. auto device = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Device(controller, port, speed, move(pipe))));
  20. TRY(device->enumerate_device());
  21. return device;
  22. }
  23. Device::Device(USBController const& controller, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
  24. : m_device_port(port)
  25. , m_device_speed(speed)
  26. , m_address(0)
  27. , m_controller(controller)
  28. , m_default_pipe(move(default_pipe))
  29. {
  30. }
  31. Device::Device(NonnullRefPtr<USBController> controller, u8 address, u8 port, DeviceSpeed speed, NonnullOwnPtr<Pipe> default_pipe)
  32. : m_device_port(port)
  33. , m_device_speed(speed)
  34. , m_address(address)
  35. , m_controller(controller)
  36. , m_default_pipe(move(default_pipe))
  37. {
  38. }
  39. Device::Device(Device const& device, NonnullOwnPtr<Pipe> default_pipe)
  40. : m_device_port(device.port())
  41. , m_device_speed(device.speed())
  42. , m_address(device.address())
  43. , m_device_descriptor(device.device_descriptor())
  44. , m_controller(device.controller())
  45. , m_default_pipe(move(default_pipe))
  46. {
  47. }
  48. Device::~Device()
  49. {
  50. }
  51. KResult 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. return KSuccess;
  100. }
  101. }