xref: /OK3568_Linux_fs/kernel/include/uapi/linux/usb/raw_gadget.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * USB Raw Gadget driver.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * See Documentation/usb/raw-gadget.rst for more details.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef _UAPI__LINUX_USB_RAW_GADGET_H
9*4882a593Smuzhiyun #define _UAPI__LINUX_USB_RAW_GADGET_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <asm/ioctl.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/usb/ch9.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /* Maximum length of driver_name/device_name in the usb_raw_init struct. */
16*4882a593Smuzhiyun #define UDC_NAME_LENGTH_MAX 128
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * struct usb_raw_init - argument for USB_RAW_IOCTL_INIT ioctl.
20*4882a593Smuzhiyun  * @speed: The speed of the emulated USB device, takes the same values as
21*4882a593Smuzhiyun  *     the usb_device_speed enum: USB_SPEED_FULL, USB_SPEED_HIGH, etc.
22*4882a593Smuzhiyun  * @driver_name: The name of the UDC driver.
23*4882a593Smuzhiyun  * @device_name: The name of a UDC instance.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * The last two fields identify a UDC the gadget driver should bind to.
26*4882a593Smuzhiyun  * For example, Dummy UDC has "dummy_udc" as its driver_name and "dummy_udc.N"
27*4882a593Smuzhiyun  * as its device_name, where N in the index of the Dummy UDC instance.
28*4882a593Smuzhiyun  * At the same time the dwc2 driver that is used on Raspberry Pi Zero, has
29*4882a593Smuzhiyun  * "20980000.usb" as both driver_name and device_name.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun struct usb_raw_init {
32*4882a593Smuzhiyun 	__u8	driver_name[UDC_NAME_LENGTH_MAX];
33*4882a593Smuzhiyun 	__u8	device_name[UDC_NAME_LENGTH_MAX];
34*4882a593Smuzhiyun 	__u8	speed;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* The type of event fetched with the USB_RAW_IOCTL_EVENT_FETCH ioctl. */
38*4882a593Smuzhiyun enum usb_raw_event_type {
39*4882a593Smuzhiyun 	USB_RAW_EVENT_INVALID = 0,
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	/* This event is queued when the driver has bound to a UDC. */
42*4882a593Smuzhiyun 	USB_RAW_EVENT_CONNECT = 1,
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* This event is queued when a new control request arrived to ep0. */
45*4882a593Smuzhiyun 	USB_RAW_EVENT_CONTROL = 2,
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/* The list might grow in the future. */
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * struct usb_raw_event - argument for USB_RAW_IOCTL_EVENT_FETCH ioctl.
52*4882a593Smuzhiyun  * @type: The type of the fetched event.
53*4882a593Smuzhiyun  * @length: Length of the data buffer. Updated by the driver and set to the
54*4882a593Smuzhiyun  *     actual length of the fetched event data.
55*4882a593Smuzhiyun  * @data: A buffer to store the fetched event data.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * Currently the fetched data buffer is empty for USB_RAW_EVENT_CONNECT,
58*4882a593Smuzhiyun  * and contains struct usb_ctrlrequest for USB_RAW_EVENT_CONTROL.
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun struct usb_raw_event {
61*4882a593Smuzhiyun 	__u32		type;
62*4882a593Smuzhiyun 	__u32		length;
63*4882a593Smuzhiyun 	__u8		data[0];
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #define USB_RAW_IO_FLAGS_ZERO	0x0001
67*4882a593Smuzhiyun #define USB_RAW_IO_FLAGS_MASK	0x0001
68*4882a593Smuzhiyun 
usb_raw_io_flags_valid(__u16 flags)69*4882a593Smuzhiyun static inline int usb_raw_io_flags_valid(__u16 flags)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	return (flags & ~USB_RAW_IO_FLAGS_MASK) == 0;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
usb_raw_io_flags_zero(__u16 flags)74*4882a593Smuzhiyun static inline int usb_raw_io_flags_zero(__u16 flags)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	return (flags & USB_RAW_IO_FLAGS_ZERO);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * struct usb_raw_ep_io - argument for USB_RAW_IOCTL_EP0/EP_WRITE/READ ioctls.
81*4882a593Smuzhiyun  * @ep: Endpoint handle as returned by USB_RAW_IOCTL_EP_ENABLE for
82*4882a593Smuzhiyun  *     USB_RAW_IOCTL_EP_WRITE/READ. Ignored for USB_RAW_IOCTL_EP0_WRITE/READ.
83*4882a593Smuzhiyun  * @flags: When USB_RAW_IO_FLAGS_ZERO is specified, the zero flag is set on
84*4882a593Smuzhiyun  *     the submitted USB request, see include/linux/usb/gadget.h for details.
85*4882a593Smuzhiyun  * @length: Length of data.
86*4882a593Smuzhiyun  * @data: Data to send for USB_RAW_IOCTL_EP0/EP_WRITE. Buffer to store received
87*4882a593Smuzhiyun  *     data for USB_RAW_IOCTL_EP0/EP_READ.
88*4882a593Smuzhiyun  */
89*4882a593Smuzhiyun struct usb_raw_ep_io {
90*4882a593Smuzhiyun 	__u16		ep;
91*4882a593Smuzhiyun 	__u16		flags;
92*4882a593Smuzhiyun 	__u32		length;
93*4882a593Smuzhiyun 	__u8		data[0];
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /* Maximum number of non-control endpoints in struct usb_raw_eps_info. */
97*4882a593Smuzhiyun #define USB_RAW_EPS_NUM_MAX	30
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* Maximum length of UDC endpoint name in struct usb_raw_ep_info. */
100*4882a593Smuzhiyun #define USB_RAW_EP_NAME_MAX	16
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /* Used as addr in struct usb_raw_ep_info if endpoint accepts any address. */
103*4882a593Smuzhiyun #define USB_RAW_EP_ADDR_ANY	0xff
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * struct usb_raw_ep_caps - exposes endpoint capabilities from struct usb_ep
107*4882a593Smuzhiyun  *     (technically from its member struct usb_ep_caps).
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun struct usb_raw_ep_caps {
110*4882a593Smuzhiyun 	__u32	type_control	: 1;
111*4882a593Smuzhiyun 	__u32	type_iso	: 1;
112*4882a593Smuzhiyun 	__u32	type_bulk	: 1;
113*4882a593Smuzhiyun 	__u32	type_int	: 1;
114*4882a593Smuzhiyun 	__u32	dir_in		: 1;
115*4882a593Smuzhiyun 	__u32	dir_out		: 1;
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun  * struct usb_raw_ep_limits - exposes endpoint limits from struct usb_ep.
120*4882a593Smuzhiyun  * @maxpacket_limit: Maximum packet size value supported by this endpoint.
121*4882a593Smuzhiyun  * @max_streams: maximum number of streams supported by this endpoint
122*4882a593Smuzhiyun  *     (actual number is 2^n).
123*4882a593Smuzhiyun  * @reserved: Empty, reserved for potential future extensions.
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun struct usb_raw_ep_limits {
126*4882a593Smuzhiyun 	__u16	maxpacket_limit;
127*4882a593Smuzhiyun 	__u16	max_streams;
128*4882a593Smuzhiyun 	__u32	reserved;
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun  * struct usb_raw_ep_info - stores information about a gadget endpoint.
133*4882a593Smuzhiyun  * @name: Name of the endpoint as it is defined in the UDC driver.
134*4882a593Smuzhiyun  * @addr: Address of the endpoint that must be specified in the endpoint
135*4882a593Smuzhiyun  *     descriptor passed to USB_RAW_IOCTL_EP_ENABLE ioctl.
136*4882a593Smuzhiyun  * @caps: Endpoint capabilities.
137*4882a593Smuzhiyun  * @limits: Endpoint limits.
138*4882a593Smuzhiyun  */
139*4882a593Smuzhiyun struct usb_raw_ep_info {
140*4882a593Smuzhiyun 	__u8				name[USB_RAW_EP_NAME_MAX];
141*4882a593Smuzhiyun 	__u32				addr;
142*4882a593Smuzhiyun 	struct usb_raw_ep_caps		caps;
143*4882a593Smuzhiyun 	struct usb_raw_ep_limits	limits;
144*4882a593Smuzhiyun };
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun  * struct usb_raw_eps_info - argument for USB_RAW_IOCTL_EPS_INFO ioctl.
148*4882a593Smuzhiyun  * eps: Structures that store information about non-control endpoints.
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun struct usb_raw_eps_info {
151*4882a593Smuzhiyun 	struct usb_raw_ep_info	eps[USB_RAW_EPS_NUM_MAX];
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun  * Initializes a Raw Gadget instance.
156*4882a593Smuzhiyun  * Accepts a pointer to the usb_raw_init struct as an argument.
157*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
158*4882a593Smuzhiyun  */
159*4882a593Smuzhiyun #define USB_RAW_IOCTL_INIT		_IOW('U', 0, struct usb_raw_init)
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun  * Instructs Raw Gadget to bind to a UDC and start emulating a USB device.
163*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
164*4882a593Smuzhiyun  */
165*4882a593Smuzhiyun #define USB_RAW_IOCTL_RUN		_IO('U', 1)
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun  * A blocking ioctl that waits for an event and returns fetched event data to
169*4882a593Smuzhiyun  * the user.
170*4882a593Smuzhiyun  * Accepts a pointer to the usb_raw_event struct.
171*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
172*4882a593Smuzhiyun  */
173*4882a593Smuzhiyun #define USB_RAW_IOCTL_EVENT_FETCH	_IOR('U', 2, struct usb_raw_event)
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun  * Queues an IN (OUT for READ) request as a response to the last setup request
177*4882a593Smuzhiyun  * received on endpoint 0 (provided that was an IN (OUT for READ) request), and
178*4882a593Smuzhiyun  * waits until the request is completed. Copies received data to user for READ.
179*4882a593Smuzhiyun  * Accepts a pointer to the usb_raw_ep_io struct as an argument.
180*4882a593Smuzhiyun  * Returns length of transferred data on success or negative error code on
181*4882a593Smuzhiyun  * failure.
182*4882a593Smuzhiyun  */
183*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP0_WRITE		_IOW('U', 3, struct usb_raw_ep_io)
184*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP0_READ		_IOWR('U', 4, struct usb_raw_ep_io)
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun  * Finds an endpoint that satisfies the parameters specified in the provided
188*4882a593Smuzhiyun  * descriptors (address, transfer type, etc.) and enables it.
189*4882a593Smuzhiyun  * Accepts a pointer to the usb_raw_ep_descs struct as an argument.
190*4882a593Smuzhiyun  * Returns enabled endpoint handle on success or negative error code on failure.
191*4882a593Smuzhiyun  */
192*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_ENABLE		_IOW('U', 5, struct usb_endpoint_descriptor)
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun  * Disables specified endpoint.
196*4882a593Smuzhiyun  * Accepts endpoint handle as an argument.
197*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
198*4882a593Smuzhiyun  */
199*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_DISABLE	_IOW('U', 6, __u32)
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun  * Queues an IN (OUT for READ) request as a response to the last setup request
203*4882a593Smuzhiyun  * received on endpoint usb_raw_ep_io.ep (provided that was an IN (OUT for READ)
204*4882a593Smuzhiyun  * request), and waits until the request is completed. Copies received data to
205*4882a593Smuzhiyun  * user for READ.
206*4882a593Smuzhiyun  * Accepts a pointer to the usb_raw_ep_io struct as an argument.
207*4882a593Smuzhiyun  * Returns length of transferred data on success or negative error code on
208*4882a593Smuzhiyun  * failure.
209*4882a593Smuzhiyun  */
210*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_WRITE		_IOW('U', 7, struct usb_raw_ep_io)
211*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_READ		_IOWR('U', 8, struct usb_raw_ep_io)
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun  * Switches the gadget into the configured state.
215*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
216*4882a593Smuzhiyun  */
217*4882a593Smuzhiyun #define USB_RAW_IOCTL_CONFIGURE		_IO('U', 9)
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun  * Constrains UDC VBUS power usage.
221*4882a593Smuzhiyun  * Accepts current limit in 2 mA units as an argument.
222*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun #define USB_RAW_IOCTL_VBUS_DRAW		_IOW('U', 10, __u32)
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Fills in the usb_raw_eps_info structure with information about non-control
228*4882a593Smuzhiyun  * endpoints available for the currently connected UDC.
229*4882a593Smuzhiyun  * Returns the number of available endpoints on success or negative error code
230*4882a593Smuzhiyun  * on failure.
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun #define USB_RAW_IOCTL_EPS_INFO		_IOR('U', 11, struct usb_raw_eps_info)
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * Stalls a pending control request on endpoint 0.
236*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
237*4882a593Smuzhiyun  */
238*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP0_STALL		_IO('U', 12)
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /*
241*4882a593Smuzhiyun  * Sets or clears halt or wedge status of the endpoint.
242*4882a593Smuzhiyun  * Accepts endpoint handle as an argument.
243*4882a593Smuzhiyun  * Returns 0 on success or negative error code on failure.
244*4882a593Smuzhiyun  */
245*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_SET_HALT	_IOW('U', 13, __u32)
246*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_CLEAR_HALT	_IOW('U', 14, __u32)
247*4882a593Smuzhiyun #define USB_RAW_IOCTL_EP_SET_WEDGE	_IOW('U', 15, __u32)
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun #endif /* _UAPI__LINUX_USB_RAW_GADGET_H */
250