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