| [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 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbuhci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver
|
|---|
| 34 | */
|
|---|
| [56fd7cf] | 35 |
|
|---|
| [9351353] | 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <ddf/interrupt.h>
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| [a720ff6] | 40 | #include <usb/host/hcd.h>
|
|---|
| [53332b5b] | 41 | #include <usb/host/ddf_helpers.h>
|
|---|
| [9351353] | 42 |
|
|---|
| 43 | #include "uhci.h"
|
|---|
| 44 |
|
|---|
| [795448f] | 45 | #include "res.h"
|
|---|
| [d2bff2f] | 46 | #include "hc.h"
|
|---|
| 47 |
|
|---|
| [76fbd9a] | 48 |
|
|---|
| [9d2d444] | 49 | /** IRQ handling callback, forward status from call to diver structure.
|
|---|
| [9351353] | 50 | *
|
|---|
| 51 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 52 | * @param[in] iid (Unused).
|
|---|
| [9d2d444] | 53 | * @param[in] call Pointer to the call from kernel.
|
|---|
| [9351353] | 54 | */
|
|---|
| 55 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 56 | {
|
|---|
| 57 | assert(dev);
|
|---|
| [a720ff6] | 58 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| [9348862] | 59 | if (!hcd || !hcd->driver.data) {
|
|---|
| [dfe4955] | 60 | usb_log_error("Interrupt on not yet initialized device.\n");
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| [2df648c2] | 63 | const uint16_t status = IPC_GET_ARG1(*call);
|
|---|
| [9348862] | 64 | hc_interrupt(hcd->driver.data, status);
|
|---|
| [9351353] | 65 | }
|
|---|
| [76fbd9a] | 66 |
|
|---|
| [9d2d444] | 67 | /** Initialize hc and rh DDF structures and their respective drivers.
|
|---|
| [17ceb72] | 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:
|
|---|
| [9d2d444] | 72 | * - gets device's hw resources
|
|---|
| 73 | * - disables UHCI legacy support (PCI config space)
|
|---|
| [23f40280] | 74 | * - attempts to enable interrupts
|
|---|
| [17ceb72] | 75 | * - registers interrupt handler
|
|---|
| 76 | */
|
|---|
| [d2bff2f] | 77 | int device_setup_uhci(ddf_dev_t *device)
|
|---|
| [9351353] | 78 | {
|
|---|
| [8d40181] | 79 | assert(device);
|
|---|
| [1664a26] | 80 |
|
|---|
| [8d40181] | 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));
|
|---|
| [3f03199] | 88 | return ret;
|
|---|
| [c53007f] | 89 | }
|
|---|
| [8d40181] | 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 |
|
|---|
| [7de1988c] | 94 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
|
|---|
| 95 | RNGABSPTR(regs), RNGSZ(regs), irq);
|
|---|
| [9351353] | 96 |
|
|---|
| [3f03199] | 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;
|
|---|
| [c53007f] | 102 | }
|
|---|
| 103 |
|
|---|
| [3f03199] | 104 | hc_t *hc = malloc(sizeof(hc_t));
|
|---|
| 105 | if (!hc) {
|
|---|
| 106 | usb_log_error("Failed to allocate UHCI HC structure.\n");
|
|---|
| [ea69749f] | 107 | ret = ENOMEM;
|
|---|
| 108 | goto ddf_hc_clean;
|
|---|
| [c53007f] | 109 | }
|
|---|
| 110 |
|
|---|
| [3f03199] | 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));
|
|---|
| [ea69749f] | 115 | goto hc_free;
|
|---|
| [3f03199] | 116 | }
|
|---|
| [9351353] | 117 |
|
|---|
| [ff34e5a] | 118 | bool interrupts = false;
|
|---|
| [57c8fc9] | 119 | ret = hcd_ddf_enable_interrupts(device);
|
|---|
| [9351353] | 120 | if (ret != EOK) {
|
|---|
| [dfe4955] | 121 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 122 | " Falling back to polling.\n", str_error(ret));
|
|---|
| [ea69749f] | 123 | unregister_interrupt_handler(device, irq);
|
|---|
| [ff34e5a] | 124 | } else {
|
|---|
| 125 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 126 | interrupts = true;
|
|---|
| [9351353] | 127 | }
|
|---|
| 128 |
|
|---|
| [3f03199] | 129 | ret = disable_legacy(device);
|
|---|
| 130 | if (ret != EOK) {
|
|---|
| 131 | usb_log_error("Failed to disable legacy USB: %s.\n",
|
|---|
| 132 | str_error(ret));
|
|---|
| [ea69749f] | 133 | goto irq_unregister;
|
|---|
| [c53007f] | 134 | }
|
|---|
| [a720ff6] | 135 |
|
|---|
| [3f03199] | 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));
|
|---|
| [ea69749f] | 139 | goto irq_unregister;
|
|---|
| 140 | // TODO This is unfortunate, we have neither legacy nor real USB
|
|---|
| [c53007f] | 141 | }
|
|---|
| [9d2d444] | 142 |
|
|---|
| [a720ff6] | 143 | hcd_set_implementation(dev_to_hcd(device), hc, hc_schedule, NULL, NULL);
|
|---|
| 144 |
|
|---|
| [e0d8b740] | 145 | /*
|
|---|
| [cb8ede1] | 146 | * Creating root hub registers a new USB device so HC
|
|---|
| 147 | * needs to be ready at this time.
|
|---|
| [e0d8b740] | 148 | */
|
|---|
| [cb8ede1] | 149 | ret = hcd_ddf_setup_root_hub(device);
|
|---|
| [e0d8b740] | 150 | if (ret != EOK) {
|
|---|
| [c53007f] | 151 | usb_log_error("Failed to setup UHCI root hub: %s.\n",
|
|---|
| [e0d8b740] | 152 | str_error(ret));
|
|---|
| [ea69749f] | 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);
|
|---|
| [e0d8b740] | 160 | }
|
|---|
| [ea69749f] | 161 | return ret;
|
|---|
| [9351353] | 162 | }
|
|---|
| 163 | /**
|
|---|
| 164 | * @}
|
|---|
| 165 | */
|
|---|