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