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