[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 |
|
---|
| 46 |
|
---|
| 47 | /** IRQ handling callback, identifies device
|
---|
| 48 | *
|
---|
| 49 | * @param[in] dev DDF instance of the device to use.
|
---|
| 50 | * @param[in] iid (Unused).
|
---|
| 51 | * @param[in] call Pointer to the call that represents interrupt.
|
---|
| 52 | */
|
---|
| 53 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
| 54 | {
|
---|
| 55 | assert(dev);
|
---|
[a9f91cd] | 56 | uhci_hc_t *hc = &((uhci_t*)dev->driver_data)->hc;
|
---|
[9351353] | 57 | uint16_t status = IPC_GET_ARG1(*call);
|
---|
| 58 | assert(hc);
|
---|
[a9f91cd] | 59 | uhci_hc_interrupt(hc, status);
|
---|
[9351353] | 60 | }
|
---|
| 61 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 62 | /** Get address of the device identified by handle.
|
---|
| 63 | *
|
---|
| 64 | * @param[in] dev DDF instance of the device to use.
|
---|
| 65 | * @param[in] iid (Unused).
|
---|
| 66 | * @param[in] call Pointer to the call that represents interrupt.
|
---|
| 67 | */
|
---|
[9351353] | 68 | static int usb_iface_get_address(
|
---|
| 69 | ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
|
---|
| 70 | {
|
---|
| 71 | assert(fun);
|
---|
[68b5ed6e] | 72 | usb_device_keeper_t *manager = &((uhci_t*)fun->dev->driver_data)->hc.device_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 | /*----------------------------------------------------------------------------*/
|
---|
| 86 | /** Gets handle of the respective hc (this or parent device).
|
---|
| 87 | *
|
---|
| 88 | * @param[in] root_hub_fun Root hub function seeking hc handle.
|
---|
| 89 | * @param[out] handle Place to write the handle.
|
---|
| 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 | /*----------------------------------------------------------------------------*/
|
---|
| 103 | /** This iface is generic for both RH and HC. */
|
---|
| 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 | /*----------------------------------------------------------------------------*/
|
---|
| 109 | static ddf_dev_ops_t uhci_hc_ops = {
|
---|
| 110 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 111 | .interfaces[USBHC_DEV_IFACE] = &uhci_hc_iface, /* see iface.h/c */
|
---|
| 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);
|
---|
| 122 | return &((uhci_rh_t*)fun->driver_data)->resource_list;
|
---|
| 123 | }
|
---|
| 124 | /*----------------------------------------------------------------------------*/
|
---|
| 125 | static hw_res_ops_t hw_res_iface = {
|
---|
| 126 | .get_resource_list = get_resource_list,
|
---|
| 127 | .enable_interrupt = NULL
|
---|
| 128 | };
|
---|
| 129 | /*----------------------------------------------------------------------------*/
|
---|
| 130 | static ddf_dev_ops_t uhci_rh_ops = {
|
---|
| 131 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 132 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
| 133 | };
|
---|
| 134 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 135 | /** Initialize hc and rh ddf structures and their respective drivers.
|
---|
| 136 | *
|
---|
| 137 | * @param[in] instance UHCI structure to use.
|
---|
| 138 | * @param[in] device DDF instance of the device to use.
|
---|
| 139 | *
|
---|
| 140 | * This function does all the preparatory work for hc and rh drivers:
|
---|
| 141 | * - gets device hw resources
|
---|
| 142 | * - disables UHCI legacy support
|
---|
| 143 | * - asks for interrupt
|
---|
| 144 | * - registers interrupt handler
|
---|
| 145 | */
|
---|
[9351353] | 146 | int uhci_init(uhci_t *instance, ddf_dev_t *device)
|
---|
| 147 | {
|
---|
| 148 | assert(instance);
|
---|
| 149 | instance->hc_fun = NULL;
|
---|
| 150 | instance->rh_fun = NULL;
|
---|
| 151 | #define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
|
---|
| 152 | if (ret != EOK) { \
|
---|
| 153 | usb_log_error(message); \
|
---|
| 154 | if (instance->hc_fun) \
|
---|
| 155 | ddf_fun_destroy(instance->hc_fun); \
|
---|
| 156 | if (instance->rh_fun) \
|
---|
| 157 | ddf_fun_destroy(instance->rh_fun); \
|
---|
| 158 | return ret; \
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | uintptr_t io_reg_base = 0;
|
---|
| 162 | size_t io_reg_size = 0;
|
---|
| 163 | int irq = 0;
|
---|
| 164 |
|
---|
| 165 | int ret =
|
---|
| 166 | pci_get_my_registers(device, &io_reg_base, &io_reg_size, &irq);
|
---|
| 167 | CHECK_RET_DEST_FUN_RETURN(ret,
|
---|
| 168 | "Failed(%d) to get I/O addresses:.\n", ret, device->handle);
|
---|
[fbefd0e] | 169 | usb_log_debug("I/O regs at 0x%X (size %zu), IRQ %d.\n",
|
---|
[9351353] | 170 | io_reg_base, io_reg_size, irq);
|
---|
| 171 |
|
---|
| 172 | ret = pci_disable_legacy(device);
|
---|
| 173 | CHECK_RET_DEST_FUN_RETURN(ret,
|
---|
| 174 | "Failed(%d) to disable legacy USB: %s.\n", ret, str_error(ret));
|
---|
| 175 |
|
---|
[ff34e5a] | 176 | bool interrupts = false;
|
---|
[9351353] | 177 | ret = pci_enable_interrupts(device);
|
---|
| 178 | if (ret != EOK) {
|
---|
| 179 | usb_log_warning(
|
---|
| 180 | "Failed(%d) to enable interrupts, fall back to polling.\n",
|
---|
| 181 | ret);
|
---|
[ff34e5a] | 182 | } else {
|
---|
| 183 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
| 184 | interrupts = true;
|
---|
[9351353] | 185 | }
|
---|
| 186 |
|
---|
| 187 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci-hc");
|
---|
| 188 | ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
|
---|
[ff34e5a] | 189 | CHECK_RET_DEST_FUN_RETURN(ret,
|
---|
| 190 | "Failed(%d) to create HC function.\n", ret);
|
---|
[9351353] | 191 |
|
---|
[ff34e5a] | 192 | ret = uhci_hc_init(&instance->hc, instance->hc_fun,
|
---|
| 193 | (void*)io_reg_base, io_reg_size, interrupts);
|
---|
[9351353] | 194 | CHECK_RET_DEST_FUN_RETURN(ret, "Failed(%d) to init uhci-hcd.\n", ret);
|
---|
| 195 | instance->hc_fun->ops = &uhci_hc_ops;
|
---|
| 196 | instance->hc_fun->driver_data = &instance->hc;
|
---|
| 197 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
| 198 | CHECK_RET_DEST_FUN_RETURN(ret,
|
---|
| 199 | "Failed(%d) to bind UHCI device function: %s.\n",
|
---|
| 200 | ret, str_error(ret));
|
---|
| 201 | #undef CHECK_RET_HC_RETURN
|
---|
| 202 |
|
---|
| 203 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
| 204 | if (ret != EOK) { \
|
---|
| 205 | usb_log_error(message); \
|
---|
| 206 | if (instance->hc_fun) \
|
---|
| 207 | ddf_fun_destroy(instance->hc_fun); \
|
---|
| 208 | if (instance->rh_fun) \
|
---|
| 209 | ddf_fun_destroy(instance->rh_fun); \
|
---|
[a9f91cd] | 210 | uhci_hc_fini(&instance->hc); \
|
---|
[9351353] | 211 | return ret; \
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* It does no harm if we register this on polling */
|
---|
| 215 | ret = register_interrupt_handler(device, irq, irq_handler,
|
---|
| 216 | &instance->hc.interrupt_code);
|
---|
| 217 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 218 | "Failed(%d) to register interrupt handler.\n", ret);
|
---|
| 219 |
|
---|
| 220 | instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci-rh");
|
---|
| 221 | ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
|
---|
| 222 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 223 | "Failed(%d) to create root hub function.\n", ret);
|
---|
| 224 |
|
---|
| 225 | ret = uhci_rh_init(&instance->rh, instance->rh_fun,
|
---|
| 226 | (uintptr_t)instance->hc.registers + 0x10, 4);
|
---|
| 227 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 228 | "Failed(%d) to setup UHCI root hub.\n", ret);
|
---|
| 229 |
|
---|
| 230 | instance->rh_fun->ops = &uhci_rh_ops;
|
---|
| 231 | instance->rh_fun->driver_data = &instance->rh;
|
---|
| 232 | ret = ddf_fun_bind(instance->rh_fun);
|
---|
| 233 | CHECK_RET_FINI_RETURN(ret,
|
---|
| 234 | "Failed(%d) to register UHCI root hub.\n", ret);
|
---|
| 235 |
|
---|
| 236 | return EOK;
|
---|
| 237 | #undef CHECK_RET_FINI_RETURN
|
---|
| 238 | }
|
---|
| 239 | /**
|
---|
| 240 | * @}
|
---|
| 241 | */
|
---|