[9351353] | 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 | */
|
---|
[58563585] | 28 |
|
---|
[17ceb72] | 29 | /** @addtogroup drvusbuhcihc
|
---|
[9351353] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[17ceb72] | 33 | * @brief UHCI Host controller driver routines
|
---|
[9351353] | 34 | */
|
---|
[8064c2f6] | 35 |
|
---|
[9351353] | 36 | #include <adt/list.h>
|
---|
[8064c2f6] | 37 | #include <assert.h>
|
---|
| 38 | #include <async.h>
|
---|
[1ae74c6] | 39 | #include <ddi.h>
|
---|
[8064c2f6] | 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>
|
---|
[8d2dd7f2] | 46 | #include <stdint.h>
|
---|
[8064c2f6] | 47 | #include <str_error.h>
|
---|
[9351353] | 48 |
|
---|
| 49 | #include <usb/debug.h>
|
---|
| 50 | #include <usb/usb.h>
|
---|
[8fc61c8] | 51 | #include <usb/host/utils/malloc32.h>
|
---|
[e6b9182] | 52 | #include <usb/host/bandwidth.h>
|
---|
[129b821f] | 53 | #include <usb/host/utility.h>
|
---|
[9351353] | 54 |
|
---|
[07f49ae] | 55 | #include "uhci_batch.h"
|
---|
[929599a8] | 56 | #include "transfer_list.h"
|
---|
[8064c2f6] | 57 | #include "hc.h"
|
---|
[9351353] | 58 |
|
---|
[8986412] | 59 | #define UHCI_INTR_ALLOW_INTERRUPTS \
|
---|
[af81980] | 60 | (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
|
---|
[8986412] | 61 | #define UHCI_STATUS_USED_INTERRUPTS \
|
---|
| 62 | (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
|
---|
[af81980] | 63 |
|
---|
[d57122c] | 64 | static const irq_pio_range_t uhci_irq_pio_ranges[] = {
|
---|
| 65 | {
|
---|
[8486c07] | 66 | .base = 0,
|
---|
[d57122c] | 67 | .size = sizeof(uhci_regs_t)
|
---|
| 68 | }
|
---|
| 69 | };
|
---|
[5fe0a697] | 70 |
|
---|
[d57122c] | 71 | static const irq_cmd_t uhci_irq_commands[] = {
|
---|
[8486c07] | 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 | }
|
---|
[dfe4955] | 96 | };
|
---|
[302a4b6] | 97 |
|
---|
[3afb758] | 98 | static void hc_init_hw(const hc_t *instance);
|
---|
[5a6cc679] | 99 | static errno_t hc_init_mem_structures(hc_t *instance);
|
---|
| 100 | static errno_t hc_init_transfer_lists(hc_t *instance);
|
---|
[9351353] | 101 |
|
---|
[5a6cc679] | 102 | static errno_t hc_debug_checker(void *arg);
|
---|
[dfe4955] | 103 |
|
---|
[76fbd9a] | 104 |
|
---|
[d57122c] | 105 | /** Generate IRQ code.
|
---|
[6210a333] | 106 | * @param[out] code IRQ code structure.
|
---|
[ba4a03a5] | 107 | * @param[in] hw_res Device's resources.
|
---|
[68e5406] | 108 | * @param[out] irq
|
---|
[dfe4955] | 109 | *
|
---|
| 110 | * @return Error code.
|
---|
| 111 | */
|
---|
[5a6cc679] | 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)
|
---|
[dfe4955] | 113 | {
|
---|
[6210a333] | 114 | assert(code);
|
---|
[ba4a03a5] | 115 | assert(hw_res);
|
---|
[6210a333] | 116 |
|
---|
[ba4a03a5] | 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))
|
---|
[dfe4955] | 122 | return EOVERFLOW;
|
---|
| 123 |
|
---|
[6210a333] | 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);
|
---|
[dfe4955] | 136 |
|
---|
[6210a333] | 137 | memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
|
---|
[ba4a03a5] | 138 | code->ranges[0].base = RNGABS(regs);
|
---|
[6210a333] | 139 |
|
---|
| 140 | memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
|
---|
[ba4a03a5] | 141 | uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
|
---|
[6210a333] | 142 | code->cmds[0].addr = (void*)®isters->usbsts;
|
---|
| 143 | code->cmds[3].addr = (void*)®isters->usbsts;
|
---|
[dfe4955] | 144 |
|
---|
[a1732929] | 145 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
|
---|
[ba4a03a5] | 146 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
---|
| 147 |
|
---|
[68e5406] | 148 | *irq = hw_res->irqs.irqs[0];
|
---|
| 149 | return EOK;
|
---|
[dfe4955] | 150 | }
|
---|
[76fbd9a] | 151 |
|
---|
[3afb758] | 152 | /** Take action based on the interrupt cause.
|
---|
| 153 | *
|
---|
[4bfcf22] | 154 | * @param[in] hcd HCD structure to use.
|
---|
[3afb758] | 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 | */
|
---|
[32fb6bce] | 162 | static void hc_interrupt(bus_t *bus, uint32_t status)
|
---|
[3afb758] | 163 | {
|
---|
[32fb6bce] | 164 | hc_t *instance = bus_to_hc(bus);
|
---|
| 165 |
|
---|
[3afb758] | 166 | /* Lower 2 bits are transaction error and transaction complete */
|
---|
| 167 | if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
|
---|
[4db49344] | 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);
|
---|
[3afb758] | 172 | }
|
---|
[4db49344] | 173 |
|
---|
[3afb758] | 174 | /* Resume interrupts are not supported */
|
---|
| 175 | if (status & UHCI_STATUS_RESUME) {
|
---|
[a1732929] | 176 | usb_log_error("Resume interrupt!");
|
---|
[3afb758] | 177 | }
|
---|
| 178 |
|
---|
| 179 | /* Bits 4 and 5 indicate hc error */
|
---|
| 180 | if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
|
---|
[a1732929] | 181 | usb_log_error("UHCI hardware failure!.");
|
---|
[3afb758] | 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 {
|
---|
[a1732929] | 192 | usb_log_fatal("Too many UHCI hardware failures!.");
|
---|
[32fb6bce] | 193 | hc_gone(&instance->base);
|
---|
[3afb758] | 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
[76fbd9a] | 197 |
|
---|
[02cacce] | 198 | /** Initialize UHCI hc driver structure
|
---|
[9351353] | 199 | *
|
---|
| 200 | * @param[in] instance Memory place to initialize.
|
---|
[7de1988c] | 201 | * @param[in] regs Range of device's I/O control registers.
|
---|
[23f40280] | 202 | * @param[in] interrupts True if hw interrupts should be used.
|
---|
[9351353] | 203 | * @return Error code.
|
---|
| 204 | * @note Should be called only once on any structure.
|
---|
[17ceb72] | 205 | *
|
---|
| 206 | * Initializes memory structures, starts up hw, and launches debugger and
|
---|
| 207 | * interrupt fibrils.
|
---|
[9351353] | 208 | */
|
---|
[5a6cc679] | 209 | errno_t hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
---|
[9351353] | 210 | {
|
---|
[32fb6bce] | 211 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[7813516] | 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;
|
---|
[9351353] | 216 |
|
---|
[fcc525d] | 217 | instance->hw_failures = 0;
|
---|
| 218 |
|
---|
[9351353] | 219 | /* allow access to hc control registers */
|
---|
[5a6cc679] | 220 | errno_t ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
|
---|
[7813516] | 221 | (void **) &instance->registers);
|
---|
[e0d8b740] | 222 | if (ret != EOK) {
|
---|
[a1732929] | 223 | usb_log_error("Failed to gain access to registers: %s.",
|
---|
[7813516] | 224 | str_error(ret));
|
---|
[e0d8b740] | 225 | return ret;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[a1732929] | 228 | usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
|
---|
[7813516] | 229 | hw_res->io_ranges.ranges[0].address.absolute,
|
---|
| 230 | hw_res->io_ranges.ranges[0].size);
|
---|
[3afb758] | 231 |
|
---|
[4db49344] | 232 | ret = hc_init_mem_structures(instance);
|
---|
[e0d8b740] | 233 | if (ret != EOK) {
|
---|
[a1732929] | 234 | usb_log_error("Failed to init UHCI memory structures: %s.",
|
---|
[e0d8b740] | 235 | str_error(ret));
|
---|
| 236 | // TODO: we should disable pio here
|
---|
| 237 | return ret;
|
---|
| 238 | }
|
---|
[7265558] | 239 |
|
---|
[e4d7363] | 240 | return EOK;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[32fb6bce] | 243 | int hc_start(hc_device_t *hcd)
|
---|
[e4d7363] | 244 | {
|
---|
[32fb6bce] | 245 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[c01cd32] | 246 | hc_init_hw(instance);
|
---|
[ea993d18] | 247 | (void)hc_debug_checker;
|
---|
[9351353] | 248 |
|
---|
[32fb6bce] | 249 | return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
|
---|
[9351353] | 250 | }
|
---|
[76fbd9a] | 251 |
|
---|
[129b821f] | 252 | int hc_setup_roothub(hc_device_t *hcd)
|
---|
| 253 | {
|
---|
| 254 | return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[7813516] | 257 | /** Safely dispose host controller internal structures
|
---|
| 258 | *
|
---|
| 259 | * @param[in] instance Host controller structure to use.
|
---|
| 260 | */
|
---|
[32fb6bce] | 261 | int hc_gone(hc_device_t *instance)
|
---|
[7813516] | 262 | {
|
---|
| 263 | assert(instance);
|
---|
| 264 | //TODO Implement
|
---|
[32fb6bce] | 265 | return ENOTSUP;
|
---|
[7813516] | 266 | }
|
---|
| 267 |
|
---|
[17ceb72] | 268 | /** Initialize UHCI hc hw resources.
|
---|
[9351353] | 269 | *
|
---|
| 270 | * @param[in] instance UHCI structure to use.
|
---|
[17ceb72] | 271 | * For magic values see UHCI Design Guide
|
---|
[9351353] | 272 | */
|
---|
[3afb758] | 273 | void hc_init_hw(const hc_t *instance)
|
---|
[9351353] | 274 | {
|
---|
| 275 | assert(instance);
|
---|
[dfe4955] | 276 | uhci_regs_t *registers = instance->registers;
|
---|
[9351353] | 277 |
|
---|
| 278 | /* Reset everything, who knows what touched it before us */
|
---|
| 279 | pio_write_16(®isters->usbcmd, UHCI_CMD_GLOBAL_RESET);
|
---|
[26858040] | 280 | async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
|
---|
[9351353] | 281 | pio_write_16(®isters->usbcmd, 0);
|
---|
| 282 |
|
---|
[26858040] | 283 | /* Reset hc, all states and counters. Hope that hw is not broken */
|
---|
[9351353] | 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 |
|
---|
[eb2a48a] | 288 | /* Set frame to exactly 1ms */
|
---|
| 289 | pio_write_8(®isters->sofmod, 64);
|
---|
| 290 |
|
---|
| 291 | /* Set frame list pointer */
|
---|
[9351353] | 292 | const uint32_t pa = addr_to_phys(instance->frame_list);
|
---|
| 293 | pio_write_32(®isters->flbaseadd, pa);
|
---|
| 294 |
|
---|
[32fb6bce] | 295 | if (instance->base.irq_cap >= 0) {
|
---|
[ff34e5a] | 296 | /* Enable all interrupts, but resume interrupt */
|
---|
| 297 | pio_write_16(&instance->registers->usbintr,
|
---|
[8986412] | 298 | UHCI_INTR_ALLOW_INTERRUPTS);
|
---|
[ff34e5a] | 299 | }
|
---|
[9351353] | 300 |
|
---|
[26858040] | 301 | const uint16_t cmd = pio_read_16(®isters->usbcmd);
|
---|
| 302 | if (cmd != 0)
|
---|
[a1732929] | 303 | usb_log_warning("Previous command value: %x.", cmd);
|
---|
[9351353] | 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 | }
|
---|
[76fbd9a] | 309 |
|
---|
[6832245] | 310 | static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
|
---|
[5fd9c30] | 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 |
|
---|
[e8277c0] | 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 |
|
---|
[5a6cc679] | 329 | static errno_t endpoint_register(endpoint_t *ep)
|
---|
[4db49344] | 330 | {
|
---|
| 331 | hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
|
---|
| 332 |
|
---|
[5a6cc679] | 333 | const errno_t err = usb2_bus_endpoint_register(&hc->bus_helper, ep);
|
---|
[4db49344] | 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 |
|
---|
[0892663a] | 351 | static void endpoint_unregister(endpoint_t *ep)
|
---|
[5dfb70c9] | 352 | {
|
---|
[929599a8] | 353 | hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
|
---|
[d369b3b] | 354 | usb2_bus_endpoint_unregister(&hc->bus_helper, ep);
|
---|
[c913f71e] | 355 |
|
---|
[2755a622] | 356 | // Check for the roothub, as it does not schedule into lists
|
---|
[129b821f] | 357 | if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
|
---|
[2755a622] | 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];
|
---|
[3ac86a4] | 365 | if (!list)
|
---|
| 366 | /*
|
---|
| 367 | * We don't support this combination (e.g. isochronous),
|
---|
| 368 | * so no transfer can be active.
|
---|
| 369 | */
|
---|
| 370 | return;
|
---|
[2755a622] | 371 |
|
---|
| 372 | fibril_mutex_lock(&list->guard);
|
---|
[4db49344] | 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;
|
---|
[5dfb70c9] | 380 | }
|
---|
[929599a8] | 381 |
|
---|
[4db49344] | 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;
|
---|
[929599a8] | 388 | }
|
---|
[4db49344] | 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);
|
---|
[5dfb70c9] | 408 | }
|
---|
| 409 |
|
---|
[d369b3b] | 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 |
|
---|
[f3ae58b] | 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 |
|
---|
[32fb6bce] | 422 | static int hc_status(bus_t *, uint32_t *);
|
---|
| 423 | static int hc_schedule(usb_transfer_batch_t *);
|
---|
| 424 |
|
---|
[6832245] | 425 | static const bus_ops_t uhci_bus_ops = {
|
---|
[32fb6bce] | 426 | .interrupt = hc_interrupt,
|
---|
| 427 | .status = hc_status,
|
---|
| 428 |
|
---|
[d369b3b] | 429 | .device_enumerate = device_enumerate,
|
---|
[f3ae58b] | 430 | .device_gone = device_gone,
|
---|
[d369b3b] | 431 |
|
---|
[e8277c0] | 432 | .endpoint_create = endpoint_create,
|
---|
[4db49344] | 433 | .endpoint_register = endpoint_register,
|
---|
[0892663a] | 434 | .endpoint_unregister = endpoint_unregister,
|
---|
| 435 |
|
---|
[6832245] | 436 | .batch_create = create_transfer_batch,
|
---|
[32fb6bce] | 437 | .batch_schedule = hc_schedule,
|
---|
[6832245] | 438 | .batch_destroy = destroy_transfer_batch,
|
---|
| 439 | };
|
---|
| 440 |
|
---|
[17ceb72] | 441 | /** Initialize UHCI hc memory structures.
|
---|
[9351353] | 442 | *
|
---|
| 443 | * @param[in] instance UHCI structure to use.
|
---|
| 444 | * @return Error code
|
---|
| 445 | * @note Should be called only once on any structure.
|
---|
[17ceb72] | 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)
|
---|
[9351353] | 450 | */
|
---|
[5a6cc679] | 451 | errno_t hc_init_mem_structures(hc_t *instance)
|
---|
[9351353] | 452 | {
|
---|
| 453 | assert(instance);
|
---|
| 454 |
|
---|
[d369b3b] | 455 | usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
|
---|
[e6b9182] | 456 |
|
---|
[d369b3b] | 457 | bus_init(&instance->bus, sizeof(device_t));
|
---|
| 458 | instance->bus.ops = &uhci_bus_ops;
|
---|
[5fd9c30] | 459 |
|
---|
[d369b3b] | 460 | hc_device_setup(&instance->base, &instance->bus);
|
---|
[32fb6bce] | 461 |
|
---|
[3afb758] | 462 | /* Init USB frame list page */
|
---|
[9351353] | 463 | instance->frame_list = get_page();
|
---|
[26858040] | 464 | if (!instance->frame_list) {
|
---|
| 465 | return ENOMEM;
|
---|
| 466 | }
|
---|
[a1732929] | 467 | usb_log_debug("Initialized frame list at %p.", instance->frame_list);
|
---|
[9351353] | 468 |
|
---|
[3afb758] | 469 | /* Init transfer lists */
|
---|
[5a6cc679] | 470 | errno_t ret = hc_init_transfer_lists(instance);
|
---|
[3afb758] | 471 | if (ret != EOK) {
|
---|
[a1732929] | 472 | usb_log_error("Failed to initialize transfer lists.");
|
---|
[3afb758] | 473 | return_page(instance->frame_list);
|
---|
| 474 | return ENOMEM;
|
---|
| 475 | }
|
---|
[4db49344] | 476 | list_initialize(&instance->pending_endpoints);
|
---|
[a1732929] | 477 | usb_log_debug("Initialized transfer lists.");
|
---|
[3afb758] | 478 |
|
---|
| 479 |
|
---|
[9351353] | 480 | /* Set all frames to point to the first queue head */
|
---|
[302a4b6] | 481 | const uint32_t queue = LINK_POINTER_QH(
|
---|
| 482 | addr_to_phys(instance->transfers_interrupt.queue_head));
|
---|
[75f9dcd] | 483 |
|
---|
| 484 | for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
|
---|
[9351353] | 485 | instance->frame_list[i] = queue;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | return EOK;
|
---|
| 489 | }
|
---|
[76fbd9a] | 490 |
|
---|
[17ceb72] | 491 | /** Initialize UHCI hc transfer lists.
|
---|
[9351353] | 492 | *
|
---|
| 493 | * @param[in] instance UHCI structure to use.
|
---|
| 494 | * @return Error code
|
---|
| 495 | * @note Should be called only once on any structure.
|
---|
[17ceb72] | 496 | *
|
---|
| 497 | * Initializes transfer lists and sets them in one chain to support proper
|
---|
| 498 | * USB scheduling. Sets pointer table for quick access.
|
---|
[9351353] | 499 | */
|
---|
[5a6cc679] | 500 | errno_t hc_init_transfer_lists(hc_t *instance)
|
---|
[9351353] | 501 | {
|
---|
| 502 | assert(instance);
|
---|
[27205841] | 503 | #define SETUP_TRANSFER_LIST(type, name) \
|
---|
| 504 | do { \
|
---|
[5a6cc679] | 505 | errno_t ret = transfer_list_init(&instance->transfers_##type, name); \
|
---|
[9351353] | 506 | if (ret != EOK) { \
|
---|
[a1732929] | 507 | usb_log_error("Failed to setup %s transfer list: %s.", \
|
---|
[26858040] | 508 | name, str_error(ret)); \
|
---|
[9351353] | 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; \
|
---|
[27205841] | 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 */
|
---|
[9351353] | 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 |
|
---|
[e247d83] | 530 | /*FSBR, This feature is not needed (adds no benefit) and is supposedly
|
---|
| 531 | * buggy on certain hw, enable at your own risk. */
|
---|
[9351353] | 532 | #ifdef FSBR
|
---|
| 533 | transfer_list_set_next(&instance->transfers_bulk_full,
|
---|
[302a4b6] | 534 | &instance->transfers_control_full);
|
---|
[9351353] | 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 | }
|
---|
[76fbd9a] | 551 |
|
---|
[5a6cc679] | 552 | static errno_t hc_status(bus_t *bus, uint32_t *status)
|
---|
[e26a9d95] | 553 | {
|
---|
[32fb6bce] | 554 | hc_t *instance = bus_to_hc(bus);
|
---|
[e26a9d95] | 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 |
|
---|
[4db49344] | 566 | /**
|
---|
| 567 | * Schedule batch for execution.
|
---|
[9351353] | 568 | *
|
---|
| 569 | * @param[in] instance UHCI structure to use.
|
---|
| 570 | * @param[in] batch Transfer batch to schedule.
|
---|
| 571 | * @return Error code
|
---|
| 572 | */
|
---|
[5a6cc679] | 573 | static errno_t hc_schedule(usb_transfer_batch_t *batch)
|
---|
[9351353] | 574 | {
|
---|
[929599a8] | 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));
|
---|
[c95c00e] | 578 |
|
---|
[929599a8] | 579 | if (batch->target.address == uhci_rh_get_address(&hc->rh))
|
---|
| 580 | return uhci_rh_schedule(&hc->rh, batch);
|
---|
[c95c00e] | 581 |
|
---|
[4db49344] | 582 | transfer_list_t * const list =
|
---|
| 583 | hc->transfers[ep->device->speed][ep->transfer_type];
|
---|
[9351353] | 584 |
|
---|
[4db49344] | 585 | if (!list)
|
---|
| 586 | return ENOTSUP;
|
---|
[76fbd9a] | 587 |
|
---|
[5a6cc679] | 588 | errno_t err;
|
---|
[4db49344] | 589 | if ((err = uhci_transfer_batch_prepare(uhci_batch)))
|
---|
| 590 | return err;
|
---|
[929599a8] | 591 |
|
---|
[4db49344] | 592 | return transfer_list_add_batch(list, uhci_batch);
|
---|
[929599a8] | 593 | }
|
---|
| 594 |
|
---|
[9351353] | 595 | /** Debug function, checks consistency of memory structures.
|
---|
| 596 | *
|
---|
| 597 | * @param[in] arg UHCI structure to use.
|
---|
[17ceb72] | 598 | * @return EOK (should never return)
|
---|
[9351353] | 599 | */
|
---|
[5a6cc679] | 600 | errno_t hc_debug_checker(void *arg)
|
---|
[9351353] | 601 | {
|
---|
[6f122df] | 602 | hc_t *instance = arg;
|
---|
[9351353] | 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)) {
|
---|
[a1732929] | 615 | usb_log_debug2("Command: %X Status: %X Intr: %x",
|
---|
[9351353] | 616 | cmd, sts, intr);
|
---|
| 617 | }
|
---|
| 618 |
|
---|
[e247d83] | 619 | const uintptr_t frame_list =
|
---|
[9351353] | 620 | pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
|
---|
| 621 | if (frame_list != addr_to_phys(instance->frame_list)) {
|
---|
[a1732929] | 622 | usb_log_debug("Framelist address: %p vs. %p.",
|
---|
[4125b7d] | 623 | (void *) frame_list,
|
---|
| 624 | (void *) addr_to_phys(instance->frame_list));
|
---|
[9351353] | 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) {
|
---|
[a1732929] | 633 | usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
|
---|
[4125b7d] | 634 | (void *) expected_pa, frnum, (void *) real_pa);
|
---|
[9351353] | 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) {
|
---|
[a1732929] | 640 | usb_log_debug("Control Slow QH: %p vs. %p.",
|
---|
[4125b7d] | 641 | (void *) expected_pa, (void *) real_pa);
|
---|
[9351353] | 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) {
|
---|
[a1732929] | 647 | usb_log_debug("Control Full QH: %p vs. %p.",
|
---|
[4125b7d] | 648 | (void *) expected_pa, (void *) real_pa);
|
---|
[9351353] | 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 ) {
|
---|
[a1732929] | 654 | usb_log_debug("Bulk QH: %p vs. %p.",
|
---|
[4125b7d] | 655 | (void *) expected_pa, (void *) real_pa);
|
---|
[9351353] | 656 | }
|
---|
| 657 | async_usleep(UHCI_DEBUGER_TIMEOUT);
|
---|
| 658 | }
|
---|
| 659 | return EOK;
|
---|
| 660 | #undef QH
|
---|
| 661 | }
|
---|
| 662 | /**
|
---|
| 663 | * @}
|
---|
| 664 | */
|
---|