source: mainline/uspace/drv/ohci/endpoint_list.c@ 28d9c95

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 28d9c95 was 2aaf804, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Use provided barriers instead of asm constructs.

Use warning instead of assert if malloc32 falls back to memalign.

  • Property mode set to 100644
File size: 7.4 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/** @addtogroup drvusbohci
29 * @{
30 */
31/** @file
32 * @brief OHCI driver transfer list implementation
33 */
34#include <errno.h>
35#include <usb/debug.h>
36#include <arch/barrier.h>
37
38#include "endpoint_list.h"
39
40/** Initialize transfer list structures.
41 *
42 * @param[in] instance Memory place to use.
43 * @param[in] name Name of the new list.
44 * @return Error code
45 *
46 * Allocates memory for internal qh_t structure.
47 */
48int endpoint_list_init(endpoint_list_t *instance, const char *name)
49{
50 assert(instance);
51 instance->name = name;
52 instance->list_head = malloc32(sizeof(ed_t));
53 if (!instance->list_head) {
54 usb_log_error("Failed to allocate list head.\n");
55 return ENOMEM;
56 }
57 instance->list_head_pa = addr_to_phys(instance->list_head);
58 usb_log_debug2("Transfer list %s setup with ED: %p(0x%0" PRIx32 ")).\n",
59 name, instance->list_head, instance->list_head_pa);
60
61 ed_init(instance->list_head, NULL);
62 list_initialize(&instance->endpoint_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 */
75void endpoint_list_set_next(endpoint_list_t *instance, endpoint_list_t *next)
76{
77 assert(instance);
78 assert(next);
79 ed_append_ed(instance->list_head, next->list_head);
80}
81/*----------------------------------------------------------------------------*/
82/** Submit transfer endpoint to the list and queue.
83 *
84 * @param[in] instance List to use.
85 * @param[in] endpoint Transfer endpoint to submit.
86 * @return Error code
87 *
88 * The endpoint is added to the end of the list and queue.
89 */
90void endpoint_list_add_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
91{
92 assert(instance);
93 assert(hcd_ep);
94 usb_log_debug2("Queue %s: Adding endpoint(%p).\n",
95 instance->name, hcd_ep);
96
97 fibril_mutex_lock(&instance->guard);
98
99 ed_t *last_ed = NULL;
100 /* Add to the hardware queue. */
101 if (list_empty(&instance->endpoint_list)) {
102 /* There is nothing scheduled */
103 last_ed = instance->list_head;
104 } else {
105 /* There is something scheduled */
106 hcd_endpoint_t *last = list_get_instance(
107 instance->endpoint_list.prev, hcd_endpoint_t, link);
108 last_ed = last->ed;
109 }
110 /* Keep link */
111 hcd_ep->ed->next = last_ed->next;
112 /* Make sure ED is written to the memory */
113 write_barrier();
114
115 /* Add ed to the hw list */
116 ed_append_ed(last_ed, hcd_ep->ed);
117 /* Make sure ED is updated */
118 write_barrier();
119
120
121 /* Add to the driver list */
122 list_append(&hcd_ep->link, &instance->endpoint_list);
123
124 hcd_endpoint_t *first = list_get_instance(
125 instance->endpoint_list.next, hcd_endpoint_t, link);
126 usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).\n",
127 hcd_ep, instance->name, first, first->ed);
128 if (last_ed == instance->list_head) {
129 usb_log_debug2("%s head ED(%p-0x%0" PRIx32 "): %x:%x:%x:%x.\n",
130 instance->name, last_ed, instance->list_head_pa,
131 last_ed->status, last_ed->td_tail, last_ed->td_head,
132 last_ed->next);
133 }
134 fibril_mutex_unlock(&instance->guard);
135}
136/*----------------------------------------------------------------------------*/
137#if 0
138/** Create list for finished endpoints.
139 *
140 * @param[in] instance List to use.
141 * @param[in] done list to fill
142 */
143void endpoint_list_remove_finished(endpoint_list_t *instance, link_t *done)
144{
145 assert(instance);
146 assert(done);
147
148 fibril_mutex_lock(&instance->guard);
149 usb_log_debug2("Checking list %s for completed endpointes(%d).\n",
150 instance->name, list_count(&instance->endpoint_list));
151 link_t *current = instance->endpoint_list.next;
152 while (current != &instance->endpoint_list) {
153 link_t *next = current->next;
154 hcd_endpoint_t *endpoint =
155 list_get_instance(current, hcd_endpoint_t, link);
156
157 if (endpoint_is_complete(endpoint)) {
158 /* Save for post-processing */
159 endpoint_list_remove_endpoint(instance, endpoint);
160 list_append(current, done);
161 }
162 current = next;
163 }
164 fibril_mutex_unlock(&instance->guard);
165}
166/*----------------------------------------------------------------------------*/
167/** Walk the list and abort all endpointes.
168 *
169 * @param[in] instance List to use.
170 */
171void endpoint_list_abort_all(endpoint_list_t *instance)
172{
173 fibril_mutex_lock(&instance->guard);
174 while (!list_empty(&instance->endpoint_list)) {
175 link_t *current = instance->endpoint_list.next;
176 hcd_endpoint_t *endpoint =
177 list_get_instance(current, hcd_endpoint_t, link);
178 endpoint_list_remove_endpoint(instance, endpoint);
179 hcd_endpoint_finish_error(endpoint, EIO);
180 }
181 fibril_mutex_unlock(&instance->guard);
182}
183#endif
184/*----------------------------------------------------------------------------*/
185/** Remove a transfer endpoint from the list and queue.
186 *
187 * @param[in] instance List to use.
188 * @param[in] endpoint Transfer endpoint to remove.
189 * @return Error code
190 *
191 * Does not lock the transfer list, caller is responsible for that.
192 */
193void endpoint_list_remove_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
194{
195 assert(instance);
196 assert(instance->list_head);
197 assert(hcd_ep);
198 assert(hcd_ep->ed);
199
200 fibril_mutex_lock(&instance->guard);
201
202 usb_log_debug2(
203 "Queue %s: removing endpoint(%p).\n", instance->name, hcd_ep);
204
205 const char *qpos = NULL;
206 ed_t *prev_ed;
207 /* Remove from the hardware queue */
208 if (instance->endpoint_list.next == &hcd_ep->link) {
209 /* I'm the first one here */
210 prev_ed = instance->list_head;
211 qpos = "FIRST";
212 } else {
213 hcd_endpoint_t *prev =
214 list_get_instance(hcd_ep->link.prev, hcd_endpoint_t, link);
215 prev_ed = prev->ed;
216 qpos = "NOT FIRST";
217 }
218 assert((prev_ed->next & ED_NEXT_PTR_MASK) == addr_to_phys(hcd_ep->ed));
219 prev_ed->next = hcd_ep->ed->next;
220 /* Make sure ED is updated */
221 write_barrier();
222
223 usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.\n",
224 hcd_ep, qpos, instance->name, hcd_ep->ed->next);
225
226 /* Remove from the endpoint list */
227 list_remove(&hcd_ep->link);
228 fibril_mutex_unlock(&instance->guard);
229}
230/**
231 * @}
232 */
Note: See TracBrowser for help on using the repository browser.