[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);
|
---|
[4abc304] | 72 | usb_log_debug2("Adding batch(%p) to queue %s.\n", batch, instance->name);
|
---|
[9a818a9] | 73 |
|
---|
[83c439c] | 74 | uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
|
---|
[9a818a9] | 75 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
[7dd3318] | 76 | pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
|
---|
[9a818a9] | 77 |
|
---|
[d6115e5] | 78 | batch->qh->next_queue = instance->queue_head->next_queue;
|
---|
[2964aa87] | 79 |
|
---|
[e0df6c2] | 80 | fibril_mutex_lock(&instance->guard);
|
---|
| 81 |
|
---|
[d6115e5] | 82 | if (instance->queue_head->element == instance->queue_head->next_queue) {
|
---|
[9a818a9] | 83 | /* there is nothing scheduled */
|
---|
[83c439c] | 84 | list_append(&batch->link, &instance->batch_list);
|
---|
[9a818a9] | 85 | instance->queue_head->element = pa;
|
---|
[4abc304] | 86 | usb_log_debug("Batch(%p) added to queue %s first.\n",
|
---|
[83c439c] | 87 | batch, instance->name);
|
---|
[e0df6c2] | 88 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 89 | return;
|
---|
| 90 | }
|
---|
[7dd3318] | 91 | /* now we can be sure that there is someting scheduled */
|
---|
[83c439c] | 92 | assert(!list_empty(&instance->batch_list));
|
---|
| 93 | batch_t *first = list_get_instance(
|
---|
| 94 | instance->batch_list.next, batch_t, link);
|
---|
| 95 | batch_t *last = list_get_instance(
|
---|
| 96 | instance->batch_list.prev, batch_t, link);
|
---|
[7dd3318] | 97 | queue_head_append_qh(last->qh, pa);
|
---|
[83c439c] | 98 | list_append(&batch->link, &instance->batch_list);
|
---|
[4abc304] | 99 | usb_log_debug("Batch(%p) added to queue %s last, first is %p.\n",
|
---|
[83c439c] | 100 | batch, instance->name, first );
|
---|
[e0df6c2] | 101 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 102 | }
|
---|
[53338bda] | 103 | /*----------------------------------------------------------------------------*/
|
---|
[83c439c] | 104 | static void transfer_list_remove_batch(
|
---|
| 105 | transfer_list_t *instance, batch_t *batch)
|
---|
[53338bda] | 106 | {
|
---|
| 107 | assert(instance);
|
---|
[83c439c] | 108 | assert(batch);
|
---|
[53338bda] | 109 | assert(instance->queue_head);
|
---|
[83c439c] | 110 | assert(batch->qh);
|
---|
[4abc304] | 111 | usb_log_debug2("Removing batch(%p) from queue %s.\n", batch, instance->name);
|
---|
[7dd3318] | 112 |
|
---|
| 113 | /* I'm the first one here */
|
---|
[e0df6c2] | 114 | if (batch->link.prev == &instance->batch_list) {
|
---|
[4abc304] | 115 | usb_log_debug("Batch(%p) removed (FIRST) from queue %s, next element %x.\n",
|
---|
| 116 | batch, instance->name, batch->qh->next_queue);
|
---|
[83c439c] | 117 | instance->queue_head->element = batch->qh->next_queue;
|
---|
[7dd3318] | 118 | } else {
|
---|
[4abc304] | 119 | usb_log_debug("Batch(%p) removed (NOT FIRST) from queue, next element %x.\n",
|
---|
| 120 | batch, instance->name, batch->qh->next_queue);
|
---|
[83c439c] | 121 | batch_t *prev = list_get_instance(batch->link.prev, batch_t, link);
|
---|
| 122 | prev->qh->next_queue = batch->qh->next_queue;
|
---|
[7dd3318] | 123 | }
|
---|
[83c439c] | 124 | list_remove(&batch->link);
|
---|
[7dd3318] | 125 | }
|
---|
| 126 | /*----------------------------------------------------------------------------*/
|
---|
[86c2ccd] | 127 | void transfer_list_remove_finished(transfer_list_t *instance)
|
---|
[7dd3318] | 128 | {
|
---|
| 129 | assert(instance);
|
---|
[86c2ccd] | 130 |
|
---|
| 131 | LIST_INITIALIZE(done);
|
---|
| 132 |
|
---|
[e0df6c2] | 133 | fibril_mutex_lock(&instance->guard);
|
---|
[83c439c] | 134 | link_t *current = instance->batch_list.next;
|
---|
| 135 | while (current != &instance->batch_list) {
|
---|
[7dd3318] | 136 | link_t *next = current->next;
|
---|
[83c439c] | 137 | batch_t *batch = list_get_instance(current, batch_t, link);
|
---|
[53338bda] | 138 |
|
---|
[83c439c] | 139 | if (batch_is_complete(batch)) {
|
---|
| 140 | transfer_list_remove_batch(instance, batch);
|
---|
[86c2ccd] | 141 | list_append(current, &done);
|
---|
[7dd3318] | 142 | }
|
---|
| 143 | current = next;
|
---|
[53338bda] | 144 | }
|
---|
[e0df6c2] | 145 | fibril_mutex_unlock(&instance->guard);
|
---|
[86c2ccd] | 146 |
|
---|
| 147 | while (!list_empty(&done)) {
|
---|
| 148 | link_t *item = done.next;
|
---|
| 149 | list_remove(item);
|
---|
| 150 | batch_t *batch = list_get_instance(item, batch_t, link);
|
---|
| 151 | batch->next_step(batch);
|
---|
| 152 | }
|
---|
[53338bda] | 153 | }
|
---|
[c56dbe0] | 154 | /**
|
---|
| 155 | * @}
|
---|
| 156 | */
|
---|