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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf73a02 was bf73a02, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usbhub: Refactor handling of global hub events.

  • Property mode set to 100644
File size: 15.8 KB
Line 
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 */
28/** @addtogroup drvusbhub
29 * @{
30 */
31/** @file
32 * @brief usb hub main functionality
33 */
34
35#include <ddf/driver.h>
36#include <bool.h>
37#include <errno.h>
38#include <str_error.h>
39#include <inttypes.h>
40#include <stdio.h>
41
42#include <usb/usb.h>
43#include <usb/dev/pipes.h>
44#include <usb/classes/classes.h>
45#include <usb/ddfiface.h>
46#include <usb/descriptor.h>
47#include <usb/dev/recognise.h>
48#include <usb/dev/request.h>
49#include <usb/classes/hub.h>
50#include <usb/dev/poll.h>
51#include <usb_iface.h>
52
53#include "usbhub.h"
54#include "utils.h"
55#include "port_status.h"
56
57#define HUB_FNC_NAME "hub"
58
59/** Standard get hub global status request */
60static const usb_device_request_setup_packet_t get_hub_status_request = {
61 .index = 0,
62 .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
63 .request = USB_HUB_REQUEST_GET_STATUS,
64 .value = 0,
65 .length = sizeof(usb_hub_status_t),
66};
67
68static int usb_set_first_configuration(usb_device_t *usb_device);
69static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
70static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
71static void usb_process_hub_over_current(usb_hub_info_t *hub_info,
72 usb_hub_status_t status);
73static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
74static void usb_hub_polling_terminated_callback(usb_device_t *device,
75 bool was_error, void *data);
76
77
78//*********************************************
79//
80// hub driver code, initialization
81//
82//*********************************************
83
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 */
92int usb_hub_add_device(usb_device_t *usb_dev)
93{
94 assert(usb_dev);
95 /* Create driver soft-state structure */
96 usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev);
97 if (hub_info == NULL) {
98 usb_log_error("Failed to create hun driver structure.\n");
99 return ENOMEM;
100 }
101
102 /* Create hc connection */
103 usb_log_debug("Initializing USB wire abstraction.\n");
104 int opResult = usb_hc_connection_initialize_from_device(
105 &hub_info->connection, hub_info->usb_device->ddf_dev);
106 if (opResult != EOK) {
107 usb_log_error("Could not initialize connection to device: %s\n",
108 str_error(opResult));
109 free(hub_info);
110 return opResult;
111 }
112
113 /* Set hub's first configuration. (There should be only one) */
114 opResult = usb_set_first_configuration(usb_dev);
115 if (opResult != EOK) {
116 usb_log_error("Could not set hub configuration: %s\n",
117 str_error(opResult));
118 free(hub_info);
119 return opResult;
120 }
121
122 //get port count and create attached_devs
123 opResult = usb_hub_process_hub_specific_info(hub_info);
124 if (opResult != EOK) {
125 usb_log_error("Could process hub specific info, %s\n",
126 str_error(opResult));
127 free(hub_info);
128 return opResult;
129 }
130
131 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
132 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
133 fun_exposed, HUB_FNC_NAME);
134 if (hub_fun == NULL) {
135 usb_log_error("Failed to create hub function.\n");
136 free(hub_info);
137 return ENOMEM;
138 }
139
140 opResult = ddf_fun_bind(hub_fun);
141 if (opResult != EOK) {
142 usb_log_error("Failed to bind hub function: %s.\n",
143 str_error(opResult));
144 free(hub_info);
145 ddf_fun_destroy(hub_fun);
146 return opResult;
147 }
148
149 opResult = usb_device_auto_poll(hub_info->usb_device, 0,
150 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
151 usb_hub_polling_terminated_callback, hub_info);
152 if (opResult != EOK) {
153 ddf_fun_destroy(hub_fun);
154 free(hub_info);
155 usb_log_error("Failed to create polling fibril: %s.\n",
156 str_error(opResult));
157 return opResult;
158 }
159 usb_log_info("Controlling hub '%s' (%zu ports).\n",
160 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
161
162 return EOK;
163}
164
165/** Callback for polling hub for changes.
166 *
167 * @param dev Device where the change occured.
168 * @param change_bitmap Bitmap of changed ports.
169 * @param change_bitmap_size Size of the bitmap in bytes.
170 * @param arg Custom argument, points to @c usb_hub_info_t.
171 * @return Whether to continue polling.
172 */
173bool hub_port_changes_callback(usb_device_t *dev,
174 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
175{
176 usb_log_debug("hub_port_changes_callback\n");
177 usb_hub_info_t *hub = arg;
178
179 /* FIXME: check that we received enough bytes. */
180 if (change_bitmap_size == 0) {
181 goto leave;
182 }
183
184 /* Lowest bit indicates global change */
185 const bool change = change_bitmap[0] & 1;
186 if (change) {
187 usb_hub_process_global_interrupt(hub);
188 }
189
190 /* N + 1 bit indicates change on port N */
191 size_t port = 1;
192 for (; port < hub->port_count + 1; port++) {
193 const bool change = (change_bitmap[port / 8] >> (port % 8)) & 1;
194 if (change) {
195 usb_hub_process_port_interrupt(hub, port);
196 }
197 }
198leave:
199 /* FIXME: proper interval. */
200 // TODO Interval should be handled by USB HC scheduler not here
201 async_usleep(1000 * 250);
202
203 return true;
204}
205
206
207//*********************************************
208//
209// support functions
210//
211//*********************************************
212
213/**
214 * create usb_hub_info_t structure
215 *
216 * Does only basic copying of known information into new structure.
217 * @param usb_dev usb device structure
218 * @return basic usb_hub_info_t structure
219 */
220static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev)
221{
222 assert(usb_dev);
223 usb_hub_info_t *result = malloc(sizeof(usb_hub_info_t));
224 if (!result)
225 return NULL;
226
227 result->usb_device = usb_dev;
228 result->control_pipe = &usb_dev->ctrl_pipe;
229 result->is_default_address_used = false;
230
231 result->ports = NULL;
232 result->port_count = -1;
233 fibril_mutex_initialize(&result->port_mutex);
234 fibril_mutex_initialize(&result->pending_ops_mutex);
235 fibril_condvar_initialize(&result->pending_ops_cv);
236 result->pending_ops_count = 0;
237
238 return result;
239}
240
241/**
242 * Load hub-specific information into hub_info structure and process if needed
243 *
244 * Particularly read port count and initialize structure holding port
245 * information. If there are non-removable devices, start initializing them.
246 * This function is hub-specific and should be run only after the hub is
247 * configured using usb_set_first_configuration function.
248 * @param hub_info hub representation
249 * @return error code
250 */
251int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info)
252{
253 assert(hub_info);
254 // get hub descriptor
255 usb_log_debug("Retrieving descriptor\n");
256 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
257 int opResult;
258
259 size_t received_size;
260 opResult = usb_request_get_descriptor(hub_info->control_pipe,
261 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
262 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
263 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
264
265 if (opResult != EOK) {
266 usb_log_error("Failed to receive hub descriptor: %s.\n",
267 str_error(opResult));
268 return opResult;
269 }
270 usb_log_debug2("Parsing descriptor\n");
271 usb_hub_descriptor_t descriptor;
272 opResult = usb_deserialize_hub_desriptor(
273 serialized_descriptor, received_size, &descriptor);
274 if (opResult != EOK) {
275 usb_log_error("Could not parse descriptor: %s\n",
276 str_error(opResult));
277 return opResult;
278 }
279 usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
280 hub_info->port_count = descriptor.ports_count;
281
282 // TODO Why +1 ?
283 hub_info->ports =
284 malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
285 if (!hub_info->ports) {
286 return ENOMEM;
287 }
288
289 size_t port;
290 for (port = 0; port < hub_info->port_count + 1; ++port) {
291 usb_hub_port_init(&hub_info->ports[port]);
292 }
293
294 const bool is_power_switched =
295 !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
296 if (is_power_switched) {
297 usb_log_debug("Hub power switched\n");
298 const bool per_port_power = descriptor.hub_characteristics
299 & HUB_CHAR_POWER_PER_PORT_FLAG;
300
301 for (port = 1; port <= hub_info->port_count; ++port) {
302 usb_log_debug("Powering port %zu.\n", port);
303 opResult = usb_hub_set_port_feature(
304 hub_info->control_pipe,
305 port, USB_HUB_FEATURE_PORT_POWER);
306 if (opResult != EOK) {
307 usb_log_error("Cannot power on port %zu: %s.\n",
308 port, str_error(opResult));
309 } else {
310 if (!per_port_power) {
311 usb_log_debug(
312 "Ganged power switching mode, "
313 "one port is enough.\n");
314 break;
315 }
316 }
317 }
318
319 } else {
320 usb_log_debug("Power not switched, ports always powered\n");
321 }
322 return EOK;
323}
324
325/**
326 * Set configuration of and USB device
327 *
328 * Check whether there is at least one configuration and sets the first one.
329 * This function should be run prior to running any hub-specific action.
330 * @param usb_device usb device representation
331 * @return error code
332 */
333static int usb_set_first_configuration(usb_device_t *usb_device)
334{
335 assert(usb_device);
336 /* Get number of possible configurations from device descriptor */
337 const size_t configuration_count =
338 usb_device->descriptors.device.configuration_count;
339 usb_log_debug("Hub has %d configurations.\n", configuration_count);
340
341 if (configuration_count < 1) {
342 usb_log_error("There are no configurations available\n");
343 return EINVAL;
344 }
345
346 // TODO: Make sure that there is enough data and the cast is correct
347 usb_standard_configuration_descriptor_t *config_descriptor
348 = (usb_standard_configuration_descriptor_t *)
349 usb_device->descriptors.configuration;
350
351 /* Set configuration. Use the configuration that was in
352 * usb_device->descriptors.configuration */
353 const int opResult = usb_request_set_configuration(
354 &usb_device->ctrl_pipe, config_descriptor->configuration_number);
355 if (opResult != EOK) {
356 usb_log_error("Failed to set hub configuration: %s.\n",
357 str_error(opResult));
358 return opResult;
359 }
360 usb_log_debug("\tUsed configuration %d\n",
361 config_descriptor->configuration_number);
362
363 return EOK;
364}
365
366//*********************************************
367//
368// change handling functions
369//
370//*********************************************
371
372/**
373 * process hub over current change
374 *
375 * This means either to power off the hub or power it on.
376 * @param hub_info hub instance
377 * @param status hub status bitmask
378 * @return error code
379 */
380static void usb_process_hub_over_current(usb_hub_info_t *hub_info,
381 usb_hub_status_t status)
382{
383 if (status & USB_HUB_STATUS_OVER_CURRENT) {
384 /* Over-current detected on one or all ports,
385 * switch them all off to prevent damage. */
386 //TODO Consider ganged power switching here.
387 size_t port;
388 for (port = 1; port <= hub_info->port_count; ++port) {
389 const int opResult = usb_hub_clear_port_feature(
390 hub_info->control_pipe, port,
391 USB_HUB_FEATURE_PORT_POWER);
392 if (opResult != EOK) {
393 usb_log_warning(
394 "HUB OVER-CURRENT: Cannot power off port"
395 " %d: %s\n",
396 port, str_error(opResult));
397 }
398 }
399 } else {
400 /* Over-current condition is gone, it is safe to turn the
401 * ports on. */
402 size_t port;
403 for (port = 1; port <= hub_info->port_count; ++port) {
404 const int opResult = usb_hub_set_port_feature(
405 hub_info->control_pipe, port,
406 USB_HUB_FEATURE_PORT_POWER);
407 if (opResult != EOK) {
408 usb_log_warning(
409 "HUB OVER-CURRENT GONE: Cannot power on "
410 "port %d; %s\n",
411 port, str_error(opResult));
412 }
413 }
414 }
415}
416/*----------------------------------------------------------------------------*/
417/**
418 * process hub interrupts
419 *
420 * The change can be either in the over-current condition or
421 * local-power change.
422 * @param hub_info hub instance
423 */
424static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info)
425{
426 assert(hub_info);
427 assert(hub_info->usb_device);
428 usb_log_debug("Global interrupt on a hub\n");
429 usb_pipe_t *ctrlpipe = &hub_info->usb_device->ctrl_pipe;
430 int opResult;
431
432 usb_hub_status_t status;
433 size_t rcvd_size;
434
435 opResult = usb_pipe_control_read(
436 ctrlpipe, &get_hub_status_request, sizeof(get_hub_status_request),
437 &status, sizeof(usb_hub_status_t), &rcvd_size);
438 if (opResult != EOK) {
439 usb_log_error("Could not get hub status: %s\n",
440 str_error(opResult));
441 return;
442 }
443 if (rcvd_size != sizeof(usb_hub_status_t)) {
444 usb_log_error("Received status has incorrect size\n");
445 return;
446 }
447
448 /* Handle status changes */
449 if (status & USB_HUB_STATUS_C_OVER_CURRENT)
450 usb_process_hub_over_current(hub_info, status);
451
452 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
453 /* NOTE: Handling this is more complicated.
454 * If the transition is from bus power to local power, all
455 * is good and we may signal the parent hub that we don't
456 * need the power.
457 * If the transition is from local power to bus power
458 * the hub should turn off all the ports and devices need
459 * to be reinitialized taking into account the limited power
460 * that is now available.
461 * There is no support for power distribution in HelenOS,
462 * (or other OSes/hub devices that I've seen) so this is not
463 * implemented.
464 * Just ACK the change.
465 */
466 const int opResult = usb_hub_clear_feature(ctrlpipe,
467 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
468 if (opResult != EOK) {
469 usb_log_error("Cannot clear hub power change flag: "
470 "%s\n",
471 str_error(opResult));
472 }
473 }
474}
475
476/**
477 * callback called from hub polling fibril when the fibril terminates
478 *
479 * Should perform a cleanup - deletes hub_info.
480 * @param device usb device afected
481 * @param was_error indicates that the fibril is stoped due to an error
482 * @param data pointer to usb_hub_info_t structure
483 */
484static void usb_hub_polling_terminated_callback(usb_device_t *device,
485 bool was_error, void *data) {
486 usb_hub_info_t * hub = data;
487 assert(hub);
488
489 fibril_mutex_lock(&hub->pending_ops_mutex);
490
491 /* The device is dead. However there might be some pending operations
492 * that we need to wait for.
493 * One of them is device adding in progress.
494 * The respective fibril is probably waiting for status change
495 * in port reset (port enable) callback.
496 * Such change would never come (otherwise we would not be here).
497 * Thus, we would flush all pending port resets.
498 */
499 if (hub->pending_ops_count > 0) {
500 fibril_mutex_lock(&hub->port_mutex);
501 size_t port;
502 for (port = 0; port < hub->port_count; port++) {
503 usb_hub_port_t *the_port = hub->ports + port;
504 fibril_mutex_lock(&the_port->reset_mutex);
505 the_port->reset_completed = true;
506 the_port->reset_okay = false;
507 fibril_condvar_broadcast(&the_port->reset_cv);
508 fibril_mutex_unlock(&the_port->reset_mutex);
509 }
510 fibril_mutex_unlock(&hub->port_mutex);
511 }
512 /* And now wait for them. */
513 while (hub->pending_ops_count > 0) {
514 fibril_condvar_wait(&hub->pending_ops_cv,
515 &hub->pending_ops_mutex);
516 }
517 fibril_mutex_unlock(&hub->pending_ops_mutex);
518
519 usb_device_destroy(hub->usb_device);
520
521 free(hub->ports);
522 free(hub);
523}
524
525
526
527
528/**
529 * @}
530 */
Note: See TracBrowser for help on using the repository browser.