| 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 usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <assert.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include <str_error.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <ops/hw_res.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <usb_iface.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "root_hub.h"
|
|---|
| 44 | #include "uhci.h"
|
|---|
| 45 |
|
|---|
| 46 | /*----------------------------------------------------------------------------*/
|
|---|
| 47 | static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
|
|---|
| 48 | devman_handle_t *handle)
|
|---|
| 49 | {
|
|---|
| 50 | ddf_fun_t *hc_fun = root_hub_fun->driver_data;
|
|---|
| 51 | assert(hc_fun != NULL);
|
|---|
| 52 |
|
|---|
| 53 | *handle = hc_fun->handle;
|
|---|
| 54 |
|
|---|
| 55 | return EOK;
|
|---|
| 56 | }
|
|---|
| 57 | /*----------------------------------------------------------------------------*/
|
|---|
| 58 | static int usb_iface_get_address_rh_impl(ddf_fun_t *fun, devman_handle_t handle,
|
|---|
| 59 | usb_address_t *address)
|
|---|
| 60 | {
|
|---|
| 61 | assert(fun);
|
|---|
| 62 | ddf_fun_t *hc_fun = fun->driver_data;
|
|---|
| 63 | assert(hc_fun);
|
|---|
| 64 | uhci_t *hc = fun_to_uhci(hc_fun);
|
|---|
| 65 | assert(hc);
|
|---|
| 66 |
|
|---|
| 67 | usb_address_t addr = device_keeper_find(&hc->device_manager,
|
|---|
| 68 | handle);
|
|---|
| 69 | if (addr < 0) {
|
|---|
| 70 | return addr;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (address != NULL) {
|
|---|
| 74 | *address = addr;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | return EOK;
|
|---|
| 78 | }
|
|---|
| 79 | /*----------------------------------------------------------------------------*/
|
|---|
| 80 | usb_iface_t usb_iface_root_hub_fun_impl = {
|
|---|
| 81 | .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
|
|---|
| 82 | .get_address = usb_iface_get_address_rh_impl
|
|---|
| 83 | };
|
|---|
| 84 | /*----------------------------------------------------------------------------*/
|
|---|
| 85 | static hw_resource_list_t *get_resource_list(ddf_fun_t *dev)
|
|---|
| 86 | {
|
|---|
| 87 | assert(dev);
|
|---|
| 88 | ddf_fun_t *hc_ddf_instance = dev->driver_data;
|
|---|
| 89 | assert(hc_ddf_instance);
|
|---|
| 90 | uhci_t *hc = hc_ddf_instance->driver_data;
|
|---|
| 91 | assert(hc);
|
|---|
| 92 |
|
|---|
| 93 | //TODO: fix memory leak
|
|---|
| 94 | hw_resource_list_t *resource_list = malloc(sizeof(hw_resource_list_t));
|
|---|
| 95 | assert(resource_list);
|
|---|
| 96 | resource_list->count = 1;
|
|---|
| 97 | resource_list->resources = malloc(sizeof(hw_resource_t));
|
|---|
| 98 | assert(resource_list->resources);
|
|---|
| 99 | resource_list->resources[0].type = IO_RANGE;
|
|---|
| 100 | resource_list->resources[0].res.io_range.address =
|
|---|
| 101 | ((uintptr_t)hc->registers) + 0x10; // see UHCI design guide
|
|---|
| 102 | resource_list->resources[0].res.io_range.size = 4;
|
|---|
| 103 | resource_list->resources[0].res.io_range.endianness = LITTLE_ENDIAN;
|
|---|
| 104 |
|
|---|
| 105 | return resource_list;
|
|---|
| 106 | }
|
|---|
| 107 | /*----------------------------------------------------------------------------*/
|
|---|
| 108 | static hw_res_ops_t hw_res_iface = {
|
|---|
| 109 | .get_resource_list = get_resource_list,
|
|---|
| 110 | .enable_interrupt = NULL
|
|---|
| 111 | };
|
|---|
| 112 | /*----------------------------------------------------------------------------*/
|
|---|
| 113 | static ddf_dev_ops_t root_hub_ops = {
|
|---|
| 114 | .interfaces[USB_DEV_IFACE] = &usb_iface_root_hub_fun_impl,
|
|---|
| 115 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
|---|
| 116 | };
|
|---|
| 117 | /*----------------------------------------------------------------------------*/
|
|---|
| 118 | int setup_root_hub(ddf_fun_t **fun, ddf_dev_t *hc)
|
|---|
| 119 | {
|
|---|
| 120 | assert(fun);
|
|---|
| 121 | assert(hc);
|
|---|
| 122 | int ret;
|
|---|
| 123 |
|
|---|
| 124 | ddf_fun_t *hub = ddf_fun_create(hc, fun_inner, "root-hub");
|
|---|
| 125 | if (!hub) {
|
|---|
| 126 | usb_log_error("Failed to create root hub device structure.\n");
|
|---|
| 127 | return ENOMEM;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | char *match_str;
|
|---|
| 131 | ret = asprintf(&match_str, "usb&uhci&root-hub");
|
|---|
| 132 | if (ret < 0) {
|
|---|
| 133 | usb_log_error("Failed to create root hub match string.\n");
|
|---|
| 134 | ddf_fun_destroy(hub);
|
|---|
| 135 | return ENOMEM;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | ret = ddf_fun_add_match_id(hub, match_str, 100);
|
|---|
| 139 | if (ret != EOK) {
|
|---|
| 140 | usb_log_error("Failed(%d) to add root hub match id: %s\n",
|
|---|
| 141 | ret, str_error(ret));
|
|---|
| 142 | ddf_fun_destroy(hub);
|
|---|
| 143 | return ret;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | hub->ops = &root_hub_ops;
|
|---|
| 147 |
|
|---|
| 148 | *fun = hub;
|
|---|
| 149 | return EOK;
|
|---|
| 150 | }
|
|---|
| 151 | /**
|
|---|
| 152 | * @}
|
|---|
| 153 | */
|
|---|