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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 351113f was 351113f, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbhost: fix return in critical section, change misleading log messages

  • Property mode set to 100644
File size: 14.4 KB
Line 
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 */
54void 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 */
68int 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 */
86int 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 */
103int 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 */
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);
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 */
179void 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 */
229int bus_device_online(device_t *dev)
230{
231 int rc;
232 assert(dev);
233
234 fibril_mutex_lock(&dev->guard);
235 if (dev->online) {
236 rc = EINVAL;
237 goto err_lock;
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 && (rc = ops->device_online(dev))) {
243 usb_log_warning("Host controller failed to make device '%s' online: %s",
244 ddf_fun_get_name(dev->fun), str_error(rc));
245 goto err_lock;
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 ((rc = 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(rc));
257 goto err;
258 }
259
260 usb_log_info("USB Device '%s' is now online.", ddf_fun_get_name(dev->fun));
261 return EOK;
262
263err_lock:
264 fibril_mutex_unlock(&dev->guard);
265err:
266 return rc;
267}
268
269/**
270 * The user requested to take this device offline.
271 */
272int bus_device_offline(device_t *dev)
273{
274 int rc;
275 assert(dev);
276
277 /* Make sure we're the one who offlines this device */
278 if (!dev->online) {
279 rc = ENOENT;
280 goto err;
281 }
282
283 /*
284 * XXX: If the device is removed/offlined just now, this can fail on
285 * assertion. We most probably need some kind of enum status field to
286 * make the synchronization work.
287 */
288
289 /* Tear down all drivers working with the device. */
290 if ((rc = ddf_fun_offline(dev->fun))) {
291 goto err;
292 }
293
294 fibril_mutex_lock(&dev->guard);
295 dev->online = false;
296 device_clean_ep_children(dev, "offlining");
297
298 /* Tell also the HC driver. */
299 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
300 if (ops)
301 ops->device_offline(dev);
302
303 fibril_mutex_unlock(&dev->guard);
304 usb_log_info("USB Device '%s' is now offline.", ddf_fun_get_name(dev->fun));
305 return EOK;
306
307err:
308 return rc;
309}
310
311/**
312 * Create and register new endpoint to the bus.
313 *
314 * @param[in] device The device of which the endpoint shall be created
315 * @param[in] desc Endpoint descriptors as reported by the device
316 * @param[out] out_ep The resulting new endpoint reference, if any. Can be NULL.
317 */
318int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
319{
320 int err;
321 assert(device);
322
323 bus_t *bus = device->bus;
324
325 const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
326 if (!register_ops)
327 return ENOTSUP;
328
329 const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
330 endpoint_t *ep;
331 if (create_ops) {
332 ep = create_ops->endpoint_create(device, desc);
333 if (!ep)
334 return ENOMEM;
335 } else {
336 ep = calloc(1, sizeof(endpoint_t));
337 if (!ep)
338 return ENOMEM;
339 endpoint_init(ep, device, desc);
340 }
341
342 /* Bus reference */
343 endpoint_add_ref(ep);
344
345 if (ep->max_transfer_size == 0) {
346 usb_log_warning("Invalid endpoint description (mps %zu, "
347 "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
348 /* Bus reference */
349 endpoint_del_ref(ep);
350 return EINVAL;
351 }
352
353 usb_log_debug("Register endpoint %d:%d %s-%s %zuB.\n",
354 device->address, ep->endpoint,
355 usb_str_transfer_type(ep->transfer_type),
356 usb_str_direction(ep->direction),
357 ep->max_transfer_size);
358
359 fibril_mutex_lock(&device->guard);
360 if (!device->online && ep->endpoint != 0) {
361 err = EAGAIN;
362 } else if (device->endpoints[ep->endpoint] != NULL) {
363 err = EEXIST;
364 } else {
365 err = register_ops->endpoint_register(ep);
366 if (!err)
367 device->endpoints[ep->endpoint] = ep;
368 }
369 fibril_mutex_unlock(&device->guard);
370 if (err) {
371 endpoint_del_ref(ep);
372 return err;
373 }
374
375 if (out_ep) {
376 /* Exporting reference */
377 endpoint_add_ref(ep);
378 *out_ep = ep;
379 }
380
381 return EOK;
382}
383
384/**
385 * Search for an endpoint. Returns a reference.
386 */
387endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
388{
389 assert(device);
390
391 fibril_mutex_lock(&device->guard);
392 endpoint_t *ep = device->endpoints[endpoint];
393 if (ep) {
394 /* Exporting reference */
395 endpoint_add_ref(ep);
396 }
397 fibril_mutex_unlock(&device->guard);
398 return ep;
399}
400
401/**
402 * Remove an endpoint from the device. Consumes a reference.
403 */
404int bus_endpoint_remove(endpoint_t *ep)
405{
406 assert(ep);
407 assert(ep->device);
408
409 device_t *device = ep->device;
410 if (!device)
411 return ENOENT;
412
413 bus_t *bus = device->bus;
414
415 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
416 if (!ops)
417 return ENOTSUP;
418
419 usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
420 device->address, ep->endpoint,
421 usb_str_transfer_type(ep->transfer_type),
422 usb_str_direction(ep->direction),
423 ep->max_transfer_size);
424
425 fibril_mutex_lock(&device->guard);
426 ops->endpoint_unregister(ep);
427 device->endpoints[ep->endpoint] = NULL;
428 fibril_mutex_unlock(&device->guard);
429
430 /* Bus reference */
431 endpoint_del_ref(ep);
432
433 /* Given reference */
434 endpoint_del_ref(ep);
435
436 return EOK;
437}
438
439/**
440 * Reserve the default address on the bus. Also, report the speed of the device
441 * that is listening on the default address.
442 *
443 * The speed is then used for devices enumerated while the address is reserved.
444 */
445int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
446{
447 assert(bus);
448
449 fibril_mutex_lock(&bus->guard);
450 if (bus->default_address_speed != USB_SPEED_MAX) {
451 fibril_mutex_unlock(&bus->guard);
452 return EAGAIN;
453 } else {
454 bus->default_address_speed = speed;
455 fibril_mutex_unlock(&bus->guard);
456 return EOK;
457 }
458}
459
460/**
461 * Release the default address.
462 */
463void bus_release_default_address(bus_t *bus)
464{
465 assert(bus);
466 bus->default_address_speed = USB_SPEED_MAX;
467}
468
469/**
470 * Initiate a transfer on the bus. Finds the target endpoint, creates
471 * a transfer batch and schedules it.
472 *
473 * @param device Device for which to send the batch
474 * @param target The target of the transfer.
475 * @param direction A direction of the transfer.
476 * @param data A pointer to the data buffer.
477 * @param size Size of the data buffer.
478 * @param setup_data Data to use in the setup stage (Control communication type)
479 * @param on_complete Callback which is called after the batch is complete
480 * @param arg Callback parameter.
481 * @param name Communication identifier (for nicer output).
482 * @return Error code.
483 */
484int bus_device_send_batch(device_t *device, usb_target_t target,
485 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
486 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
487{
488 assert(device->address == target.address);
489
490 /* Temporary reference */
491 endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
492 if (ep == NULL) {
493 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
494 device->address, target.endpoint, name);
495 return ENOENT;
496 }
497
498 assert(ep->device == device);
499
500 const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
501 on_complete, arg, name);
502
503 /* Temporary reference */
504 endpoint_del_ref(ep);
505
506 return err;
507}
508
509typedef struct {
510 fibril_mutex_t done_mtx;
511 fibril_condvar_t done_cv;
512 unsigned done;
513
514 size_t transfered_size;
515 int error;
516} sync_data_t;
517
518/**
519 * Callback for finishing the transfer. Wake the issuing thread.
520 */
521static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
522{
523 sync_data_t *d = arg;
524 assert(d);
525 d->transfered_size = transfered_size;
526 d->error = error;
527 fibril_mutex_lock(&d->done_mtx);
528 d->done = 1;
529 fibril_condvar_broadcast(&d->done_cv);
530 fibril_mutex_unlock(&d->done_mtx);
531 return EOK;
532}
533
534/**
535 * Issue a transfer on the bus, wait for result.
536 *
537 * @param device Device for which to send the batch
538 * @param target The target of the transfer.
539 * @param direction A direction of the transfer.
540 * @param data A pointer to the data buffer.
541 * @param size Size of the data buffer.
542 * @param setup_data Data to use in the setup stage (Control communication type)
543 * @param name Communication identifier (for nicer output).
544 */
545ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
546 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
547 const char *name)
548{
549 sync_data_t sd = { .done = 0 };
550 fibril_mutex_initialize(&sd.done_mtx);
551 fibril_condvar_initialize(&sd.done_cv);
552
553 const int ret = bus_device_send_batch(device, target, direction,
554 data, size, setup_data,
555 sync_transfer_complete, &sd, name);
556 if (ret != EOK)
557 return ret;
558
559 fibril_mutex_lock(&sd.done_mtx);
560 while (!sd.done) {
561 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
562 }
563 fibril_mutex_unlock(&sd.done_mtx);
564
565 return (sd.error == EOK)
566 ? (ssize_t) sd.transfered_size
567 : (ssize_t) sd.error;
568}
569
570/**
571 * @}
572 */
Note: See TracBrowser for help on using the repository browser.