1 /*************************************************************************** 2 * 3 * Copyright 2015-2020 BES. 4 * All rights reserved. All unpublished rights reserved. 5 * 6 * No part of this work may be used or reproduced in any form or by any 7 * means, or stored in a database or retrieval system, without prior written 8 * permission of BES. 9 * 10 * Use of this work is governed by a license granted by BES. 11 * This work contains confidential and proprietary information of 12 * BES. which is protected by copyright, trade secret, 13 * trademark and other intellectual property rights. 14 * 15 ****************************************************************************/ 16 #ifndef _USB_CONTROLLER_H 17 #define _USB_CONTROLLER_H 18 19 #include "plat_types.h" 20 #include "hal_usbhost.h" 21 22 // Config (default uses x bytes) 23 #define MAX_DEVICES 1 // Max number of devices 24 #define MAX_ENDPOINTS_TOTAL 8 // Max number of endpoints total 25 #define MAX_ENDPOINTS_PER_DEVICE (MAX_ENDPOINTS_TOTAL/MAX_DEVICES)// Max number of endpoints for any one device 26 27 // USB host structures 28 29 #define TOKEN_SETUP 0 30 #define TOKEN_IN 1 31 #define TOKEN_OUT 2 32 33 // Status flags from hub 34 #define PORT_CONNECTION 0 35 #define PORT_ENABLE 1 36 #define PORT_SUSPEND 2 37 #define PORT_OVER_CURRENT 3 38 #define PORT_RESET 4 39 #define PORT_POWER 8 40 #define PORT_LOW_SPEED 9 41 42 #define C_PORT_CONNECTION 16 43 #define C_PORT_ENABLE 17 44 #define C_PORT_SUSPEND 18 45 #define C_PORT_OVER_CURRENT 19 46 #define C_PORT_RESET 20 47 48 #define DEVICE_TO_HOST 0x80 49 #define HOST_TO_DEVICE 0x00 50 #define REQUEST_TYPE_CLASS 0x20 51 #define RECIPIENT_DEVICE 0x00 52 #define RECIPIENT_INTERFACE 0x01 53 #define RECIPIENT_ENDPOINT 0x02 54 #define RECIPIENT_OTHER 0x03 55 56 #define GET_STATUS 0 57 #define CLEAR_FEATURE 1 58 #define SET_FEATURE 3 59 #define SET_ADDRESS 5 60 #define GET_DESCRIPTOR 6 61 #define SET_DESCRIPTOR 7 62 #define GET_CONFIGURATION 8 63 #define SET_CONFIGURATION 9 64 #define GET_INTERFACE 10 65 #define SET_INTERFACE 11 66 #define SYNCH_FRAME 11 67 68 //=================================================================== 69 //=================================================================== 70 // Hardware defines 71 #define USBHOST_CURRENT_CONNECT_STATUS 0x00000001 72 #define USBHOST_PORT_RESET_STATUS 0x00000010 73 #define CONNECT_STATUS_CHANGE (USBHOST_CURRENT_CONNECT_STATUS << 16) 74 #define PORT_RESET_STATUS_CHANGE (USBHOST_PORT_RESET_STATUS << 16) 75 76 #define TD_ROUNDING (uint32_t)0x00040000 77 #define TD_SETUP (uint32_t)0x00000000 78 #define TD_IN (uint32_t)0x00100000 79 #define TD_OUT (uint32_t)0x00080000 80 #define TD_DELAY_INT(x) (uint32_t)((x) << 21) 81 #define TD_TOGGLE_0 (uint32_t)0x02000000 82 #define TD_TOGGLE_1 (uint32_t)0x03000000 83 #define TD_CC (uint32_t)0xF0000000 84 85 86 #define SKIP_CLEANUP_HW_CHAN (1 << 0) 87 #define SKIP_CLEANUP_CLOSE (1 << 1) 88 #define SKIP_CLEANUP_FILE_SYSTEM (1 << 2) 89 90 #define USB_ENDPORT_DMA_BUFFER_LEN 1024 91 enum USB_ENDPORT_STATE_T 92 { 93 USB_ENDPORT_STATE_FREE, 94 USB_ENDPORT_STATE_NOTQUEUED, 95 USB_ENDPORT_STATE_IDLE, 96 USB_ENDPORT_STATE_SETUPQUEUED, 97 USB_ENDPORT_STATE_DATAQUEUED, 98 USB_ENDPORT_STATE_STATUSQUEUED, 99 USB_ENDPORT_STATE_CALLBACK_PENDING, 100 // USB_ENDPORT_STATE_WAIT_PROCESS_DONE, 101 }; 102 103 // HostController EndPoint Descriptor 104 typedef struct { 105 volatile uint32_t control; 106 volatile uint32_t tail_td; 107 volatile uint32_t head_td; 108 volatile uint32_t next; 109 } HCED_T; 110 111 // HostController Transfer Descriptor 112 typedef struct { 113 volatile uint32_t control; 114 volatile uint32_t curr_buf_ptr; 115 volatile uint32_t next; 116 volatile uint32_t bufend; 117 } HCTD_T; 118 119 // Host Controller Communication Area 120 typedef struct { 121 volatile uint32_t InterruptTable[32]; 122 volatile uint16_t FrameNumber; 123 volatile uint16_t FrameNumberPad; 124 volatile uint32_t DoneHead; 125 volatile uint8_t Reserved[120]; 126 } HCCA; 127 128 enum USBHOST_CLASS_CODE_T 129 { 130 USB_CLASS_DEVICE = 0x0, 131 USB_CLASS_AUDIO = 0x01, 132 USB_CLASS_COMM = 0x02, 133 USB_CLASS_HID = 0x03, 134 USB_CLASS_PHYSICAL = 0x05, 135 USB_CLASS_IMAGING = 0x06, 136 USB_CLASS_PRINTER = 0x07, 137 USB_CLASS_MASS_STORAGE = 0x08, 138 USB_CLASS_HUB = 0x09, 139 USB_CLASS_DATA = 0x0a, 140 USB_CLASS_SMART_CARD = 0x0b, 141 USB_CLASS_CONTENT_SECURITY = 0x0d, 142 USB_CLASS_VIDEO = 0x0e, 143 USB_CALSS_PERSONAL_HEALTHCARE = 0x0f, 144 USB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, 145 USB_CLASS_WIRELESS = 0xe0, 146 USB_CLASS_MISCELLANEOUS = 0xef, 147 USB_CLASS_APP_SPECIFIC = 0xfe, 148 USB_CLASS_VENDOR_SPECIFIC = 0xff 149 }; 150 151 enum USBHOST_DESCRIPTOR_TYPE 152 { 153 DESCRIPTOR_TYPE_DEVICE = 0x01, 154 DESCRIPTOR_TYPE_CONFIG = 0x02, 155 DESCRIPTOR_TYPE_STRING = 0x03, 156 DESCRIPTOR_TYPE_INTERFACE = 0x04, 157 DESCRIPTOR_TYPE_ENDPOINT = 0x05, 158 DESCRIPTOR_TYPE_BOS = 0x0f, 159 DESCRIPTOR_TYPE_DEVICE_CAPABILITY = 0x10, 160 DESCRIPTOR_TYPE_DEVICE_HID = 0x21, 161 DESCRIPTOR_TYPE_DEVICE_REPORT = 0x22, 162 DESCRIPTOR_TYPE_DEVICE_PHYSICAL = 0x23, 163 DESCRIPTOR_TYPE_DEVICE_HUB = 0x29, 164 DESCRIPTOR_TYPE_DEVICE_SUPERSPEED_HUB = 0x2a, 165 DESCRIPTOR_TYPE_DEVICE_ENDPOINT_COMPANION = 0x2a, 166 }; 167 168 enum USBHOST_ENDPOINT_DIRECTION_T 169 { 170 USB_ENDPOINT_DIRECTION_IN = 0x80, 171 USB_ENDPOINT_DIRECTION_OUT = 0x0, 172 }; 173 174 enum USBHOST_TRANSFER_TYPE_T 175 { 176 USB_TRANSFER_TYPE_CONTROL = 0, 177 USB_TRANSFER_TYPE_ISOCHRONOUS = 1, 178 USB_TRANSFER_TYPE_BULK = 2, 179 USB_TRANSFER_TYPE_INTERRUPT = 3, 180 }; 181 182 enum USBHOST_REQUEST_T 183 { 184 USB_REQUEST_GET_STATUS = 0x00, 185 USB_REQUEST_CLEAR_FEATURE = 0x01, 186 USB_REQUEST_SET_FEATURE = 0x03, 187 USB_REQUEST_SET_ADDRESS = 0x05, 188 USB_REQUEST_GET_DESCRIPTOR = 0x06, 189 USB_REQUEST_SET_DESCRIPTOR = 0x07, 190 USB_REQUEST_GET_CONFIGURATION = 0x08, 191 USB_REQUEST_SET_CONFIGURATION = 0x09, 192 USB_REQUEST_GET_INTERFACE = 0x0a, 193 USB_REQUEST_SET_INTERFACE = 0x0b, 194 USB_REQUEST_SYNCH_FRAME = 0x0c, 195 USB_REQUEST_SET_SEL = 0x30, 196 USB_REQUEST_SET_ISOCH_DELAY = 0x31, 197 }; 198 199 enum USBHOST_REQUEST_RECIPIENT_T 200 { 201 USB_REQUEST_RECIPIENT_DEVICE = 0x00, 202 USB_REQUEST_RECIPIENT_INTERFACE = 0x01, 203 USB_REQUEST_RECIPIENT_ENDPOINT = 0x02, 204 USB_REQUEST_RECIPIENT_OTHER = 0x03, 205 }; 206 207 208 typedef struct 209 { 210 uint8_t length; 211 uint8_t descriptor_type; 212 uint16_t cdusb; 213 uint8_t device_class; 214 uint8_t device_sub_class; 215 uint8_t device_protocol; 216 uint8_t max_packet_size; 217 uint16_t vendor; 218 uint16_t dproduct; 219 uint16_t cddevice; 220 uint8_t manu_facturer; 221 uint8_t product; 222 uint8_t serial_number; 223 uint8_t num_configurations; 224 } USBHOST_DEVICE_DESCRIPTOR_T; 225 226 typedef struct 227 { 228 uint8_t length; 229 uint8_t descriptor_type; 230 uint16_t total_length; 231 uint8_t num_interface; 232 uint8_t configuration_value; 233 uint8_t configuration; 234 uint8_t attributes; 235 uint8_t max_power; 236 } USBHOST_CONFIGURATION_DICRIPTOR_T; 237 238 typedef struct 239 { 240 uint8_t length; 241 uint8_t descriptor_type; 242 uint8_t interface_number; 243 uint8_t alternate_setting; 244 uint8_t num_endpoints; 245 uint8_t interface_class; 246 uint8_t interface_sub_class; 247 uint8_t interface_protocol; 248 uint8_t interface; 249 } USBHOST_INTERFACE_DESCRIPTOR_T; 250 251 typedef struct 252 { 253 uint8_t length; 254 uint8_t descriptor_type; 255 uint8_t endpoint_address; 256 uint8_t attributes; 257 uint16_t max_packet_size; 258 uint8_t interval; 259 /* NOTE: these two are _only_ in audio endpoints. */ 260 /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */ 261 uint8_t refresh; 262 uint8_t synch_address; 263 } USBHOST_ENDPOINT_DESCRIPTOR_T; 264 265 typedef struct { 266 uint8_t length; 267 uint8_t descriptor_type; 268 uint16_t bcdhid; 269 uint8_t country_code; 270 uint8_t num_descriptors; 271 uint8_t descriptor_type2; 272 uint16_t descriptor_length; 273 } USBHOST_HID_DESCRIPTOR_T; 274 275 typedef struct { 276 uint8_t length; 277 uint8_t descriptor_type; 278 uint8_t string[1]; 279 } USBHOST_STRING_DESCRIPTION_T; 280 281 typedef void (*USBHOST_INTERRUPT_HANDLE_CB) (void); 282 typedef void (*USBHOST_PLUG_HANDLE_CB) (void); 283 284 typedef void (*USBHOST_CALLBACK)(int32_t device,\ 285 int32_t endpoint,\ 286 int32_t status,\ 287 uint8_t* data,\ 288 int32_t len,\ 289 void* user_data); 290 291 typedef struct { 292 uint8_t chan; 293 uint8_t chan_ctrl_in; 294 uint32_t dma_buff[USB_ENDPORT_DMA_BUFFER_LEN]; 295 uint32_t dma_buff_len; 296 bool first_xfer; 297 298 HCED_T descriptor; 299 HCTD_T td_head; 300 volatile uint8_t current_state; 301 bool wait_process_done_queue; 302 uint8_t flags; 303 uint16_t length; 304 uint8_t* data; 305 USBHOST_CALLBACK callback; 306 void* user_data; 307 }USBHOST_ENDPOINT_T; 308 309 typedef struct { 310 uint8_t bm_request_type; 311 uint8_t b_request; 312 uint16_t w_value; 313 uint16_t w_index; 314 uint16_t w_length; 315 } USBHOST_SETUP_T; 316 317 typedef struct { 318 uint8_t endpoint_map[MAX_ENDPOINTS_PER_DEVICE*2]; 319 uint8_t hub; 320 uint8_t port; 321 uint8_t addr; 322 uint8_t pad; 323 324 // Only if this device is a hub 325 uint8_t hub_port_count; // nonzero if this is a hub 326 uint8_t hub_interrupt_data; 327 uint8_t hub_map; 328 uint8_t hub_mask; 329 330 int32_t flags; // 1 = Disconnected 331 332 USBHOST_SETUP_T setup_buffer; 333 }USBHOST_DEVICE_T; 334 335 typedef struct { 336 HCCA communication_area; 337 USBHOST_ENDPOINT_T endpoints[MAX_ENDPOINTS_TOTAL]; 338 USBHOST_ENDPOINT_T endpoint_zero; 339 HCTD_T common_tail; 340 USBHOST_SETUP_T setup_zero; 341 342 USBHOST_DEVICE_T devices[MAX_DEVICES]; 343 uint32_t frame_number; // 32 bit ms counter 344 345 uint8_t callbacks_pending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue 346 uint8_t root_hub_status_change; // Root hub status has changed, set from ISR 347 enum HAL_USBHOST_PORT_EVENT_T root_hub_event; 348 uint8_t unused0; 349 uint8_t unused1; 350 351 uint8_t connect_pending; // Reset has initiated a connect 352 uint32_t connect_countdown; // Number of ms left after port is enabled before we can connect 353 uint32_t disconn_countdown; // Number of ms left after reset in case of no events, so that we can disconnect 354 uint8_t connect_hub; // Will connect on this hub 355 uint8_t connect_port; // ... and this port 356 uint8_t skip_cleanup_bitmap; 357 uint8_t plug_status_change; 358 enum HAL_USBHOST_PLUG_STATUS_T plug_status; 359 #ifdef USB_SLEEP_TEST 360 uint32_t sleep_count_down; 361 uint32_t wakeup_count_down; 362 uint32_t fs_test_count_down; 363 #endif 364 USBHOST_INTERRUPT_HANDLE_CB interrupt_cb; 365 USBHOST_PLUG_HANDLE_CB plug_cb; 366 bool is_open; 367 }USBHOST_T; 368 369 370 int usbhost_add_endpoint(int device, 371 int ep, 372 int attributes, 373 int max_packet_size, 374 int interval); 375 376 int usbhost_add_endpoint_by_desc(int device, 377 USBHOST_ENDPOINT_DESCRIPTOR_T* ed); 378 379 int usbhost_remove_endpoint(int device, int ep); 380 381 USBHOST_ENDPOINT_T* usbhost_get_endpoint(int device, int ep); 382 383 void usbhost_set_interrupt_cb(void *cb); 384 385 int usbhost_release(USBHOST_ENDPOINT_T* endpoint); 386 387 void usbhost_process_done_queue(void); 388 389 int usbhost_add_device(int hub, int port, bool isLowSpeed); 390 391 int usbhost_init(USBHOST_INTERRUPT_HANDLE_CB interrupt_cb,USBHOST_PLUG_HANDLE_CB plug_cb); 392 393 int usbhost_deinit(void); 394 395 void usbhost_loop(void); 396 397 USBHOST_T* usbhost_get_op(void); 398 399 int32_t usbhost_get_frame_number(uint32_t *number); 400 401 int32_t usbhost_set_frame_number(uint32_t number); 402 403 int32_t usbhost_reset_port(int hub, int port); 404 405 int32_t usbhost_resume_port(int hub, int port); 406 407 int32_t usbhost_suspend_port(int hub, int port); 408 409 int32_t usbhost_disconnect(int hub, int port); 410 411 // called after reset 412 void usbhost_connect(int hub, int port, bool lowspeed); 413 414 void usbhost_reconnect_cleanup(int hub, int port); 415 416 // Called from interrupt 417 void usbhost_hub_status_change(int hub, int port, uint32_t status); 418 419 int usbhost_wait_io_done(int device, int ep); 420 421 int32_t usbhost_transfer(int device, int ep, uint8_t flags, uint8_t* data, int length, USBHOST_CALLBACK callback, void* user_data); 422 423 int32_t usbhost_control_transfer(int device, int ep, int request_type, int request, int value, int index, uint8_t* data, int length, int *actual_len, USBHOST_CALLBACK callback, void * user_data); 424 425 int32_t usbhost_interrupt_transfer(int device, int ep, uint8_t* data, int length, USBHOST_CALLBACK callback, void* user_data); 426 427 int32_t usbhost_bulk_transfer(int device, int ep, uint8_t* data, int length, USBHOST_CALLBACK callback, void* user_data); 428 429 int32_t usbhost_transfer_abort(int device, int ep); 430 431 int32_t usbhost_getdescriptor(int device, int descType,int descIndex, uint8_t* data, int length); 432 433 int32_t usbhost_get_string(int device, int index, char* dst, int length); 434 435 int32_t usbhost_set_address(int device, int new_addr); 436 437 int32_t usbhost_set_configuration(int device, int configNum); 438 439 int32_t usbhost_set_interface(int device, int ifNum, int altNum); 440 441 // HUB stuff 442 int32_t usbhost_set_port_feature(int device, int feature, int index); 443 444 int32_t usbhost_clear_port_feature(int device, int feature, int index); 445 446 int32_t usbhost_set_port_power(int device, int port); 447 448 int32_t usbhost_set_port_reset(int device, int port); 449 450 int32_t usbhost_get_port_status(int device, int port, uint32_t* status); 451 452 uint32_t usbhost_hcd_get_port_status(void); 453 454 uint32_t usbhost_hcd_get_interrupt_status(void); 455 456 uint32_t usbhost_hcd_get_plug_status(void); 457 458 uint32_t usbhost_hcd_usb_usb_detect(uint32_t plug_status); 459 460 int32_t usbhost_hcd_set_port_enable(void); 461 462 int32_t usbhost_hcd_set_port_disable(void); 463 464 int32_t usbhost_hcd_set_port_poweron(void); 465 466 int32_t usbhost_hcd_set_port_poweroff(void); 467 468 int32_t usbhost_hcd_set_port_resume(void); 469 470 int32_t usbhost_hcd_set_port_suspend(void); 471 472 int32_t usbhost_hcd_set_port_reset(void); 473 474 int32_t usbhost_plug_thread_start(void); 475 476 int32_t usbhost_hcd_set_timeout(uint32_t ms); 477 478 int32_t usbhost_hcd_get_mps_size(int device, int ep); 479 480 #define ERR_IO_PENDING -100 481 #define ERR_ENDPOINT_NONE_LEFT -101 482 #define ERR_ENDPOINT_NOT_FOUND -102 483 #define ERR_DEVICE_NOT_FOUND -103 484 #define ERR_DEVICE_NONE_LEFT -104 485 #define ERR_HUB_INIT_FAILED -105 486 #define ERR_INTERFACE_NOT_FOUND -106 487 #define ERR_DATA_SIZE_IS_ZERO -107 488 #define ERR_DATA_SIZE_TOO_LARGE -108 489 #define ERR_DATA_SIZE_NOT_ALIGNMENT -109 490 491 #endif 492