| 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 drvusbuhcirh | 
|---|
| 29 | * @{ | 
|---|
| 30 | */ | 
|---|
| 31 | /** @file | 
|---|
| 32 | * @brief UHCI root hub initialization routines | 
|---|
| 33 | */ | 
|---|
| 34 | #include <ddf/driver.h> | 
|---|
| 35 | #include <devman.h> | 
|---|
| 36 | #include <device/hw_res.h> | 
|---|
| 37 | #include <errno.h> | 
|---|
| 38 | #include <str_error.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include <usb_iface.h> | 
|---|
| 41 | #include <usb/ddfiface.h> | 
|---|
| 42 | #include <usb/debug.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include "root_hub.h" | 
|---|
| 45 |  | 
|---|
| 46 | #define NAME "uhci-rhd" | 
|---|
| 47 |  | 
|---|
| 48 | static int hc_get_my_registers(const ddf_dev_t *dev, | 
|---|
| 49 | uintptr_t *io_reg_address, size_t *io_reg_size); | 
|---|
| 50 | /*----------------------------------------------------------------------------*/ | 
|---|
| 51 | static int uhci_rh_add_device(ddf_dev_t *device); | 
|---|
| 52 | /*----------------------------------------------------------------------------*/ | 
|---|
| 53 | static driver_ops_t uhci_rh_driver_ops = { | 
|---|
| 54 | .add_device = uhci_rh_add_device, | 
|---|
| 55 | }; | 
|---|
| 56 | /*----------------------------------------------------------------------------*/ | 
|---|
| 57 | static driver_t uhci_rh_driver = { | 
|---|
| 58 | .name = NAME, | 
|---|
| 59 | .driver_ops = &uhci_rh_driver_ops | 
|---|
| 60 | }; | 
|---|
| 61 | /*----------------------------------------------------------------------------*/ | 
|---|
| 62 | /** Initialize global driver structures (NONE). | 
|---|
| 63 | * | 
|---|
| 64 | * @param[in] argc Nmber of arguments in argv vector (ignored). | 
|---|
| 65 | * @param[in] argv Cmdline argument vector (ignored). | 
|---|
| 66 | * @return Error code. | 
|---|
| 67 | * | 
|---|
| 68 | * Driver debug level is set here. | 
|---|
| 69 | */ | 
|---|
| 70 | int main(int argc, char *argv[]) | 
|---|
| 71 | { | 
|---|
| 72 | printf(NAME ": HelenOS UHCI root hub driver.\n"); | 
|---|
| 73 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME); | 
|---|
| 74 | return ddf_driver_main(&uhci_rh_driver); | 
|---|
| 75 | } | 
|---|
| 76 | /*----------------------------------------------------------------------------*/ | 
|---|
| 77 | /** Initialize a new ddf driver instance of UHCI root hub. | 
|---|
| 78 | * | 
|---|
| 79 | * @param[in] device DDF instance of the device to initialize. | 
|---|
| 80 | * @return Error code. | 
|---|
| 81 | */ | 
|---|
| 82 | static int uhci_rh_add_device(ddf_dev_t *device) | 
|---|
| 83 | { | 
|---|
| 84 | if (!device) | 
|---|
| 85 | return EINVAL; | 
|---|
| 86 |  | 
|---|
| 87 | usb_log_debug2("uhci_rh_add_device(handle=%" PRIun ")\n", | 
|---|
| 88 | device->handle); | 
|---|
| 89 |  | 
|---|
| 90 | uintptr_t io_regs = 0; | 
|---|
| 91 | size_t io_size = 0; | 
|---|
| 92 | uhci_root_hub_t *rh = NULL; | 
|---|
| 93 | int ret = EOK; | 
|---|
| 94 |  | 
|---|
| 95 | #define CHECK_RET_FREE_RH_RETURN(ret, message...) \ | 
|---|
| 96 | if (ret != EOK) { \ | 
|---|
| 97 | usb_log_error(message); \ | 
|---|
| 98 | if (rh) \ | 
|---|
| 99 | free(rh); \ | 
|---|
| 100 | return ret; \ | 
|---|
| 101 | } else (void)0 | 
|---|
| 102 |  | 
|---|
| 103 | ret = hc_get_my_registers(device, &io_regs, &io_size); | 
|---|
| 104 | CHECK_RET_FREE_RH_RETURN(ret, | 
|---|
| 105 | "Failed to get registers from HC: %s.\n", str_error(ret)); | 
|---|
| 106 | usb_log_debug("I/O regs at %p (size %zuB).\n", | 
|---|
| 107 | (void *) io_regs, io_size); | 
|---|
| 108 |  | 
|---|
| 109 | rh = malloc(sizeof(uhci_root_hub_t)); | 
|---|
| 110 | ret = (rh == NULL) ? ENOMEM : EOK; | 
|---|
| 111 | CHECK_RET_FREE_RH_RETURN(ret, | 
|---|
| 112 | "Failed to allocate rh driver instance.\n"); | 
|---|
| 113 |  | 
|---|
| 114 | ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device); | 
|---|
| 115 | CHECK_RET_FREE_RH_RETURN(ret, | 
|---|
| 116 | "Failed(%d) to initialize rh driver instance: %s.\n", | 
|---|
| 117 | ret, str_error(ret)); | 
|---|
| 118 |  | 
|---|
| 119 | device->driver_data = rh; | 
|---|
| 120 | usb_log_info("Controlling root hub '%s' (%" PRIun ").\n", | 
|---|
| 121 | device->name, device->handle); | 
|---|
| 122 | return EOK; | 
|---|
| 123 | } | 
|---|
| 124 | /*----------------------------------------------------------------------------*/ | 
|---|
| 125 | /** Get address of I/O registers. | 
|---|
| 126 | * | 
|---|
| 127 | * @param[in] dev Device asking for the addresses. | 
|---|
| 128 | * @param[out] io_reg_address Base address of the memory range. | 
|---|
| 129 | * @param[out] io_reg_size Size of the memory range. | 
|---|
| 130 | * @return Error code. | 
|---|
| 131 | */ | 
|---|
| 132 | int hc_get_my_registers( | 
|---|
| 133 | const ddf_dev_t *dev, uintptr_t *io_reg_address, size_t *io_reg_size) | 
|---|
| 134 | { | 
|---|
| 135 | assert(dev); | 
|---|
| 136 |  | 
|---|
| 137 | const int parent_phone = devman_parent_device_connect(dev->handle, | 
|---|
| 138 | IPC_FLAG_BLOCKING); | 
|---|
| 139 | if (parent_phone < 0) { | 
|---|
| 140 | return parent_phone; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | hw_resource_list_t hw_resources; | 
|---|
| 144 | const int ret = hw_res_get_resource_list(parent_phone, &hw_resources); | 
|---|
| 145 | if (ret != EOK) { | 
|---|
| 146 | async_hangup(parent_phone); | 
|---|
| 147 | return ret; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | uintptr_t io_address = 0; | 
|---|
| 151 | size_t io_size = 0; | 
|---|
| 152 | bool io_found = false; | 
|---|
| 153 |  | 
|---|
| 154 | size_t i = 0; | 
|---|
| 155 | for (; i < hw_resources.count; i++) { | 
|---|
| 156 | hw_resource_t *res = &hw_resources.resources[i]; | 
|---|
| 157 | if (res->type == IO_RANGE) { | 
|---|
| 158 | io_address = res->res.io_range.address; | 
|---|
| 159 | io_size = res->res.io_range.size; | 
|---|
| 160 | io_found = true; | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | async_hangup(parent_phone); | 
|---|
| 164 |  | 
|---|
| 165 | if (!io_found) { | 
|---|
| 166 | return ENOENT; | 
|---|
| 167 | } | 
|---|
| 168 | if (io_reg_address != NULL) { | 
|---|
| 169 | *io_reg_address = io_address; | 
|---|
| 170 | } | 
|---|
| 171 | if (io_reg_size != NULL) { | 
|---|
| 172 | *io_reg_size = io_size; | 
|---|
| 173 | } | 
|---|
| 174 | return EOK; | 
|---|
| 175 | } | 
|---|
| 176 | /** | 
|---|
| 177 | * @} | 
|---|
| 178 | */ | 
|---|