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 drvusbohci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief OHCI driver transfer list implementation
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <libarch/barrier.h>
|
---|
39 |
|
---|
40 | #include <usb/debug.h>
|
---|
41 | #include <usb/host/utils/malloc32.h>
|
---|
42 |
|
---|
43 | #include "endpoint_list.h"
|
---|
44 |
|
---|
45 | /** Initialize transfer list structures.
|
---|
46 | *
|
---|
47 | * @param[in] instance Memory place to use.
|
---|
48 | * @param[in] name Name of the new list.
|
---|
49 | * @return Error code
|
---|
50 | *
|
---|
51 | * Allocates memory for internal ed_t structure.
|
---|
52 | */
|
---|
53 | int endpoint_list_init(endpoint_list_t *instance, const char *name)
|
---|
54 | {
|
---|
55 | assert(instance);
|
---|
56 | instance->name = name;
|
---|
57 | instance->list_head = malloc32(sizeof(ed_t));
|
---|
58 | if (!instance->list_head) {
|
---|
59 | usb_log_error("Failed to allocate list head.");
|
---|
60 | return ENOMEM;
|
---|
61 | }
|
---|
62 | instance->list_head_pa = addr_to_phys(instance->list_head);
|
---|
63 | usb_log_debug2("Transfer list %s setup with ED: %p(0x%0" PRIx32 ")).",
|
---|
64 | name, instance->list_head, instance->list_head_pa);
|
---|
65 |
|
---|
66 | ed_init(instance->list_head, NULL, NULL);
|
---|
67 | list_initialize(&instance->endpoint_list);
|
---|
68 | fibril_mutex_initialize(&instance->guard);
|
---|
69 | return EOK;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Set the next list in transfer list chain.
|
---|
73 | *
|
---|
74 | * @param[in] instance List to lead.
|
---|
75 | * @param[in] next List to append.
|
---|
76 | *
|
---|
77 | * Does not check whether this replaces an existing list.
|
---|
78 | */
|
---|
79 | void endpoint_list_set_next(
|
---|
80 | const endpoint_list_t *instance, const endpoint_list_t *next)
|
---|
81 | {
|
---|
82 | assert(instance);
|
---|
83 | assert(next);
|
---|
84 | ed_append_ed(instance->list_head, next->list_head);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Add endpoint to the list and queue.
|
---|
88 | *
|
---|
89 | * @param[in] instance List to use.
|
---|
90 | * @param[in] endpoint Endpoint to add.
|
---|
91 | *
|
---|
92 | * The endpoint is added to the end of the list and queue.
|
---|
93 | */
|
---|
94 | void endpoint_list_add_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
---|
95 | {
|
---|
96 | assert(instance);
|
---|
97 | assert(ep);
|
---|
98 | usb_log_debug2("Queue %s: Adding endpoint(%p).", instance->name, ep);
|
---|
99 |
|
---|
100 | fibril_mutex_lock(&instance->guard);
|
---|
101 |
|
---|
102 | ed_t *last_ed = NULL;
|
---|
103 | /* Add to the hardware queue. */
|
---|
104 | if (list_empty(&instance->endpoint_list)) {
|
---|
105 | /* There are no active EDs */
|
---|
106 | last_ed = instance->list_head;
|
---|
107 | } else {
|
---|
108 | /* There are active EDs, get the last one */
|
---|
109 | ohci_endpoint_t *last = list_get_instance(
|
---|
110 | list_last(&instance->endpoint_list), ohci_endpoint_t, eplist_link);
|
---|
111 | last_ed = last->ed;
|
---|
112 | }
|
---|
113 | /* Keep link */
|
---|
114 | ep->ed->next = last_ed->next;
|
---|
115 | /* Make sure ED is written to the memory */
|
---|
116 | write_barrier();
|
---|
117 |
|
---|
118 | /* Add ed to the hw queue */
|
---|
119 | ed_append_ed(last_ed, ep->ed);
|
---|
120 | /* Make sure ED is updated */
|
---|
121 | write_barrier();
|
---|
122 |
|
---|
123 | /* Add to the sw list */
|
---|
124 | list_append(&ep->eplist_link, &instance->endpoint_list);
|
---|
125 |
|
---|
126 | ohci_endpoint_t *first = list_get_instance(
|
---|
127 | list_first(&instance->endpoint_list), ohci_endpoint_t, eplist_link);
|
---|
128 | usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).",
|
---|
129 | ep, instance->name, first, first->ed);
|
---|
130 | if (last_ed == instance->list_head) {
|
---|
131 | usb_log_debug2("%s head ED(%p-0x%0" PRIx32 "): %x:%x:%x:%x.",
|
---|
132 | instance->name, last_ed, instance->list_head_pa,
|
---|
133 | last_ed->status, last_ed->td_tail, last_ed->td_head,
|
---|
134 | last_ed->next);
|
---|
135 | }
|
---|
136 | fibril_mutex_unlock(&instance->guard);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Remove endpoint from the list and queue.
|
---|
140 | *
|
---|
141 | * @param[in] instance List to use.
|
---|
142 | * @param[in] endpoint Endpoint to remove.
|
---|
143 | */
|
---|
144 | void endpoint_list_remove_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
---|
145 | {
|
---|
146 | assert(instance);
|
---|
147 | assert(instance->list_head);
|
---|
148 | assert(ep);
|
---|
149 | assert(ep->ed);
|
---|
150 |
|
---|
151 | fibril_mutex_lock(&instance->guard);
|
---|
152 |
|
---|
153 | usb_log_debug2("Queue %s: removing endpoint(%p).", instance->name, ep);
|
---|
154 |
|
---|
155 | const char *qpos = NULL;
|
---|
156 | ed_t *prev_ed;
|
---|
157 | /* Remove from the hardware queue */
|
---|
158 | if (list_first(&instance->endpoint_list) == &ep->eplist_link) {
|
---|
159 | /* I'm the first one here */
|
---|
160 | prev_ed = instance->list_head;
|
---|
161 | qpos = "FIRST";
|
---|
162 | } else {
|
---|
163 | ohci_endpoint_t *prev =
|
---|
164 | list_get_instance(ep->eplist_link.prev, ohci_endpoint_t, eplist_link);
|
---|
165 | prev_ed = prev->ed;
|
---|
166 | qpos = "NOT FIRST";
|
---|
167 | }
|
---|
168 | assert(ed_next(prev_ed) == addr_to_phys(ep->ed));
|
---|
169 | prev_ed->next = ep->ed->next;
|
---|
170 | /* Make sure ED is updated */
|
---|
171 | write_barrier();
|
---|
172 |
|
---|
173 | usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.",
|
---|
174 | ep, qpos, instance->name, ep->ed->next);
|
---|
175 |
|
---|
176 | /* Remove from the endpoint list */
|
---|
177 | list_remove(&ep->eplist_link);
|
---|
178 | fibril_mutex_unlock(&instance->guard);
|
---|
179 | }
|
---|
180 | /**
|
---|
181 | * @}
|
---|
182 | */
|
---|