| 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 libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | *
|
|---|
| 34 | * Host controller driver framework. Encapsulates DDF device of HC to an
|
|---|
| 35 | * hc_device_t, which is passed to driver implementing hc_driver_t.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <ddf/interrupt.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <macros.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <usb/debug.h>
|
|---|
| 45 | #include <usb/descriptor.h>
|
|---|
| 46 | #include <usb/request.h>
|
|---|
| 47 | #include <usb_iface.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "bus.h"
|
|---|
| 50 | #include "ddf_helpers.h"
|
|---|
| 51 | #include "endpoint.h"
|
|---|
| 52 | #include "usb_transfer_batch.h"
|
|---|
| 53 |
|
|---|
| 54 | #include "hcd.h"
|
|---|
| 55 |
|
|---|
| 56 | int hc_dev_add(ddf_dev_t *);
|
|---|
| 57 | int hc_dev_remove(ddf_dev_t *);
|
|---|
| 58 | int hc_dev_gone(ddf_dev_t *);
|
|---|
| 59 | int hc_fun_online(ddf_fun_t *);
|
|---|
| 60 | int hc_fun_offline(ddf_fun_t *);
|
|---|
| 61 |
|
|---|
| 62 | static driver_ops_t hc_driver_ops = {
|
|---|
| 63 | .dev_add = hc_dev_add,
|
|---|
| 64 | .dev_remove = hc_dev_remove,
|
|---|
| 65 | .dev_gone = hc_dev_gone,
|
|---|
| 66 | .fun_online = hc_fun_online,
|
|---|
| 67 | .fun_offline = hc_fun_offline,
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | static const hc_driver_t *hc_driver;
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * The main HC driver routine.
|
|---|
| 74 | */
|
|---|
| 75 | int hc_driver_main(const hc_driver_t *driver)
|
|---|
| 76 | {
|
|---|
| 77 | driver_t ddf_driver = {
|
|---|
| 78 | .name = driver->name,
|
|---|
| 79 | .driver_ops = &hc_driver_ops,
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | /* Remember ops to call. */
|
|---|
| 83 | hc_driver = driver;
|
|---|
| 84 |
|
|---|
| 85 | return ddf_driver_main(&ddf_driver);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * IRQ handling callback. Call the bus operation.
|
|---|
| 90 | *
|
|---|
| 91 | * Currently, there is a bus ops lookup to find the interrupt handler. So far,
|
|---|
| 92 | * the mechanism is too flexible, as it allows different instances of HC to
|
|---|
| 93 | * have different IRQ handlers, disallowing us to optimize the lookup here.
|
|---|
| 94 | * TODO: Make the bus mechanism less flexible in irq handling and remove the
|
|---|
| 95 | * lookup.
|
|---|
| 96 | */
|
|---|
| 97 | static void irq_handler(ipc_call_t *call, ddf_dev_t *dev)
|
|---|
| 98 | {
|
|---|
| 99 | assert(dev);
|
|---|
| 100 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 101 |
|
|---|
| 102 | const uint32_t status = IPC_GET_ARG1(*call);
|
|---|
| 103 | hcd->bus->ops->interrupt(hcd->bus, status);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Worker for the HW interrupt replacement fibril.
|
|---|
| 108 | */
|
|---|
| 109 | static errno_t interrupt_polling(void *arg)
|
|---|
| 110 | {
|
|---|
| 111 | bus_t *bus = arg;
|
|---|
| 112 | assert(bus);
|
|---|
| 113 |
|
|---|
| 114 | if (!bus->ops->interrupt || !bus->ops->status)
|
|---|
| 115 | return ENOTSUP;
|
|---|
| 116 |
|
|---|
| 117 | uint32_t status = 0;
|
|---|
| 118 | while (bus->ops->status(bus, &status) == EOK) {
|
|---|
| 119 | bus->ops->interrupt(bus, status);
|
|---|
| 120 | status = 0;
|
|---|
| 121 | /* We should wait 1 frame - 1ms here, but this polling is a
|
|---|
| 122 | * lame crutch anyway so don't hog the system. 10ms is still
|
|---|
| 123 | * good enough for emergency mode */
|
|---|
| 124 | async_usleep(10000);
|
|---|
| 125 | }
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * Clean the IRQ code bottom-half.
|
|---|
| 131 | */
|
|---|
| 132 | static inline void irq_code_clean(irq_code_t *code)
|
|---|
| 133 | {
|
|---|
| 134 | if (code) {
|
|---|
| 135 | free(code->ranges);
|
|---|
| 136 | free(code->cmds);
|
|---|
| 137 | code->ranges = NULL;
|
|---|
| 138 | code->cmds = NULL;
|
|---|
| 139 | code->rangecount = 0;
|
|---|
| 140 | code->cmdcount = 0;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Register an interrupt handler. If there is a callback to setup the bottom half,
|
|---|
| 146 | * invoke it and register it. Register for notifications.
|
|---|
| 147 | *
|
|---|
| 148 | * If this method fails, a polling fibril is started instead.
|
|---|
| 149 | *
|
|---|
| 150 | * @param[in] hcd Host controller device.
|
|---|
| 151 | * @param[in] hw_res Resources to be used.
|
|---|
| 152 | *
|
|---|
| 153 | * @return IRQ capability handle on success.
|
|---|
| 154 | * @return Negative error code.
|
|---|
| 155 | */
|
|---|
| 156 | static errno_t hcd_ddf_setup_interrupts(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
|---|
| 157 | {
|
|---|
| 158 | assert(hcd);
|
|---|
| 159 | irq_code_t irq_code = {0};
|
|---|
| 160 |
|
|---|
| 161 | if (!hc_driver->irq_code_gen)
|
|---|
| 162 | return ENOTSUP;
|
|---|
| 163 |
|
|---|
| 164 | int irq;
|
|---|
| 165 | errno_t ret;
|
|---|
| 166 | ret = hc_driver->irq_code_gen(&irq_code, hcd, hw_res, &irq);
|
|---|
| 167 | if (ret != EOK) {
|
|---|
| 168 | usb_log_error("Failed to generate IRQ code: %s.",
|
|---|
| 169 | str_error(irq));
|
|---|
| 170 | return irq;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /* Register handler to avoid interrupt lockup */
|
|---|
| 174 | int irq_cap;
|
|---|
| 175 | ret = register_interrupt_handler(hcd->ddf_dev, irq, irq_handler, &irq_code, &irq_cap);
|
|---|
| 176 | irq_code_clean(&irq_code);
|
|---|
| 177 | if (ret != EOK) {
|
|---|
| 178 | usb_log_error("Failed to register interrupt handler: %s.",
|
|---|
| 179 | str_error(irq_cap));
|
|---|
| 180 | return irq_cap;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /* Enable interrupts */
|
|---|
| 184 | ret = hcd_ddf_enable_interrupt(hcd, irq);
|
|---|
| 185 | if (ret != EOK) {
|
|---|
| 186 | usb_log_error("Failed to enable interrupts: %s.",
|
|---|
| 187 | str_error(ret));
|
|---|
| 188 | unregister_interrupt_handler(hcd->ddf_dev, irq_cap);
|
|---|
| 189 | return ret;
|
|---|
| 190 | }
|
|---|
| 191 | return irq_cap;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /**
|
|---|
| 195 | * Initialize HC in memory of the driver.
|
|---|
| 196 | *
|
|---|
| 197 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 198 | * - gets device's hw resources
|
|---|
| 199 | * - attempts to enable interrupts
|
|---|
| 200 | * - registers interrupt handler
|
|---|
| 201 | * - calls driver specific initialization
|
|---|
| 202 | * - registers root hub
|
|---|
| 203 | *
|
|---|
| 204 | * @param device DDF instance of the device to use
|
|---|
| 205 | * @return Error code
|
|---|
| 206 | */
|
|---|
| 207 | errno_t hc_dev_add(ddf_dev_t *device)
|
|---|
| 208 | {
|
|---|
| 209 | errno_t ret = EOK;
|
|---|
| 210 | assert(device);
|
|---|
| 211 |
|
|---|
| 212 | if (!hc_driver->hc_add) {
|
|---|
| 213 | usb_log_error("Driver '%s' does not support adding devices.", hc_driver->name);
|
|---|
| 214 | return ENOTSUP;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | ret = hcd_ddf_setup_hc(device, hc_driver->hc_device_size);
|
|---|
| 218 | if (ret != EOK) {
|
|---|
| 219 | usb_log_error("Failed to setup HC device.");
|
|---|
| 220 | return ret;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | hc_device_t *hcd = dev_to_hcd(device);
|
|---|
| 224 |
|
|---|
| 225 | hw_res_list_parsed_t hw_res;
|
|---|
| 226 | ret = hcd_ddf_get_registers(hcd, &hw_res);
|
|---|
| 227 | if (ret != EOK) {
|
|---|
| 228 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 229 | "for `%s': %s.", ddf_dev_get_name(device),
|
|---|
| 230 | str_error(ret));
|
|---|
| 231 | goto err_hcd;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | ret = hc_driver->hc_add(hcd, &hw_res);
|
|---|
| 235 | if (ret != EOK) {
|
|---|
| 236 | usb_log_error("Failed to init HCD.");
|
|---|
| 237 | goto err_hw_res;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | assert(hcd->bus);
|
|---|
| 241 |
|
|---|
| 242 | /* Setup interrupts */
|
|---|
| 243 | hcd->irq_cap = hcd_ddf_setup_interrupts(hcd, &hw_res);
|
|---|
| 244 | if (hcd->irq_cap >= 0) {
|
|---|
| 245 | usb_log_debug("Hw interrupts enabled.");
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /* Claim the device from BIOS */
|
|---|
| 249 | if (hc_driver->claim)
|
|---|
| 250 | ret = hc_driver->claim(hcd);
|
|---|
| 251 | if (ret != EOK) {
|
|---|
| 252 | usb_log_error("Failed to claim `%s' for `%s': %s",
|
|---|
| 253 | ddf_dev_get_name(device), hc_driver->name, str_error(ret));
|
|---|
| 254 | goto err_irq;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /* Start hw */
|
|---|
| 258 | if (hc_driver->start)
|
|---|
| 259 | ret = hc_driver->start(hcd);
|
|---|
| 260 | if (ret != EOK) {
|
|---|
| 261 | usb_log_error("Failed to start HCD: %s.", str_error(ret));
|
|---|
| 262 | goto err_irq;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | const bus_ops_t *ops = hcd->bus->ops;
|
|---|
| 266 |
|
|---|
| 267 | /* Need working irq replacement to setup root hub */
|
|---|
| 268 | if (hcd->irq_cap < 0 && ops->status) {
|
|---|
| 269 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd->bus);
|
|---|
| 270 | if (!hcd->polling_fibril) {
|
|---|
| 271 | usb_log_error("Failed to create polling fibril");
|
|---|
| 272 | ret = ENOMEM;
|
|---|
| 273 | goto err_started;
|
|---|
| 274 | }
|
|---|
| 275 | fibril_add_ready(hcd->polling_fibril);
|
|---|
| 276 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 277 | " Falling back to polling.", str_error(hcd->irq_cap));
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*
|
|---|
| 281 | * Creating root hub registers a new USB device so HC
|
|---|
| 282 | * needs to be ready at this time.
|
|---|
| 283 | */
|
|---|
| 284 | if (hc_driver->setup_root_hub)
|
|---|
| 285 | ret = hc_driver->setup_root_hub(hcd);
|
|---|
| 286 | if (ret != EOK) {
|
|---|
| 287 | usb_log_error("Failed to setup HC root hub: %s.",
|
|---|
| 288 | str_error(ret));
|
|---|
| 289 | goto err_polling;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | usb_log_info("Controlling new `%s' device `%s'.",
|
|---|
| 293 | hc_driver->name, ddf_dev_get_name(device));
|
|---|
| 294 | return EOK;
|
|---|
| 295 |
|
|---|
| 296 | err_polling:
|
|---|
| 297 | // TODO: Stop the polling fibril (refactor the interrupt_polling func)
|
|---|
| 298 | //
|
|---|
| 299 | err_started:
|
|---|
| 300 | if (hc_driver->stop)
|
|---|
| 301 | hc_driver->stop(hcd);
|
|---|
| 302 | err_irq:
|
|---|
| 303 | unregister_interrupt_handler(device, hcd->irq_cap);
|
|---|
| 304 | if (hc_driver->hc_remove)
|
|---|
| 305 | hc_driver->hc_remove(hcd);
|
|---|
| 306 | err_hw_res:
|
|---|
| 307 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 308 | err_hcd:
|
|---|
| 309 | hcd_ddf_clean_hc(hcd);
|
|---|
| 310 | return ret;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | errno_t hc_dev_remove(ddf_dev_t *dev)
|
|---|
| 314 | {
|
|---|
| 315 | errno_t err;
|
|---|
| 316 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 317 |
|
|---|
| 318 | if (hc_driver->stop)
|
|---|
| 319 | if ((err = hc_driver->stop(hcd)))
|
|---|
| 320 | return err;
|
|---|
| 321 |
|
|---|
| 322 | unregister_interrupt_handler(dev, hcd->irq_cap);
|
|---|
| 323 |
|
|---|
| 324 | if (hc_driver->hc_remove)
|
|---|
| 325 | if ((err = hc_driver->hc_remove(hcd)))
|
|---|
| 326 | return err;
|
|---|
| 327 |
|
|---|
| 328 | hcd_ddf_clean_hc(hcd);
|
|---|
| 329 |
|
|---|
| 330 | // TODO probably not complete
|
|---|
| 331 |
|
|---|
| 332 | return EOK;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | errno_t hc_dev_gone(ddf_dev_t *dev)
|
|---|
| 336 | {
|
|---|
| 337 | errno_t err = ENOTSUP;
|
|---|
| 338 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 339 |
|
|---|
| 340 | if (hc_driver->hc_gone)
|
|---|
| 341 | err = hc_driver->hc_gone(hcd);
|
|---|
| 342 |
|
|---|
| 343 | hcd_ddf_clean_hc(hcd);
|
|---|
| 344 |
|
|---|
| 345 | return err;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | errno_t hc_fun_online(ddf_fun_t *fun)
|
|---|
| 349 | {
|
|---|
| 350 | assert(fun);
|
|---|
| 351 |
|
|---|
| 352 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 353 | assert(dev);
|
|---|
| 354 |
|
|---|
| 355 | usb_log_info("Device(%d): Requested to be brought online.", dev->address);
|
|---|
| 356 | return bus_device_online(dev);
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | int hc_fun_offline(ddf_fun_t *fun)
|
|---|
| 360 | {
|
|---|
| 361 | assert(fun);
|
|---|
| 362 |
|
|---|
| 363 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 364 | assert(dev);
|
|---|
| 365 |
|
|---|
| 366 | usb_log_info("Device(%d): Requested to be taken offline.", dev->address);
|
|---|
| 367 | return bus_device_offline(dev);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 | /**
|
|---|
| 372 | * @}
|
|---|
| 373 | */
|
|---|