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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 53a9d02 was 53a9d02, checked in by Ondřej Hlavatý <aearsis@…>, 7 years ago

usb: fix errors introduced by recent changes

  • Property mode set to 100644
File size: 14.4 KB
RevLine 
[41924f30]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 *
[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>
[20eaa82]44#include <stdio.h>
[0892663a]45#include <str_error.h>
[64fea02]46#include <usb/debug.h>
47
48#include "endpoint.h"
49#include "bus.h"
[41924f30]50
51/**
[1102eca]52 * Initializes the base bus structure.
[41924f30]53 */
[32fb6bce]54void bus_init(bus_t *bus, size_t device_size)
[41924f30]55{
[20eaa82]56 assert(bus);
57 assert(device_size >= sizeof(device_t));
[41924f30]58 memset(bus, 0, sizeof(bus_t));
59
60 fibril_mutex_initialize(&bus->guard);
[20eaa82]61 bus->device_size = device_size;
[5e2b1ae6]62 bus->default_address_speed = USB_SPEED_MAX;
[20eaa82]63}
64
[1102eca]65/**
66 * Initialize the device_t structure belonging to a bus.
67 */
[6832245]68int bus_device_init(device_t *dev, bus_t *bus)
[20eaa82]69{
[6832245]70 assert(bus);
71
[20eaa82]72 memset(dev, 0, sizeof(*dev));
73
[6832245]74 dev->bus = bus;
75
[20eaa82]76 link_initialize(&dev->link);
77 list_initialize(&dev->devices);
78 fibril_mutex_initialize(&dev->guard);
79
80 return EOK;
81}
82
[1102eca]83/**
84 * Create a name of the ddf function node.
85 */
[6832245]86int bus_device_set_default_name(device_t *dev)
[20eaa82]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
[1102eca]98/**
99 * Invoke the device_enumerate bus operation.
[0892663a]100 *
101 * There's no need to synchronize here, because no one knows the device yet.
[1102eca]102 */
[6832245]103int bus_device_enumerate(device_t *dev)
[20eaa82]104{
105 assert(dev);
106
[6832245]107 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
108 if (!ops)
[20eaa82]109 return ENOTSUP;
110
[53a9d02]111 if (dev->online)
[0892663a]112 return EINVAL;
113
114 const int r = ops->device_enumerate(dev);
[53a9d02]115 if (r)
116 return r;
117
118 dev->online = true;
[0892663a]119
[53a9d02]120 if (dev->hub) {
[0892663a]121 fibril_mutex_lock(&dev->hub->guard);
122 list_append(&dev->link, &dev->hub->devices);
123 fibril_mutex_unlock(&dev->hub->guard);
124 }
125
[53a9d02]126 return EOK;
[0892663a]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 */
136static 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);
[9848c77]170 bus_device_gone(child);
[0892663a]171 fibril_mutex_lock(&dev->guard);
172 }
173 assert(list_empty(&dev->devices));
[20eaa82]174}
175
[1102eca]176/**
[0892663a]177 * Resolve a USB device that is gone.
[1102eca]178 */
[9848c77]179void bus_device_gone(device_t *dev)
[20eaa82]180{
181 assert(dev);
[0892663a]182 assert(dev->fun == NULL);
[20eaa82]183
[9848c77]184 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_gone);
[bad4a05]185 assert(ops);
[20eaa82]186
[0892663a]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. */
[53a9d02]195 if (dev->hub) {
196 fibril_mutex_lock(&dev->hub->guard);
197 list_remove(&dev->link);
198 fibril_mutex_unlock(&dev->hub->guard);
199 }
[0892663a]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. */
[9848c77]217 ops->device_gone(dev);
[0892663a]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);
[41924f30]224}
225
[1102eca]226/**
[0892663a]227 * The user wants this device back online.
[1102eca]228 */
[6832245]229int bus_device_online(device_t *dev)
[d37514e]230{
[0892663a]231 int err;
[d37514e]232 assert(dev);
233
[0892663a]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. */
[6832245]241 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
[0892663a]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;
[d37514e]250
[0892663a]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;
[d37514e]262}
263
[1102eca]264/**
[0892663a]265 * The user requested to take this device offline.
[1102eca]266 */
[6832245]267int bus_device_offline(device_t *dev)
[d37514e]268{
[0892663a]269 int err;
[d37514e]270 assert(dev);
271
[0892663a]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. */
[6832245]292 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
[0892663a]293 if (ops)
294 ops->device_offline(dev);
[d37514e]295
[0892663a]296 fibril_mutex_unlock(&dev->guard);
297 usb_log_info("USB Device '%s' offlined.", ddf_fun_get_name(dev->fun));
298 return EOK;
[d37514e]299}
300
[1102eca]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 */
[9efad54]308int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
[41924f30]309{
[6832245]310 int err;
[0206d35]311 assert(device);
[41924f30]312
[6832245]313 bus_t *bus = device->bus;
314
315 const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
[1102eca]316 if (!register_ops)
[6832245]317 return ENOTSUP;
[53db806]318
[1102eca]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 }
[0206d35]331
[9efad54]332 /* Bus reference */
[0206d35]333 endpoint_add_ref(ep);
334
[9efad54]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
[a6c4597]349 fibril_mutex_lock(&device->guard);
[56257ba]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 }
[a6c4597]359 fibril_mutex_unlock(&device->guard);
[56257ba]360 if (err) {
361 endpoint_del_ref(ep);
362 return err;
363 }
[0206d35]364
365 if (out_ep) {
[6832245]366 /* Exporting reference */
[41924f30]367 endpoint_add_ref(ep);
[0206d35]368 *out_ep = ep;
[41924f30]369 }
[0206d35]370
[56257ba]371 return EOK;
[41924f30]372}
373
[1102eca]374/**
375 * Search for an endpoint. Returns a reference.
[0206d35]376 */
[56257ba]377endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
[41924f30]378{
[6832245]379 assert(device);
380
[a6c4597]381 fibril_mutex_lock(&device->guard);
[56257ba]382 endpoint_t *ep = device->endpoints[endpoint];
[0206d35]383 if (ep) {
384 /* Exporting reference */
385 endpoint_add_ref(ep);
386 }
[a6c4597]387 fibril_mutex_unlock(&device->guard);
[0206d35]388 return ep;
[41924f30]389}
390
[1102eca]391/**
392 * Remove an endpoint from the device. Consumes a reference.
393 */
[6832245]394int bus_endpoint_remove(endpoint_t *ep)
[41924f30]395{
396 assert(ep);
[9efad54]397 assert(ep->device);
[41924f30]398
[a6c4597]399 device_t *device = ep->device;
400 if (!device)
401 return ENOENT;
402
403 bus_t *bus = device->bus;
[6832245]404
[9efad54]405 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
[6832245]406 if (!ops)
407 return ENOTSUP;
408
[9efad54]409 usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
[a6c4597]410 device->address, ep->endpoint,
[9efad54]411 usb_str_transfer_type(ep->transfer_type),
412 usb_str_direction(ep->direction),
413 ep->max_transfer_size);
414
[a6c4597]415 fibril_mutex_lock(&device->guard);
[bad4a05]416 ops->endpoint_unregister(ep);
417 device->endpoints[ep->endpoint] = NULL;
[a6c4597]418 fibril_mutex_unlock(&device->guard);
[41924f30]419
420 /* Bus reference */
421 endpoint_del_ref(ep);
422
[1102eca]423 /* Given reference */
424 endpoint_del_ref(ep);
425
[41924f30]426 return EOK;
427}
428
[1102eca]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 */
[10cd715]435int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
[41924f30]436{
437 assert(bus);
438
439 fibril_mutex_lock(&bus->guard);
[5e2b1ae6]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 }
[41924f30]448}
449
[1102eca]450/**
451 * Release the default address.
452 */
[5e2b1ae6]453void bus_release_default_address(bus_t *bus)
[41924f30]454{
455 assert(bus);
[5e2b1ae6]456 bus->default_address_speed = USB_SPEED_MAX;
[41924f30]457}
458
[1102eca]459/**
460 * Initiate a transfer on the bus. Finds the target endpoint, creates
461 * a transfer batch and schedules it.
462 *
[32fb6bce]463 * @param device Device for which to send the batch
[1102eca]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
[32fb6bce]470 * @param arg Callback parameter.
471 * @param name Communication identifier (for nicer output).
472 * @return Error code.
473 */
474int 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 */
[56257ba]481 endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
[32fb6bce]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
499typedef 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
[1102eca]508/**
509 * Callback for finishing the transfer. Wake the issuing thread.
510 */
[32fb6bce]511static 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
[1102eca]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 */
[32fb6bce]535ssize_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) {
[1102eca]551 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
[32fb6bce]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
[41924f30]560/**
561 * @}
562 */
Note: See TracBrowser for help on using the repository browser.