source: mainline/uspace/drv/usbhub/usbhub.c@ bc02b83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bc02b83 was 152ec79, checked in by Matus Dekanek <smekideki@…>, 14 years ago

corrected hub power change handling

  • Property mode set to 100644
File size: 14.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>
[4e8e1f5]44#include <usb/recognise.h>
[d81ef61c]45#include <usb/request.h>
[e080332]46#include <usb/classes/hub.h>
[5e07e2b5]47#include <usb/devpoll.h>
[cd4b184]48#include <stdio.h>
[e080332]49
50#include "usbhub.h"
51#include "usbhub_private.h"
52#include "port_status.h"
[f40a1e2]53#include "usb/usb.h"
[d81ef61c]54#include "usb/pipes.h"
[15b0432]55#include "usb/classes/classes.h"
[e080332]56
[df3ad97]57
[a209648]58static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev);
59
60static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info);
61
62static int usb_hub_set_configuration(usb_hub_info_t * hub_info);
63
[f35b294]64static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info);
[a209648]65
66static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
[f35b294]67 usb_hub_status_t status);
[a209648]68
69static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
[f35b294]70 usb_hub_status_t status);
[a209648]71
72static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
73
[b495a93]74
[e080332]75//*********************************************
76//
77// hub driver code, initialization
78//
79//*********************************************
80
[a209648]81/**
82 * Initialize hub device driver fibril
83 *
84 * Creates hub representation and fibril that periodically checks hub`s status.
85 * Hub representation is passed to the fibril.
86 * @param usb_dev generic usb device information
87 * @return error code
88 */
89int usb_hub_add_device(usb_device_t * usb_dev) {
90 if (!usb_dev) return EINVAL;
91 usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
92 //create hc connection
93 usb_log_debug("Initializing USB wire abstraction.\n");
94 int opResult = usb_hc_connection_initialize_from_device(
[f35b294]95 &hub_info->connection,
96 hub_info->usb_device->ddf_dev);
[a209648]97 if (opResult != EOK) {
98 usb_log_error("could not initialize connection to device, "
[f35b294]99 "errno %d\n",
100 opResult);
[a209648]101 free(hub_info);
102 return opResult;
103 }
104
105 //set hub configuration
106 opResult = usb_hub_set_configuration(hub_info);
107 if (opResult != EOK) {
108 usb_log_error("could not set hub configuration, errno %d\n",
[f35b294]109 opResult);
[a209648]110 free(hub_info);
111 return opResult;
112 }
113 //get port count and create attached_devs
114 opResult = usb_hub_process_hub_specific_info(hub_info);
115 if (opResult != EOK) {
[f35b294]116 usb_log_error("could process hub specific info, errno %d\n",
117 opResult);
[a209648]118 free(hub_info);
119 return opResult;
120 }
[9063484]121
122 usb_log_debug("Creating 'hub' function in DDF.\n");
[a209648]123 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
[f35b294]124 fun_exposed, "hub");
[a209648]125 assert(hub_fun != NULL);
126 hub_fun->ops = NULL;
127
[f35b294]128 opResult = ddf_fun_bind(hub_fun);
129 assert(opResult == EOK);
130 opResult = ddf_fun_add_to_class(hub_fun, "hub");
131 assert(opResult == EOK);
[3e490eb]132
[f35b294]133 opResult = usb_hub_start_hub_fibril(hub_info);
134 if(opResult!=EOK)
[3e490eb]135 free(hub_info);
[f35b294]136 return opResult;
[a209648]137}
138
139
[1e1b1a9]140/** Callback for polling hub for changes.
[3e490eb]141 *
142 * @param dev Device where the change occured.
143 * @param change_bitmap Bitmap of changed ports.
144 * @param change_bitmap_size Size of the bitmap in bytes.
145 * @param arg Custom argument, points to @c usb_hub_info_t.
146 * @return Whether to continue polling.
147 */
148bool hub_port_changes_callback(usb_device_t *dev,
[f35b294]149 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
[c1693dae]150 usb_log_debug("hub_port_changes_callback\n");
[3e490eb]151 usb_hub_info_t *hub = (usb_hub_info_t *) arg;
152
153 /* FIXME: check that we received enough bytes. */
154 if (change_bitmap_size == 0) {
155 goto leave;
156 }
157
[1e1b1a9]158 bool change;
[f35b294]159 change = ((uint8_t*) change_bitmap)[0] & 1;
160 if (change) {
[1e1b1a9]161 usb_hub_process_global_interrupt(hub);
162 }
163
[3e490eb]164 size_t port;
165 for (port = 1; port < hub->port_count + 1; port++) {
166 bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
167 if (change) {
168 usb_hub_process_interrupt(hub, port);
169 }
170 }
171leave:
172 /* FIXME: proper interval. */
[9014dcd]173 async_usleep(1000 * 250);
[3e490eb]174
175 return true;
176}
177
[1e1b1a9]178
[a209648]179//*********************************************
180//
181// support functions
182//
183//*********************************************
184
[15b0432]185/**
[09daa8b]186 * create usb_hub_info_t structure
187 *
188 * Does only basic copying of known information into new structure.
189 * @param usb_dev usb device structure
190 * @return basic usb_hub_info_t structure
[15b0432]191 */
[09daa8b]192static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
[f35b294]193 usb_hub_info_t * result = malloc(sizeof(usb_hub_info_t));
[195890b]194 if (!result) return NULL;
[09daa8b]195 result->usb_device = usb_dev;
196 result->status_change_pipe = usb_dev->pipes[0].pipe;
197 result->control_pipe = &usb_dev->ctrl_pipe;
198 result->is_default_address_used = false;
199 return result;
200}
201
202/**
[e6223239]203 * Load hub-specific information into hub_info structure and process if needed
[09daa8b]204 *
205 * Particularly read port count and initialize structure holding port
[e6223239]206 * information. If there are non-removable devices, start initializing them.
[09daa8b]207 * This function is hub-specific and should be run only after the hub is
208 * configured using usb_hub_set_configuration function.
[df3ad97]209 * @param hub_info hub representation
[09daa8b]210 * @return error code
211 */
[195890b]212static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
[09daa8b]213 // get hub descriptor
214 usb_log_debug("creating serialized descriptor\n");
[c1693dae]215 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
[09daa8b]216 usb_hub_descriptor_t * descriptor;
[040ab02]217 int opResult;
[09daa8b]218
219 size_t received_size;
[1c89f74]220 opResult = usb_request_get_descriptor(hub_info->control_pipe,
[f35b294]221 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
222 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
223 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
[09daa8b]224
225 if (opResult != EOK) {
[195890b]226 usb_log_error("failed when receiving hub descriptor, "
[f35b294]227 "badcode = %d\n",
228 opResult);
[09daa8b]229 free(serialized_descriptor);
[15b0432]230 return opResult;
231 }
[09daa8b]232 usb_log_debug2("deserializing descriptor\n");
[cd5b878]233 descriptor = usb_create_deserialized_hub_desriptor(
234 serialized_descriptor);
[195890b]235 if (descriptor == NULL) {
[09daa8b]236 usb_log_warning("could not deserialize descriptor \n");
[c1693dae]237 return ENOMEM;
[15b0432]238 }
[195890b]239 usb_log_debug("setting port count to %d\n", descriptor->ports_count);
[09daa8b]240 hub_info->port_count = descriptor->ports_count;
[1e1b1a9]241 /// \TODO this is not semantically correct
[c1693dae]242 bool is_power_switched =
243 ((descriptor->hub_characteristics & 1) ==0);
244 bool has_individual_port_powering =
245 ((descriptor->hub_characteristics & 1) !=0);
[f35b294]246 hub_info->ports = malloc(
247 sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
[361fcec]248 if(!hub_info->ports){
249 return ENOMEM;
250 }
[3e490eb]251 size_t port;
[9014dcd]252 for (port = 0; port < hub_info->port_count + 1; ++port) {
[3e490eb]253 usb_hub_port_init(&hub_info->ports[port]);
[15b0432]254 }
[c1693dae]255 if(is_power_switched){
256 usb_log_debug("is_power_switched\n");
[9014dcd]257
258 if(!has_individual_port_powering){
[c1693dae]259 usb_log_debug("!has_individual_port_powering\n");
260 opResult = usb_hub_set_feature(hub_info->control_pipe,
261 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
262 if (opResult != EOK) {
[4125b7d]263 usb_log_error("cannot power hub: %s\n",
264 str_error(opResult));
[c1693dae]265 }
[58226b4]266 }
[361fcec]267
268 for (port = 1; port <= hub_info->port_count; ++port) {
269 usb_log_debug("Powering port %zu.\n",port);
270 opResult = usb_hub_set_port_feature(hub_info->control_pipe,
271 port, USB_HUB_FEATURE_PORT_POWER);
272 if (opResult != EOK) {
273 usb_log_error("cannot power on port %zu: %s.\n",
274 port, str_error(opResult));
275 }
276 }
277
[c1693dae]278 }else{
[361fcec]279 usb_log_debug("!is_power_switched, not going to be powered\n");
[15b0432]280 }
[09daa8b]281 usb_log_debug2("freeing data\n");
[36cd378]282 free(descriptor);
[206f71a]283 return EOK;
[15b0432]284}
[195890b]285
[15b0432]286/**
[09daa8b]287 * Set configuration of hub
288 *
289 * Check whether there is at least one configuration and sets the first one.
290 * This function should be run prior to running any hub-specific action.
[df3ad97]291 * @param hub_info hub representation
292 * @return error code
[15b0432]293 */
[195890b]294static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
[15b0432]295 //device descriptor
[625f1ba]296 usb_standard_device_descriptor_t *std_descriptor
[f35b294]297 = &hub_info->usb_device->descriptors.device;
[fbefd0e]298 usb_log_debug("hub has %d configurations\n",
[f35b294]299 std_descriptor->configuration_count);
[195890b]300 if (std_descriptor->configuration_count < 1) {
[4d0c40b]301 usb_log_error("there are no configurations available\n");
[625f1ba]302 return EINVAL;
[15b0432]303 }
304
[d70e0a3c]305 usb_standard_configuration_descriptor_t *config_descriptor
[f35b294]306 = (usb_standard_configuration_descriptor_t *)
307 hub_info->usb_device->descriptors.configuration;
[d70e0a3c]308
309 /* Set configuration. */
[625f1ba]310 int opResult = usb_request_set_configuration(
[f35b294]311 &hub_info->usb_device->ctrl_pipe,
312 config_descriptor->configuration_number);
[15b0432]313
314 if (opResult != EOK) {
[d70e0a3c]315 usb_log_error("Failed to set hub configuration: %s.\n",
[f35b294]316 str_error(opResult));
[15b0432]317 return opResult;
318 }
[09daa8b]319 usb_log_debug("\tused configuration %d\n",
[f35b294]320 config_descriptor->configuration_number);
[625f1ba]321
[15b0432]322 return EOK;
323}
324
[e080332]325/**
[f35b294]326 * create and start fibril with hub control loop
[1e1b1a9]327 *
[f35b294]328 * Before the fibril is started, the control pipe and host controller
329 * connection of the hub is open.
330 *
331 * @param hub_info hub representing structure
[6c399765]332 * @return error code
[e080332]333 */
[f35b294]334static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info){
[1e1b1a9]335 /*
[f35b294]336 * The processing will require opened control pipe and connection
337 * to the host controller.
338 * It is waste of resources but let's hope there will be less
339 * hubs than the phone limit.
340 * FIXME: with some proper locking over pipes and session
341 * auto destruction, this could work better.
[1e1b1a9]342 */
[cd5b878]343 int rc = usb_hc_connection_open(&hub_info->connection);
[f35b294]344 if (rc != EOK) {
[cd5b878]345 //usb_pipe_end_session(hub_info->control_pipe);
[f35b294]346 usb_log_error("Failed to open connection to HC: %s.\n",
347 str_error(rc));
348 return rc;
[e93e319]349 }
[195890b]350
[f35b294]351 rc = usb_device_auto_poll(hub_info->usb_device, 0,
352 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
353 NULL, hub_info);
354 if (rc != EOK) {
355 usb_log_error("Failed to create polling fibril: %s.\n",
356 str_error(rc));
357 free(hub_info);
358 return rc;
[cd4b184]359 }
[f35b294]360
[4125b7d]361 usb_log_info("Controlling hub `%s' (%zu ports).\n",
[f35b294]362 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
[3e490eb]363 return EOK;
364}
365
[f35b294]366//*********************************************
367//
368// change handling functions
369//
370//*********************************************
371
[e080332]372
[3dba1ca]373/**
374 * process hub over current change
375 *
376 * This means either to power off the hub or power it on.
377 * @param hub_info hub instance
378 * @param status hub status bitmask
379 * @return error code
380 */
[040ab02]381static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
[f35b294]382 usb_hub_status_t status) {
[040ab02]383 int opResult;
[f35b294]384 if (usb_hub_is_status(status,USB_HUB_FEATURE_HUB_OVER_CURRENT)){
[040ab02]385 opResult = usb_hub_clear_feature(hub_info->control_pipe,
[f35b294]386 USB_HUB_FEATURE_HUB_LOCAL_POWER);
[040ab02]387 if (opResult != EOK) {
388 usb_log_error("cannot power off hub: %d\n",
[f35b294]389 opResult);
[040ab02]390 }
[f35b294]391 } else {
[040ab02]392 opResult = usb_hub_set_feature(hub_info->control_pipe,
[f35b294]393 USB_HUB_FEATURE_HUB_LOCAL_POWER);
[040ab02]394 if (opResult != EOK) {
395 usb_log_error("cannot power on hub: %d\n",
[f35b294]396 opResult);
[040ab02]397 }
398 }
399 return opResult;
400}
401
[3dba1ca]402/**
403 * process hub power change
404 *
405 * If the power has been lost, reestablish it.
406 * If it was reestablished, re-power all ports.
407 * @param hub_info hub instance
408 * @param status hub status bitmask
409 * @return error code
410 */
[040ab02]411static int usb_process_hub_power_change(usb_hub_info_t * hub_info,
[f35b294]412 usb_hub_status_t status) {
[152ec79]413 int opResult = EOK;
[9014dcd]414 if (!usb_hub_is_status(status,USB_HUB_FEATURE_HUB_LOCAL_POWER)) {
[040ab02]415 //restart power on hub
416 opResult = usb_hub_set_feature(hub_info->control_pipe,
[f35b294]417 USB_HUB_FEATURE_HUB_LOCAL_POWER);
[040ab02]418 if (opResult != EOK) {
419 usb_log_error("cannot power on hub: %d\n",
[f35b294]420 opResult);
[040ab02]421 }
[f35b294]422 } else {//power reestablished on hub- restart ports
[3e490eb]423 size_t port;
[9014dcd]424 for (port = 1; port <= hub_info->port_count; ++port) {
[040ab02]425 opResult = usb_hub_set_port_feature(
[f35b294]426 hub_info->control_pipe,
427 port, USB_HUB_FEATURE_PORT_POWER);
[040ab02]428 if (opResult != EOK) {
[4125b7d]429 usb_log_error("Cannot power on port %zu: %s.\n",
430 port, str_error(opResult));
[040ab02]431 }
432 }
[152ec79]433 }
434 if(opResult!=EOK){
435 return opResult;//no feature clearing
436 }
437 opResult = usb_hub_clear_feature(hub_info->control_pipe,
438 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
[9014dcd]439 if (opResult != EOK) {
[152ec79]440 usb_log_error("cannnot clear hub power change flag: "
441 "%d\n",
442 opResult);
[040ab02]443 }
444 return opResult;
445}
446
[3dba1ca]447/**
448 * process hub interrupts
449 *
450 * The change can be either in the over-current condition or
451 * local-power lost condition.
452 * @param hub_info hub instance
453 */
[f35b294]454static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info) {
[040ab02]455 usb_log_debug("global interrupt on a hub\n");
456 usb_pipe_t *pipe = hub_info->control_pipe;
457 int opResult;
458
459 usb_port_status_t status;
460 size_t rcvd_size;
461 usb_device_request_setup_packet_t request;
462 //int opResult;
463 usb_hub_set_hub_status_request(&request);
464 //endpoint 0
465
466 opResult = usb_pipe_control_read(
[f35b294]467 pipe,
468 &request, sizeof (usb_device_request_setup_packet_t),
469 &status, 4, &rcvd_size
470 );
[040ab02]471 if (opResult != EOK) {
472 usb_log_error("could not get hub status\n");
473 return;
474 }
475 if (rcvd_size != sizeof (usb_port_status_t)) {
476 usb_log_error("received status has incorrect size\n");
477 return;
478 }
479 //port reset
[f35b294]480 if (
481 usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
482 usb_process_hub_over_current(hub_info, status);
[040ab02]483 }
[f35b294]484 if (
485 usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
486 usb_process_hub_power_change(hub_info, status);
[040ab02]487 }
488}
489
[e080332]490/**
491 * @}
[71ed4849]492 */
Note: See TracBrowser for help on using the repository browser.