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