| 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 |
|
|---|
| 29 | /** @addtogroup drvusbohci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief OHCI driver transfer list implementation
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <libarch/barrier.h>
|
|---|
| 39 | #include "endpoint_list.h"
|
|---|
| 40 |
|
|---|
| 41 | /** Initialize transfer list structures.
|
|---|
| 42 | *
|
|---|
| 43 | * @param[in] instance Memory place to use.
|
|---|
| 44 | * @param[in] name Name of the new list.
|
|---|
| 45 | * @return Error code
|
|---|
| 46 | *
|
|---|
| 47 | * Allocates memory for internal ed_t structure.
|
|---|
| 48 | */
|
|---|
| 49 | int endpoint_list_init(endpoint_list_t *instance, const char *name)
|
|---|
| 50 | {
|
|---|
| 51 | assert(instance);
|
|---|
| 52 | instance->name = name;
|
|---|
| 53 | instance->list_head = malloc32(sizeof(ed_t));
|
|---|
| 54 | if (!instance->list_head) {
|
|---|
| 55 | usb_log_error("Failed to allocate list head.\n");
|
|---|
| 56 | return ENOMEM;
|
|---|
| 57 | }
|
|---|
| 58 | instance->list_head_pa = addr_to_phys(instance->list_head);
|
|---|
| 59 | usb_log_debug2("Transfer list %s setup with ED: %p(0x%0" PRIx32 ")).\n",
|
|---|
| 60 | name, instance->list_head, instance->list_head_pa);
|
|---|
| 61 |
|
|---|
| 62 | ed_init(instance->list_head, NULL, NULL);
|
|---|
| 63 | list_initialize(&instance->endpoint_list);
|
|---|
| 64 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 65 | return EOK;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /** Set the next list in transfer list chain.
|
|---|
| 69 | *
|
|---|
| 70 | * @param[in] instance List to lead.
|
|---|
| 71 | * @param[in] next List to append.
|
|---|
| 72 | *
|
|---|
| 73 | * Does not check whether this replaces an existing list.
|
|---|
| 74 | */
|
|---|
| 75 | void endpoint_list_set_next(
|
|---|
| 76 | const endpoint_list_t *instance, const endpoint_list_t *next)
|
|---|
| 77 | {
|
|---|
| 78 | assert(instance);
|
|---|
| 79 | assert(next);
|
|---|
| 80 | ed_append_ed(instance->list_head, next->list_head);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /** Add endpoint to the list and queue.
|
|---|
| 84 | *
|
|---|
| 85 | * @param[in] instance List to use.
|
|---|
| 86 | * @param[in] endpoint Endpoint to add.
|
|---|
| 87 | *
|
|---|
| 88 | * The endpoint is added to the end of the list and queue.
|
|---|
| 89 | */
|
|---|
| 90 | void endpoint_list_add_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
|---|
| 91 | {
|
|---|
| 92 | assert(instance);
|
|---|
| 93 | assert(ep);
|
|---|
| 94 | usb_log_debug2("Queue %s: Adding endpoint(%p).\n", instance->name, ep);
|
|---|
| 95 |
|
|---|
| 96 | fibril_mutex_lock(&instance->guard);
|
|---|
| 97 |
|
|---|
| 98 | ed_t *last_ed = NULL;
|
|---|
| 99 | /* Add to the hardware queue. */
|
|---|
| 100 | if (list_empty(&instance->endpoint_list)) {
|
|---|
| 101 | /* There are no active EDs */
|
|---|
| 102 | last_ed = instance->list_head;
|
|---|
| 103 | } else {
|
|---|
| 104 | /* There are active EDs, get the last one */
|
|---|
| 105 | ohci_endpoint_t *last = list_get_instance(
|
|---|
| 106 | list_last(&instance->endpoint_list), ohci_endpoint_t, link);
|
|---|
| 107 | last_ed = last->ed;
|
|---|
| 108 | }
|
|---|
| 109 | /* Keep link */
|
|---|
| 110 | 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, ep->ed);
|
|---|
| 116 | /* Make sure ED is updated */
|
|---|
| 117 | write_barrier();
|
|---|
| 118 |
|
|---|
| 119 | /* Add to the sw list */
|
|---|
| 120 | list_append(&ep->link, &instance->endpoint_list);
|
|---|
| 121 |
|
|---|
| 122 | ohci_endpoint_t *first = list_get_instance(
|
|---|
| 123 | list_first(&instance->endpoint_list), ohci_endpoint_t, link);
|
|---|
| 124 | usb_log_debug("HCD EP(%p) added to list %s, first is %p(%p).\n",
|
|---|
| 125 | 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 | */
|
|---|
| 140 | void endpoint_list_remove_ep(endpoint_list_t *instance, ohci_endpoint_t *ep)
|
|---|
| 141 | {
|
|---|
| 142 | assert(instance);
|
|---|
| 143 | assert(instance->list_head);
|
|---|
| 144 | assert(ep);
|
|---|
| 145 | assert(ep->ed);
|
|---|
| 146 |
|
|---|
| 147 | fibril_mutex_lock(&instance->guard);
|
|---|
| 148 |
|
|---|
| 149 | usb_log_debug2("Queue %s: removing endpoint(%p).\n", instance->name, ep);
|
|---|
| 150 |
|
|---|
| 151 | const char *qpos = NULL;
|
|---|
| 152 | ed_t *prev_ed;
|
|---|
| 153 | /* Remove from the hardware queue */
|
|---|
| 154 | if (list_first(&instance->endpoint_list) == &ep->link) {
|
|---|
| 155 | /* I'm the first one here */
|
|---|
| 156 | prev_ed = instance->list_head;
|
|---|
| 157 | qpos = "FIRST";
|
|---|
| 158 | } else {
|
|---|
| 159 | ohci_endpoint_t *prev =
|
|---|
| 160 | list_get_instance(ep->link.prev, ohci_endpoint_t, link);
|
|---|
| 161 | prev_ed = prev->ed;
|
|---|
| 162 | qpos = "NOT FIRST";
|
|---|
| 163 | }
|
|---|
| 164 | assert(ed_next(prev_ed) == addr_to_phys(ep->ed));
|
|---|
| 165 | prev_ed->next = ep->ed->next;
|
|---|
| 166 | /* Make sure ED is updated */
|
|---|
| 167 | write_barrier();
|
|---|
| 168 |
|
|---|
| 169 | usb_log_debug("HCD EP(%p) removed (%s) from %s, next %x.\n",
|
|---|
| 170 | ep, qpos, instance->name, ep->ed->next);
|
|---|
| 171 |
|
|---|
| 172 | /* Remove from the endpoint list */
|
|---|
| 173 | list_remove(&ep->link);
|
|---|
| 174 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 175 | }
|
|---|
| 176 | /**
|
|---|
| 177 | * @}
|
|---|
| 178 | */
|
|---|