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
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 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);
169 bus_device_gone(child);
170 fibril_mutex_lock(&dev->guard);
171 }
172 assert(list_empty(&dev->devices));
173}
174
175/**
176 * Resolve a USB device that is gone.
177 */
178void bus_device_gone(device_t *dev)
179{
180 assert(dev);
181 assert(dev->fun == NULL);
182
183 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_gone);
184 assert(ops);
185
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. */
214 ops->device_gone(dev);
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);
221}
222
223/**
224 * The user wants this device back online.
225 */
226int bus_device_online(device_t *dev)
227{
228 int err;
229 assert(dev);
230
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. */
238 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
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;
247
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;
259}
260
261/**
262 * The user requested to take this device offline.
263 */
264int bus_device_offline(device_t *dev)
265{
266 int err;
267 assert(dev);
268
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. */
289 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
290 if (ops)
291 ops->device_offline(dev);
292
293 fibril_mutex_unlock(&dev->guard);
294 usb_log_info("USB Device '%s' offlined.", ddf_fun_get_name(dev->fun));
295 return EOK;
296}
297
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 */
305int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
306{
307 int err;
308 assert(device);
309
310 bus_t *bus = device->bus;
311
312 const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
313 if (!register_ops)
314 return ENOTSUP;
315
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 }
328
329 /* Bus reference */
330 endpoint_add_ref(ep);
331
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
346 fibril_mutex_lock(&device->guard);
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 }
356 fibril_mutex_unlock(&device->guard);
357 if (err) {
358 endpoint_del_ref(ep);
359 return err;
360 }
361
362 if (out_ep) {
363 /* Exporting reference */
364 endpoint_add_ref(ep);
365 *out_ep = ep;
366 }
367
368 return EOK;
369}
370
371/**
372 * Search for an endpoint. Returns a reference.
373 */
374endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
375{
376 assert(device);
377
378 fibril_mutex_lock(&device->guard);
379 endpoint_t *ep = device->endpoints[endpoint];
380 if (ep) {
381 /* Exporting reference */
382 endpoint_add_ref(ep);
383 }
384 fibril_mutex_unlock(&device->guard);
385 return ep;
386}
387
388/**
389 * Remove an endpoint from the device. Consumes a reference.
390 */
391int bus_endpoint_remove(endpoint_t *ep)
392{
393 assert(ep);
394 assert(ep->device);
395
396 device_t *device = ep->device;
397 if (!device)
398 return ENOENT;
399
400 bus_t *bus = device->bus;
401
402 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
403 if (!ops)
404 return ENOTSUP;
405
406 usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
407 device->address, ep->endpoint,
408 usb_str_transfer_type(ep->transfer_type),
409 usb_str_direction(ep->direction),
410 ep->max_transfer_size);
411
412 fibril_mutex_lock(&device->guard);
413 ops->endpoint_unregister(ep);
414 device->endpoints[ep->endpoint] = NULL;
415 fibril_mutex_unlock(&device->guard);
416
417 /* Bus reference */
418 endpoint_del_ref(ep);
419
420 /* Given reference */
421 endpoint_del_ref(ep);
422
423 return EOK;
424}
425
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 */
432int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
433{
434 assert(bus);
435
436 fibril_mutex_lock(&bus->guard);
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 }
445}
446
447/**
448 * Release the default address.
449 */
450void bus_release_default_address(bus_t *bus)
451{
452 assert(bus);
453 bus->default_address_speed = USB_SPEED_MAX;
454}
455
456/**
457 * Initiate a transfer on the bus. Finds the target endpoint, creates
458 * a transfer batch and schedules it.
459 *
460 * @param device Device for which to send the batch
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
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 */
478 endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
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
505/**
506 * Callback for finishing the transfer. Wake the issuing thread.
507 */
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
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 */
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) {
548 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
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
557/**
558 * @}
559 */
Note: See TracBrowser for help on using the repository browser.