xref: /linux-6.15/include/linux/usb/otg.h (revision 9ffc93f2)
1 /* USB OTG (On The Go) defines */
2 /*
3  *
4  * These APIs may be used between USB controllers.  USB device drivers
5  * (for either host or peripheral roles) don't use these calls; they
6  * continue to use just usb_device and usb_gadget.
7  */
8 
9 #ifndef __LINUX_USB_OTG_H
10 #define __LINUX_USB_OTG_H
11 
12 #include <linux/notifier.h>
13 
14 /* OTG defines lots of enumeration states before device reset */
15 enum usb_otg_state {
16 	OTG_STATE_UNDEFINED = 0,
17 
18 	/* single-role peripheral, and dual-role default-b */
19 	OTG_STATE_B_IDLE,
20 	OTG_STATE_B_SRP_INIT,
21 	OTG_STATE_B_PERIPHERAL,
22 
23 	/* extra dual-role default-b states */
24 	OTG_STATE_B_WAIT_ACON,
25 	OTG_STATE_B_HOST,
26 
27 	/* dual-role default-a */
28 	OTG_STATE_A_IDLE,
29 	OTG_STATE_A_WAIT_VRISE,
30 	OTG_STATE_A_WAIT_BCON,
31 	OTG_STATE_A_HOST,
32 	OTG_STATE_A_SUSPEND,
33 	OTG_STATE_A_PERIPHERAL,
34 	OTG_STATE_A_WAIT_VFALL,
35 	OTG_STATE_A_VBUS_ERR,
36 };
37 
38 enum usb_phy_events {
39 	USB_EVENT_NONE,         /* no events or cable disconnected */
40 	USB_EVENT_VBUS,         /* vbus valid event */
41 	USB_EVENT_ID,           /* id was grounded */
42 	USB_EVENT_CHARGER,      /* usb dedicated charger */
43 	USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
44 };
45 
46 struct usb_phy;
47 
48 /* for transceivers connected thru an ULPI interface, the user must
49  * provide access ops
50  */
51 struct usb_phy_io_ops {
52 	int (*read)(struct usb_phy *x, u32 reg);
53 	int (*write)(struct usb_phy *x, u32 val, u32 reg);
54 };
55 
56 struct usb_otg {
57 	u8			default_a;
58 
59 	struct usb_phy		*phy;
60 	struct usb_bus		*host;
61 	struct usb_gadget	*gadget;
62 
63 	/* bind/unbind the host controller */
64 	int	(*set_host)(struct usb_otg *otg, struct usb_bus *host);
65 
66 	/* bind/unbind the peripheral controller */
67 	int	(*set_peripheral)(struct usb_otg *otg,
68 					struct usb_gadget *gadget);
69 
70 	/* effective for A-peripheral, ignored for B devices */
71 	int	(*set_vbus)(struct usb_otg *otg, bool enabled);
72 
73 	/* for B devices only:  start session with A-Host */
74 	int	(*start_srp)(struct usb_otg *otg);
75 
76 	/* start or continue HNP role switch */
77 	int	(*start_hnp)(struct usb_otg *otg);
78 
79 };
80 
81 /*
82  * the otg driver needs to interact with both device side and host side
83  * usb controllers.  it decides which controller is active at a given
84  * moment, using the transceiver, ID signal, HNP and sometimes static
85  * configuration information (including "board isn't wired for otg").
86  */
87 struct usb_phy {
88 	struct device		*dev;
89 	const char		*label;
90 	unsigned int		 flags;
91 
92 	enum usb_otg_state	state;
93 	enum usb_phy_events	last_event;
94 
95 	struct usb_otg		*otg;
96 
97 	struct usb_phy_io_ops	*io_ops;
98 	void __iomem		*io_priv;
99 
100 	/* for notification of usb_phy_events */
101 	struct atomic_notifier_head	notifier;
102 
103 	/* to pass extra port status to the root hub */
104 	u16			port_status;
105 	u16			port_change;
106 
107 	/* initialize/shutdown the OTG controller */
108 	int	(*init)(struct usb_phy *x);
109 	void	(*shutdown)(struct usb_phy *x);
110 
111 	/* effective for B devices, ignored for A-peripheral */
112 	int	(*set_power)(struct usb_phy *x,
113 				unsigned mA);
114 
115 	/* for non-OTG B devices: set transceiver into suspend mode */
116 	int	(*set_suspend)(struct usb_phy *x,
117 				int suspend);
118 
119 };
120 
121 
122 /* for board-specific init logic */
123 extern int usb_set_transceiver(struct usb_phy *);
124 
125 #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
126 /* sometimes transceivers are accessed only through e.g. ULPI */
127 extern void usb_nop_xceiv_register(void);
128 extern void usb_nop_xceiv_unregister(void);
129 #else
130 static inline void usb_nop_xceiv_register(void)
131 {
132 }
133 
134 static inline void usb_nop_xceiv_unregister(void)
135 {
136 }
137 #endif
138 
139 /* helpers for direct access thru low-level io interface */
140 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
141 {
142 	if (x->io_ops && x->io_ops->read)
143 		return x->io_ops->read(x, reg);
144 
145 	return -EINVAL;
146 }
147 
148 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
149 {
150 	if (x->io_ops && x->io_ops->write)
151 		return x->io_ops->write(x, val, reg);
152 
153 	return -EINVAL;
154 }
155 
156 static inline int
157 usb_phy_init(struct usb_phy *x)
158 {
159 	if (x->init)
160 		return x->init(x);
161 
162 	return 0;
163 }
164 
165 static inline void
166 usb_phy_shutdown(struct usb_phy *x)
167 {
168 	if (x->shutdown)
169 		x->shutdown(x);
170 }
171 
172 /* for usb host and peripheral controller drivers */
173 #ifdef CONFIG_USB_OTG_UTILS
174 extern struct usb_phy *usb_get_transceiver(void);
175 extern void usb_put_transceiver(struct usb_phy *);
176 extern const char *otg_state_string(enum usb_otg_state state);
177 #else
178 static inline struct usb_phy *usb_get_transceiver(void)
179 {
180 	return NULL;
181 }
182 
183 static inline void usb_put_transceiver(struct usb_phy *x)
184 {
185 }
186 
187 static inline const char *otg_state_string(enum usb_otg_state state)
188 {
189 	return NULL;
190 }
191 #endif
192 
193 /* Context: can sleep */
194 static inline int
195 otg_start_hnp(struct usb_otg *otg)
196 {
197 	if (otg && otg->start_hnp)
198 		return otg->start_hnp(otg);
199 
200 	return -ENOTSUPP;
201 }
202 
203 /* Context: can sleep */
204 static inline int
205 otg_set_vbus(struct usb_otg *otg, bool enabled)
206 {
207 	if (otg && otg->set_vbus)
208 		return otg->set_vbus(otg, enabled);
209 
210 	return -ENOTSUPP;
211 }
212 
213 /* for HCDs */
214 static inline int
215 otg_set_host(struct usb_otg *otg, struct usb_bus *host)
216 {
217 	if (otg && otg->set_host)
218 		return otg->set_host(otg, host);
219 
220 	return -ENOTSUPP;
221 }
222 
223 /* for usb peripheral controller drivers */
224 
225 /* Context: can sleep */
226 static inline int
227 otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
228 {
229 	if (otg && otg->set_peripheral)
230 		return otg->set_peripheral(otg, periph);
231 
232 	return -ENOTSUPP;
233 }
234 
235 static inline int
236 usb_phy_set_power(struct usb_phy *x, unsigned mA)
237 {
238 	if (x && x->set_power)
239 		return x->set_power(x, mA);
240 	return 0;
241 }
242 
243 /* Context: can sleep */
244 static inline int
245 usb_phy_set_suspend(struct usb_phy *x, int suspend)
246 {
247 	if (x->set_suspend != NULL)
248 		return x->set_suspend(x, suspend);
249 	else
250 		return 0;
251 }
252 
253 static inline int
254 otg_start_srp(struct usb_otg *otg)
255 {
256 	if (otg && otg->start_srp)
257 		return otg->start_srp(otg);
258 
259 	return -ENOTSUPP;
260 }
261 
262 /* notifiers */
263 static inline int
264 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
265 {
266 	return atomic_notifier_chain_register(&x->notifier, nb);
267 }
268 
269 static inline void
270 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
271 {
272 	atomic_notifier_chain_unregister(&x->notifier, nb);
273 }
274 
275 /* for OTG controller drivers (and maybe other stuff) */
276 extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
277 
278 #endif /* __LINUX_USB_OTG_H */
279