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