[c95c00e] | 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 |
|
---|
| 29 | #include <assert.h>
|
---|
[8064c2f6] | 30 | #include <async.h>
|
---|
| 31 | #include <ddi.h>
|
---|
| 32 | #include <errno.h>
|
---|
[c95c00e] | 33 | #include <macros.h>
|
---|
[8064c2f6] | 34 | #include <mem.h>
|
---|
| 35 | #include <sys/time.h>
|
---|
| 36 |
|
---|
[c95c00e] | 37 | #include <usb/debug.h>
|
---|
[8064c2f6] | 38 | #include <usb/descriptor.h>
|
---|
[49f2f29] | 39 | #include <usb/classes/hub.h>
|
---|
[8064c2f6] | 40 | #include <usb/request.h>
|
---|
[5fd9c30] | 41 | #include <usb/host/endpoint.h>
|
---|
[8064c2f6] | 42 | #include <usb/usb.h>
|
---|
[c95c00e] | 43 |
|
---|
| 44 | #include "uhci_rh.h"
|
---|
| 45 |
|
---|
| 46 | enum {
|
---|
| 47 | UHCI_RH_PORT_COUNT = 2,
|
---|
[49f2f29] | 48 | UHCI_PORT_BYTES = STATUS_BYTES(UHCI_RH_PORT_COUNT),
|
---|
[c95c00e] | 49 | };
|
---|
| 50 |
|
---|
| 51 | /** Hub descriptor. */
|
---|
| 52 | static const struct {
|
---|
[af4e464e] | 53 | /** Common hub descriptor header */
|
---|
[c95c00e] | 54 | usb_hub_descriptor_header_t header;
|
---|
[af4e464e] | 55 | /** Port removable status bits */
|
---|
[c95c00e] | 56 | uint8_t removable[UHCI_PORT_BYTES];
|
---|
[af4e464e] | 57 | /** Port powered status bits */
|
---|
[c95c00e] | 58 | uint8_t powered[UHCI_PORT_BYTES];
|
---|
| 59 | } __attribute__((packed)) hub_descriptor = {
|
---|
| 60 | .header = {
|
---|
| 61 | .length = sizeof(hub_descriptor),
|
---|
| 62 | .descriptor_type = USB_DESCTYPE_HUB,
|
---|
| 63 | .port_count = UHCI_RH_PORT_COUNT,
|
---|
| 64 | .characteristics =
|
---|
| 65 | HUB_CHAR_NO_POWER_SWITCH_FLAG | HUB_CHAR_NO_OC_FLAG,
|
---|
| 66 | .power_good_time = 50,
|
---|
| 67 | .max_current = 0,
|
---|
| 68 | },
|
---|
| 69 | .removable = { 0 },
|
---|
| 70 | .powered = { 0xFF },
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | static usbvirt_device_ops_t ops;
|
---|
| 74 |
|
---|
[af4e464e] | 75 | /** Initialize uhci rh structure.
|
---|
| 76 | * @param instance Memory place to initialize.
|
---|
| 77 | * @param ports Pointer to TWO UHCI RH port registers.
|
---|
| 78 | * @param name device name, passed to virthub init
|
---|
| 79 | * @return Error code, EOK on success.
|
---|
| 80 | */
|
---|
[e646c61] | 81 | int uhci_rh_init(uhci_rh_t *instance, ioport16_t *ports, const char *name)
|
---|
[c95c00e] | 82 | {
|
---|
| 83 | assert(instance);
|
---|
| 84 | instance->ports[0] = ports;
|
---|
| 85 | instance->ports[1] = ports + 1;
|
---|
| 86 | instance->reset_changed[0] = false;
|
---|
| 87 | instance->reset_changed[1] = false;
|
---|
[e646c61] | 88 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
[49f2f29] | 89 | NULL, &hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
[c95c00e] | 90 | }
|
---|
| 91 |
|
---|
[af4e464e] | 92 | /** Schedule USB batch for the root hub.
|
---|
| 93 | *
|
---|
| 94 | * @param instance UHCI rh instance
|
---|
| 95 | * @param batch USB communication batch
|
---|
| 96 | * @return EOK.
|
---|
| 97 | *
|
---|
| 98 | * The result of scheduling is always EOK. The result of communication does
|
---|
| 99 | * not have to be.
|
---|
| 100 | */
|
---|
[c95c00e] | 101 | int uhci_rh_schedule(uhci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
| 102 | {
|
---|
| 103 | assert(instance);
|
---|
| 104 | assert(batch);
|
---|
| 105 |
|
---|
[58d4880] | 106 | do {
|
---|
[a5b3de6] | 107 | batch->error = virthub_base_request(&instance->base, batch->target,
|
---|
[5fd9c30] | 108 | batch->dir, (void*) batch->setup.buffer,
|
---|
[db51a6a6] | 109 | batch->buffer, batch->buffer_size, &batch->transferred_size);
|
---|
[58d4880] | 110 | if (batch->error == ENAK)
|
---|
| 111 | async_usleep(instance->base.endpoint_descriptor.poll_interval * 1000);
|
---|
| 112 | //TODO This is flimsy, but we can't exit early because
|
---|
| 113 | //ENAK is technically an error condition
|
---|
| 114 | } while (batch->error == ENAK);
|
---|
[5fd9c30] | 115 | usb_transfer_batch_finish(batch);
|
---|
[c95c00e] | 116 | return EOK;
|
---|
| 117 | }
|
---|
[af4e464e] | 118 |
|
---|
| 119 | /** UHCI port register bits */
|
---|
[c95c00e] | 120 | enum {
|
---|
| 121 | STATUS_CONNECTED = (1 << 0),
|
---|
| 122 | STATUS_CONNECTED_CHANGED = (1 << 1),
|
---|
| 123 | STATUS_ENABLED = (1 << 2),
|
---|
| 124 | STATUS_ENABLED_CHANGED = (1 << 3),
|
---|
| 125 | STATUS_LINE_D_PLUS = (1 << 4),
|
---|
| 126 | STATUS_LINE_D_MINUS = (1 << 5),
|
---|
| 127 | STATUS_RESUME = (1 << 6),
|
---|
| 128 | STATUS_ALWAYS_ONE = (1 << 7),
|
---|
| 129 |
|
---|
| 130 | STATUS_LOW_SPEED = (1 << 8),
|
---|
| 131 | STATUS_IN_RESET = (1 << 9),
|
---|
| 132 | STATUS_SUSPEND = (1 << 12),
|
---|
| 133 |
|
---|
| 134 | STATUS_CHANGE_BITS = STATUS_CONNECTED_CHANGED | STATUS_ENABLED_CHANGED,
|
---|
| 135 | STATUS_WC_BITS = STATUS_CHANGE_BITS,
|
---|
| 136 | };
|
---|
[af4e464e] | 137 |
|
---|
[c95c00e] | 138 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
[af4e464e] | 139 |
|
---|
[c95c00e] | 140 | static void uhci_port_reset_enable(ioport16_t *port)
|
---|
| 141 | {
|
---|
| 142 | assert(port);
|
---|
| 143 | uint16_t port_status = pio_read_16(port);
|
---|
| 144 | /* We don't wan to remove changes, that's hub drivers work */
|
---|
| 145 | port_status &= ~STATUS_WC_BITS;
|
---|
| 146 | port_status |= STATUS_IN_RESET;
|
---|
| 147 | pio_write_16(port, port_status);
|
---|
| 148 | async_usleep(50000);
|
---|
| 149 | port_status = pio_read_16(port);
|
---|
| 150 | port_status &= ~STATUS_IN_RESET;
|
---|
| 151 | pio_write_16(port, port_status);
|
---|
| 152 | while ((port_status = pio_read_16(port)) & STATUS_IN_RESET);
|
---|
| 153 | /* PIO delay, should not be longer than 3ms as the device might
|
---|
| 154 | * enter suspend state. */
|
---|
| 155 | udelay(10);
|
---|
[be554d9] | 156 | /* Drop ConnectionChange as some UHCI hw
|
---|
| 157 | * sets this bit after reset, that is incorrect */
|
---|
[c95c00e] | 158 | port_status &= ~STATUS_WC_BITS;
|
---|
[be554d9] | 159 | pio_write_16(port, port_status | STATUS_ENABLED | STATUS_CONNECTED_CHANGED);
|
---|
[c95c00e] | 160 | }
|
---|
| 161 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
| 162 | do { \
|
---|
| 163 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
| 164 | return ESTALL; \
|
---|
| 165 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
| 166 | if (port != 0 && port != 1) \
|
---|
| 167 | return EINVAL; \
|
---|
| 168 | hub = virthub_get_data(device); \
|
---|
| 169 | assert(hub);\
|
---|
| 170 | } while (0)
|
---|
| 171 |
|
---|
[9e9d018e] | 172 | #define RH_DEBUG(hub, port, msg, ...) \
|
---|
[bc8bc6e] | 173 | do { \
|
---|
[e646c61] | 174 | if ((int)port >= 0) \
|
---|
[9e9d018e] | 175 | usb_log_debug("RH(%p-%d): " msg, hub, port, ##__VA_ARGS__); \
|
---|
[e646c61] | 176 | else \
|
---|
[bc8bc6e] | 177 | usb_log_debug("RH(%p):" msg, hub, ##__VA_ARGS__); \
|
---|
| 178 | } while (0)
|
---|
[e646c61] | 179 |
|
---|
[af4e464e] | 180 | /** USB HUB port state request handler.
|
---|
| 181 | * @param device Virtual hub device
|
---|
| 182 | * @param setup_packet USB setup stage data.
|
---|
| 183 | * @param[out] data destination data buffer, size must be at least
|
---|
| 184 | * setup_packet->length bytes
|
---|
| 185 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 186 | * @return Error code.
|
---|
| 187 | *
|
---|
| 188 | * Do not confuse with port status. Port state reports data line states,
|
---|
| 189 | * it is usefull for debuging purposes only.
|
---|
| 190 | */
|
---|
[c95c00e] | 191 | static int req_get_port_state(usbvirt_device_t *device,
|
---|
| 192 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 193 | uint8_t *data, size_t *act_size)
|
---|
| 194 | {
|
---|
| 195 | uhci_rh_t *hub;
|
---|
| 196 | unsigned port;
|
---|
| 197 | TEST_SIZE_INIT(1, port, hub);
|
---|
| 198 | if (setup_packet->value != 0)
|
---|
| 199 | return EINVAL;
|
---|
| 200 |
|
---|
| 201 | const uint16_t value = pio_read_16(hub->ports[port]);
|
---|
| 202 | data[0] = ((value & STATUS_LINE_D_MINUS) ? 1 : 0)
|
---|
| 203 | | ((value & STATUS_LINE_D_PLUS) ? 2 : 0);
|
---|
[a1732929] | 204 | RH_DEBUG(hub, port, "Bus state %" PRIx8 "(source %" PRIx16")",
|
---|
[e646c61] | 205 | data[0], value);
|
---|
[c95c00e] | 206 | *act_size = 1;
|
---|
| 207 | return EOK;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | #define BIT_VAL(val, bit) \
|
---|
| 211 | ((val & bit) ? 1 : 0)
|
---|
[cd3fa47] | 212 | #define UHCI2USB(val, bit, mask) \
|
---|
| 213 | (BIT_VAL(val, bit) ? (mask) : 0)
|
---|
[c95c00e] | 214 |
|
---|
[af4e464e] | 215 | /** Port status request handler.
|
---|
| 216 | * @param device Virtual hub device
|
---|
| 217 | * @param setup_packet USB setup stage data.
|
---|
| 218 | * @param[out] data destination data buffer, size must be at least
|
---|
| 219 | * setup_packet->length bytes
|
---|
| 220 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 221 | * @return Error code.
|
---|
| 222 | *
|
---|
| 223 | * Converts status reported via ioport to USB format.
|
---|
| 224 | * @note: reset change status needs to be handled in sw.
|
---|
| 225 | */
|
---|
[c95c00e] | 226 | static int req_get_port_status(usbvirt_device_t *device,
|
---|
| 227 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 228 | uint8_t *data, size_t *act_size)
|
---|
| 229 | {
|
---|
| 230 | uhci_rh_t *hub;
|
---|
| 231 | unsigned port;
|
---|
| 232 | TEST_SIZE_INIT(4, port, hub);
|
---|
| 233 | if (setup_packet->value != 0)
|
---|
| 234 | return EINVAL;
|
---|
| 235 |
|
---|
| 236 | const uint16_t val = pio_read_16(hub->ports[port]);
|
---|
[44d518b] | 237 | const uint32_t status = uint32_host2usb(
|
---|
[cd3fa47] | 238 | UHCI2USB(val, STATUS_CONNECTED, USB_HUB_PORT_STATUS_CONNECTION) |
|
---|
| 239 | UHCI2USB(val, STATUS_ENABLED, USB_HUB_PORT_STATUS_ENABLE) |
|
---|
| 240 | UHCI2USB(val, STATUS_SUSPEND, USB2_HUB_PORT_STATUS_SUSPEND) |
|
---|
| 241 | UHCI2USB(val, STATUS_IN_RESET, USB_HUB_PORT_STATUS_RESET) |
|
---|
| 242 | UHCI2USB(val, STATUS_ALWAYS_ONE, USB2_HUB_PORT_STATUS_POWER) |
|
---|
| 243 | UHCI2USB(val, STATUS_LOW_SPEED, USB2_HUB_PORT_STATUS_LOW_SPEED) |
|
---|
| 244 | UHCI2USB(val, STATUS_CONNECTED_CHANGED, USB_HUB_PORT_STATUS_C_CONNECTION) |
|
---|
| 245 | UHCI2USB(val, STATUS_ENABLED_CHANGED, USB2_HUB_PORT_STATUS_C_ENABLE) |
|
---|
| 246 | // UHCI2USB(val, STATUS_SUSPEND, USB2_HUB_PORT_STATUS_C_SUSPEND) |
|
---|
| 247 | (hub->reset_changed[port] ? USB_HUB_PORT_STATUS_C_RESET : 0)
|
---|
[c95c00e] | 248 | );
|
---|
[9e9d018e] | 249 | RH_DEBUG(hub, port, "Port status %" PRIx32 " (source %" PRIx16
|
---|
[a1732929] | 250 | "%s)", uint32_usb2host(status), val,
|
---|
[e646c61] | 251 | hub->reset_changed[port] ? "-reset" : "");
|
---|
[c95c00e] | 252 | memcpy(data, &status, sizeof(status));
|
---|
| 253 | *act_size = sizeof(status);;
|
---|
| 254 | return EOK;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[af4e464e] | 257 | /** Port clear feature request handler.
|
---|
| 258 | * @param device Virtual hub device
|
---|
| 259 | * @param setup_packet USB setup stage data.
|
---|
| 260 | * @param[out] data destination data buffer, size must be at least
|
---|
| 261 | * setup_packet->length bytes
|
---|
| 262 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
| 263 | * @return Error code.
|
---|
| 264 | */
|
---|
[c95c00e] | 265 | static int req_clear_port_feature(usbvirt_device_t *device,
|
---|
| 266 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
| 267 | uint8_t *data, size_t *act_size)
|
---|
| 268 | {
|
---|
| 269 | uhci_rh_t *hub;
|
---|
| 270 | unsigned port;
|
---|
| 271 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 272 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
[e646c61] | 273 | const uint16_t status = pio_read_16(hub->ports[port]);
|
---|
| 274 | const uint16_t val = status & (~STATUS_WC_BITS);
|
---|
[c95c00e] | 275 | switch (feature) {
|
---|
[cd3fa47] | 276 | case USB2_HUB_FEATURE_PORT_ENABLE:
|
---|
[9e9d018e] | 277 | RH_DEBUG(hub, port, "Clear port enable (status %"
|
---|
[a1732929] | 278 | PRIx16 ")", status);
|
---|
[e646c61] | 279 | pio_write_16(hub->ports[port], val & ~STATUS_ENABLED);
|
---|
[c95c00e] | 280 | break;
|
---|
[cd3fa47] | 281 | case USB2_HUB_FEATURE_PORT_SUSPEND:
|
---|
[9e9d018e] | 282 | RH_DEBUG(hub, port, "Clear port suspend (status %"
|
---|
[a1732929] | 283 | PRIx16 ")", status);
|
---|
[e646c61] | 284 | pio_write_16(hub->ports[port], val & ~STATUS_SUSPEND);
|
---|
[c95c00e] | 285 | // TODO we should do resume magic
|
---|
[a1732929] | 286 | usb_log_warning("Resume is not implemented on port %u", port);
|
---|
[c95c00e] | 287 | break;
|
---|
| 288 | case USB_HUB_FEATURE_PORT_POWER:
|
---|
[a1732929] | 289 | RH_DEBUG(hub, port, "Clear port power (status %" PRIx16 ")",
|
---|
[e646c61] | 290 | status);
|
---|
[c95c00e] | 291 | /* We are always powered */
|
---|
[a1732929] | 292 | usb_log_warning("Tried to power off port %u", port);
|
---|
[c95c00e] | 293 | break;
|
---|
| 294 | case USB_HUB_FEATURE_C_PORT_CONNECTION:
|
---|
[9e9d018e] | 295 | RH_DEBUG(hub, port, "Clear port conn change (status %"
|
---|
[a1732929] | 296 | PRIx16 ")", status);
|
---|
[e646c61] | 297 | pio_write_16(hub->ports[port], val | STATUS_CONNECTED_CHANGED);
|
---|
[c95c00e] | 298 | break;
|
---|
| 299 | case USB_HUB_FEATURE_C_PORT_RESET:
|
---|
[9e9d018e] | 300 | RH_DEBUG(hub, port, "Clear port reset change (status %"
|
---|
[a1732929] | 301 | PRIx16 ")", status);
|
---|
[e646c61] | 302 | hub->reset_changed[port] = false;
|
---|
[c95c00e] | 303 | break;
|
---|
[cd3fa47] | 304 | case USB2_HUB_FEATURE_C_PORT_ENABLE:
|
---|
[9e9d018e] | 305 | RH_DEBUG(hub, port, "Clear port enable change (status %"
|
---|
[a1732929] | 306 | PRIx16 ")", status);
|
---|
[c95c00e] | 307 | pio_write_16(hub->ports[port], status | STATUS_ENABLED_CHANGED);
|
---|
| 308 | break;
|
---|
[cd3fa47] | 309 | case USB2_HUB_FEATURE_C_PORT_SUSPEND:
|
---|
[9e9d018e] | 310 | RH_DEBUG(hub, port, "Clear port suspend change (status %"
|
---|
[a1732929] | 311 | PRIx16 ")", status);
|
---|
[c95c00e] | 312 | //TODO
|
---|
| 313 | return ENOTSUP;
|
---|
| 314 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
|
---|
[9e9d018e] | 315 | RH_DEBUG(hub, port, "Clear port OC change (status %"
|
---|
[a1732929] | 316 | PRIx16 ")", status);
|
---|
[c95c00e] | 317 | /* UHCI Does not report over current */
|
---|
[9e9d018e] | 318 | //TODO: newer chips do, but some have broken wiring
|
---|
[c95c00e] | 319 | break;
|
---|
| 320 | default:
|
---|
[9e9d018e] | 321 | RH_DEBUG(hub, port, "Clear unknown feature %d (status %"
|
---|
[a1732929] | 322 | PRIx16 ")", feature, status);
|
---|
| 323 | usb_log_warning("Clearing feature %d is unsupported",
|
---|
[e646c61] | 324 | feature);
|
---|
[c95c00e] | 325 | return ESTALL;
|
---|
| 326 | }
|
---|
| 327 | return EOK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[af4e464e] | 330 | /** Port set 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 | */
|
---|
[c95c00e] | 338 | static int req_set_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 | uhci_rh_t *hub;
|
---|
| 343 | unsigned port;
|
---|
| 344 | TEST_SIZE_INIT(0, port, hub);
|
---|
| 345 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
| 346 | const uint16_t status = pio_read_16(hub->ports[port]);
|
---|
| 347 | switch (feature) {
|
---|
| 348 | case USB_HUB_FEATURE_PORT_RESET:
|
---|
[9e9d018e] | 349 | RH_DEBUG(hub, port, "Set port reset before (status %" PRIx16
|
---|
[a1732929] | 350 | ")", status);
|
---|
[c95c00e] | 351 | uhci_port_reset_enable(hub->ports[port]);
|
---|
| 352 | hub->reset_changed[port] = true;
|
---|
[9e9d018e] | 353 | RH_DEBUG(hub, port, "Set port reset after (status %" PRIx16
|
---|
[a1732929] | 354 | ")", pio_read_16(hub->ports[port]));
|
---|
[c95c00e] | 355 | break;
|
---|
[cd3fa47] | 356 | case USB2_HUB_FEATURE_PORT_SUSPEND:
|
---|
[9e9d018e] | 357 | RH_DEBUG(hub, port, "Set port suspend (status %" PRIx16
|
---|
[a1732929] | 358 | ")", status);
|
---|
[c95c00e] | 359 | pio_write_16(hub->ports[port],
|
---|
| 360 | (status & ~STATUS_WC_BITS) | STATUS_SUSPEND);
|
---|
[a1732929] | 361 | usb_log_warning("Suspend is not implemented on port %u", port);
|
---|
[c95c00e] | 362 | break;
|
---|
| 363 | case USB_HUB_FEATURE_PORT_POWER:
|
---|
[9e9d018e] | 364 | RH_DEBUG(hub, port, "Set port power (status %" PRIx16
|
---|
[a1732929] | 365 | ")", status);
|
---|
[c95c00e] | 366 | /* We are always powered */
|
---|
[a1732929] | 367 | usb_log_warning("Tried to power port %u", port);
|
---|
[af4e464e] | 368 | break;
|
---|
[c95c00e] | 369 | case USB_HUB_FEATURE_C_PORT_CONNECTION:
|
---|
[cd3fa47] | 370 | case USB2_HUB_FEATURE_C_PORT_ENABLE:
|
---|
| 371 | case USB2_HUB_FEATURE_C_PORT_SUSPEND:
|
---|
[c95c00e] | 372 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
|
---|
[9e9d018e] | 373 | RH_DEBUG(hub, port, "Set port change flag (status %" PRIx16
|
---|
[a1732929] | 374 | ")", status);
|
---|
[c95c00e] | 375 | /* These are voluntary and don't have to be set
|
---|
| 376 | * there is no way we could do it on UHCI anyway */
|
---|
| 377 | break;
|
---|
| 378 | default:
|
---|
[9e9d018e] | 379 | RH_DEBUG(hub, port, "Set unknown feature %d (status %" PRIx16
|
---|
[a1732929] | 380 | ")", feature, status);
|
---|
| 381 | usb_log_warning("Setting feature %d is unsupported",
|
---|
[e646c61] | 382 | feature);
|
---|
[c95c00e] | 383 | return ESTALL;
|
---|
| 384 | }
|
---|
| 385 | return EOK;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[a5c2eb5f] | 388 | /** Status change handler.
|
---|
| 389 | * @param device Virtual hub device
|
---|
| 390 | * @param endpoint Endpoint number
|
---|
| 391 | * @param tr_type Transfer type
|
---|
| 392 | * @param buffer Response destination
|
---|
| 393 | * @param buffer_size Bytes available in buffer
|
---|
| 394 | * @param actual_size Size us the used part of the dest buffer.
|
---|
| 395 | *
|
---|
| 396 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
| 397 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
| 398 | * only need 1 byte.
|
---|
| 399 | */
|
---|
| 400 | static int req_status_change_handler(usbvirt_device_t *device,
|
---|
| 401 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
| 402 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
| 403 | {
|
---|
| 404 | uhci_rh_t *hub = virthub_get_data(device);
|
---|
| 405 | assert(hub);
|
---|
| 406 |
|
---|
| 407 | if (buffer_size < 1)
|
---|
| 408 | return ESTALL;
|
---|
| 409 |
|
---|
| 410 | const uint16_t status_a = pio_read_16(hub->ports[0]);
|
---|
| 411 | const uint16_t status_b = pio_read_16(hub->ports[1]);
|
---|
| 412 | const uint8_t status =
|
---|
| 413 | ((((status_a & STATUS_CHANGE_BITS) != 0) || hub->reset_changed[0]) ?
|
---|
| 414 | 0x2 : 0) |
|
---|
| 415 | ((((status_b & STATUS_CHANGE_BITS) != 0) || hub->reset_changed[1]) ?
|
---|
| 416 | 0x4 : 0);
|
---|
[bc8bc6e] | 417 | if (status)
|
---|
| 418 | RH_DEBUG(hub, -1, "Event mask %" PRIx8
|
---|
| 419 | " (status_a %" PRIx16 "%s),"
|
---|
[a1732929] | 420 | " (status_b %" PRIx16 "%s)", status,
|
---|
[bc8bc6e] | 421 | status_a, hub->reset_changed[0] ? "-reset" : "",
|
---|
| 422 | status_b, hub->reset_changed[1] ? "-reset" : "" );
|
---|
[a5c2eb5f] | 423 | ((uint8_t *)buffer)[0] = status;
|
---|
| 424 | *actual_size = 1;
|
---|
[58d4880] | 425 | return (status != 0 ? EOK : ENAK);
|
---|
[a5c2eb5f] | 426 | }
|
---|
| 427 |
|
---|
[af4e464e] | 428 | /** UHCI root hub request handlers */
|
---|
[2a5a7711] | 429 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
[c95c00e] | 430 | {
|
---|
| 431 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 432 | .name = "GetDescriptor",
|
---|
| 433 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 434 | },
|
---|
| 435 | {
|
---|
| 436 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
| 437 | .name = "GetDescriptor",
|
---|
| 438 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 439 | },
|
---|
| 440 | {
|
---|
| 441 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
| 442 | .name = "GetHubDescriptor",
|
---|
| 443 | .callback = virthub_base_get_hub_descriptor,
|
---|
| 444 | },
|
---|
| 445 | {
|
---|
| 446 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATE),
|
---|
| 447 | .name = "GetBusState",
|
---|
| 448 | .callback = req_get_port_state,
|
---|
| 449 | },
|
---|
| 450 | {
|
---|
| 451 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 452 | .name = "GetPortStatus",
|
---|
| 453 | .callback = req_get_port_status
|
---|
| 454 | },
|
---|
| 455 | {
|
---|
| 456 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 457 | .name = "ClearHubFeature",
|
---|
| 458 | /* Hub features are overcurrent and supply good,
|
---|
[af4e464e] | 459 | * this request may only clear changes that we never report*/
|
---|
[c95c00e] | 460 | .callback = req_nop,
|
---|
| 461 | },
|
---|
| 462 | {
|
---|
| 463 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
| 464 | .name = "ClearPortFeature",
|
---|
| 465 | .callback = req_clear_port_feature
|
---|
| 466 | },
|
---|
| 467 | {
|
---|
| 468 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
| 469 | .name = "GetHubStatus",
|
---|
| 470 | /* UHCI can't report OC condition or,
|
---|
| 471 | * lose power source */
|
---|
| 472 | .callback = virthub_base_get_null_status,
|
---|
| 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_SET_FEATURE),
|
---|
| 481 | .name = "SetHubFeature",
|
---|
| 482 | /* Hub features are overcurrent and supply good,
|
---|
| 483 | * this request may only set changes that we never report*/
|
---|
| 484 | .callback = req_nop,
|
---|
| 485 | },
|
---|
| 486 | {
|
---|
| 487 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
| 488 | .name = "SetPortFeature",
|
---|
| 489 | .callback = req_set_port_feature
|
---|
| 490 | },
|
---|
| 491 | {
|
---|
| 492 | .callback = NULL
|
---|
| 493 | }
|
---|
| 494 | };
|
---|
| 495 |
|
---|
| 496 | static usbvirt_device_ops_t ops = {
|
---|
| 497 | .control = control_transfer_handlers,
|
---|
| 498 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
| 499 | };
|
---|