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