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

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

Further cleanup, includes, const, …

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