| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 | /** @addtogroup drvusbuhcihc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI Host controller driver routines
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <ddi.h>
|
|---|
| 40 | #include <device/hw_res_parsed.h>
|
|---|
| 41 | #include <fibril.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <macros.h>
|
|---|
| 44 | #include <mem.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <stdint.h>
|
|---|
| 47 | #include <str_error.h>
|
|---|
| 48 |
|
|---|
| 49 | #include <usb/debug.h>
|
|---|
| 50 | #include <usb/usb.h>
|
|---|
| 51 | #include <usb/host/utils/malloc32.h>
|
|---|
| 52 | #include <usb/host/bandwidth.h>
|
|---|
| 53 | #include <usb/host/utility.h>
|
|---|
| 54 |
|
|---|
| 55 | #include "uhci_batch.h"
|
|---|
| 56 | #include "transfer_list.h"
|
|---|
| 57 | #include "hc.h"
|
|---|
| 58 |
|
|---|
| 59 | #define UHCI_INTR_ALLOW_INTERRUPTS \
|
|---|
| 60 | (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
|
|---|
| 61 | #define UHCI_STATUS_USED_INTERRUPTS \
|
|---|
| 62 | (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
|
|---|
| 63 |
|
|---|
| 64 | static const irq_pio_range_t uhci_irq_pio_ranges[] = {
|
|---|
| 65 | {
|
|---|
| 66 | .base = 0,
|
|---|
| 67 | .size = sizeof(uhci_regs_t)
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | static const irq_cmd_t uhci_irq_commands[] = {
|
|---|
| 72 | {
|
|---|
| 73 | .cmd = CMD_PIO_READ_16,
|
|---|
| 74 | .dstarg = 1,
|
|---|
| 75 | .addr = NULL
|
|---|
| 76 | },
|
|---|
| 77 | {
|
|---|
| 78 | .cmd = CMD_AND,
|
|---|
| 79 | .srcarg = 1,
|
|---|
| 80 | .dstarg = 2,
|
|---|
| 81 | .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
|
|---|
| 82 | },
|
|---|
| 83 | {
|
|---|
| 84 | .cmd = CMD_PREDICATE,
|
|---|
| 85 | .srcarg = 2,
|
|---|
| 86 | .value = 2
|
|---|
| 87 | },
|
|---|
| 88 | {
|
|---|
| 89 | .cmd = CMD_PIO_WRITE_A_16,
|
|---|
| 90 | .srcarg = 1,
|
|---|
| 91 | .addr = NULL
|
|---|
| 92 | },
|
|---|
| 93 | {
|
|---|
| 94 | .cmd = CMD_ACCEPT
|
|---|
| 95 | }
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | static void hc_init_hw(const hc_t *instance);
|
|---|
| 99 | static errno_t hc_init_mem_structures(hc_t *instance);
|
|---|
| 100 | static errno_t hc_init_transfer_lists(hc_t *instance);
|
|---|
| 101 |
|
|---|
| 102 | static errno_t hc_debug_checker(void *arg);
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | /** Generate IRQ code.
|
|---|
| 106 | * @param[out] code IRQ code structure.
|
|---|
| 107 | * @param[in] hw_res Device's resources.
|
|---|
| 108 | * @param[out] irq
|
|---|
| 109 | *
|
|---|
| 110 | * @return Error code.
|
|---|
| 111 | */
|
|---|
| 112 | errno_t hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res, int *irq)
|
|---|
| 113 | {
|
|---|
| 114 | assert(code);
|
|---|
| 115 | assert(hw_res);
|
|---|
| 116 |
|
|---|
| 117 | if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
|
|---|
| 118 | return EINVAL;
|
|---|
| 119 | const addr_range_t regs = hw_res->io_ranges.ranges[0];
|
|---|
| 120 |
|
|---|
| 121 | if (RNGSZ(regs) < sizeof(uhci_regs_t))
|
|---|
| 122 | return EOVERFLOW;
|
|---|
| 123 |
|
|---|
| 124 | code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
|
|---|
| 125 | if (code->ranges == NULL)
|
|---|
| 126 | return ENOMEM;
|
|---|
| 127 |
|
|---|
| 128 | code->cmds = malloc(sizeof(uhci_irq_commands));
|
|---|
| 129 | if (code->cmds == NULL) {
|
|---|
| 130 | free(code->ranges);
|
|---|
| 131 | return ENOMEM;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
|
|---|
| 135 | code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
|
|---|
| 136 |
|
|---|
| 137 | memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
|
|---|
| 138 | code->ranges[0].base = RNGABS(regs);
|
|---|
| 139 |
|
|---|
| 140 | memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
|
|---|
| 141 | uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
|
|---|
| 142 | code->cmds[0].addr = (void*)®isters->usbsts;
|
|---|
| 143 | code->cmds[3].addr = (void*)®isters->usbsts;
|
|---|
| 144 |
|
|---|
| 145 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
|
|---|
| 146 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
|---|
| 147 |
|
|---|
| 148 | *irq = hw_res->irqs.irqs[0];
|
|---|
| 149 | return EOK;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /** Take action based on the interrupt cause.
|
|---|
| 153 | *
|
|---|
| 154 | * @param[in] hcd HCD structure to use.
|
|---|
| 155 | * @param[in] status Value of the status register at the time of interrupt.
|
|---|
| 156 | *
|
|---|
| 157 | * Interrupt might indicate:
|
|---|
| 158 | * - transaction completed, either by triggering IOC, SPD, or an error
|
|---|
| 159 | * - some kind of device error
|
|---|
| 160 | * - resume from suspend state (not implemented)
|
|---|
| 161 | */
|
|---|
| 162 | static void hc_interrupt(bus_t *bus, uint32_t status)
|
|---|
| 163 | {
|
|---|
| 164 | hc_t *instance = bus_to_hc(bus);
|
|---|
| 165 |
|
|---|
| 166 | /* Lower 2 bits are transaction error and transaction complete */
|
|---|
| 167 | if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
|
|---|
| 168 | transfer_list_check_finished(&instance->transfers_interrupt);
|
|---|
| 169 | transfer_list_check_finished(&instance->transfers_control_slow);
|
|---|
| 170 | transfer_list_check_finished(&instance->transfers_control_full);
|
|---|
| 171 | transfer_list_check_finished(&instance->transfers_bulk_full);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /* Resume interrupts are not supported */
|
|---|
| 175 | if (status & UHCI_STATUS_RESUME) {
|
|---|
| 176 | usb_log_error("Resume interrupt!");
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /* Bits 4 and 5 indicate hc error */
|
|---|
| 180 | if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
|
|---|
| 181 | usb_log_error("UHCI hardware failure!.");
|
|---|
| 182 | ++instance->hw_failures;
|
|---|
| 183 | transfer_list_abort_all(&instance->transfers_interrupt);
|
|---|
| 184 | transfer_list_abort_all(&instance->transfers_control_slow);
|
|---|
| 185 | transfer_list_abort_all(&instance->transfers_control_full);
|
|---|
| 186 | transfer_list_abort_all(&instance->transfers_bulk_full);
|
|---|
| 187 |
|
|---|
| 188 | if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
|
|---|
| 189 | /* reinitialize hw, this triggers virtual disconnect*/
|
|---|
| 190 | hc_init_hw(instance);
|
|---|
| 191 | } else {
|
|---|
| 192 | usb_log_fatal("Too many UHCI hardware failures!.");
|
|---|
| 193 | hc_gone(&instance->base);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** Initialize UHCI hc driver structure
|
|---|
| 199 | *
|
|---|
| 200 | * @param[in] instance Memory place to initialize.
|
|---|
| 201 | * @param[in] regs Range of device's I/O control registers.
|
|---|
| 202 | * @param[in] interrupts True if hw interrupts should be used.
|
|---|
| 203 | * @return Error code.
|
|---|
| 204 | * @note Should be called only once on any structure.
|
|---|
| 205 | *
|
|---|
| 206 | * Initializes memory structures, starts up hw, and launches debugger and
|
|---|
| 207 | * interrupt fibrils.
|
|---|
| 208 | */
|
|---|
| 209 | errno_t hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
|---|
| 210 | {
|
|---|
| 211 | hc_t *instance = hcd_to_hc(hcd);
|
|---|
| 212 | assert(hw_res);
|
|---|
| 213 | if (hw_res->io_ranges.count != 1 ||
|
|---|
| 214 | hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
|
|---|
| 215 | return EINVAL;
|
|---|
| 216 |
|
|---|
| 217 | instance->hw_failures = 0;
|
|---|
| 218 |
|
|---|
| 219 | /* allow access to hc control registers */
|
|---|
| 220 | errno_t ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
|
|---|
| 221 | (void **) &instance->registers);
|
|---|
| 222 | if (ret != EOK) {
|
|---|
| 223 | usb_log_error("Failed to gain access to registers: %s.",
|
|---|
| 224 | str_error(ret));
|
|---|
| 225 | return ret;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
|
|---|
| 229 | hw_res->io_ranges.ranges[0].address.absolute,
|
|---|
| 230 | hw_res->io_ranges.ranges[0].size);
|
|---|
| 231 |
|
|---|
| 232 | ret = hc_init_mem_structures(instance);
|
|---|
| 233 | if (ret != EOK) {
|
|---|
| 234 | usb_log_error("Failed to init UHCI memory structures: %s.",
|
|---|
| 235 | str_error(ret));
|
|---|
| 236 | // TODO: we should disable pio here
|
|---|
| 237 | return ret;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | int hc_start(hc_device_t *hcd)
|
|---|
| 244 | {
|
|---|
| 245 | hc_t *instance = hcd_to_hc(hcd);
|
|---|
| 246 | hc_init_hw(instance);
|
|---|
| 247 | (void)hc_debug_checker;
|
|---|
| 248 |
|
|---|
| 249 | return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | int hc_setup_roothub(hc_device_t *hcd)
|
|---|
| 253 | {
|
|---|
| 254 | return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /** Safely dispose host controller internal structures
|
|---|
| 258 | *
|
|---|
| 259 | * @param[in] instance Host controller structure to use.
|
|---|
| 260 | */
|
|---|
| 261 | int hc_gone(hc_device_t *instance)
|
|---|
| 262 | {
|
|---|
| 263 | assert(instance);
|
|---|
| 264 | //TODO Implement
|
|---|
| 265 | return ENOTSUP;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** Initialize UHCI hc hw resources.
|
|---|
| 269 | *
|
|---|
| 270 | * @param[in] instance UHCI structure to use.
|
|---|
| 271 | * For magic values see UHCI Design Guide
|
|---|
| 272 | */
|
|---|
| 273 | void hc_init_hw(const hc_t *instance)
|
|---|
| 274 | {
|
|---|
| 275 | assert(instance);
|
|---|
| 276 | uhci_regs_t *registers = instance->registers;
|
|---|
| 277 |
|
|---|
| 278 | /* Reset everything, who knows what touched it before us */
|
|---|
| 279 | pio_write_16(®isters->usbcmd, UHCI_CMD_GLOBAL_RESET);
|
|---|
| 280 | async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
|
|---|
| 281 | pio_write_16(®isters->usbcmd, 0);
|
|---|
| 282 |
|
|---|
| 283 | /* Reset hc, all states and counters. Hope that hw is not broken */
|
|---|
| 284 | pio_write_16(®isters->usbcmd, UHCI_CMD_HCRESET);
|
|---|
| 285 | do { async_usleep(10); }
|
|---|
| 286 | while ((pio_read_16(®isters->usbcmd) & UHCI_CMD_HCRESET) != 0);
|
|---|
| 287 |
|
|---|
| 288 | /* Set frame to exactly 1ms */
|
|---|
| 289 | pio_write_8(®isters->sofmod, 64);
|
|---|
| 290 |
|
|---|
| 291 | /* Set frame list pointer */
|
|---|
| 292 | const uint32_t pa = addr_to_phys(instance->frame_list);
|
|---|
| 293 | pio_write_32(®isters->flbaseadd, pa);
|
|---|
| 294 |
|
|---|
| 295 | if (instance->base.irq_cap >= 0) {
|
|---|
| 296 | /* Enable all interrupts, but resume interrupt */
|
|---|
| 297 | pio_write_16(&instance->registers->usbintr,
|
|---|
| 298 | UHCI_INTR_ALLOW_INTERRUPTS);
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | const uint16_t cmd = pio_read_16(®isters->usbcmd);
|
|---|
| 302 | if (cmd != 0)
|
|---|
| 303 | usb_log_warning("Previous command value: %x.", cmd);
|
|---|
| 304 |
|
|---|
| 305 | /* Start the hc with large(64B) packet FSBR */
|
|---|
| 306 | pio_write_16(®isters->usbcmd,
|
|---|
| 307 | UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
|
|---|
| 311 | {
|
|---|
| 312 | uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
|
|---|
| 313 | return &batch->base;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | static void destroy_transfer_batch(usb_transfer_batch_t *batch)
|
|---|
| 317 | {
|
|---|
| 318 | uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | static endpoint_t *endpoint_create(device_t *device, const usb_endpoint_descriptors_t *desc)
|
|---|
| 322 | {
|
|---|
| 323 | endpoint_t *ep = calloc(1, sizeof(uhci_endpoint_t));
|
|---|
| 324 | if (ep)
|
|---|
| 325 | endpoint_init(ep, device, desc);
|
|---|
| 326 | return ep;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | static errno_t endpoint_register(endpoint_t *ep)
|
|---|
| 330 | {
|
|---|
| 331 | hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
|
|---|
| 332 |
|
|---|
| 333 | const errno_t err = usb2_bus_endpoint_register(&hc->bus_helper, ep);
|
|---|
| 334 | if (err)
|
|---|
| 335 | return err;
|
|---|
| 336 |
|
|---|
| 337 | transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
|
|---|
| 338 | if (!list)
|
|---|
| 339 | /*
|
|---|
| 340 | * We don't support this combination (e.g. isochronous). Do not
|
|---|
| 341 | * fail early, because that would block any device with these
|
|---|
| 342 | * endpoints from connecting. Instead, make sure these transfers
|
|---|
| 343 | * are denied soon enough with ENOTSUP not to fail on asserts.
|
|---|
| 344 | */
|
|---|
| 345 | return EOK;
|
|---|
| 346 |
|
|---|
| 347 | endpoint_set_online(ep, &list->guard);
|
|---|
| 348 | return EOK;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | static void endpoint_unregister(endpoint_t *ep)
|
|---|
| 352 | {
|
|---|
| 353 | hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
|
|---|
| 354 | usb2_bus_endpoint_unregister(&hc->bus_helper, ep);
|
|---|
| 355 |
|
|---|
| 356 | // Check for the roothub, as it does not schedule into lists
|
|---|
| 357 | if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
|
|---|
| 358 | // FIXME: We shall check the roothub for active transfer. But
|
|---|
| 359 | // as it is polling, there is no way to make it stop doing so.
|
|---|
| 360 | // Return after rewriting uhci rh.
|
|---|
| 361 | return;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
|
|---|
| 365 | if (!list)
|
|---|
| 366 | /*
|
|---|
| 367 | * We don't support this combination (e.g. isochronous),
|
|---|
| 368 | * so no transfer can be active.
|
|---|
| 369 | */
|
|---|
| 370 | return;
|
|---|
| 371 |
|
|---|
| 372 | fibril_mutex_lock(&list->guard);
|
|---|
| 373 |
|
|---|
| 374 | endpoint_set_offline_locked(ep);
|
|---|
| 375 | /* From now on, no other transfer will be scheduled. */
|
|---|
| 376 |
|
|---|
| 377 | if (!ep->active_batch) {
|
|---|
| 378 | fibril_mutex_unlock(&list->guard);
|
|---|
| 379 | return;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /* First, offer the batch a short chance to be finished. */
|
|---|
| 383 | endpoint_wait_timeout_locked(ep, 10000);
|
|---|
| 384 |
|
|---|
| 385 | if (!ep->active_batch) {
|
|---|
| 386 | fibril_mutex_unlock(&list->guard);
|
|---|
| 387 | return;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | uhci_transfer_batch_t * const batch =
|
|---|
| 391 | uhci_transfer_batch_get(ep->active_batch);
|
|---|
| 392 |
|
|---|
| 393 | /* Remove the batch from the schedule to stop it from being finished. */
|
|---|
| 394 | endpoint_deactivate_locked(ep);
|
|---|
| 395 | transfer_list_remove_batch(list, batch);
|
|---|
| 396 |
|
|---|
| 397 | fibril_mutex_unlock(&list->guard);
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | * We removed the batch from software schedule only, it's still possible
|
|---|
| 401 | * that HC has it in its caches. Better wait a while before we release
|
|---|
| 402 | * the buffers.
|
|---|
| 403 | */
|
|---|
| 404 | async_usleep(20000);
|
|---|
| 405 | batch->base.error = EINTR;
|
|---|
| 406 | batch->base.transferred_size = 0;
|
|---|
| 407 | usb_transfer_batch_finish(&batch->base);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | static int device_enumerate(device_t *dev)
|
|---|
| 411 | {
|
|---|
| 412 | hc_t * const hc = bus_to_hc(dev->bus);
|
|---|
| 413 | return usb2_bus_device_enumerate(&hc->bus_helper, dev);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | static void device_gone(device_t *dev)
|
|---|
| 417 | {
|
|---|
| 418 | hc_t * const hc = bus_to_hc(dev->bus);
|
|---|
| 419 | usb2_bus_device_gone(&hc->bus_helper, dev);
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | static int hc_status(bus_t *, uint32_t *);
|
|---|
| 423 | static int hc_schedule(usb_transfer_batch_t *);
|
|---|
| 424 |
|
|---|
| 425 | static const bus_ops_t uhci_bus_ops = {
|
|---|
| 426 | .interrupt = hc_interrupt,
|
|---|
| 427 | .status = hc_status,
|
|---|
| 428 |
|
|---|
| 429 | .device_enumerate = device_enumerate,
|
|---|
| 430 | .device_gone = device_gone,
|
|---|
| 431 |
|
|---|
| 432 | .endpoint_create = endpoint_create,
|
|---|
| 433 | .endpoint_register = endpoint_register,
|
|---|
| 434 | .endpoint_unregister = endpoint_unregister,
|
|---|
| 435 |
|
|---|
| 436 | .batch_create = create_transfer_batch,
|
|---|
| 437 | .batch_schedule = hc_schedule,
|
|---|
| 438 | .batch_destroy = destroy_transfer_batch,
|
|---|
| 439 | };
|
|---|
| 440 |
|
|---|
| 441 | /** Initialize UHCI hc memory structures.
|
|---|
| 442 | *
|
|---|
| 443 | * @param[in] instance UHCI structure to use.
|
|---|
| 444 | * @return Error code
|
|---|
| 445 | * @note Should be called only once on any structure.
|
|---|
| 446 | *
|
|---|
| 447 | * Structures:
|
|---|
| 448 | * - transfer lists (queue heads need to be accessible by the hw)
|
|---|
| 449 | * - frame list page (needs to be one UHCI hw accessible 4K page)
|
|---|
| 450 | */
|
|---|
| 451 | errno_t hc_init_mem_structures(hc_t *instance)
|
|---|
| 452 | {
|
|---|
| 453 | assert(instance);
|
|---|
| 454 |
|
|---|
| 455 | usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
|
|---|
| 456 |
|
|---|
| 457 | bus_init(&instance->bus, sizeof(device_t));
|
|---|
| 458 | instance->bus.ops = &uhci_bus_ops;
|
|---|
| 459 |
|
|---|
| 460 | hc_device_setup(&instance->base, &instance->bus);
|
|---|
| 461 |
|
|---|
| 462 | /* Init USB frame list page */
|
|---|
| 463 | instance->frame_list = get_page();
|
|---|
| 464 | if (!instance->frame_list) {
|
|---|
| 465 | return ENOMEM;
|
|---|
| 466 | }
|
|---|
| 467 | usb_log_debug("Initialized frame list at %p.", instance->frame_list);
|
|---|
| 468 |
|
|---|
| 469 | /* Init transfer lists */
|
|---|
| 470 | errno_t ret = hc_init_transfer_lists(instance);
|
|---|
| 471 | if (ret != EOK) {
|
|---|
| 472 | usb_log_error("Failed to initialize transfer lists.");
|
|---|
| 473 | return_page(instance->frame_list);
|
|---|
| 474 | return ENOMEM;
|
|---|
| 475 | }
|
|---|
| 476 | list_initialize(&instance->pending_endpoints);
|
|---|
| 477 | usb_log_debug("Initialized transfer lists.");
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 | /* Set all frames to point to the first queue head */
|
|---|
| 481 | const uint32_t queue = LINK_POINTER_QH(
|
|---|
| 482 | addr_to_phys(instance->transfers_interrupt.queue_head));
|
|---|
| 483 |
|
|---|
| 484 | for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
|
|---|
| 485 | instance->frame_list[i] = queue;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | return EOK;
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | /** Initialize UHCI hc transfer lists.
|
|---|
| 492 | *
|
|---|
| 493 | * @param[in] instance UHCI structure to use.
|
|---|
| 494 | * @return Error code
|
|---|
| 495 | * @note Should be called only once on any structure.
|
|---|
| 496 | *
|
|---|
| 497 | * Initializes transfer lists and sets them in one chain to support proper
|
|---|
| 498 | * USB scheduling. Sets pointer table for quick access.
|
|---|
| 499 | */
|
|---|
| 500 | errno_t hc_init_transfer_lists(hc_t *instance)
|
|---|
| 501 | {
|
|---|
| 502 | assert(instance);
|
|---|
| 503 | #define SETUP_TRANSFER_LIST(type, name) \
|
|---|
| 504 | do { \
|
|---|
| 505 | errno_t ret = transfer_list_init(&instance->transfers_##type, name); \
|
|---|
| 506 | if (ret != EOK) { \
|
|---|
| 507 | usb_log_error("Failed to setup %s transfer list: %s.", \
|
|---|
| 508 | name, str_error(ret)); \
|
|---|
| 509 | transfer_list_fini(&instance->transfers_bulk_full); \
|
|---|
| 510 | transfer_list_fini(&instance->transfers_control_full); \
|
|---|
| 511 | transfer_list_fini(&instance->transfers_control_slow); \
|
|---|
| 512 | transfer_list_fini(&instance->transfers_interrupt); \
|
|---|
| 513 | return ret; \
|
|---|
| 514 | } \
|
|---|
| 515 | } while (0)
|
|---|
| 516 |
|
|---|
| 517 | SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
|
|---|
| 518 | SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
|
|---|
| 519 | SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
|
|---|
| 520 | SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
|
|---|
| 521 | #undef SETUP_TRANSFER_LIST
|
|---|
| 522 | /* Connect lists into one schedule */
|
|---|
| 523 | transfer_list_set_next(&instance->transfers_control_full,
|
|---|
| 524 | &instance->transfers_bulk_full);
|
|---|
| 525 | transfer_list_set_next(&instance->transfers_control_slow,
|
|---|
| 526 | &instance->transfers_control_full);
|
|---|
| 527 | transfer_list_set_next(&instance->transfers_interrupt,
|
|---|
| 528 | &instance->transfers_control_slow);
|
|---|
| 529 |
|
|---|
| 530 | /*FSBR, This feature is not needed (adds no benefit) and is supposedly
|
|---|
| 531 | * buggy on certain hw, enable at your own risk. */
|
|---|
| 532 | #ifdef FSBR
|
|---|
| 533 | transfer_list_set_next(&instance->transfers_bulk_full,
|
|---|
| 534 | &instance->transfers_control_full);
|
|---|
| 535 | #endif
|
|---|
| 536 |
|
|---|
| 537 | /* Assign pointers to be used during scheduling */
|
|---|
| 538 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
|
|---|
| 539 | &instance->transfers_interrupt;
|
|---|
| 540 | instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
|
|---|
| 541 | &instance->transfers_interrupt;
|
|---|
| 542 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
|
|---|
| 543 | &instance->transfers_control_full;
|
|---|
| 544 | instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
|
|---|
| 545 | &instance->transfers_control_slow;
|
|---|
| 546 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
|
|---|
| 547 | &instance->transfers_bulk_full;
|
|---|
| 548 |
|
|---|
| 549 | return EOK;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | static errno_t hc_status(bus_t *bus, uint32_t *status)
|
|---|
| 553 | {
|
|---|
| 554 | hc_t *instance = bus_to_hc(bus);
|
|---|
| 555 | assert(status);
|
|---|
| 556 |
|
|---|
| 557 | *status = 0;
|
|---|
| 558 | if (instance->registers) {
|
|---|
| 559 | uint16_t s = pio_read_16(&instance->registers->usbsts);
|
|---|
| 560 | pio_write_16(&instance->registers->usbsts, s);
|
|---|
| 561 | *status = s;
|
|---|
| 562 | }
|
|---|
| 563 | return EOK;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | /**
|
|---|
| 567 | * Schedule batch for execution.
|
|---|
| 568 | *
|
|---|
| 569 | * @param[in] instance UHCI structure to use.
|
|---|
| 570 | * @param[in] batch Transfer batch to schedule.
|
|---|
| 571 | * @return Error code
|
|---|
| 572 | */
|
|---|
| 573 | static errno_t hc_schedule(usb_transfer_batch_t *batch)
|
|---|
| 574 | {
|
|---|
| 575 | uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
|
|---|
| 576 | endpoint_t *ep = batch->ep;
|
|---|
| 577 | hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
|
|---|
| 578 |
|
|---|
| 579 | if (batch->target.address == uhci_rh_get_address(&hc->rh))
|
|---|
| 580 | return uhci_rh_schedule(&hc->rh, batch);
|
|---|
| 581 |
|
|---|
| 582 | transfer_list_t * const list =
|
|---|
| 583 | hc->transfers[ep->device->speed][ep->transfer_type];
|
|---|
| 584 |
|
|---|
| 585 | if (!list)
|
|---|
| 586 | return ENOTSUP;
|
|---|
| 587 |
|
|---|
| 588 | errno_t err;
|
|---|
| 589 | if ((err = uhci_transfer_batch_prepare(uhci_batch)))
|
|---|
| 590 | return err;
|
|---|
| 591 |
|
|---|
| 592 | return transfer_list_add_batch(list, uhci_batch);
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | /** Debug function, checks consistency of memory structures.
|
|---|
| 596 | *
|
|---|
| 597 | * @param[in] arg UHCI structure to use.
|
|---|
| 598 | * @return EOK (should never return)
|
|---|
| 599 | */
|
|---|
| 600 | errno_t hc_debug_checker(void *arg)
|
|---|
| 601 | {
|
|---|
| 602 | hc_t *instance = arg;
|
|---|
| 603 | assert(instance);
|
|---|
| 604 |
|
|---|
| 605 | #define QH(queue) \
|
|---|
| 606 | instance->transfers_##queue.queue_head
|
|---|
| 607 |
|
|---|
| 608 | while (1) {
|
|---|
| 609 | const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
|
|---|
| 610 | const uint16_t sts = pio_read_16(&instance->registers->usbsts);
|
|---|
| 611 | const uint16_t intr =
|
|---|
| 612 | pio_read_16(&instance->registers->usbintr);
|
|---|
| 613 |
|
|---|
| 614 | if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
|
|---|
| 615 | usb_log_debug2("Command: %X Status: %X Intr: %x",
|
|---|
| 616 | cmd, sts, intr);
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | const uintptr_t frame_list =
|
|---|
| 620 | pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
|
|---|
| 621 | if (frame_list != addr_to_phys(instance->frame_list)) {
|
|---|
| 622 | usb_log_debug("Framelist address: %p vs. %p.",
|
|---|
| 623 | (void *) frame_list,
|
|---|
| 624 | (void *) addr_to_phys(instance->frame_list));
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| 627 | int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
|
|---|
| 628 |
|
|---|
| 629 | uintptr_t expected_pa = instance->frame_list[frnum]
|
|---|
| 630 | & LINK_POINTER_ADDRESS_MASK;
|
|---|
| 631 | uintptr_t real_pa = addr_to_phys(QH(interrupt));
|
|---|
| 632 | if (expected_pa != real_pa) {
|
|---|
| 633 | usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
|
|---|
| 634 | (void *) expected_pa, frnum, (void *) real_pa);
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| 637 | expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
|
|---|
| 638 | real_pa = addr_to_phys(QH(control_slow));
|
|---|
| 639 | if (expected_pa != real_pa) {
|
|---|
| 640 | usb_log_debug("Control Slow QH: %p vs. %p.",
|
|---|
| 641 | (void *) expected_pa, (void *) real_pa);
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
|
|---|
| 645 | real_pa = addr_to_phys(QH(control_full));
|
|---|
| 646 | if (expected_pa != real_pa) {
|
|---|
| 647 | usb_log_debug("Control Full QH: %p vs. %p.",
|
|---|
| 648 | (void *) expected_pa, (void *) real_pa);
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
|
|---|
| 652 | real_pa = addr_to_phys(QH(bulk_full));
|
|---|
| 653 | if (expected_pa != real_pa ) {
|
|---|
| 654 | usb_log_debug("Bulk QH: %p vs. %p.",
|
|---|
| 655 | (void *) expected_pa, (void *) real_pa);
|
|---|
| 656 | }
|
|---|
| 657 | async_usleep(UHCI_DEBUGER_TIMEOUT);
|
|---|
| 658 | }
|
|---|
| 659 | return EOK;
|
|---|
| 660 | #undef QH
|
|---|
| 661 | }
|
|---|
| 662 | /**
|
|---|
| 663 | * @}
|
|---|
| 664 | */
|
|---|