[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 "iface.h"
|
---|
| 45 | #include "pci.h"
|
---|
[dc5f2fb] | 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 | assert(dev->driver_data);
|
---|
| 61 | return dev->driver_data;
|
---|
| 62 | }
|
---|
[53f1c87] | 63 |
|
---|
| 64 | /** IRQ handling callback, identifies device
|
---|
| 65 | *
|
---|
| 66 | * @param[in] dev DDF instance of the device to use.
|
---|
| 67 | * @param[in] iid (Unused).
|
---|
| 68 | * @param[in] call Pointer to the call that represents interrupt.
|
---|
| 69 | */
|
---|
| 70 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
| 71 | {
|
---|
[dc5f2fb] | 72 | hc_t *hc = &dev_to_ohci(dev)->hc;
|
---|
[53f1c87] | 73 | assert(hc);
|
---|
[dc5f2fb] | 74 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
[53f1c87] | 75 | hc_interrupt(hc, status);
|
---|
| 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);
|
---|
[dc5f2fb] | 88 | usb_device_keeper_t *manager = &dev_to_ohci(fun->dev)->hc.manager;
|
---|
[53f1c87] | 89 |
|
---|
| 90 | usb_address_t addr = usb_device_keeper_find(manager, handle);
|
---|
| 91 | if (addr < 0) {
|
---|
| 92 | return addr;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | if (address != NULL) {
|
---|
| 96 | *address = addr;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 | /*----------------------------------------------------------------------------*/
|
---|
| 102 | /** Gets handle of the respective hc (this device, hc function).
|
---|
| 103 | *
|
---|
| 104 | * @param[in] root_hub_fun Root hub function seeking hc handle.
|
---|
| 105 | * @param[out] handle Place to write the handle.
|
---|
| 106 | * @return Error code.
|
---|
| 107 | */
|
---|
| 108 | static int usb_iface_get_hc_handle(
|
---|
| 109 | ddf_fun_t *fun, devman_handle_t *handle)
|
---|
| 110 | {
|
---|
[dc5f2fb] | 111 | assert(fun);
|
---|
| 112 | ddf_fun_t *hc_fun = dev_to_ohci(fun->dev)->hc_fun;
|
---|
| 113 | assert(hc_fun);
|
---|
[53f1c87] | 114 |
|
---|
[d2bff2f] | 115 | if (handle != NULL)
|
---|
| 116 | *handle = hc_fun->handle;
|
---|
[53f1c87] | 117 | return EOK;
|
---|
| 118 | }
|
---|
| 119 | /*----------------------------------------------------------------------------*/
|
---|
[dc5f2fb] | 120 | /** Root hub USB interface */
|
---|
[53f1c87] | 121 | static usb_iface_t usb_iface = {
|
---|
| 122 | .get_hc_handle = usb_iface_get_hc_handle,
|
---|
| 123 | .get_address = usb_iface_get_address
|
---|
| 124 | };
|
---|
| 125 | /*----------------------------------------------------------------------------*/
|
---|
[dc5f2fb] | 126 | /** Standard USB HC options (HC interface) */
|
---|
[53f1c87] | 127 | static ddf_dev_ops_t hc_ops = {
|
---|
| 128 | .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */
|
---|
| 129 | };
|
---|
| 130 | /*----------------------------------------------------------------------------*/
|
---|
[dc5f2fb] | 131 | /** Standard USB RH options (RH interface) */
|
---|
[53f1c87] | 132 | static ddf_dev_ops_t rh_ops = {
|
---|
| 133 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 134 | };
|
---|
| 135 | /*----------------------------------------------------------------------------*/
|
---|
| 136 | /** Initialize hc and rh ddf structures and their respective drivers.
|
---|
| 137 | *
|
---|
| 138 | * @param[in] device DDF instance of the device to use.
|
---|
[dc5f2fb] | 139 | * @param[in] instance OHCI structure to use.
|
---|
[53f1c87] | 140 | *
|
---|
| 141 | * This function does all the preparatory work for hc and rh drivers:
|
---|
| 142 | * - gets device hw resources
|
---|
[9b640c42] | 143 | * - disables OHCI legacy support
|
---|
[53f1c87] | 144 | * - asks for interrupt
|
---|
| 145 | * - registers interrupt handler
|
---|
| 146 | */
|
---|
[dc5f2fb] | 147 | int device_setup_ohci(ddf_dev_t *device)
|
---|
[53f1c87] | 148 | {
|
---|
[dc5f2fb] | 149 | ohci_t *instance = malloc(sizeof(ohci_t));
|
---|
| 150 | if (instance == NULL) {
|
---|
| 151 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
[62265ce] | 152 | return ENOMEM;
|
---|
| 153 | }
|
---|
[dc5f2fb] | 154 |
|
---|
| 155 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
| 156 | if (ret != EOK) { \
|
---|
| 157 | if (instance->hc_fun) { \
|
---|
| 158 | instance->hc_fun->ops = NULL; \
|
---|
| 159 | instance->hc_fun->driver_data = NULL; \
|
---|
| 160 | ddf_fun_destroy(instance->hc_fun); \
|
---|
| 161 | } \
|
---|
| 162 | if (instance->rh_fun) { \
|
---|
| 163 | instance->rh_fun->ops = NULL; \
|
---|
| 164 | instance->rh_fun->driver_data = NULL; \
|
---|
| 165 | ddf_fun_destroy(instance->rh_fun); \
|
---|
| 166 | } \
|
---|
| 167 | free(instance); \
|
---|
| 168 | usb_log_error(message); \
|
---|
| 169 | return ret; \
|
---|
| 170 | } else (void)0
|
---|
| 171 |
|
---|
[d2bff2f] | 172 | instance->rh_fun = NULL;
|
---|
[dc5f2fb] | 173 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "ohci-hc");
|
---|
| 174 | int ret = instance->hc_fun ? EOK : ENOMEM;
|
---|
| 175 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create OHCI HC function.\n");
|
---|
[62265ce] | 176 | instance->hc_fun->ops = &hc_ops;
|
---|
| 177 | instance->hc_fun->driver_data = &instance->hc;
|
---|
| 178 |
|
---|
| 179 | instance->rh_fun = ddf_fun_create(device, fun_inner, "ohci-rh");
|
---|
[dc5f2fb] | 180 | ret = instance->rh_fun ? EOK : ENOMEM;
|
---|
| 181 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create OHCI RH function.\n");
|
---|
[62265ce] | 182 | instance->rh_fun->ops = &rh_ops;
|
---|
| 183 |
|
---|
[dc5f2fb] | 184 | uintptr_t reg_base = 0;
|
---|
| 185 | size_t reg_size = 0;
|
---|
[53f1c87] | 186 | int irq = 0;
|
---|
| 187 |
|
---|
[dc5f2fb] | 188 | ret = pci_get_my_registers(device, ®_base, ®_size, &irq);
|
---|
| 189 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[4125b7d] | 190 | "Failed to get memory addresses for %" PRIun ": %s.\n",
|
---|
| 191 | device->handle, str_error(ret));
|
---|
| 192 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
---|
[dc5f2fb] | 193 | (void *) reg_base, reg_size, irq);
|
---|
[53f1c87] | 194 |
|
---|
| 195 | bool interrupts = false;
|
---|
| 196 | #ifdef CONFIG_USBHC_NO_INTERRUPTS
|
---|
[62265ce] | 197 | usb_log_warning("Interrupts disabled in OS config, "
|
---|
[53f1c87] | 198 | "falling back to polling.\n");
|
---|
| 199 | #else
|
---|
| 200 | ret = pci_enable_interrupts(device);
|
---|
| 201 | if (ret != EOK) {
|
---|
| 202 | usb_log_warning("Failed to enable interrupts: %s.\n",
|
---|
| 203 | str_error(ret));
|
---|
[62265ce] | 204 | usb_log_info("HW interrupts not available, "
|
---|
[53f1c87] | 205 | "falling back to polling.\n");
|
---|
| 206 | } else {
|
---|
| 207 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
| 208 | interrupts = true;
|
---|
| 209 | }
|
---|
| 210 | #endif
|
---|
| 211 |
|
---|
[dc5f2fb] | 212 | ret = hc_init(&instance->hc, reg_base, reg_size, interrupts);
|
---|
| 213 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed(%d) to init ohci-hcd.\n", ret);
|
---|
[53f1c87] | 214 |
|
---|
| 215 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
| 216 | if (ret != EOK) { \
|
---|
| 217 | hc_fini(&instance->hc); \
|
---|
[dc5f2fb] | 218 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
| 219 | } else (void)0
|
---|
[53f1c87] | 220 |
|
---|
| 221 | /* It does no harm if we register this on polling */
|
---|
[561112f] | 222 | ret = register_interrupt_handler(device, irq, irq_handler,
|
---|
| 223 | &instance->hc.interrupt_code);
|
---|
[53f1c87] | 224 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 225 | "Failed(%d) to register interrupt handler.\n", ret);
|
---|
| 226 |
|
---|
[62265ce] | 227 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
[53f1c87] | 228 | CHECK_RET_FINI_RETURN(ret,
|
---|
[62265ce] | 229 | "Failed(%d) to bind OHCI device function: %s.\n",
|
---|
| 230 | ret, str_error(ret));
|
---|
[53f1c87] | 231 |
|
---|
[76ef94e] | 232 | ret = ddf_fun_add_to_class(instance->hc_fun, USB_HC_DDF_CLASS_NAME);
|
---|
| 233 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 234 | "Failed to add OHCI to HC class: %s.\n", str_error(ret));
|
---|
[53f1c87] | 235 |
|
---|
[5d07f54] | 236 | device->driver_data = instance;
|
---|
[53f1c87] | 237 |
|
---|
[f1d16663] | 238 | hc_start_hw(&instance->hc);
|
---|
[dc5f2fb] | 239 | hc_register_hub(&instance->hc, instance->rh_fun);
|
---|
[53f1c87] | 240 | return EOK;
|
---|
[62265ce] | 241 |
|
---|
| 242 | #undef CHECK_RET_DEST_FUN_RETURN
|
---|
[53f1c87] | 243 | #undef CHECK_RET_FINI_RETURN
|
---|
| 244 | }
|
---|
| 245 | /**
|
---|
| 246 | * @}
|
---|
| 247 | */
|
---|