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