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