1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
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 <atomic.h>
|
---|
39 | #include <mem.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <usb/debug.h>
|
---|
43 | #include <usb/descriptor.h>
|
---|
44 | #include <usb/host/hcd.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 | atomic_set(&ep->refcnt, 0);
|
---|
62 | link_initialize(&ep->link);
|
---|
63 | fibril_mutex_initialize(&ep->guard);
|
---|
64 | fibril_condvar_initialize(&ep->avail);
|
---|
65 |
|
---|
66 | ep->endpoint = USB_ED_GET_EP(desc->endpoint);
|
---|
67 | ep->direction = USB_ED_GET_DIR(desc->endpoint);
|
---|
68 | ep->transfer_type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
|
---|
69 | ep->max_packet_size = USB_ED_GET_MPS(desc->endpoint);
|
---|
70 | ep->packets_per_uframe = USB_ED_GET_ADD_OPPS(desc->endpoint) + 1;
|
---|
71 |
|
---|
72 | /** Direction both is our construct never present in descriptors */
|
---|
73 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
74 | ep->direction = USB_DIRECTION_BOTH;
|
---|
75 |
|
---|
76 | ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
|
---|
77 |
|
---|
78 | ep->bandwidth = endpoint_count_bw(ep, ep->max_transfer_size);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Get the bus endpoint belongs to.
|
---|
83 | */
|
---|
84 | static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
|
---|
85 | {
|
---|
86 | return ep->device->bus->ops;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Increase the reference count on endpoint.
|
---|
91 | */
|
---|
92 | void endpoint_add_ref(endpoint_t *ep)
|
---|
93 | {
|
---|
94 | atomic_inc(&ep->refcnt);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Call the desctruction callback. Default behavior is to free the memory directly.
|
---|
99 | */
|
---|
100 | static inline void endpoint_destroy(endpoint_t *ep)
|
---|
101 | {
|
---|
102 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
|
---|
103 | if (ops) {
|
---|
104 | ops->endpoint_destroy(ep);
|
---|
105 | } else {
|
---|
106 | assert(ep->active_batch == NULL);
|
---|
107 |
|
---|
108 | /* Assume mostly the eps will be allocated by malloc. */
|
---|
109 | free(ep);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Decrease the reference count.
|
---|
115 | */
|
---|
116 | void endpoint_del_ref(endpoint_t *ep)
|
---|
117 | {
|
---|
118 | if (atomic_predec(&ep->refcnt) == 0) {
|
---|
119 | endpoint_destroy(ep);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | static void endpoint_toggle_reset(endpoint_t *ep, toggle_reset_mode_t mode);
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Wait until the endpoint have no transfer scheduled.
|
---|
127 | */
|
---|
128 | void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
|
---|
129 | {
|
---|
130 | assert(fibril_mutex_is_locked(&ep->guard));
|
---|
131 |
|
---|
132 | if (ep->active_batch != NULL)
|
---|
133 | fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
|
---|
134 |
|
---|
135 | while (timeout == 0 && ep->active_batch != NULL)
|
---|
136 | fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Mark the endpoint as active and block access for further fibrils. If the
|
---|
141 | * endpoint is already active, it will block on ep->avail condvar.
|
---|
142 | *
|
---|
143 | * Call only under endpoint guard. After you activate the endpoint and release
|
---|
144 | * the guard, you must assume that particular transfer is already finished/aborted.
|
---|
145 | *
|
---|
146 | * @param ep endpoint_t structure.
|
---|
147 | * @param batch Transfer batch this endpoint is bocked by.
|
---|
148 | */
|
---|
149 | void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
|
---|
150 | {
|
---|
151 | assert(ep);
|
---|
152 | assert(batch);
|
---|
153 | assert(batch->ep == ep);
|
---|
154 |
|
---|
155 | endpoint_wait_timeout_locked(ep, 0);
|
---|
156 | ep->active_batch = batch;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Mark the endpoint as inactive and allow access for further fibrils.
|
---|
161 | *
|
---|
162 | * @param ep endpoint_t structure.
|
---|
163 | */
|
---|
164 | void endpoint_deactivate_locked(endpoint_t *ep)
|
---|
165 | {
|
---|
166 | assert(ep);
|
---|
167 | assert(fibril_mutex_is_locked(&ep->guard));
|
---|
168 |
|
---|
169 | if (ep->active_batch && ep->active_batch->error == EOK)
|
---|
170 | endpoint_toggle_reset(ep, ep->active_batch->toggle_reset_mode);
|
---|
171 |
|
---|
172 | ep->active_batch = NULL;
|
---|
173 | fibril_condvar_signal(&ep->avail);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * The transfer on an endpoint can trigger a reset of the toggle bit. This
|
---|
178 | * function calls the respective bus callbacks to resolve it.
|
---|
179 | *
|
---|
180 | * @param ep The endpoint that triggered the reset
|
---|
181 | * @param mode Whether to reset no, one or all endpoints on a device.
|
---|
182 | */
|
---|
183 | static void endpoint_toggle_reset(endpoint_t *ep, toggle_reset_mode_t mode)
|
---|
184 | {
|
---|
185 | assert(ep);
|
---|
186 |
|
---|
187 | if (mode == RESET_NONE)
|
---|
188 | return;
|
---|
189 |
|
---|
190 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_toggle_reset);
|
---|
191 | if (!ops)
|
---|
192 | return;
|
---|
193 |
|
---|
194 |
|
---|
195 | if (mode == RESET_ALL) {
|
---|
196 | const device_t *dev = ep->device;
|
---|
197 | for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
|
---|
198 | if (dev->endpoints[i])
|
---|
199 | ops->endpoint_toggle_reset(dev->endpoints[i]);
|
---|
200 | }
|
---|
201 | } else {
|
---|
202 | ops->endpoint_toggle_reset(ep);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Call the bus operation to count bandwidth.
|
---|
208 | *
|
---|
209 | * @param ep Endpoint on which the transfer will take place.
|
---|
210 | * @param size The payload size.
|
---|
211 | */
|
---|
212 | ssize_t endpoint_count_bw(endpoint_t *ep, size_t size)
|
---|
213 | {
|
---|
214 | assert(ep);
|
---|
215 |
|
---|
216 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
|
---|
217 | if (!ops)
|
---|
218 | return 0;
|
---|
219 |
|
---|
220 | return ops->endpoint_count_bw(ep, size);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
|
---|
225 | * bandwidth requirements and schedules the batch.
|
---|
226 | *
|
---|
227 | * @param endpoint Endpoint for which to send the batch
|
---|
228 | * @param target The target of the transfer.
|
---|
229 | * @param direction A direction of the transfer.
|
---|
230 | * @param data A pointer to the data buffer.
|
---|
231 | * @param size Size of the data buffer.
|
---|
232 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
233 | * @param on_complete Callback which is called after the batch is complete
|
---|
234 | * @param arg Callback parameter.
|
---|
235 | * @param name Communication identifier (for nicer output).
|
---|
236 | */
|
---|
237 | int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
|
---|
238 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
239 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
240 | {
|
---|
241 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
242 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
243 |
|
---|
244 | device_t * const device = ep->device;
|
---|
245 | if (!device) {
|
---|
246 | usb_log_warning("Endpoint detached");
|
---|
247 | return EAGAIN;
|
---|
248 | }
|
---|
249 |
|
---|
250 | const bus_ops_t *ops = BUS_OPS_LOOKUP(device->bus->ops, batch_schedule);
|
---|
251 | if (!ops) {
|
---|
252 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
253 | return ENOTSUP;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /* Offline devices don't schedule transfers other than on EP0. */
|
---|
257 | if (!device->online && ep->endpoint > 0) {
|
---|
258 | return EAGAIN;
|
---|
259 | }
|
---|
260 |
|
---|
261 | const size_t bw = endpoint_count_bw(ep, size);
|
---|
262 | /* Check if we have enough bandwidth reserved */
|
---|
263 | if (ep->bandwidth < bw) {
|
---|
264 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
265 | "but only %zu is reserved.\n",
|
---|
266 | device->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
267 | return ENOSPC;
|
---|
268 | }
|
---|
269 |
|
---|
270 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
271 | if (!batch) {
|
---|
272 | usb_log_error("Failed to create transfer batch.\n");
|
---|
273 | return ENOMEM;
|
---|
274 | }
|
---|
275 |
|
---|
276 | batch->target = target;
|
---|
277 | batch->buffer = data;
|
---|
278 | batch->buffer_size = size;
|
---|
279 | batch->setup.packed = setup_data;
|
---|
280 | batch->dir = direction;
|
---|
281 | batch->on_complete = on_complete;
|
---|
282 | batch->on_complete_data = arg;
|
---|
283 |
|
---|
284 | /* Check for commands that reset toggle bit */
|
---|
285 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
286 | batch->toggle_reset_mode
|
---|
287 | = hcd_get_request_toggle_reset_mode(&batch->setup.packet);
|
---|
288 |
|
---|
289 | const int ret = ops->batch_schedule(batch);
|
---|
290 | if (ret != EOK) {
|
---|
291 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
292 | usb_transfer_batch_destroy(batch);
|
---|
293 | }
|
---|
294 |
|
---|
295 | return ret;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * @}
|
---|
300 | */
|
---|