source: mainline/uspace/drv/bus/usb/ehci/endpoint_list.c@ 5f5321ee

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

ehci: Implement endpoint list enqueue

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2014 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 drvusbehci
30 * @{
31 */
32/** @file
33 * @brief EHCI 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
42#include "utils/malloc32.h"
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 */
53int endpoint_list_init(endpoint_list_t *instance, const char *name)
54{
55 assert(instance);
56 instance->name = name;
57 instance->list_head = malloc32(sizeof(qh_t));
58 if (!instance->list_head) {
59 usb_log_error("Failed to allocate list head.\n");
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 ")).\n",
64 name, instance->list_head, instance->list_head_pa);
65
66 qh_init(instance->list_head, NULL);
67 list_initialize(&instance->endpoint_list);
68 fibril_mutex_initialize(&instance->guard);
69 return EOK;
70}
71
72/** Add endpoint to the end of the list and queue.
73 *
74 * @param[in] instance List to use.
75 * @param[in] endpoint Endpoint to add.
76 *
77 * The endpoint is added to the end of the list and queue.
78 */
79void endpoint_list_append_ep(endpoint_list_t *instance, ehci_endpoint_t *ep)
80{
81 assert(instance);
82 assert(ep);
83 assert(ep->qh);
84 usb_log_debug2("Queue %s: Append endpoint(%p).\n", instance->name, ep);
85
86 fibril_mutex_lock(&instance->guard);
87
88 qh_t *last_qh = NULL;
89 /* Add to the hardware queue. */
90 if (list_empty(&instance->endpoint_list)) {
91 /* There are no active EDs */
92 last_qh = instance->list_head;
93 } else {
94 /* There are active EDs, get the last one */
95 ehci_endpoint_t *last = ehci_endpoint_list_instance(
96 list_last(&instance->endpoint_list));
97 last_qh = last->qh;
98 }
99 assert(last_qh);
100 /* Keep link */
101 ep->qh->horizontal = last_qh->horizontal;
102 /* Make sure QH update is written to the memory */
103 write_barrier();
104
105 /* Add QH to the hw queue */
106 qh_append_qh(last_qh, ep->qh);
107 /* Make sure QH is updated */
108 write_barrier();
109 /* Add to the sw list */
110 list_append(&ep->link, &instance->endpoint_list);
111
112 ehci_endpoint_t *first = ehci_endpoint_list_instance(
113 list_first(&instance->endpoint_list));
114 usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).\n",
115 ep, instance->name, first, first->qh);
116 if (last_qh == instance->list_head) {
117 usb_log_debug2("%s head ED(%p-0x%0" PRIx32 "): %x:%x.\n",
118 instance->name, last_qh, instance->list_head_pa,
119 last_qh->status, last_qh->horizontal);
120 }
121 fibril_mutex_unlock(&instance->guard);
122}
123
124/** Add endpoint to the beginning of the list and queue.
125 *
126 * @param[in] instance List to use.
127 * @param[in] endpoint Endpoint to add.
128 *
129 * The endpoint is added to the end of the list and queue.
130 */
131void endpoint_list_prepend_ep(endpoint_list_t *instance, ehci_endpoint_t *ep)
132{
133 assert(instance);
134 assert(ep);
135 assert(ep->qh);
136 usb_log_debug2("Queue %s: Prepend endpoint(%p).\n", instance->name, ep);
137
138 fibril_mutex_lock(&instance->guard);
139 ep->qh->horizontal = instance->list_head->horizontal;
140 /* Make sure QH is updated */
141 write_barrier();
142 /* Add QH to the hw queue */
143 qh_append_qh(instance->list_head, ep->qh);
144 /* Add to the sw list */
145 list_prepend(&ep->link, &instance->endpoint_list);
146 fibril_mutex_unlock(&instance->guard);
147
148}
149/** Remove endpoint from the list and queue.
150 *
151 * @param[in] instance List to use.
152 * @param[in] endpoint Endpoint to remove.
153 */
154void endpoint_list_remove_ep(endpoint_list_t *instance, ehci_endpoint_t *ep)
155{
156 assert(instance);
157 assert(instance->list_head);
158 assert(ep);
159 assert(ep->qh);
160
161 fibril_mutex_lock(&instance->guard);
162
163 usb_log_debug2("Queue %s: removing endpoint(%p).\n", instance->name, ep);
164
165 const char *qpos = NULL;
166 qh_t *prev_qh;
167 /* Remove from the hardware queue */
168 if (list_first(&instance->endpoint_list) == &ep->link) {
169 /* I'm the first one here */
170 prev_qh = instance->list_head;
171 qpos = "FIRST";
172 } else {
173 ehci_endpoint_t *prev =
174 list_get_instance(ep->link.prev, ehci_endpoint_t, link);
175 prev_qh = prev->qh;
176 qpos = "NOT FIRST";
177 }
178 assert(qh_next(prev_qh) == addr_to_phys(ep->qh));
179 prev_qh->next = ep->qh->next;
180 /* Make sure ED is updated */
181 write_barrier();
182
183 usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.\n",
184 ep, qpos, instance->name, ep->qh->next);
185
186 /* Remove from the endpoint list */
187 list_remove(&ep->link);
188 fibril_mutex_unlock(&instance->guard);
189}
190/**
191 * @}
192 */
193
Note: See TracBrowser for help on using the repository browser.