| 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 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/debug.h>
|
|---|
| 37 |
|
|---|
| 38 | #include "transfer_list.h"
|
|---|
| 39 |
|
|---|
| 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 | */
|
|---|
| 51 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
|---|
| 52 | {
|
|---|
| 53 | assert(instance);
|
|---|
| 54 | instance->next = NULL;
|
|---|
| 55 | instance->name = name;
|
|---|
| 56 | instance->queue_head = malloc32(sizeof(queue_head_t));
|
|---|
| 57 | if (!instance->queue_head) {
|
|---|
| 58 | usb_log_error("Failed to allocate queue head.\n");
|
|---|
| 59 | return ENOMEM;
|
|---|
| 60 | }
|
|---|
| 61 | instance->queue_head_pa = addr_to_phys(instance->queue_head);
|
|---|
| 62 |
|
|---|
| 63 | queue_head_init(instance->queue_head);
|
|---|
| 64 | list_initialize(&instance->batch_list);
|
|---|
| 65 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 66 | return EOK;
|
|---|
| 67 | }
|
|---|
| 68 | /*----------------------------------------------------------------------------*/
|
|---|
| 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 | */
|
|---|
| 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;
|
|---|
| 81 | queue_head_append_qh(instance->queue_head, next->queue_head_pa);
|
|---|
| 82 | instance->queue_head->element = instance->queue_head->next_queue;
|
|---|
| 83 | }
|
|---|
| 84 | /*----------------------------------------------------------------------------*/
|
|---|
| 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 | */
|
|---|
| 91 | void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
|
|---|
| 92 | {
|
|---|
| 93 | assert(instance);
|
|---|
| 94 | assert(batch);
|
|---|
| 95 | usb_log_debug2(
|
|---|
| 96 | "Adding batch(%p) to queue %s.\n", batch, instance->name);
|
|---|
| 97 |
|
|---|
| 98 | uint32_t pa = (uintptr_t)addr_to_phys(batch->qh);
|
|---|
| 99 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
|---|
| 100 | pa |= LINK_POINTER_QUEUE_HEAD_FLAG;
|
|---|
| 101 |
|
|---|
| 102 | batch->qh->next_queue = instance->queue_head->next_queue;
|
|---|
| 103 |
|
|---|
| 104 | fibril_mutex_lock(&instance->guard);
|
|---|
| 105 |
|
|---|
| 106 | if (instance->queue_head->element == instance->queue_head->next_queue) {
|
|---|
| 107 | /* there is nothing scheduled */
|
|---|
| 108 | list_append(&batch->link, &instance->batch_list);
|
|---|
| 109 | instance->queue_head->element = pa;
|
|---|
| 110 | usb_log_debug("Batch(%p) added to queue %s first.\n",
|
|---|
| 111 | batch, instance->name);
|
|---|
| 112 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 113 | return;
|
|---|
| 114 | }
|
|---|
| 115 | /* now we can be sure that there is someting scheduled */
|
|---|
| 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);
|
|---|
| 121 | queue_head_append_qh(last->qh, pa);
|
|---|
| 122 | list_append(&batch->link, &instance->batch_list);
|
|---|
| 123 |
|
|---|
| 124 | usb_log_debug("Batch(%p) added to queue %s last, first is %p.\n",
|
|---|
| 125 | batch, instance->name, first);
|
|---|
| 126 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 127 | }
|
|---|
| 128 | /*----------------------------------------------------------------------------*/
|
|---|
| 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)
|
|---|
| 136 | {
|
|---|
| 137 | assert(instance);
|
|---|
| 138 | assert(batch);
|
|---|
| 139 | assert(instance->queue_head);
|
|---|
| 140 | assert(batch->qh);
|
|---|
| 141 | usb_log_debug2(
|
|---|
| 142 | "Removing batch(%p) from queue %s.\n", batch, instance->name);
|
|---|
| 143 |
|
|---|
| 144 | if (batch->link.prev == &instance->batch_list) {
|
|---|
| 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);
|
|---|
| 149 | instance->queue_head->element = batch->qh->next_queue;
|
|---|
| 150 | } else {
|
|---|
| 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);
|
|---|
| 156 | prev->qh->next_queue = batch->qh->next_queue;
|
|---|
| 157 | }
|
|---|
| 158 | list_remove(&batch->link);
|
|---|
| 159 | }
|
|---|
| 160 | /*----------------------------------------------------------------------------*/
|
|---|
| 161 | /** Checks list for finished transfers.
|
|---|
| 162 | *
|
|---|
| 163 | * @param[in] instance List to use.
|
|---|
| 164 | * @return Error code
|
|---|
| 165 | */
|
|---|
| 166 | void transfer_list_remove_finished(transfer_list_t *instance)
|
|---|
| 167 | {
|
|---|
| 168 | assert(instance);
|
|---|
| 169 |
|
|---|
| 170 | LIST_INITIALIZE(done);
|
|---|
| 171 |
|
|---|
| 172 | fibril_mutex_lock(&instance->guard);
|
|---|
| 173 | link_t *current = instance->batch_list.next;
|
|---|
| 174 | while (current != &instance->batch_list) {
|
|---|
| 175 | link_t *next = current->next;
|
|---|
| 176 | batch_t *batch = list_get_instance(current, batch_t, link);
|
|---|
| 177 |
|
|---|
| 178 | if (batch_is_complete(batch)) {
|
|---|
| 179 | transfer_list_remove_batch(instance, batch);
|
|---|
| 180 | list_append(current, &done);
|
|---|
| 181 | }
|
|---|
| 182 | current = next;
|
|---|
| 183 | }
|
|---|
| 184 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 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 | }
|
|---|
| 192 | }
|
|---|
| 193 | /**
|
|---|
| 194 | * @}
|
|---|
| 195 | */
|
|---|