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