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