source: mainline/uspace/drv/bus/usb/uhci/transfer_list.c@ af2b806

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

Rename: batch ⇒ uhci_batch to differ from library version

  • 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
29/** @addtogroup drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI driver transfer list implementation
34 */
35
36#include <errno.h>
37#include <usb/debug.h>
38#include <libarch/barrier.h>
39
40#include "transfer_list.h"
41#include "uhci_batch.h"
42
43static void transfer_list_remove_batch(
44 transfer_list_t *instance, usb_transfer_batch_t *batch);
45/*----------------------------------------------------------------------------*/
46/** Initialize transfer list structures.
47 *
48 * @param[in] instance Memory place to use.
49 * @param[in] name Name of the new list.
50 * @return Error code
51 *
52 * Allocates memory for internal qh_t structure.
53 */
54int transfer_list_init(transfer_list_t *instance, const char *name)
55{
56 assert(instance);
57 instance->name = name;
58 instance->queue_head = malloc32(sizeof(qh_t));
59 if (!instance->queue_head) {
60 usb_log_error("Failed to allocate queue head.\n");
61 return ENOMEM;
62 }
63 const uint32_t queue_head_pa = addr_to_phys(instance->queue_head);
64 usb_log_debug2("Transfer list %s setup with QH: %p (%#" PRIx32" ).\n",
65 name, instance->queue_head, queue_head_pa);
66
67 qh_init(instance->queue_head);
68 list_initialize(&instance->batch_list);
69 fibril_mutex_initialize(&instance->guard);
70 return EOK;
71}
72/*----------------------------------------------------------------------------*/
73/** Dispose transfer list structures.
74 *
75 * @param[in] instance Memory place to use.
76 *
77 * Frees memory of the internal qh_t structure.
78 */
79void transfer_list_fini(transfer_list_t *instance)
80{
81 assert(instance);
82 free32(instance->queue_head);
83}
84/** Set the next list in transfer list chain.
85 *
86 * @param[in] instance List to lead.
87 * @param[in] next List to append.
88 * @return Error code
89 *
90 * Does not check whether this replaces an existing list .
91 */
92void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
93{
94 assert(instance);
95 assert(instance->queue_head);
96 assert(next);
97 /* Set queue_head.next to point to the follower */
98 qh_set_next_qh(instance->queue_head, next->queue_head);
99}
100/*----------------------------------------------------------------------------*/
101/** Add transfer batch to the list and queue.
102 *
103 * @param[in] instance List to use.
104 * @param[in] batch Transfer batch to submit.
105 *
106 * The batch is added to the end of the list and queue.
107 */
108void transfer_list_add_batch(
109 transfer_list_t *instance, usb_transfer_batch_t *batch)
110{
111 assert(instance);
112 assert(batch);
113 usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
114
115 fibril_mutex_lock(&instance->guard);
116
117 qh_t *last_qh = NULL;
118 /* Add to the hardware queue. */
119 if (list_empty(&instance->batch_list)) {
120 /* There is nothing scheduled */
121 last_qh = instance->queue_head;
122 } else {
123 /* There is something scheduled */
124 usb_transfer_batch_t *last = usb_transfer_batch_from_link(
125 list_last(&instance->batch_list));
126 last_qh = batch_qh(last);
127 }
128 const uint32_t pa = addr_to_phys(batch_qh(batch));
129 assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
130
131 /* Make sure all data in the batch are written */
132 write_barrier();
133
134 /* keep link */
135 batch_qh(batch)->next = last_qh->next;
136 qh_set_next_qh(last_qh, batch_qh(batch));
137
138 /* Make sure the pointer is updated */
139 write_barrier();
140
141 /* Add to the driver's list */
142 list_append(&batch->link, &instance->batch_list);
143
144 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " scheduled in queue %s.\n",
145 batch, USB_TRANSFER_BATCH_ARGS(*batch), instance->name);
146 fibril_mutex_unlock(&instance->guard);
147}
148/*----------------------------------------------------------------------------*/
149/** Add completed batches 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, list_t *done)
155{
156 assert(instance);
157 assert(done);
158
159 fibril_mutex_lock(&instance->guard);
160 link_t *current = instance->batch_list.head.next;
161 while (current != &instance->batch_list.head) {
162 link_t * const 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 * const current = list_first(&instance->batch_list);
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 (list_first(&instance->batch_list) == &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_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
236 "from %s, next: %x.\n",
237 batch, USB_TRANSFER_BATCH_ARGS(*batch),
238 qpos, instance->name, batch_qh(batch)->next);
239}
240/**
241 * @}
242 */
Note: See TracBrowser for help on using the repository browser.