[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 | */
|
---|
[e6b9182] | 74 | static endpoint_t *ohci_endpoint_create(bus_t *bus)
|
---|
[2759c52] | 75 | {
|
---|
[e6b9182] | 76 | assert(bus);
|
---|
| 77 |
|
---|
[e20eaed] | 78 | ohci_endpoint_t *ohci_ep = malloc(sizeof(ohci_endpoint_t));
|
---|
| 79 | if (ohci_ep == NULL)
|
---|
[e6b9182] | 80 | return NULL;
|
---|
[2759c52] | 81 |
|
---|
[741bcdeb] | 82 | endpoint_init(&ohci_ep->base, bus);
|
---|
| 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 |
|
---|
[8b8c164] | 117 | static int ohci_register_ep(bus_t *bus_base, device_t *dev, endpoint_t *ep, const usb_endpoint_desc_t *desc)
|
---|
[e6b9182] | 118 | {
|
---|
| 119 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
| 120 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
| 121 |
|
---|
[8b8c164] | 122 | const int err = bus->parent_ops.register_endpoint(bus_base, dev, ep, desc);
|
---|
[e6b9182] | 123 | if (err)
|
---|
| 124 | return err;
|
---|
| 125 |
|
---|
| 126 | ed_init(ohci_ep->ed, ep, ohci_ep->td);
|
---|
| 127 | hc_enqueue_endpoint(bus->hc, ep);
|
---|
| 128 |
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[ee794529] | 132 | static int ohci_unregister_ep(bus_t *bus_base, endpoint_t *ep)
|
---|
[e6b9182] | 133 | {
|
---|
| 134 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
| 135 | assert(bus);
|
---|
| 136 | assert(ep);
|
---|
| 137 |
|
---|
[ee794529] | 138 | const int err = bus->parent_ops.unregister_endpoint(bus_base, ep);
|
---|
[e6b9182] | 139 | if (err)
|
---|
| 140 | return err;
|
---|
| 141 |
|
---|
| 142 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
| 143 | return EOK;
|
---|
[5fd9c30] | 144 | }
|
---|
| 145 |
|
---|
| 146 | static usb_transfer_batch_t *ohci_bus_create_batch(bus_t *bus, endpoint_t *ep)
|
---|
| 147 | {
|
---|
| 148 | ohci_transfer_batch_t *batch = ohci_transfer_batch_create(ep);
|
---|
| 149 | return &batch->base;
|
---|
| 150 | }
|
---|
[e6b9182] | 151 |
|
---|
[5fd9c30] | 152 | static void ohci_bus_destroy_batch(usb_transfer_batch_t *batch)
|
---|
| 153 | {
|
---|
| 154 | ohci_transfer_batch_destroy(ohci_transfer_batch_get(batch));
|
---|
[e6b9182] | 155 | }
|
---|
| 156 |
|
---|
| 157 | int ohci_bus_init(ohci_bus_t *bus, hc_t *hc)
|
---|
| 158 | {
|
---|
| 159 | assert(hc);
|
---|
| 160 | assert(bus);
|
---|
| 161 |
|
---|
| 162 | usb2_bus_init(&bus->base, BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
---|
| 163 |
|
---|
| 164 | bus_ops_t *ops = &bus->base.base.ops;
|
---|
| 165 | bus->parent_ops = *ops;
|
---|
| 166 | ops->create_endpoint = ohci_endpoint_create;
|
---|
| 167 | ops->destroy_endpoint = ohci_endpoint_destroy;
|
---|
| 168 | ops->endpoint_set_toggle = ohci_ep_toggle_set;
|
---|
| 169 | ops->endpoint_get_toggle = ohci_ep_toggle_get;
|
---|
| 170 |
|
---|
| 171 | ops->register_endpoint = ohci_register_ep;
|
---|
[ee794529] | 172 | ops->unregister_endpoint = ohci_unregister_ep;
|
---|
[e6b9182] | 173 |
|
---|
[5fd9c30] | 174 | ops->create_batch = ohci_bus_create_batch;
|
---|
| 175 | ops->destroy_batch = ohci_bus_destroy_batch;
|
---|
| 176 |
|
---|
[5995383c] | 177 | bus->hc = hc;
|
---|
| 178 |
|
---|
[e6b9182] | 179 | return EOK;
|
---|
[48ae3ef] | 180 | }
|
---|
[58563585] | 181 |
|
---|
[2759c52] | 182 | /**
|
---|
| 183 | * @}
|
---|
| 184 | */
|
---|