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

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

usbhub: fix hub depth off-by-one

  • Property mode set to 100644
File size: 21.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_descriptor_type_t desc_type = hub_dev->speed >= USB_SPEED_SUPER
321 ? USB_DESCTYPE_SSPEED_HUB : USB_DESCTYPE_HUB;
322
323 usb_hub_descriptor_header_t descriptor;
324 size_t received_size;
325 int opResult = usb_request_get_descriptor(control_pipe,
326 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
327 desc_type, 0, 0, &descriptor,
328 sizeof(usb_hub_descriptor_header_t), &received_size);
329 if (opResult != EOK) {
330 usb_log_error("(%p): Failed to receive hub descriptor: %s.",
331 hub_dev, str_error(opResult));
332 return opResult;
333 }
334
335 usb_log_debug("(%p): Setting port count to %d.", hub_dev,
336 descriptor.port_count);
337 hub_dev->port_count = descriptor.port_count;
338 hub_dev->control_pipe = control_pipe;
339
340 usb_log_debug("(%p): Setting hub depth to %u.", hub_dev,
341 usb_device_get_depth(hub_dev->usb_device));
342 if ((opResult = usb_hub_set_depth(hub_dev))) {
343 usb_log_error("(%p): Failed to set hub depth: %s.",
344 hub_dev, str_error(opResult));
345 return opResult;
346 }
347
348 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
349 if (!hub_dev->ports) {
350 return ENOMEM;
351 }
352
353 for (size_t port = 0; port < hub_dev->port_count; ++port) {
354 usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
355 }
356
357 hub_dev->power_switched =
358 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
359 hub_dev->per_port_power =
360 descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
361
362 if (!hub_dev->power_switched) {
363 usb_log_info("(%p): Power switching not supported, "
364 "ports always powered.", hub_dev);
365 return EOK;
366 }
367
368 usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
369 hub_dev->per_port_power ? "per port" : "ganged");
370
371 for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
372 usb_log_debug("(%p): Powering port %u.", hub_dev, port + 1);
373 const int ret = usb_hub_set_port_feature(hub_dev, port + 1, USB_HUB_FEATURE_PORT_POWER);
374
375 if (ret != EOK) {
376 usb_log_error("(%p-%u): Cannot power on port: %s.",
377 hub_dev, hub_dev->ports[port].port_number,
378 str_error(ret));
379 } else {
380 if (!hub_dev->per_port_power) {
381 usb_log_debug("(%p) Ganged power switching, "
382 "one port is enough.", hub_dev);
383 break;
384 }
385 }
386 }
387 return EOK;
388}
389
390/**
391 * Set configuration of and USB device
392 *
393 * Check whether there is at least one configuration and sets the first one.
394 * This function should be run prior to running any hub-specific action.
395 * @param usb_device usb device representation
396 * @return error code
397 */
398static int usb_set_first_configuration(usb_device_t *usb_device)
399{
400 assert(usb_device);
401 /* Get number of possible configurations from device descriptor */
402 const size_t configuration_count =
403 usb_device_descriptors(usb_device)->device.configuration_count;
404 usb_log_debug("Hub has %zu configurations.", configuration_count);
405
406 if (configuration_count < 1) {
407 usb_log_error("There are no configurations available");
408 return EINVAL;
409 }
410
411 const size_t config_size =
412 usb_device_descriptors(usb_device)->full_config_size;
413 const usb_standard_configuration_descriptor_t *config_descriptor =
414 usb_device_descriptors(usb_device)->full_config;
415
416 if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
417 usb_log_error("Configuration descriptor is not big enough"
418 " to fit standard configuration descriptor.\n");
419 return EOVERFLOW;
420 }
421
422 /* Set configuration. Use the configuration that was in
423 * usb_device->descriptors.configuration i.e. The first one. */
424 const int opResult = usb_request_set_configuration(
425 usb_device_get_default_pipe(usb_device),
426 config_descriptor->configuration_number);
427 if (opResult != EOK) {
428 usb_log_error("Failed to set hub configuration: %s.",
429 str_error(opResult));
430 } else {
431 usb_log_debug("\tUsed configuration %d",
432 config_descriptor->configuration_number);
433 }
434 return opResult;
435}
436
437/**
438 * Process hub over current change
439 *
440 * This means either to power off the hub or power it on.
441 * @param hub_dev hub instance
442 * @param status hub status bitmask
443 * @return error code
444 */
445static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
446 usb_hub_status_t status)
447{
448 if (status & USB_HUB_STATUS_OVER_CURRENT) {
449 /* Hub should remove power from all ports if it detects OC */
450 usb_log_warning("(%p) Detected hub over-current condition, "
451 "all ports should be powered off.", hub_dev);
452 return;
453 }
454
455 /* Ports are always powered. */
456 if (!hub_dev->power_switched)
457 return;
458
459 /* Over-current condition is gone, it is safe to turn the ports on. */
460 for (size_t port = 0; port < hub_dev->port_count; ++port) {
461 const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
462 if (ret != EOK) {
463 usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
464 " power on port: %s\n", hub_dev,
465 hub_dev->ports[port].port_number, str_error(ret));
466 } else {
467 if (!hub_dev->per_port_power)
468 return;
469 }
470 }
471}
472
473/**
474 * Set feature on the real hub port.
475 *
476 * @param port Port structure.
477 * @param feature Feature selector.
478 */
479int usb_hub_set_depth(const usb_hub_dev_t *hub)
480{
481 assert(hub);
482
483 /* Slower hubs do not care about depth */
484 if (hub->speed < USB_SPEED_SUPER)
485 return EOK;
486
487 const usb_device_request_setup_packet_t set_request = {
488 .request_type = USB_HUB_REQ_TYPE_SET_HUB_DEPTH,
489 .request = USB_HUB_REQUEST_SET_HUB_DEPTH,
490 .value = uint16_host2usb(usb_device_get_depth(hub->usb_device) - 1),
491 .index = 0,
492 .length = 0,
493 };
494 return usb_pipe_control_write(hub->control_pipe, &set_request,
495 sizeof(set_request), NULL, 0);
496}
497
498/**
499 * Set feature on the real hub port.
500 *
501 * @param port Port structure.
502 * @param feature Feature selector.
503 */
504int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
505{
506 assert(hub);
507 const usb_device_request_setup_packet_t clear_request = {
508 .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
509 .request = USB_DEVREQ_SET_FEATURE,
510 .index = uint16_host2usb(port_number),
511 .value = feature,
512 .length = 0,
513 };
514 return usb_pipe_control_write(hub->control_pipe, &clear_request,
515 sizeof(clear_request), NULL, 0);
516}
517
518/**
519 * Clear feature on the real hub port.
520 *
521 * @param port Port structure.
522 * @param feature Feature selector.
523 */
524int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
525{
526 assert(hub);
527 const usb_device_request_setup_packet_t clear_request = {
528 .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
529 .request = USB_DEVREQ_CLEAR_FEATURE,
530 .value = feature,
531 .index = uint16_host2usb(port_number),
532 .length = 0,
533 };
534 return usb_pipe_control_write(hub->control_pipe,
535 &clear_request, sizeof(clear_request), NULL, 0);
536}
537
538/**
539 * Retrieve port status.
540 *
541 * @param[in] port Port structure
542 * @param[out] status Where to store the port status.
543 * @return Error code.
544 */
545int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
546{
547 assert(hub);
548 assert(status);
549
550 /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
551 * Generic GET_STATUS request cannot be used because of the difference
552 * in status data size (2B vs. 4B)*/
553 const usb_device_request_setup_packet_t request = {
554 .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
555 .request = USB_HUB_REQUEST_GET_STATUS,
556 .value = 0,
557 .index = uint16_host2usb(port_number),
558 .length = sizeof(usb_port_status_t),
559 };
560 size_t recv_size;
561
562 const int rc = usb_pipe_control_read(hub->control_pipe,
563 &request, sizeof(usb_device_request_setup_packet_t),
564 status, sizeof(*status), &recv_size);
565 if (rc != EOK)
566 return rc;
567
568 if (recv_size != sizeof(*status))
569 return ELIMIT;
570
571 return EOK;
572}
573
574/**
575 * Process hub interrupts.
576 *
577 * The change can be either in the over-current condition or local-power change.
578 * @param hub_dev hub instance
579 */
580static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
581{
582 assert(hub_dev);
583 assert(hub_dev->usb_device);
584 usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
585 usb_pipe_t *control_pipe =
586 usb_device_get_default_pipe(hub_dev->usb_device);
587
588 usb_hub_status_t status;
589 size_t rcvd_size;
590 /* NOTE: We can't use standard USB GET_STATUS request, because
591 * hubs reply is 4byte instead of 2 */
592 const int opResult = usb_pipe_control_read(control_pipe,
593 &get_hub_status_request, sizeof(get_hub_status_request),
594 &status, sizeof(usb_hub_status_t), &rcvd_size);
595 if (opResult != EOK) {
596 usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
597 str_error(opResult));
598 return;
599 }
600 if (rcvd_size != sizeof(usb_hub_status_t)) {
601 usb_log_error("(%p): Received status has incorrect size: "
602 "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
603 return;
604 }
605
606 /* Handle status changes */
607 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
608 usb_hub_over_current(hub_dev, status);
609 /* Ack change in hub OC flag */
610 const int ret = usb_request_clear_feature(
611 control_pipe, USB_REQUEST_TYPE_CLASS,
612 USB_REQUEST_RECIPIENT_DEVICE,
613 USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
614 if (ret != EOK) {
615 usb_log_error("(%p): Failed to clear hub over-current "
616 "change flag: %s.\n", hub_dev, str_error(opResult));
617 }
618 }
619
620 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
621 /* NOTE: Handling this is more complicated.
622 * If the transition is from bus power to local power, all
623 * is good and we may signal the parent hub that we don't
624 * need the power.
625 * If the transition is from local power to bus power
626 * the hub should turn off all the ports and devices need
627 * to be reinitialized taking into account the limited power
628 * that is now available.
629 * There is no support for power distribution in HelenOS,
630 * (or other OSes/hub devices that I've seen) so this is not
631 * implemented.
632 * Just ACK the change.
633 */
634 const int ret = usb_request_clear_feature(
635 control_pipe, USB_REQUEST_TYPE_CLASS,
636 USB_REQUEST_RECIPIENT_DEVICE,
637 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
638 if (opResult != EOK) {
639 usb_log_error("(%p): Failed to clear hub power change "
640 "flag: %s.\n", hub_dev, str_error(ret));
641 }
642 }
643}
644
645static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
646
647/**
648 * Reserve a default address for a port across all other devices connected to
649 * the bus. We aggregate requests for ports to minimize delays between
650 * connecting multiple devices from one hub - which happens e.g. when the hub
651 * is connected with already attached devices.
652 */
653int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
654{
655 assert(hub);
656 assert(exch);
657 assert(port);
658 assert(fibril_mutex_is_locked(&port->guard));
659
660 fibril_mutex_lock(&hub->default_address_guard);
661 if (hub->default_address_requests++ == 0) {
662 /* We're the first to request the address, we can just do it */
663 fibril_mutex_unlock(&hub->default_address_guard);
664 int err;
665 while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
666 // We ignore the return value here, as we cannot give up now.
667 usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
668 }
669 return err;
670 } else {
671 /* Drop the port guard, we're going to wait */
672 fibril_mutex_unlock(&port->guard);
673
674 /* Wait for a signal */
675 fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
676
677 /* Remember ABBA, first drop the hub guard */
678 fibril_mutex_unlock(&hub->default_address_guard);
679 fibril_mutex_lock(&port->guard);
680 return EOK;
681 }
682}
683
684/**
685 * Release the default address from a port.
686 */
687int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
688{
689 int ret = EOK;
690
691 fibril_mutex_lock(&hub->default_address_guard);
692 if (--hub->default_address_requests == 0) {
693 // We must do it in critical section to prevent other fibril
694 // from requesting the address before we release
695 ret = usbhc_release_default_address(exch);
696 // This is optimistic optimization - it may wake one hub from polling sleep
697 fibril_condvar_signal(&global_hub_default_address_cv);
698 } else {
699 fibril_condvar_signal(&hub->default_address_cv);
700 }
701 fibril_mutex_unlock(&hub->default_address_guard);
702
703 return ret;
704}
705
706/**
707 * @}
708 */
Note: See TracBrowser for help on using the repository browser.