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

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

Doxygen

  • Property mode set to 100644
File size: 6.6 KB
Line 
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
39static void transfer_list_remove_batch(
40 transfer_list_t *instance, 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 */
50int 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
61 qh_init(instance->queue_head);
62 list_initialize(&instance->batch_list);
63 fibril_mutex_initialize(&instance->guard);
64 return EOK;
65}
66/*----------------------------------------------------------------------------*/
67/** Set the next list in transfer list chain.
68 *
69 * @param[in] instance List to lead.
70 * @param[in] next List to append.
71 * @return Error code
72 *
73 * Does not check whether this replaces an existing list .
74 */
75void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
76{
77 assert(instance);
78 assert(next);
79 if (!instance->queue_head)
80 return;
81 /* Set both next and element to point to the same QH */
82 qh_set_next_qh(instance->queue_head, next->queue_head_pa);
83 qh_set_element_qh(instance->queue_head, next->queue_head_pa);
84}
85/*----------------------------------------------------------------------------*/
86/** Submit transfer batch to the list and queue.
87 *
88 * @param[in] instance List to use.
89 * @param[in] batch Transfer batch to submit.
90 * @return Error code
91 *
92 * The batch is added to the end of the list and queue.
93 */
94void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
95{
96 assert(instance);
97 assert(batch);
98 usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
99
100 const uint32_t pa = addr_to_phys(batch->qh);
101 assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
102
103 /* New batch will be added to the end of the current list
104 * so set the link accordingly */
105 qh_set_next_qh(batch->qh, instance->queue_head->next);
106
107 fibril_mutex_lock(&instance->guard);
108
109 /* Add to the hardware queue. */
110 if (list_empty(&instance->batch_list)) {
111 /* There is nothing scheduled */
112 qh_t *qh = instance->queue_head;
113 assert(qh->element == qh->next);
114 qh_set_element_qh(qh, pa);
115 } else {
116 /* There is something scheduled */
117 batch_t *last = list_get_instance(
118 instance->batch_list.prev, batch_t, link);
119 qh_set_next_qh(last->qh, pa);
120 }
121 /* Add to the driver list */
122 list_append(&batch->link, &instance->batch_list);
123
124 batch_t *first = list_get_instance(
125 instance->batch_list.next, batch_t, link);
126 usb_log_debug("Batch(%p) added to queue %s, first is %p.\n",
127 batch, instance->name, first);
128 fibril_mutex_unlock(&instance->guard);
129}
130/*----------------------------------------------------------------------------*/
131/** Remove a transfer batch from the list and queue.
132 *
133 * @param[in] instance List to use.
134 * @param[in] batch Transfer batch to remove.
135 * @return Error code
136 *
137 * Does not lock the transfer list, caller is responsible for that.
138 */
139void transfer_list_remove_batch(transfer_list_t *instance, batch_t *batch)
140{
141 assert(instance);
142 assert(batch);
143 assert(instance->queue_head);
144 assert(batch->qh);
145 usb_log_debug2(
146 "Queue %s: removing batch(%p).\n", instance->name, batch);
147
148 const char * pos = NULL;
149 /* Remove from the hardware queue */
150 if (batch->link.prev == &instance->batch_list) {
151 /* I'm the first one here */
152 qh_set_element_qh(instance->queue_head, batch->qh->next);
153 pos = "FIRST";
154 } else {
155 batch_t *prev =
156 list_get_instance(batch->link.prev, batch_t, link);
157 qh_set_next_qh(prev->qh, batch->qh->next);
158 pos = "NOT FIRST";
159 }
160 /* Remove from the driver list */
161 list_remove(&batch->link);
162 usb_log_debug("Batch(%p) removed (%s) from %s, next element %x.\n",
163 batch, pos, instance->name, batch->qh->next);
164}
165/*----------------------------------------------------------------------------*/
166/** Check list for finished batches.
167 *
168 * @param[in] instance List to use.
169 * @return Error code
170 *
171 * Creates a local list of finished batches and calls next_step on each and
172 * every one. This is safer because next_step may theoretically access
173 * this transfer list leading to the deadlock if its done inline.
174 */
175void transfer_list_remove_finished(transfer_list_t *instance)
176{
177 assert(instance);
178
179 LIST_INITIALIZE(done);
180
181 fibril_mutex_lock(&instance->guard);
182 link_t *current = instance->batch_list.next;
183 while (current != &instance->batch_list) {
184 link_t *next = current->next;
185 batch_t *batch = list_get_instance(current, batch_t, link);
186
187 if (batch_is_complete(batch)) {
188 /* Save for post-processing */
189 transfer_list_remove_batch(instance, batch);
190 list_append(current, &done);
191 }
192 current = next;
193 }
194 fibril_mutex_unlock(&instance->guard);
195
196 while (!list_empty(&done)) {
197 link_t *item = done.next;
198 list_remove(item);
199 batch_t *batch = list_get_instance(item, batch_t, link);
200 batch->next_step(batch);
201 }
202}
203/**
204 * @}
205 */
Note: See TracBrowser for help on using the repository browser.