| 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 | #include <errno.h> | 
|---|
| 36 | #include <str_error.h> | 
|---|
| 37 | #include <ddf/interrupt.h> | 
|---|
| 38 | #include <usb_iface.h> | 
|---|
| 39 | #include <usb/usb.h> | 
|---|
| 40 | #include <usb/ddfiface.h> | 
|---|
| 41 | #include <usb/debug.h> | 
|---|
| 42 |  | 
|---|
| 43 | #include "ohci.h" | 
|---|
| 44 | #include "iface.h" | 
|---|
| 45 | #include "pci.h" | 
|---|
| 46 | #include "hc.h" | 
|---|
| 47 | #include "root_hub.h" | 
|---|
| 48 |  | 
|---|
| 49 | typedef struct ohci { | 
|---|
| 50 | ddf_fun_t *hc_fun; | 
|---|
| 51 | ddf_fun_t *rh_fun; | 
|---|
| 52 |  | 
|---|
| 53 | hc_t hc; | 
|---|
| 54 | rh_t rh; | 
|---|
| 55 | } ohci_t; | 
|---|
| 56 |  | 
|---|
| 57 | static inline ohci_t * dev_to_ohci(ddf_dev_t *dev) | 
|---|
| 58 | { | 
|---|
| 59 | assert(dev); | 
|---|
| 60 | return dev->driver_data; | 
|---|
| 61 | } | 
|---|
| 62 | /** IRQ handling callback, identifies device | 
|---|
| 63 | * | 
|---|
| 64 | * @param[in] dev DDF instance of the device to use. | 
|---|
| 65 | * @param[in] iid (Unused). | 
|---|
| 66 | * @param[in] call Pointer to the call that represents interrupt. | 
|---|
| 67 | */ | 
|---|
| 68 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call) | 
|---|
| 69 | { | 
|---|
| 70 | assert(dev); | 
|---|
| 71 |  | 
|---|
| 72 | ohci_t *ohci = dev_to_ohci(dev); | 
|---|
| 73 | if (!ohci) { | 
|---|
| 74 | usb_log_warning("Interrupt on device that is not ready.\n"); | 
|---|
| 75 | return; | 
|---|
| 76 | } | 
|---|
| 77 | const uint16_t status = IPC_GET_ARG1(*call); | 
|---|
| 78 | hc_interrupt(&ohci->hc, status); | 
|---|
| 79 | } | 
|---|
| 80 | /*----------------------------------------------------------------------------*/ | 
|---|
| 81 | /** Get address of the device identified by handle. | 
|---|
| 82 | * | 
|---|
| 83 | * @param[in] dev DDF instance of the device to use. | 
|---|
| 84 | * @param[in] iid (Unused). | 
|---|
| 85 | * @param[in] call Pointer to the call that represents interrupt. | 
|---|
| 86 | */ | 
|---|
| 87 | static int usb_iface_get_address( | 
|---|
| 88 | ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address) | 
|---|
| 89 | { | 
|---|
| 90 | assert(fun); | 
|---|
| 91 | usb_device_keeper_t *manager = &dev_to_ohci(fun->dev)->hc.manager; | 
|---|
| 92 |  | 
|---|
| 93 | usb_address_t addr = usb_device_keeper_find(manager, handle); | 
|---|
| 94 | if (addr < 0) { | 
|---|
| 95 | return addr; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | if (address != NULL) { | 
|---|
| 99 | *address = addr; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | return EOK; | 
|---|
| 103 | } | 
|---|
| 104 | /*----------------------------------------------------------------------------*/ | 
|---|
| 105 | /** Gets handle of the respective hc (this device, hc function). | 
|---|
| 106 | * | 
|---|
| 107 | * @param[in] root_hub_fun Root hub function seeking hc handle. | 
|---|
| 108 | * @param[out] handle Place to write the handle. | 
|---|
| 109 | * @return Error code. | 
|---|
| 110 | */ | 
|---|
| 111 | static int usb_iface_get_hc_handle( | 
|---|
| 112 | ddf_fun_t *fun, devman_handle_t *handle) | 
|---|
| 113 | { | 
|---|
| 114 | assert(fun); | 
|---|
| 115 | ddf_fun_t *hc_fun = dev_to_ohci(fun->dev)->hc_fun; | 
|---|
| 116 | assert(hc_fun); | 
|---|
| 117 |  | 
|---|
| 118 | if (handle != NULL) | 
|---|
| 119 | *handle = hc_fun->handle; | 
|---|
| 120 | return EOK; | 
|---|
| 121 | } | 
|---|
| 122 | /*----------------------------------------------------------------------------*/ | 
|---|
| 123 | /** Root hub USB interface */ | 
|---|
| 124 | static usb_iface_t usb_iface = { | 
|---|
| 125 | .get_hc_handle = usb_iface_get_hc_handle, | 
|---|
| 126 | .get_address = usb_iface_get_address | 
|---|
| 127 | }; | 
|---|
| 128 | /*----------------------------------------------------------------------------*/ | 
|---|
| 129 | /** Standard USB HC options (HC interface) */ | 
|---|
| 130 | static ddf_dev_ops_t hc_ops = { | 
|---|
| 131 | .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */ | 
|---|
| 132 | }; | 
|---|
| 133 | /*----------------------------------------------------------------------------*/ | 
|---|
| 134 | /** Standard USB RH options (RH interface) */ | 
|---|
| 135 | static ddf_dev_ops_t rh_ops = { | 
|---|
| 136 | .interfaces[USB_DEV_IFACE] = &usb_iface, | 
|---|
| 137 | }; | 
|---|
| 138 | /*----------------------------------------------------------------------------*/ | 
|---|
| 139 | /** Initialize hc and rh ddf structures and their respective drivers. | 
|---|
| 140 | * | 
|---|
| 141 | * @param[in] device DDF instance of the device to use. | 
|---|
| 142 | * @param[in] instance OHCI structure to use. | 
|---|
| 143 | * | 
|---|
| 144 | * This function does all the preparatory work for hc and rh drivers: | 
|---|
| 145 | *  - gets device hw resources | 
|---|
| 146 | *  - disables OHCI legacy support | 
|---|
| 147 | *  - asks for interrupt | 
|---|
| 148 | *  - registers interrupt handler | 
|---|
| 149 | */ | 
|---|
| 150 | int device_setup_ohci(ddf_dev_t *device) | 
|---|
| 151 | { | 
|---|
| 152 | ohci_t *instance = malloc(sizeof(ohci_t)); | 
|---|
| 153 | if (instance == NULL) { | 
|---|
| 154 | usb_log_error("Failed to allocate OHCI driver.\n"); | 
|---|
| 155 | return ENOMEM; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \ | 
|---|
| 159 | if (ret != EOK) { \ | 
|---|
| 160 | if (instance->hc_fun) { \ | 
|---|
| 161 | instance->hc_fun->ops = NULL; \ | 
|---|
| 162 | instance->hc_fun->driver_data = NULL; \ | 
|---|
| 163 | ddf_fun_destroy(instance->hc_fun); \ | 
|---|
| 164 | } \ | 
|---|
| 165 | if (instance->rh_fun) { \ | 
|---|
| 166 | instance->rh_fun->ops = NULL; \ | 
|---|
| 167 | instance->rh_fun->driver_data = NULL; \ | 
|---|
| 168 | ddf_fun_destroy(instance->rh_fun); \ | 
|---|
| 169 | } \ | 
|---|
| 170 | free(instance); \ | 
|---|
| 171 | device->driver_data = NULL; \ | 
|---|
| 172 | usb_log_error(message); \ | 
|---|
| 173 | return ret; \ | 
|---|
| 174 | } else (void)0 | 
|---|
| 175 |  | 
|---|
| 176 | instance->rh_fun = NULL; | 
|---|
| 177 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "ohci_hc"); | 
|---|
| 178 | int ret = instance->hc_fun ? EOK : ENOMEM; | 
|---|
| 179 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 180 | "Failed to create OHCI HC function: %s.\n", str_error(ret)); | 
|---|
| 181 | instance->hc_fun->ops = &hc_ops; | 
|---|
| 182 | instance->hc_fun->driver_data = &instance->hc; | 
|---|
| 183 |  | 
|---|
| 184 | instance->rh_fun = ddf_fun_create(device, fun_inner, "ohci_rh"); | 
|---|
| 185 | ret = instance->rh_fun ? EOK : ENOMEM; | 
|---|
| 186 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 187 | "Failed to create OHCI RH function: %s.\n", str_error(ret)); | 
|---|
| 188 | instance->rh_fun->ops = &rh_ops; | 
|---|
| 189 |  | 
|---|
| 190 | uintptr_t reg_base = 0; | 
|---|
| 191 | size_t reg_size = 0; | 
|---|
| 192 | int irq = 0; | 
|---|
| 193 |  | 
|---|
| 194 | ret = pci_get_my_registers(device, ®_base, ®_size, &irq); | 
|---|
| 195 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 196 | "Failed to get memory addresses for %" PRIun ": %s.\n", | 
|---|
| 197 | device->handle, str_error(ret)); | 
|---|
| 198 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n", | 
|---|
| 199 | (void *) reg_base, reg_size, irq); | 
|---|
| 200 |  | 
|---|
| 201 | const size_t cmd_count = hc_irq_cmd_count(); | 
|---|
| 202 | irq_cmd_t irq_cmds[cmd_count]; | 
|---|
| 203 | ret = | 
|---|
| 204 | hc_get_irq_commands(irq_cmds, sizeof(irq_cmds), reg_base, reg_size); | 
|---|
| 205 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 206 | "Failed to generate IRQ commands: %s.\n", str_error(ret)); | 
|---|
| 207 |  | 
|---|
| 208 | irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = irq_cmds }; | 
|---|
| 209 |  | 
|---|
| 210 | /* Register handler to avoid interrupt lockup */ | 
|---|
| 211 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code); | 
|---|
| 212 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 213 | "Failed to register interrupt handler: %s.\n", str_error(ret)); | 
|---|
| 214 |  | 
|---|
| 215 | /* Try to enable interrupts */ | 
|---|
| 216 | bool interrupts = false; | 
|---|
| 217 | ret = pci_enable_interrupts(device); | 
|---|
| 218 | if (ret != EOK) { | 
|---|
| 219 | usb_log_warning("Failed to enable interrupts: %s." | 
|---|
| 220 | " Falling back to polling\n", str_error(ret)); | 
|---|
| 221 | /* We don't need that handler */ | 
|---|
| 222 | unregister_interrupt_handler(device, irq); | 
|---|
| 223 | } else { | 
|---|
| 224 | usb_log_debug("Hw interrupts enabled.\n"); | 
|---|
| 225 | interrupts = true; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | ret = hc_init(&instance->hc, reg_base, reg_size, interrupts); | 
|---|
| 229 | CHECK_RET_DEST_FREE_RETURN(ret, | 
|---|
| 230 | "Failed to init ohci_hcd: %s.\n", str_error(ret)); | 
|---|
| 231 |  | 
|---|
| 232 | device->driver_data = instance; | 
|---|
| 233 |  | 
|---|
| 234 | #define CHECK_RET_FINI_RETURN(ret, message...) \ | 
|---|
| 235 | if (ret != EOK) { \ | 
|---|
| 236 | unregister_interrupt_handler(device, irq); \ | 
|---|
| 237 | hc_fini(&instance->hc); \ | 
|---|
| 238 | CHECK_RET_DEST_FREE_RETURN(ret, message); \ | 
|---|
| 239 | } else (void)0 | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | ret = ddf_fun_bind(instance->hc_fun); | 
|---|
| 243 | CHECK_RET_FINI_RETURN(ret, | 
|---|
| 244 | "Failed to bind OHCI device function: %s.\n", str_error(ret)); | 
|---|
| 245 |  | 
|---|
| 246 | ret = ddf_fun_add_to_class(instance->hc_fun, USB_HC_DDF_CLASS_NAME); | 
|---|
| 247 | CHECK_RET_FINI_RETURN(ret, | 
|---|
| 248 | "Failed to add OHCI to HC class: %s.\n", str_error(ret)); | 
|---|
| 249 |  | 
|---|
| 250 | hc_register_hub(&instance->hc, instance->rh_fun); | 
|---|
| 251 | return EOK; | 
|---|
| 252 |  | 
|---|
| 253 | #undef CHECK_RET_DEST_FUN_RETURN | 
|---|
| 254 | #undef CHECK_RET_FINI_RETURN | 
|---|
| 255 | } | 
|---|
| 256 | /** | 
|---|
| 257 | * @} | 
|---|
| 258 | */ | 
|---|