usb_private.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stddef.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <sys/queue.h>
  11. #include "usb/usb_types_ch9.h"
  12. #include "usb/usb_types_stack.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. // ------------------------------------------------------ Types --------------------------------------------------------
  17. typedef struct {
  18. uint8_t *data_buffer;
  19. size_t data_buffer_size;
  20. int num_bytes;
  21. int actual_num_bytes;
  22. uint32_t flags;
  23. usb_device_handle_t device_handle;
  24. uint8_t bEndpointAddress;
  25. usb_transfer_status_t status;
  26. uint32_t timeout;
  27. usb_transfer_cb_t callback;
  28. void *context;
  29. int num_isoc_packets;
  30. usb_isoc_packet_desc_t isoc_packet_desc[0];
  31. } usb_transfer_dummy_t;
  32. _Static_assert(sizeof(usb_transfer_dummy_t) == sizeof(usb_transfer_t), "usb_transfer_dummy_t does not match usb_transfer_t");
  33. struct urb_s {
  34. TAILQ_ENTRY(urb_s) tailq_entry;
  35. // HCD Layer: Handler pointer and variables. Must be initialized to NULL and 0 respectively
  36. void *hcd_ptr;
  37. uint32_t hcd_var;
  38. // Host Lib Layer:
  39. void *usb_host_client; // Currently only used when submitted to shared pipes (i.e., Device default pipes)
  40. size_t usb_host_header_size; // USB Host may need the data buffer to have a transparent header
  41. bool usb_host_inflight; // Debugging variable, used to prevent re-submitting URBs already inflight
  42. // Public transfer structure. Must be last due to variable length array
  43. usb_transfer_t transfer;
  44. };
  45. typedef struct urb_s urb_t;
  46. /**
  47. * @brief Processing request source
  48. *
  49. * Enum to indicate which layer of the USB Host stack requires processing. The main handling loop should then call that
  50. * layer's processing function (i.e., xxx_process()).
  51. */
  52. typedef enum {
  53. USB_PROC_REQ_SOURCE_USBH = 0x01,
  54. USB_PROC_REQ_SOURCE_HUB = 0x02,
  55. } usb_proc_req_source_t;
  56. /**
  57. * @brief Processing request callback
  58. *
  59. * Callback function provided to each layer of the USB Host stack so that each layer can request calls to their
  60. * processing function.
  61. */
  62. typedef bool (*usb_proc_req_cb_t)(usb_proc_req_source_t source, bool in_isr, void *context);
  63. // --------------------------------------------------- Allocation ------------------------------------------------------
  64. /**
  65. * @brief Allocate a URB
  66. *
  67. * - Data buffer is allocated in DMA capable memory
  68. * - The constant fields of the URB are also set
  69. * - The data_buffer field of the URB is set to point to start of the allocated data buffer AFTER the header. To access
  70. * the header, users need a negative offset from data_buffer.
  71. *
  72. * @param data_buffer_size Size of the URB's data buffer
  73. * @param header_size Size of header to put in front of URB's data buffer
  74. * @param num_isoc_packets Number of isochronous packet descriptors
  75. * @return urb_t* URB object
  76. */
  77. urb_t *urb_alloc(size_t data_buffer_size, size_t header_size, int num_isoc_packets);
  78. /**
  79. * @brief Free a URB
  80. *
  81. * @param urb URB object
  82. */
  83. void urb_free(urb_t *urb);
  84. #ifdef __cplusplus
  85. }
  86. #endif