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