[9351353] | 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 drvusbuhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief UHCI driver
|
---|
| 34 | */
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <str_error.h>
|
---|
| 37 | #include <ddf/interrupt.h>
|
---|
| 38 | #include <usb_iface.h>
|
---|
| 39 | #include <usb/ddfiface.h>
|
---|
| 40 | #include <usb/debug.h>
|
---|
| 41 |
|
---|
| 42 | #include "uhci.h"
|
---|
| 43 | #include "iface.h"
|
---|
| 44 | #include "pci.h"
|
---|
| 45 |
|
---|
[d2bff2f] | 46 | #include "hc.h"
|
---|
| 47 | #include "root_hub.h"
|
---|
| 48 |
|
---|
| 49 | /** Structure representing both functions of UHCI hc, USB host controller
|
---|
| 50 | * and USB root hub */
|
---|
| 51 | typedef struct uhci {
|
---|
| 52 | /** Pointer to DDF represenation of UHCI host controller */
|
---|
| 53 | ddf_fun_t *hc_fun;
|
---|
| 54 | /** Pointer to DDF represenation of UHCI root hub */
|
---|
| 55 | ddf_fun_t *rh_fun;
|
---|
| 56 |
|
---|
| 57 | /** Internal driver's represenation of UHCI host controller */
|
---|
| 58 | hc_t hc;
|
---|
| 59 | /** Internal driver's represenation of UHCI root hub */
|
---|
| 60 | rh_t rh;
|
---|
| 61 | } uhci_t;
|
---|
| 62 |
|
---|
[2df648c2] | 63 | static inline uhci_t * dev_to_uhci(const ddf_dev_t *dev)
|
---|
[d2bff2f] | 64 | {
|
---|
| 65 | assert(dev);
|
---|
| 66 | return dev->driver_data;
|
---|
| 67 | }
|
---|
| 68 | /*----------------------------------------------------------------------------*/
|
---|
[9d2d444] | 69 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
[9351353] | 70 | *
|
---|
| 71 | * @param[in] dev DDF instance of the device to use.
|
---|
| 72 | * @param[in] iid (Unused).
|
---|
[9d2d444] | 73 | * @param[in] call Pointer to the call from kernel.
|
---|
[9351353] | 74 | */
|
---|
| 75 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
| 76 | {
|
---|
| 77 | assert(dev);
|
---|
[2df648c2] | 78 | uhci_t *uhci = dev_to_uhci(dev);
|
---|
[dfe4955] | 79 | if (!uhci) {
|
---|
| 80 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
[2df648c2] | 83 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
[fc6e607] | 84 | hc_interrupt(&uhci->hc, status);
|
---|
[9351353] | 85 | }
|
---|
| 86 | /*----------------------------------------------------------------------------*/
|
---|
[2df648c2] | 87 | /** Operations supported by the HC driver */
|
---|
| 88 | static ddf_dev_ops_t hc_ops = {
|
---|
| 89 | .interfaces[USBHC_DEV_IFACE] = &hc_iface, /* see iface.h/c */
|
---|
| 90 | };
|
---|
| 91 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 92 | /** Get address of the device identified by handle.
|
---|
| 93 | *
|
---|
[9d2d444] | 94 | * @param[in] fun DDF instance of the function to use.
|
---|
| 95 | * @param[in] handle DDF handle of the driver seeking its USB address.
|
---|
[ea993d18] | 96 | * @param[out] address Found address.
|
---|
[17ceb72] | 97 | */
|
---|
[9351353] | 98 | static int usb_iface_get_address(
|
---|
| 99 | ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
|
---|
| 100 | {
|
---|
| 101 | assert(fun);
|
---|
[d2bff2f] | 102 | usb_device_keeper_t *manager = &dev_to_uhci(fun->dev)->hc.manager;
|
---|
[68b5ed6e] | 103 | usb_address_t addr = usb_device_keeper_find(manager, handle);
|
---|
[d2bff2f] | 104 |
|
---|
[9351353] | 105 | if (addr < 0) {
|
---|
| 106 | return addr;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (address != NULL) {
|
---|
| 110 | *address = addr;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return EOK;
|
---|
| 114 | }
|
---|
| 115 | /*----------------------------------------------------------------------------*/
|
---|
[9d2d444] | 116 | /** Gets handle of the respective hc.
|
---|
[9351353] | 117 | *
|
---|
[9d2d444] | 118 | * @param[in] fun DDF function of uhci device.
|
---|
[ea993d18] | 119 | * @param[out] handle Host cotnroller handle.
|
---|
[9351353] | 120 | * @return Error code.
|
---|
| 121 | */
|
---|
[2df648c2] | 122 | static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
[9351353] | 123 | {
|
---|
[d2bff2f] | 124 | assert(fun);
|
---|
| 125 | ddf_fun_t *hc_fun = dev_to_uhci(fun->dev)->hc_fun;
|
---|
| 126 | assert(hc_fun);
|
---|
[9351353] | 127 |
|
---|
[d2bff2f] | 128 | if (handle != NULL)
|
---|
| 129 | *handle = hc_fun->handle;
|
---|
[9351353] | 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 | /*----------------------------------------------------------------------------*/
|
---|
[9d2d444] | 133 | /** USB interface implementation used by RH */
|
---|
[9351353] | 134 | static usb_iface_t usb_iface = {
|
---|
| 135 | .get_hc_handle = usb_iface_get_hc_handle,
|
---|
| 136 | .get_address = usb_iface_get_address
|
---|
| 137 | };
|
---|
| 138 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 139 | /** Get root hub hw resources (I/O registers).
|
---|
[9351353] | 140 | *
|
---|
| 141 | * @param[in] fun Root hub function.
|
---|
| 142 | * @return Pointer to the resource list used by the root hub.
|
---|
| 143 | */
|
---|
| 144 | static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
|
---|
| 145 | {
|
---|
| 146 | assert(fun);
|
---|
[e247d83] | 147 | rh_t *rh = fun->driver_data;
|
---|
| 148 | assert(rh);
|
---|
| 149 | return &rh->resource_list;
|
---|
[9351353] | 150 | }
|
---|
| 151 | /*----------------------------------------------------------------------------*/
|
---|
[9d2d444] | 152 | /** Interface to provide the root hub driver with hw info */
|
---|
[9351353] | 153 | static hw_res_ops_t hw_res_iface = {
|
---|
| 154 | .get_resource_list = get_resource_list,
|
---|
[d2bff2f] | 155 | .enable_interrupt = NULL,
|
---|
[9351353] | 156 | };
|
---|
| 157 | /*----------------------------------------------------------------------------*/
|
---|
[8d6c1f1] | 158 | /** RH function support for uhci_rhd */
|
---|
[33fbe95] | 159 | static ddf_dev_ops_t rh_ops = {
|
---|
[9351353] | 160 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 161 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
| 162 | };
|
---|
| 163 | /*----------------------------------------------------------------------------*/
|
---|
[9d2d444] | 164 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
[17ceb72] | 165 | *
|
---|
| 166 | * @param[in] device DDF instance of the device to use.
|
---|
| 167 | *
|
---|
| 168 | * This function does all the preparatory work for hc and rh drivers:
|
---|
[9d2d444] | 169 | * - gets device's hw resources
|
---|
| 170 | * - disables UHCI legacy support (PCI config space)
|
---|
[23f40280] | 171 | * - attempts to enable interrupts
|
---|
[17ceb72] | 172 | * - registers interrupt handler
|
---|
| 173 | */
|
---|
[d2bff2f] | 174 | int device_setup_uhci(ddf_dev_t *device)
|
---|
[9351353] | 175 | {
|
---|
[d2bff2f] | 176 | assert(device);
|
---|
| 177 | uhci_t *instance = malloc(sizeof(uhci_t));
|
---|
| 178 | if (instance == NULL) {
|
---|
| 179 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
| 180 | return ENOMEM;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
[9351353] | 184 | if (ret != EOK) { \
|
---|
| 185 | if (instance->hc_fun) \
|
---|
[d2bff2f] | 186 | instance->hc_fun->ops = NULL; \
|
---|
| 187 | instance->hc_fun->driver_data = NULL; \
|
---|
[9351353] | 188 | ddf_fun_destroy(instance->hc_fun); \
|
---|
[d2bff2f] | 189 | if (instance->rh_fun) {\
|
---|
| 190 | instance->rh_fun->ops = NULL; \
|
---|
| 191 | instance->rh_fun->driver_data = NULL; \
|
---|
[9351353] | 192 | ddf_fun_destroy(instance->rh_fun); \
|
---|
[d2bff2f] | 193 | } \
|
---|
| 194 | free(instance); \
|
---|
[dfe4955] | 195 | device->driver_data = NULL; \
|
---|
[d2bff2f] | 196 | usb_log_error(message); \
|
---|
[9351353] | 197 | return ret; \
|
---|
[d2bff2f] | 198 | } else (void)0
|
---|
| 199 |
|
---|
| 200 | instance->rh_fun = NULL;
|
---|
[8d6c1f1] | 201 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
|
---|
[d2bff2f] | 202 | int ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
|
---|
| 203 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI HC function.\n");
|
---|
| 204 | instance->hc_fun->ops = &hc_ops;
|
---|
| 205 | instance->hc_fun->driver_data = &instance->hc;
|
---|
| 206 |
|
---|
[8d6c1f1] | 207 | instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
|
---|
[d2bff2f] | 208 | ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
|
---|
| 209 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI RH function.\n");
|
---|
| 210 | instance->rh_fun->ops = &rh_ops;
|
---|
| 211 | instance->rh_fun->driver_data = &instance->rh;
|
---|
[9351353] | 212 |
|
---|
[d2bff2f] | 213 | uintptr_t reg_base = 0;
|
---|
| 214 | size_t reg_size = 0;
|
---|
[9351353] | 215 | int irq = 0;
|
---|
| 216 |
|
---|
[d2bff2f] | 217 | ret = pci_get_my_registers(device, ®_base, ®_size, &irq);
|
---|
| 218 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[4125b7d] | 219 | "Failed to get I/O addresses for %" PRIun ": %s.\n",
|
---|
| 220 | device->handle, str_error(ret));
|
---|
| 221 | usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
|
---|
[d2bff2f] | 222 | (void *) reg_base, reg_size, irq);
|
---|
[9351353] | 223 |
|
---|
| 224 | ret = pci_disable_legacy(device);
|
---|
[d2bff2f] | 225 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[dfe4955] | 226 | "Failed to disable legacy USB: %s.\n", str_error(ret));
|
---|
| 227 |
|
---|
| 228 | const size_t cmd_count = hc_irq_cmd_count();
|
---|
| 229 | irq_cmd_t irq_cmds[cmd_count];
|
---|
| 230 | ret =
|
---|
| 231 | hc_get_irq_commands(irq_cmds, sizeof(irq_cmds), reg_base, reg_size);
|
---|
| 232 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
| 233 | "Failed to generate IRQ commands: %s.\n", str_error(ret));
|
---|
| 234 |
|
---|
| 235 | irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = irq_cmds };
|
---|
| 236 |
|
---|
| 237 | /* Register handler to avoid interrupt lockup */
|
---|
| 238 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
| 239 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
| 240 | "Failed to register interrupt handler: %s.\n", str_error(ret));
|
---|
[9351353] | 241 |
|
---|
[ff34e5a] | 242 | bool interrupts = false;
|
---|
[9351353] | 243 | ret = pci_enable_interrupts(device);
|
---|
| 244 | if (ret != EOK) {
|
---|
[dfe4955] | 245 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
| 246 | " Falling back to polling.\n", str_error(ret));
|
---|
[ff34e5a] | 247 | } else {
|
---|
| 248 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
| 249 | interrupts = true;
|
---|
[9351353] | 250 | }
|
---|
| 251 |
|
---|
[d2bff2f] | 252 | ret = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
|
---|
| 253 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[408d3c9] | 254 | "Failed to init uhci_hcd: %s.\n", str_error(ret));
|
---|
[9d2d444] | 255 |
|
---|
[dfe4955] | 256 | device->driver_data = instance;
|
---|
| 257 |
|
---|
[9351353] | 258 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
| 259 | if (ret != EOK) { \
|
---|
[c01cd32] | 260 | hc_fini(&instance->hc); \
|
---|
[d2bff2f] | 261 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
[9351353] | 262 | return ret; \
|
---|
[d2bff2f] | 263 | } else (void)0
|
---|
[9351353] | 264 |
|
---|
[d2bff2f] | 265 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
[408d3c9] | 266 | CHECK_RET_FINI_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
|
---|
| 267 | str_error(ret));
|
---|
[9351353] | 268 |
|
---|
[76ef94e] | 269 | ret = ddf_fun_add_to_class(instance->hc_fun, USB_HC_DDF_CLASS_NAME);
|
---|
[3ae5ca8] | 270 | CHECK_RET_FINI_RETURN(ret,
|
---|
[76ef94e] | 271 | "Failed to add UHCI to HC class: %s.\n", str_error(ret));
|
---|
| 272 |
|
---|
[33fbe95] | 273 | ret = rh_init(&instance->rh, instance->rh_fun,
|
---|
[9351353] | 274 | (uintptr_t)instance->hc.registers + 0x10, 4);
|
---|
| 275 | CHECK_RET_FINI_RETURN(ret,
|
---|
[408d3c9] | 276 | "Failed to setup UHCI root hub: %s.\n", str_error(ret));
|
---|
[9351353] | 277 |
|
---|
| 278 | ret = ddf_fun_bind(instance->rh_fun);
|
---|
| 279 | CHECK_RET_FINI_RETURN(ret,
|
---|
[408d3c9] | 280 | "Failed to register UHCI root hub: %s.\n", str_error(ret));
|
---|
[9351353] | 281 |
|
---|
| 282 | return EOK;
|
---|
| 283 | #undef CHECK_RET_FINI_RETURN
|
---|
| 284 | }
|
---|
| 285 | /**
|
---|
| 286 | * @}
|
---|
| 287 | */
|
---|