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

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

usbhub: Remove connected devices before confirming device gone.

  • Property mode set to 100644
File size: 16.9 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup drvusbhub
30 * @{
31 */
32/** @file
33 * @brief usb hub main functionality
34 */
35
36#include <ddf/driver.h>
37#include <bool.h>
38#include <errno.h>
39#include <str_error.h>
40#include <inttypes.h>
41#include <stdio.h>
42
43#include <usb/usb.h>
44#include <usb/debug.h>
45#include <usb/dev/pipes.h>
46#include <usb/classes/classes.h>
47#include <usb/ddfiface.h>
48#include <usb/descriptor.h>
49#include <usb/dev/recognise.h>
50#include <usb/dev/request.h>
51#include <usb/classes/hub.h>
52#include <usb/dev/poll.h>
53#include <usb_iface.h>
54
55#include "usbhub.h"
56#include "status.h"
57
58#define HUB_FNC_NAME "hub"
59
60/** Standard get hub global status request */
61static const usb_device_request_setup_packet_t get_hub_status_request = {
62 .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
63 .request = USB_HUB_REQUEST_GET_STATUS,
64 .index = 0,
65 .value = 0,
66 .length = sizeof(usb_hub_status_t),
67};
68
69static int usb_set_first_configuration(usb_device_t *usb_device);
70static usb_hub_dev_t * usb_hub_dev_create(usb_device_t *usb_dev);
71static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
72static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
73 usb_hub_status_t status);
74static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
75static void usb_hub_polling_terminated_callback(usb_device_t *device,
76 bool was_error, void *data);
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_device_gone(usb_device_t *usb_dev)
86{
87 assert(usb_dev);
88 usb_hub_dev_t *hub = usb_dev->driver_data;
89 assert(hub);
90 unsigned tries = 10;
91 while (hub->running) {
92 async_usleep(100000);
93 if (!tries--) {
94 usb_log_error("Can't remove hub, still running.\n");
95 return EINPROGRESS;
96 }
97 }
98
99 assert(!hub->running);
100
101 for (size_t port = 0; port < hub->port_count; ++port) {
102 if (hub->ports[port].attached_device.fun) {
103 const int ret =
104 usb_hub_port_fini(&hub->ports[port], hub);
105 if (ret != EOK)
106 return ret;
107 }
108 }
109 free(hub->ports);
110
111 const int ret = ddf_fun_unbind(hub->hub_fun);
112 if (ret != EOK) {
113 usb_log_error("Failed to unbind '%s' function: %s.\n",
114 HUB_FNC_NAME, str_error(ret));
115 return ret;
116 }
117 ddf_fun_destroy(hub->hub_fun);
118
119 free(hub);
120 usb_dev->driver_data = NULL;
121 usb_log_info("USB hub driver, stopped and cleaned.\n");
122 return EOK;
123}
124/*----------------------------------------------------------------------------*/
125/**
126 * Initialize hub device driver fibril
127 *
128 * Creates hub representation and fibril that periodically checks hub's status.
129 * Hub representation is passed to the fibril.
130 * @param usb_dev generic usb device information
131 * @return error code
132 */
133int usb_hub_device_add(usb_device_t *usb_dev)
134{
135 assert(usb_dev);
136 /* Create driver soft-state structure */
137 usb_hub_dev_t *hub_dev = usb_hub_dev_create(usb_dev);
138 if (hub_dev == NULL) {
139 usb_log_error("Failed to create hun driver structure.\n");
140 return ENOMEM;
141 }
142
143 /* Create hc connection */
144 usb_log_debug("Initializing USB wire abstraction.\n");
145 int opResult = usb_hc_connection_initialize_from_device(
146 &hub_dev->connection, hub_dev->usb_device->ddf_dev);
147 if (opResult != EOK) {
148 usb_log_error("Could not initialize connection to device: %s\n",
149 str_error(opResult));
150 free(hub_dev);
151 return opResult;
152 }
153
154 /* Set hub's first configuration. (There should be only one) */
155 opResult = usb_set_first_configuration(usb_dev);
156 if (opResult != EOK) {
157 usb_log_error("Could not set hub configuration: %s\n",
158 str_error(opResult));
159 free(hub_dev);
160 return opResult;
161 }
162
163 /* Get port count and create attached_devices. */
164 opResult = usb_hub_process_hub_specific_info(hub_dev);
165 if (opResult != EOK) {
166 usb_log_error("Could process hub specific info, %s\n",
167 str_error(opResult));
168 free(hub_dev);
169 return opResult;
170 }
171
172 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
173 hub_dev->hub_fun = ddf_fun_create(hub_dev->usb_device->ddf_dev,
174 fun_exposed, HUB_FNC_NAME);
175 if (hub_dev->hub_fun == NULL) {
176 usb_log_error("Failed to create hub function.\n");
177 free(hub_dev);
178 return ENOMEM;
179 }
180
181 opResult = ddf_fun_bind(hub_dev->hub_fun);
182 if (opResult != EOK) {
183 usb_log_error("Failed to bind hub function: %s.\n",
184 str_error(opResult));
185 free(hub_dev);
186 ddf_fun_destroy(hub_dev->hub_fun);
187 return opResult;
188 }
189
190 opResult = usb_device_auto_poll(hub_dev->usb_device, 0,
191 hub_port_changes_callback, ((hub_dev->port_count + 1 + 8) / 8),
192 usb_hub_polling_terminated_callback, hub_dev);
193 if (opResult != EOK) {
194 /* Function is already bound */
195 ddf_fun_unbind(hub_dev->hub_fun);
196 ddf_fun_destroy(hub_dev->hub_fun);
197 free(hub_dev);
198 usb_log_error("Failed to create polling fibril: %s.\n",
199 str_error(opResult));
200 return opResult;
201 }
202 hub_dev->running = true;
203 usb_log_info("Controlling hub '%s' (%zu ports).\n",
204 hub_dev->usb_device->ddf_dev->name, hub_dev->port_count);
205
206 return EOK;
207}
208/*----------------------------------------------------------------------------*/
209/** Callback for polling hub for changes.
210 *
211 * @param dev Device where the change occured.
212 * @param change_bitmap Bitmap of changed ports.
213 * @param change_bitmap_size Size of the bitmap in bytes.
214 * @param arg Custom argument, points to @c usb_hub_dev_t.
215 * @return Whether to continue polling.
216 */
217bool hub_port_changes_callback(usb_device_t *dev,
218 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
219{
220 usb_log_debug("hub_port_changes_callback\n");
221 usb_hub_dev_t *hub = arg;
222 assert(hub);
223
224 /* It is an error condition if we didn't receive enough data */
225 if (change_bitmap_size == 0) {
226 return false;
227 }
228
229 /* Lowest bit indicates global change */
230 const bool change = change_bitmap[0] & 1;
231 if (change) {
232 usb_hub_global_interrupt(hub);
233 }
234
235 /* N + 1 bit indicates change on port N */
236 for (size_t port = 0; port < hub->port_count + 1; port++) {
237 const size_t bit = port + 1;
238 const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
239 if (change) {
240 usb_hub_port_process_interrupt(&hub->ports[port], hub);
241 }
242 }
243 return true;
244}
245/*----------------------------------------------------------------------------*/
246/**
247 * create usb_hub_dev_t structure
248 *
249 * Does only basic copying of known information into new structure.
250 * @param usb_dev usb device structure
251 * @return basic usb_hub_dev_t structure
252 */
253static usb_hub_dev_t * usb_hub_dev_create(usb_device_t *usb_dev)
254{
255 assert(usb_dev);
256 usb_hub_dev_t *hub_dev = malloc(sizeof(usb_hub_dev_t));
257 if (!hub_dev)
258 return NULL;
259
260 hub_dev->usb_device = usb_dev;
261
262 hub_dev->ports = NULL;
263 hub_dev->port_count = 0;
264 hub_dev->pending_ops_count = 0;
265 hub_dev->running = false;
266 fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
267 fibril_condvar_initialize(&hub_dev->pending_ops_cv);
268 usb_dev->driver_data = hub_dev;
269
270 return hub_dev;
271}
272/*----------------------------------------------------------------------------*/
273/**
274 * Load hub-specific information into hub_dev structure and process if needed
275 *
276 * Read port count and initialize structures holding per port information.
277 * If there are any non-removable devices, start initializing them.
278 * This function is hub-specific and should be run only after the hub is
279 * configured using usb_set_first_configuration function.
280 * @param hub_dev hub representation
281 * @return error code
282 */
283static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
284{
285 assert(hub_dev);
286
287 /* Get hub descriptor. */
288 usb_log_debug("Retrieving descriptor\n");
289 usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
290
291 usb_hub_descriptor_header_t descriptor;
292 size_t received_size;
293 int opResult = usb_request_get_descriptor(control_pipe,
294 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
295 USB_DESCTYPE_HUB, 0, 0, &descriptor,
296 sizeof(usb_hub_descriptor_header_t), &received_size);
297 if (opResult != EOK) {
298 usb_log_error("Failed to receive hub descriptor: %s.\n",
299 str_error(opResult));
300 return opResult;
301 }
302
303 usb_log_debug("Setting port count to %d.\n", descriptor.port_count);
304 hub_dev->port_count = descriptor.port_count;
305
306 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
307 if (!hub_dev->ports) {
308 return ENOMEM;
309 }
310
311 for (size_t port = 0; port < hub_dev->port_count; ++port) {
312 usb_hub_port_init(
313 &hub_dev->ports[port], port + 1, control_pipe);
314 }
315
316 const bool is_power_switched =
317 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
318 if (is_power_switched) {
319 usb_log_debug("Hub power switched\n");
320 const bool per_port_power = descriptor.characteristics
321 & HUB_CHAR_POWER_PER_PORT_FLAG;
322
323 for (size_t port = 0; port < hub_dev->port_count; ++port) {
324 usb_log_debug("Powering port %zu.\n", port);
325 opResult = usb_hub_port_set_feature(
326 &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
327 if (opResult != EOK) {
328 usb_log_error("Cannot power on port %zu: %s.\n",
329 port, str_error(opResult));
330 } else {
331 if (!per_port_power) {
332 usb_log_debug(
333 "Ganged power switching mode, "
334 "one port is enough.\n");
335 break;
336 }
337 }
338 }
339 } else {
340 usb_log_debug("Power not switched, ports always powered\n");
341 }
342 return EOK;
343}
344/*----------------------------------------------------------------------------*/
345/**
346 * Set configuration of and USB device
347 *
348 * Check whether there is at least one configuration and sets the first one.
349 * This function should be run prior to running any hub-specific action.
350 * @param usb_device usb device representation
351 * @return error code
352 */
353static int usb_set_first_configuration(usb_device_t *usb_device)
354{
355 assert(usb_device);
356 /* Get number of possible configurations from device descriptor */
357 const size_t configuration_count =
358 usb_device->descriptors.device.configuration_count;
359 usb_log_debug("Hub has %zu configurations.\n", configuration_count);
360
361 if (configuration_count < 1) {
362 usb_log_error("There are no configurations available\n");
363 return EINVAL;
364 }
365
366 if (usb_device->descriptors.configuration_size
367 < sizeof(usb_standard_configuration_descriptor_t)) {
368 usb_log_error("Configuration descriptor is not big enough"
369 " to fit standard configuration descriptor.\n");
370 return EOVERFLOW;
371 }
372
373 // TODO: Make sure that the cast is correct
374 usb_standard_configuration_descriptor_t *config_descriptor
375 = (usb_standard_configuration_descriptor_t *)
376 usb_device->descriptors.configuration;
377
378 /* Set configuration. Use the configuration that was in
379 * usb_device->descriptors.configuration i.e. The first one. */
380 const int opResult = usb_request_set_configuration(
381 &usb_device->ctrl_pipe, config_descriptor->configuration_number);
382 if (opResult != EOK) {
383 usb_log_error("Failed to set hub configuration: %s.\n",
384 str_error(opResult));
385 } else {
386 usb_log_debug("\tUsed configuration %d\n",
387 config_descriptor->configuration_number);
388 }
389 return opResult;
390}
391/*----------------------------------------------------------------------------*/
392/**
393 * Process hub over current change
394 *
395 * This means either to power off the hub or power it on.
396 * @param hub_dev hub instance
397 * @param status hub status bitmask
398 * @return error code
399 */
400static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
401 usb_hub_status_t status)
402{
403 if (status & USB_HUB_STATUS_OVER_CURRENT) {
404 /* Hub should remove power from all ports if it detects OC */
405 usb_log_warning("Detected hub over-current condition, "
406 "all ports should be powered off.");
407 } else {
408 /* Over-current condition is gone, it is safe to turn the
409 * ports on. */
410 for (size_t port = 0; port < hub_dev->port_count; ++port) {
411 const int opResult = usb_hub_port_set_feature(
412 &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
413 // TODO: consider power policy here
414 if (opResult != EOK) {
415 usb_log_warning(
416 "HUB OVER-CURRENT GONE: Cannot power on "
417 "port %zu; %s\n",
418 port, str_error(opResult));
419 }
420 }
421 }
422 const int opResult = usb_request_clear_feature(
423 &hub_dev->usb_device->ctrl_pipe, USB_REQUEST_TYPE_CLASS,
424 USB_REQUEST_RECIPIENT_DEVICE,
425 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
426 if (opResult != EOK) {
427 usb_log_error(
428 "Failed to clear hub over-current change flag: %s.\n",
429 str_error(opResult));
430 }
431}
432/*----------------------------------------------------------------------------*/
433/**
434 * Process hub interrupts.
435 *
436 * The change can be either in the over-current condition or local-power change.
437 * @param hub_dev hub instance
438 */
439static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
440{
441 assert(hub_dev);
442 assert(hub_dev->usb_device);
443 usb_log_debug("Global interrupt on a hub\n");
444 usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
445
446 usb_hub_status_t status;
447 size_t rcvd_size;
448 /* NOTE: We can't use standard USB GET_STATUS request, because
449 * hubs reply is 4byte instead of 2 */
450 const int opResult = usb_pipe_control_read(control_pipe,
451 &get_hub_status_request, sizeof(get_hub_status_request),
452 &status, sizeof(usb_hub_status_t), &rcvd_size);
453 if (opResult != EOK) {
454 usb_log_error("Could not get hub status: %s\n",
455 str_error(opResult));
456 return;
457 }
458 if (rcvd_size != sizeof(usb_hub_status_t)) {
459 usb_log_error("Received status has incorrect size\n");
460 return;
461 }
462
463 /* Handle status changes */
464 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
465 usb_hub_over_current(hub_dev, status);
466 }
467
468 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
469 /* NOTE: Handling this is more complicated.
470 * If the transition is from bus power to local power, all
471 * is good and we may signal the parent hub that we don't
472 * need the power.
473 * If the transition is from local power to bus power
474 * the hub should turn off all the ports and devices need
475 * to be reinitialized taking into account the limited power
476 * that is now available.
477 * There is no support for power distribution in HelenOS,
478 * (or other OSes/hub devices that I've seen) so this is not
479 * implemented.
480 * Just ACK the change.
481 */
482 const int opResult = usb_request_clear_feature(
483 control_pipe, USB_REQUEST_TYPE_CLASS,
484 USB_REQUEST_RECIPIENT_DEVICE,
485 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
486 if (opResult != EOK) {
487 usb_log_error(
488 "Failed to clear hub power change flag: %s.\n",
489 str_error(opResult));
490 }
491 }
492}
493/*----------------------------------------------------------------------------*/
494/**
495 * callback called from hub polling fibril when the fibril terminates
496 *
497 * Does not perform cleanup, just marks the hub as not running.
498 * @param device usb device afected
499 * @param was_error indicates that the fibril is stoped due to an error
500 * @param data pointer to usb_hub_dev_t structure
501 */
502static void usb_hub_polling_terminated_callback(usb_device_t *device,
503 bool was_error, void *data)
504{
505 usb_hub_dev_t *hub = data;
506 assert(hub);
507
508 fibril_mutex_lock(&hub->pending_ops_mutex);
509
510 /* The device is dead. However there might be some pending operations
511 * that we need to wait for.
512 * One of them is device adding in progress.
513 * The respective fibril is probably waiting for status change
514 * in port reset (port enable) callback.
515 * Such change would never come (otherwise we would not be here).
516 * Thus, we would flush all pending port resets.
517 */
518 if (hub->pending_ops_count > 0) {
519 for (size_t port = 0; port < hub->port_count; ++port) {
520 usb_hub_port_reset_fail(&hub->ports[port]);
521 }
522 }
523 /* And now wait for them. */
524 while (hub->pending_ops_count > 0) {
525 fibril_condvar_wait(&hub->pending_ops_cv,
526 &hub->pending_ops_mutex);
527 }
528 fibril_mutex_unlock(&hub->pending_ops_mutex);
529 hub->running = false;
530}
531/**
532 * @}
533 */
Note: See TracBrowser for help on using the repository browser.