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