[f98b8269] | 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 |
|
---|
[5a2c42b] | 37 | #include "endpoint_list.h"
|
---|
[f98b8269] | 38 |
|
---|
| 39 | /** Initialize transfer list structures.
|
---|
| 40 | *
|
---|
| 41 | * @param[in] instance Memory place to use.
|
---|
| 42 | * @param[in] name Name of the new list.
|
---|
| 43 | * @return Error code
|
---|
| 44 | *
|
---|
| 45 | * Allocates memory for internal qh_t structure.
|
---|
| 46 | */
|
---|
[5a2c42b] | 47 | int endpoint_list_init(endpoint_list_t *instance, const char *name)
|
---|
[f98b8269] | 48 | {
|
---|
| 49 | assert(instance);
|
---|
| 50 | instance->name = name;
|
---|
| 51 | instance->list_head = malloc32(sizeof(ed_t));
|
---|
| 52 | if (!instance->list_head) {
|
---|
| 53 | usb_log_error("Failed to allocate list head.\n");
|
---|
| 54 | return ENOMEM;
|
---|
| 55 | }
|
---|
| 56 | instance->list_head_pa = addr_to_phys(instance->list_head);
|
---|
| 57 | usb_log_debug2("Transfer list %s setup with ED: %p(%p).\n",
|
---|
| 58 | name, instance->list_head, instance->list_head_pa);
|
---|
| 59 |
|
---|
[7786cea] | 60 | ed_init(instance->list_head, NULL);
|
---|
[5a2c42b] | 61 | list_initialize(&instance->endpoint_list);
|
---|
[f98b8269] | 62 | fibril_mutex_initialize(&instance->guard);
|
---|
| 63 | return EOK;
|
---|
| 64 | }
|
---|
| 65 | /*----------------------------------------------------------------------------*/
|
---|
| 66 | /** Set the next list in transfer list chain.
|
---|
| 67 | *
|
---|
| 68 | * @param[in] instance List to lead.
|
---|
| 69 | * @param[in] next List to append.
|
---|
| 70 | * @return Error code
|
---|
| 71 | *
|
---|
| 72 | * Does not check whether this replaces an existing list .
|
---|
| 73 | */
|
---|
[5a2c42b] | 74 | void endpoint_list_set_next(endpoint_list_t *instance, endpoint_list_t *next)
|
---|
[f98b8269] | 75 | {
|
---|
| 76 | assert(instance);
|
---|
| 77 | assert(next);
|
---|
| 78 | ed_append_ed(instance->list_head, next->list_head);
|
---|
| 79 | }
|
---|
| 80 | /*----------------------------------------------------------------------------*/
|
---|
[5a2c42b] | 81 | /** Submit transfer endpoint to the list and queue.
|
---|
[f98b8269] | 82 | *
|
---|
| 83 | * @param[in] instance List to use.
|
---|
[5a2c42b] | 84 | * @param[in] endpoint Transfer endpoint to submit.
|
---|
[f98b8269] | 85 | * @return Error code
|
---|
| 86 | *
|
---|
[5a2c42b] | 87 | * The endpoint is added to the end of the list and queue.
|
---|
[f98b8269] | 88 | */
|
---|
[5a2c42b] | 89 | void endpoint_list_add_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
|
---|
[f98b8269] | 90 | {
|
---|
| 91 | assert(instance);
|
---|
[5a2c42b] | 92 | assert(hcd_ep);
|
---|
| 93 | usb_log_debug2("Queue %s: Adding endpoint(%p).\n",
|
---|
| 94 | instance->name, hcd_ep);
|
---|
[f98b8269] | 95 |
|
---|
| 96 | fibril_mutex_lock(&instance->guard);
|
---|
| 97 |
|
---|
| 98 | ed_t *last_ed = NULL;
|
---|
| 99 | /* Add to the hardware queue. */
|
---|
[5a2c42b] | 100 | if (list_empty(&instance->endpoint_list)) {
|
---|
[f98b8269] | 101 | /* There is nothing scheduled */
|
---|
| 102 | last_ed = instance->list_head;
|
---|
| 103 | } else {
|
---|
| 104 | /* There is something scheduled */
|
---|
[5a2c42b] | 105 | hcd_endpoint_t *last = list_get_instance(
|
---|
| 106 | instance->endpoint_list.prev, hcd_endpoint_t, link);
|
---|
| 107 | last_ed = last->ed;
|
---|
[f98b8269] | 108 | }
|
---|
| 109 | /* keep link */
|
---|
[5a2c42b] | 110 | hcd_ep->ed->next = last_ed->next;
|
---|
| 111 | ed_append_ed(last_ed, hcd_ep->ed);
|
---|
[f98b8269] | 112 |
|
---|
| 113 | asm volatile ("": : :"memory");
|
---|
| 114 |
|
---|
| 115 | /* Add to the driver list */
|
---|
[5a2c42b] | 116 | list_append(&hcd_ep->link, &instance->endpoint_list);
|
---|
[f98b8269] | 117 |
|
---|
[5a2c42b] | 118 | hcd_endpoint_t *first = list_get_instance(
|
---|
| 119 | instance->endpoint_list.next, hcd_endpoint_t, link);
|
---|
| 120 | usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).\n",
|
---|
| 121 | hcd_ep, instance->name, first, first->ed);
|
---|
[8790650] | 122 | if (last_ed == instance->list_head) {
|
---|
[aa9ccf7] | 123 | usb_log_debug2("%s head ED(%p-%p): %x:%x:%x:%x.\n",
|
---|
| 124 | instance->name, last_ed, instance->list_head_pa,
|
---|
| 125 | last_ed->status, last_ed->td_tail, last_ed->td_head,
|
---|
| 126 | last_ed->next);
|
---|
[8790650] | 127 | }
|
---|
[f98b8269] | 128 | fibril_mutex_unlock(&instance->guard);
|
---|
| 129 | }
|
---|
| 130 | /*----------------------------------------------------------------------------*/
|
---|
[5a2c42b] | 131 | #if 0
|
---|
| 132 | /** Create list for finished endpoints.
|
---|
[f98b8269] | 133 | *
|
---|
| 134 | * @param[in] instance List to use.
|
---|
| 135 | * @param[in] done list to fill
|
---|
| 136 | */
|
---|
[5a2c42b] | 137 | void endpoint_list_remove_finished(endpoint_list_t *instance, link_t *done)
|
---|
[f98b8269] | 138 | {
|
---|
| 139 | assert(instance);
|
---|
| 140 | assert(done);
|
---|
| 141 |
|
---|
| 142 | fibril_mutex_lock(&instance->guard);
|
---|
[5a2c42b] | 143 | usb_log_debug2("Checking list %s for completed endpointes(%d).\n",
|
---|
| 144 | instance->name, list_count(&instance->endpoint_list));
|
---|
| 145 | link_t *current = instance->endpoint_list.next;
|
---|
| 146 | while (current != &instance->endpoint_list) {
|
---|
[f98b8269] | 147 | link_t *next = current->next;
|
---|
[5a2c42b] | 148 | hcd_endpoint_t *endpoint =
|
---|
| 149 | list_get_instance(current, hcd_endpoint_t, link);
|
---|
[f98b8269] | 150 |
|
---|
[5a2c42b] | 151 | if (endpoint_is_complete(endpoint)) {
|
---|
[f98b8269] | 152 | /* Save for post-processing */
|
---|
[5a2c42b] | 153 | endpoint_list_remove_endpoint(instance, endpoint);
|
---|
[f98b8269] | 154 | list_append(current, done);
|
---|
| 155 | }
|
---|
| 156 | current = next;
|
---|
| 157 | }
|
---|
| 158 | fibril_mutex_unlock(&instance->guard);
|
---|
| 159 | }
|
---|
| 160 | /*----------------------------------------------------------------------------*/
|
---|
[5a2c42b] | 161 | /** Walk the list and abort all endpointes.
|
---|
[f98b8269] | 162 | *
|
---|
| 163 | * @param[in] instance List to use.
|
---|
| 164 | */
|
---|
[5a2c42b] | 165 | void endpoint_list_abort_all(endpoint_list_t *instance)
|
---|
[f98b8269] | 166 | {
|
---|
| 167 | fibril_mutex_lock(&instance->guard);
|
---|
[5a2c42b] | 168 | while (!list_empty(&instance->endpoint_list)) {
|
---|
| 169 | link_t *current = instance->endpoint_list.next;
|
---|
| 170 | hcd_endpoint_t *endpoint =
|
---|
| 171 | list_get_instance(current, hcd_endpoint_t, link);
|
---|
| 172 | endpoint_list_remove_endpoint(instance, endpoint);
|
---|
| 173 | hcd_endpoint_finish_error(endpoint, EIO);
|
---|
[f98b8269] | 174 | }
|
---|
| 175 | fibril_mutex_unlock(&instance->guard);
|
---|
| 176 | }
|
---|
[5a2c42b] | 177 | #endif
|
---|
[f98b8269] | 178 | /*----------------------------------------------------------------------------*/
|
---|
[5a2c42b] | 179 | /** Remove a transfer endpoint from the list and queue.
|
---|
[f98b8269] | 180 | *
|
---|
| 181 | * @param[in] instance List to use.
|
---|
[5a2c42b] | 182 | * @param[in] endpoint Transfer endpoint to remove.
|
---|
[f98b8269] | 183 | * @return Error code
|
---|
| 184 | *
|
---|
| 185 | * Does not lock the transfer list, caller is responsible for that.
|
---|
| 186 | */
|
---|
[5a2c42b] | 187 | void endpoint_list_remove_ep(endpoint_list_t *instance, hcd_endpoint_t *hcd_ep)
|
---|
[f98b8269] | 188 | {
|
---|
| 189 | assert(instance);
|
---|
| 190 | assert(instance->list_head);
|
---|
[5a2c42b] | 191 | assert(hcd_ep);
|
---|
| 192 | assert(hcd_ep->ed);
|
---|
[7013b14] | 193 |
|
---|
| 194 | fibril_mutex_lock(&instance->guard);
|
---|
[f98b8269] | 195 |
|
---|
| 196 | usb_log_debug2(
|
---|
[5a2c42b] | 197 | "Queue %s: removing endpoint(%p).\n", instance->name, hcd_ep);
|
---|
[f98b8269] | 198 |
|
---|
| 199 | const char *qpos = NULL;
|
---|
[5a2c42b] | 200 | ed_t *prev_ed;
|
---|
[f98b8269] | 201 | /* Remove from the hardware queue */
|
---|
[5a2c42b] | 202 | if (instance->endpoint_list.next == &hcd_ep->link) {
|
---|
[f98b8269] | 203 | /* I'm the first one here */
|
---|
[5a2c42b] | 204 | prev_ed = instance->list_head;
|
---|
[f98b8269] | 205 | qpos = "FIRST";
|
---|
| 206 | } else {
|
---|
[5a2c42b] | 207 | hcd_endpoint_t *prev =
|
---|
| 208 | list_get_instance(hcd_ep->link.prev, hcd_endpoint_t, link);
|
---|
| 209 | prev_ed = prev->ed;
|
---|
[f98b8269] | 210 | qpos = "NOT FIRST";
|
---|
| 211 | }
|
---|
[5a2c42b] | 212 | assert((prev_ed->next & ED_NEXT_PTR_MASK) == addr_to_phys(hcd_ep->ed));
|
---|
| 213 | prev_ed->next = hcd_ep->ed->next;
|
---|
| 214 |
|
---|
[f98b8269] | 215 | asm volatile ("": : :"memory");
|
---|
[5a2c42b] | 216 | usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.\n",
|
---|
| 217 | hcd_ep, qpos, instance->name, hcd_ep->ed->next);
|
---|
[f98b8269] | 218 |
|
---|
[5a2c42b] | 219 | /* Remove from the endpoint list */
|
---|
| 220 | list_remove(&hcd_ep->link);
|
---|
[7013b14] | 221 | fibril_mutex_unlock(&instance->guard);
|
---|
[f98b8269] | 222 | }
|
---|
| 223 | /**
|
---|
| 224 | * @}
|
---|
| 225 | */
|
---|