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