xref: /rk3399_rockchip-uboot/include/linux/usb/gadget.h (revision 4eec44d84ffb6de469891f493481521ec4d992e2)
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 
2123cd1385SRemy Bohmer #include <linux/list.h>
2223cd1385SRemy Bohmer 
2323cd1385SRemy Bohmer struct usb_ep;
2423cd1385SRemy Bohmer 
2523cd1385SRemy Bohmer /**
2623cd1385SRemy Bohmer  * struct usb_request - describes one i/o request
2723cd1385SRemy Bohmer  * @buf: Buffer used for data.  Always provide this; some controllers
2823cd1385SRemy Bohmer  *	only use PIO, or don't use DMA for some endpoints.
2923cd1385SRemy Bohmer  * @dma: DMA address corresponding to 'buf'.  If you don't set this
3023cd1385SRemy Bohmer  *	field, and the usb controller needs one, it is responsible
3123cd1385SRemy Bohmer  *	for mapping and unmapping the buffer.
3223cd1385SRemy Bohmer  * @length: Length of that data
3323cd1385SRemy Bohmer  * @no_interrupt: If true, hints that no completion irq is needed.
3423cd1385SRemy Bohmer  *	Helpful sometimes with deep request queues that are handled
3523cd1385SRemy Bohmer  *	directly by DMA controllers.
3623cd1385SRemy Bohmer  * @zero: If true, when writing data, makes the last packet be "short"
3723cd1385SRemy Bohmer  *     by adding a zero length packet as needed;
3823cd1385SRemy Bohmer  * @short_not_ok: When reading data, makes short packets be
3923cd1385SRemy Bohmer  *     treated as errors (queue stops advancing till cleanup).
4023cd1385SRemy Bohmer  * @complete: Function called when request completes, so this request and
4123cd1385SRemy Bohmer  *	its buffer may be re-used.
4223cd1385SRemy Bohmer  *	Reads terminate with a short packet, or when the buffer fills,
4323cd1385SRemy Bohmer  *	whichever comes first.  When writes terminate, some data bytes
4423cd1385SRemy Bohmer  *	will usually still be in flight (often in a hardware fifo).
4523cd1385SRemy Bohmer  *	Errors (for reads or writes) stop the queue from advancing
4623cd1385SRemy Bohmer  *	until the completion function returns, so that any transfers
4723cd1385SRemy Bohmer  *	invalidated by the error may first be dequeued.
4823cd1385SRemy Bohmer  * @context: For use by the completion callback
4923cd1385SRemy Bohmer  * @list: For use by the gadget driver.
5023cd1385SRemy Bohmer  * @status: Reports completion code, zero or a negative errno.
5123cd1385SRemy Bohmer  *	Normally, faults block the transfer queue from advancing until
5223cd1385SRemy Bohmer  *	the completion callback returns.
5323cd1385SRemy Bohmer  *	Code "-ESHUTDOWN" indicates completion caused by device disconnect,
5423cd1385SRemy Bohmer  *	or when the driver disabled the endpoint.
5523cd1385SRemy Bohmer  * @actual: Reports bytes transferred to/from the buffer.  For reads (OUT
5623cd1385SRemy Bohmer  *	transfers) this may be less than the requested length.  If the
5723cd1385SRemy Bohmer  *	short_not_ok flag is set, short reads are treated as errors
5823cd1385SRemy Bohmer  *	even when status otherwise indicates successful completion.
5923cd1385SRemy Bohmer  *	Note that for writes (IN transfers) some data bytes may still
6023cd1385SRemy Bohmer  *	reside in a device-side FIFO when the request is reported as
6123cd1385SRemy Bohmer  *	complete.
6223cd1385SRemy Bohmer  *
6323cd1385SRemy Bohmer  * These are allocated/freed through the endpoint they're used with.  The
6423cd1385SRemy Bohmer  * hardware's driver can add extra per-request data to the memory it returns,
6523cd1385SRemy Bohmer  * which often avoids separate memory allocations (potential failures),
6623cd1385SRemy Bohmer  * later when the request is queued.
6723cd1385SRemy Bohmer  *
6823cd1385SRemy Bohmer  * Request flags affect request handling, such as whether a zero length
6923cd1385SRemy Bohmer  * packet is written (the "zero" flag), whether a short read should be
7023cd1385SRemy Bohmer  * treated as an error (blocking request queue advance, the "short_not_ok"
7123cd1385SRemy Bohmer  * flag), or hinting that an interrupt is not required (the "no_interrupt"
7223cd1385SRemy Bohmer  * flag, for use with deep request queues).
7323cd1385SRemy Bohmer  *
7423cd1385SRemy Bohmer  * Bulk endpoints can use any size buffers, and can also be used for interrupt
7523cd1385SRemy Bohmer  * transfers. interrupt-only endpoints can be much less functional.
766142e0aeSVitaly Kuzmichev  *
776142e0aeSVitaly Kuzmichev  * NOTE:  this is analagous to 'struct urb' on the host side, except that
786142e0aeSVitaly Kuzmichev  * it's thinner and promotes more pre-allocation.
7923cd1385SRemy Bohmer  */
8023cd1385SRemy Bohmer 
8123cd1385SRemy Bohmer struct usb_request {
8223cd1385SRemy Bohmer 	void			*buf;
8323cd1385SRemy Bohmer 	unsigned		length;
8423cd1385SRemy Bohmer 	dma_addr_t		dma;
8523cd1385SRemy Bohmer 
8623cd1385SRemy Bohmer 	unsigned		no_interrupt:1;
8723cd1385SRemy Bohmer 	unsigned		zero:1;
8823cd1385SRemy Bohmer 	unsigned		short_not_ok:1;
8923cd1385SRemy Bohmer 
9023cd1385SRemy Bohmer 	void			(*complete)(struct usb_ep *ep,
9123cd1385SRemy Bohmer 					struct usb_request *req);
9223cd1385SRemy Bohmer 	void			*context;
9323cd1385SRemy Bohmer 	struct list_head	list;
9423cd1385SRemy Bohmer 
9523cd1385SRemy Bohmer 	int			status;
9623cd1385SRemy Bohmer 	unsigned		actual;
9723cd1385SRemy Bohmer };
9823cd1385SRemy Bohmer 
9923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
10023cd1385SRemy Bohmer 
10123cd1385SRemy Bohmer /* endpoint-specific parts of the api to the usb controller hardware.
10223cd1385SRemy Bohmer  * unlike the urb model, (de)multiplexing layers are not required.
10323cd1385SRemy Bohmer  * (so this api could slash overhead if used on the host side...)
10423cd1385SRemy Bohmer  *
10523cd1385SRemy Bohmer  * note that device side usb controllers commonly differ in how many
10623cd1385SRemy Bohmer  * endpoints they support, as well as their capabilities.
10723cd1385SRemy Bohmer  */
10823cd1385SRemy Bohmer struct usb_ep_ops {
10923cd1385SRemy Bohmer 	int (*enable) (struct usb_ep *ep,
11023cd1385SRemy Bohmer 		const struct usb_endpoint_descriptor *desc);
11123cd1385SRemy Bohmer 	int (*disable) (struct usb_ep *ep);
11223cd1385SRemy Bohmer 
11323cd1385SRemy Bohmer 	struct usb_request *(*alloc_request) (struct usb_ep *ep,
11423cd1385SRemy Bohmer 		gfp_t gfp_flags);
11523cd1385SRemy Bohmer 	void (*free_request) (struct usb_ep *ep, struct usb_request *req);
11623cd1385SRemy Bohmer 
11723cd1385SRemy Bohmer 	int (*queue) (struct usb_ep *ep, struct usb_request *req,
11823cd1385SRemy Bohmer 		gfp_t gfp_flags);
11923cd1385SRemy Bohmer 	int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
12023cd1385SRemy Bohmer 
12123cd1385SRemy Bohmer 	int (*set_halt) (struct usb_ep *ep, int value);
12223cd1385SRemy Bohmer 	int (*fifo_status) (struct usb_ep *ep);
12323cd1385SRemy Bohmer 	void (*fifo_flush) (struct usb_ep *ep);
12423cd1385SRemy Bohmer };
12523cd1385SRemy Bohmer 
12623cd1385SRemy Bohmer /**
12723cd1385SRemy Bohmer  * struct usb_ep - device side representation of USB endpoint
12823cd1385SRemy Bohmer  * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
12923cd1385SRemy Bohmer  * @ops: Function pointers used to access hardware-specific operations.
13023cd1385SRemy Bohmer  * @ep_list:the gadget's ep_list holds all of its endpoints
13123cd1385SRemy Bohmer  * @maxpacket:The maximum packet size used on this endpoint.  The initial
13223cd1385SRemy Bohmer  *	value can sometimes be reduced (hardware allowing), according to
13323cd1385SRemy Bohmer  *      the endpoint descriptor used to configure the endpoint.
13423cd1385SRemy Bohmer  * @driver_data:for use by the gadget driver.  all other fields are
13523cd1385SRemy Bohmer  *	read-only to gadget drivers.
13623cd1385SRemy Bohmer  *
13723cd1385SRemy Bohmer  * the bus controller driver lists all the general purpose endpoints in
13823cd1385SRemy Bohmer  * gadget->ep_list.  the control endpoint (gadget->ep0) is not in that list,
13923cd1385SRemy Bohmer  * and is accessed only in response to a driver setup() callback.
14023cd1385SRemy Bohmer  */
14123cd1385SRemy Bohmer struct usb_ep {
14223cd1385SRemy Bohmer 	void			*driver_data;
14323cd1385SRemy Bohmer 	const char		*name;
14423cd1385SRemy Bohmer 	const struct usb_ep_ops	*ops;
14523cd1385SRemy Bohmer 	struct list_head	ep_list;
14623cd1385SRemy Bohmer 	unsigned		maxpacket:16;
14723cd1385SRemy Bohmer };
14823cd1385SRemy Bohmer 
14923cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
15023cd1385SRemy Bohmer 
15123cd1385SRemy Bohmer /**
15223cd1385SRemy Bohmer  * usb_ep_enable - configure endpoint, making it usable
15323cd1385SRemy Bohmer  * @ep:the endpoint being configured.  may not be the endpoint named "ep0".
15423cd1385SRemy Bohmer  *	drivers discover endpoints through the ep_list of a usb_gadget.
15523cd1385SRemy Bohmer  * @desc:descriptor for desired behavior.  caller guarantees this pointer
15623cd1385SRemy Bohmer  *	remains valid until the endpoint is disabled; the data byte order
15723cd1385SRemy Bohmer  *	is little-endian (usb-standard).
15823cd1385SRemy Bohmer  *
15923cd1385SRemy Bohmer  * when configurations are set, or when interface settings change, the driver
16023cd1385SRemy Bohmer  * will enable or disable the relevant endpoints.  while it is enabled, an
16123cd1385SRemy Bohmer  * endpoint may be used for i/o until the driver receives a disconnect() from
16223cd1385SRemy Bohmer  * the host or until the endpoint is disabled.
16323cd1385SRemy Bohmer  *
16423cd1385SRemy Bohmer  * the ep0 implementation (which calls this routine) must ensure that the
16523cd1385SRemy Bohmer  * hardware capabilities of each endpoint match the descriptor provided
16623cd1385SRemy Bohmer  * for it.  for example, an endpoint named "ep2in-bulk" would be usable
16723cd1385SRemy Bohmer  * for interrupt transfers as well as bulk, but it likely couldn't be used
16823cd1385SRemy Bohmer  * for iso transfers or for endpoint 14.  some endpoints are fully
16923cd1385SRemy Bohmer  * configurable, with more generic names like "ep-a".  (remember that for
17023cd1385SRemy Bohmer  * USB, "in" means "towards the USB master".)
17123cd1385SRemy Bohmer  *
17223cd1385SRemy Bohmer  * returns zero, or a negative error code.
17323cd1385SRemy Bohmer  */
1746142e0aeSVitaly Kuzmichev static inline int usb_ep_enable(struct usb_ep *ep,
1756142e0aeSVitaly Kuzmichev 				const struct usb_endpoint_descriptor *desc)
17623cd1385SRemy Bohmer {
17723cd1385SRemy Bohmer 	return ep->ops->enable(ep, desc);
17823cd1385SRemy Bohmer }
17923cd1385SRemy Bohmer 
18023cd1385SRemy Bohmer /**
18123cd1385SRemy Bohmer  * usb_ep_disable - endpoint is no longer usable
18223cd1385SRemy Bohmer  * @ep:the endpoint being unconfigured.  may not be the endpoint named "ep0".
18323cd1385SRemy Bohmer  *
18423cd1385SRemy Bohmer  * no other task may be using this endpoint when this is called.
18523cd1385SRemy Bohmer  * any pending and uncompleted requests will complete with status
18623cd1385SRemy Bohmer  * indicating disconnect (-ESHUTDOWN) before this call returns.
18723cd1385SRemy Bohmer  * gadget drivers must call usb_ep_enable() again before queueing
18823cd1385SRemy Bohmer  * requests to the endpoint.
18923cd1385SRemy Bohmer  *
19023cd1385SRemy Bohmer  * returns zero, or a negative error code.
19123cd1385SRemy Bohmer  */
1926142e0aeSVitaly Kuzmichev static inline int usb_ep_disable(struct usb_ep *ep)
19323cd1385SRemy Bohmer {
19423cd1385SRemy Bohmer 	return ep->ops->disable(ep);
19523cd1385SRemy Bohmer }
19623cd1385SRemy Bohmer 
19723cd1385SRemy Bohmer /**
19823cd1385SRemy Bohmer  * usb_ep_alloc_request - allocate a request object to use with this endpoint
19923cd1385SRemy Bohmer  * @ep:the endpoint to be used with with the request
20023cd1385SRemy Bohmer  * @gfp_flags:GFP_* flags to use
20123cd1385SRemy Bohmer  *
20223cd1385SRemy Bohmer  * Request objects must be allocated with this call, since they normally
20323cd1385SRemy Bohmer  * need controller-specific setup and may even need endpoint-specific
20423cd1385SRemy Bohmer  * resources such as allocation of DMA descriptors.
20523cd1385SRemy Bohmer  * Requests may be submitted with usb_ep_queue(), and receive a single
20623cd1385SRemy Bohmer  * completion callback.  Free requests with usb_ep_free_request(), when
20723cd1385SRemy Bohmer  * they are no longer needed.
20823cd1385SRemy Bohmer  *
20923cd1385SRemy Bohmer  * Returns the request, or null if one could not be allocated.
21023cd1385SRemy Bohmer  */
2116142e0aeSVitaly Kuzmichev static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
2126142e0aeSVitaly Kuzmichev 						       gfp_t gfp_flags)
21323cd1385SRemy Bohmer {
21423cd1385SRemy Bohmer 	return ep->ops->alloc_request(ep, gfp_flags);
21523cd1385SRemy Bohmer }
21623cd1385SRemy Bohmer 
21723cd1385SRemy Bohmer /**
21823cd1385SRemy Bohmer  * usb_ep_free_request - frees a request object
21923cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
22023cd1385SRemy Bohmer  * @req:the request being freed
22123cd1385SRemy Bohmer  *
22223cd1385SRemy Bohmer  * Reverses the effect of usb_ep_alloc_request().
22323cd1385SRemy Bohmer  * Caller guarantees the request is not queued, and that it will
22423cd1385SRemy Bohmer  * no longer be requeued (or otherwise used).
22523cd1385SRemy Bohmer  */
2266142e0aeSVitaly Kuzmichev static inline void usb_ep_free_request(struct usb_ep *ep,
2276142e0aeSVitaly Kuzmichev 				       struct usb_request *req)
22823cd1385SRemy Bohmer {
22923cd1385SRemy Bohmer 	ep->ops->free_request(ep, req);
23023cd1385SRemy Bohmer }
23123cd1385SRemy Bohmer 
23223cd1385SRemy Bohmer /**
23323cd1385SRemy Bohmer  * usb_ep_queue - queues (submits) an I/O request to an endpoint.
23423cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
23523cd1385SRemy Bohmer  * @req:the request being submitted
23623cd1385SRemy Bohmer  * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
23723cd1385SRemy Bohmer  *	pre-allocate all necessary memory with the request.
23823cd1385SRemy Bohmer  *
23923cd1385SRemy Bohmer  * This tells the device controller to perform the specified request through
24023cd1385SRemy Bohmer  * that endpoint (reading or writing a buffer).  When the request completes,
24123cd1385SRemy Bohmer  * including being canceled by usb_ep_dequeue(), the request's completion
24223cd1385SRemy Bohmer  * routine is called to return the request to the driver.  Any endpoint
24323cd1385SRemy Bohmer  * (except control endpoints like ep0) may have more than one transfer
24423cd1385SRemy Bohmer  * request queued; they complete in FIFO order.  Once a gadget driver
24523cd1385SRemy Bohmer  * submits a request, that request may not be examined or modified until it
24623cd1385SRemy Bohmer  * is given back to that driver through the completion callback.
24723cd1385SRemy Bohmer  *
24823cd1385SRemy Bohmer  * Each request is turned into one or more packets.  The controller driver
24923cd1385SRemy Bohmer  * never merges adjacent requests into the same packet.  OUT transfers
25023cd1385SRemy Bohmer  * will sometimes use data that's already buffered in the hardware.
25123cd1385SRemy Bohmer  * Drivers can rely on the fact that the first byte of the request's buffer
25223cd1385SRemy Bohmer  * always corresponds to the first byte of some USB packet, for both
25323cd1385SRemy Bohmer  * IN and OUT transfers.
25423cd1385SRemy Bohmer  *
25523cd1385SRemy Bohmer  * Bulk endpoints can queue any amount of data; the transfer is packetized
25623cd1385SRemy Bohmer  * automatically.  The last packet will be short if the request doesn't fill it
25723cd1385SRemy Bohmer  * out completely.  Zero length packets (ZLPs) should be avoided in portable
25823cd1385SRemy Bohmer  * protocols since not all usb hardware can successfully handle zero length
25923cd1385SRemy Bohmer  * packets.  (ZLPs may be explicitly written, and may be implicitly written if
26023cd1385SRemy Bohmer  * the request 'zero' flag is set.)  Bulk endpoints may also be used
26123cd1385SRemy Bohmer  * for interrupt transfers; but the reverse is not true, and some endpoints
26223cd1385SRemy Bohmer  * won't support every interrupt transfer.  (Such as 768 byte packets.)
26323cd1385SRemy Bohmer  *
26423cd1385SRemy Bohmer  * Interrupt-only endpoints are less functional than bulk endpoints, for
26523cd1385SRemy Bohmer  * example by not supporting queueing or not handling buffers that are
26623cd1385SRemy Bohmer  * larger than the endpoint's maxpacket size.  They may also treat data
26723cd1385SRemy Bohmer  * toggle differently.
26823cd1385SRemy Bohmer  *
26923cd1385SRemy Bohmer  * Control endpoints ... after getting a setup() callback, the driver queues
27023cd1385SRemy Bohmer  * one response (even if it would be zero length).  That enables the
27123cd1385SRemy Bohmer  * status ack, after transfering data as specified in the response.  Setup
27223cd1385SRemy Bohmer  * functions may return negative error codes to generate protocol stalls.
27323cd1385SRemy Bohmer  * (Note that some USB device controllers disallow protocol stall responses
27423cd1385SRemy Bohmer  * in some cases.)  When control responses are deferred (the response is
27523cd1385SRemy Bohmer  * written after the setup callback returns), then usb_ep_set_halt() may be
27623cd1385SRemy Bohmer  * used on ep0 to trigger protocol stalls.
27723cd1385SRemy Bohmer  *
27823cd1385SRemy Bohmer  * For periodic endpoints, like interrupt or isochronous ones, the usb host
27923cd1385SRemy Bohmer  * arranges to poll once per interval, and the gadget driver usually will
28023cd1385SRemy Bohmer  * have queued some data to transfer at that time.
28123cd1385SRemy Bohmer  *
28223cd1385SRemy Bohmer  * Returns zero, or a negative error code.  Endpoints that are not enabled
28323cd1385SRemy Bohmer  * report errors; errors will also be
28423cd1385SRemy Bohmer  * reported when the usb peripheral is disconnected.
28523cd1385SRemy Bohmer  */
2866142e0aeSVitaly Kuzmichev static inline int usb_ep_queue(struct usb_ep *ep,
2876142e0aeSVitaly Kuzmichev 			       struct usb_request *req, gfp_t gfp_flags)
28823cd1385SRemy Bohmer {
28923cd1385SRemy Bohmer 	return ep->ops->queue(ep, req, gfp_flags);
29023cd1385SRemy Bohmer }
29123cd1385SRemy Bohmer 
29223cd1385SRemy Bohmer /**
29323cd1385SRemy Bohmer  * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
29423cd1385SRemy Bohmer  * @ep:the endpoint associated with the request
29523cd1385SRemy Bohmer  * @req:the request being canceled
29623cd1385SRemy Bohmer  *
29723cd1385SRemy Bohmer  * if the request is still active on the endpoint, it is dequeued and its
29823cd1385SRemy Bohmer  * completion routine is called (with status -ECONNRESET); else a negative
29923cd1385SRemy Bohmer  * error code is returned.
30023cd1385SRemy Bohmer  *
30123cd1385SRemy Bohmer  * note that some hardware can't clear out write fifos (to unlink the request
30223cd1385SRemy Bohmer  * at the head of the queue) except as part of disconnecting from usb.  such
30323cd1385SRemy Bohmer  * restrictions prevent drivers from supporting configuration changes,
30423cd1385SRemy Bohmer  * even to configuration zero (a "chapter 9" requirement).
30523cd1385SRemy Bohmer  */
30623cd1385SRemy Bohmer static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
30723cd1385SRemy Bohmer {
30823cd1385SRemy Bohmer 	return ep->ops->dequeue(ep, req);
30923cd1385SRemy Bohmer }
31023cd1385SRemy Bohmer 
31123cd1385SRemy Bohmer /**
31223cd1385SRemy Bohmer  * usb_ep_set_halt - sets the endpoint halt feature.
31323cd1385SRemy Bohmer  * @ep: the non-isochronous endpoint being stalled
31423cd1385SRemy Bohmer  *
31523cd1385SRemy Bohmer  * Use this to stall an endpoint, perhaps as an error report.
31623cd1385SRemy Bohmer  * Except for control endpoints,
31723cd1385SRemy Bohmer  * the endpoint stays halted (will not stream any data) until the host
31823cd1385SRemy Bohmer  * clears this feature; drivers may need to empty the endpoint's request
31923cd1385SRemy Bohmer  * queue first, to make sure no inappropriate transfers happen.
32023cd1385SRemy Bohmer  *
32123cd1385SRemy Bohmer  * Note that while an endpoint CLEAR_FEATURE will be invisible to the
32223cd1385SRemy Bohmer  * gadget driver, a SET_INTERFACE will not be.  To reset endpoints for the
32323cd1385SRemy Bohmer  * current altsetting, see usb_ep_clear_halt().  When switching altsettings,
32423cd1385SRemy Bohmer  * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
32523cd1385SRemy Bohmer  *
32623cd1385SRemy Bohmer  * Returns zero, or a negative error code.  On success, this call sets
32723cd1385SRemy Bohmer  * underlying hardware state that blocks data transfers.
32823cd1385SRemy Bohmer  * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
32923cd1385SRemy Bohmer  * transfer requests are still queued, or if the controller hardware
33023cd1385SRemy Bohmer  * (usually a FIFO) still holds bytes that the host hasn't collected.
33123cd1385SRemy Bohmer  */
3326142e0aeSVitaly Kuzmichev static inline int usb_ep_set_halt(struct usb_ep *ep)
33323cd1385SRemy Bohmer {
33423cd1385SRemy Bohmer 	return ep->ops->set_halt(ep, 1);
33523cd1385SRemy Bohmer }
33623cd1385SRemy Bohmer 
33723cd1385SRemy Bohmer /**
33823cd1385SRemy Bohmer  * usb_ep_clear_halt - clears endpoint halt, and resets toggle
33923cd1385SRemy Bohmer  * @ep:the bulk or interrupt endpoint being reset
34023cd1385SRemy Bohmer  *
34123cd1385SRemy Bohmer  * Use this when responding to the standard usb "set interface" request,
34223cd1385SRemy Bohmer  * for endpoints that aren't reconfigured, after clearing any other state
34323cd1385SRemy Bohmer  * in the endpoint's i/o queue.
34423cd1385SRemy Bohmer  *
34523cd1385SRemy Bohmer  * Returns zero, or a negative error code.  On success, this call clears
34623cd1385SRemy Bohmer  * the underlying hardware state reflecting endpoint halt and data toggle.
34723cd1385SRemy Bohmer  * Note that some hardware can't support this request (like pxa2xx_udc),
34823cd1385SRemy Bohmer  * and accordingly can't correctly implement interface altsettings.
34923cd1385SRemy Bohmer  */
3506142e0aeSVitaly Kuzmichev static inline int usb_ep_clear_halt(struct usb_ep *ep)
35123cd1385SRemy Bohmer {
35223cd1385SRemy Bohmer 	return ep->ops->set_halt(ep, 0);
35323cd1385SRemy Bohmer }
35423cd1385SRemy Bohmer 
35523cd1385SRemy Bohmer /**
35623cd1385SRemy Bohmer  * usb_ep_fifo_status - returns number of bytes in fifo, or error
35723cd1385SRemy Bohmer  * @ep: the endpoint whose fifo status is being checked.
35823cd1385SRemy Bohmer  *
35923cd1385SRemy Bohmer  * FIFO endpoints may have "unclaimed data" in them in certain cases,
36023cd1385SRemy Bohmer  * such as after aborted transfers.  Hosts may not have collected all
36123cd1385SRemy Bohmer  * the IN data written by the gadget driver (and reported by a request
36223cd1385SRemy Bohmer  * completion).  The gadget driver may not have collected all the data
36323cd1385SRemy Bohmer  * written OUT to it by the host.  Drivers that need precise handling for
36423cd1385SRemy Bohmer  * fault reporting or recovery may need to use this call.
36523cd1385SRemy Bohmer  *
36623cd1385SRemy Bohmer  * This returns the number of such bytes in the fifo, or a negative
36723cd1385SRemy Bohmer  * errno if the endpoint doesn't use a FIFO or doesn't support such
36823cd1385SRemy Bohmer  * precise handling.
36923cd1385SRemy Bohmer  */
3706142e0aeSVitaly Kuzmichev static inline int usb_ep_fifo_status(struct usb_ep *ep)
37123cd1385SRemy Bohmer {
37223cd1385SRemy Bohmer 	if (ep->ops->fifo_status)
37323cd1385SRemy Bohmer 		return ep->ops->fifo_status(ep);
37423cd1385SRemy Bohmer 	else
37523cd1385SRemy Bohmer 		return -EOPNOTSUPP;
37623cd1385SRemy Bohmer }
37723cd1385SRemy Bohmer 
37823cd1385SRemy Bohmer /**
37923cd1385SRemy Bohmer  * usb_ep_fifo_flush - flushes contents of a fifo
38023cd1385SRemy Bohmer  * @ep: the endpoint whose fifo is being flushed.
38123cd1385SRemy Bohmer  *
38223cd1385SRemy Bohmer  * This call may be used to flush the "unclaimed data" that may exist in
38323cd1385SRemy Bohmer  * an endpoint fifo after abnormal transaction terminations.  The call
38423cd1385SRemy Bohmer  * must never be used except when endpoint is not being used for any
38523cd1385SRemy Bohmer  * protocol translation.
38623cd1385SRemy Bohmer  */
3876142e0aeSVitaly Kuzmichev static inline void usb_ep_fifo_flush(struct usb_ep *ep)
38823cd1385SRemy Bohmer {
38923cd1385SRemy Bohmer 	if (ep->ops->fifo_flush)
39023cd1385SRemy Bohmer 		ep->ops->fifo_flush(ep);
39123cd1385SRemy Bohmer }
39223cd1385SRemy Bohmer 
39323cd1385SRemy Bohmer 
39423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
39523cd1385SRemy Bohmer 
39623cd1385SRemy Bohmer struct usb_gadget;
39723cd1385SRemy Bohmer 
39823cd1385SRemy Bohmer /* the rest of the api to the controller hardware: device operations,
39923cd1385SRemy Bohmer  * which don't involve endpoints (or i/o).
40023cd1385SRemy Bohmer  */
40123cd1385SRemy Bohmer struct usb_gadget_ops {
40223cd1385SRemy Bohmer 	int	(*get_frame)(struct usb_gadget *);
40323cd1385SRemy Bohmer 	int	(*wakeup)(struct usb_gadget *);
40423cd1385SRemy Bohmer 	int	(*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
40523cd1385SRemy Bohmer 	int	(*vbus_session) (struct usb_gadget *, int is_active);
40623cd1385SRemy Bohmer 	int	(*vbus_draw) (struct usb_gadget *, unsigned mA);
40723cd1385SRemy Bohmer 	int	(*pullup) (struct usb_gadget *, int is_on);
40823cd1385SRemy Bohmer 	int	(*ioctl)(struct usb_gadget *,
40923cd1385SRemy Bohmer 				unsigned code, unsigned long param);
41023cd1385SRemy Bohmer };
41123cd1385SRemy Bohmer 
41223cd1385SRemy Bohmer struct device {
41323cd1385SRemy Bohmer 	void		*driver_data;	/* data private to the driver */
414*4eec44d8SLukasz Majewski 	void            *device_data;   /* data private to the device */
41523cd1385SRemy Bohmer };
41623cd1385SRemy Bohmer 
41723cd1385SRemy Bohmer /**
41823cd1385SRemy Bohmer  * struct usb_gadget - represents a usb slave device
41923cd1385SRemy Bohmer  * @ops: Function pointers used to access hardware-specific operations.
42023cd1385SRemy Bohmer  * @ep0: Endpoint zero, used when reading or writing responses to
42123cd1385SRemy Bohmer  *	driver setup() requests
42223cd1385SRemy Bohmer  * @ep_list: List of other endpoints supported by the device.
42323cd1385SRemy Bohmer  * @speed: Speed of current connection to USB host.
42423cd1385SRemy Bohmer  * @is_dualspeed: True if the controller supports both high and full speed
42523cd1385SRemy Bohmer  *	operation.  If it does, the gadget driver must also support both.
42623cd1385SRemy Bohmer  * @is_otg: True if the USB device port uses a Mini-AB jack, so that the
42723cd1385SRemy Bohmer  *	gadget driver must provide a USB OTG descriptor.
42823cd1385SRemy Bohmer  * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable
42923cd1385SRemy Bohmer  *	is in the Mini-AB jack, and HNP has been used to switch roles
43023cd1385SRemy Bohmer  *	so that the "A" device currently acts as A-Peripheral, not A-Host.
43123cd1385SRemy Bohmer  * @a_hnp_support: OTG device feature flag, indicating that the A-Host
43223cd1385SRemy Bohmer  *	supports HNP at this port.
43323cd1385SRemy Bohmer  * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
43423cd1385SRemy Bohmer  *	only supports HNP on a different root port.
43523cd1385SRemy Bohmer  * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
43623cd1385SRemy Bohmer  *	enabled HNP support.
43723cd1385SRemy Bohmer  * @name: Identifies the controller hardware type.  Used in diagnostics
43823cd1385SRemy Bohmer  *	and sometimes configuration.
43923cd1385SRemy Bohmer  * @dev: Driver model state for this abstract device.
44023cd1385SRemy Bohmer  *
44123cd1385SRemy Bohmer  * Gadgets have a mostly-portable "gadget driver" implementing device
44223cd1385SRemy Bohmer  * functions, handling all usb configurations and interfaces.  Gadget
44323cd1385SRemy Bohmer  * drivers talk to hardware-specific code indirectly, through ops vectors.
44423cd1385SRemy Bohmer  * That insulates the gadget driver from hardware details, and packages
44523cd1385SRemy Bohmer  * the hardware endpoints through generic i/o queues.  The "usb_gadget"
44623cd1385SRemy Bohmer  * and "usb_ep" interfaces provide that insulation from the hardware.
44723cd1385SRemy Bohmer  *
44823cd1385SRemy Bohmer  * Except for the driver data, all fields in this structure are
44923cd1385SRemy Bohmer  * read-only to the gadget driver.  That driver data is part of the
45023cd1385SRemy Bohmer  * "driver model" infrastructure in 2.6 (and later) kernels, and for
45123cd1385SRemy Bohmer  * earlier systems is grouped in a similar structure that's not known
45223cd1385SRemy Bohmer  * to the rest of the kernel.
45323cd1385SRemy Bohmer  *
45423cd1385SRemy Bohmer  * Values of the three OTG device feature flags are updated before the
45523cd1385SRemy Bohmer  * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
45623cd1385SRemy Bohmer  * driver suspend() calls.  They are valid only when is_otg, and when the
45723cd1385SRemy Bohmer  * device is acting as a B-Peripheral (so is_a_peripheral is false).
45823cd1385SRemy Bohmer  */
45923cd1385SRemy Bohmer struct usb_gadget {
46023cd1385SRemy Bohmer 	/* readonly to gadget driver */
46123cd1385SRemy Bohmer 	const struct usb_gadget_ops	*ops;
46223cd1385SRemy Bohmer 	struct usb_ep			*ep0;
46323cd1385SRemy Bohmer 	struct list_head		ep_list;	/* of usb_ep */
46423cd1385SRemy Bohmer 	enum usb_device_speed		speed;
46523cd1385SRemy Bohmer 	unsigned			is_dualspeed:1;
46623cd1385SRemy Bohmer 	unsigned			is_otg:1;
46723cd1385SRemy Bohmer 	unsigned			is_a_peripheral:1;
46823cd1385SRemy Bohmer 	unsigned			b_hnp_enable:1;
46923cd1385SRemy Bohmer 	unsigned			a_hnp_support:1;
47023cd1385SRemy Bohmer 	unsigned			a_alt_hnp_support:1;
47123cd1385SRemy Bohmer 	const char			*name;
47223cd1385SRemy Bohmer 	struct device			dev;
47323cd1385SRemy Bohmer };
47423cd1385SRemy Bohmer 
47523cd1385SRemy Bohmer static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
47623cd1385SRemy Bohmer {
47723cd1385SRemy Bohmer 	gadget->dev.driver_data = data;
47823cd1385SRemy Bohmer }
47923cd1385SRemy Bohmer 
48023cd1385SRemy Bohmer static inline void *get_gadget_data(struct usb_gadget *gadget)
48123cd1385SRemy Bohmer {
48223cd1385SRemy Bohmer 	return gadget->dev.driver_data;
48323cd1385SRemy Bohmer }
48423cd1385SRemy Bohmer 
485*4eec44d8SLukasz Majewski static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
486*4eec44d8SLukasz Majewski {
487*4eec44d8SLukasz Majewski 	return container_of(dev, struct usb_gadget, dev);
488*4eec44d8SLukasz Majewski }
489*4eec44d8SLukasz Majewski 
49023cd1385SRemy Bohmer /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
49123cd1385SRemy Bohmer #define gadget_for_each_ep(tmp, gadget) \
49223cd1385SRemy Bohmer 	list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
49323cd1385SRemy Bohmer 
49423cd1385SRemy Bohmer 
49523cd1385SRemy Bohmer /**
49623cd1385SRemy Bohmer  * gadget_is_dualspeed - return true iff the hardware handles high speed
49723cd1385SRemy Bohmer  * @g: controller that might support both high and full speeds
49823cd1385SRemy Bohmer  */
49923cd1385SRemy Bohmer static inline int gadget_is_dualspeed(struct usb_gadget *g)
50023cd1385SRemy Bohmer {
50123cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
50223cd1385SRemy Bohmer 	/* runtime test would check "g->is_dualspeed" ... that might be
50323cd1385SRemy Bohmer 	 * useful to work around hardware bugs, but is mostly pointless
50423cd1385SRemy Bohmer 	 */
50523cd1385SRemy Bohmer 	return 1;
50623cd1385SRemy Bohmer #else
50723cd1385SRemy Bohmer 	return 0;
50823cd1385SRemy Bohmer #endif
50923cd1385SRemy Bohmer }
51023cd1385SRemy Bohmer 
51123cd1385SRemy Bohmer /**
51223cd1385SRemy Bohmer  * gadget_is_otg - return true iff the hardware is OTG-ready
51323cd1385SRemy Bohmer  * @g: controller that might have a Mini-AB connector
51423cd1385SRemy Bohmer  *
51523cd1385SRemy Bohmer  * This is a runtime test, since kernels with a USB-OTG stack sometimes
51623cd1385SRemy Bohmer  * run on boards which only have a Mini-B (or Mini-A) connector.
51723cd1385SRemy Bohmer  */
51823cd1385SRemy Bohmer static inline int gadget_is_otg(struct usb_gadget *g)
51923cd1385SRemy Bohmer {
52023cd1385SRemy Bohmer #ifdef CONFIG_USB_OTG
52123cd1385SRemy Bohmer 	return g->is_otg;
52223cd1385SRemy Bohmer #else
52323cd1385SRemy Bohmer 	return 0;
52423cd1385SRemy Bohmer #endif
52523cd1385SRemy Bohmer }
52623cd1385SRemy Bohmer 
52723cd1385SRemy Bohmer /**
52823cd1385SRemy Bohmer  * usb_gadget_frame_number - returns the current frame number
52923cd1385SRemy Bohmer  * @gadget: controller that reports the frame number
53023cd1385SRemy Bohmer  *
53123cd1385SRemy Bohmer  * Returns the usb frame number, normally eleven bits from a SOF packet,
53223cd1385SRemy Bohmer  * or negative errno if this device doesn't support this capability.
53323cd1385SRemy Bohmer  */
53423cd1385SRemy Bohmer static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
53523cd1385SRemy Bohmer {
53623cd1385SRemy Bohmer 	return gadget->ops->get_frame(gadget);
53723cd1385SRemy Bohmer }
53823cd1385SRemy Bohmer 
53923cd1385SRemy Bohmer /**
54023cd1385SRemy Bohmer  * usb_gadget_wakeup - tries to wake up the host connected to this gadget
54123cd1385SRemy Bohmer  * @gadget: controller used to wake up the host
54223cd1385SRemy Bohmer  *
54323cd1385SRemy Bohmer  * Returns zero on success, else negative error code if the hardware
54423cd1385SRemy Bohmer  * doesn't support such attempts, or its support has not been enabled
54523cd1385SRemy Bohmer  * by the usb host.  Drivers must return device descriptors that report
54623cd1385SRemy Bohmer  * their ability to support this, or hosts won't enable it.
54723cd1385SRemy Bohmer  *
54823cd1385SRemy Bohmer  * This may also try to use SRP to wake the host and start enumeration,
54923cd1385SRemy Bohmer  * even if OTG isn't otherwise in use.  OTG devices may also start
55023cd1385SRemy Bohmer  * remote wakeup even when hosts don't explicitly enable it.
55123cd1385SRemy Bohmer  */
55223cd1385SRemy Bohmer static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
55323cd1385SRemy Bohmer {
55423cd1385SRemy Bohmer 	if (!gadget->ops->wakeup)
55523cd1385SRemy Bohmer 		return -EOPNOTSUPP;
55623cd1385SRemy Bohmer 	return gadget->ops->wakeup(gadget);
55723cd1385SRemy Bohmer }
55823cd1385SRemy Bohmer 
55923cd1385SRemy Bohmer /**
56023cd1385SRemy Bohmer  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
56123cd1385SRemy Bohmer  * @gadget:the device being declared as self-powered
56223cd1385SRemy Bohmer  *
56323cd1385SRemy Bohmer  * this affects the device status reported by the hardware driver
56423cd1385SRemy Bohmer  * to reflect that it now has a local power supply.
56523cd1385SRemy Bohmer  *
56623cd1385SRemy Bohmer  * returns zero on success, else negative errno.
56723cd1385SRemy Bohmer  */
5686142e0aeSVitaly Kuzmichev static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
56923cd1385SRemy Bohmer {
57023cd1385SRemy Bohmer 	if (!gadget->ops->set_selfpowered)
57123cd1385SRemy Bohmer 		return -EOPNOTSUPP;
57223cd1385SRemy Bohmer 	return gadget->ops->set_selfpowered(gadget, 1);
57323cd1385SRemy Bohmer }
57423cd1385SRemy Bohmer 
57523cd1385SRemy Bohmer /**
57623cd1385SRemy Bohmer  * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
57723cd1385SRemy Bohmer  * @gadget:the device being declared as bus-powered
57823cd1385SRemy Bohmer  *
57923cd1385SRemy Bohmer  * this affects the device status reported by the hardware driver.
58023cd1385SRemy Bohmer  * some hardware may not support bus-powered operation, in which
58123cd1385SRemy Bohmer  * case this feature's value can never change.
58223cd1385SRemy Bohmer  *
58323cd1385SRemy Bohmer  * returns zero on success, else negative errno.
58423cd1385SRemy Bohmer  */
5856142e0aeSVitaly Kuzmichev static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
58623cd1385SRemy Bohmer {
58723cd1385SRemy Bohmer 	if (!gadget->ops->set_selfpowered)
58823cd1385SRemy Bohmer 		return -EOPNOTSUPP;
58923cd1385SRemy Bohmer 	return gadget->ops->set_selfpowered(gadget, 0);
59023cd1385SRemy Bohmer }
59123cd1385SRemy Bohmer 
59223cd1385SRemy Bohmer /**
59323cd1385SRemy Bohmer  * usb_gadget_vbus_connect - Notify controller that VBUS is powered
59423cd1385SRemy Bohmer  * @gadget:The device which now has VBUS power.
59523cd1385SRemy Bohmer  *
59623cd1385SRemy Bohmer  * This call is used by a driver for an external transceiver (or GPIO)
59723cd1385SRemy Bohmer  * that detects a VBUS power session starting.  Common responses include
59823cd1385SRemy Bohmer  * resuming the controller, activating the D+ (or D-) pullup to let the
59923cd1385SRemy Bohmer  * host detect that a USB device is attached, and starting to draw power
60023cd1385SRemy Bohmer  * (8mA or possibly more, especially after SET_CONFIGURATION).
60123cd1385SRemy Bohmer  *
60223cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
60323cd1385SRemy Bohmer  */
6046142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
60523cd1385SRemy Bohmer {
60623cd1385SRemy Bohmer 	if (!gadget->ops->vbus_session)
60723cd1385SRemy Bohmer 		return -EOPNOTSUPP;
60823cd1385SRemy Bohmer 	return gadget->ops->vbus_session(gadget, 1);
60923cd1385SRemy Bohmer }
61023cd1385SRemy Bohmer 
61123cd1385SRemy Bohmer /**
61223cd1385SRemy Bohmer  * usb_gadget_vbus_draw - constrain controller's VBUS power usage
61323cd1385SRemy Bohmer  * @gadget:The device whose VBUS usage is being described
61423cd1385SRemy Bohmer  * @mA:How much current to draw, in milliAmperes.  This should be twice
61523cd1385SRemy Bohmer  *	the value listed in the configuration descriptor bMaxPower field.
61623cd1385SRemy Bohmer  *
61723cd1385SRemy Bohmer  * This call is used by gadget drivers during SET_CONFIGURATION calls,
61823cd1385SRemy Bohmer  * reporting how much power the device may consume.  For example, this
61923cd1385SRemy Bohmer  * could affect how quickly batteries are recharged.
62023cd1385SRemy Bohmer  *
62123cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
62223cd1385SRemy Bohmer  */
6236142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
62423cd1385SRemy Bohmer {
62523cd1385SRemy Bohmer 	if (!gadget->ops->vbus_draw)
62623cd1385SRemy Bohmer 		return -EOPNOTSUPP;
62723cd1385SRemy Bohmer 	return gadget->ops->vbus_draw(gadget, mA);
62823cd1385SRemy Bohmer }
62923cd1385SRemy Bohmer 
63023cd1385SRemy Bohmer /**
63123cd1385SRemy Bohmer  * usb_gadget_vbus_disconnect - notify controller about VBUS session end
63223cd1385SRemy Bohmer  * @gadget:the device whose VBUS supply is being described
63323cd1385SRemy Bohmer  *
63423cd1385SRemy Bohmer  * This call is used by a driver for an external transceiver (or GPIO)
63523cd1385SRemy Bohmer  * that detects a VBUS power session ending.  Common responses include
63623cd1385SRemy Bohmer  * reversing everything done in usb_gadget_vbus_connect().
63723cd1385SRemy Bohmer  *
63823cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
63923cd1385SRemy Bohmer  */
6406142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
64123cd1385SRemy Bohmer {
64223cd1385SRemy Bohmer 	if (!gadget->ops->vbus_session)
64323cd1385SRemy Bohmer 		return -EOPNOTSUPP;
64423cd1385SRemy Bohmer 	return gadget->ops->vbus_session(gadget, 0);
64523cd1385SRemy Bohmer }
64623cd1385SRemy Bohmer 
64723cd1385SRemy Bohmer /**
64823cd1385SRemy Bohmer  * usb_gadget_connect - software-controlled connect to USB host
64923cd1385SRemy Bohmer  * @gadget:the peripheral being connected
65023cd1385SRemy Bohmer  *
65123cd1385SRemy Bohmer  * Enables the D+ (or potentially D-) pullup.  The host will start
65223cd1385SRemy Bohmer  * enumerating this gadget when the pullup is active and a VBUS session
65323cd1385SRemy Bohmer  * is active (the link is powered).  This pullup is always enabled unless
65423cd1385SRemy Bohmer  * usb_gadget_disconnect() has been used to disable it.
65523cd1385SRemy Bohmer  *
65623cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
65723cd1385SRemy Bohmer  */
6586142e0aeSVitaly Kuzmichev static inline int usb_gadget_connect(struct usb_gadget *gadget)
65923cd1385SRemy Bohmer {
66023cd1385SRemy Bohmer 	if (!gadget->ops->pullup)
66123cd1385SRemy Bohmer 		return -EOPNOTSUPP;
66223cd1385SRemy Bohmer 	return gadget->ops->pullup(gadget, 1);
66323cd1385SRemy Bohmer }
66423cd1385SRemy Bohmer 
66523cd1385SRemy Bohmer /**
66623cd1385SRemy Bohmer  * usb_gadget_disconnect - software-controlled disconnect from USB host
66723cd1385SRemy Bohmer  * @gadget:the peripheral being disconnected
66823cd1385SRemy Bohmer  *
66923cd1385SRemy Bohmer  * Disables the D+ (or potentially D-) pullup, which the host may see
67023cd1385SRemy Bohmer  * as a disconnect (when a VBUS session is active).  Not all systems
67123cd1385SRemy Bohmer  * support software pullup controls.
67223cd1385SRemy Bohmer  *
67323cd1385SRemy Bohmer  * This routine may be used during the gadget driver bind() call to prevent
67423cd1385SRemy Bohmer  * the peripheral from ever being visible to the USB host, unless later
67523cd1385SRemy Bohmer  * usb_gadget_connect() is called.  For example, user mode components may
67623cd1385SRemy Bohmer  * need to be activated before the system can talk to hosts.
67723cd1385SRemy Bohmer  *
67823cd1385SRemy Bohmer  * Returns zero on success, else negative errno.
67923cd1385SRemy Bohmer  */
6806142e0aeSVitaly Kuzmichev static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
68123cd1385SRemy Bohmer {
68223cd1385SRemy Bohmer 	if (!gadget->ops->pullup)
68323cd1385SRemy Bohmer 		return -EOPNOTSUPP;
68423cd1385SRemy Bohmer 	return gadget->ops->pullup(gadget, 0);
68523cd1385SRemy Bohmer }
68623cd1385SRemy Bohmer 
68723cd1385SRemy Bohmer 
68823cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
68923cd1385SRemy Bohmer 
69023cd1385SRemy Bohmer /**
69123cd1385SRemy Bohmer  * struct usb_gadget_driver - driver for usb 'slave' devices
69223cd1385SRemy Bohmer  * @speed: Highest speed the driver handles.
69323cd1385SRemy Bohmer  * @bind: Invoked when the driver is bound to a gadget, usually
69423cd1385SRemy Bohmer  *	after registering the driver.
69523cd1385SRemy Bohmer  *	At that point, ep0 is fully initialized, and ep_list holds
69623cd1385SRemy Bohmer  *	the currently-available endpoints.
69723cd1385SRemy Bohmer  *	Called in a context that permits sleeping.
69823cd1385SRemy Bohmer  * @setup: Invoked for ep0 control requests that aren't handled by
69923cd1385SRemy Bohmer  *	the hardware level driver. Most calls must be handled by
70023cd1385SRemy Bohmer  *	the gadget driver, including descriptor and configuration
70123cd1385SRemy Bohmer  *	management.  The 16 bit members of the setup data are in
70223cd1385SRemy Bohmer  *	USB byte order. Called in_interrupt; this may not sleep.  Driver
70323cd1385SRemy Bohmer  *	queues a response to ep0, or returns negative to stall.
70423cd1385SRemy Bohmer  * @disconnect: Invoked after all transfers have been stopped,
70523cd1385SRemy Bohmer  *	when the host is disconnected.  May be called in_interrupt; this
70623cd1385SRemy Bohmer  *	may not sleep.  Some devices can't detect disconnect, so this might
70723cd1385SRemy Bohmer  *	not be called except as part of controller shutdown.
70823cd1385SRemy Bohmer  * @unbind: Invoked when the driver is unbound from a gadget,
70923cd1385SRemy Bohmer  *	usually from rmmod (after a disconnect is reported).
71023cd1385SRemy Bohmer  *	Called in a context that permits sleeping.
71123cd1385SRemy Bohmer  * @suspend: Invoked on USB suspend.  May be called in_interrupt.
71223cd1385SRemy Bohmer  * @resume: Invoked on USB resume.  May be called in_interrupt.
71323cd1385SRemy Bohmer  *
71423cd1385SRemy Bohmer  * Devices are disabled till a gadget driver successfully bind()s, which
71523cd1385SRemy Bohmer  * means the driver will handle setup() requests needed to enumerate (and
71623cd1385SRemy Bohmer  * meet "chapter 9" requirements) then do some useful work.
71723cd1385SRemy Bohmer  *
71823cd1385SRemy Bohmer  * If gadget->is_otg is true, the gadget driver must provide an OTG
71923cd1385SRemy Bohmer  * descriptor during enumeration, or else fail the bind() call.  In such
72023cd1385SRemy Bohmer  * cases, no USB traffic may flow until both bind() returns without
72123cd1385SRemy Bohmer  * having called usb_gadget_disconnect(), and the USB host stack has
72223cd1385SRemy Bohmer  * initialized.
72323cd1385SRemy Bohmer  *
72423cd1385SRemy Bohmer  * Drivers use hardware-specific knowledge to configure the usb hardware.
72523cd1385SRemy Bohmer  * endpoint addressing is only one of several hardware characteristics that
72623cd1385SRemy Bohmer  * are in descriptors the ep0 implementation returns from setup() calls.
72723cd1385SRemy Bohmer  *
72823cd1385SRemy Bohmer  * Except for ep0 implementation, most driver code shouldn't need change to
72923cd1385SRemy Bohmer  * run on top of different usb controllers.  It'll use endpoints set up by
73023cd1385SRemy Bohmer  * that ep0 implementation.
73123cd1385SRemy Bohmer  *
73223cd1385SRemy Bohmer  * The usb controller driver handles a few standard usb requests.  Those
73323cd1385SRemy Bohmer  * include set_address, and feature flags for devices, interfaces, and
73423cd1385SRemy Bohmer  * endpoints (the get_status, set_feature, and clear_feature requests).
73523cd1385SRemy Bohmer  *
73623cd1385SRemy Bohmer  * Accordingly, the driver's setup() callback must always implement all
73723cd1385SRemy Bohmer  * get_descriptor requests, returning at least a device descriptor and
73823cd1385SRemy Bohmer  * a configuration descriptor.  Drivers must make sure the endpoint
73923cd1385SRemy Bohmer  * descriptors match any hardware constraints. Some hardware also constrains
74023cd1385SRemy Bohmer  * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
74123cd1385SRemy Bohmer  *
74223cd1385SRemy Bohmer  * The driver's setup() callback must also implement set_configuration,
74323cd1385SRemy Bohmer  * and should also implement set_interface, get_configuration, and
74423cd1385SRemy Bohmer  * get_interface.  Setting a configuration (or interface) is where
74523cd1385SRemy Bohmer  * endpoints should be activated or (config 0) shut down.
74623cd1385SRemy Bohmer  *
74723cd1385SRemy Bohmer  * (Note that only the default control endpoint is supported.  Neither
74823cd1385SRemy Bohmer  * hosts nor devices generally support control traffic except to ep0.)
74923cd1385SRemy Bohmer  *
75023cd1385SRemy Bohmer  * Most devices will ignore USB suspend/resume operations, and so will
75123cd1385SRemy Bohmer  * not provide those callbacks.  However, some may need to change modes
75223cd1385SRemy Bohmer  * when the host is not longer directing those activities.  For example,
75323cd1385SRemy Bohmer  * local controls (buttons, dials, etc) may need to be re-enabled since
75423cd1385SRemy Bohmer  * the (remote) host can't do that any longer; or an error state might
75523cd1385SRemy Bohmer  * be cleared, to make the device behave identically whether or not
75623cd1385SRemy Bohmer  * power is maintained.
75723cd1385SRemy Bohmer  */
75823cd1385SRemy Bohmer struct usb_gadget_driver {
75923cd1385SRemy Bohmer 	enum usb_device_speed	speed;
76023cd1385SRemy Bohmer 	int			(*bind)(struct usb_gadget *);
76123cd1385SRemy Bohmer 	void			(*unbind)(struct usb_gadget *);
76223cd1385SRemy Bohmer 	int			(*setup)(struct usb_gadget *,
76323cd1385SRemy Bohmer 					const struct usb_ctrlrequest *);
76423cd1385SRemy Bohmer 	void			(*disconnect)(struct usb_gadget *);
76523cd1385SRemy Bohmer 	void			(*suspend)(struct usb_gadget *);
76623cd1385SRemy Bohmer 	void			(*resume)(struct usb_gadget *);
76723cd1385SRemy Bohmer };
76823cd1385SRemy Bohmer 
76923cd1385SRemy Bohmer 
77023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
77123cd1385SRemy Bohmer 
77223cd1385SRemy Bohmer /* driver modules register and unregister, as usual.
77323cd1385SRemy Bohmer  * these calls must be made in a context that can sleep.
77423cd1385SRemy Bohmer  *
77523cd1385SRemy Bohmer  * these will usually be implemented directly by the hardware-dependent
77623cd1385SRemy Bohmer  * usb bus interface driver, which will only support a single driver.
77723cd1385SRemy Bohmer  */
77823cd1385SRemy Bohmer 
77923cd1385SRemy Bohmer /**
78023cd1385SRemy Bohmer  * usb_gadget_register_driver - register a gadget driver
78123cd1385SRemy Bohmer  * @driver:the driver being registered
78223cd1385SRemy Bohmer  *
78323cd1385SRemy Bohmer  * Call this in your gadget driver's module initialization function,
78423cd1385SRemy Bohmer  * to tell the underlying usb controller driver about your driver.
78523cd1385SRemy Bohmer  * The driver's bind() function will be called to bind it to a
78623cd1385SRemy Bohmer  * gadget before this registration call returns.  It's expected that
78723cd1385SRemy Bohmer  * the bind() functions will be in init sections.
78823cd1385SRemy Bohmer  * This function must be called in a context that can sleep.
78923cd1385SRemy Bohmer  */
79023cd1385SRemy Bohmer int usb_gadget_register_driver(struct usb_gadget_driver *driver);
79123cd1385SRemy Bohmer 
79223cd1385SRemy Bohmer /**
79323cd1385SRemy Bohmer  * usb_gadget_unregister_driver - unregister a gadget driver
79423cd1385SRemy Bohmer  * @driver:the driver being unregistered
79523cd1385SRemy Bohmer  *
79623cd1385SRemy Bohmer  * Call this in your gadget driver's module cleanup function,
79723cd1385SRemy Bohmer  * to tell the underlying usb controller that your driver is
79823cd1385SRemy Bohmer  * going away.  If the controller is connected to a USB host,
79923cd1385SRemy Bohmer  * it will first disconnect().  The driver is also requested
80023cd1385SRemy Bohmer  * to unbind() and clean up any device state, before this procedure
80123cd1385SRemy Bohmer  * finally returns.  It's expected that the unbind() functions
80223cd1385SRemy Bohmer  * will in in exit sections, so may not be linked in some kernels.
80323cd1385SRemy Bohmer  * This function must be called in a context that can sleep.
80423cd1385SRemy Bohmer  */
80523cd1385SRemy Bohmer int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
80623cd1385SRemy Bohmer 
80723cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
80823cd1385SRemy Bohmer 
80923cd1385SRemy Bohmer /* utility to simplify dealing with string descriptors */
81023cd1385SRemy Bohmer 
81123cd1385SRemy Bohmer /**
81223cd1385SRemy Bohmer  * struct usb_string - wraps a C string and its USB id
81323cd1385SRemy Bohmer  * @id:the (nonzero) ID for this string
81423cd1385SRemy Bohmer  * @s:the string, in UTF-8 encoding
81523cd1385SRemy Bohmer  *
81623cd1385SRemy Bohmer  * If you're using usb_gadget_get_string(), use this to wrap a string
81723cd1385SRemy Bohmer  * together with its ID.
81823cd1385SRemy Bohmer  */
81923cd1385SRemy Bohmer struct usb_string {
82023cd1385SRemy Bohmer 	u8			id;
82123cd1385SRemy Bohmer 	const char		*s;
82223cd1385SRemy Bohmer };
82323cd1385SRemy Bohmer 
82423cd1385SRemy Bohmer /**
82523cd1385SRemy Bohmer  * struct usb_gadget_strings - a set of USB strings in a given language
82623cd1385SRemy Bohmer  * @language:identifies the strings' language (0x0409 for en-us)
82723cd1385SRemy Bohmer  * @strings:array of strings with their ids
82823cd1385SRemy Bohmer  *
82923cd1385SRemy Bohmer  * If you're using usb_gadget_get_string(), use this to wrap all the
83023cd1385SRemy Bohmer  * strings for a given language.
83123cd1385SRemy Bohmer  */
83223cd1385SRemy Bohmer struct usb_gadget_strings {
83323cd1385SRemy Bohmer 	u16			language;	/* 0x0409 for en-us */
83423cd1385SRemy Bohmer 	struct usb_string	*strings;
83523cd1385SRemy Bohmer };
83623cd1385SRemy Bohmer 
83723cd1385SRemy Bohmer /* put descriptor for string with that id into buf (buflen >= 256) */
83823cd1385SRemy Bohmer int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
83923cd1385SRemy Bohmer 
84023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
84123cd1385SRemy Bohmer 
84223cd1385SRemy Bohmer /* utility to simplify managing config descriptors */
84323cd1385SRemy Bohmer 
84423cd1385SRemy Bohmer /* write vector of descriptors into buffer */
84523cd1385SRemy Bohmer int usb_descriptor_fillbuf(void *, unsigned,
84623cd1385SRemy Bohmer 		const struct usb_descriptor_header **);
84723cd1385SRemy Bohmer 
84823cd1385SRemy Bohmer /* build config descriptor from single descriptor vector */
84923cd1385SRemy Bohmer int usb_gadget_config_buf(const struct usb_config_descriptor *config,
85023cd1385SRemy Bohmer 	void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
85123cd1385SRemy Bohmer 
85223cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
85323cd1385SRemy Bohmer 
85423cd1385SRemy Bohmer /* utility wrapping a simple endpoint selection policy */
85523cd1385SRemy Bohmer 
85623cd1385SRemy Bohmer extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
85723cd1385SRemy Bohmer 			struct usb_endpoint_descriptor *);
85823cd1385SRemy Bohmer 
85923cd1385SRemy Bohmer extern void usb_ep_autoconfig_reset(struct usb_gadget *);
86023cd1385SRemy Bohmer 
86123cd1385SRemy Bohmer extern int usb_gadget_handle_interrupts(void);
86223cd1385SRemy Bohmer 
86323cd1385SRemy Bohmer #endif	/* __LINUX_USB_GADGET_H */
864