source: mainline/uspace/drv/ohci/endpoint_list.c@ 109d55c

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

More comment fixes and code cleanup

  • Property mode set to 100644
File size: 5.7 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 ed_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 *
72 * Does not check whether this replaces an existing list.
73 */
74void 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/** Add endpoint to the list and queue.
82 *
83 * @param[in] instance List to use.
84 * @param[in] endpoint Endpoint to add.
85 *
86 * The endpoint is added to the end of the list and queue.
87 */
88void endpoint_list_add_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
89{
90 assert(instance);
91 assert(hcd_ep);
92 usb_log_debug2("Queue %s: Adding endpoint(%p).\n",
93 instance->name, hcd_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 hcd_endpoint_t *last = list_get_instance(
105 instance->endpoint_list.prev, hcd_endpoint_t, link);
106 assert(last);
107 last_ed = last->ed;
108 }
109 /* Keep link */
110 hcd_ep->ed->next = last_ed->next;
111 /* Make sure ED is written to the memory */
112 write_barrier();
113
114 /* Add ed to the hw queue */
115 ed_append_ed(last_ed, hcd_ep->ed);
116 /* Make sure ED is updated */
117 write_barrier();
118
119 /* Add to the sw list */
120 list_append(&hcd_ep->link, &instance->endpoint_list);
121
122 hcd_endpoint_t *first = list_get_instance(
123 instance->endpoint_list.next, hcd_endpoint_t, link);
124 usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).\n",
125 hcd_ep, instance->name, first, first->ed);
126 if (last_ed == instance->list_head) {
127 usb_log_debug2("%s head ED(%p-0x%0" PRIx32 "): %x:%x:%x:%x.\n",
128 instance->name, last_ed, instance->list_head_pa,
129 last_ed->status, last_ed->td_tail, last_ed->td_head,
130 last_ed->next);
131 }
132 fibril_mutex_unlock(&instance->guard);
133}
134/*----------------------------------------------------------------------------*/
135/** Remove endpoint from the list and queue.
136 *
137 * @param[in] instance List to use.
138 * @param[in] endpoint Endpoint to remove.
139 */
140void endpoint_list_remove_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
141{
142 assert(instance);
143 assert(instance->list_head);
144 assert(hcd_ep);
145 assert(hcd_ep->ed);
146
147 fibril_mutex_lock(&instance->guard);
148
149 usb_log_debug2(
150 "Queue %s: removing endpoint(%p).\n", instance->name, hcd_ep);
151
152 const char *qpos = NULL;
153 ed_t *prev_ed;
154 /* Remove from the hardware queue */
155 if (instance->endpoint_list.next == &hcd_ep->link) {
156 /* I'm the first one here */
157 prev_ed = instance->list_head;
158 qpos = "FIRST";
159 } else {
160 hcd_endpoint_t *prev =
161 list_get_instance(hcd_ep->link.prev, hcd_endpoint_t, link);
162 prev_ed = prev->ed;
163 qpos = "NOT FIRST";
164 }
165 assert((prev_ed->next & ED_NEXT_PTR_MASK) == addr_to_phys(hcd_ep->ed));
166 prev_ed->next = hcd_ep->ed->next;
167 /* Make sure ED is updated */
168 write_barrier();
169
170 usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.\n",
171 hcd_ep, qpos, instance->name, hcd_ep->ed->next);
172
173 /* Remove from the endpoint list */
174 list_remove(&hcd_ep->link);
175 fibril_mutex_unlock(&instance->guard);
176}
177/**
178 * @}
179 */
Note: See TracBrowser for help on using the repository browser.