Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _TUSB_HCD_H_ |
| 28 | #define _TUSB_HCD_H_ |
| 29 | |
| 30 | #include "common/tusb_common.h" |
| 31 | #include "osal/osal.h" |
| 32 | #include "common/tusb_fifo.h" |
| 33 | #include "hcd_attr.h" |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | //--------------------------------------------------------------------+ |
| 40 | // MACRO CONSTANT TYPEDEF |
| 41 | //--------------------------------------------------------------------+ |
| 42 | typedef enum |
| 43 | { |
| 44 | HCD_EVENT_DEVICE_ATTACH, |
| 45 | HCD_EVENT_DEVICE_REMOVE, |
| 46 | HCD_EVENT_XFER_COMPLETE, |
| 47 | |
| 48 | // Not an HCD event, just a convenient way to defer ISR function |
| 49 | USBH_EVENT_FUNC_CALL, |
| 50 | |
| 51 | HCD_EVENT_COUNT |
| 52 | } hcd_eventid_t; |
| 53 | |
| 54 | typedef struct |
| 55 | { |
| 56 | uint8_t rhport; |
| 57 | uint8_t event_id; |
| 58 | uint8_t dev_addr; |
| 59 | |
| 60 | union |
| 61 | { |
| 62 | // Attach, Remove |
| 63 | struct { |
| 64 | uint8_t hub_addr; |
| 65 | uint8_t hub_port; |
| 66 | uint8_t speed; |
| 67 | } connection; |
| 68 | |
| 69 | // XFER_COMPLETE |
| 70 | struct { |
| 71 | uint8_t ep_addr; |
| 72 | uint8_t result; |
| 73 | uint32_t len; |
| 74 | } xfer_complete; |
| 75 | |
| 76 | // FUNC_CALL |
| 77 | struct { |
| 78 | void (*func) (void*); |
| 79 | void* param; |
| 80 | }func_call; |
| 81 | }; |
| 82 | |
| 83 | } hcd_event_t; |
| 84 | |
| 85 | #if TUSB_OPT_HOST_ENABLED |
| 86 | // Max number of endpoints per device |
| 87 | enum { |
| 88 | // TODO better computation |
| 89 | HCD_MAX_ENDPOINT = CFG_TUH_DEVICE_MAX*(CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3), |
| 90 | HCD_MAX_XFER = HCD_MAX_ENDPOINT*2, |
| 91 | }; |
| 92 | |
| 93 | //#define HCD_MAX_ENDPOINT 16 |
| 94 | //#define HCD_MAX_XFER 16 |
| 95 | |
| 96 | typedef struct { |
| 97 | uint8_t rhport; |
| 98 | uint8_t hub_addr; |
| 99 | uint8_t hub_port; |
| 100 | uint8_t speed; |
| 101 | } hcd_devtree_info_t; |
| 102 | |
| 103 | #endif |
| 104 | |
| 105 | //--------------------------------------------------------------------+ |
| 106 | // Controller API |
| 107 | //--------------------------------------------------------------------+ |
| 108 | |
| 109 | // Initialize controller to host mode |
| 110 | bool hcd_init(uint8_t rhport); |
| 111 | |
| 112 | // Interrupt Handler |
| 113 | void hcd_int_handler(uint8_t rhport); |
| 114 | |
| 115 | // Enable USB interrupt |
| 116 | void hcd_int_enable (uint8_t rhport); |
| 117 | |
| 118 | // Disable USB interrupt |
| 119 | void hcd_int_disable(uint8_t rhport); |
| 120 | |
| 121 | // Get frame number (1ms) |
| 122 | uint32_t hcd_frame_number(uint8_t rhport); |
| 123 | |
| 124 | //--------------------------------------------------------------------+ |
| 125 | // Port API |
| 126 | //--------------------------------------------------------------------+ |
| 127 | |
| 128 | // Get the current connect status of roothub port |
| 129 | bool hcd_port_connect_status(uint8_t rhport); |
| 130 | |
| 131 | // Reset USB bus on the port |
| 132 | void hcd_port_reset(uint8_t rhport); |
| 133 | |
| 134 | // TODO implement later |
| 135 | void hcd_port_reset_end(uint8_t rhport); |
| 136 | |
| 137 | // Get port link speed |
| 138 | tusb_speed_t hcd_port_speed_get(uint8_t rhport); |
| 139 | |
| 140 | // HCD closes all opened endpoints belong to this device |
| 141 | void hcd_device_close(uint8_t rhport, uint8_t dev_addr); |
| 142 | |
| 143 | //--------------------------------------------------------------------+ |
| 144 | // Endpoints API |
| 145 | //--------------------------------------------------------------------+ |
| 146 | |
| 147 | bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); |
| 148 | bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); |
| 149 | bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen); |
| 150 | bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr); |
| 151 | |
| 152 | //--------------------------------------------------------------------+ |
| 153 | // USBH implemented API |
| 154 | //--------------------------------------------------------------------+ |
| 155 | |
| 156 | // Get device tree information of a device |
| 157 | // USB device tree can be complicated and manged by USBH, this help HCD to retrieve |
| 158 | // needed topology info to carry out its work |
| 159 | extern void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info); |
| 160 | |
| 161 | //------------- Event API -------------// |
| 162 | |
| 163 | // Called by HCD to notify stack |
| 164 | extern void hcd_event_handler(hcd_event_t const* event, bool in_isr); |
| 165 | |
| 166 | // Helper to send device attach event |
| 167 | extern void hcd_event_device_attach(uint8_t rhport, bool in_isr); |
| 168 | |
| 169 | // Helper to send device removal event |
| 170 | extern void hcd_event_device_remove(uint8_t rhport, bool in_isr); |
| 171 | |
| 172 | // Helper to send USB transfer event |
| 173 | extern void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr); |
| 174 | |
| 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif /* _TUSB_HCD_H_ */ |