source: mainline/uspace/drv/bus/usb/uhci/transfer_list.c@ 2ca5a198

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2ca5a198 was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

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