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 libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | * Host controller driver framework. Encapsulates DDF device of HC to an
|
---|
35 | * hc_device_t, which is passed to driver implementing hc_driver_t.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <ddf/interrupt.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <macros.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <usb/debug.h>
|
---|
45 | #include <usb/descriptor.h>
|
---|
46 | #include <usb/request.h>
|
---|
47 | #include <usb_iface.h>
|
---|
48 |
|
---|
49 | #include "bus.h"
|
---|
50 | #include "ddf_helpers.h"
|
---|
51 | #include "endpoint.h"
|
---|
52 | #include "usb_transfer_batch.h"
|
---|
53 |
|
---|
54 | #include "hcd.h"
|
---|
55 |
|
---|
56 | int hc_dev_add(ddf_dev_t *);
|
---|
57 | int hc_dev_remove(ddf_dev_t *);
|
---|
58 | int hc_dev_gone(ddf_dev_t *);
|
---|
59 | int hc_fun_online(ddf_fun_t *);
|
---|
60 | int hc_fun_offline(ddf_fun_t *);
|
---|
61 |
|
---|
62 | static driver_ops_t hc_driver_ops = {
|
---|
63 | .dev_add = hc_dev_add,
|
---|
64 | .dev_remove = hc_dev_remove,
|
---|
65 | .dev_gone = hc_dev_gone,
|
---|
66 | .fun_online = hc_fun_online,
|
---|
67 | .fun_offline = hc_fun_offline,
|
---|
68 | };
|
---|
69 |
|
---|
70 | static const hc_driver_t *hc_driver;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The main HC driver routine.
|
---|
74 | */
|
---|
75 | int hc_driver_main(const hc_driver_t *driver)
|
---|
76 | {
|
---|
77 | driver_t ddf_driver = {
|
---|
78 | .name = driver->name,
|
---|
79 | .driver_ops = &hc_driver_ops,
|
---|
80 | };
|
---|
81 |
|
---|
82 | /* Remember ops to call. */
|
---|
83 | hc_driver = driver;
|
---|
84 |
|
---|
85 | return ddf_driver_main(&ddf_driver);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * IRQ handling callback. Call the bus operation.
|
---|
90 | *
|
---|
91 | * Currently, there is a bus ops lookup to find the interrupt handler. So far,
|
---|
92 | * the mechanism is too flexible, as it allows different instances of HC to
|
---|
93 | * have different IRQ handlers, disallowing us to optimize the lookup here.
|
---|
94 | * TODO: Make the bus mechanism less flexible in irq handling and remove the
|
---|
95 | * lookup.
|
---|
96 | */
|
---|
97 | static void irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
98 | {
|
---|
99 | assert(dev);
|
---|
100 | hc_device_t *hcd = dev_to_hcd(dev);
|
---|
101 |
|
---|
102 | const bus_ops_t *ops = BUS_OPS_LOOKUP(hcd->bus->ops, interrupt);
|
---|
103 | assert(ops);
|
---|
104 |
|
---|
105 | const uint32_t status = IPC_GET_ARG1(*call);
|
---|
106 | ops->interrupt(hcd->bus, status);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Worker for the HW interrupt replacement fibril.
|
---|
111 | */
|
---|
112 | static int interrupt_polling(void *arg)
|
---|
113 | {
|
---|
114 | bus_t *bus = arg;
|
---|
115 | assert(bus);
|
---|
116 |
|
---|
117 | const bus_ops_t *interrupt_ops = BUS_OPS_LOOKUP(bus->ops, interrupt);
|
---|
118 | const bus_ops_t *status_ops = BUS_OPS_LOOKUP(bus->ops, status);
|
---|
119 | if (!interrupt_ops || !status_ops)
|
---|
120 | return ENOTSUP;
|
---|
121 |
|
---|
122 | uint32_t status = 0;
|
---|
123 | while (status_ops->status(bus, &status) == EOK) {
|
---|
124 | interrupt_ops->interrupt(bus, status);
|
---|
125 | status = 0;
|
---|
126 | /* We should wait 1 frame - 1ms here, but this polling is a
|
---|
127 | * lame crutch anyway so don't hog the system. 10ms is still
|
---|
128 | * good enough for emergency mode */
|
---|
129 | async_usleep(10000);
|
---|
130 | }
|
---|
131 | return EOK;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Clean the IRQ code bottom-half.
|
---|
136 | */
|
---|
137 | static inline void irq_code_clean(irq_code_t *code)
|
---|
138 | {
|
---|
139 | if (code) {
|
---|
140 | free(code->ranges);
|
---|
141 | free(code->cmds);
|
---|
142 | code->ranges = NULL;
|
---|
143 | code->cmds = NULL;
|
---|
144 | code->rangecount = 0;
|
---|
145 | code->cmdcount = 0;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Register an interrupt handler. If there is a callback to setup the bottom half,
|
---|
151 | * invoke it and register it. Register for notifications.
|
---|
152 | *
|
---|
153 | * If this method fails, a polling fibril is started instead.
|
---|
154 | *
|
---|
155 | * @param[in] hcd Host controller device.
|
---|
156 | * @param[in] hw_res Resources to be used.
|
---|
157 | *
|
---|
158 | * @return IRQ capability handle on success.
|
---|
159 | * @return Negative error code.
|
---|
160 | */
|
---|
161 | static int hcd_ddf_setup_interrupts(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
---|
162 | {
|
---|
163 | assert(hcd);
|
---|
164 | irq_code_t irq_code = {0};
|
---|
165 |
|
---|
166 | if (!hc_driver->irq_code_gen)
|
---|
167 | return ENOTSUP;
|
---|
168 |
|
---|
169 | const int irq = hc_driver->irq_code_gen(&irq_code, hcd, hw_res);
|
---|
170 | if (irq < 0) {
|
---|
171 | usb_log_error("Failed to generate IRQ code: %s.",
|
---|
172 | str_error(irq));
|
---|
173 | return irq;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* Register handler to avoid interrupt lockup */
|
---|
177 | const int irq_cap = register_interrupt_handler(hcd->ddf_dev, irq, irq_handler, &irq_code);
|
---|
178 | irq_code_clean(&irq_code);
|
---|
179 | if (irq_cap < 0) {
|
---|
180 | usb_log_error("Failed to register interrupt handler: %s.",
|
---|
181 | str_error(irq_cap));
|
---|
182 | return irq_cap;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* Enable interrupts */
|
---|
186 | int ret = hcd_ddf_enable_interrupt(hcd, irq);
|
---|
187 | if (ret != EOK) {
|
---|
188 | usb_log_error("Failed to enable interrupts: %s.",
|
---|
189 | str_error(ret));
|
---|
190 | unregister_interrupt_handler(hcd->ddf_dev, irq_cap);
|
---|
191 | return ret;
|
---|
192 | }
|
---|
193 | return irq_cap;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Initialize HC in memory of the driver.
|
---|
198 | *
|
---|
199 | * This function does all the preparatory work for hc and rh drivers:
|
---|
200 | * - gets device's hw resources
|
---|
201 | * - attempts to enable interrupts
|
---|
202 | * - registers interrupt handler
|
---|
203 | * - calls driver specific initialization
|
---|
204 | * - registers root hub
|
---|
205 | *
|
---|
206 | * @param device DDF instance of the device to use
|
---|
207 | * @return Error code
|
---|
208 | */
|
---|
209 | int hc_dev_add(ddf_dev_t *device)
|
---|
210 | {
|
---|
211 | int ret = EOK;
|
---|
212 | assert(device);
|
---|
213 |
|
---|
214 | if (!hc_driver->hc_add) {
|
---|
215 | usb_log_error("Driver '%s' does not support adding devices.", hc_driver->name);
|
---|
216 | return ENOTSUP;
|
---|
217 | }
|
---|
218 |
|
---|
219 | ret = hcd_ddf_setup_hc(device, hc_driver->hc_device_size);
|
---|
220 | if (ret != EOK) {
|
---|
221 | usb_log_error("Failed to setup HC device.");
|
---|
222 | return ret;
|
---|
223 | }
|
---|
224 |
|
---|
225 | hc_device_t *hcd = dev_to_hcd(device);
|
---|
226 |
|
---|
227 | hw_res_list_parsed_t hw_res;
|
---|
228 | ret = hcd_ddf_get_registers(hcd, &hw_res);
|
---|
229 | if (ret != EOK) {
|
---|
230 | usb_log_error("Failed to get register memory addresses "
|
---|
231 | "for `%s': %s.", ddf_dev_get_name(device),
|
---|
232 | str_error(ret));
|
---|
233 | goto err_hcd;
|
---|
234 | }
|
---|
235 |
|
---|
236 | ret = hc_driver->hc_add(hcd, &hw_res);
|
---|
237 | if (ret != EOK) {
|
---|
238 | usb_log_error("Failed to init HCD.");
|
---|
239 | goto err_hw_res;
|
---|
240 | }
|
---|
241 |
|
---|
242 | assert(hcd->bus);
|
---|
243 |
|
---|
244 | /* Setup interrupts */
|
---|
245 | hcd->irq_cap = hcd_ddf_setup_interrupts(hcd, &hw_res);
|
---|
246 | if (hcd->irq_cap >= 0) {
|
---|
247 | usb_log_debug("Hw interrupts enabled.");
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* Claim the device from BIOS */
|
---|
251 | if (hc_driver->claim)
|
---|
252 | ret = hc_driver->claim(hcd);
|
---|
253 | if (ret != EOK) {
|
---|
254 | usb_log_error("Failed to claim `%s' for `%s': %s",
|
---|
255 | ddf_dev_get_name(device), hc_driver->name, str_error(ret));
|
---|
256 | goto err_irq;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /* Start hw */
|
---|
260 | if (hc_driver->start)
|
---|
261 | ret = hc_driver->start(hcd);
|
---|
262 | if (ret != EOK) {
|
---|
263 | usb_log_error("Failed to start HCD: %s.", str_error(ret));
|
---|
264 | goto err_irq;
|
---|
265 | }
|
---|
266 |
|
---|
267 | const bus_ops_t *ops = BUS_OPS_LOOKUP(hcd->bus->ops, status);
|
---|
268 |
|
---|
269 | /* Need working irq replacement to setup root hub */
|
---|
270 | if (hcd->irq_cap < 0 && ops) {
|
---|
271 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd->bus);
|
---|
272 | if (!hcd->polling_fibril) {
|
---|
273 | usb_log_error("Failed to create polling fibril");
|
---|
274 | ret = ENOMEM;
|
---|
275 | goto err_started;
|
---|
276 | }
|
---|
277 | fibril_add_ready(hcd->polling_fibril);
|
---|
278 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
279 | " Falling back to polling.", str_error(hcd->irq_cap));
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Creating root hub registers a new USB device so HC
|
---|
284 | * needs to be ready at this time.
|
---|
285 | */
|
---|
286 | if (hc_driver->setup_root_hub)
|
---|
287 | ret = hc_driver->setup_root_hub(hcd);
|
---|
288 | if (ret != EOK) {
|
---|
289 | usb_log_error("Failed to setup HC root hub: %s.",
|
---|
290 | str_error(ret));
|
---|
291 | goto err_polling;
|
---|
292 | }
|
---|
293 |
|
---|
294 | usb_log_info("Controlling new `%s' device `%s'.",
|
---|
295 | hc_driver->name, ddf_dev_get_name(device));
|
---|
296 | return EOK;
|
---|
297 |
|
---|
298 | err_polling:
|
---|
299 | // TODO: Stop the polling fibril (refactor the interrupt_polling func)
|
---|
300 | //
|
---|
301 | err_started:
|
---|
302 | if (hc_driver->stop)
|
---|
303 | hc_driver->stop(hcd);
|
---|
304 | err_irq:
|
---|
305 | unregister_interrupt_handler(device, hcd->irq_cap);
|
---|
306 | if (hc_driver->hc_remove)
|
---|
307 | hc_driver->hc_remove(hcd);
|
---|
308 | err_hw_res:
|
---|
309 | hw_res_list_parsed_clean(&hw_res);
|
---|
310 | err_hcd:
|
---|
311 | hcd_ddf_clean_hc(hcd);
|
---|
312 | return ret;
|
---|
313 | }
|
---|
314 |
|
---|
315 | int hc_dev_remove(ddf_dev_t *dev)
|
---|
316 | {
|
---|
317 | int err;
|
---|
318 | hc_device_t *hcd = dev_to_hcd(dev);
|
---|
319 |
|
---|
320 | if (hc_driver->stop)
|
---|
321 | if ((err = hc_driver->stop(hcd)))
|
---|
322 | return err;
|
---|
323 |
|
---|
324 | unregister_interrupt_handler(dev, hcd->irq_cap);
|
---|
325 |
|
---|
326 | if (hc_driver->hc_remove)
|
---|
327 | if ((err = hc_driver->hc_remove(hcd)))
|
---|
328 | return err;
|
---|
329 |
|
---|
330 | hcd_ddf_clean_hc(hcd);
|
---|
331 |
|
---|
332 | // TODO probably not complete
|
---|
333 |
|
---|
334 | return EOK;
|
---|
335 | }
|
---|
336 |
|
---|
337 | int hc_dev_gone(ddf_dev_t *dev)
|
---|
338 | {
|
---|
339 | int err = ENOTSUP;
|
---|
340 | hc_device_t *hcd = dev_to_hcd(dev);
|
---|
341 |
|
---|
342 | if (hc_driver->hc_gone)
|
---|
343 | err = hc_driver->hc_gone(hcd);
|
---|
344 |
|
---|
345 | hcd_ddf_clean_hc(hcd);
|
---|
346 |
|
---|
347 | return err;
|
---|
348 | }
|
---|
349 |
|
---|
350 | int hc_fun_online(ddf_fun_t *fun)
|
---|
351 | {
|
---|
352 | assert(fun);
|
---|
353 |
|
---|
354 | device_t *dev = ddf_fun_data_get(fun);
|
---|
355 | assert(dev);
|
---|
356 |
|
---|
357 | usb_log_info("Device(%d): Requested to be brought online.", dev->address);
|
---|
358 | return bus_device_online(dev);
|
---|
359 | }
|
---|
360 |
|
---|
361 | int hc_fun_offline(ddf_fun_t *fun)
|
---|
362 | {
|
---|
363 | assert(fun);
|
---|
364 |
|
---|
365 | device_t *dev = ddf_fun_data_get(fun);
|
---|
366 | assert(dev);
|
---|
367 |
|
---|
368 | usb_log_info("Device(%d): Requested to be taken offline.", dev->address);
|
---|
369 | return bus_device_offline(dev);
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * @}
|
---|
375 | */
|
---|