source: mainline/uspace/drv/uhci-hcd/transfer_list.c@ e247d83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e247d83 was e247d83, checked in by Jan Vesely <jano.vesely@…>, 15 years ago

Const, type-casting and other minor fixes

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