| 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 |
|
|---|
| 29 | /** @addtogroup drvusbuhcihc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver transfer list implementation
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <libarch/barrier.h>
|
|---|
| 39 | #include <stdint.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 | #include <usb/host/usb_transfer_batch.h>
|
|---|
| 42 | #include <usb/host/utils/malloc32.h>
|
|---|
| 43 | #include <usb/host/utility.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "hw_struct/link_pointer.h"
|
|---|
| 46 | #include "transfer_list.h"
|
|---|
| 47 | #include "hc.h"
|
|---|
| 48 |
|
|---|
| 49 | /** Initialize transfer list structures.
|
|---|
| 50 | *
|
|---|
| 51 | * @param[in] instance Memory place to use.
|
|---|
| 52 | * @param[in] name Name of the new list.
|
|---|
| 53 | * @return Error code
|
|---|
| 54 | *
|
|---|
| 55 | * Allocates memory for internal qh_t structure.
|
|---|
| 56 | */
|
|---|
| 57 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
|---|
| 58 | {
|
|---|
| 59 | assert(instance);
|
|---|
| 60 | instance->name = name;
|
|---|
| 61 | instance->queue_head = malloc32(sizeof(qh_t));
|
|---|
| 62 | if (!instance->queue_head) {
|
|---|
| 63 | usb_log_error("Failed to allocate queue head.");
|
|---|
| 64 | return ENOMEM;
|
|---|
| 65 | }
|
|---|
| 66 | const uint32_t queue_head_pa = addr_to_phys(instance->queue_head);
|
|---|
| 67 | usb_log_debug2("Transfer list %s setup with QH: %p (%#" PRIx32" ).",
|
|---|
| 68 | name, instance->queue_head, queue_head_pa);
|
|---|
| 69 |
|
|---|
| 70 | qh_init(instance->queue_head);
|
|---|
| 71 | list_initialize(&instance->batch_list);
|
|---|
| 72 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 73 | return EOK;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Dispose transfer list structures.
|
|---|
| 77 | *
|
|---|
| 78 | * @param[in] instance Memory place to use.
|
|---|
| 79 | *
|
|---|
| 80 | * Frees memory of the internal qh_t structure.
|
|---|
| 81 | */
|
|---|
| 82 | void transfer_list_fini(transfer_list_t *instance)
|
|---|
| 83 | {
|
|---|
| 84 | assert(instance);
|
|---|
| 85 | free32(instance->queue_head);
|
|---|
| 86 | }
|
|---|
| 87 | /** Set the next list in transfer list chain.
|
|---|
| 88 | *
|
|---|
| 89 | * @param[in] instance List to lead.
|
|---|
| 90 | * @param[in] next List to append.
|
|---|
| 91 | * @return Error code
|
|---|
| 92 | *
|
|---|
| 93 | * Does not check whether this replaces an existing list .
|
|---|
| 94 | */
|
|---|
| 95 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
|---|
| 96 | {
|
|---|
| 97 | assert(instance);
|
|---|
| 98 | assert(instance->queue_head);
|
|---|
| 99 | assert(next);
|
|---|
| 100 | /* Set queue_head.next to point to the follower */
|
|---|
| 101 | qh_set_next_qh(instance->queue_head, next->queue_head);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /** Add transfer batch to the list and queue.
|
|---|
| 105 | *
|
|---|
| 106 | * @param[in] instance List to use.
|
|---|
| 107 | * @param[in] batch Transfer batch to submit.
|
|---|
| 108 | *
|
|---|
| 109 | * The batch is added to the end of the list and queue.
|
|---|
| 110 | */
|
|---|
| 111 | void transfer_list_add_batch(
|
|---|
| 112 | transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
|
|---|
| 113 | {
|
|---|
| 114 | assert(instance);
|
|---|
| 115 | assert(uhci_batch);
|
|---|
| 116 |
|
|---|
| 117 | endpoint_t *ep = uhci_batch->base.ep;
|
|---|
| 118 |
|
|---|
| 119 | /* First, wait until the endpoint is free to use */
|
|---|
| 120 | fibril_mutex_lock(&ep->guard);
|
|---|
| 121 | endpoint_activate_locked(ep, &uhci_batch->base);
|
|---|
| 122 | fibril_mutex_unlock(&ep->guard);
|
|---|
| 123 |
|
|---|
| 124 | usb_log_debug2("Batch %p adding to queue %s.",
|
|---|
| 125 | uhci_batch, instance->name);
|
|---|
| 126 |
|
|---|
| 127 | fibril_mutex_lock(&instance->guard);
|
|---|
| 128 |
|
|---|
| 129 | /* Assume there is nothing scheduled */
|
|---|
| 130 | qh_t *last_qh = instance->queue_head;
|
|---|
| 131 | /* There is something scheduled */
|
|---|
| 132 | if (!list_empty(&instance->batch_list)) {
|
|---|
| 133 | last_qh = uhci_transfer_batch_from_link(
|
|---|
| 134 | list_last(&instance->batch_list))->qh;
|
|---|
| 135 | }
|
|---|
| 136 | /* Add to the hardware queue. */
|
|---|
| 137 | const uint32_t pa = addr_to_phys(uhci_batch->qh);
|
|---|
| 138 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
|---|
| 139 |
|
|---|
| 140 | /* Make sure all data in the batch are written */
|
|---|
| 141 | write_barrier();
|
|---|
| 142 |
|
|---|
| 143 | /* keep link */
|
|---|
| 144 | uhci_batch->qh->next = last_qh->next;
|
|---|
| 145 | qh_set_next_qh(last_qh, uhci_batch->qh);
|
|---|
| 146 |
|
|---|
| 147 | /* Make sure the pointer is updated */
|
|---|
| 148 | write_barrier();
|
|---|
| 149 |
|
|---|
| 150 | /* Add to the driver's list */
|
|---|
| 151 | list_append(&uhci_batch->link, &instance->batch_list);
|
|---|
| 152 |
|
|---|
| 153 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
|
|---|
| 154 | " scheduled in queue %s.", uhci_batch,
|
|---|
| 155 | USB_TRANSFER_BATCH_ARGS(uhci_batch->base), instance->name);
|
|---|
| 156 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Reset toggle on endpoint callback.
|
|---|
| 161 | */
|
|---|
| 162 | static void uhci_reset_toggle(endpoint_t *ep)
|
|---|
| 163 | {
|
|---|
| 164 | uhci_endpoint_t *uhci_ep = (uhci_endpoint_t *) ep;
|
|---|
| 165 | uhci_ep->toggle = 0;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /** Add completed batches to the provided list.
|
|---|
| 169 | *
|
|---|
| 170 | * @param[in] instance List to use.
|
|---|
| 171 | * @param[in] done list to fill
|
|---|
| 172 | */
|
|---|
| 173 | void transfer_list_remove_finished(transfer_list_t *instance, list_t *done)
|
|---|
| 174 | {
|
|---|
| 175 | assert(instance);
|
|---|
| 176 | assert(done);
|
|---|
| 177 |
|
|---|
| 178 | fibril_mutex_lock(&instance->guard);
|
|---|
| 179 | link_t *current = list_first(&instance->batch_list);
|
|---|
| 180 | while (current && current != &instance->batch_list.head) {
|
|---|
| 181 | link_t * const next = current->next;
|
|---|
| 182 | uhci_transfer_batch_t *batch =
|
|---|
| 183 | uhci_transfer_batch_from_link(current);
|
|---|
| 184 |
|
|---|
| 185 | if (uhci_transfer_batch_check_completed(batch)) {
|
|---|
| 186 | /* Remove from schedule, save for processing */
|
|---|
| 187 | fibril_mutex_lock(&batch->base.ep->guard);
|
|---|
| 188 | assert(batch->base.ep->active_batch == &batch->base);
|
|---|
| 189 | hc_reset_toggles(&batch->base, &uhci_reset_toggle);
|
|---|
| 190 | endpoint_deactivate_locked(batch->base.ep);
|
|---|
| 191 | transfer_list_remove_batch(instance, batch);
|
|---|
| 192 | fibril_mutex_unlock(&batch->base.ep->guard);
|
|---|
| 193 |
|
|---|
| 194 | list_append(current, done);
|
|---|
| 195 | }
|
|---|
| 196 | current = next;
|
|---|
| 197 | }
|
|---|
| 198 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Walk the list and finish all batches with EINTR.
|
|---|
| 202 | *
|
|---|
| 203 | * @param[in] instance List to use.
|
|---|
| 204 | */
|
|---|
| 205 | void transfer_list_abort_all(transfer_list_t *instance)
|
|---|
| 206 | {
|
|---|
| 207 | fibril_mutex_lock(&instance->guard);
|
|---|
| 208 | while (!list_empty(&instance->batch_list)) {
|
|---|
| 209 | link_t * const current = list_first(&instance->batch_list);
|
|---|
| 210 | uhci_transfer_batch_t *batch = uhci_transfer_batch_from_link(current);
|
|---|
| 211 | transfer_list_remove_batch(instance, batch);
|
|---|
| 212 | }
|
|---|
| 213 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /** Remove a transfer batch from the list and queue.
|
|---|
| 217 | *
|
|---|
| 218 | * @param[in] instance List to use.
|
|---|
| 219 | * @param[in] batch Transfer batch to remove.
|
|---|
| 220 | *
|
|---|
| 221 | * Does not lock the transfer list, caller is responsible for that.
|
|---|
| 222 | */
|
|---|
| 223 | void transfer_list_remove_batch(
|
|---|
| 224 | transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
|
|---|
| 225 | {
|
|---|
| 226 | assert(instance);
|
|---|
| 227 | assert(instance->queue_head);
|
|---|
| 228 | assert(uhci_batch);
|
|---|
| 229 | assert(uhci_batch->qh);
|
|---|
| 230 | assert(fibril_mutex_is_locked(&instance->guard));
|
|---|
| 231 | assert(!list_empty(&instance->batch_list));
|
|---|
| 232 |
|
|---|
| 233 | usb_log_debug2("Batch %p removing from queue %s.",
|
|---|
| 234 | uhci_batch, instance->name);
|
|---|
| 235 |
|
|---|
| 236 | /* Assume I'm the first */
|
|---|
| 237 | const char *qpos = "FIRST";
|
|---|
| 238 | qh_t *prev_qh = instance->queue_head;
|
|---|
| 239 | /* Remove from the hardware queue */
|
|---|
| 240 | if (list_first(&instance->batch_list) != &uhci_batch->link) {
|
|---|
| 241 | /* There is a batch in front of me */
|
|---|
| 242 | prev_qh =
|
|---|
| 243 | uhci_transfer_batch_from_link(uhci_batch->link.prev)->qh;
|
|---|
| 244 | qpos = "NOT FIRST";
|
|---|
| 245 | }
|
|---|
| 246 | assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
|
|---|
| 247 | == addr_to_phys(uhci_batch->qh));
|
|---|
| 248 | prev_qh->next = uhci_batch->qh->next;
|
|---|
| 249 |
|
|---|
| 250 | /* Make sure the pointer is updated */
|
|---|
| 251 | write_barrier();
|
|---|
| 252 |
|
|---|
| 253 | /* Remove from the batch list */
|
|---|
| 254 | list_remove(&uhci_batch->link);
|
|---|
| 255 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
|
|---|
| 256 | "from %s, next: %x.", uhci_batch,
|
|---|
| 257 | USB_TRANSFER_BATCH_ARGS(uhci_batch->base),
|
|---|
| 258 | qpos, instance->name, uhci_batch->qh->next);
|
|---|
| 259 | }
|
|---|
| 260 | /**
|
|---|
| 261 | * @}
|
|---|
| 262 | */
|
|---|