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

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

usbhub: Request only hub descriptor header. DOn't use deserialize function.

Remove utils.h/c.

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