130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/*
230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Filesystem based user-mode API to USB Gadget controller hardware
330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Other than ep0 operations, most things are done by read() and write()
530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * on endpoint files found in one directory.  They are configured by
630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * writing descriptors, and then may be used for normal stream style
730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * i/o requests.  When ep0 is configured, the device can enumerate;
830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * when it's closed, the device disconnects from usb.  Operations on
930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * ep0 require ioctl() operations.
1030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
1130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Configuration and device descriptors get written to /dev/gadget/$CHIP,
1230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * which may then be used to read usb_gadgetfs_event structs.  The driver
1330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * may activate endpoints as it handles SET_CONFIGURATION setup events,
1430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * or earlier; writing endpoint descriptors to /dev/gadget/$ENDPOINT
1530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * then performing data transfers by reading or writing.
1630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
1730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
1830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#ifndef __LINUX_USB_GADGETFS_H
1930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define __LINUX_USB_GADGETFS_H
2030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/types.h>
2230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/ioctl.h>
2330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/usb/ch9.h>
2530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/*
2730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Events are delivered on the ep0 file descriptor, when the user mode driver
2830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * reads from this file descriptor after writing the descriptors.  Don't
2930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * stop polling this descriptor.
3030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
3130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
3230692c65c4174412c90e79489e98ab85c1a7412fBen Chengenum usb_gadgetfs_event_type {
3330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	GADGETFS_NOP = 0,
3430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
3530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	GADGETFS_CONNECT,
3630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	GADGETFS_DISCONNECT,
3730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	GADGETFS_SETUP,
3830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	GADGETFS_SUSPEND,
3930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/* and likely more ! */
4030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
4130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
4230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* NOTE:  this structure must stay the same size and layout on
4330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * both 32-bit and 64-bit kernels.
4430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
4530692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct usb_gadgetfs_event {
4630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	union {
4730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		/* NOP, DISCONNECT, SUSPEND: nothing
4830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		 * ... some hardware can't report disconnection
4930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		 */
5030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
5130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		/* CONNECT: just the speed */
5230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		enum usb_device_speed	speed;
5330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
5430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		/* SETUP: packet; DATA phase i/o precedes next event
5530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		 *(setup.bmRequestType & USB_DIR_IN) flags direction
5630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		 * ... includes SET_CONFIGURATION, SET_INTERFACE
5730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		 */
5830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng		struct usb_ctrlrequest	setup;
5930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	} u;
6030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	enum usb_gadgetfs_event_type	type;
6130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
6230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* The 'g' code is also used by printer gadget ioctl requests.
6530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Don't add any colliding codes to either driver, and keep
6630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * them in unique ranges (size 0x20 for now).
6730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
6830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* endpoint ioctls */
7030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
7130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* IN transfers may be reported to the gadget driver as complete
7230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *	when the fifo is loaded, before the host reads the data;
7330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * OUT transfers may be reported to the host's "client" driver as
7430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *	complete when they're sitting in the FIFO unread.
7530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * THIS returns how many bytes are "unclaimed" in the endpoint fifo
7630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * (needed for precise fault handling, when the hardware allows it)
7730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
7830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define	GADGETFS_FIFO_STATUS	_IO('g', 1)
7930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* discards any unclaimed data in the fifo. */
8130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define	GADGETFS_FIFO_FLUSH	_IO('g', 2)
8230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* resets endpoint halt+toggle; used to implement set_interface.
8430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * some hardware (like pxa2xx) can't support this.
8530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
8630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define	GADGETFS_CLEAR_HALT	_IO('g', 3)
8730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#endif /* __LINUX_USB_GADGETFS_H */
89