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 #include <linux/android_kabi.h>
16
17 struct usb_otg {
18 u8 default_a;
19
20 struct phy *phy;
21 /* old usb_phy interface */
22 struct usb_phy *usb_phy;
23 struct usb_bus *host;
24 struct usb_gadget *gadget;
25
26 enum usb_otg_state state;
27
28 /* bind/unbind the host controller */
29 int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
30
31 /* bind/unbind the peripheral controller */
32 int (*set_peripheral)(struct usb_otg *otg,
33 struct usb_gadget *gadget);
34
35 /* effective for A-peripheral, ignored for B devices */
36 int (*set_vbus)(struct usb_otg *otg, bool enabled);
37
38 /* for B devices only: start session with A-Host */
39 int (*start_srp)(struct usb_otg *otg);
40
41 /* start or continue HNP role switch */
42 int (*start_hnp)(struct usb_otg *otg);
43
44 ANDROID_KABI_RESERVE(1);
45 };
46
47 /**
48 * struct usb_otg_caps - describes the otg capabilities of the device
49 * @otg_rev: The OTG revision number the device is compliant with, it's
50 * in binary-coded decimal (i.e. 2.0 is 0200H).
51 * @hnp_support: Indicates if the device supports HNP.
52 * @srp_support: Indicates if the device supports SRP.
53 * @adp_support: Indicates if the device supports ADP.
54 */
55 struct usb_otg_caps {
56 u16 otg_rev;
57 bool hnp_support;
58 bool srp_support;
59 bool adp_support;
60 };
61
62 extern const char *usb_otg_state_string(enum usb_otg_state state);
63
64 /* Context: can sleep */
65 static inline int
otg_start_hnp(struct usb_otg * otg)66 otg_start_hnp(struct usb_otg *otg)
67 {
68 if (otg && otg->start_hnp)
69 return otg->start_hnp(otg);
70
71 return -ENOTSUPP;
72 }
73
74 /* Context: can sleep */
75 static inline int
otg_set_vbus(struct usb_otg * otg,bool enabled)76 otg_set_vbus(struct usb_otg *otg, bool enabled)
77 {
78 if (otg && otg->set_vbus)
79 return otg->set_vbus(otg, enabled);
80
81 return -ENOTSUPP;
82 }
83
84 /* for HCDs */
85 static inline int
otg_set_host(struct usb_otg * otg,struct usb_bus * host)86 otg_set_host(struct usb_otg *otg, struct usb_bus *host)
87 {
88 if (otg && otg->set_host)
89 return otg->set_host(otg, host);
90
91 return -ENOTSUPP;
92 }
93
94 /* for usb peripheral controller drivers */
95
96 /* Context: can sleep */
97 static inline int
otg_set_peripheral(struct usb_otg * otg,struct usb_gadget * periph)98 otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
99 {
100 if (otg && otg->set_peripheral)
101 return otg->set_peripheral(otg, periph);
102
103 return -ENOTSUPP;
104 }
105
106 static inline int
otg_start_srp(struct usb_otg * otg)107 otg_start_srp(struct usb_otg *otg)
108 {
109 if (otg && otg->start_srp)
110 return otg->start_srp(otg);
111
112 return -ENOTSUPP;
113 }
114
115 /* for OTG controller drivers (and maybe other stuff) */
116 extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
117
118 enum usb_dr_mode {
119 USB_DR_MODE_UNKNOWN,
120 USB_DR_MODE_HOST,
121 USB_DR_MODE_PERIPHERAL,
122 USB_DR_MODE_OTG,
123 };
124
125 /**
126 * usb_get_dr_mode - Get dual role mode for given device
127 * @dev: Pointer to the given device
128 *
129 * The function gets phy interface string from property 'dr_mode',
130 * and returns the corresponding enum usb_dr_mode
131 */
132 extern enum usb_dr_mode usb_get_dr_mode(struct device *dev);
133 extern enum usb_dr_mode usb_get_role_switch_default_mode(struct device *dev);
134
135 #endif /* __LINUX_USB_OTG_H */
136