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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2833bb4 was 2755a622, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

uhci: fix transfer aborting

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