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 <macros.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <str_error.h>
|
---|
47 | #include <usb/debug.h>
|
---|
48 |
|
---|
49 | #include "endpoint.h"
|
---|
50 | #include "bus.h"
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Initializes the base bus structure.
|
---|
54 | */
|
---|
55 | void bus_init(bus_t *bus, size_t device_size)
|
---|
56 | {
|
---|
57 | assert(bus);
|
---|
58 | assert(device_size >= sizeof(device_t));
|
---|
59 | memset(bus, 0, sizeof(bus_t));
|
---|
60 |
|
---|
61 | fibril_mutex_initialize(&bus->guard);
|
---|
62 | bus->device_size = device_size;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Initialize the device_t structure belonging to a bus.
|
---|
67 | */
|
---|
68 | int 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 | */
|
---|
86 | int 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), "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 | * Setup devices Transaction Translation.
|
---|
100 | *
|
---|
101 | * This applies for Low/Full speed devices under High speed hub only. Other
|
---|
102 | * devices just inherit TT from the hub.
|
---|
103 | *
|
---|
104 | * Roothub must be handled specially.
|
---|
105 | */
|
---|
106 | static void device_setup_tt(device_t *dev)
|
---|
107 | {
|
---|
108 | if (!dev->hub)
|
---|
109 | return;
|
---|
110 |
|
---|
111 | if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
|
---|
112 | /* For LS devices under HS hub */
|
---|
113 | dev->tt.dev = dev->hub;
|
---|
114 | dev->tt.port = dev->port;
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | /* Inherit hub's TT */
|
---|
118 | dev->tt = dev->hub->tt;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Invoke the device_enumerate bus operation.
|
---|
124 | *
|
---|
125 | * There's no need to synchronize here, because no one knows the device yet.
|
---|
126 | */
|
---|
127 | int bus_device_enumerate(device_t *dev)
|
---|
128 | {
|
---|
129 | assert(dev);
|
---|
130 |
|
---|
131 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
|
---|
132 | if (!ops)
|
---|
133 | return ENOTSUP;
|
---|
134 |
|
---|
135 | if (dev->online)
|
---|
136 | return EINVAL;
|
---|
137 |
|
---|
138 | device_setup_tt(dev);
|
---|
139 |
|
---|
140 | const int r = ops->device_enumerate(dev);
|
---|
141 | if (r)
|
---|
142 | return r;
|
---|
143 |
|
---|
144 | dev->online = true;
|
---|
145 |
|
---|
146 | if (dev->hub) {
|
---|
147 | fibril_mutex_lock(&dev->hub->guard);
|
---|
148 | list_append(&dev->link, &dev->hub->devices);
|
---|
149 | fibril_mutex_unlock(&dev->hub->guard);
|
---|
150 | }
|
---|
151 |
|
---|
152 | return EOK;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Clean endpoints and children that could have been left behind after
|
---|
157 | * asking the driver of device to offline/remove a device.
|
---|
158 | *
|
---|
159 | * Note that EP0's lifetime is shared with the device, and as such is not
|
---|
160 | * touched.
|
---|
161 | */
|
---|
162 | static void device_clean_ep_children(device_t *dev, const char *op)
|
---|
163 | {
|
---|
164 | assert(fibril_mutex_is_locked(&dev->guard));
|
---|
165 |
|
---|
166 | /* Unregister endpoints left behind. */
|
---|
167 | for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
|
---|
168 | if (!dev->endpoints[i])
|
---|
169 | continue;
|
---|
170 |
|
---|
171 | usb_log_warning("USB device '%s' driver left endpoint %u registered after %s.",
|
---|
172 | ddf_fun_get_name(dev->fun), i, op);
|
---|
173 |
|
---|
174 | endpoint_t * const ep = dev->endpoints[i];
|
---|
175 | endpoint_add_ref(ep);
|
---|
176 |
|
---|
177 | fibril_mutex_unlock(&dev->guard);
|
---|
178 | bus_endpoint_remove(ep);
|
---|
179 | fibril_mutex_lock(&dev->guard);
|
---|
180 |
|
---|
181 | assert(dev->endpoints[i] == NULL);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* Remove also orphaned children. */
|
---|
185 | while (!list_empty(&dev->devices)) {
|
---|
186 | device_t * const child = list_get_instance(list_first(&dev->devices), device_t, link);
|
---|
187 |
|
---|
188 | usb_log_warning("USB device '%s' driver left device '%s' behind after %s.",
|
---|
189 | ddf_fun_get_name(dev->fun), ddf_fun_get_name(child->fun), op);
|
---|
190 | /*
|
---|
191 | * The child node won't disappear, because its parent's driver
|
---|
192 | * is already dead. And the child will need the guard to remove
|
---|
193 | * itself from the list.
|
---|
194 | */
|
---|
195 | fibril_mutex_unlock(&dev->guard);
|
---|
196 | bus_device_gone(child);
|
---|
197 | fibril_mutex_lock(&dev->guard);
|
---|
198 | }
|
---|
199 | assert(list_empty(&dev->devices));
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Resolve a USB device that is gone.
|
---|
204 | */
|
---|
205 | void bus_device_gone(device_t *dev)
|
---|
206 | {
|
---|
207 | assert(dev);
|
---|
208 | assert(dev->fun != NULL);
|
---|
209 |
|
---|
210 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_gone);
|
---|
211 | const bus_ops_t *ep_ops = BUS_OPS_LOOKUP(dev->bus->ops, endpoint_unregister);
|
---|
212 |
|
---|
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. */
|
---|
221 | if (dev->hub) {
|
---|
222 | fibril_mutex_lock(&dev->hub->guard);
|
---|
223 | list_remove(&dev->link);
|
---|
224 | fibril_mutex_unlock(&dev->hub->guard);
|
---|
225 | }
|
---|
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. */
|
---|
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 | }
|
---|
253 |
|
---|
254 | /* Destroy the function, freeing also the device, unlocking mutex. */
|
---|
255 | ddf_fun_destroy(dev->fun);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * The user wants this device back online.
|
---|
260 | */
|
---|
261 | int bus_device_online(device_t *dev)
|
---|
262 | {
|
---|
263 | int rc;
|
---|
264 | assert(dev);
|
---|
265 |
|
---|
266 | fibril_mutex_lock(&dev->guard);
|
---|
267 | if (dev->online) {
|
---|
268 | rc = EINVAL;
|
---|
269 | goto err_lock;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /* First, tell the HC driver. */
|
---|
273 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
|
---|
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;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* Allow creation of new endpoints and communication with the device. */
|
---|
281 | dev->online = true;
|
---|
282 |
|
---|
283 | /* Onlining will need the guard */
|
---|
284 | fibril_mutex_unlock(&dev->guard);
|
---|
285 |
|
---|
286 | if ((rc = ddf_fun_online(dev->fun))) {
|
---|
287 | usb_log_warning("Failed to take device '%s' online: %s",
|
---|
288 | ddf_fun_get_name(dev->fun), str_error(rc));
|
---|
289 | goto err;
|
---|
290 | }
|
---|
291 |
|
---|
292 | usb_log_info("USB Device '%s' is now online.", ddf_fun_get_name(dev->fun));
|
---|
293 | return EOK;
|
---|
294 |
|
---|
295 | err_lock:
|
---|
296 | fibril_mutex_unlock(&dev->guard);
|
---|
297 | err:
|
---|
298 | return rc;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * The user requested to take this device offline.
|
---|
303 | */
|
---|
304 | int bus_device_offline(device_t *dev)
|
---|
305 | {
|
---|
306 | int rc;
|
---|
307 | assert(dev);
|
---|
308 |
|
---|
309 | /* Make sure we're the one who offlines this device */
|
---|
310 | if (!dev->online) {
|
---|
311 | rc = ENOENT;
|
---|
312 | goto err;
|
---|
313 | }
|
---|
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. */
|
---|
322 | if ((rc = ddf_fun_offline(dev->fun))) {
|
---|
323 | goto err;
|
---|
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. */
|
---|
331 | const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
|
---|
332 | if (ops)
|
---|
333 | ops->device_offline(dev);
|
---|
334 |
|
---|
335 | fibril_mutex_unlock(&dev->guard);
|
---|
336 | usb_log_info("USB Device '%s' is now offline.", ddf_fun_get_name(dev->fun));
|
---|
337 | return EOK;
|
---|
338 |
|
---|
339 | err:
|
---|
340 | return rc;
|
---|
341 | }
|
---|
342 |
|
---|
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 | */
|
---|
348 | static size_t bus_endpoint_index(usb_endpoint_t ep, usb_direction_t dir)
|
---|
349 | {
|
---|
350 | return 2 * ep + (dir == USB_DIRECTION_OUT);
|
---|
351 | }
|
---|
352 |
|
---|
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 | */
|
---|
360 | int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
|
---|
361 | {
|
---|
362 | int err;
|
---|
363 | assert(device);
|
---|
364 |
|
---|
365 | bus_t *bus = device->bus;
|
---|
366 |
|
---|
367 | const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
|
---|
368 | if (!register_ops)
|
---|
369 | return ENOTSUP;
|
---|
370 |
|
---|
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 | }
|
---|
383 |
|
---|
384 | /* Bus reference */
|
---|
385 | endpoint_add_ref(ep);
|
---|
386 |
|
---|
387 | const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction);
|
---|
388 | if (idx >= ARRAY_SIZE(device->endpoints)) {
|
---|
389 | usb_log_warning("Invalid endpoint description (ep no %u out of "
|
---|
390 | "bounds)", ep->endpoint);
|
---|
391 | goto drop;
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (ep->max_transfer_size == 0) {
|
---|
395 | usb_log_warning("Invalid endpoint description (mps %zu, "
|
---|
396 | "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
|
---|
397 | goto drop;
|
---|
398 | }
|
---|
399 |
|
---|
400 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB.",
|
---|
401 | device->address, ep->endpoint,
|
---|
402 | usb_str_transfer_type(ep->transfer_type),
|
---|
403 | usb_str_direction(ep->direction),
|
---|
404 | ep->max_transfer_size);
|
---|
405 |
|
---|
406 | fibril_mutex_lock(&device->guard);
|
---|
407 | if (!device->online && ep->endpoint != 0) {
|
---|
408 | err = EAGAIN;
|
---|
409 | } else if (device->endpoints[idx] != NULL) {
|
---|
410 | err = EEXIST;
|
---|
411 | } else {
|
---|
412 | err = register_ops->endpoint_register(ep);
|
---|
413 | if (!err)
|
---|
414 | device->endpoints[idx] = ep;
|
---|
415 | }
|
---|
416 | fibril_mutex_unlock(&device->guard);
|
---|
417 | if (err) {
|
---|
418 | endpoint_del_ref(ep);
|
---|
419 | return err;
|
---|
420 | }
|
---|
421 |
|
---|
422 | if (out_ep) {
|
---|
423 | /* Exporting reference */
|
---|
424 | endpoint_add_ref(ep);
|
---|
425 | *out_ep = ep;
|
---|
426 | }
|
---|
427 |
|
---|
428 | return EOK;
|
---|
429 | drop:
|
---|
430 | /* Bus reference */
|
---|
431 | endpoint_del_ref(ep);
|
---|
432 | return EINVAL;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Search for an endpoint. Returns a reference.
|
---|
437 | */
|
---|
438 | endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint,
|
---|
439 | usb_direction_t dir)
|
---|
440 | {
|
---|
441 | assert(device);
|
---|
442 |
|
---|
443 | const size_t idx = bus_endpoint_index(endpoint, dir);
|
---|
444 | const size_t ctrl_idx = bus_endpoint_index(endpoint, USB_DIRECTION_BOTH);
|
---|
445 |
|
---|
446 | endpoint_t *ep = NULL;
|
---|
447 |
|
---|
448 | fibril_mutex_lock(&device->guard);
|
---|
449 | if (idx < ARRAY_SIZE(device->endpoints))
|
---|
450 | ep = device->endpoints[idx];
|
---|
451 | /*
|
---|
452 | * If the endpoint was not found, it's still possible it is a control
|
---|
453 | * endpoint having direction BOTH.
|
---|
454 | */
|
---|
455 | if (!ep && ctrl_idx < ARRAY_SIZE(device->endpoints)) {
|
---|
456 | ep = device->endpoints[ctrl_idx];
|
---|
457 | if (ep && ep->transfer_type != USB_TRANSFER_CONTROL)
|
---|
458 | ep = NULL;
|
---|
459 | }
|
---|
460 | if (ep) {
|
---|
461 | /* Exporting reference */
|
---|
462 | endpoint_add_ref(ep);
|
---|
463 | }
|
---|
464 | fibril_mutex_unlock(&device->guard);
|
---|
465 | return ep;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Remove an endpoint from the device. Consumes a reference.
|
---|
470 | */
|
---|
471 | int bus_endpoint_remove(endpoint_t *ep)
|
---|
472 | {
|
---|
473 | assert(ep);
|
---|
474 | assert(ep->device);
|
---|
475 |
|
---|
476 | device_t *device = ep->device;
|
---|
477 | if (!device)
|
---|
478 | return ENOENT;
|
---|
479 |
|
---|
480 | bus_t *bus = device->bus;
|
---|
481 |
|
---|
482 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
|
---|
483 | if (!ops)
|
---|
484 | return ENOTSUP;
|
---|
485 |
|
---|
486 | usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.",
|
---|
487 | device->address, ep->endpoint,
|
---|
488 | usb_str_transfer_type(ep->transfer_type),
|
---|
489 | usb_str_direction(ep->direction),
|
---|
490 | ep->max_transfer_size);
|
---|
491 |
|
---|
492 | const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction);
|
---|
493 |
|
---|
494 | if (idx >= ARRAY_SIZE(device->endpoints))
|
---|
495 | return EINVAL;
|
---|
496 |
|
---|
497 | fibril_mutex_lock(&device->guard);
|
---|
498 | ops->endpoint_unregister(ep);
|
---|
499 | device->endpoints[idx] = NULL;
|
---|
500 | fibril_mutex_unlock(&device->guard);
|
---|
501 |
|
---|
502 | /* Bus reference */
|
---|
503 | endpoint_del_ref(ep);
|
---|
504 |
|
---|
505 | /* Given reference */
|
---|
506 | endpoint_del_ref(ep);
|
---|
507 |
|
---|
508 | return EOK;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Reserve the default address on the bus for the specified device (hub).
|
---|
513 | */
|
---|
514 | int bus_reserve_default_address(bus_t *bus, device_t *dev)
|
---|
515 | {
|
---|
516 | assert(bus);
|
---|
517 |
|
---|
518 | int err;
|
---|
519 | fibril_mutex_lock(&bus->guard);
|
---|
520 | if (bus->default_address_owner != NULL) {
|
---|
521 | err = (bus->default_address_owner == dev) ? EINVAL : EAGAIN;
|
---|
522 | } else {
|
---|
523 | bus->default_address_owner = dev;
|
---|
524 | err = EOK;
|
---|
525 | }
|
---|
526 | fibril_mutex_unlock(&bus->guard);
|
---|
527 | return err;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Release the default address.
|
---|
532 | */
|
---|
533 | void bus_release_default_address(bus_t *bus, device_t *dev)
|
---|
534 | {
|
---|
535 | assert(bus);
|
---|
536 |
|
---|
537 | fibril_mutex_lock(&bus->guard);
|
---|
538 | if (bus->default_address_owner != dev) {
|
---|
539 | usb_log_error("Device %d tried to release default address, "
|
---|
540 | "which is not reserved for it.", dev->address);
|
---|
541 | } else {
|
---|
542 | bus->default_address_owner = NULL;
|
---|
543 | }
|
---|
544 | fibril_mutex_unlock(&bus->guard);
|
---|
545 | }
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Initiate a transfer on the bus. Finds the target endpoint, creates
|
---|
549 | * a transfer batch and schedules it.
|
---|
550 | *
|
---|
551 | * @param device Device for which to send the batch
|
---|
552 | * @param target The target of the transfer.
|
---|
553 | * @param direction A direction of the transfer.
|
---|
554 | * @param data A pointer to the data buffer.
|
---|
555 | * @param size Size of the data buffer.
|
---|
556 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
557 | * @param on_complete Callback which is called after the batch is complete
|
---|
558 | * @param arg Callback parameter.
|
---|
559 | * @param name Communication identifier (for nicer output).
|
---|
560 | * @return Error code.
|
---|
561 | */
|
---|
562 | int bus_device_send_batch(device_t *device, usb_target_t target,
|
---|
563 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
564 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
565 | {
|
---|
566 | assert(device->address == target.address);
|
---|
567 |
|
---|
568 | /* Temporary reference */
|
---|
569 | endpoint_t *ep = bus_find_endpoint(device, target.endpoint, direction);
|
---|
570 | if (ep == NULL) {
|
---|
571 | usb_log_error("Endpoint(%d:%d) not registered for %s.",
|
---|
572 | device->address, target.endpoint, name);
|
---|
573 | return ENOENT;
|
---|
574 | }
|
---|
575 |
|
---|
576 | assert(ep->device == device);
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * This method is already callable from HC only, so we can force these
|
---|
580 | * conditions harder.
|
---|
581 | * Invalid values from devices shall be caught on DDF interface already.
|
---|
582 | */
|
---|
583 | assert(usb_target_is_valid(&target));
|
---|
584 | assert(direction != USB_DIRECTION_BOTH);
|
---|
585 | assert(size == 0 || data != NULL);
|
---|
586 | assert(arg == NULL || on_complete != NULL);
|
---|
587 | assert(name);
|
---|
588 |
|
---|
589 | const int err = endpoint_send_batch(ep, target, direction,
|
---|
590 | data, size, setup_data, on_complete, arg, name);
|
---|
591 |
|
---|
592 | /* Temporary reference */
|
---|
593 | endpoint_del_ref(ep);
|
---|
594 |
|
---|
595 | return err;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * A structure to pass data from the completion callback to the caller.
|
---|
600 | */
|
---|
601 | typedef struct {
|
---|
602 | fibril_mutex_t done_mtx;
|
---|
603 | fibril_condvar_t done_cv;
|
---|
604 | bool done;
|
---|
605 |
|
---|
606 | size_t transferred_size;
|
---|
607 | int error;
|
---|
608 | } sync_data_t;
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * Callback for finishing the transfer. Wake the issuing thread.
|
---|
612 | */
|
---|
613 | static int sync_transfer_complete(void *arg, int error, size_t transferred_size)
|
---|
614 | {
|
---|
615 | sync_data_t *d = arg;
|
---|
616 | assert(d);
|
---|
617 | d->transferred_size = transferred_size;
|
---|
618 | d->error = error;
|
---|
619 | fibril_mutex_lock(&d->done_mtx);
|
---|
620 | d->done = true;
|
---|
621 | fibril_condvar_broadcast(&d->done_cv);
|
---|
622 | fibril_mutex_unlock(&d->done_mtx);
|
---|
623 | return EOK;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Issue a transfer on the bus, wait for the result.
|
---|
628 | *
|
---|
629 | * @param device Device for which to send the batch
|
---|
630 | * @param target The target of the transfer.
|
---|
631 | * @param direction A direction of the transfer.
|
---|
632 | * @param data A pointer to the data buffer.
|
---|
633 | * @param size Size of the data buffer.
|
---|
634 | * @param setup_data Data to use in the setup stage (Control communication type)
|
---|
635 | * @param name Communication identifier (for nicer output).
|
---|
636 | */
|
---|
637 | ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
|
---|
638 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
639 | const char *name)
|
---|
640 | {
|
---|
641 | sync_data_t sd = { .done = false };
|
---|
642 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
643 | fibril_condvar_initialize(&sd.done_cv);
|
---|
644 |
|
---|
645 | const int ret = bus_device_send_batch(device, target, direction,
|
---|
646 | data, size, setup_data, sync_transfer_complete, &sd, name);
|
---|
647 | if (ret != EOK)
|
---|
648 | return ret;
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Note: There are requests that are completed synchronously. It is not
|
---|
652 | * therefore possible to just lock the mutex before and wait.
|
---|
653 | */
|
---|
654 | fibril_mutex_lock(&sd.done_mtx);
|
---|
655 | while (!sd.done)
|
---|
656 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
---|
657 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
658 |
|
---|
659 | return (sd.error == EOK)
|
---|
660 | ? (ssize_t) sd.transferred_size
|
---|
661 | : (ssize_t) sd.error;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * @}
|
---|
666 | */
|
---|