xref: /OK3568_Linux_fs/u-boot/include/linux/usb/gadget.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * <linux/usb/gadget.h>
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * We call the USB code inside a Linux-based peripheral device a "gadget"
5*4882a593Smuzhiyun  * driver, except for the hardware-specific bus glue.  One USB host can
6*4882a593Smuzhiyun  * master many USB gadgets, but the gadgets are only slaved to one host.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * (C) Copyright 2002-2004 by David Brownell
10*4882a593Smuzhiyun  * All Rights Reserved.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This software is licensed under the GNU GPL version 2.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
15*4882a593Smuzhiyun  *                      Remy Bohmer <linux@bohmer.net>
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #ifndef __LINUX_USB_GADGET_H
19*4882a593Smuzhiyun #define __LINUX_USB_GADGET_H
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <errno.h>
22*4882a593Smuzhiyun #include <usb.h>
23*4882a593Smuzhiyun #include <linux/compat.h>
24*4882a593Smuzhiyun #include <linux/list.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct usb_ep;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun  * struct usb_request - describes one i/o request
30*4882a593Smuzhiyun  * @buf: Buffer used for data.  Always provide this; some controllers
31*4882a593Smuzhiyun  *	only use PIO, or don't use DMA for some endpoints.
32*4882a593Smuzhiyun  * @dma: DMA address corresponding to 'buf'.  If you don't set this
33*4882a593Smuzhiyun  *	field, and the usb controller needs one, it is responsible
34*4882a593Smuzhiyun  *	for mapping and unmapping the buffer.
35*4882a593Smuzhiyun  * @stream_id: The stream id, when USB3.0 bulk streams are being used
36*4882a593Smuzhiyun  * @length: Length of that data
37*4882a593Smuzhiyun  * @no_interrupt: If true, hints that no completion irq is needed.
38*4882a593Smuzhiyun  *	Helpful sometimes with deep request queues that are handled
39*4882a593Smuzhiyun  *	directly by DMA controllers.
40*4882a593Smuzhiyun  * @zero: If true, when writing data, makes the last packet be "short"
41*4882a593Smuzhiyun  *     by adding a zero length packet as needed;
42*4882a593Smuzhiyun  * @short_not_ok: When reading data, makes short packets be
43*4882a593Smuzhiyun  *     treated as errors (queue stops advancing till cleanup).
44*4882a593Smuzhiyun  * @complete: Function called when request completes, so this request and
45*4882a593Smuzhiyun  *	its buffer may be re-used.
46*4882a593Smuzhiyun  *	Reads terminate with a short packet, or when the buffer fills,
47*4882a593Smuzhiyun  *	whichever comes first.  When writes terminate, some data bytes
48*4882a593Smuzhiyun  *	will usually still be in flight (often in a hardware fifo).
49*4882a593Smuzhiyun  *	Errors (for reads or writes) stop the queue from advancing
50*4882a593Smuzhiyun  *	until the completion function returns, so that any transfers
51*4882a593Smuzhiyun  *	invalidated by the error may first be dequeued.
52*4882a593Smuzhiyun  * @context: For use by the completion callback
53*4882a593Smuzhiyun  * @list: For use by the gadget driver.
54*4882a593Smuzhiyun  * @status: Reports completion code, zero or a negative errno.
55*4882a593Smuzhiyun  *	Normally, faults block the transfer queue from advancing until
56*4882a593Smuzhiyun  *	the completion callback returns.
57*4882a593Smuzhiyun  *	Code "-ESHUTDOWN" indicates completion caused by device disconnect,
58*4882a593Smuzhiyun  *	or when the driver disabled the endpoint.
59*4882a593Smuzhiyun  * @actual: Reports bytes transferred to/from the buffer.  For reads (OUT
60*4882a593Smuzhiyun  *	transfers) this may be less than the requested length.  If the
61*4882a593Smuzhiyun  *	short_not_ok flag is set, short reads are treated as errors
62*4882a593Smuzhiyun  *	even when status otherwise indicates successful completion.
63*4882a593Smuzhiyun  *	Note that for writes (IN transfers) some data bytes may still
64*4882a593Smuzhiyun  *	reside in a device-side FIFO when the request is reported as
65*4882a593Smuzhiyun  *	complete.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * These are allocated/freed through the endpoint they're used with.  The
68*4882a593Smuzhiyun  * hardware's driver can add extra per-request data to the memory it returns,
69*4882a593Smuzhiyun  * which often avoids separate memory allocations (potential failures),
70*4882a593Smuzhiyun  * later when the request is queued.
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Request flags affect request handling, such as whether a zero length
73*4882a593Smuzhiyun  * packet is written (the "zero" flag), whether a short read should be
74*4882a593Smuzhiyun  * treated as an error (blocking request queue advance, the "short_not_ok"
75*4882a593Smuzhiyun  * flag), or hinting that an interrupt is not required (the "no_interrupt"
76*4882a593Smuzhiyun  * flag, for use with deep request queues).
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Bulk endpoints can use any size buffers, and can also be used for interrupt
79*4882a593Smuzhiyun  * transfers. interrupt-only endpoints can be much less functional.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * NOTE:  this is analagous to 'struct urb' on the host side, except that
82*4882a593Smuzhiyun  * it's thinner and promotes more pre-allocation.
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun struct usb_request {
86*4882a593Smuzhiyun 	void			*buf;
87*4882a593Smuzhiyun 	unsigned		length;
88*4882a593Smuzhiyun 	dma_addr_t		dma;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	unsigned		stream_id:16;
91*4882a593Smuzhiyun 	unsigned		no_interrupt:1;
92*4882a593Smuzhiyun 	unsigned		zero:1;
93*4882a593Smuzhiyun 	unsigned		short_not_ok:1;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	void			(*complete)(struct usb_ep *ep,
96*4882a593Smuzhiyun 					struct usb_request *req);
97*4882a593Smuzhiyun 	void			*context;
98*4882a593Smuzhiyun 	struct list_head	list;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	int			status;
101*4882a593Smuzhiyun 	unsigned		actual;
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* endpoint-specific parts of the api to the usb controller hardware.
107*4882a593Smuzhiyun  * unlike the urb model, (de)multiplexing layers are not required.
108*4882a593Smuzhiyun  * (so this api could slash overhead if used on the host side...)
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * note that device side usb controllers commonly differ in how many
111*4882a593Smuzhiyun  * endpoints they support, as well as their capabilities.
112*4882a593Smuzhiyun  */
113*4882a593Smuzhiyun struct usb_ep_ops {
114*4882a593Smuzhiyun 	int (*enable) (struct usb_ep *ep,
115*4882a593Smuzhiyun 		const struct usb_endpoint_descriptor *desc);
116*4882a593Smuzhiyun 	int (*disable) (struct usb_ep *ep);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	struct usb_request *(*alloc_request) (struct usb_ep *ep,
119*4882a593Smuzhiyun 		gfp_t gfp_flags);
120*4882a593Smuzhiyun 	void (*free_request) (struct usb_ep *ep, struct usb_request *req);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	int (*queue) (struct usb_ep *ep, struct usb_request *req,
123*4882a593Smuzhiyun 		gfp_t gfp_flags);
124*4882a593Smuzhiyun 	int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	int (*set_halt) (struct usb_ep *ep, int value);
127*4882a593Smuzhiyun 	int (*set_wedge)(struct usb_ep *ep);
128*4882a593Smuzhiyun 	int (*fifo_status) (struct usb_ep *ep);
129*4882a593Smuzhiyun 	void (*fifo_flush) (struct usb_ep *ep);
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * struct usb_ep_caps - endpoint capabilities description
134*4882a593Smuzhiyun  * @type_control:Endpoint supports control type (reserved for ep0).
135*4882a593Smuzhiyun  * @type_iso:Endpoint supports isochronous transfers.
136*4882a593Smuzhiyun  * @type_bulk:Endpoint supports bulk transfers.
137*4882a593Smuzhiyun  * @type_int:Endpoint supports interrupt transfers.
138*4882a593Smuzhiyun  * @dir_in:Endpoint supports IN direction.
139*4882a593Smuzhiyun  * @dir_out:Endpoint supports OUT direction.
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun struct usb_ep_caps {
142*4882a593Smuzhiyun 	unsigned type_control:1;
143*4882a593Smuzhiyun 	unsigned type_iso:1;
144*4882a593Smuzhiyun 	unsigned type_bulk:1;
145*4882a593Smuzhiyun 	unsigned type_int:1;
146*4882a593Smuzhiyun 	unsigned dir_in:1;
147*4882a593Smuzhiyun 	unsigned dir_out:1;
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun  * struct usb_ep - device side representation of USB endpoint
152*4882a593Smuzhiyun  * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
153*4882a593Smuzhiyun  * @ops: Function pointers used to access hardware-specific operations.
154*4882a593Smuzhiyun  * @ep_list:the gadget's ep_list holds all of its endpoints
155*4882a593Smuzhiyun  * @caps:The structure describing types and directions supported by endoint.
156*4882a593Smuzhiyun  * @maxpacket:The maximum packet size used on this endpoint.  The initial
157*4882a593Smuzhiyun  *	value can sometimes be reduced (hardware allowing), according to
158*4882a593Smuzhiyun  *      the endpoint descriptor used to configure the endpoint.
159*4882a593Smuzhiyun  * @maxpacket_limit:The maximum packet size value which can be handled by this
160*4882a593Smuzhiyun  *	endpoint. It's set once by UDC driver when endpoint is initialized, and
161*4882a593Smuzhiyun  *	should not be changed. Should not be confused with maxpacket.
162*4882a593Smuzhiyun  * @max_streams: The maximum number of streams supported
163*4882a593Smuzhiyun  * 	by this EP (0 - 16, actual number is 2^n)
164*4882a593Smuzhiyun  * @maxburst: the maximum number of bursts supported by this EP (for usb3)
165*4882a593Smuzhiyun  * @driver_data:for use by the gadget driver.  all other fields are
166*4882a593Smuzhiyun  *	read-only to gadget drivers.
167*4882a593Smuzhiyun  * @desc: endpoint descriptor.  This pointer is set before the endpoint is
168*4882a593Smuzhiyun  * 	enabled and remains valid until the endpoint is disabled.
169*4882a593Smuzhiyun  * @comp_desc: In case of SuperSpeed support, this is the endpoint companion
170*4882a593Smuzhiyun  * 	descriptor that is used to configure the endpoint
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  * the bus controller driver lists all the general purpose endpoints in
173*4882a593Smuzhiyun  * gadget->ep_list.  the control endpoint (gadget->ep0) is not in that list,
174*4882a593Smuzhiyun  * and is accessed only in response to a driver setup() callback.
175*4882a593Smuzhiyun  */
176*4882a593Smuzhiyun struct usb_ep {
177*4882a593Smuzhiyun 	void			*driver_data;
178*4882a593Smuzhiyun 	const char		*name;
179*4882a593Smuzhiyun 	const struct usb_ep_ops	*ops;
180*4882a593Smuzhiyun 	struct list_head	ep_list;
181*4882a593Smuzhiyun 	struct usb_ep_caps	caps;
182*4882a593Smuzhiyun 	unsigned		maxpacket:16;
183*4882a593Smuzhiyun 	unsigned		maxpacket_limit:16;
184*4882a593Smuzhiyun 	unsigned		max_streams:16;
185*4882a593Smuzhiyun 	unsigned		maxburst:5;
186*4882a593Smuzhiyun 	const struct usb_endpoint_descriptor	*desc;
187*4882a593Smuzhiyun 	const struct usb_ss_ep_comp_descriptor	*comp_desc;
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * usb_ep_set_maxpacket_limit - set maximum packet size limit for endpoint
194*4882a593Smuzhiyun  * @ep:the endpoint being configured
195*4882a593Smuzhiyun  * @maxpacket_limit:value of maximum packet size limit
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * This function shoud be used only in UDC drivers to initialize endpoint
198*4882a593Smuzhiyun  * (usually in probe function).
199*4882a593Smuzhiyun  */
usb_ep_set_maxpacket_limit(struct usb_ep * ep,unsigned maxpacket_limit)200*4882a593Smuzhiyun static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
201*4882a593Smuzhiyun 					      unsigned maxpacket_limit)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	ep->maxpacket_limit = maxpacket_limit;
204*4882a593Smuzhiyun 	ep->maxpacket = maxpacket_limit;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun  * usb_ep_enable - configure endpoint, making it usable
209*4882a593Smuzhiyun  * @ep:the endpoint being configured.  may not be the endpoint named "ep0".
210*4882a593Smuzhiyun  *	drivers discover endpoints through the ep_list of a usb_gadget.
211*4882a593Smuzhiyun  * @desc:descriptor for desired behavior.  caller guarantees this pointer
212*4882a593Smuzhiyun  *	remains valid until the endpoint is disabled; the data byte order
213*4882a593Smuzhiyun  *	is little-endian (usb-standard).
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * when configurations are set, or when interface settings change, the driver
216*4882a593Smuzhiyun  * will enable or disable the relevant endpoints.  while it is enabled, an
217*4882a593Smuzhiyun  * endpoint may be used for i/o until the driver receives a disconnect() from
218*4882a593Smuzhiyun  * the host or until the endpoint is disabled.
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * the ep0 implementation (which calls this routine) must ensure that the
221*4882a593Smuzhiyun  * hardware capabilities of each endpoint match the descriptor provided
222*4882a593Smuzhiyun  * for it.  for example, an endpoint named "ep2in-bulk" would be usable
223*4882a593Smuzhiyun  * for interrupt transfers as well as bulk, but it likely couldn't be used
224*4882a593Smuzhiyun  * for iso transfers or for endpoint 14.  some endpoints are fully
225*4882a593Smuzhiyun  * configurable, with more generic names like "ep-a".  (remember that for
226*4882a593Smuzhiyun  * USB, "in" means "towards the USB master".)
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * returns zero, or a negative error code.
229*4882a593Smuzhiyun  */
usb_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)230*4882a593Smuzhiyun static inline int usb_ep_enable(struct usb_ep *ep,
231*4882a593Smuzhiyun 				const struct usb_endpoint_descriptor *desc)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	return ep->ops->enable(ep, desc);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun  * usb_ep_disable - endpoint is no longer usable
238*4882a593Smuzhiyun  * @ep:the endpoint being unconfigured.  may not be the endpoint named "ep0".
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * no other task may be using this endpoint when this is called.
241*4882a593Smuzhiyun  * any pending and uncompleted requests will complete with status
242*4882a593Smuzhiyun  * indicating disconnect (-ESHUTDOWN) before this call returns.
243*4882a593Smuzhiyun  * gadget drivers must call usb_ep_enable() again before queueing
244*4882a593Smuzhiyun  * requests to the endpoint.
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * returns zero, or a negative error code.
247*4882a593Smuzhiyun  */
usb_ep_disable(struct usb_ep * ep)248*4882a593Smuzhiyun static inline int usb_ep_disable(struct usb_ep *ep)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	return ep->ops->disable(ep);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun /**
254*4882a593Smuzhiyun  * usb_ep_alloc_request - allocate a request object to use with this endpoint
255*4882a593Smuzhiyun  * @ep:the endpoint to be used with with the request
256*4882a593Smuzhiyun  * @gfp_flags:GFP_* flags to use
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Request objects must be allocated with this call, since they normally
259*4882a593Smuzhiyun  * need controller-specific setup and may even need endpoint-specific
260*4882a593Smuzhiyun  * resources such as allocation of DMA descriptors.
261*4882a593Smuzhiyun  * Requests may be submitted with usb_ep_queue(), and receive a single
262*4882a593Smuzhiyun  * completion callback.  Free requests with usb_ep_free_request(), when
263*4882a593Smuzhiyun  * they are no longer needed.
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  * Returns the request, or null if one could not be allocated.
266*4882a593Smuzhiyun  */
usb_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)267*4882a593Smuzhiyun static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
268*4882a593Smuzhiyun 						       gfp_t gfp_flags)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	return ep->ops->alloc_request(ep, gfp_flags);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun  * usb_ep_free_request - frees a request object
275*4882a593Smuzhiyun  * @ep:the endpoint associated with the request
276*4882a593Smuzhiyun  * @req:the request being freed
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  * Reverses the effect of usb_ep_alloc_request().
279*4882a593Smuzhiyun  * Caller guarantees the request is not queued, and that it will
280*4882a593Smuzhiyun  * no longer be requeued (or otherwise used).
281*4882a593Smuzhiyun  */
usb_ep_free_request(struct usb_ep * ep,struct usb_request * req)282*4882a593Smuzhiyun static inline void usb_ep_free_request(struct usb_ep *ep,
283*4882a593Smuzhiyun 				       struct usb_request *req)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	ep->ops->free_request(ep, req);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  * usb_ep_queue - queues (submits) an I/O request to an endpoint.
290*4882a593Smuzhiyun  * @ep:the endpoint associated with the request
291*4882a593Smuzhiyun  * @req:the request being submitted
292*4882a593Smuzhiyun  * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
293*4882a593Smuzhiyun  *	pre-allocate all necessary memory with the request.
294*4882a593Smuzhiyun  *
295*4882a593Smuzhiyun  * This tells the device controller to perform the specified request through
296*4882a593Smuzhiyun  * that endpoint (reading or writing a buffer).  When the request completes,
297*4882a593Smuzhiyun  * including being canceled by usb_ep_dequeue(), the request's completion
298*4882a593Smuzhiyun  * routine is called to return the request to the driver.  Any endpoint
299*4882a593Smuzhiyun  * (except control endpoints like ep0) may have more than one transfer
300*4882a593Smuzhiyun  * request queued; they complete in FIFO order.  Once a gadget driver
301*4882a593Smuzhiyun  * submits a request, that request may not be examined or modified until it
302*4882a593Smuzhiyun  * is given back to that driver through the completion callback.
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  * Each request is turned into one or more packets.  The controller driver
305*4882a593Smuzhiyun  * never merges adjacent requests into the same packet.  OUT transfers
306*4882a593Smuzhiyun  * will sometimes use data that's already buffered in the hardware.
307*4882a593Smuzhiyun  * Drivers can rely on the fact that the first byte of the request's buffer
308*4882a593Smuzhiyun  * always corresponds to the first byte of some USB packet, for both
309*4882a593Smuzhiyun  * IN and OUT transfers.
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * Bulk endpoints can queue any amount of data; the transfer is packetized
312*4882a593Smuzhiyun  * automatically.  The last packet will be short if the request doesn't fill it
313*4882a593Smuzhiyun  * out completely.  Zero length packets (ZLPs) should be avoided in portable
314*4882a593Smuzhiyun  * protocols since not all usb hardware can successfully handle zero length
315*4882a593Smuzhiyun  * packets.  (ZLPs may be explicitly written, and may be implicitly written if
316*4882a593Smuzhiyun  * the request 'zero' flag is set.)  Bulk endpoints may also be used
317*4882a593Smuzhiyun  * for interrupt transfers; but the reverse is not true, and some endpoints
318*4882a593Smuzhiyun  * won't support every interrupt transfer.  (Such as 768 byte packets.)
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * Interrupt-only endpoints are less functional than bulk endpoints, for
321*4882a593Smuzhiyun  * example by not supporting queueing or not handling buffers that are
322*4882a593Smuzhiyun  * larger than the endpoint's maxpacket size.  They may also treat data
323*4882a593Smuzhiyun  * toggle differently.
324*4882a593Smuzhiyun  *
325*4882a593Smuzhiyun  * Control endpoints ... after getting a setup() callback, the driver queues
326*4882a593Smuzhiyun  * one response (even if it would be zero length).  That enables the
327*4882a593Smuzhiyun  * status ack, after transfering data as specified in the response.  Setup
328*4882a593Smuzhiyun  * functions may return negative error codes to generate protocol stalls.
329*4882a593Smuzhiyun  * (Note that some USB device controllers disallow protocol stall responses
330*4882a593Smuzhiyun  * in some cases.)  When control responses are deferred (the response is
331*4882a593Smuzhiyun  * written after the setup callback returns), then usb_ep_set_halt() may be
332*4882a593Smuzhiyun  * used on ep0 to trigger protocol stalls.
333*4882a593Smuzhiyun  *
334*4882a593Smuzhiyun  * For periodic endpoints, like interrupt or isochronous ones, the usb host
335*4882a593Smuzhiyun  * arranges to poll once per interval, and the gadget driver usually will
336*4882a593Smuzhiyun  * have queued some data to transfer at that time.
337*4882a593Smuzhiyun  *
338*4882a593Smuzhiyun  * Returns zero, or a negative error code.  Endpoints that are not enabled
339*4882a593Smuzhiyun  * report errors; errors will also be
340*4882a593Smuzhiyun  * reported when the usb peripheral is disconnected.
341*4882a593Smuzhiyun  */
usb_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)342*4882a593Smuzhiyun static inline int usb_ep_queue(struct usb_ep *ep,
343*4882a593Smuzhiyun 			       struct usb_request *req, gfp_t gfp_flags)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	return ep->ops->queue(ep, req, gfp_flags);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun /**
349*4882a593Smuzhiyun  * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
350*4882a593Smuzhiyun  * @ep:the endpoint associated with the request
351*4882a593Smuzhiyun  * @req:the request being canceled
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * if the request is still active on the endpoint, it is dequeued and its
354*4882a593Smuzhiyun  * completion routine is called (with status -ECONNRESET); else a negative
355*4882a593Smuzhiyun  * error code is returned.
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * note that some hardware can't clear out write fifos (to unlink the request
358*4882a593Smuzhiyun  * at the head of the queue) except as part of disconnecting from usb.  such
359*4882a593Smuzhiyun  * restrictions prevent drivers from supporting configuration changes,
360*4882a593Smuzhiyun  * even to configuration zero (a "chapter 9" requirement).
361*4882a593Smuzhiyun  */
usb_ep_dequeue(struct usb_ep * ep,struct usb_request * req)362*4882a593Smuzhiyun static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	return ep->ops->dequeue(ep, req);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /**
368*4882a593Smuzhiyun  * usb_ep_set_halt - sets the endpoint halt feature.
369*4882a593Smuzhiyun  * @ep: the non-isochronous endpoint being stalled
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * Use this to stall an endpoint, perhaps as an error report.
372*4882a593Smuzhiyun  * Except for control endpoints,
373*4882a593Smuzhiyun  * the endpoint stays halted (will not stream any data) until the host
374*4882a593Smuzhiyun  * clears this feature; drivers may need to empty the endpoint's request
375*4882a593Smuzhiyun  * queue first, to make sure no inappropriate transfers happen.
376*4882a593Smuzhiyun  *
377*4882a593Smuzhiyun  * Note that while an endpoint CLEAR_FEATURE will be invisible to the
378*4882a593Smuzhiyun  * gadget driver, a SET_INTERFACE will not be.  To reset endpoints for the
379*4882a593Smuzhiyun  * current altsetting, see usb_ep_clear_halt().  When switching altsettings,
380*4882a593Smuzhiyun  * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
381*4882a593Smuzhiyun  *
382*4882a593Smuzhiyun  * Returns zero, or a negative error code.  On success, this call sets
383*4882a593Smuzhiyun  * underlying hardware state that blocks data transfers.
384*4882a593Smuzhiyun  * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
385*4882a593Smuzhiyun  * transfer requests are still queued, or if the controller hardware
386*4882a593Smuzhiyun  * (usually a FIFO) still holds bytes that the host hasn't collected.
387*4882a593Smuzhiyun  */
usb_ep_set_halt(struct usb_ep * ep)388*4882a593Smuzhiyun static inline int usb_ep_set_halt(struct usb_ep *ep)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	return ep->ops->set_halt(ep, 1);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * usb_ep_clear_halt - clears endpoint halt, and resets toggle
395*4882a593Smuzhiyun  * @ep:the bulk or interrupt endpoint being reset
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * Use this when responding to the standard usb "set interface" request,
398*4882a593Smuzhiyun  * for endpoints that aren't reconfigured, after clearing any other state
399*4882a593Smuzhiyun  * in the endpoint's i/o queue.
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * Returns zero, or a negative error code.  On success, this call clears
402*4882a593Smuzhiyun  * the underlying hardware state reflecting endpoint halt and data toggle.
403*4882a593Smuzhiyun  * Note that some hardware can't support this request (like pxa2xx_udc),
404*4882a593Smuzhiyun  * and accordingly can't correctly implement interface altsettings.
405*4882a593Smuzhiyun  */
usb_ep_clear_halt(struct usb_ep * ep)406*4882a593Smuzhiyun static inline int usb_ep_clear_halt(struct usb_ep *ep)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	return ep->ops->set_halt(ep, 0);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun /**
412*4882a593Smuzhiyun  * usb_ep_fifo_status - returns number of bytes in fifo, or error
413*4882a593Smuzhiyun  * @ep: the endpoint whose fifo status is being checked.
414*4882a593Smuzhiyun  *
415*4882a593Smuzhiyun  * FIFO endpoints may have "unclaimed data" in them in certain cases,
416*4882a593Smuzhiyun  * such as after aborted transfers.  Hosts may not have collected all
417*4882a593Smuzhiyun  * the IN data written by the gadget driver (and reported by a request
418*4882a593Smuzhiyun  * completion).  The gadget driver may not have collected all the data
419*4882a593Smuzhiyun  * written OUT to it by the host.  Drivers that need precise handling for
420*4882a593Smuzhiyun  * fault reporting or recovery may need to use this call.
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  * This returns the number of such bytes in the fifo, or a negative
423*4882a593Smuzhiyun  * errno if the endpoint doesn't use a FIFO or doesn't support such
424*4882a593Smuzhiyun  * precise handling.
425*4882a593Smuzhiyun  */
usb_ep_fifo_status(struct usb_ep * ep)426*4882a593Smuzhiyun static inline int usb_ep_fifo_status(struct usb_ep *ep)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	if (ep->ops->fifo_status)
429*4882a593Smuzhiyun 		return ep->ops->fifo_status(ep);
430*4882a593Smuzhiyun 	else
431*4882a593Smuzhiyun 		return -EOPNOTSUPP;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /**
435*4882a593Smuzhiyun  * usb_ep_fifo_flush - flushes contents of a fifo
436*4882a593Smuzhiyun  * @ep: the endpoint whose fifo is being flushed.
437*4882a593Smuzhiyun  *
438*4882a593Smuzhiyun  * This call may be used to flush the "unclaimed data" that may exist in
439*4882a593Smuzhiyun  * an endpoint fifo after abnormal transaction terminations.  The call
440*4882a593Smuzhiyun  * must never be used except when endpoint is not being used for any
441*4882a593Smuzhiyun  * protocol translation.
442*4882a593Smuzhiyun  */
usb_ep_fifo_flush(struct usb_ep * ep)443*4882a593Smuzhiyun static inline void usb_ep_fifo_flush(struct usb_ep *ep)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	if (ep->ops->fifo_flush)
446*4882a593Smuzhiyun 		ep->ops->fifo_flush(ep);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
451*4882a593Smuzhiyun #define USB_DEFAULT_U1_DEV_EXIT_LAT	0x01	/* Less then 1 microsec */
452*4882a593Smuzhiyun #define USB_DEFAULT_U2_DEV_EXIT_LAT	0x1F4	/* Less then 500 microsec */
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun struct usb_gadget;
455*4882a593Smuzhiyun struct usb_gadget_driver;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun /* the rest of the api to the controller hardware: device operations,
458*4882a593Smuzhiyun  * which don't involve endpoints (or i/o).
459*4882a593Smuzhiyun  */
460*4882a593Smuzhiyun struct usb_gadget_ops {
461*4882a593Smuzhiyun 	int	(*get_frame)(struct usb_gadget *);
462*4882a593Smuzhiyun 	int	(*wakeup)(struct usb_gadget *);
463*4882a593Smuzhiyun 	int	(*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
464*4882a593Smuzhiyun 	int	(*vbus_session) (struct usb_gadget *, int is_active);
465*4882a593Smuzhiyun 	int	(*vbus_draw) (struct usb_gadget *, unsigned mA);
466*4882a593Smuzhiyun 	int	(*pullup) (struct usb_gadget *, int is_on);
467*4882a593Smuzhiyun 	int	(*ioctl)(struct usb_gadget *,
468*4882a593Smuzhiyun 				unsigned code, unsigned long param);
469*4882a593Smuzhiyun 	int	(*udc_start)(struct usb_gadget *,
470*4882a593Smuzhiyun 			     struct usb_gadget_driver *);
471*4882a593Smuzhiyun 	int	(*udc_stop)(struct usb_gadget *);
472*4882a593Smuzhiyun 	struct usb_ep *(*match_ep)(struct usb_gadget *,
473*4882a593Smuzhiyun 			struct usb_endpoint_descriptor *,
474*4882a593Smuzhiyun 			struct usb_ss_ep_comp_descriptor *);
475*4882a593Smuzhiyun 	void	(*udc_set_speed)(struct usb_gadget *gadget,
476*4882a593Smuzhiyun 				 enum usb_device_speed);
477*4882a593Smuzhiyun };
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun  * struct usb_gadget - represents a usb slave device
481*4882a593Smuzhiyun  * @ops: Function pointers used to access hardware-specific operations.
482*4882a593Smuzhiyun  * @ep0: Endpoint zero, used when reading or writing responses to
483*4882a593Smuzhiyun  *	driver setup() requests
484*4882a593Smuzhiyun  * @ep_list: List of other endpoints supported by the device.
485*4882a593Smuzhiyun  * @speed: Speed of current connection to USB host.
486*4882a593Smuzhiyun  * @max_speed: Maximal speed the UDC can handle.  UDC must support this
487*4882a593Smuzhiyun  *      and all slower speeds.
488*4882a593Smuzhiyun  * @is_dualspeed: true if the controller supports both high and full speed
489*4882a593Smuzhiyun  *	operation.  If it does, the gadget driver must also support both.
490*4882a593Smuzhiyun  * @is_otg: true if the USB device port uses a Mini-AB jack, so that the
491*4882a593Smuzhiyun  *	gadget driver must provide a USB OTG descriptor.
492*4882a593Smuzhiyun  * @is_a_peripheral: false unless is_otg, the "A" end of a USB cable
493*4882a593Smuzhiyun  *	is in the Mini-AB jack, and HNP has been used to switch roles
494*4882a593Smuzhiyun  *	so that the "A" device currently acts as A-Peripheral, not A-Host.
495*4882a593Smuzhiyun  * @a_hnp_support: OTG device feature flag, indicating that the A-Host
496*4882a593Smuzhiyun  *	supports HNP at this port.
497*4882a593Smuzhiyun  * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
498*4882a593Smuzhiyun  *	only supports HNP on a different root port.
499*4882a593Smuzhiyun  * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
500*4882a593Smuzhiyun  *	enabled HNP support.
501*4882a593Smuzhiyun  * @name: Identifies the controller hardware type.  Used in diagnostics
502*4882a593Smuzhiyun  *	and sometimes configuration.
503*4882a593Smuzhiyun  * @dev: Driver model state for this abstract device.
504*4882a593Smuzhiyun  * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
505*4882a593Smuzhiyun  *	MaxPacketSize.
506*4882a593Smuzhiyun  *
507*4882a593Smuzhiyun  * Gadgets have a mostly-portable "gadget driver" implementing device
508*4882a593Smuzhiyun  * functions, handling all usb configurations and interfaces.  Gadget
509*4882a593Smuzhiyun  * drivers talk to hardware-specific code indirectly, through ops vectors.
510*4882a593Smuzhiyun  * That insulates the gadget driver from hardware details, and packages
511*4882a593Smuzhiyun  * the hardware endpoints through generic i/o queues.  The "usb_gadget"
512*4882a593Smuzhiyun  * and "usb_ep" interfaces provide that insulation from the hardware.
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  * Except for the driver data, all fields in this structure are
515*4882a593Smuzhiyun  * read-only to the gadget driver.  That driver data is part of the
516*4882a593Smuzhiyun  * "driver model" infrastructure in 2.6 (and later) kernels, and for
517*4882a593Smuzhiyun  * earlier systems is grouped in a similar structure that's not known
518*4882a593Smuzhiyun  * to the rest of the kernel.
519*4882a593Smuzhiyun  *
520*4882a593Smuzhiyun  * Values of the three OTG device feature flags are updated before the
521*4882a593Smuzhiyun  * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
522*4882a593Smuzhiyun  * driver suspend() calls.  They are valid only when is_otg, and when the
523*4882a593Smuzhiyun  * device is acting as a B-Peripheral (so is_a_peripheral is false).
524*4882a593Smuzhiyun  */
525*4882a593Smuzhiyun struct usb_gadget {
526*4882a593Smuzhiyun 	/* readonly to gadget driver */
527*4882a593Smuzhiyun 	const struct usb_gadget_ops	*ops;
528*4882a593Smuzhiyun 	struct usb_ep			*ep0;
529*4882a593Smuzhiyun 	struct list_head		ep_list;	/* of usb_ep */
530*4882a593Smuzhiyun 	enum usb_device_speed		speed;
531*4882a593Smuzhiyun 	enum usb_device_speed		max_speed;
532*4882a593Smuzhiyun 	enum usb_device_state		state;
533*4882a593Smuzhiyun 	unsigned			is_dualspeed:1;
534*4882a593Smuzhiyun 	unsigned			is_otg:1;
535*4882a593Smuzhiyun 	unsigned			is_a_peripheral:1;
536*4882a593Smuzhiyun 	unsigned			b_hnp_enable:1;
537*4882a593Smuzhiyun 	unsigned			a_hnp_support:1;
538*4882a593Smuzhiyun 	unsigned			a_alt_hnp_support:1;
539*4882a593Smuzhiyun 	const char			*name;
540*4882a593Smuzhiyun 	struct device			dev;
541*4882a593Smuzhiyun 	unsigned			quirk_ep_out_aligned_size:1;
542*4882a593Smuzhiyun };
543*4882a593Smuzhiyun 
set_gadget_data(struct usb_gadget * gadget,void * data)544*4882a593Smuzhiyun static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	gadget->dev.driver_data = data;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
get_gadget_data(struct usb_gadget * gadget)549*4882a593Smuzhiyun static inline void *get_gadget_data(struct usb_gadget *gadget)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	return gadget->dev.driver_data;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
dev_to_usb_gadget(struct device * dev)554*4882a593Smuzhiyun static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	return container_of(dev, struct usb_gadget, dev);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
560*4882a593Smuzhiyun #define gadget_for_each_ep(tmp, gadget) \
561*4882a593Smuzhiyun 	list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun  * gadget_is_dualspeed - return true iff the hardware handles high speed
566*4882a593Smuzhiyun  * @g: controller that might support both high and full speeds
567*4882a593Smuzhiyun  */
gadget_is_dualspeed(struct usb_gadget * g)568*4882a593Smuzhiyun static inline int gadget_is_dualspeed(struct usb_gadget *g)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun #ifdef CONFIG_USB_GADGET_DUALSPEED
571*4882a593Smuzhiyun 	/* runtime test would check "g->is_dualspeed" ... that might be
572*4882a593Smuzhiyun 	 * useful to work around hardware bugs, but is mostly pointless
573*4882a593Smuzhiyun 	 */
574*4882a593Smuzhiyun 	return 1;
575*4882a593Smuzhiyun #else
576*4882a593Smuzhiyun 	return 0;
577*4882a593Smuzhiyun #endif
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun /**
581*4882a593Smuzhiyun  * gadget_is_superspeed() - return true if the hardware handles superspeed
582*4882a593Smuzhiyun  * @g: controller that might support superspeed
583*4882a593Smuzhiyun  */
gadget_is_superspeed(struct usb_gadget * g)584*4882a593Smuzhiyun static inline int gadget_is_superspeed(struct usb_gadget *g)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun 	return g->max_speed >= USB_SPEED_SUPER;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun  * gadget_is_otg - return true iff the hardware is OTG-ready
591*4882a593Smuzhiyun  * @g: controller that might have a Mini-AB connector
592*4882a593Smuzhiyun  *
593*4882a593Smuzhiyun  * This is a runtime test, since kernels with a USB-OTG stack sometimes
594*4882a593Smuzhiyun  * run on boards which only have a Mini-B (or Mini-A) connector.
595*4882a593Smuzhiyun  */
gadget_is_otg(struct usb_gadget * g)596*4882a593Smuzhiyun static inline int gadget_is_otg(struct usb_gadget *g)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun #ifdef CONFIG_USB_OTG
599*4882a593Smuzhiyun 	return g->is_otg;
600*4882a593Smuzhiyun #else
601*4882a593Smuzhiyun 	return 0;
602*4882a593Smuzhiyun #endif
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun /**
606*4882a593Smuzhiyun  * usb_gadget_frame_number - returns the current frame number
607*4882a593Smuzhiyun  * @gadget: controller that reports the frame number
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * Returns the usb frame number, normally eleven bits from a SOF packet,
610*4882a593Smuzhiyun  * or negative errno if this device doesn't support this capability.
611*4882a593Smuzhiyun  */
usb_gadget_frame_number(struct usb_gadget * gadget)612*4882a593Smuzhiyun static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun 	return gadget->ops->get_frame(gadget);
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun /**
618*4882a593Smuzhiyun  * usb_gadget_wakeup - tries to wake up the host connected to this gadget
619*4882a593Smuzhiyun  * @gadget: controller used to wake up the host
620*4882a593Smuzhiyun  *
621*4882a593Smuzhiyun  * Returns zero on success, else negative error code if the hardware
622*4882a593Smuzhiyun  * doesn't support such attempts, or its support has not been enabled
623*4882a593Smuzhiyun  * by the usb host.  Drivers must return device descriptors that report
624*4882a593Smuzhiyun  * their ability to support this, or hosts won't enable it.
625*4882a593Smuzhiyun  *
626*4882a593Smuzhiyun  * This may also try to use SRP to wake the host and start enumeration,
627*4882a593Smuzhiyun  * even if OTG isn't otherwise in use.  OTG devices may also start
628*4882a593Smuzhiyun  * remote wakeup even when hosts don't explicitly enable it.
629*4882a593Smuzhiyun  */
usb_gadget_wakeup(struct usb_gadget * gadget)630*4882a593Smuzhiyun static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	if (!gadget->ops->wakeup)
633*4882a593Smuzhiyun 		return -EOPNOTSUPP;
634*4882a593Smuzhiyun 	return gadget->ops->wakeup(gadget);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun  * usb_gadget_set_selfpowered - sets the device selfpowered feature.
639*4882a593Smuzhiyun  * @gadget:the device being declared as self-powered
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * this affects the device status reported by the hardware driver
642*4882a593Smuzhiyun  * to reflect that it now has a local power supply.
643*4882a593Smuzhiyun  *
644*4882a593Smuzhiyun  * returns zero on success, else negative errno.
645*4882a593Smuzhiyun  */
usb_gadget_set_selfpowered(struct usb_gadget * gadget)646*4882a593Smuzhiyun static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	if (!gadget->ops->set_selfpowered)
649*4882a593Smuzhiyun 		return -EOPNOTSUPP;
650*4882a593Smuzhiyun 	return gadget->ops->set_selfpowered(gadget, 1);
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun /**
654*4882a593Smuzhiyun  * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
655*4882a593Smuzhiyun  * @gadget:the device being declared as bus-powered
656*4882a593Smuzhiyun  *
657*4882a593Smuzhiyun  * this affects the device status reported by the hardware driver.
658*4882a593Smuzhiyun  * some hardware may not support bus-powered operation, in which
659*4882a593Smuzhiyun  * case this feature's value can never change.
660*4882a593Smuzhiyun  *
661*4882a593Smuzhiyun  * returns zero on success, else negative errno.
662*4882a593Smuzhiyun  */
usb_gadget_clear_selfpowered(struct usb_gadget * gadget)663*4882a593Smuzhiyun static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	if (!gadget->ops->set_selfpowered)
666*4882a593Smuzhiyun 		return -EOPNOTSUPP;
667*4882a593Smuzhiyun 	return gadget->ops->set_selfpowered(gadget, 0);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun /**
671*4882a593Smuzhiyun  * usb_gadget_vbus_connect - Notify controller that VBUS is powered
672*4882a593Smuzhiyun  * @gadget:The device which now has VBUS power.
673*4882a593Smuzhiyun  *
674*4882a593Smuzhiyun  * This call is used by a driver for an external transceiver (or GPIO)
675*4882a593Smuzhiyun  * that detects a VBUS power session starting.  Common responses include
676*4882a593Smuzhiyun  * resuming the controller, activating the D+ (or D-) pullup to let the
677*4882a593Smuzhiyun  * host detect that a USB device is attached, and starting to draw power
678*4882a593Smuzhiyun  * (8mA or possibly more, especially after SET_CONFIGURATION).
679*4882a593Smuzhiyun  *
680*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
681*4882a593Smuzhiyun  */
usb_gadget_vbus_connect(struct usb_gadget * gadget)682*4882a593Smuzhiyun static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	if (!gadget->ops->vbus_session)
685*4882a593Smuzhiyun 		return -EOPNOTSUPP;
686*4882a593Smuzhiyun 	return gadget->ops->vbus_session(gadget, 1);
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun /**
690*4882a593Smuzhiyun  * usb_gadget_vbus_draw - constrain controller's VBUS power usage
691*4882a593Smuzhiyun  * @gadget:The device whose VBUS usage is being described
692*4882a593Smuzhiyun  * @mA:How much current to draw, in milliAmperes.  This should be twice
693*4882a593Smuzhiyun  *	the value listed in the configuration descriptor bMaxPower field.
694*4882a593Smuzhiyun  *
695*4882a593Smuzhiyun  * This call is used by gadget drivers during SET_CONFIGURATION calls,
696*4882a593Smuzhiyun  * reporting how much power the device may consume.  For example, this
697*4882a593Smuzhiyun  * could affect how quickly batteries are recharged.
698*4882a593Smuzhiyun  *
699*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
700*4882a593Smuzhiyun  */
usb_gadget_vbus_draw(struct usb_gadget * gadget,unsigned mA)701*4882a593Smuzhiyun static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	if (!gadget->ops->vbus_draw)
704*4882a593Smuzhiyun 		return -EOPNOTSUPP;
705*4882a593Smuzhiyun 	return gadget->ops->vbus_draw(gadget, mA);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun /**
709*4882a593Smuzhiyun  * usb_gadget_vbus_disconnect - notify controller about VBUS session end
710*4882a593Smuzhiyun  * @gadget:the device whose VBUS supply is being described
711*4882a593Smuzhiyun  *
712*4882a593Smuzhiyun  * This call is used by a driver for an external transceiver (or GPIO)
713*4882a593Smuzhiyun  * that detects a VBUS power session ending.  Common responses include
714*4882a593Smuzhiyun  * reversing everything done in usb_gadget_vbus_connect().
715*4882a593Smuzhiyun  *
716*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
717*4882a593Smuzhiyun  */
usb_gadget_vbus_disconnect(struct usb_gadget * gadget)718*4882a593Smuzhiyun static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	if (!gadget->ops->vbus_session)
721*4882a593Smuzhiyun 		return -EOPNOTSUPP;
722*4882a593Smuzhiyun 	return gadget->ops->vbus_session(gadget, 0);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun  * usb_gadget_connect - software-controlled connect to USB host
727*4882a593Smuzhiyun  * @gadget:the peripheral being connected
728*4882a593Smuzhiyun  *
729*4882a593Smuzhiyun  * Enables the D+ (or potentially D-) pullup.  The host will start
730*4882a593Smuzhiyun  * enumerating this gadget when the pullup is active and a VBUS session
731*4882a593Smuzhiyun  * is active (the link is powered).  This pullup is always enabled unless
732*4882a593Smuzhiyun  * usb_gadget_disconnect() has been used to disable it.
733*4882a593Smuzhiyun  *
734*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
735*4882a593Smuzhiyun  */
usb_gadget_connect(struct usb_gadget * gadget)736*4882a593Smuzhiyun static inline int usb_gadget_connect(struct usb_gadget *gadget)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun 	if (!gadget->ops->pullup)
739*4882a593Smuzhiyun 		return -EOPNOTSUPP;
740*4882a593Smuzhiyun 	return gadget->ops->pullup(gadget, 1);
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /**
744*4882a593Smuzhiyun  * usb_gadget_disconnect - software-controlled disconnect from USB host
745*4882a593Smuzhiyun  * @gadget:the peripheral being disconnected
746*4882a593Smuzhiyun  *
747*4882a593Smuzhiyun  * Disables the D+ (or potentially D-) pullup, which the host may see
748*4882a593Smuzhiyun  * as a disconnect (when a VBUS session is active).  Not all systems
749*4882a593Smuzhiyun  * support software pullup controls.
750*4882a593Smuzhiyun  *
751*4882a593Smuzhiyun  * This routine may be used during the gadget driver bind() call to prevent
752*4882a593Smuzhiyun  * the peripheral from ever being visible to the USB host, unless later
753*4882a593Smuzhiyun  * usb_gadget_connect() is called.  For example, user mode components may
754*4882a593Smuzhiyun  * need to be activated before the system can talk to hosts.
755*4882a593Smuzhiyun  *
756*4882a593Smuzhiyun  * Returns zero on success, else negative errno.
757*4882a593Smuzhiyun  */
usb_gadget_disconnect(struct usb_gadget * gadget)758*4882a593Smuzhiyun static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	if (!gadget->ops->pullup)
761*4882a593Smuzhiyun 		return -EOPNOTSUPP;
762*4882a593Smuzhiyun 	return gadget->ops->pullup(gadget, 0);
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun /**
769*4882a593Smuzhiyun  * struct usb_gadget_driver - driver for usb 'slave' devices
770*4882a593Smuzhiyun  * @function: String describing the gadget's function
771*4882a593Smuzhiyun  * @speed: Highest speed the driver handles.
772*4882a593Smuzhiyun  * @bind: Invoked when the driver is bound to a gadget, usually
773*4882a593Smuzhiyun  *	after registering the driver.
774*4882a593Smuzhiyun  *	At that point, ep0 is fully initialized, and ep_list holds
775*4882a593Smuzhiyun  *	the currently-available endpoints.
776*4882a593Smuzhiyun  *	Called in a context that permits sleeping.
777*4882a593Smuzhiyun  * @setup: Invoked for ep0 control requests that aren't handled by
778*4882a593Smuzhiyun  *	the hardware level driver. Most calls must be handled by
779*4882a593Smuzhiyun  *	the gadget driver, including descriptor and configuration
780*4882a593Smuzhiyun  *	management.  The 16 bit members of the setup data are in
781*4882a593Smuzhiyun  *	USB byte order. Called in_interrupt; this may not sleep.  Driver
782*4882a593Smuzhiyun  *	queues a response to ep0, or returns negative to stall.
783*4882a593Smuzhiyun  * @disconnect: Invoked after all transfers have been stopped,
784*4882a593Smuzhiyun  *	when the host is disconnected.  May be called in_interrupt; this
785*4882a593Smuzhiyun  *	may not sleep.  Some devices can't detect disconnect, so this might
786*4882a593Smuzhiyun  *	not be called except as part of controller shutdown.
787*4882a593Smuzhiyun  * @unbind: Invoked when the driver is unbound from a gadget,
788*4882a593Smuzhiyun  *	usually from rmmod (after a disconnect is reported).
789*4882a593Smuzhiyun  *	Called in a context that permits sleeping.
790*4882a593Smuzhiyun  * @suspend: Invoked on USB suspend.  May be called in_interrupt.
791*4882a593Smuzhiyun  * @resume: Invoked on USB resume.  May be called in_interrupt.
792*4882a593Smuzhiyun  * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers
793*4882a593Smuzhiyun  *	and should be called in_interrupt.
794*4882a593Smuzhiyun  *
795*4882a593Smuzhiyun  * Devices are disabled till a gadget driver successfully bind()s, which
796*4882a593Smuzhiyun  * means the driver will handle setup() requests needed to enumerate (and
797*4882a593Smuzhiyun  * meet "chapter 9" requirements) then do some useful work.
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * If gadget->is_otg is true, the gadget driver must provide an OTG
800*4882a593Smuzhiyun  * descriptor during enumeration, or else fail the bind() call.  In such
801*4882a593Smuzhiyun  * cases, no USB traffic may flow until both bind() returns without
802*4882a593Smuzhiyun  * having called usb_gadget_disconnect(), and the USB host stack has
803*4882a593Smuzhiyun  * initialized.
804*4882a593Smuzhiyun  *
805*4882a593Smuzhiyun  * Drivers use hardware-specific knowledge to configure the usb hardware.
806*4882a593Smuzhiyun  * endpoint addressing is only one of several hardware characteristics that
807*4882a593Smuzhiyun  * are in descriptors the ep0 implementation returns from setup() calls.
808*4882a593Smuzhiyun  *
809*4882a593Smuzhiyun  * Except for ep0 implementation, most driver code shouldn't need change to
810*4882a593Smuzhiyun  * run on top of different usb controllers.  It'll use endpoints set up by
811*4882a593Smuzhiyun  * that ep0 implementation.
812*4882a593Smuzhiyun  *
813*4882a593Smuzhiyun  * The usb controller driver handles a few standard usb requests.  Those
814*4882a593Smuzhiyun  * include set_address, and feature flags for devices, interfaces, and
815*4882a593Smuzhiyun  * endpoints (the get_status, set_feature, and clear_feature requests).
816*4882a593Smuzhiyun  *
817*4882a593Smuzhiyun  * Accordingly, the driver's setup() callback must always implement all
818*4882a593Smuzhiyun  * get_descriptor requests, returning at least a device descriptor and
819*4882a593Smuzhiyun  * a configuration descriptor.  Drivers must make sure the endpoint
820*4882a593Smuzhiyun  * descriptors match any hardware constraints. Some hardware also constrains
821*4882a593Smuzhiyun  * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  * The driver's setup() callback must also implement set_configuration,
824*4882a593Smuzhiyun  * and should also implement set_interface, get_configuration, and
825*4882a593Smuzhiyun  * get_interface.  Setting a configuration (or interface) is where
826*4882a593Smuzhiyun  * endpoints should be activated or (config 0) shut down.
827*4882a593Smuzhiyun  *
828*4882a593Smuzhiyun  * (Note that only the default control endpoint is supported.  Neither
829*4882a593Smuzhiyun  * hosts nor devices generally support control traffic except to ep0.)
830*4882a593Smuzhiyun  *
831*4882a593Smuzhiyun  * Most devices will ignore USB suspend/resume operations, and so will
832*4882a593Smuzhiyun  * not provide those callbacks.  However, some may need to change modes
833*4882a593Smuzhiyun  * when the host is not longer directing those activities.  For example,
834*4882a593Smuzhiyun  * local controls (buttons, dials, etc) may need to be re-enabled since
835*4882a593Smuzhiyun  * the (remote) host can't do that any longer; or an error state might
836*4882a593Smuzhiyun  * be cleared, to make the device behave identically whether or not
837*4882a593Smuzhiyun  * power is maintained.
838*4882a593Smuzhiyun  */
839*4882a593Smuzhiyun struct usb_gadget_driver {
840*4882a593Smuzhiyun 	char			*function;
841*4882a593Smuzhiyun 	enum usb_device_speed	speed;
842*4882a593Smuzhiyun 	int			(*bind)(struct usb_gadget *);
843*4882a593Smuzhiyun 	void			(*unbind)(struct usb_gadget *);
844*4882a593Smuzhiyun 	int			(*setup)(struct usb_gadget *,
845*4882a593Smuzhiyun 					const struct usb_ctrlrequest *);
846*4882a593Smuzhiyun 	void			(*disconnect)(struct usb_gadget *);
847*4882a593Smuzhiyun 	void			(*suspend)(struct usb_gadget *);
848*4882a593Smuzhiyun 	void			(*resume)(struct usb_gadget *);
849*4882a593Smuzhiyun 	void			(*reset)(struct usb_gadget *);
850*4882a593Smuzhiyun };
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun /* driver modules register and unregister, as usual.
856*4882a593Smuzhiyun  * these calls must be made in a context that can sleep.
857*4882a593Smuzhiyun  *
858*4882a593Smuzhiyun  * these will usually be implemented directly by the hardware-dependent
859*4882a593Smuzhiyun  * usb bus interface driver, which will only support a single driver.
860*4882a593Smuzhiyun  */
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun /**
863*4882a593Smuzhiyun  * usb_gadget_register_driver - register a gadget driver
864*4882a593Smuzhiyun  * @driver:the driver being registered
865*4882a593Smuzhiyun  *
866*4882a593Smuzhiyun  * Call this in your gadget driver's module initialization function,
867*4882a593Smuzhiyun  * to tell the underlying usb controller driver about your driver.
868*4882a593Smuzhiyun  * The driver's bind() function will be called to bind it to a
869*4882a593Smuzhiyun  * gadget before this registration call returns.  It's expected that
870*4882a593Smuzhiyun  * the bind() functions will be in init sections.
871*4882a593Smuzhiyun  * This function must be called in a context that can sleep.
872*4882a593Smuzhiyun  */
873*4882a593Smuzhiyun int usb_gadget_register_driver(struct usb_gadget_driver *driver);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun /**
876*4882a593Smuzhiyun  * usb_gadget_unregister_driver - unregister a gadget driver
877*4882a593Smuzhiyun  * @driver:the driver being unregistered
878*4882a593Smuzhiyun  *
879*4882a593Smuzhiyun  * Call this in your gadget driver's module cleanup function,
880*4882a593Smuzhiyun  * to tell the underlying usb controller that your driver is
881*4882a593Smuzhiyun  * going away.  If the controller is connected to a USB host,
882*4882a593Smuzhiyun  * it will first disconnect().  The driver is also requested
883*4882a593Smuzhiyun  * to unbind() and clean up any device state, before this procedure
884*4882a593Smuzhiyun  * finally returns.  It's expected that the unbind() functions
885*4882a593Smuzhiyun  * will in in exit sections, so may not be linked in some kernels.
886*4882a593Smuzhiyun  * This function must be called in a context that can sleep.
887*4882a593Smuzhiyun  */
888*4882a593Smuzhiyun int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun int usb_add_gadget_udc_release(struct device *parent,
891*4882a593Smuzhiyun 		struct usb_gadget *gadget, void (*release)(struct device *dev));
892*4882a593Smuzhiyun int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
893*4882a593Smuzhiyun void usb_del_gadget_udc(struct usb_gadget *gadget);
894*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun /* utility to simplify dealing with string descriptors */
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun /**
899*4882a593Smuzhiyun  * struct usb_gadget_strings - a set of USB strings in a given language
900*4882a593Smuzhiyun  * @language:identifies the strings' language (0x0409 for en-us)
901*4882a593Smuzhiyun  * @strings:array of strings with their ids
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  * If you're using usb_gadget_get_string(), use this to wrap all the
904*4882a593Smuzhiyun  * strings for a given language.
905*4882a593Smuzhiyun  */
906*4882a593Smuzhiyun struct usb_gadget_strings {
907*4882a593Smuzhiyun 	u16			language;	/* 0x0409 for en-us */
908*4882a593Smuzhiyun 	struct usb_string	*strings;
909*4882a593Smuzhiyun };
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun /* put descriptor for string with that id into buf (buflen >= 256) */
912*4882a593Smuzhiyun int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun /* utility to simplify managing config descriptors */
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun /* write vector of descriptors into buffer */
919*4882a593Smuzhiyun int usb_descriptor_fillbuf(void *, unsigned,
920*4882a593Smuzhiyun 		const struct usb_descriptor_header **);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /* build config descriptor from single descriptor vector */
923*4882a593Smuzhiyun int usb_gadget_config_buf(const struct usb_config_descriptor *config,
924*4882a593Smuzhiyun 	void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
927*4882a593Smuzhiyun /* utility to simplify map/unmap of usb_requests to/from DMA */
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun extern int usb_gadget_map_request(struct usb_gadget *gadget,
930*4882a593Smuzhiyun 				  struct usb_request *req, int is_in);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
933*4882a593Smuzhiyun 				     struct usb_request *req, int is_in);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun /* utility to set gadget state properly */
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun extern void usb_gadget_set_state(struct usb_gadget *gadget,
940*4882a593Smuzhiyun 				 enum usb_device_state state);
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun /* utility to tell udc core that the bus reset occurs */
945*4882a593Smuzhiyun extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
946*4882a593Smuzhiyun 				 struct usb_gadget_driver *driver);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun /* utility to give requests back to the gadget layer */
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun extern void usb_gadget_giveback_request(struct usb_ep *ep,
953*4882a593Smuzhiyun 					struct usb_request *req);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun /* utility wrapping a simple endpoint selection policy */
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
960*4882a593Smuzhiyun 			struct usb_endpoint_descriptor *);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun extern void usb_ep_autoconfig_reset(struct usb_gadget *);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun extern int usb_gadget_handle_interrupts(int index);
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB_GADGET)
967*4882a593Smuzhiyun int usb_gadget_initialize(int index);
968*4882a593Smuzhiyun int usb_gadget_release(int index);
969*4882a593Smuzhiyun int dm_usb_gadget_handle_interrupts(struct udevice *dev);
970*4882a593Smuzhiyun #else
971*4882a593Smuzhiyun #include <usb.h>
usb_gadget_initialize(int index)972*4882a593Smuzhiyun static inline int usb_gadget_initialize(int index)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	return board_usb_init(index, USB_INIT_DEVICE);
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun 
usb_gadget_release(int index)977*4882a593Smuzhiyun static inline int usb_gadget_release(int index)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	return board_usb_cleanup(index, USB_INIT_DEVICE);
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun #endif
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun #endif	/* __LINUX_USB_GADGET_H */
984