USBPipe.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <AK/Types.h>
  9. #include <Kernel/Bus/USB/USBDescriptors.h>
  10. #include <Kernel/Memory/Region.h>
  11. namespace Kernel::USB {
  12. class USBController;
  13. //
  14. // A pipe is the logical connection between a memory buffer on the PC (host) and
  15. // an endpoint on the device. In this implementation, the data buffer the pipe connects
  16. // to is the physical buffer created when a Transfer is allocated.
  17. //
  18. class Pipe {
  19. public:
  20. enum class Type : u8 {
  21. Control = 0,
  22. Isochronous = 1,
  23. Bulk = 2,
  24. Interrupt = 3
  25. };
  26. enum class Direction : u8 {
  27. Out = 0,
  28. In = 1,
  29. Bidirectional = 2
  30. };
  31. enum class DeviceSpeed : u8 {
  32. LowSpeed,
  33. FullSpeed
  34. };
  35. public:
  36. static KResultOr<NonnullOwnPtr<Pipe>> try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval = 0);
  37. Type type() const { return m_type; }
  38. Direction direction() const { return m_direction; }
  39. DeviceSpeed device_speed() const { return m_speed; }
  40. i8 device_address() const { return m_device_address; }
  41. u8 endpoint_address() const { return m_endpoint_address; }
  42. u16 max_packet_size() const { return m_max_packet_size; }
  43. u8 poll_interval() const { return m_poll_interval; }
  44. bool data_toggle() const { return m_data_toggle; }
  45. void set_max_packet_size(u16 max_size) { m_max_packet_size = max_size; }
  46. void set_toggle(bool toggle) { m_data_toggle = toggle; }
  47. void set_device_address(i8 addr) { m_device_address = addr; }
  48. KResultOr<size_t> control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data);
  49. Pipe(USBController const& controller, Type type, Direction direction, u16 max_packet_size);
  50. Pipe(USBController const& controller, Type type, Direction direction, USBEndpointDescriptor& endpoint);
  51. Pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, u8 poll_interval, i8 device_address);
  52. private:
  53. friend class Device;
  54. NonnullRefPtr<USBController> m_controller;
  55. Type m_type;
  56. Direction m_direction;
  57. DeviceSpeed m_speed;
  58. i8 m_device_address { 0 }; // Device address of this pipe
  59. u8 m_endpoint_address { 0 }; // Corresponding endpoint address for this pipe
  60. u16 m_max_packet_size { 0 }; // Max packet size for this pipe
  61. u8 m_poll_interval { 0 }; // Polling interval (in frames)
  62. bool m_data_toggle { false }; // Data toggle for stuffing bit
  63. };
  64. }