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

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

usbhub: Rename usb_hub_info ⇒ usb_hub_dev.

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