| 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_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
|---|
| 98 | {
|
|---|
| 99 | assert(dev);
|
|---|
| 100 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 101 |
|
|---|
| 102 | const bus_ops_t *ops = BUS_OPS_LOOKUP(hcd->bus->ops, interrupt);
|
|---|
| 103 | assert(ops);
|
|---|
| 104 |
|
|---|
| 105 | const uint32_t status = IPC_GET_ARG1(*call);
|
|---|
| 106 | ops->interrupt(hcd->bus, status);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Worker for the HW interrupt replacement fibril.
|
|---|
| 111 | */
|
|---|
| 112 | static int interrupt_polling(void *arg)
|
|---|
| 113 | {
|
|---|
| 114 | bus_t *bus = arg;
|
|---|
| 115 | assert(bus);
|
|---|
| 116 |
|
|---|
| 117 | const bus_ops_t *interrupt_ops = BUS_OPS_LOOKUP(bus->ops, interrupt);
|
|---|
| 118 | const bus_ops_t *status_ops = BUS_OPS_LOOKUP(bus->ops, status);
|
|---|
| 119 | if (!interrupt_ops || !status_ops)
|
|---|
| 120 | return ENOTSUP;
|
|---|
| 121 |
|
|---|
| 122 | uint32_t status = 0;
|
|---|
| 123 | while (status_ops->status(bus, &status) == EOK) {
|
|---|
| 124 | interrupt_ops->interrupt(bus, status);
|
|---|
| 125 | status = 0;
|
|---|
| 126 | /* We should wait 1 frame - 1ms here, but this polling is a
|
|---|
| 127 | * lame crutch anyway so don't hog the system. 10ms is still
|
|---|
| 128 | * good enough for emergency mode */
|
|---|
| 129 | async_usleep(10000);
|
|---|
| 130 | }
|
|---|
| 131 | return EOK;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Clean the IRQ code bottom-half.
|
|---|
| 136 | */
|
|---|
| 137 | static inline void irq_code_clean(irq_code_t *code)
|
|---|
| 138 | {
|
|---|
| 139 | if (code) {
|
|---|
| 140 | free(code->ranges);
|
|---|
| 141 | free(code->cmds);
|
|---|
| 142 | code->ranges = NULL;
|
|---|
| 143 | code->cmds = NULL;
|
|---|
| 144 | code->rangecount = 0;
|
|---|
| 145 | code->cmdcount = 0;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Register an interrupt handler. If there is a callback to setup the bottom half,
|
|---|
| 151 | * invoke it and register it. Register for notifications.
|
|---|
| 152 | *
|
|---|
| 153 | * If this method fails, a polling fibril is started instead.
|
|---|
| 154 | *
|
|---|
| 155 | * @param[in] hcd Host controller device.
|
|---|
| 156 | * @param[in] hw_res Resources to be used.
|
|---|
| 157 | *
|
|---|
| 158 | * @return IRQ capability handle on success.
|
|---|
| 159 | * @return Negative error code.
|
|---|
| 160 | */
|
|---|
| 161 | static int hcd_ddf_setup_interrupts(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
|---|
| 162 | {
|
|---|
| 163 | assert(hcd);
|
|---|
| 164 | irq_code_t irq_code = {0};
|
|---|
| 165 |
|
|---|
| 166 | if (!hc_driver->irq_code_gen)
|
|---|
| 167 | return ENOTSUP;
|
|---|
| 168 |
|
|---|
| 169 | const int irq = hc_driver->irq_code_gen(&irq_code, hcd, hw_res);
|
|---|
| 170 | if (irq < 0) {
|
|---|
| 171 | usb_log_error("Failed to generate IRQ code: %s.\n",
|
|---|
| 172 | str_error(irq));
|
|---|
| 173 | return irq;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /* Register handler to avoid interrupt lockup */
|
|---|
| 177 | const int irq_cap = register_interrupt_handler(hcd->ddf_dev, irq, irq_handler, &irq_code);
|
|---|
| 178 | irq_code_clean(&irq_code);
|
|---|
| 179 | if (irq_cap < 0) {
|
|---|
| 180 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
|---|
| 181 | str_error(irq_cap));
|
|---|
| 182 | return irq_cap;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /* Enable interrupts */
|
|---|
| 186 | int ret = hcd_ddf_enable_interrupt(hcd, irq);
|
|---|
| 187 | if (ret != EOK) {
|
|---|
| 188 | usb_log_error("Failed to enable interrupts: %s.\n",
|
|---|
| 189 | str_error(ret));
|
|---|
| 190 | unregister_interrupt_handler(hcd->ddf_dev, irq_cap);
|
|---|
| 191 | return ret;
|
|---|
| 192 | }
|
|---|
| 193 | return irq_cap;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Initialize HC in memory of the driver.
|
|---|
| 198 | *
|
|---|
| 199 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 200 | * - gets device's hw resources
|
|---|
| 201 | * - attempts to enable interrupts
|
|---|
| 202 | * - registers interrupt handler
|
|---|
| 203 | * - calls driver specific initialization
|
|---|
| 204 | * - registers root hub
|
|---|
| 205 | *
|
|---|
| 206 | * @param device DDF instance of the device to use
|
|---|
| 207 | * @return Error code
|
|---|
| 208 | */
|
|---|
| 209 | int hc_dev_add(ddf_dev_t *device)
|
|---|
| 210 | {
|
|---|
| 211 | int ret = EOK;
|
|---|
| 212 | assert(device);
|
|---|
| 213 |
|
|---|
| 214 | if (!hc_driver->hc_add) {
|
|---|
| 215 | usb_log_error("Driver '%s' does not support adding devices.", hc_driver->name);
|
|---|
| 216 | return ENOTSUP;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | ret = hcd_ddf_setup_hc(device, hc_driver->hc_device_size);
|
|---|
| 220 | if (ret != EOK) {
|
|---|
| 221 | usb_log_error("Failed to setup HC device.\n");
|
|---|
| 222 | return ret;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | hc_device_t *hcd = dev_to_hcd(device);
|
|---|
| 226 |
|
|---|
| 227 | hw_res_list_parsed_t hw_res;
|
|---|
| 228 | ret = hcd_ddf_get_registers(hcd, &hw_res);
|
|---|
| 229 | if (ret != EOK) {
|
|---|
| 230 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 231 | "for `%s': %s.\n", ddf_dev_get_name(device),
|
|---|
| 232 | str_error(ret));
|
|---|
| 233 | goto err_hcd;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | ret = hc_driver->hc_add(hcd, &hw_res);
|
|---|
| 237 | if (ret != EOK) {
|
|---|
| 238 | usb_log_error("Failed to init HCD.\n");
|
|---|
| 239 | goto err_hw_res;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | assert(hcd->bus);
|
|---|
| 243 |
|
|---|
| 244 | /* Setup interrupts */
|
|---|
| 245 | hcd->irq_cap = hcd_ddf_setup_interrupts(hcd, &hw_res);
|
|---|
| 246 | if (hcd->irq_cap >= 0) {
|
|---|
| 247 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /* Claim the device from BIOS */
|
|---|
| 251 | if (hc_driver->claim)
|
|---|
| 252 | ret = hc_driver->claim(hcd);
|
|---|
| 253 | if (ret != EOK) {
|
|---|
| 254 | usb_log_error("Failed to claim `%s' for `%s': %s",
|
|---|
| 255 | ddf_dev_get_name(device), hc_driver->name, str_error(ret));
|
|---|
| 256 | goto err_irq;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /* Start hw */
|
|---|
| 260 | if (hc_driver->start)
|
|---|
| 261 | ret = hc_driver->start(hcd);
|
|---|
| 262 | if (ret != EOK) {
|
|---|
| 263 | usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
|
|---|
| 264 | goto err_irq;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | const bus_ops_t *ops = BUS_OPS_LOOKUP(hcd->bus->ops, status);
|
|---|
| 268 |
|
|---|
| 269 | /* Need working irq replacement to setup root hub */
|
|---|
| 270 | if (hcd->irq_cap < 0 && ops) {
|
|---|
| 271 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd->bus);
|
|---|
| 272 | if (!hcd->polling_fibril) {
|
|---|
| 273 | usb_log_error("Failed to create polling fibril\n");
|
|---|
| 274 | ret = ENOMEM;
|
|---|
| 275 | goto err_started;
|
|---|
| 276 | }
|
|---|
| 277 | fibril_add_ready(hcd->polling_fibril);
|
|---|
| 278 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 279 | " Falling back to polling.\n", str_error(hcd->irq_cap));
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /*
|
|---|
| 283 | * Creating root hub registers a new USB device so HC
|
|---|
| 284 | * needs to be ready at this time.
|
|---|
| 285 | */
|
|---|
| 286 | if (hc_driver->setup_root_hub)
|
|---|
| 287 | ret = hc_driver->setup_root_hub(hcd);
|
|---|
| 288 | if (ret != EOK) {
|
|---|
| 289 | usb_log_error("Failed to setup HC root hub: %s.\n",
|
|---|
| 290 | str_error(ret));
|
|---|
| 291 | goto err_polling;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | usb_log_info("Controlling new `%s' device `%s'.\n",
|
|---|
| 295 | hc_driver->name, ddf_dev_get_name(device));
|
|---|
| 296 | return EOK;
|
|---|
| 297 |
|
|---|
| 298 | err_polling:
|
|---|
| 299 | // TODO: Stop the polling fibril (refactor the interrupt_polling func)
|
|---|
| 300 | //
|
|---|
| 301 | err_started:
|
|---|
| 302 | if (hc_driver->stop)
|
|---|
| 303 | hc_driver->stop(hcd);
|
|---|
| 304 | err_irq:
|
|---|
| 305 | unregister_interrupt_handler(device, hcd->irq_cap);
|
|---|
| 306 | if (hc_driver->hc_remove)
|
|---|
| 307 | hc_driver->hc_remove(hcd);
|
|---|
| 308 | err_hw_res:
|
|---|
| 309 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 310 | err_hcd:
|
|---|
| 311 | hcd_ddf_clean_hc(hcd);
|
|---|
| 312 | return ret;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | int hc_dev_remove(ddf_dev_t *dev)
|
|---|
| 316 | {
|
|---|
| 317 | int err;
|
|---|
| 318 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 319 |
|
|---|
| 320 | if (hc_driver->stop)
|
|---|
| 321 | if ((err = hc_driver->stop(hcd)))
|
|---|
| 322 | return err;
|
|---|
| 323 |
|
|---|
| 324 | unregister_interrupt_handler(dev, hcd->irq_cap);
|
|---|
| 325 |
|
|---|
| 326 | if (hc_driver->hc_remove)
|
|---|
| 327 | if ((err = hc_driver->hc_remove(hcd)))
|
|---|
| 328 | return err;
|
|---|
| 329 |
|
|---|
| 330 | hcd_ddf_clean_hc(hcd);
|
|---|
| 331 |
|
|---|
| 332 | // TODO probably not complete
|
|---|
| 333 |
|
|---|
| 334 | return EOK;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | int hc_dev_gone(ddf_dev_t *dev)
|
|---|
| 338 | {
|
|---|
| 339 | int err = ENOTSUP;
|
|---|
| 340 | hc_device_t *hcd = dev_to_hcd(dev);
|
|---|
| 341 |
|
|---|
| 342 | if (hc_driver->hc_gone)
|
|---|
| 343 | err = hc_driver->hc_gone(hcd);
|
|---|
| 344 |
|
|---|
| 345 | hcd_ddf_clean_hc(hcd);
|
|---|
| 346 |
|
|---|
| 347 | return err;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | int hc_fun_online(ddf_fun_t *fun)
|
|---|
| 351 | {
|
|---|
| 352 | assert(fun);
|
|---|
| 353 |
|
|---|
| 354 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 355 | assert(dev);
|
|---|
| 356 |
|
|---|
| 357 | usb_log_info("Device(%d): Requested to be brought online.", dev->address);
|
|---|
| 358 | return bus_device_online(dev);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | int hc_fun_offline(ddf_fun_t *fun)
|
|---|
| 362 | {
|
|---|
| 363 | assert(fun);
|
|---|
| 364 |
|
|---|
| 365 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 366 | assert(dev);
|
|---|
| 367 |
|
|---|
| 368 | usb_log_info("Device(%d): Requested to be taken offline.", dev->address);
|
|---|
| 369 | return bus_device_offline(dev);
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | /** Get max packet size for the control endpoint 0.
|
|---|
| 373 | *
|
|---|
| 374 | * For LS, HS, and SS devices this value is fixed. For FS devices we must fetch
|
|---|
| 375 | * the first 8B of the device descriptor to determine it.
|
|---|
| 376 | *
|
|---|
| 377 | * @return Max packet size for EP 0
|
|---|
| 378 | */
|
|---|
| 379 | int hcd_get_ep0_max_packet_size(uint16_t *mps, bus_t *bus, device_t *dev)
|
|---|
| 380 | {
|
|---|
| 381 | assert(mps);
|
|---|
| 382 |
|
|---|
| 383 | static const uint16_t mps_fixed [] = {
|
|---|
| 384 | [USB_SPEED_LOW] = 8,
|
|---|
| 385 | [USB_SPEED_HIGH] = 64,
|
|---|
| 386 | [USB_SPEED_SUPER] = 512,
|
|---|
| 387 | };
|
|---|
| 388 |
|
|---|
| 389 | if (dev->speed < ARRAY_SIZE(mps_fixed) && mps_fixed[dev->speed] != 0) {
|
|---|
| 390 | *mps = mps_fixed[dev->speed];
|
|---|
| 391 | return EOK;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | const usb_target_t control_ep = {{
|
|---|
| 395 | .address = dev->address,
|
|---|
| 396 | .endpoint = 0,
|
|---|
| 397 | }};
|
|---|
| 398 |
|
|---|
| 399 | usb_standard_device_descriptor_t desc = { 0 };
|
|---|
| 400 | const usb_device_request_setup_packet_t get_device_desc_8 =
|
|---|
| 401 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
|---|
| 402 |
|
|---|
| 403 | usb_log_debug("Requesting first 8B of device descriptor to determine MPS.");
|
|---|
| 404 | ssize_t got = bus_device_send_batch_sync(dev, control_ep, USB_DIRECTION_IN,
|
|---|
| 405 | (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
|---|
| 406 | "read first 8 bytes of dev descriptor");
|
|---|
| 407 |
|
|---|
| 408 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
|---|
| 409 | const int err = got < 0 ? got : EOVERFLOW;
|
|---|
| 410 | usb_log_error("Failed to get 8B of dev descr: %s.", str_error(err));
|
|---|
| 411 | return err;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | if (desc.descriptor_type != USB_DESCTYPE_DEVICE) {
|
|---|
| 415 | usb_log_error("The device responded with wrong device descriptor.");
|
|---|
| 416 | return EIO;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | uint16_t version = uint16_usb2host(desc.usb_spec_version);
|
|---|
| 420 | if (version < 0x0300) {
|
|---|
| 421 | /* USB 2 and below have MPS raw in the field */
|
|---|
| 422 | *mps = desc.max_packet_size;
|
|---|
| 423 | } else {
|
|---|
| 424 | /* USB 3 have MPS as an 2-based exponent */
|
|---|
| 425 | *mps = (1 << desc.max_packet_size);
|
|---|
| 426 | }
|
|---|
| 427 | return EOK;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | /**
|
|---|
| 431 | * Setup devices Transaction Translation.
|
|---|
| 432 | *
|
|---|
| 433 | * This applies for Low/Full speed devices under High speed hub only. Other
|
|---|
| 434 | * devices just inherit TT from the hub.
|
|---|
| 435 | *
|
|---|
| 436 | * Roothub must be handled specially.
|
|---|
| 437 | */
|
|---|
| 438 | void hcd_setup_device_tt(device_t *dev)
|
|---|
| 439 | {
|
|---|
| 440 | if (!dev->hub)
|
|---|
| 441 | return;
|
|---|
| 442 |
|
|---|
| 443 | if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
|
|---|
| 444 | /* For LS devices under HS hub */
|
|---|
| 445 | dev->tt.address = dev->hub->address;
|
|---|
| 446 | dev->tt.port = dev->port;
|
|---|
| 447 | }
|
|---|
| 448 | else {
|
|---|
| 449 | /* Inherit hub's TT */
|
|---|
| 450 | dev->tt = dev->hub->tt;
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | /** Check setup packet data for signs of toggle reset.
|
|---|
| 455 | *
|
|---|
| 456 | * @param[in] requst Setup requst data.
|
|---|
| 457 | *
|
|---|
| 458 | * @retval -1 No endpoints need reset.
|
|---|
| 459 | * @retval 0 All endpoints need reset.
|
|---|
| 460 | * @retval >0 Specified endpoint needs reset.
|
|---|
| 461 | *
|
|---|
| 462 | */
|
|---|
| 463 | toggle_reset_mode_t hcd_get_request_toggle_reset_mode(
|
|---|
| 464 | const usb_device_request_setup_packet_t *request)
|
|---|
| 465 | {
|
|---|
| 466 | assert(request);
|
|---|
| 467 | switch (request->request)
|
|---|
| 468 | {
|
|---|
| 469 | /* Clear Feature ENPOINT_STALL */
|
|---|
| 470 | case USB_DEVREQ_CLEAR_FEATURE: /*resets only cleared ep */
|
|---|
| 471 | /* 0x2 ( HOST to device | STANDART | TO ENPOINT) */
|
|---|
| 472 | if ((request->request_type == 0x2) &&
|
|---|
| 473 | (request->value == USB_FEATURE_ENDPOINT_HALT))
|
|---|
| 474 | return RESET_EP;
|
|---|
| 475 | break;
|
|---|
| 476 | case USB_DEVREQ_SET_CONFIGURATION:
|
|---|
| 477 | case USB_DEVREQ_SET_INTERFACE:
|
|---|
| 478 | /* Recipient must be device, this resets all endpoints,
|
|---|
| 479 | * In fact there should be no endpoints but EP 0 registered
|
|---|
| 480 | * as different interfaces use different endpoints,
|
|---|
| 481 | * unless you're changing configuration or alternative
|
|---|
| 482 | * interface of an already setup device. */
|
|---|
| 483 | if (!(request->request_type & SETUP_REQUEST_TYPE_DEVICE_TO_HOST))
|
|---|
| 484 | return RESET_ALL;
|
|---|
| 485 | break;
|
|---|
| 486 | default:
|
|---|
| 487 | break;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | return RESET_NONE;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 | /**
|
|---|
| 495 | * @}
|
|---|
| 496 | */
|
|---|