xref: /rk3399_rockchip-uboot/include/linux/usb/gadget.h (revision 2d48aa69bd2e0164a22b253733564701ed3381a1)
123cd1385SRemy Bohmer /*
223cd1385SRemy Bohmer  * <linux/usb/gadget.h>
323cd1385SRemy Bohmer  *
423cd1385SRemy Bohmer  * We call the USB code inside a Linux-based peripheral device a "gadget"
523cd1385SRemy Bohmer  * driver, except for the hardware-specific bus glue.  One USB host can
623cd1385SRemy Bohmer  * master many USB gadgets, but the gadgets are only slaved to one host.
723cd1385SRemy Bohmer  *
823cd1385SRemy Bohmer  *
923cd1385SRemy Bohmer  * (C) Copyright 2002-2004 by David Brownell
1023cd1385SRemy Bohmer  * All Rights Reserved.
1123cd1385SRemy Bohmer  *
1223cd1385SRemy Bohmer  * This software is licensed under the GNU GPL version 2.
1323cd1385SRemy Bohmer  *
1423cd1385SRemy Bohmer  * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and
1523cd1385SRemy Bohmer  *                      Remy Bohmer <linux@bohmer.net>
1623cd1385SRemy Bohmer  */
1723cd1385SRemy Bohmer 
1823cd1385SRemy Bohmer #ifndef __LINUX_USB_GADGET_H
1923cd1385SRemy Bohmer #define __LINUX_USB_GADGET_H
2023cd1385SRemy Bohmer 
2169d6cbe7SLukasz Majewski #include <errno.h>
220c06db59SHeiko Schocher #include <linux/compat.h>
2323cd1385SRemy Bohmer #include <linux/list.h>
2423cd1385SRemy Bohmer 
2523cd1385SRemy Bohmer struct usb_ep;
2623cd1385SRemy Bohmer 
2723cd1385SRemy Bohmer /**
2823cd1385SRemy Bohmer  * struct usb_request - describes one i/o request
2923cd1385SRemy Bohmer  * @buf: Buffer used for data.  Always provide this; some controllers
3023cd1385SRemy Bohmer  *	only use PIO, or don't use DMA for some endpoints.
3123cd1385SRemy Bohmer  * @dma: DMA address corresponding to 'buf'.  If you don't set this
3223cd1385SRemy Bohmer  *	field, and the usb controller needs one, it is responsible
3323cd1385SRemy Bohmer  *	for mapping and unmapping the buffer.
34747a0a5bSKishon Vijay Abraham I  * @stream_id: The stream id, when USB3.0 bulk streams are being used
3523cd1385SRemy Bohmer  * @length: Length of that data
3623cd1385SRemy Bohmer  * @no_interrupt: If true, hints that no completion irq is needed.
3723cd1385SRemy Bohmer  *	Helpful sometimes with deep request queues that are handled
3823cd1385SRemy Bohmer  *	directly by DMA controllers.
3923cd1385SRemy Bohmer  * @zero: If true, when writing data, makes the last packet be "short"
4023cd1385SRemy Bohmer  *     by adding a zero length packet as needed;
4123cd1385SRemy Bohmer  * @short_not_ok: When reading data, makes short packets be
4223cd1385SRemy Bohmer  *     treated as errors (queue stops advancing till cleanup).
4323cd1385SRemy Bohmer  * @complete: Function called when request completes, so this request and
4423cd1385SRemy Bohmer  *	its buffer may be re-used.
4523cd1385SRemy Bohmer  *	Reads terminate with a short packet, or when the buffer fills,
4623cd1385SRemy Bohmer  *	whichever comes first.  When writes terminate, some data bytes
4723cd1385SRemy Bohmer  *	will usually still be in flight (often in a hardware fifo).
4823cd1385SRemy Bohmer  *	Errors (for reads or writes) stop the queue from advancing
4923cd1385SRemy Bohmer  *	until the completion function returns, so that any transfers
5023cd1385SRemy Bohmer  *	invalidated by the error may first be dequeued.
5123cd1385SRemy Bohmer  * @context: For use by the completion callback
5223cd1385SRemy Bohmer  * @list: For use by the gadget driver.
5323cd1385SRemy Bohmer  * @status: Reports completion code, zero or a negative errno.
5423cd1385SRemy Bohmer  *	Normally, faults block the transfer queue from advancing until
5523cd1385SRemy Bohmer  *	the completion callback returns.
5623cd1385SRemy Bohmer  *	Code "-ESHUTDOWN" indicates completion caused by device disconnect,
5723cd1385SRemy Bohmer  *	or when the driver disabled the endpoint.
5823cd1385SRemy Bohmer  * @actual: Reports bytes transferred to/from the buffer.  For reads (OUT
5923cd1385SRemy Bohmer  *	transfers) this may be less than the requested length.  If the
6023cd1385SRemy Bohmer  *	short_not_ok flag is set, short reads are treated as errors
6123cd1385SRemy Bohmer  *	even when status otherwise indicates successful completion.
6223cd1385SRemy Bohmer  *	Note that for writes (IN transfers) some data bytes may still
6323cd1385SRemy Bohmer  *	reside in a device-side FIFO when the request is reported as
6423cd1385SRemy Bohmer  *	complete.
6523cd1385SRemy Bohmer  *
6623cd1385SRemy Bohmer  * These are allocated/freed through the endpoint they're used with.  The
6723cd1385SRemy Bohmer  * hardware's driver can add extra per-request data to the memory it returns,
6823cd1385SRemy Bohmer  * which often avoids separate memory allocations (potential failures),
6923cd1385SRemy Bohmer  * later when the request is queued.
7023cd1385SRemy Bohmer  *
7123cd1385SRemy Bohmer  * Request flags affect request handling, such as whether a zero length
7223cd1385SRemy Bohmer  * packet is written (the "zero" flag), whether a short read should be
7323cd1385SRemy Bohmer  * treated as an error (blocking request queue advance, the "short_not_ok"
7423cd1385SRemy Bohmer  * flag), or hinting that an interrupt is not required (the "no_interrupt"
7523cd1385SRemy Bohmer  * flag, for use with deep request queues).
7623cd1385SRemy Bohmer  *
7723cd1385SRemy Bohmer  * Bulk endpoints can use any size buffers, and can also be used for interrupt
7823cd1385SRemy Bohmer  * transfers. interrupt-only endpoints can be much less functional.
796142e0aeSVitaly Kuzmichev  *
806142e0aeSVitaly Kuzmichev  * NOTE:  this is analagous to 'struct urb' on the host side, except that
816142e0aeSVitaly Kuzmichev  * it's thinner and promotes more pre-allocation.
8223cd1385SRemy Bohmer  */
8323cd1385SRemy Bohmer 
8423cd1385SRemy Bohmer struct usb_request {
8523cd1385SRemy Bohmer 	void			*buf;
8623cd1385SRemy Bohmer 	unsigned		length;
8723cd1385SRemy Bohmer 	dma_addr_t		dma;
8823cd1385SRemy Bohmer 
89747a0a5bSKishon Vijay Abraham I 	unsigned		stream_id:16;
9023cd1385SRemy Bohmer 	unsigned		no_interrupt:1;
9123cd1385SRemy Bohmer 	unsigned		zero:1;
9223cd1385SRemy Bohmer 	unsigned		short_not_ok:1;
9323cd1385SRemy Bohmer 
9423cd1385SRemy Bohmer 	void			(*complete)(struct usb_ep *ep,
9523cd1385SRemy Bohmer 					struct usb_request *req);
9623cd1385SRemy Bohmer 	void			*context;
9723cd1385SRemy Bohmer 	struct list_head	list;
9823cd1385SRemy Bohmer 
9923cd1385SRemy Bohmer 	int			status;
10023cd1385SRemy Bohmer 	unsigned		actual;
10123cd1385SRemy Bohmer };
10223cd1385SRemy Bohmer 
10323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
10423cd1385SRemy Bohmer 
10523cd1385SRemy Bohmer /* endpoint-specific parts of the api to the usb controller hardware.
10623cd1385SRemy Bohmer  * unlike the urb model, (de)multiplexing layers are not required.
10723cd1385SRemy Bohmer  * (so this api could slash overhead if used on the host side...)
10823cd1385SRemy Bohmer  *
10923cd1385SRemy Bohmer  * note that device side usb controllers commonly differ in how many
11023cd1385SRemy Bohmer  * endpoints they support, as well as their capabilities.
11123cd1385SRemy Bohmer  */
11223cd1385SRemy Bohmer struct usb_ep_ops {
11323cd1385SRemy Bohmer 	int (*enable) (struct usb_ep *ep,
11423cd1385SRemy Bohmer 		const struct usb_endpoint_descriptor *desc);
11523cd1385SRemy Bohmer 	int (*disable) (struct usb_ep *ep);
11623cd1385SRemy Bohmer 
11723cd1385SRemy Bohmer 	struct usb_request *(*alloc_request) (struct usb_ep *ep,
11823cd1385SRemy Bohmer 		gfp_t gfp_flags);
11923cd1385SRemy Bohmer 	void (*free_request) (struct usb_ep *ep, struct usb_request *req);
12023cd1385SRemy Bohmer 
12123cd1385SRemy Bohmer 	int (*queue) (struct usb_ep *ep, struct usb_request *req,
12223cd1385SRemy Bohmer 		gfp_t gfp_flags);
12323cd1385SRemy Bohmer 	int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
12423cd1385SRemy Bohmer 
12523cd1385SRemy Bohmer 	int (*set_halt) (struct usb_ep *ep, int value);
126747a0a5bSKishon Vijay Abraham I 	int (*set_wedge)(struct usb_ep *ep);
12723cd1385SRemy Bohmer 	int (*fifo_status) (struct usb_ep *ep);
12823cd1385SRemy Bohmer 	void (*fifo_flush) (struct usb_ep *ep);
12923cd1385SRemy Bohmer };
13023cd1385SRemy Bohmer 
13123cd1385SRemy Bohmer /**
13223cd1385SRemy Bohmer  * struct usb_ep - device side representation of USB endpoint
13323cd1385SRemy Bohmer  * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
13423cd1385SRemy Bohmer  * @ops: Function pointers used to access hardware-specific operations.
13523cd1385SRemy Bohmer  * @ep_list:the gadget's ep_list holds all of its endpoints
13623cd1385SRemy Bohmer  * @maxpacket:The maximum packet size used on this endpoint.  The initial
13723cd1385SRemy Bohmer  *	value can sometimes be reduced (hardware allowing), according to
13823cd1385SRemy Bohmer  *      the endpoint descriptor used to configure the endpoint.
139747a0a5bSKishon Vijay Abraham I  * @maxpacket_limit:The maximum packet size value which can be handled by this
140747a0a5bSKishon Vijay Abraham I  *	endpoint. It's set once by UDC driver when endpoint is initialized, and
141747a0a5bSKishon Vijay Abraham I  *	should not be changed. Should not be confused with maxpacket.
142747a0a5bSKishon Vijay Abraham I  * @max_streams: The maximum number of streams supported
143747a0a5bSKishon Vijay Abraham I  * 	by this EP (0 - 16, actual number is 2^n)
144747a0a5bSKishon Vijay Abraham I  * @maxburst: the maximum number of bursts supported by this EP (for usb3)
14523cd1385SRemy Bohmer  * @driver_data:for use by the gadget driver.  all other fields are
14623cd1385SRemy Bohmer  *	read-only to gadget drivers.
147747a0a5bSKishon Vijay Abraham I  * @desc: endpoint descriptor.  This pointer is set before the endpoint is
148747a0a5bSKishon Vijay Abraham I  * 	enabled and remains valid until the endpoint is disabled.
149747a0a5bSKishon Vijay Abraham I  * @comp_desc: In case of SuperSpeed support, this is the endpoint companion
150747a0a5bSKishon Vijay Abraham I  * 	descriptor that is used to configure the endpoint
15123cd1385SRemy Bohmer  *
15223cd1385SRemy Bohmer  * the bus controller driver lists all the general purpose endpoints in
15323cd1385SRemy Bohmer  * gadget->ep_list.  the control endpoint (gadget->ep0) is not in that list,
15423cd1385SRemy Bohmer  * and is accessed only in response to a driver setup() callback.
15523cd1385SRemy Bohmer  */
15623cd1385SRemy Bohmer struct usb_ep {
15723cd1385SRemy Bohmer 	void			*driver_data;
15823cd1385SRemy Bohmer 	const char		*name;
15923cd1385SRemy Bohmer 	const struct usb_ep_ops	*ops;
16023cd1385SRemy Bohmer 	struct list_head	ep_list;
16123cd1385SRemy Bohmer 	unsigned		maxpacket:16;
162747a0a5bSKishon Vijay Abraham I 	unsigned		maxpacket_limit:16;
163747a0a5bSKishon Vijay Abraham I 	unsigned		max_streams:16;
164747a0a5bSKishon Vijay Abraham I 	unsigned		maxburst:5;
165747a0a5bSKishon Vijay Abraham I 	const struct usb_endpoint_descriptor	*desc;
166747a0a5bSKishon Vijay Abraham I 	const struct usb_ss_ep_comp_descriptor	*comp_desc;
16723cd1385SRemy Bohmer };
16823cd1385SRemy Bohmer 
16923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
17023cd1385SRemy Bohmer 
17123cd1385SRemy Bohmer /**
172747a0a5bSKishon Vijay Abraham I  * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
173747a0a5bSKishon Vijay Abraham I  * @ep:the endpoint being configured
174747a0a5bSKishon Vijay Abraham I  * @maxpacket_limit:value of maximum packet size limit
175747a0a5bSKishon Vijay Abraham I  *
176747a0a5bSKishon Vijay Abraham I  * This function shoud be used only in UDC drivers to initialize endpoint
177747a0a5bSKishon Vijay Abraham I  * (usually in probe function).
178747a0a5bSKishon Vijay Abraham I  */
179747a0a5bSKishon Vijay Abraham I static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
180747a0a5bSKishon Vijay Abraham I 					      unsigned maxpacket_limit)
181747a0a5bSKishon Vijay Abraham I {
182747a0a5bSKishon Vijay Abraham I 	ep->maxpacket_limit = maxpacket_limit;
183747a0a5bSKishon Vijay Abraham I 	ep->maxpacket = maxpacket_limit;
184747a0a5bSKishon Vijay Abraham I }
185747a0a5bSKishon Vijay Abraham I 
186747a0a5bSKishon Vijay Abraham I /**
18723cd1385SRemy Bohmer  * usb_ep_enable - configure endpoint, making it usable
18823cd1385SRemy Bohmer  * @ep:the endpoint being configured.  may not be the endpoint named "ep0".
18923cd1385SRemy Bohmer  *	drivers discover endpoints through the ep_list of a usb_gadget.
19023cd1385SRemy Bohmer  * @desc:descriptor for desired behavior.  caller guarantees this pointer
19123cd1385SRemy Bohmer  *	remains valid until the endpoint is disabled; the data byte order
19223cd1385SRemy Bohmer  *	is little-endian (usb-standard).
19323cd1385SRemy Bohmer  *
19423cd1385SRemy Bohmer  * when configurations are set, or when interface settings change, the driver
19523cd1385SRemy Bohmer  * will enable or disable the relevant endpoints.  while it is enabled, an
19623cd1385SRemy Bohmer  * endpoint may be used for i/o until the driver receives a disconnect() from
19723cd1385SRemy Bohmer  * the host or until the endpoint is disabled.
19823cd1385SRemy Bohmer  *
19923cd1385SRemy Bohmer  * the ep0 implementation (which calls this routine) must ensure that the
20023cd1385SRemy Bohmer  * hardware capabilities of each endpoint match the descriptor provided
20123cd1385SRemy Bohmer  * for it.  for example, an endpoint named "ep2in-bulk" would be usable
20223cd1385SRemy Bohmer  * for interrupt transfers as well as bulk, but it likely couldn't be used
20323cd1385SRemy Bohmer  * for iso transfers or for endpoint 14.  some endpoints are fully
20423cd1385SRemy Bohmer  * configurable, with more generic names like "ep-a".  (remember that for
20523cd1385SRemy Bohmer  * USB, "in" means "towards the USB master".)
20623cd1385SRemy Bohmer  *
20723cd1385SRemy Bohmer  * returns zero, or a negative error code.
20823cd1385SRemy Bohmer  */
2096142e0aeSVitaly Kuzmichev static inline int usb_ep_enable(struct usb_ep *ep,
2106142e0aeSVitaly Kuzmichev 				const struct usb_endpoint_descriptor *desc)
21123cd1385SRemy Bohmer {
21223cd1385SRemy Bohmer 	return ep->ops->enable(ep, desc);
21323cd1385SRemy Bohmer }
21423cd1385SRemy Bohmer 
21523cd1385SRemy Bohmer /**
21623cd1385SRemy Bohmer  * usb_ep_disable - endpoint is no longer usable
21723cd1385SRemy Bohmer  * @ep:the endpoint being unconfigured.  may not be the endpoint named "ep0".
21823cd1385SRemy Bohmer  *
21923cd1385SRemy Bohmer  * no other task may be using this endpoint when this is called.
22023cd1385SRemy Bohmer  * any pending and uncompleted requests will complete with status
22123cd1385SRemy Bohmer  * indicating disconnect (-ESHUTDOWN) before this call returns.
22223cd1385SRemy Bohmer  * gadget drivers must call usb_ep_enable() again before queueing
22323cd1385SRemy Bohmer  * requests to the endpoint.
22423cd1385SRemy Bohmer  *
22523cd1385SRemy Bohmer  * returns zero, or a negative error code.
22623cd1385SRemy Bohmer  */
2276142e0aeSVitaly Kuzmichev static inline int usb_ep_disable(struct usb_ep *ep)
22823cd1385SRemy Bohmer {
22923cd1385SRemy Bohmer 	return ep->ops->disable(ep);
23023cd1385SRemy Bohmer }
23123cd1385SRemy Bohmer 
23223cd1385SRemy Bohmer /**
23323cd1385SRemy Bohmer  * usb_ep_alloc_request - allocate a request object to use with this endpoint
23423cd1385SRemy Bohmer  * @ep:the endpoint to be used with with the request
23523cd1385SRemy Bohmer  * @gfp_flags:GFP_* flags to use
23623cd1385SRemy Bohmer  *
23723cd1385SRemy Bohmer  * Request objects must be allocated with this call, since they normally
23823cd1385SRemy Bohmer  * need controller-specific setup and may even need endpoint-specific
23923cd1385SRemy Bohmer  * resources such as allocation of DMA descriptors.
24023cd1385SRemy Bohmer  * Requests may be submitted with usb_ep_queue(), and receive a single
24123cd1385SRemy Bohmer  * completion callback.  Free requests with usb_ep_free_request(), when
24223cd1385SRemy Bohmer  * they are no longer needed.
24323cd1385SRemy Bohmer  *
24423cd1385SRemy Bohmer  * Returns the request, or null if one could not be allocated.
24523cd1385SRemy Bohmer  */
2466142e0aeSVitaly Kuzmichev static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
2476142e0aeSVitaly Kuzmichev 						       gfp_t gfp_flags)
24823cd1385SRemy Bohmer {
24923cd1385SRemy Bohmer 	return ep->ops->alloc_request(ep, gfp_flags);
25023cd1385SRemy Bohmer }
25123cd1385SRemy Bohmer 
25223cd1385SRemy Bohmer /**
25323cd1385SRemy Bohmer  * usb_ep_free_request - frees a request object
25423cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
25523cd1385SRemy Bohmer  * @req:the request being freed
25623cd1385SRemy Bohmer  *
25723cd1385SRemy Bohmer  * Reverses the effect of usb_ep_alloc_request().
25823cd1385SRemy Bohmer  * Caller guarantees the request is not queued, and that it will
25923cd1385SRemy Bohmer  * no longer be requeued (or otherwise used).
26023cd1385SRemy Bohmer  */
2616142e0aeSVitaly Kuzmichev static inline void usb_ep_free_request(struct usb_ep *ep,
2626142e0aeSVitaly Kuzmichev 				       struct usb_request *req)
26323cd1385SRemy Bohmer {
26423cd1385SRemy Bohmer 	ep->ops->free_request(ep, req);
26523cd1385SRemy Bohmer }
26623cd1385SRemy Bohmer 
26723cd1385SRemy Bohmer /**
26823cd1385SRemy Bohmer  * usb_ep_queue - queues (submits) an I/O request to an endpoint.
26923cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
27023cd1385SRemy Bohmer  * @req:the request being submitted
27123cd1385SRemy Bohmer  * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
27223cd1385SRemy Bohmer  *	pre-allocate all necessary memory with the request.
27323cd1385SRemy Bohmer  *
27423cd1385SRemy Bohmer  * This tells the device controller to perform the specified request through
27523cd1385SRemy Bohmer  * that endpoint (reading or writing a buffer).  When the request completes,
27623cd1385SRemy Bohmer  * including being canceled by usb_ep_dequeue(), the request's completion
27723cd1385SRemy Bohmer  * routine is called to return the request to the driver.  Any endpoint
27823cd1385SRemy Bohmer  * (except control endpoints like ep0) may have more than one transfer
27923cd1385SRemy Bohmer  * request queued; they complete in FIFO order.  Once a gadget driver
28023cd1385SRemy Bohmer  * submits a request, that request may not be examined or modified until it
28123cd1385SRemy Bohmer  * is given back to that driver through the completion callback.
28223cd1385SRemy Bohmer  *
28323cd1385SRemy Bohmer  * Each request is turned into one or more packets.  The controller driver
28423cd1385SRemy Bohmer  * never merges adjacent requests into the same packet.  OUT transfers
28523cd1385SRemy Bohmer  * will sometimes use data that's already buffered in the hardware.
28623cd1385SRemy Bohmer  * Drivers can rely on the fact that the first byte of the request's buffer
28723cd1385SRemy Bohmer  * always corresponds to the first byte of some USB packet, for both
28823cd1385SRemy Bohmer  * IN and OUT transfers.
28923cd1385SRemy Bohmer  *
29023cd1385SRemy Bohmer  * Bulk endpoints can queue any amount of data; the transfer is packetized
29123cd1385SRemy Bohmer  * automatically.  The last packet will be short if the request doesn't fill it
29223cd1385SRemy Bohmer  * out completely.  Zero length packets (ZLPs) should be avoided in portable
29323cd1385SRemy Bohmer  * protocols since not all usb hardware can successfully handle zero length
29423cd1385SRemy Bohmer  * packets.  (ZLPs may be explicitly written, and may be implicitly written if
29523cd1385SRemy Bohmer  * the request 'zero' flag is set.)  Bulk endpoints may also be used
29623cd1385SRemy Bohmer  * for interrupt transfers; but the reverse is not true, and some endpoints
29723cd1385SRemy Bohmer  * won't support every interrupt transfer.  (Such as 768 byte packets.)
29823cd1385SRemy Bohmer  *
29923cd1385SRemy Bohmer  * Interrupt-only endpoints are less functional than bulk endpoints, for
30023cd1385SRemy Bohmer  * example by not supporting queueing or not handling buffers that are
30123cd1385SRemy Bohmer  * larger than the endpoint's maxpacket size.  They may also treat data
30223cd1385SRemy Bohmer  * toggle differently.
30323cd1385SRemy Bohmer  *
30423cd1385SRemy Bohmer  * Control endpoints ... after getting a setup() callback, the driver queues
30523cd1385SRemy Bohmer  * one response (even if it would be zero length).  That enables the
30623cd1385SRemy Bohmer  * status ack, after transfering data as specified in the response.  Setup
30723cd1385SRemy Bohmer  * functions may return negative error codes to generate protocol stalls.
30823cd1385SRemy Bohmer  * (Note that some USB device controllers disallow protocol stall responses
30923cd1385SRemy Bohmer  * in some cases.)  When control responses are deferred (the response is
31023cd1385SRemy Bohmer  * written after the setup callback returns), then usb_ep_set_halt() may be
31123cd1385SRemy Bohmer  * used on ep0 to trigger protocol stalls.
31223cd1385SRemy Bohmer  *
31323cd1385SRemy Bohmer  * For periodic endpoints, like interrupt or isochronous ones, the usb host
31423cd1385SRemy Bohmer  * arranges to poll once per interval, and the gadget driver usually will
31523cd1385SRemy Bohmer  * have queued some data to transfer at that time.
31623cd1385SRemy Bohmer  *
31723cd1385SRemy Bohmer  * Returns zero, or a negative error code.  Endpoints that are not enabled
31823cd1385SRemy Bohmer  * report errors; errors will also be
31923cd1385SRemy Bohmer  * reported when the usb peripheral is disconnected.
32023cd1385SRemy Bohmer  */
3216142e0aeSVitaly Kuzmichev static inline int usb_ep_queue(struct usb_ep *ep,
3226142e0aeSVitaly Kuzmichev 			       struct usb_request *req, gfp_t gfp_flags)
32323cd1385SRemy Bohmer {
32423cd1385SRemy Bohmer 	return ep->ops->queue(ep, req, gfp_flags);
32523cd1385SRemy Bohmer }
32623cd1385SRemy Bohmer 
32723cd1385SRemy Bohmer /**
32823cd1385SRemy Bohmer  * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
32923cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
33023cd1385SRemy Bohmer  * @req:the request being canceled
33123cd1385SRemy Bohmer  *
33223cd1385SRemy Bohmer  * if the request is still active on the endpoint, it is dequeued and its
33323cd1385SRemy Bohmer  * completion routine is called (with status -ECONNRESET); else a negative
33423cd1385SRemy Bohmer  * error code is returned.
33523cd1385SRemy Bohmer  *
33623cd1385SRemy Bohmer  * note that some hardware can't clear out write fifos (to unlink the request
33723cd1385SRemy Bohmer  * at the head of the queue) except as part of disconnecting from usb.  such
33823cd1385SRemy Bohmer  * restrictions prevent drivers from supporting configuration changes,
33923cd1385SRemy Bohmer  * even to configuration zero (a "chapter 9" requirement).
34023cd1385SRemy Bohmer  */
34123cd1385SRemy Bohmer static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
34223cd1385SRemy Bohmer {
34323cd1385SRemy Bohmer 	return ep->ops->dequeue(ep, req);
34423cd1385SRemy Bohmer }
34523cd1385SRemy Bohmer 
34623cd1385SRemy Bohmer /**
34723cd1385SRemy Bohmer  * usb_ep_set_halt - sets the endpoint halt feature.
34823cd1385SRemy Bohmer  * @ep: the non-isochronous endpoint being stalled
34923cd1385SRemy Bohmer  *
35023cd1385SRemy Bohmer  * Use this to stall an endpoint, perhaps as an error report.
35123cd1385SRemy Bohmer  * Except for control endpoints,
35223cd1385SRemy Bohmer  * the endpoint stays halted (will not stream any data) until the host
35323cd1385SRemy Bohmer  * clears this feature; drivers may need to empty the endpoint's request
35423cd1385SRemy Bohmer  * queue first, to make sure no inappropriate transfers happen.
35523cd1385SRemy Bohmer  *
35623cd1385SRemy Bohmer  * Note that while an endpoint CLEAR_FEATURE will be invisible to the
35723cd1385SRemy Bohmer  * gadget driver, a SET_INTERFACE will not be.  To reset endpoints for the
35823cd1385SRemy Bohmer  * current altsetting, see usb_ep_clear_halt().  When switching altsettings,
35923cd1385SRemy Bohmer  * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
36023cd1385SRemy Bohmer  *
36123cd1385SRemy Bohmer  * Returns zero, or a negative error code.  On success, this call sets
36223cd1385SRemy Bohmer  * underlying hardware state that blocks data transfers.
36323cd1385SRemy Bohmer  * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
36423cd1385SRemy Bohmer  * transfer requests are still queued, or if the controller hardware
36523cd1385SRemy Bohmer  * (usually a FIFO) still holds bytes that the host hasn't collected.
36623cd1385SRemy Bohmer  */
3676142e0aeSVitaly Kuzmichev static inline int usb_ep_set_halt(struct usb_ep *ep)
36823cd1385SRemy Bohmer {
36923cd1385SRemy Bohmer 	return ep->ops->set_halt(ep, 1);
37023cd1385SRemy Bohmer }
37123cd1385SRemy Bohmer 
37223cd1385SRemy Bohmer /**
37323cd1385SRemy Bohmer  * usb_ep_clear_halt - clears endpoint halt, and resets toggle
37423cd1385SRemy Bohmer  * @ep:the bulk or interrupt endpoint being reset
37523cd1385SRemy Bohmer  *
37623cd1385SRemy Bohmer  * Use this when responding to the standard usb "set interface" request,
37723cd1385SRemy Bohmer  * for endpoints that aren't reconfigured, after clearing any other state
37823cd1385SRemy Bohmer  * in the endpoint's i/o queue.
37923cd1385SRemy Bohmer  *
38023cd1385SRemy Bohmer  * Returns zero, or a negative error code.  On success, this call clears
38123cd1385SRemy Bohmer  * the underlying hardware state reflecting endpoint halt and data toggle.
38223cd1385SRemy Bohmer  * Note that some hardware can't support this request (like pxa2xx_udc),
38323cd1385SRemy Bohmer  * and accordingly can't correctly implement interface altsettings.
38423cd1385SRemy Bohmer  */
3856142e0aeSVitaly Kuzmichev static inline int usb_ep_clear_halt(struct usb_ep *ep)
38623cd1385SRemy Bohmer {
38723cd1385SRemy Bohmer 	return ep->ops->set_halt(ep, 0);
38823cd1385SRemy Bohmer }
38923cd1385SRemy Bohmer 
39023cd1385SRemy Bohmer /**
39123cd1385SRemy Bohmer  * usb_ep_fifo_status - returns number of bytes in fifo, or error
39223cd1385SRemy Bohmer  * @ep: the endpoint whose fifo status is being checked.
39323cd1385SRemy Bohmer  *
39423cd1385SRemy Bohmer  * FIFO endpoints may have "unclaimed data" in them in certain cases,
39523cd1385SRemy Bohmer  * such as after aborted transfers.  Hosts may not have collected all
39623cd1385SRemy Bohmer  * the IN data written by the gadget driver (and reported by a request
39723cd1385SRemy Bohmer  * completion).  The gadget driver may not have collected all the data
39823cd1385SRemy Bohmer  * written OUT to it by the host.  Drivers that need precise handling for
39923cd1385SRemy Bohmer  * fault reporting or recovery may need to use this call.
40023cd1385SRemy Bohmer  *
40123cd1385SRemy Bohmer  * This returns the number of such bytes in the fifo, or a negative
40223cd1385SRemy Bohmer  * errno if the endpoint doesn't use a FIFO or doesn't support such
40323cd1385SRemy Bohmer  * precise handling.
40423cd1385SRemy Bohmer  */
4056142e0aeSVitaly Kuzmichev static inline int usb_ep_fifo_status(struct usb_ep *ep)
40623cd1385SRemy Bohmer {
40723cd1385SRemy Bohmer 	if (ep->ops->fifo_status)
40823cd1385SRemy Bohmer 		return ep->ops->fifo_status(ep);
40923cd1385SRemy Bohmer 	else
41023cd1385SRemy Bohmer 		return -EOPNOTSUPP;
41123cd1385SRemy Bohmer }
41223cd1385SRemy Bohmer 
41323cd1385SRemy Bohmer /**
41423cd1385SRemy Bohmer  * usb_ep_fifo_flush - flushes contents of a fifo
41523cd1385SRemy Bohmer  * @ep: the endpoint whose fifo is being flushed.
41623cd1385SRemy Bohmer  *
41723cd1385SRemy Bohmer  * This call may be used to flush the "unclaimed data" that may exist in
41823cd1385SRemy Bohmer  * an endpoint fifo after abnormal transaction terminations.  The call
41923cd1385SRemy Bohmer  * must never be used except when endpoint is not being used for any
42023cd1385SRemy Bohmer  * protocol translation.
42123cd1385SRemy Bohmer  */
4226142e0aeSVitaly Kuzmichev static inline void usb_ep_fifo_flush(struct usb_ep *ep)
42323cd1385SRemy Bohmer {
42423cd1385SRemy Bohmer 	if (ep->ops->fifo_flush)
42523cd1385SRemy Bohmer 		ep->ops->fifo_flush(ep);
42623cd1385SRemy Bohmer }
42723cd1385SRemy Bohmer 
42823cd1385SRemy Bohmer 
42923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
43023cd1385SRemy Bohmer 
43123cd1385SRemy Bohmer struct usb_gadget;
43245d9337eSKishon Vijay Abraham I struct usb_gadget_driver;
43323cd1385SRemy Bohmer 
43423cd1385SRemy Bohmer /* the rest of the api to the controller hardware: device operations,
43523cd1385SRemy Bohmer  * which don't involve endpoints (or i/o).
43623cd1385SRemy Bohmer  */
43723cd1385SRemy Bohmer struct usb_gadget_ops {
43823cd1385SRemy Bohmer 	int	(*get_frame)(struct usb_gadget *);
43923cd1385SRemy Bohmer 	int	(*wakeup)(struct usb_gadget *);
44023cd1385SRemy Bohmer 	int	(*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
44123cd1385SRemy Bohmer 	int	(*vbus_session) (struct usb_gadget *, int is_active);
44223cd1385SRemy Bohmer 	int	(*vbus_draw) (struct usb_gadget *, unsigned mA);
44323cd1385SRemy Bohmer 	int	(*pullup) (struct usb_gadget *, int is_on);
44423cd1385SRemy Bohmer 	int	(*ioctl)(struct usb_gadget *,
44523cd1385SRemy Bohmer 				unsigned code, unsigned long param);
44645d9337eSKishon Vijay Abraham I 	int	(*udc_start)(struct usb_gadget *,
44745d9337eSKishon Vijay Abraham I 			     struct usb_gadget_driver *);
44845d9337eSKishon Vijay Abraham I 	int	(*udc_stop)(struct usb_gadget *);
44923cd1385SRemy Bohmer };
45023cd1385SRemy Bohmer 
45123cd1385SRemy Bohmer /**
45223cd1385SRemy Bohmer  * struct usb_gadget - represents a usb slave device
45323cd1385SRemy Bohmer  * @ops: Function pointers used to access hardware-specific operations.
45423cd1385SRemy Bohmer  * @ep0: Endpoint zero, used when reading or writing responses to
45523cd1385SRemy Bohmer  *	driver setup() requests
45623cd1385SRemy Bohmer  * @ep_list: List of other endpoints supported by the device.
45723cd1385SRemy Bohmer  * @speed: Speed of current connection to USB host.
458747a0a5bSKishon Vijay Abraham I  * @max_speed: Maximal speed the UDC can handle.  UDC must support this
459747a0a5bSKishon Vijay Abraham I  *      and all slower speeds.
460472d5460SYork Sun  * @is_dualspeed: true if the controller supports both high and full speed
46123cd1385SRemy Bohmer  *	operation.  If it does, the gadget driver must also support both.
462472d5460SYork Sun  * @is_otg: true if the USB device port uses a Mini-AB jack, so that the
46323cd1385SRemy Bohmer  *	gadget driver must provide a USB OTG descriptor.
464472d5460SYork Sun  * @is_a_peripheral: false unless is_otg, the "A" end of a USB cable
46523cd1385SRemy Bohmer  *	is in the Mini-AB jack, and HNP has been used to switch roles
46623cd1385SRemy Bohmer  *	so that the "A" device currently acts as A-Peripheral, not A-Host.
46723cd1385SRemy Bohmer  * @a_hnp_support: OTG device feature flag, indicating that the A-Host
46823cd1385SRemy Bohmer  *	supports HNP at this port.
46923cd1385SRemy Bohmer  * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
47023cd1385SRemy Bohmer  *	only supports HNP on a different root port.
47123cd1385SRemy Bohmer  * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
47223cd1385SRemy Bohmer  *	enabled HNP support.
47323cd1385SRemy Bohmer  * @name: Identifies the controller hardware type.  Used in diagnostics
47423cd1385SRemy Bohmer  *	and sometimes configuration.
47523cd1385SRemy Bohmer  * @dev: Driver model state for this abstract device.
476747a0a5bSKishon Vijay Abraham I  * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
477747a0a5bSKishon Vijay Abraham I  *	MaxPacketSize.
47823cd1385SRemy Bohmer  *
47923cd1385SRemy Bohmer  * Gadgets have a mostly-portable "gadget driver" implementing device
48023cd1385SRemy Bohmer  * functions, handling all usb configurations and interfaces.  Gadget
48123cd1385SRemy Bohmer  * drivers talk to hardware-specific code indirectly, through ops vectors.
48223cd1385SRemy Bohmer  * That insulates the gadget driver from hardware details, and packages
48323cd1385SRemy Bohmer  * the hardware endpoints through generic i/o queues.  The "usb_gadget"
48423cd1385SRemy Bohmer  * and "usb_ep" interfaces provide that insulation from the hardware.
48523cd1385SRemy Bohmer  *
48623cd1385SRemy Bohmer  * Except for the driver data, all fields in this structure are
48723cd1385SRemy Bohmer  * read-only to the gadget driver.  That driver data is part of the
48823cd1385SRemy Bohmer  * "driver model" infrastructure in 2.6 (and later) kernels, and for
48923cd1385SRemy Bohmer  * earlier systems is grouped in a similar structure that's not known
49023cd1385SRemy Bohmer  * to the rest of the kernel.
49123cd1385SRemy Bohmer  *
49223cd1385SRemy Bohmer  * Values of the three OTG device feature flags are updated before the
49323cd1385SRemy Bohmer  * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
49423cd1385SRemy Bohmer  * driver suspend() calls.  They are valid only when is_otg, and when the
49523cd1385SRemy Bohmer  * device is acting as a B-Peripheral (so is_a_peripheral is false).
49623cd1385SRemy Bohmer  */
49723cd1385SRemy Bohmer struct usb_gadget {
49823cd1385SRemy Bohmer 	/* readonly to gadget driver */
49923cd1385SRemy Bohmer 	const struct usb_gadget_ops	*ops;
50023cd1385SRemy Bohmer 	struct usb_ep			*ep0;
50123cd1385SRemy Bohmer 	struct list_head		ep_list;	/* of usb_ep */
50223cd1385SRemy Bohmer 	enum usb_device_speed		speed;
503747a0a5bSKishon Vijay Abraham I 	enum usb_device_speed		max_speed;
50445d9337eSKishon Vijay Abraham I 	enum usb_device_state		state;
50523cd1385SRemy Bohmer 	unsigned			is_dualspeed:1;
50623cd1385SRemy Bohmer 	unsigned			is_otg:1;
50723cd1385SRemy Bohmer 	unsigned			is_a_peripheral:1;
50823cd1385SRemy Bohmer 	unsigned			b_hnp_enable:1;
50923cd1385SRemy Bohmer 	unsigned			a_hnp_support:1;
51023cd1385SRemy Bohmer 	unsigned			a_alt_hnp_support:1;
51123cd1385SRemy Bohmer 	const char			*name;
51223cd1385SRemy Bohmer 	struct device			dev;
513747a0a5bSKishon Vijay Abraham I 	unsigned			quirk_ep_out_aligned_size:1;
51423cd1385SRemy Bohmer };
51523cd1385SRemy Bohmer 
51623cd1385SRemy Bohmer static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
51723cd1385SRemy Bohmer {
51823cd1385SRemy Bohmer 	gadget->dev.driver_data = data;
51923cd1385SRemy Bohmer }
52023cd1385SRemy Bohmer 
52123cd1385SRemy Bohmer static inline void *get_gadget_data(struct usb_gadget *gadget)
52223cd1385SRemy Bohmer {
52323cd1385SRemy Bohmer 	return gadget->dev.driver_data;
52423cd1385SRemy Bohmer }
52523cd1385SRemy Bohmer 
5264eec44d8SLukasz Majewski static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
5274eec44d8SLukasz Majewski {
5284eec44d8SLukasz Majewski 	return container_of(dev, struct usb_gadget, dev);
5294eec44d8SLukasz Majewski }
5304eec44d8SLukasz Majewski 
53123cd1385SRemy Bohmer /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
53223cd1385SRemy Bohmer #define gadget_for_each_ep(tmp, gadget) \
53323cd1385SRemy Bohmer 	list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
53423cd1385SRemy Bohmer 
53523cd1385SRemy Bohmer 
53623cd1385SRemy Bohmer /**
53723cd1385SRemy Bohmer  * gadget_is_dualspeed - return true iff the hardware handles high speed
53823cd1385SRemy Bohmer  * @g: controller that might support both high and full speeds
53923cd1385SRemy Bohmer  */
54023cd1385SRemy Bohmer static inline int gadget_is_dualspeed(struct usb_gadget *g)
54123cd1385SRemy Bohmer {
54223cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
54323cd1385SRemy Bohmer 	/* runtime test would check "g->is_dualspeed" ... that might be
54423cd1385SRemy Bohmer 	 * useful to work around hardware bugs, but is mostly pointless
54523cd1385SRemy Bohmer 	 */
54623cd1385SRemy Bohmer 	return 1;
54723cd1385SRemy Bohmer #else
54823cd1385SRemy Bohmer 	return 0;
54923cd1385SRemy Bohmer #endif
55023cd1385SRemy Bohmer }
55123cd1385SRemy Bohmer 
55223cd1385SRemy Bohmer /**
55323cd1385SRemy Bohmer  * gadget_is_otg - return true iff the hardware is OTG-ready
55423cd1385SRemy Bohmer  * @g: controller that might have a Mini-AB connector
55523cd1385SRemy Bohmer  *
55623cd1385SRemy Bohmer  * This is a runtime test, since kernels with a USB-OTG stack sometimes
55723cd1385SRemy Bohmer  * run on boards which only have a Mini-B (or Mini-A) connector.
55823cd1385SRemy Bohmer  */
55923cd1385SRemy Bohmer static inline int gadget_is_otg(struct usb_gadget *g)
56023cd1385SRemy Bohmer {
56123cd1385SRemy Bohmer #ifdef CONFIG_USB_OTG
56223cd1385SRemy Bohmer 	return g->is_otg;
56323cd1385SRemy Bohmer #else
56423cd1385SRemy Bohmer 	return 0;
56523cd1385SRemy Bohmer #endif
56623cd1385SRemy Bohmer }
56723cd1385SRemy Bohmer 
56823cd1385SRemy Bohmer /**
56923cd1385SRemy Bohmer  * usb_gadget_frame_number - returns the current frame number
57023cd1385SRemy Bohmer  * @gadget: controller that reports the frame number
57123cd1385SRemy Bohmer  *
57223cd1385SRemy Bohmer  * Returns the usb frame number, normally eleven bits from a SOF packet,
57323cd1385SRemy Bohmer  * or negative errno if this device doesn't support this capability.
57423cd1385SRemy Bohmer  */
57523cd1385SRemy Bohmer static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
57623cd1385SRemy Bohmer {
57723cd1385SRemy Bohmer 	return gadget->ops->get_frame(gadget);
57823cd1385SRemy Bohmer }
57923cd1385SRemy Bohmer 
58023cd1385SRemy Bohmer /**
58123cd1385SRemy Bohmer  * usb_gadget_wakeup - tries to wake up the host connected to this gadget
58223cd1385SRemy Bohmer  * @gadget: controller used to wake up the host
58323cd1385SRemy Bohmer  *
58423cd1385SRemy Bohmer  * Returns zero on success, else negative error code if the hardware
58523cd1385SRemy Bohmer  * doesn't support such attempts, or its support has not been enabled
58623cd1385SRemy Bohmer  * by the usb host.  Drivers must return device descriptors that report
58723cd1385SRemy Bohmer  * their ability to support this, or hosts won't enable it.
58823cd1385SRemy Bohmer  *
58923cd1385SRemy Bohmer  * This may also try to use SRP to wake the host and start enumeration,
59023cd1385SRemy Bohmer  * even if OTG isn't otherwise in use.  OTG devices may also start
59123cd1385SRemy Bohmer  * remote wakeup even when hosts don't explicitly enable it.
59223cd1385SRemy Bohmer  */
59323cd1385SRemy Bohmer static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
59423cd1385SRemy Bohmer {
59523cd1385SRemy Bohmer 	if (!gadget->ops->wakeup)
59623cd1385SRemy Bohmer 		return -EOPNOTSUPP;
59723cd1385SRemy Bohmer 	return gadget->ops->wakeup(gadget);
59823cd1385SRemy Bohmer }
59923cd1385SRemy Bohmer 
60023cd1385SRemy Bohmer /**
60123cd1385SRemy Bohmer  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
60223cd1385SRemy Bohmer  * @gadget:the device being declared as self-powered
60323cd1385SRemy Bohmer  *
60423cd1385SRemy Bohmer  * this affects the device status reported by the hardware driver
60523cd1385SRemy Bohmer  * to reflect that it now has a local power supply.
60623cd1385SRemy Bohmer  *
60723cd1385SRemy Bohmer  * returns zero on success, else negative errno.
60823cd1385SRemy Bohmer  */
6096142e0aeSVitaly Kuzmichev static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
61023cd1385SRemy Bohmer {
61123cd1385SRemy Bohmer 	if (!gadget->ops->set_selfpowered)
61223cd1385SRemy Bohmer 		return -EOPNOTSUPP;
61323cd1385SRemy Bohmer 	return gadget->ops->set_selfpowered(gadget, 1);
61423cd1385SRemy Bohmer }
61523cd1385SRemy Bohmer 
61623cd1385SRemy Bohmer /**
61723cd1385SRemy Bohmer  * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
61823cd1385SRemy Bohmer  * @gadget:the device being declared as bus-powered
61923cd1385SRemy Bohmer  *
62023cd1385SRemy Bohmer  * this affects the device status reported by the hardware driver.
62123cd1385SRemy Bohmer  * some hardware may not support bus-powered operation, in which
62223cd1385SRemy Bohmer  * case this feature's value can never change.
62323cd1385SRemy Bohmer  *
62423cd1385SRemy Bohmer  * returns zero on success, else negative errno.
62523cd1385SRemy Bohmer  */
6266142e0aeSVitaly Kuzmichev static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
62723cd1385SRemy Bohmer {
62823cd1385SRemy Bohmer 	if (!gadget->ops->set_selfpowered)
62923cd1385SRemy Bohmer 		return -EOPNOTSUPP;
63023cd1385SRemy Bohmer 	return gadget->ops->set_selfpowered(gadget, 0);
63123cd1385SRemy Bohmer }
63223cd1385SRemy Bohmer 
63323cd1385SRemy Bohmer /**
63423cd1385SRemy Bohmer  * usb_gadget_vbus_connect - Notify controller that VBUS is powered
63523cd1385SRemy Bohmer  * @gadget:The device which now has VBUS power.
63623cd1385SRemy Bohmer  *
63723cd1385SRemy Bohmer  * This call is used by a driver for an external transceiver (or GPIO)
63823cd1385SRemy Bohmer  * that detects a VBUS power session starting.  Common responses include
63923cd1385SRemy Bohmer  * resuming the controller, activating the D+ (or D-) pullup to let the
64023cd1385SRemy Bohmer  * host detect that a USB device is attached, and starting to draw power
64123cd1385SRemy Bohmer  * (8mA or possibly more, especially after SET_CONFIGURATION).
64223cd1385SRemy Bohmer  *
64323cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
64423cd1385SRemy Bohmer  */
6456142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
64623cd1385SRemy Bohmer {
64723cd1385SRemy Bohmer 	if (!gadget->ops->vbus_session)
64823cd1385SRemy Bohmer 		return -EOPNOTSUPP;
64923cd1385SRemy Bohmer 	return gadget->ops->vbus_session(gadget, 1);
65023cd1385SRemy Bohmer }
65123cd1385SRemy Bohmer 
65223cd1385SRemy Bohmer /**
65323cd1385SRemy Bohmer  * usb_gadget_vbus_draw - constrain controller's VBUS power usage
65423cd1385SRemy Bohmer  * @gadget:The device whose VBUS usage is being described
65523cd1385SRemy Bohmer  * @mA:How much current to draw, in milliAmperes.  This should be twice
65623cd1385SRemy Bohmer  *	the value listed in the configuration descriptor bMaxPower field.
65723cd1385SRemy Bohmer  *
65823cd1385SRemy Bohmer  * This call is used by gadget drivers during SET_CONFIGURATION calls,
65923cd1385SRemy Bohmer  * reporting how much power the device may consume.  For example, this
66023cd1385SRemy Bohmer  * could affect how quickly batteries are recharged.
66123cd1385SRemy Bohmer  *
66223cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
66323cd1385SRemy Bohmer  */
6646142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
66523cd1385SRemy Bohmer {
66623cd1385SRemy Bohmer 	if (!gadget->ops->vbus_draw)
66723cd1385SRemy Bohmer 		return -EOPNOTSUPP;
66823cd1385SRemy Bohmer 	return gadget->ops->vbus_draw(gadget, mA);
66923cd1385SRemy Bohmer }
67023cd1385SRemy Bohmer 
67123cd1385SRemy Bohmer /**
67223cd1385SRemy Bohmer  * usb_gadget_vbus_disconnect - notify controller about VBUS session end
67323cd1385SRemy Bohmer  * @gadget:the device whose VBUS supply is being described
67423cd1385SRemy Bohmer  *
67523cd1385SRemy Bohmer  * This call is used by a driver for an external transceiver (or GPIO)
67623cd1385SRemy Bohmer  * that detects a VBUS power session ending.  Common responses include
67723cd1385SRemy Bohmer  * reversing everything done in usb_gadget_vbus_connect().
67823cd1385SRemy Bohmer  *
67923cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
68023cd1385SRemy Bohmer  */
6816142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
68223cd1385SRemy Bohmer {
68323cd1385SRemy Bohmer 	if (!gadget->ops->vbus_session)
68423cd1385SRemy Bohmer 		return -EOPNOTSUPP;
68523cd1385SRemy Bohmer 	return gadget->ops->vbus_session(gadget, 0);
68623cd1385SRemy Bohmer }
68723cd1385SRemy Bohmer 
68823cd1385SRemy Bohmer /**
68923cd1385SRemy Bohmer  * usb_gadget_connect - software-controlled connect to USB host
69023cd1385SRemy Bohmer  * @gadget:the peripheral being connected
69123cd1385SRemy Bohmer  *
69223cd1385SRemy Bohmer  * Enables the D+ (or potentially D-) pullup.  The host will start
69323cd1385SRemy Bohmer  * enumerating this gadget when the pullup is active and a VBUS session
69423cd1385SRemy Bohmer  * is active (the link is powered).  This pullup is always enabled unless
69523cd1385SRemy Bohmer  * usb_gadget_disconnect() has been used to disable it.
69623cd1385SRemy Bohmer  *
69723cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
69823cd1385SRemy Bohmer  */
6996142e0aeSVitaly Kuzmichev static inline int usb_gadget_connect(struct usb_gadget *gadget)
70023cd1385SRemy Bohmer {
70123cd1385SRemy Bohmer 	if (!gadget->ops->pullup)
70223cd1385SRemy Bohmer 		return -EOPNOTSUPP;
70323cd1385SRemy Bohmer 	return gadget->ops->pullup(gadget, 1);
70423cd1385SRemy Bohmer }
70523cd1385SRemy Bohmer 
70623cd1385SRemy Bohmer /**
70723cd1385SRemy Bohmer  * usb_gadget_disconnect - software-controlled disconnect from USB host
70823cd1385SRemy Bohmer  * @gadget:the peripheral being disconnected
70923cd1385SRemy Bohmer  *
71023cd1385SRemy Bohmer  * Disables the D+ (or potentially D-) pullup, which the host may see
71123cd1385SRemy Bohmer  * as a disconnect (when a VBUS session is active).  Not all systems
71223cd1385SRemy Bohmer  * support software pullup controls.
71323cd1385SRemy Bohmer  *
71423cd1385SRemy Bohmer  * This routine may be used during the gadget driver bind() call to prevent
71523cd1385SRemy Bohmer  * the peripheral from ever being visible to the USB host, unless later
71623cd1385SRemy Bohmer  * usb_gadget_connect() is called.  For example, user mode components may
71723cd1385SRemy Bohmer  * need to be activated before the system can talk to hosts.
71823cd1385SRemy Bohmer  *
71923cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
72023cd1385SRemy Bohmer  */
7216142e0aeSVitaly Kuzmichev static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
72223cd1385SRemy Bohmer {
72323cd1385SRemy Bohmer 	if (!gadget->ops->pullup)
72423cd1385SRemy Bohmer 		return -EOPNOTSUPP;
72523cd1385SRemy Bohmer 	return gadget->ops->pullup(gadget, 0);
72623cd1385SRemy Bohmer }
72723cd1385SRemy Bohmer 
72823cd1385SRemy Bohmer 
72923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
73023cd1385SRemy Bohmer 
73123cd1385SRemy Bohmer /**
73223cd1385SRemy Bohmer  * struct usb_gadget_driver - driver for usb 'slave' devices
73345d9337eSKishon Vijay Abraham I  * @function: String describing the gadget's function
73423cd1385SRemy Bohmer  * @speed: Highest speed the driver handles.
73523cd1385SRemy Bohmer  * @bind: Invoked when the driver is bound to a gadget, usually
73623cd1385SRemy Bohmer  *	after registering the driver.
73723cd1385SRemy Bohmer  *	At that point, ep0 is fully initialized, and ep_list holds
73823cd1385SRemy Bohmer  *	the currently-available endpoints.
73923cd1385SRemy Bohmer  *	Called in a context that permits sleeping.
74023cd1385SRemy Bohmer  * @setup: Invoked for ep0 control requests that aren't handled by
74123cd1385SRemy Bohmer  *	the hardware level driver. Most calls must be handled by
74223cd1385SRemy Bohmer  *	the gadget driver, including descriptor and configuration
74323cd1385SRemy Bohmer  *	management.  The 16 bit members of the setup data are in
74423cd1385SRemy Bohmer  *	USB byte order. Called in_interrupt; this may not sleep.  Driver
74523cd1385SRemy Bohmer  *	queues a response to ep0, or returns negative to stall.
74623cd1385SRemy Bohmer  * @disconnect: Invoked after all transfers have been stopped,
74723cd1385SRemy Bohmer  *	when the host is disconnected.  May be called in_interrupt; this
74823cd1385SRemy Bohmer  *	may not sleep.  Some devices can't detect disconnect, so this might
74923cd1385SRemy Bohmer  *	not be called except as part of controller shutdown.
75023cd1385SRemy Bohmer  * @unbind: Invoked when the driver is unbound from a gadget,
75123cd1385SRemy Bohmer  *	usually from rmmod (after a disconnect is reported).
75223cd1385SRemy Bohmer  *	Called in a context that permits sleeping.
75323cd1385SRemy Bohmer  * @suspend: Invoked on USB suspend.  May be called in_interrupt.
75423cd1385SRemy Bohmer  * @resume: Invoked on USB resume.  May be called in_interrupt.
75545d9337eSKishon Vijay Abraham I  * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers
75645d9337eSKishon Vijay Abraham I  *	and should be called in_interrupt.
75723cd1385SRemy Bohmer  *
75823cd1385SRemy Bohmer  * Devices are disabled till a gadget driver successfully bind()s, which
75923cd1385SRemy Bohmer  * means the driver will handle setup() requests needed to enumerate (and
76023cd1385SRemy Bohmer  * meet "chapter 9" requirements) then do some useful work.
76123cd1385SRemy Bohmer  *
76223cd1385SRemy Bohmer  * If gadget->is_otg is true, the gadget driver must provide an OTG
76323cd1385SRemy Bohmer  * descriptor during enumeration, or else fail the bind() call.  In such
76423cd1385SRemy Bohmer  * cases, no USB traffic may flow until both bind() returns without
76523cd1385SRemy Bohmer  * having called usb_gadget_disconnect(), and the USB host stack has
76623cd1385SRemy Bohmer  * initialized.
76723cd1385SRemy Bohmer  *
76823cd1385SRemy Bohmer  * Drivers use hardware-specific knowledge to configure the usb hardware.
76923cd1385SRemy Bohmer  * endpoint addressing is only one of several hardware characteristics that
77023cd1385SRemy Bohmer  * are in descriptors the ep0 implementation returns from setup() calls.
77123cd1385SRemy Bohmer  *
77223cd1385SRemy Bohmer  * Except for ep0 implementation, most driver code shouldn't need change to
77323cd1385SRemy Bohmer  * run on top of different usb controllers.  It'll use endpoints set up by
77423cd1385SRemy Bohmer  * that ep0 implementation.
77523cd1385SRemy Bohmer  *
77623cd1385SRemy Bohmer  * The usb controller driver handles a few standard usb requests.  Those
77723cd1385SRemy Bohmer  * include set_address, and feature flags for devices, interfaces, and
77823cd1385SRemy Bohmer  * endpoints (the get_status, set_feature, and clear_feature requests).
77923cd1385SRemy Bohmer  *
78023cd1385SRemy Bohmer  * Accordingly, the driver's setup() callback must always implement all
78123cd1385SRemy Bohmer  * get_descriptor requests, returning at least a device descriptor and
78223cd1385SRemy Bohmer  * a configuration descriptor.  Drivers must make sure the endpoint
78323cd1385SRemy Bohmer  * descriptors match any hardware constraints. Some hardware also constrains
78423cd1385SRemy Bohmer  * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
78523cd1385SRemy Bohmer  *
78623cd1385SRemy Bohmer  * The driver's setup() callback must also implement set_configuration,
78723cd1385SRemy Bohmer  * and should also implement set_interface, get_configuration, and
78823cd1385SRemy Bohmer  * get_interface.  Setting a configuration (or interface) is where
78923cd1385SRemy Bohmer  * endpoints should be activated or (config 0) shut down.
79023cd1385SRemy Bohmer  *
79123cd1385SRemy Bohmer  * (Note that only the default control endpoint is supported.  Neither
79223cd1385SRemy Bohmer  * hosts nor devices generally support control traffic except to ep0.)
79323cd1385SRemy Bohmer  *
79423cd1385SRemy Bohmer  * Most devices will ignore USB suspend/resume operations, and so will
79523cd1385SRemy Bohmer  * not provide those callbacks.  However, some may need to change modes
79623cd1385SRemy Bohmer  * when the host is not longer directing those activities.  For example,
79723cd1385SRemy Bohmer  * local controls (buttons, dials, etc) may need to be re-enabled since
79823cd1385SRemy Bohmer  * the (remote) host can't do that any longer; or an error state might
79923cd1385SRemy Bohmer  * be cleared, to make the device behave identically whether or not
80023cd1385SRemy Bohmer  * power is maintained.
80123cd1385SRemy Bohmer  */
80223cd1385SRemy Bohmer struct usb_gadget_driver {
80345d9337eSKishon Vijay Abraham I 	char			*function;
80423cd1385SRemy Bohmer 	enum usb_device_speed	speed;
80523cd1385SRemy Bohmer 	int			(*bind)(struct usb_gadget *);
80623cd1385SRemy Bohmer 	void			(*unbind)(struct usb_gadget *);
80723cd1385SRemy Bohmer 	int			(*setup)(struct usb_gadget *,
80823cd1385SRemy Bohmer 					const struct usb_ctrlrequest *);
80923cd1385SRemy Bohmer 	void			(*disconnect)(struct usb_gadget *);
81023cd1385SRemy Bohmer 	void			(*suspend)(struct usb_gadget *);
81123cd1385SRemy Bohmer 	void			(*resume)(struct usb_gadget *);
81245d9337eSKishon Vijay Abraham I 	void			(*reset)(struct usb_gadget *);
81323cd1385SRemy Bohmer };
81423cd1385SRemy Bohmer 
81523cd1385SRemy Bohmer 
81623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
81723cd1385SRemy Bohmer 
81823cd1385SRemy Bohmer /* driver modules register and unregister, as usual.
81923cd1385SRemy Bohmer  * these calls must be made in a context that can sleep.
82023cd1385SRemy Bohmer  *
82123cd1385SRemy Bohmer  * these will usually be implemented directly by the hardware-dependent
82223cd1385SRemy Bohmer  * usb bus interface driver, which will only support a single driver.
82323cd1385SRemy Bohmer  */
82423cd1385SRemy Bohmer 
82523cd1385SRemy Bohmer /**
82623cd1385SRemy Bohmer  * usb_gadget_register_driver - register a gadget driver
82723cd1385SRemy Bohmer  * @driver:the driver being registered
82823cd1385SRemy Bohmer  *
82923cd1385SRemy Bohmer  * Call this in your gadget driver's module initialization function,
83023cd1385SRemy Bohmer  * to tell the underlying usb controller driver about your driver.
83123cd1385SRemy Bohmer  * The driver's bind() function will be called to bind it to a
83223cd1385SRemy Bohmer  * gadget before this registration call returns.  It's expected that
83323cd1385SRemy Bohmer  * the bind() functions will be in init sections.
83423cd1385SRemy Bohmer  * This function must be called in a context that can sleep.
83523cd1385SRemy Bohmer  */
83623cd1385SRemy Bohmer int usb_gadget_register_driver(struct usb_gadget_driver *driver);
83723cd1385SRemy Bohmer 
83823cd1385SRemy Bohmer /**
83923cd1385SRemy Bohmer  * usb_gadget_unregister_driver - unregister a gadget driver
84023cd1385SRemy Bohmer  * @driver:the driver being unregistered
84123cd1385SRemy Bohmer  *
84223cd1385SRemy Bohmer  * Call this in your gadget driver's module cleanup function,
84323cd1385SRemy Bohmer  * to tell the underlying usb controller that your driver is
84423cd1385SRemy Bohmer  * going away.  If the controller is connected to a USB host,
84523cd1385SRemy Bohmer  * it will first disconnect().  The driver is also requested
84623cd1385SRemy Bohmer  * to unbind() and clean up any device state, before this procedure
84723cd1385SRemy Bohmer  * finally returns.  It's expected that the unbind() functions
84823cd1385SRemy Bohmer  * will in in exit sections, so may not be linked in some kernels.
84923cd1385SRemy Bohmer  * This function must be called in a context that can sleep.
85023cd1385SRemy Bohmer  */
85123cd1385SRemy Bohmer int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
85223cd1385SRemy Bohmer 
85345d9337eSKishon Vijay Abraham I int usb_add_gadget_udc_release(struct device *parent,
85445d9337eSKishon Vijay Abraham I 		struct usb_gadget *gadget, void (*release)(struct device *dev));
85545d9337eSKishon Vijay Abraham I int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
85645d9337eSKishon Vijay Abraham I void usb_del_gadget_udc(struct usb_gadget *gadget);
85723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
85823cd1385SRemy Bohmer 
85923cd1385SRemy Bohmer /* utility to simplify dealing with string descriptors */
86023cd1385SRemy Bohmer 
86123cd1385SRemy Bohmer /**
86223cd1385SRemy Bohmer  * struct usb_string - wraps a C string and its USB id
86323cd1385SRemy Bohmer  * @id:the (nonzero) ID for this string
86423cd1385SRemy Bohmer  * @s:the string, in UTF-8 encoding
86523cd1385SRemy Bohmer  *
86623cd1385SRemy Bohmer  * If you're using usb_gadget_get_string(), use this to wrap a string
86723cd1385SRemy Bohmer  * together with its ID.
86823cd1385SRemy Bohmer  */
86923cd1385SRemy Bohmer struct usb_string {
87023cd1385SRemy Bohmer 	u8			id;
87123cd1385SRemy Bohmer 	const char		*s;
87223cd1385SRemy Bohmer };
87323cd1385SRemy Bohmer 
87423cd1385SRemy Bohmer /**
87523cd1385SRemy Bohmer  * struct usb_gadget_strings - a set of USB strings in a given language
87623cd1385SRemy Bohmer  * @language:identifies the strings' language (0x0409 for en-us)
87723cd1385SRemy Bohmer  * @strings:array of strings with their ids
87823cd1385SRemy Bohmer  *
87923cd1385SRemy Bohmer  * If you're using usb_gadget_get_string(), use this to wrap all the
88023cd1385SRemy Bohmer  * strings for a given language.
88123cd1385SRemy Bohmer  */
88223cd1385SRemy Bohmer struct usb_gadget_strings {
88323cd1385SRemy Bohmer 	u16			language;	/* 0x0409 for en-us */
88423cd1385SRemy Bohmer 	struct usb_string	*strings;
88523cd1385SRemy Bohmer };
88623cd1385SRemy Bohmer 
88723cd1385SRemy Bohmer /* put descriptor for string with that id into buf (buflen >= 256) */
88823cd1385SRemy Bohmer int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
88923cd1385SRemy Bohmer 
89023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
89123cd1385SRemy Bohmer 
89223cd1385SRemy Bohmer /* utility to simplify managing config descriptors */
89323cd1385SRemy Bohmer 
89423cd1385SRemy Bohmer /* write vector of descriptors into buffer */
89523cd1385SRemy Bohmer int usb_descriptor_fillbuf(void *, unsigned,
89623cd1385SRemy Bohmer 		const struct usb_descriptor_header **);
89723cd1385SRemy Bohmer 
89823cd1385SRemy Bohmer /* build config descriptor from single descriptor vector */
89923cd1385SRemy Bohmer int usb_gadget_config_buf(const struct usb_config_descriptor *config,
90023cd1385SRemy Bohmer 	void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
90123cd1385SRemy Bohmer 
90223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
90345d9337eSKishon Vijay Abraham I /* utility to simplify map/unmap of usb_requests to/from DMA */
90445d9337eSKishon Vijay Abraham I 
90545d9337eSKishon Vijay Abraham I extern int usb_gadget_map_request(struct usb_gadget *gadget,
90645d9337eSKishon Vijay Abraham I 				  struct usb_request *req, int is_in);
90745d9337eSKishon Vijay Abraham I 
90845d9337eSKishon Vijay Abraham I extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
90945d9337eSKishon Vijay Abraham I 				     struct usb_request *req, int is_in);
91045d9337eSKishon Vijay Abraham I 
91145d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
91245d9337eSKishon Vijay Abraham I 
91345d9337eSKishon Vijay Abraham I /* utility to set gadget state properly */
91445d9337eSKishon Vijay Abraham I 
91545d9337eSKishon Vijay Abraham I extern void usb_gadget_set_state(struct usb_gadget *gadget,
91645d9337eSKishon Vijay Abraham I 				 enum usb_device_state state);
91745d9337eSKishon Vijay Abraham I 
91845d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
91945d9337eSKishon Vijay Abraham I 
92045d9337eSKishon Vijay Abraham I /* utility to tell udc core that the bus reset occurs */
92145d9337eSKishon Vijay Abraham I extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
92245d9337eSKishon Vijay Abraham I 				 struct usb_gadget_driver *driver);
92345d9337eSKishon Vijay Abraham I 
92445d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
92545d9337eSKishon Vijay Abraham I 
92645d9337eSKishon Vijay Abraham I /* utility to give requests back to the gadget layer */
92745d9337eSKishon Vijay Abraham I 
92845d9337eSKishon Vijay Abraham I extern void usb_gadget_giveback_request(struct usb_ep *ep,
92945d9337eSKishon Vijay Abraham I 					struct usb_request *req);
93045d9337eSKishon Vijay Abraham I 
93145d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
93223cd1385SRemy Bohmer 
93323cd1385SRemy Bohmer /* utility wrapping a simple endpoint selection policy */
93423cd1385SRemy Bohmer 
93523cd1385SRemy Bohmer extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
93623cd1385SRemy Bohmer 			struct usb_endpoint_descriptor *);
93723cd1385SRemy Bohmer 
93823cd1385SRemy Bohmer extern void usb_ep_autoconfig_reset(struct usb_gadget *);
93923cd1385SRemy Bohmer 
940*2d48aa69SKishon Vijay Abraham I extern int usb_gadget_handle_interrupts(int index);
94123cd1385SRemy Bohmer 
94223cd1385SRemy Bohmer #endif	/* __LINUX_USB_GADGET_H */
943