| [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 | */
|
|---|
| [17ceb72] | 28 | /** @addtogroup drvusbuhcihc
|
|---|
| [c56dbe0] | 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| [17ceb72] | 32 | * @brief UHCI driver transfer list implementation
|
|---|
| [c56dbe0] | 33 | */
|
|---|
| [89a0485a] | 34 | #include <errno.h>
|
|---|
| [afcd86e] | 35 | #include <usb/debug.h>
|
|---|
| [2aaf804] | 36 | #include <arch/barrier.h>
|
|---|
| [afcd86e] | 37 |
|
|---|
| [563ead9] | 38 |
|
|---|
| [89a0485a] | 39 | #include "transfer_list.h"
|
|---|
| [563ead9] | 40 | #include "batch.h"
|
|---|
| [89a0485a] | 41 |
|
|---|
| [a7e2f0d] | 42 | static void transfer_list_remove_batch(
|
|---|
| [1387692] | 43 | transfer_list_t *instance, usb_transfer_batch_t *batch);
|
|---|
| [a7e2f0d] | 44 | /*----------------------------------------------------------------------------*/
|
|---|
| [17ceb72] | 45 | /** Initialize transfer list structures.
|
|---|
| [a7e2f0d] | 46 | *
|
|---|
| 47 | * @param[in] instance Memory place to use.
|
|---|
| [17ceb72] | 48 | * @param[in] name Name of the new list.
|
|---|
| [a7e2f0d] | 49 | * @return Error code
|
|---|
| 50 | *
|
|---|
| [6143ce3] | 51 | * Allocates memory for internal qh_t structure.
|
|---|
| [a7e2f0d] | 52 | */
|
|---|
| [881c47b] | 53 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
|---|
| [89a0485a] | 54 | {
|
|---|
| 55 | assert(instance);
|
|---|
| [881c47b] | 56 | instance->name = name;
|
|---|
| [6143ce3] | 57 | instance->queue_head = malloc32(sizeof(qh_t));
|
|---|
| [89a0485a] | 58 | if (!instance->queue_head) {
|
|---|
| [afcd86e] | 59 | usb_log_error("Failed to allocate queue head.\n");
|
|---|
| [89a0485a] | 60 | return ENOMEM;
|
|---|
| 61 | }
|
|---|
| [e247d83] | 62 | const uint32_t queue_head_pa = addr_to_phys(instance->queue_head);
|
|---|
| [4125b7d] | 63 | usb_log_debug2("Transfer list %s setup with QH: %p (%#" PRIx32" ).\n",
|
|---|
| [4c70554] | 64 | name, instance->queue_head, queue_head_pa);
|
|---|
| [89a0485a] | 65 |
|
|---|
| [6143ce3] | 66 | qh_init(instance->queue_head);
|
|---|
| [83c439c] | 67 | list_initialize(&instance->batch_list);
|
|---|
| [e0df6c2] | 68 | fibril_mutex_initialize(&instance->guard);
|
|---|
| [89a0485a] | 69 | return EOK;
|
|---|
| 70 | }
|
|---|
| 71 | /*----------------------------------------------------------------------------*/
|
|---|
| [4c70554] | 72 | /** Dispose transfer list structures.
|
|---|
| 73 | *
|
|---|
| 74 | * @param[in] instance Memory place to use.
|
|---|
| 75 | *
|
|---|
| [d736fe38] | 76 | * Frees memory of the internal qh_t structure.
|
|---|
| [4c70554] | 77 | */
|
|---|
| 78 | void transfer_list_fini(transfer_list_t *instance)
|
|---|
| 79 | {
|
|---|
| 80 | assert(instance);
|
|---|
| 81 | free32(instance->queue_head);
|
|---|
| 82 | }
|
|---|
| [17ceb72] | 83 | /** Set the next list in transfer list chain.
|
|---|
| [a7e2f0d] | 84 | *
|
|---|
| 85 | * @param[in] instance List to lead.
|
|---|
| 86 | * @param[in] next List to append.
|
|---|
| 87 | * @return Error code
|
|---|
| [6143ce3] | 88 | *
|
|---|
| [17ceb72] | 89 | * Does not check whether this replaces an existing list .
|
|---|
| [a7e2f0d] | 90 | */
|
|---|
| [881c47b] | 91 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
|---|
| 92 | {
|
|---|
| 93 | assert(instance);
|
|---|
| [e247d83] | 94 | assert(instance->queue_head);
|
|---|
| [881c47b] | 95 | assert(next);
|
|---|
| [4c70554] | 96 | /* Set queue_head.next to point to the follower */
|
|---|
| 97 | qh_set_next_qh(instance->queue_head, next->queue_head);
|
|---|
| [881c47b] | 98 | }
|
|---|
| 99 | /*----------------------------------------------------------------------------*/
|
|---|
| [4c70554] | 100 | /** Add transfer batch to the list and queue.
|
|---|
| [a7e2f0d] | 101 | *
|
|---|
| 102 | * @param[in] instance List to use.
|
|---|
| 103 | * @param[in] batch Transfer batch to submit.
|
|---|
| [17ceb72] | 104 | *
|
|---|
| 105 | * The batch is added to the end of the list and queue.
|
|---|
| [a7e2f0d] | 106 | */
|
|---|
| [5bf82ee] | 107 | void transfer_list_add_batch(
|
|---|
| 108 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| [9a818a9] | 109 | {
|
|---|
| 110 | assert(instance);
|
|---|
| [83c439c] | 111 | assert(batch);
|
|---|
| [6143ce3] | 112 | usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
|
|---|
| [9a818a9] | 113 |
|
|---|
| [e0df6c2] | 114 | fibril_mutex_lock(&instance->guard);
|
|---|
| 115 |
|
|---|
| [13b9cb5] | 116 | qh_t *last_qh = NULL;
|
|---|
| [17ceb72] | 117 | /* Add to the hardware queue. */
|
|---|
| [6143ce3] | 118 | if (list_empty(&instance->batch_list)) {
|
|---|
| 119 | /* There is nothing scheduled */
|
|---|
| [13b9cb5] | 120 | last_qh = instance->queue_head;
|
|---|
| [6143ce3] | 121 | } else {
|
|---|
| 122 | /* There is something scheduled */
|
|---|
| [4c70554] | 123 | usb_transfer_batch_t *last =
|
|---|
| 124 | usb_transfer_batch_from_link(instance->batch_list.prev);
|
|---|
| [e099f26] | 125 | last_qh = batch_qh(last);
|
|---|
| [9a818a9] | 126 | }
|
|---|
| [c56c5b5b] | 127 | const uint32_t pa = addr_to_phys(batch_qh(batch));
|
|---|
| [13b9cb5] | 128 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
|---|
| 129 |
|
|---|
| [2aaf804] | 130 | /* Make sure all data in the batch are written */
|
|---|
| 131 | write_barrier();
|
|---|
| 132 |
|
|---|
| [e099f26] | 133 | /* keep link */
|
|---|
| 134 | batch_qh(batch)->next = last_qh->next;
|
|---|
| [4c70554] | 135 | qh_set_next_qh(last_qh, batch_qh(batch));
|
|---|
| [13b9cb5] | 136 |
|
|---|
| [2aaf804] | 137 | /* Make sure the pointer is updated */
|
|---|
| 138 | write_barrier();
|
|---|
| [001b152] | 139 |
|
|---|
| [e247d83] | 140 | /* Add to the driver's list */
|
|---|
| [83c439c] | 141 | list_append(&batch->link, &instance->batch_list);
|
|---|
| [a7e2f0d] | 142 |
|
|---|
| [1387692] | 143 | usb_transfer_batch_t *first = list_get_instance(
|
|---|
| 144 | instance->batch_list.next, usb_transfer_batch_t, link);
|
|---|
| [6143ce3] | 145 | usb_log_debug("Batch(%p) added to queue %s, first is %p.\n",
|
|---|
| [a7e2f0d] | 146 | batch, instance->name, first);
|
|---|
| [e0df6c2] | 147 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [9a818a9] | 148 | }
|
|---|
| [53338bda] | 149 | /*----------------------------------------------------------------------------*/
|
|---|
| [4c70554] | 150 | /** Add completed bantches to the provided list.
|
|---|
| [a7e2f0d] | 151 | *
|
|---|
| 152 | * @param[in] instance List to use.
|
|---|
| [4fd3faf] | 153 | * @param[in] done list to fill
|
|---|
| [a7e2f0d] | 154 | */
|
|---|
| [1585c7e] | 155 | void transfer_list_remove_finished(transfer_list_t *instance, link_t *done)
|
|---|
| [7dd3318] | 156 | {
|
|---|
| 157 | assert(instance);
|
|---|
| [1585c7e] | 158 | assert(done);
|
|---|
| [86c2ccd] | 159 |
|
|---|
| [e0df6c2] | 160 | fibril_mutex_lock(&instance->guard);
|
|---|
| [83c439c] | 161 | link_t *current = instance->batch_list.next;
|
|---|
| 162 | while (current != &instance->batch_list) {
|
|---|
| [e247d83] | 163 | link_t * const next = current->next;
|
|---|
| [5bf82ee] | 164 | usb_transfer_batch_t *batch =
|
|---|
| [4c70554] | 165 | usb_transfer_batch_from_link(current);
|
|---|
| [53338bda] | 166 |
|
|---|
| [83c439c] | 167 | if (batch_is_complete(batch)) {
|
|---|
| [4c70554] | 168 | /* Save for processing */
|
|---|
| [83c439c] | 169 | transfer_list_remove_batch(instance, batch);
|
|---|
| [1585c7e] | 170 | list_append(current, done);
|
|---|
| [7dd3318] | 171 | }
|
|---|
| 172 | current = next;
|
|---|
| [53338bda] | 173 | }
|
|---|
| [e0df6c2] | 174 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [53338bda] | 175 | }
|
|---|
| [a963a68] | 176 | /*----------------------------------------------------------------------------*/
|
|---|
| [4c70554] | 177 | /** Walk the list and finish all batches with EINTR.
|
|---|
| [a963a68] | 178 | *
|
|---|
| 179 | * @param[in] instance List to use.
|
|---|
| 180 | */
|
|---|
| 181 | void transfer_list_abort_all(transfer_list_t *instance)
|
|---|
| 182 | {
|
|---|
| 183 | fibril_mutex_lock(&instance->guard);
|
|---|
| [13b9cb5] | 184 | while (!list_empty(&instance->batch_list)) {
|
|---|
| [e247d83] | 185 | link_t * const current = instance->batch_list.next;
|
|---|
| [5bf82ee] | 186 | usb_transfer_batch_t *batch =
|
|---|
| [4c70554] | 187 | usb_transfer_batch_from_link(current);
|
|---|
| [a963a68] | 188 | transfer_list_remove_batch(instance, batch);
|
|---|
| [4c70554] | 189 | usb_transfer_batch_finish_error(batch, EINTR);
|
|---|
| [a963a68] | 190 | }
|
|---|
| 191 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 192 | }
|
|---|
| 193 | /*----------------------------------------------------------------------------*/
|
|---|
| 194 | /** Remove a transfer batch from the list and queue.
|
|---|
| 195 | *
|
|---|
| 196 | * @param[in] instance List to use.
|
|---|
| 197 | * @param[in] batch Transfer batch to remove.
|
|---|
| 198 | *
|
|---|
| 199 | * Does not lock the transfer list, caller is responsible for that.
|
|---|
| 200 | */
|
|---|
| [5bf82ee] | 201 | void transfer_list_remove_batch(
|
|---|
| 202 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| [a963a68] | 203 | {
|
|---|
| 204 | assert(instance);
|
|---|
| 205 | assert(instance->queue_head);
|
|---|
| [81dce9f] | 206 | assert(batch);
|
|---|
| 207 | assert(batch_qh(batch));
|
|---|
| [4fb6d9ee] | 208 | assert(fibril_mutex_is_locked(&instance->guard));
|
|---|
| 209 |
|
|---|
| [a963a68] | 210 | usb_log_debug2(
|
|---|
| 211 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
|---|
| 212 |
|
|---|
| [4fb6d9ee] | 213 | const char *qpos = NULL;
|
|---|
| [4c70554] | 214 | qh_t *prev_qh = NULL;
|
|---|
| [a963a68] | 215 | /* Remove from the hardware queue */
|
|---|
| [4fb6d9ee] | 216 | if (instance->batch_list.next == &batch->link) {
|
|---|
| [a963a68] | 217 | /* I'm the first one here */
|
|---|
| [4c70554] | 218 | prev_qh = instance->queue_head;
|
|---|
| [4fb6d9ee] | 219 | qpos = "FIRST";
|
|---|
| [a963a68] | 220 | } else {
|
|---|
| [4c70554] | 221 | /* The thing before me is a batch too */
|
|---|
| [1387692] | 222 | usb_transfer_batch_t *prev =
|
|---|
| [4c70554] | 223 | usb_transfer_batch_from_link(batch->link.prev);
|
|---|
| 224 | prev_qh = batch_qh(prev);
|
|---|
| [4fb6d9ee] | 225 | qpos = "NOT FIRST";
|
|---|
| [a963a68] | 226 | }
|
|---|
| [4c70554] | 227 | assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
|
|---|
| 228 | == addr_to_phys(batch_qh(batch)));
|
|---|
| 229 | prev_qh->next = batch_qh(batch)->next;
|
|---|
| [2aaf804] | 230 |
|
|---|
| 231 | /* Make sure the pointer is updated */
|
|---|
| 232 | write_barrier();
|
|---|
| 233 |
|
|---|
| [4fb6d9ee] | 234 | /* Remove from the batch list */
|
|---|
| [a963a68] | 235 | list_remove(&batch->link);
|
|---|
| [4c70554] | 236 | usb_log_debug("Batch(%p) removed (%s) from %s, next: %x.\n",
|
|---|
| [c56c5b5b] | 237 | batch, qpos, instance->name, batch_qh(batch)->next);
|
|---|
| [a963a68] | 238 | }
|
|---|
| [c56dbe0] | 239 | /**
|
|---|
| 240 | * @}
|
|---|
| 241 | */
|
|---|