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

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

usbhub: Refactoring

Add note about GET_STATUS request.
Make parameters const if possible.

  • 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 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
251 int opResult;
252
253 size_t received_size;
254 opResult = usb_request_get_descriptor(hub_info->control_pipe,
255 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
256 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
257 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
258
259 if (opResult != EOK) {
260 usb_log_error("Failed to receive hub descriptor: %s.\n",
261 str_error(opResult));
262 return opResult;
263 }
264 usb_log_debug2("Parsing descriptor\n");
265 usb_hub_descriptor_t descriptor;
266 opResult = usb_deserialize_hub_desriptor(
267 serialized_descriptor, received_size, &descriptor);
268 if (opResult != EOK) {
269 usb_log_error("Could not parse descriptor: %s\n",
270 str_error(opResult));
271 return opResult;
272 }
273 usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
274 hub_info->port_count = descriptor.ports_count;
275
276 // TODO Why +1 ?
277 hub_info->ports =
278 malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
279 if (!hub_info->ports) {
280 return ENOMEM;
281 }
282
283 size_t port;
284 for (port = 0; port < hub_info->port_count + 1; ++port) {
285 usb_hub_port_init(&hub_info->ports[port]);
286 }
287
288 const bool is_power_switched =
289 !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
290 if (is_power_switched) {
291 usb_log_debug("Hub power switched\n");
292 const bool per_port_power = descriptor.hub_characteristics
293 & HUB_CHAR_POWER_PER_PORT_FLAG;
294
295 for (port = 1; port <= hub_info->port_count; ++port) {
296 usb_log_debug("Powering port %zu.\n", port);
297 opResult = usb_hub_set_port_feature(
298 hub_info->control_pipe,
299 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 if (status & USB_HUB_STATUS_OVER_CURRENT) {
371 /* Over-current detected on one or all ports,
372 * switch them all off to prevent damage. */
373 //TODO Consider ganged power switching here.
374 size_t port;
375 for (port = 1; port <= hub_info->port_count; ++port) {
376 const int opResult = usb_hub_clear_port_feature(
377 hub_info->control_pipe, port,
378 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 hub_info->control_pipe, port,
393 USB_HUB_FEATURE_PORT_POWER);
394 if (opResult != EOK) {
395 usb_log_warning(
396 "HUB OVER-CURRENT GONE: Cannot power on "
397 "port %d; %s\n",
398 port, str_error(opResult));
399 }
400 }
401 }
402}
403/*----------------------------------------------------------------------------*/
404/**
405 * Process hub interrupts.
406 *
407 * The change can be either in the over-current condition or local-power change.
408 * @param hub_info hub instance
409 */
410static void usb_hub_global_interrupt(const usb_hub_info_t *hub_info)
411{
412 assert(hub_info);
413 assert(hub_info->usb_device);
414 usb_log_debug("Global interrupt on a hub\n");
415 usb_pipe_t *ctrlpipe = &hub_info->usb_device->ctrl_pipe;
416
417 usb_hub_status_t status;
418 size_t rcvd_size;
419 /* NOTE: We can't use standard USB GET_STATUS request, because
420 * hubs reply is 4byte instead of 2 */
421 const int opResult = usb_pipe_control_read(
422 ctrlpipe, &get_hub_status_request, sizeof(get_hub_status_request),
423 &status, sizeof(usb_hub_status_t), &rcvd_size);
424 if (opResult != EOK) {
425 usb_log_error("Could not get hub status: %s\n",
426 str_error(opResult));
427 return;
428 }
429 if (rcvd_size != sizeof(usb_hub_status_t)) {
430 usb_log_error("Received status has incorrect size\n");
431 return;
432 }
433
434 /* Handle status changes */
435 if (status & USB_HUB_STATUS_C_OVER_CURRENT)
436 usb_hub_over_current(hub_info, status);
437
438 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
439 /* NOTE: Handling this is more complicated.
440 * If the transition is from bus power to local power, all
441 * is good and we may signal the parent hub that we don't
442 * need the power.
443 * If the transition is from local power to bus power
444 * the hub should turn off all the ports and devices need
445 * to be reinitialized taking into account the limited power
446 * that is now available.
447 * There is no support for power distribution in HelenOS,
448 * (or other OSes/hub devices that I've seen) so this is not
449 * implemented.
450 * Just ACK the change.
451 */
452 const int opResult = usb_hub_clear_feature(ctrlpipe,
453 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
454 if (opResult != EOK) {
455 usb_log_error("Cannot clear hub power change flag: "
456 "%s\n",
457 str_error(opResult));
458 }
459 }
460}
461/*----------------------------------------------------------------------------*/
462/**
463 * callback called from hub polling fibril when the fibril terminates
464 *
465 * Should perform a cleanup - deletes hub_info.
466 * @param device usb device afected
467 * @param was_error indicates that the fibril is stoped due to an error
468 * @param data pointer to usb_hub_info_t structure
469 */
470static void usb_hub_polling_terminated_callback(usb_device_t *device,
471 bool was_error, void *data)
472{
473 usb_hub_info_t *hub = data;
474 assert(hub);
475
476 fibril_mutex_lock(&hub->pending_ops_mutex);
477
478 /* The device is dead. However there might be some pending operations
479 * that we need to wait for.
480 * One of them is device adding in progress.
481 * The respective fibril is probably waiting for status change
482 * in port reset (port enable) callback.
483 * Such change would never come (otherwise we would not be here).
484 * Thus, we would flush all pending port resets.
485 */
486 if (hub->pending_ops_count > 0) {
487 fibril_mutex_lock(&hub->port_mutex);
488 size_t port;
489 for (port = 0; port < hub->port_count; port++) {
490 usb_hub_port_t *the_port = hub->ports + port;
491 fibril_mutex_lock(&the_port->reset_mutex);
492 the_port->reset_completed = true;
493 the_port->reset_okay = false;
494 fibril_condvar_broadcast(&the_port->reset_cv);
495 fibril_mutex_unlock(&the_port->reset_mutex);
496 }
497 fibril_mutex_unlock(&hub->port_mutex);
498 }
499 /* And now wait for them. */
500 while (hub->pending_ops_count > 0) {
501 fibril_condvar_wait(&hub->pending_ops_cv,
502 &hub->pending_ops_mutex);
503 }
504 fibril_mutex_unlock(&hub->pending_ops_mutex);
505
506 usb_device_destroy(hub->usb_device);
507
508 free(hub->ports);
509 free(hub);
510}
511/**
512 * @}
513 */
Note: See TracBrowser for help on using the repository browser.