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

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

uhci: Sanitize headers.

Include what you use.

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