source: mainline/uspace/drv/bus/usb/uhci/transfer_list.c@ 69a93d02

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

uhci: implemented transfer abort

  • 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 <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.\n");
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" ).\n",
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.\n",
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.\n", 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 /* Save for processing */
176 transfer_list_remove_batch(instance, batch);
177 list_append(current, done);
178 }
179 current = next;
180 }
181 fibril_mutex_unlock(&instance->guard);
182}
183
184/** Walk the list and finish all batches with EINTR.
185 *
186 * @param[in] instance List to use.
187 */
188void transfer_list_abort_all(transfer_list_t *instance)
189{
190 fibril_mutex_lock(&instance->guard);
191 while (!list_empty(&instance->batch_list)) {
192 link_t * const current = list_first(&instance->batch_list);
193 uhci_transfer_batch_t *batch = uhci_transfer_batch_from_link(current);
194 transfer_list_remove_batch(instance, batch);
195 }
196 fibril_mutex_unlock(&instance->guard);
197}
198
199/** Remove a transfer batch from the list and queue.
200 *
201 * @param[in] instance List to use.
202 * @param[in] batch Transfer batch to remove.
203 *
204 * Does not lock the transfer list, caller is responsible for that.
205 */
206void transfer_list_remove_batch(
207 transfer_list_t *instance, uhci_transfer_batch_t *uhci_batch)
208{
209 assert(instance);
210 assert(instance->queue_head);
211 assert(uhci_batch);
212 assert(uhci_batch->qh);
213 assert(fibril_mutex_is_locked(&instance->guard));
214
215 usb_log_debug2("Batch %p removing from queue %s.\n",
216 uhci_batch, instance->name);
217
218 /* Assume I'm the first */
219 const char *qpos = "FIRST";
220 qh_t *prev_qh = instance->queue_head;
221 /* Remove from the hardware queue */
222 if (list_first(&instance->batch_list) != &uhci_batch->link) {
223 /* There is a batch in front of me */
224 prev_qh =
225 uhci_transfer_batch_from_link(uhci_batch->link.prev)->qh;
226 qpos = "NOT FIRST";
227 }
228 assert((prev_qh->next & LINK_POINTER_ADDRESS_MASK)
229 == addr_to_phys(uhci_batch->qh));
230 prev_qh->next = uhci_batch->qh->next;
231
232 /* Make sure the pointer is updated */
233 write_barrier();
234
235 /* Remove from the batch list */
236 list_remove(&uhci_batch->link);
237 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " removed (%s) "
238 "from %s, next: %x.\n", uhci_batch,
239 USB_TRANSFER_BATCH_ARGS(uhci_batch->base),
240 qpos, instance->name, uhci_batch->qh->next);
241}
242/**
243 * @}
244 */
Note: See TracBrowser for help on using the repository browser.