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 |
|
---|
29 | /** @addtogroup drvusbuhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief UHCI driver
|
---|
34 | */
|
---|
35 | #include <errno.h>
|
---|
36 | #include <str_error.h>
|
---|
37 | #include <ddf/interrupt.h>
|
---|
38 | #include <usb_iface.h>
|
---|
39 | #include <usb/ddfiface.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 |
|
---|
42 | #include "uhci.h"
|
---|
43 |
|
---|
44 | #include "res.h"
|
---|
45 | #include "hc.h"
|
---|
46 | #include "root_hub.h"
|
---|
47 |
|
---|
48 | /** Structure representing both functions of UHCI hc, USB host controller
|
---|
49 | * and USB root hub */
|
---|
50 | typedef struct uhci {
|
---|
51 | /** Pointer to DDF representation of UHCI host controller */
|
---|
52 | ddf_fun_t *hc_fun;
|
---|
53 | /** Pointer to DDF representation of UHCI root hub */
|
---|
54 | ddf_fun_t *rh_fun;
|
---|
55 |
|
---|
56 | /** Internal driver's representation of UHCI host controller */
|
---|
57 | hc_t hc;
|
---|
58 | /** Internal driver's representation of UHCI root hub */
|
---|
59 | rh_t rh;
|
---|
60 | } uhci_t;
|
---|
61 |
|
---|
62 | static inline uhci_t * dev_to_uhci(const ddf_dev_t *dev)
|
---|
63 | {
|
---|
64 | assert(dev);
|
---|
65 | return dev->driver_data;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
69 | *
|
---|
70 | * @param[in] dev DDF instance of the device to use.
|
---|
71 | * @param[in] iid (Unused).
|
---|
72 | * @param[in] call Pointer to the call from kernel.
|
---|
73 | */
|
---|
74 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
75 | {
|
---|
76 | assert(dev);
|
---|
77 | uhci_t *uhci = dev_to_uhci(dev);
|
---|
78 | if (!uhci) {
|
---|
79 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
83 | hc_interrupt(&uhci->hc, status);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Operations supported by the HC driver */
|
---|
87 | static ddf_dev_ops_t hc_ops = {
|
---|
88 | .interfaces[USBHC_DEV_IFACE] = &hcd_iface, /* see iface.h/c */
|
---|
89 | };
|
---|
90 |
|
---|
91 | /** Gets handle of the respective hc.
|
---|
92 | *
|
---|
93 | * @param[in] fun DDF function of uhci device.
|
---|
94 | * @param[out] handle Host cotnroller handle.
|
---|
95 | * @return Error code.
|
---|
96 | */
|
---|
97 | static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
98 | {
|
---|
99 | assert(fun);
|
---|
100 | ddf_fun_t *hc_fun = dev_to_uhci(fun->dev)->hc_fun;
|
---|
101 | assert(hc_fun);
|
---|
102 |
|
---|
103 | if (handle != NULL)
|
---|
104 | *handle = hc_fun->handle;
|
---|
105 | return EOK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** USB interface implementation used by RH */
|
---|
109 | static usb_iface_t usb_iface = {
|
---|
110 | .get_hc_handle = usb_iface_get_hc_handle,
|
---|
111 | };
|
---|
112 |
|
---|
113 | /** Get root hub hw resources (I/O registers).
|
---|
114 | *
|
---|
115 | * @param[in] fun Root hub function.
|
---|
116 | * @return Pointer to the resource list used by the root hub.
|
---|
117 | */
|
---|
118 | static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
|
---|
119 | {
|
---|
120 | assert(fun);
|
---|
121 | rh_t *rh = fun->driver_data;
|
---|
122 | assert(rh);
|
---|
123 | return &rh->resource_list;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Interface to provide the root hub driver with hw info */
|
---|
127 | static hw_res_ops_t hw_res_iface = {
|
---|
128 | .get_resource_list = get_resource_list,
|
---|
129 | .enable_interrupt = NULL,
|
---|
130 | };
|
---|
131 |
|
---|
132 | /** RH function support for uhci_rhd */
|
---|
133 | static ddf_dev_ops_t rh_ops = {
|
---|
134 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
135 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
136 | };
|
---|
137 |
|
---|
138 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
139 | *
|
---|
140 | * @param[in] device DDF instance of the device to use.
|
---|
141 | *
|
---|
142 | * This function does all the preparatory work for hc and rh drivers:
|
---|
143 | * - gets device's hw resources
|
---|
144 | * - disables UHCI legacy support (PCI config space)
|
---|
145 | * - attempts to enable interrupts
|
---|
146 | * - registers interrupt handler
|
---|
147 | */
|
---|
148 | int device_setup_uhci(ddf_dev_t *device)
|
---|
149 | {
|
---|
150 | if (!device)
|
---|
151 | return EBADMEM;
|
---|
152 |
|
---|
153 | uhci_t *instance = ddf_dev_data_alloc(device, sizeof(uhci_t));
|
---|
154 | if (instance == NULL) {
|
---|
155 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
156 | return ENOMEM;
|
---|
157 | }
|
---|
158 |
|
---|
159 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
160 | if (ret != EOK) { \
|
---|
161 | if (instance->hc_fun) \
|
---|
162 | instance->hc_fun->driver_data = NULL; \
|
---|
163 | ddf_fun_destroy(instance->hc_fun); \
|
---|
164 | if (instance->rh_fun) {\
|
---|
165 | instance->rh_fun->driver_data = NULL; \
|
---|
166 | ddf_fun_destroy(instance->rh_fun); \
|
---|
167 | } \
|
---|
168 | usb_log_error(message); \
|
---|
169 | return ret; \
|
---|
170 | } else (void)0
|
---|
171 |
|
---|
172 | instance->rh_fun = NULL;
|
---|
173 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
|
---|
174 | int ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
|
---|
175 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI HC function.\n");
|
---|
176 | instance->hc_fun->ops = &hc_ops;
|
---|
177 | instance->hc_fun->driver_data = &instance->hc.generic;
|
---|
178 |
|
---|
179 | instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
|
---|
180 | ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
|
---|
181 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI RH function.\n");
|
---|
182 | instance->rh_fun->ops = &rh_ops;
|
---|
183 | instance->rh_fun->driver_data = &instance->rh;
|
---|
184 |
|
---|
185 | uintptr_t reg_base = 0;
|
---|
186 | size_t reg_size = 0;
|
---|
187 | int irq = 0;
|
---|
188 |
|
---|
189 | ret = get_my_registers(device, ®_base, ®_size, &irq);
|
---|
190 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
191 | "Failed to get I/O addresses for %" PRIun ": %s.\n",
|
---|
192 | device->handle, str_error(ret));
|
---|
193 | usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
|
---|
194 | (void *) reg_base, reg_size, irq);
|
---|
195 |
|
---|
196 | ret = disable_legacy(device);
|
---|
197 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
198 | "Failed to disable legacy USB: %s.\n", str_error(ret));
|
---|
199 |
|
---|
200 | const size_t ranges_count = hc_irq_pio_range_count();
|
---|
201 | const size_t cmds_count = hc_irq_cmd_count();
|
---|
202 | irq_pio_range_t irq_ranges[ranges_count];
|
---|
203 | irq_cmd_t irq_cmds[cmds_count];
|
---|
204 | ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
|
---|
205 | sizeof(irq_cmds), reg_base, reg_size);
|
---|
206 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
207 | "Failed to generate IRQ commands: %s.\n", str_error(ret));
|
---|
208 |
|
---|
209 | irq_code_t irq_code = {
|
---|
210 | .rangecount = ranges_count,
|
---|
211 | .ranges = irq_ranges,
|
---|
212 | .cmdcount = cmds_count,
|
---|
213 | .cmds = irq_cmds
|
---|
214 | };
|
---|
215 |
|
---|
216 | /* Register handler to avoid interrupt lockup */
|
---|
217 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
218 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
219 | "Failed to register interrupt handler: %s.\n", str_error(ret));
|
---|
220 |
|
---|
221 | bool interrupts = false;
|
---|
222 | ret = enable_interrupts(device);
|
---|
223 | if (ret != EOK) {
|
---|
224 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
225 | " Falling back to polling.\n", str_error(ret));
|
---|
226 | } else {
|
---|
227 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
228 | interrupts = true;
|
---|
229 | }
|
---|
230 |
|
---|
231 | ret = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
|
---|
232 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
233 | "Failed to init uhci_hcd: %s.\n", str_error(ret));
|
---|
234 |
|
---|
235 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
236 | if (ret != EOK) { \
|
---|
237 | hc_fini(&instance->hc); \
|
---|
238 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
239 | return ret; \
|
---|
240 | } else (void)0
|
---|
241 |
|
---|
242 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
243 | CHECK_RET_FINI_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
|
---|
244 | str_error(ret));
|
---|
245 |
|
---|
246 | ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
|
---|
247 | CHECK_RET_FINI_RETURN(ret,
|
---|
248 | "Failed to add UHCI to HC class: %s.\n", str_error(ret));
|
---|
249 |
|
---|
250 | ret = rh_init(&instance->rh, instance->rh_fun,
|
---|
251 | (uintptr_t)instance->hc.registers + 0x10, 4);
|
---|
252 | CHECK_RET_FINI_RETURN(ret,
|
---|
253 | "Failed to setup UHCI root hub: %s.\n", str_error(ret));
|
---|
254 |
|
---|
255 | ret = ddf_fun_bind(instance->rh_fun);
|
---|
256 | CHECK_RET_FINI_RETURN(ret,
|
---|
257 | "Failed to register UHCI root hub: %s.\n", str_error(ret));
|
---|
258 |
|
---|
259 | return EOK;
|
---|
260 | #undef CHECK_RET_FINI_RETURN
|
---|
261 | }
|
---|
262 | /**
|
---|
263 | * @}
|
---|
264 | */
|
---|