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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45457265 was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

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