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

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

usbhub: Fix double free in error paths.

Remove redundant usb_hub_dev_create.

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