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

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

usbhub: Use generic device requests for hub device features.

According to the specs. ClearHubFeature is equivalent to class specific device request ClearFeature.
Remove unused code.

  • Property mode set to 100644
File size: 16.1 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 .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
62 .request = USB_HUB_REQUEST_GET_STATUS,
63 .index = 0,
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 * create usb_hub_info_t structure
201 *
202 * Does only basic copying of known information into new structure.
203 * @param usb_dev usb device structure
204 * @return basic usb_hub_info_t structure
205 */
206static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev)
207{
208 assert(usb_dev);
209 usb_hub_info_t *info = malloc(sizeof(usb_hub_info_t));
210 if (!info)
211 return NULL;
212
213 info->usb_device = usb_dev;
214 info->control_pipe = &usb_dev->ctrl_pipe;
215
216 info->ports = NULL;
217 info->port_count = -1;
218 fibril_mutex_initialize(&info->port_mutex);
219 fibril_mutex_initialize(&info->pending_ops_mutex);
220 fibril_condvar_initialize(&info->pending_ops_cv);
221 info->pending_ops_count = 0;
222
223 return info;
224}
225/*----------------------------------------------------------------------------*/
226/**
227 * Load hub-specific information into hub_info structure and process if needed
228 *
229 * Read port count and initialize structures holding per port information.
230 * If there are any non-removable devices, start initializing them.
231 * This function is hub-specific and should be run only after the hub is
232 * configured using usb_set_first_configuration function.
233 * @param hub_info hub representation
234 * @return error code
235 */
236static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info)
237{
238 assert(hub_info);
239
240 /* Get hub descriptor. */
241 usb_log_debug("Retrieving descriptor\n");
242 usb_pipe_t *control_pipe = &hub_info->usb_device->ctrl_pipe;
243 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
244 int opResult;
245
246 size_t received_size;
247 opResult = usb_request_get_descriptor(control_pipe,
248 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
249 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
250 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
251
252 if (opResult != EOK) {
253 usb_log_error("Failed to receive hub descriptor: %s.\n",
254 str_error(opResult));
255 return opResult;
256 }
257 usb_log_debug2("Parsing descriptor\n");
258 usb_hub_descriptor_t descriptor;
259 opResult = usb_deserialize_hub_desriptor(
260 serialized_descriptor, received_size, &descriptor);
261 if (opResult != EOK) {
262 usb_log_error("Could not parse descriptor: %s\n",
263 str_error(opResult));
264 return opResult;
265 }
266 usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
267 hub_info->port_count = descriptor.ports_count;
268
269 // TODO Why +1 ?
270 hub_info->ports =
271 malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
272 if (!hub_info->ports) {
273 return ENOMEM;
274 }
275
276 size_t port;
277 for (port = 0; port < hub_info->port_count + 1; ++port) {
278 usb_hub_port_init(&hub_info->ports[port]);
279 }
280
281 const bool is_power_switched =
282 !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
283 if (is_power_switched) {
284 usb_log_debug("Hub power switched\n");
285 const bool per_port_power = descriptor.hub_characteristics
286 & HUB_CHAR_POWER_PER_PORT_FLAG;
287
288 for (port = 1; port <= hub_info->port_count; ++port) {
289 usb_log_debug("Powering port %zu.\n", port);
290 opResult = usb_hub_set_port_feature(
291 control_pipe, port, USB_HUB_FEATURE_PORT_POWER);
292 if (opResult != EOK) {
293 usb_log_error("Cannot power on port %zu: %s.\n",
294 port, str_error(opResult));
295 } else {
296 if (!per_port_power) {
297 usb_log_debug(
298 "Ganged power switching mode, "
299 "one port is enough.\n");
300 break;
301 }
302 }
303 }
304
305 } else {
306 usb_log_debug("Power not switched, ports always powered\n");
307 }
308 return EOK;
309}
310/*----------------------------------------------------------------------------*/
311/**
312 * Set configuration of and USB device
313 *
314 * Check whether there is at least one configuration and sets the first one.
315 * This function should be run prior to running any hub-specific action.
316 * @param usb_device usb device representation
317 * @return error code
318 */
319static int usb_set_first_configuration(usb_device_t *usb_device)
320{
321 assert(usb_device);
322 /* Get number of possible configurations from device descriptor */
323 const size_t configuration_count =
324 usb_device->descriptors.device.configuration_count;
325 usb_log_debug("Hub has %d configurations.\n", configuration_count);
326
327 if (configuration_count < 1) {
328 usb_log_error("There are no configurations available\n");
329 return EINVAL;
330 }
331
332 // TODO: Make sure that there is enough data and the cast is correct
333 usb_standard_configuration_descriptor_t *config_descriptor
334 = (usb_standard_configuration_descriptor_t *)
335 usb_device->descriptors.configuration;
336
337 /* Set configuration. Use the configuration that was in
338 * usb_device->descriptors.configuration i.e. The first one. */
339 const int opResult = usb_request_set_configuration(
340 &usb_device->ctrl_pipe, config_descriptor->configuration_number);
341 if (opResult != EOK) {
342 usb_log_error("Failed to set hub configuration: %s.\n",
343 str_error(opResult));
344 } else {
345 usb_log_debug("\tUsed configuration %d\n",
346 config_descriptor->configuration_number);
347 }
348 return opResult;
349}
350/*----------------------------------------------------------------------------*/
351/**
352 * Process hub over current change
353 *
354 * This means either to power off the hub or power it on.
355 * @param hub_info hub instance
356 * @param status hub status bitmask
357 * @return error code
358 */
359static void usb_hub_over_current(const usb_hub_info_t *hub_info,
360 usb_hub_status_t status)
361{
362 usb_pipe_t *control_pipe = &hub_info->usb_device->ctrl_pipe;
363 if (status & USB_HUB_STATUS_OVER_CURRENT) {
364 /* Over-current detected on one or all ports,
365 * switch them all off to prevent damage. */
366 //TODO Consider ganged power switching here.
367 size_t port;
368 for (port = 1; port <= hub_info->port_count; ++port) {
369 const int opResult = usb_hub_clear_port_feature(
370 control_pipe, port, USB_HUB_FEATURE_PORT_POWER);
371 if (opResult != EOK) {
372 usb_log_warning(
373 "HUB OVER-CURRENT: Cannot power off port"
374 " %d: %s\n",
375 port, str_error(opResult));
376 }
377 }
378 } else {
379 /* Over-current condition is gone, it is safe to turn the
380 * ports on. */
381 size_t port;
382 for (port = 1; port <= hub_info->port_count; ++port) {
383 const int opResult = usb_hub_set_port_feature(
384 control_pipe, port, USB_HUB_FEATURE_PORT_POWER);
385 if (opResult != EOK) {
386 usb_log_warning(
387 "HUB OVER-CURRENT GONE: Cannot power on "
388 "port %d; %s\n",
389 port, str_error(opResult));
390 }
391 }
392 }
393}
394/*----------------------------------------------------------------------------*/
395/**
396 * Process hub interrupts.
397 *
398 * The change can be either in the over-current condition or local-power change.
399 * @param hub_info hub instance
400 */
401static void usb_hub_global_interrupt(const usb_hub_info_t *hub_info)
402{
403 assert(hub_info);
404 assert(hub_info->usb_device);
405 usb_log_debug("Global interrupt on a hub\n");
406 usb_pipe_t *control_pipe = &hub_info->usb_device->ctrl_pipe;
407
408 usb_hub_status_t status;
409 size_t rcvd_size;
410 /* NOTE: We can't use standard USB GET_STATUS request, because
411 * hubs reply is 4byte instead of 2 */
412 const int opResult = usb_pipe_control_read(control_pipe,
413 &get_hub_status_request, sizeof(get_hub_status_request),
414 &status, sizeof(usb_hub_status_t), &rcvd_size);
415 if (opResult != EOK) {
416 usb_log_error("Could not get hub status: %s\n",
417 str_error(opResult));
418 return;
419 }
420 if (rcvd_size != sizeof(usb_hub_status_t)) {
421 usb_log_error("Received status has incorrect size\n");
422 return;
423 }
424
425 /* Handle status changes */
426 if (status & USB_HUB_STATUS_C_OVER_CURRENT)
427 usb_hub_over_current(hub_info, status);
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_info_t structure
462 */
463static void usb_hub_polling_terminated_callback(usb_device_t *device,
464 bool was_error, void *data)
465{
466 usb_hub_info_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 fibril_mutex_lock(&hub->port_mutex);
481 size_t port;
482 for (port = 0; port < hub->port_count; port++) {
483 usb_hub_port_t *the_port = hub->ports + port;
484 fibril_mutex_lock(&the_port->reset_mutex);
485 the_port->reset_completed = true;
486 the_port->reset_okay = false;
487 fibril_condvar_broadcast(&the_port->reset_cv);
488 fibril_mutex_unlock(&the_port->reset_mutex);
489 }
490 fibril_mutex_unlock(&hub->port_mutex);
491 }
492 /* And now wait for them. */
493 while (hub->pending_ops_count > 0) {
494 fibril_condvar_wait(&hub->pending_ops_cv,
495 &hub->pending_ops_mutex);
496 }
497 fibril_mutex_unlock(&hub->pending_ops_mutex);
498
499 usb_device_destroy(hub->usb_device);
500
501 free(hub->ports);
502 free(hub);
503}
504/**
505 * @}
506 */
Note: See TracBrowser for help on using the repository browser.