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 | * The Bus is a structure that serves as an interface of the HC driver
|
---|
35 | * implementation for the usbhost library. Every HC driver that uses libusbhost
|
---|
36 | * must use a bus_t (or its child), fill it with bus_ops and present it to the
|
---|
37 | * library. The library then handles the DDF interface and translates it to the
|
---|
38 | * bus callbacks.
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <ddf/driver.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <mem.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <str_error.h>
|
---|
46 | #include <usb/debug.h>
|
---|
47 |
|
---|
48 | #include "endpoint.h"
|
---|
49 | #include "bus.h"
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Initializes the base bus structure.
|
---|
53 | */
|
---|
54 | void bus_init(bus_t *bus, size_t device_size)
|
---|
55 | {
|
---|
56 | assert(bus);
|
---|
57 | assert(device_size >= sizeof(device_t));
|
---|
58 | memset(bus, 0, sizeof(bus_t));
|
---|
59 |
|
---|
60 | fibril_mutex_initialize(&bus->guard);
|
---|
61 | bus->device_size = device_size;
|
---|
62 | bus->default_address_speed = USB_SPEED_MAX;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Initialize the device_t structure belonging to a bus.
|
---|
67 | */
|
---|
68 | int bus_device_init(device_t *dev, bus_t *bus)
|
---|
69 | {
|
---|
70 | assert(bus);
|
---|
71 |
|
---|
72 | memset(dev, 0, sizeof(*dev));
|
---|
73 |
|
---|
74 | dev->bus = bus;
|
---|
75 |
|
---|
76 | link_initialize(&dev->link);
|
---|
77 | list_initialize(&dev->devices);
|
---|
78 | fibril_mutex_initialize(&dev->guard);
|
---|
79 |
|
---|
80 | return EOK;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Create a name of the ddf function node.
|
---|
85 | */
|
---|
86 | int bus_device_set_default_name(device_t *dev)
|
---|
87 | {
|
---|
88 | assert(dev);
|
---|
89 | assert(dev->fun);
|
---|
90 |
|
---|
91 | char buf[10] = { 0 }; /* usbxyz-ss */
|
---|
92 | snprintf(buf, sizeof(buf) - 1, "usb%u-%cs",
|
---|
93 | dev->address, usb_str_speed(dev->speed)[0]);
|
---|
94 |
|
---|
95 | return ddf_fun_set_name(dev->fun, buf);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Invoke the device_enumerate bus operation.
|
---|
100 | *
|
---|
101 | * There's no need to synchronize here, because no one knows the device yet.
|
---|
102 | */
|
---|
103 | int bus_device_enumerate(device_t *dev)
|
---|
104 | {
|
---|
105 | assert(dev);
|
---|
106 |
|
---|
107 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
|
---|
108 | if (!ops)
|
---|
109 | return ENOTSUP;
|
---|
110 |
|
---|
111 | if (dev->online)
|
---|
112 | return EINVAL;
|
---|
113 |
|
---|
114 | const int r = ops->device_enumerate(dev);
|
---|
115 | if (r)
|
---|
116 | return r;
|
---|
117 |
|
---|
118 | dev->online = true;
|
---|
119 |
|
---|
120 | if (dev->hub) {
|
---|
121 | fibril_mutex_lock(&dev->hub->guard);
|
---|
122 | list_append(&dev->link, &dev->hub->devices);
|
---|
123 | fibril_mutex_unlock(&dev->hub->guard);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return EOK;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Clean endpoints and children that could have been left behind after
|
---|
131 | * asking the driver of device to offline/remove a device.
|
---|
132 | *
|
---|
133 | * Note that EP0's lifetime is shared with the device, and as such is not
|
---|
134 | * touched.
|
---|
135 | */
|
---|
136 | static void device_clean_ep_children(device_t *dev, const char *op)
|
---|
137 | {
|
---|
138 | assert(fibril_mutex_is_locked(&dev->guard));
|
---|
139 |
|
---|
140 | /* Unregister endpoints left behind. */
|
---|
141 | for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
|
---|
142 | if (!dev->endpoints[i])
|
---|
143 | continue;
|
---|
144 |
|
---|
145 | usb_log_warning("USB device '%s' driver left endpoint %u registered after %s.",
|
---|
146 | ddf_fun_get_name(dev->fun), i, op);
|
---|
147 |
|
---|
148 | endpoint_t * const ep = dev->endpoints[i];
|
---|
149 | endpoint_add_ref(ep);
|
---|
150 |
|
---|
151 | fibril_mutex_unlock(&dev->guard);
|
---|
152 | bus_endpoint_remove(ep);
|
---|
153 | fibril_mutex_lock(&dev->guard);
|
---|
154 |
|
---|
155 | assert(dev->endpoints[i] == NULL);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* Remove also orphaned children. */
|
---|
159 | while (!list_empty(&dev->devices)) {
|
---|
160 | device_t * const child = list_get_instance(list_first(&dev->devices), device_t, link);
|
---|
161 |
|
---|
162 | usb_log_warning("USB device '%s' driver left device '%s' behind after %s.",
|
---|
163 | ddf_fun_get_name(dev->fun), ddf_fun_get_name(child->fun), op);
|
---|
164 | /*
|
---|
165 | * The child node won't disappear, because its parent's driver
|
---|
166 | * is already dead. And the child will need the guard to remove
|
---|
167 | * itself from the list.
|
---|
168 | */
|
---|
169 | fibril_mutex_unlock(&dev->guard);
|
---|
170 | bus_device_gone(child);
|
---|
171 | fibril_mutex_lock(&dev->guard);
|
---|
172 | }
|
---|
173 | assert(list_empty(&dev->devices));
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Resolve a USB device that is gone.
|
---|
178 | */
|
---|
179 | void bus_device_gone(device_t *dev)
|
---|
180 | {
|
---|
181 | assert(dev);
|
---|
182 | assert(dev->fun == NULL);
|
---|
183 |
|
---|
184 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_gone);
|
---|
185 | assert(ops);
|
---|
186 |
|
---|
187 | /* First, block new transfers and operations. */
|
---|
188 | fibril_mutex_lock(&dev->guard);
|
---|
189 | dev->online = false;
|
---|
190 |
|
---|
191 | /* Unbinding will need guard unlocked. */
|
---|
192 | fibril_mutex_unlock(&dev->guard);
|
---|
193 |
|
---|
194 | /* Remove our device from our hub's children. */
|
---|
195 | if (dev->hub) {
|
---|
196 | fibril_mutex_lock(&dev->hub->guard);
|
---|
197 | list_remove(&dev->link);
|
---|
198 | fibril_mutex_unlock(&dev->hub->guard);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Unbind the DDF function. That will result in dev_gone called in
|
---|
203 | * driver, which shall destroy its pipes and remove its children.
|
---|
204 | */
|
---|
205 | const int err = ddf_fun_unbind(dev->fun);
|
---|
206 | if (err) {
|
---|
207 | usb_log_error("Failed to unbind USB device '%s': %s",
|
---|
208 | ddf_fun_get_name(dev->fun), str_error(err));
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* Remove what driver left behind */
|
---|
213 | fibril_mutex_lock(&dev->guard);
|
---|
214 | device_clean_ep_children(dev, "removing");
|
---|
215 |
|
---|
216 | /* Tell the HC to release its resources. */
|
---|
217 | ops->device_gone(dev);
|
---|
218 |
|
---|
219 | /* Release the EP0 bus reference */
|
---|
220 | endpoint_del_ref(dev->endpoints[0]);
|
---|
221 |
|
---|
222 | /* Destroy the function, freeing also the device, unlocking mutex. */
|
---|
223 | ddf_fun_destroy(dev->fun);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * The user wants this device back online.
|
---|
228 | */
|
---|
229 | int bus_device_online(device_t *dev)
|
---|
230 | {
|
---|
231 | int err;
|
---|
232 | assert(dev);
|
---|
233 |
|
---|
234 | fibril_mutex_lock(&dev->guard);
|
---|
235 | if (dev->online) {
|
---|
236 | fibril_mutex_unlock(&dev->guard);
|
---|
237 | return EINVAL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /* First, tell the HC driver. */
|
---|
241 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
|
---|
242 | if (ops && (err = ops->device_online(dev))) {
|
---|
243 | usb_log_warning("Host controller refused to make device '%s' online: %s",
|
---|
244 | ddf_fun_get_name(dev->fun), str_error(err));
|
---|
245 | return err;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /* Allow creation of new endpoints and communication with the device. */
|
---|
249 | dev->online = true;
|
---|
250 |
|
---|
251 | /* Onlining will need the guard */
|
---|
252 | fibril_mutex_unlock(&dev->guard);
|
---|
253 |
|
---|
254 | if ((err = ddf_fun_online(dev->fun))) {
|
---|
255 | usb_log_warning("Failed to take device '%s' online: %s",
|
---|
256 | ddf_fun_get_name(dev->fun), str_error(err));
|
---|
257 | return err;
|
---|
258 | }
|
---|
259 |
|
---|
260 | usb_log_info("USB Device '%s' offlined.", ddf_fun_get_name(dev->fun));
|
---|
261 | return EOK;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * The user requested to take this device offline.
|
---|
266 | */
|
---|
267 | int bus_device_offline(device_t *dev)
|
---|
268 | {
|
---|
269 | int err;
|
---|
270 | assert(dev);
|
---|
271 |
|
---|
272 | /* Make sure we're the one who offlines this device */
|
---|
273 | if (!dev->online)
|
---|
274 | return ENOENT;
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * XXX: If the device is removed/offlined just now, this can fail on
|
---|
278 | * assertion. We most probably need some kind of enum status field to
|
---|
279 | * make the synchronization work.
|
---|
280 | */
|
---|
281 |
|
---|
282 | /* Tear down all drivers working with the device. */
|
---|
283 | if ((err = ddf_fun_offline(dev->fun))) {
|
---|
284 | return err;
|
---|
285 | }
|
---|
286 |
|
---|
287 | fibril_mutex_lock(&dev->guard);
|
---|
288 | dev->online = false;
|
---|
289 | device_clean_ep_children(dev, "offlining");
|
---|
290 |
|
---|
291 | /* Tell also the HC driver. */
|
---|
292 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
|
---|
293 | if (ops)
|
---|
294 | ops->device_offline(dev);
|
---|
295 |
|
---|
296 | fibril_mutex_unlock(&dev->guard);
|
---|
297 | usb_log_info("USB Device '%s' offlined.", ddf_fun_get_name(dev->fun));
|
---|
298 | return EOK;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Create and register new endpoint to the bus.
|
---|
303 | *
|
---|
304 | * @param[in] device The device of which the endpoint shall be created
|
---|
305 | * @param[in] desc Endpoint descriptors as reported by the device
|
---|
306 | * @param[out] out_ep The resulting new endpoint reference, if any. Can be NULL.
|
---|
307 | */
|
---|
308 | int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
|
---|
309 | {
|
---|
310 | int err;
|
---|
311 | assert(device);
|
---|
312 |
|
---|
313 | bus_t *bus = device->bus;
|
---|
314 |
|
---|
315 | const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
|
---|
316 | if (!register_ops)
|
---|
317 | return ENOTSUP;
|
---|
318 |
|
---|
319 | const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
|
---|
320 | endpoint_t *ep;
|
---|
321 | if (create_ops) {
|
---|
322 | ep = create_ops->endpoint_create(device, desc);
|
---|
323 | if (!ep)
|
---|
324 | return ENOMEM;
|
---|
325 | } else {
|
---|
326 | ep = calloc(1, sizeof(endpoint_t));
|
---|
327 | if (!ep)
|
---|
328 | return ENOMEM;
|
---|
329 | endpoint_init(ep, device, desc);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* Bus reference */
|
---|
333 | endpoint_add_ref(ep);
|
---|
334 |
|
---|
335 | if (ep->max_transfer_size == 0) {
|
---|
336 | usb_log_warning("Invalid endpoint description (mps %zu, "
|
---|
337 | "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
|
---|
338 | /* Bus reference */
|
---|
339 | endpoint_del_ref(ep);
|
---|
340 | return EINVAL;
|
---|
341 | }
|
---|
342 |
|
---|
343 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB.\n",
|
---|
344 | device->address, ep->endpoint,
|
---|
345 | usb_str_transfer_type(ep->transfer_type),
|
---|
346 | usb_str_direction(ep->direction),
|
---|
347 | ep->max_transfer_size);
|
---|
348 |
|
---|
349 | fibril_mutex_lock(&device->guard);
|
---|
350 | if (!device->online && ep->endpoint != 0) {
|
---|
351 | err = EAGAIN;
|
---|
352 | } else if (device->endpoints[ep->endpoint] != NULL) {
|
---|
353 | err = EEXIST;
|
---|
354 | } else {
|
---|
355 | err = register_ops->endpoint_register(ep);
|
---|
356 | if (!err)
|
---|
357 | device->endpoints[ep->endpoint] = ep;
|
---|
358 | }
|
---|
359 | fibril_mutex_unlock(&device->guard);
|
---|
360 | if (err) {
|
---|
361 | endpoint_del_ref(ep);
|
---|
362 | return err;
|
---|
363 | }
|
---|
364 |
|
---|
365 | if (out_ep) {
|
---|
366 | /* Exporting reference */
|
---|
367 | endpoint_add_ref(ep);
|
---|
368 | *out_ep = ep;
|
---|
369 | }
|
---|
370 |
|
---|
371 | return EOK;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Search for an endpoint. Returns a reference.
|
---|
376 | */
|
---|
377 | endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
|
---|
378 | {
|
---|
379 | assert(device);
|
---|
380 |
|
---|
381 | fibril_mutex_lock(&device->guard);
|
---|
382 | endpoint_t *ep = device->endpoints[endpoint];
|
---|
383 | if (ep) {
|
---|
384 | /* Exporting reference */
|
---|
385 | endpoint_add_ref(ep);
|
---|
386 | }
|
---|
387 | fibril_mutex_unlock(&device->guard);
|
---|
388 | return ep;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Remove an endpoint from the device. Consumes a reference.
|
---|
393 | */
|
---|
394 | int bus_endpoint_remove(endpoint_t *ep)
|
---|
395 | {
|
---|
396 | assert(ep);
|
---|
397 | assert(ep->device);
|
---|
398 |
|
---|
399 | device_t *device = ep->device;
|
---|
400 | if (!device)
|
---|
401 | return ENOENT;
|
---|
402 |
|
---|
403 | bus_t *bus = device->bus;
|
---|
404 |
|
---|
405 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
|
---|
406 | if (!ops)
|
---|
407 | return ENOTSUP;
|
---|
408 |
|
---|
409 | usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
|
---|
410 | device->address, ep->endpoint,
|
---|
411 | usb_str_transfer_type(ep->transfer_type),
|
---|
412 | usb_str_direction(ep->direction),
|
---|
413 | ep->max_transfer_size);
|
---|
414 |
|
---|
415 | fibril_mutex_lock(&device->guard);
|
---|
416 | ops->endpoint_unregister(ep);
|
---|
417 | device->endpoints[ep->endpoint] = NULL;
|
---|
418 | fibril_mutex_unlock(&device->guard);
|
---|
419 |
|
---|
420 | /* Bus reference */
|
---|
421 | endpoint_del_ref(ep);
|
---|
422 |
|
---|
423 | /* Given reference */
|
---|
424 | endpoint_del_ref(ep);
|
---|
425 |
|
---|
426 | return EOK;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Reserve the default address on the bus. Also, report the speed of the device
|
---|
431 | * that is listening on the default address.
|
---|
432 | *
|
---|
433 | * The speed is then used for devices enumerated while the address is reserved.
|
---|
434 | */
|
---|
435 | int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
|
---|
436 | {
|
---|
437 | assert(bus);
|
---|
438 |
|
---|
439 | fibril_mutex_lock(&bus->guard);
|
---|
440 | if (bus->default_address_speed != USB_SPEED_MAX) {
|
---|
441 | fibril_mutex_unlock(&bus->guard);
|
---|
442 | return EAGAIN;
|
---|
443 | } else {
|
---|
444 | bus->default_address_speed = speed;
|
---|
445 | fibril_mutex_unlock(&bus->guard);
|
---|
446 | return EOK;
|
---|
447 | }
|
---|
448 | }
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Release the default address.
|
---|
452 | */
|
---|
453 | void bus_release_default_address(bus_t *bus)
|
---|
454 | {
|
---|
455 | assert(bus);
|
---|
456 | bus->default_address_speed = USB_SPEED_MAX;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Initiate a transfer on the bus. Finds the target endpoint, creates
|
---|
461 | * a transfer batch and schedules it.
|
---|
462 | *
|
---|
463 | * @param device Device for which to send the batch
|
---|
464 | * @param target The target of the transfer.
|
---|
465 | * @param direction A direction of the transfer.
|
---|
466 | * @param data A pointer to the data buffer.
|
---|
467 | * @param size Size of the data buffer.
|
---|
468 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
469 | * @param on_complete Callback which is called after the batch is complete
|
---|
470 | * @param arg Callback parameter.
|
---|
471 | * @param name Communication identifier (for nicer output).
|
---|
472 | * @return Error code.
|
---|
473 | */
|
---|
474 | int bus_device_send_batch(device_t *device, usb_target_t target,
|
---|
475 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
476 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
477 | {
|
---|
478 | assert(device->address == target.address);
|
---|
479 |
|
---|
480 | /* Temporary reference */
|
---|
481 | endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
|
---|
482 | if (ep == NULL) {
|
---|
483 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
484 | device->address, target.endpoint, name);
|
---|
485 | return ENOENT;
|
---|
486 | }
|
---|
487 |
|
---|
488 | assert(ep->device == device);
|
---|
489 |
|
---|
490 | const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
|
---|
491 | on_complete, arg, name);
|
---|
492 |
|
---|
493 | /* Temporary reference */
|
---|
494 | endpoint_del_ref(ep);
|
---|
495 |
|
---|
496 | return err;
|
---|
497 | }
|
---|
498 |
|
---|
499 | typedef struct {
|
---|
500 | fibril_mutex_t done_mtx;
|
---|
501 | fibril_condvar_t done_cv;
|
---|
502 | unsigned done;
|
---|
503 |
|
---|
504 | size_t transfered_size;
|
---|
505 | int error;
|
---|
506 | } sync_data_t;
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Callback for finishing the transfer. Wake the issuing thread.
|
---|
510 | */
|
---|
511 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
---|
512 | {
|
---|
513 | sync_data_t *d = arg;
|
---|
514 | assert(d);
|
---|
515 | d->transfered_size = transfered_size;
|
---|
516 | d->error = error;
|
---|
517 | fibril_mutex_lock(&d->done_mtx);
|
---|
518 | d->done = 1;
|
---|
519 | fibril_condvar_broadcast(&d->done_cv);
|
---|
520 | fibril_mutex_unlock(&d->done_mtx);
|
---|
521 | return EOK;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Issue a transfer on the bus, wait for result.
|
---|
526 | *
|
---|
527 | * @param device Device for which to send the batch
|
---|
528 | * @param target The target of the transfer.
|
---|
529 | * @param direction A direction of the transfer.
|
---|
530 | * @param data A pointer to the data buffer.
|
---|
531 | * @param size Size of the data buffer.
|
---|
532 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
533 | * @param name Communication identifier (for nicer output).
|
---|
534 | */
|
---|
535 | ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
|
---|
536 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
537 | const char *name)
|
---|
538 | {
|
---|
539 | sync_data_t sd = { .done = 0 };
|
---|
540 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
541 | fibril_condvar_initialize(&sd.done_cv);
|
---|
542 |
|
---|
543 | const int ret = bus_device_send_batch(device, target, direction,
|
---|
544 | data, size, setup_data,
|
---|
545 | sync_transfer_complete, &sd, name);
|
---|
546 | if (ret != EOK)
|
---|
547 | return ret;
|
---|
548 |
|
---|
549 | fibril_mutex_lock(&sd.done_mtx);
|
---|
550 | while (!sd.done) {
|
---|
551 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
---|
552 | }
|
---|
553 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
554 |
|
---|
555 | return (sd.error == EOK)
|
---|
556 | ? (ssize_t) sd.transfered_size
|
---|
557 | : (ssize_t) sd.error;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * @}
|
---|
562 | */
|
---|