source: mainline/uspace/drv/bus/usb/usbhub/usbhub.c@ 51c1d500

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

usbhub: be aware of its own speed

This resulted in a bunch of changes just because the roothubs in older
HC's are virtual, and need to be aware of their own speed too.

  • Property mode set to 100644
File size: 20.3 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup drvusbhub
31 * @{
32 */
33/** @file
34 * @brief usb hub main functionality
35 */
36
37#include <ddf/driver.h>
38#include <stdbool.h>
39#include <errno.h>
40#include <str_error.h>
41#include <inttypes.h>
42#include <stdio.h>
43
44#include <usb/usb.h>
45#include <usb/debug.h>
46#include <usb/dev/pipes.h>
47#include <usb/classes/classes.h>
48#include <usb/descriptor.h>
49#include <usb/dev/recognise.h>
50#include <usb/dev/request.h>
51#include <usb/classes/hub.h>
52#include <usb/dev/poll.h>
53#include <usbhc_iface.h>
54
55#include "usbhub.h"
56#include "status.h"
57
58#define HUB_FNC_NAME "hub"
59
60/** Hub status-change endpoint description.
61 *
62 * For more information see section 11.15.1 of USB 1.1 specification.
63 */
64const usb_endpoint_description_t hub_status_change_endpoint_description =
65{
66 .transfer_type = USB_TRANSFER_INTERRUPT,
67 .direction = USB_DIRECTION_IN,
68 .interface_class = USB_CLASS_HUB,
69 .interface_subclass = 0,
70 .interface_protocol = 0,
71 .flags = 0
72};
73
74/** Standard get hub global status request */
75static const usb_device_request_setup_packet_t get_hub_status_request = {
76 .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
77 .request = USB_HUB_REQUEST_GET_STATUS,
78 .index = 0,
79 .value = 0,
80 .length = sizeof(usb_hub_status_t),
81};
82
83static int usb_set_first_configuration(usb_device_t *usb_device);
84static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
85static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
86 usb_hub_status_t status);
87static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
88
89static bool usb_hub_polling_error_callback(usb_device_t *dev, int err_code, void *arg)
90{
91 assert(dev);
92 assert(arg);
93
94 usb_log_error("Device %s polling error: %s", usb_device_get_name(dev), str_error(err_code));
95
96 return true;
97}
98
99/**
100 * Initialize hub device driver structure.
101 *
102 * Creates hub representation and fibril that periodically checks hub's status.
103 * Hub representation is passed to the fibril.
104 * @param usb_dev generic usb device information
105 * @return error code
106 */
107int usb_hub_device_add(usb_device_t *usb_dev)
108{
109 assert(usb_dev);
110 /* Create driver soft-state structure */
111 usb_hub_dev_t *hub_dev =
112 usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
113 if (hub_dev == NULL) {
114 usb_log_error("Failed to create hub driver structure.");
115 return ENOMEM;
116 }
117 hub_dev->usb_device = usb_dev;
118 hub_dev->speed = usb_device_get_speed(usb_dev);
119
120 fibril_mutex_initialize(&hub_dev->default_address_guard);
121 fibril_condvar_initialize(&hub_dev->default_address_cv);
122
123 /* Set hub's first configuration. (There should be only one) */
124 int opResult = usb_set_first_configuration(usb_dev);
125 if (opResult != EOK) {
126 usb_log_error("Could not set hub configuration: %s",
127 str_error(opResult));
128 return opResult;
129 }
130
131 /* Get port count and create attached_devices. */
132 opResult = usb_hub_process_hub_specific_info(hub_dev);
133 if (opResult != EOK) {
134 usb_log_error("Could process hub specific info, %s",
135 str_error(opResult));
136 return opResult;
137 }
138
139 /* Create hub control function. */
140 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
141 hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
142 fun_exposed, HUB_FNC_NAME);
143 if (hub_dev->hub_fun == NULL) {
144 usb_log_error("Failed to create hub function.");
145 return ENOMEM;
146 }
147
148 /* Bind hub control function. */
149 opResult = ddf_fun_bind(hub_dev->hub_fun);
150 if (opResult != EOK) {
151 usb_log_error("Failed to bind hub function: %s.",
152 str_error(opResult));
153 ddf_fun_destroy(hub_dev->hub_fun);
154 return opResult;
155 }
156
157 /* Start hub operation. */
158 usb_polling_t *polling = &hub_dev->polling;
159 opResult = usb_polling_init(polling);
160 if (opResult != EOK) {
161 /* Function is already bound */
162 ddf_fun_unbind(hub_dev->hub_fun);
163 ddf_fun_destroy(hub_dev->hub_fun);
164 usb_log_error("Failed to initialize polling fibril: %s.",
165 str_error(opResult));
166 return opResult;
167 }
168
169 polling->device = hub_dev->usb_device;
170 polling->ep_mapping = usb_device_get_mapped_ep_desc(hub_dev->usb_device,
171 &hub_status_change_endpoint_description);
172 polling->request_size = ((hub_dev->port_count + 1 + 7) / 8);
173 polling->buffer = malloc(polling->request_size);
174 polling->on_data = hub_port_changes_callback;
175 polling->on_error = usb_hub_polling_error_callback;
176 polling->arg = hub_dev;
177
178 opResult = usb_polling_start(polling);
179 if (opResult != EOK) {
180 /* Polling is already initialized. */
181 free(polling->buffer);
182 usb_polling_fini(polling);
183 ddf_fun_unbind(hub_dev->hub_fun);
184 ddf_fun_destroy(hub_dev->hub_fun);
185 usb_log_error("Failed to create polling fibril: %s.",
186 str_error(opResult));
187 return opResult;
188 }
189
190 usb_log_info("Controlling %s-speed hub '%s' (%p: %zu ports).",
191 usb_str_speed(hub_dev->speed),
192 usb_device_get_name(hub_dev->usb_device), hub_dev,
193 hub_dev->port_count);
194
195 return EOK;
196}
197
198static int usb_hub_cleanup(usb_hub_dev_t *hub)
199{
200 free(hub->polling.buffer);
201 usb_polling_fini(&hub->polling);
202
203 for (size_t port = 0; port < hub->port_count; ++port) {
204 usb_port_fini(&hub->ports[port].base);
205 }
206 free(hub->ports);
207
208 const int ret = ddf_fun_unbind(hub->hub_fun);
209 if (ret != EOK) {
210 usb_log_error("(%p) Failed to unbind '%s' function: %s.",
211 hub, HUB_FNC_NAME, str_error(ret));
212 return ret;
213 }
214 ddf_fun_destroy(hub->hub_fun);
215
216 usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
217
218 /* Device data (usb_hub_dev_t) will be freed by usbdev. */
219 return EOK;
220}
221
222/**
223 * Turn off power to all ports.
224 *
225 * @param usb_dev generic usb device information
226 * @return error code
227 */
228int usb_hub_device_remove(usb_device_t *usb_dev)
229{
230 assert(usb_dev);
231 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
232 assert(hub);
233
234 usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
235
236 /* Join polling fibril (ignoring error code). */
237 usb_polling_join(&hub->polling);
238 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
239
240 /* Destroy hub. */
241 return usb_hub_cleanup(hub);
242}
243
244/**
245 * Remove all attached devices
246 * @param usb_dev generic usb device information
247 * @return error code
248 */
249int usb_hub_device_gone(usb_device_t *usb_dev)
250{
251 assert(usb_dev);
252 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
253 assert(hub);
254
255 usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
256
257 /* Join polling fibril (ignoring error code). */
258 usb_polling_join(&hub->polling);
259 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
260
261 /* Destroy hub. */
262 return usb_hub_cleanup(hub);
263}
264
265/** Callback for polling hub for changes.
266 *
267 * @param dev Device where the change occured.
268 * @param change_bitmap Bitmap of changed ports.
269 * @param change_bitmap_size Size of the bitmap in bytes.
270 * @param arg Custom argument, points to @c usb_hub_dev_t.
271 * @return Whether to continue polling.
272 */
273bool hub_port_changes_callback(usb_device_t *dev,
274 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
275{
276 usb_hub_dev_t *hub = arg;
277 assert(hub);
278
279 /* It is an error condition if we didn't receive enough data */
280 if (change_bitmap_size == 0) {
281 return false;
282 }
283
284 /* Lowest bit indicates global change */
285 const bool change = change_bitmap[0] & 1;
286 if (change) {
287 usb_hub_global_interrupt(hub);
288 }
289
290 /* N + 1 bit indicates change on port N */
291 for (size_t port = 0; port < hub->port_count; ++port) {
292 const size_t bit = port + 1;
293 const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
294 if (change) {
295 usb_hub_port_process_interrupt(&hub->ports[port]);
296 }
297 }
298 return true;
299}
300
301/**
302 * Load hub-specific information into hub_dev structure and process if needed
303 *
304 * Read port count and initialize structures holding per port information.
305 * If there are any non-removable devices, start initializing them.
306 * This function is hub-specific and should be run only after the hub is
307 * configured using usb_set_first_configuration function.
308 * @param hub_dev hub representation
309 * @return error code
310 */
311static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
312{
313 assert(hub_dev);
314
315 /* Get hub descriptor. */
316 usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
317 usb_pipe_t *control_pipe =
318 usb_device_get_default_pipe(hub_dev->usb_device);
319
320 usb_hub_descriptor_header_t descriptor;
321 size_t received_size;
322 int opResult = usb_request_get_descriptor(control_pipe,
323 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
324 USB_DESCTYPE_HUB, 0, 0, &descriptor,
325 sizeof(usb_hub_descriptor_header_t), &received_size);
326 if (opResult != EOK) {
327 usb_log_error("(%p): Failed to receive hub descriptor: %s.",
328 hub_dev, str_error(opResult));
329 return opResult;
330 }
331
332 usb_log_debug("(%p): Setting port count to %d.", hub_dev,
333 descriptor.port_count);
334 hub_dev->port_count = descriptor.port_count;
335 hub_dev->control_pipe = control_pipe;
336
337 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
338 if (!hub_dev->ports) {
339 return ENOMEM;
340 }
341
342 for (size_t port = 0; port < hub_dev->port_count; ++port) {
343 usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
344 }
345
346 hub_dev->power_switched =
347 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
348 hub_dev->per_port_power =
349 descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
350
351 if (!hub_dev->power_switched) {
352 usb_log_info("(%p): Power switching not supported, "
353 "ports always powered.", hub_dev);
354 return EOK;
355 }
356
357 usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
358 hub_dev->per_port_power ? "per port" : "ganged");
359
360 for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
361 usb_log_debug("(%p): Powering port %u.", hub_dev, port);
362 const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
363
364 if (ret != EOK) {
365 usb_log_error("(%p-%u): Cannot power on port: %s.",
366 hub_dev, hub_dev->ports[port].port_number,
367 str_error(ret));
368 } else {
369 if (!hub_dev->per_port_power) {
370 usb_log_debug("(%p) Ganged power switching, "
371 "one port is enough.", hub_dev);
372 break;
373 }
374 }
375 }
376 return EOK;
377}
378
379/**
380 * Set configuration of and USB device
381 *
382 * Check whether there is at least one configuration and sets the first one.
383 * This function should be run prior to running any hub-specific action.
384 * @param usb_device usb device representation
385 * @return error code
386 */
387static int usb_set_first_configuration(usb_device_t *usb_device)
388{
389 assert(usb_device);
390 /* Get number of possible configurations from device descriptor */
391 const size_t configuration_count =
392 usb_device_descriptors(usb_device)->device.configuration_count;
393 usb_log_debug("Hub has %zu configurations.", configuration_count);
394
395 if (configuration_count < 1) {
396 usb_log_error("There are no configurations available");
397 return EINVAL;
398 }
399
400 const size_t config_size =
401 usb_device_descriptors(usb_device)->full_config_size;
402 const usb_standard_configuration_descriptor_t *config_descriptor =
403 usb_device_descriptors(usb_device)->full_config;
404
405 if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
406 usb_log_error("Configuration descriptor is not big enough"
407 " to fit standard configuration descriptor.\n");
408 return EOVERFLOW;
409 }
410
411 /* Set configuration. Use the configuration that was in
412 * usb_device->descriptors.configuration i.e. The first one. */
413 const int opResult = usb_request_set_configuration(
414 usb_device_get_default_pipe(usb_device),
415 config_descriptor->configuration_number);
416 if (opResult != EOK) {
417 usb_log_error("Failed to set hub configuration: %s.",
418 str_error(opResult));
419 } else {
420 usb_log_debug("\tUsed configuration %d",
421 config_descriptor->configuration_number);
422 }
423 return opResult;
424}
425
426/**
427 * Process hub over current change
428 *
429 * This means either to power off the hub or power it on.
430 * @param hub_dev hub instance
431 * @param status hub status bitmask
432 * @return error code
433 */
434static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
435 usb_hub_status_t status)
436{
437 if (status & USB_HUB_STATUS_OVER_CURRENT) {
438 /* Hub should remove power from all ports if it detects OC */
439 usb_log_warning("(%p) Detected hub over-current condition, "
440 "all ports should be powered off.", hub_dev);
441 return;
442 }
443
444 /* Ports are always powered. */
445 if (!hub_dev->power_switched)
446 return;
447
448 /* Over-current condition is gone, it is safe to turn the ports on. */
449 for (size_t port = 0; port < hub_dev->port_count; ++port) {
450 const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
451 if (ret != EOK) {
452 usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
453 " power on port: %s\n", hub_dev,
454 hub_dev->ports[port].port_number, str_error(ret));
455 } else {
456 if (!hub_dev->per_port_power)
457 return;
458 }
459 }
460}
461
462/**
463 * Set feature on the real hub port.
464 *
465 * @param port Port structure.
466 * @param feature Feature selector.
467 */
468int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
469{
470 assert(hub);
471 const usb_device_request_setup_packet_t clear_request = {
472 .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
473 .request = USB_DEVREQ_SET_FEATURE,
474 .index = uint16_host2usb(port_number),
475 .value = feature,
476 .length = 0,
477 };
478 return usb_pipe_control_write(hub->control_pipe, &clear_request,
479 sizeof(clear_request), NULL, 0);
480}
481
482/**
483 * Clear feature on the real hub port.
484 *
485 * @param port Port structure.
486 * @param feature Feature selector.
487 */
488int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
489{
490 assert(hub);
491 const usb_device_request_setup_packet_t clear_request = {
492 .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
493 .request = USB_DEVREQ_CLEAR_FEATURE,
494 .value = feature,
495 .index = uint16_host2usb(port_number),
496 .length = 0,
497 };
498 return usb_pipe_control_write(hub->control_pipe,
499 &clear_request, sizeof(clear_request), NULL, 0);
500}
501
502/**
503 * Retrieve port status.
504 *
505 * @param[in] port Port structure
506 * @param[out] status Where to store the port status.
507 * @return Error code.
508 */
509int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
510{
511 assert(hub);
512 assert(status);
513
514 /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
515 * Generic GET_STATUS request cannot be used because of the difference
516 * in status data size (2B vs. 4B)*/
517 const usb_device_request_setup_packet_t request = {
518 .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
519 .request = USB_HUB_REQUEST_GET_STATUS,
520 .value = 0,
521 .index = uint16_host2usb(port_number),
522 .length = sizeof(usb_port_status_t),
523 };
524 size_t recv_size;
525
526 const int rc = usb_pipe_control_read(hub->control_pipe,
527 &request, sizeof(usb_device_request_setup_packet_t),
528 status, sizeof(*status), &recv_size);
529 if (rc != EOK)
530 return rc;
531
532 if (recv_size != sizeof(*status))
533 return ELIMIT;
534
535 return EOK;
536}
537
538/**
539 * Process hub interrupts.
540 *
541 * The change can be either in the over-current condition or local-power change.
542 * @param hub_dev hub instance
543 */
544static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
545{
546 assert(hub_dev);
547 assert(hub_dev->usb_device);
548 usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
549 usb_pipe_t *control_pipe =
550 usb_device_get_default_pipe(hub_dev->usb_device);
551
552 usb_hub_status_t status;
553 size_t rcvd_size;
554 /* NOTE: We can't use standard USB GET_STATUS request, because
555 * hubs reply is 4byte instead of 2 */
556 const int opResult = usb_pipe_control_read(control_pipe,
557 &get_hub_status_request, sizeof(get_hub_status_request),
558 &status, sizeof(usb_hub_status_t), &rcvd_size);
559 if (opResult != EOK) {
560 usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
561 str_error(opResult));
562 return;
563 }
564 if (rcvd_size != sizeof(usb_hub_status_t)) {
565 usb_log_error("(%p): Received status has incorrect size: "
566 "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
567 return;
568 }
569
570 /* Handle status changes */
571 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
572 usb_hub_over_current(hub_dev, status);
573 /* Ack change in hub OC flag */
574 const int ret = usb_request_clear_feature(
575 control_pipe, USB_REQUEST_TYPE_CLASS,
576 USB_REQUEST_RECIPIENT_DEVICE,
577 USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
578 if (ret != EOK) {
579 usb_log_error("(%p): Failed to clear hub over-current "
580 "change flag: %s.\n", hub_dev, str_error(opResult));
581 }
582 }
583
584 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
585 /* NOTE: Handling this is more complicated.
586 * If the transition is from bus power to local power, all
587 * is good and we may signal the parent hub that we don't
588 * need the power.
589 * If the transition is from local power to bus power
590 * the hub should turn off all the ports and devices need
591 * to be reinitialized taking into account the limited power
592 * that is now available.
593 * There is no support for power distribution in HelenOS,
594 * (or other OSes/hub devices that I've seen) so this is not
595 * implemented.
596 * Just ACK the change.
597 */
598 const int ret = usb_request_clear_feature(
599 control_pipe, USB_REQUEST_TYPE_CLASS,
600 USB_REQUEST_RECIPIENT_DEVICE,
601 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
602 if (opResult != EOK) {
603 usb_log_error("(%p): Failed to clear hub power change "
604 "flag: %s.\n", hub_dev, str_error(ret));
605 }
606 }
607}
608
609static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
610
611/**
612 * Reserve a default address for a port across all other devices connected to
613 * the bus. We aggregate requests for ports to minimize delays between
614 * connecting multiple devices from one hub - which happens e.g. when the hub
615 * is connected with already attached devices.
616 */
617int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
618{
619 assert(hub);
620 assert(exch);
621 assert(port);
622 assert(fibril_mutex_is_locked(&port->guard));
623
624 fibril_mutex_lock(&hub->default_address_guard);
625 if (hub->default_address_requests++ == 0) {
626 /* We're the first to request the address, we can just do it */
627 fibril_mutex_unlock(&hub->default_address_guard);
628 int err;
629 while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
630 // We ignore the return value here, as we cannot give up now.
631 usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
632 }
633 return err;
634 } else {
635 /* Drop the port guard, we're going to wait */
636 fibril_mutex_unlock(&port->guard);
637
638 /* Wait for a signal */
639 fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
640
641 /* Remember ABBA, first drop the hub guard */
642 fibril_mutex_unlock(&hub->default_address_guard);
643 fibril_mutex_lock(&port->guard);
644 return EOK;
645 }
646}
647
648/**
649 * Release the default address from a port.
650 */
651int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
652{
653 int ret = EOK;
654
655 fibril_mutex_lock(&hub->default_address_guard);
656 if (--hub->default_address_requests == 0) {
657 // We must do it in critical section to prevent other fibril
658 // from requesting the address before we release
659 ret = usbhc_release_default_address(exch);
660 // This is optimistic optimization - it may wake one hub from polling sleep
661 fibril_condvar_signal(&global_hub_default_address_cv);
662 } else {
663 fibril_condvar_signal(&hub->default_address_cv);
664 }
665 fibril_mutex_unlock(&hub->default_address_guard);
666
667 return ret;
668}
669
670/**
671 * @}
672 */
Note: See TracBrowser for help on using the repository browser.