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 |
|
---|
42 | static 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 | */
|
---|
53 | int 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 | */
|
---|
78 | void 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 | */
|
---|
91 | void 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 | */
|
---|
107 | void 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 = usb_transfer_batch_from_link(
|
---|
124 | list_last(&instance->batch_list));
|
---|
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_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " scheduled in queue %s.\n",
|
---|
144 | batch, USB_TRANSFER_BATCH_ARGS(*batch), instance->name);
|
---|
145 | fibril_mutex_unlock(&instance->guard);
|
---|
146 | }
|
---|
147 | /*----------------------------------------------------------------------------*/
|
---|
148 | /** Add completed batches to the provided list.
|
---|
149 | *
|
---|
150 | * @param[in] instance List to use.
|
---|
151 | * @param[in] done list to fill
|
---|
152 | */
|
---|
153 | void transfer_list_remove_finished(transfer_list_t *instance, list_t *done)
|
---|
154 | {
|
---|
155 | assert(instance);
|
---|
156 | assert(done);
|
---|
157 |
|
---|
158 | fibril_mutex_lock(&instance->guard);
|
---|
159 | link_t *current = instance->batch_list.head.next;
|
---|
160 | while (current != &instance->batch_list.head) {
|
---|
161 | link_t * const next = current->next;
|
---|
162 | usb_transfer_batch_t *batch =
|
---|
163 | usb_transfer_batch_from_link(current);
|
---|
164 |
|
---|
165 | if (batch_is_complete(batch)) {
|
---|
166 | /* Save for processing */
|
---|
167 | transfer_list_remove_batch(instance, batch);
|
---|
168 | list_append(current, done);
|
---|
169 | }
|
---|
170 | current = next;
|
---|
171 | }
|
---|
172 | fibril_mutex_unlock(&instance->guard);
|
---|
173 | }
|
---|
174 | /*----------------------------------------------------------------------------*/
|
---|
175 | /** Walk the list and finish all batches with EINTR.
|
---|
176 | *
|
---|
177 | * @param[in] instance List to use.
|
---|
178 | */
|
---|
179 | void transfer_list_abort_all(transfer_list_t *instance)
|
---|
180 | {
|
---|
181 | fibril_mutex_lock(&instance->guard);
|
---|
182 | while (!list_empty(&instance->batch_list)) {
|
---|
183 | link_t * const current = list_first(&instance->batch_list);
|
---|
184 | usb_transfer_batch_t *batch =
|
---|
185 | usb_transfer_batch_from_link(current);
|
---|
186 | transfer_list_remove_batch(instance, batch);
|
---|
187 | usb_transfer_batch_finish_error(batch, EINTR);
|
---|
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 | */
|
---|
199 | void transfer_list_remove_batch(
|
---|
200 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
---|
201 | {
|
---|
202 | assert(instance);
|
---|
203 | assert(instance->queue_head);
|
---|
204 | assert(batch);
|
---|
205 | assert(batch_qh(batch));
|
---|
206 | assert(fibril_mutex_is_locked(&instance->guard));
|
---|
207 |
|
---|
208 | usb_log_debug2(
|
---|
209 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
---|
210 |
|
---|
211 | const char *qpos = NULL;
|
---|
212 | qh_t *prev_qh = NULL;
|
---|
213 | /* Remove from the hardware queue */
|
---|
214 | if (list_first(&instance->batch_list) == &batch->link) {
|
---|
215 | /* I'm the first one here */
|
---|
216 | prev_qh = instance->queue_head;
|
---|
217 | qpos = "FIRST";
|
---|
218 | } else {
|
---|
219 | /* The thing before me is a batch too */
|
---|
220 | usb_transfer_batch_t *prev =
|
---|
221 | usb_transfer_batch_from_link(batch->link.prev);
|
---|
222 | prev_qh = batch_qh(prev);
|
---|
223 | qpos = "NOT FIRST";
|
---|
224 | }
|
---|
225 | assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
|
---|
226 | == addr_to_phys(batch_qh(batch)));
|
---|
227 | prev_qh->next = batch_qh(batch)->next;
|
---|
228 |
|
---|
229 | /* Make sure the pointer is updated */
|
---|
230 | write_barrier();
|
---|
231 |
|
---|
232 | /* Remove from the batch list */
|
---|
233 | list_remove(&batch->link);
|
---|
234 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
|
---|
235 | "from %s, next: %x.\n",
|
---|
236 | batch, USB_TRANSFER_BATCH_ARGS(*batch),
|
---|
237 | qpos, instance->name, batch_qh(batch)->next);
|
---|
238 | }
|
---|
239 | /**
|
---|
240 | * @}
|
---|
241 | */
|
---|