1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libusbhost
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief UHCI host controller driver structure
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <mem.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/host/hcd.h>
|
---|
44 | #include <usb/host/utility.h>
|
---|
45 |
|
---|
46 | #include "usb_transfer_batch.h"
|
---|
47 | #include "bus.h"
|
---|
48 |
|
---|
49 | #include "endpoint.h"
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Initialize provided endpoint structure.
|
---|
53 | */
|
---|
54 | void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
|
---|
55 | {
|
---|
56 | memset(ep, 0, sizeof(endpoint_t));
|
---|
57 |
|
---|
58 | assert(dev);
|
---|
59 | ep->device = dev;
|
---|
60 |
|
---|
61 | refcount_init(&ep->refcnt);
|
---|
62 | fibril_condvar_initialize(&ep->avail);
|
---|
63 |
|
---|
64 | ep->endpoint = USB_ED_GET_EP(desc->endpoint);
|
---|
65 | ep->direction = USB_ED_GET_DIR(desc->endpoint);
|
---|
66 | ep->transfer_type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
|
---|
67 | ep->max_packet_size = USB_ED_GET_MPS(desc->endpoint);
|
---|
68 | ep->packets_per_uframe = USB_ED_GET_ADD_OPPS(desc->endpoint) + 1;
|
---|
69 |
|
---|
70 | /** Direction both is our construct never present in descriptors */
|
---|
71 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
72 | ep->direction = USB_DIRECTION_BOTH;
|
---|
73 |
|
---|
74 | ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
|
---|
75 | ep->transfer_buffer_policy = DMA_POLICY_STRICT;
|
---|
76 | ep->required_transfer_buffer_policy = DMA_POLICY_STRICT;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Get the bus endpoint belongs to.
|
---|
81 | */
|
---|
82 | static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
|
---|
83 | {
|
---|
84 | return ep->device->bus->ops;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Increase the reference count on endpoint.
|
---|
89 | */
|
---|
90 | void endpoint_add_ref(endpoint_t *ep)
|
---|
91 | {
|
---|
92 | refcount_up(&ep->refcnt);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Call the desctruction callback. Default behavior is to free the memory directly.
|
---|
97 | */
|
---|
98 | static inline void endpoint_destroy(endpoint_t *ep)
|
---|
99 | {
|
---|
100 | const bus_ops_t *ops = get_bus_ops(ep);
|
---|
101 | if (ops->endpoint_destroy) {
|
---|
102 | ops->endpoint_destroy(ep);
|
---|
103 | } else {
|
---|
104 | assert(ep->active_batch == NULL);
|
---|
105 |
|
---|
106 | /* Assume mostly the eps will be allocated by malloc. */
|
---|
107 | free(ep);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Decrease the reference count.
|
---|
113 | */
|
---|
114 | void endpoint_del_ref(endpoint_t *ep)
|
---|
115 | {
|
---|
116 | if (refcount_down(&ep->refcnt))
|
---|
117 | endpoint_destroy(ep);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Mark the endpoint as online. Supply a guard to be used for this endpoint
|
---|
122 | * synchronization.
|
---|
123 | */
|
---|
124 | void endpoint_set_online(endpoint_t *ep, fibril_mutex_t *guard)
|
---|
125 | {
|
---|
126 | ep->guard = guard;
|
---|
127 | ep->online = true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Mark the endpoint as offline. All other fibrils waiting to activate this
|
---|
132 | * endpoint will be interrupted.
|
---|
133 | */
|
---|
134 | void endpoint_set_offline_locked(endpoint_t *ep)
|
---|
135 | {
|
---|
136 | assert(ep);
|
---|
137 | assert(fibril_mutex_is_locked(ep->guard));
|
---|
138 |
|
---|
139 | ep->online = false;
|
---|
140 | fibril_condvar_broadcast(&ep->avail);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Wait until a transfer finishes. Can be used even when the endpoint is
|
---|
145 | * offline (and is interrupted by the endpoint going offline).
|
---|
146 | */
|
---|
147 | void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
|
---|
148 | {
|
---|
149 | assert(ep);
|
---|
150 | assert(fibril_mutex_is_locked(ep->guard));
|
---|
151 |
|
---|
152 | if (ep->active_batch == NULL)
|
---|
153 | return;
|
---|
154 |
|
---|
155 | fibril_condvar_wait_timeout(&ep->avail, ep->guard, timeout);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Mark the endpoint as active and block access for further fibrils. If the
|
---|
160 | * endpoint is already active, it will block on ep->avail condvar.
|
---|
161 | *
|
---|
162 | * Call only under endpoint guard. After you activate the endpoint and release
|
---|
163 | * the guard, you must assume that particular transfer is already
|
---|
164 | * finished/aborted.
|
---|
165 | *
|
---|
166 | * Activation and deactivation is not done by the library to maximize
|
---|
167 | * performance. The HC might want to prepare some memory buffers prior to
|
---|
168 | * interfering with other world.
|
---|
169 | *
|
---|
170 | * @param batch Transfer batch this endpoint is blocked by.
|
---|
171 | */
|
---|
172 | int endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
|
---|
173 | {
|
---|
174 | assert(ep);
|
---|
175 | assert(batch);
|
---|
176 | assert(batch->ep == ep);
|
---|
177 | assert(ep->guard);
|
---|
178 | assert(fibril_mutex_is_locked(ep->guard));
|
---|
179 |
|
---|
180 | while (ep->online && ep->active_batch != NULL)
|
---|
181 | fibril_condvar_wait(&ep->avail, ep->guard);
|
---|
182 |
|
---|
183 | if (!ep->online)
|
---|
184 | return EINTR;
|
---|
185 |
|
---|
186 | assert(ep->active_batch == NULL);
|
---|
187 | ep->active_batch = batch;
|
---|
188 | return EOK;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Mark the endpoint as inactive and allow access for further fibrils.
|
---|
193 | */
|
---|
194 | void endpoint_deactivate_locked(endpoint_t *ep)
|
---|
195 | {
|
---|
196 | assert(ep);
|
---|
197 | assert(fibril_mutex_is_locked(ep->guard));
|
---|
198 |
|
---|
199 | ep->active_batch = NULL;
|
---|
200 | fibril_condvar_signal(&ep->avail);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
|
---|
205 | * bandwidth requirements and schedules the batch.
|
---|
206 | *
|
---|
207 | * @param endpoint Endpoint for which to send the batch
|
---|
208 | */
|
---|
209 | errno_t endpoint_send_batch(endpoint_t *ep, const transfer_request_t *req)
|
---|
210 | {
|
---|
211 | assert(ep);
|
---|
212 | assert(req);
|
---|
213 |
|
---|
214 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
215 | usb_log_debug("%s %d:%d %zu/%zuB, setup %#016" PRIx64, req->name,
|
---|
216 | req->target.address, req->target.endpoint,
|
---|
217 | req->size, ep->max_packet_size,
|
---|
218 | req->setup);
|
---|
219 | } else {
|
---|
220 | usb_log_debug("%s %d:%d %zu/%zuB", req->name,
|
---|
221 | req->target.address, req->target.endpoint,
|
---|
222 | req->size, ep->max_packet_size);
|
---|
223 | }
|
---|
224 |
|
---|
225 | device_t *const device = ep->device;
|
---|
226 | if (!device) {
|
---|
227 | usb_log_warning("Endpoint detached");
|
---|
228 | return EAGAIN;
|
---|
229 | }
|
---|
230 |
|
---|
231 | const bus_ops_t *ops = device->bus->ops;
|
---|
232 | if (!ops->batch_schedule) {
|
---|
233 | usb_log_error("HCD does not implement scheduler.");
|
---|
234 | return ENOTSUP;
|
---|
235 | }
|
---|
236 |
|
---|
237 | size_t size = req->size;
|
---|
238 | /*
|
---|
239 | * Limit transfers with reserved bandwidth to the amount reserved.
|
---|
240 | * OUT transfers are rejected, IN can be just trimmed in advance.
|
---|
241 | */
|
---|
242 | if (size > ep->max_transfer_size &&
|
---|
243 | (ep->transfer_type == USB_TRANSFER_INTERRUPT ||
|
---|
244 | ep->transfer_type == USB_TRANSFER_ISOCHRONOUS)) {
|
---|
245 | if (req->dir == USB_DIRECTION_OUT)
|
---|
246 | return ENOSPC;
|
---|
247 | else
|
---|
248 | size = ep->max_transfer_size;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /* Offline devices don't schedule transfers other than on EP0. */
|
---|
252 | if (!device->online && ep->endpoint > 0)
|
---|
253 | return EAGAIN;
|
---|
254 |
|
---|
255 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
256 | if (!batch) {
|
---|
257 | usb_log_error("Failed to create transfer batch.");
|
---|
258 | return ENOMEM;
|
---|
259 | }
|
---|
260 |
|
---|
261 | batch->target = req->target;
|
---|
262 | batch->setup.packed = req->setup;
|
---|
263 | batch->dir = req->dir;
|
---|
264 | batch->size = size;
|
---|
265 | batch->offset = req->offset;
|
---|
266 | batch->dma_buffer = req->buffer;
|
---|
267 |
|
---|
268 | dma_buffer_acquire(&batch->dma_buffer);
|
---|
269 |
|
---|
270 | if (batch->offset != 0) {
|
---|
271 | usb_log_debug("A transfer with nonzero offset requested.");
|
---|
272 | usb_transfer_batch_bounce(batch);
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (usb_transfer_batch_bounce_required(batch))
|
---|
276 | usb_transfer_batch_bounce(batch);
|
---|
277 |
|
---|
278 | batch->on_complete = req->on_complete;
|
---|
279 | batch->on_complete_data = req->arg;
|
---|
280 |
|
---|
281 | const int ret = ops->batch_schedule(batch);
|
---|
282 | if (ret != EOK) {
|
---|
283 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
284 | usb_transfer_batch_destroy(batch);
|
---|
285 | }
|
---|
286 |
|
---|
287 | return ret;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * @}
|
---|
292 | */
|
---|