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

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

libsubdev: Add delay parameter to autopol.

usbhub: Set delay to 255 ms . Maximum allowed for endpoint.
This will be useful for devices that don't NAK poll requests.

  • 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 <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/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/**
78 * Initialize hub device driver structure.
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_device_add(usb_device_t *usb_dev)
86{
87 assert(usb_dev);
88 /* Create driver soft-state structure */
89 usb_hub_dev_t *hub_dev =
90 usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
91 if (hub_dev == NULL) {
92 usb_log_error("Failed to create hub driver structure.\n");
93 return ENOMEM;
94 }
95 hub_dev->usb_device = usb_dev;
96 hub_dev->pending_ops_count = 0;
97 hub_dev->running = false;
98 fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
99 fibril_condvar_initialize(&hub_dev->pending_ops_cv);
100
101
102 int opResult = usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
103 if (opResult != EOK) {
104 usb_log_error("Failed to start long ctrl pipe transfer: %s\n",
105 str_error(opResult));
106 return opResult;
107 }
108
109 /* Set hub's first configuration. (There should be only one) */
110 opResult = usb_set_first_configuration(usb_dev);
111 if (opResult != EOK) {
112 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
113 usb_log_error("Could not set hub configuration: %s\n",
114 str_error(opResult));
115 return opResult;
116 }
117
118 /* Get port count and create attached_devices. */
119 opResult = usb_hub_process_hub_specific_info(hub_dev);
120 if (opResult != EOK) {
121 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
122 usb_log_error("Could process hub specific info, %s\n",
123 str_error(opResult));
124 return opResult;
125 }
126
127 /* Create hub control function. */
128 usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
129 hub_dev->hub_fun = ddf_fun_create(hub_dev->usb_device->ddf_dev,
130 fun_exposed, HUB_FNC_NAME);
131 if (hub_dev->hub_fun == NULL) {
132 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
133 usb_log_error("Failed to create hub function.\n");
134 return ENOMEM;
135 }
136
137 /* Bind hub control function. */
138 opResult = ddf_fun_bind(hub_dev->hub_fun);
139 if (opResult != EOK) {
140 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
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(hub_dev->usb_device, 0,
149 hub_port_changes_callback, ((hub_dev->port_count + 1 + 7) / 8),
150 255000,
151 usb_hub_polling_terminated_callback, hub_dev);
152 if (opResult != EOK) {
153 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
154 /* Function is already bound */
155 ddf_fun_unbind(hub_dev->hub_fun);
156 ddf_fun_destroy(hub_dev->hub_fun);
157 usb_log_error("Failed to create polling fibril: %s.\n",
158 str_error(opResult));
159 return opResult;
160 }
161 hub_dev->running = true;
162 usb_log_info("Controlling hub '%s' (%zu ports).\n",
163 ddf_dev_get_name(hub_dev->usb_device->ddf_dev), hub_dev->port_count);
164
165 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
166 return EOK;
167}
168
169/**
170 * Turn off power to all ports.
171 *
172 * @param usb_dev generic usb device information
173 * @return error code
174 */
175int usb_hub_device_remove(usb_device_t *usb_dev)
176{
177 return ENOTSUP;
178}
179
180/**
181 * Remove all attached devices
182 * @param usb_dev generic usb device information
183 * @return error code
184 */
185int usb_hub_device_gone(usb_device_t *usb_dev)
186{
187 assert(usb_dev);
188 usb_hub_dev_t *hub = usb_dev->driver_data;
189 assert(hub);
190 unsigned tries = 10;
191 while (hub->running) {
192 async_usleep(100000);
193 if (!tries--) {
194 usb_log_error("Can't remove hub, still running.\n");
195 return EINPROGRESS;
196 }
197 }
198
199 assert(!hub->running);
200
201 for (size_t port = 0; port < hub->port_count; ++port) {
202 if (hub->ports[port].attached_device.fun) {
203 const int ret =
204 usb_hub_port_fini(&hub->ports[port], hub);
205 if (ret != EOK)
206 return ret;
207 }
208 }
209 free(hub->ports);
210
211 const int ret = ddf_fun_unbind(hub->hub_fun);
212 if (ret != EOK) {
213 usb_log_error("Failed to unbind '%s' function: %s.\n",
214 HUB_FNC_NAME, str_error(ret));
215 return ret;
216 }
217 ddf_fun_destroy(hub->hub_fun);
218
219 usb_log_info("USB hub driver, stopped and cleaned.\n");
220 return EOK;
221}
222
223/** Callback for polling hub for changes.
224 *
225 * @param dev Device where the change occured.
226 * @param change_bitmap Bitmap of changed ports.
227 * @param change_bitmap_size Size of the bitmap in bytes.
228 * @param arg Custom argument, points to @c usb_hub_dev_t.
229 * @return Whether to continue polling.
230 */
231bool hub_port_changes_callback(usb_device_t *dev,
232 uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
233{
234 usb_log_debug("hub_port_changes_callback\n");
235 usb_hub_dev_t *hub = arg;
236 assert(hub);
237
238 /* It is an error condition if we didn't receive enough data */
239 if (change_bitmap_size == 0) {
240 return false;
241 }
242
243 /* Lowest bit indicates global change */
244 const bool change = change_bitmap[0] & 1;
245 if (change) {
246 usb_hub_global_interrupt(hub);
247 }
248
249 /* N + 1 bit indicates change on port N */
250 for (size_t port = 0; port < hub->port_count; ++port) {
251 const size_t bit = port + 1;
252 const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
253 if (change) {
254 usb_hub_port_process_interrupt(&hub->ports[port], hub);
255 }
256 }
257 return true;
258}
259
260/**
261 * Load hub-specific information into hub_dev structure and process if needed
262 *
263 * Read port count and initialize structures holding per port information.
264 * If there are any non-removable devices, start initializing them.
265 * This function is hub-specific and should be run only after the hub is
266 * configured using usb_set_first_configuration function.
267 * @param hub_dev hub representation
268 * @return error code
269 */
270static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
271{
272 assert(hub_dev);
273
274 /* Get hub descriptor. */
275 usb_log_debug("Retrieving descriptor\n");
276 usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
277
278 usb_hub_descriptor_header_t descriptor;
279 size_t received_size;
280 int opResult = usb_request_get_descriptor(control_pipe,
281 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
282 USB_DESCTYPE_HUB, 0, 0, &descriptor,
283 sizeof(usb_hub_descriptor_header_t), &received_size);
284 if (opResult != EOK) {
285 usb_log_error("Failed to receive hub descriptor: %s.\n",
286 str_error(opResult));
287 return opResult;
288 }
289
290 usb_log_debug("Setting port count to %d.\n", descriptor.port_count);
291 hub_dev->port_count = descriptor.port_count;
292
293 hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
294 if (!hub_dev->ports) {
295 return ENOMEM;
296 }
297
298 for (size_t port = 0; port < hub_dev->port_count; ++port) {
299 usb_hub_port_init(
300 &hub_dev->ports[port], port + 1, control_pipe);
301 }
302
303 hub_dev->power_switched =
304 !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
305 hub_dev->per_port_power =
306 descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
307
308 if (!hub_dev->power_switched) {
309 usb_log_info(
310 "Power switching not supported, ports always powered.\n");
311 return EOK;
312 }
313
314 usb_log_info("Hub port power switching enabled (%s).\n",
315 hub_dev->per_port_power ? "per port" : "ganged");
316
317 for (size_t port = 0; port < hub_dev->port_count; ++port) {
318 usb_log_debug("Powering port %zu.\n", port);
319 const int ret = usb_hub_port_set_feature(
320 &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
321
322 if (ret != EOK) {
323 usb_log_error("Cannot power on port %zu: %s.\n",
324 hub_dev->ports[port].port_number, str_error(ret));
325 } else {
326 if (!hub_dev->per_port_power) {
327 usb_log_debug("Ganged power switching, "
328 "one port is enough.\n");
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.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 if (usb_device->descriptors.configuration_size
358 < sizeof(usb_standard_configuration_descriptor_t)) {
359 usb_log_error("Configuration descriptor is not big enough"
360 " to fit standard configuration descriptor.\n");
361 return EOVERFLOW;
362 }
363
364 // TODO: Make sure that the cast is correct
365 usb_standard_configuration_descriptor_t *config_descriptor
366 = (usb_standard_configuration_descriptor_t *)
367 usb_device->descriptors.configuration;
368
369 /* Set configuration. Use the configuration that was in
370 * usb_device->descriptors.configuration i.e. The first one. */
371 const int opResult = usb_request_set_configuration(
372 &usb_device->ctrl_pipe, 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("Detected hub over-current condition, "
397 "all ports should be powered off.");
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("HUB OVER-CURRENT GONE: Cannot power on"
411 " port %zu: %s\n", hub_dev->ports[port].port_number,
412 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("Global interrupt on a hub\n");
432 usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
433
434 usb_hub_status_t status;
435 size_t rcvd_size;
436 /* NOTE: We can't use standard USB GET_STATUS request, because
437 * hubs reply is 4byte instead of 2 */
438 const int opResult = usb_pipe_control_read(control_pipe,
439 &get_hub_status_request, sizeof(get_hub_status_request),
440 &status, sizeof(usb_hub_status_t), &rcvd_size);
441 if (opResult != EOK) {
442 usb_log_error("Could not get hub status: %s\n",
443 str_error(opResult));
444 return;
445 }
446 if (rcvd_size != sizeof(usb_hub_status_t)) {
447 usb_log_error("Received status has incorrect size\n");
448 return;
449 }
450
451 /* Handle status changes */
452 if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
453 usb_hub_over_current(hub_dev, status);
454 /* Ack change in hub OC flag */
455 const int ret = usb_request_clear_feature(
456 &hub_dev->usb_device->ctrl_pipe, USB_REQUEST_TYPE_CLASS,
457 USB_REQUEST_RECIPIENT_DEVICE,
458 USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
459 if (ret != EOK) {
460 usb_log_error("Failed to clear hub over-current "
461 "change flag: %s.\n", str_error(opResult));
462 }
463 }
464
465 if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
466 /* NOTE: Handling this is more complicated.
467 * If the transition is from bus power to local power, all
468 * is good and we may signal the parent hub that we don't
469 * need the power.
470 * If the transition is from local power to bus power
471 * the hub should turn off all the ports and devices need
472 * to be reinitialized taking into account the limited power
473 * that is now available.
474 * There is no support for power distribution in HelenOS,
475 * (or other OSes/hub devices that I've seen) so this is not
476 * implemented.
477 * Just ACK the change.
478 */
479 const int ret = usb_request_clear_feature(
480 control_pipe, USB_REQUEST_TYPE_CLASS,
481 USB_REQUEST_RECIPIENT_DEVICE,
482 USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
483 if (opResult != EOK) {
484 usb_log_error("Failed to clear hub power change "
485 "flag: %s.\n", str_error(ret));
486 }
487 }
488}
489
490/**
491 * callback called from hub polling fibril when the fibril terminates
492 *
493 * Does not perform cleanup, just marks the hub as not running.
494 * @param device usb device afected
495 * @param was_error indicates that the fibril is stoped due to an error
496 * @param data pointer to usb_hub_dev_t structure
497 */
498static void usb_hub_polling_terminated_callback(usb_device_t *device,
499 bool was_error, void *data)
500{
501 usb_hub_dev_t *hub = data;
502 assert(hub);
503
504 fibril_mutex_lock(&hub->pending_ops_mutex);
505
506 /* The device is dead. However there might be some pending operations
507 * that we need to wait for.
508 * One of them is device adding in progress.
509 * The respective fibril is probably waiting for status change
510 * in port reset (port enable) callback.
511 * Such change would never come (otherwise we would not be here).
512 * Thus, we would flush all pending port resets.
513 */
514 if (hub->pending_ops_count > 0) {
515 for (size_t port = 0; port < hub->port_count; ++port) {
516 usb_hub_port_reset_fail(&hub->ports[port]);
517 }
518 }
519 /* And now wait for them. */
520 while (hub->pending_ops_count > 0) {
521 fibril_condvar_wait(&hub->pending_ops_cv,
522 &hub->pending_ops_mutex);
523 }
524 fibril_mutex_unlock(&hub->pending_ops_mutex);
525 hub->running = false;
526}
527/**
528 * @}
529 */
Note: See TracBrowser for help on using the repository browser.