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