1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Filesystem based user-mode API to USB Gadget controller hardware 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Other than ep0 operations, most things are done by read() and write() 6*4882a593Smuzhiyun * on endpoint files found in one directory. They are configured by 7*4882a593Smuzhiyun * writing descriptors, and then may be used for normal stream style 8*4882a593Smuzhiyun * i/o requests. When ep0 is configured, the device can enumerate; 9*4882a593Smuzhiyun * when it's closed, the device disconnects from usb. Operations on 10*4882a593Smuzhiyun * ep0 require ioctl() operations. 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * Configuration and device descriptors get written to /dev/gadget/$CHIP, 13*4882a593Smuzhiyun * which may then be used to read usb_gadgetfs_event structs. The driver 14*4882a593Smuzhiyun * may activate endpoints as it handles SET_CONFIGURATION setup events, 15*4882a593Smuzhiyun * or earlier; writing endpoint descriptors to /dev/gadget/$ENDPOINT 16*4882a593Smuzhiyun * then performing data transfers by reading or writing. 17*4882a593Smuzhiyun */ 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun #ifndef __LINUX_USB_GADGETFS_H 20*4882a593Smuzhiyun #define __LINUX_USB_GADGETFS_H 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun #include <linux/types.h> 23*4882a593Smuzhiyun #include <linux/ioctl.h> 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun #include <linux/usb/ch9.h> 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun /* 28*4882a593Smuzhiyun * Events are delivered on the ep0 file descriptor, when the user mode driver 29*4882a593Smuzhiyun * reads from this file descriptor after writing the descriptors. Don't 30*4882a593Smuzhiyun * stop polling this descriptor. 31*4882a593Smuzhiyun */ 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun enum usb_gadgetfs_event_type { 34*4882a593Smuzhiyun GADGETFS_NOP = 0, 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun GADGETFS_CONNECT, 37*4882a593Smuzhiyun GADGETFS_DISCONNECT, 38*4882a593Smuzhiyun GADGETFS_SETUP, 39*4882a593Smuzhiyun GADGETFS_SUSPEND, 40*4882a593Smuzhiyun /* and likely more ! */ 41*4882a593Smuzhiyun }; 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* NOTE: this structure must stay the same size and layout on 44*4882a593Smuzhiyun * both 32-bit and 64-bit kernels. 45*4882a593Smuzhiyun */ 46*4882a593Smuzhiyun struct usb_gadgetfs_event { 47*4882a593Smuzhiyun union { 48*4882a593Smuzhiyun /* NOP, DISCONNECT, SUSPEND: nothing 49*4882a593Smuzhiyun * ... some hardware can't report disconnection 50*4882a593Smuzhiyun */ 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun /* CONNECT: just the speed */ 53*4882a593Smuzhiyun enum usb_device_speed speed; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun /* SETUP: packet; DATA phase i/o precedes next event 56*4882a593Smuzhiyun *(setup.bmRequestType & USB_DIR_IN) flags direction 57*4882a593Smuzhiyun * ... includes SET_CONFIGURATION, SET_INTERFACE 58*4882a593Smuzhiyun */ 59*4882a593Smuzhiyun struct usb_ctrlrequest setup; 60*4882a593Smuzhiyun } u; 61*4882a593Smuzhiyun enum usb_gadgetfs_event_type type; 62*4882a593Smuzhiyun }; 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun /* The 'g' code is also used by printer gadget ioctl requests. 66*4882a593Smuzhiyun * Don't add any colliding codes to either driver, and keep 67*4882a593Smuzhiyun * them in unique ranges (size 0x20 for now). 68*4882a593Smuzhiyun */ 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun /* endpoint ioctls */ 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun /* IN transfers may be reported to the gadget driver as complete 73*4882a593Smuzhiyun * when the fifo is loaded, before the host reads the data; 74*4882a593Smuzhiyun * OUT transfers may be reported to the host's "client" driver as 75*4882a593Smuzhiyun * complete when they're sitting in the FIFO unread. 76*4882a593Smuzhiyun * THIS returns how many bytes are "unclaimed" in the endpoint fifo 77*4882a593Smuzhiyun * (needed for precise fault handling, when the hardware allows it) 78*4882a593Smuzhiyun */ 79*4882a593Smuzhiyun #define GADGETFS_FIFO_STATUS _IO('g', 1) 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* discards any unclaimed data in the fifo. */ 82*4882a593Smuzhiyun #define GADGETFS_FIFO_FLUSH _IO('g', 2) 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun /* resets endpoint halt+toggle; used to implement set_interface. 85*4882a593Smuzhiyun * some hardware (like pxa2xx) can't support this. 86*4882a593Smuzhiyun */ 87*4882a593Smuzhiyun #define GADGETFS_CLEAR_HALT _IO('g', 3) 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun #endif /* __LINUX_USB_GADGETFS_H */ 90