[6297465] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jan Vesely
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[6297465] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 | /** @addtogroup drvusbehci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief EHCI driver
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <mem.h>
|
---|
[92900e2] | 39 | #include <str_error.h>
|
---|
[8d2dd7f2] | 40 | #include <stdint.h>
|
---|
| 41 | #include <stddef.h>
|
---|
[6297465] | 42 |
|
---|
| 43 | #include <usb/classes/hub.h>
|
---|
| 44 | #include <usb/debug.h>
|
---|
| 45 | #include <usb/descriptor.h>
|
---|
| 46 | #include <usb/request.h>
|
---|
| 47 | #include <usb/usb.h>
|
---|
| 48 |
|
---|
| 49 | #include <usb/host/endpoint.h>
|
---|
| 50 | #include <usbvirt/device.h>
|
---|
| 51 |
|
---|
| 52 | #include "ehci_rh.h"
|
---|
| 53 |
|
---|
| 54 | enum {
|
---|
| 55 | HUB_STATUS_CHANGE_PIPE = 1,
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | static usbvirt_device_ops_t ops;
|
---|
| 59 |
|
---|
| 60 | /** Initialize internal USB HUB class descriptor.
|
---|
| 61 | * @param instance EHCI root hub.
|
---|
| 62 | * Use register based info to create accurate descriptor.
|
---|
| 63 | */
|
---|
| 64 | static void ehci_rh_hub_desc_init(ehci_rh_t *instance, unsigned hcs)
|
---|
| 65 | {
|
---|
| 66 | assert(instance);
|
---|
| 67 | const unsigned dsize = sizeof(usb_hub_descriptor_header_t) +
|
---|
| 68 | STATUS_BYTES(instance->port_count) * 2;
|
---|
| 69 | assert(dsize <= sizeof(instance->hub_descriptor));
|
---|
| 70 |
|
---|
| 71 | instance->hub_descriptor.header.length = dsize;
|
---|
| 72 | instance->hub_descriptor.header.descriptor_type = USB_DESCTYPE_HUB;
|
---|
| 73 | instance->hub_descriptor.header.port_count = instance->port_count;
|
---|
| 74 | /* Bits 0,1 indicate power switching mode
|
---|
| 75 | * Bit 2 indicates device type (compound device)
|
---|
| 76 | * Bits 3,4 indicate over-current protection mode */
|
---|
| 77 | instance->hub_descriptor.header.characteristics = 0 |
|
---|
| 78 | ((hcs & EHCI_CAPS_HCS_PPC_FLAG) ? 0x09 : 0x12) |
|
---|
| 79 | ((hcs & EHCI_CAPS_HCS_INDICATORS_FLAG) ? 0x80 : 0) |
|
---|
| 80 | (0x3 << 5); /* Need 32 FS bit times */ // TODO Implement
|
---|
| 81 | instance->hub_descriptor.header.characteristics_reserved = 0;
|
---|
| 82 | instance->hub_descriptor.header.power_good_time = 50;
|
---|
| 83 | /* bHubContrCurrent, root hubs don't need no power. */
|
---|
| 84 | instance->hub_descriptor.header.max_current = 0;
|
---|
| 85 |
|
---|
| 86 | /* Device removable and some legacy 1.0 stuff*/
|
---|
| 87 | instance->hub_descriptor.rempow[0] = 0xff;
|
---|
| 88 | instance->hub_descriptor.rempow[1] = 0xff;
|
---|
| 89 | instance->hub_descriptor.rempow[2] = 0xff;
|
---|
| 90 | instance->hub_descriptor.rempow[3] = 0xff;
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 | /** Initialize EHCI root hub.
|
---|
| 94 | * @param instance Place to initialize.
|
---|
| 95 | * @param regs EHCI device registers.
|
---|
| 96 | * @param name Device name.
|
---|
| 97 | * return Error code, EOK on success.
|
---|
| 98 | *
|
---|
| 99 | * Selects preconfigured port powering mode, sets up descriptor, and
|
---|
| 100 | * initializes internal virtual hub.
|
---|
| 101 | */
|
---|
[5a6cc679] | 102 | errno_t ehci_rh_init(ehci_rh_t *instance, ehci_caps_regs_t *caps, ehci_regs_t *regs,
|
---|
[ddbd088] | 103 | fibril_mutex_t *guard, const char *name)
|
---|
[6297465] | 104 | {
|
---|
| 105 | assert(instance);
|
---|
| 106 | instance->registers = regs;
|
---|
| 107 | instance->port_count =
|
---|
| 108 | (EHCI_RD(caps->hcsparams) >> EHCI_CAPS_HCS_N_PORTS_SHIFT) &
|
---|
| 109 | EHCI_CAPS_HCS_N_PORTS_MASK;
|
---|
[a1732929] | 110 | usb_log_debug2("RH(%p): hcsparams: %x.", instance,
|
---|
[605db7f] | 111 | EHCI_RD(caps->hcsparams));
|
---|
[a1732929] | 112 | usb_log_info("RH(%p): Found %u ports.", instance,
|
---|
[605db7f] | 113 | instance->port_count);
|
---|
[6297465] | 114 |
|
---|
| 115 | if (EHCI_RD(caps->hcsparams) & EHCI_CAPS_HCS_PPC_FLAG) {
|
---|
[605db7f] | 116 | usb_log_info("RH(%p): Per-port power switching.", instance);
|
---|
[64c96b9] | 117 | } else {
|
---|
[605db7f] | 118 | usb_log_info("RH(%p): No power switching.", instance);
|
---|
[6297465] | 119 | }
|
---|
[691130cf] | 120 | for (unsigned i = 0; i < instance->port_count; ++i)
|
---|
[3bacee1] | 121 | usb_log_debug2("RH(%p-%u): status: %" PRIx32, instance, i,
|
---|
[691130cf] | 122 | EHCI_RD(regs->portsc[i]));
|
---|
[6297465] | 123 |
|
---|
[95d5dca] | 124 | for (unsigned i = 0; i < EHCI_MAX_PORTS; ++i) {
|
---|
| 125 | instance->reset_flag[i] = false;
|
---|
| 126 | instance->resume_flag[i] = false;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[6297465] | 129 | ehci_rh_hub_desc_init(instance, EHCI_RD(caps->hcsparams));
|
---|
[ddbd088] | 130 | instance->guard = guard;
|
---|
[a94cbfa] | 131 | instance->status_change_endpoint = NULL;
|
---|
[2be477d5] | 132 |
|
---|
[6297465] | 133 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
| 134 | NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Schedule USB request.
|
---|
| 138 | * @param instance OCHI root hub instance.
|
---|
| 139 | * @param batch USB requst batch to schedule.
|
---|
| 140 | * @return Always EOK.
|
---|
| 141 | * Most requests complete even before this function returns,
|
---|
| 142 | * status change requests might be postponed until there is something to report.
|
---|
| 143 | */
|
---|
[5a6cc679] | 144 | errno_t ehci_rh_schedule(ehci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
[6297465] | 145 | {
|
---|
| 146 | assert(instance);
|
---|
| 147 | assert(batch);
|
---|
[a5b3de6] | 148 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[3bacee1] | 149 | batch->dir, (void *) batch->setup.buffer,
|
---|
[1d758fc] | 150 | batch->dma_buffer.virt, batch->size,
|
---|
[c21e6a5] | 151 | &batch->transferred_size);
|
---|
[6297465] | 152 | if (batch->error == ENAK) {
|
---|
[605db7f] | 153 | usb_log_debug("RH(%p): BATCH(%p) adding as unfinished",
|
---|
[92900e2] | 154 | instance, batch);
|
---|
[a94cbfa] | 155 |
|
---|
| 156 | /* Lock the HC guard */
|
---|
[ddbd088] | 157 | fibril_mutex_lock(instance->guard);
|
---|
[a94cbfa] | 158 | const int err = endpoint_activate_locked(batch->ep, batch);
|
---|
| 159 | if (err) {
|
---|
| 160 | fibril_mutex_unlock(batch->ep->guard);
|
---|
| 161 | return err;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /*
|
---|
| 165 | * Asserting that the HC do not run two instances of the status
|
---|
| 166 | * change endpoint - shall be true.
|
---|
| 167 | */
|
---|
| 168 | assert(!instance->status_change_endpoint);
|
---|
| 169 |
|
---|
| 170 | endpoint_add_ref(batch->ep);
|
---|
| 171 | instance->status_change_endpoint = batch->ep;
|
---|
[ddbd088] | 172 | fibril_mutex_unlock(instance->guard);
|
---|
[6297465] | 173 | } else {
|
---|
[605db7f] | 174 | usb_log_debug("RH(%p): BATCH(%p) virtual request complete: %s",
|
---|
[92900e2] | 175 | instance, batch, str_error(batch->error));
|
---|
[5fd9c30] | 176 | usb_transfer_batch_finish(batch);
|
---|
[6297465] | 177 | }
|
---|
| 178 | return EOK;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Handle EHCI RHSC interrupt.
|
---|
| 182 | * @param instance EHCI root hub isntance.
|
---|
| 183 | * @return Always EOK.
|
---|
| 184 | *
|
---|
| 185 | * Interrupt means there is a change of status to report. It may trigger
|
---|
| 186 | * processing of a postponed request.
|
---|
| 187 | */
|
---|
[5a6cc679] | 188 | errno_t ehci_rh_interrupt(ehci_rh_t *instance)
|
---|
[6297465] | 189 | {
|
---|
[ddbd088] | 190 | fibril_mutex_lock(instance->guard);
|
---|
[a94cbfa] | 191 | endpoint_t *ep = instance->status_change_endpoint;
|
---|
[ddbd088] | 192 | if (!ep) {
|
---|
| 193 | fibril_mutex_unlock(instance->guard);
|
---|
[a94cbfa] | 194 | return EOK;
|
---|
[ddbd088] | 195 | }
|
---|
[a94cbfa] | 196 |
|
---|
[3bacee1] | 197 | usb_transfer_batch_t *const batch = ep->active_batch;
|
---|
[a94cbfa] | 198 | endpoint_deactivate_locked(ep);
|
---|
| 199 | instance->status_change_endpoint = NULL;
|
---|
[ddbd088] | 200 | fibril_mutex_unlock(instance->guard);
|
---|
[a94cbfa] | 201 |
|
---|
| 202 | endpoint_del_ref(ep);
|
---|
| 203 |
|
---|
[6297465] | 204 | if (batch) {
|
---|
[a94cbfa] | 205 | usb_log_debug2("RH(%p): Interrupt. Processing batch: %p",
|
---|
| 206 | instance, batch);
|
---|
[a5b3de6] | 207 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[3bacee1] | 208 | batch->dir, (void *) batch->setup.buffer,
|
---|
[1d758fc] | 209 | batch->dma_buffer.virt, batch->size,
|
---|
[c21e6a5] | 210 | &batch->transferred_size);
|
---|
[5fd9c30] | 211 | usb_transfer_batch_finish(batch);
|
---|
[6297465] | 212 | }
|
---|
| 213 | return EOK;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
| 217 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
| 218 | do { \
|
---|
| 219 | hub = virthub_get_data(device); \
|
---|
| 220 | assert(hub);\
|
---|
| 221 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
| 222 | return ESTALL; \
|
---|
| 223 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
| 224 | if (port > hub->port_count) \
|
---|
| 225 | return EINVAL; \
|
---|
| 226 | } while (0)
|
---|
| 227 |
|
---|
| 228 | /** Hub status request handler.
|
---|
| 229 | * @param device Virtual hub device
|
---|
| 230 | * @param setup_packet USB setup stage data.
|
---|
| 231 | * @param[out] data destination data buffer, size must be at least
|
---|
| 232 | * setup_packet->length bytes
|
---|
| 233 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 234 | * @return Error code.
|
---|
| 235 | */
|
---|
[5a6cc679] | 236 | static errno_t req_get_status(usbvirt_device_t *device,
|
---|
[6297465] | 237 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 238 | uint8_t *data, size_t *act_size)
|
---|
| 239 | {
|
---|
| 240 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 241 | assert(hub);
|
---|
| 242 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
| 243 | return ESTALL;
|
---|
[2be477d5] | 244 | /* ECHI RH does not report global OC, and local power is always good */
|
---|
[6297465] | 245 | const uint32_t val = 0;
|
---|
| 246 | memcpy(data, &val, sizeof(val));
|
---|
| 247 | *act_size = sizeof(val);
|
---|
| 248 | return EOK;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /** Hub set feature request handler.
|
---|
| 252 | * @param device Virtual hub device
|
---|
| 253 | * @param setup_packet USB setup stage data.
|
---|
| 254 | * @param[out] data destination data buffer, size must be at least
|
---|
| 255 | * setup_packet->length bytes
|
---|
| 256 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 257 | * @return Error code.
|
---|
| 258 | */
|
---|
[5a6cc679] | 259 | static errno_t req_clear_hub_feature(usbvirt_device_t *device,
|
---|
[6297465] | 260 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 261 | uint8_t *data, size_t *act_size)
|
---|
| 262 | {
|
---|
| 263 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 264 | assert(hub);
|
---|
| 265 |
|
---|
| 266 | /*
|
---|
| 267 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
| 268 | * C_HUB_OVER_CURRENT are supported.
|
---|
[2be477d5] | 269 | * C_HUB_LOCAL_POWER is not supported because root hubs do not support
|
---|
| 270 | * local power status feature.
|
---|
| 271 | * EHCI RH does not report global OC condition either
|
---|
[6297465] | 272 | */
|
---|
[2be477d5] | 273 | return ESTALL;
|
---|
[6297465] | 274 | }
|
---|
| 275 |
|
---|
[82a639cd] | 276 | #define BIT_VAL(val, bit) ((val & bit) ? 1 : 0)
|
---|
[cd3fa47] | 277 | #define EHCI2USB(val, bit, mask) (BIT_VAL(val, bit) ? mask : 0)
|
---|
[82a639cd] | 278 |
|
---|
[6297465] | 279 | /** Port status request handler.
|
---|
| 280 | * @param device Virtual hub device
|
---|
| 281 | * @param setup_packet USB setup stage data.
|
---|
| 282 | * @param[out] data destination data buffer, size must be at least
|
---|
| 283 | * setup_packet->length bytes
|
---|
| 284 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 285 | * @return Error code.
|
---|
| 286 | */
|
---|
[5a6cc679] | 287 | static errno_t req_get_port_status(usbvirt_device_t *device,
|
---|
[6297465] | 288 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 289 | uint8_t *data, size_t *act_size)
|
---|
| 290 | {
|
---|
| 291 | ehci_rh_t *hub;
|
---|
| 292 | unsigned port;
|
---|
| 293 | TEST_SIZE_INIT(4, port, hub);
|
---|
| 294 | if (setup_packet->value != 0)
|
---|
| 295 | return EINVAL;
|
---|
| 296 |
|
---|
[2be477d5] | 297 | const uint32_t reg = EHCI_RD(hub->registers->portsc[port]);
|
---|
[82a639cd] | 298 | const uint32_t status = uint32_host2usb(
|
---|
[cd3fa47] | 299 | EHCI2USB(reg, USB_PORTSC_CONNECT_FLAG, USB_HUB_PORT_STATUS_CONNECTION) |
|
---|
| 300 | EHCI2USB(reg, USB_PORTSC_ENABLED_FLAG, USB_HUB_PORT_STATUS_ENABLE) |
|
---|
| 301 | EHCI2USB(reg, USB_PORTSC_SUSPEND_FLAG, USB2_HUB_PORT_STATUS_SUSPEND) |
|
---|
| 302 | EHCI2USB(reg, USB_PORTSC_OC_ACTIVE_FLAG, USB_HUB_PORT_STATUS_OC) |
|
---|
| 303 | EHCI2USB(reg, USB_PORTSC_PORT_RESET_FLAG, USB_HUB_PORT_STATUS_RESET) |
|
---|
| 304 | EHCI2USB(reg, USB_PORTSC_PORT_POWER_FLAG, USB2_HUB_PORT_STATUS_POWER) |
|
---|
[82a639cd] | 305 | (((reg & USB_PORTSC_LINE_STATUS_MASK) == USB_PORTSC_LINE_STATUS_K) ?
|
---|
[3bacee1] | 306 | (USB2_HUB_PORT_STATUS_LOW_SPEED) : 0) |
|
---|
[cd3fa47] | 307 | ((reg & USB_PORTSC_PORT_OWNER_FLAG) ? 0 : USB2_HUB_PORT_STATUS_HIGH_SPEED) |
|
---|
| 308 | EHCI2USB(reg, USB_PORTSC_PORT_TEST_MASK, USB2_HUB_PORT_STATUS_TEST) |
|
---|
| 309 | EHCI2USB(reg, USB_PORTSC_INDICATOR_MASK, USB2_HUB_PORT_STATUS_INDICATOR) |
|
---|
| 310 | EHCI2USB(reg, USB_PORTSC_CONNECT_CH_FLAG, USB_HUB_PORT_STATUS_C_CONNECTION) |
|
---|
| 311 | EHCI2USB(reg, USB_PORTSC_EN_CHANGE_FLAG, USB2_HUB_PORT_STATUS_C_ENABLE) |
|
---|
| 312 | (hub->resume_flag[port] ? USB2_HUB_PORT_STATUS_C_SUSPEND : 0) |
|
---|
| 313 | EHCI2USB(reg, USB_PORTSC_OC_CHANGE_FLAG, USB_HUB_PORT_STATUS_C_OC) |
|
---|
[3bacee1] | 314 | (hub->reset_flag[port] ? USB_HUB_PORT_STATUS_C_RESET : 0));
|
---|
[82a639cd] | 315 | /* Note feature numbers for test and indicator feature do not
|
---|
[605db7f] | 316 | * correspond to the port status bit locations */
|
---|
[3bacee1] | 317 | usb_log_debug2("RH(%p-%u) port status: %" PRIx32 "(%" PRIx32 ")", hub, port,
|
---|
[691130cf] | 318 | status, reg);
|
---|
[6297465] | 319 | memcpy(data, &status, sizeof(status));
|
---|
| 320 | *act_size = sizeof(status);
|
---|
| 321 | return EOK;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[5ee3ce0] | 324 | typedef struct {
|
---|
| 325 | ehci_rh_t *hub;
|
---|
| 326 | unsigned port;
|
---|
| 327 | } ehci_rh_job_t;
|
---|
| 328 |
|
---|
[5a6cc679] | 329 | static errno_t stop_reset(void *arg)
|
---|
[5ee3ce0] | 330 | {
|
---|
| 331 | ehci_rh_job_t *job = arg;
|
---|
| 332 | async_usleep(50000);
|
---|
[605db7f] | 333 | usb_log_debug("RH(%p-%u): Clearing reset", job->hub, job->port);
|
---|
[5ee3ce0] | 334 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
| 335 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
| 336 | /* wait for reset to complete */
|
---|
| 337 | while (EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
| 338 | USB_PORTSC_PORT_RESET_FLAG) {
|
---|
| 339 | async_usleep(1);
|
---|
[84239b1] | 340 | }
|
---|
[605db7f] | 341 | usb_log_debug("RH(%p-%u): Reset complete", job->hub, job->port);
|
---|
[5ee3ce0] | 342 | /* Handle port ownership, if the port is not enabled
|
---|
| 343 | * after reset it's a full speed device */
|
---|
| 344 | if (!(EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
| 345 | USB_PORTSC_ENABLED_FLAG)) {
|
---|
[3bacee1] | 346 | usb_log_info("RH(%p-%u): Port not enabled after reset (%" PRIX32
|
---|
[691130cf] | 347 | "), giving up ownership", job->hub, job->port,
|
---|
| 348 | EHCI_RD(job->hub->registers->portsc[job->port]));
|
---|
[5ee3ce0] | 349 | EHCI_SET(job->hub->registers->portsc[job->port],
|
---|
| 350 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
| 351 | }
|
---|
[f523daf] | 352 | job->hub->reset_flag[job->port] = true;
|
---|
[5ee3ce0] | 353 | ehci_rh_interrupt(job->hub);
|
---|
| 354 | free(job);
|
---|
| 355 | return 0;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[5a6cc679] | 358 | static errno_t stop_resume(void *arg)
|
---|
[5ee3ce0] | 359 | {
|
---|
| 360 | ehci_rh_job_t *job = arg;
|
---|
| 361 | async_usleep(20000);
|
---|
[605db7f] | 362 | usb_log_debug("RH(%p-%u): Stopping resume", job->hub, job->port);
|
---|
[5ee3ce0] | 363 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
| 364 | USB_PORTSC_RESUME_FLAG);
|
---|
| 365 | job->hub->resume_flag[job->port] = true;
|
---|
| 366 | ehci_rh_interrupt(job->hub);
|
---|
| 367 | free(job);
|
---|
| 368 | return 0;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[3bacee1] | 371 | static errno_t delayed_job(errno_t (*func)(void *), ehci_rh_t *rh, unsigned port)
|
---|
[5ee3ce0] | 372 | {
|
---|
| 373 | ehci_rh_job_t *job = malloc(sizeof(*job));
|
---|
| 374 | if (!job)
|
---|
| 375 | return ENOMEM;
|
---|
| 376 | job->hub = rh;
|
---|
| 377 | job->port = port;
|
---|
| 378 | fid_t fib = fibril_create(func, job);
|
---|
| 379 | if (!fib) {
|
---|
| 380 | free(job);
|
---|
| 381 | return ENOMEM;
|
---|
| 382 | }
|
---|
| 383 | fibril_add_ready(fib);
|
---|
[605db7f] | 384 | usb_log_debug2("RH(%p-%u): Scheduled delayed stop job.", rh, port);
|
---|
[5ee3ce0] | 385 | return EOK;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 |
|
---|
[6297465] | 389 | /** Port clear feature request handler.
|
---|
| 390 | * @param device Virtual hub device
|
---|
| 391 | * @param setup_packet USB setup stage data.
|
---|
| 392 | * @param[out] data destination data buffer, size must be at least
|
---|
| 393 | * setup_packet->length bytes
|
---|
| 394 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 395 | * @return Error code.
|
---|
| 396 | */
|
---|
[5a6cc679] | 397 | static errno_t req_clear_port_feature(usbvirt_device_t *device,
|
---|
[6297465] | 398 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 399 | uint8_t *data, size_t *act_size)
|
---|
| 400 | {
|
---|
| 401 | ehci_rh_t *hub;
|
---|
| 402 | unsigned port;
|
---|
| 403 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 404 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 405 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
[3bacee1] | 406 | switch (feature) {
|
---|
[6297465] | 407 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[605db7f] | 408 | usb_log_debug2("RH(%p-%u): Clear port power.", hub, port);
|
---|
[2be477d5] | 409 | EHCI_CLR(hub->registers->portsc[port],
|
---|
| 410 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
| 411 | return EOK;
|
---|
[6297465] | 412 |
|
---|
[cd3fa47] | 413 | case USB2_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[605db7f] | 414 | usb_log_debug2("RH(%p-%u): Clear port enable.", hub, port);
|
---|
[2be477d5] | 415 | EHCI_CLR(hub->registers->portsc[port],
|
---|
| 416 | USB_PORTSC_ENABLED_FLAG);
|
---|
[6297465] | 417 | return EOK;
|
---|
| 418 |
|
---|
[cd3fa47] | 419 | case USB2_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[605db7f] | 420 | usb_log_debug2("RH(%p-%u): Clear port suspend.", hub, port);
|
---|
[2be477d5] | 421 | /* If not in suspend it's noop */
|
---|
| 422 | if ((EHCI_RD(hub->registers->portsc[port]) &
|
---|
| 423 | USB_PORTSC_SUSPEND_FLAG) == 0)
|
---|
[3bacee1] | 424 | return EOK;
|
---|
[2be477d5] | 425 | /* Host driven resume */
|
---|
| 426 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 427 | USB_PORTSC_RESUME_FLAG);
|
---|
[605db7f] | 428 | //TODO: What if creating the delayed job fails?
|
---|
| 429 | return delayed_job(stop_resume, hub, port);
|
---|
[6297465] | 430 |
|
---|
| 431 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
[605db7f] | 432 | usb_log_debug2("RH(%p-%u): Clear port connection change.",
|
---|
| 433 | hub, port);
|
---|
[2be477d5] | 434 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 435 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
| 436 | return EOK;
|
---|
[cd3fa47] | 437 | case USB2_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
[605db7f] | 438 | usb_log_debug2("RH(%p-%u): Clear port enable change.",
|
---|
| 439 | hub, port);
|
---|
[2be477d5] | 440 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 441 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
| 442 | return EOK;
|
---|
[6297465] | 443 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
[605db7f] | 444 | usb_log_debug2("RH(%p-%u): Clear port OC change.",
|
---|
| 445 | hub, port);
|
---|
[2be477d5] | 446 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 447 | USB_PORTSC_OC_CHANGE_FLAG);
|
---|
| 448 | return EOK;
|
---|
[cd3fa47] | 449 | case USB2_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
[605db7f] | 450 | usb_log_debug2("RH(%p-%u): Clear port suspend change.",
|
---|
| 451 | hub, port);
|
---|
[95d5dca] | 452 | hub->resume_flag[port] = false;
|
---|
| 453 | return EOK;
|
---|
[6297465] | 454 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
[605db7f] | 455 | usb_log_debug2("RH(%p-%u): Clear port reset change.",
|
---|
| 456 | hub, port);
|
---|
[95d5dca] | 457 | hub->reset_flag[port] = false;
|
---|
[6297465] | 458 | return EOK;
|
---|
| 459 |
|
---|
| 460 | default:
|
---|
[605db7f] | 461 | usb_log_warning("RH(%p-%u): Clear unknown feature: %u",
|
---|
| 462 | hub, port, feature);
|
---|
[6297465] | 463 | return ENOTSUP;
|
---|
| 464 | }
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | /** Port set feature request handler.
|
---|
| 468 | * @param device Virtual hub device
|
---|
| 469 | * @param setup_packet USB setup stage data.
|
---|
| 470 | * @param[out] data destination data buffer, size must be at least
|
---|
| 471 | * setup_packet->length bytes
|
---|
| 472 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 473 | * @return Error code.
|
---|
| 474 | */
|
---|
[5a6cc679] | 475 | static errno_t req_set_port_feature(usbvirt_device_t *device,
|
---|
[6297465] | 476 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 477 | uint8_t *data, size_t *act_size)
|
---|
| 478 | {
|
---|
| 479 | ehci_rh_t *hub;
|
---|
| 480 | unsigned port;
|
---|
| 481 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 482 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 483 | switch (feature) {
|
---|
[cd3fa47] | 484 | case USB2_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[605db7f] | 485 | usb_log_debug2("RH(%p-%u): Set port enable.", hub, port);
|
---|
[2be477d5] | 486 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 487 | USB_PORTSC_ENABLED_FLAG);
|
---|
| 488 | return EOK;
|
---|
[cd3fa47] | 489 | case USB2_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[605db7f] | 490 | usb_log_debug2("RH(%p-%u): Set port suspend.", hub, port);
|
---|
[2be477d5] | 491 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 492 | USB_PORTSC_SUSPEND_FLAG);
|
---|
| 493 | return EOK;
|
---|
[6297465] | 494 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
---|
[605db7f] | 495 | usb_log_debug2("RH(%p-%u): Set port reset.", hub, port);
|
---|
[2be477d5] | 496 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 497 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
[605db7f] | 498 | //TODO: What if creating the delayed job fails?
|
---|
| 499 | return delayed_job(stop_reset, hub, port);
|
---|
[2be477d5] | 500 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[605db7f] | 501 | usb_log_debug2("RH(%p-%u): Set port power.", hub, port);
|
---|
[2be477d5] | 502 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 503 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
[6297465] | 504 | return EOK;
|
---|
| 505 | default:
|
---|
[605db7f] | 506 | usb_log_warning("RH(%p-%u): Set unknown feature: %u",
|
---|
| 507 | hub, port, feature);
|
---|
[6297465] | 508 | return ENOTSUP;
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /** Status change handler.
|
---|
| 513 | * @param device Virtual hub device
|
---|
| 514 | * @param endpoint Endpoint number
|
---|
| 515 | * @param tr_type Transfer type
|
---|
| 516 | * @param buffer Response destination
|
---|
| 517 | * @param buffer_size Bytes available in buffer
|
---|
| 518 | * @param actual_size Size us the used part of the dest buffer.
|
---|
| 519 | *
|
---|
| 520 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
| 521 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
| 522 | * only need 1 byte.
|
---|
| 523 | */
|
---|
[5a6cc679] | 524 | static errno_t req_status_change_handler(usbvirt_device_t *device,
|
---|
[6297465] | 525 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
| 526 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
| 527 | {
|
---|
| 528 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 529 | assert(hub);
|
---|
| 530 |
|
---|
| 531 | if (buffer_size < STATUS_BYTES(hub->port_count))
|
---|
| 532 | return ESTALL;
|
---|
| 533 |
|
---|
| 534 | uint16_t mask = 0;
|
---|
[2be477d5] | 535 | for (unsigned port = 0; port < hub->port_count; ++port) {
|
---|
[6297465] | 536 | /* Write-clean bits are those that indicate change */
|
---|
[2be477d5] | 537 | uint32_t status = EHCI_RD(hub->registers->portsc[port]);
|
---|
[691130cf] | 538 | if ((status & USB_PORTSC_WC_MASK) || hub->reset_flag[port]) {
|
---|
[2be477d5] | 539 | /* Ignore new LS device */
|
---|
| 540 | if ((status & USB_PORTSC_CONNECT_CH_FLAG) &&
|
---|
| 541 | (status & USB_PORTSC_LINE_STATUS_MASK) ==
|
---|
[3bacee1] | 542 | USB_PORTSC_LINE_STATUS_K)
|
---|
[486f479] | 543 | EHCI_SET(hub->registers->portsc[port],
|
---|
[2be477d5] | 544 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
| 545 | else
|
---|
| 546 | mask |= (2 << port);
|
---|
| 547 | }
|
---|
[6297465] | 548 | }
|
---|
| 549 |
|
---|
[3bacee1] | 550 | usb_log_debug2("RH(%p): root hub interrupt mask: %" PRIx16, hub, mask);
|
---|
[6297465] | 551 |
|
---|
| 552 | if (mask == 0)
|
---|
| 553 | return ENAK;
|
---|
| 554 | mask = uint16_host2usb(mask);
|
---|
| 555 | memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
|
---|
| 556 | *actual_size = STATUS_BYTES(hub->port_count);
|
---|
| 557 | return EOK;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | /** EHCI root hub request handlers */
|
---|
| 561 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
| 562 | {
|
---|
| 563 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 564 | .name = "GetDescriptor",
|
---|
| 565 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 566 | },
|
---|
| 567 | {
|
---|
| 568 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 569 | .name = "GetDescriptor",
|
---|
| 570 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 571 | },
|
---|
| 572 | {
|
---|
| 573 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
| 574 | .name = "GetHubDescriptor",
|
---|
| 575 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 576 | },
|
---|
| 577 | {
|
---|
| 578 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 579 | .name = "GetPortStatus",
|
---|
| 580 | .callback = req_get_port_status,
|
---|
| 581 | },
|
---|
| 582 | {
|
---|
| 583 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 584 | .name = "ClearHubFeature",
|
---|
| 585 | .callback = req_clear_hub_feature,
|
---|
| 586 | },
|
---|
| 587 | {
|
---|
| 588 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 589 | .name = "ClearPortFeature",
|
---|
| 590 | .callback = req_clear_port_feature,
|
---|
| 591 | },
|
---|
| 592 | {
|
---|
| 593 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 594 | .name = "GetHubStatus",
|
---|
| 595 | .callback = req_get_status,
|
---|
| 596 | },
|
---|
| 597 | {
|
---|
| 598 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 599 | .name = "GetPortStatus",
|
---|
| 600 | .callback = req_get_port_status,
|
---|
| 601 | },
|
---|
| 602 | {
|
---|
| 603 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 604 | .name = "SetHubFeature",
|
---|
| 605 | .callback = req_nop,
|
---|
| 606 | },
|
---|
| 607 | {
|
---|
| 608 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 609 | .name = "SetPortFeature",
|
---|
| 610 | .callback = req_set_port_feature,
|
---|
| 611 | },
|
---|
| 612 | {
|
---|
| 613 | .callback = NULL
|
---|
| 614 | }
|
---|
| 615 | };
|
---|
| 616 |
|
---|
| 617 | /** Virtual EHCI root hub ops */
|
---|
| 618 | static usbvirt_device_ops_t ops = {
|
---|
[3bacee1] | 619 | .control = control_transfer_handlers,
|
---|
| 620 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
[6297465] | 621 | };
|
---|