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