1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbuhci
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief UHCI Host controller driver routines
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <ddi.h>
|
---|
41 | #include <device/hw_res_parsed.h>
|
---|
42 | #include <fibril.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <macros.h>
|
---|
45 | #include <mem.h>
|
---|
46 | #include <stdbool.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <stdint.h>
|
---|
49 | #include <str_error.h>
|
---|
50 |
|
---|
51 | #include <usb/debug.h>
|
---|
52 | #include <usb/usb.h>
|
---|
53 | #include <usb/host/utils/malloc32.h>
|
---|
54 | #include <usb/host/bandwidth.h>
|
---|
55 | #include <usb/host/utility.h>
|
---|
56 |
|
---|
57 | #include "uhci_batch.h"
|
---|
58 | #include "transfer_list.h"
|
---|
59 | #include "hc.h"
|
---|
60 |
|
---|
61 | #define UHCI_INTR_ALLOW_INTERRUPTS \
|
---|
62 | (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
|
---|
63 | #define UHCI_STATUS_USED_INTERRUPTS \
|
---|
64 | (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
|
---|
65 |
|
---|
66 | static const irq_pio_range_t uhci_irq_pio_ranges[] = {
|
---|
67 | {
|
---|
68 | .base = 0,
|
---|
69 | .size = sizeof(uhci_regs_t)
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | static const irq_cmd_t uhci_irq_commands[] = {
|
---|
74 | {
|
---|
75 | .cmd = CMD_PIO_READ_16,
|
---|
76 | .dstarg = 1,
|
---|
77 | .addr = NULL
|
---|
78 | },
|
---|
79 | {
|
---|
80 | .cmd = CMD_AND,
|
---|
81 | .srcarg = 1,
|
---|
82 | .dstarg = 2,
|
---|
83 | .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
|
---|
84 | },
|
---|
85 | {
|
---|
86 | .cmd = CMD_PREDICATE,
|
---|
87 | .srcarg = 2,
|
---|
88 | .value = 2
|
---|
89 | },
|
---|
90 | {
|
---|
91 | .cmd = CMD_PIO_WRITE_A_16,
|
---|
92 | .srcarg = 1,
|
---|
93 | .addr = NULL
|
---|
94 | },
|
---|
95 | {
|
---|
96 | .cmd = CMD_ACCEPT
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 | static void hc_init_hw(const hc_t *instance);
|
---|
101 | static errno_t hc_init_mem_structures(hc_t *instance);
|
---|
102 | static errno_t hc_init_transfer_lists(hc_t *instance);
|
---|
103 |
|
---|
104 | static errno_t hc_debug_checker(void *arg);
|
---|
105 |
|
---|
106 | /** Generate IRQ code.
|
---|
107 | * @param[out] code IRQ code structure.
|
---|
108 | * @param[in] hw_res Device's resources.
|
---|
109 | * @param[out] irq
|
---|
110 | *
|
---|
111 | * @return Error code.
|
---|
112 | */
|
---|
113 | errno_t hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res, int *irq)
|
---|
114 | {
|
---|
115 | assert(code);
|
---|
116 | assert(hw_res);
|
---|
117 |
|
---|
118 | if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
|
---|
119 | return EINVAL;
|
---|
120 | const addr_range_t regs = hw_res->io_ranges.ranges[0];
|
---|
121 |
|
---|
122 | if (RNGSZ(regs) < sizeof(uhci_regs_t))
|
---|
123 | return EOVERFLOW;
|
---|
124 |
|
---|
125 | code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
|
---|
126 | if (code->ranges == NULL)
|
---|
127 | return ENOMEM;
|
---|
128 |
|
---|
129 | code->cmds = malloc(sizeof(uhci_irq_commands));
|
---|
130 | if (code->cmds == NULL) {
|
---|
131 | free(code->ranges);
|
---|
132 | return ENOMEM;
|
---|
133 | }
|
---|
134 |
|
---|
135 | code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
|
---|
136 | code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
|
---|
137 |
|
---|
138 | memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
|
---|
139 | code->ranges[0].base = RNGABS(regs);
|
---|
140 |
|
---|
141 | memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
|
---|
142 | uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
|
---|
143 | code->cmds[0].addr = (void *)®isters->usbsts;
|
---|
144 | code->cmds[3].addr = (void *)®isters->usbsts;
|
---|
145 |
|
---|
146 | usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
|
---|
147 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
---|
148 |
|
---|
149 | *irq = hw_res->irqs.irqs[0];
|
---|
150 | return EOK;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Take action based on the interrupt cause.
|
---|
154 | *
|
---|
155 | * @param[in] hcd HCD structure to use.
|
---|
156 | * @param[in] status Value of the status register at the time of interrupt.
|
---|
157 | *
|
---|
158 | * Interrupt might indicate:
|
---|
159 | * - transaction completed, either by triggering IOC, SPD, or an error
|
---|
160 | * - some kind of device error
|
---|
161 | * - resume from suspend state (not implemented)
|
---|
162 | */
|
---|
163 | static void hc_interrupt(bus_t *bus, uint32_t status)
|
---|
164 | {
|
---|
165 | hc_t *instance = bus_to_hc(bus);
|
---|
166 |
|
---|
167 | /* Lower 2 bits are transaction error and transaction complete */
|
---|
168 | if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
|
---|
169 | transfer_list_check_finished(&instance->transfers_interrupt);
|
---|
170 | transfer_list_check_finished(&instance->transfers_control_slow);
|
---|
171 | transfer_list_check_finished(&instance->transfers_control_full);
|
---|
172 | transfer_list_check_finished(&instance->transfers_bulk_full);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* Resume interrupts are not supported */
|
---|
176 | if (status & UHCI_STATUS_RESUME) {
|
---|
177 | usb_log_error("Resume interrupt!");
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* Bits 4 and 5 indicate hc error */
|
---|
181 | if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
|
---|
182 | usb_log_error("UHCI hardware failure!.");
|
---|
183 | ++instance->hw_failures;
|
---|
184 | transfer_list_abort_all(&instance->transfers_interrupt);
|
---|
185 | transfer_list_abort_all(&instance->transfers_control_slow);
|
---|
186 | transfer_list_abort_all(&instance->transfers_control_full);
|
---|
187 | transfer_list_abort_all(&instance->transfers_bulk_full);
|
---|
188 |
|
---|
189 | if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
|
---|
190 | /* reinitialize hw, this triggers virtual disconnect */
|
---|
191 | hc_init_hw(instance);
|
---|
192 | } else {
|
---|
193 | usb_log_fatal("Too many UHCI hardware failures!.");
|
---|
194 | hc_gone(&instance->base);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** Initialize UHCI hc driver structure
|
---|
200 | *
|
---|
201 | * @param[in] instance Memory place to initialize.
|
---|
202 | * @param[in] regs Range of device's I/O control registers.
|
---|
203 | * @param[in] interrupts True if hw interrupts should be used.
|
---|
204 | * @return Error code.
|
---|
205 | * @note Should be called only once on any structure.
|
---|
206 | *
|
---|
207 | * Initializes memory structures, starts up hw, and launches debugger and
|
---|
208 | * interrupt fibrils.
|
---|
209 | */
|
---|
210 | errno_t hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
---|
211 | {
|
---|
212 | hc_t *instance = hcd_to_hc(hcd);
|
---|
213 | assert(hw_res);
|
---|
214 | if (hw_res->io_ranges.count != 1 ||
|
---|
215 | hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
|
---|
216 | return EINVAL;
|
---|
217 |
|
---|
218 | instance->hw_failures = 0;
|
---|
219 |
|
---|
220 | /* allow access to hc control registers */
|
---|
221 | errno_t ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
|
---|
222 | (void **) &instance->registers);
|
---|
223 | if (ret != EOK) {
|
---|
224 | usb_log_error("Failed to gain access to registers: %s.",
|
---|
225 | str_error(ret));
|
---|
226 | return ret;
|
---|
227 | }
|
---|
228 |
|
---|
229 | usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
|
---|
230 | hw_res->io_ranges.ranges[0].address.absolute,
|
---|
231 | hw_res->io_ranges.ranges[0].size);
|
---|
232 |
|
---|
233 | ret = hc_init_mem_structures(instance);
|
---|
234 | if (ret != EOK) {
|
---|
235 | usb_log_error("Failed to init UHCI memory structures: %s.",
|
---|
236 | str_error(ret));
|
---|
237 | // TODO: we should disable pio here
|
---|
238 | return ret;
|
---|
239 | }
|
---|
240 |
|
---|
241 | return EOK;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int hc_start(hc_device_t *hcd)
|
---|
245 | {
|
---|
246 | hc_t *instance = hcd_to_hc(hcd);
|
---|
247 | hc_init_hw(instance);
|
---|
248 | (void)hc_debug_checker;
|
---|
249 |
|
---|
250 | return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
|
---|
251 | }
|
---|
252 |
|
---|
253 | int hc_setup_roothub(hc_device_t *hcd)
|
---|
254 | {
|
---|
255 | return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Safely dispose host controller internal structures
|
---|
259 | *
|
---|
260 | * @param[in] instance Host controller structure to use.
|
---|
261 | */
|
---|
262 | int hc_gone(hc_device_t *instance)
|
---|
263 | {
|
---|
264 | assert(instance);
|
---|
265 | //TODO Implement
|
---|
266 | return ENOTSUP;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Initialize UHCI hc hw resources.
|
---|
270 | *
|
---|
271 | * @param[in] instance UHCI structure to use.
|
---|
272 | * For magic values see UHCI Design Guide
|
---|
273 | */
|
---|
274 | void hc_init_hw(const hc_t *instance)
|
---|
275 | {
|
---|
276 | assert(instance);
|
---|
277 | uhci_regs_t *registers = instance->registers;
|
---|
278 |
|
---|
279 | /* Reset everything, who knows what touched it before us */
|
---|
280 | pio_write_16(®isters->usbcmd, UHCI_CMD_GLOBAL_RESET);
|
---|
281 | fibril_usleep(50000); /* 50ms according to USB spec(root hub reset) */
|
---|
282 | pio_write_16(®isters->usbcmd, 0);
|
---|
283 |
|
---|
284 | /* Reset hc, all states and counters. Hope that hw is not broken */
|
---|
285 | pio_write_16(®isters->usbcmd, UHCI_CMD_HCRESET);
|
---|
286 | do {
|
---|
287 | fibril_usleep(10);
|
---|
288 | } while ((pio_read_16(®isters->usbcmd) & UHCI_CMD_HCRESET) != 0);
|
---|
289 |
|
---|
290 | /* Set frame to exactly 1ms */
|
---|
291 | pio_write_8(®isters->sofmod, 64);
|
---|
292 |
|
---|
293 | /* Set frame list pointer */
|
---|
294 | const uint32_t pa = addr_to_phys(instance->frame_list);
|
---|
295 | pio_write_32(®isters->flbaseadd, pa);
|
---|
296 |
|
---|
297 | if (cap_handle_valid(instance->base.irq_handle)) {
|
---|
298 | /* Enable all interrupts, but resume interrupt */
|
---|
299 | pio_write_16(&instance->registers->usbintr,
|
---|
300 | UHCI_INTR_ALLOW_INTERRUPTS);
|
---|
301 | }
|
---|
302 |
|
---|
303 | const uint16_t cmd = pio_read_16(®isters->usbcmd);
|
---|
304 | if (cmd != 0)
|
---|
305 | usb_log_warning("Previous command value: %x.", cmd);
|
---|
306 |
|
---|
307 | /* Start the hc with large(64B) packet FSBR */
|
---|
308 | pio_write_16(®isters->usbcmd,
|
---|
309 | UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
|
---|
310 | }
|
---|
311 |
|
---|
312 | static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
|
---|
313 | {
|
---|
314 | uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
|
---|
315 | return &batch->base;
|
---|
316 | }
|
---|
317 |
|
---|
318 | static void destroy_transfer_batch(usb_transfer_batch_t *batch)
|
---|
319 | {
|
---|
320 | uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
|
---|
321 | }
|
---|
322 |
|
---|
323 | static endpoint_t *endpoint_create(device_t *device, const usb_endpoint_descriptors_t *desc)
|
---|
324 | {
|
---|
325 | endpoint_t *ep = calloc(1, sizeof(uhci_endpoint_t));
|
---|
326 | if (ep)
|
---|
327 | endpoint_init(ep, device, desc);
|
---|
328 | return ep;
|
---|
329 | }
|
---|
330 |
|
---|
331 | static errno_t endpoint_register(endpoint_t *ep)
|
---|
332 | {
|
---|
333 | hc_t *const hc = bus_to_hc(endpoint_get_bus(ep));
|
---|
334 |
|
---|
335 | const errno_t err = usb2_bus_endpoint_register(&hc->bus_helper, ep);
|
---|
336 | if (err)
|
---|
337 | return err;
|
---|
338 |
|
---|
339 | transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
|
---|
340 | if (!list)
|
---|
341 | /*
|
---|
342 | * We don't support this combination (e.g. isochronous). Do not
|
---|
343 | * fail early, because that would block any device with these
|
---|
344 | * endpoints from connecting. Instead, make sure these transfers
|
---|
345 | * are denied soon enough with ENOTSUP not to fail on asserts.
|
---|
346 | */
|
---|
347 | return EOK;
|
---|
348 |
|
---|
349 | endpoint_set_online(ep, &list->guard);
|
---|
350 | return EOK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void endpoint_unregister(endpoint_t *ep)
|
---|
354 | {
|
---|
355 | hc_t *const hc = bus_to_hc(endpoint_get_bus(ep));
|
---|
356 | usb2_bus_endpoint_unregister(&hc->bus_helper, ep);
|
---|
357 |
|
---|
358 | // Check for the roothub, as it does not schedule into lists
|
---|
359 | if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
|
---|
360 | // FIXME: We shall check the roothub for active transfer. But
|
---|
361 | // as it is polling, there is no way to make it stop doing so.
|
---|
362 | // Return after rewriting uhci rh.
|
---|
363 | return;
|
---|
364 | }
|
---|
365 |
|
---|
366 | transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
|
---|
367 | if (!list)
|
---|
368 | /*
|
---|
369 | * We don't support this combination (e.g. isochronous),
|
---|
370 | * so no transfer can be active.
|
---|
371 | */
|
---|
372 | return;
|
---|
373 |
|
---|
374 | fibril_mutex_lock(&list->guard);
|
---|
375 |
|
---|
376 | endpoint_set_offline_locked(ep);
|
---|
377 | /* From now on, no other transfer will be scheduled. */
|
---|
378 |
|
---|
379 | if (!ep->active_batch) {
|
---|
380 | fibril_mutex_unlock(&list->guard);
|
---|
381 | return;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* First, offer the batch a short chance to be finished. */
|
---|
385 | endpoint_wait_timeout_locked(ep, 10000);
|
---|
386 |
|
---|
387 | if (!ep->active_batch) {
|
---|
388 | fibril_mutex_unlock(&list->guard);
|
---|
389 | return;
|
---|
390 | }
|
---|
391 |
|
---|
392 | uhci_transfer_batch_t *const batch =
|
---|
393 | uhci_transfer_batch_get(ep->active_batch);
|
---|
394 |
|
---|
395 | /* Remove the batch from the schedule to stop it from being finished. */
|
---|
396 | endpoint_deactivate_locked(ep);
|
---|
397 | transfer_list_remove_batch(list, batch);
|
---|
398 |
|
---|
399 | fibril_mutex_unlock(&list->guard);
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * We removed the batch from software schedule only, it's still possible
|
---|
403 | * that HC has it in its caches. Better wait a while before we release
|
---|
404 | * the buffers.
|
---|
405 | */
|
---|
406 | fibril_usleep(20000);
|
---|
407 | batch->base.error = EINTR;
|
---|
408 | batch->base.transferred_size = 0;
|
---|
409 | usb_transfer_batch_finish(&batch->base);
|
---|
410 | }
|
---|
411 |
|
---|
412 | static int device_enumerate(device_t *dev)
|
---|
413 | {
|
---|
414 | hc_t *const hc = bus_to_hc(dev->bus);
|
---|
415 | return usb2_bus_device_enumerate(&hc->bus_helper, dev);
|
---|
416 | }
|
---|
417 |
|
---|
418 | static void device_gone(device_t *dev)
|
---|
419 | {
|
---|
420 | hc_t *const hc = bus_to_hc(dev->bus);
|
---|
421 | usb2_bus_device_gone(&hc->bus_helper, dev);
|
---|
422 | }
|
---|
423 |
|
---|
424 | static int hc_status(bus_t *, uint32_t *);
|
---|
425 | static int hc_schedule(usb_transfer_batch_t *);
|
---|
426 |
|
---|
427 | static const bus_ops_t uhci_bus_ops = {
|
---|
428 | .interrupt = hc_interrupt,
|
---|
429 | .status = hc_status,
|
---|
430 |
|
---|
431 | .device_enumerate = device_enumerate,
|
---|
432 | .device_gone = device_gone,
|
---|
433 |
|
---|
434 | .endpoint_create = endpoint_create,
|
---|
435 | .endpoint_register = endpoint_register,
|
---|
436 | .endpoint_unregister = endpoint_unregister,
|
---|
437 |
|
---|
438 | .batch_create = create_transfer_batch,
|
---|
439 | .batch_schedule = hc_schedule,
|
---|
440 | .batch_destroy = destroy_transfer_batch,
|
---|
441 | };
|
---|
442 |
|
---|
443 | /** Initialize UHCI hc memory structures.
|
---|
444 | *
|
---|
445 | * @param[in] instance UHCI structure to use.
|
---|
446 | * @return Error code
|
---|
447 | * @note Should be called only once on any structure.
|
---|
448 | *
|
---|
449 | * Structures:
|
---|
450 | * - transfer lists (queue heads need to be accessible by the hw)
|
---|
451 | * - frame list page (needs to be one UHCI hw accessible 4K page)
|
---|
452 | */
|
---|
453 | errno_t hc_init_mem_structures(hc_t *instance)
|
---|
454 | {
|
---|
455 | assert(instance);
|
---|
456 |
|
---|
457 | usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
|
---|
458 |
|
---|
459 | bus_init(&instance->bus, sizeof(device_t));
|
---|
460 | instance->bus.ops = &uhci_bus_ops;
|
---|
461 |
|
---|
462 | hc_device_setup(&instance->base, &instance->bus);
|
---|
463 |
|
---|
464 | /* Init USB frame list page */
|
---|
465 | instance->frame_list = get_page();
|
---|
466 | if (!instance->frame_list) {
|
---|
467 | return ENOMEM;
|
---|
468 | }
|
---|
469 | usb_log_debug("Initialized frame list at %p.", instance->frame_list);
|
---|
470 |
|
---|
471 | /* Init transfer lists */
|
---|
472 | errno_t ret = hc_init_transfer_lists(instance);
|
---|
473 | if (ret != EOK) {
|
---|
474 | usb_log_error("Failed to initialize transfer lists.");
|
---|
475 | return_page(instance->frame_list);
|
---|
476 | return ENOMEM;
|
---|
477 | }
|
---|
478 | list_initialize(&instance->pending_endpoints);
|
---|
479 | usb_log_debug("Initialized transfer lists.");
|
---|
480 |
|
---|
481 | /* Set all frames to point to the first queue head */
|
---|
482 | const uint32_t queue = LINK_POINTER_QH(
|
---|
483 | addr_to_phys(instance->transfers_interrupt.queue_head));
|
---|
484 |
|
---|
485 | for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
|
---|
486 | instance->frame_list[i] = queue;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return EOK;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /** Initialize UHCI hc transfer lists.
|
---|
493 | *
|
---|
494 | * @param[in] instance UHCI structure to use.
|
---|
495 | * @return Error code
|
---|
496 | * @note Should be called only once on any structure.
|
---|
497 | *
|
---|
498 | * Initializes transfer lists and sets them in one chain to support proper
|
---|
499 | * USB scheduling. Sets pointer table for quick access.
|
---|
500 | */
|
---|
501 | errno_t hc_init_transfer_lists(hc_t *instance)
|
---|
502 | {
|
---|
503 | assert(instance);
|
---|
504 | #define SETUP_TRANSFER_LIST(type, name) \
|
---|
505 | do { \
|
---|
506 | errno_t ret = transfer_list_init(&instance->transfers_##type, name); \
|
---|
507 | if (ret != EOK) { \
|
---|
508 | usb_log_error("Failed to setup %s transfer list: %s.", \
|
---|
509 | name, str_error(ret)); \
|
---|
510 | transfer_list_fini(&instance->transfers_bulk_full); \
|
---|
511 | transfer_list_fini(&instance->transfers_control_full); \
|
---|
512 | transfer_list_fini(&instance->transfers_control_slow); \
|
---|
513 | transfer_list_fini(&instance->transfers_interrupt); \
|
---|
514 | return ret; \
|
---|
515 | } \
|
---|
516 | } while (0)
|
---|
517 |
|
---|
518 | SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
|
---|
519 | SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
|
---|
520 | SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
|
---|
521 | SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
|
---|
522 | #undef SETUP_TRANSFER_LIST
|
---|
523 | /* Connect lists into one schedule */
|
---|
524 | transfer_list_set_next(&instance->transfers_control_full,
|
---|
525 | &instance->transfers_bulk_full);
|
---|
526 | transfer_list_set_next(&instance->transfers_control_slow,
|
---|
527 | &instance->transfers_control_full);
|
---|
528 | transfer_list_set_next(&instance->transfers_interrupt,
|
---|
529 | &instance->transfers_control_slow);
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * FSBR, This feature is not needed (adds no benefit) and is supposedly
|
---|
533 | * buggy on certain hw, enable at your own risk.
|
---|
534 | */
|
---|
535 | #ifdef FSBR
|
---|
536 | transfer_list_set_next(&instance->transfers_bulk_full,
|
---|
537 | &instance->transfers_control_full);
|
---|
538 | #endif
|
---|
539 |
|
---|
540 | /* Assign pointers to be used during scheduling */
|
---|
541 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
|
---|
542 | &instance->transfers_interrupt;
|
---|
543 | instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
|
---|
544 | &instance->transfers_interrupt;
|
---|
545 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
|
---|
546 | &instance->transfers_control_full;
|
---|
547 | instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
|
---|
548 | &instance->transfers_control_slow;
|
---|
549 | instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
|
---|
550 | &instance->transfers_bulk_full;
|
---|
551 |
|
---|
552 | return EOK;
|
---|
553 | }
|
---|
554 |
|
---|
555 | static errno_t hc_status(bus_t *bus, uint32_t *status)
|
---|
556 | {
|
---|
557 | hc_t *instance = bus_to_hc(bus);
|
---|
558 | assert(status);
|
---|
559 |
|
---|
560 | *status = 0;
|
---|
561 | if (instance->registers) {
|
---|
562 | uint16_t s = pio_read_16(&instance->registers->usbsts);
|
---|
563 | pio_write_16(&instance->registers->usbsts, s);
|
---|
564 | *status = s;
|
---|
565 | }
|
---|
566 | return EOK;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /**
|
---|
570 | * Schedule batch for execution.
|
---|
571 | *
|
---|
572 | * @param[in] instance UHCI structure to use.
|
---|
573 | * @param[in] batch Transfer batch to schedule.
|
---|
574 | * @return Error code
|
---|
575 | */
|
---|
576 | static errno_t hc_schedule(usb_transfer_batch_t *batch)
|
---|
577 | {
|
---|
578 | uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
|
---|
579 | endpoint_t *ep = batch->ep;
|
---|
580 | hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
|
---|
581 |
|
---|
582 | if (batch->target.address == uhci_rh_get_address(&hc->rh))
|
---|
583 | return uhci_rh_schedule(&hc->rh, batch);
|
---|
584 |
|
---|
585 | transfer_list_t *const list =
|
---|
586 | hc->transfers[ep->device->speed][ep->transfer_type];
|
---|
587 |
|
---|
588 | if (!list)
|
---|
589 | return ENOTSUP;
|
---|
590 |
|
---|
591 | errno_t err;
|
---|
592 | if ((err = uhci_transfer_batch_prepare(uhci_batch)))
|
---|
593 | return err;
|
---|
594 |
|
---|
595 | return transfer_list_add_batch(list, uhci_batch);
|
---|
596 | }
|
---|
597 |
|
---|
598 | /** Debug function, checks consistency of memory structures.
|
---|
599 | *
|
---|
600 | * @param[in] arg UHCI structure to use.
|
---|
601 | * @return EOK (should never return)
|
---|
602 | */
|
---|
603 | errno_t hc_debug_checker(void *arg)
|
---|
604 | {
|
---|
605 | hc_t *instance = arg;
|
---|
606 | assert(instance);
|
---|
607 |
|
---|
608 | #define QH(queue) \
|
---|
609 | instance->transfers_##queue.queue_head
|
---|
610 |
|
---|
611 | while (true) {
|
---|
612 | const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
|
---|
613 | const uint16_t sts = pio_read_16(&instance->registers->usbsts);
|
---|
614 | const uint16_t intr =
|
---|
615 | pio_read_16(&instance->registers->usbintr);
|
---|
616 |
|
---|
617 | if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
|
---|
618 | usb_log_debug2("Command: %X Status: %X Intr: %x",
|
---|
619 | cmd, sts, intr);
|
---|
620 | }
|
---|
621 |
|
---|
622 | const uintptr_t frame_list =
|
---|
623 | pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
|
---|
624 | if (frame_list != addr_to_phys(instance->frame_list)) {
|
---|
625 | usb_log_debug("Framelist address: %p vs. %p.",
|
---|
626 | (void *) frame_list,
|
---|
627 | (void *) addr_to_phys(instance->frame_list));
|
---|
628 | }
|
---|
629 |
|
---|
630 | int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
|
---|
631 |
|
---|
632 | uintptr_t expected_pa = instance->frame_list[frnum] &
|
---|
633 | LINK_POINTER_ADDRESS_MASK;
|
---|
634 | uintptr_t real_pa = addr_to_phys(QH(interrupt));
|
---|
635 | if (expected_pa != real_pa) {
|
---|
636 | usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
|
---|
637 | (void *) expected_pa, frnum, (void *) real_pa);
|
---|
638 | }
|
---|
639 |
|
---|
640 | expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
|
---|
641 | real_pa = addr_to_phys(QH(control_slow));
|
---|
642 | if (expected_pa != real_pa) {
|
---|
643 | usb_log_debug("Control Slow QH: %p vs. %p.",
|
---|
644 | (void *) expected_pa, (void *) real_pa);
|
---|
645 | }
|
---|
646 |
|
---|
647 | expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
|
---|
648 | real_pa = addr_to_phys(QH(control_full));
|
---|
649 | if (expected_pa != real_pa) {
|
---|
650 | usb_log_debug("Control Full QH: %p vs. %p.",
|
---|
651 | (void *) expected_pa, (void *) real_pa);
|
---|
652 | }
|
---|
653 |
|
---|
654 | expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
|
---|
655 | real_pa = addr_to_phys(QH(bulk_full));
|
---|
656 | if (expected_pa != real_pa) {
|
---|
657 | usb_log_debug("Bulk QH: %p vs. %p.",
|
---|
658 | (void *) expected_pa, (void *) real_pa);
|
---|
659 | }
|
---|
660 | fibril_usleep(UHCI_DEBUGER_TIMEOUT);
|
---|
661 | }
|
---|
662 | return EOK;
|
---|
663 | #undef QH
|
---|
664 | }
|
---|
665 | /**
|
---|
666 | * @}
|
---|
667 | */
|
---|