| 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 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <ddf/interrupt.h>
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 | #include <usb/host/hcd.h>
|
|---|
| 41 | #include <usb/host/ddf_helpers.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "uhci.h"
|
|---|
| 44 |
|
|---|
| 45 | #include "res.h"
|
|---|
| 46 | #include "hc.h"
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /** IRQ handling callback, forward status from call to diver structure.
|
|---|
| 50 | *
|
|---|
| 51 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 52 | * @param[in] iid (Unused).
|
|---|
| 53 | * @param[in] call Pointer to the call from kernel.
|
|---|
| 54 | */
|
|---|
| 55 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 56 | {
|
|---|
| 57 | assert(dev);
|
|---|
| 58 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| 59 | if (!hcd || !hcd->driver.data) {
|
|---|
| 60 | usb_log_error("Interrupt on not yet initialized device.\n");
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 | const uint16_t status = IPC_GET_ARG1(*call);
|
|---|
| 64 | hc_interrupt(hcd->driver.data, status);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** Initialize hc and rh DDF structures and their respective drivers.
|
|---|
| 68 | *
|
|---|
| 69 | * @param[in] device DDF instance of the device to use.
|
|---|
| 70 | *
|
|---|
| 71 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 72 | * - gets device's hw resources
|
|---|
| 73 | * - disables UHCI legacy support (PCI config space)
|
|---|
| 74 | * - attempts to enable interrupts
|
|---|
| 75 | * - registers interrupt handler
|
|---|
| 76 | */
|
|---|
| 77 | int device_setup_uhci(ddf_dev_t *device)
|
|---|
| 78 | {
|
|---|
| 79 | assert(device);
|
|---|
| 80 |
|
|---|
| 81 | hw_res_list_parsed_t hw_res;
|
|---|
| 82 | int ret = hcd_ddf_get_registers(device, &hw_res);
|
|---|
| 83 | if (ret != EOK ||
|
|---|
| 84 | hw_res.irqs.count != 1 || hw_res.io_ranges.count != 1) {
|
|---|
| 85 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 86 | "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
|
|---|
| 87 | str_error(ret));
|
|---|
| 88 | return ret;
|
|---|
| 89 | }
|
|---|
| 90 | addr_range_t regs = hw_res.io_ranges.ranges[0];
|
|---|
| 91 | const int irq = hw_res.irqs.irqs[0];
|
|---|
| 92 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 93 |
|
|---|
| 94 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
|
|---|
| 95 | RNGABSPTR(regs), RNGSZ(regs), irq);
|
|---|
| 96 |
|
|---|
| 97 | ret = hcd_ddf_setup_hc(device, USB_SPEED_FULL,
|
|---|
| 98 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
|---|
| 99 | if (ret != EOK) {
|
|---|
| 100 | usb_log_error("Failed to setup generic HCD.\n");
|
|---|
| 101 | return ret;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | hc_t *hc = malloc(sizeof(hc_t));
|
|---|
| 105 | if (!hc) {
|
|---|
| 106 | usb_log_error("Failed to allocate UHCI HC structure.\n");
|
|---|
| 107 | ret = ENOMEM;
|
|---|
| 108 | goto ddf_hc_clean;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | ret = hc_register_irq_handler(device, ®s, irq, irq_handler);
|
|---|
| 112 | if (ret != EOK) {
|
|---|
| 113 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
|---|
| 114 | str_error(ret));
|
|---|
| 115 | goto hc_free;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | bool interrupts = false;
|
|---|
| 119 | ret = hcd_ddf_enable_interrupts(device);
|
|---|
| 120 | if (ret != EOK) {
|
|---|
| 121 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 122 | " Falling back to polling.\n", str_error(ret));
|
|---|
| 123 | unregister_interrupt_handler(device, irq);
|
|---|
| 124 | } else {
|
|---|
| 125 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 126 | interrupts = true;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | ret = disable_legacy(device);
|
|---|
| 130 | if (ret != EOK) {
|
|---|
| 131 | usb_log_error("Failed to disable legacy USB: %s.\n",
|
|---|
| 132 | str_error(ret));
|
|---|
| 133 | goto irq_unregister;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | ret = hc_init(hc, ®s, interrupts);
|
|---|
| 137 | if (ret != EOK) {
|
|---|
| 138 | usb_log_error("Failed to init uhci_hcd: %s.\n", str_error(ret));
|
|---|
| 139 | goto irq_unregister;
|
|---|
| 140 | // TODO This is unfortunate, we have neither legacy nor real USB
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
|
|---|
| 144 |
|
|---|
| 145 | /*
|
|---|
| 146 | * Creating root hub registers a new USB device so HC
|
|---|
| 147 | * needs to be ready at this time.
|
|---|
| 148 | */
|
|---|
| 149 | ret = hcd_ddf_setup_root_hub(device);
|
|---|
| 150 | if (ret != EOK) {
|
|---|
| 151 | usb_log_error("Failed to setup UHCI root hub: %s.\n",
|
|---|
| 152 | str_error(ret));
|
|---|
| 153 | hc_fini(hc);
|
|---|
| 154 | irq_unregister:
|
|---|
| 155 | unregister_interrupt_handler(device, irq);
|
|---|
| 156 | hc_free:
|
|---|
| 157 | free(hc);
|
|---|
| 158 | ddf_hc_clean:
|
|---|
| 159 | hcd_ddf_clean_hc(device);
|
|---|
| 160 | }
|
|---|
| 161 | return ret;
|
|---|
| 162 | }
|
|---|
| 163 | /**
|
|---|
| 164 | * @}
|
|---|
| 165 | */
|
|---|