| [f9351b1] | 1 | /*
|
|---|
| [741bcdeb] | 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| [f9351b1] | 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 | */
|
|---|
| [741bcdeb] | 28 |
|
|---|
| [f9351b1] | 29 | /** @addtogroup drvusbehci
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief EHCI driver
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| [45cbf897] | 38 | #include <usb/host/utils/malloc32.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 |
|
|---|
| 46 | /** Callback to set toggle on ED.
|
|---|
| 47 | *
|
|---|
| 48 | * @param[in] hcd_ep hcd endpoint structure
|
|---|
| 49 | * @param[in] toggle new value of toggle bit
|
|---|
| 50 | */
|
|---|
| [741bcdeb] | 51 | static void ehci_ep_toggle_set(endpoint_t *ep, bool toggle)
|
|---|
| [f9351b1] | 52 | {
|
|---|
| [741bcdeb] | 53 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
|---|
| [f9351b1] | 54 | assert(instance);
|
|---|
| 55 | assert(instance->qh);
|
|---|
| [741bcdeb] | 56 | ep->toggle = toggle;
|
|---|
| [42de21a] | 57 | if (qh_toggle_from_td(instance->qh))
|
|---|
| [6de4b4a1] | 58 | usb_log_warning("EP(%p): Setting toggle bit for transfer "
|
|---|
| 59 | "directed EP", instance);
|
|---|
| [42de21a] | 60 | qh_toggle_set(instance->qh, toggle);
|
|---|
| [f9351b1] | 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Callback to get value of toggle bit.
|
|---|
| 64 | *
|
|---|
| 65 | * @param[in] hcd_ep hcd endpoint structure
|
|---|
| 66 | * @return Current value of toggle bit.
|
|---|
| 67 | */
|
|---|
| [741bcdeb] | 68 | static bool ehci_ep_toggle_get(endpoint_t *ep)
|
|---|
| [f9351b1] | 69 | {
|
|---|
| [741bcdeb] | 70 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
|---|
| [f9351b1] | 71 | assert(instance);
|
|---|
| 72 | assert(instance->qh);
|
|---|
| [741bcdeb] | 73 |
|
|---|
| [42de21a] | 74 | if (qh_toggle_from_td(instance->qh))
|
|---|
| [6de4b4a1] | 75 | usb_log_warning("EP(%p): Reading useless toggle bit", instance);
|
|---|
| [42de21a] | 76 | return qh_toggle_get(instance->qh);
|
|---|
| [f9351b1] | 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Creates new hcd endpoint representation.
|
|---|
| 80 | */
|
|---|
| [6832245] | 81 | static endpoint_t *ehci_endpoint_create(device_t *dev, const usb_endpoint_desc_t *desc)
|
|---|
| [f9351b1] | 82 | {
|
|---|
| [6832245] | 83 | assert(dev);
|
|---|
| [b5f813c] | 84 |
|
|---|
| [f9351b1] | 85 | ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
|
|---|
| 86 | if (ehci_ep == NULL)
|
|---|
| [741bcdeb] | 87 | return NULL;
|
|---|
| 88 |
|
|---|
| [6832245] | 89 | endpoint_init(&ehci_ep->base, dev, desc);
|
|---|
| 90 |
|
|---|
| 91 | // TODO: extract USB2 information from desc
|
|---|
| [f9351b1] | 92 |
|
|---|
| 93 | ehci_ep->qh = malloc32(sizeof(qh_t));
|
|---|
| 94 | if (ehci_ep->qh == NULL) {
|
|---|
| 95 | free(ehci_ep);
|
|---|
| [741bcdeb] | 96 | return NULL;
|
|---|
| [f9351b1] | 97 | }
|
|---|
| 98 |
|
|---|
| [3f2cb17] | 99 | link_initialize(&ehci_ep->link);
|
|---|
| [5995383c] | 100 | return &ehci_ep->base;
|
|---|
| [f9351b1] | 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Disposes hcd endpoint structure
|
|---|
| 104 | *
|
|---|
| 105 | * @param[in] hcd driver using this instance.
|
|---|
| 106 | * @param[in] ep endpoint structure.
|
|---|
| 107 | */
|
|---|
| [741bcdeb] | 108 | static void ehci_endpoint_destroy(endpoint_t *ep)
|
|---|
| [f9351b1] | 109 | {
|
|---|
| 110 | assert(ep);
|
|---|
| 111 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
|---|
| [741bcdeb] | 112 |
|
|---|
| 113 | free32(instance->qh);
|
|---|
| 114 | free(instance);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| [6832245] | 118 | static int ehci_register_ep(endpoint_t *ep)
|
|---|
| [741bcdeb] | 119 | {
|
|---|
| [6832245] | 120 | bus_t *bus_base = endpoint_get_bus(ep);
|
|---|
| [741bcdeb] | 121 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 122 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| [6832245] | 123 | assert(fibril_mutex_is_locked(&bus_base->guard));
|
|---|
| [741bcdeb] | 124 |
|
|---|
| [6832245] | 125 | const int err = usb2_bus_ops.endpoint_register(ep);
|
|---|
| [741bcdeb] | 126 | if (err)
|
|---|
| 127 | return err;
|
|---|
| 128 |
|
|---|
| 129 | qh_init(ehci_ep->qh, ep);
|
|---|
| 130 | hc_enqueue_endpoint(bus->hc, ep);
|
|---|
| 131 |
|
|---|
| 132 | return EOK;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| [6832245] | 135 | static int ehci_unregister_ep(endpoint_t *ep)
|
|---|
| [741bcdeb] | 136 | {
|
|---|
| [6832245] | 137 | bus_t *bus_base = endpoint_get_bus(ep);
|
|---|
| [741bcdeb] | 138 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 139 | assert(bus);
|
|---|
| 140 | assert(ep);
|
|---|
| 141 |
|
|---|
| [6832245] | 142 | const int err = usb2_bus_ops.endpoint_unregister(ep);
|
|---|
| [741bcdeb] | 143 | if (err)
|
|---|
| 144 | return err;
|
|---|
| 145 |
|
|---|
| 146 | hc_dequeue_endpoint(bus->hc, ep);
|
|---|
| 147 | return EOK;
|
|---|
| [5fd9c30] | 148 | }
|
|---|
| 149 |
|
|---|
| [6832245] | 150 | static usb_transfer_batch_t *ehci_create_batch(endpoint_t *ep)
|
|---|
| [5fd9c30] | 151 | {
|
|---|
| 152 | ehci_transfer_batch_t *batch = ehci_transfer_batch_create(ep);
|
|---|
| 153 | return &batch->base;
|
|---|
| 154 | }
|
|---|
| [741bcdeb] | 155 |
|
|---|
| [6832245] | 156 | static void ehci_destroy_batch(usb_transfer_batch_t *batch)
|
|---|
| [5fd9c30] | 157 | {
|
|---|
| 158 | ehci_transfer_batch_destroy(ehci_transfer_batch_get(batch));
|
|---|
| [f9351b1] | 159 | }
|
|---|
| [741bcdeb] | 160 |
|
|---|
| [6832245] | 161 | static const bus_ops_t ehci_bus_ops = {
|
|---|
| 162 | .parent = &usb2_bus_ops,
|
|---|
| 163 |
|
|---|
| [32fb6bce] | 164 | .interrupt = ehci_hc_interrupt,
|
|---|
| 165 | .status = ehci_hc_status,
|
|---|
| [6832245] | 166 | .endpoint_destroy = ehci_endpoint_destroy,
|
|---|
| 167 | .endpoint_create = ehci_endpoint_create,
|
|---|
| 168 | .endpoint_register = ehci_register_ep,
|
|---|
| 169 | .endpoint_unregister = ehci_unregister_ep,
|
|---|
| 170 | .endpoint_set_toggle = ehci_ep_toggle_set,
|
|---|
| 171 | .endpoint_get_toggle = ehci_ep_toggle_get,
|
|---|
| 172 | .endpoint_count_bw = bandwidth_count_usb11,
|
|---|
| 173 | .batch_create = ehci_create_batch,
|
|---|
| 174 | .batch_destroy = ehci_destroy_batch,
|
|---|
| [32fb6bce] | 175 | .batch_schedule = ehci_hc_schedule,
|
|---|
| [6832245] | 176 | };
|
|---|
| 177 |
|
|---|
| [32fb6bce] | 178 | int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
|
|---|
| [741bcdeb] | 179 | {
|
|---|
| 180 | assert(hc);
|
|---|
| 181 | assert(bus);
|
|---|
| 182 |
|
|---|
| [6832245] | 183 | usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
|
|---|
| 184 | bus_t *bus_base = (bus_t *) bus;
|
|---|
| [741bcdeb] | 185 |
|
|---|
| [32fb6bce] | 186 | usb2_bus_init(usb2_bus, BANDWIDTH_AVAILABLE_USB11);
|
|---|
| [6832245] | 187 | bus_base->ops = &ehci_bus_ops;
|
|---|
| [5fd9c30] | 188 |
|
|---|
| [5995383c] | 189 | bus->hc = hc;
|
|---|
| 190 |
|
|---|
| [741bcdeb] | 191 | return EOK;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| [f9351b1] | 194 | /**
|
|---|
| 195 | * @}
|
|---|
| 196 | */
|
|---|