1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "esp_heap_caps.h"
8 #include "usb_private.h"
9 #include "usb/usb_types_ch9.h"
10 
urb_alloc(size_t data_buffer_size,size_t header_size,int num_isoc_packets)11 urb_t *urb_alloc(size_t data_buffer_size, size_t header_size, int num_isoc_packets)
12 {
13     urb_t *urb = heap_caps_calloc(1, sizeof(urb_t) + (sizeof(usb_isoc_packet_desc_t) * num_isoc_packets), MALLOC_CAP_DEFAULT);
14     uint8_t *data_buffer = heap_caps_malloc(data_buffer_size + header_size, MALLOC_CAP_DMA);
15     if (urb == NULL || data_buffer == NULL)     {
16         goto err;
17     }
18     urb->usb_host_header_size = header_size;    //Indicate that this URB's data_buffer has a header in front of it.
19     //Case as dummy transfer to write to initialize const fields
20     usb_transfer_dummy_t *dummy_transfer = (usb_transfer_dummy_t *)&urb->transfer;
21     dummy_transfer->data_buffer = (uint8_t *)(data_buffer + header_size);
22     dummy_transfer->data_buffer_size = data_buffer_size;
23     dummy_transfer->num_isoc_packets = num_isoc_packets;
24     return urb;
25 err:
26     heap_caps_free(urb);
27     heap_caps_free(data_buffer);
28     return NULL;
29 }
30 
urb_free(urb_t * urb)31 void urb_free(urb_t *urb)
32 {
33     if (urb == NULL) {
34         return;
35     }
36     heap_caps_free((uint8_t *)(urb->transfer.data_buffer - urb->usb_host_header_size));
37     heap_caps_free(urb);
38 }
39