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 | #include "tusb_option.h" |
| 28 | |
| 29 | #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_HUB) |
| 30 | |
| 31 | #include "usbh.h" |
| 32 | #include "usbh_classdriver.h" |
| 33 | #include "hub.h" |
| 34 | |
| 35 | //--------------------------------------------------------------------+ |
| 36 | // MACRO CONSTANT TYPEDEF |
| 37 | //--------------------------------------------------------------------+ |
| 38 | typedef struct |
| 39 | { |
| 40 | uint8_t itf_num; |
| 41 | uint8_t ep_in; |
| 42 | uint8_t port_count; |
| 43 | uint8_t status_change; // data from status change interrupt endpoint |
| 44 | |
| 45 | hub_port_status_response_t port_status; |
| 46 | } hub_interface_t; |
| 47 | |
| 48 | CFG_TUSB_MEM_SECTION static hub_interface_t hub_data[CFG_TUH_HUB]; |
| 49 | CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4) static uint8_t _hub_buffer[sizeof(descriptor_hub_desc_t)]; |
| 50 | |
| 51 | TU_ATTR_ALWAYS_INLINE |
| 52 | static inline hub_interface_t* get_itf(uint8_t dev_addr) |
| 53 | { |
| 54 | return &hub_data[dev_addr-1-CFG_TUH_DEVICE_MAX]; |
| 55 | } |
| 56 | |
| 57 | #if CFG_TUSB_DEBUG |
| 58 | static char const* const _hub_feature_str[] = |
| 59 | { |
| 60 | [HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION", |
| 61 | [HUB_FEATURE_PORT_ENABLE ] = "PORT_ENABLE", |
| 62 | [HUB_FEATURE_PORT_SUSPEND ] = "PORT_SUSPEND", |
| 63 | [HUB_FEATURE_PORT_OVER_CURRENT ] = "PORT_OVER_CURRENT", |
| 64 | [HUB_FEATURE_PORT_RESET ] = "PORT_RESET", |
| 65 | [HUB_FEATURE_PORT_POWER ] = "PORT_POWER", |
| 66 | [HUB_FEATURE_PORT_LOW_SPEED ] = "PORT_LOW_SPEED", |
| 67 | [HUB_FEATURE_PORT_CONNECTION_CHANGE ] = "PORT_CONNECTION_CHANGE", |
| 68 | [HUB_FEATURE_PORT_ENABLE_CHANGE ] = "PORT_ENABLE_CHANGE", |
| 69 | [HUB_FEATURE_PORT_SUSPEND_CHANGE ] = "PORT_SUSPEND_CHANGE", |
| 70 | [HUB_FEATURE_PORT_OVER_CURRENT_CHANGE ] = "PORT_OVER_CURRENT_CHANGE", |
| 71 | [HUB_FEATURE_PORT_RESET_CHANGE ] = "PORT_RESET_CHANGE", |
| 72 | [HUB_FEATURE_PORT_TEST ] = "PORT_TEST", |
| 73 | [HUB_FEATURE_PORT_INDICATOR ] = "PORT_INDICATOR", |
| 74 | }; |
| 75 | #endif |
| 76 | |
| 77 | //--------------------------------------------------------------------+ |
| 78 | // HUB |
| 79 | //--------------------------------------------------------------------+ |
| 80 | bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, tuh_control_complete_cb_t complete_cb) |
| 81 | { |
| 82 | tusb_control_request_t const request = |
| 83 | { |
| 84 | .bmRequestType_bit = |
| 85 | { |
| 86 | .recipient = TUSB_REQ_RCPT_OTHER, |
| 87 | .type = TUSB_REQ_TYPE_CLASS, |
| 88 | .direction = TUSB_DIR_OUT |
| 89 | }, |
| 90 | .bRequest = HUB_REQUEST_CLEAR_FEATURE, |
| 91 | .wValue = feature, |
| 92 | .wIndex = hub_port, |
| 93 | .wLength = 0 |
| 94 | }; |
| 95 | |
| 96 | TU_LOG2("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port); |
| 97 | TU_ASSERT( tuh_control_xfer(hub_addr, &request, NULL, complete_cb) ); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, tuh_control_complete_cb_t complete_cb) |
| 102 | { |
| 103 | tusb_control_request_t const request = |
| 104 | { |
| 105 | .bmRequestType_bit = |
| 106 | { |
| 107 | .recipient = TUSB_REQ_RCPT_OTHER, |
| 108 | .type = TUSB_REQ_TYPE_CLASS, |
| 109 | .direction = TUSB_DIR_OUT |
| 110 | }, |
| 111 | .bRequest = HUB_REQUEST_SET_FEATURE, |
| 112 | .wValue = feature, |
| 113 | .wIndex = hub_port, |
| 114 | .wLength = 0 |
| 115 | }; |
| 116 | |
| 117 | TU_LOG2("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port); |
| 118 | TU_ASSERT( tuh_control_xfer(hub_addr, &request, NULL, complete_cb) ); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_control_complete_cb_t complete_cb) |
| 123 | { |
| 124 | return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb); |
| 125 | } |
| 126 | |
| 127 | bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, tuh_control_complete_cb_t complete_cb) |
| 128 | { |
| 129 | tusb_control_request_t const request = |
| 130 | { |
| 131 | .bmRequestType_bit = |
| 132 | { |
| 133 | .recipient = TUSB_REQ_RCPT_OTHER, |
| 134 | .type = TUSB_REQ_TYPE_CLASS, |
| 135 | .direction = TUSB_DIR_IN |
| 136 | }, |
| 137 | .bRequest = HUB_REQUEST_GET_STATUS, |
| 138 | .wValue = 0, |
| 139 | .wIndex = hub_port, |
| 140 | .wLength = 4 |
| 141 | }; |
| 142 | |
| 143 | TU_LOG2("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port); |
| 144 | TU_ASSERT( tuh_control_xfer( hub_addr, &request, resp, complete_cb) ); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | //--------------------------------------------------------------------+ |
| 149 | // CLASS-USBH API (don't require to verify parameters) |
| 150 | //--------------------------------------------------------------------+ |
| 151 | void hub_init(void) |
| 152 | { |
| 153 | tu_memclr(hub_data, sizeof(hub_data)); |
| 154 | } |
| 155 | |
| 156 | bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) |
| 157 | { |
| 158 | TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass && |
| 159 | 0 == itf_desc->bInterfaceSubClass); |
| 160 | |
| 161 | // hub driver does not support multiple TT yet |
| 162 | TU_VERIFY(itf_desc->bInterfaceProtocol <= 1); |
| 163 | |
| 164 | // msc driver length is fixed |
| 165 | uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t); |
| 166 | TU_ASSERT(drv_len <= max_len); |
| 167 | |
| 168 | //------------- Interrupt Status endpoint -------------// |
| 169 | tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); |
| 170 | |
| 171 | TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && |
| 172 | TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0); |
| 173 | |
| 174 | TU_ASSERT(usbh_edpt_open(rhport, dev_addr, desc_ep)); |
| 175 | |
| 176 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 177 | |
| 178 | p_hub->itf_num = itf_desc->bInterfaceNumber; |
| 179 | p_hub->ep_in = desc_ep->bEndpointAddress; |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | void hub_close(uint8_t dev_addr) |
| 185 | { |
| 186 | TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, ); |
| 187 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 188 | |
| 189 | if (p_hub->ep_in) tu_memclr(p_hub, sizeof( hub_interface_t)); |
| 190 | } |
| 191 | |
| 192 | bool hub_status_pipe_queue(uint8_t dev_addr) |
| 193 | { |
| 194 | hub_interface_t* hub_itf = get_itf(dev_addr); |
| 195 | return usbh_edpt_xfer(dev_addr, hub_itf->ep_in, &hub_itf->status_change, 1); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | //--------------------------------------------------------------------+ |
| 200 | // Set Configure |
| 201 | //--------------------------------------------------------------------+ |
| 202 | |
| 203 | static bool config_set_port_power (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); |
| 204 | static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); |
| 205 | |
| 206 | bool hub_set_config(uint8_t dev_addr, uint8_t itf_num) |
| 207 | { |
| 208 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 209 | TU_ASSERT(itf_num == p_hub->itf_num); |
| 210 | |
| 211 | // Get Hub Descriptor |
| 212 | tusb_control_request_t const request = |
| 213 | { |
| 214 | .bmRequestType_bit = |
| 215 | { |
| 216 | .recipient = TUSB_REQ_RCPT_DEVICE, |
| 217 | .type = TUSB_REQ_TYPE_CLASS, |
| 218 | .direction = TUSB_DIR_IN |
| 219 | }, |
| 220 | .bRequest = HUB_REQUEST_GET_DESCRIPTOR, |
| 221 | .wValue = 0, |
| 222 | .wIndex = 0, |
| 223 | .wLength = sizeof(descriptor_hub_desc_t) |
| 224 | }; |
| 225 | |
| 226 | TU_ASSERT( tuh_control_xfer(dev_addr, &request, _hub_buffer, config_set_port_power) ); |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | static bool config_set_port_power (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) |
| 232 | { |
| 233 | (void) request; |
| 234 | TU_ASSERT(XFER_RESULT_SUCCESS == result); |
| 235 | |
| 236 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 237 | |
| 238 | // only use number of ports in hub descriptor |
| 239 | descriptor_hub_desc_t const* desc_hub = (descriptor_hub_desc_t const*) _hub_buffer; |
| 240 | p_hub->port_count = desc_hub->bNbrPorts; |
| 241 | |
| 242 | // May need to GET_STATUS |
| 243 | |
| 244 | // Set Port Power to be able to detect connection, starting with port 1 |
| 245 | uint8_t const hub_port = 1; |
| 246 | return hub_port_set_feature(dev_addr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete); |
| 247 | } |
| 248 | |
| 249 | static bool config_port_power_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) |
| 250 | { |
| 251 | TU_ASSERT(XFER_RESULT_SUCCESS == result); |
| 252 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 253 | |
| 254 | if (request->wIndex == p_hub->port_count) |
| 255 | { |
| 256 | // All ports are power -> queue notification status endpoint and |
| 257 | // complete the SET CONFIGURATION |
| 258 | TU_ASSERT( usbh_edpt_xfer(dev_addr, p_hub->ep_in, &p_hub->status_change, 1) ); |
| 259 | |
| 260 | usbh_driver_set_config_complete(dev_addr, p_hub->itf_num); |
| 261 | }else |
| 262 | { |
| 263 | // power next port |
| 264 | uint8_t const hub_port = (uint8_t) (request->wIndex + 1); |
| 265 | return hub_port_set_feature(dev_addr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete); |
| 266 | } |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | //--------------------------------------------------------------------+ |
| 272 | // Connection Changes |
| 273 | //--------------------------------------------------------------------+ |
| 274 | |
| 275 | static bool connection_get_status_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); |
| 276 | static bool connection_clear_conn_change_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); |
| 277 | static bool connection_port_reset_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result); |
| 278 | |
| 279 | // callback as response of interrupt endpoint polling |
| 280 | bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) |
| 281 | { |
| 282 | (void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports |
| 283 | (void) ep_addr; |
| 284 | TU_ASSERT(result == XFER_RESULT_SUCCESS); |
| 285 | |
| 286 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 287 | |
| 288 | TU_LOG2(" Port Status Change = 0x%02X\r\n", p_hub->status_change); |
| 289 | |
| 290 | // Hub ignore bit0 in status change |
| 291 | for (uint8_t port=1; port <= p_hub->port_count; port++) |
| 292 | { |
| 293 | if ( tu_bit_test(p_hub->status_change, port) ) |
| 294 | { |
| 295 | hub_port_get_status(dev_addr, port, &p_hub->port_status, connection_get_status_complete); |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // NOTE: next status transfer is queued by usbh.c after handling this request |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | static bool connection_get_status_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) |
| 306 | { |
| 307 | TU_ASSERT(result == XFER_RESULT_SUCCESS); |
| 308 | |
| 309 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 310 | uint8_t const port_num = (uint8_t) request->wIndex; |
| 311 | |
| 312 | // Connection change |
| 313 | if (p_hub->port_status.change.connection) |
| 314 | { |
| 315 | // Port is powered and enabled |
| 316 | //TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, ); |
| 317 | |
| 318 | // Acknowledge Port Connection Change |
| 319 | hub_port_clear_feature(dev_addr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete); |
| 320 | }else |
| 321 | { |
| 322 | // Other changes are: Enable, Suspend, Over Current, Reset, L1 state |
| 323 | // TODO clear change |
| 324 | |
| 325 | // prepare for next hub status |
| 326 | // TODO continue with status_change, or maybe we can do it again with status |
| 327 | hub_status_pipe_queue(dev_addr); |
| 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | static bool connection_clear_conn_change_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) |
| 334 | { |
| 335 | TU_ASSERT(result == XFER_RESULT_SUCCESS); |
| 336 | |
| 337 | hub_interface_t* p_hub = get_itf(dev_addr); |
| 338 | uint8_t const port_num = (uint8_t) request->wIndex; |
| 339 | |
| 340 | if ( p_hub->port_status.status.connection ) |
| 341 | { |
| 342 | // Reset port if attach event |
| 343 | hub_port_reset(dev_addr, port_num, connection_port_reset_complete); |
| 344 | }else |
| 345 | { |
| 346 | // submit detach event |
| 347 | hcd_event_t event = |
| 348 | { |
| 349 | .rhport = usbh_get_rhport(dev_addr), |
| 350 | .event_id = HCD_EVENT_DEVICE_REMOVE, |
| 351 | .connection = |
| 352 | { |
| 353 | .hub_addr = dev_addr, |
| 354 | .hub_port = port_num |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | hcd_event_handler(&event, false); |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | static bool connection_port_reset_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) |
| 365 | { |
| 366 | TU_ASSERT(result == XFER_RESULT_SUCCESS); |
| 367 | |
| 368 | // hub_interface_t* p_hub = get_itf(dev_addr); |
| 369 | uint8_t const port_num = (uint8_t) request->wIndex; |
| 370 | |
| 371 | // submit attach event |
| 372 | hcd_event_t event = |
| 373 | { |
| 374 | .rhport = usbh_get_rhport(dev_addr), |
| 375 | .event_id = HCD_EVENT_DEVICE_ATTACH, |
| 376 | .connection = |
| 377 | { |
| 378 | .hub_addr = dev_addr, |
| 379 | .hub_port = port_num |
| 380 | } |
| 381 | }; |
| 382 | |
| 383 | hcd_event_handler(&event, false); |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | #endif |