| 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 drvusbuhci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | /* XXX Fix this */
|
|---|
| 37 | #define _DDF_DATA_IMPLANT
|
|---|
| 38 |
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <ddf/interrupt.h>
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 | #include <usb/host/hcd.h>
|
|---|
| 44 | #include <usb/host/ddf_helpers.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "uhci.h"
|
|---|
| 47 |
|
|---|
| 48 | #include "res.h"
|
|---|
| 49 | #include "hc.h"
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | /** IRQ handling callback, forward status from call to diver structure.
|
|---|
| 53 | *
|
|---|
| 54 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 55 | * @param[in] iid (Unused).
|
|---|
| 56 | * @param[in] call Pointer to the call from kernel.
|
|---|
| 57 | */
|
|---|
| 58 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 59 | {
|
|---|
| 60 | assert(dev);
|
|---|
| 61 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| 62 | if (!hcd || !hcd->private_data) {
|
|---|
| 63 | usb_log_error("Interrupt on not yet initialized device.\n");
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 | const uint16_t status = IPC_GET_ARG1(*call);
|
|---|
| 67 | hc_interrupt(hcd->private_data, status);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /** Initialize hc and rh DDF structures and their respective drivers.
|
|---|
| 71 | *
|
|---|
| 72 | * @param[in] device DDF instance of the device to use.
|
|---|
| 73 | *
|
|---|
| 74 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 75 | * - gets device's hw resources
|
|---|
| 76 | * - disables UHCI legacy support (PCI config space)
|
|---|
| 77 | * - attempts to enable interrupts
|
|---|
| 78 | * - registers interrupt handler
|
|---|
| 79 | */
|
|---|
| 80 | int device_setup_uhci(ddf_dev_t *device)
|
|---|
| 81 | {
|
|---|
| 82 | if (!device)
|
|---|
| 83 | return EBADMEM;
|
|---|
| 84 |
|
|---|
| 85 | #define CHECK_RET_RETURN(ret, message...) \
|
|---|
| 86 | if (ret != EOK) { \
|
|---|
| 87 | usb_log_error(message); \
|
|---|
| 88 | return ret; \
|
|---|
| 89 | } else (void)0
|
|---|
| 90 |
|
|---|
| 91 | uintptr_t reg_base = 0;
|
|---|
| 92 | size_t reg_size = 0;
|
|---|
| 93 | int irq = 0;
|
|---|
| 94 |
|
|---|
| 95 | int ret = get_my_registers(device, ®_base, ®_size, &irq);
|
|---|
| 96 | CHECK_RET_RETURN(ret, "Failed to get I/O region for %" PRIun ": %s.\n",
|
|---|
| 97 | ddf_dev_get_handle(device), str_error(ret));
|
|---|
| 98 | usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
|
|---|
| 99 | (void *) reg_base, reg_size, irq);
|
|---|
| 100 |
|
|---|
| 101 | const size_t ranges_count = hc_irq_pio_range_count();
|
|---|
| 102 | const size_t cmds_count = hc_irq_cmd_count();
|
|---|
| 103 | irq_pio_range_t irq_ranges[ranges_count];
|
|---|
| 104 | irq_cmd_t irq_cmds[cmds_count];
|
|---|
| 105 | ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
|
|---|
| 106 | sizeof(irq_cmds), reg_base, reg_size);
|
|---|
| 107 | CHECK_RET_RETURN(ret, "Failed to generate IRQ commands: %s.\n",
|
|---|
| 108 | str_error(ret));
|
|---|
| 109 |
|
|---|
| 110 | irq_code_t irq_code = {
|
|---|
| 111 | .rangecount = ranges_count,
|
|---|
| 112 | .ranges = irq_ranges,
|
|---|
| 113 | .cmdcount = cmds_count,
|
|---|
| 114 | .cmds = irq_cmds
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | /* Register handler to avoid interrupt lockup */
|
|---|
| 118 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
|---|
| 119 | CHECK_RET_RETURN(ret, "Failed to register interrupt handler: %s.\n",
|
|---|
| 120 | str_error(ret));
|
|---|
| 121 |
|
|---|
| 122 | ret = disable_legacy(device);
|
|---|
| 123 | CHECK_RET_RETURN(ret, "Failed to disable legacy USB: %s.\n",
|
|---|
| 124 | str_error(ret));
|
|---|
| 125 |
|
|---|
| 126 | bool interrupts = false;
|
|---|
| 127 | ret = enable_interrupts(device);
|
|---|
| 128 | if (ret != EOK) {
|
|---|
| 129 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 130 | " Falling back to polling.\n", str_error(ret));
|
|---|
| 131 | } else {
|
|---|
| 132 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 133 | interrupts = true;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | ddf_fun_t *hc_fun = NULL;
|
|---|
| 137 | ret = hcd_ddf_setup_device(device, &hc_fun, USB_SPEED_FULL,
|
|---|
| 138 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
|---|
| 139 | CHECK_RET_RETURN(ret, "Failed to setup UHCI HCD.\n");
|
|---|
| 140 |
|
|---|
| 141 | hc_t *hc = malloc(sizeof(hc_t));
|
|---|
| 142 | ret = hc ? EOK : ENOMEM;
|
|---|
| 143 | CHECK_RET_RETURN(ret, "Failed to allocate UHCI HC structure.\n");
|
|---|
| 144 |
|
|---|
| 145 | ret = hc_init(hc, (void*)reg_base, reg_size, interrupts);
|
|---|
| 146 | CHECK_RET_RETURN(ret,
|
|---|
| 147 | "Failed to init uhci_hcd: %s.\n", str_error(ret));
|
|---|
| 148 |
|
|---|
| 149 | hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
|
|---|
| 150 |
|
|---|
| 151 | // TODO: Undo hcd_setup_device
|
|---|
| 152 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
|---|
| 153 | if (ret != EOK) { \
|
|---|
| 154 | hc_fini(hc); \
|
|---|
| 155 | CHECK_RET_RETURN(ret, message); \
|
|---|
| 156 | return ret; \
|
|---|
| 157 | } else (void)0
|
|---|
| 158 |
|
|---|
| 159 | ret = hcd_ddf_setup_root_hub(device, USB_SPEED_FULL);
|
|---|
| 160 | CHECK_RET_FINI_RETURN(ret,
|
|---|
| 161 | "Failed to setup UHCI root hub: %s.\n", str_error(ret));
|
|---|
| 162 |
|
|---|
| 163 | return EOK;
|
|---|
| 164 | #undef CHECK_RET_FINI_RETURN
|
|---|
| 165 | }
|
|---|
| 166 | /**
|
|---|
| 167 | * @}
|
|---|
| 168 | */
|
|---|