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

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

Merge mainline changes.

  • Property mode set to 100644
File size: 7.1 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
42static void transfer_list_remove_batch(
43 transfer_list_t *instance, uhci_transfer_batch_t *uhci_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, uhci_transfer_batch_t *uhci_batch)
109{
110 assert(instance);
111 assert(uhci_batch);
112 usb_log_debug2("Batch %p adding to queue %s.\n",
113 uhci_batch->usb_batch, instance->name);
114
115 fibril_mutex_lock(&instance->guard);
116
117 /* Assume there is nothing scheduled */
118 qh_t *last_qh = instance->queue_head;
119 /* There is something scheduled */
120 if (!list_empty(&instance->batch_list)) {
121 last_qh = uhci_transfer_batch_from_link(
122 list_last(&instance->batch_list))->qh;
123 }
124 /* Add to the hardware queue. */
125 const uint32_t pa = addr_to_phys(uhci_batch->qh);
126 assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
127
128 /* Make sure all data in the batch are written */
129 write_barrier();
130
131 /* keep link */
132 uhci_batch->qh->next = last_qh->next;
133 qh_set_next_qh(last_qh, uhci_batch->qh);
134
135 /* Make sure the pointer is updated */
136 write_barrier();
137
138 /* Add to the driver's list */
139 list_append(&uhci_batch->link, &instance->batch_list);
140
141 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
142 " scheduled in queue %s.\n", uhci_batch->usb_batch,
143 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch), instance->name);
144 fibril_mutex_unlock(&instance->guard);
145}
146
147/** Add completed batches to the provided list.
148 *
149 * @param[in] instance List to use.
150 * @param[in] done list to fill
151 */
152void transfer_list_remove_finished(transfer_list_t *instance, list_t *done)
153{
154 assert(instance);
155 assert(done);
156
157 fibril_mutex_lock(&instance->guard);
158 link_t *current = list_first(&instance->batch_list);
159 while (current && current != &instance->batch_list.head) {
160 link_t * const next = current->next;
161 uhci_transfer_batch_t *batch =
162 uhci_transfer_batch_from_link(current);
163
164 if (uhci_transfer_batch_is_complete(batch)) {
165 /* Save for processing */
166 transfer_list_remove_batch(instance, batch);
167 list_append(current, done);
168 }
169 current = next;
170 }
171 fibril_mutex_unlock(&instance->guard);
172}
173
174/** Walk the list and finish all batches with EINTR.
175 *
176 * @param[in] instance List to use.
177 */
178void transfer_list_abort_all(transfer_list_t *instance)
179{
180 fibril_mutex_lock(&instance->guard);
181 while (!list_empty(&instance->batch_list)) {
182 link_t * const current = list_first(&instance->batch_list);
183 uhci_transfer_batch_t *batch =
184 uhci_transfer_batch_from_link(current);
185 transfer_list_remove_batch(instance, batch);
186 uhci_transfer_batch_abort(batch);
187 }
188 fibril_mutex_unlock(&instance->guard);
189}
190
191/** Remove a transfer batch from the list and queue.
192 *
193 * @param[in] instance List to use.
194 * @param[in] batch Transfer batch to remove.
195 *
196 * Does not lock the transfer list, caller is responsible for that.
197 */
198void transfer_list_remove_batch(
199 transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
200{
201 assert(instance);
202 assert(instance->queue_head);
203 assert(uhci_batch);
204 assert(uhci_batch->qh);
205 assert(fibril_mutex_is_locked(&instance->guard));
206
207 usb_log_debug2("Batch %p removing from queue %s.\n",
208 uhci_batch->usb_batch, instance->name);
209
210 /* Assume I'm the first */
211 const char *qpos = "FIRST";
212 qh_t *prev_qh = instance->queue_head;
213 /* Remove from the hardware queue */
214 if (list_first(&instance->batch_list) != &uhci_batch->link) {
215 /* There is a batch in front of me */
216 prev_qh =
217 uhci_transfer_batch_from_link(uhci_batch->link.prev)->qh;
218 qpos = "NOT FIRST";
219 }
220 assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
221 == addr_to_phys(uhci_batch->qh));
222 prev_qh->next = uhci_batch->qh->next;
223
224 /* Make sure the pointer is updated */
225 write_barrier();
226
227 /* Remove from the batch list */
228 list_remove(&uhci_batch->link);
229 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
230 "from %s, next: %x.\n", uhci_batch->usb_batch,
231 USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
232 qpos, instance->name, uhci_batch->qh->next);
233}
234/**
235 * @}
236 */
Note: See TracBrowser for help on using the repository browser.