| [53f1c87] | 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
|
|---|
| [9b640c42] | 33 | * @brief OHCI driver
|
|---|
| [53f1c87] | 34 | */
|
|---|
| [56fd7cf] | 35 |
|
|---|
| [0d4b110] | 36 | #include <assert.h>
|
|---|
| [53f1c87] | 37 | #include <errno.h>
|
|---|
| 38 | #include <ddf/interrupt.h>
|
|---|
| [0d4b110] | 39 | #include <device/hw_res_parsed.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <sys/types.h>
|
|---|
| 43 |
|
|---|
| [53f1c87] | 44 | #include <usb/usb.h>
|
|---|
| 45 | #include <usb/debug.h>
|
|---|
| 46 |
|
|---|
| [53332b5b] | 47 | #include <usb/host/ddf_helpers.h>
|
|---|
| [0d4b110] | 48 | #include <usb/host/usb_bus.h>
|
|---|
| [53332b5b] | 49 |
|
|---|
| [53f1c87] | 50 | #include "ohci.h"
|
|---|
| [0d4b110] | 51 | #include "ohci_endpoint.h"
|
|---|
| [dc5f2fb] | 52 | #include "hc.h"
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| [53f1c87] | 55 | /** IRQ handling callback, identifies device
|
|---|
| 56 | *
|
|---|
| 57 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 58 | * @param[in] iid (Unused).
|
|---|
| 59 | * @param[in] call Pointer to the call that represents interrupt.
|
|---|
| 60 | */
|
|---|
| 61 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 62 | {
|
|---|
| [9d4c1b6] | 63 | assert(dev);
|
|---|
| [5994cc3] | 64 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| [9348862] | 65 | if (!hcd || !hcd->driver.data) {
|
|---|
| [9d4c1b6] | 66 | usb_log_warning("Interrupt on device that is not ready.\n");
|
|---|
| [1ecc5de] | 67 | return;
|
|---|
| 68 | }
|
|---|
| [2f87aa6] | 69 |
|
|---|
| [dc5f2fb] | 70 | const uint16_t status = IPC_GET_ARG1(*call);
|
|---|
| [9348862] | 71 | hc_interrupt(hcd->driver.data, status);
|
|---|
| [53f1c87] | 72 | }
|
|---|
| [76fbd9a] | 73 |
|
|---|
| [53f1c87] | 74 | /** Initialize hc and rh ddf structures and their respective drivers.
|
|---|
| 75 | *
|
|---|
| 76 | * @param[in] device DDF instance of the device to use.
|
|---|
| [dc5f2fb] | 77 | * @param[in] instance OHCI structure to use.
|
|---|
| [53f1c87] | 78 | *
|
|---|
| 79 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 80 | * - gets device hw resources
|
|---|
| [9b640c42] | 81 | * - disables OHCI legacy support
|
|---|
| [53f1c87] | 82 | * - asks for interrupt
|
|---|
| 83 | * - registers interrupt handler
|
|---|
| 84 | */
|
|---|
| [ddab093] | 85 | int device_setup_ohci(ddf_dev_t *device)
|
|---|
| 86 | {
|
|---|
| [8d40181] | 87 | hw_res_list_parsed_t hw_res;
|
|---|
| 88 | int ret = hcd_ddf_get_registers(device, &hw_res);
|
|---|
| 89 | if (ret != EOK ||
|
|---|
| 90 | hw_res.irqs.count != 1 || hw_res.mem_ranges.count != 1) {
|
|---|
| [507c6f3] | 91 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 92 | "for %" PRIun ": %s.\n", ddf_dev_get_handle(device),
|
|---|
| [3f03199] | 93 | str_error(ret));
|
|---|
| 94 | return ret;
|
|---|
| [507c6f3] | 95 | }
|
|---|
| [8d40181] | 96 | addr_range_t regs = hw_res.mem_ranges.ranges[0];
|
|---|
| 97 | const int irq = hw_res.irqs.irqs[0];
|
|---|
| 98 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 99 |
|
|---|
| [4125b7d] | 100 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
|---|
| [7de1988c] | 101 | RNGABSPTR(regs), RNGSZ(regs), irq);
|
|---|
| [53f1c87] | 102 |
|
|---|
| [3f03199] | 103 | /* Initialize generic HCD driver */
|
|---|
| 104 | ret = hcd_ddf_setup_hc(device, USB_SPEED_FULL,
|
|---|
| 105 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
|---|
| 106 | if (ret != EOK) {
|
|---|
| [bf6b7c6] | 107 | usb_log_error("Failed to setup generic hcd structures: %s.",
|
|---|
| [3f03199] | 108 | str_error(ret));
|
|---|
| 109 | return ret;
|
|---|
| [507c6f3] | 110 | }
|
|---|
| [2e8a01b] | 111 |
|
|---|
| [772a172] | 112 | hc_t *hc = malloc(sizeof(hc_t));
|
|---|
| 113 | if (!hc) {
|
|---|
| 114 | usb_log_error("Failed to allocate driver structure.\n");
|
|---|
| 115 | ret = ENOMEM;
|
|---|
| [bf6b7c6] | 116 | goto ddf_hc_clean;
|
|---|
| [3f03199] | 117 | }
|
|---|
| [ddab093] | 118 |
|
|---|
| [2e8a01b] | 119 | /* Try to enable interrupts */
|
|---|
| [53f1c87] | 120 | bool interrupts = false;
|
|---|
| [772a172] | 121 | ret = hcd_ddf_setup_interrupts(device, ®s, irq, irq_handler,
|
|---|
| 122 | hc_gen_irq_code);
|
|---|
| [53f1c87] | 123 | if (ret != EOK) {
|
|---|
| [2e8a01b] | 124 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| [dfe4955] | 125 | " Falling back to polling\n", str_error(ret));
|
|---|
| [53f1c87] | 126 | } else {
|
|---|
| 127 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 128 | interrupts = true;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| [d09791e6] | 131 | /* Initialize OHCI HC */
|
|---|
| [bf6b7c6] | 132 | ret = hc_init(hc, ®s, interrupts);
|
|---|
| [3f03199] | 133 | if (ret != EOK) {
|
|---|
| 134 | usb_log_error("Failed to init hc: %s.\n", str_error(ret));
|
|---|
| [772a172] | 135 | goto unregister_irq;
|
|---|
| [507c6f3] | 136 | }
|
|---|
| [ddab093] | 137 |
|
|---|
| [d09791e6] | 138 | /* Connect OHCI to generic HCD */
|
|---|
| [bf6b7c6] | 139 | hcd_set_implementation(dev_to_hcd(device), hc,
|
|---|
| [ddab093] | 140 | hc_schedule, ohci_endpoint_init, ohci_endpoint_fini);
|
|---|
| [d09791e6] | 141 |
|
|---|
| 142 | /* HC should be running OK. We can add root hub */
|
|---|
| [cb8ede1] | 143 | ret = hcd_ddf_setup_root_hub(device);
|
|---|
| [3f03199] | 144 | if (ret != EOK) {
|
|---|
| [bf6b7c6] | 145 | usb_log_error("Failed to register OHCI root hub: %s.\n",
|
|---|
| [3f03199] | 146 | str_error(ret));
|
|---|
| [bf6b7c6] | 147 | hc_fini(hc);
|
|---|
| 148 | unregister_irq:
|
|---|
| [507c6f3] | 149 | unregister_interrupt_handler(device, irq);
|
|---|
| [772a172] | 150 | free(hc);
|
|---|
| [bf6b7c6] | 151 | ddf_hc_clean:
|
|---|
| 152 | hcd_ddf_clean_hc(device);
|
|---|
| [3f03199] | 153 | }
|
|---|
| [5ec492b] | 154 | return ret;
|
|---|
| [53f1c87] | 155 | }
|
|---|
| 156 | /**
|
|---|
| 157 | * @}
|
|---|
| 158 | */
|
|---|