xref: /linux/drivers/usb/gadget/udc/core.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2e21cd08fSLee Jones /*
32ccea03aSFelipe Balbi  * udc.c - Core UDC Framework
42ccea03aSFelipe Balbi  *
52ccea03aSFelipe Balbi  * Copyright (C) 2010 Texas Instruments
62ccea03aSFelipe Balbi  * Author: Felipe Balbi <balbi@ti.com>
72ccea03aSFelipe Balbi  */
82ccea03aSFelipe Balbi 
9dab67a01SAndy Shevchenko #define pr_fmt(fmt)	"UDC core: " fmt
10dab67a01SAndy Shevchenko 
112ccea03aSFelipe Balbi #include <linux/kernel.h>
122ccea03aSFelipe Balbi #include <linux/module.h>
132ccea03aSFelipe Balbi #include <linux/device.h>
142ccea03aSFelipe Balbi #include <linux/list.h>
15f9d76d15SAlan Stern #include <linux/idr.h>
162ccea03aSFelipe Balbi #include <linux/err.h>
17a698908dSFelipe Balbi #include <linux/dma-mapping.h>
18614536daSFlorian Fainelli #include <linux/sched/task_stack.h>
195702f753SFelipe Balbi #include <linux/workqueue.h>
202ccea03aSFelipe Balbi 
212ccea03aSFelipe Balbi #include <linux/usb/ch9.h>
222ccea03aSFelipe Balbi #include <linux/usb/gadget.h>
230cfbd328SMichal Sojka #include <linux/usb.h>
242ccea03aSFelipe Balbi 
255e42d710SFelipe Balbi #include "trace.h"
265e42d710SFelipe Balbi 
27f9d76d15SAlan Stern static DEFINE_IDA(gadget_id_numbers);
28f9d76d15SAlan Stern 
299d11b134SGreg Kroah-Hartman static const struct bus_type gadget_bus_type;
30fc274c1eSAlan Stern 
312ccea03aSFelipe Balbi /**
322ccea03aSFelipe Balbi  * struct usb_udc - describes one usb device controller
33e21cd08fSLee Jones  * @driver: the gadget driver pointer. For use by the class code
34e21cd08fSLee Jones  * @dev: the child device to the actual controller
35e21cd08fSLee Jones  * @gadget: the gadget. For use by the class code
36e21cd08fSLee Jones  * @list: for use by the udc class driver
37e21cd08fSLee Jones  * @vbus: for udcs who care about vbus status, this value is real vbus status;
38628ef0d2SPeter Chen  * for udcs who do not care about vbus status, this value is always true
3949d08cfcSThinh Nguyen  * @started: the UDC's started state. True if the UDC had started.
4050966da8SBadhri Jagan Sridharan  * @allow_connect: Indicates whether UDC is allowed to be pulled up.
4150966da8SBadhri Jagan Sridharan  * Set/cleared by gadget_(un)bind_driver() after gadget driver is bound or
4250966da8SBadhri Jagan Sridharan  * unbound.
43159a98afSAlan Stern  * @vbus_work: work routine to handle VBUS status change notifications.
44286d9975SBadhri Jagan Sridharan  * @connect_lock: protects udc->started, gadget->connect,
45286d9975SBadhri Jagan Sridharan  * gadget->allow_connect and gadget->deactivate. The routines
46286d9975SBadhri Jagan Sridharan  * usb_gadget_connect_locked(), usb_gadget_disconnect_locked(),
47286d9975SBadhri Jagan Sridharan  * usb_udc_connect_control_locked(), usb_gadget_udc_start_locked() and
48286d9975SBadhri Jagan Sridharan  * usb_gadget_udc_stop_locked() are called with this lock held.
492ccea03aSFelipe Balbi  *
502ccea03aSFelipe Balbi  * This represents the internal data structure which is used by the UDC-class
512ccea03aSFelipe Balbi  * to hold information about udc driver and gadget together.
522ccea03aSFelipe Balbi  */
532ccea03aSFelipe Balbi struct usb_udc {
542ccea03aSFelipe Balbi 	struct usb_gadget_driver	*driver;
552ccea03aSFelipe Balbi 	struct usb_gadget		*gadget;
562ccea03aSFelipe Balbi 	struct device			dev;
572ccea03aSFelipe Balbi 	struct list_head		list;
58628ef0d2SPeter Chen 	bool				vbus;
5949d08cfcSThinh Nguyen 	bool				started;
6050966da8SBadhri Jagan Sridharan 	bool				allow_connect;
6150966da8SBadhri Jagan Sridharan 	struct work_struct		vbus_work;
62286d9975SBadhri Jagan Sridharan 	struct mutex			connect_lock;
632ccea03aSFelipe Balbi };
642ccea03aSFelipe Balbi 
658e991436SIvan Orlov static const struct class udc_class;
662ccea03aSFelipe Balbi static LIST_HEAD(udc_list);
672ccea03aSFelipe Balbi 
68fc274c1eSAlan Stern /* Protects udc_list, udc->driver, driver->is_bound, and related calls */
69fc274c1eSAlan Stern static DEFINE_MUTEX(udc_lock);
70855ed04aSRuslan Bilovol 
712ccea03aSFelipe Balbi /* ------------------------------------------------------------------------- */
722ccea03aSFelipe Balbi 
735a8d651aSFelipe Balbi /**
745a8d651aSFelipe Balbi  * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
755a8d651aSFelipe Balbi  * @ep:the endpoint being configured
765a8d651aSFelipe Balbi  * @maxpacket_limit:value of maximum packet size limit
775a8d651aSFelipe Balbi  *
785a8d651aSFelipe Balbi  * This function should be used only in UDC drivers to initialize endpoint
795a8d651aSFelipe Balbi  * (usually in probe function).
805a8d651aSFelipe Balbi  */
usb_ep_set_maxpacket_limit(struct usb_ep * ep,unsigned maxpacket_limit)815a8d651aSFelipe Balbi void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
825a8d651aSFelipe Balbi 					      unsigned maxpacket_limit)
835a8d651aSFelipe Balbi {
845a8d651aSFelipe Balbi 	ep->maxpacket_limit = maxpacket_limit;
855a8d651aSFelipe Balbi 	ep->maxpacket = maxpacket_limit;
865e42d710SFelipe Balbi 
875e42d710SFelipe Balbi 	trace_usb_ep_set_maxpacket_limit(ep, 0);
885a8d651aSFelipe Balbi }
895a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
905a8d651aSFelipe Balbi 
915a8d651aSFelipe Balbi /**
925a8d651aSFelipe Balbi  * usb_ep_enable - configure endpoint, making it usable
935a8d651aSFelipe Balbi  * @ep:the endpoint being configured.  may not be the endpoint named "ep0".
945a8d651aSFelipe Balbi  *	drivers discover endpoints through the ep_list of a usb_gadget.
955a8d651aSFelipe Balbi  *
965a8d651aSFelipe Balbi  * When configurations are set, or when interface settings change, the driver
975a8d651aSFelipe Balbi  * will enable or disable the relevant endpoints.  while it is enabled, an
985a8d651aSFelipe Balbi  * endpoint may be used for i/o until the driver receives a disconnect() from
995a8d651aSFelipe Balbi  * the host or until the endpoint is disabled.
1005a8d651aSFelipe Balbi  *
1015a8d651aSFelipe Balbi  * the ep0 implementation (which calls this routine) must ensure that the
1025a8d651aSFelipe Balbi  * hardware capabilities of each endpoint match the descriptor provided
1035a8d651aSFelipe Balbi  * for it.  for example, an endpoint named "ep2in-bulk" would be usable
1045a8d651aSFelipe Balbi  * for interrupt transfers as well as bulk, but it likely couldn't be used
1055a8d651aSFelipe Balbi  * for iso transfers or for endpoint 14.  some endpoints are fully
1065a8d651aSFelipe Balbi  * configurable, with more generic names like "ep-a".  (remember that for
107b9b70170SGreg Kroah-Hartman  * USB, "in" means "towards the USB host".)
1085a8d651aSFelipe Balbi  *
109b0d5d2a7SWesley Cheng  * This routine may be called in an atomic (interrupt) context.
110bf594c10SAlan Stern  *
1115a8d651aSFelipe Balbi  * returns zero, or a negative error code.
1125a8d651aSFelipe Balbi  */
usb_ep_enable(struct usb_ep * ep)1135a8d651aSFelipe Balbi int usb_ep_enable(struct usb_ep *ep)
1145a8d651aSFelipe Balbi {
1155e42d710SFelipe Balbi 	int ret = 0;
1165a8d651aSFelipe Balbi 
1175a8d651aSFelipe Balbi 	if (ep->enabled)
1185e42d710SFelipe Balbi 		goto out;
1195a8d651aSFelipe Balbi 
12054f83b8cSAlan Stern 	/* UDC drivers can't handle endpoints with maxpacket size 0 */
121973a5789SChris Wulff 	if (!ep->desc || usb_endpoint_maxp(ep->desc) == 0) {
122973a5789SChris Wulff 		WARN_ONCE(1, "%s: ep%d (%s) has %s\n", __func__, ep->address, ep->name,
123973a5789SChris Wulff 			  (!ep->desc) ? "NULL descriptor" : "maxpacket 0");
124973a5789SChris Wulff 
12554f83b8cSAlan Stern 		ret = -EINVAL;
12654f83b8cSAlan Stern 		goto out;
12754f83b8cSAlan Stern 	}
12854f83b8cSAlan Stern 
1295a8d651aSFelipe Balbi 	ret = ep->ops->enable(ep, ep->desc);
130f510b5a1SColin Ian King 	if (ret)
1315e42d710SFelipe Balbi 		goto out;
1325a8d651aSFelipe Balbi 
1335a8d651aSFelipe Balbi 	ep->enabled = true;
1345a8d651aSFelipe Balbi 
1355e42d710SFelipe Balbi out:
1365e42d710SFelipe Balbi 	trace_usb_ep_enable(ep, ret);
1375e42d710SFelipe Balbi 
1385e42d710SFelipe Balbi 	return ret;
1395a8d651aSFelipe Balbi }
1405a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_enable);
1415a8d651aSFelipe Balbi 
1425a8d651aSFelipe Balbi /**
1435a8d651aSFelipe Balbi  * usb_ep_disable - endpoint is no longer usable
1445a8d651aSFelipe Balbi  * @ep:the endpoint being unconfigured.  may not be the endpoint named "ep0".
1455a8d651aSFelipe Balbi  *
1465a8d651aSFelipe Balbi  * no other task may be using this endpoint when this is called.
1475a8d651aSFelipe Balbi  * any pending and uncompleted requests will complete with status
1485a8d651aSFelipe Balbi  * indicating disconnect (-ESHUTDOWN) before this call returns.
1495a8d651aSFelipe Balbi  * gadget drivers must call usb_ep_enable() again before queueing
1505a8d651aSFelipe Balbi  * requests to the endpoint.
1515a8d651aSFelipe Balbi  *
152b0d5d2a7SWesley Cheng  * This routine may be called in an atomic (interrupt) context.
153bf594c10SAlan Stern  *
1545a8d651aSFelipe Balbi  * returns zero, or a negative error code.
1555a8d651aSFelipe Balbi  */
usb_ep_disable(struct usb_ep * ep)1565a8d651aSFelipe Balbi int usb_ep_disable(struct usb_ep *ep)
1575a8d651aSFelipe Balbi {
1585e42d710SFelipe Balbi 	int ret = 0;
1595a8d651aSFelipe Balbi 
1605a8d651aSFelipe Balbi 	if (!ep->enabled)
1615e42d710SFelipe Balbi 		goto out;
1625a8d651aSFelipe Balbi 
1635a8d651aSFelipe Balbi 	ret = ep->ops->disable(ep);
1648a8b161dSStefan Agner 	if (ret)
1655e42d710SFelipe Balbi 		goto out;
1665a8d651aSFelipe Balbi 
1675a8d651aSFelipe Balbi 	ep->enabled = false;
1685a8d651aSFelipe Balbi 
1695e42d710SFelipe Balbi out:
1705e42d710SFelipe Balbi 	trace_usb_ep_disable(ep, ret);
1715e42d710SFelipe Balbi 
1725e42d710SFelipe Balbi 	return ret;
1735a8d651aSFelipe Balbi }
1745a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_disable);
1755a8d651aSFelipe Balbi 
1765a8d651aSFelipe Balbi /**
1775a8d651aSFelipe Balbi  * usb_ep_alloc_request - allocate a request object to use with this endpoint
1785a8d651aSFelipe Balbi  * @ep:the endpoint to be used with with the request
1795a8d651aSFelipe Balbi  * @gfp_flags:GFP_* flags to use
1805a8d651aSFelipe Balbi  *
1815a8d651aSFelipe Balbi  * Request objects must be allocated with this call, since they normally
1825a8d651aSFelipe Balbi  * need controller-specific setup and may even need endpoint-specific
1835a8d651aSFelipe Balbi  * resources such as allocation of DMA descriptors.
1845a8d651aSFelipe Balbi  * Requests may be submitted with usb_ep_queue(), and receive a single
1855a8d651aSFelipe Balbi  * completion callback.  Free requests with usb_ep_free_request(), when
1865a8d651aSFelipe Balbi  * they are no longer needed.
1875a8d651aSFelipe Balbi  *
1885a8d651aSFelipe Balbi  * Returns the request, or null if one could not be allocated.
1895a8d651aSFelipe Balbi  */
usb_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1905a8d651aSFelipe Balbi struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
1915a8d651aSFelipe Balbi 						       gfp_t gfp_flags)
1925a8d651aSFelipe Balbi {
1935e42d710SFelipe Balbi 	struct usb_request *req = NULL;
1945e42d710SFelipe Balbi 
1955e42d710SFelipe Balbi 	req = ep->ops->alloc_request(ep, gfp_flags);
1965e42d710SFelipe Balbi 
1975e42d710SFelipe Balbi 	trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
1985e42d710SFelipe Balbi 
1995e42d710SFelipe Balbi 	return req;
2005a8d651aSFelipe Balbi }
2015a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
2025a8d651aSFelipe Balbi 
2035a8d651aSFelipe Balbi /**
2045a8d651aSFelipe Balbi  * usb_ep_free_request - frees a request object
2055a8d651aSFelipe Balbi  * @ep:the endpoint associated with the request
2065a8d651aSFelipe Balbi  * @req:the request being freed
2075a8d651aSFelipe Balbi  *
2085a8d651aSFelipe Balbi  * Reverses the effect of usb_ep_alloc_request().
2095a8d651aSFelipe Balbi  * Caller guarantees the request is not queued, and that it will
2105a8d651aSFelipe Balbi  * no longer be requeued (or otherwise used).
2115a8d651aSFelipe Balbi  */
usb_ep_free_request(struct usb_ep * ep,struct usb_request * req)2125a8d651aSFelipe Balbi void usb_ep_free_request(struct usb_ep *ep,
2135a8d651aSFelipe Balbi 				       struct usb_request *req)
2145a8d651aSFelipe Balbi {
2155e42d710SFelipe Balbi 	trace_usb_ep_free_request(ep, req, 0);
216e74bd4d3SManu Gautam 	ep->ops->free_request(ep, req);
2175a8d651aSFelipe Balbi }
2185a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_free_request);
2195a8d651aSFelipe Balbi 
2205a8d651aSFelipe Balbi /**
2215a8d651aSFelipe Balbi  * usb_ep_queue - queues (submits) an I/O request to an endpoint.
2225a8d651aSFelipe Balbi  * @ep:the endpoint associated with the request
2235a8d651aSFelipe Balbi  * @req:the request being submitted
2245a8d651aSFelipe Balbi  * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
2255a8d651aSFelipe Balbi  *	pre-allocate all necessary memory with the request.
2265a8d651aSFelipe Balbi  *
2275a8d651aSFelipe Balbi  * This tells the device controller to perform the specified request through
2285a8d651aSFelipe Balbi  * that endpoint (reading or writing a buffer).  When the request completes,
2295a8d651aSFelipe Balbi  * including being canceled by usb_ep_dequeue(), the request's completion
2305a8d651aSFelipe Balbi  * routine is called to return the request to the driver.  Any endpoint
2315a8d651aSFelipe Balbi  * (except control endpoints like ep0) may have more than one transfer
2325a8d651aSFelipe Balbi  * request queued; they complete in FIFO order.  Once a gadget driver
2335a8d651aSFelipe Balbi  * submits a request, that request may not be examined or modified until it
2345a8d651aSFelipe Balbi  * is given back to that driver through the completion callback.
2355a8d651aSFelipe Balbi  *
2365a8d651aSFelipe Balbi  * Each request is turned into one or more packets.  The controller driver
2375a8d651aSFelipe Balbi  * never merges adjacent requests into the same packet.  OUT transfers
2385a8d651aSFelipe Balbi  * will sometimes use data that's already buffered in the hardware.
2395a8d651aSFelipe Balbi  * Drivers can rely on the fact that the first byte of the request's buffer
2405a8d651aSFelipe Balbi  * always corresponds to the first byte of some USB packet, for both
2415a8d651aSFelipe Balbi  * IN and OUT transfers.
2425a8d651aSFelipe Balbi  *
2435a8d651aSFelipe Balbi  * Bulk endpoints can queue any amount of data; the transfer is packetized
2445a8d651aSFelipe Balbi  * automatically.  The last packet will be short if the request doesn't fill it
2455a8d651aSFelipe Balbi  * out completely.  Zero length packets (ZLPs) should be avoided in portable
2465a8d651aSFelipe Balbi  * protocols since not all usb hardware can successfully handle zero length
2475a8d651aSFelipe Balbi  * packets.  (ZLPs may be explicitly written, and may be implicitly written if
2485a8d651aSFelipe Balbi  * the request 'zero' flag is set.)  Bulk endpoints may also be used
2495a8d651aSFelipe Balbi  * for interrupt transfers; but the reverse is not true, and some endpoints
2505a8d651aSFelipe Balbi  * won't support every interrupt transfer.  (Such as 768 byte packets.)
2515a8d651aSFelipe Balbi  *
2525a8d651aSFelipe Balbi  * Interrupt-only endpoints are less functional than bulk endpoints, for
2535a8d651aSFelipe Balbi  * example by not supporting queueing or not handling buffers that are
2545a8d651aSFelipe Balbi  * larger than the endpoint's maxpacket size.  They may also treat data
2555a8d651aSFelipe Balbi  * toggle differently.
2565a8d651aSFelipe Balbi  *
2575a8d651aSFelipe Balbi  * Control endpoints ... after getting a setup() callback, the driver queues
2585a8d651aSFelipe Balbi  * one response (even if it would be zero length).  That enables the
2595a8d651aSFelipe Balbi  * status ack, after transferring data as specified in the response.  Setup
2605a8d651aSFelipe Balbi  * functions may return negative error codes to generate protocol stalls.
2615a8d651aSFelipe Balbi  * (Note that some USB device controllers disallow protocol stall responses
2625a8d651aSFelipe Balbi  * in some cases.)  When control responses are deferred (the response is
2635a8d651aSFelipe Balbi  * written after the setup callback returns), then usb_ep_set_halt() may be
2645a8d651aSFelipe Balbi  * used on ep0 to trigger protocol stalls.  Depending on the controller,
2655a8d651aSFelipe Balbi  * it may not be possible to trigger a status-stage protocol stall when the
2665a8d651aSFelipe Balbi  * data stage is over, that is, from within the response's completion
2675a8d651aSFelipe Balbi  * routine.
2685a8d651aSFelipe Balbi  *
2695a8d651aSFelipe Balbi  * For periodic endpoints, like interrupt or isochronous ones, the usb host
2705a8d651aSFelipe Balbi  * arranges to poll once per interval, and the gadget driver usually will
2715a8d651aSFelipe Balbi  * have queued some data to transfer at that time.
2725a8d651aSFelipe Balbi  *
273eaa358c7SFelipe Balbi  * Note that @req's ->complete() callback must never be called from
274eaa358c7SFelipe Balbi  * within usb_ep_queue() as that can create deadlock situations.
275eaa358c7SFelipe Balbi  *
276bf594c10SAlan Stern  * This routine may be called in interrupt context.
277bf594c10SAlan Stern  *
2785a8d651aSFelipe Balbi  * Returns zero, or a negative error code.  Endpoints that are not enabled
2795a8d651aSFelipe Balbi  * report errors; errors will also be
2805a8d651aSFelipe Balbi  * reported when the usb peripheral is disconnected.
2815d1332a8SAlan Stern  *
2825d1332a8SAlan Stern  * If and only if @req is successfully queued (the return value is zero),
2835d1332a8SAlan Stern  * @req->complete() will be called exactly once, when the Gadget core and
2845d1332a8SAlan Stern  * UDC are finished with the request.  When the completion function is called,
2855d1332a8SAlan Stern  * control of the request is returned to the device driver which submitted it.
2865d1332a8SAlan Stern  * The completion handler may then immediately free or reuse @req.
2875a8d651aSFelipe Balbi  */
usb_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)2885a8d651aSFelipe Balbi int usb_ep_queue(struct usb_ep *ep,
2895a8d651aSFelipe Balbi 			       struct usb_request *req, gfp_t gfp_flags)
2905a8d651aSFelipe Balbi {
2915e42d710SFelipe Balbi 	int ret = 0;
2925a8d651aSFelipe Balbi 
2932a587a03Syuan linyu 	if (!ep->enabled && ep->address) {
2942a587a03Syuan linyu 		pr_debug("USB gadget: queue request to disabled ep 0x%x (%s)\n",
2952a587a03Syuan linyu 				 ep->address, ep->name);
2965e42d710SFelipe Balbi 		ret = -ESHUTDOWN;
2975e42d710SFelipe Balbi 		goto out;
2985e42d710SFelipe Balbi 	}
2995e42d710SFelipe Balbi 
3005e42d710SFelipe Balbi 	ret = ep->ops->queue(ep, req, gfp_flags);
3015e42d710SFelipe Balbi 
3025e42d710SFelipe Balbi out:
3035e42d710SFelipe Balbi 	trace_usb_ep_queue(ep, req, ret);
3045e42d710SFelipe Balbi 
3055e42d710SFelipe Balbi 	return ret;
3065a8d651aSFelipe Balbi }
3075a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_queue);
3085a8d651aSFelipe Balbi 
3095a8d651aSFelipe Balbi /**
3105a8d651aSFelipe Balbi  * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
3115a8d651aSFelipe Balbi  * @ep:the endpoint associated with the request
3125a8d651aSFelipe Balbi  * @req:the request being canceled
3135a8d651aSFelipe Balbi  *
3141e19a520SAlan Stern  * If the request is still active on the endpoint, it is dequeued and
3151e19a520SAlan Stern  * eventually its completion routine is called (with status -ECONNRESET);
3161e19a520SAlan Stern  * else a negative error code is returned.  This routine is asynchronous,
3171e19a520SAlan Stern  * that is, it may return before the completion routine runs.
3185a8d651aSFelipe Balbi  *
3195a8d651aSFelipe Balbi  * Note that some hardware can't clear out write fifos (to unlink the request
3205a8d651aSFelipe Balbi  * at the head of the queue) except as part of disconnecting from usb. Such
3215a8d651aSFelipe Balbi  * restrictions prevent drivers from supporting configuration changes,
3225a8d651aSFelipe Balbi  * even to configuration zero (a "chapter 9" requirement).
323bf594c10SAlan Stern  *
324bf594c10SAlan Stern  * This routine may be called in interrupt context.
3255a8d651aSFelipe Balbi  */
usb_ep_dequeue(struct usb_ep * ep,struct usb_request * req)3265a8d651aSFelipe Balbi int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
3275a8d651aSFelipe Balbi {
3285e42d710SFelipe Balbi 	int ret;
3295e42d710SFelipe Balbi 
3305e42d710SFelipe Balbi 	ret = ep->ops->dequeue(ep, req);
3315e42d710SFelipe Balbi 	trace_usb_ep_dequeue(ep, req, ret);
3325e42d710SFelipe Balbi 
3335e42d710SFelipe Balbi 	return ret;
3345a8d651aSFelipe Balbi }
3355a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_dequeue);
3365a8d651aSFelipe Balbi 
3375a8d651aSFelipe Balbi /**
3385a8d651aSFelipe Balbi  * usb_ep_set_halt - sets the endpoint halt feature.
3395a8d651aSFelipe Balbi  * @ep: the non-isochronous endpoint being stalled
3405a8d651aSFelipe Balbi  *
3415a8d651aSFelipe Balbi  * Use this to stall an endpoint, perhaps as an error report.
3425a8d651aSFelipe Balbi  * Except for control endpoints,
3435a8d651aSFelipe Balbi  * the endpoint stays halted (will not stream any data) until the host
3445a8d651aSFelipe Balbi  * clears this feature; drivers may need to empty the endpoint's request
3455a8d651aSFelipe Balbi  * queue first, to make sure no inappropriate transfers happen.
3465a8d651aSFelipe Balbi  *
3475a8d651aSFelipe Balbi  * Note that while an endpoint CLEAR_FEATURE will be invisible to the
3485a8d651aSFelipe Balbi  * gadget driver, a SET_INTERFACE will not be.  To reset endpoints for the
3495a8d651aSFelipe Balbi  * current altsetting, see usb_ep_clear_halt().  When switching altsettings,
3505a8d651aSFelipe Balbi  * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
3515a8d651aSFelipe Balbi  *
352bf594c10SAlan Stern  * This routine may be called in interrupt context.
353bf594c10SAlan Stern  *
3545a8d651aSFelipe Balbi  * Returns zero, or a negative error code.  On success, this call sets
3555a8d651aSFelipe Balbi  * underlying hardware state that blocks data transfers.
3565a8d651aSFelipe Balbi  * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
3575a8d651aSFelipe Balbi  * transfer requests are still queued, or if the controller hardware
3585a8d651aSFelipe Balbi  * (usually a FIFO) still holds bytes that the host hasn't collected.
3595a8d651aSFelipe Balbi  */
usb_ep_set_halt(struct usb_ep * ep)3605a8d651aSFelipe Balbi int usb_ep_set_halt(struct usb_ep *ep)
3615a8d651aSFelipe Balbi {
3625e42d710SFelipe Balbi 	int ret;
3635e42d710SFelipe Balbi 
3645e42d710SFelipe Balbi 	ret = ep->ops->set_halt(ep, 1);
3655e42d710SFelipe Balbi 	trace_usb_ep_set_halt(ep, ret);
3665e42d710SFelipe Balbi 
3675e42d710SFelipe Balbi 	return ret;
3685a8d651aSFelipe Balbi }
3695a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_set_halt);
3705a8d651aSFelipe Balbi 
3715a8d651aSFelipe Balbi /**
3725a8d651aSFelipe Balbi  * usb_ep_clear_halt - clears endpoint halt, and resets toggle
3735a8d651aSFelipe Balbi  * @ep:the bulk or interrupt endpoint being reset
3745a8d651aSFelipe Balbi  *
3755a8d651aSFelipe Balbi  * Use this when responding to the standard usb "set interface" request,
3765a8d651aSFelipe Balbi  * for endpoints that aren't reconfigured, after clearing any other state
3775a8d651aSFelipe Balbi  * in the endpoint's i/o queue.
3785a8d651aSFelipe Balbi  *
379bf594c10SAlan Stern  * This routine may be called in interrupt context.
380bf594c10SAlan Stern  *
3815a8d651aSFelipe Balbi  * Returns zero, or a negative error code.  On success, this call clears
3825a8d651aSFelipe Balbi  * the underlying hardware state reflecting endpoint halt and data toggle.
3835a8d651aSFelipe Balbi  * Note that some hardware can't support this request (like pxa2xx_udc),
3845a8d651aSFelipe Balbi  * and accordingly can't correctly implement interface altsettings.
3855a8d651aSFelipe Balbi  */
usb_ep_clear_halt(struct usb_ep * ep)3865a8d651aSFelipe Balbi int usb_ep_clear_halt(struct usb_ep *ep)
3875a8d651aSFelipe Balbi {
3885e42d710SFelipe Balbi 	int ret;
3895e42d710SFelipe Balbi 
3905e42d710SFelipe Balbi 	ret = ep->ops->set_halt(ep, 0);
3915e42d710SFelipe Balbi 	trace_usb_ep_clear_halt(ep, ret);
3925e42d710SFelipe Balbi 
3935e42d710SFelipe Balbi 	return ret;
3945a8d651aSFelipe Balbi }
3955a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
3965a8d651aSFelipe Balbi 
3975a8d651aSFelipe Balbi /**
3985a8d651aSFelipe Balbi  * usb_ep_set_wedge - sets the halt feature and ignores clear requests
3995a8d651aSFelipe Balbi  * @ep: the endpoint being wedged
4005a8d651aSFelipe Balbi  *
4015a8d651aSFelipe Balbi  * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
4025a8d651aSFelipe Balbi  * requests. If the gadget driver clears the halt status, it will
4035a8d651aSFelipe Balbi  * automatically unwedge the endpoint.
4045a8d651aSFelipe Balbi  *
405bf594c10SAlan Stern  * This routine may be called in interrupt context.
406bf594c10SAlan Stern  *
4075a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
4085a8d651aSFelipe Balbi  */
usb_ep_set_wedge(struct usb_ep * ep)4095a8d651aSFelipe Balbi int usb_ep_set_wedge(struct usb_ep *ep)
4105a8d651aSFelipe Balbi {
4115e42d710SFelipe Balbi 	int ret;
4125e42d710SFelipe Balbi 
4135a8d651aSFelipe Balbi 	if (ep->ops->set_wedge)
4145e42d710SFelipe Balbi 		ret = ep->ops->set_wedge(ep);
4155a8d651aSFelipe Balbi 	else
4165e42d710SFelipe Balbi 		ret = ep->ops->set_halt(ep, 1);
4175e42d710SFelipe Balbi 
4185e42d710SFelipe Balbi 	trace_usb_ep_set_wedge(ep, ret);
4195e42d710SFelipe Balbi 
4205e42d710SFelipe Balbi 	return ret;
4215a8d651aSFelipe Balbi }
4225a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
4235a8d651aSFelipe Balbi 
4245a8d651aSFelipe Balbi /**
4255a8d651aSFelipe Balbi  * usb_ep_fifo_status - returns number of bytes in fifo, or error
4265a8d651aSFelipe Balbi  * @ep: the endpoint whose fifo status is being checked.
4275a8d651aSFelipe Balbi  *
4285a8d651aSFelipe Balbi  * FIFO endpoints may have "unclaimed data" in them in certain cases,
4295a8d651aSFelipe Balbi  * such as after aborted transfers.  Hosts may not have collected all
4305a8d651aSFelipe Balbi  * the IN data written by the gadget driver (and reported by a request
4315a8d651aSFelipe Balbi  * completion).  The gadget driver may not have collected all the data
4325a8d651aSFelipe Balbi  * written OUT to it by the host.  Drivers that need precise handling for
4335a8d651aSFelipe Balbi  * fault reporting or recovery may need to use this call.
4345a8d651aSFelipe Balbi  *
435bf594c10SAlan Stern  * This routine may be called in interrupt context.
436bf594c10SAlan Stern  *
4375a8d651aSFelipe Balbi  * This returns the number of such bytes in the fifo, or a negative
4385a8d651aSFelipe Balbi  * errno if the endpoint doesn't use a FIFO or doesn't support such
4395a8d651aSFelipe Balbi  * precise handling.
4405a8d651aSFelipe Balbi  */
usb_ep_fifo_status(struct usb_ep * ep)4415a8d651aSFelipe Balbi int usb_ep_fifo_status(struct usb_ep *ep)
4425a8d651aSFelipe Balbi {
4435e42d710SFelipe Balbi 	int ret;
4445e42d710SFelipe Balbi 
4455a8d651aSFelipe Balbi 	if (ep->ops->fifo_status)
4465e42d710SFelipe Balbi 		ret = ep->ops->fifo_status(ep);
4475a8d651aSFelipe Balbi 	else
4485e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
4495e42d710SFelipe Balbi 
4505e42d710SFelipe Balbi 	trace_usb_ep_fifo_status(ep, ret);
4515e42d710SFelipe Balbi 
4525e42d710SFelipe Balbi 	return ret;
4535a8d651aSFelipe Balbi }
4545a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
4555a8d651aSFelipe Balbi 
4565a8d651aSFelipe Balbi /**
4575a8d651aSFelipe Balbi  * usb_ep_fifo_flush - flushes contents of a fifo
4585a8d651aSFelipe Balbi  * @ep: the endpoint whose fifo is being flushed.
4595a8d651aSFelipe Balbi  *
4605a8d651aSFelipe Balbi  * This call may be used to flush the "unclaimed data" that may exist in
4615a8d651aSFelipe Balbi  * an endpoint fifo after abnormal transaction terminations.  The call
4625a8d651aSFelipe Balbi  * must never be used except when endpoint is not being used for any
4635a8d651aSFelipe Balbi  * protocol translation.
464bf594c10SAlan Stern  *
465bf594c10SAlan Stern  * This routine may be called in interrupt context.
4665a8d651aSFelipe Balbi  */
usb_ep_fifo_flush(struct usb_ep * ep)4675a8d651aSFelipe Balbi void usb_ep_fifo_flush(struct usb_ep *ep)
4685a8d651aSFelipe Balbi {
4695a8d651aSFelipe Balbi 	if (ep->ops->fifo_flush)
4705a8d651aSFelipe Balbi 		ep->ops->fifo_flush(ep);
4715e42d710SFelipe Balbi 
4725e42d710SFelipe Balbi 	trace_usb_ep_fifo_flush(ep, 0);
4735a8d651aSFelipe Balbi }
4745a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
4755a8d651aSFelipe Balbi 
4765a8d651aSFelipe Balbi /* ------------------------------------------------------------------------- */
4775a8d651aSFelipe Balbi 
4785a8d651aSFelipe Balbi /**
4795a8d651aSFelipe Balbi  * usb_gadget_frame_number - returns the current frame number
4805a8d651aSFelipe Balbi  * @gadget: controller that reports the frame number
4815a8d651aSFelipe Balbi  *
4825a8d651aSFelipe Balbi  * Returns the usb frame number, normally eleven bits from a SOF packet,
4835a8d651aSFelipe Balbi  * or negative errno if this device doesn't support this capability.
4845a8d651aSFelipe Balbi  */
usb_gadget_frame_number(struct usb_gadget * gadget)4855a8d651aSFelipe Balbi int usb_gadget_frame_number(struct usb_gadget *gadget)
4865a8d651aSFelipe Balbi {
4875e42d710SFelipe Balbi 	int ret;
4885e42d710SFelipe Balbi 
4895e42d710SFelipe Balbi 	ret = gadget->ops->get_frame(gadget);
4905e42d710SFelipe Balbi 
4915e42d710SFelipe Balbi 	trace_usb_gadget_frame_number(gadget, ret);
4925e42d710SFelipe Balbi 
4935e42d710SFelipe Balbi 	return ret;
4945a8d651aSFelipe Balbi }
4955a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
4965a8d651aSFelipe Balbi 
4975a8d651aSFelipe Balbi /**
4985a8d651aSFelipe Balbi  * usb_gadget_wakeup - tries to wake up the host connected to this gadget
4995a8d651aSFelipe Balbi  * @gadget: controller used to wake up the host
5005a8d651aSFelipe Balbi  *
5015a8d651aSFelipe Balbi  * Returns zero on success, else negative error code if the hardware
5025a8d651aSFelipe Balbi  * doesn't support such attempts, or its support has not been enabled
5035a8d651aSFelipe Balbi  * by the usb host.  Drivers must return device descriptors that report
5045a8d651aSFelipe Balbi  * their ability to support this, or hosts won't enable it.
5055a8d651aSFelipe Balbi  *
5065a8d651aSFelipe Balbi  * This may also try to use SRP to wake the host and start enumeration,
5075a8d651aSFelipe Balbi  * even if OTG isn't otherwise in use.  OTG devices may also start
5085a8d651aSFelipe Balbi  * remote wakeup even when hosts don't explicitly enable it.
5095a8d651aSFelipe Balbi  */
usb_gadget_wakeup(struct usb_gadget * gadget)5105a8d651aSFelipe Balbi int usb_gadget_wakeup(struct usb_gadget *gadget)
5115a8d651aSFelipe Balbi {
5125e42d710SFelipe Balbi 	int ret = 0;
5135e42d710SFelipe Balbi 
5145e42d710SFelipe Balbi 	if (!gadget->ops->wakeup) {
5155e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
5165e42d710SFelipe Balbi 		goto out;
5175e42d710SFelipe Balbi 	}
5185e42d710SFelipe Balbi 
5195e42d710SFelipe Balbi 	ret = gadget->ops->wakeup(gadget);
5205e42d710SFelipe Balbi 
5215e42d710SFelipe Balbi out:
5225e42d710SFelipe Balbi 	trace_usb_gadget_wakeup(gadget, ret);
5235e42d710SFelipe Balbi 
5245e42d710SFelipe Balbi 	return ret;
5255a8d651aSFelipe Balbi }
5265a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
5275a8d651aSFelipe Balbi 
5285a8d651aSFelipe Balbi /**
529b93c2a68SElson Roy Serrao  * usb_gadget_set_remote_wakeup - configures the device remote wakeup feature.
530b93c2a68SElson Roy Serrao  * @gadget:the device being configured for remote wakeup
531b93c2a68SElson Roy Serrao  * @set:value to be configured.
532b93c2a68SElson Roy Serrao  *
533b93c2a68SElson Roy Serrao  * set to one to enable remote wakeup feature and zero to disable it.
534b93c2a68SElson Roy Serrao  *
535b93c2a68SElson Roy Serrao  * returns zero on success, else negative errno.
536b93c2a68SElson Roy Serrao  */
usb_gadget_set_remote_wakeup(struct usb_gadget * gadget,int set)537b93c2a68SElson Roy Serrao int usb_gadget_set_remote_wakeup(struct usb_gadget *gadget, int set)
538b93c2a68SElson Roy Serrao {
539b93c2a68SElson Roy Serrao 	int ret = 0;
540b93c2a68SElson Roy Serrao 
541b93c2a68SElson Roy Serrao 	if (!gadget->ops->set_remote_wakeup) {
542b93c2a68SElson Roy Serrao 		ret = -EOPNOTSUPP;
543b93c2a68SElson Roy Serrao 		goto out;
544b93c2a68SElson Roy Serrao 	}
545b93c2a68SElson Roy Serrao 
546b93c2a68SElson Roy Serrao 	ret = gadget->ops->set_remote_wakeup(gadget, set);
547b93c2a68SElson Roy Serrao 
548b93c2a68SElson Roy Serrao out:
549b93c2a68SElson Roy Serrao 	trace_usb_gadget_set_remote_wakeup(gadget, ret);
550b93c2a68SElson Roy Serrao 
551b93c2a68SElson Roy Serrao 	return ret;
552b93c2a68SElson Roy Serrao }
553b93c2a68SElson Roy Serrao EXPORT_SYMBOL_GPL(usb_gadget_set_remote_wakeup);
554b93c2a68SElson Roy Serrao 
555b93c2a68SElson Roy Serrao /**
5565a8d651aSFelipe Balbi  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
5575a8d651aSFelipe Balbi  * @gadget:the device being declared as self-powered
5585a8d651aSFelipe Balbi  *
5595a8d651aSFelipe Balbi  * this affects the device status reported by the hardware driver
5605a8d651aSFelipe Balbi  * to reflect that it now has a local power supply.
5615a8d651aSFelipe Balbi  *
5625a8d651aSFelipe Balbi  * returns zero on success, else negative errno.
5635a8d651aSFelipe Balbi  */
usb_gadget_set_selfpowered(struct usb_gadget * gadget)5645a8d651aSFelipe Balbi int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
5655a8d651aSFelipe Balbi {
5665e42d710SFelipe Balbi 	int ret = 0;
5675e42d710SFelipe Balbi 
5685e42d710SFelipe Balbi 	if (!gadget->ops->set_selfpowered) {
5695e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
5705e42d710SFelipe Balbi 		goto out;
5715e42d710SFelipe Balbi 	}
5725e42d710SFelipe Balbi 
5735e42d710SFelipe Balbi 	ret = gadget->ops->set_selfpowered(gadget, 1);
5745e42d710SFelipe Balbi 
5755e42d710SFelipe Balbi out:
5765e42d710SFelipe Balbi 	trace_usb_gadget_set_selfpowered(gadget, ret);
5775e42d710SFelipe Balbi 
5785e42d710SFelipe Balbi 	return ret;
5795a8d651aSFelipe Balbi }
5805a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
5815a8d651aSFelipe Balbi 
5825a8d651aSFelipe Balbi /**
5835a8d651aSFelipe Balbi  * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
5845a8d651aSFelipe Balbi  * @gadget:the device being declared as bus-powered
5855a8d651aSFelipe Balbi  *
5865a8d651aSFelipe Balbi  * this affects the device status reported by the hardware driver.
5875a8d651aSFelipe Balbi  * some hardware may not support bus-powered operation, in which
5885a8d651aSFelipe Balbi  * case this feature's value can never change.
5895a8d651aSFelipe Balbi  *
5905a8d651aSFelipe Balbi  * returns zero on success, else negative errno.
5915a8d651aSFelipe Balbi  */
usb_gadget_clear_selfpowered(struct usb_gadget * gadget)5925a8d651aSFelipe Balbi int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
5935a8d651aSFelipe Balbi {
5945e42d710SFelipe Balbi 	int ret = 0;
5955e42d710SFelipe Balbi 
5965e42d710SFelipe Balbi 	if (!gadget->ops->set_selfpowered) {
5975e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
5985e42d710SFelipe Balbi 		goto out;
5995e42d710SFelipe Balbi 	}
6005e42d710SFelipe Balbi 
6015e42d710SFelipe Balbi 	ret = gadget->ops->set_selfpowered(gadget, 0);
6025e42d710SFelipe Balbi 
6035e42d710SFelipe Balbi out:
6045e42d710SFelipe Balbi 	trace_usb_gadget_clear_selfpowered(gadget, ret);
6055e42d710SFelipe Balbi 
6065e42d710SFelipe Balbi 	return ret;
6075a8d651aSFelipe Balbi }
6085a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
6095a8d651aSFelipe Balbi 
6105a8d651aSFelipe Balbi /**
6115a8d651aSFelipe Balbi  * usb_gadget_vbus_connect - Notify controller that VBUS is powered
6125a8d651aSFelipe Balbi  * @gadget:The device which now has VBUS power.
6135a8d651aSFelipe Balbi  * Context: can sleep
6145a8d651aSFelipe Balbi  *
6155a8d651aSFelipe Balbi  * This call is used by a driver for an external transceiver (or GPIO)
6165a8d651aSFelipe Balbi  * that detects a VBUS power session starting.  Common responses include
6175a8d651aSFelipe Balbi  * resuming the controller, activating the D+ (or D-) pullup to let the
6185a8d651aSFelipe Balbi  * host detect that a USB device is attached, and starting to draw power
6195a8d651aSFelipe Balbi  * (8mA or possibly more, especially after SET_CONFIGURATION).
6205a8d651aSFelipe Balbi  *
6215a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
6225a8d651aSFelipe Balbi  */
usb_gadget_vbus_connect(struct usb_gadget * gadget)6235a8d651aSFelipe Balbi int usb_gadget_vbus_connect(struct usb_gadget *gadget)
6245a8d651aSFelipe Balbi {
6255e42d710SFelipe Balbi 	int ret = 0;
6265e42d710SFelipe Balbi 
6275e42d710SFelipe Balbi 	if (!gadget->ops->vbus_session) {
6285e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
6295e42d710SFelipe Balbi 		goto out;
6305e42d710SFelipe Balbi 	}
6315e42d710SFelipe Balbi 
6325e42d710SFelipe Balbi 	ret = gadget->ops->vbus_session(gadget, 1);
6335e42d710SFelipe Balbi 
6345e42d710SFelipe Balbi out:
6355e42d710SFelipe Balbi 	trace_usb_gadget_vbus_connect(gadget, ret);
6365e42d710SFelipe Balbi 
6375e42d710SFelipe Balbi 	return ret;
6385a8d651aSFelipe Balbi }
6395a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
6405a8d651aSFelipe Balbi 
6415a8d651aSFelipe Balbi /**
6425a8d651aSFelipe Balbi  * usb_gadget_vbus_draw - constrain controller's VBUS power usage
6435a8d651aSFelipe Balbi  * @gadget:The device whose VBUS usage is being described
6445a8d651aSFelipe Balbi  * @mA:How much current to draw, in milliAmperes.  This should be twice
6455a8d651aSFelipe Balbi  *	the value listed in the configuration descriptor bMaxPower field.
6465a8d651aSFelipe Balbi  *
6475a8d651aSFelipe Balbi  * This call is used by gadget drivers during SET_CONFIGURATION calls,
6485a8d651aSFelipe Balbi  * reporting how much power the device may consume.  For example, this
6495a8d651aSFelipe Balbi  * could affect how quickly batteries are recharged.
6505a8d651aSFelipe Balbi  *
6515a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
6525a8d651aSFelipe Balbi  */
usb_gadget_vbus_draw(struct usb_gadget * gadget,unsigned mA)6535a8d651aSFelipe Balbi int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
6545a8d651aSFelipe Balbi {
6555e42d710SFelipe Balbi 	int ret = 0;
6565e42d710SFelipe Balbi 
6575e42d710SFelipe Balbi 	if (!gadget->ops->vbus_draw) {
6585e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
6595e42d710SFelipe Balbi 		goto out;
6605e42d710SFelipe Balbi 	}
6615e42d710SFelipe Balbi 
6625e42d710SFelipe Balbi 	ret = gadget->ops->vbus_draw(gadget, mA);
6635e42d710SFelipe Balbi 	if (!ret)
6645e42d710SFelipe Balbi 		gadget->mA = mA;
6655e42d710SFelipe Balbi 
6665e42d710SFelipe Balbi out:
6675e42d710SFelipe Balbi 	trace_usb_gadget_vbus_draw(gadget, ret);
6685e42d710SFelipe Balbi 
6695e42d710SFelipe Balbi 	return ret;
6705a8d651aSFelipe Balbi }
6715a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
6725a8d651aSFelipe Balbi 
6735a8d651aSFelipe Balbi /**
6745a8d651aSFelipe Balbi  * usb_gadget_vbus_disconnect - notify controller about VBUS session end
6755a8d651aSFelipe Balbi  * @gadget:the device whose VBUS supply is being described
6765a8d651aSFelipe Balbi  * Context: can sleep
6775a8d651aSFelipe Balbi  *
6785a8d651aSFelipe Balbi  * This call is used by a driver for an external transceiver (or GPIO)
6795a8d651aSFelipe Balbi  * that detects a VBUS power session ending.  Common responses include
6805a8d651aSFelipe Balbi  * reversing everything done in usb_gadget_vbus_connect().
6815a8d651aSFelipe Balbi  *
6825a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
6835a8d651aSFelipe Balbi  */
usb_gadget_vbus_disconnect(struct usb_gadget * gadget)6845a8d651aSFelipe Balbi int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
6855a8d651aSFelipe Balbi {
6865e42d710SFelipe Balbi 	int ret = 0;
6875e42d710SFelipe Balbi 
6885e42d710SFelipe Balbi 	if (!gadget->ops->vbus_session) {
6895e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
6905e42d710SFelipe Balbi 		goto out;
6915e42d710SFelipe Balbi 	}
6925e42d710SFelipe Balbi 
6935e42d710SFelipe Balbi 	ret = gadget->ops->vbus_session(gadget, 0);
6945e42d710SFelipe Balbi 
6955e42d710SFelipe Balbi out:
6965e42d710SFelipe Balbi 	trace_usb_gadget_vbus_disconnect(gadget, ret);
6975e42d710SFelipe Balbi 
6985e42d710SFelipe Balbi 	return ret;
6995a8d651aSFelipe Balbi }
7005a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
7015a8d651aSFelipe Balbi 
usb_gadget_connect_locked(struct usb_gadget * gadget)702286d9975SBadhri Jagan Sridharan static int usb_gadget_connect_locked(struct usb_gadget *gadget)
703286d9975SBadhri Jagan Sridharan 	__must_hold(&gadget->udc->connect_lock)
7045a8d651aSFelipe Balbi {
7055e42d710SFelipe Balbi 	int ret = 0;
7065a8d651aSFelipe Balbi 
7075e42d710SFelipe Balbi 	if (!gadget->ops->pullup) {
7085e42d710SFelipe Balbi 		ret = -EOPNOTSUPP;
7095e42d710SFelipe Balbi 		goto out;
7105e42d710SFelipe Balbi 	}
7115a8d651aSFelipe Balbi 
712286d9975SBadhri Jagan Sridharan 	if (gadget->deactivated || !gadget->udc->allow_connect || !gadget->udc->started) {
7135a8d651aSFelipe Balbi 		/*
714286d9975SBadhri Jagan Sridharan 		 * If the gadget isn't usable (because it is deactivated,
715286d9975SBadhri Jagan Sridharan 		 * unbound, or not yet started), we only save the new state.
716286d9975SBadhri Jagan Sridharan 		 * The gadget will be connected automatically when it is
717286d9975SBadhri Jagan Sridharan 		 * activated/bound/started.
7185a8d651aSFelipe Balbi 		 */
7195a8d651aSFelipe Balbi 		gadget->connected = true;
7205e42d710SFelipe Balbi 		goto out;
7215a8d651aSFelipe Balbi 	}
7225a8d651aSFelipe Balbi 
7235a8d651aSFelipe Balbi 	ret = gadget->ops->pullup(gadget, 1);
7245a8d651aSFelipe Balbi 	if (!ret)
7255a8d651aSFelipe Balbi 		gadget->connected = 1;
7265e42d710SFelipe Balbi 
7275e42d710SFelipe Balbi out:
7285e42d710SFelipe Balbi 	trace_usb_gadget_connect(gadget, ret);
7295e42d710SFelipe Balbi 
7305a8d651aSFelipe Balbi 	return ret;
7315a8d651aSFelipe Balbi }
7320db213eaSBadhri Jagan Sridharan 
7330db213eaSBadhri Jagan Sridharan /**
734286d9975SBadhri Jagan Sridharan  * usb_gadget_connect - software-controlled connect to USB host
735286d9975SBadhri Jagan Sridharan  * @gadget:the peripheral being connected
7360db213eaSBadhri Jagan Sridharan  *
737286d9975SBadhri Jagan Sridharan  * Enables the D+ (or potentially D-) pullup.  The host will start
738286d9975SBadhri Jagan Sridharan  * enumerating this gadget when the pullup is active and a VBUS session
739286d9975SBadhri Jagan Sridharan  * is active (the link is powered).
7400db213eaSBadhri Jagan Sridharan  *
7410db213eaSBadhri Jagan Sridharan  * Returns zero on success, else negative errno.
7420db213eaSBadhri Jagan Sridharan  */
usb_gadget_connect(struct usb_gadget * gadget)743286d9975SBadhri Jagan Sridharan int usb_gadget_connect(struct usb_gadget *gadget)
744286d9975SBadhri Jagan Sridharan {
745286d9975SBadhri Jagan Sridharan 	int ret;
746286d9975SBadhri Jagan Sridharan 
747286d9975SBadhri Jagan Sridharan 	mutex_lock(&gadget->udc->connect_lock);
748286d9975SBadhri Jagan Sridharan 	ret = usb_gadget_connect_locked(gadget);
749286d9975SBadhri Jagan Sridharan 	mutex_unlock(&gadget->udc->connect_lock);
750286d9975SBadhri Jagan Sridharan 
751286d9975SBadhri Jagan Sridharan 	return ret;
752286d9975SBadhri Jagan Sridharan }
753286d9975SBadhri Jagan Sridharan EXPORT_SYMBOL_GPL(usb_gadget_connect);
754286d9975SBadhri Jagan Sridharan 
usb_gadget_disconnect_locked(struct usb_gadget * gadget)755286d9975SBadhri Jagan Sridharan static int usb_gadget_disconnect_locked(struct usb_gadget *gadget)
756286d9975SBadhri Jagan Sridharan 	__must_hold(&gadget->udc->connect_lock)
7570db213eaSBadhri Jagan Sridharan {
758f22e9b67SFrancesco Dolcini 	int ret = 0;
7590db213eaSBadhri Jagan Sridharan 
760f22e9b67SFrancesco Dolcini 	if (!gadget->ops->pullup) {
761f22e9b67SFrancesco Dolcini 		ret = -EOPNOTSUPP;
762f22e9b67SFrancesco Dolcini 		goto out;
763f22e9b67SFrancesco Dolcini 	}
764f22e9b67SFrancesco Dolcini 
765f22e9b67SFrancesco Dolcini 	if (!gadget->connected)
766f22e9b67SFrancesco Dolcini 		goto out;
767f22e9b67SFrancesco Dolcini 
768286d9975SBadhri Jagan Sridharan 	if (gadget->deactivated || !gadget->udc->started) {
769f22e9b67SFrancesco Dolcini 		/*
770f22e9b67SFrancesco Dolcini 		 * If gadget is deactivated we only save new state.
771f22e9b67SFrancesco Dolcini 		 * Gadget will stay disconnected after activation.
772f22e9b67SFrancesco Dolcini 		 */
773f22e9b67SFrancesco Dolcini 		gadget->connected = false;
774f22e9b67SFrancesco Dolcini 		goto out;
775f22e9b67SFrancesco Dolcini 	}
776f22e9b67SFrancesco Dolcini 
777f22e9b67SFrancesco Dolcini 	ret = gadget->ops->pullup(gadget, 0);
778f22e9b67SFrancesco Dolcini 	if (!ret)
779f22e9b67SFrancesco Dolcini 		gadget->connected = 0;
780f22e9b67SFrancesco Dolcini 
781f22e9b67SFrancesco Dolcini 	mutex_lock(&udc_lock);
782f22e9b67SFrancesco Dolcini 	if (gadget->udc->driver)
783f22e9b67SFrancesco Dolcini 		gadget->udc->driver->disconnect(gadget);
784f22e9b67SFrancesco Dolcini 	mutex_unlock(&udc_lock);
785f22e9b67SFrancesco Dolcini 
786f22e9b67SFrancesco Dolcini out:
787f22e9b67SFrancesco Dolcini 	trace_usb_gadget_disconnect(gadget, ret);
7880db213eaSBadhri Jagan Sridharan 
7890db213eaSBadhri Jagan Sridharan 	return ret;
7900db213eaSBadhri Jagan Sridharan }
791286d9975SBadhri Jagan Sridharan 
792286d9975SBadhri Jagan Sridharan /**
793286d9975SBadhri Jagan Sridharan  * usb_gadget_disconnect - software-controlled disconnect from USB host
794286d9975SBadhri Jagan Sridharan  * @gadget:the peripheral being disconnected
795286d9975SBadhri Jagan Sridharan  *
796286d9975SBadhri Jagan Sridharan  * Disables the D+ (or potentially D-) pullup, which the host may see
797286d9975SBadhri Jagan Sridharan  * as a disconnect (when a VBUS session is active).  Not all systems
798286d9975SBadhri Jagan Sridharan  * support software pullup controls.
799286d9975SBadhri Jagan Sridharan  *
800286d9975SBadhri Jagan Sridharan  * Following a successful disconnect, invoke the ->disconnect() callback
801286d9975SBadhri Jagan Sridharan  * for the current gadget driver so that UDC drivers don't need to.
802286d9975SBadhri Jagan Sridharan  *
803286d9975SBadhri Jagan Sridharan  * Returns zero on success, else negative errno.
804286d9975SBadhri Jagan Sridharan  */
usb_gadget_disconnect(struct usb_gadget * gadget)805286d9975SBadhri Jagan Sridharan int usb_gadget_disconnect(struct usb_gadget *gadget)
806286d9975SBadhri Jagan Sridharan {
807286d9975SBadhri Jagan Sridharan 	int ret;
808286d9975SBadhri Jagan Sridharan 
809286d9975SBadhri Jagan Sridharan 	mutex_lock(&gadget->udc->connect_lock);
810286d9975SBadhri Jagan Sridharan 	ret = usb_gadget_disconnect_locked(gadget);
811286d9975SBadhri Jagan Sridharan 	mutex_unlock(&gadget->udc->connect_lock);
812286d9975SBadhri Jagan Sridharan 
813286d9975SBadhri Jagan Sridharan 	return ret;
814286d9975SBadhri Jagan Sridharan }
8155a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
8165a8d651aSFelipe Balbi 
8175a8d651aSFelipe Balbi /**
8185a8d651aSFelipe Balbi  * usb_gadget_deactivate - deactivate function which is not ready to work
8195a8d651aSFelipe Balbi  * @gadget: the peripheral being deactivated
8205a8d651aSFelipe Balbi  *
8215a8d651aSFelipe Balbi  * This routine may be used during the gadget driver bind() call to prevent
8225a8d651aSFelipe Balbi  * the peripheral from ever being visible to the USB host, unless later
8235a8d651aSFelipe Balbi  * usb_gadget_activate() is called.  For example, user mode components may
8245a8d651aSFelipe Balbi  * need to be activated before the system can talk to hosts.
8255a8d651aSFelipe Balbi  *
82665dadb2bSAlan Stern  * This routine may sleep; it must not be called in interrupt context
82765dadb2bSAlan Stern  * (such as from within a gadget driver's disconnect() callback).
82865dadb2bSAlan Stern  *
8295a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
8305a8d651aSFelipe Balbi  */
usb_gadget_deactivate(struct usb_gadget * gadget)8315a8d651aSFelipe Balbi int usb_gadget_deactivate(struct usb_gadget *gadget)
8325a8d651aSFelipe Balbi {
8335e42d710SFelipe Balbi 	int ret = 0;
8345a8d651aSFelipe Balbi 
835286d9975SBadhri Jagan Sridharan 	mutex_lock(&gadget->udc->connect_lock);
8365a8d651aSFelipe Balbi 	if (gadget->deactivated)
837286d9975SBadhri Jagan Sridharan 		goto unlock;
8385a8d651aSFelipe Balbi 
8395a8d651aSFelipe Balbi 	if (gadget->connected) {
840286d9975SBadhri Jagan Sridharan 		ret = usb_gadget_disconnect_locked(gadget);
8415a8d651aSFelipe Balbi 		if (ret)
842286d9975SBadhri Jagan Sridharan 			goto unlock;
8435e42d710SFelipe Balbi 
8445a8d651aSFelipe Balbi 		/*
8455a8d651aSFelipe Balbi 		 * If gadget was being connected before deactivation, we want
8465a8d651aSFelipe Balbi 		 * to reconnect it in usb_gadget_activate().
8475a8d651aSFelipe Balbi 		 */
8485a8d651aSFelipe Balbi 		gadget->connected = true;
8495a8d651aSFelipe Balbi 	}
8505a8d651aSFelipe Balbi 	gadget->deactivated = true;
8515a8d651aSFelipe Balbi 
852286d9975SBadhri Jagan Sridharan unlock:
853286d9975SBadhri Jagan Sridharan 	mutex_unlock(&gadget->udc->connect_lock);
8545e42d710SFelipe Balbi 	trace_usb_gadget_deactivate(gadget, ret);
8555e42d710SFelipe Balbi 
8565e42d710SFelipe Balbi 	return ret;
8575a8d651aSFelipe Balbi }
8585a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
8595a8d651aSFelipe Balbi 
8605a8d651aSFelipe Balbi /**
8615a8d651aSFelipe Balbi  * usb_gadget_activate - activate function which is not ready to work
8625a8d651aSFelipe Balbi  * @gadget: the peripheral being activated
8635a8d651aSFelipe Balbi  *
8645a8d651aSFelipe Balbi  * This routine activates gadget which was previously deactivated with
8655a8d651aSFelipe Balbi  * usb_gadget_deactivate() call. It calls usb_gadget_connect() if needed.
8665a8d651aSFelipe Balbi  *
86765dadb2bSAlan Stern  * This routine may sleep; it must not be called in interrupt context.
86865dadb2bSAlan Stern  *
8695a8d651aSFelipe Balbi  * Returns zero on success, else negative errno.
8705a8d651aSFelipe Balbi  */
usb_gadget_activate(struct usb_gadget * gadget)8715a8d651aSFelipe Balbi int usb_gadget_activate(struct usb_gadget *gadget)
8725a8d651aSFelipe Balbi {
8735e42d710SFelipe Balbi 	int ret = 0;
8745e42d710SFelipe Balbi 
875286d9975SBadhri Jagan Sridharan 	mutex_lock(&gadget->udc->connect_lock);
8765a8d651aSFelipe Balbi 	if (!gadget->deactivated)
877286d9975SBadhri Jagan Sridharan 		goto unlock;
8785a8d651aSFelipe Balbi 
8795a8d651aSFelipe Balbi 	gadget->deactivated = false;
8805a8d651aSFelipe Balbi 
8815a8d651aSFelipe Balbi 	/*
8825a8d651aSFelipe Balbi 	 * If gadget has been connected before deactivation, or became connected
8835a8d651aSFelipe Balbi 	 * while it was being deactivated, we call usb_gadget_connect().
8845a8d651aSFelipe Balbi 	 */
8855a8d651aSFelipe Balbi 	if (gadget->connected)
886286d9975SBadhri Jagan Sridharan 		ret = usb_gadget_connect_locked(gadget);
8875a8d651aSFelipe Balbi 
888286d9975SBadhri Jagan Sridharan unlock:
889286d9975SBadhri Jagan Sridharan 	mutex_unlock(&gadget->udc->connect_lock);
8905e42d710SFelipe Balbi 	trace_usb_gadget_activate(gadget, ret);
8915e42d710SFelipe Balbi 
8925e42d710SFelipe Balbi 	return ret;
8935a8d651aSFelipe Balbi }
8945a8d651aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_activate);
8955a8d651aSFelipe Balbi 
8965a8d651aSFelipe Balbi /* ------------------------------------------------------------------------- */
8975a8d651aSFelipe Balbi 
898908b9613SAlan Stern #ifdef	CONFIG_HAS_DMA
899908b9613SAlan Stern 
usb_gadget_map_request_by_dev(struct device * dev,struct usb_request * req,int is_in)900679ca39fSYoshihiro Shimoda int usb_gadget_map_request_by_dev(struct device *dev,
901a698908dSFelipe Balbi 		struct usb_request *req, int is_in)
902a698908dSFelipe Balbi {
903a698908dSFelipe Balbi 	if (req->length == 0)
904a698908dSFelipe Balbi 		return 0;
905a698908dSFelipe Balbi 
90699f638ddSPaul Cercueil 	if (req->sg_was_mapped) {
90799f638ddSPaul Cercueil 		req->num_mapped_sgs = req->num_sgs;
90899f638ddSPaul Cercueil 		return 0;
90999f638ddSPaul Cercueil 	}
91099f638ddSPaul Cercueil 
911a698908dSFelipe Balbi 	if (req->num_sgs) {
912a698908dSFelipe Balbi 		int     mapped;
913a698908dSFelipe Balbi 
9147ace8fc8SYoshihiro Shimoda 		mapped = dma_map_sg(dev, req->sg, req->num_sgs,
915a698908dSFelipe Balbi 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
916a698908dSFelipe Balbi 		if (mapped == 0) {
9175096c4d3SYoshihiro Shimoda 			dev_err(dev, "failed to map SGs\n");
918a698908dSFelipe Balbi 			return -EFAULT;
919a698908dSFelipe Balbi 		}
920a698908dSFelipe Balbi 
921a698908dSFelipe Balbi 		req->num_mapped_sgs = mapped;
922a698908dSFelipe Balbi 	} else {
923614536daSFlorian Fainelli 		if (is_vmalloc_addr(req->buf)) {
924614536daSFlorian Fainelli 			dev_err(dev, "buffer is not dma capable\n");
925614536daSFlorian Fainelli 			return -EFAULT;
926614536daSFlorian Fainelli 		} else if (object_is_on_stack(req->buf)) {
927614536daSFlorian Fainelli 			dev_err(dev, "buffer is on stack\n");
928614536daSFlorian Fainelli 			return -EFAULT;
929614536daSFlorian Fainelli 		}
930614536daSFlorian Fainelli 
9317ace8fc8SYoshihiro Shimoda 		req->dma = dma_map_single(dev, req->buf, req->length,
932a698908dSFelipe Balbi 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
933a698908dSFelipe Balbi 
9347ace8fc8SYoshihiro Shimoda 		if (dma_mapping_error(dev, req->dma)) {
9357ace8fc8SYoshihiro Shimoda 			dev_err(dev, "failed to map buffer\n");
936a698908dSFelipe Balbi 			return -EFAULT;
937a698908dSFelipe Balbi 		}
93831fe084fSJack Pham 
93931fe084fSJack Pham 		req->dma_mapped = 1;
940a698908dSFelipe Balbi 	}
941a698908dSFelipe Balbi 
942a698908dSFelipe Balbi 	return 0;
943a698908dSFelipe Balbi }
944679ca39fSYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
945679ca39fSYoshihiro Shimoda 
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)946679ca39fSYoshihiro Shimoda int usb_gadget_map_request(struct usb_gadget *gadget,
947679ca39fSYoshihiro Shimoda 		struct usb_request *req, int is_in)
948679ca39fSYoshihiro Shimoda {
949679ca39fSYoshihiro Shimoda 	return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
950679ca39fSYoshihiro Shimoda }
951a698908dSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_map_request);
952a698908dSFelipe Balbi 
usb_gadget_unmap_request_by_dev(struct device * dev,struct usb_request * req,int is_in)953679ca39fSYoshihiro Shimoda void usb_gadget_unmap_request_by_dev(struct device *dev,
954a698908dSFelipe Balbi 		struct usb_request *req, int is_in)
955a698908dSFelipe Balbi {
95699f638ddSPaul Cercueil 	if (req->length == 0 || req->sg_was_mapped)
957a698908dSFelipe Balbi 		return;
958a698908dSFelipe Balbi 
959a698908dSFelipe Balbi 	if (req->num_mapped_sgs) {
96023fd537cSFelipe Balbi 		dma_unmap_sg(dev, req->sg, req->num_sgs,
961a698908dSFelipe Balbi 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
962a698908dSFelipe Balbi 
963a698908dSFelipe Balbi 		req->num_mapped_sgs = 0;
96431fe084fSJack Pham 	} else if (req->dma_mapped) {
965679ca39fSYoshihiro Shimoda 		dma_unmap_single(dev, req->dma, req->length,
966a698908dSFelipe Balbi 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
96731fe084fSJack Pham 		req->dma_mapped = 0;
968a698908dSFelipe Balbi 	}
969a698908dSFelipe Balbi }
970679ca39fSYoshihiro Shimoda EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
971679ca39fSYoshihiro Shimoda 
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)972679ca39fSYoshihiro Shimoda void usb_gadget_unmap_request(struct usb_gadget *gadget,
973679ca39fSYoshihiro Shimoda 		struct usb_request *req, int is_in)
974679ca39fSYoshihiro Shimoda {
975679ca39fSYoshihiro Shimoda 	usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
976679ca39fSYoshihiro Shimoda }
977a698908dSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
978a698908dSFelipe Balbi 
979908b9613SAlan Stern #endif	/* CONFIG_HAS_DMA */
980908b9613SAlan Stern 
981a698908dSFelipe Balbi /* ------------------------------------------------------------------------- */
982a698908dSFelipe Balbi 
9833fc2aa55SMichal Sojka /**
9843fc2aa55SMichal Sojka  * usb_gadget_giveback_request - give the request back to the gadget layer
985e21cd08fSLee Jones  * @ep: the endpoint to be used with with the request
986e21cd08fSLee Jones  * @req: the request being given back
987e21cd08fSLee Jones  *
9883fc2aa55SMichal Sojka  * This is called by device controller drivers in order to return the
9893fc2aa55SMichal Sojka  * completed request back to the gadget layer.
9903fc2aa55SMichal Sojka  */
usb_gadget_giveback_request(struct usb_ep * ep,struct usb_request * req)9913fc2aa55SMichal Sojka void usb_gadget_giveback_request(struct usb_ep *ep,
9923fc2aa55SMichal Sojka 		struct usb_request *req)
9933fc2aa55SMichal Sojka {
9940cfbd328SMichal Sojka 	if (likely(req->status == 0))
9950cfbd328SMichal Sojka 		usb_led_activity(USB_LED_EVENT_GADGET);
9960cfbd328SMichal Sojka 
9975e42d710SFelipe Balbi 	trace_usb_gadget_giveback_request(ep, req, 0);
9985e42d710SFelipe Balbi 
9993fc2aa55SMichal Sojka 	req->complete(ep, req);
10003fc2aa55SMichal Sojka }
10013fc2aa55SMichal Sojka EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
10023fc2aa55SMichal Sojka 
10033fc2aa55SMichal Sojka /* ------------------------------------------------------------------------- */
10043fc2aa55SMichal Sojka 
1005b0aea003SRobert Baldyga /**
1006b0aea003SRobert Baldyga  * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
1007b0aea003SRobert Baldyga  *	in second parameter or NULL if searched endpoint not found
1008b0aea003SRobert Baldyga  * @g: controller to check for quirk
1009b0aea003SRobert Baldyga  * @name: name of searched endpoint
1010b0aea003SRobert Baldyga  */
gadget_find_ep_by_name(struct usb_gadget * g,const char * name)1011b0aea003SRobert Baldyga struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
1012b0aea003SRobert Baldyga {
1013b0aea003SRobert Baldyga 	struct usb_ep *ep;
1014b0aea003SRobert Baldyga 
1015b0aea003SRobert Baldyga 	gadget_for_each_ep(ep, g) {
1016b0aea003SRobert Baldyga 		if (!strcmp(ep->name, name))
1017b0aea003SRobert Baldyga 			return ep;
1018b0aea003SRobert Baldyga 	}
1019b0aea003SRobert Baldyga 
1020b0aea003SRobert Baldyga 	return NULL;
1021b0aea003SRobert Baldyga }
1022b0aea003SRobert Baldyga EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
1023b0aea003SRobert Baldyga 
1024b0aea003SRobert Baldyga /* ------------------------------------------------------------------------- */
1025b0aea003SRobert Baldyga 
usb_gadget_ep_match_desc(struct usb_gadget * gadget,struct usb_ep * ep,struct usb_endpoint_descriptor * desc,struct usb_ss_ep_comp_descriptor * ep_comp)10264278c687SRobert Baldyga int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
10274278c687SRobert Baldyga 		struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
10284278c687SRobert Baldyga 		struct usb_ss_ep_comp_descriptor *ep_comp)
10294278c687SRobert Baldyga {
10304278c687SRobert Baldyga 	u8		type;
10314278c687SRobert Baldyga 	u16		max;
10324278c687SRobert Baldyga 	int		num_req_streams = 0;
10334278c687SRobert Baldyga 
10344278c687SRobert Baldyga 	/* endpoint already claimed? */
10354278c687SRobert Baldyga 	if (ep->claimed)
10364278c687SRobert Baldyga 		return 0;
10374278c687SRobert Baldyga 
10384278c687SRobert Baldyga 	type = usb_endpoint_type(desc);
103999bcb238SJaejoong Kim 	max = usb_endpoint_maxp(desc);
10404278c687SRobert Baldyga 
10414278c687SRobert Baldyga 	if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
10424278c687SRobert Baldyga 		return 0;
10434278c687SRobert Baldyga 	if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
10444278c687SRobert Baldyga 		return 0;
10454278c687SRobert Baldyga 
10464278c687SRobert Baldyga 	if (max > ep->maxpacket_limit)
10474278c687SRobert Baldyga 		return 0;
10484278c687SRobert Baldyga 
10494278c687SRobert Baldyga 	/* "high bandwidth" works only at high speed */
105011fb3799SBenjamin Herrenschmidt 	if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
10514278c687SRobert Baldyga 		return 0;
10524278c687SRobert Baldyga 
10534278c687SRobert Baldyga 	switch (type) {
10544278c687SRobert Baldyga 	case USB_ENDPOINT_XFER_CONTROL:
10554278c687SRobert Baldyga 		/* only support ep0 for portable CONTROL traffic */
10564278c687SRobert Baldyga 		return 0;
10574278c687SRobert Baldyga 	case USB_ENDPOINT_XFER_ISOC:
10584278c687SRobert Baldyga 		if (!ep->caps.type_iso)
10594278c687SRobert Baldyga 			return 0;
10604278c687SRobert Baldyga 		/* ISO:  limit 1023 bytes full speed, 1024 high/super speed */
10614278c687SRobert Baldyga 		if (!gadget_is_dualspeed(gadget) && max > 1023)
10624278c687SRobert Baldyga 			return 0;
10634278c687SRobert Baldyga 		break;
10644278c687SRobert Baldyga 	case USB_ENDPOINT_XFER_BULK:
10654278c687SRobert Baldyga 		if (!ep->caps.type_bulk)
10664278c687SRobert Baldyga 			return 0;
10674278c687SRobert Baldyga 		if (ep_comp && gadget_is_superspeed(gadget)) {
10684278c687SRobert Baldyga 			/* Get the number of required streams from the
10694278c687SRobert Baldyga 			 * EP companion descriptor and see if the EP
10704278c687SRobert Baldyga 			 * matches it
10714278c687SRobert Baldyga 			 */
10724278c687SRobert Baldyga 			num_req_streams = ep_comp->bmAttributes & 0x1f;
10734278c687SRobert Baldyga 			if (num_req_streams > ep->max_streams)
10744278c687SRobert Baldyga 				return 0;
10754278c687SRobert Baldyga 		}
10764278c687SRobert Baldyga 		break;
10774278c687SRobert Baldyga 	case USB_ENDPOINT_XFER_INT:
10784278c687SRobert Baldyga 		/* Bulk endpoints handle interrupt transfers,
10794278c687SRobert Baldyga 		 * except the toggle-quirky iso-synch kind
10804278c687SRobert Baldyga 		 */
10814278c687SRobert Baldyga 		if (!ep->caps.type_int && !ep->caps.type_bulk)
10824278c687SRobert Baldyga 			return 0;
10834278c687SRobert Baldyga 		/* INT:  limit 64 bytes full speed, 1024 high/super speed */
10844278c687SRobert Baldyga 		if (!gadget_is_dualspeed(gadget) && max > 64)
10854278c687SRobert Baldyga 			return 0;
10864278c687SRobert Baldyga 		break;
10874278c687SRobert Baldyga 	}
10884278c687SRobert Baldyga 
10894278c687SRobert Baldyga 	return 1;
10904278c687SRobert Baldyga }
10914278c687SRobert Baldyga EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
10924278c687SRobert Baldyga 
1093ce7d0008SWesley Cheng /**
1094ce7d0008SWesley Cheng  * usb_gadget_check_config - checks if the UDC can support the binded
1095ce7d0008SWesley Cheng  *	configuration
1096ce7d0008SWesley Cheng  * @gadget: controller to check the USB configuration
1097ce7d0008SWesley Cheng  *
1098ce7d0008SWesley Cheng  * Ensure that a UDC is able to support the requested resources by a
1099ce7d0008SWesley Cheng  * configuration, and that there are no resource limitations, such as
1100ce7d0008SWesley Cheng  * internal memory allocated to all requested endpoints.
1101ce7d0008SWesley Cheng  *
1102ce7d0008SWesley Cheng  * Returns zero on success, else a negative errno.
1103ce7d0008SWesley Cheng  */
usb_gadget_check_config(struct usb_gadget * gadget)1104ce7d0008SWesley Cheng int usb_gadget_check_config(struct usb_gadget *gadget)
1105ce7d0008SWesley Cheng {
1106ce7d0008SWesley Cheng 	if (gadget->ops->check_config)
1107ce7d0008SWesley Cheng 		return gadget->ops->check_config(gadget);
1108ce7d0008SWesley Cheng 	return 0;
1109ce7d0008SWesley Cheng }
1110ce7d0008SWesley Cheng EXPORT_SYMBOL_GPL(usb_gadget_check_config);
1111ce7d0008SWesley Cheng 
11124278c687SRobert Baldyga /* ------------------------------------------------------------------------- */
11134278c687SRobert Baldyga 
usb_gadget_state_work(struct work_struct * work)11145702f753SFelipe Balbi static void usb_gadget_state_work(struct work_struct *work)
11155702f753SFelipe Balbi {
11165702f753SFelipe Balbi 	struct usb_gadget *gadget = work_to_gadget(work);
1117dfea9c94SPeter Chen 	struct usb_udc *udc = gadget->udc;
11185702f753SFelipe Balbi 
1119dfea9c94SPeter Chen 	if (udc)
1120af54954aSAndreas Larsson 		sysfs_notify(&udc->dev.kobj, NULL, "state");
11215702f753SFelipe Balbi }
11225702f753SFelipe Balbi 
usb_gadget_set_state(struct usb_gadget * gadget,enum usb_device_state state)112349401f41SFelipe Balbi void usb_gadget_set_state(struct usb_gadget *gadget,
112449401f41SFelipe Balbi 		enum usb_device_state state)
112549401f41SFelipe Balbi {
112649401f41SFelipe Balbi 	gadget->state = state;
11275702f753SFelipe Balbi 	schedule_work(&gadget->work);
112849401f41SFelipe Balbi }
112949401f41SFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_set_state);
113049401f41SFelipe Balbi 
113149401f41SFelipe Balbi /* ------------------------------------------------------------------------- */
113249401f41SFelipe Balbi 
1133286d9975SBadhri Jagan Sridharan /* Acquire connect_lock before calling this function. */
usb_udc_connect_control_locked(struct usb_udc * udc)11340ea39e03SKrishna Kurapati static int usb_udc_connect_control_locked(struct usb_udc *udc) __must_hold(&udc->connect_lock)
1135628ef0d2SPeter Chen {
1136f22e9b67SFrancesco Dolcini 	if (udc->vbus)
11370ea39e03SKrishna Kurapati 		return usb_gadget_connect_locked(udc->gadget);
1138628ef0d2SPeter Chen 	else
11390ea39e03SKrishna Kurapati 		return usb_gadget_disconnect_locked(udc->gadget);
1140628ef0d2SPeter Chen }
1141628ef0d2SPeter Chen 
vbus_event_work(struct work_struct * work)114250966da8SBadhri Jagan Sridharan static void vbus_event_work(struct work_struct *work)
114350966da8SBadhri Jagan Sridharan {
114450966da8SBadhri Jagan Sridharan 	struct usb_udc *udc = container_of(work, struct usb_udc, vbus_work);
114550966da8SBadhri Jagan Sridharan 
1146286d9975SBadhri Jagan Sridharan 	mutex_lock(&udc->connect_lock);
1147286d9975SBadhri Jagan Sridharan 	usb_udc_connect_control_locked(udc);
1148286d9975SBadhri Jagan Sridharan 	mutex_unlock(&udc->connect_lock);
114950966da8SBadhri Jagan Sridharan }
115050966da8SBadhri Jagan Sridharan 
1151628ef0d2SPeter Chen /**
1152628ef0d2SPeter Chen  * usb_udc_vbus_handler - updates the udc core vbus status, and try to
1153628ef0d2SPeter Chen  * connect or disconnect gadget
1154628ef0d2SPeter Chen  * @gadget: The gadget which vbus change occurs
1155628ef0d2SPeter Chen  * @status: The vbus status
1156628ef0d2SPeter Chen  *
1157628ef0d2SPeter Chen  * The udc driver calls it when it wants to connect or disconnect gadget
1158628ef0d2SPeter Chen  * according to vbus status.
115950966da8SBadhri Jagan Sridharan  *
116050966da8SBadhri Jagan Sridharan  * This function can be invoked from interrupt context by irq handlers of
116150966da8SBadhri Jagan Sridharan  * the gadget drivers, however, usb_udc_connect_control() has to run in
116250966da8SBadhri Jagan Sridharan  * non-atomic context due to the following:
116350966da8SBadhri Jagan Sridharan  * a. Some of the gadget driver implementations expect the ->pullup
116450966da8SBadhri Jagan Sridharan  * callback to be invoked in non-atomic context.
116550966da8SBadhri Jagan Sridharan  * b. usb_gadget_disconnect() acquires udc_lock which is a mutex.
116650966da8SBadhri Jagan Sridharan  * Hence offload invocation of usb_udc_connect_control() to workqueue.
1167628ef0d2SPeter Chen  */
usb_udc_vbus_handler(struct usb_gadget * gadget,bool status)1168628ef0d2SPeter Chen void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1169628ef0d2SPeter Chen {
1170628ef0d2SPeter Chen 	struct usb_udc *udc = gadget->udc;
1171628ef0d2SPeter Chen 
1172628ef0d2SPeter Chen 	if (udc) {
1173628ef0d2SPeter Chen 		udc->vbus = status;
117450966da8SBadhri Jagan Sridharan 		schedule_work(&udc->vbus_work);
1175628ef0d2SPeter Chen 	}
1176628ef0d2SPeter Chen }
1177628ef0d2SPeter Chen EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1178628ef0d2SPeter Chen 
11792ccea03aSFelipe Balbi /**
1180974a70bdSPeter Chen  * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
1181974a70bdSPeter Chen  * @gadget: The gadget which bus reset occurs
1182974a70bdSPeter Chen  * @driver: The gadget driver we want to notify
1183974a70bdSPeter Chen  *
1184974a70bdSPeter Chen  * If the udc driver has bus reset handler, it needs to call this when the bus
1185974a70bdSPeter Chen  * reset occurs, it notifies the gadget driver that the bus reset occurs as
1186974a70bdSPeter Chen  * well as updates gadget state.
1187974a70bdSPeter Chen  */
usb_gadget_udc_reset(struct usb_gadget * gadget,struct usb_gadget_driver * driver)1188974a70bdSPeter Chen void usb_gadget_udc_reset(struct usb_gadget *gadget,
1189974a70bdSPeter Chen 		struct usb_gadget_driver *driver)
1190974a70bdSPeter Chen {
1191974a70bdSPeter Chen 	driver->reset(gadget);
1192974a70bdSPeter Chen 	usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1193974a70bdSPeter Chen }
1194974a70bdSPeter Chen EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1195974a70bdSPeter Chen 
1196974a70bdSPeter Chen /**
1197286d9975SBadhri Jagan Sridharan  * usb_gadget_udc_start_locked - tells usb device controller to start up
11982c683347SFelipe Balbi  * @udc: The UDC to be started
1199352c2dc8SSebastian Andrzej Siewior  *
1200352c2dc8SSebastian Andrzej Siewior  * This call is issued by the UDC Class driver when it's about
1201352c2dc8SSebastian Andrzej Siewior  * to register a gadget driver to the device controller, before
1202352c2dc8SSebastian Andrzej Siewior  * calling gadget driver's bind() method.
1203352c2dc8SSebastian Andrzej Siewior  *
1204352c2dc8SSebastian Andrzej Siewior  * It allows the controller to be powered off until strictly
1205352c2dc8SSebastian Andrzej Siewior  * necessary to have it powered on.
1206352c2dc8SSebastian Andrzej Siewior  *
1207352c2dc8SSebastian Andrzej Siewior  * Returns zero on success, else negative errno.
1208286d9975SBadhri Jagan Sridharan  *
1209286d9975SBadhri Jagan Sridharan  * Caller should acquire connect_lock before invoking this function.
1210352c2dc8SSebastian Andrzej Siewior  */
usb_gadget_udc_start_locked(struct usb_udc * udc)1211286d9975SBadhri Jagan Sridharan static inline int usb_gadget_udc_start_locked(struct usb_udc *udc)
1212286d9975SBadhri Jagan Sridharan 	__must_hold(&udc->connect_lock)
1213352c2dc8SSebastian Andrzej Siewior {
121449d08cfcSThinh Nguyen 	int ret;
121549d08cfcSThinh Nguyen 
121649d08cfcSThinh Nguyen 	if (udc->started) {
121749d08cfcSThinh Nguyen 		dev_err(&udc->dev, "UDC had already started\n");
121849d08cfcSThinh Nguyen 		return -EBUSY;
121949d08cfcSThinh Nguyen 	}
122049d08cfcSThinh Nguyen 
122149d08cfcSThinh Nguyen 	ret = udc->gadget->ops->udc_start(udc->gadget, udc->driver);
122249d08cfcSThinh Nguyen 	if (!ret)
122349d08cfcSThinh Nguyen 		udc->started = true;
122449d08cfcSThinh Nguyen 
122549d08cfcSThinh Nguyen 	return ret;
1226352c2dc8SSebastian Andrzej Siewior }
1227352c2dc8SSebastian Andrzej Siewior 
1228352c2dc8SSebastian Andrzej Siewior /**
1229286d9975SBadhri Jagan Sridharan  * usb_gadget_udc_stop_locked - tells usb device controller we don't need it anymore
1230e21cd08fSLee Jones  * @udc: The UDC to be stopped
1231352c2dc8SSebastian Andrzej Siewior  *
1232352c2dc8SSebastian Andrzej Siewior  * This call is issued by the UDC Class driver after calling
1233352c2dc8SSebastian Andrzej Siewior  * gadget driver's unbind() method.
1234352c2dc8SSebastian Andrzej Siewior  *
1235352c2dc8SSebastian Andrzej Siewior  * The details are implementation specific, but it can go as
1236352c2dc8SSebastian Andrzej Siewior  * far as powering off UDC completely and disable its data
1237352c2dc8SSebastian Andrzej Siewior  * line pullups.
1238286d9975SBadhri Jagan Sridharan  *
1239286d9975SBadhri Jagan Sridharan  * Caller should acquire connect lock before invoking this function.
1240352c2dc8SSebastian Andrzej Siewior  */
usb_gadget_udc_stop_locked(struct usb_udc * udc)1241286d9975SBadhri Jagan Sridharan static inline void usb_gadget_udc_stop_locked(struct usb_udc *udc)
1242286d9975SBadhri Jagan Sridharan 	__must_hold(&udc->connect_lock)
1243352c2dc8SSebastian Andrzej Siewior {
124449d08cfcSThinh Nguyen 	if (!udc->started) {
124549d08cfcSThinh Nguyen 		dev_err(&udc->dev, "UDC had already stopped\n");
124649d08cfcSThinh Nguyen 		return;
124749d08cfcSThinh Nguyen 	}
124849d08cfcSThinh Nguyen 
124922835b80SFelipe Balbi 	udc->gadget->ops->udc_stop(udc->gadget);
125049d08cfcSThinh Nguyen 	udc->started = false;
1251352c2dc8SSebastian Andrzej Siewior }
1252352c2dc8SSebastian Andrzej Siewior 
1253352c2dc8SSebastian Andrzej Siewior /**
125467fdfda4SFelipe Balbi  * usb_gadget_udc_set_speed - tells usb device controller speed supported by
125567fdfda4SFelipe Balbi  *    current driver
125667fdfda4SFelipe Balbi  * @udc: The device we want to set maximum speed
125767fdfda4SFelipe Balbi  * @speed: The maximum speed to allowed to run
125867fdfda4SFelipe Balbi  *
125967fdfda4SFelipe Balbi  * This call is issued by the UDC Class driver before calling
126067fdfda4SFelipe Balbi  * usb_gadget_udc_start() in order to make sure that we don't try to
126167fdfda4SFelipe Balbi  * connect on speeds the gadget driver doesn't support.
126267fdfda4SFelipe Balbi  */
usb_gadget_udc_set_speed(struct usb_udc * udc,enum usb_device_speed speed)126367fdfda4SFelipe Balbi static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
126467fdfda4SFelipe Balbi 					    enum usb_device_speed speed)
126567fdfda4SFelipe Balbi {
1266ead4c124SThinh Nguyen 	struct usb_gadget *gadget = udc->gadget;
1267a4f0927eSRoger Quadros 	enum usb_device_speed s;
1268a4f0927eSRoger Quadros 
1269ead4c124SThinh Nguyen 	if (speed == USB_SPEED_UNKNOWN)
1270ead4c124SThinh Nguyen 		s = gadget->max_speed;
1271ead4c124SThinh Nguyen 	else
1272ead4c124SThinh Nguyen 		s = min(speed, gadget->max_speed);
1273ead4c124SThinh Nguyen 
1274ead4c124SThinh Nguyen 	if (s == USB_SPEED_SUPER_PLUS && gadget->ops->udc_set_ssp_rate)
1275ead4c124SThinh Nguyen 		gadget->ops->udc_set_ssp_rate(gadget, gadget->max_ssp_rate);
1276ead4c124SThinh Nguyen 	else if (gadget->ops->udc_set_speed)
1277ead4c124SThinh Nguyen 		gadget->ops->udc_set_speed(gadget, s);
127867fdfda4SFelipe Balbi }
127967fdfda4SFelipe Balbi 
128067fdfda4SFelipe Balbi /**
12817dc0c55eSAlan Stern  * usb_gadget_enable_async_callbacks - tell usb device controller to enable asynchronous callbacks
12827dc0c55eSAlan Stern  * @udc: The UDC which should enable async callbacks
12837dc0c55eSAlan Stern  *
12847dc0c55eSAlan Stern  * This routine is used when binding gadget drivers.  It undoes the effect
12857dc0c55eSAlan Stern  * of usb_gadget_disable_async_callbacks(); the UDC driver should enable IRQs
12867dc0c55eSAlan Stern  * (if necessary) and resume issuing callbacks.
12877dc0c55eSAlan Stern  *
12887dc0c55eSAlan Stern  * This routine will always be called in process context.
12897dc0c55eSAlan Stern  */
usb_gadget_enable_async_callbacks(struct usb_udc * udc)12907dc0c55eSAlan Stern static inline void usb_gadget_enable_async_callbacks(struct usb_udc *udc)
12917dc0c55eSAlan Stern {
12927dc0c55eSAlan Stern 	struct usb_gadget *gadget = udc->gadget;
12937dc0c55eSAlan Stern 
12947dc0c55eSAlan Stern 	if (gadget->ops->udc_async_callbacks)
12957dc0c55eSAlan Stern 		gadget->ops->udc_async_callbacks(gadget, true);
12967dc0c55eSAlan Stern }
12977dc0c55eSAlan Stern 
12987dc0c55eSAlan Stern /**
12997dc0c55eSAlan Stern  * usb_gadget_disable_async_callbacks - tell usb device controller to disable asynchronous callbacks
13007dc0c55eSAlan Stern  * @udc: The UDC which should disable async callbacks
13017dc0c55eSAlan Stern  *
13027dc0c55eSAlan Stern  * This routine is used when unbinding gadget drivers.  It prevents a race:
13037dc0c55eSAlan Stern  * The UDC driver doesn't know when the gadget driver's ->unbind callback
13047dc0c55eSAlan Stern  * runs, so unless it is told to disable asynchronous callbacks, it might
13057dc0c55eSAlan Stern  * issue a callback (such as ->disconnect) after the unbind has completed.
13067dc0c55eSAlan Stern  *
13077dc0c55eSAlan Stern  * After this function runs, the UDC driver must suppress all ->suspend,
13087dc0c55eSAlan Stern  * ->resume, ->disconnect, ->reset, and ->setup callbacks to the gadget driver
13097dc0c55eSAlan Stern  * until async callbacks are again enabled.  A simple-minded but effective
13107dc0c55eSAlan Stern  * way to accomplish this is to tell the UDC hardware not to generate any
13117dc0c55eSAlan Stern  * more IRQs.
13127dc0c55eSAlan Stern  *
13137dc0c55eSAlan Stern  * Request completion callbacks must still be issued.  However, it's okay
13147dc0c55eSAlan Stern  * to defer them until the request is cancelled, since the pull-up will be
13157dc0c55eSAlan Stern  * turned off during the time period when async callbacks are disabled.
13167dc0c55eSAlan Stern  *
13177dc0c55eSAlan Stern  * This routine will always be called in process context.
13187dc0c55eSAlan Stern  */
usb_gadget_disable_async_callbacks(struct usb_udc * udc)13197dc0c55eSAlan Stern static inline void usb_gadget_disable_async_callbacks(struct usb_udc *udc)
13207dc0c55eSAlan Stern {
13217dc0c55eSAlan Stern 	struct usb_gadget *gadget = udc->gadget;
13227dc0c55eSAlan Stern 
13237dc0c55eSAlan Stern 	if (gadget->ops->udc_async_callbacks)
13247dc0c55eSAlan Stern 		gadget->ops->udc_async_callbacks(gadget, false);
13257dc0c55eSAlan Stern }
13267dc0c55eSAlan Stern 
13277dc0c55eSAlan Stern /**
13282ccea03aSFelipe Balbi  * usb_udc_release - release the usb_udc struct
13292ccea03aSFelipe Balbi  * @dev: the dev member within usb_udc
13302ccea03aSFelipe Balbi  *
13312ccea03aSFelipe Balbi  * This is called by driver's core in order to free memory once the last
13322ccea03aSFelipe Balbi  * reference is released.
13332ccea03aSFelipe Balbi  */
usb_udc_release(struct device * dev)13342ccea03aSFelipe Balbi static void usb_udc_release(struct device *dev)
13352ccea03aSFelipe Balbi {
13362ccea03aSFelipe Balbi 	struct usb_udc *udc;
13372ccea03aSFelipe Balbi 
13382ccea03aSFelipe Balbi 	udc = container_of(dev, struct usb_udc, dev);
13392ccea03aSFelipe Balbi 	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
13402ccea03aSFelipe Balbi 	kfree(udc);
13412ccea03aSFelipe Balbi }
13422ccea03aSFelipe Balbi 
1343019f976eSSebastian Andrzej Siewior static const struct attribute_group *usb_udc_attr_groups[];
1344792bfcf7SFelipe Balbi 
usb_udc_nop_release(struct device * dev)1345792bfcf7SFelipe Balbi static void usb_udc_nop_release(struct device *dev)
1346792bfcf7SFelipe Balbi {
1347792bfcf7SFelipe Balbi 	dev_vdbg(dev, "%s\n", __func__);
1348792bfcf7SFelipe Balbi }
1349792bfcf7SFelipe Balbi 
13502ccea03aSFelipe Balbi /**
13513301c215SAlan Stern  * usb_initialize_gadget - initialize a gadget and its embedded struct device
1352792bfcf7SFelipe Balbi  * @parent: the parent device to this udc. Usually the controller driver's
1353792bfcf7SFelipe Balbi  * device.
13543301c215SAlan Stern  * @gadget: the gadget to be initialized.
1355792bfcf7SFelipe Balbi  * @release: a gadget release function.
13562ccea03aSFelipe Balbi  */
usb_initialize_gadget(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))13573301c215SAlan Stern void usb_initialize_gadget(struct device *parent, struct usb_gadget *gadget,
1358792bfcf7SFelipe Balbi 		void (*release)(struct device *dev))
13592ccea03aSFelipe Balbi {
13605702f753SFelipe Balbi 	INIT_WORK(&gadget->work, usb_gadget_state_work);
13612ed14320SFelipe Balbi 	gadget->dev.parent = parent;
1362f07bd56bSFelipe Balbi 
1363ddf47ccbSFelipe Balbi 	if (release)
1364792bfcf7SFelipe Balbi 		gadget->dev.release = release;
1365ddf47ccbSFelipe Balbi 	else
1366792bfcf7SFelipe Balbi 		gadget->dev.release = usb_udc_nop_release;
1367792bfcf7SFelipe Balbi 
1368afd7fd81SAlan Stern 	device_initialize(&gadget->dev);
1369fc274c1eSAlan Stern 	gadget->dev.bus = &gadget_bus_type;
13703301c215SAlan Stern }
13713301c215SAlan Stern EXPORT_SYMBOL_GPL(usb_initialize_gadget);
13723301c215SAlan Stern 
13733301c215SAlan Stern /**
13743301c215SAlan Stern  * usb_add_gadget - adds a new gadget to the udc class driver list
13753301c215SAlan Stern  * @gadget: the gadget to be added to the list.
13763301c215SAlan Stern  *
13773301c215SAlan Stern  * Returns zero on success, negative errno otherwise.
13783301c215SAlan Stern  * Does not do a final usb_put_gadget() if an error occurs.
13793301c215SAlan Stern  */
usb_add_gadget(struct usb_gadget * gadget)13803301c215SAlan Stern int usb_add_gadget(struct usb_gadget *gadget)
13813301c215SAlan Stern {
13823301c215SAlan Stern 	struct usb_udc		*udc;
13833301c215SAlan Stern 	int			ret = -ENOMEM;
1384afd7fd81SAlan Stern 
1385afd7fd81SAlan Stern 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1386afd7fd81SAlan Stern 	if (!udc)
13873301c215SAlan Stern 		goto error;
1388f07bd56bSFelipe Balbi 
13892ccea03aSFelipe Balbi 	device_initialize(&udc->dev);
13902ccea03aSFelipe Balbi 	udc->dev.release = usb_udc_release;
13918e991436SIvan Orlov 	udc->dev.class = &udc_class;
1392019f976eSSebastian Andrzej Siewior 	udc->dev.groups = usb_udc_attr_groups;
13933301c215SAlan Stern 	udc->dev.parent = gadget->dev.parent;
13943301c215SAlan Stern 	ret = dev_set_name(&udc->dev, "%s",
13953301c215SAlan Stern 			kobject_name(&gadget->dev.parent->kobj));
13962ccea03aSFelipe Balbi 	if (ret)
13977ae2c3c2SAlan Stern 		goto err_put_udc;
13987ae2c3c2SAlan Stern 
13992ccea03aSFelipe Balbi 	udc->gadget = gadget;
1400dfea9c94SPeter Chen 	gadget->udc = udc;
1401286d9975SBadhri Jagan Sridharan 	mutex_init(&udc->connect_lock);
14022ccea03aSFelipe Balbi 
140349d08cfcSThinh Nguyen 	udc->started = false;
140449d08cfcSThinh Nguyen 
14052ccea03aSFelipe Balbi 	mutex_lock(&udc_lock);
14062ccea03aSFelipe Balbi 	list_add_tail(&udc->list, &udc_list);
1407fc274c1eSAlan Stern 	mutex_unlock(&udc_lock);
140850966da8SBadhri Jagan Sridharan 	INIT_WORK(&udc->vbus_work, vbus_event_work);
14092ccea03aSFelipe Balbi 
14102ccea03aSFelipe Balbi 	ret = device_add(&udc->dev);
14112ccea03aSFelipe Balbi 	if (ret)
14127ae2c3c2SAlan Stern 		goto err_unlist_udc;
14132ccea03aSFelipe Balbi 
141449401f41SFelipe Balbi 	usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
1415628ef0d2SPeter Chen 	udc->vbus = true;
141649401f41SFelipe Balbi 
1417f9d76d15SAlan Stern 	ret = ida_alloc(&gadget_id_numbers, GFP_KERNEL);
1418f9d76d15SAlan Stern 	if (ret < 0)
1419f9d76d15SAlan Stern 		goto err_del_udc;
1420f9d76d15SAlan Stern 	gadget->id_number = ret;
1421f9d76d15SAlan Stern 	dev_set_name(&gadget->dev, "gadget.%d", ret);
1422f9d76d15SAlan Stern 
14236ebb449fSAlan Stern 	ret = device_add(&gadget->dev);
14246ebb449fSAlan Stern 	if (ret)
1425f9d76d15SAlan Stern 		goto err_free_id;
14266ebb449fSAlan Stern 
14270ef40f39SRoy Luo 	ret = sysfs_create_link(&udc->dev.kobj,
14280ef40f39SRoy Luo 				&gadget->dev.kobj, "gadget");
14290ef40f39SRoy Luo 	if (ret)
14300ef40f39SRoy Luo 		goto err_del_gadget;
14310ef40f39SRoy Luo 
14322ccea03aSFelipe Balbi 	return 0;
1433f07bd56bSFelipe Balbi 
14340ef40f39SRoy Luo  err_del_gadget:
14350ef40f39SRoy Luo 	device_del(&gadget->dev);
14360ef40f39SRoy Luo 
1437f9d76d15SAlan Stern  err_free_id:
1438f9d76d15SAlan Stern 	ida_free(&gadget_id_numbers, gadget->id_number);
1439f9d76d15SAlan Stern 
14407ae2c3c2SAlan Stern  err_del_udc:
144137d9453bSMarek Szyprowski 	flush_work(&gadget->work);
144217a1dc5eSPeter Chen 	device_del(&udc->dev);
144317a1dc5eSPeter Chen 
14447ae2c3c2SAlan Stern  err_unlist_udc:
1445fc274c1eSAlan Stern 	mutex_lock(&udc_lock);
14462ccea03aSFelipe Balbi 	list_del(&udc->list);
14472ccea03aSFelipe Balbi 	mutex_unlock(&udc_lock);
14482ccea03aSFelipe Balbi 
14497ae2c3c2SAlan Stern  err_put_udc:
14507ae2c3c2SAlan Stern 	put_device(&udc->dev);
14517bce401cSFelipe Balbi 
14523301c215SAlan Stern  error:
14533301c215SAlan Stern 	return ret;
14543301c215SAlan Stern }
14553301c215SAlan Stern EXPORT_SYMBOL_GPL(usb_add_gadget);
14563301c215SAlan Stern 
14573301c215SAlan Stern /**
14583301c215SAlan Stern  * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
14593301c215SAlan Stern  * @parent: the parent device to this udc. Usually the controller driver's
14603301c215SAlan Stern  * device.
14613301c215SAlan Stern  * @gadget: the gadget to be added to the list.
14623301c215SAlan Stern  * @release: a gadget release function.
14633301c215SAlan Stern  *
14643301c215SAlan Stern  * Returns zero on success, negative errno otherwise.
14653301c215SAlan Stern  * Calls the gadget release function in the latter case.
14663301c215SAlan Stern  */
usb_add_gadget_udc_release(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))14673301c215SAlan Stern int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
14683301c215SAlan Stern 		void (*release)(struct device *dev))
14693301c215SAlan Stern {
14703301c215SAlan Stern 	int	ret;
14713301c215SAlan Stern 
14723301c215SAlan Stern 	usb_initialize_gadget(parent, gadget, release);
14733301c215SAlan Stern 	ret = usb_add_gadget(gadget);
14743301c215SAlan Stern 	if (ret)
14753301c215SAlan Stern 		usb_put_gadget(gadget);
14762ccea03aSFelipe Balbi 	return ret;
14772ccea03aSFelipe Balbi }
1478792bfcf7SFelipe Balbi EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1479792bfcf7SFelipe Balbi 
14802ccea03aSFelipe Balbi /**
1481175f7121SMarek Szyprowski  * usb_get_gadget_udc_name - get the name of the first UDC controller
1482175f7121SMarek Szyprowski  * This functions returns the name of the first UDC controller in the system.
1483175f7121SMarek Szyprowski  * Please note that this interface is usefull only for legacy drivers which
1484175f7121SMarek Szyprowski  * assume that there is only one UDC controller in the system and they need to
1485175f7121SMarek Szyprowski  * get its name before initialization. There is no guarantee that the UDC
1486175f7121SMarek Szyprowski  * of the returned name will be still available, when gadget driver registers
1487175f7121SMarek Szyprowski  * itself.
1488175f7121SMarek Szyprowski  *
1489175f7121SMarek Szyprowski  * Returns pointer to string with UDC controller name on success, NULL
1490175f7121SMarek Szyprowski  * otherwise. Caller should kfree() returned string.
1491175f7121SMarek Szyprowski  */
usb_get_gadget_udc_name(void)1492175f7121SMarek Szyprowski char *usb_get_gadget_udc_name(void)
1493175f7121SMarek Szyprowski {
1494175f7121SMarek Szyprowski 	struct usb_udc *udc;
1495175f7121SMarek Szyprowski 	char *name = NULL;
1496175f7121SMarek Szyprowski 
1497175f7121SMarek Szyprowski 	/* For now we take the first available UDC */
1498175f7121SMarek Szyprowski 	mutex_lock(&udc_lock);
1499175f7121SMarek Szyprowski 	list_for_each_entry(udc, &udc_list, list) {
1500175f7121SMarek Szyprowski 		if (!udc->driver) {
1501175f7121SMarek Szyprowski 			name = kstrdup(udc->gadget->name, GFP_KERNEL);
1502175f7121SMarek Szyprowski 			break;
1503175f7121SMarek Szyprowski 		}
1504175f7121SMarek Szyprowski 	}
1505175f7121SMarek Szyprowski 	mutex_unlock(&udc_lock);
1506175f7121SMarek Szyprowski 	return name;
1507175f7121SMarek Szyprowski }
1508175f7121SMarek Szyprowski EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1509175f7121SMarek Szyprowski 
1510175f7121SMarek Szyprowski /**
15112ccea03aSFelipe Balbi  * usb_add_gadget_udc - adds a new gadget to the udc class driver list
15122ccea03aSFelipe Balbi  * @parent: the parent device to this udc. Usually the controller
15132ccea03aSFelipe Balbi  * driver's device.
15142ccea03aSFelipe Balbi  * @gadget: the gadget to be added to the list
15152ccea03aSFelipe Balbi  *
15162ccea03aSFelipe Balbi  * Returns zero on success, negative errno otherwise.
15172ccea03aSFelipe Balbi  */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)15182ccea03aSFelipe Balbi int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
15192ccea03aSFelipe Balbi {
1520792bfcf7SFelipe Balbi 	return usb_add_gadget_udc_release(parent, gadget, NULL);
15212ccea03aSFelipe Balbi }
15222ccea03aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
15232ccea03aSFelipe Balbi 
15242ccea03aSFelipe Balbi /**
1525d59f6d95SAlan Stern  * usb_del_gadget - deletes a gadget and unregisters its udc
1526d59f6d95SAlan Stern  * @gadget: the gadget to be deleted.
15272ccea03aSFelipe Balbi  *
1528d59f6d95SAlan Stern  * This will unbind @gadget, if it is bound.
15293301c215SAlan Stern  * It will not do a final usb_put_gadget().
15302ccea03aSFelipe Balbi  */
usb_del_gadget(struct usb_gadget * gadget)15313301c215SAlan Stern void usb_del_gadget(struct usb_gadget *gadget)
15322ccea03aSFelipe Balbi {
1533dfea9c94SPeter Chen 	struct usb_udc *udc = gadget->udc;
15342ccea03aSFelipe Balbi 
1535dfea9c94SPeter Chen 	if (!udc)
15362ccea03aSFelipe Balbi 		return;
15372ccea03aSFelipe Balbi 
15382ccea03aSFelipe Balbi 	dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
15392ccea03aSFelipe Balbi 
1540dfea9c94SPeter Chen 	mutex_lock(&udc_lock);
15412ccea03aSFelipe Balbi 	list_del(&udc->list);
1542855ed04aSRuslan Bilovol 	mutex_unlock(&udc_lock);
15432ccea03aSFelipe Balbi 
15442ccea03aSFelipe Balbi 	kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
15450ef40f39SRoy Luo 	sysfs_remove_link(&udc->dev.kobj, "gadget");
15463301c215SAlan Stern 	device_del(&gadget->dev);
1547399a45e5SRoy Luo 	flush_work(&gadget->work);
1548f9d76d15SAlan Stern 	ida_free(&gadget_id_numbers, gadget->id_number);
154950966da8SBadhri Jagan Sridharan 	cancel_work_sync(&udc->vbus_work);
15506ebb449fSAlan Stern 	device_unregister(&udc->dev);
15513301c215SAlan Stern }
15523301c215SAlan Stern EXPORT_SYMBOL_GPL(usb_del_gadget);
15533301c215SAlan Stern 
15543301c215SAlan Stern /**
1555d59f6d95SAlan Stern  * usb_del_gadget_udc - unregisters a gadget
1556d59f6d95SAlan Stern  * @gadget: the gadget to be unregistered.
15573301c215SAlan Stern  *
15583301c215SAlan Stern  * Calls usb_del_gadget() and does a final usb_put_gadget().
15593301c215SAlan Stern  */
usb_del_gadget_udc(struct usb_gadget * gadget)15603301c215SAlan Stern void usb_del_gadget_udc(struct usb_gadget *gadget)
15613301c215SAlan Stern {
15623301c215SAlan Stern 	usb_del_gadget(gadget);
15633301c215SAlan Stern 	usb_put_gadget(gadget);
15642ccea03aSFelipe Balbi }
15652ccea03aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
15662ccea03aSFelipe Balbi 
15672ccea03aSFelipe Balbi /* ------------------------------------------------------------------------- */
15682ccea03aSFelipe Balbi 
gadget_match_driver(struct device * dev,const struct device_driver * drv)1569d69d8048SGreg Kroah-Hartman static int gadget_match_driver(struct device *dev, const struct device_driver *drv)
15702ccea03aSFelipe Balbi {
1571fc274c1eSAlan Stern 	struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1572fc274c1eSAlan Stern 	struct usb_udc *udc = gadget->udc;
1573*5f5cc794SGreg Kroah-Hartman 	const struct usb_gadget_driver *driver = container_of(drv,
1574fc274c1eSAlan Stern 			struct usb_gadget_driver, driver);
15752ccea03aSFelipe Balbi 
1576fc274c1eSAlan Stern 	/* If the driver specifies a udc_name, it must match the UDC's name */
1577fc274c1eSAlan Stern 	if (driver->udc_name &&
1578fc274c1eSAlan Stern 			strcmp(driver->udc_name, dev_name(&udc->dev)) != 0)
1579fc274c1eSAlan Stern 		return 0;
15802ccea03aSFelipe Balbi 
1581fc274c1eSAlan Stern 	/* If the driver is already bound to a gadget, it doesn't match */
1582fc274c1eSAlan Stern 	if (driver->is_bound)
1583fc274c1eSAlan Stern 		return 0;
1584fc274c1eSAlan Stern 
1585fc274c1eSAlan Stern 	/* Otherwise any gadget driver matches any UDC */
1586fc274c1eSAlan Stern 	return 1;
1587fc274c1eSAlan Stern }
1588fc274c1eSAlan Stern 
gadget_bind_driver(struct device * dev)1589fc274c1eSAlan Stern static int gadget_bind_driver(struct device *dev)
1590fc274c1eSAlan Stern {
1591fc274c1eSAlan Stern 	struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1592fc274c1eSAlan Stern 	struct usb_udc *udc = gadget->udc;
1593fc274c1eSAlan Stern 	struct usb_gadget_driver *driver = container_of(dev->driver,
1594fc274c1eSAlan Stern 			struct usb_gadget_driver, driver);
1595fc274c1eSAlan Stern 	int ret = 0;
1596fc274c1eSAlan Stern 
1597fc274c1eSAlan Stern 	mutex_lock(&udc_lock);
1598fc274c1eSAlan Stern 	if (driver->is_bound) {
1599fc274c1eSAlan Stern 		mutex_unlock(&udc_lock);
1600fc274c1eSAlan Stern 		return -ENXIO;		/* Driver binds to only one gadget */
1601fc274c1eSAlan Stern 	}
1602fc274c1eSAlan Stern 	driver->is_bound = true;
16032ccea03aSFelipe Balbi 	udc->driver = driver;
1604fc274c1eSAlan Stern 	mutex_unlock(&udc_lock);
1605fc274c1eSAlan Stern 
1606fc274c1eSAlan Stern 	dev_dbg(&udc->dev, "binding gadget driver [%s]\n", driver->function);
16072ccea03aSFelipe Balbi 
160867fdfda4SFelipe Balbi 	usb_gadget_udc_set_speed(udc, driver->max_speed);
160967fdfda4SFelipe Balbi 
1610ffe0b335SSebastian Andrzej Siewior 	ret = driver->bind(udc->gadget, driver);
1611352c2dc8SSebastian Andrzej Siewior 	if (ret)
1612fc274c1eSAlan Stern 		goto err_bind;
1613fc274c1eSAlan Stern 
1614286d9975SBadhri Jagan Sridharan 	mutex_lock(&udc->connect_lock);
1615286d9975SBadhri Jagan Sridharan 	ret = usb_gadget_udc_start_locked(udc);
1616286d9975SBadhri Jagan Sridharan 	if (ret) {
1617286d9975SBadhri Jagan Sridharan 		mutex_unlock(&udc->connect_lock);
1618fc274c1eSAlan Stern 		goto err_start;
1619286d9975SBadhri Jagan Sridharan 	}
16207dc0c55eSAlan Stern 	usb_gadget_enable_async_callbacks(udc);
162150966da8SBadhri Jagan Sridharan 	udc->allow_connect = true;
16220ea39e03SKrishna Kurapati 	ret = usb_udc_connect_control_locked(udc);
16230ea39e03SKrishna Kurapati 	if (ret)
16240ea39e03SKrishna Kurapati 		goto err_connect_control;
16250ea39e03SKrishna Kurapati 
1626286d9975SBadhri Jagan Sridharan 	mutex_unlock(&udc->connect_lock);
1627352c2dc8SSebastian Andrzej Siewior 
16282ccea03aSFelipe Balbi 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
16292ccea03aSFelipe Balbi 	return 0;
1630fc274c1eSAlan Stern 
16310ea39e03SKrishna Kurapati  err_connect_control:
16320ea39e03SKrishna Kurapati 	udc->allow_connect = false;
16330ea39e03SKrishna Kurapati 	usb_gadget_disable_async_callbacks(udc);
16340ea39e03SKrishna Kurapati 	if (gadget->irq)
16350ea39e03SKrishna Kurapati 		synchronize_irq(gadget->irq);
16360ea39e03SKrishna Kurapati 	usb_gadget_udc_stop_locked(udc);
16370ea39e03SKrishna Kurapati 	mutex_unlock(&udc->connect_lock);
16380ea39e03SKrishna Kurapati 
1639fc274c1eSAlan Stern  err_start:
1640fc274c1eSAlan Stern 	driver->unbind(udc->gadget);
1641fc274c1eSAlan Stern 
1642fc274c1eSAlan Stern  err_bind:
1643f8cffc84SFabio Estevam 	if (ret != -EISNAM)
16442ccea03aSFelipe Balbi 		dev_err(&udc->dev, "failed to start %s: %d\n",
1645fc274c1eSAlan Stern 			driver->function, ret);
1646fc274c1eSAlan Stern 
16471016fc0cSAlan Stern 	mutex_lock(&udc_lock);
16482ccea03aSFelipe Balbi 	udc->driver = NULL;
1649fc274c1eSAlan Stern 	driver->is_bound = false;
1650fc274c1eSAlan Stern 	mutex_unlock(&udc_lock);
1651fc274c1eSAlan Stern 
16524c49a5f0SSebastian Andrzej Siewior 	return ret;
16534c49a5f0SSebastian Andrzej Siewior }
16544c49a5f0SSebastian Andrzej Siewior 
gadget_unbind_driver(struct device * dev)1655fc274c1eSAlan Stern static void gadget_unbind_driver(struct device *dev)
16564c49a5f0SSebastian Andrzej Siewior {
1657fc274c1eSAlan Stern 	struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1658fc274c1eSAlan Stern 	struct usb_udc *udc = gadget->udc;
1659fc274c1eSAlan Stern 	struct usb_gadget_driver *driver = udc->driver;
1660fc274c1eSAlan Stern 
1661fc274c1eSAlan Stern 	dev_dbg(&udc->dev, "unbinding gadget driver [%s]\n", driver->function);
1662fc274c1eSAlan Stern 
166350966da8SBadhri Jagan Sridharan 	udc->allow_connect = false;
166450966da8SBadhri Jagan Sridharan 	cancel_work_sync(&udc->vbus_work);
1665286d9975SBadhri Jagan Sridharan 	mutex_lock(&udc->connect_lock);
1666286d9975SBadhri Jagan Sridharan 	usb_gadget_disconnect_locked(gadget);
1667fc274c1eSAlan Stern 	usb_gadget_disable_async_callbacks(udc);
1668fc274c1eSAlan Stern 	if (gadget->irq)
1669fc274c1eSAlan Stern 		synchronize_irq(gadget->irq);
167065dadb2bSAlan Stern 	mutex_unlock(&udc->connect_lock);
167165dadb2bSAlan Stern 
1672fc274c1eSAlan Stern 	udc->driver->unbind(gadget);
167365dadb2bSAlan Stern 
167465dadb2bSAlan Stern 	mutex_lock(&udc->connect_lock);
1675286d9975SBadhri Jagan Sridharan 	usb_gadget_udc_stop_locked(udc);
1676286d9975SBadhri Jagan Sridharan 	mutex_unlock(&udc->connect_lock);
1677fc274c1eSAlan Stern 
16781016fc0cSAlan Stern 	mutex_lock(&udc_lock);
1679fc274c1eSAlan Stern 	driver->is_bound = false;
1680fc274c1eSAlan Stern 	udc->driver = NULL;
1681fc274c1eSAlan Stern 	mutex_unlock(&udc_lock);
168273ea73afSRoy Luo 
168373ea73afSRoy Luo 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1684fc274c1eSAlan Stern }
1685fc274c1eSAlan Stern 
1686fc274c1eSAlan Stern /* ------------------------------------------------------------------------- */
1687fc274c1eSAlan Stern 
usb_gadget_register_driver_owner(struct usb_gadget_driver * driver,struct module * owner,const char * mod_name)1688fc274c1eSAlan Stern int usb_gadget_register_driver_owner(struct usb_gadget_driver *driver,
1689fc274c1eSAlan Stern 		struct module *owner, const char *mod_name)
1690fc274c1eSAlan Stern {
1691fc274c1eSAlan Stern 	int ret;
16924c49a5f0SSebastian Andrzej Siewior 
16934c49a5f0SSebastian Andrzej Siewior 	if (!driver || !driver->bind || !driver->setup)
16944c49a5f0SSebastian Andrzej Siewior 		return -EINVAL;
16954c49a5f0SSebastian Andrzej Siewior 
1696fc274c1eSAlan Stern 	driver->driver.bus = &gadget_bus_type;
1697fc274c1eSAlan Stern 	driver->driver.owner = owner;
1698fc274c1eSAlan Stern 	driver->driver.mod_name = mod_name;
1699df915882SJohn Keeping 	driver->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
1700fc274c1eSAlan Stern 	ret = driver_register(&driver->driver);
1701fc274c1eSAlan Stern 	if (ret) {
1702fc274c1eSAlan Stern 		pr_warn("%s: driver registration failed: %d\n",
1703fc274c1eSAlan Stern 				driver->function, ret);
1704fc274c1eSAlan Stern 		return ret;
17052284b29dSRuslan Bilovol 	}
17064c49a5f0SSebastian Andrzej Siewior 
1707fc274c1eSAlan Stern 	mutex_lock(&udc_lock);
1708fc274c1eSAlan Stern 	if (!driver->is_bound) {
1709fc274c1eSAlan Stern 		if (driver->match_existing_only) {
1710fc274c1eSAlan Stern 			pr_warn("%s: couldn't find an available UDC or it's busy\n",
1711855ed04aSRuslan Bilovol 					driver->function);
1712fc274c1eSAlan Stern 			ret = -EBUSY;
1713fc274c1eSAlan Stern 		} else {
1714fc274c1eSAlan Stern 			pr_info("%s: couldn't find an available UDC\n",
1715fc274c1eSAlan Stern 					driver->function);
1716f1bddbb3SKrzysztof Opasiak 			ret = 0;
1717f1bddbb3SKrzysztof Opasiak 		}
1718d7c90d9fSColin Ian King 	}
1719fc274c1eSAlan Stern 	mutex_unlock(&udc_lock);
1720f1bddbb3SKrzysztof Opasiak 
17211d039a80SDejin Zheng 	if (ret)
1722fc274c1eSAlan Stern 		driver_unregister(&driver->driver);
17232ccea03aSFelipe Balbi 	return ret;
17242ccea03aSFelipe Balbi }
1725fc274c1eSAlan Stern EXPORT_SYMBOL_GPL(usb_gadget_register_driver_owner);
17262ccea03aSFelipe Balbi 
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)17272ccea03aSFelipe Balbi int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
17282ccea03aSFelipe Balbi {
17292ccea03aSFelipe Balbi 	if (!driver || !driver->unbind)
17302ccea03aSFelipe Balbi 		return -EINVAL;
17312ccea03aSFelipe Balbi 
1732fc274c1eSAlan Stern 	driver_unregister(&driver->driver);
1733fc274c1eSAlan Stern 	return 0;
17342ccea03aSFelipe Balbi }
17352ccea03aSFelipe Balbi EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
17362ccea03aSFelipe Balbi 
17372ccea03aSFelipe Balbi /* ------------------------------------------------------------------------- */
17382ccea03aSFelipe Balbi 
srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1739ca35910aSGreg Kroah-Hartman static ssize_t srp_store(struct device *dev,
17402ccea03aSFelipe Balbi 		struct device_attribute *attr, const char *buf, size_t n)
17412ccea03aSFelipe Balbi {
17421d91a962SFelipe Balbi 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
17432ccea03aSFelipe Balbi 
17442ccea03aSFelipe Balbi 	if (sysfs_streq(buf, "1"))
17452ccea03aSFelipe Balbi 		usb_gadget_wakeup(udc->gadget);
17462ccea03aSFelipe Balbi 
17472ccea03aSFelipe Balbi 	return n;
17482ccea03aSFelipe Balbi }
1749ca35910aSGreg Kroah-Hartman static DEVICE_ATTR_WO(srp);
17502ccea03aSFelipe Balbi 
soft_connect_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1751ca35910aSGreg Kroah-Hartman static ssize_t soft_connect_store(struct device *dev,
17522ccea03aSFelipe Balbi 		struct device_attribute *attr, const char *buf, size_t n)
17532ccea03aSFelipe Balbi {
1754865569baSFelipe Balbi 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
1755c28095bcSThinh Nguyen 	ssize_t			ret;
17562ccea03aSFelipe Balbi 
17571016fc0cSAlan Stern 	device_lock(&udc->gadget->dev);
1758bfa6b18cSFelipe Balbi 	if (!udc->driver) {
1759bfa6b18cSFelipe Balbi 		dev_err(dev, "soft-connect without a gadget driver\n");
1760c28095bcSThinh Nguyen 		ret = -EOPNOTSUPP;
1761c28095bcSThinh Nguyen 		goto out;
1762bfa6b18cSFelipe Balbi 	}
1763bfa6b18cSFelipe Balbi 
17642ccea03aSFelipe Balbi 	if (sysfs_streq(buf, "connect")) {
1765286d9975SBadhri Jagan Sridharan 		mutex_lock(&udc->connect_lock);
1766286d9975SBadhri Jagan Sridharan 		usb_gadget_udc_start_locked(udc);
1767286d9975SBadhri Jagan Sridharan 		usb_gadget_connect_locked(udc->gadget);
1768286d9975SBadhri Jagan Sridharan 		mutex_unlock(&udc->connect_lock);
17692ccea03aSFelipe Balbi 	} else if (sysfs_streq(buf, "disconnect")) {
1770286d9975SBadhri Jagan Sridharan 		mutex_lock(&udc->connect_lock);
1771286d9975SBadhri Jagan Sridharan 		usb_gadget_disconnect_locked(udc->gadget);
1772286d9975SBadhri Jagan Sridharan 		usb_gadget_udc_stop_locked(udc);
1773286d9975SBadhri Jagan Sridharan 		mutex_unlock(&udc->connect_lock);
17742ccea03aSFelipe Balbi 	} else {
17752ccea03aSFelipe Balbi 		dev_err(dev, "unsupported command '%s'\n", buf);
1776c28095bcSThinh Nguyen 		ret = -EINVAL;
1777c28095bcSThinh Nguyen 		goto out;
17782ccea03aSFelipe Balbi 	}
17792ccea03aSFelipe Balbi 
1780c28095bcSThinh Nguyen 	ret = n;
1781c28095bcSThinh Nguyen out:
17821016fc0cSAlan Stern 	device_unlock(&udc->gadget->dev);
1783c28095bcSThinh Nguyen 	return ret;
17842ccea03aSFelipe Balbi }
1785ca35910aSGreg Kroah-Hartman static DEVICE_ATTR_WO(soft_connect);
17862ccea03aSFelipe Balbi 
state_show(struct device * dev,struct device_attribute * attr,char * buf)1787ce26bd23SGreg Kroah-Hartman static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1788ce26bd23SGreg Kroah-Hartman 			  char *buf)
178949401f41SFelipe Balbi {
179049401f41SFelipe Balbi 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
179149401f41SFelipe Balbi 	struct usb_gadget	*gadget = udc->gadget;
179249401f41SFelipe Balbi 
179349401f41SFelipe Balbi 	return sprintf(buf, "%s\n", usb_state_string(gadget->state));
179449401f41SFelipe Balbi }
1795ce26bd23SGreg Kroah-Hartman static DEVICE_ATTR_RO(state);
179649401f41SFelipe Balbi 
function_show(struct device * dev,struct device_attribute * attr,char * buf)179710416568SFelipe Balbi static ssize_t function_show(struct device *dev, struct device_attribute *attr,
179810416568SFelipe Balbi 			     char *buf)
179910416568SFelipe Balbi {
180010416568SFelipe Balbi 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
18011016fc0cSAlan Stern 	struct usb_gadget_driver *drv;
18021016fc0cSAlan Stern 	int			rc = 0;
180310416568SFelipe Balbi 
18041016fc0cSAlan Stern 	mutex_lock(&udc_lock);
18051016fc0cSAlan Stern 	drv = udc->driver;
18061016fc0cSAlan Stern 	if (drv && drv->function)
18071016fc0cSAlan Stern 		rc = scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
18081016fc0cSAlan Stern 	mutex_unlock(&udc_lock);
18091016fc0cSAlan Stern 	return rc;
181010416568SFelipe Balbi }
181110416568SFelipe Balbi static DEVICE_ATTR_RO(function);
181210416568SFelipe Balbi 
1813d327ab5bSMichal Nazarewicz #define USB_UDC_SPEED_ATTR(name, param)					\
1814ce26bd23SGreg Kroah-Hartman ssize_t name##_show(struct device *dev,					\
1815d327ab5bSMichal Nazarewicz 		struct device_attribute *attr, char *buf)		\
1816d327ab5bSMichal Nazarewicz {									\
1817d327ab5bSMichal Nazarewicz 	struct usb_udc *udc = container_of(dev, struct usb_udc, dev);	\
18183589cce2SJaejoong Kim 	return scnprintf(buf, PAGE_SIZE, "%s\n",			\
1819d327ab5bSMichal Nazarewicz 			usb_speed_string(udc->gadget->param));		\
1820d327ab5bSMichal Nazarewicz }									\
1821ce26bd23SGreg Kroah-Hartman static DEVICE_ATTR_RO(name)
1822d327ab5bSMichal Nazarewicz 
1823d327ab5bSMichal Nazarewicz static USB_UDC_SPEED_ATTR(current_speed, speed);
1824d327ab5bSMichal Nazarewicz static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1825d327ab5bSMichal Nazarewicz 
18262ccea03aSFelipe Balbi #define USB_UDC_ATTR(name)					\
1827ce26bd23SGreg Kroah-Hartman ssize_t name##_show(struct device *dev,				\
18282ccea03aSFelipe Balbi 		struct device_attribute *attr, char *buf)	\
18292ccea03aSFelipe Balbi {								\
1830019f976eSSebastian Andrzej Siewior 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev); \
18312ccea03aSFelipe Balbi 	struct usb_gadget	*gadget = udc->gadget;		\
18322ccea03aSFelipe Balbi 								\
18333589cce2SJaejoong Kim 	return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name);	\
18342ccea03aSFelipe Balbi }								\
1835ce26bd23SGreg Kroah-Hartman static DEVICE_ATTR_RO(name)
18362ccea03aSFelipe Balbi 
18372ccea03aSFelipe Balbi static USB_UDC_ATTR(is_otg);
18382ccea03aSFelipe Balbi static USB_UDC_ATTR(is_a_peripheral);
18392ccea03aSFelipe Balbi static USB_UDC_ATTR(b_hnp_enable);
18402ccea03aSFelipe Balbi static USB_UDC_ATTR(a_hnp_support);
18412ccea03aSFelipe Balbi static USB_UDC_ATTR(a_alt_hnp_support);
18423f6dd4feSPeter Chen static USB_UDC_ATTR(is_selfpowered);
18432ccea03aSFelipe Balbi 
18442ccea03aSFelipe Balbi static struct attribute *usb_udc_attrs[] = {
18452ccea03aSFelipe Balbi 	&dev_attr_srp.attr,
18462ccea03aSFelipe Balbi 	&dev_attr_soft_connect.attr,
184749401f41SFelipe Balbi 	&dev_attr_state.attr,
184810416568SFelipe Balbi 	&dev_attr_function.attr,
1849d327ab5bSMichal Nazarewicz 	&dev_attr_current_speed.attr,
1850d327ab5bSMichal Nazarewicz 	&dev_attr_maximum_speed.attr,
18512ccea03aSFelipe Balbi 
18522ccea03aSFelipe Balbi 	&dev_attr_is_otg.attr,
18532ccea03aSFelipe Balbi 	&dev_attr_is_a_peripheral.attr,
18542ccea03aSFelipe Balbi 	&dev_attr_b_hnp_enable.attr,
18552ccea03aSFelipe Balbi 	&dev_attr_a_hnp_support.attr,
18562ccea03aSFelipe Balbi 	&dev_attr_a_alt_hnp_support.attr,
18573f6dd4feSPeter Chen 	&dev_attr_is_selfpowered.attr,
18582ccea03aSFelipe Balbi 	NULL,
18592ccea03aSFelipe Balbi };
18602ccea03aSFelipe Balbi 
18612ccea03aSFelipe Balbi static const struct attribute_group usb_udc_attr_group = {
18622ccea03aSFelipe Balbi 	.attrs = usb_udc_attrs,
18632ccea03aSFelipe Balbi };
18642ccea03aSFelipe Balbi 
18652ccea03aSFelipe Balbi static const struct attribute_group *usb_udc_attr_groups[] = {
18662ccea03aSFelipe Balbi 	&usb_udc_attr_group,
18672ccea03aSFelipe Balbi 	NULL,
18682ccea03aSFelipe Balbi };
18692ccea03aSFelipe Balbi 
usb_udc_uevent(const struct device * dev,struct kobj_uevent_env * env)187023680f0bSGreg Kroah-Hartman static int usb_udc_uevent(const struct device *dev, struct kobj_uevent_env *env)
18712ccea03aSFelipe Balbi {
187223680f0bSGreg Kroah-Hartman 	const struct usb_udc	*udc = container_of(dev, struct usb_udc, dev);
18732ccea03aSFelipe Balbi 	int			ret;
18742ccea03aSFelipe Balbi 
18752ccea03aSFelipe Balbi 	ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
18762ccea03aSFelipe Balbi 	if (ret) {
18772ccea03aSFelipe Balbi 		dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
18782ccea03aSFelipe Balbi 		return ret;
18792ccea03aSFelipe Balbi 	}
18802ccea03aSFelipe Balbi 
18812191c008SAlan Stern 	mutex_lock(&udc_lock);
18822191c008SAlan Stern 	if (udc->driver)
18832ccea03aSFelipe Balbi 		ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
18842ccea03aSFelipe Balbi 				udc->driver->function);
18852191c008SAlan Stern 	mutex_unlock(&udc_lock);
18862ccea03aSFelipe Balbi 	if (ret) {
18872ccea03aSFelipe Balbi 		dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
18882ccea03aSFelipe Balbi 		return ret;
18892ccea03aSFelipe Balbi 	}
18902ccea03aSFelipe Balbi 
18912ccea03aSFelipe Balbi 	return 0;
18922ccea03aSFelipe Balbi }
18932ccea03aSFelipe Balbi 
18948e991436SIvan Orlov static const struct class udc_class = {
18958e991436SIvan Orlov 	.name		= "udc",
18968e991436SIvan Orlov 	.dev_uevent	= usb_udc_uevent,
18978e991436SIvan Orlov };
18988e991436SIvan Orlov 
18999d11b134SGreg Kroah-Hartman static const struct bus_type gadget_bus_type = {
1900fc274c1eSAlan Stern 	.name = "gadget",
1901fc274c1eSAlan Stern 	.probe = gadget_bind_driver,
1902fc274c1eSAlan Stern 	.remove = gadget_unbind_driver,
1903fc274c1eSAlan Stern 	.match = gadget_match_driver,
1904fc274c1eSAlan Stern };
1905fc274c1eSAlan Stern 
usb_udc_init(void)19062ccea03aSFelipe Balbi static int __init usb_udc_init(void)
19072ccea03aSFelipe Balbi {
1908fc274c1eSAlan Stern 	int rc;
1909fc274c1eSAlan Stern 
19108e991436SIvan Orlov 	rc = class_register(&udc_class);
19118e991436SIvan Orlov 	if (rc)
19128e991436SIvan Orlov 		return rc;
1913fc274c1eSAlan Stern 
1914fc274c1eSAlan Stern 	rc = bus_register(&gadget_bus_type);
1915fc274c1eSAlan Stern 	if (rc)
19168e991436SIvan Orlov 		class_unregister(&udc_class);
1917fc274c1eSAlan Stern 	return rc;
19182ccea03aSFelipe Balbi }
19192ccea03aSFelipe Balbi subsys_initcall(usb_udc_init);
19202ccea03aSFelipe Balbi 
usb_udc_exit(void)19212ccea03aSFelipe Balbi static void __exit usb_udc_exit(void)
19222ccea03aSFelipe Balbi {
1923fc274c1eSAlan Stern 	bus_unregister(&gadget_bus_type);
19248e991436SIvan Orlov 	class_unregister(&udc_class);
19252ccea03aSFelipe Balbi }
19262ccea03aSFelipe Balbi module_exit(usb_udc_exit);
19272ccea03aSFelipe Balbi 
19282ccea03aSFelipe Balbi MODULE_DESCRIPTION("UDC Framework");
19292ccea03aSFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
19302ccea03aSFelipe Balbi MODULE_LICENSE("GPL v2");
1931