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