source: mainline/uspace/lib/usbhost/src/bus.c@ eec201d

Last change on this file since eec201d was 64532a97, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Fix insufficient buffer size.

  • Property mode set to 100644
File size: 17.3 KB
RevLine 
[41924f30]1/*
[e0a5d4c]2 * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek
[41924f30]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 *
[1102eca]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.
[41924f30]39 */
40
[20eaa82]41#include <ddf/driver.h>
[41924f30]42#include <errno.h>
[64fea02]43#include <mem.h>
[a6afb4c]44#include <macros.h>
[20eaa82]45#include <stdio.h>
[0892663a]46#include <str_error.h>
[64fea02]47#include <usb/debug.h>
[1d758fc]48#include <usb/dma_buffer.h>
[64fea02]49
50#include "endpoint.h"
51#include "bus.h"
[41924f30]52
53/**
[1102eca]54 * Initializes the base bus structure.
[41924f30]55 */
[32fb6bce]56void bus_init(bus_t *bus, size_t device_size)
[41924f30]57{
[20eaa82]58 assert(bus);
59 assert(device_size >= sizeof(device_t));
[41924f30]60 memset(bus, 0, sizeof(bus_t));
61
62 fibril_mutex_initialize(&bus->guard);
[20eaa82]63 bus->device_size = device_size;
64}
65
[1102eca]66/**
67 * Initialize the device_t structure belonging to a bus.
68 */
[6832245]69int bus_device_init(device_t *dev, bus_t *bus)
[20eaa82]70{
[6832245]71 assert(bus);
72
[20eaa82]73 memset(dev, 0, sizeof(*dev));
74
[6832245]75 dev->bus = bus;
76
[20eaa82]77 link_initialize(&dev->link);
78 list_initialize(&dev->devices);
79 fibril_mutex_initialize(&dev->guard);
80
81 return EOK;
82}
83
[1102eca]84/**
85 * Create a name of the ddf function node.
86 */
[6832245]87int bus_device_set_default_name(device_t *dev)
[20eaa82]88{
89 assert(dev);
90 assert(dev->fun);
91
[64532a97]92 char buf[12] = { 0 }; /* usbxyz-ss */
[babcc423]93 snprintf(buf, sizeof(buf), "usb%u-%cs",
[20eaa82]94 dev->address, usb_str_speed(dev->speed)[0]);
95
96 return ddf_fun_set_name(dev->fun, buf);
97}
98
[4c25c2fb]99/**
100 * Setup devices Transaction Translation.
101 *
102 * This applies for Low/Full speed devices under High speed hub only. Other
103 * devices just inherit TT from the hub.
104 *
105 * Roothub must be handled specially.
106 */
107static void device_setup_tt(device_t *dev)
108{
109 if (!dev->hub)
110 return;
111
112 if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
113 /* For LS devices under HS hub */
114 dev->tt.dev = dev->hub;
115 dev->tt.port = dev->port;
[1433ecda]116 } else {
[4c25c2fb]117 /* Inherit hub's TT */
118 dev->tt = dev->hub->tt;
119 }
120}
121
[1102eca]122/**
123 * Invoke the device_enumerate bus operation.
[0892663a]124 *
125 * There's no need to synchronize here, because no one knows the device yet.
[1102eca]126 */
[6832245]127int bus_device_enumerate(device_t *dev)
[20eaa82]128{
129 assert(dev);
130
[296d22fc]131 if (!dev->bus->ops->device_enumerate)
[20eaa82]132 return ENOTSUP;
133
[53a9d02]134 if (dev->online)
[0892663a]135 return EINVAL;
136
[4c25c2fb]137 device_setup_tt(dev);
138
[296d22fc]139 const int r = dev->bus->ops->device_enumerate(dev);
[53a9d02]140 if (r)
141 return r;
142
143 dev->online = true;
[0892663a]144
[53a9d02]145 if (dev->hub) {
[0892663a]146 fibril_mutex_lock(&dev->hub->guard);
147 list_append(&dev->link, &dev->hub->devices);
148 fibril_mutex_unlock(&dev->hub->guard);
149 }
150
[53a9d02]151 return EOK;
[0892663a]152}
153
154/**
155 * Clean endpoints and children that could have been left behind after
156 * asking the driver of device to offline/remove a device.
157 *
158 * Note that EP0's lifetime is shared with the device, and as such is not
159 * touched.
160 */
161static void device_clean_ep_children(device_t *dev, const char *op)
162{
163 assert(fibril_mutex_is_locked(&dev->guard));
164
165 /* Unregister endpoints left behind. */
166 for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
167 if (!dev->endpoints[i])
168 continue;
169
170 usb_log_warning("USB device '%s' driver left endpoint %u registered after %s.",
171 ddf_fun_get_name(dev->fun), i, op);
172
[1433ecda]173 endpoint_t *const ep = dev->endpoints[i];
[0892663a]174 endpoint_add_ref(ep);
[1bab1c8]175
[0892663a]176 fibril_mutex_unlock(&dev->guard);
[1bab1c8]177 const int err = bus_endpoint_remove(ep);
178 if (err)
179 usb_log_warning("Endpoint %u cannot be removed. "
180 "Some deffered cleanup was faster?", ep->endpoint);
181
182 endpoint_del_ref(ep);
[0892663a]183 fibril_mutex_lock(&dev->guard);
[1bab1c8]184 }
[0892663a]185
[1bab1c8]186 for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i)
[0892663a]187 assert(dev->endpoints[i] == NULL);
188
189 /* Remove also orphaned children. */
190 while (!list_empty(&dev->devices)) {
[1433ecda]191 device_t *const child = list_get_instance(list_first(&dev->devices), device_t, link);
[0892663a]192
[1bab1c8]193 /*
194 * This is not an error condition, as devices cannot remove
195 * their children devices while they are removed, because for
196 * DDF, they are siblings.
197 */
198 usb_log_debug("USB device '%s' driver left device '%s' behind after %s.",
[0892663a]199 ddf_fun_get_name(dev->fun), ddf_fun_get_name(child->fun), op);
[1bab1c8]200
[0892663a]201 /*
202 * The child node won't disappear, because its parent's driver
203 * is already dead. And the child will need the guard to remove
204 * itself from the list.
205 */
206 fibril_mutex_unlock(&dev->guard);
[9848c77]207 bus_device_gone(child);
[0892663a]208 fibril_mutex_lock(&dev->guard);
209 }
210 assert(list_empty(&dev->devices));
[20eaa82]211}
212
[1102eca]213/**
[0892663a]214 * Resolve a USB device that is gone.
[1102eca]215 */
[9848c77]216void bus_device_gone(device_t *dev)
[20eaa82]217{
218 assert(dev);
[99a00a6]219 assert(dev->fun != NULL);
[20eaa82]220
[296d22fc]221 const bus_ops_t *ops = dev->bus->ops;
[20eaa82]222
[0892663a]223 /* First, block new transfers and operations. */
224 fibril_mutex_lock(&dev->guard);
225 dev->online = false;
226
227 /* Unbinding will need guard unlocked. */
228 fibril_mutex_unlock(&dev->guard);
229
230 /* Remove our device from our hub's children. */
[53a9d02]231 if (dev->hub) {
232 fibril_mutex_lock(&dev->hub->guard);
233 list_remove(&dev->link);
234 fibril_mutex_unlock(&dev->hub->guard);
235 }
[0892663a]236
237 /*
238 * Unbind the DDF function. That will result in dev_gone called in
239 * driver, which shall destroy its pipes and remove its children.
240 */
241 const int err = ddf_fun_unbind(dev->fun);
242 if (err) {
243 usb_log_error("Failed to unbind USB device '%s': %s",
244 ddf_fun_get_name(dev->fun), str_error(err));
245 return;
246 }
247
248 /* Remove what driver left behind */
249 fibril_mutex_lock(&dev->guard);
250 device_clean_ep_children(dev, "removing");
251
252 /* Tell the HC to release its resources. */
[296d22fc]253 if (ops->device_gone)
[7278cbc9]254 ops->device_gone(dev);
255
256 /* Check whether the driver didn't forgot EP0 */
257 if (dev->endpoints[0]) {
[296d22fc]258 if (ops->endpoint_unregister)
259 ops->endpoint_unregister(dev->endpoints[0]);
[7278cbc9]260 /* Release the EP0 bus reference */
261 endpoint_del_ref(dev->endpoints[0]);
262 }
[0892663a]263
264 /* Destroy the function, freeing also the device, unlocking mutex. */
265 ddf_fun_destroy(dev->fun);
[41924f30]266}
267
[1102eca]268/**
[0892663a]269 * The user wants this device back online.
[1102eca]270 */
[6832245]271int bus_device_online(device_t *dev)
[d37514e]272{
[351113f]273 int rc;
[d37514e]274 assert(dev);
275
[0892663a]276 fibril_mutex_lock(&dev->guard);
277 if (dev->online) {
[351113f]278 rc = EINVAL;
279 goto err_lock;
[0892663a]280 }
281
282 /* First, tell the HC driver. */
[296d22fc]283 const bus_ops_t *ops = dev->bus->ops;
284 if (ops->device_online && (rc = ops->device_online(dev))) {
[351113f]285 usb_log_warning("Host controller failed to make device '%s' online: %s",
286 ddf_fun_get_name(dev->fun), str_error(rc));
287 goto err_lock;
[0892663a]288 }
289
290 /* Allow creation of new endpoints and communication with the device. */
291 dev->online = true;
[d37514e]292
[0892663a]293 /* Onlining will need the guard */
294 fibril_mutex_unlock(&dev->guard);
295
[351113f]296 if ((rc = ddf_fun_online(dev->fun))) {
[0892663a]297 usb_log_warning("Failed to take device '%s' online: %s",
[351113f]298 ddf_fun_get_name(dev->fun), str_error(rc));
299 goto err;
[0892663a]300 }
301
[351113f]302 usb_log_info("USB Device '%s' is now online.", ddf_fun_get_name(dev->fun));
[0892663a]303 return EOK;
[351113f]304
305err_lock:
306 fibril_mutex_unlock(&dev->guard);
307err:
308 return rc;
[d37514e]309}
310
[1102eca]311/**
[0892663a]312 * The user requested to take this device offline.
[1102eca]313 */
[6832245]314int bus_device_offline(device_t *dev)
[d37514e]315{
[351113f]316 int rc;
[d37514e]317 assert(dev);
318
[0892663a]319 /* Make sure we're the one who offlines this device */
[351113f]320 if (!dev->online) {
321 rc = ENOENT;
322 goto err;
323 }
[0892663a]324
325 /*
326 * XXX: If the device is removed/offlined just now, this can fail on
327 * assertion. We most probably need some kind of enum status field to
328 * make the synchronization work.
329 */
[a35b458]330
[0892663a]331 /* Tear down all drivers working with the device. */
[351113f]332 if ((rc = ddf_fun_offline(dev->fun))) {
333 goto err;
[0892663a]334 }
335
336 fibril_mutex_lock(&dev->guard);
337 dev->online = false;
338 device_clean_ep_children(dev, "offlining");
339
340 /* Tell also the HC driver. */
[296d22fc]341 const bus_ops_t *ops = dev->bus->ops;
342 if (ops->device_offline)
[0892663a]343 ops->device_offline(dev);
[d37514e]344
[0892663a]345 fibril_mutex_unlock(&dev->guard);
[351113f]346 usb_log_info("USB Device '%s' is now offline.", ddf_fun_get_name(dev->fun));
[0892663a]347 return EOK;
[351113f]348
349err:
350 return rc;
[d37514e]351}
352
[1ed3eb4]353/**
354 * Calculate an index to the endpoint array.
355 * For the default control endpoint 0, it must return 0.
356 * For different arguments, the result is stable but not defined.
357 */
[a6afb4c]358static size_t bus_endpoint_index(usb_endpoint_t ep, usb_direction_t dir)
[1ed3eb4]359{
360 return 2 * ep + (dir == USB_DIRECTION_OUT);
361}
362
[1102eca]363/**
364 * Create and register new endpoint to the bus.
365 *
366 * @param[in] device The device of which the endpoint shall be created
367 * @param[in] desc Endpoint descriptors as reported by the device
368 * @param[out] out_ep The resulting new endpoint reference, if any. Can be NULL.
369 */
[9efad54]370int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
[41924f30]371{
[6832245]372 int err;
[0206d35]373 assert(device);
[41924f30]374
[6832245]375 bus_t *bus = device->bus;
376
[296d22fc]377 if (!bus->ops->endpoint_register)
[6832245]378 return ENOTSUP;
[53db806]379
[1102eca]380 endpoint_t *ep;
[296d22fc]381 if (bus->ops->endpoint_create) {
382 ep = bus->ops->endpoint_create(device, desc);
[1102eca]383 if (!ep)
384 return ENOMEM;
385 } else {
386 ep = calloc(1, sizeof(endpoint_t));
387 if (!ep)
388 return ENOMEM;
389 endpoint_init(ep, device, desc);
390 }
[0206d35]391
[1d758fc]392 assert((ep->required_transfer_buffer_policy & ~ep->transfer_buffer_policy) == 0);
393
[9efad54]394 /* Bus reference */
[0206d35]395 endpoint_add_ref(ep);
396
[a6afb4c]397 const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction);
398 if (idx >= ARRAY_SIZE(device->endpoints)) {
399 usb_log_warning("Invalid endpoint description (ep no %u out of "
400 "bounds)", ep->endpoint);
401 goto drop;
402 }
403
[9efad54]404 if (ep->max_transfer_size == 0) {
405 usb_log_warning("Invalid endpoint description (mps %zu, "
[1433ecda]406 "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
[a6afb4c]407 goto drop;
[9efad54]408 }
409
[a1732929]410 usb_log_debug("Register endpoint %d:%d %s-%s %zuB.",
[9efad54]411 device->address, ep->endpoint,
412 usb_str_transfer_type(ep->transfer_type),
413 usb_str_direction(ep->direction),
414 ep->max_transfer_size);
415
[a6c4597]416 fibril_mutex_lock(&device->guard);
[56257ba]417 if (!device->online && ep->endpoint != 0) {
418 err = EAGAIN;
[1ed3eb4]419 } else if (device->endpoints[idx] != NULL) {
[56257ba]420 err = EEXIST;
421 } else {
[296d22fc]422 err = bus->ops->endpoint_register(ep);
[56257ba]423 if (!err)
[1ed3eb4]424 device->endpoints[idx] = ep;
[56257ba]425 }
[a6c4597]426 fibril_mutex_unlock(&device->guard);
[56257ba]427 if (err) {
428 endpoint_del_ref(ep);
429 return err;
430 }
[0206d35]431
432 if (out_ep) {
[6832245]433 /* Exporting reference */
[41924f30]434 endpoint_add_ref(ep);
[0206d35]435 *out_ep = ep;
[41924f30]436 }
[0206d35]437
[56257ba]438 return EOK;
[a6afb4c]439drop:
440 /* Bus reference */
441 endpoint_del_ref(ep);
442 return EINVAL;
[41924f30]443}
444
[1102eca]445/**
446 * Search for an endpoint. Returns a reference.
[0206d35]447 */
[a6afb4c]448endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint,
449 usb_direction_t dir)
[41924f30]450{
[6832245]451 assert(device);
452
[a6afb4c]453 const size_t idx = bus_endpoint_index(endpoint, dir);
454 const size_t ctrl_idx = bus_endpoint_index(endpoint, USB_DIRECTION_BOTH);
455
456 endpoint_t *ep = NULL;
[1ed3eb4]457
[a6c4597]458 fibril_mutex_lock(&device->guard);
[a6afb4c]459 if (idx < ARRAY_SIZE(device->endpoints))
460 ep = device->endpoints[idx];
[1ed3eb4]461 /*
462 * If the endpoint was not found, it's still possible it is a control
463 * endpoint having direction BOTH.
464 */
[a6afb4c]465 if (!ep && ctrl_idx < ARRAY_SIZE(device->endpoints)) {
[1ed3eb4]466 ep = device->endpoints[ctrl_idx];
467 if (ep && ep->transfer_type != USB_TRANSFER_CONTROL)
468 ep = NULL;
469 }
[0206d35]470 if (ep) {
471 /* Exporting reference */
472 endpoint_add_ref(ep);
473 }
[a6c4597]474 fibril_mutex_unlock(&device->guard);
[0206d35]475 return ep;
[41924f30]476}
477
[1102eca]478/**
[1bab1c8]479 * Remove an endpoint from the device.
[1102eca]480 */
[6832245]481int bus_endpoint_remove(endpoint_t *ep)
[41924f30]482{
483 assert(ep);
[9efad54]484 assert(ep->device);
[41924f30]485
[a6c4597]486 device_t *device = ep->device;
487 if (!device)
488 return ENOENT;
489
490 bus_t *bus = device->bus;
[6832245]491
[296d22fc]492 if (!bus->ops->endpoint_unregister)
[6832245]493 return ENOTSUP;
494
[a1732929]495 usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.",
[a6c4597]496 device->address, ep->endpoint,
[9efad54]497 usb_str_transfer_type(ep->transfer_type),
498 usb_str_direction(ep->direction),
499 ep->max_transfer_size);
500
[a6afb4c]501 const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction);
502
503 if (idx >= ARRAY_SIZE(device->endpoints))
504 return EINVAL;
[1ed3eb4]505
[a6c4597]506 fibril_mutex_lock(&device->guard);
[1bab1c8]507
508 /* Check whether the endpoint is registered */
509 if (device->endpoints[idx] != ep) {
510 fibril_mutex_unlock(&device->guard);
511 return EINVAL;
512 }
513
[296d22fc]514 bus->ops->endpoint_unregister(ep);
[1ed3eb4]515 device->endpoints[idx] = NULL;
[a6c4597]516 fibril_mutex_unlock(&device->guard);
[41924f30]517
518 /* Bus reference */
519 endpoint_del_ref(ep);
520
521 return EOK;
522}
523
[1102eca]524/**
[a6afb4c]525 * Reserve the default address on the bus for the specified device (hub).
[1102eca]526 */
[eeca8a6]527int bus_reserve_default_address(bus_t *bus, device_t *dev)
[41924f30]528{
529 assert(bus);
530
[eeca8a6]531 int err;
[41924f30]532 fibril_mutex_lock(&bus->guard);
[eeca8a6]533 if (bus->default_address_owner != NULL) {
534 err = (bus->default_address_owner == dev) ? EINVAL : EAGAIN;
[5e2b1ae6]535 } else {
[eeca8a6]536 bus->default_address_owner = dev;
537 err = EOK;
[5e2b1ae6]538 }
[eeca8a6]539 fibril_mutex_unlock(&bus->guard);
540 return err;
[41924f30]541}
542
[1102eca]543/**
544 * Release the default address.
545 */
[eeca8a6]546void bus_release_default_address(bus_t *bus, device_t *dev)
[41924f30]547{
548 assert(bus);
[eeca8a6]549
550 fibril_mutex_lock(&bus->guard);
551 if (bus->default_address_owner != dev) {
[4603b35]552 usb_log_error("Device %d tried to release default address, "
553 "which is not reserved for it.", dev->address);
[eeca8a6]554 } else {
555 bus->default_address_owner = NULL;
556 }
557 fibril_mutex_unlock(&bus->guard);
[41924f30]558}
559
[1102eca]560/**
[1d758fc]561 * Assert some conditions on transfer request. As the request is an entity of
562 * HC driver only, we can force these conditions harder. Invalid values from
563 * devices shall be caught on DDF interface already.
564 */
565static void check_request(const transfer_request_t *request)
566{
567 assert(usb_target_is_valid(&request->target));
568 assert(request->dir != USB_DIRECTION_BOTH);
569 /* Non-zero offset => size is non-zero */
570 assert(request->offset == 0 || request->size != 0);
571 /* Non-zero size => buffer is set */
572 assert(request->size == 0 || dma_buffer_is_set(&request->buffer));
573 /* Non-null arg => callback is set */
574 assert(request->arg == NULL || request->on_complete != NULL);
575 assert(request->name);
576}
577
578/**
579 * Initiate a transfer with given device.
[1102eca]580 *
[32fb6bce]581 * @return Error code.
582 */
[1d758fc]583int bus_issue_transfer(device_t *device, const transfer_request_t *request)
[32fb6bce]584{
[1d758fc]585 assert(device);
586 assert(request);
587
588 check_request(request);
589 assert(device->address == request->target.address);
[32fb6bce]590
591 /* Temporary reference */
[1d758fc]592 endpoint_t *ep = bus_find_endpoint(device, request->target.endpoint, request->dir);
[32fb6bce]593 if (ep == NULL) {
[a1732929]594 usb_log_error("Endpoint(%d:%d) not registered for %s.",
[1d758fc]595 device->address, request->target.endpoint, request->name);
[32fb6bce]596 return ENOENT;
597 }
598
599 assert(ep->device == device);
600
[1d758fc]601 const int err = endpoint_send_batch(ep, request);
[32fb6bce]602
603 /* Temporary reference */
604 endpoint_del_ref(ep);
605
606 return err;
607}
608
[a6afb4c]609/**
610 * A structure to pass data from the completion callback to the caller.
611 */
[32fb6bce]612typedef struct {
613 fibril_mutex_t done_mtx;
614 fibril_condvar_t done_cv;
[a6afb4c]615 bool done;
[32fb6bce]616
[db51a6a6]617 size_t transferred_size;
[32fb6bce]618 int error;
619} sync_data_t;
620
[1102eca]621/**
622 * Callback for finishing the transfer. Wake the issuing thread.
623 */
[db51a6a6]624static int sync_transfer_complete(void *arg, int error, size_t transferred_size)
[32fb6bce]625{
626 sync_data_t *d = arg;
627 assert(d);
[db51a6a6]628 d->transferred_size = transferred_size;
[32fb6bce]629 d->error = error;
630 fibril_mutex_lock(&d->done_mtx);
[a6afb4c]631 d->done = true;
[32fb6bce]632 fibril_condvar_broadcast(&d->done_cv);
633 fibril_mutex_unlock(&d->done_mtx);
634 return EOK;
635}
636
[1102eca]637/**
[a6afb4c]638 * Issue a transfer on the bus, wait for the result.
[1102eca]639 *
640 * @param device Device for which to send the batch
641 * @param target The target of the transfer.
642 * @param direction A direction of the transfer.
643 * @param data A pointer to the data buffer.
644 * @param size Size of the data buffer.
645 * @param setup_data Data to use in the setup stage (Control communication type)
646 * @param name Communication identifier (for nicer output).
647 */
[fc3dfe6d]648errno_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
[32fb6bce]649 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
[fc3dfe6d]650 const char *name, size_t *transferred_size)
[32fb6bce]651{
[1d758fc]652 int err;
[a6afb4c]653 sync_data_t sd = { .done = false };
[32fb6bce]654 fibril_mutex_initialize(&sd.done_mtx);
655 fibril_condvar_initialize(&sd.done_cv);
656
[1d758fc]657 transfer_request_t request = {
658 .target = target,
659 .dir = direction,
660 .offset = ((uintptr_t) data) % PAGE_SIZE,
661 .size = size,
662 .setup = setup_data,
663 .on_complete = sync_transfer_complete,
664 .arg = &sd,
665 .name = name,
666 };
667
668 if (data &&
669 (err = dma_buffer_lock(&request.buffer, data - request.offset, size)))
670 return err;
671
672 if ((err = bus_issue_transfer(device, &request))) {
673 dma_buffer_unlock(&request.buffer, size);
674 return err;
675 }
[32fb6bce]676
[a6afb4c]677 /*
678 * Note: There are requests that are completed synchronously. It is not
679 * therefore possible to just lock the mutex before and wait.
680 */
[32fb6bce]681 fibril_mutex_lock(&sd.done_mtx);
[a6afb4c]682 while (!sd.done)
[1102eca]683 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
[32fb6bce]684 fibril_mutex_unlock(&sd.done_mtx);
685
[1d758fc]686 dma_buffer_unlock(&request.buffer, size);
687
[fc3dfe6d]688 if (transferred_size)
689 *transferred_size = sd.transferred_size;
690
691 return sd.error;
[32fb6bce]692}
693
[41924f30]694/**
695 * @}
696 */
Note: See TracBrowser for help on using the repository browser.