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

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

usbhub: Rename usbhub_private.h ⇒ utils.h.

Add error checks, comments, and slight refactoring.

  • Property mode set to 100644
File size: 15.3 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
59static int usb_set_first_configuration(usb_device_t *usb_device);
60static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
61static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
62static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
63 usb_hub_status_t status);
64static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
65 usb_hub_status_t status);
66static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
67static void usb_hub_polling_terminated_callback(usb_device_t *device,
68 bool was_error, void *data);
69
70
71//*********************************************
72//
73// hub driver code, initialization
74//
75//*********************************************
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_process_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 *result = malloc(sizeof(usb_hub_info_t));
217 if (!result)
218 return NULL;
219
220 result->usb_device = usb_dev;
221 result->control_pipe = &usb_dev->ctrl_pipe;
222 result->is_default_address_used = false;
223
224 result->ports = NULL;
225 result->port_count = -1;
226 fibril_mutex_initialize(&result->port_mutex);
227 fibril_mutex_initialize(&result->pending_ops_mutex);
228 fibril_condvar_initialize(&result->pending_ops_cv);
229 result->pending_ops_count = 0;
230
231 return result;
232}
233
234/**
235 * Load hub-specific information into hub_info structure and process if needed
236 *
237 * Particularly read port count and initialize structure holding port
238 * information. If there are 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 // get hub descriptor
248 usb_log_debug("Retrieving descriptor\n");
249 uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
250 int opResult;
251
252 size_t received_size;
253 opResult = usb_request_get_descriptor(hub_info->control_pipe,
254 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
255 USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
256 USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
257
258 if (opResult != EOK) {
259 usb_log_error("Failed to receive hub descriptor: %s.\n",
260 str_error(opResult));
261 return opResult;
262 }
263 usb_log_debug2("Parsing descriptor\n");
264 usb_hub_descriptor_t descriptor;
265 opResult = usb_deserialize_hub_desriptor(
266 serialized_descriptor, received_size, &descriptor);
267 if (opResult != EOK) {
268 usb_log_error("Could not parse descriptor: %s\n",
269 str_error(opResult));
270 return opResult;
271 }
272 usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
273 hub_info->port_count = descriptor.ports_count;
274
275 // TODO Why +1 ?
276 hub_info->ports =
277 malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
278 if (!hub_info->ports) {
279 return ENOMEM;
280 }
281
282 size_t port;
283 for (port = 0; port < hub_info->port_count + 1; ++port) {
284 usb_hub_port_init(&hub_info->ports[port]);
285 }
286
287 const bool is_power_switched =
288 !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
289 if (is_power_switched) {
290 usb_log_debug("Hub power switched\n");
291 const bool per_port_power = descriptor.hub_characteristics
292 & HUB_CHAR_POWER_PER_PORT_FLAG;
293
294 for (port = 1; port <= hub_info->port_count; ++port) {
295 usb_log_debug("Powering port %zu.\n", port);
296 opResult = usb_hub_set_port_feature(
297 hub_info->control_pipe,
298 port, USB_HUB_FEATURE_PORT_POWER);
299 if (opResult != EOK) {
300 usb_log_error("Cannot power on port %zu: %s.\n",
301 port, str_error(opResult));
302 } else {
303 if (!per_port_power) {
304 usb_log_debug(
305 "Ganged power switching mode, "
306 "one port is enough.\n");
307 break;
308 }
309 }
310 }
311
312 } else {
313 usb_log_debug("Power not switched, ports always powered\n");
314 }
315 return EOK;
316}
317
318/**
319 * Set configuration of and USB device
320 *
321 * Check whether there is at least one configuration and sets the first one.
322 * This function should be run prior to running any hub-specific action.
323 * @param usb_device usb device representation
324 * @return error code
325 */
326static int usb_set_first_configuration(usb_device_t *usb_device)
327{
328 assert(usb_device);
329 /* Get number of possible configurations from device descriptor */
330 const size_t configuration_count =
331 usb_device->descriptors.device.configuration_count;
332 usb_log_debug("Hub has %d configurations.\n", configuration_count);
333
334 if (configuration_count < 1) {
335 usb_log_error("There are no configurations available\n");
336 return EINVAL;
337 }
338
339 // TODO: Make sure that there is enough data and the cast is correct
340 usb_standard_configuration_descriptor_t *config_descriptor
341 = (usb_standard_configuration_descriptor_t *)
342 usb_device->descriptors.configuration;
343
344 /* Set configuration. Use the configuration that was in
345 * usb_device->descriptors.configuration */
346 const int opResult = usb_request_set_configuration(
347 &usb_device->ctrl_pipe, config_descriptor->configuration_number);
348 if (opResult != EOK) {
349 usb_log_error("Failed to set hub configuration: %s.\n",
350 str_error(opResult));
351 return opResult;
352 }
353 usb_log_debug("\tUsed configuration %d\n",
354 config_descriptor->configuration_number);
355
356 return EOK;
357}
358
359//*********************************************
360//
361// change handling functions
362//
363//*********************************************
364
365/**
366 * process hub over current change
367 *
368 * This means either to power off the hub or power it on.
369 * @param hub_info hub instance
370 * @param status hub status bitmask
371 * @return error code
372 */
373static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
374 usb_hub_status_t status) {
375 int opResult;
376 if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) {
377 //poweroff all ports
378 unsigned int port;
379 for (port = 1; port <= hub_info->port_count; ++port) {
380 opResult = usb_hub_clear_port_feature(
381 hub_info->control_pipe, port,
382 USB_HUB_FEATURE_PORT_POWER);
383 if (opResult != EOK) {
384 usb_log_warning(
385 "Cannot power off port %d; %s\n",
386 port, str_error(opResult));
387 }
388 }
389 } else {
390 //power all ports
391 unsigned int port;
392 for (port = 1; port <= hub_info->port_count; ++port) {
393 opResult = usb_hub_set_port_feature(
394 hub_info->control_pipe, port,
395 USB_HUB_FEATURE_PORT_POWER);
396 if (opResult != EOK) {
397 usb_log_warning(
398 "Cannot power off port %d; %s\n",
399 port, str_error(opResult));
400 }
401 }
402 }
403 return opResult;
404}
405
406/**
407 * process hub local power change
408 *
409 * This change is ignored.
410 * @param hub_info hub instance
411 * @param status hub status bitmask
412 * @return error code
413 */
414static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
415 usb_hub_status_t status) {
416 int opResult = EOK;
417 opResult = usb_hub_clear_feature(hub_info->control_pipe,
418 USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
419 if (opResult != EOK) {
420 usb_log_error("Cannnot clear hub power change flag: "
421 "%s\n",
422 str_error(opResult));
423 }
424 return opResult;
425}
426
427/**
428 * process hub interrupts
429 *
430 * The change can be either in the over-current condition or
431 * local-power change.
432 * @param hub_info hub instance
433 */
434static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) {
435 usb_log_debug("Global interrupt on a hub\n");
436 usb_pipe_t *pipe = hub_info->control_pipe;
437 int opResult;
438
439 usb_port_status_t status;
440 size_t rcvd_size;
441 usb_device_request_setup_packet_t request;
442 //int opResult;
443 usb_hub_set_hub_status_request(&request);
444 //endpoint 0
445
446 opResult = usb_pipe_control_read(
447 pipe,
448 &request, sizeof (usb_device_request_setup_packet_t),
449 &status, 4, &rcvd_size
450 );
451 if (opResult != EOK) {
452 usb_log_error("Could not get hub status: %s\n",
453 str_error(opResult));
454 return;
455 }
456 if (rcvd_size != sizeof (usb_port_status_t)) {
457 usb_log_error("Received status has incorrect size\n");
458 return;
459 }
460 //port reset
461 if (
462 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
463 usb_process_hub_over_current(hub_info, status);
464 }
465 if (
466 usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
467 usb_process_hub_local_power_change(hub_info, status);
468 }
469}
470
471/**
472 * callback called from hub polling fibril when the fibril terminates
473 *
474 * Should perform a cleanup - deletes hub_info.
475 * @param device usb device afected
476 * @param was_error indicates that the fibril is stoped due to an error
477 * @param data pointer to usb_hub_info_t structure
478 */
479static void usb_hub_polling_terminated_callback(usb_device_t *device,
480 bool was_error, void *data) {
481 usb_hub_info_t * hub = data;
482 assert(hub);
483
484 fibril_mutex_lock(&hub->pending_ops_mutex);
485
486 /* The device is dead. However there might be some pending operations
487 * that we need to wait for.
488 * One of them is device adding in progress.
489 * The respective fibril is probably waiting for status change
490 * in port reset (port enable) callback.
491 * Such change would never come (otherwise we would not be here).
492 * Thus, we would flush all pending port resets.
493 */
494 if (hub->pending_ops_count > 0) {
495 fibril_mutex_lock(&hub->port_mutex);
496 size_t port;
497 for (port = 0; port < hub->port_count; port++) {
498 usb_hub_port_t *the_port = hub->ports + port;
499 fibril_mutex_lock(&the_port->reset_mutex);
500 the_port->reset_completed = true;
501 the_port->reset_okay = false;
502 fibril_condvar_broadcast(&the_port->reset_cv);
503 fibril_mutex_unlock(&the_port->reset_mutex);
504 }
505 fibril_mutex_unlock(&hub->port_mutex);
506 }
507 /* And now wait for them. */
508 while (hub->pending_ops_count > 0) {
509 fibril_condvar_wait(&hub->pending_ops_cv,
510 &hub->pending_ops_mutex);
511 }
512 fibril_mutex_unlock(&hub->pending_ops_mutex);
513
514 usb_device_destroy(hub->usb_device);
515
516 free(hub->ports);
517 free(hub);
518}
519
520
521
522
523/**
524 * @}
525 */
Note: See TracBrowser for help on using the repository browser.