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

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

usbhub: fix race between def. address release/request

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