| 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 | /** @addtogroup drvusbohcihc
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI Host controller driver routines
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <libarch/ddi.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 | #include <usb/usb.h>
|
|---|
| 41 | #include <usb/hub.h>
|
|---|
| 42 | #include <usb/ddfiface.h>
|
|---|
| 43 | #include <usb/usbdevice.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "hc.h"
|
|---|
| 46 |
|
|---|
| 47 | static int dummy_reset(int foo, void *arg);
|
|---|
| 48 | static int interrupt_emulator(hc_t *instance);
|
|---|
| 49 | /*----------------------------------------------------------------------------*/
|
|---|
| 50 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
|---|
| 51 | uintptr_t regs, size_t reg_size, bool interrupts)
|
|---|
| 52 | {
|
|---|
| 53 | assert(instance);
|
|---|
| 54 | int ret = EOK;
|
|---|
| 55 |
|
|---|
| 56 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
|---|
| 57 | if (ret != EOK) {
|
|---|
| 58 | usb_log_error("Failed to gain access to device registers.\n");
|
|---|
| 59 | return ret;
|
|---|
| 60 | }
|
|---|
| 61 | instance->ddf_instance = fun;
|
|---|
| 62 | usb_device_keeper_init(&instance->manager);
|
|---|
| 63 |
|
|---|
| 64 | if (!interrupts) {
|
|---|
| 65 | instance->interrupt_emulator =
|
|---|
| 66 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
|---|
| 67 | fibril_add_ready(instance->interrupt_emulator);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | rh_init(&instance->rh, dev, instance->registers);
|
|---|
| 72 | /* TODO: implement */
|
|---|
| 73 | return EOK;
|
|---|
| 74 | }
|
|---|
| 75 | /*----------------------------------------------------------------------------*/
|
|---|
| 76 | int hc_register_hub(hc_t *instance)
|
|---|
| 77 | {
|
|---|
| 78 | async_usleep(1000000);
|
|---|
| 79 | #define CHECK_RET_RETURN(ret, msg...) \
|
|---|
| 80 | if (ret != EOK) { \
|
|---|
| 81 | usb_log_error(msg); \
|
|---|
| 82 | return ret; \
|
|---|
| 83 | } else (void)0
|
|---|
| 84 | assert(instance);
|
|---|
| 85 | assert(instance->ddf_instance);
|
|---|
| 86 | assert(instance->ddf_instance->handle);
|
|---|
| 87 | ddf_dev_t *dev = instance->rh.device;
|
|---|
| 88 | int ret = EOK;
|
|---|
| 89 |
|
|---|
| 90 | usb_hc_connection_t conn;
|
|---|
| 91 | ret =
|
|---|
| 92 | usb_hc_connection_initialize(&conn, instance->ddf_instance->handle);
|
|---|
| 93 | CHECK_RET_RETURN(ret, "Failed to initialize hc connection.\n");
|
|---|
| 94 |
|
|---|
| 95 | ret = usb_hc_connection_open(&conn);
|
|---|
| 96 | CHECK_RET_RETURN(ret, "Failed to open hc connection.\n");
|
|---|
| 97 |
|
|---|
| 98 | usb_address_t address;
|
|---|
| 99 | devman_handle_t handle;
|
|---|
| 100 | ret = usb_hc_new_device_wrapper(dev, &conn, USB_SPEED_FULL, dummy_reset,
|
|---|
| 101 | 0, instance, &address, &handle, NULL, NULL, NULL);
|
|---|
| 102 | if (ret != EOK) {
|
|---|
| 103 | usb_log_error("Failed to add rh device.\n");
|
|---|
| 104 | instance->rh.address = -1;
|
|---|
| 105 | return ret;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | ret = usb_hc_connection_close(&conn);
|
|---|
| 109 | CHECK_RET_RETURN(ret, "Failed to close hc connection.\n");
|
|---|
| 110 | return EOK;
|
|---|
| 111 | }
|
|---|
| 112 | /*----------------------------------------------------------------------------*/
|
|---|
| 113 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 114 | {
|
|---|
| 115 | assert(instance);
|
|---|
| 116 | assert(batch);
|
|---|
| 117 | if (batch->target.address == instance->rh.address) {
|
|---|
| 118 | return rh_request(&instance->rh, batch);
|
|---|
| 119 | }
|
|---|
| 120 | /* TODO: implement */
|
|---|
| 121 | return ENOTSUP;
|
|---|
| 122 | }
|
|---|
| 123 | /*----------------------------------------------------------------------------*/
|
|---|
| 124 | void hc_interrupt(hc_t *instance, uint32_t status)
|
|---|
| 125 | {
|
|---|
| 126 | assert(instance);
|
|---|
| 127 | if (status == 0)
|
|---|
| 128 | return;
|
|---|
| 129 | if (status & IS_RHSC)
|
|---|
| 130 | rh_interrupt(&instance->rh);
|
|---|
| 131 |
|
|---|
| 132 | /* TODO: Check for further interrupt causes */
|
|---|
| 133 | /* TODO: implement */
|
|---|
| 134 | }
|
|---|
| 135 | /*----------------------------------------------------------------------------*/
|
|---|
| 136 | static int dummy_reset(int foo, void *arg)
|
|---|
| 137 | {
|
|---|
| 138 | hc_t *hc = (hc_t*)arg;
|
|---|
| 139 | assert(hc);
|
|---|
| 140 | hc->rh.address = 0;
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|
| 144 | static int interrupt_emulator(hc_t *instance)
|
|---|
| 145 | {
|
|---|
| 146 | assert(instance);
|
|---|
| 147 | usb_log_info("Started interrupt emulator.\n");
|
|---|
| 148 | while (1) {
|
|---|
| 149 | uint32_t status = instance->registers->interrupt_status;
|
|---|
| 150 | instance->registers->interrupt_status = status;
|
|---|
| 151 | hc_interrupt(instance, status);
|
|---|
| 152 | async_usleep(1000);
|
|---|
| 153 | }
|
|---|
| 154 | return EOK;
|
|---|
| 155 | }
|
|---|
| 156 | /**
|
|---|
| 157 | * @}
|
|---|
| 158 | */
|
|---|