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

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

usbhost: dispose the EP0 properly (+some ehci cleanup while debugging)

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