Nico Huber | 0b78de2 | 2013-05-29 15:01:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2013 secunet Security Networks AG |
| 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 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef __USB_HUB_H |
| 31 | #define __USB_HUB_H |
| 32 | |
| 33 | #include <usb/usb.h> |
| 34 | |
| 35 | typedef struct generic_hub_ops { |
| 36 | /* negative results denote an error */ |
| 37 | |
| 38 | /* returns 1 if the hub's status changed since the last call (optional) */ |
| 39 | int (*hub_status_changed)(usbdev_t *); |
| 40 | /* returns 1 if the port's status changed since the last call */ |
| 41 | int (*port_status_changed)(usbdev_t *, int port); |
| 42 | /* returns 1 if something is connected to the port */ |
| 43 | int (*port_connected)(usbdev_t *, int port); |
| 44 | /* returns 1 if port is currently resetting */ |
| 45 | int (*port_in_reset)(usbdev_t *, int port); |
| 46 | /* returns 1 if the port is enabled */ |
| 47 | int (*port_enabled)(usbdev_t *, int port); |
| 48 | /* returns speed if port is enabled, negative value if not */ |
Julius Werner | e00ba21 | 2013-09-24 20:03:54 -0700 | [diff] [blame] | 49 | usb_speed (*port_speed)(usbdev_t *, int port); |
Nico Huber | 0b78de2 | 2013-05-29 15:01:17 +0200 | [diff] [blame] | 50 | |
| 51 | /* enables (powers up) a port (optional) */ |
| 52 | int (*enable_port)(usbdev_t *, int port); |
| 53 | /* disables (powers down) a port (optional) */ |
| 54 | int (*disable_port)(usbdev_t *, int port); |
| 55 | /* starts a port reset (required if reset_port is set to a generic one from below) */ |
| 56 | int (*start_port_reset)(usbdev_t *, int port); |
| 57 | |
| 58 | /* performs a port reset (optional, generic implementations below) */ |
| 59 | int (*reset_port)(usbdev_t *, int port); |
| 60 | } generic_hub_ops_t; |
| 61 | |
| 62 | typedef struct generic_hub { |
| 63 | int num_ports; |
| 64 | /* port numbers are always 1 based, |
| 65 | so we waste one int for convenience */ |
| 66 | int *ports; /* allocated to sizeof(*ports)*(num_ports+1) */ |
| 67 | #define NO_DEV -1 |
| 68 | |
| 69 | const generic_hub_ops_t *ops; |
| 70 | |
| 71 | void *data; |
| 72 | } generic_hub_t; |
| 73 | |
| 74 | void generic_hub_destroy(usbdev_t *); |
Shawn Nematbakhsh | a16029a | 2014-03-10 14:12:29 -0700 | [diff] [blame^] | 75 | int generic_hub_wait_for_port(usbdev_t *const dev, const int port, |
| 76 | const int wait_for, |
| 77 | int (*const port_op)(usbdev_t *, int), |
| 78 | int timeout_steps, const int step_us); |
Nico Huber | 0b78de2 | 2013-05-29 15:01:17 +0200 | [diff] [blame] | 79 | int generic_hub_resetport(usbdev_t *, int port); |
Nico Huber | 0b78de2 | 2013-05-29 15:01:17 +0200 | [diff] [blame] | 80 | int generic_hub_scanport(usbdev_t *, int port); |
| 81 | /* the provided generic_hub_ops struct has to be static */ |
| 82 | int generic_hub_init(usbdev_t *, int num_ports, const generic_hub_ops_t *); |
| 83 | |
| 84 | #define GEN_HUB(usbdev) ((generic_hub_t *)(usbdev)->data) |
| 85 | |
| 86 | #endif |