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

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

codelifting

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