source: mainline/uspace/drv/usbhub/usbhub.c@ 5c1a65e

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5c1a65e was 5c1a65e, checked in by Matus Dekanek <smekideki@…>, 15 years ago

code tiding

  • Property mode set to 100644
File size: 15.3 KB
RevLine 
[e080332]1/*
2 * Copyright (c) 2010 Matus Dekanek
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
[281ebae]28/** @addtogroup drvusbhub
[e080332]29 * @{
30 */
31/** @file
32 * @brief usb hub main functionality
33 */
34
[eb1a2f4]35#include <ddf/driver.h>
[e080332]36#include <bool.h>
37#include <errno.h>
[4e8e1f5]38#include <str_error.h>
[3e490eb]39#include <inttypes.h>
[e080332]40
[71ed4849]41#include <usb_iface.h>
[357a302]42#include <usb/ddfiface.h>
[e080332]43#include <usb/descriptor.h>
[7d521e24]44#include <usb/dev/recognise.h>
45#include <usb/dev/request.h>
[e080332]46#include <usb/classes/hub.h>
[7d521e24]47#include <usb/dev/poll.h>
[cd4b184]48#include <stdio.h>
[e080332]49
50#include "usbhub.h"
51#include "usbhub_private.h"
52#include "port_status.h"
[7d521e24]53#include <usb/usb.h>
54#include <usb/dev/pipes.h>
55#include <usb/classes/classes.h>
[e080332]56
[df3ad97]57
[5c1a65e]58static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
[a209648]59
[5c1a65e]60static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
[a209648]61
[5c1a65e]62static int usb_hub_set_configuration(usb_hub_info_t *hub_info);
[a209648]63
[5c1a65e]64static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info);
[a209648]65
[5c1a65e]66static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
[f35b294]67 usb_hub_status_t status);
[a209648]68
[5c1a65e]69static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
[f35b294]70 usb_hub_status_t status);
[a209648]71
[5c1a65e]72static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
[a209648]73
[5c1a65e]74static void usb_hub_polling_terminated_callback(usb_device_t *device,
[aca3489]75 bool was_error, void * data);
76
[b495a93]77
[e080332]78//*********************************************
79//
80// hub driver code, initialization
81//
82//*********************************************
83
[a209648]84/**
85 * Initialize hub device driver fibril
86 *
87 * Creates hub representation and fibril that periodically checks hub`s status.
88 * Hub representation is passed to the fibril.
89 * @param usb_dev generic usb device information
90 * @return error code
91 */
[5c1a65e]92int usb_hub_add_device(usb_device_t *usb_dev) {
[a209648]93 if (!usb_dev) return EINVAL;
[5c1a65e]94 usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev);
[a209648]95 //create hc connection
96 usb_log_debug("Initializing USB wire abstraction.\n");
97 int opResult = usb_hc_connection_initialize_from_device(
[f35b294]98 &hub_info->connection,
99 hub_info->usb_device->ddf_dev);
[a209648]100 if (opResult != EOK) {
[5c1a65e]101 usb_log_error("Could not initialize connection to device, "
102 " %s\n",
103 str_error(opResult));
[a209648]104 free(hub_info);
105 return opResult;
106 }
107
108 //set hub configuration
109 opResult = usb_hub_set_configuration(hub_info);
110 if (opResult != EOK) {
[5c1a65e]111 usb_log_error("Could not set hub configuration, %s\n",
112 str_error(opResult));
[a209648]113 free(hub_info);
114 return opResult;
115 }
116 //get port count and create attached_devs
117 opResult = usb_hub_process_hub_specific_info(hub_info);
118 if (opResult != EOK) {
[5c1a65e]119 usb_log_error("Could process hub specific info, %s\n",
120 str_error(opResult));
[a209648]121 free(hub_info);
122 return opResult;
123 }
[9063484]124
125 usb_log_debug("Creating 'hub' function in DDF.\n");
[a209648]126 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
[f35b294]127 fun_exposed, "hub");
[a209648]128 assert(hub_fun != NULL);
129 hub_fun->ops = NULL;
130
[f35b294]131 opResult = ddf_fun_bind(hub_fun);
132 assert(opResult == EOK);
133 opResult = ddf_fun_add_to_class(hub_fun, "hub");
134 assert(opResult == EOK);
[3e490eb]135
[f35b294]136 opResult = usb_hub_start_hub_fibril(hub_info);
[5c1a65e]137 if (opResult != EOK)
[3e490eb]138 free(hub_info);
[f35b294]139 return opResult;
[a209648]140}
141
[1e1b1a9]142/** Callback for polling hub for changes.
[3e490eb]143 *
144 * @param dev Device where the change occured.
145 * @param change_bitmap Bitmap of changed ports.
146 * @param change_bitmap_size Size of the bitmap in bytes.
147 * @param arg Custom argument, points to @c usb_hub_info_t.
148 * @return Whether to continue polling.
149 */
150bool hub_port_changes_callback(usb_device_t *dev,
[f35b294]151 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
[c1693dae]152 usb_log_debug("hub_port_changes_callback\n");
[3e490eb]153 usb_hub_info_t *hub = (usb_hub_info_t *) arg;
154
155 /* FIXME: check that we received enough bytes. */
156 if (change_bitmap_size == 0) {
157 goto leave;
158 }
159
[1e1b1a9]160 bool change;
[f35b294]161 change = ((uint8_t*) change_bitmap)[0] & 1;
162 if (change) {
[1e1b1a9]163 usb_hub_process_global_interrupt(hub);
164 }
165
[3e490eb]166 size_t port;
167 for (port = 1; port < hub->port_count + 1; port++) {
168 bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
169 if (change) {
170 usb_hub_process_interrupt(hub, port);
171 }
172 }
173leave:
174 /* FIXME: proper interval. */
[9014dcd]175 async_usleep(1000 * 250);
[3e490eb]176
177 return true;
178}
179
[1e1b1a9]180
[a209648]181//*********************************************
182//
183// support functions
184//
185//*********************************************
186
[15b0432]187/**
[09daa8b]188 * create usb_hub_info_t structure
189 *
190 * Does only basic copying of known information into new structure.
191 * @param usb_dev usb device structure
192 * @return basic usb_hub_info_t structure
[15b0432]193 */
[5c1a65e]194static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) {
195 usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t));
[195890b]196 if (!result) return NULL;
[09daa8b]197 result->usb_device = usb_dev;
198 result->status_change_pipe = usb_dev->pipes[0].pipe;
199 result->control_pipe = &usb_dev->ctrl_pipe;
200 result->is_default_address_used = false;
[3fb5a3e]201
[af6136d]202 result->ports = NULL;
[5c1a65e]203 result->port_count = (size_t) - 1;
[af6136d]204 fibril_mutex_initialize(&result->port_mutex);
205
[3fb5a3e]206 fibril_mutex_initialize(&result->pending_ops_mutex);
207 fibril_condvar_initialize(&result->pending_ops_cv);
208 result->pending_ops_count = 0;
[09daa8b]209 return result;
210}
211
212/**
[e6223239]213 * Load hub-specific information into hub_info structure and process if needed
[09daa8b]214 *
215 * Particularly read port count and initialize structure holding port
[e6223239]216 * information. If there are non-removable devices, start initializing them.
[09daa8b]217 * This function is hub-specific and should be run only after the hub is
218 * configured using usb_hub_set_configuration function.
[df3ad97]219 * @param hub_info hub representation
[09daa8b]220 * @return error code
221 */
[5c1a65e]222static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info) {
[09daa8b]223 // get hub descriptor
[5c1a65e]224 usb_log_debug("Creating serialized descriptor\n");
[c1693dae]225 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
[09daa8b]226 usb_hub_descriptor_t * descriptor;
[040ab02]227 int opResult;
[09daa8b]228
229 size_t received_size;
[1c89f74]230 opResult = usb_request_get_descriptor(hub_info->control_pipe,
[f35b294]231 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
232 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
233 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
[09daa8b]234
235 if (opResult != EOK) {
[5c1a65e]236 usb_log_error("Failed when receiving hub descriptor, "
237 "%s\n",
238 str_error(opResult));
[09daa8b]239 free(serialized_descriptor);
[15b0432]240 return opResult;
241 }
[5c1a65e]242 usb_log_debug2("Deserializing descriptor\n");
[cd5b878]243 descriptor = usb_create_deserialized_hub_desriptor(
244 serialized_descriptor);
[195890b]245 if (descriptor == NULL) {
[09daa8b]246 usb_log_warning("could not deserialize descriptor \n");
[c1693dae]247 return ENOMEM;
[15b0432]248 }
[195890b]249 usb_log_debug("setting port count to %d\n", descriptor->ports_count);
[09daa8b]250 hub_info->port_count = descriptor->ports_count;
[1e1b1a9]251 /// \TODO this is not semantically correct
[c1693dae]252 bool is_power_switched =
[5c1a65e]253 ((descriptor->hub_characteristics & 1) == 0);
[c1693dae]254 bool has_individual_port_powering =
[5c1a65e]255 ((descriptor->hub_characteristics & 1) != 0);
[f35b294]256 hub_info->ports = malloc(
257 sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
[5c1a65e]258 if (!hub_info->ports) {
[361fcec]259 return ENOMEM;
260 }
[3e490eb]261 size_t port;
[9014dcd]262 for (port = 0; port < hub_info->port_count + 1; ++port) {
[3e490eb]263 usb_hub_port_init(&hub_info->ports[port]);
[15b0432]264 }
[5c1a65e]265 if (is_power_switched) {
266 usb_log_debug("Hub power switched\n");
267
268 if (!has_individual_port_powering) {
269 usb_log_debug("Has_global powering\n");
[c1693dae]270 opResult = usb_hub_set_feature(hub_info->control_pipe,
271 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
272 if (opResult != EOK) {
[5c1a65e]273 usb_log_error("Cannot power hub: %s\n",
[4125b7d]274 str_error(opResult));
[c1693dae]275 }
[58226b4]276 }
[361fcec]277
278 for (port = 1; port <= hub_info->port_count; ++port) {
[5c1a65e]279 usb_log_debug("Powering port %zu.\n", port);
[361fcec]280 opResult = usb_hub_set_port_feature(hub_info->control_pipe,
281 port, USB_HUB_FEATURE_PORT_POWER);
282 if (opResult != EOK) {
283 usb_log_error("cannot power on port %zu: %s.\n",
284 port, str_error(opResult));
285 }
286 }
[5c1a65e]287
288 } else {
289 usb_log_debug("Power not switched, not going to be powered\n");
[15b0432]290 }
[5c1a65e]291 usb_log_debug2("Freeing data\n");
[36cd378]292 free(descriptor);
[206f71a]293 return EOK;
[15b0432]294}
[195890b]295
[15b0432]296/**
[09daa8b]297 * Set configuration of hub
298 *
299 * Check whether there is at least one configuration and sets the first one.
300 * This function should be run prior to running any hub-specific action.
[df3ad97]301 * @param hub_info hub representation
302 * @return error code
[15b0432]303 */
[5c1a65e]304static int usb_hub_set_configuration(usb_hub_info_t *hub_info) {
[15b0432]305 //device descriptor
[625f1ba]306 usb_standard_device_descriptor_t *std_descriptor
[f35b294]307 = &hub_info->usb_device->descriptors.device;
[5c1a65e]308 usb_log_debug("Hub has %d configurations\n",
[f35b294]309 std_descriptor->configuration_count);
[195890b]310 if (std_descriptor->configuration_count < 1) {
[5c1a65e]311 usb_log_error("There are no configurations available\n");
[625f1ba]312 return EINVAL;
[15b0432]313 }
314
[d70e0a3c]315 usb_standard_configuration_descriptor_t *config_descriptor
[f35b294]316 = (usb_standard_configuration_descriptor_t *)
317 hub_info->usb_device->descriptors.configuration;
[d70e0a3c]318
319 /* Set configuration. */
[625f1ba]320 int opResult = usb_request_set_configuration(
[f35b294]321 &hub_info->usb_device->ctrl_pipe,
322 config_descriptor->configuration_number);
[15b0432]323
324 if (opResult != EOK) {
[d70e0a3c]325 usb_log_error("Failed to set hub configuration: %s.\n",
[f35b294]326 str_error(opResult));
[15b0432]327 return opResult;
328 }
[5c1a65e]329 usb_log_debug("\tUsed configuration %d\n",
[f35b294]330 config_descriptor->configuration_number);
[625f1ba]331
[15b0432]332 return EOK;
333}
334
[e080332]335/**
[f35b294]336 * create and start fibril with hub control loop
[1e1b1a9]337 *
[f35b294]338 * Before the fibril is started, the control pipe and host controller
339 * connection of the hub is open.
340 *
341 * @param hub_info hub representing structure
[6c399765]342 * @return error code
[e080332]343 */
[5c1a65e]344static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) {
[49ce810]345 int rc;
[195890b]346
[f35b294]347 rc = usb_device_auto_poll(hub_info->usb_device, 0,
348 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
[3fb5a3e]349 usb_hub_polling_terminated_callback, hub_info);
[f35b294]350 if (rc != EOK) {
351 usb_log_error("Failed to create polling fibril: %s.\n",
352 str_error(rc));
353 free(hub_info);
354 return rc;
[cd4b184]355 }
[f35b294]356
[4125b7d]357 usb_log_info("Controlling hub `%s' (%zu ports).\n",
[f35b294]358 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
[3e490eb]359 return EOK;
360}
361
[f35b294]362//*********************************************
363//
364// change handling functions
365//
366//*********************************************
367
[3dba1ca]368/**
369 * process hub over current change
370 *
371 * This means either to power off the hub or power it on.
372 * @param hub_info hub instance
373 * @param status hub status bitmask
374 * @return error code
375 */
[5c1a65e]376static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
[f35b294]377 usb_hub_status_t status) {
[040ab02]378 int opResult;
[5c1a65e]379 if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) {
[ffca03c]380 //poweroff all ports
381 unsigned int port;
[5c1a65e]382 for (port = 1; port <= hub_info->port_count; ++port) {
[ffca03c]383 opResult = usb_hub_clear_port_feature(
[5c1a65e]384 hub_info->control_pipe, port,
[ffca03c]385 USB_HUB_FEATURE_PORT_POWER);
386 if (opResult != EOK) {
387 usb_log_warning(
[5c1a65e]388 "Cannot power off port %d; %s\n",
389 port, str_error(opResult));
[ffca03c]390 }
[040ab02]391 }
[f35b294]392 } else {
[ffca03c]393 //power all ports
394 unsigned int port;
[5c1a65e]395 for (port = 1; port <= hub_info->port_count; ++port) {
[ffca03c]396 opResult = usb_hub_set_port_feature(
[5c1a65e]397 hub_info->control_pipe, port,
[ffca03c]398 USB_HUB_FEATURE_PORT_POWER);
399 if (opResult != EOK) {
400 usb_log_warning(
[5c1a65e]401 "Cannot power off port %d; %s\n",
402 port, str_error(opResult));
[ffca03c]403 }
[040ab02]404 }
405 }
406 return opResult;
407}
408
[3dba1ca]409/**
[ffca03c]410 * process hub local power change
[3dba1ca]411 *
[ffca03c]412 * This change is ignored.
[3dba1ca]413 * @param hub_info hub instance
414 * @param status hub status bitmask
415 * @return error code
416 */
[5c1a65e]417static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
[f35b294]418 usb_hub_status_t status) {
[152ec79]419 int opResult = EOK;
420 opResult = usb_hub_clear_feature(hub_info->control_pipe,
421 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
[5c1a65e]422 if (opResult != EOK) {
423 usb_log_error("Cannnot clear hub power change flag: "
424 "%s\n",
425 str_error(opResult));
[040ab02]426 }
427 return opResult;
428}
429
[3dba1ca]430/**
431 * process hub interrupts
432 *
433 * The change can be either in the over-current condition or
[63d4d4fd]434 * local-power change.
[3dba1ca]435 * @param hub_info hub instance
436 */
[5c1a65e]437static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) {
438 usb_log_debug("Global interrupt on a hub\n");
[040ab02]439 usb_pipe_t *pipe = hub_info->control_pipe;
440 int opResult;
441
442 usb_port_status_t status;
443 size_t rcvd_size;
444 usb_device_request_setup_packet_t request;
445 //int opResult;
446 usb_hub_set_hub_status_request(&request);
447 //endpoint 0
448
449 opResult = usb_pipe_control_read(
[f35b294]450 pipe,
451 &request, sizeof (usb_device_request_setup_packet_t),
452 &status, 4, &rcvd_size
453 );
[040ab02]454 if (opResult != EOK) {
[5c1a65e]455 usb_log_error("Could not get hub status: %s\n",
456 str_error(opResult));
[040ab02]457 return;
458 }
459 if (rcvd_size != sizeof (usb_port_status_t)) {
[5c1a65e]460 usb_log_error("Received status has incorrect size\n");
[040ab02]461 return;
462 }
463 //port reset
[f35b294]464 if (
[5c1a65e]465 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
[f35b294]466 usb_process_hub_over_current(hub_info, status);
[040ab02]467 }
[f35b294]468 if (
[5c1a65e]469 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
[ffca03c]470 usb_process_hub_local_power_change(hub_info, status);
[040ab02]471 }
472}
473
[aca3489]474/**
475 * callback called from hub polling fibril when the fibril terminates
476 *
477 * Should perform a cleanup - deletes hub_info.
478 * @param device usb device afected
479 * @param was_error indicates that the fibril is stoped due to an error
480 * @param data pointer to usb_hub_info_t structure
481 */
[5c1a65e]482static void usb_hub_polling_terminated_callback(usb_device_t *device,
483 bool was_error, void *data) {
[3fb5a3e]484 usb_hub_info_t * hub = data;
485 assert(hub);
486
487 fibril_mutex_lock(&hub->pending_ops_mutex);
[dd143621]488
489 /* The device is dead. However there might be some pending operations
490 * that we need to wait for.
491 * One of them is device adding in progress.
492 * The respective fibril is probably waiting for status change
493 * in port reset (port enable) callback.
494 * Such change would never come (otherwise we would not be here).
495 * Thus, we would flush all pending port resets.
496 */
497 if (hub->pending_ops_count > 0) {
498 fibril_mutex_lock(&hub->port_mutex);
499 size_t port;
500 for (port = 0; port < hub->port_count; port++) {
501 usb_hub_port_t *the_port = hub->ports + port;
502 fibril_mutex_lock(&the_port->reset_mutex);
503 the_port->reset_completed = true;
504 the_port->reset_okay = false;
505 fibril_condvar_broadcast(&the_port->reset_cv);
506 fibril_mutex_unlock(&the_port->reset_mutex);
507 }
508 fibril_mutex_unlock(&hub->port_mutex);
509 }
510 /* And now wait for them. */
[3fb5a3e]511 while (hub->pending_ops_count > 0) {
512 fibril_condvar_wait(&hub->pending_ops_cv,
513 &hub->pending_ops_mutex);
514 }
515 fibril_mutex_unlock(&hub->pending_ops_mutex);
516
517 free(hub->ports);
518 free(hub);
[aca3489]519}
520
521
522
523
[e080332]524/**
525 * @}
[71ed4849]526 */
Note: See TracBrowser for help on using the repository browser.