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 | */
|
---|
28 |
|
---|
29 | /** @addtogroup drvusbehci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief EHCI driver
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <usb/host/utils/malloc32.h>
|
---|
39 | #include <usb/host/bandwidth.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 |
|
---|
42 | #include "ehci_bus.h"
|
---|
43 | #include "ehci_batch.h"
|
---|
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 | */
|
---|
51 | static void ehci_ep_toggle_set(endpoint_t *ep, bool toggle)
|
---|
52 | {
|
---|
53 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
---|
54 | assert(instance);
|
---|
55 | assert(instance->qh);
|
---|
56 | ep->toggle = toggle;
|
---|
57 | if (qh_toggle_from_td(instance->qh))
|
---|
58 | usb_log_warning("EP(%p): Setting toggle bit for transfer "
|
---|
59 | "directed EP", instance);
|
---|
60 | qh_toggle_set(instance->qh, toggle);
|
---|
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 | */
|
---|
68 | static bool ehci_ep_toggle_get(endpoint_t *ep)
|
---|
69 | {
|
---|
70 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
---|
71 | assert(instance);
|
---|
72 | assert(instance->qh);
|
---|
73 |
|
---|
74 | if (qh_toggle_from_td(instance->qh))
|
---|
75 | usb_log_warning("EP(%p): Reading useless toggle bit", instance);
|
---|
76 | return qh_toggle_get(instance->qh);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Creates new hcd endpoint representation.
|
---|
80 | */
|
---|
81 | static endpoint_t *ehci_endpoint_create(device_t *dev, const usb_endpoint_desc_t *desc)
|
---|
82 | {
|
---|
83 | assert(dev);
|
---|
84 |
|
---|
85 | ehci_endpoint_t *ehci_ep = malloc(sizeof(ehci_endpoint_t));
|
---|
86 | if (ehci_ep == NULL)
|
---|
87 | return NULL;
|
---|
88 |
|
---|
89 | endpoint_init(&ehci_ep->base, dev, desc);
|
---|
90 |
|
---|
91 | // TODO: extract USB2 information from desc
|
---|
92 |
|
---|
93 | ehci_ep->qh = malloc32(sizeof(qh_t));
|
---|
94 | if (ehci_ep->qh == NULL) {
|
---|
95 | free(ehci_ep);
|
---|
96 | return NULL;
|
---|
97 | }
|
---|
98 |
|
---|
99 | link_initialize(&ehci_ep->link);
|
---|
100 | return &ehci_ep->base;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Disposes hcd endpoint structure
|
---|
104 | *
|
---|
105 | * @param[in] hcd driver using this instance.
|
---|
106 | * @param[in] ep endpoint structure.
|
---|
107 | */
|
---|
108 | static void ehci_endpoint_destroy(endpoint_t *ep)
|
---|
109 | {
|
---|
110 | assert(ep);
|
---|
111 | ehci_endpoint_t *instance = ehci_endpoint_get(ep);
|
---|
112 |
|
---|
113 | free32(instance->qh);
|
---|
114 | free(instance);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | static int ehci_register_ep(endpoint_t *ep)
|
---|
119 | {
|
---|
120 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
121 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
---|
122 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
---|
123 | assert(fibril_mutex_is_locked(&bus_base->guard));
|
---|
124 |
|
---|
125 | const int err = usb2_bus_ops.endpoint_register(ep);
|
---|
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 |
|
---|
135 | static int ehci_unregister_ep(endpoint_t *ep)
|
---|
136 | {
|
---|
137 | bus_t *bus_base = endpoint_get_bus(ep);
|
---|
138 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
---|
139 | assert(bus);
|
---|
140 | assert(ep);
|
---|
141 |
|
---|
142 | const int err = usb2_bus_ops.endpoint_unregister(ep);
|
---|
143 | if (err)
|
---|
144 | return err;
|
---|
145 |
|
---|
146 | hc_dequeue_endpoint(bus->hc, ep);
|
---|
147 | return EOK;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static usb_transfer_batch_t *ehci_create_batch(endpoint_t *ep)
|
---|
151 | {
|
---|
152 | ehci_transfer_batch_t *batch = ehci_transfer_batch_create(ep);
|
---|
153 | return &batch->base;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void ehci_destroy_batch(usb_transfer_batch_t *batch)
|
---|
157 | {
|
---|
158 | ehci_transfer_batch_destroy(ehci_transfer_batch_get(batch));
|
---|
159 | }
|
---|
160 |
|
---|
161 | static const bus_ops_t ehci_bus_ops = {
|
---|
162 | .parent = &usb2_bus_ops,
|
---|
163 |
|
---|
164 | .interrupt = ehci_hc_interrupt,
|
---|
165 | .status = ehci_hc_status,
|
---|
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,
|
---|
175 | .batch_schedule = ehci_hc_schedule,
|
---|
176 | };
|
---|
177 |
|
---|
178 | int ehci_bus_init(ehci_bus_t *bus, hc_t *hc)
|
---|
179 | {
|
---|
180 | assert(hc);
|
---|
181 | assert(bus);
|
---|
182 |
|
---|
183 | usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
|
---|
184 | bus_t *bus_base = (bus_t *) bus;
|
---|
185 |
|
---|
186 | usb2_bus_init(usb2_bus, BANDWIDTH_AVAILABLE_USB11);
|
---|
187 | bus_base->ops = &ehci_bus_ops;
|
---|
188 |
|
---|
189 | bus->hc = hc;
|
---|
190 |
|
---|
191 | return EOK;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * @}
|
---|
196 | */
|
---|