| 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 drvusbehci
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief EHCI driver
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <usb/host/bandwidth.h>
|
|---|
| 40 | #include <usb/debug.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "ehci_bus.h"
|
|---|
| 43 | #include "ehci_batch.h"
|
|---|
| 44 | #include "hc.h"
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Callback to set toggle on ED.
|
|---|
| 48 | *
|
|---|
| 49 | * @param[in] hcd_ep hcd endpoint structure
|
|---|
| 50 | * @param[in] toggle new value of toggle bit
|
|---|
| 51 | */
|
|---|
| 52 | void ehci_ep_toggle_reset(endpoint_t *ep)
|
|---|
| 53 | {
|
|---|
| 54 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
|---|
| 55 | if (qh_toggle_from_td(instance->qh))
|
|---|
| 56 | usb_log_warning("EP(%p): Resetting toggle bit for transfer "
|
|---|
| 57 | "directed EP", instance);
|
|---|
| 58 | qh_toggle_set(instance->qh, 0);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | static int ehci_device_enumerate(device_t *dev)
|
|---|
| 62 | {
|
|---|
| 63 | ehci_bus_t *bus = (ehci_bus_t *) dev->bus;
|
|---|
| 64 | return usb2_bus_device_enumerate(&bus->helper, dev);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | static void ehci_device_gone(device_t *dev)
|
|---|
| 68 | {
|
|---|
| 69 | ehci_bus_t *bus = (ehci_bus_t *) dev->bus;
|
|---|
| 70 | usb2_bus_device_gone(&bus->helper, dev);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Creates new hcd endpoint representation.
|
|---|
| 74 | */
|
|---|
| 75 | static endpoint_t *ehci_endpoint_create(device_t *dev,
|
|---|
| 76 | const usb_endpoint_descriptors_t *desc)
|
|---|
| 77 | {
|
|---|
| 78 | assert(dev);
|
|---|
| 79 |
|
|---|
| 80 | ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
|
|---|
| 81 | if (ehci_ep == NULL)
|
|---|
| 82 | return NULL;
|
|---|
| 83 |
|
|---|
| 84 | endpoint_init(&ehci_ep->base, dev, desc);
|
|---|
| 85 |
|
|---|
| 86 | if (dma_buffer_alloc(&ehci_ep->dma_buffer, sizeof(qh_t)))
|
|---|
| 87 | return NULL;
|
|---|
| 88 |
|
|---|
| 89 | ehci_ep->qh = ehci_ep->dma_buffer.virt;
|
|---|
| 90 |
|
|---|
| 91 | link_initialize(&ehci_ep->eplist_link);
|
|---|
| 92 | link_initialize(&ehci_ep->pending_link);
|
|---|
| 93 | return &ehci_ep->base;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Disposes hcd endpoint structure
|
|---|
| 97 | *
|
|---|
| 98 | * @param[in] hcd driver using this instance.
|
|---|
| 99 | * @param[in] ep endpoint structure.
|
|---|
| 100 | */
|
|---|
| 101 | static void ehci_endpoint_destroy(endpoint_t *ep)
|
|---|
| 102 | {
|
|---|
| 103 | assert(ep);
|
|---|
| 104 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
|---|
| 105 |
|
|---|
| 106 | dma_buffer_free(&instance->dma_buffer);
|
|---|
| 107 | free(instance);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | static int ehci_register_ep(endpoint_t *ep)
|
|---|
| 112 | {
|
|---|
| 113 | bus_t *bus_base = endpoint_get_bus(ep);
|
|---|
| 114 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 115 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| 116 |
|
|---|
| 117 | const int err = usb2_bus_endpoint_register(&bus->helper, ep);
|
|---|
| 118 | if (err)
|
|---|
| 119 | return err;
|
|---|
| 120 |
|
|---|
| 121 | qh_init(ehci_ep->qh, ep);
|
|---|
| 122 | hc_enqueue_endpoint(bus->hc, ep);
|
|---|
| 123 | endpoint_set_online(ep, &bus->hc->guard);
|
|---|
| 124 | return EOK;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static void ehci_unregister_ep(endpoint_t *ep)
|
|---|
| 128 | {
|
|---|
| 129 | bus_t *bus_base = endpoint_get_bus(ep);
|
|---|
| 130 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 131 | hc_t *hc = bus->hc;
|
|---|
| 132 | assert(bus);
|
|---|
| 133 | assert(ep);
|
|---|
| 134 |
|
|---|
| 135 | usb2_bus_endpoint_unregister(&bus->helper, ep);
|
|---|
| 136 | hc_dequeue_endpoint(hc, ep);
|
|---|
| 137 | /*
|
|---|
| 138 | * Now we can be sure the active transfer will not be completed,
|
|---|
| 139 | * as it's out of the schedule, and HC acknowledged it.
|
|---|
| 140 | */
|
|---|
| 141 |
|
|---|
| 142 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| 143 |
|
|---|
| 144 | fibril_mutex_lock(&hc->guard);
|
|---|
| 145 | endpoint_set_offline_locked(ep);
|
|---|
| 146 | list_remove(&ehci_ep->pending_link);
|
|---|
| 147 | usb_transfer_batch_t * const batch = ep->active_batch;
|
|---|
| 148 | endpoint_deactivate_locked(ep);
|
|---|
| 149 | fibril_mutex_unlock(&hc->guard);
|
|---|
| 150 |
|
|---|
| 151 | if (batch) {
|
|---|
| 152 | batch->error = EINTR;
|
|---|
| 153 | batch->transferred_size = 0;
|
|---|
| 154 | usb_transfer_batch_finish(batch);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | static usb_transfer_batch_t *ehci_create_batch(endpoint_t *ep)
|
|---|
| 159 | {
|
|---|
| 160 | ehci_transfer_batch_t *batch = ehci_transfer_batch_create(ep);
|
|---|
| 161 | return &batch->base;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static void ehci_destroy_batch(usb_transfer_batch_t *batch)
|
|---|
| 165 | {
|
|---|
| 166 | ehci_transfer_batch_destroy(ehci_transfer_batch_get(batch));
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | static const bus_ops_t ehci_bus_ops = {
|
|---|
| 170 | .interrupt = ehci_hc_interrupt,
|
|---|
| 171 | .status = ehci_hc_status,
|
|---|
| 172 |
|
|---|
| 173 | .device_enumerate = ehci_device_enumerate,
|
|---|
| 174 | .device_gone = ehci_device_gone,
|
|---|
| 175 |
|
|---|
| 176 | .endpoint_destroy = ehci_endpoint_destroy,
|
|---|
| 177 | .endpoint_create = ehci_endpoint_create,
|
|---|
| 178 | .endpoint_register = ehci_register_ep,
|
|---|
| 179 | .endpoint_unregister = ehci_unregister_ep,
|
|---|
| 180 |
|
|---|
| 181 | .batch_create = ehci_create_batch,
|
|---|
| 182 | .batch_destroy = ehci_destroy_batch,
|
|---|
| 183 | .batch_schedule = ehci_hc_schedule,
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
|
|---|
| 187 | {
|
|---|
| 188 | assert(hc);
|
|---|
| 189 | assert(bus);
|
|---|
| 190 |
|
|---|
| 191 | bus_t *bus_base = (bus_t *) bus;
|
|---|
| 192 | bus_init(bus_base, sizeof(device_t));
|
|---|
| 193 | bus_base->ops = &ehci_bus_ops;
|
|---|
| 194 |
|
|---|
| 195 | usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb2);
|
|---|
| 196 |
|
|---|
| 197 | bus->hc = hc;
|
|---|
| 198 |
|
|---|
| 199 | return EOK;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * @}
|
|---|
| 204 | */
|
|---|