| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky, 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 usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <ddf/driver.h>
|
|---|
| 35 | #include <devman.h>
|
|---|
| 36 | #include <device/hw_res.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <usb_iface.h>
|
|---|
| 39 | #include <usb/ddfiface.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | #include "root_hub.h"
|
|---|
| 45 |
|
|---|
| 46 | #define NAME "uhci-rhd"
|
|---|
| 47 | static int hc_get_my_registers(ddf_dev_t *dev,
|
|---|
| 48 | uintptr_t *io_reg_address, size_t *io_reg_size);
|
|---|
| 49 | /*----------------------------------------------------------------------------*/
|
|---|
| 50 | static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
|---|
| 51 | {
|
|---|
| 52 | assert(fun);
|
|---|
| 53 | assert(fun->driver_data);
|
|---|
| 54 | assert(handle);
|
|---|
| 55 |
|
|---|
| 56 | *handle = ((uhci_root_hub_t*)fun->driver_data)->hc_handle;
|
|---|
| 57 |
|
|---|
| 58 | return EOK;
|
|---|
| 59 | }
|
|---|
| 60 | /*----------------------------------------------------------------------------*/
|
|---|
| 61 | static usb_iface_t uhci_rh_usb_iface = {
|
|---|
| 62 | .get_hc_handle = usb_iface_get_hc_handle,
|
|---|
| 63 | .get_address = usb_iface_get_address_hub_impl
|
|---|
| 64 | };
|
|---|
| 65 | /*----------------------------------------------------------------------------*/
|
|---|
| 66 | static ddf_dev_ops_t uhci_rh_ops = {
|
|---|
| 67 | .interfaces[USB_DEV_IFACE] = &uhci_rh_usb_iface,
|
|---|
| 68 | };
|
|---|
| 69 | /*----------------------------------------------------------------------------*/
|
|---|
| 70 | /** Initializes a new ddf driver instance of UHCI root hub.
|
|---|
| 71 | *
|
|---|
| 72 | * @param[in] device DDF instance of the device to initialize.
|
|---|
| 73 | * @return Error code.
|
|---|
| 74 | */
|
|---|
| 75 | static int uhci_rh_add_device(ddf_dev_t *device)
|
|---|
| 76 | {
|
|---|
| 77 | if (!device)
|
|---|
| 78 | return ENOTSUP;
|
|---|
| 79 |
|
|---|
| 80 | usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle);
|
|---|
| 81 |
|
|---|
| 82 | //device->ops = &uhci_rh_ops;
|
|---|
| 83 | (void) uhci_rh_ops;
|
|---|
| 84 |
|
|---|
| 85 | uhci_root_hub_t *rh = malloc(sizeof(uhci_root_hub_t));
|
|---|
| 86 | if (!rh) {
|
|---|
| 87 | usb_log_error("Failed to allocate memory for driver instance.\n");
|
|---|
| 88 | return ENOMEM;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | uintptr_t io_regs = 0;
|
|---|
| 92 | size_t io_size = 0;
|
|---|
| 93 |
|
|---|
| 94 | int ret = hc_get_my_registers(device, &io_regs, &io_size);
|
|---|
| 95 | assert(ret == EOK);
|
|---|
| 96 |
|
|---|
| 97 | /* TODO: verify values from hc */
|
|---|
| 98 | usb_log_info("I/O regs at 0x%X (size %zu).\n", io_regs, io_size);
|
|---|
| 99 | ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
|
|---|
| 100 | if (ret != EOK) {
|
|---|
| 101 | usb_log_error("Failed(%d) to initialize driver instance.\n", ret);
|
|---|
| 102 | free(rh);
|
|---|
| 103 | return ret;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | device->driver_data = rh;
|
|---|
| 107 | usb_log_info("Sucessfully initialized driver instance for device:%d.\n",
|
|---|
| 108 | device->handle);
|
|---|
| 109 | return EOK;
|
|---|
| 110 | }
|
|---|
| 111 | /*----------------------------------------------------------------------------*/
|
|---|
| 112 | static driver_ops_t uhci_rh_driver_ops = {
|
|---|
| 113 | .add_device = uhci_rh_add_device,
|
|---|
| 114 | };
|
|---|
| 115 | /*----------------------------------------------------------------------------*/
|
|---|
| 116 | static driver_t uhci_rh_driver = {
|
|---|
| 117 | .name = NAME,
|
|---|
| 118 | .driver_ops = &uhci_rh_driver_ops
|
|---|
| 119 | };
|
|---|
| 120 | /*----------------------------------------------------------------------------*/
|
|---|
| 121 | /** Initializes global driver structures (NONE).
|
|---|
| 122 | *
|
|---|
| 123 | * @param[in] argc Nmber of arguments in argv vector (ignored).
|
|---|
| 124 | * @param[in] argv Cmdline argument vector (ignored).
|
|---|
| 125 | * @return Error code.
|
|---|
| 126 | *
|
|---|
| 127 | * Driver debug level is set here.
|
|---|
| 128 | */
|
|---|
| 129 | int main(int argc, char *argv[])
|
|---|
| 130 | {
|
|---|
| 131 | usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
|
|---|
| 132 | return ddf_driver_main(&uhci_rh_driver);
|
|---|
| 133 | }
|
|---|
| 134 | /*----------------------------------------------------------------------------*/
|
|---|
| 135 | /** Get address of I/O registers.
|
|---|
| 136 | *
|
|---|
| 137 | * @param[in] dev Device asking for the addresses.
|
|---|
| 138 | * @param[out] io_reg_address Base address of the memory range.
|
|---|
| 139 | * @param[out] io_reg_size Size of the memory range.
|
|---|
| 140 | * @return Error code.
|
|---|
| 141 | */
|
|---|
| 142 | int hc_get_my_registers(
|
|---|
| 143 | ddf_dev_t *dev, uintptr_t *io_reg_address, size_t *io_reg_size)
|
|---|
| 144 | {
|
|---|
| 145 | assert(dev != NULL);
|
|---|
| 146 |
|
|---|
| 147 | int parent_phone = devman_parent_device_connect(dev->handle,
|
|---|
| 148 | IPC_FLAG_BLOCKING);
|
|---|
| 149 | if (parent_phone < 0) {
|
|---|
| 150 | return parent_phone;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | int rc;
|
|---|
| 154 |
|
|---|
| 155 | hw_resource_list_t hw_resources;
|
|---|
| 156 | rc = hw_res_get_resource_list(parent_phone, &hw_resources);
|
|---|
| 157 | if (rc != EOK) {
|
|---|
| 158 | goto leave;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | uintptr_t io_address = 0;
|
|---|
| 162 | size_t io_size = 0;
|
|---|
| 163 | bool io_found = false;
|
|---|
| 164 |
|
|---|
| 165 | size_t i;
|
|---|
| 166 | for (i = 0; i < hw_resources.count; i++) {
|
|---|
| 167 | hw_resource_t *res = &hw_resources.resources[i];
|
|---|
| 168 | switch (res->type)
|
|---|
| 169 | {
|
|---|
| 170 | case IO_RANGE:
|
|---|
| 171 | io_address = (uintptr_t) res->res.io_range.address;
|
|---|
| 172 | io_size = res->res.io_range.size;
|
|---|
| 173 | io_found = true;
|
|---|
| 174 |
|
|---|
| 175 | default:
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (!io_found) {
|
|---|
| 181 | rc = ENOENT;
|
|---|
| 182 | goto leave;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (io_reg_address != NULL) {
|
|---|
| 186 | *io_reg_address = io_address;
|
|---|
| 187 | }
|
|---|
| 188 | if (io_reg_size != NULL) {
|
|---|
| 189 | *io_reg_size = io_size;
|
|---|
| 190 | }
|
|---|
| 191 | rc = EOK;
|
|---|
| 192 |
|
|---|
| 193 | leave:
|
|---|
| 194 | async_hangup(parent_phone);
|
|---|
| 195 | return rc;
|
|---|
| 196 | }
|
|---|
| 197 | /**
|
|---|
| 198 | * @}
|
|---|
| 199 | */
|
|---|