[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 | */
|
---|
[c91db2a] | 111 | int ohci_rh_init(ohci_rh_t *instance, ohci_regs_t *regs, const char *name)
|
---|
| 112 | {
|
---|
| 113 | assert(instance);
|
---|
| 114 | instance->registers = regs;
|
---|
| 115 | instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
|
---|
[a1732929] | 116 | usb_log_debug2("rh_desc_a: %x.", OHCI_RD(regs->rh_desc_a));
|
---|
[c91db2a] | 117 | if (instance->port_count > OHCI_MAX_PORTS) {
|
---|
| 118 | usb_log_warning("OHCI specification does not allow %d ports. "
|
---|
[a1732929] | 119 | "Max %d ports will be used.", instance->port_count,
|
---|
[c91db2a] | 120 | OHCI_MAX_PORTS);
|
---|
| 121 | instance->port_count = OHCI_MAX_PORTS;
|
---|
| 122 | }
|
---|
[a1732929] | 123 | usb_log_info("%s: Found %u ports.", name, instance->port_count);
|
---|
[c91db2a] | 124 |
|
---|
| 125 | #if defined OHCI_POWER_SWITCH_no
|
---|
[a1732929] | 126 | usb_log_info("%s: Set power mode to no power switching.", name);
|
---|
[c91db2a] | 127 | /* Set port power mode to no power-switching. (always on) */
|
---|
| 128 | OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 129 |
|
---|
| 130 | /* Set to no over-current reporting */
|
---|
| 131 | OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 132 |
|
---|
| 133 | #elif defined OHCI_POWER_SWITCH_ganged
|
---|
[a1732929] | 134 | usb_log_info("%s: Set power mode to ganged power switching.", name);
|
---|
[c91db2a] | 135 | /* Set port power mode to ganged power-switching. */
|
---|
| 136 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 137 | OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
|
---|
| 138 |
|
---|
| 139 | /* Turn off power (hub driver will turn this back on)*/
|
---|
| 140 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
---|
| 141 |
|
---|
| 142 | /* Set to global over-current */
|
---|
| 143 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 144 | OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
---|
| 145 | #else
|
---|
[a1732929] | 146 | usb_log_info("%s: Set power mode to per-port power switching.", name);
|
---|
[c91db2a] | 147 | /* Set port power mode to per port power-switching. */
|
---|
| 148 | OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
|
---|
| 149 | OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
|
---|
| 150 |
|
---|
| 151 | /* Control all ports by global switch and turn them off */
|
---|
| 152 | OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
---|
| 153 | OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
|
---|
| 154 |
|
---|
| 155 | /* Return control to per port state */
|
---|
| 156 | OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
|
---|
| 157 |
|
---|
| 158 | /* Set per port over-current */
|
---|
| 159 | OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
|
---|
| 160 | OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
|
---|
| 161 | #endif
|
---|
| 162 |
|
---|
| 163 | ohci_rh_hub_desc_init(instance);
|
---|
[a94cbfa] | 164 | instance->status_change_endpoint = NULL;
|
---|
[c91db2a] | 165 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
| 166 | NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[52f1882] | 169 | /** Schedule USB request.
|
---|
| 170 | * @param instance OCHI root hub instance.
|
---|
| 171 | * @param batch USB requst batch to schedule.
|
---|
| 172 | * @return Always EOK.
|
---|
| 173 | * Most requests complete even before this function returns,
|
---|
| 174 | * status change requests might be postponed until there is something to report.
|
---|
| 175 | */
|
---|
[c91db2a] | 176 | int ohci_rh_schedule(ohci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
| 177 | {
|
---|
| 178 | assert(instance);
|
---|
| 179 | assert(batch);
|
---|
[a5b3de6] | 180 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 181 | batch->dir, &batch->setup.packet,
|
---|
[db51a6a6] | 182 | batch->buffer, batch->buffer_size, &batch->transferred_size);
|
---|
[c91db2a] | 183 | if (batch->error == ENAK) {
|
---|
[a94cbfa] | 184 | /* Lock the HC guard */
|
---|
| 185 | fibril_mutex_lock(batch->ep->guard);
|
---|
| 186 | const int err = endpoint_activate_locked(batch->ep, batch);
|
---|
| 187 | if (err) {
|
---|
| 188 | fibril_mutex_unlock(batch->ep->guard);
|
---|
| 189 | return err;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /*
|
---|
| 193 | * Asserting that the HC do not run two instances of the status
|
---|
| 194 | * change endpoint - shall be true.
|
---|
| 195 | */
|
---|
| 196 | assert(!instance->status_change_endpoint);
|
---|
| 197 |
|
---|
| 198 | endpoint_add_ref(batch->ep);
|
---|
| 199 | instance->status_change_endpoint = batch->ep;
|
---|
| 200 | fibril_mutex_unlock(batch->ep->guard);
|
---|
[c91db2a] | 201 | } else {
|
---|
[5fd9c30] | 202 | usb_transfer_batch_finish(batch);
|
---|
[c91db2a] | 203 | }
|
---|
| 204 | return EOK;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[52f1882] | 207 | /** Handle OHCI RHSC interrupt.
|
---|
| 208 | * @param instance OHCI root hub isntance.
|
---|
| 209 | * @return Always EOK.
|
---|
| 210 | *
|
---|
| 211 | * Interrupt means there is a change of status to report. It may trigger
|
---|
| 212 | * processing of a postponed request.
|
---|
| 213 | */
|
---|
[c91db2a] | 214 | int ohci_rh_interrupt(ohci_rh_t *instance)
|
---|
| 215 | {
|
---|
[a94cbfa] | 216 | endpoint_t *ep = instance->status_change_endpoint;
|
---|
| 217 | if (!ep)
|
---|
| 218 | return EOK;
|
---|
| 219 |
|
---|
| 220 | fibril_mutex_lock(ep->guard);
|
---|
| 221 | usb_transfer_batch_t * const batch = ep->active_batch;
|
---|
| 222 | endpoint_deactivate_locked(ep);
|
---|
| 223 | instance->status_change_endpoint = NULL;
|
---|
| 224 | fibril_mutex_unlock(ep->guard);
|
---|
| 225 |
|
---|
| 226 | endpoint_del_ref(ep);
|
---|
| 227 |
|
---|
[c91db2a] | 228 | if (batch) {
|
---|
[a5b3de6] | 229 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 230 | batch->dir, &batch->setup.packet,
|
---|
[db51a6a6] | 231 | batch->buffer, batch->buffer_size, &batch->transferred_size);
|
---|
[5fd9c30] | 232 | usb_transfer_batch_finish(batch);
|
---|
[c91db2a] | 233 | }
|
---|
| 234 | return EOK;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
| 238 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
| 239 | do { \
|
---|
| 240 | hub = virthub_get_data(device); \
|
---|
| 241 | assert(hub);\
|
---|
| 242 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
| 243 | return ESTALL; \
|
---|
| 244 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
| 245 | if (port > hub->port_count) \
|
---|
| 246 | return EINVAL; \
|
---|
| 247 | } while (0)
|
---|
| 248 |
|
---|
| 249 | /** Hub 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_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 | ohci_rh_t *hub = virthub_get_data(device);
|
---|
| 262 | assert(hub);
|
---|
| 263 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
| 264 | return ESTALL;
|
---|
| 265 | const uint32_t val = OHCI_RD(hub->registers->rh_status) &
|
---|
| 266 | (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
|
---|
| 267 | memcpy(data, &val, sizeof(val));
|
---|
| 268 | *act_size = sizeof(val);
|
---|
| 269 | return EOK;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /** Hub set feature request handler.
|
---|
| 273 | * @param device Virtual hub device
|
---|
| 274 | * @param setup_packet USB setup stage data.
|
---|
| 275 | * @param[out] data destination data buffer, size must be at least
|
---|
| 276 | * setup_packet->length bytes
|
---|
| 277 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 278 | * @return Error code.
|
---|
| 279 | */
|
---|
| 280 | static int req_clear_hub_feature(usbvirt_device_t *device,
|
---|
| 281 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 282 | uint8_t *data, size_t *act_size)
|
---|
| 283 | {
|
---|
| 284 | ohci_rh_t *hub = virthub_get_data(device);
|
---|
| 285 | assert(hub);
|
---|
| 286 |
|
---|
| 287 | /*
|
---|
| 288 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
| 289 | * C_HUB_OVER_CURRENT are supported.
|
---|
| 290 | * C_HUB_LOCAL_POWER is not supported
|
---|
| 291 | * because root hubs do not support local power status feature.
|
---|
| 292 | * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
|
---|
| 293 | * (OHCI pg. 127)
|
---|
| 294 | */
|
---|
| 295 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 296 | if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
|
---|
| 297 | OHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
|
---|
| 298 | }
|
---|
| 299 | return EOK;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | /** Port status request handler.
|
---|
| 303 | * @param device Virtual hub device
|
---|
| 304 | * @param setup_packet USB setup stage data.
|
---|
| 305 | * @param[out] data destination data buffer, size must be at least
|
---|
| 306 | * setup_packet->length bytes
|
---|
| 307 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 308 | * @return Error code.
|
---|
| 309 | */
|
---|
| 310 | static int req_get_port_status(usbvirt_device_t *device,
|
---|
| 311 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 312 | uint8_t *data, size_t *act_size)
|
---|
| 313 | {
|
---|
| 314 | ohci_rh_t *hub;
|
---|
| 315 | unsigned port;
|
---|
| 316 | TEST_SIZE_INIT(4, port, hub);
|
---|
| 317 | if (setup_packet->value != 0)
|
---|
| 318 | return EINVAL;
|
---|
| 319 |
|
---|
| 320 | const uint32_t status = OHCI_RD(hub->registers->rh_port_status[port]);
|
---|
| 321 | memcpy(data, &status, sizeof(status));
|
---|
| 322 | *act_size = sizeof(status);
|
---|
| 323 | return EOK;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | /** Port clear feature request handler.
|
---|
| 327 | * @param device Virtual hub device
|
---|
| 328 | * @param setup_packet USB setup stage data.
|
---|
| 329 | * @param[out] data destination data buffer, size must be at least
|
---|
| 330 | * setup_packet->length bytes
|
---|
| 331 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 332 | * @return Error code.
|
---|
| 333 | */
|
---|
| 334 | static int req_clear_port_feature(usbvirt_device_t *device,
|
---|
| 335 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 336 | uint8_t *data, size_t *act_size)
|
---|
| 337 | {
|
---|
| 338 | ohci_rh_t *hub;
|
---|
| 339 | unsigned port;
|
---|
| 340 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 341 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 342 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
| 343 | switch (feature)
|
---|
| 344 | {
|
---|
| 345 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
| 346 | {
|
---|
| 347 | const uint32_t rhda =
|
---|
| 348 | OHCI_RD(hub->registers->rh_desc_a);
|
---|
| 349 | /* No power switching */
|
---|
| 350 | if (rhda & RHDA_NPS_FLAG)
|
---|
| 351 | return ENOTSUP;
|
---|
| 352 | /* Ganged power switching, one port powers all */
|
---|
| 353 | if (!(rhda & RHDA_PSM_FLAG)) {
|
---|
| 354 | OHCI_WR(hub->registers->rh_status,
|
---|
| 355 | RHS_CLEAR_GLOBAL_POWER);
|
---|
| 356 | return EOK;
|
---|
| 357 | }
|
---|
| 358 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 359 | RHPS_CLEAR_PORT_POWER);
|
---|
| 360 | return EOK;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[cd3fa47] | 363 | case USB2_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
[c91db2a] | 364 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 365 | RHPS_CLEAR_PORT_ENABLE);
|
---|
| 366 | return EOK;
|
---|
| 367 |
|
---|
[cd3fa47] | 368 | case USB2_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
[c91db2a] | 369 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 370 | RHPS_CLEAR_PORT_SUSPEND);
|
---|
| 371 | return EOK;
|
---|
| 372 |
|
---|
| 373 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
[cd3fa47] | 374 | case USB2_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
| 375 | case USB2_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
[c91db2a] | 376 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
| 377 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
| 378 | usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
|
---|
[a1732929] | 379 | "C_SUSPEND, C_OC or C_RESET on port %u.", port);
|
---|
[c91db2a] | 380 | /* Bit offsets correspond to the feature number */
|
---|
| 381 | OHCI_WR(hub->registers->rh_port_status[port],
|
---|
| 382 | 1 << feature);
|
---|
| 383 | return EOK;
|
---|
| 384 |
|
---|
| 385 | default:
|
---|
| 386 | return ENOTSUP;
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | /** Port set feature request handler.
|
---|
| 391 | * @param device Virtual hub device
|
---|
| 392 | * @param setup_packet USB setup stage data.
|
---|
| 393 | * @param[out] data destination data buffer, size must be at least
|
---|
| 394 | * setup_packet->length bytes
|
---|
| 395 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 396 | * @return Error code.
|
---|
| 397 | */
|
---|
| 398 | static int req_set_port_feature(usbvirt_device_t *device,
|
---|
| 399 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 400 | uint8_t *data, size_t *act_size)
|
---|
| 401 | {
|
---|
| 402 | ohci_rh_t *hub;
|
---|
| 403 | unsigned port;
|
---|
| 404 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 405 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
[e141281] | 406 |
|
---|
[c91db2a] | 407 | switch (feature) {
|
---|
| 408 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
[e141281] | 409 | {
|
---|
| 410 | const uint32_t rhda = OHCI_RD(hub->registers->rh_desc_a);
|
---|
| 411 |
|
---|
| 412 | /* No power switching */
|
---|
| 413 | if (rhda & RHDA_NPS_FLAG)
|
---|
| 414 | return EOK;
|
---|
| 415 |
|
---|
| 416 | /* Ganged power switching, one port powers all */
|
---|
| 417 | if (!(rhda & RHDA_PSM_FLAG)) {
|
---|
| 418 | OHCI_WR(hub->registers->rh_status,RHS_SET_GLOBAL_POWER);
|
---|
| 419 | return EOK;
|
---|
| 420 | }
|
---|
[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 | */
|
---|
| 449 | static int req_status_change_handler(usbvirt_device_t *device,
|
---|
| 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 */
|
---|
| 468 | if (OHCI_RD(hub->registers->rh_port_status[port - 1])
|
---|
| 469 | & RHPS_CHANGE_WC_MASK) {
|
---|
| 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 = {
|
---|
| 543 | .control = control_transfer_handlers,
|
---|
| 544 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
| 545 | };
|
---|