1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * <linux/usb/gadget.h>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * We call the USB code inside a Linux-based peripheral device a "gadget"
6*4882a593Smuzhiyun * driver, except for the hardware-specific bus glue. One USB host can
7*4882a593Smuzhiyun * talk to many USB gadgets, but the gadgets are only able to communicate
8*4882a593Smuzhiyun * to one host.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * (C) Copyright 2002-2004 by David Brownell
12*4882a593Smuzhiyun * All Rights Reserved.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This software is licensed under the GNU GPL version 2.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #ifndef __LINUX_USB_GADGET_H
18*4882a593Smuzhiyun #define __LINUX_USB_GADGET_H
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <linux/device.h>
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/init.h>
23*4882a593Smuzhiyun #include <linux/list.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/scatterlist.h>
26*4882a593Smuzhiyun #include <linux/types.h>
27*4882a593Smuzhiyun #include <linux/workqueue.h>
28*4882a593Smuzhiyun #include <linux/usb/ch9.h>
29*4882a593Smuzhiyun #include <linux/android_kabi.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define UDC_TRACE_STR_MAX 512
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct usb_ep;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun * struct usb_request - describes one i/o request
37*4882a593Smuzhiyun * @buf: Buffer used for data. Always provide this; some controllers
38*4882a593Smuzhiyun * only use PIO, or don't use DMA for some endpoints.
39*4882a593Smuzhiyun * @dma: DMA address corresponding to 'buf'. If you don't set this
40*4882a593Smuzhiyun * field, and the usb controller needs one, it is responsible
41*4882a593Smuzhiyun * for mapping and unmapping the buffer.
42*4882a593Smuzhiyun * @sg: a scatterlist for SG-capable controllers.
43*4882a593Smuzhiyun * @num_sgs: number of SG entries
44*4882a593Smuzhiyun * @num_mapped_sgs: number of SG entries mapped to DMA (internal)
45*4882a593Smuzhiyun * @length: Length of that data
46*4882a593Smuzhiyun * @stream_id: The stream id, when USB3.0 bulk streams are being used
47*4882a593Smuzhiyun * @is_last: Indicates if this is the last request of a stream_id before
48*4882a593Smuzhiyun * switching to a different stream (required for DWC3 controllers).
49*4882a593Smuzhiyun * @no_interrupt: If true, hints that no completion irq is needed.
50*4882a593Smuzhiyun * Helpful sometimes with deep request queues that are handled
51*4882a593Smuzhiyun * directly by DMA controllers.
52*4882a593Smuzhiyun * @zero: If true, when writing data, makes the last packet be "short"
53*4882a593Smuzhiyun * by adding a zero length packet as needed;
54*4882a593Smuzhiyun * @short_not_ok: When reading data, makes short packets be
55*4882a593Smuzhiyun * treated as errors (queue stops advancing till cleanup).
56*4882a593Smuzhiyun * @dma_mapped: Indicates if request has been mapped to DMA (internal)
57*4882a593Smuzhiyun * @complete: Function called when request completes, so this request and
58*4882a593Smuzhiyun * its buffer may be re-used. The function will always be called with
59*4882a593Smuzhiyun * interrupts disabled, and it must not sleep.
60*4882a593Smuzhiyun * Reads terminate with a short packet, or when the buffer fills,
61*4882a593Smuzhiyun * whichever comes first. When writes terminate, some data bytes
62*4882a593Smuzhiyun * will usually still be in flight (often in a hardware fifo).
63*4882a593Smuzhiyun * Errors (for reads or writes) stop the queue from advancing
64*4882a593Smuzhiyun * until the completion function returns, so that any transfers
65*4882a593Smuzhiyun * invalidated by the error may first be dequeued.
66*4882a593Smuzhiyun * @context: For use by the completion callback
67*4882a593Smuzhiyun * @list: For use by the gadget driver.
68*4882a593Smuzhiyun * @frame_number: Reports the interval number in (micro)frame in which the
69*4882a593Smuzhiyun * isochronous transfer was transmitted or received.
70*4882a593Smuzhiyun * @status: Reports completion code, zero or a negative errno.
71*4882a593Smuzhiyun * Normally, faults block the transfer queue from advancing until
72*4882a593Smuzhiyun * the completion callback returns.
73*4882a593Smuzhiyun * Code "-ESHUTDOWN" indicates completion caused by device disconnect,
74*4882a593Smuzhiyun * or when the driver disabled the endpoint.
75*4882a593Smuzhiyun * @actual: Reports bytes transferred to/from the buffer. For reads (OUT
76*4882a593Smuzhiyun * transfers) this may be less than the requested length. If the
77*4882a593Smuzhiyun * short_not_ok flag is set, short reads are treated as errors
78*4882a593Smuzhiyun * even when status otherwise indicates successful completion.
79*4882a593Smuzhiyun * Note that for writes (IN transfers) some data bytes may still
80*4882a593Smuzhiyun * reside in a device-side FIFO when the request is reported as
81*4882a593Smuzhiyun * complete.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * These are allocated/freed through the endpoint they're used with. The
84*4882a593Smuzhiyun * hardware's driver can add extra per-request data to the memory it returns,
85*4882a593Smuzhiyun * which often avoids separate memory allocations (potential failures),
86*4882a593Smuzhiyun * later when the request is queued.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * Request flags affect request handling, such as whether a zero length
89*4882a593Smuzhiyun * packet is written (the "zero" flag), whether a short read should be
90*4882a593Smuzhiyun * treated as an error (blocking request queue advance, the "short_not_ok"
91*4882a593Smuzhiyun * flag), or hinting that an interrupt is not required (the "no_interrupt"
92*4882a593Smuzhiyun * flag, for use with deep request queues).
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * Bulk endpoints can use any size buffers, and can also be used for interrupt
95*4882a593Smuzhiyun * transfers. interrupt-only endpoints can be much less functional.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * NOTE: this is analogous to 'struct urb' on the host side, except that
98*4882a593Smuzhiyun * it's thinner and promotes more pre-allocation.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun struct usb_request {
102*4882a593Smuzhiyun void *buf;
103*4882a593Smuzhiyun unsigned length;
104*4882a593Smuzhiyun dma_addr_t dma;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun struct scatterlist *sg;
107*4882a593Smuzhiyun unsigned num_sgs;
108*4882a593Smuzhiyun unsigned num_mapped_sgs;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun unsigned stream_id:16;
111*4882a593Smuzhiyun unsigned is_last:1;
112*4882a593Smuzhiyun unsigned no_interrupt:1;
113*4882a593Smuzhiyun unsigned zero:1;
114*4882a593Smuzhiyun unsigned short_not_ok:1;
115*4882a593Smuzhiyun unsigned dma_mapped:1;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun void (*complete)(struct usb_ep *ep,
118*4882a593Smuzhiyun struct usb_request *req);
119*4882a593Smuzhiyun void *context;
120*4882a593Smuzhiyun struct list_head list;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun unsigned frame_number; /* ISO ONLY */
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun int status;
125*4882a593Smuzhiyun unsigned actual;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* endpoint-specific parts of the api to the usb controller hardware.
133*4882a593Smuzhiyun * unlike the urb model, (de)multiplexing layers are not required.
134*4882a593Smuzhiyun * (so this api could slash overhead if used on the host side...)
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * note that device side usb controllers commonly differ in how many
137*4882a593Smuzhiyun * endpoints they support, as well as their capabilities.
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun struct usb_ep_ops {
140*4882a593Smuzhiyun int (*enable) (struct usb_ep *ep,
141*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc);
142*4882a593Smuzhiyun int (*disable) (struct usb_ep *ep);
143*4882a593Smuzhiyun void (*dispose) (struct usb_ep *ep);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun struct usb_request *(*alloc_request) (struct usb_ep *ep,
146*4882a593Smuzhiyun gfp_t gfp_flags);
147*4882a593Smuzhiyun void (*free_request) (struct usb_ep *ep, struct usb_request *req);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun int (*queue) (struct usb_ep *ep, struct usb_request *req,
150*4882a593Smuzhiyun gfp_t gfp_flags);
151*4882a593Smuzhiyun int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun int (*set_halt) (struct usb_ep *ep, int value);
154*4882a593Smuzhiyun int (*set_wedge) (struct usb_ep *ep);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun int (*fifo_status) (struct usb_ep *ep);
157*4882a593Smuzhiyun void (*fifo_flush) (struct usb_ep *ep);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * struct usb_ep_caps - endpoint capabilities description
164*4882a593Smuzhiyun * @type_control:Endpoint supports control type (reserved for ep0).
165*4882a593Smuzhiyun * @type_iso:Endpoint supports isochronous transfers.
166*4882a593Smuzhiyun * @type_bulk:Endpoint supports bulk transfers.
167*4882a593Smuzhiyun * @type_int:Endpoint supports interrupt transfers.
168*4882a593Smuzhiyun * @dir_in:Endpoint supports IN direction.
169*4882a593Smuzhiyun * @dir_out:Endpoint supports OUT direction.
170*4882a593Smuzhiyun */
171*4882a593Smuzhiyun struct usb_ep_caps {
172*4882a593Smuzhiyun unsigned type_control:1;
173*4882a593Smuzhiyun unsigned type_iso:1;
174*4882a593Smuzhiyun unsigned type_bulk:1;
175*4882a593Smuzhiyun unsigned type_int:1;
176*4882a593Smuzhiyun unsigned dir_in:1;
177*4882a593Smuzhiyun unsigned dir_out:1;
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun #define USB_EP_CAPS_TYPE_CONTROL 0x01
181*4882a593Smuzhiyun #define USB_EP_CAPS_TYPE_ISO 0x02
182*4882a593Smuzhiyun #define USB_EP_CAPS_TYPE_BULK 0x04
183*4882a593Smuzhiyun #define USB_EP_CAPS_TYPE_INT 0x08
184*4882a593Smuzhiyun #define USB_EP_CAPS_TYPE_ALL \
185*4882a593Smuzhiyun (USB_EP_CAPS_TYPE_ISO | USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT)
186*4882a593Smuzhiyun #define USB_EP_CAPS_DIR_IN 0x01
187*4882a593Smuzhiyun #define USB_EP_CAPS_DIR_OUT 0x02
188*4882a593Smuzhiyun #define USB_EP_CAPS_DIR_ALL (USB_EP_CAPS_DIR_IN | USB_EP_CAPS_DIR_OUT)
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun #define USB_EP_CAPS(_type, _dir) \
191*4882a593Smuzhiyun { \
192*4882a593Smuzhiyun .type_control = !!(_type & USB_EP_CAPS_TYPE_CONTROL), \
193*4882a593Smuzhiyun .type_iso = !!(_type & USB_EP_CAPS_TYPE_ISO), \
194*4882a593Smuzhiyun .type_bulk = !!(_type & USB_EP_CAPS_TYPE_BULK), \
195*4882a593Smuzhiyun .type_int = !!(_type & USB_EP_CAPS_TYPE_INT), \
196*4882a593Smuzhiyun .dir_in = !!(_dir & USB_EP_CAPS_DIR_IN), \
197*4882a593Smuzhiyun .dir_out = !!(_dir & USB_EP_CAPS_DIR_OUT), \
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun * struct usb_ep - device side representation of USB endpoint
202*4882a593Smuzhiyun * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
203*4882a593Smuzhiyun * @ops: Function pointers used to access hardware-specific operations.
204*4882a593Smuzhiyun * @ep_list:the gadget's ep_list holds all of its endpoints
205*4882a593Smuzhiyun * @caps:The structure describing types and directions supported by endoint.
206*4882a593Smuzhiyun * @enabled: The current endpoint enabled/disabled state.
207*4882a593Smuzhiyun * @claimed: True if this endpoint is claimed by a function.
208*4882a593Smuzhiyun * @maxpacket:The maximum packet size used on this endpoint. The initial
209*4882a593Smuzhiyun * value can sometimes be reduced (hardware allowing), according to
210*4882a593Smuzhiyun * the endpoint descriptor used to configure the endpoint.
211*4882a593Smuzhiyun * @maxpacket_limit:The maximum packet size value which can be handled by this
212*4882a593Smuzhiyun * endpoint. It's set once by UDC driver when endpoint is initialized, and
213*4882a593Smuzhiyun * should not be changed. Should not be confused with maxpacket.
214*4882a593Smuzhiyun * @max_streams: The maximum number of streams supported
215*4882a593Smuzhiyun * by this EP (0 - 16, actual number is 2^n)
216*4882a593Smuzhiyun * @mult: multiplier, 'mult' value for SS Isoc EPs
217*4882a593Smuzhiyun * @maxburst: the maximum number of bursts supported by this EP (for usb3)
218*4882a593Smuzhiyun * @driver_data:for use by the gadget driver.
219*4882a593Smuzhiyun * @address: used to identify the endpoint when finding descriptor that
220*4882a593Smuzhiyun * matches connection speed
221*4882a593Smuzhiyun * @desc: endpoint descriptor. This pointer is set before the endpoint is
222*4882a593Smuzhiyun * enabled and remains valid until the endpoint is disabled.
223*4882a593Smuzhiyun * @comp_desc: In case of SuperSpeed support, this is the endpoint companion
224*4882a593Smuzhiyun * descriptor that is used to configure the endpoint
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * the bus controller driver lists all the general purpose endpoints in
227*4882a593Smuzhiyun * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list,
228*4882a593Smuzhiyun * and is accessed only in response to a driver setup() callback.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun struct usb_ep {
232*4882a593Smuzhiyun void *driver_data;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun const char *name;
235*4882a593Smuzhiyun const struct usb_ep_ops *ops;
236*4882a593Smuzhiyun struct list_head ep_list;
237*4882a593Smuzhiyun struct usb_ep_caps caps;
238*4882a593Smuzhiyun bool claimed;
239*4882a593Smuzhiyun bool enabled;
240*4882a593Smuzhiyun unsigned maxpacket:16;
241*4882a593Smuzhiyun unsigned maxpacket_limit:16;
242*4882a593Smuzhiyun unsigned max_streams:16;
243*4882a593Smuzhiyun unsigned mult:2;
244*4882a593Smuzhiyun unsigned maxburst:5;
245*4882a593Smuzhiyun u8 address;
246*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc;
247*4882a593Smuzhiyun const struct usb_ss_ep_comp_descriptor *comp_desc;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
250*4882a593Smuzhiyun };
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_USB_GADGET)
255*4882a593Smuzhiyun void usb_ep_set_maxpacket_limit(struct usb_ep *ep, unsigned maxpacket_limit);
256*4882a593Smuzhiyun int usb_ep_enable(struct usb_ep *ep);
257*4882a593Smuzhiyun int usb_ep_disable(struct usb_ep *ep);
258*4882a593Smuzhiyun struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
259*4882a593Smuzhiyun void usb_ep_free_request(struct usb_ep *ep, struct usb_request *req);
260*4882a593Smuzhiyun int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags);
261*4882a593Smuzhiyun int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
262*4882a593Smuzhiyun int usb_ep_set_halt(struct usb_ep *ep);
263*4882a593Smuzhiyun int usb_ep_clear_halt(struct usb_ep *ep);
264*4882a593Smuzhiyun int usb_ep_set_wedge(struct usb_ep *ep);
265*4882a593Smuzhiyun int usb_ep_fifo_status(struct usb_ep *ep);
266*4882a593Smuzhiyun void usb_ep_fifo_flush(struct usb_ep *ep);
267*4882a593Smuzhiyun #else
usb_ep_set_maxpacket_limit(struct usb_ep * ep,unsigned maxpacket_limit)268*4882a593Smuzhiyun static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
269*4882a593Smuzhiyun unsigned maxpacket_limit)
270*4882a593Smuzhiyun { }
usb_ep_enable(struct usb_ep * ep)271*4882a593Smuzhiyun static inline int usb_ep_enable(struct usb_ep *ep)
272*4882a593Smuzhiyun { return 0; }
usb_ep_disable(struct usb_ep * ep)273*4882a593Smuzhiyun static inline int usb_ep_disable(struct usb_ep *ep)
274*4882a593Smuzhiyun { return 0; }
usb_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)275*4882a593Smuzhiyun static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
276*4882a593Smuzhiyun gfp_t gfp_flags)
277*4882a593Smuzhiyun { return NULL; }
usb_ep_free_request(struct usb_ep * ep,struct usb_request * req)278*4882a593Smuzhiyun static inline void usb_ep_free_request(struct usb_ep *ep,
279*4882a593Smuzhiyun struct usb_request *req)
280*4882a593Smuzhiyun { }
usb_ep_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)281*4882a593Smuzhiyun static inline int usb_ep_queue(struct usb_ep *ep, struct usb_request *req,
282*4882a593Smuzhiyun gfp_t gfp_flags)
283*4882a593Smuzhiyun { return 0; }
usb_ep_dequeue(struct usb_ep * ep,struct usb_request * req)284*4882a593Smuzhiyun static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
285*4882a593Smuzhiyun { return 0; }
usb_ep_set_halt(struct usb_ep * ep)286*4882a593Smuzhiyun static inline int usb_ep_set_halt(struct usb_ep *ep)
287*4882a593Smuzhiyun { return 0; }
usb_ep_clear_halt(struct usb_ep * ep)288*4882a593Smuzhiyun static inline int usb_ep_clear_halt(struct usb_ep *ep)
289*4882a593Smuzhiyun { return 0; }
usb_ep_set_wedge(struct usb_ep * ep)290*4882a593Smuzhiyun static inline int usb_ep_set_wedge(struct usb_ep *ep)
291*4882a593Smuzhiyun { return 0; }
usb_ep_fifo_status(struct usb_ep * ep)292*4882a593Smuzhiyun static inline int usb_ep_fifo_status(struct usb_ep *ep)
293*4882a593Smuzhiyun { return 0; }
usb_ep_fifo_flush(struct usb_ep * ep)294*4882a593Smuzhiyun static inline void usb_ep_fifo_flush(struct usb_ep *ep)
295*4882a593Smuzhiyun { }
296*4882a593Smuzhiyun #endif /* USB_GADGET */
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun struct usb_dcd_config_params {
301*4882a593Smuzhiyun __u8 bU1devExitLat; /* U1 Device exit Latency */
302*4882a593Smuzhiyun #define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */
303*4882a593Smuzhiyun __le16 bU2DevExitLat; /* U2 Device exit Latency */
304*4882a593Smuzhiyun #define USB_DEFAULT_U2_DEV_EXIT_LAT 0x1F4 /* Less then 500 microsec */
305*4882a593Smuzhiyun __u8 besl_baseline; /* Recommended baseline BESL (0-15) */
306*4882a593Smuzhiyun __u8 besl_deep; /* Recommended deep BESL (0-15) */
307*4882a593Smuzhiyun #define USB_DEFAULT_BESL_UNSPECIFIED 0xFF /* No recommended value */
308*4882a593Smuzhiyun };
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun struct usb_gadget;
312*4882a593Smuzhiyun struct usb_gadget_driver;
313*4882a593Smuzhiyun struct usb_udc;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /* the rest of the api to the controller hardware: device operations,
316*4882a593Smuzhiyun * which don't involve endpoints (or i/o).
317*4882a593Smuzhiyun */
318*4882a593Smuzhiyun struct usb_gadget_ops {
319*4882a593Smuzhiyun int (*get_frame)(struct usb_gadget *);
320*4882a593Smuzhiyun int (*wakeup)(struct usb_gadget *);
321*4882a593Smuzhiyun int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
322*4882a593Smuzhiyun int (*vbus_session) (struct usb_gadget *, int is_active);
323*4882a593Smuzhiyun int (*vbus_draw) (struct usb_gadget *, unsigned mA);
324*4882a593Smuzhiyun int (*pullup) (struct usb_gadget *, int is_on);
325*4882a593Smuzhiyun int (*ioctl)(struct usb_gadget *,
326*4882a593Smuzhiyun unsigned code, unsigned long param);
327*4882a593Smuzhiyun void (*get_config_params)(struct usb_gadget *,
328*4882a593Smuzhiyun struct usb_dcd_config_params *);
329*4882a593Smuzhiyun int (*udc_start)(struct usb_gadget *,
330*4882a593Smuzhiyun struct usb_gadget_driver *);
331*4882a593Smuzhiyun int (*udc_stop)(struct usb_gadget *);
332*4882a593Smuzhiyun void (*udc_set_speed)(struct usb_gadget *, enum usb_device_speed);
333*4882a593Smuzhiyun void (*udc_set_ssp_rate)(struct usb_gadget *gadget,
334*4882a593Smuzhiyun enum usb_ssp_rate rate);
335*4882a593Smuzhiyun void (*udc_async_callbacks)(struct usb_gadget *gadget, bool enable);
336*4882a593Smuzhiyun struct usb_ep *(*match_ep)(struct usb_gadget *,
337*4882a593Smuzhiyun struct usb_endpoint_descriptor *,
338*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *);
339*4882a593Smuzhiyun int (*check_config)(struct usb_gadget *gadget);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
342*4882a593Smuzhiyun ANDROID_KABI_RESERVE(2);
343*4882a593Smuzhiyun ANDROID_KABI_RESERVE(3);
344*4882a593Smuzhiyun ANDROID_KABI_RESERVE(4);
345*4882a593Smuzhiyun };
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /**
348*4882a593Smuzhiyun * struct usb_gadget - represents a usb device
349*4882a593Smuzhiyun * @work: (internal use) Workqueue to be used for sysfs_notify()
350*4882a593Smuzhiyun * @udc: struct usb_udc pointer for this gadget
351*4882a593Smuzhiyun * @ops: Function pointers used to access hardware-specific operations.
352*4882a593Smuzhiyun * @ep0: Endpoint zero, used when reading or writing responses to
353*4882a593Smuzhiyun * driver setup() requests
354*4882a593Smuzhiyun * @ep_list: List of other endpoints supported by the device.
355*4882a593Smuzhiyun * @speed: Speed of current connection to USB host.
356*4882a593Smuzhiyun * @max_speed: Maximal speed the UDC can handle. UDC must support this
357*4882a593Smuzhiyun * and all slower speeds.
358*4882a593Smuzhiyun * @ssp_rate: Current connected SuperSpeed Plus signaling rate and lane count.
359*4882a593Smuzhiyun * @max_ssp_rate: Maximum SuperSpeed Plus signaling rate and lane count the UDC
360*4882a593Smuzhiyun * can handle. The UDC must support this and all slower speeds and lower
361*4882a593Smuzhiyun * number of lanes.
362*4882a593Smuzhiyun * @state: the state we are now (attached, suspended, configured, etc)
363*4882a593Smuzhiyun * @name: Identifies the controller hardware type. Used in diagnostics
364*4882a593Smuzhiyun * and sometimes configuration.
365*4882a593Smuzhiyun * @dev: Driver model state for this abstract device.
366*4882a593Smuzhiyun * @isoch_delay: value from Set Isoch Delay request. Only valid on SS/SSP
367*4882a593Smuzhiyun * @out_epnum: last used out ep number
368*4882a593Smuzhiyun * @in_epnum: last used in ep number
369*4882a593Smuzhiyun * @mA: last set mA value
370*4882a593Smuzhiyun * @otg_caps: OTG capabilities of this gadget.
371*4882a593Smuzhiyun * @sg_supported: true if we can handle scatter-gather
372*4882a593Smuzhiyun * @is_otg: True if the USB device port uses a Mini-AB jack, so that the
373*4882a593Smuzhiyun * gadget driver must provide a USB OTG descriptor.
374*4882a593Smuzhiyun * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable
375*4882a593Smuzhiyun * is in the Mini-AB jack, and HNP has been used to switch roles
376*4882a593Smuzhiyun * so that the "A" device currently acts as A-Peripheral, not A-Host.
377*4882a593Smuzhiyun * @a_hnp_support: OTG device feature flag, indicating that the A-Host
378*4882a593Smuzhiyun * supports HNP at this port.
379*4882a593Smuzhiyun * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
380*4882a593Smuzhiyun * only supports HNP on a different root port.
381*4882a593Smuzhiyun * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
382*4882a593Smuzhiyun * enabled HNP support.
383*4882a593Smuzhiyun * @hnp_polling_support: OTG device feature flag, indicating if the OTG device
384*4882a593Smuzhiyun * in peripheral mode can support HNP polling.
385*4882a593Smuzhiyun * @host_request_flag: OTG device feature flag, indicating if A-Peripheral
386*4882a593Smuzhiyun * or B-Peripheral wants to take host role.
387*4882a593Smuzhiyun * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
388*4882a593Smuzhiyun * MaxPacketSize.
389*4882a593Smuzhiyun * @quirk_altset_not_supp: UDC controller doesn't support alt settings.
390*4882a593Smuzhiyun * @quirk_stall_not_supp: UDC controller doesn't support stalling.
391*4882a593Smuzhiyun * @quirk_zlp_not_supp: UDC controller doesn't support ZLP.
392*4882a593Smuzhiyun * @quirk_avoids_skb_reserve: udc/platform wants to avoid skb_reserve() in
393*4882a593Smuzhiyun * u_ether.c to improve performance.
394*4882a593Smuzhiyun * @is_selfpowered: if the gadget is self-powered.
395*4882a593Smuzhiyun * @deactivated: True if gadget is deactivated - in deactivated state it cannot
396*4882a593Smuzhiyun * be connected.
397*4882a593Smuzhiyun * @connected: True if gadget is connected.
398*4882a593Smuzhiyun * @lpm_capable: If the gadget max_speed is FULL or HIGH, this flag
399*4882a593Smuzhiyun * indicates that it supports LPM as per the LPM ECN & errata.
400*4882a593Smuzhiyun * @irq: the interrupt number for device controller.
401*4882a593Smuzhiyun *
402*4882a593Smuzhiyun * Gadgets have a mostly-portable "gadget driver" implementing device
403*4882a593Smuzhiyun * functions, handling all usb configurations and interfaces. Gadget
404*4882a593Smuzhiyun * drivers talk to hardware-specific code indirectly, through ops vectors.
405*4882a593Smuzhiyun * That insulates the gadget driver from hardware details, and packages
406*4882a593Smuzhiyun * the hardware endpoints through generic i/o queues. The "usb_gadget"
407*4882a593Smuzhiyun * and "usb_ep" interfaces provide that insulation from the hardware.
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * Except for the driver data, all fields in this structure are
410*4882a593Smuzhiyun * read-only to the gadget driver. That driver data is part of the
411*4882a593Smuzhiyun * "driver model" infrastructure in 2.6 (and later) kernels, and for
412*4882a593Smuzhiyun * earlier systems is grouped in a similar structure that's not known
413*4882a593Smuzhiyun * to the rest of the kernel.
414*4882a593Smuzhiyun *
415*4882a593Smuzhiyun * Values of the three OTG device feature flags are updated before the
416*4882a593Smuzhiyun * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
417*4882a593Smuzhiyun * driver suspend() calls. They are valid only when is_otg, and when the
418*4882a593Smuzhiyun * device is acting as a B-Peripheral (so is_a_peripheral is false).
419*4882a593Smuzhiyun */
420*4882a593Smuzhiyun struct usb_gadget {
421*4882a593Smuzhiyun struct work_struct work;
422*4882a593Smuzhiyun struct usb_udc *udc;
423*4882a593Smuzhiyun /* readonly to gadget driver */
424*4882a593Smuzhiyun const struct usb_gadget_ops *ops;
425*4882a593Smuzhiyun struct usb_ep *ep0;
426*4882a593Smuzhiyun struct list_head ep_list; /* of usb_ep */
427*4882a593Smuzhiyun enum usb_device_speed speed;
428*4882a593Smuzhiyun enum usb_device_speed max_speed;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* USB SuperSpeed Plus only */
431*4882a593Smuzhiyun enum usb_ssp_rate ssp_rate;
432*4882a593Smuzhiyun enum usb_ssp_rate max_ssp_rate;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun enum usb_device_state state;
435*4882a593Smuzhiyun const char *name;
436*4882a593Smuzhiyun struct device dev;
437*4882a593Smuzhiyun unsigned isoch_delay;
438*4882a593Smuzhiyun unsigned out_epnum;
439*4882a593Smuzhiyun unsigned in_epnum;
440*4882a593Smuzhiyun unsigned mA;
441*4882a593Smuzhiyun struct usb_otg_caps *otg_caps;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun unsigned sg_supported:1;
444*4882a593Smuzhiyun unsigned is_otg:1;
445*4882a593Smuzhiyun unsigned is_a_peripheral:1;
446*4882a593Smuzhiyun unsigned b_hnp_enable:1;
447*4882a593Smuzhiyun unsigned a_hnp_support:1;
448*4882a593Smuzhiyun unsigned a_alt_hnp_support:1;
449*4882a593Smuzhiyun unsigned hnp_polling_support:1;
450*4882a593Smuzhiyun unsigned host_request_flag:1;
451*4882a593Smuzhiyun unsigned quirk_ep_out_aligned_size:1;
452*4882a593Smuzhiyun unsigned quirk_altset_not_supp:1;
453*4882a593Smuzhiyun unsigned quirk_stall_not_supp:1;
454*4882a593Smuzhiyun unsigned quirk_zlp_not_supp:1;
455*4882a593Smuzhiyun unsigned quirk_avoids_skb_reserve:1;
456*4882a593Smuzhiyun unsigned is_selfpowered:1;
457*4882a593Smuzhiyun unsigned deactivated:1;
458*4882a593Smuzhiyun unsigned connected:1;
459*4882a593Smuzhiyun unsigned lpm_capable:1;
460*4882a593Smuzhiyun int irq;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
463*4882a593Smuzhiyun ANDROID_KABI_RESERVE(2);
464*4882a593Smuzhiyun ANDROID_KABI_RESERVE(3);
465*4882a593Smuzhiyun ANDROID_KABI_RESERVE(4);
466*4882a593Smuzhiyun };
467*4882a593Smuzhiyun #define work_to_gadget(w) (container_of((w), struct usb_gadget, work))
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /* Interface to the device model */
set_gadget_data(struct usb_gadget * gadget,void * data)470*4882a593Smuzhiyun static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
471*4882a593Smuzhiyun { dev_set_drvdata(&gadget->dev, data); }
get_gadget_data(struct usb_gadget * gadget)472*4882a593Smuzhiyun static inline void *get_gadget_data(struct usb_gadget *gadget)
473*4882a593Smuzhiyun { return dev_get_drvdata(&gadget->dev); }
dev_to_usb_gadget(struct device * dev)474*4882a593Smuzhiyun static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun return container_of(dev, struct usb_gadget, dev);
477*4882a593Smuzhiyun }
usb_get_gadget(struct usb_gadget * gadget)478*4882a593Smuzhiyun static inline struct usb_gadget *usb_get_gadget(struct usb_gadget *gadget)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun get_device(&gadget->dev);
481*4882a593Smuzhiyun return gadget;
482*4882a593Smuzhiyun }
usb_put_gadget(struct usb_gadget * gadget)483*4882a593Smuzhiyun static inline void usb_put_gadget(struct usb_gadget *gadget)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun put_device(&gadget->dev);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun extern void usb_initialize_gadget(struct device *parent,
488*4882a593Smuzhiyun struct usb_gadget *gadget, void (*release)(struct device *dev));
489*4882a593Smuzhiyun extern int usb_add_gadget(struct usb_gadget *gadget);
490*4882a593Smuzhiyun extern void usb_del_gadget(struct usb_gadget *gadget);
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /* Legacy device-model interface */
493*4882a593Smuzhiyun extern int usb_add_gadget_udc_release(struct device *parent,
494*4882a593Smuzhiyun struct usb_gadget *gadget, void (*release)(struct device *dev));
495*4882a593Smuzhiyun extern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
496*4882a593Smuzhiyun extern void usb_del_gadget_udc(struct usb_gadget *gadget);
497*4882a593Smuzhiyun extern char *usb_get_gadget_udc_name(void);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
500*4882a593Smuzhiyun #define gadget_for_each_ep(tmp, gadget) \
501*4882a593Smuzhiyun list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /**
504*4882a593Smuzhiyun * usb_ep_align - returns @len aligned to ep's maxpacketsize.
505*4882a593Smuzhiyun * @ep: the endpoint whose maxpacketsize is used to align @len
506*4882a593Smuzhiyun * @len: buffer size's length to align to @ep's maxpacketsize
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * This helper is used to align buffer's size to an ep's maxpacketsize.
509*4882a593Smuzhiyun */
usb_ep_align(struct usb_ep * ep,size_t len)510*4882a593Smuzhiyun static inline size_t usb_ep_align(struct usb_ep *ep, size_t len)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun int max_packet_size = (size_t)usb_endpoint_maxp(ep->desc) & 0x7ff;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun return round_up(len, max_packet_size);
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /**
518*4882a593Smuzhiyun * usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget
519*4882a593Smuzhiyun * requires quirk_ep_out_aligned_size, otherwise returns len.
520*4882a593Smuzhiyun * @g: controller to check for quirk
521*4882a593Smuzhiyun * @ep: the endpoint whose maxpacketsize is used to align @len
522*4882a593Smuzhiyun * @len: buffer size's length to align to @ep's maxpacketsize
523*4882a593Smuzhiyun *
524*4882a593Smuzhiyun * This helper is used in case it's required for any reason to check and maybe
525*4882a593Smuzhiyun * align buffer's size to an ep's maxpacketsize.
526*4882a593Smuzhiyun */
527*4882a593Smuzhiyun static inline size_t
usb_ep_align_maybe(struct usb_gadget * g,struct usb_ep * ep,size_t len)528*4882a593Smuzhiyun usb_ep_align_maybe(struct usb_gadget *g, struct usb_ep *ep, size_t len)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun return g->quirk_ep_out_aligned_size ? usb_ep_align(ep, len) : len;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /**
534*4882a593Smuzhiyun * gadget_is_altset_supported - return true iff the hardware supports
535*4882a593Smuzhiyun * altsettings
536*4882a593Smuzhiyun * @g: controller to check for quirk
537*4882a593Smuzhiyun */
gadget_is_altset_supported(struct usb_gadget * g)538*4882a593Smuzhiyun static inline int gadget_is_altset_supported(struct usb_gadget *g)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun return !g->quirk_altset_not_supp;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /**
544*4882a593Smuzhiyun * gadget_is_stall_supported - return true iff the hardware supports stalling
545*4882a593Smuzhiyun * @g: controller to check for quirk
546*4882a593Smuzhiyun */
gadget_is_stall_supported(struct usb_gadget * g)547*4882a593Smuzhiyun static inline int gadget_is_stall_supported(struct usb_gadget *g)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun return !g->quirk_stall_not_supp;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /**
553*4882a593Smuzhiyun * gadget_is_zlp_supported - return true iff the hardware supports zlp
554*4882a593Smuzhiyun * @g: controller to check for quirk
555*4882a593Smuzhiyun */
gadget_is_zlp_supported(struct usb_gadget * g)556*4882a593Smuzhiyun static inline int gadget_is_zlp_supported(struct usb_gadget *g)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun return !g->quirk_zlp_not_supp;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun * gadget_avoids_skb_reserve - return true iff the hardware would like to avoid
563*4882a593Smuzhiyun * skb_reserve to improve performance.
564*4882a593Smuzhiyun * @g: controller to check for quirk
565*4882a593Smuzhiyun */
gadget_avoids_skb_reserve(struct usb_gadget * g)566*4882a593Smuzhiyun static inline int gadget_avoids_skb_reserve(struct usb_gadget *g)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun return g->quirk_avoids_skb_reserve;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun * gadget_is_dualspeed - return true iff the hardware handles high speed
573*4882a593Smuzhiyun * @g: controller that might support both high and full speeds
574*4882a593Smuzhiyun */
gadget_is_dualspeed(struct usb_gadget * g)575*4882a593Smuzhiyun static inline int gadget_is_dualspeed(struct usb_gadget *g)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun return g->max_speed >= USB_SPEED_HIGH;
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_superspeed_plus() - return true if the hardware handles
591*4882a593Smuzhiyun * superspeed plus
592*4882a593Smuzhiyun * @g: controller that might support superspeed plus
593*4882a593Smuzhiyun */
gadget_is_superspeed_plus(struct usb_gadget * g)594*4882a593Smuzhiyun static inline int gadget_is_superspeed_plus(struct usb_gadget *g)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun return g->max_speed >= USB_SPEED_SUPER_PLUS;
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /**
600*4882a593Smuzhiyun * gadget_is_otg - return true iff the hardware is OTG-ready
601*4882a593Smuzhiyun * @g: controller that might have a Mini-AB connector
602*4882a593Smuzhiyun *
603*4882a593Smuzhiyun * This is a runtime test, since kernels with a USB-OTG stack sometimes
604*4882a593Smuzhiyun * run on boards which only have a Mini-B (or Mini-A) connector.
605*4882a593Smuzhiyun */
gadget_is_otg(struct usb_gadget * g)606*4882a593Smuzhiyun static inline int gadget_is_otg(struct usb_gadget *g)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun #ifdef CONFIG_USB_OTG
609*4882a593Smuzhiyun return g->is_otg;
610*4882a593Smuzhiyun #else
611*4882a593Smuzhiyun return 0;
612*4882a593Smuzhiyun #endif
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_USB_GADGET)
618*4882a593Smuzhiyun int usb_gadget_frame_number(struct usb_gadget *gadget);
619*4882a593Smuzhiyun int usb_gadget_wakeup(struct usb_gadget *gadget);
620*4882a593Smuzhiyun int usb_gadget_set_selfpowered(struct usb_gadget *gadget);
621*4882a593Smuzhiyun int usb_gadget_clear_selfpowered(struct usb_gadget *gadget);
622*4882a593Smuzhiyun int usb_gadget_vbus_connect(struct usb_gadget *gadget);
623*4882a593Smuzhiyun int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA);
624*4882a593Smuzhiyun int usb_gadget_vbus_disconnect(struct usb_gadget *gadget);
625*4882a593Smuzhiyun int usb_gadget_connect(struct usb_gadget *gadget);
626*4882a593Smuzhiyun int usb_gadget_disconnect(struct usb_gadget *gadget);
627*4882a593Smuzhiyun int usb_gadget_deactivate(struct usb_gadget *gadget);
628*4882a593Smuzhiyun int usb_gadget_activate(struct usb_gadget *gadget);
629*4882a593Smuzhiyun int usb_gadget_check_config(struct usb_gadget *gadget);
630*4882a593Smuzhiyun #else
usb_gadget_frame_number(struct usb_gadget * gadget)631*4882a593Smuzhiyun static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
632*4882a593Smuzhiyun { return 0; }
usb_gadget_wakeup(struct usb_gadget * gadget)633*4882a593Smuzhiyun static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
634*4882a593Smuzhiyun { return 0; }
usb_gadget_set_selfpowered(struct usb_gadget * gadget)635*4882a593Smuzhiyun static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
636*4882a593Smuzhiyun { return 0; }
usb_gadget_clear_selfpowered(struct usb_gadget * gadget)637*4882a593Smuzhiyun static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
638*4882a593Smuzhiyun { return 0; }
usb_gadget_vbus_connect(struct usb_gadget * gadget)639*4882a593Smuzhiyun static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
640*4882a593Smuzhiyun { return 0; }
usb_gadget_vbus_draw(struct usb_gadget * gadget,unsigned mA)641*4882a593Smuzhiyun static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
642*4882a593Smuzhiyun { return 0; }
usb_gadget_vbus_disconnect(struct usb_gadget * gadget)643*4882a593Smuzhiyun static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
644*4882a593Smuzhiyun { return 0; }
usb_gadget_connect(struct usb_gadget * gadget)645*4882a593Smuzhiyun static inline int usb_gadget_connect(struct usb_gadget *gadget)
646*4882a593Smuzhiyun { return 0; }
usb_gadget_disconnect(struct usb_gadget * gadget)647*4882a593Smuzhiyun static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
648*4882a593Smuzhiyun { return 0; }
usb_gadget_deactivate(struct usb_gadget * gadget)649*4882a593Smuzhiyun static inline int usb_gadget_deactivate(struct usb_gadget *gadget)
650*4882a593Smuzhiyun { return 0; }
usb_gadget_activate(struct usb_gadget * gadget)651*4882a593Smuzhiyun static inline int usb_gadget_activate(struct usb_gadget *gadget)
652*4882a593Smuzhiyun { return 0; }
usb_gadget_check_config(struct usb_gadget * gadget)653*4882a593Smuzhiyun static inline int usb_gadget_check_config(struct usb_gadget *gadget)
654*4882a593Smuzhiyun { return 0; }
655*4882a593Smuzhiyun #endif /* CONFIG_USB_GADGET */
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /**
660*4882a593Smuzhiyun * struct usb_gadget_driver - driver for usb gadget devices
661*4882a593Smuzhiyun * @function: String describing the gadget's function
662*4882a593Smuzhiyun * @max_speed: Highest speed the driver handles.
663*4882a593Smuzhiyun * @setup: Invoked for ep0 control requests that aren't handled by
664*4882a593Smuzhiyun * the hardware level driver. Most calls must be handled by
665*4882a593Smuzhiyun * the gadget driver, including descriptor and configuration
666*4882a593Smuzhiyun * management. The 16 bit members of the setup data are in
667*4882a593Smuzhiyun * USB byte order. Called in_interrupt; this may not sleep. Driver
668*4882a593Smuzhiyun * queues a response to ep0, or returns negative to stall.
669*4882a593Smuzhiyun * @disconnect: Invoked after all transfers have been stopped,
670*4882a593Smuzhiyun * when the host is disconnected. May be called in_interrupt; this
671*4882a593Smuzhiyun * may not sleep. Some devices can't detect disconnect, so this might
672*4882a593Smuzhiyun * not be called except as part of controller shutdown.
673*4882a593Smuzhiyun * @bind: the driver's bind callback
674*4882a593Smuzhiyun * @unbind: Invoked when the driver is unbound from a gadget,
675*4882a593Smuzhiyun * usually from rmmod (after a disconnect is reported).
676*4882a593Smuzhiyun * Called in a context that permits sleeping.
677*4882a593Smuzhiyun * @suspend: Invoked on USB suspend. May be called in_interrupt.
678*4882a593Smuzhiyun * @resume: Invoked on USB resume. May be called in_interrupt.
679*4882a593Smuzhiyun * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers
680*4882a593Smuzhiyun * and should be called in_interrupt.
681*4882a593Smuzhiyun * @driver: Driver model state for this driver.
682*4882a593Smuzhiyun * @udc_name: A name of UDC this driver should be bound to. If udc_name is NULL,
683*4882a593Smuzhiyun * this driver will be bound to any available UDC.
684*4882a593Smuzhiyun * @pending: UDC core private data used for deferred probe of this driver.
685*4882a593Smuzhiyun * @match_existing_only: If udc is not found, return an error and don't add this
686*4882a593Smuzhiyun * gadget driver to list of pending driver
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * Devices are disabled till a gadget driver successfully bind()s, which
689*4882a593Smuzhiyun * means the driver will handle setup() requests needed to enumerate (and
690*4882a593Smuzhiyun * meet "chapter 9" requirements) then do some useful work.
691*4882a593Smuzhiyun *
692*4882a593Smuzhiyun * If gadget->is_otg is true, the gadget driver must provide an OTG
693*4882a593Smuzhiyun * descriptor during enumeration, or else fail the bind() call. In such
694*4882a593Smuzhiyun * cases, no USB traffic may flow until both bind() returns without
695*4882a593Smuzhiyun * having called usb_gadget_disconnect(), and the USB host stack has
696*4882a593Smuzhiyun * initialized.
697*4882a593Smuzhiyun *
698*4882a593Smuzhiyun * Drivers use hardware-specific knowledge to configure the usb hardware.
699*4882a593Smuzhiyun * endpoint addressing is only one of several hardware characteristics that
700*4882a593Smuzhiyun * are in descriptors the ep0 implementation returns from setup() calls.
701*4882a593Smuzhiyun *
702*4882a593Smuzhiyun * Except for ep0 implementation, most driver code shouldn't need change to
703*4882a593Smuzhiyun * run on top of different usb controllers. It'll use endpoints set up by
704*4882a593Smuzhiyun * that ep0 implementation.
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * The usb controller driver handles a few standard usb requests. Those
707*4882a593Smuzhiyun * include set_address, and feature flags for devices, interfaces, and
708*4882a593Smuzhiyun * endpoints (the get_status, set_feature, and clear_feature requests).
709*4882a593Smuzhiyun *
710*4882a593Smuzhiyun * Accordingly, the driver's setup() callback must always implement all
711*4882a593Smuzhiyun * get_descriptor requests, returning at least a device descriptor and
712*4882a593Smuzhiyun * a configuration descriptor. Drivers must make sure the endpoint
713*4882a593Smuzhiyun * descriptors match any hardware constraints. Some hardware also constrains
714*4882a593Smuzhiyun * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
715*4882a593Smuzhiyun *
716*4882a593Smuzhiyun * The driver's setup() callback must also implement set_configuration,
717*4882a593Smuzhiyun * and should also implement set_interface, get_configuration, and
718*4882a593Smuzhiyun * get_interface. Setting a configuration (or interface) is where
719*4882a593Smuzhiyun * endpoints should be activated or (config 0) shut down.
720*4882a593Smuzhiyun *
721*4882a593Smuzhiyun * (Note that only the default control endpoint is supported. Neither
722*4882a593Smuzhiyun * hosts nor devices generally support control traffic except to ep0.)
723*4882a593Smuzhiyun *
724*4882a593Smuzhiyun * Most devices will ignore USB suspend/resume operations, and so will
725*4882a593Smuzhiyun * not provide those callbacks. However, some may need to change modes
726*4882a593Smuzhiyun * when the host is not longer directing those activities. For example,
727*4882a593Smuzhiyun * local controls (buttons, dials, etc) may need to be re-enabled since
728*4882a593Smuzhiyun * the (remote) host can't do that any longer; or an error state might
729*4882a593Smuzhiyun * be cleared, to make the device behave identically whether or not
730*4882a593Smuzhiyun * power is maintained.
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun struct usb_gadget_driver {
733*4882a593Smuzhiyun char *function;
734*4882a593Smuzhiyun enum usb_device_speed max_speed;
735*4882a593Smuzhiyun int (*bind)(struct usb_gadget *gadget,
736*4882a593Smuzhiyun struct usb_gadget_driver *driver);
737*4882a593Smuzhiyun void (*unbind)(struct usb_gadget *);
738*4882a593Smuzhiyun int (*setup)(struct usb_gadget *,
739*4882a593Smuzhiyun const struct usb_ctrlrequest *);
740*4882a593Smuzhiyun void (*disconnect)(struct usb_gadget *);
741*4882a593Smuzhiyun void (*suspend)(struct usb_gadget *);
742*4882a593Smuzhiyun void (*resume)(struct usb_gadget *);
743*4882a593Smuzhiyun void (*reset)(struct usb_gadget *);
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun /* FIXME support safe rmmod */
746*4882a593Smuzhiyun struct device_driver driver;
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun char *udc_name;
749*4882a593Smuzhiyun struct list_head pending;
750*4882a593Smuzhiyun unsigned match_existing_only:1;
751*4882a593Smuzhiyun };
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /* driver modules register and unregister, as usual.
758*4882a593Smuzhiyun * these calls must be made in a context that can sleep.
759*4882a593Smuzhiyun *
760*4882a593Smuzhiyun * these will usually be implemented directly by the hardware-dependent
761*4882a593Smuzhiyun * usb bus interface driver, which will only support a single driver.
762*4882a593Smuzhiyun */
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun /**
765*4882a593Smuzhiyun * usb_gadget_probe_driver - probe a gadget driver
766*4882a593Smuzhiyun * @driver: the driver being registered
767*4882a593Smuzhiyun * Context: can sleep
768*4882a593Smuzhiyun *
769*4882a593Smuzhiyun * Call this in your gadget driver's module initialization function,
770*4882a593Smuzhiyun * to tell the underlying usb controller driver about your driver.
771*4882a593Smuzhiyun * The @bind() function will be called to bind it to a gadget before this
772*4882a593Smuzhiyun * registration call returns. It's expected that the @bind() function will
773*4882a593Smuzhiyun * be in init sections.
774*4882a593Smuzhiyun */
775*4882a593Smuzhiyun int usb_gadget_probe_driver(struct usb_gadget_driver *driver);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /**
778*4882a593Smuzhiyun * usb_gadget_unregister_driver - unregister a gadget driver
779*4882a593Smuzhiyun * @driver:the driver being unregistered
780*4882a593Smuzhiyun * Context: can sleep
781*4882a593Smuzhiyun *
782*4882a593Smuzhiyun * Call this in your gadget driver's module cleanup function,
783*4882a593Smuzhiyun * to tell the underlying usb controller that your driver is
784*4882a593Smuzhiyun * going away. If the controller is connected to a USB host,
785*4882a593Smuzhiyun * it will first disconnect(). The driver is also requested
786*4882a593Smuzhiyun * to unbind() and clean up any device state, before this procedure
787*4882a593Smuzhiyun * finally returns. It's expected that the unbind() functions
788*4882a593Smuzhiyun * will be in exit sections, so may not be linked in some kernels.
789*4882a593Smuzhiyun */
790*4882a593Smuzhiyun int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* utility to simplify dealing with string descriptors */
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun * struct usb_string - wraps a C string and its USB id
798*4882a593Smuzhiyun * @id:the (nonzero) ID for this string
799*4882a593Smuzhiyun * @s:the string, in UTF-8 encoding
800*4882a593Smuzhiyun *
801*4882a593Smuzhiyun * If you're using usb_gadget_get_string(), use this to wrap a string
802*4882a593Smuzhiyun * together with its ID.
803*4882a593Smuzhiyun */
804*4882a593Smuzhiyun struct usb_string {
805*4882a593Smuzhiyun u8 id;
806*4882a593Smuzhiyun const char *s;
807*4882a593Smuzhiyun };
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /**
810*4882a593Smuzhiyun * struct usb_gadget_strings - a set of USB strings in a given language
811*4882a593Smuzhiyun * @language:identifies the strings' language (0x0409 for en-us)
812*4882a593Smuzhiyun * @strings:array of strings with their ids
813*4882a593Smuzhiyun *
814*4882a593Smuzhiyun * If you're using usb_gadget_get_string(), use this to wrap all the
815*4882a593Smuzhiyun * strings for a given language.
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun struct usb_gadget_strings {
818*4882a593Smuzhiyun u16 language; /* 0x0409 for en-us */
819*4882a593Smuzhiyun struct usb_string *strings;
820*4882a593Smuzhiyun };
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun struct usb_gadget_string_container {
823*4882a593Smuzhiyun struct list_head list;
824*4882a593Smuzhiyun u8 *stash[];
825*4882a593Smuzhiyun };
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun /* put descriptor for string with that id into buf (buflen >= 256) */
828*4882a593Smuzhiyun int usb_gadget_get_string(const struct usb_gadget_strings *table, int id, u8 *buf);
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun /* check if the given language identifier is valid */
831*4882a593Smuzhiyun bool usb_validate_langid(u16 langid);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun /* utility to simplify managing config descriptors */
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /* write vector of descriptors into buffer */
838*4882a593Smuzhiyun int usb_descriptor_fillbuf(void *, unsigned,
839*4882a593Smuzhiyun const struct usb_descriptor_header **);
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun /* build config descriptor from single descriptor vector */
842*4882a593Smuzhiyun int usb_gadget_config_buf(const struct usb_config_descriptor *config,
843*4882a593Smuzhiyun void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun /* copy a NULL-terminated vector of descriptors */
846*4882a593Smuzhiyun struct usb_descriptor_header **usb_copy_descriptors(
847*4882a593Smuzhiyun struct usb_descriptor_header **);
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun /**
850*4882a593Smuzhiyun * usb_free_descriptors - free descriptors returned by usb_copy_descriptors()
851*4882a593Smuzhiyun * @v: vector of descriptors
852*4882a593Smuzhiyun */
usb_free_descriptors(struct usb_descriptor_header ** v)853*4882a593Smuzhiyun static inline void usb_free_descriptors(struct usb_descriptor_header **v)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun kfree(v);
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun struct usb_function;
859*4882a593Smuzhiyun int usb_assign_descriptors(struct usb_function *f,
860*4882a593Smuzhiyun struct usb_descriptor_header **fs,
861*4882a593Smuzhiyun struct usb_descriptor_header **hs,
862*4882a593Smuzhiyun struct usb_descriptor_header **ss,
863*4882a593Smuzhiyun struct usb_descriptor_header **ssp);
864*4882a593Smuzhiyun void usb_free_all_descriptors(struct usb_function *f);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun struct usb_descriptor_header *usb_otg_descriptor_alloc(
867*4882a593Smuzhiyun struct usb_gadget *gadget);
868*4882a593Smuzhiyun int usb_otg_descriptor_init(struct usb_gadget *gadget,
869*4882a593Smuzhiyun struct usb_descriptor_header *otg_desc);
870*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun /* utility to simplify map/unmap of usb_requests to/from DMA */
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun #ifdef CONFIG_HAS_DMA
875*4882a593Smuzhiyun extern int usb_gadget_map_request_by_dev(struct device *dev,
876*4882a593Smuzhiyun struct usb_request *req, int is_in);
877*4882a593Smuzhiyun extern int usb_gadget_map_request(struct usb_gadget *gadget,
878*4882a593Smuzhiyun struct usb_request *req, int is_in);
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun extern void usb_gadget_unmap_request_by_dev(struct device *dev,
881*4882a593Smuzhiyun struct usb_request *req, int is_in);
882*4882a593Smuzhiyun extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
883*4882a593Smuzhiyun struct usb_request *req, int is_in);
884*4882a593Smuzhiyun #else /* !CONFIG_HAS_DMA */
usb_gadget_map_request_by_dev(struct device * dev,struct usb_request * req,int is_in)885*4882a593Smuzhiyun static inline int usb_gadget_map_request_by_dev(struct device *dev,
886*4882a593Smuzhiyun struct usb_request *req, int is_in) { return -ENOSYS; }
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)887*4882a593Smuzhiyun static inline int usb_gadget_map_request(struct usb_gadget *gadget,
888*4882a593Smuzhiyun struct usb_request *req, int is_in) { return -ENOSYS; }
889*4882a593Smuzhiyun
usb_gadget_unmap_request_by_dev(struct device * dev,struct usb_request * req,int is_in)890*4882a593Smuzhiyun static inline void usb_gadget_unmap_request_by_dev(struct device *dev,
891*4882a593Smuzhiyun struct usb_request *req, int is_in) { }
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)892*4882a593Smuzhiyun static inline void usb_gadget_unmap_request(struct usb_gadget *gadget,
893*4882a593Smuzhiyun struct usb_request *req, int is_in) { }
894*4882a593Smuzhiyun #endif /* !CONFIG_HAS_DMA */
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun /* utility to set gadget state properly */
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun extern void usb_gadget_set_state(struct usb_gadget *gadget,
901*4882a593Smuzhiyun enum usb_device_state state);
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* utility to tell udc core that the bus reset occurs */
906*4882a593Smuzhiyun extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
907*4882a593Smuzhiyun struct usb_gadget_driver *driver);
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun /* utility to give requests back to the gadget layer */
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun extern void usb_gadget_giveback_request(struct usb_ep *ep,
914*4882a593Smuzhiyun struct usb_request *req);
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun /* utility to find endpoint by name */
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun extern struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g,
921*4882a593Smuzhiyun const char *name);
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* utility to check if endpoint caps match descriptor needs */
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun extern int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
928*4882a593Smuzhiyun struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
929*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *ep_comp);
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun /* utility to update vbus status for udc core, it may be scheduled */
934*4882a593Smuzhiyun extern void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status);
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun /* utility wrapping a simple endpoint selection policy */
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
941*4882a593Smuzhiyun struct usb_endpoint_descriptor *);
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun extern struct usb_ep *usb_ep_autoconfig_ss(struct usb_gadget *,
945*4882a593Smuzhiyun struct usb_endpoint_descriptor *,
946*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *);
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun extern void usb_ep_autoconfig_release(struct usb_ep *);
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun extern void usb_ep_autoconfig_reset(struct usb_gadget *);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun #endif /* __LINUX_USB_GADGET_H */
953