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

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

usbhub: Codestyle

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