USBEndpoint.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/Bus/USB/USBDescriptors.h>
  8. #include <Kernel/Bus/USB/USBPipe.h>
  9. namespace Kernel::USB {
  10. //
  11. // An endpoint is the "end point" of communication of a USB device. That is, data is read from and written
  12. // to an endpoint via a USB pipe. As an example, during device enumeration (where we assign an address to the
  13. // device), we communicate with the device over the default endpoint, endpoint0, which all devices _must_
  14. // contain to be compliant with the USB specification.
  15. //
  16. // And endpoint describes characteristics about the transfer between the host and the device, such as:
  17. // - The endpoint number
  18. // - Max packet size of send/recv of the endpoint
  19. // - Transfer type (bulk, interrupt, isochronous etc)
  20. //
  21. // Take for example a USB multifunction device, such as a keyboard/mouse combination. The mouse
  22. // may need to be polled every n milliseconds, meaning the transfer may be isochronous (streamed),
  23. // while the keyboard part would only generate data once we push a key (hence an interrupt transfer).
  24. // Each of these data sources would be a _different_ endpoint on the device that we read from.
  25. class USBEndpoint {
  26. public:
  27. static constexpr u8 ENDPOINT_ADDRESS_NUMBER_MASK = 0x0f;
  28. static constexpr u8 ENDPOINT_ADDRESS_DIRECTION_MASK = 0x80;
  29. static constexpr u8 ENDPOINT_ADDRESS_DIRECTION_OUT = 0x00;
  30. static constexpr u8 ENDPOINT_ADDRESS_DIRECTION_IN = 0x80;
  31. static constexpr u8 ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_MASK = 0x03;
  32. static constexpr u8 ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_CONTROL = 0x00;
  33. static constexpr u8 ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_ISOCHRONOUS = 0x01;
  34. static constexpr u8 ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_BULK = 0x02;
  35. static constexpr u8 ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_INTERRUPT = 0x03;
  36. static constexpr u8 ENDPOINT_ATTRIBUTES_ISO_MODE_SYNC_TYPE = 0x0c;
  37. static constexpr u8 ENDPOINT_ATTRIBUTES_ISO_MODE_USAGE_TYPE = 0x30;
  38. const USBEndpointDescriptor& descriptor() const { return m_descriptor; }
  39. bool is_control() const { return (m_descriptor.endpoint_attributes_bitmap & ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_MASK) == ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_CONTROL; }
  40. bool is_isochronous() const { return (m_descriptor.endpoint_attributes_bitmap & ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_MASK) == ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_ISOCHRONOUS; }
  41. bool is_bulk() const { return (m_descriptor.endpoint_attributes_bitmap & ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_MASK) == ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_BULK; }
  42. bool is_interrupt() const { return (m_descriptor.endpoint_attributes_bitmap & ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_MASK) == ENDPOINT_ATTRIBUTES_TRANSFER_TYPE_INTERRUPT; }
  43. u16 max_packet_size() const { return m_descriptor.max_packet_size; }
  44. u8 polling_interval() const { return m_descriptor.poll_interval_in_frames; }
  45. private:
  46. USBEndpoint(/* TODO */);
  47. USBEndpointDescriptor m_descriptor;
  48. Pipe m_pipe;
  49. };
  50. }