1 | /*
|
---|
2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
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 libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <ddf/driver.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <mem.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <usb/debug.h>
|
---|
41 |
|
---|
42 | #include "endpoint.h"
|
---|
43 | #include "bus.h"
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Initializes the bus structure.
|
---|
47 | */
|
---|
48 | void bus_init(bus_t *bus, size_t device_size)
|
---|
49 | {
|
---|
50 | assert(bus);
|
---|
51 | assert(device_size >= sizeof(device_t));
|
---|
52 | memset(bus, 0, sizeof(bus_t));
|
---|
53 |
|
---|
54 | fibril_mutex_initialize(&bus->guard);
|
---|
55 | bus->device_size = device_size;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int bus_device_init(device_t *dev, bus_t *bus)
|
---|
59 | {
|
---|
60 | assert(bus);
|
---|
61 |
|
---|
62 | memset(dev, 0, sizeof(*dev));
|
---|
63 |
|
---|
64 | dev->bus = bus;
|
---|
65 |
|
---|
66 | link_initialize(&dev->link);
|
---|
67 | list_initialize(&dev->devices);
|
---|
68 | fibril_mutex_initialize(&dev->guard);
|
---|
69 |
|
---|
70 | return EOK;
|
---|
71 | }
|
---|
72 |
|
---|
73 | int bus_device_set_default_name(device_t *dev)
|
---|
74 | {
|
---|
75 | assert(dev);
|
---|
76 | assert(dev->fun);
|
---|
77 |
|
---|
78 | char buf[10] = { 0 }; /* usbxyz-ss */
|
---|
79 | snprintf(buf, sizeof(buf) - 1, "usb%u-%cs",
|
---|
80 | dev->address, usb_str_speed(dev->speed)[0]);
|
---|
81 |
|
---|
82 | return ddf_fun_set_name(dev->fun, buf);
|
---|
83 | }
|
---|
84 |
|
---|
85 | int bus_device_enumerate(device_t *dev)
|
---|
86 | {
|
---|
87 | assert(dev);
|
---|
88 |
|
---|
89 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
|
---|
90 | if (!ops)
|
---|
91 | return ENOTSUP;
|
---|
92 |
|
---|
93 | return ops->device_enumerate(dev);
|
---|
94 | }
|
---|
95 |
|
---|
96 | int bus_device_remove(device_t *dev)
|
---|
97 | {
|
---|
98 | assert(dev);
|
---|
99 |
|
---|
100 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_remove);
|
---|
101 |
|
---|
102 | if (!ops)
|
---|
103 | return ENOTSUP;
|
---|
104 |
|
---|
105 | return ops->device_remove(dev);
|
---|
106 | }
|
---|
107 |
|
---|
108 | int bus_device_online(device_t *dev)
|
---|
109 | {
|
---|
110 | assert(dev);
|
---|
111 |
|
---|
112 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
|
---|
113 | if (!ops)
|
---|
114 | return ENOTSUP;
|
---|
115 |
|
---|
116 | return ops->device_online(dev);
|
---|
117 | }
|
---|
118 |
|
---|
119 | int bus_device_offline(device_t *dev)
|
---|
120 | {
|
---|
121 | assert(dev);
|
---|
122 |
|
---|
123 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
|
---|
124 | if (!ops)
|
---|
125 | return ENOTSUP;
|
---|
126 |
|
---|
127 | return ops->device_offline(dev);
|
---|
128 | }
|
---|
129 |
|
---|
130 | int bus_endpoint_add(device_t *device, const usb_endpoint_desc_t *desc, endpoint_t **out_ep)
|
---|
131 | {
|
---|
132 | int err;
|
---|
133 | assert(device);
|
---|
134 |
|
---|
135 | bus_t *bus = device->bus;
|
---|
136 |
|
---|
137 | if (desc->max_packet_size == 0 || desc->packets == 0) {
|
---|
138 | usb_log_warning("Invalid endpoint description (mps %zu, %u packets)", desc->max_packet_size, desc->packets);
|
---|
139 | return EINVAL;
|
---|
140 | }
|
---|
141 |
|
---|
142 | const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
|
---|
143 | const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
|
---|
144 | if (!create_ops || !register_ops)
|
---|
145 | return ENOTSUP;
|
---|
146 |
|
---|
147 | endpoint_t *ep = create_ops->endpoint_create(device, desc);
|
---|
148 | if (!ep)
|
---|
149 | return ENOMEM;
|
---|
150 |
|
---|
151 | /* Temporary reference */
|
---|
152 | endpoint_add_ref(ep);
|
---|
153 |
|
---|
154 | fibril_mutex_lock(&bus->guard);
|
---|
155 | err = register_ops->endpoint_register(ep);
|
---|
156 | fibril_mutex_unlock(&bus->guard);
|
---|
157 |
|
---|
158 | if (out_ep) {
|
---|
159 | /* Exporting reference */
|
---|
160 | endpoint_add_ref(ep);
|
---|
161 | *out_ep = ep;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Temporary reference */
|
---|
165 | endpoint_del_ref(ep);
|
---|
166 | return err;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Searches for an endpoint. Returns a reference.
|
---|
170 | */
|
---|
171 | endpoint_t *bus_find_endpoint(device_t *device, usb_target_t endpoint, usb_direction_t dir)
|
---|
172 | {
|
---|
173 | assert(device);
|
---|
174 |
|
---|
175 | bus_t *bus = device->bus;
|
---|
176 |
|
---|
177 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, device_find_endpoint);
|
---|
178 | if (!ops)
|
---|
179 | return NULL;
|
---|
180 |
|
---|
181 | fibril_mutex_lock(&bus->guard);
|
---|
182 | endpoint_t *ep = ops->device_find_endpoint(device, endpoint, dir);
|
---|
183 | if (ep) {
|
---|
184 | /* Exporting reference */
|
---|
185 | endpoint_add_ref(ep);
|
---|
186 | }
|
---|
187 |
|
---|
188 | fibril_mutex_unlock(&bus->guard);
|
---|
189 | return ep;
|
---|
190 | }
|
---|
191 |
|
---|
192 | int bus_endpoint_remove(endpoint_t *ep)
|
---|
193 | {
|
---|
194 | assert(ep);
|
---|
195 |
|
---|
196 | bus_t *bus = endpoint_get_bus(ep);
|
---|
197 |
|
---|
198 | const bus_ops_t *ops = BUS_OPS_LOOKUP(ep->device->bus->ops, endpoint_unregister);
|
---|
199 | if (!ops)
|
---|
200 | return ENOTSUP;
|
---|
201 |
|
---|
202 | fibril_mutex_lock(&bus->guard);
|
---|
203 | const int r = ops->endpoint_unregister(ep);
|
---|
204 | fibril_mutex_unlock(&bus->guard);
|
---|
205 |
|
---|
206 | if (r)
|
---|
207 | return r;
|
---|
208 |
|
---|
209 | /* Bus reference */
|
---|
210 | endpoint_del_ref(ep);
|
---|
211 |
|
---|
212 | return EOK;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
|
---|
216 | {
|
---|
217 | assert(bus);
|
---|
218 |
|
---|
219 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, reserve_default_address);
|
---|
220 | if (!ops)
|
---|
221 | return ENOTSUP;
|
---|
222 |
|
---|
223 | fibril_mutex_lock(&bus->guard);
|
---|
224 | const int r = ops->reserve_default_address(bus, speed);
|
---|
225 | fibril_mutex_unlock(&bus->guard);
|
---|
226 | return r;
|
---|
227 | }
|
---|
228 |
|
---|
229 | int bus_release_default_address(bus_t *bus)
|
---|
230 | {
|
---|
231 | assert(bus);
|
---|
232 |
|
---|
233 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, release_default_address);
|
---|
234 | if (!ops)
|
---|
235 | return ENOTSUP;
|
---|
236 |
|
---|
237 | fibril_mutex_lock(&bus->guard);
|
---|
238 | const int r = ops->release_default_address(bus);
|
---|
239 | fibril_mutex_unlock(&bus->guard);
|
---|
240 | return r;
|
---|
241 | }
|
---|
242 |
|
---|
243 | int bus_reset_toggle(bus_t *bus, usb_target_t target, bool all)
|
---|
244 | {
|
---|
245 | assert(bus);
|
---|
246 |
|
---|
247 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, reset_toggle);
|
---|
248 | if (!ops)
|
---|
249 | return ENOTSUP;
|
---|
250 |
|
---|
251 | fibril_mutex_lock(&bus->guard);
|
---|
252 | const int r = ops->reset_toggle(bus, target, all);
|
---|
253 | fibril_mutex_unlock(&bus->guard);
|
---|
254 | return r;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
258 | * @param device Device for which to send the batch
|
---|
259 | * @param target address and endpoint number.
|
---|
260 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
261 | * @param in Callback for device to host communication.
|
---|
262 | * @param out Callback for host to device communication.
|
---|
263 | * @param arg Callback parameter.
|
---|
264 | * @param name Communication identifier (for nicer output).
|
---|
265 | * @return Error code.
|
---|
266 | */
|
---|
267 | int bus_device_send_batch(device_t *device, usb_target_t target,
|
---|
268 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
269 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
270 | {
|
---|
271 | assert(device->address == target.address);
|
---|
272 |
|
---|
273 | /* Temporary reference */
|
---|
274 | endpoint_t *ep = bus_find_endpoint(device, target, direction);
|
---|
275 | if (ep == NULL) {
|
---|
276 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
277 | device->address, target.endpoint, name);
|
---|
278 | return ENOENT;
|
---|
279 | }
|
---|
280 |
|
---|
281 | assert(ep->device == device);
|
---|
282 |
|
---|
283 | const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
|
---|
284 | on_complete, arg, name);
|
---|
285 |
|
---|
286 | /* Temporary reference */
|
---|
287 | endpoint_del_ref(ep);
|
---|
288 |
|
---|
289 | return err;
|
---|
290 | }
|
---|
291 |
|
---|
292 | typedef struct {
|
---|
293 | fibril_mutex_t done_mtx;
|
---|
294 | fibril_condvar_t done_cv;
|
---|
295 | unsigned done;
|
---|
296 |
|
---|
297 | size_t transfered_size;
|
---|
298 | int error;
|
---|
299 | } sync_data_t;
|
---|
300 |
|
---|
301 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
---|
302 | {
|
---|
303 | sync_data_t *d = arg;
|
---|
304 | assert(d);
|
---|
305 | d->transfered_size = transfered_size;
|
---|
306 | d->error = error;
|
---|
307 | fibril_mutex_lock(&d->done_mtx);
|
---|
308 | d->done = 1;
|
---|
309 | fibril_condvar_broadcast(&d->done_cv);
|
---|
310 | fibril_mutex_unlock(&d->done_mtx);
|
---|
311 | return EOK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
|
---|
315 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
316 | const char *name)
|
---|
317 | {
|
---|
318 | sync_data_t sd = { .done = 0 };
|
---|
319 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
320 | fibril_condvar_initialize(&sd.done_cv);
|
---|
321 |
|
---|
322 | const int ret = bus_device_send_batch(device, target, direction,
|
---|
323 | data, size, setup_data,
|
---|
324 | sync_transfer_complete, &sd, name);
|
---|
325 | if (ret != EOK)
|
---|
326 | return ret;
|
---|
327 |
|
---|
328 | fibril_mutex_lock(&sd.done_mtx);
|
---|
329 | while (!sd.done) {
|
---|
330 | fibril_condvar_wait_timeout(&sd.done_cv, &sd.done_mtx, 3000000);
|
---|
331 | if (!sd.done)
|
---|
332 | usb_log_debug2("Still waiting...");
|
---|
333 | }
|
---|
334 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
335 |
|
---|
336 | return (sd.error == EOK)
|
---|
337 | ? (ssize_t) sd.transfered_size
|
---|
338 | : (ssize_t) sd.error;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * @}
|
---|
343 | */
|
---|