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