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 | /** Gets handle of the respective hc (parent device).
|
---|
48 | *
|
---|
49 | * @param[in] root_hub_fun Root hub function seeking hc handle.
|
---|
50 | * @param[out] handle Place to write the handle.
|
---|
51 | * @return Error code.
|
---|
52 | */
|
---|
53 | static int usb_iface_get_hc_handle_rh_impl(
|
---|
54 | ddf_fun_t *root_hub_fun, devman_handle_t *handle)
|
---|
55 | {
|
---|
56 | /* TODO: Can't this use parent pointer? */
|
---|
57 | ddf_fun_t *hc_fun = root_hub_fun->driver_data;
|
---|
58 | assert(hc_fun != NULL);
|
---|
59 |
|
---|
60 | *handle = hc_fun->handle;
|
---|
61 |
|
---|
62 | return EOK;
|
---|
63 | }
|
---|
64 | /*----------------------------------------------------------------------------*/
|
---|
65 | /** Gets USB address of the calling device.
|
---|
66 | *
|
---|
67 | * @param[in] fun Root hub function.
|
---|
68 | * @param[in] handle Handle of the device seeking address.
|
---|
69 | * @param[out] address Place to store found address.
|
---|
70 | * @return Error code.
|
---|
71 | */
|
---|
72 | static int usb_iface_get_address_rh_impl(
|
---|
73 | ddf_fun_t *fun, devman_handle_t handle, usb_address_t *address)
|
---|
74 | {
|
---|
75 | /* TODO: What does this do? Is it neccessary? Can't it use implemented
|
---|
76 | * hc function?*/
|
---|
77 | assert(fun);
|
---|
78 | ddf_fun_t *hc_fun = fun->driver_data;
|
---|
79 | assert(hc_fun);
|
---|
80 | uhci_t *hc = fun_to_uhci(hc_fun);
|
---|
81 | assert(hc);
|
---|
82 |
|
---|
83 | usb_address_t addr = device_keeper_find(&hc->device_manager, handle);
|
---|
84 | if (addr < 0) {
|
---|
85 | return addr;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (address != NULL) {
|
---|
89 | *address = addr;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return EOK;
|
---|
93 | }
|
---|
94 | /*----------------------------------------------------------------------------*/
|
---|
95 | usb_iface_t usb_iface_root_hub_fun_impl = {
|
---|
96 | .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
|
---|
97 | .get_address = usb_iface_get_address_rh_impl
|
---|
98 | };
|
---|
99 | /*----------------------------------------------------------------------------*/
|
---|
100 | /** Gets root hub hw resources.
|
---|
101 | *
|
---|
102 | * @param[in] fun Root hub function.
|
---|
103 | * @return Pointer to the resource list used by the root hub.
|
---|
104 | */
|
---|
105 | static hw_resource_list_t *get_resource_list(ddf_fun_t *dev)
|
---|
106 | {
|
---|
107 | assert(dev);
|
---|
108 | ddf_fun_t *hc_ddf_instance = dev->driver_data;
|
---|
109 | assert(hc_ddf_instance);
|
---|
110 | uhci_t *hc = hc_ddf_instance->driver_data;
|
---|
111 | assert(hc);
|
---|
112 |
|
---|
113 | /* TODO: fix memory leak */
|
---|
114 | hw_resource_list_t *resource_list = malloc(sizeof(hw_resource_list_t));
|
---|
115 | assert(resource_list);
|
---|
116 | resource_list->count = 1;
|
---|
117 | resource_list->resources = malloc(sizeof(hw_resource_t));
|
---|
118 | assert(resource_list->resources);
|
---|
119 | resource_list->resources[0].type = IO_RANGE;
|
---|
120 | resource_list->resources[0].res.io_range.address =
|
---|
121 | ((uintptr_t)hc->registers) + 0x10; // see UHCI design guide
|
---|
122 | resource_list->resources[0].res.io_range.size = 4;
|
---|
123 | resource_list->resources[0].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
124 |
|
---|
125 | return resource_list;
|
---|
126 | }
|
---|
127 | /*----------------------------------------------------------------------------*/
|
---|
128 | static hw_res_ops_t hw_res_iface = {
|
---|
129 | .get_resource_list = get_resource_list,
|
---|
130 | .enable_interrupt = NULL
|
---|
131 | };
|
---|
132 | /*----------------------------------------------------------------------------*/
|
---|
133 | static ddf_dev_ops_t root_hub_ops = {
|
---|
134 | .interfaces[USB_DEV_IFACE] = &usb_iface_root_hub_fun_impl,
|
---|
135 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
136 | };
|
---|
137 | /*----------------------------------------------------------------------------*/
|
---|
138 | int setup_root_hub(ddf_fun_t **fun, ddf_dev_t *hc)
|
---|
139 | {
|
---|
140 | assert(fun);
|
---|
141 | assert(hc);
|
---|
142 | int ret;
|
---|
143 |
|
---|
144 | ddf_fun_t *hub = ddf_fun_create(hc, fun_inner, "root-hub");
|
---|
145 | if (!hub) {
|
---|
146 | usb_log_error("Failed to create root hub device structure.\n");
|
---|
147 | return ENOMEM;
|
---|
148 | }
|
---|
149 |
|
---|
150 | char *match_str;
|
---|
151 | ret = asprintf(&match_str, "usb&uhci&root-hub");
|
---|
152 | if (ret < 0) {
|
---|
153 | usb_log_error("Failed to create root hub match string.\n");
|
---|
154 | ddf_fun_destroy(hub);
|
---|
155 | return ENOMEM;
|
---|
156 | }
|
---|
157 |
|
---|
158 | ret = ddf_fun_add_match_id(hub, match_str, 100);
|
---|
159 | if (ret != EOK) {
|
---|
160 | usb_log_error("Failed(%d) to add root hub match id: %s\n",
|
---|
161 | ret, str_error(ret));
|
---|
162 | ddf_fun_destroy(hub);
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 |
|
---|
166 | hub->ops = &root_hub_ops;
|
---|
167 |
|
---|
168 | *fun = hub;
|
---|
169 | return EOK;
|
---|
170 | }
|
---|
171 | /**
|
---|
172 | * @}
|
---|
173 | */
|
---|