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

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

usbhub: Reduce the use of usb_info_t.control_pipe

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