1 /* 2 * Copyright (c) 2022, sakumisu 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef USB_HC_H 7 #define USB_HC_H 8 9 #include <stdint.h> 10 11 #ifdef __cplusplus 12 extern "C" { 13 #endif 14 15 typedef void (*usbh_complete_callback_t)(void *arg, int nbytes); 16 typedef void *usbh_pipe_t; 17 18 /** 19 * @brief USB Endpoint Configuration. 20 * 21 * Structure containing the USB endpoint configuration. 22 */ 23 struct usbh_endpoint_cfg { 24 struct usbh_hubport *hport; 25 uint8_t ep_addr; /* Endpoint addr with direction */ 26 uint8_t ep_type; /* Endpoint type */ 27 uint16_t ep_mps; /* Endpoint max packet size */ 28 uint8_t ep_interval; /* Endpoint interval */ 29 uint8_t mult; /* Endpoint additional transcation */ 30 }; 31 32 /** 33 * @brief USB Iso Configuration. 34 * 35 * Structure containing the USB Iso configuration. 36 */ 37 struct usbh_iso_frame_packet { 38 uint8_t *transfer_buffer; 39 uint32_t transfer_buffer_length; 40 uint32_t actual_length; 41 int errorcode; 42 }; 43 44 /** 45 * @brief USB Urb Configuration. 46 * 47 * Structure containing the USB Urb configuration. 48 */ 49 struct usbh_urb { 50 usbh_pipe_t pipe; 51 struct usb_setup_packet *setup; 52 uint8_t *transfer_buffer; 53 uint32_t transfer_buffer_length; 54 int transfer_flags; 55 uint32_t actual_length; 56 uint32_t timeout; 57 int errorcode; 58 uint32_t num_of_iso_packets; 59 uint32_t start_frame; 60 usbh_complete_callback_t complete; 61 void *arg; 62 #if defined(__ICCARM__) || defined(__ICCRISCV__) || defined(__ICCRX__) 63 struct usbh_iso_frame_packet *iso_packet; 64 #else 65 struct usbh_iso_frame_packet iso_packet[0]; 66 #endif 67 }; 68 69 /** 70 * @brief usb host controller hardware init. 71 * 72 * @return On success will return 0, and others indicate fail. 73 */ 74 int usb_hc_init(void); 75 76 /** 77 * @brief Get frame number. 78 * 79 * @return frame number. 80 */ 81 uint16_t usbh_get_frame_number(void); 82 /** 83 * @brief control roothub. 84 * 85 * @param setup setup request buffer. 86 * @param buf buf for reading response or write data. 87 * @return On success will return 0, and others indicate fail. 88 */ 89 int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf); 90 91 /** 92 * @brief reconfig endpoint pipe. 93 * 94 * @param pipe A memory allocated for pipe. 95 * @param dev_addr device address. 96 * @param ep_mps endpoint max packet size. 97 * @param mult endpoint additional transcation 98 * @return On success will return 0, and others indicate fail. 99 */ 100 int usbh_ep_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t mult); 101 102 /** 103 * @brief Allocate pipe for endpoint 104 * 105 * @param pipe A memory location provided by the caller in which to save the allocated pipe. 106 * @param ep_cfg Describes the endpoint info to be allocated. 107 * @return On success will return 0, and others indicate fail. 108 */ 109 int usbh_pipe_alloc(usbh_pipe_t *pipe, const struct usbh_endpoint_cfg *ep_cfg); 110 111 /** 112 * @brief Free a pipe in which saves endpoint info. 113 * 114 * @param pipe A memory location provided by the caller in which to free the allocated endpoint info. 115 * @return On success will return 0, and others indicate fail. 116 */ 117 int usbh_pipe_free(usbh_pipe_t pipe); 118 119 /** 120 * @brief Submit a usb transfer request to an endpoint. 121 * 122 * If timeout is not zero, this function will be in poll transfer mode, 123 * otherwise will be in async transfer mode. 124 * 125 * @param urb Usb request block. 126 * @return On success will return 0, and others indicate fail. 127 */ 128 int usbh_submit_urb(struct usbh_urb *urb); 129 130 /** 131 * @brief Cancel a transfer request. 132 * 133 * This function will call When calls usbh_submit_urb and return -ETIMEOUT or -ESHUTDOWN. 134 * 135 * @param urb Usb request block. 136 * @return On success will return 0, and others indicate fail. 137 */ 138 int usbh_kill_urb(struct usbh_urb *urb); 139 140 #ifdef __cplusplus 141 } 142 #endif 143 144 #endif /* USB_HC_H */ 145