| [2759c52] | 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 | */
|
|---|
| [58563585] | 28 |
|
|---|
| [2759c52] | 29 | /** @addtogroup drvusbohci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief OHCI driver
|
|---|
| 34 | */
|
|---|
| [0d4b110] | 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| [2dbfe44] | 38 | #include <usb/host/utils/malloc32.h>
|
|---|
| [e6b9182] | 39 | #include <usb/host/bandwidth.h>
|
|---|
| [0d4b110] | 40 |
|
|---|
| [e6b9182] | 41 | #include "ohci_bus.h"
|
|---|
| [5fd9c30] | 42 | #include "ohci_batch.h"
|
|---|
| [620c710] | 43 | #include "hc.h"
|
|---|
| [2759c52] | 44 |
|
|---|
| [c6f82e5] | 45 | /**
|
|---|
| 46 | * Callback to reset toggle on ED.
|
|---|
| [02cacce] | 47 | *
|
|---|
| 48 | * @param[in] hcd_ep hcd endpoint structure
|
|---|
| 49 | * @param[in] toggle new value of toggle bit
|
|---|
| 50 | */
|
|---|
| [c6f82e5] | 51 | void ohci_ep_toggle_reset(endpoint_t *ep)
|
|---|
| [545764b] | 52 | {
|
|---|
| [e6b9182] | 53 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
|---|
| [545764b] | 54 | assert(instance);
|
|---|
| 55 | assert(instance->ed);
|
|---|
| [56257ba] | 56 | ed_toggle_set(instance->ed, 0);
|
|---|
| [545764b] | 57 | }
|
|---|
| [76fbd9a] | 58 |
|
|---|
| [d369b3b] | 59 | static int ohci_device_enumerate(device_t *dev)
|
|---|
| 60 | {
|
|---|
| 61 | ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
|
|---|
| 62 | return usb2_bus_device_enumerate(&bus->helper, dev);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| [f3ae58b] | 65 | static void ohci_device_gone(device_t *dev)
|
|---|
| 66 | {
|
|---|
| 67 | ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
|
|---|
| 68 | usb2_bus_device_gone(&bus->helper, dev);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| [02cacce] | 71 | /** Creates new hcd endpoint representation.
|
|---|
| 72 | */
|
|---|
| [9efad54] | 73 | static endpoint_t *ohci_endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
|
|---|
| [2759c52] | 74 | {
|
|---|
| [6832245] | 75 | assert(dev);
|
|---|
| [e6b9182] | 76 |
|
|---|
| [e67c50a] | 77 | ohci_endpoint_t *ohci_ep = calloc(1, sizeof(ohci_endpoint_t));
|
|---|
| [e20eaed] | 78 | if (ohci_ep == NULL)
|
|---|
| [e6b9182] | 79 | return NULL;
|
|---|
| [2759c52] | 80 |
|
|---|
| [6832245] | 81 | endpoint_init(&ohci_ep->base, dev, desc);
|
|---|
| [741bcdeb] | 82 |
|
|---|
| [e67c50a] | 83 | const errno_t err = dma_buffer_alloc(&ohci_ep->dma_buffer, sizeof(ed_t) + 2 * sizeof(td_t));
|
|---|
| 84 | if (err) {
|
|---|
| [e20eaed] | 85 | free(ohci_ep);
|
|---|
| [e6b9182] | 86 | return NULL;
|
|---|
| [2759c52] | 87 | }
|
|---|
| 88 |
|
|---|
| [e67c50a] | 89 | ohci_ep->ed = ohci_ep->dma_buffer.virt;
|
|---|
| 90 |
|
|---|
| 91 | ohci_ep->tds[0] = (td_t *) ohci_ep->ed + 1;
|
|---|
| 92 | ohci_ep->tds[1] = ohci_ep->tds[0] + 1;
|
|---|
| [2759c52] | 93 |
|
|---|
| [d60115a] | 94 | link_initialize(&ohci_ep->eplist_link);
|
|---|
| 95 | link_initialize(&ohci_ep->pending_link);
|
|---|
| [5995383c] | 96 | return &ohci_ep->base;
|
|---|
| [2759c52] | 97 | }
|
|---|
| [76fbd9a] | 98 |
|
|---|
| [48ae3ef] | 99 | /** Disposes hcd endpoint structure
|
|---|
| 100 | *
|
|---|
| [57e06ef] | 101 | * @param[in] hcd driver using this instance.
|
|---|
| 102 | * @param[in] ep endpoint structure.
|
|---|
| [48ae3ef] | 103 | */
|
|---|
| [e6b9182] | 104 | static void ohci_endpoint_destroy(endpoint_t *ep)
|
|---|
| [48ae3ef] | 105 | {
|
|---|
| 106 | assert(ep);
|
|---|
| 107 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
|---|
| [e6b9182] | 108 |
|
|---|
| [e67c50a] | 109 | dma_buffer_free(&instance->dma_buffer);
|
|---|
| [e6b9182] | 110 | free(instance);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| [6832245] | 114 | static int ohci_register_ep(endpoint_t *ep)
|
|---|
| [e6b9182] | 115 | {
|
|---|
| [6832245] | 116 | bus_t *bus_base = endpoint_get_bus(ep);
|
|---|
| [e6b9182] | 117 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
|---|
| 118 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
|---|
| 119 |
|
|---|
| [d369b3b] | 120 | const int err = usb2_bus_endpoint_register(&bus->helper, ep);
|
|---|
| [e6b9182] | 121 | if (err)
|
|---|
| 122 | return err;
|
|---|
| 123 |
|
|---|
| [e67c50a] | 124 | ed_init(ohci_ep->ed, ep, ohci_ep->tds[0]);
|
|---|
| [e6b9182] | 125 | hc_enqueue_endpoint(bus->hc, ep);
|
|---|
| [4db49344] | 126 | endpoint_set_online(ep, &bus->hc->guard);
|
|---|
| [e6b9182] | 127 |
|
|---|
| 128 | return EOK;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| [bad4a05] | 131 | static void ohci_unregister_ep(endpoint_t *ep)
|
|---|
| [e6b9182] | 132 | {
|
|---|
| [d60115a] | 133 | ohci_bus_t * const bus = (ohci_bus_t *) endpoint_get_bus(ep);
|
|---|
| 134 | hc_t * const hc = bus->hc;
|
|---|
| [e6b9182] | 135 | assert(ep);
|
|---|
| 136 |
|
|---|
| [d369b3b] | 137 | usb2_bus_endpoint_unregister(&bus->helper, ep);
|
|---|
| [e6b9182] | 138 | hc_dequeue_endpoint(bus->hc, ep);
|
|---|
| [d60115a] | 139 |
|
|---|
| 140 | /*
|
|---|
| [4db49344] | 141 | * Now we can be sure the active transfer will not be completed,
|
|---|
| 142 | * as it's out of the schedule, and HC acknowledged it.
|
|---|
| [d60115a] | 143 | */
|
|---|
| [4db49344] | 144 |
|
|---|
| 145 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
|---|
| 146 |
|
|---|
| [d60115a] | 147 | fibril_mutex_lock(&hc->guard);
|
|---|
| [4db49344] | 148 | endpoint_set_offline_locked(ep);
|
|---|
| [d60115a] | 149 | list_remove(&ohci_ep->pending_link);
|
|---|
| 150 | usb_transfer_batch_t * const batch = ep->active_batch;
|
|---|
| 151 | endpoint_deactivate_locked(ep);
|
|---|
| [4db49344] | 152 | fibril_mutex_unlock(&hc->guard);
|
|---|
| [d60115a] | 153 |
|
|---|
| 154 | if (batch) {
|
|---|
| 155 | batch->error = EINTR;
|
|---|
| [db51a6a6] | 156 | batch->transferred_size = 0;
|
|---|
| [d60115a] | 157 | usb_transfer_batch_finish(batch);
|
|---|
| 158 | }
|
|---|
| [5fd9c30] | 159 | }
|
|---|
| 160 |
|
|---|
| [6832245] | 161 | static usb_transfer_batch_t *ohci_create_batch(endpoint_t *ep)
|
|---|
| [5fd9c30] | 162 | {
|
|---|
| 163 | ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
|
|---|
| 164 | return &batch->base;
|
|---|
| 165 | }
|
|---|
| [e6b9182] | 166 |
|
|---|
| [6832245] | 167 | static void ohci_destroy_batch(usb_transfer_batch_t *batch)
|
|---|
| [5fd9c30] | 168 | {
|
|---|
| 169 | ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
|
|---|
| [e6b9182] | 170 | }
|
|---|
| 171 |
|
|---|
| [6832245] | 172 | static const bus_ops_t ohci_bus_ops = {
|
|---|
| [32fb6bce] | 173 | .interrupt = ohci_hc_interrupt,
|
|---|
| 174 | .status = ohci_hc_status,
|
|---|
| 175 |
|
|---|
| [d369b3b] | 176 | .device_enumerate = ohci_device_enumerate,
|
|---|
| [f3ae58b] | 177 | .device_gone = ohci_device_gone,
|
|---|
| [d369b3b] | 178 |
|
|---|
| [6832245] | 179 | .endpoint_destroy = ohci_endpoint_destroy,
|
|---|
| 180 | .endpoint_create = ohci_endpoint_create,
|
|---|
| 181 | .endpoint_register = ohci_register_ep,
|
|---|
| 182 | .endpoint_unregister = ohci_unregister_ep,
|
|---|
| [c6f82e5] | 183 |
|
|---|
| [6832245] | 184 | .batch_create = ohci_create_batch,
|
|---|
| 185 | .batch_destroy = ohci_destroy_batch,
|
|---|
| [32fb6bce] | 186 | .batch_schedule = ohci_hc_schedule,
|
|---|
| [6832245] | 187 | };
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| [32fb6bce] | 190 | int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
|
|---|
| [e6b9182] | 191 | {
|
|---|
| 192 | assert(hc);
|
|---|
| 193 | assert(bus);
|
|---|
| 194 |
|
|---|
| [6832245] | 195 | bus_t *bus_base = (bus_t *) bus;
|
|---|
| [d369b3b] | 196 | bus_init(bus_base, sizeof(device_t));
|
|---|
| [6832245] | 197 | bus_base->ops = &ohci_bus_ops;
|
|---|
| [5fd9c30] | 198 |
|
|---|
| [d369b3b] | 199 | usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb11);
|
|---|
| 200 |
|
|---|
| [5995383c] | 201 | bus->hc = hc;
|
|---|
| 202 |
|
|---|
| [e6b9182] | 203 | return EOK;
|
|---|
| [48ae3ef] | 204 | }
|
|---|
| [58563585] | 205 |
|
|---|
| [2759c52] | 206 | /**
|
|---|
| 207 | * @}
|
|---|
| 208 | */
|
|---|