[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 |
|
---|
[a7e2f0d] | 40 | static void transfer_list_remove_batch(
|
---|
| 41 | transfer_list_t *instance, batch_t *batch);
|
---|
| 42 | /*----------------------------------------------------------------------------*/
|
---|
| 43 | /** Initializes transfer list structures.
|
---|
| 44 | *
|
---|
| 45 | * @param[in] instance Memory place to use.
|
---|
| 46 | * @param[in] name Name of te new list.
|
---|
| 47 | * @return Error code
|
---|
| 48 | *
|
---|
| 49 | * Allocates memory for internat queue_head_t structure.
|
---|
| 50 | */
|
---|
[881c47b] | 51 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
[89a0485a] | 52 | {
|
---|
| 53 | assert(instance);
|
---|
[881c47b] | 54 | instance->next = NULL;
|
---|
| 55 | instance->name = name;
|
---|
[2ab6875] | 56 | instance->queue_head = malloc32(sizeof(queue_head_t));
|
---|
[89a0485a] | 57 | if (!instance->queue_head) {
|
---|
[afcd86e] | 58 | usb_log_error("Failed to allocate queue head.\n");
|
---|
[89a0485a] | 59 | return ENOMEM;
|
---|
| 60 | }
|
---|
[2ab6875] | 61 | instance->queue_head_pa = addr_to_phys(instance->queue_head);
|
---|
[89a0485a] | 62 |
|
---|
[881c47b] | 63 | queue_head_init(instance->queue_head);
|
---|
[83c439c] | 64 | list_initialize(&instance->batch_list);
|
---|
[e0df6c2] | 65 | fibril_mutex_initialize(&instance->guard);
|
---|
[89a0485a] | 66 | return EOK;
|
---|
| 67 | }
|
---|
| 68 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 69 | /** Set the next list in chain.
|
---|
| 70 | *
|
---|
| 71 | * @param[in] instance List to lead.
|
---|
| 72 | * @param[in] next List to append.
|
---|
| 73 | * @return Error code
|
---|
| 74 | */
|
---|
[881c47b] | 75 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
| 76 | {
|
---|
| 77 | assert(instance);
|
---|
| 78 | assert(next);
|
---|
| 79 | if (!instance->queue_head)
|
---|
| 80 | return;
|
---|
[7dd3318] | 81 | queue_head_append_qh(instance->queue_head, next->queue_head_pa);
|
---|
[d6115e5] | 82 | instance->queue_head->element = instance->queue_head->next_queue;
|
---|
[881c47b] | 83 | }
|
---|
| 84 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 85 | /** Submits a new transfer batch to list and queue.
|
---|
| 86 | *
|
---|
| 87 | * @param[in] instance List to use.
|
---|
| 88 | * @param[in] batch Transfer batch to submit.
|
---|
| 89 | * @return Error code
|
---|
| 90 | */
|
---|
[83c439c] | 91 | void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
|
---|
[9a818a9] | 92 | {
|
---|
| 93 | assert(instance);
|
---|
[83c439c] | 94 | assert(batch);
|
---|
[a7e2f0d] | 95 | usb_log_debug2(
|
---|
| 96 | "Adding batch(%p) to queue %s.\n", batch, instance->name);
|
---|
[9a818a9] | 97 |
|
---|
[83c439c] | 98 | uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
|
---|
[9a818a9] | 99 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
[7dd3318] | 100 | pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
|
---|
[9a818a9] | 101 |
|
---|
[d6115e5] | 102 | batch->qh->next_queue = instance->queue_head->next_queue;
|
---|
[2964aa87] | 103 |
|
---|
[e0df6c2] | 104 | fibril_mutex_lock(&instance->guard);
|
---|
| 105 |
|
---|
[d6115e5] | 106 | if (instance->queue_head->element == instance->queue_head->next_queue) {
|
---|
[9a818a9] | 107 | /* there is nothing scheduled */
|
---|
[83c439c] | 108 | list_append(&batch->link, &instance->batch_list);
|
---|
[9a818a9] | 109 | instance->queue_head->element = pa;
|
---|
[4abc304] | 110 | usb_log_debug("Batch(%p) added to queue %s first.\n",
|
---|
[83c439c] | 111 | batch, instance->name);
|
---|
[e0df6c2] | 112 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 113 | return;
|
---|
| 114 | }
|
---|
[7dd3318] | 115 | /* now we can be sure that there is someting scheduled */
|
---|
[83c439c] | 116 | assert(!list_empty(&instance->batch_list));
|
---|
| 117 | batch_t *first = list_get_instance(
|
---|
| 118 | instance->batch_list.next, batch_t, link);
|
---|
| 119 | batch_t *last = list_get_instance(
|
---|
| 120 | instance->batch_list.prev, batch_t, link);
|
---|
[7dd3318] | 121 | queue_head_append_qh(last->qh, pa);
|
---|
[83c439c] | 122 | list_append(&batch->link, &instance->batch_list);
|
---|
[a7e2f0d] | 123 |
|
---|
[4abc304] | 124 | usb_log_debug("Batch(%p) added to queue %s last, first is %p.\n",
|
---|
[a7e2f0d] | 125 | batch, instance->name, first);
|
---|
[e0df6c2] | 126 | fibril_mutex_unlock(&instance->guard);
|
---|
[9a818a9] | 127 | }
|
---|
[53338bda] | 128 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 129 | /** Removes a transfer batch from list and queue.
|
---|
| 130 | *
|
---|
| 131 | * @param[in] instance List to use.
|
---|
| 132 | * @param[in] batch Transfer batch to remove.
|
---|
| 133 | * @return Error code
|
---|
| 134 | */
|
---|
| 135 | void transfer_list_remove_batch(transfer_list_t *instance, batch_t *batch)
|
---|
[53338bda] | 136 | {
|
---|
| 137 | assert(instance);
|
---|
[83c439c] | 138 | assert(batch);
|
---|
[53338bda] | 139 | assert(instance->queue_head);
|
---|
[83c439c] | 140 | assert(batch->qh);
|
---|
[a7e2f0d] | 141 | usb_log_debug2(
|
---|
| 142 | "Removing batch(%p) from queue %s.\n", batch, instance->name);
|
---|
[7dd3318] | 143 |
|
---|
[e0df6c2] | 144 | if (batch->link.prev == &instance->batch_list) {
|
---|
[a7e2f0d] | 145 | /* I'm the first one here */
|
---|
| 146 | usb_log_debug(
|
---|
| 147 | "Batch(%p) removed (FIRST) from %s, next element %x.\n",
|
---|
| 148 | batch, instance->name, batch->qh->next_queue);
|
---|
[83c439c] | 149 | instance->queue_head->element = batch->qh->next_queue;
|
---|
[7dd3318] | 150 | } else {
|
---|
[a7e2f0d] | 151 | usb_log_debug(
|
---|
| 152 | "Batch(%p) removed (FIRST:NO) from %s, next element %x.\n",
|
---|
| 153 | batch, instance->name, batch->qh->next_queue);
|
---|
| 154 | batch_t *prev =
|
---|
| 155 | list_get_instance(batch->link.prev, batch_t, link);
|
---|
[83c439c] | 156 | prev->qh->next_queue = batch->qh->next_queue;
|
---|
[7dd3318] | 157 | }
|
---|
[83c439c] | 158 | list_remove(&batch->link);
|
---|
[7dd3318] | 159 | }
|
---|
| 160 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 161 | /** Checks list for finished transfers.
|
---|
| 162 | *
|
---|
| 163 | * @param[in] instance List to use.
|
---|
| 164 | * @return Error code
|
---|
| 165 | */
|
---|
[86c2ccd] | 166 | void transfer_list_remove_finished(transfer_list_t *instance)
|
---|
[7dd3318] | 167 | {
|
---|
| 168 | assert(instance);
|
---|
[86c2ccd] | 169 |
|
---|
| 170 | LIST_INITIALIZE(done);
|
---|
| 171 |
|
---|
[e0df6c2] | 172 | fibril_mutex_lock(&instance->guard);
|
---|
[83c439c] | 173 | link_t *current = instance->batch_list.next;
|
---|
| 174 | while (current != &instance->batch_list) {
|
---|
[7dd3318] | 175 | link_t *next = current->next;
|
---|
[83c439c] | 176 | batch_t *batch = list_get_instance(current, batch_t, link);
|
---|
[53338bda] | 177 |
|
---|
[83c439c] | 178 | if (batch_is_complete(batch)) {
|
---|
| 179 | transfer_list_remove_batch(instance, batch);
|
---|
[86c2ccd] | 180 | list_append(current, &done);
|
---|
[7dd3318] | 181 | }
|
---|
| 182 | current = next;
|
---|
[53338bda] | 183 | }
|
---|
[e0df6c2] | 184 | fibril_mutex_unlock(&instance->guard);
|
---|
[86c2ccd] | 185 |
|
---|
| 186 | while (!list_empty(&done)) {
|
---|
| 187 | link_t *item = done.next;
|
---|
| 188 | list_remove(item);
|
---|
| 189 | batch_t *batch = list_get_instance(item, batch_t, link);
|
---|
| 190 | batch->next_step(batch);
|
---|
| 191 | }
|
---|
[53338bda] | 192 | }
|
---|
[c56dbe0] | 193 | /**
|
---|
| 194 | * @}
|
---|
| 195 | */
|
---|