aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelipe Balbi <felipe.balbi@linux.intel.com>2016-05-31 06:07:47 -0400
committerFelipe Balbi <felipe.balbi@linux.intel.com>2016-06-21 03:38:34 -0400
commit5a8d651a2bde01e00caf78496390d6ae46df80af (patch)
tree13237dee50d4ce6f31d7114975a40809cfb136ee
parentd6dc2e76a860d6be0129daae43e5f12461531d20 (diff)
usb: gadget: move gadget API functions to udc-core
instead of defining all functions as static inlines, let's move them to udc-core and export them with EXPORT_SYMBOL_GPL, that way we can make sure that only GPL drivers will use them. As a side effect, it'll be nicer to add tracepoints to the gadget API. While at that, also fix Kconfig dependencies to avoid randconfig build failures. Acked-By: Sebastian Reichel <sre@kernel.org> Acked-by: Peter Chen <peter.chen@nxp.com> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
-rw-r--r--drivers/phy/Kconfig1
-rw-r--r--drivers/power/Kconfig1
-rw-r--r--drivers/usb/gadget/udc/udc-core.c573
-rw-r--r--drivers/usb/host/Kconfig2
-rw-r--r--drivers/usb/phy/Kconfig11
-rw-r--r--include/linux/usb/gadget.h585
6 files changed, 645 insertions, 528 deletions
diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig
index b869b98835f4..8d1cfb7f3ea2 100644
--- a/drivers/phy/Kconfig
+++ b/drivers/phy/Kconfig
@@ -176,6 +176,7 @@ config TWL4030_USB
176 tristate "TWL4030 USB Transceiver Driver" 176 tristate "TWL4030 USB Transceiver Driver"
177 depends on TWL4030_CORE && REGULATOR_TWL4030 && USB_MUSB_OMAP2PLUS 177 depends on TWL4030_CORE && REGULATOR_TWL4030 && USB_MUSB_OMAP2PLUS
178 depends on USB_SUPPORT 178 depends on USB_SUPPORT
179 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't 'y'
179 select GENERIC_PHY 180 select GENERIC_PHY
180 select USB_PHY 181 select USB_PHY
181 help 182 help
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 421770ddafa3..0f11a0f4c369 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -309,6 +309,7 @@ config BATTERY_RX51
309config CHARGER_ISP1704 309config CHARGER_ISP1704
310 tristate "ISP1704 USB Charger Detection" 310 tristate "ISP1704 USB Charger Detection"
311 depends on USB_PHY 311 depends on USB_PHY
312 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
312 help 313 help
313 Say Y to enable support for USB Charger Detection with 314 Say Y to enable support for USB Charger Detection with
314 ISP1707/ISP1704 USB transceivers. 315 ISP1707/ISP1704 USB transceivers.
diff --git a/drivers/usb/gadget/udc/udc-core.c b/drivers/usb/gadget/udc/udc-core.c
index e1b2dcebdc2e..abf013865e51 100644
--- a/drivers/usb/gadget/udc/udc-core.c
+++ b/drivers/usb/gadget/udc/udc-core.c
@@ -59,6 +59,579 @@ static int udc_bind_to_driver(struct usb_udc *udc,
59 59
60/* ------------------------------------------------------------------------- */ 60/* ------------------------------------------------------------------------- */
61 61
62/**
63 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
64 * @ep:the endpoint being configured
65 * @maxpacket_limit:value of maximum packet size limit
66 *
67 * This function should be used only in UDC drivers to initialize endpoint
68 * (usually in probe function).
69 */
70void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
71 unsigned maxpacket_limit)
72{
73 ep->maxpacket_limit = maxpacket_limit;
74 ep->maxpacket = maxpacket_limit;
75}
76EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
77
78/**
79 * usb_ep_enable - configure endpoint, making it usable
80 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
81 * drivers discover endpoints through the ep_list of a usb_gadget.
82 *
83 * When configurations are set, or when interface settings change, the driver
84 * will enable or disable the relevant endpoints. while it is enabled, an
85 * endpoint may be used for i/o until the driver receives a disconnect() from
86 * the host or until the endpoint is disabled.
87 *
88 * the ep0 implementation (which calls this routine) must ensure that the
89 * hardware capabilities of each endpoint match the descriptor provided
90 * for it. for example, an endpoint named "ep2in-bulk" would be usable
91 * for interrupt transfers as well as bulk, but it likely couldn't be used
92 * for iso transfers or for endpoint 14. some endpoints are fully
93 * configurable, with more generic names like "ep-a". (remember that for
94 * USB, "in" means "towards the USB master".)
95 *
96 * returns zero, or a negative error code.
97 */
98int usb_ep_enable(struct usb_ep *ep)
99{
100 int ret;
101
102 if (ep->enabled)
103 return 0;
104
105 ret = ep->ops->enable(ep, ep->desc);
106 if (ret)
107 return ret;
108
109 ep->enabled = true;
110
111 return 0;
112}
113EXPORT_SYMBOL_GPL(usb_ep_enable);
114
115/**
116 * usb_ep_disable - endpoint is no longer usable
117 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
118 *
119 * no other task may be using this endpoint when this is called.
120 * any pending and uncompleted requests will complete with status
121 * indicating disconnect (-ESHUTDOWN) before this call returns.
122 * gadget drivers must call usb_ep_enable() again before queueing
123 * requests to the endpoint.
124 *
125 * returns zero, or a negative error code.
126 */
127int usb_ep_disable(struct usb_ep *ep)
128{
129 int ret;
130
131 if (!ep->enabled)
132 return 0;
133
134 ret = ep->ops->disable(ep);
135 if (ret)
136 return ret;
137
138 ep->enabled = false;
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(usb_ep_disable);
143
144/**
145 * usb_ep_alloc_request - allocate a request object to use with this endpoint
146 * @ep:the endpoint to be used with with the request
147 * @gfp_flags:GFP_* flags to use
148 *
149 * Request objects must be allocated with this call, since they normally
150 * need controller-specific setup and may even need endpoint-specific
151 * resources such as allocation of DMA descriptors.
152 * Requests may be submitted with usb_ep_queue(), and receive a single
153 * completion callback. Free requests with usb_ep_free_request(), when
154 * they are no longer needed.
155 *
156 * Returns the request, or null if one could not be allocated.
157 */
158struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
159 gfp_t gfp_flags)
160{
161 return ep->ops->alloc_request(ep, gfp_flags);
162}
163EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
164
165/**
166 * usb_ep_free_request - frees a request object
167 * @ep:the endpoint associated with the request
168 * @req:the request being freed
169 *
170 * Reverses the effect of usb_ep_alloc_request().
171 * Caller guarantees the request is not queued, and that it will
172 * no longer be requeued (or otherwise used).
173 */
174void usb_ep_free_request(struct usb_ep *ep,
175 struct usb_request *req)
176{
177 ep->ops->free_request(ep, req);
178}
179EXPORT_SYMBOL_GPL(usb_ep_free_request);
180
181/**
182 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
183 * @ep:the endpoint associated with the request
184 * @req:the request being submitted
185 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
186 * pre-allocate all necessary memory with the request.
187 *
188 * This tells the device controller to perform the specified request through
189 * that endpoint (reading or writing a buffer). When the request completes,
190 * including being canceled by usb_ep_dequeue(), the request's completion
191 * routine is called to return the request to the driver. Any endpoint
192 * (except control endpoints like ep0) may have more than one transfer
193 * request queued; they complete in FIFO order. Once a gadget driver
194 * submits a request, that request may not be examined or modified until it
195 * is given back to that driver through the completion callback.
196 *
197 * Each request is turned into one or more packets. The controller driver
198 * never merges adjacent requests into the same packet. OUT transfers
199 * will sometimes use data that's already buffered in the hardware.
200 * Drivers can rely on the fact that the first byte of the request's buffer
201 * always corresponds to the first byte of some USB packet, for both
202 * IN and OUT transfers.
203 *
204 * Bulk endpoints can queue any amount of data; the transfer is packetized
205 * automatically. The last packet will be short if the request doesn't fill it
206 * out completely. Zero length packets (ZLPs) should be avoided in portable
207 * protocols since not all usb hardware can successfully handle zero length
208 * packets. (ZLPs may be explicitly written, and may be implicitly written if
209 * the request 'zero' flag is set.) Bulk endpoints may also be used
210 * for interrupt transfers; but the reverse is not true, and some endpoints
211 * won't support every interrupt transfer. (Such as 768 byte packets.)
212 *
213 * Interrupt-only endpoints are less functional than bulk endpoints, for
214 * example by not supporting queueing or not handling buffers that are
215 * larger than the endpoint's maxpacket size. They may also treat data
216 * toggle differently.
217 *
218 * Control endpoints ... after getting a setup() callback, the driver queues
219 * one response (even if it would be zero length). That enables the
220 * status ack, after transferring data as specified in the response. Setup
221 * functions may return negative error codes to generate protocol stalls.
222 * (Note that some USB device controllers disallow protocol stall responses
223 * in some cases.) When control responses are deferred (the response is
224 * written after the setup callback returns), then usb_ep_set_halt() may be
225 * used on ep0 to trigger protocol stalls. Depending on the controller,
226 * it may not be possible to trigger a status-stage protocol stall when the
227 * data stage is over, that is, from within the response's completion
228 * routine.
229 *
230 * For periodic endpoints, like interrupt or isochronous ones, the usb host
231 * arranges to poll once per interval, and the gadget driver usually will
232 * have queued some data to transfer at that time.
233 *
234 * Returns zero, or a negative error code. Endpoints that are not enabled
235 * report errors; errors will also be
236 * reported when the usb peripheral is disconnected.
237 */
238int usb_ep_queue(struct usb_ep *ep,
239 struct usb_request *req, gfp_t gfp_flags)
240{
241 if (WARN_ON_ONCE(!ep->enabled && ep->address))
242 return -ESHUTDOWN;
243
244 return ep->ops->queue(ep, req, gfp_flags);
245}
246EXPORT_SYMBOL_GPL(usb_ep_queue);
247
248/**
249 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
250 * @ep:the endpoint associated with the request
251 * @req:the request being canceled
252 *
253 * If the request is still active on the endpoint, it is dequeued and its
254 * completion routine is called (with status -ECONNRESET); else a negative
255 * error code is returned. This is guaranteed to happen before the call to
256 * usb_ep_dequeue() returns.
257 *
258 * Note that some hardware can't clear out write fifos (to unlink the request
259 * at the head of the queue) except as part of disconnecting from usb. Such
260 * restrictions prevent drivers from supporting configuration changes,
261 * even to configuration zero (a "chapter 9" requirement).
262 */
263int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
264{
265 return ep->ops->dequeue(ep, req);
266}
267EXPORT_SYMBOL_GPL(usb_ep_dequeue);
268
269/**
270 * usb_ep_set_halt - sets the endpoint halt feature.
271 * @ep: the non-isochronous endpoint being stalled
272 *
273 * Use this to stall an endpoint, perhaps as an error report.
274 * Except for control endpoints,
275 * the endpoint stays halted (will not stream any data) until the host
276 * clears this feature; drivers may need to empty the endpoint's request
277 * queue first, to make sure no inappropriate transfers happen.
278 *
279 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
280 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
281 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
282 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
283 *
284 * Returns zero, or a negative error code. On success, this call sets
285 * underlying hardware state that blocks data transfers.
286 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
287 * transfer requests are still queued, or if the controller hardware
288 * (usually a FIFO) still holds bytes that the host hasn't collected.
289 */
290int usb_ep_set_halt(struct usb_ep *ep)
291{
292 return ep->ops->set_halt(ep, 1);
293}
294EXPORT_SYMBOL_GPL(usb_ep_set_halt);
295
296/**
297 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
298 * @ep:the bulk or interrupt endpoint being reset
299 *
300 * Use this when responding to the standard usb "set interface" request,
301 * for endpoints that aren't reconfigured, after clearing any other state
302 * in the endpoint's i/o queue.
303 *
304 * Returns zero, or a negative error code. On success, this call clears
305 * the underlying hardware state reflecting endpoint halt and data toggle.
306 * Note that some hardware can't support this request (like pxa2xx_udc),
307 * and accordingly can't correctly implement interface altsettings.
308 */
309int usb_ep_clear_halt(struct usb_ep *ep)
310{
311 return ep->ops->set_halt(ep, 0);
312}
313EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
314
315/**
316 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
317 * @ep: the endpoint being wedged
318 *
319 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
320 * requests. If the gadget driver clears the halt status, it will
321 * automatically unwedge the endpoint.
322 *
323 * Returns zero on success, else negative errno.
324 */
325int usb_ep_set_wedge(struct usb_ep *ep)
326{
327 if (ep->ops->set_wedge)
328 return ep->ops->set_wedge(ep);
329 else
330 return ep->ops->set_halt(ep, 1);
331}
332EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
333
334/**
335 * usb_ep_fifo_status - returns number of bytes in fifo, or error
336 * @ep: the endpoint whose fifo status is being checked.
337 *
338 * FIFO endpoints may have "unclaimed data" in them in certain cases,
339 * such as after aborted transfers. Hosts may not have collected all
340 * the IN data written by the gadget driver (and reported by a request
341 * completion). The gadget driver may not have collected all the data
342 * written OUT to it by the host. Drivers that need precise handling for
343 * fault reporting or recovery may need to use this call.
344 *
345 * This returns the number of such bytes in the fifo, or a negative
346 * errno if the endpoint doesn't use a FIFO or doesn't support such
347 * precise handling.
348 */
349int usb_ep_fifo_status(struct usb_ep *ep)
350{
351 if (ep->ops->fifo_status)
352 return ep->ops->fifo_status(ep);
353 else
354 return -EOPNOTSUPP;
355}
356EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
357
358/**
359 * usb_ep_fifo_flush - flushes contents of a fifo
360 * @ep: the endpoint whose fifo is being flushed.
361 *
362 * This call may be used to flush the "unclaimed data" that may exist in
363 * an endpoint fifo after abnormal transaction terminations. The call
364 * must never be used except when endpoint is not being used for any
365 * protocol translation.
366 */
367void usb_ep_fifo_flush(struct usb_ep *ep)
368{
369 if (ep->ops->fifo_flush)
370 ep->ops->fifo_flush(ep);
371}
372EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
373
374/* ------------------------------------------------------------------------- */
375
376/**
377 * usb_gadget_frame_number - returns the current frame number
378 * @gadget: controller that reports the frame number
379 *
380 * Returns the usb frame number, normally eleven bits from a SOF packet,
381 * or negative errno if this device doesn't support this capability.
382 */
383int usb_gadget_frame_number(struct usb_gadget *gadget)
384{
385 return gadget->ops->get_frame(gadget);
386}
387EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
388
389/**
390 * usb_gadget_wakeup - tries to wake up the host connected to this gadget
391 * @gadget: controller used to wake up the host
392 *
393 * Returns zero on success, else negative error code if the hardware
394 * doesn't support such attempts, or its support has not been enabled
395 * by the usb host. Drivers must return device descriptors that report
396 * their ability to support this, or hosts won't enable it.
397 *
398 * This may also try to use SRP to wake the host and start enumeration,
399 * even if OTG isn't otherwise in use. OTG devices may also start
400 * remote wakeup even when hosts don't explicitly enable it.
401 */
402int usb_gadget_wakeup(struct usb_gadget *gadget)
403{
404 if (!gadget->ops->wakeup)
405 return -EOPNOTSUPP;
406 return gadget->ops->wakeup(gadget);
407}
408EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
409
410/**
411 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
412 * @gadget:the device being declared as self-powered
413 *
414 * this affects the device status reported by the hardware driver
415 * to reflect that it now has a local power supply.
416 *
417 * returns zero on success, else negative errno.
418 */
419int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
420{
421 if (!gadget->ops->set_selfpowered)
422 return -EOPNOTSUPP;
423 return gadget->ops->set_selfpowered(gadget, 1);
424}
425EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
426
427/**
428 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
429 * @gadget:the device being declared as bus-powered
430 *
431 * this affects the device status reported by the hardware driver.
432 * some hardware may not support bus-powered operation, in which
433 * case this feature's value can never change.
434 *
435 * returns zero on success, else negative errno.
436 */
437int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
438{
439 if (!gadget->ops->set_selfpowered)
440 return -EOPNOTSUPP;
441 return gadget->ops->set_selfpowered(gadget, 0);
442}
443EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
444
445/**
446 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
447 * @gadget:The device which now has VBUS power.
448 * Context: can sleep
449 *
450 * This call is used by a driver for an external transceiver (or GPIO)
451 * that detects a VBUS power session starting. Common responses include
452 * resuming the controller, activating the D+ (or D-) pullup to let the
453 * host detect that a USB device is attached, and starting to draw power
454 * (8mA or possibly more, especially after SET_CONFIGURATION).
455 *
456 * Returns zero on success, else negative errno.
457 */
458int usb_gadget_vbus_connect(struct usb_gadget *gadget)
459{
460 if (!gadget->ops->vbus_session)
461 return -EOPNOTSUPP;
462 return gadget->ops->vbus_session(gadget, 1);
463}
464EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
465
466/**
467 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
468 * @gadget:The device whose VBUS usage is being described
469 * @mA:How much current to draw, in milliAmperes. This should be twice
470 * the value listed in the configuration descriptor bMaxPower field.
471 *
472 * This call is used by gadget drivers during SET_CONFIGURATION calls,
473 * reporting how much power the device may consume. For example, this
474 * could affect how quickly batteries are recharged.
475 *
476 * Returns zero on success, else negative errno.
477 */
478int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
479{
480 if (!gadget->ops->vbus_draw)
481 return -EOPNOTSUPP;
482 return gadget->ops->vbus_draw(gadget, mA);
483}
484EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
485
486/**
487 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
488 * @gadget:the device whose VBUS supply is being described
489 * Context: can sleep
490 *
491 * This call is used by a driver for an external transceiver (or GPIO)
492 * that detects a VBUS power session ending. Common responses include
493 * reversing everything done in usb_gadget_vbus_connect().
494 *
495 * Returns zero on success, else negative errno.
496 */
497int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
498{
499 if (!gadget->ops->vbus_session)
500 return -EOPNOTSUPP;
501 return gadget->ops->vbus_session(gadget, 0);
502}
503EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
504
505/**
506 * usb_gadget_connect - software-controlled connect to USB host
507 * @gadget:the peripheral being connected
508 *
509 * Enables the D+ (or potentially D-) pullup. The host will start
510 * enumerating this gadget when the pullup is active and a VBUS session
511 * is active (the link is powered). This pullup is always enabled unless
512 * usb_gadget_disconnect() has been used to disable it.
513 *
514 * Returns zero on success, else negative errno.
515 */
516int usb_gadget_connect(struct usb_gadget *gadget)
517{
518 int ret;
519
520 if (!gadget->ops->pullup)
521 return -EOPNOTSUPP;
522
523 if (gadget->deactivated) {
524 /*
525 * If gadget is deactivated we only save new state.
526 * Gadget will be connected automatically after activation.
527 */
528 gadget->connected = true;
529 return 0;
530 }
531
532 ret = gadget->ops->pullup(gadget, 1);
533 if (!ret)
534 gadget->connected = 1;
535 return ret;
536}
537EXPORT_SYMBOL_GPL(usb_gadget_connect);
538
539/**
540 * usb_gadget_disconnect - software-controlled disconnect from USB host
541 * @gadget:the peripheral being disconnected
542 *
543 * Disables the D+ (or potentially D-) pullup, which the host may see
544 * as a disconnect (when a VBUS session is active). Not all systems
545 * support software pullup controls.
546 *
547 * Returns zero on success, else negative errno.
548 */
549int usb_gadget_disconnect(struct usb_gadget *gadget)
550{
551 int ret;
552
553 if (!gadget->ops->pullup)
554 return -EOPNOTSUPP;
555
556 if (gadget->deactivated) {
557 /*
558 * If gadget is deactivated we only save new state.
559 * Gadget will stay disconnected after activation.
560 */
561 gadget->connected = false;
562 return 0;
563 }
564
565 ret = gadget->ops->pullup(gadget, 0);
566 if (!ret)
567 gadget->connected = 0;
568 return ret;
569}
570EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
571
572/**
573 * usb_gadget_deactivate - deactivate function which is not ready to work
574 * @gadget: the peripheral being deactivated
575 *
576 * This routine may be used during the gadget driver bind() call to prevent
577 * the peripheral from ever being visible to the USB host, unless later
578 * usb_gadget_activate() is called. For example, user mode components may
579 * need to be activated before the system can talk to hosts.
580 *
581 * Returns zero on success, else negative errno.
582 */
583int usb_gadget_deactivate(struct usb_gadget *gadget)
584{
585 int ret;
586
587 if (gadget->deactivated)
588 return 0;
589
590 if (gadget->connected) {
591 ret = usb_gadget_disconnect(gadget);
592 if (ret)
593 return ret;
594 /*
595 * If gadget was being connected before deactivation, we want
596 * to reconnect it in usb_gadget_activate().
597 */
598 gadget->connected = true;
599 }
600 gadget->deactivated = true;
601
602 return 0;
603}
604EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
605
606/**
607 * usb_gadget_activate - activate function which is not ready to work
608 * @gadget: the peripheral being activated
609 *
610 * This routine activates gadget which was previously deactivated with
611 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
612 *
613 * Returns zero on success, else negative errno.
614 */
615int usb_gadget_activate(struct usb_gadget *gadget)
616{
617 if (!gadget->deactivated)
618 return 0;
619
620 gadget->deactivated = false;
621
622 /*
623 * If gadget has been connected before deactivation, or became connected
624 * while it was being deactivated, we call usb_gadget_connect().
625 */
626 if (gadget->connected)
627 return usb_gadget_connect(gadget);
628
629 return 0;
630}
631EXPORT_SYMBOL_GPL(usb_gadget_activate);
632
633/* ------------------------------------------------------------------------- */
634
62#ifdef CONFIG_HAS_DMA 635#ifdef CONFIG_HAS_DMA
63 636
64int usb_gadget_map_request_by_dev(struct device *dev, 637int usb_gadget_map_request_by_dev(struct device *dev,
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index d8f5674809e8..2e710a4cca52 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -180,7 +180,7 @@ config USB_EHCI_MXC
180config USB_EHCI_HCD_OMAP 180config USB_EHCI_HCD_OMAP
181 tristate "EHCI support for OMAP3 and later chips" 181 tristate "EHCI support for OMAP3 and later chips"
182 depends on ARCH_OMAP 182 depends on ARCH_OMAP
183 select NOP_USB_XCEIV 183 depends on NOP_USB_XCEIV
184 default y 184 default y
185 ---help--- 185 ---help---
186 Enables support for the on-chip EHCI controller on 186 Enables support for the on-chip EHCI controller on
diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index c6904742e2aa..b9c409a18faa 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -21,6 +21,7 @@ config AB8500_USB
21config FSL_USB2_OTG 21config FSL_USB2_OTG
22 bool "Freescale USB OTG Transceiver Driver" 22 bool "Freescale USB OTG Transceiver Driver"
23 depends on USB_EHCI_FSL && USB_FSL_USB2 && USB_OTG_FSM && PM 23 depends on USB_EHCI_FSL && USB_FSL_USB2 && USB_OTG_FSM && PM
24 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
24 select USB_PHY 25 select USB_PHY
25 help 26 help
26 Enable this to support Freescale USB OTG transceiver. 27 Enable this to support Freescale USB OTG transceiver.
@@ -29,6 +30,7 @@ config ISP1301_OMAP
29 tristate "Philips ISP1301 with OMAP OTG" 30 tristate "Philips ISP1301 with OMAP OTG"
30 depends on I2C && ARCH_OMAP_OTG 31 depends on I2C && ARCH_OMAP_OTG
31 depends on USB 32 depends on USB
33 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
32 select USB_PHY 34 select USB_PHY
33 help 35 help
34 If you say yes here you get support for the Philips ISP1301 36 If you say yes here you get support for the Philips ISP1301
@@ -43,7 +45,7 @@ config ISP1301_OMAP
43config KEYSTONE_USB_PHY 45config KEYSTONE_USB_PHY
44 tristate "Keystone USB PHY Driver" 46 tristate "Keystone USB PHY Driver"
45 depends on ARCH_KEYSTONE || COMPILE_TEST 47 depends on ARCH_KEYSTONE || COMPILE_TEST
46 select NOP_USB_XCEIV 48 depends on NOP_USB_XCEIV
47 help 49 help
48 Enable this to support Keystone USB phy. This driver provides 50 Enable this to support Keystone USB phy. This driver provides
49 interface to interact with USB 2.0 and USB 3.0 PHY that is part 51 interface to interact with USB 2.0 and USB 3.0 PHY that is part
@@ -51,6 +53,7 @@ config KEYSTONE_USB_PHY
51 53
52config NOP_USB_XCEIV 54config NOP_USB_XCEIV
53 tristate "NOP USB Transceiver Driver" 55 tristate "NOP USB Transceiver Driver"
56 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, NOP can't be built-in
54 select USB_PHY 57 select USB_PHY
55 help 58 help
56 This driver is to be used by all the usb transceiver which are either 59 This driver is to be used by all the usb transceiver which are either
@@ -63,9 +66,9 @@ config AM335X_CONTROL_USB
63config AM335X_PHY_USB 66config AM335X_PHY_USB
64 tristate "AM335x USB PHY Driver" 67 tristate "AM335x USB PHY Driver"
65 depends on ARM || COMPILE_TEST 68 depends on ARM || COMPILE_TEST
69 depends on NOP_USB_XCEIV
66 select USB_PHY 70 select USB_PHY
67 select AM335X_CONTROL_USB 71 select AM335X_CONTROL_USB
68 select NOP_USB_XCEIV
69 select USB_COMMON 72 select USB_COMMON
70 help 73 help
71 This driver provides PHY support for that phy which part for the 74 This driver provides PHY support for that phy which part for the
@@ -92,6 +95,7 @@ config TWL6030_USB
92config USB_GPIO_VBUS 95config USB_GPIO_VBUS
93 tristate "GPIO based peripheral-only VBUS sensing 'transceiver'" 96 tristate "GPIO based peripheral-only VBUS sensing 'transceiver'"
94 depends on GPIOLIB || COMPILE_TEST 97 depends on GPIOLIB || COMPILE_TEST
98 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
95 select USB_PHY 99 select USB_PHY
96 help 100 help
97 Provides simple GPIO VBUS sensing for controllers with an 101 Provides simple GPIO VBUS sensing for controllers with an
@@ -112,6 +116,7 @@ config OMAP_OTG
112config TAHVO_USB 116config TAHVO_USB
113 tristate "Tahvo USB transceiver driver" 117 tristate "Tahvo USB transceiver driver"
114 depends on MFD_RETU && EXTCON 118 depends on MFD_RETU && EXTCON
119 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
115 select USB_PHY 120 select USB_PHY
116 help 121 help
117 Enable this to support USB transceiver on Tahvo. This is used 122 Enable this to support USB transceiver on Tahvo. This is used
@@ -140,6 +145,7 @@ config USB_ISP1301
140config USB_MSM_OTG 145config USB_MSM_OTG
141 tristate "Qualcomm on-chip USB OTG controller support" 146 tristate "Qualcomm on-chip USB OTG controller support"
142 depends on (USB || USB_GADGET) && (ARCH_QCOM || COMPILE_TEST) 147 depends on (USB || USB_GADGET) && (ARCH_QCOM || COMPILE_TEST)
148 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
143 depends on RESET_CONTROLLER 149 depends on RESET_CONTROLLER
144 depends on EXTCON 150 depends on EXTCON
145 select USB_PHY 151 select USB_PHY
@@ -169,6 +175,7 @@ config USB_QCOM_8X16_PHY
169config USB_MV_OTG 175config USB_MV_OTG
170 tristate "Marvell USB OTG support" 176 tristate "Marvell USB OTG support"
171 depends on USB_EHCI_MV && USB_MV_UDC && PM && USB_OTG 177 depends on USB_EHCI_MV && USB_MV_UDC && PM && USB_OTG
178 depends on USB_GADGET || !USB_GADGET # if USB_GADGET=m, this can't be 'y'
172 select USB_PHY 179 select USB_PHY
173 help 180 help
174 Say Y here if you want to build Marvell USB OTG transciever 181 Say Y here if you want to build Marvell USB OTG transciever
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index fefe8b06a63d..c6e1149ddb0d 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -228,307 +228,49 @@ struct usb_ep {
228 228
229/*-------------------------------------------------------------------------*/ 229/*-------------------------------------------------------------------------*/
230 230
231/** 231#if IS_ENABLED(CONFIG_USB_GADGET)
232 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint 232void usb_ep_set_maxpacket_limit(struct usb_ep *ep, unsigned maxpacket_limit);
233 * @ep:the endpoint being configured 233int usb_ep_enable(struct usb_ep *ep);
234 * @maxpacket_limit:value of maximum packet size limit 234int usb_ep_disable(struct usb_ep *ep);
235 * 235struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
236 * This function should be used only in UDC drivers to initialize endpoint 236void usb_ep_free_request(struct usb_ep *ep, struct usb_request *req);
237 * (usually in probe function). 237int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags);
238 */ 238int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
239int usb_ep_set_halt(struct usb_ep *ep);
240int usb_ep_clear_halt(struct usb_ep *ep);
241int usb_ep_set_wedge(struct usb_ep *ep);
242int usb_ep_fifo_status(struct usb_ep *ep);
243void usb_ep_fifo_flush(struct usb_ep *ep);
244#else
239static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep, 245static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
240 unsigned maxpacket_limit) 246 unsigned maxpacket_limit)
241{ 247{ }
242 ep->maxpacket_limit = maxpacket_limit;
243 ep->maxpacket = maxpacket_limit;
244}
245
246/**
247 * usb_ep_enable - configure endpoint, making it usable
248 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
249 * drivers discover endpoints through the ep_list of a usb_gadget.
250 *
251 * When configurations are set, or when interface settings change, the driver
252 * will enable or disable the relevant endpoints. while it is enabled, an
253 * endpoint may be used for i/o until the driver receives a disconnect() from
254 * the host or until the endpoint is disabled.
255 *
256 * the ep0 implementation (which calls this routine) must ensure that the
257 * hardware capabilities of each endpoint match the descriptor provided
258 * for it. for example, an endpoint named "ep2in-bulk" would be usable
259 * for interrupt transfers as well as bulk, but it likely couldn't be used
260 * for iso transfers or for endpoint 14. some endpoints are fully
261 * configurable, with more generic names like "ep-a". (remember that for
262 * USB, "in" means "towards the USB master".)
263 *
264 * returns zero, or a negative error code.
265 */
266static inline int usb_ep_enable(struct usb_ep *ep) 248static inline int usb_ep_enable(struct usb_ep *ep)
267{ 249{ return 0; }
268 int ret;
269
270 if (ep->enabled)
271 return 0;
272
273 ret = ep->ops->enable(ep, ep->desc);
274 if (ret)
275 return ret;
276
277 ep->enabled = true;
278
279 return 0;
280}
281
282/**
283 * usb_ep_disable - endpoint is no longer usable
284 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
285 *
286 * no other task may be using this endpoint when this is called.
287 * any pending and uncompleted requests will complete with status
288 * indicating disconnect (-ESHUTDOWN) before this call returns.
289 * gadget drivers must call usb_ep_enable() again before queueing
290 * requests to the endpoint.
291 *
292 * returns zero, or a negative error code.
293 */
294static inline int usb_ep_disable(struct usb_ep *ep) 250static inline int usb_ep_disable(struct usb_ep *ep)
295{ 251{ return 0; }
296 int ret;
297
298 if (!ep->enabled)
299 return 0;
300
301 ret = ep->ops->disable(ep);
302 if (ret)
303 return ret;
304
305 ep->enabled = false;
306
307 return 0;
308}
309
310/**
311 * usb_ep_alloc_request - allocate a request object to use with this endpoint
312 * @ep:the endpoint to be used with with the request
313 * @gfp_flags:GFP_* flags to use
314 *
315 * Request objects must be allocated with this call, since they normally
316 * need controller-specific setup and may even need endpoint-specific
317 * resources such as allocation of DMA descriptors.
318 * Requests may be submitted with usb_ep_queue(), and receive a single
319 * completion callback. Free requests with usb_ep_free_request(), when
320 * they are no longer needed.
321 *
322 * Returns the request, or null if one could not be allocated.
323 */
324static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, 252static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
325 gfp_t gfp_flags) 253 gfp_t gfp_flags)
326{ 254{ return NULL; }
327 return ep->ops->alloc_request(ep, gfp_flags);
328}
329
330/**
331 * usb_ep_free_request - frees a request object
332 * @ep:the endpoint associated with the request
333 * @req:the request being freed
334 *
335 * Reverses the effect of usb_ep_alloc_request().
336 * Caller guarantees the request is not queued, and that it will
337 * no longer be requeued (or otherwise used).
338 */
339static inline void usb_ep_free_request(struct usb_ep *ep, 255static inline void usb_ep_free_request(struct usb_ep *ep,
340 struct usb_request *req) 256 struct usb_request *req)
341{ 257{ }
342 ep->ops->free_request(ep, req); 258static inline int usb_ep_queue(struct usb_ep *ep, struct usb_request *req,
343} 259 gfp_t gfp_flags)
344 260{ return 0; }
345/**
346 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
347 * @ep:the endpoint associated with the request
348 * @req:the request being submitted
349 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
350 * pre-allocate all necessary memory with the request.
351 *
352 * This tells the device controller to perform the specified request through
353 * that endpoint (reading or writing a buffer). When the request completes,
354 * including being canceled by usb_ep_dequeue(), the request's completion
355 * routine is called to return the request to the driver. Any endpoint
356 * (except control endpoints like ep0) may have more than one transfer
357 * request queued; they complete in FIFO order. Once a gadget driver
358 * submits a request, that request may not be examined or modified until it
359 * is given back to that driver through the completion callback.
360 *
361 * Each request is turned into one or more packets. The controller driver
362 * never merges adjacent requests into the same packet. OUT transfers
363 * will sometimes use data that's already buffered in the hardware.
364 * Drivers can rely on the fact that the first byte of the request's buffer
365 * always corresponds to the first byte of some USB packet, for both
366 * IN and OUT transfers.
367 *
368 * Bulk endpoints can queue any amount of data; the transfer is packetized
369 * automatically. The last packet will be short if the request doesn't fill it
370 * out completely. Zero length packets (ZLPs) should be avoided in portable
371 * protocols since not all usb hardware can successfully handle zero length
372 * packets. (ZLPs may be explicitly written, and may be implicitly written if
373 * the request 'zero' flag is set.) Bulk endpoints may also be used
374 * for interrupt transfers; but the reverse is not true, and some endpoints
375 * won't support every interrupt transfer. (Such as 768 byte packets.)
376 *
377 * Interrupt-only endpoints are less functional than bulk endpoints, for
378 * example by not supporting queueing or not handling buffers that are
379 * larger than the endpoint's maxpacket size. They may also treat data
380 * toggle differently.
381 *
382 * Control endpoints ... after getting a setup() callback, the driver queues
383 * one response (even if it would be zero length). That enables the
384 * status ack, after transferring data as specified in the response. Setup
385 * functions may return negative error codes to generate protocol stalls.
386 * (Note that some USB device controllers disallow protocol stall responses
387 * in some cases.) When control responses are deferred (the response is
388 * written after the setup callback returns), then usb_ep_set_halt() may be
389 * used on ep0 to trigger protocol stalls. Depending on the controller,
390 * it may not be possible to trigger a status-stage protocol stall when the
391 * data stage is over, that is, from within the response's completion
392 * routine.
393 *
394 * For periodic endpoints, like interrupt or isochronous ones, the usb host
395 * arranges to poll once per interval, and the gadget driver usually will
396 * have queued some data to transfer at that time.
397 *
398 * Returns zero, or a negative error code. Endpoints that are not enabled
399 * report errors; errors will also be
400 * reported when the usb peripheral is disconnected.
401 */
402static inline int usb_ep_queue(struct usb_ep *ep,
403 struct usb_request *req, gfp_t gfp_flags)
404{
405 if (WARN_ON_ONCE(!ep->enabled && ep->address))
406 return -ESHUTDOWN;
407
408 return ep->ops->queue(ep, req, gfp_flags);
409}
410
411/**
412 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
413 * @ep:the endpoint associated with the request
414 * @req:the request being canceled
415 *
416 * If the request is still active on the endpoint, it is dequeued and its
417 * completion routine is called (with status -ECONNRESET); else a negative
418 * error code is returned. This is guaranteed to happen before the call to
419 * usb_ep_dequeue() returns.
420 *
421 * Note that some hardware can't clear out write fifos (to unlink the request
422 * at the head of the queue) except as part of disconnecting from usb. Such
423 * restrictions prevent drivers from supporting configuration changes,
424 * even to configuration zero (a "chapter 9" requirement).
425 */
426static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 261static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
427{ 262{ return 0; }
428 return ep->ops->dequeue(ep, req);
429}
430
431/**
432 * usb_ep_set_halt - sets the endpoint halt feature.
433 * @ep: the non-isochronous endpoint being stalled
434 *
435 * Use this to stall an endpoint, perhaps as an error report.
436 * Except for control endpoints,
437 * the endpoint stays halted (will not stream any data) until the host
438 * clears this feature; drivers may need to empty the endpoint's request
439 * queue first, to make sure no inappropriate transfers happen.
440 *
441 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
442 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
443 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
444 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
445 *
446 * Returns zero, or a negative error code. On success, this call sets
447 * underlying hardware state that blocks data transfers.
448 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
449 * transfer requests are still queued, or if the controller hardware
450 * (usually a FIFO) still holds bytes that the host hasn't collected.
451 */
452static inline int usb_ep_set_halt(struct usb_ep *ep) 263static inline int usb_ep_set_halt(struct usb_ep *ep)
453{ 264{ return 0; }
454 return ep->ops->set_halt(ep, 1);
455}
456
457/**
458 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
459 * @ep:the bulk or interrupt endpoint being reset
460 *
461 * Use this when responding to the standard usb "set interface" request,
462 * for endpoints that aren't reconfigured, after clearing any other state
463 * in the endpoint's i/o queue.
464 *
465 * Returns zero, or a negative error code. On success, this call clears
466 * the underlying hardware state reflecting endpoint halt and data toggle.
467 * Note that some hardware can't support this request (like pxa2xx_udc),
468 * and accordingly can't correctly implement interface altsettings.
469 */
470static inline int usb_ep_clear_halt(struct usb_ep *ep) 265static inline int usb_ep_clear_halt(struct usb_ep *ep)
471{ 266{ return 0; }
472 return ep->ops->set_halt(ep, 0); 267static inline int usb_ep_set_wedge(struct usb_ep *ep)
473} 268{ return 0; }
474
475/**
476 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
477 * @ep: the endpoint being wedged
478 *
479 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
480 * requests. If the gadget driver clears the halt status, it will
481 * automatically unwedge the endpoint.
482 *
483 * Returns zero on success, else negative errno.
484 */
485static inline int
486usb_ep_set_wedge(struct usb_ep *ep)
487{
488 if (ep->ops->set_wedge)
489 return ep->ops->set_wedge(ep);
490 else
491 return ep->ops->set_halt(ep, 1);
492}
493
494/**
495 * usb_ep_fifo_status - returns number of bytes in fifo, or error
496 * @ep: the endpoint whose fifo status is being checked.
497 *
498 * FIFO endpoints may have "unclaimed data" in them in certain cases,
499 * such as after aborted transfers. Hosts may not have collected all
500 * the IN data written by the gadget driver (and reported by a request
501 * completion). The gadget driver may not have collected all the data
502 * written OUT to it by the host. Drivers that need precise handling for
503 * fault reporting or recovery may need to use this call.
504 *
505 * This returns the number of such bytes in the fifo, or a negative
506 * errno if the endpoint doesn't use a FIFO or doesn't support such
507 * precise handling.
508 */
509static inline int usb_ep_fifo_status(struct usb_ep *ep) 269static inline int usb_ep_fifo_status(struct usb_ep *ep)
510{ 270{ return 0; }
511 if (ep->ops->fifo_status)
512 return ep->ops->fifo_status(ep);
513 else
514 return -EOPNOTSUPP;
515}
516
517/**
518 * usb_ep_fifo_flush - flushes contents of a fifo
519 * @ep: the endpoint whose fifo is being flushed.
520 *
521 * This call may be used to flush the "unclaimed data" that may exist in
522 * an endpoint fifo after abnormal transaction terminations. The call
523 * must never be used except when endpoint is not being used for any
524 * protocol translation.
525 */
526static inline void usb_ep_fifo_flush(struct usb_ep *ep) 271static inline void usb_ep_fifo_flush(struct usb_ep *ep)
527{ 272{ }
528 if (ep->ops->fifo_flush) 273#endif /* USB_GADGET */
529 ep->ops->fifo_flush(ep);
530}
531
532 274
533/*-------------------------------------------------------------------------*/ 275/*-------------------------------------------------------------------------*/
534 276
@@ -760,251 +502,44 @@ static inline int gadget_is_otg(struct usb_gadget *g)
760#endif 502#endif
761} 503}
762 504
763/** 505/*-------------------------------------------------------------------------*/
764 * usb_gadget_frame_number - returns the current frame number
765 * @gadget: controller that reports the frame number
766 *
767 * Returns the usb frame number, normally eleven bits from a SOF packet,
768 * or negative errno if this device doesn't support this capability.
769 */
770static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
771{
772 return gadget->ops->get_frame(gadget);
773}
774 506
775/** 507#if IS_ENABLED(CONFIG_USB_GADGET)
776 * usb_gadget_wakeup - tries to wake up the host connected to this gadget 508int usb_gadget_frame_number(struct usb_gadget *gadget);
777 * @gadget: controller used to wake up the host 509int usb_gadget_wakeup(struct usb_gadget *gadget);
778 * 510int usb_gadget_set_selfpowered(struct usb_gadget *gadget);
779 * Returns zero on success, else negative error code if the hardware 511int usb_gadget_clear_selfpowered(struct usb_gadget *gadget);
780 * doesn't support such attempts, or its support has not been enabled 512int usb_gadget_vbus_connect(struct usb_gadget *gadget);
781 * by the usb host. Drivers must return device descriptors that report 513int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA);
782 * their ability to support this, or hosts won't enable it. 514int usb_gadget_vbus_disconnect(struct usb_gadget *gadget);
783 * 515int usb_gadget_connect(struct usb_gadget *gadget);
784 * This may also try to use SRP to wake the host and start enumeration, 516int usb_gadget_disconnect(struct usb_gadget *gadget);
785 * even if OTG isn't otherwise in use. OTG devices may also start 517int usb_gadget_deactivate(struct usb_gadget *gadget);
786 * remote wakeup even when hosts don't explicitly enable it. 518int usb_gadget_activate(struct usb_gadget *gadget);
787 */ 519#else
520static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
521{ return 0; }
788static inline int usb_gadget_wakeup(struct usb_gadget *gadget) 522static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
789{ 523{ return 0; }
790 if (!gadget->ops->wakeup)
791 return -EOPNOTSUPP;
792 return gadget->ops->wakeup(gadget);
793}
794
795/**
796 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
797 * @gadget:the device being declared as self-powered
798 *
799 * this affects the device status reported by the hardware driver
800 * to reflect that it now has a local power supply.
801 *
802 * returns zero on success, else negative errno.
803 */
804static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget) 524static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
805{ 525{ return 0; }
806 if (!gadget->ops->set_selfpowered)
807 return -EOPNOTSUPP;
808 return gadget->ops->set_selfpowered(gadget, 1);
809}
810
811/**
812 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
813 * @gadget:the device being declared as bus-powered
814 *
815 * this affects the device status reported by the hardware driver.
816 * some hardware may not support bus-powered operation, in which
817 * case this feature's value can never change.
818 *
819 * returns zero on success, else negative errno.
820 */
821static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget) 526static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
822{ 527{ return 0; }
823 if (!gadget->ops->set_selfpowered)
824 return -EOPNOTSUPP;
825 return gadget->ops->set_selfpowered(gadget, 0);
826}
827
828/**
829 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
830 * @gadget:The device which now has VBUS power.
831 * Context: can sleep
832 *
833 * This call is used by a driver for an external transceiver (or GPIO)
834 * that detects a VBUS power session starting. Common responses include
835 * resuming the controller, activating the D+ (or D-) pullup to let the
836 * host detect that a USB device is attached, and starting to draw power
837 * (8mA or possibly more, especially after SET_CONFIGURATION).
838 *
839 * Returns zero on success, else negative errno.
840 */
841static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget) 528static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
842{ 529{ return 0; }
843 if (!gadget->ops->vbus_session)
844 return -EOPNOTSUPP;
845 return gadget->ops->vbus_session(gadget, 1);
846}
847
848/**
849 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
850 * @gadget:The device whose VBUS usage is being described
851 * @mA:How much current to draw, in milliAmperes. This should be twice
852 * the value listed in the configuration descriptor bMaxPower field.
853 *
854 * This call is used by gadget drivers during SET_CONFIGURATION calls,
855 * reporting how much power the device may consume. For example, this
856 * could affect how quickly batteries are recharged.
857 *
858 * Returns zero on success, else negative errno.
859 */
860static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 530static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
861{ 531{ return 0; }
862 if (!gadget->ops->vbus_draw)
863 return -EOPNOTSUPP;
864 return gadget->ops->vbus_draw(gadget, mA);
865}
866
867/**
868 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
869 * @gadget:the device whose VBUS supply is being described
870 * Context: can sleep
871 *
872 * This call is used by a driver for an external transceiver (or GPIO)
873 * that detects a VBUS power session ending. Common responses include
874 * reversing everything done in usb_gadget_vbus_connect().
875 *
876 * Returns zero on success, else negative errno.
877 */
878static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget) 532static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
879{ 533{ return 0; }
880 if (!gadget->ops->vbus_session)
881 return -EOPNOTSUPP;
882 return gadget->ops->vbus_session(gadget, 0);
883}
884
885/**
886 * usb_gadget_connect - software-controlled connect to USB host
887 * @gadget:the peripheral being connected
888 *
889 * Enables the D+ (or potentially D-) pullup. The host will start
890 * enumerating this gadget when the pullup is active and a VBUS session
891 * is active (the link is powered). This pullup is always enabled unless
892 * usb_gadget_disconnect() has been used to disable it.
893 *
894 * Returns zero on success, else negative errno.
895 */
896static inline int usb_gadget_connect(struct usb_gadget *gadget) 534static inline int usb_gadget_connect(struct usb_gadget *gadget)
897{ 535{ return 0; }
898 int ret;
899
900 if (!gadget->ops->pullup)
901 return -EOPNOTSUPP;
902
903 if (gadget->deactivated) {
904 /*
905 * If gadget is deactivated we only save new state.
906 * Gadget will be connected automatically after activation.
907 */
908 gadget->connected = true;
909 return 0;
910 }
911
912 ret = gadget->ops->pullup(gadget, 1);
913 if (!ret)
914 gadget->connected = 1;
915 return ret;
916}
917
918/**
919 * usb_gadget_disconnect - software-controlled disconnect from USB host
920 * @gadget:the peripheral being disconnected
921 *
922 * Disables the D+ (or potentially D-) pullup, which the host may see
923 * as a disconnect (when a VBUS session is active). Not all systems
924 * support software pullup controls.
925 *
926 * Returns zero on success, else negative errno.
927 */
928static inline int usb_gadget_disconnect(struct usb_gadget *gadget) 536static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
929{ 537{ return 0; }
930 int ret;
931
932 if (!gadget->ops->pullup)
933 return -EOPNOTSUPP;
934
935 if (gadget->deactivated) {
936 /*
937 * If gadget is deactivated we only save new state.
938 * Gadget will stay disconnected after activation.
939 */
940 gadget->connected = false;
941 return 0;
942 }
943
944 ret = gadget->ops->pullup(gadget, 0);
945 if (!ret)
946 gadget->connected = 0;
947 return ret;
948}
949
950/**
951 * usb_gadget_deactivate - deactivate function which is not ready to work
952 * @gadget: the peripheral being deactivated
953 *
954 * This routine may be used during the gadget driver bind() call to prevent
955 * the peripheral from ever being visible to the USB host, unless later
956 * usb_gadget_activate() is called. For example, user mode components may
957 * need to be activated before the system can talk to hosts.
958 *
959 * Returns zero on success, else negative errno.
960 */
961static inline int usb_gadget_deactivate(struct usb_gadget *gadget) 538static inline int usb_gadget_deactivate(struct usb_gadget *gadget)
962{ 539{ return 0; }
963 int ret;
964
965 if (gadget->deactivated)
966 return 0;
967
968 if (gadget->connected) {
969 ret = usb_gadget_disconnect(gadget);
970 if (ret)
971 return ret;
972 /*
973 * If gadget was being connected before deactivation, we want
974 * to reconnect it in usb_gadget_activate().
975 */
976 gadget->connected = true;
977 }
978 gadget->deactivated = true;
979
980 return 0;
981}
982
983/**
984 * usb_gadget_activate - activate function which is not ready to work
985 * @gadget: the peripheral being activated
986 *
987 * This routine activates gadget which was previously deactivated with
988 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
989 *
990 * Returns zero on success, else negative errno.
991 */
992static inline int usb_gadget_activate(struct usb_gadget *gadget) 540static inline int usb_gadget_activate(struct usb_gadget *gadget)
993{ 541{ return 0; }
994 if (!gadget->deactivated) 542#endif /* CONFIG_USB_GADGET */
995 return 0;
996
997 gadget->deactivated = false;
998
999 /*
1000 * If gadget has been connected before deactivation, or became connected
1001 * while it was being deactivated, we call usb_gadget_connect().
1002 */
1003 if (gadget->connected)
1004 return usb_gadget_connect(gadget);
1005
1006 return 0;
1007}
1008 543
1009/*-------------------------------------------------------------------------*/ 544/*-------------------------------------------------------------------------*/
1010 545