| 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 drvusbohci
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI 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->list_head = malloc32(sizeof(ed_t));
|
|---|
| 55 | if (!instance->list_head) {
|
|---|
| 56 | usb_log_error("Failed to allocate list head.\n");
|
|---|
| 57 | return ENOMEM;
|
|---|
| 58 | }
|
|---|
| 59 | instance->list_head_pa = addr_to_phys(instance->list_head);
|
|---|
| 60 | usb_log_debug2("Transfer list %s setup with ED: %p(%p).\n",
|
|---|
| 61 | name, instance->list_head, instance->list_head_pa);
|
|---|
| 62 |
|
|---|
| 63 | ed_init(instance->list_head, NULL);
|
|---|
| 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 | ed_append_ed(instance->list_head, next->list_head);
|
|---|
| 82 | }
|
|---|
| 83 | /*----------------------------------------------------------------------------*/
|
|---|
| 84 | /** Submit transfer batch to the list and queue.
|
|---|
| 85 | *
|
|---|
| 86 | * @param[in] instance List to use.
|
|---|
| 87 | * @param[in] batch Transfer batch to submit.
|
|---|
| 88 | * @return Error code
|
|---|
| 89 | *
|
|---|
| 90 | * The batch is added to the end of the list and queue.
|
|---|
| 91 | */
|
|---|
| 92 | void transfer_list_add_batch(
|
|---|
| 93 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 94 | {
|
|---|
| 95 | assert(instance);
|
|---|
| 96 | assert(batch);
|
|---|
| 97 | usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
|
|---|
| 98 |
|
|---|
| 99 | fibril_mutex_lock(&instance->guard);
|
|---|
| 100 |
|
|---|
| 101 | ed_t *last_ed = NULL;
|
|---|
| 102 | /* Add to the hardware queue. */
|
|---|
| 103 | if (list_empty(&instance->batch_list)) {
|
|---|
| 104 | /* There is nothing scheduled */
|
|---|
| 105 | last_ed = instance->list_head;
|
|---|
| 106 | } else {
|
|---|
| 107 | /* There is something scheduled */
|
|---|
| 108 | usb_transfer_batch_t *last = list_get_instance(
|
|---|
| 109 | instance->batch_list.prev, usb_transfer_batch_t, link);
|
|---|
| 110 | last_ed = batch_ed(last);
|
|---|
| 111 | }
|
|---|
| 112 | /* keep link */
|
|---|
| 113 | batch_ed(batch)->next = last_ed->next;
|
|---|
| 114 | ed_append_ed(last_ed, batch_ed(batch));
|
|---|
| 115 |
|
|---|
| 116 | asm volatile ("": : :"memory");
|
|---|
| 117 |
|
|---|
| 118 | /* Add to the driver list */
|
|---|
| 119 | list_append(&batch->link, &instance->batch_list);
|
|---|
| 120 |
|
|---|
| 121 | usb_transfer_batch_t *first = list_get_instance(
|
|---|
| 122 | instance->batch_list.next, usb_transfer_batch_t, link);
|
|---|
| 123 | usb_log_debug("Batch(%p) added to list %s, first is %p(%p).\n",
|
|---|
| 124 | batch, instance->name, first, batch_ed(first));
|
|---|
| 125 | if (last_ed == instance->list_head) {
|
|---|
| 126 | usb_log_debug2("%s head ED(%p-%p): %x:%x:%x:%x.\n",
|
|---|
| 127 | instance->name, last_ed, instance->list_head_pa,
|
|---|
| 128 | last_ed->status, last_ed->td_tail, last_ed->td_head,
|
|---|
| 129 | last_ed->next);
|
|---|
| 130 | }
|
|---|
| 131 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 132 | }
|
|---|
| 133 | /*----------------------------------------------------------------------------*/
|
|---|
| 134 | /** Create list for finished batches.
|
|---|
| 135 | *
|
|---|
| 136 | * @param[in] instance List to use.
|
|---|
| 137 | * @param[in] done list to fill
|
|---|
| 138 | */
|
|---|
| 139 | void transfer_list_remove_finished(transfer_list_t *instance, link_t *done)
|
|---|
| 140 | {
|
|---|
| 141 | assert(instance);
|
|---|
| 142 | assert(done);
|
|---|
| 143 |
|
|---|
| 144 | fibril_mutex_lock(&instance->guard);
|
|---|
| 145 | usb_log_debug2("Checking list %s for completed batches(%d).\n",
|
|---|
| 146 | instance->name, list_count(&instance->batch_list));
|
|---|
| 147 | link_t *current = instance->batch_list.next;
|
|---|
| 148 | while (current != &instance->batch_list) {
|
|---|
| 149 | link_t *next = current->next;
|
|---|
| 150 | usb_transfer_batch_t *batch =
|
|---|
| 151 | list_get_instance(current, usb_transfer_batch_t, link);
|
|---|
| 152 |
|
|---|
| 153 | if (batch_is_complete(batch)) {
|
|---|
| 154 | /* Save for post-processing */
|
|---|
| 155 | transfer_list_remove_batch(instance, batch);
|
|---|
| 156 | list_append(current, done);
|
|---|
| 157 | }
|
|---|
| 158 | current = next;
|
|---|
| 159 | }
|
|---|
| 160 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 161 | }
|
|---|
| 162 | /*----------------------------------------------------------------------------*/
|
|---|
| 163 | /** Walk the list and abort all batches.
|
|---|
| 164 | *
|
|---|
| 165 | * @param[in] instance List to use.
|
|---|
| 166 | */
|
|---|
| 167 | void transfer_list_abort_all(transfer_list_t *instance)
|
|---|
| 168 | {
|
|---|
| 169 | fibril_mutex_lock(&instance->guard);
|
|---|
| 170 | while (!list_empty(&instance->batch_list)) {
|
|---|
| 171 | link_t *current = instance->batch_list.next;
|
|---|
| 172 | usb_transfer_batch_t *batch =
|
|---|
| 173 | list_get_instance(current, usb_transfer_batch_t, link);
|
|---|
| 174 | transfer_list_remove_batch(instance, batch);
|
|---|
| 175 | usb_transfer_batch_finish_error(batch, EIO);
|
|---|
| 176 | }
|
|---|
| 177 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 178 | }
|
|---|
| 179 | /*----------------------------------------------------------------------------*/
|
|---|
| 180 | /** Remove a transfer batch from the list and queue.
|
|---|
| 181 | *
|
|---|
| 182 | * @param[in] instance List to use.
|
|---|
| 183 | * @param[in] batch Transfer batch to remove.
|
|---|
| 184 | * @return Error code
|
|---|
| 185 | *
|
|---|
| 186 | * Does not lock the transfer list, caller is responsible for that.
|
|---|
| 187 | */
|
|---|
| 188 | void transfer_list_remove_batch(
|
|---|
| 189 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 190 | {
|
|---|
| 191 | assert(instance);
|
|---|
| 192 | assert(instance->list_head);
|
|---|
| 193 | assert(batch);
|
|---|
| 194 | assert(batch_ed(batch));
|
|---|
| 195 | assert(fibril_mutex_is_locked(&instance->guard));
|
|---|
| 196 |
|
|---|
| 197 | usb_log_debug2(
|
|---|
| 198 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
|---|
| 199 |
|
|---|
| 200 | const char *qpos = NULL;
|
|---|
| 201 | /* Remove from the hardware queue */
|
|---|
| 202 | if (instance->batch_list.next == &batch->link) {
|
|---|
| 203 | /* I'm the first one here */
|
|---|
| 204 | assert((instance->list_head->next & ED_NEXT_PTR_MASK)
|
|---|
| 205 | == addr_to_phys(batch_ed(batch)));
|
|---|
| 206 | instance->list_head->next = batch_ed(batch)->next;
|
|---|
| 207 | qpos = "FIRST";
|
|---|
| 208 | } else {
|
|---|
| 209 | usb_transfer_batch_t *prev =
|
|---|
| 210 | list_get_instance(
|
|---|
| 211 | batch->link.prev, usb_transfer_batch_t, link);
|
|---|
| 212 | assert((batch_ed(prev)->next & ED_NEXT_PTR_MASK)
|
|---|
| 213 | == addr_to_phys(batch_ed(batch)));
|
|---|
| 214 | batch_ed(prev)->next = batch_ed(batch)->next;
|
|---|
| 215 | qpos = "NOT FIRST";
|
|---|
| 216 | }
|
|---|
| 217 | asm volatile ("": : :"memory");
|
|---|
| 218 | usb_log_debug("Batch(%p) removed (%s) from %s, next %x.\n",
|
|---|
| 219 | batch, qpos, instance->name, batch_ed(batch)->next);
|
|---|
| 220 |
|
|---|
| 221 | /* Remove from the batch list */
|
|---|
| 222 | list_remove(&batch->link);
|
|---|
| 223 | }
|
|---|
| 224 | /**
|
|---|
| 225 | * @}
|
|---|
| 226 | */
|
|---|