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 |
|
---|
37 | #include "transfer_list.h"
|
---|
38 |
|
---|
39 | static void transfer_list_remove_batch(
|
---|
40 | transfer_list_t *instance, usb_transfer_batch_t *batch);
|
---|
41 | /*----------------------------------------------------------------------------*/
|
---|
42 | /** Initialize transfer list structures.
|
---|
43 | *
|
---|
44 | * @param[in] instance Memory place to use.
|
---|
45 | * @param[in] name Name of the new list.
|
---|
46 | * @return Error code
|
---|
47 | *
|
---|
48 | * Allocates memory for internal qh_t structure.
|
---|
49 | */
|
---|
50 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
51 | {
|
---|
52 | assert(instance);
|
---|
53 | instance->name = name;
|
---|
54 | instance->queue_head = malloc32(sizeof(qh_t));
|
---|
55 | if (!instance->queue_head) {
|
---|
56 | usb_log_error("Failed to allocate queue head.\n");
|
---|
57 | return ENOMEM;
|
---|
58 | }
|
---|
59 | instance->queue_head_pa = addr_to_phys(instance->queue_head);
|
---|
60 |
|
---|
61 | qh_init(instance->queue_head);
|
---|
62 | list_initialize(&instance->batch_list);
|
---|
63 | fibril_mutex_initialize(&instance->guard);
|
---|
64 | return EOK;
|
---|
65 | }
|
---|
66 | /*----------------------------------------------------------------------------*/
|
---|
67 | /** Set the next list in transfer list chain.
|
---|
68 | *
|
---|
69 | * @param[in] instance List to lead.
|
---|
70 | * @param[in] next List to append.
|
---|
71 | * @return Error code
|
---|
72 | *
|
---|
73 | * Does not check whether this replaces an existing list .
|
---|
74 | */
|
---|
75 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
76 | {
|
---|
77 | assert(instance);
|
---|
78 | assert(next);
|
---|
79 | if (!instance->queue_head)
|
---|
80 | return;
|
---|
81 | /* Set both queue_head.next to point to the follower */
|
---|
82 | qh_set_next_qh(instance->queue_head, next->queue_head_pa);
|
---|
83 | }
|
---|
84 | /*----------------------------------------------------------------------------*/
|
---|
85 | /** Submit transfer batch to the list and queue.
|
---|
86 | *
|
---|
87 | * @param[in] instance List to use.
|
---|
88 | * @param[in] batch Transfer batch to submit.
|
---|
89 | * @return Error code
|
---|
90 | *
|
---|
91 | * The batch is added to the end of the list and queue.
|
---|
92 | */
|
---|
93 | void transfer_list_add_batch(
|
---|
94 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
---|
95 | {
|
---|
96 | assert(instance);
|
---|
97 | assert(batch);
|
---|
98 | usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
|
---|
99 |
|
---|
100 | fibril_mutex_lock(&instance->guard);
|
---|
101 |
|
---|
102 | qh_t *last_qh = NULL;
|
---|
103 | /* Add to the hardware queue. */
|
---|
104 | if (list_empty(&instance->batch_list)) {
|
---|
105 | /* There is nothing scheduled */
|
---|
106 | last_qh = instance->queue_head;
|
---|
107 | } else {
|
---|
108 | /* There is something scheduled */
|
---|
109 | usb_transfer_batch_t *last = list_get_instance(
|
---|
110 | instance->batch_list.prev, usb_transfer_batch_t, link);
|
---|
111 | last_qh = batch_qh(last);
|
---|
112 | }
|
---|
113 | const uint32_t pa = addr_to_phys(batch_qh(batch));
|
---|
114 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
115 |
|
---|
116 | /* keep link */
|
---|
117 | batch_qh(batch)->next = last_qh->next;
|
---|
118 | qh_set_next_qh(last_qh, pa);
|
---|
119 |
|
---|
120 | /* Add to the driver list */
|
---|
121 | list_append(&batch->link, &instance->batch_list);
|
---|
122 |
|
---|
123 | usb_transfer_batch_t *first = list_get_instance(
|
---|
124 | instance->batch_list.next, usb_transfer_batch_t, link);
|
---|
125 | usb_log_debug("Batch(%p) added to queue %s, first is %p.\n",
|
---|
126 | batch, instance->name, first);
|
---|
127 | fibril_mutex_unlock(&instance->guard);
|
---|
128 | }
|
---|
129 | /*----------------------------------------------------------------------------*/
|
---|
130 | /** Check list for finished batches.
|
---|
131 | *
|
---|
132 | * @param[in] instance List to use.
|
---|
133 | * @return Error code
|
---|
134 | *
|
---|
135 | * Creates a local list of finished batches and calls next_step on each and
|
---|
136 | * every one. This is safer because next_step may theoretically access
|
---|
137 | * this transfer list leading to the deadlock if its done inline.
|
---|
138 | */
|
---|
139 | void transfer_list_remove_finished(transfer_list_t *instance)
|
---|
140 | {
|
---|
141 | assert(instance);
|
---|
142 |
|
---|
143 | LIST_INITIALIZE(done);
|
---|
144 |
|
---|
145 | fibril_mutex_lock(&instance->guard);
|
---|
146 | link_t *current = instance->batch_list.next;
|
---|
147 | while (current != &instance->batch_list) {
|
---|
148 | link_t *next = current->next;
|
---|
149 | usb_transfer_batch_t *batch =
|
---|
150 | list_get_instance(current, usb_transfer_batch_t, link);
|
---|
151 |
|
---|
152 | if (batch_is_complete(batch)) {
|
---|
153 | /* Save for post-processing */
|
---|
154 | transfer_list_remove_batch(instance, batch);
|
---|
155 | list_append(current, &done);
|
---|
156 | }
|
---|
157 | current = next;
|
---|
158 | }
|
---|
159 | fibril_mutex_unlock(&instance->guard);
|
---|
160 |
|
---|
161 | while (!list_empty(&done)) {
|
---|
162 | link_t *item = done.next;
|
---|
163 | list_remove(item);
|
---|
164 | usb_transfer_batch_t *batch =
|
---|
165 | list_get_instance(item, usb_transfer_batch_t, link);
|
---|
166 | batch->next_step(batch);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | /*----------------------------------------------------------------------------*/
|
---|
170 | /** Walk the list and abort all batches.
|
---|
171 | *
|
---|
172 | * @param[in] instance List to use.
|
---|
173 | */
|
---|
174 | void transfer_list_abort_all(transfer_list_t *instance)
|
---|
175 | {
|
---|
176 | fibril_mutex_lock(&instance->guard);
|
---|
177 | while (!list_empty(&instance->batch_list)) {
|
---|
178 | link_t *current = instance->batch_list.next;
|
---|
179 | usb_transfer_batch_t *batch =
|
---|
180 | list_get_instance(current, usb_transfer_batch_t, link);
|
---|
181 | transfer_list_remove_batch(instance, batch);
|
---|
182 | usb_transfer_batch_finish(batch, EIO);
|
---|
183 | }
|
---|
184 | fibril_mutex_unlock(&instance->guard);
|
---|
185 | }
|
---|
186 | /*----------------------------------------------------------------------------*/
|
---|
187 | /** Remove a transfer batch from the list and queue.
|
---|
188 | *
|
---|
189 | * @param[in] instance List to use.
|
---|
190 | * @param[in] batch Transfer batch to remove.
|
---|
191 | * @return Error code
|
---|
192 | *
|
---|
193 | * Does not lock the transfer list, caller is responsible for that.
|
---|
194 | */
|
---|
195 | void transfer_list_remove_batch(
|
---|
196 | transfer_list_t *instance, usb_transfer_batch_t *batch)
|
---|
197 | {
|
---|
198 | assert(instance);
|
---|
199 | assert(instance->queue_head);
|
---|
200 | assert(batch);
|
---|
201 | assert(batch_qh(batch));
|
---|
202 | assert(fibril_mutex_is_locked(&instance->guard));
|
---|
203 |
|
---|
204 | usb_log_debug2(
|
---|
205 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
---|
206 |
|
---|
207 | const char *qpos = NULL;
|
---|
208 | /* Remove from the hardware queue */
|
---|
209 | if (instance->batch_list.next == &batch->link) {
|
---|
210 | /* I'm the first one here */
|
---|
211 | assert((instance->queue_head->next & LINK_POINTER_ADDRESS_MASK)
|
---|
212 | == addr_to_phys(batch_qh(batch)));
|
---|
213 | instance->queue_head->next = batch_qh(batch)->next;
|
---|
214 | qpos = "FIRST";
|
---|
215 | } else {
|
---|
216 | usb_transfer_batch_t *prev =
|
---|
217 | list_get_instance(
|
---|
218 | batch->link.prev, usb_transfer_batch_t, link);
|
---|
219 | assert((batch_qh(prev)->next & LINK_POINTER_ADDRESS_MASK)
|
---|
220 | == addr_to_phys(batch_qh(batch)));
|
---|
221 | batch_qh(prev)->next = batch_qh(batch)->next;
|
---|
222 | qpos = "NOT FIRST";
|
---|
223 | }
|
---|
224 | /* Remove from the batch list */
|
---|
225 | list_remove(&batch->link);
|
---|
226 | usb_log_debug("Batch(%p) removed (%s) from %s, next %x.\n",
|
---|
227 | batch, qpos, instance->name, batch_qh(batch)->next);
|
---|
228 | }
|
---|
229 | /**
|
---|
230 | * @}
|
---|
231 | */
|
---|