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

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

uhci: use uhci sepcific structure instead of generic library

revert the way uhci_transfer_batch_t and usb_transfer_batch_t are included,
don't use usb_transfer_batch_t:

-private data and data dtor
-next_step pointer
-buffer_data pointer

  • 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
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("Queue %s: Adding batch(%p).\n", instance->name,
113 uhci_batch->usb_batch);
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 " scheduled in queue %s.\n",
142 uhci_batch, USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
143 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 batch->usb_batch->error = EINTR;
187 uhci_transfer_batch_call_dispose(batch);
188 }
189 fibril_mutex_unlock(&instance->guard);
190}
191/*----------------------------------------------------------------------------*/
192/** Remove a transfer batch from the list and queue.
193 *
194 * @param[in] instance List to use.
195 * @param[in] batch Transfer batch to remove.
196 *
197 * Does not lock the transfer list, caller is responsible for that.
198 */
199void transfer_list_remove_batch(
200 transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
201{
202 assert(instance);
203 assert(instance->queue_head);
204 assert(uhci_batch);
205 assert(uhci_batch->qh);
206 assert(fibril_mutex_is_locked(&instance->guard));
207
208 usb_log_debug2("Queue %s: removing batch(%p).\n",
209 instance->name, uhci_batch->usb_batch);
210
211 /* Assume I'm the first */
212 const char *qpos = "FIRST";
213 qh_t *prev_qh = instance->queue_head;
214 /* Remove from the hardware queue */
215 if (list_first(&instance->batch_list) != &uhci_batch->link) {
216 /* There is a batch in front of me */
217 prev_qh =
218 uhci_transfer_batch_from_link(uhci_batch->link.prev)->qh;
219 qpos = "NOT FIRST";
220 }
221 assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
222 == addr_to_phys(uhci_batch->qh));
223 prev_qh->next = uhci_batch->qh->next;
224
225 /* Make sure the pointer is updated */
226 write_barrier();
227
228 /* Remove from the batch list */
229 list_remove(&uhci_batch->link);
230 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
231 "from %s, next: %x.\n",
232 uhci_batch, USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
233 qpos, instance->name, uhci_batch->qh->next);
234}
235/**
236 * @}
237 */
Note: See TracBrowser for help on using the repository browser.