[c56dbe0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek
|
---|
[c56dbe0] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
[c0699467] | 29 |
|
---|
[c8ea6eca] | 30 | /** @addtogroup drvusbuhci
|
---|
[c56dbe0] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
[17ceb72] | 34 | * @brief UHCI driver transfer list implementation
|
---|
[c56dbe0] | 35 | */
|
---|
[c0699467] | 36 |
|
---|
[8064c2f6] | 37 | #include <assert.h>
|
---|
[89a0485a] | 38 | #include <errno.h>
|
---|
[05882233] | 39 | #include <barrier.h>
|
---|
[8d2dd7f2] | 40 | #include <stdint.h>
|
---|
[8064c2f6] | 41 | #include <usb/debug.h>
|
---|
| 42 | #include <usb/host/usb_transfer_batch.h>
|
---|
[8fc61c8] | 43 | #include <usb/host/utils/malloc32.h>
|
---|
[c6f82e5] | 44 | #include <usb/host/utility.h>
|
---|
[07f49ae] | 45 |
|
---|
[8064c2f6] | 46 | #include "hw_struct/link_pointer.h"
|
---|
[89a0485a] | 47 | #include "transfer_list.h"
|
---|
[c6f82e5] | 48 | #include "hc.h"
|
---|
[89a0485a] | 49 |
|
---|
[17ceb72] | 50 | /** Initialize transfer list structures.
|
---|
[a7e2f0d] | 51 | *
|
---|
| 52 | * @param[in] instance Memory place to use.
|
---|
[17ceb72] | 53 | * @param[in] name Name of the new list.
|
---|
[a7e2f0d] | 54 | * @return Error code
|
---|
| 55 | *
|
---|
[6143ce3] | 56 | * Allocates memory for internal qh_t structure.
|
---|
[a7e2f0d] | 57 | */
|
---|
[5a6cc679] | 58 | errno_t transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
[89a0485a] | 59 | {
|
---|
| 60 | assert(instance);
|
---|
[881c47b] | 61 | instance->name = name;
|
---|
[6143ce3] | 62 | instance->queue_head = malloc32(sizeof(qh_t));
|
---|
[89a0485a] | 63 | if (!instance->queue_head) {
|
---|
[a1732929] | 64 | usb_log_error("Failed to allocate queue head.");
|
---|
[89a0485a] | 65 | return ENOMEM;
|
---|
| 66 | }
|
---|
[e247d83] | 67 | const uint32_t queue_head_pa = addr_to_phys(instance->queue_head);
|
---|
[3bacee1] | 68 | usb_log_debug2("Transfer list %s setup with QH: %p (%#" PRIx32 " ).",
|
---|
[4c70554] | 69 | name, instance->queue_head, queue_head_pa);
|
---|
[89a0485a] | 70 |
|
---|
[6143ce3] | 71 | qh_init(instance->queue_head);
|
---|
[83c439c] | 72 | list_initialize(&instance->batch_list);
|
---|
[e0df6c2] | 73 | fibril_mutex_initialize(&instance->guard);
|
---|
[89a0485a] | 74 | return EOK;
|
---|
| 75 | }
|
---|
[76fbd9a] | 76 |
|
---|
[4c70554] | 77 | /** Dispose transfer list structures.
|
---|
| 78 | *
|
---|
| 79 | * @param[in] instance Memory place to use.
|
---|
| 80 | *
|
---|
[d736fe38] | 81 | * Frees memory of the internal qh_t structure.
|
---|
[4c70554] | 82 | */
|
---|
| 83 | void transfer_list_fini(transfer_list_t *instance)
|
---|
| 84 | {
|
---|
| 85 | assert(instance);
|
---|
| 86 | free32(instance->queue_head);
|
---|
| 87 | }
|
---|
[17ceb72] | 88 | /** Set the next list in transfer list chain.
|
---|
[a7e2f0d] | 89 | *
|
---|
| 90 | * @param[in] instance List to lead.
|
---|
| 91 | * @param[in] next List to append.
|
---|
| 92 | * @return Error code
|
---|
[6143ce3] | 93 | *
|
---|
[17ceb72] | 94 | * Does not check whether this replaces an existing list .
|
---|
[a7e2f0d] | 95 | */
|
---|
[881c47b] | 96 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
| 97 | {
|
---|
| 98 | assert(instance);
|
---|
[e247d83] | 99 | assert(instance->queue_head);
|
---|
[881c47b] | 100 | assert(next);
|
---|
[4c70554] | 101 | /* Set queue_head.next to point to the follower */
|
---|
| 102 | qh_set_next_qh(instance->queue_head, next->queue_head);
|
---|
[881c47b] | 103 | }
|
---|
[76fbd9a] | 104 |
|
---|
[4db49344] | 105 | /**
|
---|
| 106 | * Add transfer batch to the list and queue.
|
---|
[17ceb72] | 107 | *
|
---|
| 108 | * The batch is added to the end of the list and queue.
|
---|
[4db49344] | 109 | *
|
---|
| 110 | * @param[in] instance List to use.
|
---|
| 111 | * @param[in] batch Transfer batch to submit. After return, the batch must
|
---|
| 112 | * not be used further.
|
---|
[a7e2f0d] | 113 | */
|
---|
[4db49344] | 114 | int transfer_list_add_batch(
|
---|
[b991d37] | 115 | transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
|
---|
[9a818a9] | 116 | {
|
---|
| 117 | assert(instance);
|
---|
[b991d37] | 118 | assert(uhci_batch);
|
---|
[17873ac7] | 119 |
|
---|
| 120 | endpoint_t *ep = uhci_batch->base.ep;
|
---|
| 121 |
|
---|
[4db49344] | 122 | fibril_mutex_lock(&instance->guard);
|
---|
| 123 |
|
---|
| 124 | const int err = endpoint_activate_locked(ep, &uhci_batch->base);
|
---|
| 125 | if (err) {
|
---|
| 126 | fibril_mutex_unlock(&instance->guard);
|
---|
| 127 | return err;
|
---|
| 128 | }
|
---|
[17873ac7] | 129 |
|
---|
[a1732929] | 130 | usb_log_debug2("Batch %p adding to queue %s.",
|
---|
[58ac3ec] | 131 | uhci_batch, instance->name);
|
---|
[9a818a9] | 132 |
|
---|
[b991d37] | 133 | /* Assume there is nothing scheduled */
|
---|
| 134 | qh_t *last_qh = instance->queue_head;
|
---|
| 135 | /* There is something scheduled */
|
---|
| 136 | if (!list_empty(&instance->batch_list)) {
|
---|
| 137 | last_qh = uhci_transfer_batch_from_link(
|
---|
| 138 | list_last(&instance->batch_list))->qh;
|
---|
[9a818a9] | 139 | }
|
---|
[b991d37] | 140 | /* Add to the hardware queue. */
|
---|
| 141 | const uint32_t pa = addr_to_phys(uhci_batch->qh);
|
---|
[13b9cb5] | 142 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
| 143 |
|
---|
[2aaf804] | 144 | /* Make sure all data in the batch are written */
|
---|
| 145 | write_barrier();
|
---|
| 146 |
|
---|
[e099f26] | 147 | /* keep link */
|
---|
[b991d37] | 148 | uhci_batch->qh->next = last_qh->next;
|
---|
| 149 | qh_set_next_qh(last_qh, uhci_batch->qh);
|
---|
[13b9cb5] | 150 |
|
---|
[2aaf804] | 151 | /* Make sure the pointer is updated */
|
---|
| 152 | write_barrier();
|
---|
[001b152] | 153 |
|
---|
[e247d83] | 154 | /* Add to the driver's list */
|
---|
[b991d37] | 155 | list_append(&uhci_batch->link, &instance->batch_list);
|
---|
[a7e2f0d] | 156 |
|
---|
[b64fbc9] | 157 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
|
---|
[a1732929] | 158 | " scheduled in queue %s.", uhci_batch,
|
---|
[58ac3ec] | 159 | USB_TRANSFER_BATCH_ARGS(uhci_batch->base), instance->name);
|
---|
[e0df6c2] | 160 | fibril_mutex_unlock(&instance->guard);
|
---|
[4db49344] | 161 | return EOK;
|
---|
[9a818a9] | 162 | }
|
---|
[76fbd9a] | 163 |
|
---|
[c6f82e5] | 164 | /**
|
---|
| 165 | * Reset toggle on endpoint callback.
|
---|
| 166 | */
|
---|
| 167 | static void uhci_reset_toggle(endpoint_t *ep)
|
---|
| 168 | {
|
---|
| 169 | uhci_endpoint_t *uhci_ep = (uhci_endpoint_t *) ep;
|
---|
| 170 | uhci_ep->toggle = 0;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[b72efe8] | 173 | /** Add completed batches to the provided list.
|
---|
[a7e2f0d] | 174 | *
|
---|
| 175 | * @param[in] instance List to use.
|
---|
[4fd3faf] | 176 | * @param[in] done list to fill
|
---|
[a7e2f0d] | 177 | */
|
---|
[4db49344] | 178 | void transfer_list_check_finished(transfer_list_t *instance)
|
---|
[7dd3318] | 179 | {
|
---|
| 180 | assert(instance);
|
---|
[86c2ccd] | 181 |
|
---|
[e0df6c2] | 182 | fibril_mutex_lock(&instance->guard);
|
---|
[4db49344] | 183 | list_foreach_safe(instance->batch_list, current, next) {
|
---|
| 184 | uhci_transfer_batch_t *batch = uhci_transfer_batch_from_link(current);
|
---|
[53338bda] | 185 |
|
---|
[5fd9c30] | 186 | if (uhci_transfer_batch_check_completed(batch)) {
|
---|
[2755a622] | 187 | assert(batch->base.ep->active_batch == &batch->base);
|
---|
| 188 | endpoint_deactivate_locked(batch->base.ep);
|
---|
[4db49344] | 189 | hc_reset_toggles(&batch->base, &uhci_reset_toggle);
|
---|
[83c439c] | 190 | transfer_list_remove_batch(instance, batch);
|
---|
[4db49344] | 191 | usb_transfer_batch_finish(&batch->base);
|
---|
[7dd3318] | 192 | }
|
---|
[53338bda] | 193 | }
|
---|
[e0df6c2] | 194 | fibril_mutex_unlock(&instance->guard);
|
---|
[53338bda] | 195 | }
|
---|
[76fbd9a] | 196 |
|
---|
[4c70554] | 197 | /** Walk the list and finish all batches with EINTR.
|
---|
[a963a68] | 198 | *
|
---|
| 199 | * @param[in] instance List to use.
|
---|
| 200 | */
|
---|
| 201 | void transfer_list_abort_all(transfer_list_t *instance)
|
---|
| 202 | {
|
---|
| 203 | fibril_mutex_lock(&instance->guard);
|
---|
[13b9cb5] | 204 | while (!list_empty(&instance->batch_list)) {
|
---|
[3bacee1] | 205 | link_t *const current = list_first(&instance->batch_list);
|
---|
[0892663a] | 206 | uhci_transfer_batch_t *batch = uhci_transfer_batch_from_link(current);
|
---|
[a963a68] | 207 | transfer_list_remove_batch(instance, batch);
|
---|
[5dfb70c9] | 208 | }
|
---|
| 209 | fibril_mutex_unlock(&instance->guard);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[a963a68] | 212 | /** Remove a transfer batch from the list and queue.
|
---|
| 213 | *
|
---|
| 214 | * @param[in] instance List to use.
|
---|
| 215 | * @param[in] batch Transfer batch to remove.
|
---|
| 216 | *
|
---|
| 217 | * Does not lock the transfer list, caller is responsible for that.
|
---|
| 218 | */
|
---|
[5bf82ee] | 219 | void transfer_list_remove_batch(
|
---|
[b991d37] | 220 | transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
|
---|
[a963a68] | 221 | {
|
---|
| 222 | assert(instance);
|
---|
| 223 | assert(instance->queue_head);
|
---|
[b991d37] | 224 | assert(uhci_batch);
|
---|
| 225 | assert(uhci_batch->qh);
|
---|
[4fb6d9ee] | 226 | assert(fibril_mutex_is_locked(&instance->guard));
|
---|
[2755a622] | 227 | assert(!list_empty(&instance->batch_list));
|
---|
[4fb6d9ee] | 228 |
|
---|
[a1732929] | 229 | usb_log_debug2("Batch %p removing from queue %s.",
|
---|
[58ac3ec] | 230 | uhci_batch, instance->name);
|
---|
[a963a68] | 231 |
|
---|
[b991d37] | 232 | /* Assume I'm the first */
|
---|
| 233 | const char *qpos = "FIRST";
|
---|
| 234 | qh_t *prev_qh = instance->queue_head;
|
---|
[a963a68] | 235 | /* Remove from the hardware queue */
|
---|
[b991d37] | 236 | if (list_first(&instance->batch_list) != &uhci_batch->link) {
|
---|
| 237 | /* There is a batch in front of me */
|
---|
| 238 | prev_qh =
|
---|
| 239 | uhci_transfer_batch_from_link(uhci_batch->link.prev)->qh;
|
---|
[4fb6d9ee] | 240 | qpos = "NOT FIRST";
|
---|
[a963a68] | 241 | }
|
---|
[3bacee1] | 242 | assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK) ==
|
---|
| 243 | addr_to_phys(uhci_batch->qh));
|
---|
[b991d37] | 244 | prev_qh->next = uhci_batch->qh->next;
|
---|
[2aaf804] | 245 |
|
---|
| 246 | /* Make sure the pointer is updated */
|
---|
| 247 | write_barrier();
|
---|
| 248 |
|
---|
[4fb6d9ee] | 249 | /* Remove from the batch list */
|
---|
[b991d37] | 250 | list_remove(&uhci_batch->link);
|
---|
[c4fb5ecd] | 251 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
|
---|
[a1732929] | 252 | "from %s, next: %x.", uhci_batch,
|
---|
[58ac3ec] | 253 | USB_TRANSFER_BATCH_ARGS(uhci_batch->base),
|
---|
[b991d37] | 254 | qpos, instance->name, uhci_batch->qh->next);
|
---|
[a963a68] | 255 | }
|
---|
[c56dbe0] | 256 | /**
|
---|
| 257 | * @}
|
---|
| 258 | */
|
---|