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 | /** \ingroup group_class |
| 28 | * \defgroup ClassDriver_Hub Hub (Host only) |
| 29 | * \details Like most PC's OS, Hub support is completely hidden from Application. In fact, application cannot determine whether |
| 30 | * a device is mounted directly via roothub or via a hub's port. All Hub-related procedures are performed and managed |
| 31 | * by tinyusb stack. Unless you are trying to develop the stack itself, there are nothing else can be used by Application. |
| 32 | * \note Due to my laziness, only 1-level of Hub is supported. In other way, the stack cannot mount a hub via another hub. |
| 33 | * @{ |
| 34 | */ |
| 35 | |
| 36 | #ifndef _TUSB_HUB_H_ |
| 37 | #define _TUSB_HUB_H_ |
| 38 | |
| 39 | #include "common/tusb_common.h" |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | //D1...D0: Logical Power Switching Mode |
| 46 | //00: Ganged power switching (all ports’power at |
| 47 | //once) |
| 48 | //01: Individual port power switching |
| 49 | //1X: Reserved. Used only on 1.0 compliant hubs |
| 50 | //that implement no power switching |
| 51 | //D2: Identifies a Compound Device |
| 52 | //0: Hub is not part of a compound device. |
| 53 | //1: Hub is part of a compound device. |
| 54 | //D4...D3: Over-current Protection Mode |
| 55 | //00: Global Over-current Protection. The hub |
| 56 | //reports over-current as a summation of all |
| 57 | //ports’current draw, without a breakdown of |
| 58 | //individual port over-current status. |
| 59 | //01: Individual Port Over-current Protection. The |
| 60 | //hub reports over-current on a per-port basis. |
| 61 | //Each port has an over-current status. |
| 62 | //1X: No Over-current Protection. This option is |
| 63 | //allowed only for bus-powered hubs that do not |
| 64 | //implement over-current protection. |
| 65 | // |
| 66 | //D6...D5: TT Think TIme |
| 67 | //00: TT requires at most 8 FS bit times of inter |
| 68 | //transaction gap on a full-/low-speed |
| 69 | //downstream bus. |
| 70 | //01: TT requires at most 16 FS bit times. |
| 71 | //10: TT requires at most 24 FS bit times. |
| 72 | //11: TT requires at most 32 FS bit times. |
| 73 | //D7: Port Indicators Supported |
| 74 | //0: Port Indicators are not supported on its |
| 75 | //downstream facing ports and the |
| 76 | //PORT_INDICATOR request has no effect. |
| 77 | //1: Port Indicators are supported on its |
| 78 | //downstream facing ports and the |
| 79 | //PORT_INDICATOR request controls the |
| 80 | //indicators. See Section 11.5.3. |
| 81 | //D15...D8: Reserved |
| 82 | |
| 83 | typedef struct TU_ATTR_PACKED{ |
| 84 | uint8_t bLength ; ///< Size of descriptor |
| 85 | uint8_t bDescriptorType ; ///< Other_speed_Configuration Type |
| 86 | uint8_t bNbrPorts; |
| 87 | uint16_t wHubCharacteristics; |
| 88 | uint8_t bPwrOn2PwrGood; |
| 89 | uint8_t bHubContrCurrent; |
| 90 | uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1) |
| 91 | uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff |
| 92 | } descriptor_hub_desc_t; |
| 93 | |
| 94 | TU_VERIFY_STATIC( sizeof(descriptor_hub_desc_t) == 9, "size is not correct"); |
| 95 | |
| 96 | enum { |
| 97 | HUB_REQUEST_GET_STATUS = 0 , |
| 98 | HUB_REQUEST_CLEAR_FEATURE = 1 , |
| 99 | |
| 100 | HUB_REQUEST_SET_FEATURE = 3 , |
| 101 | |
| 102 | HUB_REQUEST_GET_DESCRIPTOR = 6 , |
| 103 | HUB_REQUEST_SET_DESCRIPTOR = 7 , |
| 104 | HUB_REQUEST_CLEAR_TT_BUFFER = 8 , |
| 105 | HUB_REQUEST_RESET_TT = 9 , |
| 106 | HUB_REQUEST_GET_TT_STATE = 10 , |
| 107 | HUB_REQUEST_STOP_TT = 11 |
| 108 | }; |
| 109 | |
| 110 | enum { |
| 111 | HUB_FEATURE_HUB_LOCAL_POWER_CHANGE = 0, |
| 112 | HUB_FEATURE_HUB_OVER_CURRENT_CHANGE |
| 113 | }; |
| 114 | |
| 115 | enum{ |
| 116 | HUB_FEATURE_PORT_CONNECTION = 0, |
| 117 | HUB_FEATURE_PORT_ENABLE = 1, |
| 118 | HUB_FEATURE_PORT_SUSPEND = 2, |
| 119 | HUB_FEATURE_PORT_OVER_CURRENT = 3, |
| 120 | HUB_FEATURE_PORT_RESET = 4, |
| 121 | |
| 122 | HUB_FEATURE_PORT_POWER = 8, |
| 123 | HUB_FEATURE_PORT_LOW_SPEED = 9, |
| 124 | |
| 125 | HUB_FEATURE_PORT_CONNECTION_CHANGE = 16, |
| 126 | HUB_FEATURE_PORT_ENABLE_CHANGE = 17, |
| 127 | HUB_FEATURE_PORT_SUSPEND_CHANGE = 18, |
| 128 | HUB_FEATURE_PORT_OVER_CURRENT_CHANGE = 19, |
| 129 | HUB_FEATURE_PORT_RESET_CHANGE = 20, |
| 130 | HUB_FEATURE_PORT_TEST = 21, |
| 131 | HUB_FEATURE_PORT_INDICATOR = 22 |
| 132 | }; |
| 133 | |
| 134 | // data in response of HUB_REQUEST_GET_STATUS, wIndex = 0 (hub) |
| 135 | typedef struct { |
| 136 | union{ |
| 137 | struct TU_ATTR_PACKED { |
| 138 | uint16_t local_power_source : 1; |
| 139 | uint16_t over_current : 1; |
| 140 | uint16_t : 14; |
| 141 | }; |
| 142 | |
| 143 | uint16_t value; |
| 144 | } status, change; |
| 145 | } hub_status_response_t; |
| 146 | |
| 147 | TU_VERIFY_STATIC( sizeof(hub_status_response_t) == 4, "size is not correct"); |
| 148 | |
| 149 | // data in response of HUB_REQUEST_GET_STATUS, wIndex = Port num |
| 150 | typedef struct { |
| 151 | union { |
| 152 | struct TU_ATTR_PACKED { |
| 153 | uint16_t connection : 1; |
| 154 | uint16_t port_enable : 1; |
| 155 | uint16_t suspend : 1; |
| 156 | uint16_t over_current : 1; |
| 157 | uint16_t reset : 1; |
| 158 | |
| 159 | uint16_t : 3; |
| 160 | uint16_t port_power : 1; |
| 161 | uint16_t low_speed : 1; |
| 162 | uint16_t high_speed : 1; |
| 163 | uint16_t port_test_mode : 1; |
| 164 | uint16_t port_indicator_control : 1; |
| 165 | uint16_t TU_RESERVED : 3; |
| 166 | }; |
| 167 | |
| 168 | uint16_t value; |
| 169 | } status, change; |
| 170 | } hub_port_status_response_t; |
| 171 | |
| 172 | TU_VERIFY_STATIC( sizeof(hub_port_status_response_t) == 4, "size is not correct"); |
| 173 | |
| 174 | bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, tuh_control_complete_cb_t complete_cb); |
| 175 | bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, tuh_control_complete_cb_t complete_cb); |
| 176 | |
| 177 | bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_control_complete_cb_t complete_cb); |
| 178 | bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, tuh_control_complete_cb_t complete_cb); |
| 179 | bool hub_status_pipe_queue(uint8_t dev_addr); |
| 180 | |
| 181 | //--------------------------------------------------------------------+ |
| 182 | // Internal Class Driver API |
| 183 | //--------------------------------------------------------------------+ |
| 184 | void hub_init (void); |
| 185 | bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); |
| 186 | bool hub_set_config (uint8_t dev_addr, uint8_t itf_num); |
| 187 | bool hub_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); |
| 188 | void hub_close (uint8_t dev_addr); |
| 189 | |
| 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #endif /* _TUSB_HUB_H_ */ |
| 195 | |
| 196 | /** @} */ |