[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>
|
---|
[741bcdeb] | 38 | #include <usb/host/bandwidth.h>
|
---|
| 39 | #include <usb/debug.h>
|
---|
[f9351b1] | 40 |
|
---|
[741bcdeb] | 41 | #include "ehci_bus.h"
|
---|
[5fd9c30] | 42 | #include "ehci_batch.h"
|
---|
[f9351b1] | 43 | #include "hc.h"
|
---|
| 44 |
|
---|
| 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 | */
|
---|
[56257ba] | 50 | static void ehci_ep_toggle_reset(endpoint_t *ep)
|
---|
[f9351b1] | 51 | {
|
---|
[741bcdeb] | 52 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
---|
[42de21a] | 53 | if (qh_toggle_from_td(instance->qh))
|
---|
[56257ba] | 54 | usb_log_warning("EP(%p): Resetting toggle bit for transfer directed EP", instance);
|
---|
| 55 | qh_toggle_set(instance->qh, 0);
|
---|
| 56 | ep->toggle = 0;
|
---|
[f9351b1] | 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | /** Creates new hcd endpoint representation.
|
---|
| 61 | */
|
---|
[9efad54] | 62 | static endpoint_t *ehci_endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
|
---|
[f9351b1] | 63 | {
|
---|
[6832245] | 64 | assert(dev);
|
---|
[b5f813c] | 65 |
|
---|
[f9351b1] | 66 | ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
|
---|
| 67 | if (ehci_ep == NULL)
|
---|
[741bcdeb] | 68 | return NULL;
|
---|
| 69 |
|
---|
[6832245] | 70 | endpoint_init(&ehci_ep->base, dev, desc);
|
---|
| 71 |
|
---|
| 72 | // TODO: extract USB2 information from desc
|
---|
[35c37fc] | 73 |
|
---|
| 74 | if (dma_buffer_alloc(&ehci_ep->dma_buffer, sizeof(qh_t)))
|
---|
[741bcdeb] | 75 | return NULL;
|
---|
[35c37fc] | 76 |
|
---|
| 77 | ehci_ep->qh = ehci_ep->dma_buffer.virt;
|
---|
[f9351b1] | 78 |
|
---|
[3f2cb17] | 79 | link_initialize(&ehci_ep->link);
|
---|
[5995383c] | 80 | return &ehci_ep->base;
|
---|
[f9351b1] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Disposes hcd endpoint structure
|
---|
| 84 | *
|
---|
| 85 | * @param[in] hcd driver using this instance.
|
---|
| 86 | * @param[in] ep endpoint structure.
|
---|
| 87 | */
|
---|
[741bcdeb] | 88 | static void ehci_endpoint_destroy(endpoint_t *ep)
|
---|
[f9351b1] | 89 | {
|
---|
| 90 | assert(ep);
|
---|
| 91 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
---|
[741bcdeb] | 92 |
|
---|
[35c37fc] | 93 | dma_buffer_free(&instance->dma_buffer);
|
---|
[741bcdeb] | 94 | free(instance);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
[6832245] | 98 | static int ehci_register_ep(endpoint_t *ep)
|
---|
[741bcdeb] | 99 | {
|
---|
[6832245] | 100 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
[741bcdeb] | 101 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
---|
| 102 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
---|
| 103 |
|
---|
[6832245] | 104 | const int err = usb2_bus_ops.endpoint_register(ep);
|
---|
[741bcdeb] | 105 | if (err)
|
---|
| 106 | return err;
|
---|
| 107 |
|
---|
| 108 | qh_init(ehci_ep->qh, ep);
|
---|
| 109 | hc_enqueue_endpoint(bus->hc, ep);
|
---|
| 110 |
|
---|
| 111 | return EOK;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[bad4a05] | 114 | static void ehci_unregister_ep(endpoint_t *ep)
|
---|
[741bcdeb] | 115 | {
|
---|
[6832245] | 116 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
[741bcdeb] | 117 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
---|
| 118 | assert(bus);
|
---|
| 119 | assert(ep);
|
---|
| 120 |
|
---|
[bad4a05] | 121 | usb2_bus_ops.endpoint_unregister(ep);
|
---|
[741bcdeb] | 122 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
[5fd9c30] | 123 | }
|
---|
| 124 |
|
---|
[6832245] | 125 | static usb_transfer_batch_t *ehci_create_batch(endpoint_t *ep)
|
---|
[5fd9c30] | 126 | {
|
---|
| 127 | ehci_transfer_batch_t *batch = ehci_transfer_batch_create(ep);
|
---|
| 128 | return &batch->base;
|
---|
| 129 | }
|
---|
[741bcdeb] | 130 |
|
---|
[6832245] | 131 | static void ehci_destroy_batch(usb_transfer_batch_t *batch)
|
---|
[5fd9c30] | 132 | {
|
---|
| 133 | ehci_transfer_batch_destroy(ehci_transfer_batch_get(batch));
|
---|
[f9351b1] | 134 | }
|
---|
[741bcdeb] | 135 |
|
---|
[6832245] | 136 | static const bus_ops_t ehci_bus_ops = {
|
---|
| 137 | .parent = &usb2_bus_ops,
|
---|
| 138 |
|
---|
[32fb6bce] | 139 | .interrupt = ehci_hc_interrupt,
|
---|
| 140 | .status = ehci_hc_status,
|
---|
[6832245] | 141 | .endpoint_destroy = ehci_endpoint_destroy,
|
---|
| 142 | .endpoint_create = ehci_endpoint_create,
|
---|
| 143 | .endpoint_register = ehci_register_ep,
|
---|
| 144 | .endpoint_unregister = ehci_unregister_ep,
|
---|
[56257ba] | 145 | .endpoint_toggle_reset = ehci_ep_toggle_reset,
|
---|
[ecbad17] | 146 | .endpoint_count_bw = bandwidth_count_usb20,
|
---|
[6832245] | 147 | .batch_create = ehci_create_batch,
|
---|
| 148 | .batch_destroy = ehci_destroy_batch,
|
---|
[32fb6bce] | 149 | .batch_schedule = ehci_hc_schedule,
|
---|
[6832245] | 150 | };
|
---|
| 151 |
|
---|
[32fb6bce] | 152 | int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
|
---|
[741bcdeb] | 153 | {
|
---|
| 154 | assert(hc);
|
---|
| 155 | assert(bus);
|
---|
| 156 |
|
---|
[6832245] | 157 | usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
|
---|
| 158 | bus_t *bus_base = (bus_t *) bus;
|
---|
[741bcdeb] | 159 |
|
---|
[ecbad17] | 160 | usb2_bus_init(usb2_bus, BANDWIDTH_AVAILABLE_USB20);
|
---|
[6832245] | 161 | bus_base->ops = &ehci_bus_ops;
|
---|
[5fd9c30] | 162 |
|
---|
[5995383c] | 163 | bus->hc = hc;
|
---|
| 164 |
|
---|
[741bcdeb] | 165 | return EOK;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[f9351b1] | 168 | /**
|
---|
| 169 | * @}
|
---|
| 170 | */
|
---|