[c56dbe0] | 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 | /** @addtogroup usb
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief UHCI driver
|
---|
| 33 | */
|
---|
[89a0485a] | 34 | #include <errno.h>
|
---|
| 35 |
|
---|
[afcd86e] | 36 | #include <usb/debug.h>
|
---|
| 37 |
|
---|
[89a0485a] | 38 | #include "transfer_list.h"
|
---|
| 39 |
|
---|
[881c47b] | 40 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
[89a0485a] | 41 | {
|
---|
| 42 | assert(instance);
|
---|
[881c47b] | 43 | instance->next = NULL;
|
---|
| 44 | instance->name = name;
|
---|
[5944244] | 45 | instance->queue_head = queue_head_get();
|
---|
[89a0485a] | 46 | if (!instance->queue_head) {
|
---|
[afcd86e] | 47 | usb_log_error("Failed to allocate queue head.\n");
|
---|
[89a0485a] | 48 | return ENOMEM;
|
---|
| 49 | }
|
---|
| 50 | instance->queue_head_pa = (uintptr_t)addr_to_phys(instance->queue_head);
|
---|
| 51 |
|
---|
[881c47b] | 52 | queue_head_init(instance->queue_head);
|
---|
[83c439c] | 53 | list_initialize(&instance->batch_list);
|
---|
[e0df6c2] | 54 | fibril_mutex_initialize(&instance->guard);
|
---|
[89a0485a] | 55 | return EOK;
|
---|
| 56 | }
|
---|
| 57 | /*----------------------------------------------------------------------------*/
|
---|
[881c47b] | 58 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
| 59 | {
|
---|
| 60 | assert(instance);
|
---|
| 61 | assert(next);
|
---|
| 62 | if (!instance->queue_head)
|
---|
| 63 | return;
|
---|
[7dd3318] | 64 | queue_head_append_qh(instance->queue_head, next->queue_head_pa);
|
---|
[d6115e5] | 65 | instance->queue_head->element = instance->queue_head->next_queue;
|
---|
[881c47b] | 66 | }
|
---|
| 67 | /*----------------------------------------------------------------------------*/
|
---|
[83c439c] | 68 | void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
|
---|
[9a818a9] | 69 | {
|
---|
| 70 | assert(instance);
|
---|
[83c439c] | 71 | assert(batch);
|
---|
[9a818a9] | 72 |
|
---|
[83c439c] | 73 | uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
|
---|
[9a818a9] | 74 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
[7dd3318] | 75 | pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
|
---|
[9a818a9] | 76 |
|
---|
[d6115e5] | 77 | batch->qh->next_queue = instance->queue_head->next_queue;
|
---|
[2964aa87] | 78 |
|
---|
[e0df6c2] | 79 | fibril_mutex_lock(&instance->guard);
|
---|
| 80 |
|
---|
[d6115e5] | 81 | if (instance->queue_head->element == instance->queue_head->next_queue) {
|
---|
[9a818a9] | 82 | /* there is nothing scheduled */
|
---|
[83c439c] | 83 | list_append(&batch->link, &instance->batch_list);
|
---|
[9a818a9] | 84 | instance->queue_head->element = pa;
|
---|
[83c439c] | 85 | usb_log_debug2("Added batch(%p) to queue %s first.\n",
|
---|
| 86 | batch, instance->name);
|
---|
[e0df6c2] | 87 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 88 | return;
|
---|
| 89 | }
|
---|
[7dd3318] | 90 | /* now we can be sure that there is someting scheduled */
|
---|
[83c439c] | 91 | assert(!list_empty(&instance->batch_list));
|
---|
| 92 | batch_t *first = list_get_instance(
|
---|
| 93 | instance->batch_list.next, batch_t, link);
|
---|
| 94 | batch_t *last = list_get_instance(
|
---|
| 95 | instance->batch_list.prev, batch_t, link);
|
---|
[7dd3318] | 96 | queue_head_append_qh(last->qh, pa);
|
---|
[83c439c] | 97 | list_append(&batch->link, &instance->batch_list);
|
---|
| 98 | usb_log_debug2("Added batch(%p) to queue %s last, first is %p.\n",
|
---|
| 99 | batch, instance->name, first );
|
---|
[e0df6c2] | 100 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 101 | }
|
---|
[53338bda] | 102 | /*----------------------------------------------------------------------------*/
|
---|
[83c439c] | 103 | static void transfer_list_remove_batch(
|
---|
| 104 | transfer_list_t *instance, batch_t *batch)
|
---|
[53338bda] | 105 | {
|
---|
| 106 | assert(instance);
|
---|
[83c439c] | 107 | assert(batch);
|
---|
[53338bda] | 108 | assert(instance->queue_head);
|
---|
[83c439c] | 109 | assert(batch->qh);
|
---|
[7dd3318] | 110 |
|
---|
| 111 | /* I'm the first one here */
|
---|
[e0df6c2] | 112 | if (batch->link.prev == &instance->batch_list) {
|
---|
[7dd3318] | 113 | usb_log_debug("Removing tracer %p was first, next element %x.\n",
|
---|
[83c439c] | 114 | batch, batch->qh->next_queue);
|
---|
| 115 | instance->queue_head->element = batch->qh->next_queue;
|
---|
[7dd3318] | 116 | } else {
|
---|
| 117 | usb_log_debug("Removing tracer %p was NOT first, next element %x.\n",
|
---|
[83c439c] | 118 | batch, batch->qh->next_queue);
|
---|
| 119 | batch_t *prev = list_get_instance(batch->link.prev, batch_t, link);
|
---|
| 120 | prev->qh->next_queue = batch->qh->next_queue;
|
---|
[7dd3318] | 121 | }
|
---|
[83c439c] | 122 | list_remove(&batch->link);
|
---|
[7dd3318] | 123 | }
|
---|
| 124 | /*----------------------------------------------------------------------------*/
|
---|
| 125 | void transfer_list_check(transfer_list_t *instance)
|
---|
| 126 | {
|
---|
| 127 | assert(instance);
|
---|
[e0df6c2] | 128 | fibril_mutex_lock(&instance->guard);
|
---|
[83c439c] | 129 | link_t *current = instance->batch_list.next;
|
---|
| 130 | while (current != &instance->batch_list) {
|
---|
[7dd3318] | 131 | link_t *next = current->next;
|
---|
[83c439c] | 132 | batch_t *batch = list_get_instance(current, batch_t, link);
|
---|
[53338bda] | 133 |
|
---|
[83c439c] | 134 | if (batch_is_complete(batch)) {
|
---|
| 135 | transfer_list_remove_batch(instance, batch);
|
---|
| 136 | batch->next_step(batch);
|
---|
[7dd3318] | 137 | }
|
---|
| 138 | current = next;
|
---|
[53338bda] | 139 | }
|
---|
[e0df6c2] | 140 | fibril_mutex_unlock(&instance->guard);
|
---|
[53338bda] | 141 | }
|
---|
[c56dbe0] | 142 | /**
|
---|
| 143 | * @}
|
---|
| 144 | */
|
---|