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 | #include <usb/host/utility.h>
|
---|
46 |
|
---|
47 | #include "usb_transfer_batch.h"
|
---|
48 | #include "bus.h"
|
---|
49 |
|
---|
50 | #include "endpoint.h"
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Initialize provided endpoint structure.
|
---|
54 | */
|
---|
55 | void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
|
---|
56 | {
|
---|
57 | memset(ep, 0, sizeof(endpoint_t));
|
---|
58 |
|
---|
59 | assert(dev);
|
---|
60 | ep->device = dev;
|
---|
61 |
|
---|
62 | atomic_set(&ep->refcnt, 0);
|
---|
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 | /**
|
---|
124 | * Wait until the endpoint have no transfer scheduled.
|
---|
125 | */
|
---|
126 | void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
|
---|
127 | {
|
---|
128 | assert(fibril_mutex_is_locked(&ep->guard));
|
---|
129 |
|
---|
130 | if (ep->active_batch != NULL)
|
---|
131 | fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
|
---|
132 |
|
---|
133 | while (timeout == 0 && ep->active_batch != NULL)
|
---|
134 | fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Mark the endpoint as active and block access for further fibrils. If the
|
---|
139 | * endpoint is already active, it will block on ep->avail condvar.
|
---|
140 | *
|
---|
141 | * Call only under endpoint guard. After you activate the endpoint and release
|
---|
142 | * the guard, you must assume that particular transfer is already finished/aborted.
|
---|
143 | *
|
---|
144 | * @param ep endpoint_t structure.
|
---|
145 | * @param batch Transfer batch this endpoint is bocked by.
|
---|
146 | */
|
---|
147 | void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
|
---|
148 | {
|
---|
149 | assert(ep);
|
---|
150 | assert(batch);
|
---|
151 | assert(batch->ep == ep);
|
---|
152 |
|
---|
153 | endpoint_wait_timeout_locked(ep, 0);
|
---|
154 | ep->active_batch = batch;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Mark the endpoint as inactive and allow access for further fibrils.
|
---|
159 | *
|
---|
160 | * @param ep endpoint_t structure.
|
---|
161 | */
|
---|
162 | void endpoint_deactivate_locked(endpoint_t *ep)
|
---|
163 | {
|
---|
164 | assert(ep);
|
---|
165 | assert(fibril_mutex_is_locked(&ep->guard));
|
---|
166 | ep->active_batch = NULL;
|
---|
167 | fibril_condvar_signal(&ep->avail);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Call the bus operation to count bandwidth.
|
---|
172 | *
|
---|
173 | * @param ep Endpoint on which the transfer will take place.
|
---|
174 | * @param size The payload size.
|
---|
175 | */
|
---|
176 | ssize_t endpoint_count_bw(endpoint_t *ep, size_t size)
|
---|
177 | {
|
---|
178 | assert(ep);
|
---|
179 |
|
---|
180 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
|
---|
181 | if (!ops)
|
---|
182 | return 0;
|
---|
183 |
|
---|
184 | return ops->endpoint_count_bw(ep, size);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
|
---|
189 | * bandwidth requirements and schedules the batch.
|
---|
190 | *
|
---|
191 | * @param endpoint Endpoint for which to send the batch
|
---|
192 | * @param target The target of the transfer.
|
---|
193 | * @param direction A direction of the transfer.
|
---|
194 | * @param data A pointer to the data buffer.
|
---|
195 | * @param size Size of the data buffer.
|
---|
196 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
197 | * @param on_complete Callback which is called after the batch is complete
|
---|
198 | * @param arg Callback parameter.
|
---|
199 | * @param name Communication identifier (for nicer output).
|
---|
200 | */
|
---|
201 | int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
|
---|
202 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
203 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
204 | {
|
---|
205 | usb_log_debug2("%s %d:%d %zu(%zu).",
|
---|
206 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
207 |
|
---|
208 | device_t * const device = ep->device;
|
---|
209 | if (!device) {
|
---|
210 | usb_log_warning("Endpoint detached");
|
---|
211 | return EAGAIN;
|
---|
212 | }
|
---|
213 |
|
---|
214 | const bus_ops_t *ops = BUS_OPS_LOOKUP(device->bus->ops, batch_schedule);
|
---|
215 | if (!ops) {
|
---|
216 | usb_log_error("HCD does not implement scheduler.");
|
---|
217 | return ENOTSUP;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Offline devices don't schedule transfers other than on EP0. */
|
---|
221 | if (!device->online && ep->endpoint > 0) {
|
---|
222 | return EAGAIN;
|
---|
223 | }
|
---|
224 |
|
---|
225 | const size_t bw = endpoint_count_bw(ep, size);
|
---|
226 | /* Check if we have enough bandwidth reserved */
|
---|
227 | if (ep->bandwidth < bw) {
|
---|
228 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
229 | "but only %zu is reserved.\n",
|
---|
230 | device->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
231 | return ENOSPC;
|
---|
232 | }
|
---|
233 |
|
---|
234 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
235 | if (!batch) {
|
---|
236 | usb_log_error("Failed to create transfer batch.");
|
---|
237 | return ENOMEM;
|
---|
238 | }
|
---|
239 |
|
---|
240 | batch->target = target;
|
---|
241 | batch->buffer = data;
|
---|
242 | batch->buffer_size = size;
|
---|
243 | batch->setup.packed = setup_data;
|
---|
244 | batch->dir = direction;
|
---|
245 | batch->on_complete = on_complete;
|
---|
246 | batch->on_complete_data = arg;
|
---|
247 |
|
---|
248 | const int ret = ops->batch_schedule(batch);
|
---|
249 | if (ret != EOK) {
|
---|
250 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
251 | usb_transfer_batch_destroy(batch);
|
---|
252 | }
|
---|
253 |
|
---|
254 | return ret;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * @}
|
---|
259 | */
|
---|