| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup drvusbohci
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief OHCI driver transfer list implementation
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <libarch/barrier.h>
|
|---|
| 40 |
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <usb/host/utils/malloc32.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "endpoint_list.h"
|
|---|
| 45 |
|
|---|
| 46 | /** Initialize transfer list structures.
|
|---|
| 47 | *
|
|---|
| 48 | * @param[in] instance Memory place to use.
|
|---|
| 49 | * @param[in] name Name of the new list.
|
|---|
| 50 | * @return Error code
|
|---|
| 51 | *
|
|---|
| 52 | * Allocates memory for internal ed_t structure.
|
|---|
| 53 | */
|
|---|
| 54 | errno_t endpoint_list_init(endpoint_list_t *instance, const char *name)
|
|---|
| 55 | {
|
|---|
| 56 | assert(instance);
|
|---|
| 57 | instance->name = name;
|
|---|
| 58 | instance->list_head = malloc32(sizeof(ed_t));
|
|---|
| 59 | if (!instance->list_head) {
|
|---|
| 60 | usb_log_error("Failed to allocate list head.");
|
|---|
| 61 | return ENOMEM;
|
|---|
| 62 | }
|
|---|
| 63 | instance->list_head_pa = addr_to_phys(instance->list_head);
|
|---|
| 64 | usb_log_debug2("Transfer list %s setup with ED: %p(0x%0" PRIx32 ")).",
|
|---|
| 65 | name, instance->list_head, instance->list_head_pa);
|
|---|
| 66 |
|
|---|
| 67 | ed_init(instance->list_head, NULL, NULL);
|
|---|
| 68 | list_initialize(&instance->endpoint_list);
|
|---|
| 69 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 70 | return EOK;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Set the next list in transfer list chain.
|
|---|
| 74 | *
|
|---|
| 75 | * @param[in] instance List to lead.
|
|---|
| 76 | * @param[in] next List to append.
|
|---|
| 77 | *
|
|---|
| 78 | * Does not check whether this replaces an existing list.
|
|---|
| 79 | */
|
|---|
| 80 | void endpoint_list_set_next(
|
|---|
| 81 | const endpoint_list_t *instance, const endpoint_list_t *next)
|
|---|
| 82 | {
|
|---|
| 83 | assert(instance);
|
|---|
| 84 | assert(next);
|
|---|
| 85 | ed_append_ed(instance->list_head, next->list_head);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /** Add endpoint to the list and queue.
|
|---|
| 89 | *
|
|---|
| 90 | * @param[in] instance List to use.
|
|---|
| 91 | * @param[in] endpoint Endpoint to add.
|
|---|
| 92 | *
|
|---|
| 93 | * The endpoint is added to the end of the list and queue.
|
|---|
| 94 | */
|
|---|
| 95 | void endpoint_list_add_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
|---|
| 96 | {
|
|---|
| 97 | assert(instance);
|
|---|
| 98 | assert(ep);
|
|---|
| 99 | usb_log_debug2("Queue %s: Adding endpoint(%p).", instance->name, ep);
|
|---|
| 100 |
|
|---|
| 101 | fibril_mutex_lock(&instance->guard);
|
|---|
| 102 |
|
|---|
| 103 | ed_t *last_ed = NULL;
|
|---|
| 104 | /* Add to the hardware queue. */
|
|---|
| 105 | if (list_empty(&instance->endpoint_list)) {
|
|---|
| 106 | /* There are no active EDs */
|
|---|
| 107 | last_ed = instance->list_head;
|
|---|
| 108 | } else {
|
|---|
| 109 | /* There are active EDs, get the last one */
|
|---|
| 110 | ohci_endpoint_t *last = list_get_instance(
|
|---|
| 111 | list_last(&instance->endpoint_list), ohci_endpoint_t, eplist_link);
|
|---|
| 112 | last_ed = last->ed;
|
|---|
| 113 | }
|
|---|
| 114 | /* Keep link */
|
|---|
| 115 | ep->ed->next = last_ed->next;
|
|---|
| 116 | /* Make sure ED is written to the memory */
|
|---|
| 117 | write_barrier();
|
|---|
| 118 |
|
|---|
| 119 | /* Add ed to the hw queue */
|
|---|
| 120 | ed_append_ed(last_ed, ep->ed);
|
|---|
| 121 | /* Make sure ED is updated */
|
|---|
| 122 | write_barrier();
|
|---|
| 123 |
|
|---|
| 124 | /* Add to the sw list */
|
|---|
| 125 | list_append(&ep->eplist_link, &instance->endpoint_list);
|
|---|
| 126 |
|
|---|
| 127 | ohci_endpoint_t *first = list_get_instance(
|
|---|
| 128 | list_first(&instance->endpoint_list), ohci_endpoint_t, eplist_link);
|
|---|
| 129 | usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).",
|
|---|
| 130 | ep, instance->name, first, first->ed);
|
|---|
| 131 | if (last_ed == instance->list_head) {
|
|---|
| 132 | usb_log_debug2("%s head ED(%p-0x%0" PRIx32 "): %x:%x:%x:%x.",
|
|---|
| 133 | instance->name, last_ed, instance->list_head_pa,
|
|---|
| 134 | last_ed->status, last_ed->td_tail, last_ed->td_head,
|
|---|
| 135 | last_ed->next);
|
|---|
| 136 | }
|
|---|
| 137 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Remove endpoint from the list and queue.
|
|---|
| 141 | *
|
|---|
| 142 | * @param[in] instance List to use.
|
|---|
| 143 | * @param[in] endpoint Endpoint to remove.
|
|---|
| 144 | */
|
|---|
| 145 | void endpoint_list_remove_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
|---|
| 146 | {
|
|---|
| 147 | assert(instance);
|
|---|
| 148 | assert(instance->list_head);
|
|---|
| 149 | assert(ep);
|
|---|
| 150 | assert(ep->ed);
|
|---|
| 151 |
|
|---|
| 152 | fibril_mutex_lock(&instance->guard);
|
|---|
| 153 |
|
|---|
| 154 | usb_log_debug2("Queue %s: removing endpoint(%p).", instance->name, ep);
|
|---|
| 155 |
|
|---|
| 156 | const char *qpos = NULL;
|
|---|
| 157 | ed_t *prev_ed;
|
|---|
| 158 | /* Remove from the hardware queue */
|
|---|
| 159 | if (list_first(&instance->endpoint_list) == &ep->eplist_link) {
|
|---|
| 160 | /* I'm the first one here */
|
|---|
| 161 | prev_ed = instance->list_head;
|
|---|
| 162 | qpos = "FIRST";
|
|---|
| 163 | } else {
|
|---|
| 164 | ohci_endpoint_t *prev =
|
|---|
| 165 | list_get_instance(ep->eplist_link.prev, ohci_endpoint_t, eplist_link);
|
|---|
| 166 | prev_ed = prev->ed;
|
|---|
| 167 | qpos = "NOT FIRST";
|
|---|
| 168 | }
|
|---|
| 169 | assert(ed_next(prev_ed) == addr_to_phys(ep->ed));
|
|---|
| 170 | prev_ed->next = ep->ed->next;
|
|---|
| 171 | /* Make sure ED is updated */
|
|---|
| 172 | write_barrier();
|
|---|
| 173 |
|
|---|
| 174 | usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.",
|
|---|
| 175 | ep, qpos, instance->name, ep->ed->next);
|
|---|
| 176 |
|
|---|
| 177 | /* Remove from the endpoint list */
|
|---|
| 178 | list_remove(&ep->eplist_link);
|
|---|
| 179 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 180 | }
|
|---|
| 181 | /**
|
|---|
| 182 | * @}
|
|---|
| 183 | */
|
|---|