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

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

usbhub: Fix power switching procedure, get rid of heap allocation.

  • Property mode set to 100644
File size: 15.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
41#include <usb_iface.h>
42#include <usb/ddfiface.h>
43#include <usb/descriptor.h>
44#include <usb/dev/recognise.h>
45#include <usb/dev/request.h>
46#include <usb/classes/hub.h>
47#include <usb/dev/poll.h>
48#include <stdio.h>
49
50#include "usbhub.h"
51#include "usbhub_private.h"
52#include "port_status.h"
53#include <usb/usb.h>
54#include <usb/dev/pipes.h>
55#include <usb/classes/classes.h>
56
57
58static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
59
60static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
61
62static int usb_hub_set_configuration(usb_hub_info_t *hub_info);
63
64static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info);
65
66static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
67 usb_hub_status_t status);
68
69static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
70 usb_hub_status_t status);
71
72static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
73
74static void usb_hub_polling_terminated_callback(usb_device_t *device,
75 bool was_error, void *data);
76
77
78//*********************************************
79//
80// hub driver code, initialization
81//
82//*********************************************
83
84/**
85 * Initialize hub device driver fibril
86 *
87 * Creates hub representation and fibril that periodically checks hub`s status.
88 * Hub representation is passed to the fibril.
89 * @param usb_dev generic usb device information
90 * @return error code
91 */
92int usb_hub_add_device(usb_device_t *usb_dev) {
93 if (!usb_dev) return EINVAL;
94 usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev);
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,
99 hub_info->usb_device->ddf_dev);
100 if (opResult != EOK) {
101 usb_log_error("Could not initialize connection to device, "
102 " %s\n",
103 str_error(opResult));
104 free(hub_info);
105 return opResult;
106 }
107
108 //set hub configuration
109 opResult = usb_hub_set_configuration(hub_info);
110 if (opResult != EOK) {
111 usb_log_error("Could not set hub configuration, %s\n",
112 str_error(opResult));
113 free(hub_info);
114 return opResult;
115 }
116 //get port count and create attached_devs
117 opResult = usb_hub_process_hub_specific_info(hub_info);
118 if (opResult != EOK) {
119 usb_log_error("Could process hub specific info, %s\n",
120 str_error(opResult));
121 free(hub_info);
122 return opResult;
123 }
124
125 usb_log_debug("Creating 'hub' function in DDF.\n");
126 ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
127 fun_exposed, "hub");
128 assert(hub_fun != NULL);
129 hub_fun->ops = NULL;
130
131 opResult = ddf_fun_bind(hub_fun);
132 assert(opResult == EOK);
133 opResult = ddf_fun_add_to_class(hub_fun, "hub");
134 assert(opResult == EOK);
135
136 opResult = usb_hub_start_hub_fibril(hub_info);
137 if (opResult != EOK)
138 free(hub_info);
139 return opResult;
140}
141
142/** Callback for polling hub for changes.
143 *
144 * @param dev Device where the change occured.
145 * @param change_bitmap Bitmap of changed ports.
146 * @param change_bitmap_size Size of the bitmap in bytes.
147 * @param arg Custom argument, points to @c usb_hub_info_t.
148 * @return Whether to continue polling.
149 */
150bool hub_port_changes_callback(usb_device_t *dev,
151 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
152 usb_log_debug("hub_port_changes_callback\n");
153 usb_hub_info_t *hub = (usb_hub_info_t *) arg;
154
155 /* FIXME: check that we received enough bytes. */
156 if (change_bitmap_size == 0) {
157 goto leave;
158 }
159
160 bool change;
161 change = ((uint8_t*) change_bitmap)[0] & 1;
162 if (change) {
163 usb_hub_process_global_interrupt(hub);
164 }
165
166 size_t port;
167 for (port = 1; port < hub->port_count + 1; port++) {
168 bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
169 if (change) {
170 usb_hub_process_port_interrupt(hub, port);
171 }
172 }
173leave:
174 /* FIXME: proper interval. */
175 async_usleep(1000 * 250);
176
177 return true;
178}
179
180
181//*********************************************
182//
183// support functions
184//
185//*********************************************
186
187/**
188 * create usb_hub_info_t structure
189 *
190 * Does only basic copying of known information into new structure.
191 * @param usb_dev usb device structure
192 * @return basic usb_hub_info_t structure
193 */
194static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) {
195 usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t));
196 if (!result) return NULL;
197 result->usb_device = usb_dev;
198 result->status_change_pipe = usb_dev->pipes[0].pipe;
199 result->control_pipe = &usb_dev->ctrl_pipe;
200 result->is_default_address_used = false;
201
202 result->ports = NULL;
203 result->port_count = (size_t) - 1;
204 fibril_mutex_initialize(&result->port_mutex);
205
206 fibril_mutex_initialize(&result->pending_ops_mutex);
207 fibril_condvar_initialize(&result->pending_ops_cv);
208 result->pending_ops_count = 0;
209 return result;
210}
211
212/**
213 * Load hub-specific information into hub_info structure and process if needed
214 *
215 * Particularly read port count and initialize structure holding port
216 * information. If there are non-removable devices, start initializing them.
217 * This function is hub-specific and should be run only after the hub is
218 * configured using usb_hub_set_configuration function.
219 * @param hub_info hub representation
220 * @return error code
221 */
222int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info)
223{
224 // get hub descriptor
225 usb_log_debug("Retrieving descriptor\n");
226 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
227 int opResult;
228
229 size_t received_size;
230 opResult = usb_request_get_descriptor(hub_info->control_pipe,
231 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
232 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
233 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
234
235 if (opResult != EOK) {
236 usb_log_error("Failed to receive hub descriptor: %s.\n",
237 str_error(opResult));
238 return opResult;
239 }
240 usb_log_debug2("Parsing descriptor\n");
241 usb_hub_descriptor_t descriptor;
242 opResult = usb_deserialize_hub_desriptor(
243 serialized_descriptor, received_size, &descriptor);
244 if (opResult != EOK) {
245 usb_log_error("Could not parse descriptor: %s\n",
246 str_error(opResult));
247 return opResult;
248 }
249 usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
250 hub_info->port_count = descriptor.ports_count;
251
252 hub_info->ports =
253 malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
254 if (!hub_info->ports) {
255 return ENOMEM;
256 }
257
258 size_t port;
259 for (port = 0; port < hub_info->port_count + 1; ++port) {
260 usb_hub_port_init(&hub_info->ports[port]);
261 }
262
263 const bool is_power_switched =
264 !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
265 if (is_power_switched) {
266 usb_log_debug("Hub power switched\n");
267 const bool per_port_power = descriptor.hub_characteristics
268 & HUB_CHAR_POWER_PER_PORT_FLAG;
269
270 for (port = 1; port <= hub_info->port_count; ++port) {
271 usb_log_debug("Powering port %zu.\n", port);
272 opResult = usb_hub_set_port_feature(
273 hub_info->control_pipe,
274 port, USB_HUB_FEATURE_PORT_POWER);
275 if (opResult != EOK) {
276 usb_log_error("Cannot power on port %zu: %s.\n",
277 port, str_error(opResult));
278 } else {
279 if (!per_port_power) {
280 usb_log_debug(
281 "Ganged power switching mode,"
282 "one port is enough.\n");
283 break;
284 }
285 }
286 }
287
288 } else {
289 usb_log_debug("Power not switched, not going to be powered\n");
290 }
291 return EOK;
292}
293
294/**
295 * Set configuration of hub
296 *
297 * Check whether there is at least one configuration and sets the first one.
298 * This function should be run prior to running any hub-specific action.
299 * @param hub_info hub representation
300 * @return error code
301 */
302static int usb_hub_set_configuration(usb_hub_info_t *hub_info) {
303 //device descriptor
304 usb_standard_device_descriptor_t *std_descriptor
305 = &hub_info->usb_device->descriptors.device;
306 usb_log_debug("Hub has %d configurations\n",
307 std_descriptor->configuration_count);
308 if (std_descriptor->configuration_count < 1) {
309 usb_log_error("There are no configurations available\n");
310 return EINVAL;
311 }
312
313 usb_standard_configuration_descriptor_t *config_descriptor
314 = (usb_standard_configuration_descriptor_t *)
315 hub_info->usb_device->descriptors.configuration;
316
317 /* Set configuration. */
318 int opResult = usb_request_set_configuration(
319 &hub_info->usb_device->ctrl_pipe,
320 config_descriptor->configuration_number);
321
322 if (opResult != EOK) {
323 usb_log_error("Failed to set hub configuration: %s.\n",
324 str_error(opResult));
325 return opResult;
326 }
327 usb_log_debug("\tUsed configuration %d\n",
328 config_descriptor->configuration_number);
329
330 return EOK;
331}
332
333/**
334 * create and start fibril with hub control loop
335 *
336 * Before the fibril is started, the control pipe and host controller
337 * connection of the hub is open.
338 *
339 * @param hub_info hub representing structure
340 * @return error code
341 */
342static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) {
343 int rc;
344
345 rc = usb_device_auto_poll(hub_info->usb_device, 0,
346 hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
347 usb_hub_polling_terminated_callback, hub_info);
348 if (rc != EOK) {
349 usb_log_error("Failed to create polling fibril: %s.\n",
350 str_error(rc));
351 free(hub_info);
352 return rc;
353 }
354
355 usb_log_info("Controlling hub `%s' (%zu ports).\n",
356 hub_info->usb_device->ddf_dev->name, hub_info->port_count);
357 return EOK;
358}
359
360//*********************************************
361//
362// change handling functions
363//
364//*********************************************
365
366/**
367 * process hub over current change
368 *
369 * This means either to power off the hub or power it on.
370 * @param hub_info hub instance
371 * @param status hub status bitmask
372 * @return error code
373 */
374static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
375 usb_hub_status_t status) {
376 int opResult;
377 if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) {
378 //poweroff all ports
379 unsigned int port;
380 for (port = 1; port <= hub_info->port_count; ++port) {
381 opResult = usb_hub_clear_port_feature(
382 hub_info->control_pipe, port,
383 USB_HUB_FEATURE_PORT_POWER);
384 if (opResult != EOK) {
385 usb_log_warning(
386 "Cannot power off port %d; %s\n",
387 port, str_error(opResult));
388 }
389 }
390 } else {
391 //power all ports
392 unsigned int port;
393 for (port = 1; port <= hub_info->port_count; ++port) {
394 opResult = usb_hub_set_port_feature(
395 hub_info->control_pipe, port,
396 USB_HUB_FEATURE_PORT_POWER);
397 if (opResult != EOK) {
398 usb_log_warning(
399 "Cannot power off port %d; %s\n",
400 port, str_error(opResult));
401 }
402 }
403 }
404 return opResult;
405}
406
407/**
408 * process hub local power change
409 *
410 * This change is ignored.
411 * @param hub_info hub instance
412 * @param status hub status bitmask
413 * @return error code
414 */
415static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
416 usb_hub_status_t status) {
417 int opResult = EOK;
418 opResult = usb_hub_clear_feature(hub_info->control_pipe,
419 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
420 if (opResult != EOK) {
421 usb_log_error("Cannnot clear hub power change flag: "
422 "%s\n",
423 str_error(opResult));
424 }
425 return opResult;
426}
427
428/**
429 * process hub interrupts
430 *
431 * The change can be either in the over-current condition or
432 * local-power change.
433 * @param hub_info hub instance
434 */
435static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) {
436 usb_log_debug("Global interrupt on a hub\n");
437 usb_pipe_t *pipe = hub_info->control_pipe;
438 int opResult;
439
440 usb_port_status_t status;
441 size_t rcvd_size;
442 usb_device_request_setup_packet_t request;
443 //int opResult;
444 usb_hub_set_hub_status_request(&request);
445 //endpoint 0
446
447 opResult = usb_pipe_control_read(
448 pipe,
449 &request, sizeof (usb_device_request_setup_packet_t),
450 &status, 4, &rcvd_size
451 );
452 if (opResult != EOK) {
453 usb_log_error("Could not get hub status: %s\n",
454 str_error(opResult));
455 return;
456 }
457 if (rcvd_size != sizeof (usb_port_status_t)) {
458 usb_log_error("Received status has incorrect size\n");
459 return;
460 }
461 //port reset
462 if (
463 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
464 usb_process_hub_over_current(hub_info, status);
465 }
466 if (
467 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
468 usb_process_hub_local_power_change(hub_info, status);
469 }
470}
471
472/**
473 * callback called from hub polling fibril when the fibril terminates
474 *
475 * Should perform a cleanup - deletes hub_info.
476 * @param device usb device afected
477 * @param was_error indicates that the fibril is stoped due to an error
478 * @param data pointer to usb_hub_info_t structure
479 */
480static void usb_hub_polling_terminated_callback(usb_device_t *device,
481 bool was_error, void *data) {
482 usb_hub_info_t * hub = data;
483 assert(hub);
484
485 fibril_mutex_lock(&hub->pending_ops_mutex);
486
487 /* The device is dead. However there might be some pending operations
488 * that we need to wait for.
489 * One of them is device adding in progress.
490 * The respective fibril is probably waiting for status change
491 * in port reset (port enable) callback.
492 * Such change would never come (otherwise we would not be here).
493 * Thus, we would flush all pending port resets.
494 */
495 if (hub->pending_ops_count > 0) {
496 fibril_mutex_lock(&hub->port_mutex);
497 size_t port;
498 for (port = 0; port < hub->port_count; port++) {
499 usb_hub_port_t *the_port = hub->ports + port;
500 fibril_mutex_lock(&the_port->reset_mutex);
501 the_port->reset_completed = true;
502 the_port->reset_okay = false;
503 fibril_condvar_broadcast(&the_port->reset_cv);
504 fibril_mutex_unlock(&the_port->reset_mutex);
505 }
506 fibril_mutex_unlock(&hub->port_mutex);
507 }
508 /* And now wait for them. */
509 while (hub->pending_ops_count > 0) {
510 fibril_condvar_wait(&hub->pending_ops_cv,
511 &hub->pending_ops_mutex);
512 }
513 fibril_mutex_unlock(&hub->pending_ops_mutex);
514
515 usb_device_destroy(hub->usb_device);
516
517 free(hub->ports);
518 free(hub);
519}
520
521
522
523
524/**
525 * @}
526 */
Note: See TracBrowser for help on using the repository browser.