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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a821f05 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
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
[9f685aa]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
[34d750c]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.
[bb70637]74 *
[34d750c]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)
[bb70637]82 */
[9f685aa]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,
[bb70637]90};
[df3ad97]91
[bf73a02]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,
[bba0f1fc]96 .index = 0,
[bf73a02]97 .value = 0,
98 .length = sizeof(usb_hub_status_t),
99};
100
[5a6cc679]101static errno_t usb_set_first_configuration(usb_device_t *);
102static errno_t usb_hub_process_hub_specific_info(usb_hub_dev_t *);
[34d750c]103static void usb_hub_over_current(const usb_hub_dev_t *, usb_hub_status_t);
[5a6cc679]104static errno_t usb_hub_polling_init(usb_hub_dev_t *, usb_endpoint_mapping_t *);
[34d750c]105static void usb_hub_global_interrupt(const usb_hub_dev_t *);
[205f0766]106
[34d750c]107static bool usb_hub_polling_error_callback(usb_device_t *dev,
[5a6cc679]108 errno_t err_code, void *arg)
[0b90f49]109{
110 assert(dev);
111 assert(arg);
112
[34d750c]113 usb_log_error("Device %s polling error: %s",
114 usb_device_get_name(dev), str_error(err_code));
[0b90f49]115
[c4e84ed6]116 return true;
[0b90f49]117}
118
[a209648]119/**
[54d1ad9]120 * Initialize hub device driver structure.
[a209648]121 *
[ea6de35]122 * Creates hub representation and fibril that periodically checks hub's status.
[a209648]123 * Hub representation is passed to the fibril.
124 * @param usb_dev generic usb device information
125 * @return error code
126 */
[5a6cc679]127errno_t usb_hub_device_add(usb_device_t *usb_dev)
[d46b13d]128{
[34d750c]129 int err;
[193da9d6]130 assert(usb_dev);
[34d750c]131
[193da9d6]132 /* Create driver soft-state structure */
[e27c2476]133 usb_hub_dev_t *hub_dev =
134 usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
[eda7a4e0]135 if (hub_dev == NULL) {
[a1732929]136 usb_log_error("Failed to create hub driver structure.");
[193da9d6]137 return ENOMEM;
138 }
[e27c2476]139 hub_dev->usb_device = usb_dev;
[129b821f]140 hub_dev->speed = usb_device_get_speed(usb_dev);
[ee9ea16]141
[193da9d6]142 /* Set hub's first configuration. (There should be only one) */
[34d750c]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;
[a209648]146 }
[d46b13d]147
[205f0766]148 /* Get port count and create attached_devices. */
[34d750c]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;
[a209648]152 }
[9063484]153
[9f685aa]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
[54d1ad9]165 /* Create hub control function. */
[a1732929]166 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
[6785b538]167 hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
[d46b13d]168 fun_exposed, HUB_FNC_NAME);
[eda7a4e0]169 if (hub_dev->hub_fun == NULL) {
[a1732929]170 usb_log_error("Failed to create hub function.");
[d46b13d]171 return ENOMEM;
172 }
[a209648]173
[54d1ad9]174 /* Bind hub control function. */
[34d750c]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;
[d46b13d]178 }
[3e490eb]179
[54d1ad9]180 /* Start hub operation. */
[34d750c]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;
[d46b13d]184 }
[8b71f3e]185
[129b821f]186 usb_log_info("Controlling %s-speed hub '%s' (%p: %zu ports).",
187 usb_str_speed(hub_dev->speed),
[e98e5fc]188 usb_device_get_name(hub_dev->usb_device), hub_dev,
189 hub_dev->port_count);
[d46b13d]190
191 return EOK;
[34d750c]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;
[a209648]198}
[76fbd9a]199
[5a6cc679]200static errno_t usb_hub_cleanup(usb_hub_dev_t *hub)
[5a73a7e]201{
[8b71f3e]202 free(hub->polling.buffer);
203 usb_polling_fini(&hub->polling);
204
[0b90f49]205 for (size_t port = 0; port < hub->port_count; ++port) {
[94f8c363]206 usb_port_fini(&hub->ports[port].base);
[0b90f49]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. */
[5a73a7e]221 return EOK;
[54d1ad9]222}
[76fbd9a]223
[91173333]224/**
225 * Turn off power to all ports.
226 *
227 * @param usb_dev generic usb device information
228 * @return error code
229 */
[5a6cc679]230errno_t usb_hub_device_remove(usb_device_t *usb_dev)
[0b90f49]231{
232 assert(usb_dev);
233 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
234 assert(hub);
235
[91173333]236 usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
[0b90f49]237
[8b71f3e]238 /* Join polling fibril (ignoring error code). */
239 usb_polling_join(&hub->polling);
[0b90f49]240 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
241
242 /* Destroy hub. */
243 return usb_hub_cleanup(hub);
244}
245
[54d1ad9]246/**
247 * Remove all attached devices
248 * @param usb_dev generic usb device information
249 * @return error code
250 */
[5a6cc679]251errno_t usb_hub_device_gone(usb_device_t *usb_dev)
[54d1ad9]252{
253 assert(usb_dev);
[0f4bff8]254 usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
[54d1ad9]255 assert(hub);
[0b90f49]256
257 usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
258
[8b71f3e]259 /* Join polling fibril (ignoring error code). */
260 usb_polling_join(&hub->polling);
[91173333]261 usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
[54d1ad9]262
[b233821]263 /* Destroy hub. */
[0b90f49]264 return usb_hub_cleanup(hub);
[54d1ad9]265}
[76fbd9a]266
[34d750c]267/**
268 * Initialize and start the polling of the Status Change Endpoint.
269 *
270 * @param mapping The mapping of Status Change Endpoint
271 */
[5a6cc679]272static errno_t usb_hub_polling_init(usb_hub_dev_t *hub_dev,
[34d750c]273 usb_endpoint_mapping_t *mapping)
274{
[5a6cc679]275 errno_t err;
[34d750c]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.
[3e490eb]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.
[6626bba9]305 * @param arg Custom argument, points to @c usb_hub_dev_t.
[3e490eb]306 * @return Whether to continue polling.
307 */
308bool hub_port_changes_callback(usb_device_t *dev,
[983e135]309 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
310{
[6626bba9]311 usb_hub_dev_t *hub = arg;
[0212751]312 assert(hub);
[3e490eb]313
[0212751]314 /* It is an error condition if we didn't receive enough data */
[3e490eb]315 if (change_bitmap_size == 0) {
[0212751]316 return false;
[3e490eb]317 }
318
[983e135]319 /* Lowest bit indicates global change */
320 const bool change = change_bitmap[0] & 1;
[f35b294]321 if (change) {
[ea6de35]322 usb_hub_global_interrupt(hub);
[1e1b1a9]323 }
324
[34d750c]325 /* Nth bit indicates change on port N */
[ea30cc1]326 for (size_t port = 0; port < hub->port_count; ++port) {
[a14d6a7]327 const size_t bit = port + 1;
328 const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
[3e490eb]329 if (change) {
[94f8c363]330 usb_hub_port_process_interrupt(&hub->ports[port]);
[3e490eb]331 }
332 }
333 return true;
334}
[76fbd9a]335
[37fce70]336static void usb_hub_power_ports(usb_hub_dev_t *hub_dev)
337{
[36e8a0c8]338 if (!hub_dev->power_switched) {
[37fce70]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);
[34d750c]349 const int ret = usb_hub_set_port_feature(hub_dev, port + 1,
350 USB_HUB_FEATURE_PORT_POWER);
[37fce70]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
[09daa8b]361/**
[eda7a4e0]362 * Load hub-specific information into hub_dev structure and process if needed
[09daa8b]363 *
[ea6de35]364 * Read port count and initialize structures holding per port information.
365 * If there are any non-removable devices, start initializing them.
[09daa8b]366 * This function is hub-specific and should be run only after the hub is
[193da9d6]367 * configured using usb_set_first_configuration function.
[eda7a4e0]368 * @param hub_dev hub representation
[09daa8b]369 * @return error code
370 */
[5a6cc679]371static errno_t usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
[5fd0dc23]372{
[eda7a4e0]373 assert(hub_dev);
[ea6de35]374
[e98e5fc]375 usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
[9f685aa]376 usb_pipe_t *control_pipe = usb_device_get_default_pipe(hub_dev->usb_device);
[09daa8b]377
[45e49e6]378 usb_descriptor_type_t desc_type = hub_dev->speed >= USB_SPEED_SUPER
379 ? USB_DESCTYPE_SSPEED_HUB : USB_DESCTYPE_HUB;
380
[34d750c]381 /* Get hub descriptor. */
[621ba8c]382 usb_hub_descriptor_header_t descriptor;
[09daa8b]383 size_t received_size;
[5a6cc679]384 errno_t opResult = usb_request_get_descriptor(control_pipe,
[f35b294]385 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
[45e49e6]386 desc_type, 0, 0, &descriptor,
[32cd37f]387 sizeof(usb_hub_descriptor_header_t), &received_size);
[09daa8b]388 if (opResult != EOK) {
[a1732929]389 usb_log_error("(%p): Failed to receive hub descriptor: %s.",
[e98e5fc]390 hub_dev, str_error(opResult));
[15b0432]391 return opResult;
392 }
[621ba8c]393
[a1732929]394 usb_log_debug("(%p): Setting port count to %d.", hub_dev,
[e98e5fc]395 descriptor.port_count);
[eda7a4e0]396 hub_dev->port_count = descriptor.port_count;
[c4e84ed6]397 hub_dev->control_pipe = control_pipe;
[5fd0dc23]398
[cb63854]399 usb_log_debug("(%p): Setting hub depth to %u.", hub_dev,
400 usb_device_get_depth(hub_dev->usb_device));
[45e49e6]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
[a14d6a7]407 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
[eda7a4e0]408 if (!hub_dev->ports) {
[361fcec]409 return ENOMEM;
410 }
[5fd0dc23]411
[a14d6a7]412 for (size_t port = 0; port < hub_dev->port_count; ++port) {
[c4e84ed6]413 usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
[15b0432]414 }
[5fd0dc23]415
[54d1ad9]416 hub_dev->power_switched =
[621ba8c]417 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
[54d1ad9]418 hub_dev->per_port_power =
419 descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
420
[9f685aa]421 const uint8_t protocol = usb_device_descriptors(hub_dev->usb_device)
422 ->device.device_protocol;
423 hub_dev->mtt_available = (protocol == 2);
424
[37fce70]425 usb_hub_power_ports(hub_dev);
[54d1ad9]426
[206f71a]427 return EOK;
[15b0432]428}
[76fbd9a]429
[15b0432]430/**
[193da9d6]431 * Set configuration of and USB device
[09daa8b]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.
[193da9d6]435 * @param usb_device usb device representation
[df3ad97]436 * @return error code
[15b0432]437 */
[5a6cc679]438static errno_t usb_set_first_configuration(usb_device_t *usb_device)
[983e135]439{
[193da9d6]440 assert(usb_device);
441 /* Get number of possible configurations from device descriptor */
442 const size_t configuration_count =
[e2dfa86]443 usb_device_descriptors(usb_device)->device.configuration_count;
[a1732929]444 usb_log_debug("Hub has %zu configurations.", configuration_count);
[983e135]445
[193da9d6]446 if (configuration_count < 1) {
[a1732929]447 usb_log_error("There are no configurations available");
[625f1ba]448 return EINVAL;
[15b0432]449 }
450
[e2dfa86]451 const size_t config_size =
452 usb_device_descriptors(usb_device)->full_config_size;
[8b68bdf]453 const usb_standard_configuration_descriptor_t *config_descriptor =
[e2dfa86]454 usb_device_descriptors(usb_device)->full_config;
[8b68bdf]455
456 if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
[fec6bf2]457 usb_log_error("Configuration descriptor is not big enough"
458 " to fit standard configuration descriptor.\n");
459 return EOVERFLOW;
460 }
461
[193da9d6]462 /* Set configuration. Use the configuration that was in
[ea6de35]463 * usb_device->descriptors.configuration i.e. The first one. */
[5a6cc679]464 errno_t opResult = usb_request_set_configuration(
[0f4bff8]465 usb_device_get_default_pipe(usb_device),
466 config_descriptor->configuration_number);
[15b0432]467 if (opResult != EOK) {
[a1732929]468 usb_log_error("Failed to set hub configuration: %s.",
[f35b294]469 str_error(opResult));
[ea6de35]470 } else {
[a1732929]471 usb_log_debug("\tUsed configuration %d",
[ea6de35]472 config_descriptor->configuration_number);
[15b0432]473 }
[effbef3]474
[ea6de35]475 return opResult;
[15b0432]476}
[76fbd9a]477
[3dba1ca]478/**
[ea6de35]479 * Process hub over current change
[3dba1ca]480 *
481 * This means either to power off the hub or power it on.
[eda7a4e0]482 * @param hub_dev hub instance
[3dba1ca]483 * @param status hub status bitmask
484 * @return error code
485 */
[eda7a4e0]486static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
[bf73a02]487 usb_hub_status_t status)
488{
489 if (status & USB_HUB_STATUS_OVER_CURRENT) {
[0212751]490 /* Hub should remove power from all ports if it detects OC */
[e98e5fc]491 usb_log_warning("(%p) Detected hub over-current condition, "
492 "all ports should be powered off.", hub_dev);
[54d1ad9]493 return;
[040ab02]494 }
[54d1ad9]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) {
[5a6cc679]502 const errno_t ret = usb_hub_set_port_feature(hub_dev, port,
[34d750c]503 USB_HUB_FEATURE_PORT_POWER);
[54d1ad9]504 if (ret != EOK) {
[e98e5fc]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));
[54d1ad9]508 } else {
509 if (!hub_dev->per_port_power)
510 return;
511 }
[0212751]512 }
[c4e84ed6]513}
514
[45e49e6]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,
[cb63854]532 .value = uint16_host2usb(usb_device_get_depth(hub->usb_device) - 1),
[45e49e6]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
[c4e84ed6]540/**
541 * Set feature on the real hub port.
542 *
543 * @param port Port structure.
544 * @param feature Feature selector.
545 */
[34d750c]546int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number,
547 usb_hub_class_feature_t feature)
[c4e84ed6]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 */
[34d750c]567int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number,
568 usb_hub_class_feature_t feature)
[c4e84ed6]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}
[54d1ad9]581
[c4e84ed6]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 */
[34d750c]589int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number,
590 usb_port_status_t *status)
[c4e84ed6]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
[9d3536e]607 uint32_t buffer;
[c4e84ed6]608 const int rc = usb_pipe_control_read(hub->control_pipe,
609 &request, sizeof(usb_device_request_setup_packet_t),
[9d3536e]610 &buffer, sizeof(buffer), &recv_size);
[c4e84ed6]611 if (rc != EOK)
612 return rc;
613
614 if (recv_size != sizeof(*status))
615 return ELIMIT;
616
[9d3536e]617 *status = uint32_usb2host(buffer);
[c4e84ed6]618 return EOK;
[040ab02]619}
[76fbd9a]620
[3dba1ca]621/**
[ea6de35]622 * Process hub interrupts.
[3dba1ca]623 *
[ea6de35]624 * The change can be either in the over-current condition or local-power change.
[eda7a4e0]625 * @param hub_dev hub instance
[3dba1ca]626 */
[eda7a4e0]627static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
[bf73a02]628{
[eda7a4e0]629 assert(hub_dev);
630 assert(hub_dev->usb_device);
[e98e5fc]631 usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
[0f4bff8]632 usb_pipe_t *control_pipe =
633 usb_device_get_default_pipe(hub_dev->usb_device);
[040ab02]634
[bf73a02]635 usb_hub_status_t status;
[040ab02]636 size_t rcvd_size;
[ea6de35]637 /* NOTE: We can't use standard USB GET_STATUS request, because
638 * hubs reply is 4byte instead of 2 */
[5a6cc679]639 const errno_t opResult = usb_pipe_control_read(control_pipe,
[75eb6735]640 &get_hub_status_request, sizeof(get_hub_status_request),
[bf73a02]641 &status, sizeof(usb_hub_status_t), &rcvd_size);
[040ab02]642 if (opResult != EOK) {
[e98e5fc]643 usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
[5c1a65e]644 str_error(opResult));
[040ab02]645 return;
646 }
[bf73a02]647 if (rcvd_size != sizeof(usb_hub_status_t)) {
[e98e5fc]648 usb_log_error("(%p): Received status has incorrect size: "
649 "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
[040ab02]650 return;
651 }
[bf73a02]652
653 /* Handle status changes */
[a61c683]654 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
[eda7a4e0]655 usb_hub_over_current(hub_dev, status);
[54d1ad9]656 /* Ack change in hub OC flag */
[5a6cc679]657 const errno_t ret = usb_request_clear_feature(
[0f4bff8]658 control_pipe, USB_REQUEST_TYPE_CLASS,
[54d1ad9]659 USB_REQUEST_RECIPIENT_DEVICE,
660 USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
661 if (ret != EOK) {
[e98e5fc]662 usb_log_error("(%p): Failed to clear hub over-current "
663 "change flag: %s.\n", hub_dev, str_error(opResult));
[54d1ad9]664 }
[a61c683]665 }
[bf73a02]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 */
[5a6cc679]681 const errno_t ret = usb_request_clear_feature(
[bba0f1fc]682 control_pipe, USB_REQUEST_TYPE_CLASS,
683 USB_REQUEST_RECIPIENT_DEVICE,
684 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
[bf73a02]685 if (opResult != EOK) {
[e98e5fc]686 usb_log_error("(%p): Failed to clear hub power change "
687 "flag: %s.\n", hub_dev, str_error(ret));
[bf73a02]688 }
[040ab02]689 }
690}
[76fbd9a]691
[34d750c]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 */
[94f8c363]698static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
[34d750c]699static FIBRIL_MUTEX_INITIALIZE(global_hub_default_address_guard);
[94f8c363]700
[51a51be]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 */
[34d750c]707int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch,
708 usb_port_t *port)
[51a51be]709{
710 assert(hub);
711 assert(exch);
[94f8c363]712 assert(port);
713 assert(fibril_mutex_is_locked(&port->guard));
[51a51be]714
[34d750c]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) {
[51a51be]721 /* Drop the port guard, we're going to wait */
[94f8c363]722 fibril_mutex_unlock(&port->guard);
[51a51be]723
[34d750c]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);
[51a51be]729
[94f8c363]730 fibril_mutex_lock(&port->guard);
[34d750c]731 err = usbhc_reserve_default_address(exch);
[51a51be]732 }
[34d750c]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;
[51a51be]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{
[34d750c]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);
[740dafc]763
764 return ret;
[51a51be]765}
766
[e080332]767/**
768 * @}
[71ed4849]769 */
Note: See TracBrowser for help on using the repository browser.