source: mainline/uspace/drv/bus/usb/uhci/hc.c@ 8033f89

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8033f89 was 3ac86a4, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

uhci: fix removal of isochronous endpoints

  • Property mode set to 100644
File size: 17.6 KB
Line 
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 drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI Host controller driver routines
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <async.h>
39#include <ddi.h>
40#include <device/hw_res_parsed.h>
41#include <fibril.h>
42#include <errno.h>
43#include <macros.h>
44#include <mem.h>
45#include <stdlib.h>
46#include <stdint.h>
47#include <str_error.h>
48
49#include <usb/debug.h>
50#include <usb/usb.h>
51#include <usb/host/utils/malloc32.h>
52#include <usb/host/bandwidth.h>
53#include <usb/host/utility.h>
54
55#include "uhci_batch.h"
56#include "transfer_list.h"
57#include "hc.h"
58
59#define UHCI_INTR_ALLOW_INTERRUPTS \
60 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
61#define UHCI_STATUS_USED_INTERRUPTS \
62 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
63
64static const irq_pio_range_t uhci_irq_pio_ranges[] = {
65 {
66 .base = 0,
67 .size = sizeof(uhci_regs_t)
68 }
69};
70
71static const irq_cmd_t uhci_irq_commands[] = {
72 {
73 .cmd = CMD_PIO_READ_16,
74 .dstarg = 1,
75 .addr = NULL
76 },
77 {
78 .cmd = CMD_AND,
79 .srcarg = 1,
80 .dstarg = 2,
81 .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
82 },
83 {
84 .cmd = CMD_PREDICATE,
85 .srcarg = 2,
86 .value = 2
87 },
88 {
89 .cmd = CMD_PIO_WRITE_A_16,
90 .srcarg = 1,
91 .addr = NULL
92 },
93 {
94 .cmd = CMD_ACCEPT
95 }
96};
97
98static void hc_init_hw(const hc_t *instance);
99static int hc_init_mem_structures(hc_t *instance, hc_device_t *);
100static int hc_init_transfer_lists(hc_t *instance);
101
102static int hc_debug_checker(void *arg);
103
104
105/** Generate IRQ code.
106 * @param[out] code IRQ code structure.
107 * @param[in] hw_res Device's resources.
108 *
109 * @return Error code.
110 */
111int hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
112{
113 assert(code);
114 assert(hw_res);
115
116 if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
117 return EINVAL;
118 const addr_range_t regs = hw_res->io_ranges.ranges[0];
119
120 if (RNGSZ(regs) < sizeof(uhci_regs_t))
121 return EOVERFLOW;
122
123 code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
124 if (code->ranges == NULL)
125 return ENOMEM;
126
127 code->cmds = malloc(sizeof(uhci_irq_commands));
128 if (code->cmds == NULL) {
129 free(code->ranges);
130 return ENOMEM;
131 }
132
133 code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
134 code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
135
136 memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
137 code->ranges[0].base = RNGABS(regs);
138
139 memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
140 uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
141 code->cmds[0].addr = (void*)&registers->usbsts;
142 code->cmds[3].addr = (void*)&registers->usbsts;
143
144 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
145 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
146
147 return hw_res->irqs.irqs[0];
148}
149
150/** Take action based on the interrupt cause.
151 *
152 * @param[in] hcd HCD structure to use.
153 * @param[in] status Value of the status register at the time of interrupt.
154 *
155 * Interrupt might indicate:
156 * - transaction completed, either by triggering IOC, SPD, or an error
157 * - some kind of device error
158 * - resume from suspend state (not implemented)
159 */
160static void hc_interrupt(bus_t *bus, uint32_t status)
161{
162 hc_t *instance = bus_to_hc(bus);
163
164 /* Lower 2 bits are transaction error and transaction complete */
165 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
166 LIST_INITIALIZE(done);
167 transfer_list_remove_finished(
168 &instance->transfers_interrupt, &done);
169 transfer_list_remove_finished(
170 &instance->transfers_control_slow, &done);
171 transfer_list_remove_finished(
172 &instance->transfers_control_full, &done);
173 transfer_list_remove_finished(
174 &instance->transfers_bulk_full, &done);
175
176 list_foreach_safe(done, current, next) {
177 list_remove(current);
178 uhci_transfer_batch_t *batch =
179 uhci_transfer_batch_from_link(current);
180 usb_transfer_batch_finish(&batch->base);
181 }
182 }
183 /* Resume interrupts are not supported */
184 if (status & UHCI_STATUS_RESUME) {
185 usb_log_error("Resume interrupt!");
186 }
187
188 /* Bits 4 and 5 indicate hc error */
189 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
190 usb_log_error("UHCI hardware failure!.");
191 ++instance->hw_failures;
192 transfer_list_abort_all(&instance->transfers_interrupt);
193 transfer_list_abort_all(&instance->transfers_control_slow);
194 transfer_list_abort_all(&instance->transfers_control_full);
195 transfer_list_abort_all(&instance->transfers_bulk_full);
196
197 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
198 /* reinitialize hw, this triggers virtual disconnect*/
199 hc_init_hw(instance);
200 } else {
201 usb_log_fatal("Too many UHCI hardware failures!.");
202 hc_gone(&instance->base);
203 }
204 }
205}
206
207/** Initialize UHCI hc driver structure
208 *
209 * @param[in] instance Memory place to initialize.
210 * @param[in] regs Range of device's I/O control registers.
211 * @param[in] interrupts True if hw interrupts should be used.
212 * @return Error code.
213 * @note Should be called only once on any structure.
214 *
215 * Initializes memory structures, starts up hw, and launches debugger and
216 * interrupt fibrils.
217 */
218int hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
219{
220 hc_t *instance = hcd_to_hc(hcd);
221 assert(hw_res);
222 if (hw_res->io_ranges.count != 1 ||
223 hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
224 return EINVAL;
225
226 instance->hw_failures = 0;
227
228 /* allow access to hc control registers */
229 int ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
230 (void **) &instance->registers);
231 if (ret != EOK) {
232 usb_log_error("Failed to gain access to registers: %s.",
233 str_error(ret));
234 return ret;
235 }
236
237 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
238 hw_res->io_ranges.ranges[0].address.absolute,
239 hw_res->io_ranges.ranges[0].size);
240
241 ret = hc_init_mem_structures(instance, hcd);
242 if (ret != EOK) {
243 usb_log_error("Failed to init UHCI memory structures: %s.",
244 str_error(ret));
245 // TODO: we should disable pio here
246 return ret;
247 }
248
249 return EOK;
250}
251
252int hc_start(hc_device_t *hcd)
253{
254 hc_t *instance = hcd_to_hc(hcd);
255 hc_init_hw(instance);
256 (void)hc_debug_checker;
257
258 return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
259}
260
261int hc_setup_roothub(hc_device_t *hcd)
262{
263 return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
264}
265
266/** Safely dispose host controller internal structures
267 *
268 * @param[in] instance Host controller structure to use.
269 */
270int hc_gone(hc_device_t *instance)
271{
272 assert(instance);
273 //TODO Implement
274 return ENOTSUP;
275}
276
277/** Initialize UHCI hc hw resources.
278 *
279 * @param[in] instance UHCI structure to use.
280 * For magic values see UHCI Design Guide
281 */
282void hc_init_hw(const hc_t *instance)
283{
284 assert(instance);
285 uhci_regs_t *registers = instance->registers;
286
287 /* Reset everything, who knows what touched it before us */
288 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
289 async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
290 pio_write_16(&registers->usbcmd, 0);
291
292 /* Reset hc, all states and counters. Hope that hw is not broken */
293 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
294 do { async_usleep(10); }
295 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
296
297 /* Set frame to exactly 1ms */
298 pio_write_8(&registers->sofmod, 64);
299
300 /* Set frame list pointer */
301 const uint32_t pa = addr_to_phys(instance->frame_list);
302 pio_write_32(&registers->flbaseadd, pa);
303
304 if (instance->base.irq_cap >= 0) {
305 /* Enable all interrupts, but resume interrupt */
306 pio_write_16(&instance->registers->usbintr,
307 UHCI_INTR_ALLOW_INTERRUPTS);
308 }
309
310 const uint16_t cmd = pio_read_16(&registers->usbcmd);
311 if (cmd != 0)
312 usb_log_warning("Previous command value: %x.", cmd);
313
314 /* Start the hc with large(64B) packet FSBR */
315 pio_write_16(&registers->usbcmd,
316 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
317}
318
319static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
320{
321 uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
322 return &batch->base;
323}
324
325static void destroy_transfer_batch(usb_transfer_batch_t *batch)
326{
327 uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
328}
329
330static void endpoint_unregister(endpoint_t *ep)
331{
332 hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
333 usb2_bus_ops.endpoint_unregister(ep);
334
335 uhci_transfer_batch_t *batch = NULL;
336
337 // Check for the roothub, as it does not schedule into lists
338 if (ep->device->address == uhci_rh_get_address(&hc->rh)) {
339 // FIXME: We shall check the roothub for active transfer. But
340 // as it is polling, there is no way to make it stop doing so.
341 // Return after rewriting uhci rh.
342 return;
343 }
344
345 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
346
347 if (!list)
348 /*
349 * We don't support this combination (e.g. isochronous),
350 * so no transfer can be active.
351 */
352 return;
353
354 // To avoid ABBA deadlock, we need to take the list first
355 fibril_mutex_lock(&list->guard);
356 fibril_mutex_lock(&ep->guard);
357 if (ep->active_batch) {
358 batch = uhci_transfer_batch_get(ep->active_batch);
359 endpoint_deactivate_locked(ep);
360 transfer_list_remove_batch(list, batch);
361 }
362 fibril_mutex_unlock(&ep->guard);
363 fibril_mutex_unlock(&list->guard);
364
365 if (batch) {
366 // The HW could have been looking at the batch.
367 // Better wait two frames before we release the buffers.
368 async_usleep(2000);
369 batch->base.error = EINTR;
370 batch->base.transferred_size = 0;
371 usb_transfer_batch_finish(&batch->base);
372 }
373}
374
375static int hc_status(bus_t *, uint32_t *);
376static int hc_schedule(usb_transfer_batch_t *);
377
378static const bus_ops_t uhci_bus_ops = {
379 .parent = &usb2_bus_ops,
380
381 .interrupt = hc_interrupt,
382 .status = hc_status,
383
384 .endpoint_unregister = endpoint_unregister,
385 .endpoint_count_bw = bandwidth_count_usb11,
386
387 .batch_create = create_transfer_batch,
388 .batch_schedule = hc_schedule,
389 .batch_destroy = destroy_transfer_batch,
390};
391
392/** Initialize UHCI hc memory structures.
393 *
394 * @param[in] instance UHCI structure to use.
395 * @return Error code
396 * @note Should be called only once on any structure.
397 *
398 * Structures:
399 * - transfer lists (queue heads need to be accessible by the hw)
400 * - frame list page (needs to be one UHCI hw accessible 4K page)
401 */
402int hc_init_mem_structures(hc_t *instance, hc_device_t *hcd)
403{
404 assert(instance);
405
406 usb2_bus_init(&instance->bus, BANDWIDTH_AVAILABLE_USB11);
407
408 bus_t *bus = (bus_t *) &instance->bus;
409 bus->ops = &uhci_bus_ops;
410
411 hc_device_setup(&instance->base, bus);
412
413 /* Init USB frame list page */
414 instance->frame_list = get_page();
415 if (!instance->frame_list) {
416 return ENOMEM;
417 }
418 usb_log_debug("Initialized frame list at %p.", instance->frame_list);
419
420 /* Init transfer lists */
421 int ret = hc_init_transfer_lists(instance);
422 if (ret != EOK) {
423 usb_log_error("Failed to initialize transfer lists.");
424 return_page(instance->frame_list);
425 return ENOMEM;
426 }
427 usb_log_debug("Initialized transfer lists.");
428
429
430 /* Set all frames to point to the first queue head */
431 const uint32_t queue = LINK_POINTER_QH(
432 addr_to_phys(instance->transfers_interrupt.queue_head));
433
434 for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
435 instance->frame_list[i] = queue;
436 }
437
438 return EOK;
439}
440
441/** Initialize UHCI hc transfer lists.
442 *
443 * @param[in] instance UHCI structure to use.
444 * @return Error code
445 * @note Should be called only once on any structure.
446 *
447 * Initializes transfer lists and sets them in one chain to support proper
448 * USB scheduling. Sets pointer table for quick access.
449 */
450int hc_init_transfer_lists(hc_t *instance)
451{
452 assert(instance);
453#define SETUP_TRANSFER_LIST(type, name) \
454do { \
455 int ret = transfer_list_init(&instance->transfers_##type, name); \
456 if (ret != EOK) { \
457 usb_log_error("Failed to setup %s transfer list: %s.", \
458 name, str_error(ret)); \
459 transfer_list_fini(&instance->transfers_bulk_full); \
460 transfer_list_fini(&instance->transfers_control_full); \
461 transfer_list_fini(&instance->transfers_control_slow); \
462 transfer_list_fini(&instance->transfers_interrupt); \
463 return ret; \
464 } \
465} while (0)
466
467 SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
468 SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
469 SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
470 SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
471#undef SETUP_TRANSFER_LIST
472 /* Connect lists into one schedule */
473 transfer_list_set_next(&instance->transfers_control_full,
474 &instance->transfers_bulk_full);
475 transfer_list_set_next(&instance->transfers_control_slow,
476 &instance->transfers_control_full);
477 transfer_list_set_next(&instance->transfers_interrupt,
478 &instance->transfers_control_slow);
479
480 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
481 * buggy on certain hw, enable at your own risk. */
482#ifdef FSBR
483 transfer_list_set_next(&instance->transfers_bulk_full,
484 &instance->transfers_control_full);
485#endif
486
487 /* Assign pointers to be used during scheduling */
488 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
489 &instance->transfers_interrupt;
490 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
491 &instance->transfers_interrupt;
492 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
493 &instance->transfers_control_full;
494 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
495 &instance->transfers_control_slow;
496 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
497 &instance->transfers_bulk_full;
498
499 return EOK;
500}
501
502static int hc_status(bus_t *bus, uint32_t *status)
503{
504 hc_t *instance = bus_to_hc(bus);
505 assert(status);
506
507 *status = 0;
508 if (instance->registers) {
509 uint16_t s = pio_read_16(&instance->registers->usbsts);
510 pio_write_16(&instance->registers->usbsts, s);
511 *status = s;
512 }
513 return EOK;
514}
515
516/** Schedule batch for execution.
517 *
518 * @param[in] instance UHCI structure to use.
519 * @param[in] batch Transfer batch to schedule.
520 * @return Error code
521 *
522 * Checks for bandwidth availability and appends the batch to the proper queue.
523 */
524static int hc_schedule(usb_transfer_batch_t *batch)
525{
526 uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
527 endpoint_t *ep = batch->ep;
528 hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
529
530 if (batch->target.address == uhci_rh_get_address(&hc->rh))
531 return uhci_rh_schedule(&hc->rh, batch);
532
533
534 const int err = uhci_transfer_batch_prepare(uhci_batch);
535 if (err)
536 return err;
537
538 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
539 assert(list);
540 transfer_list_add_batch(list, uhci_batch);
541
542 return EOK;
543}
544
545int hc_unschedule_batch(usb_transfer_batch_t *batch)
546{
547
548 return EOK;
549}
550
551/** Debug function, checks consistency of memory structures.
552 *
553 * @param[in] arg UHCI structure to use.
554 * @return EOK (should never return)
555 */
556int hc_debug_checker(void *arg)
557{
558 hc_t *instance = arg;
559 assert(instance);
560
561#define QH(queue) \
562 instance->transfers_##queue.queue_head
563
564 while (1) {
565 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
566 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
567 const uint16_t intr =
568 pio_read_16(&instance->registers->usbintr);
569
570 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
571 usb_log_debug2("Command: %X Status: %X Intr: %x",
572 cmd, sts, intr);
573 }
574
575 const uintptr_t frame_list =
576 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
577 if (frame_list != addr_to_phys(instance->frame_list)) {
578 usb_log_debug("Framelist address: %p vs. %p.",
579 (void *) frame_list,
580 (void *) addr_to_phys(instance->frame_list));
581 }
582
583 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
584
585 uintptr_t expected_pa = instance->frame_list[frnum]
586 & LINK_POINTER_ADDRESS_MASK;
587 uintptr_t real_pa = addr_to_phys(QH(interrupt));
588 if (expected_pa != real_pa) {
589 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
590 (void *) expected_pa, frnum, (void *) real_pa);
591 }
592
593 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
594 real_pa = addr_to_phys(QH(control_slow));
595 if (expected_pa != real_pa) {
596 usb_log_debug("Control Slow QH: %p vs. %p.",
597 (void *) expected_pa, (void *) real_pa);
598 }
599
600 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
601 real_pa = addr_to_phys(QH(control_full));
602 if (expected_pa != real_pa) {
603 usb_log_debug("Control Full QH: %p vs. %p.",
604 (void *) expected_pa, (void *) real_pa);
605 }
606
607 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
608 real_pa = addr_to_phys(QH(bulk_full));
609 if (expected_pa != real_pa ) {
610 usb_log_debug("Bulk QH: %p vs. %p.",
611 (void *) expected_pa, (void *) real_pa);
612 }
613 async_usleep(UHCI_DEBUGER_TIMEOUT);
614 }
615 return EOK;
616#undef QH
617}
618/**
619 * @}
620 */
Note: See TracBrowser for help on using the repository browser.