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