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