[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 |
|
---|
[02cacce] | 45 | /** Callback to set toggle on ED.
|
---|
| 46 | *
|
---|
| 47 | * @param[in] hcd_ep hcd endpoint structure
|
---|
| 48 | * @param[in] toggle new value of toggle bit
|
---|
| 49 | */
|
---|
[e6b9182] | 50 | static void ohci_ep_toggle_set(endpoint_t *ep, bool toggle)
|
---|
[545764b] | 51 | {
|
---|
[e6b9182] | 52 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
[545764b] | 53 | assert(instance);
|
---|
| 54 | assert(instance->ed);
|
---|
[e6b9182] | 55 | ep->toggle = toggle;
|
---|
[545764b] | 56 | ed_toggle_set(instance->ed, toggle);
|
---|
| 57 | }
|
---|
[76fbd9a] | 58 |
|
---|
[02cacce] | 59 | /** Callback to get value of toggle bit.
|
---|
| 60 | *
|
---|
| 61 | * @param[in] hcd_ep hcd endpoint structure
|
---|
| 62 | * @return Current value of toggle bit.
|
---|
| 63 | */
|
---|
[e6b9182] | 64 | static bool ohci_ep_toggle_get(endpoint_t *ep)
|
---|
[545764b] | 65 | {
|
---|
[e6b9182] | 66 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
[545764b] | 67 | assert(instance);
|
---|
| 68 | assert(instance->ed);
|
---|
| 69 | return ed_toggle_get(instance->ed);
|
---|
| 70 | }
|
---|
[76fbd9a] | 71 |
|
---|
[02cacce] | 72 | /** Creates new hcd endpoint representation.
|
---|
| 73 | */
|
---|
[6832245] | 74 | static endpoint_t *ohci_endpoint_create(device_t *dev, const usb_endpoint_desc_t *desc)
|
---|
[2759c52] | 75 | {
|
---|
[6832245] | 76 | assert(dev);
|
---|
[e6b9182] | 77 |
|
---|
[e20eaed] | 78 | ohci_endpoint_t *ohci_ep = malloc(sizeof(ohci_endpoint_t));
|
---|
| 79 | if (ohci_ep == NULL)
|
---|
[e6b9182] | 80 | return NULL;
|
---|
[2759c52] | 81 |
|
---|
[6832245] | 82 | endpoint_init(&ohci_ep->base, dev, desc);
|
---|
[741bcdeb] | 83 |
|
---|
[e20eaed] | 84 | ohci_ep->ed = malloc32(sizeof(ed_t));
|
---|
| 85 | if (ohci_ep->ed == NULL) {
|
---|
| 86 | free(ohci_ep);
|
---|
[e6b9182] | 87 | return NULL;
|
---|
[2759c52] | 88 | }
|
---|
| 89 |
|
---|
[e20eaed] | 90 | ohci_ep->td = malloc32(sizeof(td_t));
|
---|
| 91 | if (ohci_ep->td == NULL) {
|
---|
| 92 | free32(ohci_ep->ed);
|
---|
| 93 | free(ohci_ep);
|
---|
[e6b9182] | 94 | return NULL;
|
---|
[2759c52] | 95 | }
|
---|
| 96 |
|
---|
[a2e8c44] | 97 | link_initialize(&ohci_ep->link);
|
---|
[5995383c] | 98 | return &ohci_ep->base;
|
---|
[2759c52] | 99 | }
|
---|
[76fbd9a] | 100 |
|
---|
[48ae3ef] | 101 | /** Disposes hcd endpoint structure
|
---|
| 102 | *
|
---|
[57e06ef] | 103 | * @param[in] hcd driver using this instance.
|
---|
| 104 | * @param[in] ep endpoint structure.
|
---|
[48ae3ef] | 105 | */
|
---|
[e6b9182] | 106 | static void ohci_endpoint_destroy(endpoint_t *ep)
|
---|
[48ae3ef] | 107 | {
|
---|
| 108 | assert(ep);
|
---|
| 109 | ohci_endpoint_t *instance = ohci_endpoint_get(ep);
|
---|
[e6b9182] | 110 |
|
---|
| 111 | free32(instance->ed);
|
---|
| 112 | free32(instance->td);
|
---|
| 113 | free(instance);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[6832245] | 117 | static int ohci_register_ep(endpoint_t *ep)
|
---|
[e6b9182] | 118 | {
|
---|
[6832245] | 119 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
[e6b9182] | 120 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
| 121 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
| 122 |
|
---|
[6832245] | 123 | const int err = usb2_bus_ops.endpoint_register(ep);
|
---|
[e6b9182] | 124 | if (err)
|
---|
| 125 | return err;
|
---|
| 126 |
|
---|
| 127 | ed_init(ohci_ep->ed, ep, ohci_ep->td);
|
---|
| 128 | hc_enqueue_endpoint(bus->hc, ep);
|
---|
| 129 |
|
---|
| 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[6832245] | 133 | static int ohci_unregister_ep(endpoint_t *ep)
|
---|
[e6b9182] | 134 | {
|
---|
[6832245] | 135 | ohci_bus_t *bus = (ohci_bus_t *) endpoint_get_bus(ep);
|
---|
[e6b9182] | 136 | assert(ep);
|
---|
| 137 |
|
---|
[6832245] | 138 | const int err = usb2_bus_ops.endpoint_unregister(ep);
|
---|
[e6b9182] | 139 | if (err)
|
---|
| 140 | return err;
|
---|
| 141 |
|
---|
| 142 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
| 143 | return EOK;
|
---|
[5fd9c30] | 144 | }
|
---|
| 145 |
|
---|
[6832245] | 146 | static usb_transfer_batch_t *ohci_create_batch(endpoint_t *ep)
|
---|
[5fd9c30] | 147 | {
|
---|
| 148 | ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
|
---|
| 149 | return &batch->base;
|
---|
| 150 | }
|
---|
[e6b9182] | 151 |
|
---|
[6832245] | 152 | static void ohci_destroy_batch(usb_transfer_batch_t *batch)
|
---|
[5fd9c30] | 153 | {
|
---|
| 154 | ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
|
---|
[e6b9182] | 155 | }
|
---|
| 156 |
|
---|
[6832245] | 157 | static const bus_ops_t ohci_bus_ops = {
|
---|
| 158 | .parent = &usb2_bus_ops,
|
---|
| 159 |
|
---|
[32fb6bce] | 160 | .interrupt = ohci_hc_interrupt,
|
---|
| 161 | .status = ohci_hc_status,
|
---|
| 162 |
|
---|
[6832245] | 163 | .endpoint_destroy = ohci_endpoint_destroy,
|
---|
| 164 | .endpoint_create = ohci_endpoint_create,
|
---|
| 165 | .endpoint_register = ohci_register_ep,
|
---|
| 166 | .endpoint_unregister = ohci_unregister_ep,
|
---|
| 167 | .endpoint_count_bw = bandwidth_count_usb11,
|
---|
| 168 | .endpoint_set_toggle = ohci_ep_toggle_set,
|
---|
| 169 | .endpoint_get_toggle = ohci_ep_toggle_get,
|
---|
| 170 | .batch_create = ohci_create_batch,
|
---|
| 171 | .batch_destroy = ohci_destroy_batch,
|
---|
[32fb6bce] | 172 | .batch_schedule = ohci_hc_schedule,
|
---|
[6832245] | 173 | };
|
---|
| 174 |
|
---|
| 175 |
|
---|
[32fb6bce] | 176 | int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
|
---|
[e6b9182] | 177 | {
|
---|
| 178 | assert(hc);
|
---|
| 179 | assert(bus);
|
---|
| 180 |
|
---|
[6832245] | 181 | usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
|
---|
| 182 | bus_t *bus_base = (bus_t *) bus;
|
---|
[e6b9182] | 183 |
|
---|
[32fb6bce] | 184 | usb2_bus_init(usb2_bus, BANDWIDTH_AVAILABLE_USB11);
|
---|
[6832245] | 185 | bus_base->ops = &ohci_bus_ops;
|
---|
[5fd9c30] | 186 |
|
---|
[5995383c] | 187 | bus->hc = hc;
|
---|
| 188 |
|
---|
[e6b9182] | 189 | return EOK;
|
---|
[48ae3ef] | 190 | }
|
---|
[58563585] | 191 |
|
---|
[2759c52] | 192 | /**
|
---|
| 193 | * @}
|
---|
| 194 | */
|
---|