| 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 drvusbohci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief OHCI driver
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <ddf/interrupt.h>
|
|---|
| 39 | #include <usb/usb.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <usb/host/ddf_helpers.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "ohci.h"
|
|---|
| 45 | #include "hc.h"
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | /** IRQ handling callback, identifies device
|
|---|
| 49 | *
|
|---|
| 50 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 51 | * @param[in] iid (Unused).
|
|---|
| 52 | * @param[in] call Pointer to the call that represents interrupt.
|
|---|
| 53 | */
|
|---|
| 54 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 55 | {
|
|---|
| 56 | assert(dev);
|
|---|
| 57 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| 58 | if (!hcd || !hcd->driver.data) {
|
|---|
| 59 | usb_log_warning("Interrupt on device that is not ready.\n");
|
|---|
| 60 | return;
|
|---|
| 61 | }
|
|---|
| 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 | * @param[in] instance OHCI structure to use.
|
|---|
| 71 | *
|
|---|
| 72 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 73 | * - gets device hw resources
|
|---|
| 74 | * - disables OHCI legacy support
|
|---|
| 75 | * - asks for interrupt
|
|---|
| 76 | * - registers interrupt handler
|
|---|
| 77 | */
|
|---|
| 78 | int device_setup_ohci(ddf_dev_t *device)
|
|---|
| 79 | {
|
|---|
| 80 | hw_res_list_parsed_t hw_res;
|
|---|
| 81 | int ret = hcd_ddf_get_registers(device, &hw_res);
|
|---|
| 82 | if (ret != EOK ||
|
|---|
| 83 | hw_res.irqs.count != 1 || hw_res.mem_ranges.count != 1) {
|
|---|
| 84 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 85 | "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
|
|---|
| 86 | str_error(ret));
|
|---|
| 87 | return ret;
|
|---|
| 88 | }
|
|---|
| 89 | addr_range_t regs = hw_res.mem_ranges.ranges[0];
|
|---|
| 90 | const int irq = hw_res.irqs.irqs[0];
|
|---|
| 91 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 92 |
|
|---|
| 93 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
|---|
| 94 | RNGABSPTR(regs), RNGSZ(regs), irq);
|
|---|
| 95 |
|
|---|
| 96 | /* Initialize generic HCD driver */
|
|---|
| 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 structures: %s.",
|
|---|
| 101 | str_error(ret));
|
|---|
| 102 | return ret;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | hc_t *hc = malloc(sizeof(hc_t));
|
|---|
| 106 | if (!hc) {
|
|---|
| 107 | usb_log_error("Failed to allocate driver structure.\n");
|
|---|
| 108 | ret = ENOMEM;
|
|---|
| 109 | goto ddf_hc_clean;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /* Try to enable interrupts */
|
|---|
| 113 | bool interrupts = false;
|
|---|
| 114 | ret = hcd_ddf_setup_interrupts(device, ®s, irq, irq_handler,
|
|---|
| 115 | hc_gen_irq_code);
|
|---|
| 116 | if (ret != EOK) {
|
|---|
| 117 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 118 | " Falling back to polling\n", str_error(ret));
|
|---|
| 119 | /* We don't need that handler */
|
|---|
| 120 | unregister_interrupt_handler(device, irq);
|
|---|
| 121 | } else {
|
|---|
| 122 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 123 | interrupts = true;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /* Initialize OHCI HC */
|
|---|
| 127 | ret = hc_init(hc, ®s, interrupts);
|
|---|
| 128 | if (ret != EOK) {
|
|---|
| 129 | usb_log_error("Failed to init hc: %s.\n", str_error(ret));
|
|---|
| 130 | goto unregister_irq;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /* Connect OHCI to generic HCD */
|
|---|
| 134 | hcd_set_implementation(dev_to_hcd(device), hc,
|
|---|
| 135 | hc_schedule, ohci_endpoint_init, ohci_endpoint_fini);
|
|---|
| 136 |
|
|---|
| 137 | /* HC should be running OK. We can add root hub */
|
|---|
| 138 | ret = hcd_ddf_setup_root_hub(device);
|
|---|
| 139 | if (ret != EOK) {
|
|---|
| 140 | usb_log_error("Failed to register OHCI root hub: %s.\n",
|
|---|
| 141 | str_error(ret));
|
|---|
| 142 | hc_fini(hc);
|
|---|
| 143 | unregister_irq:
|
|---|
| 144 | unregister_interrupt_handler(device, irq);
|
|---|
| 145 | free(hc);
|
|---|
| 146 | ddf_hc_clean:
|
|---|
| 147 | hcd_ddf_clean_hc(device);
|
|---|
| 148 | }
|
|---|
| 149 | return ret;
|
|---|
| 150 | }
|
|---|
| 151 | /**
|
|---|
| 152 | * @}
|
|---|
| 153 | */
|
|---|