source: mainline/uspace/drv/usbhub/usbhub.c@ 44b1674

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

merge form usb/development

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