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