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