2 * udc.c - Core UDC Framework
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/err.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/workqueue.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb.h>
35 * struct usb_udc - describes one usb device controller
36 * @driver - the gadget driver pointer. For use by the class code
37 * @dev - the child device to the actual controller
38 * @gadget - the gadget. For use by the class code
39 * @list - for use by the udc class driver
40 * @vbus - for udcs who care about vbus status, this value is real vbus status;
41 * for udcs who do not care about vbus status, this value is always true
43 * This represents the internal data structure which is used by the UDC-class
44 * to hold information about udc driver and gadget together.
47 struct usb_gadget_driver
*driver
;
48 struct usb_gadget
*gadget
;
50 struct list_head list
;
54 static struct class *udc_class
;
55 static LIST_HEAD(udc_list
);
56 static LIST_HEAD(gadget_driver_pending_list
);
57 static DEFINE_MUTEX(udc_lock
);
59 static int udc_bind_to_driver(struct usb_udc
*udc
,
60 struct usb_gadget_driver
*driver
);
62 /* ------------------------------------------------------------------------- */
65 * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
66 * @ep:the endpoint being configured
67 * @maxpacket_limit:value of maximum packet size limit
69 * This function should be used only in UDC drivers to initialize endpoint
70 * (usually in probe function).
72 void usb_ep_set_maxpacket_limit(struct usb_ep
*ep
,
73 unsigned maxpacket_limit
)
75 ep
->maxpacket_limit
= maxpacket_limit
;
76 ep
->maxpacket
= maxpacket_limit
;
78 trace_usb_ep_set_maxpacket_limit(ep
, 0);
80 EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit
);
83 * usb_ep_enable - configure endpoint, making it usable
84 * @ep:the endpoint being configured. may not be the endpoint named "ep0".
85 * drivers discover endpoints through the ep_list of a usb_gadget.
87 * When configurations are set, or when interface settings change, the driver
88 * will enable or disable the relevant endpoints. while it is enabled, an
89 * endpoint may be used for i/o until the driver receives a disconnect() from
90 * the host or until the endpoint is disabled.
92 * the ep0 implementation (which calls this routine) must ensure that the
93 * hardware capabilities of each endpoint match the descriptor provided
94 * for it. for example, an endpoint named "ep2in-bulk" would be usable
95 * for interrupt transfers as well as bulk, but it likely couldn't be used
96 * for iso transfers or for endpoint 14. some endpoints are fully
97 * configurable, with more generic names like "ep-a". (remember that for
98 * USB, "in" means "towards the USB master".)
100 * returns zero, or a negative error code.
102 int usb_ep_enable(struct usb_ep
*ep
)
109 ret
= ep
->ops
->enable(ep
, ep
->desc
);
116 trace_usb_ep_enable(ep
, ret
);
120 EXPORT_SYMBOL_GPL(usb_ep_enable
);
123 * usb_ep_disable - endpoint is no longer usable
124 * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
126 * no other task may be using this endpoint when this is called.
127 * any pending and uncompleted requests will complete with status
128 * indicating disconnect (-ESHUTDOWN) before this call returns.
129 * gadget drivers must call usb_ep_enable() again before queueing
130 * requests to the endpoint.
132 * returns zero, or a negative error code.
134 int usb_ep_disable(struct usb_ep
*ep
)
141 ret
= ep
->ops
->disable(ep
);
150 trace_usb_ep_disable(ep
, ret
);
154 EXPORT_SYMBOL_GPL(usb_ep_disable
);
157 * usb_ep_alloc_request - allocate a request object to use with this endpoint
158 * @ep:the endpoint to be used with with the request
159 * @gfp_flags:GFP_* flags to use
161 * Request objects must be allocated with this call, since they normally
162 * need controller-specific setup and may even need endpoint-specific
163 * resources such as allocation of DMA descriptors.
164 * Requests may be submitted with usb_ep_queue(), and receive a single
165 * completion callback. Free requests with usb_ep_free_request(), when
166 * they are no longer needed.
168 * Returns the request, or null if one could not be allocated.
170 struct usb_request
*usb_ep_alloc_request(struct usb_ep
*ep
,
173 struct usb_request
*req
= NULL
;
175 req
= ep
->ops
->alloc_request(ep
, gfp_flags
);
177 trace_usb_ep_alloc_request(ep
, req
, req
? 0 : -ENOMEM
);
181 EXPORT_SYMBOL_GPL(usb_ep_alloc_request
);
184 * usb_ep_free_request - frees a request object
185 * @ep:the endpoint associated with the request
186 * @req:the request being freed
188 * Reverses the effect of usb_ep_alloc_request().
189 * Caller guarantees the request is not queued, and that it will
190 * no longer be requeued (or otherwise used).
192 void usb_ep_free_request(struct usb_ep
*ep
,
193 struct usb_request
*req
)
195 ep
->ops
->free_request(ep
, req
);
196 trace_usb_ep_free_request(ep
, req
, 0);
198 EXPORT_SYMBOL_GPL(usb_ep_free_request
);
201 * usb_ep_queue - queues (submits) an I/O request to an endpoint.
202 * @ep:the endpoint associated with the request
203 * @req:the request being submitted
204 * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
205 * pre-allocate all necessary memory with the request.
207 * This tells the device controller to perform the specified request through
208 * that endpoint (reading or writing a buffer). When the request completes,
209 * including being canceled by usb_ep_dequeue(), the request's completion
210 * routine is called to return the request to the driver. Any endpoint
211 * (except control endpoints like ep0) may have more than one transfer
212 * request queued; they complete in FIFO order. Once a gadget driver
213 * submits a request, that request may not be examined or modified until it
214 * is given back to that driver through the completion callback.
216 * Each request is turned into one or more packets. The controller driver
217 * never merges adjacent requests into the same packet. OUT transfers
218 * will sometimes use data that's already buffered in the hardware.
219 * Drivers can rely on the fact that the first byte of the request's buffer
220 * always corresponds to the first byte of some USB packet, for both
221 * IN and OUT transfers.
223 * Bulk endpoints can queue any amount of data; the transfer is packetized
224 * automatically. The last packet will be short if the request doesn't fill it
225 * out completely. Zero length packets (ZLPs) should be avoided in portable
226 * protocols since not all usb hardware can successfully handle zero length
227 * packets. (ZLPs may be explicitly written, and may be implicitly written if
228 * the request 'zero' flag is set.) Bulk endpoints may also be used
229 * for interrupt transfers; but the reverse is not true, and some endpoints
230 * won't support every interrupt transfer. (Such as 768 byte packets.)
232 * Interrupt-only endpoints are less functional than bulk endpoints, for
233 * example by not supporting queueing or not handling buffers that are
234 * larger than the endpoint's maxpacket size. They may also treat data
235 * toggle differently.
237 * Control endpoints ... after getting a setup() callback, the driver queues
238 * one response (even if it would be zero length). That enables the
239 * status ack, after transferring data as specified in the response. Setup
240 * functions may return negative error codes to generate protocol stalls.
241 * (Note that some USB device controllers disallow protocol stall responses
242 * in some cases.) When control responses are deferred (the response is
243 * written after the setup callback returns), then usb_ep_set_halt() may be
244 * used on ep0 to trigger protocol stalls. Depending on the controller,
245 * it may not be possible to trigger a status-stage protocol stall when the
246 * data stage is over, that is, from within the response's completion
249 * For periodic endpoints, like interrupt or isochronous ones, the usb host
250 * arranges to poll once per interval, and the gadget driver usually will
251 * have queued some data to transfer at that time.
253 * Returns zero, or a negative error code. Endpoints that are not enabled
254 * report errors; errors will also be
255 * reported when the usb peripheral is disconnected.
257 int usb_ep_queue(struct usb_ep
*ep
,
258 struct usb_request
*req
, gfp_t gfp_flags
)
262 if (WARN_ON_ONCE(!ep
->enabled
&& ep
->address
)) {
267 ret
= ep
->ops
->queue(ep
, req
, gfp_flags
);
270 trace_usb_ep_queue(ep
, req
, ret
);
274 EXPORT_SYMBOL_GPL(usb_ep_queue
);
277 * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
278 * @ep:the endpoint associated with the request
279 * @req:the request being canceled
281 * If the request is still active on the endpoint, it is dequeued and its
282 * completion routine is called (with status -ECONNRESET); else a negative
283 * error code is returned. This is guaranteed to happen before the call to
284 * usb_ep_dequeue() returns.
286 * Note that some hardware can't clear out write fifos (to unlink the request
287 * at the head of the queue) except as part of disconnecting from usb. Such
288 * restrictions prevent drivers from supporting configuration changes,
289 * even to configuration zero (a "chapter 9" requirement).
291 int usb_ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
295 ret
= ep
->ops
->dequeue(ep
, req
);
296 trace_usb_ep_dequeue(ep
, req
, ret
);
300 EXPORT_SYMBOL_GPL(usb_ep_dequeue
);
303 * usb_ep_set_halt - sets the endpoint halt feature.
304 * @ep: the non-isochronous endpoint being stalled
306 * Use this to stall an endpoint, perhaps as an error report.
307 * Except for control endpoints,
308 * the endpoint stays halted (will not stream any data) until the host
309 * clears this feature; drivers may need to empty the endpoint's request
310 * queue first, to make sure no inappropriate transfers happen.
312 * Note that while an endpoint CLEAR_FEATURE will be invisible to the
313 * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
314 * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
315 * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
317 * Returns zero, or a negative error code. On success, this call sets
318 * underlying hardware state that blocks data transfers.
319 * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
320 * transfer requests are still queued, or if the controller hardware
321 * (usually a FIFO) still holds bytes that the host hasn't collected.
323 int usb_ep_set_halt(struct usb_ep
*ep
)
327 ret
= ep
->ops
->set_halt(ep
, 1);
328 trace_usb_ep_set_halt(ep
, ret
);
332 EXPORT_SYMBOL_GPL(usb_ep_set_halt
);
335 * usb_ep_clear_halt - clears endpoint halt, and resets toggle
336 * @ep:the bulk or interrupt endpoint being reset
338 * Use this when responding to the standard usb "set interface" request,
339 * for endpoints that aren't reconfigured, after clearing any other state
340 * in the endpoint's i/o queue.
342 * Returns zero, or a negative error code. On success, this call clears
343 * the underlying hardware state reflecting endpoint halt and data toggle.
344 * Note that some hardware can't support this request (like pxa2xx_udc),
345 * and accordingly can't correctly implement interface altsettings.
347 int usb_ep_clear_halt(struct usb_ep
*ep
)
351 ret
= ep
->ops
->set_halt(ep
, 0);
352 trace_usb_ep_clear_halt(ep
, ret
);
356 EXPORT_SYMBOL_GPL(usb_ep_clear_halt
);
359 * usb_ep_set_wedge - sets the halt feature and ignores clear requests
360 * @ep: the endpoint being wedged
362 * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
363 * requests. If the gadget driver clears the halt status, it will
364 * automatically unwedge the endpoint.
366 * Returns zero on success, else negative errno.
368 int usb_ep_set_wedge(struct usb_ep
*ep
)
372 if (ep
->ops
->set_wedge
)
373 ret
= ep
->ops
->set_wedge(ep
);
375 ret
= ep
->ops
->set_halt(ep
, 1);
377 trace_usb_ep_set_wedge(ep
, ret
);
381 EXPORT_SYMBOL_GPL(usb_ep_set_wedge
);
384 * usb_ep_fifo_status - returns number of bytes in fifo, or error
385 * @ep: the endpoint whose fifo status is being checked.
387 * FIFO endpoints may have "unclaimed data" in them in certain cases,
388 * such as after aborted transfers. Hosts may not have collected all
389 * the IN data written by the gadget driver (and reported by a request
390 * completion). The gadget driver may not have collected all the data
391 * written OUT to it by the host. Drivers that need precise handling for
392 * fault reporting or recovery may need to use this call.
394 * This returns the number of such bytes in the fifo, or a negative
395 * errno if the endpoint doesn't use a FIFO or doesn't support such
398 int usb_ep_fifo_status(struct usb_ep
*ep
)
402 if (ep
->ops
->fifo_status
)
403 ret
= ep
->ops
->fifo_status(ep
);
407 trace_usb_ep_fifo_status(ep
, ret
);
411 EXPORT_SYMBOL_GPL(usb_ep_fifo_status
);
414 * usb_ep_fifo_flush - flushes contents of a fifo
415 * @ep: the endpoint whose fifo is being flushed.
417 * This call may be used to flush the "unclaimed data" that may exist in
418 * an endpoint fifo after abnormal transaction terminations. The call
419 * must never be used except when endpoint is not being used for any
420 * protocol translation.
422 void usb_ep_fifo_flush(struct usb_ep
*ep
)
424 if (ep
->ops
->fifo_flush
)
425 ep
->ops
->fifo_flush(ep
);
427 trace_usb_ep_fifo_flush(ep
, 0);
429 EXPORT_SYMBOL_GPL(usb_ep_fifo_flush
);
431 /* ------------------------------------------------------------------------- */
434 * usb_gadget_frame_number - returns the current frame number
435 * @gadget: controller that reports the frame number
437 * Returns the usb frame number, normally eleven bits from a SOF packet,
438 * or negative errno if this device doesn't support this capability.
440 int usb_gadget_frame_number(struct usb_gadget
*gadget
)
444 ret
= gadget
->ops
->get_frame(gadget
);
446 trace_usb_gadget_frame_number(gadget
, ret
);
450 EXPORT_SYMBOL_GPL(usb_gadget_frame_number
);
453 * usb_gadget_wakeup - tries to wake up the host connected to this gadget
454 * @gadget: controller used to wake up the host
456 * Returns zero on success, else negative error code if the hardware
457 * doesn't support such attempts, or its support has not been enabled
458 * by the usb host. Drivers must return device descriptors that report
459 * their ability to support this, or hosts won't enable it.
461 * This may also try to use SRP to wake the host and start enumeration,
462 * even if OTG isn't otherwise in use. OTG devices may also start
463 * remote wakeup even when hosts don't explicitly enable it.
465 int usb_gadget_wakeup(struct usb_gadget
*gadget
)
469 if (!gadget
->ops
->wakeup
) {
474 ret
= gadget
->ops
->wakeup(gadget
);
477 trace_usb_gadget_wakeup(gadget
, ret
);
481 EXPORT_SYMBOL_GPL(usb_gadget_wakeup
);
484 * usb_gadget_set_selfpowered - sets the device selfpowered feature.
485 * @gadget:the device being declared as self-powered
487 * this affects the device status reported by the hardware driver
488 * to reflect that it now has a local power supply.
490 * returns zero on success, else negative errno.
492 int usb_gadget_set_selfpowered(struct usb_gadget
*gadget
)
496 if (!gadget
->ops
->set_selfpowered
) {
501 ret
= gadget
->ops
->set_selfpowered(gadget
, 1);
504 trace_usb_gadget_set_selfpowered(gadget
, ret
);
508 EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered
);
511 * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
512 * @gadget:the device being declared as bus-powered
514 * this affects the device status reported by the hardware driver.
515 * some hardware may not support bus-powered operation, in which
516 * case this feature's value can never change.
518 * returns zero on success, else negative errno.
520 int usb_gadget_clear_selfpowered(struct usb_gadget
*gadget
)
524 if (!gadget
->ops
->set_selfpowered
) {
529 ret
= gadget
->ops
->set_selfpowered(gadget
, 0);
532 trace_usb_gadget_clear_selfpowered(gadget
, ret
);
536 EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered
);
539 * usb_gadget_vbus_connect - Notify controller that VBUS is powered
540 * @gadget:The device which now has VBUS power.
543 * This call is used by a driver for an external transceiver (or GPIO)
544 * that detects a VBUS power session starting. Common responses include
545 * resuming the controller, activating the D+ (or D-) pullup to let the
546 * host detect that a USB device is attached, and starting to draw power
547 * (8mA or possibly more, especially after SET_CONFIGURATION).
549 * Returns zero on success, else negative errno.
551 int usb_gadget_vbus_connect(struct usb_gadget
*gadget
)
555 if (!gadget
->ops
->vbus_session
) {
560 ret
= gadget
->ops
->vbus_session(gadget
, 1);
563 trace_usb_gadget_vbus_connect(gadget
, ret
);
567 EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect
);
570 * usb_gadget_vbus_draw - constrain controller's VBUS power usage
571 * @gadget:The device whose VBUS usage is being described
572 * @mA:How much current to draw, in milliAmperes. This should be twice
573 * the value listed in the configuration descriptor bMaxPower field.
575 * This call is used by gadget drivers during SET_CONFIGURATION calls,
576 * reporting how much power the device may consume. For example, this
577 * could affect how quickly batteries are recharged.
579 * Returns zero on success, else negative errno.
581 int usb_gadget_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
585 if (!gadget
->ops
->vbus_draw
) {
590 ret
= gadget
->ops
->vbus_draw(gadget
, mA
);
595 trace_usb_gadget_vbus_draw(gadget
, ret
);
599 EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw
);
602 * usb_gadget_vbus_disconnect - notify controller about VBUS session end
603 * @gadget:the device whose VBUS supply is being described
606 * This call is used by a driver for an external transceiver (or GPIO)
607 * that detects a VBUS power session ending. Common responses include
608 * reversing everything done in usb_gadget_vbus_connect().
610 * Returns zero on success, else negative errno.
612 int usb_gadget_vbus_disconnect(struct usb_gadget
*gadget
)
616 if (!gadget
->ops
->vbus_session
) {
621 ret
= gadget
->ops
->vbus_session(gadget
, 0);
624 trace_usb_gadget_vbus_disconnect(gadget
, ret
);
628 EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect
);
631 * usb_gadget_connect - software-controlled connect to USB host
632 * @gadget:the peripheral being connected
634 * Enables the D+ (or potentially D-) pullup. The host will start
635 * enumerating this gadget when the pullup is active and a VBUS session
636 * is active (the link is powered). This pullup is always enabled unless
637 * usb_gadget_disconnect() has been used to disable it.
639 * Returns zero on success, else negative errno.
641 int usb_gadget_connect(struct usb_gadget
*gadget
)
645 if (!gadget
->ops
->pullup
) {
650 if (gadget
->deactivated
) {
652 * If gadget is deactivated we only save new state.
653 * Gadget will be connected automatically after activation.
655 gadget
->connected
= true;
659 ret
= gadget
->ops
->pullup(gadget
, 1);
661 gadget
->connected
= 1;
664 trace_usb_gadget_connect(gadget
, ret
);
668 EXPORT_SYMBOL_GPL(usb_gadget_connect
);
671 * usb_gadget_disconnect - software-controlled disconnect from USB host
672 * @gadget:the peripheral being disconnected
674 * Disables the D+ (or potentially D-) pullup, which the host may see
675 * as a disconnect (when a VBUS session is active). Not all systems
676 * support software pullup controls.
678 * Returns zero on success, else negative errno.
680 int usb_gadget_disconnect(struct usb_gadget
*gadget
)
684 if (!gadget
->ops
->pullup
) {
689 if (gadget
->deactivated
) {
691 * If gadget is deactivated we only save new state.
692 * Gadget will stay disconnected after activation.
694 gadget
->connected
= false;
698 ret
= gadget
->ops
->pullup(gadget
, 0);
700 gadget
->connected
= 0;
703 trace_usb_gadget_disconnect(gadget
, ret
);
707 EXPORT_SYMBOL_GPL(usb_gadget_disconnect
);
710 * usb_gadget_deactivate - deactivate function which is not ready to work
711 * @gadget: the peripheral being deactivated
713 * This routine may be used during the gadget driver bind() call to prevent
714 * the peripheral from ever being visible to the USB host, unless later
715 * usb_gadget_activate() is called. For example, user mode components may
716 * need to be activated before the system can talk to hosts.
718 * Returns zero on success, else negative errno.
720 int usb_gadget_deactivate(struct usb_gadget
*gadget
)
724 if (gadget
->deactivated
)
727 if (gadget
->connected
) {
728 ret
= usb_gadget_disconnect(gadget
);
733 * If gadget was being connected before deactivation, we want
734 * to reconnect it in usb_gadget_activate().
736 gadget
->connected
= true;
738 gadget
->deactivated
= true;
741 trace_usb_gadget_deactivate(gadget
, ret
);
745 EXPORT_SYMBOL_GPL(usb_gadget_deactivate
);
748 * usb_gadget_activate - activate function which is not ready to work
749 * @gadget: the peripheral being activated
751 * This routine activates gadget which was previously deactivated with
752 * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
754 * Returns zero on success, else negative errno.
756 int usb_gadget_activate(struct usb_gadget
*gadget
)
760 if (!gadget
->deactivated
)
763 gadget
->deactivated
= false;
766 * If gadget has been connected before deactivation, or became connected
767 * while it was being deactivated, we call usb_gadget_connect().
769 if (gadget
->connected
)
770 ret
= usb_gadget_connect(gadget
);
773 trace_usb_gadget_activate(gadget
, ret
);
777 EXPORT_SYMBOL_GPL(usb_gadget_activate
);
779 /* ------------------------------------------------------------------------- */
781 #ifdef CONFIG_HAS_DMA
783 int usb_gadget_map_request_by_dev(struct device
*dev
,
784 struct usb_request
*req
, int is_in
)
786 if (req
->length
== 0)
792 mapped
= dma_map_sg(dev
, req
->sg
, req
->num_sgs
,
793 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
795 dev_err(dev
, "failed to map SGs\n");
799 req
->num_mapped_sgs
= mapped
;
801 req
->dma
= dma_map_single(dev
, req
->buf
, req
->length
,
802 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
804 if (dma_mapping_error(dev
, req
->dma
)) {
805 dev_err(dev
, "failed to map buffer\n");
812 EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev
);
814 int usb_gadget_map_request(struct usb_gadget
*gadget
,
815 struct usb_request
*req
, int is_in
)
817 return usb_gadget_map_request_by_dev(gadget
->dev
.parent
, req
, is_in
);
819 EXPORT_SYMBOL_GPL(usb_gadget_map_request
);
821 void usb_gadget_unmap_request_by_dev(struct device
*dev
,
822 struct usb_request
*req
, int is_in
)
824 if (req
->length
== 0)
827 if (req
->num_mapped_sgs
) {
828 dma_unmap_sg(dev
, req
->sg
, req
->num_sgs
,
829 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
831 req
->num_mapped_sgs
= 0;
833 dma_unmap_single(dev
, req
->dma
, req
->length
,
834 is_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
837 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev
);
839 void usb_gadget_unmap_request(struct usb_gadget
*gadget
,
840 struct usb_request
*req
, int is_in
)
842 usb_gadget_unmap_request_by_dev(gadget
->dev
.parent
, req
, is_in
);
844 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request
);
846 #endif /* CONFIG_HAS_DMA */
848 /* ------------------------------------------------------------------------- */
851 * usb_gadget_giveback_request - give the request back to the gadget layer
852 * Context: in_interrupt()
854 * This is called by device controller drivers in order to return the
855 * completed request back to the gadget layer.
857 void usb_gadget_giveback_request(struct usb_ep
*ep
,
858 struct usb_request
*req
)
860 if (likely(req
->status
== 0))
861 usb_led_activity(USB_LED_EVENT_GADGET
);
863 trace_usb_gadget_giveback_request(ep
, req
, 0);
865 req
->complete(ep
, req
);
867 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request
);
869 /* ------------------------------------------------------------------------- */
872 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
873 * in second parameter or NULL if searched endpoint not found
874 * @g: controller to check for quirk
875 * @name: name of searched endpoint
877 struct usb_ep
*gadget_find_ep_by_name(struct usb_gadget
*g
, const char *name
)
881 gadget_for_each_ep(ep
, g
) {
882 if (!strcmp(ep
->name
, name
))
888 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name
);
890 /* ------------------------------------------------------------------------- */
892 int usb_gadget_ep_match_desc(struct usb_gadget
*gadget
,
893 struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
,
894 struct usb_ss_ep_comp_descriptor
*ep_comp
)
898 int num_req_streams
= 0;
900 /* endpoint already claimed? */
904 type
= usb_endpoint_type(desc
);
905 max
= 0x7ff & usb_endpoint_maxp(desc
);
907 if (usb_endpoint_dir_in(desc
) && !ep
->caps
.dir_in
)
909 if (usb_endpoint_dir_out(desc
) && !ep
->caps
.dir_out
)
912 if (max
> ep
->maxpacket_limit
)
915 /* "high bandwidth" works only at high speed */
916 if (!gadget_is_dualspeed(gadget
) && usb_endpoint_maxp(desc
) & (3<<11))
920 case USB_ENDPOINT_XFER_CONTROL
:
921 /* only support ep0 for portable CONTROL traffic */
923 case USB_ENDPOINT_XFER_ISOC
:
924 if (!ep
->caps
.type_iso
)
926 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
927 if (!gadget_is_dualspeed(gadget
) && max
> 1023)
930 case USB_ENDPOINT_XFER_BULK
:
931 if (!ep
->caps
.type_bulk
)
933 if (ep_comp
&& gadget_is_superspeed(gadget
)) {
934 /* Get the number of required streams from the
935 * EP companion descriptor and see if the EP
938 num_req_streams
= ep_comp
->bmAttributes
& 0x1f;
939 if (num_req_streams
> ep
->max_streams
)
943 case USB_ENDPOINT_XFER_INT
:
944 /* Bulk endpoints handle interrupt transfers,
945 * except the toggle-quirky iso-synch kind
947 if (!ep
->caps
.type_int
&& !ep
->caps
.type_bulk
)
949 /* INT: limit 64 bytes full speed, 1024 high/super speed */
950 if (!gadget_is_dualspeed(gadget
) && max
> 64)
957 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc
);
959 /* ------------------------------------------------------------------------- */
961 static void usb_gadget_state_work(struct work_struct
*work
)
963 struct usb_gadget
*gadget
= work_to_gadget(work
);
964 struct usb_udc
*udc
= gadget
->udc
;
967 sysfs_notify(&udc
->dev
.kobj
, NULL
, "state");
970 void usb_gadget_set_state(struct usb_gadget
*gadget
,
971 enum usb_device_state state
)
973 gadget
->state
= state
;
974 schedule_work(&gadget
->work
);
976 EXPORT_SYMBOL_GPL(usb_gadget_set_state
);
978 /* ------------------------------------------------------------------------- */
980 static void usb_udc_connect_control(struct usb_udc
*udc
)
983 usb_gadget_connect(udc
->gadget
);
985 usb_gadget_disconnect(udc
->gadget
);
989 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
990 * connect or disconnect gadget
991 * @gadget: The gadget which vbus change occurs
992 * @status: The vbus status
994 * The udc driver calls it when it wants to connect or disconnect gadget
995 * according to vbus status.
997 void usb_udc_vbus_handler(struct usb_gadget
*gadget
, bool status
)
999 struct usb_udc
*udc
= gadget
->udc
;
1003 usb_udc_connect_control(udc
);
1006 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler
);
1009 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1010 * @gadget: The gadget which bus reset occurs
1011 * @driver: The gadget driver we want to notify
1013 * If the udc driver has bus reset handler, it needs to call this when the bus
1014 * reset occurs, it notifies the gadget driver that the bus reset occurs as
1015 * well as updates gadget state.
1017 void usb_gadget_udc_reset(struct usb_gadget
*gadget
,
1018 struct usb_gadget_driver
*driver
)
1020 driver
->reset(gadget
);
1021 usb_gadget_set_state(gadget
, USB_STATE_DEFAULT
);
1023 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset
);
1026 * usb_gadget_udc_start - tells usb device controller to start up
1027 * @udc: The UDC to be started
1029 * This call is issued by the UDC Class driver when it's about
1030 * to register a gadget driver to the device controller, before
1031 * calling gadget driver's bind() method.
1033 * It allows the controller to be powered off until strictly
1034 * necessary to have it powered on.
1036 * Returns zero on success, else negative errno.
1038 static inline int usb_gadget_udc_start(struct usb_udc
*udc
)
1040 return udc
->gadget
->ops
->udc_start(udc
->gadget
, udc
->driver
);
1044 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
1045 * @gadget: The device we want to stop activity
1046 * @driver: The driver to unbind from @gadget
1048 * This call is issued by the UDC Class driver after calling
1049 * gadget driver's unbind() method.
1051 * The details are implementation specific, but it can go as
1052 * far as powering off UDC completely and disable its data
1055 static inline void usb_gadget_udc_stop(struct usb_udc
*udc
)
1057 udc
->gadget
->ops
->udc_stop(udc
->gadget
);
1061 * usb_udc_release - release the usb_udc struct
1062 * @dev: the dev member within usb_udc
1064 * This is called by driver's core in order to free memory once the last
1065 * reference is released.
1067 static void usb_udc_release(struct device
*dev
)
1069 struct usb_udc
*udc
;
1071 udc
= container_of(dev
, struct usb_udc
, dev
);
1072 dev_dbg(dev
, "releasing '%s'\n", dev_name(dev
));
1076 static const struct attribute_group
*usb_udc_attr_groups
[];
1078 static void usb_udc_nop_release(struct device
*dev
)
1080 dev_vdbg(dev
, "%s\n", __func__
);
1083 /* should be called with udc_lock held */
1084 static int check_pending_gadget_drivers(struct usb_udc
*udc
)
1086 struct usb_gadget_driver
*driver
;
1089 list_for_each_entry(driver
, &gadget_driver_pending_list
, pending
)
1090 if (!driver
->udc_name
|| strcmp(driver
->udc_name
,
1091 dev_name(&udc
->dev
)) == 0) {
1092 ret
= udc_bind_to_driver(udc
, driver
);
1093 if (ret
!= -EPROBE_DEFER
)
1094 list_del(&driver
->pending
);
1102 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
1103 * @parent: the parent device to this udc. Usually the controller driver's
1105 * @gadget: the gadget to be added to the list.
1106 * @release: a gadget release function.
1108 * Returns zero on success, negative errno otherwise.
1110 int usb_add_gadget_udc_release(struct device
*parent
, struct usb_gadget
*gadget
,
1111 void (*release
)(struct device
*dev
))
1113 struct usb_udc
*udc
;
1116 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
1120 dev_set_name(&gadget
->dev
, "gadget");
1121 INIT_WORK(&gadget
->work
, usb_gadget_state_work
);
1122 gadget
->dev
.parent
= parent
;
1125 gadget
->dev
.release
= release
;
1127 gadget
->dev
.release
= usb_udc_nop_release
;
1129 ret
= device_register(&gadget
->dev
);
1133 device_initialize(&udc
->dev
);
1134 udc
->dev
.release
= usb_udc_release
;
1135 udc
->dev
.class = udc_class
;
1136 udc
->dev
.groups
= usb_udc_attr_groups
;
1137 udc
->dev
.parent
= parent
;
1138 ret
= dev_set_name(&udc
->dev
, "%s", kobject_name(&parent
->kobj
));
1142 udc
->gadget
= gadget
;
1145 mutex_lock(&udc_lock
);
1146 list_add_tail(&udc
->list
, &udc_list
);
1148 ret
= device_add(&udc
->dev
);
1152 usb_gadget_set_state(gadget
, USB_STATE_NOTATTACHED
);
1155 /* pick up one of pending gadget drivers */
1156 ret
= check_pending_gadget_drivers(udc
);
1160 mutex_unlock(&udc_lock
);
1165 device_del(&udc
->dev
);
1168 list_del(&udc
->list
);
1169 mutex_unlock(&udc_lock
);
1172 put_device(&udc
->dev
);
1173 device_del(&gadget
->dev
);
1176 put_device(&gadget
->dev
);
1182 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release
);
1185 * usb_get_gadget_udc_name - get the name of the first UDC controller
1186 * This functions returns the name of the first UDC controller in the system.
1187 * Please note that this interface is usefull only for legacy drivers which
1188 * assume that there is only one UDC controller in the system and they need to
1189 * get its name before initialization. There is no guarantee that the UDC
1190 * of the returned name will be still available, when gadget driver registers
1193 * Returns pointer to string with UDC controller name on success, NULL
1194 * otherwise. Caller should kfree() returned string.
1196 char *usb_get_gadget_udc_name(void)
1198 struct usb_udc
*udc
;
1201 /* For now we take the first available UDC */
1202 mutex_lock(&udc_lock
);
1203 list_for_each_entry(udc
, &udc_list
, list
) {
1205 name
= kstrdup(udc
->gadget
->name
, GFP_KERNEL
);
1209 mutex_unlock(&udc_lock
);
1212 EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name
);
1215 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
1216 * @parent: the parent device to this udc. Usually the controller
1218 * @gadget: the gadget to be added to the list
1220 * Returns zero on success, negative errno otherwise.
1222 int usb_add_gadget_udc(struct device
*parent
, struct usb_gadget
*gadget
)
1224 return usb_add_gadget_udc_release(parent
, gadget
, NULL
);
1226 EXPORT_SYMBOL_GPL(usb_add_gadget_udc
);
1228 static void usb_gadget_remove_driver(struct usb_udc
*udc
)
1230 dev_dbg(&udc
->dev
, "unregistering UDC driver [%s]\n",
1231 udc
->driver
->function
);
1233 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
1235 usb_gadget_disconnect(udc
->gadget
);
1236 udc
->driver
->disconnect(udc
->gadget
);
1237 udc
->driver
->unbind(udc
->gadget
);
1238 usb_gadget_udc_stop(udc
);
1241 udc
->dev
.driver
= NULL
;
1242 udc
->gadget
->dev
.driver
= NULL
;
1246 * usb_del_gadget_udc - deletes @udc from udc_list
1247 * @gadget: the gadget to be removed.
1249 * This, will call usb_gadget_unregister_driver() if
1250 * the @udc is still busy.
1252 void usb_del_gadget_udc(struct usb_gadget
*gadget
)
1254 struct usb_udc
*udc
= gadget
->udc
;
1259 dev_vdbg(gadget
->dev
.parent
, "unregistering gadget\n");
1261 mutex_lock(&udc_lock
);
1262 list_del(&udc
->list
);
1265 struct usb_gadget_driver
*driver
= udc
->driver
;
1267 usb_gadget_remove_driver(udc
);
1268 list_add(&driver
->pending
, &gadget_driver_pending_list
);
1270 mutex_unlock(&udc_lock
);
1272 kobject_uevent(&udc
->dev
.kobj
, KOBJ_REMOVE
);
1273 flush_work(&gadget
->work
);
1274 device_unregister(&udc
->dev
);
1275 device_unregister(&gadget
->dev
);
1277 EXPORT_SYMBOL_GPL(usb_del_gadget_udc
);
1279 /* ------------------------------------------------------------------------- */
1281 static int udc_bind_to_driver(struct usb_udc
*udc
, struct usb_gadget_driver
*driver
)
1285 dev_dbg(&udc
->dev
, "registering UDC driver [%s]\n",
1288 udc
->driver
= driver
;
1289 udc
->dev
.driver
= &driver
->driver
;
1290 udc
->gadget
->dev
.driver
= &driver
->driver
;
1292 ret
= driver
->bind(udc
->gadget
, driver
);
1295 ret
= usb_gadget_udc_start(udc
);
1297 driver
->unbind(udc
->gadget
);
1300 usb_udc_connect_control(udc
);
1302 kobject_uevent(&udc
->dev
.kobj
, KOBJ_CHANGE
);
1306 dev_err(&udc
->dev
, "failed to start %s: %d\n",
1307 udc
->driver
->function
, ret
);
1309 udc
->dev
.driver
= NULL
;
1310 udc
->gadget
->dev
.driver
= NULL
;
1314 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
)
1316 struct usb_udc
*udc
= NULL
;
1319 if (!driver
|| !driver
->bind
|| !driver
->setup
)
1322 mutex_lock(&udc_lock
);
1323 if (driver
->udc_name
) {
1324 list_for_each_entry(udc
, &udc_list
, list
) {
1325 ret
= strcmp(driver
->udc_name
, dev_name(&udc
->dev
));
1331 else if (udc
->driver
)
1336 list_for_each_entry(udc
, &udc_list
, list
) {
1337 /* For now we take the first one */
1343 if (!driver
->match_existing_only
) {
1344 list_add_tail(&driver
->pending
, &gadget_driver_pending_list
);
1345 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1350 mutex_unlock(&udc_lock
);
1353 ret
= udc_bind_to_driver(udc
, driver
);
1354 mutex_unlock(&udc_lock
);
1357 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver
);
1359 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1361 struct usb_udc
*udc
= NULL
;
1364 if (!driver
|| !driver
->unbind
)
1367 mutex_lock(&udc_lock
);
1368 list_for_each_entry(udc
, &udc_list
, list
) {
1369 if (udc
->driver
== driver
) {
1370 usb_gadget_remove_driver(udc
);
1371 usb_gadget_set_state(udc
->gadget
,
1372 USB_STATE_NOTATTACHED
);
1374 /* Maybe there is someone waiting for this UDC? */
1375 check_pending_gadget_drivers(udc
);
1377 * For now we ignore bind errors as probably it's
1378 * not a valid reason to fail other's gadget unbind
1386 list_del(&driver
->pending
);
1389 mutex_unlock(&udc_lock
);
1392 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver
);
1394 /* ------------------------------------------------------------------------- */
1396 static ssize_t
usb_udc_srp_store(struct device
*dev
,
1397 struct device_attribute
*attr
, const char *buf
, size_t n
)
1399 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
1401 if (sysfs_streq(buf
, "1"))
1402 usb_gadget_wakeup(udc
->gadget
);
1406 static DEVICE_ATTR(srp
, S_IWUSR
, NULL
, usb_udc_srp_store
);
1408 static ssize_t
usb_udc_softconn_store(struct device
*dev
,
1409 struct device_attribute
*attr
, const char *buf
, size_t n
)
1411 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
1414 dev_err(dev
, "soft-connect without a gadget driver\n");
1418 if (sysfs_streq(buf
, "connect")) {
1419 usb_gadget_udc_start(udc
);
1420 usb_gadget_connect(udc
->gadget
);
1421 } else if (sysfs_streq(buf
, "disconnect")) {
1422 usb_gadget_disconnect(udc
->gadget
);
1423 udc
->driver
->disconnect(udc
->gadget
);
1424 usb_gadget_udc_stop(udc
);
1426 dev_err(dev
, "unsupported command '%s'\n", buf
);
1432 static DEVICE_ATTR(soft_connect
, S_IWUSR
, NULL
, usb_udc_softconn_store
);
1434 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
1437 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
1438 struct usb_gadget
*gadget
= udc
->gadget
;
1440 return sprintf(buf
, "%s\n", usb_state_string(gadget
->state
));
1442 static DEVICE_ATTR_RO(state
);
1444 #define USB_UDC_SPEED_ATTR(name, param) \
1445 ssize_t name##_show(struct device *dev, \
1446 struct device_attribute *attr, char *buf) \
1448 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1449 return snprintf(buf, PAGE_SIZE, "%s\n", \
1450 usb_speed_string(udc->gadget->param)); \
1452 static DEVICE_ATTR_RO(name)
1454 static USB_UDC_SPEED_ATTR(current_speed
, speed
);
1455 static USB_UDC_SPEED_ATTR(maximum_speed
, max_speed
);
1457 #define USB_UDC_ATTR(name) \
1458 ssize_t name##_show(struct device *dev, \
1459 struct device_attribute *attr, char *buf) \
1461 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1462 struct usb_gadget *gadget = udc->gadget; \
1464 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1466 static DEVICE_ATTR_RO(name)
1468 static USB_UDC_ATTR(is_otg
);
1469 static USB_UDC_ATTR(is_a_peripheral
);
1470 static USB_UDC_ATTR(b_hnp_enable
);
1471 static USB_UDC_ATTR(a_hnp_support
);
1472 static USB_UDC_ATTR(a_alt_hnp_support
);
1473 static USB_UDC_ATTR(is_selfpowered
);
1475 static struct attribute
*usb_udc_attrs
[] = {
1477 &dev_attr_soft_connect
.attr
,
1478 &dev_attr_state
.attr
,
1479 &dev_attr_current_speed
.attr
,
1480 &dev_attr_maximum_speed
.attr
,
1482 &dev_attr_is_otg
.attr
,
1483 &dev_attr_is_a_peripheral
.attr
,
1484 &dev_attr_b_hnp_enable
.attr
,
1485 &dev_attr_a_hnp_support
.attr
,
1486 &dev_attr_a_alt_hnp_support
.attr
,
1487 &dev_attr_is_selfpowered
.attr
,
1491 static const struct attribute_group usb_udc_attr_group
= {
1492 .attrs
= usb_udc_attrs
,
1495 static const struct attribute_group
*usb_udc_attr_groups
[] = {
1496 &usb_udc_attr_group
,
1500 static int usb_udc_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1502 struct usb_udc
*udc
= container_of(dev
, struct usb_udc
, dev
);
1505 ret
= add_uevent_var(env
, "USB_UDC_NAME=%s", udc
->gadget
->name
);
1507 dev_err(dev
, "failed to add uevent USB_UDC_NAME\n");
1512 ret
= add_uevent_var(env
, "USB_UDC_DRIVER=%s",
1513 udc
->driver
->function
);
1515 dev_err(dev
, "failed to add uevent USB_UDC_DRIVER\n");
1523 static int __init
usb_udc_init(void)
1525 udc_class
= class_create(THIS_MODULE
, "udc");
1526 if (IS_ERR(udc_class
)) {
1527 pr_err("failed to create udc class --> %ld\n",
1528 PTR_ERR(udc_class
));
1529 return PTR_ERR(udc_class
);
1532 udc_class
->dev_uevent
= usb_udc_uevent
;
1535 subsys_initcall(usb_udc_init
);
1537 static void __exit
usb_udc_exit(void)
1539 class_destroy(udc_class
);
1541 module_exit(usb_udc_exit
);
1543 MODULE_DESCRIPTION("UDC Framework");
1544 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1545 MODULE_LICENSE("GPL v2");