Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / include / 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 */
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_xceiv_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 otg_transceiver;
47
48 /* for transceivers connected thru an ULPI interface, the user must
49  * provide access ops
50  */
51 struct otg_io_access_ops {
52         int (*read)(struct otg_transceiver *otg, u32 reg);
53         int (*write)(struct otg_transceiver *otg, u32 val, u32 reg);
54 };
55
56 /*
57  * the otg driver needs to interact with both device side and host side
58  * usb controllers.  it decides which controller is active at a given
59  * moment, using the transceiver, ID signal, HNP and sometimes static
60  * configuration information (including "board isn't wired for otg").
61  */
62 struct otg_transceiver {
63         struct device           *dev;
64         const char              *label;
65         unsigned int             flags;
66
67         u8                      default_a;
68         enum usb_otg_state      state;
69         enum usb_xceiv_events   last_event;
70
71         struct usb_bus          *host;
72         struct usb_gadget       *gadget;
73
74         struct device                   *io_dev;
75         struct otg_io_access_ops        *io_ops;
76         void __iomem                    *io_priv;
77
78         /* for notification of usb_xceiv_events */
79         struct atomic_notifier_head     notifier;
80
81         /* to pass extra port status to the root hub */
82         u16                     port_status;
83         u16                     port_change;
84
85         /* initialize/shutdown the OTG controller */
86         int     (*init)(struct otg_transceiver *otg);
87         void    (*shutdown)(struct otg_transceiver *otg);
88
89         /* bind/unbind the host controller */
90         int     (*set_host)(struct otg_transceiver *otg,
91                                 struct usb_bus *host);
92
93         /* bind/unbind the peripheral controller */
94         int     (*set_peripheral)(struct otg_transceiver *otg,
95                                 struct usb_gadget *gadget);
96
97         /* effective for B devices, ignored for A-peripheral */
98         int     (*set_power)(struct otg_transceiver *otg,
99                                 unsigned mA);
100
101         /* effective for A-peripheral, ignored for B devices */
102         int     (*set_vbus)(struct otg_transceiver *otg,
103                                 bool enabled);
104
105         /* for non-OTG B devices: set transceiver into suspend mode */
106         int     (*set_suspend)(struct otg_transceiver *otg,
107                                 int suspend);
108
109         /* for B devices only:  start session with A-Host */
110         int     (*start_srp)(struct otg_transceiver *otg);
111
112         /* start or continue HNP role switch */
113         int     (*start_hnp)(struct otg_transceiver *otg);
114
115 };
116
117
118 /* for board-specific init logic */
119 extern int otg_set_transceiver(struct otg_transceiver *);
120
121 #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
122 /* sometimes transceivers are accessed only through e.g. ULPI */
123 extern void usb_nop_xceiv_register(void);
124 extern void usb_nop_xceiv_unregister(void);
125 #else
126 static inline void usb_nop_xceiv_register(void)
127 {
128 }
129
130 static inline void usb_nop_xceiv_unregister(void)
131 {
132 }
133 #endif
134
135 /* helpers for direct access thru low-level io interface */
136 static inline int otg_io_read(struct otg_transceiver *otg, u32 reg)
137 {
138         if (otg->io_ops && otg->io_ops->read)
139                 return otg->io_ops->read(otg, reg);
140
141         return -EINVAL;
142 }
143
144 static inline int otg_io_write(struct otg_transceiver *otg, u32 val, u32 reg)
145 {
146         if (otg->io_ops && otg->io_ops->write)
147                 return otg->io_ops->write(otg, val, reg);
148
149         return -EINVAL;
150 }
151
152 static inline int
153 otg_init(struct otg_transceiver *otg)
154 {
155         if (otg->init)
156                 return otg->init(otg);
157
158         return 0;
159 }
160
161 static inline void
162 otg_shutdown(struct otg_transceiver *otg)
163 {
164         if (otg->shutdown)
165                 otg->shutdown(otg);
166 }
167
168 /* for usb host and peripheral controller drivers */
169 #ifdef CONFIG_USB_OTG_UTILS
170 extern struct otg_transceiver *otg_get_transceiver(void);
171 extern void otg_put_transceiver(struct otg_transceiver *);
172 extern const char *otg_state_string(enum usb_otg_state state);
173 #else
174 static inline struct otg_transceiver *otg_get_transceiver(void)
175 {
176         return NULL;
177 }
178
179 static inline void otg_put_transceiver(struct otg_transceiver *x)
180 {
181 }
182
183 static inline const char *otg_state_string(enum usb_otg_state state)
184 {
185         return NULL;
186 }
187 #endif
188
189 /* Context: can sleep */
190 static inline int
191 otg_start_hnp(struct otg_transceiver *otg)
192 {
193         return otg->start_hnp(otg);
194 }
195
196 /* Context: can sleep */
197 static inline int
198 otg_set_vbus(struct otg_transceiver *otg, bool enabled)
199 {
200         return otg->set_vbus(otg, enabled);
201 }
202
203 /* for HCDs */
204 static inline int
205 otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
206 {
207         return otg->set_host(otg, host);
208 }
209
210 /* for usb peripheral controller drivers */
211
212 /* Context: can sleep */
213 static inline int
214 otg_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *periph)
215 {
216         return otg->set_peripheral(otg, periph);
217 }
218
219 static inline int
220 otg_set_power(struct otg_transceiver *otg, unsigned mA)
221 {
222         return otg->set_power(otg, mA);
223 }
224
225 /* Context: can sleep */
226 static inline int
227 otg_set_suspend(struct otg_transceiver *otg, int suspend)
228 {
229         if (otg->set_suspend != NULL)
230                 return otg->set_suspend(otg, suspend);
231         else
232                 return 0;
233 }
234
235 static inline int
236 otg_start_srp(struct otg_transceiver *otg)
237 {
238         return otg->start_srp(otg);
239 }
240
241 /* notifiers */
242 static inline int
243 otg_register_notifier(struct otg_transceiver *otg, struct notifier_block *nb)
244 {
245         return atomic_notifier_chain_register(&otg->notifier, nb);
246 }
247
248 static inline void
249 otg_unregister_notifier(struct otg_transceiver *otg, struct notifier_block *nb)
250 {
251         atomic_notifier_chain_unregister(&otg->notifier, nb);
252 }
253
254 /* for OTG controller drivers (and maybe other stuff) */
255 extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
256
257 #endif /* __LINUX_USB_OTG_H */