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