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

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

usbhub: keep trying to power on ports, there might be multiple gangs

  • Property mode set to 100644
File size: 21.4 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>
[3cdaa7f]53#include <usbhc_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;
[129b821f]118 hub_dev->speed = usb_device_get_speed(usb_dev);
[ee9ea16]119
[51a51be]120 fibril_mutex_initialize(&hub_dev->default_address_guard);
121 fibril_condvar_initialize(&hub_dev->default_address_cv);
122
[193da9d6]123 /* Set hub's first configuration. (There should be only one) */
[d93f5afb]124 int opResult = usb_set_first_configuration(usb_dev);
[a209648]125 if (opResult != EOK) {
[a1732929]126 usb_log_error("Could not set hub configuration: %s",
[5c1a65e]127 str_error(opResult));
[a209648]128 return opResult;
129 }
[d46b13d]130
[205f0766]131 /* Get port count and create attached_devices. */
[eda7a4e0]132 opResult = usb_hub_process_hub_specific_info(hub_dev);
[a209648]133 if (opResult != EOK) {
[a1732929]134 usb_log_error("Could process hub specific info, %s",
[5c1a65e]135 str_error(opResult));
[a209648]136 return opResult;
137 }
[9063484]138
[54d1ad9]139 /* Create hub control function. */
[a1732929]140 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
[6785b538]141 hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
[d46b13d]142 fun_exposed, HUB_FNC_NAME);
[eda7a4e0]143 if (hub_dev->hub_fun == NULL) {
[a1732929]144 usb_log_error("Failed to create hub function.");
[d46b13d]145 return ENOMEM;
146 }
[a209648]147
[54d1ad9]148 /* Bind hub control function. */
[eda7a4e0]149 opResult = ddf_fun_bind(hub_dev->hub_fun);
[d46b13d]150 if (opResult != EOK) {
[a1732929]151 usb_log_error("Failed to bind hub function: %s.",
[d46b13d]152 str_error(opResult));
[eda7a4e0]153 ddf_fun_destroy(hub_dev->hub_fun);
[d46b13d]154 return opResult;
155 }
[3e490eb]156
[54d1ad9]157 /* Start hub operation. */
[8b71f3e]158 usb_polling_t *polling = &hub_dev->polling;
159 opResult = usb_polling_init(polling);
160 if (opResult != EOK) {
[01d9707]161 /* Function is already bound */
[8b71f3e]162 ddf_fun_unbind(hub_dev->hub_fun);
163 ddf_fun_destroy(hub_dev->hub_fun);
[a1732929]164 usb_log_error("Failed to initialize polling fibril: %s.",
[8b71f3e]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,
[7dddd7b]171 &hub_status_change_endpoint_description);
[8b71f3e]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);
[d46b13d]179 if (opResult != EOK) {
[01d9707]180 /* Polling is already initialized. */
[8b71f3e]181 free(polling->buffer);
[01d9707]182 usb_polling_fini(polling);
[eda7a4e0]183 ddf_fun_unbind(hub_dev->hub_fun);
184 ddf_fun_destroy(hub_dev->hub_fun);
[a1732929]185 usb_log_error("Failed to create polling fibril: %s.",
[d46b13d]186 str_error(opResult));
187 return opResult;
188 }
[8b71f3e]189
[129b821f]190 usb_log_info("Controlling %s-speed hub '%s' (%p: %zu ports).",
191 usb_str_speed(hub_dev->speed),
[e98e5fc]192 usb_device_get_name(hub_dev->usb_device), hub_dev,
193 hub_dev->port_count);
[d46b13d]194
195 return EOK;
[a209648]196}
[76fbd9a]197
[0b90f49]198static int usb_hub_cleanup(usb_hub_dev_t *hub)
[5a73a7e]199{
[8b71f3e]200 free(hub->polling.buffer);
201 usb_polling_fini(&hub->polling);
202
[0b90f49]203 for (size_t port = 0; port < hub->port_count; ++port) {
[94f8c363]204 usb_port_fini(&hub->ports[port].base);
[0b90f49]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. */
[5a73a7e]219 return EOK;
[54d1ad9]220}
[76fbd9a]221
[91173333]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)
[0b90f49]229{
230 assert(usb_dev);
231 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
232 assert(hub);
233
[91173333]234 usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
[0b90f49]235
[8b71f3e]236 /* Join polling fibril (ignoring error code). */
237 usb_polling_join(&hub->polling);
[0b90f49]238 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
239
240 /* Destroy hub. */
241 return usb_hub_cleanup(hub);
242}
243
[54d1ad9]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);
[0f4bff8]252 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
[54d1ad9]253 assert(hub);
[0b90f49]254
255 usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
256
[8b71f3e]257 /* Join polling fibril (ignoring error code). */
258 usb_polling_join(&hub->polling);
[91173333]259 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
[54d1ad9]260
[b233821]261 /* Destroy hub. */
[0b90f49]262 return usb_hub_cleanup(hub);
[54d1ad9]263}
[76fbd9a]264
[1e1b1a9]265/** Callback for polling hub for changes.
[3e490eb]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.
[6626bba9]270 * @param arg Custom argument, points to @c usb_hub_dev_t.
[3e490eb]271 * @return Whether to continue polling.
272 */
273bool hub_port_changes_callback(usb_device_t *dev,
[983e135]274 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
275{
[6626bba9]276 usb_hub_dev_t *hub = arg;
[0212751]277 assert(hub);
[3e490eb]278
[0212751]279 /* It is an error condition if we didn't receive enough data */
[3e490eb]280 if (change_bitmap_size == 0) {
[0212751]281 return false;
[3e490eb]282 }
283
[983e135]284 /* Lowest bit indicates global change */
285 const bool change = change_bitmap[0] & 1;
[f35b294]286 if (change) {
[ea6de35]287 usb_hub_global_interrupt(hub);
[1e1b1a9]288 }
289
[983e135]290 /* N + 1 bit indicates change on port N */
[ea30cc1]291 for (size_t port = 0; port < hub->port_count; ++port) {
[a14d6a7]292 const size_t bit = port + 1;
293 const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
[3e490eb]294 if (change) {
[94f8c363]295 usb_hub_port_process_interrupt(&hub->ports[port]);
[3e490eb]296 }
297 }
298 return true;
299}
[76fbd9a]300
[09daa8b]301/**
[eda7a4e0]302 * Load hub-specific information into hub_dev structure and process if needed
[09daa8b]303 *
[ea6de35]304 * Read port count and initialize structures holding per port information.
305 * If there are any non-removable devices, start initializing them.
[09daa8b]306 * This function is hub-specific and should be run only after the hub is
[193da9d6]307 * configured using usb_set_first_configuration function.
[eda7a4e0]308 * @param hub_dev hub representation
[09daa8b]309 * @return error code
310 */
[eda7a4e0]311static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
[5fd0dc23]312{
[eda7a4e0]313 assert(hub_dev);
[ea6de35]314
315 /* Get hub descriptor. */
[e98e5fc]316 usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
[0f4bff8]317 usb_pipe_t *control_pipe =
318 usb_device_get_default_pipe(hub_dev->usb_device);
[09daa8b]319
[45e49e6]320 usb_descriptor_type_t desc_type = hub_dev->speed >= USB_SPEED_SUPER
321 ? USB_DESCTYPE_SSPEED_HUB : USB_DESCTYPE_HUB;
322
[621ba8c]323 usb_hub_descriptor_header_t descriptor;
[09daa8b]324 size_t received_size;
[621ba8c]325 int opResult = usb_request_get_descriptor(control_pipe,
[f35b294]326 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
[45e49e6]327 desc_type, 0, 0, &descriptor,
[32cd37f]328 sizeof(usb_hub_descriptor_header_t), &received_size);
[09daa8b]329 if (opResult != EOK) {
[a1732929]330 usb_log_error("(%p): Failed to receive hub descriptor: %s.",
[e98e5fc]331 hub_dev, str_error(opResult));
[15b0432]332 return opResult;
333 }
[621ba8c]334
[a1732929]335 usb_log_debug("(%p): Setting port count to %d.", hub_dev,
[e98e5fc]336 descriptor.port_count);
[eda7a4e0]337 hub_dev->port_count = descriptor.port_count;
[c4e84ed6]338 hub_dev->control_pipe = control_pipe;
[5fd0dc23]339
[cb63854]340 usb_log_debug("(%p): Setting hub depth to %u.", hub_dev,
341 usb_device_get_depth(hub_dev->usb_device));
[45e49e6]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
[a14d6a7]348 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
[eda7a4e0]349 if (!hub_dev->ports) {
[361fcec]350 return ENOMEM;
351 }
[5fd0dc23]352
[a14d6a7]353 for (size_t port = 0; port < hub_dev->port_count; ++port) {
[c4e84ed6]354 usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
[15b0432]355 }
[5fd0dc23]356
[54d1ad9]357 hub_dev->power_switched =
[621ba8c]358 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
[54d1ad9]359 hub_dev->per_port_power =
360 descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
361
362 if (!hub_dev->power_switched) {
[e98e5fc]363 usb_log_info("(%p): Power switching not supported, "
364 "ports always powered.", hub_dev);
[54d1ad9]365 return EOK;
366 }
367
[a1732929]368 usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
[dc1d499]369 hub_dev->per_port_power ? "per port" : "ganged");
[54d1ad9]370
[58563585]371 for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
[4397984]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);
[54d1ad9]374
375 if (ret != EOK) {
[a1732929]376 usb_log_error("(%p-%u): Cannot power on port: %s.",
[e98e5fc]377 hub_dev, hub_dev->ports[port].port_number,
378 str_error(ret));
[54d1ad9]379 } else {
380 if (!hub_dev->per_port_power) {
[cd0cf81]381 usb_log_debug("(%p) Ganged power switching, port %u is probably already powered.", hub_dev, port + 1);
382 } else {
383 usb_log_warning("(%p): Failed to power port %u.", hub_dev, port + 1);
[361fcec]384 }
385 }
[15b0432]386 }
[206f71a]387 return EOK;
[15b0432]388}
[76fbd9a]389
[15b0432]390/**
[193da9d6]391 * Set configuration of and USB device
[09daa8b]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.
[193da9d6]395 * @param usb_device usb device representation
[df3ad97]396 * @return error code
[15b0432]397 */
[193da9d6]398static int usb_set_first_configuration(usb_device_t *usb_device)
[983e135]399{
[193da9d6]400 assert(usb_device);
401 /* Get number of possible configurations from device descriptor */
402 const size_t configuration_count =
[e2dfa86]403 usb_device_descriptors(usb_device)->device.configuration_count;
[a1732929]404 usb_log_debug("Hub has %zu configurations.", configuration_count);
[983e135]405
[193da9d6]406 if (configuration_count < 1) {
[a1732929]407 usb_log_error("There are no configurations available");
[625f1ba]408 return EINVAL;
[15b0432]409 }
410
[e2dfa86]411 const size_t config_size =
412 usb_device_descriptors(usb_device)->full_config_size;
[8b68bdf]413 const usb_standard_configuration_descriptor_t *config_descriptor =
[e2dfa86]414 usb_device_descriptors(usb_device)->full_config;
[8b68bdf]415
416 if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
[fec6bf2]417 usb_log_error("Configuration descriptor is not big enough"
418 " to fit standard configuration descriptor.\n");
419 return EOVERFLOW;
420 }
421
[193da9d6]422 /* Set configuration. Use the configuration that was in
[ea6de35]423 * usb_device->descriptors.configuration i.e. The first one. */
[193da9d6]424 const int opResult = usb_request_set_configuration(
[0f4bff8]425 usb_device_get_default_pipe(usb_device),
426 config_descriptor->configuration_number);
[15b0432]427 if (opResult != EOK) {
[a1732929]428 usb_log_error("Failed to set hub configuration: %s.",
[f35b294]429 str_error(opResult));
[ea6de35]430 } else {
[a1732929]431 usb_log_debug("\tUsed configuration %d",
[ea6de35]432 config_descriptor->configuration_number);
[15b0432]433 }
[ea6de35]434 return opResult;
[15b0432]435}
[76fbd9a]436
[3dba1ca]437/**
[ea6de35]438 * Process hub over current change
[3dba1ca]439 *
440 * This means either to power off the hub or power it on.
[eda7a4e0]441 * @param hub_dev hub instance
[3dba1ca]442 * @param status hub status bitmask
443 * @return error code
444 */
[eda7a4e0]445static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
[bf73a02]446 usb_hub_status_t status)
447{
448 if (status & USB_HUB_STATUS_OVER_CURRENT) {
[0212751]449 /* Hub should remove power from all ports if it detects OC */
[e98e5fc]450 usb_log_warning("(%p) Detected hub over-current condition, "
451 "all ports should be powered off.", hub_dev);
[54d1ad9]452 return;
[040ab02]453 }
[54d1ad9]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) {
[c4e84ed6]461 const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
[54d1ad9]462 if (ret != EOK) {
[e98e5fc]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));
[54d1ad9]466 } else {
467 if (!hub_dev->per_port_power)
468 return;
469 }
[0212751]470 }
[c4e84ed6]471}
472
[45e49e6]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,
[cb63854]490 .value = uint16_host2usb(usb_device_get_depth(hub->usb_device) - 1),
[45e49e6]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
[c4e84ed6]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}
[54d1ad9]537
[c4e84ed6]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
[9d3536e]562 uint32_t buffer;
[c4e84ed6]563 const int rc = usb_pipe_control_read(hub->control_pipe,
564 &request, sizeof(usb_device_request_setup_packet_t),
[9d3536e]565 &buffer, sizeof(buffer), &recv_size);
[c4e84ed6]566 if (rc != EOK)
567 return rc;
568
569 if (recv_size != sizeof(*status))
570 return ELIMIT;
571
[9d3536e]572 *status = uint32_usb2host(buffer);
[c4e84ed6]573 return EOK;
[040ab02]574}
[76fbd9a]575
[3dba1ca]576/**
[ea6de35]577 * Process hub interrupts.
[3dba1ca]578 *
[ea6de35]579 * The change can be either in the over-current condition or local-power change.
[eda7a4e0]580 * @param hub_dev hub instance
[3dba1ca]581 */
[eda7a4e0]582static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
[bf73a02]583{
[eda7a4e0]584 assert(hub_dev);
585 assert(hub_dev->usb_device);
[e98e5fc]586 usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
[0f4bff8]587 usb_pipe_t *control_pipe =
588 usb_device_get_default_pipe(hub_dev->usb_device);
[040ab02]589
[bf73a02]590 usb_hub_status_t status;
[040ab02]591 size_t rcvd_size;
[ea6de35]592 /* NOTE: We can't use standard USB GET_STATUS request, because
593 * hubs reply is 4byte instead of 2 */
[75eb6735]594 const int opResult = usb_pipe_control_read(control_pipe,
595 &get_hub_status_request, sizeof(get_hub_status_request),
[bf73a02]596 &status, sizeof(usb_hub_status_t), &rcvd_size);
[040ab02]597 if (opResult != EOK) {
[e98e5fc]598 usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
[5c1a65e]599 str_error(opResult));
[040ab02]600 return;
601 }
[bf73a02]602 if (rcvd_size != sizeof(usb_hub_status_t)) {
[e98e5fc]603 usb_log_error("(%p): Received status has incorrect size: "
604 "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
[040ab02]605 return;
606 }
[bf73a02]607
608 /* Handle status changes */
[a61c683]609 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
[eda7a4e0]610 usb_hub_over_current(hub_dev, status);
[54d1ad9]611 /* Ack change in hub OC flag */
612 const int ret = usb_request_clear_feature(
[0f4bff8]613 control_pipe, USB_REQUEST_TYPE_CLASS,
[54d1ad9]614 USB_REQUEST_RECIPIENT_DEVICE,
615 USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
616 if (ret != EOK) {
[e98e5fc]617 usb_log_error("(%p): Failed to clear hub over-current "
618 "change flag: %s.\n", hub_dev, str_error(opResult));
[54d1ad9]619 }
[a61c683]620 }
[bf73a02]621
622 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
623 /* NOTE: Handling this is more complicated.
624 * If the transition is from bus power to local power, all
625 * is good and we may signal the parent hub that we don't
626 * need the power.
627 * If the transition is from local power to bus power
628 * the hub should turn off all the ports and devices need
629 * to be reinitialized taking into account the limited power
630 * that is now available.
631 * There is no support for power distribution in HelenOS,
632 * (or other OSes/hub devices that I've seen) so this is not
633 * implemented.
634 * Just ACK the change.
635 */
[54d1ad9]636 const int ret = usb_request_clear_feature(
[bba0f1fc]637 control_pipe, USB_REQUEST_TYPE_CLASS,
638 USB_REQUEST_RECIPIENT_DEVICE,
639 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
[bf73a02]640 if (opResult != EOK) {
[e98e5fc]641 usb_log_error("(%p): Failed to clear hub power change "
642 "flag: %s.\n", hub_dev, str_error(ret));
[bf73a02]643 }
[040ab02]644 }
645}
[76fbd9a]646
[94f8c363]647static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
648
[51a51be]649/**
650 * Reserve a default address for a port across all other devices connected to
651 * the bus. We aggregate requests for ports to minimize delays between
652 * connecting multiple devices from one hub - which happens e.g. when the hub
653 * is connected with already attached devices.
654 */
[94f8c363]655int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
[51a51be]656{
657 assert(hub);
658 assert(exch);
[94f8c363]659 assert(port);
660 assert(fibril_mutex_is_locked(&port->guard));
[51a51be]661
662 fibril_mutex_lock(&hub->default_address_guard);
663 if (hub->default_address_requests++ == 0) {
664 /* We're the first to request the address, we can just do it */
665 fibril_mutex_unlock(&hub->default_address_guard);
666 int err;
667 while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
[94f8c363]668 // We ignore the return value here, as we cannot give up now.
669 usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
[51a51be]670 }
671 return err;
672 } else {
673 /* Drop the port guard, we're going to wait */
[94f8c363]674 fibril_mutex_unlock(&port->guard);
[51a51be]675
676 /* Wait for a signal */
677 fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
678
679 /* Remember ABBA, first drop the hub guard */
680 fibril_mutex_unlock(&hub->default_address_guard);
[94f8c363]681 fibril_mutex_lock(&port->guard);
[51a51be]682 return EOK;
683 }
684}
685
686/**
687 * Release the default address from a port.
688 */
689int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
690{
[740dafc]691 int ret = EOK;
692
[51a51be]693 fibril_mutex_lock(&hub->default_address_guard);
694 if (--hub->default_address_requests == 0) {
[740dafc]695 // We must do it in critical section to prevent other fibril
696 // from requesting the address before we release
697 ret = usbhc_release_default_address(exch);
[94f8c363]698 // This is optimistic optimization - it may wake one hub from polling sleep
699 fibril_condvar_signal(&global_hub_default_address_cv);
[51a51be]700 } else {
701 fibril_condvar_signal(&hub->default_address_cv);
702 }
[740dafc]703 fibril_mutex_unlock(&hub->default_address_guard);
704
705 return ret;
[51a51be]706}
707
[e080332]708/**
709 * @}
[71ed4849]710 */
Note: See TracBrowser for help on using the repository browser.