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