[6297465] | 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>
|
---|
[92900e2] | 38 | #include <str_error.h>
|
---|
[8d2dd7f2] | 39 | #include <stdint.h>
|
---|
| 40 | #include <stddef.h>
|
---|
[6297465] | 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,
|
---|
[691130cf] | 102 | const char *name)
|
---|
[6297465] | 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;
|
---|
[605db7f] | 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);
|
---|
[6297465] | 113 |
|
---|
| 114 | if (EHCI_RD(caps->hcsparams) & EHCI_CAPS_HCS_PPC_FLAG) {
|
---|
[605db7f] | 115 | usb_log_info("RH(%p): Per-port power switching.", instance);
|
---|
[64c96b9] | 116 | } else {
|
---|
[605db7f] | 117 | usb_log_info("RH(%p): No power switching.", instance);
|
---|
[6297465] | 118 | }
|
---|
[691130cf] | 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]));
|
---|
[6297465] | 122 |
|
---|
[95d5dca] | 123 | for (unsigned i = 0; i < EHCI_MAX_PORTS; ++i) {
|
---|
| 124 | instance->reset_flag[i] = false;
|
---|
| 125 | instance->resume_flag[i] = false;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[6297465] | 128 | ehci_rh_hub_desc_init(instance, EHCI_RD(caps->hcsparams));
|
---|
| 129 | instance->unfinished_interrupt_transfer = NULL;
|
---|
[2be477d5] | 130 |
|
---|
[6297465] | 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);
|
---|
[a5b3de6] | 146 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 147 | batch->dir, (void*) batch->setup.buffer,
|
---|
[6297465] | 148 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
| 149 | if (batch->error == ENAK) {
|
---|
[605db7f] | 150 | usb_log_debug("RH(%p): BATCH(%p) adding as unfinished",
|
---|
[92900e2] | 151 | instance, batch);
|
---|
[6297465] | 152 | /* This is safe because only status change interrupt transfers
|
---|
| 153 | * return NAK. The assertion holds true because the batch
|
---|
| 154 | * existence prevents communication with that ep */
|
---|
| 155 | assert(instance->unfinished_interrupt_transfer == NULL);
|
---|
| 156 | instance->unfinished_interrupt_transfer = batch;
|
---|
| 157 | } else {
|
---|
[605db7f] | 158 | usb_log_debug("RH(%p): BATCH(%p) virtual request complete: %s",
|
---|
[92900e2] | 159 | instance, batch, str_error(batch->error));
|
---|
[5fd9c30] | 160 | usb_transfer_batch_finish(batch);
|
---|
[6297465] | 161 | }
|
---|
| 162 | return EOK;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Handle EHCI RHSC interrupt.
|
---|
| 166 | * @param instance EHCI root hub isntance.
|
---|
| 167 | * @return Always EOK.
|
---|
| 168 | *
|
---|
| 169 | * Interrupt means there is a change of status to report. It may trigger
|
---|
| 170 | * processing of a postponed request.
|
---|
| 171 | */
|
---|
| 172 | int ehci_rh_interrupt(ehci_rh_t *instance)
|
---|
| 173 | {
|
---|
| 174 | //TODO atomic swap needed
|
---|
| 175 | usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
|
---|
| 176 | instance->unfinished_interrupt_transfer = NULL;
|
---|
[605db7f] | 177 | usb_log_debug2("RH(%p): Interrupt. Processing batch: %p",
|
---|
| 178 | instance, batch);
|
---|
[6297465] | 179 | if (batch) {
|
---|
[a5b3de6] | 180 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 181 | batch->dir, (void*) batch->setup.buffer,
|
---|
[6297465] | 182 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
[5fd9c30] | 183 | usb_transfer_batch_finish(batch);
|
---|
[6297465] | 184 | }
|
---|
| 185 | return EOK;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
| 189 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
| 190 | do { \
|
---|
| 191 | hub = virthub_get_data(device); \
|
---|
| 192 | assert(hub);\
|
---|
| 193 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
| 194 | return ESTALL; \
|
---|
| 195 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
| 196 | if (port > hub->port_count) \
|
---|
| 197 | return EINVAL; \
|
---|
| 198 | } while (0)
|
---|
| 199 |
|
---|
| 200 | /** Hub status request handler.
|
---|
| 201 | * @param device Virtual hub device
|
---|
| 202 | * @param setup_packet USB setup stage data.
|
---|
| 203 | * @param[out] data destination data buffer, size must be at least
|
---|
| 204 | * setup_packet->length bytes
|
---|
| 205 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 206 | * @return Error code.
|
---|
| 207 | */
|
---|
| 208 | static int req_get_status(usbvirt_device_t *device,
|
---|
| 209 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 210 | uint8_t *data, size_t *act_size)
|
---|
| 211 | {
|
---|
| 212 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 213 | assert(hub);
|
---|
| 214 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
| 215 | return ESTALL;
|
---|
[2be477d5] | 216 | /* ECHI RH does not report global OC, and local power is always good */
|
---|
[6297465] | 217 | const uint32_t val = 0;
|
---|
| 218 | memcpy(data, &val, sizeof(val));
|
---|
| 219 | *act_size = sizeof(val);
|
---|
| 220 | return EOK;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /** Hub set feature request handler.
|
---|
| 224 | * @param device Virtual hub device
|
---|
| 225 | * @param setup_packet USB setup stage data.
|
---|
| 226 | * @param[out] data destination data buffer, size must be at least
|
---|
| 227 | * setup_packet->length bytes
|
---|
| 228 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 229 | * @return Error code.
|
---|
| 230 | */
|
---|
| 231 | static int req_clear_hub_feature(usbvirt_device_t *device,
|
---|
| 232 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 233 | uint8_t *data, size_t *act_size)
|
---|
| 234 | {
|
---|
| 235 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 236 | assert(hub);
|
---|
| 237 |
|
---|
| 238 | /*
|
---|
| 239 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
| 240 | * C_HUB_OVER_CURRENT are supported.
|
---|
[2be477d5] | 241 | * C_HUB_LOCAL_POWER is not supported because root hubs do not support
|
---|
| 242 | * local power status feature.
|
---|
| 243 | * EHCI RH does not report global OC condition either
|
---|
[6297465] | 244 | */
|
---|
[2be477d5] | 245 | return ESTALL;
|
---|
[6297465] | 246 | }
|
---|
| 247 |
|
---|
[82a639cd] | 248 | #define BIT_VAL(val, bit) ((val & bit) ? 1 : 0)
|
---|
| 249 | #define EHCI2USB(val, bit, feat) (BIT_VAL(val, bit) << feat)
|
---|
| 250 |
|
---|
[6297465] | 251 | /** Port status 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 | */
|
---|
| 259 | static int req_get_port_status(usbvirt_device_t *device,
|
---|
| 260 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 261 | uint8_t *data, size_t *act_size)
|
---|
| 262 | {
|
---|
| 263 | ehci_rh_t *hub;
|
---|
| 264 | unsigned port;
|
---|
| 265 | TEST_SIZE_INIT(4, port, hub);
|
---|
| 266 | if (setup_packet->value != 0)
|
---|
| 267 | return EINVAL;
|
---|
| 268 |
|
---|
[2be477d5] | 269 | const uint32_t reg = EHCI_RD(hub->registers->portsc[port]);
|
---|
[82a639cd] | 270 | const uint32_t status = uint32_host2usb(
|
---|
| 271 | EHCI2USB(reg, USB_PORTSC_CONNECT_FLAG, USB_HUB_FEATURE_PORT_CONNECTION) |
|
---|
| 272 | EHCI2USB(reg, USB_PORTSC_ENABLED_FLAG, USB_HUB_FEATURE_PORT_ENABLE) |
|
---|
| 273 | EHCI2USB(reg, USB_PORTSC_SUSPEND_FLAG, USB_HUB_FEATURE_PORT_SUSPEND) |
|
---|
| 274 | EHCI2USB(reg, USB_PORTSC_OC_ACTIVE_FLAG, USB_HUB_FEATURE_PORT_OVER_CURRENT) |
|
---|
| 275 | EHCI2USB(reg, USB_PORTSC_PORT_RESET_FLAG, USB_HUB_FEATURE_PORT_RESET) |
|
---|
| 276 | EHCI2USB(reg, USB_PORTSC_PORT_POWER_FLAG, USB_HUB_FEATURE_PORT_POWER) |
|
---|
| 277 | (((reg & USB_PORTSC_LINE_STATUS_MASK) == USB_PORTSC_LINE_STATUS_K) ?
|
---|
| 278 | (1 << USB_HUB_FEATURE_PORT_LOW_SPEED) : 0) |
|
---|
[486f479] | 279 | ((reg & USB_PORTSC_PORT_OWNER_FLAG) ? 0 : (1 << USB_HUB_FEATURE_PORT_HIGH_SPEED)) |
|
---|
[82a639cd] | 280 | EHCI2USB(reg, USB_PORTSC_PORT_TEST_MASK, 11) |
|
---|
| 281 | EHCI2USB(reg, USB_PORTSC_INDICATOR_MASK, 12) |
|
---|
| 282 | EHCI2USB(reg, USB_PORTSC_CONNECT_CH_FLAG, USB_HUB_FEATURE_C_PORT_CONNECTION) |
|
---|
| 283 | EHCI2USB(reg, USB_PORTSC_EN_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_ENABLE) |
|
---|
| 284 | (hub->resume_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_SUSPEND) : 0) |
|
---|
| 285 | EHCI2USB(reg, USB_PORTSC_OC_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
|
---|
| 286 | (hub->reset_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_RESET): 0)
|
---|
[95d5dca] | 287 | );
|
---|
[82a639cd] | 288 | /* Note feature numbers for test and indicator feature do not
|
---|
[605db7f] | 289 | * correspond to the port status bit locations */
|
---|
[691130cf] | 290 | usb_log_debug2("RH(%p-%u) port status: %"PRIx32"(%"PRIx32")", hub, port,
|
---|
| 291 | status, reg);
|
---|
[6297465] | 292 | memcpy(data, &status, sizeof(status));
|
---|
| 293 | *act_size = sizeof(status);
|
---|
| 294 | return EOK;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[5ee3ce0] | 297 | typedef struct {
|
---|
| 298 | ehci_rh_t *hub;
|
---|
| 299 | unsigned port;
|
---|
| 300 | } ehci_rh_job_t;
|
---|
| 301 |
|
---|
| 302 | static int stop_reset(void *arg)
|
---|
| 303 | {
|
---|
| 304 | ehci_rh_job_t *job = arg;
|
---|
| 305 | async_usleep(50000);
|
---|
[605db7f] | 306 | usb_log_debug("RH(%p-%u): Clearing reset", job->hub, job->port);
|
---|
[5ee3ce0] | 307 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
| 308 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
| 309 | /* wait for reset to complete */
|
---|
| 310 | while (EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
| 311 | USB_PORTSC_PORT_RESET_FLAG) {
|
---|
| 312 | async_usleep(1);
|
---|
| 313 | };
|
---|
[605db7f] | 314 | usb_log_debug("RH(%p-%u): Reset complete", job->hub, job->port);
|
---|
[5ee3ce0] | 315 | /* Handle port ownership, if the port is not enabled
|
---|
| 316 | * after reset it's a full speed device */
|
---|
| 317 | if (!(EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
| 318 | USB_PORTSC_ENABLED_FLAG)) {
|
---|
[56b5569] | 319 | usb_log_info("RH(%p-%u): Port not enabled after reset (%"PRIX32
|
---|
[691130cf] | 320 | "), giving up ownership", job->hub, job->port,
|
---|
| 321 | EHCI_RD(job->hub->registers->portsc[job->port]));
|
---|
[5ee3ce0] | 322 | EHCI_SET(job->hub->registers->portsc[job->port],
|
---|
| 323 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
| 324 | }
|
---|
[f523daf] | 325 | job->hub->reset_flag[job->port] = true;
|
---|
[5ee3ce0] | 326 | ehci_rh_interrupt(job->hub);
|
---|
| 327 | free(job);
|
---|
| 328 | return 0;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | static int stop_resume(void *arg)
|
---|
| 332 | {
|
---|
| 333 | ehci_rh_job_t *job = arg;
|
---|
| 334 | async_usleep(20000);
|
---|
[605db7f] | 335 | usb_log_debug("RH(%p-%u): Stopping resume", job->hub, job->port);
|
---|
[5ee3ce0] | 336 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
| 337 | USB_PORTSC_RESUME_FLAG);
|
---|
| 338 | job->hub->resume_flag[job->port] = true;
|
---|
| 339 | ehci_rh_interrupt(job->hub);
|
---|
| 340 | free(job);
|
---|
| 341 | return 0;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | static int delayed_job(int (*func)(void*), ehci_rh_t *rh, unsigned port)
|
---|
| 345 | {
|
---|
| 346 | ehci_rh_job_t *job = malloc(sizeof(*job));
|
---|
| 347 | if (!job)
|
---|
| 348 | return ENOMEM;
|
---|
| 349 | job->hub = rh;
|
---|
| 350 | job->port = port;
|
---|
| 351 | fid_t fib = fibril_create(func, job);
|
---|
| 352 | if (!fib) {
|
---|
| 353 | free(job);
|
---|
| 354 | return ENOMEM;
|
---|
| 355 | }
|
---|
| 356 | fibril_add_ready(fib);
|
---|
[605db7f] | 357 | usb_log_debug2("RH(%p-%u): Scheduled delayed stop job.", rh, port);
|
---|
[5ee3ce0] | 358 | return EOK;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 |
|
---|
[6297465] | 362 | /** Port clear feature request handler.
|
---|
| 363 | * @param device Virtual hub device
|
---|
| 364 | * @param setup_packet USB setup stage data.
|
---|
| 365 | * @param[out] data destination data buffer, size must be at least
|
---|
| 366 | * setup_packet->length bytes
|
---|
| 367 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 368 | * @return Error code.
|
---|
| 369 | */
|
---|
| 370 | static int req_clear_port_feature(usbvirt_device_t *device,
|
---|
| 371 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 372 | uint8_t *data, size_t *act_size)
|
---|
| 373 | {
|
---|
| 374 | ehci_rh_t *hub;
|
---|
| 375 | unsigned port;
|
---|
| 376 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 377 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 378 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
| 379 | switch (feature)
|
---|
| 380 | {
|
---|
| 381 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[605db7f] | 382 | usb_log_debug2("RH(%p-%u): Clear port power.", hub, port);
|
---|
[2be477d5] | 383 | EHCI_CLR(hub->registers->portsc[port],
|
---|
| 384 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
| 385 | return EOK;
|
---|
[6297465] | 386 |
|
---|
| 387 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[605db7f] | 388 | usb_log_debug2("RH(%p-%u): Clear port enable.", hub, port);
|
---|
[2be477d5] | 389 | EHCI_CLR(hub->registers->portsc[port],
|
---|
| 390 | USB_PORTSC_ENABLED_FLAG);
|
---|
[6297465] | 391 | return EOK;
|
---|
| 392 |
|
---|
| 393 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[605db7f] | 394 | usb_log_debug2("RH(%p-%u): Clear port suspend.", hub, port);
|
---|
[2be477d5] | 395 | /* If not in suspend it's noop */
|
---|
| 396 | if ((EHCI_RD(hub->registers->portsc[port]) &
|
---|
| 397 | USB_PORTSC_SUSPEND_FLAG) == 0)
|
---|
| 398 | return EOK;
|
---|
| 399 | /* Host driven resume */
|
---|
| 400 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 401 | USB_PORTSC_RESUME_FLAG);
|
---|
[605db7f] | 402 | //TODO: What if creating the delayed job fails?
|
---|
| 403 | return delayed_job(stop_resume, hub, port);
|
---|
[6297465] | 404 |
|
---|
| 405 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
[605db7f] | 406 | usb_log_debug2("RH(%p-%u): Clear port connection change.",
|
---|
| 407 | hub, port);
|
---|
[2be477d5] | 408 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 409 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
| 410 | return EOK;
|
---|
[6297465] | 411 | case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
[605db7f] | 412 | usb_log_debug2("RH(%p-%u): Clear port enable change.",
|
---|
| 413 | hub, port);
|
---|
[2be477d5] | 414 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 415 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
| 416 | return EOK;
|
---|
[6297465] | 417 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
[605db7f] | 418 | usb_log_debug2("RH(%p-%u): Clear port OC change.",
|
---|
| 419 | hub, port);
|
---|
[2be477d5] | 420 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 421 | USB_PORTSC_OC_CHANGE_FLAG);
|
---|
| 422 | return EOK;
|
---|
| 423 | case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
[605db7f] | 424 | usb_log_debug2("RH(%p-%u): Clear port suspend change.",
|
---|
| 425 | hub, port);
|
---|
[95d5dca] | 426 | hub->resume_flag[port] = false;
|
---|
| 427 | return EOK;
|
---|
[6297465] | 428 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
[605db7f] | 429 | usb_log_debug2("RH(%p-%u): Clear port reset change.",
|
---|
| 430 | hub, port);
|
---|
[95d5dca] | 431 | hub->reset_flag[port] = false;
|
---|
[6297465] | 432 | return EOK;
|
---|
| 433 |
|
---|
| 434 | default:
|
---|
[605db7f] | 435 | usb_log_warning("RH(%p-%u): Clear unknown feature: %u",
|
---|
| 436 | hub, port, feature);
|
---|
[6297465] | 437 | return ENOTSUP;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | /** Port set feature request handler.
|
---|
| 442 | * @param device Virtual hub device
|
---|
| 443 | * @param setup_packet USB setup stage data.
|
---|
| 444 | * @param[out] data destination data buffer, size must be at least
|
---|
| 445 | * setup_packet->length bytes
|
---|
| 446 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 447 | * @return Error code.
|
---|
| 448 | */
|
---|
| 449 | static int req_set_port_feature(usbvirt_device_t *device,
|
---|
| 450 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 451 | uint8_t *data, size_t *act_size)
|
---|
| 452 | {
|
---|
| 453 | ehci_rh_t *hub;
|
---|
| 454 | unsigned port;
|
---|
| 455 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 456 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 457 | switch (feature) {
|
---|
| 458 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[605db7f] | 459 | usb_log_debug2("RH(%p-%u): Set port enable.", hub, port);
|
---|
[2be477d5] | 460 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 461 | USB_PORTSC_ENABLED_FLAG);
|
---|
| 462 | return EOK;
|
---|
[6297465] | 463 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[605db7f] | 464 | usb_log_debug2("RH(%p-%u): Set port suspend.", hub, port);
|
---|
[2be477d5] | 465 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 466 | USB_PORTSC_SUSPEND_FLAG);
|
---|
| 467 | return EOK;
|
---|
[6297465] | 468 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
---|
[605db7f] | 469 | usb_log_debug2("RH(%p-%u): Set port reset.", hub, port);
|
---|
[2be477d5] | 470 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 471 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
[605db7f] | 472 | //TODO: What if creating the delayed job fails?
|
---|
| 473 | return delayed_job(stop_reset, hub, port);
|
---|
[2be477d5] | 474 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[605db7f] | 475 | usb_log_debug2("RH(%p-%u): Set port power.", hub, port);
|
---|
[2be477d5] | 476 | EHCI_SET(hub->registers->portsc[port],
|
---|
| 477 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
[6297465] | 478 | return EOK;
|
---|
| 479 | default:
|
---|
[605db7f] | 480 | usb_log_warning("RH(%p-%u): Set unknown feature: %u",
|
---|
| 481 | hub, port, feature);
|
---|
[6297465] | 482 | return ENOTSUP;
|
---|
| 483 | }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | /** Status change handler.
|
---|
| 487 | * @param device Virtual hub device
|
---|
| 488 | * @param endpoint Endpoint number
|
---|
| 489 | * @param tr_type Transfer type
|
---|
| 490 | * @param buffer Response destination
|
---|
| 491 | * @param buffer_size Bytes available in buffer
|
---|
| 492 | * @param actual_size Size us the used part of the dest buffer.
|
---|
| 493 | *
|
---|
| 494 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
| 495 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
| 496 | * only need 1 byte.
|
---|
| 497 | */
|
---|
| 498 | static int req_status_change_handler(usbvirt_device_t *device,
|
---|
| 499 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
| 500 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
| 501 | {
|
---|
| 502 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
| 503 | assert(hub);
|
---|
| 504 |
|
---|
| 505 | if (buffer_size < STATUS_BYTES(hub->port_count))
|
---|
| 506 | return ESTALL;
|
---|
| 507 |
|
---|
| 508 | uint16_t mask = 0;
|
---|
[2be477d5] | 509 | for (unsigned port = 0; port < hub->port_count; ++port) {
|
---|
[6297465] | 510 | /* Write-clean bits are those that indicate change */
|
---|
[2be477d5] | 511 | uint32_t status = EHCI_RD(hub->registers->portsc[port]);
|
---|
[691130cf] | 512 | if ((status & USB_PORTSC_WC_MASK) || hub->reset_flag[port]) {
|
---|
[2be477d5] | 513 | /* Ignore new LS device */
|
---|
| 514 | if ((status & USB_PORTSC_CONNECT_CH_FLAG) &&
|
---|
| 515 | (status & USB_PORTSC_LINE_STATUS_MASK) ==
|
---|
| 516 | USB_PORTSC_LINE_STATUS_K)
|
---|
[486f479] | 517 | EHCI_SET(hub->registers->portsc[port],
|
---|
[2be477d5] | 518 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
| 519 | else
|
---|
| 520 | mask |= (2 << port);
|
---|
| 521 | }
|
---|
[6297465] | 522 | }
|
---|
| 523 |
|
---|
[605db7f] | 524 | usb_log_debug2("RH(%p): root hub interrupt mask: %"PRIx16, hub, mask);
|
---|
[6297465] | 525 |
|
---|
| 526 | if (mask == 0)
|
---|
| 527 | return ENAK;
|
---|
| 528 | mask = uint16_host2usb(mask);
|
---|
| 529 | memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
|
---|
| 530 | *actual_size = STATUS_BYTES(hub->port_count);
|
---|
| 531 | return EOK;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | /** EHCI root hub request handlers */
|
---|
| 535 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
| 536 | {
|
---|
| 537 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 538 | .name = "GetDescriptor",
|
---|
| 539 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 540 | },
|
---|
| 541 | {
|
---|
| 542 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 543 | .name = "GetDescriptor",
|
---|
| 544 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 545 | },
|
---|
| 546 | {
|
---|
| 547 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
| 548 | .name = "GetHubDescriptor",
|
---|
| 549 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 550 | },
|
---|
| 551 | {
|
---|
| 552 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 553 | .name = "GetPortStatus",
|
---|
| 554 | .callback = req_get_port_status,
|
---|
| 555 | },
|
---|
| 556 | {
|
---|
| 557 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 558 | .name = "ClearHubFeature",
|
---|
| 559 | .callback = req_clear_hub_feature,
|
---|
| 560 | },
|
---|
| 561 | {
|
---|
| 562 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 563 | .name = "ClearPortFeature",
|
---|
| 564 | .callback = req_clear_port_feature,
|
---|
| 565 | },
|
---|
| 566 | {
|
---|
| 567 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 568 | .name = "GetHubStatus",
|
---|
| 569 | .callback = req_get_status,
|
---|
| 570 | },
|
---|
| 571 | {
|
---|
| 572 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 573 | .name = "GetPortStatus",
|
---|
| 574 | .callback = req_get_port_status,
|
---|
| 575 | },
|
---|
| 576 | {
|
---|
| 577 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 578 | .name = "SetHubFeature",
|
---|
| 579 | .callback = req_nop,
|
---|
| 580 | },
|
---|
| 581 | {
|
---|
| 582 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 583 | .name = "SetPortFeature",
|
---|
| 584 | .callback = req_set_port_feature,
|
---|
| 585 | },
|
---|
| 586 | {
|
---|
| 587 | .callback = NULL
|
---|
| 588 | }
|
---|
| 589 | };
|
---|
| 590 |
|
---|
| 591 | /** Virtual EHCI root hub ops */
|
---|
| 592 | static usbvirt_device_ops_t ops = {
|
---|
| 593 | .control = control_transfer_handlers,
|
---|
| 594 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
| 595 | };
|
---|