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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b2aaaa0 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

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