otg.h 6.2 KB
Newer Older
1
/* USB OTG (On The Go) defines */
L
Linus Torvalds 已提交
2
/*
3
 *
L
Linus Torvalds 已提交
4 5 6 7 8
 * These APIs may be used between USB controllers.  USB device drivers
 * (for either host or peripheral roles) don't use these calls; they
 * continue to use just usb_device and usb_gadget.
 */

9 10
#ifndef __LINUX_USB_OTG_H
#define __LINUX_USB_OTG_H
L
Linus Torvalds 已提交
11

F
Felipe Balbi 已提交
12 13
#include <linux/notifier.h>

L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* OTG defines lots of enumeration states before device reset */
enum usb_otg_state {
	OTG_STATE_UNDEFINED = 0,

	/* single-role peripheral, and dual-role default-b */
	OTG_STATE_B_IDLE,
	OTG_STATE_B_SRP_INIT,
	OTG_STATE_B_PERIPHERAL,

	/* extra dual-role default-b states */
	OTG_STATE_B_WAIT_ACON,
	OTG_STATE_B_HOST,

	/* dual-role default-a */
	OTG_STATE_A_IDLE,
	OTG_STATE_A_WAIT_VRISE,
	OTG_STATE_A_WAIT_BCON,
	OTG_STATE_A_HOST,
	OTG_STATE_A_SUSPEND,
	OTG_STATE_A_PERIPHERAL,
	OTG_STATE_A_WAIT_VFALL,
	OTG_STATE_A_VBUS_ERR,
};

38
enum usb_phy_events {
F
Felipe Balbi 已提交
39 40 41 42 43 44 45
	USB_EVENT_NONE,         /* no events or cable disconnected */
	USB_EVENT_VBUS,         /* vbus valid event */
	USB_EVENT_ID,           /* id was grounded */
	USB_EVENT_CHARGER,      /* usb dedicated charger */
	USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
};

46
struct usb_phy;
47 48 49 50

/* for transceivers connected thru an ULPI interface, the user must
 * provide access ops
 */
51
struct usb_phy_io_ops {
52 53
	int (*read)(struct usb_phy *x, u32 reg);
	int (*write)(struct usb_phy *x, u32 val, u32 reg);
54 55
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
struct usb_otg {
	u8			default_a;

	struct usb_phy		*phy;
	struct usb_bus		*host;
	struct usb_gadget	*gadget;

	/* bind/unbind the host controller */
	int	(*set_host)(struct usb_otg *otg, struct usb_bus *host);

	/* bind/unbind the peripheral controller */
	int	(*set_peripheral)(struct usb_otg *otg,
					struct usb_gadget *gadget);

	/* effective for A-peripheral, ignored for B devices */
	int	(*set_vbus)(struct usb_otg *otg, bool enabled);

	/* for B devices only:  start session with A-Host */
	int	(*start_srp)(struct usb_otg *otg);

	/* start or continue HNP role switch */
	int	(*start_hnp)(struct usb_otg *otg);

};

L
Linus Torvalds 已提交
81 82 83 84 85 86
/*
 * the otg driver needs to interact with both device side and host side
 * usb controllers.  it decides which controller is active at a given
 * moment, using the transceiver, ID signal, HNP and sometimes static
 * configuration information (including "board isn't wired for otg").
 */
87
struct usb_phy {
L
Linus Torvalds 已提交
88 89
	struct device		*dev;
	const char		*label;
90
	unsigned int		 flags;
L
Linus Torvalds 已提交
91 92

	enum usb_otg_state	state;
93
	enum usb_phy_events	last_event;
L
Linus Torvalds 已提交
94

95 96
	struct usb_otg		*otg;

97
	struct device		*io_dev;
98 99
	struct usb_phy_io_ops	*io_ops;
	void __iomem		*io_priv;
100

101
	/* for notification of usb_phy_events */
102
	struct atomic_notifier_head	notifier;
F
Felipe Balbi 已提交
103

L
Linus Torvalds 已提交
104 105 106 107
	/* to pass extra port status to the root hub */
	u16			port_status;
	u16			port_change;

108
	/* initialize/shutdown the OTG controller */
109 110
	int	(*init)(struct usb_phy *x);
	void	(*shutdown)(struct usb_phy *x);
111

L
Linus Torvalds 已提交
112
	/* effective for B devices, ignored for A-peripheral */
113
	int	(*set_power)(struct usb_phy *x,
L
Linus Torvalds 已提交
114 115
				unsigned mA);

116
	/* for non-OTG B devices: set transceiver into suspend mode */
117
	int	(*set_suspend)(struct usb_phy *x,
118 119
				int suspend);

L
Linus Torvalds 已提交
120 121 122 123
};


/* for board-specific init logic */
124
extern 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 */
128 129
extern void usb_nop_xceiv_register(void);
extern void usb_nop_xceiv_unregister(void);
130 131 132 133 134 135 136 137 138
#else
static inline void usb_nop_xceiv_register(void)
{
}

static inline void usb_nop_xceiv_unregister(void)
{
}
#endif
L
Linus Torvalds 已提交
139

140
/* helpers for direct access thru low-level io interface */
141
static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
142
{
143 144
	if (x->io_ops && x->io_ops->read)
		return x->io_ops->read(x, reg);
145 146 147 148

	return -EINVAL;
}

149
static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
150
{
151 152
	if (x->io_ops && x->io_ops->write)
		return x->io_ops->write(x, val, reg);
153 154 155 156 157

	return -EINVAL;
}

static inline int
158
usb_phy_init(struct usb_phy *x)
159
{
160 161
	if (x->init)
		return x->init(x);
162 163 164 165 166

	return 0;
}

static inline void
167
usb_phy_shutdown(struct usb_phy *x)
168
{
169 170
	if (x->shutdown)
		x->shutdown(x);
171
}
L
Linus Torvalds 已提交
172 173

/* for usb host and peripheral controller drivers */
174
#ifdef CONFIG_USB_OTG_UTILS
175 176
extern struct usb_phy *usb_get_transceiver(void);
extern void usb_put_transceiver(struct usb_phy *);
177
extern const char *otg_state_string(enum usb_otg_state state);
178
#else
179
static inline struct usb_phy *usb_get_transceiver(void)
180 181 182 183
{
	return NULL;
}

184
static inline void usb_put_transceiver(struct usb_phy *x)
185 186
{
}
187 188 189 190 191

static inline const char *otg_state_string(enum usb_otg_state state)
{
	return NULL;
}
192
#endif
L
Linus Torvalds 已提交
193

194
/* Context: can sleep */
L
Linus Torvalds 已提交
195
static inline int
196
otg_start_hnp(struct usb_otg *otg)
L
Linus Torvalds 已提交
197
{
198 199
	if (otg && otg->start_hnp)
		return otg->start_hnp(otg);
200

201
	return -ENOTSUPP;
L
Linus Torvalds 已提交
202 203
}

204 205
/* Context: can sleep */
static inline int
206
otg_set_vbus(struct usb_otg *otg, bool enabled)
207
{
208 209
	if (otg && otg->set_vbus)
		return otg->set_vbus(otg, enabled);
210

211
	return -ENOTSUPP;
212
}
L
Linus Torvalds 已提交
213 214 215

/* for HCDs */
static inline int
216
otg_set_host(struct usb_otg *otg, struct usb_bus *host)
L
Linus Torvalds 已提交
217
{
218 219
	if (otg && otg->set_host)
		return otg->set_host(otg, host);
220

221
	return -ENOTSUPP;
L
Linus Torvalds 已提交
222 223 224
}

/* for usb peripheral controller drivers */
225 226

/* Context: can sleep */
L
Linus Torvalds 已提交
227
static inline int
228
otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
L
Linus Torvalds 已提交
229
{
230 231
	if (otg && otg->set_peripheral)
		return otg->set_peripheral(otg, periph);
232

233
	return -ENOTSUPP;
L
Linus Torvalds 已提交
234 235 236
}

static inline int
237
usb_phy_set_power(struct usb_phy *x, unsigned mA)
L
Linus Torvalds 已提交
238
{
239 240 241
	if (x && x->set_power)
		return x->set_power(x, mA);
	return 0;
L
Linus Torvalds 已提交
242 243
}

244
/* Context: can sleep */
245
static inline int
246
usb_phy_set_suspend(struct usb_phy *x, int suspend)
247
{
248 249
	if (x->set_suspend != NULL)
		return x->set_suspend(x, suspend);
250 251 252 253
	else
		return 0;
}

L
Linus Torvalds 已提交
254
static inline int
255
otg_start_srp(struct usb_otg *otg)
L
Linus Torvalds 已提交
256
{
257 258
	if (otg && otg->start_srp)
		return otg->start_srp(otg);
259

260
	return -ENOTSUPP;
L
Linus Torvalds 已提交
261 262
}

F
Felipe Balbi 已提交
263 264
/* notifiers */
static inline int
265
usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
F
Felipe Balbi 已提交
266
{
267
	return atomic_notifier_chain_register(&x->notifier, nb);
F
Felipe Balbi 已提交
268 269 270
}

static inline void
271
usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
F
Felipe Balbi 已提交
272
{
273
	atomic_notifier_chain_unregister(&x->notifier, nb);
F
Felipe Balbi 已提交
274
}
L
Linus Torvalds 已提交
275 276 277

/* for OTG controller drivers (and maybe other stuff) */
extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
278 279

#endif /* __LINUX_USB_OTG_H */