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

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

More comment fixes and code cleanup

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