^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * inode.c -- user mode filesystem api for usb gadget controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2003-2004 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2003 Agilent Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* #define VERBOSE_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/aio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/usb/gadgetfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * The gadgetfs API maps each endpoint to a file descriptor so that you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * can use standard synchronous read/write calls for I/O. There's some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * drivers show how this works in practice. You can also use AIO to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * eliminate I/O gaps between requests, to help when streaming data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Key parts that must be USB-specific are protocols defining how the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * read/write operations relate to the hardware state machines. There
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * are two types of files. One type is for the device, implementing ep0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * The other type is for each IN or OUT endpoint. In both cases, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * user mode driver must configure the hardware before using it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * - First, dev_config() is called when /dev/gadget/$CHIP is configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * (by writing configuration and device descriptors). Afterwards it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * may serve as a source of device events, used to handle all control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * requests other than basic enumeration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * - Then, after a SET_CONFIGURATION control request, ep_config() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * called when each /dev/gadget/ep* file is configured (by writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * endpoint descriptors). Afterwards these files are used to write()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * IN data or to read() OUT data. To halt the endpoint, a "wrong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * direction" request is issued (like reading an IN endpoint).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * not possible on all hardware. For example, precise fault handling with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * respect to data left in endpoint fifos after aborted operations; or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * selective clearing of endpoint halts, to implement SET_INTERFACE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define DRIVER_DESC "USB Gadget filesystem"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define DRIVER_VERSION "24 Aug 2004"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static const char driver_desc [] = DRIVER_DESC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static const char shortname [] = "gadgetfs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) MODULE_DESCRIPTION (DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) MODULE_AUTHOR ("David Brownell");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) MODULE_LICENSE ("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int ep_open(struct inode *, struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define GADGETFS_MAGIC 0xaee71ee7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* /dev/gadget/$CHIP represents ep0 and the whole device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) enum ep0_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* DISABLED is the initial state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) STATE_DEV_DISABLED = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* Only one open() of /dev/gadget/$CHIP; only one file tracks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * ep0/device i/o modes and binding to the controller. Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * must always write descriptors to initialize the device, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * the device becomes UNCONNECTED until enumeration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) STATE_DEV_OPENED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* From then on, ep0 fd is in either of two basic modes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * - SETUP: read/write will transfer control data and succeed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * or if "wrong direction", performs protocol stall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) STATE_DEV_UNCONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) STATE_DEV_CONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) STATE_DEV_SETUP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* UNBOUND means the driver closed ep0, so the device won't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * accessible again (DEV_DISABLED) until all fds are closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) STATE_DEV_UNBOUND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* enough for the whole queue: most events invalidate others */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define N_EVENT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define RBUF_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct dev_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) refcount_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) enum ep0_state state; /* P: lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct usb_gadgetfs_event event [N_EVENT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned ev_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct fasync_struct *fasync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u8 current_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* drivers reading ep0 MUST handle control requests (SETUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * reported that way; else the host will time out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unsigned usermode_setup : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) setup_in : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) setup_can_stall : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) setup_out_ready : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) setup_out_error : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) setup_abort : 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) gadget_registered : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned setup_wLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* the rest is basically write-once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct usb_config_descriptor *config, *hs_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct usb_device_descriptor *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct usb_gadget *gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct list_head epfiles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* except this scratch i/o buffer for ep0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u8 rbuf[RBUF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static inline void get_dev (struct dev_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) refcount_inc (&data->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void put_dev (struct dev_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (likely (!refcount_dec_and_test (&data->count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* needs no more cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) BUG_ON (waitqueue_active (&data->wait));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kfree (data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct dev_data *dev_new (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct dev_data *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dev->state = STATE_DEV_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) refcount_set (&dev->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) spin_lock_init (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) INIT_LIST_HEAD (&dev->epfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) init_waitqueue_head (&dev->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* other /dev/gadget/$ENDPOINT files represent endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) enum ep_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) STATE_EP_DISABLED = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) STATE_EP_READY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) STATE_EP_ENABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) STATE_EP_UNBOUND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct ep_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) enum ep_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) refcount_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct dev_data *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* must hold dev->lock before accessing ep or req */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct usb_ep *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) char name [16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct usb_endpoint_descriptor desc, hs_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct list_head epfiles;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static inline void get_ep (struct ep_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) refcount_inc (&data->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void put_ep (struct ep_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (likely (!refcount_dec_and_test (&data->count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) put_dev (data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* needs no more cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) BUG_ON (!list_empty (&data->epfiles));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) BUG_ON (waitqueue_active (&data->wait));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kfree (data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* most "how to use the hardware" policy choices are in userspace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * mapping endpoint roles (which the driver needs) to the capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * which the usb controller has. most of those capabilities are exposed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * implicitly, starting with the driver name and then endpoint names.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static const char *CHIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* NOTE: don't use dev_printk calls before binding to the gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * at the end of ep0 configuration, or after unbind.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) #define xprintk(d,level,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) printk(level "%s: " fmt , shortname , ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) #define DBG(dev,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) xprintk(dev , KERN_DEBUG , fmt , ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #define DBG(dev,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #endif /* DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #ifdef VERBOSE_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #define VDEBUG DBG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define VDEBUG(dev,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #endif /* DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #define ERROR(dev,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) xprintk(dev , KERN_ERR , fmt , ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define INFO(dev,fmt,args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) xprintk(dev , KERN_INFO , fmt , ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * After opening, configure non-control endpoints. Then use normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * stream read() and write() requests; and maybe ioctl() to get more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * precise FIFO status when recovering from cancellation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void epio_complete (struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct ep_data *epdata = ep->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (!req->context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (req->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) epdata->status = req->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) epdata->status = req->actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) complete ((struct completion *)req->context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* tasklock endpoint, returning when it's connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * still need dev->lock to use epdata->ep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!mutex_trylock(&epdata->lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto nonblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (epdata->state != STATE_EP_ENABLED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) (!is_write || epdata->state != STATE_EP_READY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) nonblock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) val = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) val = mutex_lock_interruptible(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) switch (epdata->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case STATE_EP_ENABLED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) case STATE_EP_READY: /* not configured yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (is_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case STATE_EP_UNBOUND: /* clean disconnect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) // case STATE_EP_DISABLED: /* "can't happen" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) default: /* error! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) pr_debug ("%s: ep %p not available, state %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) shortname, epdata, epdata->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ep_io (struct ep_data *epdata, void *buf, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) DECLARE_COMPLETION_ONSTACK (done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) spin_lock_irq (&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (likely (epdata->ep != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct usb_request *req = epdata->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) req->context = &done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) req->complete = epio_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) req->buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) req->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) value = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) spin_unlock_irq (&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (likely (value == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) value = wait_for_completion_interruptible(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (value != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) spin_lock_irq (&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (likely (epdata->ep != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) DBG (epdata->dev, "%s i/o interrupted\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) epdata->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) usb_ep_dequeue (epdata->ep, epdata->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) spin_unlock_irq (&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) wait_for_completion(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (epdata->status == -ECONNRESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) epdata->status = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) spin_unlock_irq (&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) DBG (epdata->dev, "endpoint gone\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) epdata->status = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return epdata->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ep_release (struct inode *inode, struct file *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct ep_data *data = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) value = mutex_lock_interruptible(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /* clean up if this can be reopened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (data->state != STATE_EP_UNBOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) data->state = STATE_EP_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) data->desc.bDescriptorType = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) data->hs_desc.bDescriptorType = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) usb_ep_disable(data->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) put_ep (data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct ep_data *data = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) spin_lock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (likely (data->ep != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) case GADGETFS_FIFO_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) status = usb_ep_fifo_status (data->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) case GADGETFS_FIFO_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) usb_ep_fifo_flush (data->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) case GADGETFS_CLEAR_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) status = usb_ep_clear_halt (data->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) status = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) status = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) spin_unlock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct kiocb_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) struct ep_data *epdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct kiocb *iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct mm_struct *mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct iov_iter to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) const void *to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) unsigned actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static int ep_aio_cancel(struct kiocb *iocb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct kiocb_priv *priv = iocb->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct ep_data *epdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) epdata = priv->epdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) // spin_lock(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (likely(epdata && epdata->ep && priv->req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) value = usb_ep_dequeue (epdata->ep, priv->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) value = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) // spin_unlock(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static void ep_user_copy_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct mm_struct *mm = priv->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct kiocb *iocb = priv->iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) size_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) kthread_use_mm(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) kthread_unuse_mm(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* completing the iocb can drop the ctx and mm, don't touch mm after */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) iocb->ki_complete(iocb, ret, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) kfree(priv->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kfree(priv->to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct kiocb *iocb = req->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) struct kiocb_priv *priv = iocb->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct ep_data *epdata = priv->epdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* lock against disconnect (and ideally, cancel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) spin_lock(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) priv->req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) priv->epdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /* if this was a write or a read returning no data then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * don't need to copy anything to userspace, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * complete the aio request immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (priv->to_free == NULL || unlikely(req->actual == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) kfree(req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) kfree(priv->to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) iocb->private = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /* aio_complete() reports bytes-transferred _and_ faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) iocb->ki_complete(iocb, req->actual ? req->actual : req->status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) req->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /* ep_copy_to_user() won't report both; we hide some faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (unlikely(0 != req->status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) DBG(epdata->dev, "%s fault %d len %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) ep->name, req->status, req->actual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) priv->buf = req->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) priv->actual = req->actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) INIT_WORK(&priv->work, ep_user_copy_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) schedule_work(&priv->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) usb_ep_free_request(ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) spin_unlock(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) put_ep(epdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static ssize_t ep_aio(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct kiocb_priv *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct ep_data *epdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ssize_t value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) iocb->private = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) priv->iocb = iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) kiocb_set_cancel_fn(iocb, ep_aio_cancel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) get_ep(epdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) priv->epdata = epdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) priv->actual = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* each kiocb is coupled to one usb_request, but we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * allocate or submit those if the host disconnected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) spin_lock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) value = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (unlikely(epdata->ep == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) value = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (unlikely(!req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) priv->req = req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) req->buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) req->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) req->complete = ep_aio_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) req->context = iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (unlikely(0 != value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) usb_ep_free_request(epdata->ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) spin_unlock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return -EIOCBQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) spin_unlock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) kfree(priv->to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) put_ep(epdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct ep_data *epdata = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) size_t len = iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) ssize_t value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* halt any endpoint by doing a "wrong direction" i/o call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (usb_endpoint_dir_in(&epdata->desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (usb_endpoint_xfer_isoc(&epdata->desc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) !is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) DBG (epdata->dev, "%s halt\n", epdata->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) spin_lock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) if (likely(epdata->ep != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) usb_ep_set_halt(epdata->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) spin_unlock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) buf = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (unlikely(!buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) value = ep_io(epdata, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (value >= 0 && (copy_to_iter(buf, value, to) != value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) value = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) value = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (!priv->to_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) value = ep_aio(iocb, priv, epdata, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (value == -EIOCBQUEUED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static ssize_t ep_config(struct ep_data *, const char *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct ep_data *epdata = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) size_t len = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) bool configured;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) ssize_t value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) configured = epdata->state == STATE_EP_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /* halt any endpoint by doing a "wrong direction" i/o call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (usb_endpoint_xfer_isoc(&epdata->desc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) !is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) DBG (epdata->dev, "%s halt\n", epdata->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) spin_lock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (likely(epdata->ep != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) usb_ep_set_halt(epdata->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) spin_unlock_irq(&epdata->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) buf = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (unlikely(!buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (unlikely(!copy_from_iter_full(buf, len, from))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) value = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (unlikely(!configured)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) value = ep_config(epdata, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) } else if (is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) value = ep_io(epdata, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) value = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) value = ep_aio(iocb, priv, epdata, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (value == -EIOCBQUEUED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) mutex_unlock(&epdata->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /* used after endpoint configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static const struct file_operations ep_io_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) .open = ep_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) .release = ep_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) .unlocked_ioctl = ep_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) .read_iter = ep_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) .write_iter = ep_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* ENDPOINT INITIALIZATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * status = write (fd, descriptors, sizeof descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * That write establishes the endpoint configuration, configuring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * the controller to process bulk, interrupt, or isochronous transfers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * at the right maxpacket size, and so on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * The descriptors are message type 1, identified by a host order u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * at the beginning of what's written. Descriptor order is: full/low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * speed descriptor, then optional high speed descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ep_config (struct ep_data *data, const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) struct usb_ep *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) u32 tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int value, length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (data->state != STATE_EP_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) value = -EL2HLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) value = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (len < USB_DT_ENDPOINT_SIZE + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) goto fail0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /* we might need to change message format someday */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) memcpy(&tag, buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (tag != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) goto fail0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) buf += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) len -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) /* NOTE: audio endpoint extensions not accepted here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * just don't include the extra bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /* full/low speed descriptor, then high speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) || data->desc.bDescriptorType != USB_DT_ENDPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) goto fail0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (len != USB_DT_ENDPOINT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (len != 2 * USB_DT_ENDPOINT_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) goto fail0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) USB_DT_ENDPOINT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) || data->hs_desc.bDescriptorType
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) != USB_DT_ENDPOINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) DBG(data->dev, "config %s, bad hs length or type\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) goto fail0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) spin_lock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (data->dev->state == STATE_DEV_UNBOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) value = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) goto gone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) ep = data->ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (ep == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) value = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) goto gone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) switch (data->dev->gadget->speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) case USB_SPEED_LOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) case USB_SPEED_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ep->desc = &data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) case USB_SPEED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) /* fails if caller didn't provide that descriptor... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ep->desc = &data->hs_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) DBG(data->dev, "unconnected, %s init abandoned\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) value = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto gone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) value = usb_ep_enable(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (value == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) data->state = STATE_EP_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) value = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) gone:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) spin_unlock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) data->desc.bDescriptorType = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) data->hs_desc.bDescriptorType = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) fail0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) value = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) ep_open (struct inode *inode, struct file *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct ep_data *data = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) int value = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (mutex_lock_interruptible(&data->lock) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) spin_lock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (data->dev->state == STATE_DEV_UNBOUND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) value = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) else if (data->state == STATE_EP_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) data->state = STATE_EP_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) get_ep (data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) fd->private_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) VDEBUG (data->dev, "%s ready\n", data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) DBG (data->dev, "%s state %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) data->name, data->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) spin_unlock_irq (&data->dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) /* EP0 IMPLEMENTATION can be partly in userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) * Drivers that use this facility receive various events, including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * control requests the kernel doesn't handle. Drivers that don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) * use this facility may be too simple-minded for real applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) static inline void ep0_readable (struct dev_data *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) wake_up (&dev->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) kill_fasync (&dev->fasync, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) static void clean_req (struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct dev_data *dev = ep->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (req->buf != dev->rbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) kfree(req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) req->buf = dev->rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) req->complete = epio_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) dev->setup_out_ready = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) struct dev_data *dev = ep->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) int free = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) /* for control OUT, data must still get to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) spin_lock_irqsave(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!dev->setup_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) dev->setup_out_error = (req->status != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (!dev->setup_out_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) free = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) dev->setup_out_ready = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) ep0_readable (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /* clean up as appropriate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (free && req->buf != &dev->rbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) clean_req (ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) req->complete = epio_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) spin_unlock_irqrestore(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) struct dev_data *dev = ep->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (dev->setup_out_ready) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) DBG (dev, "ep0 request busy!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) if (len > sizeof (dev->rbuf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) req->buf = kmalloc(len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (req->buf == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) req->buf = dev->rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) req->complete = ep0_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) req->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) req->zero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) enum ep0_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (dev->state <= STATE_DEV_OPENED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) /* report fd mode change before acting on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (dev->setup_abort) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) dev->setup_abort = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) retval = -EIDRM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) /* control DATA stage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if ((state = dev->state) == STATE_DEV_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (dev->setup_in) { /* stall IN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) VDEBUG(dev, "ep0in stall\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) (void) usb_ep_set_halt (dev->gadget->ep0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) retval = -EL2HLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) } else if (len == 0) { /* ack SET_CONFIGURATION etc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct usb_ep *ep = dev->gadget->ep0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) struct usb_request *req = dev->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if ((retval = setup_req (ep, req, 0)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) retval = usb_ep_queue (ep, req, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) /* assume that was SET_CONFIGURATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) if (dev->current_config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) unsigned power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (gadget_is_dualspeed(dev->gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) && (dev->gadget->speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) == USB_SPEED_HIGH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) power = dev->hs_config->bMaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) power = dev->config->bMaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) usb_gadget_vbus_draw(dev->gadget, 2 * power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) } else { /* collect OUT data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) if ((fd->f_flags & O_NONBLOCK) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) && !dev->setup_out_ready) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) retval = wait_event_interruptible (dev->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) dev->setup_out_ready != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /* FIXME state could change from under us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (dev->state != STATE_DEV_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) retval = -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (dev->setup_out_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) len = min (len, (size_t)dev->req->actual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (copy_to_user (buf, dev->req->buf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) retval = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) clean_req (dev->gadget->ep0, dev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) /* NOTE userspace can't yet choose to stall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) /* else normal: return event data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (len < sizeof dev->event [0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) len -= len % sizeof (struct usb_gadgetfs_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) dev->usermode_setup = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) scan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) /* return queued events right away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (dev->ev_next != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) unsigned i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) n = len / sizeof (struct usb_gadgetfs_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (dev->ev_next < n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) n = dev->ev_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /* ep0 i/o has special semantics during STATE_DEV_SETUP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) for (i = 0; i < n; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (dev->event [i].type == GADGETFS_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) dev->state = STATE_DEV_SETUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) n = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) len = n * sizeof (struct usb_gadgetfs_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (copy_to_user (buf, &dev->event, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) retval = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) /* NOTE this doesn't guard against broken drivers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) * concurrent ep0 readers may lose events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (dev->ev_next > n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) memmove(&dev->event[0], &dev->event[n],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) sizeof (struct usb_gadgetfs_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) * (dev->ev_next - n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) dev->ev_next -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) if (fd->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) DBG (dev, "fail %s, state %d\n", __func__, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) retval = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) case STATE_DEV_UNCONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) case STATE_DEV_CONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) DBG (dev, "%s wait\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* wait for events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) retval = wait_event_interruptible (dev->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) dev->ev_next != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) goto scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static struct usb_gadgetfs_event *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) struct usb_gadgetfs_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /* these events purge the queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) case GADGETFS_DISCONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (dev->state == STATE_DEV_SETUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) dev->setup_abort = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) case GADGETFS_CONNECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) dev->ev_next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) case GADGETFS_SETUP: /* previous request timed out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) case GADGETFS_SUSPEND: /* same effect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /* these events can't be repeated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) for (i = 0; i != dev->ev_next; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (dev->event [i].type != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) DBG(dev, "discard old event[%d] %d\n", i, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) dev->ev_next--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (i == dev->ev_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /* indices start at zero, for simplicity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) memmove (&dev->event [i], &dev->event [i + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) sizeof (struct usb_gadgetfs_event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) * (dev->ev_next - i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) BUG ();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) event = &dev->event [dev->ev_next++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) BUG_ON (dev->ev_next > N_EVENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) memset (event, 0, sizeof *event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) event->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) ssize_t retval = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) /* report fd mode change before acting on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (dev->setup_abort) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) dev->setup_abort = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) retval = -EIDRM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /* data and/or status stage for control request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) } else if (dev->state == STATE_DEV_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) len = min_t(size_t, len, dev->setup_wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (dev->setup_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) retval = setup_req (dev->gadget->ep0, dev->req, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (retval == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (copy_from_user (dev->req->buf, buf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) if (len < dev->setup_wLength)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) dev->req->zero = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) retval = usb_ep_queue (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) dev->gadget->ep0, dev->req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) clean_req (dev->gadget->ep0, dev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) retval = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /* can stall some OUT transfers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) } else if (dev->setup_can_stall) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) VDEBUG(dev, "ep0out stall\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) (void) usb_ep_set_halt (dev->gadget->ep0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) retval = -EL2HLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) DBG(dev, "bogus ep0out stall!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) DBG (dev, "fail %s, state %d\n", __func__, dev->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) ep0_fasync (int f, struct file *fd, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) // caller must F_SETOWN before signal delivery happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) return fasync_helper (f, fd, on, &dev->fasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) static struct usb_gadget_driver gadgetfs_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) dev_release (struct inode *inode, struct file *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) /* closing ep0 === shutdown all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) if (dev->gadget_registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) usb_gadget_unregister_driver (&gadgetfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) dev->gadget_registered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) /* at this point "good" hardware has disconnected the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) * device from USB; the host won't see it any more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * alternatively, all host requests will time out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) kfree (dev->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) dev->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) /* other endpoints were all decoupled from this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) dev->state = STATE_DEV_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) put_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static __poll_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) ep0_poll (struct file *fd, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) if (dev->state <= STATE_DEV_OPENED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) return DEFAULT_POLLMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) poll_wait(fd, &dev->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) /* report fd mode change before acting on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (dev->setup_abort) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) dev->setup_abort = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) mask = EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (dev->state == STATE_DEV_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) if (dev->setup_in || dev->setup_can_stall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) mask = EPOLLOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) if (dev->ev_next != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) mask = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) struct usb_gadget *gadget = dev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) long ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (dev->state == STATE_DEV_OPENED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) dev->state == STATE_DEV_UNBOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) /* Not bound to a UDC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) } else if (gadget->ops->ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) ret = gadget->ops->ioctl (gadget, code, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) /* The in-kernel gadget driver handles most ep0 issues, in particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) * enumerating the single configuration (as provided from user space).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) * Unrecognized ep0 requests may be handled in user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static void make_qualifier (struct dev_data *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct usb_qualifier_descriptor qual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) struct usb_device_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) qual.bLength = sizeof qual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) qual.bcdUSB = cpu_to_le16 (0x0200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) desc = dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) qual.bDeviceClass = desc->bDeviceClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) qual.bDeviceSubClass = desc->bDeviceSubClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) qual.bDeviceProtocol = desc->bDeviceProtocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /* assumes ep0 uses the same value for both speeds ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) qual.bNumConfigurations = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) qual.bRESERVED = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) memcpy (dev->rbuf, &qual, sizeof qual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) config_buf (struct dev_data *dev, u8 type, unsigned index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) int hs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) /* only one configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) if (index > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) if (gadget_is_dualspeed(dev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) hs = (dev->gadget->speed == USB_SPEED_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) if (type == USB_DT_OTHER_SPEED_CONFIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) hs = !hs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) if (hs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) dev->req->buf = dev->hs_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) len = le16_to_cpu(dev->hs_config->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) dev->req->buf = dev->config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) len = le16_to_cpu(dev->config->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) ((u8 *)dev->req->buf) [1] = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct dev_data *dev = get_gadget_data (gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) struct usb_request *req = dev->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) int value = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct usb_gadgetfs_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) u16 w_value = le16_to_cpu(ctrl->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) u16 w_length = le16_to_cpu(ctrl->wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (w_length > RBUF_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) if (ctrl->bRequestType & USB_DIR_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) /* Cast away the const, we are going to overwrite on purpose. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) __le16 *temp = (__le16 *)&ctrl->wLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) *temp = cpu_to_le16(RBUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) w_length = RBUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) spin_lock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) dev->setup_abort = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) if (dev->state == STATE_DEV_UNCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) if (gadget_is_dualspeed(gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) && gadget->speed == USB_SPEED_HIGH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) && dev->hs_config == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) spin_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) ERROR (dev, "no high speed config??\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) dev->state = STATE_DEV_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) INFO (dev, "connected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) event = next_event (dev, GADGETFS_CONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) event->u.speed = gadget->speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ep0_readable (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) /* host may have given up waiting for response. we can miss control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) * requests handled lower down (device/endpoint status and features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) * then ep0_{read,write} will report the wrong status. controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) * driver will have aborted pending i/o.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) } else if (dev->state == STATE_DEV_SETUP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) dev->setup_abort = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) req->buf = dev->rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) req->context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) switch (ctrl->bRequest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) case USB_REQ_GET_DESCRIPTOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (ctrl->bRequestType != USB_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) goto unrecognized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) switch (w_value >> 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) case USB_DT_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) value = min (w_length, (u16) sizeof *dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) req->buf = dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) case USB_DT_DEVICE_QUALIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) if (!dev->hs_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) value = min (w_length, (u16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) sizeof (struct usb_qualifier_descriptor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) make_qualifier (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) case USB_DT_OTHER_SPEED_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) case USB_DT_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) value = config_buf (dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) w_value >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) w_value & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) value = min (w_length, (u16) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) case USB_DT_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) goto unrecognized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) default: // all others are errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) /* currently one config, two speeds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) case USB_REQ_SET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) if (ctrl->bRequestType != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) goto unrecognized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (0 == (u8) w_value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) dev->current_config = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) usb_gadget_vbus_draw(gadget, 8 /* mA */ );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) // user mode expected to disable endpoints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) u8 config, power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (gadget_is_dualspeed(gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) && gadget->speed == USB_SPEED_HIGH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) config = dev->hs_config->bConfigurationValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) power = dev->hs_config->bMaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) config = dev->config->bConfigurationValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) power = dev->config->bMaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) if (config == (u8) w_value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) dev->current_config = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) usb_gadget_vbus_draw(gadget, 2 * power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) /* report SET_CONFIGURATION like any other control request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * except that usermode may not stall this. the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) * request mustn't be allowed start until this finishes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) * endpoints and threads set up, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) * has bad/racey automagic that prevents synchronizing here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) * even kernel mode drivers often miss them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (value == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) INFO (dev, "configuration #%d\n", dev->current_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) if (dev->usermode_setup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) dev->setup_can_stall = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) goto delegate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) #ifndef CONFIG_USB_PXA25X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) /* PXA automagically handles this request too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) case USB_REQ_GET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) if (ctrl->bRequestType != 0x80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) goto unrecognized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) *(u8 *)req->buf = dev->current_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) value = min (w_length, (u16) 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) unrecognized:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) dev->usermode_setup ? "delegate" : "fail",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) ctrl->bRequestType, ctrl->bRequest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) w_value, le16_to_cpu(ctrl->wIndex), w_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) /* if there's an ep0 reader, don't stall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (dev->usermode_setup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) dev->setup_can_stall = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) delegate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) dev->setup_wLength = w_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) dev->setup_out_ready = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) dev->setup_out_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) /* read DATA stage for OUT right away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) if (unlikely (!dev->setup_in && w_length)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) value = setup_req (gadget->ep0, dev->req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) w_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) spin_unlock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) value = usb_ep_queue (gadget->ep0, dev->req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) spin_lock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) clean_req (gadget->ep0, dev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) /* we can't currently stall these */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) dev->setup_can_stall = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) /* state changes when reader collects event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) event = next_event (dev, GADGETFS_SETUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) event->u.setup = *ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) ep0_readable (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) spin_unlock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) /* proceed with data transfer and status phases? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) if (value >= 0 && dev->state != STATE_DEV_SETUP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) req->length = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) req->zero = value < w_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) ++dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) spin_unlock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) spin_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) --dev->udc_usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) spin_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) DBG (dev, "ep_queue --> %d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) req->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) /* device stalls when value < 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) spin_unlock (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) static void destroy_ep_files (struct dev_data *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) DBG (dev, "%s %d\n", __func__, dev->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) /* dev->state must prevent interference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) while (!list_empty(&dev->epfiles)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct ep_data *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) struct inode *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) /* break link to FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) list_del_init (&ep->epfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) dentry = ep->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) ep->dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) parent = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) /* break link to controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) mutex_lock(&ep->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (ep->state == STATE_EP_ENABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) (void) usb_ep_disable (ep->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) ep->state = STATE_EP_UNBOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) usb_ep_free_request (ep->ep, ep->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) ep->ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) mutex_unlock(&ep->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) wake_up (&ep->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) put_ep (ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) /* break link to dcache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) inode_lock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) d_delete (dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) dput (dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) inode_unlock(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) static struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) gadgetfs_create_file (struct super_block *sb, char const *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) void *data, const struct file_operations *fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) static int activate_ep_files (struct dev_data *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) struct usb_ep *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) struct ep_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) gadget_for_each_ep (ep, dev->gadget) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) goto enomem0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) data->state = STATE_EP_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) init_waitqueue_head (&data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) strncpy (data->name, ep->name, sizeof (data->name) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) refcount_set (&data->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) data->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) get_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) data->ep = ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) ep->driver_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) if (!data->req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) goto enomem1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) data->dentry = gadgetfs_create_file (dev->sb, data->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) data, &ep_io_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) if (!data->dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) goto enomem2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) list_add_tail (&data->epfiles, &dev->epfiles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) enomem2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) usb_ep_free_request (ep, data->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) enomem1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) put_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) kfree (data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) enomem0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) DBG (dev, "%s enomem\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) destroy_ep_files (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) gadgetfs_unbind (struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) struct dev_data *dev = get_gadget_data (gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) DBG (dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) dev->state = STATE_DEV_UNBOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) while (dev->udc_usage > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) usleep_range(1000, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) destroy_ep_files (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) gadget->ep0->driver_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) set_gadget_data (gadget, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) /* we've already been disconnected ... no i/o is active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) if (dev->req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) usb_ep_free_request (gadget->ep0, dev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) DBG (dev, "%s done\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) put_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) static struct dev_data *the_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) static int gadgetfs_bind(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct usb_gadget_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) struct dev_data *dev = the_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (0 != strcmp (CHIP, gadget->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) pr_err("%s expected %s controller not %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) shortname, CHIP, gadget->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) set_gadget_data (gadget, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) dev->gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) gadget->ep0->driver_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) /* preallocate control response and buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) if (!dev->req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) goto enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) dev->req->context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) dev->req->complete = epio_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) if (activate_ep_files (dev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) goto enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) INFO (dev, "bound to %s driver\n", gadget->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) dev->state = STATE_DEV_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) get_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) enomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) gadgetfs_unbind (gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) gadgetfs_disconnect (struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) struct dev_data *dev = get_gadget_data (gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) spin_lock_irqsave (&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) if (dev->state == STATE_DEV_UNCONNECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) dev->state = STATE_DEV_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) INFO (dev, "disconnected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) next_event (dev, GADGETFS_DISCONNECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) ep0_readable (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) spin_unlock_irqrestore (&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) gadgetfs_suspend (struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) struct dev_data *dev = get_gadget_data (gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) INFO (dev, "suspended from state %d\n", dev->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) spin_lock_irqsave(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) switch (dev->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) case STATE_DEV_SETUP: // VERY odd... host died??
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) case STATE_DEV_CONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) case STATE_DEV_UNCONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) next_event (dev, GADGETFS_SUSPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) ep0_readable (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) spin_unlock_irqrestore(&dev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) static struct usb_gadget_driver gadgetfs_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) .function = (char *) driver_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) .bind = gadgetfs_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) .unbind = gadgetfs_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) .setup = gadgetfs_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) .reset = gadgetfs_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) .disconnect = gadgetfs_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) .suspend = gadgetfs_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) .name = shortname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) /* DEVICE INITIALIZATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) * fd = open ("/dev/gadget/$CHIP", O_RDWR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) * status = write (fd, descriptors, sizeof descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) * That write establishes the device configuration, so the kernel can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) * bind to the controller ... guaranteeing it can handle enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) * at all necessary speeds. Descriptor order is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) * . message tag (u32, host order) ... for now, must be zero; it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) * would change to support features like multi-config devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) * . full/low speed config ... all wTotalLength bytes (with interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) * class, altsetting, endpoint, and other descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) * . high speed config ... all descriptors, for high speed operation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) * this one's optional except for high-speed hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) * . device descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) * Endpoints are not yet enabled. Drivers must wait until device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) * configuration and interface altsetting changes create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) * the need to configure (or unconfigure) them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) * After initialization, the device stays active for as long as that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) * $CHIP file is open. Events must then be read from that descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) * such as configuration notifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) static int is_valid_config(struct usb_config_descriptor *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) unsigned int total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) return config->bDescriptorType == USB_DT_CONFIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) && config->bLength == USB_DT_CONFIG_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) && total >= USB_DT_CONFIG_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) && config->bConfigurationValue != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) /* FIXME check lengths: walk to end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) struct dev_data *dev = fd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) ssize_t value, length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) unsigned total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) u32 tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) char *kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) if (dev->state > STATE_DEV_OPENED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) value = ep0_write(fd, buf, len, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) (len > PAGE_SIZE * 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) /* we might need to change message format someday */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) if (copy_from_user (&tag, buf, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) if (tag != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) buf += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) length -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) kbuf = memdup_user(buf, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) if (IS_ERR(kbuf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) return PTR_ERR(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) spin_lock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) value = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) if (dev->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) kfree(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) dev->buf = kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) /* full or low speed config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) dev->config = (void *) kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) total = le16_to_cpu(dev->config->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) if (!is_valid_config(dev->config, total) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) total > length - USB_DT_DEVICE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) kbuf += total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) length -= total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) /* optional high speed config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) if (kbuf [1] == USB_DT_CONFIG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) dev->hs_config = (void *) kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) total = le16_to_cpu(dev->hs_config->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) if (!is_valid_config(dev->hs_config, total) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) total > length - USB_DT_DEVICE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) kbuf += total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) length -= total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) dev->hs_config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) /* could support multiple configs, using another encoding! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) /* device descriptor (tweaked for paranoia) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) if (length != USB_DT_DEVICE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) dev->dev = (void *)kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) if (dev->dev->bLength != USB_DT_DEVICE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) || dev->dev->bDescriptorType != USB_DT_DEVICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) || dev->dev->bNumConfigurations != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) dev->dev->bcdUSB = cpu_to_le16 (0x0200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) /* triggers gadgetfs_bind(); then we can enumerate. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) if (dev->hs_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) gadgetfs_driver.max_speed = USB_SPEED_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) gadgetfs_driver.max_speed = USB_SPEED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) value = usb_gadget_probe_driver(&gadgetfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (value != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) /* at this point "good" hardware has for the first time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) * let the USB the host see us. alternatively, if users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) * unplug/replug that will clear all the error state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) * note: everything running before here was guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) * to choke driver model style diagnostics. from here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) * on, they can work ... except in cleanup paths that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) * kick in after the ep0 descriptor is closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) value = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) dev->gadget_registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) dev->config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) dev->hs_config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) dev->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) spin_unlock_irq (&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) pr_debug ("%s: %s fail %zd, %p\n", shortname, __func__, value, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) kfree (dev->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) dev->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) dev_open (struct inode *inode, struct file *fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) struct dev_data *dev = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) int value = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) spin_lock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (dev->state == STATE_DEV_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) dev->ev_next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) dev->state = STATE_DEV_OPENED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) fd->private_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) get_dev (dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) spin_unlock_irq(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) static const struct file_operations ep0_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) .open = dev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) .read = ep0_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) .write = dev_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) .fasync = ep0_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) .poll = ep0_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) .unlocked_ioctl = dev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) .release = dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) /* FILESYSTEM AND SUPERBLOCK OPERATIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) * Mounting the filesystem creates a controller file, used first for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) * device configuration then later for event monitoring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) /* FIXME PAM etc could set this security policy without mount options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) * if epfiles inherited ownership and permissons from ep0 ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) static unsigned default_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) static unsigned default_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) static unsigned default_perm = S_IRUSR | S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) module_param (default_uid, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) module_param (default_gid, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) module_param (default_perm, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) static struct inode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) gadgetfs_make_inode (struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) void *data, const struct file_operations *fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) struct inode *inode = new_inode (sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) inode->i_ino = get_next_ino();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) inode->i_uid = make_kuid(&init_user_ns, default_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) inode->i_gid = make_kgid(&init_user_ns, default_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) inode->i_atime = inode->i_mtime = inode->i_ctime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) inode->i_private = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) inode->i_fop = fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) /* creates in fs root directory, so non-renamable and non-linkable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) * so inode and dentry are paired, until device reconfig.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) static struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) gadgetfs_create_file (struct super_block *sb, char const *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) void *data, const struct file_operations *fops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) dentry = d_alloc_name(sb->s_root, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) inode = gadgetfs_make_inode (sb, data, fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) S_IFREG | (default_perm & S_IRWXUGO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) d_add (dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) static const struct super_operations gadget_fs_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) .drop_inode = generic_delete_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) struct dev_data *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) if (the_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) CHIP = usb_get_gadget_udc_name();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) if (!CHIP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) /* superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) sb->s_blocksize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) sb->s_blocksize_bits = PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) sb->s_magic = GADGETFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) sb->s_op = &gadget_fs_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) sb->s_time_gran = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) /* root inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) inode = gadgetfs_make_inode (sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) NULL, &simple_dir_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) S_IFDIR | S_IRUGO | S_IXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) inode->i_op = &simple_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) if (!(sb->s_root = d_make_root (inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) /* the ep0 file is named after the controller we expect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) * user mode code can use it for sanity checks, like we do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) dev = dev_new ();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) dev->sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) if (!dev->dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) put_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) goto Enomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) /* other endpoint files are available after hardware setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) * from binding to a controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) the_device = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) Enomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) kfree(CHIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) CHIP = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) /* "mount -t gadgetfs path /dev/gadget" ends up here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) static int gadgetfs_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) return get_tree_single(fc, gadgetfs_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) static const struct fs_context_operations gadgetfs_context_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) .get_tree = gadgetfs_get_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) static int gadgetfs_init_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) fc->ops = &gadgetfs_context_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) gadgetfs_kill_sb (struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) kill_litter_super (sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) if (the_device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) put_dev (the_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) the_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) kfree(CHIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) CHIP = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) static struct file_system_type gadgetfs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) .name = shortname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) .init_fs_context = gadgetfs_init_fs_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) .kill_sb = gadgetfs_kill_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) MODULE_ALIAS_FS("gadgetfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) /*----------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) static int __init init (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) status = register_filesystem (&gadgetfs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) if (status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) pr_info ("%s: %s, version " DRIVER_VERSION "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) shortname, driver_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) module_init (init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) static void __exit cleanup (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) pr_debug ("unregister %s\n", shortname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) unregister_filesystem (&gadgetfs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) module_exit (cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124)