| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 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 drvusbehci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief EHCI driver transfer list implementation
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <libarch/barrier.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "endpoint_list.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Initialize transfer list structures.
|
|---|
| 45 | *
|
|---|
| 46 | * @param[in] instance Memory place to use.
|
|---|
| 47 | * @param[in] name Name of the new list.
|
|---|
| 48 | * @return Error code
|
|---|
| 49 | *
|
|---|
| 50 | * Allocates memory for internal ed_t structure.
|
|---|
| 51 | */
|
|---|
| 52 | int endpoint_list_init(endpoint_list_t *instance, const char *name)
|
|---|
| 53 | {
|
|---|
| 54 | assert(instance);
|
|---|
| 55 | instance->name = name;
|
|---|
| 56 | instance->list_head = malloc32(sizeof(qh_t));
|
|---|
| 57 | if (!instance->list_head) {
|
|---|
| 58 | usb_log_error("EPL(%p-%s): Failed to allocate list head.",
|
|---|
| 59 | instance, name);
|
|---|
| 60 | return ENOMEM;
|
|---|
| 61 | }
|
|---|
| 62 | qh_init(instance->list_head, NULL);
|
|---|
| 63 |
|
|---|
| 64 | list_initialize(&instance->endpoint_list);
|
|---|
| 65 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 66 |
|
|---|
| 67 | usb_log_debug2("EPL(%p-%s): Transfer list setup with ED: %p(%"PRIxn").",
|
|---|
| 68 | instance, name, instance->list_head,
|
|---|
| 69 | addr_to_phys(instance->list_head));
|
|---|
| 70 |
|
|---|
| 71 | return EOK;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void endpoint_list_chain(endpoint_list_t *instance, const endpoint_list_t *next)
|
|---|
| 75 | {
|
|---|
| 76 | assert(instance);
|
|---|
| 77 | assert(next);
|
|---|
| 78 | assert(instance->list_head);
|
|---|
| 79 | assert(next->list_head);
|
|---|
| 80 |
|
|---|
| 81 | usb_log_debug2("EPL(%p-%s): Chained with EPL(%p-%s).",
|
|---|
| 82 | instance, instance->name, next, next->name);
|
|---|
| 83 |
|
|---|
| 84 | qh_append_qh(instance->list_head, next->list_head);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /** Add endpoint to the end of the list and queue.
|
|---|
| 88 | *
|
|---|
| 89 | * @param[in] instance List to use.
|
|---|
| 90 | * @param[in] endpoint Endpoint to add.
|
|---|
| 91 | *
|
|---|
| 92 | * The endpoint is added to the end of the list and queue.
|
|---|
| 93 | */
|
|---|
| 94 | void endpoint_list_append_ep(endpoint_list_t *instance, ehci_endpoint_t *ep)
|
|---|
| 95 | {
|
|---|
| 96 | assert(instance);
|
|---|
| 97 | assert(ep);
|
|---|
| 98 | assert(ep->qh);
|
|---|
| 99 | usb_log_debug2("EPL(%p-%s): Append endpoint(%p).\n",
|
|---|
| 100 | instance, instance->name, ep);
|
|---|
| 101 |
|
|---|
| 102 | fibril_mutex_lock(&instance->guard);
|
|---|
| 103 |
|
|---|
| 104 | qh_t *last_qh = NULL;
|
|---|
| 105 | /* Add to the hardware queue. */
|
|---|
| 106 | if (list_empty(&instance->endpoint_list)) {
|
|---|
| 107 | /* There are no active EDs */
|
|---|
| 108 | last_qh = instance->list_head;
|
|---|
| 109 | } else {
|
|---|
| 110 | /* There are active EDs, get the last one */
|
|---|
| 111 | ehci_endpoint_t *last = ehci_endpoint_list_instance(
|
|---|
| 112 | list_last(&instance->endpoint_list));
|
|---|
| 113 | last_qh = last->qh;
|
|---|
| 114 | }
|
|---|
| 115 | assert(last_qh);
|
|---|
| 116 | /* Keep link, this might point to the queue QH, or next chained queue */
|
|---|
| 117 | ep->qh->horizontal = last_qh->horizontal;
|
|---|
| 118 | /* Make sure QH update is written to the memory */
|
|---|
| 119 | write_barrier();
|
|---|
| 120 |
|
|---|
| 121 | /* Add QH to the hw queue */
|
|---|
| 122 | qh_append_qh(last_qh, ep->qh);
|
|---|
| 123 | /* Make sure QH is updated */
|
|---|
| 124 | write_barrier();
|
|---|
| 125 | /* Add to the sw list */
|
|---|
| 126 | list_append(&ep->link, &instance->endpoint_list);
|
|---|
| 127 |
|
|---|
| 128 | ehci_endpoint_t *first = ehci_endpoint_list_instance(
|
|---|
| 129 | list_first(&instance->endpoint_list));
|
|---|
| 130 | usb_log_debug("EPL(%p-%s): EP(%p) added to list, first is %p(%p).\n",
|
|---|
| 131 | instance, instance->name, ep, first, first->qh);
|
|---|
| 132 | if (last_qh == instance->list_head) {
|
|---|
| 133 | usb_log_debug2("EPL(%p-%s): head EP(%p-%"PRIxn"): %x:%x.\n",
|
|---|
| 134 | instance, instance->name, last_qh,
|
|---|
| 135 | addr_to_phys(instance->list_head),
|
|---|
| 136 | last_qh->status, last_qh->horizontal);
|
|---|
| 137 | }
|
|---|
| 138 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /** Remove endpoint from the list and queue.
|
|---|
| 142 | *
|
|---|
| 143 | * @param[in] instance List to use.
|
|---|
| 144 | * @param[in] endpoint Endpoint to remove.
|
|---|
| 145 | */
|
|---|
| 146 | void endpoint_list_remove_ep(endpoint_list_t *instance, ehci_endpoint_t *ep)
|
|---|
| 147 | {
|
|---|
| 148 | assert(instance);
|
|---|
| 149 | assert(instance->list_head);
|
|---|
| 150 | assert(ep);
|
|---|
| 151 | assert(ep->qh);
|
|---|
| 152 |
|
|---|
| 153 | fibril_mutex_lock(&instance->guard);
|
|---|
| 154 |
|
|---|
| 155 | usb_log_debug2("EPL(%p-%s): removing EP(%p).\n",
|
|---|
| 156 | instance, instance->name, ep);
|
|---|
| 157 |
|
|---|
| 158 | const char *qpos = NULL;
|
|---|
| 159 | qh_t *prev_qh;
|
|---|
| 160 | /* Remove from the hardware queue */
|
|---|
| 161 | if (list_first(&instance->endpoint_list) == &ep->link) {
|
|---|
| 162 | /* I'm the first one here */
|
|---|
| 163 | prev_qh = instance->list_head;
|
|---|
| 164 | qpos = "FIRST";
|
|---|
| 165 | } else {
|
|---|
| 166 | prev_qh = ehci_endpoint_list_instance(ep->link.prev)->qh;
|
|---|
| 167 | qpos = "NOT FIRST";
|
|---|
| 168 | }
|
|---|
| 169 | assert(qh_next(prev_qh) == addr_to_phys(ep->qh));
|
|---|
| 170 | prev_qh->horizontal = ep->qh->horizontal;
|
|---|
| 171 | /* Make sure ED is updated */
|
|---|
| 172 | write_barrier();
|
|---|
| 173 |
|
|---|
| 174 | usb_log_debug("EPL(%p-%s): EP(%p) removed (%s), horizontal %x.\n",
|
|---|
| 175 | instance, instance->name, ep, qpos, ep->qh->horizontal);
|
|---|
| 176 |
|
|---|
| 177 | /* Remove from the endpoint list */
|
|---|
| 178 | list_remove(&ep->link);
|
|---|
| 179 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 180 | }
|
|---|
| 181 | /**
|
|---|
| 182 | * @}
|
|---|
| 183 | */
|
|---|
| 184 |
|
|---|