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

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

ehci, usbhub: Fix formating errors

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