| 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 | instance->queue_head_pa = addr_to_phys(instance->queue_head);
|
|---|
| 60 | usb_log_debug2("Transfer list %s setup with QH: %p(%p).\n",
|
|---|
| 61 | name, instance->queue_head, instance->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 | /** Set the next list in transfer list chain.
|
|---|
| 70 | *
|
|---|
| 71 | * @param[in] instance List to lead.
|
|---|
| 72 | * @param[in] next List to append.
|
|---|
| 73 | * @return Error code
|
|---|
| 74 | *
|
|---|
| 75 | * Does not check whether this replaces an existing list .
|
|---|
| 76 | */
|
|---|
| 77 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
|---|
| 78 | {
|
|---|
| 79 | assert(instance);
|
|---|
| 80 | assert(next);
|
|---|
| 81 | if (!instance->queue_head)
|
|---|
| 82 | return;
|
|---|
| 83 | /* Set both queue_head.next to point to the follower */
|
|---|
| 84 | qh_set_next_qh(instance->queue_head, next->queue_head_pa);
|
|---|
| 85 | }
|
|---|
| 86 | /*----------------------------------------------------------------------------*/
|
|---|
| 87 | /** Submit transfer batch to the list and queue.
|
|---|
| 88 | *
|
|---|
| 89 | * @param[in] instance List to use.
|
|---|
| 90 | * @param[in] batch Transfer batch to submit.
|
|---|
| 91 | * @return Error code
|
|---|
| 92 | *
|
|---|
| 93 | * The batch is added to the end of the list and queue.
|
|---|
| 94 | */
|
|---|
| 95 | void transfer_list_add_batch(
|
|---|
| 96 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 97 | {
|
|---|
| 98 | assert(instance);
|
|---|
| 99 | assert(batch);
|
|---|
| 100 | usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
|
|---|
| 101 |
|
|---|
| 102 | fibril_mutex_lock(&instance->guard);
|
|---|
| 103 |
|
|---|
| 104 | qh_t *last_qh = NULL;
|
|---|
| 105 | /* Add to the hardware queue. */
|
|---|
| 106 | if (list_empty(&instance->batch_list)) {
|
|---|
| 107 | /* There is nothing scheduled */
|
|---|
| 108 | last_qh = instance->queue_head;
|
|---|
| 109 | } else {
|
|---|
| 110 | /* There is something scheduled */
|
|---|
| 111 | usb_transfer_batch_t *last = list_get_instance(
|
|---|
| 112 | instance->batch_list.prev, usb_transfer_batch_t, link);
|
|---|
| 113 | last_qh = batch_qh(last);
|
|---|
| 114 | }
|
|---|
| 115 | const uint32_t pa = addr_to_phys(batch_qh(batch));
|
|---|
| 116 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
|---|
| 117 |
|
|---|
| 118 | /* keep link */
|
|---|
| 119 | batch_qh(batch)->next = last_qh->next;
|
|---|
| 120 | qh_set_next_qh(last_qh, pa);
|
|---|
| 121 |
|
|---|
| 122 | asm volatile ("": : :"memory");
|
|---|
| 123 |
|
|---|
| 124 | /* Add to the driver list */
|
|---|
| 125 | list_append(&batch->link, &instance->batch_list);
|
|---|
| 126 |
|
|---|
| 127 | usb_transfer_batch_t *first = list_get_instance(
|
|---|
| 128 | instance->batch_list.next, usb_transfer_batch_t, link);
|
|---|
| 129 | usb_log_debug("Batch(%p) added to queue %s, first is %p.\n",
|
|---|
| 130 | batch, instance->name, first);
|
|---|
| 131 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 132 | }
|
|---|
| 133 | /*----------------------------------------------------------------------------*/
|
|---|
| 134 | /** Check list for finished batches.
|
|---|
| 135 | *
|
|---|
| 136 | * @param[in] instance List to use.
|
|---|
| 137 | * @return Error code
|
|---|
| 138 | *
|
|---|
| 139 | * Creates a local list of finished batches and calls next_step on each and
|
|---|
| 140 | * every one. This is safer because next_step may theoretically access
|
|---|
| 141 | * this transfer list leading to the deadlock if its done inline.
|
|---|
| 142 | */
|
|---|
| 143 | void transfer_list_remove_finished(transfer_list_t *instance, link_t *done)
|
|---|
| 144 | {
|
|---|
| 145 | assert(instance);
|
|---|
| 146 | assert(done);
|
|---|
| 147 |
|
|---|
| 148 | fibril_mutex_lock(&instance->guard);
|
|---|
| 149 | link_t *current = instance->batch_list.next;
|
|---|
| 150 | while (current != &instance->batch_list) {
|
|---|
| 151 | link_t *next = current->next;
|
|---|
| 152 | usb_transfer_batch_t *batch =
|
|---|
| 153 | list_get_instance(current, usb_transfer_batch_t, link);
|
|---|
| 154 |
|
|---|
| 155 | if (batch_is_complete(batch)) {
|
|---|
| 156 | /* Save for post-processing */
|
|---|
| 157 | transfer_list_remove_batch(instance, batch);
|
|---|
| 158 | list_append(current, done);
|
|---|
| 159 | }
|
|---|
| 160 | current = next;
|
|---|
| 161 | }
|
|---|
| 162 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 163 |
|
|---|
| 164 | }
|
|---|
| 165 | /*----------------------------------------------------------------------------*/
|
|---|
| 166 | /** Walk the list and abort all batches.
|
|---|
| 167 | *
|
|---|
| 168 | * @param[in] instance List to use.
|
|---|
| 169 | */
|
|---|
| 170 | void transfer_list_abort_all(transfer_list_t *instance)
|
|---|
| 171 | {
|
|---|
| 172 | fibril_mutex_lock(&instance->guard);
|
|---|
| 173 | while (!list_empty(&instance->batch_list)) {
|
|---|
| 174 | link_t *current = instance->batch_list.next;
|
|---|
| 175 | usb_transfer_batch_t *batch =
|
|---|
| 176 | list_get_instance(current, usb_transfer_batch_t, link);
|
|---|
| 177 | transfer_list_remove_batch(instance, batch);
|
|---|
| 178 | usb_transfer_batch_finish(batch, EIO);
|
|---|
| 179 | }
|
|---|
| 180 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 181 | }
|
|---|
| 182 | /*----------------------------------------------------------------------------*/
|
|---|
| 183 | /** Remove a transfer batch from the list and queue.
|
|---|
| 184 | *
|
|---|
| 185 | * @param[in] instance List to use.
|
|---|
| 186 | * @param[in] batch Transfer batch to remove.
|
|---|
| 187 | * @return Error code
|
|---|
| 188 | *
|
|---|
| 189 | * Does not lock the transfer list, caller is responsible for that.
|
|---|
| 190 | */
|
|---|
| 191 | void transfer_list_remove_batch(
|
|---|
| 192 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 193 | {
|
|---|
| 194 | assert(instance);
|
|---|
| 195 | assert(instance->queue_head);
|
|---|
| 196 | assert(batch);
|
|---|
| 197 | assert(batch_qh(batch));
|
|---|
| 198 | assert(fibril_mutex_is_locked(&instance->guard));
|
|---|
| 199 |
|
|---|
| 200 | usb_log_debug2(
|
|---|
| 201 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
|---|
| 202 |
|
|---|
| 203 | const char *qpos = NULL;
|
|---|
| 204 | /* Remove from the hardware queue */
|
|---|
| 205 | if (instance->batch_list.next == &batch->link) {
|
|---|
| 206 | /* I'm the first one here */
|
|---|
| 207 | assert((instance->queue_head->next & LINK_POINTER_ADDRESS_MASK)
|
|---|
| 208 | == addr_to_phys(batch_qh(batch)));
|
|---|
| 209 | instance->queue_head->next = batch_qh(batch)->next;
|
|---|
| 210 | qpos = "FIRST";
|
|---|
| 211 | } else {
|
|---|
| 212 | usb_transfer_batch_t *prev =
|
|---|
| 213 | list_get_instance(
|
|---|
| 214 | batch->link.prev, usb_transfer_batch_t, link);
|
|---|
| 215 | assert((batch_qh(prev)->next & LINK_POINTER_ADDRESS_MASK)
|
|---|
| 216 | == addr_to_phys(batch_qh(batch)));
|
|---|
| 217 | batch_qh(prev)->next = batch_qh(batch)->next;
|
|---|
| 218 | qpos = "NOT FIRST";
|
|---|
| 219 | }
|
|---|
| 220 | asm volatile ("": : :"memory");
|
|---|
| 221 | /* Remove from the batch list */
|
|---|
| 222 | list_remove(&batch->link);
|
|---|
| 223 | usb_log_debug("Batch(%p) removed (%s) from %s, next %x.\n",
|
|---|
| 224 | batch, qpos, instance->name, batch_qh(batch)->next);
|
|---|
| 225 | }
|
|---|
| 226 | /**
|
|---|
| 227 | * @}
|
|---|
| 228 | */
|
|---|