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 usb
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI driver
|
---|
33 | */
|
---|
34 | #include <errno.h>
|
---|
35 |
|
---|
36 | #include <usb/debug.h>
|
---|
37 |
|
---|
38 | #include "transfer_list.h"
|
---|
39 |
|
---|
40 | static void transfer_list_remove_batch(
|
---|
41 | transfer_list_t *instance, batch_t *batch);
|
---|
42 | /*----------------------------------------------------------------------------*/
|
---|
43 | /** Initializes transfer list structures.
|
---|
44 | *
|
---|
45 | * @param[in] instance Memory place to use.
|
---|
46 | * @param[in] name Name of te new list.
|
---|
47 | * @return Error code
|
---|
48 | *
|
---|
49 | * Allocates memory for internal qh_t structure.
|
---|
50 | */
|
---|
51 | int transfer_list_init(transfer_list_t *instance, const char *name)
|
---|
52 | {
|
---|
53 | assert(instance);
|
---|
54 | instance->name = name;
|
---|
55 | instance->queue_head = malloc32(sizeof(qh_t));
|
---|
56 | if (!instance->queue_head) {
|
---|
57 | usb_log_error("Failed to allocate queue head.\n");
|
---|
58 | return ENOMEM;
|
---|
59 | }
|
---|
60 | instance->queue_head_pa = addr_to_phys(instance->queue_head);
|
---|
61 |
|
---|
62 | qh_init(instance->queue_head);
|
---|
63 | list_initialize(&instance->batch_list);
|
---|
64 | fibril_mutex_initialize(&instance->guard);
|
---|
65 | return EOK;
|
---|
66 | }
|
---|
67 | /*----------------------------------------------------------------------------*/
|
---|
68 | /** Set the next list in chain.
|
---|
69 | *
|
---|
70 | * @param[in] instance List to lead.
|
---|
71 | * @param[in] next List to append.
|
---|
72 | * @return Error code
|
---|
73 | *
|
---|
74 | * Does not check whether there was a next list already.
|
---|
75 | */
|
---|
76 | void transfer_list_set_next(transfer_list_t *instance, transfer_list_t *next)
|
---|
77 | {
|
---|
78 | assert(instance);
|
---|
79 | assert(next);
|
---|
80 | if (!instance->queue_head)
|
---|
81 | return;
|
---|
82 | /* set both next and element to point to the same QH */
|
---|
83 | qh_set_next_qh(instance->queue_head, next->queue_head_pa);
|
---|
84 | qh_set_element_qh(instance->queue_head, next->queue_head_pa);
|
---|
85 | }
|
---|
86 | /*----------------------------------------------------------------------------*/
|
---|
87 | /** Submits a new transfer batch to list and queue.
|
---|
88 | *
|
---|
89 | * @param[in] instance List to use.
|
---|
90 | * @param[in] batch Transfer batch to submit.
|
---|
91 | * @return Error code
|
---|
92 | */
|
---|
93 | void transfer_list_add_batch(transfer_list_t *instance, batch_t *batch)
|
---|
94 | {
|
---|
95 | assert(instance);
|
---|
96 | assert(batch);
|
---|
97 | usb_log_debug2("Queue %s: Adding batch(%p).\n", instance->name, batch);
|
---|
98 |
|
---|
99 | const uint32_t pa = addr_to_phys(batch->qh);
|
---|
100 | assert((pa & LINK_POINTER_ADDRESS_MASK) == pa);
|
---|
101 |
|
---|
102 | /* New batch will be added to the end of the current list
|
---|
103 | * so set the link accordingly */
|
---|
104 | qh_set_next_qh(batch->qh, instance->queue_head->next);
|
---|
105 |
|
---|
106 | fibril_mutex_lock(&instance->guard);
|
---|
107 |
|
---|
108 | if (list_empty(&instance->batch_list)) {
|
---|
109 | /* There is nothing scheduled */
|
---|
110 | qh_t *qh = instance->queue_head;
|
---|
111 | assert(qh->element == qh->next);
|
---|
112 | qh_set_element_qh(qh, pa);
|
---|
113 | } else {
|
---|
114 | /* There is something scheduled */
|
---|
115 | batch_t *last = list_get_instance(
|
---|
116 | instance->batch_list.prev, batch_t, link);
|
---|
117 | qh_set_next_qh(last->qh, pa);
|
---|
118 | }
|
---|
119 | list_append(&batch->link, &instance->batch_list);
|
---|
120 |
|
---|
121 | batch_t *first = list_get_instance(
|
---|
122 | instance->batch_list.next, batch_t, link);
|
---|
123 | usb_log_debug("Batch(%p) added to queue %s, first is %p.\n",
|
---|
124 | batch, instance->name, first);
|
---|
125 | fibril_mutex_unlock(&instance->guard);
|
---|
126 | }
|
---|
127 | /*----------------------------------------------------------------------------*/
|
---|
128 | /** Removes a transfer batch from the list and queue.
|
---|
129 | *
|
---|
130 | * @param[in] instance List to use.
|
---|
131 | * @param[in] batch Transfer batch to remove.
|
---|
132 | * @return Error code
|
---|
133 | *
|
---|
134 | * Does not lock the transfer list, caller is responsible for that.
|
---|
135 | */
|
---|
136 | void transfer_list_remove_batch(transfer_list_t *instance, batch_t *batch)
|
---|
137 | {
|
---|
138 | assert(instance);
|
---|
139 | assert(batch);
|
---|
140 | assert(instance->queue_head);
|
---|
141 | assert(batch->qh);
|
---|
142 | usb_log_debug2(
|
---|
143 | "Queue %s: removing batch(%p).\n", instance->name, batch);
|
---|
144 |
|
---|
145 | const char * pos = NULL;
|
---|
146 | if (batch->link.prev == &instance->batch_list) {
|
---|
147 | /* I'm the first one here */
|
---|
148 | qh_set_element_qh(instance->queue_head, batch->qh->next);
|
---|
149 | pos = "FIRST";
|
---|
150 | } else {
|
---|
151 | batch_t *prev =
|
---|
152 | list_get_instance(batch->link.prev, batch_t, link);
|
---|
153 | qh_set_next_qh(prev->qh, batch->qh->next);
|
---|
154 | pos = "NOT FIRST";
|
---|
155 | }
|
---|
156 | list_remove(&batch->link);
|
---|
157 | usb_log_debug("Batch(%p) removed (%s) from %s, next element %x.\n",
|
---|
158 | batch, pos, instance->name, batch->qh->next);
|
---|
159 | }
|
---|
160 | /*----------------------------------------------------------------------------*/
|
---|
161 | /** Checks list for finished batches.
|
---|
162 | *
|
---|
163 | * @param[in] instance List to use.
|
---|
164 | * @return Error code
|
---|
165 | */
|
---|
166 | void transfer_list_remove_finished(transfer_list_t *instance)
|
---|
167 | {
|
---|
168 | assert(instance);
|
---|
169 |
|
---|
170 | LIST_INITIALIZE(done);
|
---|
171 |
|
---|
172 | fibril_mutex_lock(&instance->guard);
|
---|
173 | link_t *current = instance->batch_list.next;
|
---|
174 | while (current != &instance->batch_list) {
|
---|
175 | link_t *next = current->next;
|
---|
176 | batch_t *batch = list_get_instance(current, batch_t, link);
|
---|
177 |
|
---|
178 | if (batch_is_complete(batch)) {
|
---|
179 | transfer_list_remove_batch(instance, batch);
|
---|
180 | list_append(current, &done);
|
---|
181 | }
|
---|
182 | current = next;
|
---|
183 | }
|
---|
184 | fibril_mutex_unlock(&instance->guard);
|
---|
185 |
|
---|
186 | while (!list_empty(&done)) {
|
---|
187 | link_t *item = done.next;
|
---|
188 | list_remove(item);
|
---|
189 | batch_t *batch = list_get_instance(item, batch_t, link);
|
---|
190 | batch->next_step(batch);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | /**
|
---|
194 | * @}
|
---|
195 | */
|
---|