blob: 61814a7d7280fc85f139a13efbbce279dc0011ed [file] [log] [blame]
Greg Kroah-Hartman41dceed2008-01-30 15:21:33 -08001/* USB OTG (On The Go) defines */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Robert P. J. Daydda43a02008-03-07 13:45:32 -05003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Robert P. J. Daydda43a02008-03-07 13:45:32 -05009#ifndef __LINUX_USB_OTG_H
10#define __LINUX_USB_OTG_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Felipe Balbie9a20172009-12-17 13:01:36 +020012#include <linux/notifier.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* 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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038enum usb_otg_event {
39 /* Device is not connected within
40 * TA_WAIT_BCON or not responding.
41 */
42 OTG_EVENT_DEV_CONN_TMOUT,
43 /* B-device returned STALL for
44 * B_HNP_ENABLE feature request.
45 */
46 OTG_EVENT_NO_RESP_FOR_HNP_ENABLE,
47 /* HUB class devices are not
48 * supported.
49 */
50 OTG_EVENT_HUB_NOT_SUPPORTED,
51 /* Device is not supported i.e
52 * not listed in TPL.
53 */
54 OTG_EVENT_DEV_NOT_SUPPORTED,
55 /* HNP failed due to
56 * TA_AIDL_BDIS timeout or
57 * TB_ASE0_BRST timeout
58 */
59 OTG_EVENT_HNP_FAILED,
60 /* B-device did not detect VBUS
61 * within TB_SRP_FAIL time.
62 */
63 OTG_EVENT_NO_RESP_FOR_SRP,
64};
65
Felipe Balbie9a20172009-12-17 13:01:36 +020066enum usb_xceiv_events {
67 USB_EVENT_NONE, /* no events or cable disconnected */
68 USB_EVENT_VBUS, /* vbus valid event */
69 USB_EVENT_ID, /* id was grounded */
70 USB_EVENT_CHARGER, /* usb dedicated charger */
71 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
72};
73
Daniel Mack91c8a5a2009-10-15 17:09:34 +030074struct otg_transceiver;
75
76/* for transceivers connected thru an ULPI interface, the user must
77 * provide access ops
78 */
79struct otg_io_access_ops {
80 int (*read)(struct otg_transceiver *otg, u32 reg);
81 int (*write)(struct otg_transceiver *otg, u32 val, u32 reg);
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * the otg driver needs to interact with both device side and host side
86 * usb controllers. it decides which controller is active at a given
87 * moment, using the transceiver, ID signal, HNP and sometimes static
88 * configuration information (including "board isn't wired for otg").
89 */
90struct otg_transceiver {
91 struct device *dev;
92 const char *label;
Daniel Mack91c8a5a2009-10-15 17:09:34 +030093 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 u8 default_a;
96 enum usb_otg_state state;
Hema HK647b2d92011-02-17 12:06:09 +053097 enum usb_xceiv_events last_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 struct usb_bus *host;
100 struct usb_gadget *gadget;
101
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300102 struct otg_io_access_ops *io_ops;
103 void __iomem *io_priv;
104
Felipe Balbie9a20172009-12-17 13:01:36 +0200105 /* for notification of usb_xceiv_events */
Felipe Balbicccad6d2010-09-29 10:55:49 +0300106 struct atomic_notifier_head notifier;
Felipe Balbie9a20172009-12-17 13:01:36 +0200107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* to pass extra port status to the root hub */
109 u16 port_status;
110 u16 port_change;
111
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300112 /* initialize/shutdown the OTG controller */
113 int (*init)(struct otg_transceiver *otg);
114 void (*shutdown)(struct otg_transceiver *otg);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* bind/unbind the host controller */
David Brownell3a16f7b2006-06-29 12:27:23 -0700117 int (*set_host)(struct otg_transceiver *otg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct usb_bus *host);
119
120 /* bind/unbind the peripheral controller */
121 int (*set_peripheral)(struct otg_transceiver *otg,
122 struct usb_gadget *gadget);
123
124 /* effective for B devices, ignored for A-peripheral */
125 int (*set_power)(struct otg_transceiver *otg,
126 unsigned mA);
127
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300128 /* effective for A-peripheral, ignored for B devices */
129 int (*set_vbus)(struct otg_transceiver *otg,
130 bool enabled);
131
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700132 /* for non-OTG B devices: set transceiver into suspend mode */
133 int (*set_suspend)(struct otg_transceiver *otg,
134 int suspend);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* for B devices only: start session with A-Host */
137 int (*start_srp)(struct otg_transceiver *otg);
138
139 /* start or continue HNP role switch */
140 int (*start_hnp)(struct otg_transceiver *otg);
141
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 /* send events to user space */
143 int (*send_event)(struct otg_transceiver *otg,
144 enum usb_otg_event event);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148
149/* for board-specific init logic */
150extern int otg_set_transceiver(struct otg_transceiver *);
David Brownellcc835e32009-03-31 12:28:31 -0700151
Guennadi Liakhovetski352a3372010-12-09 22:46:29 +0100152#if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
David Brownellcc835e32009-03-31 12:28:31 -0700153/* sometimes transceivers are accessed only through e.g. ULPI */
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530154extern void usb_nop_xceiv_register(void);
155extern void usb_nop_xceiv_unregister(void);
Maulik Mankad224e1542010-02-17 14:09:29 -0800156#else
157static inline void usb_nop_xceiv_register(void)
158{
159}
160
161static inline void usb_nop_xceiv_unregister(void)
162{
163}
164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300166/* helpers for direct access thru low-level io interface */
167static inline int otg_io_read(struct otg_transceiver *otg, u32 reg)
168{
169 if (otg->io_ops && otg->io_ops->read)
170 return otg->io_ops->read(otg, reg);
171
172 return -EINVAL;
173}
174
Igor Grinberg6e1c3b42010-05-27 09:32:13 +0300175static inline int otg_io_write(struct otg_transceiver *otg, u32 val, u32 reg)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300176{
177 if (otg->io_ops && otg->io_ops->write)
Igor Grinberg6e1c3b42010-05-27 09:32:13 +0300178 return otg->io_ops->write(otg, val, reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300179
180 return -EINVAL;
181}
182
183static inline int
184otg_init(struct otg_transceiver *otg)
185{
186 if (otg->init)
187 return otg->init(otg);
188
189 return 0;
190}
191
192static inline void
193otg_shutdown(struct otg_transceiver *otg)
194{
195 if (otg->shutdown)
196 otg->shutdown(otg);
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199/* for USB core, host and peripheral controller drivers */
200/* Context: can sleep */
201extern int otg_send_event(enum usb_otg_event event);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* for usb host and peripheral controller drivers */
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300204#ifdef CONFIG_USB_OTG_UTILS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205extern struct otg_transceiver *otg_get_transceiver(void);
Philipp Zabel68144e02008-11-24 12:01:17 -0800206extern void otg_put_transceiver(struct otg_transceiver *);
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200207extern const char *otg_state_string(enum usb_otg_state state);
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300208#else
209static inline struct otg_transceiver *otg_get_transceiver(void)
210{
211 return NULL;
212}
213
214static inline void otg_put_transceiver(struct otg_transceiver *x)
215{
216}
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200217
218static inline const char *otg_state_string(enum usb_otg_state state)
219{
220 return NULL;
221}
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800224/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static inline int
226otg_start_hnp(struct otg_transceiver *otg)
227{
228 return otg->start_hnp(otg);
229}
230
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300231/* Context: can sleep */
232static inline int
233otg_set_vbus(struct otg_transceiver *otg, bool enabled)
234{
235 return otg->set_vbus(otg, enabled);
236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/* for HCDs */
239static inline int
240otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
241{
242 return otg->set_host(otg, host);
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/* for usb peripheral controller drivers */
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800246
247/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static inline int
249otg_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *periph)
250{
251 return otg->set_peripheral(otg, periph);
252}
253
254static inline int
255otg_set_power(struct otg_transceiver *otg, unsigned mA)
256{
257 return otg->set_power(otg, mA);
258}
259
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800260/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static inline int
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700262otg_set_suspend(struct otg_transceiver *otg, int suspend)
263{
264 if (otg->set_suspend != NULL)
265 return otg->set_suspend(otg, suspend);
266 else
267 return 0;
268}
269
270static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271otg_start_srp(struct otg_transceiver *otg)
272{
273 return otg->start_srp(otg);
274}
275
Felipe Balbie9a20172009-12-17 13:01:36 +0200276/* notifiers */
277static inline int
278otg_register_notifier(struct otg_transceiver *otg, struct notifier_block *nb)
279{
Felipe Balbicccad6d2010-09-29 10:55:49 +0300280 return atomic_notifier_chain_register(&otg->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200281}
282
283static inline void
284otg_unregister_notifier(struct otg_transceiver *otg, struct notifier_block *nb)
285{
Felipe Balbicccad6d2010-09-29 10:55:49 +0300286 atomic_notifier_chain_unregister(&otg->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/* for OTG controller drivers (and maybe other stuff) */
290extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
Robert P. J. Daydda43a02008-03-07 13:45:32 -0500291
292#endif /* __LINUX_USB_OTG_H */