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