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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a1732929 was a1732929, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

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