1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef _USB_HCD_H 16 #define _USB_HCD_H 17 #include "usb_host.h" 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 #define USB_HCD_NAME "BES_CTROL" 24 #define USB_HCD_ADDR 0x01 25 #define USB_HCD_VERSION 0x00010001 26 #define USB_HCD_MAX_PACKET_LEN (64) 27 #define USB_HCD_AVAILABLE_BANDWIDTH (1024*1024) 28 29 #define USB_HCD_NAME_LEN 16 30 #define USB_HCD_ADDR_LEN 16 31 #define USB_HCD_PORT_SIGNAL_LEN 16 32 33 #define USB_HCD_TOKEN_SETUP 0 34 #define USB_HCD_TOKEN_IN 1 35 #define USB_HCD_TOKEN_OUT 2 36 37 #define USB_HCD_MAX_ENDPOINTS 6 38 #if 0 39 // register GINTSTS 40 // USB HCD Interrupt Status 41 #define USB_HCD_STS_INT 0x0001 /* USB Interrupt */ //PrtInt 42 #define USB_HCD_STS_ERRINT 0x0002 /* USB Error Interrupt */ 43 #define USB_HCD_STS_PCD 0x0004 /* Port Change Detect */ //ConIDStsChng 44 #define USB_HCD_STS_FLR 0x0008 /* Frame List Rellover */ 45 #define USB_HCD_STS_HSE 0x00010 /* Host System Error */ 46 #define USB_HCD_STS_IAA 0x00020 /* Interrupt on Async Advance */ 47 #define USB_HCD_STS_SRI 0x00080 /* SOF - ECHI Derivation */ //Sof 48 #define USB_HCD_STS_HCH 0x01000 /* Host Controller has Halted */ 49 #define USB_HCD_STS_RECL 0x02000 /* Reclamation */ 50 #define USB_HCD_STS_PSS 0x04000 /* Periodic Schedule Status */ 51 #define USB_HCD_STS_ASS 0x08000 /* Asynchronous Schedule Status */ 52 #define USB_HCD_STS_UAI 0x40000 /* USB Asynchronous Interrupt -EHCI derivation */ 53 #define USB_HCD_STS_UPI 0x80000 /* USB Period Interrupt - EHCI derivation */ 54 #endif 55 56 typedef enum { 57 USB_HCD_STATUS_HALT = 0, 58 USB_HCD_STATUS_OPER = 1, 59 USB_HCD_STATUS_DEAD = 2, 60 } USB_HCD_STATUS_T; 61 62 typedef enum { 63 USB_HCD_TYPE_FULL_SPEED = 0, 64 USB_HCD_TYPE_HIGH_SPEED = 1, 65 } USB_HCD_TYPE_T; 66 67 // Register GINTSTS 68 typedef enum { 69 USB_HCD_INTSTS_UNKNOWN = 0, 70 // Current Mode of Operation (CurMod) 71 // 1'b0: Device mode 72 // 1'b1: Host mode 73 USB_HCD_INTSTS_CURMOD = (0x1<<0), 74 75 // * 76 // RxFIFO Non-Empty (RxFLvl) 77 USB_HCD_INTSTS_RXFLVL = (0x1<<4), 78 79 // * 80 // Incomplete Periodic Transfer (incomplP) 81 USB_HCD_INTSTS_FETSUSP = (0x1<<22), 82 83 // * 84 // Host Port Interrupt (PrtInt) 85 USB_HCD_INTSTS_PRTINT = (0x1<<24), 86 87 // * 88 // Host Channels Interrupt (HChInt) 89 USB_HCD_INTSTS_HCHINT = (0x1<<25), 90 91 // Periodic TxFIFO Empty (PTxFEmp) 92 USB_HCD_INTSTS_PTXFEMP = (0x1<<26), 93 94 // * 95 // Connector ID Status Change (ConIDStsChng) 96 USB_HCD_INTSTS_CONLDSTSCHNG = (0x1<<28), 97 98 // * 99 // Disconnect Detected Interrupt (DisconnInt) 100 USB_HCD_INTSTS_DISCONNINT = (0x1<<29), 101 102 // Session Request/New Session Detected Interrupt (SessReqInt) 103 USB_HCD_INTSTS_SESSREQINT = (0x1<<30), 104 105 // Resume/Remote Wakeup Detected Interrupt (WkUpInt) 106 USB_HCD_INTSTS_WKUPINT = (0x1<<31), 107 } USB_HCD_INTSTS_T; 108 109 // Register HPRT 110 typedef enum { 111 USB_PORT_STATUS_UNKNOWN = 0x00, 112 USB_PORT_STATUS_CONNECTION = (0x01 << 0), 113 USB_PORT_STATUS_ENABLE = (0x01 << 1), 114 USB_PORT_STAT_SUSPEND = (0x01 << 2), 115 USB_PORT_STATUS_OVERCURRENT = (0x01 << 3), 116 USB_PORT_STATUS_RESET = (0x01 << 4), 117 USB_PORT_STATUS_POWER = (0x01 << 5), 118 USB_PORT_STATUS_FULL_SPEED = (0x01 << 6), 119 USB_PORT_STATUS_HIGH_SPEED = (0x01 << 7), 120 } USB_PORT_STATUS_T; 121 122 123 typedef enum { 124 USB_HCD_PLUG_STATUS_UNKNOWN, 125 USB_HCD_PLUG_STATUS_IN, 126 USB_HCD_PLUG_STATUS_OUT, 127 } USB_HCD_PLUG_STATUS_T; 128 129 typedef enum { 130 USB_HCD_EP_TYPE_CTRL, 131 USB_HCD_EP_TYPE_ISO, 132 USB_HCD_EP_TYPE_BULK, 133 USB_HCD_EP_TYPE_INT, 134 USB_HCD_EP_TYPE_QTY 135 } USB_HCD_EP_TYPE_T; 136 137 typedef enum 138 { 139 HCD_CMD_DISABLE_CONTROLLER = 0, // USB Host Controller disable. 140 HCD_CMD_GET_PORT_STATUS = 1, // Get USB Port status. 141 HCD_CMD_ENABLE_PORT = 2, // USB Port enable. 142 HCD_CMD_DISABLE_PORT = 3, // USB Port disable. 143 HCD_CMD_POWER_ON_PORT = 4, // USB Host power on. 144 HCD_CMD_POWER_OFF_PORT = 5, // USB Host power off. 145 HCD_CMD_RESUME_PORT = 6, // USB Port resume. 146 HCD_CMD_SUSPEND_PORT = 7, // USB Port suspend. 147 HCD_CMD_RESET_PORT = 8, // USB Port reset. 148 HCD_CMD_GET_FRAME_NUMBER = 9, // Get Frame Number, for Isochronous Transfer. 149 HCD_CMD_SET_FRAME_NUMBER = 10, // Set Frame Number, for Isochronous Transfer. 150 HCD_CMD_TRANSFER_REQUEST = 11, // Transfer Request. 151 HCD_CMD_CONTROL_TRANSFER_REQUEST = 12, // Transfer Request. 152 HCD_CMD_TRANSFER_ABORT = 13, // Transfer Abort. 153 HCD_CMD_CREATE_ENDPOINT = 14, // Create a endpoint. 154 HCD_CMD_DESTROY_ENDPOINT = 15, // Destroy a endpoint. 155 HCD_CMD_RESET_ENDPOINT = 16, // Reset endpoint. 156 HCD_CMD_PROCESS_DONE_QUEUE = 17, // Process a transmission that has already occurred 157 } HCD_CMD_T; 158 159 typedef struct 160 { 161 bool is_used; 162 bool is_in; 163 USBHOST_ENDPOINT_DESCRIPTOR_T endpoint; 164 } HCD_ENDPOINT; 165 166 typedef struct 167 { 168 int device; 169 int ep_number; 170 HCD_ENDPOINT ep[USB_HCD_MAX_ENDPOINTS]; 171 } USB_DEVICE; 172 173 typedef void (*HCD_INTERRUPT_HANDLE_CB) (void); 174 175 typedef void (*HCD_PLUG_HANDLE_CB) (void); 176 typedef void (*HCD_TRANSFER_CALLBACK)(int32_t device,\ 177 int32_t endpoint,\ 178 int32_t status,\ 179 uint8_t* data,\ 180 int32_t length,\ 181 void* user_data); 182 183 typedef struct _USB_HCD { 184 char name[USB_HCD_NAME_LEN + 1]; 185 uint8_t address[USB_HCD_ADDR_LEN]; 186 uint32_t state; 187 uint32_t type; 188 uint32_t otg_capabilities; 189 uint32_t root_hub_port_num; 190 uint32_t root_hub_port_signal[USB_HCD_PORT_SIGNAL_LEN]; 191 uint32_t task_signal; 192 uint32_t (*hcd_entry_func)(struct _USB_HCD *, uint32_t, void *); 193 uint32_t root_hub_device_connection; 194 uint32_t device_num; 195 uint32_t io; 196 uint32_t power_switch; 197 uint32_t available_bandwidth; 198 uint32_t maximum_transfer_request_size; 199 uint32_t version; 200 void *controller_hardware; 201 void *hcd_op; 202 } USB_HCD_T; 203 204 // cmd HCD_CMD_GET_PORT_STATUS Parameter 205 typedef struct _HCD_DEVICE_PORT_T 206 { 207 uint8_t device; 208 uint8_t port; 209 } HCD_DEVICE_PORT_T; 210 211 typedef struct _HCD_DEVICE_EP_T 212 { 213 uint8_t device; 214 uint8_t ep; 215 } HCD_DEVICE_EP_T; 216 217 typedef struct _HCD_HUB_EP_T 218 { 219 uint8_t hub; 220 uint8_t ep; 221 } HCD_HUB_EP_T; 222 223 224 // cmd HCD_CMD_TRANSFER_REQUEST Parameter. 225 typedef struct _HCD_TRANSFER_T 226 { 227 uint8_t device; 228 uint8_t ep; 229 uint8_t flags; 230 uint8_t* data; 231 uint32_t data_len; 232 HCD_TRANSFER_CALLBACK callback; 233 void* user_data; 234 } HCD_TRANSFER_T; 235 236 // cmd HCD_CMD_TRANSFER_REQUEST(CONTROL) Parameter. 237 typedef struct _HCD_CONTROL_TRANSFER_T 238 { 239 uint8_t device; 240 uint8_t ep; 241 int request_type; 242 int request; 243 int value; 244 int index; 245 uint8_t* data; 246 int length; 247 int actual_len; 248 USBHOSTCALLBACK callback; 249 void * user_data; 250 } HCD_CONTROL_TRANSFER_T; 251 252 // cmd HCD_CMD_CREATE_ENDPOINT Parameter 253 typedef struct _HCD_CREATE_ENDPOINT_T 254 { 255 uint8_t device; 256 uint8_t ep; 257 uint8_t attributes; 258 uint32_t max_packet_size; 259 uint32_t interval; 260 } HCD_CREATE_ENDPOINT_T; 261 262 typedef void (*HCD_INTERRUPT_HANDLE_CB) (void); 263 typedef void (*HCD_PLUG_HANDLE_CB) (void); 264 265 int usb_hcd_init(USB_HCD_T * hcd, HCD_INTERRUPT_HANDLE_CB int_handle, HCD_PLUG_HANDLE_CB plug_handle); 266 int usb_register_interrupt_cb(HCD_INTERRUPT_HANDLE_CB handle); 267 int usb_hcd_cmd(USB_HCD_T * hcd,HCD_CMD_T cmd, void* param); 268 int usb_hcd_get_interrupt_status(void); // Get interrupt status, return USB HCD Interrupt Status. 269 int usb_hcd_get_port_status(void); // Get port status, return USB HCD Port Status. 270 int usb_hcd_get_plug_status(void); // Get usb plug status, return USB HCD Plug Status. 271 int usb_hcd_detect(USB_HCD_PLUG_STATUS_T plug_status);// Detect usb device plug staus. 272 void usb_hcd_set_timeout(uint32_t ms); 273 void usb_hcd_log_disable_for_debug(bool disable); 274 int usb_hcd_cmd_get_port_status(USB_HCD_T * hcd); 275 int usb_hcd_cmd_create_point(USB_HCD_T * hcd, uint8_t device, uint8_t port, uint8_t attributes, uint8_t interval, uint8_t max_packet_size); 276 void usb_hcd_cmd_destroy_point(USB_HCD_T * hcd, USB_DEVICE usb_device); 277 void usb_hcd_cmd_disable_controller(USB_HCD_T * hcd); 278 void usb_hcd_cmd_enable_port(USB_HCD_T * hcd); 279 void usb_hcd_cmd_disable_port(USB_HCD_T * hcd); 280 void usb_hcd_cmd_power_on_port(USB_HCD_T * hcd); 281 void usb_hcd_cmd_power_off_port(USB_HCD_T * hcd); 282 void usb_hcd_cmd_resume_port(USB_HCD_T * hcd); 283 void usb_hcd_cmd_suspend_port(USB_HCD_T * hcd); 284 void usb_hcd_cmd_reset_port(USB_HCD_T * hcd); 285 int usb_hcd_cmd_get_frame_number_port(USB_HCD_T * hcd); 286 void usb_hcd_cmd_set_frame_number_port(USB_HCD_T * hcd, uint32_t frame_number); 287 void usb_hcd_cmd_transfer_req(USB_HCD_T * hcd); 288 void usb_hcd_cmd_set_addr(USB_HCD_T * hcd); 289 void usb_hcd_cmd_get_descriptor(USB_HCD_T * hcd, int descriptor_type, uint8_t* buff, int buff_len); 290 void usb_hcd_cmd_set_config(USB_HCD_T * hcd); 291 void usb_hcd_cmd_process_done_queue(USB_HCD_T * hcd); 292 int usb_hcd_cmd_bulk_transfer_req(USB_HCD_T * hcd, int device, int ep, uint8_t* data, int length, HCD_TRANSFER_CALLBACK callback, void* userdata); 293 294 #ifdef __cplusplus 295 } 296 #endif 297 #endif //_USB_HCD_H 298 299