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

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

usbhost: renamed bus_device_remove to bus_device_gone

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