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