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 *
14a187559eSBin Meng * 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>
22b95d4446SJean-Jacques Hiblot #include <usb.h>
230c06db59SHeiko Schocher #include <linux/compat.h>
2423cd1385SRemy Bohmer #include <linux/list.h>
2523cd1385SRemy Bohmer
2623cd1385SRemy Bohmer struct usb_ep;
2723cd1385SRemy Bohmer
2823cd1385SRemy Bohmer /**
2923cd1385SRemy Bohmer * struct usb_request - describes one i/o request
3023cd1385SRemy Bohmer * @buf: Buffer used for data. Always provide this; some controllers
3123cd1385SRemy Bohmer * only use PIO, or don't use DMA for some endpoints.
3223cd1385SRemy Bohmer * @dma: DMA address corresponding to 'buf'. If you don't set this
3323cd1385SRemy Bohmer * field, and the usb controller needs one, it is responsible
3423cd1385SRemy Bohmer * for mapping and unmapping the buffer.
35747a0a5bSKishon Vijay Abraham I * @stream_id: The stream id, when USB3.0 bulk streams are being used
3623cd1385SRemy Bohmer * @length: Length of that data
3723cd1385SRemy Bohmer * @no_interrupt: If true, hints that no completion irq is needed.
3823cd1385SRemy Bohmer * Helpful sometimes with deep request queues that are handled
3923cd1385SRemy Bohmer * directly by DMA controllers.
4023cd1385SRemy Bohmer * @zero: If true, when writing data, makes the last packet be "short"
4123cd1385SRemy Bohmer * by adding a zero length packet as needed;
4223cd1385SRemy Bohmer * @short_not_ok: When reading data, makes short packets be
4323cd1385SRemy Bohmer * treated as errors (queue stops advancing till cleanup).
4423cd1385SRemy Bohmer * @complete: Function called when request completes, so this request and
4523cd1385SRemy Bohmer * its buffer may be re-used.
4623cd1385SRemy Bohmer * Reads terminate with a short packet, or when the buffer fills,
4723cd1385SRemy Bohmer * whichever comes first. When writes terminate, some data bytes
4823cd1385SRemy Bohmer * will usually still be in flight (often in a hardware fifo).
4923cd1385SRemy Bohmer * Errors (for reads or writes) stop the queue from advancing
5023cd1385SRemy Bohmer * until the completion function returns, so that any transfers
5123cd1385SRemy Bohmer * invalidated by the error may first be dequeued.
5223cd1385SRemy Bohmer * @context: For use by the completion callback
5323cd1385SRemy Bohmer * @list: For use by the gadget driver.
5423cd1385SRemy Bohmer * @status: Reports completion code, zero or a negative errno.
5523cd1385SRemy Bohmer * Normally, faults block the transfer queue from advancing until
5623cd1385SRemy Bohmer * the completion callback returns.
5723cd1385SRemy Bohmer * Code "-ESHUTDOWN" indicates completion caused by device disconnect,
5823cd1385SRemy Bohmer * or when the driver disabled the endpoint.
5923cd1385SRemy Bohmer * @actual: Reports bytes transferred to/from the buffer. For reads (OUT
6023cd1385SRemy Bohmer * transfers) this may be less than the requested length. If the
6123cd1385SRemy Bohmer * short_not_ok flag is set, short reads are treated as errors
6223cd1385SRemy Bohmer * even when status otherwise indicates successful completion.
6323cd1385SRemy Bohmer * Note that for writes (IN transfers) some data bytes may still
6423cd1385SRemy Bohmer * reside in a device-side FIFO when the request is reported as
6523cd1385SRemy Bohmer * complete.
6623cd1385SRemy Bohmer *
6723cd1385SRemy Bohmer * These are allocated/freed through the endpoint they're used with. The
6823cd1385SRemy Bohmer * hardware's driver can add extra per-request data to the memory it returns,
6923cd1385SRemy Bohmer * which often avoids separate memory allocations (potential failures),
7023cd1385SRemy Bohmer * later when the request is queued.
7123cd1385SRemy Bohmer *
7223cd1385SRemy Bohmer * Request flags affect request handling, such as whether a zero length
7323cd1385SRemy Bohmer * packet is written (the "zero" flag), whether a short read should be
7423cd1385SRemy Bohmer * treated as an error (blocking request queue advance, the "short_not_ok"
7523cd1385SRemy Bohmer * flag), or hinting that an interrupt is not required (the "no_interrupt"
7623cd1385SRemy Bohmer * flag, for use with deep request queues).
7723cd1385SRemy Bohmer *
7823cd1385SRemy Bohmer * Bulk endpoints can use any size buffers, and can also be used for interrupt
7923cd1385SRemy Bohmer * transfers. interrupt-only endpoints can be much less functional.
806142e0aeSVitaly Kuzmichev *
816142e0aeSVitaly Kuzmichev * NOTE: this is analagous to 'struct urb' on the host side, except that
826142e0aeSVitaly Kuzmichev * it's thinner and promotes more pre-allocation.
8323cd1385SRemy Bohmer */
8423cd1385SRemy Bohmer
8523cd1385SRemy Bohmer struct usb_request {
8623cd1385SRemy Bohmer void *buf;
8723cd1385SRemy Bohmer unsigned length;
8823cd1385SRemy Bohmer dma_addr_t dma;
8923cd1385SRemy Bohmer
90747a0a5bSKishon Vijay Abraham I unsigned stream_id:16;
9123cd1385SRemy Bohmer unsigned no_interrupt:1;
9223cd1385SRemy Bohmer unsigned zero:1;
9323cd1385SRemy Bohmer unsigned short_not_ok:1;
9423cd1385SRemy Bohmer
9523cd1385SRemy Bohmer void (*complete)(struct usb_ep *ep,
9623cd1385SRemy Bohmer struct usb_request *req);
9723cd1385SRemy Bohmer void *context;
9823cd1385SRemy Bohmer struct list_head list;
9923cd1385SRemy Bohmer
10023cd1385SRemy Bohmer int status;
10123cd1385SRemy Bohmer unsigned actual;
10223cd1385SRemy Bohmer };
10323cd1385SRemy Bohmer
10423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
10523cd1385SRemy Bohmer
10623cd1385SRemy Bohmer /* endpoint-specific parts of the api to the usb controller hardware.
10723cd1385SRemy Bohmer * unlike the urb model, (de)multiplexing layers are not required.
10823cd1385SRemy Bohmer * (so this api could slash overhead if used on the host side...)
10923cd1385SRemy Bohmer *
11023cd1385SRemy Bohmer * note that device side usb controllers commonly differ in how many
11123cd1385SRemy Bohmer * endpoints they support, as well as their capabilities.
11223cd1385SRemy Bohmer */
11323cd1385SRemy Bohmer struct usb_ep_ops {
11423cd1385SRemy Bohmer int (*enable) (struct usb_ep *ep,
11523cd1385SRemy Bohmer const struct usb_endpoint_descriptor *desc);
11623cd1385SRemy Bohmer int (*disable) (struct usb_ep *ep);
11723cd1385SRemy Bohmer
11823cd1385SRemy Bohmer struct usb_request *(*alloc_request) (struct usb_ep *ep,
11923cd1385SRemy Bohmer gfp_t gfp_flags);
12023cd1385SRemy Bohmer void (*free_request) (struct usb_ep *ep, struct usb_request *req);
12123cd1385SRemy Bohmer
12223cd1385SRemy Bohmer int (*queue) (struct usb_ep *ep, struct usb_request *req,
12323cd1385SRemy Bohmer gfp_t gfp_flags);
12423cd1385SRemy Bohmer int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
12523cd1385SRemy Bohmer
12623cd1385SRemy Bohmer int (*set_halt) (struct usb_ep *ep, int value);
127747a0a5bSKishon Vijay Abraham I int (*set_wedge)(struct usb_ep *ep);
12823cd1385SRemy Bohmer int (*fifo_status) (struct usb_ep *ep);
12923cd1385SRemy Bohmer void (*fifo_flush) (struct usb_ep *ep);
13023cd1385SRemy Bohmer };
13123cd1385SRemy Bohmer
13223cd1385SRemy Bohmer /**
1330943909dSVignesh Raghavendra * struct usb_ep_caps - endpoint capabilities description
1340943909dSVignesh Raghavendra * @type_control:Endpoint supports control type (reserved for ep0).
1350943909dSVignesh Raghavendra * @type_iso:Endpoint supports isochronous transfers.
1360943909dSVignesh Raghavendra * @type_bulk:Endpoint supports bulk transfers.
1370943909dSVignesh Raghavendra * @type_int:Endpoint supports interrupt transfers.
1380943909dSVignesh Raghavendra * @dir_in:Endpoint supports IN direction.
1390943909dSVignesh Raghavendra * @dir_out:Endpoint supports OUT direction.
1400943909dSVignesh Raghavendra */
1410943909dSVignesh Raghavendra struct usb_ep_caps {
1420943909dSVignesh Raghavendra unsigned type_control:1;
1430943909dSVignesh Raghavendra unsigned type_iso:1;
1440943909dSVignesh Raghavendra unsigned type_bulk:1;
1450943909dSVignesh Raghavendra unsigned type_int:1;
1460943909dSVignesh Raghavendra unsigned dir_in:1;
1470943909dSVignesh Raghavendra unsigned dir_out:1;
1480943909dSVignesh Raghavendra };
1490943909dSVignesh Raghavendra
1500943909dSVignesh Raghavendra /**
15123cd1385SRemy Bohmer * struct usb_ep - device side representation of USB endpoint
15223cd1385SRemy Bohmer * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
15323cd1385SRemy Bohmer * @ops: Function pointers used to access hardware-specific operations.
15423cd1385SRemy Bohmer * @ep_list:the gadget's ep_list holds all of its endpoints
1550943909dSVignesh Raghavendra * @caps:The structure describing types and directions supported by endoint.
15623cd1385SRemy Bohmer * @maxpacket:The maximum packet size used on this endpoint. The initial
15723cd1385SRemy Bohmer * value can sometimes be reduced (hardware allowing), according to
15823cd1385SRemy Bohmer * the endpoint descriptor used to configure the endpoint.
159747a0a5bSKishon Vijay Abraham I * @maxpacket_limit:The maximum packet size value which can be handled by this
160747a0a5bSKishon Vijay Abraham I * endpoint. It's set once by UDC driver when endpoint is initialized, and
161747a0a5bSKishon Vijay Abraham I * should not be changed. Should not be confused with maxpacket.
162747a0a5bSKishon Vijay Abraham I * @max_streams: The maximum number of streams supported
163747a0a5bSKishon Vijay Abraham I * by this EP (0 - 16, actual number is 2^n)
164747a0a5bSKishon Vijay Abraham I * @maxburst: the maximum number of bursts supported by this EP (for usb3)
16523cd1385SRemy Bohmer * @driver_data:for use by the gadget driver. all other fields are
16623cd1385SRemy Bohmer * read-only to gadget drivers.
167747a0a5bSKishon Vijay Abraham I * @desc: endpoint descriptor. This pointer is set before the endpoint is
168747a0a5bSKishon Vijay Abraham I * enabled and remains valid until the endpoint is disabled.
169747a0a5bSKishon Vijay Abraham I * @comp_desc: In case of SuperSpeed support, this is the endpoint companion
170747a0a5bSKishon Vijay Abraham I * descriptor that is used to configure the endpoint
17123cd1385SRemy Bohmer *
17223cd1385SRemy Bohmer * the bus controller driver lists all the general purpose endpoints in
17323cd1385SRemy Bohmer * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list,
17423cd1385SRemy Bohmer * and is accessed only in response to a driver setup() callback.
17523cd1385SRemy Bohmer */
17623cd1385SRemy Bohmer struct usb_ep {
17723cd1385SRemy Bohmer void *driver_data;
17823cd1385SRemy Bohmer const char *name;
17923cd1385SRemy Bohmer const struct usb_ep_ops *ops;
18023cd1385SRemy Bohmer struct list_head ep_list;
1810943909dSVignesh Raghavendra struct usb_ep_caps caps;
18223cd1385SRemy Bohmer unsigned maxpacket:16;
183747a0a5bSKishon Vijay Abraham I unsigned maxpacket_limit:16;
184747a0a5bSKishon Vijay Abraham I unsigned max_streams:16;
185747a0a5bSKishon Vijay Abraham I unsigned maxburst:5;
186747a0a5bSKishon Vijay Abraham I const struct usb_endpoint_descriptor *desc;
187747a0a5bSKishon Vijay Abraham I const struct usb_ss_ep_comp_descriptor *comp_desc;
18823cd1385SRemy Bohmer };
18923cd1385SRemy Bohmer
19023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
19123cd1385SRemy Bohmer
19223cd1385SRemy Bohmer /**
193747a0a5bSKishon Vijay Abraham I * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
194747a0a5bSKishon Vijay Abraham I * @ep:the endpoint being configured
195747a0a5bSKishon Vijay Abraham I * @maxpacket_limit:value of maximum packet size limit
196747a0a5bSKishon Vijay Abraham I *
197747a0a5bSKishon Vijay Abraham I * This function shoud be used only in UDC drivers to initialize endpoint
198747a0a5bSKishon Vijay Abraham I * (usually in probe function).
199747a0a5bSKishon Vijay Abraham I */
usb_ep_set_maxpacket_limit(struct usb_ep * ep,unsigned maxpacket_limit)200747a0a5bSKishon Vijay Abraham I static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
201747a0a5bSKishon Vijay Abraham I unsigned maxpacket_limit)
202747a0a5bSKishon Vijay Abraham I {
203747a0a5bSKishon Vijay Abraham I ep->maxpacket_limit = maxpacket_limit;
204747a0a5bSKishon Vijay Abraham I ep->maxpacket = maxpacket_limit;
205747a0a5bSKishon Vijay Abraham I }
206747a0a5bSKishon Vijay Abraham I
207747a0a5bSKishon Vijay Abraham I /**
20823cd1385SRemy Bohmer * usb_ep_enable - configure endpoint, making it usable
20923cd1385SRemy Bohmer * @ep:the endpoint being configured. may not be the endpoint named "ep0".
21023cd1385SRemy Bohmer * drivers discover endpoints through the ep_list of a usb_gadget.
21123cd1385SRemy Bohmer * @desc:descriptor for desired behavior. caller guarantees this pointer
21223cd1385SRemy Bohmer * remains valid until the endpoint is disabled; the data byte order
21323cd1385SRemy Bohmer * is little-endian (usb-standard).
21423cd1385SRemy Bohmer *
21523cd1385SRemy Bohmer * when configurations are set, or when interface settings change, the driver
21623cd1385SRemy Bohmer * will enable or disable the relevant endpoints. while it is enabled, an
21723cd1385SRemy Bohmer * endpoint may be used for i/o until the driver receives a disconnect() from
21823cd1385SRemy Bohmer * the host or until the endpoint is disabled.
21923cd1385SRemy Bohmer *
22023cd1385SRemy Bohmer * the ep0 implementation (which calls this routine) must ensure that the
22123cd1385SRemy Bohmer * hardware capabilities of each endpoint match the descriptor provided
22223cd1385SRemy Bohmer * for it. for example, an endpoint named "ep2in-bulk" would be usable
22323cd1385SRemy Bohmer * for interrupt transfers as well as bulk, but it likely couldn't be used
22423cd1385SRemy Bohmer * for iso transfers or for endpoint 14. some endpoints are fully
22523cd1385SRemy Bohmer * configurable, with more generic names like "ep-a". (remember that for
22623cd1385SRemy Bohmer * USB, "in" means "towards the USB master".)
22723cd1385SRemy Bohmer *
22823cd1385SRemy Bohmer * returns zero, or a negative error code.
22923cd1385SRemy Bohmer */
usb_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)2306142e0aeSVitaly Kuzmichev static inline int usb_ep_enable(struct usb_ep *ep,
2316142e0aeSVitaly Kuzmichev const struct usb_endpoint_descriptor *desc)
23223cd1385SRemy Bohmer {
23323cd1385SRemy Bohmer return ep->ops->enable(ep, desc);
23423cd1385SRemy Bohmer }
23523cd1385SRemy Bohmer
23623cd1385SRemy Bohmer /**
23723cd1385SRemy Bohmer * usb_ep_disable - endpoint is no longer usable
23823cd1385SRemy Bohmer * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
23923cd1385SRemy Bohmer *
24023cd1385SRemy Bohmer * no other task may be using this endpoint when this is called.
24123cd1385SRemy Bohmer * any pending and uncompleted requests will complete with status
24223cd1385SRemy Bohmer * indicating disconnect (-ESHUTDOWN) before this call returns.
24323cd1385SRemy Bohmer * gadget drivers must call usb_ep_enable() again before queueing
24423cd1385SRemy Bohmer * requests to the endpoint.
24523cd1385SRemy Bohmer *
24623cd1385SRemy Bohmer * returns zero, or a negative error code.
24723cd1385SRemy Bohmer */
usb_ep_disable(struct usb_ep * ep)2486142e0aeSVitaly Kuzmichev static inline int usb_ep_disable(struct usb_ep *ep)
24923cd1385SRemy Bohmer {
25023cd1385SRemy Bohmer return ep->ops->disable(ep);
25123cd1385SRemy Bohmer }
25223cd1385SRemy Bohmer
25323cd1385SRemy Bohmer /**
25423cd1385SRemy Bohmer * usb_ep_alloc_request - allocate a request object to use with this endpoint
25523cd1385SRemy Bohmer * @ep:the endpoint to be used with with the request
25623cd1385SRemy Bohmer * @gfp_flags:GFP_* flags to use
25723cd1385SRemy Bohmer *
25823cd1385SRemy Bohmer * Request objects must be allocated with this call, since they normally
25923cd1385SRemy Bohmer * need controller-specific setup and may even need endpoint-specific
26023cd1385SRemy Bohmer * resources such as allocation of DMA descriptors.
26123cd1385SRemy Bohmer * Requests may be submitted with usb_ep_queue(), and receive a single
26223cd1385SRemy Bohmer * completion callback. Free requests with usb_ep_free_request(), when
26323cd1385SRemy Bohmer * they are no longer needed.
26423cd1385SRemy Bohmer *
26523cd1385SRemy Bohmer * Returns the request, or null if one could not be allocated.
26623cd1385SRemy Bohmer */
usb_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)2676142e0aeSVitaly Kuzmichev static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
2686142e0aeSVitaly Kuzmichev gfp_t gfp_flags)
26923cd1385SRemy Bohmer {
27023cd1385SRemy Bohmer return ep->ops->alloc_request(ep, gfp_flags);
27123cd1385SRemy Bohmer }
27223cd1385SRemy Bohmer
27323cd1385SRemy Bohmer /**
27423cd1385SRemy Bohmer * usb_ep_free_request - frees a request object
27523cd1385SRemy Bohmer * @ep:the endpoint associated with the request
27623cd1385SRemy Bohmer * @req:the request being freed
27723cd1385SRemy Bohmer *
27823cd1385SRemy Bohmer * Reverses the effect of usb_ep_alloc_request().
27923cd1385SRemy Bohmer * Caller guarantees the request is not queued, and that it will
28023cd1385SRemy Bohmer * no longer be requeued (or otherwise used).
28123cd1385SRemy Bohmer */
usb_ep_free_request(struct usb_ep * ep,struct usb_request * req)2826142e0aeSVitaly Kuzmichev static inline void usb_ep_free_request(struct usb_ep *ep,
2836142e0aeSVitaly Kuzmichev struct usb_request *req)
28423cd1385SRemy Bohmer {
28523cd1385SRemy Bohmer ep->ops->free_request(ep, req);
28623cd1385SRemy Bohmer }
28723cd1385SRemy Bohmer
28823cd1385SRemy Bohmer /**
28923cd1385SRemy Bohmer * usb_ep_queue - queues (submits) an I/O request to an endpoint.
29023cd1385SRemy Bohmer * @ep:the endpoint associated with the request
29123cd1385SRemy Bohmer * @req:the request being submitted
29223cd1385SRemy Bohmer * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
29323cd1385SRemy Bohmer * pre-allocate all necessary memory with the request.
29423cd1385SRemy Bohmer *
29523cd1385SRemy Bohmer * This tells the device controller to perform the specified request through
29623cd1385SRemy Bohmer * that endpoint (reading or writing a buffer). When the request completes,
29723cd1385SRemy Bohmer * including being canceled by usb_ep_dequeue(), the request's completion
29823cd1385SRemy Bohmer * routine is called to return the request to the driver. Any endpoint
29923cd1385SRemy Bohmer * (except control endpoints like ep0) may have more than one transfer
30023cd1385SRemy Bohmer * request queued; they complete in FIFO order. Once a gadget driver
30123cd1385SRemy Bohmer * submits a request, that request may not be examined or modified until it
30223cd1385SRemy Bohmer * is given back to that driver through the completion callback.
30323cd1385SRemy Bohmer *
30423cd1385SRemy Bohmer * Each request is turned into one or more packets. The controller driver
30523cd1385SRemy Bohmer * never merges adjacent requests into the same packet. OUT transfers
30623cd1385SRemy Bohmer * will sometimes use data that's already buffered in the hardware.
30723cd1385SRemy Bohmer * Drivers can rely on the fact that the first byte of the request's buffer
30823cd1385SRemy Bohmer * always corresponds to the first byte of some USB packet, for both
30923cd1385SRemy Bohmer * IN and OUT transfers.
31023cd1385SRemy Bohmer *
31123cd1385SRemy Bohmer * Bulk endpoints can queue any amount of data; the transfer is packetized
31223cd1385SRemy Bohmer * automatically. The last packet will be short if the request doesn't fill it
31323cd1385SRemy Bohmer * out completely. Zero length packets (ZLPs) should be avoided in portable
31423cd1385SRemy Bohmer * protocols since not all usb hardware can successfully handle zero length
31523cd1385SRemy Bohmer * packets. (ZLPs may be explicitly written, and may be implicitly written if
31623cd1385SRemy Bohmer * the request 'zero' flag is set.) Bulk endpoints may also be used
31723cd1385SRemy Bohmer * for interrupt transfers; but the reverse is not true, and some endpoints
31823cd1385SRemy Bohmer * won't support every interrupt transfer. (Such as 768 byte packets.)
31923cd1385SRemy Bohmer *
32023cd1385SRemy Bohmer * Interrupt-only endpoints are less functional than bulk endpoints, for
32123cd1385SRemy Bohmer * example by not supporting queueing or not handling buffers that are
32223cd1385SRemy Bohmer * larger than the endpoint's maxpacket size. They may also treat data
32323cd1385SRemy Bohmer * toggle differently.
32423cd1385SRemy Bohmer *
32523cd1385SRemy Bohmer * Control endpoints ... after getting a setup() callback, the driver queues
32623cd1385SRemy Bohmer * one response (even if it would be zero length). That enables the
32723cd1385SRemy Bohmer * status ack, after transfering data as specified in the response. Setup
32823cd1385SRemy Bohmer * functions may return negative error codes to generate protocol stalls.
32923cd1385SRemy Bohmer * (Note that some USB device controllers disallow protocol stall responses
33023cd1385SRemy Bohmer * in some cases.) When control responses are deferred (the response is
33123cd1385SRemy Bohmer * written after the setup callback returns), then usb_ep_set_halt() may be
33223cd1385SRemy Bohmer * used on ep0 to trigger protocol stalls.
33323cd1385SRemy Bohmer *
33423cd1385SRemy Bohmer * For periodic endpoints, like interrupt or isochronous ones, the usb host
33523cd1385SRemy Bohmer * arranges to poll once per interval, and the gadget driver usually will
33623cd1385SRemy Bohmer * have queued some data to transfer at that time.
33723cd1385SRemy Bohmer *
33823cd1385SRemy Bohmer * Returns zero, or a negative error code. Endpoints that are not enabled
33923cd1385SRemy Bohmer * report errors; errors will also be
34023cd1385SRemy Bohmer * reported when the usb peripheral is disconnected.
34123cd1385SRemy Bohmer */
usb_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)3426142e0aeSVitaly Kuzmichev static inline int usb_ep_queue(struct usb_ep *ep,
3436142e0aeSVitaly Kuzmichev struct usb_request *req, gfp_t gfp_flags)
34423cd1385SRemy Bohmer {
34523cd1385SRemy Bohmer return ep->ops->queue(ep, req, gfp_flags);
34623cd1385SRemy Bohmer }
34723cd1385SRemy Bohmer
34823cd1385SRemy Bohmer /**
34923cd1385SRemy Bohmer * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
35023cd1385SRemy Bohmer * @ep:the endpoint associated with the request
35123cd1385SRemy Bohmer * @req:the request being canceled
35223cd1385SRemy Bohmer *
35323cd1385SRemy Bohmer * if the request is still active on the endpoint, it is dequeued and its
35423cd1385SRemy Bohmer * completion routine is called (with status -ECONNRESET); else a negative
35523cd1385SRemy Bohmer * error code is returned.
35623cd1385SRemy Bohmer *
35723cd1385SRemy Bohmer * note that some hardware can't clear out write fifos (to unlink the request
35823cd1385SRemy Bohmer * at the head of the queue) except as part of disconnecting from usb. such
35923cd1385SRemy Bohmer * restrictions prevent drivers from supporting configuration changes,
36023cd1385SRemy Bohmer * even to configuration zero (a "chapter 9" requirement).
36123cd1385SRemy Bohmer */
usb_ep_dequeue(struct usb_ep * ep,struct usb_request * req)36223cd1385SRemy Bohmer static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
36323cd1385SRemy Bohmer {
36423cd1385SRemy Bohmer return ep->ops->dequeue(ep, req);
36523cd1385SRemy Bohmer }
36623cd1385SRemy Bohmer
36723cd1385SRemy Bohmer /**
36823cd1385SRemy Bohmer * usb_ep_set_halt - sets the endpoint halt feature.
36923cd1385SRemy Bohmer * @ep: the non-isochronous endpoint being stalled
37023cd1385SRemy Bohmer *
37123cd1385SRemy Bohmer * Use this to stall an endpoint, perhaps as an error report.
37223cd1385SRemy Bohmer * Except for control endpoints,
37323cd1385SRemy Bohmer * the endpoint stays halted (will not stream any data) until the host
37423cd1385SRemy Bohmer * clears this feature; drivers may need to empty the endpoint's request
37523cd1385SRemy Bohmer * queue first, to make sure no inappropriate transfers happen.
37623cd1385SRemy Bohmer *
37723cd1385SRemy Bohmer * Note that while an endpoint CLEAR_FEATURE will be invisible to the
37823cd1385SRemy Bohmer * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
37923cd1385SRemy Bohmer * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
38023cd1385SRemy Bohmer * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
38123cd1385SRemy Bohmer *
38223cd1385SRemy Bohmer * Returns zero, or a negative error code. On success, this call sets
38323cd1385SRemy Bohmer * underlying hardware state that blocks data transfers.
38423cd1385SRemy Bohmer * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
38523cd1385SRemy Bohmer * transfer requests are still queued, or if the controller hardware
38623cd1385SRemy Bohmer * (usually a FIFO) still holds bytes that the host hasn't collected.
38723cd1385SRemy Bohmer */
usb_ep_set_halt(struct usb_ep * ep)3886142e0aeSVitaly Kuzmichev static inline int usb_ep_set_halt(struct usb_ep *ep)
38923cd1385SRemy Bohmer {
39023cd1385SRemy Bohmer return ep->ops->set_halt(ep, 1);
39123cd1385SRemy Bohmer }
39223cd1385SRemy Bohmer
39323cd1385SRemy Bohmer /**
39423cd1385SRemy Bohmer * usb_ep_clear_halt - clears endpoint halt, and resets toggle
39523cd1385SRemy Bohmer * @ep:the bulk or interrupt endpoint being reset
39623cd1385SRemy Bohmer *
39723cd1385SRemy Bohmer * Use this when responding to the standard usb "set interface" request,
39823cd1385SRemy Bohmer * for endpoints that aren't reconfigured, after clearing any other state
39923cd1385SRemy Bohmer * in the endpoint's i/o queue.
40023cd1385SRemy Bohmer *
40123cd1385SRemy Bohmer * Returns zero, or a negative error code. On success, this call clears
40223cd1385SRemy Bohmer * the underlying hardware state reflecting endpoint halt and data toggle.
40323cd1385SRemy Bohmer * Note that some hardware can't support this request (like pxa2xx_udc),
40423cd1385SRemy Bohmer * and accordingly can't correctly implement interface altsettings.
40523cd1385SRemy Bohmer */
usb_ep_clear_halt(struct usb_ep * ep)4066142e0aeSVitaly Kuzmichev static inline int usb_ep_clear_halt(struct usb_ep *ep)
40723cd1385SRemy Bohmer {
40823cd1385SRemy Bohmer return ep->ops->set_halt(ep, 0);
40923cd1385SRemy Bohmer }
41023cd1385SRemy Bohmer
41123cd1385SRemy Bohmer /**
41223cd1385SRemy Bohmer * usb_ep_fifo_status - returns number of bytes in fifo, or error
41323cd1385SRemy Bohmer * @ep: the endpoint whose fifo status is being checked.
41423cd1385SRemy Bohmer *
41523cd1385SRemy Bohmer * FIFO endpoints may have "unclaimed data" in them in certain cases,
41623cd1385SRemy Bohmer * such as after aborted transfers. Hosts may not have collected all
41723cd1385SRemy Bohmer * the IN data written by the gadget driver (and reported by a request
41823cd1385SRemy Bohmer * completion). The gadget driver may not have collected all the data
41923cd1385SRemy Bohmer * written OUT to it by the host. Drivers that need precise handling for
42023cd1385SRemy Bohmer * fault reporting or recovery may need to use this call.
42123cd1385SRemy Bohmer *
42223cd1385SRemy Bohmer * This returns the number of such bytes in the fifo, or a negative
42323cd1385SRemy Bohmer * errno if the endpoint doesn't use a FIFO or doesn't support such
42423cd1385SRemy Bohmer * precise handling.
42523cd1385SRemy Bohmer */
usb_ep_fifo_status(struct usb_ep * ep)4266142e0aeSVitaly Kuzmichev static inline int usb_ep_fifo_status(struct usb_ep *ep)
42723cd1385SRemy Bohmer {
42823cd1385SRemy Bohmer if (ep->ops->fifo_status)
42923cd1385SRemy Bohmer return ep->ops->fifo_status(ep);
43023cd1385SRemy Bohmer else
43123cd1385SRemy Bohmer return -EOPNOTSUPP;
43223cd1385SRemy Bohmer }
43323cd1385SRemy Bohmer
43423cd1385SRemy Bohmer /**
43523cd1385SRemy Bohmer * usb_ep_fifo_flush - flushes contents of a fifo
43623cd1385SRemy Bohmer * @ep: the endpoint whose fifo is being flushed.
43723cd1385SRemy Bohmer *
43823cd1385SRemy Bohmer * This call may be used to flush the "unclaimed data" that may exist in
43923cd1385SRemy Bohmer * an endpoint fifo after abnormal transaction terminations. The call
44023cd1385SRemy Bohmer * must never be used except when endpoint is not being used for any
44123cd1385SRemy Bohmer * protocol translation.
44223cd1385SRemy Bohmer */
usb_ep_fifo_flush(struct usb_ep * ep)4436142e0aeSVitaly Kuzmichev static inline void usb_ep_fifo_flush(struct usb_ep *ep)
44423cd1385SRemy Bohmer {
44523cd1385SRemy Bohmer if (ep->ops->fifo_flush)
44623cd1385SRemy Bohmer ep->ops->fifo_flush(ep);
44723cd1385SRemy Bohmer }
44823cd1385SRemy Bohmer
44923cd1385SRemy Bohmer
45023cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
45126dd3474SWilliam Wu #define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */
45226dd3474SWilliam Wu #define USB_DEFAULT_U2_DEV_EXIT_LAT 0x1F4 /* Less then 500 microsec */
45323cd1385SRemy Bohmer
45423cd1385SRemy Bohmer struct usb_gadget;
45545d9337eSKishon Vijay Abraham I struct usb_gadget_driver;
45623cd1385SRemy Bohmer
45723cd1385SRemy Bohmer /* the rest of the api to the controller hardware: device operations,
45823cd1385SRemy Bohmer * which don't involve endpoints (or i/o).
45923cd1385SRemy Bohmer */
46023cd1385SRemy Bohmer struct usb_gadget_ops {
46123cd1385SRemy Bohmer int (*get_frame)(struct usb_gadget *);
46223cd1385SRemy Bohmer int (*wakeup)(struct usb_gadget *);
46323cd1385SRemy Bohmer int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
46423cd1385SRemy Bohmer int (*vbus_session) (struct usb_gadget *, int is_active);
46523cd1385SRemy Bohmer int (*vbus_draw) (struct usb_gadget *, unsigned mA);
46623cd1385SRemy Bohmer int (*pullup) (struct usb_gadget *, int is_on);
46723cd1385SRemy Bohmer int (*ioctl)(struct usb_gadget *,
46823cd1385SRemy Bohmer unsigned code, unsigned long param);
46945d9337eSKishon Vijay Abraham I int (*udc_start)(struct usb_gadget *,
47045d9337eSKishon Vijay Abraham I struct usb_gadget_driver *);
47145d9337eSKishon Vijay Abraham I int (*udc_stop)(struct usb_gadget *);
4720943909dSVignesh Raghavendra struct usb_ep *(*match_ep)(struct usb_gadget *,
4730943909dSVignesh Raghavendra struct usb_endpoint_descriptor *,
4740943909dSVignesh Raghavendra struct usb_ss_ep_comp_descriptor *);
475*16dff785SSherry Sun void (*udc_set_speed)(struct usb_gadget *gadget,
476*16dff785SSherry Sun enum usb_device_speed);
47723cd1385SRemy Bohmer };
47823cd1385SRemy Bohmer
47923cd1385SRemy Bohmer /**
48023cd1385SRemy Bohmer * struct usb_gadget - represents a usb slave device
48123cd1385SRemy Bohmer * @ops: Function pointers used to access hardware-specific operations.
48223cd1385SRemy Bohmer * @ep0: Endpoint zero, used when reading or writing responses to
48323cd1385SRemy Bohmer * driver setup() requests
48423cd1385SRemy Bohmer * @ep_list: List of other endpoints supported by the device.
48523cd1385SRemy Bohmer * @speed: Speed of current connection to USB host.
486747a0a5bSKishon Vijay Abraham I * @max_speed: Maximal speed the UDC can handle. UDC must support this
487747a0a5bSKishon Vijay Abraham I * and all slower speeds.
488472d5460SYork Sun * @is_dualspeed: true if the controller supports both high and full speed
48923cd1385SRemy Bohmer * operation. If it does, the gadget driver must also support both.
490472d5460SYork Sun * @is_otg: true if the USB device port uses a Mini-AB jack, so that the
49123cd1385SRemy Bohmer * gadget driver must provide a USB OTG descriptor.
492472d5460SYork Sun * @is_a_peripheral: false unless is_otg, the "A" end of a USB cable
49323cd1385SRemy Bohmer * is in the Mini-AB jack, and HNP has been used to switch roles
49423cd1385SRemy Bohmer * so that the "A" device currently acts as A-Peripheral, not A-Host.
49523cd1385SRemy Bohmer * @a_hnp_support: OTG device feature flag, indicating that the A-Host
49623cd1385SRemy Bohmer * supports HNP at this port.
49723cd1385SRemy Bohmer * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
49823cd1385SRemy Bohmer * only supports HNP on a different root port.
49923cd1385SRemy Bohmer * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
50023cd1385SRemy Bohmer * enabled HNP support.
50123cd1385SRemy Bohmer * @name: Identifies the controller hardware type. Used in diagnostics
50223cd1385SRemy Bohmer * and sometimes configuration.
50323cd1385SRemy Bohmer * @dev: Driver model state for this abstract device.
504747a0a5bSKishon Vijay Abraham I * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
505747a0a5bSKishon Vijay Abraham I * MaxPacketSize.
50623cd1385SRemy Bohmer *
50723cd1385SRemy Bohmer * Gadgets have a mostly-portable "gadget driver" implementing device
50823cd1385SRemy Bohmer * functions, handling all usb configurations and interfaces. Gadget
50923cd1385SRemy Bohmer * drivers talk to hardware-specific code indirectly, through ops vectors.
51023cd1385SRemy Bohmer * That insulates the gadget driver from hardware details, and packages
51123cd1385SRemy Bohmer * the hardware endpoints through generic i/o queues. The "usb_gadget"
51223cd1385SRemy Bohmer * and "usb_ep" interfaces provide that insulation from the hardware.
51323cd1385SRemy Bohmer *
51423cd1385SRemy Bohmer * Except for the driver data, all fields in this structure are
51523cd1385SRemy Bohmer * read-only to the gadget driver. That driver data is part of the
51623cd1385SRemy Bohmer * "driver model" infrastructure in 2.6 (and later) kernels, and for
51723cd1385SRemy Bohmer * earlier systems is grouped in a similar structure that's not known
51823cd1385SRemy Bohmer * to the rest of the kernel.
51923cd1385SRemy Bohmer *
52023cd1385SRemy Bohmer * Values of the three OTG device feature flags are updated before the
52123cd1385SRemy Bohmer * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
52223cd1385SRemy Bohmer * driver suspend() calls. They are valid only when is_otg, and when the
52323cd1385SRemy Bohmer * device is acting as a B-Peripheral (so is_a_peripheral is false).
52423cd1385SRemy Bohmer */
52523cd1385SRemy Bohmer struct usb_gadget {
52623cd1385SRemy Bohmer /* readonly to gadget driver */
52723cd1385SRemy Bohmer const struct usb_gadget_ops *ops;
52823cd1385SRemy Bohmer struct usb_ep *ep0;
52923cd1385SRemy Bohmer struct list_head ep_list; /* of usb_ep */
53023cd1385SRemy Bohmer enum usb_device_speed speed;
531747a0a5bSKishon Vijay Abraham I enum usb_device_speed max_speed;
53245d9337eSKishon Vijay Abraham I enum usb_device_state state;
53323cd1385SRemy Bohmer unsigned is_dualspeed:1;
53423cd1385SRemy Bohmer unsigned is_otg:1;
53523cd1385SRemy Bohmer unsigned is_a_peripheral:1;
53623cd1385SRemy Bohmer unsigned b_hnp_enable:1;
53723cd1385SRemy Bohmer unsigned a_hnp_support:1;
53823cd1385SRemy Bohmer unsigned a_alt_hnp_support:1;
53923cd1385SRemy Bohmer const char *name;
54023cd1385SRemy Bohmer struct device dev;
541747a0a5bSKishon Vijay Abraham I unsigned quirk_ep_out_aligned_size:1;
54223cd1385SRemy Bohmer };
54323cd1385SRemy Bohmer
set_gadget_data(struct usb_gadget * gadget,void * data)54423cd1385SRemy Bohmer static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
54523cd1385SRemy Bohmer {
54623cd1385SRemy Bohmer gadget->dev.driver_data = data;
54723cd1385SRemy Bohmer }
54823cd1385SRemy Bohmer
get_gadget_data(struct usb_gadget * gadget)54923cd1385SRemy Bohmer static inline void *get_gadget_data(struct usb_gadget *gadget)
55023cd1385SRemy Bohmer {
55123cd1385SRemy Bohmer return gadget->dev.driver_data;
55223cd1385SRemy Bohmer }
55323cd1385SRemy Bohmer
dev_to_usb_gadget(struct device * dev)5544eec44d8SLukasz Majewski static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
5554eec44d8SLukasz Majewski {
5564eec44d8SLukasz Majewski return container_of(dev, struct usb_gadget, dev);
5574eec44d8SLukasz Majewski }
5584eec44d8SLukasz Majewski
55923cd1385SRemy Bohmer /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
56023cd1385SRemy Bohmer #define gadget_for_each_ep(tmp, gadget) \
56123cd1385SRemy Bohmer list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
56223cd1385SRemy Bohmer
56323cd1385SRemy Bohmer
56423cd1385SRemy Bohmer /**
56523cd1385SRemy Bohmer * gadget_is_dualspeed - return true iff the hardware handles high speed
56623cd1385SRemy Bohmer * @g: controller that might support both high and full speeds
56723cd1385SRemy Bohmer */
gadget_is_dualspeed(struct usb_gadget * g)56823cd1385SRemy Bohmer static inline int gadget_is_dualspeed(struct usb_gadget *g)
56923cd1385SRemy Bohmer {
57023cd1385SRemy Bohmer #ifdef CONFIG_USB_GADGET_DUALSPEED
57123cd1385SRemy Bohmer /* runtime test would check "g->is_dualspeed" ... that might be
57223cd1385SRemy Bohmer * useful to work around hardware bugs, but is mostly pointless
57323cd1385SRemy Bohmer */
57423cd1385SRemy Bohmer return 1;
57523cd1385SRemy Bohmer #else
57623cd1385SRemy Bohmer return 0;
57723cd1385SRemy Bohmer #endif
57823cd1385SRemy Bohmer }
57923cd1385SRemy Bohmer
58023cd1385SRemy Bohmer /**
58126dd3474SWilliam Wu * gadget_is_superspeed() - return true if the hardware handles superspeed
58226dd3474SWilliam Wu * @g: controller that might support superspeed
58326dd3474SWilliam Wu */
gadget_is_superspeed(struct usb_gadget * g)58426dd3474SWilliam Wu static inline int gadget_is_superspeed(struct usb_gadget *g)
58526dd3474SWilliam Wu {
58626dd3474SWilliam Wu return g->max_speed >= USB_SPEED_SUPER;
58726dd3474SWilliam Wu }
58826dd3474SWilliam Wu
58926dd3474SWilliam Wu /**
59023cd1385SRemy Bohmer * gadget_is_otg - return true iff the hardware is OTG-ready
59123cd1385SRemy Bohmer * @g: controller that might have a Mini-AB connector
59223cd1385SRemy Bohmer *
59323cd1385SRemy Bohmer * This is a runtime test, since kernels with a USB-OTG stack sometimes
59423cd1385SRemy Bohmer * run on boards which only have a Mini-B (or Mini-A) connector.
59523cd1385SRemy Bohmer */
gadget_is_otg(struct usb_gadget * g)59623cd1385SRemy Bohmer static inline int gadget_is_otg(struct usb_gadget *g)
59723cd1385SRemy Bohmer {
59823cd1385SRemy Bohmer #ifdef CONFIG_USB_OTG
59923cd1385SRemy Bohmer return g->is_otg;
60023cd1385SRemy Bohmer #else
60123cd1385SRemy Bohmer return 0;
60223cd1385SRemy Bohmer #endif
60323cd1385SRemy Bohmer }
60423cd1385SRemy Bohmer
60523cd1385SRemy Bohmer /**
60623cd1385SRemy Bohmer * usb_gadget_frame_number - returns the current frame number
60723cd1385SRemy Bohmer * @gadget: controller that reports the frame number
60823cd1385SRemy Bohmer *
60923cd1385SRemy Bohmer * Returns the usb frame number, normally eleven bits from a SOF packet,
61023cd1385SRemy Bohmer * or negative errno if this device doesn't support this capability.
61123cd1385SRemy Bohmer */
usb_gadget_frame_number(struct usb_gadget * gadget)61223cd1385SRemy Bohmer static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
61323cd1385SRemy Bohmer {
61423cd1385SRemy Bohmer return gadget->ops->get_frame(gadget);
61523cd1385SRemy Bohmer }
61623cd1385SRemy Bohmer
61723cd1385SRemy Bohmer /**
61823cd1385SRemy Bohmer * usb_gadget_wakeup - tries to wake up the host connected to this gadget
61923cd1385SRemy Bohmer * @gadget: controller used to wake up the host
62023cd1385SRemy Bohmer *
62123cd1385SRemy Bohmer * Returns zero on success, else negative error code if the hardware
62223cd1385SRemy Bohmer * doesn't support such attempts, or its support has not been enabled
62323cd1385SRemy Bohmer * by the usb host. Drivers must return device descriptors that report
62423cd1385SRemy Bohmer * their ability to support this, or hosts won't enable it.
62523cd1385SRemy Bohmer *
62623cd1385SRemy Bohmer * This may also try to use SRP to wake the host and start enumeration,
62723cd1385SRemy Bohmer * even if OTG isn't otherwise in use. OTG devices may also start
62823cd1385SRemy Bohmer * remote wakeup even when hosts don't explicitly enable it.
62923cd1385SRemy Bohmer */
usb_gadget_wakeup(struct usb_gadget * gadget)63023cd1385SRemy Bohmer static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
63123cd1385SRemy Bohmer {
63223cd1385SRemy Bohmer if (!gadget->ops->wakeup)
63323cd1385SRemy Bohmer return -EOPNOTSUPP;
63423cd1385SRemy Bohmer return gadget->ops->wakeup(gadget);
63523cd1385SRemy Bohmer }
63623cd1385SRemy Bohmer
63723cd1385SRemy Bohmer /**
63823cd1385SRemy Bohmer * usb_gadget_set_selfpowered - sets the device selfpowered feature.
63923cd1385SRemy Bohmer * @gadget:the device being declared as self-powered
64023cd1385SRemy Bohmer *
64123cd1385SRemy Bohmer * this affects the device status reported by the hardware driver
64223cd1385SRemy Bohmer * to reflect that it now has a local power supply.
64323cd1385SRemy Bohmer *
64423cd1385SRemy Bohmer * returns zero on success, else negative errno.
64523cd1385SRemy Bohmer */
usb_gadget_set_selfpowered(struct usb_gadget * gadget)6466142e0aeSVitaly Kuzmichev static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
64723cd1385SRemy Bohmer {
64823cd1385SRemy Bohmer if (!gadget->ops->set_selfpowered)
64923cd1385SRemy Bohmer return -EOPNOTSUPP;
65023cd1385SRemy Bohmer return gadget->ops->set_selfpowered(gadget, 1);
65123cd1385SRemy Bohmer }
65223cd1385SRemy Bohmer
65323cd1385SRemy Bohmer /**
65423cd1385SRemy Bohmer * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
65523cd1385SRemy Bohmer * @gadget:the device being declared as bus-powered
65623cd1385SRemy Bohmer *
65723cd1385SRemy Bohmer * this affects the device status reported by the hardware driver.
65823cd1385SRemy Bohmer * some hardware may not support bus-powered operation, in which
65923cd1385SRemy Bohmer * case this feature's value can never change.
66023cd1385SRemy Bohmer *
66123cd1385SRemy Bohmer * returns zero on success, else negative errno.
66223cd1385SRemy Bohmer */
usb_gadget_clear_selfpowered(struct usb_gadget * gadget)6636142e0aeSVitaly Kuzmichev static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
66423cd1385SRemy Bohmer {
66523cd1385SRemy Bohmer if (!gadget->ops->set_selfpowered)
66623cd1385SRemy Bohmer return -EOPNOTSUPP;
66723cd1385SRemy Bohmer return gadget->ops->set_selfpowered(gadget, 0);
66823cd1385SRemy Bohmer }
66923cd1385SRemy Bohmer
67023cd1385SRemy Bohmer /**
67123cd1385SRemy Bohmer * usb_gadget_vbus_connect - Notify controller that VBUS is powered
67223cd1385SRemy Bohmer * @gadget:The device which now has VBUS power.
67323cd1385SRemy Bohmer *
67423cd1385SRemy Bohmer * This call is used by a driver for an external transceiver (or GPIO)
67523cd1385SRemy Bohmer * that detects a VBUS power session starting. Common responses include
67623cd1385SRemy Bohmer * resuming the controller, activating the D+ (or D-) pullup to let the
67723cd1385SRemy Bohmer * host detect that a USB device is attached, and starting to draw power
67823cd1385SRemy Bohmer * (8mA or possibly more, especially after SET_CONFIGURATION).
67923cd1385SRemy Bohmer *
68023cd1385SRemy Bohmer * Returns zero on success, else negative errno.
68123cd1385SRemy Bohmer */
usb_gadget_vbus_connect(struct usb_gadget * gadget)6826142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
68323cd1385SRemy Bohmer {
68423cd1385SRemy Bohmer if (!gadget->ops->vbus_session)
68523cd1385SRemy Bohmer return -EOPNOTSUPP;
68623cd1385SRemy Bohmer return gadget->ops->vbus_session(gadget, 1);
68723cd1385SRemy Bohmer }
68823cd1385SRemy Bohmer
68923cd1385SRemy Bohmer /**
69023cd1385SRemy Bohmer * usb_gadget_vbus_draw - constrain controller's VBUS power usage
69123cd1385SRemy Bohmer * @gadget:The device whose VBUS usage is being described
69223cd1385SRemy Bohmer * @mA:How much current to draw, in milliAmperes. This should be twice
69323cd1385SRemy Bohmer * the value listed in the configuration descriptor bMaxPower field.
69423cd1385SRemy Bohmer *
69523cd1385SRemy Bohmer * This call is used by gadget drivers during SET_CONFIGURATION calls,
69623cd1385SRemy Bohmer * reporting how much power the device may consume. For example, this
69723cd1385SRemy Bohmer * could affect how quickly batteries are recharged.
69823cd1385SRemy Bohmer *
69923cd1385SRemy Bohmer * Returns zero on success, else negative errno.
70023cd1385SRemy Bohmer */
usb_gadget_vbus_draw(struct usb_gadget * gadget,unsigned mA)7016142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
70223cd1385SRemy Bohmer {
70323cd1385SRemy Bohmer if (!gadget->ops->vbus_draw)
70423cd1385SRemy Bohmer return -EOPNOTSUPP;
70523cd1385SRemy Bohmer return gadget->ops->vbus_draw(gadget, mA);
70623cd1385SRemy Bohmer }
70723cd1385SRemy Bohmer
70823cd1385SRemy Bohmer /**
70923cd1385SRemy Bohmer * usb_gadget_vbus_disconnect - notify controller about VBUS session end
71023cd1385SRemy Bohmer * @gadget:the device whose VBUS supply is being described
71123cd1385SRemy Bohmer *
71223cd1385SRemy Bohmer * This call is used by a driver for an external transceiver (or GPIO)
71323cd1385SRemy Bohmer * that detects a VBUS power session ending. Common responses include
71423cd1385SRemy Bohmer * reversing everything done in usb_gadget_vbus_connect().
71523cd1385SRemy Bohmer *
71623cd1385SRemy Bohmer * Returns zero on success, else negative errno.
71723cd1385SRemy Bohmer */
usb_gadget_vbus_disconnect(struct usb_gadget * gadget)7186142e0aeSVitaly Kuzmichev static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
71923cd1385SRemy Bohmer {
72023cd1385SRemy Bohmer if (!gadget->ops->vbus_session)
72123cd1385SRemy Bohmer return -EOPNOTSUPP;
72223cd1385SRemy Bohmer return gadget->ops->vbus_session(gadget, 0);
72323cd1385SRemy Bohmer }
72423cd1385SRemy Bohmer
72523cd1385SRemy Bohmer /**
72623cd1385SRemy Bohmer * usb_gadget_connect - software-controlled connect to USB host
72723cd1385SRemy Bohmer * @gadget:the peripheral being connected
72823cd1385SRemy Bohmer *
72923cd1385SRemy Bohmer * Enables the D+ (or potentially D-) pullup. The host will start
73023cd1385SRemy Bohmer * enumerating this gadget when the pullup is active and a VBUS session
73123cd1385SRemy Bohmer * is active (the link is powered). This pullup is always enabled unless
73223cd1385SRemy Bohmer * usb_gadget_disconnect() has been used to disable it.
73323cd1385SRemy Bohmer *
73423cd1385SRemy Bohmer * Returns zero on success, else negative errno.
73523cd1385SRemy Bohmer */
usb_gadget_connect(struct usb_gadget * gadget)7366142e0aeSVitaly Kuzmichev static inline int usb_gadget_connect(struct usb_gadget *gadget)
73723cd1385SRemy Bohmer {
73823cd1385SRemy Bohmer if (!gadget->ops->pullup)
73923cd1385SRemy Bohmer return -EOPNOTSUPP;
74023cd1385SRemy Bohmer return gadget->ops->pullup(gadget, 1);
74123cd1385SRemy Bohmer }
74223cd1385SRemy Bohmer
74323cd1385SRemy Bohmer /**
74423cd1385SRemy Bohmer * usb_gadget_disconnect - software-controlled disconnect from USB host
74523cd1385SRemy Bohmer * @gadget:the peripheral being disconnected
74623cd1385SRemy Bohmer *
74723cd1385SRemy Bohmer * Disables the D+ (or potentially D-) pullup, which the host may see
74823cd1385SRemy Bohmer * as a disconnect (when a VBUS session is active). Not all systems
74923cd1385SRemy Bohmer * support software pullup controls.
75023cd1385SRemy Bohmer *
75123cd1385SRemy Bohmer * This routine may be used during the gadget driver bind() call to prevent
75223cd1385SRemy Bohmer * the peripheral from ever being visible to the USB host, unless later
75323cd1385SRemy Bohmer * usb_gadget_connect() is called. For example, user mode components may
75423cd1385SRemy Bohmer * need to be activated before the system can talk to hosts.
75523cd1385SRemy Bohmer *
75623cd1385SRemy Bohmer * Returns zero on success, else negative errno.
75723cd1385SRemy Bohmer */
usb_gadget_disconnect(struct usb_gadget * gadget)7586142e0aeSVitaly Kuzmichev static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
75923cd1385SRemy Bohmer {
76023cd1385SRemy Bohmer if (!gadget->ops->pullup)
76123cd1385SRemy Bohmer return -EOPNOTSUPP;
76223cd1385SRemy Bohmer return gadget->ops->pullup(gadget, 0);
76323cd1385SRemy Bohmer }
76423cd1385SRemy Bohmer
76523cd1385SRemy Bohmer
76623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
76723cd1385SRemy Bohmer
76823cd1385SRemy Bohmer /**
76923cd1385SRemy Bohmer * struct usb_gadget_driver - driver for usb 'slave' devices
77045d9337eSKishon Vijay Abraham I * @function: String describing the gadget's function
77123cd1385SRemy Bohmer * @speed: Highest speed the driver handles.
77223cd1385SRemy Bohmer * @bind: Invoked when the driver is bound to a gadget, usually
77323cd1385SRemy Bohmer * after registering the driver.
77423cd1385SRemy Bohmer * At that point, ep0 is fully initialized, and ep_list holds
77523cd1385SRemy Bohmer * the currently-available endpoints.
77623cd1385SRemy Bohmer * Called in a context that permits sleeping.
77723cd1385SRemy Bohmer * @setup: Invoked for ep0 control requests that aren't handled by
77823cd1385SRemy Bohmer * the hardware level driver. Most calls must be handled by
77923cd1385SRemy Bohmer * the gadget driver, including descriptor and configuration
78023cd1385SRemy Bohmer * management. The 16 bit members of the setup data are in
78123cd1385SRemy Bohmer * USB byte order. Called in_interrupt; this may not sleep. Driver
78223cd1385SRemy Bohmer * queues a response to ep0, or returns negative to stall.
78323cd1385SRemy Bohmer * @disconnect: Invoked after all transfers have been stopped,
78423cd1385SRemy Bohmer * when the host is disconnected. May be called in_interrupt; this
78523cd1385SRemy Bohmer * may not sleep. Some devices can't detect disconnect, so this might
78623cd1385SRemy Bohmer * not be called except as part of controller shutdown.
78723cd1385SRemy Bohmer * @unbind: Invoked when the driver is unbound from a gadget,
78823cd1385SRemy Bohmer * usually from rmmod (after a disconnect is reported).
78923cd1385SRemy Bohmer * Called in a context that permits sleeping.
79023cd1385SRemy Bohmer * @suspend: Invoked on USB suspend. May be called in_interrupt.
79123cd1385SRemy Bohmer * @resume: Invoked on USB resume. May be called in_interrupt.
79245d9337eSKishon Vijay Abraham I * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers
79345d9337eSKishon Vijay Abraham I * and should be called in_interrupt.
79423cd1385SRemy Bohmer *
79523cd1385SRemy Bohmer * Devices are disabled till a gadget driver successfully bind()s, which
79623cd1385SRemy Bohmer * means the driver will handle setup() requests needed to enumerate (and
79723cd1385SRemy Bohmer * meet "chapter 9" requirements) then do some useful work.
79823cd1385SRemy Bohmer *
79923cd1385SRemy Bohmer * If gadget->is_otg is true, the gadget driver must provide an OTG
80023cd1385SRemy Bohmer * descriptor during enumeration, or else fail the bind() call. In such
80123cd1385SRemy Bohmer * cases, no USB traffic may flow until both bind() returns without
80223cd1385SRemy Bohmer * having called usb_gadget_disconnect(), and the USB host stack has
80323cd1385SRemy Bohmer * initialized.
80423cd1385SRemy Bohmer *
80523cd1385SRemy Bohmer * Drivers use hardware-specific knowledge to configure the usb hardware.
80623cd1385SRemy Bohmer * endpoint addressing is only one of several hardware characteristics that
80723cd1385SRemy Bohmer * are in descriptors the ep0 implementation returns from setup() calls.
80823cd1385SRemy Bohmer *
80923cd1385SRemy Bohmer * Except for ep0 implementation, most driver code shouldn't need change to
81023cd1385SRemy Bohmer * run on top of different usb controllers. It'll use endpoints set up by
81123cd1385SRemy Bohmer * that ep0 implementation.
81223cd1385SRemy Bohmer *
81323cd1385SRemy Bohmer * The usb controller driver handles a few standard usb requests. Those
81423cd1385SRemy Bohmer * include set_address, and feature flags for devices, interfaces, and
81523cd1385SRemy Bohmer * endpoints (the get_status, set_feature, and clear_feature requests).
81623cd1385SRemy Bohmer *
81723cd1385SRemy Bohmer * Accordingly, the driver's setup() callback must always implement all
81823cd1385SRemy Bohmer * get_descriptor requests, returning at least a device descriptor and
81923cd1385SRemy Bohmer * a configuration descriptor. Drivers must make sure the endpoint
82023cd1385SRemy Bohmer * descriptors match any hardware constraints. Some hardware also constrains
82123cd1385SRemy Bohmer * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
82223cd1385SRemy Bohmer *
82323cd1385SRemy Bohmer * The driver's setup() callback must also implement set_configuration,
82423cd1385SRemy Bohmer * and should also implement set_interface, get_configuration, and
82523cd1385SRemy Bohmer * get_interface. Setting a configuration (or interface) is where
82623cd1385SRemy Bohmer * endpoints should be activated or (config 0) shut down.
82723cd1385SRemy Bohmer *
82823cd1385SRemy Bohmer * (Note that only the default control endpoint is supported. Neither
82923cd1385SRemy Bohmer * hosts nor devices generally support control traffic except to ep0.)
83023cd1385SRemy Bohmer *
83123cd1385SRemy Bohmer * Most devices will ignore USB suspend/resume operations, and so will
83223cd1385SRemy Bohmer * not provide those callbacks. However, some may need to change modes
83323cd1385SRemy Bohmer * when the host is not longer directing those activities. For example,
83423cd1385SRemy Bohmer * local controls (buttons, dials, etc) may need to be re-enabled since
83523cd1385SRemy Bohmer * the (remote) host can't do that any longer; or an error state might
83623cd1385SRemy Bohmer * be cleared, to make the device behave identically whether or not
83723cd1385SRemy Bohmer * power is maintained.
83823cd1385SRemy Bohmer */
83923cd1385SRemy Bohmer struct usb_gadget_driver {
84045d9337eSKishon Vijay Abraham I char *function;
84123cd1385SRemy Bohmer enum usb_device_speed speed;
84223cd1385SRemy Bohmer int (*bind)(struct usb_gadget *);
84323cd1385SRemy Bohmer void (*unbind)(struct usb_gadget *);
84423cd1385SRemy Bohmer int (*setup)(struct usb_gadget *,
84523cd1385SRemy Bohmer const struct usb_ctrlrequest *);
84623cd1385SRemy Bohmer void (*disconnect)(struct usb_gadget *);
84723cd1385SRemy Bohmer void (*suspend)(struct usb_gadget *);
84823cd1385SRemy Bohmer void (*resume)(struct usb_gadget *);
84945d9337eSKishon Vijay Abraham I void (*reset)(struct usb_gadget *);
85023cd1385SRemy Bohmer };
85123cd1385SRemy Bohmer
85223cd1385SRemy Bohmer
85323cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
85423cd1385SRemy Bohmer
85523cd1385SRemy Bohmer /* driver modules register and unregister, as usual.
85623cd1385SRemy Bohmer * these calls must be made in a context that can sleep.
85723cd1385SRemy Bohmer *
85823cd1385SRemy Bohmer * these will usually be implemented directly by the hardware-dependent
85923cd1385SRemy Bohmer * usb bus interface driver, which will only support a single driver.
86023cd1385SRemy Bohmer */
86123cd1385SRemy Bohmer
86223cd1385SRemy Bohmer /**
86323cd1385SRemy Bohmer * usb_gadget_register_driver - register a gadget driver
86423cd1385SRemy Bohmer * @driver:the driver being registered
86523cd1385SRemy Bohmer *
86623cd1385SRemy Bohmer * Call this in your gadget driver's module initialization function,
86723cd1385SRemy Bohmer * to tell the underlying usb controller driver about your driver.
86823cd1385SRemy Bohmer * The driver's bind() function will be called to bind it to a
86923cd1385SRemy Bohmer * gadget before this registration call returns. It's expected that
87023cd1385SRemy Bohmer * the bind() functions will be in init sections.
87123cd1385SRemy Bohmer * This function must be called in a context that can sleep.
87223cd1385SRemy Bohmer */
87323cd1385SRemy Bohmer int usb_gadget_register_driver(struct usb_gadget_driver *driver);
87423cd1385SRemy Bohmer
87523cd1385SRemy Bohmer /**
87623cd1385SRemy Bohmer * usb_gadget_unregister_driver - unregister a gadget driver
87723cd1385SRemy Bohmer * @driver:the driver being unregistered
87823cd1385SRemy Bohmer *
87923cd1385SRemy Bohmer * Call this in your gadget driver's module cleanup function,
88023cd1385SRemy Bohmer * to tell the underlying usb controller that your driver is
88123cd1385SRemy Bohmer * going away. If the controller is connected to a USB host,
88223cd1385SRemy Bohmer * it will first disconnect(). The driver is also requested
88323cd1385SRemy Bohmer * to unbind() and clean up any device state, before this procedure
88423cd1385SRemy Bohmer * finally returns. It's expected that the unbind() functions
88523cd1385SRemy Bohmer * will in in exit sections, so may not be linked in some kernels.
88623cd1385SRemy Bohmer * This function must be called in a context that can sleep.
88723cd1385SRemy Bohmer */
88823cd1385SRemy Bohmer int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
88923cd1385SRemy Bohmer
89045d9337eSKishon Vijay Abraham I int usb_add_gadget_udc_release(struct device *parent,
89145d9337eSKishon Vijay Abraham I struct usb_gadget *gadget, void (*release)(struct device *dev));
89245d9337eSKishon Vijay Abraham I int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
89345d9337eSKishon Vijay Abraham I void usb_del_gadget_udc(struct usb_gadget *gadget);
89423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
89523cd1385SRemy Bohmer
89623cd1385SRemy Bohmer /* utility to simplify dealing with string descriptors */
89723cd1385SRemy Bohmer
89823cd1385SRemy Bohmer /**
89923cd1385SRemy Bohmer * struct usb_gadget_strings - a set of USB strings in a given language
90023cd1385SRemy Bohmer * @language:identifies the strings' language (0x0409 for en-us)
90123cd1385SRemy Bohmer * @strings:array of strings with their ids
90223cd1385SRemy Bohmer *
90323cd1385SRemy Bohmer * If you're using usb_gadget_get_string(), use this to wrap all the
90423cd1385SRemy Bohmer * strings for a given language.
90523cd1385SRemy Bohmer */
90623cd1385SRemy Bohmer struct usb_gadget_strings {
90723cd1385SRemy Bohmer u16 language; /* 0x0409 for en-us */
90823cd1385SRemy Bohmer struct usb_string *strings;
90923cd1385SRemy Bohmer };
91023cd1385SRemy Bohmer
91123cd1385SRemy Bohmer /* put descriptor for string with that id into buf (buflen >= 256) */
91223cd1385SRemy Bohmer int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
91323cd1385SRemy Bohmer
91423cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
91523cd1385SRemy Bohmer
91623cd1385SRemy Bohmer /* utility to simplify managing config descriptors */
91723cd1385SRemy Bohmer
91823cd1385SRemy Bohmer /* write vector of descriptors into buffer */
91923cd1385SRemy Bohmer int usb_descriptor_fillbuf(void *, unsigned,
92023cd1385SRemy Bohmer const struct usb_descriptor_header **);
92123cd1385SRemy Bohmer
92223cd1385SRemy Bohmer /* build config descriptor from single descriptor vector */
92323cd1385SRemy Bohmer int usb_gadget_config_buf(const struct usb_config_descriptor *config,
92423cd1385SRemy Bohmer void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
92523cd1385SRemy Bohmer
92623cd1385SRemy Bohmer /*-------------------------------------------------------------------------*/
92745d9337eSKishon Vijay Abraham I /* utility to simplify map/unmap of usb_requests to/from DMA */
92845d9337eSKishon Vijay Abraham I
92945d9337eSKishon Vijay Abraham I extern int usb_gadget_map_request(struct usb_gadget *gadget,
93045d9337eSKishon Vijay Abraham I struct usb_request *req, int is_in);
93145d9337eSKishon Vijay Abraham I
93245d9337eSKishon Vijay Abraham I extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
93345d9337eSKishon Vijay Abraham I struct usb_request *req, int is_in);
93445d9337eSKishon Vijay Abraham I
93545d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
93645d9337eSKishon Vijay Abraham I
93745d9337eSKishon Vijay Abraham I /* utility to set gadget state properly */
93845d9337eSKishon Vijay Abraham I
93945d9337eSKishon Vijay Abraham I extern void usb_gadget_set_state(struct usb_gadget *gadget,
94045d9337eSKishon Vijay Abraham I enum usb_device_state state);
94145d9337eSKishon Vijay Abraham I
94245d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
94345d9337eSKishon Vijay Abraham I
94445d9337eSKishon Vijay Abraham I /* utility to tell udc core that the bus reset occurs */
94545d9337eSKishon Vijay Abraham I extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
94645d9337eSKishon Vijay Abraham I struct usb_gadget_driver *driver);
94745d9337eSKishon Vijay Abraham I
94845d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
94945d9337eSKishon Vijay Abraham I
95045d9337eSKishon Vijay Abraham I /* utility to give requests back to the gadget layer */
95145d9337eSKishon Vijay Abraham I
95245d9337eSKishon Vijay Abraham I extern void usb_gadget_giveback_request(struct usb_ep *ep,
95345d9337eSKishon Vijay Abraham I struct usb_request *req);
95445d9337eSKishon Vijay Abraham I
95545d9337eSKishon Vijay Abraham I /*-------------------------------------------------------------------------*/
95623cd1385SRemy Bohmer
95723cd1385SRemy Bohmer /* utility wrapping a simple endpoint selection policy */
95823cd1385SRemy Bohmer
95923cd1385SRemy Bohmer extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
96023cd1385SRemy Bohmer struct usb_endpoint_descriptor *);
96123cd1385SRemy Bohmer
96223cd1385SRemy Bohmer extern void usb_ep_autoconfig_reset(struct usb_gadget *);
96323cd1385SRemy Bohmer
9642d48aa69SKishon Vijay Abraham I extern int usb_gadget_handle_interrupts(int index);
96523cd1385SRemy Bohmer
966256dc48fSJean-Jacques Hiblot #if CONFIG_IS_ENABLED(DM_USB_GADGET)
967256dc48fSJean-Jacques Hiblot int usb_gadget_initialize(int index);
968256dc48fSJean-Jacques Hiblot int usb_gadget_release(int index);
969256dc48fSJean-Jacques Hiblot int dm_usb_gadget_handle_interrupts(struct udevice *dev);
970256dc48fSJean-Jacques Hiblot #else
971256dc48fSJean-Jacques Hiblot #include <usb.h>
usb_gadget_initialize(int index)972b95d4446SJean-Jacques Hiblot static inline int usb_gadget_initialize(int index)
973b95d4446SJean-Jacques Hiblot {
974b95d4446SJean-Jacques Hiblot return board_usb_init(index, USB_INIT_DEVICE);
975b95d4446SJean-Jacques Hiblot }
976b95d4446SJean-Jacques Hiblot
usb_gadget_release(int index)977b95d4446SJean-Jacques Hiblot static inline int usb_gadget_release(int index)
978b95d4446SJean-Jacques Hiblot {
979b95d4446SJean-Jacques Hiblot return board_usb_cleanup(index, USB_INIT_DEVICE);
980b95d4446SJean-Jacques Hiblot }
981256dc48fSJean-Jacques Hiblot #endif
982256dc48fSJean-Jacques Hiblot
98323cd1385SRemy Bohmer #endif /* __LINUX_USB_GADGET_H */
984