[c91db2a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jan Vesely
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[c91db2a] | 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 drvusbohci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief OHCI driver
|
---|
| 34 | */
|
---|
[0d4b110] | 35 |
|
---|
[c91db2a] | 36 | #include <assert.h>
|
---|
[0d4b110] | 37 | #include <errno.h>
|
---|
| 38 | #include <mem.h>
|
---|
[8d2dd7f2] | 39 | #include <stddef.h>
|
---|
| 40 | #include <stdint.h>
|
---|
[0d4b110] | 41 |
|
---|
| 42 | #include <usb/classes/hub.h>
|
---|
[c91db2a] | 43 | #include <usb/debug.h>
|
---|
[0d4b110] | 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>
|
---|
[c91db2a] | 50 |
|
---|
| 51 | #include "ohci_rh.h"
|
---|
| 52 |
|
---|
| 53 | enum {
|
---|
| 54 | HUB_STATUS_CHANGE_PIPE = 1,
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | static usbvirt_device_ops_t ops;
|
---|
| 58 |
|
---|
[52f1882] | 59 | /** Initialize internal USB HUB class descriptor.
|
---|
| 60 | * @param instance OHCI root hub.
|
---|
| 61 | * Use register based info to create accurate descriptor.
|
---|
| 62 | */
|
---|
[c91db2a] | 63 | static void ohci_rh_hub_desc_init(ohci_rh_t *instance)
|
---|
| 64 | {
|
---|
| 65 | assert(instance);
|
---|
| 66 | const unsigned dsize = sizeof(usb_hub_descriptor_header_t) +
|
---|
[1760556] | 67 | STATUS_BYTES(instance->port_count) * 2;
|
---|
[c91db2a] | 68 | assert(dsize <= sizeof(instance->hub_descriptor));
|
---|
| 69 | const uint32_t hub_desc = OHCI_RD(instance->registers->rh_desc_a);
|
---|
| 70 | const uint32_t port_desc = OHCI_RD(instance->registers->rh_desc_b);
|
---|
| 71 |
|
---|
| 72 | instance->hub_descriptor.header.length = dsize;
|
---|
| 73 | instance->hub_descriptor.header.descriptor_type = USB_DESCTYPE_HUB;
|
---|
| 74 | instance->hub_descriptor.header.port_count = instance->port_count;
|
---|
| 75 | instance->hub_descriptor.header.characteristics = 0 |
|
---|
| 76 | /* Bits 0,1 indicate power switching mode */
|
---|
| 77 | ((hub_desc & RHDA_PSM_FLAG) ? 0x01 : 0) |
|
---|
| 78 | ((hub_desc & RHDA_NPS_FLAG) ? 0x02 : 0) |
|
---|
| 79 | /* Bit 2 indicates device type (compound device) */
|
---|
| 80 | ((hub_desc & RHDA_DT_FLAG) ? 0x04 : 0) |
|
---|
| 81 | /* Bits 3,4 indicate over-current protection mode */
|
---|
| 82 | ((hub_desc & RHDA_OCPM_FLAG) ? 0x08 : 0) |
|
---|
| 83 | ((hub_desc & RHDA_NOCP_FLAG) ? 0x10 : 0);
|
---|
| 84 | instance->hub_descriptor.header.power_good_time =
|
---|
| 85 | hub_desc >> RHDA_POTPGT_SHIFT;
|
---|
| 86 | /* bHubContrCurrent, root hubs don't need no power. */
|
---|
| 87 | instance->hub_descriptor.header.max_current = 0;
|
---|
| 88 |
|
---|
| 89 | /* Device Removable and some legacy 1.0 stuff*/
|
---|
| 90 | instance->hub_descriptor.rempow[0] =
|
---|
| 91 | (port_desc >> RHDB_DR_SHIFT) & 0xff;
|
---|
| 92 | if (STATUS_BYTES(instance->port_count) == 1) {
|
---|
| 93 | instance->hub_descriptor.rempow[1] = 0xff;
|
---|
| 94 | } else {
|
---|
| 95 | instance->hub_descriptor.rempow[1] =
|
---|
[3bacee1] | 96 | ((port_desc >> RHDB_DR_SHIFT) >> 8) & 0xff;
|
---|
[c91db2a] | 97 | }
|
---|
| 98 |
|
---|
| 99 | instance->hub_descriptor.rempow[2] = 0xff;
|
---|
| 100 | instance->hub_descriptor.rempow[3] = 0xff;
|
---|
| 101 |
|
---|
| 102 | }
|
---|
[52f1882] | 103 | /** Initialize OHCI root hub.
|
---|
| 104 | * @param instance Place to initialize.
|
---|
| 105 | * @param regs OHCI device registers.
|
---|
| 106 | * @param name Device name.
|
---|
| 107 | * return Error code, EOK on success.
|
---|
| 108 | *
|
---|
| 109 | * Selects preconfigured port powering mode, sets up descriptor, and
|
---|
| 110 | * initializes internal virtual hub.
|
---|
| 111 | */
|
---|
[5a6cc679] | 112 | errno_t ohci_rh_init(ohci_rh_t *instance, ohci_regs_t *regs,
|
---|
[ee0ffa6] | 113 | fibril_mutex_t *guard, const char *name)
|
---|
[c91db2a] | 114 | {
|
---|
| 115 | assert(instance);
|
---|
| 116 | instance->registers = regs;
|
---|
| 117 | instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
|
---|
[a1732929] | 118 | usb_log_debug2("rh_desc_a: %x.", OHCI_RD(regs->rh_desc_a));
|
---|
[c91db2a] | 119 | if (instance->port_count > OHCI_MAX_PORTS) {
|
---|
| 120 | usb_log_warning("OHCI specification does not allow %d ports. "
|
---|
[a1732929] | 121 | "Max %d ports will be used.", instance->port_count,
|
---|
[c91db2a] | 122 | OHCI_MAX_PORTS);
|
---|
| 123 | instance->port_count = OHCI_MAX_PORTS;
|
---|
| 124 | }
|
---|
[a1732929] | 125 | usb_log_info("%s: Found %u ports.", name, instance->port_count);
|
---|
[c91db2a] | 126 |
|
---|
| 127 | #if defined OHCI_POWER_SWITCH_no
|
---|
[a1732929] | 128 | usb_log_info("%s: Set power mode to no power switching.", name);
|
---|
[c91db2a] | 129 | /* Set port power mode to no power-switching. (always on) */
|
---|
| 130 | OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 131 |
|
---|
| 132 | /* Set to no over-current reporting */
|
---|
| 133 | OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 134 |
|
---|
| 135 | #elif defined OHCI_POWER_SWITCH_ganged
|
---|
[a1732929] | 136 | usb_log_info("%s: Set power mode to ganged power switching.", name);
|
---|
[c91db2a] | 137 | /* Set port power mode to ganged power-switching. */
|
---|
| 138 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 139 | OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
|
---|
| 140 |
|
---|
| 141 | /* Turn off power (hub driver will turn this back on)*/
|
---|
| 142 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
---|
| 143 |
|
---|
| 144 | /* Set to global over-current */
|
---|
| 145 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 146 | OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
---|
| 147 | #else
|
---|
[a1732929] | 148 | usb_log_info("%s: Set power mode to per-port power switching.", name);
|
---|
[c91db2a] | 149 | /* Set port power mode to per port power-switching. */
|
---|
| 150 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 151 | OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
|
---|
| 152 |
|
---|
| 153 | /* Control all ports by global switch and turn them off */
|
---|
| 154 | OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
---|
| 155 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
---|
| 156 |
|
---|
| 157 | /* Return control to per port state */
|
---|
| 158 | OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
---|
| 159 |
|
---|
| 160 | /* Set per port over-current */
|
---|
| 161 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 162 | OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
---|
| 163 | #endif
|
---|
| 164 |
|
---|
| 165 | ohci_rh_hub_desc_init(instance);
|
---|
[a94cbfa] | 166 | instance->status_change_endpoint = NULL;
|
---|
[ee0ffa6] | 167 | instance->guard = guard;
|
---|
[c91db2a] | 168 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
| 169 | NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[52f1882] | 172 | /** Schedule USB request.
|
---|
| 173 | * @param instance OCHI root hub instance.
|
---|
| 174 | * @param batch USB requst batch to schedule.
|
---|
| 175 | * @return Always EOK.
|
---|
| 176 | * Most requests complete even before this function returns,
|
---|
| 177 | * status change requests might be postponed until there is something to report.
|
---|
| 178 | */
|
---|
[5a6cc679] | 179 | errno_t ohci_rh_schedule(ohci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
[c91db2a] | 180 | {
|
---|
| 181 | assert(instance);
|
---|
| 182 | assert(batch);
|
---|
[a5b3de6] | 183 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 184 | batch->dir, &batch->setup.packet,
|
---|
[1d758fc] | 185 | batch->dma_buffer.virt, batch->size, &batch->transferred_size);
|
---|
[c91db2a] | 186 | if (batch->error == ENAK) {
|
---|
[a94cbfa] | 187 | /* Lock the HC guard */
|
---|
[ee0ffa6] | 188 | fibril_mutex_lock(instance->guard);
|
---|
[a94cbfa] | 189 | const int err = endpoint_activate_locked(batch->ep, batch);
|
---|
| 190 | if (err) {
|
---|
| 191 | fibril_mutex_unlock(batch->ep->guard);
|
---|
| 192 | return err;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /*
|
---|
| 196 | * Asserting that the HC do not run two instances of the status
|
---|
| 197 | * change endpoint - shall be true.
|
---|
| 198 | */
|
---|
| 199 | assert(!instance->status_change_endpoint);
|
---|
| 200 |
|
---|
| 201 | endpoint_add_ref(batch->ep);
|
---|
| 202 | instance->status_change_endpoint = batch->ep;
|
---|
[ee0ffa6] | 203 | fibril_mutex_unlock(instance->guard);
|
---|
[c91db2a] | 204 | } else {
|
---|
[5fd9c30] | 205 | usb_transfer_batch_finish(batch);
|
---|
[c91db2a] | 206 | }
|
---|
| 207 | return EOK;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[52f1882] | 210 | /** Handle OHCI RHSC interrupt.
|
---|
| 211 | * @param instance OHCI root hub isntance.
|
---|
| 212 | * @return Always EOK.
|
---|
| 213 | *
|
---|
| 214 | * Interrupt means there is a change of status to report. It may trigger
|
---|
| 215 | * processing of a postponed request.
|
---|
| 216 | */
|
---|
[5a6cc679] | 217 | errno_t ohci_rh_interrupt(ohci_rh_t *instance)
|
---|
[c91db2a] | 218 | {
|
---|
[ee0ffa6] | 219 | fibril_mutex_lock(instance->guard);
|
---|
[a94cbfa] | 220 | endpoint_t *ep = instance->status_change_endpoint;
|
---|
[ee0ffa6] | 221 | if (!ep) {
|
---|
| 222 | fibril_mutex_unlock(instance->guard);
|
---|
[a94cbfa] | 223 | return EOK;
|
---|
[ee0ffa6] | 224 | }
|
---|
[a94cbfa] | 225 |
|
---|
[3bacee1] | 226 | usb_transfer_batch_t *const batch = ep->active_batch;
|
---|
[a94cbfa] | 227 | endpoint_deactivate_locked(ep);
|
---|
| 228 | instance->status_change_endpoint = NULL;
|
---|
[ee0ffa6] | 229 | fibril_mutex_unlock(instance->guard);
|
---|
[a94cbfa] | 230 |
|
---|
| 231 | endpoint_del_ref(ep);
|
---|
| 232 |
|
---|
[c91db2a] | 233 | if (batch) {
|
---|
[a5b3de6] | 234 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 235 | batch->dir, &batch->setup.packet,
|
---|
[1d758fc] | 236 | batch->dma_buffer.virt, batch->size, &batch->transferred_size);
|
---|
[5fd9c30] | 237 | usb_transfer_batch_finish(batch);
|
---|
[c91db2a] | 238 | }
|
---|
| 239 | return EOK;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
| 243 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
| 244 | do { \
|
---|
| 245 | hub = virthub_get_data(device); \
|
---|
| 246 | assert(hub);\
|
---|
| 247 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
| 248 | return ESTALL; \
|
---|
| 249 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
| 250 | if (port > hub->port_count) \
|
---|
| 251 | return EINVAL; \
|
---|
| 252 | } while (0)
|
---|
| 253 |
|
---|
| 254 | /** Hub status request handler.
|
---|
| 255 | * @param device Virtual hub device
|
---|
| 256 | * @param setup_packet USB setup stage data.
|
---|
| 257 | * @param[out] data destination data buffer, size must be at least
|
---|
| 258 | * setup_packet->length bytes
|
---|
| 259 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 260 | * @return Error code.
|
---|
| 261 | */
|
---|
[5a6cc679] | 262 | static errno_t req_get_status(usbvirt_device_t *device,
|
---|
[c91db2a] | 263 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 264 | uint8_t *data, size_t *act_size)
|
---|
| 265 | {
|
---|
| 266 | ohci_rh_t *hub = virthub_get_data(device);
|
---|
| 267 | assert(hub);
|
---|
| 268 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
| 269 | return ESTALL;
|
---|
| 270 | const uint32_t val = OHCI_RD(hub->registers->rh_status) &
|
---|
| 271 | (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
---|
| 272 | memcpy(data, &val, sizeof(val));
|
---|
| 273 | *act_size = sizeof(val);
|
---|
| 274 | return EOK;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** Hub set feature request handler.
|
---|
| 278 | * @param device Virtual hub device
|
---|
| 279 | * @param setup_packet USB setup stage data.
|
---|
| 280 | * @param[out] data destination data buffer, size must be at least
|
---|
| 281 | * setup_packet->length bytes
|
---|
| 282 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 283 | * @return Error code.
|
---|
| 284 | */
|
---|
[5a6cc679] | 285 | static errno_t req_clear_hub_feature(usbvirt_device_t *device,
|
---|
[c91db2a] | 286 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 287 | uint8_t *data, size_t *act_size)
|
---|
| 288 | {
|
---|
| 289 | ohci_rh_t *hub = virthub_get_data(device);
|
---|
| 290 | assert(hub);
|
---|
| 291 |
|
---|
| 292 | /*
|
---|
| 293 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
| 294 | * C_HUB_OVER_CURRENT are supported.
|
---|
| 295 | * C_HUB_LOCAL_POWER is not supported
|
---|
| 296 | * because root hubs do not support local power status feature.
|
---|
| 297 | * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
|
---|
| 298 | * (OHCI pg. 127)
|
---|
| 299 | */
|
---|
| 300 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 301 | if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
---|
| 302 | OHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
|
---|
| 303 | }
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /** Port status request handler.
|
---|
| 308 | * @param device Virtual hub device
|
---|
| 309 | * @param setup_packet USB setup stage data.
|
---|
| 310 | * @param[out] data destination data buffer, size must be at least
|
---|
| 311 | * setup_packet->length bytes
|
---|
| 312 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 313 | * @return Error code.
|
---|
| 314 | */
|
---|
[5a6cc679] | 315 | static errno_t req_get_port_status(usbvirt_device_t *device,
|
---|
[c91db2a] | 316 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 317 | uint8_t *data, size_t *act_size)
|
---|
| 318 | {
|
---|
| 319 | ohci_rh_t *hub;
|
---|
| 320 | unsigned port;
|
---|
| 321 | TEST_SIZE_INIT(4, port, hub);
|
---|
| 322 | if (setup_packet->value != 0)
|
---|
| 323 | return EINVAL;
|
---|
| 324 |
|
---|
| 325 | const uint32_t status = OHCI_RD(hub->registers->rh_port_status[port]);
|
---|
| 326 | memcpy(data, &status, sizeof(status));
|
---|
| 327 | *act_size = sizeof(status);
|
---|
| 328 | return EOK;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /** Port clear feature request handler.
|
---|
| 332 | * @param device Virtual hub device
|
---|
| 333 | * @param setup_packet USB setup stage data.
|
---|
| 334 | * @param[out] data destination data buffer, size must be at least
|
---|
| 335 | * setup_packet->length bytes
|
---|
| 336 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 337 | * @return Error code.
|
---|
| 338 | */
|
---|
[5a6cc679] | 339 | static errno_t req_clear_port_feature(usbvirt_device_t *device,
|
---|
[c91db2a] | 340 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 341 | uint8_t *data, size_t *act_size)
|
---|
| 342 | {
|
---|
| 343 | ohci_rh_t *hub;
|
---|
| 344 | unsigned port;
|
---|
[338d54a7] | 345 | uint32_t rhda;
|
---|
[c91db2a] | 346 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 347 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 348 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
[3bacee1] | 349 | switch (feature) {
|
---|
[c91db2a] | 350 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[338d54a7] | 351 | rhda = OHCI_RD(hub->registers->rh_desc_a);
|
---|
| 352 | /* No power switching */
|
---|
| 353 | if (rhda & RHDA_NPS_FLAG)
|
---|
| 354 | return ENOTSUP;
|
---|
| 355 | /* Ganged power switching, one port powers all */
|
---|
| 356 | if (!(rhda & RHDA_PSM_FLAG)) {
|
---|
| 357 | OHCI_WR(hub->registers->rh_status,
|
---|
| 358 | RHS_CLEAR_GLOBAL_POWER);
|
---|
[c91db2a] | 359 | return EOK;
|
---|
| 360 | }
|
---|
[338d54a7] | 361 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 362 | RHPS_CLEAR_PORT_POWER);
|
---|
| 363 | return EOK;
|
---|
[cd3fa47] | 364 | case USB2_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[c91db2a] | 365 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 366 | RHPS_CLEAR_PORT_ENABLE);
|
---|
| 367 | return EOK;
|
---|
| 368 |
|
---|
[cd3fa47] | 369 | case USB2_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[c91db2a] | 370 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 371 | RHPS_CLEAR_PORT_SUSPEND);
|
---|
| 372 | return EOK;
|
---|
| 373 |
|
---|
| 374 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
[cd3fa47] | 375 | case USB2_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
| 376 | case USB2_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
[c91db2a] | 377 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
| 378 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
| 379 | usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
|
---|
[a1732929] | 380 | "C_SUSPEND, C_OC or C_RESET on port %u.", port);
|
---|
[c91db2a] | 381 | /* Bit offsets correspond to the feature number */
|
---|
| 382 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 383 | 1 << feature);
|
---|
| 384 | return EOK;
|
---|
| 385 |
|
---|
| 386 | default:
|
---|
| 387 | return ENOTSUP;
|
---|
| 388 | }
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | /** Port set feature request handler.
|
---|
| 392 | * @param device Virtual hub device
|
---|
| 393 | * @param setup_packet USB setup stage data.
|
---|
| 394 | * @param[out] data destination data buffer, size must be at least
|
---|
| 395 | * setup_packet->length bytes
|
---|
| 396 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 397 | * @return Error code.
|
---|
| 398 | */
|
---|
[5a6cc679] | 399 | static errno_t req_set_port_feature(usbvirt_device_t *device,
|
---|
[c91db2a] | 400 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 401 | uint8_t *data, size_t *act_size)
|
---|
| 402 | {
|
---|
| 403 | ohci_rh_t *hub;
|
---|
| 404 | unsigned port;
|
---|
[338d54a7] | 405 | uint32_t rhda;
|
---|
[c91db2a] | 406 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 407 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
[a35b458] | 408 |
|
---|
[c91db2a] | 409 | switch (feature) {
|
---|
| 410 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[338d54a7] | 411 | rhda = OHCI_RD(hub->registers->rh_desc_a);
|
---|
| 412 |
|
---|
| 413 | /* No power switching */
|
---|
| 414 | if (rhda & RHDA_NPS_FLAG)
|
---|
| 415 | return EOK;
|
---|
| 416 |
|
---|
| 417 | /* Ganged power switching, one port powers all */
|
---|
| 418 | if (!(rhda & RHDA_PSM_FLAG)) {
|
---|
[3bacee1] | 419 | OHCI_WR(hub->registers->rh_status, RHS_SET_GLOBAL_POWER);
|
---|
[338d54a7] | 420 | return EOK;
|
---|
[c91db2a] | 421 | }
|
---|
[e141281] | 422 | /* Fall through, for per port power */
|
---|
| 423 | /* Fallthrough */
|
---|
[cd3fa47] | 424 | case USB2_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
| 425 | case USB2_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[c91db2a] | 426 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
---|
| 427 | usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
|
---|
[a1732929] | 428 | "on port %u.", port);
|
---|
[c91db2a] | 429 | /* Bit offsets correspond to the feature number */
|
---|
| 430 | OHCI_WR(hub->registers->rh_port_status[port], 1 << feature);
|
---|
| 431 | return EOK;
|
---|
| 432 | default:
|
---|
| 433 | return ENOTSUP;
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /** Status change handler.
|
---|
| 438 | * @param device Virtual hub device
|
---|
| 439 | * @param endpoint Endpoint number
|
---|
| 440 | * @param tr_type Transfer type
|
---|
| 441 | * @param buffer Response destination
|
---|
| 442 | * @param buffer_size Bytes available in buffer
|
---|
| 443 | * @param actual_size Size us the used part of the dest buffer.
|
---|
| 444 | *
|
---|
| 445 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
| 446 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
| 447 | * only need 1 byte.
|
---|
| 448 | */
|
---|
[5a6cc679] | 449 | static errno_t req_status_change_handler(usbvirt_device_t *device,
|
---|
[c91db2a] | 450 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
| 451 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
| 452 | {
|
---|
| 453 | ohci_rh_t *hub = virthub_get_data(device);
|
---|
| 454 | assert(hub);
|
---|
| 455 |
|
---|
| 456 | if (buffer_size < STATUS_BYTES(hub->port_count))
|
---|
| 457 | return ESTALL;
|
---|
| 458 |
|
---|
| 459 | uint16_t mask = 0;
|
---|
| 460 |
|
---|
| 461 | /* Only local power source change and over-current change can happen */
|
---|
| 462 | if (OHCI_RD(hub->registers->rh_status) & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
|
---|
| 463 | mask |= 1;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | for (unsigned port = 1; port <= hub->port_count; ++port) {
|
---|
| 467 | /* Write-clean bits are those that indicate change */
|
---|
[3bacee1] | 468 | if (OHCI_RD(hub->registers->rh_port_status[port - 1]) &
|
---|
| 469 | RHPS_CHANGE_WC_MASK) {
|
---|
[c91db2a] | 470 | mask |= (1 << port);
|
---|
| 471 | }
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[a1732929] | 474 | usb_log_debug2("OHCI root hub interrupt mask: %hx.", mask);
|
---|
[c91db2a] | 475 |
|
---|
| 476 | if (mask == 0)
|
---|
| 477 | return ENAK;
|
---|
| 478 | mask = uint16_host2usb(mask);
|
---|
| 479 | memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
|
---|
| 480 | *actual_size = STATUS_BYTES(hub->port_count);
|
---|
| 481 | return EOK;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /** OHCI root hub request handlers */
|
---|
| 485 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
| 486 | {
|
---|
| 487 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 488 | .name = "GetDescriptor",
|
---|
| 489 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 490 | },
|
---|
| 491 | {
|
---|
| 492 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 493 | .name = "GetDescriptor",
|
---|
| 494 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 495 | },
|
---|
| 496 | {
|
---|
| 497 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
| 498 | .name = "GetHubDescriptor",
|
---|
| 499 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 500 | },
|
---|
| 501 | {
|
---|
| 502 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 503 | .name = "GetPortStatus",
|
---|
| 504 | .callback = req_get_port_status,
|
---|
| 505 | },
|
---|
| 506 | {
|
---|
| 507 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 508 | .name = "ClearHubFeature",
|
---|
| 509 | .callback = req_clear_hub_feature,
|
---|
| 510 | },
|
---|
| 511 | {
|
---|
| 512 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 513 | .name = "ClearPortFeature",
|
---|
| 514 | .callback = req_clear_port_feature,
|
---|
| 515 | },
|
---|
| 516 | {
|
---|
| 517 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 518 | .name = "GetHubStatus",
|
---|
| 519 | .callback = req_get_status,
|
---|
| 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_SET_FEATURE),
|
---|
| 528 | .name = "SetHubFeature",
|
---|
| 529 | .callback = req_nop,
|
---|
| 530 | },
|
---|
| 531 | {
|
---|
| 532 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 533 | .name = "SetPortFeature",
|
---|
| 534 | .callback = req_set_port_feature,
|
---|
| 535 | },
|
---|
| 536 | {
|
---|
| 537 | .callback = NULL
|
---|
| 538 | }
|
---|
| 539 | };
|
---|
| 540 |
|
---|
[52f1882] | 541 | /** Virtual OHCI root hub ops */
|
---|
[c91db2a] | 542 | static usbvirt_device_ops_t ops = {
|
---|
[3bacee1] | 543 | .control = control_transfer_handlers,
|
---|
| 544 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
[c91db2a] | 545 | };
|
---|